AOMedia Codec SDK
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 
85 static const char *exec_name;
86 
87 void usage_exit(void) {
88  fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
89  exit(EXIT_FAILURE);
90 }
91 
92 int main(int argc, char **argv) {
93  int frame_cnt = 0;
94  FILE *outfile = NULL;
95  aom_codec_ctx_t codec;
96  AvxVideoReader *reader = NULL;
97  const AvxInterface *decoder = NULL;
98  const AvxVideoInfo *info = NULL;
99 
100  exec_name = argv[0];
101 
102  if (argc != 3) die("Invalid number of arguments.");
103 
104  reader = aom_video_reader_open(argv[1]);
105  if (!reader) die("Failed to open %s for reading.", argv[1]);
106 
107  if (!(outfile = fopen(argv[2], "wb")))
108  die("Failed to open %s for writing.", argv[2]);
109 
110  info = aom_video_reader_get_info(reader);
111 
112  decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
113  if (!decoder) die("Unknown input codec.");
114 
115  printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
116 
117  if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
118  die_codec(&codec, "Failed to initialize decoder.");
119 
120  while (aom_video_reader_read_frame(reader)) {
121  aom_codec_iter_t iter = NULL;
122  aom_image_t *img = NULL;
123  size_t frame_size = 0;
124  const unsigned char *frame =
125  aom_video_reader_get_frame(reader, &frame_size);
126  if (aom_codec_decode(&codec, frame, frame_size, NULL))
127  die_codec(&codec, "Failed to decode frame.");
128 
129  while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
130  aom_img_write(img, outfile);
131  ++frame_cnt;
132  }
133  }
134 
135  printf("Processed %d frames.\n", frame_cnt);
136  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
137 
138  printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
139  info->frame_width, info->frame_height, argv[2]);
140 
141  aom_video_reader_close(reader);
142 
143  fclose(outfile);
144 
145  return EXIT_SUCCESS;
146 }
aom_codec_decode
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.
aom_codec_ctx
Codec context structure.
Definition: aom_codec.h:204
aom_codec_iter_t
const typedef void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
aom_codec_iface_name
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_destroy
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_decoder.h
Describes the decoder algorithm interface to applications.
aom_codec_dec_init
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition: aom_decoder.h:135
aom_codec_get_frame
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
aom_image
Image Descriptor.
Definition: aom_image.h:141