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