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