Coverage Report

Created: 2026-05-16 07:49

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