Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 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 | // ----------------- |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 30 | // For decoders, you only have to include `aom_decoder.h` and then any |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 31 | // header files for the specific codecs you use. In this case, we're using |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 32 | // aom. |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 33 | // |
| 34 | // Initializing The Codec |
| 35 | // ---------------------- |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 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 |
Tom Finegan | c9527aa | 2014-09-30 11:21:56 -0700 | [diff] [blame] | 39 | // beyond the scope of this example, but the main gist is to open the input file |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 40 | // and parse just enough of it to determine if it's a AVx file and which AVx |
Tom Finegan | c9527aa | 2014-09-30 11:21:56 -0700 | [diff] [blame] | 41 | // codec is contained within the file. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 42 | // Note the NULL pointer passed to aom_codec_dec_init(). We do that in this |
Tom Finegan | c9527aa | 2014-09-30 11:21:56 -0700 | [diff] [blame] | 43 | // example because we want the algorithm to determine the stream configuration |
| 44 | // (width/height) and allocate memory automatically. |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 45 | // |
| 46 | // Decoding A Frame |
| 47 | // ---------------- |
| 48 | // Once the frame has been read into memory, it is decoded using the |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 49 | // `aom_codec_decode` function. The call takes a pointer to the data |
Tom Finegan | c9527aa | 2014-09-30 11:21:56 -0700 | [diff] [blame] | 50 | // (`frame`) and the length of the data (`frame_size`). No application data |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 51 | // is associated with the frame in this example, so the `user_priv` |
| 52 | // parameter is NULL. The `deadline` parameter is left at zero for this |
Tom Finegan | c9527aa | 2014-09-30 11:21:56 -0700 | [diff] [blame] | 53 | // example. This parameter is generally only used when doing adaptive post |
| 54 | // processing. |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 55 | // |
| 56 | // Codecs may produce a variable number of output frames for every call to |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 57 | // `aom_codec_decode`. These frames are retrieved by the |
| 58 | // `aom_codec_get_frame` iterator function. The iterator variable `iter` is |
| 59 | // initialized to NULL each time `aom_codec_decode` is called. |
| 60 | // `aom_codec_get_frame` is called in a loop, returning a pointer to a |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 61 | // decoded image or NULL to indicate the end of list. |
| 62 | // |
| 63 | // Processing The Decoded Data |
| 64 | // --------------------------- |
| 65 | // In this example, we simply write the encoded data to disk. It is |
| 66 | // important to honor the image's `stride` values. |
| 67 | // |
| 68 | // Cleanup |
| 69 | // ------- |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 70 | // The `aom_codec_destroy` call frees any memory allocated by the codec. |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 71 | // |
| 72 | // Error Handling |
| 73 | // -------------- |
| 74 | // This example does not special case any error return codes. If there was |
| 75 | // an error, a descriptive message is printed and the program exits. With |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 76 | // few exceptions, aom_codec functions return an enumerated error status, |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 77 | // with the value `0` indicating success. |
| 78 | |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 79 | #include <stdio.h> |
| 80 | #include <stdlib.h> |
| 81 | #include <string.h> |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 82 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 83 | #include "aom/aom_decoder.h" |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 84 | |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 85 | #include "../tools_common.h" |
| 86 | #include "../video_reader.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 87 | #include "./aom_config.h" |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 88 | |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 89 | static const char *exec_name; |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 90 | |
James Zern | 59e7a47 | 2015-05-09 10:33:26 -0700 | [diff] [blame] | 91 | void usage_exit(void) { |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 92 | fprintf(stderr, "Usage: %s <infile> <outfile>\n", exec_name); |
| 93 | exit(EXIT_FAILURE); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 96 | int main(int argc, char **argv) { |
Dmitry Kovalev | 37e6fd3 | 2014-02-05 18:34:46 -0800 | [diff] [blame] | 97 | int frame_cnt = 0; |
| 98 | FILE *outfile = NULL; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 99 | aom_codec_ctx_t codec; |
| 100 | AvxVideoReader *reader = NULL; |
| 101 | const AvxInterface *decoder = NULL; |
| 102 | const AvxVideoInfo *info = NULL; |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 103 | |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 104 | exec_name = argv[0]; |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 105 | |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 106 | if (argc != 3) die("Invalid number of arguments."); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 107 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 108 | reader = aom_video_reader_open(argv[1]); |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 109 | if (!reader) die("Failed to open %s for reading.", argv[1]); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 110 | |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 111 | if (!(outfile = fopen(argv[2], "wb"))) |
Dmitry Kovalev | 37e6fd3 | 2014-02-05 18:34:46 -0800 | [diff] [blame] | 112 | die("Failed to open %s for writing.", argv[2]); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 113 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 114 | info = aom_video_reader_get_info(reader); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 115 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 116 | decoder = get_aom_decoder_by_fourcc(info->codec_fourcc); |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 117 | if (!decoder) die("Unknown input codec."); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 118 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 119 | printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface())); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 120 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 121 | if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0)) |
Dmitry Kovalev | 37e6fd3 | 2014-02-05 18:34:46 -0800 | [diff] [blame] | 122 | die_codec(&codec, "Failed to initialize decoder."); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 123 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 124 | while (aom_video_reader_read_frame(reader)) { |
| 125 | aom_codec_iter_t iter = NULL; |
| 126 | aom_image_t *img = NULL; |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 127 | size_t frame_size = 0; |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 128 | const unsigned char *frame = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 129 | aom_video_reader_get_frame(reader, &frame_size); |
| 130 | if (aom_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0)) |
Dmitry Kovalev | 37e6fd3 | 2014-02-05 18:34:46 -0800 | [diff] [blame] | 131 | die_codec(&codec, "Failed to decode frame."); |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 132 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 133 | while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) { |
| 134 | aom_img_write(img, outfile); |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 135 | ++frame_cnt; |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 136 | } |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 137 | } |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 138 | |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 139 | printf("Processed %d frames.\n", frame_cnt); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 140 | if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 141 | |
| 142 | printf("Play: ffplay -f rawvideo -pix_fmt yuv420p -s %dx%d %s\n", |
Dmitry Kovalev | 37e6fd3 | 2014-02-05 18:34:46 -0800 | [diff] [blame] | 143 | info->frame_width, info->frame_height, argv[2]); |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 144 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 145 | aom_video_reader_close(reader); |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 146 | |
| 147 | fclose(outfile); |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 148 | |
| 149 | return EXIT_SUCCESS; |
Dmitry Kovalev | 50fa585 | 2014-01-07 15:15:25 -0800 | [diff] [blame] | 150 | } |