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.
11 #include "vpx_config.h"
12 #include "./vpx_scale_rtcd.h"
13 #include "./vpx_dsp_rtcd.h"
14 #include "./vp8_rtcd.h"
15 #include "vp8/common/onyxc_int.h"
16 #include "vp8/common/blockd.h"
18 #include "vp8/common/systemdependent.h"
19 #include "vp8/common/skin_detection.h"
20 #include "vp8/encoder/quantize.h"
21 #include "vp8/common/alloccommon.h"
23 #include "firstpass.h"
24 #include "vpx_dsp/psnr.h"
25 #include "vpx_scale/vpx_scale.h"
26 #include "vp8/common/extend.h"
28 #include "vp8/common/quant_common.h"
29 #include "segmentation.h"
31 #include "vp8/common/postproc.h"
33 #include "vpx_mem/vpx_mem.h"
34 #include "vp8/common/reconintra.h"
35 #include "vp8/common/swapyv12buffer.h"
36 #include "vp8/common/threading.h"
37 #include "vpx_ports/system_state.h"
38 #include "vpx_ports/vpx_timer.h"
40 #include "vpx_ports/arm.h"
42 #if CONFIG_MULTI_RES_ENCODING
43 #include "mr_dissim.h"
45 #include "encodeframe.h"
52 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
53 extern int vp8_update_coef_context(VP8_COMP *cpi);
54 extern void vp8_update_coef_probs(VP8_COMP *cpi);
57 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
58 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
59 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
61 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source,
62 YV12_BUFFER_CONFIG *post, int filt_lvl,
63 int low_var_thresh, int flag);
64 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
65 extern unsigned int vp8_get_processor_freq();
66 extern void print_tree_update_probs();
67 extern int vp8cx_create_encoder_threads(VP8_COMP *cpi);
68 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
70 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
72 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest);
74 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
76 static void set_default_lf_deltas(VP8_COMP *cpi);
78 extern const int vp8_gf_interval_table[101];
80 #if CONFIG_INTERNAL_STATS
82 #include "vpx_dsp/ssim.h"
88 #ifdef OUTPUT_YUV_DENOISED
89 FILE *yuv_denoised_file;
91 #ifdef OUTPUT_YUV_SKINMAP
92 FILE *yuv_skinmap_file = NULL;
102 extern int skip_true_count;
103 extern int skip_false_count;
106 #ifdef VP8_ENTROPY_STATS
107 extern int intra_mode_stats[10][10][10];
111 unsigned int frames_at_speed[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
112 0, 0, 0, 0, 0, 0, 0, 0 };
113 unsigned int tot_pm = 0;
114 unsigned int cnt_pm = 0;
115 unsigned int tot_ef = 0;
116 unsigned int cnt_ef = 0;
120 extern unsigned __int64 Sectionbits[50];
121 extern int y_modes[5];
122 extern int uv_modes[4];
123 extern int b_modes[10];
125 extern int inter_y_modes[10];
126 extern int inter_uv_modes[4];
127 extern unsigned int inter_b_modes[15];
130 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
132 extern const int qrounding_factors[129];
133 extern const int qzbin_factors[129];
134 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
135 extern const int vp8cx_base_skip_false_prob[128];
137 /* Tables relating active max Q to active min Q */
138 static const unsigned char kf_low_motion_minq[QINDEX_RANGE] = {
139 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
140 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
142 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5,
143 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 10, 10, 11,
144 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16,
145 17, 17, 18, 18, 18, 18, 19, 20, 20, 21, 21, 22, 23, 23
147 static const unsigned char kf_high_motion_minq[QINDEX_RANGE] = {
148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
150 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5,
151 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10,
152 10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 13, 13, 14, 14, 15, 15, 15, 15, 16,
153 16, 16, 16, 17, 17, 18, 18, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
154 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30
156 static const unsigned char gf_low_motion_minq[QINDEX_RANGE] = {
157 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3,
158 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8,
159 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15,
160 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24,
161 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34,
162 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 44,
163 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58
165 static const unsigned char gf_mid_motion_minq[QINDEX_RANGE] = {
166 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5,
167 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11,
168 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16, 17, 17, 18,
169 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
170 28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37,
171 37, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44, 45, 46, 47, 48, 49, 50,
172 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
174 static const unsigned char gf_high_motion_minq[QINDEX_RANGE] = {
175 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
176 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11,
177 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21,
178 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30,
179 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40,
180 40, 41, 41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
181 57, 58, 59, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80
183 static const unsigned char inter_minq[QINDEX_RANGE] = {
184 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11,
185 11, 12, 13, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24,
186 24, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 33, 33, 34, 35, 36, 36, 37, 38,
187 39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53,
188 54, 55, 55, 56, 57, 58, 59, 60, 60, 61, 62, 63, 64, 65, 66, 67, 67, 68, 69,
189 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 86,
190 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100
193 #ifdef PACKET_TESTING
194 extern FILE *vpxlogc;
197 static void save_layer_context(VP8_COMP *cpi) {
198 LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];
200 /* Save layer dependent coding state */
201 lc->target_bandwidth = cpi->target_bandwidth;
202 lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
203 lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
204 lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
205 lc->starting_buffer_level_in_ms = cpi->oxcf.starting_buffer_level_in_ms;
206 lc->optimal_buffer_level_in_ms = cpi->oxcf.optimal_buffer_level_in_ms;
207 lc->maximum_buffer_size_in_ms = cpi->oxcf.maximum_buffer_size_in_ms;
208 lc->buffer_level = cpi->buffer_level;
209 lc->bits_off_target = cpi->bits_off_target;
210 lc->total_actual_bits = cpi->total_actual_bits;
211 lc->worst_quality = cpi->worst_quality;
212 lc->active_worst_quality = cpi->active_worst_quality;
213 lc->best_quality = cpi->best_quality;
214 lc->active_best_quality = cpi->active_best_quality;
215 lc->ni_av_qi = cpi->ni_av_qi;
216 lc->ni_tot_qi = cpi->ni_tot_qi;
217 lc->ni_frames = cpi->ni_frames;
218 lc->avg_frame_qindex = cpi->avg_frame_qindex;
219 lc->rate_correction_factor = cpi->rate_correction_factor;
220 lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor;
221 lc->gf_rate_correction_factor = cpi->gf_rate_correction_factor;
222 lc->zbin_over_quant = cpi->mb.zbin_over_quant;
223 lc->inter_frame_target = cpi->inter_frame_target;
224 lc->total_byte_count = cpi->total_byte_count;
225 lc->filter_level = cpi->common.filter_level;
227 lc->last_frame_percent_intra = cpi->last_frame_percent_intra;
229 memcpy(lc->count_mb_ref_frame_usage, cpi->mb.count_mb_ref_frame_usage,
230 sizeof(cpi->mb.count_mb_ref_frame_usage));
233 static void restore_layer_context(VP8_COMP *cpi, const int layer) {
234 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
236 /* Restore layer dependent coding state */
237 cpi->current_layer = layer;
238 cpi->target_bandwidth = lc->target_bandwidth;
239 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
240 cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
241 cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
242 cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
243 cpi->oxcf.starting_buffer_level_in_ms = lc->starting_buffer_level_in_ms;
244 cpi->oxcf.optimal_buffer_level_in_ms = lc->optimal_buffer_level_in_ms;
245 cpi->oxcf.maximum_buffer_size_in_ms = lc->maximum_buffer_size_in_ms;
246 cpi->buffer_level = lc->buffer_level;
247 cpi->bits_off_target = lc->bits_off_target;
248 cpi->total_actual_bits = lc->total_actual_bits;
249 cpi->active_worst_quality = lc->active_worst_quality;
250 cpi->active_best_quality = lc->active_best_quality;
251 cpi->ni_av_qi = lc->ni_av_qi;
252 cpi->ni_tot_qi = lc->ni_tot_qi;
253 cpi->ni_frames = lc->ni_frames;
254 cpi->avg_frame_qindex = lc->avg_frame_qindex;
255 cpi->rate_correction_factor = lc->rate_correction_factor;
256 cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor;
257 cpi->gf_rate_correction_factor = lc->gf_rate_correction_factor;
258 cpi->mb.zbin_over_quant = lc->zbin_over_quant;
259 cpi->inter_frame_target = lc->inter_frame_target;
260 cpi->total_byte_count = lc->total_byte_count;
261 cpi->common.filter_level = lc->filter_level;
263 cpi->last_frame_percent_intra = lc->last_frame_percent_intra;
265 memcpy(cpi->mb.count_mb_ref_frame_usage, lc->count_mb_ref_frame_usage,
266 sizeof(cpi->mb.count_mb_ref_frame_usage));
269 static int rescale(int val, int num, int denom) {
271 int64_t llden = denom;
274 return (int)(llval * llnum / llden);
277 static void init_temporal_layer_context(VP8_COMP *cpi, VP8_CONFIG *oxcf,
279 double prev_layer_framerate) {
280 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
282 lc->framerate = cpi->output_framerate / cpi->oxcf.rate_decimator[layer];
283 lc->target_bandwidth = cpi->oxcf.target_bitrate[layer] * 1000;
285 lc->starting_buffer_level_in_ms = oxcf->starting_buffer_level;
286 lc->optimal_buffer_level_in_ms = oxcf->optimal_buffer_level;
287 lc->maximum_buffer_size_in_ms = oxcf->maximum_buffer_size;
289 lc->starting_buffer_level =
290 rescale((int)(oxcf->starting_buffer_level), lc->target_bandwidth, 1000);
292 if (oxcf->optimal_buffer_level == 0) {
293 lc->optimal_buffer_level = lc->target_bandwidth / 8;
295 lc->optimal_buffer_level =
296 rescale((int)(oxcf->optimal_buffer_level), lc->target_bandwidth, 1000);
299 if (oxcf->maximum_buffer_size == 0) {
300 lc->maximum_buffer_size = lc->target_bandwidth / 8;
302 lc->maximum_buffer_size =
303 rescale((int)(oxcf->maximum_buffer_size), lc->target_bandwidth, 1000);
306 /* Work out the average size of a frame within this layer */
308 lc->avg_frame_size_for_layer =
309 (int)((cpi->oxcf.target_bitrate[layer] -
310 cpi->oxcf.target_bitrate[layer - 1]) *
311 1000 / (lc->framerate - prev_layer_framerate));
314 lc->active_worst_quality = cpi->oxcf.worst_allowed_q;
315 lc->active_best_quality = cpi->oxcf.best_allowed_q;
316 lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
318 lc->buffer_level = lc->starting_buffer_level;
319 lc->bits_off_target = lc->starting_buffer_level;
321 lc->total_actual_bits = 0;
325 lc->rate_correction_factor = 1.0;
326 lc->key_frame_rate_correction_factor = 1.0;
327 lc->gf_rate_correction_factor = 1.0;
328 lc->inter_frame_target = 0;
331 // Upon a run-time change in temporal layers, reset the layer context parameters
332 // for any "new" layers. For "existing" layers, let them inherit the parameters
333 // from the previous layer state (at the same layer #). In future we may want
334 // to better map the previous layer state(s) to the "new" ones.
335 static void reset_temporal_layer_change(VP8_COMP *cpi, VP8_CONFIG *oxcf,
336 const int prev_num_layers) {
338 double prev_layer_framerate = 0;
339 const int curr_num_layers = cpi->oxcf.number_of_layers;
340 // If the previous state was 1 layer, get current layer context from cpi.
341 // We need this to set the layer context for the new layers below.
342 if (prev_num_layers == 1) {
343 cpi->current_layer = 0;
344 save_layer_context(cpi);
346 for (i = 0; i < curr_num_layers; ++i) {
347 LAYER_CONTEXT *lc = &cpi->layer_context[i];
348 if (i >= prev_num_layers) {
349 init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
351 // The initial buffer levels are set based on their starting levels.
352 // We could set the buffer levels based on the previous state (normalized
353 // properly by the layer bandwidths) but we would need to keep track of
354 // the previous set of layer bandwidths (i.e., target_bitrate[i])
355 // before the layer change. For now, reset to the starting levels.
357 cpi->oxcf.starting_buffer_level_in_ms * cpi->oxcf.target_bitrate[i];
358 lc->bits_off_target = lc->buffer_level;
359 // TDOD(marpan): Should we set the rate_correction_factor and
360 // active_worst/best_quality to values derived from the previous layer
361 // state (to smooth-out quality dips/rate fluctuation at transition)?
363 // We need to treat the 1 layer case separately: oxcf.target_bitrate[i]
364 // is not set for 1 layer, and the restore_layer_context/save_context()
365 // are not called in the encoding loop, so we need to call it here to
366 // pass the layer context state to |cpi|.
367 if (curr_num_layers == 1) {
368 lc->target_bandwidth = cpi->oxcf.target_bandwidth;
370 cpi->oxcf.starting_buffer_level_in_ms * lc->target_bandwidth / 1000;
371 lc->bits_off_target = lc->buffer_level;
372 restore_layer_context(cpi, 0);
374 prev_layer_framerate = cpi->output_framerate / cpi->oxcf.rate_decimator[i];
378 static void setup_features(VP8_COMP *cpi) {
379 // If segmentation enabled set the update flags
380 if (cpi->mb.e_mbd.segmentation_enabled) {
381 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
382 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
384 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
385 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
388 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
389 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
390 memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
391 memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
392 memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0,
393 sizeof(cpi->mb.e_mbd.ref_lf_deltas));
394 memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0,
395 sizeof(cpi->mb.e_mbd.mode_lf_deltas));
397 set_default_lf_deltas(cpi);
400 static void dealloc_raw_frame_buffers(VP8_COMP *cpi);
402 void vp8_initialize_enc(void) {
403 static volatile int init_done = 0;
407 vp8_init_intra_predictors();
412 static void dealloc_compressor_data(VP8_COMP *cpi) {
413 vpx_free(cpi->tplist);
416 /* Delete last frame MV storage buffers */
420 vpx_free(cpi->lf_ref_frame_sign_bias);
421 cpi->lf_ref_frame_sign_bias = 0;
423 vpx_free(cpi->lf_ref_frame);
424 cpi->lf_ref_frame = 0;
426 /* Delete sementation map */
427 vpx_free(cpi->segmentation_map);
428 cpi->segmentation_map = 0;
430 vpx_free(cpi->active_map);
433 vp8_de_alloc_frame_buffers(&cpi->common);
435 vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
436 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
437 dealloc_raw_frame_buffers(cpi);
442 /* Structure used to monitor GF usage */
443 vpx_free(cpi->gf_active_flags);
444 cpi->gf_active_flags = 0;
446 /* Activity mask based per mb zbin adjustments */
447 vpx_free(cpi->mb_activity_map);
448 cpi->mb_activity_map = 0;
450 vpx_free(cpi->mb.pip);
453 #if CONFIG_MULTITHREAD
454 /* De-allocate mutex */
455 if (cpi->pmutex != NULL) {
456 VP8_COMMON *const pc = &cpi->common;
459 for (i = 0; i < pc->mb_rows; ++i) {
460 pthread_mutex_destroy(&cpi->pmutex[i]);
462 vpx_free(cpi->pmutex);
466 vpx_free(cpi->mt_current_mb_col);
467 cpi->mt_current_mb_col = NULL;
471 static void enable_segmentation(VP8_COMP *cpi) {
472 /* Set the appropriate feature bit */
473 cpi->mb.e_mbd.segmentation_enabled = 1;
474 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
475 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
477 static void disable_segmentation(VP8_COMP *cpi) {
478 /* Clear the appropriate feature bit */
479 cpi->mb.e_mbd.segmentation_enabled = 0;
482 /* Valid values for a segment are 0 to 3
483 * Segmentation map is arrange as [Rows][Columns]
485 static void set_segmentation_map(VP8_COMP *cpi,
486 unsigned char *segmentation_map) {
487 /* Copy in the new segmentation map */
488 memcpy(cpi->segmentation_map, segmentation_map,
489 (cpi->common.mb_rows * cpi->common.mb_cols));
491 /* Signal that the map should be updated. */
492 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
493 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
496 /* The values given for each segment can be either deltas (from the default
497 * value chosen for the frame) or absolute values.
499 * Valid range for abs values is:
500 * (0-127 for MB_LVL_ALT_Q), (0-63 for SEGMENT_ALT_LF)
501 * Valid range for delta values are:
502 * (+/-127 for MB_LVL_ALT_Q), (+/-63 for SEGMENT_ALT_LF)
504 * abs_delta = SEGMENT_DELTADATA (deltas)
505 * abs_delta = SEGMENT_ABSDATA (use the absolute values given).
508 static void set_segment_data(VP8_COMP *cpi, signed char *feature_data,
509 unsigned char abs_delta) {
510 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
511 memcpy(cpi->segment_feature_data, feature_data,
512 sizeof(cpi->segment_feature_data));
515 /* A simple function to cyclically refresh the background at a lower Q */
516 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment) {
517 unsigned char *seg_map = cpi->segmentation_map;
518 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
520 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
521 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
523 cpi->cyclic_refresh_q = Q / 2;
525 if (cpi->oxcf.screen_content_mode) {
526 // Modify quality ramp-up based on Q. Above some Q level, increase the
527 // number of blocks to be refreshed, and reduce it below the thredhold.
528 // Turn-off under certain conditions (i.e., away from key frame, and if
529 // we are at good quality (low Q) and most of the blocks were
531 // in previous frame.
532 int qp_thresh = (cpi->oxcf.screen_content_mode == 2) ? 80 : 100;
533 if (Q >= qp_thresh) {
534 cpi->cyclic_refresh_mode_max_mbs_perframe =
535 (cpi->common.mb_rows * cpi->common.mb_cols) / 10;
536 } else if (cpi->frames_since_key > 250 && Q < 20 &&
537 cpi->mb.skip_true_count > (int)(0.95 * mbs_in_frame)) {
538 cpi->cyclic_refresh_mode_max_mbs_perframe = 0;
540 cpi->cyclic_refresh_mode_max_mbs_perframe =
541 (cpi->common.mb_rows * cpi->common.mb_cols) / 20;
543 block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
546 // Set every macroblock to be eligible for update.
547 // For key frame this will reset seg map to 0.
548 memset(cpi->segmentation_map, 0, mbs_in_frame);
550 if (cpi->common.frame_type != KEY_FRAME && block_count > 0) {
551 /* Cycle through the macro_block rows */
552 /* MB loop to set local segmentation map */
553 i = cpi->cyclic_refresh_mode_index;
554 assert(i < mbs_in_frame);
556 /* If the MB is as a candidate for clean up then mark it for
557 * possible boost/refresh (segment 1) The segment id may get
558 * reset to 0 later if the MB gets coded anything other than
559 * last frame 0,0 as only (last frame 0,0) MBs are eligable for
560 * refresh : that is to say Mbs likely to be background blocks.
562 if (cpi->cyclic_refresh_map[i] == 0) {
565 } else if (cpi->cyclic_refresh_map[i] < 0) {
566 cpi->cyclic_refresh_map[i]++;
570 if (i == mbs_in_frame) i = 0;
572 } while (block_count && i != cpi->cyclic_refresh_mode_index);
574 cpi->cyclic_refresh_mode_index = i;
576 #if CONFIG_TEMPORAL_DENOISING
577 if (cpi->oxcf.noise_sensitivity > 0) {
578 if (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive &&
579 Q < (int)cpi->denoiser.denoise_pars.qp_thresh &&
580 (cpi->frames_since_key >
581 2 * cpi->denoiser.denoise_pars.consec_zerolast)) {
582 // Under aggressive denoising, use segmentation to turn off loop
583 // filter below some qp thresh. The filter is reduced for all
584 // blocks that have been encoded as ZEROMV LAST x frames in a row,
585 // where x is set by cpi->denoiser.denoise_pars.consec_zerolast.
586 // This is to avoid "dot" artifacts that can occur from repeated
587 // loop filtering on noisy input source.
588 cpi->cyclic_refresh_q = Q;
589 // lf_adjustment = -MAX_LOOP_FILTER;
591 for (i = 0; i < mbs_in_frame; ++i) {
592 seg_map[i] = (cpi->consec_zero_last[i] >
593 cpi->denoiser.denoise_pars.consec_zerolast)
602 /* Activate segmentation. */
603 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
604 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
605 enable_segmentation(cpi);
607 /* Set up the quant segment data */
608 feature_data[MB_LVL_ALT_Q][0] = 0;
609 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
610 feature_data[MB_LVL_ALT_Q][2] = 0;
611 feature_data[MB_LVL_ALT_Q][3] = 0;
613 /* Set up the loop segment data */
614 feature_data[MB_LVL_ALT_LF][0] = 0;
615 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
616 feature_data[MB_LVL_ALT_LF][2] = 0;
617 feature_data[MB_LVL_ALT_LF][3] = 0;
619 /* Initialise the feature data structure */
620 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
623 static void set_default_lf_deltas(VP8_COMP *cpi) {
624 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
625 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
627 memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
628 memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
630 /* Test of ref frame deltas */
631 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
632 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
633 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
634 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
636 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; /* BPRED */
638 if (cpi->oxcf.Mode == MODE_REALTIME) {
639 cpi->mb.e_mbd.mode_lf_deltas[1] = -12; /* Zero */
641 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; /* Zero */
644 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; /* New mv */
645 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; /* Split mv */
648 /* Convenience macros for mapping speed and mode into a continuous
651 #define GOOD(x) (x + 1)
652 #define RT(x) (x + 7)
654 static int speed_map(int speed, const int *map) {
659 } while (speed >= *map++);
663 static const int thresh_mult_map_znn[] = {
664 /* map common to zero, nearest, and near */
665 0, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(2), 2000, INT_MAX
668 static const int thresh_mult_map_vhpred[] = { 1000, GOOD(2), 1500, GOOD(3),
669 2000, RT(0), 1000, RT(1),
670 2000, RT(7), INT_MAX, INT_MAX };
672 static const int thresh_mult_map_bpred[] = { 2000, GOOD(0), 2500, GOOD(2),
673 5000, GOOD(3), 7500, RT(0),
674 2500, RT(1), 5000, RT(6),
677 static const int thresh_mult_map_tm[] = { 1000, GOOD(2), 1500, GOOD(3),
678 2000, RT(0), 0, RT(1),
679 1000, RT(2), 2000, RT(7),
682 static const int thresh_mult_map_new1[] = { 1000, GOOD(2), 2000,
683 RT(0), 2000, INT_MAX };
685 static const int thresh_mult_map_new2[] = { 1000, GOOD(2), 2000, GOOD(3),
686 2500, GOOD(5), 4000, RT(0),
687 2000, RT(2), 2500, RT(5),
690 static const int thresh_mult_map_split1[] = {
691 2500, GOOD(0), 1700, GOOD(2), 10000, GOOD(3), 25000, GOOD(4), INT_MAX,
692 RT(0), 5000, RT(1), 10000, RT(2), 25000, RT(3), INT_MAX, INT_MAX
695 static const int thresh_mult_map_split2[] = {
696 5000, GOOD(0), 4500, GOOD(2), 20000, GOOD(3), 50000, GOOD(4), INT_MAX,
697 RT(0), 10000, RT(1), 20000, RT(2), 50000, RT(3), INT_MAX, INT_MAX
700 static const int mode_check_freq_map_zn2[] = {
701 /* {zero,nearest}{2,3} */
702 0, RT(10), 1 << 1, RT(11), 1 << 2, RT(12), 1 << 3, INT_MAX
705 static const int mode_check_freq_map_vhbpred[] = {
706 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(5), 4, INT_MAX
709 static const int mode_check_freq_map_near2[] = {
710 0, GOOD(5), 2, RT(0), 0, RT(3), 2,
711 RT(10), 1 << 2, RT(11), 1 << 3, RT(12), 1 << 4, INT_MAX
714 static const int mode_check_freq_map_new1[] = {
715 0, RT(10), 1 << 1, RT(11), 1 << 2, RT(12), 1 << 3, INT_MAX
718 static const int mode_check_freq_map_new2[] = { 0, GOOD(5), 4, RT(0),
720 1 << 3, RT(11), 1 << 4, RT(12),
723 static const int mode_check_freq_map_split1[] = {
724 0, GOOD(2), 2, GOOD(3), 7, RT(1), 2, RT(2), 7, INT_MAX
727 static const int mode_check_freq_map_split2[] = {
728 0, GOOD(1), 2, GOOD(2), 4, GOOD(3), 15, RT(1), 4, RT(2), 15, INT_MAX
731 void vp8_set_speed_features(VP8_COMP *cpi) {
732 SPEED_FEATURES *sf = &cpi->sf;
733 int Mode = cpi->compressor_speed;
734 int Speed = cpi->Speed;
737 VP8_COMMON *cm = &cpi->common;
738 int last_improved_quant = sf->improved_quant;
741 /* Initialise default mode frequency sampling variables */
742 for (i = 0; i < MAX_MODES; ++i) {
743 cpi->mode_check_freq[i] = 0;
746 cpi->mb.mbs_tested_so_far = 0;
747 cpi->mb.mbs_zero_last_dot_suppress = 0;
749 /* best quality defaults */
751 sf->search_method = NSTEP;
752 sf->improved_quant = 1;
753 sf->improved_dct = 1;
756 sf->quarter_pixel_search = 1;
757 sf->half_pixel_search = 1;
758 sf->iterative_sub_pixel = 1;
759 sf->optimize_coefficients = 1;
760 sf->use_fastquant_for_pick = 0;
761 sf->no_skip_block4x4_search = 1;
764 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
765 sf->improved_mv_pred = 1;
767 /* default thresholds to 0 */
768 for (i = 0; i < MAX_MODES; ++i) sf->thresh_mult[i] = 0;
770 /* Count enabled references */
772 if (cpi->ref_frame_flags & VP8_LAST_FRAME) ref_frames++;
773 if (cpi->ref_frame_flags & VP8_GOLD_FRAME) ref_frames++;
774 if (cpi->ref_frame_flags & VP8_ALTR_FRAME) ref_frames++;
776 /* Convert speed to continuous range, with clamping */
779 } else if (Mode == 2) {
782 if (Speed > 5) Speed = 5;
786 sf->thresh_mult[THR_ZERO1] = sf->thresh_mult[THR_NEAREST1] =
787 sf->thresh_mult[THR_NEAR1] = sf->thresh_mult[THR_DC] = 0; /* always */
789 sf->thresh_mult[THR_ZERO2] = sf->thresh_mult[THR_ZERO3] =
790 sf->thresh_mult[THR_NEAREST2] = sf->thresh_mult[THR_NEAREST3] =
791 sf->thresh_mult[THR_NEAR2] = sf->thresh_mult[THR_NEAR3] =
792 speed_map(Speed, thresh_mult_map_znn);
794 sf->thresh_mult[THR_V_PRED] = sf->thresh_mult[THR_H_PRED] =
795 speed_map(Speed, thresh_mult_map_vhpred);
796 sf->thresh_mult[THR_B_PRED] = speed_map(Speed, thresh_mult_map_bpred);
797 sf->thresh_mult[THR_TM] = speed_map(Speed, thresh_mult_map_tm);
798 sf->thresh_mult[THR_NEW1] = speed_map(Speed, thresh_mult_map_new1);
799 sf->thresh_mult[THR_NEW2] = sf->thresh_mult[THR_NEW3] =
800 speed_map(Speed, thresh_mult_map_new2);
801 sf->thresh_mult[THR_SPLIT1] = speed_map(Speed, thresh_mult_map_split1);
802 sf->thresh_mult[THR_SPLIT2] = sf->thresh_mult[THR_SPLIT3] =
803 speed_map(Speed, thresh_mult_map_split2);
805 // Special case for temporal layers.
806 // Reduce the thresholds for zero/nearest/near for GOLDEN, if GOLDEN is
807 // used as second reference. We don't modify thresholds for ALTREF case
808 // since ALTREF is usually used as long-term reference in temporal layers.
809 if ((cpi->Speed <= 6) && (cpi->oxcf.number_of_layers > 1) &&
810 (cpi->ref_frame_flags & VP8_LAST_FRAME) &&
811 (cpi->ref_frame_flags & VP8_GOLD_FRAME)) {
812 if (cpi->closest_reference_frame == GOLDEN_FRAME) {
813 sf->thresh_mult[THR_ZERO2] = sf->thresh_mult[THR_ZERO2] >> 3;
814 sf->thresh_mult[THR_NEAREST2] = sf->thresh_mult[THR_NEAREST2] >> 3;
815 sf->thresh_mult[THR_NEAR2] = sf->thresh_mult[THR_NEAR2] >> 3;
817 sf->thresh_mult[THR_ZERO2] = sf->thresh_mult[THR_ZERO2] >> 1;
818 sf->thresh_mult[THR_NEAREST2] = sf->thresh_mult[THR_NEAREST2] >> 1;
819 sf->thresh_mult[THR_NEAR2] = sf->thresh_mult[THR_NEAR2] >> 1;
823 cpi->mode_check_freq[THR_ZERO1] = cpi->mode_check_freq[THR_NEAREST1] =
824 cpi->mode_check_freq[THR_NEAR1] = cpi->mode_check_freq[THR_TM] =
825 cpi->mode_check_freq[THR_DC] = 0; /* always */
827 cpi->mode_check_freq[THR_ZERO2] = cpi->mode_check_freq[THR_ZERO3] =
828 cpi->mode_check_freq[THR_NEAREST2] = cpi->mode_check_freq[THR_NEAREST3] =
829 speed_map(Speed, mode_check_freq_map_zn2);
831 cpi->mode_check_freq[THR_NEAR2] = cpi->mode_check_freq[THR_NEAR3] =
832 speed_map(Speed, mode_check_freq_map_near2);
834 cpi->mode_check_freq[THR_V_PRED] = cpi->mode_check_freq[THR_H_PRED] =
835 cpi->mode_check_freq[THR_B_PRED] =
836 speed_map(Speed, mode_check_freq_map_vhbpred);
838 // For real-time mode at speed 10 keep the mode_check_freq threshold
839 // for NEW1 similar to that of speed 9.
841 if (cpi->Speed == 10 && Mode == 2) Speed2 = RT(9);
842 cpi->mode_check_freq[THR_NEW1] = speed_map(Speed2, mode_check_freq_map_new1);
844 cpi->mode_check_freq[THR_NEW2] = cpi->mode_check_freq[THR_NEW3] =
845 speed_map(Speed, mode_check_freq_map_new2);
847 cpi->mode_check_freq[THR_SPLIT1] =
848 speed_map(Speed, mode_check_freq_map_split1);
849 cpi->mode_check_freq[THR_SPLIT2] = cpi->mode_check_freq[THR_SPLIT3] =
850 speed_map(Speed, mode_check_freq_map_split2);
853 #if !CONFIG_REALTIME_ONLY
854 case 0: /* best quality mode */
856 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
861 /* Disable coefficient optimization above speed 0 */
862 sf->optimize_coefficients = 0;
863 sf->use_fastquant_for_pick = 1;
864 sf->no_skip_block4x4_search = 0;
870 sf->improved_quant = 0;
871 sf->improved_dct = 0;
873 /* Only do recode loop on key frames, golden frames and
881 sf->recode_loop = 0; /* recode loop off */
882 sf->RD = 0; /* Turn rd off */
886 sf->auto_filter = 0; /* Faster selection of loop filter */
892 sf->optimize_coefficients = 0;
895 sf->iterative_sub_pixel = 1;
896 sf->search_method = NSTEP;
899 sf->improved_quant = 0;
900 sf->improved_dct = 0;
902 sf->use_fastquant_for_pick = 1;
903 sf->no_skip_block4x4_search = 0;
907 if (Speed > 2) sf->auto_filter = 0; /* Faster selection of loop filter */
915 sf->auto_filter = 0; /* Faster selection of loop filter */
916 sf->search_method = HEX;
917 sf->iterative_sub_pixel = 0;
921 unsigned int sum = 0;
922 unsigned int total_mbs = cm->MBs;
924 unsigned int total_skip;
928 if (cpi->oxcf.encode_breakout > 2000) min = cpi->oxcf.encode_breakout;
932 for (i = 0; i < min; ++i) {
933 sum += cpi->mb.error_bins[i];
939 /* i starts from 2 to make sure thresh started from 2048 */
940 for (; i < 1024; ++i) {
941 sum += cpi->mb.error_bins[i];
944 (unsigned int)(cpi->Speed - 6) * (total_mbs - total_skip)) {
952 if (thresh < 2000) thresh = 2000;
954 if (ref_frames > 1) {
955 sf->thresh_mult[THR_NEW1] = thresh;
956 sf->thresh_mult[THR_NEAREST1] = thresh >> 1;
957 sf->thresh_mult[THR_NEAR1] = thresh >> 1;
960 if (ref_frames > 2) {
961 sf->thresh_mult[THR_NEW2] = thresh << 1;
962 sf->thresh_mult[THR_NEAREST2] = thresh;
963 sf->thresh_mult[THR_NEAR2] = thresh;
966 if (ref_frames > 3) {
967 sf->thresh_mult[THR_NEW3] = thresh << 1;
968 sf->thresh_mult[THR_NEAREST3] = thresh;
969 sf->thresh_mult[THR_NEAR3] = thresh;
972 sf->improved_mv_pred = 0;
975 if (Speed > 8) sf->quarter_pixel_search = 0;
977 if (cm->version == 0) {
978 cm->filter_type = NORMAL_LOOPFILTER;
980 if (Speed >= 14) cm->filter_type = SIMPLE_LOOPFILTER;
982 cm->filter_type = SIMPLE_LOOPFILTER;
985 /* This has a big hit on quality. Last resort */
986 if (Speed >= 15) sf->half_pixel_search = 0;
988 memset(cpi->mb.error_bins, 0, sizeof(cpi->mb.error_bins));
992 /* Slow quant, dct and trellis not worthwhile for first pass
993 * so make sure they are always turned off.
995 if (cpi->pass == 1) {
996 sf->improved_quant = 0;
997 sf->optimize_coefficients = 0;
998 sf->improved_dct = 0;
1001 if (cpi->sf.search_method == NSTEP) {
1002 vp8_init3smotion_compensation(&cpi->mb,
1003 cm->yv12_fb[cm->lst_fb_idx].y_stride);
1004 } else if (cpi->sf.search_method == DIAMOND) {
1005 vp8_init_dsmotion_compensation(&cpi->mb,
1006 cm->yv12_fb[cm->lst_fb_idx].y_stride);
1009 if (cpi->sf.improved_dct) {
1010 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
1011 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
1013 /* No fast FDCT defined for any platform at this time. */
1014 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
1015 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
1018 cpi->mb.short_walsh4x4 = vp8_short_walsh4x4;
1020 if (cpi->sf.improved_quant) {
1021 cpi->mb.quantize_b = vp8_regular_quantize_b;
1023 cpi->mb.quantize_b = vp8_fast_quantize_b;
1025 if (cpi->sf.improved_quant != last_improved_quant) vp8cx_init_quantizer(cpi);
1027 if (cpi->sf.iterative_sub_pixel == 1) {
1028 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1029 } else if (cpi->sf.quarter_pixel_search) {
1030 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1031 } else if (cpi->sf.half_pixel_search) {
1032 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1034 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1037 if (cpi->sf.optimize_coefficients == 1 && cpi->pass != 1) {
1038 cpi->mb.optimize = 1;
1040 cpi->mb.optimize = 0;
1043 if (cpi->common.full_pixel) {
1044 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1048 frames_at_speed[cpi->Speed]++;
1054 static void alloc_raw_frame_buffers(VP8_COMP *cpi) {
1055 #if VP8_TEMPORAL_ALT_REF
1056 int width = (cpi->oxcf.Width + 15) & ~15;
1057 int height = (cpi->oxcf.Height + 15) & ~15;
1060 cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1061 cpi->oxcf.lag_in_frames);
1062 if (!cpi->lookahead) {
1063 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1064 "Failed to allocate lag buffers");
1067 #if VP8_TEMPORAL_ALT_REF
1069 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer, width, height,
1070 VP8BORDERINPIXELS)) {
1071 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1072 "Failed to allocate altref buffer");
1078 static void dealloc_raw_frame_buffers(VP8_COMP *cpi) {
1079 #if VP8_TEMPORAL_ALT_REF
1080 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
1082 vp8_lookahead_destroy(cpi->lookahead);
1085 static int vp8_alloc_partition_data(VP8_COMP *cpi) {
1086 vpx_free(cpi->mb.pip);
1089 vpx_calloc((cpi->common.mb_cols + 1) * (cpi->common.mb_rows + 1),
1090 sizeof(PARTITION_INFO));
1091 if (!cpi->mb.pip) return 1;
1093 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1098 void vp8_alloc_compressor_data(VP8_COMP *cpi) {
1099 VP8_COMMON *cm = &cpi->common;
1101 int width = cm->Width;
1102 int height = cm->Height;
1103 #if CONFIG_MULTITHREAD
1104 int prev_mb_rows = cm->mb_rows;
1107 if (vp8_alloc_frame_buffers(cm, width, height)) {
1108 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1109 "Failed to allocate frame buffers");
1112 if (vp8_alloc_partition_data(cpi)) {
1113 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1114 "Failed to allocate partition data");
1117 if ((width & 0xf) != 0) width += 16 - (width & 0xf);
1119 if ((height & 0xf) != 0) height += 16 - (height & 0xf);
1121 if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame, width, height,
1122 VP8BORDERINPIXELS)) {
1123 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1124 "Failed to allocate last frame buffer");
1127 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height,
1128 VP8BORDERINPIXELS)) {
1129 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1130 "Failed to allocate scaled source buffer");
1136 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1137 unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */
1139 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1141 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1144 /* Data used for real time vc mode to see if gf needs refreshing */
1145 cpi->zeromv_count = 0;
1147 /* Structures used to monitor GF usage */
1148 vpx_free(cpi->gf_active_flags);
1150 cpi->gf_active_flags,
1151 vpx_calloc(sizeof(*cpi->gf_active_flags), cm->mb_rows * cm->mb_cols));
1152 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1154 vpx_free(cpi->mb_activity_map);
1156 cpi->mb_activity_map,
1157 vpx_calloc(sizeof(*cpi->mb_activity_map), cm->mb_rows * cm->mb_cols));
1159 /* allocate memory for storing last frame's MVs for MV prediction. */
1160 vpx_free(cpi->lfmv);
1161 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cm->mb_rows + 2) * (cm->mb_cols + 2),
1162 sizeof(*cpi->lfmv)));
1163 vpx_free(cpi->lf_ref_frame_sign_bias);
1164 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias,
1165 vpx_calloc((cm->mb_rows + 2) * (cm->mb_cols + 2),
1166 sizeof(*cpi->lf_ref_frame_sign_bias)));
1167 vpx_free(cpi->lf_ref_frame);
1168 CHECK_MEM_ERROR(cpi->lf_ref_frame,
1169 vpx_calloc((cm->mb_rows + 2) * (cm->mb_cols + 2),
1170 sizeof(*cpi->lf_ref_frame)));
1172 /* Create the encoder segmentation map and set all entries to 0 */
1173 vpx_free(cpi->segmentation_map);
1175 cpi->segmentation_map,
1176 vpx_calloc(cm->mb_rows * cm->mb_cols, sizeof(*cpi->segmentation_map)));
1177 cpi->cyclic_refresh_mode_index = 0;
1178 vpx_free(cpi->active_map);
1179 CHECK_MEM_ERROR(cpi->active_map, vpx_calloc(cm->mb_rows * cm->mb_cols,
1180 sizeof(*cpi->active_map)));
1181 memset(cpi->active_map, 1, (cm->mb_rows * cm->mb_cols));
1183 #if CONFIG_MULTITHREAD
1185 cpi->mt_sync_range = 1;
1186 } else if (width <= 1280) {
1187 cpi->mt_sync_range = 4;
1188 } else if (width <= 2560) {
1189 cpi->mt_sync_range = 8;
1191 cpi->mt_sync_range = 16;
1194 if (cpi->oxcf.multi_threaded > 1) {
1197 /* De-allocate and re-allocate mutex */
1198 if (cpi->pmutex != NULL) {
1199 for (i = 0; i < prev_mb_rows; ++i) {
1200 pthread_mutex_destroy(&cpi->pmutex[i]);
1202 vpx_free(cpi->pmutex);
1206 CHECK_MEM_ERROR(cpi->pmutex,
1207 vpx_malloc(sizeof(*cpi->pmutex) * cm->mb_rows));
1209 for (i = 0; i < cm->mb_rows; ++i) {
1210 pthread_mutex_init(&cpi->pmutex[i], NULL);
1214 vpx_free(cpi->mt_current_mb_col);
1215 CHECK_MEM_ERROR(cpi->mt_current_mb_col,
1216 vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows));
1221 vpx_free(cpi->tplist);
1222 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cm->mb_rows));
1224 #if CONFIG_TEMPORAL_DENOISING
1225 if (cpi->oxcf.noise_sensitivity > 0) {
1226 vp8_denoiser_free(&cpi->denoiser);
1227 if (vp8_denoiser_allocate(&cpi->denoiser, width, height, cm->mb_rows,
1228 cm->mb_cols, cpi->oxcf.noise_sensitivity)) {
1229 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1230 "Failed to allocate denoiser");
1237 static const int q_trans[] = {
1238 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 13, 15, 17, 18, 19,
1239 20, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 33, 35, 37, 39, 41,
1240 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 64, 67, 70, 73, 76, 79,
1241 82, 85, 88, 91, 94, 97, 100, 103, 106, 109, 112, 115, 118, 121, 124, 127,
1244 int vp8_reverse_trans(int x) {
1247 for (i = 0; i < 64; ++i) {
1248 if (q_trans[i] >= x) return i;
1253 void vp8_new_framerate(VP8_COMP *cpi, double framerate) {
1254 if (framerate < .1) framerate = 30;
1256 cpi->framerate = framerate;
1257 cpi->output_framerate = framerate;
1258 cpi->per_frame_bandwidth =
1259 (int)(cpi->oxcf.target_bandwidth / cpi->output_framerate);
1260 cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
1261 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
1262 cpi->oxcf.two_pass_vbrmin_section / 100);
1264 /* Set Maximum gf/arf interval */
1265 cpi->max_gf_interval = ((int)(cpi->output_framerate / 2.0) + 2);
1267 if (cpi->max_gf_interval < 12) cpi->max_gf_interval = 12;
1269 /* Extended interval for genuinely static scenes */
1270 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1272 /* Special conditions when altr ref frame enabled in lagged compress mode */
1273 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) {
1274 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1) {
1275 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1278 if (cpi->twopass.static_scene_max_gf_interval >
1279 cpi->oxcf.lag_in_frames - 1) {
1280 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1284 if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval) {
1285 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1289 static void init_config(VP8_COMP *cpi, VP8_CONFIG *oxcf) {
1290 VP8_COMMON *cm = &cpi->common;
1295 cpi->auto_adjust_gold_quantizer = 1;
1297 cm->version = oxcf->Version;
1298 vp8_setup_version(cm);
1300 /* Frame rate is not available on the first frame, as it's derived from
1301 * the observed timestamps. The actual value used here doesn't matter
1302 * too much, as it will adapt quickly.
1304 if (oxcf->timebase.num > 0) {
1306 (double)(oxcf->timebase.den) / (double)(oxcf->timebase.num);
1308 cpi->framerate = 30;
1311 /* If the reciprocal of the timebase seems like a reasonable framerate,
1312 * then use that as a guess, otherwise use 30.
1314 if (cpi->framerate > 180) cpi->framerate = 30;
1316 cpi->ref_framerate = cpi->framerate;
1318 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
1320 cm->refresh_golden_frame = 0;
1321 cm->refresh_last_frame = 1;
1322 cm->refresh_entropy_probs = 1;
1324 /* change includes all joint functionality */
1325 vp8_change_config(cpi, oxcf);
1327 /* Initialize active best and worst q and average q values. */
1328 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1329 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1330 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1332 /* Initialise the starting buffer levels */
1333 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1334 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1336 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1337 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1338 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1339 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1341 cpi->total_actual_bits = 0;
1342 cpi->total_target_vs_actual = 0;
1344 /* Temporal scalabilty */
1345 if (cpi->oxcf.number_of_layers > 1) {
1347 double prev_layer_framerate = 0;
1349 for (i = 0; i < cpi->oxcf.number_of_layers; ++i) {
1350 init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
1351 prev_layer_framerate =
1352 cpi->output_framerate / cpi->oxcf.rate_decimator[i];
1356 #if VP8_TEMPORAL_ALT_REF
1360 cpi->fixed_divide[0] = 0;
1362 for (i = 1; i < 512; ++i) cpi->fixed_divide[i] = 0x80000 / i;
1367 static void update_layer_contexts(VP8_COMP *cpi) {
1368 VP8_CONFIG *oxcf = &cpi->oxcf;
1370 /* Update snapshots of the layer contexts to reflect new parameters */
1371 if (oxcf->number_of_layers > 1) {
1373 double prev_layer_framerate = 0;
1375 assert(oxcf->number_of_layers <= VPX_TS_MAX_LAYERS);
1376 for (i = 0; i < oxcf->number_of_layers && i < VPX_TS_MAX_LAYERS; ++i) {
1377 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1379 lc->framerate = cpi->ref_framerate / oxcf->rate_decimator[i];
1380 lc->target_bandwidth = oxcf->target_bitrate[i] * 1000;
1382 lc->starting_buffer_level = rescale(
1383 (int)oxcf->starting_buffer_level_in_ms, lc->target_bandwidth, 1000);
1385 if (oxcf->optimal_buffer_level == 0) {
1386 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1388 lc->optimal_buffer_level = rescale(
1389 (int)oxcf->optimal_buffer_level_in_ms, lc->target_bandwidth, 1000);
1392 if (oxcf->maximum_buffer_size == 0) {
1393 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1395 lc->maximum_buffer_size = rescale((int)oxcf->maximum_buffer_size_in_ms,
1396 lc->target_bandwidth, 1000);
1399 /* Work out the average size of a frame within this layer */
1401 lc->avg_frame_size_for_layer =
1402 (int)((oxcf->target_bitrate[i] - oxcf->target_bitrate[i - 1]) *
1403 1000 / (lc->framerate - prev_layer_framerate));
1406 prev_layer_framerate = lc->framerate;
1411 void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf) {
1412 VP8_COMMON *cm = &cpi->common;
1414 unsigned int prev_number_of_layers;
1420 if (cm->version != oxcf->Version) {
1421 cm->version = oxcf->Version;
1422 vp8_setup_version(cm);
1425 last_w = cpi->oxcf.Width;
1426 last_h = cpi->oxcf.Height;
1427 prev_number_of_layers = cpi->oxcf.number_of_layers;
1431 switch (cpi->oxcf.Mode) {
1434 cpi->compressor_speed = 2;
1436 if (cpi->oxcf.cpu_used < -16) {
1437 cpi->oxcf.cpu_used = -16;
1440 if (cpi->oxcf.cpu_used > 16) cpi->oxcf.cpu_used = 16;
1444 case MODE_GOODQUALITY:
1446 cpi->compressor_speed = 1;
1448 if (cpi->oxcf.cpu_used < -5) {
1449 cpi->oxcf.cpu_used = -5;
1452 if (cpi->oxcf.cpu_used > 5) cpi->oxcf.cpu_used = 5;
1456 case MODE_BESTQUALITY:
1458 cpi->compressor_speed = 0;
1461 case MODE_FIRSTPASS:
1463 cpi->compressor_speed = 1;
1465 case MODE_SECONDPASS:
1467 cpi->compressor_speed = 1;
1469 if (cpi->oxcf.cpu_used < -5) {
1470 cpi->oxcf.cpu_used = -5;
1473 if (cpi->oxcf.cpu_used > 5) cpi->oxcf.cpu_used = 5;
1476 case MODE_SECONDPASS_BEST:
1478 cpi->compressor_speed = 0;
1482 if (cpi->pass == 0) cpi->auto_worst_q = 1;
1484 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1485 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1486 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1488 if (oxcf->fixed_q >= 0) {
1489 if (oxcf->worst_allowed_q < 0) {
1490 cpi->oxcf.fixed_q = q_trans[0];
1492 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1495 if (oxcf->alt_q < 0) {
1496 cpi->oxcf.alt_q = q_trans[0];
1498 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1501 if (oxcf->key_q < 0) {
1502 cpi->oxcf.key_q = q_trans[0];
1504 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1507 if (oxcf->gold_q < 0) {
1508 cpi->oxcf.gold_q = q_trans[0];
1510 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1514 cpi->baseline_gf_interval =
1515 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1517 // GF behavior for 1 pass CBR, used when error_resilience is off.
1518 if (!cpi->oxcf.error_resilient_mode &&
1519 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
1520 cpi->oxcf.Mode == MODE_REALTIME)
1521 cpi->baseline_gf_interval = cpi->gf_interval_onepass_cbr;
1523 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1524 cpi->oxcf.token_partitions = 3;
1527 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3) {
1528 cm->multi_token_partition = (TOKEN_PARTITION)cpi->oxcf.token_partitions;
1531 setup_features(cpi);
1536 for (i = 0; i < MAX_MB_SEGMENTS; ++i) {
1537 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1541 /* At the moment the first order values may not be > MAXQ */
1542 if (cpi->oxcf.fixed_q > MAXQ) cpi->oxcf.fixed_q = MAXQ;
1544 /* local file playback mode == really big buffer */
1545 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
1546 cpi->oxcf.starting_buffer_level = 60000;
1547 cpi->oxcf.optimal_buffer_level = 60000;
1548 cpi->oxcf.maximum_buffer_size = 240000;
1549 cpi->oxcf.starting_buffer_level_in_ms = 60000;
1550 cpi->oxcf.optimal_buffer_level_in_ms = 60000;
1551 cpi->oxcf.maximum_buffer_size_in_ms = 240000;
1554 /* Convert target bandwidth from Kbit/s to Bit/s */
1555 cpi->oxcf.target_bandwidth *= 1000;
1557 cpi->oxcf.starting_buffer_level = rescale(
1558 (int)cpi->oxcf.starting_buffer_level, cpi->oxcf.target_bandwidth, 1000);
1560 /* Set or reset optimal and maximum buffer levels. */
1561 if (cpi->oxcf.optimal_buffer_level == 0) {
1562 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1564 cpi->oxcf.optimal_buffer_level = rescale(
1565 (int)cpi->oxcf.optimal_buffer_level, cpi->oxcf.target_bandwidth, 1000);
1568 if (cpi->oxcf.maximum_buffer_size == 0) {
1569 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1571 cpi->oxcf.maximum_buffer_size = rescale((int)cpi->oxcf.maximum_buffer_size,
1572 cpi->oxcf.target_bandwidth, 1000);
1574 // Under a configuration change, where maximum_buffer_size may change,
1575 // keep buffer level clipped to the maximum allowed buffer size.
1576 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
1577 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
1578 cpi->buffer_level = cpi->bits_off_target;
1581 /* Set up frame rate and related parameters rate control values. */
1582 vp8_new_framerate(cpi, cpi->framerate);
1584 /* Set absolute upper and lower quality limits */
1585 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1586 cpi->best_quality = cpi->oxcf.best_allowed_q;
1588 /* active values should only be modified if out of new range */
1589 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q) {
1590 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1593 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q) {
1594 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1596 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q) {
1597 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1600 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q) {
1601 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1604 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
1606 cpi->cq_target_quality = cpi->oxcf.cq_level;
1608 /* Only allow dropped frames in buffered mode */
1609 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1611 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1613 // Check if the number of temporal layers has changed, and if so reset the
1614 // pattern counter and set/initialize the temporal layer context for the
1615 // new layer configuration.
1616 if (cpi->oxcf.number_of_layers != prev_number_of_layers) {
1617 // If the number of temporal layers are changed we must start at the
1618 // base of the pattern cycle, so set the layer id to 0 and reset
1619 // the temporal pattern counter.
1620 if (cpi->temporal_layer_id > 0) {
1621 cpi->temporal_layer_id = 0;
1623 cpi->temporal_pattern_counter = 0;
1624 reset_temporal_layer_change(cpi, oxcf, prev_number_of_layers);
1627 if (!cpi->initial_width) {
1628 cpi->initial_width = cpi->oxcf.Width;
1629 cpi->initial_height = cpi->oxcf.Height;
1632 cm->Width = cpi->oxcf.Width;
1633 cm->Height = cpi->oxcf.Height;
1634 assert(cm->Width <= cpi->initial_width);
1635 assert(cm->Height <= cpi->initial_height);
1637 /* TODO(jkoleszar): if an internal spatial resampling is active,
1638 * and we downsize the input image, maybe we should clear the
1639 * internal scale immediately rather than waiting for it to
1643 /* VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) */
1644 if (cpi->oxcf.Sharpness > 7) cpi->oxcf.Sharpness = 7;
1646 cm->sharpness_level = cpi->oxcf.Sharpness;
1648 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL) {
1651 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1652 Scale2Ratio(cm->vert_scale, &vr, &vs);
1654 /* always go to the next whole number */
1655 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1656 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1659 if (last_w != cpi->oxcf.Width || last_h != cpi->oxcf.Height) {
1660 cpi->force_next_frame_intra = 1;
1663 if (((cm->Width + 15) & ~15) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
1664 ((cm->Height + 15) & ~15) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
1665 cm->yv12_fb[cm->lst_fb_idx].y_width == 0) {
1666 dealloc_raw_frame_buffers(cpi);
1667 alloc_raw_frame_buffers(cpi);
1668 vp8_alloc_compressor_data(cpi);
1671 if (cpi->oxcf.fixed_q >= 0) {
1672 cpi->last_q[0] = cpi->oxcf.fixed_q;
1673 cpi->last_q[1] = cpi->oxcf.fixed_q;
1676 cpi->Speed = cpi->oxcf.cpu_used;
1678 /* force to allowlag to 0 if lag_in_frames is 0; */
1679 if (cpi->oxcf.lag_in_frames == 0) {
1680 cpi->oxcf.allow_lag = 0;
1682 /* Limit on lag buffers as these are not currently dynamically allocated */
1683 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) {
1684 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1688 cpi->alt_ref_source = NULL;
1689 cpi->is_src_frame_alt_ref = 0;
1691 #if CONFIG_TEMPORAL_DENOISING
1692 if (cpi->oxcf.noise_sensitivity) {
1693 if (!cpi->denoiser.yv12_mc_running_avg.buffer_alloc) {
1694 int width = (cpi->oxcf.Width + 15) & ~15;
1695 int height = (cpi->oxcf.Height + 15) & ~15;
1696 if (vp8_denoiser_allocate(&cpi->denoiser, width, height, cm->mb_rows,
1697 cm->mb_cols, cpi->oxcf.noise_sensitivity)) {
1698 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1699 "Failed to allocate denoiser");
1706 /* Experimental RD Code */
1707 cpi->frame_distortion = 0;
1708 cpi->last_frame_distortion = 0;
1713 #define M_LOG2_E 0.693147180559945309417
1715 #define log2f(x) (log(x) / (float)M_LOG2_E)
1717 static void cal_mvsadcosts(int *mvsadcost[2]) {
1720 mvsadcost[0][0] = 300;
1721 mvsadcost[1][0] = 300;
1724 double z = 256 * (2 * (log2f(8 * i) + .6));
1725 mvsadcost[0][i] = (int)z;
1726 mvsadcost[1][i] = (int)z;
1727 mvsadcost[0][-i] = (int)z;
1728 mvsadcost[1][-i] = (int)z;
1729 } while (++i <= mvfp_max);
1732 struct VP8_COMP *vp8_create_compressor(VP8_CONFIG *oxcf) {
1738 cpi = vpx_memalign(32, sizeof(VP8_COMP));
1739 /* Check that the CPI instance is valid */
1744 memset(cpi, 0, sizeof(VP8_COMP));
1746 if (setjmp(cm->error.jmp)) {
1747 cpi->common.error.setjmp = 0;
1748 vp8_remove_compressor(&cpi);
1752 cpi->common.error.setjmp = 1;
1754 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site),
1755 (MAX_MVSEARCH_STEPS * 8) + 1));
1757 vp8_create_common(&cpi->common);
1759 init_config(cpi, oxcf);
1761 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob,
1762 sizeof(vp8cx_base_skip_false_prob));
1763 cpi->common.current_video_frame = 0;
1764 cpi->temporal_pattern_counter = 0;
1765 cpi->temporal_layer_id = -1;
1766 cpi->kf_overspend_bits = 0;
1767 cpi->kf_bitrate_adjustment = 0;
1768 cpi->frames_till_gf_update_due = 0;
1769 cpi->gf_overspend_bits = 0;
1770 cpi->non_gf_bitrate_adjustment = 0;
1771 cpi->prob_last_coded = 128;
1772 cpi->prob_gf_coded = 128;
1773 cpi->prob_intra_coded = 63;
1775 /* Prime the recent reference frame usage counters.
1776 * Hereafter they will be maintained as a sort of moving average
1778 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1779 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1780 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1781 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1783 /* Set reference frame sign bias for ALTREF frame to 1 (for now) */
1784 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1786 cpi->twopass.gf_decay_rate = 0;
1787 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1789 cpi->gold_is_last = 0;
1790 cpi->alt_is_last = 0;
1791 cpi->gold_is_alt = 0;
1793 cpi->active_map_enabled = 0;
1796 /* Experimental code for lagged and one pass */
1797 /* Initialise one_pass GF frames stats */
1798 /* Update stats used for GF selection */
1801 cpi->one_pass_frame_index = 0;
1803 for (i = 0; i < MAX_LAG_BUFFERS; ++i)
1805 cpi->one_pass_frame_stats[i].frames_so_far = 0;
1806 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1807 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1808 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1809 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1810 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1811 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1812 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1813 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1818 cpi->mse_source_denoised = 0;
1820 /* Should we use the cyclic refresh method.
1821 * Currently there is no external control for this.
1822 * Enable it for error_resilient_mode, or for 1 pass CBR mode.
1824 cpi->cyclic_refresh_mode_enabled =
1825 (cpi->oxcf.error_resilient_mode ||
1826 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
1827 cpi->oxcf.Mode <= 2));
1828 cpi->cyclic_refresh_mode_max_mbs_perframe =
1829 (cpi->common.mb_rows * cpi->common.mb_cols) / 7;
1830 if (cpi->oxcf.number_of_layers == 1) {
1831 cpi->cyclic_refresh_mode_max_mbs_perframe =
1832 (cpi->common.mb_rows * cpi->common.mb_cols) / 20;
1833 } else if (cpi->oxcf.number_of_layers == 2) {
1834 cpi->cyclic_refresh_mode_max_mbs_perframe =
1835 (cpi->common.mb_rows * cpi->common.mb_cols) / 10;
1837 cpi->cyclic_refresh_mode_index = 0;
1838 cpi->cyclic_refresh_q = 32;
1840 // GF behavior for 1 pass CBR, used when error_resilience is off.
1841 cpi->gf_update_onepass_cbr = 0;
1842 cpi->gf_noboost_onepass_cbr = 0;
1843 if (!cpi->oxcf.error_resilient_mode &&
1844 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER && cpi->oxcf.Mode <= 2) {
1845 cpi->gf_update_onepass_cbr = 1;
1846 cpi->gf_noboost_onepass_cbr = 1;
1847 cpi->gf_interval_onepass_cbr =
1848 cpi->cyclic_refresh_mode_max_mbs_perframe > 0
1849 ? (2 * (cpi->common.mb_rows * cpi->common.mb_cols) /
1850 cpi->cyclic_refresh_mode_max_mbs_perframe)
1852 cpi->gf_interval_onepass_cbr =
1853 VPXMIN(40, VPXMAX(6, cpi->gf_interval_onepass_cbr));
1854 cpi->baseline_gf_interval = cpi->gf_interval_onepass_cbr;
1857 if (cpi->cyclic_refresh_mode_enabled) {
1858 CHECK_MEM_ERROR(cpi->cyclic_refresh_map,
1859 vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1861 cpi->cyclic_refresh_map = (signed char *)NULL;
1864 CHECK_MEM_ERROR(cpi->consec_zero_last,
1865 vpx_calloc(cm->mb_rows * cm->mb_cols, 1));
1866 CHECK_MEM_ERROR(cpi->consec_zero_last_mvbias,
1867 vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1869 #ifdef VP8_ENTROPY_STATS
1870 init_context_counters();
1873 /*Initialize the feed-forward activity masking.*/
1874 cpi->activity_avg = 90 << 12;
1876 /* Give a sensible default for the first frame. */
1877 cpi->frames_since_key = 8;
1878 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1879 cpi->this_key_frame_forced = 0;
1880 cpi->next_key_frame_forced = 0;
1882 cpi->source_alt_ref_pending = 0;
1883 cpi->source_alt_ref_active = 0;
1884 cpi->common.refresh_alt_ref_frame = 0;
1886 cpi->force_maxqp = 0;
1888 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1889 #if CONFIG_INTERNAL_STATS
1890 cpi->b_calculate_ssimg = 0;
1895 if (cpi->b_calculate_psnr) {
1896 cpi->total_sq_error = 0.0;
1897 cpi->total_sq_error2 = 0.0;
1902 cpi->totalp_y = 0.0;
1903 cpi->totalp_u = 0.0;
1904 cpi->totalp_v = 0.0;
1906 cpi->tot_recode_hits = 0;
1907 cpi->summed_quality = 0;
1908 cpi->summed_weights = 0;
1913 cpi->first_time_stamp_ever = 0x7FFFFFFF;
1915 cpi->frames_till_gf_update_due = 0;
1916 cpi->key_frame_count = 1;
1918 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1921 cpi->total_byte_count = 0;
1923 cpi->drop_frame = 0;
1925 cpi->rate_correction_factor = 1.0;
1926 cpi->key_frame_rate_correction_factor = 1.0;
1927 cpi->gf_rate_correction_factor = 1.0;
1928 cpi->twopass.est_max_qcorrection_factor = 1.0;
1930 for (i = 0; i < KEY_FRAME_CONTEXT; ++i) {
1931 cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate;
1934 #ifdef OUTPUT_YUV_SRC
1935 yuv_file = fopen("bd.yuv", "ab");
1937 #ifdef OUTPUT_YUV_DENOISED
1938 yuv_denoised_file = fopen("denoised.yuv", "ab");
1940 #ifdef OUTPUT_YUV_SKINMAP
1941 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
1945 framepsnr = fopen("framepsnr.stt", "a");
1946 kf_list = fopen("kf_list.stt", "w");
1949 cpi->output_pkt_list = oxcf->output_pkt_list;
1951 #if !CONFIG_REALTIME_ONLY
1953 if (cpi->pass == 1) {
1954 vp8_init_first_pass(cpi);
1955 } else if (cpi->pass == 2) {
1956 size_t packet_sz = sizeof(FIRSTPASS_STATS);
1957 int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1959 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1960 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1961 cpi->twopass.stats_in_end =
1962 (void *)((char *)cpi->twopass.stats_in + (packets - 1) * packet_sz);
1963 vp8_init_second_pass(cpi);
1968 if (cpi->compressor_speed == 2) {
1969 cpi->avg_encode_time = 0;
1970 cpi->avg_pick_mode_time = 0;
1973 vp8_set_speed_features(cpi);
1975 /* Set starting values of RD threshold multipliers (128 = *1) */
1976 for (i = 0; i < MAX_MODES; ++i) {
1977 cpi->mb.rd_thresh_mult[i] = 128;
1980 #ifdef VP8_ENTROPY_STATS
1981 init_mv_ref_counts();
1984 #if CONFIG_MULTITHREAD
1985 if (vp8cx_create_encoder_threads(cpi)) {
1986 vp8_remove_compressor(&cpi);
1991 cpi->fn_ptr[BLOCK_16X16].sdf = vpx_sad16x16;
1992 cpi->fn_ptr[BLOCK_16X16].vf = vpx_variance16x16;
1993 cpi->fn_ptr[BLOCK_16X16].svf = vpx_sub_pixel_variance16x16;
1994 cpi->fn_ptr[BLOCK_16X16].sdx3f = vpx_sad16x16x3;
1995 cpi->fn_ptr[BLOCK_16X16].sdx8f = vpx_sad16x16x8;
1996 cpi->fn_ptr[BLOCK_16X16].sdx4df = vpx_sad16x16x4d;
1998 cpi->fn_ptr[BLOCK_16X8].sdf = vpx_sad16x8;
1999 cpi->fn_ptr[BLOCK_16X8].vf = vpx_variance16x8;
2000 cpi->fn_ptr[BLOCK_16X8].svf = vpx_sub_pixel_variance16x8;
2001 cpi->fn_ptr[BLOCK_16X8].sdx3f = vpx_sad16x8x3;
2002 cpi->fn_ptr[BLOCK_16X8].sdx8f = vpx_sad16x8x8;
2003 cpi->fn_ptr[BLOCK_16X8].sdx4df = vpx_sad16x8x4d;
2005 cpi->fn_ptr[BLOCK_8X16].sdf = vpx_sad8x16;
2006 cpi->fn_ptr[BLOCK_8X16].vf = vpx_variance8x16;
2007 cpi->fn_ptr[BLOCK_8X16].svf = vpx_sub_pixel_variance8x16;
2008 cpi->fn_ptr[BLOCK_8X16].sdx3f = vpx_sad8x16x3;
2009 cpi->fn_ptr[BLOCK_8X16].sdx8f = vpx_sad8x16x8;
2010 cpi->fn_ptr[BLOCK_8X16].sdx4df = vpx_sad8x16x4d;
2012 cpi->fn_ptr[BLOCK_8X8].sdf = vpx_sad8x8;
2013 cpi->fn_ptr[BLOCK_8X8].vf = vpx_variance8x8;
2014 cpi->fn_ptr[BLOCK_8X8].svf = vpx_sub_pixel_variance8x8;
2015 cpi->fn_ptr[BLOCK_8X8].sdx3f = vpx_sad8x8x3;
2016 cpi->fn_ptr[BLOCK_8X8].sdx8f = vpx_sad8x8x8;
2017 cpi->fn_ptr[BLOCK_8X8].sdx4df = vpx_sad8x8x4d;
2019 cpi->fn_ptr[BLOCK_4X4].sdf = vpx_sad4x4;
2020 cpi->fn_ptr[BLOCK_4X4].vf = vpx_variance4x4;
2021 cpi->fn_ptr[BLOCK_4X4].svf = vpx_sub_pixel_variance4x4;
2022 cpi->fn_ptr[BLOCK_4X4].sdx3f = vpx_sad4x4x3;
2023 cpi->fn_ptr[BLOCK_4X4].sdx8f = vpx_sad4x4x8;
2024 cpi->fn_ptr[BLOCK_4X4].sdx4df = vpx_sad4x4x4d;
2026 #if ARCH_X86 || ARCH_X86_64
2027 cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn;
2028 cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn;
2029 cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn;
2030 cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn;
2031 cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn;
2034 cpi->full_search_sad = vp8_full_search_sad;
2035 cpi->diamond_search_sad = vp8_diamond_search_sad;
2036 cpi->refining_search_sad = vp8_refining_search_sad;
2038 /* make sure frame 1 is okay */
2039 cpi->mb.error_bins[0] = cpi->common.MBs;
2041 /* vp8cx_init_quantizer() is first called here. Add check in
2042 * vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only
2043 * called later when needed. This will avoid unnecessary calls of
2044 * vp8cx_init_quantizer() for every frame.
2046 vp8cx_init_quantizer(cpi);
2048 vp8_loop_filter_init(cm);
2050 cpi->common.error.setjmp = 0;
2052 #if CONFIG_MULTI_RES_ENCODING
2054 /* Calculate # of MBs in a row in lower-resolution level image. */
2055 if (cpi->oxcf.mr_encoder_id > 0) vp8_cal_low_res_mb_cols(cpi);
2059 /* setup RD costs to MACROBLOCK struct */
2061 cpi->mb.mvcost[0] = &cpi->rd_costs.mvcosts[0][mv_max + 1];
2062 cpi->mb.mvcost[1] = &cpi->rd_costs.mvcosts[1][mv_max + 1];
2063 cpi->mb.mvsadcost[0] = &cpi->rd_costs.mvsadcosts[0][mvfp_max + 1];
2064 cpi->mb.mvsadcost[1] = &cpi->rd_costs.mvsadcosts[1][mvfp_max + 1];
2066 cal_mvsadcosts(cpi->mb.mvsadcost);
2068 cpi->mb.mbmode_cost = cpi->rd_costs.mbmode_cost;
2069 cpi->mb.intra_uv_mode_cost = cpi->rd_costs.intra_uv_mode_cost;
2070 cpi->mb.bmode_costs = cpi->rd_costs.bmode_costs;
2071 cpi->mb.inter_bmode_costs = cpi->rd_costs.inter_bmode_costs;
2072 cpi->mb.token_costs = cpi->rd_costs.token_costs;
2074 /* setup block ptrs & offsets */
2075 vp8_setup_block_ptrs(&cpi->mb);
2076 vp8_setup_block_dptrs(&cpi->mb.e_mbd);
2081 void vp8_remove_compressor(VP8_COMP **ptr) {
2082 VP8_COMP *cpi = *ptr;
2086 if (cpi && (cpi->common.current_video_frame > 0)) {
2087 #if !CONFIG_REALTIME_ONLY
2089 if (cpi->pass == 2) {
2090 vp8_end_second_pass(cpi);
2095 #ifdef VP8_ENTROPY_STATS
2096 print_context_counters();
2097 print_tree_update_probs();
2098 print_mode_context();
2101 #if CONFIG_INTERNAL_STATS
2103 if (cpi->pass != 1) {
2104 FILE *f = fopen("opsnr.stt", "a");
2105 double time_encoded =
2106 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2108 double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded;
2110 if (cpi->b_calculate_psnr) {
2111 if (cpi->oxcf.number_of_layers > 1) {
2115 "Layer\tBitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2116 "GLPsnrP\tVPXSSIM\n");
2117 for (i = 0; i < (int)cpi->oxcf.number_of_layers; ++i) {
2119 (double)cpi->bytes_in_layer[i] * 8.0 / 1000.0 / time_encoded;
2120 double samples = 3.0 / 2 * cpi->frames_in_layer[i] *
2121 cpi->common.Width * cpi->common.Height;
2123 vpx_sse_to_psnr(samples, 255.0, cpi->total_error2[i]);
2124 double total_psnr2 =
2125 vpx_sse_to_psnr(samples, 255.0, cpi->total_error2_p[i]);
2127 100 * pow(cpi->sum_ssim[i] / cpi->sum_weights[i], 8.0);
2130 "%5d\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2132 i, dr, cpi->sum_psnr[i] / cpi->frames_in_layer[i],
2133 total_psnr, cpi->sum_psnr_p[i] / cpi->frames_in_layer[i],
2134 total_psnr2, total_ssim);
2138 3.0 / 2 * cpi->count * cpi->common.Width * cpi->common.Height;
2140 vpx_sse_to_psnr(samples, 255.0, cpi->total_sq_error);
2141 double total_psnr2 =
2142 vpx_sse_to_psnr(samples, 255.0, cpi->total_sq_error2);
2144 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2147 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2148 "GLPsnrP\tVPXSSIM\n");
2150 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2152 dr, cpi->total / cpi->count, total_psnr,
2153 cpi->totalp / cpi->count, total_psnr2, total_ssim);
2158 f = fopen("qskip.stt", "a");
2159 fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2168 if (cpi->compressor_speed == 2) {
2170 FILE *f = fopen("cxspeed.stt", "a");
2171 cnt_pm /= cpi->common.MBs;
2173 for (i = 0; i < 16; ++i) fprintf(f, "%5d", frames_at_speed[i]);
2183 extern int count_mb_seg[4];
2184 FILE *f = fopen("modes.stt", "a");
2185 double dr = (double)cpi->framerate * (double)bytes * (double)8 /
2186 (double)count / (double)1000;
2187 fprintf(f, "intra_mode in Intra Frames:\n");
2188 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1],
2189 y_modes[2], y_modes[3], y_modes[4]);
2190 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1],
2191 uv_modes[2], uv_modes[3]);
2196 for (i = 0; i < 10; ++i) fprintf(f, "%8d, ", b_modes[i]);
2201 fprintf(f, "Modes in Inter Frames:\n");
2202 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2203 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2],
2204 inter_y_modes[3], inter_y_modes[4], inter_y_modes[5],
2205 inter_y_modes[6], inter_y_modes[7], inter_y_modes[8],
2207 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0],
2208 inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2213 for (i = 0; i < 15; ++i) fprintf(f, "%8d, ", inter_b_modes[i]);
2217 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1],
2218 count_mb_seg[2], count_mb_seg[3]);
2219 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4],
2220 inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4],
2221 inter_b_modes[NEW4X4]);
2227 #ifdef VP8_ENTROPY_STATS
2230 FILE *fmode = fopen("modecontext.c", "w");
2232 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2233 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2235 "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2237 for (i = 0; i < 10; ++i) {
2238 fprintf(fmode, " { /* Above Mode : %d */\n", i);
2240 for (j = 0; j < 10; ++j) {
2241 fprintf(fmode, " {");
2243 for (k = 0; k < 10; ++k) {
2244 if (!intra_mode_stats[i][j][k])
2245 fprintf(fmode, " %5d, ", 1);
2247 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2250 fprintf(fmode, "}, /* left_mode %d */\n", j);
2253 fprintf(fmode, " },\n");
2256 fprintf(fmode, "};\n");
2261 #if defined(SECTIONBITS_OUTPUT)
2265 FILE *f = fopen("tokenbits.stt", "a");
2267 for (i = 0; i < 28; ++i) fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2277 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2278 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2279 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, cpi->time_receive_data / 1000, cpi->time_encode_mb_row / 1000, cpi->time_compress_data / 1000, (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2284 #if CONFIG_MULTITHREAD
2285 vp8cx_remove_encoder_threads(cpi);
2288 #if CONFIG_TEMPORAL_DENOISING
2289 vp8_denoiser_free(&cpi->denoiser);
2291 dealloc_compressor_data(cpi);
2292 vpx_free(cpi->mb.ss);
2294 vpx_free(cpi->cyclic_refresh_map);
2295 vpx_free(cpi->consec_zero_last);
2296 vpx_free(cpi->consec_zero_last_mvbias);
2298 vp8_remove_common(&cpi->common);
2302 #ifdef OUTPUT_YUV_SRC
2305 #ifdef OUTPUT_YUV_DENOISED
2306 fclose(yuv_denoised_file);
2308 #ifdef OUTPUT_YUV_SKINMAP
2309 fclose(yuv_skinmap_file);
2326 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2327 unsigned char *recon, int recon_stride,
2328 unsigned int cols, unsigned int rows) {
2329 unsigned int row, col;
2330 uint64_t total_sse = 0;
2333 for (row = 0; row + 16 <= rows; row += 16) {
2334 for (col = 0; col + 16 <= cols; col += 16) {
2337 vpx_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse);
2341 /* Handle odd-sized width */
2343 unsigned int border_row, border_col;
2344 unsigned char *border_orig = orig;
2345 unsigned char *border_recon = recon;
2347 for (border_row = 0; border_row < 16; ++border_row) {
2348 for (border_col = col; border_col < cols; ++border_col) {
2349 diff = border_orig[border_col] - border_recon[border_col];
2350 total_sse += diff * diff;
2353 border_orig += orig_stride;
2354 border_recon += recon_stride;
2358 orig += orig_stride * 16;
2359 recon += recon_stride * 16;
2362 /* Handle odd-sized height */
2363 for (; row < rows; ++row) {
2364 for (col = 0; col < cols; ++col) {
2365 diff = orig[col] - recon[col];
2366 total_sse += diff * diff;
2369 orig += orig_stride;
2370 recon += recon_stride;
2373 vpx_clear_system_state();
2377 static void generate_psnr_packet(VP8_COMP *cpi) {
2378 YV12_BUFFER_CONFIG *orig = cpi->Source;
2379 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2380 struct vpx_codec_cx_pkt pkt;
2383 unsigned int width = cpi->common.Width;
2384 unsigned int height = cpi->common.Height;
2386 pkt.kind = VPX_CODEC_PSNR_PKT;
2387 sse = calc_plane_error(orig->y_buffer, orig->y_stride, recon->y_buffer,
2388 recon->y_stride, width, height);
2389 pkt.data.psnr.sse[0] = sse;
2390 pkt.data.psnr.sse[1] = sse;
2391 pkt.data.psnr.samples[0] = width * height;
2392 pkt.data.psnr.samples[1] = width * height;
2394 width = (width + 1) / 2;
2395 height = (height + 1) / 2;
2397 sse = calc_plane_error(orig->u_buffer, orig->uv_stride, recon->u_buffer,
2398 recon->uv_stride, width, height);
2399 pkt.data.psnr.sse[0] += sse;
2400 pkt.data.psnr.sse[2] = sse;
2401 pkt.data.psnr.samples[0] += width * height;
2402 pkt.data.psnr.samples[2] = width * height;
2404 sse = calc_plane_error(orig->v_buffer, orig->uv_stride, recon->v_buffer,
2405 recon->uv_stride, width, height);
2406 pkt.data.psnr.sse[0] += sse;
2407 pkt.data.psnr.sse[3] = sse;
2408 pkt.data.psnr.samples[0] += width * height;
2409 pkt.data.psnr.samples[3] = width * height;
2411 for (i = 0; i < 4; ++i) {
2412 pkt.data.psnr.psnr[i] = vpx_sse_to_psnr(pkt.data.psnr.samples[i], 255.0,
2413 (double)(pkt.data.psnr.sse[i]));
2416 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2419 int vp8_use_as_reference(VP8_COMP *cpi, int ref_frame_flags) {
2420 if (ref_frame_flags > 7) return -1;
2422 cpi->ref_frame_flags = ref_frame_flags;
2425 int vp8_update_reference(VP8_COMP *cpi, int ref_frame_flags) {
2426 if (ref_frame_flags > 7) return -1;
2428 cpi->common.refresh_golden_frame = 0;
2429 cpi->common.refresh_alt_ref_frame = 0;
2430 cpi->common.refresh_last_frame = 0;
2432 if (ref_frame_flags & VP8_LAST_FRAME) cpi->common.refresh_last_frame = 1;
2434 if (ref_frame_flags & VP8_GOLD_FRAME) cpi->common.refresh_golden_frame = 1;
2436 if (ref_frame_flags & VP8_ALTR_FRAME) cpi->common.refresh_alt_ref_frame = 1;
2441 int vp8_get_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag,
2442 YV12_BUFFER_CONFIG *sd) {
2443 VP8_COMMON *cm = &cpi->common;
2446 if (ref_frame_flag == VP8_LAST_FRAME) {
2447 ref_fb_idx = cm->lst_fb_idx;
2448 } else if (ref_frame_flag == VP8_GOLD_FRAME) {
2449 ref_fb_idx = cm->gld_fb_idx;
2450 } else if (ref_frame_flag == VP8_ALTR_FRAME) {
2451 ref_fb_idx = cm->alt_fb_idx;
2456 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
2460 int vp8_set_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag,
2461 YV12_BUFFER_CONFIG *sd) {
2462 VP8_COMMON *cm = &cpi->common;
2466 if (ref_frame_flag == VP8_LAST_FRAME) {
2467 ref_fb_idx = cm->lst_fb_idx;
2468 } else if (ref_frame_flag == VP8_GOLD_FRAME) {
2469 ref_fb_idx = cm->gld_fb_idx;
2470 } else if (ref_frame_flag == VP8_ALTR_FRAME) {
2471 ref_fb_idx = cm->alt_fb_idx;
2476 vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
2480 int vp8_update_entropy(VP8_COMP *cpi, int update) {
2481 VP8_COMMON *cm = &cpi->common;
2482 cm->refresh_entropy_probs = update;
2487 #if defined(OUTPUT_YUV_SRC) || defined(OUTPUT_YUV_DENOISED) || \
2488 defined(OUTPUT_YUV_SKINMAP)
2489 void vp8_write_yuv_frame(FILE *yuv_file, YV12_BUFFER_CONFIG *s) {
2490 unsigned char *src = s->y_buffer;
2491 int h = s->y_height;
2494 fwrite(src, s->y_width, 1, yuv_file);
2502 fwrite(src, s->uv_width, 1, yuv_file);
2503 src += s->uv_stride;
2510 fwrite(src, s->uv_width, 1, yuv_file);
2511 src += s->uv_stride;
2516 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi) {
2517 VP8_COMMON *cm = &cpi->common;
2519 /* are we resizing the image */
2520 if (cm->horiz_scale != 0 || cm->vert_scale != 0) {
2521 #if CONFIG_SPATIAL_RESAMPLING
2525 if (cm->vert_scale == 3) {
2531 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2532 Scale2Ratio(cm->vert_scale, &vr, &vs);
2534 vpx_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2535 tmp_height, hs, hr, vs, vr, 0);
2537 vp8_yv12_extend_frame_borders(&cpi->scaled_source);
2538 cpi->Source = &cpi->scaled_source;
2545 static int resize_key_frame(VP8_COMP *cpi) {
2546 #if CONFIG_SPATIAL_RESAMPLING
2547 VP8_COMMON *cm = &cpi->common;
2549 /* Do we need to apply resampling for one pass cbr.
2550 * In one pass this is more limited than in two pass cbr.
2551 * The test and any change is only made once per key frame sequence.
2553 if (cpi->oxcf.allow_spatial_resampling &&
2554 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) {
2556 int new_width, new_height;
2558 /* If we are below the resample DOWN watermark then scale down a
2561 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark *
2562 cpi->oxcf.optimal_buffer_level / 100)) {
2564 (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2565 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2567 /* Should we now start scaling back up */
2568 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark *
2569 cpi->oxcf.optimal_buffer_level / 100)) {
2571 (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2572 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2575 /* Get the new height and width */
2576 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2577 Scale2Ratio(cm->vert_scale, &vr, &vs);
2578 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2579 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2581 /* If the image size has changed we need to reallocate the buffers
2582 * and resample the source image
2584 if ((cm->Width != new_width) || (cm->Height != new_height)) {
2585 cm->Width = new_width;
2586 cm->Height = new_height;
2587 vp8_alloc_compressor_data(cpi);
2588 scale_and_extend_source(cpi->un_scaled_source, cpi);
2597 static void update_alt_ref_frame_stats(VP8_COMP *cpi) {
2598 VP8_COMMON *cm = &cpi->common;
2600 /* Select an interval before next GF or altref */
2601 if (!cpi->auto_gold) cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2603 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due) {
2604 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2606 /* Set the bits per frame that we should try and recover in
2607 * subsequent inter frames to account for the extra GF spend...
2608 * note that his does not apply for GF updates that occur
2609 * coincident with a key frame as the extra cost of key frames is
2610 * dealt with elsewhere.
2612 cpi->gf_overspend_bits += cpi->projected_frame_size;
2613 cpi->non_gf_bitrate_adjustment =
2614 cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2617 /* Update data structure that monitors level of reference to last GF */
2618 memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2619 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2621 /* this frame refreshes means next frames don't unless specified by user */
2622 cpi->frames_since_golden = 0;
2624 /* Clear the alternate reference update pending flag. */
2625 cpi->source_alt_ref_pending = 0;
2627 /* Set the alternate reference frame active flag */
2628 cpi->source_alt_ref_active = 1;
2630 static void update_golden_frame_stats(VP8_COMP *cpi) {
2631 VP8_COMMON *cm = &cpi->common;
2633 /* Update the Golden frame usage counts. */
2634 if (cm->refresh_golden_frame) {
2635 /* Select an interval before next GF */
2636 if (!cpi->auto_gold) cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2638 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0)) {
2639 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2641 /* Set the bits per frame that we should try and recover in
2642 * subsequent inter frames to account for the extra GF spend...
2643 * note that his does not apply for GF updates that occur
2644 * coincident with a key frame as the extra cost of key frames
2645 * is dealt with elsewhere.
2647 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active) {
2648 /* Calcluate GF bits to be recovered
2649 * Projected size - av frame bits available for inter
2650 * frames for clip as a whole
2652 cpi->gf_overspend_bits +=
2653 (cpi->projected_frame_size - cpi->inter_frame_target);
2656 cpi->non_gf_bitrate_adjustment =
2657 cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2660 /* Update data structure that monitors level of reference to last GF */
2661 memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2662 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2664 /* this frame refreshes means next frames don't unless specified by
2667 cm->refresh_golden_frame = 0;
2668 cpi->frames_since_golden = 0;
2670 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2671 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2672 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2673 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2675 /* ******** Fixed Q test code only ************ */
2676 /* If we are going to use the ALT reference for the next group of
2677 * frames set a flag to say so.
2679 if (cpi->oxcf.fixed_q >= 0 && cpi->oxcf.play_alternate &&
2680 !cpi->common.refresh_alt_ref_frame) {
2681 cpi->source_alt_ref_pending = 1;
2682 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2685 if (!cpi->source_alt_ref_pending) cpi->source_alt_ref_active = 0;
2687 /* Decrement count down till next gf */
2688 if (cpi->frames_till_gf_update_due > 0) cpi->frames_till_gf_update_due--;
2690 } else if (!cpi->common.refresh_alt_ref_frame) {
2691 /* Decrement count down till next gf */
2692 if (cpi->frames_till_gf_update_due > 0) cpi->frames_till_gf_update_due--;
2694 if (cpi->frames_till_alt_ref_frame) cpi->frames_till_alt_ref_frame--;
2696 cpi->frames_since_golden++;
2698 if (cpi->frames_since_golden > 1) {
2699 cpi->recent_ref_frame_usage[INTRA_FRAME] +=
2700 cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME];
2701 cpi->recent_ref_frame_usage[LAST_FRAME] +=
2702 cpi->mb.count_mb_ref_frame_usage[LAST_FRAME];
2703 cpi->recent_ref_frame_usage[GOLDEN_FRAME] +=
2704 cpi->mb.count_mb_ref_frame_usage[GOLDEN_FRAME];
2705 cpi->recent_ref_frame_usage[ALTREF_FRAME] +=
2706 cpi->mb.count_mb_ref_frame_usage[ALTREF_FRAME];
2711 /* This function updates the reference frame probability estimates that
2712 * will be used during mode selection
2714 static void update_rd_ref_frame_probs(VP8_COMP *cpi) {
2715 VP8_COMMON *cm = &cpi->common;
2717 const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
2718 const int rf_intra = rfct[INTRA_FRAME];
2719 const int rf_inter =
2720 rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2722 if (cm->frame_type == KEY_FRAME) {
2723 cpi->prob_intra_coded = 255;
2724 cpi->prob_last_coded = 128;
2725 cpi->prob_gf_coded = 128;
2726 } else if (!(rf_intra + rf_inter)) {
2727 cpi->prob_intra_coded = 63;
2728 cpi->prob_last_coded = 128;
2729 cpi->prob_gf_coded = 128;
2732 /* update reference frame costs since we can do better than what we got
2735 if (cpi->oxcf.number_of_layers == 1) {
2736 if (cpi->common.refresh_alt_ref_frame) {
2737 cpi->prob_intra_coded += 40;
2738 if (cpi->prob_intra_coded > 255) cpi->prob_intra_coded = 255;
2739 cpi->prob_last_coded = 200;
2740 cpi->prob_gf_coded = 1;
2741 } else if (cpi->frames_since_golden == 0) {
2742 cpi->prob_last_coded = 214;
2743 } else if (cpi->frames_since_golden == 1) {
2744 cpi->prob_last_coded = 192;
2745 cpi->prob_gf_coded = 220;
2746 } else if (cpi->source_alt_ref_active) {
2747 cpi->prob_gf_coded -= 20;
2749 if (cpi->prob_gf_coded < 10) cpi->prob_gf_coded = 10;
2751 if (!cpi->source_alt_ref_active) cpi->prob_gf_coded = 255;
2755 #if !CONFIG_REALTIME_ONLY
2756 /* 1 = key, 0 = inter */
2757 static int decide_key_frame(VP8_COMP *cpi) {
2758 VP8_COMMON *cm = &cpi->common;
2760 int code_key_frame = 0;
2764 if (cpi->Speed > 11) return 0;
2766 /* Clear down mmx registers */
2767 vpx_clear_system_state();
2769 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0)) {
2770 double change = 1.0 *
2771 abs((int)(cpi->mb.intra_error - cpi->last_intra_error)) /
2772 (1 + cpi->last_intra_error);
2775 abs((int)(cpi->mb.prediction_error - cpi->last_prediction_error)) /
2776 (1 + cpi->last_prediction_error);
2777 double minerror = cm->MBs * 256;
2779 cpi->last_intra_error = cpi->mb.intra_error;
2780 cpi->last_prediction_error = cpi->mb.prediction_error;
2782 if (10 * cpi->mb.intra_error / (1 + cpi->mb.prediction_error) < 15 &&
2783 cpi->mb.prediction_error > minerror &&
2784 (change > .25 || change2 > .25)) {
2785 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra >
2786 * cpi->last_frame_percent_intra + 3*/
2793 /* If the following are true we might as well code a key frame */
2794 if (((cpi->this_frame_percent_intra == 100) &&
2795 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
2796 ((cpi->this_frame_percent_intra > 95) &&
2797 (cpi->this_frame_percent_intra >=
2798 (cpi->last_frame_percent_intra + 5)))) {
2801 /* in addition if the following are true and this is not a golden frame
2802 * then code a key frame Note that on golden frames there often seems
2803 * to be a pop in intra useage anyway hence this restriction is
2804 * designed to prevent spurious key frames. The Intra pop needs to be
2807 else if (((cpi->this_frame_percent_intra > 60) &&
2808 (cpi->this_frame_percent_intra >
2809 (cpi->last_frame_percent_intra * 2))) ||
2810 ((cpi->this_frame_percent_intra > 75) &&
2811 (cpi->this_frame_percent_intra >
2812 (cpi->last_frame_percent_intra * 3 / 2))) ||
2813 ((cpi->this_frame_percent_intra > 90) &&
2814 (cpi->this_frame_percent_intra >
2815 (cpi->last_frame_percent_intra + 10)))) {
2816 if (!cm->refresh_golden_frame) code_key_frame = 1;
2819 return code_key_frame;
2822 static void Pass1Encode(VP8_COMP *cpi, size_t *size, unsigned char *dest,
2823 unsigned int *frame_flags) {
2827 vp8_set_quantizer(cpi, 26);
2829 vp8_first_pass(cpi);
2834 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
2837 /* write the frame */
2842 sprintf(filename, "cx\\y%04d.raw", this_frame);
2843 yframe = fopen(filename, "wb");
2845 for (i = 0; i < frame->y_height; ++i)
2846 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
2849 sprintf(filename, "cx\\u%04d.raw", this_frame);
2850 yframe = fopen(filename, "wb");
2852 for (i = 0; i < frame->uv_height; ++i)
2853 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2856 sprintf(filename, "cx\\v%04d.raw", this_frame);
2857 yframe = fopen(filename, "wb");
2859 for (i = 0; i < frame->uv_height; ++i)
2860 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2865 /* return of 0 means drop frame */
2867 #if !CONFIG_REALTIME_ONLY
2868 /* Function to test for conditions that indeicate we should loop
2869 * back and recode a frame.
2871 static int recode_loop_test(VP8_COMP *cpi, int high_limit, int low_limit, int q,
2872 int maxq, int minq) {
2873 int force_recode = 0;
2874 VP8_COMMON *cm = &cpi->common;
2876 /* Is frame recode allowed at all
2877 * Yes if either recode mode 1 is selected or mode two is selcted
2878 * and the frame is a key frame. golden frame or alt_ref_frame
2880 if ((cpi->sf.recode_loop == 1) ||
2881 ((cpi->sf.recode_loop == 2) &&
2882 ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame ||
2883 cm->refresh_alt_ref_frame))) {
2884 /* General over and under shoot tests */
2885 if (((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
2886 ((cpi->projected_frame_size < low_limit) && (q > minq))) {
2889 /* Special Constrained quality tests */
2890 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2891 /* Undershoot and below auto cq level */
2892 if ((q > cpi->cq_target_quality) &&
2893 (cpi->projected_frame_size < ((cpi->this_frame_target * 7) >> 3))) {
2896 /* Severe undershoot and between auto and user cq level */
2897 else if ((q > cpi->oxcf.cq_level) &&
2898 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
2899 (cpi->active_best_quality > cpi->oxcf.cq_level)) {
2901 cpi->active_best_quality = cpi->oxcf.cq_level;
2906 return force_recode;
2908 #endif // !CONFIG_REALTIME_ONLY
2910 static void update_reference_frames(VP8_COMP *cpi) {
2911 VP8_COMMON *cm = &cpi->common;
2912 YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
2914 /* At this point the new frame has been encoded.
2915 * If any buffer copy / swapping is signaled it should be done here.
2918 if (cm->frame_type == KEY_FRAME) {
2919 yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME | VP8_ALTR_FRAME;
2921 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2922 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2924 cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
2926 cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
2927 cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
2928 } else /* For non key frames */
2930 if (cm->refresh_alt_ref_frame) {
2931 assert(!cm->copy_buffer_to_arf);
2933 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALTR_FRAME;
2934 cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2935 cm->alt_fb_idx = cm->new_fb_idx;
2937 cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
2938 } else if (cm->copy_buffer_to_arf) {
2939 assert(!(cm->copy_buffer_to_arf & ~0x3));
2941 if (cm->copy_buffer_to_arf == 1) {
2942 if (cm->alt_fb_idx != cm->lst_fb_idx) {
2943 yv12_fb[cm->lst_fb_idx].flags |= VP8_ALTR_FRAME;
2944 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2945 cm->alt_fb_idx = cm->lst_fb_idx;
2947 cpi->current_ref_frames[ALTREF_FRAME] =
2948 cpi->current_ref_frames[LAST_FRAME];
2950 } else /* if (cm->copy_buffer_to_arf == 2) */
2952 if (cm->alt_fb_idx != cm->gld_fb_idx) {
2953 yv12_fb[cm->gld_fb_idx].flags |= VP8_ALTR_FRAME;
2954 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2955 cm->alt_fb_idx = cm->gld_fb_idx;
2957 cpi->current_ref_frames[ALTREF_FRAME] =
2958 cpi->current_ref_frames[GOLDEN_FRAME];
2963 if (cm->refresh_golden_frame) {
2964 assert(!cm->copy_buffer_to_gf);
2966 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME;
2967 cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2968 cm->gld_fb_idx = cm->new_fb_idx;
2970 cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
2971 } else if (cm->copy_buffer_to_gf) {
2972 assert(!(cm->copy_buffer_to_arf & ~0x3));
2974 if (cm->copy_buffer_to_gf == 1) {
2975 if (cm->gld_fb_idx != cm->lst_fb_idx) {
2976 yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FRAME;
2977 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2978 cm->gld_fb_idx = cm->lst_fb_idx;
2980 cpi->current_ref_frames[GOLDEN_FRAME] =
2981 cpi->current_ref_frames[LAST_FRAME];
2983 } else /* if (cm->copy_buffer_to_gf == 2) */
2985 if (cm->alt_fb_idx != cm->gld_fb_idx) {
2986 yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FRAME;
2987 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2988 cm->gld_fb_idx = cm->alt_fb_idx;
2990 cpi->current_ref_frames[GOLDEN_FRAME] =
2991 cpi->current_ref_frames[ALTREF_FRAME];
2997 if (cm->refresh_last_frame) {
2998 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FRAME;
2999 cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FRAME;
3000 cm->lst_fb_idx = cm->new_fb_idx;
3002 cpi->current_ref_frames[LAST_FRAME] = cm->current_video_frame;
3005 #if CONFIG_TEMPORAL_DENOISING
3006 if (cpi->oxcf.noise_sensitivity) {
3007 /* we shouldn't have to keep multiple copies as we know in advance which
3008 * buffer we should start - for now to get something up and running
3009 * I've chosen to copy the buffers
3011 if (cm->frame_type == KEY_FRAME) {
3013 for (i = LAST_FRAME; i < MAX_REF_FRAMES; ++i)
3014 vp8_yv12_copy_frame(cpi->Source, &cpi->denoiser.yv12_running_avg[i]);
3015 } else /* For non key frames */
3017 vp8_yv12_extend_frame_borders(
3018 &cpi->denoiser.yv12_running_avg[INTRA_FRAME]);
3020 if (cm->refresh_alt_ref_frame || cm->copy_buffer_to_arf) {
3021 vp8_yv12_copy_frame(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3022 &cpi->denoiser.yv12_running_avg[ALTREF_FRAME]);
3024 if (cm->refresh_golden_frame || cm->copy_buffer_to_gf) {
3025 vp8_yv12_copy_frame(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3026 &cpi->denoiser.yv12_running_avg[GOLDEN_FRAME]);
3028 if (cm->refresh_last_frame) {
3029 vp8_yv12_copy_frame(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3030 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3033 if (cpi->oxcf.noise_sensitivity == 4)
3034 vp8_yv12_copy_frame(cpi->Source, &cpi->denoiser.yv12_last_source);
3039 static int measure_square_diff_partial(YV12_BUFFER_CONFIG *source,
3040 YV12_BUFFER_CONFIG *dest,
3046 int min_consec_zero_last = 10;
3047 int tot_num_blocks = (source->y_height * source->y_width) >> 8;
3048 unsigned char *src = source->y_buffer;
3049 unsigned char *dst = dest->y_buffer;
3051 /* Loop through the Y plane, every |skip| blocks along rows and colmumns,
3052 * summing the square differences, and only for blocks that have been
3053 * zero_last mode at least |x| frames in a row.
3055 for (i = 0; i < source->y_height; i += 16 * skip) {
3056 int block_index_row = (i >> 4) * cpi->common.mb_cols;
3057 for (j = 0; j < source->y_width; j += 16 * skip) {
3058 int index = block_index_row + (j >> 4);
3059 if (cpi->consec_zero_last[index] >= min_consec_zero_last) {
3061 Total += vpx_mse16x16(src + j, source->y_stride, dst + j,
3062 dest->y_stride, &sse);
3066 src += 16 * skip * source->y_stride;
3067 dst += 16 * skip * dest->y_stride;
3069 // Only return non-zero if we have at least ~1/16 samples for estimate.
3070 if (num_blocks > (tot_num_blocks >> 4)) {
3071 assert(num_blocks != 0);
3072 return (Total / num_blocks);
3078 #if CONFIG_TEMPORAL_DENOISING
3079 static void process_denoiser_mode_change(VP8_COMP *cpi) {
3080 const VP8_COMMON *const cm = &cpi->common;
3084 // Number of blocks skipped along row/column in computing the
3085 // nmse (normalized mean square error) of source.
3087 // Only select blocks for computing nmse that have been encoded
3088 // as ZERO LAST min_consec_zero_last frames in a row.
3089 // Scale with number of temporal layers.
3090 int min_consec_zero_last = 12 / cpi->oxcf.number_of_layers;
3091 // Decision is tested for changing the denoising mode every
3092 // num_mode_change times this function is called. Note that this
3093 // function called every 8 frames, so (8 * num_mode_change) is number
3094 // of frames where denoising mode change is tested for switch.
3095 int num_mode_change = 20;
3096 // Framerate factor, to compensate for larger mse at lower framerates.
3097 // Use ref_framerate, which is full source framerate for temporal layers.
3098 // TODO(marpan): Adjust this factor.
3099 int fac_framerate = cpi->ref_framerate < 25.0f ? 80 : 100;
3100 int tot_num_blocks = cm->mb_rows * cm->mb_cols;
3101 int ystride = cpi->Source->y_stride;
3102 unsigned char *src = cpi->Source->y_buffer;
3103 unsigned char *dst = cpi->denoiser.yv12_last_source.y_buffer;
3104 static const unsigned char const_source[16] = { 128, 128, 128, 128, 128, 128,
3105 128, 128, 128, 128, 128, 128,
3106 128, 128, 128, 128 };
3107 int bandwidth = (int)(cpi->target_bandwidth);
3108 // For temporal layers, use full bandwidth (top layer).
3109 if (cpi->oxcf.number_of_layers > 1) {
3110 LAYER_CONTEXT *lc = &cpi->layer_context[cpi->oxcf.number_of_layers - 1];
3111 bandwidth = (int)(lc->target_bandwidth);
3113 // Loop through the Y plane, every skip blocks along rows and columns,
3114 // summing the normalized mean square error, only for blocks that have
3115 // been encoded as ZEROMV LAST at least min_consec_zero_last least frames in
3116 // a row and have small sum difference between current and previous frame.
3117 // Normalization here is by the contrast of the current frame block.
3118 for (i = 0; i < cm->Height; i += 16 * skip) {
3119 int block_index_row = (i >> 4) * cm->mb_cols;
3120 for (j = 0; j < cm->Width; j += 16 * skip) {
3121 int index = block_index_row + (j >> 4);
3122 if (cpi->consec_zero_last[index] >= min_consec_zero_last) {
3124 const unsigned int var =
3125 vpx_variance16x16(src + j, ystride, dst + j, ystride, &sse);
3126 // Only consider this block as valid for noise measurement
3127 // if the sum_diff average of the current and previous frame
3128 // is small (to avoid effects from lighting change).
3129 if ((sse - var) < 128) {
3131 const unsigned int act =
3132 vpx_variance16x16(src + j, ystride, const_source, 0, &sse2);
3133 if (act > 0) total += sse / act;
3138 src += 16 * skip * ystride;
3139 dst += 16 * skip * ystride;
3141 total = total * fac_framerate / 100;
3143 // Only consider this frame as valid sample if we have computed nmse over
3144 // at least ~1/16 blocks, and Total > 0 (Total == 0 can happen if the
3145 // application inputs duplicate frames, or contrast is all zero).
3146 if (total > 0 && (num_blocks > (tot_num_blocks >> 4))) {
3147 // Update the recursive mean square source_diff.
3148 total = (total << 8) / num_blocks;
3149 if (cpi->denoiser.nmse_source_diff_count == 0) {
3150 // First sample in new interval.
3151 cpi->denoiser.nmse_source_diff = total;
3152 cpi->denoiser.qp_avg = cm->base_qindex;
3154 // For subsequent samples, use average with weight ~1/4 for new sample.
3155 cpi->denoiser.nmse_source_diff =
3156 (int)((total + 3 * cpi->denoiser.nmse_source_diff) >> 2);
3157 cpi->denoiser.qp_avg =
3158 (int)((cm->base_qindex + 3 * cpi->denoiser.qp_avg) >> 2);
3160 cpi->denoiser.nmse_source_diff_count++;
3162 // Check for changing the denoiser mode, when we have obtained #samples =
3163 // num_mode_change. Condition the change also on the bitrate and QP.
3164 if (cpi->denoiser.nmse_source_diff_count == num_mode_change) {
3165 // Check for going up: from normal to aggressive mode.
3166 if ((cpi->denoiser.denoiser_mode == kDenoiserOnYUV) &&
3167 (cpi->denoiser.nmse_source_diff >
3168 cpi->denoiser.threshold_aggressive_mode) &&
3169 (cpi->denoiser.qp_avg < cpi->denoiser.qp_threshold_up &&
3170 bandwidth > cpi->denoiser.bitrate_threshold)) {
3171 vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUVAggressive);
3173 // Check for going down: from aggressive to normal mode.
3174 if (((cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) &&
3175 (cpi->denoiser.nmse_source_diff <
3176 cpi->denoiser.threshold_aggressive_mode)) ||
3177 ((cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) &&
3178 (cpi->denoiser.qp_avg > cpi->denoiser.qp_threshold_down ||
3179 bandwidth < cpi->denoiser.bitrate_threshold))) {
3180 vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUV);
3183 // Reset metric and counter for next interval.
3184 cpi->denoiser.nmse_source_diff = 0;
3185 cpi->denoiser.qp_avg = 0;
3186 cpi->denoiser.nmse_source_diff_count = 0;
3191 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm) {
3192 const FRAME_TYPE frame_type = cm->frame_type;
3194 int update_any_ref_buffers = 1;
3195 if (cpi->common.refresh_last_frame == 0 &&
3196 cpi->common.refresh_golden_frame == 0 &&
3197 cpi->common.refresh_alt_ref_frame == 0) {
3198 update_any_ref_buffers = 0;
3202 cm->filter_level = 0;
3204 struct vpx_usec_timer timer;
3206 vpx_clear_system_state();
3208 vpx_usec_timer_start(&timer);
3209 if (cpi->sf.auto_filter == 0) {
3210 #if CONFIG_TEMPORAL_DENOISING
3211 if (cpi->oxcf.noise_sensitivity && cm->frame_type != KEY_FRAME) {
3212 // Use the denoised buffer for selecting base loop filter level.
3213 // Denoised signal for current frame is stored in INTRA_FRAME.
3214 // No denoising on key frames.
3215 vp8cx_pick_filter_level_fast(
3216 &cpi->denoiser.yv12_running_avg[INTRA_FRAME], cpi);
3218 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3221 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3224 #if CONFIG_TEMPORAL_DENOISING
3225 if (cpi->oxcf.noise_sensitivity && cm->frame_type != KEY_FRAME) {
3226 // Use the denoised buffer for selecting base loop filter level.
3227 // Denoised signal for current frame is stored in INTRA_FRAME.
3228 // No denoising on key frames.
3229 vp8cx_pick_filter_level(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3232 vp8cx_pick_filter_level(cpi->Source, cpi);
3235 vp8cx_pick_filter_level(cpi->Source, cpi);
3239 if (cm->filter_level > 0) {
3240 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3243 vpx_usec_timer_mark(&timer);
3244 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3247 #if CONFIG_MULTITHREAD
3248 if (cpi->b_multi_threaded) {
3249 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3253 // No need to apply loop-filter if the encoded frame does not update
3254 // any reference buffers.
3255 if (cm->filter_level > 0 && update_any_ref_buffers) {
3256 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, frame_type);
3259 vp8_yv12_extend_frame_borders(cm->frame_to_show);
3262 static void encode_frame_to_data_rate(VP8_COMP *cpi, size_t *size,
3263 unsigned char *dest,
3264 unsigned char *dest_end,
3265 unsigned int *frame_flags) {
3267 int frame_over_shoot_limit;
3268 int frame_under_shoot_limit;
3273 VP8_COMMON *cm = &cpi->common;
3274 int active_worst_qchanged = 0;
3276 #if !CONFIG_REALTIME_ONLY
3280 int zbin_oq_low = 0;
3283 int overshoot_seen = 0;
3284 int undershoot_seen = 0;
3287 int drop_mark = (int)(cpi->oxcf.drop_frames_water_mark *
3288 cpi->oxcf.optimal_buffer_level / 100);
3289 int drop_mark75 = drop_mark * 2 / 3;
3290 int drop_mark50 = drop_mark / 4;
3291 int drop_mark25 = drop_mark / 8;
3293 /* Clear down mmx registers to allow floating point in what follows */
3294 vpx_clear_system_state();
3296 if (cpi->force_next_frame_intra) {
3297 cm->frame_type = KEY_FRAME; /* delayed intra frame */
3298 cpi->force_next_frame_intra = 0;
3301 /* For an alt ref frame in 2 pass we skip the call to the second pass
3302 * function that sets the target bandwidth
3304 switch (cpi->pass) {
3305 #if !CONFIG_REALTIME_ONLY
3307 if (cpi->common.refresh_alt_ref_frame) {
3308 /* Per frame bit target for the alt ref frame */
3309 cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
3310 /* per second target bitrate */
3311 cpi->target_bandwidth =
3312 (int)(cpi->twopass.gf_bits * cpi->output_framerate);
3315 #endif // !CONFIG_REALTIME_ONLY
3317 cpi->per_frame_bandwidth =
3318 (int)(cpi->target_bandwidth / cpi->output_framerate);
3322 /* Default turn off buffer to buffer copying */
3323 cm->copy_buffer_to_gf = 0;
3324 cm->copy_buffer_to_arf = 0;
3326 /* Clear zbin over-quant value and mode boost values. */
3327 cpi->mb.zbin_over_quant = 0;
3328 cpi->mb.zbin_mode_boost = 0;
3330 /* Enable or disable mode based tweaking of the zbin
3331 * For 2 Pass Only used where GF/ARF prediction quality
3332 * is above a threshold
3334 cpi->mb.zbin_mode_boost_enabled = 1;
3335 if (cpi->pass == 2) {
3336 if (cpi->gfu_boost <= 400) {
3337 cpi->mb.zbin_mode_boost_enabled = 0;
3341 /* Current default encoder behaviour for the altref sign bias */
3342 if (cpi->source_alt_ref_active) {
3343 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3345 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3348 /* Check to see if a key frame is signaled
3349 * For two pass with auto key frame enabled cm->frame_type may already
3350 * be set, but not for one pass.
3352 if ((cm->current_video_frame == 0) || (cm->frame_flags & FRAMEFLAGS_KEY) ||
3353 (cpi->oxcf.auto_key &&
3354 (cpi->frames_since_key % cpi->key_frame_frequency == 0))) {
3355 /* Key frame from VFW/auto-keyframe/first frame */
3356 cm->frame_type = KEY_FRAME;
3357 #if CONFIG_TEMPORAL_DENOISING
3358 if (cpi->oxcf.noise_sensitivity == 4) {
3359 // For adaptive mode, reset denoiser to normal mode on key frame.
3360 vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUV);
3365 #if CONFIG_MULTI_RES_ENCODING
3366 if (cpi->oxcf.mr_total_resolutions > 1) {
3367 LOWER_RES_FRAME_INFO *low_res_frame_info =
3368 (LOWER_RES_FRAME_INFO *)cpi->oxcf.mr_low_res_mode_info;
3370 if (cpi->oxcf.mr_encoder_id) {
3371 // TODO(marpan): This constraint shouldn't be needed, as we would like
3372 // to allow for key frame setting (forced or periodic) defined per
3373 // spatial layer. For now, keep this in.
3374 cm->frame_type = low_res_frame_info->frame_type;
3376 // Check if lower resolution is available for motion vector reuse.
3377 if (cm->frame_type != KEY_FRAME) {
3378 cpi->mr_low_res_mv_avail = 1;
3379 cpi->mr_low_res_mv_avail &= !(low_res_frame_info->is_frame_dropped);
3381 if (cpi->ref_frame_flags & VP8_LAST_FRAME)
3382 cpi->mr_low_res_mv_avail &=
3383 (cpi->current_ref_frames[LAST_FRAME] ==
3384 low_res_frame_info->low_res_ref_frames[LAST_FRAME]);
3386 if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
3387 cpi->mr_low_res_mv_avail &=
3388 (cpi->current_ref_frames[GOLDEN_FRAME] ==
3389 low_res_frame_info->low_res_ref_frames[GOLDEN_FRAME]);
3391 // Don't use altref to determine whether low res is available.
3392 // TODO (marpan): Should we make this type of condition on a
3393 // per-reference frame basis?
3395 if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
3396 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[ALTREF_FRAME]
3397 == low_res_frame_info->low_res_ref_frames[ALTREF_FRAME]);
3402 // On a key frame: For the lowest resolution, keep track of the key frame
3403 // counter value. For the higher resolutions, reset the current video
3404 // frame counter to that of the lowest resolution.
3405 // This is done to the handle the case where we may stop/start encoding
3406 // higher layer(s). The restart-encoding of higher layer is only signaled
3407 // by a key frame for now.
3408 // TODO (marpan): Add flag to indicate restart-encoding of higher layer.
3409 if (cm->frame_type == KEY_FRAME) {
3410 if (cpi->oxcf.mr_encoder_id) {
3411 // If the initial starting value of the buffer level is zero (this can
3412 // happen because we may have not started encoding this higher stream),
3413 // then reset it to non-zero value based on |starting_buffer_level|.
3414 if (cpi->common.current_video_frame == 0 && cpi->buffer_level == 0) {
3416 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
3417 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
3418 for (i = 0; i < cpi->oxcf.number_of_layers; ++i) {
3419 LAYER_CONTEXT *lc = &cpi->layer_context[i];
3420 lc->bits_off_target = lc->starting_buffer_level;
3421 lc->buffer_level = lc->starting_buffer_level;
3424 cpi->common.current_video_frame =
3425 low_res_frame_info->key_frame_counter_value;
3427 low_res_frame_info->key_frame_counter_value =
3428 cpi->common.current_video_frame;
3434 // Find the reference frame closest to the current frame.
3435 cpi->closest_reference_frame = LAST_FRAME;
3436 if (cm->frame_type != KEY_FRAME) {
3438 MV_REFERENCE_FRAME closest_ref = INTRA_FRAME;
3439 if (cpi->ref_frame_flags & VP8_LAST_FRAME) {
3440 closest_ref = LAST_FRAME;
3441 } else if (cpi->ref_frame_flags & VP8_GOLD_FRAME) {
3442 closest_ref = GOLDEN_FRAME;
3443 } else if (cpi->ref_frame_flags & VP8_ALTR_FRAME) {
3444 closest_ref = ALTREF_FRAME;
3446 for (i = 1; i <= 3; ++i) {
3447 vpx_ref_frame_type_t ref_frame_type =
3448 (vpx_ref_frame_type_t)((i == 3) ? 4 : i);
3449 if (cpi->ref_frame_flags & ref_frame_type) {
3450 if ((cm->current_video_frame - cpi->current_ref_frames[i]) <
3451 (cm->current_video_frame - cpi->current_ref_frames[closest_ref])) {
3456 cpi->closest_reference_frame = closest_ref;
3459 /* Set various flags etc to special state if it is a key frame */
3460 if (cm->frame_type == KEY_FRAME) {
3463 // Set the loop filter deltas and segmentation map update
3464 setup_features(cpi);
3466 /* The alternate reference frame cannot be active for a key frame */
3467 cpi->source_alt_ref_active = 0;
3469 /* Reset the RD threshold multipliers to default of * 1 (128) */
3470 for (i = 0; i < MAX_MODES; ++i) {
3471 cpi->mb.rd_thresh_mult[i] = 128;
3474 // Reset the zero_last counter to 0 on key frame.
3475 memset(cpi->consec_zero_last, 0, cm->mb_rows * cm->mb_cols);
3476 memset(cpi->consec_zero_last_mvbias, 0,
3477 (cpi->common.mb_rows * cpi->common.mb_cols));
3481 /* Experimental code for lagged compress and one pass
3482 * Initialise one_pass GF frames stats
3483 * Update stats used for GF selection
3486 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3488 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3489 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3490 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3491 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3492 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3493 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3494 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3495 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3496 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3500 update_rd_ref_frame_probs(cpi);
3502 if (cpi->drop_frames_allowed) {
3503 /* The reset to decimation 0 is only done here for one pass.
3504 * Once it is set two pass leaves decimation on till the next kf.
3506 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0)) {
3507 cpi->decimation_factor--;
3510 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0) {
3511 cpi->decimation_factor = 1;
3513 } else if (cpi->buffer_level < drop_mark25 &&
3514 (cpi->decimation_factor == 2 || cpi->decimation_factor == 3)) {
3515 cpi->decimation_factor = 3;
3516 } else if (cpi->buffer_level < drop_mark50 &&
3517 (cpi->decimation_factor == 1 || cpi->decimation_factor == 2)) {
3518 cpi->decimation_factor = 2;
3519 } else if (cpi->buffer_level < drop_mark75 &&
3520 (cpi->decimation_factor == 0 || cpi->decimation_factor == 1)) {
3521 cpi->decimation_factor = 1;
3525 /* The following decimates the frame rate according to a regular
3526 * pattern (i.e. to 1/2 or 2/3 frame rate) This can be used to help
3527 * prevent buffer under-run in CBR mode. Alternatively it might be
3528 * desirable in some situations to drop frame rate but throw more bits
3531 * Note that dropping a key frame can be problematic if spatial
3532 * resampling is also active
3534 if (cpi->decimation_factor > 0) {
3535 switch (cpi->decimation_factor) {
3537 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3540 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3543 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3547 /* Note that we should not throw out a key frame (especially when
3548 * spatial resampling is enabled).
3550 if (cm->frame_type == KEY_FRAME) {
3551 cpi->decimation_count = cpi->decimation_factor;
3552 } else if (cpi->decimation_count > 0) {
3553 cpi->decimation_count--;
3555 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3556 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
3557 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3560 #if CONFIG_MULTI_RES_ENCODING
3561 vp8_store_drop_frame_info(cpi);
3564 cm->current_video_frame++;
3565 cpi->frames_since_key++;
3566 // We advance the temporal pattern for dropped frames.
3567 cpi->temporal_pattern_counter++;
3569 #if CONFIG_INTERNAL_STATS
3573 cpi->buffer_level = cpi->bits_off_target;
3575 if (cpi->oxcf.number_of_layers > 1) {
3578 /* Propagate bits saved by dropping the frame to higher
3581 for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
3582 LAYER_CONTEXT *lc = &cpi->layer_context[i];
3583 lc->bits_off_target += (int)(lc->target_bandwidth / lc->framerate);
3584 if (lc->bits_off_target > lc->maximum_buffer_size) {
3585 lc->bits_off_target = lc->maximum_buffer_size;
3587 lc->buffer_level = lc->bits_off_target;
3593 cpi->decimation_count = cpi->decimation_factor;
3596 cpi->decimation_count = 0;
3599 /* Decide how big to make the frame */
3600 if (!vp8_pick_frame_size(cpi)) {
3601 /*TODO: 2 drop_frame and return code could be put together. */
3602 #if CONFIG_MULTI_RES_ENCODING
3603 vp8_store_drop_frame_info(cpi);
3605 cm->current_video_frame++;
3606 cpi->frames_since_key++;
3607 // We advance the temporal pattern for dropped frames.
3608 cpi->temporal_pattern_counter++;
3612 /* Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3613 * This has a knock on effect on active best quality as well.
3614 * For CBR if the buffer reaches its maximum level then we can no longer
3615 * save up bits for later frames so we might as well use them up
3616 * on the current frame.
3618 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3619 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) &&
3620 cpi->buffered_mode) {
3621 /* Max adjustment is 1/4 */
3622 int Adjustment = cpi->active_worst_quality / 4;
3627 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size) {
3628 buff_lvl_step = (int)((cpi->oxcf.maximum_buffer_size -
3629 cpi->oxcf.optimal_buffer_level) /
3632 if (buff_lvl_step) {
3634 (int)((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) /
3641 cpi->active_worst_quality -= Adjustment;
3643 if (cpi->active_worst_quality < cpi->active_best_quality) {
3644 cpi->active_worst_quality = cpi->active_best_quality;
3649 /* Set an active best quality and if necessary active worst quality
3650 * There is some odd behavior for one pass here that needs attention.
3652 if ((cpi->pass == 2) || (cpi->ni_frames > 150)) {
3653 vpx_clear_system_state();
3655 Q = cpi->active_worst_quality;
3657 if (cm->frame_type == KEY_FRAME) {
3658 if (cpi->pass == 2) {
3659 if (cpi->gfu_boost > 600) {
3660 cpi->active_best_quality = kf_low_motion_minq[Q];
3662 cpi->active_best_quality = kf_high_motion_minq[Q];
3665 /* Special case for key frames forced because we have reached
3666 * the maximum key frame interval. Here force the Q to a range
3667 * based on the ambient Q to reduce the risk of popping
3669 if (cpi->this_key_frame_forced) {
3670 if (cpi->active_best_quality > cpi->avg_frame_qindex * 7 / 8) {
3671 cpi->active_best_quality = cpi->avg_frame_qindex * 7 / 8;
3672 } else if (cpi->active_best_quality<cpi->avg_frame_qindex>> 2) {
3673 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3677 /* One pass more conservative */
3679 cpi->active_best_quality = kf_high_motion_minq[Q];
3683 else if (cpi->oxcf.number_of_layers == 1 &&
3684 (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)) {
3685 /* Use the lower of cpi->active_worst_quality and recent
3686 * average Q as basis for GF/ARF Q limit unless last frame was
3689 if ((cpi->frames_since_key > 1) &&
3690 (cpi->avg_frame_qindex < cpi->active_worst_quality)) {
3691 Q = cpi->avg_frame_qindex;
3694 /* For constrained quality dont allow Q less than the cq level */
3695 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3696 (Q < cpi->cq_target_quality)) {
3697 Q = cpi->cq_target_quality;
3700 if (cpi->pass == 2) {
3701 if (cpi->gfu_boost > 1000) {
3702 cpi->active_best_quality = gf_low_motion_minq[Q];
3703 } else if (cpi->gfu_boost < 400) {
3704 cpi->active_best_quality = gf_high_motion_minq[Q];
3706 cpi->active_best_quality = gf_mid_motion_minq[Q];
3709 /* Constrained quality use slightly lower active best. */
3710 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
3711 cpi->active_best_quality = cpi->active_best_quality * 15 / 16;
3714 /* One pass more conservative */
3716 cpi->active_best_quality = gf_high_motion_minq[Q];
3719 cpi->active_best_quality = inter_minq[Q];
3721 /* For the constant/constrained quality mode we dont want
3722 * q to fall below the cq level.
3724 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3725 (cpi->active_best_quality < cpi->cq_target_quality)) {
3726 /* If we are strongly undershooting the target rate in the last
3727 * frames then use the user passed in cq value not the auto
3730 if (cpi->rolling_actual_bits < cpi->min_frame_bandwidth) {
3731 cpi->active_best_quality = cpi->oxcf.cq_level;
3733 cpi->active_best_quality = cpi->cq_target_quality;
3738 /* If CBR and the buffer is as full then it is reasonable to allow
3739 * higher quality on the frames to prevent bits just going to waste.
3741 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3742 /* Note that the use of >= here elliminates the risk of a devide
3743 * by 0 error in the else if clause
3745 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size) {
3746 cpi->active_best_quality = cpi->best_quality;
3748 } else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level) {
3750 (int)(((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) /
3751 (cpi->oxcf.maximum_buffer_size -
3752 cpi->oxcf.optimal_buffer_level));
3753 int min_qadjustment =
3754 ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3756 cpi->active_best_quality -= min_qadjustment;
3760 /* Make sure constrained quality mode limits are adhered to for the first
3761 * few frames of one pass encodes
3763 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
3764 if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame ||
3765 cpi->common.refresh_alt_ref_frame) {
3766 cpi->active_best_quality = cpi->best_quality;
3767 } else if (cpi->active_best_quality < cpi->cq_target_quality) {
3768 cpi->active_best_quality = cpi->cq_target_quality;
3772 /* Clip the active best and worst quality values to limits */
3773 if (cpi->active_worst_quality > cpi->worst_quality) {
3774 cpi->active_worst_quality = cpi->worst_quality;
3777 if (cpi->active_best_quality < cpi->best_quality) {
3778 cpi->active_best_quality = cpi->best_quality;
3781 if (cpi->active_worst_quality < cpi->active_best_quality) {
3782 cpi->active_worst_quality = cpi->active_best_quality;
3785 /* Determine initial Q to try */
3786 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3788 #if !CONFIG_REALTIME_ONLY
3790 /* Set highest allowed value for Zbin over quant */
3791 if (cm->frame_type == KEY_FRAME) {
3793 } else if ((cpi->oxcf.number_of_layers == 1) &&
3794 ((cm->refresh_alt_ref_frame ||
3795 (cm->refresh_golden_frame && !cpi->source_alt_ref_active)))) {
3798 zbin_oq_high = ZBIN_OQ_MAX;
3802 /* Setup background Q adjustment for error resilient mode.
3803 * For multi-layer encodes only enable this for the base layer.
3805 if (cpi->cyclic_refresh_mode_enabled) {
3806 // Special case for screen_content_mode with golden frame updates.
3808 (cpi->oxcf.screen_content_mode == 2 && cm->refresh_golden_frame);
3809 if (cpi->current_layer == 0 && cpi->force_maxqp == 0 && !disable_cr_gf) {
3810 cyclic_background_refresh(cpi, Q, 0);
3812 disable_segmentation(cpi);
3816 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit,
3817 &frame_over_shoot_limit);
3819 #if !CONFIG_REALTIME_ONLY
3820 /* Limit Q range for the adaptive loop. */
3821 bottom_index = cpi->active_best_quality;
3822 top_index = cpi->active_worst_quality;
3823 q_low = cpi->active_best_quality;
3824 q_high = cpi->active_worst_quality;
3827 vp8_save_coding_context(cpi);
3831 scale_and_extend_source(cpi->un_scaled_source, cpi);
3833 #if CONFIG_TEMPORAL_DENOISING && CONFIG_POSTPROC
3834 // Option to apply spatial blur under the aggressive or adaptive
3835 // (temporal denoising) mode.
3836 if (cpi->oxcf.noise_sensitivity >= 3) {
3837 if (cpi->denoiser.denoise_pars.spatial_blur != 0) {
3838 vp8_de_noise(cm, cpi->Source, cpi->Source,
3839 cpi->denoiser.denoise_pars.spatial_blur, 1, 0, 0);
3844 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC && !(CONFIG_TEMPORAL_DENOISING)
3846 if (cpi->oxcf.noise_sensitivity > 0) {
3850 switch (cpi->oxcf.noise_sensitivity) {
3851 case 1: l = 20; break;
3852 case 2: l = 40; break;
3853 case 3: l = 60; break;
3854 case 4: l = 80; break;
3855 case 5: l = 100; break;
3856 case 6: l = 150; break;
3859 if (cm->frame_type == KEY_FRAME) {
3860 vp8_de_noise(cm, cpi->Source, cpi->Source, l, 1, 0, 1);
3862 vp8_de_noise(cm, cpi->Source, cpi->Source, l, 1, 0, 1);
3864 src = cpi->Source->y_buffer;
3866 if (cpi->Source->y_stride < 0) {
3867 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3874 #ifdef OUTPUT_YUV_SRC
3875 vp8_write_yuv_frame(yuv_file, cpi->Source);
3879 vpx_clear_system_state();
3881 vp8_set_quantizer(cpi, Q);
3883 /* setup skip prob for costing in mode/mv decision */
3884 if (cpi->common.mb_no_coeff_skip) {
3885 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3887 if (cm->frame_type != KEY_FRAME) {
3888 if (cpi->common.refresh_alt_ref_frame) {
3889 if (cpi->last_skip_false_probs[2] != 0) {
3890 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3894 if(cpi->last_skip_false_probs[2]!=0 && abs(Q-
3895 cpi->last_skip_probs_q[2])<=16 )
3896 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3897 else if (cpi->last_skip_false_probs[2]!=0)
3898 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] +
3899 cpi->prob_skip_false ) / 2;
3901 } else if (cpi->common.refresh_golden_frame) {
3902 if (cpi->last_skip_false_probs[1] != 0) {
3903 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3907 if(cpi->last_skip_false_probs[1]!=0 && abs(Q-
3908 cpi->last_skip_probs_q[1])<=16 )
3909 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3910 else if (cpi->last_skip_false_probs[1]!=0)
3911 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] +
3912 cpi->prob_skip_false ) / 2;
3915 if (cpi->last_skip_false_probs[0] != 0) {
3916 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3920 if(cpi->last_skip_false_probs[0]!=0 && abs(Q-
3921 cpi->last_skip_probs_q[0])<=16 )
3922 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3923 else if(cpi->last_skip_false_probs[0]!=0)
3924 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] +
3925 cpi->prob_skip_false ) / 2;
3929 /* as this is for cost estimate, let's make sure it does not
3930 * go extreme eitehr way
3932 if (cpi->prob_skip_false < 5) cpi->prob_skip_false = 5;
3934 if (cpi->prob_skip_false > 250) cpi->prob_skip_false = 250;
3936 if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref) {
3937 cpi->prob_skip_false = 1;
3945 FILE *f = fopen("skip.stt", "a");
3946 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3953 if (cm->frame_type == KEY_FRAME) {
3954 if (resize_key_frame(cpi)) {
3955 /* If the frame size has changed, need to reset Q, quantizer,
3956 * and background refresh.
3958 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3959 if (cpi->cyclic_refresh_mode_enabled) {
3960 if (cpi->current_layer == 0) {
3961 cyclic_background_refresh(cpi, Q, 0);
3963 disable_segmentation(cpi);
3966 // Reset the zero_last counter to 0 on key frame.
3967 memset(cpi->consec_zero_last, 0, cm->mb_rows * cm->mb_cols);
3968 memset(cpi->consec_zero_last_mvbias, 0,
3969 (cpi->common.mb_rows * cpi->common.mb_cols));
3970 vp8_set_quantizer(cpi, Q);
3973 vp8_setup_key_frame(cpi);
3976 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
3978 if (cpi->oxcf.error_resilient_mode) cm->refresh_entropy_probs = 0;
3980 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
3981 if (cm->frame_type == KEY_FRAME) cm->refresh_entropy_probs = 1;
3984 if (cm->refresh_entropy_probs == 0) {
3985 /* save a copy for later refresh */
3986 memcpy(&cm->lfc, &cm->fc, sizeof(cm->fc));
3989 vp8_update_coef_context(cpi);
3991 vp8_update_coef_probs(cpi);
3993 /* transform / motion compensation build reconstruction frame
3994 * +pack coef partitions
3996 vp8_encode_frame(cpi);
3998 /* cpi->projected_frame_size is not needed for RT mode */
4001 /* transform / motion compensation build reconstruction frame */
4002 vp8_encode_frame(cpi);
4003 if (cpi->oxcf.screen_content_mode == 2) {
4004 if (vp8_drop_encodedframe_overshoot(cpi, Q)) return;
4007 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
4008 cpi->projected_frame_size =
4009 (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
4011 vpx_clear_system_state();
4013 /* Test to see if the stats generated for this frame indicate that
4014 * we should have coded a key frame (assuming that we didn't)!
4017 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME &&
4018 cpi->compressor_speed != 2) {
4019 #if !CONFIG_REALTIME_ONLY
4020 if (decide_key_frame(cpi)) {
4021 /* Reset all our sizing numbers and recode */
4022 cm->frame_type = KEY_FRAME;
4024 vp8_pick_frame_size(cpi);
4026 /* Clear the Alt reference frame active flag when we have
4029 cpi->source_alt_ref_active = 0;
4031 // Set the loop filter deltas and segmentation map update
4032 setup_features(cpi);
4034 vp8_restore_coding_context(cpi);
4036 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4038 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit,
4039 &frame_over_shoot_limit);
4041 /* Limit Q range for the adaptive loop. */
4042 bottom_index = cpi->active_best_quality;
4043 top_index = cpi->active_worst_quality;
4044 q_low = cpi->active_best_quality;
4045 q_high = cpi->active_worst_quality;
4055 vpx_clear_system_state();
4057 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4059 /* Are we are overshooting and up against the limit of active max Q. */
4060 if (((cpi->pass != 2) ||
4061 (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4062 (Q == cpi->active_worst_quality) &&
4063 (cpi->active_worst_quality < cpi->worst_quality) &&
4064 (cpi->projected_frame_size > frame_over_shoot_limit)) {
4065 int over_size_percent =
4066 ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) /
4067 frame_over_shoot_limit;
4069 /* If so is there any scope for relaxing it */
4070 while ((cpi->active_worst_quality < cpi->worst_quality) &&
4071 (over_size_percent > 0)) {
4072 cpi->active_worst_quality++;
4073 /* Assume 1 qstep = about 4% on frame size. */
4074 over_size_percent = (int)(over_size_percent * 0.96);
4076 #if !CONFIG_REALTIME_ONLY
4077 top_index = cpi->active_worst_quality;
4078 #endif // !CONFIG_REALTIME_ONLY
4079 /* If we have updated the active max Q do not call
4080 * vp8_update_rate_correction_factors() this loop.
4082 active_worst_qchanged = 1;
4084 active_worst_qchanged = 0;
4087 #if CONFIG_REALTIME_ONLY
4090 /* Special case handling for forced key frames */
4091 if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) {
4093 int kf_err = vp8_calc_ss_err(cpi->Source, &cm->yv12_fb[cm->new_fb_idx]);
4095 /* The key frame is not good enough */
4096 if (kf_err > ((cpi->ambient_err * 7) >> 3)) {
4098 q_high = (Q > q_low) ? (Q - 1) : q_low;
4101 Q = (q_high + q_low) >> 1;
4103 /* The key frame is much better than the previous frame */
4104 else if (kf_err < (cpi->ambient_err >> 1)) {
4106 q_low = (Q < q_high) ? (Q + 1) : q_high;
4109 Q = (q_high + q_low + 1) >> 1;
4112 /* Clamp Q to upper and lower limits: */
4115 } else if (Q < q_low) {
4122 /* Is the projected frame size out of range and are we allowed
4123 * to attempt to recode.
4125 else if (recode_loop_test(cpi, frame_over_shoot_limit,
4126 frame_under_shoot_limit, Q, top_index,
4131 /* Frame size out of permitted range. Update correction factor
4132 * & compute new Q to try...
4135 /* Frame is too large */
4136 if (cpi->projected_frame_size > cpi->this_frame_target) {
4137 /* Raise Qlow as to at least the current value */
4138 q_low = (Q < q_high) ? (Q + 1) : q_high;
4140 /* If we are using over quant do the same for zbin_oq_low */
4141 if (cpi->mb.zbin_over_quant > 0) {
4142 zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high)
4143 ? (cpi->mb.zbin_over_quant + 1)
4147 if (undershoot_seen) {
4148 /* Update rate_correction_factor unless
4149 * cpi->active_worst_quality has changed.
4151 if (!active_worst_qchanged) {
4152 vp8_update_rate_correction_factors(cpi, 1);
4155 Q = (q_high + q_low + 1) / 2;
4157 /* Adjust cpi->zbin_over_quant (only allowed when Q
4161 cpi->mb.zbin_over_quant = 0;
4163 zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high)
4164 ? (cpi->mb.zbin_over_quant + 1)
4166 cpi->mb.zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4169 /* Update rate_correction_factor unless
4170 * cpi->active_worst_quality has changed.
4172 if (!active_worst_qchanged) {
4173 vp8_update_rate_correction_factors(cpi, 0);
4176 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4178 while (((Q < q_low) || (cpi->mb.zbin_over_quant < zbin_oq_low)) &&
4180 vp8_update_rate_correction_factors(cpi, 0);
4181 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4188 /* Frame is too small */
4190 if (cpi->mb.zbin_over_quant == 0) {
4191 /* Lower q_high if not using over quant */
4192 q_high = (Q > q_low) ? (Q - 1) : q_low;
4194 /* else lower zbin_oq_high */
4195 zbin_oq_high = (cpi->mb.zbin_over_quant > zbin_oq_low)
4196 ? (cpi->mb.zbin_over_quant - 1)
4200 if (overshoot_seen) {
4201 /* Update rate_correction_factor unless
4202 * cpi->active_worst_quality has changed.
4204 if (!active_worst_qchanged) {
4205 vp8_update_rate_correction_factors(cpi, 1);
4208 Q = (q_high + q_low) / 2;
4210 /* Adjust cpi->zbin_over_quant (only allowed when Q
4214 cpi->mb.zbin_over_quant = 0;
4216 cpi->mb.zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4219 /* Update rate_correction_factor unless
4220 * cpi->active_worst_quality has changed.
4222 if (!active_worst_qchanged) {
4223 vp8_update_rate_correction_factors(cpi, 0);
4226 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4228 /* Special case reset for qlow for constrained quality.
4229 * This should only trigger where there is very substantial
4230 * undershoot on a frame and the auto cq level is above
4231 * the user passsed in value.
4233 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4238 while (((Q > q_high) || (cpi->mb.zbin_over_quant > zbin_oq_high)) &&
4240 vp8_update_rate_correction_factors(cpi, 0);
4241 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4246 undershoot_seen = 1;
4249 /* Clamp Q to upper and lower limits: */
4252 } else if (Q < q_low) {
4256 /* Clamp cpi->zbin_over_quant */
4257 cpi->mb.zbin_over_quant = (cpi->mb.zbin_over_quant < zbin_oq_low)
4259 : (cpi->mb.zbin_over_quant > zbin_oq_high)
4261 : cpi->mb.zbin_over_quant;
4267 #endif // CONFIG_REALTIME_ONLY
4269 if (cpi->is_src_frame_alt_ref) Loop = 0;
4272 vp8_restore_coding_context(cpi);
4274 #if CONFIG_INTERNAL_STATS
4275 cpi->tot_recode_hits++;
4278 } while (Loop == 1);
4280 #if defined(DROP_UNCODED_FRAMES)
4281 /* if there are no coded macroblocks at all drop this frame */
4282 if (cpi->common.MBs == cpi->mb.skip_true_count &&
4283 (cpi->drop_frame_count & 7) != 7 && cm->frame_type != KEY_FRAME) {
4284 cpi->common.current_video_frame++;
4285 cpi->frames_since_key++;
4286 cpi->drop_frame_count++;
4287 // We advance the temporal pattern for dropped frames.
4288 cpi->temporal_pattern_counter++;
4291 cpi->drop_frame_count = 0;
4295 /* Experimental code for lagged and one pass
4296 * Update stats used for one pass GF selection
4299 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4300 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4301 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4305 /* Special case code to reduce pulsing when key frames are forced at a
4306 * fixed interval. Note the reconstruction error if it is the frame before
4307 * the force key frame
4309 if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) {
4311 vp8_calc_ss_err(cpi->Source, &cm->yv12_fb[cm->new_fb_idx]);
4314 /* This frame's MVs are saved and will be used in next frame's MV predictor.
4315 * Last frame has one more line(add to bottom) and one more column(add to
4316 * right) than cm->mip. The edge elements are initialized to 0.
4318 #if CONFIG_MULTI_RES_ENCODING
4319 if (!cpi->oxcf.mr_encoder_id && cm->show_frame)
4321 if (cm->show_frame) /* do not save for altref frame */
4326 /* Point to beginning of allocated MODE_INFO arrays. */
4327 MODE_INFO *tmp = cm->mip;
4329 if (cm->frame_type != KEY_FRAME) {
4330 for (mb_row = 0; mb_row < cm->mb_rows + 1; ++mb_row) {
4331 for (mb_col = 0; mb_col < cm->mb_cols + 1; ++mb_col) {
4332 if (tmp->mbmi.ref_frame != INTRA_FRAME) {
4333 cpi->lfmv[mb_col + mb_row * (cm->mode_info_stride + 1)].as_int =
4334 tmp->mbmi.mv.as_int;
4337 cpi->lf_ref_frame_sign_bias[mb_col +
4338 mb_row * (cm->mode_info_stride + 1)] =
4339 cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4340 cpi->lf_ref_frame[mb_col + mb_row * (cm->mode_info_stride + 1)] =
4341 tmp->mbmi.ref_frame;
4348 /* Count last ref frame 0,0 usage on current encoded frame. */
4352 /* Point to beginning of MODE_INFO arrays. */
4353 MODE_INFO *tmp = cm->mi;
4355 cpi->zeromv_count = 0;
4357 if (cm->frame_type != KEY_FRAME) {
4358 for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
4359 for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
4360 if (tmp->mbmi.mode == ZEROMV && tmp->mbmi.ref_frame == LAST_FRAME) {
4361 cpi->zeromv_count++;
4370 #if CONFIG_MULTI_RES_ENCODING
4371 vp8_cal_dissimilarity(cpi);
4374 /* Update the GF useage maps.
4375 * This is done after completing the compression of a frame when all
4376 * modes etc. are finalized but before loop filter
4378 if (cpi->oxcf.number_of_layers == 1) {
4379 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4382 if (cm->frame_type == KEY_FRAME) cm->refresh_last_frame = 1;
4386 FILE *f = fopen("gfactive.stt", "a");
4387 fprintf(f, "%8d %8d %8d %8d %8d\n", cm->current_video_frame, (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols), cpi->this_iiratio, cpi->next_iiratio, cm->refresh_golden_frame);
4392 /* For inter frames the current default behavior is that when
4393 * cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4394 * This is purely an encoder decision at present.
4396 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame) {
4397 cm->copy_buffer_to_arf = 2;
4399 cm->copy_buffer_to_arf = 0;
4402 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4404 #if CONFIG_TEMPORAL_DENOISING
4405 // Get some measure of the amount of noise, by measuring the (partial) mse
4406 // between source and denoised buffer, for y channel. Partial refers to
4407 // computing the sse for a sub-sample of the frame (i.e., skip x blocks along
4409 // and only for blocks in that set that are consecutive ZEROMV_LAST mode.
4410 // Do this every ~8 frames, to further reduce complexity.
4411 // TODO(marpan): Keep this for now for the case cpi->oxcf.noise_sensitivity <
4413 // should be removed in favor of the process_denoiser_mode_change() function
4415 if (cpi->oxcf.noise_sensitivity > 0 && cpi->oxcf.noise_sensitivity < 4 &&
4416 !cpi->oxcf.screen_content_mode && cpi->frames_since_key % 8 == 0 &&
4417 cm->frame_type != KEY_FRAME) {
4418 cpi->mse_source_denoised = measure_square_diff_partial(
4419 &cpi->denoiser.yv12_running_avg[INTRA_FRAME], cpi->Source, cpi);
4422 // For the adaptive denoising mode (noise_sensitivity == 4), sample the mse
4423 // of source diff (between current and previous frame), and determine if we
4424 // should switch the denoiser mode. Sampling refers to computing the mse for
4425 // a sub-sample of the frame (i.e., skip x blocks along row/column), and
4426 // only for blocks in that set that have used ZEROMV LAST, along with some
4427 // constraint on the sum diff between blocks. This process is called every
4428 // ~8 frames, to further reduce complexity.
4429 if (cpi->oxcf.noise_sensitivity == 4 && !cpi->oxcf.screen_content_mode &&
4430 cpi->frames_since_key % 8 == 0 && cm->frame_type != KEY_FRAME) {
4431 process_denoiser_mode_change(cpi);
4435 #ifdef OUTPUT_YUV_SKINMAP
4436 if (cpi->common.current_video_frame > 1) {
4437 compute_skin_map(cpi, yuv_skinmap_file);
4441 #if CONFIG_MULTITHREAD
4442 if (cpi->b_multi_threaded) {
4443 /* start loopfilter in separate thread */
4444 sem_post(&cpi->h_event_start_lpf);
4445 cpi->b_lpf_running = 1;
4449 vp8_loopfilter_frame(cpi, cm);
4452 update_reference_frames(cpi);
4454 #ifdef OUTPUT_YUV_DENOISED
4455 vp8_write_yuv_frame(yuv_denoised_file,
4456 &cpi->denoiser.yv12_running_avg[INTRA_FRAME]);
4459 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
4460 if (cpi->oxcf.error_resilient_mode) {
4461 cm->refresh_entropy_probs = 0;
4465 #if CONFIG_MULTITHREAD
4466 /* wait that filter_level is picked so that we can continue with stream
4468 if (cpi->b_multi_threaded) sem_wait(&cpi->h_event_end_lpf);
4471 /* build the bitstream */
4472 vp8_pack_bitstream(cpi, dest, dest_end, size);
4474 /* Move storing frame_type out of the above loop since it is also
4475 * needed in motion search besides loopfilter */
4476 cm->last_frame_type = cm->frame_type;
4478 /* Update rate control heuristics */
4479 cpi->total_byte_count += (*size);
4480 cpi->projected_frame_size = (int)(*size) << 3;
4482 if (cpi->oxcf.number_of_layers > 1) {
4484 for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
4485 cpi->layer_context[i].total_byte_count += (*size);
4489 if (!active_worst_qchanged) vp8_update_rate_correction_factors(cpi, 2);
4491 cpi->last_q[cm->frame_type] = cm->base_qindex;
4493 if (cm->frame_type == KEY_FRAME) {
4494 vp8_adjust_key_frame_context(cpi);
4497 /* Keep a record of ambient average Q. */
4498 if (cm->frame_type != KEY_FRAME) {
4499 cpi->avg_frame_qindex =
4500 (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4503 /* Keep a record from which we can calculate the average Q excluding
4504 * GF updates and key frames
4506 if ((cm->frame_type != KEY_FRAME) &&
4507 ((cpi->oxcf.number_of_layers > 1) ||
4508 (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame))) {
4511 /* Calculate the average Q for normal inter frames (not key or GFU
4514 if (cpi->pass == 2) {
4515 cpi->ni_tot_qi += Q;
4516 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4518 /* Damp value for first few frames */
4519 if (cpi->ni_frames > 150) {
4520 cpi->ni_tot_qi += Q;
4521 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4523 /* For one pass, early in the clip ... average the current frame Q
4524 * value with the worstq entered by the user as a dampening measure
4527 cpi->ni_tot_qi += Q;
4529 ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4532 /* If the average Q is higher than what was used in the last
4533 * frame (after going through the recode loop to keep the frame
4534 * size within range) then use the last frame value - 1. The -1
4535 * is designed to stop Q and hence the data rate, from
4536 * progressively falling away during difficult sections, but at
4537 * the same time reduce the number of itterations around the
4540 if (Q > cpi->ni_av_qi) cpi->ni_av_qi = Q - 1;
4544 /* Update the buffer level variable. */
4545 /* Non-viewable frames are a special case and are treated as pure overhead. */
4546 if (!cm->show_frame) {
4547 cpi->bits_off_target -= cpi->projected_frame_size;
4549 cpi->bits_off_target +=
4550 cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4553 /* Clip the buffer level to the maximum specified buffer size */
4554 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
4555 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
4558 // If the frame dropper is not enabled, don't let the buffer level go below
4559 // some threshold, given here by -|maximum_buffer_size|. For now we only do
4560 // this for screen content input.
4561 if (cpi->drop_frames_allowed == 0 && cpi->oxcf.screen_content_mode &&
4562 cpi->bits_off_target < -cpi->oxcf.maximum_buffer_size) {
4563 cpi->bits_off_target = -cpi->oxcf.maximum_buffer_size;
4566 /* Rolling monitors of whether we are over or underspending used to
4567 * help regulate min and Max Q in two pass.
4569 cpi->rolling_target_bits =
4570 ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4571 cpi->rolling_actual_bits =
4572 ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4573 cpi->long_rolling_target_bits =
4574 ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4575 cpi->long_rolling_actual_bits =
4576 ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) /
4579 /* Actual bits spent */
4580 cpi->total_actual_bits += cpi->projected_frame_size;
4583 cpi->total_target_vs_actual +=
4584 (cpi->this_frame_target - cpi->projected_frame_size);
4586 cpi->buffer_level = cpi->bits_off_target;
4588 /* Propagate values to higher temporal layers */
4589 if (cpi->oxcf.number_of_layers > 1) {
4592 for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
4593 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4594 int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
4595 cpi->projected_frame_size);
4597 lc->bits_off_target += bits_off_for_this_layer;
4599 /* Clip buffer level to maximum buffer size for the layer */
4600 if (lc->bits_off_target > lc->maximum_buffer_size) {
4601 lc->bits_off_target = lc->maximum_buffer_size;
4604 lc->total_actual_bits += cpi->projected_frame_size;
4605 lc->total_target_vs_actual += bits_off_for_this_layer;
4606 lc->buffer_level = lc->bits_off_target;
4610 /* Update bits left to the kf and gf groups to account for overshoot
4611 * or undershoot on these frames
4613 if (cm->frame_type == KEY_FRAME) {
4614 cpi->twopass.kf_group_bits +=
4615 cpi->this_frame_target - cpi->projected_frame_size;
4617 if (cpi->twopass.kf_group_bits < 0) cpi->twopass.kf_group_bits = 0;
4618 } else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame) {
4619 cpi->twopass.gf_group_bits +=
4620 cpi->this_frame_target - cpi->projected_frame_size;
4622 if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
4625 if (cm->frame_type != KEY_FRAME) {
4626 if (cpi->common.refresh_alt_ref_frame) {
4627 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4628 cpi->last_skip_probs_q[2] = cm->base_qindex;
4629 } else if (cpi->common.refresh_golden_frame) {
4630 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4631 cpi->last_skip_probs_q[1] = cm->base_qindex;
4633 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4634 cpi->last_skip_probs_q[0] = cm->base_qindex;
4636 /* update the baseline */
4637 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4641 #if 0 && CONFIG_INTERNAL_STATS
4643 FILE *f = fopen("tmp.stt", "a");
4645 vpx_clear_system_state();
4647 if (cpi->twopass.total_left_stats.coded_error != 0.0)
4648 fprintf(f, "%10d %10d %10d %10d %10d %10"PRId64" %10"PRId64
4649 "%10"PRId64" %10d %6d %6d %6d %6d %5d %5d %5d %8d "
4650 "%8.2lf %"PRId64" %10.3lf %10"PRId64" %8d\n",
4651 cpi->common.current_video_frame, cpi->this_frame_target,
4652 cpi->projected_frame_size,
4653 (cpi->projected_frame_size - cpi->this_frame_target),
4654 cpi->total_target_vs_actual,
4656 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4657 cpi->total_actual_bits, cm->base_qindex,
4658 cpi->active_best_quality, cpi->active_worst_quality,
4659 cpi->ni_av_qi, cpi->cq_target_quality,
4660 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4661 cm->frame_type, cpi->gfu_boost,
4662 cpi->twopass.est_max_qcorrection_factor,
4663 cpi->twopass.bits_left,
4664 cpi->twopass.total_left_stats.coded_error,
4665 (double)cpi->twopass.bits_left /
4666 cpi->twopass.total_left_stats.coded_error,
4667 cpi->tot_recode_hits);
4669 fprintf(f, "%10d %10d %10d %10d %10d %10"PRId64" %10"PRId64
4670 "%10"PRId64" %10d %6d %6d %6d %6d %5d %5d %5d %8d "
4671 "%8.2lf %"PRId64" %10.3lf %8d\n",
4672 cpi->common.current_video_frame, cpi->this_frame_target,
4673 cpi->projected_frame_size,
4674 (cpi->projected_frame_size - cpi->this_frame_target),
4675 cpi->total_target_vs_actual,
4677 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4678 cpi->total_actual_bits, cm->base_qindex,
4679 cpi->active_best_quality, cpi->active_worst_quality,
4680 cpi->ni_av_qi, cpi->cq_target_quality,
4681 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4682 cm->frame_type, cpi->gfu_boost,
4683 cpi->twopass.est_max_qcorrection_factor,
4684 cpi->twopass.bits_left,
4685 cpi->twopass.total_left_stats.coded_error,
4686 cpi->tot_recode_hits);
4691 FILE *fmodes = fopen("Modes.stt", "a");
4693 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4694 cpi->common.current_video_frame,
4695 cm->frame_type, cm->refresh_golden_frame,
4696 cm->refresh_alt_ref_frame);
4698 fprintf(fmodes, "\n");
4706 if (cm->refresh_golden_frame == 1) {
4707 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4709 cm->frame_flags = cm->frame_flags & ~FRAMEFLAGS_GOLDEN;
4712 if (cm->refresh_alt_ref_frame == 1) {
4713 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4715 cm->frame_flags = cm->frame_flags & ~FRAMEFLAGS_ALTREF;
4718 if (cm->refresh_last_frame & cm->refresh_golden_frame) { /* both refreshed */
4719 cpi->gold_is_last = 1;
4720 } else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) {
4721 /* 1 refreshed but not the other */
4722 cpi->gold_is_last = 0;
4725 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) { /* both refreshed */
4726 cpi->alt_is_last = 1;
4727 } else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) {
4728 /* 1 refreshed but not the other */
4729 cpi->alt_is_last = 0;
4732 if (cm->refresh_alt_ref_frame &
4733 cm->refresh_golden_frame) { /* both refreshed */
4734 cpi->gold_is_alt = 1;
4735 } else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) {
4736 /* 1 refreshed but not the other */
4737 cpi->gold_is_alt = 0;
4740 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
4742 if (cpi->gold_is_last) cpi->ref_frame_flags &= ~VP8_GOLD_FRAME;
4744 if (cpi->alt_is_last) cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4746 if (cpi->gold_is_alt) cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4748 if (!cpi->oxcf.error_resilient_mode) {
4749 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame &&
4750 (cm->frame_type != KEY_FRAME)) {
4751 /* Update the alternate reference frame stats as appropriate. */
4752 update_alt_ref_frame_stats(cpi);
4754 /* Update the Golden frame stats as appropriate. */
4755 update_golden_frame_stats(cpi);
4759 if (cm->frame_type == KEY_FRAME) {
4760 /* Tell the caller that the frame was coded as a key frame */
4761 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4763 /* As this frame is a key frame the next defaults to an inter frame. */
4764 cm->frame_type = INTER_FRAME;
4766 cpi->last_frame_percent_intra = 100;
4768 *frame_flags = cm->frame_flags & ~FRAMEFLAGS_KEY;
4770 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4773 /* Clear the one shot update flags for segmentation map and mode/ref
4774 * loop filter deltas.
4776 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4777 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4778 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4780 /* Dont increment frame counters if this was an altref buffer update
4783 if (cm->show_frame) {
4784 cm->current_video_frame++;
4785 cpi->frames_since_key++;
4786 cpi->temporal_pattern_counter++;
4789 /* reset to normal state now that we are done. */
4795 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4796 recon_file = fopen(filename, "wb");
4797 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4798 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4804 /* vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show); */
4806 #if !CONFIG_REALTIME_ONLY
4807 static void Pass2Encode(VP8_COMP *cpi, size_t *size, unsigned char *dest,
4808 unsigned char *dest_end, unsigned int *frame_flags) {
4809 if (!cpi->common.refresh_alt_ref_frame) vp8_second_pass(cpi);
4811 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4812 cpi->twopass.bits_left -= 8 * (int)(*size);
4814 if (!cpi->common.refresh_alt_ref_frame) {
4815 double two_pass_min_rate =
4816 (double)(cpi->oxcf.target_bandwidth *
4817 cpi->oxcf.two_pass_vbrmin_section / 100);
4818 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->framerate);
4823 int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags,
4824 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
4826 struct vpx_usec_timer timer;
4829 vpx_usec_timer_start(&timer);
4831 /* Reinit the lookahead buffer if the frame size changes */
4832 if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height) {
4833 assert(cpi->oxcf.lag_in_frames < 2);
4834 dealloc_raw_frame_buffers(cpi);
4835 alloc_raw_frame_buffers(cpi);
4838 if (vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags,
4839 cpi->active_map_enabled ? cpi->active_map : NULL)) {
4842 vpx_usec_timer_mark(&timer);
4843 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4848 static int frame_is_reference(const VP8_COMP *cpi) {
4849 const VP8_COMMON *cm = &cpi->common;
4850 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4852 return cm->frame_type == KEY_FRAME || cm->refresh_last_frame ||
4853 cm->refresh_golden_frame || cm->refresh_alt_ref_frame ||
4854 cm->copy_buffer_to_gf || cm->copy_buffer_to_arf ||
4855 cm->refresh_entropy_probs || xd->mode_ref_lf_delta_update ||
4856 xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
4859 int vp8_get_compressed_data(VP8_COMP *cpi, unsigned int *frame_flags,
4860 size_t *size, unsigned char *dest,
4861 unsigned char *dest_end, int64_t *time_stamp,
4862 int64_t *time_end, int flush) {
4864 struct vpx_usec_timer tsctimer;
4865 struct vpx_usec_timer ticktimer;
4866 struct vpx_usec_timer cmptimer;
4867 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4869 if (!cpi) return -1;
4873 if (setjmp(cpi->common.error.jmp)) {
4874 cpi->common.error.setjmp = 0;
4875 vpx_clear_system_state();
4876 return VPX_CODEC_CORRUPT_FRAME;
4879 cpi->common.error.setjmp = 1;
4881 vpx_usec_timer_start(&cmptimer);
4885 #if !CONFIG_REALTIME_ONLY
4886 /* Should we code an alternate reference frame */
4887 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.play_alternate &&
4888 cpi->source_alt_ref_pending) {
4889 if ((cpi->source = vp8_lookahead_peek(
4890 cpi->lookahead, cpi->frames_till_gf_update_due, PEEK_FORWARD))) {
4891 cpi->alt_ref_source = cpi->source;
4892 if (cpi->oxcf.arnr_max_frames > 0) {
4893 vp8_temporal_filter_prepare_c(cpi, cpi->frames_till_gf_update_due);
4894 force_src_buffer = &cpi->alt_ref_buffer;
4896 cpi->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4897 cm->refresh_alt_ref_frame = 1;
4898 cm->refresh_golden_frame = 0;
4899 cm->refresh_last_frame = 0;
4901 /* Clear Pending alt Ref flag. */
4902 cpi->source_alt_ref_pending = 0;
4903 cpi->is_src_frame_alt_ref = 0;
4909 /* Read last frame source if we are encoding first pass. */
4910 if (cpi->pass == 1 && cm->current_video_frame > 0) {
4911 if ((cpi->last_source =
4912 vp8_lookahead_peek(cpi->lookahead, 1, PEEK_BACKWARD)) == NULL) {
4917 if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush))) {
4920 cpi->is_src_frame_alt_ref =
4921 cpi->alt_ref_source && (cpi->source == cpi->alt_ref_source);
4923 if (cpi->is_src_frame_alt_ref) cpi->alt_ref_source = NULL;
4928 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4929 cpi->un_scaled_source = cpi->Source;
4930 *time_stamp = cpi->source->ts_start;
4931 *time_end = cpi->source->ts_end;
4932 *frame_flags = cpi->source->flags;
4934 if (cpi->pass == 1 && cm->current_video_frame > 0) {
4935 cpi->last_frame_unscaled_source = &cpi->last_source->img;
4939 #if !CONFIG_REALTIME_ONLY
4941 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
4942 vp8_end_first_pass(cpi); /* get last stats packet */
4943 cpi->twopass.first_pass_done = 1;
4951 if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
4952 cpi->first_time_stamp_ever = cpi->source->ts_start;
4953 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
4956 /* adjust frame rates based on timestamps given */
4957 if (cm->show_frame) {
4958 int64_t this_duration;
4961 if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
4962 this_duration = cpi->source->ts_end - cpi->source->ts_start;
4965 int64_t last_duration;
4967 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
4968 last_duration = cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
4969 /* do a step update if the duration changes by 10% */
4970 if (last_duration) {
4971 step = (int)(((this_duration - last_duration) * 10 / last_duration));
4975 if (this_duration) {
4977 cpi->ref_framerate = 10000000.0 / this_duration;
4979 double avg_duration, interval;
4981 /* Average this frame's rate into the last second's average
4982 * frame rate. If we haven't seen 1 second yet, then average
4983 * over the whole interval seen.
4985 interval = (double)(cpi->source->ts_end - cpi->first_time_stamp_ever);
4986 if (interval > 10000000.0) interval = 10000000;
4988 avg_duration = 10000000.0 / cpi->ref_framerate;
4989 avg_duration *= (interval - avg_duration + this_duration);
4990 avg_duration /= interval;
4992 cpi->ref_framerate = 10000000.0 / avg_duration;
4994 #if CONFIG_MULTI_RES_ENCODING
4995 if (cpi->oxcf.mr_total_resolutions > 1) {
4996 LOWER_RES_FRAME_INFO *low_res_frame_info =
4997 (LOWER_RES_FRAME_INFO *)cpi->oxcf.mr_low_res_mode_info;
4998 // Frame rate should be the same for all spatial layers in
4999 // multi-res-encoding (simulcast), so we constrain the frame for
5000 // higher layers to be that of lowest resolution. This is needed
5001 // as he application may decide to skip encoding a high layer and
5002 // then start again, in which case a big jump in time-stamps will
5003 // be received for that high layer, which will yield an incorrect
5004 // frame rate (from time-stamp adjustment in above calculation).
5005 if (cpi->oxcf.mr_encoder_id) {
5006 cpi->ref_framerate = low_res_frame_info->low_res_framerate;
5008 // Keep track of frame rate for lowest resolution.
5009 low_res_frame_info->low_res_framerate = cpi->ref_framerate;
5013 if (cpi->oxcf.number_of_layers > 1) {
5016 /* Update frame rates for each layer */
5017 assert(cpi->oxcf.number_of_layers <= VPX_TS_MAX_LAYERS);
5018 for (i = 0; i < cpi->oxcf.number_of_layers && i < VPX_TS_MAX_LAYERS;
5020 LAYER_CONTEXT *lc = &cpi->layer_context[i];
5021 lc->framerate = cpi->ref_framerate / cpi->oxcf.rate_decimator[i];
5024 vp8_new_framerate(cpi, cpi->ref_framerate);
5028 cpi->last_time_stamp_seen = cpi->source->ts_start;
5029 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
5032 if (cpi->oxcf.number_of_layers > 1) {
5035 update_layer_contexts(cpi);
5037 /* Restore layer specific context & set frame rate */
5038 if (cpi->temporal_layer_id >= 0) {
5039 layer = cpi->temporal_layer_id;
5043 .layer_id[cpi->temporal_pattern_counter % cpi->oxcf.periodicity];
5045 restore_layer_context(cpi, layer);
5046 vp8_new_framerate(cpi, cpi->layer_context[layer].framerate);
5049 if (cpi->compressor_speed == 2) {
5050 vpx_usec_timer_start(&tsctimer);
5051 vpx_usec_timer_start(&ticktimer);
5054 cpi->lf_zeromv_pct = (cpi->zeromv_count * 100) / cm->MBs;
5056 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
5059 const int num_part = (1 << cm->multi_token_partition);
5060 /* the available bytes in dest */
5061 const unsigned long dest_size = dest_end - dest;
5062 const int tok_part_buff_size = (dest_size * 9) / (10 * num_part);
5064 unsigned char *dp = dest;
5066 cpi->partition_d[0] = dp;
5067 dp += dest_size / 10; /* reserve 1/10 for control partition */
5068 cpi->partition_d_end[0] = dp;
5070 for (i = 0; i < num_part; ++i) {
5071 cpi->partition_d[i + 1] = dp;
5072 dp += tok_part_buff_size;
5073 cpi->partition_d_end[i + 1] = dp;
5078 /* start with a 0 size frame */
5081 /* Clear down mmx registers */
5082 vpx_clear_system_state();
5084 cm->frame_type = INTER_FRAME;
5085 cm->frame_flags = *frame_flags;
5089 if (cm->refresh_alt_ref_frame)
5091 cm->refresh_golden_frame = 0;
5092 cm->refresh_last_frame = 0;
5096 cm->refresh_golden_frame = 0;
5097 cm->refresh_last_frame = 1;
5101 /* find a free buffer for the new frame */
5104 for (; i < NUM_YV12_BUFFERS; ++i) {
5105 if (!cm->yv12_fb[i].flags) {
5111 assert(i < NUM_YV12_BUFFERS);
5113 switch (cpi->pass) {
5114 #if !CONFIG_REALTIME_ONLY
5115 case 1: Pass1Encode(cpi, size, dest, frame_flags); break;
5116 case 2: Pass2Encode(cpi, size, dest, dest_end, frame_flags); break;
5117 #endif // !CONFIG_REALTIME_ONLY
5119 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
5123 if (cpi->compressor_speed == 2) {
5124 unsigned int duration, duration2;
5125 vpx_usec_timer_mark(&tsctimer);
5126 vpx_usec_timer_mark(&ticktimer);
5128 duration = (int)(vpx_usec_timer_elapsed(&ticktimer));
5129 duration2 = (unsigned int)((double)duration / 2);
5131 if (cm->frame_type != KEY_FRAME) {
5132 if (cpi->avg_encode_time == 0) {
5133 cpi->avg_encode_time = duration;
5135 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5141 if (cpi->avg_pick_mode_time == 0) {
5142 cpi->avg_pick_mode_time = duration2;
5144 cpi->avg_pick_mode_time =
5145 (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5151 if (cm->refresh_entropy_probs == 0) {
5152 memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5155 /* Save the contexts separately for alt ref, gold and last. */
5156 /* (TODO jbb -> Optimize this with pointers to avoid extra copies. ) */
5157 if (cm->refresh_alt_ref_frame) memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc));
5159 if (cm->refresh_golden_frame) memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc));
5161 if (cm->refresh_last_frame) memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc));
5163 /* if its a dropped frame honor the requests on subsequent frames */
5165 cpi->droppable = !frame_is_reference(cpi);
5167 /* return to normal state */
5168 cm->refresh_entropy_probs = 1;
5169 cm->refresh_alt_ref_frame = 0;
5170 cm->refresh_golden_frame = 0;
5171 cm->refresh_last_frame = 1;
5172 cm->frame_type = INTER_FRAME;
5175 /* Save layer specific state */
5176 if (cpi->oxcf.number_of_layers > 1) save_layer_context(cpi);
5178 vpx_usec_timer_mark(&cmptimer);
5179 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5181 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame) {
5182 generate_psnr_packet(cpi);
5185 #if CONFIG_INTERNAL_STATS
5187 if (cpi->pass != 1) {
5188 cpi->bytes += *size;
5190 if (cm->show_frame) {
5191 cpi->common.show_frame_mi = cpi->common.mi;
5194 if (cpi->b_calculate_psnr) {
5195 uint64_t ye, ue, ve;
5197 YV12_BUFFER_CONFIG *orig = cpi->Source;
5198 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5199 unsigned int y_width = cpi->common.Width;
5200 unsigned int y_height = cpi->common.Height;
5201 unsigned int uv_width = (y_width + 1) / 2;
5202 unsigned int uv_height = (y_height + 1) / 2;
5203 int y_samples = y_height * y_width;
5204 int uv_samples = uv_height * uv_width;
5205 int t_samples = y_samples + 2 * uv_samples;
5208 ye = calc_plane_error(orig->y_buffer, orig->y_stride, recon->y_buffer,
5209 recon->y_stride, y_width, y_height);
5211 ue = calc_plane_error(orig->u_buffer, orig->uv_stride, recon->u_buffer,
5212 recon->uv_stride, uv_width, uv_height);
5214 ve = calc_plane_error(orig->v_buffer, orig->uv_stride, recon->v_buffer,
5215 recon->uv_stride, uv_width, uv_height);
5217 sq_error = (double)(ye + ue + ve);
5219 frame_psnr = vpx_sse_to_psnr(t_samples, 255.0, sq_error);
5221 cpi->total_y += vpx_sse_to_psnr(y_samples, 255.0, (double)ye);
5222 cpi->total_u += vpx_sse_to_psnr(uv_samples, 255.0, (double)ue);
5223 cpi->total_v += vpx_sse_to_psnr(uv_samples, 255.0, (double)ve);
5224 cpi->total_sq_error += sq_error;
5225 cpi->total += frame_psnr;
5228 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5230 double frame_psnr2, frame_ssim2 = 0;
5233 vp8_deblock(cm, cm->frame_to_show, &cm->post_proc_buffer,
5234 cm->filter_level * 10 / 6, 1, 0);
5235 vpx_clear_system_state();
5237 ye = calc_plane_error(orig->y_buffer, orig->y_stride, pp->y_buffer,
5238 pp->y_stride, y_width, y_height);
5240 ue = calc_plane_error(orig->u_buffer, orig->uv_stride, pp->u_buffer,
5241 pp->uv_stride, uv_width, uv_height);
5243 ve = calc_plane_error(orig->v_buffer, orig->uv_stride, pp->v_buffer,
5244 pp->uv_stride, uv_width, uv_height);
5246 sq_error2 = (double)(ye + ue + ve);
5248 frame_psnr2 = vpx_sse_to_psnr(t_samples, 255.0, sq_error2);
5250 cpi->totalp_y += vpx_sse_to_psnr(y_samples, 255.0, (double)ye);
5251 cpi->totalp_u += vpx_sse_to_psnr(uv_samples, 255.0, (double)ue);
5252 cpi->totalp_v += vpx_sse_to_psnr(uv_samples, 255.0, (double)ve);
5253 cpi->total_sq_error2 += sq_error2;
5254 cpi->totalp += frame_psnr2;
5257 vpx_calc_ssim(cpi->Source, &cm->post_proc_buffer, &weight);
5259 cpi->summed_quality += frame_ssim2 * weight;
5260 cpi->summed_weights += weight;
5262 if (cpi->oxcf.number_of_layers > 1) {
5265 for (i = cpi->current_layer; i < cpi->oxcf.number_of_layers; ++i) {
5266 cpi->frames_in_layer[i]++;
5268 cpi->bytes_in_layer[i] += *size;
5269 cpi->sum_psnr[i] += frame_psnr;
5270 cpi->sum_psnr_p[i] += frame_psnr2;
5271 cpi->total_error2[i] += sq_error;
5272 cpi->total_error2_p[i] += sq_error2;
5273 cpi->sum_ssim[i] += frame_ssim2 * weight;
5274 cpi->sum_weights[i] += weight;
5285 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5287 skiptruecount += cpi->skip_true_count;
5288 skipfalsecount += cpi->skip_false_count;
5296 FILE *f = fopen("skip.stt", "a");
5297 fprintf(f, "frame:%4d flags:%4x Q:%4d P:%4d Size:%5d\n", cpi->common.current_video_frame, *frame_flags, cpi->common.base_qindex, cpi->prob_skip_false, *size);
5299 if (cpi->is_src_frame_alt_ref == 1)
5300 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5308 cpi->common.error.setjmp = 0;
5310 #if CONFIG_MULTITHREAD
5311 /* wait for the lpf thread done */
5312 if (cpi->b_multi_threaded && cpi->b_lpf_running) {
5313 sem_wait(&cpi->h_event_end_lpf);
5314 cpi->b_lpf_running = 0;
5321 int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest,
5322 vp8_ppflags_t *flags) {
5323 if (cpi->common.refresh_alt_ref_frame) {
5329 cpi->common.show_frame_mi = cpi->common.mi;
5330 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5334 if (cpi->common.frame_to_show) {
5335 *dest = *cpi->common.frame_to_show;
5336 dest->y_width = cpi->common.Width;
5337 dest->y_height = cpi->common.Height;
5338 dest->uv_height = cpi->common.Height / 2;
5345 vpx_clear_system_state();
5350 int vp8_set_roimap(VP8_COMP *cpi, unsigned char *map, unsigned int rows,
5351 unsigned int cols, int delta_q[4], int delta_lf[4],
5352 unsigned int threshold[4]) {
5353 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5354 int internal_delta_q[MAX_MB_SEGMENTS];
5355 const int range = 63;
5358 // This method is currently incompatible with the cyclic refresh method
5359 if (cpi->cyclic_refresh_mode_enabled) return -1;
5361 // Check number of rows and columns match
5362 if (cpi->common.mb_rows != (int)rows || cpi->common.mb_cols != (int)cols) {
5366 // Range check the delta Q values and convert the external Q range values
5367 // to internal ones.
5368 if ((abs(delta_q[0]) > range) || (abs(delta_q[1]) > range) ||
5369 (abs(delta_q[2]) > range) || (abs(delta_q[3]) > range)) {
5373 // Range check the delta lf values
5374 if ((abs(delta_lf[0]) > range) || (abs(delta_lf[1]) > range) ||
5375 (abs(delta_lf[2]) > range) || (abs(delta_lf[3]) > range)) {
5380 disable_segmentation(cpi);
5384 // Translate the external delta q values to internal values.
5385 for (i = 0; i < MAX_MB_SEGMENTS; ++i) {
5386 internal_delta_q[i] =
5387 (delta_q[i] >= 0) ? q_trans[delta_q[i]] : -q_trans[-delta_q[i]];
5390 /* Set the segmentation Map */
5391 set_segmentation_map(cpi, map);
5393 /* Activate segmentation. */
5394 enable_segmentation(cpi);
5396 /* Set up the quant segment data */
5397 feature_data[MB_LVL_ALT_Q][0] = internal_delta_q[0];
5398 feature_data[MB_LVL_ALT_Q][1] = internal_delta_q[1];
5399 feature_data[MB_LVL_ALT_Q][2] = internal_delta_q[2];
5400 feature_data[MB_LVL_ALT_Q][3] = internal_delta_q[3];
5402 /* Set up the loop segment data s */
5403 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5404 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5405 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5406 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5408 cpi->segment_encode_breakout[0] = threshold[0];
5409 cpi->segment_encode_breakout[1] = threshold[1];
5410 cpi->segment_encode_breakout[2] = threshold[2];
5411 cpi->segment_encode_breakout[3] = threshold[3];
5413 /* Initialise the feature data structure */
5414 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5419 int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows,
5420 unsigned int cols) {
5421 if ((int)rows == cpi->common.mb_rows && (int)cols == cpi->common.mb_cols) {
5423 memcpy(cpi->active_map, map, rows * cols);
5424 cpi->active_map_enabled = 1;
5426 cpi->active_map_enabled = 0;
5435 int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING horiz_mode,
5436 VPX_SCALING vert_mode) {
5437 if (horiz_mode <= ONETWO) {
5438 cpi->common.horiz_scale = horiz_mode;
5443 if (vert_mode <= ONETWO) {
5444 cpi->common.vert_scale = vert_mode;
5452 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) {
5456 unsigned char *src = source->y_buffer;
5457 unsigned char *dst = dest->y_buffer;
5459 /* Loop through the Y plane raw and reconstruction data summing
5460 * (square differences)
5462 for (i = 0; i < source->y_height; i += 16) {
5463 for (j = 0; j < source->y_width; j += 16) {
5465 Total += vpx_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
5469 src += 16 * source->y_stride;
5470 dst += 16 * dest->y_stride;
5476 int vp8_get_quantizer(VP8_COMP *cpi) { return cpi->common.base_qindex; }