2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
15 #include "./vpx_config.h"
16 #include "./vpx_scale_rtcd.h"
17 #include "vpx/internal/vpx_psnr.h"
18 #include "vpx_ports/vpx_timer.h"
20 #include "vp9/common/vp9_alloccommon.h"
21 #include "vp9/common/vp9_filter.h"
22 #include "vp9/common/vp9_idct.h"
23 #if CONFIG_VP9_POSTPROC
24 #include "vp9/common/vp9_postproc.h"
26 #include "vp9/common/vp9_reconinter.h"
27 #include "vp9/common/vp9_reconintra.h"
28 #include "vp9/common/vp9_systemdependent.h"
29 #include "vp9/common/vp9_tile_common.h"
31 #include "vp9/encoder/vp9_aq_complexity.h"
32 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
33 #include "vp9/encoder/vp9_aq_variance.h"
34 #include "vp9/encoder/vp9_bitstream.h"
35 #include "vp9/encoder/vp9_context_tree.h"
36 #include "vp9/encoder/vp9_encodeframe.h"
37 #include "vp9/encoder/vp9_encodemv.h"
38 #include "vp9/encoder/vp9_firstpass.h"
39 #include "vp9/encoder/vp9_mbgraph.h"
40 #include "vp9/encoder/vp9_encoder.h"
41 #include "vp9/encoder/vp9_picklpf.h"
42 #include "vp9/encoder/vp9_ratectrl.h"
43 #include "vp9/encoder/vp9_rd.h"
44 #include "vp9/encoder/vp9_segmentation.h"
45 #include "vp9/encoder/vp9_speed_features.h"
46 #if CONFIG_INTERNAL_STATS
47 #include "vp9/encoder/vp9_ssim.h"
49 #include "vp9/encoder/vp9_temporal_filter.h"
50 #include "vp9/encoder/vp9_resize.h"
51 #include "vp9/encoder/vp9_svc_layercontext.h"
53 void vp9_coef_tree_initialize();
55 #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
57 #define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
58 // for altref computation.
59 #define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
60 // mv. Choose a very high value for
61 // now so that HIGH_PRECISION is always
64 // #define OUTPUT_YUV_REC
66 #ifdef OUTPUT_YUV_DENOISED
67 FILE *yuv_denoised_file = NULL;
79 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
105 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
106 MACROBLOCK *const mb = &cpi->mb;
107 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
108 if (cpi->common.allow_high_precision_mv) {
109 mb->mvcost = mb->nmvcost_hp;
110 mb->mvsadcost = mb->nmvsadcost_hp;
112 mb->mvcost = mb->nmvcost;
113 mb->mvsadcost = mb->nmvsadcost;
117 static void setup_frame(VP9_COMP *cpi) {
118 VP9_COMMON *const cm = &cpi->common;
119 // Set up entropy context depending on frame type. The decoder mandates
120 // the use of the default context, index 0, for keyframes and inter
121 // frames where the error_resilient_mode or intra_only flag is set. For
122 // other inter-frames the encoder currently uses only two contexts;
123 // context 1 for ALTREF frames and context 0 for the others.
124 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
125 vp9_setup_past_independence(cm);
128 cm->frame_context_idx = cpi->refresh_alt_ref_frame;
131 if (cm->frame_type == KEY_FRAME) {
132 if (!is_two_pass_svc(cpi))
133 cpi->refresh_golden_frame = 1;
134 cpi->refresh_alt_ref_frame = 1;
135 vp9_zero(cpi->interp_filter_selected);
137 cm->fc = cm->frame_contexts[cm->frame_context_idx];
138 vp9_zero(cpi->interp_filter_selected[0]);
142 void vp9_initialize_enc() {
143 static int init_done = 0;
147 vp9_init_neighbors();
148 vp9_init_intra_predictors();
149 vp9_coef_tree_initialize();
150 vp9_tokenize_initialize();
152 vp9_rc_init_minq_luts();
153 vp9_entropy_mv_init();
154 vp9_entropy_mode_init();
155 vp9_temporal_filter_init();
160 static void dealloc_compressor_data(VP9_COMP *cpi) {
161 VP9_COMMON *const cm = &cpi->common;
164 // Delete sementation map
165 vpx_free(cpi->segmentation_map);
166 cpi->segmentation_map = NULL;
167 vpx_free(cm->last_frame_seg_map);
168 cm->last_frame_seg_map = NULL;
169 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
170 cpi->coding_context.last_frame_seg_map_copy = NULL;
172 vpx_free(cpi->complexity_map);
173 cpi->complexity_map = NULL;
175 vpx_free(cpi->nmvcosts[0]);
176 vpx_free(cpi->nmvcosts[1]);
177 cpi->nmvcosts[0] = NULL;
178 cpi->nmvcosts[1] = NULL;
180 vpx_free(cpi->nmvcosts_hp[0]);
181 vpx_free(cpi->nmvcosts_hp[1]);
182 cpi->nmvcosts_hp[0] = NULL;
183 cpi->nmvcosts_hp[1] = NULL;
185 vpx_free(cpi->nmvsadcosts[0]);
186 vpx_free(cpi->nmvsadcosts[1]);
187 cpi->nmvsadcosts[0] = NULL;
188 cpi->nmvsadcosts[1] = NULL;
190 vpx_free(cpi->nmvsadcosts_hp[0]);
191 vpx_free(cpi->nmvsadcosts_hp[1]);
192 cpi->nmvsadcosts_hp[0] = NULL;
193 cpi->nmvsadcosts_hp[1] = NULL;
195 vp9_cyclic_refresh_free(cpi->cyclic_refresh);
196 cpi->cyclic_refresh = NULL;
198 vp9_free_ref_frame_buffers(cm);
199 vp9_free_context_buffers(cm);
201 vp9_free_frame_buffer(&cpi->last_frame_uf);
202 vp9_free_frame_buffer(&cpi->scaled_source);
203 vp9_free_frame_buffer(&cpi->scaled_last_source);
204 vp9_free_frame_buffer(&cpi->alt_ref_buffer);
205 vp9_lookahead_destroy(cpi->lookahead);
210 vp9_free_pc_tree(cpi);
212 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
213 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
214 vpx_free(lc->rc_twopass_stats_in.buf);
215 lc->rc_twopass_stats_in.buf = NULL;
216 lc->rc_twopass_stats_in.sz = 0;
219 if (cpi->source_diff_var != NULL) {
220 vpx_free(cpi->source_diff_var);
221 cpi->source_diff_var = NULL;
224 for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
225 vp9_free_frame_buffer(&cpi->svc.scaled_frames[i]);
227 vpx_memset(&cpi->svc.scaled_frames[0], 0,
228 MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
231 static void save_coding_context(VP9_COMP *cpi) {
232 CODING_CONTEXT *const cc = &cpi->coding_context;
233 VP9_COMMON *cm = &cpi->common;
235 // Stores a snapshot of key state variables which can subsequently be
236 // restored with a call to vp9_restore_coding_context. These functions are
237 // intended for use in a re-code loop in vp9_compress_frame where the
238 // quantizer value is adjusted between loop iterations.
239 vp9_copy(cc->nmvjointcost, cpi->mb.nmvjointcost);
241 vpx_memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
242 MV_VALS * sizeof(*cpi->nmvcosts[0]));
243 vpx_memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
244 MV_VALS * sizeof(*cpi->nmvcosts[1]));
245 vpx_memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
246 MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
247 vpx_memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
248 MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
250 vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
252 vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
253 cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols));
255 vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
256 vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
261 static void restore_coding_context(VP9_COMP *cpi) {
262 CODING_CONTEXT *const cc = &cpi->coding_context;
263 VP9_COMMON *cm = &cpi->common;
265 // Restore key state variables to the snapshot state stored in the
266 // previous call to vp9_save_coding_context.
267 vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost);
269 vpx_memcpy(cpi->nmvcosts[0], cc->nmvcosts[0],
270 MV_VALS * sizeof(*cc->nmvcosts[0]));
271 vpx_memcpy(cpi->nmvcosts[1], cc->nmvcosts[1],
272 MV_VALS * sizeof(*cc->nmvcosts[1]));
273 vpx_memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
274 MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
275 vpx_memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
276 MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
278 vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
280 vpx_memcpy(cm->last_frame_seg_map,
281 cpi->coding_context.last_frame_seg_map_copy,
282 (cm->mi_rows * cm->mi_cols));
284 vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
285 vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
290 static void configure_static_seg_features(VP9_COMP *cpi) {
291 VP9_COMMON *const cm = &cpi->common;
292 const RATE_CONTROL *const rc = &cpi->rc;
293 struct segmentation *const seg = &cm->seg;
295 int high_q = (int)(rc->avg_q > 48.0);
298 // Disable and clear down for KF
299 if (cm->frame_type == KEY_FRAME) {
300 // Clear down the global segmentation map
301 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
303 seg->update_data = 0;
304 cpi->static_mb_pct = 0;
306 // Disable segmentation
307 vp9_disable_segmentation(seg);
309 // Clear down the segment features.
310 vp9_clearall_segfeatures(seg);
311 } else if (cpi->refresh_alt_ref_frame) {
312 // If this is an alt ref frame
313 // Clear down the global segmentation map
314 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
316 seg->update_data = 0;
317 cpi->static_mb_pct = 0;
319 // Disable segmentation and individual segment features by default
320 vp9_disable_segmentation(seg);
321 vp9_clearall_segfeatures(seg);
323 // Scan frames from current to arf frame.
324 // This function re-enables segmentation if appropriate.
325 vp9_update_mbgraph_stats(cpi);
327 // If segmentation was enabled set those features needed for the
331 seg->update_data = 1;
333 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875);
334 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
335 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
337 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
338 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
340 // Where relevant assume segment data is delta data
341 seg->abs_delta = SEGMENT_DELTADATA;
343 } else if (seg->enabled) {
344 // All other frames if segmentation has been enabled
346 // First normal frame in a valid gf or alt ref group
347 if (rc->frames_since_golden == 0) {
348 // Set up segment features for normal frames in an arf group
349 if (rc->source_alt_ref_active) {
351 seg->update_data = 1;
352 seg->abs_delta = SEGMENT_DELTADATA;
354 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125);
355 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
356 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
358 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
359 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
361 // Segment coding disabled for compred testing
362 if (high_q || (cpi->static_mb_pct == 100)) {
363 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
364 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
365 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
368 // Disable segmentation and clear down features if alt ref
369 // is not active for this group
371 vp9_disable_segmentation(seg);
373 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
376 seg->update_data = 0;
378 vp9_clearall_segfeatures(seg);
380 } else if (rc->is_src_frame_alt_ref) {
381 // Special case where we are coding over the top of a previous
383 // Segment coding disabled for compred testing
385 // Enable ref frame features for segment 0 as well
386 vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
387 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
389 // All mbs should use ALTREF_FRAME
390 vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
391 vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
392 vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
393 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
395 // Skip all MBs if high Q (0,0 mv and skip coeffs)
397 vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
398 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
400 // Enable data update
401 seg->update_data = 1;
405 // No updates.. leave things as they are.
407 seg->update_data = 0;
412 static void update_reference_segmentation_map(VP9_COMP *cpi) {
413 VP9_COMMON *const cm = &cpi->common;
414 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
415 uint8_t *cache_ptr = cm->last_frame_seg_map;
418 for (row = 0; row < cm->mi_rows; row++) {
419 MODE_INFO **mi_8x8 = mi_8x8_ptr;
420 uint8_t *cache = cache_ptr;
421 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
422 cache[0] = mi_8x8[0]->mbmi.segment_id;
423 mi_8x8_ptr += cm->mi_stride;
424 cache_ptr += cm->mi_cols;
428 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
429 VP9_COMMON *cm = &cpi->common;
430 const VP9EncoderConfig *oxcf = &cpi->oxcf;
432 cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
433 cm->subsampling_x, cm->subsampling_y,
434 #if CONFIG_VP9_HIGHBITDEPTH
435 cm->use_highbitdepth,
437 oxcf->lag_in_frames);
439 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
440 "Failed to allocate lag buffers");
442 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
443 oxcf->width, oxcf->height,
444 cm->subsampling_x, cm->subsampling_y,
445 #if CONFIG_VP9_HIGHBITDEPTH
446 cm->use_highbitdepth,
448 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
449 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
450 "Failed to allocate altref buffer");
453 static void alloc_ref_frame_buffers(VP9_COMP *cpi) {
454 VP9_COMMON *const cm = &cpi->common;
455 if (vp9_alloc_ref_frame_buffers(cm, cm->width, cm->height))
456 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
457 "Failed to allocate frame buffers");
460 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
461 VP9_COMMON *const cm = &cpi->common;
462 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
463 cm->width, cm->height,
464 cm->subsampling_x, cm->subsampling_y,
465 #if CONFIG_VP9_HIGHBITDEPTH
466 cm->use_highbitdepth,
468 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
469 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
470 "Failed to allocate last frame buffer");
472 if (vp9_realloc_frame_buffer(&cpi->scaled_source,
473 cm->width, cm->height,
474 cm->subsampling_x, cm->subsampling_y,
475 #if CONFIG_VP9_HIGHBITDEPTH
476 cm->use_highbitdepth,
478 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
479 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
480 "Failed to allocate scaled source buffer");
482 if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
483 cm->width, cm->height,
484 cm->subsampling_x, cm->subsampling_y,
485 #if CONFIG_VP9_HIGHBITDEPTH
486 cm->use_highbitdepth,
488 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
489 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
490 "Failed to allocate scaled last source buffer");
493 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
494 VP9_COMMON *cm = &cpi->common;
496 vp9_alloc_context_buffers(cm, cm->width, cm->height);
501 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
502 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
505 vp9_setup_pc_tree(&cpi->common, cpi);
508 static void update_frame_size(VP9_COMP *cpi) {
509 VP9_COMMON *const cm = &cpi->common;
510 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
512 vp9_set_mb_mi(cm, cm->width, cm->height);
513 vp9_init_context_buffers(cm);
514 init_macroblockd(cm, xd);
516 if (is_two_pass_svc(cpi)) {
517 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
518 cm->width, cm->height,
519 cm->subsampling_x, cm->subsampling_y,
520 #if CONFIG_VP9_HIGHBITDEPTH
521 cm->use_highbitdepth,
523 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
524 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
525 "Failed to reallocate alt_ref_buffer");
529 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
530 cpi->framerate = framerate < 0.1 ? 30 : framerate;
531 vp9_rc_update_framerate(cpi);
534 static void set_tile_limits(VP9_COMP *cpi) {
535 VP9_COMMON *const cm = &cpi->common;
537 int min_log2_tile_cols, max_log2_tile_cols;
538 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
540 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
541 min_log2_tile_cols, max_log2_tile_cols);
542 cm->log2_tile_rows = cpi->oxcf.tile_rows;
545 static void init_buffer_indices(VP9_COMP *cpi) {
551 static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
552 VP9_COMMON *const cm = &cpi->common;
555 cpi->framerate = oxcf->init_framerate;
557 cm->profile = oxcf->profile;
558 cm->bit_depth = oxcf->bit_depth;
559 cm->color_space = UNKNOWN;
561 cm->width = oxcf->width;
562 cm->height = oxcf->height;
563 vp9_alloc_compressor_data(cpi);
565 // Spatial scalability.
566 cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
567 // Temporal scalability.
568 cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
570 if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
571 ((cpi->svc.number_temporal_layers > 1 ||
572 cpi->svc.number_spatial_layers > 1) &&
573 cpi->oxcf.pass == 2)) {
574 vp9_init_layer_context(cpi);
577 // change includes all joint functionality
578 vp9_change_config(cpi, oxcf);
580 cpi->static_mb_pct = 0;
581 cpi->ref_frame_flags = 0;
583 init_buffer_indices(cpi);
585 set_tile_limits(cpi);
588 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
589 const VP9EncoderConfig *oxcf) {
590 const int64_t bandwidth = oxcf->target_bandwidth;
591 const int64_t starting = oxcf->starting_buffer_level_ms;
592 const int64_t optimal = oxcf->optimal_buffer_level_ms;
593 const int64_t maximum = oxcf->maximum_buffer_size_ms;
595 rc->starting_buffer_level = starting * bandwidth / 1000;
596 rc->optimal_buffer_level = (optimal == 0) ? bandwidth / 8
597 : optimal * bandwidth / 1000;
598 rc->maximum_buffer_size = (maximum == 0) ? bandwidth / 8
599 : maximum * bandwidth / 1000;
602 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
603 VP9_COMMON *const cm = &cpi->common;
604 RATE_CONTROL *const rc = &cpi->rc;
606 if (cm->profile != oxcf->profile)
607 cm->profile = oxcf->profile;
608 cm->bit_depth = oxcf->bit_depth;
610 if (cm->profile <= PROFILE_1)
611 assert(cm->bit_depth == VPX_BITS_8);
613 assert(cm->bit_depth > VPX_BITS_8);
617 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
619 cpi->refresh_golden_frame = 0;
620 cpi->refresh_last_frame = 1;
621 cm->refresh_frame_context = 1;
622 cm->reset_frame_context = 0;
624 vp9_reset_segment_features(&cm->seg);
625 vp9_set_high_precision_mv(cpi, 0);
630 for (i = 0; i < MAX_SEGMENTS; i++)
631 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
633 cpi->encode_breakout = cpi->oxcf.encode_breakout;
635 set_rc_buffer_sizes(rc, &cpi->oxcf);
637 // Under a configuration change, where maximum_buffer_size may change,
638 // keep buffer level clipped to the maximum allowed buffer size.
639 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
640 rc->buffer_level = MIN(rc->buffer_level, rc->maximum_buffer_size);
642 // Set up frame rate and related parameters rate control values.
643 vp9_new_framerate(cpi, cpi->framerate);
645 // Set absolute upper and lower quality limits
646 rc->worst_quality = cpi->oxcf.worst_allowed_q;
647 rc->best_quality = cpi->oxcf.best_allowed_q;
649 cm->interp_filter = cpi->sf.default_interp_filter;
651 cm->display_width = cpi->oxcf.width;
652 cm->display_height = cpi->oxcf.height;
654 if (cpi->initial_width) {
655 // Increasing the size of the frame beyond the first seen frame, or some
656 // otherwise signaled maximum size, is not supported.
657 // TODO(jkoleszar): exit gracefully.
658 assert(cm->width <= cpi->initial_width);
659 assert(cm->height <= cpi->initial_height);
661 update_frame_size(cpi);
663 if ((cpi->svc.number_temporal_layers > 1 &&
664 cpi->oxcf.rc_mode == VPX_CBR) ||
665 ((cpi->svc.number_temporal_layers > 1 ||
666 cpi->svc.number_spatial_layers > 1) &&
667 cpi->oxcf.pass == 2)) {
668 vp9_update_layer_context_change_config(cpi,
669 (int)cpi->oxcf.target_bandwidth);
672 cpi->alt_ref_source = NULL;
673 rc->is_src_frame_alt_ref = 0;
676 // Experimental RD Code
677 cpi->frame_distortion = 0;
678 cpi->last_frame_distortion = 0;
681 set_tile_limits(cpi);
683 cpi->ext_refresh_frame_flags_pending = 0;
684 cpi->ext_refresh_frame_context_pending = 0;
686 #if CONFIG_VP9_TEMPORAL_DENOISING
687 if (cpi->oxcf.noise_sensitivity > 0) {
688 vp9_denoiser_alloc(&(cpi->denoiser), cm->width, cm->height,
689 cm->subsampling_x, cm->subsampling_y,
690 #if CONFIG_VP9_HIGHBITDEPTH
691 cm->use_highbitdepth,
693 VP9_ENC_BORDER_IN_PIXELS);
699 #define M_LOG2_E 0.693147180559945309417
701 #define log2f(x) (log (x) / (float) M_LOG2_E)
703 static void cal_nmvjointsadcost(int *mvjointsadcost) {
704 mvjointsadcost[0] = 600;
705 mvjointsadcost[1] = 300;
706 mvjointsadcost[2] = 300;
707 mvjointsadcost[3] = 300;
710 static void cal_nmvsadcosts(int *mvsadcost[2]) {
717 double z = 256 * (2 * (log2f(8 * i) + .6));
718 mvsadcost[0][i] = (int)z;
719 mvsadcost[1][i] = (int)z;
720 mvsadcost[0][-i] = (int)z;
721 mvsadcost[1][-i] = (int)z;
722 } while (++i <= MV_MAX);
725 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
732 double z = 256 * (2 * (log2f(8 * i) + .6));
733 mvsadcost[0][i] = (int)z;
734 mvsadcost[1][i] = (int)z;
735 mvsadcost[0][-i] = (int)z;
736 mvsadcost[1][-i] = (int)z;
737 } while (++i <= MV_MAX);
741 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf) {
743 VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP));
744 VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL;
751 if (setjmp(cm->error.jmp)) {
752 cm->error.setjmp = 0;
753 vp9_remove_compressor(cpi);
757 cm->error.setjmp = 1;
761 init_config(cpi, oxcf);
762 vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
764 cm->current_video_frame = 0;
765 cpi->skippable_frame = 0;
767 // Create the encoder segmentation map and set all entries to 0
768 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
769 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
771 // Create a complexity map used for rd adjustment
772 CHECK_MEM_ERROR(cm, cpi->complexity_map,
773 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
775 // Create a map used for cyclic background refresh.
776 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
777 vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
779 // And a place holder structure is the coding context
780 // for use if we want to save and restore it
781 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
782 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
784 CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
785 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
786 CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
787 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
788 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
789 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
790 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
791 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
792 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
793 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
794 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
795 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
796 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
797 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
798 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
799 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
801 for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
802 sizeof(cpi->mbgraph_stats[0])); i++) {
803 CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
805 sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
808 #if CONFIG_FP_MB_STATS
809 cpi->use_fp_mb_stats = 0;
810 if (cpi->use_fp_mb_stats) {
811 // a place holder used to store the first pass mb stats in the first pass
812 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
813 vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
815 cpi->twopass.frame_mb_stats_buf = NULL;
819 cpi->refresh_alt_ref_frame = 0;
821 // Note that at the moment multi_arf will not work with svc.
822 // For the current check in all the execution paths are defaulted to 0
823 // pending further tuning and testing. The code is left in place here
824 // as a place holder in regard to the required paths.
825 cpi->multi_arf_last_grp_enabled = 0;
826 if (oxcf->pass == 2) {
828 cpi->multi_arf_allowed = 0;
829 cpi->multi_arf_enabled = 0;
831 // Disable by default for now.
832 cpi->multi_arf_allowed = 0;
833 cpi->multi_arf_enabled = 0;
836 cpi->multi_arf_allowed = 0;
837 cpi->multi_arf_enabled = 0;
840 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
841 #if CONFIG_INTERNAL_STATS
842 cpi->b_calculate_ssimg = 0;
847 if (cpi->b_calculate_psnr) {
852 cpi->total_sq_error = 0;
853 cpi->total_samples = 0;
859 cpi->totalp_sq_error = 0;
860 cpi->totalp_samples = 0;
862 cpi->tot_recode_hits = 0;
863 cpi->summed_quality = 0;
864 cpi->summed_weights = 0;
865 cpi->summedp_quality = 0;
866 cpi->summedp_weights = 0;
869 if (cpi->b_calculate_ssimg) {
870 cpi->total_ssimg_y = 0;
871 cpi->total_ssimg_u = 0;
872 cpi->total_ssimg_v = 0;
873 cpi->total_ssimg_all = 0;
878 cpi->first_time_stamp_ever = INT64_MAX;
880 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
881 cpi->mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
882 cpi->mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
883 cpi->mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
884 cpi->mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
885 cal_nmvsadcosts(cpi->mb.nmvsadcost);
887 cpi->mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
888 cpi->mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
889 cpi->mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
890 cpi->mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
891 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
893 #if CONFIG_VP9_TEMPORAL_DENOISING
894 #ifdef OUTPUT_YUV_DENOISED
895 yuv_denoised_file = fopen("denoised.yuv", "ab");
898 #ifdef OUTPUT_YUV_REC
899 yuv_rec_file = fopen("rec.yuv", "wb");
903 framepsnr = fopen("framepsnr.stt", "a");
904 kf_list = fopen("kf_list.stt", "w");
907 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
909 if (oxcf->pass == 1) {
910 vp9_init_first_pass(cpi);
911 } else if (oxcf->pass == 2) {
912 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
913 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
915 if (cpi->svc.number_spatial_layers > 1
916 || cpi->svc.number_temporal_layers > 1) {
917 FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
918 FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0};
921 for (i = 0; i < oxcf->ss_number_layers; ++i) {
922 FIRSTPASS_STATS *const last_packet_for_layer =
923 &stats[packets - oxcf->ss_number_layers + i];
924 const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
925 const int packets_in_layer = (int)last_packet_for_layer->count + 1;
926 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
927 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
929 vpx_free(lc->rc_twopass_stats_in.buf);
931 lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
932 CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
933 vpx_malloc(lc->rc_twopass_stats_in.sz));
934 lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
935 lc->twopass.stats_in = lc->twopass.stats_in_start;
936 lc->twopass.stats_in_end = lc->twopass.stats_in_start
937 + packets_in_layer - 1;
938 stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
942 for (i = 0; i < packets; ++i) {
943 const int layer_id = (int)stats[i].spatial_layer_id;
944 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers
945 && stats_copy[layer_id] != NULL) {
946 *stats_copy[layer_id] = stats[i];
947 ++stats_copy[layer_id];
951 vp9_init_second_pass_spatial_svc(cpi);
953 #if CONFIG_FP_MB_STATS
954 if (cpi->use_fp_mb_stats) {
955 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
956 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
958 cpi->twopass.firstpass_mb_stats.mb_stats_start =
959 oxcf->firstpass_mb_stats_in.buf;
960 cpi->twopass.firstpass_mb_stats.mb_stats_end =
961 cpi->twopass.firstpass_mb_stats.mb_stats_start +
962 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
966 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
967 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
968 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
970 vp9_init_second_pass(cpi);
974 vp9_set_speed_features(cpi);
976 // Allocate memory to store variances for a frame.
977 CHECK_MEM_ERROR(cm, cpi->source_diff_var,
978 vpx_calloc(cm->MBs, sizeof(diff)));
979 cpi->source_var_thresh = 0;
980 cpi->frames_till_next_var_check = 0;
982 // Default rd threshold factors for mode selection
983 for (i = 0; i < BLOCK_SIZES; ++i) {
984 for (j = 0; j < MAX_MODES; ++j)
985 cpi->rd.thresh_freq_fact[i][j] = 32;
988 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF)\
989 cpi->fn_ptr[BT].sdf = SDF; \
990 cpi->fn_ptr[BT].sdaf = SDAF; \
991 cpi->fn_ptr[BT].vf = VF; \
992 cpi->fn_ptr[BT].svf = SVF; \
993 cpi->fn_ptr[BT].svaf = SVAF; \
994 cpi->fn_ptr[BT].sdx3f = SDX3F; \
995 cpi->fn_ptr[BT].sdx8f = SDX8F; \
996 cpi->fn_ptr[BT].sdx4df = SDX4DF;
998 BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
999 vp9_variance32x16, vp9_sub_pixel_variance32x16,
1000 vp9_sub_pixel_avg_variance32x16, NULL, NULL, vp9_sad32x16x4d)
1002 BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
1003 vp9_variance16x32, vp9_sub_pixel_variance16x32,
1004 vp9_sub_pixel_avg_variance16x32, NULL, NULL, vp9_sad16x32x4d)
1006 BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
1007 vp9_variance64x32, vp9_sub_pixel_variance64x32,
1008 vp9_sub_pixel_avg_variance64x32, NULL, NULL, vp9_sad64x32x4d)
1010 BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
1011 vp9_variance32x64, vp9_sub_pixel_variance32x64,
1012 vp9_sub_pixel_avg_variance32x64, NULL, NULL, vp9_sad32x64x4d)
1014 BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
1015 vp9_variance32x32, vp9_sub_pixel_variance32x32,
1016 vp9_sub_pixel_avg_variance32x32, vp9_sad32x32x3, vp9_sad32x32x8,
1019 BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
1020 vp9_variance64x64, vp9_sub_pixel_variance64x64,
1021 vp9_sub_pixel_avg_variance64x64, vp9_sad64x64x3, vp9_sad64x64x8,
1024 BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
1025 vp9_variance16x16, vp9_sub_pixel_variance16x16,
1026 vp9_sub_pixel_avg_variance16x16, vp9_sad16x16x3, vp9_sad16x16x8,
1029 BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
1030 vp9_variance16x8, vp9_sub_pixel_variance16x8,
1031 vp9_sub_pixel_avg_variance16x8,
1032 vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
1034 BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
1035 vp9_variance8x16, vp9_sub_pixel_variance8x16,
1036 vp9_sub_pixel_avg_variance8x16,
1037 vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
1039 BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
1040 vp9_variance8x8, vp9_sub_pixel_variance8x8,
1041 vp9_sub_pixel_avg_variance8x8,
1042 vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
1044 BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
1045 vp9_variance8x4, vp9_sub_pixel_variance8x4,
1046 vp9_sub_pixel_avg_variance8x4, NULL, vp9_sad8x4x8, vp9_sad8x4x4d)
1048 BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
1049 vp9_variance4x8, vp9_sub_pixel_variance4x8,
1050 vp9_sub_pixel_avg_variance4x8, NULL, vp9_sad4x8x8, vp9_sad4x8x4d)
1052 BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
1053 vp9_variance4x4, vp9_sub_pixel_variance4x4,
1054 vp9_sub_pixel_avg_variance4x4,
1055 vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
1057 /* vp9_init_quantizer() is first called here. Add check in
1058 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1059 * called later when needed. This will avoid unnecessary calls of
1060 * vp9_init_quantizer() for every frame.
1062 vp9_init_quantizer(cpi);
1064 vp9_loop_filter_init(cm);
1066 cm->error.setjmp = 0;
1071 void vp9_remove_compressor(VP9_COMP *cpi) {
1077 if (cpi && (cpi->common.current_video_frame > 0)) {
1078 #if CONFIG_INTERNAL_STATS
1080 vp9_clear_system_state();
1082 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1083 if (cpi->oxcf.pass != 1) {
1084 FILE *f = fopen("opsnr.stt", "a");
1085 double time_encoded = (cpi->last_end_time_stamp_seen
1086 - cpi->first_time_stamp_ever) / 10000000.000;
1087 double total_encode_time = (cpi->time_receive_data +
1088 cpi->time_compress_data) / 1000.000;
1089 double dr = (double)cpi->bytes * (double) 8 / (double)1000
1092 if (cpi->b_calculate_psnr) {
1093 const double total_psnr =
1094 vpx_sse_to_psnr((double)cpi->total_samples, 255.0,
1095 (double)cpi->total_sq_error);
1096 const double totalp_psnr =
1097 vpx_sse_to_psnr((double)cpi->totalp_samples, 255.0,
1098 (double)cpi->totalp_sq_error);
1099 const double total_ssim = 100 * pow(cpi->summed_quality /
1100 cpi->summed_weights, 8.0);
1101 const double totalp_ssim = 100 * pow(cpi->summedp_quality /
1102 cpi->summedp_weights, 8.0);
1104 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1105 "VPXSSIM\tVPSSIMP\t Time(ms)\n");
1106 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",
1107 dr, cpi->total / cpi->count, total_psnr,
1108 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
1112 if (cpi->b_calculate_ssimg) {
1113 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n");
1114 fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
1115 cpi->total_ssimg_y / cpi->count,
1116 cpi->total_ssimg_u / cpi->count,
1117 cpi->total_ssimg_v / cpi->count,
1118 cpi->total_ssimg_all / cpi->count, total_encode_time);
1128 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
1129 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
1130 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
1131 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
1132 cpi->time_compress_data / 1000,
1133 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
1138 #if CONFIG_VP9_TEMPORAL_DENOISING
1139 if (cpi->oxcf.noise_sensitivity > 0) {
1140 vp9_denoiser_free(&(cpi->denoiser));
1144 dealloc_compressor_data(cpi);
1147 for (i = 0; i < sizeof(cpi->mbgraph_stats) /
1148 sizeof(cpi->mbgraph_stats[0]); ++i) {
1149 vpx_free(cpi->mbgraph_stats[i].mb_stats);
1152 #if CONFIG_FP_MB_STATS
1153 if (cpi->use_fp_mb_stats) {
1154 vpx_free(cpi->twopass.frame_mb_stats_buf);
1155 cpi->twopass.frame_mb_stats_buf = NULL;
1159 vp9_remove_common(&cpi->common);
1162 #if CONFIG_VP9_TEMPORAL_DENOISING
1163 #ifdef OUTPUT_YUV_DENOISED
1164 fclose(yuv_denoised_file);
1167 #ifdef OUTPUT_YUV_REC
1168 fclose(yuv_rec_file);
1184 static int64_t get_sse(const uint8_t *a, int a_stride,
1185 const uint8_t *b, int b_stride,
1186 int width, int height) {
1187 const int dw = width % 16;
1188 const int dh = height % 16;
1189 int64_t total_sse = 0;
1190 unsigned int sse = 0;
1195 variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
1196 dw, height, &sse, &sum);
1201 variance(&a[(height - dh) * a_stride], a_stride,
1202 &b[(height - dh) * b_stride], b_stride,
1203 width - dw, dh, &sse, &sum);
1207 for (y = 0; y < height / 16; ++y) {
1208 const uint8_t *pa = a;
1209 const uint8_t *pb = b;
1210 for (x = 0; x < width / 16; ++x) {
1211 vp9_mse16x16(pa, a_stride, pb, b_stride, &sse);
1226 double psnr[4]; // total/y/u/v
1227 uint64_t sse[4]; // total/y/u/v
1228 uint32_t samples[4]; // total/y/u/v
1231 static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
1233 const int widths[3] = {a->y_width, a->uv_width, a->uv_width };
1234 const int heights[3] = {a->y_height, a->uv_height, a->uv_height};
1235 const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer, a->v_buffer };
1236 const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride};
1237 const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer, b->v_buffer };
1238 const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride};
1240 uint64_t total_sse = 0;
1241 uint32_t total_samples = 0;
1243 for (i = 0; i < 3; ++i) {
1244 const int w = widths[i];
1245 const int h = heights[i];
1246 const uint32_t samples = w * h;
1247 const uint64_t sse = get_sse(a_planes[i], a_strides[i],
1248 b_planes[i], b_strides[i],
1250 psnr->sse[1 + i] = sse;
1251 psnr->samples[1 + i] = samples;
1252 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, 255.0, (double)sse);
1255 total_samples += samples;
1258 psnr->sse[0] = total_sse;
1259 psnr->samples[0] = total_samples;
1260 psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, 255.0,
1264 static void generate_psnr_packet(VP9_COMP *cpi) {
1265 struct vpx_codec_cx_pkt pkt;
1268 calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
1269 for (i = 0; i < 4; ++i) {
1270 pkt.data.psnr.samples[i] = psnr.samples[i];
1271 pkt.data.psnr.sse[i] = psnr.sse[i];
1272 pkt.data.psnr.psnr[i] = psnr.psnr[i];
1274 pkt.kind = VPX_CODEC_PSNR_PKT;
1275 if (is_two_pass_svc(cpi))
1276 cpi->svc.layer_context[cpi->svc.spatial_layer_id].psnr_pkt = pkt.data.psnr;
1278 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
1281 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
1282 if (ref_frame_flags > 7)
1285 cpi->ref_frame_flags = ref_frame_flags;
1289 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
1290 cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
1291 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
1292 cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
1293 cpi->ext_refresh_frame_flags_pending = 1;
1296 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
1297 VP9_REFFRAME ref_frame_flag) {
1298 MV_REFERENCE_FRAME ref_frame = NONE;
1299 if (ref_frame_flag == VP9_LAST_FLAG)
1300 ref_frame = LAST_FRAME;
1301 else if (ref_frame_flag == VP9_GOLD_FLAG)
1302 ref_frame = GOLDEN_FRAME;
1303 else if (ref_frame_flag == VP9_ALT_FLAG)
1304 ref_frame = ALTREF_FRAME;
1306 return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
1309 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1310 YV12_BUFFER_CONFIG *sd) {
1311 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1313 vp8_yv12_copy_frame(cfg, sd);
1320 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1321 YV12_BUFFER_CONFIG *sd) {
1322 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1324 vp8_yv12_copy_frame(sd, cfg);
1331 int vp9_update_entropy(VP9_COMP * cpi, int update) {
1332 cpi->ext_refresh_frame_context = update;
1333 cpi->ext_refresh_frame_context_pending = 1;
1337 #if CONFIG_VP9_TEMPORAL_DENOISING
1338 #if defined(OUTPUT_YUV_DENOISED)
1339 // The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
1340 // as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
1341 // not denoise the UV channels at this time. If ever we implement UV channel
1342 // denoising we will have to modify this.
1343 void vp9_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
1344 uint8_t *src = s->y_buffer;
1345 int h = s->y_height;
1348 fwrite(src, s->y_width, 1, f);
1353 h = s->uv_height / 2;
1356 fwrite(src, s->uv_width / 2, 1, f);
1357 src += s->uv_stride + s->uv_width / 2;
1361 h = s->uv_height / 2;
1364 fwrite(src, s->uv_width / 2, 1, f);
1365 src += s->uv_stride + s->uv_width / 2;
1371 #ifdef OUTPUT_YUV_REC
1372 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
1373 YV12_BUFFER_CONFIG *s = cm->frame_to_show;
1374 uint8_t *src = s->y_buffer;
1378 fwrite(src, s->y_width, 1, yuv_rec_file);
1386 fwrite(src, s->uv_width, 1, yuv_rec_file);
1387 src += s->uv_stride;
1394 fwrite(src, s->uv_width, 1, yuv_rec_file);
1395 src += s->uv_stride;
1398 fflush(yuv_rec_file);
1402 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
1403 YV12_BUFFER_CONFIG *dst) {
1404 // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
1406 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
1407 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
1408 const int src_widths[3] = {src->y_crop_width, src->uv_crop_width,
1409 src->uv_crop_width };
1410 const int src_heights[3] = {src->y_crop_height, src->uv_crop_height,
1411 src->uv_crop_height};
1412 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
1413 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
1414 const int dst_widths[3] = {dst->y_crop_width, dst->uv_crop_width,
1415 dst->uv_crop_width};
1416 const int dst_heights[3] = {dst->y_crop_height, dst->uv_crop_height,
1417 dst->uv_crop_height};
1419 for (i = 0; i < MAX_MB_PLANE; ++i)
1420 vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
1421 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
1423 vp9_extend_frame_borders(dst);
1426 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
1427 YV12_BUFFER_CONFIG *dst) {
1428 const int src_w = src->y_crop_width;
1429 const int src_h = src->y_crop_height;
1430 const int dst_w = dst->y_crop_width;
1431 const int dst_h = dst->y_crop_height;
1432 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
1433 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
1434 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
1435 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
1436 const InterpKernel *const kernel = vp9_get_interp_kernel(EIGHTTAP);
1439 for (y = 0; y < dst_h; y += 16) {
1440 for (x = 0; x < dst_w; x += 16) {
1441 for (i = 0; i < MAX_MB_PLANE; ++i) {
1442 const int factor = (i == 0 || i == 3 ? 1 : 2);
1443 const int x_q4 = x * (16 / factor) * src_w / dst_w;
1444 const int y_q4 = y * (16 / factor) * src_h / dst_h;
1445 const int src_stride = src_strides[i];
1446 const int dst_stride = dst_strides[i];
1447 const uint8_t *src_ptr = srcs[i] + (y / factor) * src_h / dst_h *
1448 src_stride + (x / factor) * src_w / dst_w;
1449 uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
1451 vp9_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
1452 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
1453 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
1454 16 / factor, 16 / factor);
1459 vp9_extend_frame_borders(dst);
1462 // Function to test for conditions that indicate we should loop
1463 // back and recode a frame.
1464 static int recode_loop_test(const VP9_COMP *cpi,
1465 int high_limit, int low_limit,
1466 int q, int maxq, int minq) {
1467 const VP9_COMMON *const cm = &cpi->common;
1468 const RATE_CONTROL *const rc = &cpi->rc;
1469 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1470 int force_recode = 0;
1472 // Special case trap if maximum allowed frame size exceeded.
1473 if (rc->projected_frame_size > rc->max_frame_bandwidth) {
1476 // Is frame recode allowed.
1477 // Yes if either recode mode 1 is selected or mode 2 is selected
1478 // and the frame is a key frame, golden frame or alt_ref_frame
1479 } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
1480 ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
1481 (cm->frame_type == KEY_FRAME ||
1482 cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1483 // General over and under shoot tests
1484 if ((rc->projected_frame_size > high_limit && q < maxq) ||
1485 (rc->projected_frame_size < low_limit && q > minq)) {
1487 } else if (cpi->oxcf.rc_mode == VPX_CQ) {
1488 // Deal with frame undershoot and whether or not we are
1489 // below the automatically set cq level.
1490 if (q > oxcf->cq_level &&
1491 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
1496 return force_recode;
1499 void vp9_update_reference_frames(VP9_COMP *cpi) {
1500 VP9_COMMON * const cm = &cpi->common;
1502 // At this point the new frame has been encoded.
1503 // If any buffer copy / swapping is signaled it should be done here.
1504 if (cm->frame_type == KEY_FRAME) {
1505 ref_cnt_fb(cm->frame_bufs,
1506 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
1507 ref_cnt_fb(cm->frame_bufs,
1508 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
1509 } else if (vp9_preserve_existing_gf(cpi)) {
1510 // We have decided to preserve the previously existing golden frame as our
1511 // new ARF frame. However, in the short term in function
1512 // vp9_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
1513 // we're updating the GF with the current decoded frame, we save it to the
1514 // ARF slot instead.
1515 // We now have to update the ARF with the current frame and swap gld_fb_idx
1516 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
1517 // slot and, if we're updating the GF, the current frame becomes the new GF.
1520 ref_cnt_fb(cm->frame_bufs,
1521 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
1523 tmp = cpi->alt_fb_idx;
1524 cpi->alt_fb_idx = cpi->gld_fb_idx;
1525 cpi->gld_fb_idx = tmp;
1527 if (is_two_pass_svc(cpi)) {
1528 cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
1529 cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
1531 } else { /* For non key/golden frames */
1532 if (cpi->refresh_alt_ref_frame) {
1533 int arf_idx = cpi->alt_fb_idx;
1534 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
1535 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
1536 arf_idx = gf_group->arf_update_idx[gf_group->index];
1539 ref_cnt_fb(cm->frame_bufs,
1540 &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
1541 vpx_memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
1542 cpi->interp_filter_selected[0],
1543 sizeof(cpi->interp_filter_selected[0]));
1546 if (cpi->refresh_golden_frame) {
1547 ref_cnt_fb(cm->frame_bufs,
1548 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
1549 if (!cpi->rc.is_src_frame_alt_ref)
1550 vpx_memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
1551 cpi->interp_filter_selected[0],
1552 sizeof(cpi->interp_filter_selected[0]));
1554 vpx_memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
1555 cpi->interp_filter_selected[ALTREF_FRAME],
1556 sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
1560 if (cpi->refresh_last_frame) {
1561 ref_cnt_fb(cm->frame_bufs,
1562 &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
1563 if (!cpi->rc.is_src_frame_alt_ref)
1564 vpx_memcpy(cpi->interp_filter_selected[LAST_FRAME],
1565 cpi->interp_filter_selected[0],
1566 sizeof(cpi->interp_filter_selected[0]));
1568 #if CONFIG_VP9_TEMPORAL_DENOISING
1569 if (cpi->oxcf.noise_sensitivity > 0) {
1570 vp9_denoiser_update_frame_info(&cpi->denoiser,
1572 cpi->common.frame_type,
1573 cpi->refresh_alt_ref_frame,
1574 cpi->refresh_golden_frame,
1575 cpi->refresh_last_frame);
1580 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
1581 MACROBLOCKD *xd = &cpi->mb.e_mbd;
1582 struct loopfilter *lf = &cm->lf;
1584 lf->filter_level = 0;
1586 struct vpx_usec_timer timer;
1588 vp9_clear_system_state();
1590 vpx_usec_timer_start(&timer);
1592 vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
1594 vpx_usec_timer_mark(&timer);
1595 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
1598 if (lf->filter_level > 0) {
1599 vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
1602 vp9_extend_frame_inner_borders(cm->frame_to_show);
1605 void vp9_scale_references(VP9_COMP *cpi) {
1606 VP9_COMMON *cm = &cpi->common;
1607 MV_REFERENCE_FRAME ref_frame;
1608 const VP9_REFFRAME ref_mask[3] = {VP9_LAST_FLAG, VP9_GOLD_FLAG, VP9_ALT_FLAG};
1610 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1611 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
1612 const YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf;
1614 // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
1615 if ((cpi->ref_frame_flags & ref_mask[ref_frame - 1]) &&
1616 (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)) {
1617 const int new_fb = get_free_fb(cm);
1618 vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
1619 cm->width, cm->height,
1620 cm->subsampling_x, cm->subsampling_y,
1621 #if CONFIG_VP9_HIGHBITDEPTH
1622 cm->use_highbitdepth,
1624 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
1625 scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf);
1626 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
1628 cpi->scaled_ref_idx[ref_frame - 1] = idx;
1629 cm->frame_bufs[idx].ref_count++;
1634 static void release_scaled_references(VP9_COMP *cpi) {
1635 VP9_COMMON *cm = &cpi->common;
1638 for (i = 0; i < 3; i++)
1639 cm->frame_bufs[cpi->scaled_ref_idx[i]].ref_count--;
1642 static void full_to_model_count(unsigned int *model_count,
1643 unsigned int *full_count) {
1645 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
1646 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
1647 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
1648 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
1649 model_count[TWO_TOKEN] += full_count[n];
1650 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
1653 static void full_to_model_counts(vp9_coeff_count_model *model_count,
1654 vp9_coeff_count *full_count) {
1657 for (i = 0; i < PLANE_TYPES; ++i)
1658 for (j = 0; j < REF_TYPES; ++j)
1659 for (k = 0; k < COEF_BANDS; ++k)
1660 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
1661 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
1664 #if 0 && CONFIG_INTERNAL_STATS
1665 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
1666 VP9_COMMON *const cm = &cpi->common;
1667 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
1670 vp9_clear_system_state();
1672 recon_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
1674 if (cpi->twopass.total_left_stats.coded_error != 0.0)
1675 fprintf(f, "%10u %10d %10d %10d %10d"
1676 "%10"PRId64" %10"PRId64" %10"PRId64" %10"PRId64" %10d "
1677 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
1678 "%6d %6d %5d %5d %5d "
1679 "%10"PRId64" %10.3lf"
1680 "%10lf %8u %10d %10d %10d\n",
1681 cpi->common.current_video_frame, cpi->rc.this_frame_target,
1682 cpi->rc.projected_frame_size,
1683 cpi->rc.projected_frame_size / cpi->common.MBs,
1684 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
1685 cpi->rc.vbr_bits_off_target,
1686 cpi->rc.total_target_vs_actual,
1687 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
1688 cpi->rc.total_actual_bits, cm->base_qindex,
1689 vp9_convert_qindex_to_q(cm->base_qindex),
1690 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
1691 vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality),
1693 vp9_convert_qindex_to_q(cpi->oxcf.cq_level),
1694 cpi->refresh_last_frame, cpi->refresh_golden_frame,
1695 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
1696 cpi->twopass.bits_left,
1697 cpi->twopass.total_left_stats.coded_error,
1698 cpi->twopass.bits_left /
1699 (1 + cpi->twopass.total_left_stats.coded_error),
1700 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
1701 cpi->twopass.kf_zeromotion_pct);
1706 FILE *const fmodes = fopen("Modes.stt", "a");
1709 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
1710 cm->frame_type, cpi->refresh_golden_frame,
1711 cpi->refresh_alt_ref_frame);
1713 for (i = 0; i < MAX_MODES; ++i)
1714 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
1716 fprintf(fmodes, "\n");
1723 static void encode_without_recode_loop(VP9_COMP *cpi,
1725 VP9_COMMON *const cm = &cpi->common;
1726 vp9_clear_system_state();
1727 vp9_set_quantizer(cm, q);
1729 // Variance adaptive and in frame q adjustment experiments are mutually
1731 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
1732 vp9_vaq_frame_setup(cpi);
1733 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1734 vp9_setup_in_frame_q_adj(cpi);
1735 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
1736 vp9_cyclic_refresh_setup(cpi);
1738 // transform / motion compensation build reconstruction frame
1739 vp9_encode_frame(cpi);
1741 // Update the skip mb flag probabilities based on the distribution
1742 // seen in the last encoder iteration.
1743 // update_base_skip_probs(cpi);
1744 vp9_clear_system_state();
1747 static void encode_with_recode_loop(VP9_COMP *cpi,
1753 VP9_COMMON *const cm = &cpi->common;
1754 RATE_CONTROL *const rc = &cpi->rc;
1757 int overshoot_seen = 0;
1758 int undershoot_seen = 0;
1759 int q_low = bottom_index, q_high = top_index;
1760 int frame_over_shoot_limit;
1761 int frame_under_shoot_limit;
1763 // Decide frame size bounds
1764 vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
1765 &frame_under_shoot_limit,
1766 &frame_over_shoot_limit);
1769 vp9_clear_system_state();
1771 vp9_set_quantizer(cm, q);
1773 if (loop_count == 0)
1776 // Variance adaptive and in frame q adjustment experiments are mutually
1778 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
1779 vp9_vaq_frame_setup(cpi);
1780 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1781 vp9_setup_in_frame_q_adj(cpi);
1784 // transform / motion compensation build reconstruction frame
1785 vp9_encode_frame(cpi);
1787 // Update the skip mb flag probabilities based on the distribution
1788 // seen in the last encoder iteration.
1789 // update_base_skip_probs(cpi);
1791 vp9_clear_system_state();
1793 // Dummy pack of the bitstream using up to date stats to get an
1794 // accurate estimate of output frame size to determine if we need
1796 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
1797 save_coding_context(cpi);
1798 if (!cpi->sf.use_nonrd_pick_mode)
1799 vp9_pack_bitstream(cpi, dest, size);
1801 rc->projected_frame_size = (int)(*size) << 3;
1802 restore_coding_context(cpi);
1804 if (frame_over_shoot_limit == 0)
1805 frame_over_shoot_limit = 1;
1808 if (cpi->oxcf.rc_mode == VPX_Q) {
1811 if ((cm->frame_type == KEY_FRAME) &&
1812 rc->this_key_frame_forced &&
1813 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
1815 int kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
1817 int high_err_target = cpi->ambient_err;
1818 int low_err_target = cpi->ambient_err >> 1;
1820 // Prevent possible divide by zero error below for perfect KF
1823 // The key frame is not good enough or we can afford
1824 // to make it better without undue risk of popping.
1825 if ((kf_err > high_err_target &&
1826 rc->projected_frame_size <= frame_over_shoot_limit) ||
1827 (kf_err > low_err_target &&
1828 rc->projected_frame_size <= frame_under_shoot_limit)) {
1830 q_high = q > q_low ? q - 1 : q_low;
1833 q = (q * high_err_target) / kf_err;
1834 q = MIN(q, (q_high + q_low) >> 1);
1835 } else if (kf_err < low_err_target &&
1836 rc->projected_frame_size >= frame_under_shoot_limit) {
1837 // The key frame is much better than the previous frame
1839 q_low = q < q_high ? q + 1 : q_high;
1842 q = (q * low_err_target) / kf_err;
1843 q = MIN(q, (q_high + q_low + 1) >> 1);
1846 // Clamp Q to upper and lower limits:
1847 q = clamp(q, q_low, q_high);
1850 } else if (recode_loop_test(
1851 cpi, frame_over_shoot_limit, frame_under_shoot_limit,
1852 q, MAX(q_high, top_index), bottom_index)) {
1853 // Is the projected frame size out of range and are we allowed
1854 // to attempt to recode.
1858 // Frame size out of permitted range:
1859 // Update correction factor & compute new Q to try...
1861 // Frame is too large
1862 if (rc->projected_frame_size > rc->this_frame_target) {
1863 // Special case if the projected size is > the max allowed.
1864 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
1865 q_high = rc->worst_quality;
1867 // Raise Qlow as to at least the current value
1868 q_low = q < q_high ? q + 1 : q_high;
1870 if (undershoot_seen || loop_count > 1) {
1871 // Update rate_correction_factor unless
1872 vp9_rc_update_rate_correction_factors(cpi, 1);
1874 q = (q_high + q_low + 1) / 2;
1876 // Update rate_correction_factor unless
1877 vp9_rc_update_rate_correction_factors(cpi, 0);
1879 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1880 bottom_index, MAX(q_high, top_index));
1882 while (q < q_low && retries < 10) {
1883 vp9_rc_update_rate_correction_factors(cpi, 0);
1884 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1885 bottom_index, MAX(q_high, top_index));
1892 // Frame is too small
1893 q_high = q > q_low ? q - 1 : q_low;
1895 if (overshoot_seen || loop_count > 1) {
1896 vp9_rc_update_rate_correction_factors(cpi, 1);
1897 q = (q_high + q_low) / 2;
1899 vp9_rc_update_rate_correction_factors(cpi, 0);
1900 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1901 bottom_index, top_index);
1902 // Special case reset for qlow for constrained quality.
1903 // This should only trigger where there is very substantial
1904 // undershoot on a frame and the auto cq level is above
1905 // the user passsed in value.
1906 if (cpi->oxcf.rc_mode == VPX_CQ &&
1911 while (q > q_high && retries < 10) {
1912 vp9_rc_update_rate_correction_factors(cpi, 0);
1913 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1914 bottom_index, top_index);
1919 undershoot_seen = 1;
1922 // Clamp Q to upper and lower limits:
1923 q = clamp(q, q_low, q_high);
1931 // Special case for overlay frame.
1932 if (rc->is_src_frame_alt_ref &&
1933 rc->projected_frame_size < rc->max_frame_bandwidth)
1939 #if CONFIG_INTERNAL_STATS
1940 cpi->tot_recode_hits++;
1946 static int get_ref_frame_flags(const VP9_COMP *cpi) {
1947 const int *const map = cpi->common.ref_frame_map;
1948 const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
1949 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
1950 const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
1951 int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
1954 flags &= ~VP9_GOLD_FLAG;
1956 if (cpi->rc.frames_till_gf_update_due == INT_MAX && !is_two_pass_svc(cpi))
1957 flags &= ~VP9_GOLD_FLAG;
1960 flags &= ~VP9_ALT_FLAG;
1963 flags &= ~VP9_ALT_FLAG;
1968 static void set_ext_overrides(VP9_COMP *cpi) {
1969 // Overrides the defaults with the externally supplied values with
1970 // vp9_update_reference() and vp9_update_entropy() calls
1971 // Note: The overrides are valid only for the next frame passed
1972 // to encode_frame_to_data_rate() function
1973 if (cpi->ext_refresh_frame_context_pending) {
1974 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
1975 cpi->ext_refresh_frame_context_pending = 0;
1977 if (cpi->ext_refresh_frame_flags_pending) {
1978 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
1979 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
1980 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
1981 cpi->ext_refresh_frame_flags_pending = 0;
1985 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
1986 YV12_BUFFER_CONFIG *unscaled,
1987 YV12_BUFFER_CONFIG *scaled) {
1988 if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
1989 cm->mi_rows * MI_SIZE != unscaled->y_height) {
1990 scale_and_extend_frame_nonnormative(unscaled, scaled);
1997 static int is_skippable_frame(const VP9_COMP *cpi) {
1998 // If the current frame does not have non-zero motion vector detected in the
1999 // first pass, and so do its previous and forward frames, then this frame
2000 // can be skipped for partition check, and the partition size is assigned
2001 // according to the variance
2002 const SVC *const svc = &cpi->svc;
2003 const TWO_PASS *const twopass = is_two_pass_svc(cpi) ?
2004 &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass;
2006 return (!frame_is_intra_only(&cpi->common) &&
2007 twopass->stats_in - 2 > twopass->stats_in_start &&
2008 twopass->stats_in < twopass->stats_in_end &&
2009 (twopass->stats_in - 1)->pcnt_inter - (twopass->stats_in - 1)->pcnt_motion
2011 (twopass->stats_in - 2)->pcnt_inter - (twopass->stats_in - 2)->pcnt_motion
2013 twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1);
2016 static void set_arf_sign_bias(VP9_COMP *cpi) {
2017 VP9_COMMON *const cm = &cpi->common;
2020 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
2021 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2022 arf_sign_bias = cpi->rc.source_alt_ref_active &&
2023 (!cpi->refresh_alt_ref_frame ||
2024 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
2027 (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
2029 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
2032 static void set_mv_search_params(VP9_COMP *cpi) {
2033 const VP9_COMMON *const cm = &cpi->common;
2034 const unsigned int max_mv_def = MIN(cm->width, cm->height);
2036 // Default based on max resolution.
2037 cpi->mv_step_param = vp9_init_search_range(max_mv_def);
2039 if (cpi->sf.mv.auto_mv_step_size) {
2040 if (frame_is_intra_only(cm)) {
2041 // Initialize max_mv_magnitude for use in the first INTER frame
2042 // after a key/intra-only frame.
2043 cpi->max_mv_magnitude = max_mv_def;
2046 // Allow mv_steps to correspond to twice the max mv magnitude found
2047 // in the previous frame, capped by the default max_mv_magnitude based
2049 cpi->mv_step_param =
2050 vp9_init_search_range(MIN(max_mv_def, 2 * cpi->max_mv_magnitude));
2051 cpi->max_mv_magnitude = 0;
2057 int setup_interp_filter_search_mask(VP9_COMP *cpi) {
2058 INTERP_FILTER ifilter;
2059 int ref_total[MAX_REF_FRAMES] = {0};
2060 MV_REFERENCE_FRAME ref;
2062 if (cpi->common.last_frame_type == KEY_FRAME ||
2063 cpi->refresh_alt_ref_frame)
2065 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
2066 for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
2067 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
2069 for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
2070 if ((ref_total[LAST_FRAME] &&
2071 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
2072 (ref_total[GOLDEN_FRAME] == 0 ||
2073 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50
2074 < ref_total[GOLDEN_FRAME]) &&
2075 (ref_total[ALTREF_FRAME] == 0 ||
2076 cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50
2077 < ref_total[ALTREF_FRAME]))
2078 mask |= 1 << ifilter;
2083 static void encode_frame_to_data_rate(VP9_COMP *cpi,
2086 unsigned int *frame_flags) {
2087 VP9_COMMON *const cm = &cpi->common;
2088 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2089 struct segmentation *const seg = &cm->seg;
2095 set_ext_overrides(cpi);
2097 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
2098 &cpi->scaled_source);
2100 if (cpi->unscaled_last_source != NULL)
2101 cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
2102 &cpi->scaled_last_source);
2104 vp9_scale_references(cpi);
2106 vp9_clear_system_state();
2108 // Enable or disable mode based tweaking of the zbin.
2109 // For 2 pass only used where GF/ARF prediction quality
2110 // is above a threshold.
2111 cpi->zbin_mode_boost = 0;
2112 cpi->zbin_mode_boost_enabled = 0;
2114 // Set the arf sign bias for this frame.
2115 set_arf_sign_bias(cpi);
2117 // Set default state for segment based loop filter update flags.
2118 cm->lf.mode_ref_delta_update = 0;
2120 set_mv_search_params(cpi);
2122 if (cpi->oxcf.pass == 2 &&
2123 cpi->sf.adaptive_interp_filter_search)
2124 cpi->sf.interp_filter_search_mask =
2125 setup_interp_filter_search_mask(cpi);
2128 // Set various flags etc to special state if it is a key frame.
2129 if (frame_is_intra_only(cm)) {
2130 // Reset the loop filter deltas and segmentation map.
2131 vp9_reset_segment_features(&cm->seg);
2133 // If segmentation is enabled force a map update for key frames.
2135 seg->update_map = 1;
2136 seg->update_data = 1;
2139 // The alternate reference frame cannot be active for a key frame.
2140 cpi->rc.source_alt_ref_active = 0;
2142 cm->error_resilient_mode = oxcf->error_resilient_mode;
2144 // By default, encoder assumes decoder can use prev_mi.
2145 if (cm->error_resilient_mode) {
2146 cm->frame_parallel_decoding_mode = 1;
2147 cm->reset_frame_context = 0;
2148 cm->refresh_frame_context = 0;
2149 } else if (cm->intra_only) {
2150 cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
2151 // Only reset the current context.
2152 cm->reset_frame_context = 2;
2155 if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
2156 cm->frame_context_idx =
2157 cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
2158 cpi->svc.temporal_layer_id;
2160 // The probs will be updated based on the frame type of its previous
2161 // frame if frame_parallel_decoding_mode is 0. The type may vary for
2162 // the frame after a key frame in base layer since we may drop enhancement
2163 // layers. So set frame_parallel_decoding_mode to 1 in this case.
2164 if (cpi->svc.number_temporal_layers == 1) {
2165 if (cpi->svc.spatial_layer_id == 0 &&
2166 cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
2167 cm->frame_parallel_decoding_mode = 1;
2169 cm->frame_parallel_decoding_mode = 0;
2170 } else if (cpi->svc.spatial_layer_id == 0) {
2171 // Find the 2nd frame in temporal base layer and 1st frame in temporal
2172 // enhancement layers from the key frame.
2174 for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
2175 if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
2176 cm->frame_parallel_decoding_mode = 1;
2180 if (i == cpi->svc.number_temporal_layers)
2181 cm->frame_parallel_decoding_mode = 0;
2185 // Configure experimental use of segmentation for enhanced coding of
2186 // static regions if indicated.
2187 // Only allowed in second pass of two pass (as requires lagged coding)
2188 // and if the relevant speed feature flag is set.
2189 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
2190 configure_static_seg_features(cpi);
2192 // Check if the current frame is skippable for the partition search in the
2193 // second pass according to the first pass stats
2194 if (oxcf->pass == 2 &&
2195 (!cpi->use_svc || is_two_pass_svc(cpi))) {
2196 cpi->skippable_frame = is_skippable_frame(cpi);
2199 // For 1 pass CBR, check if we are dropping this frame.
2200 // Never drop on key frame.
2201 if (oxcf->pass == 0 &&
2202 oxcf->rc_mode == VPX_CBR &&
2203 cm->frame_type != KEY_FRAME) {
2204 if (vp9_rc_drop_frame(cpi)) {
2205 vp9_rc_postencode_update_drop_frame(cpi);
2206 ++cm->current_video_frame;
2211 vp9_clear_system_state();
2213 #if CONFIG_VP9_POSTPROC
2214 if (oxcf->noise_sensitivity > 0) {
2216 switch (oxcf->noise_sensitivity) {
2234 vp9_denoise(cpi->Source, cpi->Source, l);
2238 #if CONFIG_INTERNAL_STATS
2241 for (i = 0; i < MAX_MODES; ++i)
2242 cpi->mode_chosen_counts[i] = 0;
2246 vp9_set_speed_features(cpi);
2248 vp9_set_rd_speed_thresholds(cpi);
2249 vp9_set_rd_speed_thresholds_sub8x8(cpi);
2251 // Decide q and q bounds.
2252 q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index);
2254 if (!frame_is_intra_only(cm)) {
2255 cm->interp_filter = cpi->sf.default_interp_filter;
2256 /* TODO: Decide this more intelligently */
2257 vp9_set_high_precision_mv(cpi, q < HIGH_PRECISION_MV_QTHRESH);
2260 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
2261 encode_without_recode_loop(cpi, q);
2263 encode_with_recode_loop(cpi, size, dest, q, bottom_index, top_index);
2266 #if CONFIG_VP9_TEMPORAL_DENOISING
2267 #ifdef OUTPUT_YUV_DENOISED
2268 if (oxcf->noise_sensitivity > 0) {
2269 vp9_write_yuv_frame_420(&cpi->denoiser.running_avg_y[INTRA_FRAME],
2276 // Special case code to reduce pulsing when key frames are forced at a
2277 // fixed interval. Note the reconstruction error if it is the frame before
2278 // the force key frame
2279 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
2280 cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2283 // If the encoder forced a KEY_FRAME decision
2284 if (cm->frame_type == KEY_FRAME)
2285 cpi->refresh_last_frame = 1;
2287 cm->frame_to_show = get_frame_new_buffer(cm);
2289 // Pick the loop filter level for the frame.
2290 loopfilter_frame(cpi, cm);
2292 // build the bitstream
2293 vp9_pack_bitstream(cpi, dest, size);
2295 if (cm->seg.update_map)
2296 update_reference_segmentation_map(cpi);
2298 release_scaled_references(cpi);
2299 vp9_update_reference_frames(cpi);
2301 for (t = TX_4X4; t <= TX_32X32; t++)
2302 full_to_model_counts(cm->counts.coef[t], cpi->coef_counts[t]);
2304 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
2305 vp9_adapt_coef_probs(cm);
2307 if (!frame_is_intra_only(cm)) {
2308 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
2309 vp9_adapt_mode_probs(cm);
2310 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
2314 if (cpi->refresh_golden_frame == 1)
2315 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
2317 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
2319 if (cpi->refresh_alt_ref_frame == 1)
2320 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
2322 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
2324 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
2326 cm->last_frame_type = cm->frame_type;
2327 vp9_rc_postencode_update(cpi, *size);
2330 output_frame_level_debug_stats(cpi);
2333 if (cm->frame_type == KEY_FRAME) {
2334 // Tell the caller that the frame was coded as a key frame
2335 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
2337 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
2340 // Clear the one shot update flags for segmentation map and mode/ref loop
2342 cm->seg.update_map = 0;
2343 cm->seg.update_data = 0;
2344 cm->lf.mode_ref_delta_update = 0;
2346 // keep track of the last coded dimensions
2347 cm->last_width = cm->width;
2348 cm->last_height = cm->height;
2350 // reset to normal state now that we are done.
2351 if (!cm->show_existing_frame) {
2352 if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0)
2353 cm->last_show_frame = 0;
2355 cm->last_show_frame = cm->show_frame;
2358 if (cm->show_frame) {
2359 vp9_swap_mi_and_prev_mi(cm);
2361 // Don't increment frame counters if this was an altref buffer
2362 // update not a real frame
2363 ++cm->current_video_frame;
2365 vp9_inc_frame_in_layer(cpi);
2368 if (is_two_pass_svc(cpi))
2369 cpi->svc.layer_context[cpi->svc.spatial_layer_id].last_frame_type =
2373 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2374 unsigned int *frame_flags) {
2375 vp9_rc_get_svc_params(cpi);
2376 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2379 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2380 unsigned int *frame_flags) {
2381 if (cpi->oxcf.rc_mode == VPX_CBR) {
2382 vp9_rc_get_one_pass_cbr_params(cpi);
2384 vp9_rc_get_one_pass_vbr_params(cpi);
2386 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2389 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
2390 uint8_t *dest, unsigned int *frame_flags) {
2391 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
2393 vp9_rc_get_second_pass_params(cpi);
2394 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2396 vp9_twopass_postencode_update(cpi);
2399 static void init_motion_estimation(VP9_COMP *cpi) {
2400 int y_stride = cpi->scaled_source.y_stride;
2402 if (cpi->sf.mv.search_method == NSTEP) {
2403 vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
2404 } else if (cpi->sf.mv.search_method == DIAMOND) {
2405 vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
2409 static void check_initial_width(VP9_COMP *cpi, int subsampling_x,
2410 int subsampling_y) {
2411 VP9_COMMON *const cm = &cpi->common;
2413 if (!cpi->initial_width) {
2414 cm->subsampling_x = subsampling_x;
2415 cm->subsampling_y = subsampling_y;
2417 alloc_raw_frame_buffers(cpi);
2418 alloc_ref_frame_buffers(cpi);
2419 alloc_util_frame_buffers(cpi);
2421 init_motion_estimation(cpi);
2423 cpi->initial_width = cm->width;
2424 cpi->initial_height = cm->height;
2429 int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
2430 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
2432 VP9_COMMON *cm = &cpi->common;
2433 struct vpx_usec_timer timer;
2435 const int subsampling_x = sd->uv_width < sd->y_width;
2436 const int subsampling_y = sd->uv_height < sd->y_height;
2438 check_initial_width(cpi, subsampling_x, subsampling_y);
2440 vpx_usec_timer_start(&timer);
2442 #if CONFIG_SPATIAL_SVC
2443 if (is_two_pass_svc(cpi))
2444 res = vp9_svc_lookahead_push(cpi, cpi->lookahead, sd, time_stamp, end_time,
2448 res = vp9_lookahead_push(cpi->lookahead,
2449 sd, time_stamp, end_time, frame_flags);
2452 vpx_usec_timer_mark(&timer);
2453 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
2455 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
2456 (subsampling_x != 1 || subsampling_y != 1)) {
2457 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
2458 "Non-4:2:0 color space requires profile 1 or 3");
2461 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
2462 (subsampling_x == 1 && subsampling_y == 1)) {
2463 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
2464 "4:2:0 color space requires profile 0 or 2");
2472 static int frame_is_reference(const VP9_COMP *cpi) {
2473 const VP9_COMMON *cm = &cpi->common;
2475 return cm->frame_type == KEY_FRAME ||
2476 cpi->refresh_last_frame ||
2477 cpi->refresh_golden_frame ||
2478 cpi->refresh_alt_ref_frame ||
2479 cm->refresh_frame_context ||
2480 cm->lf.mode_ref_delta_update ||
2481 cm->seg.update_map ||
2482 cm->seg.update_data;
2485 void adjust_frame_rate(VP9_COMP *cpi,
2486 const struct lookahead_entry *source) {
2487 int64_t this_duration;
2490 if (source->ts_start == cpi->first_time_stamp_ever) {
2491 this_duration = source->ts_end - source->ts_start;
2494 int64_t last_duration = cpi->last_end_time_stamp_seen
2495 - cpi->last_time_stamp_seen;
2497 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
2499 // do a step update if the duration changes by 10%
2501 step = (int)((this_duration - last_duration) * 10 / last_duration);
2504 if (this_duration) {
2506 vp9_new_framerate(cpi, 10000000.0 / this_duration);
2508 // Average this frame's rate into the last second's average
2509 // frame rate. If we haven't seen 1 second yet, then average
2510 // over the whole interval seen.
2511 const double interval = MIN((double)(source->ts_end
2512 - cpi->first_time_stamp_ever), 10000000.0);
2513 double avg_duration = 10000000.0 / cpi->framerate;
2514 avg_duration *= (interval - avg_duration + this_duration);
2515 avg_duration /= interval;
2517 vp9_new_framerate(cpi, 10000000.0 / avg_duration);
2520 cpi->last_time_stamp_seen = source->ts_start;
2521 cpi->last_end_time_stamp_seen = source->ts_end;
2524 // Returns 0 if this is not an alt ref else the offset of the source frame
2525 // used as the arf midpoint.
2526 static int get_arf_src_index(VP9_COMP *cpi) {
2527 RATE_CONTROL *const rc = &cpi->rc;
2528 int arf_src_index = 0;
2529 if (is_altref_enabled(cpi)) {
2530 if (cpi->oxcf.pass == 2) {
2531 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2532 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
2533 arf_src_index = gf_group->arf_src_offset[gf_group->index];
2535 } else if (rc->source_alt_ref_pending) {
2536 arf_src_index = rc->frames_till_gf_update_due;
2539 return arf_src_index;
2542 static void check_src_altref(VP9_COMP *cpi,
2543 const struct lookahead_entry *source) {
2544 RATE_CONTROL *const rc = &cpi->rc;
2546 if (cpi->oxcf.pass == 2) {
2547 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2548 rc->is_src_frame_alt_ref =
2549 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
2551 rc->is_src_frame_alt_ref = cpi->alt_ref_source &&
2552 (source == cpi->alt_ref_source);
2555 if (rc->is_src_frame_alt_ref) {
2556 // Current frame is an ARF overlay frame.
2557 cpi->alt_ref_source = NULL;
2559 // Don't refresh the last buffer for an ARF overlay frame. It will
2560 // become the GF so preserve last as an alternative prediction option.
2561 cpi->refresh_last_frame = 0;
2565 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
2566 size_t *size, uint8_t *dest,
2567 int64_t *time_stamp, int64_t *time_end, int flush) {
2568 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2569 VP9_COMMON *const cm = &cpi->common;
2570 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
2571 RATE_CONTROL *const rc = &cpi->rc;
2572 struct vpx_usec_timer cmptimer;
2573 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
2574 struct lookahead_entry *last_source = NULL;
2575 struct lookahead_entry *source = NULL;
2576 MV_REFERENCE_FRAME ref_frame;
2579 if (is_two_pass_svc(cpi) && oxcf->pass == 2) {
2580 #if CONFIG_SPATIAL_SVC
2581 vp9_svc_lookahead_peek(cpi, cpi->lookahead, 0, 1);
2583 vp9_restore_layer_context(cpi);
2586 vpx_usec_timer_start(&cmptimer);
2588 vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
2591 cm->reset_frame_context = 0;
2592 cm->refresh_frame_context = 1;
2593 cpi->refresh_last_frame = 1;
2594 cpi->refresh_golden_frame = 0;
2595 cpi->refresh_alt_ref_frame = 0;
2597 // Should we encode an arf frame.
2598 arf_src_index = get_arf_src_index(cpi);
2599 if (arf_src_index) {
2600 assert(arf_src_index <= rc->frames_to_key);
2602 #if CONFIG_SPATIAL_SVC
2603 if (is_two_pass_svc(cpi))
2604 source = vp9_svc_lookahead_peek(cpi, cpi->lookahead, arf_src_index, 0);
2607 source = vp9_lookahead_peek(cpi->lookahead, arf_src_index);
2608 if (source != NULL) {
2609 cpi->alt_ref_source = source;
2611 #if CONFIG_SPATIAL_SVC
2612 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
2614 // Reference a hidden frame from a lower layer
2615 for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
2616 if (oxcf->ss_play_alternate[i]) {
2617 cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
2622 cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
2625 if (oxcf->arnr_max_frames > 0) {
2626 // Produce the filtered ARF frame.
2627 vp9_temporal_filter(cpi, arf_src_index);
2628 vp9_extend_frame_borders(&cpi->alt_ref_buffer);
2629 force_src_buffer = &cpi->alt_ref_buffer;
2633 cpi->refresh_alt_ref_frame = 1;
2634 cpi->refresh_golden_frame = 0;
2635 cpi->refresh_last_frame = 0;
2636 rc->is_src_frame_alt_ref = 0;
2637 rc->source_alt_ref_pending = 0;
2639 rc->source_alt_ref_pending = 0;
2644 // Get last frame source.
2645 if (cm->current_video_frame > 0) {
2646 #if CONFIG_SPATIAL_SVC
2647 if (is_two_pass_svc(cpi))
2648 last_source = vp9_svc_lookahead_peek(cpi, cpi->lookahead, -1, 0);
2651 last_source = vp9_lookahead_peek(cpi->lookahead, -1);
2652 if (last_source == NULL)
2656 // Read in the source frame.
2657 #if CONFIG_SPATIAL_SVC
2658 if (is_two_pass_svc(cpi))
2659 source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
2662 source = vp9_lookahead_pop(cpi->lookahead, flush);
2663 if (source != NULL) {
2667 // Check to see if the frame should be encoded as an arf overlay.
2668 check_src_altref(cpi, source);
2673 cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
2676 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
2678 *time_stamp = source->ts_start;
2679 *time_end = source->ts_end;
2680 *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
2684 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
2685 vp9_end_first_pass(cpi); /* get last stats packet */
2686 cpi->twopass.first_pass_done = 1;
2691 if (source->ts_start < cpi->first_time_stamp_ever) {
2692 cpi->first_time_stamp_ever = source->ts_start;
2693 cpi->last_end_time_stamp_seen = source->ts_start;
2696 // Clear down mmx registers
2697 vp9_clear_system_state();
2699 // adjust frame rates based on timestamps given
2700 if (cm->show_frame) {
2701 adjust_frame_rate(cpi, source);
2704 if (cpi->svc.number_temporal_layers > 1 &&
2705 oxcf->rc_mode == VPX_CBR) {
2706 vp9_update_temporal_layer_framerate(cpi);
2707 vp9_restore_layer_context(cpi);
2710 // start with a 0 size frame
2713 /* find a free buffer for the new frame, releasing the reference previously
2716 cm->frame_bufs[cm->new_fb_idx].ref_count--;
2717 cm->new_fb_idx = get_free_fb(cm);
2719 if (!cpi->use_svc && cpi->multi_arf_allowed) {
2720 if (cm->frame_type == KEY_FRAME) {
2721 init_buffer_indices(cpi);
2722 } else if (oxcf->pass == 2) {
2723 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2724 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
2728 cpi->frame_flags = *frame_flags;
2730 if (oxcf->pass == 2 &&
2731 cm->current_video_frame == 0 &&
2732 oxcf->allow_spatial_resampling &&
2733 oxcf->rc_mode == VPX_VBR) {
2734 // Internal scaling is triggered on the first frame.
2735 vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
2736 oxcf->scaled_frame_height);
2739 // Reset the frame pointers to the current frame size
2740 vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
2741 cm->width, cm->height,
2742 cm->subsampling_x, cm->subsampling_y,
2743 #if CONFIG_VP9_HIGHBITDEPTH
2744 cm->use_highbitdepth,
2746 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2748 alloc_util_frame_buffers(cpi);
2749 init_motion_estimation(cpi);
2751 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2752 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2753 YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf;
2754 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
2757 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
2758 buf->y_crop_width, buf->y_crop_height,
2759 cm->width, cm->height);
2761 if (vp9_is_scaled(&ref_buf->sf))
2762 vp9_extend_frame_borders(buf);
2765 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
2767 if (oxcf->aq_mode == VARIANCE_AQ) {
2771 if (oxcf->pass == 1 &&
2772 (!cpi->use_svc || is_two_pass_svc(cpi))) {
2773 const int lossless = is_lossless_requested(oxcf);
2774 cpi->mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
2775 cpi->mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
2776 vp9_first_pass(cpi, source);
2777 } else if (oxcf->pass == 2 &&
2778 (!cpi->use_svc || is_two_pass_svc(cpi))) {
2779 Pass2Encode(cpi, size, dest, frame_flags);
2780 } else if (cpi->use_svc) {
2781 SvcEncode(cpi, size, dest, frame_flags);
2784 Pass0Encode(cpi, size, dest, frame_flags);
2787 if (cm->refresh_frame_context)
2788 cm->frame_contexts[cm->frame_context_idx] = cm->fc;
2790 // Frame was dropped, release scaled references.
2792 release_scaled_references(cpi);
2796 cpi->droppable = !frame_is_reference(cpi);
2799 // Save layer specific state.
2800 if ((cpi->svc.number_temporal_layers > 1 &&
2801 oxcf->rc_mode == VPX_CBR) ||
2802 ((cpi->svc.number_temporal_layers > 1 ||
2803 cpi->svc.number_spatial_layers > 1) &&
2805 vp9_save_layer_context(cpi);
2808 vpx_usec_timer_mark(&cmptimer);
2809 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
2811 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
2812 generate_psnr_packet(cpi);
2814 #if CONFIG_INTERNAL_STATS
2816 if (oxcf->pass != 1) {
2817 cpi->bytes += (int)(*size);
2819 if (cm->show_frame) {
2822 if (cpi->b_calculate_psnr) {
2823 YV12_BUFFER_CONFIG *orig = cpi->Source;
2824 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2825 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
2827 calc_psnr(orig, recon, &psnr);
2829 cpi->total += psnr.psnr[0];
2830 cpi->total_y += psnr.psnr[1];
2831 cpi->total_u += psnr.psnr[2];
2832 cpi->total_v += psnr.psnr[3];
2833 cpi->total_sq_error += psnr.sse[0];
2834 cpi->total_samples += psnr.samples[0];
2838 double frame_ssim2 = 0, weight = 0;
2839 #if CONFIG_VP9_POSTPROC
2840 // TODO(agrange) Add resizing of post-proc buffer in here when the
2841 // encoder is changed to use on-demand buffer allocation.
2842 vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
2843 cm->lf.filter_level * 10 / 6);
2845 vp9_clear_system_state();
2847 calc_psnr(orig, pp, &psnr2);
2849 cpi->totalp += psnr2.psnr[0];
2850 cpi->totalp_y += psnr2.psnr[1];
2851 cpi->totalp_u += psnr2.psnr[2];
2852 cpi->totalp_v += psnr2.psnr[3];
2853 cpi->totalp_sq_error += psnr2.sse[0];
2854 cpi->totalp_samples += psnr2.samples[0];
2856 frame_ssim2 = vp9_calc_ssim(orig, recon, &weight);
2858 cpi->summed_quality += frame_ssim2 * weight;
2859 cpi->summed_weights += weight;
2861 frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, &weight);
2863 cpi->summedp_quality += frame_ssim2 * weight;
2864 cpi->summedp_weights += weight;
2867 FILE *f = fopen("q_used.stt", "a");
2868 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
2869 cpi->common.current_video_frame, y2, u2, v2,
2870 frame_psnr2, frame_ssim2);
2878 if (cpi->b_calculate_ssimg) {
2879 double y, u, v, frame_all;
2880 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
2881 cpi->total_ssimg_y += y;
2882 cpi->total_ssimg_u += u;
2883 cpi->total_ssimg_v += v;
2884 cpi->total_ssimg_all += frame_all;
2893 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
2894 vp9_ppflags_t *flags) {
2895 VP9_COMMON *cm = &cpi->common;
2896 #if !CONFIG_VP9_POSTPROC
2900 if (!cm->show_frame) {
2904 #if CONFIG_VP9_POSTPROC
2905 ret = vp9_post_proc_frame(cm, dest, flags);
2907 if (cm->frame_to_show) {
2908 *dest = *cm->frame_to_show;
2909 dest->y_width = cm->width;
2910 dest->y_height = cm->height;
2911 dest->uv_width = cm->width >> cm->subsampling_x;
2912 dest->uv_height = cm->height >> cm->subsampling_y;
2917 #endif // !CONFIG_VP9_POSTPROC
2918 vp9_clear_system_state();
2923 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols) {
2924 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
2925 const int mi_rows = cpi->common.mi_rows;
2926 const int mi_cols = cpi->common.mi_cols;
2929 for (r = 0; r < mi_rows; r++) {
2930 for (c = 0; c < mi_cols; c++) {
2931 cpi->segmentation_map[r * mi_cols + c] =
2932 !map[(r >> 1) * cols + (c >> 1)];
2935 vp9_enable_segfeature(&cpi->common.seg, 1, SEG_LVL_SKIP);
2936 vp9_enable_segmentation(&cpi->common.seg);
2938 vp9_disable_segmentation(&cpi->common.seg);
2946 int vp9_set_internal_size(VP9_COMP *cpi,
2947 VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
2948 VP9_COMMON *cm = &cpi->common;
2949 int hr = 0, hs = 0, vr = 0, vs = 0;
2951 if (horiz_mode > ONETWO || vert_mode > ONETWO)
2954 Scale2Ratio(horiz_mode, &hr, &hs);
2955 Scale2Ratio(vert_mode, &vr, &vs);
2957 // always go to the next whole number
2958 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
2959 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
2960 assert(cm->width <= cpi->initial_width);
2961 assert(cm->height <= cpi->initial_height);
2963 update_frame_size(cpi);
2968 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
2969 unsigned int height) {
2970 VP9_COMMON *cm = &cpi->common;
2972 check_initial_width(cpi, 1, 1);
2976 if (cm->width * 5 < cpi->initial_width) {
2977 cm->width = cpi->initial_width / 5 + 1;
2978 printf("Warning: Desired width too small, changed to %d\n", cm->width);
2980 if (cm->width > cpi->initial_width) {
2981 cm->width = cpi->initial_width;
2982 printf("Warning: Desired width too large, changed to %d\n", cm->width);
2987 cm->height = height;
2988 if (cm->height * 5 < cpi->initial_height) {
2989 cm->height = cpi->initial_height / 5 + 1;
2990 printf("Warning: Desired height too small, changed to %d\n", cm->height);
2992 if (cm->height > cpi->initial_height) {
2993 cm->height = cpi->initial_height;
2994 printf("Warning: Desired height too large, changed to %d\n", cm->height);
2997 assert(cm->width <= cpi->initial_width);
2998 assert(cm->height <= cpi->initial_height);
3000 update_frame_size(cpi);
3005 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
3006 cpi->use_svc = use_svc;
3010 int vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b) {
3011 assert(a->y_crop_width == b->y_crop_width);
3012 assert(a->y_crop_height == b->y_crop_height);
3014 return (int)get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
3015 a->y_crop_width, a->y_crop_height);
3019 int vp9_get_quantizer(VP9_COMP *cpi) {
3020 return cpi->common.base_qindex;
3023 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
3024 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF |
3025 VP8_EFLAG_NO_REF_ARF)) {
3028 if (flags & VP8_EFLAG_NO_REF_LAST)
3029 ref ^= VP9_LAST_FLAG;
3031 if (flags & VP8_EFLAG_NO_REF_GF)
3032 ref ^= VP9_GOLD_FLAG;
3034 if (flags & VP8_EFLAG_NO_REF_ARF)
3035 ref ^= VP9_ALT_FLAG;
3037 vp9_use_as_reference(cpi, ref);
3040 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
3041 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF |
3042 VP8_EFLAG_FORCE_ARF)) {
3045 if (flags & VP8_EFLAG_NO_UPD_LAST)
3046 upd ^= VP9_LAST_FLAG;
3048 if (flags & VP8_EFLAG_NO_UPD_GF)
3049 upd ^= VP9_GOLD_FLAG;
3051 if (flags & VP8_EFLAG_NO_UPD_ARF)
3052 upd ^= VP9_ALT_FLAG;
3054 vp9_update_reference(cpi, upd);
3057 if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
3058 vp9_update_entropy(cpi, 0);