Coverage Report

Created: 2024-09-06 07:53

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