Coverage Report

Created: 2026-07-25 07:52

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