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