Coverage Report

Created: 2026-09-01 06:54

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