/* * Copyright (c) 2016, 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.
*/
// AV1 Set Reference Frame // ============================ // // This is an example demonstrating how to overwrite the AV1 encoder's // internal reference frame. In the sample we set the last frame to the // current frame. This technique could be used to bounce between two cameras. // // The decoder would also have to set the reference frame to the same value // on the same frame, or the video will become corrupt. The 'test_decode' // variable is set to 1 in this example that tests if the encoder and decoder // results are matching. // // Usage // ----- // This example encodes a raw video. And the last argument passed in specifies // the frame number to update the reference frame on. For example, run // examples/aom_cx_set_ref av1 352 288 in.yuv out.ivf 4 30 // The parameter is parsed as follows: // // // Extra Variables // --------------- // This example maintains the frame number passed on the command line // in the `update_frame_num` variable. // // // Configuration // ------------- // // The reference frame is updated on the frame specified on the command // line. // // Observing The Effects // --------------------- // The encoder and decoder results should be matching when the same reference // frame setting operation is done in both encoder and decoder. Otherwise, // the encoder/decoder mismatch would be seen.
/* Get the internal reference frame */ if (aom_codec_control(encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img))
die_codec(encoder, "Failed to get encoder reference frame"); if (aom_codec_control(decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img))
die_codec(decoder, "Failed to get decoder reference frame");
if (!aom_compare_img(&enc_img, &dec_img)) { int y[4], u[4], v[4]; if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
} else {
aom_find_mismatch(&enc_img, &dec_img, y, u, v);
}
// Decode 1 frame. if (test_decode) { if (aom_codec_decode(dcodec, pkt->data.frame.buf,
(unsignedint)pkt->data.frame.sz, NULL))
die_codec(dcodec, "Failed to decode frame.");
// Copy out first decoded frame, and use it as reference later. if (*frame_out == 1 && ext_ref != NULL) if (aom_codec_control(dcodec, AV1_COPY_NEW_FRAME_IMAGE, ext_ref))
die_codec(dcodec, "Failed to get decoder new frame");
}
}
}
// In this test, the bit depth of input video is 8-bit, and the input format // is AOM_IMG_FMT_I420. if (!aom_img_alloc(&raw, raw_fmt, info.frame_width, info.frame_height, 32)) {
die("Failed to allocate image.");
}
if (FORCE_HIGHBITDEPTH_DECODING) ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH; // Allocate memory with the border so that it can be used as a reference. if (!aom_img_alloc_with_border(&ext_ref, ref_fmt, info.frame_width,
info.frame_height, 32, 8,
AOM_DEC_BORDER_IN_PIXELS)) {
die("Failed to allocate image.");
}
#if CONFIG_REALTIME_ONLY
res = aom_codec_enc_config_default(encoder, &cfg, 1); #else
res = aom_codec_enc_config_default(encoder, &cfg, 0); #endif if (res) die_codec(&ecodec, "Failed to get default codec config.");
writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info); if (!writer) die("Failed to open %s for writing.", outfile_arg);
if (!(infile = fopen(infile_arg, "rb")))
die("Failed to open %s for reading.", infile_arg);
if (aom_codec_enc_init(&ecodec, encoder, &cfg, flags))
die("Failed to initialize encoder");
// Disable alt_ref. if (aom_codec_control(&ecodec, AOME_SET_ENABLEAUTOALTREF, 0))
die_codec(&ecodec, "Failed to set enable auto alt ref");
if (test_decode) {
aom_codec_iface_t *decoder = get_aom_decoder_by_short_name(codec_arg); if (aom_codec_dec_init(&dcodec, decoder, NULL, 0))
die("Failed to initialize decoder.");
}
// Encode frames. while (aom_img_read(&raw, infile)) { if (limit && frame_in >= limit) break;
aom_image_t *frame_to_encode;
if (FORCE_HIGHBITDEPTH_DECODING) { // Need to allocate larger buffer to use hbd internal. int input_shift = 0; if (!allocated_raw_shift) {
aom_img_alloc(&raw_shift, raw_fmt | AOM_IMG_FMT_HIGHBITDEPTH,
info.frame_width, info.frame_height, 32);
allocated_raw_shift = 1;
}
aom_img_upshift(&raw_shift, &raw, input_shift);
frame_to_encode = &raw_shift;
} else {
frame_to_encode = &raw;
}
if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
av1_ref_frame_t ref;
ref.idx = 0;
ref.use_external_ref = 0;
ref.img = ext_ref; // Set reference frame in encoder. if (aom_codec_control(&ecodec, AV1_SET_REFERENCE, &ref))
die_codec(&ecodec, "Failed to set encoder reference frame");
printf(" ");
#if CONFIG_REALTIME_ONLY // Set cpu speed in encoder. if (aom_codec_control(&ecodec, AOME_SET_CPUUSED, 7))
die_codec(&ecodec, "Failed to set cpu speed"); #endif
// If set_reference in decoder is commented out, the enc/dec mismatch // would be seen. if (test_decode) {
ref.use_external_ref = 1; if (aom_codec_control(&dcodec, AV1_SET_REFERENCE, &ref))
die_codec(&dcodec, "Failed to set decoder reference frame");
}
}
if (test_decode) { if (!mismatch_seen)
printf("Encoder/decoder results are matching.\n"); else
printf("Encoder/decoder results are NOT matching.\n");
}
if (test_decode) if (aom_codec_destroy(&dcodec))
die_codec(&dcodec, "Failed to destroy decoder");
if (allocated_raw_shift) aom_img_free(&raw_shift);
aom_img_free(&ext_ref);
aom_img_free(&raw); if (aom_codec_destroy(&ecodec))
die_codec(&ecodec, "Failed to destroy encoder.");
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.