1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11
12 #include "vpx_config.h"
13 #include "./vpx_scale_rtcd.h"
14 #include "vp8/common/onyxc_int.h"
15 #include "vp8/common/blockd.h"
16 #include "onyx_int.h"
17 #include "vp8/common/systemdependent.h"
18 #include "quantize.h"
19 #include "vp8/common/alloccommon.h"
20 #include "mcomp.h"
21 #include "firstpass.h"
22 #include "vpx/internal/vpx_psnr.h"
23 #include "vpx_scale/vpx_scale.h"
24 #include "vp8/common/extend.h"
25 #include "ratectrl.h"
26 #include "vp8/common/quant_common.h"
27 #include "segmentation.h"
28 #if CONFIG_POSTPROC
29 #include "vp8/common/postproc.h"
30 #endif
31 #include "vpx_mem/vpx_mem.h"
32 #include "vp8/common/swapyv12buffer.h"
33 #include "vp8/common/threading.h"
34 #include "vpx_ports/vpx_timer.h"
35 #if ARCH_ARM
36 #include "vpx_ports/arm.h"
37 #endif
38 #if CONFIG_MULTI_RES_ENCODING
39 #include "mr_dissim.h"
40 #endif
41 #include "encodeframe.h"
42
43 #include <math.h>
44 #include <stdio.h>
45 #include <limits.h>
46
47 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
48 extern int vp8_update_coef_context(VP8_COMP *cpi);
49 extern void vp8_update_coef_probs(VP8_COMP *cpi);
50 #endif
51
52 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
53 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
54 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
55
56 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
57 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
58 extern unsigned int vp8_get_processor_freq();
59 extern void print_tree_update_probs();
60 extern int vp8cx_create_encoder_threads(VP8_COMP *cpi);
61 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
62
63 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
64
65 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest);
66
67 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
68
69 static void set_default_lf_deltas(VP8_COMP *cpi);
70
71 extern const int vp8_gf_interval_table[101];
72
73 #if CONFIG_INTERNAL_STATS
74 #include "math.h"
75
76 extern double vp8_calc_ssim
77 (
78 YV12_BUFFER_CONFIG *source,
79 YV12_BUFFER_CONFIG *dest,
80 int lumamask,
81 double *weight
82 );
83
84
85 extern double vp8_calc_ssimg
86 (
87 YV12_BUFFER_CONFIG *source,
88 YV12_BUFFER_CONFIG *dest,
89 double *ssim_y,
90 double *ssim_u,
91 double *ssim_v
92 );
93
94
95 #endif
96
97
98 #ifdef OUTPUT_YUV_SRC
99 FILE *yuv_file;
100 #endif
101 #ifdef OUTPUT_YUV_DENOISED
102 FILE *yuv_denoised_file;
103 #endif
104
105 #if 0
106 FILE *framepsnr;
107 FILE *kf_list;
108 FILE *keyfile;
109 #endif
110
111 #if 0
112 extern int skip_true_count;
113 extern int skip_false_count;
114 #endif
115
116
117 #ifdef VP8_ENTROPY_STATS
118 extern int intra_mode_stats[10][10][10];
119 #endif
120
121 #ifdef SPEEDSTATS
122 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
123 unsigned int tot_pm = 0;
124 unsigned int cnt_pm = 0;
125 unsigned int tot_ef = 0;
126 unsigned int cnt_ef = 0;
127 #endif
128
129 #ifdef MODE_STATS
130 extern unsigned __int64 Sectionbits[50];
131 extern int y_modes[5] ;
132 extern int uv_modes[4] ;
133 extern int b_modes[10] ;
134
135 extern int inter_y_modes[10] ;
136 extern int inter_uv_modes[4] ;
137 extern unsigned int inter_b_modes[15];
138 #endif
139
140 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
141
142 extern const int qrounding_factors[129];
143 extern const int qzbin_factors[129];
144 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
145 extern const int vp8cx_base_skip_false_prob[128];
146
147 /* Tables relating active max Q to active min Q */
148 static const unsigned char kf_low_motion_minq[QINDEX_RANGE] =
149 {
150 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
151 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
152 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
153 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
154 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
155 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
156 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
157 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
158 };
159 static const unsigned char kf_high_motion_minq[QINDEX_RANGE] =
160 {
161 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
162 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
163 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
164 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
165 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
166 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
167 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
168 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
169 };
170 static const unsigned char gf_low_motion_minq[QINDEX_RANGE] =
171 {
172 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
173 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
174 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
175 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
176 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
177 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
178 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
179 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
180 };
181 static const unsigned char gf_mid_motion_minq[QINDEX_RANGE] =
182 {
183 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
184 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
185 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
186 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
187 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
188 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
189 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
190 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
191 };
192 static const unsigned char gf_high_motion_minq[QINDEX_RANGE] =
193 {
194 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
195 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
196 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
197 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
198 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
199 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
200 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
201 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
202 };
203 static const unsigned char inter_minq[QINDEX_RANGE] =
204 {
205 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
206 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
207 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
208 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
209 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
210 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
211 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
212 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
213 };
214
215 #ifdef PACKET_TESTING
216 extern FILE *vpxlogc;
217 #endif
218
save_layer_context(VP8_COMP * cpi)219 static void save_layer_context(VP8_COMP *cpi)
220 {
221 LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];
222
223 /* Save layer dependent coding state */
224 lc->target_bandwidth = cpi->target_bandwidth;
225 lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
226 lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
227 lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
228 lc->starting_buffer_level_in_ms = cpi->oxcf.starting_buffer_level_in_ms;
229 lc->optimal_buffer_level_in_ms = cpi->oxcf.optimal_buffer_level_in_ms;
230 lc->maximum_buffer_size_in_ms = cpi->oxcf.maximum_buffer_size_in_ms;
231 lc->buffer_level = cpi->buffer_level;
232 lc->bits_off_target = cpi->bits_off_target;
233 lc->total_actual_bits = cpi->total_actual_bits;
234 lc->worst_quality = cpi->worst_quality;
235 lc->active_worst_quality = cpi->active_worst_quality;
236 lc->best_quality = cpi->best_quality;
237 lc->active_best_quality = cpi->active_best_quality;
238 lc->ni_av_qi = cpi->ni_av_qi;
239 lc->ni_tot_qi = cpi->ni_tot_qi;
240 lc->ni_frames = cpi->ni_frames;
241 lc->avg_frame_qindex = cpi->avg_frame_qindex;
242 lc->rate_correction_factor = cpi->rate_correction_factor;
243 lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor;
244 lc->gf_rate_correction_factor = cpi->gf_rate_correction_factor;
245 lc->zbin_over_quant = cpi->mb.zbin_over_quant;
246 lc->inter_frame_target = cpi->inter_frame_target;
247 lc->total_byte_count = cpi->total_byte_count;
248 lc->filter_level = cpi->common.filter_level;
249
250 lc->last_frame_percent_intra = cpi->last_frame_percent_intra;
251
252 memcpy (lc->count_mb_ref_frame_usage,
253 cpi->mb.count_mb_ref_frame_usage,
254 sizeof(cpi->mb.count_mb_ref_frame_usage));
255 }
256
restore_layer_context(VP8_COMP * cpi,const int layer)257 static void restore_layer_context(VP8_COMP *cpi, const int layer)
258 {
259 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
260
261 /* Restore layer dependent coding state */
262 cpi->current_layer = layer;
263 cpi->target_bandwidth = lc->target_bandwidth;
264 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
265 cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
266 cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
267 cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
268 cpi->oxcf.starting_buffer_level_in_ms = lc->starting_buffer_level_in_ms;
269 cpi->oxcf.optimal_buffer_level_in_ms = lc->optimal_buffer_level_in_ms;
270 cpi->oxcf.maximum_buffer_size_in_ms = lc->maximum_buffer_size_in_ms;
271 cpi->buffer_level = lc->buffer_level;
272 cpi->bits_off_target = lc->bits_off_target;
273 cpi->total_actual_bits = lc->total_actual_bits;
274 cpi->active_worst_quality = lc->active_worst_quality;
275 cpi->active_best_quality = lc->active_best_quality;
276 cpi->ni_av_qi = lc->ni_av_qi;
277 cpi->ni_tot_qi = lc->ni_tot_qi;
278 cpi->ni_frames = lc->ni_frames;
279 cpi->avg_frame_qindex = lc->avg_frame_qindex;
280 cpi->rate_correction_factor = lc->rate_correction_factor;
281 cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor;
282 cpi->gf_rate_correction_factor = lc->gf_rate_correction_factor;
283 cpi->mb.zbin_over_quant = lc->zbin_over_quant;
284 cpi->inter_frame_target = lc->inter_frame_target;
285 cpi->total_byte_count = lc->total_byte_count;
286 cpi->common.filter_level = lc->filter_level;
287
288 cpi->last_frame_percent_intra = lc->last_frame_percent_intra;
289
290 memcpy (cpi->mb.count_mb_ref_frame_usage,
291 lc->count_mb_ref_frame_usage,
292 sizeof(cpi->mb.count_mb_ref_frame_usage));
293 }
294
rescale(int val,int num,int denom)295 static int rescale(int val, int num, int denom)
296 {
297 int64_t llnum = num;
298 int64_t llden = denom;
299 int64_t llval = val;
300
301 return (int)(llval * llnum / llden);
302 }
303
init_temporal_layer_context(VP8_COMP * cpi,VP8_CONFIG * oxcf,const int layer,double prev_layer_framerate)304 static void init_temporal_layer_context(VP8_COMP *cpi,
305 VP8_CONFIG *oxcf,
306 const int layer,
307 double prev_layer_framerate)
308 {
309 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
310
311 lc->framerate = cpi->output_framerate / cpi->oxcf.rate_decimator[layer];
312 lc->target_bandwidth = cpi->oxcf.target_bitrate[layer] * 1000;
313
314 lc->starting_buffer_level_in_ms = oxcf->starting_buffer_level;
315 lc->optimal_buffer_level_in_ms = oxcf->optimal_buffer_level;
316 lc->maximum_buffer_size_in_ms = oxcf->maximum_buffer_size;
317
318 lc->starting_buffer_level =
319 rescale((int)(oxcf->starting_buffer_level),
320 lc->target_bandwidth, 1000);
321
322 if (oxcf->optimal_buffer_level == 0)
323 lc->optimal_buffer_level = lc->target_bandwidth / 8;
324 else
325 lc->optimal_buffer_level =
326 rescale((int)(oxcf->optimal_buffer_level),
327 lc->target_bandwidth, 1000);
328
329 if (oxcf->maximum_buffer_size == 0)
330 lc->maximum_buffer_size = lc->target_bandwidth / 8;
331 else
332 lc->maximum_buffer_size =
333 rescale((int)(oxcf->maximum_buffer_size),
334 lc->target_bandwidth, 1000);
335
336 /* Work out the average size of a frame within this layer */
337 if (layer > 0)
338 lc->avg_frame_size_for_layer =
339 (int)((cpi->oxcf.target_bitrate[layer] -
340 cpi->oxcf.target_bitrate[layer-1]) * 1000 /
341 (lc->framerate - prev_layer_framerate));
342
343 lc->active_worst_quality = cpi->oxcf.worst_allowed_q;
344 lc->active_best_quality = cpi->oxcf.best_allowed_q;
345 lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
346
347 lc->buffer_level = lc->starting_buffer_level;
348 lc->bits_off_target = lc->starting_buffer_level;
349
350 lc->total_actual_bits = 0;
351 lc->ni_av_qi = 0;
352 lc->ni_tot_qi = 0;
353 lc->ni_frames = 0;
354 lc->rate_correction_factor = 1.0;
355 lc->key_frame_rate_correction_factor = 1.0;
356 lc->gf_rate_correction_factor = 1.0;
357 lc->inter_frame_target = 0;
358 }
359
360 // Upon a run-time change in temporal layers, reset the layer context parameters
361 // for any "new" layers. For "existing" layers, let them inherit the parameters
362 // from the previous layer state (at the same layer #). In future we may want
363 // to better map the previous layer state(s) to the "new" ones.
reset_temporal_layer_change(VP8_COMP * cpi,VP8_CONFIG * oxcf,const int prev_num_layers)364 static void reset_temporal_layer_change(VP8_COMP *cpi,
365 VP8_CONFIG *oxcf,
366 const int prev_num_layers)
367 {
368 int i;
369 double prev_layer_framerate = 0;
370 const int curr_num_layers = cpi->oxcf.number_of_layers;
371 // If the previous state was 1 layer, get current layer context from cpi.
372 // We need this to set the layer context for the new layers below.
373 if (prev_num_layers == 1)
374 {
375 cpi->current_layer = 0;
376 save_layer_context(cpi);
377 }
378 for (i = 0; i < curr_num_layers; i++)
379 {
380 LAYER_CONTEXT *lc = &cpi->layer_context[i];
381 if (i >= prev_num_layers)
382 {
383 init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
384 }
385 // The initial buffer levels are set based on their starting levels.
386 // We could set the buffer levels based on the previous state (normalized
387 // properly by the layer bandwidths) but we would need to keep track of
388 // the previous set of layer bandwidths (i.e., target_bitrate[i])
389 // before the layer change. For now, reset to the starting levels.
390 lc->buffer_level = cpi->oxcf.starting_buffer_level_in_ms *
391 cpi->oxcf.target_bitrate[i];
392 lc->bits_off_target = lc->buffer_level;
393 // TDOD(marpan): Should we set the rate_correction_factor and
394 // active_worst/best_quality to values derived from the previous layer
395 // state (to smooth-out quality dips/rate fluctuation at transition)?
396
397 // We need to treat the 1 layer case separately: oxcf.target_bitrate[i]
398 // is not set for 1 layer, and the restore_layer_context/save_context()
399 // are not called in the encoding loop, so we need to call it here to
400 // pass the layer context state to |cpi|.
401 if (curr_num_layers == 1)
402 {
403 lc->target_bandwidth = cpi->oxcf.target_bandwidth;
404 lc->buffer_level = cpi->oxcf.starting_buffer_level_in_ms *
405 lc->target_bandwidth / 1000;
406 lc->bits_off_target = lc->buffer_level;
407 restore_layer_context(cpi, 0);
408 }
409 prev_layer_framerate = cpi->output_framerate /
410 cpi->oxcf.rate_decimator[i];
411 }
412 }
413
setup_features(VP8_COMP * cpi)414 static void setup_features(VP8_COMP *cpi)
415 {
416 // If segmentation enabled set the update flags
417 if ( cpi->mb.e_mbd.segmentation_enabled )
418 {
419 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
420 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
421 }
422 else
423 {
424 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
425 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
426 }
427
428 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
429 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
430 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
431 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
432 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
433 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
434
435 set_default_lf_deltas(cpi);
436
437 }
438
439
440 static void dealloc_raw_frame_buffers(VP8_COMP *cpi);
441
442
dealloc_compressor_data(VP8_COMP * cpi)443 static void dealloc_compressor_data(VP8_COMP *cpi)
444 {
445 vpx_free(cpi->tplist);
446 cpi->tplist = NULL;
447
448 /* Delete last frame MV storage buffers */
449 vpx_free(cpi->lfmv);
450 cpi->lfmv = 0;
451
452 vpx_free(cpi->lf_ref_frame_sign_bias);
453 cpi->lf_ref_frame_sign_bias = 0;
454
455 vpx_free(cpi->lf_ref_frame);
456 cpi->lf_ref_frame = 0;
457
458 /* Delete sementation map */
459 vpx_free(cpi->segmentation_map);
460 cpi->segmentation_map = 0;
461
462 vpx_free(cpi->active_map);
463 cpi->active_map = 0;
464
465 vp8_de_alloc_frame_buffers(&cpi->common);
466
467 vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
468 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
469 dealloc_raw_frame_buffers(cpi);
470
471 vpx_free(cpi->tok);
472 cpi->tok = 0;
473
474 /* Structure used to monitor GF usage */
475 vpx_free(cpi->gf_active_flags);
476 cpi->gf_active_flags = 0;
477
478 /* Activity mask based per mb zbin adjustments */
479 vpx_free(cpi->mb_activity_map);
480 cpi->mb_activity_map = 0;
481
482 vpx_free(cpi->mb.pip);
483 cpi->mb.pip = 0;
484
485 #if CONFIG_MULTITHREAD
486 vpx_free(cpi->mt_current_mb_col);
487 cpi->mt_current_mb_col = NULL;
488 #endif
489 }
490
enable_segmentation(VP8_COMP * cpi)491 static void enable_segmentation(VP8_COMP *cpi)
492 {
493 /* Set the appropriate feature bit */
494 cpi->mb.e_mbd.segmentation_enabled = 1;
495 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
496 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
497 }
disable_segmentation(VP8_COMP * cpi)498 static void disable_segmentation(VP8_COMP *cpi)
499 {
500 /* Clear the appropriate feature bit */
501 cpi->mb.e_mbd.segmentation_enabled = 0;
502 }
503
504 /* Valid values for a segment are 0 to 3
505 * Segmentation map is arrange as [Rows][Columns]
506 */
set_segmentation_map(VP8_COMP * cpi,unsigned char * segmentation_map)507 static void set_segmentation_map(VP8_COMP *cpi, unsigned char *segmentation_map)
508 {
509 /* Copy in the new segmentation map */
510 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
511
512 /* Signal that the map should be updated. */
513 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
514 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
515 }
516
517 /* The values given for each segment can be either deltas (from the default
518 * value chosen for the frame) or absolute values.
519 *
520 * Valid range for abs values is:
521 * (0-127 for MB_LVL_ALT_Q), (0-63 for SEGMENT_ALT_LF)
522 * Valid range for delta values are:
523 * (+/-127 for MB_LVL_ALT_Q), (+/-63 for SEGMENT_ALT_LF)
524 *
525 * abs_delta = SEGMENT_DELTADATA (deltas)
526 * abs_delta = SEGMENT_ABSDATA (use the absolute values given).
527 *
528 */
set_segment_data(VP8_COMP * cpi,signed char * feature_data,unsigned char abs_delta)529 static void set_segment_data(VP8_COMP *cpi, signed char *feature_data, unsigned char abs_delta)
530 {
531 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
532 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
533 }
534
535
segmentation_test_function(VP8_COMP * cpi)536 static void segmentation_test_function(VP8_COMP *cpi)
537 {
538 unsigned char *seg_map;
539 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
540
541 // Create a temporary map for segmentation data.
542 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
543
544 // Set the segmentation Map
545 set_segmentation_map(cpi, seg_map);
546
547 // Activate segmentation.
548 enable_segmentation(cpi);
549
550 // Set up the quant segment data
551 feature_data[MB_LVL_ALT_Q][0] = 0;
552 feature_data[MB_LVL_ALT_Q][1] = 4;
553 feature_data[MB_LVL_ALT_Q][2] = 0;
554 feature_data[MB_LVL_ALT_Q][3] = 0;
555 // Set up the loop segment data
556 feature_data[MB_LVL_ALT_LF][0] = 0;
557 feature_data[MB_LVL_ALT_LF][1] = 0;
558 feature_data[MB_LVL_ALT_LF][2] = 0;
559 feature_data[MB_LVL_ALT_LF][3] = 0;
560
561 // Initialise the feature data structure
562 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
563 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
564
565 // Delete sementation map
566 vpx_free(seg_map);
567
568 seg_map = 0;
569 }
570
571 /* A simple function to cyclically refresh the background at a lower Q */
cyclic_background_refresh(VP8_COMP * cpi,int Q,int lf_adjustment)572 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
573 {
574 unsigned char *seg_map = cpi->segmentation_map;
575 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
576 int i;
577 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
578 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
579
580 cpi->cyclic_refresh_q = Q / 2;
581
582 // Set every macroblock to be eligible for update.
583 // For key frame this will reset seg map to 0.
584 vpx_memset(cpi->segmentation_map, 0, mbs_in_frame);
585
586 if (cpi->common.frame_type != KEY_FRAME)
587 {
588 /* Cycle through the macro_block rows */
589 /* MB loop to set local segmentation map */
590 i = cpi->cyclic_refresh_mode_index;
591 assert(i < mbs_in_frame);
592 do
593 {
594 /* If the MB is as a candidate for clean up then mark it for
595 * possible boost/refresh (segment 1) The segment id may get
596 * reset to 0 later if the MB gets coded anything other than
597 * last frame 0,0 as only (last frame 0,0) MBs are eligable for
598 * refresh : that is to say Mbs likely to be background blocks.
599 */
600 if (cpi->cyclic_refresh_map[i] == 0)
601 {
602 seg_map[i] = 1;
603 block_count --;
604 }
605 else if (cpi->cyclic_refresh_map[i] < 0)
606 cpi->cyclic_refresh_map[i]++;
607
608 i++;
609 if (i == mbs_in_frame)
610 i = 0;
611
612 }
613 while(block_count && i != cpi->cyclic_refresh_mode_index);
614
615 cpi->cyclic_refresh_mode_index = i;
616
617 #if CONFIG_TEMPORAL_DENOISING
618 if (cpi->oxcf.noise_sensitivity > 0) {
619 if (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive &&
620 Q < (int)cpi->denoiser.denoise_pars.qp_thresh) {
621 // Under aggressive denoising, use segmentation to turn off loop
622 // filter below some qp thresh. The filter is turned off for all
623 // blocks that have been encoded as ZEROMV LAST x frames in a row,
624 // where x is set by cpi->denoiser.denoise_pars.consec_zerolast.
625 // This is to avoid "dot" artifacts that can occur from repeated
626 // loop filtering on noisy input source.
627 cpi->cyclic_refresh_q = Q;
628 lf_adjustment = -MAX_LOOP_FILTER;
629 for (i = 0; i < mbs_in_frame; ++i) {
630 seg_map[i] = (cpi->consec_zero_last[i] >
631 cpi->denoiser.denoise_pars.consec_zerolast) ? 1 : 0;
632 }
633 }
634 }
635 #endif
636 }
637
638 /* Activate segmentation. */
639 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
640 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
641 enable_segmentation(cpi);
642
643 /* Set up the quant segment data */
644 feature_data[MB_LVL_ALT_Q][0] = 0;
645 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
646 feature_data[MB_LVL_ALT_Q][2] = 0;
647 feature_data[MB_LVL_ALT_Q][3] = 0;
648
649 /* Set up the loop segment data */
650 feature_data[MB_LVL_ALT_LF][0] = 0;
651 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
652 feature_data[MB_LVL_ALT_LF][2] = 0;
653 feature_data[MB_LVL_ALT_LF][3] = 0;
654
655 /* Initialise the feature data structure */
656 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
657
658 }
659
set_default_lf_deltas(VP8_COMP * cpi)660 static void set_default_lf_deltas(VP8_COMP *cpi)
661 {
662 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
663 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
664
665 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
666 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
667
668 /* Test of ref frame deltas */
669 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
670 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
671 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
672 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
673
674 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; /* BPRED */
675
676 if(cpi->oxcf.Mode == MODE_REALTIME)
677 cpi->mb.e_mbd.mode_lf_deltas[1] = -12; /* Zero */
678 else
679 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; /* Zero */
680
681 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; /* New mv */
682 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; /* Split mv */
683 }
684
685 /* Convenience macros for mapping speed and mode into a continuous
686 * range
687 */
688 #define GOOD(x) (x+1)
689 #define RT(x) (x+7)
690
speed_map(int speed,const int * map)691 static int speed_map(int speed, const int *map)
692 {
693 int res;
694
695 do
696 {
697 res = *map++;
698 } while(speed >= *map++);
699 return res;
700 }
701
702 static const int thresh_mult_map_znn[] = {
703 /* map common to zero, nearest, and near */
704 0, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(2), 2000, INT_MAX
705 };
706
707 static const int thresh_mult_map_vhpred[] = {
708 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(1), 2000,
709 RT(7), INT_MAX, INT_MAX
710 };
711
712 static const int thresh_mult_map_bpred[] = {
713 2000, GOOD(0), 2500, GOOD(2), 5000, GOOD(3), 7500, RT(0), 2500, RT(1), 5000,
714 RT(6), INT_MAX, INT_MAX
715 };
716
717 static const int thresh_mult_map_tm[] = {
718 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 0, RT(1), 1000, RT(2), 2000,
719 RT(7), INT_MAX, INT_MAX
720 };
721
722 static const int thresh_mult_map_new1[] = {
723 1000, GOOD(2), 2000, RT(0), 2000, INT_MAX
724 };
725
726 static const int thresh_mult_map_new2[] = {
727 1000, GOOD(2), 2000, GOOD(3), 2500, GOOD(5), 4000, RT(0), 2000, RT(2), 2500,
728 RT(5), 4000, INT_MAX
729 };
730
731 static const int thresh_mult_map_split1[] = {
732 2500, GOOD(0), 1700, GOOD(2), 10000, GOOD(3), 25000, GOOD(4), INT_MAX,
733 RT(0), 5000, RT(1), 10000, RT(2), 25000, RT(3), INT_MAX, INT_MAX
734 };
735
736 static const int thresh_mult_map_split2[] = {
737 5000, GOOD(0), 4500, GOOD(2), 20000, GOOD(3), 50000, GOOD(4), INT_MAX,
738 RT(0), 10000, RT(1), 20000, RT(2), 50000, RT(3), INT_MAX, INT_MAX
739 };
740
741 static const int mode_check_freq_map_zn2[] = {
742 /* {zero,nearest}{2,3} */
743 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
744 };
745
746 static const int mode_check_freq_map_vhbpred[] = {
747 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(5), 4, INT_MAX
748 };
749
750 static const int mode_check_freq_map_near2[] = {
751 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(10), 1<<2, RT(11), 1<<3, RT(12), 1<<4,
752 INT_MAX
753 };
754
755 static const int mode_check_freq_map_new1[] = {
756 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
757 };
758
759 static const int mode_check_freq_map_new2[] = {
760 0, GOOD(5), 4, RT(0), 0, RT(3), 4, RT(10), 1<<3, RT(11), 1<<4, RT(12), 1<<5,
761 INT_MAX
762 };
763
764 static const int mode_check_freq_map_split1[] = {
765 0, GOOD(2), 2, GOOD(3), 7, RT(1), 2, RT(2), 7, INT_MAX
766 };
767
768 static const int mode_check_freq_map_split2[] = {
769 0, GOOD(1), 2, GOOD(2), 4, GOOD(3), 15, RT(1), 4, RT(2), 15, INT_MAX
770 };
771
vp8_set_speed_features(VP8_COMP * cpi)772 void vp8_set_speed_features(VP8_COMP *cpi)
773 {
774 SPEED_FEATURES *sf = &cpi->sf;
775 int Mode = cpi->compressor_speed;
776 int Speed = cpi->Speed;
777 int i;
778 VP8_COMMON *cm = &cpi->common;
779 int last_improved_quant = sf->improved_quant;
780 int ref_frames;
781
782 /* Initialise default mode frequency sampling variables */
783 for (i = 0; i < MAX_MODES; i ++)
784 {
785 cpi->mode_check_freq[i] = 0;
786 }
787
788 cpi->mb.mbs_tested_so_far = 0;
789
790 /* best quality defaults */
791 sf->RD = 1;
792 sf->search_method = NSTEP;
793 sf->improved_quant = 1;
794 sf->improved_dct = 1;
795 sf->auto_filter = 1;
796 sf->recode_loop = 1;
797 sf->quarter_pixel_search = 1;
798 sf->half_pixel_search = 1;
799 sf->iterative_sub_pixel = 1;
800 sf->optimize_coefficients = 1;
801 sf->use_fastquant_for_pick = 0;
802 sf->no_skip_block4x4_search = 1;
803
804 sf->first_step = 0;
805 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
806 sf->improved_mv_pred = 1;
807
808 /* default thresholds to 0 */
809 for (i = 0; i < MAX_MODES; i++)
810 sf->thresh_mult[i] = 0;
811
812 /* Count enabled references */
813 ref_frames = 1;
814 if (cpi->ref_frame_flags & VP8_LAST_FRAME)
815 ref_frames++;
816 if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
817 ref_frames++;
818 if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
819 ref_frames++;
820
821 /* Convert speed to continuous range, with clamping */
822 if (Mode == 0)
823 Speed = 0;
824 else if (Mode == 2)
825 Speed = RT(Speed);
826 else
827 {
828 if (Speed > 5)
829 Speed = 5;
830 Speed = GOOD(Speed);
831 }
832
833 sf->thresh_mult[THR_ZERO1] =
834 sf->thresh_mult[THR_NEAREST1] =
835 sf->thresh_mult[THR_NEAR1] =
836 sf->thresh_mult[THR_DC] = 0; /* always */
837
838 sf->thresh_mult[THR_ZERO2] =
839 sf->thresh_mult[THR_ZERO3] =
840 sf->thresh_mult[THR_NEAREST2] =
841 sf->thresh_mult[THR_NEAREST3] =
842 sf->thresh_mult[THR_NEAR2] =
843 sf->thresh_mult[THR_NEAR3] = speed_map(Speed, thresh_mult_map_znn);
844
845 sf->thresh_mult[THR_V_PRED] =
846 sf->thresh_mult[THR_H_PRED] = speed_map(Speed, thresh_mult_map_vhpred);
847 sf->thresh_mult[THR_B_PRED] = speed_map(Speed, thresh_mult_map_bpred);
848 sf->thresh_mult[THR_TM] = speed_map(Speed, thresh_mult_map_tm);
849 sf->thresh_mult[THR_NEW1] = speed_map(Speed, thresh_mult_map_new1);
850 sf->thresh_mult[THR_NEW2] =
851 sf->thresh_mult[THR_NEW3] = speed_map(Speed, thresh_mult_map_new2);
852 sf->thresh_mult[THR_SPLIT1] = speed_map(Speed, thresh_mult_map_split1);
853 sf->thresh_mult[THR_SPLIT2] =
854 sf->thresh_mult[THR_SPLIT3] = speed_map(Speed, thresh_mult_map_split2);
855
856 cpi->mode_check_freq[THR_ZERO1] =
857 cpi->mode_check_freq[THR_NEAREST1] =
858 cpi->mode_check_freq[THR_NEAR1] =
859 cpi->mode_check_freq[THR_TM] =
860 cpi->mode_check_freq[THR_DC] = 0; /* always */
861
862 cpi->mode_check_freq[THR_ZERO2] =
863 cpi->mode_check_freq[THR_ZERO3] =
864 cpi->mode_check_freq[THR_NEAREST2] =
865 cpi->mode_check_freq[THR_NEAREST3] = speed_map(Speed,
866 mode_check_freq_map_zn2);
867
868 cpi->mode_check_freq[THR_NEAR2] =
869 cpi->mode_check_freq[THR_NEAR3] = speed_map(Speed,
870 mode_check_freq_map_near2);
871
872 cpi->mode_check_freq[THR_V_PRED] =
873 cpi->mode_check_freq[THR_H_PRED] =
874 cpi->mode_check_freq[THR_B_PRED] = speed_map(Speed,
875 mode_check_freq_map_vhbpred);
876 cpi->mode_check_freq[THR_NEW1] = speed_map(Speed,
877 mode_check_freq_map_new1);
878 cpi->mode_check_freq[THR_NEW2] =
879 cpi->mode_check_freq[THR_NEW3] = speed_map(Speed,
880 mode_check_freq_map_new2);
881 cpi->mode_check_freq[THR_SPLIT1] = speed_map(Speed,
882 mode_check_freq_map_split1);
883 cpi->mode_check_freq[THR_SPLIT2] =
884 cpi->mode_check_freq[THR_SPLIT3] = speed_map(Speed,
885 mode_check_freq_map_split2);
886 Speed = cpi->Speed;
887 switch (Mode)
888 {
889 #if !(CONFIG_REALTIME_ONLY)
890 case 0: /* best quality mode */
891 sf->first_step = 0;
892 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
893 break;
894 case 1:
895 case 3:
896 if (Speed > 0)
897 {
898 /* Disable coefficient optimization above speed 0 */
899 sf->optimize_coefficients = 0;
900 sf->use_fastquant_for_pick = 1;
901 sf->no_skip_block4x4_search = 0;
902
903 sf->first_step = 1;
904 }
905
906 if (Speed > 2)
907 {
908 sf->improved_quant = 0;
909 sf->improved_dct = 0;
910
911 /* Only do recode loop on key frames, golden frames and
912 * alt ref frames
913 */
914 sf->recode_loop = 2;
915
916 }
917
918 if (Speed > 3)
919 {
920 sf->auto_filter = 1;
921 sf->recode_loop = 0; /* recode loop off */
922 sf->RD = 0; /* Turn rd off */
923
924 }
925
926 if (Speed > 4)
927 {
928 sf->auto_filter = 0; /* Faster selection of loop filter */
929 }
930
931 break;
932 #endif
933 case 2:
934 sf->optimize_coefficients = 0;
935 sf->recode_loop = 0;
936 sf->auto_filter = 1;
937 sf->iterative_sub_pixel = 1;
938 sf->search_method = NSTEP;
939
940 if (Speed > 0)
941 {
942 sf->improved_quant = 0;
943 sf->improved_dct = 0;
944
945 sf->use_fastquant_for_pick = 1;
946 sf->no_skip_block4x4_search = 0;
947 sf->first_step = 1;
948 }
949
950 if (Speed > 2)
951 sf->auto_filter = 0; /* Faster selection of loop filter */
952
953 if (Speed > 3)
954 {
955 sf->RD = 0;
956 sf->auto_filter = 1;
957 }
958
959 if (Speed > 4)
960 {
961 sf->auto_filter = 0; /* Faster selection of loop filter */
962 sf->search_method = HEX;
963 sf->iterative_sub_pixel = 0;
964 }
965
966 if (Speed > 6)
967 {
968 unsigned int sum = 0;
969 unsigned int total_mbs = cm->MBs;
970 int thresh;
971 unsigned int total_skip;
972
973 int min = 2000;
974
975 if (cpi->oxcf.encode_breakout > 2000)
976 min = cpi->oxcf.encode_breakout;
977
978 min >>= 7;
979
980 for (i = 0; i < min; i++)
981 {
982 sum += cpi->mb.error_bins[i];
983 }
984
985 total_skip = sum;
986 sum = 0;
987
988 /* i starts from 2 to make sure thresh started from 2048 */
989 for (; i < 1024; i++)
990 {
991 sum += cpi->mb.error_bins[i];
992
993 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
994 break;
995 }
996
997 i--;
998 thresh = (i << 7);
999
1000 if (thresh < 2000)
1001 thresh = 2000;
1002
1003 if (ref_frames > 1)
1004 {
1005 sf->thresh_mult[THR_NEW1 ] = thresh;
1006 sf->thresh_mult[THR_NEAREST1 ] = thresh >> 1;
1007 sf->thresh_mult[THR_NEAR1 ] = thresh >> 1;
1008 }
1009
1010 if (ref_frames > 2)
1011 {
1012 sf->thresh_mult[THR_NEW2] = thresh << 1;
1013 sf->thresh_mult[THR_NEAREST2 ] = thresh;
1014 sf->thresh_mult[THR_NEAR2 ] = thresh;
1015 }
1016
1017 if (ref_frames > 3)
1018 {
1019 sf->thresh_mult[THR_NEW3] = thresh << 1;
1020 sf->thresh_mult[THR_NEAREST3 ] = thresh;
1021 sf->thresh_mult[THR_NEAR3 ] = thresh;
1022 }
1023
1024 sf->improved_mv_pred = 0;
1025 }
1026
1027 if (Speed > 8)
1028 sf->quarter_pixel_search = 0;
1029
1030 if(cm->version == 0)
1031 {
1032 cm->filter_type = NORMAL_LOOPFILTER;
1033
1034 if (Speed >= 14)
1035 cm->filter_type = SIMPLE_LOOPFILTER;
1036 }
1037 else
1038 {
1039 cm->filter_type = SIMPLE_LOOPFILTER;
1040 }
1041
1042 /* This has a big hit on quality. Last resort */
1043 if (Speed >= 15)
1044 sf->half_pixel_search = 0;
1045
1046 vpx_memset(cpi->mb.error_bins, 0, sizeof(cpi->mb.error_bins));
1047
1048 }; /* switch */
1049
1050 /* Slow quant, dct and trellis not worthwhile for first pass
1051 * so make sure they are always turned off.
1052 */
1053 if ( cpi->pass == 1 )
1054 {
1055 sf->improved_quant = 0;
1056 sf->optimize_coefficients = 0;
1057 sf->improved_dct = 0;
1058 }
1059
1060 if (cpi->sf.search_method == NSTEP)
1061 {
1062 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1063 }
1064 else if (cpi->sf.search_method == DIAMOND)
1065 {
1066 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
1067 }
1068
1069 if (cpi->sf.improved_dct)
1070 {
1071 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
1072 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
1073 }
1074 else
1075 {
1076 /* No fast FDCT defined for any platform at this time. */
1077 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
1078 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
1079 }
1080
1081 cpi->mb.short_walsh4x4 = vp8_short_walsh4x4;
1082
1083 if (cpi->sf.improved_quant)
1084 {
1085 cpi->mb.quantize_b = vp8_regular_quantize_b;
1086 cpi->mb.quantize_b_pair = vp8_regular_quantize_b_pair;
1087 }
1088 else
1089 {
1090 cpi->mb.quantize_b = vp8_fast_quantize_b;
1091 cpi->mb.quantize_b_pair = vp8_fast_quantize_b_pair;
1092 }
1093 if (cpi->sf.improved_quant != last_improved_quant)
1094 vp8cx_init_quantizer(cpi);
1095
1096 if (cpi->sf.iterative_sub_pixel == 1)
1097 {
1098 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1099 }
1100 else if (cpi->sf.quarter_pixel_search)
1101 {
1102 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1103 }
1104 else if (cpi->sf.half_pixel_search)
1105 {
1106 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
1107 }
1108 else
1109 {
1110 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1111 }
1112
1113 if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
1114 cpi->mb.optimize = 1;
1115 else
1116 cpi->mb.optimize = 0;
1117
1118 if (cpi->common.full_pixel)
1119 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
1120
1121 #ifdef SPEEDSTATS
1122 frames_at_speed[cpi->Speed]++;
1123 #endif
1124 }
1125 #undef GOOD
1126 #undef RT
1127
alloc_raw_frame_buffers(VP8_COMP * cpi)1128 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
1129 {
1130 #if VP8_TEMPORAL_ALT_REF
1131 int width = (cpi->oxcf.Width + 15) & ~15;
1132 int height = (cpi->oxcf.Height + 15) & ~15;
1133 #endif
1134
1135 cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1136 cpi->oxcf.lag_in_frames);
1137 if(!cpi->lookahead)
1138 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1139 "Failed to allocate lag buffers");
1140
1141 #if VP8_TEMPORAL_ALT_REF
1142
1143 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
1144 width, height, VP8BORDERINPIXELS))
1145 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1146 "Failed to allocate altref buffer");
1147
1148 #endif
1149 }
1150
1151
dealloc_raw_frame_buffers(VP8_COMP * cpi)1152 static void dealloc_raw_frame_buffers(VP8_COMP *cpi)
1153 {
1154 #if VP8_TEMPORAL_ALT_REF
1155 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
1156 #endif
1157 vp8_lookahead_destroy(cpi->lookahead);
1158 }
1159
1160
vp8_alloc_partition_data(VP8_COMP * cpi)1161 static int vp8_alloc_partition_data(VP8_COMP *cpi)
1162 {
1163 vpx_free(cpi->mb.pip);
1164
1165 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
1166 (cpi->common.mb_rows + 1),
1167 sizeof(PARTITION_INFO));
1168 if(!cpi->mb.pip)
1169 return 1;
1170
1171 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1172
1173 return 0;
1174 }
1175
vp8_alloc_compressor_data(VP8_COMP * cpi)1176 void vp8_alloc_compressor_data(VP8_COMP *cpi)
1177 {
1178 VP8_COMMON *cm = & cpi->common;
1179
1180 int width = cm->Width;
1181 int height = cm->Height;
1182
1183 if (vp8_alloc_frame_buffers(cm, width, height))
1184 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1185 "Failed to allocate frame buffers");
1186
1187 if (vp8_alloc_partition_data(cpi))
1188 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1189 "Failed to allocate partition data");
1190
1191
1192 if ((width & 0xf) != 0)
1193 width += 16 - (width & 0xf);
1194
1195 if ((height & 0xf) != 0)
1196 height += 16 - (height & 0xf);
1197
1198
1199 if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame,
1200 width, height, VP8BORDERINPIXELS))
1201 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1202 "Failed to allocate last frame buffer");
1203
1204 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
1205 width, height, VP8BORDERINPIXELS))
1206 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1207 "Failed to allocate scaled source buffer");
1208
1209 vpx_free(cpi->tok);
1210
1211 {
1212 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1213 unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */
1214 #else
1215 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1216 #endif
1217 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1218 }
1219
1220 /* Data used for real time vc mode to see if gf needs refreshing */
1221 cpi->zeromv_count = 0;
1222
1223
1224 /* Structures used to monitor GF usage */
1225 vpx_free(cpi->gf_active_flags);
1226 CHECK_MEM_ERROR(cpi->gf_active_flags,
1227 vpx_calloc(sizeof(*cpi->gf_active_flags),
1228 cm->mb_rows * cm->mb_cols));
1229 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1230
1231 vpx_free(cpi->mb_activity_map);
1232 CHECK_MEM_ERROR(cpi->mb_activity_map,
1233 vpx_calloc(sizeof(*cpi->mb_activity_map),
1234 cm->mb_rows * cm->mb_cols));
1235
1236 /* allocate memory for storing last frame's MVs for MV prediction. */
1237 vpx_free(cpi->lfmv);
1238 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
1239 sizeof(*cpi->lfmv)));
1240 vpx_free(cpi->lf_ref_frame_sign_bias);
1241 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias,
1242 vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
1243 sizeof(*cpi->lf_ref_frame_sign_bias)));
1244 vpx_free(cpi->lf_ref_frame);
1245 CHECK_MEM_ERROR(cpi->lf_ref_frame,
1246 vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
1247 sizeof(*cpi->lf_ref_frame)));
1248
1249 /* Create the encoder segmentation map and set all entries to 0 */
1250 vpx_free(cpi->segmentation_map);
1251 CHECK_MEM_ERROR(cpi->segmentation_map,
1252 vpx_calloc(cm->mb_rows * cm->mb_cols,
1253 sizeof(*cpi->segmentation_map)));
1254 cpi->cyclic_refresh_mode_index = 0;
1255 vpx_free(cpi->active_map);
1256 CHECK_MEM_ERROR(cpi->active_map,
1257 vpx_calloc(cm->mb_rows * cm->mb_cols,
1258 sizeof(*cpi->active_map)));
1259 vpx_memset(cpi->active_map , 1, (cm->mb_rows * cm->mb_cols));
1260
1261 #if CONFIG_MULTITHREAD
1262 if (width < 640)
1263 cpi->mt_sync_range = 1;
1264 else if (width <= 1280)
1265 cpi->mt_sync_range = 4;
1266 else if (width <= 2560)
1267 cpi->mt_sync_range = 8;
1268 else
1269 cpi->mt_sync_range = 16;
1270
1271 if (cpi->oxcf.multi_threaded > 1)
1272 {
1273 vpx_free(cpi->mt_current_mb_col);
1274 CHECK_MEM_ERROR(cpi->mt_current_mb_col,
1275 vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows));
1276 }
1277
1278 #endif
1279
1280 vpx_free(cpi->tplist);
1281 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cm->mb_rows));
1282
1283 #if CONFIG_TEMPORAL_DENOISING
1284 if (cpi->oxcf.noise_sensitivity > 0) {
1285 vp8_denoiser_free(&cpi->denoiser);
1286 vp8_denoiser_allocate(&cpi->denoiser, width, height,
1287 cm->mb_rows, cm->mb_cols,
1288 cpi->oxcf.noise_sensitivity);
1289 }
1290 #endif
1291 }
1292
1293
1294 /* Quant MOD */
1295 static const int q_trans[] =
1296 {
1297 0, 1, 2, 3, 4, 5, 7, 8,
1298 9, 10, 12, 13, 15, 17, 18, 19,
1299 20, 21, 23, 24, 25, 26, 27, 28,
1300 29, 30, 31, 33, 35, 37, 39, 41,
1301 43, 45, 47, 49, 51, 53, 55, 57,
1302 59, 61, 64, 67, 70, 73, 76, 79,
1303 82, 85, 88, 91, 94, 97, 100, 103,
1304 106, 109, 112, 115, 118, 121, 124, 127,
1305 };
1306
vp8_reverse_trans(int x)1307 int vp8_reverse_trans(int x)
1308 {
1309 int i;
1310
1311 for (i = 0; i < 64; i++)
1312 if (q_trans[i] >= x)
1313 return i;
1314
1315 return 63;
1316 }
vp8_new_framerate(VP8_COMP * cpi,double framerate)1317 void vp8_new_framerate(VP8_COMP *cpi, double framerate)
1318 {
1319 if(framerate < .1)
1320 framerate = 30;
1321
1322 cpi->framerate = framerate;
1323 cpi->output_framerate = framerate;
1324 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth /
1325 cpi->output_framerate);
1326 cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
1327 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
1328 cpi->oxcf.two_pass_vbrmin_section / 100);
1329
1330 /* Set Maximum gf/arf interval */
1331 cpi->max_gf_interval = ((int)(cpi->output_framerate / 2.0) + 2);
1332
1333 if(cpi->max_gf_interval < 12)
1334 cpi->max_gf_interval = 12;
1335
1336 /* Extended interval for genuinely static scenes */
1337 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1338
1339 /* Special conditions when altr ref frame enabled in lagged compress mode */
1340 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
1341 {
1342 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1343 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1344
1345 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1346 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1347 }
1348
1349 if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
1350 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1351 }
1352
1353
init_config(VP8_COMP * cpi,VP8_CONFIG * oxcf)1354 static void init_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1355 {
1356 VP8_COMMON *cm = &cpi->common;
1357
1358 cpi->oxcf = *oxcf;
1359
1360 cpi->auto_gold = 1;
1361 cpi->auto_adjust_gold_quantizer = 1;
1362
1363 cm->version = oxcf->Version;
1364 vp8_setup_version(cm);
1365
1366 /* frame rate is not available on the first frame, as it's derived from
1367 * the observed timestamps. The actual value used here doesn't matter
1368 * too much, as it will adapt quickly. If the reciprocal of the timebase
1369 * seems like a reasonable framerate, then use that as a guess, otherwise
1370 * use 30.
1371 */
1372 cpi->framerate = (double)(oxcf->timebase.den) /
1373 (double)(oxcf->timebase.num);
1374
1375 if (cpi->framerate > 180)
1376 cpi->framerate = 30;
1377
1378 cpi->ref_framerate = cpi->framerate;
1379
1380 /* change includes all joint functionality */
1381 vp8_change_config(cpi, oxcf);
1382
1383 /* Initialize active best and worst q and average q values. */
1384 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1385 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1386 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1387
1388 /* Initialise the starting buffer levels */
1389 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1390 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1391
1392 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1393 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1394 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1395 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1396
1397 cpi->total_actual_bits = 0;
1398 cpi->total_target_vs_actual = 0;
1399
1400 /* Temporal scalabilty */
1401 if (cpi->oxcf.number_of_layers > 1)
1402 {
1403 unsigned int i;
1404 double prev_layer_framerate=0;
1405
1406 for (i=0; i<cpi->oxcf.number_of_layers; i++)
1407 {
1408 init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
1409 prev_layer_framerate = cpi->output_framerate /
1410 cpi->oxcf.rate_decimator[i];
1411 }
1412 }
1413
1414 #if VP8_TEMPORAL_ALT_REF
1415 {
1416 int i;
1417
1418 cpi->fixed_divide[0] = 0;
1419
1420 for (i = 1; i < 512; i++)
1421 cpi->fixed_divide[i] = 0x80000 / i;
1422 }
1423 #endif
1424 }
1425
update_layer_contexts(VP8_COMP * cpi)1426 static void update_layer_contexts (VP8_COMP *cpi)
1427 {
1428 VP8_CONFIG *oxcf = &cpi->oxcf;
1429
1430 /* Update snapshots of the layer contexts to reflect new parameters */
1431 if (oxcf->number_of_layers > 1)
1432 {
1433 unsigned int i;
1434 double prev_layer_framerate=0;
1435
1436 assert(oxcf->number_of_layers <= VPX_TS_MAX_LAYERS);
1437 for (i = 0; i < oxcf->number_of_layers && i < VPX_TS_MAX_LAYERS; ++i)
1438 {
1439 LAYER_CONTEXT *lc = &cpi->layer_context[i];
1440
1441 lc->framerate =
1442 cpi->ref_framerate / oxcf->rate_decimator[i];
1443 lc->target_bandwidth = oxcf->target_bitrate[i] * 1000;
1444
1445 lc->starting_buffer_level = rescale(
1446 (int)oxcf->starting_buffer_level_in_ms,
1447 lc->target_bandwidth, 1000);
1448
1449 if (oxcf->optimal_buffer_level == 0)
1450 lc->optimal_buffer_level = lc->target_bandwidth / 8;
1451 else
1452 lc->optimal_buffer_level = rescale(
1453 (int)oxcf->optimal_buffer_level_in_ms,
1454 lc->target_bandwidth, 1000);
1455
1456 if (oxcf->maximum_buffer_size == 0)
1457 lc->maximum_buffer_size = lc->target_bandwidth / 8;
1458 else
1459 lc->maximum_buffer_size = rescale(
1460 (int)oxcf->maximum_buffer_size_in_ms,
1461 lc->target_bandwidth, 1000);
1462
1463 /* Work out the average size of a frame within this layer */
1464 if (i > 0)
1465 lc->avg_frame_size_for_layer =
1466 (int)((oxcf->target_bitrate[i] -
1467 oxcf->target_bitrate[i-1]) * 1000 /
1468 (lc->framerate - prev_layer_framerate));
1469
1470 prev_layer_framerate = lc->framerate;
1471 }
1472 }
1473 }
1474
vp8_change_config(VP8_COMP * cpi,VP8_CONFIG * oxcf)1475 void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
1476 {
1477 VP8_COMMON *cm = &cpi->common;
1478 int last_w, last_h, prev_number_of_layers;
1479
1480 if (!cpi)
1481 return;
1482
1483 if (!oxcf)
1484 return;
1485
1486 #if CONFIG_MULTITHREAD
1487 /* wait for the last picture loopfilter thread done */
1488 if (cpi->b_lpf_running)
1489 {
1490 sem_wait(&cpi->h_event_end_lpf);
1491 cpi->b_lpf_running = 0;
1492 }
1493 #endif
1494
1495 if (cm->version != oxcf->Version)
1496 {
1497 cm->version = oxcf->Version;
1498 vp8_setup_version(cm);
1499 }
1500
1501 last_w = cpi->oxcf.Width;
1502 last_h = cpi->oxcf.Height;
1503 prev_number_of_layers = cpi->oxcf.number_of_layers;
1504
1505 cpi->oxcf = *oxcf;
1506
1507 switch (cpi->oxcf.Mode)
1508 {
1509
1510 case MODE_REALTIME:
1511 cpi->pass = 0;
1512 cpi->compressor_speed = 2;
1513
1514 if (cpi->oxcf.cpu_used < -16)
1515 {
1516 cpi->oxcf.cpu_used = -16;
1517 }
1518
1519 if (cpi->oxcf.cpu_used > 16)
1520 cpi->oxcf.cpu_used = 16;
1521
1522 break;
1523
1524 case MODE_GOODQUALITY:
1525 cpi->pass = 0;
1526 cpi->compressor_speed = 1;
1527
1528 if (cpi->oxcf.cpu_used < -5)
1529 {
1530 cpi->oxcf.cpu_used = -5;
1531 }
1532
1533 if (cpi->oxcf.cpu_used > 5)
1534 cpi->oxcf.cpu_used = 5;
1535
1536 break;
1537
1538 case MODE_BESTQUALITY:
1539 cpi->pass = 0;
1540 cpi->compressor_speed = 0;
1541 break;
1542
1543 case MODE_FIRSTPASS:
1544 cpi->pass = 1;
1545 cpi->compressor_speed = 1;
1546 break;
1547 case MODE_SECONDPASS:
1548 cpi->pass = 2;
1549 cpi->compressor_speed = 1;
1550
1551 if (cpi->oxcf.cpu_used < -5)
1552 {
1553 cpi->oxcf.cpu_used = -5;
1554 }
1555
1556 if (cpi->oxcf.cpu_used > 5)
1557 cpi->oxcf.cpu_used = 5;
1558
1559 break;
1560 case MODE_SECONDPASS_BEST:
1561 cpi->pass = 2;
1562 cpi->compressor_speed = 0;
1563 break;
1564 }
1565
1566 if (cpi->pass == 0)
1567 cpi->auto_worst_q = 1;
1568
1569 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1570 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1571 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1572
1573 if (oxcf->fixed_q >= 0)
1574 {
1575 if (oxcf->worst_allowed_q < 0)
1576 cpi->oxcf.fixed_q = q_trans[0];
1577 else
1578 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
1579
1580 if (oxcf->alt_q < 0)
1581 cpi->oxcf.alt_q = q_trans[0];
1582 else
1583 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
1584
1585 if (oxcf->key_q < 0)
1586 cpi->oxcf.key_q = q_trans[0];
1587 else
1588 cpi->oxcf.key_q = q_trans[oxcf->key_q];
1589
1590 if (oxcf->gold_q < 0)
1591 cpi->oxcf.gold_q = q_trans[0];
1592 else
1593 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
1594
1595 }
1596
1597 cpi->baseline_gf_interval =
1598 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
1599
1600 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
1601
1602 cm->refresh_golden_frame = 0;
1603 cm->refresh_last_frame = 1;
1604 cm->refresh_entropy_probs = 1;
1605
1606 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1607 cpi->oxcf.token_partitions = 3;
1608 #endif
1609
1610 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
1611 cm->multi_token_partition =
1612 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
1613
1614 setup_features(cpi);
1615
1616 {
1617 int i;
1618
1619 for (i = 0; i < MAX_MB_SEGMENTS; i++)
1620 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1621 }
1622
1623 /* At the moment the first order values may not be > MAXQ */
1624 if (cpi->oxcf.fixed_q > MAXQ)
1625 cpi->oxcf.fixed_q = MAXQ;
1626
1627 /* local file playback mode == really big buffer */
1628 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
1629 {
1630 cpi->oxcf.starting_buffer_level = 60000;
1631 cpi->oxcf.optimal_buffer_level = 60000;
1632 cpi->oxcf.maximum_buffer_size = 240000;
1633 cpi->oxcf.starting_buffer_level_in_ms = 60000;
1634 cpi->oxcf.optimal_buffer_level_in_ms = 60000;
1635 cpi->oxcf.maximum_buffer_size_in_ms = 240000;
1636 }
1637
1638 /* Convert target bandwidth from Kbit/s to Bit/s */
1639 cpi->oxcf.target_bandwidth *= 1000;
1640
1641 cpi->oxcf.starting_buffer_level =
1642 rescale((int)cpi->oxcf.starting_buffer_level,
1643 cpi->oxcf.target_bandwidth, 1000);
1644
1645 /* Set or reset optimal and maximum buffer levels. */
1646 if (cpi->oxcf.optimal_buffer_level == 0)
1647 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1648 else
1649 cpi->oxcf.optimal_buffer_level =
1650 rescale((int)cpi->oxcf.optimal_buffer_level,
1651 cpi->oxcf.target_bandwidth, 1000);
1652
1653 if (cpi->oxcf.maximum_buffer_size == 0)
1654 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1655 else
1656 cpi->oxcf.maximum_buffer_size =
1657 rescale((int)cpi->oxcf.maximum_buffer_size,
1658 cpi->oxcf.target_bandwidth, 1000);
1659 // Under a configuration change, where maximum_buffer_size may change,
1660 // keep buffer level clipped to the maximum allowed buffer size.
1661 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
1662 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
1663 cpi->buffer_level = cpi->bits_off_target;
1664 }
1665
1666 /* Set up frame rate and related parameters rate control values. */
1667 vp8_new_framerate(cpi, cpi->framerate);
1668
1669 /* Set absolute upper and lower quality limits */
1670 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1671 cpi->best_quality = cpi->oxcf.best_allowed_q;
1672
1673 /* active values should only be modified if out of new range */
1674 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
1675 {
1676 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1677 }
1678 /* less likely */
1679 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
1680 {
1681 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1682 }
1683 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
1684 {
1685 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1686 }
1687 /* less likely */
1688 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
1689 {
1690 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
1691 }
1692
1693 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
1694
1695 cpi->cq_target_quality = cpi->oxcf.cq_level;
1696
1697 /* Only allow dropped frames in buffered mode */
1698 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1699
1700 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1701
1702 // Check if the number of temporal layers has changed, and if so reset the
1703 // pattern counter and set/initialize the temporal layer context for the
1704 // new layer configuration.
1705 if (cpi->oxcf.number_of_layers != prev_number_of_layers)
1706 {
1707 // If the number of temporal layers are changed we must start at the
1708 // base of the pattern cycle, so reset temporal_pattern_counter.
1709 cpi->temporal_pattern_counter = 0;
1710 reset_temporal_layer_change(cpi, oxcf, prev_number_of_layers);
1711 }
1712
1713 cm->Width = cpi->oxcf.Width;
1714 cm->Height = cpi->oxcf.Height;
1715
1716 /* TODO(jkoleszar): if an internal spatial resampling is active,
1717 * and we downsize the input image, maybe we should clear the
1718 * internal scale immediately rather than waiting for it to
1719 * correct.
1720 */
1721
1722 /* VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) */
1723 if (cpi->oxcf.Sharpness > 7)
1724 cpi->oxcf.Sharpness = 7;
1725
1726 cm->sharpness_level = cpi->oxcf.Sharpness;
1727
1728 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
1729 {
1730 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
1731 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
1732
1733 Scale2Ratio(cm->horiz_scale, &hr, &hs);
1734 Scale2Ratio(cm->vert_scale, &vr, &vs);
1735
1736 /* always go to the next whole number */
1737 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
1738 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
1739 }
1740
1741 if (last_w != cpi->oxcf.Width || last_h != cpi->oxcf.Height)
1742 cpi->force_next_frame_intra = 1;
1743
1744 if (((cm->Width + 15) & 0xfffffff0) !=
1745 cm->yv12_fb[cm->lst_fb_idx].y_width ||
1746 ((cm->Height + 15) & 0xfffffff0) !=
1747 cm->yv12_fb[cm->lst_fb_idx].y_height ||
1748 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
1749 {
1750 dealloc_raw_frame_buffers(cpi);
1751 alloc_raw_frame_buffers(cpi);
1752 vp8_alloc_compressor_data(cpi);
1753 }
1754
1755 if (cpi->oxcf.fixed_q >= 0)
1756 {
1757 cpi->last_q[0] = cpi->oxcf.fixed_q;
1758 cpi->last_q[1] = cpi->oxcf.fixed_q;
1759 }
1760
1761 cpi->Speed = cpi->oxcf.cpu_used;
1762
1763 /* force to allowlag to 0 if lag_in_frames is 0; */
1764 if (cpi->oxcf.lag_in_frames == 0)
1765 {
1766 cpi->oxcf.allow_lag = 0;
1767 }
1768 /* Limit on lag buffers as these are not currently dynamically allocated */
1769 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
1770 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1771
1772 /* YX Temp */
1773 cpi->alt_ref_source = NULL;
1774 cpi->is_src_frame_alt_ref = 0;
1775
1776 #if CONFIG_TEMPORAL_DENOISING
1777 if (cpi->oxcf.noise_sensitivity)
1778 {
1779 if (!cpi->denoiser.yv12_mc_running_avg.buffer_alloc)
1780 {
1781 int width = (cpi->oxcf.Width + 15) & ~15;
1782 int height = (cpi->oxcf.Height + 15) & ~15;
1783 vp8_denoiser_allocate(&cpi->denoiser, width, height,
1784 cm->mb_rows, cm->mb_cols,
1785 cpi->oxcf.noise_sensitivity);
1786 }
1787 }
1788 #endif
1789
1790 #if 0
1791 /* Experimental RD Code */
1792 cpi->frame_distortion = 0;
1793 cpi->last_frame_distortion = 0;
1794 #endif
1795
1796 }
1797
1798 #ifndef M_LOG2_E
1799 #define M_LOG2_E 0.693147180559945309417
1800 #endif
1801 #define log2f(x) (log (x) / (float) M_LOG2_E)
1802
cal_mvsadcosts(int * mvsadcost[2])1803 static void cal_mvsadcosts(int *mvsadcost[2])
1804 {
1805 int i = 1;
1806
1807 mvsadcost [0] [0] = 300;
1808 mvsadcost [1] [0] = 300;
1809
1810 do
1811 {
1812 double z = 256 * (2 * (log2f(8 * i) + .6));
1813 mvsadcost [0][i] = (int) z;
1814 mvsadcost [1][i] = (int) z;
1815 mvsadcost [0][-i] = (int) z;
1816 mvsadcost [1][-i] = (int) z;
1817 }
1818 while (++i <= mvfp_max);
1819 }
1820
vp8_create_compressor(VP8_CONFIG * oxcf)1821 struct VP8_COMP* vp8_create_compressor(VP8_CONFIG *oxcf)
1822 {
1823 int i;
1824
1825 VP8_COMP *cpi;
1826 VP8_COMMON *cm;
1827
1828 cpi = vpx_memalign(32, sizeof(VP8_COMP));
1829 /* Check that the CPI instance is valid */
1830 if (!cpi)
1831 return 0;
1832
1833 cm = &cpi->common;
1834
1835 vpx_memset(cpi, 0, sizeof(VP8_COMP));
1836
1837 if (setjmp(cm->error.jmp))
1838 {
1839 cpi->common.error.setjmp = 0;
1840 vp8_remove_compressor(&cpi);
1841 return 0;
1842 }
1843
1844 cpi->common.error.setjmp = 1;
1845
1846 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1847
1848 vp8_create_common(&cpi->common);
1849
1850 init_config(cpi, oxcf);
1851
1852 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
1853 cpi->common.current_video_frame = 0;
1854 cpi->temporal_pattern_counter = 0;
1855 cpi->kf_overspend_bits = 0;
1856 cpi->kf_bitrate_adjustment = 0;
1857 cpi->frames_till_gf_update_due = 0;
1858 cpi->gf_overspend_bits = 0;
1859 cpi->non_gf_bitrate_adjustment = 0;
1860 cpi->prob_last_coded = 128;
1861 cpi->prob_gf_coded = 128;
1862 cpi->prob_intra_coded = 63;
1863
1864 /* Prime the recent reference frame usage counters.
1865 * Hereafter they will be maintained as a sort of moving average
1866 */
1867 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1868 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1869 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1870 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1871
1872 /* Set reference frame sign bias for ALTREF frame to 1 (for now) */
1873 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1874
1875 cpi->twopass.gf_decay_rate = 0;
1876 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1877
1878 cpi->gold_is_last = 0 ;
1879 cpi->alt_is_last = 0 ;
1880 cpi->gold_is_alt = 0 ;
1881
1882 cpi->active_map_enabled = 0;
1883
1884 #if 0
1885 /* Experimental code for lagged and one pass */
1886 /* Initialise one_pass GF frames stats */
1887 /* Update stats used for GF selection */
1888 if (cpi->pass == 0)
1889 {
1890 cpi->one_pass_frame_index = 0;
1891
1892 for (i = 0; i < MAX_LAG_BUFFERS; i++)
1893 {
1894 cpi->one_pass_frame_stats[i].frames_so_far = 0;
1895 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
1896 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
1897 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
1898 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
1899 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
1900 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
1901 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
1902 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
1903 }
1904 }
1905 #endif
1906
1907 /* Should we use the cyclic refresh method.
1908 * Currently this is tied to error resilliant mode
1909 */
1910 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
1911 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 5;
1912 if (cpi->oxcf.number_of_layers == 1) {
1913 cpi->cyclic_refresh_mode_max_mbs_perframe =
1914 (cpi->common.mb_rows * cpi->common.mb_cols) / 20;
1915 } else if (cpi->oxcf.number_of_layers == 2) {
1916 cpi->cyclic_refresh_mode_max_mbs_perframe =
1917 (cpi->common.mb_rows * cpi->common.mb_cols) / 10;
1918 }
1919 cpi->cyclic_refresh_mode_index = 0;
1920 cpi->cyclic_refresh_q = 32;
1921
1922 if (cpi->cyclic_refresh_mode_enabled)
1923 {
1924 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1925 }
1926 else
1927 cpi->cyclic_refresh_map = (signed char *) NULL;
1928
1929 CHECK_MEM_ERROR(cpi->consec_zero_last,
1930 vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
1931
1932 #ifdef VP8_ENTROPY_STATS
1933 init_context_counters();
1934 #endif
1935
1936 /*Initialize the feed-forward activity masking.*/
1937 cpi->activity_avg = 90<<12;
1938
1939 /* Give a sensible default for the first frame. */
1940 cpi->frames_since_key = 8;
1941 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1942 cpi->this_key_frame_forced = 0;
1943 cpi->next_key_frame_forced = 0;
1944
1945 cpi->source_alt_ref_pending = 0;
1946 cpi->source_alt_ref_active = 0;
1947 cpi->common.refresh_alt_ref_frame = 0;
1948
1949 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1950 #if CONFIG_INTERNAL_STATS
1951 cpi->b_calculate_ssimg = 0;
1952
1953 cpi->count = 0;
1954 cpi->bytes = 0;
1955
1956 if (cpi->b_calculate_psnr)
1957 {
1958 cpi->total_sq_error = 0.0;
1959 cpi->total_sq_error2 = 0.0;
1960 cpi->total_y = 0.0;
1961 cpi->total_u = 0.0;
1962 cpi->total_v = 0.0;
1963 cpi->total = 0.0;
1964 cpi->totalp_y = 0.0;
1965 cpi->totalp_u = 0.0;
1966 cpi->totalp_v = 0.0;
1967 cpi->totalp = 0.0;
1968 cpi->tot_recode_hits = 0;
1969 cpi->summed_quality = 0;
1970 cpi->summed_weights = 0;
1971 }
1972
1973 if (cpi->b_calculate_ssimg)
1974 {
1975 cpi->total_ssimg_y = 0;
1976 cpi->total_ssimg_u = 0;
1977 cpi->total_ssimg_v = 0;
1978 cpi->total_ssimg_all = 0;
1979 }
1980
1981 #endif
1982
1983 cpi->first_time_stamp_ever = 0x7FFFFFFF;
1984
1985 cpi->frames_till_gf_update_due = 0;
1986 cpi->key_frame_count = 1;
1987
1988 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1989 cpi->ni_tot_qi = 0;
1990 cpi->ni_frames = 0;
1991 cpi->total_byte_count = 0;
1992
1993 cpi->drop_frame = 0;
1994
1995 cpi->rate_correction_factor = 1.0;
1996 cpi->key_frame_rate_correction_factor = 1.0;
1997 cpi->gf_rate_correction_factor = 1.0;
1998 cpi->twopass.est_max_qcorrection_factor = 1.0;
1999
2000 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
2001 {
2002 cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate;
2003 }
2004
2005 #ifdef OUTPUT_YUV_SRC
2006 yuv_file = fopen("bd.yuv", "ab");
2007 #endif
2008 #ifdef OUTPUT_YUV_DENOISED
2009 yuv_denoised_file = fopen("denoised.yuv", "ab");
2010 #endif
2011
2012 #if 0
2013 framepsnr = fopen("framepsnr.stt", "a");
2014 kf_list = fopen("kf_list.stt", "w");
2015 #endif
2016
2017 cpi->output_pkt_list = oxcf->output_pkt_list;
2018
2019 #if !(CONFIG_REALTIME_ONLY)
2020
2021 if (cpi->pass == 1)
2022 {
2023 vp8_init_first_pass(cpi);
2024 }
2025 else if (cpi->pass == 2)
2026 {
2027 size_t packet_sz = sizeof(FIRSTPASS_STATS);
2028 int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2029
2030 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2031 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2032 cpi->twopass.stats_in_end = (void*)((char *)cpi->twopass.stats_in
2033 + (packets - 1) * packet_sz);
2034 vp8_init_second_pass(cpi);
2035 }
2036
2037 #endif
2038
2039 if (cpi->compressor_speed == 2)
2040 {
2041 cpi->avg_encode_time = 0;
2042 cpi->avg_pick_mode_time = 0;
2043 }
2044
2045 vp8_set_speed_features(cpi);
2046
2047 /* Set starting values of RD threshold multipliers (128 = *1) */
2048 for (i = 0; i < MAX_MODES; i++)
2049 {
2050 cpi->mb.rd_thresh_mult[i] = 128;
2051 }
2052
2053 #ifdef VP8_ENTROPY_STATS
2054 init_mv_ref_counts();
2055 #endif
2056
2057 #if CONFIG_MULTITHREAD
2058 if(vp8cx_create_encoder_threads(cpi))
2059 {
2060 vp8_remove_compressor(&cpi);
2061 return 0;
2062 }
2063 #endif
2064
2065 cpi->fn_ptr[BLOCK_16X16].sdf = vp8_sad16x16;
2066 cpi->fn_ptr[BLOCK_16X16].vf = vp8_variance16x16;
2067 cpi->fn_ptr[BLOCK_16X16].svf = vp8_sub_pixel_variance16x16;
2068 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = vp8_variance_halfpixvar16x16_h;
2069 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = vp8_variance_halfpixvar16x16_v;
2070 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = vp8_variance_halfpixvar16x16_hv;
2071 cpi->fn_ptr[BLOCK_16X16].sdx3f = vp8_sad16x16x3;
2072 cpi->fn_ptr[BLOCK_16X16].sdx8f = vp8_sad16x16x8;
2073 cpi->fn_ptr[BLOCK_16X16].sdx4df = vp8_sad16x16x4d;
2074
2075 cpi->fn_ptr[BLOCK_16X8].sdf = vp8_sad16x8;
2076 cpi->fn_ptr[BLOCK_16X8].vf = vp8_variance16x8;
2077 cpi->fn_ptr[BLOCK_16X8].svf = vp8_sub_pixel_variance16x8;
2078 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
2079 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
2080 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
2081 cpi->fn_ptr[BLOCK_16X8].sdx3f = vp8_sad16x8x3;
2082 cpi->fn_ptr[BLOCK_16X8].sdx8f = vp8_sad16x8x8;
2083 cpi->fn_ptr[BLOCK_16X8].sdx4df = vp8_sad16x8x4d;
2084
2085 cpi->fn_ptr[BLOCK_8X16].sdf = vp8_sad8x16;
2086 cpi->fn_ptr[BLOCK_8X16].vf = vp8_variance8x16;
2087 cpi->fn_ptr[BLOCK_8X16].svf = vp8_sub_pixel_variance8x16;
2088 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
2089 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
2090 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
2091 cpi->fn_ptr[BLOCK_8X16].sdx3f = vp8_sad8x16x3;
2092 cpi->fn_ptr[BLOCK_8X16].sdx8f = vp8_sad8x16x8;
2093 cpi->fn_ptr[BLOCK_8X16].sdx4df = vp8_sad8x16x4d;
2094
2095 cpi->fn_ptr[BLOCK_8X8].sdf = vp8_sad8x8;
2096 cpi->fn_ptr[BLOCK_8X8].vf = vp8_variance8x8;
2097 cpi->fn_ptr[BLOCK_8X8].svf = vp8_sub_pixel_variance8x8;
2098 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
2099 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
2100 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
2101 cpi->fn_ptr[BLOCK_8X8].sdx3f = vp8_sad8x8x3;
2102 cpi->fn_ptr[BLOCK_8X8].sdx8f = vp8_sad8x8x8;
2103 cpi->fn_ptr[BLOCK_8X8].sdx4df = vp8_sad8x8x4d;
2104
2105 cpi->fn_ptr[BLOCK_4X4].sdf = vp8_sad4x4;
2106 cpi->fn_ptr[BLOCK_4X4].vf = vp8_variance4x4;
2107 cpi->fn_ptr[BLOCK_4X4].svf = vp8_sub_pixel_variance4x4;
2108 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
2109 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
2110 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
2111 cpi->fn_ptr[BLOCK_4X4].sdx3f = vp8_sad4x4x3;
2112 cpi->fn_ptr[BLOCK_4X4].sdx8f = vp8_sad4x4x8;
2113 cpi->fn_ptr[BLOCK_4X4].sdx4df = vp8_sad4x4x4d;
2114
2115 #if ARCH_X86 || ARCH_X86_64
2116 cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn;
2117 cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn;
2118 cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn;
2119 cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn;
2120 cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn;
2121 #endif
2122
2123 cpi->full_search_sad = vp8_full_search_sad;
2124 cpi->diamond_search_sad = vp8_diamond_search_sad;
2125 cpi->refining_search_sad = vp8_refining_search_sad;
2126
2127 /* make sure frame 1 is okay */
2128 cpi->mb.error_bins[0] = cpi->common.MBs;
2129
2130 /* vp8cx_init_quantizer() is first called here. Add check in
2131 * vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only
2132 * called later when needed. This will avoid unnecessary calls of
2133 * vp8cx_init_quantizer() for every frame.
2134 */
2135 vp8cx_init_quantizer(cpi);
2136
2137 vp8_loop_filter_init(cm);
2138
2139 cpi->common.error.setjmp = 0;
2140
2141 #if CONFIG_MULTI_RES_ENCODING
2142
2143 /* Calculate # of MBs in a row in lower-resolution level image. */
2144 if (cpi->oxcf.mr_encoder_id > 0)
2145 vp8_cal_low_res_mb_cols(cpi);
2146
2147 #endif
2148
2149 /* setup RD costs to MACROBLOCK struct */
2150
2151 cpi->mb.mvcost[0] = &cpi->rd_costs.mvcosts[0][mv_max+1];
2152 cpi->mb.mvcost[1] = &cpi->rd_costs.mvcosts[1][mv_max+1];
2153 cpi->mb.mvsadcost[0] = &cpi->rd_costs.mvsadcosts[0][mvfp_max+1];
2154 cpi->mb.mvsadcost[1] = &cpi->rd_costs.mvsadcosts[1][mvfp_max+1];
2155
2156 cal_mvsadcosts(cpi->mb.mvsadcost);
2157
2158 cpi->mb.mbmode_cost = cpi->rd_costs.mbmode_cost;
2159 cpi->mb.intra_uv_mode_cost = cpi->rd_costs.intra_uv_mode_cost;
2160 cpi->mb.bmode_costs = cpi->rd_costs.bmode_costs;
2161 cpi->mb.inter_bmode_costs = cpi->rd_costs.inter_bmode_costs;
2162 cpi->mb.token_costs = cpi->rd_costs.token_costs;
2163
2164 /* setup block ptrs & offsets */
2165 vp8_setup_block_ptrs(&cpi->mb);
2166 vp8_setup_block_dptrs(&cpi->mb.e_mbd);
2167
2168 return cpi;
2169 }
2170
2171
vp8_remove_compressor(VP8_COMP ** ptr)2172 void vp8_remove_compressor(VP8_COMP **ptr)
2173 {
2174 VP8_COMP *cpi = *ptr;
2175
2176 if (!cpi)
2177 return;
2178
2179 if (cpi && (cpi->common.current_video_frame > 0))
2180 {
2181 #if !(CONFIG_REALTIME_ONLY)
2182
2183 if (cpi->pass == 2)
2184 {
2185 vp8_end_second_pass(cpi);
2186 }
2187
2188 #endif
2189
2190 #ifdef VP8_ENTROPY_STATS
2191 print_context_counters();
2192 print_tree_update_probs();
2193 print_mode_context();
2194 #endif
2195
2196 #if CONFIG_INTERNAL_STATS
2197
2198 if (cpi->pass != 1)
2199 {
2200 FILE *f = fopen("opsnr.stt", "a");
2201 double time_encoded = (cpi->last_end_time_stamp_seen
2202 - cpi->first_time_stamp_ever) / 10000000.000;
2203 double total_encode_time = (cpi->time_receive_data +
2204 cpi->time_compress_data) / 1000.000;
2205 double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded;
2206
2207 if (cpi->b_calculate_psnr)
2208 {
2209 YV12_BUFFER_CONFIG *lst_yv12 =
2210 &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
2211
2212 if (cpi->oxcf.number_of_layers > 1)
2213 {
2214 int i;
2215
2216 fprintf(f, "Layer\tBitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2217 "GLPsnrP\tVPXSSIM\t\n");
2218 for (i=0; i<(int)cpi->oxcf.number_of_layers; i++)
2219 {
2220 double dr = (double)cpi->bytes_in_layer[i] *
2221 8.0 / 1000.0 / time_encoded;
2222 double samples = 3.0 / 2 * cpi->frames_in_layer[i] *
2223 lst_yv12->y_width * lst_yv12->y_height;
2224 double total_psnr =
2225 vpx_sse_to_psnr(samples, 255.0,
2226 cpi->total_error2[i]);
2227 double total_psnr2 =
2228 vpx_sse_to_psnr(samples, 255.0,
2229 cpi->total_error2_p[i]);
2230 double total_ssim = 100 * pow(cpi->sum_ssim[i] /
2231 cpi->sum_weights[i], 8.0);
2232
2233 fprintf(f, "%5d\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2234 "%7.3f\t%7.3f\n",
2235 i, dr,
2236 cpi->sum_psnr[i] / cpi->frames_in_layer[i],
2237 total_psnr,
2238 cpi->sum_psnr_p[i] / cpi->frames_in_layer[i],
2239 total_psnr2, total_ssim);
2240 }
2241 }
2242 else
2243 {
2244 double samples = 3.0 / 2 * cpi->count *
2245 lst_yv12->y_width * lst_yv12->y_height;
2246 double total_psnr = vpx_sse_to_psnr(samples, 255.0,
2247 cpi->total_sq_error);
2248 double total_psnr2 = vpx_sse_to_psnr(samples, 255.0,
2249 cpi->total_sq_error2);
2250 double total_ssim = 100 * pow(cpi->summed_quality /
2251 cpi->summed_weights, 8.0);
2252
2253 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
2254 "GLPsnrP\tVPXSSIM\t Time(us)\n");
2255 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2256 "%7.3f\t%8.0f\n",
2257 dr, cpi->total / cpi->count, total_psnr,
2258 cpi->totalp / cpi->count, total_psnr2,
2259 total_ssim, total_encode_time);
2260 }
2261 }
2262
2263 if (cpi->b_calculate_ssimg)
2264 {
2265 if (cpi->oxcf.number_of_layers > 1)
2266 {
2267 int i;
2268
2269 fprintf(f, "Layer\tBitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2270 "Time(us)\n");
2271 for (i=0; i<(int)cpi->oxcf.number_of_layers; i++)
2272 {
2273 double dr = (double)cpi->bytes_in_layer[i] *
2274 8.0 / 1000.0 / time_encoded;
2275 fprintf(f, "%5d\t%7.3f\t%6.4f\t"
2276 "%6.4f\t%6.4f\t%6.4f\t%8.0f\n",
2277 i, dr,
2278 cpi->total_ssimg_y_in_layer[i] /
2279 cpi->frames_in_layer[i],
2280 cpi->total_ssimg_u_in_layer[i] /
2281 cpi->frames_in_layer[i],
2282 cpi->total_ssimg_v_in_layer[i] /
2283 cpi->frames_in_layer[i],
2284 cpi->total_ssimg_all_in_layer[i] /
2285 cpi->frames_in_layer[i],
2286 total_encode_time);
2287 }
2288 }
2289 else
2290 {
2291 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
2292 "Time(us)\n");
2293 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2294 cpi->total_ssimg_y / cpi->count,
2295 cpi->total_ssimg_u / cpi->count,
2296 cpi->total_ssimg_v / cpi->count,
2297 cpi->total_ssimg_all / cpi->count, total_encode_time);
2298 }
2299 }
2300
2301 fclose(f);
2302 #if 0
2303 f = fopen("qskip.stt", "a");
2304 fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
2305 fclose(f);
2306 #endif
2307
2308 }
2309
2310 #endif
2311
2312
2313 #ifdef SPEEDSTATS
2314
2315 if (cpi->compressor_speed == 2)
2316 {
2317 int i;
2318 FILE *f = fopen("cxspeed.stt", "a");
2319 cnt_pm /= cpi->common.MBs;
2320
2321 for (i = 0; i < 16; i++)
2322 fprintf(f, "%5d", frames_at_speed[i]);
2323
2324 fprintf(f, "\n");
2325 fclose(f);
2326 }
2327
2328 #endif
2329
2330
2331 #ifdef MODE_STATS
2332 {
2333 extern int count_mb_seg[4];
2334 FILE *f = fopen("modes.stt", "a");
2335 double dr = (double)cpi->framerate * (double)bytes * (double)8 / (double)count / (double)1000 ;
2336 fprintf(f, "intra_mode in Intra Frames:\n");
2337 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
2338 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
2339 fprintf(f, "B: ");
2340 {
2341 int i;
2342
2343 for (i = 0; i < 10; i++)
2344 fprintf(f, "%8d, ", b_modes[i]);
2345
2346 fprintf(f, "\n");
2347
2348 }
2349
2350 fprintf(f, "Modes in Inter Frames:\n");
2351 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
2352 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
2353 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
2354 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
2355 fprintf(f, "B: ");
2356 {
2357 int i;
2358
2359 for (i = 0; i < 15; i++)
2360 fprintf(f, "%8d, ", inter_b_modes[i]);
2361
2362 fprintf(f, "\n");
2363
2364 }
2365 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
2366 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
2367
2368
2369
2370 fclose(f);
2371 }
2372 #endif
2373
2374 #ifdef VP8_ENTROPY_STATS
2375 {
2376 int i, j, k;
2377 FILE *fmode = fopen("modecontext.c", "w");
2378
2379 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
2380 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
2381 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
2382
2383 for (i = 0; i < 10; i++)
2384 {
2385
2386 fprintf(fmode, " { /* Above Mode : %d */\n", i);
2387
2388 for (j = 0; j < 10; j++)
2389 {
2390
2391 fprintf(fmode, " {");
2392
2393 for (k = 0; k < 10; k++)
2394 {
2395 if (!intra_mode_stats[i][j][k])
2396 fprintf(fmode, " %5d, ", 1);
2397 else
2398 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
2399 }
2400
2401 fprintf(fmode, "}, /* left_mode %d */\n", j);
2402
2403 }
2404
2405 fprintf(fmode, " },\n");
2406
2407 }
2408
2409 fprintf(fmode, "};\n");
2410 fclose(fmode);
2411 }
2412 #endif
2413
2414
2415 #if defined(SECTIONBITS_OUTPUT)
2416
2417 if (0)
2418 {
2419 int i;
2420 FILE *f = fopen("tokenbits.stt", "a");
2421
2422 for (i = 0; i < 28; i++)
2423 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
2424
2425 fprintf(f, "\n");
2426 fclose(f);
2427 }
2428
2429 #endif
2430
2431 #if 0
2432 {
2433 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2434 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2435 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);
2436 }
2437 #endif
2438
2439 }
2440
2441 #if CONFIG_MULTITHREAD
2442 vp8cx_remove_encoder_threads(cpi);
2443 #endif
2444
2445 #if CONFIG_TEMPORAL_DENOISING
2446 vp8_denoiser_free(&cpi->denoiser);
2447 #endif
2448 dealloc_compressor_data(cpi);
2449 vpx_free(cpi->mb.ss);
2450 vpx_free(cpi->tok);
2451 vpx_free(cpi->cyclic_refresh_map);
2452 vpx_free(cpi->consec_zero_last);
2453
2454 vp8_remove_common(&cpi->common);
2455 vpx_free(cpi);
2456 *ptr = 0;
2457
2458 #ifdef OUTPUT_YUV_SRC
2459 fclose(yuv_file);
2460 #endif
2461 #ifdef OUTPUT_YUV_DENOISED
2462 fclose(yuv_denoised_file);
2463 #endif
2464
2465 #if 0
2466
2467 if (keyfile)
2468 fclose(keyfile);
2469
2470 if (framepsnr)
2471 fclose(framepsnr);
2472
2473 if (kf_list)
2474 fclose(kf_list);
2475
2476 #endif
2477
2478 }
2479
2480
calc_plane_error(unsigned char * orig,int orig_stride,unsigned char * recon,int recon_stride,unsigned int cols,unsigned int rows)2481 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
2482 unsigned char *recon, int recon_stride,
2483 unsigned int cols, unsigned int rows)
2484 {
2485 unsigned int row, col;
2486 uint64_t total_sse = 0;
2487 int diff;
2488
2489 for (row = 0; row + 16 <= rows; row += 16)
2490 {
2491 for (col = 0; col + 16 <= cols; col += 16)
2492 {
2493 unsigned int sse;
2494
2495 vp8_mse16x16(orig + col, orig_stride,
2496 recon + col, recon_stride,
2497 &sse);
2498 total_sse += sse;
2499 }
2500
2501 /* Handle odd-sized width */
2502 if (col < cols)
2503 {
2504 unsigned int border_row, border_col;
2505 unsigned char *border_orig = orig;
2506 unsigned char *border_recon = recon;
2507
2508 for (border_row = 0; border_row < 16; border_row++)
2509 {
2510 for (border_col = col; border_col < cols; border_col++)
2511 {
2512 diff = border_orig[border_col] - border_recon[border_col];
2513 total_sse += diff * diff;
2514 }
2515
2516 border_orig += orig_stride;
2517 border_recon += recon_stride;
2518 }
2519 }
2520
2521 orig += orig_stride * 16;
2522 recon += recon_stride * 16;
2523 }
2524
2525 /* Handle odd-sized height */
2526 for (; row < rows; row++)
2527 {
2528 for (col = 0; col < cols; col++)
2529 {
2530 diff = orig[col] - recon[col];
2531 total_sse += diff * diff;
2532 }
2533
2534 orig += orig_stride;
2535 recon += recon_stride;
2536 }
2537
2538 vp8_clear_system_state();
2539 return total_sse;
2540 }
2541
2542
generate_psnr_packet(VP8_COMP * cpi)2543 static void generate_psnr_packet(VP8_COMP *cpi)
2544 {
2545 YV12_BUFFER_CONFIG *orig = cpi->Source;
2546 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2547 struct vpx_codec_cx_pkt pkt;
2548 uint64_t sse;
2549 int i;
2550 unsigned int width = cpi->common.Width;
2551 unsigned int height = cpi->common.Height;
2552
2553 pkt.kind = VPX_CODEC_PSNR_PKT;
2554 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
2555 recon->y_buffer, recon->y_stride,
2556 width, height);
2557 pkt.data.psnr.sse[0] = sse;
2558 pkt.data.psnr.sse[1] = sse;
2559 pkt.data.psnr.samples[0] = width * height;
2560 pkt.data.psnr.samples[1] = width * height;
2561
2562 width = (width + 1) / 2;
2563 height = (height + 1) / 2;
2564
2565 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
2566 recon->u_buffer, recon->uv_stride,
2567 width, height);
2568 pkt.data.psnr.sse[0] += sse;
2569 pkt.data.psnr.sse[2] = sse;
2570 pkt.data.psnr.samples[0] += width * height;
2571 pkt.data.psnr.samples[2] = width * height;
2572
2573 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
2574 recon->v_buffer, recon->uv_stride,
2575 width, height);
2576 pkt.data.psnr.sse[0] += sse;
2577 pkt.data.psnr.sse[3] = sse;
2578 pkt.data.psnr.samples[0] += width * height;
2579 pkt.data.psnr.samples[3] = width * height;
2580
2581 for (i = 0; i < 4; i++)
2582 pkt.data.psnr.psnr[i] = vpx_sse_to_psnr(pkt.data.psnr.samples[i], 255.0,
2583 (double)(pkt.data.psnr.sse[i]));
2584
2585 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2586 }
2587
2588
vp8_use_as_reference(VP8_COMP * cpi,int ref_frame_flags)2589 int vp8_use_as_reference(VP8_COMP *cpi, int ref_frame_flags)
2590 {
2591 if (ref_frame_flags > 7)
2592 return -1 ;
2593
2594 cpi->ref_frame_flags = ref_frame_flags;
2595 return 0;
2596 }
vp8_update_reference(VP8_COMP * cpi,int ref_frame_flags)2597 int vp8_update_reference(VP8_COMP *cpi, int ref_frame_flags)
2598 {
2599 if (ref_frame_flags > 7)
2600 return -1 ;
2601
2602 cpi->common.refresh_golden_frame = 0;
2603 cpi->common.refresh_alt_ref_frame = 0;
2604 cpi->common.refresh_last_frame = 0;
2605
2606 if (ref_frame_flags & VP8_LAST_FRAME)
2607 cpi->common.refresh_last_frame = 1;
2608
2609 if (ref_frame_flags & VP8_GOLD_FRAME)
2610 cpi->common.refresh_golden_frame = 1;
2611
2612 if (ref_frame_flags & VP8_ALTR_FRAME)
2613 cpi->common.refresh_alt_ref_frame = 1;
2614
2615 return 0;
2616 }
2617
vp8_get_reference(VP8_COMP * cpi,enum vpx_ref_frame_type ref_frame_flag,YV12_BUFFER_CONFIG * sd)2618 int vp8_get_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2619 {
2620 VP8_COMMON *cm = &cpi->common;
2621 int ref_fb_idx;
2622
2623 if (ref_frame_flag == VP8_LAST_FRAME)
2624 ref_fb_idx = cm->lst_fb_idx;
2625 else if (ref_frame_flag == VP8_GOLD_FRAME)
2626 ref_fb_idx = cm->gld_fb_idx;
2627 else if (ref_frame_flag == VP8_ALTR_FRAME)
2628 ref_fb_idx = cm->alt_fb_idx;
2629 else
2630 return -1;
2631
2632 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
2633
2634 return 0;
2635 }
vp8_set_reference(VP8_COMP * cpi,enum vpx_ref_frame_type ref_frame_flag,YV12_BUFFER_CONFIG * sd)2636 int vp8_set_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
2637 {
2638 VP8_COMMON *cm = &cpi->common;
2639
2640 int ref_fb_idx;
2641
2642 if (ref_frame_flag == VP8_LAST_FRAME)
2643 ref_fb_idx = cm->lst_fb_idx;
2644 else if (ref_frame_flag == VP8_GOLD_FRAME)
2645 ref_fb_idx = cm->gld_fb_idx;
2646 else if (ref_frame_flag == VP8_ALTR_FRAME)
2647 ref_fb_idx = cm->alt_fb_idx;
2648 else
2649 return -1;
2650
2651 vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
2652
2653 return 0;
2654 }
vp8_update_entropy(VP8_COMP * cpi,int update)2655 int vp8_update_entropy(VP8_COMP *cpi, int update)
2656 {
2657 VP8_COMMON *cm = &cpi->common;
2658 cm->refresh_entropy_probs = update;
2659
2660 return 0;
2661 }
2662
2663
2664 #if defined(OUTPUT_YUV_SRC) || defined(OUTPUT_YUV_DENOISED)
vp8_write_yuv_frame(FILE * yuv_file,YV12_BUFFER_CONFIG * s)2665 void vp8_write_yuv_frame(FILE *yuv_file, YV12_BUFFER_CONFIG *s)
2666 {
2667 unsigned char *src = s->y_buffer;
2668 int h = s->y_height;
2669
2670 do
2671 {
2672 fwrite(src, s->y_width, 1, yuv_file);
2673 src += s->y_stride;
2674 }
2675 while (--h);
2676
2677 src = s->u_buffer;
2678 h = s->uv_height;
2679
2680 do
2681 {
2682 fwrite(src, s->uv_width, 1, yuv_file);
2683 src += s->uv_stride;
2684 }
2685 while (--h);
2686
2687 src = s->v_buffer;
2688 h = s->uv_height;
2689
2690 do
2691 {
2692 fwrite(src, s->uv_width, 1, yuv_file);
2693 src += s->uv_stride;
2694 }
2695 while (--h);
2696 }
2697 #endif
2698
scale_and_extend_source(YV12_BUFFER_CONFIG * sd,VP8_COMP * cpi)2699 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
2700 {
2701 VP8_COMMON *cm = &cpi->common;
2702
2703 /* are we resizing the image */
2704 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
2705 {
2706 #if CONFIG_SPATIAL_RESAMPLING
2707 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2708 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2709 int tmp_height;
2710
2711 if (cm->vert_scale == 3)
2712 tmp_height = 9;
2713 else
2714 tmp_height = 11;
2715
2716 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2717 Scale2Ratio(cm->vert_scale, &vr, &vs);
2718
2719 vpx_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
2720 tmp_height, hs, hr, vs, vr, 0);
2721
2722 vp8_yv12_extend_frame_borders(&cpi->scaled_source);
2723 cpi->Source = &cpi->scaled_source;
2724 #endif
2725 }
2726 else
2727 cpi->Source = sd;
2728 }
2729
2730
resize_key_frame(VP8_COMP * cpi)2731 static int resize_key_frame(VP8_COMP *cpi)
2732 {
2733 #if CONFIG_SPATIAL_RESAMPLING
2734 VP8_COMMON *cm = &cpi->common;
2735
2736 /* Do we need to apply resampling for one pass cbr.
2737 * In one pass this is more limited than in two pass cbr.
2738 * The test and any change is only made once per key frame sequence.
2739 */
2740 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
2741 {
2742 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
2743 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
2744 int new_width, new_height;
2745
2746 /* If we are below the resample DOWN watermark then scale down a
2747 * notch.
2748 */
2749 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2750 {
2751 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
2752 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
2753 }
2754 /* Should we now start scaling back up */
2755 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
2756 {
2757 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
2758 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
2759 }
2760
2761 /* Get the new height and width */
2762 Scale2Ratio(cm->horiz_scale, &hr, &hs);
2763 Scale2Ratio(cm->vert_scale, &vr, &vs);
2764 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
2765 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
2766
2767 /* If the image size has changed we need to reallocate the buffers
2768 * and resample the source image
2769 */
2770 if ((cm->Width != new_width) || (cm->Height != new_height))
2771 {
2772 cm->Width = new_width;
2773 cm->Height = new_height;
2774 vp8_alloc_compressor_data(cpi);
2775 scale_and_extend_source(cpi->un_scaled_source, cpi);
2776 return 1;
2777 }
2778 }
2779
2780 #endif
2781 return 0;
2782 }
2783
2784
update_alt_ref_frame_stats(VP8_COMP * cpi)2785 static void update_alt_ref_frame_stats(VP8_COMP *cpi)
2786 {
2787 VP8_COMMON *cm = &cpi->common;
2788
2789 /* Select an interval before next GF or altref */
2790 if (!cpi->auto_gold)
2791 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2792
2793 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
2794 {
2795 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2796
2797 /* Set the bits per frame that we should try and recover in
2798 * subsequent inter frames to account for the extra GF spend...
2799 * note that his does not apply for GF updates that occur
2800 * coincident with a key frame as the extra cost of key frames is
2801 * dealt with elsewhere.
2802 */
2803 cpi->gf_overspend_bits += cpi->projected_frame_size;
2804 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2805 }
2806
2807 /* Update data structure that monitors level of reference to last GF */
2808 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2809 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2810
2811 /* this frame refreshes means next frames don't unless specified by user */
2812 cpi->frames_since_golden = 0;
2813
2814 /* Clear the alternate reference update pending flag. */
2815 cpi->source_alt_ref_pending = 0;
2816
2817 /* Set the alternate reference frame active flag */
2818 cpi->source_alt_ref_active = 1;
2819
2820
2821 }
update_golden_frame_stats(VP8_COMP * cpi)2822 static void update_golden_frame_stats(VP8_COMP *cpi)
2823 {
2824 VP8_COMMON *cm = &cpi->common;
2825
2826 /* Update the Golden frame usage counts. */
2827 if (cm->refresh_golden_frame)
2828 {
2829 /* Select an interval before next GF */
2830 if (!cpi->auto_gold)
2831 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2832
2833 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
2834 {
2835 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
2836
2837 /* Set the bits per frame that we should try and recover in
2838 * subsequent inter frames to account for the extra GF spend...
2839 * note that his does not apply for GF updates that occur
2840 * coincident with a key frame as the extra cost of key frames
2841 * is dealt with elsewhere.
2842 */
2843 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
2844 {
2845 /* Calcluate GF bits to be recovered
2846 * Projected size - av frame bits available for inter
2847 * frames for clip as a whole
2848 */
2849 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
2850 }
2851
2852 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2853
2854 }
2855
2856 /* Update data structure that monitors level of reference to last GF */
2857 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2858 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2859
2860 /* this frame refreshes means next frames don't unless specified by
2861 * user
2862 */
2863 cm->refresh_golden_frame = 0;
2864 cpi->frames_since_golden = 0;
2865
2866 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2867 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2868 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2869 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2870
2871 /* ******** Fixed Q test code only ************ */
2872 /* If we are going to use the ALT reference for the next group of
2873 * frames set a flag to say so.
2874 */
2875 if (cpi->oxcf.fixed_q >= 0 &&
2876 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
2877 {
2878 cpi->source_alt_ref_pending = 1;
2879 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2880 }
2881
2882 if (!cpi->source_alt_ref_pending)
2883 cpi->source_alt_ref_active = 0;
2884
2885 /* Decrement count down till next gf */
2886 if (cpi->frames_till_gf_update_due > 0)
2887 cpi->frames_till_gf_update_due--;
2888
2889 }
2890 else if (!cpi->common.refresh_alt_ref_frame)
2891 {
2892 /* Decrement count down till next gf */
2893 if (cpi->frames_till_gf_update_due > 0)
2894 cpi->frames_till_gf_update_due--;
2895
2896 if (cpi->frames_till_alt_ref_frame)
2897 cpi->frames_till_alt_ref_frame --;
2898
2899 cpi->frames_since_golden ++;
2900
2901 if (cpi->frames_since_golden > 1)
2902 {
2903 cpi->recent_ref_frame_usage[INTRA_FRAME] +=
2904 cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME];
2905 cpi->recent_ref_frame_usage[LAST_FRAME] +=
2906 cpi->mb.count_mb_ref_frame_usage[LAST_FRAME];
2907 cpi->recent_ref_frame_usage[GOLDEN_FRAME] +=
2908 cpi->mb.count_mb_ref_frame_usage[GOLDEN_FRAME];
2909 cpi->recent_ref_frame_usage[ALTREF_FRAME] +=
2910 cpi->mb.count_mb_ref_frame_usage[ALTREF_FRAME];
2911 }
2912 }
2913 }
2914
2915 /* This function updates the reference frame probability estimates that
2916 * will be used during mode selection
2917 */
update_rd_ref_frame_probs(VP8_COMP * cpi)2918 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
2919 {
2920 VP8_COMMON *cm = &cpi->common;
2921
2922 const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
2923 const int rf_intra = rfct[INTRA_FRAME];
2924 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2925
2926 if (cm->frame_type == KEY_FRAME)
2927 {
2928 cpi->prob_intra_coded = 255;
2929 cpi->prob_last_coded = 128;
2930 cpi->prob_gf_coded = 128;
2931 }
2932 else if (!(rf_intra + rf_inter))
2933 {
2934 cpi->prob_intra_coded = 63;
2935 cpi->prob_last_coded = 128;
2936 cpi->prob_gf_coded = 128;
2937 }
2938
2939 /* update reference frame costs since we can do better than what we got
2940 * last frame.
2941 */
2942 if (cpi->oxcf.number_of_layers == 1)
2943 {
2944 if (cpi->common.refresh_alt_ref_frame)
2945 {
2946 cpi->prob_intra_coded += 40;
2947 if (cpi->prob_intra_coded > 255)
2948 cpi->prob_intra_coded = 255;
2949 cpi->prob_last_coded = 200;
2950 cpi->prob_gf_coded = 1;
2951 }
2952 else if (cpi->frames_since_golden == 0)
2953 {
2954 cpi->prob_last_coded = 214;
2955 }
2956 else if (cpi->frames_since_golden == 1)
2957 {
2958 cpi->prob_last_coded = 192;
2959 cpi->prob_gf_coded = 220;
2960 }
2961 else if (cpi->source_alt_ref_active)
2962 {
2963 cpi->prob_gf_coded -= 20;
2964
2965 if (cpi->prob_gf_coded < 10)
2966 cpi->prob_gf_coded = 10;
2967 }
2968 if (!cpi->source_alt_ref_active)
2969 cpi->prob_gf_coded = 255;
2970 }
2971 }
2972
2973
2974 /* 1 = key, 0 = inter */
decide_key_frame(VP8_COMP * cpi)2975 static int decide_key_frame(VP8_COMP *cpi)
2976 {
2977 VP8_COMMON *cm = &cpi->common;
2978
2979 int code_key_frame = 0;
2980
2981 cpi->kf_boost = 0;
2982
2983 if (cpi->Speed > 11)
2984 return 0;
2985
2986 /* Clear down mmx registers */
2987 vp8_clear_system_state();
2988
2989 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
2990 {
2991 double change = 1.0 * abs((int)(cpi->mb.intra_error -
2992 cpi->last_intra_error)) / (1 + cpi->last_intra_error);
2993 double change2 = 1.0 * abs((int)(cpi->mb.prediction_error -
2994 cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
2995 double minerror = cm->MBs * 256;
2996
2997 cpi->last_intra_error = cpi->mb.intra_error;
2998 cpi->last_prediction_error = cpi->mb.prediction_error;
2999
3000 if (10 * cpi->mb.intra_error / (1 + cpi->mb.prediction_error) < 15
3001 && cpi->mb.prediction_error > minerror
3002 && (change > .25 || change2 > .25))
3003 {
3004 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
3005 return 1;
3006 }
3007
3008 return 0;
3009
3010 }
3011
3012 /* If the following are true we might as well code a key frame */
3013 if (((cpi->this_frame_percent_intra == 100) &&
3014 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
3015 ((cpi->this_frame_percent_intra > 95) &&
3016 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
3017 {
3018 code_key_frame = 1;
3019 }
3020 /* in addition if the following are true and this is not a golden frame
3021 * then code a key frame Note that on golden frames there often seems
3022 * to be a pop in intra useage anyway hence this restriction is
3023 * designed to prevent spurious key frames. The Intra pop needs to be
3024 * investigated.
3025 */
3026 else if (((cpi->this_frame_percent_intra > 60) &&
3027 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
3028 ((cpi->this_frame_percent_intra > 75) &&
3029 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
3030 ((cpi->this_frame_percent_intra > 90) &&
3031 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
3032 {
3033 if (!cm->refresh_golden_frame)
3034 code_key_frame = 1;
3035 }
3036
3037 return code_key_frame;
3038
3039 }
3040
3041 #if !(CONFIG_REALTIME_ONLY)
Pass1Encode(VP8_COMP * cpi,unsigned long * size,unsigned char * dest,unsigned int * frame_flags)3042 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
3043 {
3044 (void) size;
3045 (void) dest;
3046 (void) frame_flags;
3047 vp8_set_quantizer(cpi, 26);
3048
3049 vp8_first_pass(cpi);
3050 }
3051 #endif
3052
3053 #if 0
3054 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
3055 {
3056
3057 /* write the frame */
3058 FILE *yframe;
3059 int i;
3060 char filename[255];
3061
3062 sprintf(filename, "cx\\y%04d.raw", this_frame);
3063 yframe = fopen(filename, "wb");
3064
3065 for (i = 0; i < frame->y_height; i++)
3066 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
3067
3068 fclose(yframe);
3069 sprintf(filename, "cx\\u%04d.raw", this_frame);
3070 yframe = fopen(filename, "wb");
3071
3072 for (i = 0; i < frame->uv_height; i++)
3073 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3074
3075 fclose(yframe);
3076 sprintf(filename, "cx\\v%04d.raw", this_frame);
3077 yframe = fopen(filename, "wb");
3078
3079 for (i = 0; i < frame->uv_height; i++)
3080 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
3081
3082 fclose(yframe);
3083 }
3084 #endif
3085 /* return of 0 means drop frame */
3086
3087 /* Function to test for conditions that indeicate we should loop
3088 * back and recode a frame.
3089 */
recode_loop_test(VP8_COMP * cpi,int high_limit,int low_limit,int q,int maxq,int minq)3090 static int recode_loop_test( VP8_COMP *cpi,
3091 int high_limit, int low_limit,
3092 int q, int maxq, int minq )
3093 {
3094 int force_recode = 0;
3095 VP8_COMMON *cm = &cpi->common;
3096
3097 /* Is frame recode allowed at all
3098 * Yes if either recode mode 1 is selected or mode two is selcted
3099 * and the frame is a key frame. golden frame or alt_ref_frame
3100 */
3101 if ( (cpi->sf.recode_loop == 1) ||
3102 ( (cpi->sf.recode_loop == 2) &&
3103 ( (cm->frame_type == KEY_FRAME) ||
3104 cm->refresh_golden_frame ||
3105 cm->refresh_alt_ref_frame ) ) )
3106 {
3107 /* General over and under shoot tests */
3108 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
3109 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
3110 {
3111 force_recode = 1;
3112 }
3113 /* Special Constrained quality tests */
3114 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3115 {
3116 /* Undershoot and below auto cq level */
3117 if ( (q > cpi->cq_target_quality) &&
3118 (cpi->projected_frame_size <
3119 ((cpi->this_frame_target * 7) >> 3)))
3120 {
3121 force_recode = 1;
3122 }
3123 /* Severe undershoot and between auto and user cq level */
3124 else if ( (q > cpi->oxcf.cq_level) &&
3125 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
3126 (cpi->active_best_quality > cpi->oxcf.cq_level))
3127 {
3128 force_recode = 1;
3129 cpi->active_best_quality = cpi->oxcf.cq_level;
3130 }
3131 }
3132 }
3133
3134 return force_recode;
3135 }
3136
update_reference_frames(VP8_COMP * cpi)3137 static void update_reference_frames(VP8_COMP *cpi)
3138 {
3139 VP8_COMMON *cm = &cpi->common;
3140 YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
3141
3142 /* At this point the new frame has been encoded.
3143 * If any buffer copy / swapping is signaled it should be done here.
3144 */
3145
3146 if (cm->frame_type == KEY_FRAME)
3147 {
3148 yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME | VP8_ALTR_FRAME ;
3149
3150 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3151 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3152
3153 cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
3154
3155 cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
3156 cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
3157 }
3158 else /* For non key frames */
3159 {
3160 if (cm->refresh_alt_ref_frame)
3161 {
3162 assert(!cm->copy_buffer_to_arf);
3163
3164 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALTR_FRAME;
3165 cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3166 cm->alt_fb_idx = cm->new_fb_idx;
3167
3168 cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
3169 }
3170 else if (cm->copy_buffer_to_arf)
3171 {
3172 assert(!(cm->copy_buffer_to_arf & ~0x3));
3173
3174 if (cm->copy_buffer_to_arf == 1)
3175 {
3176 if(cm->alt_fb_idx != cm->lst_fb_idx)
3177 {
3178 yv12_fb[cm->lst_fb_idx].flags |= VP8_ALTR_FRAME;
3179 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3180 cm->alt_fb_idx = cm->lst_fb_idx;
3181
3182 cpi->current_ref_frames[ALTREF_FRAME] =
3183 cpi->current_ref_frames[LAST_FRAME];
3184 }
3185 }
3186 else /* if (cm->copy_buffer_to_arf == 2) */
3187 {
3188 if(cm->alt_fb_idx != cm->gld_fb_idx)
3189 {
3190 yv12_fb[cm->gld_fb_idx].flags |= VP8_ALTR_FRAME;
3191 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
3192 cm->alt_fb_idx = cm->gld_fb_idx;
3193
3194 cpi->current_ref_frames[ALTREF_FRAME] =
3195 cpi->current_ref_frames[GOLDEN_FRAME];
3196 }
3197 }
3198 }
3199
3200 if (cm->refresh_golden_frame)
3201 {
3202 assert(!cm->copy_buffer_to_gf);
3203
3204 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME;
3205 cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3206 cm->gld_fb_idx = cm->new_fb_idx;
3207
3208 cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
3209 }
3210 else if (cm->copy_buffer_to_gf)
3211 {
3212 assert(!(cm->copy_buffer_to_arf & ~0x3));
3213
3214 if (cm->copy_buffer_to_gf == 1)
3215 {
3216 if(cm->gld_fb_idx != cm->lst_fb_idx)
3217 {
3218 yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FRAME;
3219 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3220 cm->gld_fb_idx = cm->lst_fb_idx;
3221
3222 cpi->current_ref_frames[GOLDEN_FRAME] =
3223 cpi->current_ref_frames[LAST_FRAME];
3224 }
3225 }
3226 else /* if (cm->copy_buffer_to_gf == 2) */
3227 {
3228 if(cm->alt_fb_idx != cm->gld_fb_idx)
3229 {
3230 yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FRAME;
3231 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
3232 cm->gld_fb_idx = cm->alt_fb_idx;
3233
3234 cpi->current_ref_frames[GOLDEN_FRAME] =
3235 cpi->current_ref_frames[ALTREF_FRAME];
3236 }
3237 }
3238 }
3239 }
3240
3241 if (cm->refresh_last_frame)
3242 {
3243 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FRAME;
3244 cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FRAME;
3245 cm->lst_fb_idx = cm->new_fb_idx;
3246
3247 cpi->current_ref_frames[LAST_FRAME] = cm->current_video_frame;
3248 }
3249
3250 #if CONFIG_TEMPORAL_DENOISING
3251 if (cpi->oxcf.noise_sensitivity)
3252 {
3253 /* we shouldn't have to keep multiple copies as we know in advance which
3254 * buffer we should start - for now to get something up and running
3255 * I've chosen to copy the buffers
3256 */
3257 if (cm->frame_type == KEY_FRAME)
3258 {
3259 int i;
3260 for (i = LAST_FRAME; i < MAX_REF_FRAMES; ++i)
3261 vp8_yv12_copy_frame(cpi->Source,
3262 &cpi->denoiser.yv12_running_avg[i]);
3263 }
3264 else /* For non key frames */
3265 {
3266 vp8_yv12_extend_frame_borders(
3267 &cpi->denoiser.yv12_running_avg[INTRA_FRAME]);
3268
3269 if (cm->refresh_alt_ref_frame || cm->copy_buffer_to_arf)
3270 {
3271 vp8_yv12_copy_frame(
3272 &cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3273 &cpi->denoiser.yv12_running_avg[ALTREF_FRAME]);
3274 }
3275 if (cm->refresh_golden_frame || cm->copy_buffer_to_gf)
3276 {
3277 vp8_yv12_copy_frame(
3278 &cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3279 &cpi->denoiser.yv12_running_avg[GOLDEN_FRAME]);
3280 }
3281 if(cm->refresh_last_frame)
3282 {
3283 vp8_yv12_copy_frame(
3284 &cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3285 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3286 }
3287 }
3288 if (cpi->oxcf.noise_sensitivity == 4)
3289 vp8_yv12_copy_frame(cpi->Source, &cpi->denoiser.yv12_last_source);
3290
3291 }
3292 #endif
3293
3294 }
3295
3296 #if CONFIG_TEMPORAL_DENOISING
process_denoiser_mode_change(VP8_COMP * cpi)3297 static void process_denoiser_mode_change(VP8_COMP *cpi) {
3298 const VP8_COMMON *const cm = &cpi->common;
3299 int i, j;
3300 int total = 0;
3301 int num_blocks = 0;
3302 // Number of blocks skipped along row/column in computing the
3303 // nmse (normalized mean square error) of source.
3304 int skip = 2;
3305 // Only select blocks for computing nmse that have been encoded
3306 // as ZERO LAST min_consec_zero_last frames in a row.
3307 // Scale with number of temporal layers.
3308 int min_consec_zero_last = 8 / cpi->oxcf.number_of_layers;
3309 // Decision is tested for changing the denoising mode every
3310 // num_mode_change times this function is called. Note that this
3311 // function called every 8 frames, so (8 * num_mode_change) is number
3312 // of frames where denoising mode change is tested for switch.
3313 int num_mode_change = 15;
3314 // Framerate factor, to compensate for larger mse at lower framerates.
3315 // Use ref_framerate, which is full source framerate for temporal layers.
3316 // TODO(marpan): Adjust this factor.
3317 int fac_framerate = cpi->ref_framerate < 25.0f ? 80 : 100;
3318 int tot_num_blocks = cm->mb_rows * cm->mb_cols;
3319 int ystride = cpi->Source->y_stride;
3320 unsigned char *src = cpi->Source->y_buffer;
3321 unsigned char *dst = cpi->denoiser.yv12_last_source.y_buffer;
3322 static const unsigned char const_source[16] = {
3323 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
3324 128, 128, 128};
3325
3326 // Loop through the Y plane, every skip blocks along rows and columns,
3327 // summing the normalized mean square error, only for blocks that have
3328 // been encoded as ZEROMV LAST at least min_consec_zero_last least frames in
3329 // a row and have small sum difference between current and previous frame.
3330 // Normalization here is by the contrast of the current frame block.
3331 for (i = 0; i < cm->Height; i += 16 * skip) {
3332 int block_index_row = (i >> 4) * cm->mb_cols;
3333 for (j = 0; j < cm->Width; j += 16 * skip) {
3334 int index = block_index_row + (j >> 4);
3335 if (cpi->consec_zero_last[index] >= min_consec_zero_last) {
3336 unsigned int sse;
3337 const unsigned int mse = vp8_mse16x16(src + j,
3338 ystride,
3339 dst + j,
3340 ystride,
3341 &sse);
3342 const unsigned int var = vp8_variance16x16(src + j,
3343 ystride,
3344 dst + j,
3345 ystride,
3346 &sse);
3347 // Only consider this block as valid for noise measurement
3348 // if the sum_diff average of the current and previous frame
3349 // is small (to avoid effects from lighting change).
3350 if ((mse - var) < 256) {
3351 const unsigned int act = vp8_variance16x16(src + j,
3352 ystride,
3353 const_source,
3354 0,
3355 &sse);
3356 if (act > 0)
3357 total += mse / act;
3358 num_blocks++;
3359 }
3360 }
3361 }
3362 src += 16 * skip * ystride;
3363 dst += 16 * skip * ystride;
3364 }
3365 total = total * fac_framerate / 100;
3366
3367 // Only consider this frame as valid sample if we have computed nmse over
3368 // at least ~1/16 blocks, and Total > 0 (Total == 0 can happen if the
3369 // application inputs duplicate frames, or contrast is all zero).
3370 if (total > 0 &&
3371 (num_blocks > (tot_num_blocks >> 4))) {
3372 // Update the recursive mean square source_diff.
3373 if (cpi->denoiser.nmse_source_diff_count == 0) {
3374 // First sample in new interval.
3375 cpi->denoiser.nmse_source_diff = total;
3376 cpi->denoiser.qp_avg = cm->base_qindex;
3377 } else {
3378 // For subsequent samples, use average with weight ~1/4 for new sample.
3379 cpi->denoiser.nmse_source_diff = (int)((total >> 2) +
3380 3 * (cpi->denoiser.nmse_source_diff >> 2));
3381 cpi->denoiser.qp_avg = (int)((cm->base_qindex >> 2) +
3382 3 * (cpi->denoiser.qp_avg >> 2));
3383 }
3384 cpi->denoiser.nmse_source_diff_count++;
3385 }
3386 // Check for changing the denoiser mode, when we have obtained #samples =
3387 // num_mode_change. Condition the change also on the bitrate and QP.
3388 if (cpi->denoiser.nmse_source_diff_count == num_mode_change) {
3389 // Check for going up: from normal to aggressive mode.
3390 if ((cpi->denoiser.denoiser_mode == kDenoiserOnYUV) &&
3391 (cpi->denoiser.nmse_source_diff >
3392 cpi->denoiser.threshold_aggressive_mode) &&
3393 (cpi->denoiser.qp_avg < cpi->denoiser.qp_threshold_up &&
3394 cpi->target_bandwidth > cpi->denoiser.bitrate_threshold)) {
3395 vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUVAggressive);
3396 } else {
3397 // Check for going down: from aggressive to normal mode.
3398 if (((cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) &&
3399 (cpi->denoiser.nmse_source_diff <
3400 cpi->denoiser.threshold_aggressive_mode)) ||
3401 ((cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) &&
3402 (cpi->denoiser.qp_avg > cpi->denoiser.qp_threshold_down ||
3403 cpi->target_bandwidth < cpi->denoiser.bitrate_threshold))) {
3404 vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUV);
3405 }
3406 }
3407 // Reset metric and counter for next interval.
3408 cpi->denoiser.nmse_source_diff = 0;
3409 cpi->denoiser.qp_avg = 0;
3410 cpi->denoiser.nmse_source_diff_count = 0;
3411 }
3412 }
3413 #endif
3414
vp8_loopfilter_frame(VP8_COMP * cpi,VP8_COMMON * cm)3415 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
3416 {
3417 const FRAME_TYPE frame_type = cm->frame_type;
3418
3419 if (cm->no_lpf)
3420 {
3421 cm->filter_level = 0;
3422 }
3423 else
3424 {
3425 struct vpx_usec_timer timer;
3426
3427 vp8_clear_system_state();
3428
3429 vpx_usec_timer_start(&timer);
3430 if (cpi->sf.auto_filter == 0)
3431 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3432
3433 else
3434 vp8cx_pick_filter_level(cpi->Source, cpi);
3435
3436 if (cm->filter_level > 0)
3437 {
3438 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3439 }
3440
3441 vpx_usec_timer_mark(&timer);
3442 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3443 }
3444
3445 #if CONFIG_MULTITHREAD
3446 if (cpi->b_multi_threaded)
3447 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
3448 #endif
3449
3450 if (cm->filter_level > 0)
3451 {
3452 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, frame_type);
3453 }
3454
3455 vp8_yv12_extend_frame_borders(cm->frame_to_show);
3456
3457 }
3458
encode_frame_to_data_rate(VP8_COMP * cpi,unsigned long * size,unsigned char * dest,unsigned char * dest_end,unsigned int * frame_flags)3459 static void encode_frame_to_data_rate
3460 (
3461 VP8_COMP *cpi,
3462 unsigned long *size,
3463 unsigned char *dest,
3464 unsigned char* dest_end,
3465 unsigned int *frame_flags
3466 )
3467 {
3468 int Q;
3469 int frame_over_shoot_limit;
3470 int frame_under_shoot_limit;
3471
3472 int Loop = 0;
3473 int loop_count;
3474
3475 VP8_COMMON *cm = &cpi->common;
3476 int active_worst_qchanged = 0;
3477
3478 #if !(CONFIG_REALTIME_ONLY)
3479 int q_low;
3480 int q_high;
3481 int zbin_oq_high;
3482 int zbin_oq_low = 0;
3483 int top_index;
3484 int bottom_index;
3485 int overshoot_seen = 0;
3486 int undershoot_seen = 0;
3487 #endif
3488
3489 int drop_mark = (int)(cpi->oxcf.drop_frames_water_mark *
3490 cpi->oxcf.optimal_buffer_level / 100);
3491 int drop_mark75 = drop_mark * 2 / 3;
3492 int drop_mark50 = drop_mark / 4;
3493 int drop_mark25 = drop_mark / 8;
3494
3495
3496 /* Clear down mmx registers to allow floating point in what follows */
3497 vp8_clear_system_state();
3498
3499 #if CONFIG_MULTITHREAD
3500 /* wait for the last picture loopfilter thread done */
3501 if (cpi->b_lpf_running)
3502 {
3503 sem_wait(&cpi->h_event_end_lpf);
3504 cpi->b_lpf_running = 0;
3505 }
3506 #endif
3507
3508 if(cpi->force_next_frame_intra)
3509 {
3510 cm->frame_type = KEY_FRAME; /* delayed intra frame */
3511 cpi->force_next_frame_intra = 0;
3512 }
3513
3514 /* For an alt ref frame in 2 pass we skip the call to the second pass
3515 * function that sets the target bandwidth
3516 */
3517 #if !(CONFIG_REALTIME_ONLY)
3518
3519 if (cpi->pass == 2)
3520 {
3521 if (cpi->common.refresh_alt_ref_frame)
3522 {
3523 /* Per frame bit target for the alt ref frame */
3524 cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
3525 /* per second target bitrate */
3526 cpi->target_bandwidth = (int)(cpi->twopass.gf_bits *
3527 cpi->output_framerate);
3528 }
3529 }
3530 else
3531 #endif
3532 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_framerate);
3533
3534 /* Default turn off buffer to buffer copying */
3535 cm->copy_buffer_to_gf = 0;
3536 cm->copy_buffer_to_arf = 0;
3537
3538 /* Clear zbin over-quant value and mode boost values. */
3539 cpi->mb.zbin_over_quant = 0;
3540 cpi->mb.zbin_mode_boost = 0;
3541
3542 /* Enable or disable mode based tweaking of the zbin
3543 * For 2 Pass Only used where GF/ARF prediction quality
3544 * is above a threshold
3545 */
3546 cpi->mb.zbin_mode_boost_enabled = 1;
3547 if (cpi->pass == 2)
3548 {
3549 if ( cpi->gfu_boost <= 400 )
3550 {
3551 cpi->mb.zbin_mode_boost_enabled = 0;
3552 }
3553 }
3554
3555 /* Current default encoder behaviour for the altref sign bias */
3556 if (cpi->source_alt_ref_active)
3557 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3558 else
3559 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3560
3561 /* Check to see if a key frame is signaled
3562 * For two pass with auto key frame enabled cm->frame_type may already
3563 * be set, but not for one pass.
3564 */
3565 if ((cm->current_video_frame == 0) ||
3566 (cm->frame_flags & FRAMEFLAGS_KEY) ||
3567 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
3568 {
3569 /* Key frame from VFW/auto-keyframe/first frame */
3570 cm->frame_type = KEY_FRAME;
3571 #if CONFIG_TEMPORAL_DENOISING
3572 if (cpi->oxcf.noise_sensitivity == 4) {
3573 // For adaptive mode, reset denoiser to normal mode on key frame.
3574 vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUV);
3575 }
3576 #endif
3577 }
3578
3579 #if CONFIG_MULTI_RES_ENCODING
3580 /* In multi-resolution encoding, frame_type is decided by lowest-resolution
3581 * encoder. Same frame_type is adopted while encoding at other resolution.
3582 */
3583 if (cpi->oxcf.mr_encoder_id)
3584 {
3585 LOWER_RES_FRAME_INFO* low_res_frame_info
3586 = (LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info;
3587
3588 cm->frame_type = low_res_frame_info->frame_type;
3589
3590 if(cm->frame_type != KEY_FRAME)
3591 {
3592 cpi->mr_low_res_mv_avail = 1;
3593 cpi->mr_low_res_mv_avail &= !(low_res_frame_info->is_frame_dropped);
3594
3595 if (cpi->ref_frame_flags & VP8_LAST_FRAME)
3596 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[LAST_FRAME]
3597 == low_res_frame_info->low_res_ref_frames[LAST_FRAME]);
3598
3599 if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
3600 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[GOLDEN_FRAME]
3601 == low_res_frame_info->low_res_ref_frames[GOLDEN_FRAME]);
3602
3603 if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
3604 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[ALTREF_FRAME]
3605 == low_res_frame_info->low_res_ref_frames[ALTREF_FRAME]);
3606 }
3607 }
3608 #endif
3609
3610 // Find the reference frame closest to the current frame.
3611 cpi->closest_reference_frame = LAST_FRAME;
3612 if (cm->frame_type != KEY_FRAME) {
3613 int i;
3614 MV_REFERENCE_FRAME closest_ref = INTRA_FRAME;
3615 if (cpi->ref_frame_flags & VP8_LAST_FRAME) {
3616 closest_ref = LAST_FRAME;
3617 } else if (cpi->ref_frame_flags & VP8_GOLD_FRAME) {
3618 closest_ref = GOLDEN_FRAME;
3619 } else if (cpi->ref_frame_flags & VP8_ALTR_FRAME) {
3620 closest_ref = ALTREF_FRAME;
3621 }
3622 for (i = 1; i <= 3; i++) {
3623 vpx_ref_frame_type_t ref_frame_type = (vpx_ref_frame_type_t)
3624 ((i == 3) ? 4 : i);
3625 if (cpi->ref_frame_flags & ref_frame_type) {
3626 if ((cm->current_video_frame - cpi->current_ref_frames[i]) <
3627 (cm->current_video_frame - cpi->current_ref_frames[closest_ref])) {
3628 closest_ref = i;
3629 }
3630 }
3631 }
3632 cpi->closest_reference_frame = closest_ref;
3633 }
3634
3635 /* Set various flags etc to special state if it is a key frame */
3636 if (cm->frame_type == KEY_FRAME)
3637 {
3638 int i;
3639
3640 // Set the loop filter deltas and segmentation map update
3641 setup_features(cpi);
3642
3643 /* The alternate reference frame cannot be active for a key frame */
3644 cpi->source_alt_ref_active = 0;
3645
3646 /* Reset the RD threshold multipliers to default of * 1 (128) */
3647 for (i = 0; i < MAX_MODES; i++)
3648 {
3649 cpi->mb.rd_thresh_mult[i] = 128;
3650 }
3651
3652 // Reset the zero_last counter to 0 on key frame.
3653 vpx_memset(cpi->consec_zero_last, 0, cm->mb_rows * cm->mb_cols);
3654 }
3655
3656 #if 0
3657 /* Experimental code for lagged compress and one pass
3658 * Initialise one_pass GF frames stats
3659 * Update stats used for GF selection
3660 */
3661 {
3662 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3663
3664 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3665 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3666 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3667 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3668 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3669 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3670 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3671 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3672 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3673 }
3674 #endif
3675
3676 update_rd_ref_frame_probs(cpi);
3677
3678 if (cpi->drop_frames_allowed)
3679 {
3680 /* The reset to decimation 0 is only done here for one pass.
3681 * Once it is set two pass leaves decimation on till the next kf.
3682 */
3683 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
3684 cpi->decimation_factor --;
3685
3686 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
3687 cpi->decimation_factor = 1;
3688
3689 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
3690 {
3691 cpi->decimation_factor = 3;
3692 }
3693 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
3694 {
3695 cpi->decimation_factor = 2;
3696 }
3697 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
3698 {
3699 cpi->decimation_factor = 1;
3700 }
3701 }
3702
3703 /* The following decimates the frame rate according to a regular
3704 * pattern (i.e. to 1/2 or 2/3 frame rate) This can be used to help
3705 * prevent buffer under-run in CBR mode. Alternatively it might be
3706 * desirable in some situations to drop frame rate but throw more bits
3707 * at each frame.
3708 *
3709 * Note that dropping a key frame can be problematic if spatial
3710 * resampling is also active
3711 */
3712 if (cpi->decimation_factor > 0)
3713 {
3714 switch (cpi->decimation_factor)
3715 {
3716 case 1:
3717 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3718 break;
3719 case 2:
3720 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3721 break;
3722 case 3:
3723 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3724 break;
3725 }
3726
3727 /* Note that we should not throw out a key frame (especially when
3728 * spatial resampling is enabled).
3729 */
3730 if (cm->frame_type == KEY_FRAME)
3731 {
3732 cpi->decimation_count = cpi->decimation_factor;
3733 }
3734 else if (cpi->decimation_count > 0)
3735 {
3736 cpi->decimation_count --;
3737
3738 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3739 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
3740 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3741
3742 #if CONFIG_MULTI_RES_ENCODING
3743 vp8_store_drop_frame_info(cpi);
3744 #endif
3745
3746 cm->current_video_frame++;
3747 cpi->frames_since_key++;
3748 // We advance the temporal pattern for dropped frames.
3749 cpi->temporal_pattern_counter++;
3750
3751 #if CONFIG_INTERNAL_STATS
3752 cpi->count ++;
3753 #endif
3754
3755 cpi->buffer_level = cpi->bits_off_target;
3756
3757 if (cpi->oxcf.number_of_layers > 1)
3758 {
3759 unsigned int i;
3760
3761 /* Propagate bits saved by dropping the frame to higher
3762 * layers
3763 */
3764 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
3765 {
3766 LAYER_CONTEXT *lc = &cpi->layer_context[i];
3767 lc->bits_off_target += (int)(lc->target_bandwidth /
3768 lc->framerate);
3769 if (lc->bits_off_target > lc->maximum_buffer_size)
3770 lc->bits_off_target = lc->maximum_buffer_size;
3771 lc->buffer_level = lc->bits_off_target;
3772 }
3773 }
3774
3775 return;
3776 }
3777 else
3778 cpi->decimation_count = cpi->decimation_factor;
3779 }
3780 else
3781 cpi->decimation_count = 0;
3782
3783 /* Decide how big to make the frame */
3784 if (!vp8_pick_frame_size(cpi))
3785 {
3786 /*TODO: 2 drop_frame and return code could be put together. */
3787 #if CONFIG_MULTI_RES_ENCODING
3788 vp8_store_drop_frame_info(cpi);
3789 #endif
3790 cm->current_video_frame++;
3791 cpi->frames_since_key++;
3792 // We advance the temporal pattern for dropped frames.
3793 cpi->temporal_pattern_counter++;
3794 return;
3795 }
3796
3797 /* Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3798 * This has a knock on effect on active best quality as well.
3799 * For CBR if the buffer reaches its maximum level then we can no longer
3800 * save up bits for later frames so we might as well use them up
3801 * on the current frame.
3802 */
3803 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3804 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
3805 {
3806 /* Max adjustment is 1/4 */
3807 int Adjustment = cpi->active_worst_quality / 4;
3808
3809 if (Adjustment)
3810 {
3811 int buff_lvl_step;
3812
3813 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
3814 {
3815 buff_lvl_step = (int)
3816 ((cpi->oxcf.maximum_buffer_size -
3817 cpi->oxcf.optimal_buffer_level) /
3818 Adjustment);
3819
3820 if (buff_lvl_step)
3821 Adjustment = (int)
3822 ((cpi->buffer_level -
3823 cpi->oxcf.optimal_buffer_level) /
3824 buff_lvl_step);
3825 else
3826 Adjustment = 0;
3827 }
3828
3829 cpi->active_worst_quality -= Adjustment;
3830
3831 if(cpi->active_worst_quality < cpi->active_best_quality)
3832 cpi->active_worst_quality = cpi->active_best_quality;
3833 }
3834 }
3835
3836 /* Set an active best quality and if necessary active worst quality
3837 * There is some odd behavior for one pass here that needs attention.
3838 */
3839 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
3840 {
3841 vp8_clear_system_state();
3842
3843 Q = cpi->active_worst_quality;
3844
3845 if ( cm->frame_type == KEY_FRAME )
3846 {
3847 if ( cpi->pass == 2 )
3848 {
3849 if (cpi->gfu_boost > 600)
3850 cpi->active_best_quality = kf_low_motion_minq[Q];
3851 else
3852 cpi->active_best_quality = kf_high_motion_minq[Q];
3853
3854 /* Special case for key frames forced because we have reached
3855 * the maximum key frame interval. Here force the Q to a range
3856 * based on the ambient Q to reduce the risk of popping
3857 */
3858 if ( cpi->this_key_frame_forced )
3859 {
3860 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
3861 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
3862 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
3863 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3864 }
3865 }
3866 /* One pass more conservative */
3867 else
3868 cpi->active_best_quality = kf_high_motion_minq[Q];
3869 }
3870
3871 else if (cpi->oxcf.number_of_layers==1 &&
3872 (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame))
3873 {
3874 /* Use the lower of cpi->active_worst_quality and recent
3875 * average Q as basis for GF/ARF Q limit unless last frame was
3876 * a key frame.
3877 */
3878 if ( (cpi->frames_since_key > 1) &&
3879 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
3880 {
3881 Q = cpi->avg_frame_qindex;
3882 }
3883
3884 /* For constrained quality dont allow Q less than the cq level */
3885 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3886 (Q < cpi->cq_target_quality) )
3887 {
3888 Q = cpi->cq_target_quality;
3889 }
3890
3891 if ( cpi->pass == 2 )
3892 {
3893 if ( cpi->gfu_boost > 1000 )
3894 cpi->active_best_quality = gf_low_motion_minq[Q];
3895 else if ( cpi->gfu_boost < 400 )
3896 cpi->active_best_quality = gf_high_motion_minq[Q];
3897 else
3898 cpi->active_best_quality = gf_mid_motion_minq[Q];
3899
3900 /* Constrained quality use slightly lower active best. */
3901 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
3902 {
3903 cpi->active_best_quality =
3904 cpi->active_best_quality * 15/16;
3905 }
3906 }
3907 /* One pass more conservative */
3908 else
3909 cpi->active_best_quality = gf_high_motion_minq[Q];
3910 }
3911 else
3912 {
3913 cpi->active_best_quality = inter_minq[Q];
3914
3915 /* For the constant/constrained quality mode we dont want
3916 * q to fall below the cq level.
3917 */
3918 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3919 (cpi->active_best_quality < cpi->cq_target_quality) )
3920 {
3921 /* If we are strongly undershooting the target rate in the last
3922 * frames then use the user passed in cq value not the auto
3923 * cq value.
3924 */
3925 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
3926 cpi->active_best_quality = cpi->oxcf.cq_level;
3927 else
3928 cpi->active_best_quality = cpi->cq_target_quality;
3929 }
3930 }
3931
3932 /* If CBR and the buffer is as full then it is reasonable to allow
3933 * higher quality on the frames to prevent bits just going to waste.
3934 */
3935 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
3936 {
3937 /* Note that the use of >= here elliminates the risk of a devide
3938 * by 0 error in the else if clause
3939 */
3940 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
3941 cpi->active_best_quality = cpi->best_quality;
3942
3943 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
3944 {
3945 int Fraction = (int)
3946 (((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128)
3947 / (cpi->oxcf.maximum_buffer_size -
3948 cpi->oxcf.optimal_buffer_level));
3949 int min_qadjustment = ((cpi->active_best_quality -
3950 cpi->best_quality) * Fraction) / 128;
3951
3952 cpi->active_best_quality -= min_qadjustment;
3953 }
3954 }
3955 }
3956 /* Make sure constrained quality mode limits are adhered to for the first
3957 * few frames of one pass encodes
3958 */
3959 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
3960 {
3961 if ( (cm->frame_type == KEY_FRAME) ||
3962 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
3963 {
3964 cpi->active_best_quality = cpi->best_quality;
3965 }
3966 else if (cpi->active_best_quality < cpi->cq_target_quality)
3967 {
3968 cpi->active_best_quality = cpi->cq_target_quality;
3969 }
3970 }
3971
3972 /* Clip the active best and worst quality values to limits */
3973 if (cpi->active_worst_quality > cpi->worst_quality)
3974 cpi->active_worst_quality = cpi->worst_quality;
3975
3976 if (cpi->active_best_quality < cpi->best_quality)
3977 cpi->active_best_quality = cpi->best_quality;
3978
3979 if ( cpi->active_worst_quality < cpi->active_best_quality )
3980 cpi->active_worst_quality = cpi->active_best_quality;
3981
3982 /* Determine initial Q to try */
3983 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3984
3985 #if !(CONFIG_REALTIME_ONLY)
3986
3987 /* Set highest allowed value for Zbin over quant */
3988 if (cm->frame_type == KEY_FRAME)
3989 zbin_oq_high = 0;
3990 else if ((cpi->oxcf.number_of_layers == 1) && ((cm->refresh_alt_ref_frame ||
3991 (cm->refresh_golden_frame && !cpi->source_alt_ref_active))))
3992 {
3993 zbin_oq_high = 16;
3994 }
3995 else
3996 zbin_oq_high = ZBIN_OQ_MAX;
3997 #endif
3998
3999 /* Setup background Q adjustment for error resilient mode.
4000 * For multi-layer encodes only enable this for the base layer.
4001 */
4002 if (cpi->cyclic_refresh_mode_enabled)
4003 {
4004 if (cpi->current_layer==0)
4005 cyclic_background_refresh(cpi, Q, 0);
4006 else
4007 disable_segmentation(cpi);
4008 }
4009
4010 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
4011
4012 #if !(CONFIG_REALTIME_ONLY)
4013 /* Limit Q range for the adaptive loop. */
4014 bottom_index = cpi->active_best_quality;
4015 top_index = cpi->active_worst_quality;
4016 q_low = cpi->active_best_quality;
4017 q_high = cpi->active_worst_quality;
4018 #endif
4019
4020 vp8_save_coding_context(cpi);
4021
4022 loop_count = 0;
4023
4024 scale_and_extend_source(cpi->un_scaled_source, cpi);
4025
4026 #if CONFIG_TEMPORAL_DENOISING && CONFIG_POSTPROC
4027 // Option to apply spatial blur under the aggressive or adaptive
4028 // (temporal denoising) mode.
4029 if (cpi->oxcf.noise_sensitivity >= 3) {
4030 if (cpi->denoiser.denoise_pars.spatial_blur != 0) {
4031 vp8_de_noise(cm, cpi->Source, cpi->Source,
4032 cpi->denoiser.denoise_pars.spatial_blur, 1, 0, 0);
4033 }
4034 }
4035 #endif
4036
4037 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC && !(CONFIG_TEMPORAL_DENOISING)
4038
4039 if (cpi->oxcf.noise_sensitivity > 0)
4040 {
4041 unsigned char *src;
4042 int l = 0;
4043
4044 switch (cpi->oxcf.noise_sensitivity)
4045 {
4046 case 1:
4047 l = 20;
4048 break;
4049 case 2:
4050 l = 40;
4051 break;
4052 case 3:
4053 l = 60;
4054 break;
4055 case 4:
4056 l = 80;
4057 break;
4058 case 5:
4059 l = 100;
4060 break;
4061 case 6:
4062 l = 150;
4063 break;
4064 }
4065
4066
4067 if (cm->frame_type == KEY_FRAME)
4068 {
4069 vp8_de_noise(cm, cpi->Source, cpi->Source, l , 1, 0, 1);
4070 }
4071 else
4072 {
4073 vp8_de_noise(cm, cpi->Source, cpi->Source, l , 1, 0, 1);
4074
4075 src = cpi->Source->y_buffer;
4076
4077 if (cpi->Source->y_stride < 0)
4078 {
4079 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
4080 }
4081 }
4082 }
4083
4084 #endif
4085
4086
4087 #ifdef OUTPUT_YUV_SRC
4088 vp8_write_yuv_frame(yuv_file, cpi->Source);
4089 #endif
4090
4091 do
4092 {
4093 vp8_clear_system_state();
4094
4095 vp8_set_quantizer(cpi, Q);
4096
4097 /* setup skip prob for costing in mode/mv decision */
4098 if (cpi->common.mb_no_coeff_skip)
4099 {
4100 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
4101
4102 if (cm->frame_type != KEY_FRAME)
4103 {
4104 if (cpi->common.refresh_alt_ref_frame)
4105 {
4106 if (cpi->last_skip_false_probs[2] != 0)
4107 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
4108
4109 /*
4110 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
4111 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
4112 else if (cpi->last_skip_false_probs[2]!=0)
4113 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
4114 */
4115 }
4116 else if (cpi->common.refresh_golden_frame)
4117 {
4118 if (cpi->last_skip_false_probs[1] != 0)
4119 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
4120
4121 /*
4122 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
4123 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
4124 else if (cpi->last_skip_false_probs[1]!=0)
4125 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
4126 */
4127 }
4128 else
4129 {
4130 if (cpi->last_skip_false_probs[0] != 0)
4131 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
4132
4133 /*
4134 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
4135 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
4136 else if(cpi->last_skip_false_probs[0]!=0)
4137 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
4138 */
4139 }
4140
4141 /* as this is for cost estimate, let's make sure it does not
4142 * go extreme eitehr way
4143 */
4144 if (cpi->prob_skip_false < 5)
4145 cpi->prob_skip_false = 5;
4146
4147 if (cpi->prob_skip_false > 250)
4148 cpi->prob_skip_false = 250;
4149
4150 if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref)
4151 cpi->prob_skip_false = 1;
4152 }
4153
4154 #if 0
4155
4156 if (cpi->pass != 1)
4157 {
4158 FILE *f = fopen("skip.stt", "a");
4159 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
4160 fclose(f);
4161 }
4162
4163 #endif
4164
4165 }
4166
4167 if (cm->frame_type == KEY_FRAME)
4168 {
4169 if(resize_key_frame(cpi))
4170 {
4171 /* If the frame size has changed, need to reset Q, quantizer,
4172 * and background refresh.
4173 */
4174 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4175 if (cpi->cyclic_refresh_mode_enabled)
4176 {
4177 if (cpi->current_layer==0)
4178 cyclic_background_refresh(cpi, Q, 0);
4179 else
4180 disable_segmentation(cpi);
4181 }
4182 // Reset the consec_zero_last counter on key frame.
4183 vpx_memset(cpi->consec_zero_last, 0, cm->mb_rows * cm->mb_cols);
4184 vp8_set_quantizer(cpi, Q);
4185 }
4186
4187 vp8_setup_key_frame(cpi);
4188 }
4189
4190
4191
4192 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
4193 {
4194 if(cpi->oxcf.error_resilient_mode)
4195 cm->refresh_entropy_probs = 0;
4196
4197 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
4198 {
4199 if (cm->frame_type == KEY_FRAME)
4200 cm->refresh_entropy_probs = 1;
4201 }
4202
4203 if (cm->refresh_entropy_probs == 0)
4204 {
4205 /* save a copy for later refresh */
4206 vpx_memcpy(&cm->lfc, &cm->fc, sizeof(cm->fc));
4207 }
4208
4209 vp8_update_coef_context(cpi);
4210
4211 vp8_update_coef_probs(cpi);
4212
4213 /* transform / motion compensation build reconstruction frame
4214 * +pack coef partitions
4215 */
4216 vp8_encode_frame(cpi);
4217
4218 /* cpi->projected_frame_size is not needed for RT mode */
4219 }
4220 #else
4221 /* transform / motion compensation build reconstruction frame */
4222 vp8_encode_frame(cpi);
4223
4224 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
4225 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
4226 #endif
4227 vp8_clear_system_state();
4228
4229 /* Test to see if the stats generated for this frame indicate that
4230 * we should have coded a key frame (assuming that we didn't)!
4231 */
4232
4233 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME
4234 && cpi->compressor_speed != 2)
4235 {
4236 #if !(CONFIG_REALTIME_ONLY)
4237 if (decide_key_frame(cpi))
4238 {
4239 /* Reset all our sizing numbers and recode */
4240 cm->frame_type = KEY_FRAME;
4241
4242 vp8_pick_frame_size(cpi);
4243
4244 /* Clear the Alt reference frame active flag when we have
4245 * a key frame
4246 */
4247 cpi->source_alt_ref_active = 0;
4248
4249 // Set the loop filter deltas and segmentation map update
4250 setup_features(cpi);
4251
4252 vp8_restore_coding_context(cpi);
4253
4254 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4255
4256 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
4257
4258 /* Limit Q range for the adaptive loop. */
4259 bottom_index = cpi->active_best_quality;
4260 top_index = cpi->active_worst_quality;
4261 q_low = cpi->active_best_quality;
4262 q_high = cpi->active_worst_quality;
4263
4264 loop_count++;
4265 Loop = 1;
4266
4267 continue;
4268 }
4269 #endif
4270 }
4271
4272 vp8_clear_system_state();
4273
4274 if (frame_over_shoot_limit == 0)
4275 frame_over_shoot_limit = 1;
4276
4277 /* Are we are overshooting and up against the limit of active max Q. */
4278 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4279 (Q == cpi->active_worst_quality) &&
4280 (cpi->active_worst_quality < cpi->worst_quality) &&
4281 (cpi->projected_frame_size > frame_over_shoot_limit))
4282 {
4283 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
4284
4285 /* If so is there any scope for relaxing it */
4286 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
4287 {
4288 cpi->active_worst_quality++;
4289 /* Assume 1 qstep = about 4% on frame size. */
4290 over_size_percent = (int)(over_size_percent * 0.96);
4291 }
4292 #if !(CONFIG_REALTIME_ONLY)
4293 top_index = cpi->active_worst_quality;
4294 #endif
4295 /* If we have updated the active max Q do not call
4296 * vp8_update_rate_correction_factors() this loop.
4297 */
4298 active_worst_qchanged = 1;
4299 }
4300 else
4301 active_worst_qchanged = 0;
4302
4303 #if !(CONFIG_REALTIME_ONLY)
4304 /* Special case handling for forced key frames */
4305 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
4306 {
4307 int last_q = Q;
4308 int kf_err = vp8_calc_ss_err(cpi->Source,
4309 &cm->yv12_fb[cm->new_fb_idx]);
4310
4311 /* The key frame is not good enough */
4312 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
4313 {
4314 /* Lower q_high */
4315 q_high = (Q > q_low) ? (Q - 1) : q_low;
4316
4317 /* Adjust Q */
4318 Q = (q_high + q_low) >> 1;
4319 }
4320 /* The key frame is much better than the previous frame */
4321 else if ( kf_err < (cpi->ambient_err >> 1) )
4322 {
4323 /* Raise q_low */
4324 q_low = (Q < q_high) ? (Q + 1) : q_high;
4325
4326 /* Adjust Q */
4327 Q = (q_high + q_low + 1) >> 1;
4328 }
4329
4330 /* Clamp Q to upper and lower limits: */
4331 if (Q > q_high)
4332 Q = q_high;
4333 else if (Q < q_low)
4334 Q = q_low;
4335
4336 Loop = Q != last_q;
4337 }
4338
4339 /* Is the projected frame size out of range and are we allowed
4340 * to attempt to recode.
4341 */
4342 else if ( recode_loop_test( cpi,
4343 frame_over_shoot_limit, frame_under_shoot_limit,
4344 Q, top_index, bottom_index ) )
4345 {
4346 int last_q = Q;
4347 int Retries = 0;
4348
4349 /* Frame size out of permitted range. Update correction factor
4350 * & compute new Q to try...
4351 */
4352
4353 /* Frame is too large */
4354 if (cpi->projected_frame_size > cpi->this_frame_target)
4355 {
4356 /* Raise Qlow as to at least the current value */
4357 q_low = (Q < q_high) ? (Q + 1) : q_high;
4358
4359 /* If we are using over quant do the same for zbin_oq_low */
4360 if (cpi->mb.zbin_over_quant > 0)
4361 zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high) ?
4362 (cpi->mb.zbin_over_quant + 1) : zbin_oq_high;
4363
4364 if (undershoot_seen)
4365 {
4366 /* Update rate_correction_factor unless
4367 * cpi->active_worst_quality has changed.
4368 */
4369 if (!active_worst_qchanged)
4370 vp8_update_rate_correction_factors(cpi, 1);
4371
4372 Q = (q_high + q_low + 1) / 2;
4373
4374 /* Adjust cpi->zbin_over_quant (only allowed when Q
4375 * is max)
4376 */
4377 if (Q < MAXQ)
4378 cpi->mb.zbin_over_quant = 0;
4379 else
4380 {
4381 zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high) ?
4382 (cpi->mb.zbin_over_quant + 1) : zbin_oq_high;
4383 cpi->mb.zbin_over_quant =
4384 (zbin_oq_high + zbin_oq_low) / 2;
4385 }
4386 }
4387 else
4388 {
4389 /* Update rate_correction_factor unless
4390 * cpi->active_worst_quality has changed.
4391 */
4392 if (!active_worst_qchanged)
4393 vp8_update_rate_correction_factors(cpi, 0);
4394
4395 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4396
4397 while (((Q < q_low) ||
4398 (cpi->mb.zbin_over_quant < zbin_oq_low)) &&
4399 (Retries < 10))
4400 {
4401 vp8_update_rate_correction_factors(cpi, 0);
4402 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4403 Retries ++;
4404 }
4405 }
4406
4407 overshoot_seen = 1;
4408 }
4409 /* Frame is too small */
4410 else
4411 {
4412 if (cpi->mb.zbin_over_quant == 0)
4413 /* Lower q_high if not using over quant */
4414 q_high = (Q > q_low) ? (Q - 1) : q_low;
4415 else
4416 /* else lower zbin_oq_high */
4417 zbin_oq_high = (cpi->mb.zbin_over_quant > zbin_oq_low) ?
4418 (cpi->mb.zbin_over_quant - 1) : zbin_oq_low;
4419
4420 if (overshoot_seen)
4421 {
4422 /* Update rate_correction_factor unless
4423 * cpi->active_worst_quality has changed.
4424 */
4425 if (!active_worst_qchanged)
4426 vp8_update_rate_correction_factors(cpi, 1);
4427
4428 Q = (q_high + q_low) / 2;
4429
4430 /* Adjust cpi->zbin_over_quant (only allowed when Q
4431 * is max)
4432 */
4433 if (Q < MAXQ)
4434 cpi->mb.zbin_over_quant = 0;
4435 else
4436 cpi->mb.zbin_over_quant =
4437 (zbin_oq_high + zbin_oq_low) / 2;
4438 }
4439 else
4440 {
4441 /* Update rate_correction_factor unless
4442 * cpi->active_worst_quality has changed.
4443 */
4444 if (!active_worst_qchanged)
4445 vp8_update_rate_correction_factors(cpi, 0);
4446
4447 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4448
4449 /* Special case reset for qlow for constrained quality.
4450 * This should only trigger where there is very substantial
4451 * undershoot on a frame and the auto cq level is above
4452 * the user passsed in value.
4453 */
4454 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4455 (Q < q_low) )
4456 {
4457 q_low = Q;
4458 }
4459
4460 while (((Q > q_high) ||
4461 (cpi->mb.zbin_over_quant > zbin_oq_high)) &&
4462 (Retries < 10))
4463 {
4464 vp8_update_rate_correction_factors(cpi, 0);
4465 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4466 Retries ++;
4467 }
4468 }
4469
4470 undershoot_seen = 1;
4471 }
4472
4473 /* Clamp Q to upper and lower limits: */
4474 if (Q > q_high)
4475 Q = q_high;
4476 else if (Q < q_low)
4477 Q = q_low;
4478
4479 /* Clamp cpi->zbin_over_quant */
4480 cpi->mb.zbin_over_quant = (cpi->mb.zbin_over_quant < zbin_oq_low) ?
4481 zbin_oq_low : (cpi->mb.zbin_over_quant > zbin_oq_high) ?
4482 zbin_oq_high : cpi->mb.zbin_over_quant;
4483
4484 Loop = Q != last_q;
4485 }
4486 else
4487 #endif
4488 Loop = 0;
4489
4490 if (cpi->is_src_frame_alt_ref)
4491 Loop = 0;
4492
4493 if (Loop == 1)
4494 {
4495 vp8_restore_coding_context(cpi);
4496 loop_count++;
4497 #if CONFIG_INTERNAL_STATS
4498 cpi->tot_recode_hits++;
4499 #endif
4500 }
4501 }
4502 while (Loop == 1);
4503
4504 #if 0
4505 /* Experimental code for lagged and one pass
4506 * Update stats used for one pass GF selection
4507 */
4508 {
4509 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4510 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4511 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4512 }
4513 #endif
4514
4515 /* Special case code to reduce pulsing when key frames are forced at a
4516 * fixed interval. Note the reconstruction error if it is the frame before
4517 * the force key frame
4518 */
4519 if ( cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0) )
4520 {
4521 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
4522 &cm->yv12_fb[cm->new_fb_idx]);
4523 }
4524
4525 /* This frame's MVs are saved and will be used in next frame's MV predictor.
4526 * Last frame has one more line(add to bottom) and one more column(add to
4527 * right) than cm->mip. The edge elements are initialized to 0.
4528 */
4529 #if CONFIG_MULTI_RES_ENCODING
4530 if(!cpi->oxcf.mr_encoder_id && cm->show_frame)
4531 #else
4532 if(cm->show_frame) /* do not save for altref frame */
4533 #endif
4534 {
4535 int mb_row;
4536 int mb_col;
4537 /* Point to beginning of allocated MODE_INFO arrays. */
4538 MODE_INFO *tmp = cm->mip;
4539
4540 if(cm->frame_type != KEY_FRAME)
4541 {
4542 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
4543 {
4544 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
4545 {
4546 if(tmp->mbmi.ref_frame != INTRA_FRAME)
4547 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
4548
4549 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4550 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
4551 tmp++;
4552 }
4553 }
4554 }
4555 }
4556
4557 /* Count last ref frame 0,0 usage on current encoded frame. */
4558 {
4559 int mb_row;
4560 int mb_col;
4561 /* Point to beginning of MODE_INFO arrays. */
4562 MODE_INFO *tmp = cm->mi;
4563
4564 cpi->zeromv_count = 0;
4565
4566 if(cm->frame_type != KEY_FRAME)
4567 {
4568 for (mb_row = 0; mb_row < cm->mb_rows; mb_row ++)
4569 {
4570 for (mb_col = 0; mb_col < cm->mb_cols; mb_col ++)
4571 {
4572 if (tmp->mbmi.mode == ZEROMV &&
4573 tmp->mbmi.ref_frame == LAST_FRAME)
4574 cpi->zeromv_count++;
4575 tmp++;
4576 }
4577 tmp++;
4578 }
4579 }
4580 }
4581
4582 #if CONFIG_MULTI_RES_ENCODING
4583 vp8_cal_dissimilarity(cpi);
4584 #endif
4585
4586 /* Update the GF useage maps.
4587 * This is done after completing the compression of a frame when all
4588 * modes etc. are finalized but before loop filter
4589 */
4590 if (cpi->oxcf.number_of_layers == 1)
4591 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
4592
4593 if (cm->frame_type == KEY_FRAME)
4594 cm->refresh_last_frame = 1;
4595
4596 #if 0
4597 {
4598 FILE *f = fopen("gfactive.stt", "a");
4599 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);
4600 fclose(f);
4601 }
4602 #endif
4603
4604 /* For inter frames the current default behavior is that when
4605 * cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4606 * This is purely an encoder decision at present.
4607 */
4608 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
4609 cm->copy_buffer_to_arf = 2;
4610 else
4611 cm->copy_buffer_to_arf = 0;
4612
4613 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4614
4615 #if CONFIG_TEMPORAL_DENOISING
4616 // For the adaptive denoising mode (noise_sensitivity == 4), sample the mse
4617 // of source diff (between current and previous frame), and determine if we
4618 // should switch the denoiser mode. Sampling refers to computing the mse for
4619 // a sub-sample of the frame (i.e., skip x blocks along row/column), and
4620 // only for blocks in that set that have used ZEROMV LAST, along with some
4621 // constraint on the sum diff between blocks. This process is called every
4622 // ~8 frames, to further reduce complexity.
4623 if (cpi->oxcf.noise_sensitivity == 4 &&
4624 cpi->frames_since_key % 8 == 0 &&
4625 cm->frame_type != KEY_FRAME) {
4626 process_denoiser_mode_change(cpi);
4627 }
4628 #endif
4629
4630 #if CONFIG_MULTITHREAD
4631 if (cpi->b_multi_threaded)
4632 {
4633 /* start loopfilter in separate thread */
4634 sem_post(&cpi->h_event_start_lpf);
4635 cpi->b_lpf_running = 1;
4636 }
4637 else
4638 #endif
4639 {
4640 vp8_loopfilter_frame(cpi, cm);
4641 }
4642
4643 update_reference_frames(cpi);
4644
4645 #ifdef OUTPUT_YUV_DENOISED
4646 vp8_write_yuv_frame(yuv_denoised_file,
4647 &cpi->denoiser.yv12_running_avg[INTRA_FRAME]);
4648 #endif
4649
4650 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
4651 if (cpi->oxcf.error_resilient_mode)
4652 {
4653 cm->refresh_entropy_probs = 0;
4654 }
4655 #endif
4656
4657 #if CONFIG_MULTITHREAD
4658 /* wait that filter_level is picked so that we can continue with stream packing */
4659 if (cpi->b_multi_threaded)
4660 sem_wait(&cpi->h_event_end_lpf);
4661 #endif
4662
4663 /* build the bitstream */
4664 vp8_pack_bitstream(cpi, dest, dest_end, size);
4665
4666 #if CONFIG_MULTITHREAD
4667 /* if PSNR packets are generated we have to wait for the lpf */
4668 if (cpi->b_lpf_running && cpi->b_calculate_psnr)
4669 {
4670 sem_wait(&cpi->h_event_end_lpf);
4671 cpi->b_lpf_running = 0;
4672 }
4673 #endif
4674
4675 /* Move storing frame_type out of the above loop since it is also
4676 * needed in motion search besides loopfilter */
4677 cm->last_frame_type = cm->frame_type;
4678
4679 /* Update rate control heuristics */
4680 cpi->total_byte_count += (*size);
4681 cpi->projected_frame_size = (*size) << 3;
4682
4683 if (cpi->oxcf.number_of_layers > 1)
4684 {
4685 unsigned int i;
4686 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4687 cpi->layer_context[i].total_byte_count += (*size);
4688 }
4689
4690 if (!active_worst_qchanged)
4691 vp8_update_rate_correction_factors(cpi, 2);
4692
4693 cpi->last_q[cm->frame_type] = cm->base_qindex;
4694
4695 if (cm->frame_type == KEY_FRAME)
4696 {
4697 vp8_adjust_key_frame_context(cpi);
4698 }
4699
4700 /* Keep a record of ambient average Q. */
4701 if (cm->frame_type != KEY_FRAME)
4702 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4703
4704 /* Keep a record from which we can calculate the average Q excluding
4705 * GF updates and key frames
4706 */
4707 if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) ||
4708 (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)))
4709 {
4710 cpi->ni_frames++;
4711
4712 /* Calculate the average Q for normal inter frames (not key or GFU
4713 * frames).
4714 */
4715 if ( cpi->pass == 2 )
4716 {
4717 cpi->ni_tot_qi += Q;
4718 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4719 }
4720 else
4721 {
4722 /* Damp value for first few frames */
4723 if (cpi->ni_frames > 150 )
4724 {
4725 cpi->ni_tot_qi += Q;
4726 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4727 }
4728 /* For one pass, early in the clip ... average the current frame Q
4729 * value with the worstq entered by the user as a dampening measure
4730 */
4731 else
4732 {
4733 cpi->ni_tot_qi += Q;
4734 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4735 }
4736
4737 /* If the average Q is higher than what was used in the last
4738 * frame (after going through the recode loop to keep the frame
4739 * size within range) then use the last frame value - 1. The -1
4740 * is designed to stop Q and hence the data rate, from
4741 * progressively falling away during difficult sections, but at
4742 * the same time reduce the number of itterations around the
4743 * recode loop.
4744 */
4745 if (Q > cpi->ni_av_qi)
4746 cpi->ni_av_qi = Q - 1;
4747 }
4748 }
4749
4750 /* Update the buffer level variable. */
4751 /* Non-viewable frames are a special case and are treated as pure overhead. */
4752 if ( !cm->show_frame )
4753 cpi->bits_off_target -= cpi->projected_frame_size;
4754 else
4755 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4756
4757 /* Clip the buffer level to the maximum specified buffer size */
4758 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
4759 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
4760
4761 /* Rolling monitors of whether we are over or underspending used to
4762 * help regulate min and Max Q in two pass.
4763 */
4764 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
4765 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
4766 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
4767 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
4768
4769 /* Actual bits spent */
4770 cpi->total_actual_bits += cpi->projected_frame_size;
4771
4772 /* Debug stats */
4773 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
4774
4775 cpi->buffer_level = cpi->bits_off_target;
4776
4777 /* Propagate values to higher temporal layers */
4778 if (cpi->oxcf.number_of_layers > 1)
4779 {
4780 unsigned int i;
4781
4782 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
4783 {
4784 LAYER_CONTEXT *lc = &cpi->layer_context[i];
4785 int bits_off_for_this_layer =
4786 (int)(lc->target_bandwidth / lc->framerate -
4787 cpi->projected_frame_size);
4788
4789 lc->bits_off_target += bits_off_for_this_layer;
4790
4791 /* Clip buffer level to maximum buffer size for the layer */
4792 if (lc->bits_off_target > lc->maximum_buffer_size)
4793 lc->bits_off_target = lc->maximum_buffer_size;
4794
4795 lc->total_actual_bits += cpi->projected_frame_size;
4796 lc->total_target_vs_actual += bits_off_for_this_layer;
4797 lc->buffer_level = lc->bits_off_target;
4798 }
4799 }
4800
4801 /* Update bits left to the kf and gf groups to account for overshoot
4802 * or undershoot on these frames
4803 */
4804 if (cm->frame_type == KEY_FRAME)
4805 {
4806 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4807
4808 if (cpi->twopass.kf_group_bits < 0)
4809 cpi->twopass.kf_group_bits = 0 ;
4810 }
4811 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
4812 {
4813 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
4814
4815 if (cpi->twopass.gf_group_bits < 0)
4816 cpi->twopass.gf_group_bits = 0 ;
4817 }
4818
4819 if (cm->frame_type != KEY_FRAME)
4820 {
4821 if (cpi->common.refresh_alt_ref_frame)
4822 {
4823 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4824 cpi->last_skip_probs_q[2] = cm->base_qindex;
4825 }
4826 else if (cpi->common.refresh_golden_frame)
4827 {
4828 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4829 cpi->last_skip_probs_q[1] = cm->base_qindex;
4830 }
4831 else
4832 {
4833 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4834 cpi->last_skip_probs_q[0] = cm->base_qindex;
4835
4836 /* update the baseline */
4837 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4838
4839 }
4840 }
4841
4842 #if 0 && CONFIG_INTERNAL_STATS
4843 {
4844 FILE *f = fopen("tmp.stt", "a");
4845
4846 vp8_clear_system_state();
4847
4848 if (cpi->twopass.total_left_stats.coded_error != 0.0)
4849 fprintf(f, "%10d %10d %10d %10d %10d %10"PRId64" %10"PRId64
4850 "%10"PRId64" %10d %6d %6d %6d %6d %5d %5d %5d %8d "
4851 "%8.2lf %"PRId64" %10.3lf %10"PRId64" %8d\n",
4852 cpi->common.current_video_frame, cpi->this_frame_target,
4853 cpi->projected_frame_size,
4854 (cpi->projected_frame_size - cpi->this_frame_target),
4855 cpi->total_target_vs_actual,
4856 cpi->buffer_level,
4857 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4858 cpi->total_actual_bits, cm->base_qindex,
4859 cpi->active_best_quality, cpi->active_worst_quality,
4860 cpi->ni_av_qi, cpi->cq_target_quality,
4861 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4862 cm->frame_type, cpi->gfu_boost,
4863 cpi->twopass.est_max_qcorrection_factor,
4864 cpi->twopass.bits_left,
4865 cpi->twopass.total_left_stats.coded_error,
4866 (double)cpi->twopass.bits_left /
4867 cpi->twopass.total_left_stats.coded_error,
4868 cpi->tot_recode_hits);
4869 else
4870 fprintf(f, "%10d %10d %10d %10d %10d %10"PRId64" %10"PRId64
4871 "%10"PRId64" %10d %6d %6d %6d %6d %5d %5d %5d %8d "
4872 "%8.2lf %"PRId64" %10.3lf %8d\n",
4873 cpi->common.current_video_frame, cpi->this_frame_target,
4874 cpi->projected_frame_size,
4875 (cpi->projected_frame_size - cpi->this_frame_target),
4876 cpi->total_target_vs_actual,
4877 cpi->buffer_level,
4878 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4879 cpi->total_actual_bits, cm->base_qindex,
4880 cpi->active_best_quality, cpi->active_worst_quality,
4881 cpi->ni_av_qi, cpi->cq_target_quality,
4882 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4883 cm->frame_type, cpi->gfu_boost,
4884 cpi->twopass.est_max_qcorrection_factor,
4885 cpi->twopass.bits_left,
4886 cpi->twopass.total_left_stats.coded_error,
4887 cpi->tot_recode_hits);
4888
4889 fclose(f);
4890
4891 {
4892 FILE *fmodes = fopen("Modes.stt", "a");
4893
4894 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4895 cpi->common.current_video_frame,
4896 cm->frame_type, cm->refresh_golden_frame,
4897 cm->refresh_alt_ref_frame);
4898
4899 fprintf(fmodes, "\n");
4900
4901 fclose(fmodes);
4902 }
4903 }
4904
4905 #endif
4906
4907 if (cm->refresh_golden_frame == 1)
4908 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4909 else
4910 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
4911
4912 if (cm->refresh_alt_ref_frame == 1)
4913 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4914 else
4915 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
4916
4917
4918 if (cm->refresh_last_frame & cm->refresh_golden_frame)
4919 /* both refreshed */
4920 cpi->gold_is_last = 1;
4921 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame)
4922 /* 1 refreshed but not the other */
4923 cpi->gold_is_last = 0;
4924
4925 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame)
4926 /* both refreshed */
4927 cpi->alt_is_last = 1;
4928 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame)
4929 /* 1 refreshed but not the other */
4930 cpi->alt_is_last = 0;
4931
4932 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame)
4933 /* both refreshed */
4934 cpi->gold_is_alt = 1;
4935 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame)
4936 /* 1 refreshed but not the other */
4937 cpi->gold_is_alt = 0;
4938
4939 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
4940
4941 if (cpi->gold_is_last)
4942 cpi->ref_frame_flags &= ~VP8_GOLD_FRAME;
4943
4944 if (cpi->alt_is_last)
4945 cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4946
4947 if (cpi->gold_is_alt)
4948 cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4949
4950
4951 if (!cpi->oxcf.error_resilient_mode)
4952 {
4953 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
4954 /* Update the alternate reference frame stats as appropriate. */
4955 update_alt_ref_frame_stats(cpi);
4956 else
4957 /* Update the Golden frame stats as appropriate. */
4958 update_golden_frame_stats(cpi);
4959 }
4960
4961 if (cm->frame_type == KEY_FRAME)
4962 {
4963 /* Tell the caller that the frame was coded as a key frame */
4964 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4965
4966 /* As this frame is a key frame the next defaults to an inter frame. */
4967 cm->frame_type = INTER_FRAME;
4968
4969 cpi->last_frame_percent_intra = 100;
4970 }
4971 else
4972 {
4973 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
4974
4975 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4976 }
4977
4978 /* Clear the one shot update flags for segmentation map and mode/ref
4979 * loop filter deltas.
4980 */
4981 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4982 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4983 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4984
4985
4986 /* Dont increment frame counters if this was an altref buffer update
4987 * not a real frame
4988 */
4989 if (cm->show_frame)
4990 {
4991 cm->current_video_frame++;
4992 cpi->frames_since_key++;
4993 cpi->temporal_pattern_counter++;
4994 }
4995
4996 /* reset to normal state now that we are done. */
4997
4998
4999
5000 #if 0
5001 {
5002 char filename[512];
5003 FILE *recon_file;
5004 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
5005 recon_file = fopen(filename, "wb");
5006 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
5007 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
5008 fclose(recon_file);
5009 }
5010 #endif
5011
5012 /* DEBUG */
5013 /* vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show); */
5014
5015
5016 }
5017 #if !(CONFIG_REALTIME_ONLY)
Pass2Encode(VP8_COMP * cpi,unsigned long * size,unsigned char * dest,unsigned char * dest_end,unsigned int * frame_flags)5018 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned char * dest_end, unsigned int *frame_flags)
5019 {
5020
5021 if (!cpi->common.refresh_alt_ref_frame)
5022 vp8_second_pass(cpi);
5023
5024 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
5025 cpi->twopass.bits_left -= 8 * *size;
5026
5027 if (!cpi->common.refresh_alt_ref_frame)
5028 {
5029 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
5030 *cpi->oxcf.two_pass_vbrmin_section / 100);
5031 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->framerate);
5032 }
5033 }
5034 #endif
5035
vp8_receive_raw_frame(VP8_COMP * cpi,unsigned int frame_flags,YV12_BUFFER_CONFIG * sd,int64_t time_stamp,int64_t end_time)5036 int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time)
5037 {
5038 struct vpx_usec_timer timer;
5039 int res = 0;
5040
5041 vpx_usec_timer_start(&timer);
5042
5043 /* Reinit the lookahead buffer if the frame size changes */
5044 if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height)
5045 {
5046 assert(cpi->oxcf.lag_in_frames < 2);
5047 dealloc_raw_frame_buffers(cpi);
5048 alloc_raw_frame_buffers(cpi);
5049 }
5050
5051 if(vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
5052 frame_flags, cpi->active_map_enabled ? cpi->active_map : NULL))
5053 res = -1;
5054 vpx_usec_timer_mark(&timer);
5055 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
5056
5057 return res;
5058 }
5059
5060
frame_is_reference(const VP8_COMP * cpi)5061 static int frame_is_reference(const VP8_COMP *cpi)
5062 {
5063 const VP8_COMMON *cm = &cpi->common;
5064 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
5065
5066 return cm->frame_type == KEY_FRAME || cm->refresh_last_frame
5067 || cm->refresh_golden_frame || cm->refresh_alt_ref_frame
5068 || cm->copy_buffer_to_gf || cm->copy_buffer_to_arf
5069 || cm->refresh_entropy_probs
5070 || xd->mode_ref_lf_delta_update
5071 || xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
5072 }
5073
5074
vp8_get_compressed_data(VP8_COMP * cpi,unsigned int * frame_flags,unsigned long * size,unsigned char * dest,unsigned char * dest_end,int64_t * time_stamp,int64_t * time_end,int flush)5075 int vp8_get_compressed_data(VP8_COMP *cpi, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, unsigned char *dest_end, int64_t *time_stamp, int64_t *time_end, int flush)
5076 {
5077 VP8_COMMON *cm;
5078 struct vpx_usec_timer tsctimer;
5079 struct vpx_usec_timer ticktimer;
5080 struct vpx_usec_timer cmptimer;
5081 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5082
5083 if (!cpi)
5084 return -1;
5085
5086 cm = &cpi->common;
5087
5088 if (setjmp(cpi->common.error.jmp))
5089 {
5090 cpi->common.error.setjmp = 0;
5091 vp8_clear_system_state();
5092 return VPX_CODEC_CORRUPT_FRAME;
5093 }
5094
5095 cpi->common.error.setjmp = 1;
5096
5097 vpx_usec_timer_start(&cmptimer);
5098
5099 cpi->source = NULL;
5100
5101 #if !(CONFIG_REALTIME_ONLY)
5102 /* Should we code an alternate reference frame */
5103 if (cpi->oxcf.error_resilient_mode == 0 &&
5104 cpi->oxcf.play_alternate &&
5105 cpi->source_alt_ref_pending)
5106 {
5107 if ((cpi->source = vp8_lookahead_peek(cpi->lookahead,
5108 cpi->frames_till_gf_update_due,
5109 PEEK_FORWARD)))
5110 {
5111 cpi->alt_ref_source = cpi->source;
5112 if (cpi->oxcf.arnr_max_frames > 0)
5113 {
5114 vp8_temporal_filter_prepare_c(cpi,
5115 cpi->frames_till_gf_update_due);
5116 force_src_buffer = &cpi->alt_ref_buffer;
5117 }
5118 cpi->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
5119 cm->refresh_alt_ref_frame = 1;
5120 cm->refresh_golden_frame = 0;
5121 cm->refresh_last_frame = 0;
5122 cm->show_frame = 0;
5123 /* Clear Pending alt Ref flag. */
5124 cpi->source_alt_ref_pending = 0;
5125 cpi->is_src_frame_alt_ref = 0;
5126 }
5127 }
5128 #endif
5129
5130 if (!cpi->source)
5131 {
5132 /* Read last frame source if we are encoding first pass. */
5133 if (cpi->pass == 1 && cm->current_video_frame > 0)
5134 {
5135 if((cpi->last_source = vp8_lookahead_peek(cpi->lookahead, 1,
5136 PEEK_BACKWARD)) == NULL)
5137 return -1;
5138 }
5139
5140
5141 if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush)))
5142 {
5143 cm->show_frame = 1;
5144
5145 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
5146 && (cpi->source == cpi->alt_ref_source);
5147
5148 if(cpi->is_src_frame_alt_ref)
5149 cpi->alt_ref_source = NULL;
5150 }
5151 }
5152
5153 if (cpi->source)
5154 {
5155 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
5156 cpi->un_scaled_source = cpi->Source;
5157 *time_stamp = cpi->source->ts_start;
5158 *time_end = cpi->source->ts_end;
5159 *frame_flags = cpi->source->flags;
5160
5161 if (cpi->pass == 1 && cm->current_video_frame > 0)
5162 {
5163 cpi->last_frame_unscaled_source = &cpi->last_source->img;
5164 }
5165 }
5166 else
5167 {
5168 *size = 0;
5169 #if !(CONFIG_REALTIME_ONLY)
5170
5171 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done)
5172 {
5173 vp8_end_first_pass(cpi); /* get last stats packet */
5174 cpi->twopass.first_pass_done = 1;
5175 }
5176
5177 #endif
5178
5179 return -1;
5180 }
5181
5182 if (cpi->source->ts_start < cpi->first_time_stamp_ever)
5183 {
5184 cpi->first_time_stamp_ever = cpi->source->ts_start;
5185 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
5186 }
5187
5188 /* adjust frame rates based on timestamps given */
5189 if (cm->show_frame)
5190 {
5191 int64_t this_duration;
5192 int step = 0;
5193
5194 if (cpi->source->ts_start == cpi->first_time_stamp_ever)
5195 {
5196 this_duration = cpi->source->ts_end - cpi->source->ts_start;
5197 step = 1;
5198 }
5199 else
5200 {
5201 int64_t last_duration;
5202
5203 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
5204 last_duration = cpi->last_end_time_stamp_seen
5205 - cpi->last_time_stamp_seen;
5206 /* do a step update if the duration changes by 10% */
5207 if (last_duration)
5208 step = (int)(((this_duration - last_duration) *
5209 10 / last_duration));
5210 }
5211
5212 if (this_duration)
5213 {
5214 if (step)
5215 cpi->ref_framerate = 10000000.0 / this_duration;
5216 else
5217 {
5218 double avg_duration, interval;
5219
5220 /* Average this frame's rate into the last second's average
5221 * frame rate. If we haven't seen 1 second yet, then average
5222 * over the whole interval seen.
5223 */
5224 interval = (double)(cpi->source->ts_end -
5225 cpi->first_time_stamp_ever);
5226 if(interval > 10000000.0)
5227 interval = 10000000;
5228
5229 avg_duration = 10000000.0 / cpi->ref_framerate;
5230 avg_duration *= (interval - avg_duration + this_duration);
5231 avg_duration /= interval;
5232
5233 cpi->ref_framerate = 10000000.0 / avg_duration;
5234 }
5235
5236 if (cpi->oxcf.number_of_layers > 1)
5237 {
5238 unsigned int i;
5239
5240 /* Update frame rates for each layer */
5241 assert(cpi->oxcf.number_of_layers <= VPX_TS_MAX_LAYERS);
5242 for (i = 0; i < cpi->oxcf.number_of_layers &&
5243 i < VPX_TS_MAX_LAYERS; ++i)
5244 {
5245 LAYER_CONTEXT *lc = &cpi->layer_context[i];
5246 lc->framerate = cpi->ref_framerate /
5247 cpi->oxcf.rate_decimator[i];
5248 }
5249 }
5250 else
5251 vp8_new_framerate(cpi, cpi->ref_framerate);
5252 }
5253
5254 cpi->last_time_stamp_seen = cpi->source->ts_start;
5255 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
5256 }
5257
5258 if (cpi->oxcf.number_of_layers > 1)
5259 {
5260 int layer;
5261
5262 update_layer_contexts (cpi);
5263
5264 /* Restore layer specific context & set frame rate */
5265 layer = cpi->oxcf.layer_id[
5266 cpi->temporal_pattern_counter % cpi->oxcf.periodicity];
5267 restore_layer_context (cpi, layer);
5268 vp8_new_framerate(cpi, cpi->layer_context[layer].framerate);
5269 }
5270
5271 if (cpi->compressor_speed == 2)
5272 {
5273 vpx_usec_timer_start(&tsctimer);
5274 vpx_usec_timer_start(&ticktimer);
5275 }
5276
5277 cpi->lf_zeromv_pct = (cpi->zeromv_count * 100)/cm->MBs;
5278
5279 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
5280 {
5281 int i;
5282 const int num_part = (1 << cm->multi_token_partition);
5283 /* the available bytes in dest */
5284 const unsigned long dest_size = dest_end - dest;
5285 const int tok_part_buff_size = (dest_size * 9) / (10 * num_part);
5286
5287 unsigned char *dp = dest;
5288
5289 cpi->partition_d[0] = dp;
5290 dp += dest_size/10; /* reserve 1/10 for control partition */
5291 cpi->partition_d_end[0] = dp;
5292
5293 for(i = 0; i < num_part; i++)
5294 {
5295 cpi->partition_d[i + 1] = dp;
5296 dp += tok_part_buff_size;
5297 cpi->partition_d_end[i + 1] = dp;
5298 }
5299 }
5300 #endif
5301
5302 /* start with a 0 size frame */
5303 *size = 0;
5304
5305 /* Clear down mmx registers */
5306 vp8_clear_system_state();
5307
5308 cm->frame_type = INTER_FRAME;
5309 cm->frame_flags = *frame_flags;
5310
5311 #if 0
5312
5313 if (cm->refresh_alt_ref_frame)
5314 {
5315 cm->refresh_golden_frame = 0;
5316 cm->refresh_last_frame = 0;
5317 }
5318 else
5319 {
5320 cm->refresh_golden_frame = 0;
5321 cm->refresh_last_frame = 1;
5322 }
5323
5324 #endif
5325 /* find a free buffer for the new frame */
5326 {
5327 int i = 0;
5328 for(; i < NUM_YV12_BUFFERS; i++)
5329 {
5330 if(!cm->yv12_fb[i].flags)
5331 {
5332 cm->new_fb_idx = i;
5333 break;
5334 }
5335 }
5336
5337 assert(i < NUM_YV12_BUFFERS );
5338 }
5339 #if !(CONFIG_REALTIME_ONLY)
5340
5341 if (cpi->pass == 1)
5342 {
5343 Pass1Encode(cpi, size, dest, frame_flags);
5344 }
5345 else if (cpi->pass == 2)
5346 {
5347 Pass2Encode(cpi, size, dest, dest_end, frame_flags);
5348 }
5349 else
5350 #endif
5351 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
5352
5353 if (cpi->compressor_speed == 2)
5354 {
5355 unsigned int duration, duration2;
5356 vpx_usec_timer_mark(&tsctimer);
5357 vpx_usec_timer_mark(&ticktimer);
5358
5359 duration = (int)(vpx_usec_timer_elapsed(&ticktimer));
5360 duration2 = (unsigned int)((double)duration / 2);
5361
5362 if (cm->frame_type != KEY_FRAME)
5363 {
5364 if (cpi->avg_encode_time == 0)
5365 cpi->avg_encode_time = duration;
5366 else
5367 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5368 }
5369
5370 if (duration2)
5371 {
5372 {
5373
5374 if (cpi->avg_pick_mode_time == 0)
5375 cpi->avg_pick_mode_time = duration2;
5376 else
5377 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5378 }
5379 }
5380
5381 }
5382
5383 if (cm->refresh_entropy_probs == 0)
5384 {
5385 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
5386 }
5387
5388 /* Save the contexts separately for alt ref, gold and last. */
5389 /* (TODO jbb -> Optimize this with pointers to avoid extra copies. ) */
5390 if(cm->refresh_alt_ref_frame)
5391 vpx_memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc));
5392
5393 if(cm->refresh_golden_frame)
5394 vpx_memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc));
5395
5396 if(cm->refresh_last_frame)
5397 vpx_memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc));
5398
5399 /* if its a dropped frame honor the requests on subsequent frames */
5400 if (*size > 0)
5401 {
5402 cpi->droppable = !frame_is_reference(cpi);
5403
5404 /* return to normal state */
5405 cm->refresh_entropy_probs = 1;
5406 cm->refresh_alt_ref_frame = 0;
5407 cm->refresh_golden_frame = 0;
5408 cm->refresh_last_frame = 1;
5409 cm->frame_type = INTER_FRAME;
5410
5411 }
5412
5413 /* Save layer specific state */
5414 if (cpi->oxcf.number_of_layers > 1)
5415 save_layer_context (cpi);
5416
5417 vpx_usec_timer_mark(&cmptimer);
5418 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5419
5420 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
5421 {
5422 generate_psnr_packet(cpi);
5423 }
5424
5425 #if CONFIG_INTERNAL_STATS
5426
5427 if (cpi->pass != 1)
5428 {
5429 cpi->bytes += *size;
5430
5431 if (cm->show_frame)
5432 {
5433 cpi->common.show_frame_mi = cpi->common.mi;
5434 cpi->count ++;
5435
5436 if (cpi->b_calculate_psnr)
5437 {
5438 uint64_t ye,ue,ve;
5439 double frame_psnr;
5440 YV12_BUFFER_CONFIG *orig = cpi->Source;
5441 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5442 int y_samples = orig->y_height * orig->y_width ;
5443 int uv_samples = orig->uv_height * orig->uv_width ;
5444 int t_samples = y_samples + 2 * uv_samples;
5445 double sq_error;
5446
5447 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5448 recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height);
5449
5450 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5451 recon->u_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
5452
5453 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5454 recon->v_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
5455
5456 sq_error = (double)(ye + ue + ve);
5457
5458 frame_psnr = vpx_sse_to_psnr(t_samples, 255.0, sq_error);
5459
5460 cpi->total_y += vpx_sse_to_psnr(y_samples, 255.0, (double)ye);
5461 cpi->total_u += vpx_sse_to_psnr(uv_samples, 255.0, (double)ue);
5462 cpi->total_v += vpx_sse_to_psnr(uv_samples, 255.0, (double)ve);
5463 cpi->total_sq_error += sq_error;
5464 cpi->total += frame_psnr;
5465 #if CONFIG_POSTPROC
5466 {
5467 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5468 double sq_error2;
5469 double frame_psnr2, frame_ssim2 = 0;
5470 double weight = 0;
5471
5472 vp8_deblock(cm, cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0);
5473 vp8_clear_system_state();
5474
5475 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
5476 pp->y_buffer, pp->y_stride, orig->y_width, orig->y_height);
5477
5478 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
5479 pp->u_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
5480
5481 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
5482 pp->v_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
5483
5484 sq_error2 = (double)(ye + ue + ve);
5485
5486 frame_psnr2 = vpx_sse_to_psnr(t_samples, 255.0, sq_error2);
5487
5488 cpi->totalp_y += vpx_sse_to_psnr(y_samples,
5489 255.0, (double)ye);
5490 cpi->totalp_u += vpx_sse_to_psnr(uv_samples,
5491 255.0, (double)ue);
5492 cpi->totalp_v += vpx_sse_to_psnr(uv_samples,
5493 255.0, (double)ve);
5494 cpi->total_sq_error2 += sq_error2;
5495 cpi->totalp += frame_psnr2;
5496
5497 frame_ssim2 = vp8_calc_ssim(cpi->Source,
5498 &cm->post_proc_buffer, 1, &weight);
5499
5500 cpi->summed_quality += frame_ssim2 * weight;
5501 cpi->summed_weights += weight;
5502
5503 if (cpi->oxcf.number_of_layers > 1)
5504 {
5505 unsigned int i;
5506
5507 for (i=cpi->current_layer;
5508 i<cpi->oxcf.number_of_layers; i++)
5509 {
5510 cpi->frames_in_layer[i]++;
5511
5512 cpi->bytes_in_layer[i] += *size;
5513 cpi->sum_psnr[i] += frame_psnr;
5514 cpi->sum_psnr_p[i] += frame_psnr2;
5515 cpi->total_error2[i] += sq_error;
5516 cpi->total_error2_p[i] += sq_error2;
5517 cpi->sum_ssim[i] += frame_ssim2 * weight;
5518 cpi->sum_weights[i] += weight;
5519 }
5520 }
5521 }
5522 #endif
5523 }
5524
5525 if (cpi->b_calculate_ssimg)
5526 {
5527 double y, u, v, frame_all;
5528 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show,
5529 &y, &u, &v);
5530
5531 if (cpi->oxcf.number_of_layers > 1)
5532 {
5533 unsigned int i;
5534
5535 for (i=cpi->current_layer;
5536 i<cpi->oxcf.number_of_layers; i++)
5537 {
5538 if (!cpi->b_calculate_psnr)
5539 cpi->frames_in_layer[i]++;
5540
5541 cpi->total_ssimg_y_in_layer[i] += y;
5542 cpi->total_ssimg_u_in_layer[i] += u;
5543 cpi->total_ssimg_v_in_layer[i] += v;
5544 cpi->total_ssimg_all_in_layer[i] += frame_all;
5545 }
5546 }
5547 else
5548 {
5549 cpi->total_ssimg_y += y;
5550 cpi->total_ssimg_u += u;
5551 cpi->total_ssimg_v += v;
5552 cpi->total_ssimg_all += frame_all;
5553 }
5554 }
5555
5556 }
5557 }
5558
5559 #if 0
5560
5561 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5562 {
5563 skiptruecount += cpi->skip_true_count;
5564 skipfalsecount += cpi->skip_false_count;
5565 }
5566
5567 #endif
5568 #if 0
5569
5570 if (cpi->pass != 1)
5571 {
5572 FILE *f = fopen("skip.stt", "a");
5573 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);
5574
5575 if (cpi->is_src_frame_alt_ref == 1)
5576 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5577
5578 fclose(f);
5579 }
5580
5581 #endif
5582 #endif
5583
5584 cpi->common.error.setjmp = 0;
5585
5586 return 0;
5587 }
5588
vp8_get_preview_raw_frame(VP8_COMP * cpi,YV12_BUFFER_CONFIG * dest,vp8_ppflags_t * flags)5589 int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
5590 {
5591 if (cpi->common.refresh_alt_ref_frame)
5592 return -1;
5593 else
5594 {
5595 int ret;
5596
5597 #if CONFIG_MULTITHREAD
5598 if(cpi->b_lpf_running)
5599 {
5600 sem_wait(&cpi->h_event_end_lpf);
5601 cpi->b_lpf_running = 0;
5602 }
5603 #endif
5604
5605 #if CONFIG_POSTPROC
5606 cpi->common.show_frame_mi = cpi->common.mi;
5607 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5608 #else
5609
5610 if (cpi->common.frame_to_show)
5611 {
5612 *dest = *cpi->common.frame_to_show;
5613 dest->y_width = cpi->common.Width;
5614 dest->y_height = cpi->common.Height;
5615 dest->uv_height = cpi->common.Height / 2;
5616 ret = 0;
5617 }
5618 else
5619 {
5620 ret = -1;
5621 }
5622
5623 #endif
5624 vp8_clear_system_state();
5625 return ret;
5626 }
5627 }
5628
vp8_set_roimap(VP8_COMP * cpi,unsigned char * map,unsigned int rows,unsigned int cols,int delta_q[4],int delta_lf[4],unsigned int threshold[4])5629 int vp8_set_roimap(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols, int delta_q[4], int delta_lf[4], unsigned int threshold[4])
5630 {
5631 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5632 int internal_delta_q[MAX_MB_SEGMENTS];
5633 const int range = 63;
5634 int i;
5635
5636 // This method is currently incompatible with the cyclic refresh method
5637 if ( cpi->cyclic_refresh_mode_enabled )
5638 return -1;
5639
5640 // Check number of rows and columns match
5641 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
5642 return -1;
5643
5644 // Range check the delta Q values and convert the external Q range values
5645 // to internal ones.
5646 if ( (abs(delta_q[0]) > range) || (abs(delta_q[1]) > range) ||
5647 (abs(delta_q[2]) > range) || (abs(delta_q[3]) > range) )
5648 return -1;
5649
5650 // Range check the delta lf values
5651 if ( (abs(delta_lf[0]) > range) || (abs(delta_lf[1]) > range) ||
5652 (abs(delta_lf[2]) > range) || (abs(delta_lf[3]) > range) )
5653 return -1;
5654
5655 if (!map)
5656 {
5657 disable_segmentation(cpi);
5658 return 0;
5659 }
5660
5661 // Translate the external delta q values to internal values.
5662 for ( i = 0; i < MAX_MB_SEGMENTS; i++ )
5663 internal_delta_q[i] =
5664 ( delta_q[i] >= 0 ) ? q_trans[delta_q[i]] : -q_trans[-delta_q[i]];
5665
5666 /* Set the segmentation Map */
5667 set_segmentation_map(cpi, map);
5668
5669 /* Activate segmentation. */
5670 enable_segmentation(cpi);
5671
5672 /* Set up the quant segment data */
5673 feature_data[MB_LVL_ALT_Q][0] = internal_delta_q[0];
5674 feature_data[MB_LVL_ALT_Q][1] = internal_delta_q[1];
5675 feature_data[MB_LVL_ALT_Q][2] = internal_delta_q[2];
5676 feature_data[MB_LVL_ALT_Q][3] = internal_delta_q[3];
5677
5678 /* Set up the loop segment data s */
5679 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5680 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5681 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5682 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5683
5684 cpi->segment_encode_breakout[0] = threshold[0];
5685 cpi->segment_encode_breakout[1] = threshold[1];
5686 cpi->segment_encode_breakout[2] = threshold[2];
5687 cpi->segment_encode_breakout[3] = threshold[3];
5688
5689 /* Initialise the feature data structure */
5690 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5691
5692 return 0;
5693 }
5694
vp8_set_active_map(VP8_COMP * cpi,unsigned char * map,unsigned int rows,unsigned int cols)5695 int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols)
5696 {
5697 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
5698 {
5699 if (map)
5700 {
5701 vpx_memcpy(cpi->active_map, map, rows * cols);
5702 cpi->active_map_enabled = 1;
5703 }
5704 else
5705 cpi->active_map_enabled = 0;
5706
5707 return 0;
5708 }
5709 else
5710 {
5711 return -1 ;
5712 }
5713 }
5714
vp8_set_internal_size(VP8_COMP * cpi,VPX_SCALING horiz_mode,VPX_SCALING vert_mode)5715 int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
5716 {
5717 if (horiz_mode <= ONETWO)
5718 cpi->common.horiz_scale = horiz_mode;
5719 else
5720 return -1;
5721
5722 if (vert_mode <= ONETWO)
5723 cpi->common.vert_scale = vert_mode;
5724 else
5725 return -1;
5726
5727 return 0;
5728 }
5729
5730
5731
vp8_calc_ss_err(YV12_BUFFER_CONFIG * source,YV12_BUFFER_CONFIG * dest)5732 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest)
5733 {
5734 int i, j;
5735 int Total = 0;
5736
5737 unsigned char *src = source->y_buffer;
5738 unsigned char *dst = dest->y_buffer;
5739
5740 /* Loop through the Y plane raw and reconstruction data summing
5741 * (square differences)
5742 */
5743 for (i = 0; i < source->y_height; i += 16)
5744 {
5745 for (j = 0; j < source->y_width; j += 16)
5746 {
5747 unsigned int sse;
5748 Total += vp8_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
5749 }
5750
5751 src += 16 * source->y_stride;
5752 dst += 16 * dest->y_stride;
5753 }
5754
5755 return Total;
5756 }
5757
5758
vp8_get_quantizer(VP8_COMP * cpi)5759 int vp8_get_quantizer(VP8_COMP *cpi)
5760 {
5761 return cpi->common.base_qindex;
5762 }
5763