/* * Copyright (c) 2021, Alliance for Open Media. All rights reserved. * * This source code is subject to the terms of the BSD 2 Clause License and * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License * was not distributed with this source code in the LICENSE file, you can * obtain it at www.aomedia.org/license/software. If the Alliance for Open * Media Patent License 1.0 was not distributed with this source code in the * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/ #include"av1/encoder/thirdpass.h"
// This function gets the information needed from the recently decoded frame, // via various decoder APIs, and saves the info into ctx->frame_info. // Return 0: success // 1: cannot read because this is end of file // -1: failure to read the frame staticint get_frame_info(THIRD_PASS_DEC_CTX *ctx) { int ret = read_frame(ctx); if (ret != 0) return ret; int cur = ctx->frame_info_count;
if (cur >= MAX_THIRD_PASS_BUF) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Third pass frame info ran out of available slots.");
}
aom_codec_frame_flags_t frame_type_flags = 0; if (aom_codec_control(&ctx->decoder, AOMD_GET_FRAME_FLAGS,
&frame_type_flags) != AOM_CODEC_OK) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Failed to read frame flags.");
} if (frame_type_flags & AOM_FRAME_IS_KEY) {
ctx->frame_info[cur].frame_type = KEY_FRAME;
} elseif (frame_type_flags & AOM_FRAME_IS_INTRAONLY) {
ctx->frame_info[cur].frame_type = INTRA_ONLY_FRAME;
} elseif (frame_type_flags & AOM_FRAME_IS_SWITCH) {
ctx->frame_info[cur].frame_type = S_FRAME;
} else {
ctx->frame_info[cur].frame_type = INTER_FRAME;
}
// Get frame width and height int frame_size[2]; if (aom_codec_control(&ctx->decoder, AV1D_GET_FRAME_SIZE, frame_size) !=
AOM_CODEC_OK) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Failed to read frame size.");
}
// Check if we need to re-alloc the mi fields. constint mi_cols = (frame_size[0] + 3) >> 2; constint mi_rows = (frame_size[1] + 3) >> 2;
ctx->frame_info[cur].mi_stride = mi_cols;
ctx->frame_info[cur].mi_rows = mi_rows;
ctx->frame_info[cur].mi_cols = mi_cols;
// Get frame base q idx if (aom_codec_control(&ctx->decoder, AOMD_GET_BASE_Q_IDX,
&ctx->frame_info[cur].base_q_idx) != AOM_CODEC_OK) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Failed to read base q index.");
}
// Get show existing frame flag if (aom_codec_control(&ctx->decoder, AOMD_GET_SHOW_EXISTING_FRAME_FLAG,
&ctx->frame_info[cur].is_show_existing_frame) !=
AOM_CODEC_OK) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Failed to read show existing frame flag.");
}
// Get show frame flag if (aom_codec_control(&ctx->decoder, AOMD_GET_SHOW_FRAME_FLAG,
&ctx->frame_info[cur].is_show_frame) != AOM_CODEC_OK) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Failed to read show frame flag.");
}
// Get order hint if (aom_codec_control(&ctx->decoder, AOMD_GET_ORDER_HINT,
&ctx->frame_info[cur].order_hint) != AOM_CODEC_OK) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Failed to read order hint.");
}
// Clear MI info for (int mi_row = 0; mi_row < mi_rows; mi_row++) { for (int mi_col = 0; mi_col < mi_cols; mi_col++) {
ctx->frame_info[cur].mi_info[mi_row * mi_cols + mi_col].bsize =
BLOCK_INVALID;
}
}
// Get relevant information regarding each 4x4 MI
MB_MODE_INFO cur_mi_info;
THIRD_PASS_MI_INFO *const this_mi = ctx->frame_info[cur].mi_info; for (int mi_row = 0; mi_row < mi_rows; mi_row++) { for (int mi_col = 0; mi_col < mi_cols; mi_col++) { constint offset = mi_row * mi_cols + mi_col; if (this_mi[offset].bsize != BLOCK_INVALID) { continue;
} // Get info of this MI if (aom_codec_control(&ctx->decoder, AV1D_GET_MI_INFO, mi_row, mi_col,
&cur_mi_info) != AOM_CODEC_OK) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Failed to read mi info.");
} constint blk_mi_rows = mi_size_high[cur_mi_info.bsize]; constint blk_mi_cols = mi_size_wide[cur_mi_info.bsize];
for (int h = 0; h < blk_mi_rows; h++) { for (int w = 0; w < blk_mi_cols; w++) { if (h + mi_row >= mi_rows || w + mi_col >= mi_cols) { continue;
} constint this_offset = offset + h * mi_cols + w;
this_mi[this_offset].bsize = cur_mi_info.bsize;
this_mi[this_offset].partition = cur_mi_info.partition;
this_mi[this_offset].mi_row_start = mi_row;
this_mi[this_offset].mi_col_start = mi_col;
this_mi[this_offset].mv[0] = cur_mi_info.mv[0];
this_mi[this_offset].mv[1] = cur_mi_info.mv[1];
this_mi[this_offset].ref_frame[0] = cur_mi_info.ref_frame[0];
this_mi[this_offset].ref_frame[1] = cur_mi_info.ref_frame[1];
this_mi[this_offset].pred_mode = cur_mi_info.mode;
}
}
}
}
ctx->frame_info_count++;
return 0;
}
#define USE_SECOND_PASS_FILE 1
#if !USE_SECOND_PASS_FILE // Parse the frames in the gop and determine the last frame of the current GOP. // Decode more frames if necessary. The variable max_num is the maximum static // GOP length if we detect an IPPP structure, and it is expected that max_mum >= // MAX_GF_INTERVAL. staticvoid get_current_gop_end(THIRD_PASS_DEC_CTX *ctx, int max_num, int *last_idx) {
assert(max_num >= MAX_GF_INTERVAL);
*last_idx = 0; int cur_idx = 0; int arf_order_hint = -1; int num_show_frames = 0; while (num_show_frames < max_num) {
assert(cur_idx < MAX_THIRD_PASS_BUF); // Read in from bitstream if needed. if (cur_idx >= ctx->frame_info_count) { int ret = get_frame_info(ctx); if (ret == 1) { // At the end of the file, GOP ends in the prev frame. if (arf_order_hint >= 0) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Failed to derive GOP length.");
}
*last_idx = cur_idx - 1; return;
} if (ret < 0) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Failed to read frame for third pass.");
}
}
// TODO(bohanli): verify that fwd_kf works here. if (ctx->frame_info[cur_idx].frame_type == KEY_FRAME &&
ctx->frame_info[cur_idx].is_show_frame) { if (cur_idx != 0) { // If this is a key frame and is not the first kf in this kf group, we // have reached the next key frame. Stop here.
*last_idx = cur_idx - 1; return;
}
} elseif (!ctx->frame_info[cur_idx].is_show_frame &&
arf_order_hint == -1) { // If this is an arf (the first no show) if (num_show_frames <= 1) { // This is an arf and we should end the GOP with its overlay.
arf_order_hint = ctx->frame_info[cur_idx].order_hint;
} else { // There are multiple show frames before the this arf, so we treat the // frames previous to this arf as a GOP.
*last_idx = cur_idx - 1; return;
}
} elseif (arf_order_hint >= 0 && ctx->frame_info[cur_idx].order_hint ==
(unsignedint)arf_order_hint) { // If this is the overlay/show existing of the arf
assert(ctx->frame_info[cur_idx].is_show_frame);
*last_idx = cur_idx; return;
} else { // This frame is part of the GOP. if (ctx->frame_info[cur_idx].is_show_frame) num_show_frames++;
}
cur_idx++;
} // This is a long IPPP GOP and we will use a length of max_num here.
assert(arf_order_hint < 0);
*last_idx = max_num - 1; return;
} #endif
staticinlinevoid read_gop_frames(THIRD_PASS_DEC_CTX *ctx) { int cur_idx = 0; while (cur_idx < ctx->gop_info.num_frames) {
assert(cur_idx < MAX_THIRD_PASS_BUF); // Read in from bitstream if needed. if (cur_idx >= ctx->frame_info_count) { int ret = get_frame_info(ctx); if (ret != 0) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Failed to read frame for third pass.");
}
}
cur_idx++;
} return;
}
void av1_set_gop_third_pass(THIRD_PASS_DEC_CTX *ctx) { // Read in future frames in the current GOP.
read_gop_frames(ctx);
int gf_len = 0; // Check the GOP length against the value read from second_pass_file for (int i = 0; i < ctx->gop_info.num_frames; i++) { if (ctx->frame_info[i].is_show_frame) gf_len++;
}
if (gf_len != ctx->gop_info.gf_length) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Mismatch in third pass GOP length!");
}
}
void av1_pop_third_pass_info(THIRD_PASS_DEC_CTX *ctx) { if (ctx->frame_info_count == 0) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "No available frame info for third pass.");
}
ctx->frame_info_count--;
free_frame_info(&ctx->frame_info[0]); for (int i = 0; i < ctx->frame_info_count; i++) {
ctx->frame_info[i] = ctx->frame_info[i + 1];
}
ctx->frame_info[ctx->frame_info_count].mi_info = NULL;
}
count = fwrite(&sse, sizeof(sse), 1, cpi->second_pass_log_stream); if (count < 1) {
aom_internal_error(cpi->common.error, AOM_CODEC_ERROR, "Could not write to second pass log file!");
}
// write bpm_factor double factor = cpi->ppi->twopass.bpm_factor;
count = fwrite(&factor, sizeof(factor), 1, cpi->second_pass_log_stream); if (count < 1) {
aom_internal_error(cpi->common.error, AOM_CODEC_ERROR, "Could not write to second pass log file!");
}
}
} void av1_open_second_pass_log(AV1_COMP *cpi, int is_read) { const AV1EncoderConfig *const oxcf = &cpi->oxcf; if (oxcf->second_pass_log == NULL) {
aom_internal_error(cpi->common.error, AOM_CODEC_INVALID_PARAM, "No second pass log file specified for the third pass!");
} // Read the GOP length from a file. if (!cpi->second_pass_log_stream) { if (is_read) {
cpi->second_pass_log_stream = fopen(cpi->oxcf.second_pass_log, "rb");
} else {
cpi->second_pass_log_stream = fopen(cpi->oxcf.second_pass_log, "wb");
} if (!cpi->second_pass_log_stream) {
aom_internal_error(cpi->common.error, AOM_CODEC_ERROR, "Could not open second pass log file!");
}
}
}
void av1_close_second_pass_log(AV1_COMP *cpi) { if (cpi->second_pass_log_stream) { int ret = fclose(cpi->second_pass_log_stream); if (ret != 0) {
aom_internal_error(cpi->common.error, AOM_CODEC_ERROR, "Could not close second pass log file!");
}
cpi->second_pass_log_stream = 0;
}
}
void av1_read_second_pass_gop_info(FILE *second_pass_log_stream,
THIRD_PASS_GOP_INFO *gop_info, struct aom_internal_error_info *error) {
size_t count = fread(gop_info, sizeof(*gop_info), 1, second_pass_log_stream); if (count < 1) {
aom_internal_error(error, AOM_CODEC_ERROR, "Could not read from second pass log file!");
}
}
void av1_read_second_pass_per_frame_info(
FILE *second_pass_log_stream, THIRD_PASS_FRAME_INFO *frame_info_arr, int frame_info_count, struct aom_internal_error_info *error) { for (int i = 0; i < frame_info_count; i++) { // read target bits int bits = 0;
size_t count = fread(&bits, sizeof(bits), 1, second_pass_log_stream); if (count < 1) {
aom_internal_error(error, AOM_CODEC_ERROR, "Could not read from second pass log file!");
}
frame_info_arr[i].bits_allocated = bits;
// read distortion
uint64_t sse;
count = fread(&sse, sizeof(sse), 1, second_pass_log_stream); if (count < 1) {
aom_internal_error(error, AOM_CODEC_ERROR, "Could not read from second pass log file!");
}
frame_info_arr[i].sse = sse;
// read bpm factor double factor;
count = fread(&factor, sizeof(factor), 1, second_pass_log_stream); if (count < 1) {
aom_internal_error(error, AOM_CODEC_ERROR, "Could not read from second pass log file!");
}
frame_info_arr[i].bpm_factor = factor;
}
}
int av1_check_use_arf(THIRD_PASS_DEC_CTX *ctx) { if (ctx == NULL) return -1; int use_arf = 0; for (int i = 0; i < ctx->gop_info.gf_length; i++) { if (ctx->frame_info[i].order_hint != 0 &&
ctx->frame_info[i].is_show_frame == 0) {
use_arf = 1;
}
} if (use_arf != ctx->gop_info.use_arf) {
aom_internal_error(ctx->err_info, AOM_CODEC_ERROR, "Mismatch in third pass GOP length!");
} return use_arf;
}
if (this_w >= w && this_h >= h) { // find the smallest block size that contains the mapped block
bsize = this_bsize; break;
}
} if (bsize == BLOCK_INVALID) { // could not find a proper one, just use the largest then.
bsize = BLOCK_128X128;
}
return bsize;
}
PARTITION_TYPE av1_third_pass_get_sb_part_type(THIRD_PASS_DEC_CTX *ctx,
THIRD_PASS_MI_INFO *this_mi) { int mi_stride = ctx->frame_info[0].mi_stride;
int mi_row = this_mi->mi_row_start; int mi_col = this_mi->mi_col_start;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.