blob: 42ae91bc67461a1cad4baad12b43f4c9f15e4ca8 [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
21#if CONFIG_LIBYUV
Tom Finegan2abe2d42013-11-18 14:39:51 -080022#include "third_party/libyuv/include/libyuv/scale.h"
James Zern1e681d92014-08-15 21:00:09 -070023#endif
Tom Finegan2abe2d42013-11-18 14:39:51 -080024
25#include "./args.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080026#include "./ivfdec.h"
27
Yaowu Xuf883b422016-08-30 14:01:10 -070028#include "aom/aom_decoder.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070030#include "aom_ports/aom_timer.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080031
Yaowu Xuf883b422016-08-30 14:01:10 -070032#if CONFIG_AV1_DECODER
33#include "aom/aomdx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040034#endif
Tom Finegan00a35aa2013-11-14 12:37:42 -080035
Tom Finegan2abe2d42013-11-18 14:39:51 -080036#include "./md5_utils.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080037
38#include "./tools_common.h"
James Zernb6cce762014-05-10 17:44:12 -070039#if CONFIG_WEBM_IO
Tom Finegan2abe2d42013-11-18 14:39:51 -080040#include "./webmdec.h"
James Zernb6cce762014-05-10 17:44:12 -070041#endif
Dmitry Kovalev5ab635832014-01-17 17:02:37 -080042#include "./y4menc.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040043
44static const char *exec_name;
45
Yaowu Xuf883b422016-08-30 14:01:10 -070046struct AvxDecInputContext {
47 struct AvxInputContext *aom_input_ctx;
Tom Finegan2abe2d42013-11-18 14:39:51 -080048 struct WebmInputContext *webm_ctx;
49};
50
clang-format6c4d83e2016-08-08 19:03:30 -070051static const arg_def_t looparg =
52 ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
53static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
54static const arg_def_t use_yv12 =
55 ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
56static const arg_def_t use_i420 =
57 ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
58static const arg_def_t flipuvarg =
59 ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
60static const arg_def_t rawvideo =
61 ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
62static const arg_def_t noblitarg =
63 ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
64static const arg_def_t progressarg =
65 ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
66static const arg_def_t limitarg =
67 ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
68static const arg_def_t skiparg =
69 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
70static const arg_def_t postprocarg =
71 ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
72static const arg_def_t summaryarg =
73 ARG_DEF(NULL, "summary", 0, "Show timing summary");
74static const arg_def_t outputfile =
75 ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
76static const arg_def_t threadsarg =
77 ARG_DEF("t", "threads", 1, "Max threads to use");
78static const arg_def_t frameparallelarg =
79 ARG_DEF(NULL, "frame-parallel", 0, "Frame parallel decode");
80static const arg_def_t verbosearg =
81 ARG_DEF("v", "verbose", 0, "Show version string");
82static const arg_def_t error_concealment =
83 ARG_DEF(NULL, "error-concealment", 0, "Enable decoder error-concealment");
84static const arg_def_t scalearg =
85 ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
86static const arg_def_t continuearg =
87 ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
88static const arg_def_t fb_arg =
89 ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
90static const arg_def_t md5arg =
91 ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
Yaowu Xuf883b422016-08-30 14:01:10 -070092#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -070093static const arg_def_t outbitdeptharg =
94 ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070095#endif
Geza Lore490ba1a2016-03-11 17:42:49 +000096#if CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -070097static const arg_def_t tiler = ARG_DEF(NULL, "tile-row", 1,
98 "Row index of tile to decode "
99 "(-1 for all rows)");
100static const arg_def_t tilec = ARG_DEF(NULL, "tile-column", 1,
101 "Column index of tile to decode "
102 "(-1 for all columns)");
Geza Lore490ba1a2016-03-11 17:42:49 +0000103#endif // CONFIG_EXT_TILE
Dmitry Kovalevd24f4e42013-12-27 16:10:18 -0800104
clang-format6c4d83e2016-08-08 19:03:30 -0700105static const arg_def_t *all_args[] = { &codecarg,
106 &use_yv12,
107 &use_i420,
108 &flipuvarg,
109 &rawvideo,
110 &noblitarg,
111 &progressarg,
112 &limitarg,
113 &skiparg,
114 &postprocarg,
115 &summaryarg,
116 &outputfile,
117 &threadsarg,
118 &frameparallelarg,
119 &verbosearg,
120 &scalearg,
121 &fb_arg,
122 &md5arg,
123 &error_concealment,
124 &continuearg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700125#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700126 &outbitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700127#endif
Geza Lore490ba1a2016-03-11 17:42:49 +0000128#if CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700129 &tiler,
130 &tilec,
Geza Lore490ba1a2016-03-11 17:42:49 +0000131#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700132 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400133
James Zern1e681d92014-08-15 21:00:09 -0700134#if CONFIG_LIBYUV
Yaowu Xuf883b422016-08-30 14:01:10 -0700135static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
clang-format6c4d83e2016-08-08 19:03:30 -0700136 FilterModeEnum mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700137#if CONFIG_AOM_HIGHBITDEPTH
138 if (src->fmt == AOM_IMG_FMT_I42016) {
139 assert(dst->fmt == AOM_IMG_FMT_I42016);
clang-format6c4d83e2016-08-08 19:03:30 -0700140 return I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -0700141 (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
142 (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
143 (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
144 src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
145 dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
146 dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
147 dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700148 }
149#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700150 assert(src->fmt == AOM_IMG_FMT_I420);
151 assert(dst->fmt == AOM_IMG_FMT_I420);
152 return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
153 src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
154 src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
155 src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
156 dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
157 dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -0700158 dst->d_h, mode);
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800159}
James Zern1e681d92014-08-15 21:00:09 -0700160#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800161
James Zern59e7a472015-05-09 10:33:26 -0700162void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700163 int i;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400164
clang-format6c4d83e2016-08-08 19:03:30 -0700165 fprintf(stderr,
166 "Usage: %s <options> filename\n\n"
167 "Options:\n",
168 exec_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700169 arg_show_usage(stderr, all_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700170 fprintf(stderr,
171 "\nOutput File Patterns:\n\n"
172 " The -o argument specifies the name of the file(s) to "
173 "write to. If the\n argument does not include any escape "
174 "characters, the output will be\n written to a single file. "
175 "Otherwise, the filename will be calculated by\n expanding "
John Koleszar3707c342012-11-06 10:09:36 -0800176 "the following escape characters:\n");
177 fprintf(stderr,
John Koleszarc6b90392012-07-13 15:21:29 -0700178 "\n\t%%w - Frame width"
179 "\n\t%%h - Frame height"
180 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
181 "\n\n Pattern arguments are only supported in conjunction "
182 "with the --yv12 and\n --i420 options. If the -o option is "
clang-format6c4d83e2016-08-08 19:03:30 -0700183 "not specified, the output will be\n directed to stdout.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700184 fprintf(stderr, "\nIncluded decoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400185
Yaowu Xuf883b422016-08-30 14:01:10 -0700186 for (i = 0; i < get_aom_decoder_count(); ++i) {
187 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700188 fprintf(stderr, " %-6s - %s\n", decoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700189 aom_codec_iface_name(decoder->codec_interface()));
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800190 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400191
John Koleszarc6b90392012-07-13 15:21:29 -0700192 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400193}
194
clang-format6c4d83e2016-08-08 19:03:30 -0700195static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
196 size_t *buffer_size) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800197 char raw_hdr[RAW_FRAME_HDR_SZ];
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800198 size_t frame_size = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700199
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800200 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
clang-format6c4d83e2016-08-08 19:03:30 -0700201 if (!feof(infile)) warn("Failed to read RAW frame size\n");
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800202 } else {
James Zern75d7ee32014-02-04 20:06:28 -0800203 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
204 const size_t kFrameTooSmallThreshold = 256 * 1024;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800205 frame_size = mem_get_le32(raw_hdr);
John Koleszarc6b90392012-07-13 15:21:29 -0700206
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800207 if (frame_size > kCorruptFrameThreshold) {
208 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
209 frame_size = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400210 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400211
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800212 if (frame_size < kFrameTooSmallThreshold) {
213 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
214 (unsigned int)frame_size);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400215 }
216
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800217 if (frame_size > *buffer_size) {
218 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
219 if (new_buf) {
220 *buffer = new_buf;
221 *buffer_size = 2 * frame_size;
222 } else {
223 warn("Failed to allocate compressed data buffer\n");
224 frame_size = 0;
225 }
226 }
John Koleszarc6b90392012-07-13 15:21:29 -0700227 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400228
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800229 if (!feof(infile)) {
230 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
231 warn("Failed to read full frame\n");
232 return 1;
233 }
234 *bytes_read = frame_size;
235 }
236
237 return 0;
238}
239
Yaowu Xuf883b422016-08-30 14:01:10 -0700240static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800241 size_t *bytes_in_buffer, size_t *buffer_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700242 switch (input->aom_input_ctx->file_type) {
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700243#if CONFIG_WEBM_IO
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800244 case FILE_TYPE_WEBM:
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700245 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700246#endif
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800247 case FILE_TYPE_RAW:
Yaowu Xuf883b422016-08-30 14:01:10 -0700248 return raw_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700249 buffer_size);
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800250 case FILE_TYPE_IVF:
Yaowu Xuf883b422016-08-30 14:01:10 -0700251 return ivf_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700252 buffer_size);
253 default: return 1;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800254 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400255}
256
Yaowu Xuf883b422016-08-30 14:01:10 -0700257static void update_image_md5(const aom_image_t *img, const int planes[3],
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800258 MD5Context *md5) {
259 int i, y;
260
261 for (i = 0; i < 3; ++i) {
262 const int plane = planes[i];
263 const unsigned char *buf = img->planes[plane];
264 const int stride = img->stride[plane];
Yaowu Xuf883b422016-08-30 14:01:10 -0700265 const int w = aom_img_plane_width(img, plane) *
266 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
267 const int h = aom_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800268
269 for (y = 0; y < h; ++y) {
270 MD5Update(md5, buf, w);
271 buf += stride;
272 }
273 }
274}
275
Yaowu Xuf883b422016-08-30 14:01:10 -0700276static void write_image_file(const aom_image_t *img, const int planes[3],
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800277 FILE *file) {
278 int i, y;
Yaowu Xuf883b422016-08-30 14:01:10 -0700279#if CONFIG_AOM_HIGHBITDEPTH
280 const int bytes_per_sample = ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700281#else
282 const int bytes_per_sample = 1;
283#endif
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800284
285 for (i = 0; i < 3; ++i) {
286 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
John Koleszarc6b90392012-07-13 15:21:29 -0700304 si.sz = sizeof(si);
John Koleszar19255b82010-11-23 13:40:31 -0500305
Tom Finegan00a35aa2013-11-14 12:37:42 -0800306 if (fread(buf, 1, 32, input->file) == 32) {
John Koleszarc6b90392012-07-13 15:21:29 -0700307 int i;
John Koleszarcfe3f912010-10-20 10:49:12 -0400308
Tom Finegan00a35aa2013-11-14 12:37:42 -0800309 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700310 for (i = 0; i < get_aom_decoder_count(); ++i) {
311 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
312 if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
clang-format6c4d83e2016-08-08 19:03:30 -0700313 32 - 4, &si)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700314 is_raw = 1;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800315 input->fourcc = decoder->fourcc;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800316 input->width = si.w;
317 input->height = si.h;
318 input->framerate.numerator = 30;
319 input->framerate.denominator = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700320 break;
321 }
Tom Finegan00a35aa2013-11-14 12:37:42 -0800322 }
323 }
John Koleszarc6b90392012-07-13 15:21:29 -0700324 }
John Koleszarcfe3f912010-10-20 10:49:12 -0400325
Tom Finegan00a35aa2013-11-14 12:37:42 -0800326 rewind(input->file);
John Koleszarc6b90392012-07-13 15:21:29 -0700327 return is_raw;
John Koleszarcfe3f912010-10-20 10:49:12 -0400328}
329
James Zern6a2e0f02015-05-09 10:42:31 -0700330static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
James Yu6d151322014-02-22 14:51:11 +0800331 fprintf(stderr,
clang-format6c4d83e2016-08-08 19:03:30 -0700332 "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
John Koleszarc6b90392012-07-13 15:21:29 -0700333 frame_in, frame_out, dx_time,
James Yu6d151322014-02-22 14:51:11 +0800334 (double)frame_out * 1000000.0 / (double)dx_time);
John Koleszar5d12e042010-10-21 17:28:34 -0400335}
336
Frank Galligana4f30a52014-02-06 17:13:08 -0800337struct ExternalFrameBuffer {
clang-format6c4d83e2016-08-08 19:03:30 -0700338 uint8_t *data;
Frank Galligana4f30a52014-02-06 17:13:08 -0800339 size_t size;
340 int in_use;
341};
342
343struct ExternalFrameBufferList {
344 int num_external_frame_buffers;
345 struct ExternalFrameBuffer *ext_fb;
346};
347
Yaowu Xuc27fc142016-08-22 16:08:15 -0700348// Callback used by libaom to request an external frame buffer. |cb_priv|
Frank Galligana4f30a52014-02-06 17:13:08 -0800349// Application private data passed into the set function. |min_size| is the
350// minimum size in bytes needed to decode the next frame. |fb| pointer to the
351// frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700352static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
353 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800354 int i;
355 struct ExternalFrameBufferList *const ext_fb_list =
356 (struct ExternalFrameBufferList *)cb_priv;
clang-format6c4d83e2016-08-08 19:03:30 -0700357 if (ext_fb_list == NULL) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800358
359 // Find a free frame buffer.
360 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
clang-format6c4d83e2016-08-08 19:03:30 -0700361 if (!ext_fb_list->ext_fb[i].in_use) break;
Frank Galligana4f30a52014-02-06 17:13:08 -0800362 }
363
clang-format6c4d83e2016-08-08 19:03:30 -0700364 if (i == ext_fb_list->num_external_frame_buffers) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800365
366 if (ext_fb_list->ext_fb[i].size < min_size) {
367 free(ext_fb_list->ext_fb[i].data);
Frank Galligan175d9df2014-09-11 08:39:38 -0700368 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
clang-format6c4d83e2016-08-08 19:03:30 -0700369 if (!ext_fb_list->ext_fb[i].data) return -1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800370
371 ext_fb_list->ext_fb[i].size = min_size;
372 }
373
374 fb->data = ext_fb_list->ext_fb[i].data;
375 fb->size = ext_fb_list->ext_fb[i].size;
376 ext_fb_list->ext_fb[i].in_use = 1;
377
378 // Set the frame buffer's private data to point at the external frame buffer.
379 fb->priv = &ext_fb_list->ext_fb[i];
380 return 0;
381}
382
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383// Callback used by libaom when there are no references to the frame buffer.
Frank Galligana4f30a52014-02-06 17:13:08 -0800384// |cb_priv| user private data passed into the set function. |fb| pointer
385// to the frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -0700386static int release_av1_frame_buffer(void *cb_priv,
387 aom_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800388 struct ExternalFrameBuffer *const ext_fb =
389 (struct ExternalFrameBuffer *)fb->priv;
390 (void)cb_priv;
391 ext_fb->in_use = 0;
392 return 0;
393}
394
James Zern6a2e0f02015-05-09 10:42:31 -0700395static void generate_filename(const char *pattern, char *out, size_t q_len,
396 unsigned int d_w, unsigned int d_h,
397 unsigned int frame_in) {
John Koleszarc6b90392012-07-13 15:21:29 -0700398 const char *p = pattern;
399 char *q = out;
John Koleszar933d44b2010-10-21 20:40:42 -0700400
John Koleszarc6b90392012-07-13 15:21:29 -0700401 do {
402 char *next_pat = strchr(p, '%');
John Koleszar933d44b2010-10-21 20:40:42 -0700403
John Koleszarc6b90392012-07-13 15:21:29 -0700404 if (p == next_pat) {
405 size_t pat_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700406
John Koleszar3707c342012-11-06 10:09:36 -0800407 /* parse the pattern */
John Koleszarc6b90392012-07-13 15:21:29 -0700408 q[q_len - 1] = '\0';
409 switch (p[1]) {
clang-format6c4d83e2016-08-08 19:03:30 -0700410 case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
411 case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
412 case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
413 case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
414 case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
415 case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
416 case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
417 case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
418 case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
419 case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
420 case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
421 default: die("Unrecognized pattern %%%c\n", p[1]); break;
John Koleszarc6b90392012-07-13 15:21:29 -0700422 }
John Koleszar933d44b2010-10-21 20:40:42 -0700423
John Koleszarc6b90392012-07-13 15:21:29 -0700424 pat_len = strlen(q);
clang-format6c4d83e2016-08-08 19:03:30 -0700425 if (pat_len >= q_len - 1) die("Output filename too long.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700426 q += pat_len;
427 p += 2;
428 q_len -= pat_len;
429 } else {
430 size_t copy_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700431
John Koleszar3707c342012-11-06 10:09:36 -0800432 /* copy the next segment */
John Koleszarc6b90392012-07-13 15:21:29 -0700433 if (!next_pat)
434 copy_len = strlen(p);
435 else
436 copy_len = next_pat - p;
John Koleszar933d44b2010-10-21 20:40:42 -0700437
clang-format6c4d83e2016-08-08 19:03:30 -0700438 if (copy_len >= q_len - 1) die("Output filename too long.\n");
John Koleszar933d44b2010-10-21 20:40:42 -0700439
John Koleszarc6b90392012-07-13 15:21:29 -0700440 memcpy(q, p, copy_len);
441 q[copy_len] = '\0';
442 q += copy_len;
443 p += copy_len;
444 q_len -= copy_len;
445 }
446 } while (*p);
John Koleszar933d44b2010-10-21 20:40:42 -0700447}
448
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800449static int is_single_file(const char *outfile_pattern) {
450 const char *p = outfile_pattern;
451
452 do {
453 p = strchr(p, '%');
454 if (p && p[1] >= '1' && p[1] <= '9')
455 return 0; // pattern contains sequence number, so it's not unique
clang-format6c4d83e2016-08-08 19:03:30 -0700456 if (p) p++;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800457 } while (p);
458
459 return 1;
460}
461
462static void print_md5(unsigned char digest[16], const char *filename) {
463 int i;
464
clang-format6c4d83e2016-08-08 19:03:30 -0700465 for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800466 printf(" %s\n", filename);
467}
468
469static FILE *open_outfile(const char *name) {
470 if (strcmp("-", name) == 0) {
471 set_binary_mode(stdout);
472 return stdout;
473 } else {
474 FILE *file = fopen(name, "wb");
clang-format6c4d83e2016-08-08 19:03:30 -0700475 if (!file) fatal("Failed to open output file '%s'", name);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800476 return file;
477 }
478}
479
Yaowu Xuf883b422016-08-30 14:01:10 -0700480#if CONFIG_AOM_HIGHBITDEPTH
481static int img_shifted_realloc_required(const aom_image_t *img,
482 const aom_image_t *shifted,
483 aom_img_fmt_t required_fmt) {
clang-format6c4d83e2016-08-08 19:03:30 -0700484 return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700485 required_fmt != shifted->fmt;
486}
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700487#endif
488
James Zern6a2e0f02015-05-09 10:42:31 -0700489static int main_loop(int argc, const char **argv_) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700490 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700491 char *fn = NULL;
492 int i;
James Zern360e4442016-11-16 21:53:55 -0800493 int ret = EXIT_FAILURE;
clang-format6c4d83e2016-08-08 19:03:30 -0700494 uint8_t *buf = NULL;
495 size_t bytes_in_buffer = 0, buffer_size = 0;
496 FILE *infile;
497 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
498 int do_md5 = 0, progress = 0, frame_parallel = 0;
499 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
500 int arg_skip = 0;
501 int ec_enabled = 0;
502 int keep_going = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700503 const AvxInterface *interface = NULL;
504 const AvxInterface *fourcc_interface = NULL;
James Yu6d151322014-02-22 14:51:11 +0800505 uint64_t dx_time = 0;
clang-format6c4d83e2016-08-08 19:03:30 -0700506 struct arg arg;
507 char **argv, **argi, **argj;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800508
clang-format6c4d83e2016-08-08 19:03:30 -0700509 int single_file;
510 int use_y4m = 1;
511 int opt_yv12 = 0;
512 int opt_i420 = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700513 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
514#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700515 unsigned int output_bit_depth = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700516#endif
Geza Lore490ba1a2016-03-11 17:42:49 +0000517#if CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700518 int tile_row = -1;
519 int tile_col = -1;
Geza Lore490ba1a2016-03-11 17:42:49 +0000520#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700521 int frames_corrupted = 0;
522 int dec_flags = 0;
523 int do_scale = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700524 aom_image_t *scaled_img = NULL;
525#if CONFIG_AOM_HIGHBITDEPTH
526 aom_image_t *img_shifted = NULL;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700527#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700528 int frame_avail, got_data, flush_decoder = 0;
529 int num_external_frame_buffers = 0;
530 struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400531
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800532 const char *outfile_pattern = NULL;
clang-format6c4d83e2016-08-08 19:03:30 -0700533 char outfile_name[PATH_MAX] = { 0 };
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800534 FILE *outfile = NULL;
535
536 MD5Context md5_ctx;
537 unsigned char md5_digest[16];
538
Yaowu Xuf883b422016-08-30 14:01:10 -0700539 struct AvxDecInputContext input = { NULL, NULL };
540 struct AvxInputContext aom_input_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700541#if CONFIG_WEBM_IO
Jim Bankoski7ae32692014-08-19 06:44:10 -0700542 struct WebmInputContext webm_ctx;
543 memset(&(webm_ctx), 0, sizeof(webm_ctx));
Tom Finegan2abe2d42013-11-18 14:39:51 -0800544 input.webm_ctx = &webm_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700545#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700546 input.aom_input_ctx = &aom_input_ctx;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800547
John Koleszarc6b90392012-07-13 15:21:29 -0700548 /* Parse command line */
549 exec_name = argv_[0];
550 argv = argv_dup(argc - 1, argv_ + 1);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400551
John Koleszarc6b90392012-07-13 15:21:29 -0700552 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
553 memset(&arg, 0, sizeof(arg));
554 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400555
John Koleszarc6b90392012-07-13 15:21:29 -0700556 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700557 interface = get_aom_decoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800558 if (!interface)
559 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszar9596a4c2013-06-12 16:08:35 -0700560 } else if (arg_match(&arg, &looparg, argi)) {
561 // no-op
John Koleszarc6b90392012-07-13 15:21:29 -0700562 } else if (arg_match(&arg, &outputfile, argi))
563 outfile_pattern = arg.val;
564 else if (arg_match(&arg, &use_yv12, argi)) {
565 use_y4m = 0;
566 flipuv = 1;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700567 opt_yv12 = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700568 } else if (arg_match(&arg, &use_i420, argi)) {
569 use_y4m = 0;
570 flipuv = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700571 opt_i420 = 1;
572 } else if (arg_match(&arg, &rawvideo, argi)) {
573 use_y4m = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700574 } else if (arg_match(&arg, &flipuvarg, argi))
575 flipuv = 1;
576 else if (arg_match(&arg, &noblitarg, argi))
577 noblit = 1;
578 else if (arg_match(&arg, &progressarg, argi))
579 progress = 1;
580 else if (arg_match(&arg, &limitarg, argi))
581 stop_after = arg_parse_uint(&arg);
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700582 else if (arg_match(&arg, &skiparg, argi))
583 arg_skip = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700584 else if (arg_match(&arg, &postprocarg, argi))
585 postproc = 1;
586 else if (arg_match(&arg, &md5arg, argi))
587 do_md5 = 1;
588 else if (arg_match(&arg, &summaryarg, argi))
589 summary = 1;
590 else if (arg_match(&arg, &threadsarg, argi))
591 cfg.threads = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -0700592#if CONFIG_AV1_DECODER
hkuang1f841c32015-03-06 13:03:18 -0800593 else if (arg_match(&arg, &frameparallelarg, argi))
594 frame_parallel = 1;
595#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700596 else if (arg_match(&arg, &verbosearg, argi))
597 quiet = 0;
John Koleszar7d8fc262013-02-25 16:58:47 -0800598 else if (arg_match(&arg, &scalearg, argi))
599 do_scale = 1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800600 else if (arg_match(&arg, &fb_arg, argi))
601 num_external_frame_buffers = arg_parse_uint(&arg);
James Zern7690d882014-08-22 18:01:28 -0700602 else if (arg_match(&arg, &continuearg, argi))
603 keep_going = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700604#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700605 else if (arg_match(&arg, &outbitdeptharg, argi)) {
606 output_bit_depth = arg_parse_uint(&arg);
607 }
608#endif
Geza Lore490ba1a2016-03-11 17:42:49 +0000609#if CONFIG_EXT_TILE
610 else if (arg_match(&arg, &tiler, argi))
611 tile_row = arg_parse_int(&arg);
612 else if (arg_match(&arg, &tilec, argi))
613 tile_col = arg_parse_int(&arg);
614#endif // CONFIG_EXT_TILE
John Koleszarcfe3f912010-10-20 10:49:12 -0400615 else
John Koleszarc6b90392012-07-13 15:21:29 -0700616 argj++;
617 }
618
619 /* Check for unrecognized options */
620 for (argi = argv; *argi; argi++)
621 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
622 die("Error: Unrecognized option %s\n", *argi);
623
624 /* Handle non-option arguments */
625 fn = argv[0];
626
Hanno Böckb5d0a202015-02-24 15:48:21 -0800627 if (!fn) {
628 free(argv);
John Koleszarc6b90392012-07-13 15:21:29 -0700629 usage_exit();
Hanno Böckb5d0a202015-02-24 15:48:21 -0800630 }
John Koleszarc6b90392012-07-13 15:21:29 -0700631 /* Open file */
632 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
633
634 if (!infile) {
James Zern6b993f02015-02-05 21:06:29 -0800635 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
John Koleszarc6b90392012-07-13 15:21:29 -0700636 }
637#if CONFIG_OS_SUPPORT
638 /* Make sure we don't dump to the terminal, unless forced to with -o - */
639 if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) {
640 fprintf(stderr,
641 "Not dumping raw video to your terminal. Use '-o -' to "
642 "override.\n");
643 return EXIT_FAILURE;
644 }
645#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700646 input.aom_input_ctx->file = infile;
647 if (file_is_ivf(input.aom_input_ctx))
648 input.aom_input_ctx->file_type = FILE_TYPE_IVF;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700649#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700650 else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
651 input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700652#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700653 else if (file_is_raw(input.aom_input_ctx))
654 input.aom_input_ctx->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700655 else {
656 fprintf(stderr, "Unrecognized input file type.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700657#if !CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700658 fprintf(stderr, "aomdec was built without WebM container support.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700659#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700660 return EXIT_FAILURE;
661 }
662
John Koleszarc6b90392012-07-13 15:21:29 -0700663 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800664 single_file = is_single_file(outfile_pattern);
John Koleszarc6b90392012-07-13 15:21:29 -0700665
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800666 if (!noblit && single_file) {
667 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
Yaowu Xuf883b422016-08-30 14:01:10 -0700668 aom_input_ctx.width, aom_input_ctx.height, 0);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800669 if (do_md5)
670 MD5Init(&md5_ctx);
671 else
672 outfile = open_outfile(outfile_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700673 }
674
675 if (use_y4m && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700676 if (!single_file) {
clang-format6c4d83e2016-08-08 19:03:30 -0700677 fprintf(stderr,
678 "YUV4MPEG2 not supported with output patterns,"
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700679 " try --i420 or --yv12 or --rawvideo.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700680 return EXIT_FAILURE;
681 }
682
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700683#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700684 if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
685 if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
clang-format6c4d83e2016-08-08 19:03:30 -0700686 fprintf(stderr,
687 "Failed to guess framerate -- error parsing "
John Koleszarc6b90392012-07-13 15:21:29 -0700688 "webm file?\n");
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400689 return EXIT_FAILURE;
John Koleszarc6b90392012-07-13 15:21:29 -0700690 }
Dmitry Kovalev5ab635832014-01-17 17:02:37 -0800691 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700692#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700693 }
694
Yaowu Xuf883b422016-08-30 14:01:10 -0700695 fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800696 if (interface && fourcc_interface && interface != fourcc_interface)
697 warn("Header indicates codec: %s\n", fourcc_interface->name);
698 else
699 interface = fourcc_interface;
John Koleszarc6b90392012-07-13 15:21:29 -0700700
Yaowu Xuf883b422016-08-30 14:01:10 -0700701 if (!interface) interface = get_aom_decoder_by_index(0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400702
Yaowu Xuf883b422016-08-30 14:01:10 -0700703 dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0) |
704 (ec_enabled ? AOM_CODEC_USE_ERROR_CONCEALMENT : 0) |
705 (frame_parallel ? AOM_CODEC_USE_FRAME_THREADING : 0);
706 if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
clang-format6c4d83e2016-08-08 19:03:30 -0700707 dec_flags)) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800708 fprintf(stderr, "Failed to initialize decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700709 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800710 goto fail2;
John Koleszarc6b90392012-07-13 15:21:29 -0700711 }
John Koleszar933d44b2010-10-21 20:40:42 -0700712
clang-format6c4d83e2016-08-08 19:03:30 -0700713 if (!quiet) fprintf(stderr, "%s\n", decoder.name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400714
Yaowu Xuf883b422016-08-30 14:01:10 -0700715#if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
716 if (strncmp(decoder.name, "WebM Project AV1", 17) == 0) {
717 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_ROW, tile_row)) {
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700718 fprintf(stderr, "Failed to set decode_tile_row: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700719 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800720 goto fail;
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700721 }
722
Yaowu Xuf883b422016-08-30 14:01:10 -0700723 if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_COL, tile_col)) {
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700724 fprintf(stderr, "Failed to set decode_tile_col: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700725 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800726 goto fail;
Yunqing Wang8e5e3382016-05-05 16:42:57 -0700727 }
728 }
729#endif
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700730
clang-format6c4d83e2016-08-08 19:03:30 -0700731 if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700732 while (arg_skip) {
clang-format6c4d83e2016-08-08 19:03:30 -0700733 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700734 arg_skip--;
735 }
736
Frank Galligana4f30a52014-02-06 17:13:08 -0800737 if (num_external_frame_buffers > 0) {
738 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
739 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
740 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
Yaowu Xuf883b422016-08-30 14:01:10 -0700741 if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
742 release_av1_frame_buffer,
clang-format6c4d83e2016-08-08 19:03:30 -0700743 &ext_fb_list)) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800744 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700745 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800746 goto fail;
Frank Galligana4f30a52014-02-06 17:13:08 -0800747 }
748 }
749
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400750 frame_avail = 1;
751 got_data = 0;
752
John Koleszarc6b90392012-07-13 15:21:29 -0700753 /* Decode file */
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400754 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 aom_codec_iter_t iter = NULL;
756 aom_image_t *img;
757 struct aom_usec_timer timer;
clang-format6c4d83e2016-08-08 19:03:30 -0700758 int corrupted = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400759
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400760 frame_avail = 0;
761 if (!stop_after || frame_in < stop_after) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800762 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400763 frame_avail = 1;
764 frame_in++;
765
Yaowu Xuf883b422016-08-30 14:01:10 -0700766 aom_usec_timer_start(&timer);
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400767
Yaowu Xuf883b422016-08-30 14:01:10 -0700768 if (aom_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer, NULL,
clang-format6c4d83e2016-08-08 19:03:30 -0700769 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 const char *detail = aom_codec_error_detail(&decoder);
clang-format6c4d83e2016-08-08 19:03:30 -0700771 warn("Failed to decode frame %d: %s", frame_in,
Yaowu Xuf883b422016-08-30 14:01:10 -0700772 aom_codec_error(&decoder));
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400773
clang-format6c4d83e2016-08-08 19:03:30 -0700774 if (detail) warn("Additional information: %s", detail);
775 if (!keep_going) goto fail;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400776 }
777
Yaowu Xuf883b422016-08-30 14:01:10 -0700778 aom_usec_timer_mark(&timer);
779 dx_time += aom_usec_timer_elapsed(&timer);
hkuang1f841c32015-03-06 13:03:18 -0800780 } else {
781 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400782 }
hkuang1f841c32015-03-06 13:03:18 -0800783 } else {
784 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400785 }
786
Yaowu Xuf883b422016-08-30 14:01:10 -0700787 aom_usec_timer_start(&timer);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400788
hkuang1f841c32015-03-06 13:03:18 -0800789 if (flush_decoder) {
790 // Flush the decoder in frame parallel decode.
Yaowu Xuf883b422016-08-30 14:01:10 -0700791 if (aom_codec_decode(&decoder, NULL, 0, NULL, 0)) {
792 warn("Failed to flush decoder: %s", aom_codec_error(&decoder));
hkuang1f841c32015-03-06 13:03:18 -0800793 }
794 }
795
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400796 got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700797 if ((img = aom_codec_get_frame(&decoder, &iter))) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400798 ++frame_out;
799 got_data = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400800 }
801
Yaowu Xuf883b422016-08-30 14:01:10 -0700802 aom_usec_timer_mark(&timer);
803 dx_time += (unsigned int)aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -0700804
hkuang1f841c32015-03-06 13:03:18 -0800805 if (!frame_parallel &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700806 aom_codec_control(&decoder, AOMD_GET_FRAME_CORRUPTED, &corrupted)) {
807 warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder));
clang-format6c4d83e2016-08-08 19:03:30 -0700808 if (!keep_going) goto fail;
John Koleszarc6b90392012-07-13 15:21:29 -0700809 }
810 frames_corrupted += corrupted;
811
clang-format6c4d83e2016-08-08 19:03:30 -0700812 if (progress) show_progress(frame_in, frame_out, dx_time);
John Koleszarc6b90392012-07-13 15:21:29 -0700813
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800814 if (!noblit && img) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
816 const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800817 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
John Koleszarda584362013-05-06 15:52:06 -0700818
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800819 if (do_scale) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800820 if (frame_out == 1) {
821 // If the output frames are to be scaled to a fixed display size then
822 // use the width and height specified in the container. If either of
823 // these is set to 0, use the display size set in the first frame
Adrian Grange82f6af62014-01-22 09:36:07 -0800824 // header. If that is unavailable, use the raw decoded size of the
825 // first decoded frame.
Yaowu Xuf883b422016-08-30 14:01:10 -0700826 int render_width = aom_input_ctx.width;
827 int render_height = aom_input_ctx.height;
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400828 if (!render_width || !render_height) {
829 int render_size[2];
Yaowu Xuf883b422016-08-30 14:01:10 -0700830 if (aom_codec_control(&decoder, AV1D_GET_DISPLAY_SIZE,
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400831 render_size)) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800832 // As last resort use size of first frame as display size.
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400833 render_width = img->d_w;
834 render_height = img->d_h;
Adrian Grange482c39c2013-12-13 10:15:03 -0800835 } else {
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400836 render_width = render_size[0];
837 render_height = render_size[1];
Adrian Grange482c39c2013-12-13 10:15:03 -0800838 }
Adrian Grange2117fe02013-11-19 14:01:44 -0800839 }
clang-format6c4d83e2016-08-08 19:03:30 -0700840 scaled_img =
Yaowu Xuf883b422016-08-30 14:01:10 -0700841 aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
Deb Mukherjee449e5f22014-07-11 11:43:31 -0700842 scaled_img->bit_depth = img->bit_depth;
John Koleszar7d8fc262013-02-25 16:58:47 -0800843 }
Adrian Grange482c39c2013-12-13 10:15:03 -0800844
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800845 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
Deb Mukherjee47031c02014-05-16 18:52:01 -0700846#if CONFIG_LIBYUV
Tom Finegan8ef2ce82014-12-12 11:57:04 -0800847 libyuv_scale(img, scaled_img, kFilterBox);
John Koleszar7d8fc262013-02-25 16:58:47 -0800848 img = scaled_img;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700849#else
clang-format6c4d83e2016-08-08 19:03:30 -0700850 fprintf(stderr,
851 "Failed to scale output frame: %s.\n"
Deb Mukherjee47031c02014-05-16 18:52:01 -0700852 "Scaling is disabled in this configuration. "
853 "To enable scaling, configure with --enable-libyuv\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700854 aom_codec_error(&decoder));
James Zern360e4442016-11-16 21:53:55 -0800855 goto fail;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700856#endif
John Koleszar7d8fc262013-02-25 16:58:47 -0800857 }
858 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700859#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700860 // Default to codec bit depth if output bit depth not set
Ronald S. Bultjeeba342a2015-09-11 16:55:19 -0400861 if (!output_bit_depth && single_file && !do_md5) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700862 output_bit_depth = img->bit_depth;
863 }
864 // Shift up or down if necessary
Ronald S. Bultjeeba342a2015-09-11 16:55:19 -0400865 if (output_bit_depth != 0 && output_bit_depth != img->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700866 const aom_img_fmt_t shifted_fmt =
clang-format6c4d83e2016-08-08 19:03:30 -0700867 output_bit_depth == 8
Yaowu Xuf883b422016-08-30 14:01:10 -0700868 ? img->fmt ^ (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
869 : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700870 if (img_shifted &&
871 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700872 aom_img_free(img_shifted);
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700873 img_shifted = NULL;
874 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700875 if (!img_shifted) {
clang-format6c4d83e2016-08-08 19:03:30 -0700876 img_shifted =
Yaowu Xuf883b422016-08-30 14:01:10 -0700877 aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700878 img_shifted->bit_depth = output_bit_depth;
879 }
880 if (output_bit_depth > img->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700881 aom_img_upshift(img_shifted, img, output_bit_depth - img->bit_depth);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700882 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700883 aom_img_downshift(img_shifted, img,
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700884 img->bit_depth - output_bit_depth);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700885 }
886 img = img_shifted;
887 }
888#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800889
Geza Lore490ba1a2016-03-11 17:42:49 +0000890#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700891 aom_input_ctx.width = img->d_w;
892 aom_input_ctx.height = img->d_h;
Geza Lore490ba1a2016-03-11 17:42:49 +0000893#endif // CONFIG_EXT_TILE
894
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800895 if (single_file) {
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800896 if (use_y4m) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700897 char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800898 size_t len = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700899 if (img->fmt == AOM_IMG_FMT_I440 || img->fmt == AOM_IMG_FMT_I44016) {
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700900 fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
901 goto fail;
902 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800903 if (frame_out == 1) {
904 // Y4M file header
clang-format6c4d83e2016-08-08 19:03:30 -0700905 len = y4m_write_file_header(
Urvang Joshi4145bf02016-10-17 14:53:33 -0700906 y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
907 aom_input_ctx.height, &aom_input_ctx.framerate, img->fmt,
908 img->bit_depth);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800909 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700910 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800911 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700912 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800913 }
914 }
915
916 // Y4M frame header
Urvang Joshi4145bf02016-10-17 14:53:33 -0700917 len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800918 if (do_md5) {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700919 MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800920 } else {
Urvang Joshi4145bf02016-10-17 14:53:33 -0700921 fputs(y4m_buf, outfile);
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800922 }
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700923 } else {
924 if (frame_out == 1) {
925 // Check if --yv12 or --i420 options are consistent with the
926 // bit-stream decoded
927 if (opt_i420) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700928 if (img->fmt != AOM_IMG_FMT_I420 &&
929 img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700930 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
931 goto fail;
932 }
933 }
934 if (opt_yv12) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700935 if ((img->fmt != AOM_IMG_FMT_I420 &&
936 img->fmt != AOM_IMG_FMT_YV12) ||
clang-format6c4d83e2016-08-08 19:03:30 -0700937 img->bit_depth != 8) {
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700938 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
939 goto fail;
940 }
941 }
942 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -0800943 }
944
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800945 if (do_md5) {
946 update_image_md5(img, planes, &md5_ctx);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800947 } else {
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800948 write_image_file(img, planes, outfile);
949 }
950 } else {
clang-format6c4d83e2016-08-08 19:03:30 -0700951 generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
952 img->d_h, frame_in);
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800953 if (do_md5) {
954 MD5Init(&md5_ctx);
955 update_image_md5(img, planes, &md5_ctx);
956 MD5Final(md5_digest, &md5_ctx);
957 print_md5(md5_digest, outfile_name);
958 } else {
959 outfile = open_outfile(outfile_name);
960 write_image_file(img, planes, outfile);
961 fclose(outfile);
John Koleszarc6b90392012-07-13 15:21:29 -0700962 }
John Koleszarc6b90392012-07-13 15:21:29 -0700963 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400964 }
John Koleszarc6b90392012-07-13 15:21:29 -0700965 }
966
967 if (summary || progress) {
968 show_progress(frame_in, frame_out, dx_time);
969 fprintf(stderr, "\n");
970 }
971
James Zern360e4442016-11-16 21:53:55 -0800972 if (frames_corrupted) {
John Koleszarc6b90392012-07-13 15:21:29 -0700973 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
James Zern360e4442016-11-16 21:53:55 -0800974 } else {
975 ret = EXIT_SUCCESS;
976 }
John Koleszar4226f0c2011-04-27 12:04:48 -0400977
John Koleszar0ea50ce2010-05-18 11:58:33 -0400978fail:
979
Yaowu Xuf883b422016-08-30 14:01:10 -0700980 if (aom_codec_destroy(&decoder)) {
Adrian Granged427fab2013-11-21 17:19:04 -0800981 fprintf(stderr, "Failed to destroy decoder: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -0700982 aom_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -0700983 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400984
James Zern360e4442016-11-16 21:53:55 -0800985fail2:
986
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800987 if (!noblit && single_file) {
988 if (do_md5) {
989 MD5Final(md5_digest, &md5_ctx);
990 print_md5(md5_digest, outfile_name);
991 } else {
992 fclose(outfile);
993 }
994 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400995
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700996#if CONFIG_WEBM_IO
Yaowu Xuf883b422016-08-30 14:01:10 -0700997 if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
Tom Finegan2abe2d42013-11-18 14:39:51 -0800998 webm_free(input.webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700999#endif
1000
Yaowu Xuf883b422016-08-30 14:01:10 -07001001 if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
Tom Finegan2abe2d42013-11-18 14:39:51 -08001002
Yaowu Xuf883b422016-08-30 14:01:10 -07001003 if (scaled_img) aom_img_free(scaled_img);
1004#if CONFIG_AOM_HIGHBITDEPTH
1005 if (img_shifted) aom_img_free(img_shifted);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001006#endif
Adrian Granged427fab2013-11-21 17:19:04 -08001007
Frank Galligana4f30a52014-02-06 17:13:08 -08001008 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1009 free(ext_fb_list.ext_fb[i].data);
1010 }
1011 free(ext_fb_list.ext_fb);
1012
John Koleszarc6b90392012-07-13 15:21:29 -07001013 fclose(infile);
1014 free(argv);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001015
James Zern360e4442016-11-16 21:53:55 -08001016 return ret;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001017}
John Koleszar9596a4c2013-06-12 16:08:35 -07001018
1019int main(int argc, const char **argv_) {
1020 unsigned int loops = 1, i;
1021 char **argv, **argi, **argj;
1022 struct arg arg;
1023 int error = 0;
1024
1025 argv = argv_dup(argc - 1, argv_ + 1);
1026 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1027 memset(&arg, 0, sizeof(arg));
1028 arg.argv_step = 1;
1029
1030 if (arg_match(&arg, &looparg, argi)) {
1031 loops = arg_parse_uint(&arg);
1032 break;
1033 }
1034 }
1035 free(argv);
clang-format6c4d83e2016-08-08 19:03:30 -07001036 for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
John Koleszar9596a4c2013-06-12 16:08:35 -07001037 return error;
1038}