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->td.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 static void vp9_enc_setup_mi(VP9_COMMON *cm) {
144 cm->mi = cm->mip + cm->mi_stride + 1;
145 vpx_memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
146 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
147 // Clear top border row
148 vpx_memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
149 // Clear left border column
150 for (i = 1; i < cm->mi_rows + 1; ++i)
151 vpx_memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
154 static int vp9_enc_alloc_mi(VP9_COMMON *cm, int mi_size) {
155 cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
158 cm->prev_mip = vpx_calloc(mi_size, sizeof(*cm->prev_mip));
161 cm->mi_alloc_size = mi_size;
165 static void vp9_enc_free_mi(VP9_COMMON *cm) {
168 vpx_free(cm->prev_mip);
172 static void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
173 // Current mip will be the prev_mip for the next frame.
174 MODE_INFO *temp = cm->prev_mip;
175 cm->prev_mip = cm->mip;
178 // Update the upper left visible macroblock ptrs.
179 cm->mi = cm->mip + cm->mi_stride + 1;
180 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
183 void vp9_initialize_enc() {
184 static int init_done = 0;
188 vp9_init_intra_predictors();
189 vp9_coef_tree_initialize();
190 vp9_tokenize_initialize();
192 vp9_rc_init_minq_luts();
193 vp9_entropy_mv_init();
194 vp9_entropy_mode_init();
195 vp9_temporal_filter_init();
200 static void dealloc_compressor_data(VP9_COMP *cpi) {
201 VP9_COMMON *const cm = &cpi->common;
204 vpx_free(cpi->tile_data);
205 cpi->tile_data = NULL;
207 // Delete sementation map
208 vpx_free(cpi->segmentation_map);
209 cpi->segmentation_map = NULL;
210 vpx_free(cm->last_frame_seg_map);
211 cm->last_frame_seg_map = NULL;
212 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
213 cpi->coding_context.last_frame_seg_map_copy = NULL;
215 vpx_free(cpi->complexity_map);
216 cpi->complexity_map = NULL;
218 vpx_free(cpi->nmvcosts[0]);
219 vpx_free(cpi->nmvcosts[1]);
220 cpi->nmvcosts[0] = NULL;
221 cpi->nmvcosts[1] = NULL;
223 vpx_free(cpi->nmvcosts_hp[0]);
224 vpx_free(cpi->nmvcosts_hp[1]);
225 cpi->nmvcosts_hp[0] = NULL;
226 cpi->nmvcosts_hp[1] = NULL;
228 vpx_free(cpi->nmvsadcosts[0]);
229 vpx_free(cpi->nmvsadcosts[1]);
230 cpi->nmvsadcosts[0] = NULL;
231 cpi->nmvsadcosts[1] = NULL;
233 vpx_free(cpi->nmvsadcosts_hp[0]);
234 vpx_free(cpi->nmvsadcosts_hp[1]);
235 cpi->nmvsadcosts_hp[0] = NULL;
236 cpi->nmvsadcosts_hp[1] = NULL;
238 vp9_cyclic_refresh_free(cpi->cyclic_refresh);
239 cpi->cyclic_refresh = NULL;
241 vp9_free_ref_frame_buffers(cm);
242 vp9_free_context_buffers(cm);
244 vp9_free_frame_buffer(&cpi->last_frame_uf);
245 vp9_free_frame_buffer(&cpi->scaled_source);
246 vp9_free_frame_buffer(&cpi->scaled_last_source);
247 vp9_free_frame_buffer(&cpi->alt_ref_buffer);
248 vp9_lookahead_destroy(cpi->lookahead);
253 vp9_free_pc_tree(&cpi->td);
255 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
256 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
257 vpx_free(lc->rc_twopass_stats_in.buf);
258 lc->rc_twopass_stats_in.buf = NULL;
259 lc->rc_twopass_stats_in.sz = 0;
262 if (cpi->source_diff_var != NULL) {
263 vpx_free(cpi->source_diff_var);
264 cpi->source_diff_var = NULL;
267 for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
268 vp9_free_frame_buffer(&cpi->svc.scaled_frames[i]);
270 vpx_memset(&cpi->svc.scaled_frames[0], 0,
271 MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
273 vp9_free_frame_buffer(&cpi->svc.empty_frame.img);
274 vpx_memset(&cpi->svc.empty_frame, 0, sizeof(cpi->svc.empty_frame));
277 static void save_coding_context(VP9_COMP *cpi) {
278 CODING_CONTEXT *const cc = &cpi->coding_context;
279 VP9_COMMON *cm = &cpi->common;
281 // Stores a snapshot of key state variables which can subsequently be
282 // restored with a call to vp9_restore_coding_context. These functions are
283 // intended for use in a re-code loop in vp9_compress_frame where the
284 // quantizer value is adjusted between loop iterations.
285 vp9_copy(cc->nmvjointcost, cpi->td.mb.nmvjointcost);
287 vpx_memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
288 MV_VALS * sizeof(*cpi->nmvcosts[0]));
289 vpx_memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
290 MV_VALS * sizeof(*cpi->nmvcosts[1]));
291 vpx_memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
292 MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
293 vpx_memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
294 MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
296 vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
298 vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
299 cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols));
301 vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
302 vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
307 static void restore_coding_context(VP9_COMP *cpi) {
308 CODING_CONTEXT *const cc = &cpi->coding_context;
309 VP9_COMMON *cm = &cpi->common;
311 // Restore key state variables to the snapshot state stored in the
312 // previous call to vp9_save_coding_context.
313 vp9_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
315 vpx_memcpy(cpi->nmvcosts[0], cc->nmvcosts[0],
316 MV_VALS * sizeof(*cc->nmvcosts[0]));
317 vpx_memcpy(cpi->nmvcosts[1], cc->nmvcosts[1],
318 MV_VALS * sizeof(*cc->nmvcosts[1]));
319 vpx_memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
320 MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
321 vpx_memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
322 MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
324 vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
326 vpx_memcpy(cm->last_frame_seg_map,
327 cpi->coding_context.last_frame_seg_map_copy,
328 (cm->mi_rows * cm->mi_cols));
330 vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
331 vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
336 static void configure_static_seg_features(VP9_COMP *cpi) {
337 VP9_COMMON *const cm = &cpi->common;
338 const RATE_CONTROL *const rc = &cpi->rc;
339 struct segmentation *const seg = &cm->seg;
341 int high_q = (int)(rc->avg_q > 48.0);
344 // Disable and clear down for KF
345 if (cm->frame_type == KEY_FRAME) {
346 // Clear down the global segmentation map
347 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
349 seg->update_data = 0;
350 cpi->static_mb_pct = 0;
352 // Disable segmentation
353 vp9_disable_segmentation(seg);
355 // Clear down the segment features.
356 vp9_clearall_segfeatures(seg);
357 } else if (cpi->refresh_alt_ref_frame) {
358 // If this is an alt ref frame
359 // Clear down the global segmentation map
360 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
362 seg->update_data = 0;
363 cpi->static_mb_pct = 0;
365 // Disable segmentation and individual segment features by default
366 vp9_disable_segmentation(seg);
367 vp9_clearall_segfeatures(seg);
369 // Scan frames from current to arf frame.
370 // This function re-enables segmentation if appropriate.
371 vp9_update_mbgraph_stats(cpi);
373 // If segmentation was enabled set those features needed for the
377 seg->update_data = 1;
379 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
381 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
382 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
384 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
385 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
387 // Where relevant assume segment data is delta data
388 seg->abs_delta = SEGMENT_DELTADATA;
390 } else if (seg->enabled) {
391 // All other frames if segmentation has been enabled
393 // First normal frame in a valid gf or alt ref group
394 if (rc->frames_since_golden == 0) {
395 // Set up segment features for normal frames in an arf group
396 if (rc->source_alt_ref_active) {
398 seg->update_data = 1;
399 seg->abs_delta = SEGMENT_DELTADATA;
401 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
403 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
404 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
406 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
407 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
409 // Segment coding disabled for compred testing
410 if (high_q || (cpi->static_mb_pct == 100)) {
411 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
412 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
413 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
416 // Disable segmentation and clear down features if alt ref
417 // is not active for this group
419 vp9_disable_segmentation(seg);
421 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
424 seg->update_data = 0;
426 vp9_clearall_segfeatures(seg);
428 } else if (rc->is_src_frame_alt_ref) {
429 // Special case where we are coding over the top of a previous
431 // Segment coding disabled for compred testing
433 // Enable ref frame features for segment 0 as well
434 vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
435 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
437 // All mbs should use ALTREF_FRAME
438 vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
439 vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
440 vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
441 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
443 // Skip all MBs if high Q (0,0 mv and skip coeffs)
445 vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
446 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
448 // Enable data update
449 seg->update_data = 1;
453 // No updates.. leave things as they are.
455 seg->update_data = 0;
460 static void update_reference_segmentation_map(VP9_COMP *cpi) {
461 VP9_COMMON *const cm = &cpi->common;
462 MODE_INFO *mi_8x8_ptr = cm->mi;
463 uint8_t *cache_ptr = cm->last_frame_seg_map;
466 for (row = 0; row < cm->mi_rows; row++) {
467 MODE_INFO *mi_8x8 = mi_8x8_ptr;
468 uint8_t *cache = cache_ptr;
469 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
470 cache[0] = mi_8x8[0].src_mi->mbmi.segment_id;
471 mi_8x8_ptr += cm->mi_stride;
472 cache_ptr += cm->mi_cols;
476 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
477 VP9_COMMON *cm = &cpi->common;
478 const VP9EncoderConfig *oxcf = &cpi->oxcf;
480 cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
481 cm->subsampling_x, cm->subsampling_y,
482 #if CONFIG_VP9_HIGHBITDEPTH
483 cm->use_highbitdepth,
485 oxcf->lag_in_frames);
487 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
488 "Failed to allocate lag buffers");
490 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
491 oxcf->width, oxcf->height,
492 cm->subsampling_x, cm->subsampling_y,
493 #if CONFIG_VP9_HIGHBITDEPTH
494 cm->use_highbitdepth,
496 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
497 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
498 "Failed to allocate altref buffer");
501 static void alloc_ref_frame_buffers(VP9_COMP *cpi) {
502 VP9_COMMON *const cm = &cpi->common;
503 if (vp9_alloc_ref_frame_buffers(cm, cm->width, cm->height))
504 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
505 "Failed to allocate frame buffers");
508 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
509 VP9_COMMON *const cm = &cpi->common;
510 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
511 cm->width, cm->height,
512 cm->subsampling_x, cm->subsampling_y,
513 #if CONFIG_VP9_HIGHBITDEPTH
514 cm->use_highbitdepth,
516 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
517 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
518 "Failed to allocate last frame buffer");
520 if (vp9_realloc_frame_buffer(&cpi->scaled_source,
521 cm->width, cm->height,
522 cm->subsampling_x, cm->subsampling_y,
523 #if CONFIG_VP9_HIGHBITDEPTH
524 cm->use_highbitdepth,
526 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
527 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
528 "Failed to allocate scaled source buffer");
530 if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
531 cm->width, cm->height,
532 cm->subsampling_x, cm->subsampling_y,
533 #if CONFIG_VP9_HIGHBITDEPTH
534 cm->use_highbitdepth,
536 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
537 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
538 "Failed to allocate scaled last source buffer");
541 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
542 VP9_COMMON *cm = &cpi->common;
544 vp9_alloc_context_buffers(cm, cm->width, cm->height);
549 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
550 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
553 vp9_setup_pc_tree(&cpi->common, &cpi->td);
556 static void update_frame_size(VP9_COMP *cpi) {
557 VP9_COMMON *const cm = &cpi->common;
558 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
560 vp9_set_mb_mi(cm, cm->width, cm->height);
561 vp9_init_context_buffers(cm);
562 init_macroblockd(cm, xd);
564 if (is_two_pass_svc(cpi)) {
565 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
566 cm->width, cm->height,
567 cm->subsampling_x, cm->subsampling_y,
568 #if CONFIG_VP9_HIGHBITDEPTH
569 cm->use_highbitdepth,
571 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
572 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
573 "Failed to reallocate alt_ref_buffer");
577 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
578 cpi->framerate = framerate < 0.1 ? 30 : framerate;
579 vp9_rc_update_framerate(cpi);
582 static void set_tile_limits(VP9_COMP *cpi) {
583 VP9_COMMON *const cm = &cpi->common;
585 int min_log2_tile_cols, max_log2_tile_cols;
586 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
588 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
589 min_log2_tile_cols, max_log2_tile_cols);
590 cm->log2_tile_rows = cpi->oxcf.tile_rows;
593 static void init_buffer_indices(VP9_COMP *cpi) {
599 static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
600 VP9_COMMON *const cm = &cpi->common;
603 cpi->framerate = oxcf->init_framerate;
605 cm->profile = oxcf->profile;
606 cm->bit_depth = oxcf->bit_depth;
607 #if CONFIG_VP9_HIGHBITDEPTH
608 cm->use_highbitdepth = oxcf->use_highbitdepth;
610 cm->color_space = UNKNOWN;
612 cm->width = oxcf->width;
613 cm->height = oxcf->height;
614 vp9_alloc_compressor_data(cpi);
616 // Single thread case: use counts in common.
617 cpi->td.counts = &cm->counts;
619 // Spatial scalability.
620 cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
621 // Temporal scalability.
622 cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
624 if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
625 ((cpi->svc.number_temporal_layers > 1 ||
626 cpi->svc.number_spatial_layers > 1) &&
627 cpi->oxcf.pass != 1)) {
628 vp9_init_layer_context(cpi);
631 // change includes all joint functionality
632 vp9_change_config(cpi, oxcf);
634 cpi->static_mb_pct = 0;
635 cpi->ref_frame_flags = 0;
637 init_buffer_indices(cpi);
640 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
641 const VP9EncoderConfig *oxcf) {
642 const int64_t bandwidth = oxcf->target_bandwidth;
643 const int64_t starting = oxcf->starting_buffer_level_ms;
644 const int64_t optimal = oxcf->optimal_buffer_level_ms;
645 const int64_t maximum = oxcf->maximum_buffer_size_ms;
647 rc->starting_buffer_level = starting * bandwidth / 1000;
648 rc->optimal_buffer_level = (optimal == 0) ? bandwidth / 8
649 : optimal * bandwidth / 1000;
650 rc->maximum_buffer_size = (maximum == 0) ? bandwidth / 8
651 : maximum * bandwidth / 1000;
654 #if CONFIG_VP9_HIGHBITDEPTH
655 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
656 cpi->fn_ptr[BT].sdf = SDF; \
657 cpi->fn_ptr[BT].sdaf = SDAF; \
658 cpi->fn_ptr[BT].vf = VF; \
659 cpi->fn_ptr[BT].svf = SVF; \
660 cpi->fn_ptr[BT].svaf = SVAF; \
661 cpi->fn_ptr[BT].sdx3f = SDX3F; \
662 cpi->fn_ptr[BT].sdx8f = SDX8F; \
663 cpi->fn_ptr[BT].sdx4df = SDX4DF;
665 #define MAKE_BFP_SAD_WRAPPER(fnname) \
666 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
668 const uint8_t *ref_ptr, \
670 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
672 static unsigned int fnname##_bits10(const uint8_t *src_ptr, \
674 const uint8_t *ref_ptr, \
676 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
678 static unsigned int fnname##_bits12(const uint8_t *src_ptr, \
680 const uint8_t *ref_ptr, \
682 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
685 #define MAKE_BFP_SADAVG_WRAPPER(fnname) static unsigned int \
686 fnname##_bits8(const uint8_t *src_ptr, \
688 const uint8_t *ref_ptr, \
690 const uint8_t *second_pred) { \
691 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
693 static unsigned int fnname##_bits10(const uint8_t *src_ptr, \
695 const uint8_t *ref_ptr, \
697 const uint8_t *second_pred) { \
698 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
701 static unsigned int fnname##_bits12(const uint8_t *src_ptr, \
703 const uint8_t *ref_ptr, \
705 const uint8_t *second_pred) { \
706 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
710 #define MAKE_BFP_SAD3_WRAPPER(fnname) \
711 static void fnname##_bits8(const uint8_t *src_ptr, \
713 const uint8_t *ref_ptr, \
715 unsigned int *sad_array) { \
716 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
718 static void fnname##_bits10(const uint8_t *src_ptr, \
720 const uint8_t *ref_ptr, \
722 unsigned int *sad_array) { \
724 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
725 for (i = 0; i < 3; i++) \
726 sad_array[i] >>= 2; \
728 static void fnname##_bits12(const uint8_t *src_ptr, \
730 const uint8_t *ref_ptr, \
732 unsigned int *sad_array) { \
734 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
735 for (i = 0; i < 3; i++) \
736 sad_array[i] >>= 4; \
739 #define MAKE_BFP_SAD8_WRAPPER(fnname) \
740 static void fnname##_bits8(const uint8_t *src_ptr, \
742 const uint8_t *ref_ptr, \
744 unsigned int *sad_array) { \
745 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
747 static void fnname##_bits10(const uint8_t *src_ptr, \
749 const uint8_t *ref_ptr, \
751 unsigned int *sad_array) { \
753 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
754 for (i = 0; i < 8; i++) \
755 sad_array[i] >>= 2; \
757 static void fnname##_bits12(const uint8_t *src_ptr, \
759 const uint8_t *ref_ptr, \
761 unsigned int *sad_array) { \
763 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
764 for (i = 0; i < 8; i++) \
765 sad_array[i] >>= 4; \
767 #define MAKE_BFP_SAD4D_WRAPPER(fnname) \
768 static void fnname##_bits8(const uint8_t *src_ptr, \
770 const uint8_t* const ref_ptr[], \
772 unsigned int *sad_array) { \
773 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
775 static void fnname##_bits10(const uint8_t *src_ptr, \
777 const uint8_t* const ref_ptr[], \
779 unsigned int *sad_array) { \
781 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
782 for (i = 0; i < 4; i++) \
783 sad_array[i] >>= 2; \
785 static void fnname##_bits12(const uint8_t *src_ptr, \
787 const uint8_t* const ref_ptr[], \
789 unsigned int *sad_array) { \
791 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
792 for (i = 0; i < 4; i++) \
793 sad_array[i] >>= 4; \
796 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad32x16)
797 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad32x16_avg)
798 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad32x16x4d)
799 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad16x32)
800 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad16x32_avg)
801 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad16x32x4d)
802 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad64x32)
803 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad64x32_avg)
804 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad64x32x4d)
805 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad32x64)
806 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad32x64_avg)
807 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad32x64x4d)
808 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad32x32)
809 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad32x32_avg)
810 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad32x32x3)
811 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad32x32x8)
812 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad32x32x4d)
813 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad64x64)
814 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad64x64_avg)
815 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad64x64x3)
816 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad64x64x8)
817 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad64x64x4d)
818 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad16x16)
819 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad16x16_avg)
820 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad16x16x3)
821 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad16x16x8)
822 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad16x16x4d)
823 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad16x8)
824 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad16x8_avg)
825 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad16x8x3)
826 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad16x8x8)
827 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad16x8x4d)
828 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad8x16)
829 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad8x16_avg)
830 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad8x16x3)
831 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad8x16x8)
832 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad8x16x4d)
833 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad8x8)
834 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad8x8_avg)
835 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad8x8x3)
836 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad8x8x8)
837 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad8x8x4d)
838 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad8x4)
839 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad8x4_avg)
840 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad8x4x8)
841 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad8x4x4d)
842 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad4x8)
843 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad4x8_avg)
844 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad4x8x8)
845 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad4x8x4d)
846 MAKE_BFP_SAD_WRAPPER(vp9_highbd_sad4x4)
847 MAKE_BFP_SADAVG_WRAPPER(vp9_highbd_sad4x4_avg)
848 MAKE_BFP_SAD3_WRAPPER(vp9_highbd_sad4x4x3)
849 MAKE_BFP_SAD8_WRAPPER(vp9_highbd_sad4x4x8)
850 MAKE_BFP_SAD4D_WRAPPER(vp9_highbd_sad4x4x4d)
852 static void highbd_set_var_fns(VP9_COMP *const cpi) {
853 VP9_COMMON *const cm = &cpi->common;
854 if (cm->use_highbitdepth) {
855 switch (cm->bit_depth) {
857 HIGHBD_BFP(BLOCK_32X16,
858 vp9_highbd_sad32x16_bits8,
859 vp9_highbd_sad32x16_avg_bits8,
860 vp9_highbd_variance32x16,
861 vp9_highbd_sub_pixel_variance32x16,
862 vp9_highbd_sub_pixel_avg_variance32x16,
865 vp9_highbd_sad32x16x4d_bits8)
867 HIGHBD_BFP(BLOCK_16X32,
868 vp9_highbd_sad16x32_bits8,
869 vp9_highbd_sad16x32_avg_bits8,
870 vp9_highbd_variance16x32,
871 vp9_highbd_sub_pixel_variance16x32,
872 vp9_highbd_sub_pixel_avg_variance16x32,
875 vp9_highbd_sad16x32x4d_bits8)
877 HIGHBD_BFP(BLOCK_64X32,
878 vp9_highbd_sad64x32_bits8,
879 vp9_highbd_sad64x32_avg_bits8,
880 vp9_highbd_variance64x32,
881 vp9_highbd_sub_pixel_variance64x32,
882 vp9_highbd_sub_pixel_avg_variance64x32,
885 vp9_highbd_sad64x32x4d_bits8)
887 HIGHBD_BFP(BLOCK_32X64,
888 vp9_highbd_sad32x64_bits8,
889 vp9_highbd_sad32x64_avg_bits8,
890 vp9_highbd_variance32x64,
891 vp9_highbd_sub_pixel_variance32x64,
892 vp9_highbd_sub_pixel_avg_variance32x64,
895 vp9_highbd_sad32x64x4d_bits8)
897 HIGHBD_BFP(BLOCK_32X32,
898 vp9_highbd_sad32x32_bits8,
899 vp9_highbd_sad32x32_avg_bits8,
900 vp9_highbd_variance32x32,
901 vp9_highbd_sub_pixel_variance32x32,
902 vp9_highbd_sub_pixel_avg_variance32x32,
903 vp9_highbd_sad32x32x3_bits8,
904 vp9_highbd_sad32x32x8_bits8,
905 vp9_highbd_sad32x32x4d_bits8)
907 HIGHBD_BFP(BLOCK_64X64,
908 vp9_highbd_sad64x64_bits8,
909 vp9_highbd_sad64x64_avg_bits8,
910 vp9_highbd_variance64x64,
911 vp9_highbd_sub_pixel_variance64x64,
912 vp9_highbd_sub_pixel_avg_variance64x64,
913 vp9_highbd_sad64x64x3_bits8,
914 vp9_highbd_sad64x64x8_bits8,
915 vp9_highbd_sad64x64x4d_bits8)
917 HIGHBD_BFP(BLOCK_16X16,
918 vp9_highbd_sad16x16_bits8,
919 vp9_highbd_sad16x16_avg_bits8,
920 vp9_highbd_variance16x16,
921 vp9_highbd_sub_pixel_variance16x16,
922 vp9_highbd_sub_pixel_avg_variance16x16,
923 vp9_highbd_sad16x16x3_bits8,
924 vp9_highbd_sad16x16x8_bits8,
925 vp9_highbd_sad16x16x4d_bits8)
927 HIGHBD_BFP(BLOCK_16X8,
928 vp9_highbd_sad16x8_bits8,
929 vp9_highbd_sad16x8_avg_bits8,
930 vp9_highbd_variance16x8,
931 vp9_highbd_sub_pixel_variance16x8,
932 vp9_highbd_sub_pixel_avg_variance16x8,
933 vp9_highbd_sad16x8x3_bits8,
934 vp9_highbd_sad16x8x8_bits8,
935 vp9_highbd_sad16x8x4d_bits8)
937 HIGHBD_BFP(BLOCK_8X16,
938 vp9_highbd_sad8x16_bits8,
939 vp9_highbd_sad8x16_avg_bits8,
940 vp9_highbd_variance8x16,
941 vp9_highbd_sub_pixel_variance8x16,
942 vp9_highbd_sub_pixel_avg_variance8x16,
943 vp9_highbd_sad8x16x3_bits8,
944 vp9_highbd_sad8x16x8_bits8,
945 vp9_highbd_sad8x16x4d_bits8)
947 HIGHBD_BFP(BLOCK_8X8,
948 vp9_highbd_sad8x8_bits8,
949 vp9_highbd_sad8x8_avg_bits8,
950 vp9_highbd_variance8x8,
951 vp9_highbd_sub_pixel_variance8x8,
952 vp9_highbd_sub_pixel_avg_variance8x8,
953 vp9_highbd_sad8x8x3_bits8,
954 vp9_highbd_sad8x8x8_bits8,
955 vp9_highbd_sad8x8x4d_bits8)
957 HIGHBD_BFP(BLOCK_8X4,
958 vp9_highbd_sad8x4_bits8,
959 vp9_highbd_sad8x4_avg_bits8,
960 vp9_highbd_variance8x4,
961 vp9_highbd_sub_pixel_variance8x4,
962 vp9_highbd_sub_pixel_avg_variance8x4,
964 vp9_highbd_sad8x4x8_bits8,
965 vp9_highbd_sad8x4x4d_bits8)
967 HIGHBD_BFP(BLOCK_4X8,
968 vp9_highbd_sad4x8_bits8,
969 vp9_highbd_sad4x8_avg_bits8,
970 vp9_highbd_variance4x8,
971 vp9_highbd_sub_pixel_variance4x8,
972 vp9_highbd_sub_pixel_avg_variance4x8,
974 vp9_highbd_sad4x8x8_bits8,
975 vp9_highbd_sad4x8x4d_bits8)
977 HIGHBD_BFP(BLOCK_4X4,
978 vp9_highbd_sad4x4_bits8,
979 vp9_highbd_sad4x4_avg_bits8,
980 vp9_highbd_variance4x4,
981 vp9_highbd_sub_pixel_variance4x4,
982 vp9_highbd_sub_pixel_avg_variance4x4,
983 vp9_highbd_sad4x4x3_bits8,
984 vp9_highbd_sad4x4x8_bits8,
985 vp9_highbd_sad4x4x4d_bits8)
989 HIGHBD_BFP(BLOCK_32X16,
990 vp9_highbd_sad32x16_bits10,
991 vp9_highbd_sad32x16_avg_bits10,
992 vp9_highbd_10_variance32x16,
993 vp9_highbd_10_sub_pixel_variance32x16,
994 vp9_highbd_10_sub_pixel_avg_variance32x16,
997 vp9_highbd_sad32x16x4d_bits10)
999 HIGHBD_BFP(BLOCK_16X32,
1000 vp9_highbd_sad16x32_bits10,
1001 vp9_highbd_sad16x32_avg_bits10,
1002 vp9_highbd_10_variance16x32,
1003 vp9_highbd_10_sub_pixel_variance16x32,
1004 vp9_highbd_10_sub_pixel_avg_variance16x32,
1007 vp9_highbd_sad16x32x4d_bits10)
1009 HIGHBD_BFP(BLOCK_64X32,
1010 vp9_highbd_sad64x32_bits10,
1011 vp9_highbd_sad64x32_avg_bits10,
1012 vp9_highbd_10_variance64x32,
1013 vp9_highbd_10_sub_pixel_variance64x32,
1014 vp9_highbd_10_sub_pixel_avg_variance64x32,
1017 vp9_highbd_sad64x32x4d_bits10)
1019 HIGHBD_BFP(BLOCK_32X64,
1020 vp9_highbd_sad32x64_bits10,
1021 vp9_highbd_sad32x64_avg_bits10,
1022 vp9_highbd_10_variance32x64,
1023 vp9_highbd_10_sub_pixel_variance32x64,
1024 vp9_highbd_10_sub_pixel_avg_variance32x64,
1027 vp9_highbd_sad32x64x4d_bits10)
1029 HIGHBD_BFP(BLOCK_32X32,
1030 vp9_highbd_sad32x32_bits10,
1031 vp9_highbd_sad32x32_avg_bits10,
1032 vp9_highbd_10_variance32x32,
1033 vp9_highbd_10_sub_pixel_variance32x32,
1034 vp9_highbd_10_sub_pixel_avg_variance32x32,
1035 vp9_highbd_sad32x32x3_bits10,
1036 vp9_highbd_sad32x32x8_bits10,
1037 vp9_highbd_sad32x32x4d_bits10)
1039 HIGHBD_BFP(BLOCK_64X64,
1040 vp9_highbd_sad64x64_bits10,
1041 vp9_highbd_sad64x64_avg_bits10,
1042 vp9_highbd_10_variance64x64,
1043 vp9_highbd_10_sub_pixel_variance64x64,
1044 vp9_highbd_10_sub_pixel_avg_variance64x64,
1045 vp9_highbd_sad64x64x3_bits10,
1046 vp9_highbd_sad64x64x8_bits10,
1047 vp9_highbd_sad64x64x4d_bits10)
1049 HIGHBD_BFP(BLOCK_16X16,
1050 vp9_highbd_sad16x16_bits10,
1051 vp9_highbd_sad16x16_avg_bits10,
1052 vp9_highbd_10_variance16x16,
1053 vp9_highbd_10_sub_pixel_variance16x16,
1054 vp9_highbd_10_sub_pixel_avg_variance16x16,
1055 vp9_highbd_sad16x16x3_bits10,
1056 vp9_highbd_sad16x16x8_bits10,
1057 vp9_highbd_sad16x16x4d_bits10)
1059 HIGHBD_BFP(BLOCK_16X8,
1060 vp9_highbd_sad16x8_bits10,
1061 vp9_highbd_sad16x8_avg_bits10,
1062 vp9_highbd_10_variance16x8,
1063 vp9_highbd_10_sub_pixel_variance16x8,
1064 vp9_highbd_10_sub_pixel_avg_variance16x8,
1065 vp9_highbd_sad16x8x3_bits10,
1066 vp9_highbd_sad16x8x8_bits10,
1067 vp9_highbd_sad16x8x4d_bits10)
1069 HIGHBD_BFP(BLOCK_8X16,
1070 vp9_highbd_sad8x16_bits10,
1071 vp9_highbd_sad8x16_avg_bits10,
1072 vp9_highbd_10_variance8x16,
1073 vp9_highbd_10_sub_pixel_variance8x16,
1074 vp9_highbd_10_sub_pixel_avg_variance8x16,
1075 vp9_highbd_sad8x16x3_bits10,
1076 vp9_highbd_sad8x16x8_bits10,
1077 vp9_highbd_sad8x16x4d_bits10)
1079 HIGHBD_BFP(BLOCK_8X8,
1080 vp9_highbd_sad8x8_bits10,
1081 vp9_highbd_sad8x8_avg_bits10,
1082 vp9_highbd_10_variance8x8,
1083 vp9_highbd_10_sub_pixel_variance8x8,
1084 vp9_highbd_10_sub_pixel_avg_variance8x8,
1085 vp9_highbd_sad8x8x3_bits10,
1086 vp9_highbd_sad8x8x8_bits10,
1087 vp9_highbd_sad8x8x4d_bits10)
1089 HIGHBD_BFP(BLOCK_8X4,
1090 vp9_highbd_sad8x4_bits10,
1091 vp9_highbd_sad8x4_avg_bits10,
1092 vp9_highbd_10_variance8x4,
1093 vp9_highbd_10_sub_pixel_variance8x4,
1094 vp9_highbd_10_sub_pixel_avg_variance8x4,
1096 vp9_highbd_sad8x4x8_bits10,
1097 vp9_highbd_sad8x4x4d_bits10)
1099 HIGHBD_BFP(BLOCK_4X8,
1100 vp9_highbd_sad4x8_bits10,
1101 vp9_highbd_sad4x8_avg_bits10,
1102 vp9_highbd_10_variance4x8,
1103 vp9_highbd_10_sub_pixel_variance4x8,
1104 vp9_highbd_10_sub_pixel_avg_variance4x8,
1106 vp9_highbd_sad4x8x8_bits10,
1107 vp9_highbd_sad4x8x4d_bits10)
1109 HIGHBD_BFP(BLOCK_4X4,
1110 vp9_highbd_sad4x4_bits10,
1111 vp9_highbd_sad4x4_avg_bits10,
1112 vp9_highbd_10_variance4x4,
1113 vp9_highbd_10_sub_pixel_variance4x4,
1114 vp9_highbd_10_sub_pixel_avg_variance4x4,
1115 vp9_highbd_sad4x4x3_bits10,
1116 vp9_highbd_sad4x4x8_bits10,
1117 vp9_highbd_sad4x4x4d_bits10)
1121 HIGHBD_BFP(BLOCK_32X16,
1122 vp9_highbd_sad32x16_bits12,
1123 vp9_highbd_sad32x16_avg_bits12,
1124 vp9_highbd_12_variance32x16,
1125 vp9_highbd_12_sub_pixel_variance32x16,
1126 vp9_highbd_12_sub_pixel_avg_variance32x16,
1129 vp9_highbd_sad32x16x4d_bits12)
1131 HIGHBD_BFP(BLOCK_16X32,
1132 vp9_highbd_sad16x32_bits12,
1133 vp9_highbd_sad16x32_avg_bits12,
1134 vp9_highbd_12_variance16x32,
1135 vp9_highbd_12_sub_pixel_variance16x32,
1136 vp9_highbd_12_sub_pixel_avg_variance16x32,
1139 vp9_highbd_sad16x32x4d_bits12)
1141 HIGHBD_BFP(BLOCK_64X32,
1142 vp9_highbd_sad64x32_bits12,
1143 vp9_highbd_sad64x32_avg_bits12,
1144 vp9_highbd_12_variance64x32,
1145 vp9_highbd_12_sub_pixel_variance64x32,
1146 vp9_highbd_12_sub_pixel_avg_variance64x32,
1149 vp9_highbd_sad64x32x4d_bits12)
1151 HIGHBD_BFP(BLOCK_32X64,
1152 vp9_highbd_sad32x64_bits12,
1153 vp9_highbd_sad32x64_avg_bits12,
1154 vp9_highbd_12_variance32x64,
1155 vp9_highbd_12_sub_pixel_variance32x64,
1156 vp9_highbd_12_sub_pixel_avg_variance32x64,
1159 vp9_highbd_sad32x64x4d_bits12)
1161 HIGHBD_BFP(BLOCK_32X32,
1162 vp9_highbd_sad32x32_bits12,
1163 vp9_highbd_sad32x32_avg_bits12,
1164 vp9_highbd_12_variance32x32,
1165 vp9_highbd_12_sub_pixel_variance32x32,
1166 vp9_highbd_12_sub_pixel_avg_variance32x32,
1167 vp9_highbd_sad32x32x3_bits12,
1168 vp9_highbd_sad32x32x8_bits12,
1169 vp9_highbd_sad32x32x4d_bits12)
1171 HIGHBD_BFP(BLOCK_64X64,
1172 vp9_highbd_sad64x64_bits12,
1173 vp9_highbd_sad64x64_avg_bits12,
1174 vp9_highbd_12_variance64x64,
1175 vp9_highbd_12_sub_pixel_variance64x64,
1176 vp9_highbd_12_sub_pixel_avg_variance64x64,
1177 vp9_highbd_sad64x64x3_bits12,
1178 vp9_highbd_sad64x64x8_bits12,
1179 vp9_highbd_sad64x64x4d_bits12)
1181 HIGHBD_BFP(BLOCK_16X16,
1182 vp9_highbd_sad16x16_bits12,
1183 vp9_highbd_sad16x16_avg_bits12,
1184 vp9_highbd_12_variance16x16,
1185 vp9_highbd_12_sub_pixel_variance16x16,
1186 vp9_highbd_12_sub_pixel_avg_variance16x16,
1187 vp9_highbd_sad16x16x3_bits12,
1188 vp9_highbd_sad16x16x8_bits12,
1189 vp9_highbd_sad16x16x4d_bits12)
1191 HIGHBD_BFP(BLOCK_16X8,
1192 vp9_highbd_sad16x8_bits12,
1193 vp9_highbd_sad16x8_avg_bits12,
1194 vp9_highbd_12_variance16x8,
1195 vp9_highbd_12_sub_pixel_variance16x8,
1196 vp9_highbd_12_sub_pixel_avg_variance16x8,
1197 vp9_highbd_sad16x8x3_bits12,
1198 vp9_highbd_sad16x8x8_bits12,
1199 vp9_highbd_sad16x8x4d_bits12)
1201 HIGHBD_BFP(BLOCK_8X16,
1202 vp9_highbd_sad8x16_bits12,
1203 vp9_highbd_sad8x16_avg_bits12,
1204 vp9_highbd_12_variance8x16,
1205 vp9_highbd_12_sub_pixel_variance8x16,
1206 vp9_highbd_12_sub_pixel_avg_variance8x16,
1207 vp9_highbd_sad8x16x3_bits12,
1208 vp9_highbd_sad8x16x8_bits12,
1209 vp9_highbd_sad8x16x4d_bits12)
1211 HIGHBD_BFP(BLOCK_8X8,
1212 vp9_highbd_sad8x8_bits12,
1213 vp9_highbd_sad8x8_avg_bits12,
1214 vp9_highbd_12_variance8x8,
1215 vp9_highbd_12_sub_pixel_variance8x8,
1216 vp9_highbd_12_sub_pixel_avg_variance8x8,
1217 vp9_highbd_sad8x8x3_bits12,
1218 vp9_highbd_sad8x8x8_bits12,
1219 vp9_highbd_sad8x8x4d_bits12)
1221 HIGHBD_BFP(BLOCK_8X4,
1222 vp9_highbd_sad8x4_bits12,
1223 vp9_highbd_sad8x4_avg_bits12,
1224 vp9_highbd_12_variance8x4,
1225 vp9_highbd_12_sub_pixel_variance8x4,
1226 vp9_highbd_12_sub_pixel_avg_variance8x4,
1228 vp9_highbd_sad8x4x8_bits12,
1229 vp9_highbd_sad8x4x4d_bits12)
1231 HIGHBD_BFP(BLOCK_4X8,
1232 vp9_highbd_sad4x8_bits12,
1233 vp9_highbd_sad4x8_avg_bits12,
1234 vp9_highbd_12_variance4x8,
1235 vp9_highbd_12_sub_pixel_variance4x8,
1236 vp9_highbd_12_sub_pixel_avg_variance4x8,
1238 vp9_highbd_sad4x8x8_bits12,
1239 vp9_highbd_sad4x8x4d_bits12)
1241 HIGHBD_BFP(BLOCK_4X4,
1242 vp9_highbd_sad4x4_bits12,
1243 vp9_highbd_sad4x4_avg_bits12,
1244 vp9_highbd_12_variance4x4,
1245 vp9_highbd_12_sub_pixel_variance4x4,
1246 vp9_highbd_12_sub_pixel_avg_variance4x4,
1247 vp9_highbd_sad4x4x3_bits12,
1248 vp9_highbd_sad4x4x8_bits12,
1249 vp9_highbd_sad4x4x4d_bits12)
1253 assert(0 && "cm->bit_depth should be VPX_BITS_8, "
1254 "VPX_BITS_10 or VPX_BITS_12");
1258 #endif // CONFIG_VP9_HIGHBITDEPTH
1260 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
1261 VP9_COMMON *const cm = &cpi->common;
1262 RATE_CONTROL *const rc = &cpi->rc;
1264 if (cm->profile != oxcf->profile)
1265 cm->profile = oxcf->profile;
1266 cm->bit_depth = oxcf->bit_depth;
1268 if (cm->profile <= PROFILE_1)
1269 assert(cm->bit_depth == VPX_BITS_8);
1271 assert(cm->bit_depth > VPX_BITS_8);
1274 #if CONFIG_VP9_HIGHBITDEPTH
1275 cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
1276 #endif // CONFIG_VP9_HIGHBITDEPTH
1278 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1280 cpi->refresh_golden_frame = 0;
1281 cpi->refresh_last_frame = 1;
1282 cm->refresh_frame_context = 1;
1283 cm->reset_frame_context = 0;
1285 vp9_reset_segment_features(&cm->seg);
1286 vp9_set_high_precision_mv(cpi, 0);
1291 for (i = 0; i < MAX_SEGMENTS; i++)
1292 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1294 cpi->encode_breakout = cpi->oxcf.encode_breakout;
1296 set_rc_buffer_sizes(rc, &cpi->oxcf);
1298 // Under a configuration change, where maximum_buffer_size may change,
1299 // keep buffer level clipped to the maximum allowed buffer size.
1300 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
1301 rc->buffer_level = MIN(rc->buffer_level, rc->maximum_buffer_size);
1303 // Set up frame rate and related parameters rate control values.
1304 vp9_new_framerate(cpi, cpi->framerate);
1306 // Set absolute upper and lower quality limits
1307 rc->worst_quality = cpi->oxcf.worst_allowed_q;
1308 rc->best_quality = cpi->oxcf.best_allowed_q;
1310 cm->interp_filter = cpi->sf.default_interp_filter;
1312 cm->display_width = cpi->oxcf.width;
1313 cm->display_height = cpi->oxcf.height;
1315 if (cpi->initial_width) {
1316 // Increasing the size of the frame beyond the first seen frame, or some
1317 // otherwise signaled maximum size, is not supported.
1318 // TODO(jkoleszar): exit gracefully.
1319 assert(cm->width <= cpi->initial_width);
1320 assert(cm->height <= cpi->initial_height);
1322 update_frame_size(cpi);
1324 if ((cpi->svc.number_temporal_layers > 1 &&
1325 cpi->oxcf.rc_mode == VPX_CBR) ||
1326 ((cpi->svc.number_temporal_layers > 1 ||
1327 cpi->svc.number_spatial_layers > 1) &&
1328 cpi->oxcf.pass != 1)) {
1329 vp9_update_layer_context_change_config(cpi,
1330 (int)cpi->oxcf.target_bandwidth);
1333 cpi->alt_ref_source = NULL;
1334 rc->is_src_frame_alt_ref = 0;
1337 // Experimental RD Code
1338 cpi->frame_distortion = 0;
1339 cpi->last_frame_distortion = 0;
1342 set_tile_limits(cpi);
1344 cpi->ext_refresh_frame_flags_pending = 0;
1345 cpi->ext_refresh_frame_context_pending = 0;
1347 #if CONFIG_VP9_HIGHBITDEPTH
1348 highbd_set_var_fns(cpi);
1351 #if CONFIG_VP9_TEMPORAL_DENOISING
1352 if (cpi->oxcf.noise_sensitivity > 0) {
1353 vp9_denoiser_alloc(&(cpi->denoiser), cm->width, cm->height,
1354 cm->subsampling_x, cm->subsampling_y,
1355 #if CONFIG_VP9_HIGHBITDEPTH
1356 cm->use_highbitdepth,
1358 VP9_ENC_BORDER_IN_PIXELS);
1364 #define M_LOG2_E 0.693147180559945309417
1366 #define log2f(x) (log (x) / (float) M_LOG2_E)
1368 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1369 mvjointsadcost[0] = 600;
1370 mvjointsadcost[1] = 300;
1371 mvjointsadcost[2] = 300;
1372 mvjointsadcost[3] = 300;
1375 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1378 mvsadcost[0][0] = 0;
1379 mvsadcost[1][0] = 0;
1382 double z = 256 * (2 * (log2f(8 * i) + .6));
1383 mvsadcost[0][i] = (int)z;
1384 mvsadcost[1][i] = (int)z;
1385 mvsadcost[0][-i] = (int)z;
1386 mvsadcost[1][-i] = (int)z;
1387 } while (++i <= MV_MAX);
1390 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1393 mvsadcost[0][0] = 0;
1394 mvsadcost[1][0] = 0;
1397 double z = 256 * (2 * (log2f(8 * i) + .6));
1398 mvsadcost[0][i] = (int)z;
1399 mvsadcost[1][i] = (int)z;
1400 mvsadcost[0][-i] = (int)z;
1401 mvsadcost[1][-i] = (int)z;
1402 } while (++i <= MV_MAX);
1406 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf) {
1408 VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP));
1409 VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL;
1416 if (setjmp(cm->error.jmp)) {
1417 cm->error.setjmp = 0;
1418 vp9_remove_compressor(cpi);
1422 cm->error.setjmp = 1;
1423 cm->alloc_mi = vp9_enc_alloc_mi;
1424 cm->free_mi = vp9_enc_free_mi;
1425 cm->setup_mi = vp9_enc_setup_mi;
1427 CHECK_MEM_ERROR(cm, cm->fc,
1428 (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
1429 CHECK_MEM_ERROR(cm, cm->frame_contexts,
1430 (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS,
1431 sizeof(*cm->frame_contexts)));
1435 init_config(cpi, oxcf);
1436 vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
1438 cm->current_video_frame = 0;
1439 cpi->partition_search_skippable_frame = 0;
1440 cpi->tile_data = NULL;
1442 // Create the encoder segmentation map and set all entries to 0
1443 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1444 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1446 // Create a complexity map used for rd adjustment
1447 CHECK_MEM_ERROR(cm, cpi->complexity_map,
1448 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1450 // Create a map used for cyclic background refresh.
1451 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
1452 vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
1454 // And a place holder structure is the coding context
1455 // for use if we want to save and restore it
1456 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1457 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1459 CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
1460 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
1461 CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
1462 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
1463 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
1464 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
1465 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
1466 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
1467 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
1468 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
1469 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
1470 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
1471 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
1472 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
1473 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
1474 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
1476 for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
1477 sizeof(cpi->mbgraph_stats[0])); i++) {
1478 CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
1479 vpx_calloc(cm->MBs *
1480 sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1483 #if CONFIG_FP_MB_STATS
1484 cpi->use_fp_mb_stats = 0;
1485 if (cpi->use_fp_mb_stats) {
1486 // a place holder used to store the first pass mb stats in the first pass
1487 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
1488 vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
1490 cpi->twopass.frame_mb_stats_buf = NULL;
1494 cpi->refresh_alt_ref_frame = 0;
1495 cpi->multi_arf_last_grp_enabled = 0;
1497 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1498 #if CONFIG_INTERNAL_STATS
1499 cpi->b_calculate_ssimg = 0;
1504 if (cpi->b_calculate_psnr) {
1509 cpi->total_sq_error = 0;
1510 cpi->total_samples = 0;
1512 cpi->totalp_y = 0.0;
1513 cpi->totalp_u = 0.0;
1514 cpi->totalp_v = 0.0;
1516 cpi->totalp_sq_error = 0;
1517 cpi->totalp_samples = 0;
1519 cpi->tot_recode_hits = 0;
1520 cpi->summed_quality = 0;
1521 cpi->summed_weights = 0;
1522 cpi->summedp_quality = 0;
1523 cpi->summedp_weights = 0;
1526 if (cpi->b_calculate_ssimg) {
1527 cpi->total_ssimg_y = 0;
1528 cpi->total_ssimg_u = 0;
1529 cpi->total_ssimg_v = 0;
1530 cpi->total_ssimg_all = 0;
1535 cpi->first_time_stamp_ever = INT64_MAX;
1537 cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
1538 cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
1539 cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
1540 cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
1541 cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
1542 cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
1544 cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
1545 cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
1546 cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
1547 cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
1548 cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
1550 #if CONFIG_VP9_TEMPORAL_DENOISING
1551 #ifdef OUTPUT_YUV_DENOISED
1552 yuv_denoised_file = fopen("denoised.yuv", "ab");
1555 #ifdef OUTPUT_YUV_REC
1556 yuv_rec_file = fopen("rec.yuv", "wb");
1560 framepsnr = fopen("framepsnr.stt", "a");
1561 kf_list = fopen("kf_list.stt", "w");
1564 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
1566 if (oxcf->pass == 1) {
1567 vp9_init_first_pass(cpi);
1568 } else if (oxcf->pass == 2) {
1569 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
1570 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1572 if (cpi->svc.number_spatial_layers > 1
1573 || cpi->svc.number_temporal_layers > 1) {
1574 FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
1575 FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0};
1578 for (i = 0; i < oxcf->ss_number_layers; ++i) {
1579 FIRSTPASS_STATS *const last_packet_for_layer =
1580 &stats[packets - oxcf->ss_number_layers + i];
1581 const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
1582 const int packets_in_layer = (int)last_packet_for_layer->count + 1;
1583 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
1584 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
1586 vpx_free(lc->rc_twopass_stats_in.buf);
1588 lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
1589 CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
1590 vpx_malloc(lc->rc_twopass_stats_in.sz));
1591 lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
1592 lc->twopass.stats_in = lc->twopass.stats_in_start;
1593 lc->twopass.stats_in_end = lc->twopass.stats_in_start
1594 + packets_in_layer - 1;
1595 stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
1599 for (i = 0; i < packets; ++i) {
1600 const int layer_id = (int)stats[i].spatial_layer_id;
1601 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers
1602 && stats_copy[layer_id] != NULL) {
1603 *stats_copy[layer_id] = stats[i];
1604 ++stats_copy[layer_id];
1608 vp9_init_second_pass_spatial_svc(cpi);
1610 #if CONFIG_FP_MB_STATS
1611 if (cpi->use_fp_mb_stats) {
1612 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
1613 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
1615 cpi->twopass.firstpass_mb_stats.mb_stats_start =
1616 oxcf->firstpass_mb_stats_in.buf;
1617 cpi->twopass.firstpass_mb_stats.mb_stats_end =
1618 cpi->twopass.firstpass_mb_stats.mb_stats_start +
1619 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
1623 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1624 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1625 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
1627 vp9_init_second_pass(cpi);
1631 vp9_set_speed_features_framesize_independent(cpi);
1632 vp9_set_speed_features_framesize_dependent(cpi);
1634 // Allocate memory to store variances for a frame.
1635 CHECK_MEM_ERROR(cm, cpi->source_diff_var,
1636 vpx_calloc(cm->MBs, sizeof(diff)));
1637 cpi->source_var_thresh = 0;
1638 cpi->frames_till_next_var_check = 0;
1640 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF)\
1641 cpi->fn_ptr[BT].sdf = SDF; \
1642 cpi->fn_ptr[BT].sdaf = SDAF; \
1643 cpi->fn_ptr[BT].vf = VF; \
1644 cpi->fn_ptr[BT].svf = SVF; \
1645 cpi->fn_ptr[BT].svaf = SVAF; \
1646 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1647 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1648 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1650 BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
1651 vp9_variance32x16, vp9_sub_pixel_variance32x16,
1652 vp9_sub_pixel_avg_variance32x16, NULL, NULL, vp9_sad32x16x4d)
1654 BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
1655 vp9_variance16x32, vp9_sub_pixel_variance16x32,
1656 vp9_sub_pixel_avg_variance16x32, NULL, NULL, vp9_sad16x32x4d)
1658 BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
1659 vp9_variance64x32, vp9_sub_pixel_variance64x32,
1660 vp9_sub_pixel_avg_variance64x32, NULL, NULL, vp9_sad64x32x4d)
1662 BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
1663 vp9_variance32x64, vp9_sub_pixel_variance32x64,
1664 vp9_sub_pixel_avg_variance32x64, NULL, NULL, vp9_sad32x64x4d)
1666 BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
1667 vp9_variance32x32, vp9_sub_pixel_variance32x32,
1668 vp9_sub_pixel_avg_variance32x32, vp9_sad32x32x3, vp9_sad32x32x8,
1671 BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
1672 vp9_variance64x64, vp9_sub_pixel_variance64x64,
1673 vp9_sub_pixel_avg_variance64x64, vp9_sad64x64x3, vp9_sad64x64x8,
1676 BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
1677 vp9_variance16x16, vp9_sub_pixel_variance16x16,
1678 vp9_sub_pixel_avg_variance16x16, vp9_sad16x16x3, vp9_sad16x16x8,
1681 BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
1682 vp9_variance16x8, vp9_sub_pixel_variance16x8,
1683 vp9_sub_pixel_avg_variance16x8,
1684 vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
1686 BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
1687 vp9_variance8x16, vp9_sub_pixel_variance8x16,
1688 vp9_sub_pixel_avg_variance8x16,
1689 vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
1691 BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
1692 vp9_variance8x8, vp9_sub_pixel_variance8x8,
1693 vp9_sub_pixel_avg_variance8x8,
1694 vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
1696 BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
1697 vp9_variance8x4, vp9_sub_pixel_variance8x4,
1698 vp9_sub_pixel_avg_variance8x4, NULL, vp9_sad8x4x8, vp9_sad8x4x4d)
1700 BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
1701 vp9_variance4x8, vp9_sub_pixel_variance4x8,
1702 vp9_sub_pixel_avg_variance4x8, NULL, vp9_sad4x8x8, vp9_sad4x8x4d)
1704 BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
1705 vp9_variance4x4, vp9_sub_pixel_variance4x4,
1706 vp9_sub_pixel_avg_variance4x4,
1707 vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
1709 #if CONFIG_VP9_HIGHBITDEPTH
1710 highbd_set_var_fns(cpi);
1713 /* vp9_init_quantizer() is first called here. Add check in
1714 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1715 * called later when needed. This will avoid unnecessary calls of
1716 * vp9_init_quantizer() for every frame.
1718 vp9_init_quantizer(cpi);
1720 vp9_loop_filter_init(cm);
1722 cm->error.setjmp = 0;
1727 void vp9_remove_compressor(VP9_COMP *cpi) {
1728 VP9_COMMON *const cm = &cpi->common;
1734 if (cpi && (cm->current_video_frame > 0)) {
1735 #if CONFIG_INTERNAL_STATS
1737 vp9_clear_system_state();
1739 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1740 if (cpi->oxcf.pass != 1) {
1741 FILE *f = fopen("opsnr.stt", "a");
1742 double time_encoded = (cpi->last_end_time_stamp_seen
1743 - cpi->first_time_stamp_ever) / 10000000.000;
1744 double total_encode_time = (cpi->time_receive_data +
1745 cpi->time_compress_data) / 1000.000;
1747 (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
1748 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
1750 if (cpi->b_calculate_psnr) {
1751 const double total_psnr =
1752 vpx_sse_to_psnr((double)cpi->total_samples, peak,
1753 (double)cpi->total_sq_error);
1754 const double totalp_psnr =
1755 vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
1756 (double)cpi->totalp_sq_error);
1757 const double total_ssim = 100 * pow(cpi->summed_quality /
1758 cpi->summed_weights, 8.0);
1759 const double totalp_ssim = 100 * pow(cpi->summedp_quality /
1760 cpi->summedp_weights, 8.0);
1762 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1763 "VPXSSIM\tVPSSIMP\t Time(ms)\n");
1764 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",
1765 dr, cpi->total / cpi->count, total_psnr,
1766 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
1770 if (cpi->b_calculate_ssimg) {
1771 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n");
1772 fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
1773 cpi->total_ssimg_y / cpi->count,
1774 cpi->total_ssimg_u / cpi->count,
1775 cpi->total_ssimg_v / cpi->count,
1776 cpi->total_ssimg_all / cpi->count, total_encode_time);
1786 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
1787 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
1788 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
1789 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
1790 cpi->time_compress_data / 1000,
1791 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
1796 #if CONFIG_VP9_TEMPORAL_DENOISING
1797 if (cpi->oxcf.noise_sensitivity > 0) {
1798 vp9_denoiser_free(&(cpi->denoiser));
1802 dealloc_compressor_data(cpi);
1805 for (i = 0; i < sizeof(cpi->mbgraph_stats) /
1806 sizeof(cpi->mbgraph_stats[0]); ++i) {
1807 vpx_free(cpi->mbgraph_stats[i].mb_stats);
1810 #if CONFIG_FP_MB_STATS
1811 if (cpi->use_fp_mb_stats) {
1812 vpx_free(cpi->twopass.frame_mb_stats_buf);
1813 cpi->twopass.frame_mb_stats_buf = NULL;
1817 vp9_remove_common(cm);
1820 #if CONFIG_VP9_TEMPORAL_DENOISING
1821 #ifdef OUTPUT_YUV_DENOISED
1822 fclose(yuv_denoised_file);
1825 #ifdef OUTPUT_YUV_REC
1826 fclose(yuv_rec_file);
1843 static int64_t get_sse(const uint8_t *a, int a_stride,
1844 const uint8_t *b, int b_stride,
1845 int width, int height) {
1846 const int dw = width % 16;
1847 const int dh = height % 16;
1848 int64_t total_sse = 0;
1849 unsigned int sse = 0;
1854 variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
1855 dw, height, &sse, &sum);
1860 variance(&a[(height - dh) * a_stride], a_stride,
1861 &b[(height - dh) * b_stride], b_stride,
1862 width - dw, dh, &sse, &sum);
1866 for (y = 0; y < height / 16; ++y) {
1867 const uint8_t *pa = a;
1868 const uint8_t *pb = b;
1869 for (x = 0; x < width / 16; ++x) {
1870 vp9_mse16x16(pa, a_stride, pb, b_stride, &sse);
1884 #if CONFIG_VP9_HIGHBITDEPTH
1885 static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
1886 const uint8_t *b8, int b_stride,
1887 int width, int height,
1888 unsigned int input_shift) {
1889 const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
1890 const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
1891 int64_t total_sse = 0;
1893 for (y = 0; y < height; ++y) {
1894 for (x = 0; x < width; ++x) {
1896 diff = (a[x] >> input_shift) - (b[x] >> input_shift);
1897 total_sse += diff * diff;
1905 static int64_t highbd_get_sse(const uint8_t *a, int a_stride,
1906 const uint8_t *b, int b_stride,
1907 int width, int height) {
1908 int64_t total_sse = 0;
1910 const int dw = width % 16;
1911 const int dh = height % 16;
1912 unsigned int sse = 0;
1915 highbd_variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
1916 dw, height, &sse, &sum);
1920 highbd_variance(&a[(height - dh) * a_stride], a_stride,
1921 &b[(height - dh) * b_stride], b_stride,
1922 width - dw, dh, &sse, &sum);
1925 for (y = 0; y < height / 16; ++y) {
1926 const uint8_t *pa = a;
1927 const uint8_t *pb = b;
1928 for (x = 0; x < width / 16; ++x) {
1929 vp9_highbd_mse16x16(pa, a_stride, pb, b_stride, &sse);
1939 #endif // CONFIG_VP9_HIGHBITDEPTH
1942 double psnr[4]; // total/y/u/v
1943 uint64_t sse[4]; // total/y/u/v
1944 uint32_t samples[4]; // total/y/u/v
1947 static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
1949 static const double peak = 255.0;
1950 const int widths[3] = {a->y_width, a->uv_width, a->uv_width };
1951 const int heights[3] = {a->y_height, a->uv_height, a->uv_height};
1952 const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer, a->v_buffer };
1953 const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride};
1954 const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer, b->v_buffer };
1955 const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride};
1957 uint64_t total_sse = 0;
1958 uint32_t total_samples = 0;
1960 for (i = 0; i < 3; ++i) {
1961 const int w = widths[i];
1962 const int h = heights[i];
1963 const uint32_t samples = w * h;
1964 const uint64_t sse = get_sse(a_planes[i], a_strides[i],
1965 b_planes[i], b_strides[i],
1967 psnr->sse[1 + i] = sse;
1968 psnr->samples[1 + i] = samples;
1969 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
1972 total_samples += samples;
1975 psnr->sse[0] = total_sse;
1976 psnr->samples[0] = total_samples;
1977 psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, peak,
1981 #if CONFIG_VP9_HIGHBITDEPTH
1982 static void calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
1983 const YV12_BUFFER_CONFIG *b,
1985 unsigned int bit_depth,
1986 unsigned int in_bit_depth) {
1987 const int widths[3] = {a->y_width, a->uv_width, a->uv_width };
1988 const int heights[3] = {a->y_height, a->uv_height, a->uv_height};
1989 const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer, a->v_buffer };
1990 const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride};
1991 const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer, b->v_buffer };
1992 const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride};
1994 uint64_t total_sse = 0;
1995 uint32_t total_samples = 0;
1996 const double peak = (double)((1 << in_bit_depth) - 1);
1997 const unsigned int input_shift = bit_depth - in_bit_depth;
1999 for (i = 0; i < 3; ++i) {
2000 const int w = widths[i];
2001 const int h = heights[i];
2002 const uint32_t samples = w * h;
2004 if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
2006 sse = highbd_get_sse_shift(a_planes[i], a_strides[i],
2007 b_planes[i], b_strides[i], w, h,
2010 sse = highbd_get_sse(a_planes[i], a_strides[i],
2011 b_planes[i], b_strides[i], w, h);
2014 sse = get_sse(a_planes[i], a_strides[i],
2015 b_planes[i], b_strides[i],
2018 psnr->sse[1 + i] = sse;
2019 psnr->samples[1 + i] = samples;
2020 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
2023 total_samples += samples;
2026 psnr->sse[0] = total_sse;
2027 psnr->samples[0] = total_samples;
2028 psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, peak,
2031 #endif // CONFIG_VP9_HIGHBITDEPTH
2033 static void generate_psnr_packet(VP9_COMP *cpi) {
2034 struct vpx_codec_cx_pkt pkt;
2037 #if CONFIG_VP9_HIGHBITDEPTH
2038 calc_highbd_psnr(cpi->Source, cpi->common.frame_to_show, &psnr,
2039 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2041 calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
2044 for (i = 0; i < 4; ++i) {
2045 pkt.data.psnr.samples[i] = psnr.samples[i];
2046 pkt.data.psnr.sse[i] = psnr.sse[i];
2047 pkt.data.psnr.psnr[i] = psnr.psnr[i];
2049 pkt.kind = VPX_CODEC_PSNR_PKT;
2050 if (is_two_pass_svc(cpi))
2051 cpi->svc.layer_context[cpi->svc.spatial_layer_id].psnr_pkt = pkt.data.psnr;
2053 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2056 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
2057 if (ref_frame_flags > 7)
2060 cpi->ref_frame_flags = ref_frame_flags;
2064 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
2065 cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
2066 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
2067 cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
2068 cpi->ext_refresh_frame_flags_pending = 1;
2071 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
2072 VP9_REFFRAME ref_frame_flag) {
2073 MV_REFERENCE_FRAME ref_frame = NONE;
2074 if (ref_frame_flag == VP9_LAST_FLAG)
2075 ref_frame = LAST_FRAME;
2076 else if (ref_frame_flag == VP9_GOLD_FLAG)
2077 ref_frame = GOLDEN_FRAME;
2078 else if (ref_frame_flag == VP9_ALT_FLAG)
2079 ref_frame = ALTREF_FRAME;
2081 return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2084 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2085 YV12_BUFFER_CONFIG *sd) {
2086 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2088 vp8_yv12_copy_frame(cfg, sd);
2095 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2096 YV12_BUFFER_CONFIG *sd) {
2097 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2099 vp8_yv12_copy_frame(sd, cfg);
2106 int vp9_update_entropy(VP9_COMP * cpi, int update) {
2107 cpi->ext_refresh_frame_context = update;
2108 cpi->ext_refresh_frame_context_pending = 1;
2112 #if CONFIG_VP9_TEMPORAL_DENOISING
2113 #if defined(OUTPUT_YUV_DENOISED)
2114 // The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
2115 // as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
2116 // not denoise the UV channels at this time. If ever we implement UV channel
2117 // denoising we will have to modify this.
2118 void vp9_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
2119 uint8_t *src = s->y_buffer;
2120 int h = s->y_height;
2123 fwrite(src, s->y_width, 1, f);
2128 h = s->uv_height / 2;
2131 fwrite(src, s->uv_width / 2, 1, f);
2132 src += s->uv_stride + s->uv_width / 2;
2136 h = s->uv_height / 2;
2139 fwrite(src, s->uv_width / 2, 1, f);
2140 src += s->uv_stride + s->uv_width / 2;
2146 #ifdef OUTPUT_YUV_REC
2147 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2148 YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2149 uint8_t *src = s->y_buffer;
2152 #if CONFIG_VP9_HIGHBITDEPTH
2153 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
2154 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
2157 fwrite(src16, s->y_width, 2, yuv_rec_file);
2158 src16 += s->y_stride;
2161 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
2165 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2166 src16 += s->uv_stride;
2169 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
2173 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2174 src16 += s->uv_stride;
2177 fflush(yuv_rec_file);
2180 #endif // CONFIG_VP9_HIGHBITDEPTH
2183 fwrite(src, s->y_width, 1, yuv_rec_file);
2191 fwrite(src, s->uv_width, 1, yuv_rec_file);
2192 src += s->uv_stride;
2199 fwrite(src, s->uv_width, 1, yuv_rec_file);
2200 src += s->uv_stride;
2203 fflush(yuv_rec_file);
2207 #if CONFIG_VP9_HIGHBITDEPTH
2208 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2209 YV12_BUFFER_CONFIG *dst,
2212 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2213 YV12_BUFFER_CONFIG *dst) {
2214 #endif // CONFIG_VP9_HIGHBITDEPTH
2215 // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
2217 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
2218 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
2219 const int src_widths[3] = {src->y_crop_width, src->uv_crop_width,
2220 src->uv_crop_width };
2221 const int src_heights[3] = {src->y_crop_height, src->uv_crop_height,
2222 src->uv_crop_height};
2223 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
2224 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
2225 const int dst_widths[3] = {dst->y_crop_width, dst->uv_crop_width,
2226 dst->uv_crop_width};
2227 const int dst_heights[3] = {dst->y_crop_height, dst->uv_crop_height,
2228 dst->uv_crop_height};
2230 for (i = 0; i < MAX_MB_PLANE; ++i) {
2231 #if CONFIG_VP9_HIGHBITDEPTH
2232 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2233 vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
2234 src_strides[i], dsts[i], dst_heights[i],
2235 dst_widths[i], dst_strides[i], bd);
2237 vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2238 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2241 vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2242 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2243 #endif // CONFIG_VP9_HIGHBITDEPTH
2245 vp9_extend_frame_borders(dst);
2248 #if CONFIG_VP9_HIGHBITDEPTH
2249 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2250 YV12_BUFFER_CONFIG *dst, int bd) {
2252 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2253 YV12_BUFFER_CONFIG *dst) {
2254 #endif // CONFIG_VP9_HIGHBITDEPTH
2255 const int src_w = src->y_crop_width;
2256 const int src_h = src->y_crop_height;
2257 const int dst_w = dst->y_crop_width;
2258 const int dst_h = dst->y_crop_height;
2259 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
2260 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
2261 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
2262 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
2263 const InterpKernel *const kernel = vp9_get_interp_kernel(EIGHTTAP);
2266 for (y = 0; y < dst_h; y += 16) {
2267 for (x = 0; x < dst_w; x += 16) {
2268 for (i = 0; i < MAX_MB_PLANE; ++i) {
2269 const int factor = (i == 0 || i == 3 ? 1 : 2);
2270 const int x_q4 = x * (16 / factor) * src_w / dst_w;
2271 const int y_q4 = y * (16 / factor) * src_h / dst_h;
2272 const int src_stride = src_strides[i];
2273 const int dst_stride = dst_strides[i];
2274 const uint8_t *src_ptr = srcs[i] + (y / factor) * src_h / dst_h *
2275 src_stride + (x / factor) * src_w / dst_w;
2276 uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
2278 #if CONFIG_VP9_HIGHBITDEPTH
2279 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2280 vp9_highbd_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2281 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2282 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2283 16 / factor, 16 / factor, bd);
2285 vp9_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2286 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2287 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2288 16 / factor, 16 / factor);
2291 vp9_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2292 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2293 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2294 16 / factor, 16 / factor);
2295 #endif // CONFIG_VP9_HIGHBITDEPTH
2300 vp9_extend_frame_borders(dst);
2303 // Function to test for conditions that indicate we should loop
2304 // back and recode a frame.
2305 static int recode_loop_test(const VP9_COMP *cpi,
2306 int high_limit, int low_limit,
2307 int q, int maxq, int minq) {
2308 const RATE_CONTROL *const rc = &cpi->rc;
2309 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2310 int force_recode = 0;
2312 // Special case trap if maximum allowed frame size exceeded.
2313 if (rc->projected_frame_size > rc->max_frame_bandwidth) {
2316 // Is frame recode allowed.
2317 // Yes if either recode mode 1 is selected or mode 2 is selected
2318 // and the frame is a key frame, golden frame or alt_ref_frame
2319 } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
2320 ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
2321 frame_is_kf_gf_arf(cpi))) {
2322 // General over and under shoot tests
2323 if ((rc->projected_frame_size > high_limit && q < maxq) ||
2324 (rc->projected_frame_size < low_limit && q > minq)) {
2326 } else if (cpi->oxcf.rc_mode == VPX_CQ) {
2327 // Deal with frame undershoot and whether or not we are
2328 // below the automatically set cq level.
2329 if (q > oxcf->cq_level &&
2330 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2335 return force_recode;
2338 void vp9_update_reference_frames(VP9_COMP *cpi) {
2339 VP9_COMMON * const cm = &cpi->common;
2341 // At this point the new frame has been encoded.
2342 // If any buffer copy / swapping is signaled it should be done here.
2343 if (cm->frame_type == KEY_FRAME) {
2344 ref_cnt_fb(cm->frame_bufs,
2345 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2346 ref_cnt_fb(cm->frame_bufs,
2347 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2348 } else if (vp9_preserve_existing_gf(cpi)) {
2349 // We have decided to preserve the previously existing golden frame as our
2350 // new ARF frame. However, in the short term in function
2351 // vp9_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
2352 // we're updating the GF with the current decoded frame, we save it to the
2353 // ARF slot instead.
2354 // We now have to update the ARF with the current frame and swap gld_fb_idx
2355 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
2356 // slot and, if we're updating the GF, the current frame becomes the new GF.
2359 ref_cnt_fb(cm->frame_bufs,
2360 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2362 tmp = cpi->alt_fb_idx;
2363 cpi->alt_fb_idx = cpi->gld_fb_idx;
2364 cpi->gld_fb_idx = tmp;
2366 if (is_two_pass_svc(cpi)) {
2367 cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
2368 cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
2370 } else { /* For non key/golden frames */
2371 if (cpi->refresh_alt_ref_frame) {
2372 int arf_idx = cpi->alt_fb_idx;
2373 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
2374 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2375 arf_idx = gf_group->arf_update_idx[gf_group->index];
2378 ref_cnt_fb(cm->frame_bufs,
2379 &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2380 vpx_memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
2381 cpi->interp_filter_selected[0],
2382 sizeof(cpi->interp_filter_selected[0]));
2385 if (cpi->refresh_golden_frame) {
2386 ref_cnt_fb(cm->frame_bufs,
2387 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2388 if (!cpi->rc.is_src_frame_alt_ref)
2389 vpx_memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2390 cpi->interp_filter_selected[0],
2391 sizeof(cpi->interp_filter_selected[0]));
2393 vpx_memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2394 cpi->interp_filter_selected[ALTREF_FRAME],
2395 sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
2399 if (cpi->refresh_last_frame) {
2400 ref_cnt_fb(cm->frame_bufs,
2401 &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
2402 if (!cpi->rc.is_src_frame_alt_ref)
2403 vpx_memcpy(cpi->interp_filter_selected[LAST_FRAME],
2404 cpi->interp_filter_selected[0],
2405 sizeof(cpi->interp_filter_selected[0]));
2407 #if CONFIG_VP9_TEMPORAL_DENOISING
2408 if (cpi->oxcf.noise_sensitivity > 0) {
2409 vp9_denoiser_update_frame_info(&cpi->denoiser,
2411 cpi->common.frame_type,
2412 cpi->refresh_alt_ref_frame,
2413 cpi->refresh_golden_frame,
2414 cpi->refresh_last_frame);
2419 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2420 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2421 struct loopfilter *lf = &cm->lf;
2423 lf->filter_level = 0;
2425 struct vpx_usec_timer timer;
2427 vp9_clear_system_state();
2429 vpx_usec_timer_start(&timer);
2431 vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
2433 vpx_usec_timer_mark(&timer);
2434 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2437 if (lf->filter_level > 0) {
2438 vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
2441 vp9_extend_frame_inner_borders(cm->frame_to_show);
2444 void vp9_scale_references(VP9_COMP *cpi) {
2445 VP9_COMMON *cm = &cpi->common;
2446 MV_REFERENCE_FRAME ref_frame;
2447 const VP9_REFFRAME ref_mask[3] = {VP9_LAST_FLAG, VP9_GOLD_FLAG, VP9_ALT_FLAG};
2449 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2450 // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
2451 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
2452 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2453 const YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf;
2455 #if CONFIG_VP9_HIGHBITDEPTH
2456 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2457 const int new_fb = get_free_fb(cm);
2458 cm->cur_frame = &cm->frame_bufs[new_fb];
2459 vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
2460 cm->width, cm->height,
2461 cm->subsampling_x, cm->subsampling_y,
2462 cm->use_highbitdepth,
2463 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2464 scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf,
2465 (int)cm->bit_depth);
2467 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2468 const int new_fb = get_free_fb(cm);
2469 vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
2470 cm->width, cm->height,
2471 cm->subsampling_x, cm->subsampling_y,
2472 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2473 scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf);
2474 #endif // CONFIG_VP9_HIGHBITDEPTH
2475 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2476 if (cm->frame_bufs[new_fb].mvs == NULL ||
2477 cm->frame_bufs[new_fb].mi_rows < cm->mi_rows ||
2478 cm->frame_bufs[new_fb].mi_cols < cm->mi_cols) {
2479 cm->frame_bufs[new_fb].mvs =
2480 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
2481 sizeof(*cm->frame_bufs[new_fb].mvs));
2482 cm->frame_bufs[new_fb].mi_rows = cm->mi_rows;
2483 cm->frame_bufs[new_fb].mi_cols = cm->mi_cols;
2486 cpi->scaled_ref_idx[ref_frame - 1] = idx;
2487 ++cm->frame_bufs[idx].ref_count;
2490 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_REF_BUFFER_IDX;
2495 static void release_scaled_references(VP9_COMP *cpi) {
2496 VP9_COMMON *cm = &cpi->common;
2498 for (i = 0; i < MAX_REF_FRAMES; ++i) {
2499 const int idx = cpi->scaled_ref_idx[i];
2500 RefCntBuffer *const buf =
2501 idx != INVALID_REF_BUFFER_IDX ? &cm->frame_bufs[idx] : NULL;
2504 cpi->scaled_ref_idx[i] = INVALID_REF_BUFFER_IDX;
2509 static void full_to_model_count(unsigned int *model_count,
2510 unsigned int *full_count) {
2512 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
2513 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
2514 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
2515 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
2516 model_count[TWO_TOKEN] += full_count[n];
2517 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
2520 static void full_to_model_counts(vp9_coeff_count_model *model_count,
2521 vp9_coeff_count *full_count) {
2524 for (i = 0; i < PLANE_TYPES; ++i)
2525 for (j = 0; j < REF_TYPES; ++j)
2526 for (k = 0; k < COEF_BANDS; ++k)
2527 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
2528 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
2531 #if 0 && CONFIG_INTERNAL_STATS
2532 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
2533 VP9_COMMON *const cm = &cpi->common;
2534 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
2537 vp9_clear_system_state();
2539 recon_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2541 if (cpi->twopass.total_left_stats.coded_error != 0.0)
2542 fprintf(f, "%10u %10d %10d %10d %10d"
2543 "%10"PRId64" %10"PRId64" %10"PRId64" %10"PRId64" %10d "
2544 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2545 "%6d %6d %5d %5d %5d "
2546 "%10"PRId64" %10.3lf"
2547 "%10lf %8u %10d %10d %10d\n",
2548 cpi->common.current_video_frame, cpi->rc.this_frame_target,
2549 cpi->rc.projected_frame_size,
2550 cpi->rc.projected_frame_size / cpi->common.MBs,
2551 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
2552 cpi->rc.vbr_bits_off_target,
2553 cpi->rc.total_target_vs_actual,
2554 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
2555 cpi->rc.total_actual_bits, cm->base_qindex,
2556 vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
2557 (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
2558 vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
2561 vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
2562 cpi->refresh_last_frame, cpi->refresh_golden_frame,
2563 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
2564 cpi->twopass.bits_left,
2565 cpi->twopass.total_left_stats.coded_error,
2566 cpi->twopass.bits_left /
2567 (1 + cpi->twopass.total_left_stats.coded_error),
2568 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
2569 cpi->twopass.kf_zeromotion_pct);
2574 FILE *const fmodes = fopen("Modes.stt", "a");
2577 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
2578 cm->frame_type, cpi->refresh_golden_frame,
2579 cpi->refresh_alt_ref_frame);
2581 for (i = 0; i < MAX_MODES; ++i)
2582 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
2584 fprintf(fmodes, "\n");
2591 static void set_mv_search_params(VP9_COMP *cpi) {
2592 const VP9_COMMON *const cm = &cpi->common;
2593 const unsigned int max_mv_def = MIN(cm->width, cm->height);
2595 // Default based on max resolution.
2596 cpi->mv_step_param = vp9_init_search_range(max_mv_def);
2598 if (cpi->sf.mv.auto_mv_step_size) {
2599 if (frame_is_intra_only(cm)) {
2600 // Initialize max_mv_magnitude for use in the first INTER frame
2601 // after a key/intra-only frame.
2602 cpi->max_mv_magnitude = max_mv_def;
2604 if (cm->show_frame) {
2605 // Allow mv_steps to correspond to twice the max mv magnitude found
2606 // in the previous frame, capped by the default max_mv_magnitude based
2608 cpi->mv_step_param =
2609 vp9_init_search_range(MIN(max_mv_def, 2 * cpi->max_mv_magnitude));
2611 cpi->max_mv_magnitude = 0;
2616 static void set_size_independent_vars(VP9_COMP *cpi) {
2617 vp9_set_speed_features_framesize_independent(cpi);
2618 vp9_set_rd_speed_thresholds(cpi);
2619 vp9_set_rd_speed_thresholds_sub8x8(cpi);
2620 cpi->common.interp_filter = cpi->sf.default_interp_filter;
2623 static void set_size_dependent_vars(VP9_COMP *cpi, int *q,
2624 int *bottom_index, int *top_index) {
2625 VP9_COMMON *const cm = &cpi->common;
2626 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2628 // Setup variables that depend on the dimensions of the frame.
2629 vp9_set_speed_features_framesize_dependent(cpi);
2631 // Decide q and q bounds.
2632 *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
2634 if (!frame_is_intra_only(cm)) {
2635 vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
2638 // Configure experimental use of segmentation for enhanced coding of
2639 // static regions if indicated.
2640 // Only allowed in the second pass of a two pass encode, as it requires
2641 // lagged coding, and if the relevant speed feature flag is set.
2642 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
2643 configure_static_seg_features(cpi);
2645 #if CONFIG_VP9_POSTPROC
2646 if (oxcf->noise_sensitivity > 0) {
2648 switch (oxcf->noise_sensitivity) {
2666 vp9_denoise(cpi->Source, cpi->Source, l);
2668 #endif // CONFIG_VP9_POSTPROC
2671 static void init_motion_estimation(VP9_COMP *cpi) {
2672 int y_stride = cpi->scaled_source.y_stride;
2674 if (cpi->sf.mv.search_method == NSTEP) {
2675 vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
2676 } else if (cpi->sf.mv.search_method == DIAMOND) {
2677 vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
2681 void set_frame_size(VP9_COMP *cpi) {
2683 VP9_COMMON *const cm = &cpi->common;
2684 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2685 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
2687 if (oxcf->pass == 2 &&
2688 cm->current_video_frame == 0 &&
2689 oxcf->resize_mode == RESIZE_FIXED &&
2690 oxcf->rc_mode == VPX_VBR) {
2691 // Internal scaling is triggered on the first frame.
2692 vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
2693 oxcf->scaled_frame_height);
2696 if ((oxcf->pass == 2) &&
2698 (is_two_pass_svc(cpi) &&
2699 cpi->svc.encode_empty_frame_state != ENCODING))) {
2700 vp9_set_target_rate(cpi);
2703 // Reset the frame pointers to the current frame size.
2704 vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
2705 cm->width, cm->height,
2706 cm->subsampling_x, cm->subsampling_y,
2707 #if CONFIG_VP9_HIGHBITDEPTH
2708 cm->use_highbitdepth,
2710 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2712 alloc_util_frame_buffers(cpi);
2713 init_motion_estimation(cpi);
2715 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2716 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2717 YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf;
2718 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
2721 #if CONFIG_VP9_HIGHBITDEPTH
2722 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
2723 buf->y_crop_width, buf->y_crop_height,
2724 cm->width, cm->height,
2725 (buf->flags & YV12_FLAG_HIGHBITDEPTH) ?
2728 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
2729 buf->y_crop_width, buf->y_crop_height,
2730 cm->width, cm->height);
2731 #endif // CONFIG_VP9_HIGHBITDEPTH
2732 if (vp9_is_scaled(&ref_buf->sf))
2733 vp9_extend_frame_borders(buf);
2736 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
2739 static void encode_without_recode_loop(VP9_COMP *cpi) {
2740 VP9_COMMON *const cm = &cpi->common;
2741 int q, bottom_index, top_index; // Dummy variables.
2743 vp9_clear_system_state();
2745 set_frame_size(cpi);
2747 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
2748 &cpi->scaled_source);
2750 if (cpi->unscaled_last_source != NULL)
2751 cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
2752 &cpi->scaled_last_source);
2754 if (frame_is_intra_only(cm) == 0) {
2755 vp9_scale_references(cpi);
2758 set_size_independent_vars(cpi);
2759 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
2761 vp9_set_quantizer(cm, q);
2763 // Variance adaptive and in frame q adjustment experiments are mutually
2765 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2766 vp9_vaq_frame_setup(cpi);
2767 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2768 vp9_setup_in_frame_q_adj(cpi);
2769 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
2770 vp9_cyclic_refresh_setup(cpi);
2772 // transform / motion compensation build reconstruction frame
2773 vp9_encode_frame(cpi);
2775 // Update the skip mb flag probabilities based on the distribution
2776 // seen in the last encoder iteration.
2777 // update_base_skip_probs(cpi);
2778 vp9_clear_system_state();
2781 static void encode_with_recode_loop(VP9_COMP *cpi,
2784 VP9_COMMON *const cm = &cpi->common;
2785 RATE_CONTROL *const rc = &cpi->rc;
2786 int bottom_index, top_index;
2789 int overshoot_seen = 0;
2790 int undershoot_seen = 0;
2791 int frame_over_shoot_limit;
2792 int frame_under_shoot_limit;
2793 int q = 0, q_low = 0, q_high = 0;
2794 int frame_size_changed = 0;
2796 set_size_independent_vars(cpi);
2799 vp9_clear_system_state();
2801 set_frame_size(cpi);
2803 if (loop_count == 0 || frame_size_changed != 0) {
2804 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
2805 q_low = bottom_index;
2808 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
2809 set_mv_search_params(cpi);
2812 // Decide frame size bounds
2813 vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
2814 &frame_under_shoot_limit,
2815 &frame_over_shoot_limit);
2817 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
2818 &cpi->scaled_source);
2820 if (cpi->unscaled_last_source != NULL)
2821 cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
2822 &cpi->scaled_last_source);
2824 if (frame_is_intra_only(cm) == 0) {
2825 if (loop_count > 0) {
2826 release_scaled_references(cpi);
2828 vp9_scale_references(cpi);
2831 vp9_set_quantizer(cm, q);
2833 if (loop_count == 0)
2836 // Variance adaptive and in frame q adjustment experiments are mutually
2838 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2839 vp9_vaq_frame_setup(cpi);
2840 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2841 vp9_setup_in_frame_q_adj(cpi);
2844 // transform / motion compensation build reconstruction frame
2845 vp9_encode_frame(cpi);
2847 // Update the skip mb flag probabilities based on the distribution
2848 // seen in the last encoder iteration.
2849 // update_base_skip_probs(cpi);
2851 vp9_clear_system_state();
2853 // Dummy pack of the bitstream using up to date stats to get an
2854 // accurate estimate of output frame size to determine if we need
2856 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
2857 save_coding_context(cpi);
2858 if (!cpi->sf.use_nonrd_pick_mode)
2859 vp9_pack_bitstream(cpi, dest, size);
2861 rc->projected_frame_size = (int)(*size) << 3;
2862 restore_coding_context(cpi);
2864 if (frame_over_shoot_limit == 0)
2865 frame_over_shoot_limit = 1;
2868 if (cpi->oxcf.rc_mode == VPX_Q) {
2871 if ((cm->frame_type == KEY_FRAME) &&
2872 rc->this_key_frame_forced &&
2873 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
2877 int high_err_target = cpi->ambient_err;
2878 int low_err_target = cpi->ambient_err >> 1;
2880 #if CONFIG_VP9_HIGHBITDEPTH
2881 if (cm->use_highbitdepth) {
2882 kf_err = vp9_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm),
2885 kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2888 kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2889 #endif // CONFIG_VP9_HIGHBITDEPTH
2891 // Prevent possible divide by zero error below for perfect KF
2894 // The key frame is not good enough or we can afford
2895 // to make it better without undue risk of popping.
2896 if ((kf_err > high_err_target &&
2897 rc->projected_frame_size <= frame_over_shoot_limit) ||
2898 (kf_err > low_err_target &&
2899 rc->projected_frame_size <= frame_under_shoot_limit)) {
2901 q_high = q > q_low ? q - 1 : q_low;
2904 q = (q * high_err_target) / kf_err;
2905 q = MIN(q, (q_high + q_low) >> 1);
2906 } else if (kf_err < low_err_target &&
2907 rc->projected_frame_size >= frame_under_shoot_limit) {
2908 // The key frame is much better than the previous frame
2910 q_low = q < q_high ? q + 1 : q_high;
2913 q = (q * low_err_target) / kf_err;
2914 q = MIN(q, (q_high + q_low + 1) >> 1);
2917 // Clamp Q to upper and lower limits:
2918 q = clamp(q, q_low, q_high);
2921 } else if (recode_loop_test(
2922 cpi, frame_over_shoot_limit, frame_under_shoot_limit,
2923 q, MAX(q_high, top_index), bottom_index)) {
2924 // Is the projected frame size out of range and are we allowed
2925 // to attempt to recode.
2929 // Frame size out of permitted range:
2930 // Update correction factor & compute new Q to try...
2932 // Frame is too large
2933 if (rc->projected_frame_size > rc->this_frame_target) {
2934 // Special case if the projected size is > the max allowed.
2935 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
2936 q_high = rc->worst_quality;
2938 // Raise Qlow as to at least the current value
2939 q_low = q < q_high ? q + 1 : q_high;
2941 if (undershoot_seen || loop_count > 1) {
2942 // Update rate_correction_factor unless
2943 vp9_rc_update_rate_correction_factors(cpi, 1);
2945 q = (q_high + q_low + 1) / 2;
2947 // Update rate_correction_factor unless
2948 vp9_rc_update_rate_correction_factors(cpi, 0);
2950 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2951 bottom_index, MAX(q_high, top_index));
2953 while (q < q_low && retries < 10) {
2954 vp9_rc_update_rate_correction_factors(cpi, 0);
2955 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2956 bottom_index, MAX(q_high, top_index));
2963 // Frame is too small
2964 q_high = q > q_low ? q - 1 : q_low;
2966 if (overshoot_seen || loop_count > 1) {
2967 vp9_rc_update_rate_correction_factors(cpi, 1);
2968 q = (q_high + q_low) / 2;
2970 vp9_rc_update_rate_correction_factors(cpi, 0);
2971 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2972 bottom_index, top_index);
2973 // Special case reset for qlow for constrained quality.
2974 // This should only trigger where there is very substantial
2975 // undershoot on a frame and the auto cq level is above
2976 // the user passsed in value.
2977 if (cpi->oxcf.rc_mode == VPX_CQ &&
2982 while (q > q_high && retries < 10) {
2983 vp9_rc_update_rate_correction_factors(cpi, 0);
2984 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2985 bottom_index, top_index);
2990 undershoot_seen = 1;
2993 // Clamp Q to upper and lower limits:
2994 q = clamp(q, q_low, q_high);
3002 // Special case for overlay frame.
3003 if (rc->is_src_frame_alt_ref &&
3004 rc->projected_frame_size < rc->max_frame_bandwidth)
3010 #if CONFIG_INTERNAL_STATS
3011 cpi->tot_recode_hits++;
3017 static int get_ref_frame_flags(const VP9_COMP *cpi) {
3018 const int *const map = cpi->common.ref_frame_map;
3019 const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
3020 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
3021 const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
3022 int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3025 flags &= ~VP9_GOLD_FLAG;
3027 if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
3028 (cpi->svc.number_temporal_layers == 1 &&
3029 cpi->svc.number_spatial_layers == 1))
3030 flags &= ~VP9_GOLD_FLAG;
3033 flags &= ~VP9_ALT_FLAG;
3036 flags &= ~VP9_ALT_FLAG;
3041 static void set_ext_overrides(VP9_COMP *cpi) {
3042 // Overrides the defaults with the externally supplied values with
3043 // vp9_update_reference() and vp9_update_entropy() calls
3044 // Note: The overrides are valid only for the next frame passed
3045 // to encode_frame_to_data_rate() function
3046 if (cpi->ext_refresh_frame_context_pending) {
3047 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
3048 cpi->ext_refresh_frame_context_pending = 0;
3050 if (cpi->ext_refresh_frame_flags_pending) {
3051 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
3052 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
3053 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
3054 cpi->ext_refresh_frame_flags_pending = 0;
3058 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
3059 YV12_BUFFER_CONFIG *unscaled,
3060 YV12_BUFFER_CONFIG *scaled) {
3061 if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
3062 cm->mi_rows * MI_SIZE != unscaled->y_height) {
3063 #if CONFIG_VP9_HIGHBITDEPTH
3064 scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
3066 scale_and_extend_frame_nonnormative(unscaled, scaled);
3067 #endif // CONFIG_VP9_HIGHBITDEPTH
3074 static void set_arf_sign_bias(VP9_COMP *cpi) {
3075 VP9_COMMON *const cm = &cpi->common;
3078 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3079 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3080 arf_sign_bias = cpi->rc.source_alt_ref_active &&
3081 (!cpi->refresh_alt_ref_frame ||
3082 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
3085 (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
3087 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
3090 int setup_interp_filter_search_mask(VP9_COMP *cpi) {
3091 INTERP_FILTER ifilter;
3092 int ref_total[MAX_REF_FRAMES] = {0};
3093 MV_REFERENCE_FRAME ref;
3095 if (cpi->common.last_frame_type == KEY_FRAME ||
3096 cpi->refresh_alt_ref_frame)
3098 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
3099 for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
3100 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
3102 for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
3103 if ((ref_total[LAST_FRAME] &&
3104 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
3105 (ref_total[GOLDEN_FRAME] == 0 ||
3106 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50
3107 < ref_total[GOLDEN_FRAME]) &&
3108 (ref_total[ALTREF_FRAME] == 0 ||
3109 cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50
3110 < ref_total[ALTREF_FRAME]))
3111 mask |= 1 << ifilter;
3116 static void encode_frame_to_data_rate(VP9_COMP *cpi,
3119 unsigned int *frame_flags) {
3120 VP9_COMMON *const cm = &cpi->common;
3121 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3122 struct segmentation *const seg = &cm->seg;
3125 set_ext_overrides(cpi);
3127 vp9_clear_system_state();
3129 // Enable or disable mode based tweaking of the zbin.
3130 // For 2 pass only used where GF/ARF prediction quality
3131 // is above a threshold.
3132 cpi->zbin_mode_boost = 0;
3133 cpi->zbin_mode_boost_enabled = 0;
3135 // Set the arf sign bias for this frame.
3136 set_arf_sign_bias(cpi);
3138 // Set default state for segment based loop filter update flags.
3139 cm->lf.mode_ref_delta_update = 0;
3141 if (cpi->oxcf.pass == 2 &&
3142 cpi->sf.adaptive_interp_filter_search)
3143 cpi->sf.interp_filter_search_mask =
3144 setup_interp_filter_search_mask(cpi);
3146 // Set various flags etc to special state if it is a key frame.
3147 if (frame_is_intra_only(cm)) {
3148 // Reset the loop filter deltas and segmentation map.
3149 vp9_reset_segment_features(&cm->seg);
3151 // If segmentation is enabled force a map update for key frames.
3153 seg->update_map = 1;
3154 seg->update_data = 1;
3157 // The alternate reference frame cannot be active for a key frame.
3158 cpi->rc.source_alt_ref_active = 0;
3160 cm->error_resilient_mode = oxcf->error_resilient_mode;
3162 // By default, encoder assumes decoder can use prev_mi.
3163 if (cm->error_resilient_mode) {
3164 cm->frame_parallel_decoding_mode = 1;
3165 cm->reset_frame_context = 0;
3166 cm->refresh_frame_context = 0;
3167 } else if (cm->intra_only) {
3168 cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
3169 // Only reset the current context.
3170 cm->reset_frame_context = 2;
3173 if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
3174 // Use the last frame context for the empty frame.
3175 cm->frame_context_idx =
3176 (cpi->svc.encode_empty_frame_state == ENCODING) ? FRAME_CONTEXTS - 1 :
3177 cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
3178 cpi->svc.temporal_layer_id;
3180 // The probs will be updated based on the frame type of its previous
3181 // frame if frame_parallel_decoding_mode is 0. The type may vary for
3182 // the frame after a key frame in base layer since we may drop enhancement
3183 // layers. So set frame_parallel_decoding_mode to 1 in this case.
3184 if (cpi->svc.number_temporal_layers == 1) {
3185 if (cpi->svc.spatial_layer_id == 0 &&
3186 cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
3187 cm->frame_parallel_decoding_mode = 1;
3189 cm->frame_parallel_decoding_mode = 0;
3190 } else if (cpi->svc.spatial_layer_id == 0) {
3191 // Find the 2nd frame in temporal base layer and 1st frame in temporal
3192 // enhancement layers from the key frame.
3194 for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
3195 if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
3196 cm->frame_parallel_decoding_mode = 1;
3200 if (i == cpi->svc.number_temporal_layers)
3201 cm->frame_parallel_decoding_mode = 0;
3205 // For 1 pass CBR, check if we are dropping this frame.
3206 // Never drop on key frame.
3207 if (oxcf->pass == 0 &&
3208 oxcf->rc_mode == VPX_CBR &&
3209 cm->frame_type != KEY_FRAME) {
3210 if (vp9_rc_drop_frame(cpi)) {
3211 vp9_rc_postencode_update_drop_frame(cpi);
3212 ++cm->current_video_frame;
3217 vp9_clear_system_state();
3219 #if CONFIG_INTERNAL_STATS
3222 for (i = 0; i < MAX_MODES; ++i)
3223 cpi->mode_chosen_counts[i] = 0;
3227 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
3228 encode_without_recode_loop(cpi);
3230 encode_with_recode_loop(cpi, size, dest);
3233 #if CONFIG_VP9_TEMPORAL_DENOISING
3234 #ifdef OUTPUT_YUV_DENOISED
3235 if (oxcf->noise_sensitivity > 0) {
3236 vp9_write_yuv_frame_420(&cpi->denoiser.running_avg_y[INTRA_FRAME],
3243 // Special case code to reduce pulsing when key frames are forced at a
3244 // fixed interval. Note the reconstruction error if it is the frame before
3245 // the force key frame
3246 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
3247 #if CONFIG_VP9_HIGHBITDEPTH
3248 if (cm->use_highbitdepth) {
3249 cpi->ambient_err = vp9_highbd_get_y_sse(cpi->Source,
3250 get_frame_new_buffer(cm),
3253 cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3256 cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3257 #endif // CONFIG_VP9_HIGHBITDEPTH
3260 // If the encoder forced a KEY_FRAME decision
3261 if (cm->frame_type == KEY_FRAME)
3262 cpi->refresh_last_frame = 1;
3264 cm->frame_to_show = get_frame_new_buffer(cm);
3266 // Pick the loop filter level for the frame.
3267 loopfilter_frame(cpi, cm);
3269 // build the bitstream
3270 vp9_pack_bitstream(cpi, dest, size);
3272 if (cm->seg.update_map)
3273 update_reference_segmentation_map(cpi);
3275 if (frame_is_intra_only(cm) == 0) {
3276 release_scaled_references(cpi);
3278 vp9_update_reference_frames(cpi);
3280 for (t = TX_4X4; t <= TX_32X32; t++)
3281 full_to_model_counts(cpi->td.counts->coef[t],
3282 cpi->td.rd_counts.coef_counts[t]);
3284 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
3285 vp9_adapt_coef_probs(cm);
3287 if (!frame_is_intra_only(cm)) {
3288 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
3289 vp9_adapt_mode_probs(cm);
3290 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
3294 if (cpi->refresh_golden_frame == 1)
3295 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
3297 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
3299 if (cpi->refresh_alt_ref_frame == 1)
3300 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
3302 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
3304 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
3306 cm->last_frame_type = cm->frame_type;
3308 if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
3309 vp9_rc_postencode_update(cpi, *size);
3312 output_frame_level_debug_stats(cpi);
3315 if (cm->frame_type == KEY_FRAME) {
3316 // Tell the caller that the frame was coded as a key frame
3317 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
3319 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
3322 // Clear the one shot update flags for segmentation map and mode/ref loop
3324 cm->seg.update_map = 0;
3325 cm->seg.update_data = 0;
3326 cm->lf.mode_ref_delta_update = 0;
3328 // keep track of the last coded dimensions
3329 cm->last_width = cm->width;
3330 cm->last_height = cm->height;
3332 // reset to normal state now that we are done.
3333 if (!cm->show_existing_frame)
3334 cm->last_show_frame = cm->show_frame;
3336 if (cm->show_frame) {
3337 vp9_swap_mi_and_prev_mi(cm);
3338 // Don't increment frame counters if this was an altref buffer
3339 // update not a real frame
3340 ++cm->current_video_frame;
3342 vp9_inc_frame_in_layer(cpi);
3344 cm->prev_frame = cm->cur_frame;
3346 if (is_two_pass_svc(cpi))
3347 cpi->svc.layer_context[cpi->svc.spatial_layer_id].last_frame_type =
3351 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3352 unsigned int *frame_flags) {
3353 vp9_rc_get_svc_params(cpi);
3354 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3357 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3358 unsigned int *frame_flags) {
3359 if (cpi->oxcf.rc_mode == VPX_CBR) {
3360 vp9_rc_get_one_pass_cbr_params(cpi);
3362 vp9_rc_get_one_pass_vbr_params(cpi);
3364 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3367 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
3368 uint8_t *dest, unsigned int *frame_flags) {
3369 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
3370 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3372 if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
3373 vp9_twopass_postencode_update(cpi);
3376 static void check_initial_width(VP9_COMP *cpi,
3377 #if CONFIG_VP9_HIGHBITDEPTH
3378 int use_highbitdepth,
3380 int subsampling_x, int subsampling_y) {
3381 VP9_COMMON *const cm = &cpi->common;
3383 if (!cpi->initial_width) {
3384 cm->subsampling_x = subsampling_x;
3385 cm->subsampling_y = subsampling_y;
3386 #if CONFIG_VP9_HIGHBITDEPTH
3387 cm->use_highbitdepth = use_highbitdepth;
3390 alloc_raw_frame_buffers(cpi);
3391 alloc_ref_frame_buffers(cpi);
3392 alloc_util_frame_buffers(cpi);
3394 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3396 cpi->initial_width = cm->width;
3397 cpi->initial_height = cm->height;
3398 cpi->initial_mbs = cm->MBs;
3403 int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
3404 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3406 VP9_COMMON *cm = &cpi->common;
3407 struct vpx_usec_timer timer;
3409 const int subsampling_x = sd->subsampling_x;
3410 const int subsampling_y = sd->subsampling_y;
3411 #if CONFIG_VP9_HIGHBITDEPTH
3412 const int use_highbitdepth = sd->flags & YV12_FLAG_HIGHBITDEPTH;
3413 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
3415 check_initial_width(cpi, subsampling_x, subsampling_y);
3416 #endif // CONFIG_VP9_HIGHBITDEPTH
3418 vpx_usec_timer_start(&timer);
3420 if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags))
3422 vpx_usec_timer_mark(&timer);
3423 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
3425 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
3426 (subsampling_x != 1 || subsampling_y != 1)) {
3427 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
3428 "Non-4:2:0 color space requires profile 1 or 3");
3431 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
3432 (subsampling_x == 1 && subsampling_y == 1)) {
3433 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
3434 "4:2:0 color space requires profile 0 or 2");
3442 static int frame_is_reference(const VP9_COMP *cpi) {
3443 const VP9_COMMON *cm = &cpi->common;
3445 return cm->frame_type == KEY_FRAME ||
3446 cpi->refresh_last_frame ||
3447 cpi->refresh_golden_frame ||
3448 cpi->refresh_alt_ref_frame ||
3449 cm->refresh_frame_context ||
3450 cm->lf.mode_ref_delta_update ||
3451 cm->seg.update_map ||
3452 cm->seg.update_data;
3455 void adjust_frame_rate(VP9_COMP *cpi,
3456 const struct lookahead_entry *source) {
3457 int64_t this_duration;
3460 if (source->ts_start == cpi->first_time_stamp_ever) {
3461 this_duration = source->ts_end - source->ts_start;
3464 int64_t last_duration = cpi->last_end_time_stamp_seen
3465 - cpi->last_time_stamp_seen;
3467 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
3469 // do a step update if the duration changes by 10%
3471 step = (int)((this_duration - last_duration) * 10 / last_duration);
3474 if (this_duration) {
3476 vp9_new_framerate(cpi, 10000000.0 / this_duration);
3478 // Average this frame's rate into the last second's average
3479 // frame rate. If we haven't seen 1 second yet, then average
3480 // over the whole interval seen.
3481 const double interval = MIN((double)(source->ts_end
3482 - cpi->first_time_stamp_ever), 10000000.0);
3483 double avg_duration = 10000000.0 / cpi->framerate;
3484 avg_duration *= (interval - avg_duration + this_duration);
3485 avg_duration /= interval;
3487 vp9_new_framerate(cpi, 10000000.0 / avg_duration);
3490 cpi->last_time_stamp_seen = source->ts_start;
3491 cpi->last_end_time_stamp_seen = source->ts_end;
3494 // Returns 0 if this is not an alt ref else the offset of the source frame
3495 // used as the arf midpoint.
3496 static int get_arf_src_index(VP9_COMP *cpi) {
3497 RATE_CONTROL *const rc = &cpi->rc;
3498 int arf_src_index = 0;
3499 if (is_altref_enabled(cpi)) {
3500 if (cpi->oxcf.pass == 2) {
3501 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3502 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
3503 arf_src_index = gf_group->arf_src_offset[gf_group->index];
3505 } else if (rc->source_alt_ref_pending) {
3506 arf_src_index = rc->frames_till_gf_update_due;
3509 return arf_src_index;
3512 static void check_src_altref(VP9_COMP *cpi,
3513 const struct lookahead_entry *source) {
3514 RATE_CONTROL *const rc = &cpi->rc;
3516 if (cpi->oxcf.pass == 2) {
3517 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3518 rc->is_src_frame_alt_ref =
3519 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
3521 rc->is_src_frame_alt_ref = cpi->alt_ref_source &&
3522 (source == cpi->alt_ref_source);
3525 if (rc->is_src_frame_alt_ref) {
3526 // Current frame is an ARF overlay frame.
3527 cpi->alt_ref_source = NULL;
3529 // Don't refresh the last buffer for an ARF overlay frame. It will
3530 // become the GF so preserve last as an alternative prediction option.
3531 cpi->refresh_last_frame = 0;
3535 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
3536 size_t *size, uint8_t *dest,
3537 int64_t *time_stamp, int64_t *time_end, int flush) {
3538 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3539 VP9_COMMON *const cm = &cpi->common;
3540 RATE_CONTROL *const rc = &cpi->rc;
3541 struct vpx_usec_timer cmptimer;
3542 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
3543 struct lookahead_entry *last_source = NULL;
3544 struct lookahead_entry *source = NULL;
3548 if (is_two_pass_svc(cpi)) {
3549 #if CONFIG_SPATIAL_SVC
3550 vp9_svc_start_frame(cpi);
3551 // Use a small empty frame instead of a real frame
3552 if (cpi->svc.encode_empty_frame_state == ENCODING)
3553 source = &cpi->svc.empty_frame;
3555 if (oxcf->pass == 2)
3556 vp9_restore_layer_context(cpi);
3559 vpx_usec_timer_start(&cmptimer);
3561 vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
3563 // Is multi-arf enabled.
3564 // Note that at the moment multi_arf is only configured for 2 pass VBR and
3565 // will not work properly with svc.
3566 if ((oxcf->pass == 2) && !cpi->use_svc &&
3567 (cpi->oxcf.enable_auto_arf > 1))
3568 cpi->multi_arf_allowed = 1;
3570 cpi->multi_arf_allowed = 0;
3573 cm->reset_frame_context = 0;
3574 cm->refresh_frame_context = 1;
3575 cpi->refresh_last_frame = 1;
3576 cpi->refresh_golden_frame = 0;
3577 cpi->refresh_alt_ref_frame = 0;
3579 // Should we encode an arf frame.
3580 arf_src_index = get_arf_src_index(cpi);
3582 // Skip alt frame if we encode the empty frame
3583 if (is_two_pass_svc(cpi) && source != NULL)
3586 if (arf_src_index) {
3587 assert(arf_src_index <= rc->frames_to_key);
3589 if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
3590 cpi->alt_ref_source = source;
3592 #if CONFIG_SPATIAL_SVC
3593 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
3595 // Reference a hidden frame from a lower layer
3596 for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
3597 if (oxcf->ss_enable_auto_arf[i]) {
3598 cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
3603 cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
3606 if (oxcf->arnr_max_frames > 0) {
3607 // Produce the filtered ARF frame.
3608 vp9_temporal_filter(cpi, arf_src_index);
3609 vp9_extend_frame_borders(&cpi->alt_ref_buffer);
3610 force_src_buffer = &cpi->alt_ref_buffer;
3614 cpi->refresh_alt_ref_frame = 1;
3615 cpi->refresh_golden_frame = 0;
3616 cpi->refresh_last_frame = 0;
3617 rc->is_src_frame_alt_ref = 0;
3618 rc->source_alt_ref_pending = 0;
3620 rc->source_alt_ref_pending = 0;
3625 // Get last frame source.
3626 if (cm->current_video_frame > 0) {
3627 if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
3631 // Read in the source frame.
3632 #if CONFIG_SPATIAL_SVC
3633 if (is_two_pass_svc(cpi))
3634 source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
3637 source = vp9_lookahead_pop(cpi->lookahead, flush);
3638 if (source != NULL) {
3641 // if the flags indicate intra frame, but if the current picture is for
3642 // non-zero spatial layer, it should not be an intra picture.
3643 // TODO(Won Kap): this needs to change if per-layer intra frame is
3645 if ((source->flags | VPX_EFLAG_FORCE_KF) && cpi->svc.spatial_layer_id) {
3646 source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
3649 // Check to see if the frame should be encoded as an arf overlay.
3650 check_src_altref(cpi, source);
3655 cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
3658 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
3660 *time_stamp = source->ts_start;
3661 *time_end = source->ts_end;
3662 *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
3666 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
3667 vp9_end_first_pass(cpi); /* get last stats packet */
3668 cpi->twopass.first_pass_done = 1;
3673 if (source->ts_start < cpi->first_time_stamp_ever) {
3674 cpi->first_time_stamp_ever = source->ts_start;
3675 cpi->last_end_time_stamp_seen = source->ts_start;
3678 // Clear down mmx registers
3679 vp9_clear_system_state();
3681 // adjust frame rates based on timestamps given
3682 if (cm->show_frame) {
3683 adjust_frame_rate(cpi, source);
3686 if (cpi->svc.number_temporal_layers > 1 &&
3687 oxcf->rc_mode == VPX_CBR) {
3688 vp9_update_temporal_layer_framerate(cpi);
3689 vp9_restore_layer_context(cpi);
3692 // Find a free buffer for the new frame, releasing the reference previously
3694 cm->frame_bufs[cm->new_fb_idx].ref_count--;
3695 cm->new_fb_idx = get_free_fb(cm);
3696 cm->cur_frame = &cm->frame_bufs[cm->new_fb_idx];
3698 if (!cpi->use_svc && cpi->multi_arf_allowed) {
3699 if (cm->frame_type == KEY_FRAME) {
3700 init_buffer_indices(cpi);
3701 } else if (oxcf->pass == 2) {
3702 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3703 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
3707 // Start with a 0 size frame.
3710 cpi->frame_flags = *frame_flags;
3712 if ((oxcf->pass == 2) &&
3714 (is_two_pass_svc(cpi) &&
3715 cpi->svc.encode_empty_frame_state != ENCODING))) {
3716 vp9_rc_get_second_pass_params(cpi);
3718 set_frame_size(cpi);
3721 for (i = 0; i < MAX_REF_FRAMES; ++i)
3722 cpi->scaled_ref_idx[i] = INVALID_REF_BUFFER_IDX;
3724 if (oxcf->pass == 1 &&
3725 (!cpi->use_svc || is_two_pass_svc(cpi))) {
3726 const int lossless = is_lossless_requested(oxcf);
3727 #if CONFIG_VP9_HIGHBITDEPTH
3728 if (cpi->oxcf.use_highbitdepth)
3729 cpi->td.mb.fwd_txm4x4 = lossless ?
3730 vp9_highbd_fwht4x4 : vp9_highbd_fdct4x4;
3732 cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3733 cpi->td.mb.highbd_itxm_add = lossless ? vp9_highbd_iwht4x4_add :
3734 vp9_highbd_idct4x4_add;
3736 cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3737 #endif // CONFIG_VP9_HIGHBITDEPTH
3738 cpi->td.mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
3739 vp9_first_pass(cpi, source);
3740 } else if (oxcf->pass == 2 &&
3741 (!cpi->use_svc || is_two_pass_svc(cpi))) {
3742 Pass2Encode(cpi, size, dest, frame_flags);
3743 } else if (cpi->use_svc) {
3744 SvcEncode(cpi, size, dest, frame_flags);
3747 Pass0Encode(cpi, size, dest, frame_flags);
3750 if (cm->refresh_frame_context)
3751 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
3753 // No frame encoded, or frame was dropped, release scaled references.
3754 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
3755 release_scaled_references(cpi);
3759 cpi->droppable = !frame_is_reference(cpi);
3762 // Save layer specific state.
3763 if ((cpi->svc.number_temporal_layers > 1 &&
3764 oxcf->rc_mode == VPX_CBR) ||
3765 ((cpi->svc.number_temporal_layers > 1 ||
3766 cpi->svc.number_spatial_layers > 1) &&
3768 vp9_save_layer_context(cpi);
3771 vpx_usec_timer_mark(&cmptimer);
3772 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
3774 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
3775 generate_psnr_packet(cpi);
3777 #if CONFIG_INTERNAL_STATS
3779 if (oxcf->pass != 1) {
3780 cpi->bytes += (int)(*size);
3782 if (cm->show_frame) {
3785 if (cpi->b_calculate_psnr) {
3786 YV12_BUFFER_CONFIG *orig = cpi->Source;
3787 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
3788 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
3790 #if CONFIG_VP9_HIGHBITDEPTH
3791 calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
3792 cpi->oxcf.input_bit_depth);
3794 calc_psnr(orig, recon, &psnr);
3795 #endif // CONFIG_VP9_HIGHBITDEPTH
3797 cpi->total += psnr.psnr[0];
3798 cpi->total_y += psnr.psnr[1];
3799 cpi->total_u += psnr.psnr[2];
3800 cpi->total_v += psnr.psnr[3];
3801 cpi->total_sq_error += psnr.sse[0];
3802 cpi->total_samples += psnr.samples[0];
3806 double frame_ssim2 = 0, weight = 0;
3807 #if CONFIG_VP9_POSTPROC
3808 // TODO(agrange) Add resizing of post-proc buffer in here when the
3809 // encoder is changed to use on-demand buffer allocation.
3810 vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
3811 cm->lf.filter_level * 10 / 6);
3813 vp9_clear_system_state();
3815 #if CONFIG_VP9_HIGHBITDEPTH
3816 calc_highbd_psnr(orig, pp, &psnr, cpi->td.mb.e_mbd.bd,
3817 cpi->oxcf.input_bit_depth);
3819 calc_psnr(orig, pp, &psnr2);
3820 #endif // CONFIG_VP9_HIGHBITDEPTH
3822 cpi->totalp += psnr2.psnr[0];
3823 cpi->totalp_y += psnr2.psnr[1];
3824 cpi->totalp_u += psnr2.psnr[2];
3825 cpi->totalp_v += psnr2.psnr[3];
3826 cpi->totalp_sq_error += psnr2.sse[0];
3827 cpi->totalp_samples += psnr2.samples[0];
3829 #if CONFIG_VP9_HIGHBITDEPTH
3830 if (cm->use_highbitdepth) {
3831 frame_ssim2 = vp9_highbd_calc_ssim(orig, recon, &weight, xd->bd);
3833 frame_ssim2 = vp9_calc_ssim(orig, recon, &weight);
3836 frame_ssim2 = vp9_calc_ssim(orig, recon, &weight);
3837 #endif // CONFIG_VP9_HIGHBITDEPTH
3839 cpi->summed_quality += frame_ssim2 * weight;
3840 cpi->summed_weights += weight;
3842 #if CONFIG_VP9_HIGHBITDEPTH
3843 if (cm->use_highbitdepth) {
3844 frame_ssim2 = vp9_highbd_calc_ssim(
3845 orig, &cm->post_proc_buffer, &weight, xd->bd);
3847 frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, &weight);
3850 frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, &weight);
3851 #endif // CONFIG_VP9_HIGHBITDEPTH
3853 cpi->summedp_quality += frame_ssim2 * weight;
3854 cpi->summedp_weights += weight;
3857 FILE *f = fopen("q_used.stt", "a");
3858 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3859 cpi->common.current_video_frame, y2, u2, v2,
3860 frame_psnr2, frame_ssim2);
3868 if (cpi->b_calculate_ssimg) {
3869 double y, u, v, frame_all;
3870 #if CONFIG_VP9_HIGHBITDEPTH
3871 if (cm->use_highbitdepth) {
3872 frame_all = vp9_highbd_calc_ssimg(cpi->Source, cm->frame_to_show, &y,
3875 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u,
3879 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
3880 #endif // CONFIG_VP9_HIGHBITDEPTH
3881 cpi->total_ssimg_y += y;
3882 cpi->total_ssimg_u += u;
3883 cpi->total_ssimg_v += v;
3884 cpi->total_ssimg_all += frame_all;
3891 if (is_two_pass_svc(cpi)) {
3892 if (cpi->svc.encode_empty_frame_state == ENCODING)
3893 cpi->svc.encode_empty_frame_state = ENCODED;
3895 if (cm->show_frame) {
3896 ++cpi->svc.spatial_layer_to_encode;
3897 if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
3898 cpi->svc.spatial_layer_to_encode = 0;
3900 // May need the empty frame after an visible frame.
3901 cpi->svc.encode_empty_frame_state = NEED_TO_ENCODE;
3907 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
3908 vp9_ppflags_t *flags) {
3909 VP9_COMMON *cm = &cpi->common;
3910 #if !CONFIG_VP9_POSTPROC
3914 if (!cm->show_frame) {
3918 #if CONFIG_VP9_POSTPROC
3919 ret = vp9_post_proc_frame(cm, dest, flags);
3921 if (cm->frame_to_show) {
3922 *dest = *cm->frame_to_show;
3923 dest->y_width = cm->width;
3924 dest->y_height = cm->height;
3925 dest->uv_width = cm->width >> cm->subsampling_x;
3926 dest->uv_height = cm->height >> cm->subsampling_y;
3931 #endif // !CONFIG_VP9_POSTPROC
3932 vp9_clear_system_state();
3937 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols) {
3938 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
3939 const int mi_rows = cpi->common.mi_rows;
3940 const int mi_cols = cpi->common.mi_cols;
3943 for (r = 0; r < mi_rows; r++) {
3944 for (c = 0; c < mi_cols; c++) {
3945 cpi->segmentation_map[r * mi_cols + c] =
3946 !map[(r >> 1) * cols + (c >> 1)];
3949 vp9_enable_segfeature(&cpi->common.seg, 1, SEG_LVL_SKIP);
3950 vp9_enable_segmentation(&cpi->common.seg);
3952 vp9_disable_segmentation(&cpi->common.seg);
3960 int vp9_set_internal_size(VP9_COMP *cpi,
3961 VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
3962 VP9_COMMON *cm = &cpi->common;
3963 int hr = 0, hs = 0, vr = 0, vs = 0;
3965 if (horiz_mode > ONETWO || vert_mode > ONETWO)
3968 Scale2Ratio(horiz_mode, &hr, &hs);
3969 Scale2Ratio(vert_mode, &vr, &vs);
3971 // always go to the next whole number
3972 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
3973 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
3974 assert(cm->width <= cpi->initial_width);
3975 assert(cm->height <= cpi->initial_height);
3977 update_frame_size(cpi);
3982 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
3983 unsigned int height) {
3984 VP9_COMMON *cm = &cpi->common;
3985 #if CONFIG_VP9_HIGHBITDEPTH
3986 check_initial_width(cpi, 1, 1, cm->use_highbitdepth);
3988 check_initial_width(cpi, 1, 1);
3989 #endif // CONFIG_VP9_HIGHBITDEPTH
3993 if (cm->width > cpi->initial_width) {
3994 cm->width = cpi->initial_width;
3995 printf("Warning: Desired width too large, changed to %d\n", cm->width);
4000 cm->height = height;
4001 if (cm->height > cpi->initial_height) {
4002 cm->height = cpi->initial_height;
4003 printf("Warning: Desired height too large, changed to %d\n", cm->height);
4006 assert(cm->width <= cpi->initial_width);
4007 assert(cm->height <= cpi->initial_height);
4009 update_frame_size(cpi);
4014 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
4015 cpi->use_svc = use_svc;
4019 int vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b) {
4020 assert(a->y_crop_width == b->y_crop_width);
4021 assert(a->y_crop_height == b->y_crop_height);
4023 return (int)get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
4024 a->y_crop_width, a->y_crop_height);
4027 #if CONFIG_VP9_HIGHBITDEPTH
4028 int vp9_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
4029 const YV12_BUFFER_CONFIG *b,
4030 vpx_bit_depth_t bit_depth) {
4033 assert(a->y_crop_width == b->y_crop_width);
4034 assert(a->y_crop_height == b->y_crop_height);
4035 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
4036 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
4037 switch (bit_depth) {
4039 highbd_variance(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
4040 a->y_crop_width, a->y_crop_height, &sse, &sum);
4043 highbd_10_variance(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
4044 a->y_crop_width, a->y_crop_height, &sse, &sum);
4047 highbd_12_variance(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
4048 a->y_crop_width, a->y_crop_height, &sse, &sum);
4051 assert(0 && "bit_depth should be VPX_BITS_8, VPX_BITS_10 or VPX_BITS_12");
4055 #endif // CONFIG_VP9_HIGHBITDEPTH
4057 int vp9_get_quantizer(VP9_COMP *cpi) {
4058 return cpi->common.base_qindex;
4061 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
4062 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF |
4063 VP8_EFLAG_NO_REF_ARF)) {
4066 if (flags & VP8_EFLAG_NO_REF_LAST)
4067 ref ^= VP9_LAST_FLAG;
4069 if (flags & VP8_EFLAG_NO_REF_GF)
4070 ref ^= VP9_GOLD_FLAG;
4072 if (flags & VP8_EFLAG_NO_REF_ARF)
4073 ref ^= VP9_ALT_FLAG;
4075 vp9_use_as_reference(cpi, ref);
4078 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
4079 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF |
4080 VP8_EFLAG_FORCE_ARF)) {
4083 if (flags & VP8_EFLAG_NO_UPD_LAST)
4084 upd ^= VP9_LAST_FLAG;
4086 if (flags & VP8_EFLAG_NO_UPD_GF)
4087 upd ^= VP9_GOLD_FLAG;
4089 if (flags & VP8_EFLAG_NO_UPD_ARF)
4090 upd ^= VP9_ALT_FLAG;
4092 vp9_update_reference(cpi, upd);
4095 if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
4096 vp9_update_entropy(cpi, 0);