xref: /aosp_15_r20/external/libaom/examples/aom_cx_set_ref.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 
62 static const char *exec_name;
63 
usage_exit(void)64 void usage_exit(void) {
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 
testing_decode(aom_codec_ctx_t * encoder,aom_codec_ctx_t * decoder,unsigned int frame_out,int * mismatch_seen)72 static 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 %u 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 
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,aom_image_t * ext_ref)124 static 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 
main(int argc,char ** argv)177 int main(int argc, char **argv) {
178   FILE *infile = NULL;
179   // Encoder
180   aom_codec_ctx_t ecodec;
181   aom_codec_enc_cfg_t cfg;
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   int flags = 0;
190   int allocated_raw_shift = 0;
191   aom_img_fmt_t raw_fmt = AOM_IMG_FMT_I420;
192   aom_img_fmt_t ref_fmt = AOM_IMG_FMT_I420;
193 
194   // Test encoder/decoder mismatch.
195   int test_decode = 1;
196   // Decoder
197   aom_codec_ctx_t dcodec;
198   unsigned int frame_out = 0;
199 
200   // The frame number to set reference frame on
201   unsigned int update_frame_num = 0;
202   int mismatch_seen = 0;
203 
204   const int fps = 30;
205   const int bitrate = 500;
206 
207   const char *codec_arg = NULL;
208   const char *width_arg = NULL;
209   const char *height_arg = NULL;
210   const char *infile_arg = NULL;
211   const char *outfile_arg = NULL;
212   const char *update_frame_num_arg = NULL;
213   unsigned int limit = 0;
214   exec_name = argv[0];
215 
216   // Clear explicitly, as simply assigning "{ 0 }" generates
217   // "missing-field-initializers" warning in some compilers.
218   memset(&ecodec, 0, sizeof(ecodec));
219   memset(&cfg, 0, sizeof(cfg));
220   memset(&info, 0, sizeof(info));
221 
222   if (argc < 7) die("Invalid number of arguments");
223 
224   codec_arg = argv[1];
225   width_arg = argv[2];
226   height_arg = argv[3];
227   infile_arg = argv[4];
228   outfile_arg = argv[5];
229   update_frame_num_arg = argv[6];
230 
231   aom_codec_iface_t *encoder = get_aom_encoder_by_short_name(codec_arg);
232   if (!encoder) die("Unsupported codec.");
233 
234   update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0);
235   // In AV1, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
236   // allocated while calling aom_codec_encode(), thus, setting reference for
237   // 1st frame isn't supported.
238   if (update_frame_num <= 1) {
239     die("Couldn't parse frame number '%s'\n", update_frame_num_arg);
240   }
241 
242   if (argc > 7) {
243     limit = (unsigned int)strtoul(argv[7], NULL, 0);
244     if (update_frame_num > limit)
245       die("Update frame number couldn't larger than limit\n");
246   }
247 
248   info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
249   info.frame_width = (int)strtol(width_arg, NULL, 0);
250   info.frame_height = (int)strtol(height_arg, NULL, 0);
251   info.time_base.numerator = 1;
252   info.time_base.denominator = fps;
253 
254   if (info.frame_width <= 0 || info.frame_height <= 0) {
255     die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
256   }
257 
258   // In this test, the bit depth of input video is 8-bit, and the input format
259   // is AOM_IMG_FMT_I420.
260   if (!aom_img_alloc(&raw, raw_fmt, info.frame_width, info.frame_height, 32)) {
261     die("Failed to allocate image.");
262   }
263 
264   if (FORCE_HIGHBITDEPTH_DECODING) ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
265   // Allocate memory with the border so that it can be used as a reference.
266   if (!aom_img_alloc_with_border(&ext_ref, ref_fmt, info.frame_width,
267                                  info.frame_height, 32, 8,
268                                  AOM_DEC_BORDER_IN_PIXELS)) {
269     die("Failed to allocate image.");
270   }
271 
272   printf("Using %s\n", aom_codec_iface_name(encoder));
273 
274 #if CONFIG_REALTIME_ONLY
275   res = aom_codec_enc_config_default(encoder, &cfg, 1);
276 #else
277   res = aom_codec_enc_config_default(encoder, &cfg, 0);
278 #endif
279   if (res) die_codec(&ecodec, "Failed to get default codec config.");
280 
281   cfg.g_w = info.frame_width;
282   cfg.g_h = info.frame_height;
283   cfg.g_timebase.num = info.time_base.numerator;
284   cfg.g_timebase.den = info.time_base.denominator;
285   cfg.rc_target_bitrate = bitrate;
286   cfg.g_lag_in_frames = 3;
287   cfg.g_bit_depth = AOM_BITS_8;
288 
289   flags |= (cfg.g_bit_depth > AOM_BITS_8 || FORCE_HIGHBITDEPTH_DECODING)
290                ? AOM_CODEC_USE_HIGHBITDEPTH
291                : 0;
292 
293   writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
294   if (!writer) die("Failed to open %s for writing.", outfile_arg);
295 
296   if (!(infile = fopen(infile_arg, "rb")))
297     die("Failed to open %s for reading.", infile_arg);
298 
299   if (aom_codec_enc_init(&ecodec, encoder, &cfg, flags))
300     die("Failed to initialize encoder");
301 
302   // Disable alt_ref.
303   if (aom_codec_control(&ecodec, AOME_SET_ENABLEAUTOALTREF, 0))
304     die_codec(&ecodec, "Failed to set enable auto alt ref");
305 
306   if (test_decode) {
307     aom_codec_iface_t *decoder = get_aom_decoder_by_short_name(codec_arg);
308     if (aom_codec_dec_init(&dcodec, decoder, NULL, 0))
309       die("Failed to initialize decoder.");
310   }
311 
312   // Encode frames.
313   while (aom_img_read(&raw, infile)) {
314     if (limit && frame_in >= limit) break;
315     aom_image_t *frame_to_encode;
316 
317     if (FORCE_HIGHBITDEPTH_DECODING) {
318       // Need to allocate larger buffer to use hbd internal.
319       int input_shift = 0;
320       if (!allocated_raw_shift) {
321         aom_img_alloc(&raw_shift, raw_fmt | AOM_IMG_FMT_HIGHBITDEPTH,
322                       info.frame_width, info.frame_height, 32);
323         allocated_raw_shift = 1;
324       }
325       aom_img_upshift(&raw_shift, &raw, input_shift);
326       frame_to_encode = &raw_shift;
327     } else {
328       frame_to_encode = &raw;
329     }
330 
331     if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
332       av1_ref_frame_t ref;
333       ref.idx = 0;
334       ref.use_external_ref = 0;
335       ref.img = ext_ref;
336       // Set reference frame in encoder.
337       if (aom_codec_control(&ecodec, AV1_SET_REFERENCE, &ref))
338         die_codec(&ecodec, "Failed to set encoder reference frame");
339       printf(" <SET_REF>");
340 
341 #if CONFIG_REALTIME_ONLY
342       // Set cpu speed in encoder.
343       if (aom_codec_control(&ecodec, AOME_SET_CPUUSED, 7))
344         die_codec(&ecodec, "Failed to set cpu speed");
345 #endif
346 
347       // If set_reference in decoder is commented out, the enc/dec mismatch
348       // would be seen.
349       if (test_decode) {
350         ref.use_external_ref = 1;
351         if (aom_codec_control(&dcodec, AV1_SET_REFERENCE, &ref))
352           die_codec(&dcodec, "Failed to set decoder reference frame");
353       }
354     }
355 
356     encode_frame(&ecodec, frame_to_encode, frame_in, writer, test_decode,
357                  &dcodec, &frame_out, &mismatch_seen, &ext_ref);
358     frame_in++;
359     if (mismatch_seen) break;
360   }
361 
362   // Flush encoder.
363   if (!mismatch_seen)
364     while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec,
365                         &frame_out, &mismatch_seen, NULL)) {
366     }
367 
368   printf("\n");
369   fclose(infile);
370   printf("Processed %u frames.\n", frame_out);
371 
372   if (test_decode) {
373     if (!mismatch_seen)
374       printf("Encoder/decoder results are matching.\n");
375     else
376       printf("Encoder/decoder results are NOT matching.\n");
377   }
378 
379   if (test_decode)
380     if (aom_codec_destroy(&dcodec))
381       die_codec(&dcodec, "Failed to destroy decoder");
382 
383   if (allocated_raw_shift) aom_img_free(&raw_shift);
384   aom_img_free(&ext_ref);
385   aom_img_free(&raw);
386   if (aom_codec_destroy(&ecodec))
387     die_codec(&ecodec, "Failed to destroy encoder.");
388 
389   aom_video_writer_close(writer);
390 
391   return EXIT_SUCCESS;
392 }
393