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
96 void vp9_init_quantizer(VP9_COMP *cpi);
98 static const double in_frame_q_adj_ratio[MAX_SEGMENTS] =
99 {1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
101 static INLINE void Scale2Ratio(int mode, int *hr, int *hs) {
127 static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
128 MACROBLOCK *const mb = &cpi->mb;
129 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
130 if (cpi->common.allow_high_precision_mv) {
131 mb->mvcost = mb->nmvcost_hp;
132 mb->mvsadcost = mb->nmvsadcost_hp;
134 mb->mvcost = mb->nmvcost;
135 mb->mvsadcost = mb->nmvsadcost;
139 void vp9_initialize_enc() {
140 static int init_done = 0;
143 vp9_initialize_common();
144 vp9_coef_tree_initialize();
145 vp9_tokenize_initialize();
146 vp9_init_quant_tables();
148 vp9_rc_init_minq_luts();
149 // init_base_skip_probs();
150 vp9_entropy_mv_init();
151 vp9_entropy_mode_init();
156 static void dealloc_compressor_data(VP9_COMP *cpi) {
157 // Delete sementation map
158 vpx_free(cpi->segmentation_map);
159 cpi->segmentation_map = 0;
160 vpx_free(cpi->common.last_frame_seg_map);
161 cpi->common.last_frame_seg_map = 0;
162 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
163 cpi->coding_context.last_frame_seg_map_copy = 0;
165 vpx_free(cpi->complexity_map);
166 cpi->complexity_map = 0;
167 vpx_free(cpi->active_map);
170 vp9_free_frame_buffers(&cpi->common);
172 vp9_free_frame_buffer(&cpi->last_frame_uf);
173 vp9_free_frame_buffer(&cpi->scaled_source);
174 vp9_free_frame_buffer(&cpi->alt_ref_buffer);
175 vp9_lookahead_destroy(cpi->lookahead);
180 // Activity mask based per mb zbin adjustments
181 vpx_free(cpi->mb_activity_map);
182 cpi->mb_activity_map = 0;
183 vpx_free(cpi->mb_norm_activity_map);
184 cpi->mb_norm_activity_map = 0;
186 vpx_free(cpi->above_context[0]);
187 cpi->above_context[0] = NULL;
189 vpx_free(cpi->above_seg_context);
190 cpi->above_seg_context = NULL;
193 // Computes a q delta (in "q index" terms) to get from a starting q value
196 int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget) {
198 int start_index = cpi->rc.worst_quality;
199 int target_index = cpi->rc.worst_quality;
201 // Convert the average q value to an index.
202 for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
204 if (vp9_convert_qindex_to_q(i) >= qstart)
208 // Convert the q target to an index
209 for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
211 if (vp9_convert_qindex_to_q(i) >= qtarget)
215 return target_index - start_index;
218 // Computes a q delta (in "q index" terms) to get from a starting q value
219 // to a value that should equate to thegiven rate ratio.
221 int vp9_compute_qdelta_by_rate(VP9_COMP *cpi,
222 double base_q_index, double rate_target_ratio) {
224 int base_bits_per_mb;
225 int target_bits_per_mb;
226 int target_index = cpi->rc.worst_quality;
228 // Make SURE use of floating point in this function is safe.
229 vp9_clear_system_state();
231 // Look up the current projected bits per block for the base index
232 base_bits_per_mb = vp9_rc_bits_per_mb(cpi->common.frame_type,
235 // Find the target bits per mb based on the base value and given ratio.
236 target_bits_per_mb = rate_target_ratio * base_bits_per_mb;
238 // Convert the q target to an index
239 for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
241 if (vp9_rc_bits_per_mb(cpi->common.frame_type,
242 i, 1.0) <= target_bits_per_mb )
246 return target_index - base_q_index;
249 // This function sets up a set of segments with delta Q values around
250 // the baseline frame quantizer.
251 static void setup_in_frame_q_adj(VP9_COMP *cpi) {
252 VP9_COMMON *cm = &cpi->common;
253 struct segmentation *seg = &cm->seg;
258 // Make SURE use of floating point in this function is safe.
259 vp9_clear_system_state();
261 if (cm->frame_type == KEY_FRAME ||
262 cpi->refresh_alt_ref_frame ||
263 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
264 // Clear down the segment map
265 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
267 // Clear down the complexity map used for rd
268 vpx_memset(cpi->complexity_map, 0, cm->mi_rows * cm->mi_cols);
270 vp9_enable_segmentation((VP9_PTR)cpi);
271 vp9_clearall_segfeatures(seg);
273 // Select delta coding method
274 seg->abs_delta = SEGMENT_DELTADATA;
276 // Segment 0 "Q" feature is disabled so it defaults to the baseline Q
277 vp9_disable_segfeature(seg, 0, SEG_LVL_ALT_Q);
279 // Use some of the segments for in frame Q adjustment
280 for (segment = 1; segment < 2; segment++) {
282 vp9_compute_qdelta_by_rate(cpi, cm->base_qindex,
283 in_frame_q_adj_ratio[segment]);
284 vp9_enable_segfeature(seg, segment, SEG_LVL_ALT_Q);
285 vp9_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta);
289 static void configure_static_seg_features(VP9_COMP *cpi) {
290 VP9_COMMON *cm = &cpi->common;
291 struct segmentation *seg = &cm->seg;
293 int high_q = (int)(cpi->rc.avg_q > 48.0);
296 // Disable and clear down for KF
297 if (cm->frame_type == KEY_FRAME) {
298 // Clear down the global segmentation map
299 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
301 seg->update_data = 0;
302 cpi->static_mb_pct = 0;
304 // Disable segmentation
305 vp9_disable_segmentation((VP9_PTR)cpi);
307 // Clear down the segment features.
308 vp9_clearall_segfeatures(seg);
309 } else if (cpi->refresh_alt_ref_frame) {
310 // If this is an alt ref frame
311 // Clear down the global segmentation map
312 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
314 seg->update_data = 0;
315 cpi->static_mb_pct = 0;
317 // Disable segmentation and individual segment features by default
318 vp9_disable_segmentation((VP9_PTR)cpi);
319 vp9_clearall_segfeatures(seg);
321 // Scan frames from current to arf frame.
322 // This function re-enables segmentation if appropriate.
323 vp9_update_mbgraph_stats(cpi);
325 // If segmentation was enabled set those features needed for the
329 seg->update_data = 1;
331 qi_delta = vp9_compute_qdelta(
332 cpi, cpi->rc.avg_q, (cpi->rc.avg_q * 0.875));
333 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2));
334 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
336 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
337 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
339 // Where relevant assume segment data is delta data
340 seg->abs_delta = SEGMENT_DELTADATA;
342 } else if (seg->enabled) {
343 // All other frames if segmentation has been enabled
345 // First normal frame in a valid gf or alt ref group
346 if (cpi->rc.frames_since_golden == 0) {
347 // Set up segment features for normal frames in an arf group
348 if (cpi->rc.source_alt_ref_active) {
350 seg->update_data = 1;
351 seg->abs_delta = SEGMENT_DELTADATA;
353 qi_delta = vp9_compute_qdelta(cpi, cpi->rc.avg_q,
354 (cpi->rc.avg_q * 1.125));
355 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
356 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
358 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
359 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
361 // Segment coding disabled for compred testing
362 if (high_q || (cpi->static_mb_pct == 100)) {
363 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
364 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
365 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
368 // Disable segmentation and clear down features if alt ref
369 // is not active for this group
371 vp9_disable_segmentation((VP9_PTR)cpi);
373 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
376 seg->update_data = 0;
378 vp9_clearall_segfeatures(seg);
380 } else if (cpi->rc.is_src_frame_alt_ref) {
381 // Special case where we are coding over the top of a previous
383 // Segment coding disabled for compred testing
385 // Enable ref frame features for segment 0 as well
386 vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
387 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
389 // All mbs should use ALTREF_FRAME
390 vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
391 vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
392 vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
393 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
395 // Skip all MBs if high Q (0,0 mv and skip coeffs)
397 vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
398 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
400 // Enable data update
401 seg->update_data = 1;
405 // No updates.. leave things as they are.
407 seg->update_data = 0;
412 // DEBUG: Print out the segment id of each MB in the current frame.
413 static void print_seg_map(VP9_COMP *cpi) {
414 VP9_COMMON *cm = &cpi->common;
417 FILE *statsfile = fopen("segmap.stt", "a");
419 fprintf(statsfile, "%10d\n", cm->current_video_frame);
421 for (row = 0; row < cpi->common.mi_rows; row++) {
422 for (col = 0; col < cpi->common.mi_cols; col++) {
423 fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
426 fprintf(statsfile, "\n");
428 fprintf(statsfile, "\n");
433 static void update_reference_segmentation_map(VP9_COMP *cpi) {
434 VP9_COMMON *const cm = &cpi->common;
436 MODE_INFO **mi_8x8, **mi_8x8_ptr = cm->mi_grid_visible;
437 uint8_t *cache_ptr = cm->last_frame_seg_map, *cache;
439 for (row = 0; row < cm->mi_rows; row++) {
442 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
443 cache[0] = mi_8x8[0]->mbmi.segment_id;
444 mi_8x8_ptr += cm->mode_info_stride;
445 cache_ptr += cm->mi_cols;
448 static int is_slowest_mode(int mode) {
449 return (mode == MODE_SECONDPASS_BEST || mode == MODE_BESTQUALITY);
452 static void set_rd_speed_thresholds(VP9_COMP *cpi) {
453 SPEED_FEATURES *sf = &cpi->sf;
456 // Set baseline threshold values
457 for (i = 0; i < MAX_MODES; ++i)
458 sf->thresh_mult[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0;
460 sf->thresh_mult[THR_NEARESTMV] = 0;
461 sf->thresh_mult[THR_NEARESTG] = 0;
462 sf->thresh_mult[THR_NEARESTA] = 0;
464 sf->thresh_mult[THR_DC] += 1000;
466 sf->thresh_mult[THR_NEWMV] += 1000;
467 sf->thresh_mult[THR_NEWA] += 1000;
468 sf->thresh_mult[THR_NEWG] += 1000;
470 sf->thresh_mult[THR_NEARMV] += 1000;
471 sf->thresh_mult[THR_NEARA] += 1000;
472 sf->thresh_mult[THR_COMP_NEARESTLA] += 1000;
473 sf->thresh_mult[THR_COMP_NEARESTGA] += 1000;
475 sf->thresh_mult[THR_TM] += 1000;
477 sf->thresh_mult[THR_COMP_NEARLA] += 1500;
478 sf->thresh_mult[THR_COMP_NEWLA] += 2000;
479 sf->thresh_mult[THR_NEARG] += 1000;
480 sf->thresh_mult[THR_COMP_NEARGA] += 1500;
481 sf->thresh_mult[THR_COMP_NEWGA] += 2000;
483 sf->thresh_mult[THR_ZEROMV] += 2000;
484 sf->thresh_mult[THR_ZEROG] += 2000;
485 sf->thresh_mult[THR_ZEROA] += 2000;
486 sf->thresh_mult[THR_COMP_ZEROLA] += 2500;
487 sf->thresh_mult[THR_COMP_ZEROGA] += 2500;
489 sf->thresh_mult[THR_H_PRED] += 2000;
490 sf->thresh_mult[THR_V_PRED] += 2000;
491 sf->thresh_mult[THR_D45_PRED ] += 2500;
492 sf->thresh_mult[THR_D135_PRED] += 2500;
493 sf->thresh_mult[THR_D117_PRED] += 2500;
494 sf->thresh_mult[THR_D153_PRED] += 2500;
495 sf->thresh_mult[THR_D207_PRED] += 2500;
496 sf->thresh_mult[THR_D63_PRED] += 2500;
498 /* disable frame modes if flags not set */
499 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
500 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
501 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
502 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
503 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
505 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
506 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
507 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
508 sf->thresh_mult[THR_NEARG ] = INT_MAX;
509 sf->thresh_mult[THR_NEWG ] = INT_MAX;
511 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
512 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
513 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
514 sf->thresh_mult[THR_NEARA ] = INT_MAX;
515 sf->thresh_mult[THR_NEWA ] = INT_MAX;
518 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
519 (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
520 sf->thresh_mult[THR_COMP_ZEROLA ] = INT_MAX;
521 sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
522 sf->thresh_mult[THR_COMP_NEARLA ] = INT_MAX;
523 sf->thresh_mult[THR_COMP_NEWLA ] = INT_MAX;
525 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
526 (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
527 sf->thresh_mult[THR_COMP_ZEROGA ] = INT_MAX;
528 sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
529 sf->thresh_mult[THR_COMP_NEARGA ] = INT_MAX;
530 sf->thresh_mult[THR_COMP_NEWGA ] = INT_MAX;
534 static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi) {
535 SPEED_FEATURES *sf = &cpi->sf;
538 for (i = 0; i < MAX_REFS; ++i)
539 sf->thresh_mult_sub8x8[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0;
541 sf->thresh_mult_sub8x8[THR_LAST] += 2500;
542 sf->thresh_mult_sub8x8[THR_GOLD] += 2500;
543 sf->thresh_mult_sub8x8[THR_ALTR] += 2500;
544 sf->thresh_mult_sub8x8[THR_INTRA] += 2500;
545 sf->thresh_mult_sub8x8[THR_COMP_LA] += 4500;
546 sf->thresh_mult_sub8x8[THR_COMP_GA] += 4500;
548 // Check for masked out split cases.
549 for (i = 0; i < MAX_REFS; i++) {
550 if (sf->disable_split_mask & (1 << i))
551 sf->thresh_mult_sub8x8[i] = INT_MAX;
554 // disable mode test if frame flag is not set
555 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
556 sf->thresh_mult_sub8x8[THR_LAST] = INT_MAX;
557 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
558 sf->thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
559 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
560 sf->thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
561 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
562 (VP9_LAST_FLAG | VP9_ALT_FLAG))
563 sf->thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
564 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
565 (VP9_GOLD_FLAG | VP9_ALT_FLAG))
566 sf->thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
569 static void set_good_speed_feature(VP9_COMMON *cm,
573 sf->adaptive_rd_thresh = 1;
574 sf->recode_loop = ((speed < 1) ? ALLOW_RECODE : ALLOW_RECODE_KFMAXBW);
576 sf->use_square_partition_only = !frame_is_intra_only(cm);
577 sf->less_rectangular_check = 1;
578 sf->tx_size_search_method = frame_is_intra_only(cm)
579 ? USE_FULL_RD : USE_LARGESTALL;
581 if (MIN(cm->width, cm->height) >= 720)
582 sf->disable_split_mask = cm->show_frame ?
583 DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
585 sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
587 sf->use_rd_breakout = 1;
588 sf->adaptive_motion_search = 1;
589 sf->adaptive_pred_interp_filter = 1;
590 sf->auto_mv_step_size = 1;
591 sf->adaptive_rd_thresh = 2;
592 sf->recode_loop = ALLOW_RECODE_KFARFGF;
593 sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
594 sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
595 sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
598 sf->use_square_partition_only = !frame_is_intra_only(cm);
599 sf->less_rectangular_check = 1;
600 sf->tx_size_search_method = frame_is_intra_only(cm)
601 ? USE_FULL_RD : USE_LARGESTALL;
603 if (MIN(cm->width, cm->height) >= 720)
604 sf->disable_split_mask = cm->show_frame ?
605 DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
607 sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
609 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
610 FLAG_SKIP_INTRA_BESTINTER |
611 FLAG_SKIP_COMP_BESTINTRA |
612 FLAG_SKIP_INTRA_LOWVAR;
613 sf->use_rd_breakout = 1;
614 sf->adaptive_motion_search = 1;
615 sf->adaptive_pred_interp_filter = 2;
616 sf->reference_masking = 1;
617 sf->auto_mv_step_size = 1;
619 sf->disable_filter_search_var_thresh = 50;
620 sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
622 sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
623 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
624 sf->adjust_partitioning_from_last_frame = 1;
625 sf->last_partitioning_redo_frequency = 3;
627 sf->adaptive_rd_thresh = 2;
628 sf->recode_loop = ALLOW_RECODE_KFARFGF;
629 sf->use_lp32x32fdct = 1;
630 sf->mode_skip_start = 11;
631 sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
632 sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
633 sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
634 sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
637 sf->use_square_partition_only = 1;
638 sf->tx_size_search_method = USE_LARGESTALL;
640 if (MIN(cm->width, cm->height) >= 720)
641 sf->disable_split_mask = DISABLE_ALL_SPLIT;
643 sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
645 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
646 FLAG_SKIP_INTRA_BESTINTER |
647 FLAG_SKIP_COMP_BESTINTRA |
648 FLAG_SKIP_INTRA_LOWVAR;
650 sf->use_rd_breakout = 1;
651 sf->adaptive_motion_search = 1;
652 sf->adaptive_pred_interp_filter = 2;
653 sf->reference_masking = 1;
654 sf->auto_mv_step_size = 1;
656 sf->disable_filter_search_var_thresh = 100;
657 sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
659 sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
660 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
661 sf->adjust_partitioning_from_last_frame = 1;
662 sf->last_partitioning_redo_frequency = 3;
664 sf->use_uv_intra_rd_estimate = 1;
665 sf->skip_encode_sb = 1;
666 sf->use_lp32x32fdct = 1;
667 sf->subpel_iters_per_step = 1;
668 sf->use_fast_coef_updates = 2;
670 sf->adaptive_rd_thresh = 4;
671 sf->mode_skip_start = 6;
674 sf->use_square_partition_only = 1;
675 sf->tx_size_search_method = USE_LARGESTALL;
676 sf->disable_split_mask = DISABLE_ALL_SPLIT;
678 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
679 FLAG_SKIP_INTRA_BESTINTER |
680 FLAG_SKIP_COMP_BESTINTRA |
681 FLAG_SKIP_COMP_REFMISMATCH |
682 FLAG_SKIP_INTRA_LOWVAR |
683 FLAG_EARLY_TERMINATE;
685 sf->use_rd_breakout = 1;
686 sf->adaptive_motion_search = 1;
687 sf->adaptive_pred_interp_filter = 2;
688 sf->reference_masking = 1;
689 sf->auto_mv_step_size = 1;
691 sf->disable_filter_search_var_thresh = 200;
692 sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
694 sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
695 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
696 sf->adjust_partitioning_from_last_frame = 1;
697 sf->last_partitioning_redo_frequency = 3;
699 sf->use_uv_intra_rd_estimate = 1;
700 sf->skip_encode_sb = 1;
701 sf->use_lp32x32fdct = 1;
702 sf->subpel_iters_per_step = 1;
703 sf->use_fast_coef_updates = 2;
705 sf->adaptive_rd_thresh = 4;
706 sf->mode_skip_start = 6;
709 sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
710 sf->use_one_partition_size_always = 1;
711 sf->always_this_block_size = BLOCK_16X16;
712 sf->tx_size_search_method = frame_is_intra_only(cm) ?
713 USE_FULL_RD : USE_LARGESTALL;
714 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
715 FLAG_SKIP_INTRA_BESTINTER |
716 FLAG_SKIP_COMP_BESTINTRA |
717 FLAG_SKIP_COMP_REFMISMATCH |
718 FLAG_SKIP_INTRA_LOWVAR |
719 FLAG_EARLY_TERMINATE;
720 sf->use_rd_breakout = 1;
721 sf->use_lp32x32fdct = 1;
722 sf->optimize_coefficients = 0;
723 sf->auto_mv_step_size = 1;
724 sf->reference_masking = 1;
726 sf->disable_split_mask = DISABLE_ALL_SPLIT;
727 sf->search_method = HEX;
728 sf->subpel_iters_per_step = 1;
729 sf->disable_split_var_thresh = 64;
730 sf->disable_filter_search_var_thresh = 500;
731 for (i = 0; i < TX_SIZES; i++) {
732 sf->intra_y_mode_mask[i] = INTRA_DC_ONLY;
733 sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
735 sf->use_fast_coef_updates = 2;
736 sf->adaptive_rd_thresh = 4;
737 sf->mode_skip_start = 6;
740 static void set_rt_speed_feature(VP9_COMMON *cm,
743 sf->static_segmentation = 0;
744 sf->adaptive_rd_thresh = 1;
745 sf->recode_loop = ((speed < 1) ? ALLOW_RECODE : ALLOW_RECODE_KFMAXBW);
746 sf->encode_breakout_thresh = 1;
749 sf->use_square_partition_only = !frame_is_intra_only(cm);
750 sf->less_rectangular_check = 1;
751 sf->tx_size_search_method =
752 frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
754 if (MIN(cm->width, cm->height) >= 720)
755 sf->disable_split_mask = cm->show_frame ?
756 DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
758 sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
760 sf->use_rd_breakout = 1;
761 sf->adaptive_motion_search = 1;
762 sf->adaptive_pred_interp_filter = 1;
763 sf->auto_mv_step_size = 1;
764 sf->adaptive_rd_thresh = 2;
765 sf->recode_loop = ALLOW_RECODE_KFARFGF;
766 sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
767 sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
768 sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
769 sf->encode_breakout_thresh = 8;
772 sf->use_square_partition_only = !frame_is_intra_only(cm);
773 sf->less_rectangular_check = 1;
774 sf->tx_size_search_method =
775 frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
777 if (MIN(cm->width, cm->height) >= 720)
778 sf->disable_split_mask = cm->show_frame ?
779 DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
781 sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
783 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH
784 | FLAG_SKIP_INTRA_BESTINTER | FLAG_SKIP_COMP_BESTINTRA
785 | FLAG_SKIP_INTRA_LOWVAR;
787 sf->use_rd_breakout = 1;
788 sf->adaptive_motion_search = 1;
789 sf->adaptive_pred_interp_filter = 2;
790 sf->auto_mv_step_size = 1;
791 sf->reference_masking = 1;
793 sf->disable_filter_search_var_thresh = 50;
794 sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
796 sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
797 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
798 sf->adjust_partitioning_from_last_frame = 1;
799 sf->last_partitioning_redo_frequency = 3;
801 sf->adaptive_rd_thresh = 2;
802 sf->recode_loop = ALLOW_RECODE_KFARFGF;
803 sf->use_lp32x32fdct = 1;
804 sf->mode_skip_start = 11;
805 sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
806 sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
807 sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
808 sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
809 sf->encode_breakout_thresh = 200;
812 sf->use_square_partition_only = 1;
813 sf->tx_size_search_method = USE_LARGESTALL;
815 if (MIN(cm->width, cm->height) >= 720)
816 sf->disable_split_mask = DISABLE_ALL_SPLIT;
818 sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
820 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH
821 | FLAG_SKIP_INTRA_BESTINTER | FLAG_SKIP_COMP_BESTINTRA
822 | FLAG_SKIP_INTRA_LOWVAR;
824 sf->disable_filter_search_var_thresh = 100;
825 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
826 sf->use_uv_intra_rd_estimate = 1;
827 sf->skip_encode_sb = 1;
828 sf->subpel_iters_per_step = 1;
829 sf->use_fast_coef_updates = 2;
830 sf->adaptive_rd_thresh = 4;
831 sf->mode_skip_start = 6;
832 sf->encode_breakout_thresh = 400;
835 sf->optimize_coefficients = 0;
836 sf->disable_split_mask = DISABLE_ALL_SPLIT;
837 sf->use_fast_lpf_pick = 2;
838 sf->encode_breakout_thresh = 700;
842 sf->adaptive_rd_thresh = 5;
843 sf->auto_min_max_partition_size = frame_is_intra_only(cm) ?
844 RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
845 sf->subpel_force_stop = 1;
846 for (i = 0; i < TX_SIZES; i++) {
847 sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
848 sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
850 sf->frame_parameter_update = 0;
851 sf->encode_breakout_thresh = 1000;
854 sf->always_this_block_size = BLOCK_16X16;
855 sf->use_pick_mode = 1;
856 sf->encode_breakout_thresh = 1000;
860 void vp9_set_speed_features(VP9_COMP *cpi) {
861 SPEED_FEATURES *sf = &cpi->sf;
862 VP9_COMMON *cm = &cpi->common;
863 int speed = cpi->speed;
866 // Convert negative speed to positive
870 for (i = 0; i < MAX_MODES; ++i)
871 cpi->mode_chosen_counts[i] = 0;
873 // best quality defaults
874 sf->frame_parameter_update = 1;
875 sf->search_method = NSTEP;
876 sf->recode_loop = ALLOW_RECODE;
877 sf->subpel_search_method = SUBPEL_TREE;
878 sf->subpel_iters_per_step = 2;
879 sf->subpel_force_stop = 0;
880 sf->optimize_coefficients = !cpi->oxcf.lossless;
881 sf->reduce_first_step_size = 0;
882 sf->auto_mv_step_size = 0;
883 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
884 sf->comp_inter_joint_search_thresh = BLOCK_4X4;
885 sf->adaptive_rd_thresh = 0;
886 sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF;
887 sf->tx_size_search_method = USE_FULL_RD;
888 sf->use_lp32x32fdct = 0;
889 sf->adaptive_motion_search = 0;
890 sf->adaptive_pred_interp_filter = 0;
891 sf->reference_masking = 0;
892 sf->use_one_partition_size_always = 0;
893 sf->less_rectangular_check = 0;
894 sf->use_square_partition_only = 0;
895 sf->auto_min_max_partition_size = NOT_IN_USE;
896 sf->max_partition_size = BLOCK_64X64;
897 sf->min_partition_size = BLOCK_4X4;
898 sf->adjust_partitioning_from_last_frame = 0;
899 sf->last_partitioning_redo_frequency = 4;
900 sf->disable_split_mask = 0;
901 sf->mode_search_skip_flags = 0;
902 sf->disable_split_var_thresh = 0;
903 sf->disable_filter_search_var_thresh = 0;
904 for (i = 0; i < TX_SIZES; i++) {
905 sf->intra_y_mode_mask[i] = ALL_INTRA_MODES;
906 sf->intra_uv_mode_mask[i] = ALL_INTRA_MODES;
908 sf->use_rd_breakout = 0;
909 sf->skip_encode_sb = 0;
910 sf->use_uv_intra_rd_estimate = 0;
911 sf->use_fast_lpf_pick = 0;
912 sf->use_fast_coef_updates = 0;
913 sf->mode_skip_start = MAX_MODES; // Mode index at which mode skip mask set
914 sf->use_pick_mode = 0;
915 sf->encode_breakout_thresh = 0;
917 switch (cpi->oxcf.mode) {
918 case MODE_BESTQUALITY:
919 case MODE_SECONDPASS_BEST: // This is the best quality mode.
920 cpi->diamond_search_sad = vp9_full_range_search;
923 case MODE_GOODQUALITY:
924 case MODE_SECONDPASS:
925 set_good_speed_feature(cm, sf, speed);
928 set_rt_speed_feature(cm, sf, speed);
932 // Set rd thresholds based on mode and speed setting
933 set_rd_speed_thresholds(cpi);
934 set_rd_speed_thresholds_sub8x8(cpi);
936 // Slow quant, dct and trellis not worthwhile for first pass
937 // so make sure they are always turned off.
938 if (cpi->pass == 1) {
939 sf->optimize_coefficients = 0;
942 // No recode for 1 pass.
943 if (cpi->pass == 0) {
944 sf->recode_loop = DISALLOW_RECODE;
945 sf->optimize_coefficients = 0;
948 cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
949 if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
950 cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
953 if (cpi->sf.subpel_search_method == SUBPEL_TREE) {
954 cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
955 cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_tree;
958 cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1;
960 if (cpi->encode_breakout && cpi->oxcf.mode == MODE_REALTIME &&
961 sf->encode_breakout_thresh > cpi->encode_breakout)
962 cpi->encode_breakout = sf->encode_breakout_thresh;
965 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
966 VP9_COMMON *cm = &cpi->common;
968 cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height,
969 cm->subsampling_x, cm->subsampling_y,
970 cpi->oxcf.lag_in_frames);
972 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
973 "Failed to allocate lag buffers");
975 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
976 cpi->oxcf.width, cpi->oxcf.height,
977 cm->subsampling_x, cm->subsampling_y,
978 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
979 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
980 "Failed to allocate altref buffer");
983 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
984 VP9_COMMON *cm = &cpi->common;
986 if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
987 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
988 "Failed to allocate frame buffers");
990 if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
991 cm->width, cm->height,
992 cm->subsampling_x, cm->subsampling_y,
993 VP9_ENC_BORDER_IN_PIXELS))
994 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
995 "Failed to allocate last frame buffer");
997 if (vp9_alloc_frame_buffer(&cpi->scaled_source,
998 cm->width, cm->height,
999 cm->subsampling_x, cm->subsampling_y,
1000 VP9_ENC_BORDER_IN_PIXELS))
1001 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1002 "Failed to allocate scaled source buffer");
1007 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
1009 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1012 vpx_free(cpi->mb_activity_map);
1013 CHECK_MEM_ERROR(cm, cpi->mb_activity_map,
1014 vpx_calloc(sizeof(unsigned int),
1015 cm->mb_rows * cm->mb_cols));
1017 vpx_free(cpi->mb_norm_activity_map);
1018 CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map,
1019 vpx_calloc(sizeof(unsigned int),
1020 cm->mb_rows * cm->mb_cols));
1022 // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm
1023 // block where mi unit size is 8x8.
1024 vpx_free(cpi->above_context[0]);
1025 CHECK_MEM_ERROR(cm, cpi->above_context[0],
1026 vpx_calloc(2 * mi_cols_aligned_to_sb(cm->mi_cols) *
1028 sizeof(*cpi->above_context[0])));
1030 vpx_free(cpi->above_seg_context);
1031 CHECK_MEM_ERROR(cm, cpi->above_seg_context,
1032 vpx_calloc(mi_cols_aligned_to_sb(cm->mi_cols),
1033 sizeof(*cpi->above_seg_context)));
1037 static void update_frame_size(VP9_COMP *cpi) {
1038 VP9_COMMON *cm = &cpi->common;
1040 vp9_update_frame_size(cm);
1042 // Update size of buffers local to this frame
1043 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
1044 cm->width, cm->height,
1045 cm->subsampling_x, cm->subsampling_y,
1046 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
1047 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1048 "Failed to reallocate last frame buffer");
1050 if (vp9_realloc_frame_buffer(&cpi->scaled_source,
1051 cm->width, cm->height,
1052 cm->subsampling_x, cm->subsampling_y,
1053 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
1054 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1055 "Failed to reallocate scaled source buffer");
1058 int y_stride = cpi->scaled_source.y_stride;
1060 if (cpi->sf.search_method == NSTEP) {
1061 vp9_init3smotion_compensation(&cpi->mb, y_stride);
1062 } else if (cpi->sf.search_method == DIAMOND) {
1063 vp9_init_dsmotion_compensation(&cpi->mb, y_stride);
1069 for (i = 1; i < MAX_MB_PLANE; ++i) {
1070 cpi->above_context[i] = cpi->above_context[0] +
1071 i * sizeof(*cpi->above_context[0]) * 2 *
1072 mi_cols_aligned_to_sb(cm->mi_cols);
1078 // Table that converts 0-63 Q range values passed in outside to the Qindex
1079 // range used internally.
1080 static const int q_trans[] = {
1081 0, 4, 8, 12, 16, 20, 24, 28,
1082 32, 36, 40, 44, 48, 52, 56, 60,
1083 64, 68, 72, 76, 80, 84, 88, 92,
1084 96, 100, 104, 108, 112, 116, 120, 124,
1085 128, 132, 136, 140, 144, 148, 152, 156,
1086 160, 164, 168, 172, 176, 180, 184, 188,
1087 192, 196, 200, 204, 208, 212, 216, 220,
1088 224, 228, 232, 236, 240, 244, 249, 255,
1091 int vp9_reverse_trans(int x) {
1094 for (i = 0; i < 64; i++)
1095 if (q_trans[i] >= x)
1101 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
1102 VP9_COMMON *const cm = &cpi->common;
1103 int64_t vbr_max_bits;
1105 if (framerate < 0.1)
1108 cpi->oxcf.framerate = framerate;
1109 cpi->output_framerate = cpi->oxcf.framerate;
1110 cpi->rc.av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth
1111 / cpi->output_framerate);
1112 cpi->rc.min_frame_bandwidth = (int)(cpi->rc.av_per_frame_bandwidth *
1113 cpi->oxcf.two_pass_vbrmin_section / 100);
1116 cpi->rc.min_frame_bandwidth = MAX(cpi->rc.min_frame_bandwidth,
1117 FRAME_OVERHEAD_BITS);
1119 // A maximum bitrate for a frame is defined.
1120 // The baseline for this aligns with HW implementations that
1121 // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1122 // per 16x16 MB (averaged over a frame). However this limit is extended if
1123 // a very high rate is given on the command line or the the rate cannnot
1124 // be acheived because of a user specificed max q (e.g. when the user
1125 // specifies lossless encode.
1127 vbr_max_bits = ((int64_t)cpi->rc.av_per_frame_bandwidth *
1128 (int64_t)cpi->oxcf.two_pass_vbrmax_section) / 100;
1129 cpi->rc.max_frame_bandwidth =
1130 MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
1132 // Set Maximum gf/arf interval
1133 cpi->rc.max_gf_interval = 16;
1135 // Extended interval for genuinely static scenes
1136 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1138 // Special conditions when alt ref frame enabled in lagged compress mode
1139 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) {
1140 if (cpi->rc.max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1141 cpi->rc.max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1143 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1144 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1147 if (cpi->rc.max_gf_interval > cpi->twopass.static_scene_max_gf_interval)
1148 cpi->rc.max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1151 static int64_t rescale(int val, int64_t num, int denom) {
1152 int64_t llnum = num;
1153 int64_t llden = denom;
1154 int64_t llval = val;
1156 return (llval * llnum / llden);
1159 // Initialize layer context data from init_config().
1160 static void init_layer_context(VP9_COMP *const cpi) {
1161 const VP9_CONFIG *const oxcf = &cpi->oxcf;
1162 int temporal_layer = 0;
1163 cpi->svc.spatial_layer_id = 0;
1164 cpi->svc.temporal_layer_id = 0;
1165 for (temporal_layer = 0; temporal_layer < cpi->svc.number_temporal_layers;
1167 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
1168 RATE_CONTROL *const lrc = &lc->rc;
1169 lrc->avg_frame_qindex[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
1170 lrc->last_q[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
1171 lrc->ni_av_qi = q_trans[oxcf->worst_allowed_q];
1172 lrc->total_actual_bits = 0;
1173 lrc->total_target_vs_actual = 0;
1178 lrc->decimation_count = 0;
1179 lrc->decimation_factor = 0;
1180 lrc->rate_correction_factor = 1.0;
1181 lrc->key_frame_rate_correction_factor = 1.0;
1182 lc->target_bandwidth = oxcf->ts_target_bitrate[temporal_layer] *
1184 lrc->buffer_level = rescale((int)(oxcf->starting_buffer_level),
1185 lc->target_bandwidth, 1000);
1186 lrc->bits_off_target = lrc->buffer_level;
1190 // Update the layer context from a change_config() call.
1191 static void update_layer_context_change_config(VP9_COMP *const cpi,
1192 const int target_bandwidth) {
1193 const VP9_CONFIG *const oxcf = &cpi->oxcf;
1194 const RATE_CONTROL *const rc = &cpi->rc;
1195 int temporal_layer = 0;
1196 float bitrate_alloc = 1.0;
1197 for (temporal_layer = 0; temporal_layer < cpi->svc.number_temporal_layers;
1199 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
1200 RATE_CONTROL *const lrc = &lc->rc;
1201 lc->target_bandwidth = oxcf->ts_target_bitrate[temporal_layer] * 1000;
1202 bitrate_alloc = (float)lc->target_bandwidth / (float)target_bandwidth;
1203 // Update buffer-related quantities.
1204 lc->starting_buffer_level = oxcf->starting_buffer_level * bitrate_alloc;
1205 lc->optimal_buffer_level = oxcf->optimal_buffer_level * bitrate_alloc;
1206 lc->maximum_buffer_size = oxcf->maximum_buffer_size * bitrate_alloc;
1207 lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size);
1208 lrc->buffer_level = MIN(lrc->buffer_level, lc->maximum_buffer_size);
1209 // Update framerate-related quantities.
1210 lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[temporal_layer];
1211 lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
1212 lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
1213 // Update qp-related quantities.
1214 lrc->worst_quality = rc->worst_quality;
1215 lrc->best_quality = rc->best_quality;
1219 // Prior to encoding the frame, update framerate-related quantities
1220 // for the current layer.
1221 static void update_layer_framerate(VP9_COMP *const cpi) {
1222 int temporal_layer = cpi->svc.temporal_layer_id;
1223 const VP9_CONFIG *const oxcf = &cpi->oxcf;
1224 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
1225 RATE_CONTROL *const lrc = &lc->rc;
1226 lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[temporal_layer];
1227 lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
1228 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
1229 // Update the average layer frame size (non-cumulative per-frame-bw).
1230 if (temporal_layer == 0) {
1231 lc->avg_frame_size = lrc->av_per_frame_bandwidth;
1233 double prev_layer_framerate = oxcf->framerate /
1234 oxcf->ts_rate_decimator[temporal_layer - 1];
1235 int prev_layer_target_bandwidth =
1236 oxcf->ts_target_bitrate[temporal_layer - 1] * 1000;
1237 lc->avg_frame_size =
1238 (int)(lc->target_bandwidth - prev_layer_target_bandwidth) /
1239 (lc->framerate - prev_layer_framerate);
1243 // Prior to encoding the frame, set the layer context, for the current layer
1244 // to be encoded, to the cpi struct.
1245 static void restore_layer_context(VP9_COMP *const cpi) {
1246 int temporal_layer = cpi->svc.temporal_layer_id;
1247 LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
1248 int frame_since_key = cpi->rc.frames_since_key;
1249 int frame_to_key = cpi->rc.frames_to_key;
1251 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
1252 cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
1253 cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
1254 cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
1255 cpi->output_framerate = lc->framerate;
1256 // Reset the frames_since_key and frames_to_key counters to their values
1257 // before the layer restore. Keep these defined for the stream (not layer).
1258 cpi->rc.frames_since_key = frame_since_key;
1259 cpi->rc.frames_to_key = frame_to_key;
1262 // Save the layer context after encoding the frame.
1263 static void save_layer_context(VP9_COMP *const cpi) {
1264 int temporal_layer = cpi->svc.temporal_layer_id;
1265 LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
1267 lc->target_bandwidth = cpi->oxcf.target_bandwidth;
1268 lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
1269 lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
1270 lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
1271 lc->framerate = cpi->output_framerate;
1274 static void set_tile_limits(VP9_COMP *cpi) {
1275 VP9_COMMON *const cm = &cpi->common;
1277 int min_log2_tile_cols, max_log2_tile_cols;
1278 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1280 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
1281 min_log2_tile_cols, max_log2_tile_cols);
1282 cm->log2_tile_rows = cpi->oxcf.tile_rows;
1285 static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
1286 VP9_COMP *cpi = (VP9_COMP *)(ptr);
1287 VP9_COMMON *const cm = &cpi->common;
1292 cm->version = oxcf->version;
1294 cm->width = oxcf->width;
1295 cm->height = oxcf->height;
1296 cm->subsampling_x = 0;
1297 cm->subsampling_y = 0;
1298 vp9_alloc_compressor_data(cpi);
1300 // Spatial scalability.
1301 cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
1302 // Temporal scalability.
1303 cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
1305 if (cpi->svc.number_temporal_layers > 1 &&
1306 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1307 init_layer_context(cpi);
1310 // change includes all joint functionality
1311 vp9_change_config(ptr, oxcf);
1313 // Initialize active best and worst q and average q values.
1314 if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1315 cpi->rc.avg_frame_qindex[0] = cpi->oxcf.worst_allowed_q;
1316 cpi->rc.avg_frame_qindex[1] = cpi->oxcf.worst_allowed_q;
1317 cpi->rc.avg_frame_qindex[2] = cpi->oxcf.worst_allowed_q;
1319 cpi->rc.avg_frame_qindex[0] = (cpi->oxcf.worst_allowed_q +
1320 cpi->oxcf.best_allowed_q) / 2;
1321 cpi->rc.avg_frame_qindex[1] = (cpi->oxcf.worst_allowed_q +
1322 cpi->oxcf.best_allowed_q) / 2;
1323 cpi->rc.avg_frame_qindex[2] = (cpi->oxcf.worst_allowed_q +
1324 cpi->oxcf.best_allowed_q) / 2;
1326 cpi->rc.last_q[0] = cpi->oxcf.best_allowed_q;
1327 cpi->rc.last_q[1] = cpi->oxcf.best_allowed_q;
1328 cpi->rc.last_q[2] = cpi->oxcf.best_allowed_q;
1330 // Initialise the starting buffer levels
1331 cpi->rc.buffer_level = cpi->oxcf.starting_buffer_level;
1332 cpi->rc.bits_off_target = cpi->oxcf.starting_buffer_level;
1334 cpi->rc.rolling_target_bits = cpi->rc.av_per_frame_bandwidth;
1335 cpi->rc.rolling_actual_bits = cpi->rc.av_per_frame_bandwidth;
1336 cpi->rc.long_rolling_target_bits = cpi->rc.av_per_frame_bandwidth;
1337 cpi->rc.long_rolling_actual_bits = cpi->rc.av_per_frame_bandwidth;
1339 cpi->rc.total_actual_bits = 0;
1340 cpi->rc.total_target_vs_actual = 0;
1342 cpi->static_mb_pct = 0;
1344 cpi->lst_fb_idx = 0;
1345 cpi->gld_fb_idx = 1;
1346 cpi->alt_fb_idx = 2;
1348 set_tile_limits(cpi);
1350 cpi->fixed_divide[0] = 0;
1351 for (i = 1; i < 512; i++)
1352 cpi->fixed_divide[i] = 0x80000 / i;
1355 void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
1356 VP9_COMP *cpi = (VP9_COMP *)(ptr);
1357 VP9_COMMON *const cm = &cpi->common;
1362 if (cm->version != oxcf->version) {
1363 cm->version = oxcf->version;
1368 switch (cpi->oxcf.mode) {
1369 // Real time and one pass deprecated in test code base
1370 case MODE_GOODQUALITY:
1372 cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
1375 case MODE_FIRSTPASS:
1379 case MODE_SECONDPASS:
1381 cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
1384 case MODE_SECONDPASS_BEST:
1393 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1394 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1395 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1397 cpi->oxcf.lossless = oxcf->lossless;
1398 cpi->mb.e_mbd.itxm_add = cpi->oxcf.lossless ? vp9_iwht4x4_add
1400 cpi->rc.baseline_gf_interval = DEFAULT_GF_INTERVAL;
1402 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
1404 cpi->refresh_golden_frame = 0;
1405 cpi->refresh_last_frame = 1;
1406 cm->refresh_frame_context = 1;
1407 cm->reset_frame_context = 0;
1409 vp9_reset_segment_features(&cm->seg);
1410 set_high_precision_mv(cpi, 0);
1415 for (i = 0; i < MAX_SEGMENTS; i++)
1416 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1418 cpi->encode_breakout = cpi->oxcf.encode_breakout;
1420 // local file playback mode == really big buffer
1421 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
1422 cpi->oxcf.starting_buffer_level = 60000;
1423 cpi->oxcf.optimal_buffer_level = 60000;
1424 cpi->oxcf.maximum_buffer_size = 240000;
1427 // Convert target bandwidth from Kbit/s to Bit/s
1428 cpi->oxcf.target_bandwidth *= 1000;
1430 cpi->oxcf.starting_buffer_level = rescale(cpi->oxcf.starting_buffer_level,
1431 cpi->oxcf.target_bandwidth, 1000);
1433 // Set or reset optimal and maximum buffer levels.
1434 if (cpi->oxcf.optimal_buffer_level == 0)
1435 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1437 cpi->oxcf.optimal_buffer_level = rescale(cpi->oxcf.optimal_buffer_level,
1438 cpi->oxcf.target_bandwidth, 1000);
1440 if (cpi->oxcf.maximum_buffer_size == 0)
1441 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1443 cpi->oxcf.maximum_buffer_size = rescale(cpi->oxcf.maximum_buffer_size,
1444 cpi->oxcf.target_bandwidth, 1000);
1445 // Under a configuration change, where maximum_buffer_size may change,
1446 // keep buffer level clipped to the maximum allowed buffer size.
1447 cpi->rc.bits_off_target = MIN(cpi->rc.bits_off_target,
1448 cpi->oxcf.maximum_buffer_size);
1449 cpi->rc.buffer_level = MIN(cpi->rc.buffer_level,
1450 cpi->oxcf.maximum_buffer_size);
1452 // Set up frame rate and related parameters rate control values.
1453 vp9_new_framerate(cpi, cpi->oxcf.framerate);
1455 // Set absolute upper and lower quality limits
1456 cpi->rc.worst_quality = cpi->oxcf.worst_allowed_q;
1457 cpi->rc.best_quality = cpi->oxcf.best_allowed_q;
1459 // active values should only be modified if out of new range
1461 cpi->cq_target_quality = cpi->oxcf.cq_level;
1463 cm->interp_filter = DEFAULT_INTERP_FILTER;
1465 cm->display_width = cpi->oxcf.width;
1466 cm->display_height = cpi->oxcf.height;
1468 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1469 cpi->oxcf.sharpness = MIN(7, cpi->oxcf.sharpness);
1471 cpi->common.lf.sharpness_level = cpi->oxcf.sharpness;
1473 if (cpi->initial_width) {
1474 // Increasing the size of the frame beyond the first seen frame, or some
1475 // otherwise signaled maximum size, is not supported.
1476 // TODO(jkoleszar): exit gracefully.
1477 assert(cm->width <= cpi->initial_width);
1478 assert(cm->height <= cpi->initial_height);
1480 update_frame_size(cpi);
1482 if (cpi->svc.number_temporal_layers > 1 &&
1483 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1484 update_layer_context_change_config(cpi, cpi->oxcf.target_bandwidth);
1487 cpi->speed = cpi->oxcf.cpu_used;
1489 if (cpi->oxcf.lag_in_frames == 0) {
1490 // Force allow_lag to 0 if lag_in_frames is 0.
1491 cpi->oxcf.allow_lag = 0;
1492 } else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) {
1493 // Limit on lag buffers as these are not currently dynamically allocated.
1494 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1497 #if CONFIG_MULTIPLE_ARF
1498 vp9_zero(cpi->alt_ref_source);
1500 cpi->alt_ref_source = NULL;
1502 cpi->rc.is_src_frame_alt_ref = 0;
1505 // Experimental RD Code
1506 cpi->frame_distortion = 0;
1507 cpi->last_frame_distortion = 0;
1510 set_tile_limits(cpi);
1512 cpi->ext_refresh_frame_flags_pending = 0;
1513 cpi->ext_refresh_frame_context_pending = 0;
1516 #define M_LOG2_E 0.693147180559945309417
1517 #define log2f(x) (log (x) / (float) M_LOG2_E)
1519 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1520 mvjointsadcost[0] = 600;
1521 mvjointsadcost[1] = 300;
1522 mvjointsadcost[2] = 300;
1523 mvjointsadcost[0] = 300;
1526 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1529 mvsadcost[0][0] = 0;
1530 mvsadcost[1][0] = 0;
1533 double z = 256 * (2 * (log2f(8 * i) + .6));
1534 mvsadcost[0][i] = (int)z;
1535 mvsadcost[1][i] = (int)z;
1536 mvsadcost[0][-i] = (int)z;
1537 mvsadcost[1][-i] = (int)z;
1538 } while (++i <= MV_MAX);
1541 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1544 mvsadcost[0][0] = 0;
1545 mvsadcost[1][0] = 0;
1548 double z = 256 * (2 * (log2f(8 * i) + .6));
1549 mvsadcost[0][i] = (int)z;
1550 mvsadcost[1][i] = (int)z;
1551 mvsadcost[0][-i] = (int)z;
1552 mvsadcost[1][-i] = (int)z;
1553 } while (++i <= MV_MAX);
1556 static void alloc_mode_context(VP9_COMMON *cm, int num_4x4_blk,
1557 PICK_MODE_CONTEXT *ctx) {
1558 int num_pix = num_4x4_blk << 4;
1560 ctx->num_4x4_blk = num_4x4_blk;
1561 CHECK_MEM_ERROR(cm, ctx->zcoeff_blk,
1562 vpx_calloc(num_4x4_blk, sizeof(uint8_t)));
1563 for (i = 0; i < MAX_MB_PLANE; ++i) {
1564 for (k = 0; k < 3; ++k) {
1565 CHECK_MEM_ERROR(cm, ctx->coeff[i][k],
1566 vpx_memalign(16, num_pix * sizeof(int16_t)));
1567 CHECK_MEM_ERROR(cm, ctx->qcoeff[i][k],
1568 vpx_memalign(16, num_pix * sizeof(int16_t)));
1569 CHECK_MEM_ERROR(cm, ctx->dqcoeff[i][k],
1570 vpx_memalign(16, num_pix * sizeof(int16_t)));
1571 CHECK_MEM_ERROR(cm, ctx->eobs[i][k],
1572 vpx_memalign(16, num_pix * sizeof(uint16_t)));
1573 ctx->coeff_pbuf[i][k] = ctx->coeff[i][k];
1574 ctx->qcoeff_pbuf[i][k] = ctx->qcoeff[i][k];
1575 ctx->dqcoeff_pbuf[i][k] = ctx->dqcoeff[i][k];
1576 ctx->eobs_pbuf[i][k] = ctx->eobs[i][k];
1581 static void free_mode_context(PICK_MODE_CONTEXT *ctx) {
1583 vpx_free(ctx->zcoeff_blk);
1584 ctx->zcoeff_blk = 0;
1585 for (i = 0; i < MAX_MB_PLANE; ++i) {
1586 for (k = 0; k < 3; ++k) {
1587 vpx_free(ctx->coeff[i][k]);
1588 ctx->coeff[i][k] = 0;
1589 vpx_free(ctx->qcoeff[i][k]);
1590 ctx->qcoeff[i][k] = 0;
1591 vpx_free(ctx->dqcoeff[i][k]);
1592 ctx->dqcoeff[i][k] = 0;
1593 vpx_free(ctx->eobs[i][k]);
1594 ctx->eobs[i][k] = 0;
1599 static void init_pick_mode_context(VP9_COMP *cpi) {
1601 VP9_COMMON *const cm = &cpi->common;
1602 MACROBLOCK *const x = &cpi->mb;
1605 for (i = 0; i < BLOCK_SIZES; ++i) {
1606 const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
1607 const int num_4x4_h = num_4x4_blocks_high_lookup[i];
1608 const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
1609 if (i < BLOCK_16X16) {
1610 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1611 for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) {
1612 for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) {
1613 PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1614 alloc_mode_context(cm, num_4x4_blk, ctx);
1618 } else if (i < BLOCK_32X32) {
1619 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1620 for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) {
1621 PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1622 ctx->num_4x4_blk = num_4x4_blk;
1623 alloc_mode_context(cm, num_4x4_blk, ctx);
1626 } else if (i < BLOCK_64X64) {
1627 for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) {
1628 PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1629 ctx->num_4x4_blk = num_4x4_blk;
1630 alloc_mode_context(cm, num_4x4_blk, ctx);
1633 PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1634 ctx->num_4x4_blk = num_4x4_blk;
1635 alloc_mode_context(cm, num_4x4_blk, ctx);
1640 static void free_pick_mode_context(MACROBLOCK *x) {
1643 for (i = 0; i < BLOCK_SIZES; ++i) {
1644 const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
1645 const int num_4x4_h = num_4x4_blocks_high_lookup[i];
1646 const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
1647 if (i < BLOCK_16X16) {
1648 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1649 for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) {
1650 for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) {
1651 PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1652 free_mode_context(ctx);
1656 } else if (i < BLOCK_32X32) {
1657 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1658 for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) {
1659 PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1660 free_mode_context(ctx);
1663 } else if (i < BLOCK_64X64) {
1664 for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) {
1665 PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1666 free_mode_context(ctx);
1669 PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1670 free_mode_context(ctx);
1675 VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) {
1685 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP9_COMP));
1686 // Check that the CPI instance is valid
1694 if (setjmp(cm->error.jmp)) {
1695 VP9_PTR ptr = ctx.ptr;
1697 ctx.cpi->common.error.setjmp = 0;
1698 vp9_remove_compressor(&ptr);
1702 cm->error.setjmp = 1;
1704 CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site),
1705 (MAX_MVSEARCH_STEPS * 8) + 1));
1707 vp9_create_common(cm);
1711 init_config((VP9_PTR)cpi, oxcf);
1713 init_pick_mode_context(cpi);
1715 cm->current_video_frame = 0;
1717 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1718 cm->ref_frame_sign_bias[ALTREF_FRAME] = 1;
1720 cpi->rc.baseline_gf_interval = DEFAULT_GF_INTERVAL;
1722 cpi->gold_is_last = 0;
1723 cpi->alt_is_last = 0;
1724 cpi->gold_is_alt = 0;
1726 // Create the encoder segmentation map and set all entries to 0
1727 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1728 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1730 // Create a complexity map used for rd adjustment
1731 CHECK_MEM_ERROR(cm, cpi->complexity_map,
1732 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1735 // And a place holder structure is the coding context
1736 // for use if we want to save and restore it
1737 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1738 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1740 CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1));
1741 vpx_memset(cpi->active_map, 1, cm->MBs);
1742 cpi->active_map_enabled = 0;
1744 for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
1745 sizeof(cpi->mbgraph_stats[0])); i++) {
1746 CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
1747 vpx_calloc(cm->MBs *
1748 sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1751 /*Initialize the feed-forward activity masking.*/
1752 cpi->activity_avg = 90 << 12;
1753 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1755 cpi->rc.frames_since_key = 8; // Sensible default for first frame.
1756 cpi->rc.this_key_frame_forced = 0;
1757 cpi->rc.next_key_frame_forced = 0;
1759 cpi->rc.source_alt_ref_pending = 0;
1760 cpi->rc.source_alt_ref_active = 0;
1761 cpi->refresh_alt_ref_frame = 0;
1763 #if CONFIG_MULTIPLE_ARF
1764 // Turn multiple ARF usage on/off. This is a quick hack for the initial test
1765 // version. It should eventually be set via the codec API.
1766 cpi->multi_arf_enabled = 1;
1768 if (cpi->multi_arf_enabled) {
1769 cpi->sequence_number = 0;
1770 cpi->frame_coding_order_period = 0;
1771 vp9_zero(cpi->frame_coding_order);
1772 vp9_zero(cpi->arf_buffer_idx);
1776 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1777 #if CONFIG_INTERNAL_STATS
1778 cpi->b_calculate_ssimg = 0;
1783 if (cpi->b_calculate_psnr) {
1788 cpi->total_sq_error = 0;
1789 cpi->total_samples = 0;
1791 cpi->totalp_y = 0.0;
1792 cpi->totalp_u = 0.0;
1793 cpi->totalp_v = 0.0;
1795 cpi->totalp_sq_error = 0;
1796 cpi->totalp_samples = 0;
1798 cpi->tot_recode_hits = 0;
1799 cpi->summed_quality = 0;
1800 cpi->summed_weights = 0;
1801 cpi->summedp_quality = 0;
1802 cpi->summedp_weights = 0;
1805 if (cpi->b_calculate_ssimg) {
1806 cpi->total_ssimg_y = 0;
1807 cpi->total_ssimg_u = 0;
1808 cpi->total_ssimg_v = 0;
1809 cpi->total_ssimg_all = 0;
1814 cpi->first_time_stamp_ever = INT64_MAX;
1816 cpi->rc.frames_till_gf_update_due = 0;
1818 cpi->rc.ni_av_qi = cpi->oxcf.worst_allowed_q;
1819 cpi->rc.ni_tot_qi = 0;
1820 cpi->rc.ni_frames = 0;
1821 cpi->rc.tot_q = 0.0;
1822 cpi->rc.avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q);
1824 cpi->rc.rate_correction_factor = 1.0;
1825 cpi->rc.key_frame_rate_correction_factor = 1.0;
1826 cpi->rc.gf_rate_correction_factor = 1.0;
1828 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
1829 cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX];
1830 cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX];
1831 cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX];
1832 cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX];
1833 cal_nmvsadcosts(cpi->mb.nmvsadcost);
1835 cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX];
1836 cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX];
1837 cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX];
1838 cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX];
1839 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
1841 #ifdef OUTPUT_YUV_SRC
1842 yuv_file = fopen("bd.yuv", "ab");
1844 #ifdef OUTPUT_YUV_REC
1845 yuv_rec_file = fopen("rec.yuv", "wb");
1849 framepsnr = fopen("framepsnr.stt", "a");
1850 kf_list = fopen("kf_list.stt", "w");
1853 cpi->output_pkt_list = oxcf->output_pkt_list;
1855 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
1857 if (cpi->pass == 1) {
1858 vp9_init_first_pass(cpi);
1859 } else if (cpi->pass == 2) {
1860 size_t packet_sz = sizeof(FIRSTPASS_STATS);
1861 int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1863 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1864 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1865 cpi->twopass.stats_in_end = (void *)((char *)cpi->twopass.stats_in
1866 + (packets - 1) * packet_sz);
1867 vp9_init_second_pass(cpi);
1870 vp9_set_speed_features(cpi);
1872 // Default rd threshold factors for mode selection
1873 for (i = 0; i < BLOCK_SIZES; ++i) {
1874 for (j = 0; j < MAX_MODES; ++j)
1875 cpi->rd_thresh_freq_fact[i][j] = 32;
1876 for (j = 0; j < MAX_REFS; ++j)
1877 cpi->rd_thresh_freq_sub8x8[i][j] = 32;
1880 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \
1881 SDX3F, SDX8F, SDX4DF)\
1882 cpi->fn_ptr[BT].sdf = SDF; \
1883 cpi->fn_ptr[BT].sdaf = SDAF; \
1884 cpi->fn_ptr[BT].vf = VF; \
1885 cpi->fn_ptr[BT].svf = SVF; \
1886 cpi->fn_ptr[BT].svaf = SVAF; \
1887 cpi->fn_ptr[BT].svf_halfpix_h = SVFHH; \
1888 cpi->fn_ptr[BT].svf_halfpix_v = SVFHV; \
1889 cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \
1890 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1891 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1892 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1894 BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
1895 vp9_variance32x16, vp9_sub_pixel_variance32x16,
1896 vp9_sub_pixel_avg_variance32x16, NULL, NULL,
1900 BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
1901 vp9_variance16x32, vp9_sub_pixel_variance16x32,
1902 vp9_sub_pixel_avg_variance16x32, NULL, NULL,
1906 BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
1907 vp9_variance64x32, vp9_sub_pixel_variance64x32,
1908 vp9_sub_pixel_avg_variance64x32, NULL, NULL,
1912 BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
1913 vp9_variance32x64, vp9_sub_pixel_variance32x64,
1914 vp9_sub_pixel_avg_variance32x64, NULL, NULL,
1918 BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
1919 vp9_variance32x32, vp9_sub_pixel_variance32x32,
1920 vp9_sub_pixel_avg_variance32x32, vp9_variance_halfpixvar32x32_h,
1921 vp9_variance_halfpixvar32x32_v,
1922 vp9_variance_halfpixvar32x32_hv, vp9_sad32x32x3, vp9_sad32x32x8,
1925 BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
1926 vp9_variance64x64, vp9_sub_pixel_variance64x64,
1927 vp9_sub_pixel_avg_variance64x64, vp9_variance_halfpixvar64x64_h,
1928 vp9_variance_halfpixvar64x64_v,
1929 vp9_variance_halfpixvar64x64_hv, vp9_sad64x64x3, vp9_sad64x64x8,
1932 BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
1933 vp9_variance16x16, vp9_sub_pixel_variance16x16,
1934 vp9_sub_pixel_avg_variance16x16, vp9_variance_halfpixvar16x16_h,
1935 vp9_variance_halfpixvar16x16_v,
1936 vp9_variance_halfpixvar16x16_hv, vp9_sad16x16x3, vp9_sad16x16x8,
1939 BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
1940 vp9_variance16x8, vp9_sub_pixel_variance16x8,
1941 vp9_sub_pixel_avg_variance16x8, NULL, NULL, NULL,
1942 vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
1944 BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
1945 vp9_variance8x16, vp9_sub_pixel_variance8x16,
1946 vp9_sub_pixel_avg_variance8x16, NULL, NULL, NULL,
1947 vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
1949 BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
1950 vp9_variance8x8, vp9_sub_pixel_variance8x8,
1951 vp9_sub_pixel_avg_variance8x8, NULL, NULL, NULL,
1952 vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
1954 BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
1955 vp9_variance8x4, vp9_sub_pixel_variance8x4,
1956 vp9_sub_pixel_avg_variance8x4, NULL, NULL,
1957 NULL, NULL, vp9_sad8x4x8,
1960 BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
1961 vp9_variance4x8, vp9_sub_pixel_variance4x8,
1962 vp9_sub_pixel_avg_variance4x8, NULL, NULL,
1963 NULL, NULL, vp9_sad4x8x8,
1966 BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
1967 vp9_variance4x4, vp9_sub_pixel_variance4x4,
1968 vp9_sub_pixel_avg_variance4x4, NULL, NULL, NULL,
1969 vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
1971 cpi->full_search_sad = vp9_full_search_sad;
1972 cpi->diamond_search_sad = vp9_diamond_search_sad;
1973 cpi->refining_search_sad = vp9_refining_search_sad;
1975 /* vp9_init_quantizer() is first called here. Add check in
1976 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1977 * called later when needed. This will avoid unnecessary calls of
1978 * vp9_init_quantizer() for every frame.
1980 vp9_init_quantizer(cpi);
1982 vp9_loop_filter_init(cm);
1984 cm->error.setjmp = 0;
1986 vp9_zero(cpi->common.counts.uv_mode);
1988 #ifdef MODE_TEST_HIT_STATS
1989 vp9_zero(cpi->mode_test_hits);
1992 return (VP9_PTR) cpi;
1995 void vp9_remove_compressor(VP9_PTR *ptr) {
1996 VP9_COMP *cpi = (VP9_COMP *)(*ptr);
2002 if (cpi && (cpi->common.current_video_frame > 0)) {
2003 if (cpi->pass == 2) {
2004 vp9_end_second_pass(cpi);
2007 #if CONFIG_INTERNAL_STATS
2009 vp9_clear_system_state();
2011 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
2012 if (cpi->pass != 1) {
2013 FILE *f = fopen("opsnr.stt", "a");
2014 double time_encoded = (cpi->last_end_time_stamp_seen
2015 - cpi->first_time_stamp_ever) / 10000000.000;
2016 double total_encode_time = (cpi->time_receive_data +
2017 cpi->time_compress_data) / 1000.000;
2018 double dr = (double)cpi->bytes * (double) 8 / (double)1000
2021 if (cpi->b_calculate_psnr) {
2022 const double total_psnr = vp9_mse2psnr(cpi->total_samples, 255.0,
2023 cpi->total_sq_error);
2024 const double totalp_psnr = vp9_mse2psnr(cpi->totalp_samples, 255.0,
2025 cpi->totalp_sq_error);
2026 const double total_ssim = 100 * pow(cpi->summed_quality /
2027 cpi->summed_weights, 8.0);
2028 const double totalp_ssim = 100 * pow(cpi->summedp_quality /
2029 cpi->summedp_weights, 8.0);
2031 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
2032 "VPXSSIM\tVPSSIMP\t Time(ms)\n");
2033 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",
2034 dr, cpi->total / cpi->count, total_psnr,
2035 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
2039 if (cpi->b_calculate_ssimg) {
2040 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n");
2041 fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2042 cpi->total_ssimg_y / cpi->count,
2043 cpi->total_ssimg_u / cpi->count,
2044 cpi->total_ssimg_v / cpi->count,
2045 cpi->total_ssimg_all / cpi->count, total_encode_time);
2053 #ifdef MODE_TEST_HIT_STATS
2054 if (cpi->pass != 1) {
2055 double norm_per_pixel_mode_tests = 0;
2056 double norm_counts[BLOCK_SIZES];
2059 int norm_factors[BLOCK_SIZES] =
2060 {256, 128, 128, 64, 32, 32, 16, 8, 8, 4, 2, 2, 1};
2061 FILE *f = fopen("mode_hit_stats.stt", "a");
2063 // On average, how many mode tests do we do
2064 for (i = 0; i < BLOCK_SIZES; ++i) {
2065 norm_counts[i] = (double)cpi->mode_test_hits[i] /
2066 (double)norm_factors[i];
2067 norm_per_pixel_mode_tests += norm_counts[i];
2069 // Convert to a number per 64x64 and per frame
2070 sb64_per_frame = ((cpi->common.height + 63) / 64) *
2071 ((cpi->common.width + 63) / 64);
2072 norm_per_pixel_mode_tests =
2073 norm_per_pixel_mode_tests /
2074 (double)(cpi->common.current_video_frame * sb64_per_frame);
2076 fprintf(f, "%6.4f\n", norm_per_pixel_mode_tests);
2083 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2084 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2085 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2086 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2087 cpi->time_compress_data / 1000,
2088 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2093 free_pick_mode_context(&cpi->mb);
2094 dealloc_compressor_data(cpi);
2095 vpx_free(cpi->mb.ss);
2098 for (i = 0; i < sizeof(cpi->mbgraph_stats) /
2099 sizeof(cpi->mbgraph_stats[0]); ++i) {
2100 vpx_free(cpi->mbgraph_stats[i].mb_stats);
2103 vp9_remove_common(&cpi->common);
2107 #ifdef OUTPUT_YUV_SRC
2110 #ifdef OUTPUT_YUV_REC
2111 fclose(yuv_rec_file);
2129 static uint64_t calc_plane_error(const uint8_t *orig, int orig_stride,
2130 const uint8_t *recon, int recon_stride,
2131 unsigned int cols, unsigned int rows) {
2132 unsigned int row, col;
2133 uint64_t total_sse = 0;
2136 for (row = 0; row + 16 <= rows; row += 16) {
2137 for (col = 0; col + 16 <= cols; col += 16) {
2140 vp9_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse);
2144 /* Handle odd-sized width */
2146 unsigned int border_row, border_col;
2147 const uint8_t *border_orig = orig;
2148 const uint8_t *border_recon = recon;
2150 for (border_row = 0; border_row < 16; border_row++) {
2151 for (border_col = col; border_col < cols; border_col++) {
2152 diff = border_orig[border_col] - border_recon[border_col];
2153 total_sse += diff * diff;
2156 border_orig += orig_stride;
2157 border_recon += recon_stride;
2161 orig += orig_stride * 16;
2162 recon += recon_stride * 16;
2165 /* Handle odd-sized height */
2166 for (; row < rows; row++) {
2167 for (col = 0; col < cols; col++) {
2168 diff = orig[col] - recon[col];
2169 total_sse += diff * diff;
2172 orig += orig_stride;
2173 recon += recon_stride;
2180 double psnr[4]; // total/y/u/v
2181 uint64_t sse[4]; // total/y/u/v
2182 uint32_t samples[4]; // total/y/u/v
2185 static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
2187 const int widths[3] = {a->y_width, a->uv_width, a->uv_width };
2188 const int heights[3] = {a->y_height, a->uv_height, a->uv_height};
2189 const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer, a->v_buffer };
2190 const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride};
2191 const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer, b->v_buffer };
2192 const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride};
2194 uint64_t total_sse = 0;
2195 uint32_t total_samples = 0;
2197 for (i = 0; i < 3; ++i) {
2198 const int w = widths[i];
2199 const int h = heights[i];
2200 const uint32_t samples = w * h;
2201 const double sse = calc_plane_error(a_planes[i], a_strides[i],
2202 b_planes[i], b_strides[i],
2204 psnr->sse[1 + i] = sse;
2205 psnr->samples[1 + i] = samples;
2206 psnr->psnr[1 + i] = vp9_mse2psnr(samples, 255.0, sse);
2209 total_samples += samples;
2212 psnr->sse[0] = total_sse;
2213 psnr->samples[0] = total_samples;
2214 psnr->psnr[0] = vp9_mse2psnr(total_samples, 255.0, total_sse);
2217 static void generate_psnr_packet(VP9_COMP *cpi) {
2218 struct vpx_codec_cx_pkt pkt;
2221 calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
2222 for (i = 0; i < 4; ++i) {
2223 pkt.data.psnr.samples[i] = psnr.samples[i];
2224 pkt.data.psnr.sse[i] = psnr.sse[i];
2225 pkt.data.psnr.psnr[i] = psnr.psnr[i];
2227 pkt.kind = VPX_CODEC_PSNR_PKT;
2228 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2231 int vp9_use_as_reference(VP9_PTR ptr, int ref_frame_flags) {
2232 VP9_COMP *cpi = (VP9_COMP *)(ptr);
2234 if (ref_frame_flags > 7)
2237 cpi->ref_frame_flags = ref_frame_flags;
2241 int vp9_update_reference(VP9_PTR ptr, int ref_frame_flags) {
2242 VP9_COMP *cpi = (VP9_COMP *)(ptr);
2244 if (ref_frame_flags > 7)
2247 cpi->ext_refresh_golden_frame = 0;
2248 cpi->ext_refresh_alt_ref_frame = 0;
2249 cpi->ext_refresh_last_frame = 0;
2251 if (ref_frame_flags & VP9_LAST_FLAG)
2252 cpi->ext_refresh_last_frame = 1;
2254 if (ref_frame_flags & VP9_GOLD_FLAG)
2255 cpi->ext_refresh_golden_frame = 1;
2257 if (ref_frame_flags & VP9_ALT_FLAG)
2258 cpi->ext_refresh_alt_ref_frame = 1;
2260 cpi->ext_refresh_frame_flags_pending = 1;
2264 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
2265 VP9_REFFRAME ref_frame_flag) {
2266 MV_REFERENCE_FRAME ref_frame = NONE;
2267 if (ref_frame_flag == VP9_LAST_FLAG)
2268 ref_frame = LAST_FRAME;
2269 else if (ref_frame_flag == VP9_GOLD_FLAG)
2270 ref_frame = GOLDEN_FRAME;
2271 else if (ref_frame_flag == VP9_ALT_FLAG)
2272 ref_frame = ALTREF_FRAME;
2274 return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2277 int vp9_copy_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag,
2278 YV12_BUFFER_CONFIG *sd) {
2279 VP9_COMP *const cpi = (VP9_COMP *)ptr;
2280 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2282 vp8_yv12_copy_frame(cfg, sd);
2289 int vp9_get_reference_enc(VP9_PTR ptr, int index, YV12_BUFFER_CONFIG **fb) {
2290 VP9_COMP *cpi = (VP9_COMP *)ptr;
2291 VP9_COMMON *cm = &cpi->common;
2293 if (index < 0 || index >= REF_FRAMES)
2296 *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf;
2300 int vp9_set_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag,
2301 YV12_BUFFER_CONFIG *sd) {
2302 VP9_COMP *cpi = (VP9_COMP *)ptr;
2303 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2305 vp8_yv12_copy_frame(sd, cfg);
2312 int vp9_update_entropy(VP9_PTR comp, int update) {
2313 ((VP9_COMP *)comp)->ext_refresh_frame_context = update;
2314 ((VP9_COMP *)comp)->ext_refresh_frame_context_pending = 1;
2319 #ifdef OUTPUT_YUV_SRC
2320 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s) {
2321 uint8_t *src = s->y_buffer;
2322 int h = s->y_height;
2325 fwrite(src, s->y_width, 1, yuv_file);
2333 fwrite(src, s->uv_width, 1, yuv_file);
2334 src += s->uv_stride;
2341 fwrite(src, s->uv_width, 1, yuv_file);
2342 src += s->uv_stride;
2347 #ifdef OUTPUT_YUV_REC
2348 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2349 YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2350 uint8_t *src = s->y_buffer;
2354 fwrite(src, s->y_width, 1, yuv_rec_file);
2362 fwrite(src, s->uv_width, 1, yuv_rec_file);
2363 src += s->uv_stride;
2370 fwrite(src, s->uv_width, 1, yuv_rec_file);
2371 src += s->uv_stride;
2375 if (s->alpha_buffer) {
2376 src = s->alpha_buffer;
2377 h = s->alpha_height;
2379 fwrite(src, s->alpha_width, 1, yuv_rec_file);
2380 src += s->alpha_stride;
2385 fflush(yuv_rec_file);
2389 static void scale_and_extend_frame_nonnormative(YV12_BUFFER_CONFIG *src_fb,
2390 YV12_BUFFER_CONFIG *dst_fb) {
2391 const int in_w = src_fb->y_crop_width;
2392 const int in_h = src_fb->y_crop_height;
2393 const int out_w = dst_fb->y_crop_width;
2394 const int out_h = dst_fb->y_crop_height;
2395 const int in_w_uv = src_fb->uv_crop_width;
2396 const int in_h_uv = src_fb->uv_crop_height;
2397 const int out_w_uv = dst_fb->uv_crop_width;
2398 const int out_h_uv = dst_fb->uv_crop_height;
2401 uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer,
2402 src_fb->alpha_buffer};
2403 int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride,
2404 src_fb->alpha_stride};
2406 uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer,
2407 dst_fb->alpha_buffer};
2408 int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride,
2409 dst_fb->alpha_stride};
2411 for (i = 0; i < MAX_MB_PLANE; ++i) {
2412 if (i == 0 || i == 3) {
2413 // Y and alpha planes
2414 vp9_resize_plane(srcs[i], in_h, in_w, src_strides[i],
2415 dsts[i], out_h, out_w, dst_strides[i]);
2418 vp9_resize_plane(srcs[i], in_h_uv, in_w_uv, src_strides[i],
2419 dsts[i], out_h_uv, out_w_uv, dst_strides[i]);
2422 vp8_yv12_extend_frame_borders(dst_fb);
2425 static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb,
2426 YV12_BUFFER_CONFIG *dst_fb) {
2427 const int in_w = src_fb->y_crop_width;
2428 const int in_h = src_fb->y_crop_height;
2429 const int out_w = dst_fb->y_crop_width;
2430 const int out_h = dst_fb->y_crop_height;
2433 uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer,
2434 src_fb->alpha_buffer};
2435 int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride,
2436 src_fb->alpha_stride};
2438 uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer,
2439 dst_fb->alpha_buffer};
2440 int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride,
2441 dst_fb->alpha_stride};
2443 for (y = 0; y < out_h; y += 16) {
2444 for (x = 0; x < out_w; x += 16) {
2445 for (i = 0; i < MAX_MB_PLANE; ++i) {
2446 const int factor = (i == 0 || i == 3 ? 1 : 2);
2447 const int x_q4 = x * (16 / factor) * in_w / out_w;
2448 const int y_q4 = y * (16 / factor) * in_h / out_h;
2449 const int src_stride = src_strides[i];
2450 const int dst_stride = dst_strides[i];
2451 uint8_t *src = srcs[i] + y / factor * in_h / out_h * src_stride +
2452 x / factor * in_w / out_w;
2453 uint8_t *dst = dsts[i] + y / factor * dst_stride + x / factor;
2455 vp9_convolve8(src, src_stride, dst, dst_stride,
2456 vp9_sub_pel_filters_8[x_q4 & 0xf], 16 * in_w / out_w,
2457 vp9_sub_pel_filters_8[y_q4 & 0xf], 16 * in_h / out_h,
2458 16 / factor, 16 / factor);
2463 vp8_yv12_extend_frame_borders(dst_fb);
2466 static int find_fp_qindex() {
2469 for (i = 0; i < QINDEX_RANGE; i++) {
2470 if (vp9_convert_qindex_to_q(i) >= 30.0) {
2475 if (i == QINDEX_RANGE)
2481 #define WRITE_RECON_BUFFER 0
2482 #if WRITE_RECON_BUFFER
2483 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) {
2488 snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame);
2489 yframe = fopen(filename, "wb");
2491 for (i = 0; i < frame->y_height; i++)
2492 fwrite(frame->y_buffer + i * frame->y_stride,
2493 frame->y_width, 1, yframe);
2496 snprintf(filename, sizeof(filename), "cx\\u%04d.raw", this_frame);
2497 yframe = fopen(filename, "wb");
2499 for (i = 0; i < frame->uv_height; i++)
2500 fwrite(frame->u_buffer + i * frame->uv_stride,
2501 frame->uv_width, 1, yframe);
2504 snprintf(filename, sizeof(filename), "cx\\v%04d.raw", this_frame);
2505 yframe = fopen(filename, "wb");
2507 for (i = 0; i < frame->uv_height; i++)
2508 fwrite(frame->v_buffer + i * frame->uv_stride,
2509 frame->uv_width, 1, yframe);
2515 static double compute_edge_pixel_proportion(YV12_BUFFER_CONFIG *frame) {
2516 #define EDGE_THRESH 128
2518 int num_edge_pels = 0;
2519 int num_pels = (frame->y_height - 2) * (frame->y_width - 2);
2520 uint8_t *prev = frame->y_buffer + 1;
2521 uint8_t *curr = frame->y_buffer + 1 + frame->y_stride;
2522 uint8_t *next = frame->y_buffer + 1 + 2 * frame->y_stride;
2523 for (i = 1; i < frame->y_height - 1; i++) {
2524 for (j = 1; j < frame->y_width - 1; j++) {
2525 /* Sobel hor and ver gradients */
2526 int v = 2 * (curr[1] - curr[-1]) + (prev[1] - prev[-1]) +
2527 (next[1] - next[-1]);
2528 int h = 2 * (prev[0] - next[0]) + (prev[1] - next[1]) +
2529 (prev[-1] - next[-1]);
2530 h = (h < 0 ? -h : h);
2531 v = (v < 0 ? -v : v);
2532 if (h > EDGE_THRESH || v > EDGE_THRESH)
2538 curr += frame->y_stride - frame->y_width + 2;
2539 prev += frame->y_stride - frame->y_width + 2;
2540 next += frame->y_stride - frame->y_width + 2;
2542 return (double)num_edge_pels / num_pels;
2545 // Function to test for conditions that indicate we should loop
2546 // back and recode a frame.
2547 static int recode_loop_test(const VP9_COMP *cpi,
2548 int high_limit, int low_limit,
2549 int q, int maxq, int minq) {
2550 const VP9_COMMON *const cm = &cpi->common;
2551 const RATE_CONTROL *const rc = &cpi->rc;
2552 int force_recode = 0;
2554 // Special case trap if maximum allowed frame size exceeded.
2555 if (rc->projected_frame_size > rc->max_frame_bandwidth) {
2558 // Is frame recode allowed.
2559 // Yes if either recode mode 1 is selected or mode 2 is selected
2560 // and the frame is a key frame, golden frame or alt_ref_frame
2561 } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
2562 ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
2563 (cm->frame_type == KEY_FRAME ||
2564 cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
2565 // General over and under shoot tests
2566 if ((rc->projected_frame_size > high_limit && q < maxq) ||
2567 (rc->projected_frame_size < low_limit && q > minq)) {
2569 } else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2570 // Deal with frame undershoot and whether or not we are
2571 // below the automatically set cq level.
2572 if (q > cpi->cq_target_quality &&
2573 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2578 return force_recode;
2581 static void update_reference_frames(VP9_COMP * const cpi) {
2582 VP9_COMMON * const cm = &cpi->common;
2584 // At this point the new frame has been encoded.
2585 // If any buffer copy / swapping is signaled it should be done here.
2586 if (cm->frame_type == KEY_FRAME) {
2587 ref_cnt_fb(cm->frame_bufs,
2588 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2589 ref_cnt_fb(cm->frame_bufs,
2590 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2592 #if CONFIG_MULTIPLE_ARF
2593 else if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame &&
2594 !cpi->refresh_alt_ref_frame) {
2596 else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame &&
2599 /* Preserve the previously existing golden frame and update the frame in
2600 * the alt ref slot instead. This is highly specific to the current use of
2601 * alt-ref as a forward reference, and this needs to be generalized as
2602 * other uses are implemented (like RTC/temporal scaling)
2604 * The update to the buffer in the alt ref slot was signaled in
2605 * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated
2606 * as the golden frame next time.
2610 ref_cnt_fb(cm->frame_bufs,
2611 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2613 tmp = cpi->alt_fb_idx;
2614 cpi->alt_fb_idx = cpi->gld_fb_idx;
2615 cpi->gld_fb_idx = tmp;
2616 } else { /* For non key/golden frames */
2617 if (cpi->refresh_alt_ref_frame) {
2618 int arf_idx = cpi->alt_fb_idx;
2619 #if CONFIG_MULTIPLE_ARF
2620 if (cpi->multi_arf_enabled) {
2621 arf_idx = cpi->arf_buffer_idx[cpi->sequence_number + 1];
2624 ref_cnt_fb(cm->frame_bufs,
2625 &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2628 if (cpi->refresh_golden_frame) {
2629 ref_cnt_fb(cm->frame_bufs,
2630 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2634 if (cpi->refresh_last_frame) {
2635 ref_cnt_fb(cm->frame_bufs,
2636 &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
2640 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2641 MACROBLOCKD *xd = &cpi->mb.e_mbd;
2642 struct loopfilter *lf = &cm->lf;
2644 lf->filter_level = 0;
2646 struct vpx_usec_timer timer;
2648 vp9_clear_system_state();
2650 vpx_usec_timer_start(&timer);
2652 vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.use_fast_lpf_pick);
2654 vpx_usec_timer_mark(&timer);
2655 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2658 if (lf->filter_level > 0) {
2659 vp9_set_alt_lf_level(cpi, lf->filter_level);
2660 vp9_loop_filter_frame(cm, xd, lf->filter_level, 0, 0);
2663 vp9_extend_frame_inner_borders(cm->frame_to_show,
2664 cm->subsampling_x, cm->subsampling_y);
2667 static void scale_references(VP9_COMP *cpi) {
2668 VP9_COMMON *cm = &cpi->common;
2669 MV_REFERENCE_FRAME ref_frame;
2671 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2672 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2673 YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf;
2675 if (ref->y_crop_width != cm->width ||
2676 ref->y_crop_height != cm->height) {
2677 const int new_fb = get_free_fb(cm);
2678 vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
2679 cm->width, cm->height,
2680 cm->subsampling_x, cm->subsampling_y,
2681 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2682 scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf);
2683 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2685 cpi->scaled_ref_idx[ref_frame - 1] = idx;
2686 cm->frame_bufs[idx].ref_count++;
2691 static void release_scaled_references(VP9_COMP *cpi) {
2692 VP9_COMMON *cm = &cpi->common;
2695 for (i = 0; i < 3; i++)
2696 cm->frame_bufs[cpi->scaled_ref_idx[i]].ref_count--;
2699 static void full_to_model_count(unsigned int *model_count,
2700 unsigned int *full_count) {
2702 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
2703 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
2704 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
2705 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
2706 model_count[TWO_TOKEN] += full_count[n];
2707 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
2710 static void full_to_model_counts(vp9_coeff_count_model *model_count,
2711 vp9_coeff_count *full_count) {
2714 for (i = 0; i < PLANE_TYPES; ++i)
2715 for (j = 0; j < REF_TYPES; ++j)
2716 for (k = 0; k < COEF_BANDS; ++k)
2717 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
2718 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
2721 #if 0 && CONFIG_INTERNAL_STATS
2722 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
2723 VP9_COMMON *const cm = &cpi->common;
2724 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
2727 vp9_clear_system_state(); // __asm emms;
2729 recon_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
2731 if (cpi->twopass.total_left_stats.coded_error != 0.0)
2732 fprintf(f, "%10u %10d %10d %10d %10d %10d "
2733 "%10"PRId64" %10"PRId64" %10d "
2734 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2735 "%6d %6d %5d %5d %5d "
2736 "%10"PRId64" %10.3lf"
2737 "%10lf %8u %10d %10d %10d\n",
2738 cpi->common.current_video_frame, cpi->rc.this_frame_target,
2739 cpi->rc.projected_frame_size,
2740 cpi->rc.projected_frame_size / cpi->common.MBs,
2741 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
2742 cpi->rc.total_target_vs_actual,
2743 (cpi->oxcf.starting_buffer_level - cpi->rc.bits_off_target),
2744 cpi->rc.total_actual_bits, cm->base_qindex,
2745 vp9_convert_qindex_to_q(cm->base_qindex),
2746 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
2748 vp9_convert_qindex_to_q(cpi->rc.ni_av_qi),
2749 vp9_convert_qindex_to_q(cpi->cq_target_quality),
2750 cpi->refresh_last_frame, cpi->refresh_golden_frame,
2751 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
2752 cpi->twopass.bits_left,
2753 cpi->twopass.total_left_stats.coded_error,
2754 cpi->twopass.bits_left /
2755 (1 + cpi->twopass.total_left_stats.coded_error),
2756 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
2757 cpi->twopass.kf_zeromotion_pct);
2762 FILE *const fmodes = fopen("Modes.stt", "a");
2765 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
2766 cm->frame_type, cpi->refresh_golden_frame,
2767 cpi->refresh_alt_ref_frame);
2769 for (i = 0; i < MAX_MODES; ++i)
2770 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
2771 for (i = 0; i < MAX_REFS; ++i)
2772 fprintf(fmodes, "%5d ", cpi->sub8x8_mode_chosen_counts[i]);
2774 fprintf(fmodes, "\n");
2781 static void encode_without_recode_loop(VP9_COMP *cpi,
2785 VP9_COMMON *const cm = &cpi->common;
2786 vp9_clear_system_state(); // __asm emms;
2787 vp9_set_quantizer(cpi, q);
2789 // Set up entropy context depending on frame type. The decoder mandates
2790 // the use of the default context, index 0, for keyframes and inter
2791 // frames where the error_resilient_mode or intra_only flag is set. For
2792 // other inter-frames the encoder currently uses only two contexts;
2793 // context 1 for ALTREF frames and context 0 for the others.
2794 if (cm->frame_type == KEY_FRAME) {
2795 vp9_setup_key_frame(cpi);
2797 if (!cm->intra_only && !cm->error_resilient_mode) {
2798 cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame;
2800 vp9_setup_inter_frame(cpi);
2802 // Variance adaptive and in frame q adjustment experiments are mutually
2804 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2805 vp9_vaq_frame_setup(cpi);
2806 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2807 setup_in_frame_q_adj(cpi);
2809 // transform / motion compensation build reconstruction frame
2810 vp9_encode_frame(cpi);
2812 // Update the skip mb flag probabilities based on the distribution
2813 // seen in the last encoder iteration.
2814 // update_base_skip_probs(cpi);
2815 vp9_clear_system_state(); // __asm emms;
2818 static void encode_with_recode_loop(VP9_COMP *cpi,
2824 VP9_COMMON *const cm = &cpi->common;
2827 int overshoot_seen = 0;
2828 int undershoot_seen = 0;
2829 int q_low = bottom_index, q_high = top_index;
2830 int frame_over_shoot_limit;
2831 int frame_under_shoot_limit;
2833 // Decide frame size bounds
2834 vp9_rc_compute_frame_size_bounds(cpi, cpi->rc.this_frame_target,
2835 &frame_under_shoot_limit,
2836 &frame_over_shoot_limit);
2839 vp9_clear_system_state(); // __asm emms;
2841 vp9_set_quantizer(cpi, q);
2843 if (loop_count == 0) {
2844 // Set up entropy context depending on frame type. The decoder mandates
2845 // the use of the default context, index 0, for keyframes and inter
2846 // frames where the error_resilient_mode or intra_only flag is set. For
2847 // other inter-frames the encoder currently uses only two contexts;
2848 // context 1 for ALTREF frames and context 0 for the others.
2849 if (cm->frame_type == KEY_FRAME) {
2850 vp9_setup_key_frame(cpi);
2852 if (!cm->intra_only && !cm->error_resilient_mode) {
2853 cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame;
2855 vp9_setup_inter_frame(cpi);
2859 // Variance adaptive and in frame q adjustment experiments are mutually
2861 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2862 vp9_vaq_frame_setup(cpi);
2863 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2864 setup_in_frame_q_adj(cpi);
2867 // transform / motion compensation build reconstruction frame
2868 vp9_encode_frame(cpi);
2870 // Update the skip mb flag probabilities based on the distribution
2871 // seen in the last encoder iteration.
2872 // update_base_skip_probs(cpi);
2874 vp9_clear_system_state(); // __asm emms;
2876 // Dummy pack of the bitstream using up to date stats to get an
2877 // accurate estimate of output frame size to determine if we need
2879 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
2880 vp9_save_coding_context(cpi);
2881 cpi->dummy_packing = 1;
2882 if (!cpi->sf.use_pick_mode)
2883 vp9_pack_bitstream(cpi, dest, size);
2885 cpi->rc.projected_frame_size = (*size) << 3;
2886 vp9_restore_coding_context(cpi);
2888 if (frame_over_shoot_limit == 0)
2889 frame_over_shoot_limit = 1;
2892 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
2895 if ((cm->frame_type == KEY_FRAME) &&
2896 cpi->rc.this_key_frame_forced &&
2897 (cpi->rc.projected_frame_size < cpi->rc.max_frame_bandwidth)) {
2899 int kf_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
2901 int high_err_target = cpi->ambient_err;
2902 int low_err_target = cpi->ambient_err >> 1;
2904 // Prevent possible divide by zero error below for perfect KF
2907 // The key frame is not good enough or we can afford
2908 // to make it better without undue risk of popping.
2909 if ((kf_err > high_err_target &&
2910 cpi->rc.projected_frame_size <= frame_over_shoot_limit) ||
2911 (kf_err > low_err_target &&
2912 cpi->rc.projected_frame_size <= frame_under_shoot_limit)) {
2914 q_high = q > q_low ? q - 1 : q_low;
2917 q = (q * high_err_target) / kf_err;
2918 q = MIN(q, (q_high + q_low) >> 1);
2919 } else if (kf_err < low_err_target &&
2920 cpi->rc.projected_frame_size >= frame_under_shoot_limit) {
2921 // The key frame is much better than the previous frame
2923 q_low = q < q_high ? q + 1 : q_high;
2926 q = (q * low_err_target) / kf_err;
2927 q = MIN(q, (q_high + q_low + 1) >> 1);
2930 // Clamp Q to upper and lower limits:
2931 q = clamp(q, q_low, q_high);
2934 } else if (recode_loop_test(
2935 cpi, frame_over_shoot_limit, frame_under_shoot_limit,
2936 q, MAX(q_high, top_index), bottom_index)) {
2937 // Is the projected frame size out of range and are we allowed
2938 // to attempt to recode.
2942 // Frame size out of permitted range:
2943 // Update correction factor & compute new Q to try...
2945 // Frame is too large
2946 if (cpi->rc.projected_frame_size > cpi->rc.this_frame_target) {
2947 // Special case if the projected size is > the max allowed.
2948 if (cpi->rc.projected_frame_size >= cpi->rc.max_frame_bandwidth)
2949 q_high = cpi->rc.worst_quality;
2951 // Raise Qlow as to at least the current value
2952 q_low = q < q_high ? q + 1 : q_high;
2954 if (undershoot_seen || loop_count > 1) {
2955 // Update rate_correction_factor unless
2956 vp9_rc_update_rate_correction_factors(cpi, 1);
2958 q = (q_high + q_low + 1) / 2;
2960 // Update rate_correction_factor unless
2961 vp9_rc_update_rate_correction_factors(cpi, 0);
2963 q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
2964 bottom_index, MAX(q_high, top_index));
2966 while (q < q_low && retries < 10) {
2967 vp9_rc_update_rate_correction_factors(cpi, 0);
2968 q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
2969 bottom_index, MAX(q_high, top_index));
2976 // Frame is too small
2977 q_high = q > q_low ? q - 1 : q_low;
2979 if (overshoot_seen || loop_count > 1) {
2980 vp9_rc_update_rate_correction_factors(cpi, 1);
2981 q = (q_high + q_low) / 2;
2983 vp9_rc_update_rate_correction_factors(cpi, 0);
2984 q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
2985 bottom_index, top_index);
2986 // Special case reset for qlow for constrained quality.
2987 // This should only trigger where there is very substantial
2988 // undershoot on a frame and the auto cq level is above
2989 // the user passsed in value.
2990 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
2995 while (q > q_high && retries < 10) {
2996 vp9_rc_update_rate_correction_factors(cpi, 0);
2997 q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
2998 bottom_index, top_index);
3003 undershoot_seen = 1;
3006 // Clamp Q to upper and lower limits:
3007 q = clamp(q, q_low, q_high);
3015 // Special case for overlay frame.
3016 if (cpi->rc.is_src_frame_alt_ref &&
3017 (cpi->rc.projected_frame_size < cpi->rc.max_frame_bandwidth))
3023 #if CONFIG_INTERNAL_STATS
3024 cpi->tot_recode_hits++;
3030 static void get_ref_frame_flags(VP9_COMP *cpi) {
3031 if (cpi->refresh_last_frame & cpi->refresh_golden_frame)
3032 cpi->gold_is_last = 1;
3033 else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame)
3034 cpi->gold_is_last = 0;
3036 if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame)
3037 cpi->alt_is_last = 1;
3038 else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame)
3039 cpi->alt_is_last = 0;
3041 if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame)
3042 cpi->gold_is_alt = 1;
3043 else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame)
3044 cpi->gold_is_alt = 0;
3046 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3048 if (cpi->gold_is_last)
3049 cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
3051 if (cpi->alt_is_last)
3052 cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
3054 if (cpi->gold_is_alt)
3055 cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
3058 static void set_ext_overrides(VP9_COMP *cpi) {
3059 // Overrides the defaults with the externally supplied values with
3060 // vp9_update_reference() and vp9_update_entropy() calls
3061 // Note: The overrides are valid only for the next frame passed
3062 // to encode_frame_to_data_rate() function
3063 if (cpi->ext_refresh_frame_context_pending) {
3064 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
3065 cpi->ext_refresh_frame_context_pending = 0;
3067 if (cpi->ext_refresh_frame_flags_pending) {
3068 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
3069 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
3070 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
3071 cpi->ext_refresh_frame_flags_pending = 0;
3075 static void encode_frame_to_data_rate(VP9_COMP *cpi,
3078 unsigned int *frame_flags) {
3079 VP9_COMMON *const cm = &cpi->common;
3085 SPEED_FEATURES *const sf = &cpi->sf;
3086 unsigned int max_mv_def = MIN(cm->width, cm->height);
3087 struct segmentation *const seg = &cm->seg;
3089 set_ext_overrides(cpi);
3091 /* Scale the source buffer, if required. */
3092 if (cm->mi_cols * MI_SIZE != cpi->un_scaled_source->y_width ||
3093 cm->mi_rows * MI_SIZE != cpi->un_scaled_source->y_height) {
3094 scale_and_extend_frame_nonnormative(cpi->un_scaled_source,
3095 &cpi->scaled_source);
3096 cpi->Source = &cpi->scaled_source;
3098 cpi->Source = cpi->un_scaled_source;
3100 scale_references(cpi);
3102 vp9_clear_system_state();
3104 // Enable or disable mode based tweaking of the zbin.
3105 // For 2 pass only used where GF/ARF prediction quality
3106 // is above a threshold.
3107 cpi->zbin_mode_boost = 0;
3108 cpi->zbin_mode_boost_enabled = 0;
3110 // Current default encoder behavior for the altref sign bias.
3111 cm->ref_frame_sign_bias[ALTREF_FRAME] = cpi->rc.source_alt_ref_active;
3113 // Set default state for segment based loop filter update flags.
3114 cm->lf.mode_ref_delta_update = 0;
3116 // Initialize cpi->mv_step_param to default based on max resolution.
3117 cpi->mv_step_param = vp9_init_search_range(cpi, max_mv_def);
3118 // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate.
3119 if (sf->auto_mv_step_size) {
3120 if (frame_is_intra_only(cm)) {
3121 // Initialize max_mv_magnitude for use in the first INTER frame
3122 // after a key/intra-only frame.
3123 cpi->max_mv_magnitude = max_mv_def;
3126 // Allow mv_steps to correspond to twice the max mv magnitude found
3127 // in the previous frame, capped by the default max_mv_magnitude based
3129 cpi->mv_step_param = vp9_init_search_range(cpi, MIN(max_mv_def, 2 *
3130 cpi->max_mv_magnitude));
3131 cpi->max_mv_magnitude = 0;
3135 // Set various flags etc to special state if it is a key frame.
3136 if (frame_is_intra_only(cm)) {
3137 vp9_setup_key_frame(cpi);
3138 // Reset the loop filter deltas and segmentation map.
3139 vp9_reset_segment_features(&cm->seg);
3141 // If segmentation is enabled force a map update for key frames.
3143 seg->update_map = 1;
3144 seg->update_data = 1;
3147 // The alternate reference frame cannot be active for a key frame.
3148 cpi->rc.source_alt_ref_active = 0;
3150 cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0);
3151 cm->frame_parallel_decoding_mode =
3152 (cpi->oxcf.frame_parallel_decoding_mode != 0);
3153 if (cm->error_resilient_mode) {
3154 cm->frame_parallel_decoding_mode = 1;
3155 cm->reset_frame_context = 0;
3156 cm->refresh_frame_context = 0;
3157 } else if (cm->intra_only) {
3158 // Only reset the current context.
3159 cm->reset_frame_context = 2;
3163 // Configure experimental use of segmentation for enhanced coding of
3164 // static regions if indicated.
3165 // Only allowed in second pass of two pass (as requires lagged coding)
3166 // and if the relevant speed feature flag is set.
3167 if (cpi->pass == 2 && cpi->sf.static_segmentation)
3168 configure_static_seg_features(cpi);
3170 // For 1 pass CBR, check if we are dropping this frame.
3171 // Never drop on key frame.
3172 if (cpi->pass == 0 &&
3173 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
3174 cm->frame_type != KEY_FRAME) {
3175 if (vp9_rc_drop_frame(cpi)) {
3176 vp9_rc_postencode_update_drop_frame(cpi);
3177 ++cm->current_video_frame;
3182 vp9_clear_system_state();
3184 vp9_zero(cpi->rd_tx_select_threshes);
3186 #if CONFIG_VP9_POSTPROC
3187 if (cpi->oxcf.noise_sensitivity > 0) {
3189 switch (cpi->oxcf.noise_sensitivity) {
3207 vp9_denoise(cpi->Source, cpi->Source, l);
3211 #ifdef OUTPUT_YUV_SRC
3212 vp9_write_yuv_frame(cpi->Source);
3215 // Decide q and q bounds.
3216 q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index);
3218 if (!frame_is_intra_only(cm)) {
3219 cm->interp_filter = DEFAULT_INTERP_FILTER;
3220 /* TODO: Decide this more intelligently */
3221 set_high_precision_mv(cpi, (q < HIGH_PRECISION_MV_QTHRESH));
3224 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
3225 encode_without_recode_loop(cpi, size, dest, q);
3227 encode_with_recode_loop(cpi, size, dest, q, bottom_index, top_index);
3230 // Special case code to reduce pulsing when key frames are forced at a
3231 // fixed interval. Note the reconstruction error if it is the frame before
3232 // the force key frame
3233 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
3234 cpi->ambient_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
3237 // If the encoder forced a KEY_FRAME decision
3238 if (cm->frame_type == KEY_FRAME)
3239 cpi->refresh_last_frame = 1;
3241 cm->frame_to_show = get_frame_new_buffer(cm);
3243 #if WRITE_RECON_BUFFER
3245 write_cx_frame_to_file(cm->frame_to_show,
3246 cm->current_video_frame);
3248 write_cx_frame_to_file(cm->frame_to_show,
3249 cm->current_video_frame + 1000);
3252 // Pick the loop filter level for the frame.
3253 loopfilter_frame(cpi, cm);
3255 #if WRITE_RECON_BUFFER
3257 write_cx_frame_to_file(cm->frame_to_show,
3258 cm->current_video_frame + 2000);
3260 write_cx_frame_to_file(cm->frame_to_show,
3261 cm->current_video_frame + 3000);
3264 // build the bitstream
3265 cpi->dummy_packing = 0;
3266 vp9_pack_bitstream(cpi, dest, size);
3268 if (cm->seg.update_map)
3269 update_reference_segmentation_map(cpi);
3271 release_scaled_references(cpi);
3272 update_reference_frames(cpi);
3274 for (t = TX_4X4; t <= TX_32X32; t++)
3275 full_to_model_counts(cm->counts.coef[t], cpi->coef_counts[t]);
3277 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
3278 vp9_adapt_coef_probs(cm);
3280 if (!frame_is_intra_only(cm)) {
3281 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
3282 vp9_adapt_mode_probs(cm);
3283 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
3288 output_frame_level_debug_stats(cpi);
3290 if (cpi->refresh_golden_frame == 1)
3291 cm->frame_flags |= FRAMEFLAGS_GOLDEN;
3293 cm->frame_flags &= ~FRAMEFLAGS_GOLDEN;
3295 if (cpi->refresh_alt_ref_frame == 1)
3296 cm->frame_flags |= FRAMEFLAGS_ALTREF;
3298 cm->frame_flags &= ~FRAMEFLAGS_ALTREF;
3300 get_ref_frame_flags(cpi);
3302 vp9_rc_postencode_update(cpi, *size);
3304 if (cm->frame_type == KEY_FRAME) {
3305 // Tell the caller that the frame was coded as a key frame
3306 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
3308 #if CONFIG_MULTIPLE_ARF
3309 // Reset the sequence number.
3310 if (cpi->multi_arf_enabled) {
3311 cpi->sequence_number = 0;
3312 cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
3313 cpi->new_frame_coding_order_period = -1;
3317 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
3319 #if CONFIG_MULTIPLE_ARF
3320 /* Increment position in the coded frame sequence. */
3321 if (cpi->multi_arf_enabled) {
3322 ++cpi->sequence_number;
3323 if (cpi->sequence_number >= cpi->frame_coding_order_period) {
3324 cpi->sequence_number = 0;
3325 cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
3326 cpi->new_frame_coding_order_period = -1;
3328 cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
3329 assert(cpi->this_frame_weight >= 0);
3334 // Clear the one shot update flags for segmentation map and mode/ref loop
3336 cm->seg.update_map = 0;
3337 cm->seg.update_data = 0;
3338 cm->lf.mode_ref_delta_update = 0;
3340 // keep track of the last coded dimensions
3341 cm->last_width = cm->width;
3342 cm->last_height = cm->height;
3344 // reset to normal state now that we are done.
3345 if (!cm->show_existing_frame)
3346 cm->last_show_frame = cm->show_frame;
3348 if (cm->show_frame) {
3349 // current mip will be the prev_mip for the next frame
3350 MODE_INFO *temp = cm->prev_mip;
3351 MODE_INFO **temp2 = cm->prev_mi_grid_base;
3352 cm->prev_mip = cm->mip;
3354 cm->prev_mi_grid_base = cm->mi_grid_base;
3355 cm->mi_grid_base = temp2;
3357 // update the upper left visible macroblock ptrs
3358 cm->mi = cm->mip + cm->mode_info_stride + 1;
3359 cm->mi_grid_visible = cm->mi_grid_base + cm->mode_info_stride + 1;
3361 cpi->mb.e_mbd.mi_8x8 = cm->mi_grid_visible;
3362 cpi->mb.e_mbd.mi_8x8[0] = cm->mi;
3364 // Don't increment frame counters if this was an altref buffer
3365 // update not a real frame
3366 ++cm->current_video_frame;
3370 cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1;
3371 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1;
3374 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3375 unsigned int *frame_flags) {
3376 vp9_rc_get_svc_params(cpi);
3377 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3380 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3381 unsigned int *frame_flags) {
3382 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3383 vp9_rc_get_one_pass_cbr_params(cpi);
3385 vp9_rc_get_one_pass_vbr_params(cpi);
3387 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3390 static void Pass1Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3391 unsigned int *frame_flags) {
3396 vp9_rc_get_first_pass_params(cpi);
3397 vp9_set_quantizer(cpi, find_fp_qindex());
3398 vp9_first_pass(cpi);
3401 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
3402 uint8_t *dest, unsigned int *frame_flags) {
3403 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
3405 vp9_rc_get_second_pass_params(cpi);
3406 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3408 vp9_twopass_postencode_update(cpi, *size);
3411 static void check_initial_width(VP9_COMP *cpi, int subsampling_x,
3412 int subsampling_y) {
3413 VP9_COMMON *const cm = &cpi->common;
3414 if (!cpi->initial_width) {
3415 cm->subsampling_x = subsampling_x;
3416 cm->subsampling_y = subsampling_y;
3417 alloc_raw_frame_buffers(cpi);
3418 cpi->initial_width = cm->width;
3419 cpi->initial_height = cm->height;
3424 int vp9_receive_raw_frame(VP9_PTR ptr, unsigned int frame_flags,
3425 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3427 VP9_COMP *cpi = (VP9_COMP *) ptr;
3428 VP9_COMMON *cm = &cpi->common;
3429 struct vpx_usec_timer timer;
3431 const int subsampling_x = sd->uv_width < sd->y_width;
3432 const int subsampling_y = sd->uv_height < sd->y_height;
3434 check_initial_width(cpi, subsampling_x, subsampling_y);
3435 vpx_usec_timer_start(&timer);
3436 if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags,
3437 cpi->active_map_enabled ? cpi->active_map : NULL))
3439 vpx_usec_timer_mark(&timer);
3440 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
3442 if (cm->version == 0 && (subsampling_x != 1 || subsampling_y != 1)) {
3443 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
3444 "Non-4:2:0 color space requires profile >= 1");
3452 static int frame_is_reference(const VP9_COMP *cpi) {
3453 const VP9_COMMON *cm = &cpi->common;
3455 return cm->frame_type == KEY_FRAME ||
3456 cpi->refresh_last_frame ||
3457 cpi->refresh_golden_frame ||
3458 cpi->refresh_alt_ref_frame ||
3459 cm->refresh_frame_context ||
3460 cm->lf.mode_ref_delta_update ||
3461 cm->seg.update_map ||
3462 cm->seg.update_data;
3465 #if CONFIG_MULTIPLE_ARF
3466 int is_next_frame_arf(VP9_COMP *cpi) {
3467 // Negative entry in frame_coding_order indicates an ARF at this position.
3468 return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0;
3472 void adjust_frame_rate(VP9_COMP *cpi) {
3473 int64_t this_duration;
3476 if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
3477 this_duration = cpi->source->ts_end - cpi->source->ts_start;
3480 int64_t last_duration = cpi->last_end_time_stamp_seen
3481 - cpi->last_time_stamp_seen;
3483 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
3485 // do a step update if the duration changes by 10%
3487 step = (int)((this_duration - last_duration) * 10 / last_duration);
3490 if (this_duration) {
3492 vp9_new_framerate(cpi, 10000000.0 / this_duration);
3494 // Average this frame's rate into the last second's average
3495 // frame rate. If we haven't seen 1 second yet, then average
3496 // over the whole interval seen.
3497 const double interval = MIN((double)(cpi->source->ts_end
3498 - cpi->first_time_stamp_ever), 10000000.0);
3499 double avg_duration = 10000000.0 / cpi->oxcf.framerate;
3500 avg_duration *= (interval - avg_duration + this_duration);
3501 avg_duration /= interval;
3503 vp9_new_framerate(cpi, 10000000.0 / avg_duration);
3506 cpi->last_time_stamp_seen = cpi->source->ts_start;
3507 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
3510 int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags,
3511 size_t *size, uint8_t *dest,
3512 int64_t *time_stamp, int64_t *time_end, int flush) {
3513 VP9_COMP *cpi = (VP9_COMP *) ptr;
3514 VP9_COMMON *cm = &cpi->common;
3515 MACROBLOCKD *xd = &cpi->mb.e_mbd;
3516 struct vpx_usec_timer cmptimer;
3517 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
3518 MV_REFERENCE_FRAME ref_frame;
3523 vpx_usec_timer_start(&cmptimer);
3527 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
3530 cm->reset_frame_context = 0;
3531 cm->refresh_frame_context = 1;
3532 cpi->refresh_last_frame = 1;
3533 cpi->refresh_golden_frame = 0;
3534 cpi->refresh_alt_ref_frame = 0;
3536 // Should we code an alternate reference frame.
3537 if (cpi->oxcf.play_alternate && cpi->rc.source_alt_ref_pending) {
3540 #if CONFIG_MULTIPLE_ARF
3541 assert(!cpi->multi_arf_enabled ||
3542 cpi->frame_coding_order[cpi->sequence_number] < 0);
3544 if (cpi->multi_arf_enabled && (cpi->pass == 2))
3545 frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number])
3546 - cpi->next_frame_in_order;
3549 frames_to_arf = cpi->rc.frames_till_gf_update_due;
3551 assert(frames_to_arf <= cpi->rc.frames_to_key);
3553 if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) {
3554 #if CONFIG_MULTIPLE_ARF
3555 cpi->alt_ref_source[cpi->arf_buffered] = cpi->source;
3557 cpi->alt_ref_source = cpi->source;
3560 if (cpi->oxcf.arnr_max_frames > 0) {
3561 // Produce the filtered ARF frame.
3562 // TODO(agrange) merge these two functions.
3563 vp9_configure_arnr_filter(cpi, frames_to_arf, cpi->rc.gfu_boost);
3564 vp9_temporal_filter_prepare(cpi, frames_to_arf);
3565 vp9_extend_frame_borders(&cpi->alt_ref_buffer,
3566 cm->subsampling_x, cm->subsampling_y);
3567 force_src_buffer = &cpi->alt_ref_buffer;
3571 cpi->refresh_alt_ref_frame = 1;
3572 cpi->refresh_golden_frame = 0;
3573 cpi->refresh_last_frame = 0;
3574 cpi->rc.is_src_frame_alt_ref = 0;
3576 #if CONFIG_MULTIPLE_ARF
3577 if (!cpi->multi_arf_enabled)
3579 cpi->rc.source_alt_ref_pending = 0;
3581 cpi->rc.source_alt_ref_pending = 0;
3586 #if CONFIG_MULTIPLE_ARF
3589 if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) {
3593 #if CONFIG_MULTIPLE_ARF
3594 // Is this frame the ARF overlay.
3595 cpi->rc.is_src_frame_alt_ref = 0;
3596 for (i = 0; i < cpi->arf_buffered; ++i) {
3597 if (cpi->source == cpi->alt_ref_source[i]) {
3598 cpi->rc.is_src_frame_alt_ref = 1;
3599 cpi->refresh_golden_frame = 1;
3604 cpi->rc.is_src_frame_alt_ref = cpi->alt_ref_source
3605 && (cpi->source == cpi->alt_ref_source);
3607 if (cpi->rc.is_src_frame_alt_ref) {
3608 // Current frame is an ARF overlay frame.
3609 #if CONFIG_MULTIPLE_ARF
3610 cpi->alt_ref_source[i] = NULL;
3612 cpi->alt_ref_source = NULL;
3614 // Don't refresh the last buffer for an ARF overlay frame. It will
3615 // become the GF so preserve last as an alternative prediction option.
3616 cpi->refresh_last_frame = 0;
3618 #if CONFIG_MULTIPLE_ARF
3619 ++cpi->next_frame_in_order;
3625 cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
3626 : &cpi->source->img;
3627 *time_stamp = cpi->source->ts_start;
3628 *time_end = cpi->source->ts_end;
3629 *frame_flags = cpi->source->flags;
3631 #if CONFIG_MULTIPLE_ARF
3632 if ((cm->frame_type != KEY_FRAME) && (cpi->pass == 2))
3633 cpi->rc.source_alt_ref_pending = is_next_frame_arf(cpi);
3637 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
3638 vp9_end_first_pass(cpi); /* get last stats packet */
3639 cpi->twopass.first_pass_done = 1;
3644 if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
3645 cpi->first_time_stamp_ever = cpi->source->ts_start;
3646 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
3649 // adjust frame rates based on timestamps given
3650 if (cm->show_frame) {
3651 adjust_frame_rate(cpi);
3654 if (cpi->svc.number_temporal_layers > 1 &&
3655 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3656 update_layer_framerate(cpi);
3657 restore_layer_context(cpi);
3660 // start with a 0 size frame
3663 // Clear down mmx registers
3664 vp9_clear_system_state(); // __asm emms;
3666 /* find a free buffer for the new frame, releasing the reference previously
3669 cm->frame_bufs[cm->new_fb_idx].ref_count--;
3670 cm->new_fb_idx = get_free_fb(cm);
3672 #if CONFIG_MULTIPLE_ARF
3673 /* Set up the correct ARF frame. */
3674 if (cpi->refresh_alt_ref_frame) {
3675 ++cpi->arf_buffered;
3677 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
3679 cpi->alt_fb_idx = cpi->arf_buffer_idx[cpi->sequence_number];
3683 cm->frame_flags = *frame_flags;
3685 // Reset the frame pointers to the current frame size
3686 vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
3687 cm->width, cm->height,
3688 cm->subsampling_x, cm->subsampling_y,
3689 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
3691 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3692 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
3693 YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf;
3694 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
3697 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
3698 buf->y_crop_width, buf->y_crop_height,
3699 cm->width, cm->height);
3701 if (vp9_is_scaled(&ref_buf->sf))
3702 vp9_extend_frame_borders(buf, cm->subsampling_x, cm->subsampling_y);
3705 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3706 xd->interp_kernel = vp9_get_interp_kernel(
3707 DEFAULT_INTERP_FILTER == SWITCHABLE ? EIGHTTAP : DEFAULT_INTERP_FILTER);
3709 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3714 SvcEncode(cpi, size, dest, frame_flags);
3715 } else if (cpi->pass == 1) {
3716 Pass1Encode(cpi, size, dest, frame_flags);
3717 } else if (cpi->pass == 2) {
3718 Pass2Encode(cpi, size, dest, frame_flags);
3721 Pass0Encode(cpi, size, dest, frame_flags);
3724 if (cm->refresh_frame_context)
3725 cm->frame_contexts[cm->frame_context_idx] = cm->fc;
3727 // Frame was dropped, release scaled references.
3729 release_scaled_references(cpi);
3733 cpi->droppable = !frame_is_reference(cpi);
3736 // Save layer specific state.
3737 if (cpi->svc.number_temporal_layers > 1 &&
3738 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3739 save_layer_context(cpi);
3742 vpx_usec_timer_mark(&cmptimer);
3743 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
3745 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
3746 generate_psnr_packet(cpi);
3748 #if CONFIG_INTERNAL_STATS
3750 if (cpi->pass != 1) {
3751 cpi->bytes += *size;
3753 if (cm->show_frame) {
3756 if (cpi->b_calculate_psnr) {
3757 YV12_BUFFER_CONFIG *orig = cpi->Source;
3758 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
3759 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
3761 calc_psnr(orig, recon, &psnr);
3763 cpi->total += psnr.psnr[0];
3764 cpi->total_y += psnr.psnr[1];
3765 cpi->total_u += psnr.psnr[2];
3766 cpi->total_v += psnr.psnr[3];
3767 cpi->total_sq_error += psnr.sse[0];
3768 cpi->total_samples += psnr.samples[0];
3772 double frame_ssim2 = 0, weight = 0;
3773 #if CONFIG_VP9_POSTPROC
3774 vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
3775 cm->lf.filter_level * 10 / 6);
3777 vp9_clear_system_state();
3779 calc_psnr(orig, pp, &psnr2);
3781 cpi->totalp += psnr2.psnr[0];
3782 cpi->totalp_y += psnr2.psnr[1];
3783 cpi->totalp_u += psnr2.psnr[2];
3784 cpi->totalp_v += psnr2.psnr[3];
3785 cpi->totalp_sq_error += psnr2.sse[0];
3786 cpi->totalp_samples += psnr2.samples[0];
3788 frame_ssim2 = vp9_calc_ssim(orig, recon, 1, &weight);
3790 cpi->summed_quality += frame_ssim2 * weight;
3791 cpi->summed_weights += weight;
3793 frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, 1, &weight);
3795 cpi->summedp_quality += frame_ssim2 * weight;
3796 cpi->summedp_weights += weight;
3799 FILE *f = fopen("q_used.stt", "a");
3800 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3801 cpi->common.current_video_frame, y2, u2, v2,
3802 frame_psnr2, frame_ssim2);
3809 if (cpi->b_calculate_ssimg) {
3810 double y, u, v, frame_all;
3811 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
3812 cpi->total_ssimg_y += y;
3813 cpi->total_ssimg_u += u;
3814 cpi->total_ssimg_v += v;
3815 cpi->total_ssimg_all += frame_all;
3824 int vp9_get_preview_raw_frame(VP9_PTR comp, YV12_BUFFER_CONFIG *dest,
3825 vp9_ppflags_t *flags) {
3826 VP9_COMP *cpi = (VP9_COMP *) comp;
3828 if (!cpi->common.show_frame) {
3832 #if CONFIG_VP9_POSTPROC
3833 ret = vp9_post_proc_frame(&cpi->common, dest, flags);
3836 if (cpi->common.frame_to_show) {
3837 *dest = *cpi->common.frame_to_show;
3838 dest->y_width = cpi->common.width;
3839 dest->y_height = cpi->common.height;
3840 dest->uv_width = cpi->common.width >> cpi->common.subsampling_x;
3841 dest->uv_height = cpi->common.height >> cpi->common.subsampling_y;
3847 #endif // !CONFIG_VP9_POSTPROC
3848 vp9_clear_system_state();
3853 int vp9_set_roimap(VP9_PTR comp, unsigned char *map, unsigned int rows,
3854 unsigned int cols, int delta_q[MAX_SEGMENTS],
3855 int delta_lf[MAX_SEGMENTS],
3856 unsigned int threshold[MAX_SEGMENTS]) {
3857 VP9_COMP *cpi = (VP9_COMP *) comp;
3858 signed char feature_data[SEG_LVL_MAX][MAX_SEGMENTS];
3859 struct segmentation *seg = &cpi->common.seg;
3862 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
3866 vp9_disable_segmentation((VP9_PTR)cpi);
3870 // Set the segmentation Map
3871 vp9_set_segmentation_map((VP9_PTR)cpi, map);
3873 // Activate segmentation.
3874 vp9_enable_segmentation((VP9_PTR)cpi);
3876 // Set up the quant, LF and breakout threshold segment data
3877 for (i = 0; i < MAX_SEGMENTS; i++) {
3878 feature_data[SEG_LVL_ALT_Q][i] = delta_q[i];
3879 feature_data[SEG_LVL_ALT_LF][i] = delta_lf[i];
3880 cpi->segment_encode_breakout[i] = threshold[i];
3883 // Enable the loop and quant changes in the feature mask
3884 for (i = 0; i < MAX_SEGMENTS; i++) {
3886 vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
3888 vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q);
3891 vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF);
3893 vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF);
3896 // Initialize the feature data structure
3897 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
3898 vp9_set_segment_data((VP9_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
3903 int vp9_set_active_map(VP9_PTR comp, unsigned char *map,
3904 unsigned int rows, unsigned int cols) {
3905 VP9_COMP *cpi = (VP9_COMP *) comp;
3907 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
3909 vpx_memcpy(cpi->active_map, map, rows * cols);
3910 cpi->active_map_enabled = 1;
3912 cpi->active_map_enabled = 0;
3917 // cpi->active_map_enabled = 0;
3922 int vp9_set_internal_size(VP9_PTR comp,
3923 VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
3924 VP9_COMP *cpi = (VP9_COMP *) comp;
3925 VP9_COMMON *cm = &cpi->common;
3926 int hr = 0, hs = 0, vr = 0, vs = 0;
3928 if (horiz_mode > ONETWO || vert_mode > ONETWO)
3931 Scale2Ratio(horiz_mode, &hr, &hs);
3932 Scale2Ratio(vert_mode, &vr, &vs);
3934 // always go to the next whole number
3935 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
3936 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
3938 assert(cm->width <= cpi->initial_width);
3939 assert(cm->height <= cpi->initial_height);
3940 update_frame_size(cpi);
3944 int vp9_set_size_literal(VP9_PTR comp, unsigned int width,
3945 unsigned int height) {
3946 VP9_COMP *cpi = (VP9_COMP *)comp;
3947 VP9_COMMON *cm = &cpi->common;
3949 check_initial_width(cpi, 1, 1);
3953 if (cm->width * 5 < cpi->initial_width) {
3954 cm->width = cpi->initial_width / 5 + 1;
3955 printf("Warning: Desired width too small, changed to %d\n", cm->width);
3957 if (cm->width > cpi->initial_width) {
3958 cm->width = cpi->initial_width;
3959 printf("Warning: Desired width too large, changed to %d\n", cm->width);
3964 cm->height = height;
3965 if (cm->height * 5 < cpi->initial_height) {
3966 cm->height = cpi->initial_height / 5 + 1;
3967 printf("Warning: Desired height too small, changed to %d\n", cm->height);
3969 if (cm->height > cpi->initial_height) {
3970 cm->height = cpi->initial_height;
3971 printf("Warning: Desired height too large, changed to %d\n", cm->height);
3975 assert(cm->width <= cpi->initial_width);
3976 assert(cm->height <= cpi->initial_height);
3977 update_frame_size(cpi);
3981 void vp9_set_svc(VP9_PTR comp, int use_svc) {
3982 VP9_COMP *cpi = (VP9_COMP *)comp;
3983 cpi->use_svc = use_svc;
3987 int vp9_calc_ss_err(const YV12_BUFFER_CONFIG *source,
3988 const YV12_BUFFER_CONFIG *reference) {
3992 const uint8_t *src = source->y_buffer;
3993 const uint8_t *ref = reference->y_buffer;
3995 // Loop through the Y plane raw and reconstruction data summing
3996 // (square differences)
3997 for (i = 0; i < source->y_height; i += 16) {
3998 for (j = 0; j < source->y_width; j += 16) {
4000 total += vp9_mse16x16(src + j, source->y_stride,
4001 ref + j, reference->y_stride, &sse);
4004 src += 16 * source->y_stride;
4005 ref += 16 * reference->y_stride;
4012 int vp9_get_quantizer(VP9_PTR c) {
4013 return ((VP9_COMP *)c)->common.base_qindex;