blob: e05153f104a02121c76286bdb4530ce6876381f8 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
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.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010 */
11
John Koleszarda584362013-05-06 15:52:06 -070012#include <assert.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040013#include <stdio.h>
14#include <stdlib.h>
15#include <stdarg.h>
16#include <string.h>
John Koleszar933d44b2010-10-21 20:40:42 -070017#include <limits.h>
Tero Rintaluoma11a222f2011-01-24 11:21:40 +020018
Yaowu Xuf883b422016-08-30 14:01:10 -070019#include "./aom_config.h"
James Zern1e681d92014-08-15 21:00:09 -070020
James Zernb9b28382016-12-09 16:50:39 -080021#if CONFIG_OS_SUPPORT
22#if HAVE_UNISTD_H
23#include <unistd.h> // NOLINT
24#elif !defined(STDOUT_FILENO)
25#define STDOUT_FILENO 1
26#endif
27#endif
28
James Zern1e681d92014-08-15 21:00:09 -070029#if CONFIG_LIBYUV
Tom Finegan2abe2d42013-11-18 14:39:51 -080030#include "third_party/libyuv/include/libyuv/scale.h"
James Zern1e681d92014-08-15 21:00:09 -070031#endif
Tom Finegan2abe2d42013-11-18 14:39:51 -080032
33#include "./args.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080034#include "./ivfdec.h"
35
Yaowu Xuf883b422016-08-30 14:01:10 -070036#include "aom/aom_decoder.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070037#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070038#include "aom_ports/aom_timer.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080039
Yaowu Xuf883b422016-08-30 14:01:10 -070040#if CONFIG_AV1_DECODER
41#include "aom/aomdx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040042#endif
Tom Finegan00a35aa2013-11-14 12:37:42 -080043
Tom Finegan2abe2d42013-11-18 14:39:51 -080044#include "./md5_utils.h"
Cyril Concolato6c788832017-10-30 16:30:35 -070045#include "./obudec.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080046#include "./tools_common.h"
Tom Finegan61b71ab2018-03-22 15:58:38 -070047
James Zernb6cce762014-05-10 17:44:12 -070048#if CONFIG_WEBM_IO
Tom Finegan2abe2d42013-11-18 14:39:51 -080049#include "./webmdec.h"
James Zernb6cce762014-05-10 17:44:12 -070050#endif
Cyril Concolato6c788832017-10-30 16:30:35 -070051
Dmitry Kovalev5ab635832014-01-17 17:02:37 -080052#include "./y4menc.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040053
54static const char *exec_name;
55
Yaowu Xuf883b422016-08-30 14:01:10 -070056struct AvxDecInputContext {
57 struct AvxInputContext *aom_input_ctx;
Tom Finegan66ae9162018-03-19 18:12:50 -070058 struct ObuDecInputContext *obu_ctx;
Tom Finegan2abe2d42013-11-18 14:39:51 -080059 struct WebmInputContext *webm_ctx;
60};
61
James Zernfd8c78c2017-11-27 16:37:33 -080062static const arg_def_t help =
63 ARG_DEF(NULL, "help", 0, "Show usage options and exit");
clang-format6c4d83e2016-08-08 19:03:30 -070064static const arg_def_t looparg =
65 ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
66static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
67static const arg_def_t use_yv12 =
68 ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
69static const arg_def_t use_i420 =
70 ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
71static const arg_def_t flipuvarg =
72 ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
73static const arg_def_t rawvideo =
74 ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
75static const arg_def_t noblitarg =
76 ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
77static const arg_def_t progressarg =
78 ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
79static const arg_def_t limitarg =
80 ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
81static const arg_def_t skiparg =
82 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
83static const arg_def_t postprocarg =
84 ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
85static const arg_def_t summaryarg =
86 ARG_DEF(NULL, "summary", 0, "Show timing summary");
87static const arg_def_t outputfile =
88 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
89static const arg_def_t threadsarg =
90 ARG_DEF("t", "threads", 1, "Max threads to use");
clang-format6c4d83e2016-08-08 19:03:30 -070091static const arg_def_t verbosearg =
92 ARG_DEF("v", "verbose", 0, "Show version string");
clang-format6c4d83e2016-08-08 19:03:30 -070093static const arg_def_t scalearg =
94 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
95static const arg_def_t continuearg =
96 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
97static const arg_def_t fb_arg =
98 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
99static const arg_def_t md5arg =
100 ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
Peter Boströma1f64322017-01-19 11:35:30 -0500101static const arg_def_t framestatsarg =
102 ARG_DEF(NULL, "framestats", 1, "Output per-frame stats (.csv format)");
clang-format6c4d83e2016-08-08 19:03:30 -0700103static const arg_def_t outbitdeptharg =
104 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800105static const arg_def_t tilem = ARG_DEF(NULL, "tile-mode", 1,
106 "Tile coding mode "
107 "(0 for normal tile coding mode)");
clang-format6c4d83e2016-08-08 19:03:30 -0700108static const arg_def_t tiler = ARG_DEF(NULL, "tile-row", 1,
109 "Row index of tile to decode "
110 "(-1 for all rows)");
111static const arg_def_t tilec = ARG_DEF(NULL, "tile-column", 1,
112 "Column index of tile to decode "
113 "(-1 for all columns)");
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400114static const arg_def_t isannexb =
115 ARG_DEF(NULL, "annexb", 0, "Bitstream is in Annex-B format");
Dmitry Kovalevd24f4e42013-12-27 16:10:18 -0800116
Yaowu Xud3e7c682017-12-21 14:08:25 -0800117static const arg_def_t *all_args[] = {
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400118 &help, &codecarg, &use_yv12, &use_i420,
119 &flipuvarg, &rawvideo, &noblitarg, &progressarg,
120 &limitarg, &skiparg, &postprocarg, &summaryarg,
121 &outputfile, &threadsarg, &verbosearg, &scalearg,
122 &fb_arg, &md5arg, &framestatsarg, &continuearg,
123 &outbitdeptharg, &tilem, &tiler, &tilec,
124 &isannexb, NULL
Yaowu Xud3e7c682017-12-21 14:08:25 -0800125};
John Koleszar0ea50ce2010-05-18 11:58:33 -0400126
James Zern1e681d92014-08-15 21:00:09 -0700127#if CONFIG_LIBYUV
Yaowu Xuf883b422016-08-30 14:01:10 -0700128static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
clang-format6c4d83e2016-08-08 19:03:30 -0700129 FilterModeEnum mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700130 if (src->fmt == AOM_IMG_FMT_I42016) {
131 assert(dst->fmt == AOM_IMG_FMT_I42016);
clang-format6c4d83e2016-08-08 19:03:30 -0700132 return I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -0700133 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
134 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
135 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
136 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
137 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
138 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
139 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700140 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700141 assert(src->fmt == AOM_IMG_FMT_I420);
142 assert(dst->fmt == AOM_IMG_FMT_I420);
143 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
144 src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
145 src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
146 src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
147 dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
148 dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -0700149 dst->d_h, mode);
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800150}
James Zern1e681d92014-08-15 21:00:09 -0700151#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800152
James Zernfd8c78c2017-11-27 16:37:33 -0800153void show_help(FILE *fout, int shorthelp) {
154 fprintf(fout, "Usage: %s <options> filename\n\n", exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400155
James Zernfd8c78c2017-11-27 16:37:33 -0800156 if (shorthelp) {
157 fprintf(fout, "Use --help to see the full list of options.\n");
158 return;
159 }
160
161 fprintf(fout, "Options:\n");
162 arg_show_usage(fout, all_args);
163 fprintf(fout,
John Koleszarc6b90392012-07-13 15:21:29 -0700164 "\nOutput File Patterns:\n\n"
165 " The -o argument specifies the name of the file(s) to "
166 "write to. If the\n argument does not include any escape "
167 "characters, the output will be\n written to a single file. "
168 "Otherwise, the filename will be calculated by\n expanding "
John Koleszar3707c342012-11-06 10:09:36 -0800169 "the following escape characters:\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800170 fprintf(fout,
John Koleszarc6b90392012-07-13 15:21:29 -0700171 "\n\t%%w - Frame width"
172 "\n\t%%h - Frame height"
173 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
174 "\n\n Pattern arguments are only supported in conjunction "
175 "with the --yv12 and\n --i420 options. If the -o option is "
clang-format6c4d83e2016-08-08 19:03:30 -0700176 "not specified, the output will be\n directed to stdout.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800177 fprintf(fout, "\nIncluded decoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400178
James Zernfd8c78c2017-11-27 16:37:33 -0800179 for (int i = 0; i < get_aom_decoder_count(); ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700180 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
James Zernfd8c78c2017-11-27 16:37:33 -0800181 fprintf(fout, " %-6s - %s\n", decoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700182 aom_codec_iface_name(decoder->codec_interface()));
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800183 }
James Zernfd8c78c2017-11-27 16:37:33 -0800184}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400185
James Zernfd8c78c2017-11-27 16:37:33 -0800186void usage_exit(void) {
187 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700188 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400189}
190
clang-format6c4d83e2016-08-08 19:03:30 -0700191static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
192 size_t *buffer_size) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800193 char raw_hdr[RAW_FRAME_HDR_SZ];
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800194 size_t frame_size = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700195
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800196 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
clang-format6c4d83e2016-08-08 19:03:30 -0700197 if (!feof(infile)) warn("Failed to read RAW frame size\n");
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800198 } else {
James Zern75d7ee32014-02-04 20:06:28 -0800199 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
200 const size_t kFrameTooSmallThreshold = 256 * 1024;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800201 frame_size = mem_get_le32(raw_hdr);
John Koleszarc6b90392012-07-13 15:21:29 -0700202
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800203 if (frame_size > kCorruptFrameThreshold) {
204 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
205 frame_size = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400206 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400207
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800208 if (frame_size < kFrameTooSmallThreshold) {
209 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
210 (unsigned int)frame_size);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400211 }
212
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800213 if (frame_size > *buffer_size) {
214 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
215 if (new_buf) {
216 *buffer = new_buf;
217 *buffer_size = 2 * frame_size;
218 } else {
219 warn("Failed to allocate compressed data buffer\n");
220 frame_size = 0;
221 }
222 }
John Koleszarc6b90392012-07-13 15:21:29 -0700223 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400224
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800225 if (!feof(infile)) {
226 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
227 warn("Failed to read full frame\n");
228 return 1;
229 }
230 *bytes_read = frame_size;
231 }
232
233 return 0;
234}
235
Yaowu Xuf883b422016-08-30 14:01:10 -0700236static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800237 size_t *bytes_in_buffer, size_t *buffer_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700238 switch (input->aom_input_ctx->file_type) {
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700239#if CONFIG_WEBM_IO
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800240 case FILE_TYPE_WEBM:
Tom Finegan707b5752018-03-29 15:41:08 -0700241 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer,
242 buffer_size);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700243#endif
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800244 case FILE_TYPE_RAW:
Yaowu Xuf883b422016-08-30 14:01:10 -0700245 return raw_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700246 buffer_size);
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800247 case FILE_TYPE_IVF:
Yaowu Xuf883b422016-08-30 14:01:10 -0700248 return ivf_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700249 buffer_size);
Cyril Concolato6c788832017-10-30 16:30:35 -0700250 case FILE_TYPE_OBU:
Yaowu Xu7f313c02018-03-28 12:50:16 -0700251 return obudec_read_temporal_unit(input->obu_ctx, buf, bytes_in_buffer,
Tom Finegan82a41182018-03-30 11:32:37 -0700252 buffer_size);
clang-format6c4d83e2016-08-08 19:03:30 -0700253 default: return 1;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800254 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400255}
256
Yaowu Xuf883b422016-08-30 14:01:10 -0700257static void update_image_md5(const aom_image_t *img, const int planes[3],
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800258 MD5Context *md5) {
259 int i, y;
260
261 for (i = 0; i < 3; ++i) {
262 const int plane = planes[i];
263 const unsigned char *buf = img->planes[plane];
264 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700265 const int w = aom_img_plane_width(img, plane) *
266 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
267 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800268
269 for (y = 0; y < h; ++y) {
270 MD5Update(md5, buf, w);
271 buf += stride;
272 }
273 }
274}
275
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000276static void write_image_file(const aom_image_t *img, const int *planes,
277 const int num_planes, FILE *file) {
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800278 int i, y;
Yaowu Xuf883b422016-08-30 14:01:10 -0700279 const int bytes_per_sample = ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800280
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000281 for (i = 0; i < num_planes; ++i) {
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800282 const int plane = planes[i];
283 const unsigned char *buf = img->planes[plane];
284 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700285 const int w = aom_img_plane_width(img, plane);
286 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800287
288 for (y = 0; y < h; ++y) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700289 fwrite(buf, bytes_per_sample, w, file);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800290 buf += stride;
291 }
John Koleszarc6b90392012-07-13 15:21:29 -0700292 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400293}
294
Yaowu Xuf883b422016-08-30 14:01:10 -0700295static int file_is_raw(struct AvxInputContext *input) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800296 uint8_t buf[32];
John Koleszarc6b90392012-07-13 15:21:29 -0700297 int is_raw = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700298 aom_codec_stream_info_t si;
John Koleszarcfe3f912010-10-20 10:49:12 -0400299
Tom Finegan00a35aa2013-11-14 12:37:42 -0800300 if (fread(buf, 1, 32, input->file) == 32) {
John Koleszarc6b90392012-07-13 15:21:29 -0700301 int i;
John Koleszarcfe3f912010-10-20 10:49:12 -0400302
Tom Finegan00a35aa2013-11-14 12:37:42 -0800303 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700304 for (i = 0; i < get_aom_decoder_count(); ++i) {
305 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
306 if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
clang-format6c4d83e2016-08-08 19:03:30 -0700307 32 - 4, &si)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700308 is_raw = 1;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800309 input->fourcc = decoder->fourcc;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800310 input->width = si.w;
311 input->height = si.h;
312 input->framerate.numerator = 30;
313 input->framerate.denominator = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700314 break;
315 }
Tom Finegan00a35aa2013-11-14 12:37:42 -0800316 }
317 }
John Koleszarc6b90392012-07-13 15:21:29 -0700318 }
John Koleszarcfe3f912010-10-20 10:49:12 -0400319
Tom Finegan00a35aa2013-11-14 12:37:42 -0800320 rewind(input->file);
John Koleszarc6b90392012-07-13 15:21:29 -0700321 return is_raw;
John Koleszarcfe3f912010-10-20 10:49:12 -0400322}
323
James Zern6a2e0f02015-05-09 10:42:31 -0700324static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
James Yu6d151322014-02-22 14:51:11 +0800325 fprintf(stderr,
clang-format6c4d83e2016-08-08 19:03:30 -0700326 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
John Koleszarc6b90392012-07-13 15:21:29 -0700327 frame_in, frame_out, dx_time,
James Yu6d151322014-02-22 14:51:11 +0800328 (double)frame_out * 1000000.0 / (double)dx_time);
John Koleszar5d12e042010-10-21 17:28:34 -0400329}
330
Frank Galligana4f30a52014-02-06 17:13:08 -0800331struct ExternalFrameBuffer {
clang-format6c4d83e2016-08-08 19:03:30 -0700332 uint8_t *data;
Frank Galligana4f30a52014-02-06 17:13:08 -0800333 size_t size;
334 int in_use;
335};
336
337struct ExternalFrameBufferList {
338 int num_external_frame_buffers;
339 struct ExternalFrameBuffer *ext_fb;
340};
341
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342// Callback used by libaom to request an external frame buffer. |cb_priv|
Frank Galligana4f30a52014-02-06 17:13:08 -0800343// Application private data passed into the set function. |min_size| is the
344// minimum size in bytes needed to decode the next frame. |fb| pointer to the
345// frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700346static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
347 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800348 int i;
349 struct ExternalFrameBufferList *const ext_fb_list =
350 (struct ExternalFrameBufferList *)cb_priv;
clang-format6c4d83e2016-08-08 19:03:30 -0700351 if (ext_fb_list == NULL) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800352
353 // Find a free frame buffer.
354 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
clang-format6c4d83e2016-08-08 19:03:30 -0700355 if (!ext_fb_list->ext_fb[i].in_use) break;
Frank Galligana4f30a52014-02-06 17:13:08 -0800356 }
357
clang-format6c4d83e2016-08-08 19:03:30 -0700358 if (i == ext_fb_list->num_external_frame_buffers) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800359
360 if (ext_fb_list->ext_fb[i].size < min_size) {
361 free(ext_fb_list->ext_fb[i].data);
Frank Galligan175d9df2014-09-11 08:39:38 -0700362 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
clang-format6c4d83e2016-08-08 19:03:30 -0700363 if (!ext_fb_list->ext_fb[i].data) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800364
365 ext_fb_list->ext_fb[i].size = min_size;
366 }
367
368 fb->data = ext_fb_list->ext_fb[i].data;
369 fb->size = ext_fb_list->ext_fb[i].size;
370 ext_fb_list->ext_fb[i].in_use = 1;
371
372 // Set the frame buffer's private data to point at the external frame buffer.
373 fb->priv = &ext_fb_list->ext_fb[i];
374 return 0;
375}
376
Yaowu Xuc27fc142016-08-22 16:08:15 -0700377// Callback used by libaom when there are no references to the frame buffer.
Frank Galligana4f30a52014-02-06 17:13:08 -0800378// |cb_priv| user private data passed into the set function. |fb| pointer
379// to the frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700380static int release_av1_frame_buffer(void *cb_priv,
381 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800382 struct ExternalFrameBuffer *const ext_fb =
383 (struct ExternalFrameBuffer *)fb->priv;
384 (void)cb_priv;
385 ext_fb->in_use = 0;
386 return 0;
387}
388
James Zern6a2e0f02015-05-09 10:42:31 -0700389static void generate_filename(const char *pattern, char *out, size_t q_len,
390 unsigned int d_w, unsigned int d_h,
391 unsigned int frame_in) {
John Koleszarc6b90392012-07-13 15:21:29 -0700392 const char *p = pattern;
393 char *q = out;
John Koleszar933d44b2010-10-21 20:40:42 -0700394
John Koleszarc6b90392012-07-13 15:21:29 -0700395 do {
396 char *next_pat = strchr(p, '%');
John Koleszar933d44b2010-10-21 20:40:42 -0700397
John Koleszarc6b90392012-07-13 15:21:29 -0700398 if (p == next_pat) {
399 size_t pat_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700400
John Koleszar3707c342012-11-06 10:09:36 -0800401 /* parse the pattern */
John Koleszarc6b90392012-07-13 15:21:29 -0700402 q[q_len - 1] = '\0';
403 switch (p[1]) {
clang-format6c4d83e2016-08-08 19:03:30 -0700404 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
405 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
406 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
407 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
408 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
409 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
410 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
411 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
412 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
413 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
414 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
415 default: die("Unrecognized pattern %%%c\n", p[1]); break;
John Koleszarc6b90392012-07-13 15:21:29 -0700416 }
John Koleszar933d44b2010-10-21 20:40:42 -0700417
John Koleszarc6b90392012-07-13 15:21:29 -0700418 pat_len = strlen(q);
clang-format6c4d83e2016-08-08 19:03:30 -0700419 if (pat_len >= q_len - 1) die("Output filename too long.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700420 q += pat_len;
421 p += 2;
422 q_len -= pat_len;
423 } else {
424 size_t copy_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700425
John Koleszar3707c342012-11-06 10:09:36 -0800426 /* copy the next segment */
John Koleszarc6b90392012-07-13 15:21:29 -0700427 if (!next_pat)
428 copy_len = strlen(p);
429 else
430 copy_len = next_pat - p;
John Koleszar933d44b2010-10-21 20:40:42 -0700431
clang-format6c4d83e2016-08-08 19:03:30 -0700432 if (copy_len >= q_len - 1) die("Output filename too long.\n");
John Koleszar933d44b2010-10-21 20:40:42 -0700433
John Koleszarc6b90392012-07-13 15:21:29 -0700434 memcpy(q, p, copy_len);
435 q[copy_len] = '\0';
436 q += copy_len;
437 p += copy_len;
438 q_len -= copy_len;
439 }
440 } while (*p);
John Koleszar933d44b2010-10-21 20:40:42 -0700441}
442
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800443static int is_single_file(const char *outfile_pattern) {
444 const char *p = outfile_pattern;
445
446 do {
447 p = strchr(p, '%');
448 if (p && p[1] >= '1' && p[1] <= '9')
449 return 0; // pattern contains sequence number, so it's not unique
clang-format6c4d83e2016-08-08 19:03:30 -0700450 if (p) p++;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800451 } while (p);
452
453 return 1;
454}
455
456static void print_md5(unsigned char digest[16], const char *filename) {
457 int i;
458
clang-format6c4d83e2016-08-08 19:03:30 -0700459 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800460 printf(" %s\n", filename);
461}
462
463static FILE *open_outfile(const char *name) {
464 if (strcmp("-", name) == 0) {
465 set_binary_mode(stdout);
466 return stdout;
467 } else {
468 FILE *file = fopen(name, "wb");
clang-format6c4d83e2016-08-08 19:03:30 -0700469 if (!file) fatal("Failed to open output file '%s'", name);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800470 return file;
471 }
472}
473
Yaowu Xuf883b422016-08-30 14:01:10 -0700474static int img_shifted_realloc_required(const aom_image_t *img,
475 const aom_image_t *shifted,
476 aom_img_fmt_t required_fmt) {
clang-format6c4d83e2016-08-08 19:03:30 -0700477 return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700478 required_fmt != shifted->fmt;
479}
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700480
James Zern6a2e0f02015-05-09 10:42:31 -0700481static int main_loop(int argc, const char **argv_) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700482 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700483 char *fn = NULL;
484 int i;
James Zern360e4442016-11-16 21:53:55 -0800485 int ret = EXIT_FAILURE;
clang-format6c4d83e2016-08-08 19:03:30 -0700486 uint8_t *buf = NULL;
487 size_t bytes_in_buffer = 0, buffer_size = 0;
488 FILE *infile;
489 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800490 int do_md5 = 0, progress = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700491 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
492 int arg_skip = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700493 int keep_going = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700494 const AvxInterface *interface = NULL;
495 const AvxInterface *fourcc_interface = NULL;
James Yu6d151322014-02-22 14:51:11 +0800496 uint64_t dx_time = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700497 struct arg arg;
498 char **argv, **argi, **argj;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800499
clang-format6c4d83e2016-08-08 19:03:30 -0700500 int single_file;
501 int use_y4m = 1;
502 int opt_yv12 = 0;
503 int opt_i420 = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800504 int opt_raw = 0;
Debargha Mukherjee2ccf4b92018-02-27 17:30:46 -0800505 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, { 1 } };
clang-format6c4d83e2016-08-08 19:03:30 -0700506 unsigned int output_bit_depth = 0;
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800507 unsigned int tile_mode = 0;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400508 unsigned int is_annexb = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700509 int tile_row = -1;
510 int tile_col = -1;
clang-format6c4d83e2016-08-08 19:03:30 -0700511 int frames_corrupted = 0;
512 int dec_flags = 0;
513 int do_scale = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700514 aom_image_t *scaled_img = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700515 aom_image_t *img_shifted = NULL;
clang-format6c4d83e2016-08-08 19:03:30 -0700516 int frame_avail, got_data, flush_decoder = 0;
517 int num_external_frame_buffers = 0;
518 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400519
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800520 const char *outfile_pattern = NULL;
clang-format6c4d83e2016-08-08 19:03:30 -0700521 char outfile_name[PATH_MAX] = { 0 };
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800522 FILE *outfile = NULL;
523
Peter Boströma1f64322017-01-19 11:35:30 -0500524 FILE *framestats_file = NULL;
525
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800526 MD5Context md5_ctx;
527 unsigned char md5_digest[16];
528
Tom Finegan66ae9162018-03-19 18:12:50 -0700529 struct AvxDecInputContext input = { NULL, NULL, NULL };
Yaowu Xuf883b422016-08-30 14:01:10 -0700530 struct AvxInputContext aom_input_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700531#if CONFIG_WEBM_IO
Jim Bankoski7ae32692014-08-19 06:44:10 -0700532 struct WebmInputContext webm_ctx;
Tom Finegan66ae9162018-03-19 18:12:50 -0700533 memset(&webm_ctx, 0, sizeof(webm_ctx));
Tom Finegan2abe2d42013-11-18 14:39:51 -0800534 input.webm_ctx = &webm_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700535#endif
Tom Finegan82a41182018-03-30 11:32:37 -0700536 struct ObuDecInputContext obu_ctx = { NULL, NULL, 0, 0, 0, 0 };
Tom Finegan66ae9162018-03-19 18:12:50 -0700537 obu_ctx.avx_ctx = &aom_input_ctx;
538 input.obu_ctx = &obu_ctx;
Yaowu Xuf883b422016-08-30 14:01:10 -0700539 input.aom_input_ctx = &aom_input_ctx;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800540
John Koleszarc6b90392012-07-13 15:21:29 -0700541 /* Parse command line */
542 exec_name = argv_[0];
543 argv = argv_dup(argc - 1, argv_ + 1);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400544
John Koleszarc6b90392012-07-13 15:21:29 -0700545 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
546 memset(&arg, 0, sizeof(arg));
547 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400548
James Zernfd8c78c2017-11-27 16:37:33 -0800549 if (arg_match(&arg, &help, argi)) {
550 show_help(stdout, 0);
551 exit(EXIT_SUCCESS);
552 } else if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700553 interface = get_aom_decoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800554 if (!interface)
555 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszar9596a4c2013-06-12 16:08:35 -0700556 } else if (arg_match(&arg, &looparg, argi)) {
557 // no-op
Peter Boströma1f64322017-01-19 11:35:30 -0500558 } else if (arg_match(&arg, &outputfile, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700559 outfile_pattern = arg.val;
Peter Boströma1f64322017-01-19 11:35:30 -0500560 } else if (arg_match(&arg, &use_yv12, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700561 use_y4m = 0;
562 flipuv = 1;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700563 opt_yv12 = 1;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800564 opt_i420 = 0;
565 opt_raw = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700566 } else if (arg_match(&arg, &use_i420, argi)) {
567 use_y4m = 0;
568 flipuv = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800569 opt_yv12 = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700570 opt_i420 = 1;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800571 opt_raw = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700572 } else if (arg_match(&arg, &rawvideo, argi)) {
573 use_y4m = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800574 opt_yv12 = 0;
575 opt_i420 = 0;
576 opt_raw = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500577 } else if (arg_match(&arg, &flipuvarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700578 flipuv = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500579 } else if (arg_match(&arg, &noblitarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700580 noblit = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500581 } else if (arg_match(&arg, &progressarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700582 progress = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500583 } else if (arg_match(&arg, &limitarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700584 stop_after = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500585 } else if (arg_match(&arg, &skiparg, argi)) {
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700586 arg_skip = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500587 } else if (arg_match(&arg, &postprocarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700588 postproc = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500589 } else if (arg_match(&arg, &md5arg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700590 do_md5 = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500591 } else if (arg_match(&arg, &framestatsarg, argi)) {
592 framestats_file = fopen(arg.val, "w");
593 if (!framestats_file) {
594 die("Error: Could not open --framestats file (%s) for writing.\n",
595 arg.val);
596 }
597 } else if (arg_match(&arg, &summaryarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700598 summary = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500599 } else if (arg_match(&arg, &threadsarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700600 cfg.threads = arg_parse_uint(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800601 } else if (arg_match(&arg, &verbosearg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700602 quiet = 0;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800603 } else if (arg_match(&arg, &scalearg, argi)) {
John Koleszar7d8fc262013-02-25 16:58:47 -0800604 do_scale = 1;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800605 } else if (arg_match(&arg, &fb_arg, argi)) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800606 num_external_frame_buffers = arg_parse_uint(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800607 } else if (arg_match(&arg, &continuearg, argi)) {
James Zern7690d882014-08-22 18:01:28 -0700608 keep_going = 1;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800609 } else if (arg_match(&arg, &outbitdeptharg, argi)) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700610 output_bit_depth = arg_parse_uint(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800611 } else if (arg_match(&arg, &tilem, argi)) {
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800612 tile_mode = arg_parse_int(&arg);
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400613 } else if (arg_match(&arg, &isannexb, argi)) {
614 is_annexb = 1;
615 input.obu_ctx->is_annexb = 1;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800616 } else if (arg_match(&arg, &tiler, argi)) {
Geza Lore490ba1a2016-03-11 17:42:49 +0000617 tile_row = arg_parse_int(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800618 } else if (arg_match(&arg, &tilec, argi)) {
Geza Lore490ba1a2016-03-11 17:42:49 +0000619 tile_col = arg_parse_int(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800620 } else {
John Koleszarc6b90392012-07-13 15:21:29 -0700621 argj++;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800622 }
John Koleszarc6b90392012-07-13 15:21:29 -0700623 }
624
625 /* Check for unrecognized options */
626 for (argi = argv; *argi; argi++)
627 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
628 die("Error: Unrecognized option %s\n", *argi);
629
630 /* Handle non-option arguments */
631 fn = argv[0];
632
Hanno Böckb5d0a202015-02-24 15:48:21 -0800633 if (!fn) {
634 free(argv);
James Zernfd8c78c2017-11-27 16:37:33 -0800635 fprintf(stderr, "No input file specified!\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700636 usage_exit();
Hanno Böckb5d0a202015-02-24 15:48:21 -0800637 }
John Koleszarc6b90392012-07-13 15:21:29 -0700638 /* Open file */
639 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
640
641 if (!infile) {
James Zern6b993f02015-02-05 21:06:29 -0800642 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
John Koleszarc6b90392012-07-13 15:21:29 -0700643 }
644#if CONFIG_OS_SUPPORT
645 /* Make sure we don't dump to the terminal, unless forced to with -o - */
James Zernb9b28382016-12-09 16:50:39 -0800646 if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700647 fprintf(stderr,
648 "Not dumping raw video to your terminal. Use '-o -' to "
649 "override.\n");
650 return EXIT_FAILURE;
651 }
652#endif
Tom Fineganccb8cc42018-02-22 14:08:13 -0800653 input.aom_input_ctx->filename = fn;
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 input.aom_input_ctx->file = infile;
655 if (file_is_ivf(input.aom_input_ctx))
656 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700657#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700658 else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
659 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700660#endif
Tom Finegan66ae9162018-03-19 18:12:50 -0700661 else if (file_is_obu(&obu_ctx))
Cyril Concolato6c788832017-10-30 16:30:35 -0700662 input.aom_input_ctx->file_type = FILE_TYPE_OBU;
Yaowu Xuf883b422016-08-30 14:01:10 -0700663 else if (file_is_raw(input.aom_input_ctx))
664 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700665 else {
666 fprintf(stderr, "Unrecognized input file type.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700667#if !CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700668 fprintf(stderr, "aomdec was built without WebM container support.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700669#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700670 return EXIT_FAILURE;
671 }
672
John Koleszarc6b90392012-07-13 15:21:29 -0700673 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800674 single_file = is_single_file(outfile_pattern);
John Koleszarc6b90392012-07-13 15:21:29 -0700675
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800676 if (!noblit && single_file) {
677 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
Yaowu Xuf883b422016-08-30 14:01:10 -0700678 aom_input_ctx.width, aom_input_ctx.height, 0);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800679 if (do_md5)
680 MD5Init(&md5_ctx);
681 else
682 outfile = open_outfile(outfile_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700683 }
684
685 if (use_y4m && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700686 if (!single_file) {
clang-format6c4d83e2016-08-08 19:03:30 -0700687 fprintf(stderr,
688 "YUV4MPEG2 not supported with output patterns,"
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700689 " try --i420 or --yv12 or --rawvideo.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700690 return EXIT_FAILURE;
691 }
692
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700693#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700694 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
695 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
clang-format6c4d83e2016-08-08 19:03:30 -0700696 fprintf(stderr,
697 "Failed to guess framerate -- error parsing "
John Koleszarc6b90392012-07-13 15:21:29 -0700698 "webm file?\n");
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400699 return EXIT_FAILURE;
John Koleszarc6b90392012-07-13 15:21:29 -0700700 }
Dmitry Kovalev5ab635832014-01-17 17:02:37 -0800701 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700702#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700703 }
704
Yaowu Xuf883b422016-08-30 14:01:10 -0700705 fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800706 if (interface && fourcc_interface && interface != fourcc_interface)
707 warn("Header indicates codec: %s\n", fourcc_interface->name);
708 else
709 interface = fourcc_interface;
John Koleszarc6b90392012-07-13 15:21:29 -0700710
Yaowu Xuf883b422016-08-30 14:01:10 -0700711 if (!interface) interface = get_aom_decoder_by_index(0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400712
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800713 dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0);
Yaowu Xuf883b422016-08-30 14:01:10 -0700714 if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
clang-format6c4d83e2016-08-08 19:03:30 -0700715 dec_flags)) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800716 fprintf(stderr, "Failed to initialize decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700717 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800718 goto fail2;
John Koleszarc6b90392012-07-13 15:21:29 -0700719 }
John Koleszar933d44b2010-10-21 20:40:42 -0700720
clang-format6c4d83e2016-08-08 19:03:30 -0700721 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400722
Yaowu Xudd6ef6c2018-03-02 16:43:52 -0800723#if CONFIG_AV1_DECODER
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800724 if (aom_codec_control(&decoder, AV1_SET_TILE_MODE, tile_mode)) {
725 fprintf(stderr, "Failed to set decode_tile_mode: %s\n",
726 aom_codec_error(&decoder));
727 goto fail;
728 }
729
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400730 if (aom_codec_control(&decoder, AV1D_SET_IS_ANNEXB, is_annexb)) {
731 fprintf(stderr, "Failed to set is_annexb: %s\n", aom_codec_error(&decoder));
732 goto fail;
733 }
734
Jingning Hanf79d48d2016-12-20 14:52:35 -0800735 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_ROW, tile_row)) {
736 fprintf(stderr, "Failed to set decode_tile_row: %s\n",
737 aom_codec_error(&decoder));
738 goto fail;
739 }
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700740
Jingning Hanf79d48d2016-12-20 14:52:35 -0800741 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_COL, tile_col)) {
742 fprintf(stderr, "Failed to set decode_tile_col: %s\n",
743 aom_codec_error(&decoder));
744 goto fail;
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700745 }
746#endif
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700747
clang-format6c4d83e2016-08-08 19:03:30 -0700748 if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700749 while (arg_skip) {
clang-format6c4d83e2016-08-08 19:03:30 -0700750 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700751 arg_skip--;
752 }
753
Frank Galligana4f30a52014-02-06 17:13:08 -0800754 if (num_external_frame_buffers > 0) {
755 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
756 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
757 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
Yaowu Xuf883b422016-08-30 14:01:10 -0700758 if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
759 release_av1_frame_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700760 &ext_fb_list)) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800761 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700762 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800763 goto fail;
Frank Galligana4f30a52014-02-06 17:13:08 -0800764 }
765 }
766
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400767 frame_avail = 1;
768 got_data = 0;
769
Peter Boströma1f64322017-01-19 11:35:30 -0500770 if (framestats_file) fprintf(framestats_file, "bytes,qp\r\n");
771
John Koleszarc6b90392012-07-13 15:21:29 -0700772 /* Decode file */
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400773 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700774 aom_codec_iter_t iter = NULL;
775 aom_image_t *img;
776 struct aom_usec_timer timer;
clang-format6c4d83e2016-08-08 19:03:30 -0700777 int corrupted = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400778
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400779 frame_avail = 0;
780 if (!stop_after || frame_in < stop_after) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800781 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400782 frame_avail = 1;
783 frame_in++;
784
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 aom_usec_timer_start(&timer);
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400786
Sean DuBois47cc2552018-01-23 07:44:16 +0000787 if (aom_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer,
788 NULL)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 const char *detail = aom_codec_error_detail(&decoder);
clang-format6c4d83e2016-08-08 19:03:30 -0700790 warn("Failed to decode frame %d: %s", frame_in,
Yaowu Xuf883b422016-08-30 14:01:10 -0700791 aom_codec_error(&decoder));
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400792
clang-format6c4d83e2016-08-08 19:03:30 -0700793 if (detail) warn("Additional information: %s", detail);
794 if (!keep_going) goto fail;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400795 }
796
Peter Boströma1f64322017-01-19 11:35:30 -0500797 if (framestats_file) {
798 int qp;
799 if (aom_codec_control(&decoder, AOMD_GET_LAST_QUANTIZER, &qp)) {
800 warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
801 aom_codec_error(&decoder));
802 if (!keep_going) goto fail;
803 }
804 fprintf(framestats_file, "%d,%d\r\n", (int)bytes_in_buffer, qp);
805 }
806
Yaowu Xuf883b422016-08-30 14:01:10 -0700807 aom_usec_timer_mark(&timer);
808 dx_time += aom_usec_timer_elapsed(&timer);
hkuang1f841c32015-03-06 13:03:18 -0800809 } else {
810 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400811 }
hkuang1f841c32015-03-06 13:03:18 -0800812 } else {
813 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400814 }
815
Yaowu Xuf883b422016-08-30 14:01:10 -0700816 aom_usec_timer_start(&timer);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400817
hkuang1f841c32015-03-06 13:03:18 -0800818 if (flush_decoder) {
819 // Flush the decoder in frame parallel decode.
Sean DuBois47cc2552018-01-23 07:44:16 +0000820 if (aom_codec_decode(&decoder, NULL, 0, NULL)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700821 warn("Failed to flush decoder: %s", aom_codec_error(&decoder));
hkuang1f841c32015-03-06 13:03:18 -0800822 }
823 }
824
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400825 got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700826 if ((img = aom_codec_get_frame(&decoder, &iter))) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400827 ++frame_out;
828 got_data = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400829 }
830
Yaowu Xuf883b422016-08-30 14:01:10 -0700831 aom_usec_timer_mark(&timer);
832 dx_time += (unsigned int)aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -0700833
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800834 if (aom_codec_control(&decoder, AOMD_GET_FRAME_CORRUPTED, &corrupted)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700835 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder));
clang-format6c4d83e2016-08-08 19:03:30 -0700836 if (!keep_going) goto fail;
John Koleszarc6b90392012-07-13 15:21:29 -0700837 }
838 frames_corrupted += corrupted;
839
clang-format6c4d83e2016-08-08 19:03:30 -0700840 if (progress) show_progress(frame_in, frame_out, dx_time);
John Koleszarc6b90392012-07-13 15:21:29 -0700841
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800842 if (!noblit && img) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700843 const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
844 const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800845 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
John Koleszarda584362013-05-06 15:52:06 -0700846
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800847 if (do_scale) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800848 if (frame_out == 1) {
849 // If the output frames are to be scaled to a fixed display size then
850 // use the width and height specified in the container. If either of
851 // these is set to 0, use the display size set in the first frame
Adrian Grange82f6af62014-01-22 09:36:07 -0800852 // header. If that is unavailable, use the raw decoded size of the
853 // first decoded frame.
Yaowu Xuf883b422016-08-30 14:01:10 -0700854 int render_width = aom_input_ctx.width;
855 int render_height = aom_input_ctx.height;
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400856 if (!render_width || !render_height) {
857 int render_size[2];
Yaowu Xuf883b422016-08-30 14:01:10 -0700858 if (aom_codec_control(&decoder, AV1D_GET_DISPLAY_SIZE,
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400859 render_size)) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800860 // As last resort use size of first frame as display size.
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400861 render_width = img->d_w;
862 render_height = img->d_h;
Adrian Grange482c39c2013-12-13 10:15:03 -0800863 } else {
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400864 render_width = render_size[0];
865 render_height = render_size[1];
Adrian Grange482c39c2013-12-13 10:15:03 -0800866 }
Adrian Grange2117fe02013-11-19 14:01:44 -0800867 }
clang-format6c4d83e2016-08-08 19:03:30 -0700868 scaled_img =
Yaowu Xuf883b422016-08-30 14:01:10 -0700869 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
Deb Mukherjee449e5f22014-07-11 11:43:31 -0700870 scaled_img->bit_depth = img->bit_depth;
John Koleszar7d8fc262013-02-25 16:58:47 -0800871 }
Adrian Grange482c39c2013-12-13 10:15:03 -0800872
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800873 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
Deb Mukherjee47031c02014-05-16 18:52:01 -0700874#if CONFIG_LIBYUV
Tom Finegan8ef2ce82014-12-12 11:57:04 -0800875 libyuv_scale(img, scaled_img, kFilterBox);
John Koleszar7d8fc262013-02-25 16:58:47 -0800876 img = scaled_img;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700877#else
clang-format6c4d83e2016-08-08 19:03:30 -0700878 fprintf(stderr,
Johanne07a6752018-01-10 12:47:44 -0800879 "Failed to scale output frame: %s.\n"
880 "libyuv is required for scaling but is currently disabled.\n"
881 "Be sure to specify -DCONFIG_LIBYUV=1 when running cmake.\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700882 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800883 goto fail;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700884#endif
John Koleszar7d8fc262013-02-25 16:58:47 -0800885 }
886 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700887 // Default to codec bit depth if output bit depth not set
Ronald S. Bultjeeba342a2015-09-11 16:55:19 -0400888 if (!output_bit_depth && single_file && !do_md5) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700889 output_bit_depth = img->bit_depth;
890 }
891 // Shift up or down if necessary
Sebastien Alaiwan18605a32017-05-12 10:46:09 +0200892 if (output_bit_depth != 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700893 const aom_img_fmt_t shifted_fmt =
clang-format6c4d83e2016-08-08 19:03:30 -0700894 output_bit_depth == 8
Yaowu Xuf883b422016-08-30 14:01:10 -0700895 ? img->fmt ^ (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
896 : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
Sebastien Alaiwan18605a32017-05-12 10:46:09 +0200897
898 if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
899 if (img_shifted &&
900 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
901 aom_img_free(img_shifted);
902 img_shifted = NULL;
903 }
904 if (!img_shifted) {
905 img_shifted =
906 aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
907 img_shifted->bit_depth = output_bit_depth;
908 }
909 if (output_bit_depth > img->bit_depth) {
910 aom_img_upshift(img_shifted, img,
911 output_bit_depth - img->bit_depth);
912 } else {
913 aom_img_downshift(img_shifted, img,
914 img->bit_depth - output_bit_depth);
915 }
916 img = img_shifted;
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700917 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700918 }
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800919
Yaowu Xuf883b422016-08-30 14:01:10 -0700920 aom_input_ctx.width = img->d_w;
921 aom_input_ctx.height = img->d_h;
Geza Lore490ba1a2016-03-11 17:42:49 +0000922
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800923 int num_planes = (!use_y4m && opt_raw && img->monochrome) ? 1 : 3;
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000924
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800925 if (single_file) {
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800926 if (use_y4m) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700927 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800928 size_t len = 0;
929 if (frame_out == 1) {
930 // Y4M file header
clang-format6c4d83e2016-08-08 19:03:30 -0700931 len = y4m_write_file_header(
Urvang Joshi4145bf02016-10-17 14:53:33 -0700932 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
933 aom_input_ctx.height, &aom_input_ctx.framerate, img->fmt,
934 img->bit_depth);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800935 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700936 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800937 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700938 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800939 }
940 }
941
942 // Y4M frame header
Urvang Joshi4145bf02016-10-17 14:53:33 -0700943 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800944 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700945 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800946 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700947 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800948 }
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700949 } else {
950 if (frame_out == 1) {
951 // Check if --yv12 or --i420 options are consistent with the
952 // bit-stream decoded
953 if (opt_i420) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700954 if (img->fmt != AOM_IMG_FMT_I420 &&
955 img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700956 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
957 goto fail;
958 }
959 }
960 if (opt_yv12) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700961 if ((img->fmt != AOM_IMG_FMT_I420 &&
962 img->fmt != AOM_IMG_FMT_YV12) ||
clang-format6c4d83e2016-08-08 19:03:30 -0700963 img->bit_depth != 8) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700964 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
965 goto fail;
966 }
967 }
968 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800969 }
970
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800971 if (do_md5) {
972 update_image_md5(img, planes, &md5_ctx);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800973 } else {
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000974 write_image_file(img, planes, num_planes, outfile);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800975 }
976 } else {
clang-format6c4d83e2016-08-08 19:03:30 -0700977 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
978 img->d_h, frame_in);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800979 if (do_md5) {
980 MD5Init(&md5_ctx);
981 update_image_md5(img, planes, &md5_ctx);
982 MD5Final(md5_digest, &md5_ctx);
983 print_md5(md5_digest, outfile_name);
984 } else {
985 outfile = open_outfile(outfile_name);
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000986 write_image_file(img, planes, num_planes, outfile);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800987 fclose(outfile);
John Koleszarc6b90392012-07-13 15:21:29 -0700988 }
John Koleszarc6b90392012-07-13 15:21:29 -0700989 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400990 }
John Koleszarc6b90392012-07-13 15:21:29 -0700991 }
992
993 if (summary || progress) {
994 show_progress(frame_in, frame_out, dx_time);
995 fprintf(stderr, "\n");
996 }
997
James Zern360e4442016-11-16 21:53:55 -0800998 if (frames_corrupted) {
John Koleszarc6b90392012-07-13 15:21:29 -0700999 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
James Zern360e4442016-11-16 21:53:55 -08001000 } else {
1001 ret = EXIT_SUCCESS;
1002 }
John Koleszar4226f0c2011-04-27 12:04:48 -04001003
John Koleszar0ea50ce2010-05-18 11:58:33 -04001004fail:
1005
Yaowu Xuf883b422016-08-30 14:01:10 -07001006 if (aom_codec_destroy(&decoder)) {
Adrian Granged427fab2013-11-21 17:19:04 -08001007 fprintf(stderr, "Failed to destroy decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001008 aom_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -07001009 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001010
James Zern360e4442016-11-16 21:53:55 -08001011fail2:
1012
Dmitry Kovalev347b3062014-01-22 09:04:07 -08001013 if (!noblit && single_file) {
1014 if (do_md5) {
1015 MD5Final(md5_digest, &md5_ctx);
1016 print_md5(md5_digest, outfile_name);
1017 } else {
1018 fclose(outfile);
1019 }
1020 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001021
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001022#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -07001023 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
Tom Finegan2abe2d42013-11-18 14:39:51 -08001024 webm_free(input.webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001025#endif
Tom Finegan66ae9162018-03-19 18:12:50 -07001026 if (input.aom_input_ctx->file_type == FILE_TYPE_OBU)
1027 obudec_free(input.obu_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001028
Yaowu Xuf883b422016-08-30 14:01:10 -07001029 if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
Tom Finegan2abe2d42013-11-18 14:39:51 -08001030
Yaowu Xuf883b422016-08-30 14:01:10 -07001031 if (scaled_img) aom_img_free(scaled_img);
Yaowu Xuf883b422016-08-30 14:01:10 -07001032 if (img_shifted) aom_img_free(img_shifted);
Adrian Granged427fab2013-11-21 17:19:04 -08001033
Frank Galligana4f30a52014-02-06 17:13:08 -08001034 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1035 free(ext_fb_list.ext_fb[i].data);
1036 }
1037 free(ext_fb_list.ext_fb);
1038
John Koleszarc6b90392012-07-13 15:21:29 -07001039 fclose(infile);
Peter Boströma1f64322017-01-19 11:35:30 -05001040 if (framestats_file) fclose(framestats_file);
1041
John Koleszarc6b90392012-07-13 15:21:29 -07001042 free(argv);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001043
James Zern360e4442016-11-16 21:53:55 -08001044 return ret;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001045}
John Koleszar9596a4c2013-06-12 16:08:35 -07001046
1047int main(int argc, const char **argv_) {
1048 unsigned int loops = 1, i;
1049 char **argv, **argi, **argj;
1050 struct arg arg;
1051 int error = 0;
1052
1053 argv = argv_dup(argc - 1, argv_ + 1);
1054 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1055 memset(&arg, 0, sizeof(arg));
1056 arg.argv_step = 1;
1057
1058 if (arg_match(&arg, &looparg, argi)) {
1059 loops = arg_parse_uint(&arg);
1060 break;
1061 }
1062 }
1063 free(argv);
clang-format6c4d83e2016-08-08 19:03:30 -07001064 for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
John Koleszar9596a4c2013-06-12 16:08:35 -07001065 return error;
1066}