2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
15 #include "./vpx_config.h"
16 #include "./vpx_scale_rtcd.h"
17 #include "vpx/internal/vpx_psnr.h"
18 #include "vpx_ports/vpx_timer.h"
20 #include "vp9/common/vp9_alloccommon.h"
21 #include "vp9/common/vp9_filter.h"
22 #include "vp9/common/vp9_idct.h"
23 #if CONFIG_VP9_POSTPROC
24 #include "vp9/common/vp9_postproc.h"
26 #include "vp9/common/vp9_reconinter.h"
27 #include "vp9/common/vp9_reconintra.h"
28 #include "vp9/common/vp9_systemdependent.h"
29 #include "vp9/common/vp9_tile_common.h"
31 #include "vp9/encoder/vp9_aq_complexity.h"
32 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
33 #include "vp9/encoder/vp9_aq_variance.h"
34 #include "vp9/encoder/vp9_bitstream.h"
35 #include "vp9/encoder/vp9_context_tree.h"
36 #include "vp9/encoder/vp9_encodeframe.h"
37 #include "vp9/encoder/vp9_encodemv.h"
38 #include "vp9/encoder/vp9_firstpass.h"
39 #include "vp9/encoder/vp9_mbgraph.h"
40 #include "vp9/encoder/vp9_encoder.h"
41 #include "vp9/encoder/vp9_picklpf.h"
42 #include "vp9/encoder/vp9_ratectrl.h"
43 #include "vp9/encoder/vp9_rd.h"
44 #include "vp9/encoder/vp9_segmentation.h"
45 #include "vp9/encoder/vp9_speed_features.h"
46 #if CONFIG_INTERNAL_STATS
47 #include "vp9/encoder/vp9_ssim.h"
49 #include "vp9/encoder/vp9_temporal_filter.h"
50 #include "vp9/encoder/vp9_resize.h"
51 #include "vp9/encoder/vp9_svc_layercontext.h"
53 void vp9_coef_tree_initialize();
55 #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
57 #define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
58 // for altref computation.
59 #define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
60 // mv. Choose a very high value for
61 // now so that HIGH_PRECISION is always
64 // #define OUTPUT_YUV_REC
66 #ifdef OUTPUT_YUV_DENOISED
67 FILE *yuv_denoised_file = NULL;
79 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
105 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
106 MACROBLOCK *const mb = &cpi->mb;
107 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
108 if (cpi->common.allow_high_precision_mv) {
109 mb->mvcost = mb->nmvcost_hp;
110 mb->mvsadcost = mb->nmvsadcost_hp;
112 mb->mvcost = mb->nmvcost;
113 mb->mvsadcost = mb->nmvsadcost;
117 static void setup_frame(VP9_COMP *cpi) {
118 VP9_COMMON *const cm = &cpi->common;
119 // Set up entropy context depending on frame type. The decoder mandates
120 // the use of the default context, index 0, for keyframes and inter
121 // frames where the error_resilient_mode or intra_only flag is set. For
122 // other inter-frames the encoder currently uses only two contexts;
123 // context 1 for ALTREF frames and context 0 for the others.
124 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
125 vp9_setup_past_independence(cm);
128 cm->frame_context_idx = cpi->refresh_alt_ref_frame;
131 if (cm->frame_type == KEY_FRAME) {
132 if (!is_two_pass_svc(cpi))
133 cpi->refresh_golden_frame = 1;
134 cpi->refresh_alt_ref_frame = 1;
135 vp9_zero(cpi->interp_filter_selected);
137 cm->fc = cm->frame_contexts[cm->frame_context_idx];
138 vp9_zero(cpi->interp_filter_selected[0]);
142 void vp9_initialize_enc() {
143 static int init_done = 0;
147 vp9_init_intra_predictors();
148 vp9_coef_tree_initialize();
149 vp9_tokenize_initialize();
151 vp9_rc_init_minq_luts();
152 vp9_entropy_mv_init();
153 vp9_entropy_mode_init();
154 vp9_temporal_filter_init();
159 static void dealloc_compressor_data(VP9_COMP *cpi) {
160 VP9_COMMON *const cm = &cpi->common;
163 // Delete sementation map
164 vpx_free(cpi->segmentation_map);
165 cpi->segmentation_map = NULL;
166 vpx_free(cm->last_frame_seg_map);
167 cm->last_frame_seg_map = NULL;
168 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
169 cpi->coding_context.last_frame_seg_map_copy = NULL;
171 vpx_free(cpi->complexity_map);
172 cpi->complexity_map = NULL;
174 vpx_free(cpi->nmvcosts[0]);
175 vpx_free(cpi->nmvcosts[1]);
176 cpi->nmvcosts[0] = NULL;
177 cpi->nmvcosts[1] = NULL;
179 vpx_free(cpi->nmvcosts_hp[0]);
180 vpx_free(cpi->nmvcosts_hp[1]);
181 cpi->nmvcosts_hp[0] = NULL;
182 cpi->nmvcosts_hp[1] = NULL;
184 vpx_free(cpi->nmvsadcosts[0]);
185 vpx_free(cpi->nmvsadcosts[1]);
186 cpi->nmvsadcosts[0] = NULL;
187 cpi->nmvsadcosts[1] = NULL;
189 vpx_free(cpi->nmvsadcosts_hp[0]);
190 vpx_free(cpi->nmvsadcosts_hp[1]);
191 cpi->nmvsadcosts_hp[0] = NULL;
192 cpi->nmvsadcosts_hp[1] = NULL;
194 vp9_cyclic_refresh_free(cpi->cyclic_refresh);
195 cpi->cyclic_refresh = NULL;
197 vp9_free_ref_frame_buffers(cm);
198 vp9_free_context_buffers(cm);
200 vp9_free_frame_buffer(&cpi->last_frame_uf);
201 vp9_free_frame_buffer(&cpi->scaled_source);
202 vp9_free_frame_buffer(&cpi->scaled_last_source);
203 vp9_free_frame_buffer(&cpi->alt_ref_buffer);
204 vp9_lookahead_destroy(cpi->lookahead);
209 vp9_free_pc_tree(cpi);
211 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
212 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
213 vpx_free(lc->rc_twopass_stats_in.buf);
214 lc->rc_twopass_stats_in.buf = NULL;
215 lc->rc_twopass_stats_in.sz = 0;
218 if (cpi->source_diff_var != NULL) {
219 vpx_free(cpi->source_diff_var);
220 cpi->source_diff_var = NULL;
223 for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
224 vp9_free_frame_buffer(&cpi->svc.scaled_frames[i]);
226 vpx_memset(&cpi->svc.scaled_frames[0], 0,
227 MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
230 static void save_coding_context(VP9_COMP *cpi) {
231 CODING_CONTEXT *const cc = &cpi->coding_context;
232 VP9_COMMON *cm = &cpi->common;
234 // Stores a snapshot of key state variables which can subsequently be
235 // restored with a call to vp9_restore_coding_context. These functions are
236 // intended for use in a re-code loop in vp9_compress_frame where the
237 // quantizer value is adjusted between loop iterations.
238 vp9_copy(cc->nmvjointcost, cpi->mb.nmvjointcost);
240 vpx_memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
241 MV_VALS * sizeof(*cpi->nmvcosts[0]));
242 vpx_memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
243 MV_VALS * sizeof(*cpi->nmvcosts[1]));
244 vpx_memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
245 MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
246 vpx_memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
247 MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
249 vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
251 vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
252 cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols));
254 vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
255 vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
260 static void restore_coding_context(VP9_COMP *cpi) {
261 CODING_CONTEXT *const cc = &cpi->coding_context;
262 VP9_COMMON *cm = &cpi->common;
264 // Restore key state variables to the snapshot state stored in the
265 // previous call to vp9_save_coding_context.
266 vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost);
268 vpx_memcpy(cpi->nmvcosts[0], cc->nmvcosts[0],
269 MV_VALS * sizeof(*cc->nmvcosts[0]));
270 vpx_memcpy(cpi->nmvcosts[1], cc->nmvcosts[1],
271 MV_VALS * sizeof(*cc->nmvcosts[1]));
272 vpx_memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
273 MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
274 vpx_memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
275 MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
277 vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
279 vpx_memcpy(cm->last_frame_seg_map,
280 cpi->coding_context.last_frame_seg_map_copy,
281 (cm->mi_rows * cm->mi_cols));
283 vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
284 vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
289 static void configure_static_seg_features(VP9_COMP *cpi) {
290 VP9_COMMON *const cm = &cpi->common;
291 const RATE_CONTROL *const rc = &cpi->rc;
292 struct segmentation *const seg = &cm->seg;
294 int high_q = (int)(rc->avg_q > 48.0);
297 // Disable and clear down for KF
298 if (cm->frame_type == KEY_FRAME) {
299 // Clear down the global segmentation map
300 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
302 seg->update_data = 0;
303 cpi->static_mb_pct = 0;
305 // Disable segmentation
306 vp9_disable_segmentation(seg);
308 // Clear down the segment features.
309 vp9_clearall_segfeatures(seg);
310 } else if (cpi->refresh_alt_ref_frame) {
311 // If this is an alt ref frame
312 // Clear down the global segmentation map
313 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
315 seg->update_data = 0;
316 cpi->static_mb_pct = 0;
318 // Disable segmentation and individual segment features by default
319 vp9_disable_segmentation(seg);
320 vp9_clearall_segfeatures(seg);
322 // Scan frames from current to arf frame.
323 // This function re-enables segmentation if appropriate.
324 vp9_update_mbgraph_stats(cpi);
326 // If segmentation was enabled set those features needed for the
330 seg->update_data = 1;
332 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
334 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
335 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
337 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
338 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
340 // Where relevant assume segment data is delta data
341 seg->abs_delta = SEGMENT_DELTADATA;
343 } else if (seg->enabled) {
344 // All other frames if segmentation has been enabled
346 // First normal frame in a valid gf or alt ref group
347 if (rc->frames_since_golden == 0) {
348 // Set up segment features for normal frames in an arf group
349 if (rc->source_alt_ref_active) {
351 seg->update_data = 1;
352 seg->abs_delta = SEGMENT_DELTADATA;
354 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
356 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
357 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
359 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
360 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
362 // Segment coding disabled for compred testing
363 if (high_q || (cpi->static_mb_pct == 100)) {
364 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
365 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
366 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
369 // Disable segmentation and clear down features if alt ref
370 // is not active for this group
372 vp9_disable_segmentation(seg);
374 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
377 seg->update_data = 0;
379 vp9_clearall_segfeatures(seg);
381 } else if (rc->is_src_frame_alt_ref) {
382 // Special case where we are coding over the top of a previous
384 // Segment coding disabled for compred testing
386 // Enable ref frame features for segment 0 as well
387 vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
388 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
390 // All mbs should use ALTREF_FRAME
391 vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
392 vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
393 vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
394 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
396 // Skip all MBs if high Q (0,0 mv and skip coeffs)
398 vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
399 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
401 // Enable data update
402 seg->update_data = 1;
406 // No updates.. leave things as they are.
408 seg->update_data = 0;
413 static void update_reference_segmentation_map(VP9_COMP *cpi) {
414 VP9_COMMON *const cm = &cpi->common;
415 MODE_INFO *mi_8x8_ptr = cm->mi;
416 uint8_t *cache_ptr = cm->last_frame_seg_map;
419 for (row = 0; row < cm->mi_rows; row++) {
420 MODE_INFO *mi_8x8 = mi_8x8_ptr;
421 uint8_t *cache = cache_ptr;
422 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
423 cache[0] = mi_8x8[0].src_mi->mbmi.segment_id;
424 mi_8x8_ptr += cm->mi_stride;
425 cache_ptr += cm->mi_cols;
429 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
430 VP9_COMMON *cm = &cpi->common;
431 const VP9EncoderConfig *oxcf = &cpi->oxcf;
433 cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
434 cm->subsampling_x, cm->subsampling_y,
435 #if CONFIG_VP9_HIGHBITDEPTH
436 cm->use_highbitdepth,
438 oxcf->lag_in_frames);
440 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
441 "Failed to allocate lag buffers");
443 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
444 oxcf->width, oxcf->height,
445 cm->subsampling_x, cm->subsampling_y,
446 #if CONFIG_VP9_HIGHBITDEPTH
447 cm->use_highbitdepth,
449 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
450 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
451 "Failed to allocate altref buffer");
454 static void alloc_ref_frame_buffers(VP9_COMP *cpi) {
455 VP9_COMMON *const cm = &cpi->common;
456 if (vp9_alloc_ref_frame_buffers(cm, cm->width, cm->height))
457 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
458 "Failed to allocate frame buffers");
461 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
462 VP9_COMMON *const cm = &cpi->common;
463 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
464 cm->width, cm->height,
465 cm->subsampling_x, cm->subsampling_y,
466 #if CONFIG_VP9_HIGHBITDEPTH
467 cm->use_highbitdepth,
469 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
470 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
471 "Failed to allocate last frame buffer");
473 if (vp9_realloc_frame_buffer(&cpi->scaled_source,
474 cm->width, cm->height,
475 cm->subsampling_x, cm->subsampling_y,
476 #if CONFIG_VP9_HIGHBITDEPTH
477 cm->use_highbitdepth,
479 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
480 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
481 "Failed to allocate scaled source buffer");
483 if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
484 cm->width, cm->height,
485 cm->subsampling_x, cm->subsampling_y,
486 #if CONFIG_VP9_HIGHBITDEPTH
487 cm->use_highbitdepth,
489 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
490 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
491 "Failed to allocate scaled last source buffer");
494 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
495 VP9_COMMON *cm = &cpi->common;
497 vp9_alloc_context_buffers(cm, cm->width, cm->height);
502 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
503 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
506 vp9_setup_pc_tree(&cpi->common, cpi);
509 static void update_frame_size(VP9_COMP *cpi) {
510 VP9_COMMON *const cm = &cpi->common;
511 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
513 vp9_set_mb_mi(cm, cm->width, cm->height);
514 vp9_init_context_buffers(cm);
515 init_macroblockd(cm, xd);
517 if (is_two_pass_svc(cpi)) {
518 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
519 cm->width, cm->height,
520 cm->subsampling_x, cm->subsampling_y,
521 #if CONFIG_VP9_HIGHBITDEPTH
522 cm->use_highbitdepth,
524 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
525 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
526 "Failed to reallocate alt_ref_buffer");
530 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
531 cpi->framerate = framerate < 0.1 ? 30 : framerate;
532 vp9_rc_update_framerate(cpi);
535 static void set_tile_limits(VP9_COMP *cpi) {
536 VP9_COMMON *const cm = &cpi->common;
538 int min_log2_tile_cols, max_log2_tile_cols;
539 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
541 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
542 min_log2_tile_cols, max_log2_tile_cols);
543 cm->log2_tile_rows = cpi->oxcf.tile_rows;
546 static void init_buffer_indices(VP9_COMP *cpi) {
552 static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
553 VP9_COMMON *const cm = &cpi->common;
556 cpi->framerate = oxcf->init_framerate;
558 cm->profile = oxcf->profile;
559 cm->bit_depth = oxcf->bit_depth;
560 #if CONFIG_VP9_HIGHBITDEPTH
561 cm->use_highbitdepth = oxcf->use_highbitdepth;
563 cm->color_space = UNKNOWN;
565 cm->width = oxcf->width;
566 cm->height = oxcf->height;
567 vp9_alloc_compressor_data(cpi);
569 // Spatial scalability.
570 cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
571 // Temporal scalability.
572 cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
574 if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
575 ((cpi->svc.number_temporal_layers > 1 ||
576 cpi->svc.number_spatial_layers > 1) &&
577 cpi->oxcf.pass == 2)) {
578 vp9_init_layer_context(cpi);
581 // change includes all joint functionality
582 vp9_change_config(cpi, oxcf);
584 cpi->static_mb_pct = 0;
585 cpi->ref_frame_flags = 0;
587 init_buffer_indices(cpi);
590 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
591 const VP9EncoderConfig *oxcf) {
592 const int64_t bandwidth = oxcf->target_bandwidth;
593 const int64_t starting = oxcf->starting_buffer_level_ms;
594 const int64_t optimal = oxcf->optimal_buffer_level_ms;
595 const int64_t maximum = oxcf->maximum_buffer_size_ms;
597 rc->starting_buffer_level = starting * bandwidth / 1000;
598 rc->optimal_buffer_level = (optimal == 0) ? bandwidth / 8
599 : optimal * bandwidth / 1000;
600 rc->maximum_buffer_size = (maximum == 0) ? bandwidth / 8
601 : maximum * bandwidth / 1000;
604 #if CONFIG_VP9_HIGHBITDEPTH
605 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
606 cpi->fn_ptr[BT].sdf = SDF; \
607 cpi->fn_ptr[BT].sdaf = SDAF; \
608 cpi->fn_ptr[BT].vf = VF; \
609 cpi->fn_ptr[BT].svf = SVF; \
610 cpi->fn_ptr[BT].svaf = SVAF; \
611 cpi->fn_ptr[BT].sdx3f = SDX3F; \
612 cpi->fn_ptr[BT].sdx8f = SDX8F; \
613 cpi->fn_ptr[BT].sdx4df = SDX4DF;
615 #define MAKE_BFP_SAD_WRAPPER(fnname) \
616 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
618 const uint8_t *ref_ptr, \
620 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
622 static unsigned int fnname##_bits10(const uint8_t *src_ptr, \
624 const uint8_t *ref_ptr, \
626 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
628 static unsigned int fnname##_bits12(const uint8_t *src_ptr, \
630 const uint8_t *ref_ptr, \
632 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
635 #define MAKE_BFP_SADAVG_WRAPPER(fnname) static unsigned int \
636 fnname##_bits8(const uint8_t *src_ptr, \
638 const uint8_t *ref_ptr, \
640 const uint8_t *second_pred) { \
641 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
643 static unsigned int fnname##_bits10(const uint8_t *src_ptr, \
645 const uint8_t *ref_ptr, \
647 const uint8_t *second_pred) { \
648 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
651 static unsigned int fnname##_bits12(const uint8_t *src_ptr, \
653 const uint8_t *ref_ptr, \
655 const uint8_t *second_pred) { \
656 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
660 #define MAKE_BFP_SAD3_WRAPPER(fnname) \
661 static void fnname##_bits8(const uint8_t *src_ptr, \
663 const uint8_t *ref_ptr, \
665 unsigned int *sad_array) { \
666 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
668 static void fnname##_bits10(const uint8_t *src_ptr, \
670 const uint8_t *ref_ptr, \
672 unsigned int *sad_array) { \
674 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
675 for (i = 0; i < 3; i++) \
676 sad_array[i] >>= 2; \
678 static void fnname##_bits12(const uint8_t *src_ptr, \
680 const uint8_t *ref_ptr, \
682 unsigned int *sad_array) { \
684 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
685 for (i = 0; i < 3; i++) \
686 sad_array[i] >>= 4; \
689 #define MAKE_BFP_SAD8_WRAPPER(fnname) \
690 static void fnname##_bits8(const uint8_t *src_ptr, \
692 const uint8_t *ref_ptr, \
694 unsigned int *sad_array) { \
695 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
697 static void fnname##_bits10(const uint8_t *src_ptr, \
699 const uint8_t *ref_ptr, \
701 unsigned int *sad_array) { \
703 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
704 for (i = 0; i < 8; i++) \
705 sad_array[i] >>= 2; \
707 static void fnname##_bits12(const uint8_t *src_ptr, \
709 const uint8_t *ref_ptr, \
711 unsigned int *sad_array) { \
713 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
714 for (i = 0; i < 8; i++) \
715 sad_array[i] >>= 4; \
717 #define MAKE_BFP_SAD4D_WRAPPER(fnname) \
718 static void fnname##_bits8(const uint8_t *src_ptr, \
720 const uint8_t* const ref_ptr[], \
722 unsigned int *sad_array) { \
723 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
725 static void fnname##_bits10(const uint8_t *src_ptr, \
727 const uint8_t* const ref_ptr[], \
729 unsigned int *sad_array) { \
731 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
732 for (i = 0; i < 4; i++) \
733 sad_array[i] >>= 2; \
735 static void fnname##_bits12(const uint8_t *src_ptr, \
737 const uint8_t* const ref_ptr[], \
739 unsigned int *sad_array) { \
741 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
742 for (i = 0; i < 4; i++) \
743 sad_array[i] >>= 4; \
746 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad32x16)
747 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad32x16_avg)
748 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad32x16x4d)
749 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad16x32)
750 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad16x32_avg)
751 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad16x32x4d)
752 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad64x32)
753 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad64x32_avg)
754 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad64x32x4d)
755 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad32x64)
756 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad32x64_avg)
757 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad32x64x4d)
758 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad32x32)
759 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad32x32_avg)
760 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad32x32x3)
761 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad32x32x8)
762 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad32x32x4d)
763 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad64x64)
764 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad64x64_avg)
765 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad64x64x3)
766 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad64x64x8)
767 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad64x64x4d)
768 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad16x16)
769 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad16x16_avg)
770 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad16x16x3)
771 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad16x16x8)
772 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad16x16x4d)
773 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad16x8)
774 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad16x8_avg)
775 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad16x8x3)
776 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad16x8x8)
777 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad16x8x4d)
778 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad8x16)
779 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad8x16_avg)
780 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad8x16x3)
781 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad8x16x8)
782 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad8x16x4d)
783 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad8x8)
784 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad8x8_avg)
785 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad8x8x3)
786 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad8x8x8)
787 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad8x8x4d)
788 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad8x4)
789 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad8x4_avg)
790 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad8x4x8)
791 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad8x4x4d)
792 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad4x8)
793 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad4x8_avg)
794 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad4x8x8)
795 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad4x8x4d)
796 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad4x4)
797 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad4x4_avg)
798 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad4x4x3)
799 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad4x4x8)
800 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad4x4x4d)
802 static void highbd_set_var_fns(VP9_COMP *const cpi) {
803 VP9_COMMON *const cm = &cpi->common;
804 if (cm->use_highbitdepth) {
805 switch (cm->bit_depth) {
807 HIGHBD_BFP(BLOCK_32X16,
808 vp9_highbd_sad32x16_bits8,
809 vp9_highbd_sad32x16_avg_bits8,
810 vp9_highbd_variance32x16,
811 vp9_highbd_sub_pixel_variance32x16,
812 vp9_highbd_sub_pixel_avg_variance32x16,
815 vp9_highbd_sad32x16x4d_bits8)
817 HIGHBD_BFP(BLOCK_16X32,
818 vp9_highbd_sad16x32_bits8,
819 vp9_highbd_sad16x32_avg_bits8,
820 vp9_highbd_variance16x32,
821 vp9_highbd_sub_pixel_variance16x32,
822 vp9_highbd_sub_pixel_avg_variance16x32,
825 vp9_highbd_sad16x32x4d_bits8)
827 HIGHBD_BFP(BLOCK_64X32,
828 vp9_highbd_sad64x32_bits8,
829 vp9_highbd_sad64x32_avg_bits8,
830 vp9_highbd_variance64x32,
831 vp9_highbd_sub_pixel_variance64x32,
832 vp9_highbd_sub_pixel_avg_variance64x32,
835 vp9_highbd_sad64x32x4d_bits8)
837 HIGHBD_BFP(BLOCK_32X64,
838 vp9_highbd_sad32x64_bits8,
839 vp9_highbd_sad32x64_avg_bits8,
840 vp9_highbd_variance32x64,
841 vp9_highbd_sub_pixel_variance32x64,
842 vp9_highbd_sub_pixel_avg_variance32x64,
845 vp9_highbd_sad32x64x4d_bits8)
847 HIGHBD_BFP(BLOCK_32X32,
848 vp9_highbd_sad32x32_bits8,
849 vp9_highbd_sad32x32_avg_bits8,
850 vp9_highbd_variance32x32,
851 vp9_highbd_sub_pixel_variance32x32,
852 vp9_highbd_sub_pixel_avg_variance32x32,
853 vp9_highbd_sad32x32x3_bits8,
854 vp9_highbd_sad32x32x8_bits8,
855 vp9_highbd_sad32x32x4d_bits8)
857 HIGHBD_BFP(BLOCK_64X64,
858 vp9_highbd_sad64x64_bits8,
859 vp9_highbd_sad64x64_avg_bits8,
860 vp9_highbd_variance64x64,
861 vp9_highbd_sub_pixel_variance64x64,
862 vp9_highbd_sub_pixel_avg_variance64x64,
863 vp9_highbd_sad64x64x3_bits8,
864 vp9_highbd_sad64x64x8_bits8,
865 vp9_highbd_sad64x64x4d_bits8)
867 HIGHBD_BFP(BLOCK_16X16,
868 vp9_highbd_sad16x16_bits8,
869 vp9_highbd_sad16x16_avg_bits8,
870 vp9_highbd_variance16x16,
871 vp9_highbd_sub_pixel_variance16x16,
872 vp9_highbd_sub_pixel_avg_variance16x16,
873 vp9_highbd_sad16x16x3_bits8,
874 vp9_highbd_sad16x16x8_bits8,
875 vp9_highbd_sad16x16x4d_bits8)
877 HIGHBD_BFP(BLOCK_16X8,
878 vp9_highbd_sad16x8_bits8,
879 vp9_highbd_sad16x8_avg_bits8,
880 vp9_highbd_variance16x8,
881 vp9_highbd_sub_pixel_variance16x8,
882 vp9_highbd_sub_pixel_avg_variance16x8,
883 vp9_highbd_sad16x8x3_bits8,
884 vp9_highbd_sad16x8x8_bits8,
885 vp9_highbd_sad16x8x4d_bits8)
887 HIGHBD_BFP(BLOCK_8X16,
888 vp9_highbd_sad8x16_bits8,
889 vp9_highbd_sad8x16_avg_bits8,
890 vp9_highbd_variance8x16,
891 vp9_highbd_sub_pixel_variance8x16,
892 vp9_highbd_sub_pixel_avg_variance8x16,
893 vp9_highbd_sad8x16x3_bits8,
894 vp9_highbd_sad8x16x8_bits8,
895 vp9_highbd_sad8x16x4d_bits8)
897 HIGHBD_BFP(BLOCK_8X8,
898 vp9_highbd_sad8x8_bits8,
899 vp9_highbd_sad8x8_avg_bits8,
900 vp9_highbd_variance8x8,
901 vp9_highbd_sub_pixel_variance8x8,
902 vp9_highbd_sub_pixel_avg_variance8x8,
903 vp9_highbd_sad8x8x3_bits8,
904 vp9_highbd_sad8x8x8_bits8,
905 vp9_highbd_sad8x8x4d_bits8)
907 HIGHBD_BFP(BLOCK_8X4,
908 vp9_highbd_sad8x4_bits8,
909 vp9_highbd_sad8x4_avg_bits8,
910 vp9_highbd_variance8x4,
911 vp9_highbd_sub_pixel_variance8x4,
912 vp9_highbd_sub_pixel_avg_variance8x4,
914 vp9_highbd_sad8x4x8_bits8,
915 vp9_highbd_sad8x4x4d_bits8)
917 HIGHBD_BFP(BLOCK_4X8,
918 vp9_highbd_sad4x8_bits8,
919 vp9_highbd_sad4x8_avg_bits8,
920 vp9_highbd_variance4x8,
921 vp9_highbd_sub_pixel_variance4x8,
922 vp9_highbd_sub_pixel_avg_variance4x8,
924 vp9_highbd_sad4x8x8_bits8,
925 vp9_highbd_sad4x8x4d_bits8)
927 HIGHBD_BFP(BLOCK_4X4,
928 vp9_highbd_sad4x4_bits8,
929 vp9_highbd_sad4x4_avg_bits8,
930 vp9_highbd_variance4x4,
931 vp9_highbd_sub_pixel_variance4x4,
932 vp9_highbd_sub_pixel_avg_variance4x4,
933 vp9_highbd_sad4x4x3_bits8,
934 vp9_highbd_sad4x4x8_bits8,
935 vp9_highbd_sad4x4x4d_bits8)
939 HIGHBD_BFP(BLOCK_32X16,
940 vp9_highbd_sad32x16_bits10,
941 vp9_highbd_sad32x16_avg_bits10,
942 vp9_highbd_10_variance32x16,
943 vp9_highbd_10_sub_pixel_variance32x16,
944 vp9_highbd_10_sub_pixel_avg_variance32x16,
947 vp9_highbd_sad32x16x4d_bits10)
949 HIGHBD_BFP(BLOCK_16X32,
950 vp9_highbd_sad16x32_bits10,
951 vp9_highbd_sad16x32_avg_bits10,
952 vp9_highbd_10_variance16x32,
953 vp9_highbd_10_sub_pixel_variance16x32,
954 vp9_highbd_10_sub_pixel_avg_variance16x32,
957 vp9_highbd_sad16x32x4d_bits10)
959 HIGHBD_BFP(BLOCK_64X32,
960 vp9_highbd_sad64x32_bits10,
961 vp9_highbd_sad64x32_avg_bits10,
962 vp9_highbd_10_variance64x32,
963 vp9_highbd_10_sub_pixel_variance64x32,
964 vp9_highbd_10_sub_pixel_avg_variance64x32,
967 vp9_highbd_sad64x32x4d_bits10)
969 HIGHBD_BFP(BLOCK_32X64,
970 vp9_highbd_sad32x64_bits10,
971 vp9_highbd_sad32x64_avg_bits10,
972 vp9_highbd_10_variance32x64,
973 vp9_highbd_10_sub_pixel_variance32x64,
974 vp9_highbd_10_sub_pixel_avg_variance32x64,
977 vp9_highbd_sad32x64x4d_bits10)
979 HIGHBD_BFP(BLOCK_32X32,
980 vp9_highbd_sad32x32_bits10,
981 vp9_highbd_sad32x32_avg_bits10,
982 vp9_highbd_10_variance32x32,
983 vp9_highbd_10_sub_pixel_variance32x32,
984 vp9_highbd_10_sub_pixel_avg_variance32x32,
985 vp9_highbd_sad32x32x3_bits10,
986 vp9_highbd_sad32x32x8_bits10,
987 vp9_highbd_sad32x32x4d_bits10)
989 HIGHBD_BFP(BLOCK_64X64,
990 vp9_highbd_sad64x64_bits10,
991 vp9_highbd_sad64x64_avg_bits10,
992 vp9_highbd_10_variance64x64,
993 vp9_highbd_10_sub_pixel_variance64x64,
994 vp9_highbd_10_sub_pixel_avg_variance64x64,
995 vp9_highbd_sad64x64x3_bits10,
996 vp9_highbd_sad64x64x8_bits10,
997 vp9_highbd_sad64x64x4d_bits10)
999 HIGHBD_BFP(BLOCK_16X16,
1000 vp9_highbd_sad16x16_bits10,
1001 vp9_highbd_sad16x16_avg_bits10,
1002 vp9_highbd_10_variance16x16,
1003 vp9_highbd_10_sub_pixel_variance16x16,
1004 vp9_highbd_10_sub_pixel_avg_variance16x16,
1005 vp9_highbd_sad16x16x3_bits10,
1006 vp9_highbd_sad16x16x8_bits10,
1007 vp9_highbd_sad16x16x4d_bits10)
1009 HIGHBD_BFP(BLOCK_16X8,
1010 vp9_highbd_sad16x8_bits10,
1011 vp9_highbd_sad16x8_avg_bits10,
1012 vp9_highbd_10_variance16x8,
1013 vp9_highbd_10_sub_pixel_variance16x8,
1014 vp9_highbd_10_sub_pixel_avg_variance16x8,
1015 vp9_highbd_sad16x8x3_bits10,
1016 vp9_highbd_sad16x8x8_bits10,
1017 vp9_highbd_sad16x8x4d_bits10)
1019 HIGHBD_BFP(BLOCK_8X16,
1020 vp9_highbd_sad8x16_bits10,
1021 vp9_highbd_sad8x16_avg_bits10,
1022 vp9_highbd_10_variance8x16,
1023 vp9_highbd_10_sub_pixel_variance8x16,
1024 vp9_highbd_10_sub_pixel_avg_variance8x16,
1025 vp9_highbd_sad8x16x3_bits10,
1026 vp9_highbd_sad8x16x8_bits10,
1027 vp9_highbd_sad8x16x4d_bits10)
1029 HIGHBD_BFP(BLOCK_8X8,
1030 vp9_highbd_sad8x8_bits10,
1031 vp9_highbd_sad8x8_avg_bits10,
1032 vp9_highbd_10_variance8x8,
1033 vp9_highbd_10_sub_pixel_variance8x8,
1034 vp9_highbd_10_sub_pixel_avg_variance8x8,
1035 vp9_highbd_sad8x8x3_bits10,
1036 vp9_highbd_sad8x8x8_bits10,
1037 vp9_highbd_sad8x8x4d_bits10)
1039 HIGHBD_BFP(BLOCK_8X4,
1040 vp9_highbd_sad8x4_bits10,
1041 vp9_highbd_sad8x4_avg_bits10,
1042 vp9_highbd_10_variance8x4,
1043 vp9_highbd_10_sub_pixel_variance8x4,
1044 vp9_highbd_10_sub_pixel_avg_variance8x4,
1046 vp9_highbd_sad8x4x8_bits10,
1047 vp9_highbd_sad8x4x4d_bits10)
1049 HIGHBD_BFP(BLOCK_4X8,
1050 vp9_highbd_sad4x8_bits10,
1051 vp9_highbd_sad4x8_avg_bits10,
1052 vp9_highbd_10_variance4x8,
1053 vp9_highbd_10_sub_pixel_variance4x8,
1054 vp9_highbd_10_sub_pixel_avg_variance4x8,
1056 vp9_highbd_sad4x8x8_bits10,
1057 vp9_highbd_sad4x8x4d_bits10)
1059 HIGHBD_BFP(BLOCK_4X4,
1060 vp9_highbd_sad4x4_bits10,
1061 vp9_highbd_sad4x4_avg_bits10,
1062 vp9_highbd_10_variance4x4,
1063 vp9_highbd_10_sub_pixel_variance4x4,
1064 vp9_highbd_10_sub_pixel_avg_variance4x4,
1065 vp9_highbd_sad4x4x3_bits10,
1066 vp9_highbd_sad4x4x8_bits10,
1067 vp9_highbd_sad4x4x4d_bits10)
1071 HIGHBD_BFP(BLOCK_32X16,
1072 vp9_highbd_sad32x16_bits12,
1073 vp9_highbd_sad32x16_avg_bits12,
1074 vp9_highbd_12_variance32x16,
1075 vp9_highbd_12_sub_pixel_variance32x16,
1076 vp9_highbd_12_sub_pixel_avg_variance32x16,
1079 vp9_highbd_sad32x16x4d_bits12)
1081 HIGHBD_BFP(BLOCK_16X32,
1082 vp9_highbd_sad16x32_bits12,
1083 vp9_highbd_sad16x32_avg_bits12,
1084 vp9_highbd_12_variance16x32,
1085 vp9_highbd_12_sub_pixel_variance16x32,
1086 vp9_highbd_12_sub_pixel_avg_variance16x32,
1089 vp9_highbd_sad16x32x4d_bits12)
1091 HIGHBD_BFP(BLOCK_64X32,
1092 vp9_highbd_sad64x32_bits12,
1093 vp9_highbd_sad64x32_avg_bits12,
1094 vp9_highbd_12_variance64x32,
1095 vp9_highbd_12_sub_pixel_variance64x32,
1096 vp9_highbd_12_sub_pixel_avg_variance64x32,
1099 vp9_highbd_sad64x32x4d_bits12)
1101 HIGHBD_BFP(BLOCK_32X64,
1102 vp9_highbd_sad32x64_bits12,
1103 vp9_highbd_sad32x64_avg_bits12,
1104 vp9_highbd_12_variance32x64,
1105 vp9_highbd_12_sub_pixel_variance32x64,
1106 vp9_highbd_12_sub_pixel_avg_variance32x64,
1109 vp9_highbd_sad32x64x4d_bits12)
1111 HIGHBD_BFP(BLOCK_32X32,
1112 vp9_highbd_sad32x32_bits12,
1113 vp9_highbd_sad32x32_avg_bits12,
1114 vp9_highbd_12_variance32x32,
1115 vp9_highbd_12_sub_pixel_variance32x32,
1116 vp9_highbd_12_sub_pixel_avg_variance32x32,
1117 vp9_highbd_sad32x32x3_bits12,
1118 vp9_highbd_sad32x32x8_bits12,
1119 vp9_highbd_sad32x32x4d_bits12)
1121 HIGHBD_BFP(BLOCK_64X64,
1122 vp9_highbd_sad64x64_bits12,
1123 vp9_highbd_sad64x64_avg_bits12,
1124 vp9_highbd_12_variance64x64,
1125 vp9_highbd_12_sub_pixel_variance64x64,
1126 vp9_highbd_12_sub_pixel_avg_variance64x64,
1127 vp9_highbd_sad64x64x3_bits12,
1128 vp9_highbd_sad64x64x8_bits12,
1129 vp9_highbd_sad64x64x4d_bits12)
1131 HIGHBD_BFP(BLOCK_16X16,
1132 vp9_highbd_sad16x16_bits12,
1133 vp9_highbd_sad16x16_avg_bits12,
1134 vp9_highbd_12_variance16x16,
1135 vp9_highbd_12_sub_pixel_variance16x16,
1136 vp9_highbd_12_sub_pixel_avg_variance16x16,
1137 vp9_highbd_sad16x16x3_bits12,
1138 vp9_highbd_sad16x16x8_bits12,
1139 vp9_highbd_sad16x16x4d_bits12)
1141 HIGHBD_BFP(BLOCK_16X8,
1142 vp9_highbd_sad16x8_bits12,
1143 vp9_highbd_sad16x8_avg_bits12,
1144 vp9_highbd_12_variance16x8,
1145 vp9_highbd_12_sub_pixel_variance16x8,
1146 vp9_highbd_12_sub_pixel_avg_variance16x8,
1147 vp9_highbd_sad16x8x3_bits12,
1148 vp9_highbd_sad16x8x8_bits12,
1149 vp9_highbd_sad16x8x4d_bits12)
1151 HIGHBD_BFP(BLOCK_8X16,
1152 vp9_highbd_sad8x16_bits12,
1153 vp9_highbd_sad8x16_avg_bits12,
1154 vp9_highbd_12_variance8x16,
1155 vp9_highbd_12_sub_pixel_variance8x16,
1156 vp9_highbd_12_sub_pixel_avg_variance8x16,
1157 vp9_highbd_sad8x16x3_bits12,
1158 vp9_highbd_sad8x16x8_bits12,
1159 vp9_highbd_sad8x16x4d_bits12)
1161 HIGHBD_BFP(BLOCK_8X8,
1162 vp9_highbd_sad8x8_bits12,
1163 vp9_highbd_sad8x8_avg_bits12,
1164 vp9_highbd_12_variance8x8,
1165 vp9_highbd_12_sub_pixel_variance8x8,
1166 vp9_highbd_12_sub_pixel_avg_variance8x8,
1167 vp9_highbd_sad8x8x3_bits12,
1168 vp9_highbd_sad8x8x8_bits12,
1169 vp9_highbd_sad8x8x4d_bits12)
1171 HIGHBD_BFP(BLOCK_8X4,
1172 vp9_highbd_sad8x4_bits12,
1173 vp9_highbd_sad8x4_avg_bits12,
1174 vp9_highbd_12_variance8x4,
1175 vp9_highbd_12_sub_pixel_variance8x4,
1176 vp9_highbd_12_sub_pixel_avg_variance8x4,
1178 vp9_highbd_sad8x4x8_bits12,
1179 vp9_highbd_sad8x4x4d_bits12)
1181 HIGHBD_BFP(BLOCK_4X8,
1182 vp9_highbd_sad4x8_bits12,
1183 vp9_highbd_sad4x8_avg_bits12,
1184 vp9_highbd_12_variance4x8,
1185 vp9_highbd_12_sub_pixel_variance4x8,
1186 vp9_highbd_12_sub_pixel_avg_variance4x8,
1188 vp9_highbd_sad4x8x8_bits12,
1189 vp9_highbd_sad4x8x4d_bits12)
1191 HIGHBD_BFP(BLOCK_4X4,
1192 vp9_highbd_sad4x4_bits12,
1193 vp9_highbd_sad4x4_avg_bits12,
1194 vp9_highbd_12_variance4x4,
1195 vp9_highbd_12_sub_pixel_variance4x4,
1196 vp9_highbd_12_sub_pixel_avg_variance4x4,
1197 vp9_highbd_sad4x4x3_bits12,
1198 vp9_highbd_sad4x4x8_bits12,
1199 vp9_highbd_sad4x4x4d_bits12)
1203 assert(0 && "cm->bit_depth should be VPX_BITS_8, "
1204 "VPX_BITS_10 or VPX_BITS_12");
1208 #endif // CONFIG_VP9_HIGHBITDEPTH
1210 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
1211 VP9_COMMON *const cm = &cpi->common;
1212 RATE_CONTROL *const rc = &cpi->rc;
1214 if (cm->profile != oxcf->profile)
1215 cm->profile = oxcf->profile;
1216 cm->bit_depth = oxcf->bit_depth;
1218 if (cm->profile <= PROFILE_1)
1219 assert(cm->bit_depth == VPX_BITS_8);
1221 assert(cm->bit_depth > VPX_BITS_8);
1224 #if CONFIG_VP9_HIGHBITDEPTH
1225 cpi->mb.e_mbd.bd = (int)cm->bit_depth;
1226 #endif // CONFIG_VP9_HIGHBITDEPTH
1228 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1230 cpi->refresh_golden_frame = 0;
1231 cpi->refresh_last_frame = 1;
1232 cm->refresh_frame_context = 1;
1233 cm->reset_frame_context = 0;
1235 vp9_reset_segment_features(&cm->seg);
1236 vp9_set_high_precision_mv(cpi, 0);
1241 for (i = 0; i < MAX_SEGMENTS; i++)
1242 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1244 cpi->encode_breakout = cpi->oxcf.encode_breakout;
1246 set_rc_buffer_sizes(rc, &cpi->oxcf);
1248 // Under a configuration change, where maximum_buffer_size may change,
1249 // keep buffer level clipped to the maximum allowed buffer size.
1250 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
1251 rc->buffer_level = MIN(rc->buffer_level, rc->maximum_buffer_size);
1253 // Set up frame rate and related parameters rate control values.
1254 vp9_new_framerate(cpi, cpi->framerate);
1256 // Set absolute upper and lower quality limits
1257 rc->worst_quality = cpi->oxcf.worst_allowed_q;
1258 rc->best_quality = cpi->oxcf.best_allowed_q;
1260 cm->interp_filter = cpi->sf.default_interp_filter;
1262 cm->display_width = cpi->oxcf.width;
1263 cm->display_height = cpi->oxcf.height;
1265 if (cpi->initial_width) {
1266 // Increasing the size of the frame beyond the first seen frame, or some
1267 // otherwise signaled maximum size, is not supported.
1268 // TODO(jkoleszar): exit gracefully.
1269 assert(cm->width <= cpi->initial_width);
1270 assert(cm->height <= cpi->initial_height);
1272 update_frame_size(cpi);
1274 if ((cpi->svc.number_temporal_layers > 1 &&
1275 cpi->oxcf.rc_mode == VPX_CBR) ||
1276 ((cpi->svc.number_temporal_layers > 1 ||
1277 cpi->svc.number_spatial_layers > 1) &&
1278 cpi->oxcf.pass == 2)) {
1279 vp9_update_layer_context_change_config(cpi,
1280 (int)cpi->oxcf.target_bandwidth);
1283 cpi->alt_ref_source = NULL;
1284 rc->is_src_frame_alt_ref = 0;
1287 // Experimental RD Code
1288 cpi->frame_distortion = 0;
1289 cpi->last_frame_distortion = 0;
1292 set_tile_limits(cpi);
1294 cpi->ext_refresh_frame_flags_pending = 0;
1295 cpi->ext_refresh_frame_context_pending = 0;
1297 #if CONFIG_VP9_HIGHBITDEPTH
1298 highbd_set_var_fns(cpi);
1301 #if CONFIG_VP9_TEMPORAL_DENOISING
1302 if (cpi->oxcf.noise_sensitivity > 0) {
1303 vp9_denoiser_alloc(&(cpi->denoiser), cm->width, cm->height,
1304 cm->subsampling_x, cm->subsampling_y,
1305 #if CONFIG_VP9_HIGHBITDEPTH
1306 cm->use_highbitdepth,
1308 VP9_ENC_BORDER_IN_PIXELS);
1314 #define M_LOG2_E 0.693147180559945309417
1316 #define log2f(x) (log (x) / (float) M_LOG2_E)
1318 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1319 mvjointsadcost[0] = 600;
1320 mvjointsadcost[1] = 300;
1321 mvjointsadcost[2] = 300;
1322 mvjointsadcost[3] = 300;
1325 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1328 mvsadcost[0][0] = 0;
1329 mvsadcost[1][0] = 0;
1332 double z = 256 * (2 * (log2f(8 * i) + .6));
1333 mvsadcost[0][i] = (int)z;
1334 mvsadcost[1][i] = (int)z;
1335 mvsadcost[0][-i] = (int)z;
1336 mvsadcost[1][-i] = (int)z;
1337 } while (++i <= MV_MAX);
1340 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1343 mvsadcost[0][0] = 0;
1344 mvsadcost[1][0] = 0;
1347 double z = 256 * (2 * (log2f(8 * i) + .6));
1348 mvsadcost[0][i] = (int)z;
1349 mvsadcost[1][i] = (int)z;
1350 mvsadcost[0][-i] = (int)z;
1351 mvsadcost[1][-i] = (int)z;
1352 } while (++i <= MV_MAX);
1356 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf) {
1358 VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP));
1359 VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL;
1366 if (setjmp(cm->error.jmp)) {
1367 cm->error.setjmp = 0;
1368 vp9_remove_compressor(cpi);
1372 cm->error.setjmp = 1;
1376 init_config(cpi, oxcf);
1377 vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
1379 cm->current_video_frame = 0;
1380 cpi->partition_search_skippable_frame = 0;
1382 // Create the encoder segmentation map and set all entries to 0
1383 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1384 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1386 // Create a complexity map used for rd adjustment
1387 CHECK_MEM_ERROR(cm, cpi->complexity_map,
1388 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1390 // Create a map used for cyclic background refresh.
1391 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
1392 vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
1394 // And a place holder structure is the coding context
1395 // for use if we want to save and restore it
1396 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1397 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1399 CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
1400 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
1401 CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
1402 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
1403 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
1404 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
1405 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
1406 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
1407 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
1408 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
1409 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
1410 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
1411 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
1412 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
1413 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
1414 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
1416 for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
1417 sizeof(cpi->mbgraph_stats[0])); i++) {
1418 CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
1419 vpx_calloc(cm->MBs *
1420 sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1423 #if CONFIG_FP_MB_STATS
1424 cpi->use_fp_mb_stats = 0;
1425 if (cpi->use_fp_mb_stats) {
1426 // a place holder used to store the first pass mb stats in the first pass
1427 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
1428 vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
1430 cpi->twopass.frame_mb_stats_buf = NULL;
1434 cpi->refresh_alt_ref_frame = 0;
1436 // Note that at the moment multi_arf will not work with svc.
1437 // For the current check in all the execution paths are defaulted to 0
1438 // pending further tuning and testing. The code is left in place here
1439 // as a place holder in regard to the required paths.
1440 cpi->multi_arf_last_grp_enabled = 0;
1441 if (oxcf->pass == 2) {
1443 cpi->multi_arf_allowed = 0;
1444 cpi->multi_arf_enabled = 0;
1446 // Disable by default for now.
1447 cpi->multi_arf_allowed = 0;
1448 cpi->multi_arf_enabled = 0;
1451 cpi->multi_arf_allowed = 0;
1452 cpi->multi_arf_enabled = 0;
1455 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1456 #if CONFIG_INTERNAL_STATS
1457 cpi->b_calculate_ssimg = 0;
1462 if (cpi->b_calculate_psnr) {
1467 cpi->total_sq_error = 0;
1468 cpi->total_samples = 0;
1470 cpi->totalp_y = 0.0;
1471 cpi->totalp_u = 0.0;
1472 cpi->totalp_v = 0.0;
1474 cpi->totalp_sq_error = 0;
1475 cpi->totalp_samples = 0;
1477 cpi->tot_recode_hits = 0;
1478 cpi->summed_quality = 0;
1479 cpi->summed_weights = 0;
1480 cpi->summedp_quality = 0;
1481 cpi->summedp_weights = 0;
1484 if (cpi->b_calculate_ssimg) {
1485 cpi->total_ssimg_y = 0;
1486 cpi->total_ssimg_u = 0;
1487 cpi->total_ssimg_v = 0;
1488 cpi->total_ssimg_all = 0;
1493 cpi->first_time_stamp_ever = INT64_MAX;
1495 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
1496 cpi->mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
1497 cpi->mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
1498 cpi->mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
1499 cpi->mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
1500 cal_nmvsadcosts(cpi->mb.nmvsadcost);
1502 cpi->mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
1503 cpi->mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
1504 cpi->mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
1505 cpi->mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
1506 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
1508 #if CONFIG_VP9_TEMPORAL_DENOISING
1509 #ifdef OUTPUT_YUV_DENOISED
1510 yuv_denoised_file = fopen("denoised.yuv", "ab");
1513 #ifdef OUTPUT_YUV_REC
1514 yuv_rec_file = fopen("rec.yuv", "wb");
1518 framepsnr = fopen("framepsnr.stt", "a");
1519 kf_list = fopen("kf_list.stt", "w");
1522 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
1524 if (oxcf->pass == 1) {
1525 vp9_init_first_pass(cpi);
1526 } else if (oxcf->pass == 2) {
1527 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
1528 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1530 if (cpi->svc.number_spatial_layers > 1
1531 || cpi->svc.number_temporal_layers > 1) {
1532 FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
1533 FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0};
1536 for (i = 0; i < oxcf->ss_number_layers; ++i) {
1537 FIRSTPASS_STATS *const last_packet_for_layer =
1538 &stats[packets - oxcf->ss_number_layers + i];
1539 const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
1540 const int packets_in_layer = (int)last_packet_for_layer->count + 1;
1541 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
1542 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
1544 vpx_free(lc->rc_twopass_stats_in.buf);
1546 lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
1547 CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
1548 vpx_malloc(lc->rc_twopass_stats_in.sz));
1549 lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
1550 lc->twopass.stats_in = lc->twopass.stats_in_start;
1551 lc->twopass.stats_in_end = lc->twopass.stats_in_start
1552 + packets_in_layer - 1;
1553 stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
1557 for (i = 0; i < packets; ++i) {
1558 const int layer_id = (int)stats[i].spatial_layer_id;
1559 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers
1560 && stats_copy[layer_id] != NULL) {
1561 *stats_copy[layer_id] = stats[i];
1562 ++stats_copy[layer_id];
1566 vp9_init_second_pass_spatial_svc(cpi);
1568 #if CONFIG_FP_MB_STATS
1569 if (cpi->use_fp_mb_stats) {
1570 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
1571 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
1573 cpi->twopass.firstpass_mb_stats.mb_stats_start =
1574 oxcf->firstpass_mb_stats_in.buf;
1575 cpi->twopass.firstpass_mb_stats.mb_stats_end =
1576 cpi->twopass.firstpass_mb_stats.mb_stats_start +
1577 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
1581 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1582 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1583 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
1585 vp9_init_second_pass(cpi);
1589 vp9_set_speed_features(cpi);
1591 // Allocate memory to store variances for a frame.
1592 CHECK_MEM_ERROR(cm, cpi->source_diff_var,
1593 vpx_calloc(cm->MBs, sizeof(diff)));
1594 cpi->source_var_thresh = 0;
1595 cpi->frames_till_next_var_check = 0;
1597 // Default rd threshold factors for mode selection
1598 for (i = 0; i < BLOCK_SIZES; ++i) {
1599 for (j = 0; j < MAX_MODES; ++j) {
1600 cpi->rd.thresh_freq_fact[i][j] = 32;
1601 cpi->rd.mode_map[i][j] = j;
1605 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF)\
1606 cpi->fn_ptr[BT].sdf = SDF; \
1607 cpi->fn_ptr[BT].sdaf = SDAF; \
1608 cpi->fn_ptr[BT].vf = VF; \
1609 cpi->fn_ptr[BT].svf = SVF; \
1610 cpi->fn_ptr[BT].svaf = SVAF; \
1611 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1612 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1613 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1615 BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
1616 vp9_variance32x16, vp9_sub_pixel_variance32x16,
1617 vp9_sub_pixel_avg_variance32x16, NULL, NULL, vp9_sad32x16x4d)
1619 BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
1620 vp9_variance16x32, vp9_sub_pixel_variance16x32,
1621 vp9_sub_pixel_avg_variance16x32, NULL, NULL, vp9_sad16x32x4d)
1623 BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
1624 vp9_variance64x32, vp9_sub_pixel_variance64x32,
1625 vp9_sub_pixel_avg_variance64x32, NULL, NULL, vp9_sad64x32x4d)
1627 BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
1628 vp9_variance32x64, vp9_sub_pixel_variance32x64,
1629 vp9_sub_pixel_avg_variance32x64, NULL, NULL, vp9_sad32x64x4d)
1631 BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
1632 vp9_variance32x32, vp9_sub_pixel_variance32x32,
1633 vp9_sub_pixel_avg_variance32x32, vp9_sad32x32x3, vp9_sad32x32x8,
1636 BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
1637 vp9_variance64x64, vp9_sub_pixel_variance64x64,
1638 vp9_sub_pixel_avg_variance64x64, vp9_sad64x64x3, vp9_sad64x64x8,
1641 BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
1642 vp9_variance16x16, vp9_sub_pixel_variance16x16,
1643 vp9_sub_pixel_avg_variance16x16, vp9_sad16x16x3, vp9_sad16x16x8,
1646 BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
1647 vp9_variance16x8, vp9_sub_pixel_variance16x8,
1648 vp9_sub_pixel_avg_variance16x8,
1649 vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
1651 BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
1652 vp9_variance8x16, vp9_sub_pixel_variance8x16,
1653 vp9_sub_pixel_avg_variance8x16,
1654 vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
1656 BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
1657 vp9_variance8x8, vp9_sub_pixel_variance8x8,
1658 vp9_sub_pixel_avg_variance8x8,
1659 vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
1661 BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
1662 vp9_variance8x4, vp9_sub_pixel_variance8x4,
1663 vp9_sub_pixel_avg_variance8x4, NULL, vp9_sad8x4x8, vp9_sad8x4x4d)
1665 BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
1666 vp9_variance4x8, vp9_sub_pixel_variance4x8,
1667 vp9_sub_pixel_avg_variance4x8, NULL, vp9_sad4x8x8, vp9_sad4x8x4d)
1669 BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
1670 vp9_variance4x4, vp9_sub_pixel_variance4x4,
1671 vp9_sub_pixel_avg_variance4x4,
1672 vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
1674 #if CONFIG_VP9_HIGHBITDEPTH
1675 highbd_set_var_fns(cpi);
1678 /* vp9_init_quantizer() is first called here. Add check in
1679 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1680 * called later when needed. This will avoid unnecessary calls of
1681 * vp9_init_quantizer() for every frame.
1683 vp9_init_quantizer(cpi);
1685 vp9_loop_filter_init(cm);
1687 cm->error.setjmp = 0;
1692 void vp9_remove_compressor(VP9_COMP *cpi) {
1698 if (cpi && (cpi->common.current_video_frame > 0)) {
1699 #if CONFIG_INTERNAL_STATS
1701 vp9_clear_system_state();
1703 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1704 if (cpi->oxcf.pass != 1) {
1705 FILE *f = fopen("opsnr.stt", "a");
1706 double time_encoded = (cpi->last_end_time_stamp_seen
1707 - cpi->first_time_stamp_ever) / 10000000.000;
1708 double total_encode_time = (cpi->time_receive_data +
1709 cpi->time_compress_data) / 1000.000;
1711 (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
1712 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
1714 if (cpi->b_calculate_psnr) {
1715 const double total_psnr =
1716 vpx_sse_to_psnr((double)cpi->total_samples, peak,
1717 (double)cpi->total_sq_error);
1718 const double totalp_psnr =
1719 vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
1720 (double)cpi->totalp_sq_error);
1721 const double total_ssim = 100 * pow(cpi->summed_quality /
1722 cpi->summed_weights, 8.0);
1723 const double totalp_ssim = 100 * pow(cpi->summedp_quality /
1724 cpi->summedp_weights, 8.0);
1726 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1727 "VPXSSIM\tVPSSIMP\t Time(ms)\n");
1728 fprintf(f, "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f\n",
1729 dr, cpi->total / cpi->count, total_psnr,
1730 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
1734 if (cpi->b_calculate_ssimg) {
1735 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n");
1736 fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
1737 cpi->total_ssimg_y / cpi->count,
1738 cpi->total_ssimg_u / cpi->count,
1739 cpi->total_ssimg_v / cpi->count,
1740 cpi->total_ssimg_all / cpi->count, total_encode_time);
1750 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
1751 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
1752 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
1753 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
1754 cpi->time_compress_data / 1000,
1755 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
1760 #if CONFIG_VP9_TEMPORAL_DENOISING
1761 if (cpi->oxcf.noise_sensitivity > 0) {
1762 vp9_denoiser_free(&(cpi->denoiser));
1766 dealloc_compressor_data(cpi);
1769 for (i = 0; i < sizeof(cpi->mbgraph_stats) /
1770 sizeof(cpi->mbgraph_stats[0]); ++i) {
1771 vpx_free(cpi->mbgraph_stats[i].mb_stats);
1774 #if CONFIG_FP_MB_STATS
1775 if (cpi->use_fp_mb_stats) {
1776 vpx_free(cpi->twopass.frame_mb_stats_buf);
1777 cpi->twopass.frame_mb_stats_buf = NULL;
1781 vp9_remove_common(&cpi->common);
1784 #if CONFIG_VP9_TEMPORAL_DENOISING
1785 #ifdef OUTPUT_YUV_DENOISED
1786 fclose(yuv_denoised_file);
1789 #ifdef OUTPUT_YUV_REC
1790 fclose(yuv_rec_file);
1807 static int64_t get_sse(const uint8_t *a, int a_stride,
1808 const uint8_t *b, int b_stride,
1809 int width, int height) {
1810 const int dw = width % 16;
1811 const int dh = height % 16;
1812 int64_t total_sse = 0;
1813 unsigned int sse = 0;
1818 variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
1819 dw, height, &sse, &sum);
1824 variance(&a[(height - dh) * a_stride], a_stride,
1825 &b[(height - dh) * b_stride], b_stride,
1826 width - dw, dh, &sse, &sum);
1830 for (y = 0; y < height / 16; ++y) {
1831 const uint8_t *pa = a;
1832 const uint8_t *pb = b;
1833 for (x = 0; x < width / 16; ++x) {
1834 vp9_mse16x16(pa, a_stride, pb, b_stride, &sse);
1848 #if CONFIG_VP9_HIGHBITDEPTH
1849 static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
1850 const uint8_t *b8, int b_stride,
1851 int width, int height,
1852 unsigned int input_shift) {
1853 const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
1854 const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
1855 int64_t total_sse = 0;
1857 for (y = 0; y < height; ++y) {
1858 for (x = 0; x < width; ++x) {
1860 diff = (a[x] >> input_shift) - (b[x] >> input_shift);
1861 total_sse += diff * diff;
1869 static int64_t highbd_get_sse(const uint8_t *a, int a_stride,
1870 const uint8_t *b, int b_stride,
1871 int width, int height) {
1872 int64_t total_sse = 0;
1874 const int dw = width % 16;
1875 const int dh = height % 16;
1876 unsigned int sse = 0;
1879 highbd_variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
1880 dw, height, &sse, &sum);
1884 highbd_variance(&a[(height - dh) * a_stride], a_stride,
1885 &b[(height - dh) * b_stride], b_stride,
1886 width - dw, dh, &sse, &sum);
1889 for (y = 0; y < height / 16; ++y) {
1890 const uint8_t *pa = a;
1891 const uint8_t *pb = b;
1892 for (x = 0; x < width / 16; ++x) {
1893 vp9_highbd_mse16x16(pa, a_stride, pb, b_stride, &sse);
1903 #endif // CONFIG_VP9_HIGHBITDEPTH
1906 double psnr[4]; // total/y/u/v
1907 uint64_t sse[4]; // total/y/u/v
1908 uint32_t samples[4]; // total/y/u/v
1911 static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
1913 static const double peak = 255.0;
1914 const int widths[3] = {a->y_width, a->uv_width, a->uv_width };
1915 const int heights[3] = {a->y_height, a->uv_height, a->uv_height};
1916 const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer, a->v_buffer };
1917 const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride};
1918 const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer, b->v_buffer };
1919 const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride};
1921 uint64_t total_sse = 0;
1922 uint32_t total_samples = 0;
1924 for (i = 0; i < 3; ++i) {
1925 const int w = widths[i];
1926 const int h = heights[i];
1927 const uint32_t samples = w * h;
1928 const uint64_t sse = get_sse(a_planes[i], a_strides[i],
1929 b_planes[i], b_strides[i],
1931 psnr->sse[1 + i] = sse;
1932 psnr->samples[1 + i] = samples;
1933 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
1936 total_samples += samples;
1939 psnr->sse[0] = total_sse;
1940 psnr->samples[0] = total_samples;
1941 psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, peak,
1945 #if CONFIG_VP9_HIGHBITDEPTH
1946 static void calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
1947 const YV12_BUFFER_CONFIG *b,
1949 unsigned int bit_depth,
1950 unsigned int in_bit_depth) {
1951 const int widths[3] = {a->y_width, a->uv_width, a->uv_width };
1952 const int heights[3] = {a->y_height, a->uv_height, a->uv_height};
1953 const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer, a->v_buffer };
1954 const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride};
1955 const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer, b->v_buffer };
1956 const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride};
1958 uint64_t total_sse = 0;
1959 uint32_t total_samples = 0;
1960 const double peak = (double)((1 << in_bit_depth) - 1);
1961 const unsigned int input_shift = bit_depth - in_bit_depth;
1963 for (i = 0; i < 3; ++i) {
1964 const int w = widths[i];
1965 const int h = heights[i];
1966 const uint32_t samples = w * h;
1968 if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
1970 sse = highbd_get_sse_shift(a_planes[i], a_strides[i],
1971 b_planes[i], b_strides[i], w, h,
1974 sse = highbd_get_sse(a_planes[i], a_strides[i],
1975 b_planes[i], b_strides[i], w, h);
1978 sse = get_sse(a_planes[i], a_strides[i],
1979 b_planes[i], b_strides[i],
1982 psnr->sse[1 + i] = sse;
1983 psnr->samples[1 + i] = samples;
1984 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
1987 total_samples += samples;
1990 psnr->sse[0] = total_sse;
1991 psnr->samples[0] = total_samples;
1992 psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, peak,
1995 #endif // CONFIG_VP9_HIGHBITDEPTH
1997 static void generate_psnr_packet(VP9_COMP *cpi) {
1998 struct vpx_codec_cx_pkt pkt;
2001 #if CONFIG_VP9_HIGHBITDEPTH
2002 calc_highbd_psnr(cpi->Source, cpi->common.frame_to_show, &psnr,
2003 cpi->mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2005 calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
2008 for (i = 0; i < 4; ++i) {
2009 pkt.data.psnr.samples[i] = psnr.samples[i];
2010 pkt.data.psnr.sse[i] = psnr.sse[i];
2011 pkt.data.psnr.psnr[i] = psnr.psnr[i];
2013 pkt.kind = VPX_CODEC_PSNR_PKT;
2014 if (is_two_pass_svc(cpi))
2015 cpi->svc.layer_context[cpi->svc.spatial_layer_id].psnr_pkt = pkt.data.psnr;
2017 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2020 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
2021 if (ref_frame_flags > 7)
2024 cpi->ref_frame_flags = ref_frame_flags;
2028 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
2029 cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
2030 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
2031 cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
2032 cpi->ext_refresh_frame_flags_pending = 1;
2035 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
2036 VP9_REFFRAME ref_frame_flag) {
2037 MV_REFERENCE_FRAME ref_frame = NONE;
2038 if (ref_frame_flag == VP9_LAST_FLAG)
2039 ref_frame = LAST_FRAME;
2040 else if (ref_frame_flag == VP9_GOLD_FLAG)
2041 ref_frame = GOLDEN_FRAME;
2042 else if (ref_frame_flag == VP9_ALT_FLAG)
2043 ref_frame = ALTREF_FRAME;
2045 return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2048 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2049 YV12_BUFFER_CONFIG *sd) {
2050 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2052 vp8_yv12_copy_frame(cfg, sd);
2059 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2060 YV12_BUFFER_CONFIG *sd) {
2061 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2063 vp8_yv12_copy_frame(sd, cfg);
2070 int vp9_update_entropy(VP9_COMP * cpi, int update) {
2071 cpi->ext_refresh_frame_context = update;
2072 cpi->ext_refresh_frame_context_pending = 1;
2076 #if CONFIG_VP9_TEMPORAL_DENOISING
2077 #if defined(OUTPUT_YUV_DENOISED)
2078 // The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
2079 // as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
2080 // not denoise the UV channels at this time. If ever we implement UV channel
2081 // denoising we will have to modify this.
2082 void vp9_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
2083 uint8_t *src = s->y_buffer;
2084 int h = s->y_height;
2087 fwrite(src, s->y_width, 1, f);
2092 h = s->uv_height / 2;
2095 fwrite(src, s->uv_width / 2, 1, f);
2096 src += s->uv_stride + s->uv_width / 2;
2100 h = s->uv_height / 2;
2103 fwrite(src, s->uv_width / 2, 1, f);
2104 src += s->uv_stride + s->uv_width / 2;
2110 #ifdef OUTPUT_YUV_REC
2111 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2112 YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2113 uint8_t *src = s->y_buffer;
2116 #if CONFIG_VP9_HIGHBITDEPTH
2117 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
2118 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
2121 fwrite(src16, s->y_width, 2, yuv_rec_file);
2122 src16 += s->y_stride;
2125 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
2129 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2130 src16 += s->uv_stride;
2133 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
2137 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2138 src16 += s->uv_stride;
2141 fflush(yuv_rec_file);
2144 #endif // CONFIG_VP9_HIGHBITDEPTH
2147 fwrite(src, s->y_width, 1, yuv_rec_file);
2155 fwrite(src, s->uv_width, 1, yuv_rec_file);
2156 src += s->uv_stride;
2163 fwrite(src, s->uv_width, 1, yuv_rec_file);
2164 src += s->uv_stride;
2167 fflush(yuv_rec_file);
2171 #if CONFIG_VP9_HIGHBITDEPTH
2172 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2173 YV12_BUFFER_CONFIG *dst,
2176 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2177 YV12_BUFFER_CONFIG *dst) {
2178 #endif // CONFIG_VP9_HIGHBITDEPTH
2179 // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
2181 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
2182 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
2183 const int src_widths[3] = {src->y_crop_width, src->uv_crop_width,
2184 src->uv_crop_width };
2185 const int src_heights[3] = {src->y_crop_height, src->uv_crop_height,
2186 src->uv_crop_height};
2187 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
2188 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
2189 const int dst_widths[3] = {dst->y_crop_width, dst->uv_crop_width,
2190 dst->uv_crop_width};
2191 const int dst_heights[3] = {dst->y_crop_height, dst->uv_crop_height,
2192 dst->uv_crop_height};
2194 for (i = 0; i < MAX_MB_PLANE; ++i) {
2195 #if CONFIG_VP9_HIGHBITDEPTH
2196 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2197 vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
2198 src_strides[i], dsts[i], dst_heights[i],
2199 dst_widths[i], dst_strides[i], bd);
2201 vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2202 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2205 vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2206 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2207 #endif // CONFIG_VP9_HIGHBITDEPTH
2209 vp9_extend_frame_borders(dst);
2212 #if CONFIG_VP9_HIGHBITDEPTH
2213 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2214 YV12_BUFFER_CONFIG *dst, int bd) {
2216 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2217 YV12_BUFFER_CONFIG *dst) {
2218 #endif // CONFIG_VP9_HIGHBITDEPTH
2219 const int src_w = src->y_crop_width;
2220 const int src_h = src->y_crop_height;
2221 const int dst_w = dst->y_crop_width;
2222 const int dst_h = dst->y_crop_height;
2223 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
2224 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
2225 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
2226 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
2227 const InterpKernel *const kernel = vp9_get_interp_kernel(EIGHTTAP);
2230 for (y = 0; y < dst_h; y += 16) {
2231 for (x = 0; x < dst_w; x += 16) {
2232 for (i = 0; i < MAX_MB_PLANE; ++i) {
2233 const int factor = (i == 0 || i == 3 ? 1 : 2);
2234 const int x_q4 = x * (16 / factor) * src_w / dst_w;
2235 const int y_q4 = y * (16 / factor) * src_h / dst_h;
2236 const int src_stride = src_strides[i];
2237 const int dst_stride = dst_strides[i];
2238 const uint8_t *src_ptr = srcs[i] + (y / factor) * src_h / dst_h *
2239 src_stride + (x / factor) * src_w / dst_w;
2240 uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
2242 #if CONFIG_VP9_HIGHBITDEPTH
2243 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2244 vp9_highbd_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2245 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2246 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2247 16 / factor, 16 / factor, bd);
2249 vp9_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2250 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2251 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2252 16 / factor, 16 / factor);
2255 vp9_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2256 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2257 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2258 16 / factor, 16 / factor);
2259 #endif // CONFIG_VP9_HIGHBITDEPTH
2264 vp9_extend_frame_borders(dst);
2267 // Function to test for conditions that indicate we should loop
2268 // back and recode a frame.
2269 static int recode_loop_test(const VP9_COMP *cpi,
2270 int high_limit, int low_limit,
2271 int q, int maxq, int minq) {
2272 const VP9_COMMON *const cm = &cpi->common;
2273 const RATE_CONTROL *const rc = &cpi->rc;
2274 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2275 int force_recode = 0;
2277 // Special case trap if maximum allowed frame size exceeded.
2278 if (rc->projected_frame_size > rc->max_frame_bandwidth) {
2281 // Is frame recode allowed.
2282 // Yes if either recode mode 1 is selected or mode 2 is selected
2283 // and the frame is a key frame, golden frame or alt_ref_frame
2284 } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
2285 ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
2286 (cm->frame_type == KEY_FRAME ||
2287 cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
2288 // General over and under shoot tests
2289 if ((rc->projected_frame_size > high_limit && q < maxq) ||
2290 (rc->projected_frame_size < low_limit && q > minq)) {
2292 } else if (cpi->oxcf.rc_mode == VPX_CQ) {
2293 // Deal with frame undershoot and whether or not we are
2294 // below the automatically set cq level.
2295 if (q > oxcf->cq_level &&
2296 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2301 return force_recode;
2304 void vp9_update_reference_frames(VP9_COMP *cpi) {
2305 VP9_COMMON * const cm = &cpi->common;
2307 // At this point the new frame has been encoded.
2308 // If any buffer copy / swapping is signaled it should be done here.
2309 if (cm->frame_type == KEY_FRAME) {
2310 ref_cnt_fb(cm->frame_bufs,
2311 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2312 ref_cnt_fb(cm->frame_bufs,
2313 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2314 } else if (vp9_preserve_existing_gf(cpi)) {
2315 // We have decided to preserve the previously existing golden frame as our
2316 // new ARF frame. However, in the short term in function
2317 // vp9_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
2318 // we're updating the GF with the current decoded frame, we save it to the
2319 // ARF slot instead.
2320 // We now have to update the ARF with the current frame and swap gld_fb_idx
2321 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
2322 // slot and, if we're updating the GF, the current frame becomes the new GF.
2325 ref_cnt_fb(cm->frame_bufs,
2326 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2328 tmp = cpi->alt_fb_idx;
2329 cpi->alt_fb_idx = cpi->gld_fb_idx;
2330 cpi->gld_fb_idx = tmp;
2332 if (is_two_pass_svc(cpi)) {
2333 cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
2334 cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
2336 } else { /* For non key/golden frames */
2337 if (cpi->refresh_alt_ref_frame) {
2338 int arf_idx = cpi->alt_fb_idx;
2339 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
2340 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2341 arf_idx = gf_group->arf_update_idx[gf_group->index];
2344 ref_cnt_fb(cm->frame_bufs,
2345 &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2346 vpx_memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
2347 cpi->interp_filter_selected[0],
2348 sizeof(cpi->interp_filter_selected[0]));
2351 if (cpi->refresh_golden_frame) {
2352 ref_cnt_fb(cm->frame_bufs,
2353 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2354 if (!cpi->rc.is_src_frame_alt_ref)
2355 vpx_memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2356 cpi->interp_filter_selected[0],
2357 sizeof(cpi->interp_filter_selected[0]));
2359 vpx_memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2360 cpi->interp_filter_selected[ALTREF_FRAME],
2361 sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
2365 if (cpi->refresh_last_frame) {
2366 ref_cnt_fb(cm->frame_bufs,
2367 &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
2368 if (!cpi->rc.is_src_frame_alt_ref)
2369 vpx_memcpy(cpi->interp_filter_selected[LAST_FRAME],
2370 cpi->interp_filter_selected[0],
2371 sizeof(cpi->interp_filter_selected[0]));
2373 #if CONFIG_VP9_TEMPORAL_DENOISING
2374 if (cpi->oxcf.noise_sensitivity > 0) {
2375 vp9_denoiser_update_frame_info(&cpi->denoiser,
2377 cpi->common.frame_type,
2378 cpi->refresh_alt_ref_frame,
2379 cpi->refresh_golden_frame,
2380 cpi->refresh_last_frame);
2385 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2386 MACROBLOCKD *xd = &cpi->mb.e_mbd;
2387 struct loopfilter *lf = &cm->lf;
2389 lf->filter_level = 0;
2391 struct vpx_usec_timer timer;
2393 vp9_clear_system_state();
2395 vpx_usec_timer_start(&timer);
2397 vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
2399 vpx_usec_timer_mark(&timer);
2400 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2403 if (lf->filter_level > 0) {
2404 vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
2407 vp9_extend_frame_inner_borders(cm->frame_to_show);
2410 void vp9_scale_references(VP9_COMP *cpi) {
2411 VP9_COMMON *cm = &cpi->common;
2412 MV_REFERENCE_FRAME ref_frame;
2413 const VP9_REFFRAME ref_mask[3] = {VP9_LAST_FLAG, VP9_GOLD_FLAG, VP9_ALT_FLAG};
2415 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2416 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2417 const YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf;
2419 // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
2420 if ((cpi->ref_frame_flags & ref_mask[ref_frame - 1]) &&
2421 (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)) {
2422 const int new_fb = get_free_fb(cm);
2423 vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
2424 cm->width, cm->height,
2425 cm->subsampling_x, cm->subsampling_y,
2426 #if CONFIG_VP9_HIGHBITDEPTH
2427 cm->use_highbitdepth,
2428 #endif // CONFIG_VP9_HIGHBITDEPTH
2429 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2430 #if CONFIG_VP9_HIGHBITDEPTH
2431 scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf,
2432 (int)cm->bit_depth);
2434 scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf);
2435 #endif // CONFIG_VP9_HIGHBITDEPTH
2436 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2438 cpi->scaled_ref_idx[ref_frame - 1] = idx;
2439 cm->frame_bufs[idx].ref_count++;
2444 static void release_scaled_references(VP9_COMP *cpi) {
2445 VP9_COMMON *cm = &cpi->common;
2448 for (i = 0; i < 3; i++)
2449 cm->frame_bufs[cpi->scaled_ref_idx[i]].ref_count--;
2452 static void full_to_model_count(unsigned int *model_count,
2453 unsigned int *full_count) {
2455 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
2456 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
2457 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
2458 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
2459 model_count[TWO_TOKEN] += full_count[n];
2460 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
2463 static void full_to_model_counts(vp9_coeff_count_model *model_count,
2464 vp9_coeff_count *full_count) {
2467 for (i = 0; i < PLANE_TYPES; ++i)
2468 for (j = 0; j < REF_TYPES; ++j)
2469 for (k = 0; k < COEF_BANDS; ++k)
2470 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
2471 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
2474 #if 0 && CONFIG_INTERNAL_STATS
2475 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
2476 VP9_COMMON *const cm = &cpi->common;
2477 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
2480 vp9_clear_system_state();
2482 recon_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2484 if (cpi->twopass.total_left_stats.coded_error != 0.0)
2485 fprintf(f, "%10u %10d %10d %10d %10d"
2486 "%10"PRId64" %10"PRId64" %10"PRId64" %10"PRId64" %10d "
2487 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2488 "%6d %6d %5d %5d %5d "
2489 "%10"PRId64" %10.3lf"
2490 "%10lf %8u %10d %10d %10d\n",
2491 cpi->common.current_video_frame, cpi->rc.this_frame_target,
2492 cpi->rc.projected_frame_size,
2493 cpi->rc.projected_frame_size / cpi->common.MBs,
2494 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
2495 cpi->rc.vbr_bits_off_target,
2496 cpi->rc.total_target_vs_actual,
2497 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
2498 cpi->rc.total_actual_bits, cm->base_qindex,
2499 vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
2500 (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
2501 vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
2504 vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
2505 cpi->refresh_last_frame, cpi->refresh_golden_frame,
2506 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
2507 cpi->twopass.bits_left,
2508 cpi->twopass.total_left_stats.coded_error,
2509 cpi->twopass.bits_left /
2510 (1 + cpi->twopass.total_left_stats.coded_error),
2511 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
2512 cpi->twopass.kf_zeromotion_pct);
2517 FILE *const fmodes = fopen("Modes.stt", "a");
2520 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
2521 cm->frame_type, cpi->refresh_golden_frame,
2522 cpi->refresh_alt_ref_frame);
2524 for (i = 0; i < MAX_MODES; ++i)
2525 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
2527 fprintf(fmodes, "\n");
2534 static void encode_without_recode_loop(VP9_COMP *cpi,
2536 VP9_COMMON *const cm = &cpi->common;
2537 vp9_clear_system_state();
2538 vp9_set_quantizer(cm, q);
2540 // Variance adaptive and in frame q adjustment experiments are mutually
2542 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2543 vp9_vaq_frame_setup(cpi);
2544 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2545 vp9_setup_in_frame_q_adj(cpi);
2546 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
2547 vp9_cyclic_refresh_setup(cpi);
2549 // transform / motion compensation build reconstruction frame
2550 vp9_encode_frame(cpi);
2552 // Update the skip mb flag probabilities based on the distribution
2553 // seen in the last encoder iteration.
2554 // update_base_skip_probs(cpi);
2555 vp9_clear_system_state();
2558 static void encode_with_recode_loop(VP9_COMP *cpi,
2564 VP9_COMMON *const cm = &cpi->common;
2565 RATE_CONTROL *const rc = &cpi->rc;
2568 int overshoot_seen = 0;
2569 int undershoot_seen = 0;
2570 int q_low = bottom_index, q_high = top_index;
2571 int frame_over_shoot_limit;
2572 int frame_under_shoot_limit;
2574 // Decide frame size bounds
2575 vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
2576 &frame_under_shoot_limit,
2577 &frame_over_shoot_limit);
2580 vp9_clear_system_state();
2582 vp9_set_quantizer(cm, q);
2584 if (loop_count == 0)
2587 // Variance adaptive and in frame q adjustment experiments are mutually
2589 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2590 vp9_vaq_frame_setup(cpi);
2591 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2592 vp9_setup_in_frame_q_adj(cpi);
2595 // transform / motion compensation build reconstruction frame
2596 vp9_encode_frame(cpi);
2598 // Update the skip mb flag probabilities based on the distribution
2599 // seen in the last encoder iteration.
2600 // update_base_skip_probs(cpi);
2602 vp9_clear_system_state();
2604 // Dummy pack of the bitstream using up to date stats to get an
2605 // accurate estimate of output frame size to determine if we need
2607 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
2608 save_coding_context(cpi);
2609 if (!cpi->sf.use_nonrd_pick_mode)
2610 vp9_pack_bitstream(cpi, dest, size);
2612 rc->projected_frame_size = (int)(*size) << 3;
2613 restore_coding_context(cpi);
2615 if (frame_over_shoot_limit == 0)
2616 frame_over_shoot_limit = 1;
2619 if (cpi->oxcf.rc_mode == VPX_Q) {
2622 if ((cm->frame_type == KEY_FRAME) &&
2623 rc->this_key_frame_forced &&
2624 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
2628 int high_err_target = cpi->ambient_err;
2629 int low_err_target = cpi->ambient_err >> 1;
2631 #if CONFIG_VP9_HIGHBITDEPTH
2632 if (cm->use_highbitdepth) {
2633 kf_err = vp9_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm),
2636 kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2639 kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2640 #endif // CONFIG_VP9_HIGHBITDEPTH
2642 // Prevent possible divide by zero error below for perfect KF
2645 // The key frame is not good enough or we can afford
2646 // to make it better without undue risk of popping.
2647 if ((kf_err > high_err_target &&
2648 rc->projected_frame_size <= frame_over_shoot_limit) ||
2649 (kf_err > low_err_target &&
2650 rc->projected_frame_size <= frame_under_shoot_limit)) {
2652 q_high = q > q_low ? q - 1 : q_low;
2655 q = (q * high_err_target) / kf_err;
2656 q = MIN(q, (q_high + q_low) >> 1);
2657 } else if (kf_err < low_err_target &&
2658 rc->projected_frame_size >= frame_under_shoot_limit) {
2659 // The key frame is much better than the previous frame
2661 q_low = q < q_high ? q + 1 : q_high;
2664 q = (q * low_err_target) / kf_err;
2665 q = MIN(q, (q_high + q_low + 1) >> 1);
2668 // Clamp Q to upper and lower limits:
2669 q = clamp(q, q_low, q_high);
2672 } else if (recode_loop_test(
2673 cpi, frame_over_shoot_limit, frame_under_shoot_limit,
2674 q, MAX(q_high, top_index), bottom_index)) {
2675 // Is the projected frame size out of range and are we allowed
2676 // to attempt to recode.
2680 // Frame size out of permitted range:
2681 // Update correction factor & compute new Q to try...
2683 // Frame is too large
2684 if (rc->projected_frame_size > rc->this_frame_target) {
2685 // Special case if the projected size is > the max allowed.
2686 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
2687 q_high = rc->worst_quality;
2689 // Raise Qlow as to at least the current value
2690 q_low = q < q_high ? q + 1 : q_high;
2692 if (undershoot_seen || loop_count > 1) {
2693 // Update rate_correction_factor unless
2694 vp9_rc_update_rate_correction_factors(cpi, 1);
2696 q = (q_high + q_low + 1) / 2;
2698 // Update rate_correction_factor unless
2699 vp9_rc_update_rate_correction_factors(cpi, 0);
2701 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2702 bottom_index, MAX(q_high, top_index));
2704 while (q < q_low && retries < 10) {
2705 vp9_rc_update_rate_correction_factors(cpi, 0);
2706 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2707 bottom_index, MAX(q_high, top_index));
2714 // Frame is too small
2715 q_high = q > q_low ? q - 1 : q_low;
2717 if (overshoot_seen || loop_count > 1) {
2718 vp9_rc_update_rate_correction_factors(cpi, 1);
2719 q = (q_high + q_low) / 2;
2721 vp9_rc_update_rate_correction_factors(cpi, 0);
2722 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2723 bottom_index, top_index);
2724 // Special case reset for qlow for constrained quality.
2725 // This should only trigger where there is very substantial
2726 // undershoot on a frame and the auto cq level is above
2727 // the user passsed in value.
2728 if (cpi->oxcf.rc_mode == VPX_CQ &&
2733 while (q > q_high && retries < 10) {
2734 vp9_rc_update_rate_correction_factors(cpi, 0);
2735 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2736 bottom_index, top_index);
2741 undershoot_seen = 1;
2744 // Clamp Q to upper and lower limits:
2745 q = clamp(q, q_low, q_high);
2753 // Special case for overlay frame.
2754 if (rc->is_src_frame_alt_ref &&
2755 rc->projected_frame_size < rc->max_frame_bandwidth)
2761 #if CONFIG_INTERNAL_STATS
2762 cpi->tot_recode_hits++;
2768 static int get_ref_frame_flags(const VP9_COMP *cpi) {
2769 const int *const map = cpi->common.ref_frame_map;
2770 const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
2771 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
2772 const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
2773 int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
2776 flags &= ~VP9_GOLD_FLAG;
2778 if (cpi->rc.frames_till_gf_update_due == INT_MAX && !is_two_pass_svc(cpi))
2779 flags &= ~VP9_GOLD_FLAG;
2782 flags &= ~VP9_ALT_FLAG;
2785 flags &= ~VP9_ALT_FLAG;
2790 static void set_ext_overrides(VP9_COMP *cpi) {
2791 // Overrides the defaults with the externally supplied values with
2792 // vp9_update_reference() and vp9_update_entropy() calls
2793 // Note: The overrides are valid only for the next frame passed
2794 // to encode_frame_to_data_rate() function
2795 if (cpi->ext_refresh_frame_context_pending) {
2796 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
2797 cpi->ext_refresh_frame_context_pending = 0;
2799 if (cpi->ext_refresh_frame_flags_pending) {
2800 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
2801 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
2802 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
2803 cpi->ext_refresh_frame_flags_pending = 0;
2807 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
2808 YV12_BUFFER_CONFIG *unscaled,
2809 YV12_BUFFER_CONFIG *scaled) {
2810 if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
2811 cm->mi_rows * MI_SIZE != unscaled->y_height) {
2812 #if CONFIG_VP9_HIGHBITDEPTH
2813 scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
2815 scale_and_extend_frame_nonnormative(unscaled, scaled);
2816 #endif // CONFIG_VP9_HIGHBITDEPTH
2823 static int is_skippable_frame(const VP9_COMP *cpi) {
2824 // If the current frame does not have non-zero motion vector detected in the
2825 // first pass, and so do its previous and forward frames, then this frame
2826 // can be skipped for partition check, and the partition size is assigned
2827 // according to the variance
2828 const SVC *const svc = &cpi->svc;
2829 const TWO_PASS *const twopass = is_two_pass_svc(cpi) ?
2830 &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass;
2832 return (!frame_is_intra_only(&cpi->common) &&
2833 twopass->stats_in - 2 > twopass->stats_in_start &&
2834 twopass->stats_in < twopass->stats_in_end &&
2835 (twopass->stats_in - 1)->pcnt_inter - (twopass->stats_in - 1)->pcnt_motion
2837 (twopass->stats_in - 2)->pcnt_inter - (twopass->stats_in - 2)->pcnt_motion
2839 twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1);
2842 static void set_arf_sign_bias(VP9_COMP *cpi) {
2843 VP9_COMMON *const cm = &cpi->common;
2846 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
2847 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2848 arf_sign_bias = cpi->rc.source_alt_ref_active &&
2849 (!cpi->refresh_alt_ref_frame ||
2850 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
2853 (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
2855 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
2858 static void set_mv_search_params(VP9_COMP *cpi) {
2859 const VP9_COMMON *const cm = &cpi->common;
2860 const unsigned int max_mv_def = MIN(cm->width, cm->height);
2862 // Default based on max resolution.
2863 cpi->mv_step_param = vp9_init_search_range(max_mv_def);
2865 if (cpi->sf.mv.auto_mv_step_size) {
2866 if (frame_is_intra_only(cm)) {
2867 // Initialize max_mv_magnitude for use in the first INTER frame
2868 // after a key/intra-only frame.
2869 cpi->max_mv_magnitude = max_mv_def;
2872 // Allow mv_steps to correspond to twice the max mv magnitude found
2873 // in the previous frame, capped by the default max_mv_magnitude based
2875 cpi->mv_step_param =
2876 vp9_init_search_range(MIN(max_mv_def, 2 * cpi->max_mv_magnitude));
2877 cpi->max_mv_magnitude = 0;
2883 int setup_interp_filter_search_mask(VP9_COMP *cpi) {
2884 INTERP_FILTER ifilter;
2885 int ref_total[MAX_REF_FRAMES] = {0};
2886 MV_REFERENCE_FRAME ref;
2888 if (cpi->common.last_frame_type == KEY_FRAME ||
2889 cpi->refresh_alt_ref_frame)
2891 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
2892 for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
2893 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
2895 for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
2896 if ((ref_total[LAST_FRAME] &&
2897 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
2898 (ref_total[GOLDEN_FRAME] == 0 ||
2899 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50
2900 < ref_total[GOLDEN_FRAME]) &&
2901 (ref_total[ALTREF_FRAME] == 0 ||
2902 cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50
2903 < ref_total[ALTREF_FRAME]))
2904 mask |= 1 << ifilter;
2909 static void encode_frame_to_data_rate(VP9_COMP *cpi,
2912 unsigned int *frame_flags) {
2913 VP9_COMMON *const cm = &cpi->common;
2914 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2915 struct segmentation *const seg = &cm->seg;
2921 set_ext_overrides(cpi);
2923 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
2924 &cpi->scaled_source);
2926 if (cpi->unscaled_last_source != NULL)
2927 cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
2928 &cpi->scaled_last_source);
2930 vp9_scale_references(cpi);
2932 vp9_clear_system_state();
2934 // Enable or disable mode based tweaking of the zbin.
2935 // For 2 pass only used where GF/ARF prediction quality
2936 // is above a threshold.
2937 cpi->zbin_mode_boost = 0;
2938 cpi->zbin_mode_boost_enabled = 0;
2940 // Set the arf sign bias for this frame.
2941 set_arf_sign_bias(cpi);
2943 // Set default state for segment based loop filter update flags.
2944 cm->lf.mode_ref_delta_update = 0;
2946 set_mv_search_params(cpi);
2948 if (cpi->oxcf.pass == 2 &&
2949 cpi->sf.adaptive_interp_filter_search)
2950 cpi->sf.interp_filter_search_mask =
2951 setup_interp_filter_search_mask(cpi);
2954 // Set various flags etc to special state if it is a key frame.
2955 if (frame_is_intra_only(cm)) {
2956 // Reset the loop filter deltas and segmentation map.
2957 vp9_reset_segment_features(&cm->seg);
2959 // If segmentation is enabled force a map update for key frames.
2961 seg->update_map = 1;
2962 seg->update_data = 1;
2965 // The alternate reference frame cannot be active for a key frame.
2966 cpi->rc.source_alt_ref_active = 0;
2968 cm->error_resilient_mode = oxcf->error_resilient_mode;
2970 // By default, encoder assumes decoder can use prev_mi.
2971 if (cm->error_resilient_mode) {
2972 cm->frame_parallel_decoding_mode = 1;
2973 cm->reset_frame_context = 0;
2974 cm->refresh_frame_context = 0;
2975 } else if (cm->intra_only) {
2976 cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
2977 // Only reset the current context.
2978 cm->reset_frame_context = 2;
2981 if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
2982 cm->frame_context_idx =
2983 cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
2984 cpi->svc.temporal_layer_id;
2986 // The probs will be updated based on the frame type of its previous
2987 // frame if frame_parallel_decoding_mode is 0. The type may vary for
2988 // the frame after a key frame in base layer since we may drop enhancement
2989 // layers. So set frame_parallel_decoding_mode to 1 in this case.
2990 if (cpi->svc.number_temporal_layers == 1) {
2991 if (cpi->svc.spatial_layer_id == 0 &&
2992 cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
2993 cm->frame_parallel_decoding_mode = 1;
2995 cm->frame_parallel_decoding_mode = 0;
2996 } else if (cpi->svc.spatial_layer_id == 0) {
2997 // Find the 2nd frame in temporal base layer and 1st frame in temporal
2998 // enhancement layers from the key frame.
3000 for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
3001 if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
3002 cm->frame_parallel_decoding_mode = 1;
3006 if (i == cpi->svc.number_temporal_layers)
3007 cm->frame_parallel_decoding_mode = 0;
3011 // Configure experimental use of segmentation for enhanced coding of
3012 // static regions if indicated.
3013 // Only allowed in second pass of two pass (as requires lagged coding)
3014 // and if the relevant speed feature flag is set.
3015 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3016 configure_static_seg_features(cpi);
3018 // Check if the current frame is skippable for the partition search in the
3019 // second pass according to the first pass stats
3020 if (cpi->sf.allow_partition_search_skip && oxcf->pass == 2 &&
3021 (!cpi->use_svc || is_two_pass_svc(cpi))) {
3022 cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
3025 // For 1 pass CBR, check if we are dropping this frame.
3026 // Never drop on key frame.
3027 if (oxcf->pass == 0 &&
3028 oxcf->rc_mode == VPX_CBR &&
3029 cm->frame_type != KEY_FRAME) {
3030 if (vp9_rc_drop_frame(cpi)) {
3031 vp9_rc_postencode_update_drop_frame(cpi);
3032 ++cm->current_video_frame;
3037 vp9_clear_system_state();
3039 #if CONFIG_VP9_POSTPROC
3040 if (oxcf->noise_sensitivity > 0) {
3042 switch (oxcf->noise_sensitivity) {
3060 vp9_denoise(cpi->Source, cpi->Source, l);
3064 #if CONFIG_INTERNAL_STATS
3067 for (i = 0; i < MAX_MODES; ++i)
3068 cpi->mode_chosen_counts[i] = 0;
3072 vp9_set_speed_features(cpi);
3074 vp9_set_rd_speed_thresholds(cpi);
3075 vp9_set_rd_speed_thresholds_sub8x8(cpi);
3077 // Decide q and q bounds.
3078 q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index);
3080 if (!frame_is_intra_only(cm)) {
3081 cm->interp_filter = cpi->sf.default_interp_filter;
3082 /* TODO: Decide this more intelligently */
3083 vp9_set_high_precision_mv(cpi, q < HIGH_PRECISION_MV_QTHRESH);
3086 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
3087 encode_without_recode_loop(cpi, q);
3089 encode_with_recode_loop(cpi, size, dest, q, bottom_index, top_index);
3092 #if CONFIG_VP9_TEMPORAL_DENOISING
3093 #ifdef OUTPUT_YUV_DENOISED
3094 if (oxcf->noise_sensitivity > 0) {
3095 vp9_write_yuv_frame_420(&cpi->denoiser.running_avg_y[INTRA_FRAME],
3102 // Special case code to reduce pulsing when key frames are forced at a
3103 // fixed interval. Note the reconstruction error if it is the frame before
3104 // the force key frame
3105 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
3106 #if CONFIG_VP9_HIGHBITDEPTH
3107 if (cm->use_highbitdepth) {
3108 cpi->ambient_err = vp9_highbd_get_y_sse(cpi->Source,
3109 get_frame_new_buffer(cm),
3112 cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3115 cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3116 #endif // CONFIG_VP9_HIGHBITDEPTH
3119 // If the encoder forced a KEY_FRAME decision
3120 if (cm->frame_type == KEY_FRAME)
3121 cpi->refresh_last_frame = 1;
3123 cm->frame_to_show = get_frame_new_buffer(cm);
3125 // Pick the loop filter level for the frame.
3126 loopfilter_frame(cpi, cm);
3128 // build the bitstream
3129 vp9_pack_bitstream(cpi, dest, size);
3131 if (cm->seg.update_map)
3132 update_reference_segmentation_map(cpi);
3134 release_scaled_references(cpi);
3135 vp9_update_reference_frames(cpi);
3137 for (t = TX_4X4; t <= TX_32X32; t++)
3138 full_to_model_counts(cm->counts.coef[t], cpi->coef_counts[t]);
3140 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
3141 vp9_adapt_coef_probs(cm);
3143 if (!frame_is_intra_only(cm)) {
3144 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
3145 vp9_adapt_mode_probs(cm);
3146 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
3150 if (cpi->refresh_golden_frame == 1)
3151 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
3153 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
3155 if (cpi->refresh_alt_ref_frame == 1)
3156 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
3158 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
3160 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
3162 cm->last_frame_type = cm->frame_type;
3163 vp9_rc_postencode_update(cpi, *size);
3166 output_frame_level_debug_stats(cpi);
3169 if (cm->frame_type == KEY_FRAME) {
3170 // Tell the caller that the frame was coded as a key frame
3171 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
3173 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
3176 // Clear the one shot update flags for segmentation map and mode/ref loop
3178 cm->seg.update_map = 0;
3179 cm->seg.update_data = 0;
3180 cm->lf.mode_ref_delta_update = 0;
3182 // keep track of the last coded dimensions
3183 cm->last_width = cm->width;
3184 cm->last_height = cm->height;
3186 // reset to normal state now that we are done.
3187 if (!cm->show_existing_frame) {
3188 if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0)
3189 cm->last_show_frame = 0;
3191 cm->last_show_frame = cm->show_frame;
3194 if (cm->show_frame) {
3195 vp9_swap_mi_and_prev_mi(cm);
3197 // Don't increment frame counters if this was an altref buffer
3198 // update not a real frame
3199 ++cm->current_video_frame;
3201 vp9_inc_frame_in_layer(cpi);
3204 if (is_two_pass_svc(cpi))
3205 cpi->svc.layer_context[cpi->svc.spatial_layer_id].last_frame_type =
3209 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3210 unsigned int *frame_flags) {
3211 vp9_rc_get_svc_params(cpi);
3212 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3215 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3216 unsigned int *frame_flags) {
3217 if (cpi->oxcf.rc_mode == VPX_CBR) {
3218 vp9_rc_get_one_pass_cbr_params(cpi);
3220 vp9_rc_get_one_pass_vbr_params(cpi);
3222 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3225 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
3226 uint8_t *dest, unsigned int *frame_flags) {
3227 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
3228 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3229 vp9_twopass_postencode_update(cpi);
3232 static void init_motion_estimation(VP9_COMP *cpi) {
3233 int y_stride = cpi->scaled_source.y_stride;
3235 if (cpi->sf.mv.search_method == NSTEP) {
3236 vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
3237 } else if (cpi->sf.mv.search_method == DIAMOND) {
3238 vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
3242 static void check_initial_width(VP9_COMP *cpi,
3243 #if CONFIG_VP9_HIGHBITDEPTH
3244 int use_highbitdepth,
3246 int subsampling_x, int subsampling_y) {
3247 VP9_COMMON *const cm = &cpi->common;
3249 if (!cpi->initial_width) {
3250 cm->subsampling_x = subsampling_x;
3251 cm->subsampling_y = subsampling_y;
3252 #if CONFIG_VP9_HIGHBITDEPTH
3253 cm->use_highbitdepth = use_highbitdepth;
3256 alloc_raw_frame_buffers(cpi);
3257 alloc_ref_frame_buffers(cpi);
3258 alloc_util_frame_buffers(cpi);
3260 init_motion_estimation(cpi);
3262 cpi->initial_width = cm->width;
3263 cpi->initial_height = cm->height;
3268 int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
3269 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3271 VP9_COMMON *cm = &cpi->common;
3272 struct vpx_usec_timer timer;
3274 const int subsampling_x = sd->subsampling_x;
3275 const int subsampling_y = sd->subsampling_y;
3276 #if CONFIG_VP9_HIGHBITDEPTH
3277 const int use_highbitdepth = sd->flags & YV12_FLAG_HIGHBITDEPTH;
3278 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
3280 check_initial_width(cpi, subsampling_x, subsampling_y);
3281 #endif // CONFIG_VP9_HIGHBITDEPTH
3283 vpx_usec_timer_start(&timer);
3285 if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags))
3287 vpx_usec_timer_mark(&timer);
3288 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
3290 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
3291 (subsampling_x != 1 || subsampling_y != 1)) {
3292 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
3293 "Non-4:2:0 color space requires profile 1 or 3");
3296 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
3297 (subsampling_x == 1 && subsampling_y == 1)) {
3298 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
3299 "4:2:0 color space requires profile 0 or 2");
3307 static int frame_is_reference(const VP9_COMP *cpi) {
3308 const VP9_COMMON *cm = &cpi->common;
3310 return cm->frame_type == KEY_FRAME ||
3311 cpi->refresh_last_frame ||
3312 cpi->refresh_golden_frame ||
3313 cpi->refresh_alt_ref_frame ||
3314 cm->refresh_frame_context ||
3315 cm->lf.mode_ref_delta_update ||
3316 cm->seg.update_map ||
3317 cm->seg.update_data;
3320 void adjust_frame_rate(VP9_COMP *cpi,
3321 const struct lookahead_entry *source) {
3322 int64_t this_duration;
3325 if (source->ts_start == cpi->first_time_stamp_ever) {
3326 this_duration = source->ts_end - source->ts_start;
3329 int64_t last_duration = cpi->last_end_time_stamp_seen
3330 - cpi->last_time_stamp_seen;
3332 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
3334 // do a step update if the duration changes by 10%
3336 step = (int)((this_duration - last_duration) * 10 / last_duration);
3339 if (this_duration) {
3341 vp9_new_framerate(cpi, 10000000.0 / this_duration);
3343 // Average this frame's rate into the last second's average
3344 // frame rate. If we haven't seen 1 second yet, then average
3345 // over the whole interval seen.
3346 const double interval = MIN((double)(source->ts_end
3347 - cpi->first_time_stamp_ever), 10000000.0);
3348 double avg_duration = 10000000.0 / cpi->framerate;
3349 avg_duration *= (interval - avg_duration + this_duration);
3350 avg_duration /= interval;
3352 vp9_new_framerate(cpi, 10000000.0 / avg_duration);
3355 cpi->last_time_stamp_seen = source->ts_start;
3356 cpi->last_end_time_stamp_seen = source->ts_end;
3359 // Returns 0 if this is not an alt ref else the offset of the source frame
3360 // used as the arf midpoint.
3361 static int get_arf_src_index(VP9_COMP *cpi) {
3362 RATE_CONTROL *const rc = &cpi->rc;
3363 int arf_src_index = 0;
3364 if (is_altref_enabled(cpi)) {
3365 if (cpi->oxcf.pass == 2) {
3366 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3367 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
3368 arf_src_index = gf_group->arf_src_offset[gf_group->index];
3370 } else if (rc->source_alt_ref_pending) {
3371 arf_src_index = rc->frames_till_gf_update_due;
3374 return arf_src_index;
3377 static void check_src_altref(VP9_COMP *cpi,
3378 const struct lookahead_entry *source) {
3379 RATE_CONTROL *const rc = &cpi->rc;
3381 if (cpi->oxcf.pass == 2) {
3382 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3383 rc->is_src_frame_alt_ref =
3384 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
3386 rc->is_src_frame_alt_ref = cpi->alt_ref_source &&
3387 (source == cpi->alt_ref_source);
3390 if (rc->is_src_frame_alt_ref) {
3391 // Current frame is an ARF overlay frame.
3392 cpi->alt_ref_source = NULL;
3394 // Don't refresh the last buffer for an ARF overlay frame. It will
3395 // become the GF so preserve last as an alternative prediction option.
3396 cpi->refresh_last_frame = 0;
3400 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
3401 size_t *size, uint8_t *dest,
3402 int64_t *time_stamp, int64_t *time_end, int flush) {
3403 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3404 VP9_COMMON *const cm = &cpi->common;
3405 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
3406 RATE_CONTROL *const rc = &cpi->rc;
3407 struct vpx_usec_timer cmptimer;
3408 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
3409 struct lookahead_entry *last_source = NULL;
3410 struct lookahead_entry *source = NULL;
3411 MV_REFERENCE_FRAME ref_frame;
3414 if (is_two_pass_svc(cpi)) {
3415 #if CONFIG_SPATIAL_SVC
3416 vp9_svc_start_frame(cpi);
3418 if (oxcf->pass == 2)
3419 vp9_restore_layer_context(cpi);
3422 vpx_usec_timer_start(&cmptimer);
3424 vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
3427 cm->reset_frame_context = 0;
3428 cm->refresh_frame_context = 1;
3429 cpi->refresh_last_frame = 1;
3430 cpi->refresh_golden_frame = 0;
3431 cpi->refresh_alt_ref_frame = 0;
3433 // Should we encode an arf frame.
3434 arf_src_index = get_arf_src_index(cpi);
3435 if (arf_src_index) {
3436 assert(arf_src_index <= rc->frames_to_key);
3438 if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
3439 cpi->alt_ref_source = source;
3441 #if CONFIG_SPATIAL_SVC
3442 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
3444 // Reference a hidden frame from a lower layer
3445 for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
3446 if (oxcf->ss_play_alternate[i]) {
3447 cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
3452 cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
3455 if (oxcf->arnr_max_frames > 0) {
3456 // Produce the filtered ARF frame.
3457 vp9_temporal_filter(cpi, arf_src_index);
3458 vp9_extend_frame_borders(&cpi->alt_ref_buffer);
3459 force_src_buffer = &cpi->alt_ref_buffer;
3463 cpi->refresh_alt_ref_frame = 1;
3464 cpi->refresh_golden_frame = 0;
3465 cpi->refresh_last_frame = 0;
3466 rc->is_src_frame_alt_ref = 0;
3467 rc->source_alt_ref_pending = 0;
3469 rc->source_alt_ref_pending = 0;
3474 // Get last frame source.
3475 if (cm->current_video_frame > 0) {
3476 if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
3480 // Read in the source frame.
3481 #if CONFIG_SPATIAL_SVC
3482 if (is_two_pass_svc(cpi))
3483 source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
3486 source = vp9_lookahead_pop(cpi->lookahead, flush);
3487 if (source != NULL) {
3491 // Check to see if the frame should be encoded as an arf overlay.
3492 check_src_altref(cpi, source);
3497 cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
3500 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
3502 *time_stamp = source->ts_start;
3503 *time_end = source->ts_end;
3504 *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
3508 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
3509 vp9_end_first_pass(cpi); /* get last stats packet */
3510 cpi->twopass.first_pass_done = 1;
3515 if (source->ts_start < cpi->first_time_stamp_ever) {
3516 cpi->first_time_stamp_ever = source->ts_start;
3517 cpi->last_end_time_stamp_seen = source->ts_start;
3520 // Clear down mmx registers
3521 vp9_clear_system_state();
3523 // adjust frame rates based on timestamps given
3524 if (cm->show_frame) {
3525 adjust_frame_rate(cpi, source);
3528 if (cpi->svc.number_temporal_layers > 1 &&
3529 oxcf->rc_mode == VPX_CBR) {
3530 vp9_update_temporal_layer_framerate(cpi);
3531 vp9_restore_layer_context(cpi);
3534 // start with a 0 size frame
3537 /* find a free buffer for the new frame, releasing the reference previously
3540 cm->frame_bufs[cm->new_fb_idx].ref_count--;
3541 cm->new_fb_idx = get_free_fb(cm);
3543 // For two pass encodes analyse the first pass stats and determine
3544 // the bit allocation and other parameters for this frame / group of frames.
3545 if ((oxcf->pass == 2) && (!cpi->use_svc || is_two_pass_svc(cpi))) {
3546 vp9_rc_get_second_pass_params(cpi);
3549 if (!cpi->use_svc && cpi->multi_arf_allowed) {
3550 if (cm->frame_type == KEY_FRAME) {
3551 init_buffer_indices(cpi);
3552 } else if (oxcf->pass == 2) {
3553 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3554 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
3558 cpi->frame_flags = *frame_flags;
3560 if (oxcf->pass == 2 &&
3561 cm->current_video_frame == 0 &&
3562 oxcf->allow_spatial_resampling &&
3563 oxcf->rc_mode == VPX_VBR) {
3564 // Internal scaling is triggered on the first frame.
3565 vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3566 oxcf->scaled_frame_height);
3569 // Reset the frame pointers to the current frame size
3570 vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
3571 cm->width, cm->height,
3572 cm->subsampling_x, cm->subsampling_y,
3573 #if CONFIG_VP9_HIGHBITDEPTH
3574 cm->use_highbitdepth,
3576 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
3578 alloc_util_frame_buffers(cpi);
3579 init_motion_estimation(cpi);
3581 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3582 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
3583 YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf;
3584 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
3587 #if CONFIG_VP9_HIGHBITDEPTH
3588 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
3589 buf->y_crop_width, buf->y_crop_height,
3590 cm->width, cm->height,
3591 (buf->flags & YV12_FLAG_HIGHBITDEPTH) ?
3594 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
3595 buf->y_crop_width, buf->y_crop_height,
3596 cm->width, cm->height);
3597 #endif // CONFIG_VP9_HIGHBITDEPTH
3598 if (vp9_is_scaled(&ref_buf->sf))
3599 vp9_extend_frame_borders(buf);
3602 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3604 if (oxcf->aq_mode == VARIANCE_AQ) {
3608 if (oxcf->pass == 1 &&
3609 (!cpi->use_svc || is_two_pass_svc(cpi))) {
3610 const int lossless = is_lossless_requested(oxcf);
3611 #if CONFIG_VP9_HIGHBITDEPTH
3612 if (cpi->oxcf.use_highbitdepth)
3613 cpi->mb.fwd_txm4x4 = lossless ? vp9_highbd_fwht4x4 : vp9_highbd_fdct4x4;
3615 cpi->mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3616 cpi->mb.highbd_itxm_add = lossless ? vp9_highbd_iwht4x4_add :
3617 vp9_highbd_idct4x4_add;
3619 cpi->mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3620 #endif // CONFIG_VP9_HIGHBITDEPTH
3621 cpi->mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
3622 vp9_first_pass(cpi, source);
3623 } else if (oxcf->pass == 2 &&
3624 (!cpi->use_svc || is_two_pass_svc(cpi))) {
3625 Pass2Encode(cpi, size, dest, frame_flags);
3626 } else if (cpi->use_svc) {
3627 SvcEncode(cpi, size, dest, frame_flags);
3630 Pass0Encode(cpi, size, dest, frame_flags);
3633 if (cm->refresh_frame_context)
3634 cm->frame_contexts[cm->frame_context_idx] = cm->fc;
3636 // Frame was dropped, release scaled references.
3638 release_scaled_references(cpi);
3642 cpi->droppable = !frame_is_reference(cpi);
3645 // Save layer specific state.
3646 if ((cpi->svc.number_temporal_layers > 1 &&
3647 oxcf->rc_mode == VPX_CBR) ||
3648 ((cpi->svc.number_temporal_layers > 1 ||
3649 cpi->svc.number_spatial_layers > 1) &&
3651 vp9_save_layer_context(cpi);
3654 vpx_usec_timer_mark(&cmptimer);
3655 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
3657 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
3658 generate_psnr_packet(cpi);
3660 #if CONFIG_INTERNAL_STATS
3662 if (oxcf->pass != 1) {
3663 cpi->bytes += (int)(*size);
3665 if (cm->show_frame) {
3668 if (cpi->b_calculate_psnr) {
3669 YV12_BUFFER_CONFIG *orig = cpi->Source;
3670 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
3671 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
3673 #if CONFIG_VP9_HIGHBITDEPTH
3674 calc_highbd_psnr(orig, recon, &psnr, cpi->mb.e_mbd.bd,
3675 cpi->oxcf.input_bit_depth);
3677 calc_psnr(orig, recon, &psnr);
3678 #endif // CONFIG_VP9_HIGHBITDEPTH
3680 cpi->total += psnr.psnr[0];
3681 cpi->total_y += psnr.psnr[1];
3682 cpi->total_u += psnr.psnr[2];
3683 cpi->total_v += psnr.psnr[3];
3684 cpi->total_sq_error += psnr.sse[0];
3685 cpi->total_samples += psnr.samples[0];
3689 double frame_ssim2 = 0, weight = 0;
3690 #if CONFIG_VP9_POSTPROC
3691 // TODO(agrange) Add resizing of post-proc buffer in here when the
3692 // encoder is changed to use on-demand buffer allocation.
3693 vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
3694 cm->lf.filter_level * 10 / 6);
3696 vp9_clear_system_state();
3698 #if CONFIG_VP9_HIGHBITDEPTH
3699 calc_highbd_psnr(orig, pp, &psnr, cpi->mb.e_mbd.bd,
3700 cpi->oxcf.input_bit_depth);
3702 calc_psnr(orig, pp, &psnr2);
3703 #endif // CONFIG_VP9_HIGHBITDEPTH
3705 cpi->totalp += psnr2.psnr[0];
3706 cpi->totalp_y += psnr2.psnr[1];
3707 cpi->totalp_u += psnr2.psnr[2];
3708 cpi->totalp_v += psnr2.psnr[3];
3709 cpi->totalp_sq_error += psnr2.sse[0];
3710 cpi->totalp_samples += psnr2.samples[0];
3712 #if CONFIG_VP9_HIGHBITDEPTH
3713 if (cm->use_highbitdepth) {
3714 frame_ssim2 = vp9_highbd_calc_ssim(orig, recon, &weight, xd->bd);
3716 frame_ssim2 = vp9_calc_ssim(orig, recon, &weight);
3719 frame_ssim2 = vp9_calc_ssim(orig, recon, &weight);
3720 #endif // CONFIG_VP9_HIGHBITDEPTH
3722 cpi->summed_quality += frame_ssim2 * weight;
3723 cpi->summed_weights += weight;
3725 #if CONFIG_VP9_HIGHBITDEPTH
3726 if (cm->use_highbitdepth) {
3727 frame_ssim2 = vp9_highbd_calc_ssim(
3728 orig, &cm->post_proc_buffer, &weight, xd->bd);
3730 frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, &weight);
3733 frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, &weight);
3734 #endif // CONFIG_VP9_HIGHBITDEPTH
3736 cpi->summedp_quality += frame_ssim2 * weight;
3737 cpi->summedp_weights += weight;
3740 FILE *f = fopen("q_used.stt", "a");
3741 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3742 cpi->common.current_video_frame, y2, u2, v2,
3743 frame_psnr2, frame_ssim2);
3751 if (cpi->b_calculate_ssimg) {
3752 double y, u, v, frame_all;
3753 #if CONFIG_VP9_HIGHBITDEPTH
3754 if (cm->use_highbitdepth) {
3755 frame_all = vp9_highbd_calc_ssimg(cpi->Source, cm->frame_to_show, &y,
3758 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u,
3762 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
3763 #endif // CONFIG_VP9_HIGHBITDEPTH
3764 cpi->total_ssimg_y += y;
3765 cpi->total_ssimg_u += u;
3766 cpi->total_ssimg_v += v;
3767 cpi->total_ssimg_all += frame_all;
3774 if (is_two_pass_svc(cpi) && cm->show_frame) {
3775 ++cpi->svc.spatial_layer_to_encode;
3776 if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
3777 cpi->svc.spatial_layer_to_encode = 0;
3782 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
3783 vp9_ppflags_t *flags) {
3784 VP9_COMMON *cm = &cpi->common;
3785 #if !CONFIG_VP9_POSTPROC
3789 if (!cm->show_frame) {
3793 #if CONFIG_VP9_POSTPROC
3794 ret = vp9_post_proc_frame(cm, dest, flags);
3796 if (cm->frame_to_show) {
3797 *dest = *cm->frame_to_show;
3798 dest->y_width = cm->width;
3799 dest->y_height = cm->height;
3800 dest->uv_width = cm->width >> cm->subsampling_x;
3801 dest->uv_height = cm->height >> cm->subsampling_y;
3806 #endif // !CONFIG_VP9_POSTPROC
3807 vp9_clear_system_state();
3812 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols) {
3813 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
3814 const int mi_rows = cpi->common.mi_rows;
3815 const int mi_cols = cpi->common.mi_cols;
3818 for (r = 0; r < mi_rows; r++) {
3819 for (c = 0; c < mi_cols; c++) {
3820 cpi->segmentation_map[r * mi_cols + c] =
3821 !map[(r >> 1) * cols + (c >> 1)];
3824 vp9_enable_segfeature(&cpi->common.seg, 1, SEG_LVL_SKIP);
3825 vp9_enable_segmentation(&cpi->common.seg);
3827 vp9_disable_segmentation(&cpi->common.seg);
3835 int vp9_set_internal_size(VP9_COMP *cpi,
3836 VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
3837 VP9_COMMON *cm = &cpi->common;
3838 int hr = 0, hs = 0, vr = 0, vs = 0;
3840 if (horiz_mode > ONETWO || vert_mode > ONETWO)
3843 Scale2Ratio(horiz_mode, &hr, &hs);
3844 Scale2Ratio(vert_mode, &vr, &vs);
3846 // always go to the next whole number
3847 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
3848 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
3849 assert(cm->width <= cpi->initial_width);
3850 assert(cm->height <= cpi->initial_height);
3852 update_frame_size(cpi);
3857 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
3858 unsigned int height) {
3859 VP9_COMMON *cm = &cpi->common;
3860 #if CONFIG_VP9_HIGHBITDEPTH
3861 check_initial_width(cpi, 1, 1, cm->use_highbitdepth);
3863 check_initial_width(cpi, 1, 1);
3864 #endif // CONFIG_VP9_HIGHBITDEPTH
3868 if (cm->width * 5 < cpi->initial_width) {
3869 cm->width = cpi->initial_width / 5 + 1;
3870 printf("Warning: Desired width too small, changed to %d\n", cm->width);
3872 if (cm->width > cpi->initial_width) {
3873 cm->width = cpi->initial_width;
3874 printf("Warning: Desired width too large, changed to %d\n", cm->width);
3879 cm->height = height;
3880 if (cm->height * 5 < cpi->initial_height) {
3881 cm->height = cpi->initial_height / 5 + 1;
3882 printf("Warning: Desired height too small, changed to %d\n", cm->height);
3884 if (cm->height > cpi->initial_height) {
3885 cm->height = cpi->initial_height;
3886 printf("Warning: Desired height too large, changed to %d\n", cm->height);
3889 assert(cm->width <= cpi->initial_width);
3890 assert(cm->height <= cpi->initial_height);
3892 update_frame_size(cpi);
3897 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
3898 cpi->use_svc = use_svc;
3902 int vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b) {
3903 assert(a->y_crop_width == b->y_crop_width);
3904 assert(a->y_crop_height == b->y_crop_height);
3906 return (int)get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
3907 a->y_crop_width, a->y_crop_height);
3910 #if CONFIG_VP9_HIGHBITDEPTH
3911 int vp9_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
3912 const YV12_BUFFER_CONFIG *b,
3913 vpx_bit_depth_t bit_depth) {
3916 assert(a->y_crop_width == b->y_crop_width);
3917 assert(a->y_crop_height == b->y_crop_height);
3918 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
3919 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
3920 switch (bit_depth) {
3922 highbd_variance(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
3923 a->y_crop_width, a->y_crop_height, &sse, &sum);
3926 highbd_10_variance(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
3927 a->y_crop_width, a->y_crop_height, &sse, &sum);
3930 highbd_12_variance(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
3931 a->y_crop_width, a->y_crop_height, &sse, &sum);
3934 assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
3938 #endif // CONFIG_VP9_HIGHBITDEPTH
3940 int vp9_get_quantizer(VP9_COMP *cpi) {
3941 return cpi->common.base_qindex;
3944 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
3945 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF |
3946 VP8_EFLAG_NO_REF_ARF)) {
3949 if (flags & VP8_EFLAG_NO_REF_LAST)
3950 ref ^= VP9_LAST_FLAG;
3952 if (flags & VP8_EFLAG_NO_REF_GF)
3953 ref ^= VP9_GOLD_FLAG;
3955 if (flags & VP8_EFLAG_NO_REF_ARF)
3956 ref ^= VP9_ALT_FLAG;
3958 vp9_use_as_reference(cpi, ref);
3961 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
3962 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF |
3963 VP8_EFLAG_FORCE_ARF)) {
3966 if (flags & VP8_EFLAG_NO_UPD_LAST)
3967 upd ^= VP9_LAST_FLAG;
3969 if (flags & VP8_EFLAG_NO_UPD_GF)
3970 upd ^= VP9_GOLD_FLAG;
3972 if (flags & VP8_EFLAG_NO_UPD_ARF)
3973 upd ^= VP9_ALT_FLAG;
3975 vp9_update_reference(cpi, upd);
3978 if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
3979 vp9_update_entropy(cpi, 0);