2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
15 #include "./vpx_config.h"
16 #include "./vpx_scale_rtcd.h"
18 #include "vp9/common/vp9_alloccommon.h"
19 #include "vp9/common/vp9_filter.h"
20 #include "vp9/common/vp9_idct.h"
21 #if CONFIG_VP9_POSTPROC
22 #include "vp9/common/vp9_postproc.h"
24 #include "vp9/common/vp9_reconinter.h"
25 #include "vp9/common/vp9_systemdependent.h"
26 #include "vp9/common/vp9_tile_common.h"
28 #include "vp9/encoder/vp9_encodemv.h"
29 #include "vp9/encoder/vp9_firstpass.h"
30 #include "vp9/encoder/vp9_mbgraph.h"
31 #include "vp9/encoder/vp9_onyx_int.h"
32 #include "vp9/encoder/vp9_picklpf.h"
33 #include "vp9/encoder/vp9_psnr.h"
34 #include "vp9/encoder/vp9_ratectrl.h"
35 #include "vp9/encoder/vp9_rdopt.h"
36 #include "vp9/encoder/vp9_segmentation.h"
37 #include "vp9/encoder/vp9_temporal_filter.h"
38 #include "vp9/encoder/vp9_vaq.h"
39 #include "vp9/encoder/vp9_resize.h"
41 #include "vpx_ports/vpx_timer.h"
43 void vp9_entropy_mode_init();
44 void vp9_coef_tree_initialize();
46 #define DEFAULT_INTERP_FILTER SWITCHABLE
48 #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
50 #define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
51 // for altref computation.
52 #define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
53 // mv. Choose a very high value for
54 // now so that HIGH_PRECISION is always
57 // Masks for partially or completely disabling split mode
58 #define DISABLE_ALL_SPLIT 0x3F
59 #define DISABLE_ALL_INTER_SPLIT 0x1F
60 #define DISABLE_COMPOUND_SPLIT 0x18
61 #define LAST_AND_INTRA_SPLIT_ONLY 0x1E
63 // Max rate target for 1080P and below encodes under normal circumstances
64 // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
65 #define MAX_MB_RATE 250
66 #define MAXRATE_1080P 2025000
68 #if CONFIG_INTERNAL_STATS
69 extern double vp9_calc_ssim(YV12_BUFFER_CONFIG
*source
,
70 YV12_BUFFER_CONFIG
*dest
, int lumamask
,
74 extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG
*source
,
75 YV12_BUFFER_CONFIG
*dest
, double *ssim_y
,
76 double *ssim_u
, double *ssim_v
);
81 // #define OUTPUT_YUV_REC
97 unsigned int frames_at_speed
[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101 #if defined(SECTIONBITS_OUTPUT)
102 extern unsigned __int64 Sectionbits
[500];
105 extern void vp9_init_quantizer(VP9_COMP
*cpi
);
107 static const double in_frame_q_adj_ratio
[MAX_SEGMENTS
] =
108 {1.0, 1.5, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0};
110 static INLINE
void Scale2Ratio(int mode
, int *hr
, int *hs
) {
136 static void set_high_precision_mv(VP9_COMP
*cpi
, int allow_high_precision_mv
) {
137 MACROBLOCK
*const mb
= &cpi
->mb
;
138 cpi
->common
.allow_high_precision_mv
= allow_high_precision_mv
;
139 if (cpi
->common
.allow_high_precision_mv
) {
140 mb
->mvcost
= mb
->nmvcost_hp
;
141 mb
->mvsadcost
= mb
->nmvsadcost_hp
;
143 mb
->mvcost
= mb
->nmvcost
;
144 mb
->mvsadcost
= mb
->nmvsadcost
;
148 void vp9_initialize_enc() {
149 static int init_done
= 0;
152 vp9_initialize_common();
153 vp9_coef_tree_initialize();
154 vp9_tokenize_initialize();
155 vp9_init_quant_tables();
157 vp9_rc_init_minq_luts();
158 // init_base_skip_probs();
159 vp9_entropy_mv_init();
160 vp9_entropy_mode_init();
165 static void dealloc_compressor_data(VP9_COMP
*cpi
) {
166 // Delete sementation map
167 vpx_free(cpi
->segmentation_map
);
168 cpi
->segmentation_map
= 0;
169 vpx_free(cpi
->common
.last_frame_seg_map
);
170 cpi
->common
.last_frame_seg_map
= 0;
171 vpx_free(cpi
->coding_context
.last_frame_seg_map_copy
);
172 cpi
->coding_context
.last_frame_seg_map_copy
= 0;
174 vpx_free(cpi
->complexity_map
);
175 cpi
->complexity_map
= 0;
176 vpx_free(cpi
->active_map
);
179 vp9_free_frame_buffers(&cpi
->common
);
181 vp9_free_frame_buffer(&cpi
->last_frame_uf
);
182 vp9_free_frame_buffer(&cpi
->scaled_source
);
183 vp9_free_frame_buffer(&cpi
->alt_ref_buffer
);
184 vp9_lookahead_destroy(cpi
->lookahead
);
189 // Activity mask based per mb zbin adjustments
190 vpx_free(cpi
->mb_activity_map
);
191 cpi
->mb_activity_map
= 0;
192 vpx_free(cpi
->mb_norm_activity_map
);
193 cpi
->mb_norm_activity_map
= 0;
195 vpx_free(cpi
->above_context
[0]);
196 cpi
->above_context
[0] = NULL
;
198 vpx_free(cpi
->above_seg_context
);
199 cpi
->above_seg_context
= NULL
;
202 // Computes a q delta (in "q index" terms) to get from a starting q value
205 int vp9_compute_qdelta(const VP9_COMP
*cpi
, double qstart
, double qtarget
) {
207 int start_index
= cpi
->rc
.worst_quality
;
208 int target_index
= cpi
->rc
.worst_quality
;
210 // Convert the average q value to an index.
211 for (i
= cpi
->rc
.best_quality
; i
< cpi
->rc
.worst_quality
; i
++) {
213 if (vp9_convert_qindex_to_q(i
) >= qstart
)
217 // Convert the q target to an index
218 for (i
= cpi
->rc
.best_quality
; i
< cpi
->rc
.worst_quality
; i
++) {
220 if (vp9_convert_qindex_to_q(i
) >= qtarget
)
224 return target_index
- start_index
;
227 // Computes a q delta (in "q index" terms) to get from a starting q value
228 // to a value that should equate to thegiven rate ratio.
230 int vp9_compute_qdelta_by_rate(VP9_COMP
*cpi
,
231 double base_q_index
, double rate_target_ratio
) {
233 int base_bits_per_mb
;
234 int target_bits_per_mb
;
235 int target_index
= cpi
->rc
.worst_quality
;
237 // Make SURE use of floating point in this function is safe.
238 vp9_clear_system_state();
240 // Look up the current projected bits per block for the base index
241 base_bits_per_mb
= vp9_rc_bits_per_mb(cpi
->common
.frame_type
,
244 // Find the target bits per mb based on the base value and given ratio.
245 target_bits_per_mb
= rate_target_ratio
* base_bits_per_mb
;
247 // Convert the q target to an index
248 for (i
= cpi
->rc
.best_quality
; i
< cpi
->rc
.worst_quality
; i
++) {
250 if (vp9_rc_bits_per_mb(cpi
->common
.frame_type
,
251 i
, 1.0) <= target_bits_per_mb
)
255 return target_index
- base_q_index
;
258 // This function sets up a set of segments with delta Q values around
259 // the baseline frame quantizer.
260 static void setup_in_frame_q_adj(VP9_COMP
*cpi
) {
261 VP9_COMMON
*cm
= &cpi
->common
;
262 struct segmentation
*seg
= &cm
->seg
;
267 // Make SURE use of floating point in this function is safe.
268 vp9_clear_system_state();
270 if (cm
->frame_type
== KEY_FRAME
||
271 cpi
->refresh_alt_ref_frame
||
272 (cpi
->refresh_golden_frame
&& !cpi
->rc
.is_src_frame_alt_ref
)) {
273 // Clear down the segment map
274 vpx_memset(cpi
->segmentation_map
, 0, cm
->mi_rows
* cm
->mi_cols
);
276 // Clear down the complexity map used for rd
277 vpx_memset(cpi
->complexity_map
, 0, cm
->mi_rows
* cm
->mi_cols
);
279 // Enable segmentation
280 vp9_enable_segmentation((VP9_PTR
)cpi
);
281 vp9_clearall_segfeatures(seg
);
283 // Select delta coding method
284 seg
->abs_delta
= SEGMENT_DELTADATA
;
286 // Segment 0 "Q" feature is disabled so it defaults to the baseline Q
287 vp9_disable_segfeature(seg
, 0, SEG_LVL_ALT_Q
);
289 // Use some of the segments for in frame Q adjustment
290 for (segment
= 1; segment
< 3; segment
++) {
292 vp9_compute_qdelta_by_rate(cpi
, cm
->base_qindex
,
293 in_frame_q_adj_ratio
[segment
]);
294 vp9_enable_segfeature(seg
, segment
, SEG_LVL_ALT_Q
);
295 vp9_set_segdata(seg
, segment
, SEG_LVL_ALT_Q
, qindex_delta
);
299 static void configure_static_seg_features(VP9_COMP
*cpi
) {
300 VP9_COMMON
*cm
= &cpi
->common
;
301 struct segmentation
*seg
= &cm
->seg
;
303 int high_q
= (int)(cpi
->rc
.avg_q
> 48.0);
306 // Disable and clear down for KF
307 if (cm
->frame_type
== KEY_FRAME
) {
308 // Clear down the global segmentation map
309 vpx_memset(cpi
->segmentation_map
, 0, cm
->mi_rows
* cm
->mi_cols
);
311 seg
->update_data
= 0;
312 cpi
->static_mb_pct
= 0;
314 // Disable segmentation
315 vp9_disable_segmentation((VP9_PTR
)cpi
);
317 // Clear down the segment features.
318 vp9_clearall_segfeatures(seg
);
319 } else if (cpi
->refresh_alt_ref_frame
) {
320 // If this is an alt ref frame
321 // Clear down the global segmentation map
322 vpx_memset(cpi
->segmentation_map
, 0, cm
->mi_rows
* cm
->mi_cols
);
324 seg
->update_data
= 0;
325 cpi
->static_mb_pct
= 0;
327 // Disable segmentation and individual segment features by default
328 vp9_disable_segmentation((VP9_PTR
)cpi
);
329 vp9_clearall_segfeatures(seg
);
331 // Scan frames from current to arf frame.
332 // This function re-enables segmentation if appropriate.
333 vp9_update_mbgraph_stats(cpi
);
335 // If segmentation was enabled set those features needed for the
339 seg
->update_data
= 1;
341 qi_delta
= vp9_compute_qdelta(
342 cpi
, cpi
->rc
.avg_q
, (cpi
->rc
.avg_q
* 0.875));
343 vp9_set_segdata(seg
, 1, SEG_LVL_ALT_Q
, (qi_delta
- 2));
344 vp9_set_segdata(seg
, 1, SEG_LVL_ALT_LF
, -2);
346 vp9_enable_segfeature(seg
, 1, SEG_LVL_ALT_Q
);
347 vp9_enable_segfeature(seg
, 1, SEG_LVL_ALT_LF
);
349 // Where relevant assume segment data is delta data
350 seg
->abs_delta
= SEGMENT_DELTADATA
;
352 } else if (seg
->enabled
) {
353 // All other frames if segmentation has been enabled
355 // First normal frame in a valid gf or alt ref group
356 if (cpi
->rc
.frames_since_golden
== 0) {
357 // Set up segment features for normal frames in an arf group
358 if (cpi
->rc
.source_alt_ref_active
) {
360 seg
->update_data
= 1;
361 seg
->abs_delta
= SEGMENT_DELTADATA
;
363 qi_delta
= vp9_compute_qdelta(cpi
, cpi
->rc
.avg_q
,
364 (cpi
->rc
.avg_q
* 1.125));
365 vp9_set_segdata(seg
, 1, SEG_LVL_ALT_Q
, (qi_delta
+ 2));
366 vp9_enable_segfeature(seg
, 1, SEG_LVL_ALT_Q
);
368 vp9_set_segdata(seg
, 1, SEG_LVL_ALT_LF
, -2);
369 vp9_enable_segfeature(seg
, 1, SEG_LVL_ALT_LF
);
371 // Segment coding disabled for compred testing
372 if (high_q
|| (cpi
->static_mb_pct
== 100)) {
373 vp9_set_segdata(seg
, 1, SEG_LVL_REF_FRAME
, ALTREF_FRAME
);
374 vp9_enable_segfeature(seg
, 1, SEG_LVL_REF_FRAME
);
375 vp9_enable_segfeature(seg
, 1, SEG_LVL_SKIP
);
378 // Disable segmentation and clear down features if alt ref
379 // is not active for this group
381 vp9_disable_segmentation((VP9_PTR
)cpi
);
383 vpx_memset(cpi
->segmentation_map
, 0, cm
->mi_rows
* cm
->mi_cols
);
386 seg
->update_data
= 0;
388 vp9_clearall_segfeatures(seg
);
390 } else if (cpi
->rc
.is_src_frame_alt_ref
) {
391 // Special case where we are coding over the top of a previous
393 // Segment coding disabled for compred testing
395 // Enable ref frame features for segment 0 as well
396 vp9_enable_segfeature(seg
, 0, SEG_LVL_REF_FRAME
);
397 vp9_enable_segfeature(seg
, 1, SEG_LVL_REF_FRAME
);
399 // All mbs should use ALTREF_FRAME
400 vp9_clear_segdata(seg
, 0, SEG_LVL_REF_FRAME
);
401 vp9_set_segdata(seg
, 0, SEG_LVL_REF_FRAME
, ALTREF_FRAME
);
402 vp9_clear_segdata(seg
, 1, SEG_LVL_REF_FRAME
);
403 vp9_set_segdata(seg
, 1, SEG_LVL_REF_FRAME
, ALTREF_FRAME
);
405 // Skip all MBs if high Q (0,0 mv and skip coeffs)
407 vp9_enable_segfeature(seg
, 0, SEG_LVL_SKIP
);
408 vp9_enable_segfeature(seg
, 1, SEG_LVL_SKIP
);
410 // Enable data update
411 seg
->update_data
= 1;
415 // No updates.. leave things as they are.
417 seg
->update_data
= 0;
422 // DEBUG: Print out the segment id of each MB in the current frame.
423 static void print_seg_map(VP9_COMP
*cpi
) {
424 VP9_COMMON
*cm
= &cpi
->common
;
427 FILE *statsfile
= fopen("segmap.stt", "a");
429 fprintf(statsfile
, "%10d\n", cm
->current_video_frame
);
431 for (row
= 0; row
< cpi
->common
.mi_rows
; row
++) {
432 for (col
= 0; col
< cpi
->common
.mi_cols
; col
++) {
433 fprintf(statsfile
, "%10d", cpi
->segmentation_map
[map_index
]);
436 fprintf(statsfile
, "\n");
438 fprintf(statsfile
, "\n");
443 static void update_reference_segmentation_map(VP9_COMP
*cpi
) {
444 VP9_COMMON
*const cm
= &cpi
->common
;
446 MODE_INFO
**mi_8x8
, **mi_8x8_ptr
= cm
->mi_grid_visible
;
447 uint8_t *cache_ptr
= cm
->last_frame_seg_map
, *cache
;
449 for (row
= 0; row
< cm
->mi_rows
; row
++) {
452 for (col
= 0; col
< cm
->mi_cols
; col
++, mi_8x8
++, cache
++)
453 cache
[0] = mi_8x8
[0]->mbmi
.segment_id
;
454 mi_8x8_ptr
+= cm
->mode_info_stride
;
455 cache_ptr
+= cm
->mi_cols
;
459 static void set_rd_speed_thresholds(VP9_COMP
*cpi
, int mode
) {
460 SPEED_FEATURES
*sf
= &cpi
->sf
;
463 // Set baseline threshold values
464 for (i
= 0; i
< MAX_MODES
; ++i
)
465 sf
->thresh_mult
[i
] = mode
== 0 ? -500 : 0;
467 sf
->thresh_mult
[THR_NEARESTMV
] = 0;
468 sf
->thresh_mult
[THR_NEARESTG
] = 0;
469 sf
->thresh_mult
[THR_NEARESTA
] = 0;
471 sf
->thresh_mult
[THR_DC
] += 1000;
473 sf
->thresh_mult
[THR_NEWMV
] += 1000;
474 sf
->thresh_mult
[THR_NEWA
] += 1000;
475 sf
->thresh_mult
[THR_NEWG
] += 1000;
477 sf
->thresh_mult
[THR_NEARMV
] += 1000;
478 sf
->thresh_mult
[THR_NEARA
] += 1000;
479 sf
->thresh_mult
[THR_COMP_NEARESTLA
] += 1000;
480 sf
->thresh_mult
[THR_COMP_NEARESTGA
] += 1000;
482 sf
->thresh_mult
[THR_TM
] += 1000;
484 sf
->thresh_mult
[THR_COMP_NEARLA
] += 1500;
485 sf
->thresh_mult
[THR_COMP_NEWLA
] += 2000;
486 sf
->thresh_mult
[THR_NEARG
] += 1000;
487 sf
->thresh_mult
[THR_COMP_NEARGA
] += 1500;
488 sf
->thresh_mult
[THR_COMP_NEWGA
] += 2000;
490 sf
->thresh_mult
[THR_ZEROMV
] += 2000;
491 sf
->thresh_mult
[THR_ZEROG
] += 2000;
492 sf
->thresh_mult
[THR_ZEROA
] += 2000;
493 sf
->thresh_mult
[THR_COMP_ZEROLA
] += 2500;
494 sf
->thresh_mult
[THR_COMP_ZEROGA
] += 2500;
496 sf
->thresh_mult
[THR_H_PRED
] += 2000;
497 sf
->thresh_mult
[THR_V_PRED
] += 2000;
498 sf
->thresh_mult
[THR_D45_PRED
] += 2500;
499 sf
->thresh_mult
[THR_D135_PRED
] += 2500;
500 sf
->thresh_mult
[THR_D117_PRED
] += 2500;
501 sf
->thresh_mult
[THR_D153_PRED
] += 2500;
502 sf
->thresh_mult
[THR_D207_PRED
] += 2500;
503 sf
->thresh_mult
[THR_D63_PRED
] += 2500;
505 /* disable frame modes if flags not set */
506 if (!(cpi
->ref_frame_flags
& VP9_LAST_FLAG
)) {
507 sf
->thresh_mult
[THR_NEWMV
] = INT_MAX
;
508 sf
->thresh_mult
[THR_NEARESTMV
] = INT_MAX
;
509 sf
->thresh_mult
[THR_ZEROMV
] = INT_MAX
;
510 sf
->thresh_mult
[THR_NEARMV
] = INT_MAX
;
512 if (!(cpi
->ref_frame_flags
& VP9_GOLD_FLAG
)) {
513 sf
->thresh_mult
[THR_NEARESTG
] = INT_MAX
;
514 sf
->thresh_mult
[THR_ZEROG
] = INT_MAX
;
515 sf
->thresh_mult
[THR_NEARG
] = INT_MAX
;
516 sf
->thresh_mult
[THR_NEWG
] = INT_MAX
;
518 if (!(cpi
->ref_frame_flags
& VP9_ALT_FLAG
)) {
519 sf
->thresh_mult
[THR_NEARESTA
] = INT_MAX
;
520 sf
->thresh_mult
[THR_ZEROA
] = INT_MAX
;
521 sf
->thresh_mult
[THR_NEARA
] = INT_MAX
;
522 sf
->thresh_mult
[THR_NEWA
] = INT_MAX
;
525 if ((cpi
->ref_frame_flags
& (VP9_LAST_FLAG
| VP9_ALT_FLAG
)) !=
526 (VP9_LAST_FLAG
| VP9_ALT_FLAG
)) {
527 sf
->thresh_mult
[THR_COMP_ZEROLA
] = INT_MAX
;
528 sf
->thresh_mult
[THR_COMP_NEARESTLA
] = INT_MAX
;
529 sf
->thresh_mult
[THR_COMP_NEARLA
] = INT_MAX
;
530 sf
->thresh_mult
[THR_COMP_NEWLA
] = INT_MAX
;
532 if ((cpi
->ref_frame_flags
& (VP9_GOLD_FLAG
| VP9_ALT_FLAG
)) !=
533 (VP9_GOLD_FLAG
| VP9_ALT_FLAG
)) {
534 sf
->thresh_mult
[THR_COMP_ZEROGA
] = INT_MAX
;
535 sf
->thresh_mult
[THR_COMP_NEARESTGA
] = INT_MAX
;
536 sf
->thresh_mult
[THR_COMP_NEARGA
] = INT_MAX
;
537 sf
->thresh_mult
[THR_COMP_NEWGA
] = INT_MAX
;
541 static void set_rd_speed_thresholds_sub8x8(VP9_COMP
*cpi
, int mode
) {
542 SPEED_FEATURES
*sf
= &cpi
->sf
;
545 for (i
= 0; i
< MAX_REFS
; ++i
)
546 sf
->thresh_mult_sub8x8
[i
] = mode
== 0 ? -500 : 0;
548 sf
->thresh_mult_sub8x8
[THR_LAST
] += 2500;
549 sf
->thresh_mult_sub8x8
[THR_GOLD
] += 2500;
550 sf
->thresh_mult_sub8x8
[THR_ALTR
] += 2500;
551 sf
->thresh_mult_sub8x8
[THR_INTRA
] += 2500;
552 sf
->thresh_mult_sub8x8
[THR_COMP_LA
] += 4500;
553 sf
->thresh_mult_sub8x8
[THR_COMP_GA
] += 4500;
555 // Check for masked out split cases.
556 for (i
= 0; i
< MAX_REFS
; i
++) {
557 if (sf
->disable_split_mask
& (1 << i
))
558 sf
->thresh_mult_sub8x8
[i
] = INT_MAX
;
561 // disable mode test if frame flag is not set
562 if (!(cpi
->ref_frame_flags
& VP9_LAST_FLAG
))
563 sf
->thresh_mult_sub8x8
[THR_LAST
] = INT_MAX
;
564 if (!(cpi
->ref_frame_flags
& VP9_GOLD_FLAG
))
565 sf
->thresh_mult_sub8x8
[THR_GOLD
] = INT_MAX
;
566 if (!(cpi
->ref_frame_flags
& VP9_ALT_FLAG
))
567 sf
->thresh_mult_sub8x8
[THR_ALTR
] = INT_MAX
;
568 if ((cpi
->ref_frame_flags
& (VP9_LAST_FLAG
| VP9_ALT_FLAG
)) !=
569 (VP9_LAST_FLAG
| VP9_ALT_FLAG
))
570 sf
->thresh_mult_sub8x8
[THR_COMP_LA
] = INT_MAX
;
571 if ((cpi
->ref_frame_flags
& (VP9_GOLD_FLAG
| VP9_ALT_FLAG
)) !=
572 (VP9_GOLD_FLAG
| VP9_ALT_FLAG
))
573 sf
->thresh_mult_sub8x8
[THR_COMP_GA
] = INT_MAX
;
576 static void set_good_speed_feature(VP9_COMMON
*cm
,
580 sf
->adaptive_rd_thresh
= 1;
581 sf
->recode_loop
= (speed
< 1);
583 sf
->use_square_partition_only
= !frame_is_intra_only(cm
);
584 sf
->less_rectangular_check
= 1;
585 sf
->tx_size_search_method
= frame_is_intra_only(cm
)
586 ? USE_FULL_RD
: USE_LARGESTALL
;
588 if (MIN(cm
->width
, cm
->height
) >= 720)
589 sf
->disable_split_mask
= cm
->show_frame
?
590 DISABLE_ALL_SPLIT
: DISABLE_ALL_INTER_SPLIT
;
592 sf
->disable_split_mask
= DISABLE_COMPOUND_SPLIT
;
594 sf
->use_rd_breakout
= 1;
595 sf
->adaptive_motion_search
= 1;
596 sf
->adaptive_pred_interp_filter
= 1;
597 sf
->auto_mv_step_size
= 1;
598 sf
->adaptive_rd_thresh
= 2;
600 sf
->intra_y_mode_mask
[TX_32X32
] = INTRA_DC_H_V
;
601 sf
->intra_uv_mode_mask
[TX_32X32
] = INTRA_DC_H_V
;
602 sf
->intra_uv_mode_mask
[TX_16X16
] = INTRA_DC_H_V
;
605 sf
->use_square_partition_only
= !frame_is_intra_only(cm
);
606 sf
->less_rectangular_check
= 1;
607 sf
->tx_size_search_method
= frame_is_intra_only(cm
)
608 ? USE_FULL_RD
: USE_LARGESTALL
;
610 if (MIN(cm
->width
, cm
->height
) >= 720)
611 sf
->disable_split_mask
= cm
->show_frame
?
612 DISABLE_ALL_SPLIT
: DISABLE_ALL_INTER_SPLIT
;
614 sf
->disable_split_mask
= LAST_AND_INTRA_SPLIT_ONLY
;
616 sf
->mode_search_skip_flags
= FLAG_SKIP_INTRA_DIRMISMATCH
|
617 FLAG_SKIP_INTRA_BESTINTER
|
618 FLAG_SKIP_COMP_BESTINTRA
|
619 FLAG_SKIP_INTRA_LOWVAR
;
620 sf
->use_rd_breakout
= 1;
621 sf
->adaptive_motion_search
= 1;
622 sf
->adaptive_pred_interp_filter
= 2;
623 sf
->reference_masking
= 1;
624 sf
->auto_mv_step_size
= 1;
626 sf
->disable_filter_search_var_thresh
= 50;
627 sf
->comp_inter_joint_search_thresh
= BLOCK_SIZES
;
629 sf
->auto_min_max_partition_size
= RELAXED_NEIGHBORING_MIN_MAX
;
630 sf
->use_lastframe_partitioning
= LAST_FRAME_PARTITION_LOW_MOTION
;
631 sf
->adjust_partitioning_from_last_frame
= 1;
632 sf
->last_partitioning_redo_frequency
= 3;
634 sf
->adaptive_rd_thresh
= 2;
636 sf
->use_lp32x32fdct
= 1;
637 sf
->mode_skip_start
= 11;
638 sf
->intra_y_mode_mask
[TX_32X32
] = INTRA_DC_H_V
;
639 sf
->intra_y_mode_mask
[TX_16X16
] = INTRA_DC_H_V
;
640 sf
->intra_uv_mode_mask
[TX_32X32
] = INTRA_DC_H_V
;
641 sf
->intra_uv_mode_mask
[TX_16X16
] = INTRA_DC_H_V
;
644 sf
->use_square_partition_only
= 1;
645 sf
->tx_size_search_method
= USE_LARGESTALL
;
647 if (MIN(cm
->width
, cm
->height
) >= 720)
648 sf
->disable_split_mask
= DISABLE_ALL_SPLIT
;
650 sf
->disable_split_mask
= DISABLE_ALL_INTER_SPLIT
;
652 sf
->mode_search_skip_flags
= FLAG_SKIP_INTRA_DIRMISMATCH
|
653 FLAG_SKIP_INTRA_BESTINTER
|
654 FLAG_SKIP_COMP_BESTINTRA
|
655 FLAG_SKIP_INTRA_LOWVAR
;
657 sf
->use_rd_breakout
= 1;
658 sf
->adaptive_motion_search
= 1;
659 sf
->adaptive_pred_interp_filter
= 2;
660 sf
->reference_masking
= 1;
661 sf
->auto_mv_step_size
= 1;
663 sf
->disable_filter_search_var_thresh
= 100;
664 sf
->comp_inter_joint_search_thresh
= BLOCK_SIZES
;
666 sf
->auto_min_max_partition_size
= RELAXED_NEIGHBORING_MIN_MAX
;
667 sf
->use_lastframe_partitioning
= LAST_FRAME_PARTITION_ALL
;
668 sf
->adjust_partitioning_from_last_frame
= 1;
669 sf
->last_partitioning_redo_frequency
= 3;
671 sf
->use_uv_intra_rd_estimate
= 1;
672 sf
->skip_encode_sb
= 1;
673 sf
->use_lp32x32fdct
= 1;
674 sf
->subpel_iters_per_step
= 1;
675 sf
->use_fast_coef_updates
= 2;
677 sf
->adaptive_rd_thresh
= 4;
678 sf
->mode_skip_start
= 6;
681 sf
->use_square_partition_only
= 1;
682 sf
->tx_size_search_method
= USE_LARGESTALL
;
683 sf
->disable_split_mask
= DISABLE_ALL_SPLIT
;
685 sf
->mode_search_skip_flags
= FLAG_SKIP_INTRA_DIRMISMATCH
|
686 FLAG_SKIP_INTRA_BESTINTER
|
687 FLAG_SKIP_COMP_BESTINTRA
|
688 FLAG_SKIP_COMP_REFMISMATCH
|
689 FLAG_SKIP_INTRA_LOWVAR
|
690 FLAG_EARLY_TERMINATE
;
692 sf
->use_rd_breakout
= 1;
693 sf
->adaptive_motion_search
= 1;
694 sf
->adaptive_pred_interp_filter
= 2;
695 sf
->reference_masking
= 1;
696 sf
->auto_mv_step_size
= 1;
698 sf
->disable_filter_search_var_thresh
= 200;
699 sf
->comp_inter_joint_search_thresh
= BLOCK_SIZES
;
701 sf
->auto_min_max_partition_size
= RELAXED_NEIGHBORING_MIN_MAX
;
702 sf
->use_lastframe_partitioning
= LAST_FRAME_PARTITION_ALL
;
703 sf
->adjust_partitioning_from_last_frame
= 1;
704 sf
->last_partitioning_redo_frequency
= 3;
706 sf
->use_uv_intra_rd_estimate
= 1;
707 sf
->skip_encode_sb
= 1;
708 sf
->use_lp32x32fdct
= 1;
709 sf
->subpel_iters_per_step
= 1;
710 sf
->use_fast_coef_updates
= 2;
712 sf
->adaptive_rd_thresh
= 4;
713 sf
->mode_skip_start
= 6;
716 sf
->comp_inter_joint_search_thresh
= BLOCK_SIZES
;
717 sf
->use_one_partition_size_always
= 1;
718 sf
->always_this_block_size
= BLOCK_16X16
;
719 sf
->tx_size_search_method
= frame_is_intra_only(cm
) ?
720 USE_FULL_RD
: USE_LARGESTALL
;
721 sf
->mode_search_skip_flags
= FLAG_SKIP_INTRA_DIRMISMATCH
|
722 FLAG_SKIP_INTRA_BESTINTER
|
723 FLAG_SKIP_COMP_BESTINTRA
|
724 FLAG_SKIP_COMP_REFMISMATCH
|
725 FLAG_SKIP_INTRA_LOWVAR
|
726 FLAG_EARLY_TERMINATE
;
727 sf
->use_rd_breakout
= 1;
728 sf
->use_lp32x32fdct
= 1;
729 sf
->optimize_coefficients
= 0;
730 sf
->auto_mv_step_size
= 1;
731 sf
->reference_masking
= 1;
733 sf
->disable_split_mask
= DISABLE_ALL_SPLIT
;
734 sf
->search_method
= HEX
;
735 sf
->subpel_iters_per_step
= 1;
736 sf
->disable_split_var_thresh
= 64;
737 sf
->disable_filter_search_var_thresh
= 500;
738 for (i
= 0; i
< TX_SIZES
; i
++) {
739 sf
->intra_y_mode_mask
[i
] = INTRA_DC_ONLY
;
740 sf
->intra_uv_mode_mask
[i
] = INTRA_DC_ONLY
;
742 sf
->use_fast_coef_updates
= 2;
743 sf
->adaptive_rd_thresh
= 4;
744 sf
->mode_skip_start
= 6;
747 static void set_rt_speed_feature(VP9_COMMON
*cm
,
750 sf
->static_segmentation
= 0;
751 sf
->adaptive_rd_thresh
= 1;
752 sf
->recode_loop
= (speed
< 1);
754 sf
->use_square_partition_only
= !frame_is_intra_only(cm
);
755 sf
->less_rectangular_check
= 1;
756 sf
->tx_size_search_method
=
757 frame_is_intra_only(cm
) ? USE_FULL_RD
: USE_LARGESTALL
;
759 if (MIN(cm
->width
, cm
->height
) >= 720)
760 sf
->disable_split_mask
= cm
->show_frame
?
761 DISABLE_ALL_SPLIT
: DISABLE_ALL_INTER_SPLIT
;
763 sf
->disable_split_mask
= DISABLE_COMPOUND_SPLIT
;
765 sf
->use_rd_breakout
= 1;
766 sf
->adaptive_motion_search
= 1;
767 sf
->adaptive_pred_interp_filter
= 1;
768 sf
->auto_mv_step_size
= 1;
769 sf
->adaptive_rd_thresh
= 2;
771 sf
->intra_y_mode_mask
[TX_32X32
] = INTRA_DC_H_V
;
772 sf
->intra_uv_mode_mask
[TX_32X32
] = INTRA_DC_H_V
;
773 sf
->intra_uv_mode_mask
[TX_16X16
] = INTRA_DC_H_V
;
776 sf
->use_square_partition_only
= !frame_is_intra_only(cm
);
777 sf
->less_rectangular_check
= 1;
778 sf
->tx_size_search_method
=
779 frame_is_intra_only(cm
) ? USE_FULL_RD
: USE_LARGESTALL
;
781 if (MIN(cm
->width
, cm
->height
) >= 720)
782 sf
->disable_split_mask
= cm
->show_frame
?
783 DISABLE_ALL_SPLIT
: DISABLE_ALL_INTER_SPLIT
;
785 sf
->disable_split_mask
= LAST_AND_INTRA_SPLIT_ONLY
;
787 sf
->mode_search_skip_flags
= FLAG_SKIP_INTRA_DIRMISMATCH
788 | FLAG_SKIP_INTRA_BESTINTER
| FLAG_SKIP_COMP_BESTINTRA
789 | FLAG_SKIP_INTRA_LOWVAR
;
791 sf
->use_rd_breakout
= 1;
792 sf
->adaptive_motion_search
= 1;
793 sf
->adaptive_pred_interp_filter
= 2;
794 sf
->auto_mv_step_size
= 1;
795 sf
->reference_masking
= 1;
797 sf
->disable_filter_search_var_thresh
= 50;
798 sf
->comp_inter_joint_search_thresh
= BLOCK_SIZES
;
800 sf
->auto_min_max_partition_size
= RELAXED_NEIGHBORING_MIN_MAX
;
801 sf
->use_lastframe_partitioning
= LAST_FRAME_PARTITION_LOW_MOTION
;
802 sf
->adjust_partitioning_from_last_frame
= 1;
803 sf
->last_partitioning_redo_frequency
= 3;
805 sf
->adaptive_rd_thresh
= 2;
807 sf
->use_lp32x32fdct
= 1;
808 sf
->mode_skip_start
= 11;
809 sf
->intra_y_mode_mask
[TX_32X32
] = INTRA_DC_H_V
;
810 sf
->intra_y_mode_mask
[TX_16X16
] = INTRA_DC_H_V
;
811 sf
->intra_uv_mode_mask
[TX_32X32
] = INTRA_DC_H_V
;
812 sf
->intra_uv_mode_mask
[TX_16X16
] = INTRA_DC_H_V
;
815 sf
->use_square_partition_only
= 1;
816 sf
->tx_size_search_method
= USE_LARGESTALL
;
818 if (MIN(cm
->width
, cm
->height
) >= 720)
819 sf
->disable_split_mask
= DISABLE_ALL_SPLIT
;
821 sf
->disable_split_mask
= DISABLE_ALL_INTER_SPLIT
;
823 sf
->mode_search_skip_flags
= FLAG_SKIP_INTRA_DIRMISMATCH
824 | FLAG_SKIP_INTRA_BESTINTER
| FLAG_SKIP_COMP_BESTINTRA
825 | FLAG_SKIP_INTRA_LOWVAR
;
827 sf
->disable_filter_search_var_thresh
= 100;
828 sf
->use_lastframe_partitioning
= LAST_FRAME_PARTITION_ALL
;
829 sf
->use_uv_intra_rd_estimate
= 1;
830 sf
->skip_encode_sb
= 1;
831 sf
->subpel_iters_per_step
= 1;
832 sf
->use_fast_coef_updates
= 2;
833 sf
->adaptive_rd_thresh
= 4;
834 sf
->mode_skip_start
= 6;
837 sf
->optimize_coefficients
= 0;
841 sf
->disable_split_mask
= DISABLE_ALL_SPLIT
;
842 sf
->auto_min_max_partition_size
= frame_is_intra_only(cm
) ?
843 RELAXED_NEIGHBORING_MIN_MAX
: STRICT_NEIGHBORING_MIN_MAX
;
844 sf
->subpel_force_stop
= 1;
845 for (i
= 0; i
< TX_SIZES
; i
++) {
846 sf
->intra_y_mode_mask
[i
] = INTRA_DC_H_V
;
847 sf
->intra_uv_mode_mask
[i
] = INTRA_DC_ONLY
;
849 sf
->use_fast_lpf_pick
= 2;
853 void vp9_set_speed_features(VP9_COMP
*cpi
) {
854 SPEED_FEATURES
*sf
= &cpi
->sf
;
855 VP9_COMMON
*cm
= &cpi
->common
;
856 int mode
= cpi
->compressor_speed
;
857 int speed
= cpi
->speed
;
860 // Convert negative speed to positive
864 for (i
= 0; i
< MAX_MODES
; ++i
)
865 cpi
->mode_chosen_counts
[i
] = 0;
867 // best quality defaults
869 sf
->search_method
= NSTEP
;
871 sf
->subpel_search_method
= SUBPEL_TREE
;
872 sf
->subpel_iters_per_step
= 2;
873 sf
->subpel_force_stop
= 0;
874 sf
->optimize_coefficients
= !cpi
->oxcf
.lossless
;
875 sf
->reduce_first_step_size
= 0;
876 sf
->auto_mv_step_size
= 0;
877 sf
->max_step_search_steps
= MAX_MVSEARCH_STEPS
;
878 sf
->comp_inter_joint_search_thresh
= BLOCK_4X4
;
879 sf
->adaptive_rd_thresh
= 0;
880 sf
->use_lastframe_partitioning
= LAST_FRAME_PARTITION_OFF
;
881 sf
->tx_size_search_method
= USE_FULL_RD
;
882 sf
->use_lp32x32fdct
= 0;
883 sf
->adaptive_motion_search
= 0;
884 sf
->adaptive_pred_interp_filter
= 0;
885 sf
->reference_masking
= 0;
886 sf
->use_one_partition_size_always
= 0;
887 sf
->less_rectangular_check
= 0;
888 sf
->use_square_partition_only
= 0;
889 sf
->auto_min_max_partition_size
= NOT_IN_USE
;
890 sf
->max_partition_size
= BLOCK_64X64
;
891 sf
->min_partition_size
= BLOCK_4X4
;
892 sf
->adjust_partitioning_from_last_frame
= 0;
893 sf
->last_partitioning_redo_frequency
= 4;
894 sf
->disable_split_mask
= 0;
895 sf
->mode_search_skip_flags
= 0;
896 sf
->disable_split_var_thresh
= 0;
897 sf
->disable_filter_search_var_thresh
= 0;
898 for (i
= 0; i
< TX_SIZES
; i
++) {
899 sf
->intra_y_mode_mask
[i
] = ALL_INTRA_MODES
;
900 sf
->intra_uv_mode_mask
[i
] = ALL_INTRA_MODES
;
902 sf
->use_rd_breakout
= 0;
903 sf
->skip_encode_sb
= 0;
904 sf
->use_uv_intra_rd_estimate
= 0;
905 sf
->use_fast_lpf_pick
= 0;
906 sf
->use_fast_coef_updates
= 0;
907 sf
->using_small_partition_info
= 0;
908 sf
->mode_skip_start
= MAX_MODES
; // Mode index at which mode skip mask set
911 case 0: // This is the best quality mode.
912 cpi
->diamond_search_sad
= vp9_full_range_search
;
915 set_good_speed_feature(cm
, sf
, speed
);
919 set_rt_speed_feature(cm
, sf
, speed
);
923 // Set rd thresholds based on mode and speed setting
924 set_rd_speed_thresholds(cpi
, mode
);
925 set_rd_speed_thresholds_sub8x8(cpi
, mode
);
927 // Slow quant, dct and trellis not worthwhile for first pass
928 // so make sure they are always turned off.
929 if (cpi
->pass
== 1) {
930 sf
->optimize_coefficients
= 0;
933 // No recode for 1 pass.
934 if (cpi
->pass
== 0) {
936 sf
->optimize_coefficients
= 0;
939 cpi
->mb
.fwd_txm4x4
= vp9_fdct4x4
;
940 if (cpi
->oxcf
.lossless
|| cpi
->mb
.e_mbd
.lossless
) {
941 cpi
->mb
.fwd_txm4x4
= vp9_fwht4x4
;
944 if (cpi
->sf
.subpel_search_method
== SUBPEL_TREE
) {
945 cpi
->find_fractional_mv_step
= vp9_find_best_sub_pixel_tree
;
946 cpi
->find_fractional_mv_step_comp
= vp9_find_best_sub_pixel_comp_tree
;
949 cpi
->mb
.optimize
= cpi
->sf
.optimize_coefficients
== 1 && cpi
->pass
!= 1;
952 frames_at_speed
[cpi
->speed
]++;
956 static void alloc_raw_frame_buffers(VP9_COMP
*cpi
) {
957 VP9_COMMON
*cm
= &cpi
->common
;
959 cpi
->lookahead
= vp9_lookahead_init(cpi
->oxcf
.width
, cpi
->oxcf
.height
,
960 cm
->subsampling_x
, cm
->subsampling_y
,
961 cpi
->oxcf
.lag_in_frames
);
963 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
964 "Failed to allocate lag buffers");
966 if (vp9_realloc_frame_buffer(&cpi
->alt_ref_buffer
,
967 cpi
->oxcf
.width
, cpi
->oxcf
.height
,
968 cm
->subsampling_x
, cm
->subsampling_y
,
969 VP9_ENC_BORDER_IN_PIXELS
))
970 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
971 "Failed to allocate altref buffer");
974 void vp9_alloc_compressor_data(VP9_COMP
*cpi
) {
975 VP9_COMMON
*cm
= &cpi
->common
;
977 if (vp9_alloc_frame_buffers(cm
, cm
->width
, cm
->height
))
978 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
979 "Failed to allocate frame buffers");
981 if (vp9_alloc_frame_buffer(&cpi
->last_frame_uf
,
982 cm
->width
, cm
->height
,
983 cm
->subsampling_x
, cm
->subsampling_y
,
984 VP9_ENC_BORDER_IN_PIXELS
))
985 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
986 "Failed to allocate last frame buffer");
988 if (vp9_alloc_frame_buffer(&cpi
->scaled_source
,
989 cm
->width
, cm
->height
,
990 cm
->subsampling_x
, cm
->subsampling_y
,
991 VP9_ENC_BORDER_IN_PIXELS
))
992 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
993 "Failed to allocate scaled source buffer");
998 unsigned int tokens
= get_token_alloc(cm
->mb_rows
, cm
->mb_cols
);
1000 CHECK_MEM_ERROR(cm
, cpi
->tok
, vpx_calloc(tokens
, sizeof(*cpi
->tok
)));
1003 vpx_free(cpi
->mb_activity_map
);
1004 CHECK_MEM_ERROR(cm
, cpi
->mb_activity_map
,
1005 vpx_calloc(sizeof(unsigned int),
1006 cm
->mb_rows
* cm
->mb_cols
));
1008 vpx_free(cpi
->mb_norm_activity_map
);
1009 CHECK_MEM_ERROR(cm
, cpi
->mb_norm_activity_map
,
1010 vpx_calloc(sizeof(unsigned int),
1011 cm
->mb_rows
* cm
->mb_cols
));
1013 // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm
1014 // block where mi unit size is 8x8.
1015 vpx_free(cpi
->above_context
[0]);
1016 CHECK_MEM_ERROR(cm
, cpi
->above_context
[0],
1017 vpx_calloc(2 * mi_cols_aligned_to_sb(cm
->mi_cols
) *
1019 sizeof(*cpi
->above_context
[0])));
1021 vpx_free(cpi
->above_seg_context
);
1022 CHECK_MEM_ERROR(cm
, cpi
->above_seg_context
,
1023 vpx_calloc(mi_cols_aligned_to_sb(cm
->mi_cols
),
1024 sizeof(*cpi
->above_seg_context
)));
1028 static void update_frame_size(VP9_COMP
*cpi
) {
1029 VP9_COMMON
*cm
= &cpi
->common
;
1031 vp9_update_frame_size(cm
);
1033 // Update size of buffers local to this frame
1034 if (vp9_realloc_frame_buffer(&cpi
->last_frame_uf
,
1035 cm
->width
, cm
->height
,
1036 cm
->subsampling_x
, cm
->subsampling_y
,
1037 VP9_ENC_BORDER_IN_PIXELS
))
1038 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
1039 "Failed to reallocate last frame buffer");
1041 if (vp9_realloc_frame_buffer(&cpi
->scaled_source
,
1042 cm
->width
, cm
->height
,
1043 cm
->subsampling_x
, cm
->subsampling_y
,
1044 VP9_ENC_BORDER_IN_PIXELS
))
1045 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
1046 "Failed to reallocate scaled source buffer");
1049 int y_stride
= cpi
->scaled_source
.y_stride
;
1051 if (cpi
->sf
.search_method
== NSTEP
) {
1052 vp9_init3smotion_compensation(&cpi
->mb
, y_stride
);
1053 } else if (cpi
->sf
.search_method
== DIAMOND
) {
1054 vp9_init_dsmotion_compensation(&cpi
->mb
, y_stride
);
1060 for (i
= 1; i
< MAX_MB_PLANE
; ++i
) {
1061 cpi
->above_context
[i
] = cpi
->above_context
[0] +
1062 i
* sizeof(*cpi
->above_context
[0]) * 2 *
1063 mi_cols_aligned_to_sb(cm
->mi_cols
);
1069 // Table that converts 0-63 Q range values passed in outside to the Qindex
1070 // range used internally.
1071 static const int q_trans
[] = {
1072 0, 4, 8, 12, 16, 20, 24, 28,
1073 32, 36, 40, 44, 48, 52, 56, 60,
1074 64, 68, 72, 76, 80, 84, 88, 92,
1075 96, 100, 104, 108, 112, 116, 120, 124,
1076 128, 132, 136, 140, 144, 148, 152, 156,
1077 160, 164, 168, 172, 176, 180, 184, 188,
1078 192, 196, 200, 204, 208, 212, 216, 220,
1079 224, 228, 232, 236, 240, 244, 249, 255,
1082 int vp9_reverse_trans(int x
) {
1085 for (i
= 0; i
< 64; i
++)
1086 if (q_trans
[i
] >= x
)
1092 void vp9_new_framerate(VP9_COMP
*cpi
, double framerate
) {
1093 VP9_COMMON
*const cm
= &cpi
->common
;
1094 int64_t vbr_max_bits
;
1096 if (framerate
< 0.1)
1099 cpi
->oxcf
.framerate
= framerate
;
1100 cpi
->output_framerate
= cpi
->oxcf
.framerate
;
1101 cpi
->rc
.per_frame_bandwidth
= (int)(cpi
->oxcf
.target_bandwidth
1102 / cpi
->output_framerate
);
1103 cpi
->rc
.av_per_frame_bandwidth
= (int)(cpi
->oxcf
.target_bandwidth
1104 / cpi
->output_framerate
);
1105 cpi
->rc
.min_frame_bandwidth
= (int)(cpi
->rc
.av_per_frame_bandwidth
*
1106 cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
1109 cpi
->rc
.min_frame_bandwidth
= MAX(cpi
->rc
.min_frame_bandwidth
,
1110 FRAME_OVERHEAD_BITS
);
1112 // A maximum bitrate for a frame is defined.
1113 // The baseline for this aligns with HW implementations that
1114 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1115 // per 16x16 MB (averaged over a frame). However this limit is extended if
1116 // a very high rate is given on the command line or the the rate cannnot
1117 // be acheived because of a user specificed max q (e.g. when the user
1118 // specifies lossless encode.
1120 vbr_max_bits
= ((int64_t)cpi
->rc
.av_per_frame_bandwidth
*
1121 (int64_t)cpi
->oxcf
.two_pass_vbrmax_section
) / 100;
1122 cpi
->rc
.max_frame_bandwidth
=
1123 MAX(MAX((cm
->MBs
* MAX_MB_RATE
), MAXRATE_1080P
), vbr_max_bits
);
1125 // Set Maximum gf/arf interval
1126 cpi
->rc
.max_gf_interval
= 16;
1128 // Extended interval for genuinely static scenes
1129 cpi
->twopass
.static_scene_max_gf_interval
= cpi
->key_frame_frequency
>> 1;
1131 // Special conditions when alt ref frame enabled in lagged compress mode
1132 if (cpi
->oxcf
.play_alternate
&& cpi
->oxcf
.lag_in_frames
) {
1133 if (cpi
->rc
.max_gf_interval
> cpi
->oxcf
.lag_in_frames
- 1)
1134 cpi
->rc
.max_gf_interval
= cpi
->oxcf
.lag_in_frames
- 1;
1136 if (cpi
->twopass
.static_scene_max_gf_interval
> cpi
->oxcf
.lag_in_frames
- 1)
1137 cpi
->twopass
.static_scene_max_gf_interval
= cpi
->oxcf
.lag_in_frames
- 1;
1140 if (cpi
->rc
.max_gf_interval
> cpi
->twopass
.static_scene_max_gf_interval
)
1141 cpi
->rc
.max_gf_interval
= cpi
->twopass
.static_scene_max_gf_interval
;
1144 static int64_t rescale(int val
, int64_t num
, int denom
) {
1145 int64_t llnum
= num
;
1146 int64_t llden
= denom
;
1147 int64_t llval
= val
;
1149 return (llval
* llnum
/ llden
);
1152 static void set_tile_limits(VP9_COMP
*cpi
) {
1153 VP9_COMMON
*const cm
= &cpi
->common
;
1155 int min_log2_tile_cols
, max_log2_tile_cols
;
1156 vp9_get_tile_n_bits(cm
->mi_cols
, &min_log2_tile_cols
, &max_log2_tile_cols
);
1158 cm
->log2_tile_cols
= clamp(cpi
->oxcf
.tile_columns
,
1159 min_log2_tile_cols
, max_log2_tile_cols
);
1160 cm
->log2_tile_rows
= cpi
->oxcf
.tile_rows
;
1163 static void init_config(VP9_PTR ptr
, VP9_CONFIG
*oxcf
) {
1164 VP9_COMP
*cpi
= (VP9_COMP
*)(ptr
);
1165 VP9_COMMON
*const cm
= &cpi
->common
;
1170 cm
->version
= oxcf
->version
;
1172 cm
->width
= oxcf
->width
;
1173 cm
->height
= oxcf
->height
;
1174 cm
->subsampling_x
= 0;
1175 cm
->subsampling_y
= 0;
1176 vp9_alloc_compressor_data(cpi
);
1178 // change includes all joint functionality
1179 vp9_change_config(ptr
, oxcf
);
1181 // Initialize active best and worst q and average q values.
1182 cpi
->rc
.active_worst_quality
= cpi
->oxcf
.worst_allowed_q
;
1184 if (cpi
->pass
== 0 && cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
) {
1185 cpi
->rc
.avg_frame_qindex
[0] = cpi
->oxcf
.worst_allowed_q
;
1186 cpi
->rc
.avg_frame_qindex
[1] = cpi
->oxcf
.worst_allowed_q
;
1187 cpi
->rc
.avg_frame_qindex
[2] = cpi
->oxcf
.worst_allowed_q
;
1189 cpi
->rc
.avg_frame_qindex
[0] = (cpi
->oxcf
.worst_allowed_q
+
1190 cpi
->oxcf
.best_allowed_q
) / 2;
1191 cpi
->rc
.avg_frame_qindex
[1] = (cpi
->oxcf
.worst_allowed_q
+
1192 cpi
->oxcf
.best_allowed_q
) / 2;
1193 cpi
->rc
.avg_frame_qindex
[2] = (cpi
->oxcf
.worst_allowed_q
+
1194 cpi
->oxcf
.best_allowed_q
) / 2;
1196 cpi
->rc
.last_q
[0] = cpi
->oxcf
.best_allowed_q
;
1197 cpi
->rc
.last_q
[1] = cpi
->oxcf
.best_allowed_q
;
1198 cpi
->rc
.last_q
[2] = cpi
->oxcf
.best_allowed_q
;
1200 // Initialise the starting buffer levels
1201 cpi
->rc
.buffer_level
= cpi
->oxcf
.starting_buffer_level
;
1202 cpi
->rc
.bits_off_target
= cpi
->oxcf
.starting_buffer_level
;
1204 cpi
->rc
.rolling_target_bits
= cpi
->rc
.av_per_frame_bandwidth
;
1205 cpi
->rc
.rolling_actual_bits
= cpi
->rc
.av_per_frame_bandwidth
;
1206 cpi
->rc
.long_rolling_target_bits
= cpi
->rc
.av_per_frame_bandwidth
;
1207 cpi
->rc
.long_rolling_actual_bits
= cpi
->rc
.av_per_frame_bandwidth
;
1209 cpi
->rc
.total_actual_bits
= 0;
1210 cpi
->rc
.total_target_vs_actual
= 0;
1212 cpi
->static_mb_pct
= 0;
1214 cpi
->lst_fb_idx
= 0;
1215 cpi
->gld_fb_idx
= 1;
1216 cpi
->alt_fb_idx
= 2;
1218 cpi
->current_layer
= 0;
1221 set_tile_limits(cpi
);
1223 cpi
->fixed_divide
[0] = 0;
1224 for (i
= 1; i
< 512; i
++)
1225 cpi
->fixed_divide
[i
] = 0x80000 / i
;
1229 void vp9_change_config(VP9_PTR ptr
, VP9_CONFIG
*oxcf
) {
1230 VP9_COMP
*cpi
= (VP9_COMP
*)(ptr
);
1231 VP9_COMMON
*const cm
= &cpi
->common
;
1236 if (cm
->version
!= oxcf
->version
) {
1237 cm
->version
= oxcf
->version
;
1242 switch (cpi
->oxcf
.mode
) {
1243 // Real time and one pass deprecated in test code base
1244 case MODE_GOODQUALITY
:
1246 cpi
->compressor_speed
= 2;
1247 cpi
->oxcf
.cpu_used
= clamp(cpi
->oxcf
.cpu_used
, -5, 5);
1250 case MODE_FIRSTPASS
:
1252 cpi
->compressor_speed
= 1;
1255 case MODE_SECONDPASS
:
1257 cpi
->compressor_speed
= 1;
1258 cpi
->oxcf
.cpu_used
= clamp(cpi
->oxcf
.cpu_used
, -5, 5);
1261 case MODE_SECONDPASS_BEST
:
1263 cpi
->compressor_speed
= 0;
1268 cpi
->compressor_speed
= 3;
1272 cpi
->oxcf
.worst_allowed_q
= q_trans
[oxcf
->worst_allowed_q
];
1273 cpi
->oxcf
.best_allowed_q
= q_trans
[oxcf
->best_allowed_q
];
1274 cpi
->oxcf
.cq_level
= q_trans
[cpi
->oxcf
.cq_level
];
1276 cpi
->oxcf
.lossless
= oxcf
->lossless
;
1277 cpi
->mb
.e_mbd
.itxm_add
= cpi
->oxcf
.lossless
? vp9_iwht4x4_add
1279 cpi
->rc
.baseline_gf_interval
= DEFAULT_GF_INTERVAL
;
1281 cpi
->ref_frame_flags
= VP9_ALT_FLAG
| VP9_GOLD_FLAG
| VP9_LAST_FLAG
;
1283 cpi
->refresh_golden_frame
= 0;
1284 cpi
->refresh_last_frame
= 1;
1285 cm
->refresh_frame_context
= 1;
1286 cm
->reset_frame_context
= 0;
1288 vp9_reset_segment_features(&cm
->seg
);
1289 set_high_precision_mv(cpi
, 0);
1294 for (i
= 0; i
< MAX_SEGMENTS
; i
++)
1295 cpi
->segment_encode_breakout
[i
] = cpi
->oxcf
.encode_breakout
;
1298 // local file playback mode == really big buffer
1299 if (cpi
->oxcf
.end_usage
== USAGE_LOCAL_FILE_PLAYBACK
) {
1300 cpi
->oxcf
.starting_buffer_level
= 60000;
1301 cpi
->oxcf
.optimal_buffer_level
= 60000;
1302 cpi
->oxcf
.maximum_buffer_size
= 240000;
1305 // Convert target bandwidth from Kbit/s to Bit/s
1306 cpi
->oxcf
.target_bandwidth
*= 1000;
1308 cpi
->oxcf
.starting_buffer_level
= rescale(cpi
->oxcf
.starting_buffer_level
,
1309 cpi
->oxcf
.target_bandwidth
, 1000);
1311 // Set or reset optimal and maximum buffer levels.
1312 if (cpi
->oxcf
.optimal_buffer_level
== 0)
1313 cpi
->oxcf
.optimal_buffer_level
= cpi
->oxcf
.target_bandwidth
/ 8;
1315 cpi
->oxcf
.optimal_buffer_level
= rescale(cpi
->oxcf
.optimal_buffer_level
,
1316 cpi
->oxcf
.target_bandwidth
, 1000);
1318 if (cpi
->oxcf
.maximum_buffer_size
== 0)
1319 cpi
->oxcf
.maximum_buffer_size
= cpi
->oxcf
.target_bandwidth
/ 8;
1321 cpi
->oxcf
.maximum_buffer_size
= rescale(cpi
->oxcf
.maximum_buffer_size
,
1322 cpi
->oxcf
.target_bandwidth
, 1000);
1323 // Under a configuration change, where maximum_buffer_size may change,
1324 // keep buffer level clipped to the maximum allowed buffer size.
1325 if (cpi
->rc
.bits_off_target
> cpi
->oxcf
.maximum_buffer_size
) {
1326 cpi
->rc
.bits_off_target
= cpi
->oxcf
.maximum_buffer_size
;
1327 cpi
->rc
.buffer_level
= cpi
->rc
.bits_off_target
;
1330 // Set up frame rate and related parameters rate control values.
1331 vp9_new_framerate(cpi
, cpi
->oxcf
.framerate
);
1333 // Set absolute upper and lower quality limits
1334 cpi
->rc
.worst_quality
= cpi
->oxcf
.worst_allowed_q
;
1335 cpi
->rc
.best_quality
= cpi
->oxcf
.best_allowed_q
;
1337 // active values should only be modified if out of new range
1338 cpi
->rc
.active_worst_quality
= clamp(cpi
->rc
.active_worst_quality
,
1339 cpi
->rc
.best_quality
,
1340 cpi
->rc
.worst_quality
);
1342 cpi
->cq_target_quality
= cpi
->oxcf
.cq_level
;
1344 cm
->interp_filter
= DEFAULT_INTERP_FILTER
;
1346 cpi
->target_bandwidth
= cpi
->oxcf
.target_bandwidth
;
1348 cm
->display_width
= cpi
->oxcf
.width
;
1349 cm
->display_height
= cpi
->oxcf
.height
;
1351 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1352 cpi
->oxcf
.sharpness
= MIN(7, cpi
->oxcf
.sharpness
);
1354 cpi
->common
.lf
.sharpness_level
= cpi
->oxcf
.sharpness
;
1356 if (cpi
->initial_width
) {
1357 // Increasing the size of the frame beyond the first seen frame, or some
1358 // otherwise signalled maximum size, is not supported.
1359 // TODO(jkoleszar): exit gracefully.
1360 assert(cm
->width
<= cpi
->initial_width
);
1361 assert(cm
->height
<= cpi
->initial_height
);
1363 update_frame_size(cpi
);
1365 cpi
->speed
= cpi
->oxcf
.cpu_used
;
1367 if (cpi
->oxcf
.lag_in_frames
== 0) {
1368 // force to allowlag to 0 if lag_in_frames is 0;
1369 cpi
->oxcf
.allow_lag
= 0;
1370 } else if (cpi
->oxcf
.lag_in_frames
> MAX_LAG_BUFFERS
) {
1371 // Limit on lag buffers as these are not currently dynamically allocated
1372 cpi
->oxcf
.lag_in_frames
= MAX_LAG_BUFFERS
;
1376 #if CONFIG_MULTIPLE_ARF
1377 vp9_zero(cpi
->alt_ref_source
);
1379 cpi
->alt_ref_source
= NULL
;
1381 cpi
->rc
.is_src_frame_alt_ref
= 0;
1384 // Experimental RD Code
1385 cpi
->frame_distortion
= 0;
1386 cpi
->last_frame_distortion
= 0;
1389 set_tile_limits(cpi
);
1391 cpi
->ext_refresh_frame_flags_pending
= 0;
1392 cpi
->ext_refresh_frame_context_pending
= 0;
1395 #define M_LOG2_E 0.693147180559945309417
1396 #define log2f(x) (log (x) / (float) M_LOG2_E)
1398 static void cal_nmvjointsadcost(int *mvjointsadcost
) {
1399 mvjointsadcost
[0] = 600;
1400 mvjointsadcost
[1] = 300;
1401 mvjointsadcost
[2] = 300;
1402 mvjointsadcost
[0] = 300;
1405 static void cal_nmvsadcosts(int *mvsadcost
[2]) {
1408 mvsadcost
[0][0] = 0;
1409 mvsadcost
[1][0] = 0;
1412 double z
= 256 * (2 * (log2f(8 * i
) + .6));
1413 mvsadcost
[0][i
] = (int)z
;
1414 mvsadcost
[1][i
] = (int)z
;
1415 mvsadcost
[0][-i
] = (int)z
;
1416 mvsadcost
[1][-i
] = (int)z
;
1417 } while (++i
<= MV_MAX
);
1420 static void cal_nmvsadcosts_hp(int *mvsadcost
[2]) {
1423 mvsadcost
[0][0] = 0;
1424 mvsadcost
[1][0] = 0;
1427 double z
= 256 * (2 * (log2f(8 * i
) + .6));
1428 mvsadcost
[0][i
] = (int)z
;
1429 mvsadcost
[1][i
] = (int)z
;
1430 mvsadcost
[0][-i
] = (int)z
;
1431 mvsadcost
[1][-i
] = (int)z
;
1432 } while (++i
<= MV_MAX
);
1435 static void alloc_mode_context(VP9_COMMON
*cm
, int num_4x4_blk
,
1436 PICK_MODE_CONTEXT
*ctx
) {
1437 int num_pix
= num_4x4_blk
<< 4;
1439 ctx
->num_4x4_blk
= num_4x4_blk
;
1440 CHECK_MEM_ERROR(cm
, ctx
->zcoeff_blk
,
1441 vpx_calloc(num_4x4_blk
, sizeof(uint8_t)));
1442 for (i
= 0; i
< MAX_MB_PLANE
; ++i
) {
1443 for (k
= 0; k
< 3; ++k
) {
1444 CHECK_MEM_ERROR(cm
, ctx
->coeff
[i
][k
],
1445 vpx_memalign(16, num_pix
* sizeof(int16_t)));
1446 CHECK_MEM_ERROR(cm
, ctx
->qcoeff
[i
][k
],
1447 vpx_memalign(16, num_pix
* sizeof(int16_t)));
1448 CHECK_MEM_ERROR(cm
, ctx
->dqcoeff
[i
][k
],
1449 vpx_memalign(16, num_pix
* sizeof(int16_t)));
1450 CHECK_MEM_ERROR(cm
, ctx
->eobs
[i
][k
],
1451 vpx_memalign(16, num_pix
* sizeof(uint16_t)));
1452 ctx
->coeff_pbuf
[i
][k
] = ctx
->coeff
[i
][k
];
1453 ctx
->qcoeff_pbuf
[i
][k
] = ctx
->qcoeff
[i
][k
];
1454 ctx
->dqcoeff_pbuf
[i
][k
] = ctx
->dqcoeff
[i
][k
];
1455 ctx
->eobs_pbuf
[i
][k
] = ctx
->eobs
[i
][k
];
1460 static void free_mode_context(PICK_MODE_CONTEXT
*ctx
) {
1462 vpx_free(ctx
->zcoeff_blk
);
1463 ctx
->zcoeff_blk
= 0;
1464 for (i
= 0; i
< MAX_MB_PLANE
; ++i
) {
1465 for (k
= 0; k
< 3; ++k
) {
1466 vpx_free(ctx
->coeff
[i
][k
]);
1467 ctx
->coeff
[i
][k
] = 0;
1468 vpx_free(ctx
->qcoeff
[i
][k
]);
1469 ctx
->qcoeff
[i
][k
] = 0;
1470 vpx_free(ctx
->dqcoeff
[i
][k
]);
1471 ctx
->dqcoeff
[i
][k
] = 0;
1472 vpx_free(ctx
->eobs
[i
][k
]);
1473 ctx
->eobs
[i
][k
] = 0;
1478 static void init_pick_mode_context(VP9_COMP
*cpi
) {
1480 VP9_COMMON
*const cm
= &cpi
->common
;
1481 MACROBLOCK
*const x
= &cpi
->mb
;
1484 for (i
= 0; i
< BLOCK_SIZES
; ++i
) {
1485 const int num_4x4_w
= num_4x4_blocks_wide_lookup
[i
];
1486 const int num_4x4_h
= num_4x4_blocks_high_lookup
[i
];
1487 const int num_4x4_blk
= MAX(4, num_4x4_w
* num_4x4_h
);
1488 if (i
< BLOCK_16X16
) {
1489 for (x
->sb_index
= 0; x
->sb_index
< 4; ++x
->sb_index
) {
1490 for (x
->mb_index
= 0; x
->mb_index
< 4; ++x
->mb_index
) {
1491 for (x
->b_index
= 0; x
->b_index
< 16 / num_4x4_blk
; ++x
->b_index
) {
1492 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1493 alloc_mode_context(cm
, num_4x4_blk
, ctx
);
1497 } else if (i
< BLOCK_32X32
) {
1498 for (x
->sb_index
= 0; x
->sb_index
< 4; ++x
->sb_index
) {
1499 for (x
->mb_index
= 0; x
->mb_index
< 64 / num_4x4_blk
; ++x
->mb_index
) {
1500 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1501 ctx
->num_4x4_blk
= num_4x4_blk
;
1502 alloc_mode_context(cm
, num_4x4_blk
, ctx
);
1505 } else if (i
< BLOCK_64X64
) {
1506 for (x
->sb_index
= 0; x
->sb_index
< 256 / num_4x4_blk
; ++x
->sb_index
) {
1507 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1508 ctx
->num_4x4_blk
= num_4x4_blk
;
1509 alloc_mode_context(cm
, num_4x4_blk
, ctx
);
1512 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1513 ctx
->num_4x4_blk
= num_4x4_blk
;
1514 alloc_mode_context(cm
, num_4x4_blk
, ctx
);
1519 static void free_pick_mode_context(MACROBLOCK
*x
) {
1522 for (i
= 0; i
< BLOCK_SIZES
; ++i
) {
1523 const int num_4x4_w
= num_4x4_blocks_wide_lookup
[i
];
1524 const int num_4x4_h
= num_4x4_blocks_high_lookup
[i
];
1525 const int num_4x4_blk
= MAX(4, num_4x4_w
* num_4x4_h
);
1526 if (i
< BLOCK_16X16
) {
1527 for (x
->sb_index
= 0; x
->sb_index
< 4; ++x
->sb_index
) {
1528 for (x
->mb_index
= 0; x
->mb_index
< 4; ++x
->mb_index
) {
1529 for (x
->b_index
= 0; x
->b_index
< 16 / num_4x4_blk
; ++x
->b_index
) {
1530 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1531 free_mode_context(ctx
);
1535 } else if (i
< BLOCK_32X32
) {
1536 for (x
->sb_index
= 0; x
->sb_index
< 4; ++x
->sb_index
) {
1537 for (x
->mb_index
= 0; x
->mb_index
< 64 / num_4x4_blk
; ++x
->mb_index
) {
1538 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1539 free_mode_context(ctx
);
1542 } else if (i
< BLOCK_64X64
) {
1543 for (x
->sb_index
= 0; x
->sb_index
< 256 / num_4x4_blk
; ++x
->sb_index
) {
1544 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1545 free_mode_context(ctx
);
1548 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1549 free_mode_context(ctx
);
1554 VP9_PTR
vp9_create_compressor(VP9_CONFIG
*oxcf
) {
1564 cpi
= ctx
.cpi
= vpx_memalign(32, sizeof(VP9_COMP
));
1565 // Check that the CPI instance is valid
1573 if (setjmp(cm
->error
.jmp
)) {
1574 VP9_PTR ptr
= ctx
.ptr
;
1576 ctx
.cpi
->common
.error
.setjmp
= 0;
1577 vp9_remove_compressor(&ptr
);
1581 cm
->error
.setjmp
= 1;
1583 CHECK_MEM_ERROR(cm
, cpi
->mb
.ss
, vpx_calloc(sizeof(search_site
),
1584 (MAX_MVSEARCH_STEPS
* 8) + 1));
1586 vp9_create_common(cm
);
1588 init_config((VP9_PTR
)cpi
, oxcf
);
1590 init_pick_mode_context(cpi
);
1592 cm
->current_video_frame
= 0;
1594 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1595 cm
->ref_frame_sign_bias
[ALTREF_FRAME
] = 1;
1597 cpi
->rc
.baseline_gf_interval
= DEFAULT_GF_INTERVAL
;
1599 cpi
->gold_is_last
= 0;
1600 cpi
->alt_is_last
= 0;
1601 cpi
->gold_is_alt
= 0;
1603 // Spatial scalability
1604 cpi
->number_spatial_layers
= oxcf
->ss_number_layers
;
1606 // Create the encoder segmentation map and set all entries to 0
1607 CHECK_MEM_ERROR(cm
, cpi
->segmentation_map
,
1608 vpx_calloc(cm
->mi_rows
* cm
->mi_cols
, 1));
1610 // Create a complexity map used for rd adjustment
1611 CHECK_MEM_ERROR(cm
, cpi
->complexity_map
,
1612 vpx_calloc(cm
->mi_rows
* cm
->mi_cols
, 1));
1615 // And a place holder structure is the coding context
1616 // for use if we want to save and restore it
1617 CHECK_MEM_ERROR(cm
, cpi
->coding_context
.last_frame_seg_map_copy
,
1618 vpx_calloc(cm
->mi_rows
* cm
->mi_cols
, 1));
1620 CHECK_MEM_ERROR(cm
, cpi
->active_map
, vpx_calloc(cm
->MBs
, 1));
1621 vpx_memset(cpi
->active_map
, 1, cm
->MBs
);
1622 cpi
->active_map_enabled
= 0;
1624 for (i
= 0; i
< (sizeof(cpi
->mbgraph_stats
) /
1625 sizeof(cpi
->mbgraph_stats
[0])); i
++) {
1626 CHECK_MEM_ERROR(cm
, cpi
->mbgraph_stats
[i
].mb_stats
,
1627 vpx_calloc(cm
->MBs
*
1628 sizeof(*cpi
->mbgraph_stats
[i
].mb_stats
), 1));
1631 #ifdef ENTROPY_STATS
1633 init_context_counters();
1636 /*Initialize the feed-forward activity masking.*/
1637 cpi
->activity_avg
= 90 << 12;
1638 cpi
->key_frame_frequency
= cpi
->oxcf
.key_freq
;
1640 cpi
->rc
.frames_since_key
= 8; // Sensible default for first frame.
1641 cpi
->rc
.this_key_frame_forced
= 0;
1642 cpi
->rc
.next_key_frame_forced
= 0;
1644 cpi
->rc
.source_alt_ref_pending
= 0;
1645 cpi
->rc
.source_alt_ref_active
= 0;
1646 cpi
->refresh_alt_ref_frame
= 0;
1648 #if CONFIG_MULTIPLE_ARF
1649 // Turn multiple ARF usage on/off. This is a quick hack for the initial test
1650 // version. It should eventually be set via the codec API.
1651 cpi
->multi_arf_enabled
= 1;
1653 if (cpi
->multi_arf_enabled
) {
1654 cpi
->sequence_number
= 0;
1655 cpi
->frame_coding_order_period
= 0;
1656 vp9_zero(cpi
->frame_coding_order
);
1657 vp9_zero(cpi
->arf_buffer_idx
);
1661 cpi
->b_calculate_psnr
= CONFIG_INTERNAL_STATS
;
1662 #if CONFIG_INTERNAL_STATS
1663 cpi
->b_calculate_ssimg
= 0;
1668 if (cpi
->b_calculate_psnr
) {
1673 cpi
->total_sq_error
= 0;
1674 cpi
->total_samples
= 0;
1676 cpi
->totalp_y
= 0.0;
1677 cpi
->totalp_u
= 0.0;
1678 cpi
->totalp_v
= 0.0;
1680 cpi
->totalp_sq_error
= 0;
1681 cpi
->totalp_samples
= 0;
1683 cpi
->tot_recode_hits
= 0;
1684 cpi
->summed_quality
= 0;
1685 cpi
->summed_weights
= 0;
1686 cpi
->summedp_quality
= 0;
1687 cpi
->summedp_weights
= 0;
1690 if (cpi
->b_calculate_ssimg
) {
1691 cpi
->total_ssimg_y
= 0;
1692 cpi
->total_ssimg_u
= 0;
1693 cpi
->total_ssimg_v
= 0;
1694 cpi
->total_ssimg_all
= 0;
1699 cpi
->first_time_stamp_ever
= INT64_MAX
;
1701 cpi
->rc
.frames_till_gf_update_due
= 0;
1703 cpi
->rc
.ni_av_qi
= cpi
->oxcf
.worst_allowed_q
;
1704 cpi
->rc
.ni_tot_qi
= 0;
1705 cpi
->rc
.ni_frames
= 0;
1706 cpi
->rc
.tot_q
= 0.0;
1707 cpi
->rc
.avg_q
= vp9_convert_qindex_to_q(cpi
->oxcf
.worst_allowed_q
);
1709 cpi
->rc
.rate_correction_factor
= 1.0;
1710 cpi
->rc
.key_frame_rate_correction_factor
= 1.0;
1711 cpi
->rc
.gf_rate_correction_factor
= 1.0;
1713 cal_nmvjointsadcost(cpi
->mb
.nmvjointsadcost
);
1714 cpi
->mb
.nmvcost
[0] = &cpi
->mb
.nmvcosts
[0][MV_MAX
];
1715 cpi
->mb
.nmvcost
[1] = &cpi
->mb
.nmvcosts
[1][MV_MAX
];
1716 cpi
->mb
.nmvsadcost
[0] = &cpi
->mb
.nmvsadcosts
[0][MV_MAX
];
1717 cpi
->mb
.nmvsadcost
[1] = &cpi
->mb
.nmvsadcosts
[1][MV_MAX
];
1718 cal_nmvsadcosts(cpi
->mb
.nmvsadcost
);
1720 cpi
->mb
.nmvcost_hp
[0] = &cpi
->mb
.nmvcosts_hp
[0][MV_MAX
];
1721 cpi
->mb
.nmvcost_hp
[1] = &cpi
->mb
.nmvcosts_hp
[1][MV_MAX
];
1722 cpi
->mb
.nmvsadcost_hp
[0] = &cpi
->mb
.nmvsadcosts_hp
[0][MV_MAX
];
1723 cpi
->mb
.nmvsadcost_hp
[1] = &cpi
->mb
.nmvsadcosts_hp
[1][MV_MAX
];
1724 cal_nmvsadcosts_hp(cpi
->mb
.nmvsadcost_hp
);
1726 #ifdef OUTPUT_YUV_SRC
1727 yuv_file
= fopen("bd.yuv", "ab");
1729 #ifdef OUTPUT_YUV_REC
1730 yuv_rec_file
= fopen("rec.yuv", "wb");
1734 framepsnr
= fopen("framepsnr.stt", "a");
1735 kf_list
= fopen("kf_list.stt", "w");
1738 cpi
->output_pkt_list
= oxcf
->output_pkt_list
;
1740 cpi
->enable_encode_breakout
= 1;
1742 if (cpi
->pass
== 1) {
1743 vp9_init_first_pass(cpi
);
1744 } else if (cpi
->pass
== 2) {
1745 size_t packet_sz
= sizeof(FIRSTPASS_STATS
);
1746 int packets
= (int)(oxcf
->two_pass_stats_in
.sz
/ packet_sz
);
1748 cpi
->twopass
.stats_in_start
= oxcf
->two_pass_stats_in
.buf
;
1749 cpi
->twopass
.stats_in
= cpi
->twopass
.stats_in_start
;
1750 cpi
->twopass
.stats_in_end
= (void *)((char *)cpi
->twopass
.stats_in
1751 + (packets
- 1) * packet_sz
);
1752 vp9_init_second_pass(cpi
);
1755 vp9_set_speed_features(cpi
);
1757 // Default rd threshold factors for mode selection
1758 for (i
= 0; i
< BLOCK_SIZES
; ++i
) {
1759 for (j
= 0; j
< MAX_MODES
; ++j
)
1760 cpi
->rd_thresh_freq_fact
[i
][j
] = 32;
1761 for (j
= 0; j
< MAX_REFS
; ++j
)
1762 cpi
->rd_thresh_freq_sub8x8
[i
][j
] = 32;
1765 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \
1766 SDX3F, SDX8F, SDX4DF)\
1767 cpi->fn_ptr[BT].sdf = SDF; \
1768 cpi->fn_ptr[BT].sdaf = SDAF; \
1769 cpi->fn_ptr[BT].vf = VF; \
1770 cpi->fn_ptr[BT].svf = SVF; \
1771 cpi->fn_ptr[BT].svaf = SVAF; \
1772 cpi->fn_ptr[BT].svf_halfpix_h = SVFHH; \
1773 cpi->fn_ptr[BT].svf_halfpix_v = SVFHV; \
1774 cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \
1775 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1776 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1777 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1779 BFP(BLOCK_32X16
, vp9_sad32x16
, vp9_sad32x16_avg
,
1780 vp9_variance32x16
, vp9_sub_pixel_variance32x16
,
1781 vp9_sub_pixel_avg_variance32x16
, NULL
, NULL
,
1785 BFP(BLOCK_16X32
, vp9_sad16x32
, vp9_sad16x32_avg
,
1786 vp9_variance16x32
, vp9_sub_pixel_variance16x32
,
1787 vp9_sub_pixel_avg_variance16x32
, NULL
, NULL
,
1791 BFP(BLOCK_64X32
, vp9_sad64x32
, vp9_sad64x32_avg
,
1792 vp9_variance64x32
, vp9_sub_pixel_variance64x32
,
1793 vp9_sub_pixel_avg_variance64x32
, NULL
, NULL
,
1797 BFP(BLOCK_32X64
, vp9_sad32x64
, vp9_sad32x64_avg
,
1798 vp9_variance32x64
, vp9_sub_pixel_variance32x64
,
1799 vp9_sub_pixel_avg_variance32x64
, NULL
, NULL
,
1803 BFP(BLOCK_32X32
, vp9_sad32x32
, vp9_sad32x32_avg
,
1804 vp9_variance32x32
, vp9_sub_pixel_variance32x32
,
1805 vp9_sub_pixel_avg_variance32x32
, vp9_variance_halfpixvar32x32_h
,
1806 vp9_variance_halfpixvar32x32_v
,
1807 vp9_variance_halfpixvar32x32_hv
, vp9_sad32x32x3
, vp9_sad32x32x8
,
1810 BFP(BLOCK_64X64
, vp9_sad64x64
, vp9_sad64x64_avg
,
1811 vp9_variance64x64
, vp9_sub_pixel_variance64x64
,
1812 vp9_sub_pixel_avg_variance64x64
, vp9_variance_halfpixvar64x64_h
,
1813 vp9_variance_halfpixvar64x64_v
,
1814 vp9_variance_halfpixvar64x64_hv
, vp9_sad64x64x3
, vp9_sad64x64x8
,
1817 BFP(BLOCK_16X16
, vp9_sad16x16
, vp9_sad16x16_avg
,
1818 vp9_variance16x16
, vp9_sub_pixel_variance16x16
,
1819 vp9_sub_pixel_avg_variance16x16
, vp9_variance_halfpixvar16x16_h
,
1820 vp9_variance_halfpixvar16x16_v
,
1821 vp9_variance_halfpixvar16x16_hv
, vp9_sad16x16x3
, vp9_sad16x16x8
,
1824 BFP(BLOCK_16X8
, vp9_sad16x8
, vp9_sad16x8_avg
,
1825 vp9_variance16x8
, vp9_sub_pixel_variance16x8
,
1826 vp9_sub_pixel_avg_variance16x8
, NULL
, NULL
, NULL
,
1827 vp9_sad16x8x3
, vp9_sad16x8x8
, vp9_sad16x8x4d
)
1829 BFP(BLOCK_8X16
, vp9_sad8x16
, vp9_sad8x16_avg
,
1830 vp9_variance8x16
, vp9_sub_pixel_variance8x16
,
1831 vp9_sub_pixel_avg_variance8x16
, NULL
, NULL
, NULL
,
1832 vp9_sad8x16x3
, vp9_sad8x16x8
, vp9_sad8x16x4d
)
1834 BFP(BLOCK_8X8
, vp9_sad8x8
, vp9_sad8x8_avg
,
1835 vp9_variance8x8
, vp9_sub_pixel_variance8x8
,
1836 vp9_sub_pixel_avg_variance8x8
, NULL
, NULL
, NULL
,
1837 vp9_sad8x8x3
, vp9_sad8x8x8
, vp9_sad8x8x4d
)
1839 BFP(BLOCK_8X4
, vp9_sad8x4
, vp9_sad8x4_avg
,
1840 vp9_variance8x4
, vp9_sub_pixel_variance8x4
,
1841 vp9_sub_pixel_avg_variance8x4
, NULL
, NULL
,
1842 NULL
, NULL
, vp9_sad8x4x8
,
1845 BFP(BLOCK_4X8
, vp9_sad4x8
, vp9_sad4x8_avg
,
1846 vp9_variance4x8
, vp9_sub_pixel_variance4x8
,
1847 vp9_sub_pixel_avg_variance4x8
, NULL
, NULL
,
1848 NULL
, NULL
, vp9_sad4x8x8
,
1851 BFP(BLOCK_4X4
, vp9_sad4x4
, vp9_sad4x4_avg
,
1852 vp9_variance4x4
, vp9_sub_pixel_variance4x4
,
1853 vp9_sub_pixel_avg_variance4x4
, NULL
, NULL
, NULL
,
1854 vp9_sad4x4x3
, vp9_sad4x4x8
, vp9_sad4x4x4d
)
1856 cpi
->full_search_sad
= vp9_full_search_sad
;
1857 cpi
->diamond_search_sad
= vp9_diamond_search_sad
;
1858 cpi
->refining_search_sad
= vp9_refining_search_sad
;
1860 /* vp9_init_quantizer() is first called here. Add check in
1861 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1862 * called later when needed. This will avoid unnecessary calls of
1863 * vp9_init_quantizer() for every frame.
1865 vp9_init_quantizer(cpi
);
1867 vp9_loop_filter_init(cm
);
1869 cm
->error
.setjmp
= 0;
1871 vp9_zero(cpi
->common
.counts
.uv_mode
);
1873 #ifdef MODE_TEST_HIT_STATS
1874 vp9_zero(cpi
->mode_test_hits
);
1877 return (VP9_PTR
) cpi
;
1880 void vp9_remove_compressor(VP9_PTR
*ptr
) {
1881 VP9_COMP
*cpi
= (VP9_COMP
*)(*ptr
);
1887 if (cpi
&& (cpi
->common
.current_video_frame
> 0)) {
1888 if (cpi
->pass
== 2) {
1889 vp9_end_second_pass(cpi
);
1892 #if CONFIG_INTERNAL_STATS
1894 vp9_clear_system_state();
1896 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1897 if (cpi
->pass
!= 1) {
1898 FILE *f
= fopen("opsnr.stt", "a");
1899 double time_encoded
= (cpi
->last_end_time_stamp_seen
1900 - cpi
->first_time_stamp_ever
) / 10000000.000;
1901 double total_encode_time
= (cpi
->time_receive_data
+
1902 cpi
->time_compress_data
) / 1000.000;
1903 double dr
= (double)cpi
->bytes
* (double) 8 / (double)1000
1906 if (cpi
->b_calculate_psnr
) {
1907 const double total_psnr
= vp9_mse2psnr(cpi
->total_samples
, 255.0,
1908 cpi
->total_sq_error
);
1909 const double totalp_psnr
= vp9_mse2psnr(cpi
->totalp_samples
, 255.0,
1910 cpi
->totalp_sq_error
);
1911 const double total_ssim
= 100 * pow(cpi
->summed_quality
/
1912 cpi
->summed_weights
, 8.0);
1913 const double totalp_ssim
= 100 * pow(cpi
->summedp_quality
/
1914 cpi
->summedp_weights
, 8.0);
1916 fprintf(f
, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1917 "VPXSSIM\tVPSSIMP\t Time(ms)\n");
1918 fprintf(f
, "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f\n",
1919 dr
, cpi
->total
/ cpi
->count
, total_psnr
,
1920 cpi
->totalp
/ cpi
->count
, totalp_psnr
, total_ssim
, totalp_ssim
,
1924 if (cpi
->b_calculate_ssimg
) {
1925 fprintf(f
, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n");
1926 fprintf(f
, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr
,
1927 cpi
->total_ssimg_y
/ cpi
->count
,
1928 cpi
->total_ssimg_u
/ cpi
->count
,
1929 cpi
->total_ssimg_v
/ cpi
->count
,
1930 cpi
->total_ssimg_all
/ cpi
->count
, total_encode_time
);
1938 #ifdef MODE_TEST_HIT_STATS
1939 if (cpi
->pass
!= 1) {
1940 double norm_per_pixel_mode_tests
= 0;
1941 double norm_counts
[BLOCK_SIZES
];
1944 int norm_factors
[BLOCK_SIZES
] =
1945 {256, 128, 128, 64, 32, 32, 16, 8, 8, 4, 2, 2, 1};
1946 FILE *f
= fopen("mode_hit_stats.stt", "a");
1948 // On average, how many mode tests do we do
1949 for (i
= 0; i
< BLOCK_SIZES
; ++i
) {
1950 norm_counts
[i
] = (double)cpi
->mode_test_hits
[i
] /
1951 (double)norm_factors
[i
];
1952 norm_per_pixel_mode_tests
+= norm_counts
[i
];
1954 // Convert to a number per 64x64 and per frame
1955 sb64_per_frame
= ((cpi
->common
.height
+ 63) / 64) *
1956 ((cpi
->common
.width
+ 63) / 64);
1957 norm_per_pixel_mode_tests
=
1958 norm_per_pixel_mode_tests
/
1959 (double)(cpi
->common
.current_video_frame
* sb64_per_frame
);
1961 fprintf(f
, "%6.4f\n", norm_per_pixel_mode_tests
);
1966 #if defined(SECTIONBITS_OUTPUT)
1970 FILE *f
= fopen("tokenbits.stt", "a");
1972 for (i
= 0; i
< 28; i
++)
1973 fprintf(f
, "%8d", (int)(Sectionbits
[i
] / 256));
1983 printf("\n_pick_loop_filter_level:%d\n", cpi
->time_pick_lpf
/ 1000);
1984 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
1985 printf("%6d %10ld %10ld %10ld %10ld\n", cpi
->common
.current_video_frame
,
1986 cpi
->time_receive_data
/ 1000, cpi
->time_encode_sb_row
/ 1000,
1987 cpi
->time_compress_data
/ 1000,
1988 (cpi
->time_receive_data
+ cpi
->time_compress_data
) / 1000);
1993 free_pick_mode_context(&cpi
->mb
);
1994 dealloc_compressor_data(cpi
);
1995 vpx_free(cpi
->mb
.ss
);
1998 for (i
= 0; i
< sizeof(cpi
->mbgraph_stats
) /
1999 sizeof(cpi
->mbgraph_stats
[0]); ++i
) {
2000 vpx_free(cpi
->mbgraph_stats
[i
].mb_stats
);
2003 vp9_remove_common(&cpi
->common
);
2007 #ifdef OUTPUT_YUV_SRC
2010 #ifdef OUTPUT_YUV_REC
2011 fclose(yuv_rec_file
);
2029 static uint64_t calc_plane_error(const uint8_t *orig
, int orig_stride
,
2030 const uint8_t *recon
, int recon_stride
,
2031 unsigned int cols
, unsigned int rows
) {
2032 unsigned int row
, col
;
2033 uint64_t total_sse
= 0;
2036 for (row
= 0; row
+ 16 <= rows
; row
+= 16) {
2037 for (col
= 0; col
+ 16 <= cols
; col
+= 16) {
2040 vp9_mse16x16(orig
+ col
, orig_stride
, recon
+ col
, recon_stride
, &sse
);
2044 /* Handle odd-sized width */
2046 unsigned int border_row
, border_col
;
2047 const uint8_t *border_orig
= orig
;
2048 const uint8_t *border_recon
= recon
;
2050 for (border_row
= 0; border_row
< 16; border_row
++) {
2051 for (border_col
= col
; border_col
< cols
; border_col
++) {
2052 diff
= border_orig
[border_col
] - border_recon
[border_col
];
2053 total_sse
+= diff
* diff
;
2056 border_orig
+= orig_stride
;
2057 border_recon
+= recon_stride
;
2061 orig
+= orig_stride
* 16;
2062 recon
+= recon_stride
* 16;
2065 /* Handle odd-sized height */
2066 for (; row
< rows
; row
++) {
2067 for (col
= 0; col
< cols
; col
++) {
2068 diff
= orig
[col
] - recon
[col
];
2069 total_sse
+= diff
* diff
;
2072 orig
+= orig_stride
;
2073 recon
+= recon_stride
;
2080 double psnr
[4]; // total/y/u/v
2081 uint64_t sse
[4]; // total/y/u/v
2082 uint32_t samples
[4]; // total/y/u/v
2085 static void calc_psnr(const YV12_BUFFER_CONFIG
*a
, const YV12_BUFFER_CONFIG
*b
,
2087 const int widths
[3] = {a
->y_width
, a
->uv_width
, a
->uv_width
};
2088 const int heights
[3] = {a
->y_height
, a
->uv_height
, a
->uv_height
};
2089 const uint8_t *a_planes
[3] = {a
->y_buffer
, a
->u_buffer
, a
->v_buffer
};
2090 const int a_strides
[3] = {a
->y_stride
, a
->uv_stride
, a
->uv_stride
};
2091 const uint8_t *b_planes
[3] = {b
->y_buffer
, b
->u_buffer
, b
->v_buffer
};
2092 const int b_strides
[3] = {b
->y_stride
, b
->uv_stride
, b
->uv_stride
};
2094 uint64_t total_sse
= 0;
2095 uint32_t total_samples
= 0;
2097 for (i
= 0; i
< 3; ++i
) {
2098 const int w
= widths
[i
];
2099 const int h
= heights
[i
];
2100 const uint32_t samples
= w
* h
;
2101 const double sse
= calc_plane_error(a_planes
[i
], a_strides
[i
],
2102 b_planes
[i
], b_strides
[i
],
2104 psnr
->sse
[1 + i
] = sse
;
2105 psnr
->samples
[1 + i
] = samples
;
2106 psnr
->psnr
[1 + i
] = vp9_mse2psnr(samples
, 255.0, sse
);
2109 total_samples
+= samples
;
2112 psnr
->sse
[0] = total_sse
;
2113 psnr
->samples
[0] = total_samples
;
2114 psnr
->psnr
[0] = vp9_mse2psnr(total_samples
, 255.0, total_sse
);
2117 static void generate_psnr_packet(VP9_COMP
*cpi
) {
2118 struct vpx_codec_cx_pkt pkt
;
2121 calc_psnr(cpi
->Source
, cpi
->common
.frame_to_show
, &psnr
);
2122 for (i
= 0; i
< 4; ++i
) {
2123 pkt
.data
.psnr
.samples
[i
] = psnr
.samples
[i
];
2124 pkt
.data
.psnr
.sse
[i
] = psnr
.sse
[i
];
2125 pkt
.data
.psnr
.psnr
[i
] = psnr
.psnr
[i
];
2127 pkt
.kind
= VPX_CODEC_PSNR_PKT
;
2128 vpx_codec_pkt_list_add(cpi
->output_pkt_list
, &pkt
);
2131 int vp9_use_as_reference(VP9_PTR ptr
, int ref_frame_flags
) {
2132 VP9_COMP
*cpi
= (VP9_COMP
*)(ptr
);
2134 if (ref_frame_flags
> 7)
2137 cpi
->ref_frame_flags
= ref_frame_flags
;
2141 int vp9_update_reference(VP9_PTR ptr
, int ref_frame_flags
) {
2142 VP9_COMP
*cpi
= (VP9_COMP
*)(ptr
);
2144 if (ref_frame_flags
> 7)
2147 cpi
->ext_refresh_golden_frame
= 0;
2148 cpi
->ext_refresh_alt_ref_frame
= 0;
2149 cpi
->ext_refresh_last_frame
= 0;
2151 if (ref_frame_flags
& VP9_LAST_FLAG
)
2152 cpi
->ext_refresh_last_frame
= 1;
2154 if (ref_frame_flags
& VP9_GOLD_FLAG
)
2155 cpi
->ext_refresh_golden_frame
= 1;
2157 if (ref_frame_flags
& VP9_ALT_FLAG
)
2158 cpi
->ext_refresh_alt_ref_frame
= 1;
2160 cpi
->ext_refresh_frame_flags_pending
= 1;
2164 static YV12_BUFFER_CONFIG
*get_vp9_ref_frame_buffer(VP9_COMP
*cpi
,
2165 VP9_REFFRAME ref_frame_flag
) {
2166 MV_REFERENCE_FRAME ref_frame
= NONE
;
2167 if (ref_frame_flag
== VP9_LAST_FLAG
)
2168 ref_frame
= LAST_FRAME
;
2169 else if (ref_frame_flag
== VP9_GOLD_FLAG
)
2170 ref_frame
= GOLDEN_FRAME
;
2171 else if (ref_frame_flag
== VP9_ALT_FLAG
)
2172 ref_frame
= ALTREF_FRAME
;
2174 return ref_frame
== NONE
? NULL
: get_ref_frame_buffer(cpi
, ref_frame
);
2177 int vp9_copy_reference_enc(VP9_PTR ptr
, VP9_REFFRAME ref_frame_flag
,
2178 YV12_BUFFER_CONFIG
*sd
) {
2179 VP9_COMP
*const cpi
= (VP9_COMP
*)ptr
;
2180 YV12_BUFFER_CONFIG
*cfg
= get_vp9_ref_frame_buffer(cpi
, ref_frame_flag
);
2182 vp8_yv12_copy_frame(cfg
, sd
);
2189 int vp9_get_reference_enc(VP9_PTR ptr
, int index
, YV12_BUFFER_CONFIG
**fb
) {
2190 VP9_COMP
*cpi
= (VP9_COMP
*)ptr
;
2191 VP9_COMMON
*cm
= &cpi
->common
;
2193 if (index
< 0 || index
>= REF_FRAMES
)
2196 *fb
= &cm
->yv12_fb
[cm
->ref_frame_map
[index
]];
2200 int vp9_set_reference_enc(VP9_PTR ptr
, VP9_REFFRAME ref_frame_flag
,
2201 YV12_BUFFER_CONFIG
*sd
) {
2202 VP9_COMP
*cpi
= (VP9_COMP
*)ptr
;
2203 YV12_BUFFER_CONFIG
*cfg
= get_vp9_ref_frame_buffer(cpi
, ref_frame_flag
);
2205 vp8_yv12_copy_frame(sd
, cfg
);
2212 int vp9_update_entropy(VP9_PTR comp
, int update
) {
2213 ((VP9_COMP
*)comp
)->ext_refresh_frame_context
= update
;
2214 ((VP9_COMP
*)comp
)->ext_refresh_frame_context_pending
= 1;
2219 #ifdef OUTPUT_YUV_SRC
2220 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG
*s
) {
2221 uint8_t *src
= s
->y_buffer
;
2222 int h
= s
->y_height
;
2225 fwrite(src
, s
->y_width
, 1, yuv_file
);
2233 fwrite(src
, s
->uv_width
, 1, yuv_file
);
2234 src
+= s
->uv_stride
;
2241 fwrite(src
, s
->uv_width
, 1, yuv_file
);
2242 src
+= s
->uv_stride
;
2247 #ifdef OUTPUT_YUV_REC
2248 void vp9_write_yuv_rec_frame(VP9_COMMON
*cm
) {
2249 YV12_BUFFER_CONFIG
*s
= cm
->frame_to_show
;
2250 uint8_t *src
= s
->y_buffer
;
2254 fwrite(src
, s
->y_width
, 1, yuv_rec_file
);
2262 fwrite(src
, s
->uv_width
, 1, yuv_rec_file
);
2263 src
+= s
->uv_stride
;
2270 fwrite(src
, s
->uv_width
, 1, yuv_rec_file
);
2271 src
+= s
->uv_stride
;
2275 if (s
->alpha_buffer
) {
2276 src
= s
->alpha_buffer
;
2277 h
= s
->alpha_height
;
2279 fwrite(src
, s
->alpha_width
, 1, yuv_rec_file
);
2280 src
+= s
->alpha_stride
;
2285 fflush(yuv_rec_file
);
2289 static void scale_and_extend_frame_nonnormative(YV12_BUFFER_CONFIG
*src_fb
,
2290 YV12_BUFFER_CONFIG
*dst_fb
) {
2291 const int in_w
= src_fb
->y_crop_width
;
2292 const int in_h
= src_fb
->y_crop_height
;
2293 const int out_w
= dst_fb
->y_crop_width
;
2294 const int out_h
= dst_fb
->y_crop_height
;
2295 const int in_w_uv
= src_fb
->uv_crop_width
;
2296 const int in_h_uv
= src_fb
->uv_crop_height
;
2297 const int out_w_uv
= dst_fb
->uv_crop_width
;
2298 const int out_h_uv
= dst_fb
->uv_crop_height
;
2301 uint8_t *srcs
[4] = {src_fb
->y_buffer
, src_fb
->u_buffer
, src_fb
->v_buffer
,
2302 src_fb
->alpha_buffer
};
2303 int src_strides
[4] = {src_fb
->y_stride
, src_fb
->uv_stride
, src_fb
->uv_stride
,
2304 src_fb
->alpha_stride
};
2306 uint8_t *dsts
[4] = {dst_fb
->y_buffer
, dst_fb
->u_buffer
, dst_fb
->v_buffer
,
2307 dst_fb
->alpha_buffer
};
2308 int dst_strides
[4] = {dst_fb
->y_stride
, dst_fb
->uv_stride
, dst_fb
->uv_stride
,
2309 dst_fb
->alpha_stride
};
2311 for (i
= 0; i
< MAX_MB_PLANE
; ++i
) {
2312 if (i
== 0 || i
== 3) {
2313 // Y and alpha planes
2314 vp9_resize_plane(srcs
[i
], in_h
, in_w
, src_strides
[i
],
2315 dsts
[i
], out_h
, out_w
, dst_strides
[i
]);
2318 vp9_resize_plane(srcs
[i
], in_h_uv
, in_w_uv
, src_strides
[i
],
2319 dsts
[i
], out_h_uv
, out_w_uv
, dst_strides
[i
]);
2322 vp8_yv12_extend_frame_borders(dst_fb
);
2325 static void scale_and_extend_frame(YV12_BUFFER_CONFIG
*src_fb
,
2326 YV12_BUFFER_CONFIG
*dst_fb
) {
2327 const int in_w
= src_fb
->y_crop_width
;
2328 const int in_h
= src_fb
->y_crop_height
;
2329 const int out_w
= dst_fb
->y_crop_width
;
2330 const int out_h
= dst_fb
->y_crop_height
;
2333 uint8_t *srcs
[4] = {src_fb
->y_buffer
, src_fb
->u_buffer
, src_fb
->v_buffer
,
2334 src_fb
->alpha_buffer
};
2335 int src_strides
[4] = {src_fb
->y_stride
, src_fb
->uv_stride
, src_fb
->uv_stride
,
2336 src_fb
->alpha_stride
};
2338 uint8_t *dsts
[4] = {dst_fb
->y_buffer
, dst_fb
->u_buffer
, dst_fb
->v_buffer
,
2339 dst_fb
->alpha_buffer
};
2340 int dst_strides
[4] = {dst_fb
->y_stride
, dst_fb
->uv_stride
, dst_fb
->uv_stride
,
2341 dst_fb
->alpha_stride
};
2343 for (y
= 0; y
< out_h
; y
+= 16) {
2344 for (x
= 0; x
< out_w
; x
+= 16) {
2345 for (i
= 0; i
< MAX_MB_PLANE
; ++i
) {
2346 const int factor
= (i
== 0 || i
== 3 ? 1 : 2);
2347 const int x_q4
= x
* (16 / factor
) * in_w
/ out_w
;
2348 const int y_q4
= y
* (16 / factor
) * in_h
/ out_h
;
2349 const int src_stride
= src_strides
[i
];
2350 const int dst_stride
= dst_strides
[i
];
2351 uint8_t *src
= srcs
[i
] + y
/ factor
* in_h
/ out_h
* src_stride
+
2352 x
/ factor
* in_w
/ out_w
;
2353 uint8_t *dst
= dsts
[i
] + y
/ factor
* dst_stride
+ x
/ factor
;
2355 vp9_convolve8(src
, src_stride
, dst
, dst_stride
,
2356 vp9_sub_pel_filters_8
[x_q4
& 0xf], 16 * in_w
/ out_w
,
2357 vp9_sub_pel_filters_8
[y_q4
& 0xf], 16 * in_h
/ out_h
,
2358 16 / factor
, 16 / factor
);
2363 vp8_yv12_extend_frame_borders(dst_fb
);
2366 static int find_fp_qindex() {
2369 for (i
= 0; i
< QINDEX_RANGE
; i
++) {
2370 if (vp9_convert_qindex_to_q(i
) >= 30.0) {
2375 if (i
== QINDEX_RANGE
)
2381 #define WRITE_RECON_BUFFER 0
2382 #if WRITE_RECON_BUFFER
2383 void write_cx_frame_to_file(YV12_BUFFER_CONFIG
*frame
, int this_frame
) {
2388 snprintf(filename
, sizeof(filename
), "cx\\y%04d.raw", this_frame
);
2389 yframe
= fopen(filename
, "wb");
2391 for (i
= 0; i
< frame
->y_height
; i
++)
2392 fwrite(frame
->y_buffer
+ i
* frame
->y_stride
,
2393 frame
->y_width
, 1, yframe
);
2396 snprintf(filename
, sizeof(filename
), "cx\\u%04d.raw", this_frame
);
2397 yframe
= fopen(filename
, "wb");
2399 for (i
= 0; i
< frame
->uv_height
; i
++)
2400 fwrite(frame
->u_buffer
+ i
* frame
->uv_stride
,
2401 frame
->uv_width
, 1, yframe
);
2404 snprintf(filename
, sizeof(filename
), "cx\\v%04d.raw", this_frame
);
2405 yframe
= fopen(filename
, "wb");
2407 for (i
= 0; i
< frame
->uv_height
; i
++)
2408 fwrite(frame
->v_buffer
+ i
* frame
->uv_stride
,
2409 frame
->uv_width
, 1, yframe
);
2415 static double compute_edge_pixel_proportion(YV12_BUFFER_CONFIG
*frame
) {
2416 #define EDGE_THRESH 128
2418 int num_edge_pels
= 0;
2419 int num_pels
= (frame
->y_height
- 2) * (frame
->y_width
- 2);
2420 uint8_t *prev
= frame
->y_buffer
+ 1;
2421 uint8_t *curr
= frame
->y_buffer
+ 1 + frame
->y_stride
;
2422 uint8_t *next
= frame
->y_buffer
+ 1 + 2 * frame
->y_stride
;
2423 for (i
= 1; i
< frame
->y_height
- 1; i
++) {
2424 for (j
= 1; j
< frame
->y_width
- 1; j
++) {
2425 /* Sobel hor and ver gradients */
2426 int v
= 2 * (curr
[1] - curr
[-1]) + (prev
[1] - prev
[-1]) +
2427 (next
[1] - next
[-1]);
2428 int h
= 2 * (prev
[0] - next
[0]) + (prev
[1] - next
[1]) +
2429 (prev
[-1] - next
[-1]);
2430 h
= (h
< 0 ? -h
: h
);
2431 v
= (v
< 0 ? -v
: v
);
2432 if (h
> EDGE_THRESH
|| v
> EDGE_THRESH
)
2438 curr
+= frame
->y_stride
- frame
->y_width
+ 2;
2439 prev
+= frame
->y_stride
- frame
->y_width
+ 2;
2440 next
+= frame
->y_stride
- frame
->y_width
+ 2;
2442 return (double)num_edge_pels
/ num_pels
;
2445 // Function to test for conditions that indicate we should loop
2446 // back and recode a frame.
2447 static int recode_loop_test(VP9_COMP
*cpi
,
2448 int high_limit
, int low_limit
,
2449 int q
, int maxq
, int minq
) {
2450 int force_recode
= 0;
2451 VP9_COMMON
*cm
= &cpi
->common
;
2453 // Special case trap if maximum allowed frame size exceeded.
2454 if (cpi
->rc
.projected_frame_size
> cpi
->rc
.max_frame_bandwidth
) {
2457 // Is frame recode allowed.
2458 // Yes if either recode mode 1 is selected or mode 2 is selected
2459 // and the frame is a key frame, golden frame or alt_ref_frame
2460 } else if ((cpi
->sf
.recode_loop
== 1) ||
2461 ((cpi
->sf
.recode_loop
== 2) &&
2462 ((cm
->frame_type
== KEY_FRAME
) ||
2463 cpi
->refresh_golden_frame
||
2464 cpi
->refresh_alt_ref_frame
))) {
2465 // General over and under shoot tests
2466 if (((cpi
->rc
.projected_frame_size
> high_limit
) && (q
< maxq
)) ||
2467 ((cpi
->rc
.projected_frame_size
< low_limit
) && (q
> minq
))) {
2469 } else if (cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
) {
2470 // Deal with frame undershoot and whether or not we are
2471 // below the automatically set cq level.
2472 if (q
> cpi
->cq_target_quality
&&
2473 cpi
->rc
.projected_frame_size
<
2474 ((cpi
->rc
.this_frame_target
* 7) >> 3)) {
2479 return force_recode
;
2482 static void update_reference_frames(VP9_COMP
* const cpi
) {
2483 VP9_COMMON
* const cm
= &cpi
->common
;
2485 // At this point the new frame has been encoded.
2486 // If any buffer copy / swapping is signaled it should be done here.
2487 if (cm
->frame_type
== KEY_FRAME
) {
2488 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2489 &cm
->ref_frame_map
[cpi
->gld_fb_idx
], cm
->new_fb_idx
);
2490 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2491 &cm
->ref_frame_map
[cpi
->alt_fb_idx
], cm
->new_fb_idx
);
2493 #if CONFIG_MULTIPLE_ARF
2494 else if (!cpi
->multi_arf_enabled
&& cpi
->refresh_golden_frame
&&
2495 !cpi
->refresh_alt_ref_frame
) {
2497 else if (cpi
->refresh_golden_frame
&& !cpi
->refresh_alt_ref_frame
&&
2500 /* Preserve the previously existing golden frame and update the frame in
2501 * the alt ref slot instead. This is highly specific to the current use of
2502 * alt-ref as a forward reference, and this needs to be generalized as
2503 * other uses are implemented (like RTC/temporal scaling)
2505 * The update to the buffer in the alt ref slot was signaled in
2506 * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated
2507 * as the golden frame next time.
2511 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2512 &cm
->ref_frame_map
[cpi
->alt_fb_idx
], cm
->new_fb_idx
);
2514 tmp
= cpi
->alt_fb_idx
;
2515 cpi
->alt_fb_idx
= cpi
->gld_fb_idx
;
2516 cpi
->gld_fb_idx
= tmp
;
2517 } else { /* For non key/golden frames */
2518 if (cpi
->refresh_alt_ref_frame
) {
2519 int arf_idx
= cpi
->alt_fb_idx
;
2520 #if CONFIG_MULTIPLE_ARF
2521 if (cpi
->multi_arf_enabled
) {
2522 arf_idx
= cpi
->arf_buffer_idx
[cpi
->sequence_number
+ 1];
2525 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2526 &cm
->ref_frame_map
[arf_idx
], cm
->new_fb_idx
);
2529 if (cpi
->refresh_golden_frame
) {
2530 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2531 &cm
->ref_frame_map
[cpi
->gld_fb_idx
], cm
->new_fb_idx
);
2535 if (cpi
->refresh_last_frame
) {
2536 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2537 &cm
->ref_frame_map
[cpi
->lst_fb_idx
], cm
->new_fb_idx
);
2541 static void loopfilter_frame(VP9_COMP
*cpi
, VP9_COMMON
*cm
) {
2542 MACROBLOCKD
*xd
= &cpi
->mb
.e_mbd
;
2543 struct loopfilter
*lf
= &cm
->lf
;
2545 lf
->filter_level
= 0;
2547 struct vpx_usec_timer timer
;
2549 vp9_clear_system_state();
2551 vpx_usec_timer_start(&timer
);
2553 if (cpi
->compressor_speed
== 3)
2554 lf
->filter_level
= 4;
2556 vp9_pick_filter_level(cpi
->Source
, cpi
, cpi
->sf
.use_fast_lpf_pick
);
2558 vpx_usec_timer_mark(&timer
);
2559 cpi
->time_pick_lpf
+= vpx_usec_timer_elapsed(&timer
);
2562 if (lf
->filter_level
> 0) {
2563 vp9_set_alt_lf_level(cpi
, lf
->filter_level
);
2564 vp9_loop_filter_frame(cm
, xd
, lf
->filter_level
, 0, 0);
2567 vp9_extend_frame_inner_borders(cm
->frame_to_show
,
2568 cm
->subsampling_x
, cm
->subsampling_y
);
2571 static void scale_references(VP9_COMP
*cpi
) {
2572 VP9_COMMON
*cm
= &cpi
->common
;
2573 MV_REFERENCE_FRAME ref_frame
;
2575 for (ref_frame
= LAST_FRAME
; ref_frame
<= ALTREF_FRAME
; ++ref_frame
) {
2576 const int idx
= cm
->ref_frame_map
[get_ref_frame_idx(cpi
, ref_frame
)];
2577 YV12_BUFFER_CONFIG
*ref
= &cm
->yv12_fb
[idx
];
2579 if (ref
->y_crop_width
!= cm
->width
||
2580 ref
->y_crop_height
!= cm
->height
) {
2581 const int new_fb
= get_free_fb(cm
);
2582 vp9_realloc_frame_buffer(&cm
->yv12_fb
[new_fb
],
2583 cm
->width
, cm
->height
,
2584 cm
->subsampling_x
, cm
->subsampling_y
,
2585 VP9_ENC_BORDER_IN_PIXELS
);
2586 scale_and_extend_frame(ref
, &cm
->yv12_fb
[new_fb
]);
2587 cpi
->scaled_ref_idx
[ref_frame
- 1] = new_fb
;
2589 cpi
->scaled_ref_idx
[ref_frame
- 1] = idx
;
2590 cm
->fb_idx_ref_cnt
[idx
]++;
2595 static void release_scaled_references(VP9_COMP
*cpi
) {
2596 VP9_COMMON
*cm
= &cpi
->common
;
2599 for (i
= 0; i
< 3; i
++)
2600 cm
->fb_idx_ref_cnt
[cpi
->scaled_ref_idx
[i
]]--;
2603 static void full_to_model_count(unsigned int *model_count
,
2604 unsigned int *full_count
) {
2606 model_count
[ZERO_TOKEN
] = full_count
[ZERO_TOKEN
];
2607 model_count
[ONE_TOKEN
] = full_count
[ONE_TOKEN
];
2608 model_count
[TWO_TOKEN
] = full_count
[TWO_TOKEN
];
2609 for (n
= THREE_TOKEN
; n
< EOB_TOKEN
; ++n
)
2610 model_count
[TWO_TOKEN
] += full_count
[n
];
2611 model_count
[EOB_MODEL_TOKEN
] = full_count
[EOB_TOKEN
];
2614 static void full_to_model_counts(vp9_coeff_count_model
*model_count
,
2615 vp9_coeff_count
*full_count
) {
2618 for (i
= 0; i
< PLANE_TYPES
; ++i
)
2619 for (j
= 0; j
< REF_TYPES
; ++j
)
2620 for (k
= 0; k
< COEF_BANDS
; ++k
)
2621 for (l
= 0; l
< BAND_COEFF_CONTEXTS(k
); ++l
)
2622 full_to_model_count(model_count
[i
][j
][k
][l
], full_count
[i
][j
][k
][l
]);
2625 #if 0 && CONFIG_INTERNAL_STATS
2626 static void output_frame_level_debug_stats(VP9_COMP
*cpi
) {
2627 VP9_COMMON
*const cm
= &cpi
->common
;
2628 FILE *const f
= fopen("tmp.stt", cm
->current_video_frame
? "a" : "w");
2631 vp9_clear_system_state(); // __asm emms;
2633 recon_err
= vp9_calc_ss_err(cpi
->Source
, get_frame_new_buffer(cm
));
2635 if (cpi
->twopass
.total_left_stats
.coded_error
!= 0.0)
2636 fprintf(f
, "%10u %10d %10d %10d %10d %10d "
2637 "%10"PRId64
" %10"PRId64
" %10d "
2638 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2639 "%6d %6d %5d %5d %5d "
2640 "%10"PRId64
" %10.3lf"
2641 "%10lf %8u %10d %10d %10d\n",
2642 cpi
->common
.current_video_frame
, cpi
->rc
.this_frame_target
,
2643 cpi
->rc
.projected_frame_size
,
2644 cpi
->rc
.projected_frame_size
/ cpi
->common
.MBs
,
2645 (cpi
->rc
.projected_frame_size
- cpi
->rc
.this_frame_target
),
2646 cpi
->rc
.total_target_vs_actual
,
2647 (cpi
->oxcf
.starting_buffer_level
- cpi
->rc
.bits_off_target
),
2648 cpi
->rc
.total_actual_bits
, cm
->base_qindex
,
2649 vp9_convert_qindex_to_q(cm
->base_qindex
),
2650 (double)vp9_dc_quant(cm
->base_qindex
, 0) / 4.0,
2651 vp9_convert_qindex_to_q(cpi
->rc
.active_worst_quality
), cpi
->rc
.avg_q
,
2652 vp9_convert_qindex_to_q(cpi
->rc
.ni_av_qi
),
2653 vp9_convert_qindex_to_q(cpi
->cq_target_quality
),
2654 cpi
->refresh_last_frame
, cpi
->refresh_golden_frame
,
2655 cpi
->refresh_alt_ref_frame
, cm
->frame_type
, cpi
->rc
.gfu_boost
,
2656 cpi
->twopass
.bits_left
,
2657 cpi
->twopass
.total_left_stats
.coded_error
,
2658 cpi
->twopass
.bits_left
/
2659 (1 + cpi
->twopass
.total_left_stats
.coded_error
),
2660 cpi
->tot_recode_hits
, recon_err
, cpi
->rc
.kf_boost
,
2661 cpi
->twopass
.kf_zeromotion_pct
);
2666 FILE *const fmodes
= fopen("Modes.stt", "a");
2669 fprintf(fmodes
, "%6d:%1d:%1d:%1d ", cpi
->common
.current_video_frame
,
2670 cm
->frame_type
, cpi
->refresh_golden_frame
,
2671 cpi
->refresh_alt_ref_frame
);
2673 for (i
= 0; i
< MAX_MODES
; ++i
)
2674 fprintf(fmodes
, "%5d ", cpi
->mode_chosen_counts
[i
]);
2675 for (i
= 0; i
< MAX_REFS
; ++i
)
2676 fprintf(fmodes
, "%5d ", cpi
->sub8x8_mode_chosen_counts
[i
]);
2678 fprintf(fmodes
, "\n");
2685 static void encode_with_recode_loop(VP9_COMP
*cpi
,
2691 int frame_over_shoot_limit
,
2692 int frame_under_shoot_limit
) {
2693 VP9_COMMON
*const cm
= &cpi
->common
;
2696 int overshoot_seen
= 0;
2697 int undershoot_seen
= 0;
2698 int q_low
= bottom_index
, q_high
= top_index
;
2701 vp9_clear_system_state(); // __asm emms;
2703 vp9_set_quantizer(cpi
, *q
);
2705 if (loop_count
== 0) {
2706 // Set up entropy context depending on frame type. The decoder mandates
2707 // the use of the default context, index 0, for keyframes and inter
2708 // frames where the error_resilient_mode or intra_only flag is set. For
2709 // other inter-frames the encoder currently uses only two contexts;
2710 // context 1 for ALTREF frames and context 0 for the others.
2711 if (cm
->frame_type
== KEY_FRAME
) {
2712 vp9_setup_key_frame(cpi
);
2714 if (!cm
->intra_only
&& !cm
->error_resilient_mode
) {
2715 cpi
->common
.frame_context_idx
= cpi
->refresh_alt_ref_frame
;
2717 vp9_setup_inter_frame(cpi
);
2721 // Variance adaptive and in frame q adjustment experiments are mutually
2723 if (cpi
->oxcf
.aq_mode
== VARIANCE_AQ
) {
2724 vp9_vaq_frame_setup(cpi
);
2725 } else if (cpi
->oxcf
.aq_mode
== COMPLEXITY_AQ
) {
2726 setup_in_frame_q_adj(cpi
);
2729 // transform / motion compensation build reconstruction frame
2731 vp9_encode_frame(cpi
);
2733 // Update the skip mb flag probabilities based on the distribution
2734 // seen in the last encoder iteration.
2735 // update_base_skip_probs(cpi);
2737 vp9_clear_system_state(); // __asm emms;
2739 // Dummy pack of the bitstream using up to date stats to get an
2740 // accurate estimate of output frame size to determine if we need
2742 if (cpi
->sf
.recode_loop
!= 0) {
2743 vp9_save_coding_context(cpi
);
2744 cpi
->dummy_packing
= 1;
2745 if (cpi
->compressor_speed
!= 3)
2746 vp9_pack_bitstream(cpi
, dest
, size
);
2748 cpi
->rc
.projected_frame_size
= (*size
) << 3;
2749 vp9_restore_coding_context(cpi
);
2751 if (frame_over_shoot_limit
== 0)
2752 frame_over_shoot_limit
= 1;
2755 if (cpi
->oxcf
.end_usage
== USAGE_CONSTANT_QUALITY
) {
2758 if ((cm
->frame_type
== KEY_FRAME
) &&
2759 cpi
->rc
.this_key_frame_forced
&&
2760 (cpi
->rc
.projected_frame_size
< cpi
->rc
.max_frame_bandwidth
)) {
2762 int kf_err
= vp9_calc_ss_err(cpi
->Source
, get_frame_new_buffer(cm
));
2764 int high_err_target
= cpi
->ambient_err
;
2765 int low_err_target
= cpi
->ambient_err
>> 1;
2767 // Prevent possible divide by zero error below for perfect KF
2770 // The key frame is not good enough or we can afford
2771 // to make it better without undue risk of popping.
2772 if ((kf_err
> high_err_target
&&
2773 cpi
->rc
.projected_frame_size
<= frame_over_shoot_limit
) ||
2774 (kf_err
> low_err_target
&&
2775 cpi
->rc
.projected_frame_size
<= frame_under_shoot_limit
)) {
2777 q_high
= *q
> q_low
? *q
- 1 : q_low
;
2780 *q
= ((*q
) * high_err_target
) / kf_err
;
2781 *q
= MIN((*q
), (q_high
+ q_low
) >> 1);
2782 } else if (kf_err
< low_err_target
&&
2783 cpi
->rc
.projected_frame_size
>= frame_under_shoot_limit
) {
2784 // The key frame is much better than the previous frame
2786 q_low
= *q
< q_high
? *q
+ 1 : q_high
;
2789 *q
= ((*q
) * low_err_target
) / kf_err
;
2790 *q
= MIN((*q
), (q_high
+ q_low
+ 1) >> 1);
2793 // Clamp Q to upper and lower limits:
2794 *q
= clamp(*q
, q_low
, q_high
);
2796 loop
= *q
!= last_q
;
2797 } else if (recode_loop_test(
2798 cpi
, frame_over_shoot_limit
, frame_under_shoot_limit
,
2799 *q
, MAX(q_high
, top_index
), bottom_index
)) {
2800 // Is the projected frame size out of range and are we allowed
2801 // to attempt to recode.
2805 // Frame size out of permitted range:
2806 // Update correction factor & compute new Q to try...
2808 // Frame is too large
2809 if (cpi
->rc
.projected_frame_size
> cpi
->rc
.this_frame_target
) {
2810 // Special case if the projected size is > the max allowed.
2811 if (cpi
->rc
.projected_frame_size
>= cpi
->rc
.max_frame_bandwidth
)
2812 q_high
= cpi
->rc
.worst_quality
;
2814 // Raise Qlow as to at least the current value
2815 q_low
= *q
< q_high
? *q
+ 1 : q_high
;
2817 if (undershoot_seen
|| loop_count
> 1) {
2818 // Update rate_correction_factor unless
2819 vp9_rc_update_rate_correction_factors(cpi
, 1);
2821 *q
= (q_high
+ q_low
+ 1) / 2;
2823 // Update rate_correction_factor unless
2824 vp9_rc_update_rate_correction_factors(cpi
, 0);
2826 *q
= vp9_rc_regulate_q(cpi
, cpi
->rc
.this_frame_target
,
2827 bottom_index
, MAX(q_high
, top_index
));
2829 while (*q
< q_low
&& retries
< 10) {
2830 vp9_rc_update_rate_correction_factors(cpi
, 0);
2831 *q
= vp9_rc_regulate_q(cpi
, cpi
->rc
.this_frame_target
,
2832 bottom_index
, MAX(q_high
, top_index
));
2839 // Frame is too small
2840 q_high
= *q
> q_low
? *q
- 1 : q_low
;
2842 if (overshoot_seen
|| loop_count
> 1) {
2843 vp9_rc_update_rate_correction_factors(cpi
, 1);
2844 *q
= (q_high
+ q_low
) / 2;
2846 vp9_rc_update_rate_correction_factors(cpi
, 0);
2847 *q
= vp9_rc_regulate_q(cpi
, cpi
->rc
.this_frame_target
,
2848 bottom_index
, top_index
);
2849 // Special case reset for qlow for constrained quality.
2850 // This should only trigger where there is very substantial
2851 // undershoot on a frame and the auto cq level is above
2852 // the user passsed in value.
2853 if (cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
&&
2858 while (*q
> q_high
&& retries
< 10) {
2859 vp9_rc_update_rate_correction_factors(cpi
, 0);
2860 *q
= vp9_rc_regulate_q(cpi
, cpi
->rc
.this_frame_target
,
2861 bottom_index
, top_index
);
2866 undershoot_seen
= 1;
2869 // Clamp Q to upper and lower limits:
2870 *q
= clamp(*q
, q_low
, q_high
);
2872 loop
= *q
!= last_q
;
2878 // Special case for overlay frame.
2879 if (cpi
->rc
.is_src_frame_alt_ref
&&
2880 (cpi
->rc
.projected_frame_size
< cpi
->rc
.max_frame_bandwidth
))
2886 #if CONFIG_INTERNAL_STATS
2887 cpi
->tot_recode_hits
++;
2893 static void get_ref_frame_flags(VP9_COMP
*cpi
) {
2894 if (cpi
->refresh_last_frame
& cpi
->refresh_golden_frame
)
2895 cpi
->gold_is_last
= 1;
2896 else if (cpi
->refresh_last_frame
^ cpi
->refresh_golden_frame
)
2897 cpi
->gold_is_last
= 0;
2899 if (cpi
->refresh_last_frame
& cpi
->refresh_alt_ref_frame
)
2900 cpi
->alt_is_last
= 1;
2901 else if (cpi
->refresh_last_frame
^ cpi
->refresh_alt_ref_frame
)
2902 cpi
->alt_is_last
= 0;
2904 if (cpi
->refresh_alt_ref_frame
& cpi
->refresh_golden_frame
)
2905 cpi
->gold_is_alt
= 1;
2906 else if (cpi
->refresh_alt_ref_frame
^ cpi
->refresh_golden_frame
)
2907 cpi
->gold_is_alt
= 0;
2909 cpi
->ref_frame_flags
= VP9_ALT_FLAG
| VP9_GOLD_FLAG
| VP9_LAST_FLAG
;
2911 if (cpi
->gold_is_last
)
2912 cpi
->ref_frame_flags
&= ~VP9_GOLD_FLAG
;
2914 if (cpi
->alt_is_last
)
2915 cpi
->ref_frame_flags
&= ~VP9_ALT_FLAG
;
2917 if (cpi
->gold_is_alt
)
2918 cpi
->ref_frame_flags
&= ~VP9_ALT_FLAG
;
2921 static void set_ext_overrides(VP9_COMP
*cpi
) {
2922 // Overrides the defaults with the externally supplied values with
2923 // vp9_update_reference() and vp9_update_entropy() calls
2924 // Note: The overrides are valid only for the next frame passed
2925 // to encode_frame_to_data_rate() function
2926 if (cpi
->ext_refresh_frame_context_pending
) {
2927 cpi
->common
.refresh_frame_context
= cpi
->ext_refresh_frame_context
;
2928 cpi
->ext_refresh_frame_context_pending
= 0;
2930 if (cpi
->ext_refresh_frame_flags_pending
) {
2931 cpi
->refresh_last_frame
= cpi
->ext_refresh_last_frame
;
2932 cpi
->refresh_golden_frame
= cpi
->ext_refresh_golden_frame
;
2933 cpi
->refresh_alt_ref_frame
= cpi
->ext_refresh_alt_ref_frame
;
2934 cpi
->ext_refresh_frame_flags_pending
= 0;
2938 static void encode_frame_to_data_rate(VP9_COMP
*cpi
,
2941 unsigned int *frame_flags
) {
2942 VP9_COMMON
*const cm
= &cpi
->common
;
2945 int frame_over_shoot_limit
;
2946 int frame_under_shoot_limit
;
2950 SPEED_FEATURES
*const sf
= &cpi
->sf
;
2951 unsigned int max_mv_def
= MIN(cpi
->common
.width
, cpi
->common
.height
);
2952 struct segmentation
*const seg
= &cm
->seg
;
2954 set_ext_overrides(cpi
);
2956 /* Scale the source buffer, if required. */
2957 if (cm
->mi_cols
* 8 != cpi
->un_scaled_source
->y_width
||
2958 cm
->mi_rows
* 8 != cpi
->un_scaled_source
->y_height
) {
2959 scale_and_extend_frame_nonnormative(cpi
->un_scaled_source
,
2960 &cpi
->scaled_source
);
2961 cpi
->Source
= &cpi
->scaled_source
;
2963 cpi
->Source
= cpi
->un_scaled_source
;
2965 scale_references(cpi
);
2967 // Clear down mmx registers to allow floating point in what follows.
2968 vp9_clear_system_state();
2970 // Clear zbin over-quant value and mode boost values.
2971 cpi
->zbin_mode_boost
= 0;
2973 // Enable or disable mode based tweaking of the zbin.
2974 // For 2 pass only used where GF/ARF prediction quality
2975 // is above a threshold.
2976 cpi
->zbin_mode_boost
= 0;
2977 cpi
->zbin_mode_boost_enabled
= 0;
2979 // Current default encoder behavior for the altref sign bias.
2980 cpi
->common
.ref_frame_sign_bias
[ALTREF_FRAME
] = cpi
->rc
.source_alt_ref_active
;
2982 // Set default state for segment based loop filter update flags.
2983 cm
->lf
.mode_ref_delta_update
= 0;
2985 // Initialize cpi->mv_step_param to default based on max resolution.
2986 cpi
->mv_step_param
= vp9_init_search_range(cpi
, max_mv_def
);
2987 // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate.
2988 if (sf
->auto_mv_step_size
) {
2989 if (frame_is_intra_only(&cpi
->common
)) {
2990 // Initialize max_mv_magnitude for use in the first INTER frame
2991 // after a key/intra-only frame.
2992 cpi
->max_mv_magnitude
= max_mv_def
;
2995 // Allow mv_steps to correspond to twice the max mv magnitude found
2996 // in the previous frame, capped by the default max_mv_magnitude based
2998 cpi
->mv_step_param
= vp9_init_search_range(
2999 cpi
, MIN(max_mv_def
, 2 * cpi
->max_mv_magnitude
));
3000 cpi
->max_mv_magnitude
= 0;
3004 // Set various flags etc to special state if it is a key frame.
3005 if (frame_is_intra_only(cm
)) {
3006 vp9_setup_key_frame(cpi
);
3007 // Reset the loop filter deltas and segmentation map.
3008 vp9_reset_segment_features(&cm
->seg
);
3010 // If segmentation is enabled force a map update for key frames.
3012 seg
->update_map
= 1;
3013 seg
->update_data
= 1;
3016 // The alternate reference frame cannot be active for a key frame.
3017 cpi
->rc
.source_alt_ref_active
= 0;
3019 cm
->error_resilient_mode
= (cpi
->oxcf
.error_resilient_mode
!= 0);
3020 cm
->frame_parallel_decoding_mode
=
3021 (cpi
->oxcf
.frame_parallel_decoding_mode
!= 0);
3022 if (cm
->error_resilient_mode
) {
3023 cm
->frame_parallel_decoding_mode
= 1;
3024 cm
->reset_frame_context
= 0;
3025 cm
->refresh_frame_context
= 0;
3026 } else if (cm
->intra_only
) {
3027 // Only reset the current context.
3028 cm
->reset_frame_context
= 2;
3032 // Configure experimental use of segmentation for enhanced coding of
3033 // static regions if indicated.
3034 // Only allowed in second pass of two pass (as requires lagged coding)
3035 // and if the relevant speed feature flag is set.
3036 if ((cpi
->pass
== 2) && (cpi
->sf
.static_segmentation
)) {
3037 configure_static_seg_features(cpi
);
3040 // For 1 pass CBR, check if we are dropping this frame.
3041 // Never drop on key frame.
3042 if (cpi
->pass
== 0 &&
3043 cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
&&
3044 cm
->frame_type
!= KEY_FRAME
) {
3045 if (vp9_drop_frame(cpi
)) {
3046 // Update buffer level with zero size, update frame counters, and return.
3047 vp9_update_buffer_level(cpi
, 0);
3048 cm
->last_frame_type
= cm
->frame_type
;
3049 vp9_rc_postencode_update_drop_frame(cpi
);
3050 cm
->current_video_frame
++;
3055 vp9_clear_system_state();
3057 vp9_zero(cpi
->rd_tx_select_threshes
);
3059 #if CONFIG_VP9_POSTPROC
3060 if (cpi
->oxcf
.noise_sensitivity
> 0) {
3062 switch (cpi
->oxcf
.noise_sensitivity
) {
3080 vp9_denoise(cpi
->Source
, cpi
->Source
, l
);
3084 #ifdef OUTPUT_YUV_SRC
3085 vp9_write_yuv_frame(cpi
->Source
);
3088 // Decide how big to make the frame.
3089 vp9_rc_pick_frame_size_target(cpi
);
3091 // Decide frame size bounds
3092 vp9_rc_compute_frame_size_bounds(cpi
, cpi
->rc
.this_frame_target
,
3093 &frame_under_shoot_limit
,
3094 &frame_over_shoot_limit
);
3096 // Decide q and q bounds.
3097 q
= vp9_rc_pick_q_and_adjust_q_bounds(cpi
,
3101 // JBB : This is realtime mode. In real time mode the first frame
3102 // should be larger. Q of 0 is disabled because we force tx size to be
3104 if (cpi
->compressor_speed
== 3) {
3105 if (cpi
->common
.current_video_frame
== 0)
3112 if (!frame_is_intra_only(cm
)) {
3113 cm
->interp_filter
= DEFAULT_INTERP_FILTER
;
3114 /* TODO: Decide this more intelligently */
3115 set_high_precision_mv(cpi
, (q
< HIGH_PRECISION_MV_QTHRESH
));
3118 encode_with_recode_loop(cpi
,
3124 frame_over_shoot_limit
,
3125 frame_under_shoot_limit
);
3127 // Special case code to reduce pulsing when key frames are forced at a
3128 // fixed interval. Note the reconstruction error if it is the frame before
3129 // the force key frame
3130 if (cpi
->rc
.next_key_frame_forced
&& cpi
->rc
.frames_to_key
== 1) {
3131 cpi
->ambient_err
= vp9_calc_ss_err(cpi
->Source
, get_frame_new_buffer(cm
));
3134 // If the encoder forced a KEY_FRAME decision
3135 if (cm
->frame_type
== KEY_FRAME
)
3136 cpi
->refresh_last_frame
= 1;
3138 cm
->frame_to_show
= get_frame_new_buffer(cm
);
3140 #if WRITE_RECON_BUFFER
3142 write_cx_frame_to_file(cm
->frame_to_show
,
3143 cm
->current_video_frame
);
3145 write_cx_frame_to_file(cm
->frame_to_show
,
3146 cm
->current_video_frame
+ 1000);
3149 // Pick the loop filter level for the frame.
3150 loopfilter_frame(cpi
, cm
);
3152 #if WRITE_RECON_BUFFER
3154 write_cx_frame_to_file(cm
->frame_to_show
,
3155 cm
->current_video_frame
+ 2000);
3157 write_cx_frame_to_file(cm
->frame_to_show
,
3158 cm
->current_video_frame
+ 3000);
3161 // build the bitstream
3162 cpi
->dummy_packing
= 0;
3163 vp9_pack_bitstream(cpi
, dest
, size
);
3165 if (cm
->seg
.update_map
)
3166 update_reference_segmentation_map(cpi
);
3168 release_scaled_references(cpi
);
3169 update_reference_frames(cpi
);
3171 for (t
= TX_4X4
; t
<= TX_32X32
; t
++)
3172 full_to_model_counts(cpi
->common
.counts
.coef
[t
],
3173 cpi
->coef_counts
[t
]);
3174 if (!cpi
->common
.error_resilient_mode
&&
3175 !cpi
->common
.frame_parallel_decoding_mode
) {
3176 vp9_adapt_coef_probs(&cpi
->common
);
3179 if (!frame_is_intra_only(&cpi
->common
)) {
3180 if (!cpi
->common
.error_resilient_mode
&&
3181 !cpi
->common
.frame_parallel_decoding_mode
) {
3182 vp9_adapt_mode_probs(&cpi
->common
);
3183 vp9_adapt_mv_probs(&cpi
->common
, cpi
->common
.allow_high_precision_mv
);
3187 #ifdef ENTROPY_STATS
3188 vp9_update_mode_context_stats(cpi
);
3191 /* Move storing frame_type out of the above loop since it is also
3192 * needed in motion search besides loopfilter */
3193 cm
->last_frame_type
= cm
->frame_type
;
3196 output_frame_level_debug_stats(cpi
);
3198 if (cpi
->refresh_golden_frame
== 1)
3199 cm
->frame_flags
= cm
->frame_flags
| FRAMEFLAGS_GOLDEN
;
3201 cm
->frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_GOLDEN
;
3203 if (cpi
->refresh_alt_ref_frame
== 1)
3204 cm
->frame_flags
= cm
->frame_flags
| FRAMEFLAGS_ALTREF
;
3206 cm
->frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_ALTREF
;
3208 get_ref_frame_flags(cpi
);
3210 vp9_rc_postencode_update(cpi
, *size
);
3212 if (cm
->frame_type
== KEY_FRAME
) {
3213 // Tell the caller that the frame was coded as a key frame
3214 *frame_flags
= cm
->frame_flags
| FRAMEFLAGS_KEY
;
3216 #if CONFIG_MULTIPLE_ARF
3217 // Reset the sequence number.
3218 if (cpi
->multi_arf_enabled
) {
3219 cpi
->sequence_number
= 0;
3220 cpi
->frame_coding_order_period
= cpi
->new_frame_coding_order_period
;
3221 cpi
->new_frame_coding_order_period
= -1;
3225 *frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_KEY
;
3227 #if CONFIG_MULTIPLE_ARF
3228 /* Increment position in the coded frame sequence. */
3229 if (cpi
->multi_arf_enabled
) {
3230 ++cpi
->sequence_number
;
3231 if (cpi
->sequence_number
>= cpi
->frame_coding_order_period
) {
3232 cpi
->sequence_number
= 0;
3233 cpi
->frame_coding_order_period
= cpi
->new_frame_coding_order_period
;
3234 cpi
->new_frame_coding_order_period
= -1;
3236 cpi
->this_frame_weight
= cpi
->arf_weight
[cpi
->sequence_number
];
3237 assert(cpi
->this_frame_weight
>= 0);
3242 // Clear the one shot update flags for segmentation map and mode/ref loop
3244 cm
->seg
.update_map
= 0;
3245 cm
->seg
.update_data
= 0;
3246 cm
->lf
.mode_ref_delta_update
= 0;
3248 // keep track of the last coded dimensions
3249 cm
->last_width
= cm
->width
;
3250 cm
->last_height
= cm
->height
;
3252 // reset to normal state now that we are done.
3253 cm
->last_show_frame
= cm
->show_frame
;
3254 if (cm
->show_frame
) {
3255 // current mip will be the prev_mip for the next frame
3256 MODE_INFO
*temp
= cm
->prev_mip
;
3257 MODE_INFO
**temp2
= cm
->prev_mi_grid_base
;
3258 cm
->prev_mip
= cm
->mip
;
3260 cm
->prev_mi_grid_base
= cm
->mi_grid_base
;
3261 cm
->mi_grid_base
= temp2
;
3263 // update the upper left visible macroblock ptrs
3264 cm
->mi
= cm
->mip
+ cm
->mode_info_stride
+ 1;
3265 cm
->mi_grid_visible
= cm
->mi_grid_base
+ cm
->mode_info_stride
+ 1;
3267 cpi
->mb
.e_mbd
.mi_8x8
= cm
->mi_grid_visible
;
3268 cpi
->mb
.e_mbd
.mi_8x8
[0] = cm
->mi
;
3270 // Don't increment frame counters if this was an altref buffer
3271 // update not a real frame
3272 ++cm
->current_video_frame
;
3275 cm
->prev_mi
= cm
->prev_mip
+ cm
->mode_info_stride
+ 1;
3276 cm
->prev_mi_grid_visible
= cm
->prev_mi_grid_base
+ cm
->mode_info_stride
+ 1;
3279 static void SvcEncode(VP9_COMP
*cpi
, size_t *size
, uint8_t *dest
,
3280 unsigned int *frame_flags
) {
3281 vp9_get_svc_params(cpi
);
3282 encode_frame_to_data_rate(cpi
, size
, dest
, frame_flags
);
3285 static void Pass0Encode(VP9_COMP
*cpi
, size_t *size
, uint8_t *dest
,
3286 unsigned int *frame_flags
) {
3287 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
) {
3288 vp9_get_one_pass_cbr_params(cpi
);
3290 vp9_get_one_pass_params(cpi
);
3292 encode_frame_to_data_rate(cpi
, size
, dest
, frame_flags
);
3295 static void Pass1Encode(VP9_COMP
*cpi
, size_t *size
, uint8_t *dest
,
3296 unsigned int *frame_flags
) {
3301 vp9_get_first_pass_params(cpi
);
3302 vp9_set_quantizer(cpi
, find_fp_qindex());
3303 vp9_first_pass(cpi
);
3306 static void Pass2Encode(VP9_COMP
*cpi
, size_t *size
,
3307 uint8_t *dest
, unsigned int *frame_flags
) {
3308 cpi
->enable_encode_breakout
= 1;
3310 vp9_get_second_pass_params(cpi
);
3311 encode_frame_to_data_rate(cpi
, size
, dest
, frame_flags
);
3313 vp9_twopass_postencode_update(cpi
, *size
);
3316 static void check_initial_width(VP9_COMP
*cpi
, int subsampling_x
,
3317 int subsampling_y
) {
3318 VP9_COMMON
*const cm
= &cpi
->common
;
3319 if (!cpi
->initial_width
) {
3320 cm
->subsampling_x
= subsampling_x
;
3321 cm
->subsampling_y
= subsampling_y
;
3322 alloc_raw_frame_buffers(cpi
);
3323 cpi
->initial_width
= cm
->width
;
3324 cpi
->initial_height
= cm
->height
;
3329 int vp9_receive_raw_frame(VP9_PTR ptr
, unsigned int frame_flags
,
3330 YV12_BUFFER_CONFIG
*sd
, int64_t time_stamp
,
3332 VP9_COMP
*cpi
= (VP9_COMP
*) ptr
;
3333 VP9_COMMON
*cm
= &cpi
->common
;
3334 struct vpx_usec_timer timer
;
3336 const int subsampling_x
= sd
->uv_width
< sd
->y_width
;
3337 const int subsampling_y
= sd
->uv_height
< sd
->y_height
;
3339 check_initial_width(cpi
, subsampling_x
, subsampling_y
);
3340 vpx_usec_timer_start(&timer
);
3341 if (vp9_lookahead_push(cpi
->lookahead
, sd
, time_stamp
, end_time
, frame_flags
,
3342 cpi
->active_map_enabled
? cpi
->active_map
: NULL
))
3344 vpx_usec_timer_mark(&timer
);
3345 cpi
->time_receive_data
+= vpx_usec_timer_elapsed(&timer
);
3347 if (cm
->version
== 0 && (subsampling_x
!= 1 || subsampling_y
!= 1)) {
3348 vpx_internal_error(&cm
->error
, VPX_CODEC_INVALID_PARAM
,
3349 "Non-4:2:0 color space requires profile >= 1");
3357 static int frame_is_reference(const VP9_COMP
*cpi
) {
3358 const VP9_COMMON
*cm
= &cpi
->common
;
3360 return cm
->frame_type
== KEY_FRAME
||
3361 cpi
->refresh_last_frame
||
3362 cpi
->refresh_golden_frame
||
3363 cpi
->refresh_alt_ref_frame
||
3364 cm
->refresh_frame_context
||
3365 cm
->lf
.mode_ref_delta_update
||
3366 cm
->seg
.update_map
||
3367 cm
->seg
.update_data
;
3370 #if CONFIG_MULTIPLE_ARF
3371 int is_next_frame_arf(VP9_COMP
*cpi
) {
3372 // Negative entry in frame_coding_order indicates an ARF at this position.
3373 return cpi
->frame_coding_order
[cpi
->sequence_number
+ 1] < 0 ? 1 : 0;
3377 void adjust_frame_rate(VP9_COMP
*cpi
) {
3378 int64_t this_duration
;
3381 if (cpi
->source
->ts_start
== cpi
->first_time_stamp_ever
) {
3382 this_duration
= cpi
->source
->ts_end
- cpi
->source
->ts_start
;
3385 int64_t last_duration
= cpi
->last_end_time_stamp_seen
3386 - cpi
->last_time_stamp_seen
;
3388 this_duration
= cpi
->source
->ts_end
- cpi
->last_end_time_stamp_seen
;
3390 // do a step update if the duration changes by 10%
3392 step
= (int)((this_duration
- last_duration
) * 10 / last_duration
);
3395 if (this_duration
) {
3397 vp9_new_framerate(cpi
, 10000000.0 / this_duration
);
3399 // Average this frame's rate into the last second's average
3400 // frame rate. If we haven't seen 1 second yet, then average
3401 // over the whole interval seen.
3402 const double interval
= MIN((double)(cpi
->source
->ts_end
3403 - cpi
->first_time_stamp_ever
), 10000000.0);
3404 double avg_duration
= 10000000.0 / cpi
->oxcf
.framerate
;
3405 avg_duration
*= (interval
- avg_duration
+ this_duration
);
3406 avg_duration
/= interval
;
3408 vp9_new_framerate(cpi
, 10000000.0 / avg_duration
);
3411 cpi
->last_time_stamp_seen
= cpi
->source
->ts_start
;
3412 cpi
->last_end_time_stamp_seen
= cpi
->source
->ts_end
;
3415 int vp9_get_compressed_data(VP9_PTR ptr
, unsigned int *frame_flags
,
3416 size_t *size
, uint8_t *dest
,
3417 int64_t *time_stamp
, int64_t *time_end
, int flush
) {
3418 VP9_COMP
*cpi
= (VP9_COMP
*) ptr
;
3419 VP9_COMMON
*cm
= &cpi
->common
;
3420 MACROBLOCKD
*xd
= &cpi
->mb
.e_mbd
;
3421 struct vpx_usec_timer cmptimer
;
3422 YV12_BUFFER_CONFIG
*force_src_buffer
= NULL
;
3423 MV_REFERENCE_FRAME ref_frame
;
3428 vpx_usec_timer_start(&cmptimer
);
3432 set_high_precision_mv(cpi
, ALTREF_HIGH_PRECISION_MV
);
3435 cm
->reset_frame_context
= 0;
3436 cm
->refresh_frame_context
= 1;
3437 cpi
->refresh_last_frame
= 1;
3438 cpi
->refresh_golden_frame
= 0;
3439 cpi
->refresh_alt_ref_frame
= 0;
3441 // Should we code an alternate reference frame.
3442 if (cpi
->oxcf
.play_alternate
&& cpi
->rc
.source_alt_ref_pending
) {
3445 #if CONFIG_MULTIPLE_ARF
3446 assert(!cpi
->multi_arf_enabled
||
3447 cpi
->frame_coding_order
[cpi
->sequence_number
] < 0);
3449 if (cpi
->multi_arf_enabled
&& (cpi
->pass
== 2))
3450 frames_to_arf
= (-cpi
->frame_coding_order
[cpi
->sequence_number
])
3451 - cpi
->next_frame_in_order
;
3454 frames_to_arf
= cpi
->rc
.frames_till_gf_update_due
;
3456 assert(frames_to_arf
<= cpi
->rc
.frames_to_key
);
3458 if ((cpi
->source
= vp9_lookahead_peek(cpi
->lookahead
, frames_to_arf
))) {
3459 #if CONFIG_MULTIPLE_ARF
3460 cpi
->alt_ref_source
[cpi
->arf_buffered
] = cpi
->source
;
3462 cpi
->alt_ref_source
= cpi
->source
;
3465 if (cpi
->oxcf
.arnr_max_frames
> 0) {
3466 // Produce the filtered ARF frame.
3467 // TODO(agrange) merge these two functions.
3468 vp9_configure_arnr_filter(cpi
, frames_to_arf
, cpi
->rc
.gfu_boost
);
3469 vp9_temporal_filter_prepare(cpi
, frames_to_arf
);
3470 vp9_extend_frame_borders(&cpi
->alt_ref_buffer
,
3471 cm
->subsampling_x
, cm
->subsampling_y
);
3472 force_src_buffer
= &cpi
->alt_ref_buffer
;
3476 cpi
->refresh_alt_ref_frame
= 1;
3477 cpi
->refresh_golden_frame
= 0;
3478 cpi
->refresh_last_frame
= 0;
3479 cpi
->rc
.is_src_frame_alt_ref
= 0;
3481 #if CONFIG_MULTIPLE_ARF
3482 if (!cpi
->multi_arf_enabled
)
3484 cpi
->rc
.source_alt_ref_pending
= 0;
3486 cpi
->rc
.source_alt_ref_pending
= 0;
3491 #if CONFIG_MULTIPLE_ARF
3494 if ((cpi
->source
= vp9_lookahead_pop(cpi
->lookahead
, flush
))) {
3498 #if CONFIG_MULTIPLE_ARF
3499 // Is this frame the ARF overlay.
3500 cpi
->rc
.is_src_frame_alt_ref
= 0;
3501 for (i
= 0; i
< cpi
->arf_buffered
; ++i
) {
3502 if (cpi
->source
== cpi
->alt_ref_source
[i
]) {
3503 cpi
->rc
.is_src_frame_alt_ref
= 1;
3504 cpi
->refresh_golden_frame
= 1;
3509 cpi
->rc
.is_src_frame_alt_ref
= cpi
->alt_ref_source
3510 && (cpi
->source
== cpi
->alt_ref_source
);
3512 if (cpi
->rc
.is_src_frame_alt_ref
) {
3513 // Current frame is an ARF overlay frame.
3514 #if CONFIG_MULTIPLE_ARF
3515 cpi
->alt_ref_source
[i
] = NULL
;
3517 cpi
->alt_ref_source
= NULL
;
3519 // Don't refresh the last buffer for an ARF overlay frame. It will
3520 // become the GF so preserve last as an alternative prediction option.
3521 cpi
->refresh_last_frame
= 0;
3523 #if CONFIG_MULTIPLE_ARF
3524 ++cpi
->next_frame_in_order
;
3530 cpi
->un_scaled_source
= cpi
->Source
= force_src_buffer
? force_src_buffer
3531 : &cpi
->source
->img
;
3532 *time_stamp
= cpi
->source
->ts_start
;
3533 *time_end
= cpi
->source
->ts_end
;
3534 *frame_flags
= cpi
->source
->flags
;
3536 #if CONFIG_MULTIPLE_ARF
3537 if ((cm
->frame_type
!= KEY_FRAME
) && (cpi
->pass
== 2))
3538 cpi
->rc
.source_alt_ref_pending
= is_next_frame_arf(cpi
);
3542 if (flush
&& cpi
->pass
== 1 && !cpi
->twopass
.first_pass_done
) {
3543 vp9_end_first_pass(cpi
); /* get last stats packet */
3544 cpi
->twopass
.first_pass_done
= 1;
3549 if (cpi
->source
->ts_start
< cpi
->first_time_stamp_ever
) {
3550 cpi
->first_time_stamp_ever
= cpi
->source
->ts_start
;
3551 cpi
->last_end_time_stamp_seen
= cpi
->source
->ts_start
;
3554 // adjust frame rates based on timestamps given
3555 if (cm
->show_frame
) {
3556 adjust_frame_rate(cpi
);
3559 // start with a 0 size frame
3562 // Clear down mmx registers
3563 vp9_clear_system_state(); // __asm emms;
3565 /* find a free buffer for the new frame, releasing the reference previously
3568 cm
->fb_idx_ref_cnt
[cm
->new_fb_idx
]--;
3569 cm
->new_fb_idx
= get_free_fb(cm
);
3571 #if CONFIG_MULTIPLE_ARF
3572 /* Set up the correct ARF frame. */
3573 if (cpi
->refresh_alt_ref_frame
) {
3574 ++cpi
->arf_buffered
;
3576 if (cpi
->multi_arf_enabled
&& (cm
->frame_type
!= KEY_FRAME
) &&
3578 cpi
->alt_fb_idx
= cpi
->arf_buffer_idx
[cpi
->sequence_number
];
3582 cm
->frame_flags
= *frame_flags
;
3584 // Reset the frame pointers to the current frame size
3585 vp9_realloc_frame_buffer(get_frame_new_buffer(cm
),
3586 cm
->width
, cm
->height
,
3587 cm
->subsampling_x
, cm
->subsampling_y
,
3588 VP9_ENC_BORDER_IN_PIXELS
);
3590 for (ref_frame
= LAST_FRAME
; ref_frame
<= ALTREF_FRAME
; ++ref_frame
) {
3591 const int idx
= cm
->ref_frame_map
[get_ref_frame_idx(cpi
, ref_frame
)];
3592 YV12_BUFFER_CONFIG
*const buf
= &cm
->yv12_fb
[idx
];
3594 RefBuffer
*const ref_buf
= &cm
->frame_refs
[ref_frame
- 1];
3597 vp9_setup_scale_factors_for_frame(&ref_buf
->sf
,
3598 buf
->y_crop_width
, buf
->y_crop_height
,
3599 cm
->width
, cm
->height
);
3601 if (vp9_is_scaled(&ref_buf
->sf
))
3602 vp9_extend_frame_borders(buf
, cm
->subsampling_x
, cm
->subsampling_y
);
3605 set_ref_ptrs(cm
, xd
, LAST_FRAME
, LAST_FRAME
);
3606 xd
->interp_kernel
= vp9_get_interp_kernel(
3607 DEFAULT_INTERP_FILTER
== SWITCHABLE
? EIGHTTAP
: DEFAULT_INTERP_FILTER
);
3609 if (cpi
->oxcf
.aq_mode
== VARIANCE_AQ
)
3613 SvcEncode(cpi
, size
, dest
, frame_flags
);
3614 } else if (cpi
->pass
== 1) {
3615 Pass1Encode(cpi
, size
, dest
, frame_flags
);
3616 } else if (cpi
->pass
== 2) {
3617 Pass2Encode(cpi
, size
, dest
, frame_flags
);
3620 Pass0Encode(cpi
, size
, dest
, frame_flags
);
3623 if (cm
->refresh_frame_context
)
3624 cm
->frame_contexts
[cm
->frame_context_idx
] = cm
->fc
;
3626 // Frame was dropped, release scaled references.
3628 release_scaled_references(cpi
);
3632 cpi
->droppable
= !frame_is_reference(cpi
);
3635 vpx_usec_timer_mark(&cmptimer
);
3636 cpi
->time_compress_data
+= vpx_usec_timer_elapsed(&cmptimer
);
3638 if (cpi
->b_calculate_psnr
&& cpi
->pass
!= 1 && cm
->show_frame
)
3639 generate_psnr_packet(cpi
);
3641 #if CONFIG_INTERNAL_STATS
3643 if (cpi
->pass
!= 1) {
3644 cpi
->bytes
+= *size
;
3646 if (cm
->show_frame
) {
3649 if (cpi
->b_calculate_psnr
) {
3650 YV12_BUFFER_CONFIG
*orig
= cpi
->Source
;
3651 YV12_BUFFER_CONFIG
*recon
= cpi
->common
.frame_to_show
;
3652 YV12_BUFFER_CONFIG
*pp
= &cm
->post_proc_buffer
;
3654 calc_psnr(orig
, recon
, &psnr
);
3656 cpi
->total
+= psnr
.psnr
[0];
3657 cpi
->total_y
+= psnr
.psnr
[1];
3658 cpi
->total_u
+= psnr
.psnr
[2];
3659 cpi
->total_v
+= psnr
.psnr
[3];
3660 cpi
->total_sq_error
+= psnr
.sse
[0];
3661 cpi
->total_samples
+= psnr
.samples
[0];
3665 double frame_ssim2
= 0, weight
= 0;
3666 #if CONFIG_VP9_POSTPROC
3667 vp9_deblock(cm
->frame_to_show
, &cm
->post_proc_buffer
,
3668 cm
->lf
.filter_level
* 10 / 6);
3670 vp9_clear_system_state();
3672 calc_psnr(orig
, pp
, &psnr2
);
3674 cpi
->totalp
+= psnr2
.psnr
[0];
3675 cpi
->totalp_y
+= psnr2
.psnr
[1];
3676 cpi
->totalp_u
+= psnr2
.psnr
[2];
3677 cpi
->totalp_v
+= psnr2
.psnr
[3];
3678 cpi
->totalp_sq_error
+= psnr2
.sse
[0];
3679 cpi
->totalp_samples
+= psnr2
.samples
[0];
3681 frame_ssim2
= vp9_calc_ssim(orig
, recon
, 1, &weight
);
3683 cpi
->summed_quality
+= frame_ssim2
* weight
;
3684 cpi
->summed_weights
+= weight
;
3686 frame_ssim2
= vp9_calc_ssim(orig
, &cm
->post_proc_buffer
, 1, &weight
);
3688 cpi
->summedp_quality
+= frame_ssim2
* weight
;
3689 cpi
->summedp_weights
+= weight
;
3692 FILE *f
= fopen("q_used.stt", "a");
3693 fprintf(f
, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3694 cpi
->common
.current_video_frame
, y2
, u2
, v2
,
3695 frame_psnr2
, frame_ssim2
);
3702 if (cpi
->b_calculate_ssimg
) {
3703 double y
, u
, v
, frame_all
;
3704 frame_all
= vp9_calc_ssimg(cpi
->Source
, cm
->frame_to_show
, &y
, &u
, &v
);
3705 cpi
->total_ssimg_y
+= y
;
3706 cpi
->total_ssimg_u
+= u
;
3707 cpi
->total_ssimg_v
+= v
;
3708 cpi
->total_ssimg_all
+= frame_all
;
3717 int vp9_get_preview_raw_frame(VP9_PTR comp
, YV12_BUFFER_CONFIG
*dest
,
3718 vp9_ppflags_t
*flags
) {
3719 VP9_COMP
*cpi
= (VP9_COMP
*) comp
;
3721 if (!cpi
->common
.show_frame
) {
3725 #if CONFIG_VP9_POSTPROC
3726 ret
= vp9_post_proc_frame(&cpi
->common
, dest
, flags
);
3729 if (cpi
->common
.frame_to_show
) {
3730 *dest
= *cpi
->common
.frame_to_show
;
3731 dest
->y_width
= cpi
->common
.width
;
3732 dest
->y_height
= cpi
->common
.height
;
3733 dest
->uv_width
= cpi
->common
.width
>> cpi
->common
.subsampling_x
;
3734 dest
->uv_height
= cpi
->common
.height
>> cpi
->common
.subsampling_y
;
3740 #endif // !CONFIG_VP9_POSTPROC
3741 vp9_clear_system_state();
3746 int vp9_set_roimap(VP9_PTR comp
, unsigned char *map
, unsigned int rows
,
3747 unsigned int cols
, int delta_q
[MAX_SEGMENTS
],
3748 int delta_lf
[MAX_SEGMENTS
],
3749 unsigned int threshold
[MAX_SEGMENTS
]) {
3750 VP9_COMP
*cpi
= (VP9_COMP
*) comp
;
3751 signed char feature_data
[SEG_LVL_MAX
][MAX_SEGMENTS
];
3752 struct segmentation
*seg
= &cpi
->common
.seg
;
3755 if (cpi
->common
.mb_rows
!= rows
|| cpi
->common
.mb_cols
!= cols
)
3759 vp9_disable_segmentation((VP9_PTR
)cpi
);
3763 // Set the segmentation Map
3764 vp9_set_segmentation_map((VP9_PTR
)cpi
, map
);
3766 // Activate segmentation.
3767 vp9_enable_segmentation((VP9_PTR
)cpi
);
3769 // Set up the quant, LF and breakout threshold segment data
3770 for (i
= 0; i
< MAX_SEGMENTS
; i
++) {
3771 feature_data
[SEG_LVL_ALT_Q
][i
] = delta_q
[i
];
3772 feature_data
[SEG_LVL_ALT_LF
][i
] = delta_lf
[i
];
3773 cpi
->segment_encode_breakout
[i
] = threshold
[i
];
3776 // Enable the loop and quant changes in the feature mask
3777 for (i
= 0; i
< MAX_SEGMENTS
; i
++) {
3779 vp9_enable_segfeature(seg
, i
, SEG_LVL_ALT_Q
);
3781 vp9_disable_segfeature(seg
, i
, SEG_LVL_ALT_Q
);
3784 vp9_enable_segfeature(seg
, i
, SEG_LVL_ALT_LF
);
3786 vp9_disable_segfeature(seg
, i
, SEG_LVL_ALT_LF
);
3789 // Initialize the feature data structure
3790 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
3791 vp9_set_segment_data((VP9_PTR
)cpi
, &feature_data
[0][0], SEGMENT_DELTADATA
);
3796 int vp9_set_active_map(VP9_PTR comp
, unsigned char *map
,
3797 unsigned int rows
, unsigned int cols
) {
3798 VP9_COMP
*cpi
= (VP9_COMP
*) comp
;
3800 if (rows
== cpi
->common
.mb_rows
&& cols
== cpi
->common
.mb_cols
) {
3802 vpx_memcpy(cpi
->active_map
, map
, rows
* cols
);
3803 cpi
->active_map_enabled
= 1;
3805 cpi
->active_map_enabled
= 0;
3810 // cpi->active_map_enabled = 0;
3815 int vp9_set_internal_size(VP9_PTR comp
,
3816 VPX_SCALING horiz_mode
, VPX_SCALING vert_mode
) {
3817 VP9_COMP
*cpi
= (VP9_COMP
*) comp
;
3818 VP9_COMMON
*cm
= &cpi
->common
;
3819 int hr
= 0, hs
= 0, vr
= 0, vs
= 0;
3821 if (horiz_mode
> ONETWO
|| vert_mode
> ONETWO
)
3824 Scale2Ratio(horiz_mode
, &hr
, &hs
);
3825 Scale2Ratio(vert_mode
, &vr
, &vs
);
3827 // always go to the next whole number
3828 cm
->width
= (hs
- 1 + cpi
->oxcf
.width
* hr
) / hs
;
3829 cm
->height
= (vs
- 1 + cpi
->oxcf
.height
* vr
) / vs
;
3831 assert(cm
->width
<= cpi
->initial_width
);
3832 assert(cm
->height
<= cpi
->initial_height
);
3833 update_frame_size(cpi
);
3837 int vp9_set_size_literal(VP9_PTR comp
, unsigned int width
,
3838 unsigned int height
) {
3839 VP9_COMP
*cpi
= (VP9_COMP
*)comp
;
3840 VP9_COMMON
*cm
= &cpi
->common
;
3842 check_initial_width(cpi
, 1, 1);
3846 if (cm
->width
* 5 < cpi
->initial_width
) {
3847 cm
->width
= cpi
->initial_width
/ 5 + 1;
3848 printf("Warning: Desired width too small, changed to %d \n", cm
->width
);
3850 if (cm
->width
> cpi
->initial_width
) {
3851 cm
->width
= cpi
->initial_width
;
3852 printf("Warning: Desired width too large, changed to %d \n", cm
->width
);
3857 cm
->height
= height
;
3858 if (cm
->height
* 5 < cpi
->initial_height
) {
3859 cm
->height
= cpi
->initial_height
/ 5 + 1;
3860 printf("Warning: Desired height too small, changed to %d \n", cm
->height
);
3862 if (cm
->height
> cpi
->initial_height
) {
3863 cm
->height
= cpi
->initial_height
;
3864 printf("Warning: Desired height too large, changed to %d \n", cm
->height
);
3868 assert(cm
->width
<= cpi
->initial_width
);
3869 assert(cm
->height
<= cpi
->initial_height
);
3870 update_frame_size(cpi
);
3874 void vp9_set_svc(VP9_PTR comp
, int use_svc
) {
3875 VP9_COMP
*cpi
= (VP9_COMP
*)comp
;
3876 cpi
->use_svc
= use_svc
;
3880 int vp9_calc_ss_err(const YV12_BUFFER_CONFIG
*source
,
3881 const YV12_BUFFER_CONFIG
*reference
) {
3885 const uint8_t *src
= source
->y_buffer
;
3886 const uint8_t *ref
= reference
->y_buffer
;
3888 // Loop through the Y plane raw and reconstruction data summing
3889 // (square differences)
3890 for (i
= 0; i
< source
->y_height
; i
+= 16) {
3891 for (j
= 0; j
< source
->y_width
; j
+= 16) {
3893 total
+= vp9_mse16x16(src
+ j
, source
->y_stride
,
3894 ref
+ j
, reference
->y_stride
, &sse
);
3897 src
+= 16 * source
->y_stride
;
3898 ref
+= 16 * reference
->y_stride
;
3905 int vp9_get_quantizer(VP9_PTR c
) {
3906 return ((VP9_COMP
*)c
)->common
.base_qindex
;