blob: 0ab864601976962aaf4ec54580a1f558275972ec [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010 */
11
John Koleszarda584362013-05-06 15:52:06 -070012#include <assert.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040013#include <stdio.h>
14#include <stdlib.h>
15#include <stdarg.h>
16#include <string.h>
John Koleszar933d44b2010-10-21 20:40:42 -070017#include <limits.h>
Tero Rintaluoma11a222f2011-01-24 11:21:40 +020018
Yaowu Xuf883b422016-08-30 14:01:10 -070019#include "./aom_config.h"
James Zern1e681d92014-08-15 21:00:09 -070020
James Zernb9b28382016-12-09 16:50:39 -080021#if CONFIG_OS_SUPPORT
22#if HAVE_UNISTD_H
23#include <unistd.h> // NOLINT
24#elif !defined(STDOUT_FILENO)
25#define STDOUT_FILENO 1
26#endif
27#endif
28
James Zern1e681d92014-08-15 21:00:09 -070029#if CONFIG_LIBYUV
Tom Finegan2abe2d42013-11-18 14:39:51 -080030#include "third_party/libyuv/include/libyuv/scale.h"
James Zern1e681d92014-08-15 21:00:09 -070031#endif
Tom Finegan2abe2d42013-11-18 14:39:51 -080032
33#include "./args.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080034#include "./ivfdec.h"
35
Yaowu Xuf883b422016-08-30 14:01:10 -070036#include "aom/aom_decoder.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070037#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070038#include "aom_ports/aom_timer.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080039
Yaowu Xuf883b422016-08-30 14:01:10 -070040#if CONFIG_AV1_DECODER
41#include "aom/aomdx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040042#endif
Tom Finegan00a35aa2013-11-14 12:37:42 -080043
Tom Finegan2abe2d42013-11-18 14:39:51 -080044#include "./md5_utils.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080045
Cyril Concolato6c788832017-10-30 16:30:35 -070046#if CONFIG_OBU_NO_IVF
47#include "./obudec.h"
48#endif
49
Tom Finegan00a35aa2013-11-14 12:37:42 -080050#include "./tools_common.h"
James Zernb6cce762014-05-10 17:44:12 -070051#if CONFIG_WEBM_IO
Tom Finegan2abe2d42013-11-18 14:39:51 -080052#include "./webmdec.h"
James Zernb6cce762014-05-10 17:44:12 -070053#endif
Cyril Concolato6c788832017-10-30 16:30:35 -070054
Dmitry Kovalev5ab63582014-01-17 17:02:37 -080055#include "./y4menc.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040056
57static const char *exec_name;
58
Yaowu Xuf883b422016-08-30 14:01:10 -070059struct AvxDecInputContext {
60 struct AvxInputContext *aom_input_ctx;
Tom Finegan2abe2d42013-11-18 14:39:51 -080061 struct WebmInputContext *webm_ctx;
62};
63
James Zernfd8c78c2017-11-27 16:37:33 -080064static const arg_def_t help =
65 ARG_DEF(NULL, "help", 0, "Show usage options and exit");
clang-format6c4d83e2016-08-08 19:03:30 -070066static const arg_def_t looparg =
67 ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
68static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
69static const arg_def_t use_yv12 =
70 ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
71static const arg_def_t use_i420 =
72 ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
73static const arg_def_t flipuvarg =
74 ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
75static const arg_def_t rawvideo =
76 ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
77static const arg_def_t noblitarg =
78 ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
79static const arg_def_t progressarg =
80 ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
81static const arg_def_t limitarg =
82 ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
83static const arg_def_t skiparg =
84 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
85static const arg_def_t postprocarg =
86 ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
87static const arg_def_t summaryarg =
88 ARG_DEF(NULL, "summary", 0, "Show timing summary");
89static const arg_def_t outputfile =
90 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
91static const arg_def_t threadsarg =
92 ARG_DEF("t", "threads", 1, "Max threads to use");
93static const arg_def_t frameparallelarg =
94 ARG_DEF(NULL, "frame-parallel", 0, "Frame parallel decode");
95static const arg_def_t verbosearg =
96 ARG_DEF("v", "verbose", 0, "Show version string");
clang-format6c4d83e2016-08-08 19:03:30 -070097static const arg_def_t scalearg =
98 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
99static const arg_def_t continuearg =
100 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
101static const arg_def_t fb_arg =
102 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
103static const arg_def_t md5arg =
104 ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
Peter Boströma1f64322017-01-19 11:35:30 -0500105static const arg_def_t framestatsarg =
106 ARG_DEF(NULL, "framestats", 1, "Output per-frame stats (.csv format)");
clang-format6c4d83e2016-08-08 19:03:30 -0700107static const arg_def_t outbitdeptharg =
108 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
Geza Lore490ba1a2016-03-11 17:42:49 +0000109#if CONFIG_EXT_TILE
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)");
Geza Lore490ba1a2016-03-11 17:42:49 +0000116#endif // CONFIG_EXT_TILE
Dmitry Kovalevd24f4e42013-12-27 16:10:18 -0800117
Yaowu Xud3e7c682017-12-21 14:08:25 -0800118static const arg_def_t *all_args[] = {
119 &help, &codecarg, &use_yv12, &use_i420, &flipuvarg,
120 &rawvideo, &noblitarg, &progressarg, &limitarg, &skiparg,
121 &postprocarg, &summaryarg, &outputfile, &threadsarg, &frameparallelarg,
122 &verbosearg, &scalearg, &fb_arg, &md5arg, &framestatsarg,
123 &continuearg, &outbitdeptharg,
Geza Lore490ba1a2016-03-11 17:42:49 +0000124#if CONFIG_EXT_TILE
Yaowu Xud3e7c682017-12-21 14:08:25 -0800125 &tiler, &tilec,
Geza Lore490ba1a2016-03-11 17:42:49 +0000126#endif // CONFIG_EXT_TILE
Yaowu Xud3e7c682017-12-21 14:08:25 -0800127 NULL
128};
John Koleszar0ea50ce2010-05-18 11:58:33 -0400129
James Zern1e681d92014-08-15 21:00:09 -0700130#if CONFIG_LIBYUV
Yaowu Xuf883b422016-08-30 14:01:10 -0700131static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
clang-format6c4d83e2016-08-08 19:03:30 -0700132 FilterModeEnum mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700133 if (src->fmt == AOM_IMG_FMT_I42016) {
134 assert(dst->fmt == AOM_IMG_FMT_I42016);
clang-format6c4d83e2016-08-08 19:03:30 -0700135 return I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -0700136 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
137 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
138 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
139 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
140 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
141 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
142 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700143 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700144 assert(src->fmt == AOM_IMG_FMT_I420);
145 assert(dst->fmt == AOM_IMG_FMT_I420);
146 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
147 src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
148 src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
149 src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
150 dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
151 dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -0700152 dst->d_h, mode);
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800153}
James Zern1e681d92014-08-15 21:00:09 -0700154#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800155
James Zernfd8c78c2017-11-27 16:37:33 -0800156void show_help(FILE *fout, int shorthelp) {
157 fprintf(fout, "Usage: %s <options> filename\n\n", exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400158
James Zernfd8c78c2017-11-27 16:37:33 -0800159 if (shorthelp) {
160 fprintf(fout, "Use --help to see the full list of options.\n");
161 return;
162 }
163
164 fprintf(fout, "Options:\n");
165 arg_show_usage(fout, all_args);
166 fprintf(fout,
John Koleszarc6b90392012-07-13 15:21:29 -0700167 "\nOutput File Patterns:\n\n"
168 " The -o argument specifies the name of the file(s) to "
169 "write to. If the\n argument does not include any escape "
170 "characters, the output will be\n written to a single file. "
171 "Otherwise, the filename will be calculated by\n expanding "
John Koleszar3707c342012-11-06 10:09:36 -0800172 "the following escape characters:\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800173 fprintf(fout,
John Koleszarc6b90392012-07-13 15:21:29 -0700174 "\n\t%%w - Frame width"
175 "\n\t%%h - Frame height"
176 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
177 "\n\n Pattern arguments are only supported in conjunction "
178 "with the --yv12 and\n --i420 options. If the -o option is "
clang-format6c4d83e2016-08-08 19:03:30 -0700179 "not specified, the output will be\n directed to stdout.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800180 fprintf(fout, "\nIncluded decoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400181
James Zernfd8c78c2017-11-27 16:37:33 -0800182 for (int i = 0; i < get_aom_decoder_count(); ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700183 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
James Zernfd8c78c2017-11-27 16:37:33 -0800184 fprintf(fout, " %-6s - %s\n", decoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700185 aom_codec_iface_name(decoder->codec_interface()));
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800186 }
James Zernfd8c78c2017-11-27 16:37:33 -0800187}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400188
James Zernfd8c78c2017-11-27 16:37:33 -0800189void usage_exit(void) {
190 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700191 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400192}
193
clang-format6c4d83e2016-08-08 19:03:30 -0700194static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
195 size_t *buffer_size) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800196 char raw_hdr[RAW_FRAME_HDR_SZ];
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800197 size_t frame_size = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700198
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800199 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
clang-format6c4d83e2016-08-08 19:03:30 -0700200 if (!feof(infile)) warn("Failed to read RAW frame size\n");
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800201 } else {
James Zern75d7ee32014-02-04 20:06:28 -0800202 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
203 const size_t kFrameTooSmallThreshold = 256 * 1024;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800204 frame_size = mem_get_le32(raw_hdr);
John Koleszarc6b90392012-07-13 15:21:29 -0700205
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800206 if (frame_size > kCorruptFrameThreshold) {
207 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
208 frame_size = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400209 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400210
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800211 if (frame_size < kFrameTooSmallThreshold) {
212 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
213 (unsigned int)frame_size);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400214 }
215
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800216 if (frame_size > *buffer_size) {
217 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
218 if (new_buf) {
219 *buffer = new_buf;
220 *buffer_size = 2 * frame_size;
221 } else {
222 warn("Failed to allocate compressed data buffer\n");
223 frame_size = 0;
224 }
225 }
John Koleszarc6b90392012-07-13 15:21:29 -0700226 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400227
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800228 if (!feof(infile)) {
229 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
230 warn("Failed to read full frame\n");
231 return 1;
232 }
233 *bytes_read = frame_size;
234 }
235
236 return 0;
237}
238
Yaowu Xuf883b422016-08-30 14:01:10 -0700239static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800240 size_t *bytes_in_buffer, size_t *buffer_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700241 switch (input->aom_input_ctx->file_type) {
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700242#if CONFIG_WEBM_IO
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800243 case FILE_TYPE_WEBM:
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700244 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700245#endif
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800246 case FILE_TYPE_RAW:
Yaowu Xuf883b422016-08-30 14:01:10 -0700247 return raw_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700248 buffer_size);
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800249 case FILE_TYPE_IVF:
Yaowu Xuf883b422016-08-30 14:01:10 -0700250 return ivf_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700251 buffer_size);
Cyril Concolato6c788832017-10-30 16:30:35 -0700252#if CONFIG_OBU_NO_IVF
253 case FILE_TYPE_OBU:
254 return obu_read_temporal_unit(input->aom_input_ctx->file, buf,
255 bytes_in_buffer, buffer_size);
256#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700257 default: return 1;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800258 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400259}
260
Yaowu Xuf883b422016-08-30 14:01:10 -0700261static void update_image_md5(const aom_image_t *img, const int planes[3],
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800262 MD5Context *md5) {
263 int i, y;
264
265 for (i = 0; i < 3; ++i) {
266 const int plane = planes[i];
267 const unsigned char *buf = img->planes[plane];
268 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700269 const int w = aom_img_plane_width(img, plane) *
270 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
271 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800272
273 for (y = 0; y < h; ++y) {
274 MD5Update(md5, buf, w);
275 buf += stride;
276 }
277 }
278}
279
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000280static void write_image_file(const aom_image_t *img, const int *planes,
281 const int num_planes, FILE *file) {
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800282 int i, y;
Yaowu Xuf883b422016-08-30 14:01:10 -0700283 const int bytes_per_sample = ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800284
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000285 for (i = 0; i < num_planes; ++i) {
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800286 const int plane = planes[i];
287 const unsigned char *buf = img->planes[plane];
288 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700289 const int w = aom_img_plane_width(img, plane);
290 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800291
292 for (y = 0; y < h; ++y) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700293 fwrite(buf, bytes_per_sample, w, file);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800294 buf += stride;
295 }
John Koleszarc6b90392012-07-13 15:21:29 -0700296 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400297}
298
Yaowu Xuf883b422016-08-30 14:01:10 -0700299static int file_is_raw(struct AvxInputContext *input) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800300 uint8_t buf[32];
John Koleszarc6b90392012-07-13 15:21:29 -0700301 int is_raw = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700302 aom_codec_stream_info_t si;
John Koleszarcfe3f912010-10-20 10:49:12 -0400303
Tom Finegan00a35aa2013-11-14 12:37:42 -0800304 if (fread(buf, 1, 32, input->file) == 32) {
John Koleszarc6b90392012-07-13 15:21:29 -0700305 int i;
John Koleszarcfe3f912010-10-20 10:49:12 -0400306
Tom Finegan00a35aa2013-11-14 12:37:42 -0800307 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700308 for (i = 0; i < get_aom_decoder_count(); ++i) {
309 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
310 if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
clang-format6c4d83e2016-08-08 19:03:30 -0700311 32 - 4, &si)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700312 is_raw = 1;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800313 input->fourcc = decoder->fourcc;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800314 input->width = si.w;
315 input->height = si.h;
316 input->framerate.numerator = 30;
317 input->framerate.denominator = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700318 break;
319 }
Tom Finegan00a35aa2013-11-14 12:37:42 -0800320 }
321 }
John Koleszarc6b90392012-07-13 15:21:29 -0700322 }
John Koleszarcfe3f912010-10-20 10:49:12 -0400323
Tom Finegan00a35aa2013-11-14 12:37:42 -0800324 rewind(input->file);
John Koleszarc6b90392012-07-13 15:21:29 -0700325 return is_raw;
John Koleszarcfe3f912010-10-20 10:49:12 -0400326}
327
James Zern6a2e0f02015-05-09 10:42:31 -0700328static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
James Yu6d151322014-02-22 14:51:11 +0800329 fprintf(stderr,
clang-format6c4d83e2016-08-08 19:03:30 -0700330 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
John Koleszarc6b90392012-07-13 15:21:29 -0700331 frame_in, frame_out, dx_time,
James Yu6d151322014-02-22 14:51:11 +0800332 (double)frame_out * 1000000.0 / (double)dx_time);
John Koleszar5d12e042010-10-21 17:28:34 -0400333}
334
Frank Galligana4f30a52014-02-06 17:13:08 -0800335struct ExternalFrameBuffer {
clang-format6c4d83e2016-08-08 19:03:30 -0700336 uint8_t *data;
Frank Galligana4f30a52014-02-06 17:13:08 -0800337 size_t size;
338 int in_use;
339};
340
341struct ExternalFrameBufferList {
342 int num_external_frame_buffers;
343 struct ExternalFrameBuffer *ext_fb;
344};
345
Yaowu Xuc27fc142016-08-22 16:08:15 -0700346// Callback used by libaom to request an external frame buffer. |cb_priv|
Frank Galligana4f30a52014-02-06 17:13:08 -0800347// Application private data passed into the set function. |min_size| is the
348// minimum size in bytes needed to decode the next frame. |fb| pointer to the
349// frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700350static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
351 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800352 int i;
353 struct ExternalFrameBufferList *const ext_fb_list =
354 (struct ExternalFrameBufferList *)cb_priv;
clang-format6c4d83e2016-08-08 19:03:30 -0700355 if (ext_fb_list == NULL) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800356
357 // Find a free frame buffer.
358 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
clang-format6c4d83e2016-08-08 19:03:30 -0700359 if (!ext_fb_list->ext_fb[i].in_use) break;
Frank Galligana4f30a52014-02-06 17:13:08 -0800360 }
361
clang-format6c4d83e2016-08-08 19:03:30 -0700362 if (i == ext_fb_list->num_external_frame_buffers) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800363
364 if (ext_fb_list->ext_fb[i].size < min_size) {
365 free(ext_fb_list->ext_fb[i].data);
Frank Galligan175d9df2014-09-11 08:39:38 -0700366 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
clang-format6c4d83e2016-08-08 19:03:30 -0700367 if (!ext_fb_list->ext_fb[i].data) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800368
369 ext_fb_list->ext_fb[i].size = min_size;
370 }
371
372 fb->data = ext_fb_list->ext_fb[i].data;
373 fb->size = ext_fb_list->ext_fb[i].size;
374 ext_fb_list->ext_fb[i].in_use = 1;
375
376 // Set the frame buffer's private data to point at the external frame buffer.
377 fb->priv = &ext_fb_list->ext_fb[i];
378 return 0;
379}
380
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381// Callback used by libaom when there are no references to the frame buffer.
Frank Galligana4f30a52014-02-06 17:13:08 -0800382// |cb_priv| user private data passed into the set function. |fb| pointer
383// to the frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700384static int release_av1_frame_buffer(void *cb_priv,
385 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800386 struct ExternalFrameBuffer *const ext_fb =
387 (struct ExternalFrameBuffer *)fb->priv;
388 (void)cb_priv;
389 ext_fb->in_use = 0;
390 return 0;
391}
392
James Zern6a2e0f02015-05-09 10:42:31 -0700393static void generate_filename(const char *pattern, char *out, size_t q_len,
394 unsigned int d_w, unsigned int d_h,
395 unsigned int frame_in) {
John Koleszarc6b90392012-07-13 15:21:29 -0700396 const char *p = pattern;
397 char *q = out;
John Koleszar933d44b2010-10-21 20:40:42 -0700398
John Koleszarc6b90392012-07-13 15:21:29 -0700399 do {
400 char *next_pat = strchr(p, '%');
John Koleszar933d44b2010-10-21 20:40:42 -0700401
John Koleszarc6b90392012-07-13 15:21:29 -0700402 if (p == next_pat) {
403 size_t pat_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700404
John Koleszar3707c342012-11-06 10:09:36 -0800405 /* parse the pattern */
John Koleszarc6b90392012-07-13 15:21:29 -0700406 q[q_len - 1] = '\0';
407 switch (p[1]) {
clang-format6c4d83e2016-08-08 19:03:30 -0700408 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
409 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
410 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
411 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
412 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
413 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
414 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
415 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
416 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
417 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
418 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
419 default: die("Unrecognized pattern %%%c\n", p[1]); break;
John Koleszarc6b90392012-07-13 15:21:29 -0700420 }
John Koleszar933d44b2010-10-21 20:40:42 -0700421
John Koleszarc6b90392012-07-13 15:21:29 -0700422 pat_len = strlen(q);
clang-format6c4d83e2016-08-08 19:03:30 -0700423 if (pat_len >= q_len - 1) die("Output filename too long.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700424 q += pat_len;
425 p += 2;
426 q_len -= pat_len;
427 } else {
428 size_t copy_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700429
John Koleszar3707c342012-11-06 10:09:36 -0800430 /* copy the next segment */
John Koleszarc6b90392012-07-13 15:21:29 -0700431 if (!next_pat)
432 copy_len = strlen(p);
433 else
434 copy_len = next_pat - p;
John Koleszar933d44b2010-10-21 20:40:42 -0700435
clang-format6c4d83e2016-08-08 19:03:30 -0700436 if (copy_len >= q_len - 1) die("Output filename too long.\n");
John Koleszar933d44b2010-10-21 20:40:42 -0700437
John Koleszarc6b90392012-07-13 15:21:29 -0700438 memcpy(q, p, copy_len);
439 q[copy_len] = '\0';
440 q += copy_len;
441 p += copy_len;
442 q_len -= copy_len;
443 }
444 } while (*p);
John Koleszar933d44b2010-10-21 20:40:42 -0700445}
446
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800447static int is_single_file(const char *outfile_pattern) {
448 const char *p = outfile_pattern;
449
450 do {
451 p = strchr(p, '%');
452 if (p && p[1] >= '1' && p[1] <= '9')
453 return 0; // pattern contains sequence number, so it's not unique
clang-format6c4d83e2016-08-08 19:03:30 -0700454 if (p) p++;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800455 } while (p);
456
457 return 1;
458}
459
460static void print_md5(unsigned char digest[16], const char *filename) {
461 int i;
462
clang-format6c4d83e2016-08-08 19:03:30 -0700463 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800464 printf(" %s\n", filename);
465}
466
467static FILE *open_outfile(const char *name) {
468 if (strcmp("-", name) == 0) {
469 set_binary_mode(stdout);
470 return stdout;
471 } else {
472 FILE *file = fopen(name, "wb");
clang-format6c4d83e2016-08-08 19:03:30 -0700473 if (!file) fatal("Failed to open output file '%s'", name);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800474 return file;
475 }
476}
477
Yaowu Xuf883b422016-08-30 14:01:10 -0700478static int img_shifted_realloc_required(const aom_image_t *img,
479 const aom_image_t *shifted,
480 aom_img_fmt_t required_fmt) {
clang-format6c4d83e2016-08-08 19:03:30 -0700481 return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700482 required_fmt != shifted->fmt;
483}
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700484
James Zern6a2e0f02015-05-09 10:42:31 -0700485static int main_loop(int argc, const char **argv_) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700486 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700487 char *fn = NULL;
488 int i;
James Zern360e4442016-11-16 21:53:55 -0800489 int ret = EXIT_FAILURE;
clang-format6c4d83e2016-08-08 19:03:30 -0700490 uint8_t *buf = NULL;
491 size_t bytes_in_buffer = 0, buffer_size = 0;
492 FILE *infile;
493 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
494 int do_md5 = 0, progress = 0, frame_parallel = 0;
495 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
496 int arg_skip = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700497 int keep_going = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700498 const AvxInterface *interface = NULL;
499 const AvxInterface *fourcc_interface = NULL;
James Yu6d151322014-02-22 14:51:11 +0800500 uint64_t dx_time = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700501 struct arg arg;
502 char **argv, **argi, **argj;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800503
clang-format6c4d83e2016-08-08 19:03:30 -0700504 int single_file;
505 int use_y4m = 1;
506 int opt_yv12 = 0;
507 int opt_i420 = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800508 int opt_raw = 0;
Imdad Sardharwallab5def022017-12-14 11:20:24 +0000509 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH };
clang-format6c4d83e2016-08-08 19:03:30 -0700510 unsigned int output_bit_depth = 0;
Geza Lore490ba1a2016-03-11 17:42:49 +0000511#if CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700512 int tile_row = -1;
513 int tile_col = -1;
Geza Lore490ba1a2016-03-11 17:42:49 +0000514#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700515 int frames_corrupted = 0;
516 int dec_flags = 0;
517 int do_scale = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700518 aom_image_t *scaled_img = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700519 aom_image_t *img_shifted = NULL;
clang-format6c4d83e2016-08-08 19:03:30 -0700520 int frame_avail, got_data, flush_decoder = 0;
521 int num_external_frame_buffers = 0;
522 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400523
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800524 const char *outfile_pattern = NULL;
clang-format6c4d83e2016-08-08 19:03:30 -0700525 char outfile_name[PATH_MAX] = { 0 };
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800526 FILE *outfile = NULL;
527
Peter Boströma1f64322017-01-19 11:35:30 -0500528 FILE *framestats_file = NULL;
529
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800530 MD5Context md5_ctx;
531 unsigned char md5_digest[16];
532
Yaowu Xuf883b422016-08-30 14:01:10 -0700533 struct AvxDecInputContext input = { NULL, NULL };
534 struct AvxInputContext aom_input_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700535#if CONFIG_WEBM_IO
Jim Bankoski7ae32692014-08-19 06:44:10 -0700536 struct WebmInputContext webm_ctx;
537 memset(&(webm_ctx), 0, sizeof(webm_ctx));
Tom Finegan2abe2d42013-11-18 14:39:51 -0800538 input.webm_ctx = &webm_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700539#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700540 input.aom_input_ctx = &aom_input_ctx;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800541
John Koleszarc6b90392012-07-13 15:21:29 -0700542 /* Parse command line */
543 exec_name = argv_[0];
544 argv = argv_dup(argc - 1, argv_ + 1);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400545
John Koleszarc6b90392012-07-13 15:21:29 -0700546 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
547 memset(&arg, 0, sizeof(arg));
548 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400549
James Zernfd8c78c2017-11-27 16:37:33 -0800550 if (arg_match(&arg, &help, argi)) {
551 show_help(stdout, 0);
552 exit(EXIT_SUCCESS);
553 } else if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700554 interface = get_aom_decoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800555 if (!interface)
556 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszar9596a4c2013-06-12 16:08:35 -0700557 } else if (arg_match(&arg, &looparg, argi)) {
558 // no-op
Peter Boströma1f64322017-01-19 11:35:30 -0500559 } else if (arg_match(&arg, &outputfile, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700560 outfile_pattern = arg.val;
Peter Boströma1f64322017-01-19 11:35:30 -0500561 } else if (arg_match(&arg, &use_yv12, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700562 use_y4m = 0;
563 flipuv = 1;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700564 opt_yv12 = 1;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800565 opt_i420 = 0;
566 opt_raw = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700567 } else if (arg_match(&arg, &use_i420, argi)) {
568 use_y4m = 0;
569 flipuv = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800570 opt_yv12 = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700571 opt_i420 = 1;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800572 opt_raw = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700573 } else if (arg_match(&arg, &rawvideo, argi)) {
574 use_y4m = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800575 opt_yv12 = 0;
576 opt_i420 = 0;
577 opt_raw = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500578 } else if (arg_match(&arg, &flipuvarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700579 flipuv = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500580 } else if (arg_match(&arg, &noblitarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700581 noblit = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500582 } else if (arg_match(&arg, &progressarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700583 progress = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500584 } else if (arg_match(&arg, &limitarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700585 stop_after = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500586 } else if (arg_match(&arg, &skiparg, argi)) {
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700587 arg_skip = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500588 } else if (arg_match(&arg, &postprocarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700589 postproc = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500590 } else if (arg_match(&arg, &md5arg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700591 do_md5 = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500592 } else if (arg_match(&arg, &framestatsarg, argi)) {
593 framestats_file = fopen(arg.val, "w");
594 if (!framestats_file) {
595 die("Error: Could not open --framestats file (%s) for writing.\n",
596 arg.val);
597 }
598 } else if (arg_match(&arg, &summaryarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700599 summary = 1;
Peter Boströma1f64322017-01-19 11:35:30 -0500600 } else if (arg_match(&arg, &threadsarg, argi)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700601 cfg.threads = arg_parse_uint(&arg);
Peter Boströma1f64322017-01-19 11:35:30 -0500602 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700603#if CONFIG_AV1_DECODER
hkuang1f841c32015-03-06 13:03:18 -0800604 else if (arg_match(&arg, &frameparallelarg, argi))
605 frame_parallel = 1;
606#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700607 else if (arg_match(&arg, &verbosearg, argi))
608 quiet = 0;
John Koleszar7d8fc262013-02-25 16:58:47 -0800609 else if (arg_match(&arg, &scalearg, argi))
610 do_scale = 1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800611 else if (arg_match(&arg, &fb_arg, argi))
612 num_external_frame_buffers = arg_parse_uint(&arg);
James Zern7690d882014-08-22 18:01:28 -0700613 else if (arg_match(&arg, &continuearg, argi))
614 keep_going = 1;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700615 else if (arg_match(&arg, &outbitdeptharg, argi)) {
616 output_bit_depth = arg_parse_uint(&arg);
617 }
Geza Lore490ba1a2016-03-11 17:42:49 +0000618#if CONFIG_EXT_TILE
619 else if (arg_match(&arg, &tiler, argi))
620 tile_row = arg_parse_int(&arg);
621 else if (arg_match(&arg, &tilec, argi))
622 tile_col = arg_parse_int(&arg);
623#endif // CONFIG_EXT_TILE
John Koleszarcfe3f912010-10-20 10:49:12 -0400624 else
John Koleszarc6b90392012-07-13 15:21:29 -0700625 argj++;
626 }
627
628 /* Check for unrecognized options */
629 for (argi = argv; *argi; argi++)
630 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
631 die("Error: Unrecognized option %s\n", *argi);
632
633 /* Handle non-option arguments */
634 fn = argv[0];
635
Hanno Böckb5d0a202015-02-24 15:48:21 -0800636 if (!fn) {
637 free(argv);
James Zernfd8c78c2017-11-27 16:37:33 -0800638 fprintf(stderr, "No input file specified!\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700639 usage_exit();
Hanno Böckb5d0a202015-02-24 15:48:21 -0800640 }
John Koleszarc6b90392012-07-13 15:21:29 -0700641 /* Open file */
642 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
643
644 if (!infile) {
James Zern6b993f02015-02-05 21:06:29 -0800645 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
John Koleszarc6b90392012-07-13 15:21:29 -0700646 }
647#if CONFIG_OS_SUPPORT
648 /* Make sure we don't dump to the terminal, unless forced to with -o - */
James Zernb9b28382016-12-09 16:50:39 -0800649 if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700650 fprintf(stderr,
651 "Not dumping raw video to your terminal. Use '-o -' to "
652 "override.\n");
653 return EXIT_FAILURE;
654 }
655#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700656 input.aom_input_ctx->file = infile;
657 if (file_is_ivf(input.aom_input_ctx))
658 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700659#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700660 else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
661 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700662#endif
Cyril Concolato6c788832017-10-30 16:30:35 -0700663#if CONFIG_OBU_NO_IVF
664 else if (file_is_obu(input.aom_input_ctx))
665 input.aom_input_ctx->file_type = FILE_TYPE_OBU;
666#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700667 else if (file_is_raw(input.aom_input_ctx))
668 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700669 else {
670 fprintf(stderr, "Unrecognized input file type.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700671#if !CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700672 fprintf(stderr, "aomdec was built without WebM container support.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700673#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700674 return EXIT_FAILURE;
675 }
676
John Koleszarc6b90392012-07-13 15:21:29 -0700677 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800678 single_file = is_single_file(outfile_pattern);
John Koleszarc6b90392012-07-13 15:21:29 -0700679
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800680 if (!noblit && single_file) {
681 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
Yaowu Xuf883b422016-08-30 14:01:10 -0700682 aom_input_ctx.width, aom_input_ctx.height, 0);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800683 if (do_md5)
684 MD5Init(&md5_ctx);
685 else
686 outfile = open_outfile(outfile_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700687 }
688
689 if (use_y4m && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700690 if (!single_file) {
clang-format6c4d83e2016-08-08 19:03:30 -0700691 fprintf(stderr,
692 "YUV4MPEG2 not supported with output patterns,"
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700693 " try --i420 or --yv12 or --rawvideo.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700694 return EXIT_FAILURE;
695 }
696
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700697#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700698 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
699 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
clang-format6c4d83e2016-08-08 19:03:30 -0700700 fprintf(stderr,
701 "Failed to guess framerate -- error parsing "
John Koleszarc6b90392012-07-13 15:21:29 -0700702 "webm file?\n");
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400703 return EXIT_FAILURE;
John Koleszarc6b90392012-07-13 15:21:29 -0700704 }
Dmitry Kovalev5ab63582014-01-17 17:02:37 -0800705 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700706#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700707 }
708
Yaowu Xuf883b422016-08-30 14:01:10 -0700709 fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800710 if (interface && fourcc_interface && interface != fourcc_interface)
711 warn("Header indicates codec: %s\n", fourcc_interface->name);
712 else
713 interface = fourcc_interface;
John Koleszarc6b90392012-07-13 15:21:29 -0700714
Yaowu Xuf883b422016-08-30 14:01:10 -0700715 if (!interface) interface = get_aom_decoder_by_index(0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400716
Yaowu Xuf883b422016-08-30 14:01:10 -0700717 dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0) |
Yaowu Xuf883b422016-08-30 14:01:10 -0700718 (frame_parallel ? AOM_CODEC_USE_FRAME_THREADING : 0);
719 if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
clang-format6c4d83e2016-08-08 19:03:30 -0700720 dec_flags)) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800721 fprintf(stderr, "Failed to initialize decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700722 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800723 goto fail2;
John Koleszarc6b90392012-07-13 15:21:29 -0700724 }
John Koleszar933d44b2010-10-21 20:40:42 -0700725
clang-format6c4d83e2016-08-08 19:03:30 -0700726 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400727
Yaowu Xuf883b422016-08-30 14:01:10 -0700728#if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
Jingning Hanf79d48d2016-12-20 14:52:35 -0800729 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_ROW, tile_row)) {
730 fprintf(stderr, "Failed to set decode_tile_row: %s\n",
731 aom_codec_error(&decoder));
732 goto fail;
733 }
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700734
Jingning Hanf79d48d2016-12-20 14:52:35 -0800735 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_COL, tile_col)) {
736 fprintf(stderr, "Failed to set decode_tile_col: %s\n",
737 aom_codec_error(&decoder));
738 goto fail;
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700739 }
740#endif
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700741
clang-format6c4d83e2016-08-08 19:03:30 -0700742 if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700743 while (arg_skip) {
clang-format6c4d83e2016-08-08 19:03:30 -0700744 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700745 arg_skip--;
746 }
747
Frank Galligana4f30a52014-02-06 17:13:08 -0800748 if (num_external_frame_buffers > 0) {
749 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
750 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
751 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
Yaowu Xuf883b422016-08-30 14:01:10 -0700752 if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
753 release_av1_frame_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700754 &ext_fb_list)) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800755 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700756 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800757 goto fail;
Frank Galligana4f30a52014-02-06 17:13:08 -0800758 }
759 }
760
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400761 frame_avail = 1;
762 got_data = 0;
763
Peter Boströma1f64322017-01-19 11:35:30 -0500764 if (framestats_file) fprintf(framestats_file, "bytes,qp\r\n");
765
John Koleszarc6b90392012-07-13 15:21:29 -0700766 /* Decode file */
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400767 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700768 aom_codec_iter_t iter = NULL;
769 aom_image_t *img;
770 struct aom_usec_timer timer;
clang-format6c4d83e2016-08-08 19:03:30 -0700771 int corrupted = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400772
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400773 frame_avail = 0;
774 if (!stop_after || frame_in < stop_after) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800775 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400776 frame_avail = 1;
777 frame_in++;
778
Yaowu Xuf883b422016-08-30 14:01:10 -0700779 aom_usec_timer_start(&timer);
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400780
Yaowu Xuf883b422016-08-30 14:01:10 -0700781 if (aom_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer, NULL,
clang-format6c4d83e2016-08-08 19:03:30 -0700782 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700783 const char *detail = aom_codec_error_detail(&decoder);
clang-format6c4d83e2016-08-08 19:03:30 -0700784 warn("Failed to decode frame %d: %s", frame_in,
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 aom_codec_error(&decoder));
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400786
clang-format6c4d83e2016-08-08 19:03:30 -0700787 if (detail) warn("Additional information: %s", detail);
788 if (!keep_going) goto fail;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400789 }
790
Peter Boströma1f64322017-01-19 11:35:30 -0500791 if (framestats_file) {
792 int qp;
793 if (aom_codec_control(&decoder, AOMD_GET_LAST_QUANTIZER, &qp)) {
794 warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
795 aom_codec_error(&decoder));
796 if (!keep_going) goto fail;
797 }
798 fprintf(framestats_file, "%d,%d\r\n", (int)bytes_in_buffer, qp);
799 }
800
Yaowu Xuf883b422016-08-30 14:01:10 -0700801 aom_usec_timer_mark(&timer);
802 dx_time += aom_usec_timer_elapsed(&timer);
hkuang1f841c32015-03-06 13:03:18 -0800803 } else {
804 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400805 }
hkuang1f841c32015-03-06 13:03:18 -0800806 } else {
807 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400808 }
809
Yaowu Xuf883b422016-08-30 14:01:10 -0700810 aom_usec_timer_start(&timer);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400811
hkuang1f841c32015-03-06 13:03:18 -0800812 if (flush_decoder) {
813 // Flush the decoder in frame parallel decode.
Yaowu Xuf883b422016-08-30 14:01:10 -0700814 if (aom_codec_decode(&decoder, NULL, 0, NULL, 0)) {
815 warn("Failed to flush decoder: %s", aom_codec_error(&decoder));
hkuang1f841c32015-03-06 13:03:18 -0800816 }
817 }
818
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400819 got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700820 if ((img = aom_codec_get_frame(&decoder, &iter))) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400821 ++frame_out;
822 got_data = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400823 }
824
Yaowu Xuf883b422016-08-30 14:01:10 -0700825 aom_usec_timer_mark(&timer);
826 dx_time += (unsigned int)aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -0700827
hkuang1f841c32015-03-06 13:03:18 -0800828 if (!frame_parallel &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700829 aom_codec_control(&decoder, AOMD_GET_FRAME_CORRUPTED, &corrupted)) {
830 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder));
clang-format6c4d83e2016-08-08 19:03:30 -0700831 if (!keep_going) goto fail;
John Koleszarc6b90392012-07-13 15:21:29 -0700832 }
833 frames_corrupted += corrupted;
834
clang-format6c4d83e2016-08-08 19:03:30 -0700835 if (progress) show_progress(frame_in, frame_out, dx_time);
John Koleszarc6b90392012-07-13 15:21:29 -0700836
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800837 if (!noblit && img) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700838 const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
839 const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800840 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
John Koleszarda584362013-05-06 15:52:06 -0700841
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800842 if (do_scale) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800843 if (frame_out == 1) {
844 // If the output frames are to be scaled to a fixed display size then
845 // use the width and height specified in the container. If either of
846 // these is set to 0, use the display size set in the first frame
Adrian Grange82f6af62014-01-22 09:36:07 -0800847 // header. If that is unavailable, use the raw decoded size of the
848 // first decoded frame.
Yaowu Xuf883b422016-08-30 14:01:10 -0700849 int render_width = aom_input_ctx.width;
850 int render_height = aom_input_ctx.height;
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400851 if (!render_width || !render_height) {
852 int render_size[2];
Yaowu Xuf883b422016-08-30 14:01:10 -0700853 if (aom_codec_control(&decoder, AV1D_GET_DISPLAY_SIZE,
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400854 render_size)) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800855 // As last resort use size of first frame as display size.
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400856 render_width = img->d_w;
857 render_height = img->d_h;
Adrian Grange482c39c2013-12-13 10:15:03 -0800858 } else {
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400859 render_width = render_size[0];
860 render_height = render_size[1];
Adrian Grange482c39c2013-12-13 10:15:03 -0800861 }
Adrian Grange2117fe02013-11-19 14:01:44 -0800862 }
clang-format6c4d83e2016-08-08 19:03:30 -0700863 scaled_img =
Yaowu Xuf883b422016-08-30 14:01:10 -0700864 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
Deb Mukherjee449e5f22014-07-11 11:43:31 -0700865 scaled_img->bit_depth = img->bit_depth;
John Koleszar7d8fc262013-02-25 16:58:47 -0800866 }
Adrian Grange482c39c2013-12-13 10:15:03 -0800867
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800868 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
Deb Mukherjee47031c02014-05-16 18:52:01 -0700869#if CONFIG_LIBYUV
Tom Finegan8ef2ce82014-12-12 11:57:04 -0800870 libyuv_scale(img, scaled_img, kFilterBox);
John Koleszar7d8fc262013-02-25 16:58:47 -0800871 img = scaled_img;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700872#else
clang-format6c4d83e2016-08-08 19:03:30 -0700873 fprintf(stderr,
Johanne07a6752018-01-10 12:47:44 -0800874 "Failed to scale output frame: %s.\n"
875 "libyuv is required for scaling but is currently disabled.\n"
876 "Be sure to specify -DCONFIG_LIBYUV=1 when running cmake.\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700877 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800878 goto fail;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700879#endif
John Koleszar7d8fc262013-02-25 16:58:47 -0800880 }
881 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700882 // Default to codec bit depth if output bit depth not set
Ronald S. Bultjeeba342a2015-09-11 16:55:19 -0400883 if (!output_bit_depth && single_file && !do_md5) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700884 output_bit_depth = img->bit_depth;
885 }
886 // Shift up or down if necessary
Sebastien Alaiwan18605a32017-05-12 10:46:09 +0200887 if (output_bit_depth != 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700888 const aom_img_fmt_t shifted_fmt =
clang-format6c4d83e2016-08-08 19:03:30 -0700889 output_bit_depth == 8
Yaowu Xuf883b422016-08-30 14:01:10 -0700890 ? img->fmt ^ (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
891 : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
Sebastien Alaiwan18605a32017-05-12 10:46:09 +0200892
893 if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
894 if (img_shifted &&
895 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
896 aom_img_free(img_shifted);
897 img_shifted = NULL;
898 }
899 if (!img_shifted) {
900 img_shifted =
901 aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
902 img_shifted->bit_depth = output_bit_depth;
903 }
904 if (output_bit_depth > img->bit_depth) {
905 aom_img_upshift(img_shifted, img,
906 output_bit_depth - img->bit_depth);
907 } else {
908 aom_img_downshift(img_shifted, img,
909 img->bit_depth - output_bit_depth);
910 }
911 img = img_shifted;
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700912 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700913 }
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800914
Geza Lore490ba1a2016-03-11 17:42:49 +0000915#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700916 aom_input_ctx.width = img->d_w;
917 aom_input_ctx.height = img->d_h;
Geza Lore490ba1a2016-03-11 17:42:49 +0000918#endif // CONFIG_EXT_TILE
919
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800920#if CONFIG_MONO_VIDEO
921 int num_planes = (!use_y4m && opt_raw && img->monochrome) ? 1 : 3;
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000922#else
923 int num_planes = 3;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800924 (void)opt_raw;
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000925#endif
926
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800927 if (single_file) {
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800928 if (use_y4m) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700929 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800930 size_t len = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700931 if (img->fmt == AOM_IMG_FMT_I440 || img->fmt == AOM_IMG_FMT_I44016) {
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700932 fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
933 goto fail;
934 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800935 if (frame_out == 1) {
936 // Y4M file header
clang-format6c4d83e2016-08-08 19:03:30 -0700937 len = y4m_write_file_header(
Urvang Joshi4145bf02016-10-17 14:53:33 -0700938 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
939 aom_input_ctx.height, &aom_input_ctx.framerate, img->fmt,
940 img->bit_depth);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800941 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700942 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800943 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700944 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800945 }
946 }
947
948 // Y4M frame header
Urvang Joshi4145bf02016-10-17 14:53:33 -0700949 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800950 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700951 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800952 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700953 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800954 }
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700955 } else {
956 if (frame_out == 1) {
957 // Check if --yv12 or --i420 options are consistent with the
958 // bit-stream decoded
959 if (opt_i420) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700960 if (img->fmt != AOM_IMG_FMT_I420 &&
961 img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700962 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
963 goto fail;
964 }
965 }
966 if (opt_yv12) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700967 if ((img->fmt != AOM_IMG_FMT_I420 &&
968 img->fmt != AOM_IMG_FMT_YV12) ||
clang-format6c4d83e2016-08-08 19:03:30 -0700969 img->bit_depth != 8) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700970 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
971 goto fail;
972 }
973 }
974 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800975 }
976
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800977 if (do_md5) {
978 update_image_md5(img, planes, &md5_ctx);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800979 } else {
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000980 write_image_file(img, planes, num_planes, outfile);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800981 }
982 } else {
clang-format6c4d83e2016-08-08 19:03:30 -0700983 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
984 img->d_h, frame_in);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800985 if (do_md5) {
986 MD5Init(&md5_ctx);
987 update_image_md5(img, planes, &md5_ctx);
988 MD5Final(md5_digest, &md5_ctx);
989 print_md5(md5_digest, outfile_name);
990 } else {
991 outfile = open_outfile(outfile_name);
Imdad Sardharwalla730c8052017-11-30 17:39:15 +0000992 write_image_file(img, planes, num_planes, outfile);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800993 fclose(outfile);
John Koleszarc6b90392012-07-13 15:21:29 -0700994 }
John Koleszarc6b90392012-07-13 15:21:29 -0700995 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400996 }
John Koleszarc6b90392012-07-13 15:21:29 -0700997 }
998
999 if (summary || progress) {
1000 show_progress(frame_in, frame_out, dx_time);
1001 fprintf(stderr, "\n");
1002 }
1003
James Zern360e4442016-11-16 21:53:55 -08001004 if (frames_corrupted) {
John Koleszarc6b90392012-07-13 15:21:29 -07001005 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
James Zern360e4442016-11-16 21:53:55 -08001006 } else {
1007 ret = EXIT_SUCCESS;
1008 }
John Koleszar4226f0c2011-04-27 12:04:48 -04001009
John Koleszar0ea50ce2010-05-18 11:58:33 -04001010fail:
1011
Yaowu Xuf883b422016-08-30 14:01:10 -07001012 if (aom_codec_destroy(&decoder)) {
Adrian Granged427fab2013-11-21 17:19:04 -08001013 fprintf(stderr, "Failed to destroy decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001014 aom_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -07001015 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001016
James Zern360e4442016-11-16 21:53:55 -08001017fail2:
1018
Dmitry Kovalev347b3062014-01-22 09:04:07 -08001019 if (!noblit && single_file) {
1020 if (do_md5) {
1021 MD5Final(md5_digest, &md5_ctx);
1022 print_md5(md5_digest, outfile_name);
1023 } else {
1024 fclose(outfile);
1025 }
1026 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001027
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001028#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -07001029 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
Tom Finegan2abe2d42013-11-18 14:39:51 -08001030 webm_free(input.webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001031#endif
1032
Yaowu Xuf883b422016-08-30 14:01:10 -07001033 if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
Tom Finegan2abe2d42013-11-18 14:39:51 -08001034
Yaowu Xuf883b422016-08-30 14:01:10 -07001035 if (scaled_img) aom_img_free(scaled_img);
Yaowu Xuf883b422016-08-30 14:01:10 -07001036 if (img_shifted) aom_img_free(img_shifted);
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}