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