Coverage Report

Created: 2026-10-28 08:13

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
161k
static int rescale(int val, int num, int denom) {
271
161k
  int64_t llnum = num;
272
161k
  int64_t llden = denom;
273
161k
  int64_t llval = val;
274
275
161k
  int64_t result = (llval * llnum / llden);
276
161k
  if (result <= INT_MAX)
277
147k
    return (int)result;
278
14.0k
  else
279
14.0k
    return INT_MAX;
280
161k
}
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
72.9k
static void setup_features(VP8_COMP *cpi) {
387
  // If segmentation enabled set the update flags
388
72.9k
  if (cpi->mb.e_mbd.segmentation_enabled) {
389
0
    cpi->mb.e_mbd.update_mb_segmentation_map = 1;
390
0
    cpi->mb.e_mbd.update_mb_segmentation_data = 1;
391
72.9k
  } else {
392
72.9k
    cpi->mb.e_mbd.update_mb_segmentation_map = 0;
393
72.9k
    cpi->mb.e_mbd.update_mb_segmentation_data = 0;
394
72.9k
  }
395
396
72.9k
  cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
397
72.9k
  cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
398
72.9k
  memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
399
72.9k
  memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
400
72.9k
  memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0,
401
72.9k
         sizeof(cpi->mb.e_mbd.ref_lf_deltas));
402
72.9k
  memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0,
403
72.9k
         sizeof(cpi->mb.e_mbd.mode_lf_deltas));
404
405
72.9k
  set_default_lf_deltas(cpi);
406
72.9k
}
407
408
static void dealloc_raw_frame_buffers(VP8_COMP *cpi);
409
410
1
static void initialize_enc(void) {
411
1
  vpx_dsp_rtcd();
412
1
  vp8_init_intra_predictors();
413
1
}
414
415
6.04k
void vp8_initialize_enc(void) { once(initialize_enc); }
416
417
5.99k
static void dealloc_compressor_data(VP8_COMP *cpi) {
418
5.99k
  vpx_free(cpi->tplist);
419
5.99k
  cpi->tplist = NULL;
420
421
  /* Delete last frame MV storage buffers */
422
5.99k
  vpx_free(cpi->lfmv);
423
5.99k
  cpi->lfmv = 0;
424
425
5.99k
  vpx_free(cpi->lf_ref_frame_sign_bias);
426
5.99k
  cpi->lf_ref_frame_sign_bias = 0;
427
428
5.99k
  vpx_free(cpi->lf_ref_frame);
429
5.99k
  cpi->lf_ref_frame = 0;
430
431
  /* Delete sementation map */
432
5.99k
  vpx_free(cpi->segmentation_map);
433
5.99k
  cpi->segmentation_map = 0;
434
435
5.99k
  vpx_free(cpi->active_map);
436
5.99k
  cpi->active_map = 0;
437
438
5.99k
  vp8_de_alloc_frame_buffers(&cpi->common);
439
440
5.99k
  vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
441
5.99k
  vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
442
5.99k
  vp8_yv12_de_alloc_frame_buffer(&cpi->raw_source_frame);
443
5.99k
  dealloc_raw_frame_buffers(cpi);
444
445
5.99k
  vpx_free(cpi->tok);
446
5.99k
  cpi->tok = 0;
447
448
  /* Structure used to monitor GF usage */
449
5.99k
  vpx_free(cpi->gf_active_flags);
450
5.99k
  cpi->gf_active_flags = 0;
451
452
  /* Activity mask based per mb zbin adjustments */
453
5.99k
  vpx_free(cpi->mb_activity_map);
454
5.99k
  cpi->mb_activity_map = 0;
455
456
5.99k
  vpx_free(cpi->mb.pip);
457
5.99k
  cpi->mb.pip = 0;
458
5.99k
}
459
460
0
static void enable_segmentation(VP8_COMP *cpi) {
461
  /* Set the appropriate feature bit */
462
0
  cpi->mb.e_mbd.segmentation_enabled = 1;
463
0
  cpi->mb.e_mbd.update_mb_segmentation_map = 1;
464
0
  cpi->mb.e_mbd.update_mb_segmentation_data = 1;
465
0
}
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
0
                             unsigned char abs_delta) {
499
0
  cpi->mb.e_mbd.mb_segment_abs_delta = abs_delta;
500
0
  memcpy(cpi->segment_feature_data, feature_data,
501
0
         sizeof(cpi->segment_feature_data));
502
0
}
503
504
/* A simple function to cyclically refresh the background at a lower Q */
505
0
static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment) {
506
0
  unsigned char *seg_map = cpi->segmentation_map;
507
0
  signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
508
0
  int i;
509
0
  int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
510
0
  int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
511
512
0
  cpi->cyclic_refresh_q = Q / 2;
513
514
0
  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
0
  memset(cpi->segmentation_map, 0, mbs_in_frame);
538
539
0
  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
0
  cpi->mb.e_mbd.update_mb_segmentation_map = 1;
593
0
  cpi->mb.e_mbd.update_mb_segmentation_data = 1;
594
0
  enable_segmentation(cpi);
595
596
  /* Set up the quant segment data */
597
0
  feature_data[MB_LVL_ALT_Q][0] = 0;
598
0
  feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
599
0
  feature_data[MB_LVL_ALT_Q][2] = 0;
600
0
  feature_data[MB_LVL_ALT_Q][3] = 0;
601
602
  /* Set up the loop segment data */
603
0
  feature_data[MB_LVL_ALT_LF][0] = 0;
604
0
  feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
605
0
  feature_data[MB_LVL_ALT_LF][2] = 0;
606
0
  feature_data[MB_LVL_ALT_LF][3] = 0;
607
608
  /* Initialise the feature data structure */
609
0
  set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
610
0
}
611
612
110k
static void compute_skin_map(VP8_COMP *cpi) {
613
110k
  int mb_row, mb_col, num_bl;
614
110k
  VP8_COMMON *cm = &cpi->common;
615
110k
  const uint8_t *src_y = cpi->Source->y_buffer;
616
110k
  const uint8_t *src_u = cpi->Source->u_buffer;
617
110k
  const uint8_t *src_v = cpi->Source->v_buffer;
618
110k
  const int src_ystride = cpi->Source->y_stride;
619
110k
  const int src_uvstride = cpi->Source->uv_stride;
620
621
110k
  const SKIN_DETECTION_BLOCK_SIZE bsize =
622
110k
      (cm->Width * cm->Height <= 352 * 288) ? SKIN_8X8 : SKIN_16X16;
623
624
428k
  for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
625
318k
    num_bl = 0;
626
1.79M
    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
627
1.47M
      const int bl_index = mb_row * cm->mb_cols + mb_col;
628
1.47M
      cpi->skin_map[bl_index] =
629
1.47M
          vp8_compute_skin_block(src_y, src_u, src_v, src_ystride, src_uvstride,
630
1.47M
                                 bsize, cpi->consec_zero_last[bl_index], 0);
631
1.47M
      num_bl++;
632
1.47M
      src_y += 16;
633
1.47M
      src_u += 8;
634
1.47M
      src_v += 8;
635
1.47M
    }
636
318k
    src_y += (src_ystride << 4) - (num_bl << 4);
637
318k
    src_u += (src_uvstride << 3) - (num_bl << 3);
638
318k
    src_v += (src_uvstride << 3) - (num_bl << 3);
639
318k
  }
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
259k
  for (mb_row = 1; mb_row < cm->mb_rows - 1; mb_row++) {
644
641k
    for (mb_col = 1; mb_col < cm->mb_cols - 1; mb_col++) {
645
493k
      const int bl_index = mb_row * cm->mb_cols + mb_col;
646
493k
      int num_neighbor = 0;
647
493k
      int mi, mj;
648
493k
      int non_skin_threshold = 8;
649
650
1.97M
      for (mi = -1; mi <= 1; mi += 1) {
651
5.91M
        for (mj = -1; mj <= 1; mj += 1) {
652
4.43M
          int bl_neighbor_index = (mb_row + mi) * cm->mb_cols + mb_col + mj;
653
4.43M
          if (cpi->skin_map[bl_neighbor_index]) num_neighbor++;
654
4.43M
        }
655
1.47M
      }
656
657
493k
      if (cpi->skin_map[bl_index] && num_neighbor < 2)
658
897
        cpi->skin_map[bl_index] = 0;
659
493k
      if (!cpi->skin_map[bl_index] && num_neighbor == non_skin_threshold)
660
32
        cpi->skin_map[bl_index] = 1;
661
493k
    }
662
148k
  }
663
110k
}
664
665
72.9k
static void set_default_lf_deltas(VP8_COMP *cpi) {
666
72.9k
  cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
667
72.9k
  cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
668
669
72.9k
  memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
670
72.9k
  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
72.9k
  cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
674
72.9k
  cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
675
72.9k
  cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
676
72.9k
  cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
677
678
72.9k
  cpi->mb.e_mbd.mode_lf_deltas[0] = 4; /* BPRED */
679
680
72.9k
  if (cpi->oxcf.Mode == MODE_REALTIME) {
681
8.55k
    cpi->mb.e_mbd.mode_lf_deltas[1] = -12; /* Zero */
682
64.3k
  } else {
683
64.3k
    cpi->mb.e_mbd.mode_lf_deltas[1] = -2; /* Zero */
684
64.3k
  }
685
686
72.9k
  cpi->mb.e_mbd.mode_lf_deltas[2] = 2; /* New mv */
687
72.9k
  cpi->mb.e_mbd.mode_lf_deltas[3] = 4; /* Split mv */
688
72.9k
}
689
690
/* Convenience macros for mapping speed and mode into a continuous
691
 * range
692
 */
693
85.2k
#define GOOD(x) ((x) + 1)
694
57.3k
#define RT(x) ((x) + 7)
695
696
2.22M
static int speed_map(int speed, const int *map) {
697
2.22M
  int res;
698
699
5.94M
  do {
700
5.94M
    res = *map++;
701
5.94M
  } while (speed >= *map++);
702
2.22M
  return res;
703
2.22M
}
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
148k
void vp8_set_speed_features(VP8_COMP *cpi) {
774
148k
  SPEED_FEATURES *sf = &cpi->sf;
775
148k
  int Mode = cpi->compressor_speed;
776
148k
  int Speed = cpi->Speed;
777
148k
  int Speed2;
778
148k
  int i;
779
148k
  VP8_COMMON *cm = &cpi->common;
780
148k
  int last_improved_quant = sf->improved_quant;
781
148k
  int ref_frames;
782
783
  /* Initialise default mode frequency sampling variables */
784
3.11M
  for (i = 0; i < MAX_MODES; ++i) {
785
2.97M
    cpi->mode_check_freq[i] = 0;
786
2.97M
  }
787
788
148k
  cpi->mb.mbs_tested_so_far = 0;
789
148k
  cpi->mb.mbs_zero_last_dot_suppress = 0;
790
791
  /* best quality defaults */
792
148k
  sf->RD = 1;
793
148k
  sf->search_method = NSTEP;
794
148k
  sf->improved_quant = 1;
795
148k
  sf->improved_dct = 1;
796
148k
  sf->auto_filter = 1;
797
148k
  sf->recode_loop = 1;
798
148k
  sf->quarter_pixel_search = 1;
799
148k
  sf->half_pixel_search = 1;
800
148k
  sf->iterative_sub_pixel = 1;
801
148k
  sf->optimize_coefficients = 1;
802
148k
  sf->use_fastquant_for_pick = 0;
803
148k
  sf->no_skip_block4x4_search = 1;
804
805
148k
  sf->first_step = 0;
806
148k
  sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
807
148k
  sf->improved_mv_pred = 1;
808
809
  /* default thresholds to 0 */
810
3.11M
  for (i = 0; i < MAX_MODES; ++i) sf->thresh_mult[i] = 0;
811
812
  /* Count enabled references */
813
148k
  ref_frames = 1;
814
148k
  if (cpi->ref_frame_flags & VP8_LAST_FRAME) ref_frames++;
815
148k
  if (cpi->ref_frame_flags & VP8_GOLD_FRAME) ref_frames++;
816
148k
  if (cpi->ref_frame_flags & VP8_ALTR_FRAME) ref_frames++;
817
818
  /* Convert speed to continuous range, with clamping */
819
148k
  if (Mode == 0) {
820
5.99k
    Speed = 0;
821
142k
  } else if (Mode == 2) {
822
57.3k
    Speed = RT(Speed);
823
85.2k
  } else {
824
85.2k
    if (Speed > 5) Speed = 5;
825
85.2k
    Speed = GOOD(Speed);
826
85.2k
  }
827
828
148k
  sf->thresh_mult[THR_ZERO1] = sf->thresh_mult[THR_NEAREST1] =
829
148k
      sf->thresh_mult[THR_NEAR1] = sf->thresh_mult[THR_DC] = 0; /* always */
830
831
148k
  sf->thresh_mult[THR_ZERO2] = sf->thresh_mult[THR_ZERO3] =
832
148k
      sf->thresh_mult[THR_NEAREST2] = sf->thresh_mult[THR_NEAREST3] =
833
148k
          sf->thresh_mult[THR_NEAR2] = sf->thresh_mult[THR_NEAR3] =
834
148k
              speed_map(Speed, thresh_mult_map_znn);
835
836
148k
  sf->thresh_mult[THR_V_PRED] = sf->thresh_mult[THR_H_PRED] =
837
148k
      speed_map(Speed, thresh_mult_map_vhpred);
838
148k
  sf->thresh_mult[THR_B_PRED] = speed_map(Speed, thresh_mult_map_bpred);
839
148k
  sf->thresh_mult[THR_TM] = speed_map(Speed, thresh_mult_map_tm);
840
148k
  sf->thresh_mult[THR_NEW1] = speed_map(Speed, thresh_mult_map_new1);
841
148k
  sf->thresh_mult[THR_NEW2] = sf->thresh_mult[THR_NEW3] =
842
148k
      speed_map(Speed, thresh_mult_map_new2);
843
148k
  sf->thresh_mult[THR_SPLIT1] = speed_map(Speed, thresh_mult_map_split1);
844
148k
  sf->thresh_mult[THR_SPLIT2] = sf->thresh_mult[THR_SPLIT3] =
845
148k
      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
148k
  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
148k
  cpi->mode_check_freq[THR_ZERO1] = cpi->mode_check_freq[THR_NEAREST1] =
866
148k
      cpi->mode_check_freq[THR_NEAR1] = cpi->mode_check_freq[THR_TM] =
867
148k
          cpi->mode_check_freq[THR_DC] = 0; /* always */
868
869
148k
  cpi->mode_check_freq[THR_ZERO2] = cpi->mode_check_freq[THR_ZERO3] =
870
148k
      cpi->mode_check_freq[THR_NEAREST2] = cpi->mode_check_freq[THR_NEAREST3] =
871
148k
          speed_map(Speed, mode_check_freq_map_zn2);
872
873
148k
  cpi->mode_check_freq[THR_NEAR2] = cpi->mode_check_freq[THR_NEAR3] =
874
148k
      speed_map(Speed, mode_check_freq_map_near2);
875
876
148k
  cpi->mode_check_freq[THR_V_PRED] = cpi->mode_check_freq[THR_H_PRED] =
877
148k
      cpi->mode_check_freq[THR_B_PRED] =
878
148k
          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
148k
  Speed2 = Speed;
883
148k
  if (cpi->Speed == 10 && Mode == 2) Speed2 = RT(9);
884
148k
  cpi->mode_check_freq[THR_NEW1] = speed_map(Speed2, mode_check_freq_map_new1);
885
886
148k
  cpi->mode_check_freq[THR_NEW2] = cpi->mode_check_freq[THR_NEW3] =
887
148k
      speed_map(Speed, mode_check_freq_map_new2);
888
889
148k
  cpi->mode_check_freq[THR_SPLIT1] =
890
148k
      speed_map(Speed, mode_check_freq_map_split1);
891
148k
  cpi->mode_check_freq[THR_SPLIT2] = cpi->mode_check_freq[THR_SPLIT3] =
892
148k
      speed_map(Speed, mode_check_freq_map_split2);
893
148k
  Speed = cpi->Speed;
894
148k
  switch (Mode) {
895
0
#if !CONFIG_REALTIME_ONLY
896
5.99k
    case 0: /* best quality mode */
897
5.99k
      sf->first_step = 0;
898
5.99k
      sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
899
5.99k
      break;
900
85.2k
    case 1:
901
85.2k
    case 3:
902
85.2k
      if (Speed > 0) {
903
        /* Disable coefficient optimization above speed 0 */
904
85.2k
        sf->optimize_coefficients = 0;
905
85.2k
        sf->use_fastquant_for_pick = 1;
906
85.2k
        sf->no_skip_block4x4_search = 0;
907
908
85.2k
        sf->first_step = 1;
909
85.2k
      }
910
911
85.2k
      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
85.2k
      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
85.2k
      if (Speed > 4) {
928
0
        sf->auto_filter = 0; /* Faster selection of loop filter */
929
0
      }
930
931
85.2k
      break;
932
0
#endif
933
57.3k
    case 2:
934
57.3k
      sf->optimize_coefficients = 0;
935
57.3k
      sf->recode_loop = 0;
936
57.3k
      sf->auto_filter = 1;
937
57.3k
      sf->iterative_sub_pixel = 1;
938
57.3k
      sf->search_method = NSTEP;
939
940
57.3k
      if (Speed > 0) {
941
57.3k
        sf->improved_quant = 0;
942
57.3k
        sf->improved_dct = 0;
943
944
57.3k
        sf->use_fastquant_for_pick = 1;
945
57.3k
        sf->no_skip_block4x4_search = 0;
946
57.3k
        sf->first_step = 1;
947
57.3k
      }
948
949
57.3k
      if (Speed > 2) sf->auto_filter = 0; /* Faster selection of loop filter */
950
951
57.3k
      if (Speed > 3) {
952
57.3k
        sf->RD = 0;
953
57.3k
        sf->auto_filter = 1;
954
57.3k
      }
955
956
57.3k
      if (Speed > 4) {
957
10
        sf->auto_filter = 0; /* Faster selection of loop filter */
958
10
        sf->search_method = HEX;
959
10
        sf->iterative_sub_pixel = 0;
960
10
      }
961
962
57.3k
      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
57.3k
      if (Speed > 8) sf->quarter_pixel_search = 0;
1019
1020
57.3k
      if (cm->version == 0) {
1021
57.3k
        cm->filter_type = NORMAL_LOOPFILTER;
1022
1023
57.3k
        if (Speed >= 14) cm->filter_type = SIMPLE_LOOPFILTER;
1024
57.3k
      } else {
1025
0
        cm->filter_type = SIMPLE_LOOPFILTER;
1026
0
      }
1027
1028
      /* This has a big hit on quality. Last resort */
1029
57.3k
      if (Speed >= 15) sf->half_pixel_search = 0;
1030
1031
57.3k
      memset(cpi->mb.error_bins, 0, sizeof(cpi->mb.error_bins));
1032
1033
148k
  } /* switch */
1034
1035
  /* Slow quant, dct and trellis not worthwhile for first pass
1036
   * so make sure they are always turned off.
1037
   */
1038
148k
  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
148k
  if (cpi->sf.search_method == NSTEP) {
1045
148k
    vp8_init3smotion_compensation(&cpi->mb,
1046
148k
                                  cm->yv12_fb[cm->lst_fb_idx].y_stride);
1047
148k
  } 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
148k
  if (cpi->sf.improved_dct) {
1053
91.2k
    cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
1054
91.2k
    cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
1055
91.2k
  } else {
1056
    /* No fast FDCT defined for any platform at this time. */
1057
57.3k
    cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
1058
57.3k
    cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
1059
57.3k
  }
1060
1061
148k
  cpi->mb.short_walsh4x4 = vp8_short_walsh4x4;
1062
1063
148k
  if (cpi->sf.improved_quant) {
1064
91.2k
    cpi->mb.quantize_b = vp8_regular_quantize_b;
1065
91.2k
  } else {
1066
57.3k
    cpi->mb.quantize_b = vp8_fast_quantize_b;
1067
57.3k
  }
1068
148k
  if (cpi->sf.improved_quant != last_improved_quant) vp8cx_init_quantizer(cpi);
1069
1070
148k
  if (cpi->sf.iterative_sub_pixel == 1) {
1071
148k
    cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
1072
148k
  } else if (cpi->sf.quarter_pixel_search) {
1073
10
    cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
1074
10
  } 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
148k
  if (cpi->sf.optimize_coefficients == 1 && cpi->pass != 1) {
1081
5.99k
    cpi->mb.optimize = 1;
1082
142k
  } else {
1083
142k
    cpi->mb.optimize = 0;
1084
142k
  }
1085
1086
148k
  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
148k
}
1094
#undef GOOD
1095
#undef RT
1096
1097
5.99k
static void alloc_raw_frame_buffers(VP8_COMP *cpi) {
1098
5.99k
#if VP8_TEMPORAL_ALT_REF
1099
5.99k
  int width = (cpi->oxcf.Width + 15) & ~15;
1100
5.99k
  int height = (cpi->oxcf.Height + 15) & ~15;
1101
5.99k
#endif
1102
1103
5.99k
  cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
1104
5.99k
                                      cpi->oxcf.lag_in_frames);
1105
5.99k
  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
5.99k
#if VP8_TEMPORAL_ALT_REF
1111
1112
5.99k
  if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer, width, height,
1113
5.99k
                                  VP8BORDERINPIXELS)) {
1114
0
    vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1115
0
                       "Failed to allocate altref buffer");
1116
0
  }
1117
1118
5.99k
#endif
1119
5.99k
}
1120
1121
11.9k
static void dealloc_raw_frame_buffers(VP8_COMP *cpi) {
1122
11.9k
#if VP8_TEMPORAL_ALT_REF
1123
11.9k
  vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
1124
11.9k
#endif
1125
11.9k
  vp8_lookahead_destroy(cpi->lookahead);
1126
11.9k
#if CONFIG_TEMPORAL_DENOISING
1127
11.9k
  vp8_denoiser_free(&cpi->denoiser);
1128
11.9k
#endif
1129
11.9k
}
1130
1131
5.99k
static int vp8_alloc_partition_data(VP8_COMP *cpi) {
1132
5.99k
  vpx_free(cpi->mb.pip);
1133
1134
5.99k
  cpi->mb.pip =
1135
5.99k
      vpx_calloc((cpi->common.mb_cols + 1) * (cpi->common.mb_rows + 1),
1136
5.99k
                 sizeof(PARTITION_INFO));
1137
5.99k
  if (!cpi->mb.pip) return 1;
1138
1139
5.99k
  cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
1140
1141
5.99k
  return 0;
1142
5.99k
}
1143
1144
5.99k
void vp8_alloc_compressor_data(VP8_COMP *cpi) {
1145
5.99k
  VP8_COMMON *cm = &cpi->common;
1146
1147
5.99k
  int width = cm->Width;
1148
5.99k
  int height = cm->Height;
1149
1150
5.99k
  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
5.99k
  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
5.99k
  if ((width & 0xf) != 0) width += 16 - (width & 0xf);
1161
1162
5.99k
  if ((height & 0xf) != 0) height += 16 - (height & 0xf);
1163
1164
5.99k
  if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame, width, height,
1165
5.99k
                                  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
5.99k
  if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, width, height,
1171
5.99k
                                  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
5.99k
  vpx_free(cpi->tok);
1177
1178
5.99k
  {
1179
#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1180
    unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */
1181
#else
1182
5.99k
    unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
1183
5.99k
#endif
1184
5.99k
    CHECK_MEM_ERROR(&cpi->common.error, cpi->tok,
1185
5.99k
                    vpx_calloc(tokens, sizeof(*cpi->tok)));
1186
5.99k
  }
1187
1188
  /* Data used for real time vc mode to see if gf needs refreshing */
1189
5.99k
  cpi->zeromv_count = 0;
1190
1191
  /* Structures used to monitor GF usage */
1192
5.99k
  vpx_free(cpi->gf_active_flags);
1193
5.99k
  CHECK_MEM_ERROR(
1194
5.99k
      &cpi->common.error, cpi->gf_active_flags,
1195
5.99k
      vpx_calloc(sizeof(*cpi->gf_active_flags), cm->mb_rows * cm->mb_cols));
1196
5.99k
  cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
1197
1198
5.99k
  vpx_free(cpi->mb_activity_map);
1199
5.99k
  CHECK_MEM_ERROR(
1200
5.99k
      &cpi->common.error, cpi->mb_activity_map,
1201
5.99k
      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
5.99k
  vpx_free(cpi->lfmv);
1205
5.99k
  CHECK_MEM_ERROR(
1206
5.99k
      &cpi->common.error, cpi->lfmv,
1207
5.99k
      vpx_calloc((cm->mb_rows + 2) * (cm->mb_cols + 2), sizeof(*cpi->lfmv)));
1208
5.99k
  vpx_free(cpi->lf_ref_frame_sign_bias);
1209
5.99k
  CHECK_MEM_ERROR(&cpi->common.error, cpi->lf_ref_frame_sign_bias,
1210
5.99k
                  vpx_calloc((cm->mb_rows + 2) * (cm->mb_cols + 2),
1211
5.99k
                             sizeof(*cpi->lf_ref_frame_sign_bias)));
1212
5.99k
  vpx_free(cpi->lf_ref_frame);
1213
5.99k
  CHECK_MEM_ERROR(&cpi->common.error, cpi->lf_ref_frame,
1214
5.99k
                  vpx_calloc((cm->mb_rows + 2) * (cm->mb_cols + 2),
1215
5.99k
                             sizeof(*cpi->lf_ref_frame)));
1216
1217
  /* Create the encoder segmentation map and set all entries to 0 */
1218
5.99k
  vpx_free(cpi->segmentation_map);
1219
5.99k
  CHECK_MEM_ERROR(
1220
5.99k
      &cpi->common.error, cpi->segmentation_map,
1221
5.99k
      vpx_calloc(cm->mb_rows * cm->mb_cols, sizeof(*cpi->segmentation_map)));
1222
5.99k
  cpi->cyclic_refresh_mode_index = 0;
1223
5.99k
  vpx_free(cpi->active_map);
1224
5.99k
  CHECK_MEM_ERROR(
1225
5.99k
      &cpi->common.error, cpi->active_map,
1226
5.99k
      vpx_calloc(cm->mb_rows * cm->mb_cols, sizeof(*cpi->active_map)));
1227
5.99k
  memset(cpi->active_map, 1, (cm->mb_rows * cm->mb_cols));
1228
1229
5.99k
#if CONFIG_MULTITHREAD
1230
5.99k
  if (width < 640) {
1231
5.42k
    cpi->mt_sync_range = 1;
1232
5.42k
  } else if (width <= 1280) {
1233
248
    cpi->mt_sync_range = 4;
1234
325
  } else if (width <= 2560) {
1235
48
    cpi->mt_sync_range = 8;
1236
277
  } else {
1237
277
    cpi->mt_sync_range = 16;
1238
277
  }
1239
5.99k
#endif
1240
1241
5.99k
  vpx_free(cpi->tplist);
1242
5.99k
  CHECK_MEM_ERROR(&cpi->common.error, cpi->tplist,
1243
5.99k
                  vpx_malloc(sizeof(TOKENLIST) * cm->mb_rows));
1244
1245
5.99k
#if CONFIG_TEMPORAL_DENOISING
1246
5.99k
  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
5.99k
#endif
1255
5.99k
}
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
110k
int vp8_reverse_trans(int x) {
1266
110k
  int i;
1267
1268
3.32M
  for (i = 0; i < 64; ++i) {
1269
3.32M
    if (q_trans[i] >= x) return i;
1270
3.32M
  }
1271
1272
0
  return 63;
1273
110k
}
1274
1275
274k
static double clamp_framerate(double framerate) {
1276
274k
  if (framerate < .1)
1277
27.2k
    return 30.0;
1278
247k
  else
1279
247k
    return framerate;
1280
274k
}
1281
1282
164k
void vp8_new_framerate(VP8_COMP *cpi, double framerate) {
1283
164k
  framerate = clamp_framerate(framerate);
1284
164k
  cpi->framerate = framerate;
1285
164k
  cpi->output_framerate = framerate;
1286
164k
  const double per_frame_bandwidth =
1287
164k
      round(cpi->oxcf.target_bandwidth / cpi->output_framerate);
1288
164k
  cpi->per_frame_bandwidth = (int)VPXMIN(per_frame_bandwidth, INT_MAX);
1289
164k
  cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
1290
164k
  const int64_t vbr_min_bits = (int64_t)cpi->av_per_frame_bandwidth *
1291
164k
                               cpi->oxcf.two_pass_vbrmin_section / 100;
1292
164k
  cpi->min_frame_bandwidth = (int)VPXMIN(vbr_min_bits, INT_MAX);
1293
1294
  /* Set Maximum gf/arf interval */
1295
164k
  cpi->max_gf_interval = ((int)(cpi->output_framerate / 2.0) + 2);
1296
1297
164k
  if (cpi->max_gf_interval < 12) cpi->max_gf_interval = 12;
1298
1299
  /* Extended interval for genuinely static scenes */
1300
164k
  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
164k
  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
164k
  if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval) {
1315
31.6k
    cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1316
31.6k
  }
1317
164k
}
1318
1319
5.99k
static void init_config(VP8_COMP *cpi, const VP8_CONFIG *oxcf) {
1320
5.99k
  VP8_COMMON *cm = &cpi->common;
1321
1322
5.99k
  cpi->oxcf = *oxcf;
1323
1324
5.99k
  cpi->auto_gold = 1;
1325
5.99k
  cpi->auto_adjust_gold_quantizer = 1;
1326
1327
5.99k
  cm->version = oxcf->Version;
1328
5.99k
  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
5.99k
  if (oxcf->timebase.num > 0) {
1335
5.99k
    cpi->framerate =
1336
5.99k
        (double)(oxcf->timebase.den) / (double)(oxcf->timebase.num);
1337
5.99k
  } 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
5.99k
  if (cpi->framerate > 180) cpi->framerate = 30;
1345
1346
5.99k
  cpi->ref_framerate = cpi->framerate;
1347
1348
5.99k
  cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
1349
1350
5.99k
  cm->refresh_golden_frame = 0;
1351
5.99k
  cm->refresh_last_frame = 1;
1352
5.99k
  cm->refresh_entropy_probs = 1;
1353
1354
  /* change includes all joint functionality */
1355
5.99k
  vp8_change_config(cpi, oxcf);
1356
1357
  /* Initialize active best and worst q and average q values. */
1358
5.99k
  cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1359
5.99k
  cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1360
5.99k
  cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1361
1362
  /* Initialise the starting buffer levels */
1363
5.99k
  cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1364
5.99k
  cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1365
1366
5.99k
  cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1367
5.99k
  cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1368
5.99k
  cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1369
5.99k
  cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1370
1371
5.99k
  cpi->total_actual_bits = 0;
1372
5.99k
  cpi->total_target_vs_actual = 0;
1373
1374
  /* Temporal scalabilty */
1375
5.99k
  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
5.99k
#if VP8_TEMPORAL_ALT_REF
1387
5.99k
  {
1388
5.99k
    int i;
1389
1390
5.99k
    cpi->fixed_divide[0] = 0;
1391
1392
3.06M
    for (i = 1; i < 512; ++i) cpi->fixed_divide[i] = 0x80000 / i;
1393
5.99k
  }
1394
5.99k
#endif
1395
5.99k
}
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
53.8k
void vp8_change_config(VP8_COMP *cpi, const VP8_CONFIG *oxcf) {
1445
53.8k
  VP8_COMMON *cm = &cpi->common;
1446
53.8k
  int last_w, last_h;
1447
53.8k
  unsigned int prev_number_of_layers;
1448
53.8k
  double raw_target_rate;
1449
1450
53.8k
  if (!cpi) return;
1451
1452
53.8k
  if (!oxcf) return;
1453
1454
53.8k
  if (cm->version != oxcf->Version) {
1455
0
    cm->version = oxcf->Version;
1456
0
    vp8_setup_version(cm);
1457
0
  }
1458
1459
53.8k
  last_w = cpi->oxcf.Width;
1460
53.8k
  last_h = cpi->oxcf.Height;
1461
53.8k
  prev_number_of_layers = cpi->oxcf.number_of_layers;
1462
1463
53.8k
  cpi->oxcf = *oxcf;
1464
1465
53.8k
  switch (cpi->oxcf.Mode) {
1466
2.32k
    case MODE_REALTIME:
1467
2.32k
      cpi->pass = 0;
1468
2.32k
      cpi->compressor_speed = 2;
1469
1470
2.32k
      if (cpi->oxcf.cpu_used < -16) {
1471
0
        cpi->oxcf.cpu_used = -16;
1472
0
      }
1473
1474
2.32k
      if (cpi->oxcf.cpu_used > 16) cpi->oxcf.cpu_used = 16;
1475
1476
2.32k
      break;
1477
1478
3.49k
    case MODE_GOODQUALITY:
1479
3.49k
      cpi->pass = 0;
1480
3.49k
      cpi->compressor_speed = 1;
1481
1482
3.49k
      if (cpi->oxcf.cpu_used < -5) {
1483
0
        cpi->oxcf.cpu_used = -5;
1484
0
      }
1485
1486
3.49k
      if (cpi->oxcf.cpu_used > 5) cpi->oxcf.cpu_used = 5;
1487
1488
3.49k
      break;
1489
1490
47.9k
    case MODE_BESTQUALITY:
1491
47.9k
      cpi->pass = 0;
1492
47.9k
      cpi->compressor_speed = 0;
1493
47.9k
      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
53.8k
  }
1515
1516
53.8k
  if (cpi->pass == 0) cpi->auto_worst_q = 1;
1517
1518
53.8k
  cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1519
53.8k
  cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1520
53.8k
  cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1521
1522
53.8k
  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
53.8k
  cpi->ext_refresh_frame_flags_pending = 0;
1549
1550
53.8k
  cpi->baseline_gf_interval =
1551
53.8k
      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
53.8k
  if (!cpi->oxcf.error_resilient_mode &&
1555
53.8k
      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
53.8k
  if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3) {
1564
53.8k
    cm->multi_token_partition = (TOKEN_PARTITION)cpi->oxcf.token_partitions;
1565
53.8k
  }
1566
1567
53.8k
  setup_features(cpi);
1568
1569
53.8k
  if (!cpi->use_roi_static_threshold) {
1570
53.8k
    int i;
1571
269k
    for (i = 0; i < MAX_MB_SEGMENTS; ++i) {
1572
215k
      cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1573
215k
    }
1574
53.8k
  }
1575
1576
  /* At the moment the first order values may not be > MAXQ */
1577
53.8k
  if (cpi->oxcf.fixed_q > MAXQ) cpi->oxcf.fixed_q = MAXQ;
1578
1579
  /* local file playback mode == really big buffer */
1580
53.8k
  if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
1581
50.3k
    cpi->oxcf.starting_buffer_level = 60000;
1582
50.3k
    cpi->oxcf.optimal_buffer_level = 60000;
1583
50.3k
    cpi->oxcf.maximum_buffer_size = 240000;
1584
50.3k
    cpi->oxcf.starting_buffer_level_in_ms = 60000;
1585
50.3k
    cpi->oxcf.optimal_buffer_level_in_ms = 60000;
1586
50.3k
    cpi->oxcf.maximum_buffer_size_in_ms = 240000;
1587
50.3k
  }
1588
1589
53.8k
  raw_target_rate = ((int64_t)cpi->oxcf.Width * cpi->oxcf.Height * 8 * 3 *
1590
53.8k
                     cpi->framerate / 1000.0);
1591
53.8k
  if (cpi->oxcf.target_bandwidth > raw_target_rate)
1592
43.8k
    cpi->oxcf.target_bandwidth = (unsigned int)raw_target_rate;
1593
  /* Convert target bandwidth from Kbit/s to Bit/s */
1594
53.8k
  cpi->oxcf.target_bandwidth *= 1000;
1595
1596
53.8k
  cpi->oxcf.starting_buffer_level = rescale(
1597
53.8k
      (int)cpi->oxcf.starting_buffer_level, cpi->oxcf.target_bandwidth, 1000);
1598
1599
  /* Set or reset optimal and maximum buffer levels. */
1600
53.8k
  if (cpi->oxcf.optimal_buffer_level == 0) {
1601
0
    cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1602
53.8k
  } else {
1603
53.8k
    cpi->oxcf.optimal_buffer_level = rescale(
1604
53.8k
        (int)cpi->oxcf.optimal_buffer_level, cpi->oxcf.target_bandwidth, 1000);
1605
53.8k
  }
1606
1607
53.8k
  if (cpi->oxcf.maximum_buffer_size == 0) {
1608
0
    cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1609
53.8k
  } else {
1610
53.8k
    cpi->oxcf.maximum_buffer_size = rescale((int)cpi->oxcf.maximum_buffer_size,
1611
53.8k
                                            cpi->oxcf.target_bandwidth, 1000);
1612
53.8k
  }
1613
  // Under a configuration change, where maximum_buffer_size may change,
1614
  // keep buffer level clipped to the maximum allowed buffer size.
1615
53.8k
  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
53.8k
  vp8_new_framerate(cpi, cpi->framerate);
1622
1623
  /* Set absolute upper and lower quality limits */
1624
53.8k
  cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1625
53.8k
  cpi->best_quality = cpi->oxcf.best_allowed_q;
1626
1627
  /* active values should only be modified if out of new range */
1628
53.8k
  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
53.8k
  else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q) {
1633
5.99k
    cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
1634
5.99k
  }
1635
53.8k
  if (cpi->active_best_quality < cpi->oxcf.best_allowed_q) {
1636
5.99k
    cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1637
5.99k
  }
1638
  /* less likely */
1639
47.8k
  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
53.8k
  cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
1644
1645
53.8k
  cpi->cq_target_quality = cpi->oxcf.cq_level;
1646
1647
  /* Only allow dropped frames in buffered mode */
1648
53.8k
  cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
1649
1650
53.8k
  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
53.8k
  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
53.8k
  if (!cpi->initial_width) {
1667
5.99k
    cpi->initial_width = cpi->oxcf.Width;
1668
5.99k
    cpi->initial_height = cpi->oxcf.Height;
1669
5.99k
  }
1670
1671
53.8k
  cm->Width = cpi->oxcf.Width;
1672
53.8k
  cm->Height = cpi->oxcf.Height;
1673
53.8k
  assert(cm->Width <= cpi->initial_width);
1674
53.8k
  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
53.8k
  if (cpi->oxcf.Sharpness > 7) cpi->oxcf.Sharpness = 7;
1684
1685
53.8k
  cm->sharpness_level = cpi->oxcf.Sharpness;
1686
1687
53.8k
  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
53.8k
  if (last_w != cpi->oxcf.Width || last_h != cpi->oxcf.Height) {
1699
0
    cpi->force_next_frame_intra = 1;
1700
0
  }
1701
1702
53.8k
  if (((cm->Width + 15) & ~15) != cm->yv12_fb[cm->lst_fb_idx].y_width ||
1703
47.8k
      ((cm->Height + 15) & ~15) != cm->yv12_fb[cm->lst_fb_idx].y_height ||
1704
47.8k
      cm->yv12_fb[cm->lst_fb_idx].y_width == 0) {
1705
5.99k
    dealloc_raw_frame_buffers(cpi);
1706
5.99k
    alloc_raw_frame_buffers(cpi);
1707
5.99k
    vp8_alloc_compressor_data(cpi);
1708
5.99k
  }
1709
1710
53.8k
  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
53.8k
  cpi->Speed = cpi->oxcf.cpu_used;
1716
1717
  /* force to allowlag to 0 if lag_in_frames is 0; */
1718
53.8k
  if (cpi->oxcf.lag_in_frames == 0) {
1719
53.8k
    cpi->oxcf.allow_lag = 0;
1720
53.8k
  }
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
53.8k
  cpi->alt_ref_source = NULL;
1728
53.8k
  cpi->is_src_frame_alt_ref = 0;
1729
1730
53.8k
#if CONFIG_TEMPORAL_DENOISING
1731
53.8k
  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
53.8k
#endif
1743
1744
#if 0
1745
    /* Experimental RD Code */
1746
    cpi->frame_distortion = 0;
1747
    cpi->last_frame_distortion = 0;
1748
#endif
1749
53.8k
}
1750
1751
#ifndef M_LOG2_E
1752
1.52M
#define M_LOG2_E 0.693147180559945309417
1753
#endif
1754
1.52M
#define log2f(x) (log(x) / (float)M_LOG2_E)
1755
1756
5.99k
static void cal_mvsadcosts(int *mvsadcost[2]) {
1757
5.99k
  int i = 1;
1758
1759
5.99k
  mvsadcost[0][0] = 300;
1760
5.99k
  mvsadcost[1][0] = 300;
1761
1762
1.52M
  do {
1763
1.52M
    double z = 256 * (2 * (log2f(8 * i) + .6));
1764
1.52M
    mvsadcost[0][i] = (int)z;
1765
1.52M
    mvsadcost[1][i] = (int)z;
1766
1.52M
    mvsadcost[0][-i] = (int)z;
1767
1.52M
    mvsadcost[1][-i] = (int)z;
1768
1.52M
  } while (++i <= mvfp_max);
1769
5.99k
}
1770
1771
5.99k
struct VP8_COMP *vp8_create_compressor(const VP8_CONFIG *oxcf) {
1772
5.99k
  int i;
1773
1774
5.99k
  VP8_COMP *cpi;
1775
5.99k
  VP8_COMMON *cm;
1776
1777
5.99k
  cpi = vpx_memalign(32, sizeof(VP8_COMP));
1778
  /* Check that the CPI instance is valid */
1779
5.99k
  if (!cpi) return 0;
1780
1781
5.99k
  cm = &cpi->common;
1782
1783
5.99k
  memset(cpi, 0, sizeof(VP8_COMP));
1784
1785
5.99k
  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
5.99k
  cpi->common.error.setjmp = 1;
1792
1793
5.99k
  CHECK_MEM_ERROR(
1794
5.99k
      &cpi->common.error, cpi->mb.ss,
1795
5.99k
      vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
1796
1797
5.99k
  vp8_create_common(&cpi->common);
1798
1799
5.99k
  init_config(cpi, oxcf);
1800
1801
5.99k
  memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob,
1802
5.99k
         sizeof(vp8cx_base_skip_false_prob));
1803
5.99k
  cpi->common.current_video_frame = 0;
1804
5.99k
  cpi->temporal_pattern_counter = 0;
1805
5.99k
  cpi->temporal_layer_id = -1;
1806
5.99k
  cpi->kf_overspend_bits = 0;
1807
5.99k
  cpi->kf_bitrate_adjustment = 0;
1808
5.99k
  cpi->frames_till_gf_update_due = 0;
1809
5.99k
  cpi->gf_overspend_bits = 0;
1810
5.99k
  cpi->non_gf_bitrate_adjustment = 0;
1811
5.99k
  cpi->prob_last_coded = 128;
1812
5.99k
  cpi->prob_gf_coded = 128;
1813
5.99k
  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
5.99k
  cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
1819
5.99k
  cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
1820
5.99k
  cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
1821
5.99k
  cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
1822
1823
  /* Set reference frame sign bias for ALTREF frame to 1 (for now) */
1824
5.99k
  cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
1825
1826
5.99k
  cpi->twopass.gf_decay_rate = 0;
1827
5.99k
  cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1828
1829
5.99k
  cpi->gold_is_last = 0;
1830
5.99k
  cpi->alt_is_last = 0;
1831
5.99k
  cpi->gold_is_alt = 0;
1832
1833
5.99k
  cpi->active_map_enabled = 0;
1834
1835
5.99k
  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
5.99k
  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
5.99k
  cpi->cyclic_refresh_mode_enabled =
1867
5.99k
      (cpi->oxcf.error_resilient_mode ||
1868
5.99k
       (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
1869
0
        cpi->oxcf.Mode <= 2));
1870
5.99k
  cpi->cyclic_refresh_mode_max_mbs_perframe =
1871
5.99k
      (cpi->common.mb_rows * cpi->common.mb_cols) / 7;
1872
5.99k
  if (cpi->oxcf.number_of_layers == 1) {
1873
5.99k
    cpi->cyclic_refresh_mode_max_mbs_perframe =
1874
5.99k
        (cpi->common.mb_rows * cpi->common.mb_cols) / 20;
1875
5.99k
  } 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
5.99k
  cpi->cyclic_refresh_mode_index = 0;
1880
5.99k
  cpi->cyclic_refresh_q = 32;
1881
1882
  // GF behavior for 1 pass CBR, used when error_resilience is off.
1883
5.99k
  cpi->gf_update_onepass_cbr = 0;
1884
5.99k
  cpi->gf_noboost_onepass_cbr = 0;
1885
5.99k
  if (!cpi->oxcf.error_resilient_mode &&
1886
5.99k
      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
5.99k
  if (cpi->cyclic_refresh_mode_enabled) {
1899
0
    CHECK_MEM_ERROR(&cpi->common.error, cpi->cyclic_refresh_map,
1900
0
                    vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1901
5.99k
  } else {
1902
5.99k
    cpi->cyclic_refresh_map = (signed char *)NULL;
1903
5.99k
  }
1904
1905
5.99k
  CHECK_MEM_ERROR(
1906
5.99k
      &cpi->common.error, cpi->skin_map,
1907
5.99k
      vpx_calloc(cm->mb_rows * cm->mb_cols, sizeof(cpi->skin_map[0])));
1908
1909
5.99k
  CHECK_MEM_ERROR(&cpi->common.error, cpi->consec_zero_last,
1910
5.99k
                  vpx_calloc(cm->mb_rows * cm->mb_cols, 1));
1911
5.99k
  CHECK_MEM_ERROR(&cpi->common.error, cpi->consec_zero_last_mvbias,
1912
5.99k
                  vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
1913
1914
  /*Initialize the feed-forward activity masking.*/
1915
5.99k
  cpi->activity_avg = 90 << 12;
1916
1917
  /* Give a sensible default for the first frame. */
1918
5.99k
  cpi->frames_since_key = 8;
1919
5.99k
  cpi->key_frame_frequency = cpi->oxcf.key_freq;
1920
5.99k
  cpi->this_key_frame_forced = 0;
1921
5.99k
  cpi->next_key_frame_forced = 0;
1922
1923
5.99k
  cpi->source_alt_ref_pending = 0;
1924
5.99k
  cpi->source_alt_ref_active = 0;
1925
5.99k
  cpi->common.refresh_alt_ref_frame = 0;
1926
1927
5.99k
  cpi->force_maxqp = 0;
1928
5.99k
  cpi->frames_since_last_drop_overshoot = 0;
1929
5.99k
  cpi->rt_always_update_correction_factor = 0;
1930
5.99k
  cpi->rt_drop_recode_on_overshoot = 1;
1931
1932
5.99k
  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
5.99k
  cpi->first_time_stamp_ever = 0x7FFFFFFF;
1958
1959
5.99k
  cpi->frames_till_gf_update_due = 0;
1960
5.99k
  cpi->key_frame_count = 1;
1961
1962
5.99k
  cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1963
5.99k
  cpi->ni_tot_qi = 0;
1964
5.99k
  cpi->ni_frames = 0;
1965
5.99k
  cpi->total_byte_count = 0;
1966
1967
5.99k
  cpi->drop_frame = 0;
1968
1969
5.99k
  cpi->rate_correction_factor = 1.0;
1970
5.99k
  cpi->key_frame_rate_correction_factor = 1.0;
1971
5.99k
  cpi->gf_rate_correction_factor = 1.0;
1972
5.99k
  cpi->twopass.est_max_qcorrection_factor = 1.0;
1973
1974
35.9k
  for (i = 0; i < KEY_FRAME_CONTEXT; ++i) {
1975
29.9k
    cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate;
1976
29.9k
  }
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
5.99k
  cpi->output_pkt_list = oxcf->output_pkt_list;
1994
1995
5.99k
#if !CONFIG_REALTIME_ONLY
1996
1997
5.99k
  if (cpi->pass == 1) {
1998
0
    vp8_init_first_pass(cpi);
1999
5.99k
  } 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
5.99k
#endif
2011
2012
5.99k
  if (cpi->compressor_speed == 2) {
2013
0
    cpi->avg_encode_time = 0;
2014
0
    cpi->avg_pick_mode_time = 0;
2015
0
  }
2016
2017
5.99k
  vp8_set_speed_features(cpi);
2018
2019
  /* Set starting values of RD threshold multipliers (128 = *1) */
2020
125k
  for (i = 0; i < MAX_MODES; ++i) {
2021
119k
    cpi->mb.rd_thresh_mult[i] = 128;
2022
119k
  }
2023
2024
5.99k
#if CONFIG_MULTITHREAD
2025
5.99k
  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
5.99k
#endif
2031
2032
5.99k
  cpi->fn_ptr[BLOCK_16X16].sdf = vpx_sad16x16;
2033
5.99k
  cpi->fn_ptr[BLOCK_16X16].vf = vpx_variance16x16;
2034
5.99k
  cpi->fn_ptr[BLOCK_16X16].svf = vpx_sub_pixel_variance16x16;
2035
5.99k
  cpi->fn_ptr[BLOCK_16X16].sdx4df = vpx_sad16x16x4d;
2036
2037
5.99k
  cpi->fn_ptr[BLOCK_16X8].sdf = vpx_sad16x8;
2038
5.99k
  cpi->fn_ptr[BLOCK_16X8].vf = vpx_variance16x8;
2039
5.99k
  cpi->fn_ptr[BLOCK_16X8].svf = vpx_sub_pixel_variance16x8;
2040
5.99k
  cpi->fn_ptr[BLOCK_16X8].sdx4df = vpx_sad16x8x4d;
2041
2042
5.99k
  cpi->fn_ptr[BLOCK_8X16].sdf = vpx_sad8x16;
2043
5.99k
  cpi->fn_ptr[BLOCK_8X16].vf = vpx_variance8x16;
2044
5.99k
  cpi->fn_ptr[BLOCK_8X16].svf = vpx_sub_pixel_variance8x16;
2045
5.99k
  cpi->fn_ptr[BLOCK_8X16].sdx4df = vpx_sad8x16x4d;
2046
2047
5.99k
  cpi->fn_ptr[BLOCK_8X8].sdf = vpx_sad8x8;
2048
5.99k
  cpi->fn_ptr[BLOCK_8X8].vf = vpx_variance8x8;
2049
5.99k
  cpi->fn_ptr[BLOCK_8X8].svf = vpx_sub_pixel_variance8x8;
2050
5.99k
  cpi->fn_ptr[BLOCK_8X8].sdx4df = vpx_sad8x8x4d;
2051
2052
5.99k
  cpi->fn_ptr[BLOCK_4X4].sdf = vpx_sad4x4;
2053
5.99k
  cpi->fn_ptr[BLOCK_4X4].vf = vpx_variance4x4;
2054
5.99k
  cpi->fn_ptr[BLOCK_4X4].svf = vpx_sub_pixel_variance4x4;
2055
5.99k
  cpi->fn_ptr[BLOCK_4X4].sdx4df = vpx_sad4x4x4d;
2056
2057
5.99k
#if VPX_ARCH_X86 || VPX_ARCH_X86_64
2058
5.99k
  cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn;
2059
5.99k
  cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn;
2060
5.99k
  cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn;
2061
5.99k
  cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn;
2062
5.99k
  cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn;
2063
5.99k
#endif
2064
2065
5.99k
  cpi->diamond_search_sad = vp8_diamond_search_sad;
2066
5.99k
  cpi->refining_search_sad = vp8_refining_search_sad;
2067
2068
  /* make sure frame 1 is okay */
2069
5.99k
  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
5.99k
  vp8cx_init_quantizer(cpi);
2077
2078
5.99k
  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
5.99k
  cpi->mb.mvcost[0] = &cpi->rd_costs.mvcosts[0][mv_max + 1];
2090
5.99k
  cpi->mb.mvcost[1] = &cpi->rd_costs.mvcosts[1][mv_max + 1];
2091
5.99k
  cpi->mb.mvsadcost[0] = &cpi->rd_costs.mvsadcosts[0][mvfp_max + 1];
2092
5.99k
  cpi->mb.mvsadcost[1] = &cpi->rd_costs.mvsadcosts[1][mvfp_max + 1];
2093
2094
5.99k
  cal_mvsadcosts(cpi->mb.mvsadcost);
2095
2096
5.99k
  cpi->mb.mbmode_cost = cpi->rd_costs.mbmode_cost;
2097
5.99k
  cpi->mb.intra_uv_mode_cost = cpi->rd_costs.intra_uv_mode_cost;
2098
5.99k
  cpi->mb.bmode_costs = cpi->rd_costs.bmode_costs;
2099
5.99k
  cpi->mb.inter_bmode_costs = cpi->rd_costs.inter_bmode_costs;
2100
5.99k
  cpi->mb.token_costs = cpi->rd_costs.token_costs;
2101
2102
  /* setup block ptrs & offsets */
2103
5.99k
  vp8_setup_block_ptrs(&cpi->mb);
2104
5.99k
  vp8_setup_block_dptrs(&cpi->mb.e_mbd);
2105
2106
5.99k
  cpi->common.error.setjmp = 0;
2107
2108
5.99k
  return cpi;
2109
5.99k
}
2110
2111
6.04k
void vp8_remove_compressor(VP8_COMP **comp) {
2112
6.04k
  VP8_COMP *cpi = *comp;
2113
2114
6.04k
  if (!cpi) return;
2115
2116
5.99k
  if (cpi && (cpi->common.current_video_frame > 0)) {
2117
5.73k
#if !CONFIG_REALTIME_ONLY
2118
2119
5.73k
    if (cpi->pass == 2) {
2120
0
      vp8_end_second_pass(cpi);
2121
0
    }
2122
2123
5.73k
#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
5.73k
  }
2273
2274
5.99k
#if CONFIG_MULTITHREAD
2275
5.99k
  vp8cx_remove_encoder_threads(cpi);
2276
5.99k
#endif
2277
2278
5.99k
#if CONFIG_TEMPORAL_DENOISING
2279
5.99k
  vp8_denoiser_free(&cpi->denoiser);
2280
5.99k
#endif
2281
5.99k
  dealloc_compressor_data(cpi);
2282
5.99k
  vpx_free(cpi->mb.ss);
2283
5.99k
  vpx_free(cpi->tok);
2284
5.99k
  vpx_free(cpi->skin_map);
2285
5.99k
  vpx_free(cpi->cyclic_refresh_map);
2286
5.99k
  vpx_free(cpi->consec_zero_last);
2287
5.99k
  vpx_free(cpi->consec_zero_last_mvbias);
2288
2289
5.99k
  vp8_remove_common(&cpi->common);
2290
5.99k
  vpx_free(cpi);
2291
5.99k
  *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
5.99k
}
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
110k
static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi) {
2493
110k
  VP8_COMMON *cm = &cpi->common;
2494
2495
  /* are we resizing the image */
2496
110k
  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
110k
  } else {
2517
110k
    cpi->Source = sd;
2518
110k
  }
2519
110k
}
2520
2521
31.9k
static int resize_key_frame(VP8_COMP *cpi) {
2522
31.9k
#if CONFIG_SPATIAL_RESAMPLING
2523
31.9k
  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
31.9k
  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
31.9k
#endif
2572
31.9k
  return 0;
2573
31.9k
}
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
110k
static void update_golden_frame_stats(VP8_COMP *cpi) {
2609
110k
  VP8_COMMON *cm = &cpi->common;
2610
2611
  /* Update the Golden frame usage counts. */
2612
110k
  if (cm->refresh_golden_frame) {
2613
    /* Select an interval before next GF */
2614
27.4k
    if (!cpi->auto_gold) cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
2615
2616
27.4k
    if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0)) {
2617
27.4k
      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
27.4k
      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
8.29k
        cpi->gf_overspend_bits +=
2631
8.29k
            (cpi->projected_frame_size - cpi->inter_frame_target);
2632
8.29k
        cpi->gf_overspend_bits = VPXMAX(0, cpi->gf_overspend_bits);
2633
8.29k
      }
2634
2635
27.4k
      cpi->non_gf_bitrate_adjustment =
2636
27.4k
          cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
2637
27.4k
    }
2638
2639
    /* Update data structure that monitors level of reference to last GF */
2640
27.4k
    memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
2641
27.4k
    cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
2642
2643
    /* this frame refreshes means next frames don't unless specified by
2644
     * user
2645
     */
2646
27.4k
    cm->refresh_golden_frame = 0;
2647
27.4k
    cpi->frames_since_golden = 0;
2648
2649
27.4k
    cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
2650
27.4k
    cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
2651
27.4k
    cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
2652
27.4k
    cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
2653
2654
    /* ******** Fixed Q test code only ************ */
2655
    /* If we are going to use the ALT reference for the next group of
2656
     * frames set a flag to say so.
2657
     */
2658
27.4k
    if (cpi->oxcf.fixed_q >= 0 && cpi->oxcf.play_alternate &&
2659
0
        !cpi->common.refresh_alt_ref_frame) {
2660
0
      cpi->source_alt_ref_pending = 1;
2661
0
      cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2662
0
    }
2663
2664
27.4k
    if (!cpi->source_alt_ref_pending) cpi->source_alt_ref_active = 0;
2665
2666
    /* Decrement count down till next gf */
2667
27.4k
    if (cpi->frames_till_gf_update_due > 0) cpi->frames_till_gf_update_due--;
2668
2669
83.4k
  } else if (!cpi->common.refresh_alt_ref_frame) {
2670
    /* Decrement count down till next gf */
2671
83.4k
    if (cpi->frames_till_gf_update_due > 0) cpi->frames_till_gf_update_due--;
2672
2673
83.4k
    if (cpi->frames_till_alt_ref_frame) cpi->frames_till_alt_ref_frame--;
2674
2675
83.4k
    cpi->frames_since_golden++;
2676
2677
83.4k
    if (cpi->frames_since_golden > 1) {
2678
67.0k
      cpi->recent_ref_frame_usage[INTRA_FRAME] +=
2679
67.0k
          cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME];
2680
67.0k
      cpi->recent_ref_frame_usage[LAST_FRAME] +=
2681
67.0k
          cpi->mb.count_mb_ref_frame_usage[LAST_FRAME];
2682
67.0k
      cpi->recent_ref_frame_usage[GOLDEN_FRAME] +=
2683
67.0k
          cpi->mb.count_mb_ref_frame_usage[GOLDEN_FRAME];
2684
67.0k
      cpi->recent_ref_frame_usage[ALTREF_FRAME] +=
2685
67.0k
          cpi->mb.count_mb_ref_frame_usage[ALTREF_FRAME];
2686
67.0k
    }
2687
83.4k
  }
2688
110k
}
2689
2690
/* This function updates the reference frame probability estimates that
2691
 * will be used during mode selection
2692
 */
2693
110k
static void update_rd_ref_frame_probs(VP8_COMP *cpi) {
2694
110k
  VP8_COMMON *cm = &cpi->common;
2695
2696
110k
  const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
2697
110k
  const int rf_intra = rfct[INTRA_FRAME];
2698
110k
  const int rf_inter =
2699
110k
      rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
2700
2701
110k
  if (cm->frame_type == KEY_FRAME) {
2702
15.9k
    cpi->prob_intra_coded = 255;
2703
15.9k
    cpi->prob_last_coded = 128;
2704
15.9k
    cpi->prob_gf_coded = 128;
2705
94.8k
  } else if (!(rf_intra + rf_inter)) {
2706
8.56k
    cpi->prob_intra_coded = 63;
2707
8.56k
    cpi->prob_last_coded = 128;
2708
8.56k
    cpi->prob_gf_coded = 128;
2709
8.56k
  }
2710
2711
  /* update reference frame costs since we can do better than what we got
2712
   * last frame.
2713
   */
2714
110k
  if (cpi->oxcf.number_of_layers == 1) {
2715
110k
    if (cpi->common.refresh_alt_ref_frame) {
2716
0
      cpi->prob_intra_coded += 40;
2717
0
      if (cpi->prob_intra_coded > 255) cpi->prob_intra_coded = 255;
2718
0
      cpi->prob_last_coded = 200;
2719
0
      cpi->prob_gf_coded = 1;
2720
110k
    } else if (cpi->frames_since_golden == 0) {
2721
31.3k
      cpi->prob_last_coded = 214;
2722
79.5k
    } else if (cpi->frames_since_golden == 1) {
2723
14.3k
      cpi->prob_last_coded = 192;
2724
14.3k
      cpi->prob_gf_coded = 220;
2725
65.1k
    } else if (cpi->source_alt_ref_active) {
2726
0
      cpi->prob_gf_coded -= 20;
2727
2728
0
      if (cpi->prob_gf_coded < 10) cpi->prob_gf_coded = 10;
2729
0
    }
2730
110k
    if (!cpi->source_alt_ref_active) cpi->prob_gf_coded = 255;
2731
110k
  }
2732
110k
}
2733
2734
#if !CONFIG_REALTIME_ONLY
2735
/* 1 = key, 0 = inter */
2736
59.4k
static int decide_key_frame(VP8_COMP *cpi) {
2737
59.4k
  VP8_COMMON *cm = &cpi->common;
2738
2739
59.4k
  int code_key_frame = 0;
2740
2741
59.4k
  cpi->kf_boost = 0;
2742
2743
59.4k
  if (cpi->Speed > 11) return 0;
2744
2745
  /* Clear down mmx registers */
2746
59.4k
  vpx_clear_system_state();
2747
2748
59.4k
  if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0)) {
2749
0
    double change = 1.0 *
2750
0
                    abs((int)(cpi->mb.intra_error - cpi->last_intra_error)) /
2751
0
                    (1 + cpi->last_intra_error);
2752
0
    double change2 =
2753
0
        1.0 *
2754
0
        abs((int)(cpi->mb.prediction_error - cpi->last_prediction_error)) /
2755
0
        (1 + cpi->last_prediction_error);
2756
0
    double minerror = cm->MBs * 256;
2757
2758
0
    cpi->last_intra_error = cpi->mb.intra_error;
2759
0
    cpi->last_prediction_error = cpi->mb.prediction_error;
2760
2761
0
    if (10 * cpi->mb.intra_error / (1 + cpi->mb.prediction_error) < 15 &&
2762
0
        cpi->mb.prediction_error > minerror &&
2763
0
        (change > .25 || change2 > .25)) {
2764
      /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra >
2765
       * cpi->last_frame_percent_intra + 3*/
2766
0
      return 1;
2767
0
    }
2768
2769
0
    return 0;
2770
0
  }
2771
2772
  /* If the following are true we might as well code a key frame */
2773
59.4k
  if (((cpi->this_frame_percent_intra == 100) &&
2774
10.9k
       (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
2775
56.9k
      ((cpi->this_frame_percent_intra > 95) &&
2776
8.70k
       (cpi->this_frame_percent_intra >=
2777
8.70k
        (cpi->last_frame_percent_intra + 5)))) {
2778
2.58k
    code_key_frame = 1;
2779
2.58k
  }
2780
  /* in addition if the following are true and this is not a golden frame
2781
   * then code a key frame Note that on golden frames there often seems
2782
   * to be a pop in intra usage anyway hence this restriction is
2783
   * designed to prevent spurious key frames. The Intra pop needs to be
2784
   * investigated.
2785
   */
2786
56.8k
  else if (((cpi->this_frame_percent_intra > 60) &&
2787
15.7k
            (cpi->this_frame_percent_intra >
2788
15.7k
             (cpi->last_frame_percent_intra * 2))) ||
2789
56.2k
           ((cpi->this_frame_percent_intra > 75) &&
2790
10.2k
            (cpi->this_frame_percent_intra >
2791
10.2k
             (cpi->last_frame_percent_intra * 3 / 2))) ||
2792
56.1k
           ((cpi->this_frame_percent_intra > 90) &&
2793
9.30k
            (cpi->this_frame_percent_intra >
2794
9.30k
             (cpi->last_frame_percent_intra + 10)))) {
2795
831
    if (!cm->refresh_golden_frame) code_key_frame = 1;
2796
831
  }
2797
2798
59.4k
  return code_key_frame;
2799
59.4k
}
2800
2801
0
static void Pass1Encode(VP8_COMP *cpi) {
2802
0
  vp8_set_quantizer(cpi, 26);
2803
0
  vp8_first_pass(cpi);
2804
0
}
2805
#endif
2806
2807
#if 0
2808
void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
2809
{
2810
2811
    /* write the frame */
2812
    FILE *yframe;
2813
    int i;
2814
    char filename[255];
2815
2816
    sprintf(filename, "cx\\y%04d.raw", this_frame);
2817
    yframe = fopen(filename, "wb");
2818
2819
    for (i = 0; i < frame->y_height; ++i)
2820
        fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
2821
2822
    fclose(yframe);
2823
    sprintf(filename, "cx\\u%04d.raw", this_frame);
2824
    yframe = fopen(filename, "wb");
2825
2826
    for (i = 0; i < frame->uv_height; ++i)
2827
        fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2828
2829
    fclose(yframe);
2830
    sprintf(filename, "cx\\v%04d.raw", this_frame);
2831
    yframe = fopen(filename, "wb");
2832
2833
    for (i = 0; i < frame->uv_height; ++i)
2834
        fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
2835
2836
    fclose(yframe);
2837
}
2838
#endif
2839
2840
#if !CONFIG_REALTIME_ONLY
2841
/* Function to test for conditions that indeicate we should loop
2842
 * back and recode a frame.
2843
 */
2844
static int recode_loop_test(VP8_COMP *cpi, int high_limit, int low_limit, int q,
2845
139k
                            int maxq, int minq) {
2846
139k
  int force_recode = 0;
2847
139k
  VP8_COMMON *cm = &cpi->common;
2848
2849
  /* Is frame recode allowed at all
2850
   * Yes if either recode mode 1 is selected or mode two is selcted
2851
   * and the frame is a key frame. golden frame or alt_ref_frame
2852
   */
2853
139k
  if ((cpi->sf.recode_loop == 1) ||
2854
57.3k
      ((cpi->sf.recode_loop == 2) &&
2855
0
       ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame ||
2856
82.0k
        cm->refresh_alt_ref_frame))) {
2857
    /* General over and under shoot tests */
2858
82.0k
    if (((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
2859
64.9k
        ((cpi->projected_frame_size < low_limit) && (q > minq))) {
2860
26.8k
      force_recode = 1;
2861
26.8k
    }
2862
    /* Special Constrained quality tests */
2863
55.1k
    else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2864
      /* Undershoot and below auto cq level */
2865
15.0k
      if ((q > cpi->cq_target_quality) &&
2866
8.45k
          (cpi->projected_frame_size < ((cpi->this_frame_target * 7) >> 3))) {
2867
3.90k
        force_recode = 1;
2868
3.90k
      }
2869
      /* Severe undershoot and between auto and user cq level */
2870
11.1k
      else if ((q > cpi->oxcf.cq_level) &&
2871
4.55k
               (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
2872
0
               (cpi->active_best_quality > cpi->oxcf.cq_level)) {
2873
0
        force_recode = 1;
2874
0
        cpi->active_best_quality = cpi->oxcf.cq_level;
2875
0
      }
2876
15.0k
    }
2877
82.0k
  }
2878
2879
139k
  return force_recode;
2880
139k
}
2881
#endif  // !CONFIG_REALTIME_ONLY
2882
2883
110k
static void update_reference_frames(VP8_COMP *cpi) {
2884
110k
  VP8_COMMON *cm = &cpi->common;
2885
110k
  YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
2886
2887
  /* At this point the new frame has been encoded.
2888
   * If any buffer copy / swapping is signaled it should be done here.
2889
   */
2890
2891
110k
  if (cm->frame_type == KEY_FRAME) {
2892
19.1k
    yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME | VP8_ALTR_FRAME;
2893
2894
19.1k
    yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2895
19.1k
    yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2896
2897
19.1k
    cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
2898
2899
19.1k
    cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
2900
19.1k
    cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
2901
91.7k
  } else {
2902
91.7k
    if (cm->refresh_alt_ref_frame) {
2903
0
      assert(!cm->copy_buffer_to_arf);
2904
2905
0
      cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALTR_FRAME;
2906
0
      cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2907
0
      cm->alt_fb_idx = cm->new_fb_idx;
2908
2909
0
      cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
2910
91.7k
    } else if (cm->copy_buffer_to_arf) {
2911
8.29k
      assert(!(cm->copy_buffer_to_arf & ~0x3));
2912
2913
8.29k
      if (cm->copy_buffer_to_arf == 1) {
2914
0
        if (cm->alt_fb_idx != cm->lst_fb_idx) {
2915
0
          yv12_fb[cm->lst_fb_idx].flags |= VP8_ALTR_FRAME;
2916
0
          yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2917
0
          cm->alt_fb_idx = cm->lst_fb_idx;
2918
2919
0
          cpi->current_ref_frames[ALTREF_FRAME] =
2920
0
              cpi->current_ref_frames[LAST_FRAME];
2921
0
        }
2922
8.29k
      } else {
2923
8.29k
        if (cm->alt_fb_idx != cm->gld_fb_idx) {
2924
6.51k
          yv12_fb[cm->gld_fb_idx].flags |= VP8_ALTR_FRAME;
2925
6.51k
          yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
2926
6.51k
          cm->alt_fb_idx = cm->gld_fb_idx;
2927
2928
6.51k
          cpi->current_ref_frames[ALTREF_FRAME] =
2929
6.51k
              cpi->current_ref_frames[GOLDEN_FRAME];
2930
6.51k
        }
2931
8.29k
      }
2932
8.29k
    }
2933
2934
91.7k
    if (cm->refresh_golden_frame) {
2935
8.29k
      assert(!cm->copy_buffer_to_gf);
2936
2937
8.29k
      cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME;
2938
8.29k
      cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2939
8.29k
      cm->gld_fb_idx = cm->new_fb_idx;
2940
2941
8.29k
      cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
2942
83.4k
    } else if (cm->copy_buffer_to_gf) {
2943
0
      assert(!(cm->copy_buffer_to_arf & ~0x3));
2944
2945
0
      if (cm->copy_buffer_to_gf == 1) {
2946
0
        if (cm->gld_fb_idx != cm->lst_fb_idx) {
2947
0
          yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FRAME;
2948
0
          yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2949
0
          cm->gld_fb_idx = cm->lst_fb_idx;
2950
2951
0
          cpi->current_ref_frames[GOLDEN_FRAME] =
2952
0
              cpi->current_ref_frames[LAST_FRAME];
2953
0
        }
2954
0
      } else {
2955
0
        if (cm->alt_fb_idx != cm->gld_fb_idx) {
2956
0
          yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FRAME;
2957
0
          yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
2958
0
          cm->gld_fb_idx = cm->alt_fb_idx;
2959
2960
0
          cpi->current_ref_frames[GOLDEN_FRAME] =
2961
0
              cpi->current_ref_frames[ALTREF_FRAME];
2962
0
        }
2963
0
      }
2964
0
    }
2965
91.7k
  }
2966
2967
110k
  if (cm->refresh_last_frame) {
2968
110k
    cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FRAME;
2969
110k
    cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FRAME;
2970
110k
    cm->lst_fb_idx = cm->new_fb_idx;
2971
2972
110k
    cpi->current_ref_frames[LAST_FRAME] = cm->current_video_frame;
2973
110k
  }
2974
2975
110k
#if CONFIG_TEMPORAL_DENOISING
2976
110k
  if (cpi->oxcf.noise_sensitivity) {
2977
    /* we shouldn't have to keep multiple copies as we know in advance which
2978
     * buffer we should start - for now to get something up and running
2979
     * I've chosen to copy the buffers
2980
     */
2981
0
    if (cm->frame_type == KEY_FRAME) {
2982
0
      int i;
2983
0
      for (i = LAST_FRAME; i < MAX_REF_FRAMES; ++i)
2984
0
        vp8_yv12_copy_frame(cpi->Source, &cpi->denoiser.yv12_running_avg[i]);
2985
0
    } else {
2986
0
      vp8_yv12_extend_frame_borders(
2987
0
          &cpi->denoiser.yv12_running_avg[INTRA_FRAME]);
2988
2989
0
      if (cm->refresh_alt_ref_frame || cm->copy_buffer_to_arf) {
2990
0
        vp8_yv12_copy_frame(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
2991
0
                            &cpi->denoiser.yv12_running_avg[ALTREF_FRAME]);
2992
0
      }
2993
0
      if (cm->refresh_golden_frame || cm->copy_buffer_to_gf) {
2994
0
        vp8_yv12_copy_frame(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
2995
0
                            &cpi->denoiser.yv12_running_avg[GOLDEN_FRAME]);
2996
0
      }
2997
0
      if (cm->refresh_last_frame) {
2998
0
        if (cpi->denoiser.denoiser_mode == kDenoiserOnYOnly) {
2999
0
          vp8_yv12_copy_frame(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3000
0
                              &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
3001
0
        } else {
3002
0
          YV12_BUFFER_CONFIG tmp = cpi->denoiser.yv12_running_avg[INTRA_FRAME];
3003
0
          cpi->denoiser.yv12_running_avg[INTRA_FRAME] =
3004
0
              cpi->denoiser.yv12_running_avg[LAST_FRAME];
3005
0
          cpi->denoiser.yv12_running_avg[LAST_FRAME] = tmp;
3006
0
        }
3007
0
      }
3008
0
    }
3009
0
    if (cpi->oxcf.noise_sensitivity == 4)
3010
0
      vp8_yv12_copy_frame(cpi->Source, &cpi->denoiser.yv12_last_source);
3011
0
  }
3012
110k
#endif
3013
110k
}
3014
3015
static int measure_square_diff_partial(YV12_BUFFER_CONFIG *source,
3016
                                       YV12_BUFFER_CONFIG *dest,
3017
0
                                       VP8_COMP *cpi) {
3018
0
  int i, j;
3019
0
  int Total = 0;
3020
0
  int num_blocks = 0;
3021
0
  int skip = 2;
3022
0
  int min_consec_zero_last = 10;
3023
0
  int tot_num_blocks = (source->y_height * source->y_width) >> 8;
3024
0
  unsigned char *src = source->y_buffer;
3025
0
  unsigned char *dst = dest->y_buffer;
3026
3027
  /* Loop through the Y plane, every |skip| blocks along rows and colmumns,
3028
   * summing the square differences, and only for blocks that have been
3029
   * zero_last mode at least |x| frames in a row.
3030
   */
3031
0
  for (i = 0; i < source->y_height; i += 16 * skip) {
3032
0
    int block_index_row = (i >> 4) * cpi->common.mb_cols;
3033
0
    for (j = 0; j < source->y_width; j += 16 * skip) {
3034
0
      int index = block_index_row + (j >> 4);
3035
0
      if (cpi->consec_zero_last[index] >= min_consec_zero_last) {
3036
0
        unsigned int sse;
3037
0
        Total += vpx_mse16x16(src + j, source->y_stride, dst + j,
3038
0
                              dest->y_stride, &sse);
3039
0
        num_blocks++;
3040
0
      }
3041
0
    }
3042
0
    src += 16 * skip * source->y_stride;
3043
0
    dst += 16 * skip * dest->y_stride;
3044
0
  }
3045
  // Only return non-zero if we have at least ~1/16 samples for estimate.
3046
0
  if (num_blocks > (tot_num_blocks >> 4)) {
3047
0
    assert(num_blocks != 0);
3048
0
    return (Total / num_blocks);
3049
0
  } else {
3050
0
    return 0;
3051
0
  }
3052
0
}
3053
3054
#if CONFIG_TEMPORAL_DENOISING
3055
0
static void process_denoiser_mode_change(VP8_COMP *cpi) {
3056
0
  const VP8_COMMON *const cm = &cpi->common;
3057
0
  int i, j;
3058
0
  int total = 0;
3059
0
  int num_blocks = 0;
3060
  // Number of blocks skipped along row/column in computing the
3061
  // nmse (normalized mean square error) of source.
3062
0
  int skip = 2;
3063
  // Only select blocks for computing nmse that have been encoded
3064
  // as ZERO LAST min_consec_zero_last frames in a row.
3065
  // Scale with number of temporal layers.
3066
0
  int min_consec_zero_last = 12 / cpi->oxcf.number_of_layers;
3067
  // Decision is tested for changing the denoising mode every
3068
  // num_mode_change times this function is called. Note that this
3069
  // function called every 8 frames, so (8 * num_mode_change) is number
3070
  // of frames where denoising mode change is tested for switch.
3071
0
  int num_mode_change = 20;
3072
  // Framerate factor, to compensate for larger mse at lower framerates.
3073
  // Use ref_framerate, which is full source framerate for temporal layers.
3074
  // TODO(marpan): Adjust this factor.
3075
0
  int fac_framerate = cpi->ref_framerate < 25.0f ? 80 : 100;
3076
0
  int tot_num_blocks = cm->mb_rows * cm->mb_cols;
3077
0
  int ystride = cpi->Source->y_stride;
3078
0
  unsigned char *src = cpi->Source->y_buffer;
3079
0
  unsigned char *dst = cpi->denoiser.yv12_last_source.y_buffer;
3080
0
  static const unsigned char const_source[16] = { 128, 128, 128, 128, 128, 128,
3081
0
                                                  128, 128, 128, 128, 128, 128,
3082
0
                                                  128, 128, 128, 128 };
3083
0
  int bandwidth = (int)(cpi->target_bandwidth);
3084
  // For temporal layers, use full bandwidth (top layer).
3085
0
  if (cpi->oxcf.number_of_layers > 1) {
3086
0
    LAYER_CONTEXT *lc = &cpi->layer_context[cpi->oxcf.number_of_layers - 1];
3087
0
    bandwidth = (int)(lc->target_bandwidth);
3088
0
  }
3089
  // Loop through the Y plane, every skip blocks along rows and columns,
3090
  // summing the normalized mean square error, only for blocks that have
3091
  // been encoded as ZEROMV LAST at least min_consec_zero_last least frames in
3092
  // a row and have small sum difference between current and previous frame.
3093
  // Normalization here is by the contrast of the current frame block.
3094
0
  for (i = 0; i < cm->Height; i += 16 * skip) {
3095
0
    int block_index_row = (i >> 4) * cm->mb_cols;
3096
0
    for (j = 0; j < cm->Width; j += 16 * skip) {
3097
0
      int index = block_index_row + (j >> 4);
3098
0
      if (cpi->consec_zero_last[index] >= min_consec_zero_last) {
3099
0
        unsigned int sse;
3100
0
        const unsigned int var =
3101
0
            vpx_variance16x16(src + j, ystride, dst + j, ystride, &sse);
3102
        // Only consider this block as valid for noise measurement
3103
        // if the sum_diff average of the current and previous frame
3104
        // is small (to avoid effects from lighting change).
3105
0
        if ((sse - var) < 128) {
3106
0
          unsigned int sse2;
3107
0
          const unsigned int act =
3108
0
              vpx_variance16x16(src + j, ystride, const_source, 0, &sse2);
3109
0
          if (act > 0) total += sse / act;
3110
0
          num_blocks++;
3111
0
        }
3112
0
      }
3113
0
    }
3114
0
    src += 16 * skip * ystride;
3115
0
    dst += 16 * skip * ystride;
3116
0
  }
3117
0
  total = total * fac_framerate / 100;
3118
3119
  // Only consider this frame as valid sample if we have computed nmse over
3120
  // at least ~1/16 blocks, and Total > 0 (Total == 0 can happen if the
3121
  // application inputs duplicate frames, or contrast is all zero).
3122
0
  if (total > 0 && (num_blocks > (tot_num_blocks >> 4))) {
3123
    // Update the recursive mean square source_diff.
3124
0
    total = (total << 8) / num_blocks;
3125
0
    if (cpi->denoiser.nmse_source_diff_count == 0) {
3126
      // First sample in new interval.
3127
0
      cpi->denoiser.nmse_source_diff = total;
3128
0
      cpi->denoiser.qp_avg = cm->base_qindex;
3129
0
    } else {
3130
      // For subsequent samples, use average with weight ~1/4 for new sample.
3131
0
      cpi->denoiser.nmse_source_diff =
3132
0
          (int)((total + 3 * cpi->denoiser.nmse_source_diff) >> 2);
3133
0
      cpi->denoiser.qp_avg =
3134
0
          (int)((cm->base_qindex + 3 * cpi->denoiser.qp_avg) >> 2);
3135
0
    }
3136
0
    cpi->denoiser.nmse_source_diff_count++;
3137
0
  }
3138
  // Check for changing the denoiser mode, when we have obtained #samples =
3139
  // num_mode_change. Condition the change also on the bitrate and QP.
3140
0
  if (cpi->denoiser.nmse_source_diff_count == num_mode_change) {
3141
    // Check for going up: from normal to aggressive mode.
3142
0
    if ((cpi->denoiser.denoiser_mode == kDenoiserOnYUV) &&
3143
0
        (cpi->denoiser.nmse_source_diff >
3144
0
         cpi->denoiser.threshold_aggressive_mode) &&
3145
0
        (cpi->denoiser.qp_avg < cpi->denoiser.qp_threshold_up &&
3146
0
         bandwidth > cpi->denoiser.bitrate_threshold)) {
3147
0
      vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUVAggressive);
3148
0
    } else {
3149
      // Check for going down: from aggressive to normal mode.
3150
0
      if (((cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) &&
3151
0
           (cpi->denoiser.nmse_source_diff <
3152
0
            cpi->denoiser.threshold_aggressive_mode)) ||
3153
0
          ((cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) &&
3154
0
           (cpi->denoiser.qp_avg > cpi->denoiser.qp_threshold_down ||
3155
0
            bandwidth < cpi->denoiser.bitrate_threshold))) {
3156
0
        vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUV);
3157
0
      }
3158
0
    }
3159
    // Reset metric and counter for next interval.
3160
0
    cpi->denoiser.nmse_source_diff = 0;
3161
0
    cpi->denoiser.qp_avg = 0;
3162
0
    cpi->denoiser.nmse_source_diff_count = 0;
3163
0
  }
3164
0
}
3165
#endif
3166
3167
110k
void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm) {
3168
110k
  const FRAME_TYPE frame_type = cm->frame_type;
3169
3170
110k
  int update_any_ref_buffers = 1;
3171
110k
  if (cpi->common.refresh_last_frame == 0 &&
3172
0
      cpi->common.refresh_golden_frame == 0 &&
3173
0
      cpi->common.refresh_alt_ref_frame == 0) {
3174
0
    update_any_ref_buffers = 0;
3175
0
  }
3176
3177
110k
  if (cm->no_lpf) {
3178
0
    cm->filter_level = 0;
3179
110k
  } else {
3180
#if CONFIG_INTERNAL_STATS
3181
    struct vpx_usec_timer timer;
3182
#endif
3183
3184
110k
    vpx_clear_system_state();
3185
3186
#if CONFIG_INTERNAL_STATS
3187
    vpx_usec_timer_start(&timer);
3188
#endif
3189
110k
    if (cpi->sf.auto_filter == 0) {
3190
10
#if CONFIG_TEMPORAL_DENOISING
3191
10
      if (cpi->oxcf.noise_sensitivity && cm->frame_type != KEY_FRAME) {
3192
        // Use the denoised buffer for selecting base loop filter level.
3193
        // Denoised signal for current frame is stored in INTRA_FRAME.
3194
        // No denoising on key frames.
3195
0
        vp8cx_pick_filter_level_fast(
3196
0
            &cpi->denoiser.yv12_running_avg[INTRA_FRAME], cpi);
3197
10
      } else {
3198
10
        vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3199
10
      }
3200
#else
3201
      vp8cx_pick_filter_level_fast(cpi->Source, cpi);
3202
#endif
3203
110k
    } else {
3204
110k
#if CONFIG_TEMPORAL_DENOISING
3205
110k
      if (cpi->oxcf.noise_sensitivity && cm->frame_type != KEY_FRAME) {
3206
        // Use the denoised buffer for selecting base loop filter level.
3207
        // Denoised signal for current frame is stored in INTRA_FRAME.
3208
        // No denoising on key frames.
3209
0
        vp8cx_pick_filter_level(&cpi->denoiser.yv12_running_avg[INTRA_FRAME],
3210
0
                                cpi);
3211
110k
      } else {
3212
110k
        vp8cx_pick_filter_level(cpi->Source, cpi);
3213
110k
      }
3214
#else
3215
      vp8cx_pick_filter_level(cpi->Source, cpi);
3216
#endif
3217
110k
    }
3218
3219
110k
    if (cm->filter_level > 0) {
3220
98.3k
      vp8cx_set_alt_lf_level(cpi, cm->filter_level);
3221
98.3k
    }
3222
3223
#if CONFIG_INTERNAL_STATS
3224
    vpx_usec_timer_mark(&timer);
3225
    cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3226
#endif
3227
110k
  }
3228
3229
110k
#if CONFIG_MULTITHREAD
3230
110k
  if (vpx_atomic_load_acquire(&cpi->b_multi_threaded)) {
3231
    /* signal that we have set filter_level */
3232
0
    vp8_sem_post(&cpi->h_event_end_lpf);
3233
0
  }
3234
110k
#endif
3235
3236
  // No need to apply loop-filter if the encoded frame does not update
3237
  // any reference buffers.
3238
110k
  if (cm->filter_level > 0 && update_any_ref_buffers) {
3239
98.3k
    vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, frame_type);
3240
98.3k
  }
3241
3242
110k
  vp8_yv12_extend_frame_borders(cm->frame_to_show);
3243
110k
}
3244
// Return 1 if frame is to be dropped. Update frame drop decimation
3245
// counters.
3246
110k
int vp8_check_drop_buffer(VP8_COMP *cpi) {
3247
110k
  VP8_COMMON *cm = &cpi->common;
3248
110k
  int drop_mark = (int)(cpi->oxcf.drop_frames_water_mark *
3249
110k
                        cpi->oxcf.optimal_buffer_level / 100);
3250
110k
  int drop_mark75 = drop_mark * 2 / 3;
3251
110k
  int drop_mark50 = drop_mark / 4;
3252
110k
  int drop_mark25 = drop_mark / 8;
3253
110k
  if (cpi->drop_frames_allowed) {
3254
    /* The reset to decimation 0 is only done here for one pass.
3255
     * Once it is set two pass leaves decimation on till the next kf.
3256
     */
3257
0
    if (cpi->buffer_level > drop_mark && cpi->decimation_factor > 0) {
3258
0
      cpi->decimation_factor--;
3259
0
    }
3260
3261
0
    if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0) {
3262
0
      cpi->decimation_factor = 1;
3263
3264
0
    } else if (cpi->buffer_level < drop_mark25 &&
3265
0
               (cpi->decimation_factor == 2 || cpi->decimation_factor == 3)) {
3266
0
      cpi->decimation_factor = 3;
3267
0
    } else if (cpi->buffer_level < drop_mark50 &&
3268
0
               (cpi->decimation_factor == 1 || cpi->decimation_factor == 2)) {
3269
0
      cpi->decimation_factor = 2;
3270
0
    } else if (cpi->buffer_level < drop_mark75 &&
3271
0
               (cpi->decimation_factor == 0 || cpi->decimation_factor == 1)) {
3272
0
      cpi->decimation_factor = 1;
3273
0
    }
3274
0
  }
3275
3276
  /* The following decimates the frame rate according to a regular
3277
   * pattern (i.e. to 1/2 or 2/3 frame rate) This can be used to help
3278
   * prevent buffer under-run in CBR mode. Alternatively it might be
3279
   * desirable in some situations to drop frame rate but throw more bits
3280
   * at each frame.
3281
   *
3282
   * Note that dropping a key frame can be problematic if spatial
3283
   * resampling is also active
3284
   */
3285
110k
  if (cpi->decimation_factor > 0 && cpi->drop_frames_allowed) {
3286
0
    switch (cpi->decimation_factor) {
3287
0
      case 1:
3288
0
        cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
3289
0
        break;
3290
0
      case 2:
3291
0
        cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3292
0
        break;
3293
0
      case 3:
3294
0
        cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
3295
0
        break;
3296
0
    }
3297
3298
    /* Note that we should not throw out a key frame (especially when
3299
     * spatial resampling is enabled).
3300
     */
3301
0
    if (cm->frame_type == KEY_FRAME) {
3302
0
      cpi->decimation_count = cpi->decimation_factor;
3303
0
    } else if (cpi->decimation_count > 0) {
3304
0
      cpi->decimation_count--;
3305
3306
0
      cpi->bits_off_target += cpi->av_per_frame_bandwidth;
3307
0
      if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
3308
0
        cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3309
0
      }
3310
3311
#if CONFIG_MULTI_RES_ENCODING
3312
      vp8_store_drop_frame_info(cpi);
3313
#endif
3314
3315
0
      cm->current_video_frame++;
3316
0
      cpi->frames_since_key++;
3317
0
      cpi->ext_refresh_frame_flags_pending = 0;
3318
      // We advance the temporal pattern for dropped frames.
3319
0
      cpi->temporal_pattern_counter++;
3320
3321
#if CONFIG_INTERNAL_STATS
3322
      cpi->count++;
3323
#endif
3324
3325
0
      cpi->buffer_level = cpi->bits_off_target;
3326
3327
0
      if (cpi->oxcf.number_of_layers > 1) {
3328
0
        unsigned int i;
3329
3330
        /* Propagate bits saved by dropping the frame to higher
3331
         * layers
3332
         */
3333
0
        for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
3334
0
          LAYER_CONTEXT *lc = &cpi->layer_context[i];
3335
0
          lc->bits_off_target += (int)(lc->target_bandwidth / lc->framerate);
3336
0
          if (lc->bits_off_target > lc->maximum_buffer_size) {
3337
0
            lc->bits_off_target = lc->maximum_buffer_size;
3338
0
          }
3339
0
          lc->buffer_level = lc->bits_off_target;
3340
0
        }
3341
0
      }
3342
0
      return 1;
3343
0
    } else {
3344
0
      cpi->decimation_count = cpi->decimation_factor;
3345
0
    }
3346
110k
  } else {
3347
110k
    cpi->decimation_count = 0;
3348
110k
  }
3349
110k
  return 0;
3350
110k
}
3351
3352
static void encode_frame_to_data_rate(VP8_COMP *cpi, size_t *size,
3353
                                      unsigned char *dest,
3354
                                      unsigned char *dest_end,
3355
110k
                                      unsigned int *frame_flags) {
3356
110k
  int Q;
3357
110k
  int frame_over_shoot_limit;
3358
110k
  int frame_under_shoot_limit;
3359
3360
110k
  int Loop = 0;
3361
3362
110k
  VP8_COMMON *cm = &cpi->common;
3363
110k
  int active_worst_qchanged = 0;
3364
3365
110k
#if !CONFIG_REALTIME_ONLY
3366
110k
  int q_low;
3367
110k
  int q_high;
3368
110k
  int zbin_oq_high;
3369
110k
  int zbin_oq_low = 0;
3370
110k
  int top_index;
3371
110k
  int bottom_index;
3372
110k
  int overshoot_seen = 0;
3373
110k
  int undershoot_seen = 0;
3374
110k
#endif
3375
3376
  /* Clear down mmx registers to allow floating point in what follows */
3377
110k
  vpx_clear_system_state();
3378
3379
110k
  if (cpi->force_next_frame_intra) {
3380
0
    cm->frame_type = KEY_FRAME; /* delayed intra frame */
3381
0
    cpi->force_next_frame_intra = 0;
3382
0
  }
3383
3384
  /* For an alt ref frame in 2 pass we skip the call to the second pass
3385
   * function that sets the target bandwidth
3386
   */
3387
110k
  switch (cpi->pass) {
3388
0
#if !CONFIG_REALTIME_ONLY
3389
0
    case 2:
3390
0
      if (cpi->common.refresh_alt_ref_frame) {
3391
        /* Per frame bit target for the alt ref frame */
3392
0
        cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
3393
        /* per second target bitrate */
3394
0
        cpi->target_bandwidth =
3395
0
            (int)(cpi->twopass.gf_bits * cpi->output_framerate);
3396
0
      }
3397
0
      break;
3398
0
#endif  // !CONFIG_REALTIME_ONLY
3399
110k
    default: {
3400
110k
      const double per_frame_bandwidth =
3401
110k
          round(cpi->target_bandwidth / cpi->output_framerate);
3402
110k
      cpi->per_frame_bandwidth = (int)VPXMIN(per_frame_bandwidth, INT_MAX);
3403
110k
      break;
3404
0
    }
3405
110k
  }
3406
3407
  /* Default turn off buffer to buffer copying */
3408
110k
  cm->copy_buffer_to_gf = 0;
3409
110k
  cm->copy_buffer_to_arf = 0;
3410
3411
  /* Clear zbin over-quant value and mode boost values. */
3412
110k
  cpi->mb.zbin_over_quant = 0;
3413
110k
  cpi->mb.zbin_mode_boost = 0;
3414
3415
  /* Enable or disable mode based tweaking of the zbin
3416
   * For 2 Pass Only used where GF/ARF prediction quality
3417
   * is above a threshold
3418
   */
3419
110k
  cpi->mb.zbin_mode_boost_enabled = 1;
3420
110k
  if (cpi->pass == 2) {
3421
0
    if (cpi->gfu_boost <= 400) {
3422
0
      cpi->mb.zbin_mode_boost_enabled = 0;
3423
0
    }
3424
0
  }
3425
3426
  /* Current default encoder behaviour for the altref sign bias */
3427
110k
  if (cpi->source_alt_ref_active) {
3428
0
    cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
3429
110k
  } else {
3430
110k
    cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
3431
110k
  }
3432
3433
  /* Check to see if a key frame is signaled
3434
   * For two pass with auto key frame enabled cm->frame_type may already
3435
   * be set, but not for one pass.
3436
   */
3437
110k
  if ((cm->current_video_frame == 0) || (cm->frame_flags & FRAMEFLAGS_KEY) ||
3438
98.8k
      (cpi->oxcf.auto_key &&
3439
98.8k
       (cpi->frames_since_key % cpi->key_frame_frequency == 0))) {
3440
    /* Key frame from VFW/auto-keyframe/first frame */
3441
15.9k
    cm->frame_type = KEY_FRAME;
3442
15.9k
#if CONFIG_TEMPORAL_DENOISING
3443
15.9k
    if (cpi->oxcf.noise_sensitivity == 4) {
3444
      // For adaptive mode, reset denoiser to normal mode on key frame.
3445
0
      vp8_denoiser_set_parameters(&cpi->denoiser, kDenoiserOnYUV);
3446
0
    }
3447
15.9k
#endif
3448
15.9k
  }
3449
3450
#if CONFIG_MULTI_RES_ENCODING
3451
  if (cpi->oxcf.mr_total_resolutions > 1) {
3452
    LOWER_RES_FRAME_INFO *low_res_frame_info =
3453
        (LOWER_RES_FRAME_INFO *)cpi->oxcf.mr_low_res_mode_info;
3454
3455
    if (cpi->oxcf.mr_encoder_id) {
3456
      // Check if lower resolution is available for motion vector reuse.
3457
      if (cm->frame_type != KEY_FRAME) {
3458
        cpi->mr_low_res_mv_avail = 1;
3459
        cpi->mr_low_res_mv_avail &= !(low_res_frame_info->is_frame_dropped);
3460
3461
        if (cpi->ref_frame_flags & VP8_LAST_FRAME)
3462
          cpi->mr_low_res_mv_avail &=
3463
              (cpi->current_ref_frames[LAST_FRAME] ==
3464
               low_res_frame_info->low_res_ref_frames[LAST_FRAME]);
3465
3466
        if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
3467
          cpi->mr_low_res_mv_avail &=
3468
              (cpi->current_ref_frames[GOLDEN_FRAME] ==
3469
               low_res_frame_info->low_res_ref_frames[GOLDEN_FRAME]);
3470
3471
        // Don't use altref to determine whether low res is available.
3472
        // TODO (marpan): Should we make this type of condition on a
3473
        // per-reference frame basis?
3474
        /*
3475
        if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
3476
            cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[ALTREF_FRAME]
3477
                     == low_res_frame_info->low_res_ref_frames[ALTREF_FRAME]);
3478
        */
3479
      }
3480
      // Disable motion vector reuse (i.e., disable any usage of the low_res)
3481
      // if the previous lower stream is skipped/disabled.
3482
      if (low_res_frame_info->skip_encoding_prev_stream) {
3483
        cpi->mr_low_res_mv_avail = 0;
3484
      }
3485
    }
3486
    // This stream is not skipped (i.e., it's being encoded), so set this skip
3487
    // flag to 0. This is needed for the next stream (i.e., which is the next
3488
    // frame to be encoded).
3489
    low_res_frame_info->skip_encoding_prev_stream = 0;
3490
3491
    // On a key frame: For the lowest resolution, keep track of the key frame
3492
    // counter value. For the higher resolutions, reset the current video
3493
    // frame counter to that of the lowest resolution.
3494
    // This is done to the handle the case where we may stop/start encoding
3495
    // higher layer(s). The restart-encoding of higher layer is only signaled
3496
    // by a key frame for now.
3497
    // TODO (marpan): Add flag to indicate restart-encoding of higher layer.
3498
    if (cm->frame_type == KEY_FRAME) {
3499
      if (cpi->oxcf.mr_encoder_id) {
3500
        // If the initial starting value of the buffer level is zero (this can
3501
        // happen because we may have not started encoding this higher stream),
3502
        // then reset it to non-zero value based on |starting_buffer_level|.
3503
        if (cpi->common.current_video_frame == 0 && cpi->buffer_level == 0) {
3504
          unsigned int i;
3505
          cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
3506
          cpi->buffer_level = cpi->oxcf.starting_buffer_level;
3507
          for (i = 0; i < cpi->oxcf.number_of_layers; ++i) {
3508
            LAYER_CONTEXT *lc = &cpi->layer_context[i];
3509
            lc->bits_off_target = lc->starting_buffer_level;
3510
            lc->buffer_level = lc->starting_buffer_level;
3511
          }
3512
        }
3513
        cpi->common.current_video_frame =
3514
            low_res_frame_info->key_frame_counter_value;
3515
      } else {
3516
        low_res_frame_info->key_frame_counter_value =
3517
            cpi->common.current_video_frame;
3518
      }
3519
    }
3520
  }
3521
#endif
3522
3523
  // Find the reference frame closest to the current frame.
3524
110k
  cpi->closest_reference_frame = LAST_FRAME;
3525
110k
  if (cm->frame_type != KEY_FRAME) {
3526
94.8k
    int i;
3527
94.8k
    MV_REFERENCE_FRAME closest_ref = INTRA_FRAME;
3528
94.8k
    if (cpi->ref_frame_flags & VP8_LAST_FRAME) {
3529
94.8k
      closest_ref = LAST_FRAME;
3530
94.8k
    } else if (cpi->ref_frame_flags & VP8_GOLD_FRAME) {
3531
0
      closest_ref = GOLDEN_FRAME;
3532
0
    } else if (cpi->ref_frame_flags & VP8_ALTR_FRAME) {
3533
0
      closest_ref = ALTREF_FRAME;
3534
0
    }
3535
379k
    for (i = 1; i <= 3; ++i) {
3536
284k
      vpx_ref_frame_type_t ref_frame_type =
3537
284k
          (vpx_ref_frame_type_t)((i == 3) ? 4 : i);
3538
284k
      if (cpi->ref_frame_flags & ref_frame_type) {
3539
236k
        if ((cm->current_video_frame - cpi->current_ref_frames[i]) <
3540
236k
            (cm->current_video_frame - cpi->current_ref_frames[closest_ref])) {
3541
0
          closest_ref = i;
3542
0
        }
3543
236k
      }
3544
284k
    }
3545
94.8k
    cpi->closest_reference_frame = closest_ref;
3546
94.8k
  }
3547
3548
  /* Set various flags etc to special state if it is a key frame */
3549
110k
  if (cm->frame_type == KEY_FRAME) {
3550
15.9k
    int i;
3551
3552
    // Set the loop filter deltas and segmentation map update
3553
15.9k
    setup_features(cpi);
3554
3555
    /* The alternate reference frame cannot be active for a key frame */
3556
15.9k
    cpi->source_alt_ref_active = 0;
3557
3558
    /* Reset the RD threshold multipliers to default of * 1 (128) */
3559
335k
    for (i = 0; i < MAX_MODES; ++i) {
3560
319k
      cpi->mb.rd_thresh_mult[i] = 128;
3561
319k
    }
3562
3563
    // Reset the zero_last counter to 0 on key frame.
3564
15.9k
    memset(cpi->consec_zero_last, 0, cm->mb_rows * cm->mb_cols);
3565
15.9k
    memset(cpi->consec_zero_last_mvbias, 0,
3566
15.9k
           (cpi->common.mb_rows * cpi->common.mb_cols));
3567
15.9k
  }
3568
3569
#if 0
3570
    /* Experimental code for lagged compress and one pass
3571
     * Initialise one_pass GF frames stats
3572
     * Update stats used for GF selection
3573
     */
3574
    {
3575
        cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
3576
3577
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
3578
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
3579
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
3580
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
3581
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
3582
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
3583
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
3584
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
3585
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
3586
    }
3587
#endif
3588
3589
110k
  update_rd_ref_frame_probs(cpi);
3590
3591
110k
  if (vp8_check_drop_buffer(cpi)) {
3592
0
    return;
3593
0
  }
3594
3595
  /* Decide how big to make the frame */
3596
110k
  if (!vp8_pick_frame_size(cpi)) {
3597
/*TODO: 2 drop_frame and return code could be put together. */
3598
#if CONFIG_MULTI_RES_ENCODING
3599
    vp8_store_drop_frame_info(cpi);
3600
#endif
3601
0
    cm->current_video_frame++;
3602
0
    cpi->frames_since_key++;
3603
0
    cpi->ext_refresh_frame_flags_pending = 0;
3604
    // We advance the temporal pattern for dropped frames.
3605
0
    cpi->temporal_pattern_counter++;
3606
0
    return;
3607
0
  }
3608
3609
  /* Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
3610
   * This has a knock on effect on active best quality as well.
3611
   * For CBR if the buffer reaches its maximum level then we can no longer
3612
   * save up bits for later frames so we might as well use them up
3613
   * on the current frame.
3614
   */
3615
110k
  if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
3616
0
      (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) &&
3617
0
      cpi->buffered_mode) {
3618
    /* Max adjustment is 1/4 */
3619
0
    int Adjustment = cpi->active_worst_quality / 4;
3620
3621
0
    if (Adjustment) {
3622
0
      int buff_lvl_step;
3623
3624
0
      if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size) {
3625
0
        buff_lvl_step = (int)((cpi->oxcf.maximum_buffer_size -
3626
0
                               cpi->oxcf.optimal_buffer_level) /
3627
0
                              Adjustment);
3628
3629
0
        if (buff_lvl_step) {
3630
0
          Adjustment =
3631
0
              (int)((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) /
3632
0
                    buff_lvl_step);
3633
0
        } else {
3634
0
          Adjustment = 0;
3635
0
        }
3636
0
      }
3637
3638
0
      cpi->active_worst_quality -= Adjustment;
3639
3640
0
      if (cpi->active_worst_quality < cpi->active_best_quality) {
3641
0
        cpi->active_worst_quality = cpi->active_best_quality;
3642
0
      }
3643
0
    }
3644
0
  }
3645
3646
  /* Set an active best quality and if necessary active worst quality
3647
   * There is some odd behavior for one pass here that needs attention.
3648
   */
3649
110k
  if ((cpi->pass == 2) || (cpi->ni_frames > 150)) {
3650
10.3k
    vpx_clear_system_state();
3651
3652
10.3k
    Q = cpi->active_worst_quality;
3653
3654
10.3k
    if (cm->frame_type == KEY_FRAME) {
3655
203
      if (cpi->pass == 2) {
3656
0
        if (cpi->gfu_boost > 600) {
3657
0
          cpi->active_best_quality = kf_low_motion_minq[Q];
3658
0
        } else {
3659
0
          cpi->active_best_quality = kf_high_motion_minq[Q];
3660
0
        }
3661
3662
        /* Special case for key frames forced because we have reached
3663
         * the maximum key frame interval. Here force the Q to a range
3664
         * based on the ambient Q to reduce the risk of popping
3665
         */
3666
0
        if (cpi->this_key_frame_forced) {
3667
0
          if (cpi->active_best_quality > cpi->avg_frame_qindex * 7 / 8) {
3668
0
            cpi->active_best_quality = cpi->avg_frame_qindex * 7 / 8;
3669
0
          } else if (cpi->active_best_quality < (cpi->avg_frame_qindex >> 2)) {
3670
0
            cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
3671
0
          }
3672
0
        }
3673
0
      }
3674
      /* One pass more conservative */
3675
203
      else {
3676
203
        cpi->active_best_quality = kf_high_motion_minq[Q];
3677
203
      }
3678
203
    }
3679
3680
10.1k
    else if (cpi->oxcf.number_of_layers == 1 &&
3681
10.1k
             (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)) {
3682
      /* Use the lower of cpi->active_worst_quality and recent
3683
       * average Q as basis for GF/ARF Q limit unless last frame was
3684
       * a key frame.
3685
       */
3686
1.03k
      if ((cpi->frames_since_key > 1) &&
3687
1.03k
          (cpi->avg_frame_qindex < cpi->active_worst_quality)) {
3688
701
        Q = cpi->avg_frame_qindex;
3689
701
      }
3690
3691
      /* For constrained quality don't allow Q less than the cq level */
3692
1.03k
      if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3693
363
          (Q < cpi->cq_target_quality)) {
3694
99
        Q = cpi->cq_target_quality;
3695
99
      }
3696
3697
1.03k
      if (cpi->pass == 2) {
3698
0
        if (cpi->gfu_boost > 1000) {
3699
0
          cpi->active_best_quality = gf_low_motion_minq[Q];
3700
0
        } else if (cpi->gfu_boost < 400) {
3701
0
          cpi->active_best_quality = gf_high_motion_minq[Q];
3702
0
        } else {
3703
0
          cpi->active_best_quality = gf_mid_motion_minq[Q];
3704
0
        }
3705
3706
        /* Constrained quality use slightly lower active best. */
3707
0
        if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
3708
0
          cpi->active_best_quality = cpi->active_best_quality * 15 / 16;
3709
0
        }
3710
0
      }
3711
      /* One pass more conservative */
3712
1.03k
      else {
3713
1.03k
        cpi->active_best_quality = gf_high_motion_minq[Q];
3714
1.03k
      }
3715
9.15k
    } else {
3716
9.15k
      cpi->active_best_quality = inter_minq[Q];
3717
3718
      /* For the constant/constrained quality mode we don't want
3719
       * q to fall below the cq level.
3720
       */
3721
9.15k
      if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
3722
3.30k
          (cpi->active_best_quality < cpi->cq_target_quality)) {
3723
        /* If we are strongly undershooting the target rate in the last
3724
         * frames then use the user passed in cq value not the auto
3725
         * cq value.
3726
         */
3727
1.17k
        if (cpi->rolling_actual_bits < cpi->min_frame_bandwidth) {
3728
0
          cpi->active_best_quality = cpi->oxcf.cq_level;
3729
1.17k
        } else {
3730
1.17k
          cpi->active_best_quality = cpi->cq_target_quality;
3731
1.17k
        }
3732
1.17k
      }
3733
9.15k
    }
3734
3735
    /* If CBR and the buffer is as full then it is reasonable to allow
3736
     * higher quality on the frames to prevent bits just going to waste.
3737
     */
3738
10.3k
    if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3739
      /* Note that the use of >= here elliminates the risk of a divide
3740
       * by 0 error in the else if clause
3741
       */
3742
0
      if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size) {
3743
0
        cpi->active_best_quality = cpi->best_quality;
3744
3745
0
      } else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level) {
3746
0
        int Fraction =
3747
0
            (int)(((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) /
3748
0
                  (cpi->oxcf.maximum_buffer_size -
3749
0
                   cpi->oxcf.optimal_buffer_level));
3750
0
        int min_qadjustment =
3751
0
            ((cpi->active_best_quality - cpi->best_quality) * Fraction) / 128;
3752
3753
0
        cpi->active_best_quality -= min_qadjustment;
3754
0
      }
3755
0
    }
3756
10.3k
  }
3757
  /* Make sure constrained quality mode limits are adhered to for the first
3758
   * few frames of one pass encodes
3759
   */
3760
100k
  else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
3761
18.6k
    if ((cm->frame_type == KEY_FRAME) || cm->refresh_golden_frame ||
3762
15.3k
        cpi->common.refresh_alt_ref_frame) {
3763
3.33k
      cpi->active_best_quality = cpi->best_quality;
3764
15.3k
    } else if (cpi->active_best_quality < cpi->cq_target_quality) {
3765
14.0k
      cpi->active_best_quality = cpi->cq_target_quality;
3766
14.0k
    }
3767
18.6k
  }
3768
3769
  /* Clip the active best and worst quality values to limits */
3770
110k
  if (cpi->active_worst_quality > cpi->worst_quality) {
3771
0
    cpi->active_worst_quality = cpi->worst_quality;
3772
0
  }
3773
3774
110k
  if (cpi->active_best_quality < cpi->best_quality) {
3775
2.51k
    cpi->active_best_quality = cpi->best_quality;
3776
2.51k
  }
3777
3778
110k
  if (cpi->active_worst_quality < cpi->active_best_quality) {
3779
0
    cpi->active_worst_quality = cpi->active_best_quality;
3780
0
  }
3781
3782
  /* Determine initial Q to try */
3783
110k
  Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3784
3785
110k
#if !CONFIG_REALTIME_ONLY
3786
3787
  /* Set highest allowed value for Zbin over quant */
3788
110k
  if (cm->frame_type == KEY_FRAME) {
3789
15.9k
    zbin_oq_high = 0;
3790
94.8k
  } else if ((cpi->oxcf.number_of_layers == 1) &&
3791
94.8k
             ((cm->refresh_alt_ref_frame ||
3792
94.8k
               (cm->refresh_golden_frame && !cpi->source_alt_ref_active)))) {
3793
8.59k
    zbin_oq_high = 16;
3794
86.3k
  } else {
3795
86.3k
    zbin_oq_high = ZBIN_OQ_MAX;
3796
86.3k
  }
3797
110k
#endif
3798
3799
110k
  compute_skin_map(cpi);
3800
3801
  /* Setup background Q adjustment for error resilient mode.
3802
   * For multi-layer encodes only enable this for the base layer.
3803
   */
3804
110k
  if (cpi->cyclic_refresh_mode_enabled) {
3805
    // Special case for screen_content_mode with golden frame updates.
3806
0
    int disable_cr_gf =
3807
0
        (cpi->oxcf.screen_content_mode == 2 && cm->refresh_golden_frame);
3808
0
    if (cpi->current_layer == 0 && cpi->force_maxqp == 0 && !disable_cr_gf) {
3809
0
      cyclic_background_refresh(cpi, Q, 0);
3810
0
    } else {
3811
0
      disable_segmentation(cpi);
3812
0
    }
3813
0
  }
3814
3815
110k
  vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit,
3816
110k
                                &frame_over_shoot_limit);
3817
3818
110k
#if !CONFIG_REALTIME_ONLY
3819
  /* Limit Q range for the adaptive loop. */
3820
110k
  bottom_index = cpi->active_best_quality;
3821
110k
  top_index = cpi->active_worst_quality;
3822
110k
  q_low = cpi->active_best_quality;
3823
110k
  q_high = cpi->active_worst_quality;
3824
110k
#endif
3825
3826
110k
  vp8_save_coding_context(cpi);
3827
3828
110k
  scale_and_extend_source(cpi->un_scaled_source, cpi);
3829
3830
110k
  if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame &&
3831
0
      cpi->oxcf.noise_sensitivity > 0) {
3832
0
    if (cpi->raw_source_frame.y_width != cpi->Source->y_width ||
3833
0
        cpi->raw_source_frame.y_height != cpi->Source->y_height) {
3834
0
      vp8_yv12_de_alloc_frame_buffer(&cpi->raw_source_frame);
3835
0
      if (vp8_yv12_alloc_frame_buffer(
3836
0
              &cpi->raw_source_frame, cpi->Source->y_width,
3837
0
              cpi->Source->y_height, VP8BORDERINPIXELS) < 0) {
3838
0
        vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3839
0
                           "Failed to allocate raw source frame buffer");
3840
0
      }
3841
0
    }
3842
0
    vp8_yv12_copy_frame(cpi->Source, &cpi->raw_source_frame);
3843
0
  }
3844
3845
110k
#if CONFIG_TEMPORAL_DENOISING && CONFIG_POSTPROC
3846
  // Option to apply spatial blur under the aggressive or adaptive
3847
  // (temporal denoising) mode.
3848
110k
  if (cpi->oxcf.noise_sensitivity >= 3) {
3849
0
    if (cpi->denoiser.denoise_pars.spatial_blur != 0) {
3850
0
      vp8_de_noise(cm, cpi->Source, cpi->denoiser.denoise_pars.spatial_blur, 1);
3851
0
    }
3852
0
  }
3853
110k
#endif
3854
3855
#if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC && !(CONFIG_TEMPORAL_DENOISING)
3856
3857
  if (cpi->oxcf.noise_sensitivity > 0) {
3858
    unsigned char *src;
3859
    int l = 0;
3860
3861
    switch (cpi->oxcf.noise_sensitivity) {
3862
      case 1: l = 20; break;
3863
      case 2: l = 40; break;
3864
      case 3: l = 60; break;
3865
      case 4: l = 80; break;
3866
      case 5: l = 100; break;
3867
      case 6: l = 150; break;
3868
    }
3869
3870
    if (cm->frame_type == KEY_FRAME) {
3871
      vp8_de_noise(cm, cpi->Source, l, 1);
3872
    } else {
3873
      vp8_de_noise(cm, cpi->Source, l, 1);
3874
3875
      src = cpi->Source->y_buffer;
3876
3877
      if (cpi->Source->y_stride < 0) {
3878
        src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
3879
      }
3880
    }
3881
  }
3882
3883
#endif
3884
3885
#ifdef OUTPUT_YUV_SRC
3886
  vpx_write_yuv_frame(yuv_file, cpi->Source);
3887
#endif
3888
3889
142k
  do {
3890
142k
    vpx_clear_system_state();
3891
3892
142k
    vp8_set_quantizer(cpi, Q);
3893
3894
    /* setup skip prob for costing in mode/mv decision */
3895
142k
    if (cpi->common.mb_no_coeff_skip) {
3896
142k
      cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
3897
3898
142k
      if (cm->frame_type != KEY_FRAME) {
3899
110k
        if (cpi->common.refresh_alt_ref_frame) {
3900
0
          if (cpi->last_skip_false_probs[2] != 0) {
3901
0
            cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3902
0
          }
3903
3904
          /*
3905
                              if(cpi->last_skip_false_probs[2]!=0 && abs(Q-
3906
             cpi->last_skip_probs_q[2])<=16 )
3907
             cpi->prob_skip_false = cpi->last_skip_false_probs[2];
3908
                              else if (cpi->last_skip_false_probs[2]!=0)
3909
             cpi->prob_skip_false = (cpi->last_skip_false_probs[2]  +
3910
             cpi->prob_skip_false ) / 2;
3911
             */
3912
110k
        } else if (cpi->common.refresh_golden_frame) {
3913
10.8k
          if (cpi->last_skip_false_probs[1] != 0) {
3914
8.37k
            cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3915
8.37k
          }
3916
3917
          /*
3918
                              if(cpi->last_skip_false_probs[1]!=0 && abs(Q-
3919
             cpi->last_skip_probs_q[1])<=16 )
3920
             cpi->prob_skip_false = cpi->last_skip_false_probs[1];
3921
                              else if (cpi->last_skip_false_probs[1]!=0)
3922
             cpi->prob_skip_false = (cpi->last_skip_false_probs[1]  +
3923
             cpi->prob_skip_false ) / 2;
3924
             */
3925
99.7k
        } else {
3926
99.7k
          if (cpi->last_skip_false_probs[0] != 0) {
3927
90.0k
            cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3928
90.0k
          }
3929
3930
          /*
3931
          if(cpi->last_skip_false_probs[0]!=0 && abs(Q-
3932
          cpi->last_skip_probs_q[0])<=16 )
3933
              cpi->prob_skip_false = cpi->last_skip_false_probs[0];
3934
          else if(cpi->last_skip_false_probs[0]!=0)
3935
              cpi->prob_skip_false = (cpi->last_skip_false_probs[0]  +
3936
          cpi->prob_skip_false ) / 2;
3937
              */
3938
99.7k
        }
3939
3940
        /* as this is for cost estimate, let's make sure it does not
3941
         * go extreme eitehr way
3942
         */
3943
110k
        if (cpi->prob_skip_false < 5) cpi->prob_skip_false = 5;
3944
3945
110k
        if (cpi->prob_skip_false > 250) cpi->prob_skip_false = 250;
3946
3947
110k
        if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref) {
3948
0
          cpi->prob_skip_false = 1;
3949
0
        }
3950
110k
      }
3951
3952
#if 0
3953
3954
            if (cpi->pass != 1)
3955
            {
3956
                FILE *f = fopen("skip.stt", "a");
3957
                fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
3958
                fclose(f);
3959
            }
3960
3961
#endif
3962
142k
    }
3963
3964
142k
    if (cm->frame_type == KEY_FRAME) {
3965
31.9k
      if (resize_key_frame(cpi)) {
3966
        /* If the frame size has changed, need to reset Q, quantizer,
3967
         * and background refresh.
3968
         */
3969
0
        Q = vp8_regulate_q(cpi, cpi->this_frame_target);
3970
0
        if (cpi->cyclic_refresh_mode_enabled) {
3971
0
          if (cpi->current_layer == 0) {
3972
0
            cyclic_background_refresh(cpi, Q, 0);
3973
0
          } else {
3974
0
            disable_segmentation(cpi);
3975
0
          }
3976
0
        }
3977
        // Reset the zero_last counter to 0 on key frame.
3978
0
        memset(cpi->consec_zero_last, 0, cm->mb_rows * cm->mb_cols);
3979
0
        memset(cpi->consec_zero_last_mvbias, 0,
3980
0
               (cpi->common.mb_rows * cpi->common.mb_cols));
3981
0
        vp8_set_quantizer(cpi, Q);
3982
0
      }
3983
3984
31.9k
      vp8_setup_key_frame(cpi);
3985
31.9k
    }
3986
3987
#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
3988
    {
3989
      if (cpi->oxcf.error_resilient_mode) cm->refresh_entropy_probs = 0;
3990
3991
      if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
3992
        if (cm->frame_type == KEY_FRAME) cm->refresh_entropy_probs = 1;
3993
      }
3994
3995
      if (cm->refresh_entropy_probs == 0) {
3996
        /* save a copy for later refresh */
3997
        cm->lfc = cm->fc;
3998
      }
3999
4000
      vp8_update_coef_context(cpi);
4001
4002
      vp8_update_coef_probs(cpi);
4003
4004
      /* transform / motion compensation build reconstruction frame
4005
       * +pack coef partitions
4006
       */
4007
      vp8_encode_frame(cpi);
4008
4009
      /* cpi->projected_frame_size is not needed for RT mode */
4010
    }
4011
#else
4012
    /* transform / motion compensation build reconstruction frame */
4013
142k
    vp8_encode_frame(cpi);
4014
4015
142k
    if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
4016
0
        cpi->rt_drop_recode_on_overshoot == 1) {
4017
0
      if (vp8_drop_encodedframe_overshoot(cpi, Q)) {
4018
0
        vpx_clear_system_state();
4019
0
        return;
4020
0
      }
4021
0
      if (cm->frame_type != KEY_FRAME)
4022
0
        cpi->last_pred_err_mb =
4023
0
            (int)(cpi->mb.prediction_error / cpi->common.MBs);
4024
0
    }
4025
4026
142k
    cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
4027
142k
    cpi->projected_frame_size =
4028
142k
        (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
4029
142k
#endif
4030
142k
    vpx_clear_system_state();
4031
4032
    /* Test to see if the stats generated for this frame indicate that
4033
     * we should have coded a key frame (assuming that we didn't)!
4034
     */
4035
4036
142k
    if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME &&
4037
110k
        cpi->compressor_speed != 2) {
4038
59.4k
#if !CONFIG_REALTIME_ONLY
4039
59.4k
      if (decide_key_frame(cpi)) {
4040
        /* Reset all our sizing numbers and recode */
4041
3.17k
        cm->frame_type = KEY_FRAME;
4042
4043
3.17k
        vp8_pick_frame_size(cpi);
4044
4045
        /* Clear the Alt reference frame active flag when we have
4046
         * a key frame
4047
         */
4048
3.17k
        cpi->source_alt_ref_active = 0;
4049
4050
        // Set the loop filter deltas and segmentation map update
4051
3.17k
        setup_features(cpi);
4052
4053
3.17k
        vp8_restore_coding_context(cpi);
4054
4055
3.17k
        Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4056
4057
3.17k
        vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit,
4058
3.17k
                                      &frame_over_shoot_limit);
4059
4060
        /* Limit Q range for the adaptive loop. */
4061
3.17k
        bottom_index = cpi->active_best_quality;
4062
3.17k
        top_index = cpi->active_worst_quality;
4063
3.17k
        q_low = cpi->active_best_quality;
4064
3.17k
        q_high = cpi->active_worst_quality;
4065
4066
3.17k
        Loop = 1;
4067
4068
3.17k
        continue;
4069
3.17k
      }
4070
59.4k
#endif
4071
59.4k
    }
4072
4073
139k
    vpx_clear_system_state();
4074
4075
139k
    if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4076
4077
    /* Are we are overshooting and up against the limit of active max Q. */
4078
139k
    if (!cpi->rt_always_update_correction_factor &&
4079
139k
        ((cpi->pass != 2) ||
4080
0
         (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
4081
139k
        (Q == cpi->active_worst_quality) &&
4082
34.2k
        (cpi->active_worst_quality < cpi->worst_quality) &&
4083
2.27k
        (cpi->projected_frame_size > frame_over_shoot_limit)) {
4084
558
      int over_size_percent =
4085
558
          ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) /
4086
558
          frame_over_shoot_limit;
4087
4088
      /* If so is there any scope for relaxing it */
4089
23.9k
      while ((cpi->active_worst_quality < cpi->worst_quality) &&
4090
23.7k
             (over_size_percent > 0)) {
4091
23.3k
        cpi->active_worst_quality++;
4092
        /* Assume 1 qstep = about 4% on frame size. */
4093
23.3k
        over_size_percent = (int)(over_size_percent * 0.96);
4094
23.3k
      }
4095
558
#if !CONFIG_REALTIME_ONLY
4096
558
      top_index = cpi->active_worst_quality;
4097
558
#endif  // !CONFIG_REALTIME_ONLY
4098
      /* If we have updated the active max Q do not call
4099
       * vp8_update_rate_correction_factors() this loop.
4100
       */
4101
558
      active_worst_qchanged = 1;
4102
138k
    } else {
4103
138k
      active_worst_qchanged = 0;
4104
138k
    }
4105
4106
#if CONFIG_REALTIME_ONLY
4107
    Loop = 0;
4108
#else
4109
    /* Special case handling for forced key frames */
4110
139k
    if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) {
4111
0
      int last_q = Q;
4112
0
      int kf_err = vp8_calc_ss_err(cpi->Source, &cm->yv12_fb[cm->new_fb_idx]);
4113
4114
      /* The key frame is not good enough */
4115
0
      if (kf_err > ((cpi->ambient_err * 7) >> 3)) {
4116
        /* Lower q_high */
4117
0
        q_high = (Q > q_low) ? (Q - 1) : q_low;
4118
4119
        /* Adjust Q */
4120
0
        Q = (q_high + q_low) >> 1;
4121
0
      }
4122
      /* The key frame is much better than the previous frame */
4123
0
      else if (kf_err < (cpi->ambient_err >> 1)) {
4124
        /* Raise q_low */
4125
0
        q_low = (Q < q_high) ? (Q + 1) : q_high;
4126
4127
        /* Adjust Q */
4128
0
        Q = (q_high + q_low + 1) >> 1;
4129
0
      }
4130
4131
      /* Clamp Q to upper and lower limits: */
4132
0
      if (Q > q_high) {
4133
0
        Q = q_high;
4134
0
      } else if (Q < q_low) {
4135
0
        Q = q_low;
4136
0
      }
4137
4138
0
      Loop = Q != last_q;
4139
0
    }
4140
4141
    /* Is the projected frame size out of range and are we allowed
4142
     * to attempt to recode.
4143
     */
4144
139k
    else if (recode_loop_test(cpi, frame_over_shoot_limit,
4145
139k
                              frame_under_shoot_limit, Q, top_index,
4146
139k
                              bottom_index)) {
4147
30.7k
      int last_q = Q;
4148
30.7k
      int Retries = 0;
4149
4150
      /* Frame size out of permitted range. Update correction factor
4151
       * & compute new Q to try...
4152
       */
4153
4154
      /* Frame is too large */
4155
30.7k
      if (cpi->projected_frame_size > cpi->this_frame_target) {
4156
        /* Raise Qlow as to at least the current value */
4157
17.1k
        q_low = (Q < q_high) ? (Q + 1) : q_high;
4158
4159
        /* If we are using over quant do the same for zbin_oq_low */
4160
17.1k
        if (cpi->mb.zbin_over_quant > 0) {
4161
0
          zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high)
4162
0
                            ? (cpi->mb.zbin_over_quant + 1)
4163
0
                            : zbin_oq_high;
4164
0
        }
4165
4166
17.1k
        if (undershoot_seen) {
4167
          /* Update rate_correction_factor unless
4168
           * cpi->active_worst_quality has changed.
4169
           */
4170
1.33k
          if (!active_worst_qchanged) {
4171
1.33k
            vp8_update_rate_correction_factors(cpi, 1);
4172
1.33k
          }
4173
4174
1.33k
          Q = (q_high + q_low + 1) / 2;
4175
4176
          /* Adjust cpi->zbin_over_quant (only allowed when Q
4177
           * is max)
4178
           */
4179
1.33k
          if (Q < MAXQ) {
4180
1.33k
            cpi->mb.zbin_over_quant = 0;
4181
1.33k
          } else {
4182
2
            zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high)
4183
2
                              ? (cpi->mb.zbin_over_quant + 1)
4184
2
                              : zbin_oq_high;
4185
2
            cpi->mb.zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4186
2
          }
4187
15.7k
        } else {
4188
          /* Update rate_correction_factor unless
4189
           * cpi->active_worst_quality has changed.
4190
           */
4191
15.7k
          if (!active_worst_qchanged) {
4192
15.2k
            vp8_update_rate_correction_factors(cpi, 0);
4193
15.2k
          }
4194
4195
15.7k
          Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4196
4197
15.8k
          while (((Q < q_low) || (cpi->mb.zbin_over_quant < zbin_oq_low)) &&
4198
37
                 (Retries < 10)) {
4199
37
            vp8_update_rate_correction_factors(cpi, 0);
4200
37
            Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4201
37
            Retries++;
4202
37
          }
4203
15.7k
        }
4204
4205
17.1k
        overshoot_seen = 1;
4206
17.1k
      }
4207
      /* Frame is too small */
4208
13.6k
      else {
4209
13.6k
        if (cpi->mb.zbin_over_quant == 0) {
4210
          /* Lower q_high if not using over quant */
4211
12.1k
          q_high = (Q > q_low) ? (Q - 1) : q_low;
4212
12.1k
        } else {
4213
          /* else lower zbin_oq_high */
4214
1.47k
          zbin_oq_high = (cpi->mb.zbin_over_quant > zbin_oq_low)
4215
1.47k
                             ? (cpi->mb.zbin_over_quant - 1)
4216
1.47k
                             : zbin_oq_low;
4217
1.47k
        }
4218
4219
13.6k
        if (overshoot_seen) {
4220
          /* Update rate_correction_factor unless
4221
           * cpi->active_worst_quality has changed.
4222
           */
4223
909
          if (!active_worst_qchanged) {
4224
909
            vp8_update_rate_correction_factors(cpi, 1);
4225
909
          }
4226
4227
909
          Q = (q_high + q_low) / 2;
4228
4229
          /* Adjust cpi->zbin_over_quant (only allowed when Q
4230
           * is max)
4231
           */
4232
909
          if (Q < MAXQ) {
4233
909
            cpi->mb.zbin_over_quant = 0;
4234
909
          } else {
4235
0
            cpi->mb.zbin_over_quant = (zbin_oq_high + zbin_oq_low) / 2;
4236
0
          }
4237
12.7k
        } else {
4238
          /* Update rate_correction_factor unless
4239
           * cpi->active_worst_quality has changed.
4240
           */
4241
12.7k
          if (!active_worst_qchanged) {
4242
12.7k
            vp8_update_rate_correction_factors(cpi, 0);
4243
12.7k
          }
4244
4245
12.7k
          Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4246
4247
          /* Special case reset for qlow for constrained quality.
4248
           * This should only trigger where there is very substantial
4249
           * undershoot on a frame and the auto cq level is above
4250
           * the user passsed in value.
4251
           */
4252
12.7k
          if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
4253
3.89k
              (Q < q_low)) {
4254
0
            q_low = Q;
4255
0
          }
4256
4257
20.3k
          while (((Q > q_high) || (cpi->mb.zbin_over_quant > zbin_oq_high)) &&
4258
8.33k
                 (Retries < 10)) {
4259
7.62k
            vp8_update_rate_correction_factors(cpi, 0);
4260
7.62k
            Q = vp8_regulate_q(cpi, cpi->this_frame_target);
4261
7.62k
            Retries++;
4262
7.62k
          }
4263
12.7k
        }
4264
4265
13.6k
        undershoot_seen = 1;
4266
13.6k
      }
4267
4268
      /* Clamp Q to upper and lower limits: */
4269
30.7k
      if (Q > q_high) {
4270
561
        Q = q_high;
4271
30.2k
      } else if (Q < q_low) {
4272
0
        Q = q_low;
4273
0
      }
4274
4275
      /* Clamp cpi->zbin_over_quant */
4276
30.7k
      cpi->mb.zbin_over_quant =
4277
30.7k
          (cpi->mb.zbin_over_quant < zbin_oq_low)    ? zbin_oq_low
4278
30.7k
          : (cpi->mb.zbin_over_quant > zbin_oq_high) ? zbin_oq_high
4279
30.7k
                                                     : cpi->mb.zbin_over_quant;
4280
4281
30.7k
      Loop = Q != last_q;
4282
108k
    } else {
4283
108k
      Loop = 0;
4284
108k
    }
4285
139k
#endif  // CONFIG_REALTIME_ONLY
4286
4287
139k
    if (cpi->is_src_frame_alt_ref) Loop = 0;
4288
4289
139k
    if (Loop == 1) {
4290
28.5k
      vp8_restore_coding_context(cpi);
4291
#if CONFIG_INTERNAL_STATS
4292
      cpi->tot_recode_hits++;
4293
#endif
4294
28.5k
    }
4295
142k
  } while (Loop == 1);
4296
4297
#if defined(DROP_UNCODED_FRAMES)
4298
  /* if there are no coded macroblocks at all drop this frame */
4299
  if (cpi->common.MBs == cpi->mb.skip_true_count &&
4300
      (cpi->drop_frame_count & 7) != 7 && cm->frame_type != KEY_FRAME) {
4301
    cpi->common.current_video_frame++;
4302
    cpi->frames_since_key++;
4303
    cpi->drop_frame_count++;
4304
    cpi->ext_refresh_frame_flags_pending = 0;
4305
    // We advance the temporal pattern for dropped frames.
4306
    cpi->temporal_pattern_counter++;
4307
    return;
4308
  }
4309
  cpi->drop_frame_count = 0;
4310
#endif
4311
4312
#if 0
4313
    /* Experimental code for lagged and one pass
4314
     * Update stats used for one pass GF selection
4315
     */
4316
    {
4317
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
4318
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
4319
        cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
4320
    }
4321
#endif
4322
4323
  /* Special case code to reduce pulsing when key frames are forced at a
4324
   * fixed interval. Note the reconstruction error if it is the frame before
4325
   * the force key frame
4326
   */
4327
110k
  if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) {
4328
0
    cpi->ambient_err =
4329
0
        vp8_calc_ss_err(cpi->Source, &cm->yv12_fb[cm->new_fb_idx]);
4330
0
  }
4331
4332
/* This frame's MVs are saved and will be used in next frame's MV predictor.
4333
 * Last frame has one more line(add to bottom) and one more column(add to
4334
 * right) than cm->mip. The edge elements are initialized to 0.
4335
 */
4336
#if CONFIG_MULTI_RES_ENCODING
4337
  if (!cpi->oxcf.mr_encoder_id && cm->show_frame)
4338
#else
4339
110k
  if (cm->show_frame) /* do not save for altref frame */
4340
110k
#endif
4341
110k
  {
4342
110k
    int mb_row;
4343
110k
    int mb_col;
4344
    /* Point to beginning of allocated MODE_INFO arrays. */
4345
110k
    MODE_INFO *tmp = cm->mip;
4346
4347
110k
    if (cm->frame_type != KEY_FRAME) {
4348
397k
      for (mb_row = 0; mb_row < cm->mb_rows + 1; ++mb_row) {
4349
1.77M
        for (mb_col = 0; mb_col < cm->mb_cols + 1; ++mb_col) {
4350
1.46M
          if (tmp->mbmi.ref_frame != INTRA_FRAME) {
4351
428k
            cpi->lfmv[mb_col + mb_row * (cm->mode_info_stride + 1)].as_int =
4352
428k
                tmp->mbmi.mv.as_int;
4353
428k
          }
4354
4355
1.46M
          cpi->lf_ref_frame_sign_bias[mb_col +
4356
1.46M
                                      mb_row * (cm->mode_info_stride + 1)] =
4357
1.46M
              cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
4358
1.46M
          cpi->lf_ref_frame[mb_col + mb_row * (cm->mode_info_stride + 1)] =
4359
1.46M
              tmp->mbmi.ref_frame;
4360
1.46M
          tmp++;
4361
1.46M
        }
4362
305k
      }
4363
91.7k
    }
4364
110k
  }
4365
4366
  /* Count last ref frame 0,0 usage on current encoded frame. */
4367
110k
  {
4368
110k
    int mb_row;
4369
110k
    int mb_col;
4370
    /* Point to beginning of MODE_INFO arrays. */
4371
110k
    MODE_INFO *tmp = cm->mi;
4372
4373
110k
    cpi->zeromv_count = 0;
4374
4375
110k
    if (cm->frame_type != KEY_FRAME) {
4376
305k
      for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
4377
1.09M
        for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
4378
877k
          if (tmp->mbmi.mode == ZEROMV && tmp->mbmi.ref_frame == LAST_FRAME) {
4379
128k
            cpi->zeromv_count++;
4380
128k
          }
4381
877k
          tmp++;
4382
877k
        }
4383
213k
        tmp++;
4384
213k
      }
4385
91.7k
    }
4386
110k
  }
4387
4388
#if CONFIG_MULTI_RES_ENCODING
4389
  vp8_cal_dissimilarity(cpi);
4390
#endif
4391
4392
  /* Update the GF usage maps.
4393
   * This is done after completing the compression of a frame when all
4394
   * modes etc. are finalized but before loop filter
4395
   */
4396
110k
  if (cpi->oxcf.number_of_layers == 1) {
4397
110k
    vp8_update_gf_usage_maps(cpi, cm, &cpi->mb);
4398
110k
  }
4399
4400
110k
  if (cm->frame_type == KEY_FRAME) cm->refresh_last_frame = 1;
4401
4402
#if 0
4403
    {
4404
        FILE *f = fopen("gfactive.stt", "a");
4405
        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);
4406
        fclose(f);
4407
    }
4408
#endif
4409
4410
  /* For inter frames the current default behavior is that when
4411
   * cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
4412
   * This is purely an encoder decision at present.
4413
   * Avoid this behavior when refresh flags are set by the user.
4414
   */
4415
110k
  if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame &&
4416
27.4k
      !cpi->ext_refresh_frame_flags_pending) {
4417
27.4k
    cm->copy_buffer_to_arf = 2;
4418
83.4k
  } else {
4419
83.4k
    cm->copy_buffer_to_arf = 0;
4420
83.4k
  }
4421
4422
110k
  cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
4423
4424
110k
#if CONFIG_TEMPORAL_DENOISING
4425
  // Get some measure of the amount of noise, by measuring the (partial) mse
4426
  // between source and denoised buffer, for y channel. Partial refers to
4427
  // computing the sse for a sub-sample of the frame (i.e., skip x blocks along
4428
  // row/column),
4429
  // and only for blocks in that set that are consecutive ZEROMV_LAST mode.
4430
  // Do this every ~8 frames, to further reduce complexity.
4431
  // TODO(marpan): Keep this for now for the case cpi->oxcf.noise_sensitivity <
4432
  // 4,
4433
  // should be removed in favor of the process_denoiser_mode_change() function
4434
  // below.
4435
110k
  if (cpi->oxcf.noise_sensitivity > 0 && cpi->oxcf.noise_sensitivity < 4 &&
4436
0
      !cpi->oxcf.screen_content_mode && cpi->frames_since_key % 8 == 0 &&
4437
0
      cm->frame_type != KEY_FRAME) {
4438
0
    cpi->mse_source_denoised = measure_square_diff_partial(
4439
0
        &cpi->denoiser.yv12_running_avg[INTRA_FRAME], cpi->Source, cpi);
4440
0
  }
4441
4442
  // For the adaptive denoising mode (noise_sensitivity == 4), sample the mse
4443
  // of source diff (between current and previous frame), and determine if we
4444
  // should switch the denoiser mode. Sampling refers to computing the mse for
4445
  // a sub-sample of the frame (i.e., skip x blocks along row/column), and
4446
  // only for blocks in that set that have used ZEROMV LAST, along with some
4447
  // constraint on the sum diff between blocks. This process is called every
4448
  // ~8 frames, to further reduce complexity.
4449
110k
  if (cpi->oxcf.noise_sensitivity == 4 && !cpi->oxcf.screen_content_mode &&
4450
0
      cpi->frames_since_key % 8 == 0 && cm->frame_type != KEY_FRAME) {
4451
0
    process_denoiser_mode_change(cpi);
4452
0
  }
4453
110k
#endif
4454
4455
#ifdef OUTPUT_YUV_SKINMAP
4456
  if (cpi->common.current_video_frame > 1) {
4457
    vp8_compute_skin_map(cpi, yuv_skinmap_file);
4458
  }
4459
#endif
4460
4461
110k
#if CONFIG_MULTITHREAD
4462
110k
  if (vpx_atomic_load_acquire(&cpi->b_multi_threaded)) {
4463
    /* start loopfilter in separate thread */
4464
0
    vp8_sem_post(&cpi->h_event_start_lpf);
4465
0
    cpi->b_lpf_running = 1;
4466
    /* wait for the filter_level to be picked so that we can continue with
4467
     * stream packing */
4468
0
    vp8_sem_wait(&cpi->h_event_end_lpf);
4469
0
  } else
4470
110k
#endif
4471
110k
  {
4472
110k
    vp8_loopfilter_frame(cpi, cm);
4473
110k
  }
4474
4475
110k
  update_reference_frames(cpi);
4476
4477
#ifdef OUTPUT_YUV_DENOISED
4478
  vpx_write_yuv_frame(yuv_denoised_file,
4479
                      &cpi->denoiser.yv12_running_avg[INTRA_FRAME]);
4480
#endif
4481
4482
110k
#if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
4483
110k
  if (cpi->oxcf.error_resilient_mode) {
4484
0
    cm->refresh_entropy_probs = 0;
4485
0
  }
4486
110k
#endif
4487
4488
  /* build the bitstream */
4489
110k
  vp8_pack_bitstream(cpi, dest, dest_end, size);
4490
4491
  /* Move storing frame_type out of the above loop since it is also
4492
   * needed in motion search besides loopfilter */
4493
110k
  cm->last_frame_type = cm->frame_type;
4494
4495
  /* Update rate control heuristics */
4496
110k
  cpi->total_byte_count += (*size);
4497
110k
  cpi->projected_frame_size = (int)(*size) << 3;
4498
4499
110k
  if (cpi->oxcf.number_of_layers > 1) {
4500
0
    unsigned int i;
4501
0
    for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
4502
0
      cpi->layer_context[i].total_byte_count += (*size);
4503
0
    }
4504
0
  }
4505
4506
110k
  if (!active_worst_qchanged) vp8_update_rate_correction_factors(cpi, 2);
4507
4508
110k
  cpi->last_q[cm->frame_type] = cm->base_qindex;
4509
4510
110k
  if (cm->frame_type == KEY_FRAME) {
4511
19.1k
    vp8_adjust_key_frame_context(cpi);
4512
19.1k
  }
4513
4514
  /* Keep a record of ambient average Q. */
4515
110k
  if (cm->frame_type != KEY_FRAME) {
4516
91.7k
    cpi->avg_frame_qindex =
4517
91.7k
        (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
4518
91.7k
  }
4519
4520
  /* Keep a record from which we can calculate the average Q excluding
4521
   * GF updates and key frames
4522
   */
4523
110k
  if ((cm->frame_type != KEY_FRAME) &&
4524
91.7k
      ((cpi->oxcf.number_of_layers > 1) ||
4525
91.7k
       (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame))) {
4526
83.4k
    cpi->ni_frames++;
4527
4528
    /* Calculate the average Q for normal inter frames (not key or GFU
4529
     * frames).
4530
     */
4531
83.4k
    if (cpi->pass == 2) {
4532
0
      cpi->ni_tot_qi += Q;
4533
0
      cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4534
83.4k
    } else {
4535
      /* Damp value for first few frames */
4536
83.4k
      if (cpi->ni_frames > 150) {
4537
9.06k
        cpi->ni_tot_qi += Q;
4538
9.06k
        cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
4539
9.06k
      }
4540
      /* For one pass, early in the clip ... average the current frame Q
4541
       * value with the worstq entered by the user as a dampening measure
4542
       */
4543
74.3k
      else {
4544
74.3k
        cpi->ni_tot_qi += Q;
4545
74.3k
        cpi->ni_av_qi =
4546
74.3k
            ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
4547
74.3k
      }
4548
4549
      /* If the average Q is higher than what was used in the last
4550
       * frame (after going through the recode loop to keep the frame
4551
       * size within range) then use the last frame value - 1. The -1
4552
       * is designed to stop Q and hence the data rate, from
4553
       * progressively falling away during difficult sections, but at
4554
       * the same time reduce the number of iterations around the
4555
       * recode loop.
4556
       */
4557
83.4k
      if (Q > cpi->ni_av_qi) cpi->ni_av_qi = Q - 1;
4558
83.4k
    }
4559
83.4k
  }
4560
4561
  /* Update the buffer level variable. */
4562
  /* Non-viewable frames are a special case and are treated as pure overhead. */
4563
110k
  if (!cm->show_frame) {
4564
0
    cpi->bits_off_target -= cpi->projected_frame_size;
4565
110k
  } else {
4566
110k
    cpi->bits_off_target +=
4567
110k
        cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
4568
110k
  }
4569
4570
  /* Clip the buffer level to the maximum specified buffer size */
4571
110k
  if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) {
4572
6.58k
    cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
4573
6.58k
  }
4574
4575
  // Don't let the buffer level go below some threshold, given here
4576
  // by -|maximum_buffer_size|. For now we only do this for
4577
  // screen content input.
4578
110k
  if (cpi->oxcf.screen_content_mode &&
4579
0
      cpi->bits_off_target < -cpi->oxcf.maximum_buffer_size) {
4580
0
    cpi->bits_off_target = -cpi->oxcf.maximum_buffer_size;
4581
0
  }
4582
4583
  /* Rolling monitors of whether we are over or underspending used to
4584
   * help regulate min and Max Q in two pass.
4585
   */
4586
110k
  cpi->rolling_target_bits = (int)ROUND64_POWER_OF_TWO(
4587
110k
      (int64_t)cpi->rolling_target_bits * 3 + cpi->this_frame_target, 2);
4588
110k
  cpi->rolling_actual_bits = (int)ROUND64_POWER_OF_TWO(
4589
110k
      (int64_t)cpi->rolling_actual_bits * 3 + cpi->projected_frame_size, 2);
4590
110k
  cpi->long_rolling_target_bits = (int)ROUND64_POWER_OF_TWO(
4591
110k
      (int64_t)cpi->long_rolling_target_bits * 31 + cpi->this_frame_target, 5);
4592
110k
  cpi->long_rolling_actual_bits = (int)ROUND64_POWER_OF_TWO(
4593
110k
      (int64_t)cpi->long_rolling_actual_bits * 31 + cpi->projected_frame_size,
4594
110k
      5);
4595
4596
  /* Actual bits spent */
4597
110k
  cpi->total_actual_bits += cpi->projected_frame_size;
4598
4599
#if 0 && CONFIG_INTERNAL_STATS
4600
  /* Debug stats */
4601
  cpi->total_target_vs_actual +=
4602
      (cpi->this_frame_target - cpi->projected_frame_size);
4603
#endif
4604
4605
110k
  cpi->buffer_level = cpi->bits_off_target;
4606
4607
  /* Propagate values to higher temporal layers */
4608
110k
  if (cpi->oxcf.number_of_layers > 1) {
4609
0
    unsigned int i;
4610
4611
0
    for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; ++i) {
4612
0
      LAYER_CONTEXT *lc = &cpi->layer_context[i];
4613
0
      int bits_off_for_this_layer = (int)round(
4614
0
          lc->target_bandwidth / lc->framerate - cpi->projected_frame_size);
4615
4616
0
      lc->bits_off_target += bits_off_for_this_layer;
4617
4618
      /* Clip buffer level to maximum buffer size for the layer */
4619
0
      if (lc->bits_off_target > lc->maximum_buffer_size) {
4620
0
        lc->bits_off_target = lc->maximum_buffer_size;
4621
0
      }
4622
4623
0
      lc->total_actual_bits += cpi->projected_frame_size;
4624
0
      lc->total_target_vs_actual += bits_off_for_this_layer;
4625
0
      lc->buffer_level = lc->bits_off_target;
4626
0
    }
4627
0
  }
4628
4629
  /* Update bits left to the kf and gf groups to account for overshoot
4630
   * or undershoot on these frames
4631
   */
4632
110k
  if (cm->frame_type == KEY_FRAME) {
4633
19.1k
    cpi->twopass.kf_group_bits +=
4634
19.1k
        cpi->this_frame_target - cpi->projected_frame_size;
4635
4636
19.1k
    if (cpi->twopass.kf_group_bits < 0) cpi->twopass.kf_group_bits = 0;
4637
91.7k
  } else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame) {
4638
8.29k
    cpi->twopass.gf_group_bits +=
4639
8.29k
        cpi->this_frame_target - cpi->projected_frame_size;
4640
4641
8.29k
    if (cpi->twopass.gf_group_bits < 0) cpi->twopass.gf_group_bits = 0;
4642
8.29k
  }
4643
4644
110k
  if (cm->frame_type != KEY_FRAME) {
4645
91.7k
    if (cpi->common.refresh_alt_ref_frame) {
4646
0
      cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
4647
0
      cpi->last_skip_probs_q[2] = cm->base_qindex;
4648
91.7k
    } else if (cpi->common.refresh_golden_frame) {
4649
8.29k
      cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
4650
8.29k
      cpi->last_skip_probs_q[1] = cm->base_qindex;
4651
83.4k
    } else {
4652
83.4k
      cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
4653
83.4k
      cpi->last_skip_probs_q[0] = cm->base_qindex;
4654
4655
      /* update the baseline */
4656
83.4k
      cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
4657
83.4k
    }
4658
91.7k
  }
4659
4660
#if 0 && CONFIG_INTERNAL_STATS
4661
    {
4662
        FILE *f = fopen("tmp.stt", "a");
4663
4664
        vpx_clear_system_state();
4665
4666
        if (cpi->twopass.total_left_stats.coded_error != 0.0)
4667
            fprintf(f, "%10d %10d %10d %10d %10d %10"PRId64" %10"PRId64
4668
                       "%10"PRId64" %10d %6d %6d %6d %6d %5d %5d %5d %8d "
4669
                       "%8.2lf %"PRId64" %10.3lf %10"PRId64" %8d\n",
4670
                       cpi->common.current_video_frame, cpi->this_frame_target,
4671
                       cpi->projected_frame_size,
4672
                       (cpi->projected_frame_size - cpi->this_frame_target),
4673
                       cpi->total_target_vs_actual,
4674
                       cpi->buffer_level,
4675
                       (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4676
                       cpi->total_actual_bits, cm->base_qindex,
4677
                       cpi->active_best_quality, cpi->active_worst_quality,
4678
                       cpi->ni_av_qi, cpi->cq_target_quality,
4679
                       cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4680
                       cm->frame_type, cpi->gfu_boost,
4681
                       cpi->twopass.est_max_qcorrection_factor,
4682
                       cpi->twopass.bits_left,
4683
                       cpi->twopass.total_left_stats.coded_error,
4684
                       (double)cpi->twopass.bits_left /
4685
                           cpi->twopass.total_left_stats.coded_error,
4686
                       cpi->tot_recode_hits);
4687
        else
4688
            fprintf(f, "%10d %10d %10d %10d %10d %10"PRId64" %10"PRId64
4689
                       "%10"PRId64" %10d %6d %6d %6d %6d %5d %5d %5d %8d "
4690
                       "%8.2lf %"PRId64" %10.3lf %8d\n",
4691
                       cpi->common.current_video_frame, cpi->this_frame_target,
4692
                       cpi->projected_frame_size,
4693
                       (cpi->projected_frame_size - cpi->this_frame_target),
4694
                       cpi->total_target_vs_actual,
4695
                       cpi->buffer_level,
4696
                       (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
4697
                       cpi->total_actual_bits, cm->base_qindex,
4698
                       cpi->active_best_quality, cpi->active_worst_quality,
4699
                       cpi->ni_av_qi, cpi->cq_target_quality,
4700
                       cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
4701
                       cm->frame_type, cpi->gfu_boost,
4702
                       cpi->twopass.est_max_qcorrection_factor,
4703
                       cpi->twopass.bits_left,
4704
                       cpi->twopass.total_left_stats.coded_error,
4705
                       cpi->tot_recode_hits);
4706
4707
        fclose(f);
4708
4709
        {
4710
            FILE *fmodes = fopen("Modes.stt", "a");
4711
4712
            fprintf(fmodes, "%6d:%1d:%1d:%1d ",
4713
                        cpi->common.current_video_frame,
4714
                        cm->frame_type, cm->refresh_golden_frame,
4715
                        cm->refresh_alt_ref_frame);
4716
4717
            fprintf(fmodes, "\n");
4718
4719
            fclose(fmodes);
4720
        }
4721
    }
4722
4723
#endif
4724
4725
110k
  cpi->ext_refresh_frame_flags_pending = 0;
4726
4727
110k
  if (cm->refresh_golden_frame == 1) {
4728
27.4k
    cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
4729
83.4k
  } else {
4730
83.4k
    cm->frame_flags = cm->frame_flags & ~FRAMEFLAGS_GOLDEN;
4731
83.4k
  }
4732
4733
110k
  if (cm->refresh_alt_ref_frame == 1) {
4734
19.1k
    cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
4735
91.7k
  } else {
4736
91.7k
    cm->frame_flags = cm->frame_flags & ~FRAMEFLAGS_ALTREF;
4737
91.7k
  }
4738
4739
110k
  if (cm->refresh_last_frame & cm->refresh_golden_frame) { /* both refreshed */
4740
27.4k
    cpi->gold_is_last = 1;
4741
83.4k
  } else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) {
4742
    /* 1 refreshed but not the other */
4743
83.4k
    cpi->gold_is_last = 0;
4744
83.4k
  }
4745
4746
110k
  if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) { /* both refreshed */
4747
19.1k
    cpi->alt_is_last = 1;
4748
91.7k
  } else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) {
4749
    /* 1 refreshed but not the other */
4750
91.7k
    cpi->alt_is_last = 0;
4751
91.7k
  }
4752
4753
110k
  if (cm->refresh_alt_ref_frame &
4754
110k
      cm->refresh_golden_frame) { /* both refreshed */
4755
19.1k
    cpi->gold_is_alt = 1;
4756
91.7k
  } else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) {
4757
    /* 1 refreshed but not the other */
4758
8.29k
    cpi->gold_is_alt = 0;
4759
8.29k
  }
4760
4761
110k
  cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
4762
4763
110k
  if (cpi->gold_is_last) cpi->ref_frame_flags &= ~VP8_GOLD_FRAME;
4764
4765
110k
  if (cpi->alt_is_last) cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4766
4767
110k
  if (cpi->gold_is_alt) cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
4768
4769
110k
  if (!cpi->oxcf.error_resilient_mode) {
4770
110k
    if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame &&
4771
0
        (cm->frame_type != KEY_FRAME)) {
4772
      /* Update the alternate reference frame stats as appropriate. */
4773
0
      update_alt_ref_frame_stats(cpi);
4774
110k
    } else {
4775
      /* Update the Golden frame stats as appropriate. */
4776
110k
      update_golden_frame_stats(cpi);
4777
110k
    }
4778
110k
  }
4779
4780
110k
  if (cm->frame_type == KEY_FRAME) {
4781
    /* Tell the caller that the frame was coded as a key frame */
4782
19.1k
    *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
4783
4784
    /* As this frame is a key frame  the next defaults to an inter frame. */
4785
19.1k
    cm->frame_type = INTER_FRAME;
4786
4787
19.1k
    cpi->last_frame_percent_intra = 100;
4788
91.7k
  } else {
4789
91.7k
    *frame_flags = cm->frame_flags & ~FRAMEFLAGS_KEY;
4790
4791
91.7k
    cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
4792
91.7k
  }
4793
4794
  /* Clear the one shot update flags for segmentation map and mode/ref
4795
   * loop filter deltas.
4796
   */
4797
110k
  cpi->mb.e_mbd.update_mb_segmentation_map = 0;
4798
110k
  cpi->mb.e_mbd.update_mb_segmentation_data = 0;
4799
110k
  cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
4800
4801
  /* Don't increment frame counters if this was an altref buffer update
4802
   * not a real frame
4803
   */
4804
110k
  if (cm->show_frame) {
4805
110k
    cm->current_video_frame++;
4806
110k
    cpi->frames_since_key++;
4807
110k
    cpi->temporal_pattern_counter++;
4808
110k
  }
4809
4810
#if 0
4811
    {
4812
        char filename[512];
4813
        FILE *recon_file;
4814
        sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
4815
        recon_file = fopen(filename, "wb");
4816
        fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
4817
               cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
4818
        fclose(recon_file);
4819
    }
4820
#endif
4821
4822
  /* DEBUG */
4823
  /* vpx_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show); */
4824
110k
}
4825
#if !CONFIG_REALTIME_ONLY
4826
static void Pass2Encode(VP8_COMP *cpi, size_t *size, unsigned char *dest,
4827
0
                        unsigned char *dest_end, unsigned int *frame_flags) {
4828
0
  if (!cpi->common.refresh_alt_ref_frame) vp8_second_pass(cpi);
4829
4830
0
  encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
4831
0
  cpi->twopass.bits_left -= 8 * (int)(*size);
4832
4833
0
  if (!cpi->common.refresh_alt_ref_frame) {
4834
0
    double two_pass_min_rate =
4835
0
        (double)(cpi->oxcf.target_bandwidth *
4836
0
                 cpi->oxcf.two_pass_vbrmin_section / 100);
4837
0
    cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->framerate);
4838
0
  }
4839
0
}
4840
#endif
4841
4842
int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags,
4843
                          YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
4844
110k
                          int64_t end_time) {
4845
#if CONFIG_INTERNAL_STATS
4846
  struct vpx_usec_timer timer;
4847
#endif
4848
110k
  int res = 0;
4849
4850
#if CONFIG_INTERNAL_STATS
4851
  vpx_usec_timer_start(&timer);
4852
#endif
4853
4854
  /* Reinit the lookahead buffer if the frame size changes */
4855
110k
  if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height) {
4856
0
    assert(cpi->oxcf.lag_in_frames < 2);
4857
0
    dealloc_raw_frame_buffers(cpi);
4858
0
    alloc_raw_frame_buffers(cpi);
4859
0
  }
4860
4861
110k
  if (vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags,
4862
110k
                         cpi->active_map_enabled ? cpi->active_map : NULL)) {
4863
0
    res = -1;
4864
0
  }
4865
#if CONFIG_INTERNAL_STATS
4866
  vpx_usec_timer_mark(&timer);
4867
  cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
4868
#endif
4869
4870
110k
  return res;
4871
110k
}
4872
4873
110k
static int frame_is_reference(const VP8_COMP *cpi) {
4874
110k
  const VP8_COMMON *cm = &cpi->common;
4875
110k
  const MACROBLOCKD *xd = &cpi->mb.e_mbd;
4876
4877
110k
  return cm->frame_type == KEY_FRAME || cm->refresh_last_frame ||
4878
0
         cm->refresh_golden_frame || cm->refresh_alt_ref_frame ||
4879
0
         cm->copy_buffer_to_gf || cm->copy_buffer_to_arf ||
4880
0
         cm->refresh_entropy_probs || xd->mode_ref_lf_delta_update ||
4881
0
         xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
4882
110k
}
4883
4884
int vp8_get_compressed_data(VP8_COMP *cpi, unsigned int *frame_flags,
4885
                            size_t *size, unsigned char *dest,
4886
                            unsigned char *dest_end, int64_t *time_stamp,
4887
227k
                            int64_t *time_end, int flush) {
4888
227k
  VP8_COMMON *cm;
4889
227k
  struct vpx_usec_timer ticktimer;
4890
#if CONFIG_INTERNAL_STATS
4891
  struct vpx_usec_timer cmptimer;
4892
#endif
4893
227k
  YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4894
4895
227k
  if (!cpi) return -1;
4896
4897
227k
  cm = &cpi->common;
4898
4899
#if CONFIG_INTERNAL_STATS
4900
  vpx_usec_timer_start(&cmptimer);
4901
#endif
4902
4903
227k
  cpi->source = NULL;
4904
4905
227k
#if !CONFIG_REALTIME_ONLY
4906
  /* Should we code an alternate reference frame */
4907
227k
  if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.play_alternate &&
4908
0
      cpi->source_alt_ref_pending) {
4909
0
    if ((cpi->source = vp8_lookahead_peek(
4910
0
             cpi->lookahead, cpi->frames_till_gf_update_due, PEEK_FORWARD))) {
4911
0
      cpi->alt_ref_source = cpi->source;
4912
0
      if (cpi->oxcf.arnr_max_frames > 0) {
4913
0
        vp8_temporal_filter_prepare_c(cpi, cpi->frames_till_gf_update_due);
4914
0
        force_src_buffer = &cpi->alt_ref_buffer;
4915
0
      }
4916
0
      cpi->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
4917
0
      cm->refresh_alt_ref_frame = 1;
4918
0
      cm->refresh_golden_frame = 0;
4919
0
      cm->refresh_last_frame = 0;
4920
0
      cm->show_frame = 0;
4921
      /* Clear Pending alt Ref flag. */
4922
0
      cpi->source_alt_ref_pending = 0;
4923
0
      cpi->is_src_frame_alt_ref = 0;
4924
0
    }
4925
0
  }
4926
227k
#endif
4927
4928
227k
  if (!cpi->source) {
4929
    /* Read last frame source if we are encoding first pass. */
4930
227k
    if (cpi->pass == 1 && cm->current_video_frame > 0) {
4931
0
      if ((cpi->last_source =
4932
0
               vp8_lookahead_peek(cpi->lookahead, 1, PEEK_BACKWARD)) == NULL) {
4933
0
        return -1;
4934
0
      }
4935
0
    }
4936
4937
227k
    if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush))) {
4938
110k
      cm->show_frame = 1;
4939
4940
110k
      cpi->is_src_frame_alt_ref =
4941
110k
          cpi->alt_ref_source && (cpi->source == cpi->alt_ref_source);
4942
4943
110k
      if (cpi->is_src_frame_alt_ref) cpi->alt_ref_source = NULL;
4944
110k
    }
4945
227k
  }
4946
4947
227k
  if (cpi->source) {
4948
110k
    cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
4949
110k
    cpi->un_scaled_source = cpi->Source;
4950
110k
    *time_stamp = cpi->source->ts_start;
4951
110k
    *time_end = cpi->source->ts_end;
4952
110k
    *frame_flags = cpi->source->flags;
4953
4954
110k
    if (cpi->pass == 1 && cm->current_video_frame > 0) {
4955
0
      cpi->last_frame_unscaled_source = &cpi->last_source->img;
4956
0
    }
4957
116k
  } else {
4958
116k
    *size = 0;
4959
116k
#if !CONFIG_REALTIME_ONLY
4960
4961
116k
    if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
4962
0
      vp8_end_first_pass(cpi); /* get last stats packet */
4963
0
      cpi->twopass.first_pass_done = 1;
4964
0
    }
4965
4966
116k
#endif
4967
4968
116k
    return -1;
4969
116k
  }
4970
4971
110k
  if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
4972
5.73k
    cpi->first_time_stamp_ever = cpi->source->ts_start;
4973
5.73k
    cpi->last_end_time_stamp_seen = cpi->source->ts_start;
4974
5.73k
  }
4975
4976
  /* adjust frame rates based on timestamps given */
4977
110k
  if (cm->show_frame) {
4978
110k
    int64_t this_duration;
4979
110k
    int step = 0;
4980
4981
110k
    if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
4982
110k
      this_duration = cpi->source->ts_end - cpi->source->ts_start;
4983
110k
      step = 1;
4984
110k
    } else {
4985
0
      int64_t last_duration;
4986
4987
0
      this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
4988
0
      last_duration = cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
4989
      // Cap this to avoid overflow of (this_duration - last_duration) * 10
4990
0
      this_duration = VPXMIN(this_duration, INT64_MAX / 10);
4991
      /* do a step update if the duration changes by 10% */
4992
0
      if (last_duration) {
4993
0
        step = (int)(((this_duration - last_duration) * 10 / last_duration));
4994
0
      }
4995
0
    }
4996
4997
110k
    if (this_duration) {
4998
110k
      if (step) {
4999
110k
        cpi->ref_framerate = 10000000.0 / this_duration;
5000
110k
      } else {
5001
0
        double avg_duration, interval;
5002
5003
        /* Average this frame's rate into the last second's average
5004
         * frame rate. If we haven't seen 1 second yet, then average
5005
         * over the whole interval seen.
5006
         */
5007
0
        interval = (double)(cpi->source->ts_end - cpi->first_time_stamp_ever);
5008
0
        if (interval > 10000000.0) interval = 10000000;
5009
5010
0
        avg_duration = 10000000.0 / cpi->ref_framerate;
5011
0
        avg_duration *= (interval - avg_duration + this_duration);
5012
0
        avg_duration /= interval;
5013
5014
0
        cpi->ref_framerate = 10000000.0 / avg_duration;
5015
0
      }
5016
#if CONFIG_MULTI_RES_ENCODING
5017
      if (cpi->oxcf.mr_total_resolutions > 1) {
5018
        LOWER_RES_FRAME_INFO *low_res_frame_info =
5019
            (LOWER_RES_FRAME_INFO *)cpi->oxcf.mr_low_res_mode_info;
5020
        // Frame rate should be the same for all spatial layers in
5021
        // multi-res-encoding (simulcast), so we constrain the frame for
5022
        // higher layers to be that of lowest resolution. This is needed
5023
        // as he application may decide to skip encoding a high layer and
5024
        // then start again, in which case a big jump in time-stamps will
5025
        // be received for that high layer, which will yield an incorrect
5026
        // frame rate (from time-stamp adjustment in above calculation).
5027
        if (cpi->oxcf.mr_encoder_id) {
5028
          if (!low_res_frame_info->skip_encoding_base_stream)
5029
            cpi->ref_framerate = low_res_frame_info->low_res_framerate;
5030
        } else {
5031
          // Keep track of frame rate for lowest resolution.
5032
          low_res_frame_info->low_res_framerate = cpi->ref_framerate;
5033
          // The base stream is being encoded so set skip flag to 0.
5034
          low_res_frame_info->skip_encoding_base_stream = 0;
5035
        }
5036
      }
5037
#endif
5038
110k
      cpi->ref_framerate = clamp_framerate(cpi->ref_framerate);
5039
110k
      if (cpi->oxcf.number_of_layers > 1) {
5040
0
        unsigned int i;
5041
5042
        /* Update frame rates for each layer */
5043
0
        assert(cpi->oxcf.number_of_layers <= VPX_TS_MAX_LAYERS);
5044
0
        for (i = 0; i < cpi->oxcf.number_of_layers && i < VPX_TS_MAX_LAYERS;
5045
0
             ++i) {
5046
0
          LAYER_CONTEXT *lc = &cpi->layer_context[i];
5047
0
          lc->framerate = cpi->ref_framerate / cpi->oxcf.rate_decimator[i];
5048
0
        }
5049
110k
      } else {
5050
110k
        vp8_new_framerate(cpi, cpi->ref_framerate);
5051
110k
      }
5052
110k
    }
5053
5054
110k
    cpi->last_time_stamp_seen = cpi->source->ts_start;
5055
110k
    cpi->last_end_time_stamp_seen = cpi->source->ts_end;
5056
110k
  }
5057
5058
110k
  if (cpi->oxcf.number_of_layers > 1) {
5059
0
    int layer;
5060
5061
0
    vp8_update_layer_contexts(cpi);
5062
5063
    /* Restore layer specific context & set frame rate */
5064
0
    if (cpi->temporal_layer_id >= 0) {
5065
0
      layer = cpi->temporal_layer_id;
5066
0
    } else {
5067
0
      layer =
5068
0
          cpi->oxcf
5069
0
              .layer_id[cpi->temporal_pattern_counter % cpi->oxcf.periodicity];
5070
0
    }
5071
0
    vp8_restore_layer_context(cpi, layer);
5072
0
    vp8_new_framerate(cpi, cpi->layer_context[layer].framerate);
5073
0
  }
5074
5075
110k
  if (cpi->compressor_speed == 2) {
5076
57.3k
    vpx_usec_timer_start(&ticktimer);
5077
57.3k
  }
5078
5079
110k
  cpi->lf_zeromv_pct = (cpi->zeromv_count * 100) / cm->MBs;
5080
5081
#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
5082
  {
5083
    int i;
5084
    const int num_part = (1 << cm->multi_token_partition);
5085
    /* the available bytes in dest */
5086
    const unsigned long dest_size = dest_end - dest;
5087
    const int tok_part_buff_size = (dest_size * 9) / (10 * num_part);
5088
5089
    unsigned char *dp = dest;
5090
5091
    cpi->partition_d[0] = dp;
5092
    dp += dest_size / 10; /* reserve 1/10 for control partition */
5093
    cpi->partition_d_end[0] = dp;
5094
5095
    for (i = 0; i < num_part; ++i) {
5096
      cpi->partition_d[i + 1] = dp;
5097
      dp += tok_part_buff_size;
5098
      cpi->partition_d_end[i + 1] = dp;
5099
    }
5100
  }
5101
#endif
5102
5103
  /* start with a 0 size frame */
5104
110k
  *size = 0;
5105
5106
  /* Clear down mmx registers */
5107
110k
  vpx_clear_system_state();
5108
5109
110k
  cm->frame_type = INTER_FRAME;
5110
110k
  cm->frame_flags = *frame_flags;
5111
5112
#if 0
5113
5114
    if (cm->refresh_alt_ref_frame)
5115
    {
5116
        cm->refresh_golden_frame = 0;
5117
        cm->refresh_last_frame = 0;
5118
    }
5119
    else
5120
    {
5121
        cm->refresh_golden_frame = 0;
5122
        cm->refresh_last_frame = 1;
5123
    }
5124
5125
#endif
5126
  /* find a free buffer for the new frame */
5127
110k
  {
5128
110k
    int i = 0;
5129
263k
    for (; i < NUM_YV12_BUFFERS; ++i) {
5130
263k
      if (!cm->yv12_fb[i].flags) {
5131
110k
        cm->new_fb_idx = i;
5132
110k
        break;
5133
110k
      }
5134
263k
    }
5135
5136
110k
    assert(i < NUM_YV12_BUFFERS);
5137
110k
  }
5138
110k
  switch (cpi->pass) {
5139
0
#if !CONFIG_REALTIME_ONLY
5140
0
    case 1: Pass1Encode(cpi); break;
5141
0
    case 2: Pass2Encode(cpi, size, dest, dest_end, frame_flags); break;
5142
0
#endif  // !CONFIG_REALTIME_ONLY
5143
110k
    default:
5144
110k
      encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
5145
110k
      break;
5146
110k
  }
5147
5148
110k
  if (cpi->compressor_speed == 2) {
5149
57.3k
    unsigned int duration, duration2;
5150
57.3k
    vpx_usec_timer_mark(&ticktimer);
5151
5152
57.3k
    duration = (int)(vpx_usec_timer_elapsed(&ticktimer));
5153
57.3k
    duration2 = (unsigned int)((double)duration / 2);
5154
5155
57.3k
    if (cm->frame_type != KEY_FRAME) {
5156
57.3k
      if (cpi->avg_encode_time == 0) {
5157
56.9k
        cpi->avg_encode_time = duration;
5158
56.9k
      } else {
5159
302
        cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
5160
302
      }
5161
57.3k
    }
5162
5163
57.3k
    if (duration2) {
5164
57.3k
      {
5165
57.3k
        if (cpi->avg_pick_mode_time == 0) {
5166
56.9k
          cpi->avg_pick_mode_time = duration2;
5167
56.9k
        } else {
5168
302
          cpi->avg_pick_mode_time =
5169
302
              (7 * cpi->avg_pick_mode_time + duration2) >> 3;
5170
302
        }
5171
57.3k
      }
5172
57.3k
    }
5173
57.3k
  }
5174
5175
110k
  if (cm->refresh_entropy_probs == 0) {
5176
0
    cm->fc = cm->lfc;
5177
0
  }
5178
5179
  /* Save the contexts separately for alt ref, gold and last. */
5180
  /* (TODO jbb -> Optimize this with pointers to avoid extra copies. ) */
5181
110k
  if (cm->refresh_alt_ref_frame) cpi->lfc_a = cm->fc;
5182
5183
110k
  if (cm->refresh_golden_frame) cpi->lfc_g = cm->fc;
5184
5185
110k
  if (cm->refresh_last_frame) cpi->lfc_n = cm->fc;
5186
5187
  /* if it's a dropped frame honor the requests on subsequent frames */
5188
110k
  if (*size > 0) {
5189
110k
    cpi->droppable = !frame_is_reference(cpi);
5190
5191
    /* return to normal state */
5192
110k
    cm->refresh_entropy_probs = 1;
5193
110k
    cm->refresh_alt_ref_frame = 0;
5194
110k
    cm->refresh_golden_frame = 0;
5195
110k
    cm->refresh_last_frame = 1;
5196
110k
    cm->frame_type = INTER_FRAME;
5197
110k
  }
5198
5199
  /* Save layer specific state */
5200
110k
  if (cpi->oxcf.number_of_layers > 1) vp8_save_layer_context(cpi);
5201
5202
#if CONFIG_INTERNAL_STATS
5203
  vpx_usec_timer_mark(&cmptimer);
5204
  cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
5205
#endif
5206
5207
110k
#if CONFIG_MULTITHREAD
5208
  /* wait for the lpf thread done */
5209
110k
  if (vpx_atomic_load_acquire(&cpi->b_multi_threaded) && cpi->b_lpf_running) {
5210
0
    vp8_sem_wait(&cpi->h_event_end_lpf);
5211
0
    cpi->b_lpf_running = 0;
5212
0
  }
5213
110k
#endif
5214
5215
110k
  if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame) {
5216
0
    generate_psnr_packet(cpi);
5217
0
  }
5218
5219
#if CONFIG_INTERNAL_STATS
5220
5221
  if (cpi->pass != 1) {
5222
    cpi->bytes += *size;
5223
5224
    if (cm->show_frame) {
5225
      cpi->common.show_frame_mi = cpi->common.mi;
5226
      cpi->count++;
5227
5228
      if (cpi->b_calculate_psnr) {
5229
        uint64_t ye, ue, ve;
5230
        double frame_psnr;
5231
        YV12_BUFFER_CONFIG *orig = (cpi->oxcf.noise_sensitivity > 0 &&
5232
                                    cpi->raw_source_frame.buffer_alloc)
5233
                                       ? &cpi->raw_source_frame
5234
                                       : cpi->Source;
5235
        YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5236
        unsigned int y_width = cpi->common.Width;
5237
        unsigned int y_height = cpi->common.Height;
5238
        unsigned int uv_width = (y_width + 1) / 2;
5239
        unsigned int uv_height = (y_height + 1) / 2;
5240
        int y_samples = y_height * y_width;
5241
        int uv_samples = uv_height * uv_width;
5242
        int t_samples = y_samples + 2 * uv_samples;
5243
        double sq_error;
5244
5245
        ye = calc_plane_error(orig->y_buffer, orig->y_stride, recon->y_buffer,
5246
                              recon->y_stride, y_width, y_height);
5247
5248
        ue = calc_plane_error(orig->u_buffer, orig->uv_stride, recon->u_buffer,
5249
                              recon->uv_stride, uv_width, uv_height);
5250
5251
        ve = calc_plane_error(orig->v_buffer, orig->uv_stride, recon->v_buffer,
5252
                              recon->uv_stride, uv_width, uv_height);
5253
5254
        sq_error = (double)(ye + ue + ve);
5255
5256
        frame_psnr = vpx_sse_to_psnr(t_samples, 255.0, sq_error);
5257
5258
        cpi->total_y += vpx_sse_to_psnr(y_samples, 255.0, (double)ye);
5259
        cpi->total_u += vpx_sse_to_psnr(uv_samples, 255.0, (double)ue);
5260
        cpi->total_v += vpx_sse_to_psnr(uv_samples, 255.0, (double)ve);
5261
        cpi->total_sq_error += sq_error;
5262
        cpi->total += frame_psnr;
5263
#if CONFIG_POSTPROC
5264
        {
5265
          YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
5266
          double sq_error2;
5267
          double frame_psnr2, frame_ssim2 = 0;
5268
          double weight = 0;
5269
5270
          vp8_deblock(cm, cm->frame_to_show, &cm->post_proc_buffer,
5271
                      cm->filter_level * 10 / 6);
5272
          vpx_clear_system_state();
5273
5274
          ye = calc_plane_error(orig->y_buffer, orig->y_stride, pp->y_buffer,
5275
                                pp->y_stride, y_width, y_height);
5276
5277
          ue = calc_plane_error(orig->u_buffer, orig->uv_stride, pp->u_buffer,
5278
                                pp->uv_stride, uv_width, uv_height);
5279
5280
          ve = calc_plane_error(orig->v_buffer, orig->uv_stride, pp->v_buffer,
5281
                                pp->uv_stride, uv_width, uv_height);
5282
5283
          sq_error2 = (double)(ye + ue + ve);
5284
5285
          frame_psnr2 = vpx_sse_to_psnr(t_samples, 255.0, sq_error2);
5286
5287
          cpi->totalp_y += vpx_sse_to_psnr(y_samples, 255.0, (double)ye);
5288
          cpi->totalp_u += vpx_sse_to_psnr(uv_samples, 255.0, (double)ue);
5289
          cpi->totalp_v += vpx_sse_to_psnr(uv_samples, 255.0, (double)ve);
5290
          cpi->total_sq_error2 += sq_error2;
5291
          cpi->totalp += frame_psnr2;
5292
5293
          frame_ssim2 =
5294
              vpx_calc_ssim(cpi->Source, &cm->post_proc_buffer, &weight);
5295
5296
          cpi->summed_quality += frame_ssim2 * weight;
5297
          cpi->summed_weights += weight;
5298
5299
          if (cpi->oxcf.number_of_layers > 1) {
5300
            unsigned int i;
5301
5302
            for (i = cpi->current_layer; i < cpi->oxcf.number_of_layers; ++i) {
5303
              cpi->frames_in_layer[i]++;
5304
5305
              cpi->bytes_in_layer[i] += *size;
5306
              cpi->sum_psnr[i] += frame_psnr;
5307
              cpi->sum_psnr_p[i] += frame_psnr2;
5308
              cpi->total_error2[i] += sq_error;
5309
              cpi->total_error2_p[i] += sq_error2;
5310
              cpi->sum_ssim[i] += frame_ssim2 * weight;
5311
              cpi->sum_weights[i] += weight;
5312
            }
5313
          }
5314
        }
5315
#endif
5316
      }
5317
    }
5318
  }
5319
5320
#if 0
5321
5322
    if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
5323
    {
5324
        skiptruecount += cpi->skip_true_count;
5325
        skipfalsecount += cpi->skip_false_count;
5326
    }
5327
5328
#endif
5329
#if 0
5330
5331
    if (cpi->pass != 1)
5332
    {
5333
        FILE *f = fopen("skip.stt", "a");
5334
        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);
5335
5336
        if (cpi->is_src_frame_alt_ref == 1)
5337
            fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
5338
5339
        fclose(f);
5340
    }
5341
5342
#endif
5343
#endif
5344
5345
110k
  return 0;
5346
110k
}
5347
5348
int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest,
5349
0
                              vp8_ppflags_t *flags) {
5350
0
  if (cpi->common.refresh_alt_ref_frame) {
5351
0
    return -1;
5352
0
  } else {
5353
0
    int ret;
5354
5355
0
#if CONFIG_POSTPROC
5356
0
    cpi->common.show_frame_mi = cpi->common.mi;
5357
0
    ret = vp8_post_proc_frame(&cpi->common, dest, flags);
5358
#else
5359
    (void)flags;
5360
5361
    if (cpi->common.frame_to_show) {
5362
      *dest = *cpi->common.frame_to_show;
5363
      dest->y_width = cpi->common.Width;
5364
      dest->y_height = cpi->common.Height;
5365
      dest->uv_height = cpi->common.Height / 2;
5366
      ret = 0;
5367
    } else {
5368
      ret = -1;
5369
    }
5370
5371
#endif
5372
0
    vpx_clear_system_state();
5373
0
    return ret;
5374
0
  }
5375
0
}
5376
5377
int vp8_set_roimap(VP8_COMP *cpi, unsigned char *map, unsigned int rows,
5378
                   unsigned int cols, int delta_q[4], int delta_lf[4],
5379
0
                   unsigned int threshold[4]) {
5380
0
  signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
5381
0
  int internal_delta_q[MAX_MB_SEGMENTS];
5382
0
  const int range = 63;
5383
0
  int i;
5384
5385
  // Check number of rows and columns match
5386
0
  if (cpi->common.mb_rows != (int)rows || cpi->common.mb_cols != (int)cols) {
5387
0
    return -1;
5388
0
  }
5389
5390
0
  for (i = 0; i < MAX_MB_SEGMENTS; ++i) {
5391
    // Note abs() alone can't be used as the behavior of abs(INT_MIN) is
5392
    // undefined.
5393
0
    if (delta_q[i] > range || delta_q[i] < -range || delta_lf[i] > range ||
5394
0
        delta_lf[i] < -range) {
5395
0
      return -1;
5396
0
    }
5397
0
  }
5398
5399
  // Also disable segmentation if no deltas are specified.
5400
0
  if (!map || (delta_q[0] == 0 && delta_q[1] == 0 && delta_q[2] == 0 &&
5401
0
               delta_q[3] == 0 && delta_lf[0] == 0 && delta_lf[1] == 0 &&
5402
0
               delta_lf[2] == 0 && delta_lf[3] == 0 && threshold[0] == 0 &&
5403
0
               threshold[1] == 0 && threshold[2] == 0 && threshold[3] == 0)) {
5404
0
    disable_segmentation(cpi);
5405
0
    return 0;
5406
0
  }
5407
5408
  // Translate the external delta q values to internal values.
5409
0
  for (i = 0; i < MAX_MB_SEGMENTS; ++i) {
5410
0
    internal_delta_q[i] =
5411
0
        (delta_q[i] >= 0) ? q_trans[delta_q[i]] : -q_trans[-delta_q[i]];
5412
0
  }
5413
5414
  /* Set the segmentation Map */
5415
0
  set_segmentation_map(cpi, map);
5416
5417
  /* Activate segmentation. */
5418
0
  enable_segmentation(cpi);
5419
5420
  /* Set up the quant segment data */
5421
0
  feature_data[MB_LVL_ALT_Q][0] = internal_delta_q[0];
5422
0
  feature_data[MB_LVL_ALT_Q][1] = internal_delta_q[1];
5423
0
  feature_data[MB_LVL_ALT_Q][2] = internal_delta_q[2];
5424
0
  feature_data[MB_LVL_ALT_Q][3] = internal_delta_q[3];
5425
5426
  /* Set up the loop segment data s */
5427
0
  feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
5428
0
  feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
5429
0
  feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
5430
0
  feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
5431
5432
0
  cpi->segment_encode_breakout[0] = threshold[0];
5433
0
  cpi->segment_encode_breakout[1] = threshold[1];
5434
0
  cpi->segment_encode_breakout[2] = threshold[2];
5435
0
  cpi->segment_encode_breakout[3] = threshold[3];
5436
5437
  /* Initialise the feature data structure */
5438
0
  set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
5439
5440
0
  if (threshold[0] != 0 || threshold[1] != 0 || threshold[2] != 0 ||
5441
0
      threshold[3] != 0)
5442
0
    cpi->use_roi_static_threshold = 1;
5443
0
  cpi->cyclic_refresh_mode_enabled = 0;
5444
5445
0
  return 0;
5446
0
}
5447
5448
int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows,
5449
0
                       unsigned int cols) {
5450
0
  if ((int)rows == cpi->common.mb_rows && (int)cols == cpi->common.mb_cols) {
5451
0
    if (map) {
5452
0
      memcpy(cpi->active_map, map, rows * cols);
5453
0
      cpi->active_map_enabled = 1;
5454
0
    } else {
5455
0
      cpi->active_map_enabled = 0;
5456
0
    }
5457
5458
0
    return 0;
5459
0
  } else {
5460
0
    return -1;
5461
0
  }
5462
0
}
5463
5464
int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING_MODE horiz_mode,
5465
0
                          VPX_SCALING_MODE vert_mode) {
5466
0
  if (horiz_mode <= VP8E_ONETWO) {
5467
0
    cpi->common.horiz_scale = horiz_mode;
5468
0
  } else {
5469
0
    return -1;
5470
0
  }
5471
5472
0
  if (vert_mode <= VP8E_ONETWO) {
5473
0
    cpi->common.vert_scale = vert_mode;
5474
0
  } else {
5475
0
    return -1;
5476
0
  }
5477
5478
0
  return 0;
5479
0
}
5480
5481
738k
int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) {
5482
738k
  int i, j;
5483
738k
  int Total = 0;
5484
5485
738k
  unsigned char *src = source->y_buffer;
5486
738k
  unsigned char *dst = dest->y_buffer;
5487
5488
  /* Loop through the Y plane raw and reconstruction data summing
5489
   * (square differences)
5490
   */
5491
2.63M
  for (i = 0; i < source->y_height; i += 16) {
5492
10.2M
    for (j = 0; j < source->y_width; j += 16) {
5493
8.40M
      unsigned int sse;
5494
8.40M
      Total += vpx_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
5495
8.40M
                            &sse);
5496
8.40M
    }
5497
5498
1.89M
    src += 16 * source->y_stride;
5499
1.89M
    dst += 16 * dest->y_stride;
5500
1.89M
  }
5501
5502
738k
  return Total;
5503
738k
}
5504
5505
110k
int vp8_get_quantizer(VP8_COMP *cpi) { return cpi->common.base_qindex; }