blob: acfc7ce6b032cf88eb63f45129f2591df89cc574 [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
46#include "./tools_common.h"
James Zernb6cce762014-05-10 17:44:12 -070047#if CONFIG_WEBM_IO
Tom Finegan2abe2d42013-11-18 14:39:51 -080048#include "./webmdec.h"
James Zernb6cce762014-05-10 17:44:12 -070049#endif
Dmitry Kovalev5ab63582014-01-17 17:02:37 -080050#include "./y4menc.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040051
52static const char *exec_name;
53
Yaowu Xuf883b422016-08-30 14:01:10 -070054struct AvxDecInputContext {
55 struct AvxInputContext *aom_input_ctx;
Tom Finegan2abe2d42013-11-18 14:39:51 -080056 struct WebmInputContext *webm_ctx;
57};
58
clang-format6c4d83e2016-08-08 19:03:30 -070059static const arg_def_t looparg =
60 ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
61static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
62static const arg_def_t use_yv12 =
63 ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
64static const arg_def_t use_i420 =
65 ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
66static const arg_def_t flipuvarg =
67 ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
68static const arg_def_t rawvideo =
69 ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
70static const arg_def_t noblitarg =
71 ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
72static const arg_def_t progressarg =
73 ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
74static const arg_def_t limitarg =
75 ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
76static const arg_def_t skiparg =
77 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
78static const arg_def_t postprocarg =
79 ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
80static const arg_def_t summaryarg =
81 ARG_DEF(NULL, "summary", 0, "Show timing summary");
82static const arg_def_t outputfile =
83 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
84static const arg_def_t threadsarg =
85 ARG_DEF("t", "threads", 1, "Max threads to use");
86static const arg_def_t frameparallelarg =
87 ARG_DEF(NULL, "frame-parallel", 0, "Frame parallel decode");
88static const arg_def_t verbosearg =
89 ARG_DEF("v", "verbose", 0, "Show version string");
90static const arg_def_t error_concealment =
91 ARG_DEF(NULL, "error-concealment", 0, "Enable decoder error-concealment");
92static const arg_def_t scalearg =
93 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
94static const arg_def_t continuearg =
95 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
96static const arg_def_t fb_arg =
97 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
98static const arg_def_t md5arg =
99 ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
Peter Boströma1f64322017-01-19 11:35:30 -0500100static const arg_def_t framestatsarg =
101 ARG_DEF(NULL, "framestats", 1, "Output per-frame stats (.csv format)");
Yaowu Xuf883b422016-08-30 14:01:10 -0700102#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700103static const arg_def_t outbitdeptharg =
104 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700105#endif
Geza Lore490ba1a2016-03-11 17:42:49 +0000106#if CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700107static const arg_def_t tiler = ARG_DEF(NULL, "tile-row", 1,
108 "Row index of tile to decode "
109 "(-1 for all rows)");
110static const arg_def_t tilec = ARG_DEF(NULL, "tile-column", 1,
111 "Column index of tile to decode "
112 "(-1 for all columns)");
Geza Lore490ba1a2016-03-11 17:42:49 +0000113#endif // CONFIG_EXT_TILE
Dmitry Kovalevd24f4e42013-12-27 16:10:18 -0800114
clang-format6c4d83e2016-08-08 19:03:30 -0700115static const arg_def_t *all_args[] = { &codecarg,
116 &use_yv12,
117 &use_i420,
118 &flipuvarg,
119 &rawvideo,
120 &noblitarg,
121 &progressarg,
122 &limitarg,
123 &skiparg,
124 &postprocarg,
125 &summaryarg,
126 &outputfile,
127 &threadsarg,
128 &frameparallelarg,
129 &verbosearg,
130 &scalearg,
131 &fb_arg,
132 &md5arg,
Peter Boströma1f64322017-01-19 11:35:30 -0500133 &framestatsarg,
clang-format6c4d83e2016-08-08 19:03:30 -0700134 &error_concealment,
135 &continuearg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700136#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700137 &outbitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700138#endif
Geza Lore490ba1a2016-03-11 17:42:49 +0000139#if CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700140 &tiler,
141 &tilec,
Geza Lore490ba1a2016-03-11 17:42:49 +0000142#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700143 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400144
James Zern1e681d92014-08-15 21:00:09 -0700145#if CONFIG_LIBYUV
Yaowu Xuf883b422016-08-30 14:01:10 -0700146static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
clang-format6c4d83e2016-08-08 19:03:30 -0700147 FilterModeEnum mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700148#if CONFIG_AOM_HIGHBITDEPTH
149 if (src->fmt == AOM_IMG_FMT_I42016) {
150 assert(dst->fmt == AOM_IMG_FMT_I42016);
clang-format6c4d83e2016-08-08 19:03:30 -0700151 return I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -0700152 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
153 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
154 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
155 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
156 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
157 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
158 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700159 }
160#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700161 assert(src->fmt == AOM_IMG_FMT_I420);
162 assert(dst->fmt == AOM_IMG_FMT_I420);
163 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
164 src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
165 src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
166 src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
167 dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
168 dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -0700169 dst->d_h, mode);
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800170}
James Zern1e681d92014-08-15 21:00:09 -0700171#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800172
James Zern59e7a472015-05-09 10:33:26 -0700173void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700174 int i;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400175
clang-format6c4d83e2016-08-08 19:03:30 -0700176 fprintf(stderr,
177 "Usage: %s <options> filename\n\n"
178 "Options:\n",
179 exec_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700180 arg_show_usage(stderr, all_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700181 fprintf(stderr,
182 "\nOutput File Patterns:\n\n"
183 " The -o argument specifies the name of the file(s) to "
184 "write to. If the\n argument does not include any escape "
185 "characters, the output will be\n written to a single file. "
186 "Otherwise, the filename will be calculated by\n expanding "
John Koleszar3707c342012-11-06 10:09:36 -0800187 "the following escape characters:\n");
188 fprintf(stderr,
John Koleszarc6b90392012-07-13 15:21:29 -0700189 "\n\t%%w - Frame width"
190 "\n\t%%h - Frame height"
191 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
192 "\n\n Pattern arguments are only supported in conjunction "
193 "with the --yv12 and\n --i420 options. If the -o option is "
clang-format6c4d83e2016-08-08 19:03:30 -0700194 "not specified, the output will be\n directed to stdout.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700195 fprintf(stderr, "\nIncluded decoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400196
Yaowu Xuf883b422016-08-30 14:01:10 -0700197 for (i = 0; i < get_aom_decoder_count(); ++i) {
198 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700199 fprintf(stderr, " %-6s - %s\n", decoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700200 aom_codec_iface_name(decoder->codec_interface()));
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800201 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400202
John Koleszarc6b90392012-07-13 15:21:29 -0700203 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400204}
205
clang-format6c4d83e2016-08-08 19:03:30 -0700206static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
207 size_t *buffer_size) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800208 char raw_hdr[RAW_FRAME_HDR_SZ];
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800209 size_t frame_size = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700210
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800211 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
clang-format6c4d83e2016-08-08 19:03:30 -0700212 if (!feof(infile)) warn("Failed to read RAW frame size\n");
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800213 } else {
James Zern75d7ee32014-02-04 20:06:28 -0800214 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
215 const size_t kFrameTooSmallThreshold = 256 * 1024;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800216 frame_size = mem_get_le32(raw_hdr);
John Koleszarc6b90392012-07-13 15:21:29 -0700217
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800218 if (frame_size > kCorruptFrameThreshold) {
219 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
220 frame_size = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400221 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400222
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800223 if (frame_size < kFrameTooSmallThreshold) {
224 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
225 (unsigned int)frame_size);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400226 }
227
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800228 if (frame_size > *buffer_size) {
229 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
230 if (new_buf) {
231 *buffer = new_buf;
232 *buffer_size = 2 * frame_size;
233 } else {
234 warn("Failed to allocate compressed data buffer\n");
235 frame_size = 0;
236 }
237 }
John Koleszarc6b90392012-07-13 15:21:29 -0700238 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400239
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800240 if (!feof(infile)) {
241 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
242 warn("Failed to read full frame\n");
243 return 1;
244 }
245 *bytes_read = frame_size;
246 }
247
248 return 0;
249}
250
Yaowu Xuf883b422016-08-30 14:01:10 -0700251static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800252 size_t *bytes_in_buffer, size_t *buffer_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700253 switch (input->aom_input_ctx->file_type) {
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700254#if CONFIG_WEBM_IO
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800255 case FILE_TYPE_WEBM:
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700256 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700257#endif
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800258 case FILE_TYPE_RAW:
Yaowu Xuf883b422016-08-30 14:01:10 -0700259 return raw_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700260 buffer_size);
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800261 case FILE_TYPE_IVF:
Yaowu Xuf883b422016-08-30 14:01:10 -0700262 return ivf_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700263 buffer_size);
264 default: return 1;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800265 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400266}
267
Yaowu Xuf883b422016-08-30 14:01:10 -0700268static void update_image_md5(const aom_image_t *img, const int planes[3],
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800269 MD5Context *md5) {
270 int i, y;
271
272 for (i = 0; i < 3; ++i) {
273 const int plane = planes[i];
274 const unsigned char *buf = img->planes[plane];
275 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700276 const int w = aom_img_plane_width(img, plane) *
277 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
278 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800279
280 for (y = 0; y < h; ++y) {
281 MD5Update(md5, buf, w);
282 buf += stride;
283 }
284 }
285}
286
Yaowu Xuf883b422016-08-30 14:01:10 -0700287static void write_image_file(const aom_image_t *img, const int planes[3],
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800288 FILE *file) {
289 int i, y;
Yaowu Xuf883b422016-08-30 14:01:10 -0700290#if CONFIG_AOM_HIGHBITDEPTH
291 const int bytes_per_sample = ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700292#else
293 const int bytes_per_sample = 1;
294#endif
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800295
296 for (i = 0; i < 3; ++i) {
297 const int plane = planes[i];
298 const unsigned char *buf = img->planes[plane];
299 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700300 const int w = aom_img_plane_width(img, plane);
301 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800302
303 for (y = 0; y < h; ++y) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700304 fwrite(buf, bytes_per_sample, w, file);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800305 buf += stride;
306 }
John Koleszarc6b90392012-07-13 15:21:29 -0700307 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400308}
309
Yaowu Xuf883b422016-08-30 14:01:10 -0700310static int file_is_raw(struct AvxInputContext *input) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800311 uint8_t buf[32];
John Koleszarc6b90392012-07-13 15:21:29 -0700312 int is_raw = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700313 aom_codec_stream_info_t si;
John Koleszarcfe3f912010-10-20 10:49:12 -0400314
John Koleszarc6b90392012-07-13 15:21:29 -0700315 si.sz = sizeof(si);
John Koleszar19255b82010-11-23 13:40:31 -0500316
Tom Finegan00a35aa2013-11-14 12:37:42 -0800317 if (fread(buf, 1, 32, input->file) == 32) {
John Koleszarc6b90392012-07-13 15:21:29 -0700318 int i;
John Koleszarcfe3f912010-10-20 10:49:12 -0400319
Tom Finegan00a35aa2013-11-14 12:37:42 -0800320 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700321 for (i = 0; i < get_aom_decoder_count(); ++i) {
322 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
323 if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
clang-format6c4d83e2016-08-08 19:03:30 -0700324 32 - 4, &si)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700325 is_raw = 1;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800326 input->fourcc = decoder->fourcc;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800327 input->width = si.w;
328 input->height = si.h;
329 input->framerate.numerator = 30;
330 input->framerate.denominator = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700331 break;
332 }
Tom Finegan00a35aa2013-11-14 12:37:42 -0800333 }
334 }
John Koleszarc6b90392012-07-13 15:21:29 -0700335 }
John Koleszarcfe3f912010-10-20 10:49:12 -0400336
Tom Finegan00a35aa2013-11-14 12:37:42 -0800337 rewind(input->file);
John Koleszarc6b90392012-07-13 15:21:29 -0700338 return is_raw;
John Koleszarcfe3f912010-10-20 10:49:12 -0400339}
340
James Zern6a2e0f02015-05-09 10:42:31 -0700341static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
James Yu6d151322014-02-22 14:51:11 +0800342 fprintf(stderr,
clang-format6c4d83e2016-08-08 19:03:30 -0700343 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
John Koleszarc6b90392012-07-13 15:21:29 -0700344 frame_in, frame_out, dx_time,
James Yu6d151322014-02-22 14:51:11 +0800345 (double)frame_out * 1000000.0 / (double)dx_time);
John Koleszar5d12e042010-10-21 17:28:34 -0400346}
347
Frank Galligana4f30a52014-02-06 17:13:08 -0800348struct ExternalFrameBuffer {
clang-format6c4d83e2016-08-08 19:03:30 -0700349 uint8_t *data;
Frank Galligana4f30a52014-02-06 17:13:08 -0800350 size_t size;
351 int in_use;
352};
353
354struct ExternalFrameBufferList {
355 int num_external_frame_buffers;
356 struct ExternalFrameBuffer *ext_fb;
357};
358
Yaowu Xuc27fc142016-08-22 16:08:15 -0700359// Callback used by libaom to request an external frame buffer. |cb_priv|
Frank Galligana4f30a52014-02-06 17:13:08 -0800360// Application private data passed into the set function. |min_size| is the
361// minimum size in bytes needed to decode the next frame. |fb| pointer to the
362// frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700363static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
364 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800365 int i;
366 struct ExternalFrameBufferList *const ext_fb_list =
367 (struct ExternalFrameBufferList *)cb_priv;
clang-format6c4d83e2016-08-08 19:03:30 -0700368 if (ext_fb_list == NULL) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800369
370 // Find a free frame buffer.
371 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
clang-format6c4d83e2016-08-08 19:03:30 -0700372 if (!ext_fb_list->ext_fb[i].in_use) break;
Frank Galligana4f30a52014-02-06 17:13:08 -0800373 }
374
clang-format6c4d83e2016-08-08 19:03:30 -0700375 if (i == ext_fb_list->num_external_frame_buffers) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800376
377 if (ext_fb_list->ext_fb[i].size < min_size) {
378 free(ext_fb_list->ext_fb[i].data);
Frank Galligan175d9df2014-09-11 08:39:38 -0700379 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
clang-format6c4d83e2016-08-08 19:03:30 -0700380 if (!ext_fb_list->ext_fb[i].data) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800381
382 ext_fb_list->ext_fb[i].size = min_size;
383 }
384
385 fb->data = ext_fb_list->ext_fb[i].data;
386 fb->size = ext_fb_list->ext_fb[i].size;
387 ext_fb_list->ext_fb[i].in_use = 1;
388
389 // Set the frame buffer's private data to point at the external frame buffer.
390 fb->priv = &ext_fb_list->ext_fb[i];
391 return 0;
392}
393
Yaowu Xuc27fc142016-08-22 16:08:15 -0700394// Callback used by libaom when there are no references to the frame buffer.
Frank Galligana4f30a52014-02-06 17:13:08 -0800395// |cb_priv| user private data passed into the set function. |fb| pointer
396// to the frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700397static int release_av1_frame_buffer(void *cb_priv,
398 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800399 struct ExternalFrameBuffer *const ext_fb =
400 (struct ExternalFrameBuffer *)fb->priv;
401 (void)cb_priv;
402 ext_fb->in_use = 0;
403 return 0;
404}
405
James Zern6a2e0f02015-05-09 10:42:31 -0700406static void generate_filename(const char *pattern, char *out, size_t q_len,
407 unsigned int d_w, unsigned int d_h,
408 unsigned int frame_in) {
John Koleszarc6b90392012-07-13 15:21:29 -0700409 const char *p = pattern;
410 char *q = out;
John Koleszar933d44b2010-10-21 20:40:42 -0700411
John Koleszarc6b90392012-07-13 15:21:29 -0700412 do {
413 char *next_pat = strchr(p, '%');
John Koleszar933d44b2010-10-21 20:40:42 -0700414
John Koleszarc6b90392012-07-13 15:21:29 -0700415 if (p == next_pat) {
416 size_t pat_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700417
John Koleszar3707c342012-11-06 10:09:36 -0800418 /* parse the pattern */
John Koleszarc6b90392012-07-13 15:21:29 -0700419 q[q_len - 1] = '\0';
420 switch (p[1]) {
clang-format6c4d83e2016-08-08 19:03:30 -0700421 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
422 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
423 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
424 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
425 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
426 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
427 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
428 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
429 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
430 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
431 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
432 default: die("Unrecognized pattern %%%c\n", p[1]); break;
John Koleszarc6b90392012-07-13 15:21:29 -0700433 }
John Koleszar933d44b2010-10-21 20:40:42 -0700434
John Koleszarc6b90392012-07-13 15:21:29 -0700435 pat_len = strlen(q);
clang-format6c4d83e2016-08-08 19:03:30 -0700436 if (pat_len >= q_len - 1) die("Output filename too long.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700437 q += pat_len;
438 p += 2;
439 q_len -= pat_len;
440 } else {
441 size_t copy_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700442
John Koleszar3707c342012-11-06 10:09:36 -0800443 /* copy the next segment */
John Koleszarc6b90392012-07-13 15:21:29 -0700444 if (!next_pat)
445 copy_len = strlen(p);
446 else
447 copy_len = next_pat - p;
John Koleszar933d44b2010-10-21 20:40:42 -0700448
clang-format6c4d83e2016-08-08 19:03:30 -0700449 if (copy_len >= q_len - 1) die("Output filename too long.\n");
John Koleszar933d44b2010-10-21 20:40:42 -0700450
John Koleszarc6b90392012-07-13 15:21:29 -0700451 memcpy(q, p, copy_len);
452 q[copy_len] = '\0';
453 q += copy_len;
454 p += copy_len;
455 q_len -= copy_len;
456 }
457 } while (*p);
John Koleszar933d44b2010-10-21 20:40:42 -0700458}
459
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800460static int is_single_file(const char *outfile_pattern) {
461 const char *p = outfile_pattern;
462
463 do {
464 p = strchr(p, '%');
465 if (p && p[1] >= '1' && p[1] <= '9')
466 return 0; // pattern contains sequence number, so it's not unique
clang-format6c4d83e2016-08-08 19:03:30 -0700467 if (p) p++;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800468 } while (p);
469
470 return 1;
471}
472
473static void print_md5(unsigned char digest[16], const char *filename) {
474 int i;
475
clang-format6c4d83e2016-08-08 19:03:30 -0700476 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800477 printf(" %s\n", filename);
478}
479
480static FILE *open_outfile(const char *name) {
481 if (strcmp("-", name) == 0) {
482 set_binary_mode(stdout);
483 return stdout;
484 } else {
485 FILE *file = fopen(name, "wb");
clang-format6c4d83e2016-08-08 19:03:30 -0700486 if (!file) fatal("Failed to open output file '%s'", name);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800487 return file;
488 }
489}
490
Yaowu Xuf883b422016-08-30 14:01:10 -0700491#if CONFIG_AOM_HIGHBITDEPTH
492static int img_shifted_realloc_required(const aom_image_t *img,
493 const aom_image_t *shifted,
494 aom_img_fmt_t required_fmt) {
clang-format6c4d83e2016-08-08 19:03:30 -0700495 return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700496 required_fmt != shifted->fmt;
497}
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700498#endif
499
James Zern6a2e0f02015-05-09 10:42:31 -0700500static int main_loop(int argc, const char **argv_) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700501 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700502 char *fn = NULL;
503 int i;
James Zern360e4442016-11-16 21:53:55 -0800504 int ret = EXIT_FAILURE;
clang-format6c4d83e2016-08-08 19:03:30 -0700505 uint8_t *buf = NULL;
506 size_t bytes_in_buffer = 0, buffer_size = 0;
507 FILE *infile;
508 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
509 int do_md5 = 0, progress = 0, frame_parallel = 0;
510 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
511 int arg_skip = 0;
512 int ec_enabled = 0;
513 int keep_going = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700514 const AvxInterface *interface = NULL;
515 const AvxInterface *fourcc_interface = NULL;
James Yu6d151322014-02-22 14:51:11 +0800516 uint64_t dx_time = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700517 struct arg arg;
518 char **argv, **argi, **argj;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800519
clang-format6c4d83e2016-08-08 19:03:30 -0700520 int single_file;
521 int use_y4m = 1;
522 int opt_yv12 = 0;
523 int opt_i420 = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700524 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
525#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700526 unsigned int output_bit_depth = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700527#endif
Geza Lore490ba1a2016-03-11 17:42:49 +0000528#if CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700529 int tile_row = -1;
530 int tile_col = -1;
Geza Lore490ba1a2016-03-11 17:42:49 +0000531#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700532 int frames_corrupted = 0;
533 int dec_flags = 0;
534 int do_scale = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700535 aom_image_t *scaled_img = NULL;
536#if CONFIG_AOM_HIGHBITDEPTH
537 aom_image_t *img_shifted = NULL;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700538#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700539 int frame_avail, got_data, flush_decoder = 0;
540 int num_external_frame_buffers = 0;
541 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400542
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800543 const char *outfile_pattern = NULL;
clang-format6c4d83e2016-08-08 19:03:30 -0700544 char outfile_name[PATH_MAX] = { 0 };
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800545 FILE *outfile = NULL;
546
Peter Boströma1f64322017-01-19 11:35:30 -0500547 FILE *framestats_file = NULL;
548
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800549 MD5Context md5_ctx;
550 unsigned char md5_digest[16];
551
Yaowu Xuf883b422016-08-30 14:01:10 -0700552 struct AvxDecInputContext input = { NULL, NULL };
553 struct AvxInputContext aom_input_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700554#if CONFIG_WEBM_IO
Jim Bankoski7ae32692014-08-19 06:44:10 -0700555 struct WebmInputContext webm_ctx;
556 memset(&(webm_ctx), 0, sizeof(webm_ctx));
Tom Finegan2abe2d42013-11-18 14:39:51 -0800557 input.webm_ctx = &webm_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700558#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700559 input.aom_input_ctx = &aom_input_ctx;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800560
John Koleszarc6b90392012-07-13 15:21:29 -0700561 /* Parse command line */
562 exec_name = argv_[0];
563 argv = argv_dup(argc - 1, argv_ + 1);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400564
John Koleszarc6b90392012-07-13 15:21:29 -0700565 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
566 memset(&arg, 0, sizeof(arg));
567 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400568
John Koleszarc6b90392012-07-13 15:21:29 -0700569 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700570 interface = get_aom_decoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800571 if (!interface)
572 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszar9596a4c2013-06-12 16:08:35 -0700573 } else if (arg_match(&arg, &looparg, argi)) {
574 // no-op
Peter Boströma1f64322017-01-19 11:35:30 -0500575 } else if (arg_match(&arg, &outputfile, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700576 outfile_pattern = arg.val;
Peter Boströma1f64322017-01-19 11:35:30 -0500577 } else if (arg_match(&arg, &use_yv12, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700578 use_y4m = 0;
579 flipuv = 1;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700580 opt_yv12 = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700581 } else if (arg_match(&arg, &use_i420, argi)) {
582 use_y4m = 0;
583 flipuv = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700584 opt_i420 = 1;
585 } else if (arg_match(&arg, &rawvideo, argi)) {
586 use_y4m = 0;
Peter Boströma1f64322017-01-19 11:35:30 -0500587 } else if (arg_match(&arg, &flipuvarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700588 flipuv = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500589 } else if (arg_match(&arg, &noblitarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700590 noblit = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500591 } else if (arg_match(&arg, &progressarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700592 progress = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500593 } else if (arg_match(&arg, &limitarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700594 stop_after = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500595 } else if (arg_match(&arg, &skiparg, argi)) {
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700596 arg_skip = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500597 } else if (arg_match(&arg, &postprocarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700598 postproc = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500599 } else if (arg_match(&arg, &md5arg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700600 do_md5 = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500601 } else if (arg_match(&arg, &framestatsarg, argi)) {
602 framestats_file = fopen(arg.val, "w");
603 if (!framestats_file) {
604 die("Error: Could not open --framestats file (%s) for writing.\n",
605 arg.val);
606 }
607 } else if (arg_match(&arg, &summaryarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700608 summary = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500609 } else if (arg_match(&arg, &threadsarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700610 cfg.threads = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500611 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700612#if CONFIG_AV1_DECODER
hkuang1f841c32015-03-06 13:03:18 -0800613 else if (arg_match(&arg, &frameparallelarg, argi))
614 frame_parallel = 1;
615#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700616 else if (arg_match(&arg, &verbosearg, argi))
617 quiet = 0;
John Koleszar7d8fc262013-02-25 16:58:47 -0800618 else if (arg_match(&arg, &scalearg, argi))
619 do_scale = 1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800620 else if (arg_match(&arg, &fb_arg, argi))
621 num_external_frame_buffers = arg_parse_uint(&arg);
James Zern7690d882014-08-22 18:01:28 -0700622 else if (arg_match(&arg, &continuearg, argi))
623 keep_going = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700624#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700625 else if (arg_match(&arg, &outbitdeptharg, argi)) {
626 output_bit_depth = arg_parse_uint(&arg);
627 }
628#endif
Geza Lore490ba1a2016-03-11 17:42:49 +0000629#if CONFIG_EXT_TILE
630 else if (arg_match(&arg, &tiler, argi))
631 tile_row = arg_parse_int(&arg);
632 else if (arg_match(&arg, &tilec, argi))
633 tile_col = arg_parse_int(&arg);
634#endif // CONFIG_EXT_TILE
John Koleszarcfe3f912010-10-20 10:49:12 -0400635 else
John Koleszarc6b90392012-07-13 15:21:29 -0700636 argj++;
637 }
638
639 /* Check for unrecognized options */
640 for (argi = argv; *argi; argi++)
641 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
642 die("Error: Unrecognized option %s\n", *argi);
643
644 /* Handle non-option arguments */
645 fn = argv[0];
646
Hanno Böckb5d0a202015-02-24 15:48:21 -0800647 if (!fn) {
648 free(argv);
John Koleszarc6b90392012-07-13 15:21:29 -0700649 usage_exit();
Hanno Böckb5d0a202015-02-24 15:48:21 -0800650 }
John Koleszarc6b90392012-07-13 15:21:29 -0700651 /* Open file */
652 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
653
654 if (!infile) {
James Zern6b993f02015-02-05 21:06:29 -0800655 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
John Koleszarc6b90392012-07-13 15:21:29 -0700656 }
657#if CONFIG_OS_SUPPORT
658 /* Make sure we don't dump to the terminal, unless forced to with -o - */
James Zernb9b28382016-12-09 16:50:39 -0800659 if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700660 fprintf(stderr,
661 "Not dumping raw video to your terminal. Use '-o -' to "
662 "override.\n");
663 return EXIT_FAILURE;
664 }
665#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700666 input.aom_input_ctx->file = infile;
667 if (file_is_ivf(input.aom_input_ctx))
668 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700669#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700670 else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
671 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700672#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700673 else if (file_is_raw(input.aom_input_ctx))
674 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700675 else {
676 fprintf(stderr, "Unrecognized input file type.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700677#if !CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700678 fprintf(stderr, "aomdec was built without WebM container support.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700679#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700680 return EXIT_FAILURE;
681 }
682
John Koleszarc6b90392012-07-13 15:21:29 -0700683 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800684 single_file = is_single_file(outfile_pattern);
John Koleszarc6b90392012-07-13 15:21:29 -0700685
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800686 if (!noblit && single_file) {
687 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
Yaowu Xuf883b422016-08-30 14:01:10 -0700688 aom_input_ctx.width, aom_input_ctx.height, 0);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800689 if (do_md5)
690 MD5Init(&md5_ctx);
691 else
692 outfile = open_outfile(outfile_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700693 }
694
695 if (use_y4m && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700696 if (!single_file) {
clang-format6c4d83e2016-08-08 19:03:30 -0700697 fprintf(stderr,
698 "YUV4MPEG2 not supported with output patterns,"
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700699 " try --i420 or --yv12 or --rawvideo.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700700 return EXIT_FAILURE;
701 }
702
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700703#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700704 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
705 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
clang-format6c4d83e2016-08-08 19:03:30 -0700706 fprintf(stderr,
707 "Failed to guess framerate -- error parsing "
John Koleszarc6b90392012-07-13 15:21:29 -0700708 "webm file?\n");
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400709 return EXIT_FAILURE;
John Koleszarc6b90392012-07-13 15:21:29 -0700710 }
Dmitry Kovalev5ab63582014-01-17 17:02:37 -0800711 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700712#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700713 }
714
Yaowu Xuf883b422016-08-30 14:01:10 -0700715 fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800716 if (interface && fourcc_interface && interface != fourcc_interface)
717 warn("Header indicates codec: %s\n", fourcc_interface->name);
718 else
719 interface = fourcc_interface;
John Koleszarc6b90392012-07-13 15:21:29 -0700720
Yaowu Xuf883b422016-08-30 14:01:10 -0700721 if (!interface) interface = get_aom_decoder_by_index(0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400722
Yaowu Xuf883b422016-08-30 14:01:10 -0700723 dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0) |
724 (ec_enabled ? AOM_CODEC_USE_ERROR_CONCEALMENT : 0) |
725 (frame_parallel ? AOM_CODEC_USE_FRAME_THREADING : 0);
726 if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
clang-format6c4d83e2016-08-08 19:03:30 -0700727 dec_flags)) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800728 fprintf(stderr, "Failed to initialize decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800730 goto fail2;
John Koleszarc6b90392012-07-13 15:21:29 -0700731 }
John Koleszar933d44b2010-10-21 20:40:42 -0700732
clang-format6c4d83e2016-08-08 19:03:30 -0700733 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400734
Yaowu Xuf883b422016-08-30 14:01:10 -0700735#if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
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
Yaowu Xuf883b422016-08-30 14:01:10 -0700788 if (aom_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer, NULL,
clang-format6c4d83e2016-08-08 19:03:30 -0700789 0)) {
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.
Yaowu Xuf883b422016-08-30 14:01:10 -0700821 if (aom_codec_decode(&decoder, NULL, 0, NULL, 0)) {
822 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,
881 "Failed to scale output frame: %s.\n"
Deb Mukherjee47031c02014-05-16 18:52:01 -0700882 "Scaling is disabled in this configuration. "
883 "To enable scaling, configure with --enable-libyuv\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 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700889#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700890 // Default to codec bit depth if output bit depth not set
Ronald S. Bultjeeba342a2015-09-11 16:55:19 -0400891 if (!output_bit_depth && single_file && !do_md5) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700892 output_bit_depth = img->bit_depth;
893 }
894 // Shift up or down if necessary
Ronald S. Bultjeeba342a2015-09-11 16:55:19 -0400895 if (output_bit_depth != 0 && output_bit_depth != img->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700896 const aom_img_fmt_t shifted_fmt =
clang-format6c4d83e2016-08-08 19:03:30 -0700897 output_bit_depth == 8
Yaowu Xuf883b422016-08-30 14:01:10 -0700898 ? img->fmt ^ (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
899 : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700900 if (img_shifted &&
901 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700902 aom_img_free(img_shifted);
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700903 img_shifted = NULL;
904 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700905 if (!img_shifted) {
clang-format6c4d83e2016-08-08 19:03:30 -0700906 img_shifted =
Yaowu Xuf883b422016-08-30 14:01:10 -0700907 aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700908 img_shifted->bit_depth = output_bit_depth;
909 }
910 if (output_bit_depth > img->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700911 aom_img_upshift(img_shifted, img, output_bit_depth - img->bit_depth);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700912 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700913 aom_img_downshift(img_shifted, img,
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700914 img->bit_depth - output_bit_depth);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700915 }
916 img = img_shifted;
917 }
918#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800919
Geza Lore490ba1a2016-03-11 17:42:49 +0000920#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700921 aom_input_ctx.width = img->d_w;
922 aom_input_ctx.height = img->d_h;
Geza Lore490ba1a2016-03-11 17:42:49 +0000923#endif // CONFIG_EXT_TILE
924
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800925 if (single_file) {
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800926 if (use_y4m) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700927 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800928 size_t len = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700929 if (img->fmt == AOM_IMG_FMT_I440 || img->fmt == AOM_IMG_FMT_I44016) {
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700930 fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
931 goto fail;
932 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800933 if (frame_out == 1) {
934 // Y4M file header
clang-format6c4d83e2016-08-08 19:03:30 -0700935 len = y4m_write_file_header(
Urvang Joshi4145bf02016-10-17 14:53:33 -0700936 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
937 aom_input_ctx.height, &aom_input_ctx.framerate, img->fmt,
938 img->bit_depth);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800939 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700940 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800941 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700942 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800943 }
944 }
945
946 // Y4M frame header
Urvang Joshi4145bf02016-10-17 14:53:33 -0700947 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800948 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700949 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800950 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700951 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800952 }
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700953 } else {
954 if (frame_out == 1) {
955 // Check if --yv12 or --i420 options are consistent with the
956 // bit-stream decoded
957 if (opt_i420) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700958 if (img->fmt != AOM_IMG_FMT_I420 &&
959 img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700960 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
961 goto fail;
962 }
963 }
964 if (opt_yv12) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700965 if ((img->fmt != AOM_IMG_FMT_I420 &&
966 img->fmt != AOM_IMG_FMT_YV12) ||
clang-format6c4d83e2016-08-08 19:03:30 -0700967 img->bit_depth != 8) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700968 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
969 goto fail;
970 }
971 }
972 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800973 }
974
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800975 if (do_md5) {
976 update_image_md5(img, planes, &md5_ctx);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800977 } else {
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800978 write_image_file(img, planes, outfile);
979 }
980 } else {
clang-format6c4d83e2016-08-08 19:03:30 -0700981 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
982 img->d_h, frame_in);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800983 if (do_md5) {
984 MD5Init(&md5_ctx);
985 update_image_md5(img, planes, &md5_ctx);
986 MD5Final(md5_digest, &md5_ctx);
987 print_md5(md5_digest, outfile_name);
988 } else {
989 outfile = open_outfile(outfile_name);
990 write_image_file(img, planes, outfile);
991 fclose(outfile);
John Koleszarc6b90392012-07-13 15:21:29 -0700992 }
John Koleszarc6b90392012-07-13 15:21:29 -0700993 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400994 }
John Koleszarc6b90392012-07-13 15:21:29 -0700995 }
996
997 if (summary || progress) {
998 show_progress(frame_in, frame_out, dx_time);
999 fprintf(stderr, "\n");
1000 }
1001
James Zern360e4442016-11-16 21:53:55 -08001002 if (frames_corrupted) {
John Koleszarc6b90392012-07-13 15:21:29 -07001003 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
James Zern360e4442016-11-16 21:53:55 -08001004 } else {
1005 ret = EXIT_SUCCESS;
1006 }
John Koleszar4226f0c2011-04-27 12:04:48 -04001007
John Koleszar0ea50ce2010-05-18 11:58:33 -04001008fail:
1009
Yaowu Xuf883b422016-08-30 14:01:10 -07001010 if (aom_codec_destroy(&decoder)) {
Adrian Granged427fab2013-11-21 17:19:04 -08001011 fprintf(stderr, "Failed to destroy decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001012 aom_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -07001013 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001014
James Zern360e4442016-11-16 21:53:55 -08001015fail2:
1016
Dmitry Kovalev347b3062014-01-22 09:04:07 -08001017 if (!noblit && single_file) {
1018 if (do_md5) {
1019 MD5Final(md5_digest, &md5_ctx);
1020 print_md5(md5_digest, outfile_name);
1021 } else {
1022 fclose(outfile);
1023 }
1024 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001025
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001026#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -07001027 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
Tom Finegan2abe2d42013-11-18 14:39:51 -08001028 webm_free(input.webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001029#endif
1030
Yaowu Xuf883b422016-08-30 14:01:10 -07001031 if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
Tom Finegan2abe2d42013-11-18 14:39:51 -08001032
Yaowu Xuf883b422016-08-30 14:01:10 -07001033 if (scaled_img) aom_img_free(scaled_img);
1034#if CONFIG_AOM_HIGHBITDEPTH
1035 if (img_shifted) aom_img_free(img_shifted);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001036#endif
Adrian Granged427fab2013-11-21 17:19:04 -08001037
Frank Galligana4f30a52014-02-06 17:13:08 -08001038 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1039 free(ext_fb_list.ext_fb[i].data);
1040 }
1041 free(ext_fb_list.ext_fb);
1042
John Koleszarc6b90392012-07-13 15:21:29 -07001043 fclose(infile);
Peter Boströma1f64322017-01-19 11:35:30 -05001044 if (framestats_file) fclose(framestats_file);
1045
John Koleszarc6b90392012-07-13 15:21:29 -07001046 free(argv);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001047
James Zern360e4442016-11-16 21:53:55 -08001048 return ret;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001049}
John Koleszar9596a4c2013-06-12 16:08:35 -07001050
1051int main(int argc, const char **argv_) {
1052 unsigned int loops = 1, i;
1053 char **argv, **argi, **argj;
1054 struct arg arg;
1055 int error = 0;
1056
1057 argv = argv_dup(argc - 1, argv_ + 1);
1058 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1059 memset(&arg, 0, sizeof(arg));
1060 arg.argv_step = 1;
1061
1062 if (arg_match(&arg, &looparg, argi)) {
1063 loops = arg_parse_uint(&arg);
1064 break;
1065 }
1066 }
1067 free(argv);
clang-format6c4d83e2016-08-08 19:03:30 -07001068 for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
John Koleszar9596a4c2013-06-12 16:08:35 -07001069 return error;
1070}