Coverage Report

Created: 2025-08-28 07:12

/src/libvpx/vp9/encoder/vp9_encoder.c
Line
Count
Source (jump to first uncovered line)
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 <limits.h>
12
#include <math.h>
13
#include <stdint.h>
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include "./vp9_rtcd.h"
19
#include "./vpx_config.h"
20
#include "./vpx_dsp_rtcd.h"
21
#include "./vpx_scale_rtcd.h"
22
#include "vpx/vpx_codec.h"
23
#include "vpx/vpx_ext_ratectrl.h"
24
#include "vpx_dsp/psnr.h"
25
#include "vpx_dsp/vpx_dsp_common.h"
26
#include "vpx_dsp/vpx_filter.h"
27
#if CONFIG_INTERNAL_STATS
28
#include "vpx_dsp/ssim.h"
29
#endif
30
#include "vpx_mem/vpx_mem.h"
31
#include "vpx_ports/mem.h"
32
#include "vpx_ports/system_state.h"
33
#include "vpx_ports/vpx_once.h"
34
#include "vpx_ports/vpx_timer.h"
35
#include "vpx_util/vpx_pthread.h"
36
#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
37
#include "vpx_util/vpx_debug_util.h"
38
#endif  // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
39
40
#include "vp9/common/vp9_alloccommon.h"
41
#include "vp9/common/vp9_blockd.h"
42
#include "vp9/common/vp9_enums.h"
43
#include "vp9/common/vp9_filter.h"
44
#include "vp9/common/vp9_idct.h"
45
#if CONFIG_VP9_POSTPROC
46
#include "vp9/common/vp9_postproc.h"
47
#endif
48
#include "vp9/common/vp9_reconinter.h"
49
#include "vp9/common/vp9_reconintra.h"
50
#include "vp9/common/vp9_scale.h"
51
#include "vp9/common/vp9_tile_common.h"
52
53
#if !CONFIG_REALTIME_ONLY
54
#include "vp9/encoder/vp9_alt_ref_aq.h"
55
#include "vp9/encoder/vp9_aq_360.h"
56
#include "vp9/encoder/vp9_aq_complexity.h"
57
#endif
58
#include "vp9/encoder/vp9_aq_cyclicrefresh.h"
59
#if !CONFIG_REALTIME_ONLY
60
#include "vp9/encoder/vp9_aq_variance.h"
61
#endif
62
#include "vp9/encoder/vp9_bitstream.h"
63
#if CONFIG_INTERNAL_STATS
64
#include "vp9/encoder/vp9_blockiness.h"
65
#endif
66
#include "vp9/encoder/vp9_context_tree.h"
67
#include "vp9/encoder/vp9_encodeframe.h"
68
#include "vp9/encoder/vp9_encodemb.h"
69
#include "vp9/encoder/vp9_encodemv.h"
70
#include "vp9/encoder/vp9_encoder.h"
71
#include "vp9/encoder/vp9_ethread.h"
72
#include "vp9/encoder/vp9_extend.h"
73
#include "vp9/encoder/vp9_firstpass.h"
74
#include "vp9/encoder/vp9_mbgraph.h"
75
#if CONFIG_NON_GREEDY_MV
76
#include "vp9/encoder/vp9_mcomp.h"
77
#endif
78
#include "vp9/encoder/vp9_multi_thread.h"
79
#include "vp9/encoder/vp9_noise_estimate.h"
80
#include "vp9/encoder/vp9_picklpf.h"
81
#include "vp9/encoder/vp9_quantize.h"
82
#include "vp9/encoder/vp9_ratectrl.h"
83
#include "vp9/encoder/vp9_rd.h"
84
#include "vp9/encoder/vp9_resize.h"
85
#include "vp9/encoder/vp9_segmentation.h"
86
#include "vp9/encoder/vp9_skin_detection.h"
87
#include "vp9/encoder/vp9_speed_features.h"
88
#include "vp9/encoder/vp9_svc_layercontext.h"
89
#include "vp9/encoder/vp9_temporal_filter.h"
90
#include "vp9/encoder/vp9_tpl_model.h"
91
#include "vp9/vp9_cx_iface.h"
92
93
24.9k
#define AM_SEGMENT_ID_INACTIVE 7
94
0
#define AM_SEGMENT_ID_ACTIVE 0
95
96
// Whether to use high precision mv for altref computation.
97
133k
#define ALTREF_HIGH_PRECISION_MV 1
98
99
// Q threshold for high precision mv. Choose a very high value for now so that
100
// HIGH_PRECISION is always chosen.
101
40.6k
#define HIGH_PRECISION_MV_QTHRESH 200
102
103
0
#define FRAME_SIZE_FACTOR 128  // empirical params for context model threshold
104
0
#define FRAME_RATE_FACTOR 8
105
106
#ifdef OUTPUT_YUV_DENOISED
107
FILE *yuv_denoised_file = NULL;
108
#endif
109
#ifdef OUTPUT_YUV_SKINMAP
110
static FILE *yuv_skinmap_file = NULL;
111
#endif
112
#ifdef OUTPUT_YUV_REC
113
FILE *yuv_rec_file;
114
#endif
115
#ifdef OUTPUT_YUV_SVC_SRC
116
FILE *yuv_svc_src[3] = { NULL, NULL, NULL };
117
#endif
118
119
#if 0
120
FILE *framepsnr;
121
FILE *kf_list;
122
FILE *keyfile;
123
#endif
124
125
#ifdef ENABLE_KF_DENOISE
126
// Test condition for spatial denoise of source.
127
static int is_spatial_denoise_enabled(VP9_COMP *cpi) {
128
  VP9_COMMON *const cm = &cpi->common;
129
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
130
131
  return (oxcf->pass != 1) && !is_lossless_requested(&cpi->oxcf) &&
132
         frame_is_intra_only(cm);
133
}
134
#endif
135
136
#if !CONFIG_REALTIME_ONLY
137
// compute adaptive threshold for skip recoding
138
0
static int compute_context_model_thresh(const VP9_COMP *const cpi) {
139
0
  const VP9_COMMON *const cm = &cpi->common;
140
0
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
141
0
  const int frame_size = (cm->width * cm->height) >> 10;
142
0
  const int bitrate = (int)(oxcf->target_bandwidth >> 10);
143
0
  const int qindex_factor = cm->base_qindex + (MAXQ >> 1);
144
145
  // This equation makes the threshold adaptive to frame size.
146
  // Coding gain obtained by recoding comes from alternate frames of large
147
  // content change. We skip recoding if the difference of previous and current
148
  // frame context probability model is less than a certain threshold.
149
  // The first component is the most critical part to guarantee adaptivity.
150
  // Other parameters are estimated based on normal setting of hd resolution
151
  // parameters. e.g. frame_size = 1920x1080, bitrate = 8000, qindex_factor < 50
152
0
  const int thresh =
153
0
      ((FRAME_SIZE_FACTOR * frame_size - FRAME_RATE_FACTOR * bitrate) *
154
0
       qindex_factor) >>
155
0
      9;
156
157
0
  return thresh;
158
0
}
159
160
// compute the total cost difference between current
161
// and previous frame context prob model.
162
0
static int compute_context_model_diff(const VP9_COMMON *const cm) {
163
0
  const FRAME_CONTEXT *const pre_fc =
164
0
      &cm->frame_contexts[cm->frame_context_idx];
165
0
  const FRAME_CONTEXT *const cur_fc = cm->fc;
166
0
  const FRAME_COUNTS *counts = &cm->counts;
167
0
  vpx_prob pre_last_prob, cur_last_prob;
168
0
  int diff = 0;
169
0
  int i, j, k, l, m, n;
170
171
  // y_mode_prob
172
0
  for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) {
173
0
    for (j = 0; j < INTRA_MODES - 1; ++j) {
174
0
      diff += (int)counts->y_mode[i][j] *
175
0
              (pre_fc->y_mode_prob[i][j] - cur_fc->y_mode_prob[i][j]);
176
0
    }
177
0
    pre_last_prob = MAX_PROB - pre_fc->y_mode_prob[i][INTRA_MODES - 2];
178
0
    cur_last_prob = MAX_PROB - cur_fc->y_mode_prob[i][INTRA_MODES - 2];
179
180
0
    diff += (int)counts->y_mode[i][INTRA_MODES - 1] *
181
0
            (pre_last_prob - cur_last_prob);
182
0
  }
183
184
  // uv_mode_prob
185
0
  for (i = 0; i < INTRA_MODES; ++i) {
186
0
    for (j = 0; j < INTRA_MODES - 1; ++j) {
187
0
      diff += (int)counts->uv_mode[i][j] *
188
0
              (pre_fc->uv_mode_prob[i][j] - cur_fc->uv_mode_prob[i][j]);
189
0
    }
190
0
    pre_last_prob = MAX_PROB - pre_fc->uv_mode_prob[i][INTRA_MODES - 2];
191
0
    cur_last_prob = MAX_PROB - cur_fc->uv_mode_prob[i][INTRA_MODES - 2];
192
193
0
    diff += (int)counts->uv_mode[i][INTRA_MODES - 1] *
194
0
            (pre_last_prob - cur_last_prob);
195
0
  }
196
197
  // partition_prob
198
0
  for (i = 0; i < PARTITION_CONTEXTS; ++i) {
199
0
    for (j = 0; j < PARTITION_TYPES - 1; ++j) {
200
0
      diff += (int)counts->partition[i][j] *
201
0
              (pre_fc->partition_prob[i][j] - cur_fc->partition_prob[i][j]);
202
0
    }
203
0
    pre_last_prob = MAX_PROB - pre_fc->partition_prob[i][PARTITION_TYPES - 2];
204
0
    cur_last_prob = MAX_PROB - cur_fc->partition_prob[i][PARTITION_TYPES - 2];
205
206
0
    diff += (int)counts->partition[i][PARTITION_TYPES - 1] *
207
0
            (pre_last_prob - cur_last_prob);
208
0
  }
209
210
  // coef_probs
211
0
  for (i = 0; i < TX_SIZES; ++i) {
212
0
    for (j = 0; j < PLANE_TYPES; ++j) {
213
0
      for (k = 0; k < REF_TYPES; ++k) {
214
0
        for (l = 0; l < COEF_BANDS; ++l) {
215
0
          for (m = 0; m < BAND_COEFF_CONTEXTS(l); ++m) {
216
0
            for (n = 0; n < UNCONSTRAINED_NODES; ++n) {
217
0
              diff += (int)counts->coef[i][j][k][l][m][n] *
218
0
                      (pre_fc->coef_probs[i][j][k][l][m][n] -
219
0
                       cur_fc->coef_probs[i][j][k][l][m][n]);
220
0
            }
221
222
0
            pre_last_prob =
223
0
                MAX_PROB -
224
0
                pre_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
225
0
            cur_last_prob =
226
0
                MAX_PROB -
227
0
                cur_fc->coef_probs[i][j][k][l][m][UNCONSTRAINED_NODES - 1];
228
229
0
            diff += (int)counts->coef[i][j][k][l][m][UNCONSTRAINED_NODES] *
230
0
                    (pre_last_prob - cur_last_prob);
231
0
          }
232
0
        }
233
0
      }
234
0
    }
235
0
  }
236
237
  // switchable_interp_prob
238
0
  for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i) {
239
0
    for (j = 0; j < SWITCHABLE_FILTERS - 1; ++j) {
240
0
      diff += (int)counts->switchable_interp[i][j] *
241
0
              (pre_fc->switchable_interp_prob[i][j] -
242
0
               cur_fc->switchable_interp_prob[i][j]);
243
0
    }
244
0
    pre_last_prob =
245
0
        MAX_PROB - pre_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
246
0
    cur_last_prob =
247
0
        MAX_PROB - cur_fc->switchable_interp_prob[i][SWITCHABLE_FILTERS - 2];
248
249
0
    diff += (int)counts->switchable_interp[i][SWITCHABLE_FILTERS - 1] *
250
0
            (pre_last_prob - cur_last_prob);
251
0
  }
252
253
  // inter_mode_probs
254
0
  for (i = 0; i < INTER_MODE_CONTEXTS; ++i) {
255
0
    for (j = 0; j < INTER_MODES - 1; ++j) {
256
0
      diff += (int)counts->inter_mode[i][j] *
257
0
              (pre_fc->inter_mode_probs[i][j] - cur_fc->inter_mode_probs[i][j]);
258
0
    }
259
0
    pre_last_prob = MAX_PROB - pre_fc->inter_mode_probs[i][INTER_MODES - 2];
260
0
    cur_last_prob = MAX_PROB - cur_fc->inter_mode_probs[i][INTER_MODES - 2];
261
262
0
    diff += (int)counts->inter_mode[i][INTER_MODES - 1] *
263
0
            (pre_last_prob - cur_last_prob);
264
0
  }
265
266
  // intra_inter_prob
267
0
  for (i = 0; i < INTRA_INTER_CONTEXTS; ++i) {
268
0
    diff += (int)counts->intra_inter[i][0] *
269
0
            (pre_fc->intra_inter_prob[i] - cur_fc->intra_inter_prob[i]);
270
271
0
    pre_last_prob = MAX_PROB - pre_fc->intra_inter_prob[i];
272
0
    cur_last_prob = MAX_PROB - cur_fc->intra_inter_prob[i];
273
274
0
    diff += (int)counts->intra_inter[i][1] * (pre_last_prob - cur_last_prob);
275
0
  }
276
277
  // comp_inter_prob
278
0
  for (i = 0; i < COMP_INTER_CONTEXTS; ++i) {
279
0
    diff += (int)counts->comp_inter[i][0] *
280
0
            (pre_fc->comp_inter_prob[i] - cur_fc->comp_inter_prob[i]);
281
282
0
    pre_last_prob = MAX_PROB - pre_fc->comp_inter_prob[i];
283
0
    cur_last_prob = MAX_PROB - cur_fc->comp_inter_prob[i];
284
285
0
    diff += (int)counts->comp_inter[i][1] * (pre_last_prob - cur_last_prob);
286
0
  }
287
288
  // single_ref_prob
289
0
  for (i = 0; i < REF_CONTEXTS; ++i) {
290
0
    for (j = 0; j < 2; ++j) {
291
0
      diff += (int)counts->single_ref[i][j][0] *
292
0
              (pre_fc->single_ref_prob[i][j] - cur_fc->single_ref_prob[i][j]);
293
294
0
      pre_last_prob = MAX_PROB - pre_fc->single_ref_prob[i][j];
295
0
      cur_last_prob = MAX_PROB - cur_fc->single_ref_prob[i][j];
296
297
0
      diff +=
298
0
          (int)counts->single_ref[i][j][1] * (pre_last_prob - cur_last_prob);
299
0
    }
300
0
  }
301
302
  // comp_ref_prob
303
0
  for (i = 0; i < REF_CONTEXTS; ++i) {
304
0
    diff += (int)counts->comp_ref[i][0] *
305
0
            (pre_fc->comp_ref_prob[i] - cur_fc->comp_ref_prob[i]);
306
307
0
    pre_last_prob = MAX_PROB - pre_fc->comp_ref_prob[i];
308
0
    cur_last_prob = MAX_PROB - cur_fc->comp_ref_prob[i];
309
310
0
    diff += (int)counts->comp_ref[i][1] * (pre_last_prob - cur_last_prob);
311
0
  }
312
313
  // tx_probs
314
0
  for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
315
    // p32x32
316
0
    for (j = 0; j < TX_SIZES - 1; ++j) {
317
0
      diff += (int)counts->tx.p32x32[i][j] *
318
0
              (pre_fc->tx_probs.p32x32[i][j] - cur_fc->tx_probs.p32x32[i][j]);
319
0
    }
320
0
    pre_last_prob = MAX_PROB - pre_fc->tx_probs.p32x32[i][TX_SIZES - 2];
321
0
    cur_last_prob = MAX_PROB - cur_fc->tx_probs.p32x32[i][TX_SIZES - 2];
322
323
0
    diff += (int)counts->tx.p32x32[i][TX_SIZES - 1] *
324
0
            (pre_last_prob - cur_last_prob);
325
326
    // p16x16
327
0
    for (j = 0; j < TX_SIZES - 2; ++j) {
328
0
      diff += (int)counts->tx.p16x16[i][j] *
329
0
              (pre_fc->tx_probs.p16x16[i][j] - cur_fc->tx_probs.p16x16[i][j]);
330
0
    }
331
0
    pre_last_prob = MAX_PROB - pre_fc->tx_probs.p16x16[i][TX_SIZES - 3];
332
0
    cur_last_prob = MAX_PROB - cur_fc->tx_probs.p16x16[i][TX_SIZES - 3];
333
334
0
    diff += (int)counts->tx.p16x16[i][TX_SIZES - 2] *
335
0
            (pre_last_prob - cur_last_prob);
336
337
    // p8x8
338
0
    for (j = 0; j < TX_SIZES - 3; ++j) {
339
0
      diff += (int)counts->tx.p8x8[i][j] *
340
0
              (pre_fc->tx_probs.p8x8[i][j] - cur_fc->tx_probs.p8x8[i][j]);
341
0
    }
342
0
    pre_last_prob = MAX_PROB - pre_fc->tx_probs.p8x8[i][TX_SIZES - 4];
343
0
    cur_last_prob = MAX_PROB - cur_fc->tx_probs.p8x8[i][TX_SIZES - 4];
344
345
0
    diff +=
346
0
        (int)counts->tx.p8x8[i][TX_SIZES - 3] * (pre_last_prob - cur_last_prob);
347
0
  }
348
349
  // skip_probs
350
0
  for (i = 0; i < SKIP_CONTEXTS; ++i) {
351
0
    diff += (int)counts->skip[i][0] *
352
0
            (pre_fc->skip_probs[i] - cur_fc->skip_probs[i]);
353
354
0
    pre_last_prob = MAX_PROB - pre_fc->skip_probs[i];
355
0
    cur_last_prob = MAX_PROB - cur_fc->skip_probs[i];
356
357
0
    diff += (int)counts->skip[i][1] * (pre_last_prob - cur_last_prob);
358
0
  }
359
360
  // mv
361
0
  for (i = 0; i < MV_JOINTS - 1; ++i) {
362
0
    diff += (int)counts->mv.joints[i] *
363
0
            (pre_fc->nmvc.joints[i] - cur_fc->nmvc.joints[i]);
364
0
  }
365
0
  pre_last_prob = MAX_PROB - pre_fc->nmvc.joints[MV_JOINTS - 2];
366
0
  cur_last_prob = MAX_PROB - cur_fc->nmvc.joints[MV_JOINTS - 2];
367
368
0
  diff +=
369
0
      (int)counts->mv.joints[MV_JOINTS - 1] * (pre_last_prob - cur_last_prob);
370
371
0
  for (i = 0; i < 2; ++i) {
372
0
    const nmv_component_counts *nmv_count = &counts->mv.comps[i];
373
0
    const nmv_component *pre_nmv_prob = &pre_fc->nmvc.comps[i];
374
0
    const nmv_component *cur_nmv_prob = &cur_fc->nmvc.comps[i];
375
376
    // sign
377
0
    diff += (int)nmv_count->sign[0] * (pre_nmv_prob->sign - cur_nmv_prob->sign);
378
379
0
    pre_last_prob = MAX_PROB - pre_nmv_prob->sign;
380
0
    cur_last_prob = MAX_PROB - cur_nmv_prob->sign;
381
382
0
    diff += (int)nmv_count->sign[1] * (pre_last_prob - cur_last_prob);
383
384
    // classes
385
0
    for (j = 0; j < MV_CLASSES - 1; ++j) {
386
0
      diff += (int)nmv_count->classes[j] *
387
0
              (pre_nmv_prob->classes[j] - cur_nmv_prob->classes[j]);
388
0
    }
389
0
    pre_last_prob = MAX_PROB - pre_nmv_prob->classes[MV_CLASSES - 2];
390
0
    cur_last_prob = MAX_PROB - cur_nmv_prob->classes[MV_CLASSES - 2];
391
392
0
    diff += (int)nmv_count->classes[MV_CLASSES - 1] *
393
0
            (pre_last_prob - cur_last_prob);
394
395
    // class0
396
0
    for (j = 0; j < CLASS0_SIZE - 1; ++j) {
397
0
      diff += (int)nmv_count->class0[j] *
398
0
              (pre_nmv_prob->class0[j] - cur_nmv_prob->class0[j]);
399
0
    }
400
0
    pre_last_prob = MAX_PROB - pre_nmv_prob->class0[CLASS0_SIZE - 2];
401
0
    cur_last_prob = MAX_PROB - cur_nmv_prob->class0[CLASS0_SIZE - 2];
402
403
0
    diff += (int)nmv_count->class0[CLASS0_SIZE - 1] *
404
0
            (pre_last_prob - cur_last_prob);
405
406
    // bits
407
0
    for (j = 0; j < MV_OFFSET_BITS; ++j) {
408
0
      diff += (int)nmv_count->bits[j][0] *
409
0
              (pre_nmv_prob->bits[j] - cur_nmv_prob->bits[j]);
410
411
0
      pre_last_prob = MAX_PROB - pre_nmv_prob->bits[j];
412
0
      cur_last_prob = MAX_PROB - cur_nmv_prob->bits[j];
413
414
0
      diff += (int)nmv_count->bits[j][1] * (pre_last_prob - cur_last_prob);
415
0
    }
416
417
    // class0_fp
418
0
    for (j = 0; j < CLASS0_SIZE; ++j) {
419
0
      for (k = 0; k < MV_FP_SIZE - 1; ++k) {
420
0
        diff += (int)nmv_count->class0_fp[j][k] *
421
0
                (pre_nmv_prob->class0_fp[j][k] - cur_nmv_prob->class0_fp[j][k]);
422
0
      }
423
0
      pre_last_prob = MAX_PROB - pre_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
424
0
      cur_last_prob = MAX_PROB - cur_nmv_prob->class0_fp[j][MV_FP_SIZE - 2];
425
426
0
      diff += (int)nmv_count->class0_fp[j][MV_FP_SIZE - 1] *
427
0
              (pre_last_prob - cur_last_prob);
428
0
    }
429
430
    // fp
431
0
    for (j = 0; j < MV_FP_SIZE - 1; ++j) {
432
0
      diff +=
433
0
          (int)nmv_count->fp[j] * (pre_nmv_prob->fp[j] - cur_nmv_prob->fp[j]);
434
0
    }
435
0
    pre_last_prob = MAX_PROB - pre_nmv_prob->fp[MV_FP_SIZE - 2];
436
0
    cur_last_prob = MAX_PROB - cur_nmv_prob->fp[MV_FP_SIZE - 2];
437
438
0
    diff +=
439
0
        (int)nmv_count->fp[MV_FP_SIZE - 1] * (pre_last_prob - cur_last_prob);
440
441
    // class0_hp
442
0
    diff += (int)nmv_count->class0_hp[0] *
443
0
            (pre_nmv_prob->class0_hp - cur_nmv_prob->class0_hp);
444
445
0
    pre_last_prob = MAX_PROB - pre_nmv_prob->class0_hp;
446
0
    cur_last_prob = MAX_PROB - cur_nmv_prob->class0_hp;
447
448
0
    diff += (int)nmv_count->class0_hp[1] * (pre_last_prob - cur_last_prob);
449
450
    // hp
451
0
    diff += (int)nmv_count->hp[0] * (pre_nmv_prob->hp - cur_nmv_prob->hp);
452
453
0
    pre_last_prob = MAX_PROB - pre_nmv_prob->hp;
454
0
    cur_last_prob = MAX_PROB - cur_nmv_prob->hp;
455
456
0
    diff += (int)nmv_count->hp[1] * (pre_last_prob - cur_last_prob);
457
0
  }
458
459
0
  return -diff;
460
0
}
461
#endif  // !CONFIG_REALTIME_ONLY
462
463
// Test for whether to calculate metrics for the frame.
464
106k
static int is_psnr_calc_enabled(const VP9_COMP *cpi) {
465
106k
  const VP9_COMMON *const cm = &cpi->common;
466
106k
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
467
468
106k
  return cpi->b_calculate_psnr && (oxcf->pass != 1) && cm->show_frame;
469
106k
}
470
471
/* clang-format off */
472
const Vp9LevelSpec vp9_level_defs[VP9_LEVELS] = {
473
  //         sample rate    size   breadth  bitrate  cpb
474
  { LEVEL_1,   829440,      36864,    512,   200,    400,    2, 1,  4,  8 },
475
  { LEVEL_1_1, 2764800,     73728,    768,   800,    1000,   2, 1,  4,  8 },
476
  { LEVEL_2,   4608000,     122880,   960,   1800,   1500,   2, 1,  4,  8 },
477
  { LEVEL_2_1, 9216000,     245760,   1344,  3600,   2800,   2, 2,  4,  8 },
478
  { LEVEL_3,   20736000,    552960,   2048,  7200,   6000,   2, 4,  4,  8 },
479
  { LEVEL_3_1, 36864000,    983040,   2752,  12000,  10000,  2, 4,  4,  8 },
480
  { LEVEL_4,   83558400,    2228224,  4160,  18000,  16000,  4, 4,  4,  8 },
481
  { LEVEL_4_1, 160432128,   2228224,  4160,  30000,  18000,  4, 4,  5,  6 },
482
  { LEVEL_5,   311951360,   8912896,  8384,  60000,  36000,  6, 8,  6,  4 },
483
  { LEVEL_5_1, 588251136,   8912896,  8384,  120000, 46000,  8, 8,  10, 4 },
484
  // TODO(huisu): update max_cpb_size for level 5_2 ~ 6_2 when
485
  // they are finalized (currently tentative).
486
  { LEVEL_5_2, 1176502272,  8912896,  8384,  180000, 90000,  8, 8,  10, 4 },
487
  { LEVEL_6,   1176502272,  35651584, 16832, 180000, 90000,  8, 16, 10, 4 },
488
  { LEVEL_6_1, 2353004544u, 35651584, 16832, 240000, 180000, 8, 16, 10, 4 },
489
  { LEVEL_6_2, 4706009088u, 35651584, 16832, 480000, 360000, 8, 16, 10, 4 },
490
};
491
/* clang-format on */
492
493
static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] = {
494
  "The average bit-rate is too high.",
495
  "The picture size is too large.",
496
  "The picture width/height is too large.",
497
  "The luma sample rate is too large.",
498
  "The CPB size is too large.",
499
  "The compression ratio is too small",
500
  "Too many column tiles are used.",
501
  "The alt-ref distance is too small.",
502
  "Too many reference buffers are used."
503
};
504
505
0
static INLINE void Scale2Ratio(VPX_SCALING_MODE mode, int *hr, int *hs) {
506
0
  switch (mode) {
507
0
    case VP8E_NORMAL:
508
0
      *hr = 1;
509
0
      *hs = 1;
510
0
      break;
511
0
    case VP8E_FOURFIVE:
512
0
      *hr = 4;
513
0
      *hs = 5;
514
0
      break;
515
0
    case VP8E_THREEFIVE:
516
0
      *hr = 3;
517
0
      *hs = 5;
518
0
      break;
519
0
    default:
520
0
      assert(mode == VP8E_ONETWO);
521
0
      *hr = 1;
522
0
      *hs = 2;
523
0
      break;
524
0
  }
525
0
}
526
527
// Mark all inactive blocks as active. Other segmentation features may be set
528
// so memset cannot be used, instead only inactive blocks should be reset.
529
53.1k
static void suppress_active_map(VP9_COMP *cpi) {
530
53.1k
  unsigned char *const seg_map = cpi->segmentation_map;
531
532
53.1k
  if (cpi->active_map.enabled || cpi->active_map.update) {
533
0
    const int rows = cpi->common.mi_rows;
534
0
    const int cols = cpi->common.mi_cols;
535
0
    int i;
536
537
0
    for (i = 0; i < rows * cols; ++i)
538
0
      if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
539
0
        seg_map[i] = AM_SEGMENT_ID_ACTIVE;
540
0
  }
541
53.1k
}
542
543
53.1k
static void apply_active_map(VP9_COMP *cpi) {
544
53.1k
  struct segmentation *const seg = &cpi->common.seg;
545
53.1k
  unsigned char *const seg_map = cpi->segmentation_map;
546
53.1k
  const unsigned char *const active_map = cpi->active_map.map;
547
53.1k
  int i;
548
549
53.1k
  assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
550
551
53.1k
  if (frame_is_intra_only(&cpi->common)) {
552
12.4k
    cpi->active_map.enabled = 0;
553
12.4k
    cpi->active_map.update = 1;
554
12.4k
  }
555
556
53.1k
  if (cpi->active_map.update) {
557
12.4k
    if (cpi->active_map.enabled) {
558
0
      for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
559
0
        if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
560
0
      vp9_enable_segmentation(seg);
561
0
      vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
562
0
      vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
563
      // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
564
      // filter level being zero regardless of the value of seg->abs_delta.
565
0
      vp9_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
566
0
                      -MAX_LOOP_FILTER);
567
12.4k
    } else {
568
12.4k
      vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
569
12.4k
      vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
570
12.4k
      if (seg->enabled) {
571
0
        seg->update_data = 1;
572
0
        seg->update_map = 1;
573
0
      }
574
12.4k
    }
575
12.4k
    cpi->active_map.update = 0;
576
12.4k
  }
577
53.1k
}
578
579
0
static void apply_roi_map(VP9_COMP *cpi) {
580
0
  VP9_COMMON *cm = &cpi->common;
581
0
  struct segmentation *const seg = &cm->seg;
582
0
  vpx_roi_map_t *roi = &cpi->roi;
583
0
  const int *delta_q = roi->delta_q;
584
0
  const int *delta_lf = roi->delta_lf;
585
0
  const int *skip = roi->skip;
586
0
  int ref_frame[8];
587
0
  int internal_delta_q[MAX_SEGMENTS];
588
0
  int i;
589
590
  // TODO(jianj): Investigate why ROI not working in speed < 5 or in non
591
  // realtime mode.
592
0
  if (cpi->oxcf.mode != REALTIME || cpi->oxcf.speed < 5) return;
593
0
  if (!roi->enabled) return;
594
595
0
  memcpy(&ref_frame, roi->ref_frame, sizeof(ref_frame));
596
597
0
  vp9_enable_segmentation(seg);
598
0
  vp9_clearall_segfeatures(seg);
599
  // Select delta coding method;
600
0
  seg->abs_delta = SEGMENT_DELTADATA;
601
602
0
  memcpy(cpi->segmentation_map, roi->roi_map, (cm->mi_rows * cm->mi_cols));
603
604
0
  for (i = 0; i < MAX_SEGMENTS; ++i) {
605
    // Translate the external delta q values to internal values.
606
0
    internal_delta_q[i] = vp9_quantizer_to_qindex(abs(delta_q[i]));
607
0
    if (delta_q[i] < 0) internal_delta_q[i] = -internal_delta_q[i];
608
0
    vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q);
609
0
    vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF);
610
0
    if (internal_delta_q[i] != 0) {
611
0
      vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
612
0
      vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, internal_delta_q[i]);
613
0
    }
614
0
    if (delta_lf[i] != 0) {
615
0
      vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF);
616
0
      vp9_set_segdata(seg, i, SEG_LVL_ALT_LF, delta_lf[i]);
617
0
    }
618
0
    if (skip[i] != 0) {
619
0
      vp9_enable_segfeature(seg, i, SEG_LVL_SKIP);
620
0
      vp9_set_segdata(seg, i, SEG_LVL_SKIP, 0);
621
0
    }
622
0
    if (ref_frame[i] >= 0) {
623
0
      int valid_ref = 1;
624
      // ALTREF is not used as reference for nonrd_pickmode with 0 lag.
625
0
      if (ref_frame[i] == ALTREF_FRAME && cpi->sf.use_nonrd_pick_mode)
626
0
        valid_ref = 0;
627
      // If GOLDEN is selected, make sure it's set as reference.
628
0
      if (ref_frame[i] == GOLDEN_FRAME &&
629
0
          !(cpi->ref_frame_flags & ref_frame_to_flag(ref_frame[i]))) {
630
0
        valid_ref = 0;
631
0
      }
632
      // GOLDEN was updated in previous encoded frame, so GOLDEN and LAST are
633
      // same reference.
634
0
      if (ref_frame[i] == GOLDEN_FRAME && cpi->rc.frames_since_golden == 0)
635
0
        ref_frame[i] = LAST_FRAME;
636
0
      if (valid_ref) {
637
0
        vp9_enable_segfeature(seg, i, SEG_LVL_REF_FRAME);
638
0
        vp9_set_segdata(seg, i, SEG_LVL_REF_FRAME, ref_frame[i]);
639
0
      }
640
0
    }
641
0
  }
642
0
  roi->enabled = 1;
643
0
}
644
645
3.99k
static void init_level_info(Vp9LevelInfo *level_info) {
646
3.99k
  Vp9LevelStats *const level_stats = &level_info->level_stats;
647
3.99k
  Vp9LevelSpec *const level_spec = &level_info->level_spec;
648
649
3.99k
  memset(level_stats, 0, sizeof(*level_stats));
650
3.99k
  memset(level_spec, 0, sizeof(*level_spec));
651
3.99k
  level_spec->level = LEVEL_UNKNOWN;
652
3.99k
  level_spec->min_altref_distance = INT_MAX;
653
3.99k
}
654
655
0
static int check_seg_range(int seg_data[8], int range) {
656
0
  int i;
657
0
  for (i = 0; i < 8; ++i) {
658
    // Note abs() alone can't be used as the behavior of abs(INT_MIN) is
659
    // undefined.
660
0
    if (seg_data[i] > range || seg_data[i] < -range) {
661
0
      return 0;
662
0
    }
663
0
  }
664
0
  return 1;
665
0
}
666
667
0
VP9_LEVEL vp9_get_level(const Vp9LevelSpec *const level_spec) {
668
0
  int i;
669
0
  const Vp9LevelSpec *this_level;
670
671
0
  vpx_clear_system_state();
672
673
0
  for (i = 0; i < VP9_LEVELS; ++i) {
674
0
    this_level = &vp9_level_defs[i];
675
0
    if ((double)level_spec->max_luma_sample_rate >
676
0
            (double)this_level->max_luma_sample_rate *
677
0
                (1 + SAMPLE_RATE_GRACE_P) ||
678
0
        level_spec->max_luma_picture_size > this_level->max_luma_picture_size ||
679
0
        level_spec->max_luma_picture_breadth >
680
0
            this_level->max_luma_picture_breadth ||
681
0
        level_spec->average_bitrate > this_level->average_bitrate ||
682
0
        level_spec->max_cpb_size > this_level->max_cpb_size ||
683
0
        level_spec->compression_ratio < this_level->compression_ratio ||
684
0
        level_spec->max_col_tiles > this_level->max_col_tiles ||
685
0
        level_spec->min_altref_distance < this_level->min_altref_distance ||
686
0
        level_spec->max_ref_frame_buffers > this_level->max_ref_frame_buffers)
687
0
      continue;
688
0
    break;
689
0
  }
690
0
  return (i == VP9_LEVELS) ? LEVEL_UNKNOWN : vp9_level_defs[i].level;
691
0
}
692
693
vpx_codec_err_t vp9_set_roi_map(VP9_COMP *cpi, unsigned char *map,
694
                                unsigned int rows, unsigned int cols,
695
                                int delta_q[8], int delta_lf[8], int skip[8],
696
0
                                int ref_frame[8]) {
697
0
  VP9_COMMON *cm = &cpi->common;
698
0
  vpx_roi_map_t *roi = &cpi->roi;
699
0
  const int range = 63;
700
0
  const int ref_frame_range = 3;  // Alt-ref
701
0
  const int skip_range = 1;
702
0
  const int frame_rows = cpi->common.mi_rows;
703
0
  const int frame_cols = cpi->common.mi_cols;
704
705
  // Check number of rows and columns match
706
0
  if (frame_rows != (int)rows || frame_cols != (int)cols) {
707
0
    return VPX_CODEC_INVALID_PARAM;
708
0
  }
709
710
0
  if (!check_seg_range(delta_q, range) || !check_seg_range(delta_lf, range) ||
711
0
      !check_seg_range(ref_frame, ref_frame_range) ||
712
0
      !check_seg_range(skip, skip_range))
713
0
    return VPX_CODEC_INVALID_PARAM;
714
715
  // Also disable segmentation if no deltas are specified.
716
0
  if (!map ||
717
0
      (!(delta_q[0] | delta_q[1] | delta_q[2] | delta_q[3] | delta_q[4] |
718
0
         delta_q[5] | delta_q[6] | delta_q[7] | delta_lf[0] | delta_lf[1] |
719
0
         delta_lf[2] | delta_lf[3] | delta_lf[4] | delta_lf[5] | delta_lf[6] |
720
0
         delta_lf[7] | skip[0] | skip[1] | skip[2] | skip[3] | skip[4] |
721
0
         skip[5] | skip[6] | skip[7]) &&
722
0
       (ref_frame[0] == -1 && ref_frame[1] == -1 && ref_frame[2] == -1 &&
723
0
        ref_frame[3] == -1 && ref_frame[4] == -1 && ref_frame[5] == -1 &&
724
0
        ref_frame[6] == -1 && ref_frame[7] == -1))) {
725
0
    vp9_disable_segmentation(&cm->seg);
726
0
    cpi->roi.enabled = 0;
727
0
    return VPX_CODEC_OK;
728
0
  }
729
730
0
  if (roi->roi_map) {
731
0
    vpx_free(roi->roi_map);
732
0
    roi->roi_map = NULL;
733
0
  }
734
0
  roi->roi_map = vpx_malloc(rows * cols);
735
0
  if (!roi->roi_map) return VPX_CODEC_MEM_ERROR;
736
737
  // Copy to ROI structure in the compressor.
738
0
  memcpy(roi->roi_map, map, rows * cols);
739
0
  memcpy(&roi->delta_q, delta_q, MAX_SEGMENTS * sizeof(delta_q[0]));
740
0
  memcpy(&roi->delta_lf, delta_lf, MAX_SEGMENTS * sizeof(delta_lf[0]));
741
0
  memcpy(&roi->skip, skip, MAX_SEGMENTS * sizeof(skip[0]));
742
0
  memcpy(&roi->ref_frame, ref_frame, MAX_SEGMENTS * sizeof(ref_frame[0]));
743
0
  roi->enabled = 1;
744
0
  roi->rows = rows;
745
0
  roi->cols = cols;
746
747
0
  return VPX_CODEC_OK;
748
0
}
749
750
int vp9_set_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
751
0
                       int cols) {
752
0
  if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
753
0
    unsigned char *const active_map_8x8 = cpi->active_map.map;
754
0
    const int mi_rows = cpi->common.mi_rows;
755
0
    const int mi_cols = cpi->common.mi_cols;
756
0
    cpi->active_map.update = 1;
757
0
    if (new_map_16x16) {
758
0
      int r, c;
759
0
      for (r = 0; r < mi_rows; ++r) {
760
0
        for (c = 0; c < mi_cols; ++c) {
761
0
          active_map_8x8[r * mi_cols + c] =
762
0
              new_map_16x16[(r >> 1) * cols + (c >> 1)]
763
0
                  ? AM_SEGMENT_ID_ACTIVE
764
0
                  : AM_SEGMENT_ID_INACTIVE;
765
0
        }
766
0
      }
767
0
      cpi->active_map.enabled = 1;
768
0
    } else {
769
0
      cpi->active_map.enabled = 0;
770
0
    }
771
0
    return 0;
772
0
  } else {
773
0
    return -1;
774
0
  }
775
0
}
776
777
int vp9_get_active_map(VP9_COMP *cpi, unsigned char *new_map_16x16, int rows,
778
0
                       int cols) {
779
0
  if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
780
0
      new_map_16x16) {
781
0
    unsigned char *const seg_map_8x8 = cpi->segmentation_map;
782
0
    const int mi_rows = cpi->common.mi_rows;
783
0
    const int mi_cols = cpi->common.mi_cols;
784
0
    memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
785
0
    if (cpi->active_map.enabled) {
786
0
      int r, c;
787
0
      for (r = 0; r < mi_rows; ++r) {
788
0
        for (c = 0; c < mi_cols; ++c) {
789
          // Cyclic refresh segments are considered active despite not having
790
          // AM_SEGMENT_ID_ACTIVE
791
0
          new_map_16x16[(r >> 1) * cols + (c >> 1)] |=
792
0
              seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
793
0
        }
794
0
      }
795
0
    }
796
0
    return 0;
797
0
  } else {
798
0
    return -1;
799
0
  }
800
0
}
801
802
208k
void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
803
208k
  MACROBLOCK *const mb = &cpi->td.mb;
804
208k
  cpi->common.allow_high_precision_mv = allow_high_precision_mv;
805
208k
  if (cpi->common.allow_high_precision_mv) {
806
164k
    mb->mvcost = mb->nmvcost_hp;
807
164k
    mb->mvsadcost = mb->nmvsadcost_hp;
808
164k
  } else {
809
43.9k
    mb->mvcost = mb->nmvcost;
810
43.9k
    mb->mvsadcost = mb->nmvsadcost;
811
43.9k
  }
812
208k
}
813
814
53.1k
static void setup_frame(VP9_COMP *cpi) {
815
53.1k
  VP9_COMMON *const cm = &cpi->common;
816
  // Set up entropy context depending on frame type. The decoder mandates
817
  // the use of the default context, index 0, for keyframes and inter
818
  // frames where the error_resilient_mode or intra_only flag is set. For
819
  // other inter-frames the encoder currently uses only two contexts;
820
  // context 1 for ALTREF frames and context 0 for the others.
821
53.1k
  if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
822
12.4k
    vp9_setup_past_independence(cm);
823
40.6k
  } else {
824
40.6k
    if (!cpi->use_svc) cm->frame_context_idx = cpi->refresh_alt_ref_frame;
825
40.6k
  }
826
827
  // TODO(jingning): Overwrite the frame_context_idx index in multi-layer ARF
828
  // case. Need some further investigation on if we could apply this to single
829
  // layer ARF case as well.
830
53.1k
  if (cpi->multi_layer_arf && !cpi->use_svc) {
831
0
    GF_GROUP *const gf_group = &cpi->twopass.gf_group;
832
0
    const int gf_group_index = gf_group->index;
833
0
    const int boost_frame =
834
0
        !cpi->rc.is_src_frame_alt_ref &&
835
0
        (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
836
837
    // frame_context_idx           Frame Type
838
    //        0              Intra only frame, base layer ARF
839
    //        1              ARFs with layer depth = 2,3
840
    //        2              ARFs with layer depth > 3
841
    //        3              Non-boosted frames
842
0
    if (frame_is_intra_only(cm)) {
843
0
      cm->frame_context_idx = 0;
844
0
    } else if (boost_frame) {
845
0
      if (gf_group->rf_level[gf_group_index] == GF_ARF_STD)
846
0
        cm->frame_context_idx = 0;
847
0
      else if (gf_group->layer_depth[gf_group_index] <= 3)
848
0
        cm->frame_context_idx = 1;
849
0
      else
850
0
        cm->frame_context_idx = 2;
851
0
    } else {
852
0
      cm->frame_context_idx = 3;
853
0
    }
854
0
  }
855
856
53.1k
  if (cm->frame_type == KEY_FRAME) {
857
12.4k
    cpi->refresh_golden_frame = 1;
858
12.4k
    cpi->refresh_alt_ref_frame = 1;
859
12.4k
    vp9_zero(cpi->interp_filter_selected);
860
40.6k
  } else {
861
40.6k
    *cm->fc = cm->frame_contexts[cm->frame_context_idx];
862
40.6k
    vp9_zero(cpi->interp_filter_selected[0]);
863
40.6k
  }
864
53.1k
}
865
866
34.5k
static void vp9_enc_setup_mi(VP9_COMMON *cm) {
867
34.5k
  int i;
868
34.5k
  cm->mi = cm->mip + cm->mi_stride + 1;
869
34.5k
  memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
870
34.5k
  cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
871
  // Clear top border row
872
34.5k
  memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
873
  // Clear left border column
874
2.38M
  for (i = 1; i < cm->mi_rows + 1; ++i)
875
2.35M
    memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
876
877
34.5k
  cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
878
34.5k
  cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
879
880
34.5k
  memset(cm->mi_grid_base, 0,
881
34.5k
         cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
882
34.5k
}
883
884
3.99k
static int vp9_enc_alloc_mi(VP9_COMMON *cm, int mi_size) {
885
3.99k
  cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
886
3.99k
  if (!cm->mip) return 1;
887
3.99k
  cm->prev_mip = vpx_calloc(mi_size, sizeof(*cm->prev_mip));
888
3.99k
  if (!cm->prev_mip) return 1;
889
3.99k
  cm->mi_alloc_size = mi_size;
890
891
3.99k
  cm->mi_grid_base =
892
3.99k
      (MODE_INFO **)vpx_calloc(mi_size, sizeof(*cm->mi_grid_base));
893
3.99k
  if (!cm->mi_grid_base) return 1;
894
3.99k
  cm->prev_mi_grid_base =
895
3.99k
      (MODE_INFO **)vpx_calloc(mi_size, sizeof(*cm->prev_mi_grid_base));
896
3.99k
  if (!cm->prev_mi_grid_base) return 1;
897
898
3.99k
  return 0;
899
3.99k
}
900
901
11.9k
static void vp9_enc_free_mi(VP9_COMMON *cm) {
902
11.9k
  vpx_free(cm->mip);
903
11.9k
  cm->mip = NULL;
904
11.9k
  vpx_free(cm->prev_mip);
905
11.9k
  cm->prev_mip = NULL;
906
11.9k
  vpx_free(cm->mi_grid_base);
907
11.9k
  cm->mi_grid_base = NULL;
908
11.9k
  vpx_free(cm->prev_mi_grid_base);
909
11.9k
  cm->prev_mi_grid_base = NULL;
910
11.9k
  cm->mi_alloc_size = 0;
911
11.9k
}
912
913
53.0k
static void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
914
  // Current mip will be the prev_mip for the next frame.
915
53.0k
  MODE_INFO **temp_base = cm->prev_mi_grid_base;
916
53.0k
  MODE_INFO *temp = cm->prev_mip;
917
918
  // Skip update prev_mi frame in show_existing_frame mode.
919
53.0k
  if (cm->show_existing_frame) return;
920
921
53.0k
  cm->prev_mip = cm->mip;
922
53.0k
  cm->mip = temp;
923
924
  // Update the upper left visible macroblock ptrs.
925
53.0k
  cm->mi = cm->mip + cm->mi_stride + 1;
926
53.0k
  cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
927
928
53.0k
  cm->prev_mi_grid_base = cm->mi_grid_base;
929
53.0k
  cm->mi_grid_base = temp_base;
930
53.0k
  cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
931
53.0k
  cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
932
53.0k
}
933
934
1
static void initialize_enc(void) {
935
1
  vp9_rtcd();
936
1
  vpx_dsp_rtcd();
937
1
  vpx_scale_rtcd();
938
1
  vp9_init_intra_predictors();
939
1
  vp9_init_me_luts();
940
1
  vp9_rc_init_minq_luts();
941
1
  vp9_entropy_mv_init();
942
1
#if !CONFIG_REALTIME_ONLY
943
1
  vp9_temporal_filter_init();
944
1
#endif
945
1
}
946
947
4.04k
void vp9_initialize_enc(void) { once(initialize_enc); }
948
949
3.99k
static void dealloc_compressor_data(VP9_COMP *cpi) {
950
3.99k
  VP9_COMMON *const cm = &cpi->common;
951
3.99k
  int i;
952
953
3.99k
  vpx_free(cpi->mbmi_ext_base);
954
3.99k
  cpi->mbmi_ext_base = NULL;
955
956
3.99k
  vpx_free(cpi->tile_data);
957
3.99k
  cpi->tile_data = NULL;
958
959
3.99k
  vpx_free(cpi->segmentation_map);
960
3.99k
  cpi->segmentation_map = NULL;
961
3.99k
  vpx_free(cpi->coding_context.last_frame_seg_map_copy);
962
3.99k
  cpi->coding_context.last_frame_seg_map_copy = NULL;
963
964
3.99k
  vpx_free(cpi->nmvcosts[0]);
965
3.99k
  vpx_free(cpi->nmvcosts[1]);
966
3.99k
  cpi->nmvcosts[0] = NULL;
967
3.99k
  cpi->nmvcosts[1] = NULL;
968
969
3.99k
  vpx_free(cpi->nmvcosts_hp[0]);
970
3.99k
  vpx_free(cpi->nmvcosts_hp[1]);
971
3.99k
  cpi->nmvcosts_hp[0] = NULL;
972
3.99k
  cpi->nmvcosts_hp[1] = NULL;
973
974
3.99k
  vpx_free(cpi->nmvsadcosts[0]);
975
3.99k
  vpx_free(cpi->nmvsadcosts[1]);
976
3.99k
  cpi->nmvsadcosts[0] = NULL;
977
3.99k
  cpi->nmvsadcosts[1] = NULL;
978
979
3.99k
  vpx_free(cpi->nmvsadcosts_hp[0]);
980
3.99k
  vpx_free(cpi->nmvsadcosts_hp[1]);
981
3.99k
  cpi->nmvsadcosts_hp[0] = NULL;
982
3.99k
  cpi->nmvsadcosts_hp[1] = NULL;
983
984
3.99k
  vpx_free(cpi->skin_map);
985
3.99k
  cpi->skin_map = NULL;
986
987
3.99k
  vpx_free(cpi->prev_partition);
988
3.99k
  cpi->prev_partition = NULL;
989
990
3.99k
  vpx_free(cpi->svc.prev_partition_svc);
991
3.99k
  cpi->svc.prev_partition_svc = NULL;
992
993
3.99k
  vpx_free(cpi->prev_segment_id);
994
3.99k
  cpi->prev_segment_id = NULL;
995
996
3.99k
  vpx_free(cpi->prev_variance_low);
997
3.99k
  cpi->prev_variance_low = NULL;
998
999
3.99k
  vpx_free(cpi->copied_frame_cnt);
1000
3.99k
  cpi->copied_frame_cnt = NULL;
1001
1002
3.99k
  vpx_free(cpi->content_state_sb_fd);
1003
3.99k
  cpi->content_state_sb_fd = NULL;
1004
1005
3.99k
  vpx_free(cpi->count_arf_frame_usage);
1006
3.99k
  cpi->count_arf_frame_usage = NULL;
1007
3.99k
  vpx_free(cpi->count_lastgolden_frame_usage);
1008
3.99k
  cpi->count_lastgolden_frame_usage = NULL;
1009
1010
3.99k
  vp9_cyclic_refresh_free(cpi->cyclic_refresh);
1011
3.99k
  cpi->cyclic_refresh = NULL;
1012
1013
3.99k
  vpx_free(cpi->active_map.map);
1014
3.99k
  cpi->active_map.map = NULL;
1015
1016
3.99k
  vpx_free(cpi->roi.roi_map);
1017
3.99k
  cpi->roi.roi_map = NULL;
1018
1019
3.99k
  vpx_free(cpi->consec_zero_mv);
1020
3.99k
  cpi->consec_zero_mv = NULL;
1021
1022
3.99k
  vpx_free(cpi->mb_wiener_variance);
1023
3.99k
  cpi->mb_wiener_variance = NULL;
1024
1025
3.99k
  vpx_free(cpi->sb_mul_scale);
1026
3.99k
  cpi->sb_mul_scale = NULL;
1027
1028
3.99k
  vpx_free(cpi->mi_ssim_rdmult_scaling_factors);
1029
3.99k
  cpi->mi_ssim_rdmult_scaling_factors = NULL;
1030
1031
3.99k
  vp9_free_ref_frame_buffers(cm->buffer_pool);
1032
#if CONFIG_VP9_POSTPROC
1033
  vp9_free_postproc_buffers(cm);
1034
#endif
1035
3.99k
  vp9_free_context_buffers(cm);
1036
1037
3.99k
  vpx_free_frame_buffer(&cpi->last_frame_uf);
1038
3.99k
  vpx_free_frame_buffer(&cpi->scaled_source);
1039
3.99k
  vpx_free_frame_buffer(&cpi->scaled_last_source);
1040
3.99k
  vpx_free_frame_buffer(&cpi->tf_buffer);
1041
#ifdef ENABLE_KF_DENOISE
1042
  vpx_free_frame_buffer(&cpi->raw_unscaled_source);
1043
  vpx_free_frame_buffer(&cpi->raw_scaled_source);
1044
#endif
1045
1046
3.99k
  vp9_lookahead_destroy(cpi->lookahead);
1047
1048
3.99k
  vpx_free(cpi->tile_tok[0][0]);
1049
3.99k
  cpi->tile_tok[0][0] = 0;
1050
1051
3.99k
  vpx_free(cpi->tplist[0][0]);
1052
3.99k
  cpi->tplist[0][0] = NULL;
1053
1054
3.99k
  vp9_free_pc_tree(&cpi->td);
1055
1056
7.99k
  for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
1057
3.99k
    LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
1058
3.99k
    vpx_free(lc->rc_twopass_stats_in.buf);
1059
3.99k
    lc->rc_twopass_stats_in.buf = NULL;
1060
3.99k
    lc->rc_twopass_stats_in.sz = 0;
1061
3.99k
  }
1062
1063
103k
  for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
1064
99.8k
    vpx_free_frame_buffer(&cpi->svc.scaled_frames[i]);
1065
99.8k
  }
1066
3.99k
  memset(&cpi->svc.scaled_frames[0], 0,
1067
3.99k
         MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
1068
1069
3.99k
  vpx_free_frame_buffer(&cpi->svc.scaled_temp);
1070
3.99k
  memset(&cpi->svc.scaled_temp, 0, sizeof(cpi->svc.scaled_temp));
1071
1072
3.99k
  vpx_free_frame_buffer(&cpi->svc.empty_frame.img);
1073
3.99k
  memset(&cpi->svc.empty_frame, 0, sizeof(cpi->svc.empty_frame));
1074
1075
3.99k
  vp9_free_svc_cyclic_refresh(cpi);
1076
3.99k
}
1077
1078
0
static void save_coding_context(VP9_COMP *cpi) {
1079
0
  CODING_CONTEXT *const cc = &cpi->coding_context;
1080
0
  VP9_COMMON *cm = &cpi->common;
1081
1082
  // Stores a snapshot of key state variables which can subsequently be
1083
  // restored with a call to vp9_restore_coding_context. These functions are
1084
  // intended for use in a re-code loop in vp9_compress_frame where the
1085
  // quantizer value is adjusted between loop iterations.
1086
0
  vp9_copy(cc->nmvjointcost, cpi->td.mb.nmvjointcost);
1087
1088
0
  memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
1089
0
         MV_VALS * sizeof(*cpi->nmvcosts[0]));
1090
0
  memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
1091
0
         MV_VALS * sizeof(*cpi->nmvcosts[1]));
1092
0
  memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
1093
0
         MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
1094
0
  memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
1095
0
         MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
1096
1097
0
  vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
1098
1099
0
  memcpy(cpi->coding_context.last_frame_seg_map_copy, cm->last_frame_seg_map,
1100
0
         (cm->mi_rows * cm->mi_cols));
1101
1102
0
  vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
1103
0
  vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
1104
1105
0
  cc->fc = *cm->fc;
1106
0
}
1107
1108
0
static void restore_coding_context(VP9_COMP *cpi) {
1109
0
  CODING_CONTEXT *const cc = &cpi->coding_context;
1110
0
  VP9_COMMON *cm = &cpi->common;
1111
1112
  // Restore key state variables to the snapshot state stored in the
1113
  // previous call to vp9_save_coding_context.
1114
0
  vp9_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
1115
1116
0
  memcpy(cpi->nmvcosts[0], cc->nmvcosts[0], MV_VALS * sizeof(*cc->nmvcosts[0]));
1117
0
  memcpy(cpi->nmvcosts[1], cc->nmvcosts[1], MV_VALS * sizeof(*cc->nmvcosts[1]));
1118
0
  memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
1119
0
         MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
1120
0
  memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
1121
0
         MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
1122
1123
0
  vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
1124
1125
0
  memcpy(cm->last_frame_seg_map, cpi->coding_context.last_frame_seg_map_copy,
1126
0
         (cm->mi_rows * cm->mi_cols));
1127
1128
0
  vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
1129
0
  vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
1130
1131
0
  *cm->fc = cc->fc;
1132
0
}
1133
1134
#if !CONFIG_REALTIME_ONLY
1135
0
static void configure_static_seg_features(VP9_COMP *cpi) {
1136
0
  VP9_COMMON *const cm = &cpi->common;
1137
0
  const RATE_CONTROL *const rc = &cpi->rc;
1138
0
  struct segmentation *const seg = &cm->seg;
1139
1140
0
  int high_q = (int)(rc->avg_q > 48.0);
1141
0
  int qi_delta;
1142
1143
  // Disable and clear down for KF
1144
0
  if (cm->frame_type == KEY_FRAME) {
1145
    // Clear down the global segmentation map
1146
0
    memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
1147
0
    seg->update_map = 0;
1148
0
    seg->update_data = 0;
1149
0
    cpi->static_mb_pct = 0;
1150
1151
    // Disable segmentation
1152
0
    vp9_disable_segmentation(seg);
1153
1154
    // Clear down the segment features.
1155
0
    vp9_clearall_segfeatures(seg);
1156
0
  } else if (cpi->refresh_alt_ref_frame) {
1157
    // If this is an alt ref frame
1158
    // Clear down the global segmentation map
1159
0
    memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
1160
0
    seg->update_map = 0;
1161
0
    seg->update_data = 0;
1162
0
    cpi->static_mb_pct = 0;
1163
1164
    // Disable segmentation and individual segment features by default
1165
0
    vp9_disable_segmentation(seg);
1166
0
    vp9_clearall_segfeatures(seg);
1167
1168
    // Scan frames from current to arf frame.
1169
    // This function re-enables segmentation if appropriate.
1170
0
    vp9_update_mbgraph_stats(cpi);
1171
1172
    // If segmentation was enabled set those features needed for the
1173
    // arf itself.
1174
0
    if (seg->enabled) {
1175
0
      seg->update_map = 1;
1176
0
      seg->update_data = 1;
1177
1178
0
      qi_delta =
1179
0
          vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
1180
0
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
1181
0
      vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
1182
1183
0
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
1184
0
      vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
1185
1186
      // Where relevant assume segment data is delta data
1187
0
      seg->abs_delta = SEGMENT_DELTADATA;
1188
0
    }
1189
0
  } else if (seg->enabled) {
1190
    // All other frames if segmentation has been enabled
1191
1192
    // First normal frame in a valid gf or alt ref group
1193
0
    if (rc->frames_since_golden == 0) {
1194
      // Set up segment features for normal frames in an arf group
1195
0
      if (rc->source_alt_ref_active) {
1196
0
        seg->update_map = 0;
1197
0
        seg->update_data = 1;
1198
0
        seg->abs_delta = SEGMENT_DELTADATA;
1199
1200
0
        qi_delta =
1201
0
            vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
1202
0
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
1203
0
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
1204
1205
0
        vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
1206
0
        vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
1207
1208
        // Segment coding disabled for compred testing
1209
0
        if (high_q || (cpi->static_mb_pct == 100)) {
1210
0
          vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1211
0
          vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
1212
0
          vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
1213
0
        }
1214
0
      } else {
1215
        // Disable segmentation and clear down features if alt ref
1216
        // is not active for this group
1217
1218
0
        vp9_disable_segmentation(seg);
1219
1220
0
        memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
1221
1222
0
        seg->update_map = 0;
1223
0
        seg->update_data = 0;
1224
1225
0
        vp9_clearall_segfeatures(seg);
1226
0
      }
1227
0
    } else if (rc->is_src_frame_alt_ref) {
1228
      // Special case where we are coding over the top of a previous
1229
      // alt ref frame.
1230
      // Segment coding disabled for compred testing
1231
1232
      // Enable ref frame features for segment 0 as well
1233
0
      vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
1234
0
      vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
1235
1236
      // All mbs should use ALTREF_FRAME
1237
0
      vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
1238
0
      vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1239
0
      vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
1240
0
      vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
1241
1242
      // Skip all MBs if high Q (0,0 mv and skip coeffs)
1243
0
      if (high_q) {
1244
0
        vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
1245
0
        vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
1246
0
      }
1247
      // Enable data update
1248
0
      seg->update_data = 1;
1249
0
    } else {
1250
      // All other frames.
1251
1252
      // No updates.. leave things as they are.
1253
0
      seg->update_map = 0;
1254
0
      seg->update_data = 0;
1255
0
    }
1256
0
  }
1257
0
}
1258
#endif  // !CONFIG_REALTIME_ONLY
1259
1260
0
static void update_reference_segmentation_map(VP9_COMP *cpi) {
1261
0
  VP9_COMMON *const cm = &cpi->common;
1262
0
  MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
1263
0
  uint8_t *cache_ptr = cm->last_frame_seg_map;
1264
0
  int row, col;
1265
1266
0
  for (row = 0; row < cm->mi_rows; row++) {
1267
0
    MODE_INFO **mi_8x8 = mi_8x8_ptr;
1268
0
    uint8_t *cache = cache_ptr;
1269
0
    for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
1270
0
      cache[0] = mi_8x8[0]->segment_id;
1271
0
    mi_8x8_ptr += cm->mi_stride;
1272
0
    cache_ptr += cm->mi_cols;
1273
0
  }
1274
0
}
1275
1276
53.7k
static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
1277
53.7k
  VP9_COMMON *cm = &cpi->common;
1278
53.7k
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
1279
1280
53.7k
  if (!cpi->lookahead)
1281
3.87k
    cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
1282
3.87k
                                        cm->subsampling_x, cm->subsampling_y,
1283
3.87k
#if CONFIG_VP9_HIGHBITDEPTH
1284
3.87k
                                        cm->use_highbitdepth,
1285
3.87k
#endif
1286
3.87k
                                        oxcf->lag_in_frames);
1287
53.7k
  if (!cpi->lookahead)
1288
0
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1289
0
                       "Failed to allocate lag buffers");
1290
1291
  // TODO(agrange) Check if ARF is enabled and skip allocation if not.
1292
53.7k
  if (vpx_realloc_frame_buffer(&cpi->tf_buffer, oxcf->width, oxcf->height,
1293
53.7k
                               cm->subsampling_x, cm->subsampling_y,
1294
53.7k
#if CONFIG_VP9_HIGHBITDEPTH
1295
53.7k
                               cm->use_highbitdepth,
1296
53.7k
#endif
1297
53.7k
                               VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1298
53.7k
                               NULL, NULL, NULL))
1299
0
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1300
0
                       "Failed to allocate temporal filter buffer");
1301
53.7k
}
1302
1303
57.0k
static void alloc_util_frame_buffers(VP9_COMP *cpi) {
1304
57.0k
  VP9_COMMON *const cm = &cpi->common;
1305
57.0k
  if (vpx_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
1306
57.0k
                               cm->subsampling_x, cm->subsampling_y,
1307
57.0k
#if CONFIG_VP9_HIGHBITDEPTH
1308
57.0k
                               cm->use_highbitdepth,
1309
57.0k
#endif
1310
57.0k
                               VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1311
57.0k
                               NULL, NULL, NULL))
1312
27
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1313
27
                       "Failed to allocate last frame buffer");
1314
1315
57.0k
  if (vpx_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
1316
57.0k
                               cm->subsampling_x, cm->subsampling_y,
1317
57.0k
#if CONFIG_VP9_HIGHBITDEPTH
1318
57.0k
                               cm->use_highbitdepth,
1319
57.0k
#endif
1320
57.0k
                               VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1321
57.0k
                               NULL, NULL, NULL))
1322
0
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1323
0
                       "Failed to allocate scaled source buffer");
1324
1325
  // For 1 pass cbr: allocate scaled_frame that may be used as an intermediate
1326
  // buffer for a 2 stage down-sampling: two stages of 1:2 down-sampling for a
1327
  // target of 1/4x1/4. number_spatial_layers must be greater than 2.
1328
57.0k
  if (is_one_pass_svc(cpi) && !cpi->svc.scaled_temp_is_alloc &&
1329
57.0k
      cpi->svc.number_spatial_layers > 2) {
1330
0
    cpi->svc.scaled_temp_is_alloc = 1;
1331
0
    if (vpx_realloc_frame_buffer(
1332
0
            &cpi->svc.scaled_temp, cm->width >> 1, cm->height >> 1,
1333
0
            cm->subsampling_x, cm->subsampling_y,
1334
0
#if CONFIG_VP9_HIGHBITDEPTH
1335
0
            cm->use_highbitdepth,
1336
0
#endif
1337
0
            VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
1338
0
      vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1339
0
                         "Failed to allocate scaled_frame for svc ");
1340
0
  }
1341
1342
57.0k
  if (vpx_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
1343
57.0k
                               cm->subsampling_x, cm->subsampling_y,
1344
57.0k
#if CONFIG_VP9_HIGHBITDEPTH
1345
57.0k
                               cm->use_highbitdepth,
1346
57.0k
#endif
1347
57.0k
                               VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1348
57.0k
                               NULL, NULL, NULL))
1349
0
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1350
0
                       "Failed to allocate scaled last source buffer");
1351
#ifdef ENABLE_KF_DENOISE
1352
  if (vpx_realloc_frame_buffer(&cpi->raw_unscaled_source, cm->width, cm->height,
1353
                               cm->subsampling_x, cm->subsampling_y,
1354
#if CONFIG_VP9_HIGHBITDEPTH
1355
                               cm->use_highbitdepth,
1356
#endif
1357
                               VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1358
                               NULL, NULL, NULL))
1359
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1360
                       "Failed to allocate unscaled raw source frame buffer");
1361
1362
  if (vpx_realloc_frame_buffer(&cpi->raw_scaled_source, cm->width, cm->height,
1363
                               cm->subsampling_x, cm->subsampling_y,
1364
#if CONFIG_VP9_HIGHBITDEPTH
1365
                               cm->use_highbitdepth,
1366
#endif
1367
                               VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
1368
                               NULL, NULL, NULL))
1369
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1370
                       "Failed to allocate scaled raw source frame buffer");
1371
#endif
1372
57.0k
}
1373
1374
3.99k
static void alloc_context_buffers_ext(VP9_COMP *cpi) {
1375
3.99k
  VP9_COMMON *cm = &cpi->common;
1376
3.99k
  int mi_size = cm->mi_cols * cm->mi_rows;
1377
1378
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->mbmi_ext_base,
1379
3.99k
                  vpx_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
1380
3.99k
}
1381
1382
3.99k
static void alloc_compressor_data(VP9_COMP *cpi) {
1383
3.99k
  VP9_COMMON *cm = &cpi->common;
1384
3.99k
  int sb_rows;
1385
1386
3.99k
  if (vp9_alloc_context_buffers(cm, cm->width, cm->height)) {
1387
0
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1388
0
                       "Failed to allocate context buffers");
1389
0
  }
1390
1391
3.99k
  alloc_context_buffers_ext(cpi);
1392
1393
3.99k
  vpx_free(cpi->tile_tok[0][0]);
1394
1395
3.99k
  {
1396
3.99k
    unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
1397
3.99k
    CHECK_MEM_ERROR(&cm->error, cpi->tile_tok[0][0],
1398
3.99k
                    vpx_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
1399
3.99k
  }
1400
1401
3.99k
  sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
1402
3.99k
  vpx_free(cpi->tplist[0][0]);
1403
3.99k
  CHECK_MEM_ERROR(
1404
3.99k
      &cm->error, cpi->tplist[0][0],
1405
3.99k
      vpx_calloc(sb_rows * 4 * (1 << 6), sizeof(*cpi->tplist[0][0])));
1406
1407
3.99k
  vp9_setup_pc_tree(&cpi->common, &cpi->td);
1408
3.99k
}
1409
1410
87.1k
void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
1411
87.1k
  cpi->framerate = framerate < 0.1 ? 30 : framerate;
1412
87.1k
  vp9_rc_update_framerate(cpi);
1413
87.1k
}
1414
1415
69.0k
static void set_tile_limits(VP9_COMP *cpi) {
1416
69.0k
  VP9_COMMON *const cm = &cpi->common;
1417
1418
69.0k
  int min_log2_tile_cols, max_log2_tile_cols;
1419
69.0k
  vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1420
1421
69.0k
  cm->log2_tile_cols =
1422
69.0k
      clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
1423
69.0k
  cm->log2_tile_rows = cpi->oxcf.tile_rows;
1424
1425
69.0k
  if (cpi->oxcf.target_level == LEVEL_AUTO) {
1426
0
    const int level_tile_cols =
1427
0
        log_tile_cols_from_picsize_level(cpi->common.width, cpi->common.height);
1428
0
    if (cm->log2_tile_cols > level_tile_cols) {
1429
0
      cm->log2_tile_cols = VPXMAX(level_tile_cols, min_log2_tile_cols);
1430
0
    }
1431
0
  }
1432
69.0k
}
1433
1434
34.5k
static void update_frame_size(VP9_COMP *cpi) {
1435
34.5k
  VP9_COMMON *const cm = &cpi->common;
1436
34.5k
  MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1437
1438
34.5k
  vp9_set_mb_mi(cm, cm->width, cm->height);
1439
34.5k
  vp9_init_context_buffers(cm);
1440
34.5k
  vp9_init_macroblockd(cm, xd, NULL);
1441
34.5k
  cpi->td.mb.mbmi_ext_base = cpi->mbmi_ext_base;
1442
34.5k
  memset(cpi->mbmi_ext_base, 0,
1443
34.5k
         cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
1444
1445
34.5k
  set_tile_limits(cpi);
1446
34.5k
}
1447
1448
3.99k
static void init_buffer_indices(VP9_COMP *cpi) {
1449
3.99k
  int ref_frame;
1450
1451
35.9k
  for (ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame)
1452
31.9k
    cpi->ref_fb_idx[ref_frame] = ref_frame;
1453
1454
3.99k
  cpi->lst_fb_idx = cpi->ref_fb_idx[LAST_FRAME - 1];
1455
3.99k
  cpi->gld_fb_idx = cpi->ref_fb_idx[GOLDEN_FRAME - 1];
1456
3.99k
  cpi->alt_fb_idx = cpi->ref_fb_idx[ALTREF_FRAME - 1];
1457
3.99k
}
1458
1459
3.99k
static void init_level_constraint(LevelConstraint *lc) {
1460
3.99k
  lc->level_index = -1;
1461
3.99k
  lc->max_cpb_size = INT_MAX;
1462
3.99k
  lc->max_frame_size = INT_MAX;
1463
3.99k
  lc->fail_flag = 0;
1464
3.99k
}
1465
1466
38.5k
static void set_level_constraint(LevelConstraint *ls, int8_t level_index) {
1467
38.5k
  vpx_clear_system_state();
1468
38.5k
  ls->level_index = level_index;
1469
38.5k
  if (level_index >= 0) {
1470
0
    ls->max_cpb_size = vp9_level_defs[level_index].max_cpb_size * (double)1000;
1471
0
  }
1472
38.5k
}
1473
1474
3.99k
static void init_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
1475
3.99k
  VP9_COMMON *const cm = &cpi->common;
1476
1477
3.99k
  cpi->oxcf = *oxcf;
1478
3.99k
  cpi->framerate = oxcf->init_framerate;
1479
3.99k
  cm->profile = oxcf->profile;
1480
3.99k
  cm->bit_depth = oxcf->bit_depth;
1481
3.99k
#if CONFIG_VP9_HIGHBITDEPTH
1482
3.99k
  cm->use_highbitdepth = oxcf->use_highbitdepth;
1483
3.99k
#endif
1484
3.99k
  cm->color_space = oxcf->color_space;
1485
3.99k
  cm->color_range = oxcf->color_range;
1486
1487
3.99k
  cpi->target_level = oxcf->target_level;
1488
3.99k
  cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
1489
3.99k
  set_level_constraint(&cpi->level_constraint,
1490
3.99k
                       get_level_index(cpi->target_level));
1491
1492
3.99k
  cm->width = oxcf->width;
1493
3.99k
  cm->height = oxcf->height;
1494
3.99k
  alloc_compressor_data(cpi);
1495
1496
3.99k
  cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
1497
1498
  // Single thread case: use counts in common.
1499
3.99k
  cpi->td.counts = &cm->counts;
1500
1501
  // Spatial scalability.
1502
3.99k
  cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
1503
  // Temporal scalability.
1504
3.99k
  cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
1505
1506
3.99k
  if ((cpi->svc.number_temporal_layers > 1) ||
1507
3.99k
      ((cpi->svc.number_temporal_layers > 1 ||
1508
3.99k
        cpi->svc.number_spatial_layers > 1) &&
1509
3.99k
       cpi->oxcf.pass != 1)) {
1510
0
    vp9_init_layer_context(cpi);
1511
0
  }
1512
1513
  // change includes all joint functionality
1514
3.99k
  vp9_change_config(cpi, oxcf);
1515
1516
3.99k
  cpi->static_mb_pct = 0;
1517
3.99k
  cpi->ref_frame_flags = 0;
1518
1519
3.99k
  init_buffer_indices(cpi);
1520
1521
3.99k
  vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
1522
3.99k
  cpi->fixed_qp_onepass = 0;
1523
3.99k
}
1524
1525
34.5k
void vp9_check_reset_rc_flag(VP9_COMP *cpi) {
1526
34.5k
  RATE_CONTROL *rc = &cpi->rc;
1527
1528
34.5k
  if (cpi->common.current_video_frame >
1529
34.5k
      (unsigned int)cpi->svc.number_spatial_layers) {
1530
0
    if (cpi->use_svc) {
1531
0
      vp9_svc_check_reset_layer_rc_flag(cpi);
1532
0
    } else {
1533
0
      if (rc->avg_frame_bandwidth / 3 > (rc->last_avg_frame_bandwidth >> 1) ||
1534
0
          rc->avg_frame_bandwidth < (rc->last_avg_frame_bandwidth >> 1)) {
1535
0
        rc->rc_1_frame = 0;
1536
0
        rc->rc_2_frame = 0;
1537
0
        rc->bits_off_target = rc->optimal_buffer_level;
1538
0
        rc->buffer_level = rc->optimal_buffer_level;
1539
0
      }
1540
0
    }
1541
0
  }
1542
34.5k
}
1543
1544
34.5k
void vp9_set_rc_buffer_sizes(VP9_COMP *cpi) {
1545
34.5k
  RATE_CONTROL *rc = &cpi->rc;
1546
34.5k
  const VP9EncoderConfig *oxcf = &cpi->oxcf;
1547
1548
34.5k
  const int64_t bandwidth = oxcf->target_bandwidth;
1549
34.5k
  const int64_t starting = oxcf->starting_buffer_level_ms;
1550
34.5k
  const int64_t optimal = oxcf->optimal_buffer_level_ms;
1551
34.5k
  const int64_t maximum = oxcf->maximum_buffer_size_ms;
1552
1553
34.5k
  rc->starting_buffer_level = starting * bandwidth / 1000;
1554
34.5k
  rc->optimal_buffer_level =
1555
34.5k
      (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1556
34.5k
  rc->maximum_buffer_size =
1557
34.5k
      (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1558
1559
  // Under a configuration change, where maximum_buffer_size may change,
1560
  // keep buffer level clipped to the maximum allowed buffer size.
1561
34.5k
  rc->bits_off_target = VPXMIN(rc->bits_off_target, rc->maximum_buffer_size);
1562
34.5k
  rc->buffer_level = VPXMIN(rc->buffer_level, rc->maximum_buffer_size);
1563
34.5k
}
1564
1565
#if CONFIG_VP9_HIGHBITDEPTH
1566
#define HIGHBD_BFP(BT, SDF, SDSF, SDAF, VF, SVF, SVAF, SDX4DF, SDSX4DF) \
1567
0
  cpi->fn_ptr[BT].sdf = SDF;                                            \
1568
0
  cpi->fn_ptr[BT].sdsf = SDSF;                                          \
1569
0
  cpi->fn_ptr[BT].sdaf = SDAF;                                          \
1570
0
  cpi->fn_ptr[BT].vf = VF;                                              \
1571
0
  cpi->fn_ptr[BT].svf = SVF;                                            \
1572
0
  cpi->fn_ptr[BT].svaf = SVAF;                                          \
1573
0
  cpi->fn_ptr[BT].sdx4df = SDX4DF;                                      \
1574
0
  cpi->fn_ptr[BT].sdsx4df = SDSX4DF;
1575
1576
#define MAKE_BFP_SAD_WRAPPER(fnname)                                           \
1577
  static unsigned int fnname##_bits8(const uint8_t *src_ptr,                   \
1578
                                     int source_stride,                        \
1579
0
                                     const uint8_t *ref_ptr, int ref_stride) { \
1580
0
    return fnname(src_ptr, source_stride, ref_ptr, ref_stride);                \
1581
0
  }                                                                            \
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x16_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x16_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x32_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x32_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x32_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x32_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x64_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x64_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x32_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x32_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x64_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x64_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x16_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x16_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x8_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x8_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x16_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x16_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x8_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x8_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x4_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x4_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x8_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x8_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x4_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x4_bits8
1582
  static unsigned int fnname##_bits10(                                         \
1583
      const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1584
0
      int ref_stride) {                                                        \
1585
0
    return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2;           \
1586
0
  }                                                                            \
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x16_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x16_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x32_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x32_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x32_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x32_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x64_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x64_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x32_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x32_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x64_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x64_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x16_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x16_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x8_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x8_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x16_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x16_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x8_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x8_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x4_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x4_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x8_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x8_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x4_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x4_bits10
1587
  static unsigned int fnname##_bits12(                                         \
1588
      const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1589
0
      int ref_stride) {                                                        \
1590
0
    return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4;           \
1591
0
  }
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x16_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x16_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x32_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x32_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x32_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x32_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x64_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x64_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x32_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x32_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x64_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x64_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x16_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x16_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x8_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x8_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x16_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x16_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x8_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x8_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x4_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x4_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x8_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x8_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x4_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x4_bits12
1592
1593
#define MAKE_BFP_SADAVG_WRAPPER(fnname)                                        \
1594
  static unsigned int fnname##_bits8(                                          \
1595
      const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1596
0
      int ref_stride, const uint8_t *second_pred) {                            \
1597
0
    return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred);   \
1598
0
  }                                                                            \
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x16_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x32_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x32_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x64_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x32_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x64_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x16_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x8_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x16_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x8_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x4_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x8_avg_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x4_avg_bits8
1599
  static unsigned int fnname##_bits10(                                         \
1600
      const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1601
0
      int ref_stride, const uint8_t *second_pred) {                            \
1602
0
    return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1603
0
           2;                                                                  \
1604
0
  }                                                                            \
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x16_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x32_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x32_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x64_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x32_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x64_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x16_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x8_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x16_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x8_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x4_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x8_avg_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x4_avg_bits10
1605
  static unsigned int fnname##_bits12(                                         \
1606
      const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr,       \
1607
0
      int ref_stride, const uint8_t *second_pred) {                            \
1608
0
    return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1609
0
           4;                                                                  \
1610
0
  }
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x16_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x32_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x32_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x64_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x32_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x64_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x16_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x8_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x16_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x8_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x4_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x8_avg_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x4_avg_bits12
1611
1612
#define MAKE_BFP_SAD4D_WRAPPER(fnname)                                        \
1613
  static void fnname##_bits8(const uint8_t *src_ptr, int source_stride,       \
1614
                             const uint8_t *const ref_ptr[], int ref_stride,  \
1615
0
                             unsigned int *sad_array) {                       \
1616
0
    fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
1617
0
  }                                                                           \
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x16x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x16x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x32x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x32x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x32x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x32x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x64x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x64x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x32x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x32x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x64x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x64x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x16x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x16x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x8x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x8x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x16x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x16x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x8x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x8x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x4x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x4x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x8x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x8x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x4x4d_bits8
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x4x4d_bits8
1618
  static void fnname##_bits10(const uint8_t *src_ptr, int source_stride,      \
1619
                              const uint8_t *const ref_ptr[], int ref_stride, \
1620
0
                              unsigned int *sad_array) {                      \
1621
0
    int i;                                                                    \
1622
0
    fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
1623
0
    for (i = 0; i < 4; i++) sad_array[i] >>= 2;                               \
1624
0
  }                                                                           \
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x16x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x16x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x32x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x32x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x32x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x32x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x64x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x64x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x32x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x32x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x64x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x64x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x16x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x16x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x8x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x8x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x16x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x16x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x8x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x8x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x4x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x4x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x8x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x8x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x4x4d_bits10
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x4x4d_bits10
1625
  static void fnname##_bits12(const uint8_t *src_ptr, int source_stride,      \
1626
                              const uint8_t *const ref_ptr[], int ref_stride, \
1627
0
                              unsigned int *sad_array) {                      \
1628
0
    int i;                                                                    \
1629
0
    fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array);           \
1630
0
    for (i = 0; i < 4; i++) sad_array[i] >>= 4;                               \
1631
0
  }
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x16x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x16x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x32x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x32x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x32x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x32x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x64x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x64x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad32x32x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_32x32x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad64x64x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_64x64x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x16x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x16x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad16x8x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_16x8x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x16x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x16x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x8x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x8x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad8x4x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_8x4x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x8x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x8x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad4x4x4d_bits12
Unexecuted instantiation: vp9_encoder.c:vpx_highbd_sad_skip_4x4x4d_bits12
1632
1633
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x16)
1634
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_32x16)
1635
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x16_avg)
1636
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x16x4d)
1637
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_32x16x4d)
1638
1639
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x32)
1640
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_16x32)
1641
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x32_avg)
1642
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x32x4d)
1643
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_16x32x4d)
1644
1645
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x32)
1646
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_64x32)
1647
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x32_avg)
1648
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x32x4d)
1649
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_64x32x4d)
1650
1651
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x64)
1652
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_32x64)
1653
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x64_avg)
1654
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x64x4d)
1655
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_32x64x4d)
1656
1657
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x32)
1658
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_32x32)
1659
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x32_avg)
1660
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x32x4d)
1661
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_32x32x4d)
1662
1663
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x64)
1664
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_64x64)
1665
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x64_avg)
1666
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x64x4d)
1667
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_64x64x4d)
1668
1669
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x16)
1670
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_16x16)
1671
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x16_avg)
1672
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x16x4d)
1673
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_16x16x4d)
1674
1675
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x8)
1676
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_16x8)
1677
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x8_avg)
1678
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x8x4d)
1679
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_16x8x4d)
1680
1681
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x16)
1682
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_8x16)
1683
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x16_avg)
1684
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x16x4d)
1685
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_8x16x4d)
1686
1687
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x8)
1688
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_8x8)
1689
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x8_avg)
1690
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x8x4d)
1691
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_8x8x4d)
1692
1693
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x4)
1694
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_8x4)
1695
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x4_avg)
1696
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x4x4d)
1697
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_8x4x4d)
1698
1699
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x8)
1700
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_4x8)
1701
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x8_avg)
1702
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x8x4d)
1703
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_4x8x4d)
1704
1705
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x4)
1706
MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad_skip_4x4)
1707
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x4_avg)
1708
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x4x4d)
1709
MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad_skip_4x4x4d)
1710
1711
38.5k
static void highbd_set_var_fns(VP9_COMP *const cpi) {
1712
38.5k
  VP9_COMMON *const cm = &cpi->common;
1713
38.5k
  if (cm->use_highbitdepth) {
1714
0
    switch (cm->bit_depth) {
1715
0
      case VPX_BITS_8:
1716
0
        HIGHBD_BFP(
1717
0
            BLOCK_32X16, vpx_highbd_sad32x16_bits8,
1718
0
            vpx_highbd_sad_skip_32x16_bits8, vpx_highbd_sad32x16_avg_bits8,
1719
0
            vpx_highbd_8_variance32x16, vpx_highbd_8_sub_pixel_variance32x16,
1720
0
            vpx_highbd_8_sub_pixel_avg_variance32x16,
1721
0
            vpx_highbd_sad32x16x4d_bits8, vpx_highbd_sad_skip_32x16x4d_bits8)
1722
1723
0
        HIGHBD_BFP(
1724
0
            BLOCK_16X32, vpx_highbd_sad16x32_bits8,
1725
0
            vpx_highbd_sad_skip_16x32_bits8, vpx_highbd_sad16x32_avg_bits8,
1726
0
            vpx_highbd_8_variance16x32, vpx_highbd_8_sub_pixel_variance16x32,
1727
0
            vpx_highbd_8_sub_pixel_avg_variance16x32,
1728
0
            vpx_highbd_sad16x32x4d_bits8, vpx_highbd_sad_skip_16x32x4d_bits8)
1729
1730
0
        HIGHBD_BFP(
1731
0
            BLOCK_64X32, vpx_highbd_sad64x32_bits8,
1732
0
            vpx_highbd_sad_skip_64x32_bits8, vpx_highbd_sad64x32_avg_bits8,
1733
0
            vpx_highbd_8_variance64x32, vpx_highbd_8_sub_pixel_variance64x32,
1734
0
            vpx_highbd_8_sub_pixel_avg_variance64x32,
1735
0
            vpx_highbd_sad64x32x4d_bits8, vpx_highbd_sad_skip_64x32x4d_bits8)
1736
1737
0
        HIGHBD_BFP(
1738
0
            BLOCK_32X64, vpx_highbd_sad32x64_bits8,
1739
0
            vpx_highbd_sad_skip_32x64_bits8, vpx_highbd_sad32x64_avg_bits8,
1740
0
            vpx_highbd_8_variance32x64, vpx_highbd_8_sub_pixel_variance32x64,
1741
0
            vpx_highbd_8_sub_pixel_avg_variance32x64,
1742
0
            vpx_highbd_sad32x64x4d_bits8, vpx_highbd_sad_skip_32x64x4d_bits8)
1743
1744
0
        HIGHBD_BFP(
1745
0
            BLOCK_32X32, vpx_highbd_sad32x32_bits8,
1746
0
            vpx_highbd_sad_skip_32x32_bits8, vpx_highbd_sad32x32_avg_bits8,
1747
0
            vpx_highbd_8_variance32x32, vpx_highbd_8_sub_pixel_variance32x32,
1748
0
            vpx_highbd_8_sub_pixel_avg_variance32x32,
1749
0
            vpx_highbd_sad32x32x4d_bits8, vpx_highbd_sad_skip_32x32x4d_bits8)
1750
1751
0
        HIGHBD_BFP(
1752
0
            BLOCK_64X64, vpx_highbd_sad64x64_bits8,
1753
0
            vpx_highbd_sad_skip_64x64_bits8, vpx_highbd_sad64x64_avg_bits8,
1754
0
            vpx_highbd_8_variance64x64, vpx_highbd_8_sub_pixel_variance64x64,
1755
0
            vpx_highbd_8_sub_pixel_avg_variance64x64,
1756
0
            vpx_highbd_sad64x64x4d_bits8, vpx_highbd_sad_skip_64x64x4d_bits8)
1757
1758
0
        HIGHBD_BFP(
1759
0
            BLOCK_16X16, vpx_highbd_sad16x16_bits8,
1760
0
            vpx_highbd_sad_skip_16x16_bits8, vpx_highbd_sad16x16_avg_bits8,
1761
0
            vpx_highbd_8_variance16x16, vpx_highbd_8_sub_pixel_variance16x16,
1762
0
            vpx_highbd_8_sub_pixel_avg_variance16x16,
1763
0
            vpx_highbd_sad16x16x4d_bits8, vpx_highbd_sad_skip_16x16x4d_bits8)
1764
1765
0
        HIGHBD_BFP(
1766
0
            BLOCK_16X8, vpx_highbd_sad16x8_bits8,
1767
0
            vpx_highbd_sad_skip_16x8_bits8, vpx_highbd_sad16x8_avg_bits8,
1768
0
            vpx_highbd_8_variance16x8, vpx_highbd_8_sub_pixel_variance16x8,
1769
0
            vpx_highbd_8_sub_pixel_avg_variance16x8,
1770
0
            vpx_highbd_sad16x8x4d_bits8, vpx_highbd_sad_skip_16x8x4d_bits8)
1771
1772
0
        HIGHBD_BFP(
1773
0
            BLOCK_8X16, vpx_highbd_sad8x16_bits8,
1774
0
            vpx_highbd_sad_skip_8x16_bits8, vpx_highbd_sad8x16_avg_bits8,
1775
0
            vpx_highbd_8_variance8x16, vpx_highbd_8_sub_pixel_variance8x16,
1776
0
            vpx_highbd_8_sub_pixel_avg_variance8x16,
1777
0
            vpx_highbd_sad8x16x4d_bits8, vpx_highbd_sad_skip_8x16x4d_bits8)
1778
1779
0
        HIGHBD_BFP(BLOCK_8X8, vpx_highbd_sad8x8_bits8,
1780
0
                   vpx_highbd_sad_skip_8x8_bits8, vpx_highbd_sad8x8_avg_bits8,
1781
0
                   vpx_highbd_8_variance8x8, vpx_highbd_8_sub_pixel_variance8x8,
1782
0
                   vpx_highbd_8_sub_pixel_avg_variance8x8,
1783
0
                   vpx_highbd_sad8x8x4d_bits8, vpx_highbd_sad_skip_8x8x4d_bits8)
1784
1785
0
        HIGHBD_BFP(BLOCK_8X4, vpx_highbd_sad8x4_bits8,
1786
0
                   vpx_highbd_sad_skip_8x4_bits8, vpx_highbd_sad8x4_avg_bits8,
1787
0
                   vpx_highbd_8_variance8x4, vpx_highbd_8_sub_pixel_variance8x4,
1788
0
                   vpx_highbd_8_sub_pixel_avg_variance8x4,
1789
0
                   vpx_highbd_sad8x4x4d_bits8, vpx_highbd_sad_skip_8x4x4d_bits8)
1790
1791
0
        HIGHBD_BFP(BLOCK_4X8, vpx_highbd_sad4x8_bits8,
1792
0
                   vpx_highbd_sad_skip_4x8_bits8, vpx_highbd_sad4x8_avg_bits8,
1793
0
                   vpx_highbd_8_variance4x8, vpx_highbd_8_sub_pixel_variance4x8,
1794
0
                   vpx_highbd_8_sub_pixel_avg_variance4x8,
1795
0
                   vpx_highbd_sad4x8x4d_bits8, vpx_highbd_sad_skip_4x8x4d_bits8)
1796
1797
0
        HIGHBD_BFP(BLOCK_4X4, vpx_highbd_sad4x4_bits8,
1798
0
                   vpx_highbd_sad_skip_4x4_bits8, vpx_highbd_sad4x4_avg_bits8,
1799
0
                   vpx_highbd_8_variance4x4, vpx_highbd_8_sub_pixel_variance4x4,
1800
0
                   vpx_highbd_8_sub_pixel_avg_variance4x4,
1801
0
                   vpx_highbd_sad4x4x4d_bits8, vpx_highbd_sad_skip_4x4x4d_bits8)
1802
0
        break;
1803
1804
0
      case VPX_BITS_10:
1805
0
        HIGHBD_BFP(
1806
0
            BLOCK_32X16, vpx_highbd_sad32x16_bits10,
1807
0
            vpx_highbd_sad_skip_32x16_bits10, vpx_highbd_sad32x16_avg_bits10,
1808
0
            vpx_highbd_10_variance32x16, vpx_highbd_10_sub_pixel_variance32x16,
1809
0
            vpx_highbd_10_sub_pixel_avg_variance32x16,
1810
0
            vpx_highbd_sad32x16x4d_bits10, vpx_highbd_sad_skip_32x16x4d_bits10)
1811
1812
0
        HIGHBD_BFP(
1813
0
            BLOCK_16X32, vpx_highbd_sad16x32_bits10,
1814
0
            vpx_highbd_sad_skip_16x32_bits10, vpx_highbd_sad16x32_avg_bits10,
1815
0
            vpx_highbd_10_variance16x32, vpx_highbd_10_sub_pixel_variance16x32,
1816
0
            vpx_highbd_10_sub_pixel_avg_variance16x32,
1817
0
            vpx_highbd_sad16x32x4d_bits10, vpx_highbd_sad_skip_16x32x4d_bits10)
1818
1819
0
        HIGHBD_BFP(
1820
0
            BLOCK_64X32, vpx_highbd_sad64x32_bits10,
1821
0
            vpx_highbd_sad_skip_64x32_bits10, vpx_highbd_sad64x32_avg_bits10,
1822
0
            vpx_highbd_10_variance64x32, vpx_highbd_10_sub_pixel_variance64x32,
1823
0
            vpx_highbd_10_sub_pixel_avg_variance64x32,
1824
0
            vpx_highbd_sad64x32x4d_bits10, vpx_highbd_sad_skip_64x32x4d_bits10)
1825
1826
0
        HIGHBD_BFP(
1827
0
            BLOCK_32X64, vpx_highbd_sad32x64_bits10,
1828
0
            vpx_highbd_sad_skip_32x64_bits10, vpx_highbd_sad32x64_avg_bits10,
1829
0
            vpx_highbd_10_variance32x64, vpx_highbd_10_sub_pixel_variance32x64,
1830
0
            vpx_highbd_10_sub_pixel_avg_variance32x64,
1831
0
            vpx_highbd_sad32x64x4d_bits10, vpx_highbd_sad_skip_32x64x4d_bits10)
1832
1833
0
        HIGHBD_BFP(
1834
0
            BLOCK_32X32, vpx_highbd_sad32x32_bits10,
1835
0
            vpx_highbd_sad_skip_32x32_bits10, vpx_highbd_sad32x32_avg_bits10,
1836
0
            vpx_highbd_10_variance32x32, vpx_highbd_10_sub_pixel_variance32x32,
1837
0
            vpx_highbd_10_sub_pixel_avg_variance32x32,
1838
0
            vpx_highbd_sad32x32x4d_bits10, vpx_highbd_sad_skip_32x32x4d_bits10)
1839
1840
0
        HIGHBD_BFP(
1841
0
            BLOCK_64X64, vpx_highbd_sad64x64_bits10,
1842
0
            vpx_highbd_sad_skip_64x64_bits10, vpx_highbd_sad64x64_avg_bits10,
1843
0
            vpx_highbd_10_variance64x64, vpx_highbd_10_sub_pixel_variance64x64,
1844
0
            vpx_highbd_10_sub_pixel_avg_variance64x64,
1845
0
            vpx_highbd_sad64x64x4d_bits10, vpx_highbd_sad_skip_64x64x4d_bits10)
1846
1847
0
        HIGHBD_BFP(
1848
0
            BLOCK_16X16, vpx_highbd_sad16x16_bits10,
1849
0
            vpx_highbd_sad_skip_16x16_bits10, vpx_highbd_sad16x16_avg_bits10,
1850
0
            vpx_highbd_10_variance16x16, vpx_highbd_10_sub_pixel_variance16x16,
1851
0
            vpx_highbd_10_sub_pixel_avg_variance16x16,
1852
0
            vpx_highbd_sad16x16x4d_bits10, vpx_highbd_sad_skip_16x16x4d_bits10)
1853
1854
0
        HIGHBD_BFP(
1855
0
            BLOCK_16X8, vpx_highbd_sad16x8_bits10,
1856
0
            vpx_highbd_sad_skip_16x8_bits10, vpx_highbd_sad16x8_avg_bits10,
1857
0
            vpx_highbd_10_variance16x8, vpx_highbd_10_sub_pixel_variance16x8,
1858
0
            vpx_highbd_10_sub_pixel_avg_variance16x8,
1859
0
            vpx_highbd_sad16x8x4d_bits10, vpx_highbd_sad_skip_16x8x4d_bits10)
1860
1861
0
        HIGHBD_BFP(
1862
0
            BLOCK_8X16, vpx_highbd_sad8x16_bits10,
1863
0
            vpx_highbd_sad_skip_8x16_bits10, vpx_highbd_sad8x16_avg_bits10,
1864
0
            vpx_highbd_10_variance8x16, vpx_highbd_10_sub_pixel_variance8x16,
1865
0
            vpx_highbd_10_sub_pixel_avg_variance8x16,
1866
0
            vpx_highbd_sad8x16x4d_bits10, vpx_highbd_sad_skip_8x16x4d_bits10)
1867
1868
0
        HIGHBD_BFP(
1869
0
            BLOCK_8X8, vpx_highbd_sad8x8_bits10, vpx_highbd_sad_skip_8x8_bits10,
1870
0
            vpx_highbd_sad8x8_avg_bits10, vpx_highbd_10_variance8x8,
1871
0
            vpx_highbd_10_sub_pixel_variance8x8,
1872
0
            vpx_highbd_10_sub_pixel_avg_variance8x8,
1873
0
            vpx_highbd_sad8x8x4d_bits10, vpx_highbd_sad_skip_8x8x4d_bits10)
1874
1875
0
        HIGHBD_BFP(
1876
0
            BLOCK_8X4, vpx_highbd_sad8x4_bits10, vpx_highbd_sad_skip_8x4_bits10,
1877
0
            vpx_highbd_sad8x4_avg_bits10, vpx_highbd_10_variance8x4,
1878
0
            vpx_highbd_10_sub_pixel_variance8x4,
1879
0
            vpx_highbd_10_sub_pixel_avg_variance8x4,
1880
0
            vpx_highbd_sad8x4x4d_bits10, vpx_highbd_sad_skip_8x4x4d_bits10)
1881
1882
0
        HIGHBD_BFP(
1883
0
            BLOCK_4X8, vpx_highbd_sad4x8_bits10, vpx_highbd_sad_skip_4x8_bits10,
1884
0
            vpx_highbd_sad4x8_avg_bits10, vpx_highbd_10_variance4x8,
1885
0
            vpx_highbd_10_sub_pixel_variance4x8,
1886
0
            vpx_highbd_10_sub_pixel_avg_variance4x8,
1887
0
            vpx_highbd_sad4x8x4d_bits10, vpx_highbd_sad_skip_4x8x4d_bits10)
1888
1889
0
        HIGHBD_BFP(
1890
0
            BLOCK_4X4, vpx_highbd_sad4x4_bits10, vpx_highbd_sad_skip_4x4_bits10,
1891
0
            vpx_highbd_sad4x4_avg_bits10, vpx_highbd_10_variance4x4,
1892
0
            vpx_highbd_10_sub_pixel_variance4x4,
1893
0
            vpx_highbd_10_sub_pixel_avg_variance4x4,
1894
0
            vpx_highbd_sad4x4x4d_bits10, vpx_highbd_sad_skip_4x4x4d_bits10)
1895
0
        break;
1896
1897
0
      default:
1898
0
        assert(cm->bit_depth == VPX_BITS_12);
1899
0
        HIGHBD_BFP(
1900
0
            BLOCK_32X16, vpx_highbd_sad32x16_bits12,
1901
0
            vpx_highbd_sad_skip_32x16_bits12, vpx_highbd_sad32x16_avg_bits12,
1902
0
            vpx_highbd_12_variance32x16, vpx_highbd_12_sub_pixel_variance32x16,
1903
0
            vpx_highbd_12_sub_pixel_avg_variance32x16,
1904
0
            vpx_highbd_sad32x16x4d_bits12, vpx_highbd_sad_skip_32x16x4d_bits12)
1905
1906
0
        HIGHBD_BFP(
1907
0
            BLOCK_16X32, vpx_highbd_sad16x32_bits12,
1908
0
            vpx_highbd_sad_skip_16x32_bits12, vpx_highbd_sad16x32_avg_bits12,
1909
0
            vpx_highbd_12_variance16x32, vpx_highbd_12_sub_pixel_variance16x32,
1910
0
            vpx_highbd_12_sub_pixel_avg_variance16x32,
1911
0
            vpx_highbd_sad16x32x4d_bits12, vpx_highbd_sad_skip_16x32x4d_bits12)
1912
1913
0
        HIGHBD_BFP(
1914
0
            BLOCK_64X32, vpx_highbd_sad64x32_bits12,
1915
0
            vpx_highbd_sad_skip_64x32_bits12, vpx_highbd_sad64x32_avg_bits12,
1916
0
            vpx_highbd_12_variance64x32, vpx_highbd_12_sub_pixel_variance64x32,
1917
0
            vpx_highbd_12_sub_pixel_avg_variance64x32,
1918
0
            vpx_highbd_sad64x32x4d_bits12, vpx_highbd_sad_skip_64x32x4d_bits12)
1919
1920
0
        HIGHBD_BFP(
1921
0
            BLOCK_32X64, vpx_highbd_sad32x64_bits12,
1922
0
            vpx_highbd_sad_skip_32x64_bits12, vpx_highbd_sad32x64_avg_bits12,
1923
0
            vpx_highbd_12_variance32x64, vpx_highbd_12_sub_pixel_variance32x64,
1924
0
            vpx_highbd_12_sub_pixel_avg_variance32x64,
1925
0
            vpx_highbd_sad32x64x4d_bits12, vpx_highbd_sad_skip_32x64x4d_bits12)
1926
1927
0
        HIGHBD_BFP(
1928
0
            BLOCK_32X32, vpx_highbd_sad32x32_bits12,
1929
0
            vpx_highbd_sad_skip_32x32_bits12, vpx_highbd_sad32x32_avg_bits12,
1930
0
            vpx_highbd_12_variance32x32, vpx_highbd_12_sub_pixel_variance32x32,
1931
0
            vpx_highbd_12_sub_pixel_avg_variance32x32,
1932
0
            vpx_highbd_sad32x32x4d_bits12, vpx_highbd_sad_skip_32x32x4d_bits12)
1933
1934
0
        HIGHBD_BFP(
1935
0
            BLOCK_64X64, vpx_highbd_sad64x64_bits12,
1936
0
            vpx_highbd_sad_skip_64x64_bits12, vpx_highbd_sad64x64_avg_bits12,
1937
0
            vpx_highbd_12_variance64x64, vpx_highbd_12_sub_pixel_variance64x64,
1938
0
            vpx_highbd_12_sub_pixel_avg_variance64x64,
1939
0
            vpx_highbd_sad64x64x4d_bits12, vpx_highbd_sad_skip_64x64x4d_bits12)
1940
1941
0
        HIGHBD_BFP(
1942
0
            BLOCK_16X16, vpx_highbd_sad16x16_bits12,
1943
0
            vpx_highbd_sad_skip_16x16_bits12, vpx_highbd_sad16x16_avg_bits12,
1944
0
            vpx_highbd_12_variance16x16, vpx_highbd_12_sub_pixel_variance16x16,
1945
0
            vpx_highbd_12_sub_pixel_avg_variance16x16,
1946
0
            vpx_highbd_sad16x16x4d_bits12, vpx_highbd_sad_skip_16x16x4d_bits12)
1947
1948
0
        HIGHBD_BFP(
1949
0
            BLOCK_16X8, vpx_highbd_sad16x8_bits12,
1950
0
            vpx_highbd_sad_skip_16x8_bits12, vpx_highbd_sad16x8_avg_bits12,
1951
0
            vpx_highbd_12_variance16x8, vpx_highbd_12_sub_pixel_variance16x8,
1952
0
            vpx_highbd_12_sub_pixel_avg_variance16x8,
1953
0
            vpx_highbd_sad16x8x4d_bits12, vpx_highbd_sad_skip_16x8x4d_bits12)
1954
1955
0
        HIGHBD_BFP(
1956
0
            BLOCK_8X16, vpx_highbd_sad8x16_bits12,
1957
0
            vpx_highbd_sad_skip_8x16_bits12, vpx_highbd_sad8x16_avg_bits12,
1958
0
            vpx_highbd_12_variance8x16, vpx_highbd_12_sub_pixel_variance8x16,
1959
0
            vpx_highbd_12_sub_pixel_avg_variance8x16,
1960
0
            vpx_highbd_sad8x16x4d_bits12, vpx_highbd_sad_skip_8x16x4d_bits12)
1961
1962
0
        HIGHBD_BFP(
1963
0
            BLOCK_8X8, vpx_highbd_sad8x8_bits12, vpx_highbd_sad_skip_8x8_bits12,
1964
0
            vpx_highbd_sad8x8_avg_bits12, vpx_highbd_12_variance8x8,
1965
0
            vpx_highbd_12_sub_pixel_variance8x8,
1966
0
            vpx_highbd_12_sub_pixel_avg_variance8x8,
1967
0
            vpx_highbd_sad8x8x4d_bits12, vpx_highbd_sad_skip_8x8x4d_bits12)
1968
1969
0
        HIGHBD_BFP(
1970
0
            BLOCK_8X4, vpx_highbd_sad8x4_bits12, vpx_highbd_sad_skip_8x4_bits12,
1971
0
            vpx_highbd_sad8x4_avg_bits12, vpx_highbd_12_variance8x4,
1972
0
            vpx_highbd_12_sub_pixel_variance8x4,
1973
0
            vpx_highbd_12_sub_pixel_avg_variance8x4,
1974
0
            vpx_highbd_sad8x4x4d_bits12, vpx_highbd_sad_skip_8x4x4d_bits12)
1975
1976
0
        HIGHBD_BFP(
1977
0
            BLOCK_4X8, vpx_highbd_sad4x8_bits12, vpx_highbd_sad_skip_4x8_bits12,
1978
0
            vpx_highbd_sad4x8_avg_bits12, vpx_highbd_12_variance4x8,
1979
0
            vpx_highbd_12_sub_pixel_variance4x8,
1980
0
            vpx_highbd_12_sub_pixel_avg_variance4x8,
1981
0
            vpx_highbd_sad4x8x4d_bits12, vpx_highbd_sad_skip_4x8x4d_bits12)
1982
1983
0
        HIGHBD_BFP(
1984
0
            BLOCK_4X4, vpx_highbd_sad4x4_bits12, vpx_highbd_sad_skip_4x4_bits12,
1985
0
            vpx_highbd_sad4x4_avg_bits12, vpx_highbd_12_variance4x4,
1986
0
            vpx_highbd_12_sub_pixel_variance4x4,
1987
0
            vpx_highbd_12_sub_pixel_avg_variance4x4,
1988
0
            vpx_highbd_sad4x4x4d_bits12, vpx_highbd_sad_skip_4x4x4d_bits12)
1989
0
        break;
1990
0
    }
1991
0
  }
1992
38.5k
}
1993
#endif  // CONFIG_VP9_HIGHBITDEPTH
1994
1995
3.99k
static void realloc_segmentation_maps(VP9_COMP *cpi) {
1996
3.99k
  VP9_COMMON *const cm = &cpi->common;
1997
1998
  // Create the encoder segmentation map and set all entries to 0
1999
3.99k
  vpx_free(cpi->segmentation_map);
2000
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->segmentation_map,
2001
3.99k
                  vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
2002
2003
  // Create a map used for cyclic background refresh.
2004
3.99k
  if (cpi->cyclic_refresh) vp9_cyclic_refresh_free(cpi->cyclic_refresh);
2005
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->cyclic_refresh,
2006
3.99k
                  vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
2007
2008
  // Create a map used to mark inactive areas.
2009
3.99k
  vpx_free(cpi->active_map.map);
2010
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->active_map.map,
2011
3.99k
                  vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
2012
2013
  // And a place holder structure is the coding context
2014
  // for use if we want to save and restore it
2015
3.99k
  vpx_free(cpi->coding_context.last_frame_seg_map_copy);
2016
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->coding_context.last_frame_seg_map_copy,
2017
3.99k
                  vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
2018
3.99k
}
2019
2020
0
static void alloc_copy_partition_data(VP9_COMP *cpi) {
2021
0
  VP9_COMMON *const cm = &cpi->common;
2022
0
  if (cpi->prev_partition == NULL) {
2023
0
    CHECK_MEM_ERROR(&cm->error, cpi->prev_partition,
2024
0
                    (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
2025
0
                                             sizeof(*cpi->prev_partition)));
2026
0
  }
2027
0
  if (cpi->prev_segment_id == NULL) {
2028
0
    CHECK_MEM_ERROR(
2029
0
        &cm->error, cpi->prev_segment_id,
2030
0
        (int8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
2031
0
                             sizeof(*cpi->prev_segment_id)));
2032
0
  }
2033
0
  if (cpi->prev_variance_low == NULL) {
2034
0
    CHECK_MEM_ERROR(&cm->error, cpi->prev_variance_low,
2035
0
                    (uint8_t *)vpx_calloc(
2036
0
                        (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) * 25,
2037
0
                        sizeof(*cpi->prev_variance_low)));
2038
0
  }
2039
0
  if (cpi->copied_frame_cnt == NULL) {
2040
0
    CHECK_MEM_ERROR(
2041
0
        &cm->error, cpi->copied_frame_cnt,
2042
0
        (uint8_t *)vpx_calloc((cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1),
2043
0
                              sizeof(*cpi->copied_frame_cnt)));
2044
0
  }
2045
0
}
2046
2047
0
static void free_copy_partition_data(VP9_COMP *cpi) {
2048
0
  vpx_free(cpi->prev_partition);
2049
0
  cpi->prev_partition = NULL;
2050
0
  vpx_free(cpi->prev_segment_id);
2051
0
  cpi->prev_segment_id = NULL;
2052
0
  vpx_free(cpi->prev_variance_low);
2053
0
  cpi->prev_variance_low = NULL;
2054
0
  vpx_free(cpi->copied_frame_cnt);
2055
0
  cpi->copied_frame_cnt = NULL;
2056
0
}
2057
2058
#if CONFIG_VP9_TEMPORAL_DENOISING
2059
static void setup_denoiser_buffer(VP9_COMP *cpi) {
2060
  VP9_COMMON *const cm = &cpi->common;
2061
  if (cpi->oxcf.noise_sensitivity > 0 &&
2062
      !cpi->denoiser.frame_buffer_initialized) {
2063
    if (vp9_denoiser_alloc(cm, &cpi->svc, &cpi->denoiser, cpi->use_svc,
2064
                           cpi->oxcf.noise_sensitivity, cm->width, cm->height,
2065
                           cm->subsampling_x, cm->subsampling_y,
2066
#if CONFIG_VP9_HIGHBITDEPTH
2067
                           cm->use_highbitdepth,
2068
#endif
2069
                           VP9_ENC_BORDER_IN_PIXELS))
2070
      vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2071
                         "Failed to allocate denoiser");
2072
  }
2073
}
2074
#endif
2075
2076
34.5k
void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
2077
34.5k
  VP9_COMMON *const cm = &cpi->common;
2078
34.5k
  RATE_CONTROL *const rc = &cpi->rc;
2079
34.5k
  int last_w = cpi->oxcf.width;
2080
34.5k
  int last_h = cpi->oxcf.height;
2081
2082
34.5k
  vp9_init_quantizer(cpi);
2083
34.5k
  if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
2084
34.5k
  cm->bit_depth = oxcf->bit_depth;
2085
34.5k
  cm->color_space = oxcf->color_space;
2086
34.5k
  cm->color_range = oxcf->color_range;
2087
2088
34.5k
  cpi->target_level = oxcf->target_level;
2089
34.5k
  cpi->keep_level_stats = oxcf->target_level != LEVEL_MAX;
2090
34.5k
  set_level_constraint(&cpi->level_constraint,
2091
34.5k
                       get_level_index(cpi->target_level));
2092
2093
34.5k
  if (cm->profile <= PROFILE_1)
2094
34.5k
    assert(cm->bit_depth == VPX_BITS_8);
2095
0
  else
2096
0
    assert(cm->bit_depth > VPX_BITS_8);
2097
2098
34.5k
  cpi->oxcf = *oxcf;
2099
34.5k
#if CONFIG_VP9_HIGHBITDEPTH
2100
34.5k
  cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
2101
34.5k
#endif  // CONFIG_VP9_HIGHBITDEPTH
2102
2103
34.5k
  if ((oxcf->pass == 0) && (oxcf->rc_mode == VPX_Q)) {
2104
1.82k
    rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2105
32.7k
  } else {
2106
32.7k
    rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2107
32.7k
  }
2108
2109
34.5k
  cpi->refresh_golden_frame = 0;
2110
34.5k
  cpi->refresh_last_frame = 1;
2111
34.5k
  cm->refresh_frame_context = 1;
2112
34.5k
  cm->reset_frame_context = 0;
2113
2114
34.5k
  vp9_reset_segment_features(&cm->seg);
2115
34.5k
  vp9_set_high_precision_mv(cpi, 0);
2116
2117
34.5k
  {
2118
34.5k
    int i;
2119
2120
310k
    for (i = 0; i < MAX_SEGMENTS; i++)
2121
276k
      cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
2122
34.5k
  }
2123
34.5k
  cpi->encode_breakout = cpi->oxcf.encode_breakout;
2124
2125
34.5k
  vp9_set_rc_buffer_sizes(cpi);
2126
2127
  // Set up frame rate and related parameters rate control values.
2128
34.5k
  vp9_new_framerate(cpi, cpi->framerate);
2129
2130
  // Set absolute upper and lower quality limits
2131
34.5k
  rc->worst_quality = cpi->oxcf.worst_allowed_q;
2132
34.5k
  rc->best_quality = cpi->oxcf.best_allowed_q;
2133
2134
34.5k
  cm->interp_filter = cpi->sf.default_interp_filter;
2135
2136
34.5k
  if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2137
0
    cm->render_width = cpi->oxcf.render_width;
2138
0
    cm->render_height = cpi->oxcf.render_height;
2139
34.5k
  } else {
2140
34.5k
    cm->render_width = cpi->oxcf.width;
2141
34.5k
    cm->render_height = cpi->oxcf.height;
2142
34.5k
  }
2143
34.5k
  if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
2144
0
    cm->width = cpi->oxcf.width;
2145
0
    cm->height = cpi->oxcf.height;
2146
0
    cpi->external_resize = 1;
2147
0
  }
2148
2149
34.5k
  int new_mi_size = 0;
2150
34.5k
  vp9_set_mb_mi(cm, cm->width, cm->height);
2151
34.5k
  new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
2152
34.5k
  if (cm->mi_alloc_size < new_mi_size) {
2153
0
    vp9_free_context_buffers(cm);
2154
0
    vp9_free_pc_tree(&cpi->td);
2155
0
    vpx_free(cpi->mbmi_ext_base);
2156
0
    alloc_compressor_data(cpi);
2157
0
    realloc_segmentation_maps(cpi);
2158
0
    cpi->initial_width = cpi->initial_height = 0;
2159
0
    cpi->external_resize = 0;
2160
34.5k
  } else if (cm->mi_alloc_size == new_mi_size &&
2161
34.5k
             (cpi->oxcf.width > last_w || cpi->oxcf.height > last_h)) {
2162
0
    if (vp9_alloc_loop_filter(cm)) {
2163
0
      vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2164
0
                         "Failed to allocate loop filter data");
2165
0
    }
2166
0
  }
2167
2168
34.5k
  if (cm->current_video_frame == 0 || last_w != cpi->oxcf.width ||
2169
34.5k
      last_h != cpi->oxcf.height)
2170
34.5k
    update_frame_size(cpi);
2171
2172
34.5k
  if (last_w != cpi->oxcf.width || last_h != cpi->oxcf.height) {
2173
0
    vpx_free(cpi->consec_zero_mv);
2174
0
    CHECK_MEM_ERROR(
2175
0
        &cm->error, cpi->consec_zero_mv,
2176
0
        vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
2177
2178
0
    vpx_free(cpi->skin_map);
2179
0
    CHECK_MEM_ERROR(
2180
0
        &cm->error, cpi->skin_map,
2181
0
        vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->skin_map)));
2182
2183
0
    if (cpi->svc.number_spatial_layers > 1) {
2184
#if CONFIG_VP9_TEMPORAL_DENOISING
2185
      // Reset the denoiser for svc on the resize change.
2186
      if (cpi->oxcf.noise_sensitivity > 0) {
2187
        vp9_denoiser_free(&cpi->denoiser);
2188
        setup_denoiser_buffer(cpi);
2189
      }
2190
#endif
2191
0
      if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
2192
0
        for (int sl = 0; sl < cpi->svc.number_spatial_layers; ++sl) {
2193
0
          const int layer =
2194
0
              LAYER_IDS_TO_IDX(sl, 0, cpi->svc.number_temporal_layers);
2195
0
          LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer];
2196
0
          lc->sb_index = 0;
2197
0
          lc->actual_num_seg1_blocks = 0;
2198
0
          lc->actual_num_seg2_blocks = 0;
2199
0
          lc->counter_encode_maxq_scene_change = 0;
2200
0
          vpx_free(lc->map);
2201
0
          CHECK_MEM_ERROR(
2202
0
              &cm->error, lc->map,
2203
0
              vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*lc->map)));
2204
0
          vpx_free(lc->last_coded_q_map);
2205
0
          CHECK_MEM_ERROR(&cm->error, lc->last_coded_q_map,
2206
0
                          vpx_malloc(cm->mi_rows * cm->mi_cols *
2207
0
                                     sizeof(*lc->last_coded_q_map)));
2208
0
          memset(lc->last_coded_q_map, MAXQ, cm->mi_rows * cm->mi_cols);
2209
0
          vpx_free(lc->consec_zero_mv);
2210
0
          CHECK_MEM_ERROR(&cm->error, lc->consec_zero_mv,
2211
0
                          vpx_calloc(cm->mi_rows * cm->mi_cols,
2212
0
                                     sizeof(*lc->consec_zero_mv)));
2213
0
        }
2214
0
        cpi->refresh_golden_frame = 1;
2215
0
        cpi->refresh_alt_ref_frame = 1;
2216
0
      }
2217
0
    }
2218
2219
0
    free_copy_partition_data(cpi);
2220
0
    alloc_copy_partition_data(cpi);
2221
0
    if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
2222
0
        cpi->svc.number_spatial_layers == 1)
2223
0
      vp9_cyclic_refresh_reset_resize(cpi);
2224
0
    rc->rc_1_frame = 0;
2225
0
    rc->rc_2_frame = 0;
2226
0
  }
2227
2228
34.5k
  if ((cpi->svc.number_temporal_layers > 1) ||
2229
34.5k
      ((cpi->svc.number_temporal_layers > 1 ||
2230
34.5k
        cpi->svc.number_spatial_layers > 1) &&
2231
34.5k
       cpi->oxcf.pass != 1)) {
2232
0
    vp9_update_layer_context_change_config(cpi,
2233
0
                                           (int)cpi->oxcf.target_bandwidth);
2234
0
  }
2235
2236
34.5k
  vp9_check_reset_rc_flag(cpi);
2237
2238
34.5k
  cpi->alt_ref_source = NULL;
2239
34.5k
  rc->is_src_frame_alt_ref = 0;
2240
2241
#if 0
2242
  // Experimental RD Code
2243
  cpi->frame_distortion = 0;
2244
  cpi->last_frame_distortion = 0;
2245
#endif
2246
2247
34.5k
  set_tile_limits(cpi);
2248
2249
34.5k
  cpi->ext_refresh_frame_flags_pending = 0;
2250
34.5k
  cpi->ext_refresh_frame_context_pending = 0;
2251
2252
34.5k
#if CONFIG_VP9_HIGHBITDEPTH
2253
34.5k
  highbd_set_var_fns(cpi);
2254
34.5k
#endif
2255
2256
34.5k
  vp9_set_row_mt(cpi);
2257
34.5k
}
2258
2259
/***********************************************************************
2260
 * Read before modifying 'cal_nmvjointsadcost' or 'cal_nmvsadcosts'    *
2261
 ***********************************************************************
2262
 * The following 2 functions ('cal_nmvjointsadcost' and                *
2263
 * 'cal_nmvsadcosts') are used to calculate cost lookup tables         *
2264
 * used by 'vp9_diamond_search_sad'. The C implementation of the       *
2265
 * function is generic, but the NEON intrinsics optimised version      *
2266
 * relies on the following properties of the computed tables:          *
2267
 * For cal_nmvjointsadcost:                                            *
2268
 *   - mvjointsadcost[1] == mvjointsadcost[2] == mvjointsadcost[3]     *
2269
 * For cal_nmvsadcosts:                                                *
2270
 *   - For all i: mvsadcost[0][i] == mvsadcost[1][i]                   *
2271
 *         (Equal costs for both components)                           *
2272
 *   - For all i: mvsadcost[0][i] == mvsadcost[0][-i]                  *
2273
 *         (Cost function is even)                                     *
2274
 * If these do not hold, then the NEON optimised version of the        *
2275
 * 'vp9_diamond_search_sad' function cannot be used as it is, in which *
2276
 * case you can revert to using the C function instead.                *
2277
 ***********************************************************************/
2278
2279
3.99k
static void cal_nmvjointsadcost(int *mvjointsadcost) {
2280
  /*********************************************************************
2281
   * Warning: Read the comments above before modifying this function   *
2282
   *********************************************************************/
2283
3.99k
  mvjointsadcost[0] = 600;
2284
3.99k
  mvjointsadcost[1] = 300;
2285
3.99k
  mvjointsadcost[2] = 300;
2286
3.99k
  mvjointsadcost[3] = 300;
2287
3.99k
}
2288
2289
3.99k
static void cal_nmvsadcosts(int *mvsadcost[2]) {
2290
  /*********************************************************************
2291
   * Warning: Read the comments above before modifying this function   *
2292
   *********************************************************************/
2293
3.99k
  int i = 1;
2294
2295
3.99k
  mvsadcost[0][0] = 0;
2296
3.99k
  mvsadcost[1][0] = 0;
2297
2298
65.4M
  do {
2299
65.4M
    double z = 256 * (2 * (log2f(8 * i) + .6));
2300
65.4M
    mvsadcost[0][i] = (int)z;
2301
65.4M
    mvsadcost[1][i] = (int)z;
2302
65.4M
    mvsadcost[0][-i] = (int)z;
2303
65.4M
    mvsadcost[1][-i] = (int)z;
2304
65.4M
  } while (++i <= MV_MAX);
2305
3.99k
}
2306
2307
3.99k
static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
2308
3.99k
  int i = 1;
2309
2310
3.99k
  mvsadcost[0][0] = 0;
2311
3.99k
  mvsadcost[1][0] = 0;
2312
2313
65.4M
  do {
2314
65.4M
    double z = 256 * (2 * (log2f(8 * i) + .6));
2315
65.4M
    mvsadcost[0][i] = (int)z;
2316
65.4M
    mvsadcost[1][i] = (int)z;
2317
65.4M
    mvsadcost[0][-i] = (int)z;
2318
65.4M
    mvsadcost[1][-i] = (int)z;
2319
65.4M
  } while (++i <= MV_MAX);
2320
3.99k
}
2321
2322
3.99k
static void init_ref_frame_bufs(VP9_COMMON *cm) {
2323
3.99k
  int i;
2324
3.99k
  BufferPool *const pool = cm->buffer_pool;
2325
3.99k
  cm->new_fb_idx = INVALID_IDX;
2326
35.9k
  for (i = 0; i < REF_FRAMES; ++i) {
2327
31.9k
    cm->ref_frame_map[i] = INVALID_IDX;
2328
31.9k
  }
2329
51.9k
  for (i = 0; i < FRAME_BUFFERS; ++i) {
2330
47.9k
    pool->frame_bufs[i].ref_count = 0;
2331
47.9k
  }
2332
3.99k
}
2333
2334
static void update_initial_width(VP9_COMP *cpi, int use_highbitdepth,
2335
53.7k
                                 int subsampling_x, int subsampling_y) {
2336
53.7k
  VP9_COMMON *const cm = &cpi->common;
2337
#if !CONFIG_VP9_HIGHBITDEPTH
2338
  (void)use_highbitdepth;
2339
  assert(use_highbitdepth == 0);
2340
#endif
2341
2342
53.7k
  if (!cpi->initial_width ||
2343
53.7k
#if CONFIG_VP9_HIGHBITDEPTH
2344
53.7k
      cm->use_highbitdepth != use_highbitdepth ||
2345
53.7k
#endif
2346
53.7k
      cm->subsampling_x != subsampling_x ||
2347
53.7k
      cm->subsampling_y != subsampling_y) {
2348
3.90k
    cm->subsampling_x = subsampling_x;
2349
3.90k
    cm->subsampling_y = subsampling_y;
2350
3.90k
#if CONFIG_VP9_HIGHBITDEPTH
2351
3.90k
    cm->use_highbitdepth = use_highbitdepth;
2352
3.90k
#endif
2353
3.90k
    alloc_util_frame_buffers(cpi);
2354
    // The initial_width/height is used to clamp the encoding width/height in
2355
    // vp9_set_size_literal(). The check below is added to avoid setting the
2356
    // initial_width/height to a smaller resolution than the one configured.
2357
    // This can happen when the user passes in a lower resolution on the very
2358
    // first frame (after creating the encoder with a larger resolution). For
2359
    // spatial layers this will prevent user from going back up in resolution
2360
    // (i.e., the top layer will get stuck at the lower resolution).
2361
3.90k
    if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
2362
0
      cpi->initial_width = cm->width;
2363
0
      cpi->initial_height = cm->height;
2364
0
    }
2365
3.90k
    cpi->initial_mbs = cm->MBs;
2366
3.90k
  }
2367
53.7k
}
2368
2369
VP9_COMP *vp9_create_compressor(const VP9EncoderConfig *oxcf,
2370
3.99k
                                BufferPool *const pool) {
2371
3.99k
  unsigned int i;
2372
3.99k
  VP9_COMP *volatile const cpi = vpx_memalign(32, sizeof(*cpi));
2373
3.99k
  VP9_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
2374
2375
3.99k
  if (!cm) return NULL;
2376
2377
3.99k
  vp9_zero(*cpi);
2378
2379
3.99k
  if (setjmp(cm->error.jmp)) {
2380
0
    cm->error.setjmp = 0;
2381
0
    vp9_remove_compressor(cpi);
2382
0
    return 0;
2383
0
  }
2384
2385
3.99k
  cm->error.setjmp = 1;
2386
3.99k
  cm->alloc_mi = vp9_enc_alloc_mi;
2387
3.99k
  cm->free_mi = vp9_enc_free_mi;
2388
3.99k
  cm->setup_mi = vp9_enc_setup_mi;
2389
2390
3.99k
  CHECK_MEM_ERROR(&cm->error, cm->fc,
2391
3.99k
                  (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
2392
3.99k
  CHECK_MEM_ERROR(
2393
3.99k
      &cm->error, cm->frame_contexts,
2394
3.99k
      (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
2395
2396
3.99k
  cpi->compute_frame_low_motion_onepass = 1;
2397
3.99k
  cpi->use_svc = 0;
2398
3.99k
  cpi->resize_state = ORIG;
2399
3.99k
  cpi->external_resize = 0;
2400
3.99k
  cpi->resize_avg_qp = 0;
2401
3.99k
  cpi->resize_buffer_underflow = 0;
2402
3.99k
  cpi->use_skin_detection = 0;
2403
3.99k
  cpi->common.buffer_pool = pool;
2404
3.99k
  init_ref_frame_bufs(cm);
2405
2406
3.99k
  cpi->force_update_segmentation = 0;
2407
2408
3.99k
  init_config(cpi, oxcf);
2409
3.99k
  cpi->frame_info = vp9_get_frame_info(oxcf);
2410
2411
3.99k
  vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
2412
3.99k
  vp9_init_rd_parameters(cpi);
2413
2414
3.99k
  init_frame_indexes(cm);
2415
3.99k
  cpi->initial_width = cpi->oxcf.width;
2416
3.99k
  cpi->initial_height = cpi->oxcf.height;
2417
3.99k
  cpi->tile_data = NULL;
2418
2419
3.99k
  realloc_segmentation_maps(cpi);
2420
2421
3.99k
  CHECK_MEM_ERROR(
2422
3.99k
      &cm->error, cpi->skin_map,
2423
3.99k
      vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->skin_map)));
2424
2425
3.99k
#if !CONFIG_REALTIME_ONLY
2426
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->alt_ref_aq, vp9_alt_ref_aq_create());
2427
3.99k
#endif
2428
2429
3.99k
  CHECK_MEM_ERROR(
2430
3.99k
      &cm->error, cpi->consec_zero_mv,
2431
3.99k
      vpx_calloc(cm->mi_rows * cm->mi_cols, sizeof(*cpi->consec_zero_mv)));
2432
2433
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->nmvcosts[0],
2434
3.99k
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
2435
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->nmvcosts[1],
2436
3.99k
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
2437
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->nmvcosts_hp[0],
2438
3.99k
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
2439
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->nmvcosts_hp[1],
2440
3.99k
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
2441
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->nmvsadcosts[0],
2442
3.99k
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
2443
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->nmvsadcosts[1],
2444
3.99k
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
2445
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->nmvsadcosts_hp[0],
2446
3.99k
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
2447
3.99k
  CHECK_MEM_ERROR(&cm->error, cpi->nmvsadcosts_hp[1],
2448
3.99k
                  vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
2449
2450
103k
  for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2451
99.8k
       i++) {
2452
99.8k
    CHECK_MEM_ERROR(
2453
99.8k
        &cm->error, cpi->mbgraph_stats[i].mb_stats,
2454
99.8k
        vpx_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
2455
99.8k
  }
2456
2457
3.99k
  cpi->refresh_alt_ref_frame = 0;
2458
3.99k
  cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2459
2460
3.99k
  init_level_info(&cpi->level_info);
2461
3.99k
  init_level_constraint(&cpi->level_constraint);
2462
2463
#if CONFIG_INTERNAL_STATS
2464
  cpi->b_calculate_blockiness = 1;
2465
  cpi->b_calculate_consistency = 1;
2466
  cpi->total_inconsistency = 0;
2467
  cpi->psnr.worst = 100.0;
2468
  cpi->worst_ssim = 100.0;
2469
2470
  cpi->count = 0;
2471
  cpi->bytes = 0;
2472
2473
  if (cpi->b_calculate_psnr) {
2474
    cpi->total_sq_error = 0;
2475
    cpi->total_samples = 0;
2476
2477
    cpi->totalp_sq_error = 0;
2478
    cpi->totalp_samples = 0;
2479
2480
    cpi->tot_recode_hits = 0;
2481
    cpi->summed_quality = 0;
2482
    cpi->summed_weights = 0;
2483
    cpi->summedp_quality = 0;
2484
    cpi->summedp_weights = 0;
2485
  }
2486
2487
  cpi->fastssim.worst = 100.0;
2488
2489
  cpi->psnrhvs.worst = 100.0;
2490
2491
  if (cpi->b_calculate_blockiness) {
2492
    cpi->total_blockiness = 0;
2493
    cpi->worst_blockiness = 0.0;
2494
  }
2495
2496
  if (cpi->b_calculate_consistency) {
2497
    CHECK_MEM_ERROR(&cm->error, cpi->ssim_vars,
2498
                    vpx_calloc(cpi->common.mi_rows * cpi->common.mi_cols,
2499
                               sizeof(*cpi->ssim_vars) * 4));
2500
    cpi->worst_consistency = 100.0;
2501
  } else {
2502
    cpi->ssim_vars = NULL;
2503
  }
2504
2505
#endif
2506
2507
3.99k
  cpi->first_time_stamp_ever = INT64_MAX;
2508
2509
  /*********************************************************************
2510
   * Warning: Read the comments around 'cal_nmvjointsadcost' and       *
2511
   * 'cal_nmvsadcosts' before modifying how these tables are computed. *
2512
   *********************************************************************/
2513
3.99k
  cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
2514
3.99k
  cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
2515
3.99k
  cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
2516
3.99k
  cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
2517
3.99k
  cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
2518
3.99k
  cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
2519
2520
3.99k
  cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
2521
3.99k
  cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
2522
3.99k
  cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
2523
3.99k
  cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
2524
3.99k
  cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
2525
2526
#if CONFIG_VP9_TEMPORAL_DENOISING
2527
#ifdef OUTPUT_YUV_DENOISED
2528
  yuv_denoised_file = fopen("denoised.yuv", "ab");
2529
#endif
2530
#endif
2531
#ifdef OUTPUT_YUV_SKINMAP
2532
  yuv_skinmap_file = fopen("skinmap.yuv", "wb");
2533
#endif
2534
#ifdef OUTPUT_YUV_REC
2535
  yuv_rec_file = fopen("rec.yuv", "wb");
2536
#endif
2537
#ifdef OUTPUT_YUV_SVC_SRC
2538
  yuv_svc_src[0] = fopen("svc_src_0.yuv", "wb");
2539
  yuv_svc_src[1] = fopen("svc_src_1.yuv", "wb");
2540
  yuv_svc_src[2] = fopen("svc_src_2.yuv", "wb");
2541
#endif
2542
2543
#if 0
2544
  framepsnr = fopen("framepsnr.stt", "a");
2545
  kf_list = fopen("kf_list.stt", "w");
2546
#endif
2547
2548
3.99k
  cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
2549
2550
3.99k
  {
2551
3.99k
    vpx_codec_err_t codec_status = vp9_extrc_init(&cpi->ext_ratectrl);
2552
3.99k
    if (codec_status != VPX_CODEC_OK) {
2553
0
      vpx_internal_error(&cm->error, codec_status, "vp9_extrc_init() failed");
2554
0
    }
2555
3.99k
  }
2556
2557
3.99k
#if !CONFIG_REALTIME_ONLY
2558
3.99k
  if (oxcf->pass == 1) {
2559
0
    vp9_init_first_pass(cpi);
2560
3.99k
  } else if (oxcf->pass == 2) {
2561
0
    const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2562
0
    const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2563
2564
0
    if (cpi->svc.number_spatial_layers > 1 ||
2565
0
        cpi->svc.number_temporal_layers > 1) {
2566
0
      FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
2567
0
      FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = { 0 };
2568
0
      int n;
2569
2570
0
      for (n = 0; n < oxcf->ss_number_layers; ++n) {
2571
0
        FIRSTPASS_STATS *const last_packet_for_layer =
2572
0
            &stats[packets - oxcf->ss_number_layers + n];
2573
0
        const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
2574
0
        const int packets_in_layer = (int)last_packet_for_layer->count + 1;
2575
0
        if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
2576
0
          int num_frames;
2577
0
          LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
2578
2579
0
          vpx_free(lc->rc_twopass_stats_in.buf);
2580
2581
0
          lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
2582
0
          CHECK_MEM_ERROR(&cm->error, lc->rc_twopass_stats_in.buf,
2583
0
                          vpx_malloc(lc->rc_twopass_stats_in.sz));
2584
0
          lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
2585
0
          lc->twopass.stats_in = lc->twopass.stats_in_start;
2586
0
          lc->twopass.stats_in_end =
2587
0
              lc->twopass.stats_in_start + packets_in_layer - 1;
2588
          // Note the last packet is cumulative first pass stats.
2589
          // So the number of frames is packet number minus one
2590
0
          num_frames = packets_in_layer - 1;
2591
0
          fps_init_first_pass_info(&lc->twopass.first_pass_info,
2592
0
                                   lc->rc_twopass_stats_in.buf, num_frames);
2593
0
          stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
2594
0
        }
2595
0
      }
2596
2597
0
      for (n = 0; n < packets; ++n) {
2598
0
        const int layer_id = (int)stats[n].spatial_layer_id;
2599
0
        if (layer_id >= 0 && layer_id < oxcf->ss_number_layers &&
2600
0
            stats_copy[layer_id] != NULL) {
2601
0
          *stats_copy[layer_id] = stats[n];
2602
0
          ++stats_copy[layer_id];
2603
0
        }
2604
0
      }
2605
2606
0
      vp9_init_second_pass_spatial_svc(cpi);
2607
0
    } else {
2608
0
      int num_frames;
2609
2610
0
      cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2611
0
      cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2612
0
      cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2613
      // Note the last packet is cumulative first pass stats.
2614
      // So the number of frames is packet number minus one
2615
0
      num_frames = packets - 1;
2616
0
      fps_init_first_pass_info(&cpi->twopass.first_pass_info,
2617
0
                               oxcf->two_pass_stats_in.buf, num_frames);
2618
2619
0
      vp9_init_second_pass(cpi);
2620
0
    }
2621
0
  }
2622
3.99k
#endif  // !CONFIG_REALTIME_ONLY
2623
2624
3.99k
  cpi->mb_wiener_var_cols = 0;
2625
3.99k
  cpi->mb_wiener_var_rows = 0;
2626
3.99k
  cpi->mb_wiener_variance = NULL;
2627
2628
3.99k
  vp9_set_speed_features_framesize_independent(cpi, oxcf->speed);
2629
3.99k
  vp9_set_speed_features_framesize_dependent(cpi, oxcf->speed);
2630
2631
3.99k
  {
2632
3.99k
    const int bsize = BLOCK_16X16;
2633
3.99k
    const int w = num_8x8_blocks_wide_lookup[bsize];
2634
3.99k
    const int h = num_8x8_blocks_high_lookup[bsize];
2635
3.99k
    const int num_cols = (cm->mi_cols + w - 1) / w;
2636
3.99k
    const int num_rows = (cm->mi_rows + h - 1) / h;
2637
3.99k
    CHECK_MEM_ERROR(&cm->error, cpi->mi_ssim_rdmult_scaling_factors,
2638
3.99k
                    vpx_calloc(num_rows * num_cols,
2639
3.99k
                               sizeof(*cpi->mi_ssim_rdmult_scaling_factors)));
2640
3.99k
  }
2641
2642
3.99k
  cpi->kmeans_data_arr_alloc = 0;
2643
#if CONFIG_NON_GREEDY_MV
2644
  cpi->tpl_ready = 0;
2645
#endif  // CONFIG_NON_GREEDY_MV
2646
203k
  for (i = 0; i < MAX_ARF_GOP_SIZE; ++i) {
2647
199k
    cpi->tpl_stats[i].tpl_stats_ptr = NULL;
2648
199k
  }
2649
2650
3.99k
#define BFP(BT, SDF, SDSF, SDAF, VF, SVF, SVAF, SDX4DF, SDSX4DF) \
2651
51.9k
  cpi->fn_ptr[BT].sdf = SDF;                                     \
2652
51.9k
  cpi->fn_ptr[BT].sdsf = SDSF;                                   \
2653
51.9k
  cpi->fn_ptr[BT].sdaf = SDAF;                                   \
2654
51.9k
  cpi->fn_ptr[BT].vf = VF;                                       \
2655
51.9k
  cpi->fn_ptr[BT].svf = SVF;                                     \
2656
51.9k
  cpi->fn_ptr[BT].svaf = SVAF;                                   \
2657
51.9k
  cpi->fn_ptr[BT].sdx4df = SDX4DF;                               \
2658
51.9k
  cpi->fn_ptr[BT].sdsx4df = SDSX4DF;
2659
2660
3.99k
  BFP(BLOCK_32X16, vpx_sad32x16, vpx_sad_skip_32x16, vpx_sad32x16_avg,
2661
3.99k
      vpx_variance32x16, vpx_sub_pixel_variance32x16,
2662
3.99k
      vpx_sub_pixel_avg_variance32x16, vpx_sad32x16x4d, vpx_sad_skip_32x16x4d)
2663
2664
3.99k
  BFP(BLOCK_16X32, vpx_sad16x32, vpx_sad_skip_16x32, vpx_sad16x32_avg,
2665
3.99k
      vpx_variance16x32, vpx_sub_pixel_variance16x32,
2666
3.99k
      vpx_sub_pixel_avg_variance16x32, vpx_sad16x32x4d, vpx_sad_skip_16x32x4d)
2667
2668
3.99k
  BFP(BLOCK_64X32, vpx_sad64x32, vpx_sad_skip_64x32, vpx_sad64x32_avg,
2669
3.99k
      vpx_variance64x32, vpx_sub_pixel_variance64x32,
2670
3.99k
      vpx_sub_pixel_avg_variance64x32, vpx_sad64x32x4d, vpx_sad_skip_64x32x4d)
2671
2672
3.99k
  BFP(BLOCK_32X64, vpx_sad32x64, vpx_sad_skip_32x64, vpx_sad32x64_avg,
2673
3.99k
      vpx_variance32x64, vpx_sub_pixel_variance32x64,
2674
3.99k
      vpx_sub_pixel_avg_variance32x64, vpx_sad32x64x4d, vpx_sad_skip_32x64x4d)
2675
2676
3.99k
  BFP(BLOCK_32X32, vpx_sad32x32, vpx_sad_skip_32x32, vpx_sad32x32_avg,
2677
3.99k
      vpx_variance32x32, vpx_sub_pixel_variance32x32,
2678
3.99k
      vpx_sub_pixel_avg_variance32x32, vpx_sad32x32x4d, vpx_sad_skip_32x32x4d)
2679
2680
3.99k
  BFP(BLOCK_64X64, vpx_sad64x64, vpx_sad_skip_64x64, vpx_sad64x64_avg,
2681
3.99k
      vpx_variance64x64, vpx_sub_pixel_variance64x64,
2682
3.99k
      vpx_sub_pixel_avg_variance64x64, vpx_sad64x64x4d, vpx_sad_skip_64x64x4d)
2683
2684
3.99k
  BFP(BLOCK_16X16, vpx_sad16x16, vpx_sad_skip_16x16, vpx_sad16x16_avg,
2685
3.99k
      vpx_variance16x16, vpx_sub_pixel_variance16x16,
2686
3.99k
      vpx_sub_pixel_avg_variance16x16, vpx_sad16x16x4d, vpx_sad_skip_16x16x4d)
2687
2688
3.99k
  BFP(BLOCK_16X8, vpx_sad16x8, vpx_sad_skip_16x8, vpx_sad16x8_avg,
2689
3.99k
      vpx_variance16x8, vpx_sub_pixel_variance16x8,
2690
3.99k
      vpx_sub_pixel_avg_variance16x8, vpx_sad16x8x4d, vpx_sad_skip_16x8x4d)
2691
2692
3.99k
  BFP(BLOCK_8X16, vpx_sad8x16, vpx_sad_skip_8x16, vpx_sad8x16_avg,
2693
3.99k
      vpx_variance8x16, vpx_sub_pixel_variance8x16,
2694
3.99k
      vpx_sub_pixel_avg_variance8x16, vpx_sad8x16x4d, vpx_sad_skip_8x16x4d)
2695
2696
3.99k
  BFP(BLOCK_8X8, vpx_sad8x8, vpx_sad_skip_8x8, vpx_sad8x8_avg, vpx_variance8x8,
2697
3.99k
      vpx_sub_pixel_variance8x8, vpx_sub_pixel_avg_variance8x8, vpx_sad8x8x4d,
2698
3.99k
      vpx_sad_skip_8x8x4d)
2699
2700
3.99k
  BFP(BLOCK_8X4, vpx_sad8x4, vpx_sad_skip_8x4, vpx_sad8x4_avg, vpx_variance8x4,
2701
3.99k
      vpx_sub_pixel_variance8x4, vpx_sub_pixel_avg_variance8x4, vpx_sad8x4x4d,
2702
3.99k
      vpx_sad_skip_8x4x4d)
2703
2704
3.99k
  BFP(BLOCK_4X8, vpx_sad4x8, vpx_sad_skip_4x8, vpx_sad4x8_avg, vpx_variance4x8,
2705
3.99k
      vpx_sub_pixel_variance4x8, vpx_sub_pixel_avg_variance4x8, vpx_sad4x8x4d,
2706
3.99k
      vpx_sad_skip_4x8x4d)
2707
2708
3.99k
  BFP(BLOCK_4X4, vpx_sad4x4, vpx_sad_skip_4x4, vpx_sad4x4_avg, vpx_variance4x4,
2709
3.99k
      vpx_sub_pixel_variance4x4, vpx_sub_pixel_avg_variance4x4, vpx_sad4x4x4d,
2710
3.99k
      vpx_sad_skip_4x4x4d)
2711
2712
3.99k
#if CONFIG_VP9_HIGHBITDEPTH
2713
3.99k
  highbd_set_var_fns(cpi);
2714
3.99k
#endif
2715
2716
  /* vp9_init_quantizer() is first called here. Add check in
2717
   * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
2718
   * called later when needed. This will avoid unnecessary calls of
2719
   * vp9_init_quantizer() for every frame.
2720
   */
2721
3.99k
  vp9_init_quantizer(cpi);
2722
2723
3.99k
  vp9_loop_filter_init(cm);
2724
2725
  // Set up the unit scaling factor used during motion search.
2726
3.99k
#if CONFIG_VP9_HIGHBITDEPTH
2727
3.99k
  vp9_setup_scale_factors_for_frame(&cpi->me_sf, cm->width, cm->height,
2728
3.99k
                                    cm->width, cm->height,
2729
3.99k
                                    cm->use_highbitdepth);
2730
#else
2731
  vp9_setup_scale_factors_for_frame(&cpi->me_sf, cm->width, cm->height,
2732
                                    cm->width, cm->height);
2733
#endif  // CONFIG_VP9_HIGHBITDEPTH
2734
3.99k
  cpi->td.mb.me_sf = &cpi->me_sf;
2735
2736
3.99k
  cm->error.setjmp = 0;
2737
2738
3.99k
  return cpi;
2739
3.99k
}
2740
2741
#if CONFIG_INTERNAL_STATS
2742
#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2743
2744
#define SNPRINT2(H, T, V) \
2745
  snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2746
#endif  // CONFIG_INTERNAL_STATS
2747
2748
4.04k
void vp9_remove_compressor(VP9_COMP *cpi) {
2749
4.04k
  VP9_COMMON *cm;
2750
4.04k
  unsigned int i;
2751
2752
4.04k
  if (!cpi) return;
2753
2754
#if CONFIG_INTERNAL_STATS
2755
  vpx_free(cpi->ssim_vars);
2756
#endif
2757
2758
3.99k
  cm = &cpi->common;
2759
3.99k
  if (cm->current_video_frame > 0) {
2760
#if CONFIG_INTERNAL_STATS
2761
    vpx_clear_system_state();
2762
2763
    if (cpi->oxcf.pass != 1) {
2764
      char headings[512] = { 0 };
2765
      char results[512] = { 0 };
2766
      FILE *f = fopen("opsnr.stt", "a");
2767
      double time_encoded =
2768
          (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2769
          10000000.000;
2770
      double total_encode_time =
2771
          (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2772
      const double dr =
2773
          (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2774
      const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2775
      const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2776
      const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2777
2778
      if (cpi->b_calculate_psnr) {
2779
        const double total_psnr = vpx_sse_to_psnr(
2780
            (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2781
        const double totalp_psnr = vpx_sse_to_psnr(
2782
            (double)cpi->totalp_samples, peak, (double)cpi->totalp_sq_error);
2783
        const double total_ssim =
2784
            100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2785
        const double totalp_ssim =
2786
            100 * pow(cpi->summedp_quality / cpi->summedp_weights, 8.0);
2787
2788
        snprintf(headings, sizeof(headings),
2789
                 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
2790
                 "VPXSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
2791
                 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
2792
                 "AVPsnrY\tAPsnrCb\tAPsnrCr");
2793
        snprintf(results, sizeof(results),
2794
                 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2795
                 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2796
                 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2797
                 "%7.3f\t%7.3f\t%7.3f",
2798
                 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
2799
                 cpi->psnrp.stat[ALL] / cpi->count, totalp_psnr, total_ssim,
2800
                 totalp_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2801
                 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
2802
                 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
2803
                 cpi->psnr.stat[Y] / cpi->count, cpi->psnr.stat[U] / cpi->count,
2804
                 cpi->psnr.stat[V] / cpi->count);
2805
2806
        if (cpi->b_calculate_blockiness) {
2807
          SNPRINT(headings, "\t  Block\tWstBlck");
2808
          SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2809
          SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2810
        }
2811
2812
        if (cpi->b_calculate_consistency) {
2813
          double consistency =
2814
              vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
2815
                              (double)cpi->total_inconsistency);
2816
2817
          SNPRINT(headings, "\tConsist\tWstCons");
2818
          SNPRINT2(results, "\t%7.3f", consistency);
2819
          SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2820
        }
2821
2822
        SNPRINT(headings, "\t    Time\tRcErr\tAbsErr");
2823
        SNPRINT2(results, "\t%8.0f", total_encode_time);
2824
        SNPRINT2(results, "\t%7.2f", rate_err);
2825
        SNPRINT2(results, "\t%7.2f", fabs(rate_err));
2826
2827
        fprintf(f, "%s\tAPsnr611\n", headings);
2828
        fprintf(
2829
            f, "%s\t%7.3f\n", results,
2830
            (6 * cpi->psnr.stat[Y] + cpi->psnr.stat[U] + cpi->psnr.stat[V]) /
2831
                (cpi->count * 8));
2832
      }
2833
2834
      fclose(f);
2835
    }
2836
#endif
2837
2838
#if 0
2839
    {
2840
      printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2841
      printf("\n_frames receive_data encod_mb_row compress_frame  Total\n");
2842
      printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2843
             cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2844
             cpi->time_compress_data / 1000,
2845
             (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2846
    }
2847
#endif
2848
3.87k
  }
2849
2850
#if CONFIG_VP9_TEMPORAL_DENOISING
2851
  vp9_denoiser_free(&(cpi->denoiser));
2852
#endif
2853
2854
3.99k
  if (cpi->kmeans_data_arr_alloc) {
2855
3.87k
#if CONFIG_MULTITHREAD
2856
3.87k
    pthread_mutex_destroy(&cpi->kmeans_mutex);
2857
3.87k
#endif
2858
3.87k
    vpx_free(cpi->kmeans_data_arr);
2859
3.87k
  }
2860
2861
3.99k
  vp9_free_tpl_buffer(cpi);
2862
2863
3.99k
  vp9_loop_filter_dealloc(&cpi->lf_row_sync);
2864
3.99k
  vp9_bitstream_encode_tiles_buffer_dealloc(cpi);
2865
3.99k
  vp9_row_mt_mem_dealloc(cpi);
2866
3.99k
  vp9_encode_free_mt_data(cpi);
2867
2868
3.99k
#if !CONFIG_REALTIME_ONLY
2869
3.99k
  vp9_alt_ref_aq_destroy(cpi->alt_ref_aq);
2870
3.99k
#endif
2871
2872
3.99k
  dealloc_compressor_data(cpi);
2873
2874
103k
  for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2875
99.8k
       ++i) {
2876
99.8k
    vpx_free(cpi->mbgraph_stats[i].mb_stats);
2877
99.8k
  }
2878
2879
3.99k
  vp9_extrc_delete(&cpi->ext_ratectrl);
2880
2881
  // Help detect use after free of the error detail string.
2882
3.99k
  memset(cm->error.detail, 'A', sizeof(cm->error.detail) - 1);
2883
3.99k
  cm->error.detail[sizeof(cm->error.detail) - 1] = '\0';
2884
2885
3.99k
  vp9_remove_common(cm);
2886
3.99k
  vp9_free_ref_frame_buffers(cm->buffer_pool);
2887
#if CONFIG_VP9_POSTPROC
2888
  vp9_free_postproc_buffers(cm);
2889
#endif
2890
3.99k
  vpx_free(cpi);
2891
2892
#if CONFIG_VP9_TEMPORAL_DENOISING
2893
#ifdef OUTPUT_YUV_DENOISED
2894
  fclose(yuv_denoised_file);
2895
#endif
2896
#endif
2897
#ifdef OUTPUT_YUV_SKINMAP
2898
  fclose(yuv_skinmap_file);
2899
#endif
2900
#ifdef OUTPUT_YUV_REC
2901
  fclose(yuv_rec_file);
2902
#endif
2903
#ifdef OUTPUT_YUV_SVC_SRC
2904
  fclose(yuv_svc_src[0]);
2905
  fclose(yuv_svc_src[1]);
2906
  fclose(yuv_svc_src[2]);
2907
#endif
2908
2909
#if 0
2910
2911
  if (keyfile)
2912
    fclose(keyfile);
2913
2914
  if (framepsnr)
2915
    fclose(framepsnr);
2916
2917
  if (kf_list)
2918
    fclose(kf_list);
2919
2920
#endif
2921
3.99k
}
2922
2923
53.0k
int vp9_get_psnr(const VP9_COMP *cpi, PSNR_STATS *psnr) {
2924
53.0k
  if (is_psnr_calc_enabled(cpi)) {
2925
0
#if CONFIG_VP9_HIGHBITDEPTH
2926
0
    vpx_calc_highbd_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, psnr,
2927
0
                         cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2928
#else
2929
    vpx_calc_psnr(cpi->raw_source_frame, cpi->common.frame_to_show, psnr);
2930
#endif
2931
0
    return 1;
2932
53.0k
  } else {
2933
53.0k
    vp9_zero(*psnr);
2934
53.0k
    return 0;
2935
53.0k
  }
2936
53.0k
}
2937
2938
0
int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
2939
0
  if (ref_frame_flags > 7) return -1;
2940
2941
0
  cpi->ref_frame_flags = ref_frame_flags;
2942
0
  return 0;
2943
0
}
2944
2945
0
void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
2946
0
  cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
2947
0
  cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
2948
0
  cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
2949
0
  cpi->ext_refresh_frame_flags_pending = 1;
2950
0
}
2951
2952
static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(
2953
0
    VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag) {
2954
0
  MV_REFERENCE_FRAME ref_frame = NO_REF_FRAME;
2955
0
  if (ref_frame_flag == VP9_LAST_FLAG)
2956
0
    ref_frame = LAST_FRAME;
2957
0
  else if (ref_frame_flag == VP9_GOLD_FLAG)
2958
0
    ref_frame = GOLDEN_FRAME;
2959
0
  else if (ref_frame_flag == VP9_ALT_FLAG)
2960
0
    ref_frame = ALTREF_FRAME;
2961
2962
0
  return ref_frame == NO_REF_FRAME ? NULL
2963
0
                                   : get_ref_frame_buffer(cpi, ref_frame);
2964
0
}
2965
2966
int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2967
0
                           YV12_BUFFER_CONFIG *sd) {
2968
0
  YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2969
0
  if (cfg) {
2970
0
    vpx_yv12_copy_frame(cfg, sd);
2971
0
    return 0;
2972
0
  } else {
2973
0
    return -1;
2974
0
  }
2975
0
}
2976
2977
int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2978
0
                          YV12_BUFFER_CONFIG *sd) {
2979
0
  YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2980
0
  if (cfg) {
2981
0
    vpx_yv12_copy_frame(sd, cfg);
2982
0
    return 0;
2983
0
  } else {
2984
0
    return -1;
2985
0
  }
2986
0
}
2987
2988
0
int vp9_update_entropy(VP9_COMP *cpi, int update) {
2989
0
  cpi->ext_refresh_frame_context = update;
2990
0
  cpi->ext_refresh_frame_context_pending = 1;
2991
0
  return 0;
2992
0
}
2993
2994
#ifdef OUTPUT_YUV_REC
2995
void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2996
  YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2997
  uint8_t *src = s->y_buffer;
2998
  int h = cm->height;
2999
3000
#if CONFIG_VP9_HIGHBITDEPTH
3001
  if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3002
    uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3003
3004
    do {
3005
      fwrite(src16, s->y_width, 2, yuv_rec_file);
3006
      src16 += s->y_stride;
3007
    } while (--h);
3008
3009
    src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3010
    h = s->uv_height;
3011
3012
    do {
3013
      fwrite(src16, s->uv_width, 2, yuv_rec_file);
3014
      src16 += s->uv_stride;
3015
    } while (--h);
3016
3017
    src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3018
    h = s->uv_height;
3019
3020
    do {
3021
      fwrite(src16, s->uv_width, 2, yuv_rec_file);
3022
      src16 += s->uv_stride;
3023
    } while (--h);
3024
3025
    fflush(yuv_rec_file);
3026
    return;
3027
  }
3028
#endif  // CONFIG_VP9_HIGHBITDEPTH
3029
3030
  do {
3031
    fwrite(src, s->y_width, 1, yuv_rec_file);
3032
    src += s->y_stride;
3033
  } while (--h);
3034
3035
  src = s->u_buffer;
3036
  h = s->uv_height;
3037
3038
  do {
3039
    fwrite(src, s->uv_width, 1, yuv_rec_file);
3040
    src += s->uv_stride;
3041
  } while (--h);
3042
3043
  src = s->v_buffer;
3044
  h = s->uv_height;
3045
3046
  do {
3047
    fwrite(src, s->uv_width, 1, yuv_rec_file);
3048
    src += s->uv_stride;
3049
  } while (--h);
3050
3051
  fflush(yuv_rec_file);
3052
}
3053
#endif
3054
3055
#if CONFIG_VP9_HIGHBITDEPTH
3056
void vp9_scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
3057
0
                                             YV12_BUFFER_CONFIG *dst, int bd) {
3058
#else
3059
void vp9_scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
3060
                                             YV12_BUFFER_CONFIG *dst) {
3061
#endif  // CONFIG_VP9_HIGHBITDEPTH
3062
  // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
3063
0
  int i;
3064
0
  const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
3065
0
                                   src->v_buffer };
3066
0
  const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
3067
0
  const int src_widths[3] = { src->y_crop_width, src->uv_crop_width,
3068
0
                              src->uv_crop_width };
3069
0
  const int src_heights[3] = { src->y_crop_height, src->uv_crop_height,
3070
0
                               src->uv_crop_height };
3071
0
  uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
3072
0
  const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
3073
0
  const int dst_widths[3] = { dst->y_crop_width, dst->uv_crop_width,
3074
0
                              dst->uv_crop_width };
3075
0
  const int dst_heights[3] = { dst->y_crop_height, dst->uv_crop_height,
3076
0
                               dst->uv_crop_height };
3077
3078
0
  for (i = 0; i < MAX_MB_PLANE; ++i) {
3079
0
#if CONFIG_VP9_HIGHBITDEPTH
3080
0
    if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
3081
0
      vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
3082
0
                              src_strides[i], dsts[i], dst_heights[i],
3083
0
                              dst_widths[i], dst_strides[i], bd);
3084
0
    } else {
3085
0
      vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
3086
0
                       dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
3087
0
    }
3088
#else
3089
    vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
3090
                     dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
3091
#endif  // CONFIG_VP9_HIGHBITDEPTH
3092
0
  }
3093
0
  vpx_extend_frame_borders(dst);
3094
0
}
3095
3096
#if CONFIG_VP9_HIGHBITDEPTH
3097
static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
3098
                                   YV12_BUFFER_CONFIG *dst, int bd,
3099
                                   INTERP_FILTER filter_type,
3100
0
                                   int phase_scaler) {
3101
0
  const int src_w = src->y_crop_width;
3102
0
  const int src_h = src->y_crop_height;
3103
0
  const int dst_w = dst->y_crop_width;
3104
0
  const int dst_h = dst->y_crop_height;
3105
3106
  // The issue b/311394513 reveals a corner case bug.
3107
  // For bd = 8, vpx_scaled_2d() requires both x_step_q4 and y_step_q4 are less
3108
  // than or equal to 64. For bd >= 10, vpx_highbd_convolve8() requires both
3109
  // x_step_q4 and y_step_q4 are less than or equal to 32. If this condition
3110
  // isn't met, it needs to call vp9_scale_and_extend_frame_nonnormative() that
3111
  // supports arbitrary scaling.
3112
0
  const int x_step_q4 = 16 * src_w / dst_w;
3113
0
  const int y_step_q4 = 16 * src_h / dst_h;
3114
0
  const int is_arbitrary_scaling =
3115
0
      (bd == 8 && (x_step_q4 > 64 || y_step_q4 > 64)) ||
3116
0
      (bd >= 10 && (x_step_q4 > 32 || y_step_q4 > 32));
3117
0
  if (is_arbitrary_scaling) {
3118
0
    vp9_scale_and_extend_frame_nonnormative(src, dst, bd);
3119
0
    return;
3120
0
  }
3121
3122
0
  const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
3123
0
                                   src->v_buffer };
3124
0
  const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
3125
0
  uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
3126
0
  const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
3127
0
  const InterpKernel *const kernel = vp9_filter_kernels[filter_type];
3128
0
  int x, y, i;
3129
3130
0
  for (i = 0; i < MAX_MB_PLANE; ++i) {
3131
0
    const int factor = (i == 0 || i == 3 ? 1 : 2);
3132
0
    const int src_stride = src_strides[i];
3133
0
    const int dst_stride = dst_strides[i];
3134
0
    for (y = 0; y < dst_h; y += 16) {
3135
0
      const int y_q4 = y * (16 / factor) * src_h / dst_h + phase_scaler;
3136
0
      for (x = 0; x < dst_w; x += 16) {
3137
0
        const int x_q4 = x * (16 / factor) * src_w / dst_w + phase_scaler;
3138
0
        const uint8_t *src_ptr = srcs[i] +
3139
0
                                 (y / factor) * src_h / dst_h * src_stride +
3140
0
                                 (x / factor) * src_w / dst_w;
3141
0
        uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
3142
3143
0
        if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
3144
0
          vpx_highbd_convolve8(CONVERT_TO_SHORTPTR(src_ptr), src_stride,
3145
0
                               CONVERT_TO_SHORTPTR(dst_ptr), dst_stride, kernel,
3146
0
                               x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
3147
0
                               16 * src_h / dst_h, 16 / factor, 16 / factor,
3148
0
                               bd);
3149
0
        } else {
3150
0
          vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride, kernel,
3151
0
                        x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
3152
0
                        16 * src_h / dst_h, 16 / factor, 16 / factor);
3153
0
        }
3154
0
      }
3155
0
    }
3156
0
  }
3157
3158
0
  vpx_extend_frame_borders(dst);
3159
0
}
3160
#endif  // CONFIG_VP9_HIGHBITDEPTH
3161
3162
#if !CONFIG_REALTIME_ONLY
3163
0
static int scale_down(VP9_COMP *cpi, int q) {
3164
0
  RATE_CONTROL *const rc = &cpi->rc;
3165
0
  GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3166
0
  int scale = 0;
3167
0
  assert(frame_is_kf_gf_arf(cpi));
3168
3169
0
  if (rc->frame_size_selector == UNSCALED &&
3170
0
      q >= rc->rf_level_maxq[gf_group->rf_level[gf_group->index]]) {
3171
0
    const int max_size_thresh =
3172
0
        (int)(rate_thresh_mult[SCALE_STEP1] *
3173
0
              VPXMAX(rc->this_frame_target, rc->avg_frame_bandwidth));
3174
0
    scale = rc->projected_frame_size > max_size_thresh ? 1 : 0;
3175
0
  }
3176
0
  return scale;
3177
0
}
3178
3179
0
static int big_rate_miss_high_threshold(VP9_COMP *cpi) {
3180
0
  const RATE_CONTROL *const rc = &cpi->rc;
3181
0
  int big_miss_high;
3182
3183
0
  if (frame_is_kf_gf_arf(cpi))
3184
0
    big_miss_high = rc->this_frame_target * 3 / 2;
3185
0
  else
3186
0
    big_miss_high = rc->this_frame_target * 2;
3187
3188
0
  return big_miss_high;
3189
0
}
3190
3191
0
static int big_rate_miss(VP9_COMP *cpi) {
3192
0
  const RATE_CONTROL *const rc = &cpi->rc;
3193
0
  int big_miss_high;
3194
0
  int big_miss_low;
3195
3196
  // Ignore for overlay frames
3197
0
  if (rc->is_src_frame_alt_ref) {
3198
0
    return 0;
3199
0
  } else {
3200
0
    big_miss_low = (rc->this_frame_target / 2);
3201
0
    big_miss_high = big_rate_miss_high_threshold(cpi);
3202
3203
0
    return (rc->projected_frame_size > big_miss_high) ||
3204
0
           (rc->projected_frame_size < big_miss_low);
3205
0
  }
3206
0
}
3207
3208
// test in two pass for the first
3209
0
static int two_pass_first_group_inter(VP9_COMP *cpi) {
3210
0
  if (cpi->oxcf.pass == 2) {
3211
0
    TWO_PASS *const twopass = &cpi->twopass;
3212
0
    GF_GROUP *const gf_group = &twopass->gf_group;
3213
0
    const int gfg_index = gf_group->index;
3214
3215
0
    if (gfg_index == 0) return gf_group->update_type[gfg_index] == LF_UPDATE;
3216
0
    return gf_group->update_type[gfg_index - 1] != LF_UPDATE &&
3217
0
           gf_group->update_type[gfg_index] == LF_UPDATE;
3218
0
  } else {
3219
0
    return 0;
3220
0
  }
3221
0
}
3222
3223
// Function to test for conditions that indicate we should loop
3224
// back and recode a frame.
3225
static int recode_loop_test(VP9_COMP *cpi, int high_limit, int low_limit, int q,
3226
0
                            int maxq, int minq) {
3227
0
  const RATE_CONTROL *const rc = &cpi->rc;
3228
0
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3229
0
  const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3230
0
  int force_recode = 0;
3231
3232
0
  if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3233
0
      big_rate_miss(cpi) || (cpi->sf.recode_loop == ALLOW_RECODE) ||
3234
0
      (two_pass_first_group_inter(cpi) &&
3235
0
       (cpi->sf.recode_loop == ALLOW_RECODE_FIRST)) ||
3236
0
      (frame_is_kfgfarf && (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF))) {
3237
0
    if (frame_is_kfgfarf && (oxcf->resize_mode == RESIZE_DYNAMIC) &&
3238
0
        scale_down(cpi, q)) {
3239
      // Code this group at a lower resolution.
3240
0
      cpi->resize_pending = 1;
3241
0
      return 1;
3242
0
    }
3243
3244
    // Force recode for extreme overshoot.
3245
0
    if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3246
0
        (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
3247
0
         rc->projected_frame_size >= big_rate_miss_high_threshold(cpi))) {
3248
0
      return 1;
3249
0
    }
3250
3251
    // TODO(agrange) high_limit could be greater than the scale-down threshold.
3252
0
    if ((rc->projected_frame_size > high_limit && q < maxq) ||
3253
0
        (rc->projected_frame_size < low_limit && q > minq)) {
3254
0
      force_recode = 1;
3255
0
    } else if (cpi->oxcf.rc_mode == VPX_CQ) {
3256
      // Deal with frame undershoot and whether or not we are
3257
      // below the automatically set cq level.
3258
0
      if (q > oxcf->cq_level &&
3259
0
          rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3260
0
        force_recode = 1;
3261
0
      }
3262
0
    }
3263
0
  }
3264
0
  return force_recode;
3265
0
}
3266
#endif  // !CONFIG_REALTIME_ONLY
3267
3268
53.0k
static void update_ref_frames(VP9_COMP *cpi) {
3269
53.0k
  VP9_COMMON *const cm = &cpi->common;
3270
53.0k
  BufferPool *const pool = cm->buffer_pool;
3271
53.0k
  GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3272
3273
53.0k
  if (cpi->ext_ratectrl.ready &&
3274
53.0k
      (cpi->ext_ratectrl.funcs.rc_type & VPX_RC_GOP) != 0 &&
3275
53.0k
      cpi->ext_ratectrl.funcs.get_gop_decision != NULL) {
3276
0
    const int this_gf_index = gf_group->index;
3277
0
    const int update_ref_idx = gf_group->update_ref_idx[this_gf_index];
3278
0
    if (gf_group->update_type[this_gf_index] == KF_UPDATE) {
3279
0
      ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[0], cm->new_fb_idx);
3280
0
      ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[1], cm->new_fb_idx);
3281
0
      ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[2], cm->new_fb_idx);
3282
0
    } else if (update_ref_idx != INVALID_IDX) {
3283
0
      ref_cnt_fb(pool->frame_bufs,
3284
0
                 &cm->ref_frame_map[gf_group->update_ref_idx[this_gf_index]],
3285
0
                 cm->new_fb_idx);
3286
0
    }
3287
3288
0
    const int next_gf_index = gf_group->index + 1;
3289
3290
    // Overlay frame should ideally look at the colocated ref frame from rc lib.
3291
    // Here temporarily just don't update the indices.
3292
0
    if (next_gf_index < gf_group->gf_group_size) {
3293
0
      cpi->lst_fb_idx = gf_group->ext_rc_ref[next_gf_index].last_index;
3294
0
      cpi->gld_fb_idx = gf_group->ext_rc_ref[next_gf_index].golden_index;
3295
0
      cpi->alt_fb_idx = gf_group->ext_rc_ref[next_gf_index].altref_index;
3296
0
    }
3297
3298
0
    return;
3299
0
  }
3300
3301
53.0k
  if (cpi->rc.show_arf_as_gld) {
3302
0
    int tmp = cpi->alt_fb_idx;
3303
0
    cpi->alt_fb_idx = cpi->gld_fb_idx;
3304
0
    cpi->gld_fb_idx = tmp;
3305
53.0k
  } else if (cm->show_existing_frame) {
3306
    // Pop ARF.
3307
0
    cpi->lst_fb_idx = cpi->alt_fb_idx;
3308
0
    cpi->alt_fb_idx =
3309
0
        stack_pop(gf_group->arf_index_stack, gf_group->stack_size);
3310
0
    --gf_group->stack_size;
3311
0
  }
3312
3313
  // At this point the new frame has been encoded.
3314
  // If any buffer copy / swapping is signaled it should be done here.
3315
53.0k
  if (cm->frame_type == KEY_FRAME) {
3316
12.4k
    ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3317
12.4k
               cm->new_fb_idx);
3318
12.4k
    ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3319
12.4k
               cm->new_fb_idx);
3320
40.6k
  } else if (vp9_preserve_existing_gf(cpi)) {
3321
    // We have decided to preserve the previously existing golden frame as our
3322
    // new ARF frame. However, in the short term in function
3323
    // vp9_get_refresh_mask() we left it in the GF slot and, if
3324
    // we're updating the GF with the current decoded frame, we save it to the
3325
    // ARF slot instead.
3326
    // We now have to update the ARF with the current frame and swap gld_fb_idx
3327
    // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3328
    // slot and, if we're updating the GF, the current frame becomes the new GF.
3329
0
    int tmp;
3330
3331
0
    ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3332
0
               cm->new_fb_idx);
3333
3334
0
    tmp = cpi->alt_fb_idx;
3335
0
    cpi->alt_fb_idx = cpi->gld_fb_idx;
3336
0
    cpi->gld_fb_idx = tmp;
3337
40.6k
  } else { /* For non key/golden frames */
3338
40.6k
    if (cpi->refresh_alt_ref_frame) {
3339
0
      int arf_idx = gf_group->top_arf_idx;
3340
3341
      // Push new ARF into stack.
3342
0
      stack_push(gf_group->arf_index_stack, cpi->alt_fb_idx,
3343
0
                 gf_group->stack_size);
3344
0
      ++gf_group->stack_size;
3345
3346
0
      assert(arf_idx < REF_FRAMES);
3347
3348
0
      ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
3349
0
      memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
3350
0
             cpi->interp_filter_selected[0],
3351
0
             sizeof(cpi->interp_filter_selected[0]));
3352
3353
0
      cpi->alt_fb_idx = arf_idx;
3354
0
    }
3355
3356
40.6k
    if (cpi->refresh_golden_frame) {
3357
2.76k
      ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3358
2.76k
                 cm->new_fb_idx);
3359
2.76k
      if (!cpi->rc.is_src_frame_alt_ref)
3360
2.76k
        memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3361
2.76k
               cpi->interp_filter_selected[0],
3362
2.76k
               sizeof(cpi->interp_filter_selected[0]));
3363
0
      else
3364
0
        memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3365
0
               cpi->interp_filter_selected[ALTREF_FRAME],
3366
0
               sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
3367
2.76k
    }
3368
40.6k
  }
3369
3370
53.0k
  if (cpi->refresh_last_frame) {
3371
53.0k
    ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
3372
53.0k
               cm->new_fb_idx);
3373
53.0k
    if (!cpi->rc.is_src_frame_alt_ref)
3374
53.0k
      memcpy(cpi->interp_filter_selected[LAST_FRAME],
3375
53.0k
             cpi->interp_filter_selected[0],
3376
53.0k
             sizeof(cpi->interp_filter_selected[0]));
3377
53.0k
  }
3378
3379
53.0k
  if (gf_group->update_type[gf_group->index] == MID_OVERLAY_UPDATE) {
3380
0
    cpi->alt_fb_idx =
3381
0
        stack_pop(gf_group->arf_index_stack, gf_group->stack_size);
3382
0
    --gf_group->stack_size;
3383
0
  }
3384
53.0k
}
3385
3386
53.0k
void vp9_update_reference_frames(VP9_COMP *cpi) {
3387
53.0k
  update_ref_frames(cpi);
3388
3389
#if CONFIG_VP9_TEMPORAL_DENOISING
3390
  vp9_denoiser_update_ref_frame(cpi);
3391
#endif
3392
3393
53.0k
  if (is_one_pass_svc(cpi)) vp9_svc_update_ref_frame(cpi);
3394
53.0k
}
3395
3396
53.1k
static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
3397
53.1k
  MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
3398
53.1k
  struct loopfilter *lf = &cm->lf;
3399
53.1k
  int is_reference_frame =
3400
53.1k
      (cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
3401
53.1k
       cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame);
3402
53.1k
  if (cpi->use_svc &&
3403
53.1k
      cpi->svc.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS)
3404
0
    is_reference_frame = !cpi->svc.non_reference_frame;
3405
3406
  // Skip loop filter in show_existing_frame mode.
3407
53.1k
  if (cm->show_existing_frame) {
3408
0
    lf->filter_level = 0;
3409
0
    return;
3410
0
  }
3411
3412
53.1k
  if (cpi->loopfilter_ctrl == NO_LOOPFILTER ||
3413
53.1k
      (!is_reference_frame && cpi->loopfilter_ctrl == LOOPFILTER_REFERENCE)) {
3414
0
    lf->filter_level = 0;
3415
0
    vpx_extend_frame_inner_borders(cm->frame_to_show);
3416
0
    return;
3417
0
  }
3418
3419
53.1k
  if (xd->lossless) {
3420
7.24k
    lf->filter_level = 0;
3421
7.24k
    lf->last_filt_level = 0;
3422
45.8k
  } else {
3423
#if CONFIG_INTERNAL_STATS
3424
    struct vpx_usec_timer timer;
3425
#endif
3426
3427
45.8k
    vpx_clear_system_state();
3428
3429
#if CONFIG_INTERNAL_STATS
3430
    vpx_usec_timer_start(&timer);
3431
#endif
3432
3433
45.8k
    if (!cpi->rc.is_src_frame_alt_ref) {
3434
45.8k
      if ((cpi->common.frame_type == KEY_FRAME) &&
3435
45.8k
          (!cpi->rc.this_key_frame_forced)) {
3436
9.10k
        lf->last_filt_level = 0;
3437
9.10k
      }
3438
45.8k
      vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
3439
45.8k
      lf->last_filt_level = lf->filter_level;
3440
45.8k
    } else {
3441
0
      lf->filter_level = 0;
3442
0
    }
3443
3444
#if CONFIG_INTERNAL_STATS
3445
    vpx_usec_timer_mark(&timer);
3446
    cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
3447
#endif
3448
45.8k
  }
3449
3450
53.1k
  if (lf->filter_level > 0 && is_reference_frame) {
3451
31.3k
    vp9_build_mask_frame(cm, lf->filter_level, 0);
3452
3453
31.3k
    if (cpi->num_workers > 1)
3454
0
      vp9_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
3455
0
                               lf->filter_level, 0, 0, cpi->workers,
3456
0
                               cpi->num_workers, &cpi->lf_row_sync);
3457
31.3k
    else
3458
31.3k
      vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
3459
31.3k
  }
3460
3461
53.1k
  vpx_extend_frame_inner_borders(cm->frame_to_show);
3462
53.1k
}
3463
3464
40.6k
void vp9_scale_references(VP9_COMP *cpi) {
3465
40.6k
  VP9_COMMON *cm = &cpi->common;
3466
40.6k
  MV_REFERENCE_FRAME ref_frame;
3467
40.6k
  const VP9_REFFRAME ref_mask[3] = { VP9_LAST_FLAG, VP9_GOLD_FLAG,
3468
40.6k
                                     VP9_ALT_FLAG };
3469
3470
162k
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3471
    // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
3472
122k
    if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3473
99.3k
      BufferPool *const pool = cm->buffer_pool;
3474
99.3k
      const YV12_BUFFER_CONFIG *const ref =
3475
99.3k
          get_ref_frame_buffer(cpi, ref_frame);
3476
3477
99.3k
      if (ref == NULL) {
3478
0
        cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3479
0
        continue;
3480
0
      }
3481
3482
99.3k
      if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3483
0
        RefCntBuffer *new_fb_ptr = NULL;
3484
0
        int force_scaling = 0;
3485
0
        int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3486
0
        if (new_fb == INVALID_IDX) {
3487
0
          new_fb = get_free_fb(cm);
3488
0
          force_scaling = 1;
3489
0
        }
3490
0
        if (new_fb == INVALID_IDX) return;
3491
0
        new_fb_ptr = &pool->frame_bufs[new_fb];
3492
0
        if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3493
0
            new_fb_ptr->buf.y_crop_height != cm->height) {
3494
0
#if CONFIG_VP9_HIGHBITDEPTH
3495
0
          if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
3496
0
                                       cm->subsampling_x, cm->subsampling_y,
3497
0
                                       cm->use_highbitdepth,
3498
0
                                       VP9_ENC_BORDER_IN_PIXELS,
3499
0
                                       cm->byte_alignment, NULL, NULL, NULL))
3500
0
            vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3501
0
                               "Failed to allocate frame buffer");
3502
0
          scale_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth,
3503
0
                                 EIGHTTAP, 0);
3504
#else
3505
          if (vpx_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
3506
                                       cm->subsampling_x, cm->subsampling_y,
3507
                                       VP9_ENC_BORDER_IN_PIXELS,
3508
                                       cm->byte_alignment, NULL, NULL, NULL))
3509
            vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3510
                               "Failed to allocate frame buffer");
3511
          vp9_scale_and_extend_frame(ref, &new_fb_ptr->buf, EIGHTTAP, 0);
3512
#endif  // CONFIG_VP9_HIGHBITDEPTH
3513
0
          cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3514
0
          alloc_frame_mvs(cm, new_fb);
3515
0
        }
3516
99.3k
      } else {
3517
99.3k
        int buf_idx;
3518
99.3k
        RefCntBuffer *buf = NULL;
3519
99.3k
        if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
3520
          // Check for release of scaled reference.
3521
99.3k
          buf_idx = cpi->scaled_ref_idx[ref_frame - 1];
3522
99.3k
          if (buf_idx != INVALID_IDX) {
3523
0
            buf = &pool->frame_bufs[buf_idx];
3524
0
            --buf->ref_count;
3525
0
            cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3526
0
          }
3527
99.3k
        }
3528
99.3k
        buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3529
99.3k
        buf = &pool->frame_bufs[buf_idx];
3530
99.3k
        buf->buf.y_crop_width = ref->y_crop_width;
3531
99.3k
        buf->buf.y_crop_height = ref->y_crop_height;
3532
99.3k
        cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3533
99.3k
        ++buf->ref_count;
3534
99.3k
      }
3535
99.3k
    } else {
3536
22.6k
      if (cpi->oxcf.pass != 0 || cpi->use_svc)
3537
0
        cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3538
22.6k
    }
3539
122k
  }
3540
40.6k
}
3541
3542
40.6k
static void release_scaled_references(VP9_COMP *cpi) {
3543
40.6k
  VP9_COMMON *cm = &cpi->common;
3544
40.6k
  int i;
3545
40.6k
  if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
3546
    // Only release scaled references under certain conditions:
3547
    // if reference will be updated, or if scaled reference has same resolution.
3548
40.6k
    int refresh[3];
3549
40.6k
    refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
3550
40.6k
    refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
3551
40.6k
    refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
3552
162k
    for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3553
121k
      const int idx = cpi->scaled_ref_idx[i - 1];
3554
121k
      if (idx != INVALID_IDX) {
3555
99.2k
        RefCntBuffer *const buf = &cm->buffer_pool->frame_bufs[idx];
3556
99.2k
        const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
3557
99.2k
        if (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
3558
99.2k
                               buf->buf.y_crop_height == ref->y_crop_height)) {
3559
99.2k
          --buf->ref_count;
3560
99.2k
          cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
3561
99.2k
        }
3562
99.2k
      }
3563
121k
    }
3564
40.6k
  } else {
3565
0
    for (i = 0; i < REFS_PER_FRAME; ++i) {
3566
0
      const int idx = cpi->scaled_ref_idx[i];
3567
0
      if (idx != INVALID_IDX) {
3568
0
        RefCntBuffer *const buf = &cm->buffer_pool->frame_bufs[idx];
3569
0
        --buf->ref_count;
3570
0
        cpi->scaled_ref_idx[i] = INVALID_IDX;
3571
0
      }
3572
0
    }
3573
0
  }
3574
40.6k
}
3575
3576
static void full_to_model_count(unsigned int *model_count,
3577
28.0M
                                unsigned int *full_count) {
3578
28.0M
  int n;
3579
28.0M
  model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
3580
28.0M
  model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
3581
28.0M
  model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
3582
252M
  for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
3583
224M
    model_count[TWO_TOKEN] += full_count[n];
3584
28.0M
  model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
3585
28.0M
}
3586
3587
static void full_to_model_counts(vp9_coeff_count_model *model_count,
3588
212k
                                 vp9_coeff_count *full_count) {
3589
212k
  int i, j, k, l;
3590
3591
636k
  for (i = 0; i < PLANE_TYPES; ++i)
3592
1.27M
    for (j = 0; j < REF_TYPES; ++j)
3593
5.94M
      for (k = 0; k < COEF_BANDS; ++k)
3594
33.1M
        for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
3595
28.0M
          full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
3596
212k
}
3597
3598
#if 0 && CONFIG_INTERNAL_STATS
3599
static void output_frame_level_debug_stats(VP9_COMP *cpi) {
3600
  VP9_COMMON *const cm = &cpi->common;
3601
  FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
3602
  int64_t recon_err;
3603
3604
  vpx_clear_system_state();
3605
3606
#if CONFIG_VP9_HIGHBITDEPTH
3607
  if (cm->use_highbitdepth) {
3608
    recon_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3609
  } else {
3610
    recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3611
  }
3612
#else
3613
  recon_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3614
#endif  // CONFIG_VP9_HIGHBITDEPTH
3615
3616
3617
  if (cpi->twopass.total_left_stats.coded_error != 0.0) {
3618
    double dc_quant_devisor;
3619
#if CONFIG_VP9_HIGHBITDEPTH
3620
    switch (cm->bit_depth) {
3621
      case VPX_BITS_8:
3622
        dc_quant_devisor = 4.0;
3623
        break;
3624
      case VPX_BITS_10:
3625
        dc_quant_devisor = 16.0;
3626
        break;
3627
      default:
3628
        assert(cm->bit_depth == VPX_BITS_12);
3629
        dc_quant_devisor = 64.0;
3630
        break;
3631
    }
3632
#else
3633
    dc_quant_devisor = 4.0;
3634
#endif
3635
3636
    if (!cm->current_video_frame) {
3637
      fprintf(f, "frame, width, height, last ts, last end ts, "
3638
          "source_alt_ref_pending, source_alt_ref_active, "
3639
          "this_frame_target, projected_frame_size, "
3640
          "projected_frame_size / MBs, "
3641
          "projected_frame_size - this_frame_target, "
3642
          "vbr_bits_off_target, vbr_bits_off_target_fast, "
3643
          "twopass.extend_minq, twopass.extend_minq_fast, "
3644
          "total_target_vs_actual, "
3645
          "starting_buffer_level - bits_off_target, "
3646
          "total_actual_bits, base_qindex, q for base_qindex, "
3647
          "dc quant, q for active_worst_quality, avg_q, q for oxcf.cq_level, "
3648
          "refresh_last_frame, refresh_golden_frame, refresh_alt_ref_frame, "
3649
          "frame_type, gfu_boost, "
3650
          "twopass.bits_left, "
3651
          "twopass.total_left_stats.coded_error, "
3652
          "twopass.bits_left / (1 + twopass.total_left_stats.coded_error), "
3653
          "tot_recode_hits, recon_err, kf_boost, "
3654
          "twopass.kf_zeromotion_pct, twopass.fr_content_type, "
3655
          "filter_level, seg.aq_av_offset\n");
3656
    }
3657
3658
    fprintf(f, "%10u, %d, %d, %10"PRId64", %10"PRId64", %d, %d, %10d, %10d, "
3659
        "%10d, %10d, %10"PRId64", %10"PRId64", %5d, %5d, %10"PRId64", "
3660
        "%10"PRId64", %10"PRId64", %10d, %7.2lf, %7.2lf, %7.2lf, %7.2lf, "
3661
        "%7.2lf, %6d, %6d, %5d, %5d, %5d, %10"PRId64", %10.3lf, %10lf, %8u, "
3662
        "%10"PRId64", %10d, %10d, %10d, %10d, %10d\n",
3663
        cpi->common.current_video_frame,
3664
        cm->width, cm->height,
3665
        cpi->last_time_stamp_seen,
3666
        cpi->last_end_time_stamp_seen,
3667
        cpi->rc.source_alt_ref_pending,
3668
        cpi->rc.source_alt_ref_active,
3669
        cpi->rc.this_frame_target,
3670
        cpi->rc.projected_frame_size,
3671
        cpi->rc.projected_frame_size / cpi->common.MBs,
3672
        (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
3673
        cpi->rc.vbr_bits_off_target,
3674
        cpi->rc.vbr_bits_off_target_fast,
3675
        cpi->twopass.extend_minq,
3676
        cpi->twopass.extend_minq_fast,
3677
        cpi->rc.total_target_vs_actual,
3678
        (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
3679
        cpi->rc.total_actual_bits, cm->base_qindex,
3680
        vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
3681
        (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) /
3682
            dc_quant_devisor,
3683
        vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
3684
                                cm->bit_depth),
3685
        cpi->rc.avg_q,
3686
        vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
3687
        cpi->refresh_last_frame, cpi->refresh_golden_frame,
3688
        cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
3689
        cpi->twopass.bits_left,
3690
        cpi->twopass.total_left_stats.coded_error,
3691
        cpi->twopass.bits_left /
3692
            (1 + cpi->twopass.total_left_stats.coded_error),
3693
        cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
3694
        cpi->twopass.kf_zeromotion_pct,
3695
        cpi->twopass.fr_content_type,
3696
        cm->lf.filter_level,
3697
        cm->seg.aq_av_offset);
3698
  }
3699
  fclose(f);
3700
3701
  if (0) {
3702
    FILE *const fmodes = fopen("Modes.stt", "a");
3703
    int i;
3704
3705
    fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
3706
            cm->frame_type, cpi->refresh_golden_frame,
3707
            cpi->refresh_alt_ref_frame);
3708
3709
    for (i = 0; i < MAX_MODES; ++i)
3710
      fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
3711
3712
    fprintf(fmodes, "\n");
3713
3714
    fclose(fmodes);
3715
  }
3716
}
3717
#endif
3718
3719
0
static void set_mv_search_params(VP9_COMP *cpi) {
3720
0
  const VP9_COMMON *const cm = &cpi->common;
3721
0
  const unsigned int max_mv_def = VPXMIN(cm->width, cm->height);
3722
3723
  // Default based on max resolution.
3724
0
  cpi->mv_step_param = vp9_init_search_range(max_mv_def);
3725
3726
0
  if (cpi->sf.mv.auto_mv_step_size) {
3727
0
    if (frame_is_intra_only(cm)) {
3728
      // Initialize max_mv_magnitude for use in the first INTER frame
3729
      // after a key/intra-only frame.
3730
0
      cpi->max_mv_magnitude = max_mv_def;
3731
0
    } else {
3732
0
      if (cm->show_frame) {
3733
        // Allow mv_steps to correspond to twice the max mv magnitude found
3734
        // in the previous frame, capped by the default max_mv_magnitude based
3735
        // on resolution.
3736
0
        cpi->mv_step_param = vp9_init_search_range(
3737
0
            VPXMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
3738
0
      }
3739
0
      cpi->max_mv_magnitude = 0;
3740
0
    }
3741
0
  }
3742
0
}
3743
3744
53.1k
static void set_size_independent_vars(VP9_COMP *cpi) {
3745
53.1k
  vp9_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
3746
53.1k
  vp9_set_rd_speed_thresholds(cpi);
3747
53.1k
  vp9_set_rd_speed_thresholds_sub8x8(cpi);
3748
53.1k
  cpi->common.interp_filter = cpi->sf.default_interp_filter;
3749
53.1k
}
3750
3751
static void set_size_dependent_vars(VP9_COMP *cpi, int *q, int *bottom_index,
3752
53.1k
                                    int *top_index) {
3753
53.1k
  VP9_COMMON *const cm = &cpi->common;
3754
3755
  // Setup variables that depend on the dimensions of the frame.
3756
53.1k
  vp9_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
3757
3758
  // Decide q and q bounds.
3759
53.1k
  *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
3760
3761
53.1k
  if (cpi->oxcf.rc_mode == VPX_CBR && cpi->rc.force_max_q) {
3762
0
    *q = cpi->rc.worst_quality;
3763
0
    cpi->rc.force_max_q = 0;
3764
0
  }
3765
3766
53.1k
  if (cpi->use_svc) {
3767
0
    cpi->svc.base_qindex[cpi->svc.spatial_layer_id] = *q;
3768
0
  }
3769
3770
53.1k
  if (!frame_is_intra_only(cm)) {
3771
40.6k
    vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
3772
40.6k
  }
3773
3774
53.1k
#if !CONFIG_REALTIME_ONLY
3775
  // Configure experimental use of segmentation for enhanced coding of
3776
  // static regions if indicated.
3777
  // Only allowed in the second pass of a two pass encode, as it requires
3778
  // lagged coding, and if the relevant speed feature flag is set.
3779
53.1k
  if (cpi->oxcf.pass == 2 && cpi->sf.static_segmentation)
3780
0
    configure_static_seg_features(cpi);
3781
53.1k
#endif  // !CONFIG_REALTIME_ONLY
3782
3783
#if CONFIG_VP9_POSTPROC && !(CONFIG_VP9_TEMPORAL_DENOISING)
3784
  if (cpi->oxcf.noise_sensitivity > 0) {
3785
    int l = 0;
3786
    switch (cpi->oxcf.noise_sensitivity) {
3787
      case 1: l = 20; break;
3788
      case 2: l = 40; break;
3789
      case 3: l = 60; break;
3790
      case 4:
3791
      case 5: l = 100; break;
3792
      case 6: l = 150; break;
3793
    }
3794
    if (!cpi->common.postproc_state.limits) {
3795
      CHECK_MEM_ERROR(&cm->error, cpi->common.postproc_state.limits,
3796
                      vpx_calloc(cpi->un_scaled_source->y_width,
3797
                                 sizeof(*cpi->common.postproc_state.limits)));
3798
    }
3799
    vp9_denoise(&cpi->common, cpi->Source, cpi->Source, l,
3800
                cpi->common.postproc_state.limits);
3801
  }
3802
#endif  // CONFIG_VP9_POSTPROC
3803
53.1k
}
3804
3805
106k
static void init_motion_estimation(VP9_COMP *cpi) {
3806
106k
  int y_stride = cpi->scaled_source.y_stride;
3807
3808
106k
  if (cpi->sf.mv.search_method == NSTEP) {
3809
106k
    vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
3810
106k
  } else if (cpi->sf.mv.search_method == DIAMOND) {
3811
0
    vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
3812
0
  }
3813
106k
}
3814
3815
53.1k
static void set_frame_size(VP9_COMP *cpi) {
3816
53.1k
  int ref_frame;
3817
53.1k
  VP9_COMMON *const cm = &cpi->common;
3818
53.1k
  VP9EncoderConfig *const oxcf = &cpi->oxcf;
3819
53.1k
  MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
3820
3821
53.1k
#if !CONFIG_REALTIME_ONLY
3822
53.1k
  if (oxcf->pass == 2 && oxcf->rc_mode == VPX_VBR &&
3823
53.1k
      ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
3824
0
       (oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending))) {
3825
0
    calculate_coded_size(cpi, &oxcf->scaled_frame_width,
3826
0
                         &oxcf->scaled_frame_height);
3827
3828
    // There has been a change in frame size.
3829
0
    vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3830
0
                         oxcf->scaled_frame_height);
3831
0
  }
3832
53.1k
#endif  // !CONFIG_REALTIME_ONLY
3833
3834
53.1k
  if (oxcf->pass == 0 && oxcf->rc_mode == VPX_CBR &&
3835
53.1k
      oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending != 0) {
3836
    // For SVC scaled width/height will have been set (svc->resize_set=1)
3837
    // in get_svc_params based on the layer width/height.
3838
0
    if (!cpi->use_svc || !cpi->svc.resize_set) {
3839
0
      oxcf->scaled_frame_width =
3840
0
          (oxcf->width * cpi->resize_scale_num) / cpi->resize_scale_den;
3841
0
      oxcf->scaled_frame_height =
3842
0
          (oxcf->height * cpi->resize_scale_num) / cpi->resize_scale_den;
3843
      // There has been a change in frame size.
3844
0
      vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3845
0
                           oxcf->scaled_frame_height);
3846
0
    }
3847
3848
    // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3849
0
    set_mv_search_params(cpi);
3850
3851
0
    vp9_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
3852
#if CONFIG_VP9_TEMPORAL_DENOISING
3853
    // Reset the denoiser on the resized frame.
3854
    if (cpi->oxcf.noise_sensitivity > 0) {
3855
      vp9_denoiser_free(&(cpi->denoiser));
3856
      setup_denoiser_buffer(cpi);
3857
      // Dynamic resize is only triggered for non-SVC, so we can force
3858
      // golden frame update here as temporary fix to denoiser.
3859
      cpi->refresh_golden_frame = 1;
3860
    }
3861
#endif
3862
0
  }
3863
3864
53.1k
  if ((oxcf->pass == 2) && !cpi->use_svc) {
3865
0
    vp9_set_target_rate(cpi);
3866
0
  }
3867
3868
53.1k
  alloc_frame_mvs(cm, cm->new_fb_idx);
3869
3870
  // Reset the frame pointers to the current frame size.
3871
53.1k
  if (vpx_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
3872
53.1k
                               cm->subsampling_x, cm->subsampling_y,
3873
53.1k
#if CONFIG_VP9_HIGHBITDEPTH
3874
53.1k
                               cm->use_highbitdepth,
3875
53.1k
#endif
3876
53.1k
                               VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
3877
53.1k
                               NULL, NULL, NULL))
3878
0
    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
3879
0
                       "Failed to allocate frame buffer");
3880
3881
53.1k
  alloc_util_frame_buffers(cpi);
3882
53.1k
  init_motion_estimation(cpi);
3883
3884
53.1k
  int has_valid_ref_frame = 0;
3885
212k
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3886
159k
    RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
3887
159k
    const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3888
3889
159k
    ref_buf->idx = buf_idx;
3890
3891
159k
    if (buf_idx != INVALID_IDX) {
3892
147k
      YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3893
147k
      ref_buf->buf = buf;
3894
147k
#if CONFIG_VP9_HIGHBITDEPTH
3895
147k
      vp9_setup_scale_factors_for_frame(
3896
147k
          &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
3897
147k
          cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
3898
#else
3899
      vp9_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3900
                                        buf->y_crop_height, cm->width,
3901
                                        cm->height);
3902
#endif  // CONFIG_VP9_HIGHBITDEPTH
3903
147k
      has_valid_ref_frame |= vp9_is_valid_scale(&ref_buf->sf);
3904
147k
      if (vp9_is_scaled(&ref_buf->sf)) vpx_extend_frame_borders(buf);
3905
147k
    } else {
3906
11.6k
      ref_buf->buf = NULL;
3907
11.6k
    }
3908
159k
  }
3909
53.1k
  if (!frame_is_intra_only(cm) && !has_valid_ref_frame) {
3910
0
    vpx_internal_error(
3911
0
        &cm->error, VPX_CODEC_ERROR,
3912
0
        "Can't find at least one reference frame with valid size");
3913
0
  }
3914
3915
53.1k
  set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3916
53.1k
}
3917
3918
53.1k
static void save_encode_params(VP9_COMP *cpi) {
3919
53.1k
  int tile_idx;
3920
53.1k
  int i, j;
3921
53.1k
  TileDataEnc *tile_data;
3922
53.1k
  RD_OPT *rd_opt = &cpi->rd;
3923
265k
  for (i = 0; i < MAX_REF_FRAMES; i++) {
3924
850k
    for (j = 0; j < REFERENCE_MODES; j++)
3925
637k
      rd_opt->prediction_type_threshes_prev[i][j] =
3926
637k
          rd_opt->prediction_type_threshes[i][j];
3927
3928
1.06M
    for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; j++)
3929
850k
      rd_opt->filter_threshes_prev[i][j] = rd_opt->filter_threshes[i][j];
3930
212k
  }
3931
3932
103k
  for (tile_idx = 0; tile_idx < cpi->allocated_tiles; tile_idx++) {
3933
50.3k
    assert(cpi->tile_data);
3934
50.3k
    tile_data = &cpi->tile_data[tile_idx];
3935
50.3k
    vp9_copy(tile_data->thresh_freq_fact_prev, tile_data->thresh_freq_fact);
3936
50.3k
  }
3937
53.1k
}
3938
3939
0
static INLINE void set_raw_source_frame(VP9_COMP *cpi) {
3940
#ifdef ENABLE_KF_DENOISE
3941
  if (is_spatial_denoise_enabled(cpi)) {
3942
    cpi->raw_source_frame = vp9_scale_if_required(
3943
        cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
3944
        (oxcf->pass == 0), EIGHTTAP, 0);
3945
  } else {
3946
    cpi->raw_source_frame = cpi->Source;
3947
  }
3948
#else
3949
0
  cpi->raw_source_frame = cpi->Source;
3950
0
#endif
3951
0
}
3952
3953
static YV12_BUFFER_CONFIG *svc_twostage_scale(
3954
    VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
3955
    YV12_BUFFER_CONFIG *scaled_temp, INTERP_FILTER filter_type,
3956
0
    int phase_scaler, INTERP_FILTER filter_type2, int phase_scaler2) {
3957
0
  if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
3958
0
      cm->mi_rows * MI_SIZE != unscaled->y_height) {
3959
0
#if CONFIG_VP9_HIGHBITDEPTH
3960
0
    if (cm->bit_depth == VPX_BITS_8) {
3961
0
      vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
3962
0
                                 phase_scaler2);
3963
0
      vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type,
3964
0
                                 phase_scaler);
3965
0
    } else {
3966
0
      scale_and_extend_frame(unscaled, scaled_temp, (int)cm->bit_depth,
3967
0
                             filter_type2, phase_scaler2);
3968
0
      scale_and_extend_frame(scaled_temp, scaled, (int)cm->bit_depth,
3969
0
                             filter_type, phase_scaler);
3970
0
    }
3971
#else
3972
    vp9_scale_and_extend_frame(unscaled, scaled_temp, filter_type2,
3973
                               phase_scaler2);
3974
    vp9_scale_and_extend_frame(scaled_temp, scaled, filter_type, phase_scaler);
3975
#endif  // CONFIG_VP9_HIGHBITDEPTH
3976
0
    return scaled;
3977
0
  } else {
3978
0
    return unscaled;
3979
0
  }
3980
0
}
3981
3982
static int encode_without_recode_loop(VP9_COMP *cpi, size_t *size,
3983
53.1k
                                      uint8_t *dest, size_t dest_size) {
3984
53.1k
  VP9_COMMON *const cm = &cpi->common;
3985
53.1k
  SVC *const svc = &cpi->svc;
3986
53.1k
  int q = 0, bottom_index = 0, top_index = 0;
3987
53.1k
  int no_drop_scene_change = 0;
3988
53.1k
  const INTERP_FILTER filter_scaler =
3989
53.1k
      (is_one_pass_svc(cpi))
3990
53.1k
          ? svc->downsample_filter_type[svc->spatial_layer_id]
3991
53.1k
          : EIGHTTAP;
3992
53.1k
  const int phase_scaler =
3993
53.1k
      (is_one_pass_svc(cpi))
3994
53.1k
          ? svc->downsample_filter_phase[svc->spatial_layer_id]
3995
53.1k
          : 0;
3996
3997
53.1k
  if (cm->show_existing_frame) {
3998
0
    cpi->rc.this_frame_target = 0;
3999
0
    if (is_psnr_calc_enabled(cpi)) set_raw_source_frame(cpi);
4000
0
    return 1;
4001
0
  }
4002
4003
53.1k
  svc->time_stamp_prev[svc->spatial_layer_id] = svc->time_stamp_superframe;
4004
4005
  // Flag to check if its valid to compute the source sad (used for
4006
  // scene detection and for superblock content state in CBR mode).
4007
  // The flag may get reset below based on SVC or resizing state.
4008
53.1k
  cpi->compute_source_sad_onepass = cpi->oxcf.mode == REALTIME;
4009
4010
53.1k
  vpx_clear_system_state();
4011
4012
53.1k
  set_frame_size(cpi);
4013
4014
53.1k
  if (is_one_pass_svc(cpi) &&
4015
53.1k
      cpi->un_scaled_source->y_width == cm->width << 2 &&
4016
53.1k
      cpi->un_scaled_source->y_height == cm->height << 2 &&
4017
53.1k
      svc->scaled_temp.y_width == cm->width << 1 &&
4018
53.1k
      svc->scaled_temp.y_height == cm->height << 1) {
4019
    // For svc, if it is a 1/4x1/4 downscaling, do a two-stage scaling to take
4020
    // advantage of the 1:2 optimized scaler. In the process, the 1/2x1/2
4021
    // result will be saved in scaled_temp and might be used later.
4022
0
    const INTERP_FILTER filter_scaler2 = svc->downsample_filter_type[1];
4023
0
    const int phase_scaler2 = svc->downsample_filter_phase[1];
4024
0
    cpi->Source = svc_twostage_scale(
4025
0
        cm, cpi->un_scaled_source, &cpi->scaled_source, &svc->scaled_temp,
4026
0
        filter_scaler, phase_scaler, filter_scaler2, phase_scaler2);
4027
0
    svc->scaled_one_half = 1;
4028
53.1k
  } else if (is_one_pass_svc(cpi) &&
4029
53.1k
             cpi->un_scaled_source->y_width == cm->width << 1 &&
4030
53.1k
             cpi->un_scaled_source->y_height == cm->height << 1 &&
4031
53.1k
             svc->scaled_one_half) {
4032
    // If the spatial layer is 1/2x1/2 and the scaling is already done in the
4033
    // two-stage scaling, use the result directly.
4034
0
    cpi->Source = &svc->scaled_temp;
4035
0
    svc->scaled_one_half = 0;
4036
53.1k
  } else {
4037
53.1k
    cpi->Source = vp9_scale_if_required(
4038
53.1k
        cm, cpi->un_scaled_source, &cpi->scaled_source, (cpi->oxcf.pass == 0),
4039
53.1k
        filter_scaler, phase_scaler);
4040
53.1k
  }
4041
#ifdef OUTPUT_YUV_SVC_SRC
4042
  // Write out at most 3 spatial layers.
4043
  if (is_one_pass_svc(cpi) && svc->spatial_layer_id < 3) {
4044
    vpx_write_yuv_frame(yuv_svc_src[svc->spatial_layer_id], cpi->Source);
4045
  }
4046
#endif
4047
  // Unfiltered raw source used in metrics calculation if the source
4048
  // has been filtered.
4049
53.1k
  if (is_psnr_calc_enabled(cpi)) {
4050
#ifdef ENABLE_KF_DENOISE
4051
    if (is_spatial_denoise_enabled(cpi)) {
4052
      cpi->raw_source_frame = vp9_scale_if_required(
4053
          cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
4054
          (cpi->oxcf.pass == 0), EIGHTTAP, phase_scaler);
4055
    } else {
4056
      cpi->raw_source_frame = cpi->Source;
4057
    }
4058
#else
4059
0
    cpi->raw_source_frame = cpi->Source;
4060
0
#endif
4061
0
  }
4062
4063
53.1k
  if ((cpi->use_svc &&
4064
53.1k
       (svc->spatial_layer_id < svc->number_spatial_layers - 1 ||
4065
0
        svc->temporal_layer_id < svc->number_temporal_layers - 1 ||
4066
0
        svc->current_superframe < 1)) ||
4067
53.1k
      cpi->resize_pending || cpi->resize_state || cpi->external_resize ||
4068
53.1k
      cpi->resize_state != ORIG) {
4069
0
    cpi->compute_source_sad_onepass = 0;
4070
0
    if (cpi->content_state_sb_fd != NULL)
4071
0
      memset(cpi->content_state_sb_fd, 0,
4072
0
             (cm->mi_stride >> 3) * ((cm->mi_rows >> 3) + 1) *
4073
0
                 sizeof(*cpi->content_state_sb_fd));
4074
0
  }
4075
4076
  // Avoid scaling last_source unless its needed.
4077
  // Last source is needed if avg_source_sad() is used, or if noise estimation
4078
  // is enabled.
4079
53.1k
  if (cpi->unscaled_last_source != NULL &&
4080
53.1k
      (cpi->oxcf.content == VP9E_CONTENT_SCREEN ||
4081
49.2k
       (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_VBR &&
4082
49.2k
        cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5) ||
4083
49.2k
       (cpi->noise_estimate.enabled && !cpi->oxcf.noise_sensitivity) ||
4084
49.2k
       cpi->compute_source_sad_onepass))
4085
29.5k
    cpi->Last_Source = vp9_scale_if_required(
4086
29.5k
        cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
4087
29.5k
        (cpi->oxcf.pass == 0), EIGHTTAP, 0);
4088
4089
53.1k
  if (cpi->Last_Source == NULL ||
4090
53.1k
      cpi->Last_Source->y_width != cpi->Source->y_width ||
4091
53.1k
      cpi->Last_Source->y_height != cpi->Source->y_height)
4092
23.5k
    cpi->compute_source_sad_onepass = 0;
4093
4094
53.1k
  if (frame_is_intra_only(cm) || cpi->resize_pending != 0) {
4095
12.4k
    memset(cpi->consec_zero_mv, 0,
4096
12.4k
           cm->mi_rows * cm->mi_cols * sizeof(*cpi->consec_zero_mv));
4097
12.4k
  }
4098
4099
#if CONFIG_VP9_TEMPORAL_DENOISING
4100
  if (cpi->oxcf.noise_sensitivity > 0 && cpi->use_svc)
4101
    vp9_denoiser_reset_on_first_frame(cpi);
4102
#endif
4103
4104
  // Scene detection is always used for VBR mode or screen-content case.
4105
  // For other cases (e.g., CBR mode) use it for 5 <= speed.
4106
53.1k
  cpi->rc.high_source_sad = 0;
4107
53.1k
  cpi->rc.hybrid_intra_scene_change = 0;
4108
53.1k
  cpi->rc.re_encode_maxq_scene_change = 0;
4109
53.1k
  if (cm->show_frame && cpi->oxcf.mode == REALTIME &&
4110
53.1k
      !cpi->disable_scene_detection_rtc_ratectrl &&
4111
53.1k
      (cpi->oxcf.rc_mode == VPX_VBR ||
4112
31.9k
       cpi->oxcf.content == VP9E_CONTENT_SCREEN || cpi->oxcf.speed >= 5))
4113
29.3k
    vp9_scene_detection_onepass(cpi);
4114
4115
53.1k
  if (svc->spatial_layer_id == svc->first_spatial_layer_to_encode) {
4116
53.1k
    svc->high_source_sad_superframe = cpi->rc.high_source_sad;
4117
53.1k
    svc->high_num_blocks_with_motion = cpi->rc.high_num_blocks_with_motion;
4118
    // On scene change reset temporal layer pattern to TL0.
4119
    // Note that if the base/lower spatial layers are skipped: instead of
4120
    // inserting base layer here, we force max-q for the next superframe
4121
    // with lower spatial layers: this is done in vp9_encodedframe_overshoot()
4122
    // when max-q is decided for the current layer.
4123
    // Only do this reset for bypass/flexible mode.
4124
53.1k
    if (svc->high_source_sad_superframe && svc->temporal_layer_id > 0 &&
4125
53.1k
        svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
4126
      // rc->high_source_sad will get reset so copy it to restore it.
4127
0
      int tmp_high_source_sad = cpi->rc.high_source_sad;
4128
0
      vp9_svc_reset_temporal_layers(cpi, cm->frame_type == KEY_FRAME);
4129
0
      cpi->rc.high_source_sad = tmp_high_source_sad;
4130
0
    }
4131
53.1k
  }
4132
4133
53.1k
  vp9_update_noise_estimate(cpi);
4134
4135
  // For 1 pass CBR, check if we are dropping this frame.
4136
  // Never drop on key frame, if base layer is key for svc,
4137
  // on scene change, or if superframe has layer sync.
4138
53.1k
  if ((cpi->rc.high_source_sad || svc->high_source_sad_superframe) &&
4139
53.1k
      !(cpi->rc.use_post_encode_drop && svc->last_layer_dropped[0]))
4140
0
    no_drop_scene_change = 1;
4141
53.1k
  if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
4142
53.1k
      !frame_is_intra_only(cm) && !no_drop_scene_change &&
4143
53.1k
      !svc->superframe_has_layer_sync &&
4144
53.1k
      (!cpi->use_svc ||
4145
0
       !svc->layer_context[svc->temporal_layer_id].is_key_frame)) {
4146
0
    if (vp9_rc_drop_frame(cpi)) return 0;
4147
0
  }
4148
4149
  // For 1 pass SVC, only ZEROMV is allowed for spatial reference frame
4150
  // when svc->force_zero_mode_spatial_ref = 1. Under those conditions we can
4151
  // avoid this frame-level upsampling (for non intra_only frames).
4152
  // For SVC single_layer mode, dynamic resize is allowed and we need to
4153
  // scale references for this case.
4154
53.1k
  if (frame_is_intra_only(cm) == 0 &&
4155
53.1k
      ((svc->single_layer_svc && cpi->oxcf.resize_mode == RESIZE_DYNAMIC) ||
4156
40.6k
       !(is_one_pass_svc(cpi) && svc->force_zero_mode_spatial_ref))) {
4157
40.6k
    vp9_scale_references(cpi);
4158
40.6k
  }
4159
4160
53.1k
  set_size_independent_vars(cpi);
4161
53.1k
  set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4162
4163
  // search method and step parameter might be changed in speed settings.
4164
53.1k
  init_motion_estimation(cpi);
4165
4166
53.1k
  if (cpi->sf.copy_partition_flag) alloc_copy_partition_data(cpi);
4167
4168
53.1k
  if (cpi->sf.svc_use_lowres_part &&
4169
53.1k
      svc->spatial_layer_id == svc->number_spatial_layers - 2) {
4170
0
    if (svc->prev_partition_svc == NULL) {
4171
0
      CHECK_MEM_ERROR(
4172
0
          &cm->error, svc->prev_partition_svc,
4173
0
          (BLOCK_SIZE *)vpx_calloc(cm->mi_stride * cm->mi_rows,
4174
0
                                   sizeof(*svc->prev_partition_svc)));
4175
0
    }
4176
0
  }
4177
4178
  // TODO(jianj): Look into issue of skin detection with high bitdepth.
4179
53.1k
  if (cm->bit_depth == 8 && cpi->oxcf.speed >= 5 && cpi->oxcf.pass == 0 &&
4180
53.1k
      cpi->oxcf.rc_mode == VPX_CBR &&
4181
53.1k
      cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
4182
53.1k
      cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
4183
0
    cpi->use_skin_detection = 1;
4184
0
  }
4185
4186
  // Enable post encode frame dropping for CBR on non key frame, when
4187
  // ext_use_post_encode_drop is specified by user.
4188
53.1k
  cpi->rc.use_post_encode_drop = cpi->rc.ext_use_post_encode_drop &&
4189
53.1k
                                 cpi->oxcf.rc_mode == VPX_CBR &&
4190
53.1k
                                 cm->frame_type != KEY_FRAME;
4191
4192
53.1k
  vp9_set_quantizer(cpi, q, 0);
4193
53.1k
  vp9_set_variance_partition_thresholds(cpi, q, 0);
4194
4195
53.1k
  setup_frame(cpi);
4196
4197
53.1k
  suppress_active_map(cpi);
4198
4199
53.1k
  if (cpi->use_svc) {
4200
    // On non-zero spatial layer, check for disabling inter-layer
4201
    // prediction.
4202
0
    if (svc->spatial_layer_id > 0) vp9_svc_constrain_inter_layer_pred(cpi);
4203
0
    vp9_svc_assert_constraints_pattern(cpi);
4204
0
  }
4205
4206
53.1k
  if (cpi->rc.last_post_encode_dropped_scene_change) {
4207
0
    cpi->rc.high_source_sad = 1;
4208
0
    svc->high_source_sad_superframe = 1;
4209
    // For now disable use_source_sad since Last_Source will not be the previous
4210
    // encoded but the dropped one.
4211
0
    cpi->sf.use_source_sad = 0;
4212
0
    cpi->rc.last_post_encode_dropped_scene_change = 0;
4213
0
  }
4214
  // Check if this high_source_sad (scene/slide change) frame should be
4215
  // encoded at high/max QP, and if so, set the q and adjust some rate
4216
  // control parameters.
4217
53.1k
  if (cpi->sf.overshoot_detection_cbr_rt == FAST_DETECTION_MAXQ &&
4218
53.1k
      (cpi->rc.high_source_sad ||
4219
0
       (cpi->use_svc && svc->high_source_sad_superframe))) {
4220
0
    if (vp9_encodedframe_overshoot(cpi, -1, &q)) {
4221
0
      vp9_set_quantizer(cpi, q, 0);
4222
0
      vp9_set_variance_partition_thresholds(cpi, q, 0);
4223
0
    }
4224
0
  }
4225
4226
53.1k
#if !CONFIG_REALTIME_ONLY
4227
  // Variance adaptive and in frame q adjustment experiments are mutually
4228
  // exclusive.
4229
53.1k
  if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
4230
0
    vp9_vaq_frame_setup(cpi);
4231
53.1k
  } else if (cpi->oxcf.aq_mode == EQUATOR360_AQ) {
4232
0
    vp9_360aq_frame_setup(cpi);
4233
53.1k
  } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
4234
0
    vp9_setup_in_frame_q_adj(cpi);
4235
53.1k
  } else if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ) {
4236
    // it may be pretty bad for rate-control,
4237
    // and I should handle it somehow
4238
0
    vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
4239
53.1k
  } else {
4240
53.1k
#endif
4241
    // If ROI is enabled and skip feature is used for segmentation, apply cyclic
4242
    // refresh but not apply ROI for skip for the first 20 frames (defined by
4243
    // FRAMES_NO_SKIPPING_AFTER_KEY) after key frame to improve quality.
4244
53.1k
    if (cpi->roi.enabled && !frame_is_intra_only(cm)) {
4245
0
      if (cpi->roi.skip[BACKGROUND_SEG_SKIP_ID]) {
4246
0
        if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
4247
0
          vp9_cyclic_refresh_setup(cpi);
4248
0
        if (cpi->rc.frames_since_key > FRAMES_NO_SKIPPING_AFTER_KEY)
4249
0
          apply_roi_map(cpi);
4250
0
      } else {
4251
0
        apply_roi_map(cpi);
4252
0
      }
4253
53.1k
    } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
4254
0
      vp9_cyclic_refresh_setup(cpi);
4255
0
    }
4256
4257
53.1k
#if !CONFIG_REALTIME_ONLY
4258
53.1k
  }
4259
53.1k
#endif
4260
4261
53.1k
  apply_active_map(cpi);
4262
4263
53.1k
  vp9_encode_frame(cpi);
4264
4265
  // Check if we should re-encode this frame at high Q because of high
4266
  // overshoot based on the encoded frame size. Only for frames where
4267
  // high temporal-source SAD is detected.
4268
  // For SVC: all spatial layers are checked for re-encoding.
4269
53.1k
  if (cpi->sf.overshoot_detection_cbr_rt == RE_ENCODE_MAXQ &&
4270
53.1k
      (cpi->rc.high_source_sad ||
4271
0
       (cpi->use_svc && svc->high_source_sad_superframe))) {
4272
0
    int frame_size = 0;
4273
    // Get an estimate of the encoded frame size.
4274
0
    save_coding_context(cpi);
4275
0
    vp9_pack_bitstream(cpi, dest, dest_size, size);
4276
0
    restore_coding_context(cpi);
4277
0
    frame_size = (int)(*size) << 3;
4278
    // Check if encoded frame will overshoot too much, and if so, set the q and
4279
    // adjust some rate control parameters, and return to re-encode the frame.
4280
0
    if (vp9_encodedframe_overshoot(cpi, frame_size, &q)) {
4281
0
      vpx_clear_system_state();
4282
0
      vp9_set_quantizer(cpi, q, 0);
4283
0
      vp9_set_variance_partition_thresholds(cpi, q, 0);
4284
0
      suppress_active_map(cpi);
4285
      // Turn-off cyclic refresh for re-encoded frame.
4286
0
      if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
4287
0
        CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
4288
0
        unsigned char *const seg_map = cpi->segmentation_map;
4289
0
        memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
4290
0
        memset(cr->last_coded_q_map, MAXQ,
4291
0
               cm->mi_rows * cm->mi_cols * sizeof(*cr->last_coded_q_map));
4292
0
        cr->sb_index = 0;
4293
0
        vp9_disable_segmentation(&cm->seg);
4294
0
      }
4295
0
      apply_active_map(cpi);
4296
0
      vp9_encode_frame(cpi);
4297
0
    }
4298
0
  }
4299
4300
  // Update some stats from cyclic refresh, and check for golden frame update.
4301
53.1k
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
4302
53.1k
      !frame_is_intra_only(cm) && cpi->cyclic_refresh->content_mode)
4303
0
    vp9_cyclic_refresh_postencode(cpi);
4304
4305
  // Update the skip mb flag probabilities based on the distribution
4306
  // seen in the last encoder iteration.
4307
  // update_base_skip_probs(cpi);
4308
53.1k
  vpx_clear_system_state();
4309
53.1k
  return 1;
4310
53.1k
}
4311
4312
53.0k
static int get_ref_frame_flags(const VP9_COMP *cpi) {
4313
53.0k
  const int *const map = cpi->common.ref_frame_map;
4314
53.0k
  const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
4315
53.0k
  const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
4316
53.0k
  const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4317
53.0k
  int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
4318
4319
53.0k
  if (gold_is_last) flags &= ~VP9_GOLD_FLAG;
4320
4321
53.0k
  if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
4322
53.0k
      (cpi->svc.number_temporal_layers == 1 &&
4323
0
       cpi->svc.number_spatial_layers == 1))
4324
0
    flags &= ~VP9_GOLD_FLAG;
4325
4326
53.0k
  if (alt_is_last) flags &= ~VP9_ALT_FLAG;
4327
4328
53.0k
  if (gold_is_alt) flags &= ~VP9_ALT_FLAG;
4329
4330
53.0k
  return flags;
4331
53.0k
}
4332
4333
#if !CONFIG_REALTIME_ONLY
4334
#define MAX_QSTEP_ADJ 4
4335
0
static int get_qstep_adj(int rate_excess, int rate_limit) {
4336
0
  int qstep =
4337
0
      rate_limit ? ((rate_excess + rate_limit / 2) / rate_limit) : INT_MAX;
4338
0
  return VPXMIN(qstep, MAX_QSTEP_ADJ);
4339
0
}
4340
4341
static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size, uint8_t *dest,
4342
0
                                    size_t dest_size) {
4343
0
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
4344
0
  VP9_COMMON *const cm = &cpi->common;
4345
0
  RATE_CONTROL *const rc = &cpi->rc;
4346
0
  int bottom_index, top_index;
4347
0
  int loop_count = 0;
4348
0
  int loop_at_this_size = 0;
4349
0
  int loop = 0;
4350
0
  int overshoot_seen = 0;
4351
0
  int undershoot_seen = 0;
4352
0
  int frame_over_shoot_limit;
4353
0
  int frame_under_shoot_limit;
4354
0
  int q = 0, q_low = 0, q_high = 0;
4355
0
  int enable_acl;
4356
#ifdef AGGRESSIVE_VBR
4357
  int qrange_adj = 1;
4358
#endif
4359
4360
0
  const int orig_rc_max_frame_bandwidth = rc->max_frame_bandwidth;
4361
4362
0
  if (cm->show_existing_frame) {
4363
0
    rc->this_frame_target = 0;
4364
0
    if (is_psnr_calc_enabled(cpi)) set_raw_source_frame(cpi);
4365
0
    return;
4366
0
  }
4367
4368
0
  set_size_independent_vars(cpi);
4369
4370
0
  enable_acl = cpi->sf.allow_acl ? (cm->frame_type == KEY_FRAME) ||
4371
0
                                       (cpi->twopass.gf_group.index == 1)
4372
0
                                 : 0;
4373
4374
#if CONFIG_COLLECT_COMPONENT_TIMING
4375
  printf("\n Encoding a frame: \n");
4376
#endif
4377
0
  do {
4378
0
    vpx_clear_system_state();
4379
4380
0
    set_frame_size(cpi);
4381
4382
0
    if (loop_count == 0 || cpi->resize_pending != 0) {
4383
0
      set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4384
4385
#ifdef AGGRESSIVE_VBR
4386
      if (two_pass_first_group_inter(cpi)) {
4387
        // Adjustment limits for min and max q
4388
        qrange_adj = VPXMAX(1, (top_index - bottom_index) / 2);
4389
4390
        bottom_index =
4391
            VPXMAX(bottom_index - qrange_adj / 2, oxcf->best_allowed_q);
4392
        top_index = VPXMIN(oxcf->worst_allowed_q, top_index + qrange_adj / 2);
4393
      }
4394
#endif
4395
      // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4396
0
      set_mv_search_params(cpi);
4397
4398
      // Reset the loop state for new frame size.
4399
0
      overshoot_seen = 0;
4400
0
      undershoot_seen = 0;
4401
4402
      // Reconfiguration for change in frame size has concluded.
4403
0
      cpi->resize_pending = 0;
4404
4405
0
      q_low = bottom_index;
4406
0
      q_high = top_index;
4407
4408
0
      loop_at_this_size = 0;
4409
0
    }
4410
4411
    // Decide frame size bounds first time through.
4412
0
    if (loop_count == 0) {
4413
0
      vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4414
0
                                       &frame_under_shoot_limit,
4415
0
                                       &frame_over_shoot_limit);
4416
0
    }
4417
4418
0
    cpi->Source =
4419
0
        vp9_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source,
4420
0
                              (oxcf->pass == 0), EIGHTTAP, 0);
4421
4422
    // Unfiltered raw source used in metrics calculation if the source
4423
    // has been filtered.
4424
0
    if (is_psnr_calc_enabled(cpi)) {
4425
#ifdef ENABLE_KF_DENOISE
4426
      if (is_spatial_denoise_enabled(cpi)) {
4427
        cpi->raw_source_frame = vp9_scale_if_required(
4428
            cm, &cpi->raw_unscaled_source, &cpi->raw_scaled_source,
4429
            (oxcf->pass == 0), EIGHTTAP, 0);
4430
      } else {
4431
        cpi->raw_source_frame = cpi->Source;
4432
      }
4433
#else
4434
0
      cpi->raw_source_frame = cpi->Source;
4435
0
#endif
4436
0
    }
4437
4438
0
    if (cpi->unscaled_last_source != NULL)
4439
0
      cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
4440
0
                                               &cpi->scaled_last_source,
4441
0
                                               (oxcf->pass == 0), EIGHTTAP, 0);
4442
4443
0
    if (frame_is_intra_only(cm) == 0) {
4444
0
      if (loop_count > 0) {
4445
0
        release_scaled_references(cpi);
4446
0
      }
4447
0
      vp9_scale_references(cpi);
4448
0
    }
4449
4450
0
    const GF_GROUP *gf_group = &cpi->twopass.gf_group;
4451
0
    int ext_rc_delta_q_uv = 0;
4452
0
    if (cpi->ext_ratectrl.ready &&
4453
0
        (cpi->ext_ratectrl.funcs.rc_type & VPX_RC_QP) != 0 &&
4454
0
        cpi->ext_ratectrl.funcs.get_encodeframe_decision != NULL) {
4455
0
      vpx_codec_err_t codec_status;
4456
0
      vpx_rc_encodeframe_decision_t encode_frame_decision;
4457
0
      int sb_size = num_8x8_blocks_wide_lookup[BLOCK_64X64] * MI_SIZE;
4458
0
      int frame_height_sb = (cm->height + sb_size - 1) / sb_size;
4459
0
      int frame_width_sb = (cm->width + sb_size - 1) / sb_size;
4460
0
      CHECK_MEM_ERROR(&cm->error, encode_frame_decision.sb_params_list,
4461
0
                      (sb_params *)vpx_calloc(
4462
0
                          frame_height_sb * frame_width_sb,
4463
0
                          sizeof(*encode_frame_decision.sb_params_list)));
4464
0
      codec_status = vp9_extrc_get_encodeframe_decision(
4465
0
          &cpi->ext_ratectrl, gf_group->index, &encode_frame_decision);
4466
0
      if (codec_status != VPX_CODEC_OK) {
4467
0
        vpx_internal_error(&cm->error, codec_status,
4468
0
                           "vp9_extrc_get_encodeframe_decision() failed");
4469
0
      }
4470
0
      for (int idx = 0; idx < frame_height_sb * frame_width_sb; ++idx) {
4471
0
        cpi->sb_mul_scale[idx] =
4472
0
            (((int64_t)encode_frame_decision.sb_params_list[idx].rdmult * 256) /
4473
0
             (encode_frame_decision.rdmult + 1));
4474
0
      }
4475
0
      vpx_free(encode_frame_decision.sb_params_list);
4476
      // If the external model recommends a reserved value, we use
4477
      // libvpx's default q.
4478
0
      if (encode_frame_decision.q_index != VPX_DEFAULT_Q) {
4479
0
        q = encode_frame_decision.q_index;
4480
0
      }
4481
0
      ext_rc_delta_q_uv = encode_frame_decision.delta_q_uv;
4482
0
    }
4483
4484
0
    if (cpi->ext_ratectrl.ready && cpi->ext_ratectrl.log_file) {
4485
0
      fprintf(cpi->ext_ratectrl.log_file,
4486
0
              "ENCODE_FRAME_INFO gop_index %d update_type %d q %d\n",
4487
0
              gf_group->index, gf_group->update_type[gf_group->index], q);
4488
0
    }
4489
4490
0
    vp9_set_quantizer(cpi, q, ext_rc_delta_q_uv);
4491
4492
0
    if (loop_count == 0) setup_frame(cpi);
4493
4494
    // Variance adaptive and in frame q adjustment experiments are mutually
4495
    // exclusive.
4496
0
    if (oxcf->aq_mode == VARIANCE_AQ) {
4497
0
      vp9_vaq_frame_setup(cpi);
4498
0
    } else if (oxcf->aq_mode == EQUATOR360_AQ) {
4499
0
      vp9_360aq_frame_setup(cpi);
4500
0
    } else if (oxcf->aq_mode == COMPLEXITY_AQ) {
4501
0
      vp9_setup_in_frame_q_adj(cpi);
4502
0
    } else if (oxcf->aq_mode == LOOKAHEAD_AQ) {
4503
0
      vp9_alt_ref_aq_setup_map(cpi->alt_ref_aq, cpi);
4504
0
    } else if (oxcf->aq_mode == PSNR_AQ) {
4505
0
      vp9_psnr_aq_mode_setup(&cm->seg);
4506
0
    }
4507
4508
0
    vp9_encode_frame(cpi);
4509
4510
    // Update the skip mb flag probabilities based on the distribution
4511
    // seen in the last encoder iteration.
4512
    // update_base_skip_probs(cpi);
4513
4514
0
    vpx_clear_system_state();
4515
4516
    // Dummy pack of the bitstream using up to date stats to get an
4517
    // accurate estimate of output frame size to determine if we need
4518
    // to recode.
4519
0
    if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
4520
0
      save_coding_context(cpi);
4521
0
      if (!cpi->sf.use_nonrd_pick_mode)
4522
0
        vp9_pack_bitstream(cpi, dest, dest_size, size);
4523
4524
0
      rc->projected_frame_size = (int)(*size) << 3;
4525
4526
0
      if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4527
0
    }
4528
4529
0
    if (cpi->ext_ratectrl.ready &&
4530
0
        (cpi->ext_ratectrl.funcs.rc_type & VPX_RC_QP) != 0) {
4531
0
      break;
4532
0
    }
4533
4534
0
    if (oxcf->rc_mode == VPX_Q) {
4535
0
      loop = 0;
4536
0
    } else {
4537
0
      if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4538
0
          (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4539
0
        int last_q = q;
4540
0
        int64_t kf_err;
4541
4542
0
        int64_t high_err_target = cpi->ambient_err;
4543
0
        int64_t low_err_target = cpi->ambient_err >> 1;
4544
4545
0
#if CONFIG_VP9_HIGHBITDEPTH
4546
0
        if (cm->use_highbitdepth) {
4547
0
          kf_err = vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4548
0
        } else {
4549
0
          kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4550
0
        }
4551
#else
4552
        kf_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4553
#endif  // CONFIG_VP9_HIGHBITDEPTH
4554
4555
        // Prevent possible divide by zero error below for perfect KF
4556
0
        kf_err += !kf_err;
4557
4558
        // The key frame is not good enough or we can afford
4559
        // to make it better without undue risk of popping.
4560
0
        if ((kf_err > high_err_target &&
4561
0
             rc->projected_frame_size <= frame_over_shoot_limit) ||
4562
0
            (kf_err > low_err_target &&
4563
0
             rc->projected_frame_size <= frame_under_shoot_limit)) {
4564
          // Lower q_high
4565
0
          q_high = q > q_low ? q - 1 : q_low;
4566
4567
          // Adjust Q
4568
0
          q = (int)((q * high_err_target) / kf_err);
4569
0
          q = VPXMIN(q, (q_high + q_low) >> 1);
4570
0
        } else if (kf_err < low_err_target &&
4571
0
                   rc->projected_frame_size >= frame_under_shoot_limit) {
4572
          // The key frame is much better than the previous frame
4573
          // Raise q_low
4574
0
          q_low = q < q_high ? q + 1 : q_high;
4575
4576
          // Adjust Q
4577
0
          q = (int)((q * low_err_target) / kf_err);
4578
0
          q = VPXMIN(q, (q_high + q_low + 1) >> 1);
4579
0
        }
4580
4581
        // Clamp Q to upper and lower limits:
4582
0
        q = clamp(q, q_low, q_high);
4583
4584
0
        loop = q != last_q;
4585
0
      } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4586
0
                                  frame_under_shoot_limit, q,
4587
0
                                  VPXMAX(q_high, top_index), bottom_index)) {
4588
        // Is the projected frame size out of range and are we allowed
4589
        // to attempt to recode.
4590
0
        int last_q = q;
4591
0
        int retries = 0;
4592
0
        int qstep;
4593
4594
0
        if (cpi->resize_pending == 1) {
4595
          // Change in frame size so go back around the recode loop.
4596
0
          cpi->rc.frame_size_selector =
4597
0
              SCALE_STEP1 - cpi->rc.frame_size_selector;
4598
0
          cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
4599
4600
#if CONFIG_INTERNAL_STATS
4601
          ++cpi->tot_recode_hits;
4602
#endif
4603
0
          ++loop_count;
4604
0
          loop = 1;
4605
0
          continue;
4606
0
        }
4607
4608
        // Frame size out of permitted range:
4609
        // Update correction factor & compute new Q to try...
4610
4611
        // Frame is too large
4612
0
        if (rc->projected_frame_size > rc->this_frame_target) {
4613
          // Special case if the projected size is > the max allowed.
4614
0
          if ((q == q_high) &&
4615
0
              ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
4616
0
               (!rc->is_src_frame_alt_ref &&
4617
0
                (rc->projected_frame_size >=
4618
0
                 big_rate_miss_high_threshold(cpi))))) {
4619
0
            int max_rate = VPXMAX(1, VPXMIN(rc->max_frame_bandwidth,
4620
0
                                            big_rate_miss_high_threshold(cpi)));
4621
0
            double q_val_high;
4622
0
            q_val_high = vp9_convert_qindex_to_q(q_high, cm->bit_depth);
4623
0
            q_val_high =
4624
0
                q_val_high * ((double)rc->projected_frame_size / max_rate);
4625
0
            q_high = vp9_convert_q_to_qindex(q_val_high, cm->bit_depth);
4626
0
            q_high = clamp(q_high, rc->best_quality, rc->worst_quality);
4627
0
          }
4628
4629
          // Raise Qlow as to at least the current value
4630
0
          qstep =
4631
0
              get_qstep_adj(rc->projected_frame_size, rc->this_frame_target);
4632
0
          q_low = VPXMIN(q + qstep, q_high);
4633
4634
0
          if (undershoot_seen || loop_at_this_size > 1) {
4635
            // Update rate_correction_factor unless
4636
0
            vp9_rc_update_rate_correction_factors(cpi);
4637
4638
0
            q = (q_high + q_low + 1) / 2;
4639
0
          } else {
4640
            // Update rate_correction_factor unless
4641
0
            vp9_rc_update_rate_correction_factors(cpi);
4642
4643
0
            q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4644
0
                                  VPXMAX(q_high, top_index));
4645
4646
0
            while (q < q_low && retries < 10) {
4647
0
              vp9_rc_update_rate_correction_factors(cpi);
4648
0
              q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4649
0
                                    VPXMAX(q_high, top_index));
4650
0
              retries++;
4651
0
            }
4652
0
          }
4653
4654
0
          overshoot_seen = 1;
4655
0
        } else {
4656
          // Frame is too small
4657
0
          qstep =
4658
0
              get_qstep_adj(rc->this_frame_target, rc->projected_frame_size);
4659
0
          q_high = VPXMAX(q - qstep, q_low);
4660
4661
0
          if (overshoot_seen || loop_at_this_size > 1) {
4662
0
            vp9_rc_update_rate_correction_factors(cpi);
4663
0
            q = (q_high + q_low) / 2;
4664
0
          } else {
4665
0
            vp9_rc_update_rate_correction_factors(cpi);
4666
0
            q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
4667
0
                                  VPXMIN(q_low, bottom_index), top_index);
4668
            // Special case reset for qlow for constrained quality.
4669
            // This should only trigger where there is very substantial
4670
            // undershoot on a frame and the auto cq level is above
4671
            // the user passed in value.
4672
0
            if (oxcf->rc_mode == VPX_CQ && q < q_low) {
4673
0
              q_low = q;
4674
0
            }
4675
4676
0
            while (q > q_high && retries < 10) {
4677
0
              vp9_rc_update_rate_correction_factors(cpi);
4678
0
              q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
4679
0
                                    VPXMIN(q_low, bottom_index), top_index);
4680
0
              retries++;
4681
0
            }
4682
0
          }
4683
0
          undershoot_seen = 1;
4684
0
        }
4685
4686
        // Clamp Q to upper and lower limits:
4687
0
        q = clamp(q, q_low, q_high);
4688
4689
0
        loop = (q != last_q);
4690
0
      } else {
4691
0
        loop = 0;
4692
0
      }
4693
0
    }
4694
4695
    // Special case for overlay frame.
4696
0
    if (rc->is_src_frame_alt_ref &&
4697
0
        rc->projected_frame_size < rc->max_frame_bandwidth)
4698
0
      loop = 0;
4699
4700
0
    if (loop) {
4701
0
      ++loop_count;
4702
0
      ++loop_at_this_size;
4703
4704
#if CONFIG_INTERNAL_STATS
4705
      ++cpi->tot_recode_hits;
4706
#endif
4707
0
    }
4708
4709
0
    if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF)
4710
0
      if (loop) restore_coding_context(cpi);
4711
#if CONFIG_COLLECT_COMPONENT_TIMING
4712
    if (loop) printf("\n Recoding:");
4713
#endif
4714
0
  } while (loop);
4715
4716
0
  rc->max_frame_bandwidth = orig_rc_max_frame_bandwidth;
4717
4718
#ifdef AGGRESSIVE_VBR
4719
  if (two_pass_first_group_inter(cpi)) {
4720
    cpi->twopass.active_worst_quality =
4721
        VPXMIN(q + qrange_adj, oxcf->worst_allowed_q);
4722
  } else if (!frame_is_kf_gf_arf(cpi)) {
4723
#else
4724
0
  if (!frame_is_kf_gf_arf(cpi)) {
4725
0
#endif
4726
    // Have we been forced to adapt Q outside the expected range by an extreme
4727
    // rate miss. If so adjust the active maxQ for the subsequent frames.
4728
0
    if (!rc->is_src_frame_alt_ref && (q > cpi->twopass.active_worst_quality)) {
4729
0
      cpi->twopass.active_worst_quality = q;
4730
0
    } else if (oxcf->vbr_corpus_complexity && q == q_low &&
4731
0
               rc->projected_frame_size < rc->this_frame_target) {
4732
0
      cpi->twopass.active_worst_quality =
4733
0
          VPXMAX(q, cpi->twopass.active_worst_quality - 1);
4734
0
    }
4735
0
  }
4736
4737
0
  if (enable_acl) {
4738
    // Skip recoding, if model diff is below threshold
4739
0
    const int thresh = compute_context_model_thresh(cpi);
4740
0
    const int diff = compute_context_model_diff(cm);
4741
0
    if (diff >= thresh) {
4742
0
      vp9_encode_frame(cpi);
4743
0
    }
4744
0
  }
4745
0
  if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
4746
0
    vpx_clear_system_state();
4747
0
    restore_coding_context(cpi);
4748
0
  }
4749
0
}
4750
#endif  // !CONFIG_REALTIME_ONLY
4751
4752
53.1k
static void set_ext_overrides(VP9_COMP *cpi) {
4753
  // Overrides the defaults with the externally supplied values with
4754
  // vp9_update_reference() and vp9_update_entropy() calls
4755
  // Note: The overrides are valid only for the next frame passed
4756
  // to encode_frame_to_data_rate() function
4757
53.1k
  if (cpi->ext_refresh_frame_context_pending) {
4758
0
    cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4759
0
    cpi->ext_refresh_frame_context_pending = 0;
4760
0
  }
4761
53.1k
  if (cpi->ext_refresh_frame_flags_pending) {
4762
0
    cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4763
0
    cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4764
0
    cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
4765
0
  }
4766
53.1k
}
4767
4768
YV12_BUFFER_CONFIG *vp9_scale_if_required(
4769
    VP9_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
4770
82.6k
    int use_normative_scaler, INTERP_FILTER filter_type, int phase_scaler) {
4771
82.6k
  if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
4772
82.6k
      cm->mi_rows * MI_SIZE != unscaled->y_height) {
4773
0
#if CONFIG_VP9_HIGHBITDEPTH
4774
0
    if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
4775
0
        unscaled->y_height <= (scaled->y_height << 1))
4776
0
      if (cm->bit_depth == VPX_BITS_8)
4777
0
        vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
4778
0
      else
4779
0
        scale_and_extend_frame(unscaled, scaled, (int)cm->bit_depth,
4780
0
                               filter_type, phase_scaler);
4781
0
    else
4782
0
      vp9_scale_and_extend_frame_nonnormative(unscaled, scaled,
4783
0
                                              (int)cm->bit_depth);
4784
#else
4785
    if (use_normative_scaler && unscaled->y_width <= (scaled->y_width << 1) &&
4786
        unscaled->y_height <= (scaled->y_height << 1))
4787
      vp9_scale_and_extend_frame(unscaled, scaled, filter_type, phase_scaler);
4788
    else
4789
      vp9_scale_and_extend_frame_nonnormative(unscaled, scaled);
4790
#endif  // CONFIG_VP9_HIGHBITDEPTH
4791
0
    return scaled;
4792
82.6k
  } else {
4793
82.6k
    return unscaled;
4794
82.6k
  }
4795
82.6k
}
4796
4797
53.1k
static void set_ref_sign_bias(VP9_COMP *cpi) {
4798
53.1k
  VP9_COMMON *const cm = &cpi->common;
4799
53.1k
  RefCntBuffer *const ref_buffer = get_ref_cnt_buffer(cm, cm->new_fb_idx);
4800
53.1k
  const int cur_frame_index = ref_buffer->frame_index;
4801
53.1k
  MV_REFERENCE_FRAME ref_frame;
4802
4803
212k
  for (ref_frame = LAST_FRAME; ref_frame < MAX_REF_FRAMES; ++ref_frame) {
4804
159k
    const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
4805
159k
    const RefCntBuffer *const ref_cnt_buf =
4806
159k
        get_ref_cnt_buffer(&cpi->common, buf_idx);
4807
159k
    if (ref_cnt_buf) {
4808
147k
      cm->ref_frame_sign_bias[ref_frame] =
4809
147k
          cur_frame_index < ref_cnt_buf->frame_index;
4810
147k
    }
4811
159k
  }
4812
53.1k
}
4813
4814
0
static int setup_interp_filter_search_mask(VP9_COMP *cpi) {
4815
0
  INTERP_FILTER ifilter;
4816
0
  int ref_total[MAX_REF_FRAMES] = { 0 };
4817
0
  MV_REFERENCE_FRAME ref;
4818
0
  int mask = 0;
4819
0
  if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
4820
0
    return mask;
4821
0
  for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
4822
0
    for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
4823
0
      ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
4824
4825
0
  for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
4826
0
    if ((ref_total[LAST_FRAME] &&
4827
0
         cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
4828
0
        (ref_total[GOLDEN_FRAME] == 0 ||
4829
0
         cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
4830
0
             ref_total[GOLDEN_FRAME]) &&
4831
0
        (ref_total[ALTREF_FRAME] == 0 ||
4832
0
         cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50 <
4833
0
             ref_total[ALTREF_FRAME]))
4834
0
      mask |= 1 << ifilter;
4835
0
  }
4836
0
  return mask;
4837
0
}
4838
4839
#ifdef ENABLE_KF_DENOISE
4840
// Baseline kernel weights for denoise
4841
static uint8_t dn_kernel_3[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
4842
static uint8_t dn_kernel_5[25] = { 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 4,
4843
                                   2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1 };
4844
4845
static INLINE void add_denoise_point(int centre_val, int data_val, int thresh,
4846
                                     uint8_t point_weight, int *sum_val,
4847
                                     int *sum_weight) {
4848
  if (abs(centre_val - data_val) <= thresh) {
4849
    *sum_weight += point_weight;
4850
    *sum_val += (int)data_val * (int)point_weight;
4851
  }
4852
}
4853
4854
static void spatial_denoise_point(uint8_t *src_ptr, const int stride,
4855
                                  const int strength) {
4856
  int sum_weight = 0;
4857
  int sum_val = 0;
4858
  int thresh = strength;
4859
  int kernel_size = 5;
4860
  int half_k_size = 2;
4861
  int i, j;
4862
  int max_diff = 0;
4863
  uint8_t *tmp_ptr;
4864
  uint8_t *kernel_ptr;
4865
4866
  // Find the maximum deviation from the source point in the locale.
4867
  tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
4868
  for (i = 0; i < kernel_size + 2; ++i) {
4869
    for (j = 0; j < kernel_size + 2; ++j) {
4870
      max_diff = VPXMAX(max_diff, abs((int)*src_ptr - (int)tmp_ptr[j]));
4871
    }
4872
    tmp_ptr += stride;
4873
  }
4874
4875
  // Select the kernel size.
4876
  if (max_diff > (strength + (strength >> 1))) {
4877
    kernel_size = 3;
4878
    half_k_size = 1;
4879
    thresh = thresh >> 1;
4880
  }
4881
  kernel_ptr = (kernel_size == 3) ? dn_kernel_3 : dn_kernel_5;
4882
4883
  // Apply the kernel
4884
  tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
4885
  for (i = 0; i < kernel_size; ++i) {
4886
    for (j = 0; j < kernel_size; ++j) {
4887
      add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernel_ptr,
4888
                        &sum_val, &sum_weight);
4889
      ++kernel_ptr;
4890
    }
4891
    tmp_ptr += stride;
4892
  }
4893
4894
  // Update the source value with the new filtered value
4895
  *src_ptr = (uint8_t)((sum_val + (sum_weight >> 1)) / sum_weight);
4896
}
4897
4898
#if CONFIG_VP9_HIGHBITDEPTH
4899
static void highbd_spatial_denoise_point(uint16_t *src_ptr, const int stride,
4900
                                         const int strength) {
4901
  int sum_weight = 0;
4902
  int sum_val = 0;
4903
  int thresh = strength;
4904
  int kernel_size = 5;
4905
  int half_k_size = 2;
4906
  int i, j;
4907
  int max_diff = 0;
4908
  uint16_t *tmp_ptr;
4909
  uint8_t *kernel_ptr;
4910
4911
  // Find the maximum deviation from the source point in the locale.
4912
  tmp_ptr = src_ptr - (stride * (half_k_size + 1)) - (half_k_size + 1);
4913
  for (i = 0; i < kernel_size + 2; ++i) {
4914
    for (j = 0; j < kernel_size + 2; ++j) {
4915
      max_diff = VPXMAX(max_diff, abs((int)src_ptr - (int)tmp_ptr[j]));
4916
    }
4917
    tmp_ptr += stride;
4918
  }
4919
4920
  // Select the kernel size.
4921
  if (max_diff > (strength + (strength >> 1))) {
4922
    kernel_size = 3;
4923
    half_k_size = 1;
4924
    thresh = thresh >> 1;
4925
  }
4926
  kernel_ptr = (kernel_size == 3) ? dn_kernel_3 : dn_kernel_5;
4927
4928
  // Apply the kernel
4929
  tmp_ptr = src_ptr - (stride * half_k_size) - half_k_size;
4930
  for (i = 0; i < kernel_size; ++i) {
4931
    for (j = 0; j < kernel_size; ++j) {
4932
      add_denoise_point((int)*src_ptr, (int)tmp_ptr[j], thresh, *kernel_ptr,
4933
                        &sum_val, &sum_weight);
4934
      ++kernel_ptr;
4935
    }
4936
    tmp_ptr += stride;
4937
  }
4938
4939
  // Update the source value with the new filtered value
4940
  *src_ptr = (uint16_t)((sum_val + (sum_weight >> 1)) / sum_weight);
4941
}
4942
#endif  // CONFIG_VP9_HIGHBITDEPTH
4943
4944
// Apply thresholded spatial noise suppression to a given buffer.
4945
static void spatial_denoise_buffer(VP9_COMP *cpi, uint8_t *buffer,
4946
                                   const int stride, const int width,
4947
                                   const int height, const int strength) {
4948
  VP9_COMMON *const cm = &cpi->common;
4949
  uint8_t *src_ptr = buffer;
4950
  int row;
4951
  int col;
4952
4953
  for (row = 0; row < height; ++row) {
4954
    for (col = 0; col < width; ++col) {
4955
#if CONFIG_VP9_HIGHBITDEPTH
4956
      if (cm->use_highbitdepth)
4957
        highbd_spatial_denoise_point(CONVERT_TO_SHORTPTR(&src_ptr[col]), stride,
4958
                                     strength);
4959
      else
4960
        spatial_denoise_point(&src_ptr[col], stride, strength);
4961
#else
4962
      spatial_denoise_point(&src_ptr[col], stride, strength);
4963
#endif  // CONFIG_VP9_HIGHBITDEPTH
4964
    }
4965
    src_ptr += stride;
4966
  }
4967
}
4968
4969
// Apply thresholded spatial noise suppression to source.
4970
static void spatial_denoise_frame(VP9_COMP *cpi) {
4971
  YV12_BUFFER_CONFIG *src = cpi->Source;
4972
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
4973
  TWO_PASS *const twopass = &cpi->twopass;
4974
  VP9_COMMON *const cm = &cpi->common;
4975
4976
  // Base the filter strength on the current active max Q.
4977
  const int q = (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
4978
                                              cm->bit_depth));
4979
  int strength = clamp(q >> 4, oxcf->arnr_strength >> 2, oxcf->arnr_strength);
4980
4981
  // Denoise each of Y,U and V buffers.
4982
  spatial_denoise_buffer(cpi, src->y_buffer, src->y_stride, src->y_width,
4983
                         src->y_height, strength);
4984
4985
  strength += (strength >> 1);
4986
  spatial_denoise_buffer(cpi, src->u_buffer, src->uv_stride, src->uv_width,
4987
                         src->uv_height, strength << 1);
4988
4989
  spatial_denoise_buffer(cpi, src->v_buffer, src->uv_stride, src->uv_width,
4990
                         src->uv_height, strength << 1);
4991
}
4992
#endif  // ENABLE_KF_DENOISE
4993
4994
#if !CONFIG_REALTIME_ONLY
4995
static void vp9_try_disable_lookahead_aq(VP9_COMP *cpi, size_t *size,
4996
0
                                         uint8_t *dest, size_t dest_size) {
4997
0
  if (cpi->common.seg.enabled)
4998
0
    if (ALT_REF_AQ_PROTECT_GAIN) {
4999
0
      size_t nsize = *size;
5000
0
      int overhead;
5001
5002
      // TODO(yuryg): optimize this, as
5003
      // we don't really need to repack
5004
5005
0
      save_coding_context(cpi);
5006
0
      vp9_disable_segmentation(&cpi->common.seg);
5007
0
      vp9_pack_bitstream(cpi, dest, dest_size, &nsize);
5008
0
      restore_coding_context(cpi);
5009
5010
0
      overhead = (int)*size - (int)nsize;
5011
5012
0
      if (vp9_alt_ref_aq_disable_if(cpi->alt_ref_aq, overhead, (int)*size))
5013
0
        vp9_encode_frame(cpi);
5014
0
      else
5015
0
        vp9_enable_segmentation(&cpi->common.seg);
5016
0
    }
5017
0
}
5018
#endif
5019
5020
53.1k
static void set_frame_index(VP9_COMP *cpi, VP9_COMMON *cm) {
5021
53.1k
  RefCntBuffer *const ref_buffer = get_ref_cnt_buffer(cm, cm->new_fb_idx);
5022
5023
53.1k
  if (ref_buffer) {
5024
53.1k
    const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5025
53.1k
    ref_buffer->frame_index =
5026
53.1k
        cm->current_video_frame + gf_group->arf_src_offset[gf_group->index];
5027
53.1k
    ref_buffer->frame_coding_index = cm->current_frame_coding_index;
5028
53.1k
  }
5029
53.1k
}
5030
5031
0
static void set_mb_ssim_rdmult_scaling(VP9_COMP *cpi) {
5032
0
  VP9_COMMON *cm = &cpi->common;
5033
0
  ThreadData *td = &cpi->td;
5034
0
  MACROBLOCK *x = &td->mb;
5035
0
  MACROBLOCKD *xd = &x->e_mbd;
5036
0
  uint8_t *y_buffer = cpi->Source->y_buffer;
5037
0
  const int y_stride = cpi->Source->y_stride;
5038
0
  const int block_size = BLOCK_16X16;
5039
5040
0
  const int num_8x8_w = num_8x8_blocks_wide_lookup[block_size];
5041
0
  const int num_8x8_h = num_8x8_blocks_high_lookup[block_size];
5042
0
  const int num_cols = (cm->mi_cols + num_8x8_w - 1) / num_8x8_w;
5043
0
  const int num_rows = (cm->mi_rows + num_8x8_h - 1) / num_8x8_h;
5044
0
  double log_sum = 0.0;
5045
0
  int row, col;
5046
5047
  // Loop through each 64x64 block.
5048
0
  for (row = 0; row < num_rows; ++row) {
5049
0
    for (col = 0; col < num_cols; ++col) {
5050
0
      int mi_row, mi_col;
5051
0
      double var = 0.0, num_of_var = 0.0;
5052
0
      const int index = row * num_cols + col;
5053
5054
0
      for (mi_row = row * num_8x8_h;
5055
0
           mi_row < cm->mi_rows && mi_row < (row + 1) * num_8x8_h; ++mi_row) {
5056
0
        for (mi_col = col * num_8x8_w;
5057
0
             mi_col < cm->mi_cols && mi_col < (col + 1) * num_8x8_w; ++mi_col) {
5058
0
          struct buf_2d buf;
5059
0
          const int row_offset_y = mi_row << 3;
5060
0
          const int col_offset_y = mi_col << 3;
5061
5062
0
          buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
5063
0
          buf.stride = y_stride;
5064
5065
          // In order to make SSIM_VAR_SCALE in a same scale for both 8 bit
5066
          // and high bit videos, the variance needs to be divided by 2.0 or
5067
          // 64.0 separately.
5068
          // TODO(sdeng): need to tune for 12bit videos.
5069
0
#if CONFIG_VP9_HIGHBITDEPTH
5070
0
          if (cpi->Source->flags & YV12_FLAG_HIGHBITDEPTH)
5071
0
            var += vp9_high_get_sby_variance(cpi, &buf, BLOCK_8X8, xd->bd);
5072
0
          else
5073
0
#endif
5074
0
            var += vp9_get_sby_variance(cpi, &buf, BLOCK_8X8);
5075
5076
0
          num_of_var += 1.0;
5077
0
        }
5078
0
      }
5079
0
      var = var / num_of_var / 64.0;
5080
5081
      // Curve fitting with an exponential model on all 16x16 blocks from the
5082
      // Midres dataset.
5083
0
      var = 67.035434 * (1 - exp(-0.0021489 * var)) + 17.492222;
5084
0
      cpi->mi_ssim_rdmult_scaling_factors[index] = var;
5085
0
      log_sum += log(var);
5086
0
    }
5087
0
  }
5088
0
  log_sum = exp(log_sum / (double)(num_rows * num_cols));
5089
5090
0
  for (row = 0; row < num_rows; ++row) {
5091
0
    for (col = 0; col < num_cols; ++col) {
5092
0
      const int index = row * num_cols + col;
5093
0
      cpi->mi_ssim_rdmult_scaling_factors[index] /= log_sum;
5094
0
    }
5095
0
  }
5096
5097
0
  (void)xd;
5098
0
}
5099
5100
// Process the wiener variance in 16x16 block basis.
5101
0
static int qsort_comp(const void *elem1, const void *elem2) {
5102
0
  int a = *((const int *)elem1);
5103
0
  int b = *((const int *)elem2);
5104
0
  if (a > b) return 1;
5105
0
  if (a < b) return -1;
5106
0
  return 0;
5107
0
}
5108
5109
0
static void init_mb_wiener_var_buffer(VP9_COMP *cpi) {
5110
0
  VP9_COMMON *cm = &cpi->common;
5111
5112
0
  if (cpi->mb_wiener_variance && cpi->mb_wiener_var_rows >= cm->mb_rows &&
5113
0
      cpi->mb_wiener_var_cols >= cm->mb_cols)
5114
0
    return;
5115
5116
0
  vpx_free(cpi->mb_wiener_variance);
5117
0
  cpi->mb_wiener_variance = NULL;
5118
5119
0
  CHECK_MEM_ERROR(
5120
0
      &cm->error, cpi->mb_wiener_variance,
5121
0
      vpx_calloc(cm->mb_rows * cm->mb_cols, sizeof(*cpi->mb_wiener_variance)));
5122
0
  cpi->mb_wiener_var_rows = cm->mb_rows;
5123
0
  cpi->mb_wiener_var_cols = cm->mb_cols;
5124
0
}
5125
5126
53.1k
static void init_sb_mul_scale_buffer(VP9_COMP *cpi) {
5127
53.1k
  VP9_COMMON *cm = &cpi->common;
5128
5129
53.1k
  if (cpi->mb_wiener_var_rows >= cm->mb_rows &&
5130
53.1k
      cpi->mb_wiener_var_cols >= cm->mb_cols)
5131
49.2k
    return;
5132
5133
3.87k
  vpx_free(cpi->sb_mul_scale);
5134
3.87k
  cpi->sb_mul_scale = NULL;
5135
5136
3.87k
  CHECK_MEM_ERROR(
5137
3.87k
      &cm->error, cpi->sb_mul_scale,
5138
3.87k
      vpx_calloc(cm->mb_rows * cm->mb_cols, sizeof(*cpi->sb_mul_scale)));
5139
3.87k
  cpi->mb_wiener_var_rows = cm->mb_rows;
5140
3.87k
  cpi->mb_wiener_var_cols = cm->mb_cols;
5141
3.87k
}
5142
5143
0
static void set_mb_wiener_variance(VP9_COMP *cpi) {
5144
0
  VP9_COMMON *cm = &cpi->common;
5145
0
  uint8_t *buffer = cpi->Source->y_buffer;
5146
0
  int buf_stride = cpi->Source->y_stride;
5147
5148
0
#if CONFIG_VP9_HIGHBITDEPTH
5149
0
  ThreadData *td = &cpi->td;
5150
0
  MACROBLOCK *x = &td->mb;
5151
0
  MACROBLOCKD *xd = &x->e_mbd;
5152
0
  DECLARE_ALIGNED(16, uint16_t, zero_pred16[32 * 32]);
5153
0
  DECLARE_ALIGNED(16, uint8_t, zero_pred8[32 * 32]);
5154
0
  uint8_t *zero_pred;
5155
#else
5156
  DECLARE_ALIGNED(16, uint8_t, zero_pred[32 * 32]);
5157
#endif
5158
5159
0
  DECLARE_ALIGNED(16, int16_t, src_diff[32 * 32]);
5160
0
  DECLARE_ALIGNED(16, tran_low_t, coeff[32 * 32]);
5161
5162
0
  int mb_row, mb_col, count = 0;
5163
  // Hard coded operating block size
5164
0
  const int block_size = 16;
5165
0
  const int coeff_count = block_size * block_size;
5166
0
  const TX_SIZE tx_size = TX_16X16;
5167
5168
0
#if CONFIG_VP9_HIGHBITDEPTH
5169
0
  xd->cur_buf = cpi->Source;
5170
0
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
5171
0
    zero_pred = CONVERT_TO_BYTEPTR(zero_pred16);
5172
0
    memset(zero_pred16, 0, sizeof(*zero_pred16) * coeff_count);
5173
0
  } else {
5174
0
    zero_pred = zero_pred8;
5175
0
    memset(zero_pred8, 0, sizeof(*zero_pred8) * coeff_count);
5176
0
  }
5177
#else
5178
  memset(zero_pred, 0, sizeof(*zero_pred) * coeff_count);
5179
#endif
5180
5181
0
  cpi->norm_wiener_variance = 0;
5182
5183
0
  for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
5184
0
    for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
5185
0
      int idx;
5186
0
      int16_t median_val = 0;
5187
0
      uint8_t *mb_buffer =
5188
0
          buffer + mb_row * block_size * buf_stride + mb_col * block_size;
5189
0
      int64_t wiener_variance = 0;
5190
5191
0
#if CONFIG_VP9_HIGHBITDEPTH
5192
0
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
5193
0
        vpx_highbd_subtract_block(block_size, block_size, src_diff, block_size,
5194
0
                                  mb_buffer, buf_stride, zero_pred, block_size,
5195
0
                                  xd->bd);
5196
0
        vp9_highbd_wht_fwd_txfm(src_diff, block_size, coeff, tx_size);
5197
0
      } else {
5198
0
        vpx_subtract_block(block_size, block_size, src_diff, block_size,
5199
0
                           mb_buffer, buf_stride, zero_pred, block_size);
5200
0
        vp9_wht_fwd_txfm(src_diff, block_size, coeff, tx_size);
5201
0
      }
5202
#else
5203
      vpx_subtract_block(block_size, block_size, src_diff, block_size,
5204
                         mb_buffer, buf_stride, zero_pred, block_size);
5205
      vp9_wht_fwd_txfm(src_diff, block_size, coeff, tx_size);
5206
#endif  // CONFIG_VP9_HIGHBITDEPTH
5207
5208
0
      coeff[0] = 0;
5209
0
      for (idx = 1; idx < coeff_count; ++idx) coeff[idx] = abs(coeff[idx]);
5210
5211
0
      qsort(coeff, coeff_count - 1, sizeof(*coeff), qsort_comp);
5212
5213
      // Noise level estimation
5214
0
      median_val = coeff[coeff_count / 2];
5215
5216
      // Wiener filter
5217
0
      for (idx = 1; idx < coeff_count; ++idx) {
5218
0
        int64_t sqr_coeff = (int64_t)coeff[idx] * coeff[idx];
5219
0
        int64_t tmp_coeff = (int64_t)coeff[idx];
5220
0
        if (median_val) {
5221
0
          tmp_coeff = (sqr_coeff * coeff[idx]) /
5222
0
                      (sqr_coeff + (int64_t)median_val * median_val);
5223
0
        }
5224
0
        wiener_variance += tmp_coeff * tmp_coeff;
5225
0
      }
5226
0
      cpi->mb_wiener_variance[mb_row * cm->mb_cols + mb_col] =
5227
0
          wiener_variance / coeff_count;
5228
0
      cpi->norm_wiener_variance +=
5229
0
          cpi->mb_wiener_variance[mb_row * cm->mb_cols + mb_col];
5230
0
      ++count;
5231
0
    }
5232
0
  }
5233
5234
0
  if (count) cpi->norm_wiener_variance /= count;
5235
0
  cpi->norm_wiener_variance = VPXMAX(1, cpi->norm_wiener_variance);
5236
0
}
5237
5238
#if !CONFIG_REALTIME_ONLY
5239
static PSNR_STATS compute_psnr_stats(const YV12_BUFFER_CONFIG *source_frame,
5240
                                     const YV12_BUFFER_CONFIG *coded_frame,
5241
                                     uint32_t bit_depth,
5242
0
                                     uint32_t input_bit_depth) {
5243
0
  PSNR_STATS psnr;
5244
0
#if CONFIG_VP9_HIGHBITDEPTH
5245
0
  vpx_calc_highbd_psnr(source_frame, coded_frame, &psnr, bit_depth,
5246
0
                       input_bit_depth);
5247
#else   // CONFIG_VP9_HIGHBITDEPTH
5248
  (void)bit_depth;
5249
  (void)input_bit_depth;
5250
  vpx_calc_psnr(source_frame, coded_frame, &psnr);
5251
#endif  // CONFIG_VP9_HIGHBITDEPTH
5252
0
  return psnr;
5253
0
}
5254
5255
static void update_encode_frame_result_basic(
5256
    FRAME_UPDATE_TYPE update_type, int show_idx, int quantize_index,
5257
0
    ENCODE_FRAME_RESULT *encode_frame_result) {
5258
0
  encode_frame_result->show_idx = show_idx;
5259
0
  encode_frame_result->update_type = update_type;
5260
0
  encode_frame_result->quantize_index = quantize_index;
5261
0
}
5262
#endif  // !CONFIG_REALTIME_ONLY
5263
5264
static void encode_frame_to_data_rate(
5265
    VP9_COMP *cpi, size_t *size, uint8_t *dest, size_t dest_size,
5266
53.1k
    unsigned int *frame_flags, ENCODE_FRAME_RESULT *encode_frame_result) {
5267
53.1k
  VP9_COMMON *const cm = &cpi->common;
5268
53.1k
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
5269
53.1k
  struct segmentation *const seg = &cm->seg;
5270
53.1k
  TX_SIZE t;
5271
5272
53.1k
  if (vp9_svc_check_skip_enhancement_layer(cpi)) return;
5273
5274
53.1k
  set_ext_overrides(cpi);
5275
53.1k
  vpx_clear_system_state();
5276
5277
#ifdef ENABLE_KF_DENOISE
5278
  // Spatial denoise of key frame.
5279
  if (is_spatial_denoise_enabled(cpi)) spatial_denoise_frame(cpi);
5280
#endif
5281
5282
53.1k
  if (cm->show_existing_frame == 0) {
5283
    // Update frame index
5284
53.1k
    set_frame_index(cpi, cm);
5285
5286
    // Set the arf sign bias for this frame.
5287
53.1k
    set_ref_sign_bias(cpi);
5288
53.1k
  }
5289
5290
  // On the very first frame set the deadline_mode_previous_frame to
5291
  // the current mode.
5292
53.1k
  if (cpi->common.current_video_frame == 0)
5293
3.87k
    cpi->deadline_mode_previous_frame = cpi->oxcf.mode;
5294
5295
  // Set default state for segment based loop filter update flags.
5296
53.1k
  cm->lf.mode_ref_delta_update = 0;
5297
5298
53.1k
  if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
5299
0
    cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
5300
5301
  // Set various flags etc to special state if it is a key frame.
5302
53.1k
  if (frame_is_intra_only(cm)) {
5303
    // Reset the loop filter deltas and segmentation map.
5304
12.4k
    vp9_reset_segment_features(&cm->seg);
5305
5306
    // If segmentation is enabled force a map update for key frames.
5307
12.4k
    if (seg->enabled) {
5308
0
      seg->update_map = 1;
5309
0
      seg->update_data = 1;
5310
0
    }
5311
5312
    // The alternate reference frame cannot be active for a key frame.
5313
12.4k
    cpi->rc.source_alt_ref_active = 0;
5314
5315
12.4k
    cm->error_resilient_mode = oxcf->error_resilient_mode;
5316
12.4k
    cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
5317
5318
    // By default, encoder assumes decoder can use prev_mi.
5319
12.4k
    if (cm->error_resilient_mode) {
5320
0
      cm->frame_parallel_decoding_mode = 1;
5321
0
      cm->reset_frame_context = 0;
5322
0
      cm->refresh_frame_context = 0;
5323
12.4k
    } else if (cm->intra_only) {
5324
      // Only reset the current context.
5325
0
      cm->reset_frame_context = 2;
5326
0
    }
5327
12.4k
  }
5328
5329
53.1k
  if (oxcf->tuning == VP8_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
5330
5331
53.1k
  if (oxcf->aq_mode == PERCEPTUAL_AQ) {
5332
0
    init_mb_wiener_var_buffer(cpi);
5333
0
    set_mb_wiener_variance(cpi);
5334
0
  }
5335
5336
53.1k
  init_sb_mul_scale_buffer(cpi);
5337
5338
53.1k
  vpx_clear_system_state();
5339
5340
#if CONFIG_INTERNAL_STATS
5341
  memset(cpi->mode_chosen_counts, 0,
5342
         MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5343
#endif
5344
  // Backup to ensure consistency between recodes
5345
53.1k
  save_encode_params(cpi);
5346
53.1k
  if (cpi->ext_ratectrl.ready &&
5347
53.1k
      (cpi->ext_ratectrl.funcs.rc_type & VPX_RC_RDMULT) != 0 &&
5348
53.1k
      cpi->ext_ratectrl.funcs.get_frame_rdmult != NULL) {
5349
0
    vpx_codec_err_t codec_status;
5350
0
    const GF_GROUP *gf_group = &cpi->twopass.gf_group;
5351
0
    FRAME_UPDATE_TYPE update_type = gf_group->update_type[gf_group->index];
5352
0
    const int ref_frame_flags = get_ref_frame_flags(cpi);
5353
0
    RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES];
5354
0
    const RefCntBuffer *curr_frame_buf = get_ref_cnt_buffer(cm, cm->new_fb_idx);
5355
    // index 0 of a gf group is always KEY/OVERLAY/GOLDEN.
5356
    // index 1 refers to the first encoding frame in a gf group.
5357
    // Therefore if it is ARF_UPDATE, it means this gf group uses alt ref.
5358
    // See function define_gf_group_structure().
5359
0
    const int use_alt_ref = gf_group->update_type[1] == ARF_UPDATE;
5360
0
    int ext_rdmult = VPX_DEFAULT_RDMULT;
5361
0
    get_ref_frame_bufs(cpi, ref_frame_bufs);
5362
0
    codec_status = vp9_extrc_get_frame_rdmult(
5363
0
        &cpi->ext_ratectrl, curr_frame_buf->frame_index,
5364
0
        cm->current_frame_coding_index, gf_group->index, update_type,
5365
0
        gf_group->gf_group_size, use_alt_ref, ref_frame_bufs, ref_frame_flags,
5366
0
        &ext_rdmult);
5367
0
    if (codec_status != VPX_CODEC_OK) {
5368
0
      vpx_internal_error(&cm->error, codec_status,
5369
0
                         "vp9_extrc_get_frame_rdmult() failed");
5370
0
    }
5371
0
    cpi->ext_ratectrl.ext_rdmult = ext_rdmult;
5372
0
  }
5373
5374
53.1k
  if (cpi->sf.recode_loop == DISALLOW_RECODE) {
5375
53.1k
    if (!encode_without_recode_loop(cpi, size, dest, dest_size)) return;
5376
53.1k
  } else {
5377
0
#if !CONFIG_REALTIME_ONLY
5378
#if CONFIG_COLLECT_COMPONENT_TIMING
5379
    start_timing(cpi, encode_with_recode_loop_time);
5380
#endif
5381
0
    encode_with_recode_loop(cpi, size, dest, dest_size);
5382
#if CONFIG_COLLECT_COMPONENT_TIMING
5383
    end_timing(cpi, encode_with_recode_loop_time);
5384
#endif
5385
0
#endif  // !CONFIG_REALTIME_ONLY
5386
0
  }
5387
5388
  // TODO(jingning): When using show existing frame mode, we assume that the
5389
  // current ARF will be directly used as the final reconstructed frame. This is
5390
  // an encoder control scheme. One could in principle explore other
5391
  // possibilities to arrange the reference frame buffer and their coding order.
5392
53.1k
  if (cm->show_existing_frame) {
5393
0
    ref_cnt_fb(cm->buffer_pool->frame_bufs, &cm->new_fb_idx,
5394
0
               cm->ref_frame_map[cpi->alt_fb_idx]);
5395
0
  }
5396
5397
53.1k
#if !CONFIG_REALTIME_ONLY
5398
  // Disable segmentation if it decrease rate/distortion ratio
5399
53.1k
  if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
5400
0
    vp9_try_disable_lookahead_aq(cpi, size, dest, dest_size);
5401
53.1k
#endif
5402
5403
#if CONFIG_VP9_TEMPORAL_DENOISING
5404
#ifdef OUTPUT_YUV_DENOISED
5405
  if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
5406
    vpx_write_yuv_frame(yuv_denoised_file,
5407
                        &cpi->denoiser.running_avg_y[INTRA_FRAME]);
5408
  }
5409
#endif
5410
#endif
5411
#ifdef OUTPUT_YUV_SKINMAP
5412
  if (cpi->common.current_video_frame > 1) {
5413
    vp9_output_skin_map(cpi, yuv_skinmap_file);
5414
  }
5415
#endif
5416
5417
  // Special case code to reduce pulsing when key frames are forced at a
5418
  // fixed interval. Note the reconstruction error if it is the frame before
5419
  // the force key frame
5420
53.1k
  if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
5421
0
#if CONFIG_VP9_HIGHBITDEPTH
5422
0
    if (cm->use_highbitdepth) {
5423
0
      cpi->ambient_err =
5424
0
          vpx_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
5425
0
    } else {
5426
0
      cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
5427
0
    }
5428
#else
5429
    cpi->ambient_err = vpx_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
5430
#endif  // CONFIG_VP9_HIGHBITDEPTH
5431
0
  }
5432
5433
  // If the encoder forced a KEY_FRAME decision
5434
53.1k
  if (cm->frame_type == KEY_FRAME) cpi->refresh_last_frame = 1;
5435
5436
53.1k
  cm->frame_to_show = get_frame_new_buffer(cm);
5437
53.1k
  cm->frame_to_show->color_space = cm->color_space;
5438
53.1k
  cm->frame_to_show->color_range = cm->color_range;
5439
53.1k
  cm->frame_to_show->render_width = cm->render_width;
5440
53.1k
  cm->frame_to_show->render_height = cm->render_height;
5441
5442
#if CONFIG_COLLECT_COMPONENT_TIMING
5443
  start_timing(cpi, loopfilter_frame_time);
5444
#endif
5445
  // Pick the loop filter level for the frame.
5446
53.1k
  loopfilter_frame(cpi, cm);
5447
#if CONFIG_COLLECT_COMPONENT_TIMING
5448
  end_timing(cpi, loopfilter_frame_time);
5449
#endif
5450
5451
53.1k
  if (cpi->rc.use_post_encode_drop) save_coding_context(cpi);
5452
5453
#if CONFIG_COLLECT_COMPONENT_TIMING
5454
  start_timing(cpi, vp9_pack_bitstream_time);
5455
#endif
5456
  // build the bitstream
5457
53.1k
  vp9_pack_bitstream(cpi, dest, dest_size, size);
5458
#if CONFIG_COLLECT_COMPONENT_TIMING
5459
  end_timing(cpi, vp9_pack_bitstream_time);
5460
#endif
5461
5462
53.1k
  if (cpi->ext_ratectrl.ready &&
5463
53.1k
      cpi->ext_ratectrl.funcs.update_encodeframe_result != NULL) {
5464
0
    vpx_codec_err_t codec_status = vp9_extrc_update_encodeframe_result(
5465
0
        &cpi->ext_ratectrl, (*size) << 3, cm->base_qindex);
5466
0
    if (codec_status != VPX_CODEC_OK) {
5467
0
      vpx_internal_error(&cm->error, codec_status,
5468
0
                         "vp9_extrc_update_encodeframe_result() failed");
5469
0
    }
5470
0
  }
5471
#if CONFIG_REALTIME_ONLY
5472
  (void)encode_frame_result;
5473
  assert(encode_frame_result == NULL);
5474
#else   // CONFIG_REALTIME_ONLY
5475
53.1k
  if (encode_frame_result != NULL) {
5476
0
    const RefCntBuffer *coded_frame_buf =
5477
0
        get_ref_cnt_buffer(cm, cm->new_fb_idx);
5478
0
    RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES];
5479
0
    FRAME_UPDATE_TYPE update_type =
5480
0
        cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index];
5481
0
    int quantize_index = vp9_get_quantizer(cpi);
5482
0
    get_ref_frame_bufs(cpi, ref_frame_bufs);
5483
    // update_encode_frame_result() depends on twopass.gf_group.index and
5484
    // cm->new_fb_idx, cpi->Source, cpi->lst_fb_idx, cpi->gld_fb_idx and
5485
    // cpi->alt_fb_idx are updated for current frame and have
5486
    // not been updated for the next frame yet.
5487
    // The update locations are as follows.
5488
    // 1) twopass.gf_group.index is initialized at define_gf_group by vp9_zero()
5489
    // for the first frame in the gf_group and is updated for the next frame at
5490
    // vp9_twopass_postencode_update().
5491
    // 2) cpi->Source is updated at the beginning of vp9_get_compressed_data()
5492
    // 3) cm->new_fb_idx is updated at the beginning of
5493
    // vp9_get_compressed_data() by get_free_fb(cm).
5494
    // 4) cpi->lst_fb_idx/gld_fb_idx/alt_fb_idx will be updated for the next
5495
    // frame at vp9_update_reference_frames().
5496
    // This function needs to be called before vp9_update_reference_frames().
5497
    // TODO(angiebird): Improve the codebase to make the update of frame
5498
    // dependent variables more robust.
5499
5500
0
    update_encode_frame_result_basic(update_type, coded_frame_buf->frame_index,
5501
0
                                     quantize_index, encode_frame_result);
5502
0
    if (cpi->ext_ratectrl.ready && cpi->ext_ratectrl.log_file) {
5503
0
      PSNR_STATS psnr =
5504
0
          compute_psnr_stats(cpi->Source, &coded_frame_buf->buf, cm->bit_depth,
5505
0
                             cpi->oxcf.input_bit_depth);
5506
0
      fprintf(cpi->ext_ratectrl.log_file,
5507
0
              "ENCODE_FRAME_RESULT gop_index %d psnr %f bits %zu\n",
5508
0
              cpi->twopass.gf_group.index, psnr.psnr[0], (*size) << 3);
5509
0
    }
5510
0
  }
5511
53.1k
#endif  // CONFIG_REALTIME_ONLY
5512
5513
53.1k
  if (cpi->rc.use_post_encode_drop && cm->base_qindex < cpi->rc.worst_quality &&
5514
53.1k
      cpi->svc.spatial_layer_id == 0 && post_encode_drop_cbr(cpi, size)) {
5515
0
    restore_coding_context(cpi);
5516
0
    return;
5517
0
  }
5518
5519
53.1k
  cpi->last_frame_dropped = 0;
5520
53.1k
  cpi->svc.last_layer_dropped[cpi->svc.spatial_layer_id] = 0;
5521
53.1k
  if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
5522
53.0k
    cpi->svc.num_encoded_top_layer++;
5523
5524
  // Keep track of the frame buffer index updated/refreshed for the
5525
  // current encoded TL0 superframe.
5526
53.1k
  if (cpi->svc.temporal_layer_id == 0) {
5527
53.0k
    if (cpi->refresh_last_frame)
5528
53.0k
      cpi->svc.fb_idx_upd_tl0[cpi->svc.spatial_layer_id] = cpi->lst_fb_idx;
5529
0
    else if (cpi->refresh_golden_frame)
5530
0
      cpi->svc.fb_idx_upd_tl0[cpi->svc.spatial_layer_id] = cpi->gld_fb_idx;
5531
0
    else if (cpi->refresh_alt_ref_frame)
5532
0
      cpi->svc.fb_idx_upd_tl0[cpi->svc.spatial_layer_id] = cpi->alt_fb_idx;
5533
53.0k
  }
5534
5535
53.1k
  if (cm->seg.update_map) update_reference_segmentation_map(cpi);
5536
5537
53.1k
  if (frame_is_intra_only(cm) == 0) {
5538
40.6k
    release_scaled_references(cpi);
5539
40.6k
  }
5540
53.1k
  vp9_update_reference_frames(cpi);
5541
5542
53.1k
  if (!cm->show_existing_frame) {
5543
265k
    for (t = TX_4X4; t <= TX_32X32; ++t) {
5544
212k
      full_to_model_counts(cpi->td.counts->coef[t],
5545
212k
                           cpi->td.rd_counts.coef_counts[t]);
5546
212k
    }
5547
5548
53.0k
    if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
5549
0
      if (!frame_is_intra_only(cm)) {
5550
0
        vp9_adapt_mode_probs(cm);
5551
0
        vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
5552
0
      }
5553
0
      vp9_adapt_coef_probs(cm);
5554
0
    }
5555
53.0k
  }
5556
5557
53.1k
  cpi->ext_refresh_frame_flags_pending = 0;
5558
5559
53.1k
  if (cpi->refresh_golden_frame == 1)
5560
15.1k
    cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5561
37.9k
  else
5562
37.9k
    cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5563
5564
53.1k
  if (cpi->refresh_alt_ref_frame == 1)
5565
12.4k
    cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5566
40.7k
  else
5567
40.7k
    cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5568
5569
53.1k
  cpi->ref_frame_flags = get_ref_frame_flags(cpi);
5570
5571
53.1k
  cm->last_frame_type = cm->frame_type;
5572
5573
53.1k
  vp9_rc_postencode_update(cpi, *size);
5574
5575
53.1k
  if (cpi->compute_frame_low_motion_onepass && oxcf->pass == 0 &&
5576
53.1k
      !frame_is_intra_only(cm) &&
5577
53.1k
      (!cpi->use_svc ||
5578
40.6k
       (cpi->use_svc &&
5579
0
        !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame &&
5580
40.6k
        cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1))) {
5581
40.6k
    vp9_compute_frame_low_motion(cpi);
5582
40.6k
  }
5583
5584
53.1k
  *size = VPXMAX(1, *size);
5585
5586
#if 0
5587
  output_frame_level_debug_stats(cpi);
5588
#endif
5589
5590
53.1k
  if (cm->frame_type == KEY_FRAME) {
5591
    // Tell the caller that the frame was coded as a key frame
5592
12.4k
    *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5593
40.7k
  } else {
5594
40.7k
    *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5595
40.7k
  }
5596
5597
  // Clear the one shot update flags for segmentation map and mode/ref loop
5598
  // filter deltas.
5599
53.1k
  cm->seg.update_map = 0;
5600
53.1k
  cm->seg.update_data = 0;
5601
53.1k
  cm->lf.mode_ref_delta_update = 0;
5602
5603
  // keep track of the last coded dimensions
5604
53.1k
  cm->last_width = cm->width;
5605
53.1k
  cm->last_height = cm->height;
5606
5607
  // reset to normal state now that we are done.
5608
53.1k
  if (!cm->show_existing_frame) {
5609
53.0k
    cm->last_show_frame = cm->show_frame;
5610
53.0k
    cm->prev_frame = cm->cur_frame;
5611
53.0k
  }
5612
5613
53.1k
  if (cm->show_frame) {
5614
53.0k
    vp9_swap_mi_and_prev_mi(cm);
5615
53.0k
    if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
5616
53.0k
  }
5617
53.1k
  update_frame_indexes(cm, cm->show_frame);
5618
5619
53.1k
  if (cpi->use_svc) {
5620
0
    cpi->svc
5621
0
        .layer_context[cpi->svc.spatial_layer_id *
5622
0
                           cpi->svc.number_temporal_layers +
5623
0
                       cpi->svc.temporal_layer_id]
5624
0
        .last_frame_type = cm->frame_type;
5625
    // Reset layer_sync back to 0 for next frame.
5626
0
    cpi->svc.spatial_layer_sync[cpi->svc.spatial_layer_id] = 0;
5627
0
  }
5628
5629
53.1k
  cpi->force_update_segmentation = 0;
5630
5631
53.1k
#if !CONFIG_REALTIME_ONLY
5632
53.1k
  if (cpi->oxcf.aq_mode == LOOKAHEAD_AQ)
5633
0
    vp9_alt_ref_aq_unset_all(cpi->alt_ref_aq, cpi);
5634
53.1k
#endif
5635
5636
53.1k
  cpi->svc.previous_frame_is_intra_only = cm->intra_only;
5637
53.1k
  cpi->svc.set_intra_only_frame = 0;
5638
53.1k
}
5639
5640
static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
5641
0
                      size_t dest_size, unsigned int *frame_flags) {
5642
0
  vp9_rc_get_svc_params(cpi);
5643
0
  encode_frame_to_data_rate(cpi, size, dest, dest_size, frame_flags,
5644
                            /*encode_frame_result = */ NULL);
5645
0
}
5646
5647
static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
5648
53.1k
                        size_t dest_size, unsigned int *frame_flags) {
5649
53.1k
  if (cpi->oxcf.rc_mode == VPX_CBR) {
5650
0
    vp9_rc_get_one_pass_cbr_params(cpi);
5651
53.1k
  } else {
5652
53.1k
    vp9_rc_get_one_pass_vbr_params(cpi);
5653
53.1k
  }
5654
53.1k
  encode_frame_to_data_rate(cpi, size, dest, dest_size, frame_flags,
5655
                            /*encode_frame_result = */ NULL);
5656
53.1k
}
5657
5658
#if !CONFIG_REALTIME_ONLY
5659
static void Pass2Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
5660
                        size_t dest_size, unsigned int *frame_flags,
5661
0
                        ENCODE_FRAME_RESULT *encode_frame_result) {
5662
0
  cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
5663
#if CONFIG_MISMATCH_DEBUG
5664
  mismatch_move_frame_idx_w();
5665
#endif
5666
0
  encode_frame_to_data_rate(cpi, size, dest, dest_size, frame_flags,
5667
0
                            encode_frame_result);
5668
0
}
5669
#endif  // !CONFIG_REALTIME_ONLY
5670
5671
int vp9_receive_raw_frame(VP9_COMP *cpi, vpx_enc_frame_flags_t frame_flags,
5672
                          YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5673
53.7k
                          int64_t end_time) {
5674
53.7k
  VP9_COMMON *const cm = &cpi->common;
5675
#if CONFIG_INTERNAL_STATS
5676
  struct vpx_usec_timer timer;
5677
#endif
5678
53.7k
  int res = 0;
5679
53.7k
  const int subsampling_x = sd->subsampling_x;
5680
53.7k
  const int subsampling_y = sd->subsampling_y;
5681
53.7k
#if CONFIG_VP9_HIGHBITDEPTH
5682
53.7k
  const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
5683
#else
5684
  const int use_highbitdepth = 0;
5685
#endif
5686
5687
53.7k
  update_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
5688
#if CONFIG_VP9_TEMPORAL_DENOISING
5689
  setup_denoiser_buffer(cpi);
5690
#endif
5691
5692
53.7k
  alloc_raw_frame_buffers(cpi);
5693
5694
#if CONFIG_INTERNAL_STATS
5695
  vpx_usec_timer_start(&timer);
5696
#endif
5697
5698
53.7k
  if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
5699
53.7k
                         use_highbitdepth, frame_flags))
5700
0
    res = -1;
5701
#if CONFIG_INTERNAL_STATS
5702
  vpx_usec_timer_mark(&timer);
5703
  cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
5704
#endif
5705
5706
53.7k
  if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
5707
53.7k
      (subsampling_x != 1 || subsampling_y != 1)) {
5708
0
    vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
5709
0
                       "Non-4:2:0 color format requires profile 1 or 3");
5710
0
    res = -1;
5711
0
  }
5712
53.7k
  if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
5713
53.7k
      (subsampling_x == 1 && subsampling_y == 1)) {
5714
0
    vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
5715
0
                       "4:2:0 color format requires profile 0 or 2");
5716
0
    res = -1;
5717
0
  }
5718
53.7k
  if (cm->color_space == VPX_CS_SRGB) {
5719
0
    if (cm->profile == PROFILE_0 || cm->profile == PROFILE_2) {
5720
0
      vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
5721
0
                         "SRGB color space requires profile 1 or 3");
5722
0
      res = -1;
5723
0
    }
5724
0
    if (subsampling_x != 0 || subsampling_y != 0) {
5725
0
      vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
5726
0
                         "SRGB color space requires 4:4:4");
5727
0
      res = -1;
5728
0
    }
5729
0
  }
5730
53.7k
  return res;
5731
53.7k
}
5732
5733
53.0k
static int frame_is_reference(const VP9_COMP *cpi) {
5734
53.0k
  const VP9_COMMON *cm = &cpi->common;
5735
5736
53.0k
  return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
5737
53.0k
         cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame ||
5738
53.0k
         cm->refresh_frame_context || cm->lf.mode_ref_delta_update ||
5739
53.0k
         cm->seg.update_map || cm->seg.update_data;
5740
53.0k
}
5741
5742
static void adjust_frame_rate(VP9_COMP *cpi,
5743
53.1k
                              const struct lookahead_entry *source) {
5744
53.1k
  int64_t this_duration;
5745
53.1k
  int step = 0;
5746
5747
53.1k
  if (source->ts_start == cpi->first_time_stamp_ever) {
5748
53.1k
    this_duration = source->ts_end - source->ts_start;
5749
53.1k
    step = 1;
5750
53.1k
  } else {
5751
0
    int64_t last_duration =
5752
0
        cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5753
5754
0
    this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5755
5756
    // do a step update if the duration changes by 10%
5757
0
    if (last_duration)
5758
0
      step = (int)((this_duration - last_duration) * 10 / last_duration);
5759
0
  }
5760
5761
53.1k
  if (this_duration) {
5762
52.6k
    if (step) {
5763
52.6k
      vp9_new_framerate(cpi, 10000000.0 / this_duration);
5764
52.6k
    } else {
5765
      // Average this frame's rate into the last second's average
5766
      // frame rate. If we haven't seen 1 second yet, then average
5767
      // over the whole interval seen.
5768
0
      const double interval = VPXMIN(
5769
0
          (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5770
0
      double avg_duration = 10000000.0 / cpi->framerate;
5771
0
      avg_duration *= (interval - avg_duration + this_duration);
5772
0
      avg_duration /= interval;
5773
5774
0
      vp9_new_framerate(cpi, 10000000.0 / avg_duration);
5775
0
    }
5776
52.6k
  }
5777
53.1k
  cpi->last_time_stamp_seen = source->ts_start;
5778
53.1k
  cpi->last_end_time_stamp_seen = source->ts_end;
5779
53.1k
}
5780
5781
// Returns 0 if this is not an alt ref else the offset of the source frame
5782
// used as the arf midpoint.
5783
133k
static int get_arf_src_index(VP9_COMP *cpi) {
5784
133k
  RATE_CONTROL *const rc = &cpi->rc;
5785
133k
  int arf_src_index = 0;
5786
133k
  if (is_altref_enabled(cpi)) {
5787
133k
    if (cpi->oxcf.pass == 2) {
5788
0
      const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5789
0
      if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5790
0
        arf_src_index = gf_group->arf_src_offset[gf_group->index];
5791
0
      }
5792
133k
    } else if (rc->source_alt_ref_pending) {
5793
0
      arf_src_index = rc->frames_till_gf_update_due;
5794
0
    }
5795
133k
  }
5796
133k
  return arf_src_index;
5797
133k
}
5798
5799
static void check_src_altref(VP9_COMP *cpi,
5800
53.1k
                             const struct lookahead_entry *source) {
5801
53.1k
  RATE_CONTROL *const rc = &cpi->rc;
5802
5803
53.1k
  if (cpi->oxcf.pass == 2) {
5804
0
    const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5805
0
    rc->is_src_frame_alt_ref =
5806
0
        (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
5807
53.1k
  } else {
5808
53.1k
    rc->is_src_frame_alt_ref =
5809
53.1k
        cpi->alt_ref_source && (source == cpi->alt_ref_source);
5810
53.1k
  }
5811
5812
53.1k
  if (rc->is_src_frame_alt_ref) {
5813
    // Current frame is an ARF overlay frame.
5814
0
    cpi->alt_ref_source = NULL;
5815
5816
    // Don't refresh the last buffer for an ARF overlay frame. It will
5817
    // become the GF so preserve last as an alternative prediction option.
5818
0
    cpi->refresh_last_frame = 0;
5819
0
  }
5820
53.1k
}
5821
5822
#if CONFIG_INTERNAL_STATS
5823
static void adjust_image_stat(double y, double u, double v, double all,
5824
                              ImageStat *s) {
5825
  s->stat[Y] += y;
5826
  s->stat[U] += u;
5827
  s->stat[V] += v;
5828
  s->stat[ALL] += all;
5829
  s->worst = VPXMIN(s->worst, all);
5830
}
5831
#endif  // CONFIG_INTERNAL_STATS
5832
5833
// Adjust the maximum allowable frame size for the target level.
5834
0
static void level_rc_framerate(VP9_COMP *cpi, int arf_src_index) {
5835
0
  RATE_CONTROL *const rc = &cpi->rc;
5836
0
  LevelConstraint *const ls = &cpi->level_constraint;
5837
0
  VP9_COMMON *const cm = &cpi->common;
5838
0
  const double max_cpb_size = ls->max_cpb_size;
5839
0
  vpx_clear_system_state();
5840
0
  rc->max_frame_bandwidth = VPXMIN(rc->max_frame_bandwidth, ls->max_frame_size);
5841
0
  if (frame_is_intra_only(cm)) {
5842
0
    rc->max_frame_bandwidth =
5843
0
        VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.5));
5844
0
  } else if (arf_src_index > 0) {
5845
0
    rc->max_frame_bandwidth =
5846
0
        VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.4));
5847
0
  } else {
5848
0
    rc->max_frame_bandwidth =
5849
0
        VPXMIN(rc->max_frame_bandwidth, (int)(max_cpb_size * 0.2));
5850
0
  }
5851
0
}
5852
5853
0
static void update_level_info(VP9_COMP *cpi, size_t *size, int arf_src_index) {
5854
0
  VP9_COMMON *const cm = &cpi->common;
5855
0
  Vp9LevelInfo *const level_info = &cpi->level_info;
5856
0
  Vp9LevelSpec *const level_spec = &level_info->level_spec;
5857
0
  Vp9LevelStats *const level_stats = &level_info->level_stats;
5858
0
  int i, idx;
5859
0
  uint64_t luma_samples, dur_end;
5860
0
  const uint32_t luma_pic_size = cm->width * cm->height;
5861
0
  const uint32_t luma_pic_breadth = VPXMAX(cm->width, cm->height);
5862
0
  LevelConstraint *const level_constraint = &cpi->level_constraint;
5863
0
  const int8_t level_index = level_constraint->level_index;
5864
0
  double cpb_data_size;
5865
5866
0
  vpx_clear_system_state();
5867
5868
  // update level_stats
5869
0
  level_stats->total_compressed_size += *size;
5870
0
  if (cm->show_frame) {
5871
0
    level_stats->total_uncompressed_size +=
5872
0
        luma_pic_size +
5873
0
        2 * (luma_pic_size >> (cm->subsampling_x + cm->subsampling_y));
5874
0
    level_stats->time_encoded =
5875
0
        (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
5876
0
        (double)TICKS_PER_SEC;
5877
0
  }
5878
5879
0
  if (arf_src_index > 0) {
5880
0
    if (!level_stats->seen_first_altref) {
5881
0
      level_stats->seen_first_altref = 1;
5882
0
    } else if (level_stats->frames_since_last_altref <
5883
0
               level_spec->min_altref_distance) {
5884
0
      level_spec->min_altref_distance = level_stats->frames_since_last_altref;
5885
0
    }
5886
0
    level_stats->frames_since_last_altref = 0;
5887
0
  } else {
5888
0
    ++level_stats->frames_since_last_altref;
5889
0
  }
5890
5891
0
  if (level_stats->frame_window_buffer.len < FRAME_WINDOW_SIZE - 1) {
5892
0
    idx = (level_stats->frame_window_buffer.start +
5893
0
           level_stats->frame_window_buffer.len++) %
5894
0
          FRAME_WINDOW_SIZE;
5895
0
  } else {
5896
0
    idx = level_stats->frame_window_buffer.start;
5897
0
    level_stats->frame_window_buffer.start = (idx + 1) % FRAME_WINDOW_SIZE;
5898
0
  }
5899
0
  level_stats->frame_window_buffer.buf[idx].ts = cpi->last_time_stamp_seen;
5900
0
  level_stats->frame_window_buffer.buf[idx].size = (uint32_t)(*size);
5901
0
  level_stats->frame_window_buffer.buf[idx].luma_samples = luma_pic_size;
5902
5903
0
  if (cm->frame_type == KEY_FRAME) {
5904
0
    level_stats->ref_refresh_map = 0;
5905
0
  } else {
5906
0
    int count = 0;
5907
0
    level_stats->ref_refresh_map |= vp9_get_refresh_mask(cpi);
5908
    // Also need to consider the case where the encoder refers to a buffer
5909
    // that has been implicitly refreshed after encoding a keyframe.
5910
0
    if (!cm->intra_only) {
5911
0
      level_stats->ref_refresh_map |= (1 << cpi->lst_fb_idx);
5912
0
      level_stats->ref_refresh_map |= (1 << cpi->gld_fb_idx);
5913
0
      level_stats->ref_refresh_map |= (1 << cpi->alt_fb_idx);
5914
0
    }
5915
0
    for (i = 0; i < REF_FRAMES; ++i) {
5916
0
      count += (level_stats->ref_refresh_map >> i) & 1;
5917
0
    }
5918
0
    if (count > level_spec->max_ref_frame_buffers) {
5919
0
      level_spec->max_ref_frame_buffers = count;
5920
0
    }
5921
0
  }
5922
5923
  // update average_bitrate
5924
0
  level_spec->average_bitrate = (double)level_stats->total_compressed_size /
5925
0
                                125.0 / level_stats->time_encoded;
5926
5927
  // update max_luma_sample_rate
5928
0
  luma_samples = 0;
5929
0
  for (i = 0; i < level_stats->frame_window_buffer.len; ++i) {
5930
0
    idx = (level_stats->frame_window_buffer.start +
5931
0
           level_stats->frame_window_buffer.len - 1 - i) %
5932
0
          FRAME_WINDOW_SIZE;
5933
0
    if (i == 0) {
5934
0
      dur_end = level_stats->frame_window_buffer.buf[idx].ts;
5935
0
    }
5936
0
    if (dur_end - level_stats->frame_window_buffer.buf[idx].ts >=
5937
0
        TICKS_PER_SEC) {
5938
0
      break;
5939
0
    }
5940
0
    luma_samples += level_stats->frame_window_buffer.buf[idx].luma_samples;
5941
0
  }
5942
0
  if (luma_samples > level_spec->max_luma_sample_rate) {
5943
0
    level_spec->max_luma_sample_rate = luma_samples;
5944
0
  }
5945
5946
  // update max_cpb_size
5947
0
  cpb_data_size = 0;
5948
0
  for (i = 0; i < CPB_WINDOW_SIZE; ++i) {
5949
0
    if (i >= level_stats->frame_window_buffer.len) break;
5950
0
    idx = (level_stats->frame_window_buffer.start +
5951
0
           level_stats->frame_window_buffer.len - 1 - i) %
5952
0
          FRAME_WINDOW_SIZE;
5953
0
    cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
5954
0
  }
5955
0
  cpb_data_size = cpb_data_size / 125.0;
5956
0
  if (cpb_data_size > level_spec->max_cpb_size) {
5957
0
    level_spec->max_cpb_size = cpb_data_size;
5958
0
  }
5959
5960
  // update max_luma_picture_size
5961
0
  if (luma_pic_size > level_spec->max_luma_picture_size) {
5962
0
    level_spec->max_luma_picture_size = luma_pic_size;
5963
0
  }
5964
5965
  // update max_luma_picture_breadth
5966
0
  if (luma_pic_breadth > level_spec->max_luma_picture_breadth) {
5967
0
    level_spec->max_luma_picture_breadth = luma_pic_breadth;
5968
0
  }
5969
5970
  // update compression_ratio
5971
0
  level_spec->compression_ratio = (double)level_stats->total_uncompressed_size *
5972
0
                                  cm->bit_depth /
5973
0
                                  level_stats->total_compressed_size / 8.0;
5974
5975
  // update max_col_tiles
5976
0
  if (level_spec->max_col_tiles < (1 << cm->log2_tile_cols)) {
5977
0
    level_spec->max_col_tiles = (1 << cm->log2_tile_cols);
5978
0
  }
5979
5980
0
  if (level_index >= 0 && level_constraint->fail_flag == 0) {
5981
0
    if (level_spec->max_luma_picture_size >
5982
0
        vp9_level_defs[level_index].max_luma_picture_size) {
5983
0
      level_constraint->fail_flag |= (1 << LUMA_PIC_SIZE_TOO_LARGE);
5984
0
      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
5985
0
                         "Failed to encode to the target level %d. %s",
5986
0
                         vp9_level_defs[level_index].level,
5987
0
                         level_fail_messages[LUMA_PIC_SIZE_TOO_LARGE]);
5988
0
    }
5989
5990
0
    if (level_spec->max_luma_picture_breadth >
5991
0
        vp9_level_defs[level_index].max_luma_picture_breadth) {
5992
0
      level_constraint->fail_flag |= (1 << LUMA_PIC_BREADTH_TOO_LARGE);
5993
0
      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
5994
0
                         "Failed to encode to the target level %d. %s",
5995
0
                         vp9_level_defs[level_index].level,
5996
0
                         level_fail_messages[LUMA_PIC_BREADTH_TOO_LARGE]);
5997
0
    }
5998
5999
0
    if ((double)level_spec->max_luma_sample_rate >
6000
0
        (double)vp9_level_defs[level_index].max_luma_sample_rate *
6001
0
            (1 + SAMPLE_RATE_GRACE_P)) {
6002
0
      level_constraint->fail_flag |= (1 << LUMA_SAMPLE_RATE_TOO_LARGE);
6003
0
      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6004
0
                         "Failed to encode to the target level %d. %s",
6005
0
                         vp9_level_defs[level_index].level,
6006
0
                         level_fail_messages[LUMA_SAMPLE_RATE_TOO_LARGE]);
6007
0
    }
6008
6009
0
    if (level_spec->max_col_tiles > vp9_level_defs[level_index].max_col_tiles) {
6010
0
      level_constraint->fail_flag |= (1 << TOO_MANY_COLUMN_TILE);
6011
0
      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6012
0
                         "Failed to encode to the target level %d. %s",
6013
0
                         vp9_level_defs[level_index].level,
6014
0
                         level_fail_messages[TOO_MANY_COLUMN_TILE]);
6015
0
    }
6016
6017
0
    if (level_spec->min_altref_distance <
6018
0
        vp9_level_defs[level_index].min_altref_distance) {
6019
0
      level_constraint->fail_flag |= (1 << ALTREF_DIST_TOO_SMALL);
6020
0
      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6021
0
                         "Failed to encode to the target level %d. %s",
6022
0
                         vp9_level_defs[level_index].level,
6023
0
                         level_fail_messages[ALTREF_DIST_TOO_SMALL]);
6024
0
    }
6025
6026
0
    if (level_spec->max_ref_frame_buffers >
6027
0
        vp9_level_defs[level_index].max_ref_frame_buffers) {
6028
0
      level_constraint->fail_flag |= (1 << TOO_MANY_REF_BUFFER);
6029
0
      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6030
0
                         "Failed to encode to the target level %d. %s",
6031
0
                         vp9_level_defs[level_index].level,
6032
0
                         level_fail_messages[TOO_MANY_REF_BUFFER]);
6033
0
    }
6034
6035
0
    if (level_spec->max_cpb_size > vp9_level_defs[level_index].max_cpb_size) {
6036
0
      level_constraint->fail_flag |= (1 << CPB_TOO_LARGE);
6037
0
      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
6038
0
                         "Failed to encode to the target level %d. %s",
6039
0
                         vp9_level_defs[level_index].level,
6040
0
                         level_fail_messages[CPB_TOO_LARGE]);
6041
0
    }
6042
6043
    // Set an upper bound for the next frame size. It will be used in
6044
    // level_rc_framerate() before encoding the next frame.
6045
0
    cpb_data_size = 0;
6046
0
    for (i = 0; i < CPB_WINDOW_SIZE - 1; ++i) {
6047
0
      if (i >= level_stats->frame_window_buffer.len) break;
6048
0
      idx = (level_stats->frame_window_buffer.start +
6049
0
             level_stats->frame_window_buffer.len - 1 - i) %
6050
0
            FRAME_WINDOW_SIZE;
6051
0
      cpb_data_size += level_stats->frame_window_buffer.buf[idx].size;
6052
0
    }
6053
0
    cpb_data_size = cpb_data_size / 125.0;
6054
0
    level_constraint->max_frame_size =
6055
0
        (int)((vp9_level_defs[level_index].max_cpb_size - cpb_data_size) *
6056
0
              1000.0);
6057
0
    if (level_stats->frame_window_buffer.len < CPB_WINDOW_SIZE - 1)
6058
0
      level_constraint->max_frame_size >>= 1;
6059
0
  }
6060
0
}
6061
6062
void vp9_get_ref_frame_info(FRAME_UPDATE_TYPE update_type, int ref_frame_flags,
6063
                            RefCntBuffer *ref_frame_bufs[MAX_INTER_REF_FRAMES],
6064
                            int *ref_frame_coding_indexes,
6065
0
                            int *ref_frame_valid_list) {
6066
0
  if (update_type != KF_UPDATE) {
6067
0
    const VP9_REFFRAME inter_ref_flags[MAX_INTER_REF_FRAMES] = { VP9_LAST_FLAG,
6068
0
                                                                 VP9_GOLD_FLAG,
6069
0
                                                                 VP9_ALT_FLAG };
6070
0
    int i;
6071
0
    for (i = 0; i < MAX_INTER_REF_FRAMES; ++i) {
6072
0
      assert(ref_frame_bufs[i] != NULL);
6073
0
      ref_frame_coding_indexes[i] = ref_frame_bufs[i]->frame_coding_index;
6074
0
      ref_frame_valid_list[i] = (ref_frame_flags & inter_ref_flags[i]) != 0;
6075
0
    }
6076
0
  } else {
6077
    // No reference frame is available when this is a key frame.
6078
0
    int i;
6079
0
    for (i = 0; i < MAX_INTER_REF_FRAMES; ++i) {
6080
0
      ref_frame_coding_indexes[i] = -1;
6081
0
      ref_frame_valid_list[i] = 0;
6082
0
    }
6083
0
  }
6084
0
}
6085
6086
84.9k
void vp9_init_encode_frame_result(ENCODE_FRAME_RESULT *encode_frame_result) {
6087
84.9k
  encode_frame_result->show_idx = -1;  // Actual encoding doesn't happen.
6088
84.9k
}
6089
6090
// Returns if TPL stats need to be calculated.
6091
53.1k
static INLINE int should_run_tpl(VP9_COMP *cpi, int gf_group_index) {
6092
53.1k
  RATE_CONTROL *const rc = &cpi->rc;
6093
53.1k
  if (!cpi->sf.enable_tpl_model) return 0;
6094
  // If there is an ARF for this GOP, TPL stats is always calculated.
6095
23.5k
  if (gf_group_index == 1 &&
6096
23.5k
      cpi->twopass.gf_group.update_type[gf_group_index] == ARF_UPDATE)
6097
0
    return 1;
6098
  // If this GOP doesn't have an ARF, TPL stats is still calculated, only when
6099
  // external rate control is used.
6100
23.5k
  if (cpi->ext_ratectrl.ready &&
6101
23.5k
      cpi->ext_ratectrl.funcs.send_tpl_gop_stats != NULL &&
6102
23.5k
      rc->frames_till_gf_update_due == rc->baseline_gf_interval &&
6103
23.5k
      cpi->twopass.gf_group.update_type[1] != ARF_UPDATE) {
6104
0
    return 1;
6105
0
  }
6106
23.5k
  return 0;
6107
23.5k
}
6108
6109
int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
6110
                            size_t *size, uint8_t *dest, size_t dest_size,
6111
                            int64_t *time_stamp, int64_t *time_end, int flush,
6112
133k
                            ENCODE_FRAME_RESULT *encode_frame_result) {
6113
133k
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
6114
133k
  VP9_COMMON *const cm = &cpi->common;
6115
133k
  BufferPool *const pool = cm->buffer_pool;
6116
133k
  RATE_CONTROL *const rc = &cpi->rc;
6117
#if CONFIG_INTERNAL_STATS
6118
  struct vpx_usec_timer cmptimer;
6119
#endif
6120
133k
  YV12_BUFFER_CONFIG *force_src_buffer = NULL;
6121
133k
  struct lookahead_entry *last_source = NULL;
6122
133k
  struct lookahead_entry *source = NULL;
6123
133k
  int arf_src_index;
6124
133k
  const int gf_group_index = cpi->twopass.gf_group.index;
6125
133k
  int i;
6126
6127
#if CONFIG_COLLECT_COMPONENT_TIMING
6128
  if (oxcf->pass == 2) start_timing(cpi, vp9_get_compressed_data_time);
6129
#endif
6130
6131
133k
  if (is_one_pass_svc(cpi)) {
6132
0
    vp9_one_pass_svc_start_layer(cpi);
6133
0
  }
6134
6135
#if CONFIG_INTERNAL_STATS
6136
  vpx_usec_timer_start(&cmptimer);
6137
#endif
6138
6139
133k
  vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
6140
6141
  // Is multi-arf enabled.
6142
  // Note that at the moment multi_arf is only configured for 2 pass VBR and
6143
  // will not work properly with svc.
6144
  // Enable the Jingning's new "multi_layer_arf" code if "enable_auto_arf"
6145
  // is greater than or equal to 2.
6146
133k
  if ((oxcf->pass == 2) && !cpi->use_svc && (cpi->oxcf.enable_auto_arf >= 2))
6147
0
    cpi->multi_layer_arf = 1;
6148
133k
  else
6149
133k
    cpi->multi_layer_arf = 0;
6150
6151
  // Normal defaults
6152
133k
  cm->reset_frame_context = 0;
6153
133k
  cm->refresh_frame_context = 1;
6154
133k
  if (!is_one_pass_svc(cpi)) {
6155
133k
    cpi->refresh_last_frame = 1;
6156
133k
    cpi->refresh_golden_frame = 0;
6157
133k
    cpi->refresh_alt_ref_frame = 0;
6158
133k
  }
6159
6160
  // Should we encode an arf frame.
6161
133k
  arf_src_index = get_arf_src_index(cpi);
6162
6163
133k
  if (arf_src_index) {
6164
0
    for (i = 0; i <= arf_src_index; ++i) {
6165
0
      struct lookahead_entry *e = vp9_lookahead_peek(cpi->lookahead, i);
6166
      // Avoid creating an alt-ref if there's a forced keyframe pending.
6167
0
      if (e == NULL) {
6168
0
        break;
6169
0
      } else if (e->flags == VPX_EFLAG_FORCE_KF) {
6170
0
        arf_src_index = 0;
6171
0
        flush = 1;
6172
0
        break;
6173
0
      }
6174
0
    }
6175
0
  }
6176
6177
  // Clear arf index stack before group of pictures processing starts.
6178
133k
  if (gf_group_index == 1) {
6179
0
    stack_init(cpi->twopass.gf_group.arf_index_stack, MAX_LAG_BUFFERS * 2);
6180
0
    cpi->twopass.gf_group.stack_size = 0;
6181
0
  }
6182
6183
133k
  if (arf_src_index) {
6184
0
    if (!(cpi->ext_ratectrl.ready &&
6185
0
          (cpi->ext_ratectrl.funcs.rc_type & VPX_RC_GOP) != 0 &&
6186
0
          cpi->ext_ratectrl.funcs.get_gop_decision != NULL)) {
6187
      // This assert only makes sense when not using external RC.
6188
0
      assert(arf_src_index <= rc->frames_to_key);
6189
0
    }
6190
0
    if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
6191
0
      cpi->alt_ref_source = source;
6192
6193
0
#if !CONFIG_REALTIME_ONLY
6194
0
      if ((oxcf->mode != REALTIME) && (oxcf->arnr_max_frames > 0) &&
6195
0
          (oxcf->arnr_strength > 0)) {
6196
0
        int bitrate = cpi->rc.avg_frame_bandwidth / 40;
6197
0
        int not_low_bitrate = bitrate > ALT_REF_AQ_LOW_BITRATE_BOUNDARY;
6198
6199
0
        int not_last_frame = (cpi->lookahead->sz - arf_src_index > 1);
6200
0
        not_last_frame |= ALT_REF_AQ_APPLY_TO_LAST_FRAME;
6201
6202
#if CONFIG_COLLECT_COMPONENT_TIMING
6203
        start_timing(cpi, vp9_temporal_filter_time);
6204
#endif
6205
        // Produce the filtered ARF frame.
6206
0
        vp9_temporal_filter(cpi, arf_src_index);
6207
0
        vpx_extend_frame_borders(&cpi->tf_buffer);
6208
#if CONFIG_COLLECT_COMPONENT_TIMING
6209
        end_timing(cpi, vp9_temporal_filter_time);
6210
#endif
6211
6212
        // for small bitrates segmentation overhead usually
6213
        // eats all bitrate gain from enabling delta quantizers
6214
0
        if (cpi->oxcf.alt_ref_aq != 0 && not_low_bitrate && not_last_frame)
6215
0
          vp9_alt_ref_aq_setup_mode(cpi->alt_ref_aq, cpi);
6216
6217
0
        force_src_buffer = &cpi->tf_buffer;
6218
0
      }
6219
0
#endif
6220
0
      cm->show_frame = 0;
6221
0
      cm->intra_only = 0;
6222
0
      cpi->refresh_alt_ref_frame = 1;
6223
0
      cpi->refresh_golden_frame = 0;
6224
0
      cpi->refresh_last_frame = 0;
6225
0
      rc->is_src_frame_alt_ref = 0;
6226
0
      rc->source_alt_ref_pending = 0;
6227
0
    } else {
6228
0
      rc->source_alt_ref_pending = 0;
6229
0
    }
6230
0
  }
6231
6232
133k
  if (!source) {
6233
    // Get last frame source.
6234
133k
    if (cm->current_video_frame > 0) {
6235
101k
      if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
6236
0
        return -1;
6237
101k
    }
6238
6239
    // Read in the source frame.
6240
133k
    if (cpi->use_svc || cpi->svc.set_intra_only_frame)
6241
0
      source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
6242
133k
    else
6243
133k
      source = vp9_lookahead_pop(cpi->lookahead, flush);
6244
6245
133k
    if (source != NULL) {
6246
53.1k
      cm->show_frame = 1;
6247
53.1k
      cm->intra_only = 0;
6248
      // If the flags indicate intra frame, but if the current picture is for
6249
      // spatial layer above first_spatial_layer_to_encode, it should not be an
6250
      // intra picture.
6251
53.1k
      if ((source->flags & VPX_EFLAG_FORCE_KF) && cpi->use_svc &&
6252
53.1k
          cpi->svc.spatial_layer_id > cpi->svc.first_spatial_layer_to_encode) {
6253
0
        source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
6254
0
      }
6255
6256
      // Check to see if the frame should be encoded as an arf overlay.
6257
53.1k
      check_src_altref(cpi, source);
6258
53.1k
    }
6259
133k
  }
6260
6261
133k
  if (source) {
6262
53.1k
    cpi->un_scaled_source = cpi->Source =
6263
53.1k
        force_src_buffer ? force_src_buffer : &source->img;
6264
6265
#ifdef ENABLE_KF_DENOISE
6266
    // Copy of raw source for metrics calculation.
6267
    if (is_psnr_calc_enabled(cpi))
6268
      vp9_copy_and_extend_frame(cpi->Source, &cpi->raw_unscaled_source);
6269
#endif
6270
6271
53.1k
    cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
6272
6273
53.1k
    *time_stamp = source->ts_start;
6274
53.1k
    *time_end = source->ts_end;
6275
53.1k
    *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
6276
80.2k
  } else {
6277
80.2k
    *size = 0;
6278
80.2k
    return -1;
6279
80.2k
  }
6280
6281
53.1k
  if (source->ts_start < cpi->first_time_stamp_ever) {
6282
3.87k
    cpi->first_time_stamp_ever = source->ts_start;
6283
3.87k
    cpi->last_end_time_stamp_seen = source->ts_start;
6284
3.87k
  }
6285
6286
  // Clear down mmx registers
6287
53.1k
  vpx_clear_system_state();
6288
6289
  // adjust frame rates based on timestamps given
6290
53.1k
  if (cm->show_frame) {
6291
53.1k
    if (cpi->use_svc && cpi->svc.use_set_ref_frame_config &&
6292
53.1k
        cpi->svc.duration[cpi->svc.spatial_layer_id] > 0)
6293
0
      vp9_svc_adjust_frame_rate(cpi);
6294
53.1k
    else
6295
53.1k
      adjust_frame_rate(cpi, source);
6296
53.1k
  }
6297
6298
53.1k
  if (is_one_pass_svc(cpi)) {
6299
0
    vp9_update_temporal_layer_framerate(cpi);
6300
0
    vp9_restore_layer_context(cpi);
6301
0
  }
6302
6303
  // Find a free buffer for the new frame, releasing the reference previously
6304
  // held.
6305
53.1k
  if (cm->new_fb_idx != INVALID_IDX) {
6306
49.2k
    --pool->frame_bufs[cm->new_fb_idx].ref_count;
6307
49.2k
  }
6308
53.1k
  cm->new_fb_idx = get_free_fb(cm);
6309
6310
53.1k
  if (cm->new_fb_idx == INVALID_IDX) return -1;
6311
53.1k
  cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
6312
  // If the frame buffer for current frame is the same as previous frame, MV in
6313
  // the base layer shouldn't be used as it'll cause data race.
6314
53.1k
  if (cpi->svc.spatial_layer_id > 0 && cm->cur_frame == cm->prev_frame) {
6315
0
    cpi->svc.use_base_mv = 0;
6316
0
  }
6317
  // Start with a 0 size frame.
6318
53.1k
  *size = 0;
6319
6320
53.1k
  cpi->frame_flags = *frame_flags;
6321
6322
53.1k
#if !CONFIG_REALTIME_ONLY
6323
53.1k
  if ((oxcf->pass == 2) && !cpi->use_svc) {
6324
#if CONFIG_COLLECT_COMPONENT_TIMING
6325
    start_timing(cpi, vp9_rc_get_second_pass_params_time);
6326
#endif
6327
0
    vp9_rc_get_second_pass_params(cpi);
6328
#if CONFIG_COLLECT_COMPONENT_TIMING
6329
    end_timing(cpi, vp9_rc_get_second_pass_params_time);
6330
#endif
6331
53.1k
  } else if (oxcf->pass == 1) {
6332
0
    set_frame_size(cpi);
6333
0
  }
6334
6335
  // Key frame temporal filtering
6336
53.1k
  const int is_key_temporal_filter_enabled =
6337
53.1k
      oxcf->enable_keyframe_filtering && cpi->oxcf.mode != REALTIME &&
6338
53.1k
      (oxcf->pass != 1) && !cpi->use_svc &&
6339
53.1k
      !is_lossless_requested(&cpi->oxcf) && cm->frame_type == KEY_FRAME &&
6340
53.1k
      (oxcf->arnr_max_frames > 0) && (oxcf->arnr_strength > 0) &&
6341
53.1k
      cpi->oxcf.speed < 2;
6342
  // Save the pointer to the original source image.
6343
53.1k
  YV12_BUFFER_CONFIG *source_buffer = cpi->un_scaled_source;
6344
6345
53.1k
  if (is_key_temporal_filter_enabled && source != NULL) {
6346
    // Produce the filtered Key frame. Set distance to -1 since the key frame
6347
    // is already popped out.
6348
0
    vp9_temporal_filter(cpi, -1);
6349
0
    vpx_extend_frame_borders(&cpi->tf_buffer);
6350
0
    force_src_buffer = &cpi->tf_buffer;
6351
0
    cpi->un_scaled_source = cpi->Source =
6352
0
        force_src_buffer ? force_src_buffer : &source->img;
6353
0
  }
6354
53.1k
#endif  // !CONFIG_REALTIME_ONLY
6355
6356
53.1k
  if (oxcf->pass != 1 && cpi->level_constraint.level_index >= 0 &&
6357
53.1k
      cpi->level_constraint.fail_flag == 0)
6358
0
    level_rc_framerate(cpi, arf_src_index);
6359
6360
53.1k
  if (cpi->oxcf.pass != 0 || cpi->use_svc || frame_is_intra_only(cm) == 1) {
6361
58.6k
    for (i = 0; i < REFS_PER_FRAME; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
6362
14.6k
  }
6363
6364
53.1k
  if (cpi->kmeans_data_arr_alloc == 0) {
6365
3.87k
    const int mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
6366
3.87k
    const int mi_rows = mi_cols_aligned_to_sb(cm->mi_rows);
6367
3.87k
#if CONFIG_MULTITHREAD
6368
3.87k
    pthread_mutex_init(&cpi->kmeans_mutex, NULL);
6369
3.87k
#endif
6370
3.87k
    CHECK_MEM_ERROR(
6371
3.87k
        &cm->error, cpi->kmeans_data_arr,
6372
3.87k
        vpx_calloc(mi_rows * mi_cols, sizeof(*cpi->kmeans_data_arr)));
6373
3.87k
    cpi->kmeans_data_stride = mi_cols;
6374
3.87k
    cpi->kmeans_data_arr_alloc = 1;
6375
3.87k
  }
6376
6377
#if CONFIG_NON_GREEDY_MV
6378
  {
6379
    const int mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
6380
    const int mi_rows = mi_cols_aligned_to_sb(cm->mi_rows);
6381
    Status status = vp9_alloc_motion_field_info(
6382
        &cpi->motion_field_info, MAX_ARF_GOP_SIZE, mi_rows, mi_cols);
6383
    if (status == STATUS_FAILED) {
6384
      vpx_internal_error(&(cm)->error, VPX_CODEC_MEM_ERROR,
6385
                         "vp9_alloc_motion_field_info failed");
6386
    }
6387
  }
6388
#endif  // CONFIG_NON_GREEDY_MV
6389
6390
#if CONFIG_COLLECT_COMPONENT_TIMING
6391
  start_timing(cpi, setup_tpl_stats_time);
6392
#endif
6393
53.1k
  if (should_run_tpl(cpi, cpi->twopass.gf_group.index)) {
6394
0
    vp9_init_tpl_buffer(cpi);
6395
0
    vp9_estimate_tpl_qp_gop(cpi);
6396
0
    vp9_setup_tpl_stats(cpi);
6397
0
  }
6398
#if CONFIG_COLLECT_COMPONENT_TIMING
6399
  end_timing(cpi, setup_tpl_stats_time);
6400
#endif
6401
6402
#if CONFIG_BITSTREAM_DEBUG
6403
  assert(cpi->oxcf.max_threads == 0 &&
6404
         "bitstream debug tool does not support multithreading");
6405
  bitstream_queue_record_write();
6406
#endif
6407
#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
6408
  bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
6409
#endif
6410
6411
53.1k
  cpi->td.mb.fp_src_pred = 0;
6412
#if CONFIG_REALTIME_ONLY
6413
  (void)encode_frame_result;
6414
  if (cpi->use_svc) {
6415
    SvcEncode(cpi, size, dest, dest_size, frame_flags);
6416
  } else {
6417
    // One pass encode
6418
    Pass0Encode(cpi, size, dest, dest_size, frame_flags);
6419
  }
6420
#else  // !CONFIG_REALTIME_ONLY
6421
53.1k
  if (oxcf->pass == 1 && !cpi->use_svc) {
6422
0
    const int lossless = is_lossless_requested(oxcf);
6423
0
#if CONFIG_VP9_HIGHBITDEPTH
6424
0
    if (cpi->oxcf.use_highbitdepth)
6425
0
      cpi->td.mb.fwd_txfm4x4 =
6426
0
          lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
6427
0
    else
6428
0
      cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
6429
0
    cpi->td.mb.highbd_inv_txfm_add =
6430
0
        lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
6431
#else
6432
    cpi->td.mb.fwd_txfm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
6433
#endif  // CONFIG_VP9_HIGHBITDEPTH
6434
0
    cpi->td.mb.inv_txfm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
6435
0
    vp9_first_pass(cpi, source);
6436
53.1k
  } else if (oxcf->pass == 2 && !cpi->use_svc) {
6437
#if CONFIG_COLLECT_COMPONENT_TIMING
6438
    // Accumulate 2nd pass time in 2-pass case.
6439
    start_timing(cpi, Pass2Encode_time);
6440
#endif
6441
0
    Pass2Encode(cpi, size, dest, dest_size, frame_flags, encode_frame_result);
6442
0
    vp9_twopass_postencode_update(cpi);
6443
#if CONFIG_COLLECT_COMPONENT_TIMING
6444
    end_timing(cpi, Pass2Encode_time);
6445
#endif
6446
53.1k
  } else if (cpi->use_svc) {
6447
0
    SvcEncode(cpi, size, dest, dest_size, frame_flags);
6448
53.1k
  } else {
6449
    // One pass encode
6450
53.1k
    Pass0Encode(cpi, size, dest, dest_size, frame_flags);
6451
53.1k
  }
6452
53.1k
#endif  // CONFIG_REALTIME_ONLY
6453
6454
53.1k
  if (cm->show_frame) cm->cur_show_frame_fb_idx = cm->new_fb_idx;
6455
6456
53.1k
  if (cm->refresh_frame_context)
6457
53.0k
    cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
6458
6459
  // No frame encoded, or frame was dropped, release scaled references.
6460
53.1k
  if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
6461
0
    release_scaled_references(cpi);
6462
0
  }
6463
6464
53.1k
  if (*size > 0) {
6465
53.0k
    cpi->droppable = !frame_is_reference(cpi);
6466
53.0k
  }
6467
6468
  // Save layer specific state.
6469
53.1k
  if (is_one_pass_svc(cpi) || ((cpi->svc.number_temporal_layers > 1 ||
6470
53.0k
                                cpi->svc.number_spatial_layers > 1) &&
6471
53.0k
                               oxcf->pass == 2)) {
6472
0
    vp9_save_layer_context(cpi);
6473
0
  }
6474
6475
53.1k
  if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
6476
53.0k
    cpi->fixed_qp_onepass = 0;
6477
6478
#if CONFIG_INTERNAL_STATS
6479
  vpx_usec_timer_mark(&cmptimer);
6480
  cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
6481
#endif
6482
6483
53.1k
  if (cpi->keep_level_stats && oxcf->pass != 1)
6484
0
    update_level_info(cpi, size, arf_src_index);
6485
6486
53.1k
#if !CONFIG_REALTIME_ONLY
6487
53.1k
  if (is_key_temporal_filter_enabled && cpi->b_calculate_psnr) {
6488
0
    cpi->raw_source_frame = vp9_scale_if_required(
6489
0
        cm, source_buffer, &cpi->scaled_source, (oxcf->pass == 0), EIGHTTAP, 0);
6490
0
  }
6491
53.1k
#endif  // !CONFIG_REALTIME_ONLY
6492
6493
#if CONFIG_INTERNAL_STATS
6494
6495
  if (oxcf->pass != 1 && !cpi->last_frame_dropped) {
6496
    double samples = 0.0;
6497
    cpi->bytes += *size;
6498
6499
    if (cm->show_frame) {
6500
      uint32_t bit_depth = 8;
6501
      uint32_t in_bit_depth = 8;
6502
      cpi->count++;
6503
#if CONFIG_VP9_HIGHBITDEPTH
6504
      if (cm->use_highbitdepth) {
6505
        in_bit_depth = cpi->oxcf.input_bit_depth;
6506
        bit_depth = cm->bit_depth;
6507
      }
6508
#endif
6509
6510
      if (cpi->b_calculate_psnr) {
6511
        YV12_BUFFER_CONFIG *orig = cpi->raw_source_frame;
6512
        YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
6513
        YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
6514
        PSNR_STATS psnr;
6515
#if CONFIG_VP9_HIGHBITDEPTH
6516
        vpx_calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
6517
                             in_bit_depth);
6518
#else
6519
        vpx_calc_psnr(orig, recon, &psnr);
6520
#endif  // CONFIG_VP9_HIGHBITDEPTH
6521
6522
        adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3],
6523
                          psnr.psnr[0], &cpi->psnr);
6524
        cpi->total_sq_error += psnr.sse[0];
6525
        cpi->total_samples += psnr.samples[0];
6526
        samples = psnr.samples[0];
6527
6528
        {
6529
          PSNR_STATS psnr2;
6530
          double frame_ssim2 = 0, weight = 0;
6531
#if CONFIG_VP9_POSTPROC
6532
          if (vpx_alloc_frame_buffer(
6533
                  pp, recon->y_crop_width, recon->y_crop_height,
6534
                  cm->subsampling_x, cm->subsampling_y,
6535
#if CONFIG_VP9_HIGHBITDEPTH
6536
                  cm->use_highbitdepth,
6537
#endif
6538
                  VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment) < 0) {
6539
            vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
6540
                               "Failed to allocate post processing buffer");
6541
          }
6542
          {
6543
            vp9_ppflags_t ppflags;
6544
            ppflags.post_proc_flag = VP9D_DEBLOCK;
6545
            ppflags.deblocking_level = 0;  // not used in vp9_post_proc_frame()
6546
            ppflags.noise_level = 0;       // not used in vp9_post_proc_frame()
6547
            vp9_post_proc_frame(cm, pp, &ppflags,
6548
                                cpi->un_scaled_source->y_width);
6549
          }
6550
#endif
6551
          vpx_clear_system_state();
6552
6553
#if CONFIG_VP9_HIGHBITDEPTH
6554
          vpx_calc_highbd_psnr(orig, pp, &psnr2, cpi->td.mb.e_mbd.bd,
6555
                               cpi->oxcf.input_bit_depth);
6556
#else
6557
          vpx_calc_psnr(orig, pp, &psnr2);
6558
#endif  // CONFIG_VP9_HIGHBITDEPTH
6559
6560
          cpi->totalp_sq_error += psnr2.sse[0];
6561
          cpi->totalp_samples += psnr2.samples[0];
6562
          adjust_image_stat(psnr2.psnr[1], psnr2.psnr[2], psnr2.psnr[3],
6563
                            psnr2.psnr[0], &cpi->psnrp);
6564
6565
#if CONFIG_VP9_HIGHBITDEPTH
6566
          if (cm->use_highbitdepth) {
6567
            frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight, bit_depth,
6568
                                               in_bit_depth);
6569
          } else {
6570
            frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
6571
          }
6572
#else
6573
          frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
6574
#endif  // CONFIG_VP9_HIGHBITDEPTH
6575
6576
          cpi->worst_ssim = VPXMIN(cpi->worst_ssim, frame_ssim2);
6577
          cpi->summed_quality += frame_ssim2 * weight;
6578
          cpi->summed_weights += weight;
6579
6580
#if CONFIG_VP9_HIGHBITDEPTH
6581
          if (cm->use_highbitdepth) {
6582
            frame_ssim2 = vpx_highbd_calc_ssim(orig, pp, &weight, bit_depth,
6583
                                               in_bit_depth);
6584
          } else {
6585
            frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
6586
          }
6587
#else
6588
          frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
6589
#endif  // CONFIG_VP9_HIGHBITDEPTH
6590
6591
          cpi->summedp_quality += frame_ssim2 * weight;
6592
          cpi->summedp_weights += weight;
6593
#if 0
6594
          if (cm->show_frame) {
6595
            FILE *f = fopen("q_used.stt", "a");
6596
            fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
6597
                    cpi->common.current_video_frame, psnr2.psnr[1],
6598
                    psnr2.psnr[2], psnr2.psnr[3], psnr2.psnr[0], frame_ssim2);
6599
            fclose(f);
6600
          }
6601
#endif
6602
        }
6603
      }
6604
      if (cpi->b_calculate_blockiness) {
6605
#if CONFIG_VP9_HIGHBITDEPTH
6606
        if (!cm->use_highbitdepth)
6607
#endif
6608
        {
6609
          double frame_blockiness = vp9_get_blockiness(
6610
              cpi->Source->y_buffer, cpi->Source->y_stride,
6611
              cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
6612
              cpi->Source->y_width, cpi->Source->y_height);
6613
          cpi->worst_blockiness =
6614
              VPXMAX(cpi->worst_blockiness, frame_blockiness);
6615
          cpi->total_blockiness += frame_blockiness;
6616
        }
6617
      }
6618
6619
      if (cpi->b_calculate_consistency) {
6620
#if CONFIG_VP9_HIGHBITDEPTH
6621
        if (!cm->use_highbitdepth)
6622
#endif
6623
        {
6624
          double this_inconsistency = vpx_get_ssim_metrics(
6625
              cpi->Source->y_buffer, cpi->Source->y_stride,
6626
              cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
6627
              cpi->Source->y_width, cpi->Source->y_height, cpi->ssim_vars,
6628
              &cpi->metrics, 1);
6629
6630
          const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
6631
          double consistency =
6632
              vpx_sse_to_psnr(samples, peak, (double)cpi->total_inconsistency);
6633
          if (consistency > 0.0)
6634
            cpi->worst_consistency =
6635
                VPXMIN(cpi->worst_consistency, consistency);
6636
          cpi->total_inconsistency += this_inconsistency;
6637
        }
6638
      }
6639
6640
      {
6641
        double y, u, v, frame_all;
6642
        frame_all = vpx_calc_fastssim(cpi->Source, cm->frame_to_show, &y, &u,
6643
                                      &v, bit_depth, in_bit_depth);
6644
        adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
6645
      }
6646
      {
6647
        double y, u, v, frame_all;
6648
        frame_all = vpx_psnrhvs(cpi->Source, cm->frame_to_show, &y, &u, &v,
6649
                                bit_depth, in_bit_depth);
6650
        adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6651
      }
6652
    }
6653
  }
6654
6655
#endif
6656
6657
#if CONFIG_COLLECT_COMPONENT_TIMING
6658
  if (oxcf->pass == 2) end_timing(cpi, vp9_get_compressed_data_time);
6659
6660
  // Print out timing information.
6661
  // Note: Use "cpi->frame_component_time[0] > 100 us" to avoid showing of
6662
  // show_existing_frame and lag-in-frames.
6663
  //  if (cpi->frame_component_time[0] > 100)
6664
  if (oxcf->pass == 2) {
6665
    uint64_t frame_total = 0, total = 0;
6666
    int i;
6667
6668
    fprintf(stderr,
6669
            "\n Frame number: %d, Frame type: %s, Show Frame: %d, Q: %d\n",
6670
            cm->current_video_frame, get_frame_type_enum(cm->frame_type),
6671
            cm->show_frame, cm->base_qindex);
6672
    for (i = 0; i < kTimingComponents; i++) {
6673
      cpi->component_time[i] += cpi->frame_component_time[i];
6674
      // Use vp9_get_compressed_data_time (i = 0) as the total time.
6675
      if (i == 0) {
6676
        frame_total = cpi->frame_component_time[0];
6677
        total = cpi->component_time[0];
6678
      }
6679
      fprintf(stderr,
6680
              " %50s:  %15" PRId64 " us [%6.2f%%] (total: %15" PRId64
6681
              " us [%6.2f%%])\n",
6682
              get_component_name(i), cpi->frame_component_time[i],
6683
              (float)((float)cpi->frame_component_time[i] * 100.0 /
6684
                      (float)frame_total),
6685
              cpi->component_time[i],
6686
              (float)((float)cpi->component_time[i] * 100.0 / (float)total));
6687
      cpi->frame_component_time[i] = 0;
6688
    }
6689
  }
6690
#endif
6691
6692
53.1k
  if (is_one_pass_svc(cpi)) {
6693
0
    if (cm->show_frame) {
6694
0
      ++cpi->svc.spatial_layer_to_encode;
6695
0
      if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
6696
0
        cpi->svc.spatial_layer_to_encode = 0;
6697
0
    }
6698
0
  }
6699
6700
53.1k
  vpx_clear_system_state();
6701
53.1k
  return 0;
6702
53.1k
}
6703
6704
int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
6705
0
                              vp9_ppflags_t *flags) {
6706
0
  VP9_COMMON *cm = &cpi->common;
6707
0
#if !CONFIG_VP9_POSTPROC
6708
0
  (void)flags;
6709
0
#endif
6710
6711
0
  if (!cm->show_frame) {
6712
0
    return -1;
6713
0
  } else {
6714
0
    int ret;
6715
#if CONFIG_VP9_POSTPROC
6716
    ret = vp9_post_proc_frame(cm, dest, flags, cpi->un_scaled_source->y_width);
6717
#else
6718
0
    if (cm->frame_to_show) {
6719
0
      *dest = *cm->frame_to_show;
6720
0
      dest->y_width = cm->width;
6721
0
      dest->y_height = cm->height;
6722
0
      dest->uv_width = cm->width >> cm->subsampling_x;
6723
0
      dest->uv_height = cm->height >> cm->subsampling_y;
6724
0
      ret = 0;
6725
0
    } else {
6726
0
      ret = -1;
6727
0
    }
6728
0
#endif  // !CONFIG_VP9_POSTPROC
6729
0
    vpx_clear_system_state();
6730
0
    return ret;
6731
0
  }
6732
0
}
6733
6734
int vp9_set_internal_size(VP9_COMP *cpi, VPX_SCALING_MODE horiz_mode,
6735
0
                          VPX_SCALING_MODE vert_mode) {
6736
0
  VP9_COMMON *cm = &cpi->common;
6737
0
  int hr = 0, hs = 0, vr = 0, vs = 0;
6738
6739
0
  if (horiz_mode > VP8E_ONETWO || vert_mode > VP8E_ONETWO) return -1;
6740
6741
0
  Scale2Ratio(horiz_mode, &hr, &hs);
6742
0
  Scale2Ratio(vert_mode, &vr, &vs);
6743
6744
  // always go to the next whole number
6745
0
  cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6746
0
  cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
6747
0
  if (cm->current_video_frame) {
6748
0
    assert(cm->width <= cpi->initial_width);
6749
0
    assert(cm->height <= cpi->initial_height);
6750
0
  }
6751
6752
0
  update_frame_size(cpi);
6753
6754
0
  return 0;
6755
0
}
6756
6757
int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
6758
0
                         unsigned int height) {
6759
0
  VP9_COMMON *cm = &cpi->common;
6760
0
#if CONFIG_VP9_HIGHBITDEPTH
6761
0
  update_initial_width(cpi, cm->use_highbitdepth, cpi->common.subsampling_x,
6762
0
                       cpi->common.subsampling_y);
6763
#else
6764
  update_initial_width(cpi, 0, cpi->common.subsampling_x,
6765
                       cpi->common.subsampling_y);
6766
#endif  // CONFIG_VP9_HIGHBITDEPTH
6767
6768
#if CONFIG_VP9_TEMPORAL_DENOISING
6769
  setup_denoiser_buffer(cpi);
6770
#endif
6771
0
  alloc_raw_frame_buffers(cpi);
6772
0
  if (width) {
6773
0
    cm->width = width;
6774
0
    if (cm->width > cpi->initial_width) {
6775
0
      cm->width = cpi->initial_width;
6776
0
    }
6777
0
  }
6778
6779
0
  if (height) {
6780
0
    cm->height = height;
6781
0
    if (cm->height > cpi->initial_height) {
6782
0
      cm->height = cpi->initial_height;
6783
0
    }
6784
0
  }
6785
0
  assert(cm->width <= cpi->initial_width);
6786
0
  assert(cm->height <= cpi->initial_height);
6787
6788
0
  update_frame_size(cpi);
6789
6790
0
  return 0;
6791
0
}
6792
6793
0
void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
6794
0
  cpi->use_svc = use_svc;
6795
0
  return;
6796
0
}
6797
6798
52.7k
int vp9_get_quantizer(const VP9_COMP *cpi) { return cpi->common.base_qindex; }
6799
6800
84.9k
void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
6801
84.9k
  if (flags &
6802
84.9k
      (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF)) {
6803
0
    int ref = 7;
6804
6805
0
    if (flags & VP8_EFLAG_NO_REF_LAST) ref ^= VP9_LAST_FLAG;
6806
6807
0
    if (flags & VP8_EFLAG_NO_REF_GF) ref ^= VP9_GOLD_FLAG;
6808
6809
0
    if (flags & VP8_EFLAG_NO_REF_ARF) ref ^= VP9_ALT_FLAG;
6810
6811
0
    vp9_use_as_reference(cpi, ref);
6812
0
  }
6813
6814
84.9k
  if (flags &
6815
84.9k
      (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
6816
84.9k
       VP8_EFLAG_FORCE_GF | VP8_EFLAG_FORCE_ARF)) {
6817
0
    int upd = 7;
6818
6819
0
    if (flags & VP8_EFLAG_NO_UPD_LAST) upd ^= VP9_LAST_FLAG;
6820
6821
0
    if (flags & VP8_EFLAG_NO_UPD_GF) upd ^= VP9_GOLD_FLAG;
6822
6823
0
    if (flags & VP8_EFLAG_NO_UPD_ARF) upd ^= VP9_ALT_FLAG;
6824
6825
0
    vp9_update_reference(cpi, upd);
6826
0
  }
6827
6828
84.9k
  if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
6829
0
    vp9_update_entropy(cpi, 0);
6830
0
  }
6831
84.9k
}
6832
6833
34.5k
void vp9_set_row_mt(VP9_COMP *cpi) {
6834
  // Enable row based multi-threading for supported modes of encoding
6835
34.5k
  cpi->row_mt = 0;
6836
34.5k
  if (((cpi->oxcf.mode == GOOD || cpi->oxcf.mode == BEST) &&
6837
34.5k
       cpi->oxcf.speed < 5 && cpi->oxcf.pass == 1) &&
6838
34.5k
      cpi->oxcf.row_mt && !cpi->use_svc)
6839
0
    cpi->row_mt = 1;
6840
6841
34.5k
  if (cpi->oxcf.mode == GOOD && cpi->oxcf.speed < 5 &&
6842
34.5k
      (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) && cpi->oxcf.row_mt &&
6843
34.5k
      !cpi->use_svc)
6844
0
    cpi->row_mt = 1;
6845
6846
  // In realtime mode, enable row based multi-threading for all the speed levels
6847
  // where non-rd path is used.
6848
34.5k
  if (cpi->oxcf.mode == REALTIME && cpi->oxcf.speed >= 5 && cpi->oxcf.row_mt) {
6849
0
    cpi->row_mt = 1;
6850
0
  }
6851
6852
34.5k
  if (cpi->row_mt)
6853
0
    cpi->row_mt_bit_exact = 1;
6854
34.5k
  else
6855
34.5k
    cpi->row_mt_bit_exact = 0;
6856
34.5k
}