blob: c229242238733a0b63319b6b47f1bca729eb87f0 [file] [log] [blame]
Soo-Chul Hanf8589862018-01-24 03:13:14 +00001/*
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
Wan-Teh Changb7c5d312018-05-15 14:24:54 -070019// enhancement layers are stored as separate files, out_lyr0.yuv and
20// out_lyr1.yuv, respectively.
Soo-Chul Hanf8589862018-01-24 03:13:14 +000021//
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"
David Barkerfb356012018-06-07 13:34:44 +010078#include "aom/aomdx.h"
Tom Finegan77902132018-05-21 10:19:15 -070079#include "common/obudec.h"
80#include "common/tools_common.h"
81#include "common/video_reader.h"
Soo-Chul Hanf8589862018-01-24 03:13:14 +000082
83static const char *exec_name;
84
85#define MAX_LAYERS 5
86
Soo-Chul Hanf8589862018-01-24 03:13:14 +000087void usage_exit(void) {
88 fprintf(stderr, "Usage: %s <infile>\n", exec_name);
89 exit(EXIT_FAILURE);
90}
91
92int 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;
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000102 struct AvxInputContext aom_input_ctx;
David Barkera8de8812018-06-07 13:06:31 +0100103 struct ObuDecInputContext obu_ctx = { &aom_input_ctx, NULL, 0, 0, 0 };
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000104 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]);
Tom Finegan66ae9162018-03-19 18:12:50 -0700114 obu_ctx.avx_ctx->file = inputfile;
115 obu_ctx.avx_ctx->filename = argv[1];
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000116
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
David Barkerfb356012018-06-07 13:34:44 +0100123 if (aom_codec_control(&codec, AV1D_SET_OUTPUT_ALL_LAYERS, 1)) {
124 die_codec(&codec, "Failed to set output_all_layers control.");
125 }
126
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400127 // peak sequence header OBU to get number of spatial layers
Hui Sue19c3212018-02-21 14:32:18 -0800128 const size_t ret = fread(tmpbuf, 1, 32, inputfile);
129 if (ret != 32) die_codec(&codec, "Input is not a valid obu file");
Soo-Chul Han4ea3c5e2018-04-06 10:18:32 -0400130 si.is_annexb = 0;
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000131 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
Soo-Chul Han4ea3c5e2018-04-06 10:18:32 -0400136 if (!file_is_obu(&obu_ctx))
137 die_codec(&codec, "Input is not a valid obu file");
138
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400139 // 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++) {
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000146 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
Tom Finegan66ae9162018-03-19 18:12:50 -0700151 while (!obudec_read_temporal_unit(&obu_ctx, &buf, &bytes_in_buffer,
Tom Finegan82a41182018-03-30 11:32:37 -0700152 &buffer_size)) {
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000153 aom_codec_iter_t iter = NULL;
154 aom_image_t *img = NULL;
Tom Finegan21f48252018-05-15 11:31:04 -0700155 if (aom_codec_decode(&codec, buf, bytes_in_buffer, NULL))
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000156 die_codec(&codec, "Failed to decode frame.");
157
158 while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
Soo-Chul Han4ea3c5e2018-04-06 10:18:32 -0400159 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);
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400164 if (img->spatial_id == 0) {
Wan-Teh Changb7c5d312018-05-15 14:24:54 -0700165 printf("Writing base layer 0 %d\n", frame_cnt);
Soo-Chul Han4ea3c5e2018-04-06 10:18:32 -0400166 aom_img_write(img_shifted, outfile[0]);
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400167 } else if (img->spatial_id <= (int)(si.number_spatial_layers - 1)) {
Wan-Teh Changb7c5d312018-05-15 14:24:54 -0700168 printf("Writing enhancement layer %d %d\n", img->spatial_id, frame_cnt);
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400169 aom_img_write(img_shifted, outfile[img->spatial_id]);
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000170 } else {
Wan-Teh Changb7c5d312018-05-15 14:24:54 -0700171 die_codec(&codec, "Invalid bitstream. Layer id exceeds layer count");
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000172 }
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400173 if (img->spatial_id == (int)(si.number_spatial_layers - 1)) ++frame_cnt;
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000174 }
175 }
176
177 printf("Processed %d frames.\n", frame_cnt);
178 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
179
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400180 for (i = 0; i < si.number_spatial_layers; i++) fclose(outfile[i]);
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000181
182 fclose(inputfile);
183
184 return EXIT_SUCCESS;
185}