Coverage Report

Created: 2022-08-24 06:17

/src/aom/av1/encoder/encoder.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#include <limits.h>
13
#include <float.h>
14
#include <math.h>
15
#include <stdio.h>
16
#include <time.h>
17
#include <stdlib.h>
18
19
#include "config/aom_config.h"
20
#include "config/aom_dsp_rtcd.h"
21
22
#include "aom/aomcx.h"
23
24
#if CONFIG_DENOISE
25
#include "aom_dsp/grain_table.h"
26
#include "aom_dsp/noise_util.h"
27
#include "aom_dsp/noise_model.h"
28
#endif
29
#include "aom_dsp/psnr.h"
30
#if CONFIG_INTERNAL_STATS
31
#include "aom_dsp/ssim.h"
32
#endif
33
#include "aom_ports/aom_once.h"
34
#include "aom_ports/aom_timer.h"
35
#include "aom_ports/mem.h"
36
#include "aom_scale/aom_scale.h"
37
#if CONFIG_BITSTREAM_DEBUG
38
#include "aom_util/debug_util.h"
39
#endif  // CONFIG_BITSTREAM_DEBUG
40
41
#include "av1/common/alloccommon.h"
42
#include "av1/common/filter.h"
43
#include "av1/common/idct.h"
44
#include "av1/common/reconinter.h"
45
#include "av1/common/reconintra.h"
46
#include "av1/common/resize.h"
47
#include "av1/common/tile_common.h"
48
49
#include "av1/encoder/allintra_vis.h"
50
#include "av1/encoder/aq_complexity.h"
51
#include "av1/encoder/aq_cyclicrefresh.h"
52
#include "av1/encoder/aq_variance.h"
53
#include "av1/encoder/bitstream.h"
54
#include "av1/encoder/context_tree.h"
55
#include "av1/encoder/dwt.h"
56
#include "av1/encoder/encodeframe.h"
57
#include "av1/encoder/encodemv.h"
58
#include "av1/encoder/encode_strategy.h"
59
#include "av1/encoder/encoder.h"
60
#include "av1/encoder/encoder_alloc.h"
61
#include "av1/encoder/encoder_utils.h"
62
#include "av1/encoder/encodetxb.h"
63
#include "av1/encoder/ethread.h"
64
#include "av1/encoder/firstpass.h"
65
#include "av1/encoder/hash_motion.h"
66
#include "av1/encoder/hybrid_fwd_txfm.h"
67
#include "av1/encoder/intra_mode_search.h"
68
#include "av1/encoder/mv_prec.h"
69
#include "av1/encoder/pass2_strategy.h"
70
#include "av1/encoder/pickcdef.h"
71
#include "av1/encoder/picklpf.h"
72
#include "av1/encoder/pickrst.h"
73
#include "av1/encoder/random.h"
74
#include "av1/encoder/ratectrl.h"
75
#include "av1/encoder/rc_utils.h"
76
#include "av1/encoder/rd.h"
77
#include "av1/encoder/rdopt.h"
78
#include "av1/encoder/segmentation.h"
79
#include "av1/encoder/speed_features.h"
80
#include "av1/encoder/superres_scale.h"
81
#include "av1/encoder/thirdpass.h"
82
#include "av1/encoder/tpl_model.h"
83
#include "av1/encoder/reconinter_enc.h"
84
#include "av1/encoder/var_based_part.h"
85
86
0
#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
87
88
// #define OUTPUT_YUV_REC
89
#ifdef OUTPUT_YUV_REC
90
FILE *yuv_rec_file;
91
#define FILE_NAME_LEN 100
92
#endif
93
94
#ifdef OUTPUT_YUV_DENOISED
95
FILE *yuv_denoised_file = NULL;
96
#endif
97
98
0
static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
99
0
  switch (mode) {
100
0
    case NORMAL:
101
0
      *hr = 1;
102
0
      *hs = 1;
103
0
      break;
104
0
    case FOURFIVE:
105
0
      *hr = 4;
106
0
      *hs = 5;
107
0
      break;
108
0
    case THREEFIVE:
109
0
      *hr = 3;
110
0
      *hs = 5;
111
0
      break;
112
0
    case THREEFOUR:
113
0
      *hr = 3;
114
0
      *hs = 4;
115
0
      break;
116
0
    case ONEFOUR:
117
0
      *hr = 1;
118
0
      *hs = 4;
119
0
      break;
120
0
    case ONEEIGHT:
121
0
      *hr = 1;
122
0
      *hs = 8;
123
0
      break;
124
0
    case ONETWO:
125
0
      *hr = 1;
126
0
      *hs = 2;
127
0
      break;
128
0
    default:
129
0
      *hr = 1;
130
0
      *hs = 1;
131
0
      assert(0);
132
0
      break;
133
0
  }
134
0
}
135
136
int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
137
0
                       int cols) {
138
0
  const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
139
0
  if (rows == mi_params->mb_rows && cols == mi_params->mb_cols) {
140
0
    unsigned char *const active_map_8x8 = cpi->active_map.map;
141
0
    const int mi_rows = mi_params->mi_rows;
142
0
    const int mi_cols = mi_params->mi_cols;
143
0
    const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
144
0
    const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
145
0
    cpi->active_map.update = 1;
146
0
    if (new_map_16x16) {
147
0
      int r, c;
148
0
      for (r = 0; r < mi_rows; ++r) {
149
0
        for (c = 0; c < mi_cols; ++c) {
150
0
          active_map_8x8[r * mi_cols + c] =
151
0
              new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
152
0
                  ? AM_SEGMENT_ID_ACTIVE
153
0
                  : AM_SEGMENT_ID_INACTIVE;
154
0
        }
155
0
      }
156
0
      cpi->active_map.enabled = 1;
157
0
    } else {
158
0
      cpi->active_map.enabled = 0;
159
0
    }
160
0
    return 0;
161
0
  } else {
162
0
    return -1;
163
0
  }
164
0
}
165
166
int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
167
0
                       int cols) {
168
0
  const CommonModeInfoParams *const mi_params = &cpi->common.mi_params;
169
0
  if (rows == mi_params->mb_rows && cols == mi_params->mb_cols &&
170
0
      new_map_16x16) {
171
0
    unsigned char *const seg_map_8x8 = cpi->enc_seg.map;
172
0
    const int mi_rows = mi_params->mi_rows;
173
0
    const int mi_cols = mi_params->mi_cols;
174
0
    const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
175
0
    const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
176
177
0
    memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
178
0
    if (cpi->active_map.enabled) {
179
0
      int r, c;
180
0
      for (r = 0; r < mi_rows; ++r) {
181
0
        for (c = 0; c < mi_cols; ++c) {
182
          // Cyclic refresh segments are considered active despite not having
183
          // AM_SEGMENT_ID_ACTIVE
184
0
          new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
185
0
              seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
186
0
        }
187
0
      }
188
0
    }
189
0
    return 0;
190
0
  } else {
191
0
    return -1;
192
0
  }
193
0
}
194
195
1
static void initialize_enc(void) {
196
1
  av1_rtcd();
197
1
  aom_dsp_rtcd();
198
1
  aom_scale_rtcd();
199
1
  av1_init_intra_predictors();
200
1
  av1_init_me_luts();
201
1
  av1_rc_init_minq_luts();
202
1
  av1_init_wedge_masks();
203
1
}
204
205
1.26k
void av1_initialize_enc(void) { aom_once(initialize_enc); }
206
207
0
static void update_reference_segmentation_map(AV1_COMP *cpi) {
208
0
  AV1_COMMON *const cm = &cpi->common;
209
0
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
210
0
  MB_MODE_INFO **mi_4x4_ptr = mi_params->mi_grid_base;
211
0
  uint8_t *cache_ptr = cm->cur_frame->seg_map;
212
213
0
  for (int row = 0; row < mi_params->mi_rows; row++) {
214
0
    MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
215
0
    uint8_t *cache = cache_ptr;
216
0
    for (int col = 0; col < mi_params->mi_cols; col++, mi_4x4++, cache++)
217
0
      cache[0] = mi_4x4[0]->segment_id;
218
0
    mi_4x4_ptr += mi_params->mi_stride;
219
0
    cache_ptr += mi_params->mi_cols;
220
0
  }
221
0
}
222
223
9.05k
void av1_new_framerate(AV1_COMP *cpi, double framerate) {
224
9.05k
  cpi->framerate = framerate < 0.1 ? 30 : framerate;
225
9.05k
  av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
226
9.05k
}
227
228
double av1_get_compression_ratio(const AV1_COMMON *const cm,
229
0
                                 size_t encoded_frame_size) {
230
0
  const int upscaled_width = cm->superres_upscaled_width;
231
0
  const int height = cm->height;
232
0
  const int luma_pic_size = upscaled_width * height;
233
0
  const SequenceHeader *const seq_params = cm->seq_params;
234
0
  const BITSTREAM_PROFILE profile = seq_params->profile;
235
0
  const int pic_size_profile_factor =
236
0
      profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
237
0
  encoded_frame_size =
238
0
      (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
239
0
  const size_t uncompressed_frame_size =
240
0
      (luma_pic_size * pic_size_profile_factor) >> 3;
241
0
  return uncompressed_frame_size / (double)encoded_frame_size;
242
0
}
243
244
static void set_tile_info(AV1_COMMON *const cm,
245
15.5k
                          const TileConfig *const tile_cfg) {
246
15.5k
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
247
15.5k
  const SequenceHeader *const seq_params = cm->seq_params;
248
15.5k
  CommonTileParams *const tiles = &cm->tiles;
249
15.5k
  int i, start_sb;
250
251
15.5k
  av1_get_tile_limits(cm);
252
253
  // configure tile columns
254
15.5k
  if (tile_cfg->tile_width_count == 0 || tile_cfg->tile_height_count == 0) {
255
15.5k
    tiles->uniform_spacing = 1;
256
15.5k
    tiles->log2_cols = AOMMAX(tile_cfg->tile_columns, tiles->min_log2_cols);
257
15.5k
    tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols);
258
15.5k
  } else {
259
0
    int mi_cols =
260
0
        ALIGN_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2);
261
0
    int sb_cols = mi_cols >> seq_params->mib_size_log2;
262
0
    int size_sb, j = 0;
263
0
    tiles->uniform_spacing = 0;
264
0
    for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
265
0
      tiles->col_start_sb[i] = start_sb;
266
0
      size_sb = tile_cfg->tile_widths[j++];
267
0
      if (j >= tile_cfg->tile_width_count) j = 0;
268
0
      start_sb += AOMMIN(size_sb, tiles->max_width_sb);
269
0
    }
270
0
    tiles->cols = i;
271
0
    tiles->col_start_sb[i] = sb_cols;
272
0
  }
273
15.5k
  av1_calculate_tile_cols(seq_params, mi_params->mi_rows, mi_params->mi_cols,
274
15.5k
                          tiles);
275
276
  // configure tile rows
277
15.5k
  if (tiles->uniform_spacing) {
278
15.5k
    tiles->log2_rows = AOMMAX(tile_cfg->tile_rows, tiles->min_log2_rows);
279
15.5k
    tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows);
280
15.5k
  } else {
281
0
    int mi_rows =
282
0
        ALIGN_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2);
283
0
    int sb_rows = mi_rows >> seq_params->mib_size_log2;
284
0
    int size_sb, j = 0;
285
0
    for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
286
0
      tiles->row_start_sb[i] = start_sb;
287
0
      size_sb = tile_cfg->tile_heights[j++];
288
0
      if (j >= tile_cfg->tile_height_count) j = 0;
289
0
      start_sb += AOMMIN(size_sb, tiles->max_height_sb);
290
0
    }
291
0
    tiles->rows = i;
292
0
    tiles->row_start_sb[i] = sb_rows;
293
0
  }
294
15.5k
  av1_calculate_tile_rows(seq_params, mi_params->mi_rows, tiles);
295
15.5k
}
296
297
7.79k
void av1_update_frame_size(AV1_COMP *cpi) {
298
7.79k
  AV1_COMMON *const cm = &cpi->common;
299
7.79k
  MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
300
301
  // Setup mi_params here in case we need more mi's.
302
7.79k
  CommonModeInfoParams *const mi_params = &cm->mi_params;
303
7.79k
  mi_params->set_mb_mi(mi_params, cm->width, cm->height, cpi->oxcf.mode,
304
7.79k
                       cpi->sf.part_sf.default_min_partition_size);
305
306
7.79k
  av1_init_macroblockd(cm, xd);
307
308
7.79k
  if (!is_stat_generation_stage(cpi))
309
7.79k
    alloc_context_buffers_ext(cm, &cpi->mbmi_ext_info);
310
311
7.79k
  if (!cpi->ppi->seq_params_locked)
312
7.79k
    set_sb_size(cm->seq_params,
313
7.79k
                av1_select_sb_size(&cpi->oxcf, cm->width, cm->height,
314
7.79k
                                   cpi->svc.number_spatial_layers));
315
316
7.79k
  set_tile_info(cm, &cpi->oxcf.tile_cfg);
317
7.79k
}
318
319
static INLINE int does_level_match(int width, int height, double fps,
320
                                   int lvl_width, int lvl_height,
321
7.79k
                                   double lvl_fps, int lvl_dim_mult) {
322
7.79k
  const int64_t lvl_luma_pels = lvl_width * lvl_height;
323
7.79k
  const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
324
7.79k
  const int64_t luma_pels = width * height;
325
7.79k
  const double display_sample_rate = luma_pels * fps;
326
7.79k
  return luma_pels <= lvl_luma_pels &&
327
7.79k
         display_sample_rate <= lvl_display_sample_rate &&
328
7.79k
         width <= lvl_width * lvl_dim_mult &&
329
7.79k
         height <= lvl_height * lvl_dim_mult;
330
7.79k
}
331
332
static void set_bitstream_level_tier(AV1_PRIMARY *const ppi, int width,
333
7.79k
                                     int height, double init_framerate) {
334
7.79k
  SequenceHeader *const seq_params = &ppi->seq_params;
335
  // TODO(any): This is a placeholder function that only addresses dimensions
336
  // and max display sample rates.
337
  // Need to add checks for max bit rate, max decoded luma sample rate, header
338
  // rate, etc. that are not covered by this function.
339
7.79k
  AV1_LEVEL level = SEQ_LEVEL_MAX;
340
7.79k
  if (does_level_match(width, height, init_framerate, 512, 288, 30.0, 4)) {
341
7.79k
    level = SEQ_LEVEL_2_0;
342
7.79k
  } else if (does_level_match(width, height, init_framerate, 704, 396, 30.0,
343
0
                              4)) {
344
0
    level = SEQ_LEVEL_2_1;
345
0
  } else if (does_level_match(width, height, init_framerate, 1088, 612, 30.0,
346
0
                              4)) {
347
0
    level = SEQ_LEVEL_3_0;
348
0
  } else if (does_level_match(width, height, init_framerate, 1376, 774, 30.0,
349
0
                              4)) {
350
0
    level = SEQ_LEVEL_3_1;
351
0
  } else if (does_level_match(width, height, init_framerate, 2048, 1152, 30.0,
352
0
                              3)) {
353
0
    level = SEQ_LEVEL_4_0;
354
0
  } else if (does_level_match(width, height, init_framerate, 2048, 1152, 60.0,
355
0
                              3)) {
356
0
    level = SEQ_LEVEL_4_1;
357
0
  } else if (does_level_match(width, height, init_framerate, 4096, 2176, 30.0,
358
0
                              2)) {
359
0
    level = SEQ_LEVEL_5_0;
360
0
  } else if (does_level_match(width, height, init_framerate, 4096, 2176, 60.0,
361
0
                              2)) {
362
0
    level = SEQ_LEVEL_5_1;
363
0
  } else if (does_level_match(width, height, init_framerate, 4096, 2176, 120.0,
364
0
                              2)) {
365
0
    level = SEQ_LEVEL_5_2;
366
0
  } else if (does_level_match(width, height, init_framerate, 8192, 4352, 30.0,
367
0
                              2)) {
368
0
    level = SEQ_LEVEL_6_0;
369
0
  } else if (does_level_match(width, height, init_framerate, 8192, 4352, 60.0,
370
0
                              2)) {
371
0
    level = SEQ_LEVEL_6_1;
372
0
  } else if (does_level_match(width, height, init_framerate, 8192, 4352, 120.0,
373
0
                              2)) {
374
0
    level = SEQ_LEVEL_6_2;
375
0
  }
376
377
257k
  for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
378
249k
    seq_params->seq_level_idx[i] = level;
379
    // Set the maximum parameters for bitrate and buffer size for this profile,
380
    // level, and tier
381
249k
    seq_params->op_params[i].bitrate = av1_max_level_bitrate(
382
249k
        seq_params->profile, seq_params->seq_level_idx[i], seq_params->tier[i]);
383
    // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
384
    // check
385
249k
    if (seq_params->op_params[i].bitrate == 0)
386
0
      aom_internal_error(
387
0
          &ppi->error, AOM_CODEC_UNSUP_BITSTREAM,
388
0
          "AV1 does not support this combination of profile, level, and tier.");
389
    // Buffer size in bits/s is bitrate in bits/s * 1 s
390
249k
    seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
391
249k
  }
392
7.79k
}
393
394
void av1_init_seq_coding_tools(AV1_PRIMARY *const ppi,
395
7.79k
                               const AV1EncoderConfig *oxcf, int use_svc) {
396
7.79k
  SequenceHeader *const seq = &ppi->seq_params;
397
7.79k
  const FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg;
398
7.79k
  const ToolCfg *const tool_cfg = &oxcf->tool_cfg;
399
400
7.79k
  seq->still_picture =
401
7.79k
      !tool_cfg->force_video_mode && (oxcf->input_cfg.limit == 1);
402
7.79k
  seq->reduced_still_picture_hdr =
403
7.79k
      seq->still_picture && !tool_cfg->full_still_picture_hdr;
404
7.79k
  seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
405
7.79k
  seq->force_integer_mv = 2;
406
7.79k
  seq->order_hint_info.enable_order_hint = tool_cfg->enable_order_hint;
407
7.79k
  seq->frame_id_numbers_present_flag =
408
7.79k
      !seq->reduced_still_picture_hdr &&
409
7.79k
      !oxcf->tile_cfg.enable_large_scale_tile &&
410
7.79k
      tool_cfg->error_resilient_mode && !use_svc;
411
7.79k
  if (seq->reduced_still_picture_hdr) {
412
7.79k
    seq->order_hint_info.enable_order_hint = 0;
413
7.79k
    seq->force_screen_content_tools = 2;
414
7.79k
    seq->force_integer_mv = 2;
415
7.79k
  }
416
7.79k
  seq->order_hint_info.order_hint_bits_minus_1 =
417
7.79k
      seq->order_hint_info.enable_order_hint
418
7.79k
          ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
419
7.79k
          : -1;
420
421
7.79k
  seq->max_frame_width = frm_dim_cfg->forced_max_frame_width
422
7.79k
                             ? frm_dim_cfg->forced_max_frame_width
423
7.79k
                             : frm_dim_cfg->width;
424
7.79k
  seq->max_frame_height = frm_dim_cfg->forced_max_frame_height
425
7.79k
                              ? frm_dim_cfg->forced_max_frame_height
426
7.79k
                              : frm_dim_cfg->height;
427
7.79k
  seq->num_bits_width =
428
7.79k
      (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
429
7.79k
  seq->num_bits_height =
430
7.79k
      (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
431
7.79k
  assert(seq->num_bits_width <= 16);
432
7.79k
  assert(seq->num_bits_height <= 16);
433
434
7.79k
  seq->frame_id_length = FRAME_ID_LENGTH;
435
7.79k
  seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
436
437
7.79k
  seq->enable_dual_filter = tool_cfg->enable_dual_filter;
438
7.79k
  seq->order_hint_info.enable_dist_wtd_comp =
439
7.79k
      oxcf->comp_type_cfg.enable_dist_wtd_comp;
440
7.79k
  seq->order_hint_info.enable_dist_wtd_comp &=
441
7.79k
      seq->order_hint_info.enable_order_hint;
442
7.79k
  seq->order_hint_info.enable_ref_frame_mvs = tool_cfg->ref_frame_mvs_present;
443
7.79k
  seq->order_hint_info.enable_ref_frame_mvs &=
444
7.79k
      seq->order_hint_info.enable_order_hint;
445
7.79k
  seq->enable_superres = oxcf->superres_cfg.enable_superres;
446
7.79k
  seq->enable_cdef = tool_cfg->cdef_control != CDEF_NONE ? 1 : 0;
447
7.79k
  seq->enable_restoration = tool_cfg->enable_restoration;
448
7.79k
  seq->enable_warped_motion = oxcf->motion_mode_cfg.enable_warped_motion;
449
7.79k
  seq->enable_interintra_compound = tool_cfg->enable_interintra_comp;
450
7.79k
  seq->enable_masked_compound = oxcf->comp_type_cfg.enable_masked_comp;
451
7.79k
  seq->enable_intra_edge_filter = oxcf->intra_mode_cfg.enable_intra_edge_filter;
452
7.79k
  seq->enable_filter_intra = oxcf->intra_mode_cfg.enable_filter_intra;
453
454
7.79k
  set_bitstream_level_tier(ppi, frm_dim_cfg->width, frm_dim_cfg->height,
455
7.79k
                           oxcf->input_cfg.init_framerate);
456
457
7.79k
  if (seq->operating_points_cnt_minus_1 == 0) {
458
7.79k
    seq->operating_point_idc[0] = 0;
459
7.79k
  } else {
460
    // Set operating_point_idc[] such that the i=0 point corresponds to the
461
    // highest quality operating point (all layers), and subsequent
462
    // operarting points (i > 0) are lower quality corresponding to
463
    // skip decoding enhancement  layers (temporal first).
464
0
    int i = 0;
465
0
    assert(seq->operating_points_cnt_minus_1 ==
466
0
           (int)(ppi->number_spatial_layers * ppi->number_temporal_layers - 1));
467
0
    for (unsigned int sl = 0; sl < ppi->number_spatial_layers; sl++) {
468
0
      for (unsigned int tl = 0; tl < ppi->number_temporal_layers; tl++) {
469
0
        seq->operating_point_idc[i] =
470
0
            (~(~0u << (ppi->number_spatial_layers - sl)) << 8) |
471
0
            ~(~0u << (ppi->number_temporal_layers - tl));
472
0
        i++;
473
0
      }
474
0
    }
475
0
  }
476
7.79k
}
477
478
static void init_config_sequence(struct AV1_PRIMARY *ppi,
479
1.26k
                                 AV1EncoderConfig *oxcf) {
480
1.26k
  SequenceHeader *const seq_params = &ppi->seq_params;
481
1.26k
  const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
482
1.26k
  const ColorCfg *const color_cfg = &oxcf->color_cfg;
483
484
1.26k
  ppi->use_svc = 0;
485
1.26k
  ppi->number_spatial_layers = 1;
486
1.26k
  ppi->number_temporal_layers = 1;
487
488
1.26k
  seq_params->profile = oxcf->profile;
489
1.26k
  seq_params->bit_depth = oxcf->tool_cfg.bit_depth;
490
1.26k
  seq_params->use_highbitdepth = oxcf->use_highbitdepth;
491
1.26k
  seq_params->color_primaries = color_cfg->color_primaries;
492
1.26k
  seq_params->transfer_characteristics = color_cfg->transfer_characteristics;
493
1.26k
  seq_params->matrix_coefficients = color_cfg->matrix_coefficients;
494
1.26k
  seq_params->monochrome = oxcf->tool_cfg.enable_monochrome;
495
1.26k
  seq_params->chroma_sample_position = color_cfg->chroma_sample_position;
496
1.26k
  seq_params->color_range = color_cfg->color_range;
497
1.26k
  seq_params->timing_info_present = dec_model_cfg->timing_info_present;
498
1.26k
  seq_params->timing_info.num_units_in_display_tick =
499
1.26k
      dec_model_cfg->timing_info.num_units_in_display_tick;
500
1.26k
  seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale;
501
1.26k
  seq_params->timing_info.equal_picture_interval =
502
1.26k
      dec_model_cfg->timing_info.equal_picture_interval;
503
1.26k
  seq_params->timing_info.num_ticks_per_picture =
504
1.26k
      dec_model_cfg->timing_info.num_ticks_per_picture;
505
506
1.26k
  seq_params->display_model_info_present_flag =
507
1.26k
      dec_model_cfg->display_model_info_present_flag;
508
1.26k
  seq_params->decoder_model_info_present_flag =
509
1.26k
      dec_model_cfg->decoder_model_info_present_flag;
510
1.26k
  if (dec_model_cfg->decoder_model_info_present_flag) {
511
    // set the decoder model parameters in schedule mode
512
0
    seq_params->decoder_model_info.num_units_in_decoding_tick =
513
0
        dec_model_cfg->num_units_in_decoding_tick;
514
0
    ppi->buffer_removal_time_present = 1;
515
0
    av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
516
0
    av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
517
1.26k
  } else if (seq_params->timing_info_present &&
518
1.26k
             seq_params->timing_info.equal_picture_interval &&
519
1.26k
             !seq_params->decoder_model_info_present_flag) {
520
    // set the decoder model parameters in resource availability mode
521
0
    av1_set_resource_availability_parameters(&seq_params->op_params[0]);
522
1.26k
  } else {
523
1.26k
    seq_params->op_params[0].initial_display_delay =
524
1.26k
        10;  // Default value (not signaled)
525
1.26k
  }
526
527
1.26k
  if (seq_params->monochrome) {
528
0
    seq_params->subsampling_x = 1;
529
0
    seq_params->subsampling_y = 1;
530
1.26k
  } else if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
531
1.26k
             seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
532
1.26k
             seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
533
0
    seq_params->subsampling_x = 0;
534
0
    seq_params->subsampling_y = 0;
535
1.26k
  } else {
536
1.26k
    if (seq_params->profile == 0) {
537
1.26k
      seq_params->subsampling_x = 1;
538
1.26k
      seq_params->subsampling_y = 1;
539
1.26k
    } else if (seq_params->profile == 1) {
540
0
      seq_params->subsampling_x = 0;
541
0
      seq_params->subsampling_y = 0;
542
0
    } else {
543
0
      if (seq_params->bit_depth == AOM_BITS_12) {
544
0
        seq_params->subsampling_x = oxcf->input_cfg.chroma_subsampling_x;
545
0
        seq_params->subsampling_y = oxcf->input_cfg.chroma_subsampling_y;
546
0
      } else {
547
0
        seq_params->subsampling_x = 1;
548
0
        seq_params->subsampling_y = 0;
549
0
      }
550
0
    }
551
1.26k
  }
552
1.26k
  av1_change_config_seq(ppi, oxcf, NULL);
553
1.26k
}
554
555
1.26k
static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
556
1.26k
  AV1_COMMON *const cm = &cpi->common;
557
1.26k
  ResizePendingParams *resize_pending_params = &cpi->resize_pending_params;
558
559
1.26k
  cpi->oxcf = *oxcf;
560
1.26k
  cpi->framerate = oxcf->input_cfg.init_framerate;
561
562
1.26k
  cm->width = oxcf->frm_dim_cfg.width;
563
1.26k
  cm->height = oxcf->frm_dim_cfg.height;
564
1.26k
  cpi->is_dropped_frame = false;
565
566
1.26k
  alloc_compressor_data(cpi);
567
568
1.26k
  av1_update_film_grain_parameters(cpi, oxcf);
569
570
  // Single thread case: use counts in common.
571
1.26k
  cpi->td.counts = &cpi->counts;
572
573
  // Set init SVC parameters.
574
1.26k
  cpi->svc.set_ref_frame_config = 0;
575
1.26k
  cpi->svc.non_reference_frame = 0;
576
1.26k
  cpi->svc.number_spatial_layers = 1;
577
1.26k
  cpi->svc.number_temporal_layers = 1;
578
1.26k
  cm->spatial_layer_id = 0;
579
1.26k
  cm->temporal_layer_id = 0;
580
581
  // change includes all joint functionality
582
1.26k
  av1_change_config(cpi, oxcf, false);
583
584
1.26k
  cpi->ref_frame_flags = 0;
585
586
  // Reset resize pending flags
587
1.26k
  resize_pending_params->width = 0;
588
1.26k
  resize_pending_params->height = 0;
589
590
1.26k
  init_buffer_indices(&cpi->force_intpel_info, cm->remapped_ref_idx);
591
592
1.26k
  av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
593
1.26k
}
594
595
void av1_change_config_seq(struct AV1_PRIMARY *ppi,
596
                           const AV1EncoderConfig *oxcf,
597
7.79k
                           bool *is_sb_size_changed) {
598
7.79k
  SequenceHeader *const seq_params = &ppi->seq_params;
599
7.79k
  const FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg;
600
7.79k
  const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg;
601
7.79k
  const ColorCfg *const color_cfg = &oxcf->color_cfg;
602
603
7.79k
  if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
604
7.79k
  seq_params->bit_depth = oxcf->tool_cfg.bit_depth;
605
7.79k
  seq_params->color_primaries = color_cfg->color_primaries;
606
7.79k
  seq_params->transfer_characteristics = color_cfg->transfer_characteristics;
607
7.79k
  seq_params->matrix_coefficients = color_cfg->matrix_coefficients;
608
7.79k
  seq_params->monochrome = oxcf->tool_cfg.enable_monochrome;
609
7.79k
  seq_params->chroma_sample_position = color_cfg->chroma_sample_position;
610
7.79k
  seq_params->color_range = color_cfg->color_range;
611
612
7.79k
  assert(IMPLIES(seq_params->profile <= PROFILE_1,
613
7.79k
                 seq_params->bit_depth <= AOM_BITS_10));
614
615
7.79k
  seq_params->timing_info_present = dec_model_cfg->timing_info_present;
616
7.79k
  seq_params->timing_info.num_units_in_display_tick =
617
7.79k
      dec_model_cfg->timing_info.num_units_in_display_tick;
618
7.79k
  seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale;
619
7.79k
  seq_params->timing_info.equal_picture_interval =
620
7.79k
      dec_model_cfg->timing_info.equal_picture_interval;
621
7.79k
  seq_params->timing_info.num_ticks_per_picture =
622
7.79k
      dec_model_cfg->timing_info.num_ticks_per_picture;
623
624
7.79k
  seq_params->display_model_info_present_flag =
625
7.79k
      dec_model_cfg->display_model_info_present_flag;
626
7.79k
  seq_params->decoder_model_info_present_flag =
627
7.79k
      dec_model_cfg->decoder_model_info_present_flag;
628
7.79k
  if (dec_model_cfg->decoder_model_info_present_flag) {
629
    // set the decoder model parameters in schedule mode
630
0
    seq_params->decoder_model_info.num_units_in_decoding_tick =
631
0
        dec_model_cfg->num_units_in_decoding_tick;
632
0
    ppi->buffer_removal_time_present = 1;
633
0
    av1_set_aom_dec_model_info(&seq_params->decoder_model_info);
634
0
    av1_set_dec_model_op_parameters(&seq_params->op_params[0]);
635
7.79k
  } else if (seq_params->timing_info_present &&
636
7.79k
             seq_params->timing_info.equal_picture_interval &&
637
7.79k
             !seq_params->decoder_model_info_present_flag) {
638
    // set the decoder model parameters in resource availability mode
639
0
    av1_set_resource_availability_parameters(&seq_params->op_params[0]);
640
7.79k
  } else {
641
7.79k
    seq_params->op_params[0].initial_display_delay =
642
7.79k
        10;  // Default value (not signaled)
643
7.79k
  }
644
645
7.79k
  av1_update_film_grain_parameters_seq(ppi, oxcf);
646
647
7.79k
  int sb_size = seq_params->sb_size;
648
  // Superblock size should not be updated after the first key frame.
649
7.79k
  if (!ppi->seq_params_locked) {
650
7.79k
    set_sb_size(seq_params, av1_select_sb_size(oxcf, frm_dim_cfg->width,
651
7.79k
                                               frm_dim_cfg->height,
652
7.79k
                                               ppi->number_spatial_layers));
653
257k
    for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
654
249k
      seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
655
7.79k
  }
656
7.79k
  if (is_sb_size_changed != NULL && sb_size != seq_params->sb_size)
657
1.26k
    *is_sb_size_changed = true;
658
659
  // Init sequence level coding tools
660
  // This should not be called after the first key frame.
661
7.79k
  if (!ppi->seq_params_locked) {
662
7.79k
    seq_params->operating_points_cnt_minus_1 =
663
7.79k
        (ppi->number_spatial_layers > 1 || ppi->number_temporal_layers > 1)
664
7.79k
            ? ppi->number_spatial_layers * ppi->number_temporal_layers - 1
665
7.79k
            : 0;
666
7.79k
    av1_init_seq_coding_tools(ppi, oxcf, ppi->use_svc);
667
7.79k
  }
668
7.79k
  seq_params->timing_info_present &= !seq_params->reduced_still_picture_hdr;
669
670
7.79k
#if CONFIG_AV1_HIGHBITDEPTH
671
7.79k
  highbd_set_var_fns(ppi);
672
7.79k
#endif
673
674
7.79k
  set_primary_rc_buffer_sizes(oxcf, ppi);
675
7.79k
}
676
677
void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf,
678
7.79k
                       bool is_sb_size_changed) {
679
7.79k
  AV1_COMMON *const cm = &cpi->common;
680
7.79k
  SequenceHeader *const seq_params = cm->seq_params;
681
7.79k
  RATE_CONTROL *const rc = &cpi->rc;
682
7.79k
  PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
683
7.79k
  MACROBLOCK *const x = &cpi->td.mb;
684
7.79k
  AV1LevelParams *const level_params = &cpi->ppi->level_params;
685
7.79k
  InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
686
7.79k
  RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame;
687
7.79k
  const FrameDimensionCfg *const frm_dim_cfg = &cpi->oxcf.frm_dim_cfg;
688
7.79k
  const RateControlCfg *const rc_cfg = &oxcf->rc_cfg;
689
690
  // in case of LAP, lag in frames is set according to number of lap buffers
691
  // calculated at init time. This stores and restores LAP's lag in frames to
692
  // prevent override by new cfg.
693
7.79k
  int lap_lag_in_frames = -1;
694
7.79k
  if (cpi->ppi->lap_enabled && cpi->compressor_stage == LAP_STAGE) {
695
0
    lap_lag_in_frames = cpi->oxcf.gf_cfg.lag_in_frames;
696
0
  }
697
698
7.79k
  av1_update_film_grain_parameters(cpi, oxcf);
699
700
7.79k
  cpi->oxcf = *oxcf;
701
  // When user provides superres_mode = AOM_SUPERRES_AUTO, we still initialize
702
  // superres mode for current encoding = AOM_SUPERRES_NONE. This is to ensure
703
  // that any analysis (e.g. TPL) happening outside the main encoding loop still
704
  // happens at full resolution.
705
  // This value will later be set appropriately just before main encoding loop.
706
7.79k
  cpi->superres_mode = oxcf->superres_cfg.superres_mode == AOM_SUPERRES_AUTO
707
7.79k
                           ? AOM_SUPERRES_NONE
708
7.79k
                           : oxcf->superres_cfg.superres_mode;  // default
709
7.79k
  x->e_mbd.bd = (int)seq_params->bit_depth;
710
7.79k
  x->e_mbd.global_motion = cm->global_motion;
711
712
7.79k
  memcpy(level_params->target_seq_level_idx, cpi->oxcf.target_seq_level_idx,
713
7.79k
         sizeof(level_params->target_seq_level_idx));
714
7.79k
  level_params->keep_level_stats = 0;
715
257k
  for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
716
249k
    if (level_params->target_seq_level_idx[i] <= SEQ_LEVELS) {
717
0
      level_params->keep_level_stats |= 1u << i;
718
0
      if (!level_params->level_info[i]) {
719
0
        CHECK_MEM_ERROR(cm, level_params->level_info[i],
720
0
                        aom_calloc(1, sizeof(*level_params->level_info[i])));
721
0
      }
722
0
    }
723
249k
  }
724
725
  // TODO(huisu@): level targeting currently only works for the 0th operating
726
  // point, so scalable coding is not supported yet.
727
7.79k
  if (level_params->target_seq_level_idx[0] < SEQ_LEVELS) {
728
    // Adjust encoder config in order to meet target level.
729
0
    config_target_level(cpi, level_params->target_seq_level_idx[0],
730
0
                        seq_params->tier[0]);
731
0
  }
732
733
7.79k
  if (has_no_stats_stage(cpi) && (rc_cfg->mode == AOM_Q)) {
734
7.79k
    p_rc->baseline_gf_interval = FIXED_GF_INTERVAL;
735
7.79k
  } else {
736
0
    p_rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
737
0
  }
738
739
7.79k
  refresh_frame->golden_frame = false;
740
7.79k
  refresh_frame->bwd_ref_frame = false;
741
742
7.79k
  cm->features.refresh_frame_context =
743
7.79k
      (oxcf->tool_cfg.frame_parallel_decoding_mode)
744
7.79k
          ? REFRESH_FRAME_CONTEXT_DISABLED
745
7.79k
          : REFRESH_FRAME_CONTEXT_BACKWARD;
746
7.79k
  if (oxcf->tile_cfg.enable_large_scale_tile)
747
0
    cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
748
749
7.79k
  if (x->palette_buffer == NULL) {
750
1.26k
    CHECK_MEM_ERROR(cm, x->palette_buffer,
751
1.26k
                    aom_memalign(16, sizeof(*x->palette_buffer)));
752
1.26k
  }
753
754
7.79k
  if (x->tmp_conv_dst == NULL) {
755
1.26k
    CHECK_MEM_ERROR(
756
1.26k
        cm, x->tmp_conv_dst,
757
1.26k
        aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
758
1.26k
    x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
759
1.26k
  }
760
  // The buffers 'tmp_pred_bufs[]' and 'comp_rd_buffer' are used in inter frames
761
  // to store intermediate inter mode prediction results and are not required
762
  // for allintra encoding mode. Hence, the memory allocations for these buffers
763
  // are avoided for allintra encoding mode.
764
7.79k
  if (cpi->oxcf.kf_cfg.key_freq_max != 0) {
765
0
    if (x->comp_rd_buffer.pred0 == NULL)
766
0
      alloc_compound_type_rd_buffers(cm->error, &x->comp_rd_buffer);
767
768
0
    for (int i = 0; i < 2; ++i) {
769
0
      if (x->tmp_pred_bufs[i] == NULL) {
770
0
        CHECK_MEM_ERROR(cm, x->tmp_pred_bufs[i],
771
0
                        aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
772
0
                                             sizeof(*x->tmp_pred_bufs[i])));
773
0
        x->e_mbd.tmp_obmc_bufs[i] = x->tmp_pred_bufs[i];
774
0
      }
775
0
    }
776
0
  }
777
778
7.79k
  av1_reset_segment_features(cm);
779
780
7.79k
  av1_set_high_precision_mv(cpi, 1, 0);
781
782
  // Under a configuration change, where maximum_buffer_size may change,
783
  // keep buffer level clipped to the maximum allowed buffer size.
784
7.79k
  p_rc->bits_off_target =
785
7.79k
      AOMMIN(p_rc->bits_off_target, p_rc->maximum_buffer_size);
786
7.79k
  p_rc->buffer_level = AOMMIN(p_rc->buffer_level, p_rc->maximum_buffer_size);
787
788
  // Set up frame rate and related parameters rate control values.
789
7.79k
  av1_new_framerate(cpi, cpi->framerate);
790
791
  // Set absolute upper and lower quality limits
792
7.79k
  rc->worst_quality = rc_cfg->worst_allowed_q;
793
7.79k
  rc->best_quality = rc_cfg->best_allowed_q;
794
795
7.79k
  cm->features.interp_filter =
796
7.79k
      oxcf->tile_cfg.enable_large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
797
7.79k
  cm->features.switchable_motion_mode = 1;
798
799
7.79k
  if (frm_dim_cfg->render_width > 0 && frm_dim_cfg->render_height > 0) {
800
0
    cm->render_width = frm_dim_cfg->render_width;
801
0
    cm->render_height = frm_dim_cfg->render_height;
802
7.79k
  } else {
803
7.79k
    cm->render_width = frm_dim_cfg->width;
804
7.79k
    cm->render_height = frm_dim_cfg->height;
805
7.79k
  }
806
7.79k
  cm->width = frm_dim_cfg->width;
807
7.79k
  cm->height = frm_dim_cfg->height;
808
809
7.79k
  if (initial_dimensions->width || is_sb_size_changed) {
810
1.26k
    if (cm->width > initial_dimensions->width ||
811
1.26k
        cm->height > initial_dimensions->height || is_sb_size_changed) {
812
1.26k
      av1_free_context_buffers(cm);
813
1.26k
      av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
814
1.26k
      av1_free_sms_tree(&cpi->td);
815
1.26k
      av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
816
1.26k
      cpi->td.firstpass_ctx = NULL;
817
1.26k
      alloc_compressor_data(cpi);
818
1.26k
      realloc_segmentation_maps(cpi);
819
1.26k
      initial_dimensions->width = initial_dimensions->height = 0;
820
1.26k
    }
821
1.26k
  }
822
7.79k
  av1_update_frame_size(cpi);
823
824
7.79k
  rc->is_src_frame_alt_ref = 0;
825
826
7.79k
  set_tile_info(cm, &cpi->oxcf.tile_cfg);
827
828
7.79k
  if (!cpi->svc.set_ref_frame_config)
829
7.79k
    cpi->ext_flags.refresh_frame.update_pending = 0;
830
7.79k
  cpi->ext_flags.refresh_frame_context_pending = 0;
831
832
7.79k
  if (cpi->ppi->use_svc)
833
0
    av1_update_layer_context_change_config(cpi, rc_cfg->target_bandwidth);
834
835
7.79k
  check_reset_rc_flag(cpi);
836
837
  // restore the value of lag_in_frame for LAP stage.
838
7.79k
  if (lap_lag_in_frames != -1) {
839
0
    cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames;
840
0
  }
841
7.79k
}
842
843
static INLINE void init_frame_info(FRAME_INFO *frame_info,
844
1.26k
                                   const AV1_COMMON *const cm) {
845
1.26k
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
846
1.26k
  const SequenceHeader *const seq_params = cm->seq_params;
847
1.26k
  frame_info->frame_width = cm->width;
848
1.26k
  frame_info->frame_height = cm->height;
849
1.26k
  frame_info->mi_cols = mi_params->mi_cols;
850
1.26k
  frame_info->mi_rows = mi_params->mi_rows;
851
1.26k
  frame_info->mb_cols = mi_params->mb_cols;
852
1.26k
  frame_info->mb_rows = mi_params->mb_rows;
853
1.26k
  frame_info->num_mbs = mi_params->MBs;
854
1.26k
  frame_info->bit_depth = seq_params->bit_depth;
855
1.26k
  frame_info->subsampling_x = seq_params->subsampling_x;
856
1.26k
  frame_info->subsampling_y = seq_params->subsampling_y;
857
1.26k
}
858
859
1.26k
static INLINE void init_frame_index_set(FRAME_INDEX_SET *frame_index_set) {
860
1.26k
  frame_index_set->show_frame_count = 0;
861
1.26k
}
862
863
static INLINE void update_frame_index_set(FRAME_INDEX_SET *frame_index_set,
864
1.26k
                                          int is_show_frame) {
865
1.26k
  if (is_show_frame) {
866
1.26k
    frame_index_set->show_frame_count++;
867
1.26k
  }
868
1.26k
}
869
870
AV1_PRIMARY *av1_create_primary_compressor(
871
    struct aom_codec_pkt_list *pkt_list_head, int num_lap_buffers,
872
1.26k
    AV1EncoderConfig *oxcf) {
873
1.26k
  AV1_PRIMARY *volatile const ppi = aom_memalign(32, sizeof(AV1_PRIMARY));
874
1.26k
  if (!ppi) return NULL;
875
1.26k
  av1_zero(*ppi);
876
877
  // The jmp_buf is valid only for the duration of the function that calls
878
  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
879
  // before it returns.
880
1.26k
  if (setjmp(ppi->error.jmp)) {
881
0
    ppi->error.setjmp = 0;
882
0
    av1_remove_primary_compressor(ppi);
883
0
    return 0;
884
0
  }
885
1.26k
  ppi->error.setjmp = 1;
886
887
1.26k
  ppi->seq_params_locked = 0;
888
1.26k
  ppi->lap_enabled = num_lap_buffers > 0;
889
1.26k
  ppi->output_pkt_list = pkt_list_head;
890
1.26k
  ppi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
891
1.26k
  ppi->frames_left = oxcf->input_cfg.limit;
892
#if CONFIG_FRAME_PARALLEL_ENCODE
893
  ppi->num_fp_contexts = 1;
894
#endif
895
896
1.26k
  init_config_sequence(ppi, oxcf);
897
898
#if CONFIG_ENTROPY_STATS
899
  av1_zero(ppi->aggregate_fc);
900
#endif  // CONFIG_ENTROPY_STATS
901
902
1.26k
  av1_primary_rc_init(oxcf, &ppi->p_rc);
903
904
  // For two pass and lag_in_frames > 33 in LAP.
905
1.26k
  ppi->p_rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_2;
906
1.26k
  if (ppi->lap_enabled) {
907
0
    if ((num_lap_buffers <
908
0
         (MAX_GF_LENGTH_LAP + SCENE_CUT_KEY_TEST_INTERVAL + 1)) &&
909
0
        num_lap_buffers >= (MAX_GF_LENGTH_LAP + 3)) {
910
      /*
911
       * For lag in frames >= 19 and <33, enable scenecut
912
       * with limited future frame prediction.
913
       */
914
0
      ppi->p_rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_1;
915
0
    } else if (num_lap_buffers < (MAX_GF_LENGTH_LAP + 3)) {
916
      // Disable scenecut when lag_in_frames < 19.
917
0
      ppi->p_rc.enable_scenecut_detection = DISABLE_SCENECUT;
918
0
    }
919
0
  }
920
921
1.26k
#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
922
27.7k
  ppi->fn_ptr[BT].sdf = SDF;                                    \
923
27.7k
  ppi->fn_ptr[BT].sdaf = SDAF;                                  \
924
27.7k
  ppi->fn_ptr[BT].vf = VF;                                      \
925
27.7k
  ppi->fn_ptr[BT].svf = SVF;                                    \
926
27.7k
  ppi->fn_ptr[BT].svaf = SVAF;                                  \
927
27.7k
  ppi->fn_ptr[BT].sdx4df = SDX4DF;                              \
928
27.7k
  ppi->fn_ptr[BT].jsdaf = JSDAF;                                \
929
27.7k
  ppi->fn_ptr[BT].jsvaf = JSVAF;
930
931
// Realtime mode doesn't use 4x rectangular blocks.
932
1.26k
#if !CONFIG_REALTIME_ONLY
933
1.26k
  BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
934
1.26k
      aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
935
1.26k
      aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
936
1.26k
      aom_dist_wtd_sub_pixel_avg_variance4x16)
937
938
1.26k
  BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
939
1.26k
      aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
940
1.26k
      aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
941
1.26k
      aom_dist_wtd_sub_pixel_avg_variance16x4)
942
943
1.26k
  BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
944
1.26k
      aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
945
1.26k
      aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
946
1.26k
      aom_dist_wtd_sub_pixel_avg_variance8x32)
947
948
1.26k
  BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
949
1.26k
      aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
950
1.26k
      aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
951
1.26k
      aom_dist_wtd_sub_pixel_avg_variance32x8)
952
953
1.26k
  BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
954
1.26k
      aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
955
1.26k
      aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
956
1.26k
      aom_dist_wtd_sub_pixel_avg_variance16x64)
957
958
1.26k
  BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
959
1.26k
      aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
960
1.26k
      aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
961
1.26k
      aom_dist_wtd_sub_pixel_avg_variance64x16)
962
1.26k
#endif  // !CONFIG_REALTIME_ONLY
963
964
1.26k
  BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
965
1.26k
      aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
966
1.26k
      aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
967
1.26k
      aom_dist_wtd_sub_pixel_avg_variance128x128)
968
969
1.26k
  BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
970
1.26k
      aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
971
1.26k
      aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
972
1.26k
      aom_dist_wtd_sub_pixel_avg_variance128x64)
973
974
1.26k
  BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
975
1.26k
      aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
976
1.26k
      aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
977
1.26k
      aom_dist_wtd_sub_pixel_avg_variance64x128)
978
979
1.26k
  BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
980
1.26k
      aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
981
1.26k
      aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
982
1.26k
      aom_dist_wtd_sub_pixel_avg_variance32x16)
983
984
1.26k
  BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
985
1.26k
      aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
986
1.26k
      aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
987
1.26k
      aom_dist_wtd_sub_pixel_avg_variance16x32)
988
989
1.26k
  BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
990
1.26k
      aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
991
1.26k
      aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
992
1.26k
      aom_dist_wtd_sub_pixel_avg_variance64x32)
993
994
1.26k
  BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
995
1.26k
      aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
996
1.26k
      aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
997
1.26k
      aom_dist_wtd_sub_pixel_avg_variance32x64)
998
999
1.26k
  BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
1000
1.26k
      aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
1001
1.26k
      aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
1002
1.26k
      aom_dist_wtd_sub_pixel_avg_variance32x32)
1003
1004
1.26k
  BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
1005
1.26k
      aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
1006
1.26k
      aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
1007
1.26k
      aom_dist_wtd_sub_pixel_avg_variance64x64)
1008
1009
1.26k
  BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
1010
1.26k
      aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
1011
1.26k
      aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
1012
1.26k
      aom_dist_wtd_sub_pixel_avg_variance16x16)
1013
1014
1.26k
  BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
1015
1.26k
      aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
1016
1.26k
      aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
1017
1.26k
      aom_dist_wtd_sub_pixel_avg_variance16x8)
1018
1019
1.26k
  BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
1020
1.26k
      aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
1021
1.26k
      aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
1022
1.26k
      aom_dist_wtd_sub_pixel_avg_variance8x16)
1023
1024
1.26k
  BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
1025
1.26k
      aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
1026
1.26k
      aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
1027
1028
1.26k
  BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
1029
1.26k
      aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
1030
1.26k
      aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
1031
1032
1.26k
  BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
1033
1.26k
      aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
1034
1.26k
      aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
1035
1036
1.26k
  BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
1037
1.26k
      aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
1038
1.26k
      aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
1039
1040
1.26k
#if !CONFIG_REALTIME_ONLY
1041
1.26k
#define OBFP(BT, OSDF, OVF, OSVF) \
1042
27.7k
  ppi->fn_ptr[BT].osdf = OSDF;    \
1043
27.7k
  ppi->fn_ptr[BT].ovf = OVF;      \
1044
27.7k
  ppi->fn_ptr[BT].osvf = OSVF;
1045
1046
1.26k
  OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
1047
1.26k
       aom_obmc_sub_pixel_variance128x128)
1048
1.26k
  OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
1049
1.26k
       aom_obmc_sub_pixel_variance128x64)
1050
1.26k
  OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
1051
1.26k
       aom_obmc_sub_pixel_variance64x128)
1052
1.26k
  OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
1053
1.26k
       aom_obmc_sub_pixel_variance64x64)
1054
1.26k
  OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
1055
1.26k
       aom_obmc_sub_pixel_variance64x32)
1056
1.26k
  OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
1057
1.26k
       aom_obmc_sub_pixel_variance32x64)
1058
1.26k
  OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
1059
1.26k
       aom_obmc_sub_pixel_variance32x32)
1060
1.26k
  OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
1061
1.26k
       aom_obmc_sub_pixel_variance32x16)
1062
1.26k
  OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
1063
1.26k
       aom_obmc_sub_pixel_variance16x32)
1064
1.26k
  OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
1065
1.26k
       aom_obmc_sub_pixel_variance16x16)
1066
1.26k
  OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
1067
1.26k
       aom_obmc_sub_pixel_variance16x8)
1068
1.26k
  OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
1069
1.26k
       aom_obmc_sub_pixel_variance8x16)
1070
1.26k
  OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
1071
1.26k
       aom_obmc_sub_pixel_variance8x8)
1072
1.26k
  OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
1073
1.26k
       aom_obmc_sub_pixel_variance4x8)
1074
1.26k
  OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
1075
1.26k
       aom_obmc_sub_pixel_variance8x4)
1076
1.26k
  OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
1077
1.26k
       aom_obmc_sub_pixel_variance4x4)
1078
1.26k
  OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
1079
1.26k
       aom_obmc_sub_pixel_variance4x16)
1080
1.26k
  OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
1081
1.26k
       aom_obmc_sub_pixel_variance16x4)
1082
1.26k
  OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
1083
1.26k
       aom_obmc_sub_pixel_variance8x32)
1084
1.26k
  OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
1085
1.26k
       aom_obmc_sub_pixel_variance32x8)
1086
1.26k
  OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
1087
1.26k
       aom_obmc_sub_pixel_variance16x64)
1088
1.26k
  OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
1089
1.26k
       aom_obmc_sub_pixel_variance64x16)
1090
1.26k
#endif  // !CONFIG_REALTIME_ONLY
1091
1092
1.26k
#define MBFP(BT, MCSDF, MCSVF)  \
1093
27.7k
  ppi->fn_ptr[BT].msdf = MCSDF; \
1094
27.7k
  ppi->fn_ptr[BT].msvf = MCSVF;
1095
1096
1.26k
  MBFP(BLOCK_128X128, aom_masked_sad128x128,
1097
1.26k
       aom_masked_sub_pixel_variance128x128)
1098
1.26k
  MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
1099
1.26k
  MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
1100
1.26k
  MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
1101
1.26k
  MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
1102
1.26k
  MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
1103
1.26k
  MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
1104
1.26k
  MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
1105
1.26k
  MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
1106
1.26k
  MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
1107
1.26k
  MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
1108
1.26k
  MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
1109
1.26k
  MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
1110
1.26k
  MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
1111
1.26k
  MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
1112
1.26k
  MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
1113
1114
1.26k
#if !CONFIG_REALTIME_ONLY
1115
1.26k
  MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
1116
1.26k
  MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
1117
1.26k
  MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
1118
1.26k
  MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
1119
1.26k
  MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
1120
1.26k
  MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
1121
1.26k
#endif
1122
1123
1.26k
#define SDSFP(BT, SDSF, SDSX4DF) \
1124
23.9k
  ppi->fn_ptr[BT].sdsf = SDSF;   \
1125
23.9k
  ppi->fn_ptr[BT].sdsx4df = SDSX4DF;
1126
1127
1.26k
  SDSFP(BLOCK_128X128, aom_sad_skip_128x128, aom_sad_skip_128x128x4d);
1128
1.26k
  SDSFP(BLOCK_128X64, aom_sad_skip_128x64, aom_sad_skip_128x64x4d);
1129
1.26k
  SDSFP(BLOCK_64X128, aom_sad_skip_64x128, aom_sad_skip_64x128x4d);
1130
1.26k
  SDSFP(BLOCK_64X64, aom_sad_skip_64x64, aom_sad_skip_64x64x4d);
1131
1.26k
  SDSFP(BLOCK_64X32, aom_sad_skip_64x32, aom_sad_skip_64x32x4d);
1132
1133
1.26k
  SDSFP(BLOCK_32X64, aom_sad_skip_32x64, aom_sad_skip_32x64x4d);
1134
1.26k
  SDSFP(BLOCK_32X32, aom_sad_skip_32x32, aom_sad_skip_32x32x4d);
1135
1.26k
  SDSFP(BLOCK_32X16, aom_sad_skip_32x16, aom_sad_skip_32x16x4d);
1136
1137
1.26k
  SDSFP(BLOCK_16X32, aom_sad_skip_16x32, aom_sad_skip_16x32x4d);
1138
1.26k
  SDSFP(BLOCK_16X16, aom_sad_skip_16x16, aom_sad_skip_16x16x4d);
1139
1.26k
  SDSFP(BLOCK_16X8, aom_sad_skip_16x8, aom_sad_skip_16x8x4d);
1140
1.26k
  SDSFP(BLOCK_8X16, aom_sad_skip_8x16, aom_sad_skip_8x16x4d);
1141
1.26k
  SDSFP(BLOCK_8X8, aom_sad_skip_8x8, aom_sad_skip_8x8x4d);
1142
1143
1.26k
  SDSFP(BLOCK_4X8, aom_sad_skip_4x8, aom_sad_skip_4x8x4d);
1144
1145
1.26k
#if !CONFIG_REALTIME_ONLY
1146
1.26k
  SDSFP(BLOCK_64X16, aom_sad_skip_64x16, aom_sad_skip_64x16x4d);
1147
1.26k
  SDSFP(BLOCK_16X64, aom_sad_skip_16x64, aom_sad_skip_16x64x4d);
1148
1.26k
  SDSFP(BLOCK_32X8, aom_sad_skip_32x8, aom_sad_skip_32x8x4d);
1149
1.26k
  SDSFP(BLOCK_8X32, aom_sad_skip_8x32, aom_sad_skip_8x32x4d);
1150
1.26k
  SDSFP(BLOCK_4X16, aom_sad_skip_4x16, aom_sad_skip_4x16x4d);
1151
1.26k
#endif
1152
1.26k
#undef SDSFP
1153
1154
1.26k
#if CONFIG_AV1_HIGHBITDEPTH
1155
1.26k
  highbd_set_var_fns(ppi);
1156
1.26k
#endif
1157
1158
1.26k
  {
1159
    // As cm->mi_params is a part of the frame level context (cpi), it is
1160
    // unavailable at this point. mi_params is created as a local temporary
1161
    // variable, to be passed into the functions used for allocating tpl
1162
    // buffers. The values in this variable are populated according to initial
1163
    // width and height of the frame.
1164
1.26k
    CommonModeInfoParams mi_params;
1165
1.26k
    enc_set_mb_mi(&mi_params, oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,
1166
1.26k
                  oxcf->mode, BLOCK_4X4);
1167
1168
1.26k
    const int bsize = BLOCK_16X16;
1169
1.26k
    const int w = mi_size_wide[bsize];
1170
1.26k
    const int h = mi_size_high[bsize];
1171
1.26k
    const int num_cols = (mi_params.mi_cols + w - 1) / w;
1172
1.26k
    const int num_rows = (mi_params.mi_rows + h - 1) / h;
1173
1.26k
    AOM_CHECK_MEM_ERROR(&ppi->error, ppi->tpl_rdmult_scaling_factors,
1174
1.26k
                        aom_calloc(num_rows * num_cols,
1175
1.26k
                                   sizeof(*ppi->tpl_rdmult_scaling_factors)));
1176
1.26k
    AOM_CHECK_MEM_ERROR(
1177
1.26k
        &ppi->error, ppi->tpl_sb_rdmult_scaling_factors,
1178
1.26k
        aom_calloc(num_rows * num_cols,
1179
1.26k
                   sizeof(*ppi->tpl_sb_rdmult_scaling_factors)));
1180
1181
1.26k
#if !CONFIG_REALTIME_ONLY
1182
1.26k
    if (oxcf->pass != AOM_RC_FIRST_PASS) {
1183
1.26k
      av1_setup_tpl_buffers(ppi, &mi_params, oxcf->frm_dim_cfg.width,
1184
1.26k
                            oxcf->frm_dim_cfg.height, 0,
1185
1.26k
                            oxcf->gf_cfg.lag_in_frames);
1186
1.26k
    }
1187
1.26k
#endif
1188
1189
#if CONFIG_INTERNAL_STATS
1190
    ppi->b_calculate_blockiness = 1;
1191
    ppi->b_calculate_consistency = 1;
1192
1193
    for (int i = 0; i <= STAT_ALL; i++) {
1194
      ppi->psnr[0].stat[i] = 0;
1195
      ppi->psnr[1].stat[i] = 0;
1196
1197
      ppi->fastssim.stat[i] = 0;
1198
      ppi->psnrhvs.stat[i] = 0;
1199
    }
1200
1201
    ppi->psnr[0].worst = 100.0;
1202
    ppi->psnr[1].worst = 100.0;
1203
    ppi->worst_ssim = 100.0;
1204
    ppi->worst_ssim_hbd = 100.0;
1205
1206
    ppi->count[0] = 0;
1207
    ppi->count[1] = 0;
1208
    ppi->total_bytes = 0;
1209
1210
    if (ppi->b_calculate_psnr) {
1211
      ppi->total_sq_error[0] = 0;
1212
      ppi->total_samples[0] = 0;
1213
      ppi->total_sq_error[1] = 0;
1214
      ppi->total_samples[1] = 0;
1215
      ppi->total_recode_hits = 0;
1216
      ppi->summed_quality = 0;
1217
      ppi->summed_weights = 0;
1218
      ppi->summed_quality_hbd = 0;
1219
      ppi->summed_weights_hbd = 0;
1220
    }
1221
1222
    ppi->fastssim.worst = 100.0;
1223
    ppi->psnrhvs.worst = 100.0;
1224
1225
    if (ppi->b_calculate_blockiness) {
1226
      ppi->total_blockiness = 0;
1227
      ppi->worst_blockiness = 0.0;
1228
    }
1229
1230
    ppi->total_inconsistency = 0;
1231
    ppi->worst_consistency = 100.0;
1232
    if (ppi->b_calculate_consistency) {
1233
      AOM_CHECK_MEM_ERROR(&ppi->error, ppi->ssim_vars,
1234
                          aom_malloc(sizeof(*ppi->ssim_vars) * 4 *
1235
                                     mi_params.mi_rows * mi_params.mi_cols));
1236
    }
1237
#endif
1238
1.26k
  }
1239
1240
1.26k
  ppi->error.setjmp = 0;
1241
1.26k
  return ppi;
1242
1.26k
}
1243
1244
AV1_COMP *av1_create_compressor(AV1_PRIMARY *ppi, AV1EncoderConfig *oxcf,
1245
                                BufferPool *const pool, COMPRESSOR_STAGE stage,
1246
1.26k
                                int lap_lag_in_frames) {
1247
1.26k
  AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
1248
1.26k
  AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
1249
1250
1.26k
  if (!cm) return NULL;
1251
1252
1.26k
  av1_zero(*cpi);
1253
1254
1.26k
  cpi->ppi = ppi;
1255
1.26k
  cm->seq_params = &ppi->seq_params;
1256
#if CONFIG_FRAME_PARALLEL_ENCODE
1257
  cm->error =
1258
      (struct aom_internal_error_info *)aom_calloc(1, sizeof(*cm->error));
1259
#else
1260
1.26k
  cm->error = &ppi->error;
1261
1.26k
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
1262
1263
  // The jmp_buf is valid only for the duration of the function that calls
1264
  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
1265
  // before it returns.
1266
1.26k
  if (setjmp(cm->error->jmp)) {
1267
0
    cm->error->setjmp = 0;
1268
0
    av1_remove_compressor(cpi);
1269
0
    return 0;
1270
0
  }
1271
1272
1.26k
  cm->error->setjmp = 1;
1273
1.26k
  cpi->compressor_stage = stage;
1274
1275
#if CONFIG_FRAME_PARALLEL_ENCODE
1276
  cpi->do_frame_data_update = true;
1277
#endif
1278
1279
1.26k
  CommonModeInfoParams *const mi_params = &cm->mi_params;
1280
1.26k
  mi_params->free_mi = enc_free_mi;
1281
1.26k
  mi_params->setup_mi = enc_setup_mi;
1282
1.26k
  mi_params->set_mb_mi =
1283
1.26k
      (oxcf->pass == AOM_RC_FIRST_PASS || cpi->compressor_stage == LAP_STAGE)
1284
1.26k
          ? stat_stage_set_mb_mi
1285
1.26k
          : enc_set_mb_mi;
1286
1287
1.26k
  mi_params->mi_alloc_bsize = BLOCK_4X4;
1288
1289
1.26k
  CHECK_MEM_ERROR(cm, cm->fc,
1290
1.26k
                  (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
1291
1.26k
  CHECK_MEM_ERROR(
1292
1.26k
      cm, cm->default_frame_context,
1293
1.26k
      (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
1294
1.26k
  memset(cm->fc, 0, sizeof(*cm->fc));
1295
1.26k
  memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
1296
1297
1.26k
  cpi->common.buffer_pool = pool;
1298
1299
1.26k
  init_config(cpi, oxcf);
1300
1.26k
  if (cpi->compressor_stage == LAP_STAGE) {
1301
0
    cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames;
1302
0
  }
1303
1304
1.26k
  av1_rc_init(&cpi->oxcf, &cpi->rc);
1305
1306
1.26k
  init_frame_info(&cpi->frame_info, cm);
1307
1.26k
  init_frame_index_set(&cpi->frame_index_set);
1308
1309
1.26k
  cm->current_frame.frame_number = 0;
1310
1.26k
  cm->current_frame_id = -1;
1311
1.26k
  cpi->tile_data = NULL;
1312
1.26k
  cpi->last_show_frame_buf = NULL;
1313
1.26k
  realloc_segmentation_maps(cpi);
1314
1315
1.26k
  cpi->refresh_frame.alt_ref_frame = false;
1316
1317
#if CONFIG_SPEED_STATS
1318
  cpi->tx_search_count = 0;
1319
#endif  // CONFIG_SPEED_STATS
1320
1321
1.26k
  cpi->time_stamps.first_ts_start = INT64_MAX;
1322
1323
#ifdef OUTPUT_YUV_REC
1324
  yuv_rec_file = fopen("rec.yuv", "wb");
1325
#endif
1326
#ifdef OUTPUT_YUV_DENOISED
1327
  yuv_denoised_file = fopen("denoised.yuv", "wb");
1328
#endif
1329
1330
1.26k
#if !CONFIG_REALTIME_ONLY
1331
1.26k
  if (is_stat_consumption_stage(cpi)) {
1332
0
    const size_t packet_sz = sizeof(FIRSTPASS_STATS);
1333
0
    const int packets = (int)(oxcf->twopass_stats_in.sz / packet_sz);
1334
1335
0
    if (!cpi->ppi->lap_enabled) {
1336
      /*Re-initialize to stats buffer, populated by application in the case of
1337
       * two pass*/
1338
0
      cpi->ppi->twopass.stats_buf_ctx->stats_in_start =
1339
0
          oxcf->twopass_stats_in.buf;
1340
0
      cpi->twopass_frame.stats_in =
1341
0
          cpi->ppi->twopass.stats_buf_ctx->stats_in_start;
1342
0
      cpi->ppi->twopass.stats_buf_ctx->stats_in_end =
1343
0
          &cpi->ppi->twopass.stats_buf_ctx->stats_in_start[packets - 1];
1344
1345
      // The buffer size is packets - 1 because the last packet is total_stats.
1346
0
      av1_firstpass_info_init(&cpi->ppi->twopass.firstpass_info,
1347
0
                              oxcf->twopass_stats_in.buf, packets - 1);
1348
0
      av1_init_second_pass(cpi);
1349
0
    } else {
1350
0
      av1_firstpass_info_init(&cpi->ppi->twopass.firstpass_info, NULL, 0);
1351
0
      av1_init_single_pass_lap(cpi);
1352
0
    }
1353
0
  }
1354
1.26k
#endif
1355
1356
  // The buffer "obmc_buffer" is used in inter frames for fast obmc search.
1357
  // Hence, the memory allocation for the same is avoided for allintra encoding
1358
  // mode.
1359
1.26k
  if (cpi->oxcf.kf_cfg.key_freq_max != 0)
1360
0
    alloc_obmc_buffers(&cpi->td.mb.obmc_buffer, cm->error);
1361
1362
3.78k
  for (int x = 0; x < 2; x++)
1363
7.57k
    for (int y = 0; y < 2; y++)
1364
5.04k
      CHECK_MEM_ERROR(
1365
1.26k
          cm, cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
1366
1.26k
          (uint32_t *)aom_malloc(
1367
1.26k
              AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
1368
1.26k
              sizeof(*cpi->td.mb.intrabc_hash_info.hash_value_buffer[0][0])));
1369
1370
1.26k
  cpi->td.mb.intrabc_hash_info.g_crc_initialized = 0;
1371
1372
1.26k
  av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
1373
1.26k
  av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
1374
1375
1.26k
  CHECK_MEM_ERROR(cm, cpi->consec_zero_mv,
1376
1.26k
                  aom_calloc((mi_params->mi_rows * mi_params->mi_cols) >> 2,
1377
1.26k
                             sizeof(*cpi->consec_zero_mv)));
1378
1379
1.26k
  cpi->mb_weber_stats = NULL;
1380
1.26k
  cpi->mb_delta_q = NULL;
1381
1382
1.26k
  {
1383
1.26k
    const int bsize = BLOCK_16X16;
1384
1.26k
    const int w = mi_size_wide[bsize];
1385
1.26k
    const int h = mi_size_high[bsize];
1386
1.26k
    const int num_cols = (mi_params->mi_cols + w - 1) / w;
1387
1.26k
    const int num_rows = (mi_params->mi_rows + h - 1) / h;
1388
1.26k
    CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
1389
1.26k
                    aom_calloc(num_rows * num_cols,
1390
1.26k
                               sizeof(*cpi->ssim_rdmult_scaling_factors)));
1391
1.26k
  }
1392
1393
#if CONFIG_TUNE_VMAF
1394
  {
1395
    const int bsize = BLOCK_64X64;
1396
    const int w = mi_size_wide[bsize];
1397
    const int h = mi_size_high[bsize];
1398
    const int num_cols = (mi_params->mi_cols + w - 1) / w;
1399
    const int num_rows = (mi_params->mi_rows + h - 1) / h;
1400
    CHECK_MEM_ERROR(cm, cpi->vmaf_info.rdmult_scaling_factors,
1401
                    aom_calloc(num_rows * num_cols,
1402
                               sizeof(*cpi->vmaf_info.rdmult_scaling_factors)));
1403
    for (int i = 0; i < MAX_ARF_LAYERS; i++) {
1404
      cpi->vmaf_info.last_frame_unsharp_amount[i] = -1.0;
1405
      cpi->vmaf_info.last_frame_ysse[i] = -1.0;
1406
      cpi->vmaf_info.last_frame_vmaf[i] = -1.0;
1407
    }
1408
    cpi->vmaf_info.original_qindex = -1;
1409
    cpi->vmaf_info.vmaf_model = NULL;
1410
  }
1411
#endif
1412
1413
#if CONFIG_TUNE_BUTTERAUGLI
1414
  {
1415
    const int w = mi_size_wide[butteraugli_rdo_bsize];
1416
    const int h = mi_size_high[butteraugli_rdo_bsize];
1417
    const int num_cols = (mi_params->mi_cols + w - 1) / w;
1418
    const int num_rows = (mi_params->mi_rows + h - 1) / h;
1419
    CHECK_MEM_ERROR(
1420
        cm, cpi->butteraugli_info.rdmult_scaling_factors,
1421
        aom_malloc(num_rows * num_cols *
1422
                   sizeof(*cpi->butteraugli_info.rdmult_scaling_factors)));
1423
    memset(&cpi->butteraugli_info.source, 0,
1424
           sizeof(cpi->butteraugli_info.source));
1425
    memset(&cpi->butteraugli_info.resized_source, 0,
1426
           sizeof(cpi->butteraugli_info.resized_source));
1427
    cpi->butteraugli_info.recon_set = false;
1428
  }
1429
#endif
1430
1431
#if CONFIG_COLLECT_PARTITION_STATS
1432
  av1_zero(cpi->partition_stats);
1433
#endif  // CONFIG_COLLECT_PARTITION_STATS
1434
1435
  /* av1_init_quantizer() is first called here. Add check in
1436
   * av1_frame_init_quantizer() so that av1_init_quantizer is only
1437
   * called later when needed. This will avoid unnecessary calls of
1438
   * av1_init_quantizer() for every frame.
1439
   */
1440
1.26k
  av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
1441
1.26k
                     cm->seq_params->bit_depth);
1442
1.26k
  av1_qm_init(&cm->quant_params, av1_num_planes(cm));
1443
1444
1.26k
  av1_loop_filter_init(cm);
1445
1.26k
  cm->superres_scale_denominator = SCALE_NUMERATOR;
1446
1.26k
  cm->superres_upscaled_width = oxcf->frm_dim_cfg.width;
1447
1.26k
  cm->superres_upscaled_height = oxcf->frm_dim_cfg.height;
1448
1.26k
#if !CONFIG_REALTIME_ONLY
1449
1.26k
  av1_loop_restoration_precal();
1450
1.26k
#endif
1451
1452
1.26k
  cpi->third_pass_ctx = NULL;
1453
1.26k
  if (cpi->oxcf.pass == AOM_RC_THIRD_PASS) {
1454
0
    av1_init_thirdpass_ctx(cm, &cpi->third_pass_ctx, NULL);
1455
0
  }
1456
1457
1.26k
  cpi->second_pass_log_stream = NULL;
1458
1459
1.26k
  cm->error->setjmp = 0;
1460
1.26k
  return cpi;
1461
1.26k
}
1462
1463
#if CONFIG_INTERNAL_STATS
1464
#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
1465
1466
#define SNPRINT2(H, T, V) \
1467
  snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
1468
#endif  // CONFIG_INTERNAL_STATS
1469
1470
// This function will change the state and free the mutex of corresponding
1471
// workers and terminate the object. The object can not be re-used unless a call
1472
// to reset() is made.
1473
1.26k
static AOM_INLINE void terminate_worker_data(AV1_PRIMARY *ppi) {
1474
1.26k
  PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
1475
2.72k
  for (int t = p_mt_info->num_workers - 1; t >= 0; --t) {
1476
1.45k
    AVxWorker *const worker = &p_mt_info->workers[t];
1477
1.45k
    aom_get_worker_interface()->end(worker);
1478
1.45k
  }
1479
1.26k
}
1480
1481
// Deallocate allocated thread_data.
1482
1.26k
static AOM_INLINE void free_thread_data(AV1_PRIMARY *ppi) {
1483
1.26k
  PrimaryMultiThreadInfo *const p_mt_info = &ppi->p_mt_info;
1484
2.09k
  for (int t = 1; t < p_mt_info->num_workers; ++t) {
1485
830
    EncWorkerData *const thread_data = &p_mt_info->tile_thr_data[t];
1486
#if CONFIG_FRAME_PARALLEL_ENCODE
1487
    thread_data->td = thread_data->original_td;
1488
#endif
1489
830
    aom_free(thread_data->td->tctx);
1490
830
    aom_free(thread_data->td->palette_buffer);
1491
830
    aom_free(thread_data->td->tmp_conv_dst);
1492
830
    release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
1493
2.49k
    for (int j = 0; j < 2; ++j) {
1494
1.66k
      aom_free(thread_data->td->tmp_pred_bufs[j]);
1495
1.66k
    }
1496
830
    aom_free(thread_data->td->pixel_gradient_info);
1497
830
    release_obmc_buffers(&thread_data->td->obmc_buffer);
1498
830
    aom_free(thread_data->td->vt64x64);
1499
1500
2.49k
    for (int x = 0; x < 2; x++) {
1501
4.98k
      for (int y = 0; y < 2; y++) {
1502
3.32k
        aom_free(thread_data->td->hash_value_buffer[x][y]);
1503
3.32k
        thread_data->td->hash_value_buffer[x][y] = NULL;
1504
3.32k
      }
1505
1.66k
    }
1506
830
    aom_free(thread_data->td->counts);
1507
830
    av1_free_pmc(thread_data->td->firstpass_ctx,
1508
830
                 ppi->seq_params.monochrome ? 1 : MAX_MB_PLANE);
1509
830
    thread_data->td->firstpass_ctx = NULL;
1510
830
    av1_free_shared_coeff_buffer(&thread_data->td->shared_coeff_buf);
1511
830
    av1_free_sms_tree(thread_data->td);
1512
830
    aom_free(thread_data->td);
1513
830
  }
1514
1.26k
}
1515
1516
1.26k
void av1_remove_primary_compressor(AV1_PRIMARY *ppi) {
1517
1.26k
  if (!ppi) return;
1518
1.26k
#if !CONFIG_REALTIME_ONLY
1519
1.26k
  av1_tf_info_free(&ppi->tf_info);
1520
1.26k
#endif  // !CONFIG_REALTIME_ONLY
1521
1522
41.6k
  for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
1523
40.3k
    aom_free(ppi->level_params.level_info[i]);
1524
40.3k
  }
1525
1.26k
  av1_lookahead_destroy(ppi->lookahead);
1526
1527
1.26k
  aom_free(ppi->tpl_rdmult_scaling_factors);
1528
1.26k
  ppi->tpl_rdmult_scaling_factors = NULL;
1529
1.26k
  aom_free(ppi->tpl_sb_rdmult_scaling_factors);
1530
1.26k
  ppi->tpl_sb_rdmult_scaling_factors = NULL;
1531
1532
1.26k
  TplParams *const tpl_data = &ppi->tpl_data;
1533
61.8k
  for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
1534
60.5k
    aom_free(tpl_data->tpl_stats_pool[frame]);
1535
60.5k
    aom_free_frame_buffer(&tpl_data->tpl_rec_pool[frame]);
1536
60.5k
  }
1537
1538
1.26k
#if !CONFIG_REALTIME_ONLY
1539
1.26k
  av1_tpl_dealloc(&tpl_data->tpl_mt_sync);
1540
1.26k
#endif
1541
1542
1.26k
  terminate_worker_data(ppi);
1543
1.26k
  free_thread_data(ppi);
1544
1545
1.26k
  aom_free(ppi->p_mt_info.tile_thr_data);
1546
1.26k
  aom_free(ppi->p_mt_info.workers);
1547
1548
1.26k
  aom_free(ppi);
1549
1.26k
}
1550
1551
1.26k
void av1_remove_compressor(AV1_COMP *cpi) {
1552
1.26k
  if (!cpi) return;
1553
1554
1.26k
  AV1_COMMON *cm = &cpi->common;
1555
1.26k
  if (cm->current_frame.frame_number > 0) {
1556
#if CONFIG_SPEED_STATS
1557
    if (!is_stat_generation_stage(cpi)) {
1558
      fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
1559
    }
1560
#endif  // CONFIG_SPEED_STATS
1561
1562
#if CONFIG_COLLECT_PARTITION_STATS == 2
1563
    if (!is_stat_generation_stage(cpi)) {
1564
      av1_print_fr_partition_timing_stats(&cpi->partition_stats,
1565
                                          "fr_part_timing_data.csv");
1566
    }
1567
#endif
1568
1.26k
  }
1569
1570
#if CONFIG_AV1_TEMPORAL_DENOISING
1571
  av1_denoiser_free(&(cpi->denoiser));
1572
#endif
1573
1574
#if CONFIG_FRAME_PARALLEL_ENCODE
1575
  aom_free(cm->error);
1576
#endif
1577
1.26k
  aom_free(cpi->td.tctx);
1578
1.26k
  MultiThreadInfo *const mt_info = &cpi->mt_info;
1579
1.26k
#if CONFIG_MULTITHREAD
1580
1.26k
  pthread_mutex_t *const enc_row_mt_mutex_ = mt_info->enc_row_mt.mutex_;
1581
1.26k
  pthread_mutex_t *const gm_mt_mutex_ = mt_info->gm_sync.mutex_;
1582
1.26k
  pthread_mutex_t *const pack_bs_mt_mutex_ = mt_info->pack_bs_sync.mutex_;
1583
1.26k
  if (enc_row_mt_mutex_ != NULL) {
1584
628
    pthread_mutex_destroy(enc_row_mt_mutex_);
1585
628
    aom_free(enc_row_mt_mutex_);
1586
628
  }
1587
1.26k
  if (gm_mt_mutex_ != NULL) {
1588
628
    pthread_mutex_destroy(gm_mt_mutex_);
1589
628
    aom_free(gm_mt_mutex_);
1590
628
  }
1591
1.26k
  if (pack_bs_mt_mutex_ != NULL) {
1592
628
    pthread_mutex_destroy(pack_bs_mt_mutex_);
1593
628
    aom_free(pack_bs_mt_mutex_);
1594
628
  }
1595
1.26k
#endif
1596
1.26k
  av1_row_mt_mem_dealloc(cpi);
1597
1598
1.26k
  if (mt_info->num_workers > 1) {
1599
628
    av1_loop_filter_dealloc(&mt_info->lf_row_sync);
1600
628
    av1_cdef_mt_dealloc(&mt_info->cdef_sync);
1601
628
#if !CONFIG_REALTIME_ONLY
1602
628
    int num_lr_workers =
1603
628
        av1_get_num_mod_workers_for_alloc(&cpi->ppi->p_mt_info, MOD_LR);
1604
628
    av1_loop_restoration_dealloc(&mt_info->lr_row_sync, num_lr_workers);
1605
628
    av1_gm_dealloc(&mt_info->gm_sync);
1606
628
    av1_tf_mt_dealloc(&mt_info->tf_sync);
1607
628
#endif
1608
628
  }
1609
1610
1.26k
  av1_free_thirdpass_ctx(cpi->third_pass_ctx);
1611
1612
1.26k
  av1_close_second_pass_log(cpi);
1613
1614
1.26k
  dealloc_compressor_data(cpi);
1615
1616
1.26k
  av1_ext_part_delete(&cpi->ext_part_controller);
1617
1618
1.26k
  av1_remove_common(cm);
1619
1620
1.26k
  aom_free(cpi);
1621
1622
#ifdef OUTPUT_YUV_REC
1623
  fclose(yuv_rec_file);
1624
#endif
1625
1626
#ifdef OUTPUT_YUV_DENOISED
1627
  fclose(yuv_denoised_file);
1628
#endif
1629
1.26k
}
1630
1631
0
static void generate_psnr_packet(AV1_COMP *cpi) {
1632
0
  struct aom_codec_cx_pkt pkt;
1633
0
  int i;
1634
0
  PSNR_STATS psnr;
1635
0
#if CONFIG_AV1_HIGHBITDEPTH
1636
0
  const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
1637
0
  const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
1638
0
  aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
1639
0
                       bit_depth, in_bit_depth);
1640
#else
1641
  aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
1642
#endif
1643
1644
0
  for (i = 0; i < 4; ++i) {
1645
0
    pkt.data.psnr.samples[i] = psnr.samples[i];
1646
0
    pkt.data.psnr.sse[i] = psnr.sse[i];
1647
0
    pkt.data.psnr.psnr[i] = psnr.psnr[i];
1648
0
  }
1649
1650
0
#if CONFIG_AV1_HIGHBITDEPTH
1651
0
  if ((cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) &&
1652
0
      (in_bit_depth < bit_depth)) {
1653
0
    for (i = 0; i < 4; ++i) {
1654
0
      pkt.data.psnr.samples_hbd[i] = psnr.samples_hbd[i];
1655
0
      pkt.data.psnr.sse_hbd[i] = psnr.sse_hbd[i];
1656
0
      pkt.data.psnr.psnr_hbd[i] = psnr.psnr_hbd[i];
1657
0
    }
1658
0
  }
1659
0
#endif
1660
1661
0
  pkt.kind = AOM_CODEC_PSNR_PKT;
1662
0
  aom_codec_pkt_list_add(cpi->ppi->output_pkt_list, &pkt);
1663
0
}
1664
1665
0
int av1_use_as_reference(int *ext_ref_frame_flags, int ref_frame_flags) {
1666
0
  if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
1667
1668
0
  *ext_ref_frame_flags = ref_frame_flags;
1669
0
  return 0;
1670
0
}
1671
1672
0
int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
1673
0
  AV1_COMMON *const cm = &cpi->common;
1674
0
  const int num_planes = av1_num_planes(cm);
1675
0
  YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
1676
0
  if (cfg) {
1677
0
    aom_yv12_copy_frame(cfg, sd, num_planes);
1678
0
    return 0;
1679
0
  } else {
1680
0
    return -1;
1681
0
  }
1682
0
}
1683
1684
0
int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
1685
0
  AV1_COMMON *const cm = &cpi->common;
1686
0
  const int num_planes = av1_num_planes(cm);
1687
0
  YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
1688
0
  if (cfg) {
1689
0
    aom_yv12_copy_frame(sd, cfg, num_planes);
1690
0
    return 0;
1691
0
  } else {
1692
0
    return -1;
1693
0
  }
1694
0
}
1695
1696
#ifdef OUTPUT_YUV_REC
1697
void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
1698
  uint8_t *src = s->y_buffer;
1699
  int h = cm->height;
1700
  if (yuv_rec_file == NULL) return;
1701
  if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
1702
    uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
1703
1704
    do {
1705
      fwrite(src16, s->y_width, 2, yuv_rec_file);
1706
      src16 += s->y_stride;
1707
    } while (--h);
1708
1709
    src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
1710
    h = s->uv_height;
1711
1712
    do {
1713
      fwrite(src16, s->uv_width, 2, yuv_rec_file);
1714
      src16 += s->uv_stride;
1715
    } while (--h);
1716
1717
    src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
1718
    h = s->uv_height;
1719
1720
    do {
1721
      fwrite(src16, s->uv_width, 2, yuv_rec_file);
1722
      src16 += s->uv_stride;
1723
    } while (--h);
1724
1725
    fflush(yuv_rec_file);
1726
    return;
1727
  }
1728
1729
  do {
1730
    fwrite(src, s->y_width, 1, yuv_rec_file);
1731
    src += s->y_stride;
1732
  } while (--h);
1733
1734
  src = s->u_buffer;
1735
  h = s->uv_height;
1736
1737
  do {
1738
    fwrite(src, s->uv_width, 1, yuv_rec_file);
1739
    src += s->uv_stride;
1740
  } while (--h);
1741
1742
  src = s->v_buffer;
1743
  h = s->uv_height;
1744
1745
  do {
1746
    fwrite(src, s->uv_width, 1, yuv_rec_file);
1747
    src += s->uv_stride;
1748
  } while (--h);
1749
1750
  fflush(yuv_rec_file);
1751
}
1752
#endif  // OUTPUT_YUV_REC
1753
1754
2.52k
void av1_set_mv_search_params(AV1_COMP *cpi) {
1755
2.52k
  const AV1_COMMON *const cm = &cpi->common;
1756
2.52k
  MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
1757
2.52k
  const int max_mv_def = AOMMAX(cm->width, cm->height);
1758
1759
  // Default based on max resolution.
1760
2.52k
  mv_search_params->mv_step_param = av1_init_search_range(max_mv_def);
1761
1762
2.52k
  if (cpi->sf.mv_sf.auto_mv_step_size) {
1763
2.52k
    if (frame_is_intra_only(cm)) {
1764
      // Initialize max_mv_magnitude for use in the first INTER frame
1765
      // after a key/intra-only frame.
1766
2.52k
      mv_search_params->max_mv_magnitude = max_mv_def;
1767
2.52k
    } else {
1768
      // Use adaptive mv steps based on previous frame stats for show frames and
1769
      // internal arfs.
1770
0
      FRAME_UPDATE_TYPE cur_update_type =
1771
0
          cpi->ppi->gf_group.update_type[cpi->gf_frame_index];
1772
0
      int use_auto_mv_step =
1773
0
          (cm->show_frame || cur_update_type == INTNL_ARF_UPDATE) &&
1774
0
          mv_search_params->max_mv_magnitude != -1 &&
1775
0
          cpi->sf.mv_sf.auto_mv_step_size >= 2;
1776
0
      if (use_auto_mv_step) {
1777
        // Allow mv_steps to correspond to twice the max mv magnitude found
1778
        // in the previous frame, capped by the default max_mv_magnitude based
1779
        // on resolution.
1780
0
        mv_search_params->mv_step_param = av1_init_search_range(
1781
0
            AOMMIN(max_mv_def, 2 * mv_search_params->max_mv_magnitude));
1782
0
      }
1783
#if CONFIG_FRAME_PARALLEL_ENCODE
1784
      // Reset max_mv_magnitude based on update flag.
1785
      if (cpi->do_frame_data_update) mv_search_params->max_mv_magnitude = -1;
1786
#else
1787
0
      mv_search_params->max_mv_magnitude = -1;
1788
0
#endif
1789
0
    }
1790
2.52k
  }
1791
2.52k
}
1792
1793
1.26k
void av1_set_screen_content_options(AV1_COMP *cpi, FeatureFlags *features) {
1794
1.26k
  const AV1_COMMON *const cm = &cpi->common;
1795
1796
1.26k
  if (cm->seq_params->force_screen_content_tools != 2) {
1797
0
    features->allow_screen_content_tools = features->allow_intrabc =
1798
0
        cm->seq_params->force_screen_content_tools;
1799
0
    return;
1800
0
  }
1801
1802
1.26k
  if (cpi->oxcf.mode == REALTIME) {
1803
0
    assert(cm->seq_params->reduced_still_picture_hdr);
1804
0
    features->allow_screen_content_tools = features->allow_intrabc = 0;
1805
0
    return;
1806
0
  }
1807
1808
  // Screen content tools are not evaluated in non-RD encoding mode, i.e., when
1809
  // use_nonrd_pick_mode = 1 and hybrid_intra_pickmode = 0. Hence, screen
1810
  // content detection is disabled.
1811
1.26k
  if (cpi->sf.rt_sf.use_nonrd_pick_mode &&
1812
1.26k
      !cpi->sf.rt_sf.hybrid_intra_pickmode) {
1813
0
    features->allow_screen_content_tools = features->allow_intrabc = 0;
1814
0
    return;
1815
0
  }
1816
1817
1.26k
  if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) {
1818
0
    features->allow_screen_content_tools = features->allow_intrabc = 1;
1819
0
    return;
1820
0
  }
1821
1822
  // Estimate if the source frame is screen content, based on the portion of
1823
  // blocks that have few luma colors.
1824
1.26k
  const uint8_t *src = cpi->unfiltered_source->y_buffer;
1825
1.26k
  assert(src != NULL);
1826
1.26k
  const int use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
1827
1.26k
  const int stride = cpi->unfiltered_source->y_stride;
1828
1.26k
  const int width = cpi->unfiltered_source->y_width;
1829
1.26k
  const int height = cpi->unfiltered_source->y_height;
1830
1.26k
  const int bd = cm->seq_params->bit_depth;
1831
1.26k
  const int blk_w = 16;
1832
1.26k
  const int blk_h = 16;
1833
  // These threshold values are selected experimentally.
1834
1.26k
  const int color_thresh = 4;
1835
1.26k
  const unsigned int var_thresh = 0;
1836
  // Counts of blocks with no more than color_thresh colors.
1837
1.26k
  int counts_1 = 0;
1838
  // Counts of blocks with no more than color_thresh colors and variance larger
1839
  // than var_thresh.
1840
1.26k
  int counts_2 = 0;
1841
1842
12.4k
  for (int r = 0; r + blk_h <= height; r += blk_h) {
1843
129k
    for (int c = 0; c + blk_w <= width; c += blk_w) {
1844
118k
      int count_buf[1 << 8];  // Maximum (1 << 8) bins for hbd path.
1845
118k
      const uint8_t *const this_src = src + r * stride + c;
1846
118k
      int n_colors;
1847
118k
      if (use_hbd)
1848
0
        av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd, NULL,
1849
0
                                count_buf, &n_colors, NULL);
1850
118k
      else
1851
118k
        av1_count_colors(this_src, stride, blk_w, blk_h, count_buf, &n_colors);
1852
118k
      if (n_colors > 1 && n_colors <= color_thresh) {
1853
0
        ++counts_1;
1854
0
        struct buf_2d buf;
1855
0
        buf.stride = stride;
1856
0
        buf.buf = (uint8_t *)this_src;
1857
0
        const unsigned int var =
1858
0
            use_hbd
1859
0
                ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
1860
0
                : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
1861
0
        if (var > var_thresh) ++counts_2;
1862
0
      }
1863
118k
    }
1864
11.1k
  }
1865
1866
  // The threshold values are selected experimentally.
1867
1.26k
  features->allow_screen_content_tools =
1868
1.26k
      counts_1 * blk_h * blk_w * 10 > width * height;
1869
  // IntraBC would force loop filters off, so we use more strict rules that also
1870
  // requires that the block has high variance.
1871
1.26k
  features->allow_intrabc = features->allow_screen_content_tools &&
1872
1.26k
                            counts_2 * blk_h * blk_w * 12 > width * height;
1873
1.26k
  cpi->use_screen_content_tools = features->allow_screen_content_tools;
1874
1.26k
  cpi->is_screen_content_type =
1875
1.26k
      features->allow_intrabc ||
1876
1.26k
      (counts_1 * blk_h * blk_w * 10 > width * height * 4 &&
1877
1.26k
       counts_2 * blk_h * blk_w * 30 > width * height);
1878
1.26k
}
1879
1880
// Function pointer to search site config initialization
1881
// of different search method functions.
1882
typedef void (*av1_init_search_site_config)(search_site_config *cfg, int stride,
1883
                                            int level);
1884
1885
av1_init_search_site_config
1886
    av1_init_motion_compensation[NUM_DISTINCT_SEARCH_METHODS] = {
1887
      av1_init_dsmotion_compensation,     av1_init_motion_compensation_nstep,
1888
      av1_init_motion_compensation_nstep, av1_init_dsmotion_compensation,
1889
      av1_init_motion_compensation_hex,   av1_init_motion_compensation_bigdia,
1890
      av1_init_motion_compensation_square
1891
    };
1892
1893
3.78k
static void init_motion_estimation(AV1_COMP *cpi) {
1894
3.78k
  AV1_COMMON *const cm = &cpi->common;
1895
3.78k
  MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params;
1896
3.78k
  const int aligned_width = (cm->width + 7) & ~7;
1897
3.78k
  const int y_stride =
1898
3.78k
      aom_calc_y_stride(aligned_width, cpi->oxcf.border_in_pixels);
1899
3.78k
  const int y_stride_src = ((cpi->oxcf.frm_dim_cfg.width != cm->width ||
1900
3.78k
                             cpi->oxcf.frm_dim_cfg.height != cm->height) ||
1901
3.78k
                            av1_superres_scaled(cm))
1902
3.78k
                               ? y_stride
1903
3.78k
                               : cpi->ppi->lookahead->buf->img.y_stride;
1904
3.78k
  int fpf_y_stride =
1905
3.78k
      cm->cur_frame != NULL ? cm->cur_frame->buf.y_stride : y_stride;
1906
1907
  // Update if search_site_cfg is uninitialized or the current frame has a new
1908
  // stride
1909
3.78k
  const int should_update =
1910
3.78k
      !mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride ||
1911
3.78k
      !mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][DIAMOND].stride ||
1912
3.78k
      (y_stride !=
1913
2.52k
       mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride);
1914
1915
3.78k
  if (!should_update) {
1916
2.52k
    return;
1917
2.52k
  }
1918
1919
  // Initialization of search_site_cfg for NUM_DISTINCT_SEARCH_METHODS.
1920
10.0k
  for (SEARCH_METHODS i = DIAMOND; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
1921
8.83k
    const int level = ((i == NSTEP_8PT) || (i == CLAMPED_DIAMOND)) ? 1 : 0;
1922
8.83k
    av1_init_motion_compensation[i](
1923
8.83k
        &mv_search_params->search_site_cfg[SS_CFG_SRC][i], y_stride, level);
1924
8.83k
    av1_init_motion_compensation[i](
1925
8.83k
        &mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][i], y_stride_src,
1926
8.83k
        level);
1927
8.83k
  }
1928
1929
  // First pass search site config initialization.
1930
1.26k
  av1_init_motion_fpf(&mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
1931
1.26k
                      fpf_y_stride);
1932
8.83k
  for (SEARCH_METHODS i = NSTEP; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
1933
7.57k
    memcpy(&mv_search_params->search_site_cfg[SS_CFG_FPF][i],
1934
7.57k
           &mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
1935
7.57k
           sizeof(search_site_config));
1936
7.57k
  }
1937
1.26k
}
1938
1939
#if !CONFIG_REALTIME_ONLY
1940
#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
1941
static void set_restoration_unit_size(int width, int height, int sx, int sy,
1942
0
                                      RestorationInfo *rst) {
1943
0
  (void)width;
1944
0
  (void)height;
1945
0
  (void)sx;
1946
0
  (void)sy;
1947
#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
1948
  int s = AOMMIN(sx, sy);
1949
#else
1950
0
  int s = 0;
1951
0
#endif  // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
1952
1953
0
  if (width * height > 352 * 288)
1954
0
    rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
1955
0
  else
1956
0
    rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
1957
0
  rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
1958
0
  rst[2].restoration_unit_size = rst[1].restoration_unit_size;
1959
0
}
1960
#endif
1961
1962
1.26k
static void init_ref_frame_bufs(AV1_COMP *cpi) {
1963
1.26k
  AV1_COMMON *const cm = &cpi->common;
1964
1.26k
  int i;
1965
1.26k
  BufferPool *const pool = cm->buffer_pool;
1966
1.26k
  cm->cur_frame = NULL;
1967
11.3k
  for (i = 0; i < REF_FRAMES; ++i) {
1968
10.0k
    cm->ref_frame_map[i] = NULL;
1969
10.0k
  }
1970
21.4k
  for (i = 0; i < FRAME_BUFFERS; ++i) {
1971
20.1k
    pool->frame_bufs[i].ref_count = 0;
1972
20.1k
  }
1973
1.26k
}
1974
1975
void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
1976
1.26k
                             int subsampling_x, int subsampling_y) {
1977
1.26k
  AV1_COMMON *const cm = &cpi->common;
1978
1.26k
  SequenceHeader *const seq_params = cm->seq_params;
1979
1.26k
  InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
1980
1981
1.26k
  if (!initial_dimensions->width ||
1982
1.26k
      seq_params->use_highbitdepth != use_highbitdepth ||
1983
1.26k
      seq_params->subsampling_x != subsampling_x ||
1984
1.26k
      seq_params->subsampling_y != subsampling_y) {
1985
1.26k
    seq_params->subsampling_x = subsampling_x;
1986
1.26k
    seq_params->subsampling_y = subsampling_y;
1987
1.26k
    seq_params->use_highbitdepth = use_highbitdepth;
1988
1989
1.26k
    av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed);
1990
1.26k
    av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed);
1991
1992
1.26k
    if (!is_stat_generation_stage(cpi)) {
1993
1.26k
#if !CONFIG_REALTIME_ONLY
1994
1.26k
      av1_tf_info_alloc(&cpi->ppi->tf_info, cpi);
1995
1.26k
#endif  // !CONFIG_REALTIME_ONLY
1996
1.26k
    }
1997
1.26k
    init_ref_frame_bufs(cpi);
1998
1999
1.26k
    init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
2000
2001
1.26k
    initial_dimensions->width = cm->width;
2002
1.26k
    initial_dimensions->height = cm->height;
2003
1.26k
    cpi->initial_mbs = cm->mi_params.MBs;
2004
1.26k
  }
2005
1.26k
}
2006
2007
#if CONFIG_AV1_TEMPORAL_DENOISING
2008
static void setup_denoiser_buffer(AV1_COMP *cpi) {
2009
  AV1_COMMON *const cm = &cpi->common;
2010
  if (cpi->oxcf.noise_sensitivity > 0 &&
2011
      !cpi->denoiser.frame_buffer_initialized) {
2012
    if (av1_denoiser_alloc(
2013
            cm, &cpi->svc, &cpi->denoiser, cpi->ppi->use_svc,
2014
            cpi->oxcf.noise_sensitivity, cm->width, cm->height,
2015
            cm->seq_params->subsampling_x, cm->seq_params->subsampling_y,
2016
            cm->seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS))
2017
      aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
2018
                         "Failed to allocate denoiser");
2019
  }
2020
}
2021
#endif
2022
2023
// Returns 1 if the assigned width or height was <= 0.
2024
0
int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
2025
0
  AV1_COMMON *cm = &cpi->common;
2026
0
  InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
2027
0
  av1_check_initial_width(cpi, cm->seq_params->use_highbitdepth,
2028
0
                          cm->seq_params->subsampling_x,
2029
0
                          cm->seq_params->subsampling_y);
2030
2031
0
  if (width <= 0 || height <= 0) return 1;
2032
2033
0
  cm->width = width;
2034
0
  cm->height = height;
2035
2036
#if CONFIG_AV1_TEMPORAL_DENOISING
2037
  setup_denoiser_buffer(cpi);
2038
#endif
2039
2040
0
  if (initial_dimensions->width && initial_dimensions->height &&
2041
0
      (cm->width > initial_dimensions->width ||
2042
0
       cm->height > initial_dimensions->height)) {
2043
0
    av1_free_context_buffers(cm);
2044
0
    av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
2045
0
    av1_free_sms_tree(&cpi->td);
2046
0
    av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
2047
0
    cpi->td.firstpass_ctx = NULL;
2048
0
    alloc_mb_mode_info_buffers(cpi);
2049
0
    alloc_compressor_data(cpi);
2050
0
    realloc_segmentation_maps(cpi);
2051
0
    initial_dimensions->width = initial_dimensions->height = 0;
2052
0
  }
2053
0
  alloc_mb_mode_info_buffers(cpi);
2054
0
  av1_update_frame_size(cpi);
2055
2056
0
  return 0;
2057
0
}
2058
2059
2.52k
void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
2060
2.52k
  AV1_COMMON *const cm = &cpi->common;
2061
2.52k
  const SequenceHeader *const seq_params = cm->seq_params;
2062
2.52k
  const int num_planes = av1_num_planes(cm);
2063
2.52k
  MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
2064
2.52k
  int ref_frame;
2065
2066
2.52k
  if (width != cm->width || height != cm->height) {
2067
    // There has been a change in the encoded frame size
2068
0
    av1_set_size_literal(cpi, width, height);
2069
    // Recalculate 'all_lossless' in case super-resolution was (un)selected.
2070
0
    cm->features.all_lossless =
2071
0
        cm->features.coded_lossless && !av1_superres_scaled(cm);
2072
2073
0
    av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height);
2074
#if CONFIG_AV1_TEMPORAL_DENOISING
2075
    // Reset the denoiser on the resized frame.
2076
    if (cpi->oxcf.noise_sensitivity > 0) {
2077
      av1_denoiser_free(&(cpi->denoiser));
2078
      setup_denoiser_buffer(cpi);
2079
    }
2080
#endif
2081
0
  }
2082
2083
2.52k
  if (is_stat_consumption_stage(cpi)) {
2084
0
    av1_set_target_rate(cpi, cm->width, cm->height);
2085
0
  }
2086
2087
2.52k
  alloc_frame_mvs(cm, cm->cur_frame);
2088
2089
  // Allocate above context buffers
2090
2.52k
  CommonContexts *const above_contexts = &cm->above_contexts;
2091
2.52k
  if (above_contexts->num_planes < av1_num_planes(cm) ||
2092
2.52k
      above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
2093
2.52k
      above_contexts->num_tile_rows < cm->tiles.rows) {
2094
1.26k
    av1_free_above_context_buffers(above_contexts);
2095
1.26k
    if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
2096
1.26k
                                        cm->mi_params.mi_cols,
2097
1.26k
                                        av1_num_planes(cm)))
2098
0
      aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
2099
0
                         "Failed to allocate context buffers");
2100
1.26k
  }
2101
2102
  // Reset the frame pointers to the current frame size.
2103
2.52k
  if (aom_realloc_frame_buffer(
2104
2.52k
          &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
2105
2.52k
          seq_params->subsampling_y, seq_params->use_highbitdepth,
2106
2.52k
          cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
2107
2.52k
          NULL, cpi->oxcf.tool_cfg.enable_global_motion))
2108
0
    aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
2109
0
                       "Failed to allocate frame buffer");
2110
2111
2.52k
  if (!is_stat_generation_stage(cpi)) av1_init_cdef_worker(cpi);
2112
2113
2.52k
#if !CONFIG_REALTIME_ONLY
2114
2.52k
  if (is_restoration_used(cm)) {
2115
0
    const int frame_width = cm->superres_upscaled_width;
2116
0
    const int frame_height = cm->superres_upscaled_height;
2117
0
    set_restoration_unit_size(frame_width, frame_height,
2118
0
                              seq_params->subsampling_x,
2119
0
                              seq_params->subsampling_y, cm->rst_info);
2120
0
    for (int i = 0; i < num_planes; ++i)
2121
0
      cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
2122
2123
0
    av1_alloc_restoration_buffers(cm);
2124
    // Store the allocated restoration buffers in MT object.
2125
0
    if (cpi->ppi->p_mt_info.num_workers > 1) {
2126
0
      av1_init_lr_mt_buffers(cpi);
2127
0
    }
2128
0
  }
2129
2.52k
#endif
2130
2131
2.52k
  init_motion_estimation(cpi);
2132
2133
20.1k
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2134
17.6k
    RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
2135
17.6k
    if (buf != NULL) {
2136
0
      struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
2137
0
      av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
2138
0
                                        buf->buf.y_crop_height, cm->width,
2139
0
                                        cm->height);
2140
0
      if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
2141
0
    }
2142
17.6k
  }
2143
2144
2.52k
  av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
2145
2.52k
                                    cm->width, cm->height);
2146
2147
2.52k
  set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
2148
2.52k
}
2149
2150
/*!\brief Select and apply cdef filters and switchable restoration filters
2151
 *
2152
 * \ingroup high_level_algo
2153
 */
2154
static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
2155
                                   MACROBLOCKD *xd, int use_restoration,
2156
1.26k
                                   int use_cdef) {
2157
1.26k
#if !CONFIG_REALTIME_ONLY
2158
1.26k
  if (use_restoration)
2159
0
    av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
2160
#else
2161
  (void)use_restoration;
2162
#endif
2163
2164
1.26k
  if (use_cdef) {
2165
#if CONFIG_COLLECT_COMPONENT_TIMING
2166
    start_timing(cpi, cdef_time);
2167
#endif
2168
0
    const int num_workers = cpi->mt_info.num_mod_workers[MOD_CDEF];
2169
    // Find CDEF parameters
2170
0
    av1_cdef_search(&cpi->mt_info, &cm->cur_frame->buf, cpi->source, cm, xd,
2171
0
                    cpi->sf.lpf_sf.cdef_pick_method, cpi->td.mb.rdmult,
2172
0
                    cpi->sf.rt_sf.skip_cdef_sb, cpi->rc.frames_since_key,
2173
0
                    cpi->oxcf.tool_cfg.cdef_control,
2174
0
                    cpi->svc.non_reference_frame);
2175
2176
    // Apply the filter
2177
0
    if (!cpi->svc.non_reference_frame) {
2178
0
      if (num_workers > 1) {
2179
0
        av1_cdef_frame_mt(cm, xd, cpi->mt_info.cdef_worker,
2180
0
                          cpi->mt_info.workers, &cpi->mt_info.cdef_sync,
2181
0
                          num_workers, av1_cdef_init_fb_row_mt);
2182
0
      } else {
2183
0
        av1_cdef_frame(&cm->cur_frame->buf, cm, xd, av1_cdef_init_fb_row);
2184
0
      }
2185
0
    }
2186
#if CONFIG_COLLECT_COMPONENT_TIMING
2187
    end_timing(cpi, cdef_time);
2188
#endif
2189
1.26k
  } else {
2190
1.26k
    cm->cdef_info.cdef_bits = 0;
2191
1.26k
    cm->cdef_info.cdef_strengths[0] = 0;
2192
1.26k
    cm->cdef_info.nb_cdef_strengths = 1;
2193
1.26k
    cm->cdef_info.cdef_uv_strengths[0] = 0;
2194
1.26k
  }
2195
2196
1.26k
  av1_superres_post_encode(cpi);
2197
2198
1.26k
#if !CONFIG_REALTIME_ONLY
2199
#if CONFIG_COLLECT_COMPONENT_TIMING
2200
  start_timing(cpi, loop_restoration_time);
2201
#endif
2202
1.26k
  if (use_restoration) {
2203
0
    MultiThreadInfo *const mt_info = &cpi->mt_info;
2204
0
    const int num_workers = mt_info->num_mod_workers[MOD_LR];
2205
0
    av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
2206
0
    av1_pick_filter_restoration(cpi->source, cpi);
2207
0
    if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
2208
0
        cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
2209
0
        cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
2210
0
      if (num_workers > 1)
2211
0
        av1_loop_restoration_filter_frame_mt(
2212
0
            &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers,
2213
0
            &mt_info->lr_row_sync, &cpi->lr_ctxt);
2214
0
      else
2215
0
        av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
2216
0
                                          &cpi->lr_ctxt);
2217
0
    }
2218
1.26k
  } else {
2219
1.26k
    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
2220
1.26k
    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
2221
1.26k
    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
2222
1.26k
  }
2223
#if CONFIG_COLLECT_COMPONENT_TIMING
2224
  end_timing(cpi, loop_restoration_time);
2225
#endif
2226
1.26k
#endif  // !CONFIG_REALTIME_ONLY
2227
1.26k
}
2228
2229
/*!\brief Select and apply in-loop deblocking filters, cdef filters, and
2230
 * restoration filters
2231
 *
2232
 * \ingroup high_level_algo
2233
 */
2234
1.26k
static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
2235
1.26k
  MultiThreadInfo *const mt_info = &cpi->mt_info;
2236
1.26k
  const int num_workers = mt_info->num_mod_workers[MOD_LPF];
2237
1.26k
  const int num_planes = av1_num_planes(cm);
2238
1.26k
  MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2239
2240
1.26k
  assert(IMPLIES(is_lossless_requested(&cpi->oxcf.rc_cfg),
2241
1.26k
                 cm->features.coded_lossless && cm->features.all_lossless));
2242
2243
1.26k
  const int use_loopfilter =
2244
1.26k
      !cm->features.coded_lossless && !cm->tiles.large_scale;
2245
1.26k
  const int use_cdef = cm->seq_params->enable_cdef &&
2246
1.26k
                       !cm->features.coded_lossless && !cm->tiles.large_scale;
2247
1.26k
  const int use_restoration = is_restoration_used(cm);
2248
1.26k
  const int cur_width = cm->cur_frame->width;
2249
1.26k
  const int cur_height = cm->cur_frame->height;
2250
1.26k
  const int cur_width_mib = cm->mi_params.mi_cols * MI_SIZE;
2251
1.26k
  const int cur_height_mib = cm->mi_params.mi_rows * MI_SIZE;
2252
1.26k
  const int is_realtime =
2253
1.26k
      cpi->sf.rt_sf.use_nonrd_pick_mode && !(cm->mi_params.mi_cols % 2) &&
2254
1.26k
      !(cm->mi_params.mi_rows % 2) && (cur_width_mib - cur_width < MI_SIZE) &&
2255
1.26k
      (cur_height_mib - cur_height < MI_SIZE);
2256
2257
1.26k
  struct loopfilter *lf = &cm->lf;
2258
2259
#if CONFIG_COLLECT_COMPONENT_TIMING
2260
  start_timing(cpi, loop_filter_time);
2261
#endif
2262
1.26k
  if (use_loopfilter) {
2263
1.03k
    av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick);
2264
1.03k
  } else {
2265
225
    lf->filter_level[0] = 0;
2266
225
    lf->filter_level[1] = 0;
2267
225
  }
2268
2269
1.26k
  if ((lf->filter_level[0] || lf->filter_level[1]) &&
2270
1.26k
      !cpi->svc.non_reference_frame) {
2271
29
    av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
2272
29
                             mt_info->workers, num_workers,
2273
29
                             &mt_info->lf_row_sync, is_realtime);
2274
29
  }
2275
#if CONFIG_COLLECT_COMPONENT_TIMING
2276
  end_timing(cpi, loop_filter_time);
2277
#endif
2278
2279
1.26k
  cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
2280
1.26k
}
2281
2282
/*!\brief Encode a frame without the recode loop, usually used in one-pass
2283
 * encoding and realtime coding.
2284
 *
2285
 * \ingroup high_level_algo
2286
 *
2287
 * \param[in]    cpi             Top-level encoder structure
2288
 *
2289
 * \return Returns a value to indicate if the encoding is done successfully.
2290
 * \retval #AOM_CODEC_OK
2291
 * \retval #AOM_CODEC_ERROR
2292
 */
2293
1.26k
static int encode_without_recode(AV1_COMP *cpi) {
2294
1.26k
  AV1_COMMON *const cm = &cpi->common;
2295
1.26k
  const QuantizationCfg *const q_cfg = &cpi->oxcf.q_cfg;
2296
1.26k
  SVC *const svc = &cpi->svc;
2297
1.26k
  const int resize_pending = is_frame_resize_pending(cpi);
2298
2299
1.26k
  int top_index = 0, bottom_index = 0, q = 0;
2300
1.26k
  YV12_BUFFER_CONFIG *unscaled = cpi->unscaled_source;
2301
1.26k
  InterpFilter filter_scaler =
2302
1.26k
      cpi->ppi->use_svc ? svc->downsample_filter_type[svc->spatial_layer_id]
2303
1.26k
                        : EIGHTTAP_SMOOTH;
2304
1.26k
  int phase_scaler = cpi->ppi->use_svc
2305
1.26k
                         ? svc->downsample_filter_phase[svc->spatial_layer_id]
2306
1.26k
                         : 0;
2307
2308
1.26k
  set_size_independent_vars(cpi);
2309
1.26k
  av1_setup_frame_size(cpi);
2310
1.26k
  av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
2311
1.26k
  av1_set_mv_search_params(cpi);
2312
2313
1.26k
  if (!cpi->ppi->use_svc) {
2314
1.26k
    phase_scaler = 8;
2315
    // 2:1 scaling.
2316
1.26k
    if ((cm->width << 1) == unscaled->y_crop_width &&
2317
1.26k
        (cm->height << 1) == unscaled->y_crop_height) {
2318
0
      filter_scaler = BILINEAR;
2319
      // For lower resolutions use eighttap_smooth.
2320
0
      if (cm->width * cm->height <= 320 * 180) filter_scaler = EIGHTTAP_SMOOTH;
2321
1.26k
    } else if ((cm->width << 2) == unscaled->y_crop_width &&
2322
1.26k
               (cm->height << 2) == unscaled->y_crop_height) {
2323
      // 4:1 scaling.
2324
0
      filter_scaler = EIGHTTAP_SMOOTH;
2325
1.26k
    } else if ((cm->width << 2) == 3 * unscaled->y_crop_width &&
2326
1.26k
               (cm->height << 2) == 3 * unscaled->y_crop_height) {
2327
      // 4:3 scaling.
2328
0
      filter_scaler = EIGHTTAP_REGULAR;
2329
0
    }
2330
1.26k
  }
2331
2332
1.26k
  allocate_gradient_info_for_hog(&cpi->td.pixel_gradient_info, cpi);
2333
2334
1.26k
  const SPEED_FEATURES *sf = &cpi->sf;
2335
1.26k
  if (sf->part_sf.partition_search_type == VAR_BASED_PARTITION)
2336
0
    variance_partition_alloc(cpi);
2337
2338
1.26k
  if (cm->current_frame.frame_type == KEY_FRAME ||
2339
1.26k
      ((sf->inter_sf.extra_prune_warped && cpi->refresh_frame.golden_frame)))
2340
1.26k
    copy_frame_prob_info(cpi);
2341
2342
#if CONFIG_COLLECT_COMPONENT_TIMING
2343
  printf("\n Encoding a frame: \n");
2344
#endif
2345
2346
#if CONFIG_TUNE_BUTTERAUGLI
2347
  if (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) {
2348
    av1_setup_butteraugli_rdmult(cpi);
2349
  }
2350
#endif
2351
2352
1.26k
  cpi->source = av1_realloc_and_scale_if_required(
2353
1.26k
      cm, unscaled, &cpi->scaled_source, filter_scaler, phase_scaler, true,
2354
1.26k
      false, cpi->oxcf.border_in_pixels,
2355
1.26k
      cpi->oxcf.tool_cfg.enable_global_motion);
2356
1.26k
  if (frame_is_intra_only(cm) || resize_pending != 0) {
2357
1.26k
    memset(cpi->consec_zero_mv, 0,
2358
1.26k
           ((cm->mi_params.mi_rows * cm->mi_params.mi_cols) >> 2) *
2359
1.26k
               sizeof(*cpi->consec_zero_mv));
2360
1.26k
  }
2361
2362
1.26k
  if (cpi->unscaled_last_source != NULL) {
2363
0
    cpi->last_source = av1_realloc_and_scale_if_required(
2364
0
        cm, cpi->unscaled_last_source, &cpi->scaled_last_source, filter_scaler,
2365
0
        phase_scaler, true, false, cpi->oxcf.border_in_pixels,
2366
0
        cpi->oxcf.tool_cfg.enable_global_motion);
2367
0
  }
2368
2369
1.26k
  if (cpi->sf.rt_sf.use_temporal_noise_estimate) {
2370
0
    av1_update_noise_estimate(cpi);
2371
0
  }
2372
2373
#if CONFIG_AV1_TEMPORAL_DENOISING
2374
  if (cpi->oxcf.noise_sensitivity > 0 && cpi->ppi->use_svc)
2375
    av1_denoiser_reset_on_first_frame(cpi);
2376
#endif
2377
2378
  // For 1 spatial layer encoding: if the (non-LAST) reference has different
2379
  // resolution from the source then disable that reference. This is to avoid
2380
  // significant increase in encode time from scaling the references in
2381
  // av1_scale_references. Note GOLDEN is forced to update on the (first/tigger)
2382
  // resized frame and ALTREF will be refreshed ~4 frames later, so both
2383
  // references become available again after few frames.
2384
1.26k
  if (svc->number_spatial_layers == 1) {
2385
1.26k
    if (cpi->ref_frame_flags & av1_ref_frame_flag_list[GOLDEN_FRAME]) {
2386
0
      const YV12_BUFFER_CONFIG *const ref =
2387
0
          get_ref_frame_yv12_buf(cm, GOLDEN_FRAME);
2388
0
      if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)
2389
0
        cpi->ref_frame_flags ^= AOM_GOLD_FLAG;
2390
0
    }
2391
1.26k
    if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ALTREF_FRAME]) {
2392
0
      const YV12_BUFFER_CONFIG *const ref =
2393
0
          get_ref_frame_yv12_buf(cm, ALTREF_FRAME);
2394
0
      if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)
2395
0
        cpi->ref_frame_flags ^= AOM_ALT_FLAG;
2396
0
    }
2397
1.26k
  }
2398
2399
#if CONFIG_FRAME_PARALLEL_ENCODE
2400
  int scale_references = 0;
2401
#if CONFIG_FPMT_TEST
2402
  scale_references =
2403
      cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE ? 1 : 0;
2404
#endif  // CONFIG_FPMT_TEST
2405
  if (scale_references ||
2406
      cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0)
2407
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
2408
1.26k
  {
2409
    // For SVC the inter-layer/spatial prediction is not done for newmv
2410
    // (zero_mode is forced), and since the scaled references are only
2411
    // use for newmv search, we can avoid scaling here.
2412
1.26k
    if (!frame_is_intra_only(cm) &&
2413
1.26k
        !(cpi->ppi->use_svc && cpi->svc.force_zero_mode_spatial_ref))
2414
0
      av1_scale_references(cpi, filter_scaler, phase_scaler, 1);
2415
1.26k
  }
2416
2417
1.26k
  av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2418
1.26k
                    q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq);
2419
1.26k
  av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
2420
1.26k
  if ((q_cfg->deltaq_mode != NO_DELTA_Q) || q_cfg->enable_chroma_deltaq)
2421
1.26k
    av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
2422
1.26k
                       cm->seq_params->bit_depth);
2423
1.26k
  av1_set_variance_partition_thresholds(cpi, q, 0);
2424
1.26k
  av1_setup_frame(cpi);
2425
2426
  // Check if this high_source_sad (scene/slide change) frame should be
2427
  // encoded at high/max QP, and if so, set the q and adjust some rate
2428
  // control parameters.
2429
1.26k
  if (cpi->sf.rt_sf.overshoot_detection_cbr == FAST_DETECTION_MAXQ &&
2430
1.26k
      (cpi->rc.high_source_sad ||
2431
0
       (cpi->ppi->use_svc && cpi->svc.high_source_sad_superframe))) {
2432
0
    if (av1_encodedframe_overshoot_cbr(cpi, &q)) {
2433
0
      av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2434
0
                        q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq);
2435
0
      av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
2436
0
      if (q_cfg->deltaq_mode != NO_DELTA_Q || q_cfg->enable_chroma_deltaq)
2437
0
        av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
2438
0
                           cm->seq_params->bit_depth);
2439
0
      av1_set_variance_partition_thresholds(cpi, q, 0);
2440
0
      if (frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
2441
0
          cm->features.primary_ref_frame == PRIMARY_REF_NONE)
2442
0
        av1_setup_frame(cpi);
2443
0
    }
2444
0
  }
2445
2446
1.26k
  if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ) {
2447
0
    suppress_active_map(cpi);
2448
0
    av1_cyclic_refresh_setup(cpi);
2449
0
    av1_apply_active_map(cpi);
2450
0
  }
2451
1.26k
  if (cm->seg.enabled) {
2452
0
    if (!cm->seg.update_data && cm->prev_frame) {
2453
0
      segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
2454
0
      cm->seg.enabled = cm->prev_frame->seg.enabled;
2455
0
    } else {
2456
0
      av1_calculate_segdata(&cm->seg);
2457
0
    }
2458
1.26k
  } else {
2459
1.26k
    memset(&cm->seg, 0, sizeof(cm->seg));
2460
1.26k
  }
2461
1.26k
  segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
2462
1.26k
  cm->cur_frame->seg.enabled = cm->seg.enabled;
2463
2464
#if CONFIG_COLLECT_COMPONENT_TIMING
2465
  start_timing(cpi, av1_encode_frame_time);
2466
#endif
2467
2468
  // Set the motion vector precision based on mv stats from the last coded
2469
  // frame.
2470
1.26k
  if (!frame_is_intra_only(cm)) av1_pick_and_set_high_precision_mv(cpi, q);
2471
2472
  // transform / motion compensation build reconstruction frame
2473
1.26k
  av1_encode_frame(cpi);
2474
2475
  // Update some stats from cyclic refresh.
2476
1.26k
  if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ && !cpi->rc.rtc_external_ratectrl &&
2477
1.26k
      !frame_is_intra_only(cm))
2478
0
    av1_cyclic_refresh_postencode(cpi);
2479
2480
  // Adjust the refresh of the golden (longer-term) reference based on QP
2481
  // selected for this frame. This is for CBR with 1 layer/non-svc RTC mode.
2482
1.26k
  if (!frame_is_intra_only(cm) && cpi->oxcf.rc_cfg.mode == AOM_CBR &&
2483
1.26k
      cpi->oxcf.mode == REALTIME && svc->number_spatial_layers == 1 &&
2484
1.26k
      svc->number_temporal_layers == 1 && !cpi->rc.rtc_external_ratectrl &&
2485
1.26k
      sf->rt_sf.gf_refresh_based_on_qp)
2486
0
    av1_adjust_gf_refresh_qp_one_pass_rt(cpi);
2487
2488
#if CONFIG_COLLECT_COMPONENT_TIMING
2489
  end_timing(cpi, av1_encode_frame_time);
2490
#endif
2491
#if CONFIG_INTERNAL_STATS
2492
  ++cpi->frame_recode_hits;
2493
#endif
2494
2495
1.26k
  return AOM_CODEC_OK;
2496
1.26k
}
2497
2498
#if !CONFIG_REALTIME_ONLY
2499
2500
/*!\brief Recode loop for encoding one frame. the purpose of encoding one frame
2501
 * for multiple times can be approaching a target bitrate or adjusting the usage
2502
 * of global motions.
2503
 *
2504
 * \ingroup high_level_algo
2505
 *
2506
 * \param[in]    cpi             Top-level encoder structure
2507
 * \param[in]    size            Bitstream size
2508
 * \param[in]    dest            Bitstream output
2509
 *
2510
 * \return Returns a value to indicate if the encoding is done successfully.
2511
 * \retval #AOM_CODEC_OK
2512
 * \retval -1
2513
 * \retval #AOM_CODEC_ERROR
2514
 */
2515
0
static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
2516
0
  AV1_COMMON *const cm = &cpi->common;
2517
0
  RATE_CONTROL *const rc = &cpi->rc;
2518
0
  GlobalMotionInfo *const gm_info = &cpi->gm_info;
2519
0
  const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2520
0
  const QuantizationCfg *const q_cfg = &oxcf->q_cfg;
2521
0
  const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE);
2522
  // Must allow recode if minimum compression ratio is set.
2523
0
  assert(IMPLIES(oxcf->rc_cfg.min_cr > 0, allow_recode));
2524
2525
0
  set_size_independent_vars(cpi);
2526
0
  if (is_stat_consumption_stage_twopass(cpi) &&
2527
0
      cpi->sf.interp_sf.adaptive_interp_filter_search)
2528
0
    cpi->interp_search_flags.interp_filter_search_mask =
2529
0
        av1_setup_interp_filter_search_mask(cpi);
2530
0
  cpi->source->buf_8bit_valid = 0;
2531
2532
0
  av1_setup_frame_size(cpi);
2533
2534
0
  if (av1_superres_in_recode_allowed(cpi) &&
2535
0
      cpi->superres_mode != AOM_SUPERRES_NONE &&
2536
0
      cm->superres_scale_denominator == SCALE_NUMERATOR) {
2537
    // Superres mode is currently enabled, but the denominator selected will
2538
    // disable superres. So no need to continue, as we will go through another
2539
    // recode loop for full-resolution after this anyway.
2540
0
    return -1;
2541
0
  }
2542
2543
0
  int top_index = 0, bottom_index = 0;
2544
0
  int q = 0, q_low = 0, q_high = 0;
2545
0
  av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
2546
0
  q_low = bottom_index;
2547
0
  q_high = top_index;
2548
2549
0
  av1_set_mv_search_params(cpi);
2550
2551
0
  allocate_gradient_info_for_hog(&cpi->td.pixel_gradient_info, cpi);
2552
2553
0
  if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION)
2554
0
    variance_partition_alloc(cpi);
2555
2556
0
  if (cm->current_frame.frame_type == KEY_FRAME) copy_frame_prob_info(cpi);
2557
2558
#if CONFIG_COLLECT_COMPONENT_TIMING
2559
  printf("\n Encoding a frame: \n");
2560
#endif
2561
2562
0
#if !CONFIG_RD_COMMAND
2563
  // Determine whether to use screen content tools using two fast encoding.
2564
0
  if (!cpi->sf.hl_sf.disable_extra_sc_testing)
2565
0
    av1_determine_sc_tools_with_encoding(cpi, q);
2566
0
#endif  // !CONFIG_RD_COMMAND
2567
2568
#if CONFIG_TUNE_VMAF
2569
  if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
2570
    av1_vmaf_neg_preprocessing(cpi, cpi->unscaled_source);
2571
  }
2572
#endif
2573
2574
#if CONFIG_TUNE_BUTTERAUGLI
2575
  cpi->butteraugli_info.recon_set = false;
2576
  int original_q = 0;
2577
#endif
2578
2579
#if CONFIG_FRAME_PARALLEL_ENCODE
2580
  cpi->num_frame_recode = 0;
2581
#endif
2582
  // Loop variables
2583
0
  int loop = 0;
2584
0
  int loop_count = 0;
2585
0
  int overshoot_seen = 0;
2586
0
  int undershoot_seen = 0;
2587
0
  int low_cr_seen = 0;
2588
0
  int last_loop_allow_hp = 0;
2589
2590
0
  do {
2591
0
    loop = 0;
2592
0
    int do_mv_stats_collection = 1;
2593
2594
    // if frame was scaled calculate global_motion_search again if already
2595
    // done
2596
0
    if (loop_count > 0 && cpi->source && gm_info->search_done) {
2597
0
      if (cpi->source->y_crop_width != cm->width ||
2598
0
          cpi->source->y_crop_height != cm->height) {
2599
0
        gm_info->search_done = 0;
2600
0
      }
2601
0
    }
2602
0
    cpi->source = av1_realloc_and_scale_if_required(
2603
0
        cm, cpi->unscaled_source, &cpi->scaled_source, EIGHTTAP_REGULAR, 0,
2604
0
        false, false, cpi->oxcf.border_in_pixels,
2605
0
        cpi->oxcf.tool_cfg.enable_global_motion);
2606
2607
#if CONFIG_TUNE_BUTTERAUGLI
2608
    if (oxcf->tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) {
2609
      if (loop_count == 0) {
2610
        original_q = q;
2611
        // TODO(sdeng): different q here does not make big difference. Use a
2612
        // faster pass instead.
2613
        q = 96;
2614
        av1_setup_butteraugli_source(cpi);
2615
      } else {
2616
        q = original_q;
2617
      }
2618
    }
2619
#endif
2620
2621
0
    if (cpi->unscaled_last_source != NULL) {
2622
0
      cpi->last_source = av1_realloc_and_scale_if_required(
2623
0
          cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
2624
0
          EIGHTTAP_REGULAR, 0, false, false, cpi->oxcf.border_in_pixels,
2625
0
          cpi->oxcf.tool_cfg.enable_global_motion);
2626
0
    }
2627
2628
#if CONFIG_FRAME_PARALLEL_ENCODE
2629
    int scale_references = 0;
2630
#if CONFIG_FPMT_TEST
2631
    scale_references =
2632
        cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE ? 1 : 0;
2633
#endif  // CONFIG_FPMT_TEST
2634
    if (scale_references ||
2635
        cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) {
2636
#else
2637
0
    {
2638
0
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
2639
0
      if (!frame_is_intra_only(cm)) {
2640
0
        if (loop_count > 0) {
2641
0
          release_scaled_references(cpi);
2642
0
        }
2643
0
        av1_scale_references(cpi, EIGHTTAP_REGULAR, 0, 0);
2644
0
      }
2645
0
    }
2646
2647
#if CONFIG_TUNE_VMAF
2648
    if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
2649
        oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
2650
      cpi->vmaf_info.original_qindex = q;
2651
      q = av1_get_vmaf_base_qindex(cpi, q);
2652
    }
2653
#endif
2654
2655
#if CONFIG_RD_COMMAND
2656
    RD_COMMAND *rd_command = &cpi->rd_command;
2657
    RD_OPTION option = rd_command->option_ls[rd_command->frame_index];
2658
    if (option == RD_OPTION_SET_Q || option == RD_OPTION_SET_Q_RDMULT) {
2659
      q = rd_command->q_index_ls[rd_command->frame_index];
2660
    }
2661
#endif  // CONFIG_RD_COMMAND
2662
2663
#if CONFIG_BITRATE_ACCURACY
2664
    if (cpi->vbr_rc_info.q_index_list_ready) {
2665
      q = cpi->vbr_rc_info.q_index_list[cpi->gf_frame_index];
2666
    }
2667
#endif
2668
0
    av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
2669
0
                      q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq);
2670
0
    av1_set_speed_features_qindex_dependent(cpi, oxcf->speed);
2671
2672
0
    if (q_cfg->deltaq_mode != NO_DELTA_Q || q_cfg->enable_chroma_deltaq)
2673
0
      av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
2674
0
                         cm->seq_params->bit_depth);
2675
2676
0
    av1_set_variance_partition_thresholds(cpi, q, 0);
2677
2678
    // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
2679
    //        cm->current_frame.frame_number, cm->show_frame, q,
2680
    //        cm->current_frame.frame_type, cm->superres_scale_denominator);
2681
2682
0
    if (loop_count == 0) {
2683
0
      av1_setup_frame(cpi);
2684
0
    } else if (get_primary_ref_frame_buf(cm) == NULL) {
2685
      // Base q-index may have changed, so we need to assign proper default coef
2686
      // probs before every iteration.
2687
0
      av1_default_coef_probs(cm);
2688
0
      av1_setup_frame_contexts(cm);
2689
0
    }
2690
2691
0
    if (q_cfg->aq_mode == VARIANCE_AQ) {
2692
0
      av1_vaq_frame_setup(cpi);
2693
0
    } else if (q_cfg->aq_mode == COMPLEXITY_AQ) {
2694
0
      av1_setup_in_frame_q_adj(cpi);
2695
0
    }
2696
2697
0
    if (cm->seg.enabled) {
2698
0
      if (!cm->seg.update_data && cm->prev_frame) {
2699
0
        segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
2700
0
        cm->seg.enabled = cm->prev_frame->seg.enabled;
2701
0
      } else {
2702
0
        av1_calculate_segdata(&cm->seg);
2703
0
      }
2704
0
    } else {
2705
0
      memset(&cm->seg, 0, sizeof(cm->seg));
2706
0
    }
2707
0
    segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
2708
0
    cm->cur_frame->seg.enabled = cm->seg.enabled;
2709
2710
#if CONFIG_COLLECT_COMPONENT_TIMING
2711
    start_timing(cpi, av1_encode_frame_time);
2712
#endif
2713
    // Set the motion vector precision based on mv stats from the last coded
2714
    // frame.
2715
0
    if (!frame_is_intra_only(cm)) {
2716
0
      av1_pick_and_set_high_precision_mv(cpi, q);
2717
2718
      // If the precision has changed during different iteration of the loop,
2719
      // then we need to reset the global motion vectors
2720
0
      if (loop_count > 0 &&
2721
0
          cm->features.allow_high_precision_mv != last_loop_allow_hp) {
2722
0
        gm_info->search_done = 0;
2723
0
      }
2724
0
      last_loop_allow_hp = cm->features.allow_high_precision_mv;
2725
0
    }
2726
2727
    // transform / motion compensation build reconstruction frame
2728
0
    av1_encode_frame(cpi);
2729
2730
#if CONFIG_FRAME_PARALLEL_ENCODE
2731
    // Disable mv_stats collection for parallel frames based on update flag.
2732
    if (!cpi->do_frame_data_update) do_mv_stats_collection = 0;
2733
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
2734
2735
    // Reset the mv_stats in case we are interrupted by an intraframe or an
2736
    // overlay frame.
2737
0
    if (cpi->ppi->mv_stats.valid && do_mv_stats_collection) {
2738
#if CONFIG_FRAME_PARALLEL_ENCODE
2739
      av1_zero(cpi->mv_stats);
2740
#else
2741
0
      av1_zero(cpi->ppi->mv_stats);
2742
0
#endif
2743
0
    }
2744
    // Gather the mv_stats for the next frame
2745
0
    if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
2746
0
        av1_frame_allows_smart_mv(cpi) && do_mv_stats_collection) {
2747
0
      av1_collect_mv_stats(cpi, q);
2748
0
    }
2749
2750
#if CONFIG_COLLECT_COMPONENT_TIMING
2751
    end_timing(cpi, av1_encode_frame_time);
2752
#endif
2753
2754
#if CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND
2755
    const int do_dummy_pack = 1;
2756
#else   // CONFIG_BITRATE_ACCURACY
2757
    // Dummy pack of the bitstream using up to date stats to get an
2758
    // accurate estimate of output frame size to determine if we need
2759
    // to recode.
2760
0
    const int do_dummy_pack =
2761
0
        (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
2762
0
         oxcf->rc_cfg.mode != AOM_Q) ||
2763
0
        oxcf->rc_cfg.min_cr > 0;
2764
0
#endif  // CONFIG_BITRATE_ACCURACY
2765
0
    if (do_dummy_pack) {
2766
0
      av1_finalize_encoded_frame(cpi);
2767
0
      int largest_tile_id = 0;  // Output from bitstream: unused here
2768
0
      rc->coefficient_size = 0;
2769
0
      if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) !=
2770
0
          AOM_CODEC_OK) {
2771
0
        return AOM_CODEC_ERROR;
2772
0
      }
2773
2774
#if CONFIG_BITRATE_ACCURACY
2775
      cpi->vbr_rc_info.actual_coeff_bitrate_byframe[cpi->gf_frame_index] =
2776
          rc->coefficient_size;
2777
#endif
2778
2779
      // bits used for this frame
2780
0
      rc->projected_frame_size = (int)(*size) << 3;
2781
#if CONFIG_RD_COMMAND
2782
      PSNR_STATS psnr;
2783
      aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
2784
      printf("q %d rdmult %d rate %d dist %" PRIu64 "\n", q, cpi->rd.RDMULT,
2785
             rc->projected_frame_size, psnr.sse[0]);
2786
      ++rd_command->frame_index;
2787
      if (rd_command->frame_index == rd_command->frame_count) {
2788
        exit(0);
2789
      }
2790
#endif  // CONFIG_RD_COMMAND
2791
2792
#if CONFIG_BITRATE_ACCURACY
2793
      cpi->vbr_rc_info.actual_bitrate_byframe[cpi->gf_frame_index] =
2794
          rc->projected_frame_size;
2795
      cpi->vbr_rc_info.actual_mv_bitrate_byframe[cpi->gf_frame_index] =
2796
          rc->projected_frame_size -
2797
          cpi->vbr_rc_info.actual_coeff_bitrate_byframe[cpi->gf_frame_index];
2798
#if 0
2799
      vbr_rc_info_log(&cpi->vbr_rc_info, cpi->gf_frame_index,
2800
                      cpi->ppi->gf_group.size, cpi->ppi->gf_group.update_type);
2801
#endif
2802
2803
#endif
2804
0
    }
2805
2806
#if CONFIG_TUNE_VMAF
2807
    if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING &&
2808
        oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) {
2809
      q = cpi->vmaf_info.original_qindex;
2810
    }
2811
#endif
2812
0
    if (allow_recode) {
2813
      // Update q and decide whether to do a recode loop
2814
0
      recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
2815
0
                           bottom_index, &undershoot_seen, &overshoot_seen,
2816
0
                           &low_cr_seen, loop_count);
2817
0
    }
2818
2819
#if CONFIG_TUNE_BUTTERAUGLI
2820
    if (loop_count == 0 && oxcf->tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) {
2821
      loop = 1;
2822
      av1_setup_butteraugli_rdmult_and_restore_source(cpi, 0.4);
2823
    }
2824
#endif
2825
2826
#if CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND
2827
    loop = 0;  // turn off recode loop when CONFIG_BITRATE_ACCURACY is on
2828
#endif         // CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND
2829
2830
0
    if (loop) {
2831
0
      ++loop_count;
2832
#if CONFIG_FRAME_PARALLEL_ENCODE
2833
      cpi->num_frame_recode =
2834
          (cpi->num_frame_recode < (NUM_RECODES_PER_FRAME - 1))
2835
              ? (cpi->num_frame_recode + 1)
2836
              : (NUM_RECODES_PER_FRAME - 1);
2837
#endif
2838
#if CONFIG_INTERNAL_STATS
2839
      ++cpi->frame_recode_hits;
2840
#endif
2841
0
    }
2842
#if CONFIG_COLLECT_COMPONENT_TIMING
2843
    if (loop) printf("\n Recoding:");
2844
#endif
2845
0
  } while (loop);
2846
2847
0
  return AOM_CODEC_OK;
2848
0
}
2849
#endif  // !CONFIG_REALTIME_ONLY
2850
2851
// TODO(jingning, paulwilkins): Set up high grain level to test
2852
// hardware decoders. Need to adapt the actual noise variance
2853
// according to the difference between reconstructed frame and the
2854
// source signal.
2855
0
static void set_grain_syn_params(AV1_COMMON *cm) {
2856
0
  aom_film_grain_t *film_grain_params = &cm->film_grain_params;
2857
0
  film_grain_params->apply_grain = 1;
2858
0
  film_grain_params->update_parameters = 1;
2859
0
  film_grain_params->random_seed = rand() & 0xffff;
2860
2861
0
  film_grain_params->num_y_points = 1;
2862
0
  film_grain_params->scaling_points_y[0][0] = 128;
2863
0
  film_grain_params->scaling_points_y[0][1] = 100;
2864
2865
0
  film_grain_params->num_cb_points = 1;
2866
0
  film_grain_params->scaling_points_cb[0][0] = 128;
2867
0
  film_grain_params->scaling_points_cb[0][1] = 100;
2868
2869
0
  film_grain_params->num_cr_points = 1;
2870
0
  film_grain_params->scaling_points_cr[0][0] = 128;
2871
0
  film_grain_params->scaling_points_cr[0][1] = 100;
2872
2873
0
  film_grain_params->chroma_scaling_from_luma = 0;
2874
0
  film_grain_params->scaling_shift = 1;
2875
0
  film_grain_params->ar_coeff_lag = 0;
2876
0
  film_grain_params->ar_coeff_shift = 1;
2877
0
  film_grain_params->overlap_flag = 1;
2878
0
  film_grain_params->grain_scale_shift = 0;
2879
0
}
2880
2881
/*!\brief Recode loop or a single loop for encoding one frame, followed by
2882
 * in-loop deblocking filters, CDEF filters, and restoration filters.
2883
 *
2884
 * \ingroup high_level_algo
2885
 * \callgraph
2886
 * \callergraph
2887
 *
2888
 * \param[in]    cpi             Top-level encoder structure
2889
 * \param[in]    size            Bitstream size
2890
 * \param[in]    dest            Bitstream output
2891
 * \param[in]    sse             Total distortion of the frame
2892
 * \param[in]    rate            Total rate of the frame
2893
 * \param[in]    largest_tile_id Tile id of the last tile
2894
 *
2895
 * \return Returns a value to indicate if the encoding is done successfully.
2896
 * \retval #AOM_CODEC_OK
2897
 * \retval #AOM_CODEC_ERROR
2898
 */
2899
static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
2900
                                              uint8_t *dest, int64_t *sse,
2901
                                              int64_t *rate,
2902
1.26k
                                              int *largest_tile_id) {
2903
#if CONFIG_COLLECT_COMPONENT_TIMING
2904
  start_timing(cpi, encode_with_or_without_recode_time);
2905
#endif
2906
#if CONFIG_FRAME_PARALLEL_ENCODE
2907
  for (int i = 0; i < NUM_RECODES_PER_FRAME; i++) {
2908
    cpi->do_update_frame_probs_txtype[i] = 0;
2909
    cpi->do_update_frame_probs_obmc[i] = 0;
2910
    cpi->do_update_frame_probs_warp[i] = 0;
2911
    cpi->do_update_frame_probs_interpfilter[i] = 0;
2912
  }
2913
  cpi->do_update_vbr_bits_off_target_fast = 0;
2914
#endif
2915
2916
1.26k
  int err;
2917
#if CONFIG_REALTIME_ONLY
2918
  err = encode_without_recode(cpi);
2919
#else
2920
1.26k
  if (cpi->sf.hl_sf.recode_loop == DISALLOW_RECODE)
2921
1.26k
    err = encode_without_recode(cpi);
2922
0
  else
2923
0
    err = encode_with_recode_loop(cpi, size, dest);
2924
1.26k
#endif
2925
#if CONFIG_COLLECT_COMPONENT_TIMING
2926
  end_timing(cpi, encode_with_or_without_recode_time);
2927
#endif
2928
1.26k
  if (err != AOM_CODEC_OK) {
2929
0
    if (err == -1) {
2930
      // special case as described in encode_with_recode_loop().
2931
      // Encoding was skipped.
2932
0
      err = AOM_CODEC_OK;
2933
0
      if (sse != NULL) *sse = INT64_MAX;
2934
0
      if (rate != NULL) *rate = INT64_MAX;
2935
0
      *largest_tile_id = 0;
2936
0
    }
2937
0
    return err;
2938
0
  }
2939
2940
#ifdef OUTPUT_YUV_DENOISED
2941
  const AV1EncoderConfig *const oxcf = &cpi->oxcf;
2942
  if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) {
2943
    aom_write_yuv_frame(yuv_denoised_file,
2944
                        &cpi->denoiser.running_avg_y[INTRA_FRAME]);
2945
  }
2946
#endif
2947
2948
1.26k
  AV1_COMMON *const cm = &cpi->common;
2949
1.26k
  SequenceHeader *const seq_params = cm->seq_params;
2950
2951
  // Special case code to reduce pulsing when key frames are forced at a
2952
  // fixed interval. Note the reconstruction error if it is the frame before
2953
  // the force key frame
2954
1.26k
  if (cpi->ppi->p_rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
2955
0
#if CONFIG_AV1_HIGHBITDEPTH
2956
0
    if (seq_params->use_highbitdepth) {
2957
0
      cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
2958
0
    } else {
2959
0
      cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
2960
0
    }
2961
#else
2962
    cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
2963
#endif
2964
0
  }
2965
2966
1.26k
  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
2967
1.26k
  cm->cur_frame->buf.transfer_characteristics =
2968
1.26k
      seq_params->transfer_characteristics;
2969
1.26k
  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
2970
1.26k
  cm->cur_frame->buf.monochrome = seq_params->monochrome;
2971
1.26k
  cm->cur_frame->buf.chroma_sample_position =
2972
1.26k
      seq_params->chroma_sample_position;
2973
1.26k
  cm->cur_frame->buf.color_range = seq_params->color_range;
2974
1.26k
  cm->cur_frame->buf.render_width = cm->render_width;
2975
1.26k
  cm->cur_frame->buf.render_height = cm->render_height;
2976
2977
  // Pick the loop filter level for the frame.
2978
1.26k
  if (!cm->features.allow_intrabc) {
2979
1.26k
    loopfilter_frame(cpi, cm);
2980
1.26k
  } else {
2981
0
    cm->lf.filter_level[0] = 0;
2982
0
    cm->lf.filter_level[1] = 0;
2983
0
    cm->cdef_info.cdef_bits = 0;
2984
0
    cm->cdef_info.cdef_strengths[0] = 0;
2985
0
    cm->cdef_info.nb_cdef_strengths = 1;
2986
0
    cm->cdef_info.cdef_uv_strengths[0] = 0;
2987
0
    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
2988
0
    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
2989
0
    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
2990
0
  }
2991
2992
  // TODO(debargha): Fix mv search range on encoder side
2993
  // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
2994
1.26k
  aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
2995
2996
#ifdef OUTPUT_YUV_REC
2997
  aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
2998
#endif
2999
3000
1.26k
  if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_FILM) {
3001
0
    set_grain_syn_params(cm);
3002
0
  }
3003
3004
1.26k
  av1_finalize_encoded_frame(cpi);
3005
  // Build the bitstream
3006
#if CONFIG_COLLECT_COMPONENT_TIMING
3007
  start_timing(cpi, av1_pack_bitstream_final_time);
3008
#endif
3009
1.26k
  cpi->rc.coefficient_size = 0;
3010
1.26k
  if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
3011
0
    return AOM_CODEC_ERROR;
3012
#if CONFIG_COLLECT_COMPONENT_TIMING
3013
  end_timing(cpi, av1_pack_bitstream_final_time);
3014
#endif
3015
3016
  // Compute sse and rate.
3017
1.26k
  if (sse != NULL) {
3018
0
#if CONFIG_AV1_HIGHBITDEPTH
3019
0
    *sse = (seq_params->use_highbitdepth)
3020
0
               ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
3021
0
               : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
3022
#else
3023
    *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
3024
#endif
3025
0
  }
3026
1.26k
  if (rate != NULL) {
3027
0
    const int64_t bits = (*size << 3);
3028
0
    *rate = (bits << 5);  // To match scale.
3029
0
  }
3030
1.26k
  return AOM_CODEC_OK;
3031
1.26k
}
3032
3033
static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
3034
                                            uint8_t *dest,
3035
0
                                            int *largest_tile_id) {
3036
0
  const AV1_COMMON *const cm = &cpi->common;
3037
0
  assert(cm->seq_params->enable_superres);
3038
0
  assert(av1_superres_in_recode_allowed(cpi));
3039
0
  aom_codec_err_t err = AOM_CODEC_OK;
3040
0
  av1_save_all_coding_context(cpi);
3041
3042
0
  int64_t sse1 = INT64_MAX;
3043
0
  int64_t rate1 = INT64_MAX;
3044
0
  int largest_tile_id1 = 0;
3045
0
  int64_t sse2 = INT64_MAX;
3046
0
  int64_t rate2 = INT64_MAX;
3047
0
  int largest_tile_id2;
3048
0
  double proj_rdcost1 = DBL_MAX;
3049
0
  const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
3050
0
  const FRAME_UPDATE_TYPE update_type =
3051
0
      gf_group->update_type[cpi->gf_frame_index];
3052
0
  const aom_bit_depth_t bit_depth = cm->seq_params->bit_depth;
3053
3054
  // Encode with superres.
3055
0
  if (cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_ALL) {
3056
0
    SuperResCfg *const superres_cfg = &cpi->oxcf.superres_cfg;
3057
0
    int64_t superres_sses[SCALE_NUMERATOR];
3058
0
    int64_t superres_rates[SCALE_NUMERATOR];
3059
0
    int superres_largest_tile_ids[SCALE_NUMERATOR];
3060
    // Use superres for Key-frames and Alt-ref frames only.
3061
0
    if (update_type != OVERLAY_UPDATE && update_type != INTNL_OVERLAY_UPDATE) {
3062
0
      for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
3063
0
           ++denom) {
3064
0
        superres_cfg->superres_scale_denominator = denom;
3065
0
        superres_cfg->superres_kf_scale_denominator = denom;
3066
0
        const int this_index = denom - (SCALE_NUMERATOR + 1);
3067
3068
0
        cpi->superres_mode = AOM_SUPERRES_AUTO;  // Super-res on for this loop.
3069
0
        err = encode_with_recode_loop_and_filter(
3070
0
            cpi, size, dest, &superres_sses[this_index],
3071
0
            &superres_rates[this_index],
3072
0
            &superres_largest_tile_ids[this_index]);
3073
0
        cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
3074
0
        if (err != AOM_CODEC_OK) return err;
3075
0
        restore_all_coding_context(cpi);
3076
0
      }
3077
      // Reset.
3078
0
      superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
3079
0
      superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
3080
0
    } else {
3081
0
      for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
3082
0
           ++denom) {
3083
0
        const int this_index = denom - (SCALE_NUMERATOR + 1);
3084
0
        superres_sses[this_index] = INT64_MAX;
3085
0
        superres_rates[this_index] = INT64_MAX;
3086
0
      }
3087
0
    }
3088
    // Encode without superres.
3089
0
    assert(cpi->superres_mode == AOM_SUPERRES_NONE);
3090
0
    err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
3091
0
                                             &largest_tile_id2);
3092
0
    if (err != AOM_CODEC_OK) return err;
3093
3094
    // Note: Both use common rdmult based on base qindex of fullres.
3095
0
    const int64_t rdmult = av1_compute_rd_mult_based_on_qindex(
3096
0
        bit_depth, update_type, cm->quant_params.base_qindex);
3097
3098
    // Find the best rdcost among all superres denoms.
3099
0
    int best_denom = -1;
3100
0
    for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
3101
0
         ++denom) {
3102
0
      const int this_index = denom - (SCALE_NUMERATOR + 1);
3103
0
      const int64_t this_sse = superres_sses[this_index];
3104
0
      const int64_t this_rate = superres_rates[this_index];
3105
0
      const int this_largest_tile_id = superres_largest_tile_ids[this_index];
3106
0
      const double this_rdcost = RDCOST_DBL_WITH_NATIVE_BD_DIST(
3107
0
          rdmult, this_rate, this_sse, bit_depth);
3108
0
      if (this_rdcost < proj_rdcost1) {
3109
0
        sse1 = this_sse;
3110
0
        rate1 = this_rate;
3111
0
        largest_tile_id1 = this_largest_tile_id;
3112
0
        proj_rdcost1 = this_rdcost;
3113
0
        best_denom = denom;
3114
0
      }
3115
0
    }
3116
0
    const double proj_rdcost2 =
3117
0
        RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate2, sse2, bit_depth);
3118
    // Re-encode with superres if it's better.
3119
0
    if (proj_rdcost1 < proj_rdcost2) {
3120
0
      restore_all_coding_context(cpi);
3121
      // TODO(urvang): We should avoid rerunning the recode loop by saving
3122
      // previous output+state, or running encode only for the selected 'q' in
3123
      // previous step.
3124
      // Again, temporarily force the best denom.
3125
0
      superres_cfg->superres_scale_denominator = best_denom;
3126
0
      superres_cfg->superres_kf_scale_denominator = best_denom;
3127
0
      int64_t sse3 = INT64_MAX;
3128
0
      int64_t rate3 = INT64_MAX;
3129
0
      cpi->superres_mode =
3130
0
          AOM_SUPERRES_AUTO;  // Super-res on for this recode loop.
3131
0
      err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
3132
0
                                               largest_tile_id);
3133
0
      cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
3134
0
      assert(sse1 == sse3);
3135
0
      assert(rate1 == rate3);
3136
0
      assert(largest_tile_id1 == *largest_tile_id);
3137
      // Reset.
3138
0
      superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
3139
0
      superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
3140
0
    } else {
3141
0
      *largest_tile_id = largest_tile_id2;
3142
0
    }
3143
0
  } else {
3144
0
    assert(cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_DUAL);
3145
0
    cpi->superres_mode =
3146
0
        AOM_SUPERRES_AUTO;  // Super-res on for this recode loop.
3147
0
    err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
3148
0
                                             &largest_tile_id1);
3149
0
    cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
3150
0
    if (err != AOM_CODEC_OK) return err;
3151
0
    restore_all_coding_context(cpi);
3152
    // Encode without superres.
3153
0
    assert(cpi->superres_mode == AOM_SUPERRES_NONE);
3154
0
    err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
3155
0
                                             &largest_tile_id2);
3156
0
    if (err != AOM_CODEC_OK) return err;
3157
3158
    // Note: Both use common rdmult based on base qindex of fullres.
3159
0
    const int64_t rdmult = av1_compute_rd_mult_based_on_qindex(
3160
0
        bit_depth, update_type, cm->quant_params.base_qindex);
3161
0
    proj_rdcost1 =
3162
0
        RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate1, sse1, bit_depth);
3163
0
    const double proj_rdcost2 =
3164
0
        RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate2, sse2, bit_depth);
3165
    // Re-encode with superres if it's better.
3166
0
    if (proj_rdcost1 < proj_rdcost2) {
3167
0
      restore_all_coding_context(cpi);
3168
      // TODO(urvang): We should avoid rerunning the recode loop by saving
3169
      // previous output+state, or running encode only for the selected 'q' in
3170
      // previous step.
3171
0
      int64_t sse3 = INT64_MAX;
3172
0
      int64_t rate3 = INT64_MAX;
3173
0
      cpi->superres_mode =
3174
0
          AOM_SUPERRES_AUTO;  // Super-res on for this recode loop.
3175
0
      err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
3176
0
                                               largest_tile_id);
3177
0
      cpi->superres_mode = AOM_SUPERRES_NONE;  // Reset to default (full-res).
3178
0
      assert(sse1 == sse3);
3179
0
      assert(rate1 == rate3);
3180
0
      assert(largest_tile_id1 == *largest_tile_id);
3181
0
    } else {
3182
0
      *largest_tile_id = largest_tile_id2;
3183
0
    }
3184
0
  }
3185
3186
0
  return err;
3187
0
}
3188
3189
// Conditions to disable cdf_update mode in selective mode for real-time.
3190
// Handle case for layers, scene change, and resizing.
3191
0
static int selective_disable_cdf_rtc(AV1_COMP *cpi) {
3192
0
  AV1_COMMON *const cm = &cpi->common;
3193
0
  RATE_CONTROL *const rc = &cpi->rc;
3194
  // For single layer.
3195
0
  if (cpi->svc.number_spatial_layers == 1 &&
3196
0
      cpi->svc.number_temporal_layers == 1) {
3197
    // Don't disable on intra_only, scene change (high_source_sad = 1),
3198
    // or resized frame. To avoid quality loss for now, force enable at
3199
    // every 8 frames.
3200
0
    if (frame_is_intra_only(cm) || is_frame_resize_pending(cpi) ||
3201
0
        rc->high_source_sad || rc->frames_since_key < 10 ||
3202
0
        cm->current_frame.frame_number % 8 == 0)
3203
0
      return 0;
3204
0
    else
3205
0
      return 1;
3206
0
  } else if (cpi->svc.number_temporal_layers > 1) {
3207
    // Disable only on top temporal enhancement layer for now.
3208
0
    return cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1;
3209
0
  }
3210
0
  return 1;
3211
0
}
3212
3213
#if !CONFIG_REALTIME_ONLY
3214
static void subtract_stats(FIRSTPASS_STATS *section,
3215
0
                           const FIRSTPASS_STATS *frame) {
3216
0
  section->frame -= frame->frame;
3217
0
  section->weight -= frame->weight;
3218
0
  section->intra_error -= frame->intra_error;
3219
0
  section->frame_avg_wavelet_energy -= frame->frame_avg_wavelet_energy;
3220
0
  section->coded_error -= frame->coded_error;
3221
0
  section->sr_coded_error -= frame->sr_coded_error;
3222
0
  section->pcnt_inter -= frame->pcnt_inter;
3223
0
  section->pcnt_motion -= frame->pcnt_motion;
3224
0
  section->pcnt_second_ref -= frame->pcnt_second_ref;
3225
0
  section->pcnt_neutral -= frame->pcnt_neutral;
3226
0
  section->intra_skip_pct -= frame->intra_skip_pct;
3227
0
  section->inactive_zone_rows -= frame->inactive_zone_rows;
3228
0
  section->inactive_zone_cols -= frame->inactive_zone_cols;
3229
0
  section->MVr -= frame->MVr;
3230
0
  section->mvr_abs -= frame->mvr_abs;
3231
0
  section->MVc -= frame->MVc;
3232
0
  section->mvc_abs -= frame->mvc_abs;
3233
0
  section->MVrv -= frame->MVrv;
3234
0
  section->MVcv -= frame->MVcv;
3235
0
  section->mv_in_out_count -= frame->mv_in_out_count;
3236
0
  section->new_mv_count -= frame->new_mv_count;
3237
0
  section->count -= frame->count;
3238
0
  section->duration -= frame->duration;
3239
0
}
3240
3241
1.26k
static void calculate_frame_avg_haar_energy(AV1_COMP *cpi) {
3242
1.26k
  TWO_PASS *const twopass = &cpi->ppi->twopass;
3243
1.26k
  const FIRSTPASS_STATS *const total_stats =
3244
1.26k
      twopass->stats_buf_ctx->total_stats;
3245
3246
1.26k
  if (is_one_pass_rt_params(cpi) ||
3247
1.26k
      (cpi->oxcf.q_cfg.deltaq_mode != DELTA_Q_PERCEPTUAL) ||
3248
1.26k
      (is_fp_wavelet_energy_invalid(total_stats) == 0))
3249
1.26k
    return;
3250
3251
0
  const int num_mbs = (cpi->oxcf.resize_cfg.resize_mode != RESIZE_NONE)
3252
0
                          ? cpi->initial_mbs
3253
0
                          : cpi->common.mi_params.MBs;
3254
0
  const YV12_BUFFER_CONFIG *const unfiltered_source = cpi->unfiltered_source;
3255
0
  const uint8_t *const src = unfiltered_source->y_buffer;
3256
0
  const int hbd = unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH;
3257
0
  const int stride = unfiltered_source->y_stride;
3258
0
  const BLOCK_SIZE fp_block_size =
3259
0
      get_fp_block_size(cpi->is_screen_content_type);
3260
0
  const int fp_block_size_width = block_size_wide[fp_block_size];
3261
0
  const int fp_block_size_height = block_size_high[fp_block_size];
3262
0
  const int num_unit_cols =
3263
0
      get_num_blocks(unfiltered_source->y_crop_width, fp_block_size_width);
3264
0
  const int num_unit_rows =
3265
0
      get_num_blocks(unfiltered_source->y_crop_height, fp_block_size_height);
3266
0
  const int num_8x8_cols = num_unit_cols * (fp_block_size_width / 8);
3267
0
  const int num_8x8_rows = num_unit_rows * (fp_block_size_height / 8);
3268
0
  int64_t frame_avg_wavelet_energy = av1_haar_ac_sad_mxn_uint8_input(
3269
0
      src, stride, hbd, num_8x8_rows, num_8x8_cols);
3270
3271
0
  cpi->twopass_frame.frame_avg_haar_energy =
3272
0
      log(((double)frame_avg_wavelet_energy / num_mbs) + 1.0);
3273
0
}
3274
#endif
3275
3276
extern void av1_print_frame_contexts(const FRAME_CONTEXT *fc,
3277
                                     const char *filename);
3278
3279
/*!\brief Run the final pass encoding for 1-pass/2-pass encoding mode, and pack
3280
 * the bitstream
3281
 *
3282
 * \ingroup high_level_algo
3283
 * \callgraph
3284
 * \callergraph
3285
 *
3286
 * \param[in]    cpi             Top-level encoder structure
3287
 * \param[in]    size            Bitstream size
3288
 * \param[in]    dest            Bitstream output
3289
 *
3290
 * \return Returns a value to indicate if the encoding is done successfully.
3291
 * \retval #AOM_CODEC_OK
3292
 * \retval #AOM_CODEC_ERROR
3293
 */
3294
static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
3295
1.26k
                                     uint8_t *dest) {
3296
1.26k
  AV1_COMMON *const cm = &cpi->common;
3297
1.26k
  SequenceHeader *const seq_params = cm->seq_params;
3298
1.26k
  CurrentFrame *const current_frame = &cm->current_frame;
3299
1.26k
  const AV1EncoderConfig *const oxcf = &cpi->oxcf;
3300
1.26k
  struct segmentation *const seg = &cm->seg;
3301
1.26k
  FeatureFlags *const features = &cm->features;
3302
1.26k
  const TileConfig *const tile_cfg = &oxcf->tile_cfg;
3303
3304
#if CONFIG_COLLECT_COMPONENT_TIMING
3305
  start_timing(cpi, encode_frame_to_data_rate_time);
3306
#endif
3307
3308
1.26k
  if (frame_is_intra_only(cm)) {
3309
1.26k
    av1_set_screen_content_options(cpi, features);
3310
1.26k
  }
3311
3312
1.26k
#if !CONFIG_REALTIME_ONLY
3313
1.26k
  calculate_frame_avg_haar_energy(cpi);
3314
1.26k
#endif
3315
3316
  // frame type has been decided outside of this function call
3317
1.26k
  cm->cur_frame->frame_type = current_frame->frame_type;
3318
3319
1.26k
  cm->tiles.large_scale = tile_cfg->enable_large_scale_tile;
3320
1.26k
  cm->tiles.single_tile_decoding = tile_cfg->enable_single_tile_decoding;
3321
3322
1.26k
  features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
3323
  // features->allow_ref_frame_mvs needs to be written into the frame header
3324
  // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case
3325
  // is separated from frame_might_allow_ref_frame_mvs().
3326
1.26k
  features->allow_ref_frame_mvs &= !cm->tiles.large_scale;
3327
3328
1.26k
  features->allow_warped_motion = oxcf->motion_mode_cfg.allow_warped_motion &&
3329
1.26k
                                  frame_might_allow_warped_motion(cm);
3330
3331
1.26k
  cpi->last_frame_type = current_frame->frame_type;
3332
3333
1.26k
  if (frame_is_sframe(cm)) {
3334
0
    GF_GROUP *gf_group = &cpi->ppi->gf_group;
3335
    // S frame will wipe out any previously encoded altref so we cannot place
3336
    // an overlay frame
3337
0
    gf_group->update_type[gf_group->size] = GF_UPDATE;
3338
0
  }
3339
3340
1.26k
  if (encode_show_existing_frame(cm)) {
3341
0
    av1_finalize_encoded_frame(cpi);
3342
    // Build the bitstream
3343
0
    int largest_tile_id = 0;  // Output from bitstream: unused here
3344
0
    cpi->rc.coefficient_size = 0;
3345
0
    if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
3346
0
      return AOM_CODEC_ERROR;
3347
3348
0
    if (seq_params->frame_id_numbers_present_flag &&
3349
0
        current_frame->frame_type == KEY_FRAME) {
3350
      // Displaying a forward key-frame, so reset the ref buffer IDs
3351
0
      int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
3352
0
      for (int i = 0; i < REF_FRAMES; i++)
3353
0
        cm->ref_frame_id[i] = display_frame_id;
3354
0
    }
3355
3356
#if DUMP_RECON_FRAMES == 1
3357
    // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
3358
    av1_dump_filtered_recon_frames(cpi);
3359
#endif  // DUMP_RECON_FRAMES
3360
3361
    // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3362
    //       for the purpose to verify no mismatch between encoder and decoder.
3363
0
    if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
3364
3365
#if CONFIG_AV1_TEMPORAL_DENOISING
3366
    av1_denoiser_update_ref_frame(cpi);
3367
#endif
3368
3369
    // Since we allocate a spot for the OVERLAY frame in the gf group, we need
3370
    // to do post-encoding update accordingly.
3371
0
    av1_set_target_rate(cpi, cm->width, cm->height);
3372
3373
0
    if (is_psnr_calc_enabled(cpi)) {
3374
0
      cpi->source =
3375
0
          realloc_and_scale_source(cpi, cm->cur_frame->buf.y_crop_width,
3376
0
                                   cm->cur_frame->buf.y_crop_height);
3377
0
    }
3378
3379
0
    ++current_frame->frame_number;
3380
0
    update_frame_index_set(&cpi->frame_index_set, cm->show_frame);
3381
0
    return AOM_CODEC_OK;
3382
0
  }
3383
3384
  // Work out whether to force_integer_mv this frame
3385
1.26k
  if (!is_stat_generation_stage(cpi) &&
3386
1.26k
      cpi->common.features.allow_screen_content_tools &&
3387
1.26k
      !frame_is_intra_only(cm)) {
3388
0
    if (cpi->common.seq_params->force_integer_mv == 2) {
3389
      // Adaptive mode: see what previous frame encoded did
3390
0
      if (cpi->unscaled_last_source != NULL) {
3391
0
        features->cur_frame_force_integer_mv = av1_is_integer_mv(
3392
0
            cpi->source, cpi->unscaled_last_source, &cpi->force_intpel_info);
3393
0
      } else {
3394
0
        cpi->common.features.cur_frame_force_integer_mv = 0;
3395
0
      }
3396
0
    } else {
3397
0
      cpi->common.features.cur_frame_force_integer_mv =
3398
0
          cpi->common.seq_params->force_integer_mv;
3399
0
    }
3400
1.26k
  } else {
3401
1.26k
    cpi->common.features.cur_frame_force_integer_mv = 0;
3402
1.26k
  }
3403
3404
  // Set default state for segment based loop filter update flags.
3405
1.26k
  cm->lf.mode_ref_delta_update = 0;
3406
3407
  // Set various flags etc to special state if it is a key frame.
3408
1.26k
  if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
3409
    // Reset the loop filter deltas and segmentation map.
3410
1.26k
    av1_reset_segment_features(cm);
3411
3412
    // If segmentation is enabled force a map update for key frames.
3413
1.26k
    if (seg->enabled) {
3414
0
      seg->update_map = 1;
3415
0
      seg->update_data = 1;
3416
0
    }
3417
1.26k
  }
3418
1.26k
  if (tile_cfg->mtu == 0) {
3419
1.26k
    cpi->num_tg = tile_cfg->num_tile_groups;
3420
1.26k
  } else {
3421
    // Use a default value for the purposes of weighting costs in probability
3422
    // updates
3423
0
    cpi->num_tg = DEFAULT_MAX_NUM_TG;
3424
0
  }
3425
3426
  // For 1 pass CBR, check if we are dropping this frame.
3427
  // Never drop on key frame.
3428
1.26k
  if (has_no_stats_stage(cpi) && oxcf->rc_cfg.mode == AOM_CBR &&
3429
1.26k
      current_frame->frame_type != KEY_FRAME) {
3430
0
    if (cpi->oxcf.rc_cfg.target_bandwidth == 0 || av1_rc_drop_frame(cpi)) {
3431
0
      av1_setup_frame_size(cpi);
3432
0
      av1_set_mv_search_params(cpi);
3433
0
      av1_rc_postencode_update_drop_frame(cpi);
3434
0
      release_scaled_references(cpi);
3435
0
      cpi->is_dropped_frame = true;
3436
0
      return AOM_CODEC_OK;
3437
0
    }
3438
0
  }
3439
3440
1.26k
  if (oxcf->tune_cfg.tuning == AOM_TUNE_SSIM) {
3441
1.26k
    av1_set_mb_ssim_rdmult_scaling(cpi);
3442
1.26k
  }
3443
3444
#if CONFIG_TUNE_VMAF
3445
  if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
3446
      oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN ||
3447
      oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) {
3448
    av1_set_mb_vmaf_rdmult_scaling(cpi);
3449
  }
3450
#endif
3451
3452
1.26k
  if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL_AI &&
3453
1.26k
      cpi->sf.rt_sf.use_nonrd_pick_mode == 0) {
3454
0
    av1_init_mb_wiener_var_buffer(cpi);
3455
0
    av1_set_mb_wiener_variance(cpi);
3456
0
  }
3457
3458
1.26k
  if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_USER_RATING_BASED) {
3459
0
    av1_init_mb_ur_var_buffer(cpi);
3460
0
    av1_set_mb_ur_variance(cpi);
3461
0
  }
3462
3463
#if CONFIG_INTERNAL_STATS
3464
  memset(cpi->mode_chosen_counts, 0,
3465
         MAX_MODES * sizeof(*cpi->mode_chosen_counts));
3466
#endif
3467
3468
1.26k
  if (seq_params->frame_id_numbers_present_flag) {
3469
    /* Non-normative definition of current_frame_id ("frame counter" with
3470
     * wraparound) */
3471
0
    if (cm->current_frame_id == -1) {
3472
0
      int lsb, msb;
3473
      /* quasi-random initialization of current_frame_id for a key frame */
3474
0
      if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
3475
0
        lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
3476
0
        msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
3477
0
      } else {
3478
0
        lsb = cpi->source->y_buffer[0] & 0xff;
3479
0
        msb = cpi->source->y_buffer[1] & 0xff;
3480
0
      }
3481
0
      cm->current_frame_id =
3482
0
          ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
3483
3484
      // S_frame is meant for stitching different streams of different
3485
      // resolutions together, so current_frame_id must be the
3486
      // same across different streams of the same content current_frame_id
3487
      // should be the same and not random. 0x37 is a chosen number as start
3488
      // point
3489
0
      if (oxcf->kf_cfg.sframe_dist != 0) cm->current_frame_id = 0x37;
3490
0
    } else {
3491
0
      cm->current_frame_id =
3492
0
          (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
3493
0
          (1 << seq_params->frame_id_length);
3494
0
    }
3495
0
  }
3496
3497
1.26k
  switch (oxcf->algo_cfg.cdf_update_mode) {
3498
0
    case 0:  // No CDF update for any frames(4~6% compression loss).
3499
0
      features->disable_cdf_update = 1;
3500
0
      break;
3501
1.26k
    case 1:  // Enable CDF update for all frames.
3502
1.26k
      features->disable_cdf_update = 0;
3503
1.26k
      break;
3504
0
    case 2:
3505
      // Strategically determine at which frames to do CDF update.
3506
      // Currently only enable CDF update for all-intra and no-show frames(1.5%
3507
      // compression loss) for good qualiy or allintra mode.
3508
0
      if (oxcf->mode == GOOD || oxcf->mode == ALLINTRA) {
3509
0
        features->disable_cdf_update =
3510
0
            (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
3511
0
      } else {
3512
0
        features->disable_cdf_update = selective_disable_cdf_rtc(cpi);
3513
0
      }
3514
0
      break;
3515
1.26k
  }
3516
3517
#if CONFIG_FRAME_PARALLEL_ENCODE
3518
#if CONFIG_FRAME_PARALLEL_ENCODE_2
3519
  // Disable cdf update for the INTNL_ARF_UPDATE frame with
3520
  // frame_parallel_level 1.
3521
  if (!cpi->do_frame_data_update &&
3522
      cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) {
3523
    assert(cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 1);
3524
    features->disable_cdf_update = 1;
3525
  }
3526
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
3527
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
3528
3529
1.26k
  int largest_tile_id = 0;
3530
1.26k
  if (av1_superres_in_recode_allowed(cpi)) {
3531
0
    if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
3532
0
        AOM_CODEC_OK) {
3533
0
      return AOM_CODEC_ERROR;
3534
0
    }
3535
1.26k
  } else {
3536
1.26k
    const aom_superres_mode orig_superres_mode = cpi->superres_mode;  // save
3537
1.26k
    cpi->superres_mode = cpi->oxcf.superres_cfg.superres_mode;
3538
1.26k
    if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
3539
1.26k
                                           &largest_tile_id) != AOM_CODEC_OK) {
3540
0
      return AOM_CODEC_ERROR;
3541
0
    }
3542
1.26k
    cpi->superres_mode = orig_superres_mode;  // restore
3543
1.26k
  }
3544
3545
  // Update reference frame ids for reference frames this frame will overwrite
3546
1.26k
  if (seq_params->frame_id_numbers_present_flag) {
3547
0
    for (int i = 0; i < REF_FRAMES; i++) {
3548
0
      if ((current_frame->refresh_frame_flags >> i) & 1) {
3549
0
        cm->ref_frame_id[i] = cm->current_frame_id;
3550
0
      }
3551
0
    }
3552
0
  }
3553
3554
1.26k
  if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
3555
1.26k
    cpi->svc.num_encoded_top_layer++;
3556
3557
#if DUMP_RECON_FRAMES == 1
3558
  // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
3559
  av1_dump_filtered_recon_frames(cpi);
3560
#endif  // DUMP_RECON_FRAMES
3561
3562
1.26k
  if (cm->seg.enabled) {
3563
0
    if (cm->seg.update_map) {
3564
0
      update_reference_segmentation_map(cpi);
3565
0
    } else if (cm->last_frame_seg_map) {
3566
0
      memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
3567
0
             cm->cur_frame->mi_cols * cm->cur_frame->mi_rows *
3568
0
                 sizeof(*cm->cur_frame->seg_map));
3569
0
    }
3570
0
  }
3571
3572
#if CONFIG_FRAME_PARALLEL_ENCODE
3573
  int release_scaled_refs = 0;
3574
#if CONFIG_FPMT_TEST
3575
  release_scaled_refs =
3576
      (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) ? 1 : 0;
3577
#endif  // CONFIG_FPMT_TEST
3578
  if (release_scaled_refs ||
3579
      cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) {
3580
#else
3581
1.26k
  {
3582
1.26k
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
3583
1.26k
    if (frame_is_intra_only(cm) == 0) {
3584
0
      release_scaled_references(cpi);
3585
0
    }
3586
1.26k
  }
3587
#if CONFIG_AV1_TEMPORAL_DENOISING
3588
  av1_denoiser_update_ref_frame(cpi);
3589
#endif
3590
3591
  // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3592
  //       for the purpose to verify no mismatch between encoder and decoder.
3593
1.26k
  if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
3594
3595
1.26k
  if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
3596
1.26k
    *cm->fc = cpi->tile_data[largest_tile_id].tctx;
3597
1.26k
    av1_reset_cdf_symbol_counters(cm->fc);
3598
1.26k
  }
3599
1.26k
  if (!cm->tiles.large_scale) {
3600
1.26k
    cm->cur_frame->frame_context = *cm->fc;
3601
1.26k
  }
3602
3603
1.26k
  if (tile_cfg->enable_ext_tile_debug) {
3604
    // (yunqing) This test ensures the correctness of large scale tile coding.
3605
0
    if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) {
3606
0
      char fn[20] = "./fc";
3607
0
      fn[4] = current_frame->frame_number / 100 + '0';
3608
0
      fn[5] = (current_frame->frame_number % 100) / 10 + '0';
3609
0
      fn[6] = (current_frame->frame_number % 10) + '0';
3610
0
      fn[7] = '\0';
3611
0
      av1_print_frame_contexts(cm->fc, fn);
3612
0
    }
3613
0
  }
3614
3615
1.26k
  cpi->last_frame_type = current_frame->frame_type;
3616
3617
  // Clear the one shot update flags for segmentation map and mode/ref loop
3618
  // filter deltas.
3619
1.26k
  cm->seg.update_map = 0;
3620
1.26k
  cm->seg.update_data = 0;
3621
1.26k
  cm->lf.mode_ref_delta_update = 0;
3622
3623
  // A droppable frame might not be shown but it always
3624
  // takes a space in the gf group. Therefore, even when
3625
  // it is not shown, we still need update the count down.
3626
1.26k
  if (cm->show_frame) {
3627
1.26k
    update_frame_index_set(&cpi->frame_index_set, cm->show_frame);
3628
1.26k
    ++current_frame->frame_number;
3629
1.26k
  }
3630
3631
#if CONFIG_COLLECT_COMPONENT_TIMING
3632
  end_timing(cpi, encode_frame_to_data_rate_time);
3633
#endif
3634
3635
1.26k
  return AOM_CODEC_OK;
3636
1.26k
}
3637
3638
int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
3639
               const EncodeFrameInput *const frame_input,
3640
               const EncodeFrameParams *const frame_params,
3641
1.26k
               EncodeFrameResults *const frame_results) {
3642
1.26k
  AV1_COMMON *const cm = &cpi->common;
3643
1.26k
  CurrentFrame *const current_frame = &cm->current_frame;
3644
3645
1.26k
  cpi->unscaled_source = frame_input->source;
3646
1.26k
  cpi->source = frame_input->source;
3647
1.26k
  cpi->unscaled_last_source = frame_input->last_source;
3648
3649
1.26k
  current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
3650
1.26k
  cm->features.error_resilient_mode = frame_params->error_resilient_mode;
3651
1.26k
  cm->features.primary_ref_frame = frame_params->primary_ref_frame;
3652
1.26k
  cm->current_frame.frame_type = frame_params->frame_type;
3653
1.26k
  cm->show_frame = frame_params->show_frame;
3654
1.26k
  cpi->ref_frame_flags = frame_params->ref_frame_flags;
3655
1.26k
  cpi->speed = frame_params->speed;
3656
1.26k
  cm->show_existing_frame = frame_params->show_existing_frame;
3657
1.26k
  cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
3658
3659
1.26k
  memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
3660
1.26k
         REF_FRAMES * sizeof(*cm->remapped_ref_idx));
3661
3662
1.26k
  memcpy(&cpi->refresh_frame, &frame_params->refresh_frame,
3663
1.26k
         sizeof(cpi->refresh_frame));
3664
3665
1.26k
  if (current_frame->frame_type == KEY_FRAME &&
3666
1.26k
      cpi->ppi->gf_group.refbuf_state[cpi->gf_frame_index] == REFBUF_RESET) {
3667
1.26k
    current_frame->frame_number = 0;
3668
1.26k
  }
3669
3670
1.26k
  current_frame->order_hint =
3671
1.26k
      current_frame->frame_number + frame_params->order_offset;
3672
3673
1.26k
  current_frame->display_order_hint = current_frame->order_hint;
3674
1.26k
  current_frame->order_hint %=
3675
1.26k
      (1 << (cm->seq_params->order_hint_info.order_hint_bits_minus_1 + 1));
3676
3677
#if CONFIG_FRAME_PARALLEL_ENCODE
3678
  current_frame->pyramid_level = get_true_pyr_level(
3679
      cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index],
3680
      current_frame->display_order_hint, cpi->ppi->gf_group.max_layer_depth);
3681
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
3682
3683
1.26k
  if (is_stat_generation_stage(cpi)) {
3684
0
#if !CONFIG_REALTIME_ONLY
3685
0
    av1_first_pass(cpi, frame_input->ts_duration);
3686
0
#endif
3687
1.26k
  } else if (cpi->oxcf.pass == AOM_RC_ONE_PASS ||
3688
1.26k
             cpi->oxcf.pass >= AOM_RC_SECOND_PASS) {
3689
1.26k
    if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
3690
1.26k
        AOM_CODEC_OK) {
3691
0
      return AOM_CODEC_ERROR;
3692
0
    }
3693
1.26k
  } else {
3694
0
    return AOM_CODEC_ERROR;
3695
0
  }
3696
3697
1.26k
  return AOM_CODEC_OK;
3698
1.26k
}
3699
3700
#if CONFIG_DENOISE
3701
static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
3702
                            int block_size, float noise_level,
3703
0
                            int64_t time_stamp, int64_t end_time) {
3704
0
  AV1_COMMON *const cm = &cpi->common;
3705
0
  if (!cpi->denoise_and_model) {
3706
0
    cpi->denoise_and_model = aom_denoise_and_model_alloc(
3707
0
        cm->seq_params->bit_depth, block_size, noise_level);
3708
0
    if (!cpi->denoise_and_model) {
3709
0
      aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
3710
0
                         "Error allocating denoise and model");
3711
0
      return -1;
3712
0
    }
3713
0
  }
3714
0
  if (!cpi->film_grain_table) {
3715
0
    cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
3716
0
    if (!cpi->film_grain_table) {
3717
0
      aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
3718
0
                         "Error allocating grain table");
3719
0
      return -1;
3720
0
    }
3721
0
    memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
3722
0
  }
3723
0
  if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
3724
0
                                &cm->film_grain_params,
3725
0
                                cpi->oxcf.enable_dnl_denoising)) {
3726
0
    if (cm->film_grain_params.apply_grain) {
3727
0
      aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
3728
0
                                  &cm->film_grain_params);
3729
0
    }
3730
0
  }
3731
0
  return 0;
3732
0
}
3733
#endif
3734
3735
int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
3736
                          YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3737
1.26k
                          int64_t end_time) {
3738
1.26k
  AV1_COMMON *const cm = &cpi->common;
3739
1.26k
  const SequenceHeader *const seq_params = cm->seq_params;
3740
1.26k
  int res = 0;
3741
1.26k
  const int subsampling_x = sd->subsampling_x;
3742
1.26k
  const int subsampling_y = sd->subsampling_y;
3743
1.26k
  const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
3744
3745
#if CONFIG_TUNE_VMAF
3746
  if (!is_stat_generation_stage(cpi) &&
3747
      cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
3748
    av1_vmaf_frame_preprocessing(cpi, sd);
3749
  }
3750
  if (!is_stat_generation_stage(cpi) &&
3751
      cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
3752
    av1_vmaf_blk_preprocessing(cpi, sd);
3753
  }
3754
#endif
3755
3756
#if CONFIG_INTERNAL_STATS
3757
  struct aom_usec_timer timer;
3758
  aom_usec_timer_start(&timer);
3759
#endif
3760
3761
#if CONFIG_AV1_TEMPORAL_DENOISING
3762
  setup_denoiser_buffer(cpi);
3763
#endif
3764
3765
1.26k
#if CONFIG_DENOISE
3766
  // even if denoise_noise_level is > 0, we don't need need to denoise on pass
3767
  // 1 of 2 if enable_dnl_denoising is disabled since the 2nd pass will be
3768
  // encoding the original (non-denoised) frame
3769
1.26k
  if (cpi->oxcf.noise_level > 0 && !(cpi->oxcf.pass == AOM_RC_FIRST_PASS &&
3770
0
                                     !cpi->oxcf.enable_dnl_denoising)) {
3771
0
#if !CONFIG_REALTIME_ONLY
3772
    // Choose a synthetic noise level for still images for enhanced perceptual
3773
    // quality based on an estimated noise level in the source, but only if
3774
    // the noise level is set on the command line to > 0.
3775
0
    if (cpi->oxcf.mode == ALLINTRA) {
3776
      // No noise synthesis if source is very clean.
3777
      // Uses a low edge threshold to focus on smooth areas.
3778
      // Increase output noise setting a little compared to measured value.
3779
0
      cpi->oxcf.noise_level =
3780
0
          (float)(av1_estimate_noise_from_single_plane(
3781
0
                      sd, 0, cm->seq_params->bit_depth, 16) -
3782
0
                  0.1);
3783
0
      cpi->oxcf.noise_level = (float)AOMMAX(0.0, cpi->oxcf.noise_level);
3784
0
      if (cpi->oxcf.noise_level > 0.0) {
3785
0
        cpi->oxcf.noise_level += (float)0.5;
3786
0
      }
3787
0
      cpi->oxcf.noise_level = (float)AOMMIN(5.0, cpi->oxcf.noise_level);
3788
0
    }
3789
0
#endif
3790
3791
0
    if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
3792
0
                         cpi->oxcf.noise_level, time_stamp, end_time) < 0)
3793
0
      res = -1;
3794
0
  }
3795
1.26k
#endif  //  CONFIG_DENOISE
3796
3797
1.26k
  if (av1_lookahead_push(cpi->ppi->lookahead, sd, time_stamp, end_time,
3798
1.26k
                         use_highbitdepth, frame_flags))
3799
0
    res = -1;
3800
#if CONFIG_INTERNAL_STATS
3801
  aom_usec_timer_mark(&timer);
3802
  cpi->ppi->total_time_receive_data += aom_usec_timer_elapsed(&timer);
3803
#endif
3804
3805
  // Note: Regarding profile setting, the following checks are added to help
3806
  // choose a proper profile for the input video. The criterion is that all
3807
  // bitstreams must be designated as the lowest profile that match its content.
3808
  // E.G. A bitstream that contains 4:4:4 video must be designated as High
3809
  // Profile in the seq header, and likewise a bitstream that contains 4:2:2
3810
  // bitstream must be designated as Professional Profile in the sequence
3811
  // header.
3812
1.26k
  if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
3813
1.26k
      (subsampling_x != 1 || subsampling_y != 1)) {
3814
0
    aom_internal_error(cm->error, AOM_CODEC_INVALID_PARAM,
3815
0
                       "Non-4:2:0 color format requires profile 1 or 2");
3816
0
    res = -1;
3817
0
  }
3818
1.26k
  if ((seq_params->profile == PROFILE_1) &&
3819
1.26k
      !(subsampling_x == 0 && subsampling_y == 0)) {
3820
0
    aom_internal_error(cm->error, AOM_CODEC_INVALID_PARAM,
3821
0
                       "Profile 1 requires 4:4:4 color format");
3822
0
    res = -1;
3823
0
  }
3824
1.26k
  if ((seq_params->profile == PROFILE_2) &&
3825
1.26k
      (seq_params->bit_depth <= AOM_BITS_10) &&
3826
1.26k
      !(subsampling_x == 1 && subsampling_y == 0)) {
3827
0
    aom_internal_error(cm->error, AOM_CODEC_INVALID_PARAM,
3828
0
                       "Profile 2 bit-depth <= 10 requires 4:2:2 color format");
3829
0
    res = -1;
3830
0
  }
3831
3832
1.26k
  return res;
3833
1.26k
}
3834
3835
#if CONFIG_ENTROPY_STATS
3836
void print_entropy_stats(AV1_PRIMARY *const ppi) {
3837
  if (!ppi->cpi) return;
3838
3839
  if (ppi->cpi->oxcf.pass != 1 &&
3840
      ppi->cpi->common.current_frame.frame_number > 0) {
3841
    fprintf(stderr, "Writing counts.stt\n");
3842
    FILE *f = fopen("counts.stt", "wb");
3843
    fwrite(&ppi->aggregate_fc, sizeof(ppi->aggregate_fc), 1, f);
3844
    fclose(f);
3845
  }
3846
}
3847
#endif  // CONFIG_ENTROPY_STATS
3848
3849
#if CONFIG_INTERNAL_STATS
3850
extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
3851
                                 const unsigned char *img2, int img2_pitch,
3852
                                 int width, int height);
3853
3854
static void adjust_image_stat(double y, double u, double v, double all,
3855
                              ImageStat *s) {
3856
  s->stat[STAT_Y] += y;
3857
  s->stat[STAT_U] += u;
3858
  s->stat[STAT_V] += v;
3859
  s->stat[STAT_ALL] += all;
3860
  s->worst = AOMMIN(s->worst, all);
3861
}
3862
3863
static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
3864
  AV1_PRIMARY *const ppi = cpi->ppi;
3865
  AV1_COMMON *const cm = &cpi->common;
3866
  double samples = 0.0;
3867
  const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
3868
  const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
3869
3870
  if (cpi->ppi->use_svc &&
3871
      cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1)
3872
    return;
3873
3874
#if CONFIG_INTER_STATS_ONLY
3875
  if (cm->current_frame.frame_type == KEY_FRAME) return;  // skip key frame
3876
#endif
3877
  cpi->bytes += frame_bytes;
3878
  if (cm->show_frame) {
3879
    const YV12_BUFFER_CONFIG *orig = cpi->source;
3880
    const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
3881
    double y, u, v, frame_all;
3882
3883
    ppi->count[0]++;
3884
    ppi->count[1]++;
3885
    if (cpi->ppi->b_calculate_psnr) {
3886
      PSNR_STATS psnr;
3887
      double weight[2] = { 0.0, 0.0 };
3888
      double frame_ssim2[2] = { 0.0, 0.0 };
3889
#if CONFIG_AV1_HIGHBITDEPTH
3890
      aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
3891
#else
3892
      aom_calc_psnr(orig, recon, &psnr);
3893
#endif
3894
      adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
3895
                        &(ppi->psnr[0]));
3896
      ppi->total_sq_error[0] += psnr.sse[0];
3897
      ppi->total_samples[0] += psnr.samples[0];
3898
      samples = psnr.samples[0];
3899
3900
      aom_calc_ssim(orig, recon, bit_depth, in_bit_depth,
3901
                    cm->seq_params->use_highbitdepth, weight, frame_ssim2);
3902
3903
      ppi->worst_ssim = AOMMIN(ppi->worst_ssim, frame_ssim2[0]);
3904
      ppi->summed_quality += frame_ssim2[0] * weight[0];
3905
      ppi->summed_weights += weight[0];
3906
3907
#if CONFIG_AV1_HIGHBITDEPTH
3908
      // Compute PSNR based on stream bit depth
3909
      if ((cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) &&
3910
          (in_bit_depth < bit_depth)) {
3911
        adjust_image_stat(psnr.psnr_hbd[1], psnr.psnr_hbd[2], psnr.psnr_hbd[3],
3912
                          psnr.psnr_hbd[0], &ppi->psnr[1]);
3913
        ppi->total_sq_error[1] += psnr.sse_hbd[0];
3914
        ppi->total_samples[1] += psnr.samples_hbd[0];
3915
3916
        ppi->worst_ssim_hbd = AOMMIN(ppi->worst_ssim_hbd, frame_ssim2[1]);
3917
        ppi->summed_quality_hbd += frame_ssim2[1] * weight[1];
3918
        ppi->summed_weights_hbd += weight[1];
3919
      }
3920
#endif
3921
3922
#if 0
3923
      {
3924
        FILE *f = fopen("q_used.stt", "a");
3925
        double y2 = psnr.psnr[1];
3926
        double u2 = psnr.psnr[2];
3927
        double v2 = psnr.psnr[3];
3928
        double frame_psnr2 = psnr.psnr[0];
3929
        fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3930
                cm->current_frame.frame_number, y2, u2, v2,
3931
                frame_psnr2, frame_ssim2);
3932
        fclose(f);
3933
      }
3934
#endif
3935
    }
3936
    if (ppi->b_calculate_blockiness) {
3937
      if (!cm->seq_params->use_highbitdepth) {
3938
        const double frame_blockiness =
3939
            av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
3940
                               recon->y_stride, orig->y_width, orig->y_height);
3941
        ppi->worst_blockiness = AOMMAX(ppi->worst_blockiness, frame_blockiness);
3942
        ppi->total_blockiness += frame_blockiness;
3943
      }
3944
3945
      if (ppi->b_calculate_consistency) {
3946
        if (!cm->seq_params->use_highbitdepth) {
3947
          const double this_inconsistency = aom_get_ssim_metrics(
3948
              orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
3949
              orig->y_width, orig->y_height, ppi->ssim_vars, &ppi->metrics, 1);
3950
3951
          const double peak = (double)((1 << in_bit_depth) - 1);
3952
          const double consistency =
3953
              aom_sse_to_psnr(samples, peak, ppi->total_inconsistency);
3954
          if (consistency > 0.0)
3955
            ppi->worst_consistency =
3956
                AOMMIN(ppi->worst_consistency, consistency);
3957
          ppi->total_inconsistency += this_inconsistency;
3958
        }
3959
      }
3960
    }
3961
3962
    frame_all =
3963
        aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
3964
    adjust_image_stat(y, u, v, frame_all, &ppi->fastssim);
3965
    frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
3966
    adjust_image_stat(y, u, v, frame_all, &ppi->psnrhvs);
3967
  }
3968
}
3969
3970
void print_internal_stats(AV1_PRIMARY *ppi) {
3971
  if (!ppi->cpi) return;
3972
  AV1_COMP *const cpi = ppi->cpi;
3973
3974
  if (ppi->cpi->oxcf.pass != 1 &&
3975
      ppi->cpi->common.current_frame.frame_number > 0) {
3976
    char headings[512] = { 0 };
3977
    char results[512] = { 0 };
3978
    FILE *f = fopen("opsnr.stt", "a");
3979
    double time_encoded =
3980
        (cpi->time_stamps.prev_ts_end - cpi->time_stamps.first_ts_start) /
3981
        10000000.000;
3982
    double total_encode_time =
3983
        (ppi->total_time_receive_data + ppi->total_time_compress_data) /
3984
        1000.000;
3985
    const double dr =
3986
        (double)ppi->total_bytes * (double)8 / (double)1000 / time_encoded;
3987
    const double peak =
3988
        (double)((1 << ppi->cpi->oxcf.input_cfg.input_bit_depth) - 1);
3989
    const double target_rate =
3990
        (double)ppi->cpi->oxcf.rc_cfg.target_bandwidth / 1000;
3991
    const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3992
3993
    if (ppi->b_calculate_psnr) {
3994
      const double total_psnr = aom_sse_to_psnr(
3995
          (double)ppi->total_samples[0], peak, (double)ppi->total_sq_error[0]);
3996
      const double total_ssim =
3997
          100 * pow(ppi->summed_quality / ppi->summed_weights, 8.0);
3998
      snprintf(headings, sizeof(headings),
3999
               "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
4000
               "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
4001
               "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
4002
               "AVPsrnY\tAPsnrCb\tAPsnrCr");
4003
      snprintf(results, sizeof(results),
4004
               "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
4005
               "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
4006
               "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
4007
               "%7.3f\t%7.3f\t%7.3f",
4008
               dr, ppi->psnr[0].stat[STAT_ALL] / ppi->count[0], total_psnr,
4009
               ppi->psnr[0].stat[STAT_ALL] / ppi->count[0], total_psnr,
4010
               total_ssim, total_ssim,
4011
               ppi->fastssim.stat[STAT_ALL] / ppi->count[0],
4012
               ppi->psnrhvs.stat[STAT_ALL] / ppi->count[0], ppi->psnr[0].worst,
4013
               ppi->worst_ssim, ppi->fastssim.worst, ppi->psnrhvs.worst,
4014
               ppi->psnr[0].stat[STAT_Y] / ppi->count[0],
4015
               ppi->psnr[0].stat[STAT_U] / ppi->count[0],
4016
               ppi->psnr[0].stat[STAT_V] / ppi->count[0]);
4017
4018
      if (ppi->b_calculate_blockiness) {
4019
        SNPRINT(headings, "\t  Block\tWstBlck");
4020
        SNPRINT2(results, "\t%7.3f", ppi->total_blockiness / ppi->count[0]);
4021
        SNPRINT2(results, "\t%7.3f", ppi->worst_blockiness);
4022
      }
4023
4024
      if (ppi->b_calculate_consistency) {
4025
        double consistency =
4026
            aom_sse_to_psnr((double)ppi->total_samples[0], peak,
4027
                            (double)ppi->total_inconsistency);
4028
4029
        SNPRINT(headings, "\tConsist\tWstCons");
4030
        SNPRINT2(results, "\t%7.3f", consistency);
4031
        SNPRINT2(results, "\t%7.3f", ppi->worst_consistency);
4032
      }
4033
4034
      SNPRINT(headings, "\t   Time\tRcErr\tAbsErr");
4035
      SNPRINT2(results, "\t%8.0f", total_encode_time);
4036
      SNPRINT2(results, " %7.2f", rate_err);
4037
      SNPRINT2(results, " %7.2f", fabs(rate_err));
4038
4039
      SNPRINT(headings, "\tAPsnr611");
4040
      SNPRINT2(results, " %7.3f",
4041
               (6 * ppi->psnr[0].stat[STAT_Y] + ppi->psnr[0].stat[STAT_U] +
4042
                ppi->psnr[0].stat[STAT_V]) /
4043
                   (ppi->count[0] * 8));
4044
4045
#if CONFIG_AV1_HIGHBITDEPTH
4046
      const uint32_t in_bit_depth = ppi->cpi->oxcf.input_cfg.input_bit_depth;
4047
      const uint32_t bit_depth = ppi->seq_params.bit_depth;
4048
      // Since cpi->source->flags is not available here, but total_samples[1]
4049
      // will be non-zero if cpi->source->flags & YV12_FLAG_HIGHBITDEPTH was
4050
      // true in compute_internal_stats
4051
      if ((ppi->total_samples[1] > 0) && (in_bit_depth < bit_depth)) {
4052
        const double peak_hbd = (double)((1 << bit_depth) - 1);
4053
        const double total_psnr_hbd =
4054
            aom_sse_to_psnr((double)ppi->total_samples[1], peak_hbd,
4055
                            (double)ppi->total_sq_error[1]);
4056
        const double total_ssim_hbd =
4057
            100 * pow(ppi->summed_quality_hbd / ppi->summed_weights_hbd, 8.0);
4058
        SNPRINT(headings,
4059
                "\t AVGPsnrH GLBPsnrH AVPsnrPH GLPsnrPH"
4060
                " AVPsnrYH APsnrCbH APsnrCrH WstPsnrH"
4061
                " AOMSSIMH VPSSIMPH WstSsimH");
4062
        SNPRINT2(results, "\t%7.3f",
4063
                 ppi->psnr[1].stat[STAT_ALL] / ppi->count[1]);
4064
        SNPRINT2(results, "  %7.3f", total_psnr_hbd);
4065
        SNPRINT2(results, "  %7.3f",
4066
                 ppi->psnr[1].stat[STAT_ALL] / ppi->count[1]);
4067
        SNPRINT2(results, "  %7.3f", total_psnr_hbd);
4068
        SNPRINT2(results, "  %7.3f", ppi->psnr[1].stat[STAT_Y] / ppi->count[1]);
4069
        SNPRINT2(results, "  %7.3f", ppi->psnr[1].stat[STAT_U] / ppi->count[1]);
4070
        SNPRINT2(results, "  %7.3f", ppi->psnr[1].stat[STAT_V] / ppi->count[1]);
4071
        SNPRINT2(results, "  %7.3f", ppi->psnr[1].worst);
4072
        SNPRINT2(results, "  %7.3f", total_ssim_hbd);
4073
        SNPRINT2(results, "  %7.3f", total_ssim_hbd);
4074
        SNPRINT2(results, "  %7.3f", ppi->worst_ssim_hbd);
4075
      }
4076
#endif
4077
      fprintf(f, "%s\n", headings);
4078
      fprintf(f, "%s\n", results);
4079
    }
4080
4081
    fclose(f);
4082
4083
    if (ppi->ssim_vars != NULL) {
4084
      aom_free(ppi->ssim_vars);
4085
      ppi->ssim_vars = NULL;
4086
    }
4087
  }
4088
}
4089
#endif  // CONFIG_INTERNAL_STATS
4090
4091
1.26k
static AOM_INLINE void update_keyframe_counters(AV1_COMP *cpi) {
4092
1.26k
  if (cpi->common.show_frame && cpi->rc.frames_to_key) {
4093
1.26k
#if !CONFIG_REALTIME_ONLY
4094
1.26k
    FIRSTPASS_INFO *firstpass_info = &cpi->ppi->twopass.firstpass_info;
4095
1.26k
    if (firstpass_info->past_stats_count > FIRSTPASS_INFO_STATS_PAST_MIN) {
4096
0
      av1_firstpass_info_move_cur_index_and_pop(firstpass_info);
4097
1.26k
    } else {
4098
      // When there is not enough past stats, we move the current
4099
      // index without popping the past stats
4100
1.26k
      av1_firstpass_info_move_cur_index(firstpass_info);
4101
1.26k
    }
4102
1.26k
#endif
4103
1.26k
    cpi->rc.frames_since_key++;
4104
1.26k
    cpi->rc.frames_to_key--;
4105
1.26k
    cpi->rc.frames_to_fwd_kf--;
4106
1.26k
  }
4107
1.26k
}
4108
4109
1.26k
static AOM_INLINE void update_frames_till_gf_update(AV1_COMP *cpi) {
4110
  // TODO(weitinglin): Updating this counter for is_frame_droppable
4111
  // is a work-around to handle the condition when a frame is drop.
4112
  // We should fix the cpi->common.show_frame flag
4113
  // instead of checking the other condition to update the counter properly.
4114
1.26k
  if (cpi->common.show_frame ||
4115
1.26k
      is_frame_droppable(&cpi->svc, &cpi->ext_flags.refresh_frame)) {
4116
    // Decrement count down till next gf
4117
1.26k
    if (cpi->rc.frames_till_gf_update_due > 0)
4118
0
      cpi->rc.frames_till_gf_update_due--;
4119
1.26k
  }
4120
1.26k
}
4121
4122
1.26k
static AOM_INLINE void update_gf_group_index(AV1_COMP *cpi) {
4123
  // Increment the gf group index ready for the next frame.
4124
1.26k
  ++cpi->gf_frame_index;
4125
1.26k
}
4126
4127
static void update_fb_of_context_type(const AV1_COMP *const cpi,
4128
1.26k
                                      int *const fb_of_context_type) {
4129
1.26k
  const AV1_COMMON *const cm = &cpi->common;
4130
1.26k
  const int current_frame_ref_type = get_current_frame_ref_type(cpi);
4131
4132
1.26k
  if (frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
4133
1.26k
      cpi->ext_flags.use_primary_ref_none) {
4134
11.3k
    for (int i = 0; i < REF_FRAMES; i++) {
4135
10.0k
      fb_of_context_type[i] = -1;
4136
10.0k
    }
4137
1.26k
    fb_of_context_type[current_frame_ref_type] =
4138
1.26k
        cm->show_frame ? get_ref_frame_map_idx(cm, GOLDEN_FRAME)
4139
1.26k
                       : get_ref_frame_map_idx(cm, ALTREF_FRAME);
4140
1.26k
  }
4141
4142
1.26k
  if (!encode_show_existing_frame(cm)) {
4143
    // Refresh fb_of_context_type[]: see encoder.h for explanation
4144
1.26k
    if (cm->current_frame.frame_type == KEY_FRAME) {
4145
      // All ref frames are refreshed, pick one that will live long enough
4146
1.26k
      fb_of_context_type[current_frame_ref_type] = 0;
4147
1.26k
    } else {
4148
      // If more than one frame is refreshed, it doesn't matter which one we
4149
      // pick so pick the first.  LST sometimes doesn't refresh any: this is ok
4150
4151
0
      for (int i = 0; i < REF_FRAMES; i++) {
4152
0
        if (cm->current_frame.refresh_frame_flags & (1 << i)) {
4153
0
          fb_of_context_type[current_frame_ref_type] = i;
4154
0
          break;
4155
0
        }
4156
0
      }
4157
0
    }
4158
1.26k
  }
4159
1.26k
}
4160
4161
1.26k
static void update_rc_counts(AV1_COMP *cpi) {
4162
1.26k
  update_keyframe_counters(cpi);
4163
1.26k
  update_frames_till_gf_update(cpi);
4164
1.26k
  update_gf_group_index(cpi);
4165
1.26k
}
4166
4167
#if CONFIG_FRAME_PARALLEL_ENCODE
4168
static void update_end_of_frame_stats(AV1_COMP *cpi) {
4169
  if (cpi->do_frame_data_update) {
4170
    // Store current frame loopfilter levels in ppi, if update flag is set.
4171
    if (!cpi->common.show_existing_frame) {
4172
      AV1_COMMON *const cm = &cpi->common;
4173
      struct loopfilter *const lf = &cm->lf;
4174
      cpi->ppi->filter_level[0] = lf->filter_level[0];
4175
      cpi->ppi->filter_level[1] = lf->filter_level[1];
4176
      cpi->ppi->filter_level_u = lf->filter_level_u;
4177
      cpi->ppi->filter_level_v = lf->filter_level_v;
4178
    }
4179
  }
4180
4181
  // Store frame level mv_stats from cpi to ppi.
4182
  cpi->ppi->mv_stats = cpi->mv_stats;
4183
}
4184
#endif
4185
4186
// Updates frame level stats related to global motion
4187
1.26k
static AOM_INLINE void update_gm_stats(AV1_COMP *cpi) {
4188
1.26k
  FRAME_UPDATE_TYPE update_type =
4189
1.26k
      cpi->ppi->gf_group.update_type[cpi->gf_frame_index];
4190
1.26k
  int i, is_gm_present = 0;
4191
4192
  // Check if the current frame has any valid global motion model across its
4193
  // reference frames
4194
11.3k
  for (i = 0; i < REF_FRAMES; i++) {
4195
10.0k
    if (cpi->common.global_motion[i].wmtype != IDENTITY) {
4196
0
      is_gm_present = 1;
4197
0
      break;
4198
0
    }
4199
10.0k
  }
4200
1.26k
  int update_actual_stats = 1;
4201
#if CONFIG_FRAME_PARALLEL_ENCODE && CONFIG_FPMT_TEST
4202
  update_actual_stats =
4203
      (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) ? 0 : 1;
4204
  if (!update_actual_stats) {
4205
    if (cpi->ppi->temp_valid_gm_model_found[update_type] == INT32_MAX) {
4206
      cpi->ppi->temp_valid_gm_model_found[update_type] = is_gm_present;
4207
    } else {
4208
      cpi->ppi->temp_valid_gm_model_found[update_type] |= is_gm_present;
4209
    }
4210
    int show_existing_between_parallel_frames =
4211
        (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] ==
4212
             INTNL_OVERLAY_UPDATE &&
4213
         cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2);
4214
    if (cpi->do_frame_data_update == 1 &&
4215
        !show_existing_between_parallel_frames) {
4216
      for (i = 0; i < FRAME_UPDATE_TYPES; i++) {
4217
        cpi->ppi->valid_gm_model_found[i] =
4218
            cpi->ppi->temp_valid_gm_model_found[i];
4219
      }
4220
    }
4221
  }
4222
#endif
4223
1.26k
  if (update_actual_stats) {
4224
1.26k
    if (cpi->ppi->valid_gm_model_found[update_type] == INT32_MAX) {
4225
1.26k
      cpi->ppi->valid_gm_model_found[update_type] = is_gm_present;
4226
1.26k
    } else {
4227
0
      cpi->ppi->valid_gm_model_found[update_type] |= is_gm_present;
4228
0
    }
4229
1.26k
  }
4230
1.26k
}
4231
4232
void av1_post_encode_updates(AV1_COMP *const cpi,
4233
1.26k
                             const AV1_COMP_DATA *const cpi_data) {
4234
1.26k
  AV1_PRIMARY *const ppi = cpi->ppi;
4235
1.26k
  AV1_COMMON *const cm = &cpi->common;
4236
4237
1.26k
  update_gm_stats(cpi);
4238
4239
1.26k
#if !CONFIG_REALTIME_ONLY
4240
  // Update the total stats remaining structure.
4241
1.26k
  if (cpi->twopass_frame.this_frame != NULL &&
4242
1.26k
      ppi->twopass.stats_buf_ctx->total_left_stats) {
4243
0
    subtract_stats(ppi->twopass.stats_buf_ctx->total_left_stats,
4244
0
                   cpi->twopass_frame.this_frame);
4245
0
  }
4246
1.26k
#endif
4247
4248
1.26k
  if (!is_stat_generation_stage(cpi) && !cpi->is_dropped_frame) {
4249
#if CONFIG_FRAME_PARALLEL_ENCODE
4250
#if CONFIG_FRAME_PARALLEL_ENCODE_2
4251
    // Before calling refresh_reference_frames(), copy ppi->ref_frame_map_copy
4252
    // to cm->ref_frame_map for frame_parallel_level 2 frame in a parallel
4253
    // encode set of lower layer frames.
4254
    // TODO(Remya): Move ref_frame_map from AV1_COMMON to AV1_PRIMARY to avoid
4255
    // copy.
4256
    if (ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 2 &&
4257
        ppi->gf_group.frame_parallel_level[cpi->gf_frame_index - 1] == 1 &&
4258
        ppi->gf_group.update_type[cpi->gf_frame_index - 1] ==
4259
            INTNL_ARF_UPDATE) {
4260
      memcpy(cm->ref_frame_map, ppi->ref_frame_map_copy,
4261
             sizeof(cm->ref_frame_map));
4262
    }
4263
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
4264
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
4265
1.26k
    refresh_reference_frames(cpi);
4266
#if CONFIG_FRAME_PARALLEL_ENCODE
4267
#if CONFIG_FRAME_PARALLEL_ENCODE_2
4268
    // For frame_parallel_level 1 frame in a parallel encode set of lower layer
4269
    // frames, store the updated cm->ref_frame_map in ppi->ref_frame_map_copy.
4270
    if (ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 1 &&
4271
        ppi->gf_group.update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) {
4272
      memcpy(ppi->ref_frame_map_copy, cm->ref_frame_map,
4273
             sizeof(cm->ref_frame_map));
4274
    }
4275
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
4276
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
4277
1.26k
    av1_rc_postencode_update(cpi, cpi_data->frame_size);
4278
1.26k
  }
4279
4280
1.26k
  if (cpi_data->pop_lookahead == 1) {
4281
1.26k
    av1_lookahead_pop(cpi->ppi->lookahead, cpi_data->flush,
4282
1.26k
                      cpi->compressor_stage);
4283
1.26k
  }
4284
#if CONFIG_FRAME_PARALLEL_ENCODE
4285
  if (cpi->common.show_frame) {
4286
    cpi->ppi->ts_start_last_show_frame = cpi_data->ts_frame_start;
4287
    cpi->ppi->ts_end_last_show_frame = cpi_data->ts_frame_end;
4288
  }
4289
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
4290
1.26k
  if (ppi->level_params.keep_level_stats && !is_stat_generation_stage(cpi)) {
4291
    // Initialize level info. at the beginning of each sequence.
4292
0
    if (cm->current_frame.frame_type == KEY_FRAME &&
4293
0
        ppi->gf_group.refbuf_state[cpi->gf_frame_index] == REFBUF_RESET) {
4294
0
      av1_init_level_info(cpi);
4295
0
    }
4296
0
    av1_update_level_info(cpi, cpi_data->frame_size, cpi_data->ts_frame_start,
4297
0
                          cpi_data->ts_frame_end);
4298
0
  }
4299
4300
1.26k
  if (!is_stat_generation_stage(cpi)) {
4301
1.26k
#if !CONFIG_REALTIME_ONLY
4302
1.26k
    if (!has_no_stats_stage(cpi)) av1_twopass_postencode_update(cpi);
4303
1.26k
#endif
4304
1.26k
    update_fb_of_context_type(cpi, ppi->fb_of_context_type);
4305
1.26k
    update_rc_counts(cpi);
4306
#if CONFIG_FRAME_PARALLEL_ENCODE
4307
    update_end_of_frame_stats(cpi);
4308
#endif
4309
1.26k
  }
4310
4311
1.26k
  if (cpi->oxcf.pass == AOM_RC_THIRD_PASS && cpi->third_pass_ctx) {
4312
0
    av1_pop_third_pass_info(cpi->third_pass_ctx);
4313
0
  }
4314
4315
1.26k
  if (ppi->use_svc) av1_save_layer_context(cpi);
4316
4317
  // Note *size = 0 indicates a dropped frame for which psnr is not calculated
4318
1.26k
  if (ppi->b_calculate_psnr && cpi_data->frame_size > 0) {
4319
0
    if (cm->show_existing_frame ||
4320
0
        (!is_stat_generation_stage(cpi) && cm->show_frame)) {
4321
0
      generate_psnr_packet(cpi);
4322
0
    }
4323
0
  }
4324
4325
#if CONFIG_INTERNAL_STATS
4326
  if (!is_stat_generation_stage(cpi)) {
4327
    compute_internal_stats(cpi, (int)cpi_data->frame_size);
4328
  }
4329
#endif  // CONFIG_INTERNAL_STATS
4330
4331
  // Write frame info. Subtract 1 from frame index since if was incremented in
4332
  // update_rc_counts.
4333
1.26k
  av1_write_second_pass_per_frame_info(cpi, cpi->gf_frame_index - 1);
4334
1.26k
}
4335
4336
2.52k
int av1_get_compressed_data(AV1_COMP *cpi, AV1_COMP_DATA *const cpi_data) {
4337
2.52k
  const AV1EncoderConfig *const oxcf = &cpi->oxcf;
4338
2.52k
  AV1_COMMON *const cm = &cpi->common;
4339
4340
#if CONFIG_FRAME_PARALLEL_ENCODE
4341
  // The jmp_buf is valid only for the duration of the function that calls
4342
  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
4343
  // before it returns.
4344
  if (setjmp(cm->error->jmp)) {
4345
    cm->error->setjmp = 0;
4346
    return cm->error->error_code;
4347
  }
4348
  cm->error->setjmp = 1;
4349
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
4350
4351
#if CONFIG_INTERNAL_STATS
4352
  cpi->frame_recode_hits = 0;
4353
  cpi->time_compress_data = 0;
4354
  cpi->bytes = 0;
4355
#endif
4356
#if CONFIG_ENTROPY_STATS
4357
  if (cpi->compressor_stage == ENCODE_STAGE) {
4358
    av1_zero(cpi->counts);
4359
  }
4360
#endif
4361
4362
#if CONFIG_BITSTREAM_DEBUG
4363
  assert(cpi->oxcf.max_threads <= 1 &&
4364
         "bitstream debug tool does not support multithreading");
4365
  bitstream_queue_record_write();
4366
  aom_bitstream_queue_set_frame_write(cm->current_frame.order_hint * 2 +
4367
                                      cm->show_frame);
4368
#endif
4369
2.52k
  if (cpi->ppi->use_svc && cpi->ppi->number_spatial_layers > 1) {
4370
0
    av1_one_pass_cbr_svc_start_layer(cpi);
4371
0
  }
4372
4373
2.52k
  cpi->is_dropped_frame = false;
4374
2.52k
  cm->showable_frame = 0;
4375
2.52k
  cpi_data->frame_size = 0;
4376
2.52k
  cpi->available_bs_size = cpi_data->cx_data_sz;
4377
#if CONFIG_INTERNAL_STATS
4378
  struct aom_usec_timer cmptimer;
4379
  aom_usec_timer_start(&cmptimer);
4380
#endif
4381
2.52k
  av1_set_high_precision_mv(cpi, 1, 0);
4382
4383
  // Normal defaults
4384
2.52k
  cm->features.refresh_frame_context =
4385
2.52k
      oxcf->tool_cfg.frame_parallel_decoding_mode
4386
2.52k
          ? REFRESH_FRAME_CONTEXT_DISABLED
4387
2.52k
          : REFRESH_FRAME_CONTEXT_BACKWARD;
4388
2.52k
  if (oxcf->tile_cfg.enable_large_scale_tile)
4389
0
    cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
4390
4391
2.52k
  if (assign_cur_frame_new_fb(cm) == NULL) {
4392
#if CONFIG_FRAME_PARALLEL_ENCODE
4393
    aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
4394
                       "Failed to allocate new cur_frame");
4395
#else
4396
0
    return AOM_CODEC_ERROR;
4397
0
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
4398
0
  }
4399
4400
#if CONFIG_COLLECT_COMPONENT_TIMING
4401
  // Accumulate 2nd pass time in 2-pass case or 1 pass time in 1-pass case.
4402
  if (cpi->oxcf.pass == 2 || cpi->oxcf.pass == 0)
4403
    start_timing(cpi, av1_encode_strategy_time);
4404
#endif
4405
4406
2.52k
  const int result = av1_encode_strategy(
4407
2.52k
      cpi, &cpi_data->frame_size, cpi_data->cx_data, &cpi_data->lib_flags,
4408
2.52k
      &cpi_data->ts_frame_start, &cpi_data->ts_frame_end,
4409
2.52k
      cpi_data->timestamp_ratio, &cpi_data->pop_lookahead, cpi_data->flush);
4410
4411
#if CONFIG_COLLECT_COMPONENT_TIMING
4412
  if (cpi->oxcf.pass == 2 || cpi->oxcf.pass == 0)
4413
    end_timing(cpi, av1_encode_strategy_time);
4414
4415
  // Print out timing information.
4416
  // Note: Use "cpi->frame_component_time[0] > 100 us" to avoid showing of
4417
  // show_existing_frame and lag-in-frames.
4418
  if ((cpi->oxcf.pass == 2 || cpi->oxcf.pass == 0) &&
4419
      cpi->frame_component_time[0] > 100) {
4420
    int i;
4421
    uint64_t frame_total = 0, total = 0;
4422
    const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
4423
    FRAME_UPDATE_TYPE frame_update_type =
4424
        get_frame_update_type(gf_group, cpi->gf_frame_index);
4425
4426
    fprintf(stderr,
4427
            "\n Frame number: %d, Frame type: %s, Show Frame: %d, Frame Update "
4428
            "Type: %d, Q: %d\n",
4429
            cm->current_frame.frame_number,
4430
            get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame,
4431
            frame_update_type, cm->quant_params.base_qindex);
4432
    for (i = 0; i < kTimingComponents; i++) {
4433
      cpi->component_time[i] += cpi->frame_component_time[i];
4434
      // Use av1_encode_strategy_time (i = 0) as the total time.
4435
      if (i == 0) {
4436
        frame_total = cpi->frame_component_time[0];
4437
        total = cpi->component_time[0];
4438
      }
4439
      fprintf(stderr,
4440
              " %50s:  %15" PRId64 " us [%6.2f%%] (total: %15" PRId64
4441
              " us [%6.2f%%])\n",
4442
              get_component_name(i), cpi->frame_component_time[i],
4443
              (float)((float)cpi->frame_component_time[i] * 100.0 /
4444
                      (float)frame_total),
4445
              cpi->component_time[i],
4446
              (float)((float)cpi->component_time[i] * 100.0 / (float)total));
4447
      cpi->frame_component_time[i] = 0;
4448
    }
4449
  }
4450
#endif
4451
4452
2.52k
  if (result == -1) {
4453
#if CONFIG_FRAME_PARALLEL_ENCODE
4454
    cm->error->setjmp = 0;
4455
#endif
4456
    // Returning -1 indicates no frame encoded; more input is required
4457
1.26k
    return -1;
4458
1.26k
  }
4459
1.26k
  if (result != AOM_CODEC_OK) {
4460
#if CONFIG_FRAME_PARALLEL_ENCODE
4461
    aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
4462
                       "Failed to encode frame");
4463
#else
4464
0
    return AOM_CODEC_ERROR;
4465
0
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
4466
0
  }
4467
#if CONFIG_INTERNAL_STATS
4468
  aom_usec_timer_mark(&cmptimer);
4469
  cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
4470
#endif  // CONFIG_INTERNAL_STATS
4471
4472
#if CONFIG_SPEED_STATS
4473
  if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) {
4474
    cpi->tx_search_count += cpi->td.mb.txfm_search_info.tx_search_count;
4475
    cpi->td.mb.txfm_search_info.tx_search_count = 0;
4476
  }
4477
#endif  // CONFIG_SPEED_STATS
4478
4479
#if CONFIG_FRAME_PARALLEL_ENCODE
4480
  cm->error->setjmp = 0;
4481
#endif
4482
1.26k
  return AOM_CODEC_OK;
4483
1.26k
}
4484
4485
#if CONFIG_FRAME_PARALLEL_ENCODE
4486
// Populates cpi->scaled_ref_buf corresponding to frames in a parallel encode
4487
// set. Also sets the bitmask 'ref_buffers_used_map'.
4488
void av1_scale_references_fpmt(AV1_COMP *cpi, int *ref_buffers_used_map) {
4489
  AV1_COMMON *cm = &cpi->common;
4490
  MV_REFERENCE_FRAME ref_frame;
4491
4492
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4493
    // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
4494
    if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
4495
      const YV12_BUFFER_CONFIG *const ref =
4496
          get_ref_frame_yv12_buf(cm, ref_frame);
4497
4498
      if (ref == NULL) {
4499
        cpi->scaled_ref_buf[ref_frame - 1] = NULL;
4500
        continue;
4501
      }
4502
4503
      // FPMT does not support scaling yet.
4504
      assert(ref->y_crop_width == cm->width &&
4505
             ref->y_crop_height == cm->height);
4506
4507
      RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
4508
      cpi->scaled_ref_buf[ref_frame - 1] = buf;
4509
      for (int i = 0; i < FRAME_BUFFERS; ++i) {
4510
        if (&cm->buffer_pool->frame_bufs[i] == buf) {
4511
          *ref_buffers_used_map |= (1 << i);
4512
        }
4513
      }
4514
    } else {
4515
      if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
4516
    }
4517
  }
4518
}
4519
4520
// Increments the ref_count of frame buffers referenced by cpi->scaled_ref_buf
4521
// corresponding to frames in a parallel encode set.
4522
void av1_increment_scaled_ref_counts_fpmt(BufferPool *buffer_pool,
4523
                                          int ref_buffers_used_map) {
4524
  for (int i = 0; i < FRAME_BUFFERS; ++i) {
4525
    if (ref_buffers_used_map & (1 << i)) {
4526
      ++buffer_pool->frame_bufs[i].ref_count;
4527
    }
4528
  }
4529
}
4530
4531
// Releases cpi->scaled_ref_buf corresponding to frames in a parallel encode
4532
// set.
4533
void av1_release_scaled_references_fpmt(AV1_COMP *cpi) {
4534
  // TODO(isbs): only refresh the necessary frames, rather than all of them
4535
  for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4536
    RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
4537
    if (buf != NULL) {
4538
      cpi->scaled_ref_buf[i] = NULL;
4539
    }
4540
  }
4541
}
4542
4543
// Decrements the ref_count of frame buffers referenced by cpi->scaled_ref_buf
4544
// corresponding to frames in a parallel encode set.
4545
void av1_decrement_ref_counts_fpmt(BufferPool *buffer_pool,
4546
                                   int ref_buffers_used_map) {
4547
  for (int i = 0; i < FRAME_BUFFERS; ++i) {
4548
    if (ref_buffers_used_map & (1 << i)) {
4549
      --buffer_pool->frame_bufs[i].ref_count;
4550
    }
4551
  }
4552
}
4553
4554
// Initialize parallel frame contexts with screen content decisions.
4555
void av1_init_sc_decisions(AV1_PRIMARY *const ppi) {
4556
  AV1_COMP *const first_cpi = ppi->cpi;
4557
  for (int i = 1; i < ppi->num_fp_contexts; ++i) {
4558
    AV1_COMP *cur_cpi = ppi->parallel_cpi[i];
4559
    cur_cpi->common.features.allow_screen_content_tools =
4560
        first_cpi->common.features.allow_screen_content_tools;
4561
    cur_cpi->common.features.allow_intrabc =
4562
        first_cpi->common.features.allow_intrabc;
4563
    cur_cpi->use_screen_content_tools = first_cpi->use_screen_content_tools;
4564
    cur_cpi->is_screen_content_type = first_cpi->is_screen_content_type;
4565
  }
4566
}
4567
4568
AV1_COMP *av1_get_parallel_frame_enc_data(AV1_PRIMARY *const ppi,
4569
                                          AV1_COMP_DATA *const first_cpi_data) {
4570
  int cpi_idx = 0;
4571
4572
  // Loop over parallel_cpi to find the cpi that processed the current
4573
  // gf_frame_index ahead of time.
4574
  for (int i = 1; i < ppi->num_fp_contexts; i++) {
4575
    if (ppi->cpi->gf_frame_index == ppi->parallel_cpi[i]->gf_frame_index) {
4576
      cpi_idx = i;
4577
      break;
4578
    }
4579
  }
4580
4581
  assert(cpi_idx > 0);
4582
  assert(!ppi->parallel_cpi[cpi_idx]->common.show_existing_frame);
4583
4584
  // Release the previously-used frame-buffer.
4585
  if (ppi->cpi->common.cur_frame != NULL) {
4586
    --ppi->cpi->common.cur_frame->ref_count;
4587
    ppi->cpi->common.cur_frame = NULL;
4588
  }
4589
4590
  // Swap the appropriate parallel_cpi with the parallel_cpi[0].
4591
  ppi->cpi = ppi->parallel_cpi[cpi_idx];
4592
  ppi->parallel_cpi[cpi_idx] = ppi->parallel_cpi[0];
4593
  ppi->parallel_cpi[0] = ppi->cpi;
4594
4595
  // Copy appropriate parallel_frames_data to local data.
4596
  {
4597
    AV1_COMP_DATA *data = &ppi->parallel_frames_data[cpi_idx - 1];
4598
    assert(data->frame_size > 0);
4599
    assert(first_cpi_data->cx_data_sz > data->frame_size);
4600
4601
    first_cpi_data->lib_flags = data->lib_flags;
4602
    first_cpi_data->ts_frame_start = data->ts_frame_start;
4603
    first_cpi_data->ts_frame_end = data->ts_frame_end;
4604
    memcpy(first_cpi_data->cx_data, data->cx_data, data->frame_size);
4605
    first_cpi_data->frame_size = data->frame_size;
4606
    if (ppi->cpi->common.show_frame) {
4607
      first_cpi_data->pop_lookahead = 1;
4608
    }
4609
  }
4610
4611
  return ppi->cpi;
4612
}
4613
4614
// Initialises frames belonging to a parallel encode set.
4615
int av1_init_parallel_frame_context(const AV1_COMP_DATA *const first_cpi_data,
4616
                                    AV1_PRIMARY *const ppi,
4617
                                    int *ref_buffers_used_map) {
4618
  AV1_COMP *const first_cpi = ppi->cpi;
4619
  GF_GROUP *const gf_group = &ppi->gf_group;
4620
  int gf_index_start = first_cpi->gf_frame_index;
4621
  assert(gf_group->frame_parallel_level[gf_index_start] == 1);
4622
  int parallel_frame_count = 0;
4623
  int cur_frame_num = first_cpi->common.current_frame.frame_number;
4624
  int show_frame_count = first_cpi->frame_index_set.show_frame_count;
4625
  int frames_since_key = first_cpi->rc.frames_since_key;
4626
  int frames_to_key = first_cpi->rc.frames_to_key;
4627
  int frames_to_fwd_kf = first_cpi->rc.frames_to_fwd_kf;
4628
  int cur_frame_disp = cur_frame_num + gf_group->arf_src_offset[gf_index_start];
4629
  const FIRSTPASS_STATS *stats_in = first_cpi->twopass_frame.stats_in;
4630
4631
  assert(*ref_buffers_used_map == 0);
4632
4633
  // Release the previously used frame-buffer by a frame_parallel_level 1 frame.
4634
  if (first_cpi->common.cur_frame != NULL) {
4635
    --first_cpi->common.cur_frame->ref_count;
4636
    first_cpi->common.cur_frame = NULL;
4637
  }
4638
4639
  RefFrameMapPair ref_frame_map_pairs[REF_FRAMES];
4640
  RefFrameMapPair first_ref_frame_map_pairs[REF_FRAMES];
4641
  init_ref_map_pair(first_cpi, first_ref_frame_map_pairs);
4642
  memcpy(ref_frame_map_pairs, first_ref_frame_map_pairs,
4643
         sizeof(RefFrameMapPair) * REF_FRAMES);
4644
4645
#if CONFIG_FRAME_PARALLEL_ENCODE_2
4646
  // Store the reference refresh index of frame_parallel_level 1 frame in a
4647
  // parallel encode set of lower layer frames.
4648
  if (gf_group->update_type[gf_index_start] == INTNL_ARF_UPDATE) {
4649
    first_cpi->ref_refresh_index = av1_calc_refresh_idx_for_intnl_arf(
4650
        first_cpi, ref_frame_map_pairs, gf_index_start);
4651
    assert(first_cpi->ref_refresh_index != INVALID_IDX &&
4652
           first_cpi->ref_refresh_index < REF_FRAMES);
4653
    first_cpi->refresh_idx_available = true;
4654
    // Update ref_frame_map_pairs.
4655
    ref_frame_map_pairs[first_cpi->ref_refresh_index].disp_order =
4656
        gf_group->display_idx[gf_index_start];
4657
    ref_frame_map_pairs[first_cpi->ref_refresh_index].pyr_level =
4658
        gf_group->layer_depth[gf_index_start];
4659
  }
4660
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
4661
4662
  // Set do_frame_data_update flag as false for frame_parallel_level 1 frame.
4663
  first_cpi->do_frame_data_update = false;
4664
  if (gf_group->arf_src_offset[gf_index_start] == 0) {
4665
    first_cpi->time_stamps.prev_ts_start = ppi->ts_start_last_show_frame;
4666
    first_cpi->time_stamps.prev_ts_end = ppi->ts_end_last_show_frame;
4667
  }
4668
4669
  av1_get_ref_frames(NULL, first_ref_frame_map_pairs, cur_frame_disp,
4670
#if CONFIG_FRAME_PARALLEL_ENCODE_2
4671
                     first_cpi, gf_index_start, 1,
4672
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
4673
                     first_cpi->common.remapped_ref_idx);
4674
4675
  av1_scale_references_fpmt(first_cpi, ref_buffers_used_map);
4676
  parallel_frame_count++;
4677
4678
  // Iterate through the GF_GROUP to find the remaining frame_parallel_level 2
4679
  // frames which are part of the current parallel encode set and initialize the
4680
  // required cpi elements.
4681
  for (int i = gf_index_start + 1; i < gf_group->size; i++) {
4682
    // Update frame counters if previous frame was show frame or show existing
4683
    // frame.
4684
    if (gf_group->arf_src_offset[i - 1] == 0) {
4685
      cur_frame_num++;
4686
      show_frame_count++;
4687
      if (frames_to_fwd_kf <= 0)
4688
        frames_to_fwd_kf = first_cpi->oxcf.kf_cfg.fwd_kf_dist;
4689
      if (frames_to_key) {
4690
        frames_since_key++;
4691
        frames_to_key--;
4692
        frames_to_fwd_kf--;
4693
      }
4694
      stats_in++;
4695
    }
4696
    cur_frame_disp = cur_frame_num + gf_group->arf_src_offset[i];
4697
    if (gf_group->frame_parallel_level[i] == 2) {
4698
      AV1_COMP *cur_cpi = ppi->parallel_cpi[parallel_frame_count];
4699
      AV1_COMP_DATA *cur_cpi_data =
4700
          &ppi->parallel_frames_data[parallel_frame_count - 1];
4701
      cur_cpi->gf_frame_index = i;
4702
      cur_cpi->framerate = first_cpi->framerate;
4703
      cur_cpi->common.current_frame.frame_number = cur_frame_num;
4704
      cur_cpi->common.current_frame.frame_type = gf_group->frame_type[i];
4705
      cur_cpi->frame_index_set.show_frame_count = show_frame_count;
4706
      cur_cpi->rc.frames_since_key = frames_since_key;
4707
      cur_cpi->rc.frames_to_key = frames_to_key;
4708
      cur_cpi->rc.frames_to_fwd_kf = frames_to_fwd_kf;
4709
      cur_cpi->rc.active_worst_quality = first_cpi->rc.active_worst_quality;
4710
      cur_cpi->rc.avg_frame_bandwidth = first_cpi->rc.avg_frame_bandwidth;
4711
      cur_cpi->rc.max_frame_bandwidth = first_cpi->rc.max_frame_bandwidth;
4712
      cur_cpi->rc.min_frame_bandwidth = first_cpi->rc.min_frame_bandwidth;
4713
      cur_cpi->rc.intervals_till_gf_calculate_due =
4714
          first_cpi->rc.intervals_till_gf_calculate_due;
4715
      cur_cpi->mv_search_params.max_mv_magnitude =
4716
          first_cpi->mv_search_params.max_mv_magnitude;
4717
      if (gf_group->update_type[cur_cpi->gf_frame_index] == INTNL_ARF_UPDATE) {
4718
        cur_cpi->common.lf.mode_ref_delta_enabled = 1;
4719
      }
4720
      cur_cpi->do_frame_data_update = false;
4721
      // Initialize prev_ts_start and prev_ts_end for show frame(s) and show
4722
      // existing frame(s).
4723
      if (gf_group->arf_src_offset[i] == 0) {
4724
        // Choose source of prev frame.
4725
        int src_index = gf_group->src_offset[i];
4726
        struct lookahead_entry *prev_source = av1_lookahead_peek(
4727
            ppi->lookahead, src_index - 1, cur_cpi->compressor_stage);
4728
        // Save timestamps of prev frame.
4729
        cur_cpi->time_stamps.prev_ts_start = prev_source->ts_start;
4730
        cur_cpi->time_stamps.prev_ts_end = prev_source->ts_end;
4731
      }
4732
      cur_cpi->time_stamps.first_ts_start =
4733
          first_cpi->time_stamps.first_ts_start;
4734
4735
      memcpy(cur_cpi->common.ref_frame_map, first_cpi->common.ref_frame_map,
4736
             sizeof(first_cpi->common.ref_frame_map));
4737
      cur_cpi_data->lib_flags = 0;
4738
      cur_cpi_data->timestamp_ratio = first_cpi_data->timestamp_ratio;
4739
      cur_cpi_data->flush = first_cpi_data->flush;
4740
      cur_cpi_data->frame_size = 0;
4741
#if CONFIG_FRAME_PARALLEL_ENCODE_2
4742
      if (gf_group->update_type[gf_index_start] == INTNL_ARF_UPDATE) {
4743
        // If the first frame in a parallel encode set is INTNL_ARF_UPDATE
4744
        // frame, initialize lib_flags of frame_parallel_level 2 frame in the
4745
        // set with that of frame_parallel_level 1 frame.
4746
        cur_cpi_data->lib_flags = first_cpi_data->lib_flags;
4747
        // Store the reference refresh index of frame_parallel_level 2 frame in
4748
        // a parallel encode set of lower layer frames.
4749
        cur_cpi->ref_refresh_index =
4750
            av1_calc_refresh_idx_for_intnl_arf(cur_cpi, ref_frame_map_pairs, i);
4751
        cur_cpi->refresh_idx_available = true;
4752
        // Skip the reference frame which will be refreshed by
4753
        // frame_parallel_level 1 frame in a parallel encode set of lower layer
4754
        // frames.
4755
        cur_cpi->ref_idx_to_skip = first_cpi->ref_refresh_index;
4756
      } else {
4757
        cur_cpi->ref_idx_to_skip = INVALID_IDX;
4758
        cur_cpi->ref_refresh_index = INVALID_IDX;
4759
        cur_cpi->refresh_idx_available = false;
4760
      }
4761
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
4762
      cur_cpi->twopass_frame.stats_in = stats_in;
4763
4764
      av1_get_ref_frames(NULL, first_ref_frame_map_pairs, cur_frame_disp,
4765
#if CONFIG_FRAME_PARALLEL_ENCODE_2
4766
                         cur_cpi, i, 1,
4767
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
4768
                         cur_cpi->common.remapped_ref_idx);
4769
      av1_scale_references_fpmt(cur_cpi, ref_buffers_used_map);
4770
      parallel_frame_count++;
4771
    }
4772
4773
    // Set do_frame_data_update to true for the last frame_parallel_level 2
4774
    // frame in the current parallel encode set.
4775
    if (i == (gf_group->size - 1) ||
4776
        (gf_group->frame_parallel_level[i + 1] == 0 &&
4777
         (gf_group->update_type[i + 1] == ARF_UPDATE ||
4778
          gf_group->update_type[i + 1] == INTNL_ARF_UPDATE)) ||
4779
        gf_group->frame_parallel_level[i + 1] == 1) {
4780
      ppi->parallel_cpi[parallel_frame_count - 1]->do_frame_data_update = true;
4781
      break;
4782
    }
4783
  }
4784
4785
  av1_increment_scaled_ref_counts_fpmt(first_cpi->common.buffer_pool,
4786
                                       *ref_buffers_used_map);
4787
4788
  // Return the number of frames in the parallel encode set.
4789
  return parallel_frame_count;
4790
}
4791
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
4792
4793
0
int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
4794
0
  AV1_COMMON *cm = &cpi->common;
4795
0
  if (!cm->show_frame) {
4796
0
    return -1;
4797
0
  } else {
4798
0
    int ret;
4799
0
    if (cm->cur_frame != NULL) {
4800
0
      *dest = cm->cur_frame->buf;
4801
0
      dest->y_width = cm->width;
4802
0
      dest->y_height = cm->height;
4803
0
      dest->uv_width = cm->width >> cm->seq_params->subsampling_x;
4804
0
      dest->uv_height = cm->height >> cm->seq_params->subsampling_y;
4805
0
      ret = 0;
4806
0
    } else {
4807
0
      ret = -1;
4808
0
    }
4809
0
    return ret;
4810
0
  }
4811
0
}
4812
4813
0
int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
4814
0
  if (cpi->last_show_frame_buf == NULL) return -1;
4815
4816
0
  *frame = cpi->last_show_frame_buf->buf;
4817
0
  return 0;
4818
0
}
4819
4820
aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
4821
                                       YV12_BUFFER_CONFIG *new_frame,
4822
0
                                       YV12_BUFFER_CONFIG *sd) {
4823
0
  const int num_planes = av1_num_planes(cm);
4824
0
  if (!equal_dimensions_and_border(new_frame, sd))
4825
0
    aom_internal_error(cm->error, AOM_CODEC_ERROR,
4826
0
                       "Incorrect buffer dimensions");
4827
0
  else
4828
0
    aom_yv12_copy_frame(new_frame, sd, num_planes);
4829
4830
0
  return cm->error->error_code;
4831
0
}
4832
4833
int av1_set_internal_size(AV1EncoderConfig *const oxcf,
4834
                          ResizePendingParams *resize_pending_params,
4835
0
                          AOM_SCALING horiz_mode, AOM_SCALING vert_mode) {
4836
0
  int hr = 0, hs = 0, vr = 0, vs = 0;
4837
4838
0
  if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
4839
4840
0
  Scale2Ratio(horiz_mode, &hr, &hs);
4841
0
  Scale2Ratio(vert_mode, &vr, &vs);
4842
4843
  // always go to the next whole number
4844
0
  resize_pending_params->width = (hs - 1 + oxcf->frm_dim_cfg.width * hr) / hs;
4845
0
  resize_pending_params->height = (vs - 1 + oxcf->frm_dim_cfg.height * vr) / vs;
4846
4847
0
  if (horiz_mode != NORMAL || vert_mode != NORMAL) {
4848
0
    oxcf->resize_cfg.resize_mode = RESIZE_FIXED;
4849
0
    oxcf->algo_cfg.enable_tpl_model = 0;
4850
0
  }
4851
0
  return 0;
4852
0
}
4853
4854
0
int av1_get_quantizer(AV1_COMP *cpi) {
4855
0
  return cpi->common.quant_params.base_qindex;
4856
0
}
4857
4858
0
int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
4859
0
  size_t output_size = 0;
4860
0
  size_t total_bytes_read = 0;
4861
0
  size_t remaining_size = *frame_size;
4862
0
  uint8_t *buff_ptr = buffer;
4863
4864
  // go through each OBUs
4865
0
  while (total_bytes_read < *frame_size) {
4866
0
    uint8_t saved_obu_header[2];
4867
0
    uint64_t obu_payload_size;
4868
0
    size_t length_of_payload_size;
4869
0
    size_t length_of_obu_size;
4870
0
    uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
4871
0
    size_t obu_bytes_read = obu_header_size;  // bytes read for current obu
4872
4873
    // save the obu header (1 or 2 bytes)
4874
0
    memmove(saved_obu_header, buff_ptr, obu_header_size);
4875
    // clear the obu_has_size_field
4876
0
    saved_obu_header[0] = saved_obu_header[0] & (~0x2);
4877
4878
    // get the payload_size and length of payload_size
4879
0
    if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
4880
0
                        &obu_payload_size, &length_of_payload_size) != 0) {
4881
0
      return AOM_CODEC_ERROR;
4882
0
    }
4883
0
    obu_bytes_read += length_of_payload_size;
4884
4885
    // calculate the length of size of the obu header plus payload
4886
0
    length_of_obu_size =
4887
0
        aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
4888
4889
    // move the rest of data to new location
4890
0
    memmove(buff_ptr + length_of_obu_size + obu_header_size,
4891
0
            buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
4892
0
    obu_bytes_read += (size_t)obu_payload_size;
4893
4894
    // write the new obu size
4895
0
    const uint64_t obu_size = obu_header_size + obu_payload_size;
4896
0
    size_t coded_obu_size;
4897
0
    if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
4898
0
                        &coded_obu_size) != 0) {
4899
0
      return AOM_CODEC_ERROR;
4900
0
    }
4901
4902
    // write the saved (modified) obu_header following obu size
4903
0
    memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
4904
4905
0
    total_bytes_read += obu_bytes_read;
4906
0
    remaining_size -= obu_bytes_read;
4907
0
    buff_ptr += length_of_obu_size + obu_size;
4908
0
    output_size += length_of_obu_size + (size_t)obu_size;
4909
0
  }
4910
4911
0
  *frame_size = output_size;
4912
0
  return AOM_CODEC_OK;
4913
0
}
4914
4915
static void svc_set_updates_ref_frame_config(
4916
0
    ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags, SVC *const svc) {
4917
0
  ext_refresh_frame_flags->update_pending = 1;
4918
0
  ext_refresh_frame_flags->last_frame = svc->refresh[svc->ref_idx[0]];
4919
0
  ext_refresh_frame_flags->golden_frame = svc->refresh[svc->ref_idx[3]];
4920
0
  ext_refresh_frame_flags->bwd_ref_frame = svc->refresh[svc->ref_idx[4]];
4921
0
  ext_refresh_frame_flags->alt2_ref_frame = svc->refresh[svc->ref_idx[5]];
4922
0
  ext_refresh_frame_flags->alt_ref_frame = svc->refresh[svc->ref_idx[6]];
4923
0
  svc->non_reference_frame = 1;
4924
0
  for (int i = 0; i < REF_FRAMES; i++) {
4925
0
    if (svc->refresh[i] == 1) {
4926
0
      svc->non_reference_frame = 0;
4927
0
      break;
4928
0
    }
4929
0
  }
4930
0
}
4931
4932
0
static int svc_set_references_external_ref_frame_config(AV1_COMP *cpi) {
4933
  // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
4934
  // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
4935
0
  int ref = AOM_REFFRAME_ALL;
4936
0
  for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
4937
0
    if (!cpi->svc.reference[i]) ref ^= (1 << i);
4938
0
  }
4939
0
  return ref;
4940
0
}
4941
4942
3.78k
void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
4943
  // TODO(yunqingwang): For what references to use, external encoding flags
4944
  // should be consistent with internal reference frame selection. Need to
4945
  // ensure that there is not conflict between the two. In AV1 encoder, the
4946
  // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
4947
  // GOLDEN, BWDREF, ALTREF2.
4948
4949
3.78k
  ExternalFlags *const ext_flags = &cpi->ext_flags;
4950
3.78k
  ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
4951
3.78k
      &ext_flags->refresh_frame;
4952
3.78k
  ext_flags->ref_frame_flags = AOM_REFFRAME_ALL;
4953
3.78k
  if (flags &
4954
3.78k
      (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
4955
3.78k
       AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
4956
3.78k
       AOM_EFLAG_NO_REF_ARF2)) {
4957
0
    int ref = AOM_REFFRAME_ALL;
4958
4959
0
    if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
4960
0
    if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
4961
0
    if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
4962
4963
0
    if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
4964
4965
0
    if (flags & AOM_EFLAG_NO_REF_ARF) {
4966
0
      ref ^= AOM_ALT_FLAG;
4967
0
      ref ^= AOM_BWD_FLAG;
4968
0
      ref ^= AOM_ALT2_FLAG;
4969
0
    } else {
4970
0
      if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
4971
0
      if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
4972
0
    }
4973
4974
0
    av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
4975
3.78k
  } else {
4976
3.78k
    if (cpi->svc.set_ref_frame_config) {
4977
0
      int ref = svc_set_references_external_ref_frame_config(cpi);
4978
0
      av1_use_as_reference(&ext_flags->ref_frame_flags, ref);
4979
0
    }
4980
3.78k
  }
4981
4982
3.78k
  if (flags &
4983
3.78k
      (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
4984
0
    int upd = AOM_REFFRAME_ALL;
4985
4986
    // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
4987
0
    if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
4988
4989
0
    if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
4990
4991
0
    if (flags & AOM_EFLAG_NO_UPD_ARF) {
4992
0
      upd ^= AOM_ALT_FLAG;
4993
0
      upd ^= AOM_BWD_FLAG;
4994
0
      upd ^= AOM_ALT2_FLAG;
4995
0
    }
4996
4997
0
    ext_refresh_frame_flags->last_frame = (upd & AOM_LAST_FLAG) != 0;
4998
0
    ext_refresh_frame_flags->golden_frame = (upd & AOM_GOLD_FLAG) != 0;
4999
0
    ext_refresh_frame_flags->alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
5000
0
    ext_refresh_frame_flags->bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
5001
0
    ext_refresh_frame_flags->alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
5002
0
    ext_refresh_frame_flags->update_pending = 1;
5003
3.78k
  } else {
5004
3.78k
    if (cpi->svc.set_ref_frame_config)
5005
0
      svc_set_updates_ref_frame_config(ext_refresh_frame_flags, &cpi->svc);
5006
3.78k
    else
5007
3.78k
      ext_refresh_frame_flags->update_pending = 0;
5008
3.78k
  }
5009
5010
3.78k
  ext_flags->use_ref_frame_mvs = cpi->oxcf.tool_cfg.enable_ref_frame_mvs &
5011
3.78k
                                 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
5012
3.78k
  ext_flags->use_error_resilient = cpi->oxcf.tool_cfg.error_resilient_mode |
5013
3.78k
                                   ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
5014
3.78k
  ext_flags->use_s_frame =
5015
3.78k
      cpi->oxcf.kf_cfg.enable_sframe | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
5016
3.78k
  ext_flags->use_primary_ref_none =
5017
3.78k
      (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
5018
5019
3.78k
  if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
5020
0
    update_entropy(&ext_flags->refresh_frame_context,
5021
0
                   &ext_flags->refresh_frame_context_pending, 0);
5022
0
  }
5023
3.78k
}
5024
5025
0
aom_fixed_buf_t *av1_get_global_headers(AV1_PRIMARY *ppi) {
5026
0
  if (!ppi) return NULL;
5027
5028
0
  uint8_t header_buf[512] = { 0 };
5029
0
  const uint32_t sequence_header_size =
5030
0
      av1_write_sequence_header_obu(&ppi->seq_params, &header_buf[0]);
5031
0
  assert(sequence_header_size <= sizeof(header_buf));
5032
0
  if (sequence_header_size == 0) return NULL;
5033
5034
0
  const size_t obu_header_size = 1;
5035
0
  const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
5036
0
  const size_t payload_offset = obu_header_size + size_field_size;
5037
5038
0
  if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
5039
0
  memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
5040
5041
0
  if (av1_write_obu_header(&ppi->level_params, &ppi->cpi->frame_header_count,
5042
0
                           OBU_SEQUENCE_HEADER, 0,
5043
0
                           &header_buf[0]) != obu_header_size) {
5044
0
    return NULL;
5045
0
  }
5046
5047
0
  size_t coded_size_field_size = 0;
5048
0
  if (aom_uleb_encode(sequence_header_size, size_field_size,
5049
0
                      &header_buf[obu_header_size],
5050
0
                      &coded_size_field_size) != 0) {
5051
0
    return NULL;
5052
0
  }
5053
0
  assert(coded_size_field_size == size_field_size);
5054
5055
0
  aom_fixed_buf_t *global_headers =
5056
0
      (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
5057
0
  if (!global_headers) return NULL;
5058
5059
0
  const size_t global_header_buf_size =
5060
0
      obu_header_size + size_field_size + sequence_header_size;
5061
5062
0
  global_headers->buf = malloc(global_header_buf_size);
5063
0
  if (!global_headers->buf) {
5064
0
    free(global_headers);
5065
0
    return NULL;
5066
0
  }
5067
5068
0
  memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
5069
0
  global_headers->sz = global_header_buf_size;
5070
0
  return global_headers;
5071
0
}