Coverage Report

Created: 2022-08-24 06:17

/src/aom/av1/encoder/thirdpass.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2021, 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 "aom/aom_codec.h"
13
#include "aom/aomdx.h"
14
#include "aom_dsp/psnr.h"
15
#include "aom_mem/aom_mem.h"
16
#include "av1/av1_iface_common.h"
17
#include "av1/encoder/encoder.h"
18
#include "av1/encoder/firstpass.h"
19
#include "av1/encoder/thirdpass.h"
20
#include "av1/common/blockd.h"
21
22
#if CONFIG_THREE_PASS
23
#include "common/ivfdec.h"
24
#endif
25
26
#if CONFIG_THREE_PASS
27
static void setup_two_pass_stream_input(
28
    struct AvxInputContext **input_ctx_ptr, const char *input_file_name,
29
    struct aom_internal_error_info *err_info) {
30
  FILE *infile;
31
  infile = fopen(input_file_name, "rb");
32
  if (!infile) {
33
    aom_internal_error(err_info, AOM_CODEC_INVALID_PARAM,
34
                       "Failed to open input file '%s'.", input_file_name);
35
  }
36
  struct AvxInputContext *aom_input_ctx = aom_malloc(sizeof(*aom_input_ctx));
37
  if (!aom_input_ctx) {
38
    fclose(infile);
39
    aom_internal_error(err_info, AOM_CODEC_MEM_ERROR,
40
                       "Failed to allocate memory for third-pass context.");
41
  }
42
  memset(aom_input_ctx, 0, sizeof(*aom_input_ctx));
43
  aom_input_ctx->filename = input_file_name;
44
  aom_input_ctx->file = infile;
45
46
  if (file_is_ivf(aom_input_ctx)) {
47
    aom_input_ctx->file_type = FILE_TYPE_IVF;
48
  } else {
49
    fclose(infile);
50
    aom_free(aom_input_ctx);
51
    aom_internal_error(err_info, AOM_CODEC_INVALID_PARAM,
52
                       "Unrecognized input file type.");
53
  }
54
  *input_ctx_ptr = aom_input_ctx;
55
}
56
57
static void init_third_pass(THIRD_PASS_DEC_CTX *ctx) {
58
  if (!ctx->input_ctx) {
59
    if (ctx->input_file_name == NULL) {
60
      aom_internal_error(ctx->err_info, AOM_CODEC_INVALID_PARAM,
61
                         "No third pass input specified.");
62
    }
63
    setup_two_pass_stream_input(&ctx->input_ctx, ctx->input_file_name,
64
                                ctx->err_info);
65
  }
66
67
#if CONFIG_AV1_DECODER
68
  if (!ctx->decoder.iface) {
69
    aom_codec_iface_t *decoder_iface = &aom_codec_av1_inspect_algo;
70
    if (aom_codec_dec_init(&ctx->decoder, decoder_iface, NULL, 0)) {
71
      aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
72
                         "Failed to initialize decoder.");
73
    }
74
  }
75
#else
76
  aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
77
                     "To utilize three-pass encoding, libaom must be built "
78
                     "with CONFIG_AV1_DECODER=1.");
79
#endif
80
}
81
#endif  // CONFIG_THREE_PASS
82
83
// Return 0: success
84
//        1: cannot read because this is end of file
85
//       -1: failure to read the frame
86
0
static int read_frame(THIRD_PASS_DEC_CTX *ctx) {
87
#if CONFIG_THREE_PASS
88
  if (!ctx->input_ctx || !ctx->decoder.iface) {
89
    init_third_pass(ctx);
90
  }
91
  if (!ctx->have_frame) {
92
    if (ivf_read_frame(ctx->input_ctx->file, &ctx->buf, &ctx->bytes_in_buffer,
93
                       &ctx->buffer_size, NULL) != 0) {
94
      if (feof(ctx->input_ctx->file)) {
95
        return 1;
96
      } else {
97
        return -1;
98
      }
99
    }
100
    ctx->frame = ctx->buf;
101
    ctx->end_frame = ctx->frame + ctx->bytes_in_buffer;
102
    ctx->have_frame = 1;
103
  }
104
#else
105
0
  aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
106
0
                     "Cannot parse bitstream without CONFIG_THREE_PASS.");
107
0
#endif
108
0
  Av1DecodeReturn adr;
109
0
  if (aom_codec_decode(&ctx->decoder, ctx->frame,
110
0
                       (unsigned int)ctx->bytes_in_buffer,
111
0
                       &adr) != AOM_CODEC_OK) {
112
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
113
0
                       "Failed to decode frame for third pass.");
114
0
  }
115
0
  ctx->this_frame_bits = (int)(adr.buf - ctx->frame) << 3;
116
0
  ctx->frame = adr.buf;
117
0
  ctx->bytes_in_buffer = ctx->end_frame - ctx->frame;
118
0
  if (ctx->frame == ctx->end_frame) ctx->have_frame = 0;
119
0
  return 0;
120
0
}
121
122
0
static void free_frame_info(THIRD_PASS_FRAME_INFO *frame_info) {
123
0
  if (!frame_info) return;
124
0
  aom_free(frame_info->mi_info);
125
0
  frame_info->mi_info = NULL;
126
0
}
127
128
// This function gets the information needed from the recently decoded frame,
129
// via various decoder APIs, and saves the info into ctx->frame_info.
130
// Return 0: success
131
//        1: cannot read because this is end of file
132
//       -1: failure to read the frame
133
0
static int get_frame_info(THIRD_PASS_DEC_CTX *ctx) {
134
0
  int ret = read_frame(ctx);
135
0
  if (ret != 0) return ret;
136
0
  int cur = ctx->frame_info_count;
137
138
0
  ctx->frame_info[cur].actual_bits = ctx->this_frame_bits;
139
140
0
  if (cur >= MAX_THIRD_PASS_BUF) {
141
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
142
0
                       "Third pass frame info ran out of available slots.");
143
0
  }
144
0
  int frame_type_flags = 0;
145
0
  if (aom_codec_control(&ctx->decoder, AOMD_GET_FRAME_FLAGS,
146
0
                        &frame_type_flags) != AOM_CODEC_OK) {
147
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
148
0
                       "Failed to read frame flags.");
149
0
  }
150
0
  if (frame_type_flags & AOM_FRAME_IS_KEY) {
151
0
    ctx->frame_info[cur].frame_type = KEY_FRAME;
152
0
  } else if (frame_type_flags & AOM_FRAME_IS_INTRAONLY) {
153
0
    ctx->frame_info[cur].frame_type = INTRA_ONLY_FRAME;
154
0
  } else if (frame_type_flags & AOM_FRAME_IS_SWITCH) {
155
0
    ctx->frame_info[cur].frame_type = S_FRAME;
156
0
  } else {
157
0
    ctx->frame_info[cur].frame_type = INTER_FRAME;
158
0
  }
159
160
  // Get frame width and height
161
0
  int frame_size[2];
162
0
  if (aom_codec_control(&ctx->decoder, AV1D_GET_FRAME_SIZE, frame_size) !=
163
0
      AOM_CODEC_OK) {
164
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
165
0
                       "Failed to read frame size.");
166
0
  }
167
168
  // Check if we need to re-alloc the mi fields.
169
0
  const int mi_cols = (frame_size[0] + 3) >> 2;
170
0
  const int mi_rows = (frame_size[1] + 3) >> 2;
171
0
  ctx->frame_info[cur].mi_stride = mi_cols;
172
0
  ctx->frame_info[cur].mi_rows = mi_rows;
173
0
  ctx->frame_info[cur].mi_cols = mi_cols;
174
175
0
  if (ctx->frame_info[cur].width != frame_size[0] ||
176
0
      ctx->frame_info[cur].height != frame_size[1] ||
177
0
      !ctx->frame_info[cur].mi_info) {
178
0
    free_frame_info(&ctx->frame_info[cur]);
179
180
0
    ctx->frame_info[cur].mi_info =
181
0
        aom_malloc(mi_cols * mi_rows * sizeof(*ctx->frame_info[cur].mi_info));
182
183
0
    if (!ctx->frame_info[cur].mi_info) {
184
0
      aom_internal_error(ctx->err_info, AOM_CODEC_MEM_ERROR,
185
0
                         "Failed to allocate mi buffer for the third pass.");
186
0
    }
187
0
  }
188
189
0
  ctx->frame_info[cur].width = frame_size[0];
190
0
  ctx->frame_info[cur].height = frame_size[1];
191
192
  // Get frame base q idx
193
0
  if (aom_codec_control(&ctx->decoder, AOMD_GET_BASE_Q_IDX,
194
0
                        &ctx->frame_info[cur].base_q_idx) != AOM_CODEC_OK) {
195
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
196
0
                       "Failed to read base q index.");
197
0
  }
198
199
  // Get show existing frame flag
200
0
  if (aom_codec_control(&ctx->decoder, AOMD_GET_SHOW_EXISTING_FRAME_FLAG,
201
0
                        &ctx->frame_info[cur].is_show_existing_frame) !=
202
0
      AOM_CODEC_OK) {
203
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
204
0
                       "Failed to read show existing frame flag.");
205
0
  }
206
207
  // Get show frame flag
208
0
  if (aom_codec_control(&ctx->decoder, AOMD_GET_SHOW_FRAME_FLAG,
209
0
                        &ctx->frame_info[cur].is_show_frame) != AOM_CODEC_OK) {
210
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
211
0
                       "Failed to read show frame flag.");
212
0
  }
213
214
  // Get order hint
215
0
  if (aom_codec_control(&ctx->decoder, AOMD_GET_ORDER_HINT,
216
0
                        &ctx->frame_info[cur].order_hint) != AOM_CODEC_OK) {
217
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
218
0
                       "Failed to read order hint.");
219
0
  }
220
221
  // Clear MI info
222
0
  for (int mi_row = 0; mi_row < mi_rows; mi_row++) {
223
0
    for (int mi_col = 0; mi_col < mi_cols; mi_col++) {
224
0
      ctx->frame_info[cur].mi_info[mi_row * mi_cols + mi_col].bsize =
225
0
          BLOCK_INVALID;
226
0
    }
227
0
  }
228
229
  // Get relevant information regarding each 4x4 MI
230
0
  MB_MODE_INFO cur_mi_info;
231
0
  THIRD_PASS_MI_INFO *const this_mi = ctx->frame_info[cur].mi_info;
232
0
  for (int mi_row = 0; mi_row < mi_rows; mi_row++) {
233
0
    for (int mi_col = 0; mi_col < mi_cols; mi_col++) {
234
0
      const int offset = mi_row * mi_cols + mi_col;
235
0
      if (this_mi[offset].bsize != BLOCK_INVALID) {
236
0
        continue;
237
0
      }
238
      // Get info of this MI
239
0
      if (aom_codec_control(&ctx->decoder, AV1D_GET_MI_INFO, mi_row, mi_col,
240
0
                            &cur_mi_info) != AOM_CODEC_OK) {
241
0
        aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
242
0
                           "Failed to read mi info.");
243
0
      }
244
0
      const int blk_mi_rows = mi_size_high[cur_mi_info.bsize];
245
0
      const int blk_mi_cols = mi_size_wide[cur_mi_info.bsize];
246
247
0
      for (int h = 0; h < blk_mi_rows; h++) {
248
0
        for (int w = 0; w < blk_mi_cols; w++) {
249
0
          if (h + mi_row >= mi_rows || w + mi_col >= mi_cols) {
250
0
            continue;
251
0
          }
252
0
          const int this_offset = offset + h * mi_cols + w;
253
0
          this_mi[this_offset].bsize = cur_mi_info.bsize;
254
0
          this_mi[this_offset].partition = cur_mi_info.partition;
255
0
          this_mi[this_offset].mi_row_start = mi_row;
256
0
          this_mi[this_offset].mi_col_start = mi_col;
257
0
          this_mi[this_offset].mv[0] = cur_mi_info.mv[0];
258
0
          this_mi[this_offset].mv[1] = cur_mi_info.mv[1];
259
0
          this_mi[this_offset].ref_frame[0] = cur_mi_info.ref_frame[0];
260
0
          this_mi[this_offset].ref_frame[1] = cur_mi_info.ref_frame[1];
261
0
          this_mi[this_offset].pred_mode = cur_mi_info.mode;
262
0
        }
263
0
      }
264
0
    }
265
0
  }
266
267
0
  ctx->frame_info_count++;
268
269
0
  return 0;
270
0
}
271
272
#define USE_SECOND_PASS_FILE 1
273
274
#if !USE_SECOND_PASS_FILE
275
// Parse the frames in the gop and determine the last frame of the current GOP.
276
// Decode more frames if necessary. The variable max_num is the maximum static
277
// GOP length if we detect an IPPP structure, and it is expected that max_mum >=
278
// MAX_GF_INTERVAL.
279
static void get_current_gop_end(THIRD_PASS_DEC_CTX *ctx, int max_num,
280
                                int *last_idx) {
281
  assert(max_num >= MAX_GF_INTERVAL);
282
  *last_idx = 0;
283
  int cur_idx = 0;
284
  int arf_order_hint = -1;
285
  int num_show_frames = 0;
286
  while (num_show_frames < max_num) {
287
    assert(cur_idx < MAX_THIRD_PASS_BUF);
288
    // Read in from bitstream if needed.
289
    if (cur_idx >= ctx->frame_info_count) {
290
      int ret = get_frame_info(ctx);
291
      if (ret == 1) {
292
        // At the end of the file, GOP ends in the prev frame.
293
        if (arf_order_hint >= 0) {
294
          aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
295
                             "Failed to derive GOP length.");
296
        }
297
        *last_idx = cur_idx - 1;
298
        return;
299
      }
300
      if (ret < 0) {
301
        aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
302
                           "Failed to read frame for third pass.");
303
      }
304
    }
305
306
    // TODO(bohanli): verify that fwd_kf works here.
307
    if (ctx->frame_info[cur_idx].frame_type == KEY_FRAME &&
308
        ctx->frame_info[cur_idx].is_show_frame) {
309
      if (cur_idx != 0) {
310
        // If this is a key frame and is not the first kf in this kf group, we
311
        // have reached the next key frame. Stop here.
312
        *last_idx = cur_idx - 1;
313
        return;
314
      }
315
    } else if (!ctx->frame_info[cur_idx].is_show_frame &&
316
               arf_order_hint == -1) {
317
      // If this is an arf (the first no show)
318
      if (num_show_frames <= 1) {
319
        // This is an arf and we should end the GOP with its overlay.
320
        arf_order_hint = ctx->frame_info[cur_idx].order_hint;
321
      } else {
322
        // There are multiple show frames before the this arf, so we treat the
323
        // frames previous to this arf as a GOP.
324
        *last_idx = cur_idx - 1;
325
        return;
326
      }
327
    } else if (arf_order_hint >= 0 && ctx->frame_info[cur_idx].order_hint ==
328
                                          (unsigned int)arf_order_hint) {
329
      // If this is the overlay/show existing of the arf
330
      assert(ctx->frame_info[cur_idx].is_show_frame);
331
      *last_idx = cur_idx;
332
      return;
333
    } else {
334
      // This frame is part of the GOP.
335
      if (ctx->frame_info[cur_idx].is_show_frame) num_show_frames++;
336
    }
337
    cur_idx++;
338
  }
339
  // This is a long IPPP GOP and we will use a length of max_num here.
340
  assert(arf_order_hint < 0);
341
  *last_idx = max_num - 1;
342
  return;
343
}
344
#endif
345
346
0
static AOM_INLINE void read_gop_frames(THIRD_PASS_DEC_CTX *ctx) {
347
0
  int cur_idx = 0;
348
0
  while (cur_idx < ctx->gop_info.num_frames) {
349
0
    assert(cur_idx < MAX_THIRD_PASS_BUF);
350
    // Read in from bitstream if needed.
351
0
    if (cur_idx >= ctx->frame_info_count) {
352
0
      int ret = get_frame_info(ctx);
353
0
      if (ret != 0) {
354
0
        aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
355
0
                           "Failed to read frame for third pass.");
356
0
      }
357
0
    }
358
0
    cur_idx++;
359
0
  }
360
0
  return;
361
0
}
362
363
0
void av1_set_gop_third_pass(THIRD_PASS_DEC_CTX *ctx) {
364
  // Read in future frames in the current GOP.
365
0
  read_gop_frames(ctx);
366
367
0
  int gf_len = 0;
368
  // Check the GOP length against the value read from second_pass_file
369
0
  for (int i = 0; i < ctx->gop_info.num_frames; i++) {
370
0
    if (ctx->frame_info[i].is_show_frame) gf_len++;
371
0
  }
372
373
0
  if (gf_len != ctx->gop_info.gf_length) {
374
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
375
0
                       "Mismatch in third pass GOP length!");
376
0
  }
377
0
}
378
379
0
void av1_pop_third_pass_info(THIRD_PASS_DEC_CTX *ctx) {
380
0
  if (ctx->frame_info_count == 0) {
381
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
382
0
                       "No available frame info for third pass.");
383
0
  }
384
0
  ctx->frame_info_count--;
385
0
  free_frame_info(&ctx->frame_info[0]);
386
0
  for (int i = 0; i < ctx->frame_info_count; i++) {
387
0
    ctx->frame_info[i] = ctx->frame_info[i + 1];
388
0
  }
389
0
  ctx->frame_info[ctx->frame_info_count].mi_info = NULL;
390
0
}
391
392
void av1_init_thirdpass_ctx(AV1_COMMON *cm, THIRD_PASS_DEC_CTX **ctx,
393
0
                            const char *file) {
394
0
  av1_free_thirdpass_ctx(*ctx);
395
0
  CHECK_MEM_ERROR(cm, *ctx, aom_calloc(1, sizeof(**ctx)));
396
0
  THIRD_PASS_DEC_CTX *ctx_ptr = *ctx;
397
0
  ctx_ptr->input_file_name = file;
398
0
  ctx_ptr->prev_gop_end = -1;
399
0
  ctx_ptr->err_info = cm->error;
400
0
}
401
402
1.26k
void av1_free_thirdpass_ctx(THIRD_PASS_DEC_CTX *ctx) {
403
1.26k
  if (ctx == NULL) return;
404
0
  if (ctx->decoder.iface) {
405
0
    aom_codec_destroy(&ctx->decoder);
406
0
  }
407
#if CONFIG_THREE_PASS
408
  if (ctx->input_ctx && ctx->input_ctx->file) fclose(ctx->input_ctx->file);
409
  aom_free(ctx->input_ctx);
410
#endif
411
0
  if (ctx->buf) free(ctx->buf);
412
0
  for (int i = 0; i < MAX_THIRD_PASS_BUF; i++) {
413
0
    free_frame_info(&ctx->frame_info[i]);
414
0
  }
415
0
  aom_free(ctx);
416
0
}
417
418
1.26k
void av1_write_second_pass_gop_info(AV1_COMP *cpi) {
419
1.26k
  const AV1EncoderConfig *const oxcf = &cpi->oxcf;
420
1.26k
  const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
421
1.26k
  const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
422
423
1.26k
  if (oxcf->pass == AOM_RC_SECOND_PASS && oxcf->second_pass_log) {
424
    // Write the GOP length to a log file.
425
0
    av1_open_second_pass_log(cpi, 0);
426
427
0
    THIRD_PASS_GOP_INFO gop_info;
428
429
0
    gop_info.num_frames = gf_group->size;
430
0
    gop_info.use_arf = (gf_group->arf_index >= 0);
431
0
    gop_info.gf_length = p_rc->baseline_gf_interval;
432
433
0
    size_t count =
434
0
        fwrite(&gop_info, sizeof(gop_info), 1, cpi->second_pass_log_stream);
435
0
    if (count < 1) {
436
0
      aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
437
0
                         "Could not write to second pass log file!");
438
0
    }
439
0
  }
440
1.26k
}
441
442
1.26k
void av1_write_second_pass_per_frame_info(AV1_COMP *cpi, int gf_index) {
443
1.26k
  const AV1EncoderConfig *const oxcf = &cpi->oxcf;
444
1.26k
  const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
445
446
1.26k
  if (oxcf->pass == AOM_RC_SECOND_PASS && oxcf->second_pass_log) {
447
    // write target bitrate
448
0
    int bits = gf_group->bit_allocation[gf_index];
449
0
    size_t count = fwrite(&bits, sizeof(bits), 1, cpi->second_pass_log_stream);
450
0
    if (count < 1) {
451
0
      aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
452
0
                         "Could not write to second pass log file!");
453
0
    }
454
455
    // write sse
456
0
    uint64_t sse = 0;
457
0
    int pkt_idx = cpi->ppi->output_pkt_list->cnt - 1;
458
0
    if (pkt_idx >= 0 &&
459
0
        cpi->ppi->output_pkt_list->pkts[pkt_idx].kind == AOM_CODEC_PSNR_PKT) {
460
0
      sse = cpi->ppi->output_pkt_list->pkts[pkt_idx].data.psnr.sse[0];
461
#if CONFIG_INTERNAL_STATS
462
    } else if (cpi->ppi->b_calculate_psnr) {
463
      sse = cpi->ppi->total_sq_error[0];
464
#endif
465
0
    } else {
466
0
      const YV12_BUFFER_CONFIG *orig = cpi->source;
467
0
      const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
468
0
      PSNR_STATS psnr;
469
0
#if CONFIG_AV1_HIGHBITDEPTH
470
0
      const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
471
0
      const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
472
0
      aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
473
#else
474
      aom_calc_psnr(orig, recon, &psnr);
475
#endif
476
0
      sse = psnr.sse[0];
477
0
    }
478
479
0
    count = fwrite(&sse, sizeof(sse), 1, cpi->second_pass_log_stream);
480
0
    if (count < 1) {
481
0
      aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
482
0
                         "Could not write to second pass log file!");
483
0
    }
484
485
    // write bpm_factor
486
0
    double factor = cpi->ppi->twopass.bpm_factor;
487
0
    count = fwrite(&factor, sizeof(factor), 1, cpi->second_pass_log_stream);
488
0
    if (count < 1) {
489
0
      aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
490
0
                         "Could not write to second pass log file!");
491
0
    }
492
0
  }
493
1.26k
}
494
0
void av1_open_second_pass_log(AV1_COMP *cpi, int is_read) {
495
0
  const AV1EncoderConfig *const oxcf = &cpi->oxcf;
496
0
  if (oxcf->second_pass_log == NULL) {
497
0
    aom_internal_error(cpi->common.error, AOM_CODEC_INVALID_PARAM,
498
0
                       "No second pass log file specified for the third pass!");
499
0
  }
500
  // Read the GOP length from a file.
501
0
  if (!cpi->second_pass_log_stream) {
502
0
    if (is_read) {
503
0
      cpi->second_pass_log_stream = fopen(cpi->oxcf.second_pass_log, "rb");
504
0
    } else {
505
0
      cpi->second_pass_log_stream = fopen(cpi->oxcf.second_pass_log, "wb");
506
0
    }
507
0
    if (!cpi->second_pass_log_stream) {
508
0
      aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
509
0
                         "Could not open second pass log file!");
510
0
    }
511
0
  }
512
0
}
513
514
1.26k
void av1_close_second_pass_log(AV1_COMP *cpi) {
515
1.26k
  if (cpi->second_pass_log_stream) {
516
0
    int ret = fclose(cpi->second_pass_log_stream);
517
0
    if (ret != 0) {
518
0
      aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
519
0
                         "Could not close second pass log file!");
520
0
    }
521
0
    cpi->second_pass_log_stream = 0;
522
0
  }
523
1.26k
}
524
525
void av1_read_second_pass_gop_info(FILE *second_pass_log_stream,
526
                                   THIRD_PASS_GOP_INFO *gop_info,
527
0
                                   struct aom_internal_error_info *error) {
528
0
  size_t count = fread(gop_info, sizeof(*gop_info), 1, second_pass_log_stream);
529
0
  if (count < 1) {
530
0
    aom_internal_error(error, AOM_CODEC_ERROR,
531
0
                       "Could not read from second pass log file!");
532
0
  }
533
0
}
534
535
void av1_read_second_pass_per_frame_info(
536
    FILE *second_pass_log_stream, THIRD_PASS_FRAME_INFO *frame_info_arr,
537
0
    int frame_info_count, struct aom_internal_error_info *error) {
538
0
  for (int i = 0; i < frame_info_count; i++) {
539
    // read target bits
540
0
    int bits = 0;
541
0
    size_t count = fread(&bits, sizeof(bits), 1, second_pass_log_stream);
542
0
    if (count < 1) {
543
0
      aom_internal_error(error, AOM_CODEC_ERROR,
544
0
                         "Could not read from second pass log file!");
545
0
    }
546
0
    frame_info_arr[i].bits_allocated = bits;
547
548
    // read distortion
549
0
    uint64_t sse;
550
0
    count = fread(&sse, sizeof(sse), 1, second_pass_log_stream);
551
0
    if (count < 1) {
552
0
      aom_internal_error(error, AOM_CODEC_ERROR,
553
0
                         "Could not read from second pass log file!");
554
0
    }
555
0
    frame_info_arr[i].sse = sse;
556
557
    // read bpm factor
558
0
    double factor;
559
0
    count = fread(&factor, sizeof(factor), 1, second_pass_log_stream);
560
0
    if (count < 1) {
561
0
      aom_internal_error(error, AOM_CODEC_ERROR,
562
0
                         "Could not read from second pass log file!");
563
0
    }
564
0
    frame_info_arr[i].bpm_factor = factor;
565
0
  }
566
0
}
567
568
0
int av1_check_use_arf(THIRD_PASS_DEC_CTX *ctx) {
569
0
  if (ctx == NULL) return -1;
570
0
  int use_arf = 0;
571
0
  for (int i = 0; i < ctx->gop_info.gf_length; i++) {
572
0
    if (ctx->frame_info[i].order_hint != 0 &&
573
0
        ctx->frame_info[i].is_show_frame == 0) {
574
0
      use_arf = 1;
575
0
    }
576
0
  }
577
0
  if (use_arf != ctx->gop_info.use_arf) {
578
0
    aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
579
0
                       "Mismatch in third pass GOP length!");
580
0
  }
581
0
  return use_arf;
582
0
}
583
584
void av1_get_third_pass_ratio(THIRD_PASS_DEC_CTX *ctx, int fidx, int fheight,
585
0
                              int fwidth, double *ratio_h, double *ratio_w) {
586
0
  assert(ctx);
587
0
  assert(fidx < ctx->frame_info_count);
588
0
  const int fheight_second_pass = ctx->frame_info[fidx].height;
589
0
  const int fwidth_second_pass = ctx->frame_info[fidx].width;
590
0
  assert(fheight_second_pass <= fheight && fwidth_second_pass <= fwidth);
591
592
0
  *ratio_h = (double)fheight / fheight_second_pass;
593
0
  *ratio_w = (double)fwidth / fwidth_second_pass;
594
0
}
595
596
THIRD_PASS_MI_INFO *av1_get_third_pass_mi(THIRD_PASS_DEC_CTX *ctx, int fidx,
597
                                          int mi_row, int mi_col,
598
0
                                          double ratio_h, double ratio_w) {
599
0
  assert(ctx);
600
0
  assert(fidx < ctx->frame_info_count);
601
602
0
  const int mi_rows_second_pass = ctx->frame_info[fidx].mi_rows;
603
0
  const int mi_cols_second_pass = ctx->frame_info[fidx].mi_cols;
604
605
0
  const int mi_row_second_pass =
606
0
      clamp((int)round(mi_row / ratio_h), 0, mi_rows_second_pass - 1);
607
0
  const int mi_col_second_pass =
608
0
      clamp((int)round(mi_col / ratio_w), 0, mi_cols_second_pass - 1);
609
610
0
  const int mi_stride_second_pass = ctx->frame_info[fidx].mi_stride;
611
0
  THIRD_PASS_MI_INFO *this_mi = ctx->frame_info[fidx].mi_info +
612
0
                                mi_row_second_pass * mi_stride_second_pass +
613
0
                                mi_col_second_pass;
614
0
  return this_mi;
615
0
}
616
617
void av1_third_pass_get_adjusted_mi(THIRD_PASS_MI_INFO *third_pass_mi,
618
                                    double ratio_h, double ratio_w, int *mi_row,
619
0
                                    int *mi_col) {
620
0
  *mi_row = (int)round(third_pass_mi->mi_row_start * ratio_h);
621
0
  *mi_col = (int)round(third_pass_mi->mi_col_start * ratio_w);
622
0
}
623
624
int_mv av1_get_third_pass_adjusted_mv(THIRD_PASS_MI_INFO *this_mi,
625
                                      double ratio_h, double ratio_w,
626
0
                                      MV_REFERENCE_FRAME frame) {
627
0
  assert(this_mi != NULL);
628
0
  int_mv cur_mv;
629
0
  cur_mv.as_int = INVALID_MV;
630
631
0
  if (frame < LAST_FRAME || frame > ALTREF_FRAME) return cur_mv;
632
633
0
  for (int r = 0; r < 2; r++) {
634
0
    if (this_mi->ref_frame[r] == frame) {
635
0
      cur_mv.as_mv.row = (int16_t)round(this_mi->mv[r].as_mv.row * ratio_h);
636
0
      cur_mv.as_mv.col = (int16_t)round(this_mi->mv[r].as_mv.col * ratio_w);
637
0
    }
638
0
  }
639
640
0
  return cur_mv;
641
0
}
642
643
BLOCK_SIZE av1_get_third_pass_adjusted_blk_size(THIRD_PASS_MI_INFO *this_mi,
644
                                                double ratio_h,
645
0
                                                double ratio_w) {
646
0
  assert(this_mi != NULL);
647
0
  BLOCK_SIZE bsize = BLOCK_INVALID;
648
649
0
  const BLOCK_SIZE bsize_second_pass = this_mi->bsize;
650
0
  assert(bsize_second_pass != BLOCK_INVALID);
651
652
0
  const int w_second_pass = block_size_wide[bsize_second_pass];
653
0
  const int h_second_pass = block_size_high[bsize_second_pass];
654
655
0
  int part_type;
656
657
0
  if (w_second_pass == h_second_pass) {
658
0
    part_type = PARTITION_NONE;
659
0
  } else if (w_second_pass / h_second_pass == 2) {
660
0
    part_type = PARTITION_HORZ;
661
0
  } else if (w_second_pass / h_second_pass == 4) {
662
0
    part_type = PARTITION_HORZ_4;
663
0
  } else if (h_second_pass / w_second_pass == 2) {
664
0
    part_type = PARTITION_VERT;
665
0
  } else if (h_second_pass / w_second_pass == 4) {
666
0
    part_type = PARTITION_VERT_4;
667
0
  } else {
668
0
    part_type = PARTITION_INVALID;
669
0
  }
670
0
  assert(part_type != PARTITION_INVALID);
671
672
0
  const int w = (int)(round(w_second_pass * ratio_w));
673
0
  const int h = (int)(round(h_second_pass * ratio_h));
674
675
0
  for (int i = 0; i < SQR_BLOCK_SIZES; i++) {
676
0
    const BLOCK_SIZE this_bsize = subsize_lookup[part_type][i];
677
0
    if (this_bsize == BLOCK_INVALID) continue;
678
679
0
    const int this_w = block_size_wide[this_bsize];
680
0
    const int this_h = block_size_high[this_bsize];
681
682
0
    if (this_w >= w && this_h >= h) {
683
      // find the smallest block size that contains the mapped block
684
0
      bsize = this_bsize;
685
0
      break;
686
0
    }
687
0
  }
688
0
  if (bsize == BLOCK_INVALID) {
689
    // could not find a proper one, just use the largest then.
690
0
    bsize = BLOCK_128X128;
691
0
  }
692
693
0
  return bsize;
694
0
}
695
696
PARTITION_TYPE av1_third_pass_get_sb_part_type(THIRD_PASS_DEC_CTX *ctx,
697
0
                                               THIRD_PASS_MI_INFO *this_mi) {
698
0
  int mi_stride = ctx->frame_info[0].mi_stride;
699
700
0
  int mi_row = this_mi->mi_row_start;
701
0
  int mi_col = this_mi->mi_col_start;
702
703
0
  THIRD_PASS_MI_INFO *corner_mi =
704
0
      &ctx->frame_info[0].mi_info[mi_row * mi_stride + mi_col];
705
706
0
  return corner_mi->partition;
707
0
}