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   aom_codec_ctx_t codec;
97   const AvxInterface *decoder = NULL;
98   FILE *inputfile = NULL;
99   uint8_t *buf = NULL;
100   size_t bytes_in_buffer = 0;
101   size_t buffer_size = 0;
102   struct AvxInputContext aom_input_ctx;
103   struct ObuDecInputContext obu_ctx = { &aom_input_ctx, NULL, 0, 0, 0 };
104   aom_codec_stream_info_t si;
105   uint8_t tmpbuf[32];
106   unsigned int i;
107 
108   exec_name = argv[0];
109 
110   if (argc != 2) die("Invalid number of arguments.");
111 
112   if (!(inputfile = fopen(argv[1], "rb")))
113     die("Failed to open %s for read.", argv[1]);
114   obu_ctx.avx_ctx->file = inputfile;
115   obu_ctx.avx_ctx->filename = argv[1];
116 
117   decoder = get_aom_decoder_by_index(0);
118   printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
119 
120   if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
121     die_codec(&codec, "Failed to initialize decoder.");
122 
123   if (aom_codec_control(&codec, AV1D_SET_OUTPUT_ALL_LAYERS, 1)) {
124     die_codec(&codec, "Failed to set output_all_layers control.");
125   }
126 
127   // peak sequence header OBU to get number of spatial layers
128   const size_t ret = fread(tmpbuf, 1, 32, inputfile);
129   if (ret != 32) die_codec(&codec, "Input is not a valid obu file");
130   si.is_annexb = 0;
131   if (aom_codec_peek_stream_info(decoder->codec_interface(), tmpbuf, 32, &si)) {
132     die_codec(&codec, "Input is not a valid obu file");
133   }
134   fseek(inputfile, -32, SEEK_CUR);
135 
136   if (!file_is_obu(&obu_ctx))
137     die_codec(&codec, "Input is not a valid obu file");
138 
139   // open base layer output yuv file
140   snprintf(filename, sizeof(filename), "out_lyr%d.yuv", 0);
141   if (!(outfile[0] = fopen(filename, "wb")))
142     die("Failed top open output for writing.");
143 
144   // open any enhancement layer output yuv files
145   for (i = 1; i < si.number_spatial_layers; i++) {
146     snprintf(filename, sizeof(filename), "out_lyr%d.yuv", i);
147     if (!(outfile[i] = fopen(filename, "wb")))
148       die("Failed to open output for writing.");
149   }
150 
151   while (!obudec_read_temporal_unit(&obu_ctx, &buf, &bytes_in_buffer,
152                                     &buffer_size)) {
153     aom_codec_iter_t iter = NULL;
154     aom_image_t *img = NULL;
155     if (aom_codec_decode(&codec, buf, bytes_in_buffer, NULL))
156       die_codec(&codec, "Failed to decode frame.");
157 
158     while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
159       aom_image_t *img_shifted =
160           aom_img_alloc(NULL, AOM_IMG_FMT_I420, img->d_w, img->d_h, 16);
161       img_shifted->bit_depth = 8;
162       aom_img_downshift(img_shifted, img,
163                         img->bit_depth - img_shifted->bit_depth);
164       if (img->spatial_id == 0) {
165         printf("Writing        base layer 0 %d\n", frame_cnt);
166         aom_img_write(img_shifted, outfile[0]);
167       } else if (img->spatial_id <= (int)(si.number_spatial_layers - 1)) {
168         printf("Writing enhancement layer %d %d\n", img->spatial_id, frame_cnt);
169         aom_img_write(img_shifted, outfile[img->spatial_id]);
170       } else {
171         die_codec(&codec, "Invalid bitstream. Layer id exceeds layer count");
172       }
173       if (img->spatial_id == (int)(si.number_spatial_layers - 1)) ++frame_cnt;
174     }
175   }
176 
177   printf("Processed %d frames.\n", frame_cnt);
178   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
179 
180   for (i = 0; i < si.number_spatial_layers; i++) fclose(outfile[i]);
181 
182   fclose(inputfile);
183 
184   return EXIT_SUCCESS;
185 }
186