AOMedia Codec SDK
aom_cx_set_ref
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// AV1 Set Reference Frame
13// ============================
14//
15// This is an example demonstrating how to overwrite the AV1 encoder's
16// internal reference frame. In the sample we set the last frame to the
17// current frame. This technique could be used to bounce between two cameras.
18//
19// The decoder would also have to set the reference frame to the same value
20// on the same frame, or the video will become corrupt. The 'test_decode'
21// variable is set to 1 in this example that tests if the encoder and decoder
22// results are matching.
23//
24// Usage
25// -----
26// This example encodes a raw video. And the last argument passed in specifies
27// the frame number to update the reference frame on. For example, run
28// examples/aom_cx_set_ref av1 352 288 in.yuv out.ivf 4 30
29// The parameter is parsed as follows:
30//
31//
32// Extra Variables
33// ---------------
34// This example maintains the frame number passed on the command line
35// in the `update_frame_num` variable.
36//
37//
38// Configuration
39// -------------
40//
41// The reference frame is updated on the frame specified on the command
42// line.
43//
44// Observing The Effects
45// ---------------------
46// The encoder and decoder results should be matching when the same reference
47// frame setting operation is done in both encoder and decoder. Otherwise,
48// the encoder/decoder mismatch would be seen.
49
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53
54#include "aom/aom_decoder.h"
55#include "aom/aom_encoder.h"
56#include "aom/aomcx.h"
57#include "aom_scale/yv12config.h"
58#include "common/tools_common.h"
59#include "common/video_writer.h"
60#include "examples/encoder_util.h"
61
62static const char *exec_name;
63
64void usage_exit() {
65 fprintf(stderr,
66 "Usage: %s <codec> <width> <height> <infile> <outfile> "
67 "<frame> <limit(optional)>\n",
68 exec_name);
69 exit(EXIT_FAILURE);
70}
71
72static void testing_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
73 unsigned int frame_out, int *mismatch_seen) {
74 aom_image_t enc_img, dec_img;
75
76 if (*mismatch_seen) return;
77
78 /* Get the internal reference frame */
79 if (aom_codec_control(encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img))
80 die_codec(encoder, "Failed to get encoder reference frame");
81 if (aom_codec_control(decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img))
82 die_codec(decoder, "Failed to get decoder reference frame");
83
84 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
85 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
86 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
87 aom_image_t enc_hbd_img;
88 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
89 enc_img.d_w, enc_img.d_h, 16);
90 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
91 enc_img = enc_hbd_img;
92 }
93 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
94 aom_image_t dec_hbd_img;
95 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
96 dec_img.d_w, dec_img.d_h, 16);
97 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
98 dec_img = dec_hbd_img;
99 }
100 }
101
102 if (!aom_compare_img(&enc_img, &dec_img)) {
103 int y[4], u[4], v[4];
104 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
105 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
106 } else {
107 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
108 }
109
110 printf(
111 "Encode/decode mismatch on frame %d at"
112 " Y[%d, %d] {%d/%d},"
113 " U[%d, %d] {%d/%d},"
114 " V[%d, %d] {%d/%d}",
115 frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
116 v[2], v[3]);
117 *mismatch_seen = 1;
118 }
119
120 aom_img_free(&enc_img);
121 aom_img_free(&dec_img);
122}
123
124static int encode_frame(aom_codec_ctx_t *ecodec, aom_image_t *img,
125 unsigned int frame_in, AvxVideoWriter *writer,
126 int test_decode, aom_codec_ctx_t *dcodec,
127 unsigned int *frame_out, int *mismatch_seen,
128 aom_image_t *ext_ref) {
129 int got_pkts = 0;
130 aom_codec_iter_t iter = NULL;
131 const aom_codec_cx_pkt_t *pkt = NULL;
132 int got_data;
133 const aom_codec_err_t res = aom_codec_encode(ecodec, img, frame_in, 1, 0);
134 if (res != AOM_CODEC_OK) die_codec(ecodec, "Failed to encode frame");
135
136 got_data = 0;
137
138 while ((pkt = aom_codec_get_cx_data(ecodec, &iter)) != NULL) {
139 got_pkts = 1;
140
141 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
142 const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
143
144 ++*frame_out;
145
146 if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
147 pkt->data.frame.sz,
148 pkt->data.frame.pts)) {
149 die_codec(ecodec, "Failed to write compressed frame");
150 }
151 printf(keyframe ? "K" : ".");
152 fflush(stdout);
153 got_data = 1;
154
155 // Decode 1 frame.
156 if (test_decode) {
157 if (aom_codec_decode(dcodec, pkt->data.frame.buf,
158 (unsigned int)pkt->data.frame.sz, NULL))
159 die_codec(dcodec, "Failed to decode frame.");
160
161 // Copy out first decoded frame, and use it as reference later.
162 if (*frame_out == 1 && ext_ref != NULL)
163 if (aom_codec_control(dcodec, AV1_COPY_NEW_FRAME_IMAGE, ext_ref))
164 die_codec(dcodec, "Failed to get decoder new frame");
165 }
166 }
167 }
168
169 // Mismatch checking
170 if (got_data && test_decode) {
171 testing_decode(ecodec, dcodec, *frame_out, mismatch_seen);
172 }
173
174 return got_pkts;
175}
176
177int main(int argc, char **argv) {
178 FILE *infile = NULL;
179 // Encoder
180 aom_codec_ctx_t ecodec;
182 unsigned int frame_in = 0;
183 aom_image_t raw;
184 aom_image_t raw_shift;
185 aom_image_t ext_ref;
186 aom_codec_err_t res;
187 AvxVideoInfo info;
188 AvxVideoWriter *writer = NULL;
189 const AvxInterface *encoder = NULL;
190 int flags = 0;
191 int allocated_raw_shift = 0;
194
195 // Test encoder/decoder mismatch.
196 int test_decode = 1;
197 // Decoder
198 aom_codec_ctx_t dcodec;
199 unsigned int frame_out = 0;
200
201 // The frame number to set reference frame on
202 unsigned int update_frame_num = 0;
203 int mismatch_seen = 0;
204
205 const int fps = 30;
206 const int bitrate = 500;
207
208 const char *codec_arg = NULL;
209 const char *width_arg = NULL;
210 const char *height_arg = NULL;
211 const char *infile_arg = NULL;
212 const char *outfile_arg = NULL;
213 const char *update_frame_num_arg = NULL;
214 unsigned int limit = 0;
215 exec_name = argv[0];
216
217 // Clear explicitly, as simply assigning "{ 0 }" generates
218 // "missing-field-initializers" warning in some compilers.
219 memset(&ecodec, 0, sizeof(ecodec));
220 memset(&cfg, 0, sizeof(cfg));
221 memset(&info, 0, sizeof(info));
222
223 if (argc < 7) die("Invalid number of arguments");
224
225 codec_arg = argv[1];
226 width_arg = argv[2];
227 height_arg = argv[3];
228 infile_arg = argv[4];
229 outfile_arg = argv[5];
230 update_frame_num_arg = argv[6];
231
232 encoder = get_aom_encoder_by_name(codec_arg);
233 if (!encoder) die("Unsupported codec.");
234
235 update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
236 // In AV1, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
237 // allocated while calling aom_codec_encode(), thus, setting reference for
238 // 1st frame isn't supported.
239 if (update_frame_num <= 1) {
240 die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
241 }
242
243 if (argc > 7) {
244 limit = (unsigned int)strtoul(argv[7], NULL, 0);
245 if (update_frame_num > limit)
246 die("Update frame number couldn't larger than limit\n");
247 }
248
249 info.codec_fourcc = encoder->fourcc;
250 info.frame_width = (int)strtol(width_arg, NULL, 0);
251 info.frame_height = (int)strtol(height_arg, NULL, 0);
252 info.time_base.numerator = 1;
253 info.time_base.denominator = fps;
254
255 if (info.frame_width <= 0 || info.frame_height <= 0) {
256 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
257 }
258
259 // In this test, the bit depth of input video is 8-bit, and the input format
260 // is AOM_IMG_FMT_I420.
261 if (!aom_img_alloc(&raw, raw_fmt, info.frame_width, info.frame_height, 32)) {
262 die("Failed to allocate image.");
263 }
264
265 if (!CONFIG_LOWBITDEPTH) ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
266 // Allocate memory with the border so that it can be used as a reference.
267 if (!aom_img_alloc_with_border(&ext_ref, ref_fmt, info.frame_width,
268 info.frame_height, 32, 8,
269 AOM_BORDER_IN_PIXELS)) {
270 die("Failed to allocate image.");
271 }
272
273 printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
274
275 res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
276 if (res) die_codec(&ecodec, "Failed to get default codec config.");
277
278 cfg.g_w = info.frame_width;
279 cfg.g_h = info.frame_height;
280 cfg.g_timebase.num = info.time_base.numerator;
281 cfg.g_timebase.den = info.time_base.denominator;
282 cfg.rc_target_bitrate = bitrate;
283 cfg.g_lag_in_frames = 3;
285
286 flags |= (cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH)
288 : 0;
289
290 writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
291 if (!writer) die("Failed to open %s for writing.", outfile_arg);
292
293 if (!(infile = fopen(infile_arg, "rb")))
294 die("Failed to open %s for reading.", infile_arg);
295
296 if (aom_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, flags))
297 die_codec(&ecodec, "Failed to initialize encoder");
298
299 // Disable alt_ref.
301 die_codec(&ecodec, "Failed to set enable auto alt ref");
302
303 if (test_decode) {
304 const AvxInterface *decoder = get_aom_decoder_by_name(codec_arg);
305 if (aom_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0))
306 die_codec(&dcodec, "Failed to initialize decoder.");
307 }
308
309 // Encode frames.
310 while (aom_img_read(&raw, infile)) {
311 if (limit && frame_in >= limit) break;
312 aom_image_t *frame_to_encode;
313
314 if (!CONFIG_LOWBITDEPTH) {
315 // Need to allocate larger buffer to use hbd internal.
316 int input_shift = 0;
317 if (!allocated_raw_shift) {
318 aom_img_alloc(&raw_shift, raw_fmt | AOM_IMG_FMT_HIGHBITDEPTH,
319 info.frame_width, info.frame_height, 32);
320 allocated_raw_shift = 1;
321 }
322 aom_img_upshift(&raw_shift, &raw, input_shift);
323 frame_to_encode = &raw_shift;
324 } else {
325 frame_to_encode = &raw;
326 }
327
328 if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
329 av1_ref_frame_t ref;
330 ref.idx = 0;
331 ref.use_external_ref = 0;
332 ref.img = ext_ref;
333 // Set reference frame in encoder.
334 if (aom_codec_control(&ecodec, AV1_SET_REFERENCE, &ref))
335 die_codec(&ecodec, "Failed to set encoder reference frame");
336 printf(" <SET_REF>");
337
338 // If set_reference in decoder is commented out, the enc/dec mismatch
339 // would be seen.
340 if (test_decode) {
341 ref.use_external_ref = 1;
342 if (aom_codec_control(&dcodec, AV1_SET_REFERENCE, &ref))
343 die_codec(&dcodec, "Failed to set decoder reference frame");
344 }
345 }
346
347 encode_frame(&ecodec, frame_to_encode, frame_in, writer, test_decode,
348 &dcodec, &frame_out, &mismatch_seen, &ext_ref);
349 frame_in++;
350 if (mismatch_seen) break;
351 }
352
353 // Flush encoder.
354 if (!mismatch_seen)
355 while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
356 &frame_out, &mismatch_seen, NULL)) {
357 }
358
359 printf("\n");
360 fclose(infile);
361 printf("Processed %d frames.\n", frame_out);
362
363 if (test_decode) {
364 if (!mismatch_seen)
365 printf("Encoder/decoder results are matching.\n");
366 else
367 printf("Encoder/decoder results are NOT matching.\n");
368 }
369
370 if (test_decode)
371 if (aom_codec_destroy(&dcodec))
372 die_codec(&dcodec, "Failed to destroy decoder");
373
374 if (allocated_raw_shift) aom_img_free(&raw_shift);
375 aom_img_free(&ext_ref);
376 aom_img_free(&raw);
377 if (aom_codec_destroy(&ecodec))
378 die_codec(&ecodec, "Failed to destroy encoder.");
379
380 aom_video_writer_close(writer);
381
382 return EXIT_SUCCESS;
383}
Describes the decoder algorithm interface to applications.
Describes the encoder algorithm interface to applications.
aom_image_t * aom_img_alloc_with_border(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align, unsigned int size_align, unsigned int border)
Open a descriptor, allocating storage for the underlying image with a border.
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition: aom_image.h:38
aom_image_t * aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
@ AOM_IMG_FMT_I420
Definition: aom_image.h:45
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
void aom_img_free(aom_image_t *img)
Close an image descriptor.
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
@ AOME_SET_ENABLEAUTOALTREF
Codec control function to enable automatic set and use alf frames.
Definition: aomcx.h:186
@ AV1_GET_NEW_FRAME_IMAGE
Definition: aom.h:65
@ AV1_SET_REFERENCE
Definition: aom.h:60
@ AV1_COPY_NEW_FRAME_IMAGE
Definition: aom.h:66
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:414
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:101
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
@ AOM_BITS_8
Definition: aom_codec.h:225
@ AOM_CODEC_OK
Operation completed without error.
Definition: aom_codec.h:103
aom_codec_err_t aom_codec_decode(aom_codec_ctx_t *ctx, const uint8_t *data, size_t data_sz, void *user_priv)
Decode data.
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition: aom_decoder.h:135
const aom_codec_cx_pkt_t * aom_codec_get_cx_data(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Encoded data iterator.
aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img, aom_codec_pts_t pts, unsigned long duration, aom_enc_frame_flags_t flags)
Encode a frame.
#define aom_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver()
Definition: aom_encoder.h:764
aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface, aom_codec_enc_cfg_t *cfg, unsigned int reserved)
Get a default configuration.
#define AOM_CODEC_USE_HIGHBITDEPTH
Make the encoder output one partition at a time.
Definition: aom_encoder.h:70
#define AOM_FRAME_IS_KEY
Definition: aom_encoder.h:96
@ AOM_CODEC_CX_FRAME_PKT
Definition: aom_encoder.h:126
Codec context structure.
Definition: aom_codec.h:204
Encoder output packet.
Definition: aom_encoder.h:138
size_t sz
Definition: aom_encoder.h:143
enum aom_codec_cx_pkt_kind kind
Definition: aom_encoder.h:139
union aom_codec_cx_pkt::@1 data
aom_codec_pts_t pts
time stamp to show frame (in timebase units)
Definition: aom_encoder.h:145
struct aom_codec_cx_pkt::@1::@2 frame
aom_codec_frame_flags_t flags
Definition: aom_encoder.h:148
void * buf
Definition: aom_encoder.h:142
Encoder configuration structure.
Definition: aom_encoder.h:228
struct aom_rational g_timebase
Stream timebase units.
Definition: aom_encoder.h:325
unsigned int g_h
Height of the frame.
Definition: aom_encoder.h:276
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: aom_encoder.h:354
aom_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: aom_encoder.h:303
unsigned int g_w
Width of the frame.
Definition: aom_encoder.h:267
unsigned int rc_target_bitrate
Target data rate.
Definition: aom_encoder.h:481
Image Descriptor.
Definition: aom_image.h:141
aom_img_fmt_t fmt
Definition: aom_image.h:142
unsigned int d_w
Definition: aom_image.h:156
unsigned int d_h
Definition: aom_image.h:157
int num
Definition: aom_encoder.h:179
int den
Definition: aom_encoder.h:180
AV1 specific reference frame data struct.
Definition: aom.h:108
int use_external_ref
Definition: aom.h:110
aom_image_t img
Definition: aom.h:111
int idx
Definition: aom.h:109