blob: e7c6a7d3ab074b5254d058a077bf032eea232e35 [file] [log] [blame]
Dmitry Kovalev50fa5852014-01-07 15:15:25 -08001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Dmitry Kovalev50fa5852014-01-07 15:15:25 -08003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080010 */
11
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080012// 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// -----------------
Yaowu Xuf883b422016-08-30 14:01:10 -070030// For decoders, you only have to include `aom_decoder.h` and then any
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080031// header files for the specific codecs you use. In this case, we're using
Yaowu Xu9c01aa12016-09-01 14:32:49 -070032// aom.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080033//
34// Initializing The Codec
35// ----------------------
Yaowu Xuf883b422016-08-30 14:01:10 -070036// 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
Tom Fineganc9527aa2014-09-30 11:21:56 -070039// beyond the scope of this example, but the main gist is to open the input file
Yaowu Xuf883b422016-08-30 14:01:10 -070040// and parse just enough of it to determine if it's a AVx file and which AVx
Tom Fineganc9527aa2014-09-30 11:21:56 -070041// codec is contained within the file.
Yaowu Xuf883b422016-08-30 14:01:10 -070042// Note the NULL pointer passed to aom_codec_dec_init(). We do that in this
Tom Fineganc9527aa2014-09-30 11:21:56 -070043// example because we want the algorithm to determine the stream configuration
44// (width/height) and allocate memory automatically.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080045//
46// Decoding A Frame
47// ----------------
48// Once the frame has been read into memory, it is decoded using the
Yaowu Xuf883b422016-08-30 14:01:10 -070049// `aom_codec_decode` function. The call takes a pointer to the data
Tom Fineganc9527aa2014-09-30 11:21:56 -070050// (`frame`) and the length of the data (`frame_size`). No application data
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080051// is associated with the frame in this example, so the `user_priv`
Sean DuBois47cc2552018-01-23 07:44:16 +000052// parameter is NULL.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080053//
54// Codecs may produce a variable number of output frames for every call to
Yaowu Xuf883b422016-08-30 14:01:10 -070055// `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
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080059// 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// -------
Yaowu Xuf883b422016-08-30 14:01:10 -070068// The `aom_codec_destroy` call frees any memory allocated by the codec.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080069//
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
Yaowu Xuf883b422016-08-30 14:01:10 -070074// few exceptions, aom_codec functions return an enumerated error status,
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080075// with the value `0` indicating success.
76
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080077#include <stdio.h>
78#include <stdlib.h>
79#include <string.h>
Dmitry Kovalev7ec27692014-01-27 13:40:29 -080080
Yaowu Xuf883b422016-08-30 14:01:10 -070081#include "aom/aom_decoder.h"
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080082
Tom Finegan9e96bdc2015-02-04 16:11:57 -080083#include "../tools_common.h"
84#include "../video_reader.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070085#include "./aom_config.h"
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080086
Dmitry Kovalev7ec27692014-01-27 13:40:29 -080087static const char *exec_name;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080088
James Zern59e7a472015-05-09 10:33:26 -070089void usage_exit(void) {
Dmitry Kovalev7ec27692014-01-27 13:40:29 -080090 fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name);
91 exit(EXIT_FAILURE);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080092}
93
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080094int main(int argc, char **argv) {
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -080095 int frame_cnt = 0;
96 FILE *outfile = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -070097 aom_codec_ctx_t codec;
98 AvxVideoReader *reader = NULL;
99 const AvxInterface *decoder = NULL;
100 const AvxVideoInfo *info = NULL;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800101
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800102 exec_name = argv[0];
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800103
clang-format397d9642016-08-08 18:51:16 -0700104 if (argc != 3) die("Invalid number of arguments.");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800105
Yaowu Xuf883b422016-08-30 14:01:10 -0700106 reader = aom_video_reader_open(argv[1]);
clang-format397d9642016-08-08 18:51:16 -0700107 if (!reader) die("Failed to open %s for reading.", argv[1]);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800108
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800109 if (!(outfile = fopen(argv[2], "wb")))
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800110 die("Failed to open %s for writing.", argv[2]);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800111
Yaowu Xuf883b422016-08-30 14:01:10 -0700112 info = aom_video_reader_get_info(reader);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800113
Yaowu Xuf883b422016-08-30 14:01:10 -0700114 decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
clang-format397d9642016-08-08 18:51:16 -0700115 if (!decoder) die("Unknown input codec.");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800116
Yaowu Xuf883b422016-08-30 14:01:10 -0700117 printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800118
Yaowu Xuf883b422016-08-30 14:01:10 -0700119 if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800120 die_codec(&codec, "Failed to initialize decoder.");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800121
Yaowu Xuf883b422016-08-30 14:01:10 -0700122 while (aom_video_reader_read_frame(reader)) {
123 aom_codec_iter_t iter = NULL;
124 aom_image_t *img = NULL;
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800125 size_t frame_size = 0;
clang-format397d9642016-08-08 18:51:16 -0700126 const unsigned char *frame =
Yaowu Xuf883b422016-08-30 14:01:10 -0700127 aom_video_reader_get_frame(reader, &frame_size);
Sean DuBois47cc2552018-01-23 07:44:16 +0000128 if (aom_codec_decode(&codec, frame, (unsigned int)frame_size, NULL))
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800129 die_codec(&codec, "Failed to decode frame.");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800130
Yaowu Xuf883b422016-08-30 14:01:10 -0700131 while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
132 aom_img_write(img, outfile);
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800133 ++frame_cnt;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800134 }
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800135 }
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800136
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800137 printf("Processed %d frames.\n", frame_cnt);
Yaowu Xuf883b422016-08-30 14:01:10 -0700138 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800139
140 printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n",
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800141 info->frame_width, info->frame_height, argv[2]);
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800142
Yaowu Xuf883b422016-08-30 14:01:10 -0700143 aom_video_reader_close(reader);
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800144
145 fclose(outfile);
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800146
147 return EXIT_SUCCESS;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800148}