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 "./vp9_rtcd.h"
16 #include "./vpx_config.h"
17 #include "./vpx_dsp_rtcd.h"
18 #include "./vpx_scale_rtcd.h"
19 #include "vpx_dsp/psnr.h"
20 #include "vpx_dsp/vpx_dsp_common.h"
21 #include "vpx_dsp/vpx_filter.h"
22 #if CONFIG_INTERNAL_STATS
23 #include "vpx_dsp/ssim.h"
25 #include "vpx_ports/mem.h"
26 #include "vpx_ports/system_state.h"
27 #include "vpx_ports/vpx_timer.h"
29 #include "vp9/common/vp9_alloccommon.h"
30 #include "vp9/common/vp9_filter.h"
31 #include "vp9/common/vp9_idct.h"
32 #if CONFIG_VP9_POSTPROC
33 #include "vp9/common/vp9_postproc.h"
35 #include "vp9/common/vp9_reconinter.h"
36 #include "vp9/common/vp9_reconintra.h"
37 #include "vp9/common/vp9_tile_common.h"
39 #include "vp9/encoder/vp9_alt_ref_aq.h"
40 #include "vp9/encoder/vp9_aq_360.h"
41 #include "vp9/encoder/vp9_aq_complexity.h"
42 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
43 #include "vp9/encoder/vp9_aq_variance.h"
44 #include "vp9/encoder/vp9_bitstream.h"
45 #include "vp9/encoder/vp9_context_tree.h"
46 #include "vp9/encoder/vp9_encodeframe.h"
47 #include "vp9/encoder/vp9_encodemv.h"
48 #include "vp9/encoder/vp9_encoder.h"
49 #include "vp9/encoder/vp9_extend.h"
50 #include "vp9/encoder/vp9_ethread.h"
51 #include "vp9/encoder/vp9_firstpass.h"
52 #include "vp9/encoder/vp9_mbgraph.h"
53 #include "vp9/encoder/vp9_multi_thread.h"
54 #include "vp9/encoder/vp9_noise_estimate.h"
55 #include "vp9/encoder/vp9_picklpf.h"
56 #include "vp9/encoder/vp9_ratectrl.h"
57 #include "vp9/encoder/vp9_rd.h"
58 #include "vp9/encoder/vp9_resize.h"
59 #include "vp9/encoder/vp9_segmentation.h"
60 #include "vp9/encoder/vp9_skin_detection.h"
61 #include "vp9/encoder/vp9_speed_features.h"
62 #include "vp9/encoder/vp9_svc_layercontext.h"
63 #include "vp9/encoder/vp9_temporal_filter.h"
65 #define AM_SEGMENT_ID_INACTIVE 7
66 #define AM_SEGMENT_ID_ACTIVE 0
68 #define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
69 // for altref computation.
70 #define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
71 // mv. Choose a very high value for
72 // now so that HIGH_PRECISION is always
74 // #define OUTPUT_YUV_REC
76 #ifdef OUTPUT_YUV_DENOISED
77 FILE *yuv_denoised_file = NULL;
79 #ifdef OUTPUT_YUV_SKINMAP
80 FILE *yuv_skinmap_file = NULL;
92 #ifdef ENABLE_KF_DENOISE
93 // Test condition for spatial denoise of source.
94 static int is_spatial_denoise_enabled(VP9_COMP *cpi) {
95 VP9_COMMON *const cm = &cpi->common;
96 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
98 return (oxcf->pass != 1) && !is_lossless_requested(&cpi->oxcf) &&
99 frame_is_intra_only(cm);
103 // Test for whether to calculate metrics for the frame.
104 static int is_psnr_calc_enabled(VP9_COMP *cpi) {
105 VP9_COMMON *const cm = &cpi->common;
106 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
108 return cpi->b_calculate_psnr && (oxcf->pass != 1) && cm->show_frame;
111 /* clang-format off */
112 const Vp9LevelSpec vp9_level_defs[VP9_LEVELS] = {
113 { LEVEL_1, 829440, 36864, 200, 400, 2, 1, 4, 8 },
114 { LEVEL_1_1, 2764800, 73728, 800, 1000, 2, 1, 4, 8 },
115 { LEVEL_2, 4608000, 122880, 1800, 1500, 2, 1, 4, 8 },
116 { LEVEL_2_1, 9216000, 245760, 3600, 2800, 2, 2, 4, 8 },
117 { LEVEL_3, 20736000, 552960, 7200, 6000, 2, 4, 4, 8 },
118 { LEVEL_3_1, 36864000, 983040, 12000, 10000, 2, 4, 4, 8 },
119 { LEVEL_4, 83558400, 2228224, 18000, 16000, 4, 4, 4, 8 },
120 { LEVEL_4_1, 160432128, 2228224, 30000, 18000, 4, 4, 5, 6 },
121 { LEVEL_5, 311951360, 8912896, 60000, 36000, 6, 8, 6, 4 },
122 { LEVEL_5_1, 588251136, 8912896, 120000, 46000, 8, 8, 10, 4 },
123 // TODO(huisu): update max_cpb_size for level 5_2 ~ 6_2 when
124 // they are finalized (currently TBD).
125 { LEVEL_5_2, 1176502272, 8912896, 180000, 0, 8, 8, 10, 4 },
126 { LEVEL_6, 1176502272, 35651584, 180000, 0, 8, 16, 10, 4 },
127 { LEVEL_6_1, 2353004544u, 35651584, 240000, 0, 8, 16, 10, 4 },
128 { LEVEL_6_2, 4706009088u, 35651584, 480000, 0, 8, 16, 10, 4 },
130 /* clang-format on */
132 static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] =
133 { "The average bit-rate is too high.",
134 "The picture size is too large.",
135 "The luma sample rate is too large.",
136 "The CPB size is too large.",
137 "The compression ratio is too small",
138 "Too many column tiles are used.",
139 "The alt-ref distance is too small.",
140 "Too many reference buffers are used." };
142 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
168 // Mark all inactive blocks as active. Other segmentation features may be set
169 // so memset cannot be used, instead only inactive blocks should be reset.
170 static void suppress_active_map(VP9_COMP *cpi) {
171 unsigned char *const seg_map = cpi->segmentation_map;
173 if (cpi->active_map.enabled || cpi->active_map.update) {
174 const int rows = cpi->common.mi_rows;
175 const int cols = cpi->common.mi_cols;
178 for (i = 0; i < rows * cols; ++i)
179 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
180 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
184 static void apply_active_map(VP9_COMP *cpi) {
185 struct segmentation *const seg = &cpi->common.seg;
186 unsigned char *const seg_map = cpi->segmentation_map;
187 const unsigned char *const active_map = cpi->active_map.map;
190 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
192 if (frame_is_intra_only(&cpi->common)) {
193 cpi->active_map.enabled = 0;
194 cpi->active_map.update = 1;
197 if (cpi->active_map.update) {
198 if (cpi->active_map.enabled) {
199 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
200 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
201 vp9_enable_segmentation(seg);
202 vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
203 vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
204 // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
205 // filter level being zero regardless of the value of seg->abs_delta.
206 vp9_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
209 vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
210 vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
212 seg->update_data = 1;
216 cpi->active_map.update = 0;
220 static void init_level_info(Vp9LevelInfo *level_info) {
221 Vp9LevelStats *const level_stats = &level_info->level_stats;
222 Vp9LevelSpec *const level_spec = &level_info->level_spec;
224 memset(level_stats, 0, sizeof(*level_stats));
225 memset(level_spec, 0, sizeof(*level_spec));
226 level_spec->level = LEVEL_UNKNOWN;
227 level_spec->min_altref_distance = INT_MAX;
230 VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec) {
232 const Vp9LevelSpec *this_level;
234 vpx_clear_system_state();
236 for (i = 0; i < VP9_LEVELS; ++i) {
237 this_level = &vp9_level_defs[i];
238 if ((double)level_spec->max_luma_sample_rate >
239 (double)this_level->max_luma_sample_rate *
240 (1 + SAMPLE_RATE_GRACE_P) ||
241 level_spec->max_luma_picture_size > this_level->max_luma_picture_size ||
242 level_spec->average_bitrate > this_level->average_bitrate ||
243 level_spec->max_cpb_size > this_level->max_cpb_size ||
244 level_spec->compression_ratio < this_level->compression_ratio ||
245 level_spec->max_col_tiles > this_level->max_col_tiles ||
246 level_spec->min_altref_distance < this_level->min_altref_distance ||
247 level_spec->max_ref_frame_buffers > this_level->max_ref_frame_buffers)
251 return (i == VP9_LEVELS) ? LEVEL_UNKNOWN : vp9_level_defs[i].level;
254 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
256 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
257 unsigned char *const active_map_8x8 = cpi->active_map.map;
258 const int mi_rows = cpi->common.mi_rows;
259 const int mi_cols = cpi->common.mi_cols;
260 cpi->active_map.update = 1;
263 for (r = 0; r < mi_rows; ++r) {
264 for (c = 0; c < mi_cols; ++c) {
265 active_map_8x8[r * mi_cols + c] =
266 new_map_16x16[(r >> 1) * cols + (c >> 1)]
267 ? AM_SEGMENT_ID_ACTIVE
268 : AM_SEGMENT_ID_INACTIVE;
271 cpi->active_map.enabled = 1;
273 cpi->active_map.enabled = 0;
281 int vp9_get_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
283 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
285 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
286 const int mi_rows = cpi->common.mi_rows;
287 const int mi_cols = cpi->common.mi_cols;
288 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
289 if (cpi->active_map.enabled) {
291 for (r = 0; r < mi_rows; ++r) {
292 for (c = 0; c < mi_cols; ++c) {
293 // Cyclic refresh segments are considered active despite not having
294 // AM_SEGMENT_ID_ACTIVE
295 new_map_16x16[(r >> 1) * cols + (c >> 1)] |=
296 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
306 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
307 MACROBLOCK *const mb = &cpi->td.mb;
308 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
309 if (cpi->common.allow_high_precision_mv) {
310 mb->mvcost = mb->nmvcost_hp;
311 mb->mvsadcost = mb->nmvsadcost_hp;
313 mb->mvcost = mb->nmvcost;
314 mb->mvsadcost = mb->nmvsadcost;
318 static void setup_frame(VP9_COMP *cpi) {
319 VP9_COMMON *const cm = &cpi->common;
320 // Set up entropy context depending on frame type. The decoder mandates
321 // the use of the default context, index 0, for keyframes and inter
322 // frames where the error_resilient_mode or intra_only flag is set. For
323 // other inter-frames the encoder currently uses only two contexts;
324 // context 1 for ALTREF frames and context 0 for the others.
325 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
326 vp9_setup_past_independence(cm);
328 if (!cpi->use_svc) cm->frame_context_idx = cpi->refresh_alt_ref_frame;
331 if (cm->frame_type == KEY_FRAME) {
332 if (!is_two_pass_svc(cpi)) cpi->refresh_golden_frame = 1;
333 cpi->refresh_alt_ref_frame = 1;
334 vp9_zero(cpi->interp_filter_selected);
336 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
337 vp9_zero(cpi->interp_filter_selected[0]);
341 static void vp9_enc_setup_mi(VP9_COMMON *cm) {
343 cm->mi = cm->mip + cm->mi_stride + 1;
344 memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
345 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
346 // Clear top border row
347 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
348 // Clear left border column
349 for (i = 1; i < cm->mi_rows + 1; ++i)
350 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
352 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
353 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
355 memset(cm->mi_grid_base, 0,
356 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
359 static int vp9_enc_alloc_mi(VP9_COMMON *cm, int mi_size) {
360 cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
361 if (!cm->mip) return 1;
362 cm->prev_mip = vpx_calloc(mi_size, sizeof(*cm->prev_mip));
363 if (!cm->prev_mip) return 1;
364 cm->mi_alloc_size = mi_size;
366 cm->mi_grid_base = (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
367 if (!cm->mi_grid_base) return 1;
368 cm->prev_mi_grid_base =
369 (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *));
370 if (!cm->prev_mi_grid_base) return 1;
375 static void vp9_enc_free_mi(VP9_COMMON *cm) {
378 vpx_free(cm->prev_mip);
380 vpx_free(cm->mi_grid_base);
381 cm->mi_grid_base = NULL;
382 vpx_free(cm->prev_mi_grid_base);
383 cm->prev_mi_grid_base = NULL;
386 static void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
387 // Current mip will be the prev_mip for the next frame.
388 MODE_INFO **temp_base = cm->prev_mi_grid_base;
389 MODE_INFO *temp = cm->prev_mip;
390 cm->prev_mip = cm->mip;
393 // Update the upper left visible macroblock ptrs.
394 cm->mi = cm->mip + cm->mi_stride + 1;
395 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
397 cm->prev_mi_grid_base = cm->mi_grid_base;
398 cm->mi_grid_base = temp_base;
399 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
400 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
403 void vp9_initialize_enc(void) {
404 static volatile int init_done = 0;
410 vp9_init_intra_predictors();
412 vp9_rc_init_minq_luts();
413 vp9_entropy_mv_init();
414 vp9_temporal_filter_init();
419 static void dealloc_compressor_data(VP9_COMP *cpi) {
420 VP9_COMMON *const cm = &cpi->common;
423 vpx_free(cpi->mbmi_ext_base);
424 cpi->mbmi_ext_base = NULL;
426 vpx_free(cpi->tile_data);
427 cpi->tile_data = NULL;
429 vpx_free(cpi->segmentation_map);
430 cpi->segmentation_map = NULL;
431 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
432 cpi->coding_context.last_frame_seg_map_copy = NULL;
434 vpx_free(cpi->nmvcosts[0]);
435 vpx_free(cpi->nmvcosts[1]);
436 cpi->nmvcosts[0] = NULL;
437 cpi->nmvcosts[1] = NULL;
439 vpx_free(cpi->nmvcosts_hp[0]);
440 vpx_free(cpi->nmvcosts_hp[1]);
441 cpi->nmvcosts_hp[0] = NULL;
442 cpi->nmvcosts_hp[1] = NULL;
444 vpx_free(cpi->nmvsadcosts[0]);
445 vpx_free(cpi->nmvsadcosts[1]);
446 cpi->nmvsadcosts[0] = NULL;
447 cpi->nmvsadcosts[1] = NULL;
449 vpx_free(cpi->nmvsadcosts_hp[0]);
450 vpx_free(cpi->nmvsadcosts_hp[1]);
451 cpi->nmvsadcosts_hp[0] = NULL;
452 cpi->nmvsadcosts_hp[1] = NULL;
454 vpx_free(cpi->prev_partition);
455 cpi->prev_partition = NULL;
457 vpx_free(cpi->prev_segment_id);
458 cpi->prev_segment_id = NULL;
460 vpx_free(cpi->prev_variance_low);
461 cpi->prev_variance_low = NULL;
463 vpx_free(cpi->copied_frame_cnt);
464 cpi->copied_frame_cnt = NULL;
466 vpx_free(cpi->avg_source_sad_sb);
467 cpi->avg_source_sad_sb = NULL;
469 vp9_cyclic_refresh_free(cpi->cyclic_refresh);
470 cpi->cyclic_refresh = NULL;
472 vpx_free(cpi->active_map.map);
473 cpi->active_map.map = NULL;
475 vpx_free(cpi->consec_zero_mv);
476 cpi->consec_zero_mv = NULL;
478 vp9_free_ref_frame_buffers(cm->buffer_pool);
479 #if CONFIG_VP9_POSTPROC
480 vp9_free_postproc_buffers(cm);
482 vp9_free_context_buffers(cm);
484 vpx_free_frame_buffer(&cpi->last_frame_uf);
485 vpx_free_frame_buffer(&cpi->scaled_source);
486 vpx_free_frame_buffer(&cpi->scaled_last_source);
487 vpx_free_frame_buffer(&cpi->alt_ref_buffer);
488 #ifdef ENABLE_KF_DENOISE
489 vpx_free_frame_buffer(&cpi->raw_unscaled_source);
490 vpx_free_frame_buffer(&cpi->raw_scaled_source);
493 vp9_lookahead_destroy(cpi->lookahead);
495 vpx_free(cpi->tile_tok[0][0]);
496 cpi->tile_tok[0][0] = 0;
498 vpx_free(cpi->tplist[0][0]);
499 cpi->tplist[0][0] = NULL;
501 vp9_free_pc_tree(&cpi->td);
503 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
504 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
505 vpx_free(lc->rc_twopass_stats_in.buf);
506 lc->rc_twopass_stats_in.buf = NULL;
507 lc->rc_twopass_stats_in.sz = 0;
510 if (cpi->source_diff_var != NULL) {
511 vpx_free(cpi->source_diff_var);
512 cpi->source_diff_var = NULL;
515 for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
516 vpx_free_frame_buffer(&cpi->svc.scaled_frames[i]);
518 memset(&cpi->svc.scaled_frames[0], 0,
519 MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
521 vpx_free_frame_buffer(&cpi->svc.scaled_temp);
522 memset(&cpi->svc.scaled_temp, 0, sizeof(cpi->svc.scaled_temp));
524 vpx_free_frame_buffer(&cpi->svc.empty_frame.img);
525 memset(&cpi->svc.empty_frame, 0, sizeof(cpi->svc.empty_frame));
527 vp9_free_svc_cyclic_refresh(cpi);
530 static void save_coding_context(VP9_COMP *cpi) {
531 CODING_CONTEXT *const cc = &cpi->coding_context;
532 VP9_COMMON *cm = &cpi->common;
534 // Stores a snapshot of key state variables which can subsequently be
535 // restored with a call to vp9_restore_coding_context. These functions are
536 // intended for use in a re-code loop in vp9_compress_frame where the
537 // quantizer value is adjusted between loop iterations.
538 vp9_copy(cc->nmvjointcost, cpi->td.mb.nmvjointcost);
540 memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
541 MV_VALS * sizeof(*cpi->nmvcosts[0]));
542 memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
543 MV_VALS * sizeof(*cpi->nmvcosts[1]));
544 memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
545 MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
546 memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
547 MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
549 vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
551 memcpy(cpi->coding_context.last_frame_seg_map_copy, cm->last_frame_seg_map,
552 (cm->mi_rows * cm->mi_cols));
554 vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
555 vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
560 static void restore_coding_context(VP9_COMP *cpi) {
561 CODING_CONTEXT *const cc = &cpi->coding_context;
562 VP9_COMMON *cm = &cpi->common;
564 // Restore key state variables to the snapshot state stored in the
565 // previous call to vp9_save_coding_context.
566 vp9_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
568 memcpy(cpi->nmvcosts[0], cc->nmvcosts[0], MV_VALS * sizeof(*cc->nmvcosts[0]));
569 memcpy(cpi->nmvcosts[1], cc->nmvcosts[1], MV_VALS * sizeof(*cc->nmvcosts[1]));
570 memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
571 MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
572 memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
573 MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
575 vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
577 memcpy(cm->last_frame_seg_map, cpi->coding_context.last_frame_seg_map_copy,
578 (cm->mi_rows * cm->mi_cols));
580 vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
581 vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
586 static void configure_static_seg_features(VP9_COMP *cpi) {
587 VP9_COMMON *const cm = &cpi->common;
588 const RATE_CONTROL *const rc = &cpi->rc;
589 struct segmentation *const seg = &cm->seg;
591 int high_q = (int)(rc->avg_q > 48.0);
594 // Disable and clear down for KF
595 if (cm->frame_type == KEY_FRAME) {
596 // Clear down the global segmentation map
597 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
599 seg->update_data = 0;
600 cpi->static_mb_pct = 0;
602 // Disable segmentation
603 vp9_disable_segmentation(seg);
605 // Clear down the segment features.
606 vp9_clearall_segfeatures(seg);
607 } else if (cpi->refresh_alt_ref_frame) {
608 // If this is an alt ref frame
609 // Clear down the global segmentation map
610 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
612 seg->update_data = 0;
613 cpi->static_mb_pct = 0;
615 // Disable segmentation and individual segment features by default
616 vp9_disable_segmentation(seg);
617 vp9_clearall_segfeatures(seg);
619 // Scan frames from current to arf frame.
620 // This function re-enables segmentation if appropriate.
621 vp9_update_mbgraph_stats(cpi);
623 // If segmentation was enabled set those features needed for the
627 seg->update_data = 1;
630 vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
631 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
632 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
634 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
635 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
637 // Where relevant assume segment data is delta data
638 seg->abs_delta = SEGMENT_DELTADATA;
640 } else if (seg->enabled) {
641 // All other frames if segmentation has been enabled
643 // First normal frame in a valid gf or alt ref group
644 if (rc->frames_since_golden == 0) {
645 // Set up segment features for normal frames in an arf group
646 if (rc->source_alt_ref_active) {
648 seg->update_data = 1;
649 seg->abs_delta = SEGMENT_DELTADATA;
652 vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
653 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
654 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
656 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
657 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
659 // Segment coding disabled for compred testing
660 if (high_q || (cpi->static_mb_pct == 100)) {
661 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
662 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
663 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
666 // Disable segmentation and clear down features if alt ref
667 // is not active for this group
669 vp9_disable_segmentation(seg);
671 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
674 seg->update_data = 0;
676 vp9_clearall_segfeatures(seg);
678 } else if (rc->is_src_frame_alt_ref) {
679 // Special case where we are coding over the top of a previous
681 // Segment coding disabled for compred testing
683 // Enable ref frame features for segment 0 as well
684 vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
685 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
687 // All mbs should use ALTREF_FRAME
688 vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
689 vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
690 vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
691 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
693 // Skip all MBs if high Q (0,0 mv and skip coeffs)
695 vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
696 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
698 // Enable data update
699 seg->update_data = 1;
703 // No updates.. leave things as they are.
705 seg->update_data = 0;
710 static void update_reference_segmentation_map(VP9_COMP *cpi) {
711 VP9_COMMON *const cm = &cpi->common;
712 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
713 uint8_t *cache_ptr = cm->last_frame_seg_map;
716 for (row = 0; row < cm->mi_rows; row++) {
717 MODE_INFO **mi_8x8 = mi_8x8_ptr;
718 uint8_t *cache = cache_ptr;
719 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
720 cache[0] = mi_8x8[0]->segment_id;
721 mi_8x8_ptr += cm->mi_stride;
722 cache_ptr += cm->mi_cols;
726 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
727 VP9_COMMON *cm = &cpi->common;
728 const VP9EncoderConfig *oxcf = &cpi->oxcf;
731 cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
732 cm->subsampling_x, cm->subsampling_y,
733 #if CONFIG_VP9_HIGHBITDEPTH
734 cm->use_highbitdepth,
736 oxcf->lag_in_frames);
738 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
739 "Failed to allocate lag buffers");
741 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
742 if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
743 cm->subsampling_x, cm->subsampling_y,
744 #if CONFIG_VP9_HIGHBITDEPTH
745 cm->use_highbitdepth,
747 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
749 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
750 "Failed to allocate altref buffer");
753 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
754 VP9_COMMON *const cm = &cpi->common;
755 if (vpx_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
756 cm->subsampling_x, cm->subsampling_y,
757 #if CONFIG_VP9_HIGHBITDEPTH
758 cm->use_highbitdepth,
760 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
762 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
763 "Failed to allocate last frame buffer");
765 if (vpx_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
766 cm->subsampling_x, cm->subsampling_y,
767 #if CONFIG_VP9_HIGHBITDEPTH
768 cm->use_highbitdepth,
770 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
772 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
773 "Failed to allocate scaled source buffer");
775 // For 1 pass cbr: allocate scaled_frame that may be used as an intermediate
776 // buffer for a 2 stage down-sampling: two stages of 1:2 down-sampling for a
777 // target of 1/4x1/4.
778 if (is_one_pass_cbr_svc(cpi) && !cpi->svc.scaled_temp_is_alloc) {
779 cpi->svc.scaled_temp_is_alloc = 1;
780 if (vpx_realloc_frame_buffer(
781 &cpi->svc.scaled_temp, cm->width >> 1, cm->height >> 1,
782 cm->subsampling_x, cm->subsampling_y,
783 #if CONFIG_VP9_HIGHBITDEPTH
784 cm->use_highbitdepth,
786 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
787 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
788 "Failed to allocate scaled_frame for svc ");
791 if (vpx_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
792 cm->subsampling_x, cm->subsampling_y,
793 #if CONFIG_VP9_HIGHBITDEPTH
794 cm->use_highbitdepth,
796 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
798 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
799 "Failed to allocate scaled last source buffer");
800 #ifdef ENABLE_KF_DENOISE
801 if (vpx_realloc_frame_buffer(&cpi->raw_unscaled_source, cm->width, cm->height,
802 cm->subsampling_x, cm->subsampling_y,
803 #if CONFIG_VP9_HIGHBITDEPTH
804 cm->use_highbitdepth,
806 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
808 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
809 "Failed to allocate unscaled raw source frame buffer");
811 if (vpx_realloc_frame_buffer(&cpi->raw_scaled_source, cm->width, cm->height,
812 cm->subsampling_x, cm->subsampling_y,
813 #if CONFIG_VP9_HIGHBITDEPTH
814 cm->use_highbitdepth,
816 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
818 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
819 "Failed to allocate scaled raw source frame buffer");
823 static int alloc_context_buffers_ext(VP9_COMP *cpi) {
824 VP9_COMMON *cm = &cpi->common;
825 int mi_size = cm->mi_cols * cm->mi_rows;
827 cpi->mbmi_ext_base = vpx_calloc(mi_size, sizeof(*cpi->mbmi_ext_base));
828 if (!cpi->mbmi_ext_base) return 1;
833 static void alloc_compressor_data(VP9_COMP *cpi) {
834 VP9_COMMON *cm = &cpi->common;
837 vp9_alloc_context_buffers(cm, cm->width, cm->height);
839 alloc_context_buffers_ext(cpi);
841 vpx_free(cpi->tile_tok[0][0]);
844 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
845 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
846 vpx_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
849 sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
850 vpx_free(cpi->tplist[0][0]);
852 cm, cpi->tplist[0][0],
853 vpx_calloc(sb_rows * 4 * (1 << 6), sizeof(*cpi->tplist[0][0])));
855 vp9_setup_pc_tree(&cpi->common, &cpi->td);
858 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
859 cpi->framerate = framerate < 0.1 ? 30 : framerate;
860 vp9_rc_update_framerate(cpi);
863 static void set_tile_limits(VP9_COMP *cpi) {
864 VP9_COMMON *const cm = &cpi->common;
866 int min_log2_tile_cols, max_log2_tile_cols;
867 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
869 if (is_two_pass_svc(cpi) && (cpi->svc.encode_empty_frame_state == ENCODING ||
870 cpi->svc.number_spatial_layers > 1)) {
871 cm->log2_tile_cols = 0;
872 cm->log2_tile_rows = 0;
875 clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
876 cm->log2_tile_rows = cpi->oxcf.tile_rows;
880 static void update_frame_size(VP9_COMP *cpi) {
881 VP9_COMMON *const cm = &cpi->common;
882 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
884 vp9_set_mb_mi(cm, cm->width, cm->height);
885 vp9_init_context_buffers(cm);
886 vp9_init_macroblockd(cm, xd, NULL);
887 cpi->td.mb.mbmi_ext_base = cpi->mbmi_ext_base;
888 memset(cpi->mbmi_ext_base, 0,
889 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
891 set_tile_limits(cpi);
893 if (is_two_pass_svc(cpi)) {
894 if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer, cm->width, cm->height,
895 cm->subsampling_x, cm->subsampling_y,
896 #if CONFIG_VP9_HIGHBITDEPTH
897 cm->use_highbitdepth,
899 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
901 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
902 "Failed to reallocate alt_ref_buffer");
906 static void init_buffer_indices(VP9_COMP *cpi) {
912 static void init_level_constraint(LevelConstraint *lc) {
913 lc->level_index = -1;
914 lc->max_cpb_size = INT_MAX;
915 lc->max_frame_size = INT_MAX;
916 lc->rc_config_updated = 0;
920 static void set_level_constraint(LevelConstraint *ls, int8_t level_index) {
921 vpx_clear_system_state();
922 ls->level_index = level_index;
923 if (level_index >= 0) {
924 ls->max_cpb_size = vp9_level_defs[level_index].max_cpb_size * (double)1000;
928 static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
929 VP9_COMMON *const cm = &cpi->common;
932 cpi->framerate = oxcf->init_framerate;
933 cm->profile = oxcf->profile;
934 cm->bit_depth = oxcf->bit_depth;
935 #if CONFIG_VP9_HIGHBITDEPTH
936 cm->use_highbitdepth = oxcf->use_highbitdepth;
938 cm->color_space = oxcf->color_space;
939 cm->color_range = oxcf->color_range;
941 cpi->target_level = oxcf->target_level;
942 cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
943 set_level_constraint(&cpi->level_constraint,
944 get_level_index(cpi->target_level));
946 cm->width = oxcf->width;
947 cm->height = oxcf->height;
948 alloc_compressor_data(cpi);
950 cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
952 // Single thread case: use counts in common.
953 cpi->td.counts = &cm->counts;
955 // Spatial scalability.
956 cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
957 // Temporal scalability.
958 cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
960 if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
961 ((cpi->svc.number_temporal_layers > 1 ||
962 cpi->svc.number_spatial_layers > 1) &&
963 cpi->oxcf.pass != 1)) {
964 vp9_init_layer_context(cpi);
967 // change includes all joint functionality
968 vp9_change_config(cpi, oxcf);
970 cpi->static_mb_pct = 0;
971 cpi->ref_frame_flags = 0;
973 init_buffer_indices(cpi);
975 vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
978 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
979 const VP9EncoderConfig *oxcf) {
980 const int64_t bandwidth = oxcf->target_bandwidth;
981 const int64_t starting = oxcf->starting_buffer_level_ms;
982 const int64_t optimal = oxcf->optimal_buffer_level_ms;
983 const int64_t maximum = oxcf->maximum_buffer_size_ms;
985 rc->starting_buffer_level = starting * bandwidth / 1000;
986 rc->optimal_buffer_level =
987 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
988 rc->maximum_buffer_size =
989 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
992 #if CONFIG_VP9_HIGHBITDEPTH
993 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
994 cpi->fn_ptr[BT].sdf = SDF; \
995 cpi->fn_ptr[BT].sdaf = SDAF; \
996 cpi->fn_ptr[BT].vf = VF; \
997 cpi->fn_ptr[BT].svf = SVF; \
998 cpi->fn_ptr[BT].svaf = SVAF; \
999 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1000 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1001 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1003 #define MAKE_BFP_SAD_WRAPPER(fnname) \
1004 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1005 int source_stride, \
1006 const uint8_t *ref_ptr, int ref_stride) { \
1007 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1009 static unsigned int fnname##_bits10( \
1010 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1012 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1014 static unsigned int fnname##_bits12( \
1015 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1017 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1020 #define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1021 static unsigned int fnname##_bits8( \
1022 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1023 int ref_stride, const uint8_t *second_pred) { \
1024 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1026 static unsigned int fnname##_bits10( \
1027 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1028 int ref_stride, const uint8_t *second_pred) { \
1029 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1032 static unsigned int fnname##_bits12( \
1033 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1034 int ref_stride, const uint8_t *second_pred) { \
1035 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1039 #define MAKE_BFP_SAD3_WRAPPER(fnname) \
1040 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1041 const uint8_t *ref_ptr, int ref_stride, \
1042 unsigned int *sad_array) { \
1043 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1045 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1046 const uint8_t *ref_ptr, int ref_stride, \
1047 unsigned int *sad_array) { \
1049 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1050 for (i = 0; i < 3; i++) sad_array[i] >>= 2; \
1052 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1053 const uint8_t *ref_ptr, int ref_stride, \
1054 unsigned int *sad_array) { \
1056 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1057 for (i = 0; i < 3; i++) sad_array[i] >>= 4; \
1060 #define MAKE_BFP_SAD8_WRAPPER(fnname) \
1061 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1062 const uint8_t *ref_ptr, int ref_stride, \
1063 unsigned int *sad_array) { \
1064 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1066 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1067 const uint8_t *ref_ptr, int ref_stride, \
1068 unsigned int *sad_array) { \
1070 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1071 for (i = 0; i < 8; i++) sad_array[i] >>= 2; \
1073 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1074 const uint8_t *ref_ptr, int ref_stride, \
1075 unsigned int *sad_array) { \
1077 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1078 for (i = 0; i < 8; i++) sad_array[i] >>= 4; \
1080 #define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1081 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1082 const uint8_t *const ref_ptr[], int ref_stride, \
1083 unsigned int *sad_array) { \
1084 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1086 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1087 const uint8_t *const ref_ptr[], int ref_stride, \
1088 unsigned int *sad_array) { \
1090 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1091 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1093 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1094 const uint8_t *const ref_ptr[], int ref_stride, \
1095 unsigned int *sad_array) { \
1097 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1098 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1101 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x16)
1102 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x16_avg)
1103 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x16x4d)
1104 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x32)
1105 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x32_avg)
1106 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x32x4d)
1107 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x32)
1108 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x32_avg)
1109 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x32x4d)
1110 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x64)
1111 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x64_avg)
1112 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x64x4d)
1113 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x32)
1114 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x32_avg)
1115 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad32x32x3)
1116 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad32x32x8)
1117 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x32x4d)
1118 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x64)
1119 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x64_avg)
1120 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad64x64x3)
1121 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad64x64x8)
1122 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x64x4d)
1123 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x16)
1124 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x16_avg)
1125 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad16x16x3)
1126 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad16x16x8)
1127 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x16x4d)
1128 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x8)
1129 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x8_avg)
1130 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad16x8x3)
1131 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad16x8x8)
1132 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x8x4d)
1133 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x16)
1134 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x16_avg)
1135 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad8x16x3)
1136 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad8x16x8)
1137 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x16x4d)
1138 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x8)
1139 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x8_avg)
1140 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad8x8x3)
1141 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad8x8x8)
1142 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x8x4d)
1143 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x4)
1144 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x4_avg)
1145 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad8x4x8)
1146 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x4x4d)
1147 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x8)
1148 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x8_avg)
1149 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad4x8x8)
1150 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x8x4d)
1151 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x4)
1152 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x4_avg)
1153 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad4x4x3)
1154 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad4x4x8)
1155 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x4x4d)
1157 static void highbd_set_var_fns(VP9_COMP *const cpi) {
1158 VP9_COMMON *const cm = &cpi->common;
1159 if (cm->use_highbitdepth) {
1160 switch (cm->bit_depth) {
1162 HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits8,
1163 vpx_highbd_sad32x16_avg_bits8, vpx_highbd_8_variance32x16,
1164 vpx_highbd_8_sub_pixel_variance32x16,
1165 vpx_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
1166 vpx_highbd_sad32x16x4d_bits8)
1168 HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits8,
1169 vpx_highbd_sad16x32_avg_bits8, vpx_highbd_8_variance16x32,
1170 vpx_highbd_8_sub_pixel_variance16x32,
1171 vpx_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
1172 vpx_highbd_sad16x32x4d_bits8)
1174 HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits8,
1175 vpx_highbd_sad64x32_avg_bits8, vpx_highbd_8_variance64x32,
1176 vpx_highbd_8_sub_pixel_variance64x32,
1177 vpx_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
1178 vpx_highbd_sad64x32x4d_bits8)
1180 HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits8,
1181 vpx_highbd_sad32x64_avg_bits8, vpx_highbd_8_variance32x64,
1182 vpx_highbd_8_sub_pixel_variance32x64,
1183 vpx_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
1184 vpx_highbd_sad32x64x4d_bits8)
1186 HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits8,
1187 vpx_highbd_sad32x32_avg_bits8, vpx_highbd_8_variance32x32,
1188 vpx_highbd_8_sub_pixel_variance32x32,
1189 vpx_highbd_8_sub_pixel_avg_variance32x32,
1190 vpx_highbd_sad32x32x3_bits8, vpx_highbd_sad32x32x8_bits8,
1191 vpx_highbd_sad32x32x4d_bits8)
1193 HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits8,
1194 vpx_highbd_sad64x64_avg_bits8, vpx_highbd_8_variance64x64,
1195 vpx_highbd_8_sub_pixel_variance64x64,
1196 vpx_highbd_8_sub_pixel_avg_variance64x64,
1197 vpx_highbd_sad64x64x3_bits8, vpx_highbd_sad64x64x8_bits8,
1198 vpx_highbd_sad64x64x4d_bits8)
1200 HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits8,
1201 vpx_highbd_sad16x16_avg_bits8, vpx_highbd_8_variance16x16,
1202 vpx_highbd_8_sub_pixel_variance16x16,
1203 vpx_highbd_8_sub_pixel_avg_variance16x16,
1204 vpx_highbd_sad16x16x3_bits8, vpx_highbd_sad16x16x8_bits8,
1205 vpx_highbd_sad16x16x4d_bits8)
1208 BLOCK_16X8, vpx_highbd_sad16x8_bits8, vpx_highbd_sad16x8_avg_bits8,
1209 vpx_highbd_8_variance16x8, vpx_highbd_8_sub_pixel_variance16x8,
1210 vpx_highbd_8_sub_pixel_avg_variance16x8, vpx_highbd_sad16x8x3_bits8,
1211 vpx_highbd_sad16x8x8_bits8, vpx_highbd_sad16x8x4d_bits8)
1214 BLOCK_8X16, vpx_highbd_sad8x16_bits8, vpx_highbd_sad8x16_avg_bits8,
1215 vpx_highbd_8_variance8x16, vpx_highbd_8_sub_pixel_variance8x16,
1216 vpx_highbd_8_sub_pixel_avg_variance8x16, vpx_highbd_sad8x16x3_bits8,
1217 vpx_highbd_sad8x16x8_bits8, vpx_highbd_sad8x16x4d_bits8)
1220 BLOCK_8X8, vpx_highbd_sad8x8_bits8, vpx_highbd_sad8x8_avg_bits8,
1221 vpx_highbd_8_variance8x8, vpx_highbd_8_sub_pixel_variance8x8,
1222 vpx_highbd_8_sub_pixel_avg_variance8x8, vpx_highbd_sad8x8x3_bits8,
1223 vpx_highbd_sad8x8x8_bits8, vpx_highbd_sad8x8x4d_bits8)
1225 HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits8,
1226 vpx_highbd_sad8x4_avg_bits8, vpx_highbd_8_variance8x4,
1227 vpx_highbd_8_sub_pixel_variance8x4,
1228 vpx_highbd_8_sub_pixel_avg_variance8x4, NULL,
1229 vpx_highbd_sad8x4x8_bits8, vpx_highbd_sad8x4x4d_bits8)
1231 HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits8,
1232 vpx_highbd_sad4x8_avg_bits8, vpx_highbd_8_variance4x8,
1233 vpx_highbd_8_sub_pixel_variance4x8,
1234 vpx_highbd_8_sub_pixel_avg_variance4x8, NULL,
1235 vpx_highbd_sad4x8x8_bits8, vpx_highbd_sad4x8x4d_bits8)
1238 BLOCK_4X4, vpx_highbd_sad4x4_bits8, vpx_highbd_sad4x4_avg_bits8,
1239 vpx_highbd_8_variance4x4, vpx_highbd_8_sub_pixel_variance4x4,
1240 vpx_highbd_8_sub_pixel_avg_variance4x4, vpx_highbd_sad4x4x3_bits8,
1241 vpx_highbd_sad4x4x8_bits8, vpx_highbd_sad4x4x4d_bits8)
1245 HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits10,
1246 vpx_highbd_sad32x16_avg_bits10, vpx_highbd_10_variance32x16,
1247 vpx_highbd_10_sub_pixel_variance32x16,
1248 vpx_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
1249 vpx_highbd_sad32x16x4d_bits10)
1251 HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits10,
1252 vpx_highbd_sad16x32_avg_bits10, vpx_highbd_10_variance16x32,
1253 vpx_highbd_10_sub_pixel_variance16x32,
1254 vpx_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
1255 vpx_highbd_sad16x32x4d_bits10)
1257 HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits10,
1258 vpx_highbd_sad64x32_avg_bits10, vpx_highbd_10_variance64x32,
1259 vpx_highbd_10_sub_pixel_variance64x32,
1260 vpx_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
1261 vpx_highbd_sad64x32x4d_bits10)
1263 HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits10,
1264 vpx_highbd_sad32x64_avg_bits10, vpx_highbd_10_variance32x64,
1265 vpx_highbd_10_sub_pixel_variance32x64,
1266 vpx_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
1267 vpx_highbd_sad32x64x4d_bits10)
1269 HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits10,
1270 vpx_highbd_sad32x32_avg_bits10, vpx_highbd_10_variance32x32,
1271 vpx_highbd_10_sub_pixel_variance32x32,
1272 vpx_highbd_10_sub_pixel_avg_variance32x32,
1273 vpx_highbd_sad32x32x3_bits10, vpx_highbd_sad32x32x8_bits10,
1274 vpx_highbd_sad32x32x4d_bits10)
1276 HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits10,
1277 vpx_highbd_sad64x64_avg_bits10, vpx_highbd_10_variance64x64,
1278 vpx_highbd_10_sub_pixel_variance64x64,
1279 vpx_highbd_10_sub_pixel_avg_variance64x64,
1280 vpx_highbd_sad64x64x3_bits10, vpx_highbd_sad64x64x8_bits10,
1281 vpx_highbd_sad64x64x4d_bits10)
1283 HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits10,
1284 vpx_highbd_sad16x16_avg_bits10, vpx_highbd_10_variance16x16,
1285 vpx_highbd_10_sub_pixel_variance16x16,
1286 vpx_highbd_10_sub_pixel_avg_variance16x16,
1287 vpx_highbd_sad16x16x3_bits10, vpx_highbd_sad16x16x8_bits10,
1288 vpx_highbd_sad16x16x4d_bits10)
1290 HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits10,
1291 vpx_highbd_sad16x8_avg_bits10, vpx_highbd_10_variance16x8,
1292 vpx_highbd_10_sub_pixel_variance16x8,
1293 vpx_highbd_10_sub_pixel_avg_variance16x8,
1294 vpx_highbd_sad16x8x3_bits10, vpx_highbd_sad16x8x8_bits10,
1295 vpx_highbd_sad16x8x4d_bits10)
1297 HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits10,
1298 vpx_highbd_sad8x16_avg_bits10, vpx_highbd_10_variance8x16,
1299 vpx_highbd_10_sub_pixel_variance8x16,
1300 vpx_highbd_10_sub_pixel_avg_variance8x16,
1301 vpx_highbd_sad8x16x3_bits10, vpx_highbd_sad8x16x8_bits10,
1302 vpx_highbd_sad8x16x4d_bits10)
1305 BLOCK_8X8, vpx_highbd_sad8x8_bits10, vpx_highbd_sad8x8_avg_bits10,
1306 vpx_highbd_10_variance8x8, vpx_highbd_10_sub_pixel_variance8x8,
1307 vpx_highbd_10_sub_pixel_avg_variance8x8, vpx_highbd_sad8x8x3_bits10,
1308 vpx_highbd_sad8x8x8_bits10, vpx_highbd_sad8x8x4d_bits10)
1310 HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits10,
1311 vpx_highbd_sad8x4_avg_bits10, vpx_highbd_10_variance8x4,
1312 vpx_highbd_10_sub_pixel_variance8x4,
1313 vpx_highbd_10_sub_pixel_avg_variance8x4, NULL,
1314 vpx_highbd_sad8x4x8_bits10, vpx_highbd_sad8x4x4d_bits10)
1316 HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits10,
1317 vpx_highbd_sad4x8_avg_bits10, vpx_highbd_10_variance4x8,
1318 vpx_highbd_10_sub_pixel_variance4x8,
1319 vpx_highbd_10_sub_pixel_avg_variance4x8, NULL,
1320 vpx_highbd_sad4x8x8_bits10, vpx_highbd_sad4x8x4d_bits10)
1323 BLOCK_4X4, vpx_highbd_sad4x4_bits10, vpx_highbd_sad4x4_avg_bits10,
1324 vpx_highbd_10_variance4x4, vpx_highbd_10_sub_pixel_variance4x4,
1325 vpx_highbd_10_sub_pixel_avg_variance4x4, vpx_highbd_sad4x4x3_bits10,
1326 vpx_highbd_sad4x4x8_bits10, vpx_highbd_sad4x4x4d_bits10)
1330 HIGHBD_BFP(BLOCK_32X16, vpx_highbd_sad32x16_bits12,
1331 vpx_highbd_sad32x16_avg_bits12, vpx_highbd_12_variance32x16,
1332 vpx_highbd_12_sub_pixel_variance32x16,
1333 vpx_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
1334 vpx_highbd_sad32x16x4d_bits12)
1336 HIGHBD_BFP(BLOCK_16X32, vpx_highbd_sad16x32_bits12,
1337 vpx_highbd_sad16x32_avg_bits12, vpx_highbd_12_variance16x32,
1338 vpx_highbd_12_sub_pixel_variance16x32,
1339 vpx_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
1340 vpx_highbd_sad16x32x4d_bits12)
1342 HIGHBD_BFP(BLOCK_64X32, vpx_highbd_sad64x32_bits12,
1343 vpx_highbd_sad64x32_avg_bits12, vpx_highbd_12_variance64x32,
1344 vpx_highbd_12_sub_pixel_variance64x32,
1345 vpx_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
1346 vpx_highbd_sad64x32x4d_bits12)
1348 HIGHBD_BFP(BLOCK_32X64, vpx_highbd_sad32x64_bits12,
1349 vpx_highbd_sad32x64_avg_bits12, vpx_highbd_12_variance32x64,
1350 vpx_highbd_12_sub_pixel_variance32x64,
1351 vpx_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
1352 vpx_highbd_sad32x64x4d_bits12)
1354 HIGHBD_BFP(BLOCK_32X32, vpx_highbd_sad32x32_bits12,
1355 vpx_highbd_sad32x32_avg_bits12, vpx_highbd_12_variance32x32,
1356 vpx_highbd_12_sub_pixel_variance32x32,
1357 vpx_highbd_12_sub_pixel_avg_variance32x32,
1358 vpx_highbd_sad32x32x3_bits12, vpx_highbd_sad32x32x8_bits12,
1359 vpx_highbd_sad32x32x4d_bits12)
1361 HIGHBD_BFP(BLOCK_64X64, vpx_highbd_sad64x64_bits12,
1362 vpx_highbd_sad64x64_avg_bits12, vpx_highbd_12_variance64x64,
1363 vpx_highbd_12_sub_pixel_variance64x64,
1364 vpx_highbd_12_sub_pixel_avg_variance64x64,
1365 vpx_highbd_sad64x64x3_bits12, vpx_highbd_sad64x64x8_bits12,
1366 vpx_highbd_sad64x64x4d_bits12)
1368 HIGHBD_BFP(BLOCK_16X16, vpx_highbd_sad16x16_bits12,
1369 vpx_highbd_sad16x16_avg_bits12, vpx_highbd_12_variance16x16,
1370 vpx_highbd_12_sub_pixel_variance16x16,
1371 vpx_highbd_12_sub_pixel_avg_variance16x16,
1372 vpx_highbd_sad16x16x3_bits12, vpx_highbd_sad16x16x8_bits12,
1373 vpx_highbd_sad16x16x4d_bits12)
1375 HIGHBD_BFP(BLOCK_16X8, vpx_highbd_sad16x8_bits12,
1376 vpx_highbd_sad16x8_avg_bits12, vpx_highbd_12_variance16x8,
1377 vpx_highbd_12_sub_pixel_variance16x8,
1378 vpx_highbd_12_sub_pixel_avg_variance16x8,
1379 vpx_highbd_sad16x8x3_bits12, vpx_highbd_sad16x8x8_bits12,
1380 vpx_highbd_sad16x8x4d_bits12)
1382 HIGHBD_BFP(BLOCK_8X16, vpx_highbd_sad8x16_bits12,
1383 vpx_highbd_sad8x16_avg_bits12, vpx_highbd_12_variance8x16,
1384 vpx_highbd_12_sub_pixel_variance8x16,
1385 vpx_highbd_12_sub_pixel_avg_variance8x16,
1386 vpx_highbd_sad8x16x3_bits12, vpx_highbd_sad8x16x8_bits12,
1387 vpx_highbd_sad8x16x4d_bits12)
1390 BLOCK_8X8, vpx_highbd_sad8x8_bits12, vpx_highbd_sad8x8_avg_bits12,
1391 vpx_highbd_12_variance8x8, vpx_highbd_12_sub_pixel_variance8x8,
1392 vpx_highbd_12_sub_pixel_avg_variance8x8, vpx_highbd_sad8x8x3_bits12,
1393 vpx_highbd_sad8x8x8_bits12, vpx_highbd_sad8x8x4d_bits12)
1395 HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits12,
1396 vpx_highbd_sad8x4_avg_bits12, vpx_highbd_12_variance8x4,
1397 vpx_highbd_12_sub_pixel_variance8x4,
1398 vpx_highbd_12_sub_pixel_avg_variance8x4, NULL,
1399 vpx_highbd_sad8x4x8_bits12, vpx_highbd_sad8x4x4d_bits12)
1401 HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits12,
1402 vpx_highbd_sad4x8_avg_bits12, vpx_highbd_12_variance4x8,
1403 vpx_highbd_12_sub_pixel_variance4x8,
1404 vpx_highbd_12_sub_pixel_avg_variance4x8, NULL,
1405 vpx_highbd_sad4x8x8_bits12, vpx_highbd_sad4x8x4d_bits12)
1408 BLOCK_4X4, vpx_highbd_sad4x4_bits12, vpx_highbd_sad4x4_avg_bits12,
1409 vpx_highbd_12_variance4x4, vpx_highbd_12_sub_pixel_variance4x4,
1410 vpx_highbd_12_sub_pixel_avg_variance4x4, vpx_highbd_sad4x4x3_bits12,
1411 vpx_highbd_sad4x4x8_bits12, vpx_highbd_sad4x4x4d_bits12)
1416 "cm->bit_depth should be VPX_BITS_8, "
1417 "VPX_BITS_10 or VPX_BITS_12");
1421 #endif // CONFIG_VP9_HIGHBITDEPTH
1423 static void realloc_segmentation_maps(VP9_COMP *cpi) {
1424 VP9_COMMON *const cm = &cpi->common;
1426 // Create the encoder segmentation map and set all entries to 0
1427 vpx_free(cpi->segmentation_map);
1428 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1429 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1431 // Create a map used for cyclic background refresh.
1432 if (cpi->cyclic_refresh) vp9_cyclic_refresh_free(cpi->cyclic_refresh);
1433 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
1434 vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
1436 // Create a map used to mark inactive areas.
1437 vpx_free(cpi->active_map.map);
1438 CHECK_MEM_ERROR(cm, cpi->active_map.map,
1439 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1441 // And a place holder structure is the coding context
1442 // for use if we want to save and restore it
1443 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
1444 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1445 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1448 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
1449 VP9_COMMON *const cm = &cpi->common;
1450 RATE_CONTROL *const rc = &cpi->rc;
1451 int last_w = cpi->oxcf.width;
1452 int last_h = cpi->oxcf.height;
1454 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
1455 cm->bit_depth = oxcf->bit_depth;
1456 cm->color_space = oxcf->color_space;
1457 cm->color_range = oxcf->color_range;
1459 cpi->target_level = oxcf->target_level;
1460 cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
1461 set_level_constraint(&cpi->level_constraint,
1462 get_level_index(cpi->target_level));
1464 if (cm->profile <= PROFILE_1)
1465 assert(cm->bit_depth == VPX_BITS_8);
1467 assert(cm->bit_depth > VPX_BITS_8);
1470 #if CONFIG_VP9_HIGHBITDEPTH
1471 cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
1472 #endif // CONFIG_VP9_HIGHBITDEPTH
1474 if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
1475 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
1477 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
1480 cpi->refresh_golden_frame = 0;
1481 cpi->refresh_last_frame = 1;
1482 cm->refresh_frame_context = 1;
1483 cm->reset_frame_context = 0;
1485 vp9_reset_segment_features(&cm->seg);
1486 vp9_set_high_precision_mv(cpi, 0);
1491 for (i = 0; i < MAX_SEGMENTS; i++)
1492 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1494 cpi->encode_breakout = cpi->oxcf.encode_breakout;
1496 set_rc_buffer_sizes(rc, &cpi->oxcf);
1498 // Under a configuration change, where maximum_buffer_size may change,
1499 // keep buffer level clipped to the maximum allowed buffer size.
1500 rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
1501 rc->buffer_level = VPXMIN(rc->buffer_level, rc->maximum_buffer_size);
1503 // Set up frame rate and related parameters rate control values.
1504 vp9_new_framerate(cpi, cpi->framerate);
1506 // Set absolute upper and lower quality limits
1507 rc->worst_quality = cpi->oxcf.worst_allowed_q;
1508 rc->best_quality = cpi->oxcf.best_allowed_q;
1510 cm->interp_filter = cpi->sf.default_interp_filter;
1512 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
1513 cm->render_width = cpi->oxcf.render_width;
1514 cm->render_height = cpi->oxcf.render_height;
1516 cm->render_width = cpi->oxcf.width;
1517 cm->render_height = cpi->oxcf.height;
1519 if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
1520 cm->width = cpi->oxcf.width;
1521 cm->height = cpi->oxcf.height;
1522 cpi->external_resize = 1;
1525 if (cpi->initial_width) {
1526 int new_mi_size = 0;
1527 vp9_set_mb_mi(cm, cm->width, cm->height);
1528 new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
1529 if (cm->mi_alloc_size < new_mi_size) {
1530 vp9_free_context_buffers(cm);
1531 alloc_compressor_data(cpi);
1532 realloc_segmentation_maps(cpi);
1533 cpi->initial_width = cpi->initial_height = 0;
1534 cpi->external_resize = 0;
1535 } else if (cm->mi_alloc_size == new_mi_size &&
1536 (cpi->oxcf.width > last_w || cpi->oxcf.height > last_h)) {
1537 vp9_alloc_loop_filter(cm);
1541 if (cm->current_video_frame == 0 || last_w != cpi->oxcf.width ||
1542 last_h != cpi->oxcf.height)
1543 update_frame_size(cpi);
1545 if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
1546 memset(cpi->consec_zero_mv, 0,
1547 cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
1548 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1549 vp9_cyclic_refresh_reset_resize(cpi);
1552 if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
1553 ((cpi->svc.number_temporal_layers > 1 ||
1554 cpi->svc.number_spatial_layers > 1) &&
1555 cpi->oxcf.pass != 1)) {
1556 vp9_update_layer_context_change_config(cpi,
1557 (int)cpi->oxcf.target_bandwidth);
1560 cpi->alt_ref_source = NULL;
1561 rc->is_src_frame_alt_ref = 0;
1564 // Experimental RD Code
1565 cpi->frame_distortion = 0;
1566 cpi->last_frame_distortion = 0;
1569 set_tile_limits(cpi);
1571 cpi->ext_refresh_frame_flags_pending = 0;
1572 cpi->ext_refresh_frame_context_pending = 0;
1574 #if CONFIG_VP9_HIGHBITDEPTH
1575 highbd_set_var_fns(cpi);
1578 // Enable multi-threading for first pass.
1580 if (((cpi->oxcf.mode == GOOD || cpi->oxcf.mode == BEST) &&
1581 cpi->oxcf.speed < 5 && cpi->oxcf.pass == 1) &&
1582 cpi->oxcf.new_mt && !cpi->use_svc)
1585 if (cpi->oxcf.mode == GOOD && cpi->oxcf.speed < 5 &&
1586 (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) && cpi->oxcf.new_mt &&
1592 #define M_LOG2_E 0.693147180559945309417
1594 #define log2f(x) (log(x) / (float)M_LOG2_E)
1596 /***********************************************************************
1597 * Read before modifying 'cal_nmvjointsadcost' or 'cal_nmvsadcosts' *
1598 ***********************************************************************
1599 * The following 2 functions ('cal_nmvjointsadcost' and *
1600 * 'cal_nmvsadcosts') are used to calculate cost lookup tables *
1601 * used by 'vp9_diamond_search_sad'. The C implementation of the *
1602 * function is generic, but the AVX intrinsics optimised version *
1603 * relies on the following properties of the computed tables: *
1604 * For cal_nmvjointsadcost: *
1605 * - mvjointsadcost[1] == mvjointsadcost[2] == mvjointsadcost[3] *
1606 * For cal_nmvsadcosts: *
1607 * - For all i: mvsadcost[0][i] == mvsadcost[1][i] *
1608 * (Equal costs for both components) *
1609 * - For all i: mvsadcost[0][i] == mvsadcost[0][-i] *
1610 * (Cost function is even) *
1611 * If these do not hold, then the AVX optimised version of the *
1612 * 'vp9_diamond_search_sad' function cannot be used as it is, in which *
1613 * case you can revert to using the C function instead. *
1614 ***********************************************************************/
1616 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1617 /*********************************************************************
1618 * Warning: Read the comments above before modifying this function *
1619 *********************************************************************/
1620 mvjointsadcost[0] = 600;
1621 mvjointsadcost[1] = 300;
1622 mvjointsadcost[2] = 300;
1623 mvjointsadcost[3] = 300;
1626 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1627 /*********************************************************************
1628 * Warning: Read the comments above before modifying this function *
1629 *********************************************************************/
1632 mvsadcost[0][0] = 0;
1633 mvsadcost[1][0] = 0;
1636 double z = 256 * (2 * (log2f(8 * i) + .6));
1637 mvsadcost[0][i] = (int)z;
1638 mvsadcost[1][i] = (int)z;
1639 mvsadcost[0][-i] = (int)z;
1640 mvsadcost[1][-i] = (int)z;
1641 } while (++i <= MV_MAX);
1644 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1647 mvsadcost[0][0] = 0;
1648 mvsadcost[1][0] = 0;
1651 double z = 256 * (2 * (log2f(8 * i) + .6));
1652 mvsadcost[0][i] = (int)z;
1653 mvsadcost[1][i] = (int)z;
1654 mvsadcost[0][-i] = (int)z;
1655 mvsadcost[1][-i] = (int)z;
1656 } while (++i <= MV_MAX);
1659 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf,
1660 BufferPool *const pool) {
1662 VP9_COMP *volatile const cpi = vpx_memalign(32, sizeof(VP9_COMP));
1663 VP9_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
1665 if (!cm) return NULL;
1669 if (setjmp(cm->error.jmp)) {
1670 cm->error.setjmp = 0;
1671 vp9_remove_compressor(cpi);
1675 cm->error.setjmp = 1;
1676 cm->alloc_mi = vp9_enc_alloc_mi;
1677 cm->free_mi = vp9_enc_free_mi;
1678 cm->setup_mi = vp9_enc_setup_mi;
1680 CHECK_MEM_ERROR(cm, cm->fc, (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
1682 cm, cm->frame_contexts,
1683 (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
1686 cpi->resize_state = 0;
1687 cpi->external_resize = 0;
1688 cpi->resize_avg_qp = 0;
1689 cpi->resize_buffer_underflow = 0;
1690 cpi->use_skin_detection = 0;
1691 cpi->common.buffer_pool = pool;
1693 cpi->force_update_segmentation = 0;
1695 init_config(cpi, oxcf);
1696 vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
1698 cm->current_video_frame = 0;
1699 cpi->partition_search_skippable_frame = 0;
1700 cpi->tile_data = NULL;
1702 realloc_segmentation_maps(cpi);
1704 CHECK_MEM_ERROR(cm, cpi->alt_ref_aq, vp9_alt_ref_aq_create());
1707 cm, cpi->consec_zero_mv,
1708 vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
1710 CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
1711 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
1712 CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
1713 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
1714 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
1715 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
1716 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
1717 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
1718 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
1719 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
1720 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
1721 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
1722 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
1723 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
1724 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
1725 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
1727 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
1730 cm, cpi->mbgraph_stats[i].mb_stats,
1731 vpx_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1734 #if CONFIG_FP_MB_STATS
1735 cpi->use_fp_mb_stats = 0;
1736 if (cpi->use_fp_mb_stats) {
1737 // a place holder used to store the first pass mb stats in the first pass
1738 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
1739 vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
1741 cpi->twopass.frame_mb_stats_buf = NULL;
1745 #if ENABLE_MT_BIT_MATCH
1747 cm, cpi->twopass.fp_mb_float_stats,
1748 vpx_calloc(cm->MBs * sizeof(*cpi->twopass.fp_mb_float_stats), 1));
1751 cpi->refresh_alt_ref_frame = 0;
1752 cpi->multi_arf_last_grp_enabled = 0;
1754 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1756 init_level_info(&cpi->level_info);
1757 init_level_constraint(&cpi->level_constraint);
1759 #if CONFIG_INTERNAL_STATS
1760 cpi->b_calculate_blockiness = 1;
1761 cpi->b_calculate_consistency = 1;
1762 cpi->total_inconsistency = 0;
1763 cpi->psnr.worst = 100.0;
1764 cpi->worst_ssim = 100.0;
1769 if (cpi->b_calculate_psnr) {
1770 cpi->total_sq_error = 0;
1771 cpi->total_samples = 0;
1773 cpi->totalp_sq_error = 0;
1774 cpi->totalp_samples = 0;
1776 cpi->tot_recode_hits = 0;
1777 cpi->summed_quality = 0;
1778 cpi->summed_weights = 0;
1779 cpi->summedp_quality = 0;
1780 cpi->summedp_weights = 0;
1783 cpi->fastssim.worst = 100.0;
1785 cpi->psnrhvs.worst = 100.0;
1787 if (cpi->b_calculate_blockiness) {
1788 cpi->total_blockiness = 0;
1789 cpi->worst_blockiness = 0.0;
1792 if (cpi->b_calculate_consistency) {
1793 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
1794 vpx_malloc(sizeof(*cpi->ssim_vars) * 4 *
1795 cpi->common.mi_rows * cpi->common.mi_cols));
1796 cpi->worst_consistency = 100.0;
1801 cpi->first_time_stamp_ever = INT64_MAX;
1803 /*********************************************************************
1804 * Warning: Read the comments around 'cal_nmvjointsadcost' and *
1805 * 'cal_nmvsadcosts' before modifying how these tables are computed. *
1806 *********************************************************************/
1807 cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
1808 cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
1809 cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
1810 cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
1811 cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
1812 cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
1814 cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
1815 cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
1816 cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
1817 cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
1818 cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
1820 #if CONFIG_VP9_TEMPORAL_DENOISING
1821 #ifdef OUTPUT_YUV_DENOISED
1822 yuv_denoised_file = fopen("denoised.yuv", "ab");
1825 #ifdef OUTPUT_YUV_SKINMAP
1826 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
1828 #ifdef OUTPUT_YUV_REC
1829 yuv_rec_file = fopen("rec.yuv", "wb");
1833 framepsnr = fopen("framepsnr.stt", "a");
1834 kf_list = fopen("kf_list.stt", "w");
1837 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
1839 if (oxcf->pass == 1) {
1840 vp9_init_first_pass(cpi);
1841 } else if (oxcf->pass == 2) {
1842 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
1843 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1845 if (cpi->svc.number_spatial_layers > 1 ||
1846 cpi->svc.number_temporal_layers > 1) {
1847 FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
1848 FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = { 0 };
1851 for (i = 0; i < oxcf->ss_number_layers; ++i) {
1852 FIRSTPASS_STATS *const last_packet_for_layer =
1853 &stats[packets - oxcf->ss_number_layers + i];
1854 const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
1855 const int packets_in_layer = (int)last_packet_for_layer->count + 1;
1856 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
1857 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
1859 vpx_free(lc->rc_twopass_stats_in.buf);
1861 lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
1862 CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
1863 vpx_malloc(lc->rc_twopass_stats_in.sz));
1864 lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
1865 lc->twopass.stats_in = lc->twopass.stats_in_start;
1866 lc->twopass.stats_in_end =
1867 lc->twopass.stats_in_start + packets_in_layer - 1;
1868 stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
1872 for (i = 0; i < packets; ++i) {
1873 const int layer_id = (int)stats[i].spatial_layer_id;
1874 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers &&
1875 stats_copy[layer_id] != NULL) {
1876 *stats_copy[layer_id] = stats[i];
1877 ++stats_copy[layer_id];
1881 vp9_init_second_pass_spatial_svc(cpi);
1883 #if CONFIG_FP_MB_STATS
1884 if (cpi->use_fp_mb_stats) {
1885 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
1886 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
1888 cpi->twopass.firstpass_mb_stats.mb_stats_start =
1889 oxcf->firstpass_mb_stats_in.buf;
1890 cpi->twopass.firstpass_mb_stats.mb_stats_end =
1891 cpi->twopass.firstpass_mb_stats.mb_stats_start +
1892 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
1896 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1897 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1898 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
1900 vp9_init_second_pass(cpi);
1904 vp9_set_speed_features_framesize_independent(cpi);
1905 vp9_set_speed_features_framesize_dependent(cpi);
1907 // Allocate memory to store variances for a frame.
1908 CHECK_MEM_ERROR(cm, cpi->source_diff_var, vpx_calloc(cm->MBs, sizeof(diff)));
1909 cpi->source_var_thresh = 0;
1910 cpi->frames_till_next_var_check = 0;
1912 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
1913 cpi->fn_ptr[BT].sdf = SDF; \
1914 cpi->fn_ptr[BT].sdaf = SDAF; \
1915 cpi->fn_ptr[BT].vf = VF; \
1916 cpi->fn_ptr[BT].svf = SVF; \
1917 cpi->fn_ptr[BT].svaf = SVAF; \
1918 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1919 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1920 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1922 BFP(BLOCK_32X16, vpx_sad32x16, vpx_sad32x16_avg, vpx_variance32x16,
1923 vpx_sub_pixel_variance32x16, vpx_sub_pixel_avg_variance32x16, NULL, NULL,
1926 BFP(BLOCK_16X32, vpx_sad16x32, vpx_sad16x32_avg, vpx_variance16x32,
1927 vpx_sub_pixel_variance16x32, vpx_sub_pixel_avg_variance16x32, NULL, NULL,
1930 BFP(BLOCK_64X32, vpx_sad64x32, vpx_sad64x32_avg, vpx_variance64x32,
1931 vpx_sub_pixel_variance64x32, vpx_sub_pixel_avg_variance64x32, NULL, NULL,
1934 BFP(BLOCK_32X64, vpx_sad32x64, vpx_sad32x64_avg, vpx_variance32x64,
1935 vpx_sub_pixel_variance32x64, vpx_sub_pixel_avg_variance32x64, NULL, NULL,
1938 BFP(BLOCK_32X32, vpx_sad32x32, vpx_sad32x32_avg, vpx_variance32x32,
1939 vpx_sub_pixel_variance32x32, vpx_sub_pixel_avg_variance32x32,
1940 vpx_sad32x32x3, vpx_sad32x32x8, vpx_sad32x32x4d)
1942 BFP(BLOCK_64X64, vpx_sad64x64, vpx_sad64x64_avg, vpx_variance64x64,
1943 vpx_sub_pixel_variance64x64, vpx_sub_pixel_avg_variance64x64,
1944 vpx_sad64x64x3, vpx_sad64x64x8, vpx_sad64x64x4d)
1946 BFP(BLOCK_16X16, vpx_sad16x16, vpx_sad16x16_avg, vpx_variance16x16,
1947 vpx_sub_pixel_variance16x16, vpx_sub_pixel_avg_variance16x16,
1948 vpx_sad16x16x3, vpx_sad16x16x8, vpx_sad16x16x4d)
1950 BFP(BLOCK_16X8, vpx_sad16x8, vpx_sad16x8_avg, vpx_variance16x8,
1951 vpx_sub_pixel_variance16x8, vpx_sub_pixel_avg_variance16x8, vpx_sad16x8x3,
1952 vpx_sad16x8x8, vpx_sad16x8x4d)
1954 BFP(BLOCK_8X16, vpx_sad8x16, vpx_sad8x16_avg, vpx_variance8x16,
1955 vpx_sub_pixel_variance8x16, vpx_sub_pixel_avg_variance8x16, vpx_sad8x16x3,
1956 vpx_sad8x16x8, vpx_sad8x16x4d)
1958 BFP(BLOCK_8X8, vpx_sad8x8, vpx_sad8x8_avg, vpx_variance8x8,
1959 vpx_sub_pixel_variance8x8, vpx_sub_pixel_avg_variance8x8, vpx_sad8x8x3,
1960 vpx_sad8x8x8, vpx_sad8x8x4d)
1962 BFP(BLOCK_8X4, vpx_sad8x4, vpx_sad8x4_avg, vpx_variance8x4,
1963 vpx_sub_pixel_variance8x4, vpx_sub_pixel_avg_variance8x4, NULL,
1964 vpx_sad8x4x8, vpx_sad8x4x4d)
1966 BFP(BLOCK_4X8, vpx_sad4x8, vpx_sad4x8_avg, vpx_variance4x8,
1967 vpx_sub_pixel_variance4x8, vpx_sub_pixel_avg_variance4x8, NULL,
1968 vpx_sad4x8x8, vpx_sad4x8x4d)
1970 BFP(BLOCK_4X4, vpx_sad4x4, vpx_sad4x4_avg, vpx_variance4x4,
1971 vpx_sub_pixel_variance4x4, vpx_sub_pixel_avg_variance4x4, vpx_sad4x4x3,
1972 vpx_sad4x4x8, vpx_sad4x4x4d)
1974 #if CONFIG_VP9_HIGHBITDEPTH
1975 highbd_set_var_fns(cpi);
1978 /* vp9_init_quantizer() is first called here. Add check in
1979 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1980 * called later when needed. This will avoid unnecessary calls of
1981 * vp9_init_quantizer() for every frame.
1983 vp9_init_quantizer(cpi);
1985 vp9_loop_filter_init(cm);
1987 cm->error.setjmp = 0;
1992 #if CONFIG_INTERNAL_STATS
1993 #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
1995 #define SNPRINT2(H, T, V) \
1996 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
1997 #endif // CONFIG_INTERNAL_STATS
1999 void vp9_remove_compressor(VP9_COMP *cpi) {
2007 if (cm->current_video_frame > 0) {
2008 #if CONFIG_INTERNAL_STATS
2009 vpx_clear_system_state();
2011 if (cpi->oxcf.pass != 1) {
2012 char headings[512] = { 0 };
2013 char results[512] = { 0 };
2014 FILE *f = fopen("opsnr.stt", "a");
2015 double time_encoded =
2016 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2018 double total_encode_time =
2019 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2021 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2022 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2023 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2024 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2026 if (cpi->b_calculate_psnr) {
2027 const double total_psnr = vpx_sse_to_psnr(
2028 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2029 const double totalp_psnr = vpx_sse_to_psnr(
2030 (double)cpi->totalp_samples, peak, (double)cpi->totalp_sq_error);
2031 const double total_ssim =
2032 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2033 const double totalp_ssim =
2034 100 * pow(cpi->summedp_quality / cpi->summedp_weights, 8.0);
2036 snprintf(headings, sizeof(headings),
2037 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
2038 "VPXSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
2039 "WstPsnr\tWstSsim\tWstFast\tWstHVS");
2040 snprintf(results, sizeof(results),
2041 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2042 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2043 "%7.3f\t%7.3f\t%7.3f\t%7.3f",
2044 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
2045 cpi->psnrp.stat[ALL] / cpi->count, totalp_psnr, total_ssim,
2046 totalp_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2047 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
2048 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
2050 if (cpi->b_calculate_blockiness) {
2051 SNPRINT(headings, "\t Block\tWstBlck");
2052 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2053 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2056 if (cpi->b_calculate_consistency) {
2057 double consistency =
2058 vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
2059 (double)cpi->total_inconsistency);
2061 SNPRINT(headings, "\tConsist\tWstCons");
2062 SNPRINT2(results, "\t%7.3f", consistency);
2063 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2066 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
2067 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
2068 rate_err, fabs(rate_err));
2078 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2079 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2080 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2081 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2082 cpi->time_compress_data / 1000,
2083 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2088 #if CONFIG_VP9_TEMPORAL_DENOISING
2089 vp9_denoiser_free(&(cpi->denoiser));
2092 for (t = 0; t < cpi->num_workers; ++t) {
2093 VPxWorker *const worker = &cpi->workers[t];
2094 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2096 // Deallocate allocated threads.
2097 vpx_get_worker_interface()->end(worker);
2099 // Deallocate allocated thread data.
2100 if (t < cpi->num_workers - 1) {
2101 vpx_free(thread_data->td->counts);
2102 vp9_free_pc_tree(thread_data->td);
2103 vpx_free(thread_data->td);
2106 vpx_free(cpi->tile_thr_data);
2107 vpx_free(cpi->workers);
2108 vp9_row_mt_mem_dealloc(cpi);
2110 if (cpi->num_workers > 1) {
2111 vp9_loop_filter_dealloc(&cpi->lf_row_sync);
2112 vp9_bitstream_encode_tiles_buffer_dealloc(cpi);
2115 vp9_alt_ref_aq_destroy(cpi->alt_ref_aq);
2117 dealloc_compressor_data(cpi);
2119 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2121 vpx_free(cpi->mbgraph_stats[i].mb_stats);
2124 #if CONFIG_FP_MB_STATS
2125 if (cpi->use_fp_mb_stats) {
2126 vpx_free(cpi->twopass.frame_mb_stats_buf);
2127 cpi->twopass.frame_mb_stats_buf = NULL;
2131 #if ENABLE_MT_BIT_MATCH
2132 vpx_free(cpi->twopass.fp_mb_float_stats);
2133 cpi->twopass.fp_mb_float_stats = NULL;
2136 vp9_remove_common(cm);
2137 vp9_free_ref_frame_buffers(cm->buffer_pool);
2138 #if CONFIG_VP9_POSTPROC
2139 vp9_free_postproc_buffers(cm);
2143 #if CONFIG_VP9_TEMPORAL_DENOISING
2144 #ifdef OUTPUT_YUV_DENOISED
2145 fclose(yuv_denoised_file);
2148 #ifdef OUTPUT_YUV_SKINMAP
2149 fclose(yuv_skinmap_file);
2151 #ifdef OUTPUT_YUV_REC
2152 fclose(yuv_rec_file);
2169 static void generate_psnr_packet(VP9_COMP *cpi) {
2170 struct vpx_codec_cx_pkt pkt;
2173 #if CONFIG_VP9_HIGHBITDEPTH
2174 vpx_calc_highbd_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr,
2175 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2177 vpx_calc_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, &psnr);
2180 for (i = 0; i < 4; ++i) {
2181 pkt.data.psnr.samples[i] = psnr.samples[i];
2182 pkt.data.psnr.sse[i] = psnr.sse[i];
2183 pkt.data.psnr.psnr[i] = psnr.psnr[i];
2185 pkt.kind = VPX_CODEC_PSNR_PKT;
2188 .layer_context[cpi->svc.spatial_layer_id *
2189 cpi->svc.number_temporal_layers]
2190 .psnr_pkt = pkt.data.psnr;
2192 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2195 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
2196 if (ref_frame_flags > 7) return -1;
2198 cpi->ref_frame_flags = ref_frame_flags;
2202 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
2203 cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
2204 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
2205 cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
2206 cpi->ext_refresh_frame_flags_pending = 1;
2209 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(
2210 VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag) {
2211 MV_REFERENCE_FRAME ref_frame = NONE;
2212 if (ref_frame_flag == VP9_LAST_FLAG)
2213 ref_frame = LAST_FRAME;
2214 else if (ref_frame_flag == VP9_GOLD_FLAG)
2215 ref_frame = GOLDEN_FRAME;
2216 else if (ref_frame_flag == VP9_ALT_FLAG)
2217 ref_frame = ALTREF_FRAME;
2219 return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2222 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2223 YV12_BUFFER_CONFIG *sd) {
2224 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2226 vp8_yv12_copy_frame(cfg, sd);
2233 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2234 YV12_BUFFER_CONFIG *sd) {
2235 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2237 vp8_yv12_copy_frame(sd, cfg);
2244 int vp9_update_entropy(VP9_COMP *cpi, int update) {
2245 cpi->ext_refresh_frame_context = update;
2246 cpi->ext_refresh_frame_context_pending = 1;
2250 #if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
2251 // The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
2252 // as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
2253 // not denoise the UV channels at this time. If ever we implement UV channel
2254 // denoising we will have to modify this.
2255 void vp9_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
2256 uint8_t *src = s->y_buffer;
2257 int h = s->y_height;
2260 fwrite(src, s->y_width, 1, f);
2268 fwrite(src, s->uv_width, 1, f);
2269 src += s->uv_stride;
2276 fwrite(src, s->uv_width, 1, f);
2277 src += s->uv_stride;
2282 #ifdef OUTPUT_YUV_REC
2283 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2284 YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2285 uint8_t *src = s->y_buffer;
2288 #if CONFIG_VP9_HIGHBITDEPTH
2289 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
2290 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
2293 fwrite(src16, s->y_width, 2, yuv_rec_file);
2294 src16 += s->y_stride;
2297 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
2301 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2302 src16 += s->uv_stride;
2305 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
2309 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2310 src16 += s->uv_stride;
2313 fflush(yuv_rec_file);
2316 #endif // CONFIG_VP9_HIGHBITDEPTH
2319 fwrite(src, s->y_width, 1, yuv_rec_file);
2327 fwrite(src, s->uv_width, 1, yuv_rec_file);
2328 src += s->uv_stride;
2335 fwrite(src, s->uv_width, 1, yuv_rec_file);
2336 src += s->uv_stride;
2339 fflush(yuv_rec_file);
2343 #if CONFIG_VP9_HIGHBITDEPTH
2344 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2345 YV12_BUFFER_CONFIG *dst,
2348 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2349 YV12_BUFFER_CONFIG *dst) {
2350 #endif // CONFIG_VP9_HIGHBITDEPTH
2351 // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
2353 const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
2355 const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
2356 const int src_widths[3] = { src->y_crop_width, src->uv_crop_width,
2357 src->uv_crop_width };
2358 const int src_heights[3] = { src->y_crop_height, src->uv_crop_height,
2359 src->uv_crop_height };
2360 uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
2361 const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
2362 const int dst_widths[3] = { dst->y_crop_width, dst->uv_crop_width,
2363 dst->uv_crop_width };
2364 const int dst_heights[3] = { dst->y_crop_height, dst->uv_crop_height,
2365 dst->uv_crop_height };
2367 for (i = 0; i < MAX_MB_PLANE; ++i) {
2368 #if CONFIG_VP9_HIGHBITDEPTH
2369 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2370 vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
2371 src_strides[i], dsts[i], dst_heights[i],
2372 dst_widths[i], dst_strides[i], bd);
2374 vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2375 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2378 vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2379 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2380 #endif // CONFIG_VP9_HIGHBITDEPTH
2382 vpx_extend_frame_borders(dst);
2385 #if CONFIG_VP9_HIGHBITDEPTH
2386 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2387 YV12_BUFFER_CONFIG *dst, int bd) {
2388 const int src_w = src->y_crop_width;
2389 const int src_h = src->y_crop_height;
2390 const int dst_w = dst->y_crop_width;
2391 const int dst_h = dst->y_crop_height;
2392 const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
2394 const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
2395 uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
2396 const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
2397 const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP];
2400 for (i = 0; i < MAX_MB_PLANE; ++i) {
2401 const int factor = (i == 0 || i == 3 ? 1 : 2);
2402 const int src_stride = src_strides[i];
2403 const int dst_stride = dst_strides[i];
2404 for (y = 0; y < dst_h; y += 16) {
2405 const int y_q4 = y * (16 / factor) * src_h / dst_h;
2406 for (x = 0; x < dst_w; x += 16) {
2407 const int x_q4 = x * (16 / factor) * src_w / dst_w;
2408 const uint8_t *src_ptr = srcs[i] +
2409 (y / factor) * src_h / dst_h * src_stride +
2410 (x / factor) * src_w / dst_w;
2411 uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
2413 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2414 vpx_highbd_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2415 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2416 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2417 16 / factor, 16 / factor, bd);
2419 vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
2420 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2421 kernel[y_q4 & 0xf], 16 * src_h / dst_h, 16 / factor,
2428 vpx_extend_frame_borders(dst);
2431 void vp9_scale_and_extend_frame_c(const YV12_BUFFER_CONFIG *src,
2432 YV12_BUFFER_CONFIG *dst) {
2433 const int src_w = src->y_crop_width;
2434 const int src_h = src->y_crop_height;
2435 const int dst_w = dst->y_crop_width;
2436 const int dst_h = dst->y_crop_height;
2437 const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
2439 const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
2440 uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
2441 const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
2442 const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP];
2445 for (i = 0; i < MAX_MB_PLANE; ++i) {
2446 const int factor = (i == 0 || i == 3 ? 1 : 2);
2447 const int src_stride = src_strides[i];
2448 const int dst_stride = dst_strides[i];
2449 for (y = 0; y < dst_h; y += 16) {
2450 const int y_q4 = y * (16 / factor) * src_h / dst_h;
2451 for (x = 0; x < dst_w; x += 16) {
2452 const int x_q4 = x * (16 / factor) * src_w / dst_w;
2453 const uint8_t *src_ptr = srcs[i] +
2454 (y / factor) * src_h / dst_h * src_stride +
2455 (x / factor) * src_w / dst_w;
2456 uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
2458 vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
2459 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2460 kernel[y_q4 & 0xf], 16 * src_h / dst_h, 16 / factor,
2466 vpx_extend_frame_borders(dst);
2468 #endif // CONFIG_VP9_HIGHBITDEPTH
2470 static int scale_down(VP9_COMP *cpi, int q) {
2471 RATE_CONTROL *const rc = &cpi->rc;
2472 GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2474 assert(frame_is_kf_gf_arf(cpi));
2476 if (rc->frame_size_selector == UNSCALED &&
2477 q >= rc->rf_level_maxq[gf_group->rf_level[gf_group->index]]) {
2478 const int max_size_thresh =
2479 (int)(rate_thresh_mult[SCALE_STEP1] *
2480 VPXMAX(rc->this_frame_target, rc->avg_frame_bandwidth));
2481 scale = rc->projected_frame_size > max_size_thresh ? 1 : 0;
2486 static int big_rate_miss(VP9_COMP *cpi, int high_limit, int low_limit) {
2487 const RATE_CONTROL *const rc = &cpi->rc;
2489 return (rc->projected_frame_size > ((high_limit * 3) / 2)) ||
2490 (rc->projected_frame_size < (low_limit / 2));
2493 // test in two pass for the first
2494 static int two_pass_first_group_inter(VP9_COMP *cpi) {
2495 TWO_PASS *const twopass = &cpi->twopass;
2496 GF_GROUP *const gf_group = &twopass->gf_group;
2497 if ((cpi->oxcf.pass == 2) &&
2498 (gf_group->index == gf_group->first_inter_index)) {
2505 // Function to test for conditions that indicate we should loop
2506 // back and recode a frame.
2507 static int recode_loop_test(VP9_COMP *cpi, int high_limit, int low_limit, int q,
2508 int maxq, int minq) {
2509 const RATE_CONTROL *const rc = &cpi->rc;
2510 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2511 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
2512 int force_recode = 0;
2514 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
2515 big_rate_miss(cpi, high_limit, low_limit) ||
2516 (cpi->sf.recode_loop == ALLOW_RECODE) ||
2517 (two_pass_first_group_inter(cpi) &&
2518 (cpi->sf.recode_loop == ALLOW_RECODE_FIRST)) ||
2519 (frame_is_kfgfarf && (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF))) {
2520 if (frame_is_kfgfarf && (oxcf->resize_mode == RESIZE_DYNAMIC) &&
2521 scale_down(cpi, q)) {
2522 // Code this group at a lower resolution.
2523 cpi->resize_pending = 1;
2526 // Force recode if projected_frame_size > max_frame_bandwidth
2527 if (rc->projected_frame_size >= rc->max_frame_bandwidth) return 1;
2529 // TODO(agrange) high_limit could be greater than the scale-down threshold.
2530 if ((rc->projected_frame_size > high_limit && q < maxq) ||
2531 (rc->projected_frame_size < low_limit && q > minq)) {
2533 } else if (cpi->oxcf.rc_mode == VPX_CQ) {
2534 // Deal with frame undershoot and whether or not we are
2535 // below the automatically set cq level.
2536 if (q > oxcf->cq_level &&
2537 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2542 return force_recode;
2545 void vp9_update_reference_frames(VP9_COMP *cpi) {
2546 VP9_COMMON *const cm = &cpi->common;
2547 BufferPool *const pool = cm->buffer_pool;
2549 // At this point the new frame has been encoded.
2550 // If any buffer copy / swapping is signaled it should be done here.
2551 if (cm->frame_type == KEY_FRAME) {
2552 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
2554 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
2556 } else if (vp9_preserve_existing_gf(cpi)) {
2557 // We have decided to preserve the previously existing golden frame as our
2558 // new ARF frame. However, in the short term in function
2559 // vp9_get_refresh_mask() we left it in the GF slot and, if
2560 // we're updating the GF with the current decoded frame, we save it to the
2561 // ARF slot instead.
2562 // We now have to update the ARF with the current frame and swap gld_fb_idx
2563 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
2564 // slot and, if we're updating the GF, the current frame becomes the new GF.
2567 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
2570 tmp = cpi->alt_fb_idx;
2571 cpi->alt_fb_idx = cpi->gld_fb_idx;
2572 cpi->gld_fb_idx = tmp;
2574 if (is_two_pass_svc(cpi)) {
2575 cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
2576 cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
2578 } else { /* For non key/golden frames */
2579 if (cpi->refresh_alt_ref_frame) {
2580 int arf_idx = cpi->alt_fb_idx;
2581 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
2582 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2583 arf_idx = gf_group->arf_update_idx[gf_group->index];
2586 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2587 memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
2588 cpi->interp_filter_selected[0],
2589 sizeof(cpi->interp_filter_selected[0]));
2592 if (cpi->refresh_golden_frame) {
2593 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
2595 if (!cpi->rc.is_src_frame_alt_ref)
2596 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2597 cpi->interp_filter_selected[0],
2598 sizeof(cpi->interp_filter_selected[0]));
2600 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2601 cpi->interp_filter_selected[ALTREF_FRAME],
2602 sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
2606 if (cpi->refresh_last_frame) {
2607 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
2609 if (!cpi->rc.is_src_frame_alt_ref)
2610 memcpy(cpi->interp_filter_selected[LAST_FRAME],
2611 cpi->interp_filter_selected[0],
2612 sizeof(cpi->interp_filter_selected[0]));
2614 #if CONFIG_VP9_TEMPORAL_DENOISING
2615 if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
2616 cpi->denoiser.denoising_level > kDenLowLow) {
2617 vp9_denoiser_update_frame_info(
2618 &cpi->denoiser, *cpi->Source, cpi->common.frame_type,
2619 cpi->refresh_alt_ref_frame, cpi->refresh_golden_frame,
2620 cpi->refresh_last_frame, cpi->resize_pending);
2623 if (is_one_pass_cbr_svc(cpi)) {
2624 // Keep track of frame index for each reference frame.
2625 SVC *const svc = &cpi->svc;
2626 if (cm->frame_type == KEY_FRAME) {
2627 svc->ref_frame_index[cpi->lst_fb_idx] = svc->current_superframe;
2628 svc->ref_frame_index[cpi->gld_fb_idx] = svc->current_superframe;
2629 svc->ref_frame_index[cpi->alt_fb_idx] = svc->current_superframe;
2631 if (cpi->refresh_last_frame)
2632 svc->ref_frame_index[cpi->lst_fb_idx] = svc->current_superframe;
2633 if (cpi->refresh_golden_frame)
2634 svc->ref_frame_index[cpi->gld_fb_idx] = svc->current_superframe;
2635 if (cpi->refresh_alt_ref_frame)
2636 svc->ref_frame_index[cpi->alt_fb_idx] = svc->current_superframe;
2641 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2642 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2643 struct loopfilter *lf = &cm->lf;
2646 lf->filter_level = 0;
2647 lf->last_filt_level = 0;
2649 struct vpx_usec_timer timer;
2651 vpx_clear_system_state();
2653 vpx_usec_timer_start(&timer);
2655 if (!cpi->rc.is_src_frame_alt_ref) {
2656 if ((cpi->common.frame_type == KEY_FRAME) &&
2657 (!cpi->rc.this_key_frame_forced)) {
2658 lf->last_filt_level = 0;
2660 vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
2661 lf->last_filt_level = lf->filter_level;
2663 lf->filter_level = 0;
2666 vpx_usec_timer_mark(&timer);
2667 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2670 if (lf->filter_level > 0) {
2671 vp9_build_mask_frame(cm, lf->filter_level, 0);
2673 if (cpi->num_workers > 1)
2674 vp9_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
2675 lf->filter_level, 0, 0, cpi->workers,
2676 cpi->num_workers, &cpi->lf_row_sync);
2678 vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
2681 vpx_extend_frame_inner_borders(cm->frame_to_show);
2684 static INLINE void alloc_frame_mvs(VP9_COMMON *const cm, int buffer_idx) {
2685 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
2686 if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
2687 new_fb_ptr->mi_cols < cm->mi_cols) {
2688 vpx_free(new_fb_ptr->mvs);
2689 CHECK_MEM_ERROR(cm, new_fb_ptr->mvs,
2690 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
2691 sizeof(*new_fb_ptr->mvs)));
2692 new_fb_ptr->mi_rows = cm->mi_rows;
2693 new_fb_ptr->mi_cols = cm->mi_cols;
2697 void vp9_scale_references(VP9_COMP *cpi) {
2698 VP9_COMMON *cm = &cpi->common;
2699 MV_REFERENCE_FRAME ref_frame;
2700 const VP9_REFFRAME ref_mask[3] = { VP9_LAST_FLAG, VP9_GOLD_FLAG,
2703 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2704 // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
2705 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
2706 BufferPool *const pool = cm->buffer_pool;
2707 const YV12_BUFFER_CONFIG *const ref =
2708 get_ref_frame_buffer(cpi, ref_frame);
2711 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
2715 #if CONFIG_VP9_HIGHBITDEPTH
2716 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2717 RefCntBuffer *new_fb_ptr = NULL;
2718 int force_scaling = 0;
2719 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
2720 if (new_fb == INVALID_IDX) {
2721 new_fb = get_free_fb(cm);
2724 if (new_fb == INVALID_IDX) return;
2725 new_fb_ptr = &pool->frame_bufs[new_fb];
2726 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
2727 new_fb_ptr->buf.y_crop_height != cm->height) {
2728 if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
2729 cm->subsampling_x, cm->subsampling_y,
2730 cm->use_highbitdepth,
2731 VP9_ENC_BORDER_IN_PIXELS,
2732 cm->byte_alignment, NULL, NULL, NULL))
2733 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2734 "Failed to allocate frame buffer");
2735 scale_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth);
2736 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2737 alloc_frame_mvs(cm, new_fb);
2740 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2741 RefCntBuffer *new_fb_ptr = NULL;
2742 int force_scaling = 0;
2743 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
2744 if (new_fb == INVALID_IDX) {
2745 new_fb = get_free_fb(cm);
2748 if (new_fb == INVALID_IDX) return;
2749 new_fb_ptr = &pool->frame_bufs[new_fb];
2750 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
2751 new_fb_ptr->buf.y_crop_height != cm->height) {
2752 if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
2753 cm->subsampling_x, cm->subsampling_y,
2754 VP9_ENC_BORDER_IN_PIXELS,
2755 cm->byte_alignment, NULL, NULL, NULL))
2756 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2757 "Failed to allocate frame buffer");
2758 vp9_scale_and_extend_frame(ref, &new_fb_ptr->buf);
2759 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2760 alloc_frame_mvs(cm, new_fb);
2762 #endif // CONFIG_VP9_HIGHBITDEPTH
2765 RefCntBuffer *buf = NULL;
2766 if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
2767 // Check for release of scaled reference.
2768 buf_idx = cpi->scaled_ref_idx[ref_frame - 1];
2769 buf = (buf_idx != INVALID_IDX) ? &pool->frame_bufs[buf_idx] : NULL;
2772 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
2775 buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
2776 buf = &pool->frame_bufs[buf_idx];
2777 buf->buf.y_crop_width = ref->y_crop_width;
2778 buf->buf.y_crop_height = ref->y_crop_height;
2779 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
2783 if (cpi->oxcf.pass != 0 || cpi->use_svc)
2784 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
2789 static void release_scaled_references(VP9_COMP *cpi) {
2790 VP9_COMMON *cm = &cpi->common;
2792 if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
2793 // Only release scaled references under certain conditions:
2794 // if reference will be updated, or if scaled reference has same resolution.
2796 refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
2797 refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
2798 refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
2799 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2800 const int idx = cpi->scaled_ref_idx[i - 1];
2801 RefCntBuffer *const buf =
2802 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
2803 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
2805 (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
2806 buf->buf.y_crop_height == ref->y_crop_height))) {
2808 cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
2812 for (i = 0; i < MAX_REF_FRAMES; ++i) {
2813 const int idx = cpi->scaled_ref_idx[i];
2814 RefCntBuffer *const buf =
2815 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
2818 cpi->scaled_ref_idx[i] = INVALID_IDX;
2824 static void full_to_model_count(unsigned int *model_count,
2825 unsigned int *full_count) {
2827 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
2828 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
2829 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
2830 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
2831 model_count[TWO_TOKEN] += full_count[n];
2832 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
2835 static void full_to_model_counts(vp9_coeff_count_model *model_count,
2836 vp9_coeff_count *full_count) {
2839 for (i = 0; i < PLANE_TYPES; ++i)
2840 for (j = 0; j < REF_TYPES; ++j)
2841 for (k = 0; k < COEF_BANDS; ++k)
2842 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
2843 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
2846 #if 0 && CONFIG_INTERNAL_STATS
2847 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
2848 VP9_COMMON *const cm = &cpi->common;
2849 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
2852 vpx_clear_system_state();
2854 #if CONFIG_VP9_HIGHBITDEPTH
2855 if (cm->use_highbitdepth) {
2856 recon_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2858 recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2861 recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2862 #endif // CONFIG_VP9_HIGHBITDEPTH
2865 if (cpi->twopass.total_left_stats.coded_error != 0.0) {
2866 double dc_quant_devisor;
2867 #if CONFIG_VP9_HIGHBITDEPTH
2868 switch (cm->bit_depth) {
2870 dc_quant_devisor = 4.0;
2873 dc_quant_devisor = 16.0;
2876 dc_quant_devisor = 64.0;
2879 assert(0 && "bit_depth must be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
2883 dc_quant_devisor = 4.0;
2886 fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
2887 "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
2888 "%10"PRId64" %10"PRId64" %10d "
2889 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2890 "%6d %6d %5d %5d %5d "
2891 "%10"PRId64" %10.3lf"
2892 "%10lf %8u %10"PRId64" %10d %10d %10d %10d %10d\n",
2893 cpi->common.current_video_frame,
2894 cm->width, cm->height,
2895 cpi->rc.source_alt_ref_pending,
2896 cpi->rc.source_alt_ref_active,
2897 cpi->rc.this_frame_target,
2898 cpi->rc.projected_frame_size,
2899 cpi->rc.projected_frame_size / cpi->common.MBs,
2900 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
2901 cpi->rc.vbr_bits_off_target,
2902 cpi->rc.vbr_bits_off_target_fast,
2903 cpi->twopass.extend_minq,
2904 cpi->twopass.extend_minq_fast,
2905 cpi->rc.total_target_vs_actual,
2906 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
2907 cpi->rc.total_actual_bits, cm->base_qindex,
2908 vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
2909 (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) /
2911 vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
2914 vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
2915 cpi->refresh_last_frame, cpi->refresh_golden_frame,
2916 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
2917 cpi->twopass.bits_left,
2918 cpi->twopass.total_left_stats.coded_error,
2919 cpi->twopass.bits_left /
2920 (1 + cpi->twopass.total_left_stats.coded_error),
2921 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
2922 cpi->twopass.kf_zeromotion_pct,
2923 cpi->twopass.fr_content_type,
2924 cm->lf.filter_level,
2925 cm->seg.aq_av_offset);
2930 FILE *const fmodes = fopen("Modes.stt", "a");
2933 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
2934 cm->frame_type, cpi->refresh_golden_frame,
2935 cpi->refresh_alt_ref_frame);
2937 for (i = 0; i < MAX_MODES; ++i)
2938 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
2940 fprintf(fmodes, "\n");
2947 static void set_mv_search_params(VP9_COMP *cpi) {
2948 const VP9_COMMON *const cm = &cpi->common;
2949 const unsigned int max_mv_def = VPXMIN(cm->width, cm->height);
2951 // Default based on max resolution.
2952 cpi->mv_step_param = vp9_init_search_range(max_mv_def);
2954 if (cpi->sf.mv.auto_mv_step_size) {
2955 if (frame_is_intra_only(cm)) {
2956 // Initialize max_mv_magnitude for use in the first INTER frame
2957 // after a key/intra-only frame.
2958 cpi->max_mv_magnitude = max_mv_def;
2960 if (cm->show_frame) {
2961 // Allow mv_steps to correspond to twice the max mv magnitude found
2962 // in the previous frame, capped by the default max_mv_magnitude based
2964 cpi->mv_step_param = vp9_init_search_range(
2965 VPXMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
2967 cpi->max_mv_magnitude = 0;
2972 static void set_size_independent_vars(VP9_COMP *cpi) {
2973 vp9_set_speed_features_framesize_independent(cpi);
2974 vp9_set_rd_speed_thresholds(cpi);
2975 vp9_set_rd_speed_thresholds_sub8x8(cpi);
2976 cpi->common.interp_filter = cpi->sf.default_interp_filter;
2979 static void set_size_dependent_vars(VP9_COMP *cpi, int *q, int *bottom_index,
2981 VP9_COMMON *const cm = &cpi->common;
2982 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2984 // Setup variables that depend on the dimensions of the frame.
2985 vp9_set_speed_features_framesize_dependent(cpi);
2987 // Decide q and q bounds.
2988 *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
2990 if (!frame_is_intra_only(cm)) {
2991 vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
2994 // Configure experimental use of segmentation for enhanced coding of
2995 // static regions if indicated.
2996 // Only allowed in the second pass of a two pass encode, as it requires
2997 // lagged coding, and if the relevant speed feature flag is set.
2998 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
2999 configure_static_seg_features(cpi);
3001 #if CONFIG_VP9_POSTPROC && !(CONFIG_VP9_TEMPORAL_DENOISING)
3002 if (oxcf->noise_sensitivity > 0) {
3004 switch (oxcf->noise_sensitivity) {
3005 case 1: l = 20; break;
3006 case 2: l = 40; break;
3007 case 3: l = 60; break;
3009 case 5: l = 100; break;
3010 case 6: l = 150; break;
3012 if (!cpi->common.postproc_state.limits) {
3013 cpi->common.postproc_state.limits = vpx_calloc(
3014 cpi->common.width, sizeof(*cpi->common.postproc_state.limits));
3016 vp9_denoise(cpi->Source, cpi->Source, l, cpi->common.postproc_state.limits);
3018 #endif // CONFIG_VP9_POSTPROC
3021 #if CONFIG_VP9_TEMPORAL_DENOISING
3022 static void setup_denoiser_buffer(VP9_COMP *cpi) {
3023 VP9_COMMON *const cm = &cpi->common;
3024 if (cpi->oxcf.noise_sensitivity > 0 &&
3025 !cpi->denoiser.frame_buffer_initialized) {
3026 if (vp9_denoiser_alloc(&cpi->denoiser, cm->width, cm->height,
3027 cm->subsampling_x, cm->subsampling_y,
3028 #if CONFIG_VP9_HIGHBITDEPTH
3029 cm->use_highbitdepth,
3031 VP9_ENC_BORDER_IN_PIXELS))
3032 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3033 "Failed to allocate denoiser");
3038 static void init_motion_estimation(VP9_COMP *cpi) {
3039 int y_stride = cpi->scaled_source.y_stride;
3041 if (cpi->sf.mv.search_method == NSTEP) {
3042 vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
3043 } else if (cpi->sf.mv.search_method == DIAMOND) {
3044 vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
3048 static void set_frame_size(VP9_COMP *cpi) {
3050 VP9_COMMON *const cm = &cpi->common;
3051 VP9EncoderConfig *const oxcf = &cpi->oxcf;
3052 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
3054 if (oxcf->pass == 2 && oxcf->rc_mode == VPX_VBR &&
3055 ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
3056 (oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending))) {
3057 calculate_coded_size(cpi, &oxcf->scaled_frame_width,
3058 &oxcf->scaled_frame_height);
3060 // There has been a change in frame size.
3061 vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3062 oxcf->scaled_frame_height);
3065 if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR && !cpi->use_svc &&
3066 oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending != 0) {
3067 oxcf->scaled_frame_width =
3068 (oxcf->width * cpi->resize_scale_num) / cpi->resize_scale_den;
3069 oxcf->scaled_frame_height =
3070 (oxcf->height * cpi->resize_scale_num) / cpi->resize_scale_den;
3071 // There has been a change in frame size.
3072 vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3073 oxcf->scaled_frame_height);
3075 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3076 set_mv_search_params(cpi);
3078 vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
3079 #if CONFIG_VP9_TEMPORAL_DENOISING
3080 // Reset the denoiser on the resized frame.
3081 if (cpi->oxcf.noise_sensitivity > 0) {
3082 vp9_denoiser_free(&(cpi->denoiser));
3083 setup_denoiser_buffer(cpi);
3084 // Dynamic resize is only triggered for non-SVC, so we can force
3085 // golden frame update here as temporary fix to denoiser.
3086 cpi->refresh_golden_frame = 1;
3091 if ((oxcf->pass == 2) &&
3092 (!cpi->use_svc || (is_two_pass_svc(cpi) &&
3093 cpi->svc.encode_empty_frame_state != ENCODING))) {
3094 vp9_set_target_rate(cpi);
3097 alloc_frame_mvs(cm, cm->new_fb_idx);
3099 // Reset the frame pointers to the current frame size.
3100 if (vpx_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
3101 cm->subsampling_x, cm->subsampling_y,
3102 #if CONFIG_VP9_HIGHBITDEPTH
3103 cm->use_highbitdepth,
3105 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
3107 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3108 "Failed to allocate frame buffer");
3110 alloc_util_frame_buffers(cpi);
3111 init_motion_estimation(cpi);
3113 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3114 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
3115 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3117 ref_buf->idx = buf_idx;
3119 if (buf_idx != INVALID_IDX) {
3120 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3122 #if CONFIG_VP9_HIGHBITDEPTH
3123 vp9_setup_scale_factors_for_frame(
3124 &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
3125 cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
3127 vp9_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3128 buf->y_crop_height, cm->width,
3130 #endif // CONFIG_VP9_HIGHBITDEPTH
3131 if (vp9_is_scaled(&ref_buf->sf)) vpx_extend_frame_borders(buf);
3133 ref_buf->buf = NULL;
3137 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3140 static void encode_without_recode_loop(VP9_COMP *cpi, size_t *size,
3142 VP9_COMMON *const cm = &cpi->common;
3143 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
3145 vpx_clear_system_state();
3147 set_frame_size(cpi);
3149 if (is_one_pass_cbr_svc(cpi) &&
3150 cpi->un_scaled_source->y_width == cm->width << 2 &&
3151 cpi->un_scaled_source->y_height == cm->height << 2 &&
3152 cpi->svc.scaled_temp.y_width == cm->width << 1 &&
3153 cpi->svc.scaled_temp.y_height == cm->height << 1) {
3154 // For svc, if it is a 1/4x1/4 downscaling, do a two-stage scaling to take
3155 // advantage of the 1:2 optimized scaler. In the process, the 1/2x1/2
3156 // result will be saved in scaled_temp and might be used later.
3157 cpi->Source = vp9_svc_twostage_scale(
3158 cm, cpi->un_scaled_source, &cpi->scaled_source, &cpi->svc.scaled_temp);
3159 cpi->svc.scaled_one_half = 1;
3160 } else if (is_one_pass_cbr_svc(cpi) &&
3161 cpi->un_scaled_source->y_width == cm->width << 1 &&
3162 cpi->un_scaled_source->y_height == cm->height << 1 &&
3163 cpi->svc.scaled_one_half) {
3164 // If the spatial layer is 1/2x1/2 and the scaling is already done in the
3165 // two-stage scaling, use the result directly.
3166 cpi->Source = &cpi->svc.scaled_temp;
3167 cpi->svc.scaled_one_half = 0;
3169 cpi->Source = vp9_scale_if_required(
3170 cm, cpi->un_scaled_source, &cpi->scaled_source, (cpi->oxcf.pass == 0));
3172 // Unfiltered raw source used in metrics calculation if the source
3173 // has been filtered.
3174 if (is_psnr_calc_enabled(cpi)) {
3175 #ifdef ENABLE_KF_DENOISE
3176 if (is_spatial_denoise_enabled(cpi)) {
3177 cpi->raw_source_frame =
3178 vp9_scale_if_required(cm, &cpi->raw_unscaled_source,
3179 &cpi->raw_scaled_source, (cpi->oxcf.pass == 0));
3181 cpi->raw_source_frame = cpi->Source;
3184 cpi->raw_source_frame = cpi->Source;
3188 // Avoid scaling last_source unless its needed.
3189 // Last source is needed if vp9_avg_source_sad() is used, or if
3190 // partition_search_type == SOURCE_VAR_BASED_PARTITION, or if noise
3191 // estimation is enabled.
3192 if (cpi->unscaled_last_source != NULL &&
3193 (cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
3194 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_VBR &&
3195 cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5) ||
3196 cpi->sf.partition_search_type == SOURCE_VAR_BASED_PARTITION ||
3197 cpi->noise_estimate.enabled || cpi->sf.use_source_sad))
3199 vp9_scale_if_required(cm, cpi->unscaled_last_source,
3200 &cpi->scaled_last_source, (cpi->oxcf.pass == 0));
3202 if (cm->frame_type == KEY_FRAME || cpi->resize_pending != 0) {
3203 memset(cpi->consec_zero_mv, 0,
3204 cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
3207 vp9_update_noise_estimate(cpi);
3209 if (cpi->oxcf.pass == 0 && cpi->oxcf.mode == REALTIME &&
3210 cpi->oxcf.speed >= 5 && cpi->resize_state == 0 &&
3211 (cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
3212 cpi->oxcf.rc_mode == VPX_VBR || cpi->sf.use_source_sad) &&
3214 vp9_avg_source_sad(cpi);
3216 // For 1 pass SVC, since only ZEROMV is allowed for upsampled reference
3217 // frame (i.e, svc->force_zero_mode_spatial_ref = 0), we can avoid this
3218 // frame-level upsampling.
3219 if (frame_is_intra_only(cm) == 0 && !is_one_pass_cbr_svc(cpi)) {
3220 vp9_scale_references(cpi);
3223 set_size_independent_vars(cpi);
3224 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3226 if (cpi->oxcf.speed >= 5 && cpi->oxcf.pass == 0 &&
3227 cpi->oxcf.rc_mode == VPX_CBR &&
3228 cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
3229 cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3230 cpi->use_skin_detection = 1;
3233 vp9_set_quantizer(cm, q);
3234 vp9_set_variance_partition_thresholds(cpi, q);
3238 suppress_active_map(cpi);
3240 // Variance adaptive and in frame q adjustment experiments are mutually
3242 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3243 vp9_vaq_frame_setup(cpi);
3244 } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
3245 vp9_360aq_frame_setup(cpi);
3246 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
3247 vp9_setup_in_frame_q_adj(cpi);
3248 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3249 vp9_cyclic_refresh_setup(cpi);
3250 } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
3251 // it may be pretty bad for rate-control,
3252 // and I should handle it somehow
3253 vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
3256 apply_active_map(cpi);
3258 vp9_encode_frame(cpi);
3260 // Check if we should drop this frame because of high overshoot.
3261 // Only for frames where high temporal-source SAD is detected.
3262 if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
3263 cpi->resize_state == 0 && cm->frame_type != KEY_FRAME &&
3264 cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
3265 cpi->rc.high_source_sad == 1) {
3267 // Get an estimate of the encoded frame size.
3268 save_coding_context(cpi);
3269 vp9_pack_bitstream(cpi, dest, size);
3270 restore_coding_context(cpi);
3271 frame_size = (int)(*size) << 3;
3272 // Check if encoded frame will overshoot too much, and if so, set the q and
3273 // adjust some rate control parameters, and return to re-encode the frame.
3274 if (vp9_encodedframe_overshoot(cpi, frame_size, &q)) {
3275 vpx_clear_system_state();
3276 vp9_set_quantizer(cm, q);
3277 vp9_set_variance_partition_thresholds(cpi, q);
3278 suppress_active_map(cpi);
3279 // Turn-off cyclic refresh for re-encoded frame.
3280 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3281 unsigned char *const seg_map = cpi->segmentation_map;
3282 memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
3283 vp9_disable_segmentation(&cm->seg);
3285 apply_active_map(cpi);
3286 vp9_encode_frame(cpi);
3290 // Update some stats from cyclic refresh, and check if we should not update
3291 // golden reference, for non-SVC 1 pass CBR.
3292 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
3293 !cpi->use_svc && cpi->ext_refresh_frame_flags_pending == 0 &&
3294 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
3295 !cpi->oxcf.gf_cbr_boost_pct))
3296 vp9_cyclic_refresh_check_golden_update(cpi);
3298 // Update the skip mb flag probabilities based on the distribution
3299 // seen in the last encoder iteration.
3300 // update_base_skip_probs(cpi);
3301 vpx_clear_system_state();
3304 #define MAX_QSTEP_ADJ 4
3305 static int get_qstep_adj(int rate_excess, int rate_limit) {
3307 rate_limit ? ((rate_excess + rate_limit / 2) / rate_limit) : INT_MAX;
3308 return VPXMIN(qstep, MAX_QSTEP_ADJ);
3311 static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
3313 VP9_COMMON *const cm = &cpi->common;
3314 RATE_CONTROL *const rc = &cpi->rc;
3315 int bottom_index, top_index;
3317 int loop_at_this_size = 0;
3319 int overshoot_seen = 0;
3320 int undershoot_seen = 0;
3321 int frame_over_shoot_limit;
3322 int frame_under_shoot_limit;
3323 int q = 0, q_low = 0, q_high = 0;
3326 set_size_independent_vars(cpi);
3328 enable_acl = cpi->sf.allow_acl
3329 ? (cm->frame_type == KEY_FRAME) || (cm->show_frame == 0)
3333 vpx_clear_system_state();
3335 set_frame_size(cpi);
3337 if (loop_count == 0 || cpi->resize_pending != 0) {
3338 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3340 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3341 set_mv_search_params(cpi);
3343 // Reset the loop state for new frame size.
3345 undershoot_seen = 0;
3347 // Reconfiguration for change in frame size has concluded.
3348 cpi->resize_pending = 0;
3350 q_low = bottom_index;
3353 loop_at_this_size = 0;
3356 // Decide frame size bounds first time through.
3357 if (loop_count == 0) {
3358 vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
3359 &frame_under_shoot_limit,
3360 &frame_over_shoot_limit);
3363 cpi->Source = vp9_scale_if_required(
3364 cm, cpi->un_scaled_source, &cpi->scaled_source, (cpi->oxcf.pass == 0));
3366 // Unfiltered raw source used in metrics calculation if the source
3367 // has been filtered.
3368 if (is_psnr_calc_enabled(cpi)) {
3369 #ifdef ENABLE_KF_DENOISE
3370 if (is_spatial_denoise_enabled(cpi)) {
3371 cpi->raw_source_frame = vp9_scale_if_required(
3372 cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
3373 (cpi->oxcf.pass == 0));
3375 cpi->raw_source_frame = cpi->Source;
3378 cpi->raw_source_frame = cpi->Source;
3382 if (cpi->unscaled_last_source != NULL)
3383 cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
3384 &cpi->scaled_last_source,
3385 (cpi->oxcf.pass == 0));
3387 if (frame_is_intra_only(cm) == 0) {
3388 if (loop_count > 0) {
3389 release_scaled_references(cpi);
3391 vp9_scale_references(cpi);
3394 vp9_set_quantizer(cm, q);
3396 if (loop_count == 0) setup_frame(cpi);
3398 // Variance adaptive and in frame q adjustment experiments are mutually
3400 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3401 vp9_vaq_frame_setup(cpi);
3402 } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
3403 vp9_360aq_frame_setup(cpi);
3404 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
3405 vp9_setup_in_frame_q_adj(cpi);
3406 } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
3407 vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
3410 vp9_encode_frame(cpi);
3412 // Update the skip mb flag probabilities based on the distribution
3413 // seen in the last encoder iteration.
3414 // update_base_skip_probs(cpi);
3416 vpx_clear_system_state();
3418 // Dummy pack of the bitstream using up to date stats to get an
3419 // accurate estimate of output frame size to determine if we need
3421 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
3422 save_coding_context(cpi);
3423 if (!cpi->sf.use_nonrd_pick_mode) vp9_pack_bitstream(cpi, dest, size);
3425 rc->projected_frame_size = (int)(*size) << 3;
3427 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
3430 if (cpi->oxcf.rc_mode == VPX_Q) {
3433 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
3434 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
3438 int64_t high_err_target = cpi->ambient_err;
3439 int64_t low_err_target = cpi->ambient_err >> 1;
3441 #if CONFIG_VP9_HIGHBITDEPTH
3442 if (cm->use_highbitdepth) {
3443 kf_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3445 kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3448 kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3449 #endif // CONFIG_VP9_HIGHBITDEPTH
3451 // Prevent possible divide by zero error below for perfect KF
3454 // The key frame is not good enough or we can afford
3455 // to make it better without undue risk of popping.
3456 if ((kf_err > high_err_target &&
3457 rc->projected_frame_size <= frame_over_shoot_limit) ||
3458 (kf_err > low_err_target &&
3459 rc->projected_frame_size <= frame_under_shoot_limit)) {
3461 q_high = q > q_low ? q - 1 : q_low;
3464 q = (int)((q * high_err_target) / kf_err);
3465 q = VPXMIN(q, (q_high + q_low) >> 1);
3466 } else if (kf_err < low_err_target &&
3467 rc->projected_frame_size >= frame_under_shoot_limit) {
3468 // The key frame is much better than the previous frame
3470 q_low = q < q_high ? q + 1 : q_high;
3473 q = (int)((q * low_err_target) / kf_err);
3474 q = VPXMIN(q, (q_high + q_low + 1) >> 1);
3477 // Clamp Q to upper and lower limits:
3478 q = clamp(q, q_low, q_high);
3481 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
3482 frame_under_shoot_limit, q,
3483 VPXMAX(q_high, top_index), bottom_index)) {
3484 // Is the projected frame size out of range and are we allowed
3485 // to attempt to recode.
3490 if (cpi->resize_pending == 1) {
3491 // Change in frame size so go back around the recode loop.
3492 cpi->rc.frame_size_selector =
3493 SCALE_STEP1 - cpi->rc.frame_size_selector;
3494 cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
3496 #if CONFIG_INTERNAL_STATS
3497 ++cpi->tot_recode_hits;
3504 // Frame size out of permitted range:
3505 // Update correction factor & compute new Q to try...
3507 // Frame is too large
3508 if (rc->projected_frame_size > rc->this_frame_target) {
3509 // Special case if the projected size is > the max allowed.
3510 if (rc->projected_frame_size >= rc->max_frame_bandwidth) {
3512 q_val_high = vp9_convert_qindex_to_q(q_high, cm->bit_depth);
3513 q_val_high = q_val_high * ((double)rc->projected_frame_size /
3514 rc->max_frame_bandwidth);
3515 q_high = vp9_convert_q_to_qindex(q_val_high, cm->bit_depth);
3516 q_high = clamp(q_high, rc->best_quality, rc->worst_quality);
3519 // Raise Qlow as to at least the current value
3521 get_qstep_adj(rc->projected_frame_size, rc->this_frame_target);
3522 q_low = VPXMIN(q + qstep, q_high);
3523 // q_low = q < q_high ? q + 1 : q_high;
3525 if (undershoot_seen || loop_at_this_size > 1) {
3526 // Update rate_correction_factor unless
3527 vp9_rc_update_rate_correction_factors(cpi);
3529 q = (q_high + q_low + 1) / 2;
3531 // Update rate_correction_factor unless
3532 vp9_rc_update_rate_correction_factors(cpi);
3534 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3535 VPXMAX(q_high, top_index));
3537 while (q < q_low && retries < 10) {
3538 vp9_rc_update_rate_correction_factors(cpi);
3539 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3540 VPXMAX(q_high, top_index));
3547 // Frame is too small
3549 get_qstep_adj(rc->this_frame_target, rc->projected_frame_size);
3550 q_high = VPXMAX(q - qstep, q_low);
3551 // q_high = q > q_low ? q - 1 : q_low;
3553 if (overshoot_seen || loop_at_this_size > 1) {
3554 vp9_rc_update_rate_correction_factors(cpi);
3555 q = (q_high + q_low) / 2;
3557 vp9_rc_update_rate_correction_factors(cpi);
3558 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3560 // Special case reset for qlow for constrained quality.
3561 // This should only trigger where there is very substantial
3562 // undershoot on a frame and the auto cq level is above
3563 // the user passsed in value.
3564 if (cpi->oxcf.rc_mode == VPX_CQ && q < q_low) {
3568 while (q > q_high && retries < 10) {
3569 vp9_rc_update_rate_correction_factors(cpi);
3570 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
3576 undershoot_seen = 1;
3579 // Clamp Q to upper and lower limits:
3580 q = clamp(q, q_low, q_high);
3582 loop = (q != last_q);
3588 // Special case for overlay frame.
3589 if (rc->is_src_frame_alt_ref &&
3590 rc->projected_frame_size < rc->max_frame_bandwidth)
3595 ++loop_at_this_size;
3597 #if CONFIG_INTERNAL_STATS
3598 ++cpi->tot_recode_hits;
3602 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF)
3603 if (loop || !enable_acl) restore_coding_context(cpi);
3607 vp9_encode_frame(cpi);
3608 vpx_clear_system_state();
3609 restore_coding_context(cpi);
3610 vp9_pack_bitstream(cpi, dest, size);
3612 vp9_encode_frame(cpi);
3613 vpx_clear_system_state();
3615 restore_coding_context(cpi);
3619 static int get_ref_frame_flags(const VP9_COMP *cpi) {
3620 const int *const map = cpi->common.ref_frame_map;
3621 const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
3622 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
3623 const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
3624 int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3626 if (gold_is_last) flags &= ~VP9_GOLD_FLAG;
3628 if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
3629 (cpi->svc.number_temporal_layers == 1 &&
3630 cpi->svc.number_spatial_layers == 1))
3631 flags &= ~VP9_GOLD_FLAG;
3633 if (alt_is_last) flags &= ~VP9_ALT_FLAG;
3635 if (gold_is_alt) flags &= ~VP9_ALT_FLAG;
3640 static void set_ext_overrides(VP9_COMP *cpi) {
3641 // Overrides the defaults with the externally supplied values with
3642 // vp9_update_reference() and vp9_update_entropy() calls
3643 // Note: The overrides are valid only for the next frame passed
3644 // to encode_frame_to_data_rate() function
3645 if (cpi->ext_refresh_frame_context_pending) {
3646 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
3647 cpi->ext_refresh_frame_context_pending = 0;
3649 if (cpi->ext_refresh_frame_flags_pending) {
3650 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
3651 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
3652 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
3656 YV12_BUFFER_CONFIG *vp9_svc_twostage_scale(VP9_COMMON *cm,
3657 YV12_BUFFER_CONFIG *unscaled,
3658 YV12_BUFFER_CONFIG *scaled,
3659 YV12_BUFFER_CONFIG *scaled_temp) {
3660 if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
3661 cm->mi_rows * MI_SIZE != unscaled->y_height) {
3662 #if CONFIG_VP9_HIGHBITDEPTH
3663 scale_and_extend_frame(unscaled, scaled_temp, (int)cm->bit_depth);
3664 scale_and_extend_frame(scaled_temp, scaled, (int)cm->bit_depth);
3666 vp9_scale_and_extend_frame(unscaled, scaled_temp);
3667 vp9_scale_and_extend_frame(scaled_temp, scaled);
3668 #endif // CONFIG_VP9_HIGHBITDEPTH
3675 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
3676 YV12_BUFFER_CONFIG *unscaled,
3677 YV12_BUFFER_CONFIG *scaled,
3678 int use_normative_scaler) {
3679 if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
3680 cm->mi_rows * MI_SIZE != unscaled->y_height) {
3681 #if CONFIG_VP9_HIGHBITDEPTH
3682 if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
3683 unscaled->y_height <= (scaled->y_height << 1))
3684 scale_and_extend_frame(unscaled, scaled, (int)cm->bit_depth);
3686 scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
3688 if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
3689 unscaled->y_height <= (scaled->y_height << 1))
3690 vp9_scale_and_extend_frame(unscaled, scaled);
3692 scale_and_extend_frame_nonnormative(unscaled, scaled);
3693 #endif // CONFIG_VP9_HIGHBITDEPTH
3700 static void set_arf_sign_bias(VP9_COMP *cpi) {
3701 VP9_COMMON *const cm = &cpi->common;
3704 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3705 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3706 arf_sign_bias = cpi->rc.source_alt_ref_active &&
3707 (!cpi->refresh_alt_ref_frame ||
3708 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
3711 (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
3713 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
3716 static int setup_interp_filter_search_mask(VP9_COMP *cpi) {
3717 INTERP_FILTER ifilter;
3718 int ref_total[MAX_REF_FRAMES] = { 0 };
3719 MV_REFERENCE_FRAME ref;
3721 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
3723 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
3724 for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
3725 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
3727 for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
3728 if ((ref_total[LAST_FRAME] &&
3729 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
3730 (ref_total[GOLDEN_FRAME] == 0 ||
3731 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
3732 ref_total[GOLDEN_FRAME]) &&
3733 (ref_total[ALTREF_FRAME] == 0 ||
3734 cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50 <
3735 ref_total[ALTREF_FRAME]))
3736 mask |= 1 << ifilter;
3741 #ifdef ENABLE_KF_DENOISE
3742 // Baseline Kernal weights for denoise
3743 static uint8_t dn_kernal_3[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
3744 static uint8_t dn_kernal_5[25] = { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 4,
3745 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1 };
3747 static INLINE void add_denoise_point(int centre_val, int data_val, int thresh,
3748 uint8_t point_weight, int *sum_val,
3750 if (abs(centre_val - data_val) <= thresh) {
3751 *sum_weight += point_weight;
3752 *sum_val += (int)data_val * (int)point_weight;
3756 static void spatial_denoise_point(uint8_t *src_ptr, const int stride,
3757 const int strength) {
3760 int thresh = strength;
3761 int kernal_size = 5;
3762 int half_k_size = 2;
3766 uint8_t *kernal_ptr;
3768 // Find the maximum deviation from the source point in the locale.
3769 tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
3770 for (i = 0; i < kernal_size + 2; ++i) {
3771 for (j = 0; j < kernal_size + 2; ++j) {
3772 max_diff = VPXMAX(max_diff, abs((int)*src_ptr - (int)tmp_ptr[j]));
3777 // Select the kernal size.
3778 if (max_diff > (strength + (strength >> 1))) {
3781 thresh = thresh >> 1;
3783 kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
3786 tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
3787 for (i = 0; i < kernal_size; ++i) {
3788 for (j = 0; j < kernal_size; ++j) {
3789 add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
3790 &sum_val, &sum_weight);
3796 // Update the source value with the new filtered value
3797 *src_ptr = (uint8_t)((sum_val + (sum_weight >> 1)) / sum_weight);
3800 #if CONFIG_VP9_HIGHBITDEPTH
3801 static void highbd_spatial_denoise_point(uint16_t *src_ptr, const int stride,
3802 const int strength) {
3805 int thresh = strength;
3806 int kernal_size = 5;
3807 int half_k_size = 2;
3811 uint8_t *kernal_ptr;
3813 // Find the maximum deviation from the source point in the locale.
3814 tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
3815 for (i = 0; i < kernal_size + 2; ++i) {
3816 for (j = 0; j < kernal_size + 2; ++j) {
3817 max_diff = VPXMAX(max_diff, abs((int)src_ptr - (int)tmp_ptr[j]));
3822 // Select the kernal size.
3823 if (max_diff > (strength + (strength >> 1))) {
3826 thresh = thresh >> 1;
3828 kernal_ptr = (kernal_size == 3) ? dn_kernal_3 : dn_kernal_5;
3831 tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
3832 for (i = 0; i < kernal_size; ++i) {
3833 for (j = 0; j < kernal_size; ++j) {
3834 add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernal_ptr,
3835 &sum_val, &sum_weight);
3841 // Update the source value with the new filtered value
3842 *src_ptr = (uint16_t)((sum_val + (sum_weight >> 1)) / sum_weight);
3844 #endif // CONFIG_VP9_HIGHBITDEPTH
3846 // Apply thresholded spatial noise supression to a given buffer.
3847 static void spatial_denoise_buffer(VP9_COMP *cpi, uint8_t *buffer,
3848 const int stride, const int width,
3849 const int height, const int strength) {
3850 VP9_COMMON *const cm = &cpi->common;
3851 uint8_t *src_ptr = buffer;
3855 for (row = 0; row < height; ++row) {
3856 for (col = 0; col < width; ++col) {
3857 #if CONFIG_VP9_HIGHBITDEPTH
3858 if (cm->use_highbitdepth)
3859 highbd_spatial_denoise_point(CONVERT_TO_SHORTPTR(&src_ptr[col]), stride,
3862 spatial_denoise_point(&src_ptr[col], stride, strength);
3864 spatial_denoise_point(&src_ptr[col], stride, strength);
3865 #endif // CONFIG_VP9_HIGHBITDEPTH
3871 // Apply thresholded spatial noise supression to source.
3872 static void spatial_denoise_frame(VP9_COMP *cpi) {
3873 YV12_BUFFER_CONFIG *src = cpi->Source;
3874 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3875 TWO_PASS *const twopass = &cpi->twopass;
3876 VP9_COMMON *const cm = &cpi->common;
3878 // Base the filter strength on the current active max Q.
3879 const int q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
3882 VPXMAX(oxcf->arnr_strength >> 2, VPXMIN(oxcf->arnr_strength, (q >> 4)));
3884 // Denoise each of Y,U and V buffers.
3885 spatial_denoise_buffer(cpi, src->y_buffer, src->y_stride, src->y_width,
3886 src->y_height, strength);
3888 strength += (strength >> 1);
3889 spatial_denoise_buffer(cpi, src->u_buffer, src->uv_stride, src->uv_width,
3890 src->uv_height, strength << 1);
3892 spatial_denoise_buffer(cpi, src->v_buffer, src->uv_stride, src->uv_width,
3893 src->uv_height, strength << 1);
3895 #endif // ENABLE_KF_DENOISE
3897 static void vp9_try_disable_lookahead_aq(VP9_COMP *cpi, size_t *size,
3899 if (cpi->common.seg.enabled)
3900 if (ALT_REF_AQ_PROTECT_GAIN) {
3901 size_t nsize = *size;
3904 // TODO(yuryg): optimize this, as
3905 // we don't really need to repack
3907 save_coding_context(cpi);
3908 vp9_disable_segmentation(&cpi->common.seg);
3909 vp9_pack_bitstream(cpi, dest, &nsize);
3910 restore_coding_context(cpi);
3912 overhead = (int)*size - (int)nsize;
3914 if (vp9_alt_ref_aq_disable_if(cpi->alt_ref_aq, overhead, (int)*size))
3915 vp9_encode_frame(cpi);
3917 vp9_enable_segmentation(&cpi->common.seg);
3921 static void encode_frame_to_data_rate(VP9_COMP *cpi, size_t *size,
3923 unsigned int *frame_flags) {
3924 VP9_COMMON *const cm = &cpi->common;
3925 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3926 struct segmentation *const seg = &cm->seg;
3929 set_ext_overrides(cpi);
3930 vpx_clear_system_state();
3932 #ifdef ENABLE_KF_DENOISE
3933 // Spatial denoise of key frame.
3934 if (is_spatial_denoise_enabled(cpi)) spatial_denoise_frame(cpi);
3937 // Set the arf sign bias for this frame.
3938 set_arf_sign_bias(cpi);
3940 // Set default state for segment based loop filter update flags.
3941 cm->lf.mode_ref_delta_update = 0;
3943 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
3944 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
3946 // Set various flags etc to special state if it is a key frame.
3947 if (frame_is_intra_only(cm)) {
3948 // Reset the loop filter deltas and segmentation map.
3949 vp9_reset_segment_features(&cm->seg);
3951 // If segmentation is enabled force a map update for key frames.
3953 seg->update_map = 1;
3954 seg->update_data = 1;
3957 // The alternate reference frame cannot be active for a key frame.
3958 cpi->rc.source_alt_ref_active = 0;
3960 cm->error_resilient_mode = oxcf->error_resilient_mode;
3961 cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
3963 // By default, encoder assumes decoder can use prev_mi.
3964 if (cm->error_resilient_mode) {
3965 cm->frame_parallel_decoding_mode = 1;
3966 cm->reset_frame_context = 0;
3967 cm->refresh_frame_context = 0;
3968 } else if (cm->intra_only) {
3969 // Only reset the current context.
3970 cm->reset_frame_context = 2;
3973 if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
3974 // Use context 0 for intra only empty frame, but the last frame context
3975 // for other empty frames.
3976 if (cpi->svc.encode_empty_frame_state == ENCODING) {
3977 if (cpi->svc.encode_intra_empty_frame != 0)
3978 cm->frame_context_idx = 0;
3980 cm->frame_context_idx = FRAME_CONTEXTS - 1;
3982 cm->frame_context_idx =
3983 cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
3984 cpi->svc.temporal_layer_id;
3987 cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
3989 // The probs will be updated based on the frame type of its previous
3990 // frame if frame_parallel_decoding_mode is 0. The type may vary for
3991 // the frame after a key frame in base layer since we may drop enhancement
3992 // layers. So set frame_parallel_decoding_mode to 1 in this case.
3993 if (cm->frame_parallel_decoding_mode == 0) {
3994 if (cpi->svc.number_temporal_layers == 1) {
3995 if (cpi->svc.spatial_layer_id == 0 &&
3996 cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
3997 cm->frame_parallel_decoding_mode = 1;
3998 } else if (cpi->svc.spatial_layer_id == 0) {
3999 // Find the 2nd frame in temporal base layer and 1st frame in temporal
4000 // enhancement layers from the key frame.
4002 for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
4003 if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
4004 cm->frame_parallel_decoding_mode = 1;
4012 // For 1 pass CBR, check if we are dropping this frame.
4013 // For spatial layers, for now only check for frame-dropping on first spatial
4014 // layer, and if decision is to drop, we drop whole super-frame.
4015 if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR &&
4016 cm->frame_type != KEY_FRAME) {
4017 if (vp9_rc_drop_frame(cpi) ||
4018 (is_one_pass_cbr_svc(cpi) && cpi->svc.rc_drop_superframe == 1)) {
4019 vp9_rc_postencode_update_drop_frame(cpi);
4020 ++cm->current_video_frame;
4021 cpi->ext_refresh_frame_flags_pending = 0;
4022 cpi->svc.rc_drop_superframe = 1;
4023 // TODO(marpan): Advancing the svc counters on dropped frames can break
4024 // the referencing scheme for the fixed svc patterns defined in
4025 // vp9_one_pass_cbr_svc_start_layer(). Look into fixing this issue, but
4026 // for now, don't advance the svc frame counters on dropped frame.
4027 // if (cpi->use_svc)
4028 // vp9_inc_frame_in_layer(cpi);
4033 vpx_clear_system_state();
4035 #if CONFIG_INTERNAL_STATS
4036 memset(cpi->mode_chosen_counts, 0,
4037 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4040 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
4041 encode_without_recode_loop(cpi, size, dest);
4043 encode_with_recode_loop(cpi, size, dest);
4046 // Disable segmentation if it decrease rate/distortion ratio
4047 if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
4048 vp9_try_disable_lookahead_aq(cpi, size, dest);
4050 #if CONFIG_VP9_TEMPORAL_DENOISING
4051 #ifdef OUTPUT_YUV_DENOISED
4052 if (oxcf->noise_sensitivity > 0) {
4053 vp9_write_yuv_frame_420(&cpi->denoiser.running_avg_y[INTRA_FRAME],
4058 #ifdef OUTPUT_YUV_SKINMAP
4059 if (cpi->common.current_video_frame > 1) {
4060 vp9_compute_skin_map(cpi, yuv_skinmap_file);
4064 // Special case code to reduce pulsing when key frames are forced at a
4065 // fixed interval. Note the reconstruction error if it is the frame before
4066 // the force key frame
4067 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
4068 #if CONFIG_VP9_HIGHBITDEPTH
4069 if (cm->use_highbitdepth) {
4071 vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4073 cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4076 cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4077 #endif // CONFIG_VP9_HIGHBITDEPTH
4080 // If the encoder forced a KEY_FRAME decision
4081 if (cm->frame_type == KEY_FRAME) cpi->refresh_last_frame = 1;
4083 cm->frame_to_show = get_frame_new_buffer(cm);
4084 cm->frame_to_show->color_space = cm->color_space;
4085 cm->frame_to_show->color_range = cm->color_range;
4086 cm->frame_to_show->render_width = cm->render_width;
4087 cm->frame_to_show->render_height = cm->render_height;
4089 // Pick the loop filter level for the frame.
4090 loopfilter_frame(cpi, cm);
4092 // build the bitstream
4093 vp9_pack_bitstream(cpi, dest, size);
4095 if (cm->seg.update_map) update_reference_segmentation_map(cpi);
4097 if (frame_is_intra_only(cm) == 0) {
4098 release_scaled_references(cpi);
4100 vp9_update_reference_frames(cpi);
4102 for (t = TX_4X4; t <= TX_32X32; t++)
4103 full_to_model_counts(cpi->td.counts->coef[t],
4104 cpi->td.rd_counts.coef_counts[t]);
4106 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
4107 vp9_adapt_coef_probs(cm);
4109 if (!frame_is_intra_only(cm)) {
4110 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
4111 vp9_adapt_mode_probs(cm);
4112 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
4116 cpi->ext_refresh_frame_flags_pending = 0;
4118 if (cpi->refresh_golden_frame == 1)
4119 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
4121 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4123 if (cpi->refresh_alt_ref_frame == 1)
4124 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
4126 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4128 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4130 cm->last_frame_type = cm->frame_type;
4132 if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
4133 vp9_rc_postencode_update(cpi, *size);
4136 output_frame_level_debug_stats(cpi);
4139 if (cm->frame_type == KEY_FRAME) {
4140 // Tell the caller that the frame was coded as a key frame
4141 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
4143 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4146 // Clear the one shot update flags for segmentation map and mode/ref loop
4148 cm->seg.update_map = 0;
4149 cm->seg.update_data = 0;
4150 cm->lf.mode_ref_delta_update = 0;
4152 // keep track of the last coded dimensions
4153 cm->last_width = cm->width;
4154 cm->last_height = cm->height;
4156 // reset to normal state now that we are done.
4157 if (!cm->show_existing_frame) cm->last_show_frame = cm->show_frame;
4159 if (cm->show_frame) {
4160 vp9_swap_mi_and_prev_mi(cm);
4161 // Don't increment frame counters if this was an altref buffer
4162 // update not a real frame
4163 ++cm->current_video_frame;
4164 if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
4166 cm->prev_frame = cm->cur_frame;
4170 .layer_context[cpi->svc.spatial_layer_id *
4171 cpi->svc.number_temporal_layers +
4172 cpi->svc.temporal_layer_id]
4173 .last_frame_type = cm->frame_type;
4175 cpi->force_update_segmentation = 0;
4177 if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
4178 vp9_alt_ref_aq_unset_all(cpi->alt_ref_aq, cpi);
4181 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4182 unsigned int *frame_flags) {
4183 vp9_rc_get_svc_params(cpi);
4184 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4187 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4188 unsigned int *frame_flags) {
4189 if (cpi->oxcf.rc_mode == VPX_CBR) {
4190 vp9_rc_get_one_pass_cbr_params(cpi);
4192 vp9_rc_get_one_pass_vbr_params(cpi);
4194 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4197 static void Pass2Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4198 unsigned int *frame_flags) {
4199 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
4200 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4202 if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
4203 vp9_twopass_postencode_update(cpi);
4206 static void init_ref_frame_bufs(VP9_COMMON *cm) {
4208 BufferPool *const pool = cm->buffer_pool;
4209 cm->new_fb_idx = INVALID_IDX;
4210 for (i = 0; i < REF_FRAMES; ++i) {
4211 cm->ref_frame_map[i] = INVALID_IDX;
4212 pool->frame_bufs[i].ref_count = 0;
4216 static void check_initial_width(VP9_COMP *cpi,
4217 #if CONFIG_VP9_HIGHBITDEPTH
4218 int use_highbitdepth,
4220 int subsampling_x, int subsampling_y) {
4221 VP9_COMMON *const cm = &cpi->common;
4223 if (!cpi->initial_width ||
4224 #if CONFIG_VP9_HIGHBITDEPTH
4225 cm->use_highbitdepth != use_highbitdepth ||
4227 cm->subsampling_x != subsampling_x ||
4228 cm->subsampling_y != subsampling_y) {
4229 cm->subsampling_x = subsampling_x;
4230 cm->subsampling_y = subsampling_y;
4231 #if CONFIG_VP9_HIGHBITDEPTH
4232 cm->use_highbitdepth = use_highbitdepth;
4235 alloc_raw_frame_buffers(cpi);
4236 init_ref_frame_bufs(cm);
4237 alloc_util_frame_buffers(cpi);
4239 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4241 cpi->initial_width = cm->width;
4242 cpi->initial_height = cm->height;
4243 cpi->initial_mbs = cm->MBs;
4247 int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
4248 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
4250 VP9_COMMON *const cm = &cpi->common;
4251 struct vpx_usec_timer timer;
4253 const int subsampling_x = sd->subsampling_x;
4254 const int subsampling_y = sd->subsampling_y;
4255 #if CONFIG_VP9_HIGHBITDEPTH
4256 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
4259 #if CONFIG_VP9_HIGHBITDEPTH
4260 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
4262 check_initial_width(cpi, subsampling_x, subsampling_y);
4263 #endif // CONFIG_VP9_HIGHBITDEPTH
4265 #if CONFIG_VP9_TEMPORAL_DENOISING
4266 setup_denoiser_buffer(cpi);
4268 vpx_usec_timer_start(&timer);
4270 if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4271 #if CONFIG_VP9_HIGHBITDEPTH
4273 #endif // CONFIG_VP9_HIGHBITDEPTH
4276 vpx_usec_timer_mark(&timer);
4277 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4279 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
4280 (subsampling_x != 1 || subsampling_y != 1)) {
4281 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
4282 "Non-4:2:0 color format requires profile 1 or 3");
4285 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
4286 (subsampling_x == 1 && subsampling_y == 1)) {
4287 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
4288 "4:2:0 color format requires profile 0 or 2");
4295 static int frame_is_reference(const VP9_COMP *cpi) {
4296 const VP9_COMMON *cm = &cpi->common;
4298 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
4299 cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame ||
4300 cm->refresh_frame_context || cm->lf.mode_ref_delta_update ||
4301 cm->seg.update_map || cm->seg.update_data;
4304 static void adjust_frame_rate(VP9_COMP *cpi,
4305 const struct lookahead_entry *source) {
4306 int64_t this_duration;
4309 if (source->ts_start == cpi->first_time_stamp_ever) {
4310 this_duration = source->ts_end - source->ts_start;
4313 int64_t last_duration =
4314 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
4316 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
4318 // do a step update if the duration changes by 10%
4320 step = (int)((this_duration - last_duration) * 10 / last_duration);
4323 if (this_duration) {
4325 vp9_new_framerate(cpi, 10000000.0 / this_duration);
4327 // Average this frame's rate into the last second's average
4328 // frame rate. If we haven't seen 1 second yet, then average
4329 // over the whole interval seen.
4330 const double interval = VPXMIN(
4331 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
4332 double avg_duration = 10000000.0 / cpi->framerate;
4333 avg_duration *= (interval - avg_duration + this_duration);
4334 avg_duration /= interval;
4336 vp9_new_framerate(cpi, 10000000.0 / avg_duration);
4339 cpi->last_time_stamp_seen = source->ts_start;
4340 cpi->last_end_time_stamp_seen = source->ts_end;
4343 // Returns 0 if this is not an alt ref else the offset of the source frame
4344 // used as the arf midpoint.
4345 static int get_arf_src_index(VP9_COMP *cpi) {
4346 RATE_CONTROL *const rc = &cpi->rc;
4347 int arf_src_index = 0;
4348 if (is_altref_enabled(cpi)) {
4349 if (cpi->oxcf.pass == 2) {
4350 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4351 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4352 arf_src_index = gf_group->arf_src_offset[gf_group->index];
4354 } else if (rc->source_alt_ref_pending) {
4355 arf_src_index = rc->frames_till_gf_update_due;
4358 return arf_src_index;
4361 static void check_src_altref(VP9_COMP *cpi,
4362 const struct lookahead_entry *source) {
4363 RATE_CONTROL *const rc = &cpi->rc;
4365 if (cpi->oxcf.pass == 2) {
4366 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4367 rc->is_src_frame_alt_ref =
4368 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
4370 rc->is_src_frame_alt_ref =
4371 cpi->alt_ref_source && (source == cpi->alt_ref_source);
4374 if (rc->is_src_frame_alt_ref) {
4375 // Current frame is an ARF overlay frame.
4376 cpi->alt_ref_source = NULL;
4378 // Don't refresh the last buffer for an ARF overlay frame. It will
4379 // become the GF so preserve last as an alternative prediction option.
4380 cpi->refresh_last_frame = 0;
4384 #if CONFIG_INTERNAL_STATS
4385 extern double vp9_get_blockiness(const uint8_t *img1, int img1_pitch,
4386 const uint8_t *img2, int img2_pitch, int width,
4389 static void adjust_image_stat(double y, double u, double v, double all,
4394 s->stat[ALL] += all;
4395 s->worst = VPXMIN(s->worst, all);
4397 #endif // CONFIG_INTERNAL_STATS
4399 // Adjust the maximum allowable frame size for the target level.
4400 static void level_rc_framerate(VP9_COMP *cpi, int arf_src_index) {
4401 RATE_CONTROL *const rc = &cpi->rc;
4402 LevelConstraint *const ls = &cpi->level_constraint;
4403 VP9_COMMON *const cm = &cpi->common;
4404 const double max_cpb_size = ls->max_cpb_size;
4405 vpx_clear_system_state();
4406 rc->max_frame_bandwidth = VPXMIN(rc->max_frame_bandwidth, ls->max_frame_size);
4407 if (frame_is_intra_only(cm)) {
4408 rc->max_frame_bandwidth =
4409 VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.5));
4410 } else if (arf_src_index > 0) {
4411 rc->max_frame_bandwidth =
4412 VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.4));
4414 rc->max_frame_bandwidth =
4415 VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.2));
4419 static void update_level_info(VP9_COMP *cpi, size_t *size, int arf_src_index) {
4420 VP9_COMMON *const cm = &cpi->common;
4421 Vp9LevelInfo *const level_info = &cpi->level_info;
4422 Vp9LevelSpec *const level_spec = &level_info->level_spec;
4423 Vp9LevelStats *const level_stats = &level_info->level_stats;
4425 uint64_t luma_samples, dur_end;
4426 const uint32_t luma_pic_size = cm->width * cm->height;
4427 LevelConstraint *const level_constraint = &cpi->level_constraint;
4428 const int8_t level_index = level_constraint->level_index;
4429 double cpb_data_size;
4431 vpx_clear_system_state();
4433 // update level_stats
4434 level_stats->total_compressed_size += *size;
4435 if (cm->show_frame) {
4436 level_stats->total_uncompressed_size +=
4438 2 * (luma_pic_size >> (cm->subsampling_x + cm->subsampling_y));
4439 level_stats->time_encoded =
4440 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
4441 (double)TICKS_PER_SEC;
4444 if (arf_src_index > 0) {
4445 if (!level_stats->seen_first_altref) {
4446 level_stats->seen_first_altref = 1;
4447 } else if (level_stats->frames_since_last_altref <
4448 level_spec->min_altref_distance) {
4449 level_spec->min_altref_distance = level_stats->frames_since_last_altref;
4451 level_stats->frames_since_last_altref = 0;
4453 ++level_stats->frames_since_last_altref;
4456 if (level_stats->frame_window_buffer.len < FRAME_WINDOW_SIZE - 1) {
4457 idx = (level_stats->frame_window_buffer.start +
4458 level_stats->frame_window_buffer.len++) %
4461 idx = level_stats->frame_window_buffer.start;
4462 level_stats->frame_window_buffer.start = (idx + 1) % FRAME_WINDOW_SIZE;
4464 level_stats->frame_window_buffer.buf[idx].ts = cpi->last_time_stamp_seen;
4465 level_stats->frame_window_buffer.buf[idx].size = (uint32_t)(*size);
4466 level_stats->frame_window_buffer.buf[idx].luma_samples = luma_pic_size;
4468 if (cm->frame_type == KEY_FRAME) {
4469 level_stats->ref_refresh_map = 0;
4472 level_stats->ref_refresh_map |= vp9_get_refresh_mask(cpi);
4473 // Also need to consider the case where the encoder refers to a buffer
4474 // that has been implicitly refreshed after encoding a keyframe.
4475 if (!cm->intra_only) {
4476 level_stats->ref_refresh_map |= (1 << cpi->lst_fb_idx);
4477 level_stats->ref_refresh_map |= (1 << cpi->gld_fb_idx);
4478 level_stats->ref_refresh_map |= (1 << cpi->alt_fb_idx);
4480 for (i = 0; i < REF_FRAMES; ++i) {
4481 count += (level_stats->ref_refresh_map >> i) & 1;
4483 if (count > level_spec->max_ref_frame_buffers) {
4484 level_spec->max_ref_frame_buffers = count;
4488 // update average_bitrate
4489 level_spec->average_bitrate = (double)level_stats->total_compressed_size /
4490 125.0 / level_stats->time_encoded;
4492 // update max_luma_sample_rate
4494 for (i = 0; i < level_stats->frame_window_buffer.len; ++i) {
4495 idx = (level_stats->frame_window_buffer.start +
4496 level_stats->frame_window_buffer.len - 1 - i) %
4499 dur_end = level_stats->frame_window_buffer.buf[idx].ts;
4501 if (dur_end - level_stats->frame_window_buffer.buf[idx].ts >=
4505 luma_samples += level_stats->frame_window_buffer.buf[idx].luma_samples;
4507 if (luma_samples > level_spec->max_luma_sample_rate) {
4508 level_spec->max_luma_sample_rate = luma_samples;
4511 // update max_cpb_size
4513 for (i = 0; i < CPB_WINDOW_SIZE; ++i) {
4514 if (i >= level_stats->frame_window_buffer.len) break;
4515 idx = (level_stats->frame_window_buffer.start +
4516 level_stats->frame_window_buffer.len - 1 - i) %
4518 cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
4520 cpb_data_size = cpb_data_size / 125.0;
4521 if (cpb_data_size > level_spec->max_cpb_size) {
4522 level_spec->max_cpb_size = cpb_data_size;
4525 // update max_luma_picture_size
4526 if (luma_pic_size > level_spec->max_luma_picture_size) {
4527 level_spec->max_luma_picture_size = luma_pic_size;
4530 // update compression_ratio
4531 level_spec->compression_ratio = (double)level_stats->total_uncompressed_size *
4533 level_stats->total_compressed_size / 8.0;
4535 // update max_col_tiles
4536 if (level_spec->max_col_tiles < (1 << cm->log2_tile_cols)) {
4537 level_spec->max_col_tiles = (1 << cm->log2_tile_cols);
4540 if (level_index >= 0 && level_constraint->fail_flag == 0) {
4541 if (level_spec->max_luma_picture_size >
4542 vp9_level_defs[level_index].max_luma_picture_size) {
4543 level_constraint->fail_flag |= (1 << LUMA_PIC_SIZE_TOO_LARGE);
4544 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4545 "Failed to encode to the target level %d. %s",
4546 vp9_level_defs[level_index].level,
4547 level_fail_messages[LUMA_PIC_SIZE_TOO_LARGE]);
4550 if ((double)level_spec->max_luma_sample_rate >
4551 (double)vp9_level_defs[level_index].max_luma_sample_rate *
4552 (1 + SAMPLE_RATE_GRACE_P)) {
4553 level_constraint->fail_flag |= (1 << LUMA_SAMPLE_RATE_TOO_LARGE);
4554 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4555 "Failed to encode to the target level %d. %s",
4556 vp9_level_defs[level_index].level,
4557 level_fail_messages[LUMA_SAMPLE_RATE_TOO_LARGE]);
4560 if (level_spec->max_col_tiles > vp9_level_defs[level_index].max_col_tiles) {
4561 level_constraint->fail_flag |= (1 << TOO_MANY_COLUMN_TILE);
4562 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4563 "Failed to encode to the target level %d. %s",
4564 vp9_level_defs[level_index].level,
4565 level_fail_messages[TOO_MANY_COLUMN_TILE]);
4568 if (level_spec->min_altref_distance <
4569 vp9_level_defs[level_index].min_altref_distance) {
4570 level_constraint->fail_flag |= (1 << ALTREF_DIST_TOO_SMALL);
4571 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4572 "Failed to encode to the target level %d. %s",
4573 vp9_level_defs[level_index].level,
4574 level_fail_messages[ALTREF_DIST_TOO_SMALL]);
4577 if (level_spec->max_ref_frame_buffers >
4578 vp9_level_defs[level_index].max_ref_frame_buffers) {
4579 level_constraint->fail_flag |= (1 << TOO_MANY_REF_BUFFER);
4580 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4581 "Failed to encode to the target level %d. %s",
4582 vp9_level_defs[level_index].level,
4583 level_fail_messages[TOO_MANY_REF_BUFFER]);
4586 if (level_spec->max_cpb_size > vp9_level_defs[level_index].max_cpb_size) {
4587 level_constraint->fail_flag |= (1 << CPB_TOO_LARGE);
4588 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
4589 "Failed to encode to the target level %d. %s",
4590 vp9_level_defs[level_index].level,
4591 level_fail_messages[CPB_TOO_LARGE]);
4594 // Set an upper bound for the next frame size. It will be used in
4595 // level_rc_framerate() before encoding the next frame.
4597 for (i = 0; i < CPB_WINDOW_SIZE - 1; ++i) {
4598 if (i >= level_stats->frame_window_buffer.len) break;
4599 idx = (level_stats->frame_window_buffer.start +
4600 level_stats->frame_window_buffer.len - 1 - i) %
4602 cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
4604 cpb_data_size = cpb_data_size / 125.0;
4605 level_constraint->max_frame_size =
4606 (int)((vp9_level_defs[level_index].max_cpb_size - cpb_data_size) *
4608 if (level_stats->frame_window_buffer.len < CPB_WINDOW_SIZE - 1)
4609 level_constraint->max_frame_size >>= 1;
4613 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
4614 size_t *size, uint8_t *dest, int64_t *time_stamp,
4615 int64_t *time_end, int flush) {
4616 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
4617 VP9_COMMON *const cm = &cpi->common;
4618 BufferPool *const pool = cm->buffer_pool;
4619 RATE_CONTROL *const rc = &cpi->rc;
4620 struct vpx_usec_timer cmptimer;
4621 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4622 struct lookahead_entry *last_source = NULL;
4623 struct lookahead_entry *source = NULL;
4627 if (is_two_pass_svc(cpi)) {
4628 #if CONFIG_SPATIAL_SVC
4629 vp9_svc_start_frame(cpi);
4630 // Use a small empty frame instead of a real frame
4631 if (cpi->svc.encode_empty_frame_state == ENCODING)
4632 source = &cpi->svc.empty_frame;
4634 if (oxcf->pass == 2) vp9_restore_layer_context(cpi);
4635 } else if (is_one_pass_cbr_svc(cpi)) {
4636 vp9_one_pass_cbr_svc_start_layer(cpi);
4639 vpx_usec_timer_start(&cmptimer);
4641 vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
4643 // Is multi-arf enabled.
4644 // Note that at the moment multi_arf is only configured for 2 pass VBR and
4645 // will not work properly with svc.
4646 if ((oxcf->pass == 2) && !cpi->use_svc && (cpi->oxcf.enable_auto_arf > 1))
4647 cpi->multi_arf_allowed = 1;
4649 cpi->multi_arf_allowed = 0;
4652 cm->reset_frame_context = 0;
4653 cm->refresh_frame_context = 1;
4654 if (!is_one_pass_cbr_svc(cpi)) {
4655 cpi->refresh_last_frame = 1;
4656 cpi->refresh_golden_frame = 0;
4657 cpi->refresh_alt_ref_frame = 0;
4660 // Should we encode an arf frame.
4661 arf_src_index = get_arf_src_index(cpi);
4663 // Skip alt frame if we encode the empty frame
4664 if (is_two_pass_svc(cpi) && source != NULL) arf_src_index = 0;
4666 if (arf_src_index) {
4667 for (i = 0; i <= arf_src_index; ++i) {
4668 struct lookahead_entry *e = vp9_lookahead_peek(cpi->lookahead, i);
4669 // Avoid creating an alt-ref if there's a forced keyframe pending.
4672 } else if (e->flags == VPX_EFLAG_FORCE_KF) {
4680 if (arf_src_index) {
4681 assert(arf_src_index <= rc->frames_to_key);
4683 if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
4684 cpi->alt_ref_source = source;
4686 #if CONFIG_SPATIAL_SVC
4687 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
4689 // Reference a hidden frame from a lower layer
4690 for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
4691 if (oxcf->ss_enable_auto_arf[i]) {
4692 cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
4697 cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
4700 if ((oxcf->mode != REALTIME) && (oxcf->arnr_max_frames > 0) &&
4701 (oxcf->arnr_strength > 0)) {
4702 int bitrate = cpi->rc.avg_frame_bandwidth / 40;
4703 int not_low_bitrate = bitrate > ALT_REF_AQ_LOW_BITRATE_BOUNDARY;
4705 int not_last_frame = (cpi->lookahead->sz - arf_src_index > 1);
4706 not_last_frame |= ALT_REF_AQ_APPLY_TO_LAST_FRAME;
4708 // Produce the filtered ARF frame.
4709 vp9_temporal_filter(cpi, arf_src_index);
4710 vpx_extend_frame_borders(&cpi->alt_ref_buffer);
4712 // for small bitrates segmentation overhead usually
4713 // eats all bitrate gain from enabling delta quantizers
4714 if (cpi->oxcf.alt_ref_aq != 0 && not_low_bitrate && not_last_frame)
4715 vp9_alt_ref_aq_setup_mode(cpi->alt_ref_aq, cpi);
4717 force_src_buffer = &cpi->alt_ref_buffer;
4722 cpi->refresh_alt_ref_frame = 1;
4723 cpi->refresh_golden_frame = 0;
4724 cpi->refresh_last_frame = 0;
4725 rc->is_src_frame_alt_ref = 0;
4726 rc->source_alt_ref_pending = 0;
4728 rc->source_alt_ref_pending = 0;
4733 // Get last frame source.
4734 if (cm->current_video_frame > 0) {
4735 if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
4739 // Read in the source frame.
4741 source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
4743 source = vp9_lookahead_pop(cpi->lookahead, flush);
4745 if (source != NULL) {
4748 // if the flags indicate intra frame, but if the current picture is for
4749 // non-zero spatial layer, it should not be an intra picture.
4750 // TODO(Won Kap): this needs to change if per-layer intra frame is
4752 if ((source->flags & VPX_EFLAG_FORCE_KF) &&
4753 cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode) {
4754 source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
4757 // Check to see if the frame should be encoded as an arf overlay.
4758 check_src_altref(cpi, source);
4763 cpi->un_scaled_source = cpi->Source =
4764 force_src_buffer ? force_src_buffer : &source->img;
4766 #ifdef ENABLE_KF_DENOISE
4767 // Copy of raw source for metrics calculation.
4768 if (is_psnr_calc_enabled(cpi))
4769 vp9_copy_and_extend_frame(cpi->Source, &cpi->raw_unscaled_source);
4772 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
4774 *time_stamp = source->ts_start;
4775 *time_end = source->ts_end;
4776 *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
4780 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
4781 vp9_end_first_pass(cpi); /* get last stats packet */
4782 cpi->twopass.first_pass_done = 1;
4787 if (source->ts_start < cpi->first_time_stamp_ever) {
4788 cpi->first_time_stamp_ever = source->ts_start;
4789 cpi->last_end_time_stamp_seen = source->ts_start;
4792 // Clear down mmx registers
4793 vpx_clear_system_state();
4795 // adjust frame rates based on timestamps given
4796 if (cm->show_frame) {
4797 adjust_frame_rate(cpi, source);
4800 if (is_one_pass_cbr_svc(cpi)) {
4801 vp9_update_temporal_layer_framerate(cpi);
4802 vp9_restore_layer_context(cpi);
4805 // Find a free buffer for the new frame, releasing the reference previously
4807 if (cm->new_fb_idx != INVALID_IDX) {
4808 --pool->frame_bufs[cm->new_fb_idx].ref_count;
4810 cm->new_fb_idx = get_free_fb(cm);
4812 if (cm->new_fb_idx == INVALID_IDX) return -1;
4814 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
4816 if (!cpi->use_svc && cpi->multi_arf_allowed) {
4817 if (cm->frame_type == KEY_FRAME) {
4818 init_buffer_indices(cpi);
4819 } else if (oxcf->pass == 2) {
4820 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4821 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
4825 // Start with a 0 size frame.
4828 cpi->frame_flags = *frame_flags;
4830 if ((oxcf->pass == 2) &&
4831 (!cpi->use_svc || (is_two_pass_svc(cpi) &&
4832 cpi->svc.encode_empty_frame_state != ENCODING))) {
4833 vp9_rc_get_second_pass_params(cpi);
4834 } else if (oxcf->pass == 1) {
4835 set_frame_size(cpi);
4838 if (oxcf->pass != 1 && cpi->level_constraint.level_index >= 0 &&
4839 cpi->level_constraint.fail_flag == 0)
4840 level_rc_framerate(cpi, arf_src_index);
4842 if (cpi->oxcf.pass != 0 || cpi->use_svc || frame_is_intra_only(cm) == 1) {
4843 for (i = 0; i < MAX_REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
4846 cpi->td.mb.fp_src_pred = 0;
4847 if (oxcf->pass == 1 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
4848 const int lossless = is_lossless_requested(oxcf);
4849 #if CONFIG_VP9_HIGHBITDEPTH
4850 if (cpi->oxcf.use_highbitdepth)
4851 cpi->td.mb.fwd_txm4x4 =
4852 lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
4854 cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4855 cpi->td.mb.highbd_itxm_add =
4856 lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
4858 cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4859 #endif // CONFIG_VP9_HIGHBITDEPTH
4860 cpi->td.mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
4861 vp9_first_pass(cpi, source);
4862 } else if (oxcf->pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
4863 Pass2Encode(cpi, size, dest, frame_flags);
4864 } else if (cpi->use_svc) {
4865 SvcEncode(cpi, size, dest, frame_flags);
4868 Pass0Encode(cpi, size, dest, frame_flags);
4871 if (cm->refresh_frame_context)
4872 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4874 // No frame encoded, or frame was dropped, release scaled references.
4875 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
4876 release_scaled_references(cpi);
4880 cpi->droppable = !frame_is_reference(cpi);
4883 // Save layer specific state.
4884 if (is_one_pass_cbr_svc(cpi) || ((cpi->svc.number_temporal_layers > 1 ||
4885 cpi->svc.number_spatial_layers > 1) &&
4887 vp9_save_layer_context(cpi);
4890 vpx_usec_timer_mark(&cmptimer);
4891 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
4893 // Should we calculate metrics for the frame.
4894 if (is_psnr_calc_enabled(cpi)) generate_psnr_packet(cpi);
4896 if (cpi->keep_level_stats && oxcf->pass != 1)
4897 update_level_info(cpi, size, arf_src_index);
4899 #if CONFIG_INTERNAL_STATS
4901 if (oxcf->pass != 1) {
4902 double samples = 0.0;
4903 cpi->bytes += (int)(*size);
4905 if (cm->show_frame) {
4906 uint32_t bit_depth = 8;
4907 uint32_t in_bit_depth = 8;
4909 #if CONFIG_VP9_HIGHBITDEPTH
4910 if (cm->use_highbitdepth) {
4911 in_bit_depth = cpi->oxcf.input_bit_depth;
4912 bit_depth = cm->bit_depth;
4916 if (cpi->b_calculate_psnr) {
4917 YV12_BUFFER_CONFIG *orig = cpi->raw_source_frame;
4918 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
4919 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
4921 #if CONFIG_VP9_HIGHBITDEPTH
4922 vpx_calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
4925 vpx_calc_psnr(orig, recon, &psnr);
4926 #endif // CONFIG_VP9_HIGHBITDEPTH
4928 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3],
4929 psnr.psnr[0], &cpi->psnr);
4930 cpi->total_sq_error += psnr.sse[0];
4931 cpi->total_samples += psnr.samples[0];
4932 samples = psnr.samples[0];
4936 double frame_ssim2 = 0, weight = 0;
4937 #if CONFIG_VP9_POSTPROC
4938 if (vpx_alloc_frame_buffer(
4939 pp, recon->y_crop_width, recon->y_crop_height,
4940 cm->subsampling_x, cm->subsampling_y,
4941 #if CONFIG_VP9_HIGHBITDEPTH
4942 cm->use_highbitdepth,
4944 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment) < 0) {
4945 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
4946 "Failed to allocate post processing buffer");
4949 vp9_ppflags_t ppflags;
4950 ppflags.post_proc_flag = VP9D_DEBLOCK;
4951 ppflags.deblocking_level = 0; // not used in vp9_post_proc_frame()
4952 ppflags.noise_level = 0; // not used in vp9_post_proc_frame()
4953 vp9_post_proc_frame(cm, pp, &ppflags);
4956 vpx_clear_system_state();
4958 #if CONFIG_VP9_HIGHBITDEPTH
4959 vpx_calc_highbd_psnr(orig, pp, &psnr2, cpi->td.mb.e_mbd.bd,
4960 cpi->oxcf.input_bit_depth);
4962 vpx_calc_psnr(orig, pp, &psnr2);
4963 #endif // CONFIG_VP9_HIGHBITDEPTH
4965 cpi->totalp_sq_error += psnr2.sse[0];
4966 cpi->totalp_samples += psnr2.samples[0];
4967 adjust_image_stat(psnr2.psnr[1], psnr2.psnr[2], psnr2.psnr[3],
4968 psnr2.psnr[0], &cpi->psnrp);
4970 #if CONFIG_VP9_HIGHBITDEPTH
4971 if (cm->use_highbitdepth) {
4972 frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight, bit_depth,
4975 frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
4978 frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
4979 #endif // CONFIG_VP9_HIGHBITDEPTH
4981 cpi->worst_ssim = VPXMIN(cpi->worst_ssim, frame_ssim2);
4982 cpi->summed_quality += frame_ssim2 * weight;
4983 cpi->summed_weights += weight;
4985 #if CONFIG_VP9_HIGHBITDEPTH
4986 if (cm->use_highbitdepth) {
4987 frame_ssim2 = vpx_highbd_calc_ssim(orig, pp, &weight, bit_depth,
4990 frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
4993 frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
4994 #endif // CONFIG_VP9_HIGHBITDEPTH
4996 cpi->summedp_quality += frame_ssim2 * weight;
4997 cpi->summedp_weights += weight;
5000 FILE *f = fopen("q_used.stt", "a");
5001 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
5002 cpi->common.current_video_frame, y2, u2, v2,
5003 frame_psnr2, frame_ssim2);
5009 if (cpi->b_calculate_blockiness) {
5010 #if CONFIG_VP9_HIGHBITDEPTH
5011 if (!cm->use_highbitdepth)
5014 double frame_blockiness = vp9_get_blockiness(
5015 cpi->Source->y_buffer, cpi->Source->y_stride,
5016 cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
5017 cpi->Source->y_width, cpi->Source->y_height);
5018 cpi->worst_blockiness =
5019 VPXMAX(cpi->worst_blockiness, frame_blockiness);
5020 cpi->total_blockiness += frame_blockiness;
5024 if (cpi->b_calculate_consistency) {
5025 #if CONFIG_VP9_HIGHBITDEPTH
5026 if (!cm->use_highbitdepth)
5029 double this_inconsistency = vpx_get_ssim_metrics(
5030 cpi->Source->y_buffer, cpi->Source->y_stride,
5031 cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
5032 cpi->Source->y_width, cpi->Source->y_height, cpi->ssim_vars,
5035 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
5036 double consistency =
5037 vpx_sse_to_psnr(samples, peak, (double)cpi->total_inconsistency);
5038 if (consistency > 0.0)
5039 cpi->worst_consistency =
5040 VPXMIN(cpi->worst_consistency, consistency);
5041 cpi->total_inconsistency += this_inconsistency;
5046 double y, u, v, frame_all;
5047 frame_all = vpx_calc_fastssim(cpi->Source, cm->frame_to_show, &y, &u,
5048 &v, bit_depth, in_bit_depth);
5049 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
5052 double y, u, v, frame_all;
5053 frame_all = vpx_psnrhvs(cpi->Source, cm->frame_to_show, &y, &u, &v,
5054 bit_depth, in_bit_depth);
5055 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5062 if (is_two_pass_svc(cpi)) {
5063 if (cpi->svc.encode_empty_frame_state == ENCODING) {
5064 cpi->svc.encode_empty_frame_state = ENCODED;
5065 cpi->svc.encode_intra_empty_frame = 0;
5068 if (cm->show_frame) {
5069 ++cpi->svc.spatial_layer_to_encode;
5070 if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
5071 cpi->svc.spatial_layer_to_encode = 0;
5073 // May need the empty frame after an visible frame.
5074 cpi->svc.encode_empty_frame_state = NEED_TO_ENCODE;
5076 } else if (is_one_pass_cbr_svc(cpi)) {
5077 if (cm->show_frame) {
5078 ++cpi->svc.spatial_layer_to_encode;
5079 if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
5080 cpi->svc.spatial_layer_to_encode = 0;
5084 vpx_clear_system_state();
5088 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
5089 vp9_ppflags_t *flags) {
5090 VP9_COMMON *cm = &cpi->common;
5091 #if !CONFIG_VP9_POSTPROC
5095 if (!cm->show_frame) {
5099 #if CONFIG_VP9_POSTPROC
5100 ret = vp9_post_proc_frame(cm, dest, flags);
5102 if (cm->frame_to_show) {
5103 *dest = *cm->frame_to_show;
5104 dest->y_width = cm->width;
5105 dest->y_height = cm->height;
5106 dest->uv_width = cm->width >> cm->subsampling_x;
5107 dest->uv_height = cm->height >> cm->subsampling_y;
5112 #endif // !CONFIG_VP9_POSTPROC
5113 vpx_clear_system_state();
5118 int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING horiz_mode,
5119 VPX_SCALING vert_mode) {
5120 VP9_COMMON *cm = &cpi->common;
5121 int hr = 0, hs = 0, vr = 0, vs = 0;
5123 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5125 Scale2Ratio(horiz_mode, &hr, &hs);
5126 Scale2Ratio(vert_mode, &vr, &vs);
5128 // always go to the next whole number
5129 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5130 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
5131 if (cm->current_video_frame) {
5132 assert(cm->width <= cpi->initial_width);
5133 assert(cm->height <= cpi->initial_height);
5136 update_frame_size(cpi);
5141 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
5142 unsigned int height) {
5143 VP9_COMMON *cm = &cpi->common;
5144 #if CONFIG_VP9_HIGHBITDEPTH
5145 check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
5147 check_initial_width(cpi, 1, 1);
5148 #endif // CONFIG_VP9_HIGHBITDEPTH
5150 #if CONFIG_VP9_TEMPORAL_DENOISING
5151 setup_denoiser_buffer(cpi);
5156 if (cm->width > cpi->initial_width) {
5157 cm->width = cpi->initial_width;
5158 printf("Warning: Desired width too large, changed to %d\n", cm->width);
5163 cm->height = height;
5164 if (cm->height > cpi->initial_height) {
5165 cm->height = cpi->initial_height;
5166 printf("Warning: Desired height too large, changed to %d\n", cm->height);
5169 assert(cm->width <= cpi->initial_width);
5170 assert(cm->height <= cpi->initial_height);
5172 update_frame_size(cpi);
5177 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
5178 cpi->use_svc = use_svc;
5182 int vp9_get_quantizer(VP9_COMP *cpi) { return cpi->common.base_qindex; }
5184 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
5186 (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
5189 if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP9_LAST_FLAG;
5191 if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP9_GOLD_FLAG;
5193 if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP9_ALT_FLAG;
5195 vp9_use_as_reference(cpi, ref);
5199 (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
5200 VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
5203 if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP9_LAST_FLAG;
5205 if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP9_GOLD_FLAG;
5207 if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP9_ALT_FLAG;
5209 vp9_update_reference(cpi, upd);
5212 if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
5213 vp9_update_entropy(cpi, 0);