blob: 54cbecb49186b8a19ae0d0f41bf929b55a01e5d6 [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");
clang-format6c4d83e2016-08-08 19:03:30 -070093static const arg_def_t verbosearg =
94 ARG_DEF("v", "verbose", 0, "Show version string");
clang-format6c4d83e2016-08-08 19:03:30 -070095static const arg_def_t scalearg =
96 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
97static const arg_def_t continuearg =
98 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
99static const arg_def_t fb_arg =
100 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
101static const arg_def_t md5arg =
102 ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
Peter Boströma1f64322017-01-19 11:35:30 -0500103static const arg_def_t framestatsarg =
104 ARG_DEF(NULL, "framestats", 1, "Output per-frame stats (.csv format)");
clang-format6c4d83e2016-08-08 19:03:30 -0700105static const arg_def_t outbitdeptharg =
106 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800107static const arg_def_t tilem = ARG_DEF(NULL, "tile-mode", 1,
108 "Tile coding mode "
109 "(0 for normal tile coding mode)");
clang-format6c4d83e2016-08-08 19:03:30 -0700110static const arg_def_t tiler = ARG_DEF(NULL, "tile-row", 1,
111 "Row index of tile to decode "
112 "(-1 for all rows)");
113static const arg_def_t tilec = ARG_DEF(NULL, "tile-column", 1,
114 "Column index of tile to decode "
115 "(-1 for all columns)");
Dmitry Kovalevd24f4e42013-12-27 16:10:18 -0800116
Yaowu Xud3e7c682017-12-21 14:08:25 -0800117static const arg_def_t *all_args[] = {
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800118 &help, &codecarg, &use_yv12, &use_i420, &flipuvarg,
119 &rawvideo, &noblitarg, &progressarg, &limitarg, &skiparg,
120 &postprocarg, &summaryarg, &outputfile, &threadsarg, &verbosearg,
121 &scalearg, &fb_arg, &md5arg, &framestatsarg, &continuearg,
122 &outbitdeptharg, &tilem, &tiler, &tilec, NULL
Yaowu Xud3e7c682017-12-21 14:08:25 -0800123};
John Koleszar0ea50ce2010-05-18 11:58:33 -0400124
James Zern1e681d92014-08-15 21:00:09 -0700125#if CONFIG_LIBYUV
Yaowu Xuf883b422016-08-30 14:01:10 -0700126static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
clang-format6c4d83e2016-08-08 19:03:30 -0700127 FilterModeEnum mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700128 if (src->fmt == AOM_IMG_FMT_I42016) {
129 assert(dst->fmt == AOM_IMG_FMT_I42016);
clang-format6c4d83e2016-08-08 19:03:30 -0700130 return I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -0700131 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
132 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
133 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
134 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
135 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
136 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
137 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700138 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700139 assert(src->fmt == AOM_IMG_FMT_I420);
140 assert(dst->fmt == AOM_IMG_FMT_I420);
141 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
142 src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
143 src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
144 src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
145 dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
146 dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -0700147 dst->d_h, mode);
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800148}
James Zern1e681d92014-08-15 21:00:09 -0700149#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800150
James Zernfd8c78c2017-11-27 16:37:33 -0800151void show_help(FILE *fout, int shorthelp) {
152 fprintf(fout, "Usage: %s <options> filename\n\n", exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400153
James Zernfd8c78c2017-11-27 16:37:33 -0800154 if (shorthelp) {
155 fprintf(fout, "Use --help to see the full list of options.\n");
156 return;
157 }
158
159 fprintf(fout, "Options:\n");
160 arg_show_usage(fout, all_args);
161 fprintf(fout,
John Koleszarc6b90392012-07-13 15:21:29 -0700162 "\nOutput File Patterns:\n\n"
163 " The -o argument specifies the name of the file(s) to "
164 "write to. If the\n argument does not include any escape "
165 "characters, the output will be\n written to a single file. "
166 "Otherwise, the filename will be calculated by\n expanding "
John Koleszar3707c342012-11-06 10:09:36 -0800167 "the following escape characters:\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800168 fprintf(fout,
John Koleszarc6b90392012-07-13 15:21:29 -0700169 "\n\t%%w - Frame width"
170 "\n\t%%h - Frame height"
171 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
172 "\n\n Pattern arguments are only supported in conjunction "
173 "with the --yv12 and\n --i420 options. If the -o option is "
clang-format6c4d83e2016-08-08 19:03:30 -0700174 "not specified, the output will be\n directed to stdout.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800175 fprintf(fout, "\nIncluded decoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400176
James Zernfd8c78c2017-11-27 16:37:33 -0800177 for (int i = 0; i < get_aom_decoder_count(); ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700178 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
James Zernfd8c78c2017-11-27 16:37:33 -0800179 fprintf(fout, " %-6s - %s\n", decoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700180 aom_codec_iface_name(decoder->codec_interface()));
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800181 }
James Zernfd8c78c2017-11-27 16:37:33 -0800182}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400183
James Zernfd8c78c2017-11-27 16:37:33 -0800184void usage_exit(void) {
185 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700186 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400187}
188
clang-format6c4d83e2016-08-08 19:03:30 -0700189static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
190 size_t *buffer_size) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800191 char raw_hdr[RAW_FRAME_HDR_SZ];
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800192 size_t frame_size = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700193
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800194 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
clang-format6c4d83e2016-08-08 19:03:30 -0700195 if (!feof(infile)) warn("Failed to read RAW frame size\n");
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800196 } else {
James Zern75d7ee32014-02-04 20:06:28 -0800197 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
198 const size_t kFrameTooSmallThreshold = 256 * 1024;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800199 frame_size = mem_get_le32(raw_hdr);
John Koleszarc6b90392012-07-13 15:21:29 -0700200
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800201 if (frame_size > kCorruptFrameThreshold) {
202 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
203 frame_size = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400204 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400205
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800206 if (frame_size < kFrameTooSmallThreshold) {
207 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
208 (unsigned int)frame_size);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400209 }
210
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800211 if (frame_size > *buffer_size) {
212 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
213 if (new_buf) {
214 *buffer = new_buf;
215 *buffer_size = 2 * frame_size;
216 } else {
217 warn("Failed to allocate compressed data buffer\n");
218 frame_size = 0;
219 }
220 }
John Koleszarc6b90392012-07-13 15:21:29 -0700221 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400222
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800223 if (!feof(infile)) {
224 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
225 warn("Failed to read full frame\n");
226 return 1;
227 }
228 *bytes_read = frame_size;
229 }
230
231 return 0;
232}
233
Yaowu Xuf883b422016-08-30 14:01:10 -0700234static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800235 size_t *bytes_in_buffer, size_t *buffer_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700236 switch (input->aom_input_ctx->file_type) {
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700237#if CONFIG_WEBM_IO
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800238 case FILE_TYPE_WEBM:
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700239 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700240#endif
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800241 case FILE_TYPE_RAW:
Yaowu Xuf883b422016-08-30 14:01:10 -0700242 return raw_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700243 buffer_size);
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800244 case FILE_TYPE_IVF:
Yaowu Xuf883b422016-08-30 14:01:10 -0700245 return ivf_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700246 buffer_size);
Cyril Concolato6c788832017-10-30 16:30:35 -0700247#if CONFIG_OBU_NO_IVF
248 case FILE_TYPE_OBU:
249 return obu_read_temporal_unit(input->aom_input_ctx->file, buf,
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000250#if CONFIG_SCALABILITY
251 bytes_in_buffer, buffer_size, 0);
252#else
Cyril Concolato6c788832017-10-30 16:30:35 -0700253 bytes_in_buffer, buffer_size);
254#endif
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000255#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700256 default: return 1;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800257 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400258}
259
Yaowu Xuf883b422016-08-30 14:01:10 -0700260static void update_image_md5(const aom_image_t *img, const int planes[3],
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800261 MD5Context *md5) {
262 int i, y;
263
264 for (i = 0; i < 3; ++i) {
265 const int plane = planes[i];
266 const unsigned char *buf = img->planes[plane];
267 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700268 const int w = aom_img_plane_width(img, plane) *
269 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
270 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800271
272 for (y = 0; y < h; ++y) {
273 MD5Update(md5, buf, w);
274 buf += stride;
275 }
276 }
277}
278
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000279static void write_image_file(const aom_image_t *img, const int *planes,
280 const int num_planes, FILE *file) {
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800281 int i, y;
Yaowu Xuf883b422016-08-30 14:01:10 -0700282 const int bytes_per_sample = ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800283
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000284 for (i = 0; i < num_planes; ++i) {
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800285 const int plane = planes[i];
286 const unsigned char *buf = img->planes[plane];
287 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700288 const int w = aom_img_plane_width(img, plane);
289 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800290
291 for (y = 0; y < h; ++y) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700292 fwrite(buf, bytes_per_sample, w, file);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800293 buf += stride;
294 }
John Koleszarc6b90392012-07-13 15:21:29 -0700295 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400296}
297
Yaowu Xuf883b422016-08-30 14:01:10 -0700298static int file_is_raw(struct AvxInputContext *input) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800299 uint8_t buf[32];
John Koleszarc6b90392012-07-13 15:21:29 -0700300 int is_raw = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700301 aom_codec_stream_info_t si;
John Koleszarcfe3f912010-10-20 10:49:12 -0400302
Tom Finegan00a35aa2013-11-14 12:37:42 -0800303 if (fread(buf, 1, 32, input->file) == 32) {
John Koleszarc6b90392012-07-13 15:21:29 -0700304 int i;
John Koleszarcfe3f912010-10-20 10:49:12 -0400305
Tom Finegan00a35aa2013-11-14 12:37:42 -0800306 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700307 for (i = 0; i < get_aom_decoder_count(); ++i) {
308 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
309 if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
clang-format6c4d83e2016-08-08 19:03:30 -0700310 32 - 4, &si)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700311 is_raw = 1;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800312 input->fourcc = decoder->fourcc;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800313 input->width = si.w;
314 input->height = si.h;
315 input->framerate.numerator = 30;
316 input->framerate.denominator = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700317 break;
318 }
Tom Finegan00a35aa2013-11-14 12:37:42 -0800319 }
320 }
John Koleszarc6b90392012-07-13 15:21:29 -0700321 }
John Koleszarcfe3f912010-10-20 10:49:12 -0400322
Tom Finegan00a35aa2013-11-14 12:37:42 -0800323 rewind(input->file);
John Koleszarc6b90392012-07-13 15:21:29 -0700324 return is_raw;
John Koleszarcfe3f912010-10-20 10:49:12 -0400325}
326
James Zern6a2e0f02015-05-09 10:42:31 -0700327static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
James Yu6d151322014-02-22 14:51:11 +0800328 fprintf(stderr,
clang-format6c4d83e2016-08-08 19:03:30 -0700329 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
John Koleszarc6b90392012-07-13 15:21:29 -0700330 frame_in, frame_out, dx_time,
James Yu6d151322014-02-22 14:51:11 +0800331 (double)frame_out * 1000000.0 / (double)dx_time);
John Koleszar5d12e042010-10-21 17:28:34 -0400332}
333
Frank Galligana4f30a52014-02-06 17:13:08 -0800334struct ExternalFrameBuffer {
clang-format6c4d83e2016-08-08 19:03:30 -0700335 uint8_t *data;
Frank Galligana4f30a52014-02-06 17:13:08 -0800336 size_t size;
337 int in_use;
338};
339
340struct ExternalFrameBufferList {
341 int num_external_frame_buffers;
342 struct ExternalFrameBuffer *ext_fb;
343};
344
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345// Callback used by libaom to request an external frame buffer. |cb_priv|
Frank Galligana4f30a52014-02-06 17:13:08 -0800346// Application private data passed into the set function. |min_size| is the
347// minimum size in bytes needed to decode the next frame. |fb| pointer to the
348// frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700349static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
350 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800351 int i;
352 struct ExternalFrameBufferList *const ext_fb_list =
353 (struct ExternalFrameBufferList *)cb_priv;
clang-format6c4d83e2016-08-08 19:03:30 -0700354 if (ext_fb_list == NULL) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800355
356 // Find a free frame buffer.
357 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
clang-format6c4d83e2016-08-08 19:03:30 -0700358 if (!ext_fb_list->ext_fb[i].in_use) break;
Frank Galligana4f30a52014-02-06 17:13:08 -0800359 }
360
clang-format6c4d83e2016-08-08 19:03:30 -0700361 if (i == ext_fb_list->num_external_frame_buffers) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800362
363 if (ext_fb_list->ext_fb[i].size < min_size) {
364 free(ext_fb_list->ext_fb[i].data);
Frank Galligan175d9df2014-09-11 08:39:38 -0700365 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
clang-format6c4d83e2016-08-08 19:03:30 -0700366 if (!ext_fb_list->ext_fb[i].data) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800367
368 ext_fb_list->ext_fb[i].size = min_size;
369 }
370
371 fb->data = ext_fb_list->ext_fb[i].data;
372 fb->size = ext_fb_list->ext_fb[i].size;
373 ext_fb_list->ext_fb[i].in_use = 1;
374
375 // Set the frame buffer's private data to point at the external frame buffer.
376 fb->priv = &ext_fb_list->ext_fb[i];
377 return 0;
378}
379
Yaowu Xuc27fc142016-08-22 16:08:15 -0700380// Callback used by libaom when there are no references to the frame buffer.
Frank Galligana4f30a52014-02-06 17:13:08 -0800381// |cb_priv| user private data passed into the set function. |fb| pointer
382// to the frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700383static int release_av1_frame_buffer(void *cb_priv,
384 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800385 struct ExternalFrameBuffer *const ext_fb =
386 (struct ExternalFrameBuffer *)fb->priv;
387 (void)cb_priv;
388 ext_fb->in_use = 0;
389 return 0;
390}
391
James Zern6a2e0f02015-05-09 10:42:31 -0700392static void generate_filename(const char *pattern, char *out, size_t q_len,
393 unsigned int d_w, unsigned int d_h,
394 unsigned int frame_in) {
John Koleszarc6b90392012-07-13 15:21:29 -0700395 const char *p = pattern;
396 char *q = out;
John Koleszar933d44b2010-10-21 20:40:42 -0700397
John Koleszarc6b90392012-07-13 15:21:29 -0700398 do {
399 char *next_pat = strchr(p, '%');
John Koleszar933d44b2010-10-21 20:40:42 -0700400
John Koleszarc6b90392012-07-13 15:21:29 -0700401 if (p == next_pat) {
402 size_t pat_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700403
John Koleszar3707c342012-11-06 10:09:36 -0800404 /* parse the pattern */
John Koleszarc6b90392012-07-13 15:21:29 -0700405 q[q_len - 1] = '\0';
406 switch (p[1]) {
clang-format6c4d83e2016-08-08 19:03:30 -0700407 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
408 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
409 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
410 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
411 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
412 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
413 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
414 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
415 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
416 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
417 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
418 default: die("Unrecognized pattern %%%c\n", p[1]); break;
John Koleszarc6b90392012-07-13 15:21:29 -0700419 }
John Koleszar933d44b2010-10-21 20:40:42 -0700420
John Koleszarc6b90392012-07-13 15:21:29 -0700421 pat_len = strlen(q);
clang-format6c4d83e2016-08-08 19:03:30 -0700422 if (pat_len >= q_len - 1) die("Output filename too long.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700423 q += pat_len;
424 p += 2;
425 q_len -= pat_len;
426 } else {
427 size_t copy_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700428
John Koleszar3707c342012-11-06 10:09:36 -0800429 /* copy the next segment */
John Koleszarc6b90392012-07-13 15:21:29 -0700430 if (!next_pat)
431 copy_len = strlen(p);
432 else
433 copy_len = next_pat - p;
John Koleszar933d44b2010-10-21 20:40:42 -0700434
clang-format6c4d83e2016-08-08 19:03:30 -0700435 if (copy_len >= q_len - 1) die("Output filename too long.\n");
John Koleszar933d44b2010-10-21 20:40:42 -0700436
John Koleszarc6b90392012-07-13 15:21:29 -0700437 memcpy(q, p, copy_len);
438 q[copy_len] = '\0';
439 q += copy_len;
440 p += copy_len;
441 q_len -= copy_len;
442 }
443 } while (*p);
John Koleszar933d44b2010-10-21 20:40:42 -0700444}
445
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800446static int is_single_file(const char *outfile_pattern) {
447 const char *p = outfile_pattern;
448
449 do {
450 p = strchr(p, '%');
451 if (p && p[1] >= '1' && p[1] <= '9')
452 return 0; // pattern contains sequence number, so it's not unique
clang-format6c4d83e2016-08-08 19:03:30 -0700453 if (p) p++;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800454 } while (p);
455
456 return 1;
457}
458
459static void print_md5(unsigned char digest[16], const char *filename) {
460 int i;
461
clang-format6c4d83e2016-08-08 19:03:30 -0700462 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800463 printf(" %s\n", filename);
464}
465
466static FILE *open_outfile(const char *name) {
467 if (strcmp("-", name) == 0) {
468 set_binary_mode(stdout);
469 return stdout;
470 } else {
471 FILE *file = fopen(name, "wb");
clang-format6c4d83e2016-08-08 19:03:30 -0700472 if (!file) fatal("Failed to open output file '%s'", name);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800473 return file;
474 }
475}
476
Yaowu Xuf883b422016-08-30 14:01:10 -0700477static int img_shifted_realloc_required(const aom_image_t *img,
478 const aom_image_t *shifted,
479 aom_img_fmt_t required_fmt) {
clang-format6c4d83e2016-08-08 19:03:30 -0700480 return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700481 required_fmt != shifted->fmt;
482}
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700483
James Zern6a2e0f02015-05-09 10:42:31 -0700484static int main_loop(int argc, const char **argv_) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700485 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700486 char *fn = NULL;
487 int i;
James Zern360e4442016-11-16 21:53:55 -0800488 int ret = EXIT_FAILURE;
clang-format6c4d83e2016-08-08 19:03:30 -0700489 uint8_t *buf = NULL;
490 size_t bytes_in_buffer = 0, buffer_size = 0;
491 FILE *infile;
492 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800493 int do_md5 = 0, progress = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700494 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
495 int arg_skip = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700496 int keep_going = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700497 const AvxInterface *interface = NULL;
498 const AvxInterface *fourcc_interface = NULL;
James Yu6d151322014-02-22 14:51:11 +0800499 uint64_t dx_time = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700500 struct arg arg;
501 char **argv, **argi, **argj;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800502
clang-format6c4d83e2016-08-08 19:03:30 -0700503 int single_file;
504 int use_y4m = 1;
505 int opt_yv12 = 0;
506 int opt_i420 = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800507 int opt_raw = 0;
Debargha Mukherjee2ccf4b92018-02-27 17:30:46 -0800508 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, { 1 } };
clang-format6c4d83e2016-08-08 19:03:30 -0700509 unsigned int output_bit_depth = 0;
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800510 unsigned int tile_mode = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700511 int tile_row = -1;
512 int tile_col = -1;
clang-format6c4d83e2016-08-08 19:03:30 -0700513 int frames_corrupted = 0;
514 int dec_flags = 0;
515 int do_scale = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700516 aom_image_t *scaled_img = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700517 aom_image_t *img_shifted = NULL;
clang-format6c4d83e2016-08-08 19:03:30 -0700518 int frame_avail, got_data, flush_decoder = 0;
519 int num_external_frame_buffers = 0;
520 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400521
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800522 const char *outfile_pattern = NULL;
clang-format6c4d83e2016-08-08 19:03:30 -0700523 char outfile_name[PATH_MAX] = { 0 };
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800524 FILE *outfile = NULL;
525
Peter Boströma1f64322017-01-19 11:35:30 -0500526 FILE *framestats_file = NULL;
527
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800528 MD5Context md5_ctx;
529 unsigned char md5_digest[16];
530
Yaowu Xuf883b422016-08-30 14:01:10 -0700531 struct AvxDecInputContext input = { NULL, NULL };
532 struct AvxInputContext aom_input_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700533#if CONFIG_WEBM_IO
Jim Bankoski7ae32692014-08-19 06:44:10 -0700534 struct WebmInputContext webm_ctx;
535 memset(&(webm_ctx), 0, sizeof(webm_ctx));
Tom Finegan2abe2d42013-11-18 14:39:51 -0800536 input.webm_ctx = &webm_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700537#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700538 input.aom_input_ctx = &aom_input_ctx;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800539
John Koleszarc6b90392012-07-13 15:21:29 -0700540 /* Parse command line */
541 exec_name = argv_[0];
542 argv = argv_dup(argc - 1, argv_ + 1);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400543
John Koleszarc6b90392012-07-13 15:21:29 -0700544 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
545 memset(&arg, 0, sizeof(arg));
546 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400547
James Zernfd8c78c2017-11-27 16:37:33 -0800548 if (arg_match(&arg, &help, argi)) {
549 show_help(stdout, 0);
550 exit(EXIT_SUCCESS);
551 } else if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700552 interface = get_aom_decoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800553 if (!interface)
554 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszar9596a4c2013-06-12 16:08:35 -0700555 } else if (arg_match(&arg, &looparg, argi)) {
556 // no-op
Peter Boströma1f64322017-01-19 11:35:30 -0500557 } else if (arg_match(&arg, &outputfile, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700558 outfile_pattern = arg.val;
Peter Boströma1f64322017-01-19 11:35:30 -0500559 } else if (arg_match(&arg, &use_yv12, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700560 use_y4m = 0;
561 flipuv = 1;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700562 opt_yv12 = 1;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800563 opt_i420 = 0;
564 opt_raw = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700565 } else if (arg_match(&arg, &use_i420, argi)) {
566 use_y4m = 0;
567 flipuv = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800568 opt_yv12 = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700569 opt_i420 = 1;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800570 opt_raw = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700571 } else if (arg_match(&arg, &rawvideo, argi)) {
572 use_y4m = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800573 opt_yv12 = 0;
574 opt_i420 = 0;
575 opt_raw = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500576 } else if (arg_match(&arg, &flipuvarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700577 flipuv = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500578 } else if (arg_match(&arg, &noblitarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700579 noblit = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500580 } else if (arg_match(&arg, &progressarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700581 progress = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500582 } else if (arg_match(&arg, &limitarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700583 stop_after = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500584 } else if (arg_match(&arg, &skiparg, argi)) {
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700585 arg_skip = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500586 } else if (arg_match(&arg, &postprocarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700587 postproc = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500588 } else if (arg_match(&arg, &md5arg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700589 do_md5 = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500590 } else if (arg_match(&arg, &framestatsarg, argi)) {
591 framestats_file = fopen(arg.val, "w");
592 if (!framestats_file) {
593 die("Error: Could not open --framestats file (%s) for writing.\n",
594 arg.val);
595 }
596 } else if (arg_match(&arg, &summaryarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700597 summary = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500598 } else if (arg_match(&arg, &threadsarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700599 cfg.threads = arg_parse_uint(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800600 } else if (arg_match(&arg, &verbosearg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700601 quiet = 0;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800602 } else if (arg_match(&arg, &scalearg, argi)) {
John Koleszar7d8fc262013-02-25 16:58:47 -0800603 do_scale = 1;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800604 } else if (arg_match(&arg, &fb_arg, argi)) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800605 num_external_frame_buffers = arg_parse_uint(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800606 } else if (arg_match(&arg, &continuearg, argi)) {
James Zern7690d882014-08-22 18:01:28 -0700607 keep_going = 1;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800608 } else if (arg_match(&arg, &outbitdeptharg, argi)) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700609 output_bit_depth = arg_parse_uint(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800610 } else if (arg_match(&arg, &tilem, argi)) {
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800611 tile_mode = arg_parse_int(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800612 } else if (arg_match(&arg, &tiler, argi)) {
Geza Lore490ba1a2016-03-11 17:42:49 +0000613 tile_row = arg_parse_int(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800614 } else if (arg_match(&arg, &tilec, argi)) {
Geza Lore490ba1a2016-03-11 17:42:49 +0000615 tile_col = arg_parse_int(&arg);
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800616 } else {
John Koleszarc6b90392012-07-13 15:21:29 -0700617 argj++;
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800618 }
John Koleszarc6b90392012-07-13 15:21:29 -0700619 }
620
621 /* Check for unrecognized options */
622 for (argi = argv; *argi; argi++)
623 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
624 die("Error: Unrecognized option %s\n", *argi);
625
626 /* Handle non-option arguments */
627 fn = argv[0];
628
Hanno Böckb5d0a202015-02-24 15:48:21 -0800629 if (!fn) {
630 free(argv);
James Zernfd8c78c2017-11-27 16:37:33 -0800631 fprintf(stderr, "No input file specified!\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700632 usage_exit();
Hanno Böckb5d0a202015-02-24 15:48:21 -0800633 }
John Koleszarc6b90392012-07-13 15:21:29 -0700634 /* Open file */
635 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
636
637 if (!infile) {
James Zern6b993f02015-02-05 21:06:29 -0800638 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
John Koleszarc6b90392012-07-13 15:21:29 -0700639 }
640#if CONFIG_OS_SUPPORT
641 /* Make sure we don't dump to the terminal, unless forced to with -o - */
James Zernb9b28382016-12-09 16:50:39 -0800642 if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700643 fprintf(stderr,
644 "Not dumping raw video to your terminal. Use '-o -' to "
645 "override.\n");
646 return EXIT_FAILURE;
647 }
648#endif
Tom Fineganccb8cc42018-02-22 14:08:13 -0800649 input.aom_input_ctx->filename = fn;
Yaowu Xuf883b422016-08-30 14:01:10 -0700650 input.aom_input_ctx->file = infile;
651 if (file_is_ivf(input.aom_input_ctx))
652 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700653#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
655 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700656#endif
Cyril Concolato6c788832017-10-30 16:30:35 -0700657#if CONFIG_OBU_NO_IVF
658 else if (file_is_obu(input.aom_input_ctx))
659 input.aom_input_ctx->file_type = FILE_TYPE_OBU;
660#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700661 else if (file_is_raw(input.aom_input_ctx))
662 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700663 else {
664 fprintf(stderr, "Unrecognized input file type.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700665#if !CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700666 fprintf(stderr, "aomdec was built without WebM container support.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700667#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700668 return EXIT_FAILURE;
669 }
670
John Koleszarc6b90392012-07-13 15:21:29 -0700671 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800672 single_file = is_single_file(outfile_pattern);
John Koleszarc6b90392012-07-13 15:21:29 -0700673
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800674 if (!noblit && single_file) {
675 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
Yaowu Xuf883b422016-08-30 14:01:10 -0700676 aom_input_ctx.width, aom_input_ctx.height, 0);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800677 if (do_md5)
678 MD5Init(&md5_ctx);
679 else
680 outfile = open_outfile(outfile_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700681 }
682
683 if (use_y4m && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700684 if (!single_file) {
clang-format6c4d83e2016-08-08 19:03:30 -0700685 fprintf(stderr,
686 "YUV4MPEG2 not supported with output patterns,"
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700687 " try --i420 or --yv12 or --rawvideo.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700688 return EXIT_FAILURE;
689 }
690
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700691#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700692 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
693 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
clang-format6c4d83e2016-08-08 19:03:30 -0700694 fprintf(stderr,
695 "Failed to guess framerate -- error parsing "
John Koleszarc6b90392012-07-13 15:21:29 -0700696 "webm file?\n");
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400697 return EXIT_FAILURE;
John Koleszarc6b90392012-07-13 15:21:29 -0700698 }
Dmitry Kovalev5ab63582014-01-17 17:02:37 -0800699 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700700#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700701 }
702
Yaowu Xuf883b422016-08-30 14:01:10 -0700703 fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800704 if (interface && fourcc_interface && interface != fourcc_interface)
705 warn("Header indicates codec: %s\n", fourcc_interface->name);
706 else
707 interface = fourcc_interface;
John Koleszarc6b90392012-07-13 15:21:29 -0700708
Yaowu Xuf883b422016-08-30 14:01:10 -0700709 if (!interface) interface = get_aom_decoder_by_index(0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400710
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800711 dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0);
Yaowu Xuf883b422016-08-30 14:01:10 -0700712 if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
clang-format6c4d83e2016-08-08 19:03:30 -0700713 dec_flags)) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800714 fprintf(stderr, "Failed to initialize decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700715 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800716 goto fail2;
John Koleszarc6b90392012-07-13 15:21:29 -0700717 }
John Koleszar933d44b2010-10-21 20:40:42 -0700718
clang-format6c4d83e2016-08-08 19:03:30 -0700719 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400720
Yaowu Xudd6ef6c2018-03-02 16:43:52 -0800721#if CONFIG_AV1_DECODER
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800722 if (aom_codec_control(&decoder, AV1_SET_TILE_MODE, tile_mode)) {
723 fprintf(stderr, "Failed to set decode_tile_mode: %s\n",
724 aom_codec_error(&decoder));
725 goto fail;
726 }
727
Jingning Hanf79d48d2016-12-20 14:52:35 -0800728 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_ROW, tile_row)) {
729 fprintf(stderr, "Failed to set decode_tile_row: %s\n",
730 aom_codec_error(&decoder));
731 goto fail;
732 }
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700733
Jingning Hanf79d48d2016-12-20 14:52:35 -0800734 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_COL, tile_col)) {
735 fprintf(stderr, "Failed to set decode_tile_col: %s\n",
736 aom_codec_error(&decoder));
737 goto fail;
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700738 }
739#endif
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700740
clang-format6c4d83e2016-08-08 19:03:30 -0700741 if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700742 while (arg_skip) {
clang-format6c4d83e2016-08-08 19:03:30 -0700743 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700744 arg_skip--;
745 }
746
Frank Galligana4f30a52014-02-06 17:13:08 -0800747 if (num_external_frame_buffers > 0) {
748 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
749 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
750 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
752 release_av1_frame_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700753 &ext_fb_list)) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800754 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800756 goto fail;
Frank Galligana4f30a52014-02-06 17:13:08 -0800757 }
758 }
759
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400760 frame_avail = 1;
761 got_data = 0;
762
Peter Boströma1f64322017-01-19 11:35:30 -0500763 if (framestats_file) fprintf(framestats_file, "bytes,qp\r\n");
764
John Koleszarc6b90392012-07-13 15:21:29 -0700765 /* Decode file */
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400766 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700767 aom_codec_iter_t iter = NULL;
768 aom_image_t *img;
769 struct aom_usec_timer timer;
clang-format6c4d83e2016-08-08 19:03:30 -0700770 int corrupted = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400771
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400772 frame_avail = 0;
773 if (!stop_after || frame_in < stop_after) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800774 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400775 frame_avail = 1;
776 frame_in++;
777
Yaowu Xuf883b422016-08-30 14:01:10 -0700778 aom_usec_timer_start(&timer);
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400779
Sean DuBois47cc2552018-01-23 07:44:16 +0000780 if (aom_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer,
781 NULL)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700782 const char *detail = aom_codec_error_detail(&decoder);
clang-format6c4d83e2016-08-08 19:03:30 -0700783 warn("Failed to decode frame %d: %s", frame_in,
Yaowu Xuf883b422016-08-30 14:01:10 -0700784 aom_codec_error(&decoder));
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400785
clang-format6c4d83e2016-08-08 19:03:30 -0700786 if (detail) warn("Additional information: %s", detail);
787 if (!keep_going) goto fail;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400788 }
789
Peter Boströma1f64322017-01-19 11:35:30 -0500790 if (framestats_file) {
791 int qp;
792 if (aom_codec_control(&decoder, AOMD_GET_LAST_QUANTIZER, &qp)) {
793 warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
794 aom_codec_error(&decoder));
795 if (!keep_going) goto fail;
796 }
797 fprintf(framestats_file, "%d,%d\r\n", (int)bytes_in_buffer, qp);
798 }
799
Yaowu Xuf883b422016-08-30 14:01:10 -0700800 aom_usec_timer_mark(&timer);
801 dx_time += aom_usec_timer_elapsed(&timer);
hkuang1f841c32015-03-06 13:03:18 -0800802 } else {
803 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400804 }
hkuang1f841c32015-03-06 13:03:18 -0800805 } else {
806 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400807 }
808
Yaowu Xuf883b422016-08-30 14:01:10 -0700809 aom_usec_timer_start(&timer);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400810
hkuang1f841c32015-03-06 13:03:18 -0800811 if (flush_decoder) {
812 // Flush the decoder in frame parallel decode.
Sean DuBois47cc2552018-01-23 07:44:16 +0000813 if (aom_codec_decode(&decoder, NULL, 0, NULL)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700814 warn("Failed to flush decoder: %s", aom_codec_error(&decoder));
hkuang1f841c32015-03-06 13:03:18 -0800815 }
816 }
817
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400818 got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700819 if ((img = aom_codec_get_frame(&decoder, &iter))) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400820 ++frame_out;
821 got_data = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400822 }
823
Yaowu Xuf883b422016-08-30 14:01:10 -0700824 aom_usec_timer_mark(&timer);
825 dx_time += (unsigned int)aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -0700826
Debargha Mukherjeef9f2d892018-03-08 01:09:36 -0800827 if (aom_codec_control(&decoder, AOMD_GET_FRAME_CORRUPTED, &corrupted)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700828 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder));
clang-format6c4d83e2016-08-08 19:03:30 -0700829 if (!keep_going) goto fail;
John Koleszarc6b90392012-07-13 15:21:29 -0700830 }
831 frames_corrupted += corrupted;
832
clang-format6c4d83e2016-08-08 19:03:30 -0700833 if (progress) show_progress(frame_in, frame_out, dx_time);
John Koleszarc6b90392012-07-13 15:21:29 -0700834
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800835 if (!noblit && img) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700836 const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
837 const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800838 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
John Koleszarda584362013-05-06 15:52:06 -0700839
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800840 if (do_scale) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800841 if (frame_out == 1) {
842 // If the output frames are to be scaled to a fixed display size then
843 // use the width and height specified in the container. If either of
844 // these is set to 0, use the display size set in the first frame
Adrian Grange82f6af62014-01-22 09:36:07 -0800845 // header. If that is unavailable, use the raw decoded size of the
846 // first decoded frame.
Yaowu Xuf883b422016-08-30 14:01:10 -0700847 int render_width = aom_input_ctx.width;
848 int render_height = aom_input_ctx.height;
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400849 if (!render_width || !render_height) {
850 int render_size[2];
Yaowu Xuf883b422016-08-30 14:01:10 -0700851 if (aom_codec_control(&decoder, AV1D_GET_DISPLAY_SIZE,
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400852 render_size)) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800853 // As last resort use size of first frame as display size.
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400854 render_width = img->d_w;
855 render_height = img->d_h;
Adrian Grange482c39c2013-12-13 10:15:03 -0800856 } else {
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400857 render_width = render_size[0];
858 render_height = render_size[1];
Adrian Grange482c39c2013-12-13 10:15:03 -0800859 }
Adrian Grange2117fe02013-11-19 14:01:44 -0800860 }
clang-format6c4d83e2016-08-08 19:03:30 -0700861 scaled_img =
Yaowu Xuf883b422016-08-30 14:01:10 -0700862 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
Deb Mukherjee449e5f22014-07-11 11:43:31 -0700863 scaled_img->bit_depth = img->bit_depth;
John Koleszar7d8fc262013-02-25 16:58:47 -0800864 }
Adrian Grange482c39c2013-12-13 10:15:03 -0800865
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800866 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
Deb Mukherjee47031c02014-05-16 18:52:01 -0700867#if CONFIG_LIBYUV
Tom Finegan8ef2ce82014-12-12 11:57:04 -0800868 libyuv_scale(img, scaled_img, kFilterBox);
John Koleszar7d8fc262013-02-25 16:58:47 -0800869 img = scaled_img;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700870#else
clang-format6c4d83e2016-08-08 19:03:30 -0700871 fprintf(stderr,
Johanne07a6752018-01-10 12:47:44 -0800872 "Failed to scale output frame: %s.\n"
873 "libyuv is required for scaling but is currently disabled.\n"
874 "Be sure to specify -DCONFIG_LIBYUV=1 when running cmake.\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700875 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800876 goto fail;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700877#endif
John Koleszar7d8fc262013-02-25 16:58:47 -0800878 }
879 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700880 // Default to codec bit depth if output bit depth not set
Ronald S. Bultjeeba342a2015-09-11 16:55:19 -0400881 if (!output_bit_depth && single_file && !do_md5) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700882 output_bit_depth = img->bit_depth;
883 }
884 // Shift up or down if necessary
Sebastien Alaiwan18605a32017-05-12 10:46:09 +0200885 if (output_bit_depth != 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700886 const aom_img_fmt_t shifted_fmt =
clang-format6c4d83e2016-08-08 19:03:30 -0700887 output_bit_depth == 8
Yaowu Xuf883b422016-08-30 14:01:10 -0700888 ? img->fmt ^ (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
889 : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
Sebastien Alaiwan18605a32017-05-12 10:46:09 +0200890
891 if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
892 if (img_shifted &&
893 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
894 aom_img_free(img_shifted);
895 img_shifted = NULL;
896 }
897 if (!img_shifted) {
898 img_shifted =
899 aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
900 img_shifted->bit_depth = output_bit_depth;
901 }
902 if (output_bit_depth > img->bit_depth) {
903 aom_img_upshift(img_shifted, img,
904 output_bit_depth - img->bit_depth);
905 } else {
906 aom_img_downshift(img_shifted, img,
907 img->bit_depth - output_bit_depth);
908 }
909 img = img_shifted;
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700910 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700911 }
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800912
Yaowu Xuf883b422016-08-30 14:01:10 -0700913 aom_input_ctx.width = img->d_w;
914 aom_input_ctx.height = img->d_h;
Geza Lore490ba1a2016-03-11 17:42:49 +0000915
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800916#if CONFIG_MONO_VIDEO
917 int num_planes = (!use_y4m && opt_raw && img->monochrome) ? 1 : 3;
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000918#else
919 int num_planes = 3;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800920 (void)opt_raw;
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000921#endif
922
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800923 if (single_file) {
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800924 if (use_y4m) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700925 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800926 size_t len = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700927 if (img->fmt == AOM_IMG_FMT_I440 || img->fmt == AOM_IMG_FMT_I44016) {
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700928 fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
929 goto fail;
930 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800931 if (frame_out == 1) {
932 // Y4M file header
clang-format6c4d83e2016-08-08 19:03:30 -0700933 len = y4m_write_file_header(
Urvang Joshi4145bf02016-10-17 14:53:33 -0700934 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
935 aom_input_ctx.height, &aom_input_ctx.framerate, img->fmt,
936 img->bit_depth);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800937 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700938 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800939 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700940 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800941 }
942 }
943
944 // Y4M frame header
Urvang Joshi4145bf02016-10-17 14:53:33 -0700945 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
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 }
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700951 } else {
952 if (frame_out == 1) {
953 // Check if --yv12 or --i420 options are consistent with the
954 // bit-stream decoded
955 if (opt_i420) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700956 if (img->fmt != AOM_IMG_FMT_I420 &&
957 img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700958 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
959 goto fail;
960 }
961 }
962 if (opt_yv12) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700963 if ((img->fmt != AOM_IMG_FMT_I420 &&
964 img->fmt != AOM_IMG_FMT_YV12) ||
clang-format6c4d83e2016-08-08 19:03:30 -0700965 img->bit_depth != 8) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700966 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
967 goto fail;
968 }
969 }
970 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800971 }
972
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800973 if (do_md5) {
974 update_image_md5(img, planes, &md5_ctx);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800975 } else {
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000976 write_image_file(img, planes, num_planes, outfile);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800977 }
978 } else {
clang-format6c4d83e2016-08-08 19:03:30 -0700979 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
980 img->d_h, frame_in);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800981 if (do_md5) {
982 MD5Init(&md5_ctx);
983 update_image_md5(img, planes, &md5_ctx);
984 MD5Final(md5_digest, &md5_ctx);
985 print_md5(md5_digest, outfile_name);
986 } else {
987 outfile = open_outfile(outfile_name);
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000988 write_image_file(img, planes, num_planes, outfile);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800989 fclose(outfile);
John Koleszarc6b90392012-07-13 15:21:29 -0700990 }
John Koleszarc6b90392012-07-13 15:21:29 -0700991 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400992 }
John Koleszarc6b90392012-07-13 15:21:29 -0700993 }
994
995 if (summary || progress) {
996 show_progress(frame_in, frame_out, dx_time);
997 fprintf(stderr, "\n");
998 }
999
James Zern360e4442016-11-16 21:53:55 -08001000 if (frames_corrupted) {
John Koleszarc6b90392012-07-13 15:21:29 -07001001 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
James Zern360e4442016-11-16 21:53:55 -08001002 } else {
1003 ret = EXIT_SUCCESS;
1004 }
John Koleszar4226f0c2011-04-27 12:04:48 -04001005
John Koleszar0ea50ce2010-05-18 11:58:33 -04001006fail:
1007
Yaowu Xuf883b422016-08-30 14:01:10 -07001008 if (aom_codec_destroy(&decoder)) {
Adrian Granged427fab2013-11-21 17:19:04 -08001009 fprintf(stderr, "Failed to destroy decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001010 aom_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -07001011 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001012
James Zern360e4442016-11-16 21:53:55 -08001013fail2:
1014
Dmitry Kovalev347b3062014-01-22 09:04:07 -08001015 if (!noblit && single_file) {
1016 if (do_md5) {
1017 MD5Final(md5_digest, &md5_ctx);
1018 print_md5(md5_digest, outfile_name);
1019 } else {
1020 fclose(outfile);
1021 }
1022 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001023
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001024#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -07001025 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
Tom Finegan2abe2d42013-11-18 14:39:51 -08001026 webm_free(input.webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001027#endif
1028
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}