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_systemdependent.h"
28 #include "vp9/common/vp9_tile_common.h"
30 #include "vp9/encoder/vp9_aq_complexity.h"
31 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
32 #include "vp9/encoder/vp9_aq_variance.h"
33 #include "vp9/encoder/vp9_bitstream.h"
34 #include "vp9/encoder/vp9_context_tree.h"
35 #include "vp9/encoder/vp9_encodeframe.h"
36 #include "vp9/encoder/vp9_encodemv.h"
37 #include "vp9/encoder/vp9_firstpass.h"
38 #include "vp9/encoder/vp9_mbgraph.h"
39 #include "vp9/encoder/vp9_encoder.h"
40 #include "vp9/encoder/vp9_picklpf.h"
41 #include "vp9/encoder/vp9_ratectrl.h"
42 #include "vp9/encoder/vp9_rdopt.h"
43 #include "vp9/encoder/vp9_segmentation.h"
44 #include "vp9/encoder/vp9_speed_features.h"
45 #if CONFIG_INTERNAL_STATS
46 #include "vp9/encoder/vp9_ssim.h"
48 #include "vp9/encoder/vp9_temporal_filter.h"
49 #include "vp9/encoder/vp9_resize.h"
50 #include "vp9/encoder/vp9_svc_layercontext.h"
52 void vp9_coef_tree_initialize();
54 #define DEFAULT_INTERP_FILTER SWITCHABLE
56 #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
58 #define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
59 // for altref computation.
60 #define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
61 // mv. Choose a very high value for
62 // now so that HIGH_PRECISION is always
65 // #define OUTPUT_YUV_REC
67 #ifdef OUTPUT_YUV_DENOISED
68 FILE *yuv_denoised_file;
83 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
109 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
110 MACROBLOCK *const mb = &cpi->mb;
111 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
112 if (cpi->common.allow_high_precision_mv) {
113 mb->mvcost = mb->nmvcost_hp;
114 mb->mvsadcost = mb->nmvsadcost_hp;
116 mb->mvcost = mb->nmvcost;
117 mb->mvsadcost = mb->nmvsadcost;
121 static void setup_frame(VP9_COMP *cpi) {
122 VP9_COMMON *const cm = &cpi->common;
123 // Set up entropy context depending on frame type. The decoder mandates
124 // the use of the default context, index 0, for keyframes and inter
125 // frames where the error_resilient_mode or intra_only flag is set. For
126 // other inter-frames the encoder currently uses only two contexts;
127 // context 1 for ALTREF frames and context 0 for the others.
128 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
129 vp9_setup_past_independence(cm);
132 cm->frame_context_idx = cpi->refresh_alt_ref_frame;
135 if (cm->frame_type == KEY_FRAME) {
136 cpi->refresh_golden_frame = 1;
137 cpi->refresh_alt_ref_frame = 1;
139 cm->fc = cm->frame_contexts[cm->frame_context_idx];
143 void vp9_initialize_enc() {
144 static int init_done = 0;
147 vp9_init_neighbors();
148 vp9_coef_tree_initialize();
149 vp9_tokenize_initialize();
151 vp9_rc_init_minq_luts();
152 vp9_entropy_mv_init();
153 vp9_entropy_mode_init();
154 vp9_temporal_filter_init();
159 static void dealloc_compressor_data(VP9_COMP *cpi) {
160 VP9_COMMON *const cm = &cpi->common;
163 // Delete sementation map
164 vpx_free(cpi->segmentation_map);
165 cpi->segmentation_map = NULL;
166 vpx_free(cm->last_frame_seg_map);
167 cm->last_frame_seg_map = NULL;
168 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
169 cpi->coding_context.last_frame_seg_map_copy = NULL;
171 vpx_free(cpi->complexity_map);
172 cpi->complexity_map = NULL;
174 vp9_cyclic_refresh_free(cpi->cyclic_refresh);
175 cpi->cyclic_refresh = NULL;
177 vp9_free_frame_buffers(cm);
178 vp9_free_context_buffers(cm);
180 vp9_free_frame_buffer(&cpi->last_frame_uf);
181 vp9_free_frame_buffer(&cpi->scaled_source);
182 vp9_free_frame_buffer(&cpi->scaled_last_source);
183 vp9_free_frame_buffer(&cpi->alt_ref_buffer);
184 vp9_lookahead_destroy(cpi->lookahead);
189 vp9_free_pc_tree(cpi);
191 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
192 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
193 vpx_free(lc->rc_twopass_stats_in.buf);
194 lc->rc_twopass_stats_in.buf = NULL;
195 lc->rc_twopass_stats_in.sz = 0;
199 static void save_coding_context(VP9_COMP *cpi) {
200 CODING_CONTEXT *const cc = &cpi->coding_context;
201 VP9_COMMON *cm = &cpi->common;
203 // Stores a snapshot of key state variables which can subsequently be
204 // restored with a call to vp9_restore_coding_context. These functions are
205 // intended for use in a re-code loop in vp9_compress_frame where the
206 // quantizer value is adjusted between loop iterations.
207 vp9_copy(cc->nmvjointcost, cpi->mb.nmvjointcost);
208 vp9_copy(cc->nmvcosts, cpi->mb.nmvcosts);
209 vp9_copy(cc->nmvcosts_hp, cpi->mb.nmvcosts_hp);
211 vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
213 vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
214 cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols));
216 vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
217 vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
222 static void restore_coding_context(VP9_COMP *cpi) {
223 CODING_CONTEXT *const cc = &cpi->coding_context;
224 VP9_COMMON *cm = &cpi->common;
226 // Restore key state variables to the snapshot state stored in the
227 // previous call to vp9_save_coding_context.
228 vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost);
229 vp9_copy(cpi->mb.nmvcosts, cc->nmvcosts);
230 vp9_copy(cpi->mb.nmvcosts_hp, cc->nmvcosts_hp);
232 vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
234 vpx_memcpy(cm->last_frame_seg_map,
235 cpi->coding_context.last_frame_seg_map_copy,
236 (cm->mi_rows * cm->mi_cols));
238 vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
239 vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
244 static void configure_static_seg_features(VP9_COMP *cpi) {
245 VP9_COMMON *const cm = &cpi->common;
246 const RATE_CONTROL *const rc = &cpi->rc;
247 struct segmentation *const seg = &cm->seg;
249 int high_q = (int)(rc->avg_q > 48.0);
252 // Disable and clear down for KF
253 if (cm->frame_type == KEY_FRAME) {
254 // Clear down the global segmentation map
255 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
257 seg->update_data = 0;
258 cpi->static_mb_pct = 0;
260 // Disable segmentation
261 vp9_disable_segmentation(seg);
263 // Clear down the segment features.
264 vp9_clearall_segfeatures(seg);
265 } else if (cpi->refresh_alt_ref_frame) {
266 // If this is an alt ref frame
267 // Clear down the global segmentation map
268 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
270 seg->update_data = 0;
271 cpi->static_mb_pct = 0;
273 // Disable segmentation and individual segment features by default
274 vp9_disable_segmentation(seg);
275 vp9_clearall_segfeatures(seg);
277 // Scan frames from current to arf frame.
278 // This function re-enables segmentation if appropriate.
279 vp9_update_mbgraph_stats(cpi);
281 // If segmentation was enabled set those features needed for the
285 seg->update_data = 1;
287 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875);
288 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
289 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
291 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
292 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
294 // Where relevant assume segment data is delta data
295 seg->abs_delta = SEGMENT_DELTADATA;
297 } else if (seg->enabled) {
298 // All other frames if segmentation has been enabled
300 // First normal frame in a valid gf or alt ref group
301 if (rc->frames_since_golden == 0) {
302 // Set up segment features for normal frames in an arf group
303 if (rc->source_alt_ref_active) {
305 seg->update_data = 1;
306 seg->abs_delta = SEGMENT_DELTADATA;
308 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125);
309 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
310 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
312 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
313 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
315 // Segment coding disabled for compred testing
316 if (high_q || (cpi->static_mb_pct == 100)) {
317 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
318 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
319 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
322 // Disable segmentation and clear down features if alt ref
323 // is not active for this group
325 vp9_disable_segmentation(seg);
327 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
330 seg->update_data = 0;
332 vp9_clearall_segfeatures(seg);
334 } else if (rc->is_src_frame_alt_ref) {
335 // Special case where we are coding over the top of a previous
337 // Segment coding disabled for compred testing
339 // Enable ref frame features for segment 0 as well
340 vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
341 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
343 // All mbs should use ALTREF_FRAME
344 vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
345 vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
346 vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
347 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
349 // Skip all MBs if high Q (0,0 mv and skip coeffs)
351 vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
352 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
354 // Enable data update
355 seg->update_data = 1;
359 // No updates.. leave things as they are.
361 seg->update_data = 0;
366 static void update_reference_segmentation_map(VP9_COMP *cpi) {
367 VP9_COMMON *const cm = &cpi->common;
368 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
369 uint8_t *cache_ptr = cm->last_frame_seg_map;
372 for (row = 0; row < cm->mi_rows; row++) {
373 MODE_INFO **mi_8x8 = mi_8x8_ptr;
374 uint8_t *cache = cache_ptr;
375 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
376 cache[0] = mi_8x8[0]->mbmi.segment_id;
377 mi_8x8_ptr += cm->mi_stride;
378 cache_ptr += cm->mi_cols;
383 static void set_speed_features(VP9_COMP *cpi) {
384 #if CONFIG_INTERNAL_STATS
386 for (i = 0; i < MAX_MODES; ++i)
387 cpi->mode_chosen_counts[i] = 0;
390 vp9_set_speed_features(cpi);
392 // Set rd thresholds based on mode and speed setting
393 vp9_set_rd_speed_thresholds(cpi);
394 vp9_set_rd_speed_thresholds_sub8x8(cpi);
397 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
398 VP9_COMMON *cm = &cpi->common;
399 const VP9EncoderConfig *oxcf = &cpi->oxcf;
401 cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
402 cm->subsampling_x, cm->subsampling_y,
403 oxcf->lag_in_frames);
405 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
406 "Failed to allocate lag buffers");
408 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
409 oxcf->width, oxcf->height,
410 cm->subsampling_x, cm->subsampling_y,
411 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
412 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
413 "Failed to allocate altref buffer");
416 static void alloc_ref_frame_buffers(VP9_COMP *cpi) {
417 VP9_COMMON *const cm = &cpi->common;
418 if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
419 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
420 "Failed to allocate frame buffers");
423 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
424 VP9_COMMON *const cm = &cpi->common;
425 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
426 cm->width, cm->height,
427 cm->subsampling_x, cm->subsampling_y,
428 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
429 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
430 "Failed to allocate last frame buffer");
432 if (vp9_realloc_frame_buffer(&cpi->scaled_source,
433 cm->width, cm->height,
434 cm->subsampling_x, cm->subsampling_y,
435 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
436 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
437 "Failed to allocate scaled source buffer");
439 if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
440 cm->width, cm->height,
441 cm->subsampling_x, cm->subsampling_y,
442 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
443 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
444 "Failed to allocate scaled last source buffer");
447 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
448 VP9_COMMON *cm = &cpi->common;
450 vp9_alloc_context_buffers(cm, cm->width, cm->height);
455 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
456 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
459 vp9_setup_pc_tree(&cpi->common, cpi);
462 static void update_frame_size(VP9_COMP *cpi) {
463 VP9_COMMON *const cm = &cpi->common;
464 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
465 vp9_update_frame_size(cm);
466 init_macroblockd(cm, xd);
469 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
470 cpi->oxcf.framerate = framerate < 0.1 ? 30 : framerate;
471 vp9_rc_update_framerate(cpi);
474 int64_t vp9_rescale(int64_t val, int64_t num, int denom) {
476 int64_t llden = denom;
479 return (llval * llnum / llden);
482 static void set_tile_limits(VP9_COMP *cpi) {
483 VP9_COMMON *const cm = &cpi->common;
485 int min_log2_tile_cols, max_log2_tile_cols;
486 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
488 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
489 min_log2_tile_cols, max_log2_tile_cols);
490 cm->log2_tile_rows = cpi->oxcf.tile_rows;
493 static void init_buffer_indices(VP9_COMP *cpi) {
499 static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
500 VP9_COMMON *const cm = &cpi->common;
504 cm->profile = oxcf->profile;
505 cm->bit_depth = oxcf->bit_depth;
507 cm->width = oxcf->width;
508 cm->height = oxcf->height;
509 vp9_alloc_compressor_data(cpi);
511 // Spatial scalability.
512 cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
513 // Temporal scalability.
514 cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
516 if ((cpi->svc.number_temporal_layers > 1 &&
517 cpi->oxcf.rc_mode == VPX_CBR) ||
518 (cpi->svc.number_spatial_layers > 1 &&
519 cpi->oxcf.mode == TWO_PASS_SECOND_BEST)) {
520 vp9_init_layer_context(cpi);
523 // change includes all joint functionality
524 vp9_change_config(cpi, oxcf);
526 cpi->static_mb_pct = 0;
527 cpi->ref_frame_flags = 0;
529 init_buffer_indices(cpi);
531 set_tile_limits(cpi);
534 static int get_pass(MODE mode) {
544 case TWO_PASS_SECOND_GOOD:
545 case TWO_PASS_SECOND_BEST:
551 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
552 VP9_COMMON *const cm = &cpi->common;
553 RATE_CONTROL *const rc = &cpi->rc;
555 if (cm->profile != oxcf->profile)
556 cm->profile = oxcf->profile;
557 cm->bit_depth = oxcf->bit_depth;
559 if (cm->profile <= PROFILE_1)
560 assert(cm->bit_depth == BITS_8);
562 assert(cm->bit_depth > BITS_8);
565 cpi->pass = get_pass(cpi->oxcf.mode);
567 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
569 cpi->refresh_golden_frame = 0;
570 cpi->refresh_last_frame = 1;
571 cm->refresh_frame_context = 1;
572 cm->reset_frame_context = 0;
574 vp9_reset_segment_features(&cm->seg);
575 vp9_set_high_precision_mv(cpi, 0);
580 for (i = 0; i < MAX_SEGMENTS; i++)
581 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
583 cpi->encode_breakout = cpi->oxcf.encode_breakout;
585 // local file playback mode == really big buffer
586 if (cpi->oxcf.rc_mode == VPX_VBR) {
587 cpi->oxcf.starting_buffer_level_ms = 60000;
588 cpi->oxcf.optimal_buffer_level_ms = 60000;
589 cpi->oxcf.maximum_buffer_size_ms = 240000;
592 rc->starting_buffer_level = vp9_rescale(cpi->oxcf.starting_buffer_level_ms,
593 cpi->oxcf.target_bandwidth, 1000);
595 // Set or reset optimal and maximum buffer levels.
596 if (cpi->oxcf.optimal_buffer_level_ms == 0)
597 rc->optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
599 rc->optimal_buffer_level = vp9_rescale(cpi->oxcf.optimal_buffer_level_ms,
600 cpi->oxcf.target_bandwidth, 1000);
602 if (cpi->oxcf.maximum_buffer_size_ms == 0)
603 rc->maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
605 rc->maximum_buffer_size = vp9_rescale(cpi->oxcf.maximum_buffer_size_ms,
606 cpi->oxcf.target_bandwidth, 1000);
607 // Under a configuration change, where maximum_buffer_size may change,
608 // keep buffer level clipped to the maximum allowed buffer size.
609 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
610 rc->buffer_level = MIN(rc->buffer_level, rc->maximum_buffer_size);
612 // Set up frame rate and related parameters rate control values.
613 vp9_new_framerate(cpi, cpi->oxcf.framerate);
615 // Set absolute upper and lower quality limits
616 rc->worst_quality = cpi->oxcf.worst_allowed_q;
617 rc->best_quality = cpi->oxcf.best_allowed_q;
619 cm->interp_filter = DEFAULT_INTERP_FILTER;
621 cm->display_width = cpi->oxcf.width;
622 cm->display_height = cpi->oxcf.height;
624 if (cpi->initial_width) {
625 // Increasing the size of the frame beyond the first seen frame, or some
626 // otherwise signaled maximum size, is not supported.
627 // TODO(jkoleszar): exit gracefully.
628 assert(cm->width <= cpi->initial_width);
629 assert(cm->height <= cpi->initial_height);
631 update_frame_size(cpi);
633 if ((cpi->svc.number_temporal_layers > 1 &&
634 cpi->oxcf.rc_mode == VPX_CBR) ||
635 (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2)) {
636 vp9_update_layer_context_change_config(cpi,
637 (int)cpi->oxcf.target_bandwidth);
640 cpi->alt_ref_source = NULL;
641 rc->is_src_frame_alt_ref = 0;
644 // Experimental RD Code
645 cpi->frame_distortion = 0;
646 cpi->last_frame_distortion = 0;
649 set_tile_limits(cpi);
651 cpi->ext_refresh_frame_flags_pending = 0;
652 cpi->ext_refresh_frame_context_pending = 0;
655 vp9_denoiser_alloc(&(cpi->denoiser), cm->width, cm->height,
656 // TODO(tkopp) An unrelated bug causes
657 // cm->subsampling_{x,y} to be uninitialized at this point
658 // in execution. For now we assume YUV-420, which is x/y
661 // cm->subsampling_x, cm->subsampling_y,
662 VP9_ENC_BORDER_IN_PIXELS);
667 #define M_LOG2_E 0.693147180559945309417
669 #define log2f(x) (log (x) / (float) M_LOG2_E)
671 static void cal_nmvjointsadcost(int *mvjointsadcost) {
672 mvjointsadcost[0] = 600;
673 mvjointsadcost[1] = 300;
674 mvjointsadcost[2] = 300;
675 mvjointsadcost[3] = 300;
678 static void cal_nmvsadcosts(int *mvsadcost[2]) {
685 double z = 256 * (2 * (log2f(8 * i) + .6));
686 mvsadcost[0][i] = (int)z;
687 mvsadcost[1][i] = (int)z;
688 mvsadcost[0][-i] = (int)z;
689 mvsadcost[1][-i] = (int)z;
690 } while (++i <= MV_MAX);
693 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
700 double z = 256 * (2 * (log2f(8 * i) + .6));
701 mvsadcost[0][i] = (int)z;
702 mvsadcost[1][i] = (int)z;
703 mvsadcost[0][-i] = (int)z;
704 mvsadcost[1][-i] = (int)z;
705 } while (++i <= MV_MAX);
709 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf) {
711 VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP));
712 VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL;
719 if (setjmp(cm->error.jmp)) {
720 cm->error.setjmp = 0;
721 vp9_remove_compressor(cpi);
725 cm->error.setjmp = 1;
731 init_config(cpi, oxcf);
732 vp9_rc_init(&cpi->oxcf, cpi->pass, &cpi->rc);
734 cm->current_video_frame = 0;
736 // Set reference frame sign bias for ALTREF frame to 1 (for now)
737 cm->ref_frame_sign_bias[ALTREF_FRAME] = 1;
739 cpi->gold_is_last = 0;
740 cpi->alt_is_last = 0;
741 cpi->gold_is_alt = 0;
743 // Create the encoder segmentation map and set all entries to 0
744 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
745 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
747 // Create a complexity map used for rd adjustment
748 CHECK_MEM_ERROR(cm, cpi->complexity_map,
749 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
751 // Create a map used for cyclic background refresh.
752 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
753 vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
755 // And a place holder structure is the coding context
756 // for use if we want to save and restore it
757 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
758 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
760 for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
761 sizeof(cpi->mbgraph_stats[0])); i++) {
762 CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
764 sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
767 cpi->refresh_alt_ref_frame = 0;
769 // Note that at the moment multi_arf will not work with svc.
770 // For the current check in all the execution paths are defaulted to 0
771 // pending further tuning and testing. The code is left in place here
772 // as a place holder in regard to the required paths.
773 if (cpi->pass == 2) {
775 cpi->multi_arf_allowed = 0;
776 cpi->multi_arf_enabled = 0;
778 // Disable by default for now.
779 cpi->multi_arf_allowed = 0;
780 cpi->multi_arf_enabled = 0;
783 cpi->multi_arf_allowed = 0;
784 cpi->multi_arf_enabled = 0;
787 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
788 #if CONFIG_INTERNAL_STATS
789 cpi->b_calculate_ssimg = 0;
794 if (cpi->b_calculate_psnr) {
799 cpi->total_sq_error = 0;
800 cpi->total_samples = 0;
806 cpi->totalp_sq_error = 0;
807 cpi->totalp_samples = 0;
809 cpi->tot_recode_hits = 0;
810 cpi->summed_quality = 0;
811 cpi->summed_weights = 0;
812 cpi->summedp_quality = 0;
813 cpi->summedp_weights = 0;
816 if (cpi->b_calculate_ssimg) {
817 cpi->total_ssimg_y = 0;
818 cpi->total_ssimg_u = 0;
819 cpi->total_ssimg_v = 0;
820 cpi->total_ssimg_all = 0;
825 cpi->first_time_stamp_ever = INT64_MAX;
827 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
828 cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX];
829 cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX];
830 cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX];
831 cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX];
832 cal_nmvsadcosts(cpi->mb.nmvsadcost);
834 cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX];
835 cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX];
836 cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX];
837 cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX];
838 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
840 #ifdef OUTPUT_YUV_DENOISED
841 yuv_denoised_file = fopen("denoised.yuv", "ab");
843 #ifdef OUTPUT_YUV_SRC
844 yuv_file = fopen("bd.yuv", "ab");
846 #ifdef OUTPUT_YUV_REC
847 yuv_rec_file = fopen("rec.yuv", "wb");
851 framepsnr = fopen("framepsnr.stt", "a");
852 kf_list = fopen("kf_list.stt", "w");
855 cpi->output_pkt_list = oxcf->output_pkt_list;
857 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
859 if (cpi->pass == 1) {
860 vp9_init_first_pass(cpi);
861 } else if (cpi->pass == 2) {
862 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
863 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
865 if (cpi->svc.number_spatial_layers > 1
866 && cpi->svc.number_temporal_layers == 1) {
867 FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
868 FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0};
871 for (i = 0; i < oxcf->ss_number_layers; ++i) {
872 FIRSTPASS_STATS *const last_packet_for_layer =
873 &stats[packets - oxcf->ss_number_layers + i];
874 const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
875 const int packets_in_layer = (int)last_packet_for_layer->count + 1;
876 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
877 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
879 vpx_free(lc->rc_twopass_stats_in.buf);
881 lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
882 CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
883 vpx_malloc(lc->rc_twopass_stats_in.sz));
884 lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
885 lc->twopass.stats_in = lc->twopass.stats_in_start;
886 lc->twopass.stats_in_end = lc->twopass.stats_in_start
887 + packets_in_layer - 1;
888 stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
892 for (i = 0; i < packets; ++i) {
893 const int layer_id = (int)stats[i].spatial_layer_id;
894 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers
895 && stats_copy[layer_id] != NULL) {
896 *stats_copy[layer_id] = stats[i];
897 ++stats_copy[layer_id];
901 vp9_init_second_pass_spatial_svc(cpi);
903 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
904 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
905 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
907 vp9_init_second_pass(cpi);
911 set_speed_features(cpi);
913 // Default rd threshold factors for mode selection
914 for (i = 0; i < BLOCK_SIZES; ++i) {
915 for (j = 0; j < MAX_MODES; ++j)
916 cpi->rd.thresh_freq_fact[i][j] = 32;
919 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF)\
920 cpi->fn_ptr[BT].sdf = SDF; \
921 cpi->fn_ptr[BT].sdaf = SDAF; \
922 cpi->fn_ptr[BT].vf = VF; \
923 cpi->fn_ptr[BT].svf = SVF; \
924 cpi->fn_ptr[BT].svaf = SVAF; \
925 cpi->fn_ptr[BT].sdx3f = SDX3F; \
926 cpi->fn_ptr[BT].sdx8f = SDX8F; \
927 cpi->fn_ptr[BT].sdx4df = SDX4DF;
929 BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
930 vp9_variance32x16, vp9_sub_pixel_variance32x16,
931 vp9_sub_pixel_avg_variance32x16, NULL, NULL, vp9_sad32x16x4d)
933 BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
934 vp9_variance16x32, vp9_sub_pixel_variance16x32,
935 vp9_sub_pixel_avg_variance16x32, NULL, NULL, vp9_sad16x32x4d)
937 BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
938 vp9_variance64x32, vp9_sub_pixel_variance64x32,
939 vp9_sub_pixel_avg_variance64x32, NULL, NULL, vp9_sad64x32x4d)
941 BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
942 vp9_variance32x64, vp9_sub_pixel_variance32x64,
943 vp9_sub_pixel_avg_variance32x64, NULL, NULL, vp9_sad32x64x4d)
945 BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
946 vp9_variance32x32, vp9_sub_pixel_variance32x32,
947 vp9_sub_pixel_avg_variance32x32, vp9_sad32x32x3, vp9_sad32x32x8,
950 BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
951 vp9_variance64x64, vp9_sub_pixel_variance64x64,
952 vp9_sub_pixel_avg_variance64x64, vp9_sad64x64x3, vp9_sad64x64x8,
955 BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
956 vp9_variance16x16, vp9_sub_pixel_variance16x16,
957 vp9_sub_pixel_avg_variance16x16, vp9_sad16x16x3, vp9_sad16x16x8,
960 BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
961 vp9_variance16x8, vp9_sub_pixel_variance16x8,
962 vp9_sub_pixel_avg_variance16x8,
963 vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
965 BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
966 vp9_variance8x16, vp9_sub_pixel_variance8x16,
967 vp9_sub_pixel_avg_variance8x16,
968 vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
970 BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
971 vp9_variance8x8, vp9_sub_pixel_variance8x8,
972 vp9_sub_pixel_avg_variance8x8,
973 vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
975 BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
976 vp9_variance8x4, vp9_sub_pixel_variance8x4,
977 vp9_sub_pixel_avg_variance8x4, NULL, vp9_sad8x4x8, vp9_sad8x4x4d)
979 BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
980 vp9_variance4x8, vp9_sub_pixel_variance4x8,
981 vp9_sub_pixel_avg_variance4x8, NULL, vp9_sad4x8x8, vp9_sad4x8x4d)
983 BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
984 vp9_variance4x4, vp9_sub_pixel_variance4x4,
985 vp9_sub_pixel_avg_variance4x4,
986 vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
988 cpi->full_search_sad = vp9_full_search_sad;
989 cpi->diamond_search_sad = vp9_diamond_search_sad;
990 cpi->refining_search_sad = vp9_refining_search_sad;
992 /* vp9_init_quantizer() is first called here. Add check in
993 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
994 * called later when needed. This will avoid unnecessary calls of
995 * vp9_init_quantizer() for every frame.
997 vp9_init_quantizer(cpi);
999 vp9_loop_filter_init(cm);
1001 cm->error.setjmp = 0;
1006 void vp9_remove_compressor(VP9_COMP *cpi) {
1012 if (cpi && (cpi->common.current_video_frame > 0)) {
1013 #if CONFIG_INTERNAL_STATS
1015 vp9_clear_system_state();
1017 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1018 if (cpi->pass != 1) {
1019 FILE *f = fopen("opsnr.stt", "a");
1020 double time_encoded = (cpi->last_end_time_stamp_seen
1021 - cpi->first_time_stamp_ever) / 10000000.000;
1022 double total_encode_time = (cpi->time_receive_data +
1023 cpi->time_compress_data) / 1000.000;
1024 double dr = (double)cpi->bytes * (double) 8 / (double)1000
1027 if (cpi->b_calculate_psnr) {
1028 const double total_psnr =
1029 vpx_sse_to_psnr((double)cpi->total_samples, 255.0,
1030 (double)cpi->total_sq_error);
1031 const double totalp_psnr =
1032 vpx_sse_to_psnr((double)cpi->totalp_samples, 255.0,
1033 (double)cpi->totalp_sq_error);
1034 const double total_ssim = 100 * pow(cpi->summed_quality /
1035 cpi->summed_weights, 8.0);
1036 const double totalp_ssim = 100 * pow(cpi->summedp_quality /
1037 cpi->summedp_weights, 8.0);
1039 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1040 "VPXSSIM\tVPSSIMP\t Time(ms)\n");
1041 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",
1042 dr, cpi->total / cpi->count, total_psnr,
1043 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
1047 if (cpi->b_calculate_ssimg) {
1048 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n");
1049 fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
1050 cpi->total_ssimg_y / cpi->count,
1051 cpi->total_ssimg_u / cpi->count,
1052 cpi->total_ssimg_v / cpi->count,
1053 cpi->total_ssimg_all / cpi->count, total_encode_time);
1063 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
1064 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
1065 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
1066 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
1067 cpi->time_compress_data / 1000,
1068 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
1073 #if CONFIG_DENOISING
1074 vp9_denoiser_free(&(cpi->denoiser));
1077 dealloc_compressor_data(cpi);
1080 for (i = 0; i < sizeof(cpi->mbgraph_stats) /
1081 sizeof(cpi->mbgraph_stats[0]); ++i) {
1082 vpx_free(cpi->mbgraph_stats[i].mb_stats);
1085 vp9_remove_common(&cpi->common);
1088 #ifdef OUTPUT_YUV_DENOISED
1089 fclose(yuv_denoised_file);
1091 #ifdef OUTPUT_YUV_SRC
1094 #ifdef OUTPUT_YUV_REC
1095 fclose(yuv_rec_file);
1111 static int64_t get_sse(const uint8_t *a, int a_stride,
1112 const uint8_t *b, int b_stride,
1113 int width, int height) {
1114 const int dw = width % 16;
1115 const int dh = height % 16;
1116 int64_t total_sse = 0;
1117 unsigned int sse = 0;
1122 variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
1123 dw, height, &sse, &sum);
1128 variance(&a[(height - dh) * a_stride], a_stride,
1129 &b[(height - dh) * b_stride], b_stride,
1130 width - dw, dh, &sse, &sum);
1134 for (y = 0; y < height / 16; ++y) {
1135 const uint8_t *pa = a;
1136 const uint8_t *pb = b;
1137 for (x = 0; x < width / 16; ++x) {
1138 vp9_mse16x16(pa, a_stride, pb, b_stride, &sse);
1153 double psnr[4]; // total/y/u/v
1154 uint64_t sse[4]; // total/y/u/v
1155 uint32_t samples[4]; // total/y/u/v
1158 static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
1160 const int widths[3] = {a->y_width, a->uv_width, a->uv_width };
1161 const int heights[3] = {a->y_height, a->uv_height, a->uv_height};
1162 const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer, a->v_buffer };
1163 const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride};
1164 const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer, b->v_buffer };
1165 const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride};
1167 uint64_t total_sse = 0;
1168 uint32_t total_samples = 0;
1170 for (i = 0; i < 3; ++i) {
1171 const int w = widths[i];
1172 const int h = heights[i];
1173 const uint32_t samples = w * h;
1174 const uint64_t sse = get_sse(a_planes[i], a_strides[i],
1175 b_planes[i], b_strides[i],
1177 psnr->sse[1 + i] = sse;
1178 psnr->samples[1 + i] = samples;
1179 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, 255.0, (double)sse);
1182 total_samples += samples;
1185 psnr->sse[0] = total_sse;
1186 psnr->samples[0] = total_samples;
1187 psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, 255.0,
1191 static void generate_psnr_packet(VP9_COMP *cpi) {
1192 struct vpx_codec_cx_pkt pkt;
1195 calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
1196 for (i = 0; i < 4; ++i) {
1197 pkt.data.psnr.samples[i] = psnr.samples[i];
1198 pkt.data.psnr.sse[i] = psnr.sse[i];
1199 pkt.data.psnr.psnr[i] = psnr.psnr[i];
1201 pkt.kind = VPX_CODEC_PSNR_PKT;
1202 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
1205 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
1206 if (ref_frame_flags > 7)
1209 cpi->ref_frame_flags = ref_frame_flags;
1213 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
1214 cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
1215 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
1216 cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
1217 cpi->ext_refresh_frame_flags_pending = 1;
1220 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
1221 VP9_REFFRAME ref_frame_flag) {
1222 MV_REFERENCE_FRAME ref_frame = NONE;
1223 if (ref_frame_flag == VP9_LAST_FLAG)
1224 ref_frame = LAST_FRAME;
1225 else if (ref_frame_flag == VP9_GOLD_FLAG)
1226 ref_frame = GOLDEN_FRAME;
1227 else if (ref_frame_flag == VP9_ALT_FLAG)
1228 ref_frame = ALTREF_FRAME;
1230 return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
1233 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1234 YV12_BUFFER_CONFIG *sd) {
1235 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1237 vp8_yv12_copy_frame(cfg, sd);
1244 int vp9_get_reference_enc(VP9_COMP *cpi, int index, YV12_BUFFER_CONFIG **fb) {
1245 VP9_COMMON *cm = &cpi->common;
1247 if (index < 0 || index >= REF_FRAMES)
1250 *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf;
1254 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1255 YV12_BUFFER_CONFIG *sd) {
1256 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1258 vp8_yv12_copy_frame(sd, cfg);
1265 int vp9_update_entropy(VP9_COMP * cpi, int update) {
1266 cpi->ext_refresh_frame_context = update;
1267 cpi->ext_refresh_frame_context_pending = 1;
1272 #if defined(OUTPUT_YUV_SRC) || defined(OUTPUT_YUV_DENOISED)
1273 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s, FILE *f) {
1274 uint8_t *src = s->y_buffer;
1275 int h = s->y_height;
1278 fwrite(src, s->y_width, 1, f);
1286 fwrite(src, s->uv_width, 1, f);
1287 src += s->uv_stride;
1294 fwrite(src, s->uv_width, 1, f);
1295 src += s->uv_stride;
1300 #ifdef OUTPUT_YUV_REC
1301 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
1302 YV12_BUFFER_CONFIG *s = cm->frame_to_show;
1303 uint8_t *src = s->y_buffer;
1307 fwrite(src, s->y_width, 1, yuv_rec_file);
1315 fwrite(src, s->uv_width, 1, yuv_rec_file);
1316 src += s->uv_stride;
1323 fwrite(src, s->uv_width, 1, yuv_rec_file);
1324 src += s->uv_stride;
1328 if (s->alpha_buffer) {
1329 src = s->alpha_buffer;
1330 h = s->alpha_height;
1332 fwrite(src, s->alpha_width, 1, yuv_rec_file);
1333 src += s->alpha_stride;
1338 fflush(yuv_rec_file);
1342 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
1343 YV12_BUFFER_CONFIG *dst) {
1344 // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
1346 const uint8_t *const srcs[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
1348 const int src_strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
1350 const int src_widths[4] = {src->y_crop_width, src->uv_crop_width,
1351 src->uv_crop_width, src->y_crop_width};
1352 const int src_heights[4] = {src->y_crop_height, src->uv_crop_height,
1353 src->uv_crop_height, src->y_crop_height};
1354 uint8_t *const dsts[4] = {dst->y_buffer, dst->u_buffer, dst->v_buffer,
1356 const int dst_strides[4] = {dst->y_stride, dst->uv_stride, dst->uv_stride,
1358 const int dst_widths[4] = {dst->y_crop_width, dst->uv_crop_width,
1359 dst->uv_crop_width, dst->y_crop_width};
1360 const int dst_heights[4] = {dst->y_crop_height, dst->uv_crop_height,
1361 dst->uv_crop_height, dst->y_crop_height};
1363 for (i = 0; i < MAX_MB_PLANE; ++i)
1364 vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
1365 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
1367 // TODO(hkuang): Call C version explicitly
1368 // as neon version only expand border size 32.
1369 vp8_yv12_extend_frame_borders_c(dst);
1372 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
1373 YV12_BUFFER_CONFIG *dst) {
1374 const int src_w = src->y_crop_width;
1375 const int src_h = src->y_crop_height;
1376 const int dst_w = dst->y_crop_width;
1377 const int dst_h = dst->y_crop_height;
1378 const uint8_t *const srcs[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
1380 const int src_strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
1382 uint8_t *const dsts[4] = {dst->y_buffer, dst->u_buffer, dst->v_buffer,
1384 const int dst_strides[4] = {dst->y_stride, dst->uv_stride, dst->uv_stride,
1386 const InterpKernel *const kernel = vp9_get_interp_kernel(EIGHTTAP);
1389 for (y = 0; y < dst_h; y += 16) {
1390 for (x = 0; x < dst_w; x += 16) {
1391 for (i = 0; i < MAX_MB_PLANE; ++i) {
1392 const int factor = (i == 0 || i == 3 ? 1 : 2);
1393 const int x_q4 = x * (16 / factor) * src_w / dst_w;
1394 const int y_q4 = y * (16 / factor) * src_h / dst_h;
1395 const int src_stride = src_strides[i];
1396 const int dst_stride = dst_strides[i];
1397 const uint8_t *src_ptr = srcs[i] + (y / factor) * src_h / dst_h *
1398 src_stride + (x / factor) * src_w / dst_w;
1399 uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
1401 vp9_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
1402 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
1403 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
1404 16 / factor, 16 / factor);
1409 // TODO(hkuang): Call C version explicitly
1410 // as neon version only expand border size 32.
1411 vp8_yv12_extend_frame_borders_c(dst);
1414 #define WRITE_RECON_BUFFER 0
1415 #if WRITE_RECON_BUFFER
1416 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) {
1421 snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame);
1422 yframe = fopen(filename, "wb");
1424 for (i = 0; i < frame->y_height; i++)
1425 fwrite(frame->y_buffer + i * frame->y_stride,
1426 frame->y_width, 1, yframe);
1429 snprintf(filename, sizeof(filename), "cx\\u%04d.raw", this_frame);
1430 yframe = fopen(filename, "wb");
1432 for (i = 0; i < frame->uv_height; i++)
1433 fwrite(frame->u_buffer + i * frame->uv_stride,
1434 frame->uv_width, 1, yframe);
1437 snprintf(filename, sizeof(filename), "cx\\v%04d.raw", this_frame);
1438 yframe = fopen(filename, "wb");
1440 for (i = 0; i < frame->uv_height; i++)
1441 fwrite(frame->v_buffer + i * frame->uv_stride,
1442 frame->uv_width, 1, yframe);
1448 // Function to test for conditions that indicate we should loop
1449 // back and recode a frame.
1450 static int recode_loop_test(const VP9_COMP *cpi,
1451 int high_limit, int low_limit,
1452 int q, int maxq, int minq) {
1453 const VP9_COMMON *const cm = &cpi->common;
1454 const RATE_CONTROL *const rc = &cpi->rc;
1455 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1456 int force_recode = 0;
1458 // Special case trap if maximum allowed frame size exceeded.
1459 if (rc->projected_frame_size > rc->max_frame_bandwidth) {
1462 // Is frame recode allowed.
1463 // Yes if either recode mode 1 is selected or mode 2 is selected
1464 // and the frame is a key frame, golden frame or alt_ref_frame
1465 } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
1466 ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
1467 (cm->frame_type == KEY_FRAME ||
1468 cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1469 // General over and under shoot tests
1470 if ((rc->projected_frame_size > high_limit && q < maxq) ||
1471 (rc->projected_frame_size < low_limit && q > minq)) {
1473 } else if (cpi->oxcf.rc_mode == VPX_CQ) {
1474 // Deal with frame undershoot and whether or not we are
1475 // below the automatically set cq level.
1476 if (q > oxcf->cq_level &&
1477 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
1482 return force_recode;
1485 void vp9_update_reference_frames(VP9_COMP *cpi) {
1486 VP9_COMMON * const cm = &cpi->common;
1488 // At this point the new frame has been encoded.
1489 // If any buffer copy / swapping is signaled it should be done here.
1490 if (cm->frame_type == KEY_FRAME) {
1491 ref_cnt_fb(cm->frame_bufs,
1492 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
1493 ref_cnt_fb(cm->frame_bufs,
1494 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
1495 } else if (!cpi->multi_arf_allowed && cpi->refresh_golden_frame &&
1496 cpi->rc.is_src_frame_alt_ref && !cpi->use_svc) {
1497 /* Preserve the previously existing golden frame and update the frame in
1498 * the alt ref slot instead. This is highly specific to the current use of
1499 * alt-ref as a forward reference, and this needs to be generalized as
1500 * other uses are implemented (like RTC/temporal scaling)
1502 * The update to the buffer in the alt ref slot was signaled in
1503 * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated
1504 * as the golden frame next time.
1508 ref_cnt_fb(cm->frame_bufs,
1509 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
1511 tmp = cpi->alt_fb_idx;
1512 cpi->alt_fb_idx = cpi->gld_fb_idx;
1513 cpi->gld_fb_idx = tmp;
1514 } else { /* For non key/golden frames */
1515 if (cpi->refresh_alt_ref_frame) {
1516 int arf_idx = cpi->alt_fb_idx;
1517 if ((cpi->pass == 2) && cpi->multi_arf_enabled) {
1518 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
1519 arf_idx = gf_group->arf_update_idx[gf_group->index];
1522 ref_cnt_fb(cm->frame_bufs,
1523 &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
1526 if (cpi->refresh_golden_frame) {
1527 ref_cnt_fb(cm->frame_bufs,
1528 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
1532 if (cpi->refresh_last_frame) {
1533 ref_cnt_fb(cm->frame_bufs,
1534 &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
1536 #if CONFIG_DENOISING
1537 vp9_denoiser_update_frame_info(&cpi->denoiser,
1539 cpi->common.frame_type,
1540 cpi->refresh_alt_ref_frame,
1541 cpi->refresh_golden_frame,
1542 cpi->refresh_last_frame);
1546 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
1547 MACROBLOCKD *xd = &cpi->mb.e_mbd;
1548 struct loopfilter *lf = &cm->lf;
1550 lf->filter_level = 0;
1552 struct vpx_usec_timer timer;
1554 vp9_clear_system_state();
1556 vpx_usec_timer_start(&timer);
1558 vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
1560 vpx_usec_timer_mark(&timer);
1561 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
1564 if (lf->filter_level > 0) {
1565 vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
1568 vp9_extend_frame_inner_borders(cm->frame_to_show);
1571 void vp9_scale_references(VP9_COMP *cpi) {
1572 VP9_COMMON *cm = &cpi->common;
1573 MV_REFERENCE_FRAME ref_frame;
1574 const VP9_REFFRAME ref_mask[3] = {VP9_LAST_FLAG, VP9_GOLD_FLAG, VP9_ALT_FLAG};
1576 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1577 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
1578 const YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf;
1580 // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
1581 if ((cpi->ref_frame_flags & ref_mask[ref_frame - 1]) &&
1582 (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)) {
1583 const int new_fb = get_free_fb(cm);
1584 vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
1585 cm->width, cm->height,
1586 cm->subsampling_x, cm->subsampling_y,
1587 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
1588 scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf);
1589 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
1591 cpi->scaled_ref_idx[ref_frame - 1] = idx;
1592 cm->frame_bufs[idx].ref_count++;
1597 static void release_scaled_references(VP9_COMP *cpi) {
1598 VP9_COMMON *cm = &cpi->common;
1601 for (i = 0; i < 3; i++)
1602 cm->frame_bufs[cpi->scaled_ref_idx[i]].ref_count--;
1605 static void full_to_model_count(unsigned int *model_count,
1606 unsigned int *full_count) {
1608 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
1609 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
1610 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
1611 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
1612 model_count[TWO_TOKEN] += full_count[n];
1613 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
1616 static void full_to_model_counts(vp9_coeff_count_model *model_count,
1617 vp9_coeff_count *full_count) {
1620 for (i = 0; i < PLANE_TYPES; ++i)
1621 for (j = 0; j < REF_TYPES; ++j)
1622 for (k = 0; k < COEF_BANDS; ++k)
1623 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
1624 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
1627 #if 0 && CONFIG_INTERNAL_STATS
1628 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
1629 VP9_COMMON *const cm = &cpi->common;
1630 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
1633 vp9_clear_system_state();
1635 recon_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
1637 if (cpi->twopass.total_left_stats.coded_error != 0.0)
1638 fprintf(f, "%10u %10d %10d %10d %10d"
1639 "%10"PRId64" %10"PRId64" %10"PRId64" %10"PRId64" %10d "
1640 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
1641 "%6d %6d %5d %5d %5d "
1642 "%10"PRId64" %10.3lf"
1643 "%10lf %8u %10d %10d %10d\n",
1644 cpi->common.current_video_frame, cpi->rc.this_frame_target,
1645 cpi->rc.projected_frame_size,
1646 cpi->rc.projected_frame_size / cpi->common.MBs,
1647 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
1648 cpi->rc.vbr_bits_off_target,
1649 cpi->rc.total_target_vs_actual,
1650 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
1651 cpi->rc.total_actual_bits, cm->base_qindex,
1652 vp9_convert_qindex_to_q(cm->base_qindex),
1653 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
1655 vp9_convert_qindex_to_q(cpi->rc.ni_av_qi),
1656 vp9_convert_qindex_to_q(cpi->oxcf.cq_level),
1657 cpi->refresh_last_frame, cpi->refresh_golden_frame,
1658 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
1659 cpi->twopass.bits_left,
1660 cpi->twopass.total_left_stats.coded_error,
1661 cpi->twopass.bits_left /
1662 (1 + cpi->twopass.total_left_stats.coded_error),
1663 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
1664 cpi->twopass.kf_zeromotion_pct);
1669 FILE *const fmodes = fopen("Modes.stt", "a");
1672 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
1673 cm->frame_type, cpi->refresh_golden_frame,
1674 cpi->refresh_alt_ref_frame);
1676 for (i = 0; i < MAX_MODES; ++i)
1677 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
1679 fprintf(fmodes, "\n");
1686 static void encode_without_recode_loop(VP9_COMP *cpi,
1688 VP9_COMMON *const cm = &cpi->common;
1689 vp9_clear_system_state();
1690 vp9_set_quantizer(cm, q);
1692 // Variance adaptive and in frame q adjustment experiments are mutually
1694 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
1695 vp9_vaq_frame_setup(cpi);
1696 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1697 vp9_setup_in_frame_q_adj(cpi);
1698 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
1699 vp9_cyclic_refresh_setup(cpi);
1701 // transform / motion compensation build reconstruction frame
1702 vp9_encode_frame(cpi);
1704 // Update the skip mb flag probabilities based on the distribution
1705 // seen in the last encoder iteration.
1706 // update_base_skip_probs(cpi);
1707 vp9_clear_system_state();
1710 static void encode_with_recode_loop(VP9_COMP *cpi,
1716 VP9_COMMON *const cm = &cpi->common;
1717 RATE_CONTROL *const rc = &cpi->rc;
1720 int overshoot_seen = 0;
1721 int undershoot_seen = 0;
1722 int q_low = bottom_index, q_high = top_index;
1723 int frame_over_shoot_limit;
1724 int frame_under_shoot_limit;
1726 // Decide frame size bounds
1727 vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
1728 &frame_under_shoot_limit,
1729 &frame_over_shoot_limit);
1732 vp9_clear_system_state();
1734 vp9_set_quantizer(cm, q);
1736 if (loop_count == 0)
1739 // Variance adaptive and in frame q adjustment experiments are mutually
1741 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
1742 vp9_vaq_frame_setup(cpi);
1743 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1744 vp9_setup_in_frame_q_adj(cpi);
1747 // transform / motion compensation build reconstruction frame
1748 vp9_encode_frame(cpi);
1750 // Update the skip mb flag probabilities based on the distribution
1751 // seen in the last encoder iteration.
1752 // update_base_skip_probs(cpi);
1754 vp9_clear_system_state();
1756 // Dummy pack of the bitstream using up to date stats to get an
1757 // accurate estimate of output frame size to determine if we need
1759 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
1760 save_coding_context(cpi);
1761 cpi->dummy_packing = 1;
1762 if (!cpi->sf.use_nonrd_pick_mode)
1763 vp9_pack_bitstream(cpi, dest, size);
1765 rc->projected_frame_size = (int)(*size) << 3;
1766 restore_coding_context(cpi);
1768 if (frame_over_shoot_limit == 0)
1769 frame_over_shoot_limit = 1;
1772 if (cpi->oxcf.rc_mode == VPX_Q) {
1775 if ((cm->frame_type == KEY_FRAME) &&
1776 rc->this_key_frame_forced &&
1777 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
1779 int kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
1781 int high_err_target = cpi->ambient_err;
1782 int low_err_target = cpi->ambient_err >> 1;
1784 // Prevent possible divide by zero error below for perfect KF
1787 // The key frame is not good enough or we can afford
1788 // to make it better without undue risk of popping.
1789 if ((kf_err > high_err_target &&
1790 rc->projected_frame_size <= frame_over_shoot_limit) ||
1791 (kf_err > low_err_target &&
1792 rc->projected_frame_size <= frame_under_shoot_limit)) {
1794 q_high = q > q_low ? q - 1 : q_low;
1797 q = (q * high_err_target) / kf_err;
1798 q = MIN(q, (q_high + q_low) >> 1);
1799 } else if (kf_err < low_err_target &&
1800 rc->projected_frame_size >= frame_under_shoot_limit) {
1801 // The key frame is much better than the previous frame
1803 q_low = q < q_high ? q + 1 : q_high;
1806 q = (q * low_err_target) / kf_err;
1807 q = MIN(q, (q_high + q_low + 1) >> 1);
1810 // Clamp Q to upper and lower limits:
1811 q = clamp(q, q_low, q_high);
1814 } else if (recode_loop_test(
1815 cpi, frame_over_shoot_limit, frame_under_shoot_limit,
1816 q, MAX(q_high, top_index), bottom_index)) {
1817 // Is the projected frame size out of range and are we allowed
1818 // to attempt to recode.
1822 // Frame size out of permitted range:
1823 // Update correction factor & compute new Q to try...
1825 // Frame is too large
1826 if (rc->projected_frame_size > rc->this_frame_target) {
1827 // Special case if the projected size is > the max allowed.
1828 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
1829 q_high = rc->worst_quality;
1831 // Raise Qlow as to at least the current value
1832 q_low = q < q_high ? q + 1 : q_high;
1834 if (undershoot_seen || loop_count > 1) {
1835 // Update rate_correction_factor unless
1836 vp9_rc_update_rate_correction_factors(cpi, 1);
1838 q = (q_high + q_low + 1) / 2;
1840 // Update rate_correction_factor unless
1841 vp9_rc_update_rate_correction_factors(cpi, 0);
1843 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1844 bottom_index, MAX(q_high, top_index));
1846 while (q < q_low && retries < 10) {
1847 vp9_rc_update_rate_correction_factors(cpi, 0);
1848 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1849 bottom_index, MAX(q_high, top_index));
1856 // Frame is too small
1857 q_high = q > q_low ? q - 1 : q_low;
1859 if (overshoot_seen || loop_count > 1) {
1860 vp9_rc_update_rate_correction_factors(cpi, 1);
1861 q = (q_high + q_low) / 2;
1863 vp9_rc_update_rate_correction_factors(cpi, 0);
1864 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1865 bottom_index, top_index);
1866 // Special case reset for qlow for constrained quality.
1867 // This should only trigger where there is very substantial
1868 // undershoot on a frame and the auto cq level is above
1869 // the user passsed in value.
1870 if (cpi->oxcf.rc_mode == VPX_CQ &&
1875 while (q > q_high && retries < 10) {
1876 vp9_rc_update_rate_correction_factors(cpi, 0);
1877 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1878 bottom_index, top_index);
1883 undershoot_seen = 1;
1886 // Clamp Q to upper and lower limits:
1887 q = clamp(q, q_low, q_high);
1895 // Special case for overlay frame.
1896 if (rc->is_src_frame_alt_ref &&
1897 rc->projected_frame_size < rc->max_frame_bandwidth)
1903 #if CONFIG_INTERNAL_STATS
1904 cpi->tot_recode_hits++;
1910 static void get_ref_frame_flags(VP9_COMP *cpi) {
1911 if (cpi->refresh_last_frame & cpi->refresh_golden_frame)
1912 cpi->gold_is_last = 1;
1913 else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame)
1914 cpi->gold_is_last = 0;
1916 if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame)
1917 cpi->alt_is_last = 1;
1918 else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame)
1919 cpi->alt_is_last = 0;
1921 if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame)
1922 cpi->gold_is_alt = 1;
1923 else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame)
1924 cpi->gold_is_alt = 0;
1926 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
1928 if (cpi->gold_is_last)
1929 cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
1931 if (cpi->rc.frames_till_gf_update_due == INT_MAX)
1932 cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
1934 if (cpi->alt_is_last)
1935 cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
1937 if (cpi->gold_is_alt)
1938 cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
1941 static void set_ext_overrides(VP9_COMP *cpi) {
1942 // Overrides the defaults with the externally supplied values with
1943 // vp9_update_reference() and vp9_update_entropy() calls
1944 // Note: The overrides are valid only for the next frame passed
1945 // to encode_frame_to_data_rate() function
1946 if (cpi->ext_refresh_frame_context_pending) {
1947 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
1948 cpi->ext_refresh_frame_context_pending = 0;
1950 if (cpi->ext_refresh_frame_flags_pending) {
1951 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
1952 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
1953 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
1954 cpi->ext_refresh_frame_flags_pending = 0;
1958 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
1959 YV12_BUFFER_CONFIG *unscaled,
1960 YV12_BUFFER_CONFIG *scaled) {
1961 if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
1962 cm->mi_rows * MI_SIZE != unscaled->y_height) {
1963 scale_and_extend_frame_nonnormative(unscaled, scaled);
1970 static void encode_frame_to_data_rate(VP9_COMP *cpi,
1973 unsigned int *frame_flags) {
1974 VP9_COMMON *const cm = &cpi->common;
1980 const SPEED_FEATURES *const sf = &cpi->sf;
1981 const unsigned int max_mv_def = MIN(cm->width, cm->height);
1982 struct segmentation *const seg = &cm->seg;
1983 set_ext_overrides(cpi);
1985 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
1986 &cpi->scaled_source);
1988 if (cpi->unscaled_last_source != NULL)
1989 cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
1990 &cpi->scaled_last_source);
1992 vp9_scale_references(cpi);
1994 vp9_clear_system_state();
1996 // Enable or disable mode based tweaking of the zbin.
1997 // For 2 pass only used where GF/ARF prediction quality
1998 // is above a threshold.
1999 cpi->zbin_mode_boost = 0;
2000 cpi->zbin_mode_boost_enabled = 0;
2002 // Current default encoder behavior for the altref sign bias.
2003 cm->ref_frame_sign_bias[ALTREF_FRAME] = cpi->rc.source_alt_ref_active;
2005 // Set default state for segment based loop filter update flags.
2006 cm->lf.mode_ref_delta_update = 0;
2008 // Initialize cpi->mv_step_param to default based on max resolution.
2009 cpi->mv_step_param = vp9_init_search_range(sf, max_mv_def);
2010 // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate.
2011 if (sf->mv.auto_mv_step_size) {
2012 if (frame_is_intra_only(cm)) {
2013 // Initialize max_mv_magnitude for use in the first INTER frame
2014 // after a key/intra-only frame.
2015 cpi->max_mv_magnitude = max_mv_def;
2018 // Allow mv_steps to correspond to twice the max mv magnitude found
2019 // in the previous frame, capped by the default max_mv_magnitude based
2021 cpi->mv_step_param = vp9_init_search_range(sf, MIN(max_mv_def, 2 *
2022 cpi->max_mv_magnitude));
2023 cpi->max_mv_magnitude = 0;
2027 // Set various flags etc to special state if it is a key frame.
2028 if (frame_is_intra_only(cm)) {
2029 // Reset the loop filter deltas and segmentation map.
2030 vp9_reset_segment_features(&cm->seg);
2032 // If segmentation is enabled force a map update for key frames.
2034 seg->update_map = 1;
2035 seg->update_data = 1;
2038 // The alternate reference frame cannot be active for a key frame.
2039 cpi->rc.source_alt_ref_active = 0;
2041 cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0);
2042 cm->frame_parallel_decoding_mode =
2043 (cpi->oxcf.frame_parallel_decoding_mode != 0);
2045 // By default, encoder assumes decoder can use prev_mi.
2046 cm->coding_use_prev_mi = 1;
2047 if (cm->error_resilient_mode) {
2048 cm->coding_use_prev_mi = 0;
2049 cm->frame_parallel_decoding_mode = 1;
2050 cm->reset_frame_context = 0;
2051 cm->refresh_frame_context = 0;
2052 } else if (cm->intra_only) {
2053 // Only reset the current context.
2054 cm->reset_frame_context = 2;
2058 // Configure experimental use of segmentation for enhanced coding of
2059 // static regions if indicated.
2060 // Only allowed in second pass of two pass (as requires lagged coding)
2061 // and if the relevant speed feature flag is set.
2062 if (cpi->pass == 2 && cpi->sf.static_segmentation)
2063 configure_static_seg_features(cpi);
2065 // For 1 pass CBR, check if we are dropping this frame.
2066 // Never drop on key frame.
2067 if (cpi->pass == 0 &&
2068 cpi->oxcf.rc_mode == VPX_CBR &&
2069 cm->frame_type != KEY_FRAME) {
2070 if (vp9_rc_drop_frame(cpi)) {
2071 vp9_rc_postencode_update_drop_frame(cpi);
2072 ++cm->current_video_frame;
2077 vp9_clear_system_state();
2079 #if CONFIG_VP9_POSTPROC
2080 if (cpi->oxcf.noise_sensitivity > 0) {
2082 switch (cpi->oxcf.noise_sensitivity) {
2100 vp9_denoise(cpi->Source, cpi->Source, l);
2104 #ifdef OUTPUT_YUV_DENOISED
2105 vp9_write_yuv_frame(&cpi->denoiser.running_avg_y[INTRA_FRAME],
2108 #ifdef OUTPUT_YUV_SRC
2109 vp9_write_yuv_frame(cpi->Source, yuv_file);
2112 set_speed_features(cpi);
2114 // Decide q and q bounds.
2115 q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index);
2117 if (!frame_is_intra_only(cm)) {
2118 cm->interp_filter = DEFAULT_INTERP_FILTER;
2119 /* TODO: Decide this more intelligently */
2120 vp9_set_high_precision_mv(cpi, q < HIGH_PRECISION_MV_QTHRESH);
2123 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
2124 encode_without_recode_loop(cpi, q);
2126 encode_with_recode_loop(cpi, size, dest, q, bottom_index, top_index);
2129 // Special case code to reduce pulsing when key frames are forced at a
2130 // fixed interval. Note the reconstruction error if it is the frame before
2131 // the force key frame
2132 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
2133 cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2136 // If the encoder forced a KEY_FRAME decision
2137 if (cm->frame_type == KEY_FRAME)
2138 cpi->refresh_last_frame = 1;
2140 cm->frame_to_show = get_frame_new_buffer(cm);
2142 #if WRITE_RECON_BUFFER
2144 write_cx_frame_to_file(cm->frame_to_show,
2145 cm->current_video_frame);
2147 write_cx_frame_to_file(cm->frame_to_show,
2148 cm->current_video_frame + 1000);
2151 // Pick the loop filter level for the frame.
2152 loopfilter_frame(cpi, cm);
2154 #if WRITE_RECON_BUFFER
2156 write_cx_frame_to_file(cm->frame_to_show,
2157 cm->current_video_frame + 2000);
2159 write_cx_frame_to_file(cm->frame_to_show,
2160 cm->current_video_frame + 3000);
2163 // build the bitstream
2164 cpi->dummy_packing = 0;
2165 vp9_pack_bitstream(cpi, dest, size);
2167 if (cm->seg.update_map)
2168 update_reference_segmentation_map(cpi);
2170 release_scaled_references(cpi);
2171 vp9_update_reference_frames(cpi);
2173 for (t = TX_4X4; t <= TX_32X32; t++)
2174 full_to_model_counts(cm->counts.coef[t], cpi->coef_counts[t]);
2176 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
2177 vp9_adapt_coef_probs(cm);
2179 if (!frame_is_intra_only(cm)) {
2180 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
2181 vp9_adapt_mode_probs(cm);
2182 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
2186 if (cpi->refresh_golden_frame == 1)
2187 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
2189 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
2191 if (cpi->refresh_alt_ref_frame == 1)
2192 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
2194 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
2196 get_ref_frame_flags(cpi);
2198 cm->last_frame_type = cm->frame_type;
2199 vp9_rc_postencode_update(cpi, *size);
2202 output_frame_level_debug_stats(cpi);
2205 if (cm->frame_type == KEY_FRAME) {
2206 // Tell the caller that the frame was coded as a key frame
2207 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
2209 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
2212 // Clear the one shot update flags for segmentation map and mode/ref loop
2214 cm->seg.update_map = 0;
2215 cm->seg.update_data = 0;
2216 cm->lf.mode_ref_delta_update = 0;
2218 // keep track of the last coded dimensions
2219 cm->last_width = cm->width;
2220 cm->last_height = cm->height;
2222 // reset to normal state now that we are done.
2223 if (!cm->show_existing_frame)
2224 cm->last_show_frame = cm->show_frame;
2226 if (cm->show_frame) {
2227 vp9_swap_mi_and_prev_mi(cm);
2229 // Don't increment frame counters if this was an altref buffer
2230 // update not a real frame
2231 ++cm->current_video_frame;
2233 vp9_inc_frame_in_layer(&cpi->svc);
2237 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2238 unsigned int *frame_flags) {
2239 vp9_rc_get_svc_params(cpi);
2240 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2243 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2244 unsigned int *frame_flags) {
2245 if (cpi->oxcf.rc_mode == VPX_CBR) {
2246 vp9_rc_get_one_pass_cbr_params(cpi);
2248 vp9_rc_get_one_pass_vbr_params(cpi);
2250 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2253 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
2254 uint8_t *dest, unsigned int *frame_flags) {
2255 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
2257 vp9_rc_get_second_pass_params(cpi);
2258 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2260 vp9_twopass_postencode_update(cpi);
2263 static void init_motion_estimation(VP9_COMP *cpi) {
2264 int y_stride = cpi->scaled_source.y_stride;
2266 if (cpi->sf.mv.search_method == NSTEP) {
2267 vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
2268 } else if (cpi->sf.mv.search_method == DIAMOND) {
2269 vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
2273 static void check_initial_width(VP9_COMP *cpi, int subsampling_x,
2274 int subsampling_y) {
2275 VP9_COMMON *const cm = &cpi->common;
2277 if (!cpi->initial_width) {
2278 cm->subsampling_x = subsampling_x;
2279 cm->subsampling_y = subsampling_y;
2281 alloc_raw_frame_buffers(cpi);
2282 alloc_ref_frame_buffers(cpi);
2283 alloc_util_frame_buffers(cpi);
2285 init_motion_estimation(cpi);
2287 cpi->initial_width = cm->width;
2288 cpi->initial_height = cm->height;
2293 int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
2294 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
2296 VP9_COMMON *cm = &cpi->common;
2297 struct vpx_usec_timer timer;
2299 const int subsampling_x = sd->uv_width < sd->y_width;
2300 const int subsampling_y = sd->uv_height < sd->y_height;
2301 const int is_spatial_svc = cpi->use_svc &&
2302 (cpi->svc.number_temporal_layers == 1);
2304 check_initial_width(cpi, subsampling_x, subsampling_y);
2306 vpx_usec_timer_start(&timer);
2308 #ifdef CONFIG_SPATIAL_SVC
2310 res = vp9_svc_lookahead_push(cpi, cpi->lookahead, sd, time_stamp, end_time,
2314 res = vp9_lookahead_push(cpi->lookahead,
2315 sd, time_stamp, end_time, frame_flags);
2318 vpx_usec_timer_mark(&timer);
2319 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
2321 if (cm->profile == PROFILE_0 && (subsampling_x != 1 || subsampling_y != 1)) {
2322 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
2323 "Non-4:2:0 color space requires profile >= 1");
2331 static int frame_is_reference(const VP9_COMP *cpi) {
2332 const VP9_COMMON *cm = &cpi->common;
2334 return cm->frame_type == KEY_FRAME ||
2335 cpi->refresh_last_frame ||
2336 cpi->refresh_golden_frame ||
2337 cpi->refresh_alt_ref_frame ||
2338 cm->refresh_frame_context ||
2339 cm->lf.mode_ref_delta_update ||
2340 cm->seg.update_map ||
2341 cm->seg.update_data;
2344 void adjust_frame_rate(VP9_COMP *cpi) {
2345 int64_t this_duration;
2348 if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
2349 this_duration = cpi->source->ts_end - cpi->source->ts_start;
2352 int64_t last_duration = cpi->last_end_time_stamp_seen
2353 - cpi->last_time_stamp_seen;
2355 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
2357 // do a step update if the duration changes by 10%
2359 step = (int)((this_duration - last_duration) * 10 / last_duration);
2362 if (this_duration) {
2364 vp9_new_framerate(cpi, 10000000.0 / this_duration);
2366 // Average this frame's rate into the last second's average
2367 // frame rate. If we haven't seen 1 second yet, then average
2368 // over the whole interval seen.
2369 const double interval = MIN((double)(cpi->source->ts_end
2370 - cpi->first_time_stamp_ever), 10000000.0);
2371 double avg_duration = 10000000.0 / cpi->oxcf.framerate;
2372 avg_duration *= (interval - avg_duration + this_duration);
2373 avg_duration /= interval;
2375 vp9_new_framerate(cpi, 10000000.0 / avg_duration);
2378 cpi->last_time_stamp_seen = cpi->source->ts_start;
2379 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
2382 // Returns 0 if this is not an alt ref else the offset of the source frame
2383 // used as the arf midpoint.
2384 static int get_arf_src_index(VP9_COMP *cpi) {
2385 RATE_CONTROL *const rc = &cpi->rc;
2386 int arf_src_index = 0;
2387 if (is_altref_enabled(&cpi->oxcf)) {
2388 if (cpi->pass == 2) {
2389 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2390 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
2391 arf_src_index = gf_group->arf_src_offset[gf_group->index];
2393 } else if (rc->source_alt_ref_pending) {
2394 arf_src_index = rc->frames_till_gf_update_due;
2397 return arf_src_index;
2400 static void is_src_altref(VP9_COMP *cpi) {
2401 RATE_CONTROL *const rc = &cpi->rc;
2403 if (cpi->pass == 2) {
2404 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2405 rc->is_src_frame_alt_ref =
2406 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
2408 rc->is_src_frame_alt_ref = cpi->alt_ref_source &&
2409 (cpi->source == cpi->alt_ref_source);
2412 if (rc->is_src_frame_alt_ref) {
2413 // Current frame is an ARF overlay frame.
2414 cpi->alt_ref_source = NULL;
2416 // Don't refresh the last buffer for an ARF overlay frame. It will
2417 // become the GF so preserve last as an alternative prediction option.
2418 cpi->refresh_last_frame = 0;
2422 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
2423 size_t *size, uint8_t *dest,
2424 int64_t *time_stamp, int64_t *time_end, int flush) {
2425 VP9_COMMON *const cm = &cpi->common;
2426 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
2427 RATE_CONTROL *const rc = &cpi->rc;
2428 struct vpx_usec_timer cmptimer;
2429 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
2430 MV_REFERENCE_FRAME ref_frame;
2432 const int is_spatial_svc = cpi->use_svc &&
2433 (cpi->svc.number_temporal_layers == 1);
2438 if (is_spatial_svc && cpi->pass == 2) {
2439 vp9_svc_lookahead_peek(cpi, cpi->lookahead, 0, 1);
2440 vp9_restore_layer_context(cpi);
2443 vpx_usec_timer_start(&cmptimer);
2446 cpi->last_source = NULL;
2448 vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
2451 cm->reset_frame_context = 0;
2452 cm->refresh_frame_context = 1;
2453 cpi->refresh_last_frame = 1;
2454 cpi->refresh_golden_frame = 0;
2455 cpi->refresh_alt_ref_frame = 0;
2457 // Should we encode an arf frame.
2458 arf_src_index = get_arf_src_index(cpi);
2459 if (arf_src_index) {
2460 assert(arf_src_index <= rc->frames_to_key);
2462 #ifdef CONFIG_SPATIAL_SVC
2464 cpi->source = vp9_svc_lookahead_peek(cpi, cpi->lookahead,
2468 cpi->source = vp9_lookahead_peek(cpi->lookahead, arf_src_index);
2469 if (cpi->source != NULL) {
2470 cpi->alt_ref_source = cpi->source;
2472 if (cpi->oxcf.arnr_max_frames > 0) {
2473 // Produce the filtered ARF frame.
2474 // TODO(agrange) merge these two functions.
2475 vp9_configure_arnr_filter(cpi, arf_src_index, rc->gfu_boost);
2476 vp9_temporal_filter_prepare(cpi, arf_src_index);
2477 vp9_extend_frame_borders(&cpi->alt_ref_buffer);
2478 force_src_buffer = &cpi->alt_ref_buffer;
2482 cpi->refresh_alt_ref_frame = 1;
2483 cpi->refresh_golden_frame = 0;
2484 cpi->refresh_last_frame = 0;
2485 rc->is_src_frame_alt_ref = 0;
2486 rc->source_alt_ref_pending = 0;
2488 rc->source_alt_ref_pending = 0;
2493 // Get last frame source.
2494 if (cm->current_video_frame > 0) {
2495 #ifdef CONFIG_SPATIAL_SVC
2497 cpi->last_source = vp9_svc_lookahead_peek(cpi, cpi->lookahead, -1, 0);
2500 cpi->last_source = vp9_lookahead_peek(cpi->lookahead, -1);
2501 if (cpi->last_source == NULL)
2505 // Read in the source frame.
2506 #ifdef CONFIG_SPATIAL_SVC
2508 cpi->source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
2511 cpi->source = vp9_lookahead_pop(cpi->lookahead, flush);
2512 if (cpi->source != NULL) {
2516 // Check to see if the frame to be encoded is an overlay for a previous
2517 // arf frame and if so configure it as such.
2523 cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
2524 : &cpi->source->img;
2526 if (cpi->last_source != NULL) {
2527 cpi->unscaled_last_source = &cpi->last_source->img;
2529 cpi->unscaled_last_source = NULL;
2532 *time_stamp = cpi->source->ts_start;
2533 *time_end = cpi->source->ts_end;
2535 (cpi->source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
2539 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
2540 vp9_end_first_pass(cpi); /* get last stats packet */
2541 cpi->twopass.first_pass_done = 1;
2546 if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
2547 cpi->first_time_stamp_ever = cpi->source->ts_start;
2548 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
2551 // adjust frame rates based on timestamps given
2552 if (cm->show_frame) {
2553 adjust_frame_rate(cpi);
2556 if (cpi->svc.number_temporal_layers > 1 &&
2557 cpi->oxcf.rc_mode == VPX_CBR) {
2558 vp9_update_temporal_layer_framerate(cpi);
2559 vp9_restore_layer_context(cpi);
2562 // start with a 0 size frame
2565 // Clear down mmx registers
2566 vp9_clear_system_state();
2568 /* find a free buffer for the new frame, releasing the reference previously
2571 cm->frame_bufs[cm->new_fb_idx].ref_count--;
2572 cm->new_fb_idx = get_free_fb(cm);
2574 if (!cpi->use_svc && cpi->multi_arf_allowed) {
2575 if (cm->frame_type == KEY_FRAME) {
2576 init_buffer_indices(cpi);
2577 } else if (cpi->pass == 2) {
2578 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2579 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
2583 cpi->frame_flags = *frame_flags;
2585 if (cpi->pass == 2 &&
2586 cm->current_video_frame == 0 &&
2587 cpi->oxcf.allow_spatial_resampling &&
2588 cpi->oxcf.rc_mode == VPX_VBR) {
2589 // Internal scaling is triggered on the first frame.
2590 vp9_set_size_literal(cpi, cpi->oxcf.scaled_frame_width,
2591 cpi->oxcf.scaled_frame_height);
2594 // Reset the frame pointers to the current frame size
2595 vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
2596 cm->width, cm->height,
2597 cm->subsampling_x, cm->subsampling_y,
2598 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2600 alloc_util_frame_buffers(cpi);
2601 init_motion_estimation(cpi);
2603 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2604 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2605 YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf;
2606 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
2609 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
2610 buf->y_crop_width, buf->y_crop_height,
2611 cm->width, cm->height);
2613 if (vp9_is_scaled(&ref_buf->sf))
2614 vp9_extend_frame_borders(buf);
2617 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
2619 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2623 if (cpi->pass == 1 &&
2624 (!cpi->use_svc || cpi->svc.number_temporal_layers == 1)) {
2625 const int lossless = is_lossless_requested(&cpi->oxcf);
2626 cpi->mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
2627 cpi->mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
2628 vp9_first_pass(cpi);
2629 } else if (cpi->pass == 2 &&
2630 (!cpi->use_svc || cpi->svc.number_temporal_layers == 1)) {
2631 Pass2Encode(cpi, size, dest, frame_flags);
2632 } else if (cpi->use_svc) {
2633 SvcEncode(cpi, size, dest, frame_flags);
2636 Pass0Encode(cpi, size, dest, frame_flags);
2639 if (cm->refresh_frame_context)
2640 cm->frame_contexts[cm->frame_context_idx] = cm->fc;
2642 // Frame was dropped, release scaled references.
2644 release_scaled_references(cpi);
2648 cpi->droppable = !frame_is_reference(cpi);
2651 // Save layer specific state.
2652 if ((cpi->svc.number_temporal_layers > 1 &&
2653 cpi->oxcf.rc_mode == VPX_CBR) ||
2654 (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2)) {
2655 vp9_save_layer_context(cpi);
2658 vpx_usec_timer_mark(&cmptimer);
2659 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
2661 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
2662 generate_psnr_packet(cpi);
2664 #if CONFIG_INTERNAL_STATS
2666 if (cpi->pass != 1) {
2667 cpi->bytes += (int)(*size);
2669 if (cm->show_frame) {
2672 if (cpi->b_calculate_psnr) {
2673 YV12_BUFFER_CONFIG *orig = cpi->Source;
2674 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2675 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
2677 calc_psnr(orig, recon, &psnr);
2679 cpi->total += psnr.psnr[0];
2680 cpi->total_y += psnr.psnr[1];
2681 cpi->total_u += psnr.psnr[2];
2682 cpi->total_v += psnr.psnr[3];
2683 cpi->total_sq_error += psnr.sse[0];
2684 cpi->total_samples += psnr.samples[0];
2688 double frame_ssim2 = 0, weight = 0;
2689 #if CONFIG_VP9_POSTPROC
2690 vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
2691 cm->lf.filter_level * 10 / 6);
2693 vp9_clear_system_state();
2695 calc_psnr(orig, pp, &psnr2);
2697 cpi->totalp += psnr2.psnr[0];
2698 cpi->totalp_y += psnr2.psnr[1];
2699 cpi->totalp_u += psnr2.psnr[2];
2700 cpi->totalp_v += psnr2.psnr[3];
2701 cpi->totalp_sq_error += psnr2.sse[0];
2702 cpi->totalp_samples += psnr2.samples[0];
2704 frame_ssim2 = vp9_calc_ssim(orig, recon, 1, &weight);
2706 cpi->summed_quality += frame_ssim2 * weight;
2707 cpi->summed_weights += weight;
2709 frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, 1, &weight);
2711 cpi->summedp_quality += frame_ssim2 * weight;
2712 cpi->summedp_weights += weight;
2715 FILE *f = fopen("q_used.stt", "a");
2716 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
2717 cpi->common.current_video_frame, y2, u2, v2,
2718 frame_psnr2, frame_ssim2);
2725 if (cpi->b_calculate_ssimg) {
2726 double y, u, v, frame_all;
2727 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
2728 cpi->total_ssimg_y += y;
2729 cpi->total_ssimg_u += u;
2730 cpi->total_ssimg_v += v;
2731 cpi->total_ssimg_all += frame_all;
2740 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
2741 vp9_ppflags_t *flags) {
2742 VP9_COMMON *cm = &cpi->common;
2743 #if !CONFIG_VP9_POSTPROC
2747 if (!cm->show_frame) {
2751 #if CONFIG_VP9_POSTPROC
2752 ret = vp9_post_proc_frame(cm, dest, flags);
2754 if (cm->frame_to_show) {
2755 *dest = *cm->frame_to_show;
2756 dest->y_width = cm->width;
2757 dest->y_height = cm->height;
2758 dest->uv_width = cm->width >> cm->subsampling_x;
2759 dest->uv_height = cm->height >> cm->subsampling_y;
2764 #endif // !CONFIG_VP9_POSTPROC
2765 vp9_clear_system_state();
2770 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols) {
2771 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
2772 const int mi_rows = cpi->common.mi_rows;
2773 const int mi_cols = cpi->common.mi_cols;
2776 for (r = 0; r < mi_rows; r++) {
2777 for (c = 0; c < mi_cols; c++) {
2778 cpi->segmentation_map[r * mi_cols + c] =
2779 !map[(r >> 1) * cols + (c >> 1)];
2782 vp9_enable_segfeature(&cpi->common.seg, 1, SEG_LVL_SKIP);
2783 vp9_enable_segmentation(&cpi->common.seg);
2785 vp9_disable_segmentation(&cpi->common.seg);
2793 int vp9_set_internal_size(VP9_COMP *cpi,
2794 VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
2795 VP9_COMMON *cm = &cpi->common;
2796 int hr = 0, hs = 0, vr = 0, vs = 0;
2798 if (horiz_mode > ONETWO || vert_mode > ONETWO)
2801 Scale2Ratio(horiz_mode, &hr, &hs);
2802 Scale2Ratio(vert_mode, &vr, &vs);
2804 // always go to the next whole number
2805 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
2806 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
2808 assert(cm->width <= cpi->initial_width);
2809 assert(cm->height <= cpi->initial_height);
2810 update_frame_size(cpi);
2814 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
2815 unsigned int height) {
2816 VP9_COMMON *cm = &cpi->common;
2818 check_initial_width(cpi, 1, 1);
2822 if (cm->width * 5 < cpi->initial_width) {
2823 cm->width = cpi->initial_width / 5 + 1;
2824 printf("Warning: Desired width too small, changed to %d\n", cm->width);
2826 if (cm->width > cpi->initial_width) {
2827 cm->width = cpi->initial_width;
2828 printf("Warning: Desired width too large, changed to %d\n", cm->width);
2833 cm->height = height;
2834 if (cm->height * 5 < cpi->initial_height) {
2835 cm->height = cpi->initial_height / 5 + 1;
2836 printf("Warning: Desired height too small, changed to %d\n", cm->height);
2838 if (cm->height > cpi->initial_height) {
2839 cm->height = cpi->initial_height;
2840 printf("Warning: Desired height too large, changed to %d\n", cm->height);
2844 assert(cm->width <= cpi->initial_width);
2845 assert(cm->height <= cpi->initial_height);
2846 update_frame_size(cpi);
2850 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
2851 cpi->use_svc = use_svc;
2855 int vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b) {
2856 assert(a->y_crop_width == b->y_crop_width);
2857 assert(a->y_crop_height == b->y_crop_height);
2859 return (int)get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
2860 a->y_crop_width, a->y_crop_height);
2864 int vp9_get_quantizer(VP9_COMP *cpi) {
2865 return cpi->common.base_qindex;
2868 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
2869 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF |
2870 VP8_EFLAG_NO_REF_ARF)) {
2873 if (flags & VP8_EFLAG_NO_REF_LAST)
2874 ref ^= VP9_LAST_FLAG;
2876 if (flags & VP8_EFLAG_NO_REF_GF)
2877 ref ^= VP9_GOLD_FLAG;
2879 if (flags & VP8_EFLAG_NO_REF_ARF)
2880 ref ^= VP9_ALT_FLAG;
2882 vp9_use_as_reference(cpi, ref);
2885 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
2886 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF |
2887 VP8_EFLAG_FORCE_ARF)) {
2890 if (flags & VP8_EFLAG_NO_UPD_LAST)
2891 upd ^= VP9_LAST_FLAG;
2893 if (flags & VP8_EFLAG_NO_UPD_GF)
2894 upd ^= VP9_GOLD_FLAG;
2896 if (flags & VP8_EFLAG_NO_UPD_ARF)
2897 upd ^= VP9_ALT_FLAG;
2899 vp9_update_reference(cpi, upd);
2902 if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
2903 vp9_update_entropy(cpi, 0);