2016-03-25 21:57:20 +03:00
|
|
|
/*
|
2016-09-02 00:32:49 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2016-03-25 21:57:20 +03:00
|
|
|
*
|
2016-09-02 00:32:49 +03:00
|
|
|
* 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.
|
2016-03-25 21:57:20 +03:00
|
|
|
*/
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
// AV1 Set Reference Frame
|
2016-03-25 21:57:20 +03:00
|
|
|
// ============================
|
|
|
|
//
|
2016-08-31 00:01:10 +03:00
|
|
|
// This is an example demonstrating how to overwrite the AV1 encoder's
|
2016-03-25 21:57:20 +03:00
|
|
|
// 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
|
2016-08-31 00:01:10 +03:00
|
|
|
// examples/aom_cx_set_ref av1 352 288 in.yuv out.ivf 4 30
|
2016-03-25 21:57:20 +03:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#include "aom/aomcx.h"
|
|
|
|
#include "aom/aom_decoder.h"
|
|
|
|
#include "aom/aom_encoder.h"
|
2017-04-21 03:56:27 +03:00
|
|
|
#include "examples/encoder_util.h"
|
2016-03-25 21:57:20 +03:00
|
|
|
#include "./tools_common.h"
|
|
|
|
#include "./video_writer.h"
|
|
|
|
|
|
|
|
static const char *exec_name;
|
|
|
|
|
|
|
|
void usage_exit() {
|
2016-08-09 04:51:16 +03:00
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: %s <codec> <width> <height> <infile> <outfile> "
|
2016-03-25 21:57:20 +03:00
|
|
|
"<frame> <limit(optional)>\n",
|
|
|
|
exec_name);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
static void testing_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
|
2016-07-14 22:33:48 +03:00
|
|
|
unsigned int frame_out, int *mismatch_seen) {
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_image_t enc_img, dec_img;
|
|
|
|
struct av1_ref_frame ref_enc, ref_dec;
|
2016-03-25 21:57:20 +03:00
|
|
|
|
2016-08-09 04:51:16 +03:00
|
|
|
if (*mismatch_seen) return;
|
2016-03-25 21:57:20 +03:00
|
|
|
|
|
|
|
ref_enc.idx = 0;
|
|
|
|
ref_dec.idx = 0;
|
2016-08-31 00:01:10 +03:00
|
|
|
if (aom_codec_control(encoder, AV1_GET_REFERENCE, &ref_enc))
|
2016-08-09 04:51:16 +03:00
|
|
|
die_codec(encoder, "Failed to get encoder reference frame");
|
2016-03-25 21:57:20 +03:00
|
|
|
enc_img = ref_enc.img;
|
2016-08-31 00:01:10 +03:00
|
|
|
if (aom_codec_control(decoder, AV1_GET_REFERENCE, &ref_dec))
|
2016-03-25 21:57:20 +03:00
|
|
|
die_codec(decoder, "Failed to get decoder reference frame");
|
|
|
|
dec_img = ref_dec.img;
|
|
|
|
|
2017-04-21 03:56:27 +03:00
|
|
|
if (!aom_compare_img(&enc_img, &dec_img)) {
|
2016-03-25 21:57:20 +03:00
|
|
|
int y[4], u[4], v[4];
|
|
|
|
|
|
|
|
*mismatch_seen = 1;
|
|
|
|
|
2017-04-21 03:56:27 +03:00
|
|
|
aom_find_mismatch(&enc_img, &dec_img, y, u, v);
|
2016-08-09 04:51:16 +03:00
|
|
|
printf(
|
|
|
|
"Encode/decode mismatch on frame %d at"
|
|
|
|
" Y[%d, %d] {%d/%d},"
|
|
|
|
" U[%d, %d] {%d/%d},"
|
|
|
|
" V[%d, %d] {%d/%d}",
|
|
|
|
frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
|
|
|
|
v[2], v[3]);
|
2016-03-25 21:57:20 +03:00
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_img_free(&enc_img);
|
|
|
|
aom_img_free(&dec_img);
|
2016-03-25 21:57:20 +03:00
|
|
|
}
|
|
|
|
|
2016-07-14 22:33:48 +03:00
|
|
|
static int encode_frame(aom_codec_ctx_t *ecodec, aom_image_t *img,
|
|
|
|
unsigned int frame_in, AvxVideoWriter *writer,
|
|
|
|
int test_decode, aom_codec_ctx_t *dcodec,
|
|
|
|
unsigned int *frame_out, int *mismatch_seen) {
|
2016-03-25 21:57:20 +03:00
|
|
|
int got_pkts = 0;
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_codec_iter_t iter = NULL;
|
|
|
|
const aom_codec_cx_pkt_t *pkt = NULL;
|
2016-03-25 21:57:20 +03:00
|
|
|
int got_data;
|
2016-08-31 00:01:10 +03:00
|
|
|
const aom_codec_err_t res =
|
|
|
|
aom_codec_encode(ecodec, img, frame_in, 1, 0, AOM_DL_GOOD_QUALITY);
|
|
|
|
if (res != AOM_CODEC_OK) die_codec(ecodec, "Failed to encode frame");
|
2016-03-25 21:57:20 +03:00
|
|
|
|
|
|
|
got_data = 0;
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
while ((pkt = aom_codec_get_cx_data(ecodec, &iter)) != NULL) {
|
2016-03-25 21:57:20 +03:00
|
|
|
got_pkts = 1;
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
|
|
|
|
const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
|
2016-03-25 21:57:20 +03:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
|
2016-08-09 04:51:16 +03:00
|
|
|
*frame_out += 1;
|
|
|
|
}
|
2016-03-25 21:57:20 +03:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
|
2016-03-25 21:57:20 +03:00
|
|
|
pkt->data.frame.sz,
|
|
|
|
pkt->data.frame.pts)) {
|
|
|
|
die_codec(ecodec, "Failed to write compressed frame");
|
|
|
|
}
|
|
|
|
printf(keyframe ? "K" : ".");
|
|
|
|
fflush(stdout);
|
|
|
|
got_data = 1;
|
|
|
|
|
|
|
|
// Decode 1 frame.
|
|
|
|
if (test_decode) {
|
2016-08-31 00:01:10 +03:00
|
|
|
if (aom_codec_decode(dcodec, pkt->data.frame.buf,
|
2016-03-25 21:57:20 +03:00
|
|
|
(unsigned int)pkt->data.frame.sz, NULL, 0))
|
|
|
|
die_codec(dcodec, "Failed to decode frame.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mismatch checking
|
|
|
|
if (got_data && test_decode) {
|
2016-07-14 22:33:48 +03:00
|
|
|
testing_decode(ecodec, dcodec, *frame_out, mismatch_seen);
|
2016-03-25 21:57:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return got_pkts;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
FILE *infile = NULL;
|
|
|
|
// Encoder
|
2016-07-14 22:33:48 +03:00
|
|
|
aom_codec_ctx_t ecodec;
|
|
|
|
aom_codec_enc_cfg_t cfg;
|
2016-03-25 21:57:20 +03:00
|
|
|
unsigned int frame_in = 0;
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_image_t raw;
|
|
|
|
aom_codec_err_t res;
|
2016-07-14 22:33:48 +03:00
|
|
|
AvxVideoInfo info;
|
2016-08-31 00:01:10 +03:00
|
|
|
AvxVideoWriter *writer = NULL;
|
|
|
|
const AvxInterface *encoder = NULL;
|
2016-03-25 21:57:20 +03:00
|
|
|
|
|
|
|
// Test encoder/decoder mismatch.
|
|
|
|
int test_decode = 1;
|
|
|
|
// Decoder
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_codec_ctx_t dcodec;
|
2016-03-25 21:57:20 +03:00
|
|
|
unsigned int frame_out = 0;
|
|
|
|
|
|
|
|
// The frame number to set reference frame on
|
2016-07-23 03:14:22 +03:00
|
|
|
unsigned int update_frame_num = 0;
|
2016-03-25 21:57:20 +03:00
|
|
|
int mismatch_seen = 0;
|
|
|
|
|
|
|
|
const int fps = 30;
|
|
|
|
const int bitrate = 500;
|
|
|
|
|
|
|
|
const char *codec_arg = NULL;
|
|
|
|
const char *width_arg = NULL;
|
|
|
|
const char *height_arg = NULL;
|
|
|
|
const char *infile_arg = NULL;
|
|
|
|
const char *outfile_arg = NULL;
|
2016-07-09 02:09:36 +03:00
|
|
|
const char *update_frame_num_arg = NULL;
|
2016-07-23 03:14:22 +03:00
|
|
|
unsigned int limit = 0;
|
2016-03-25 21:57:20 +03:00
|
|
|
exec_name = argv[0];
|
|
|
|
|
2016-07-14 22:33:48 +03:00
|
|
|
// Clear explicitly, as simply assigning "{ 0 }" generates
|
|
|
|
// "missing-field-initializers" warning in some compilers.
|
|
|
|
memset(&ecodec, 0, sizeof(ecodec));
|
|
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
|
2016-08-09 04:51:16 +03:00
|
|
|
if (argc < 7) die("Invalid number of arguments");
|
2016-03-25 21:57:20 +03:00
|
|
|
|
|
|
|
codec_arg = argv[1];
|
|
|
|
width_arg = argv[2];
|
|
|
|
height_arg = argv[3];
|
|
|
|
infile_arg = argv[4];
|
|
|
|
outfile_arg = argv[5];
|
2016-07-09 02:09:36 +03:00
|
|
|
update_frame_num_arg = argv[6];
|
2016-03-25 21:57:20 +03:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
encoder = get_aom_encoder_by_name(codec_arg);
|
2016-08-09 04:51:16 +03:00
|
|
|
if (!encoder) die("Unsupported codec.");
|
2016-03-25 21:57:20 +03:00
|
|
|
|
2016-07-09 02:09:36 +03:00
|
|
|
update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
|
2016-08-31 00:01:10 +03:00
|
|
|
// In AV1, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
|
|
|
|
// allocated while calling aom_codec_encode(), thus, setting reference for
|
2016-03-25 21:57:20 +03:00
|
|
|
// 1st frame isn't supported.
|
2016-07-09 02:09:36 +03:00
|
|
|
if (update_frame_num <= 1) {
|
|
|
|
die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
|
|
|
|
}
|
2016-03-25 21:57:20 +03:00
|
|
|
|
|
|
|
if (argc > 7) {
|
2016-07-09 02:09:36 +03:00
|
|
|
limit = (unsigned int)strtoul(argv[7], NULL, 0);
|
2016-03-25 21:57:20 +03:00
|
|
|
if (update_frame_num > limit)
|
|
|
|
die("Update frame number couldn't larger than limit\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
info.codec_fourcc = encoder->fourcc;
|
2017-03-23 10:25:55 +03:00
|
|
|
info.frame_width = (int)strtol(width_arg, NULL, 0);
|
|
|
|
info.frame_height = (int)strtol(height_arg, NULL, 0);
|
2016-03-25 21:57:20 +03:00
|
|
|
info.time_base.numerator = 1;
|
|
|
|
info.time_base.denominator = fps;
|
|
|
|
|
2016-08-09 04:51:16 +03:00
|
|
|
if (info.frame_width <= 0 || info.frame_height <= 0 ||
|
|
|
|
(info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
|
2016-03-25 21:57:20 +03:00
|
|
|
die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
|
2016-08-09 04:51:16 +03:00
|
|
|
info.frame_height, 1)) {
|
2016-03-25 21:57:20 +03:00
|
|
|
die("Failed to allocate image.");
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
|
2016-03-25 21:57:20 +03:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
|
2016-08-09 04:51:16 +03:00
|
|
|
if (res) die_codec(&ecodec, "Failed to get default codec config.");
|
2016-03-25 21:57:20 +03:00
|
|
|
|
|
|
|
cfg.g_w = info.frame_width;
|
|
|
|
cfg.g_h = info.frame_height;
|
|
|
|
cfg.g_timebase.num = info.time_base.numerator;
|
|
|
|
cfg.g_timebase.den = info.time_base.denominator;
|
|
|
|
cfg.rc_target_bitrate = bitrate;
|
|
|
|
cfg.g_lag_in_frames = 3;
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
|
2016-08-09 04:51:16 +03:00
|
|
|
if (!writer) die("Failed to open %s for writing.", outfile_arg);
|
2016-03-25 21:57:20 +03:00
|
|
|
|
|
|
|
if (!(infile = fopen(infile_arg, "rb")))
|
|
|
|
die("Failed to open %s for reading.", infile_arg);
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
if (aom_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, 0))
|
2016-03-25 21:57:20 +03:00
|
|
|
die_codec(&ecodec, "Failed to initialize encoder");
|
|
|
|
|
|
|
|
// Disable alt_ref.
|
2016-08-31 00:01:10 +03:00
|
|
|
if (aom_codec_control(&ecodec, AOME_SET_ENABLEAUTOALTREF, 0))
|
2016-03-25 21:57:20 +03:00
|
|
|
die_codec(&ecodec, "Failed to set enable auto alt ref");
|
|
|
|
|
|
|
|
if (test_decode) {
|
2016-08-31 00:01:10 +03:00
|
|
|
const AvxInterface *decoder = get_aom_decoder_by_name(codec_arg);
|
|
|
|
if (aom_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0))
|
2016-08-09 04:51:16 +03:00
|
|
|
die_codec(&dcodec, "Failed to initialize decoder.");
|
2016-03-25 21:57:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode frames.
|
2016-08-31 00:01:10 +03:00
|
|
|
while (aom_img_read(&raw, infile)) {
|
2016-08-09 04:51:16 +03:00
|
|
|
if (limit && frame_in >= limit) break;
|
2016-03-25 21:57:20 +03:00
|
|
|
if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_ref_frame_t ref;
|
|
|
|
ref.frame_type = AOM_LAST_FRAME;
|
2016-03-25 21:57:20 +03:00
|
|
|
ref.img = raw;
|
|
|
|
// Set reference frame in encoder.
|
2016-08-31 00:01:10 +03:00
|
|
|
if (aom_codec_control(&ecodec, AOM_SET_REFERENCE, &ref))
|
2016-03-25 21:57:20 +03:00
|
|
|
die_codec(&ecodec, "Failed to set reference frame");
|
|
|
|
printf(" <SET_REF>");
|
|
|
|
|
|
|
|
// If set_reference in decoder is commented out, the enc/dec mismatch
|
|
|
|
// would be seen.
|
|
|
|
if (test_decode) {
|
2016-08-31 00:01:10 +03:00
|
|
|
if (aom_codec_control(&dcodec, AOM_SET_REFERENCE, &ref))
|
2016-03-25 21:57:20 +03:00
|
|
|
die_codec(&dcodec, "Failed to set reference frame");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 22:33:48 +03:00
|
|
|
encode_frame(&ecodec, &raw, frame_in, writer, test_decode, &dcodec,
|
2016-08-09 04:51:16 +03:00
|
|
|
&frame_out, &mismatch_seen);
|
2016-03-25 21:57:20 +03:00
|
|
|
frame_in++;
|
2016-08-09 04:51:16 +03:00
|
|
|
if (mismatch_seen) break;
|
2016-03-25 21:57:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush encoder.
|
|
|
|
if (!mismatch_seen)
|
2016-07-14 22:33:48 +03:00
|
|
|
while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
|
|
|
|
&frame_out, &mismatch_seen)) {
|
2016-08-09 04:51:16 +03:00
|
|
|
}
|
2016-03-25 21:57:20 +03:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
fclose(infile);
|
|
|
|
printf("Processed %d frames.\n", frame_out);
|
|
|
|
|
|
|
|
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)
|
2016-08-31 00:01:10 +03:00
|
|
|
if (aom_codec_destroy(&dcodec))
|
2016-03-25 21:57:20 +03:00
|
|
|
die_codec(&dcodec, "Failed to destroy decoder");
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_img_free(&raw);
|
|
|
|
if (aom_codec_destroy(&ecodec))
|
2016-03-25 21:57:20 +03:00
|
|
|
die_codec(&ecodec, "Failed to destroy encoder.");
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_video_writer_close(writer);
|
2016-03-25 21:57:20 +03:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|