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 // Simple Decoder
13*77c1e3ccSAndroid Build Coastguard Worker // ==============
14*77c1e3ccSAndroid Build Coastguard Worker //
15*77c1e3ccSAndroid Build Coastguard Worker // This is an example of a simple decoder loop. It takes an input file
16*77c1e3ccSAndroid Build Coastguard Worker // containing the compressed data (in IVF format), passes it through the
17*77c1e3ccSAndroid Build Coastguard Worker // decoder, and writes the decompressed frames to disk. Other decoder
18*77c1e3ccSAndroid Build Coastguard Worker // examples build upon this one.
19*77c1e3ccSAndroid Build Coastguard Worker //
20*77c1e3ccSAndroid Build Coastguard Worker // The details of the IVF format have been elided from this example for
21*77c1e3ccSAndroid Build Coastguard Worker // simplicity of presentation, as IVF files will not generally be used by
22*77c1e3ccSAndroid Build Coastguard Worker // your application. In general, an IVF file consists of a file header,
23*77c1e3ccSAndroid Build Coastguard Worker // followed by a variable number of frames. Each frame consists of a frame
24*77c1e3ccSAndroid Build Coastguard Worker // header followed by a variable length payload. The length of the payload
25*77c1e3ccSAndroid Build Coastguard Worker // is specified in the first four bytes of the frame header. The payload is
26*77c1e3ccSAndroid Build Coastguard Worker // the raw compressed data.
27*77c1e3ccSAndroid Build Coastguard Worker //
28*77c1e3ccSAndroid Build Coastguard Worker // Standard Includes
29*77c1e3ccSAndroid Build Coastguard Worker // -----------------
30*77c1e3ccSAndroid Build Coastguard Worker // For decoders, you only have to include `aom_decoder.h` and then any
31*77c1e3ccSAndroid Build Coastguard Worker // header files for the specific codecs you use. In this case, we're using
32*77c1e3ccSAndroid Build Coastguard Worker // aom.
33*77c1e3ccSAndroid Build Coastguard Worker //
34*77c1e3ccSAndroid Build Coastguard Worker // Initializing The Codec
35*77c1e3ccSAndroid Build Coastguard Worker // ----------------------
36*77c1e3ccSAndroid Build Coastguard Worker // The libaom decoder is initialized by the call to aom_codec_dec_init().
37*77c1e3ccSAndroid Build Coastguard Worker // Determining the codec interface to use is handled by AvxVideoReader and the
38*77c1e3ccSAndroid Build Coastguard Worker // functions prefixed with aom_video_reader_. Discussion of those functions is
39*77c1e3ccSAndroid Build Coastguard Worker // beyond the scope of this example, but the main gist is to open the input file
40*77c1e3ccSAndroid Build Coastguard Worker // and parse just enough of it to determine if it's a AVx file and which AVx
41*77c1e3ccSAndroid Build Coastguard Worker // codec is contained within the file.
42*77c1e3ccSAndroid Build Coastguard Worker // Note the NULL pointer passed to aom_codec_dec_init(). We do that in this
43*77c1e3ccSAndroid Build Coastguard Worker // example because we want the algorithm to determine the stream configuration
44*77c1e3ccSAndroid Build Coastguard Worker // (width/height) and allocate memory automatically.
45*77c1e3ccSAndroid Build Coastguard Worker //
46*77c1e3ccSAndroid Build Coastguard Worker // Decoding A Frame
47*77c1e3ccSAndroid Build Coastguard Worker // ----------------
48*77c1e3ccSAndroid Build Coastguard Worker // Once the frame has been read into memory, it is decoded using the
49*77c1e3ccSAndroid Build Coastguard Worker // `aom_codec_decode` function. The call takes a pointer to the data
50*77c1e3ccSAndroid Build Coastguard Worker // (`frame`) and the length of the data (`frame_size`). No application data
51*77c1e3ccSAndroid Build Coastguard Worker // is associated with the frame in this example, so the `user_priv`
52*77c1e3ccSAndroid Build Coastguard Worker // parameter is NULL.
53*77c1e3ccSAndroid Build Coastguard Worker //
54*77c1e3ccSAndroid Build Coastguard Worker // Codecs may produce a variable number of output frames for every call to
55*77c1e3ccSAndroid Build Coastguard Worker // `aom_codec_decode`. These frames are retrieved by the
56*77c1e3ccSAndroid Build Coastguard Worker // `aom_codec_get_frame` iterator function. The iterator variable `iter` is
57*77c1e3ccSAndroid Build Coastguard Worker // initialized to NULL each time `aom_codec_decode` is called.
58*77c1e3ccSAndroid Build Coastguard Worker // `aom_codec_get_frame` is called in a loop, returning a pointer to a
59*77c1e3ccSAndroid Build Coastguard Worker // decoded image or NULL to indicate the end of list.
60*77c1e3ccSAndroid Build Coastguard Worker //
61*77c1e3ccSAndroid Build Coastguard Worker // Processing The Decoded Data
62*77c1e3ccSAndroid Build Coastguard Worker // ---------------------------
63*77c1e3ccSAndroid Build Coastguard Worker // In this example, we simply write the encoded data to disk. It is
64*77c1e3ccSAndroid Build Coastguard Worker // important to honor the image's `stride` values.
65*77c1e3ccSAndroid Build Coastguard Worker //
66*77c1e3ccSAndroid Build Coastguard Worker // Cleanup
67*77c1e3ccSAndroid Build Coastguard Worker // -------
68*77c1e3ccSAndroid Build Coastguard Worker // The `aom_codec_destroy` call frees any memory allocated by the codec.
69*77c1e3ccSAndroid Build Coastguard Worker //
70*77c1e3ccSAndroid Build Coastguard Worker // Error Handling
71*77c1e3ccSAndroid Build Coastguard Worker // --------------
72*77c1e3ccSAndroid Build Coastguard Worker // This example does not special case any error return codes. If there was
73*77c1e3ccSAndroid Build Coastguard Worker // an error, a descriptive message is printed and the program exits. With
74*77c1e3ccSAndroid Build Coastguard Worker // few exceptions, aom_codec functions return an enumerated error status,
75*77c1e3ccSAndroid Build Coastguard Worker // with the value `0` indicating success.
76*77c1e3ccSAndroid Build Coastguard Worker
77*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
78*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
79*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
80*77c1e3ccSAndroid Build Coastguard Worker
81*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_decoder.h"
82*77c1e3ccSAndroid Build Coastguard Worker #include "common/tools_common.h"
83*77c1e3ccSAndroid Build Coastguard Worker #include "common/video_reader.h"
84*77c1e3ccSAndroid Build Coastguard Worker
85*77c1e3ccSAndroid Build Coastguard Worker static const char *exec_name;
86*77c1e3ccSAndroid Build Coastguard Worker
usage_exit(void)87*77c1e3ccSAndroid Build Coastguard Worker void usage_exit(void) {
88*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
89*77c1e3ccSAndroid Build Coastguard Worker exit(EXIT_FAILURE);
90*77c1e3ccSAndroid Build Coastguard Worker }
91*77c1e3ccSAndroid Build Coastguard Worker
main(int argc,char ** argv)92*77c1e3ccSAndroid Build Coastguard Worker int main(int argc, char **argv) {
93*77c1e3ccSAndroid Build Coastguard Worker int frame_cnt = 0;
94*77c1e3ccSAndroid Build Coastguard Worker FILE *outfile = NULL;
95*77c1e3ccSAndroid Build Coastguard Worker AvxVideoReader *reader = NULL;
96*77c1e3ccSAndroid Build Coastguard Worker const AvxVideoInfo *info = NULL;
97*77c1e3ccSAndroid Build Coastguard Worker
98*77c1e3ccSAndroid Build Coastguard Worker exec_name = argv[0];
99*77c1e3ccSAndroid Build Coastguard Worker
100*77c1e3ccSAndroid Build Coastguard Worker if (argc != 3) die("Invalid number of arguments.");
101*77c1e3ccSAndroid Build Coastguard Worker
102*77c1e3ccSAndroid Build Coastguard Worker reader = aom_video_reader_open(argv[1]);
103*77c1e3ccSAndroid Build Coastguard Worker if (!reader) die("Failed to open %s for reading.", argv[1]);
104*77c1e3ccSAndroid Build Coastguard Worker
105*77c1e3ccSAndroid Build Coastguard Worker if (!(outfile = fopen(argv[2], "wb")))
106*77c1e3ccSAndroid Build Coastguard Worker die("Failed to open %s for writing.", argv[2]);
107*77c1e3ccSAndroid Build Coastguard Worker
108*77c1e3ccSAndroid Build Coastguard Worker info = aom_video_reader_get_info(reader);
109*77c1e3ccSAndroid Build Coastguard Worker
110*77c1e3ccSAndroid Build Coastguard Worker aom_codec_iface_t *decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
111*77c1e3ccSAndroid Build Coastguard Worker if (!decoder) die("Unknown input codec.");
112*77c1e3ccSAndroid Build Coastguard Worker
113*77c1e3ccSAndroid Build Coastguard Worker printf("Using %s\n", aom_codec_iface_name(decoder));
114*77c1e3ccSAndroid Build Coastguard Worker
115*77c1e3ccSAndroid Build Coastguard Worker aom_codec_ctx_t codec;
116*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_dec_init(&codec, decoder, NULL, 0))
117*77c1e3ccSAndroid Build Coastguard Worker die("Failed to initialize decoder.");
118*77c1e3ccSAndroid Build Coastguard Worker
119*77c1e3ccSAndroid Build Coastguard Worker while (aom_video_reader_read_frame(reader)) {
120*77c1e3ccSAndroid Build Coastguard Worker aom_codec_iter_t iter = NULL;
121*77c1e3ccSAndroid Build Coastguard Worker aom_image_t *img = NULL;
122*77c1e3ccSAndroid Build Coastguard Worker size_t frame_size = 0;
123*77c1e3ccSAndroid Build Coastguard Worker const unsigned char *frame =
124*77c1e3ccSAndroid Build Coastguard Worker aom_video_reader_get_frame(reader, &frame_size);
125*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_decode(&codec, frame, frame_size, NULL))
126*77c1e3ccSAndroid Build Coastguard Worker die_codec(&codec, "Failed to decode frame.");
127*77c1e3ccSAndroid Build Coastguard Worker
128*77c1e3ccSAndroid Build Coastguard Worker while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
129*77c1e3ccSAndroid Build Coastguard Worker aom_img_write(img, outfile);
130*77c1e3ccSAndroid Build Coastguard Worker ++frame_cnt;
131*77c1e3ccSAndroid Build Coastguard Worker }
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
142*77c1e3ccSAndroid Build Coastguard Worker fclose(outfile);
143*77c1e3ccSAndroid Build Coastguard Worker
144*77c1e3ccSAndroid Build Coastguard Worker return EXIT_SUCCESS;
145*77c1e3ccSAndroid Build Coastguard Worker }
146