Coverage Report

Created: 2026-05-16 07:49

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