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