1 /*
2 * Copyright (c) 2018, 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 // Scalable Decoder
13 // ==============
14 //
15 // This is an example of a scalable decoder loop. It takes a 2-spatial-layer
16 // input file
17 // containing the compressed data (in OBU format), passes it through the
18 // decoder, and writes the decompressed frames to disk. The base layer and
19 // enhancement layers are stored as separate files, out_lyr0.yuv and
20 // out_lyr1.yuv, respectively.
21 //
22 // Standard Includes
23 // -----------------
24 // For decoders, you only have to include `aom_decoder.h` and then any
25 // header files for the specific codecs you use. In this case, we're using
26 // av1.
27 //
28 // Initializing The Codec
29 // ----------------------
30 // The libaom decoder is initialized by the call to aom_codec_dec_init().
31 // Determining the codec interface to use is handled by AvxVideoReader and the
32 // functions prefixed with aom_video_reader_. Discussion of those functions is
33 // beyond the scope of this example, but the main gist is to open the input file
34 // and parse just enough of it to determine if it's a AVx file and which AVx
35 // codec is contained within the file.
36 // Note the NULL pointer passed to aom_codec_dec_init(). We do that in this
37 // example because we want the algorithm to determine the stream configuration
38 // (width/height) and allocate memory automatically.
39 //
40 // Decoding A Frame
41 // ----------------
42 // Once the frame has been read into memory, it is decoded using the
43 // `aom_codec_decode` function. The call takes a pointer to the data
44 // (`frame`) and the length of the data (`frame_size`). No application data
45 // is associated with the frame in this example, so the `user_priv`
46 // parameter is NULL. The `deadline` parameter is left at zero for this
47 // example. This parameter is generally only used when doing adaptive post
48 // processing.
49 //
50 // Codecs may produce a variable number of output frames for every call to
51 // `aom_codec_decode`. These frames are retrieved by the
52 // `aom_codec_get_frame` iterator function. The iterator variable `iter` is
53 // initialized to NULL each time `aom_codec_decode` is called.
54 // `aom_codec_get_frame` is called in a loop, returning a pointer to a
55 // decoded image or NULL to indicate the end of list.
56 //
57 // Processing The Decoded Data
58 // ---------------------------
59 // In this example, we simply write the encoded data to disk. It is
60 // important to honor the image's `stride` values.
61 //
62 // Cleanup
63 // -------
64 // The `aom_codec_destroy` call frees any memory allocated by the codec.
65 //
66 // Error Handling
67 // --------------
68 // This example does not special case any error return codes. If there was
69 // an error, a descriptive message is printed and the program exits. With
70 // few exceptions, aom_codec functions return an enumerated error status,
71 // with the value `0` indicating success.
72
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76
77 #include "aom/aom_decoder.h"
78 #include "aom/aomdx.h"
79 #include "common/obudec.h"
80 #include "common/tools_common.h"
81 #include "common/video_reader.h"
82
83 static const char *exec_name;
84
85 #define MAX_LAYERS 5
86
usage_exit(void)87 void usage_exit(void) {
88 fprintf(stderr, "Usage: %s <infile>\n", exec_name);
89 exit(EXIT_FAILURE);
90 }
91
main(int argc,char ** argv)92 int main(int argc, char **argv) {
93 int frame_cnt = 0;
94 FILE *outfile[MAX_LAYERS];
95 char filename[80];
96 FILE *inputfile = NULL;
97 uint8_t *buf = NULL;
98 size_t bytes_in_buffer = 0;
99 size_t buffer_size = 0;
100 struct AvxInputContext aom_input_ctx;
101 struct ObuDecInputContext obu_ctx = { &aom_input_ctx, NULL, 0, 0, 0 };
102 aom_codec_stream_info_t si;
103 uint8_t tmpbuf[32];
104 unsigned int i;
105
106 exec_name = argv[0];
107
108 if (argc != 2) die("Invalid number of arguments.");
109
110 if (!(inputfile = fopen(argv[1], "rb")))
111 die("Failed to open %s for read.", argv[1]);
112 obu_ctx.avx_ctx->file = inputfile;
113 obu_ctx.avx_ctx->filename = argv[1];
114
115 aom_codec_iface_t *decoder = get_aom_decoder_by_index(0);
116 printf("Using %s\n", aom_codec_iface_name(decoder));
117
118 aom_codec_ctx_t codec;
119 if (aom_codec_dec_init(&codec, decoder, NULL, 0))
120 die("Failed to initialize decoder.");
121
122 if (aom_codec_control(&codec, AV1D_SET_OUTPUT_ALL_LAYERS, 1)) {
123 die_codec(&codec, "Failed to set output_all_layers control.");
124 }
125
126 // peak sequence header OBU to get number of spatial layers
127 const size_t ret = fread(tmpbuf, 1, 32, inputfile);
128 if (ret != 32) die_codec(&codec, "Input is not a valid obu file");
129 si.is_annexb = 0;
130 if (aom_codec_peek_stream_info(decoder, tmpbuf, 32, &si)) {
131 die_codec(&codec, "Input is not a valid obu file");
132 }
133 fseek(inputfile, -32, SEEK_CUR);
134
135 if (!file_is_obu(&obu_ctx))
136 die_codec(&codec, "Input is not a valid obu file");
137
138 // open base layer output yuv file
139 snprintf(filename, sizeof(filename), "out_lyr%d.yuv", 0);
140 if (!(outfile[0] = fopen(filename, "wb")))
141 die("Failed top open output for writing.");
142
143 // open any enhancement layer output yuv files
144 for (i = 1; i < si.number_spatial_layers; i++) {
145 snprintf(filename, sizeof(filename), "out_lyr%u.yuv", i);
146 if (!(outfile[i] = fopen(filename, "wb")))
147 die("Failed to open output for writing.");
148 }
149
150 while (!obudec_read_temporal_unit(&obu_ctx, &buf, &bytes_in_buffer,
151 &buffer_size)) {
152 aom_codec_iter_t iter = NULL;
153 aom_image_t *img = NULL;
154 if (aom_codec_decode(&codec, buf, bytes_in_buffer, NULL))
155 die_codec(&codec, "Failed to decode frame.");
156
157 while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
158 aom_image_t *img_shifted =
159 aom_img_alloc(NULL, AOM_IMG_FMT_I420, img->d_w, img->d_h, 16);
160 img_shifted->bit_depth = 8;
161 aom_img_downshift(img_shifted, img,
162 img->bit_depth - img_shifted->bit_depth);
163 if (img->spatial_id == 0) {
164 printf("Writing base layer 0 %d\n", frame_cnt);
165 aom_img_write(img_shifted, outfile[0]);
166 } else if (img->spatial_id <= (int)(si.number_spatial_layers - 1)) {
167 printf("Writing enhancement layer %d %d\n", img->spatial_id, frame_cnt);
168 aom_img_write(img_shifted, outfile[img->spatial_id]);
169 } else {
170 die_codec(&codec, "Invalid bitstream. Layer id exceeds layer count");
171 }
172 if (img->spatial_id == (int)(si.number_spatial_layers - 1)) ++frame_cnt;
173 }
174 }
175
176 printf("Processed %d frames.\n", frame_cnt);
177 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
178
179 for (i = 0; i < si.number_spatial_layers; i++) fclose(outfile[i]);
180
181 fclose(inputfile);
182
183 return EXIT_SUCCESS;
184 }
185