Coverage Report

Created: 2026-05-23 07:06

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