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
= 1;
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
= 1;
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
= 1;
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
= 1;
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 for (i
= 0; i
< TX_SIZES
; i
++) {
843 sf
->intra_y_mode_mask
[i
] = INTRA_DC_H_V
;
844 sf
->intra_uv_mode_mask
[i
] = INTRA_DC_ONLY
;
846 sf
->use_fast_lpf_pick
= 2;
850 void vp9_set_speed_features(VP9_COMP
*cpi
) {
851 SPEED_FEATURES
*sf
= &cpi
->sf
;
852 VP9_COMMON
*cm
= &cpi
->common
;
853 int mode
= cpi
->compressor_speed
;
854 int speed
= cpi
->speed
;
857 // Convert negative speed to positive
861 for (i
= 0; i
< MAX_MODES
; ++i
)
862 cpi
->mode_chosen_counts
[i
] = 0;
864 // best quality defaults
866 sf
->search_method
= NSTEP
;
868 sf
->subpel_search_method
= SUBPEL_TREE
;
869 sf
->subpel_iters_per_step
= 2;
870 sf
->optimize_coefficients
= !cpi
->oxcf
.lossless
;
871 sf
->reduce_first_step_size
= 0;
872 sf
->auto_mv_step_size
= 0;
873 sf
->max_step_search_steps
= MAX_MVSEARCH_STEPS
;
874 sf
->comp_inter_joint_search_thresh
= BLOCK_4X4
;
875 sf
->adaptive_rd_thresh
= 0;
876 sf
->use_lastframe_partitioning
= LAST_FRAME_PARTITION_OFF
;
877 sf
->tx_size_search_method
= USE_FULL_RD
;
878 sf
->use_lp32x32fdct
= 0;
879 sf
->adaptive_motion_search
= 0;
880 sf
->adaptive_pred_interp_filter
= 0;
881 sf
->reference_masking
= 0;
882 sf
->use_one_partition_size_always
= 0;
883 sf
->less_rectangular_check
= 0;
884 sf
->use_square_partition_only
= 0;
885 sf
->auto_min_max_partition_size
= 0;
886 sf
->max_partition_size
= BLOCK_64X64
;
887 sf
->min_partition_size
= BLOCK_4X4
;
888 sf
->adjust_partitioning_from_last_frame
= 0;
889 sf
->last_partitioning_redo_frequency
= 4;
890 sf
->disable_split_mask
= 0;
891 sf
->mode_search_skip_flags
= 0;
892 sf
->disable_split_var_thresh
= 0;
893 sf
->disable_filter_search_var_thresh
= 0;
894 for (i
= 0; i
< TX_SIZES
; i
++) {
895 sf
->intra_y_mode_mask
[i
] = ALL_INTRA_MODES
;
896 sf
->intra_uv_mode_mask
[i
] = ALL_INTRA_MODES
;
898 sf
->use_rd_breakout
= 0;
899 sf
->skip_encode_sb
= 0;
900 sf
->use_uv_intra_rd_estimate
= 0;
901 sf
->use_fast_lpf_pick
= 0;
902 sf
->use_fast_coef_updates
= 0;
903 sf
->using_small_partition_info
= 0;
904 sf
->mode_skip_start
= MAX_MODES
; // Mode index at which mode skip mask set
907 case 0: // This is the best quality mode.
908 cpi
->diamond_search_sad
= vp9_full_range_search
;
911 set_good_speed_feature(cm
, sf
, speed
);
915 set_rt_speed_feature(cm
, sf
, speed
);
919 // Set rd thresholds based on mode and speed setting
920 set_rd_speed_thresholds(cpi
, mode
);
921 set_rd_speed_thresholds_sub8x8(cpi
, mode
);
923 // Slow quant, dct and trellis not worthwhile for first pass
924 // so make sure they are always turned off.
925 if (cpi
->pass
== 1) {
926 sf
->optimize_coefficients
= 0;
929 // No recode for 1 pass.
930 if (cpi
->pass
== 0) {
932 sf
->optimize_coefficients
= 0;
935 cpi
->mb
.fwd_txm4x4
= vp9_fdct4x4
;
936 if (cpi
->oxcf
.lossless
|| cpi
->mb
.e_mbd
.lossless
) {
937 cpi
->mb
.fwd_txm4x4
= vp9_fwht4x4
;
940 if (cpi
->sf
.subpel_search_method
== SUBPEL_TREE
) {
941 cpi
->find_fractional_mv_step
= vp9_find_best_sub_pixel_tree
;
942 cpi
->find_fractional_mv_step_comp
= vp9_find_best_sub_pixel_comp_tree
;
945 cpi
->mb
.optimize
= cpi
->sf
.optimize_coefficients
== 1 && cpi
->pass
!= 1;
948 frames_at_speed
[cpi
->speed
]++;
952 static void alloc_raw_frame_buffers(VP9_COMP
*cpi
) {
953 VP9_COMMON
*cm
= &cpi
->common
;
955 cpi
->lookahead
= vp9_lookahead_init(cpi
->oxcf
.width
, cpi
->oxcf
.height
,
956 cm
->subsampling_x
, cm
->subsampling_y
,
957 cpi
->oxcf
.lag_in_frames
);
959 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
960 "Failed to allocate lag buffers");
962 if (vp9_realloc_frame_buffer(&cpi
->alt_ref_buffer
,
963 cpi
->oxcf
.width
, cpi
->oxcf
.height
,
964 cm
->subsampling_x
, cm
->subsampling_y
,
965 VP9_ENC_BORDER_IN_PIXELS
))
966 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
967 "Failed to allocate altref buffer");
970 void vp9_alloc_compressor_data(VP9_COMP
*cpi
) {
971 VP9_COMMON
*cm
= &cpi
->common
;
973 if (vp9_alloc_frame_buffers(cm
, cm
->width
, cm
->height
))
974 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
975 "Failed to allocate frame buffers");
977 if (vp9_alloc_frame_buffer(&cpi
->last_frame_uf
,
978 cm
->width
, cm
->height
,
979 cm
->subsampling_x
, cm
->subsampling_y
,
980 VP9_ENC_BORDER_IN_PIXELS
))
981 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
982 "Failed to allocate last frame buffer");
984 if (vp9_alloc_frame_buffer(&cpi
->scaled_source
,
985 cm
->width
, cm
->height
,
986 cm
->subsampling_x
, cm
->subsampling_y
,
987 VP9_ENC_BORDER_IN_PIXELS
))
988 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
989 "Failed to allocate scaled source buffer");
994 unsigned int tokens
= get_token_alloc(cm
->mb_rows
, cm
->mb_cols
);
996 CHECK_MEM_ERROR(cm
, cpi
->tok
, vpx_calloc(tokens
, sizeof(*cpi
->tok
)));
999 vpx_free(cpi
->mb_activity_map
);
1000 CHECK_MEM_ERROR(cm
, cpi
->mb_activity_map
,
1001 vpx_calloc(sizeof(unsigned int),
1002 cm
->mb_rows
* cm
->mb_cols
));
1004 vpx_free(cpi
->mb_norm_activity_map
);
1005 CHECK_MEM_ERROR(cm
, cpi
->mb_norm_activity_map
,
1006 vpx_calloc(sizeof(unsigned int),
1007 cm
->mb_rows
* cm
->mb_cols
));
1009 // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm
1010 // block where mi unit size is 8x8.
1011 vpx_free(cpi
->above_context
[0]);
1012 CHECK_MEM_ERROR(cm
, cpi
->above_context
[0],
1013 vpx_calloc(2 * mi_cols_aligned_to_sb(cm
->mi_cols
) *
1015 sizeof(*cpi
->above_context
[0])));
1017 vpx_free(cpi
->above_seg_context
);
1018 CHECK_MEM_ERROR(cm
, cpi
->above_seg_context
,
1019 vpx_calloc(mi_cols_aligned_to_sb(cm
->mi_cols
),
1020 sizeof(*cpi
->above_seg_context
)));
1024 static void update_frame_size(VP9_COMP
*cpi
) {
1025 VP9_COMMON
*cm
= &cpi
->common
;
1027 vp9_update_frame_size(cm
);
1029 // Update size of buffers local to this frame
1030 if (vp9_realloc_frame_buffer(&cpi
->last_frame_uf
,
1031 cm
->width
, cm
->height
,
1032 cm
->subsampling_x
, cm
->subsampling_y
,
1033 VP9_ENC_BORDER_IN_PIXELS
))
1034 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
1035 "Failed to reallocate last frame buffer");
1037 if (vp9_realloc_frame_buffer(&cpi
->scaled_source
,
1038 cm
->width
, cm
->height
,
1039 cm
->subsampling_x
, cm
->subsampling_y
,
1040 VP9_ENC_BORDER_IN_PIXELS
))
1041 vpx_internal_error(&cm
->error
, VPX_CODEC_MEM_ERROR
,
1042 "Failed to reallocate scaled source buffer");
1045 int y_stride
= cpi
->scaled_source
.y_stride
;
1047 if (cpi
->sf
.search_method
== NSTEP
) {
1048 vp9_init3smotion_compensation(&cpi
->mb
, y_stride
);
1049 } else if (cpi
->sf
.search_method
== DIAMOND
) {
1050 vp9_init_dsmotion_compensation(&cpi
->mb
, y_stride
);
1056 for (i
= 1; i
< MAX_MB_PLANE
; ++i
) {
1057 cpi
->above_context
[i
] = cpi
->above_context
[0] +
1058 i
* sizeof(*cpi
->above_context
[0]) * 2 *
1059 mi_cols_aligned_to_sb(cm
->mi_cols
);
1065 // Table that converts 0-63 Q range values passed in outside to the Qindex
1066 // range used internally.
1067 static const int q_trans
[] = {
1068 0, 4, 8, 12, 16, 20, 24, 28,
1069 32, 36, 40, 44, 48, 52, 56, 60,
1070 64, 68, 72, 76, 80, 84, 88, 92,
1071 96, 100, 104, 108, 112, 116, 120, 124,
1072 128, 132, 136, 140, 144, 148, 152, 156,
1073 160, 164, 168, 172, 176, 180, 184, 188,
1074 192, 196, 200, 204, 208, 212, 216, 220,
1075 224, 228, 232, 236, 240, 244, 249, 255,
1078 int vp9_reverse_trans(int x
) {
1081 for (i
= 0; i
< 64; i
++)
1082 if (q_trans
[i
] >= x
)
1088 void vp9_new_framerate(VP9_COMP
*cpi
, double framerate
) {
1089 VP9_COMMON
*const cm
= &cpi
->common
;
1090 int64_t vbr_max_bits
;
1092 if (framerate
< 0.1)
1095 cpi
->oxcf
.framerate
= framerate
;
1096 cpi
->output_framerate
= cpi
->oxcf
.framerate
;
1097 cpi
->rc
.per_frame_bandwidth
= (int)(cpi
->oxcf
.target_bandwidth
1098 / cpi
->output_framerate
);
1099 cpi
->rc
.av_per_frame_bandwidth
= (int)(cpi
->oxcf
.target_bandwidth
1100 / cpi
->output_framerate
);
1101 cpi
->rc
.min_frame_bandwidth
= (int)(cpi
->rc
.av_per_frame_bandwidth
*
1102 cpi
->oxcf
.two_pass_vbrmin_section
/ 100);
1105 cpi
->rc
.min_frame_bandwidth
= MAX(cpi
->rc
.min_frame_bandwidth
,
1106 FRAME_OVERHEAD_BITS
);
1108 // A maximum bitrate for a frame is defined.
1109 // The baseline for this aligns with HW implementations that
1110 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1111 // per 16x16 MB (averaged over a frame). However this limit is extended if
1112 // a very high rate is given on the command line or the the rate cannnot
1113 // be acheived because of a user specificed max q (e.g. when the user
1114 // specifies lossless encode.
1116 vbr_max_bits
= ((int64_t)cpi
->rc
.av_per_frame_bandwidth
*
1117 (int64_t)cpi
->oxcf
.two_pass_vbrmax_section
) / 100;
1118 cpi
->rc
.max_frame_bandwidth
=
1119 MAX(MAX((cm
->MBs
* MAX_MB_RATE
), MAXRATE_1080P
), vbr_max_bits
);
1121 // Set Maximum gf/arf interval
1122 cpi
->rc
.max_gf_interval
= 16;
1124 // Extended interval for genuinely static scenes
1125 cpi
->twopass
.static_scene_max_gf_interval
= cpi
->key_frame_frequency
>> 1;
1127 // Special conditions when alt ref frame enabled in lagged compress mode
1128 if (cpi
->oxcf
.play_alternate
&& cpi
->oxcf
.lag_in_frames
) {
1129 if (cpi
->rc
.max_gf_interval
> cpi
->oxcf
.lag_in_frames
- 1)
1130 cpi
->rc
.max_gf_interval
= cpi
->oxcf
.lag_in_frames
- 1;
1132 if (cpi
->twopass
.static_scene_max_gf_interval
> cpi
->oxcf
.lag_in_frames
- 1)
1133 cpi
->twopass
.static_scene_max_gf_interval
= cpi
->oxcf
.lag_in_frames
- 1;
1136 if (cpi
->rc
.max_gf_interval
> cpi
->twopass
.static_scene_max_gf_interval
)
1137 cpi
->rc
.max_gf_interval
= cpi
->twopass
.static_scene_max_gf_interval
;
1140 static int64_t rescale(int val
, int64_t num
, int denom
) {
1141 int64_t llnum
= num
;
1142 int64_t llden
= denom
;
1143 int64_t llval
= val
;
1145 return (llval
* llnum
/ llden
);
1148 static void set_tile_limits(VP9_COMP
*cpi
) {
1149 VP9_COMMON
*const cm
= &cpi
->common
;
1151 int min_log2_tile_cols
, max_log2_tile_cols
;
1152 vp9_get_tile_n_bits(cm
->mi_cols
, &min_log2_tile_cols
, &max_log2_tile_cols
);
1154 cm
->log2_tile_cols
= clamp(cpi
->oxcf
.tile_columns
,
1155 min_log2_tile_cols
, max_log2_tile_cols
);
1156 cm
->log2_tile_rows
= cpi
->oxcf
.tile_rows
;
1159 static void init_config(VP9_PTR ptr
, VP9_CONFIG
*oxcf
) {
1160 VP9_COMP
*cpi
= (VP9_COMP
*)(ptr
);
1161 VP9_COMMON
*const cm
= &cpi
->common
;
1166 cm
->version
= oxcf
->version
;
1168 cm
->width
= oxcf
->width
;
1169 cm
->height
= oxcf
->height
;
1170 cm
->subsampling_x
= 0;
1171 cm
->subsampling_y
= 0;
1172 vp9_alloc_compressor_data(cpi
);
1174 // change includes all joint functionality
1175 vp9_change_config(ptr
, oxcf
);
1177 // Initialize active best and worst q and average q values.
1178 cpi
->rc
.active_worst_quality
= cpi
->oxcf
.worst_allowed_q
;
1180 if (cpi
->pass
== 0 && cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
) {
1181 cpi
->rc
.avg_frame_qindex
[0] = cpi
->oxcf
.worst_allowed_q
;
1182 cpi
->rc
.avg_frame_qindex
[1] = cpi
->oxcf
.worst_allowed_q
;
1183 cpi
->rc
.avg_frame_qindex
[2] = cpi
->oxcf
.worst_allowed_q
;
1185 cpi
->rc
.avg_frame_qindex
[0] = (cpi
->oxcf
.worst_allowed_q
+
1186 cpi
->oxcf
.best_allowed_q
) / 2;
1187 cpi
->rc
.avg_frame_qindex
[1] = (cpi
->oxcf
.worst_allowed_q
+
1188 cpi
->oxcf
.best_allowed_q
) / 2;
1189 cpi
->rc
.avg_frame_qindex
[2] = (cpi
->oxcf
.worst_allowed_q
+
1190 cpi
->oxcf
.best_allowed_q
) / 2;
1192 cpi
->rc
.last_q
[0] = cpi
->oxcf
.best_allowed_q
;
1193 cpi
->rc
.last_q
[1] = cpi
->oxcf
.best_allowed_q
;
1194 cpi
->rc
.last_q
[2] = cpi
->oxcf
.best_allowed_q
;
1196 // Initialise the starting buffer levels
1197 cpi
->rc
.buffer_level
= cpi
->oxcf
.starting_buffer_level
;
1198 cpi
->rc
.bits_off_target
= cpi
->oxcf
.starting_buffer_level
;
1200 cpi
->rc
.rolling_target_bits
= cpi
->rc
.av_per_frame_bandwidth
;
1201 cpi
->rc
.rolling_actual_bits
= cpi
->rc
.av_per_frame_bandwidth
;
1202 cpi
->rc
.long_rolling_target_bits
= cpi
->rc
.av_per_frame_bandwidth
;
1203 cpi
->rc
.long_rolling_actual_bits
= cpi
->rc
.av_per_frame_bandwidth
;
1205 cpi
->rc
.total_actual_bits
= 0;
1206 cpi
->rc
.total_target_vs_actual
= 0;
1208 cpi
->static_mb_pct
= 0;
1210 cpi
->lst_fb_idx
= 0;
1211 cpi
->gld_fb_idx
= 1;
1212 cpi
->alt_fb_idx
= 2;
1214 cpi
->current_layer
= 0;
1217 set_tile_limits(cpi
);
1219 cpi
->fixed_divide
[0] = 0;
1220 for (i
= 1; i
< 512; i
++)
1221 cpi
->fixed_divide
[i
] = 0x80000 / i
;
1225 void vp9_change_config(VP9_PTR ptr
, VP9_CONFIG
*oxcf
) {
1226 VP9_COMP
*cpi
= (VP9_COMP
*)(ptr
);
1227 VP9_COMMON
*const cm
= &cpi
->common
;
1232 if (cm
->version
!= oxcf
->version
) {
1233 cm
->version
= oxcf
->version
;
1238 switch (cpi
->oxcf
.mode
) {
1239 // Real time and one pass deprecated in test code base
1240 case MODE_GOODQUALITY
:
1242 cpi
->compressor_speed
= 2;
1243 cpi
->oxcf
.cpu_used
= clamp(cpi
->oxcf
.cpu_used
, -5, 5);
1246 case MODE_FIRSTPASS
:
1248 cpi
->compressor_speed
= 1;
1251 case MODE_SECONDPASS
:
1253 cpi
->compressor_speed
= 1;
1254 cpi
->oxcf
.cpu_used
= clamp(cpi
->oxcf
.cpu_used
, -5, 5);
1257 case MODE_SECONDPASS_BEST
:
1259 cpi
->compressor_speed
= 0;
1263 cpi
->oxcf
.worst_allowed_q
= q_trans
[oxcf
->worst_allowed_q
];
1264 cpi
->oxcf
.best_allowed_q
= q_trans
[oxcf
->best_allowed_q
];
1265 cpi
->oxcf
.cq_level
= q_trans
[cpi
->oxcf
.cq_level
];
1267 cpi
->oxcf
.lossless
= oxcf
->lossless
;
1268 cpi
->mb
.e_mbd
.itxm_add
= cpi
->oxcf
.lossless
? vp9_iwht4x4_add
1270 cpi
->rc
.baseline_gf_interval
= DEFAULT_GF_INTERVAL
;
1272 cpi
->ref_frame_flags
= VP9_ALT_FLAG
| VP9_GOLD_FLAG
| VP9_LAST_FLAG
;
1274 cpi
->refresh_golden_frame
= 0;
1275 cpi
->refresh_last_frame
= 1;
1276 cm
->refresh_frame_context
= 1;
1277 cm
->reset_frame_context
= 0;
1279 vp9_reset_segment_features(&cm
->seg
);
1280 set_high_precision_mv(cpi
, 0);
1285 for (i
= 0; i
< MAX_SEGMENTS
; i
++)
1286 cpi
->segment_encode_breakout
[i
] = cpi
->oxcf
.encode_breakout
;
1289 // local file playback mode == really big buffer
1290 if (cpi
->oxcf
.end_usage
== USAGE_LOCAL_FILE_PLAYBACK
) {
1291 cpi
->oxcf
.starting_buffer_level
= 60000;
1292 cpi
->oxcf
.optimal_buffer_level
= 60000;
1293 cpi
->oxcf
.maximum_buffer_size
= 240000;
1296 // Convert target bandwidth from Kbit/s to Bit/s
1297 cpi
->oxcf
.target_bandwidth
*= 1000;
1299 cpi
->oxcf
.starting_buffer_level
= rescale(cpi
->oxcf
.starting_buffer_level
,
1300 cpi
->oxcf
.target_bandwidth
, 1000);
1302 // Set or reset optimal and maximum buffer levels.
1303 if (cpi
->oxcf
.optimal_buffer_level
== 0)
1304 cpi
->oxcf
.optimal_buffer_level
= cpi
->oxcf
.target_bandwidth
/ 8;
1306 cpi
->oxcf
.optimal_buffer_level
= rescale(cpi
->oxcf
.optimal_buffer_level
,
1307 cpi
->oxcf
.target_bandwidth
, 1000);
1309 if (cpi
->oxcf
.maximum_buffer_size
== 0)
1310 cpi
->oxcf
.maximum_buffer_size
= cpi
->oxcf
.target_bandwidth
/ 8;
1312 cpi
->oxcf
.maximum_buffer_size
= rescale(cpi
->oxcf
.maximum_buffer_size
,
1313 cpi
->oxcf
.target_bandwidth
, 1000);
1314 // Under a configuration change, where maximum_buffer_size may change,
1315 // keep buffer level clipped to the maximum allowed buffer size.
1316 if (cpi
->rc
.bits_off_target
> cpi
->oxcf
.maximum_buffer_size
) {
1317 cpi
->rc
.bits_off_target
= cpi
->oxcf
.maximum_buffer_size
;
1318 cpi
->rc
.buffer_level
= cpi
->rc
.bits_off_target
;
1321 // Set up frame rate and related parameters rate control values.
1322 vp9_new_framerate(cpi
, cpi
->oxcf
.framerate
);
1324 // Set absolute upper and lower quality limits
1325 cpi
->rc
.worst_quality
= cpi
->oxcf
.worst_allowed_q
;
1326 cpi
->rc
.best_quality
= cpi
->oxcf
.best_allowed_q
;
1328 // active values should only be modified if out of new range
1329 cpi
->rc
.active_worst_quality
= clamp(cpi
->rc
.active_worst_quality
,
1330 cpi
->rc
.best_quality
,
1331 cpi
->rc
.worst_quality
);
1333 cpi
->cq_target_quality
= cpi
->oxcf
.cq_level
;
1335 cm
->interp_filter
= DEFAULT_INTERP_FILTER
;
1337 cpi
->target_bandwidth
= cpi
->oxcf
.target_bandwidth
;
1339 cm
->display_width
= cpi
->oxcf
.width
;
1340 cm
->display_height
= cpi
->oxcf
.height
;
1342 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1343 cpi
->oxcf
.sharpness
= MIN(7, cpi
->oxcf
.sharpness
);
1345 cpi
->common
.lf
.sharpness_level
= cpi
->oxcf
.sharpness
;
1347 if (cpi
->initial_width
) {
1348 // Increasing the size of the frame beyond the first seen frame, or some
1349 // otherwise signalled maximum size, is not supported.
1350 // TODO(jkoleszar): exit gracefully.
1351 assert(cm
->width
<= cpi
->initial_width
);
1352 assert(cm
->height
<= cpi
->initial_height
);
1354 update_frame_size(cpi
);
1356 cpi
->speed
= cpi
->oxcf
.cpu_used
;
1358 if (cpi
->oxcf
.lag_in_frames
== 0) {
1359 // force to allowlag to 0 if lag_in_frames is 0;
1360 cpi
->oxcf
.allow_lag
= 0;
1361 } else if (cpi
->oxcf
.lag_in_frames
> MAX_LAG_BUFFERS
) {
1362 // Limit on lag buffers as these are not currently dynamically allocated
1363 cpi
->oxcf
.lag_in_frames
= MAX_LAG_BUFFERS
;
1367 #if CONFIG_MULTIPLE_ARF
1368 vp9_zero(cpi
->alt_ref_source
);
1370 cpi
->alt_ref_source
= NULL
;
1372 cpi
->rc
.is_src_frame_alt_ref
= 0;
1375 // Experimental RD Code
1376 cpi
->frame_distortion
= 0;
1377 cpi
->last_frame_distortion
= 0;
1380 set_tile_limits(cpi
);
1382 cpi
->ext_refresh_frame_flags_pending
= 0;
1383 cpi
->ext_refresh_frame_context_pending
= 0;
1386 #define M_LOG2_E 0.693147180559945309417
1387 #define log2f(x) (log (x) / (float) M_LOG2_E)
1389 static void cal_nmvjointsadcost(int *mvjointsadcost
) {
1390 mvjointsadcost
[0] = 600;
1391 mvjointsadcost
[1] = 300;
1392 mvjointsadcost
[2] = 300;
1393 mvjointsadcost
[0] = 300;
1396 static void cal_nmvsadcosts(int *mvsadcost
[2]) {
1399 mvsadcost
[0][0] = 0;
1400 mvsadcost
[1][0] = 0;
1403 double z
= 256 * (2 * (log2f(8 * i
) + .6));
1404 mvsadcost
[0][i
] = (int)z
;
1405 mvsadcost
[1][i
] = (int)z
;
1406 mvsadcost
[0][-i
] = (int)z
;
1407 mvsadcost
[1][-i
] = (int)z
;
1408 } while (++i
<= MV_MAX
);
1411 static void cal_nmvsadcosts_hp(int *mvsadcost
[2]) {
1414 mvsadcost
[0][0] = 0;
1415 mvsadcost
[1][0] = 0;
1418 double z
= 256 * (2 * (log2f(8 * i
) + .6));
1419 mvsadcost
[0][i
] = (int)z
;
1420 mvsadcost
[1][i
] = (int)z
;
1421 mvsadcost
[0][-i
] = (int)z
;
1422 mvsadcost
[1][-i
] = (int)z
;
1423 } while (++i
<= MV_MAX
);
1426 static void alloc_mode_context(VP9_COMMON
*cm
, int num_4x4_blk
,
1427 PICK_MODE_CONTEXT
*ctx
) {
1428 int num_pix
= num_4x4_blk
<< 4;
1430 ctx
->num_4x4_blk
= num_4x4_blk
;
1431 CHECK_MEM_ERROR(cm
, ctx
->zcoeff_blk
,
1432 vpx_calloc(num_4x4_blk
, sizeof(uint8_t)));
1433 for (i
= 0; i
< MAX_MB_PLANE
; ++i
) {
1434 for (k
= 0; k
< 3; ++k
) {
1435 CHECK_MEM_ERROR(cm
, ctx
->coeff
[i
][k
],
1436 vpx_memalign(16, num_pix
* sizeof(int16_t)));
1437 CHECK_MEM_ERROR(cm
, ctx
->qcoeff
[i
][k
],
1438 vpx_memalign(16, num_pix
* sizeof(int16_t)));
1439 CHECK_MEM_ERROR(cm
, ctx
->dqcoeff
[i
][k
],
1440 vpx_memalign(16, num_pix
* sizeof(int16_t)));
1441 CHECK_MEM_ERROR(cm
, ctx
->eobs
[i
][k
],
1442 vpx_memalign(16, num_pix
* sizeof(uint16_t)));
1443 ctx
->coeff_pbuf
[i
][k
] = ctx
->coeff
[i
][k
];
1444 ctx
->qcoeff_pbuf
[i
][k
] = ctx
->qcoeff
[i
][k
];
1445 ctx
->dqcoeff_pbuf
[i
][k
] = ctx
->dqcoeff
[i
][k
];
1446 ctx
->eobs_pbuf
[i
][k
] = ctx
->eobs
[i
][k
];
1451 static void free_mode_context(PICK_MODE_CONTEXT
*ctx
) {
1453 vpx_free(ctx
->zcoeff_blk
);
1454 ctx
->zcoeff_blk
= 0;
1455 for (i
= 0; i
< MAX_MB_PLANE
; ++i
) {
1456 for (k
= 0; k
< 3; ++k
) {
1457 vpx_free(ctx
->coeff
[i
][k
]);
1458 ctx
->coeff
[i
][k
] = 0;
1459 vpx_free(ctx
->qcoeff
[i
][k
]);
1460 ctx
->qcoeff
[i
][k
] = 0;
1461 vpx_free(ctx
->dqcoeff
[i
][k
]);
1462 ctx
->dqcoeff
[i
][k
] = 0;
1463 vpx_free(ctx
->eobs
[i
][k
]);
1464 ctx
->eobs
[i
][k
] = 0;
1469 static void init_pick_mode_context(VP9_COMP
*cpi
) {
1471 VP9_COMMON
*const cm
= &cpi
->common
;
1472 MACROBLOCK
*const x
= &cpi
->mb
;
1475 for (i
= 0; i
< BLOCK_SIZES
; ++i
) {
1476 const int num_4x4_w
= num_4x4_blocks_wide_lookup
[i
];
1477 const int num_4x4_h
= num_4x4_blocks_high_lookup
[i
];
1478 const int num_4x4_blk
= MAX(4, num_4x4_w
* num_4x4_h
);
1479 if (i
< BLOCK_16X16
) {
1480 for (x
->sb_index
= 0; x
->sb_index
< 4; ++x
->sb_index
) {
1481 for (x
->mb_index
= 0; x
->mb_index
< 4; ++x
->mb_index
) {
1482 for (x
->b_index
= 0; x
->b_index
< 16 / num_4x4_blk
; ++x
->b_index
) {
1483 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1484 alloc_mode_context(cm
, num_4x4_blk
, ctx
);
1488 } else if (i
< BLOCK_32X32
) {
1489 for (x
->sb_index
= 0; x
->sb_index
< 4; ++x
->sb_index
) {
1490 for (x
->mb_index
= 0; x
->mb_index
< 64 / num_4x4_blk
; ++x
->mb_index
) {
1491 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1492 ctx
->num_4x4_blk
= num_4x4_blk
;
1493 alloc_mode_context(cm
, num_4x4_blk
, ctx
);
1496 } else if (i
< BLOCK_64X64
) {
1497 for (x
->sb_index
= 0; x
->sb_index
< 256 / num_4x4_blk
; ++x
->sb_index
) {
1498 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1499 ctx
->num_4x4_blk
= num_4x4_blk
;
1500 alloc_mode_context(cm
, num_4x4_blk
, ctx
);
1503 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1504 ctx
->num_4x4_blk
= num_4x4_blk
;
1505 alloc_mode_context(cm
, num_4x4_blk
, ctx
);
1510 static void free_pick_mode_context(MACROBLOCK
*x
) {
1513 for (i
= 0; i
< BLOCK_SIZES
; ++i
) {
1514 const int num_4x4_w
= num_4x4_blocks_wide_lookup
[i
];
1515 const int num_4x4_h
= num_4x4_blocks_high_lookup
[i
];
1516 const int num_4x4_blk
= MAX(4, num_4x4_w
* num_4x4_h
);
1517 if (i
< BLOCK_16X16
) {
1518 for (x
->sb_index
= 0; x
->sb_index
< 4; ++x
->sb_index
) {
1519 for (x
->mb_index
= 0; x
->mb_index
< 4; ++x
->mb_index
) {
1520 for (x
->b_index
= 0; x
->b_index
< 16 / num_4x4_blk
; ++x
->b_index
) {
1521 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1522 free_mode_context(ctx
);
1526 } else if (i
< BLOCK_32X32
) {
1527 for (x
->sb_index
= 0; x
->sb_index
< 4; ++x
->sb_index
) {
1528 for (x
->mb_index
= 0; x
->mb_index
< 64 / num_4x4_blk
; ++x
->mb_index
) {
1529 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1530 free_mode_context(ctx
);
1533 } else if (i
< BLOCK_64X64
) {
1534 for (x
->sb_index
= 0; x
->sb_index
< 256 / num_4x4_blk
; ++x
->sb_index
) {
1535 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1536 free_mode_context(ctx
);
1539 PICK_MODE_CONTEXT
*ctx
= get_block_context(x
, i
);
1540 free_mode_context(ctx
);
1545 VP9_PTR
vp9_create_compressor(VP9_CONFIG
*oxcf
) {
1555 cpi
= ctx
.cpi
= vpx_memalign(32, sizeof(VP9_COMP
));
1556 // Check that the CPI instance is valid
1564 if (setjmp(cm
->error
.jmp
)) {
1565 VP9_PTR ptr
= ctx
.ptr
;
1567 ctx
.cpi
->common
.error
.setjmp
= 0;
1568 vp9_remove_compressor(&ptr
);
1572 cm
->error
.setjmp
= 1;
1574 CHECK_MEM_ERROR(cm
, cpi
->mb
.ss
, vpx_calloc(sizeof(search_site
),
1575 (MAX_MVSEARCH_STEPS
* 8) + 1));
1577 vp9_create_common(cm
);
1579 init_config((VP9_PTR
)cpi
, oxcf
);
1581 init_pick_mode_context(cpi
);
1583 cm
->current_video_frame
= 0;
1585 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1586 cm
->ref_frame_sign_bias
[ALTREF_FRAME
] = 1;
1588 cpi
->rc
.baseline_gf_interval
= DEFAULT_GF_INTERVAL
;
1590 cpi
->gold_is_last
= 0;
1591 cpi
->alt_is_last
= 0;
1592 cpi
->gold_is_alt
= 0;
1594 // Spatial scalability
1595 cpi
->number_spatial_layers
= oxcf
->ss_number_layers
;
1597 // Create the encoder segmentation map and set all entries to 0
1598 CHECK_MEM_ERROR(cm
, cpi
->segmentation_map
,
1599 vpx_calloc(cm
->mi_rows
* cm
->mi_cols
, 1));
1601 // Create a complexity map used for rd adjustment
1602 CHECK_MEM_ERROR(cm
, cpi
->complexity_map
,
1603 vpx_calloc(cm
->mi_rows
* cm
->mi_cols
, 1));
1606 // And a place holder structure is the coding context
1607 // for use if we want to save and restore it
1608 CHECK_MEM_ERROR(cm
, cpi
->coding_context
.last_frame_seg_map_copy
,
1609 vpx_calloc(cm
->mi_rows
* cm
->mi_cols
, 1));
1611 CHECK_MEM_ERROR(cm
, cpi
->active_map
, vpx_calloc(cm
->MBs
, 1));
1612 vpx_memset(cpi
->active_map
, 1, cm
->MBs
);
1613 cpi
->active_map_enabled
= 0;
1615 for (i
= 0; i
< (sizeof(cpi
->mbgraph_stats
) /
1616 sizeof(cpi
->mbgraph_stats
[0])); i
++) {
1617 CHECK_MEM_ERROR(cm
, cpi
->mbgraph_stats
[i
].mb_stats
,
1618 vpx_calloc(cm
->MBs
*
1619 sizeof(*cpi
->mbgraph_stats
[i
].mb_stats
), 1));
1622 #ifdef ENTROPY_STATS
1624 init_context_counters();
1627 /*Initialize the feed-forward activity masking.*/
1628 cpi
->activity_avg
= 90 << 12;
1629 cpi
->key_frame_frequency
= cpi
->oxcf
.key_freq
;
1631 cpi
->rc
.frames_since_key
= 8; // Sensible default for first frame.
1632 cpi
->rc
.this_key_frame_forced
= 0;
1633 cpi
->rc
.next_key_frame_forced
= 0;
1635 cpi
->rc
.source_alt_ref_pending
= 0;
1636 cpi
->rc
.source_alt_ref_active
= 0;
1637 cpi
->refresh_alt_ref_frame
= 0;
1639 #if CONFIG_MULTIPLE_ARF
1640 // Turn multiple ARF usage on/off. This is a quick hack for the initial test
1641 // version. It should eventually be set via the codec API.
1642 cpi
->multi_arf_enabled
= 1;
1644 if (cpi
->multi_arf_enabled
) {
1645 cpi
->sequence_number
= 0;
1646 cpi
->frame_coding_order_period
= 0;
1647 vp9_zero(cpi
->frame_coding_order
);
1648 vp9_zero(cpi
->arf_buffer_idx
);
1652 cpi
->b_calculate_psnr
= CONFIG_INTERNAL_STATS
;
1653 #if CONFIG_INTERNAL_STATS
1654 cpi
->b_calculate_ssimg
= 0;
1659 if (cpi
->b_calculate_psnr
) {
1664 cpi
->total_sq_error
= 0;
1665 cpi
->total_samples
= 0;
1667 cpi
->totalp_y
= 0.0;
1668 cpi
->totalp_u
= 0.0;
1669 cpi
->totalp_v
= 0.0;
1671 cpi
->totalp_sq_error
= 0;
1672 cpi
->totalp_samples
= 0;
1674 cpi
->tot_recode_hits
= 0;
1675 cpi
->summed_quality
= 0;
1676 cpi
->summed_weights
= 0;
1677 cpi
->summedp_quality
= 0;
1678 cpi
->summedp_weights
= 0;
1681 if (cpi
->b_calculate_ssimg
) {
1682 cpi
->total_ssimg_y
= 0;
1683 cpi
->total_ssimg_u
= 0;
1684 cpi
->total_ssimg_v
= 0;
1685 cpi
->total_ssimg_all
= 0;
1690 cpi
->first_time_stamp_ever
= INT64_MAX
;
1692 cpi
->rc
.frames_till_gf_update_due
= 0;
1694 cpi
->rc
.ni_av_qi
= cpi
->oxcf
.worst_allowed_q
;
1695 cpi
->rc
.ni_tot_qi
= 0;
1696 cpi
->rc
.ni_frames
= 0;
1697 cpi
->rc
.tot_q
= 0.0;
1698 cpi
->rc
.avg_q
= vp9_convert_qindex_to_q(cpi
->oxcf
.worst_allowed_q
);
1700 cpi
->rc
.rate_correction_factor
= 1.0;
1701 cpi
->rc
.key_frame_rate_correction_factor
= 1.0;
1702 cpi
->rc
.gf_rate_correction_factor
= 1.0;
1704 cal_nmvjointsadcost(cpi
->mb
.nmvjointsadcost
);
1705 cpi
->mb
.nmvcost
[0] = &cpi
->mb
.nmvcosts
[0][MV_MAX
];
1706 cpi
->mb
.nmvcost
[1] = &cpi
->mb
.nmvcosts
[1][MV_MAX
];
1707 cpi
->mb
.nmvsadcost
[0] = &cpi
->mb
.nmvsadcosts
[0][MV_MAX
];
1708 cpi
->mb
.nmvsadcost
[1] = &cpi
->mb
.nmvsadcosts
[1][MV_MAX
];
1709 cal_nmvsadcosts(cpi
->mb
.nmvsadcost
);
1711 cpi
->mb
.nmvcost_hp
[0] = &cpi
->mb
.nmvcosts_hp
[0][MV_MAX
];
1712 cpi
->mb
.nmvcost_hp
[1] = &cpi
->mb
.nmvcosts_hp
[1][MV_MAX
];
1713 cpi
->mb
.nmvsadcost_hp
[0] = &cpi
->mb
.nmvsadcosts_hp
[0][MV_MAX
];
1714 cpi
->mb
.nmvsadcost_hp
[1] = &cpi
->mb
.nmvsadcosts_hp
[1][MV_MAX
];
1715 cal_nmvsadcosts_hp(cpi
->mb
.nmvsadcost_hp
);
1717 #ifdef OUTPUT_YUV_SRC
1718 yuv_file
= fopen("bd.yuv", "ab");
1720 #ifdef OUTPUT_YUV_REC
1721 yuv_rec_file
= fopen("rec.yuv", "wb");
1725 framepsnr
= fopen("framepsnr.stt", "a");
1726 kf_list
= fopen("kf_list.stt", "w");
1729 cpi
->output_pkt_list
= oxcf
->output_pkt_list
;
1731 cpi
->enable_encode_breakout
= 1;
1733 if (cpi
->pass
== 1) {
1734 vp9_init_first_pass(cpi
);
1735 } else if (cpi
->pass
== 2) {
1736 size_t packet_sz
= sizeof(FIRSTPASS_STATS
);
1737 int packets
= (int)(oxcf
->two_pass_stats_in
.sz
/ packet_sz
);
1739 cpi
->twopass
.stats_in_start
= oxcf
->two_pass_stats_in
.buf
;
1740 cpi
->twopass
.stats_in
= cpi
->twopass
.stats_in_start
;
1741 cpi
->twopass
.stats_in_end
= (void *)((char *)cpi
->twopass
.stats_in
1742 + (packets
- 1) * packet_sz
);
1743 vp9_init_second_pass(cpi
);
1746 vp9_set_speed_features(cpi
);
1748 // Default rd threshold factors for mode selection
1749 for (i
= 0; i
< BLOCK_SIZES
; ++i
) {
1750 for (j
= 0; j
< MAX_MODES
; ++j
)
1751 cpi
->rd_thresh_freq_fact
[i
][j
] = 32;
1752 for (j
= 0; j
< MAX_REFS
; ++j
)
1753 cpi
->rd_thresh_freq_sub8x8
[i
][j
] = 32;
1756 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \
1757 SDX3F, SDX8F, SDX4DF)\
1758 cpi->fn_ptr[BT].sdf = SDF; \
1759 cpi->fn_ptr[BT].sdaf = SDAF; \
1760 cpi->fn_ptr[BT].vf = VF; \
1761 cpi->fn_ptr[BT].svf = SVF; \
1762 cpi->fn_ptr[BT].svaf = SVAF; \
1763 cpi->fn_ptr[BT].svf_halfpix_h = SVFHH; \
1764 cpi->fn_ptr[BT].svf_halfpix_v = SVFHV; \
1765 cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \
1766 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1767 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1768 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1770 BFP(BLOCK_32X16
, vp9_sad32x16
, vp9_sad32x16_avg
,
1771 vp9_variance32x16
, vp9_sub_pixel_variance32x16
,
1772 vp9_sub_pixel_avg_variance32x16
, NULL
, NULL
,
1776 BFP(BLOCK_16X32
, vp9_sad16x32
, vp9_sad16x32_avg
,
1777 vp9_variance16x32
, vp9_sub_pixel_variance16x32
,
1778 vp9_sub_pixel_avg_variance16x32
, NULL
, NULL
,
1782 BFP(BLOCK_64X32
, vp9_sad64x32
, vp9_sad64x32_avg
,
1783 vp9_variance64x32
, vp9_sub_pixel_variance64x32
,
1784 vp9_sub_pixel_avg_variance64x32
, NULL
, NULL
,
1788 BFP(BLOCK_32X64
, vp9_sad32x64
, vp9_sad32x64_avg
,
1789 vp9_variance32x64
, vp9_sub_pixel_variance32x64
,
1790 vp9_sub_pixel_avg_variance32x64
, NULL
, NULL
,
1794 BFP(BLOCK_32X32
, vp9_sad32x32
, vp9_sad32x32_avg
,
1795 vp9_variance32x32
, vp9_sub_pixel_variance32x32
,
1796 vp9_sub_pixel_avg_variance32x32
, vp9_variance_halfpixvar32x32_h
,
1797 vp9_variance_halfpixvar32x32_v
,
1798 vp9_variance_halfpixvar32x32_hv
, vp9_sad32x32x3
, vp9_sad32x32x8
,
1801 BFP(BLOCK_64X64
, vp9_sad64x64
, vp9_sad64x64_avg
,
1802 vp9_variance64x64
, vp9_sub_pixel_variance64x64
,
1803 vp9_sub_pixel_avg_variance64x64
, vp9_variance_halfpixvar64x64_h
,
1804 vp9_variance_halfpixvar64x64_v
,
1805 vp9_variance_halfpixvar64x64_hv
, vp9_sad64x64x3
, vp9_sad64x64x8
,
1808 BFP(BLOCK_16X16
, vp9_sad16x16
, vp9_sad16x16_avg
,
1809 vp9_variance16x16
, vp9_sub_pixel_variance16x16
,
1810 vp9_sub_pixel_avg_variance16x16
, vp9_variance_halfpixvar16x16_h
,
1811 vp9_variance_halfpixvar16x16_v
,
1812 vp9_variance_halfpixvar16x16_hv
, vp9_sad16x16x3
, vp9_sad16x16x8
,
1815 BFP(BLOCK_16X8
, vp9_sad16x8
, vp9_sad16x8_avg
,
1816 vp9_variance16x8
, vp9_sub_pixel_variance16x8
,
1817 vp9_sub_pixel_avg_variance16x8
, NULL
, NULL
, NULL
,
1818 vp9_sad16x8x3
, vp9_sad16x8x8
, vp9_sad16x8x4d
)
1820 BFP(BLOCK_8X16
, vp9_sad8x16
, vp9_sad8x16_avg
,
1821 vp9_variance8x16
, vp9_sub_pixel_variance8x16
,
1822 vp9_sub_pixel_avg_variance8x16
, NULL
, NULL
, NULL
,
1823 vp9_sad8x16x3
, vp9_sad8x16x8
, vp9_sad8x16x4d
)
1825 BFP(BLOCK_8X8
, vp9_sad8x8
, vp9_sad8x8_avg
,
1826 vp9_variance8x8
, vp9_sub_pixel_variance8x8
,
1827 vp9_sub_pixel_avg_variance8x8
, NULL
, NULL
, NULL
,
1828 vp9_sad8x8x3
, vp9_sad8x8x8
, vp9_sad8x8x4d
)
1830 BFP(BLOCK_8X4
, vp9_sad8x4
, vp9_sad8x4_avg
,
1831 vp9_variance8x4
, vp9_sub_pixel_variance8x4
,
1832 vp9_sub_pixel_avg_variance8x4
, NULL
, NULL
,
1833 NULL
, NULL
, vp9_sad8x4x8
,
1836 BFP(BLOCK_4X8
, vp9_sad4x8
, vp9_sad4x8_avg
,
1837 vp9_variance4x8
, vp9_sub_pixel_variance4x8
,
1838 vp9_sub_pixel_avg_variance4x8
, NULL
, NULL
,
1839 NULL
, NULL
, vp9_sad4x8x8
,
1842 BFP(BLOCK_4X4
, vp9_sad4x4
, vp9_sad4x4_avg
,
1843 vp9_variance4x4
, vp9_sub_pixel_variance4x4
,
1844 vp9_sub_pixel_avg_variance4x4
, NULL
, NULL
, NULL
,
1845 vp9_sad4x4x3
, vp9_sad4x4x8
, vp9_sad4x4x4d
)
1847 cpi
->full_search_sad
= vp9_full_search_sad
;
1848 cpi
->diamond_search_sad
= vp9_diamond_search_sad
;
1849 cpi
->refining_search_sad
= vp9_refining_search_sad
;
1851 /* vp9_init_quantizer() is first called here. Add check in
1852 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1853 * called later when needed. This will avoid unnecessary calls of
1854 * vp9_init_quantizer() for every frame.
1856 vp9_init_quantizer(cpi
);
1858 vp9_loop_filter_init(cm
);
1860 cm
->error
.setjmp
= 0;
1862 vp9_zero(cpi
->common
.counts
.uv_mode
);
1864 #ifdef MODE_TEST_HIT_STATS
1865 vp9_zero(cpi
->mode_test_hits
);
1868 return (VP9_PTR
) cpi
;
1871 void vp9_remove_compressor(VP9_PTR
*ptr
) {
1872 VP9_COMP
*cpi
= (VP9_COMP
*)(*ptr
);
1878 if (cpi
&& (cpi
->common
.current_video_frame
> 0)) {
1879 if (cpi
->pass
== 2) {
1880 vp9_end_second_pass(cpi
);
1883 #if CONFIG_INTERNAL_STATS
1885 vp9_clear_system_state();
1887 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1888 if (cpi
->pass
!= 1) {
1889 FILE *f
= fopen("opsnr.stt", "a");
1890 double time_encoded
= (cpi
->last_end_time_stamp_seen
1891 - cpi
->first_time_stamp_ever
) / 10000000.000;
1892 double total_encode_time
= (cpi
->time_receive_data
+
1893 cpi
->time_compress_data
) / 1000.000;
1894 double dr
= (double)cpi
->bytes
* (double) 8 / (double)1000
1897 if (cpi
->b_calculate_psnr
) {
1898 const double total_psnr
= vp9_mse2psnr(cpi
->total_samples
, 255.0,
1899 cpi
->total_sq_error
);
1900 const double totalp_psnr
= vp9_mse2psnr(cpi
->totalp_samples
, 255.0,
1901 cpi
->totalp_sq_error
);
1902 const double total_ssim
= 100 * pow(cpi
->summed_quality
/
1903 cpi
->summed_weights
, 8.0);
1904 const double totalp_ssim
= 100 * pow(cpi
->summedp_quality
/
1905 cpi
->summedp_weights
, 8.0);
1907 fprintf(f
, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1908 "VPXSSIM\tVPSSIMP\t Time(ms)\n");
1909 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",
1910 dr
, cpi
->total
/ cpi
->count
, total_psnr
,
1911 cpi
->totalp
/ cpi
->count
, totalp_psnr
, total_ssim
, totalp_ssim
,
1915 if (cpi
->b_calculate_ssimg
) {
1916 fprintf(f
, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n");
1917 fprintf(f
, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr
,
1918 cpi
->total_ssimg_y
/ cpi
->count
,
1919 cpi
->total_ssimg_u
/ cpi
->count
,
1920 cpi
->total_ssimg_v
/ cpi
->count
,
1921 cpi
->total_ssimg_all
/ cpi
->count
, total_encode_time
);
1929 #ifdef MODE_TEST_HIT_STATS
1930 if (cpi
->pass
!= 1) {
1931 double norm_per_pixel_mode_tests
= 0;
1932 double norm_counts
[BLOCK_SIZES
];
1935 int norm_factors
[BLOCK_SIZES
] =
1936 {256, 128, 128, 64, 32, 32, 16, 8, 8, 4, 2, 2, 1};
1937 FILE *f
= fopen("mode_hit_stats.stt", "a");
1939 // On average, how many mode tests do we do
1940 for (i
= 0; i
< BLOCK_SIZES
; ++i
) {
1941 norm_counts
[i
] = (double)cpi
->mode_test_hits
[i
] /
1942 (double)norm_factors
[i
];
1943 norm_per_pixel_mode_tests
+= norm_counts
[i
];
1945 // Convert to a number per 64x64 and per frame
1946 sb64_per_frame
= ((cpi
->common
.height
+ 63) / 64) *
1947 ((cpi
->common
.width
+ 63) / 64);
1948 norm_per_pixel_mode_tests
=
1949 norm_per_pixel_mode_tests
/
1950 (double)(cpi
->common
.current_video_frame
* sb64_per_frame
);
1952 fprintf(f
, "%6.4f\n", norm_per_pixel_mode_tests
);
1957 #if defined(SECTIONBITS_OUTPUT)
1961 FILE *f
= fopen("tokenbits.stt", "a");
1963 for (i
= 0; i
< 28; i
++)
1964 fprintf(f
, "%8d", (int)(Sectionbits
[i
] / 256));
1974 printf("\n_pick_loop_filter_level:%d\n", cpi
->time_pick_lpf
/ 1000);
1975 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
1976 printf("%6d %10ld %10ld %10ld %10ld\n", cpi
->common
.current_video_frame
,
1977 cpi
->time_receive_data
/ 1000, cpi
->time_encode_sb_row
/ 1000,
1978 cpi
->time_compress_data
/ 1000,
1979 (cpi
->time_receive_data
+ cpi
->time_compress_data
) / 1000);
1984 free_pick_mode_context(&cpi
->mb
);
1985 dealloc_compressor_data(cpi
);
1986 vpx_free(cpi
->mb
.ss
);
1989 for (i
= 0; i
< sizeof(cpi
->mbgraph_stats
) /
1990 sizeof(cpi
->mbgraph_stats
[0]); ++i
) {
1991 vpx_free(cpi
->mbgraph_stats
[i
].mb_stats
);
1994 vp9_remove_common(&cpi
->common
);
1998 #ifdef OUTPUT_YUV_SRC
2001 #ifdef OUTPUT_YUV_REC
2002 fclose(yuv_rec_file
);
2020 static uint64_t calc_plane_error(const uint8_t *orig
, int orig_stride
,
2021 const uint8_t *recon
, int recon_stride
,
2022 unsigned int cols
, unsigned int rows
) {
2023 unsigned int row
, col
;
2024 uint64_t total_sse
= 0;
2027 for (row
= 0; row
+ 16 <= rows
; row
+= 16) {
2028 for (col
= 0; col
+ 16 <= cols
; col
+= 16) {
2031 vp9_mse16x16(orig
+ col
, orig_stride
, recon
+ col
, recon_stride
, &sse
);
2035 /* Handle odd-sized width */
2037 unsigned int border_row
, border_col
;
2038 const uint8_t *border_orig
= orig
;
2039 const uint8_t *border_recon
= recon
;
2041 for (border_row
= 0; border_row
< 16; border_row
++) {
2042 for (border_col
= col
; border_col
< cols
; border_col
++) {
2043 diff
= border_orig
[border_col
] - border_recon
[border_col
];
2044 total_sse
+= diff
* diff
;
2047 border_orig
+= orig_stride
;
2048 border_recon
+= recon_stride
;
2052 orig
+= orig_stride
* 16;
2053 recon
+= recon_stride
* 16;
2056 /* Handle odd-sized height */
2057 for (; row
< rows
; row
++) {
2058 for (col
= 0; col
< cols
; col
++) {
2059 diff
= orig
[col
] - recon
[col
];
2060 total_sse
+= diff
* diff
;
2063 orig
+= orig_stride
;
2064 recon
+= recon_stride
;
2071 double psnr
[4]; // total/y/u/v
2072 uint64_t sse
[4]; // total/y/u/v
2073 uint32_t samples
[4]; // total/y/u/v
2076 static void calc_psnr(const YV12_BUFFER_CONFIG
*a
, const YV12_BUFFER_CONFIG
*b
,
2078 const int widths
[3] = {a
->y_width
, a
->uv_width
, a
->uv_width
};
2079 const int heights
[3] = {a
->y_height
, a
->uv_height
, a
->uv_height
};
2080 const uint8_t *a_planes
[3] = {a
->y_buffer
, a
->u_buffer
, a
->v_buffer
};
2081 const int a_strides
[3] = {a
->y_stride
, a
->uv_stride
, a
->uv_stride
};
2082 const uint8_t *b_planes
[3] = {b
->y_buffer
, b
->u_buffer
, b
->v_buffer
};
2083 const int b_strides
[3] = {b
->y_stride
, b
->uv_stride
, b
->uv_stride
};
2085 uint64_t total_sse
= 0;
2086 uint32_t total_samples
= 0;
2088 for (i
= 0; i
< 3; ++i
) {
2089 const int w
= widths
[i
];
2090 const int h
= heights
[i
];
2091 const uint32_t samples
= w
* h
;
2092 const double sse
= calc_plane_error(a_planes
[i
], a_strides
[i
],
2093 b_planes
[i
], b_strides
[i
],
2095 psnr
->sse
[1 + i
] = sse
;
2096 psnr
->samples
[1 + i
] = samples
;
2097 psnr
->psnr
[1 + i
] = vp9_mse2psnr(samples
, 255.0, sse
);
2100 total_samples
+= samples
;
2103 psnr
->sse
[0] = total_sse
;
2104 psnr
->samples
[0] = total_samples
;
2105 psnr
->psnr
[0] = vp9_mse2psnr(total_samples
, 255.0, total_sse
);
2108 static void generate_psnr_packet(VP9_COMP
*cpi
) {
2109 struct vpx_codec_cx_pkt pkt
;
2112 calc_psnr(cpi
->Source
, cpi
->common
.frame_to_show
, &psnr
);
2113 for (i
= 0; i
< 4; ++i
) {
2114 pkt
.data
.psnr
.samples
[i
] = psnr
.samples
[i
];
2115 pkt
.data
.psnr
.sse
[i
] = psnr
.sse
[i
];
2116 pkt
.data
.psnr
.psnr
[i
] = psnr
.psnr
[i
];
2118 pkt
.kind
= VPX_CODEC_PSNR_PKT
;
2119 vpx_codec_pkt_list_add(cpi
->output_pkt_list
, &pkt
);
2122 int vp9_use_as_reference(VP9_PTR ptr
, int ref_frame_flags
) {
2123 VP9_COMP
*cpi
= (VP9_COMP
*)(ptr
);
2125 if (ref_frame_flags
> 7)
2128 cpi
->ref_frame_flags
= ref_frame_flags
;
2132 int vp9_update_reference(VP9_PTR ptr
, int ref_frame_flags
) {
2133 VP9_COMP
*cpi
= (VP9_COMP
*)(ptr
);
2135 if (ref_frame_flags
> 7)
2138 cpi
->ext_refresh_golden_frame
= 0;
2139 cpi
->ext_refresh_alt_ref_frame
= 0;
2140 cpi
->ext_refresh_last_frame
= 0;
2142 if (ref_frame_flags
& VP9_LAST_FLAG
)
2143 cpi
->ext_refresh_last_frame
= 1;
2145 if (ref_frame_flags
& VP9_GOLD_FLAG
)
2146 cpi
->ext_refresh_golden_frame
= 1;
2148 if (ref_frame_flags
& VP9_ALT_FLAG
)
2149 cpi
->ext_refresh_alt_ref_frame
= 1;
2151 cpi
->ext_refresh_frame_flags_pending
= 1;
2155 static YV12_BUFFER_CONFIG
*get_vp9_ref_frame_buffer(VP9_COMP
*cpi
,
2156 VP9_REFFRAME ref_frame_flag
) {
2157 MV_REFERENCE_FRAME ref_frame
= NONE
;
2158 if (ref_frame_flag
== VP9_LAST_FLAG
)
2159 ref_frame
= LAST_FRAME
;
2160 else if (ref_frame_flag
== VP9_GOLD_FLAG
)
2161 ref_frame
= GOLDEN_FRAME
;
2162 else if (ref_frame_flag
== VP9_ALT_FLAG
)
2163 ref_frame
= ALTREF_FRAME
;
2165 return ref_frame
== NONE
? NULL
: get_ref_frame_buffer(cpi
, ref_frame
);
2168 int vp9_copy_reference_enc(VP9_PTR ptr
, VP9_REFFRAME ref_frame_flag
,
2169 YV12_BUFFER_CONFIG
*sd
) {
2170 VP9_COMP
*const cpi
= (VP9_COMP
*)ptr
;
2171 YV12_BUFFER_CONFIG
*cfg
= get_vp9_ref_frame_buffer(cpi
, ref_frame_flag
);
2173 vp8_yv12_copy_frame(cfg
, sd
);
2180 int vp9_get_reference_enc(VP9_PTR ptr
, int index
, YV12_BUFFER_CONFIG
**fb
) {
2181 VP9_COMP
*cpi
= (VP9_COMP
*)ptr
;
2182 VP9_COMMON
*cm
= &cpi
->common
;
2184 if (index
< 0 || index
>= REF_FRAMES
)
2187 *fb
= &cm
->yv12_fb
[cm
->ref_frame_map
[index
]];
2191 int vp9_set_reference_enc(VP9_PTR ptr
, VP9_REFFRAME ref_frame_flag
,
2192 YV12_BUFFER_CONFIG
*sd
) {
2193 VP9_COMP
*cpi
= (VP9_COMP
*)ptr
;
2194 YV12_BUFFER_CONFIG
*cfg
= get_vp9_ref_frame_buffer(cpi
, ref_frame_flag
);
2196 vp8_yv12_copy_frame(sd
, cfg
);
2203 int vp9_update_entropy(VP9_PTR comp
, int update
) {
2204 ((VP9_COMP
*)comp
)->ext_refresh_frame_context
= update
;
2205 ((VP9_COMP
*)comp
)->ext_refresh_frame_context_pending
= 1;
2210 #ifdef OUTPUT_YUV_SRC
2211 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG
*s
) {
2212 uint8_t *src
= s
->y_buffer
;
2213 int h
= s
->y_height
;
2216 fwrite(src
, s
->y_width
, 1, yuv_file
);
2224 fwrite(src
, s
->uv_width
, 1, yuv_file
);
2225 src
+= s
->uv_stride
;
2232 fwrite(src
, s
->uv_width
, 1, yuv_file
);
2233 src
+= s
->uv_stride
;
2238 #ifdef OUTPUT_YUV_REC
2239 void vp9_write_yuv_rec_frame(VP9_COMMON
*cm
) {
2240 YV12_BUFFER_CONFIG
*s
= cm
->frame_to_show
;
2241 uint8_t *src
= s
->y_buffer
;
2245 fwrite(src
, s
->y_width
, 1, yuv_rec_file
);
2253 fwrite(src
, s
->uv_width
, 1, yuv_rec_file
);
2254 src
+= s
->uv_stride
;
2261 fwrite(src
, s
->uv_width
, 1, yuv_rec_file
);
2262 src
+= s
->uv_stride
;
2266 if (s
->alpha_buffer
) {
2267 src
= s
->alpha_buffer
;
2268 h
= s
->alpha_height
;
2270 fwrite(src
, s
->alpha_width
, 1, yuv_rec_file
);
2271 src
+= s
->alpha_stride
;
2276 fflush(yuv_rec_file
);
2280 static void scale_and_extend_frame_nonnormative(YV12_BUFFER_CONFIG
*src_fb
,
2281 YV12_BUFFER_CONFIG
*dst_fb
) {
2282 const int in_w
= src_fb
->y_crop_width
;
2283 const int in_h
= src_fb
->y_crop_height
;
2284 const int out_w
= dst_fb
->y_crop_width
;
2285 const int out_h
= dst_fb
->y_crop_height
;
2286 const int in_w_uv
= src_fb
->uv_crop_width
;
2287 const int in_h_uv
= src_fb
->uv_crop_height
;
2288 const int out_w_uv
= dst_fb
->uv_crop_width
;
2289 const int out_h_uv
= dst_fb
->uv_crop_height
;
2292 uint8_t *srcs
[4] = {src_fb
->y_buffer
, src_fb
->u_buffer
, src_fb
->v_buffer
,
2293 src_fb
->alpha_buffer
};
2294 int src_strides
[4] = {src_fb
->y_stride
, src_fb
->uv_stride
, src_fb
->uv_stride
,
2295 src_fb
->alpha_stride
};
2297 uint8_t *dsts
[4] = {dst_fb
->y_buffer
, dst_fb
->u_buffer
, dst_fb
->v_buffer
,
2298 dst_fb
->alpha_buffer
};
2299 int dst_strides
[4] = {dst_fb
->y_stride
, dst_fb
->uv_stride
, dst_fb
->uv_stride
,
2300 dst_fb
->alpha_stride
};
2302 for (i
= 0; i
< MAX_MB_PLANE
; ++i
) {
2303 if (i
== 0 || i
== 3) {
2304 // Y and alpha planes
2305 vp9_resize_plane(srcs
[i
], in_h
, in_w
, src_strides
[i
],
2306 dsts
[i
], out_h
, out_w
, dst_strides
[i
]);
2309 vp9_resize_plane(srcs
[i
], in_h_uv
, in_w_uv
, src_strides
[i
],
2310 dsts
[i
], out_h_uv
, out_w_uv
, dst_strides
[i
]);
2313 vp8_yv12_extend_frame_borders(dst_fb
);
2316 static void scale_and_extend_frame(YV12_BUFFER_CONFIG
*src_fb
,
2317 YV12_BUFFER_CONFIG
*dst_fb
) {
2318 const int in_w
= src_fb
->y_crop_width
;
2319 const int in_h
= src_fb
->y_crop_height
;
2320 const int out_w
= dst_fb
->y_crop_width
;
2321 const int out_h
= dst_fb
->y_crop_height
;
2324 uint8_t *srcs
[4] = {src_fb
->y_buffer
, src_fb
->u_buffer
, src_fb
->v_buffer
,
2325 src_fb
->alpha_buffer
};
2326 int src_strides
[4] = {src_fb
->y_stride
, src_fb
->uv_stride
, src_fb
->uv_stride
,
2327 src_fb
->alpha_stride
};
2329 uint8_t *dsts
[4] = {dst_fb
->y_buffer
, dst_fb
->u_buffer
, dst_fb
->v_buffer
,
2330 dst_fb
->alpha_buffer
};
2331 int dst_strides
[4] = {dst_fb
->y_stride
, dst_fb
->uv_stride
, dst_fb
->uv_stride
,
2332 dst_fb
->alpha_stride
};
2334 for (y
= 0; y
< out_h
; y
+= 16) {
2335 for (x
= 0; x
< out_w
; x
+= 16) {
2336 for (i
= 0; i
< MAX_MB_PLANE
; ++i
) {
2337 const int factor
= (i
== 0 || i
== 3 ? 1 : 2);
2338 const int x_q4
= x
* (16 / factor
) * in_w
/ out_w
;
2339 const int y_q4
= y
* (16 / factor
) * in_h
/ out_h
;
2340 const int src_stride
= src_strides
[i
];
2341 const int dst_stride
= dst_strides
[i
];
2342 uint8_t *src
= srcs
[i
] + y
/ factor
* in_h
/ out_h
* src_stride
+
2343 x
/ factor
* in_w
/ out_w
;
2344 uint8_t *dst
= dsts
[i
] + y
/ factor
* dst_stride
+ x
/ factor
;
2346 vp9_convolve8(src
, src_stride
, dst
, dst_stride
,
2347 vp9_sub_pel_filters_8
[x_q4
& 0xf], 16 * in_w
/ out_w
,
2348 vp9_sub_pel_filters_8
[y_q4
& 0xf], 16 * in_h
/ out_h
,
2349 16 / factor
, 16 / factor
);
2354 vp8_yv12_extend_frame_borders(dst_fb
);
2357 static int find_fp_qindex() {
2360 for (i
= 0; i
< QINDEX_RANGE
; i
++) {
2361 if (vp9_convert_qindex_to_q(i
) >= 30.0) {
2366 if (i
== QINDEX_RANGE
)
2372 #define WRITE_RECON_BUFFER 0
2373 #if WRITE_RECON_BUFFER
2374 void write_cx_frame_to_file(YV12_BUFFER_CONFIG
*frame
, int this_frame
) {
2379 snprintf(filename
, sizeof(filename
), "cx\\y%04d.raw", this_frame
);
2380 yframe
= fopen(filename
, "wb");
2382 for (i
= 0; i
< frame
->y_height
; i
++)
2383 fwrite(frame
->y_buffer
+ i
* frame
->y_stride
,
2384 frame
->y_width
, 1, yframe
);
2387 snprintf(filename
, sizeof(filename
), "cx\\u%04d.raw", this_frame
);
2388 yframe
= fopen(filename
, "wb");
2390 for (i
= 0; i
< frame
->uv_height
; i
++)
2391 fwrite(frame
->u_buffer
+ i
* frame
->uv_stride
,
2392 frame
->uv_width
, 1, yframe
);
2395 snprintf(filename
, sizeof(filename
), "cx\\v%04d.raw", this_frame
);
2396 yframe
= fopen(filename
, "wb");
2398 for (i
= 0; i
< frame
->uv_height
; i
++)
2399 fwrite(frame
->v_buffer
+ i
* frame
->uv_stride
,
2400 frame
->uv_width
, 1, yframe
);
2406 static double compute_edge_pixel_proportion(YV12_BUFFER_CONFIG
*frame
) {
2407 #define EDGE_THRESH 128
2409 int num_edge_pels
= 0;
2410 int num_pels
= (frame
->y_height
- 2) * (frame
->y_width
- 2);
2411 uint8_t *prev
= frame
->y_buffer
+ 1;
2412 uint8_t *curr
= frame
->y_buffer
+ 1 + frame
->y_stride
;
2413 uint8_t *next
= frame
->y_buffer
+ 1 + 2 * frame
->y_stride
;
2414 for (i
= 1; i
< frame
->y_height
- 1; i
++) {
2415 for (j
= 1; j
< frame
->y_width
- 1; j
++) {
2416 /* Sobel hor and ver gradients */
2417 int v
= 2 * (curr
[1] - curr
[-1]) + (prev
[1] - prev
[-1]) +
2418 (next
[1] - next
[-1]);
2419 int h
= 2 * (prev
[0] - next
[0]) + (prev
[1] - next
[1]) +
2420 (prev
[-1] - next
[-1]);
2421 h
= (h
< 0 ? -h
: h
);
2422 v
= (v
< 0 ? -v
: v
);
2423 if (h
> EDGE_THRESH
|| v
> EDGE_THRESH
)
2429 curr
+= frame
->y_stride
- frame
->y_width
+ 2;
2430 prev
+= frame
->y_stride
- frame
->y_width
+ 2;
2431 next
+= frame
->y_stride
- frame
->y_width
+ 2;
2433 return (double)num_edge_pels
/ num_pels
;
2436 // Function to test for conditions that indicate we should loop
2437 // back and recode a frame.
2438 static int recode_loop_test(VP9_COMP
*cpi
,
2439 int high_limit
, int low_limit
,
2440 int q
, int maxq
, int minq
) {
2441 int force_recode
= 0;
2442 VP9_COMMON
*cm
= &cpi
->common
;
2444 // Special case trap if maximum allowed frame size exceeded.
2445 if (cpi
->rc
.projected_frame_size
> cpi
->rc
.max_frame_bandwidth
) {
2448 // Is frame recode allowed.
2449 // Yes if either recode mode 1 is selected or mode 2 is selected
2450 // and the frame is a key frame, golden frame or alt_ref_frame
2451 } else if ((cpi
->sf
.recode_loop
== 1) ||
2452 ((cpi
->sf
.recode_loop
== 2) &&
2453 ((cm
->frame_type
== KEY_FRAME
) ||
2454 cpi
->refresh_golden_frame
||
2455 cpi
->refresh_alt_ref_frame
))) {
2456 // General over and under shoot tests
2457 if (((cpi
->rc
.projected_frame_size
> high_limit
) && (q
< maxq
)) ||
2458 ((cpi
->rc
.projected_frame_size
< low_limit
) && (q
> minq
))) {
2460 } else if (cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
) {
2461 // Deal with frame undershoot and whether or not we are
2462 // below the automatically set cq level.
2463 if (q
> cpi
->cq_target_quality
&&
2464 cpi
->rc
.projected_frame_size
<
2465 ((cpi
->rc
.this_frame_target
* 7) >> 3)) {
2470 return force_recode
;
2473 static void update_reference_frames(VP9_COMP
* const cpi
) {
2474 VP9_COMMON
* const cm
= &cpi
->common
;
2476 // At this point the new frame has been encoded.
2477 // If any buffer copy / swapping is signaled it should be done here.
2478 if (cm
->frame_type
== KEY_FRAME
) {
2479 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2480 &cm
->ref_frame_map
[cpi
->gld_fb_idx
], cm
->new_fb_idx
);
2481 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2482 &cm
->ref_frame_map
[cpi
->alt_fb_idx
], cm
->new_fb_idx
);
2484 #if CONFIG_MULTIPLE_ARF
2485 else if (!cpi
->multi_arf_enabled
&& cpi
->refresh_golden_frame
&&
2486 !cpi
->refresh_alt_ref_frame
) {
2488 else if (cpi
->refresh_golden_frame
&& !cpi
->refresh_alt_ref_frame
&&
2491 /* Preserve the previously existing golden frame and update the frame in
2492 * the alt ref slot instead. This is highly specific to the current use of
2493 * alt-ref as a forward reference, and this needs to be generalized as
2494 * other uses are implemented (like RTC/temporal scaling)
2496 * The update to the buffer in the alt ref slot was signaled in
2497 * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated
2498 * as the golden frame next time.
2502 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2503 &cm
->ref_frame_map
[cpi
->alt_fb_idx
], cm
->new_fb_idx
);
2505 tmp
= cpi
->alt_fb_idx
;
2506 cpi
->alt_fb_idx
= cpi
->gld_fb_idx
;
2507 cpi
->gld_fb_idx
= tmp
;
2508 } else { /* For non key/golden frames */
2509 if (cpi
->refresh_alt_ref_frame
) {
2510 int arf_idx
= cpi
->alt_fb_idx
;
2511 #if CONFIG_MULTIPLE_ARF
2512 if (cpi
->multi_arf_enabled
) {
2513 arf_idx
= cpi
->arf_buffer_idx
[cpi
->sequence_number
+ 1];
2516 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2517 &cm
->ref_frame_map
[arf_idx
], cm
->new_fb_idx
);
2520 if (cpi
->refresh_golden_frame
) {
2521 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2522 &cm
->ref_frame_map
[cpi
->gld_fb_idx
], cm
->new_fb_idx
);
2526 if (cpi
->refresh_last_frame
) {
2527 ref_cnt_fb(cm
->fb_idx_ref_cnt
,
2528 &cm
->ref_frame_map
[cpi
->lst_fb_idx
], cm
->new_fb_idx
);
2532 static void loopfilter_frame(VP9_COMP
*cpi
, VP9_COMMON
*cm
) {
2533 MACROBLOCKD
*xd
= &cpi
->mb
.e_mbd
;
2534 struct loopfilter
*lf
= &cm
->lf
;
2536 lf
->filter_level
= 0;
2538 struct vpx_usec_timer timer
;
2540 vp9_clear_system_state();
2542 vpx_usec_timer_start(&timer
);
2544 vp9_pick_filter_level(cpi
->Source
, cpi
, cpi
->sf
.use_fast_lpf_pick
);
2546 vpx_usec_timer_mark(&timer
);
2547 cpi
->time_pick_lpf
+= vpx_usec_timer_elapsed(&timer
);
2550 if (lf
->filter_level
> 0) {
2551 vp9_set_alt_lf_level(cpi
, lf
->filter_level
);
2552 vp9_loop_filter_frame(cm
, xd
, lf
->filter_level
, 0, 0);
2555 vp9_extend_frame_inner_borders(cm
->frame_to_show
,
2556 cm
->subsampling_x
, cm
->subsampling_y
);
2559 static void scale_references(VP9_COMP
*cpi
) {
2560 VP9_COMMON
*cm
= &cpi
->common
;
2561 MV_REFERENCE_FRAME ref_frame
;
2563 for (ref_frame
= LAST_FRAME
; ref_frame
<= ALTREF_FRAME
; ++ref_frame
) {
2564 const int idx
= cm
->ref_frame_map
[get_ref_frame_idx(cpi
, ref_frame
)];
2565 YV12_BUFFER_CONFIG
*ref
= &cm
->yv12_fb
[idx
];
2567 if (ref
->y_crop_width
!= cm
->width
||
2568 ref
->y_crop_height
!= cm
->height
) {
2569 const int new_fb
= get_free_fb(cm
);
2570 vp9_realloc_frame_buffer(&cm
->yv12_fb
[new_fb
],
2571 cm
->width
, cm
->height
,
2572 cm
->subsampling_x
, cm
->subsampling_y
,
2573 VP9_ENC_BORDER_IN_PIXELS
);
2574 scale_and_extend_frame(ref
, &cm
->yv12_fb
[new_fb
]);
2575 cpi
->scaled_ref_idx
[ref_frame
- 1] = new_fb
;
2577 cpi
->scaled_ref_idx
[ref_frame
- 1] = idx
;
2578 cm
->fb_idx_ref_cnt
[idx
]++;
2583 static void release_scaled_references(VP9_COMP
*cpi
) {
2584 VP9_COMMON
*cm
= &cpi
->common
;
2587 for (i
= 0; i
< 3; i
++)
2588 cm
->fb_idx_ref_cnt
[cpi
->scaled_ref_idx
[i
]]--;
2591 static void full_to_model_count(unsigned int *model_count
,
2592 unsigned int *full_count
) {
2594 model_count
[ZERO_TOKEN
] = full_count
[ZERO_TOKEN
];
2595 model_count
[ONE_TOKEN
] = full_count
[ONE_TOKEN
];
2596 model_count
[TWO_TOKEN
] = full_count
[TWO_TOKEN
];
2597 for (n
= THREE_TOKEN
; n
< EOB_TOKEN
; ++n
)
2598 model_count
[TWO_TOKEN
] += full_count
[n
];
2599 model_count
[EOB_MODEL_TOKEN
] = full_count
[EOB_TOKEN
];
2602 static void full_to_model_counts(vp9_coeff_count_model
*model_count
,
2603 vp9_coeff_count
*full_count
) {
2606 for (i
= 0; i
< PLANE_TYPES
; ++i
)
2607 for (j
= 0; j
< REF_TYPES
; ++j
)
2608 for (k
= 0; k
< COEF_BANDS
; ++k
)
2609 for (l
= 0; l
< BAND_COEFF_CONTEXTS(k
); ++l
)
2610 full_to_model_count(model_count
[i
][j
][k
][l
], full_count
[i
][j
][k
][l
]);
2613 #if 0 && CONFIG_INTERNAL_STATS
2614 static void output_frame_level_debug_stats(VP9_COMP
*cpi
) {
2615 VP9_COMMON
*const cm
= &cpi
->common
;
2616 FILE *const f
= fopen("tmp.stt", cm
->current_video_frame
? "a" : "w");
2619 vp9_clear_system_state(); // __asm emms;
2621 recon_err
= vp9_calc_ss_err(cpi
->Source
, get_frame_new_buffer(cm
));
2623 if (cpi
->twopass
.total_left_stats
.coded_error
!= 0.0)
2624 fprintf(f
, "%10u %10d %10d %10d %10d %10d "
2625 "%10"PRId64
" %10"PRId64
" %10d "
2626 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2627 "%6d %6d %5d %5d %5d "
2628 "%10"PRId64
" %10.3lf"
2629 "%10lf %8u %10d %10d %10d\n",
2630 cpi
->common
.current_video_frame
, cpi
->rc
.this_frame_target
,
2631 cpi
->rc
.projected_frame_size
,
2632 cpi
->rc
.projected_frame_size
/ cpi
->common
.MBs
,
2633 (cpi
->rc
.projected_frame_size
- cpi
->rc
.this_frame_target
),
2634 cpi
->rc
.total_target_vs_actual
,
2635 (cpi
->oxcf
.starting_buffer_level
- cpi
->rc
.bits_off_target
),
2636 cpi
->rc
.total_actual_bits
, cm
->base_qindex
,
2637 vp9_convert_qindex_to_q(cm
->base_qindex
),
2638 (double)vp9_dc_quant(cm
->base_qindex
, 0) / 4.0,
2639 vp9_convert_qindex_to_q(cpi
->rc
.active_worst_quality
), cpi
->rc
.avg_q
,
2640 vp9_convert_qindex_to_q(cpi
->rc
.ni_av_qi
),
2641 vp9_convert_qindex_to_q(cpi
->cq_target_quality
),
2642 cpi
->refresh_last_frame
, cpi
->refresh_golden_frame
,
2643 cpi
->refresh_alt_ref_frame
, cm
->frame_type
, cpi
->rc
.gfu_boost
,
2644 cpi
->twopass
.bits_left
,
2645 cpi
->twopass
.total_left_stats
.coded_error
,
2646 cpi
->twopass
.bits_left
/
2647 (1 + cpi
->twopass
.total_left_stats
.coded_error
),
2648 cpi
->tot_recode_hits
, recon_err
, cpi
->rc
.kf_boost
,
2649 cpi
->twopass
.kf_zeromotion_pct
);
2654 FILE *const fmodes
= fopen("Modes.stt", "a");
2657 fprintf(fmodes
, "%6d:%1d:%1d:%1d ", cpi
->common
.current_video_frame
,
2658 cm
->frame_type
, cpi
->refresh_golden_frame
,
2659 cpi
->refresh_alt_ref_frame
);
2661 for (i
= 0; i
< MAX_MODES
; ++i
)
2662 fprintf(fmodes
, "%5d ", cpi
->mode_chosen_counts
[i
]);
2663 for (i
= 0; i
< MAX_REFS
; ++i
)
2664 fprintf(fmodes
, "%5d ", cpi
->sub8x8_mode_chosen_counts
[i
]);
2666 fprintf(fmodes
, "\n");
2673 static void encode_with_recode_loop(VP9_COMP
*cpi
,
2679 int frame_over_shoot_limit
,
2680 int frame_under_shoot_limit
) {
2681 VP9_COMMON
*const cm
= &cpi
->common
;
2684 int overshoot_seen
= 0;
2685 int undershoot_seen
= 0;
2686 int q_low
= bottom_index
, q_high
= top_index
;
2689 vp9_clear_system_state(); // __asm emms;
2691 vp9_set_quantizer(cpi
, *q
);
2693 if (loop_count
== 0) {
2694 // Set up entropy context depending on frame type. The decoder mandates
2695 // the use of the default context, index 0, for keyframes and inter
2696 // frames where the error_resilient_mode or intra_only flag is set. For
2697 // other inter-frames the encoder currently uses only two contexts;
2698 // context 1 for ALTREF frames and context 0 for the others.
2699 if (cm
->frame_type
== KEY_FRAME
) {
2700 vp9_setup_key_frame(cpi
);
2702 if (!cm
->intra_only
&& !cm
->error_resilient_mode
) {
2703 cpi
->common
.frame_context_idx
= cpi
->refresh_alt_ref_frame
;
2705 vp9_setup_inter_frame(cpi
);
2709 // Variance adaptive and in frame q adjustment experiments are mutually
2711 if (cpi
->oxcf
.aq_mode
== VARIANCE_AQ
) {
2712 vp9_vaq_frame_setup(cpi
);
2713 } else if (cpi
->oxcf
.aq_mode
== COMPLEXITY_AQ
) {
2714 setup_in_frame_q_adj(cpi
);
2717 // transform / motion compensation build reconstruction frame
2719 vp9_encode_frame(cpi
);
2721 // Update the skip mb flag probabilities based on the distribution
2722 // seen in the last encoder iteration.
2723 // update_base_skip_probs(cpi);
2725 vp9_clear_system_state(); // __asm emms;
2727 // Dummy pack of the bitstream using up to date stats to get an
2728 // accurate estimate of output frame size to determine if we need
2730 if (cpi
->sf
.recode_loop
!= 0) {
2731 vp9_save_coding_context(cpi
);
2732 cpi
->dummy_packing
= 1;
2733 vp9_pack_bitstream(cpi
, dest
, size
);
2734 cpi
->rc
.projected_frame_size
= (*size
) << 3;
2735 vp9_restore_coding_context(cpi
);
2737 if (frame_over_shoot_limit
== 0)
2738 frame_over_shoot_limit
= 1;
2741 if (cpi
->oxcf
.end_usage
== USAGE_CONSTANT_QUALITY
) {
2744 if ((cm
->frame_type
== KEY_FRAME
) &&
2745 cpi
->rc
.this_key_frame_forced
&&
2746 (cpi
->rc
.projected_frame_size
< cpi
->rc
.max_frame_bandwidth
)) {
2748 int kf_err
= vp9_calc_ss_err(cpi
->Source
, get_frame_new_buffer(cm
));
2750 int high_err_target
= cpi
->ambient_err
;
2751 int low_err_target
= cpi
->ambient_err
>> 1;
2753 // Prevent possible divide by zero error below for perfect KF
2756 // The key frame is not good enough or we can afford
2757 // to make it better without undue risk of popping.
2758 if ((kf_err
> high_err_target
&&
2759 cpi
->rc
.projected_frame_size
<= frame_over_shoot_limit
) ||
2760 (kf_err
> low_err_target
&&
2761 cpi
->rc
.projected_frame_size
<= frame_under_shoot_limit
)) {
2763 q_high
= *q
> q_low
? *q
- 1 : q_low
;
2766 *q
= ((*q
) * high_err_target
) / kf_err
;
2767 *q
= MIN((*q
), (q_high
+ q_low
) >> 1);
2768 } else if (kf_err
< low_err_target
&&
2769 cpi
->rc
.projected_frame_size
>= frame_under_shoot_limit
) {
2770 // The key frame is much better than the previous frame
2772 q_low
= *q
< q_high
? *q
+ 1 : q_high
;
2775 *q
= ((*q
) * low_err_target
) / kf_err
;
2776 *q
= MIN((*q
), (q_high
+ q_low
+ 1) >> 1);
2779 // Clamp Q to upper and lower limits:
2780 *q
= clamp(*q
, q_low
, q_high
);
2782 loop
= *q
!= last_q
;
2783 } else if (recode_loop_test(
2784 cpi
, frame_over_shoot_limit
, frame_under_shoot_limit
,
2785 *q
, MAX(q_high
, top_index
), bottom_index
)) {
2786 // Is the projected frame size out of range and are we allowed
2787 // to attempt to recode.
2791 // Frame size out of permitted range:
2792 // Update correction factor & compute new Q to try...
2794 // Frame is too large
2795 if (cpi
->rc
.projected_frame_size
> cpi
->rc
.this_frame_target
) {
2796 // Special case if the projected size is > the max allowed.
2797 if (cpi
->rc
.projected_frame_size
>= cpi
->rc
.max_frame_bandwidth
)
2798 q_high
= cpi
->rc
.worst_quality
;
2800 // Raise Qlow as to at least the current value
2801 q_low
= *q
< q_high
? *q
+ 1 : q_high
;
2803 if (undershoot_seen
|| loop_count
> 1) {
2804 // Update rate_correction_factor unless
2805 vp9_rc_update_rate_correction_factors(cpi
, 1);
2807 *q
= (q_high
+ q_low
+ 1) / 2;
2809 // Update rate_correction_factor unless
2810 vp9_rc_update_rate_correction_factors(cpi
, 0);
2812 *q
= vp9_rc_regulate_q(cpi
, cpi
->rc
.this_frame_target
,
2813 bottom_index
, MAX(q_high
, top_index
));
2815 while (*q
< q_low
&& retries
< 10) {
2816 vp9_rc_update_rate_correction_factors(cpi
, 0);
2817 *q
= vp9_rc_regulate_q(cpi
, cpi
->rc
.this_frame_target
,
2818 bottom_index
, MAX(q_high
, top_index
));
2825 // Frame is too small
2826 q_high
= *q
> q_low
? *q
- 1 : q_low
;
2828 if (overshoot_seen
|| loop_count
> 1) {
2829 vp9_rc_update_rate_correction_factors(cpi
, 1);
2830 *q
= (q_high
+ q_low
) / 2;
2832 vp9_rc_update_rate_correction_factors(cpi
, 0);
2833 *q
= vp9_rc_regulate_q(cpi
, cpi
->rc
.this_frame_target
,
2834 bottom_index
, top_index
);
2835 // Special case reset for qlow for constrained quality.
2836 // This should only trigger where there is very substantial
2837 // undershoot on a frame and the auto cq level is above
2838 // the user passsed in value.
2839 if (cpi
->oxcf
.end_usage
== USAGE_CONSTRAINED_QUALITY
&&
2844 while (*q
> q_high
&& retries
< 10) {
2845 vp9_rc_update_rate_correction_factors(cpi
, 0);
2846 *q
= vp9_rc_regulate_q(cpi
, cpi
->rc
.this_frame_target
,
2847 bottom_index
, top_index
);
2852 undershoot_seen
= 1;
2855 // Clamp Q to upper and lower limits:
2856 *q
= clamp(*q
, q_low
, q_high
);
2858 loop
= *q
!= last_q
;
2864 // Special case for overlay frame.
2865 if (cpi
->rc
.is_src_frame_alt_ref
&&
2866 (cpi
->rc
.projected_frame_size
< cpi
->rc
.max_frame_bandwidth
))
2872 #if CONFIG_INTERNAL_STATS
2873 cpi
->tot_recode_hits
++;
2879 static void get_ref_frame_flags(VP9_COMP
*cpi
) {
2880 if (cpi
->refresh_last_frame
& cpi
->refresh_golden_frame
)
2881 cpi
->gold_is_last
= 1;
2882 else if (cpi
->refresh_last_frame
^ cpi
->refresh_golden_frame
)
2883 cpi
->gold_is_last
= 0;
2885 if (cpi
->refresh_last_frame
& cpi
->refresh_alt_ref_frame
)
2886 cpi
->alt_is_last
= 1;
2887 else if (cpi
->refresh_last_frame
^ cpi
->refresh_alt_ref_frame
)
2888 cpi
->alt_is_last
= 0;
2890 if (cpi
->refresh_alt_ref_frame
& cpi
->refresh_golden_frame
)
2891 cpi
->gold_is_alt
= 1;
2892 else if (cpi
->refresh_alt_ref_frame
^ cpi
->refresh_golden_frame
)
2893 cpi
->gold_is_alt
= 0;
2895 cpi
->ref_frame_flags
= VP9_ALT_FLAG
| VP9_GOLD_FLAG
| VP9_LAST_FLAG
;
2897 if (cpi
->gold_is_last
)
2898 cpi
->ref_frame_flags
&= ~VP9_GOLD_FLAG
;
2900 if (cpi
->alt_is_last
)
2901 cpi
->ref_frame_flags
&= ~VP9_ALT_FLAG
;
2903 if (cpi
->gold_is_alt
)
2904 cpi
->ref_frame_flags
&= ~VP9_ALT_FLAG
;
2907 static void set_ext_overrides(VP9_COMP
*cpi
) {
2908 // Overrides the defaults with the externally supplied values with
2909 // vp9_update_reference() and vp9_update_entropy() calls
2910 // Note: The overrides are valid only for the next frame passed
2911 // to encode_frame_to_data_rate() function
2912 if (cpi
->ext_refresh_frame_context_pending
) {
2913 cpi
->common
.refresh_frame_context
= cpi
->ext_refresh_frame_context
;
2914 cpi
->ext_refresh_frame_context_pending
= 0;
2916 if (cpi
->ext_refresh_frame_flags_pending
) {
2917 cpi
->refresh_last_frame
= cpi
->ext_refresh_last_frame
;
2918 cpi
->refresh_golden_frame
= cpi
->ext_refresh_golden_frame
;
2919 cpi
->refresh_alt_ref_frame
= cpi
->ext_refresh_alt_ref_frame
;
2920 cpi
->ext_refresh_frame_flags_pending
= 0;
2924 static void encode_frame_to_data_rate(VP9_COMP
*cpi
,
2927 unsigned int *frame_flags
) {
2928 VP9_COMMON
*const cm
= &cpi
->common
;
2931 int frame_over_shoot_limit
;
2932 int frame_under_shoot_limit
;
2936 SPEED_FEATURES
*const sf
= &cpi
->sf
;
2937 unsigned int max_mv_def
= MIN(cpi
->common
.width
, cpi
->common
.height
);
2938 struct segmentation
*const seg
= &cm
->seg
;
2940 set_ext_overrides(cpi
);
2942 /* Scale the source buffer, if required. */
2943 if (cm
->mi_cols
* 8 != cpi
->un_scaled_source
->y_width
||
2944 cm
->mi_rows
* 8 != cpi
->un_scaled_source
->y_height
) {
2945 scale_and_extend_frame_nonnormative(cpi
->un_scaled_source
,
2946 &cpi
->scaled_source
);
2947 cpi
->Source
= &cpi
->scaled_source
;
2949 cpi
->Source
= cpi
->un_scaled_source
;
2951 scale_references(cpi
);
2953 // Clear down mmx registers to allow floating point in what follows.
2954 vp9_clear_system_state();
2956 // For an alt ref frame in 2 pass we skip the call to the second
2957 // pass function that sets the target bandwidth so we must set it here.
2958 if (cpi
->refresh_alt_ref_frame
) {
2959 // Set a per frame bit target for the alt ref frame.
2960 cpi
->rc
.per_frame_bandwidth
= cpi
->twopass
.gf_bits
;
2961 // Set a per second target bitrate.
2962 cpi
->target_bandwidth
= (int)(cpi
->twopass
.gf_bits
* cpi
->output_framerate
);
2965 // Clear zbin over-quant value and mode boost values.
2966 cpi
->zbin_mode_boost
= 0;
2968 // Enable or disable mode based tweaking of the zbin.
2969 // For 2 pass only used where GF/ARF prediction quality
2970 // is above a threshold.
2971 cpi
->zbin_mode_boost
= 0;
2972 cpi
->zbin_mode_boost_enabled
= 0;
2974 // Current default encoder behavior for the altref sign bias.
2975 cpi
->common
.ref_frame_sign_bias
[ALTREF_FRAME
] = cpi
->rc
.source_alt_ref_active
;
2977 // Set default state for segment based loop filter update flags.
2978 cm
->lf
.mode_ref_delta_update
= 0;
2980 // Initialize cpi->mv_step_param to default based on max resolution.
2981 cpi
->mv_step_param
= vp9_init_search_range(cpi
, max_mv_def
);
2982 // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate.
2983 if (sf
->auto_mv_step_size
) {
2984 if (frame_is_intra_only(&cpi
->common
)) {
2985 // Initialize max_mv_magnitude for use in the first INTER frame
2986 // after a key/intra-only frame.
2987 cpi
->max_mv_magnitude
= max_mv_def
;
2990 // Allow mv_steps to correspond to twice the max mv magnitude found
2991 // in the previous frame, capped by the default max_mv_magnitude based
2993 cpi
->mv_step_param
= vp9_init_search_range(
2994 cpi
, MIN(max_mv_def
, 2 * cpi
->max_mv_magnitude
));
2995 cpi
->max_mv_magnitude
= 0;
2999 // Set various flags etc to special state if it is a key frame.
3000 if (frame_is_intra_only(cm
)) {
3001 vp9_setup_key_frame(cpi
);
3002 // Reset the loop filter deltas and segmentation map.
3003 vp9_reset_segment_features(&cm
->seg
);
3005 // If segmentation is enabled force a map update for key frames.
3007 seg
->update_map
= 1;
3008 seg
->update_data
= 1;
3011 // The alternate reference frame cannot be active for a key frame.
3012 cpi
->rc
.source_alt_ref_active
= 0;
3014 cm
->error_resilient_mode
= (cpi
->oxcf
.error_resilient_mode
!= 0);
3015 cm
->frame_parallel_decoding_mode
=
3016 (cpi
->oxcf
.frame_parallel_decoding_mode
!= 0);
3017 if (cm
->error_resilient_mode
) {
3018 cm
->frame_parallel_decoding_mode
= 1;
3019 cm
->reset_frame_context
= 0;
3020 cm
->refresh_frame_context
= 0;
3021 } else if (cm
->intra_only
) {
3022 // Only reset the current context.
3023 cm
->reset_frame_context
= 2;
3027 // Configure experimental use of segmentation for enhanced coding of
3028 // static regions if indicated.
3029 // Only allowed in second pass of two pass (as requires lagged coding)
3030 // and if the relevant speed feature flag is set.
3031 if ((cpi
->pass
== 2) && (cpi
->sf
.static_segmentation
)) {
3032 configure_static_seg_features(cpi
);
3035 // For 1 pass CBR, check if we are dropping this frame.
3036 // Never drop on key frame.
3037 if (cpi
->pass
== 0 &&
3038 cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
&&
3039 cm
->frame_type
!= KEY_FRAME
) {
3040 if (vp9_drop_frame(cpi
)) {
3041 // Update buffer level with zero size, update frame counters, and return.
3042 vp9_update_buffer_level(cpi
, 0);
3043 cm
->last_frame_type
= cm
->frame_type
;
3044 vp9_rc_postencode_update_drop_frame(cpi
);
3045 cm
->current_video_frame
++;
3050 vp9_clear_system_state();
3052 vp9_zero(cpi
->rd_tx_select_threshes
);
3054 #if CONFIG_VP9_POSTPROC
3055 if (cpi
->oxcf
.noise_sensitivity
> 0) {
3057 switch (cpi
->oxcf
.noise_sensitivity
) {
3075 vp9_denoise(cpi
->Source
, cpi
->Source
, l
);
3079 #ifdef OUTPUT_YUV_SRC
3080 vp9_write_yuv_frame(cpi
->Source
);
3083 // Decide how big to make the frame.
3084 vp9_rc_pick_frame_size_target(cpi
);
3086 // Decide frame size bounds
3087 vp9_rc_compute_frame_size_bounds(cpi
, cpi
->rc
.this_frame_target
,
3088 &frame_under_shoot_limit
,
3089 &frame_over_shoot_limit
);
3091 // Decide q and q bounds
3092 q
= vp9_rc_pick_q_and_adjust_q_bounds(cpi
,
3096 if (!frame_is_intra_only(cm
)) {
3097 cm
->interp_filter
= DEFAULT_INTERP_FILTER
;
3098 /* TODO: Decide this more intelligently */
3099 set_high_precision_mv(cpi
, (q
< HIGH_PRECISION_MV_QTHRESH
));
3102 encode_with_recode_loop(cpi
,
3108 frame_over_shoot_limit
,
3109 frame_under_shoot_limit
);
3111 // Special case code to reduce pulsing when key frames are forced at a
3112 // fixed interval. Note the reconstruction error if it is the frame before
3113 // the force key frame
3114 if (cpi
->rc
.next_key_frame_forced
&& cpi
->rc
.frames_to_key
== 1) {
3115 cpi
->ambient_err
= vp9_calc_ss_err(cpi
->Source
, get_frame_new_buffer(cm
));
3118 // If the encoder forced a KEY_FRAME decision
3119 if (cm
->frame_type
== KEY_FRAME
)
3120 cpi
->refresh_last_frame
= 1;
3122 cm
->frame_to_show
= get_frame_new_buffer(cm
);
3124 #if WRITE_RECON_BUFFER
3126 write_cx_frame_to_file(cm
->frame_to_show
,
3127 cm
->current_video_frame
);
3129 write_cx_frame_to_file(cm
->frame_to_show
,
3130 cm
->current_video_frame
+ 1000);
3133 // Pick the loop filter level for the frame.
3134 loopfilter_frame(cpi
, cm
);
3136 #if WRITE_RECON_BUFFER
3138 write_cx_frame_to_file(cm
->frame_to_show
,
3139 cm
->current_video_frame
+ 2000);
3141 write_cx_frame_to_file(cm
->frame_to_show
,
3142 cm
->current_video_frame
+ 3000);
3145 // build the bitstream
3146 cpi
->dummy_packing
= 0;
3147 vp9_pack_bitstream(cpi
, dest
, size
);
3149 if (cm
->seg
.update_map
)
3150 update_reference_segmentation_map(cpi
);
3152 release_scaled_references(cpi
);
3153 update_reference_frames(cpi
);
3155 for (t
= TX_4X4
; t
<= TX_32X32
; t
++)
3156 full_to_model_counts(cpi
->common
.counts
.coef
[t
],
3157 cpi
->coef_counts
[t
]);
3158 if (!cpi
->common
.error_resilient_mode
&&
3159 !cpi
->common
.frame_parallel_decoding_mode
) {
3160 vp9_adapt_coef_probs(&cpi
->common
);
3163 if (!frame_is_intra_only(&cpi
->common
)) {
3164 if (!cpi
->common
.error_resilient_mode
&&
3165 !cpi
->common
.frame_parallel_decoding_mode
) {
3166 vp9_adapt_mode_probs(&cpi
->common
);
3167 vp9_adapt_mv_probs(&cpi
->common
, cpi
->common
.allow_high_precision_mv
);
3171 #ifdef ENTROPY_STATS
3172 vp9_update_mode_context_stats(cpi
);
3175 /* Move storing frame_type out of the above loop since it is also
3176 * needed in motion search besides loopfilter */
3177 cm
->last_frame_type
= cm
->frame_type
;
3180 output_frame_level_debug_stats(cpi
);
3182 if (cpi
->refresh_golden_frame
== 1)
3183 cm
->frame_flags
= cm
->frame_flags
| FRAMEFLAGS_GOLDEN
;
3185 cm
->frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_GOLDEN
;
3187 if (cpi
->refresh_alt_ref_frame
== 1)
3188 cm
->frame_flags
= cm
->frame_flags
| FRAMEFLAGS_ALTREF
;
3190 cm
->frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_ALTREF
;
3192 get_ref_frame_flags(cpi
);
3194 vp9_rc_postencode_update(cpi
, *size
);
3196 if (cm
->frame_type
== KEY_FRAME
) {
3197 // Tell the caller that the frame was coded as a key frame
3198 *frame_flags
= cm
->frame_flags
| FRAMEFLAGS_KEY
;
3200 #if CONFIG_MULTIPLE_ARF
3201 // Reset the sequence number.
3202 if (cpi
->multi_arf_enabled
) {
3203 cpi
->sequence_number
= 0;
3204 cpi
->frame_coding_order_period
= cpi
->new_frame_coding_order_period
;
3205 cpi
->new_frame_coding_order_period
= -1;
3209 *frame_flags
= cm
->frame_flags
&~FRAMEFLAGS_KEY
;
3211 #if CONFIG_MULTIPLE_ARF
3212 /* Increment position in the coded frame sequence. */
3213 if (cpi
->multi_arf_enabled
) {
3214 ++cpi
->sequence_number
;
3215 if (cpi
->sequence_number
>= cpi
->frame_coding_order_period
) {
3216 cpi
->sequence_number
= 0;
3217 cpi
->frame_coding_order_period
= cpi
->new_frame_coding_order_period
;
3218 cpi
->new_frame_coding_order_period
= -1;
3220 cpi
->this_frame_weight
= cpi
->arf_weight
[cpi
->sequence_number
];
3221 assert(cpi
->this_frame_weight
>= 0);
3226 // Clear the one shot update flags for segmentation map and mode/ref loop
3228 cm
->seg
.update_map
= 0;
3229 cm
->seg
.update_data
= 0;
3230 cm
->lf
.mode_ref_delta_update
= 0;
3232 // keep track of the last coded dimensions
3233 cm
->last_width
= cm
->width
;
3234 cm
->last_height
= cm
->height
;
3236 // reset to normal state now that we are done.
3237 cm
->last_show_frame
= cm
->show_frame
;
3238 if (cm
->show_frame
) {
3239 // current mip will be the prev_mip for the next frame
3240 MODE_INFO
*temp
= cm
->prev_mip
;
3241 MODE_INFO
**temp2
= cm
->prev_mi_grid_base
;
3242 cm
->prev_mip
= cm
->mip
;
3244 cm
->prev_mi_grid_base
= cm
->mi_grid_base
;
3245 cm
->mi_grid_base
= temp2
;
3247 // update the upper left visible macroblock ptrs
3248 cm
->mi
= cm
->mip
+ cm
->mode_info_stride
+ 1;
3249 cm
->mi_grid_visible
= cm
->mi_grid_base
+ cm
->mode_info_stride
+ 1;
3251 cpi
->mb
.e_mbd
.mi_8x8
= cm
->mi_grid_visible
;
3252 cpi
->mb
.e_mbd
.mi_8x8
[0] = cm
->mi
;
3254 // Don't increment frame counters if this was an altref buffer
3255 // update not a real frame
3256 ++cm
->current_video_frame
;
3259 cm
->prev_mi
= cm
->prev_mip
+ cm
->mode_info_stride
+ 1;
3260 cm
->prev_mi_grid_visible
= cm
->prev_mi_grid_base
+ cm
->mode_info_stride
+ 1;
3263 static void SvcEncode(VP9_COMP
*cpi
, size_t *size
, uint8_t *dest
,
3264 unsigned int *frame_flags
) {
3265 vp9_get_svc_params(cpi
);
3266 encode_frame_to_data_rate(cpi
, size
, dest
, frame_flags
);
3269 static void Pass0Encode(VP9_COMP
*cpi
, size_t *size
, uint8_t *dest
,
3270 unsigned int *frame_flags
) {
3271 if (cpi
->oxcf
.end_usage
== USAGE_STREAM_FROM_SERVER
) {
3272 vp9_get_one_pass_cbr_params(cpi
);
3274 vp9_get_one_pass_params(cpi
);
3276 encode_frame_to_data_rate(cpi
, size
, dest
, frame_flags
);
3279 static void Pass1Encode(VP9_COMP
*cpi
, size_t *size
, uint8_t *dest
,
3280 unsigned int *frame_flags
) {
3285 vp9_get_first_pass_params(cpi
);
3286 vp9_set_quantizer(cpi
, find_fp_qindex());
3287 vp9_first_pass(cpi
);
3290 static void Pass2Encode(VP9_COMP
*cpi
, size_t *size
,
3291 uint8_t *dest
, unsigned int *frame_flags
) {
3292 cpi
->enable_encode_breakout
= 1;
3294 vp9_get_second_pass_params(cpi
);
3295 encode_frame_to_data_rate(cpi
, size
, dest
, frame_flags
);
3296 // vp9_print_modes_and_motion_vectors(&cpi->common, "encode.stt");
3298 vp9_twopass_postencode_update(cpi
, *size
);
3301 static void check_initial_width(VP9_COMP
*cpi
, int subsampling_x
,
3302 int subsampling_y
) {
3303 VP9_COMMON
*const cm
= &cpi
->common
;
3304 if (!cpi
->initial_width
) {
3305 cm
->subsampling_x
= subsampling_x
;
3306 cm
->subsampling_y
= subsampling_y
;
3307 alloc_raw_frame_buffers(cpi
);
3308 cpi
->initial_width
= cm
->width
;
3309 cpi
->initial_height
= cm
->height
;
3314 int vp9_receive_raw_frame(VP9_PTR ptr
, unsigned int frame_flags
,
3315 YV12_BUFFER_CONFIG
*sd
, int64_t time_stamp
,
3317 VP9_COMP
*cpi
= (VP9_COMP
*) ptr
;
3318 VP9_COMMON
*cm
= &cpi
->common
;
3319 struct vpx_usec_timer timer
;
3321 const int subsampling_x
= sd
->uv_width
< sd
->y_width
;
3322 const int subsampling_y
= sd
->uv_height
< sd
->y_height
;
3324 check_initial_width(cpi
, subsampling_x
, subsampling_y
);
3325 vpx_usec_timer_start(&timer
);
3326 if (vp9_lookahead_push(cpi
->lookahead
, sd
, time_stamp
, end_time
, frame_flags
,
3327 cpi
->active_map_enabled
? cpi
->active_map
: NULL
))
3329 vpx_usec_timer_mark(&timer
);
3330 cpi
->time_receive_data
+= vpx_usec_timer_elapsed(&timer
);
3332 if (cm
->version
== 0 && (subsampling_x
!= 1 || subsampling_y
!= 1)) {
3333 vpx_internal_error(&cm
->error
, VPX_CODEC_INVALID_PARAM
,
3334 "Non-4:2:0 color space requires profile >= 1");
3342 static int frame_is_reference(const VP9_COMP
*cpi
) {
3343 const VP9_COMMON
*cm
= &cpi
->common
;
3345 return cm
->frame_type
== KEY_FRAME
||
3346 cpi
->refresh_last_frame
||
3347 cpi
->refresh_golden_frame
||
3348 cpi
->refresh_alt_ref_frame
||
3349 cm
->refresh_frame_context
||
3350 cm
->lf
.mode_ref_delta_update
||
3351 cm
->seg
.update_map
||
3352 cm
->seg
.update_data
;
3355 #if CONFIG_MULTIPLE_ARF
3356 int is_next_frame_arf(VP9_COMP
*cpi
) {
3357 // Negative entry in frame_coding_order indicates an ARF at this position.
3358 return cpi
->frame_coding_order
[cpi
->sequence_number
+ 1] < 0 ? 1 : 0;
3362 void adjust_frame_rate(VP9_COMP
*cpi
) {
3363 int64_t this_duration
;
3366 if (cpi
->source
->ts_start
== cpi
->first_time_stamp_ever
) {
3367 this_duration
= cpi
->source
->ts_end
- cpi
->source
->ts_start
;
3370 int64_t last_duration
= cpi
->last_end_time_stamp_seen
3371 - cpi
->last_time_stamp_seen
;
3373 this_duration
= cpi
->source
->ts_end
- cpi
->last_end_time_stamp_seen
;
3375 // do a step update if the duration changes by 10%
3377 step
= (int)((this_duration
- last_duration
) * 10 / last_duration
);
3380 if (this_duration
) {
3382 vp9_new_framerate(cpi
, 10000000.0 / this_duration
);
3384 // Average this frame's rate into the last second's average
3385 // frame rate. If we haven't seen 1 second yet, then average
3386 // over the whole interval seen.
3387 const double interval
= MIN((double)(cpi
->source
->ts_end
3388 - cpi
->first_time_stamp_ever
), 10000000.0);
3389 double avg_duration
= 10000000.0 / cpi
->oxcf
.framerate
;
3390 avg_duration
*= (interval
- avg_duration
+ this_duration
);
3391 avg_duration
/= interval
;
3393 vp9_new_framerate(cpi
, 10000000.0 / avg_duration
);
3396 cpi
->last_time_stamp_seen
= cpi
->source
->ts_start
;
3397 cpi
->last_end_time_stamp_seen
= cpi
->source
->ts_end
;
3400 int vp9_get_compressed_data(VP9_PTR ptr
, unsigned int *frame_flags
,
3401 size_t *size
, uint8_t *dest
,
3402 int64_t *time_stamp
, int64_t *time_end
, int flush
) {
3403 VP9_COMP
*cpi
= (VP9_COMP
*) ptr
;
3404 VP9_COMMON
*cm
= &cpi
->common
;
3405 struct vpx_usec_timer cmptimer
;
3406 YV12_BUFFER_CONFIG
*force_src_buffer
= NULL
;
3407 MV_REFERENCE_FRAME ref_frame
;
3412 vpx_usec_timer_start(&cmptimer
);
3416 set_high_precision_mv(cpi
, ALTREF_HIGH_PRECISION_MV
);
3419 cm
->reset_frame_context
= 0;
3420 cm
->refresh_frame_context
= 1;
3421 cpi
->refresh_last_frame
= 1;
3422 cpi
->refresh_golden_frame
= 0;
3423 cpi
->refresh_alt_ref_frame
= 0;
3425 // Should we code an alternate reference frame.
3426 if (cpi
->oxcf
.play_alternate
&& cpi
->rc
.source_alt_ref_pending
) {
3429 #if CONFIG_MULTIPLE_ARF
3430 assert(!cpi
->multi_arf_enabled
||
3431 cpi
->frame_coding_order
[cpi
->sequence_number
] < 0);
3433 if (cpi
->multi_arf_enabled
&& (cpi
->pass
== 2))
3434 frames_to_arf
= (-cpi
->frame_coding_order
[cpi
->sequence_number
])
3435 - cpi
->next_frame_in_order
;
3438 frames_to_arf
= cpi
->rc
.frames_till_gf_update_due
;
3440 assert(frames_to_arf
<= cpi
->rc
.frames_to_key
);
3442 if ((cpi
->source
= vp9_lookahead_peek(cpi
->lookahead
, frames_to_arf
))) {
3443 #if CONFIG_MULTIPLE_ARF
3444 cpi
->alt_ref_source
[cpi
->arf_buffered
] = cpi
->source
;
3446 cpi
->alt_ref_source
= cpi
->source
;
3449 if (cpi
->oxcf
.arnr_max_frames
> 0) {
3450 // Produce the filtered ARF frame.
3451 // TODO(agrange) merge these two functions.
3452 configure_arnr_filter(cpi
, cm
->current_video_frame
+ frames_to_arf
,
3454 vp9_temporal_filter_prepare(cpi
, frames_to_arf
);
3455 vp9_extend_frame_borders(&cpi
->alt_ref_buffer
,
3456 cm
->subsampling_x
, cm
->subsampling_y
);
3457 force_src_buffer
= &cpi
->alt_ref_buffer
;
3461 cpi
->refresh_alt_ref_frame
= 1;
3462 cpi
->refresh_golden_frame
= 0;
3463 cpi
->refresh_last_frame
= 0;
3464 cpi
->rc
.is_src_frame_alt_ref
= 0;
3466 #if CONFIG_MULTIPLE_ARF
3467 if (!cpi
->multi_arf_enabled
)
3469 cpi
->rc
.source_alt_ref_pending
= 0; // Clear Pending altf Ref flag.
3474 #if CONFIG_MULTIPLE_ARF
3477 if ((cpi
->source
= vp9_lookahead_pop(cpi
->lookahead
, flush
))) {
3481 #if CONFIG_MULTIPLE_ARF
3482 // Is this frame the ARF overlay.
3483 cpi
->rc
.is_src_frame_alt_ref
= 0;
3484 for (i
= 0; i
< cpi
->arf_buffered
; ++i
) {
3485 if (cpi
->source
== cpi
->alt_ref_source
[i
]) {
3486 cpi
->rc
.is_src_frame_alt_ref
= 1;
3487 cpi
->refresh_golden_frame
= 1;
3492 cpi
->rc
.is_src_frame_alt_ref
= cpi
->alt_ref_source
3493 && (cpi
->source
== cpi
->alt_ref_source
);
3495 if (cpi
->rc
.is_src_frame_alt_ref
) {
3496 // Current frame is an ARF overlay frame.
3497 #if CONFIG_MULTIPLE_ARF
3498 cpi
->alt_ref_source
[i
] = NULL
;
3500 cpi
->alt_ref_source
= NULL
;
3502 // Don't refresh the last buffer for an ARF overlay frame. It will
3503 // become the GF so preserve last as an alternative prediction option.
3504 cpi
->refresh_last_frame
= 0;
3506 #if CONFIG_MULTIPLE_ARF
3507 ++cpi
->next_frame_in_order
;
3513 cpi
->un_scaled_source
= cpi
->Source
= force_src_buffer
? force_src_buffer
3514 : &cpi
->source
->img
;
3515 *time_stamp
= cpi
->source
->ts_start
;
3516 *time_end
= cpi
->source
->ts_end
;
3517 *frame_flags
= cpi
->source
->flags
;
3519 #if CONFIG_MULTIPLE_ARF
3520 if ((cm
->frame_type
!= KEY_FRAME
) && (cpi
->pass
== 2))
3521 cpi
->rc
.source_alt_ref_pending
= is_next_frame_arf(cpi
);
3525 if (flush
&& cpi
->pass
== 1 && !cpi
->twopass
.first_pass_done
) {
3526 vp9_end_first_pass(cpi
); /* get last stats packet */
3527 cpi
->twopass
.first_pass_done
= 1;
3532 if (cpi
->source
->ts_start
< cpi
->first_time_stamp_ever
) {
3533 cpi
->first_time_stamp_ever
= cpi
->source
->ts_start
;
3534 cpi
->last_end_time_stamp_seen
= cpi
->source
->ts_start
;
3537 // adjust frame rates based on timestamps given
3538 if (cm
->show_frame
) {
3539 adjust_frame_rate(cpi
);
3542 // start with a 0 size frame
3545 // Clear down mmx registers
3546 vp9_clear_system_state(); // __asm emms;
3548 /* find a free buffer for the new frame, releasing the reference previously
3551 cm
->fb_idx_ref_cnt
[cm
->new_fb_idx
]--;
3552 cm
->new_fb_idx
= get_free_fb(cm
);
3554 #if CONFIG_MULTIPLE_ARF
3555 /* Set up the correct ARF frame. */
3556 if (cpi
->refresh_alt_ref_frame
) {
3557 ++cpi
->arf_buffered
;
3559 if (cpi
->multi_arf_enabled
&& (cm
->frame_type
!= KEY_FRAME
) &&
3561 cpi
->alt_fb_idx
= cpi
->arf_buffer_idx
[cpi
->sequence_number
];
3565 cm
->frame_flags
= *frame_flags
;
3567 // Reset the frame pointers to the current frame size
3568 vp9_realloc_frame_buffer(get_frame_new_buffer(cm
),
3569 cm
->width
, cm
->height
,
3570 cm
->subsampling_x
, cm
->subsampling_y
,
3571 VP9_ENC_BORDER_IN_PIXELS
);
3573 for (ref_frame
= LAST_FRAME
; ref_frame
<= ALTREF_FRAME
; ++ref_frame
) {
3574 const int idx
= cm
->ref_frame_map
[get_ref_frame_idx(cpi
, ref_frame
)];
3575 YV12_BUFFER_CONFIG
*const buf
= &cm
->yv12_fb
[idx
];
3577 RefBuffer
*const ref_buf
= &cm
->frame_refs
[ref_frame
- 1];
3580 vp9_setup_scale_factors_for_frame(&ref_buf
->sf
,
3581 buf
->y_crop_width
, buf
->y_crop_height
,
3582 cm
->width
, cm
->height
);
3584 if (vp9_is_scaled(&ref_buf
->sf
))
3585 vp9_extend_frame_borders(buf
, cm
->subsampling_x
, cm
->subsampling_y
);
3588 vp9_setup_interp_filters(&cpi
->mb
.e_mbd
, DEFAULT_INTERP_FILTER
, cm
);
3590 if (cpi
->oxcf
.aq_mode
== VARIANCE_AQ
) {
3595 SvcEncode(cpi
, size
, dest
, frame_flags
);
3596 } else if (cpi
->pass
== 1) {
3597 Pass1Encode(cpi
, size
, dest
, frame_flags
);
3598 } else if (cpi
->pass
== 2) {
3599 Pass2Encode(cpi
, size
, dest
, frame_flags
);
3602 Pass0Encode(cpi
, size
, dest
, frame_flags
);
3605 if (cm
->refresh_frame_context
)
3606 cm
->frame_contexts
[cm
->frame_context_idx
] = cm
->fc
;
3608 // Frame was dropped, release scaled references.
3610 release_scaled_references(cpi
);
3614 cpi
->droppable
= !frame_is_reference(cpi
);
3617 vpx_usec_timer_mark(&cmptimer
);
3618 cpi
->time_compress_data
+= vpx_usec_timer_elapsed(&cmptimer
);
3620 if (cpi
->b_calculate_psnr
&& cpi
->pass
!= 1 && cm
->show_frame
)
3621 generate_psnr_packet(cpi
);
3623 #if CONFIG_INTERNAL_STATS
3625 if (cpi
->pass
!= 1) {
3626 cpi
->bytes
+= *size
;
3628 if (cm
->show_frame
) {
3631 if (cpi
->b_calculate_psnr
) {
3632 YV12_BUFFER_CONFIG
*orig
= cpi
->Source
;
3633 YV12_BUFFER_CONFIG
*recon
= cpi
->common
.frame_to_show
;
3634 YV12_BUFFER_CONFIG
*pp
= &cm
->post_proc_buffer
;
3636 calc_psnr(orig
, recon
, &psnr
);
3638 cpi
->total
+= psnr
.psnr
[0];
3639 cpi
->total_y
+= psnr
.psnr
[1];
3640 cpi
->total_u
+= psnr
.psnr
[2];
3641 cpi
->total_v
+= psnr
.psnr
[3];
3642 cpi
->total_sq_error
+= psnr
.sse
[0];
3643 cpi
->total_samples
+= psnr
.samples
[0];
3647 double frame_ssim2
= 0, weight
= 0;
3648 #if CONFIG_VP9_POSTPROC
3649 vp9_deblock(cm
->frame_to_show
, &cm
->post_proc_buffer
,
3650 cm
->lf
.filter_level
* 10 / 6);
3652 vp9_clear_system_state();
3654 calc_psnr(orig
, pp
, &psnr2
);
3656 cpi
->totalp
+= psnr2
.psnr
[0];
3657 cpi
->totalp_y
+= psnr2
.psnr
[1];
3658 cpi
->totalp_u
+= psnr2
.psnr
[2];
3659 cpi
->totalp_v
+= psnr2
.psnr
[3];
3660 cpi
->totalp_sq_error
+= psnr2
.sse
[0];
3661 cpi
->totalp_samples
+= psnr2
.samples
[0];
3663 frame_ssim2
= vp9_calc_ssim(orig
, recon
, 1, &weight
);
3665 cpi
->summed_quality
+= frame_ssim2
* weight
;
3666 cpi
->summed_weights
+= weight
;
3668 frame_ssim2
= vp9_calc_ssim(orig
, &cm
->post_proc_buffer
, 1, &weight
);
3670 cpi
->summedp_quality
+= frame_ssim2
* weight
;
3671 cpi
->summedp_weights
+= weight
;
3674 FILE *f
= fopen("q_used.stt", "a");
3675 fprintf(f
, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3676 cpi
->common
.current_video_frame
, y2
, u2
, v2
,
3677 frame_psnr2
, frame_ssim2
);
3684 if (cpi
->b_calculate_ssimg
) {
3685 double y
, u
, v
, frame_all
;
3686 frame_all
= vp9_calc_ssimg(cpi
->Source
, cm
->frame_to_show
, &y
, &u
, &v
);
3687 cpi
->total_ssimg_y
+= y
;
3688 cpi
->total_ssimg_u
+= u
;
3689 cpi
->total_ssimg_v
+= v
;
3690 cpi
->total_ssimg_all
+= frame_all
;
3699 int vp9_get_preview_raw_frame(VP9_PTR comp
, YV12_BUFFER_CONFIG
*dest
,
3700 vp9_ppflags_t
*flags
) {
3701 VP9_COMP
*cpi
= (VP9_COMP
*) comp
;
3703 if (!cpi
->common
.show_frame
) {
3707 #if CONFIG_VP9_POSTPROC
3708 ret
= vp9_post_proc_frame(&cpi
->common
, dest
, flags
);
3711 if (cpi
->common
.frame_to_show
) {
3712 *dest
= *cpi
->common
.frame_to_show
;
3713 dest
->y_width
= cpi
->common
.width
;
3714 dest
->y_height
= cpi
->common
.height
;
3715 dest
->uv_width
= cpi
->common
.width
>> cpi
->common
.subsampling_x
;
3716 dest
->uv_height
= cpi
->common
.height
>> cpi
->common
.subsampling_y
;
3722 #endif // !CONFIG_VP9_POSTPROC
3723 vp9_clear_system_state();
3728 int vp9_set_roimap(VP9_PTR comp
, unsigned char *map
, unsigned int rows
,
3729 unsigned int cols
, int delta_q
[MAX_SEGMENTS
],
3730 int delta_lf
[MAX_SEGMENTS
],
3731 unsigned int threshold
[MAX_SEGMENTS
]) {
3732 VP9_COMP
*cpi
= (VP9_COMP
*) comp
;
3733 signed char feature_data
[SEG_LVL_MAX
][MAX_SEGMENTS
];
3734 struct segmentation
*seg
= &cpi
->common
.seg
;
3737 if (cpi
->common
.mb_rows
!= rows
|| cpi
->common
.mb_cols
!= cols
)
3741 vp9_disable_segmentation((VP9_PTR
)cpi
);
3745 // Set the segmentation Map
3746 vp9_set_segmentation_map((VP9_PTR
)cpi
, map
);
3748 // Activate segmentation.
3749 vp9_enable_segmentation((VP9_PTR
)cpi
);
3751 // Set up the quant, LF and breakout threshold segment data
3752 for (i
= 0; i
< MAX_SEGMENTS
; i
++) {
3753 feature_data
[SEG_LVL_ALT_Q
][i
] = delta_q
[i
];
3754 feature_data
[SEG_LVL_ALT_LF
][i
] = delta_lf
[i
];
3755 cpi
->segment_encode_breakout
[i
] = threshold
[i
];
3758 // Enable the loop and quant changes in the feature mask
3759 for (i
= 0; i
< MAX_SEGMENTS
; i
++) {
3761 vp9_enable_segfeature(seg
, i
, SEG_LVL_ALT_Q
);
3763 vp9_disable_segfeature(seg
, i
, SEG_LVL_ALT_Q
);
3766 vp9_enable_segfeature(seg
, i
, SEG_LVL_ALT_LF
);
3768 vp9_disable_segfeature(seg
, i
, SEG_LVL_ALT_LF
);
3771 // Initialize the feature data structure
3772 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
3773 vp9_set_segment_data((VP9_PTR
)cpi
, &feature_data
[0][0], SEGMENT_DELTADATA
);
3778 int vp9_set_active_map(VP9_PTR comp
, unsigned char *map
,
3779 unsigned int rows
, unsigned int cols
) {
3780 VP9_COMP
*cpi
= (VP9_COMP
*) comp
;
3782 if (rows
== cpi
->common
.mb_rows
&& cols
== cpi
->common
.mb_cols
) {
3784 vpx_memcpy(cpi
->active_map
, map
, rows
* cols
);
3785 cpi
->active_map_enabled
= 1;
3787 cpi
->active_map_enabled
= 0;
3792 // cpi->active_map_enabled = 0;
3797 int vp9_set_internal_size(VP9_PTR comp
,
3798 VPX_SCALING horiz_mode
, VPX_SCALING vert_mode
) {
3799 VP9_COMP
*cpi
= (VP9_COMP
*) comp
;
3800 VP9_COMMON
*cm
= &cpi
->common
;
3801 int hr
= 0, hs
= 0, vr
= 0, vs
= 0;
3803 if (horiz_mode
> ONETWO
|| vert_mode
> ONETWO
)
3806 Scale2Ratio(horiz_mode
, &hr
, &hs
);
3807 Scale2Ratio(vert_mode
, &vr
, &vs
);
3809 // always go to the next whole number
3810 cm
->width
= (hs
- 1 + cpi
->oxcf
.width
* hr
) / hs
;
3811 cm
->height
= (vs
- 1 + cpi
->oxcf
.height
* vr
) / vs
;
3813 assert(cm
->width
<= cpi
->initial_width
);
3814 assert(cm
->height
<= cpi
->initial_height
);
3815 update_frame_size(cpi
);
3819 int vp9_set_size_literal(VP9_PTR comp
, unsigned int width
,
3820 unsigned int height
) {
3821 VP9_COMP
*cpi
= (VP9_COMP
*)comp
;
3822 VP9_COMMON
*cm
= &cpi
->common
;
3824 check_initial_width(cpi
, 1, 1);
3828 if (cm
->width
* 5 < cpi
->initial_width
) {
3829 cm
->width
= cpi
->initial_width
/ 5 + 1;
3830 printf("Warning: Desired width too small, changed to %d \n", cm
->width
);
3832 if (cm
->width
> cpi
->initial_width
) {
3833 cm
->width
= cpi
->initial_width
;
3834 printf("Warning: Desired width too large, changed to %d \n", cm
->width
);
3839 cm
->height
= height
;
3840 if (cm
->height
* 5 < cpi
->initial_height
) {
3841 cm
->height
= cpi
->initial_height
/ 5 + 1;
3842 printf("Warning: Desired height too small, changed to %d \n", cm
->height
);
3844 if (cm
->height
> cpi
->initial_height
) {
3845 cm
->height
= cpi
->initial_height
;
3846 printf("Warning: Desired height too large, changed to %d \n", cm
->height
);
3850 assert(cm
->width
<= cpi
->initial_width
);
3851 assert(cm
->height
<= cpi
->initial_height
);
3852 update_frame_size(cpi
);
3856 void vp9_set_svc(VP9_PTR comp
, int use_svc
) {
3857 VP9_COMP
*cpi
= (VP9_COMP
*)comp
;
3858 cpi
->use_svc
= use_svc
;
3862 int vp9_calc_ss_err(const YV12_BUFFER_CONFIG
*source
,
3863 const YV12_BUFFER_CONFIG
*reference
) {
3867 const uint8_t *src
= source
->y_buffer
;
3868 const uint8_t *ref
= reference
->y_buffer
;
3870 // Loop through the Y plane raw and reconstruction data summing
3871 // (square differences)
3872 for (i
= 0; i
< source
->y_height
; i
+= 16) {
3873 for (j
= 0; j
< source
->y_width
; j
+= 16) {
3875 total
+= vp9_mse16x16(src
+ j
, source
->y_stride
,
3876 ref
+ j
, reference
->y_stride
, &sse
);
3879 src
+= 16 * source
->y_stride
;
3880 ref
+= 16 * reference
->y_stride
;
3887 int vp9_get_quantizer(VP9_PTR c
) {
3888 return ((VP9_COMP
*)c
)->common
.base_qindex
;