xref: /aosp_15_r20/external/libaom/examples/decode_with_drops.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 // Decode With Drops Example
13 // =========================
14 //
15 // This is an example utility which drops a series of frames, as specified
16 // on the command line. This is useful for observing the error recovery
17 // features of the codec.
18 //
19 // Usage
20 // -----
21 // This example adds a single argument to the `simple_decoder` example,
22 // which specifies the range or pattern of frames to drop. The parameter is
23 // parsed as follows:
24 //
25 // Dropping A Range Of Frames
26 // --------------------------
27 // To drop a range of frames, specify the starting frame and the ending
28 // frame to drop, separated by a dash. The following command will drop
29 // frames 5 through 10 (base 1).
30 //
31 //  $ ./decode_with_drops in.ivf out.i420 5-10
32 //
33 //
34 // Dropping A Pattern Of Frames
35 // ----------------------------
36 // To drop a pattern of frames, specify the number of frames to drop and
37 // the number of frames after which to repeat the pattern, separated by
38 // a forward-slash. The following command will drop 3 of 7 frames.
39 // Specifically, it will decode 4 frames, then drop 3 frames, and then
40 // repeat.
41 //
42 //  $ ./decode_with_drops in.ivf out.i420 3/7
43 //
44 //
45 // Extra Variables
46 // ---------------
47 // This example maintains the pattern passed on the command line in the
48 // `n`, `m`, and `is_range` variables:
49 //
50 //
51 // Making The Drop Decision
52 // ------------------------
53 // The example decides whether to drop the frame based on the current
54 // frame number, immediately before decoding the frame.
55 
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 
60 #include "aom/aom_decoder.h"
61 #include "aom/aomdx.h"
62 #include "common/tools_common.h"
63 #include "common/video_reader.h"
64 
65 static const char *exec_name;
66 
usage_exit(void)67 void usage_exit(void) {
68   fprintf(stderr, "Usage: %s <infile> <outfile> <N-M|N/M>\n", exec_name);
69   exit(EXIT_FAILURE);
70 }
71 
main(int argc,char ** argv)72 int main(int argc, char **argv) {
73   int frame_cnt = 0;
74   FILE *outfile = NULL;
75   AvxVideoReader *reader = NULL;
76   const AvxVideoInfo *info = NULL;
77   int n = 0;
78   int m = 0;
79   int is_range = 0;
80   char *nptr = NULL;
81 
82   exec_name = argv[0];
83 
84   if (argc != 4) die("Invalid number of arguments.");
85 
86   reader = aom_video_reader_open(argv[1]);
87   if (!reader) die("Failed to open %s for reading.", argv[1]);
88 
89   if (!(outfile = fopen(argv[2], "wb")))
90     die("Failed to open %s for writing.", argv[2]);
91 
92   n = (int)strtol(argv[3], &nptr, 0);
93   m = (int)strtol(nptr + 1, NULL, 0);
94   is_range = (*nptr == '-');
95   if (!n || !m || (*nptr != '-' && *nptr != '/'))
96     die("Couldn't parse pattern %s.\n", argv[3]);
97 
98   info = aom_video_reader_get_info(reader);
99 
100   aom_codec_iface_t *decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
101   if (!decoder) die("Unknown input codec.");
102 
103   printf("Using %s\n", aom_codec_iface_name(decoder));
104   aom_codec_ctx_t codec;
105   if (aom_codec_dec_init(&codec, decoder, NULL, 0))
106     die("Failed to initialize decoder.");
107 
108   while (aom_video_reader_read_frame(reader)) {
109     aom_codec_iter_t iter = NULL;
110     aom_image_t *img = NULL;
111     size_t frame_size = 0;
112     int skip;
113     const unsigned char *frame =
114         aom_video_reader_get_frame(reader, &frame_size);
115     ++frame_cnt;
116 
117     skip = (is_range && frame_cnt >= n && frame_cnt <= m) ||
118            (!is_range && m - (frame_cnt - 1) % m <= n);
119 
120     if (!skip) {
121       putc('.', stdout);
122       if (aom_codec_decode(&codec, frame, frame_size, NULL))
123         die_codec(&codec, "Failed to decode frame.");
124 
125       while ((img = aom_codec_get_frame(&codec, &iter)) != NULL)
126         aom_img_write(img, outfile);
127     } else {
128       putc('X', stdout);
129     }
130 
131     fflush(stdout);
132   }
133 
134   printf("Processed %d frames.\n", frame_cnt);
135   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
136 
137   printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
138          info->frame_width, info->frame_height, argv[2]);
139 
140   aom_video_reader_close(reader);
141   fclose(outfile);
142 
143   return EXIT_SUCCESS;
144 }
145