Coverage Report

Created: 2026-10-17 06:45

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