blob: 9534febf97b1e9219e3a878f07c7ce0d1dfbd40e [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"
Tom Finegan00a35aa2013-11-14 12:37:42 -080045
Cyril Concolato6c788832017-10-30 16:30:35 -070046#if CONFIG_OBU_NO_IVF
47#include "./obudec.h"
48#endif
49
Tom Finegan00a35aa2013-11-14 12:37:42 -080050#include "./tools_common.h"
James Zernb6cce762014-05-10 17:44:12 -070051#if CONFIG_WEBM_IO
Tom Finegan2abe2d42013-11-18 14:39:51 -080052#include "./webmdec.h"
James Zernb6cce762014-05-10 17:44:12 -070053#endif
Cyril Concolato6c788832017-10-30 16:30:35 -070054
Dmitry Kovalev5ab63582014-01-17 17:02:37 -080055#include "./y4menc.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040056
57static const char *exec_name;
58
Yaowu Xuf883b422016-08-30 14:01:10 -070059struct AvxDecInputContext {
60 struct AvxInputContext *aom_input_ctx;
Tom Finegan2abe2d42013-11-18 14:39:51 -080061 struct WebmInputContext *webm_ctx;
62};
63
James Zernfd8c78c2017-11-27 16:37:33 -080064static const arg_def_t help =
65 ARG_DEF(NULL, "help", 0, "Show usage options and exit");
clang-format6c4d83e2016-08-08 19:03:30 -070066static const arg_def_t looparg =
67 ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
68static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
69static const arg_def_t use_yv12 =
70 ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
71static const arg_def_t use_i420 =
72 ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
73static const arg_def_t flipuvarg =
74 ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
75static const arg_def_t rawvideo =
76 ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
77static const arg_def_t noblitarg =
78 ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
79static const arg_def_t progressarg =
80 ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
81static const arg_def_t limitarg =
82 ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
83static const arg_def_t skiparg =
84 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
85static const arg_def_t postprocarg =
86 ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
87static const arg_def_t summaryarg =
88 ARG_DEF(NULL, "summary", 0, "Show timing summary");
89static const arg_def_t outputfile =
90 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
91static const arg_def_t threadsarg =
92 ARG_DEF("t", "threads", 1, "Max threads to use");
93static const arg_def_t frameparallelarg =
94 ARG_DEF(NULL, "frame-parallel", 0, "Frame parallel decode");
95static const arg_def_t verbosearg =
96 ARG_DEF("v", "verbose", 0, "Show version string");
clang-format6c4d83e2016-08-08 19:03:30 -070097static const arg_def_t scalearg =
98 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
99static const arg_def_t continuearg =
100 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
101static const arg_def_t fb_arg =
102 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
103static const arg_def_t md5arg =
104 ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
Peter Boströma1f64322017-01-19 11:35:30 -0500105static const arg_def_t framestatsarg =
106 ARG_DEF(NULL, "framestats", 1, "Output per-frame stats (.csv format)");
clang-format6c4d83e2016-08-08 19:03:30 -0700107static const arg_def_t outbitdeptharg =
108 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800109static const arg_def_t tilem = ARG_DEF(NULL, "tile-mode", 1,
110 "Tile coding mode "
111 "(0 for normal tile coding mode)");
clang-format6c4d83e2016-08-08 19:03:30 -0700112static const arg_def_t tiler = ARG_DEF(NULL, "tile-row", 1,
113 "Row index of tile to decode "
114 "(-1 for all rows)");
115static const arg_def_t tilec = ARG_DEF(NULL, "tile-column", 1,
116 "Column index of tile to decode "
117 "(-1 for all columns)");
Dmitry Kovalevd24f4e42013-12-27 16:10:18 -0800118
Yaowu Xud3e7c682017-12-21 14:08:25 -0800119static const arg_def_t *all_args[] = {
120 &help, &codecarg, &use_yv12, &use_i420, &flipuvarg,
121 &rawvideo, &noblitarg, &progressarg, &limitarg, &skiparg,
122 &postprocarg, &summaryarg, &outputfile, &threadsarg, &frameparallelarg,
123 &verbosearg, &scalearg, &fb_arg, &md5arg, &framestatsarg,
Yaowu Xudd6ef6c2018-03-02 16:43:52 -0800124 &continuearg, &outbitdeptharg, &tilem, &tiler, &tilec,
Yaowu Xud3e7c682017-12-21 14:08:25 -0800125 NULL
126};
John Koleszar0ea50ce2010-05-18 11:58:33 -0400127
James Zern1e681d92014-08-15 21:00:09 -0700128#if CONFIG_LIBYUV
Yaowu Xuf883b422016-08-30 14:01:10 -0700129static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
clang-format6c4d83e2016-08-08 19:03:30 -0700130 FilterModeEnum mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700131 if (src->fmt == AOM_IMG_FMT_I42016) {
132 assert(dst->fmt == AOM_IMG_FMT_I42016);
clang-format6c4d83e2016-08-08 19:03:30 -0700133 return I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -0700134 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
135 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
136 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
137 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
138 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
139 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
140 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700141 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700142 assert(src->fmt == AOM_IMG_FMT_I420);
143 assert(dst->fmt == AOM_IMG_FMT_I420);
144 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
145 src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
146 src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
147 src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
148 dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
149 dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -0700150 dst->d_h, mode);
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800151}
James Zern1e681d92014-08-15 21:00:09 -0700152#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800153
James Zernfd8c78c2017-11-27 16:37:33 -0800154void show_help(FILE *fout, int shorthelp) {
155 fprintf(fout, "Usage: %s <options> filename\n\n", exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400156
James Zernfd8c78c2017-11-27 16:37:33 -0800157 if (shorthelp) {
158 fprintf(fout, "Use --help to see the full list of options.\n");
159 return;
160 }
161
162 fprintf(fout, "Options:\n");
163 arg_show_usage(fout, all_args);
164 fprintf(fout,
John Koleszarc6b90392012-07-13 15:21:29 -0700165 "\nOutput File Patterns:\n\n"
166 " The -o argument specifies the name of the file(s) to "
167 "write to. If the\n argument does not include any escape "
168 "characters, the output will be\n written to a single file. "
169 "Otherwise, the filename will be calculated by\n expanding "
John Koleszar3707c342012-11-06 10:09:36 -0800170 "the following escape characters:\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800171 fprintf(fout,
John Koleszarc6b90392012-07-13 15:21:29 -0700172 "\n\t%%w - Frame width"
173 "\n\t%%h - Frame height"
174 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
175 "\n\n Pattern arguments are only supported in conjunction "
176 "with the --yv12 and\n --i420 options. If the -o option is "
clang-format6c4d83e2016-08-08 19:03:30 -0700177 "not specified, the output will be\n directed to stdout.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800178 fprintf(fout, "\nIncluded decoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400179
James Zernfd8c78c2017-11-27 16:37:33 -0800180 for (int i = 0; i < get_aom_decoder_count(); ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700181 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
James Zernfd8c78c2017-11-27 16:37:33 -0800182 fprintf(fout, " %-6s - %s\n", decoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700183 aom_codec_iface_name(decoder->codec_interface()));
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800184 }
James Zernfd8c78c2017-11-27 16:37:33 -0800185}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400186
James Zernfd8c78c2017-11-27 16:37:33 -0800187void usage_exit(void) {
188 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700189 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400190}
191
clang-format6c4d83e2016-08-08 19:03:30 -0700192static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
193 size_t *buffer_size) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800194 char raw_hdr[RAW_FRAME_HDR_SZ];
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800195 size_t frame_size = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700196
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800197 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
clang-format6c4d83e2016-08-08 19:03:30 -0700198 if (!feof(infile)) warn("Failed to read RAW frame size\n");
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800199 } else {
James Zern75d7ee32014-02-04 20:06:28 -0800200 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
201 const size_t kFrameTooSmallThreshold = 256 * 1024;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800202 frame_size = mem_get_le32(raw_hdr);
John Koleszarc6b90392012-07-13 15:21:29 -0700203
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800204 if (frame_size > kCorruptFrameThreshold) {
205 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
206 frame_size = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400207 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400208
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800209 if (frame_size < kFrameTooSmallThreshold) {
210 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
211 (unsigned int)frame_size);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400212 }
213
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800214 if (frame_size > *buffer_size) {
215 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
216 if (new_buf) {
217 *buffer = new_buf;
218 *buffer_size = 2 * frame_size;
219 } else {
220 warn("Failed to allocate compressed data buffer\n");
221 frame_size = 0;
222 }
223 }
John Koleszarc6b90392012-07-13 15:21:29 -0700224 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400225
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800226 if (!feof(infile)) {
227 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
228 warn("Failed to read full frame\n");
229 return 1;
230 }
231 *bytes_read = frame_size;
232 }
233
234 return 0;
235}
236
Yaowu Xuf883b422016-08-30 14:01:10 -0700237static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800238 size_t *bytes_in_buffer, size_t *buffer_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700239 switch (input->aom_input_ctx->file_type) {
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700240#if CONFIG_WEBM_IO
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800241 case FILE_TYPE_WEBM:
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700242 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
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#if CONFIG_OBU_NO_IVF
251 case FILE_TYPE_OBU:
252 return obu_read_temporal_unit(input->aom_input_ctx->file, buf,
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000253#if CONFIG_SCALABILITY
254 bytes_in_buffer, buffer_size, 0);
255#else
Cyril Concolato6c788832017-10-30 16:30:35 -0700256 bytes_in_buffer, buffer_size);
257#endif
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000258#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700259 default: return 1;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800260 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400261}
262
Yaowu Xuf883b422016-08-30 14:01:10 -0700263static void update_image_md5(const aom_image_t *img, const int planes[3],
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800264 MD5Context *md5) {
265 int i, y;
266
267 for (i = 0; i < 3; ++i) {
268 const int plane = planes[i];
269 const unsigned char *buf = img->planes[plane];
270 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700271 const int w = aom_img_plane_width(img, plane) *
272 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
273 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800274
275 for (y = 0; y < h; ++y) {
276 MD5Update(md5, buf, w);
277 buf += stride;
278 }
279 }
280}
281
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000282static void write_image_file(const aom_image_t *img, const int *planes,
283 const int num_planes, FILE *file) {
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800284 int i, y;
Yaowu Xuf883b422016-08-30 14:01:10 -0700285 const int bytes_per_sample = ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800286
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000287 for (i = 0; i < num_planes; ++i) {
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800288 const int plane = planes[i];
289 const unsigned char *buf = img->planes[plane];
290 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700291 const int w = aom_img_plane_width(img, plane);
292 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800293
294 for (y = 0; y < h; ++y) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700295 fwrite(buf, bytes_per_sample, w, file);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800296 buf += stride;
297 }
John Koleszarc6b90392012-07-13 15:21:29 -0700298 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400299}
300
Yaowu Xuf883b422016-08-30 14:01:10 -0700301static int file_is_raw(struct AvxInputContext *input) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800302 uint8_t buf[32];
John Koleszarc6b90392012-07-13 15:21:29 -0700303 int is_raw = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700304 aom_codec_stream_info_t si;
John Koleszarcfe3f912010-10-20 10:49:12 -0400305
Tom Finegan00a35aa2013-11-14 12:37:42 -0800306 if (fread(buf, 1, 32, input->file) == 32) {
John Koleszarc6b90392012-07-13 15:21:29 -0700307 int i;
John Koleszarcfe3f912010-10-20 10:49:12 -0400308
Tom Finegan00a35aa2013-11-14 12:37:42 -0800309 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700310 for (i = 0; i < get_aom_decoder_count(); ++i) {
311 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
312 if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
clang-format6c4d83e2016-08-08 19:03:30 -0700313 32 - 4, &si)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700314 is_raw = 1;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800315 input->fourcc = decoder->fourcc;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800316 input->width = si.w;
317 input->height = si.h;
318 input->framerate.numerator = 30;
319 input->framerate.denominator = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700320 break;
321 }
Tom Finegan00a35aa2013-11-14 12:37:42 -0800322 }
323 }
John Koleszarc6b90392012-07-13 15:21:29 -0700324 }
John Koleszarcfe3f912010-10-20 10:49:12 -0400325
Tom Finegan00a35aa2013-11-14 12:37:42 -0800326 rewind(input->file);
John Koleszarc6b90392012-07-13 15:21:29 -0700327 return is_raw;
John Koleszarcfe3f912010-10-20 10:49:12 -0400328}
329
James Zern6a2e0f02015-05-09 10:42:31 -0700330static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
James Yu6d151322014-02-22 14:51:11 +0800331 fprintf(stderr,
clang-format6c4d83e2016-08-08 19:03:30 -0700332 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
John Koleszarc6b90392012-07-13 15:21:29 -0700333 frame_in, frame_out, dx_time,
James Yu6d151322014-02-22 14:51:11 +0800334 (double)frame_out * 1000000.0 / (double)dx_time);
John Koleszar5d12e042010-10-21 17:28:34 -0400335}
336
Frank Galligana4f30a52014-02-06 17:13:08 -0800337struct ExternalFrameBuffer {
clang-format6c4d83e2016-08-08 19:03:30 -0700338 uint8_t *data;
Frank Galligana4f30a52014-02-06 17:13:08 -0800339 size_t size;
340 int in_use;
341};
342
343struct ExternalFrameBufferList {
344 int num_external_frame_buffers;
345 struct ExternalFrameBuffer *ext_fb;
346};
347
Yaowu Xuc27fc142016-08-22 16:08:15 -0700348// Callback used by libaom to request an external frame buffer. |cb_priv|
Frank Galligana4f30a52014-02-06 17:13:08 -0800349// Application private data passed into the set function. |min_size| is the
350// minimum size in bytes needed to decode the next frame. |fb| pointer to the
351// frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700352static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
353 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800354 int i;
355 struct ExternalFrameBufferList *const ext_fb_list =
356 (struct ExternalFrameBufferList *)cb_priv;
clang-format6c4d83e2016-08-08 19:03:30 -0700357 if (ext_fb_list == NULL) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800358
359 // Find a free frame buffer.
360 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
clang-format6c4d83e2016-08-08 19:03:30 -0700361 if (!ext_fb_list->ext_fb[i].in_use) break;
Frank Galligana4f30a52014-02-06 17:13:08 -0800362 }
363
clang-format6c4d83e2016-08-08 19:03:30 -0700364 if (i == ext_fb_list->num_external_frame_buffers) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800365
366 if (ext_fb_list->ext_fb[i].size < min_size) {
367 free(ext_fb_list->ext_fb[i].data);
Frank Galligan175d9df2014-09-11 08:39:38 -0700368 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
clang-format6c4d83e2016-08-08 19:03:30 -0700369 if (!ext_fb_list->ext_fb[i].data) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800370
371 ext_fb_list->ext_fb[i].size = min_size;
372 }
373
374 fb->data = ext_fb_list->ext_fb[i].data;
375 fb->size = ext_fb_list->ext_fb[i].size;
376 ext_fb_list->ext_fb[i].in_use = 1;
377
378 // Set the frame buffer's private data to point at the external frame buffer.
379 fb->priv = &ext_fb_list->ext_fb[i];
380 return 0;
381}
382
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383// Callback used by libaom when there are no references to the frame buffer.
Frank Galligana4f30a52014-02-06 17:13:08 -0800384// |cb_priv| user private data passed into the set function. |fb| pointer
385// to the frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700386static int release_av1_frame_buffer(void *cb_priv,
387 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800388 struct ExternalFrameBuffer *const ext_fb =
389 (struct ExternalFrameBuffer *)fb->priv;
390 (void)cb_priv;
391 ext_fb->in_use = 0;
392 return 0;
393}
394
James Zern6a2e0f02015-05-09 10:42:31 -0700395static void generate_filename(const char *pattern, char *out, size_t q_len,
396 unsigned int d_w, unsigned int d_h,
397 unsigned int frame_in) {
John Koleszarc6b90392012-07-13 15:21:29 -0700398 const char *p = pattern;
399 char *q = out;
John Koleszar933d44b2010-10-21 20:40:42 -0700400
John Koleszarc6b90392012-07-13 15:21:29 -0700401 do {
402 char *next_pat = strchr(p, '%');
John Koleszar933d44b2010-10-21 20:40:42 -0700403
John Koleszarc6b90392012-07-13 15:21:29 -0700404 if (p == next_pat) {
405 size_t pat_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700406
John Koleszar3707c342012-11-06 10:09:36 -0800407 /* parse the pattern */
John Koleszarc6b90392012-07-13 15:21:29 -0700408 q[q_len - 1] = '\0';
409 switch (p[1]) {
clang-format6c4d83e2016-08-08 19:03:30 -0700410 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
411 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
412 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
413 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
414 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
415 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
416 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
417 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
418 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
419 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
420 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
421 default: die("Unrecognized pattern %%%c\n", p[1]); break;
John Koleszarc6b90392012-07-13 15:21:29 -0700422 }
John Koleszar933d44b2010-10-21 20:40:42 -0700423
John Koleszarc6b90392012-07-13 15:21:29 -0700424 pat_len = strlen(q);
clang-format6c4d83e2016-08-08 19:03:30 -0700425 if (pat_len >= q_len - 1) die("Output filename too long.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700426 q += pat_len;
427 p += 2;
428 q_len -= pat_len;
429 } else {
430 size_t copy_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700431
John Koleszar3707c342012-11-06 10:09:36 -0800432 /* copy the next segment */
John Koleszarc6b90392012-07-13 15:21:29 -0700433 if (!next_pat)
434 copy_len = strlen(p);
435 else
436 copy_len = next_pat - p;
John Koleszar933d44b2010-10-21 20:40:42 -0700437
clang-format6c4d83e2016-08-08 19:03:30 -0700438 if (copy_len >= q_len - 1) die("Output filename too long.\n");
John Koleszar933d44b2010-10-21 20:40:42 -0700439
John Koleszarc6b90392012-07-13 15:21:29 -0700440 memcpy(q, p, copy_len);
441 q[copy_len] = '\0';
442 q += copy_len;
443 p += copy_len;
444 q_len -= copy_len;
445 }
446 } while (*p);
John Koleszar933d44b2010-10-21 20:40:42 -0700447}
448
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800449static int is_single_file(const char *outfile_pattern) {
450 const char *p = outfile_pattern;
451
452 do {
453 p = strchr(p, '%');
454 if (p && p[1] >= '1' && p[1] <= '9')
455 return 0; // pattern contains sequence number, so it's not unique
clang-format6c4d83e2016-08-08 19:03:30 -0700456 if (p) p++;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800457 } while (p);
458
459 return 1;
460}
461
462static void print_md5(unsigned char digest[16], const char *filename) {
463 int i;
464
clang-format6c4d83e2016-08-08 19:03:30 -0700465 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800466 printf(" %s\n", filename);
467}
468
469static FILE *open_outfile(const char *name) {
470 if (strcmp("-", name) == 0) {
471 set_binary_mode(stdout);
472 return stdout;
473 } else {
474 FILE *file = fopen(name, "wb");
clang-format6c4d83e2016-08-08 19:03:30 -0700475 if (!file) fatal("Failed to open output file '%s'", name);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800476 return file;
477 }
478}
479
Yaowu Xuf883b422016-08-30 14:01:10 -0700480static int img_shifted_realloc_required(const aom_image_t *img,
481 const aom_image_t *shifted,
482 aom_img_fmt_t required_fmt) {
clang-format6c4d83e2016-08-08 19:03:30 -0700483 return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700484 required_fmt != shifted->fmt;
485}
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700486
James Zern6a2e0f02015-05-09 10:42:31 -0700487static int main_loop(int argc, const char **argv_) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700488 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700489 char *fn = NULL;
490 int i;
James Zern360e4442016-11-16 21:53:55 -0800491 int ret = EXIT_FAILURE;
clang-format6c4d83e2016-08-08 19:03:30 -0700492 uint8_t *buf = NULL;
493 size_t bytes_in_buffer = 0, buffer_size = 0;
494 FILE *infile;
495 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
496 int do_md5 = 0, progress = 0, frame_parallel = 0;
497 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
498 int arg_skip = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700499 int keep_going = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700500 const AvxInterface *interface = NULL;
501 const AvxInterface *fourcc_interface = NULL;
James Yu6d151322014-02-22 14:51:11 +0800502 uint64_t dx_time = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700503 struct arg arg;
504 char **argv, **argi, **argj;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800505
clang-format6c4d83e2016-08-08 19:03:30 -0700506 int single_file;
507 int use_y4m = 1;
508 int opt_yv12 = 0;
509 int opt_i420 = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800510 int opt_raw = 0;
Debargha Mukherjee2ccf4b92018-02-27 17:30:46 -0800511 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, { 1 } };
clang-format6c4d83e2016-08-08 19:03:30 -0700512 unsigned int output_bit_depth = 0;
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800513 unsigned int tile_mode = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700514 int tile_row = -1;
515 int tile_col = -1;
clang-format6c4d83e2016-08-08 19:03:30 -0700516 int frames_corrupted = 0;
517 int dec_flags = 0;
518 int do_scale = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700519 aom_image_t *scaled_img = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700520 aom_image_t *img_shifted = NULL;
clang-format6c4d83e2016-08-08 19:03:30 -0700521 int frame_avail, got_data, flush_decoder = 0;
522 int num_external_frame_buffers = 0;
523 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400524
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800525 const char *outfile_pattern = NULL;
clang-format6c4d83e2016-08-08 19:03:30 -0700526 char outfile_name[PATH_MAX] = { 0 };
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800527 FILE *outfile = NULL;
528
Peter Boströma1f64322017-01-19 11:35:30 -0500529 FILE *framestats_file = NULL;
530
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800531 MD5Context md5_ctx;
532 unsigned char md5_digest[16];
533
Yaowu Xuf883b422016-08-30 14:01:10 -0700534 struct AvxDecInputContext input = { NULL, NULL };
535 struct AvxInputContext aom_input_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700536#if CONFIG_WEBM_IO
Jim Bankoski7ae32692014-08-19 06:44:10 -0700537 struct WebmInputContext webm_ctx;
538 memset(&(webm_ctx), 0, sizeof(webm_ctx));
Tom Finegan2abe2d42013-11-18 14:39:51 -0800539 input.webm_ctx = &webm_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700540#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700541 input.aom_input_ctx = &aom_input_ctx;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800542
John Koleszarc6b90392012-07-13 15:21:29 -0700543 /* Parse command line */
544 exec_name = argv_[0];
545 argv = argv_dup(argc - 1, argv_ + 1);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400546
John Koleszarc6b90392012-07-13 15:21:29 -0700547 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
548 memset(&arg, 0, sizeof(arg));
549 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400550
James Zernfd8c78c2017-11-27 16:37:33 -0800551 if (arg_match(&arg, &help, argi)) {
552 show_help(stdout, 0);
553 exit(EXIT_SUCCESS);
554 } else if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700555 interface = get_aom_decoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800556 if (!interface)
557 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszar9596a4c2013-06-12 16:08:35 -0700558 } else if (arg_match(&arg, &looparg, argi)) {
559 // no-op
Peter Boströma1f64322017-01-19 11:35:30 -0500560 } else if (arg_match(&arg, &outputfile, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700561 outfile_pattern = arg.val;
Peter Boströma1f64322017-01-19 11:35:30 -0500562 } else if (arg_match(&arg, &use_yv12, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700563 use_y4m = 0;
564 flipuv = 1;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700565 opt_yv12 = 1;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800566 opt_i420 = 0;
567 opt_raw = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700568 } else if (arg_match(&arg, &use_i420, argi)) {
569 use_y4m = 0;
570 flipuv = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800571 opt_yv12 = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700572 opt_i420 = 1;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800573 opt_raw = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700574 } else if (arg_match(&arg, &rawvideo, argi)) {
575 use_y4m = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800576 opt_yv12 = 0;
577 opt_i420 = 0;
578 opt_raw = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500579 } else if (arg_match(&arg, &flipuvarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700580 flipuv = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500581 } else if (arg_match(&arg, &noblitarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700582 noblit = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500583 } else if (arg_match(&arg, &progressarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700584 progress = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500585 } else if (arg_match(&arg, &limitarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700586 stop_after = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500587 } else if (arg_match(&arg, &skiparg, argi)) {
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700588 arg_skip = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500589 } else if (arg_match(&arg, &postprocarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700590 postproc = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500591 } else if (arg_match(&arg, &md5arg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700592 do_md5 = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500593 } else if (arg_match(&arg, &framestatsarg, argi)) {
594 framestats_file = fopen(arg.val, "w");
595 if (!framestats_file) {
596 die("Error: Could not open --framestats file (%s) for writing.\n",
597 arg.val);
598 }
599 } else if (arg_match(&arg, &summaryarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700600 summary = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500601 } else if (arg_match(&arg, &threadsarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700602 cfg.threads = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500603 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700604#if CONFIG_AV1_DECODER
hkuang1f841c32015-03-06 13:03:18 -0800605 else if (arg_match(&arg, &frameparallelarg, argi))
606 frame_parallel = 1;
607#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700608 else if (arg_match(&arg, &verbosearg, argi))
609 quiet = 0;
John Koleszar7d8fc262013-02-25 16:58:47 -0800610 else if (arg_match(&arg, &scalearg, argi))
611 do_scale = 1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800612 else if (arg_match(&arg, &fb_arg, argi))
613 num_external_frame_buffers = arg_parse_uint(&arg);
James Zern7690d882014-08-22 18:01:28 -0700614 else if (arg_match(&arg, &continuearg, argi))
615 keep_going = 1;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700616 else if (arg_match(&arg, &outbitdeptharg, argi)) {
617 output_bit_depth = arg_parse_uint(&arg);
Yaowu Xudd6ef6c2018-03-02 16:43:52 -0800618 } else if (arg_match(&arg, &tilem, argi))
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800619 tile_mode = arg_parse_int(&arg);
Geza Lore490ba1a2016-03-11 17:42:49 +0000620 else if (arg_match(&arg, &tiler, argi))
621 tile_row = arg_parse_int(&arg);
622 else if (arg_match(&arg, &tilec, argi))
623 tile_col = arg_parse_int(&arg);
John Koleszarcfe3f912010-10-20 10:49:12 -0400624 else
John Koleszarc6b90392012-07-13 15:21:29 -0700625 argj++;
626 }
627
628 /* Check for unrecognized options */
629 for (argi = argv; *argi; argi++)
630 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
631 die("Error: Unrecognized option %s\n", *argi);
632
633 /* Handle non-option arguments */
634 fn = argv[0];
635
Hanno Böckb5d0a202015-02-24 15:48:21 -0800636 if (!fn) {
637 free(argv);
James Zernfd8c78c2017-11-27 16:37:33 -0800638 fprintf(stderr, "No input file specified!\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700639 usage_exit();
Hanno Böckb5d0a202015-02-24 15:48:21 -0800640 }
John Koleszarc6b90392012-07-13 15:21:29 -0700641 /* Open file */
642 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
643
644 if (!infile) {
James Zern6b993f02015-02-05 21:06:29 -0800645 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
John Koleszarc6b90392012-07-13 15:21:29 -0700646 }
647#if CONFIG_OS_SUPPORT
648 /* Make sure we don't dump to the terminal, unless forced to with -o - */
James Zernb9b28382016-12-09 16:50:39 -0800649 if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700650 fprintf(stderr,
651 "Not dumping raw video to your terminal. Use '-o -' to "
652 "override.\n");
653 return EXIT_FAILURE;
654 }
655#endif
Tom Fineganccb8cc42018-02-22 14:08:13 -0800656 input.aom_input_ctx->filename = fn;
Yaowu Xuf883b422016-08-30 14:01:10 -0700657 input.aom_input_ctx->file = infile;
658 if (file_is_ivf(input.aom_input_ctx))
659 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700660#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700661 else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
662 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700663#endif
Cyril Concolato6c788832017-10-30 16:30:35 -0700664#if CONFIG_OBU_NO_IVF
665 else if (file_is_obu(input.aom_input_ctx))
666 input.aom_input_ctx->file_type = FILE_TYPE_OBU;
667#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700668 else if (file_is_raw(input.aom_input_ctx))
669 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700670 else {
671 fprintf(stderr, "Unrecognized input file type.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700672#if !CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700673 fprintf(stderr, "aomdec was built without WebM container support.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700674#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700675 return EXIT_FAILURE;
676 }
677
John Koleszarc6b90392012-07-13 15:21:29 -0700678 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800679 single_file = is_single_file(outfile_pattern);
John Koleszarc6b90392012-07-13 15:21:29 -0700680
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800681 if (!noblit && single_file) {
682 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
Yaowu Xuf883b422016-08-30 14:01:10 -0700683 aom_input_ctx.width, aom_input_ctx.height, 0);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800684 if (do_md5)
685 MD5Init(&md5_ctx);
686 else
687 outfile = open_outfile(outfile_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700688 }
689
690 if (use_y4m && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700691 if (!single_file) {
clang-format6c4d83e2016-08-08 19:03:30 -0700692 fprintf(stderr,
693 "YUV4MPEG2 not supported with output patterns,"
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700694 " try --i420 or --yv12 or --rawvideo.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700695 return EXIT_FAILURE;
696 }
697
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700698#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700699 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
700 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
clang-format6c4d83e2016-08-08 19:03:30 -0700701 fprintf(stderr,
702 "Failed to guess framerate -- error parsing "
John Koleszarc6b90392012-07-13 15:21:29 -0700703 "webm file?\n");
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400704 return EXIT_FAILURE;
John Koleszarc6b90392012-07-13 15:21:29 -0700705 }
Dmitry Kovalev5ab63582014-01-17 17:02:37 -0800706 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700707#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700708 }
709
Yaowu Xuf883b422016-08-30 14:01:10 -0700710 fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800711 if (interface && fourcc_interface && interface != fourcc_interface)
712 warn("Header indicates codec: %s\n", fourcc_interface->name);
713 else
714 interface = fourcc_interface;
John Koleszarc6b90392012-07-13 15:21:29 -0700715
Yaowu Xuf883b422016-08-30 14:01:10 -0700716 if (!interface) interface = get_aom_decoder_by_index(0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400717
Yaowu Xuf883b422016-08-30 14:01:10 -0700718 dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0) |
Yaowu Xuf883b422016-08-30 14:01:10 -0700719 (frame_parallel ? AOM_CODEC_USE_FRAME_THREADING : 0);
720 if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
clang-format6c4d83e2016-08-08 19:03:30 -0700721 dec_flags)) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800722 fprintf(stderr, "Failed to initialize decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700723 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800724 goto fail2;
John Koleszarc6b90392012-07-13 15:21:29 -0700725 }
John Koleszar933d44b2010-10-21 20:40:42 -0700726
clang-format6c4d83e2016-08-08 19:03:30 -0700727 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400728
Yaowu Xudd6ef6c2018-03-02 16:43:52 -0800729#if CONFIG_AV1_DECODER
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800730 if (aom_codec_control(&decoder, AV1_SET_TILE_MODE, tile_mode)) {
731 fprintf(stderr, "Failed to set decode_tile_mode: %s\n",
732 aom_codec_error(&decoder));
733 goto fail;
734 }
735
Jingning Hanf79d48d2016-12-20 14:52:35 -0800736 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_ROW, tile_row)) {
737 fprintf(stderr, "Failed to set decode_tile_row: %s\n",
738 aom_codec_error(&decoder));
739 goto fail;
740 }
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700741
Jingning Hanf79d48d2016-12-20 14:52:35 -0800742 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_COL, tile_col)) {
743 fprintf(stderr, "Failed to set decode_tile_col: %s\n",
744 aom_codec_error(&decoder));
745 goto fail;
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700746 }
747#endif
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700748
clang-format6c4d83e2016-08-08 19:03:30 -0700749 if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700750 while (arg_skip) {
clang-format6c4d83e2016-08-08 19:03:30 -0700751 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700752 arg_skip--;
753 }
754
Frank Galligana4f30a52014-02-06 17:13:08 -0800755 if (num_external_frame_buffers > 0) {
756 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
757 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
758 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
Yaowu Xuf883b422016-08-30 14:01:10 -0700759 if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
760 release_av1_frame_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700761 &ext_fb_list)) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800762 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700763 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800764 goto fail;
Frank Galligana4f30a52014-02-06 17:13:08 -0800765 }
766 }
767
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400768 frame_avail = 1;
769 got_data = 0;
770
Peter Boströma1f64322017-01-19 11:35:30 -0500771 if (framestats_file) fprintf(framestats_file, "bytes,qp\r\n");
772
John Koleszarc6b90392012-07-13 15:21:29 -0700773 /* Decode file */
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400774 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700775 aom_codec_iter_t iter = NULL;
776 aom_image_t *img;
777 struct aom_usec_timer timer;
clang-format6c4d83e2016-08-08 19:03:30 -0700778 int corrupted = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400779
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400780 frame_avail = 0;
781 if (!stop_after || frame_in < stop_after) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800782 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400783 frame_avail = 1;
784 frame_in++;
785
Yaowu Xuf883b422016-08-30 14:01:10 -0700786 aom_usec_timer_start(&timer);
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400787
Sean DuBois47cc2552018-01-23 07:44:16 +0000788 if (aom_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer,
789 NULL)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700790 const char *detail = aom_codec_error_detail(&decoder);
clang-format6c4d83e2016-08-08 19:03:30 -0700791 warn("Failed to decode frame %d: %s", frame_in,
Yaowu Xuf883b422016-08-30 14:01:10 -0700792 aom_codec_error(&decoder));
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400793
clang-format6c4d83e2016-08-08 19:03:30 -0700794 if (detail) warn("Additional information: %s", detail);
795 if (!keep_going) goto fail;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400796 }
797
Peter Boströma1f64322017-01-19 11:35:30 -0500798 if (framestats_file) {
799 int qp;
800 if (aom_codec_control(&decoder, AOMD_GET_LAST_QUANTIZER, &qp)) {
801 warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
802 aom_codec_error(&decoder));
803 if (!keep_going) goto fail;
804 }
805 fprintf(framestats_file, "%d,%d\r\n", (int)bytes_in_buffer, qp);
806 }
807
Yaowu Xuf883b422016-08-30 14:01:10 -0700808 aom_usec_timer_mark(&timer);
809 dx_time += aom_usec_timer_elapsed(&timer);
hkuang1f841c32015-03-06 13:03:18 -0800810 } else {
811 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400812 }
hkuang1f841c32015-03-06 13:03:18 -0800813 } else {
814 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400815 }
816
Yaowu Xuf883b422016-08-30 14:01:10 -0700817 aom_usec_timer_start(&timer);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400818
hkuang1f841c32015-03-06 13:03:18 -0800819 if (flush_decoder) {
820 // Flush the decoder in frame parallel decode.
Sean DuBois47cc2552018-01-23 07:44:16 +0000821 if (aom_codec_decode(&decoder, NULL, 0, NULL)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700822 warn("Failed to flush decoder: %s", aom_codec_error(&decoder));
hkuang1f841c32015-03-06 13:03:18 -0800823 }
824 }
825
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400826 got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700827 if ((img = aom_codec_get_frame(&decoder, &iter))) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400828 ++frame_out;
829 got_data = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400830 }
831
Yaowu Xuf883b422016-08-30 14:01:10 -0700832 aom_usec_timer_mark(&timer);
833 dx_time += (unsigned int)aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -0700834
hkuang1f841c32015-03-06 13:03:18 -0800835 if (!frame_parallel &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700836 aom_codec_control(&decoder, AOMD_GET_FRAME_CORRUPTED, &corrupted)) {
837 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder));
clang-format6c4d83e2016-08-08 19:03:30 -0700838 if (!keep_going) goto fail;
John Koleszarc6b90392012-07-13 15:21:29 -0700839 }
840 frames_corrupted += corrupted;
841
clang-format6c4d83e2016-08-08 19:03:30 -0700842 if (progress) show_progress(frame_in, frame_out, dx_time);
John Koleszarc6b90392012-07-13 15:21:29 -0700843
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800844 if (!noblit && img) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700845 const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
846 const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800847 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
John Koleszarda584362013-05-06 15:52:06 -0700848
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800849 if (do_scale) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800850 if (frame_out == 1) {
851 // If the output frames are to be scaled to a fixed display size then
852 // use the width and height specified in the container. If either of
853 // these is set to 0, use the display size set in the first frame
Adrian Grange82f6af62014-01-22 09:36:07 -0800854 // header. If that is unavailable, use the raw decoded size of the
855 // first decoded frame.
Yaowu Xuf883b422016-08-30 14:01:10 -0700856 int render_width = aom_input_ctx.width;
857 int render_height = aom_input_ctx.height;
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400858 if (!render_width || !render_height) {
859 int render_size[2];
Yaowu Xuf883b422016-08-30 14:01:10 -0700860 if (aom_codec_control(&decoder, AV1D_GET_DISPLAY_SIZE,
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400861 render_size)) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800862 // As last resort use size of first frame as display size.
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400863 render_width = img->d_w;
864 render_height = img->d_h;
Adrian Grange482c39c2013-12-13 10:15:03 -0800865 } else {
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400866 render_width = render_size[0];
867 render_height = render_size[1];
Adrian Grange482c39c2013-12-13 10:15:03 -0800868 }
Adrian Grange2117fe02013-11-19 14:01:44 -0800869 }
clang-format6c4d83e2016-08-08 19:03:30 -0700870 scaled_img =
Yaowu Xuf883b422016-08-30 14:01:10 -0700871 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
Deb Mukherjee449e5f22014-07-11 11:43:31 -0700872 scaled_img->bit_depth = img->bit_depth;
John Koleszar7d8fc262013-02-25 16:58:47 -0800873 }
Adrian Grange482c39c2013-12-13 10:15:03 -0800874
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800875 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
Deb Mukherjee47031c02014-05-16 18:52:01 -0700876#if CONFIG_LIBYUV
Tom Finegan8ef2ce82014-12-12 11:57:04 -0800877 libyuv_scale(img, scaled_img, kFilterBox);
John Koleszar7d8fc262013-02-25 16:58:47 -0800878 img = scaled_img;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700879#else
clang-format6c4d83e2016-08-08 19:03:30 -0700880 fprintf(stderr,
Johanne07a6752018-01-10 12:47:44 -0800881 "Failed to scale output frame: %s.\n"
882 "libyuv is required for scaling but is currently disabled.\n"
883 "Be sure to specify -DCONFIG_LIBYUV=1 when running cmake.\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700884 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800885 goto fail;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700886#endif
John Koleszar7d8fc262013-02-25 16:58:47 -0800887 }
888 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700889 // Default to codec bit depth if output bit depth not set
Ronald S. Bultjeeba342a2015-09-11 16:55:19 -0400890 if (!output_bit_depth && single_file && !do_md5) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700891 output_bit_depth = img->bit_depth;
892 }
893 // Shift up or down if necessary
Sebastien Alaiwan18605a32017-05-12 10:46:09 +0200894 if (output_bit_depth != 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700895 const aom_img_fmt_t shifted_fmt =
clang-format6c4d83e2016-08-08 19:03:30 -0700896 output_bit_depth == 8
Yaowu Xuf883b422016-08-30 14:01:10 -0700897 ? img->fmt ^ (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
898 : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
Sebastien Alaiwan18605a32017-05-12 10:46:09 +0200899
900 if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
901 if (img_shifted &&
902 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
903 aom_img_free(img_shifted);
904 img_shifted = NULL;
905 }
906 if (!img_shifted) {
907 img_shifted =
908 aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
909 img_shifted->bit_depth = output_bit_depth;
910 }
911 if (output_bit_depth > img->bit_depth) {
912 aom_img_upshift(img_shifted, img,
913 output_bit_depth - img->bit_depth);
914 } else {
915 aom_img_downshift(img_shifted, img,
916 img->bit_depth - output_bit_depth);
917 }
918 img = img_shifted;
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700919 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700920 }
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800921
Yaowu Xuf883b422016-08-30 14:01:10 -0700922 aom_input_ctx.width = img->d_w;
923 aom_input_ctx.height = img->d_h;
Geza Lore490ba1a2016-03-11 17:42:49 +0000924
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800925#if CONFIG_MONO_VIDEO
926 int num_planes = (!use_y4m && opt_raw && img->monochrome) ? 1 : 3;
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000927#else
928 int num_planes = 3;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800929 (void)opt_raw;
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000930#endif
931
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800932 if (single_file) {
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800933 if (use_y4m) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700934 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800935 size_t len = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700936 if (img->fmt == AOM_IMG_FMT_I440 || img->fmt == AOM_IMG_FMT_I44016) {
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700937 fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
938 goto fail;
939 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800940 if (frame_out == 1) {
941 // Y4M file header
clang-format6c4d83e2016-08-08 19:03:30 -0700942 len = y4m_write_file_header(
Urvang Joshi4145bf02016-10-17 14:53:33 -0700943 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
944 aom_input_ctx.height, &aom_input_ctx.framerate, img->fmt,
945 img->bit_depth);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800946 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700947 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800948 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700949 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800950 }
951 }
952
953 // Y4M frame header
Urvang Joshi4145bf02016-10-17 14:53:33 -0700954 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800955 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700956 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800957 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700958 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800959 }
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700960 } else {
961 if (frame_out == 1) {
962 // Check if --yv12 or --i420 options are consistent with the
963 // bit-stream decoded
964 if (opt_i420) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700965 if (img->fmt != AOM_IMG_FMT_I420 &&
966 img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700967 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
968 goto fail;
969 }
970 }
971 if (opt_yv12) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700972 if ((img->fmt != AOM_IMG_FMT_I420 &&
973 img->fmt != AOM_IMG_FMT_YV12) ||
clang-format6c4d83e2016-08-08 19:03:30 -0700974 img->bit_depth != 8) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700975 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
976 goto fail;
977 }
978 }
979 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800980 }
981
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800982 if (do_md5) {
983 update_image_md5(img, planes, &md5_ctx);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800984 } else {
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000985 write_image_file(img, planes, num_planes, outfile);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800986 }
987 } else {
clang-format6c4d83e2016-08-08 19:03:30 -0700988 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
989 img->d_h, frame_in);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800990 if (do_md5) {
991 MD5Init(&md5_ctx);
992 update_image_md5(img, planes, &md5_ctx);
993 MD5Final(md5_digest, &md5_ctx);
994 print_md5(md5_digest, outfile_name);
995 } else {
996 outfile = open_outfile(outfile_name);
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000997 write_image_file(img, planes, num_planes, outfile);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800998 fclose(outfile);
John Koleszarc6b90392012-07-13 15:21:29 -0700999 }
John Koleszarc6b90392012-07-13 15:21:29 -07001000 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001001 }
John Koleszarc6b90392012-07-13 15:21:29 -07001002 }
1003
1004 if (summary || progress) {
1005 show_progress(frame_in, frame_out, dx_time);
1006 fprintf(stderr, "\n");
1007 }
1008
James Zern360e4442016-11-16 21:53:55 -08001009 if (frames_corrupted) {
John Koleszarc6b90392012-07-13 15:21:29 -07001010 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
James Zern360e4442016-11-16 21:53:55 -08001011 } else {
1012 ret = EXIT_SUCCESS;
1013 }
John Koleszar4226f0c2011-04-27 12:04:48 -04001014
John Koleszar0ea50ce2010-05-18 11:58:33 -04001015fail:
1016
Yaowu Xuf883b422016-08-30 14:01:10 -07001017 if (aom_codec_destroy(&decoder)) {
Adrian Granged427fab2013-11-21 17:19:04 -08001018 fprintf(stderr, "Failed to destroy decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001019 aom_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -07001020 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001021
James Zern360e4442016-11-16 21:53:55 -08001022fail2:
1023
Dmitry Kovalev347b3062014-01-22 09:04:07 -08001024 if (!noblit && single_file) {
1025 if (do_md5) {
1026 MD5Final(md5_digest, &md5_ctx);
1027 print_md5(md5_digest, outfile_name);
1028 } else {
1029 fclose(outfile);
1030 }
1031 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001032
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001033#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -07001034 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
Tom Finegan2abe2d42013-11-18 14:39:51 -08001035 webm_free(input.webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001036#endif
1037
Yaowu Xuf883b422016-08-30 14:01:10 -07001038 if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
Tom Finegan2abe2d42013-11-18 14:39:51 -08001039
Yaowu Xuf883b422016-08-30 14:01:10 -07001040 if (scaled_img) aom_img_free(scaled_img);
Yaowu Xuf883b422016-08-30 14:01:10 -07001041 if (img_shifted) aom_img_free(img_shifted);
Adrian Granged427fab2013-11-21 17:19:04 -08001042
Frank Galligana4f30a52014-02-06 17:13:08 -08001043 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1044 free(ext_fb_list.ext_fb[i].data);
1045 }
1046 free(ext_fb_list.ext_fb);
1047
John Koleszarc6b90392012-07-13 15:21:29 -07001048 fclose(infile);
Peter Boströma1f64322017-01-19 11:35:30 -05001049 if (framestats_file) fclose(framestats_file);
1050
John Koleszarc6b90392012-07-13 15:21:29 -07001051 free(argv);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001052
James Zern360e4442016-11-16 21:53:55 -08001053 return ret;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001054}
John Koleszar9596a4c2013-06-12 16:08:35 -07001055
1056int main(int argc, const char **argv_) {
1057 unsigned int loops = 1, i;
1058 char **argv, **argi, **argj;
1059 struct arg arg;
1060 int error = 0;
1061
1062 argv = argv_dup(argc - 1, argv_ + 1);
1063 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1064 memset(&arg, 0, sizeof(arg));
1065 arg.argv_step = 1;
1066
1067 if (arg_match(&arg, &looparg, argi)) {
1068 loops = arg_parse_uint(&arg);
1069 break;
1070 }
1071 }
1072 free(argv);
clang-format6c4d83e2016-08-08 19:03:30 -07001073 for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
John Koleszar9596a4c2013-06-12 16:08:35 -07001074 return error;
1075}