Coverage Report

Created: 2026-10-28 08:13

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