blob: 1bef4bd4aae0a4ce0d6e909200358d4ceb4d60b5 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
John Koleszarda584362013-05-06 15:52:06 -070011#include <assert.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040012#include <stdio.h>
13#include <stdlib.h>
14#include <stdarg.h>
15#include <string.h>
John Koleszar933d44b2010-10-21 20:40:42 -070016#include <limits.h>
Tero Rintaluoma11a222f2011-01-24 11:21:40 +020017
James Zern1e681d92014-08-15 21:00:09 -070018#include "./vpx_config.h"
19
20#if CONFIG_LIBYUV
Tom Finegan2abe2d42013-11-18 14:39:51 -080021#include "third_party/libyuv/include/libyuv/scale.h"
James Zern1e681d92014-08-15 21:00:09 -070022#endif
Tom Finegan2abe2d42013-11-18 14:39:51 -080023
24#include "./args.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080025#include "./ivfdec.h"
26
John Koleszarb7492342010-05-24 11:39:59 -040027#include "vpx/vpx_decoder.h"
Dmitry Kovalev4334c072014-02-26 16:32:09 -080028#include "vpx_ports/mem_ops.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040029#include "vpx_ports/vpx_timer.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080030
Jingning Han3ee6db62015-08-05 19:00:31 -070031#if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER || CONFIG_VP10_DECODER
John Koleszarb7492342010-05-24 11:39:59 -040032#include "vpx/vp8dx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040033#endif
Tom Finegan00a35aa2013-11-14 12:37:42 -080034
Tom Finegan2abe2d42013-11-18 14:39:51 -080035#include "./md5_utils.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080036
37#include "./tools_common.h"
James Zernb6cce762014-05-10 17:44:12 -070038#if CONFIG_WEBM_IO
Tom Finegan2abe2d42013-11-18 14:39:51 -080039#include "./webmdec.h"
James Zernb6cce762014-05-10 17:44:12 -070040#endif
Dmitry Kovalev5ab63582014-01-17 17:02:37 -080041#include "./y4menc.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040042
43static const char *exec_name;
44
Tom Finegan2abe2d42013-11-18 14:39:51 -080045struct VpxDecInputContext {
46 struct VpxInputContext *vpx_input_ctx;
47 struct WebmInputContext *webm_ctx;
48};
49
Deb Mukherjeea349ee32014-10-13 14:27:53 -070050static const arg_def_t looparg = ARG_DEF(
51 NULL, "loops", 1, "Number of times to decode the file");
52static const arg_def_t codecarg = ARG_DEF(
53 NULL, "codec", 1, "Codec to use");
54static const arg_def_t use_yv12 = ARG_DEF(
55 NULL, "yv12", 0, "Output raw YV12 frames");
56static const arg_def_t use_i420 = ARG_DEF(
57 NULL, "i420", 0, "Output raw I420 frames");
58static const arg_def_t flipuvarg = ARG_DEF(
59 NULL, "flipuv", 0, "Flip the chroma planes in the output");
60static const arg_def_t rawvideo = ARG_DEF(
61 NULL, "rawvideo", 0, "Output raw YUV frames");
62static const arg_def_t noblitarg = ARG_DEF(
63 NULL, "noblit", 0, "Don't process the decoded frames");
64static const arg_def_t progressarg = ARG_DEF(
65 NULL, "progress", 0, "Show progress after each frame decodes");
66static const arg_def_t limitarg = ARG_DEF(
67 NULL, "limit", 1, "Stop decoding after n frames");
68static const arg_def_t skiparg = ARG_DEF(
69 NULL, "skip", 1, "Skip the first n input frames");
70static const arg_def_t postprocarg = ARG_DEF(
71 NULL, "postproc", 0, "Postprocess decoded frames");
72static const arg_def_t summaryarg = ARG_DEF(
73 NULL, "summary", 0, "Show timing summary");
74static const arg_def_t outputfile = ARG_DEF(
75 "o", "output", 1, "Output file name pattern (see below)");
76static const arg_def_t threadsarg = ARG_DEF(
77 "t", "threads", 1, "Max threads to use");
hkuang1f841c32015-03-06 13:03:18 -080078static const arg_def_t frameparallelarg = ARG_DEF(
79 NULL, "frame-parallel", 0, "Frame parallel decode");
Deb Mukherjeea349ee32014-10-13 14:27:53 -070080static const arg_def_t verbosearg = ARG_DEF(
81 "v", "verbose", 0, "Show version string");
82static const arg_def_t error_concealment = ARG_DEF(
83 NULL, "error-concealment", 0, "Enable decoder error-concealment");
84static const arg_def_t scalearg = ARG_DEF(
85 "S", "scale", 0, "Scale output frames uniformly");
86static const arg_def_t continuearg = ARG_DEF(
87 "k", "keep-going", 0, "(debug) Continue decoding after error");
88static const arg_def_t fb_arg = ARG_DEF(
89 NULL, "frame-buffers", 1, "Number of frame buffers to use");
90static const arg_def_t md5arg = ARG_DEF(
91 NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
Yaowu Xu72889a22015-08-14 12:16:07 -070092#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070093static const arg_def_t outbitdeptharg = ARG_DEF(
Deb Mukherjeea349ee32014-10-13 14:27:53 -070094 NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070095#endif
Dmitry Kovalevd24f4e42013-12-27 16:10:18 -080096
John Koleszarc6b90392012-07-13 15:21:29 -070097static const arg_def_t *all_args[] = {
Deb Mukherjee090f4d42014-07-16 09:37:13 -070098 &codecarg, &use_yv12, &use_i420, &flipuvarg, &rawvideo, &noblitarg,
Yaowu Xuc394ffc2012-10-11 15:12:35 -070099 &progressarg, &limitarg, &skiparg, &postprocarg, &summaryarg, &outputfile,
hkuang1f841c32015-03-06 13:03:18 -0800100 &threadsarg, &frameparallelarg, &verbosearg, &scalearg, &fb_arg,
James Zern95853db2014-07-01 19:02:15 -0700101 &md5arg, &error_concealment, &continuearg,
Yaowu Xu72889a22015-08-14 12:16:07 -0700102#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700103 &outbitdeptharg,
104#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700105 NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -0400106};
107
John Koleszara9c75972012-11-08 17:09:30 -0800108#if CONFIG_VP8_DECODER
James Zern3a070ba2015-06-05 16:18:49 -0700109static const arg_def_t addnoise_level = ARG_DEF(
110 NULL, "noise-level", 1, "Enable VP8 postproc add noise");
111static const arg_def_t deblock = ARG_DEF(
112 NULL, "deblock", 0, "Enable VP8 deblocking");
113static const arg_def_t demacroblock_level = ARG_DEF(
114 NULL, "demacroblock-level", 1, "Enable VP8 demacroblocking, w/ level");
115static const arg_def_t pp_debug_info = ARG_DEF(
116 NULL, "pp-debug-info", 1, "Enable VP8 visible debug info");
117static const arg_def_t pp_disp_ref_frame = ARG_DEF(
118 NULL, "pp-dbg-ref-frame", 1,
119 "Display only selected reference frame per macro block");
120static const arg_def_t pp_disp_mb_modes = ARG_DEF(
121 NULL, "pp-dbg-mb-modes", 1, "Display only selected macro block modes");
122static const arg_def_t pp_disp_b_modes = ARG_DEF(
123 NULL, "pp-dbg-b-modes", 1, "Display only selected block modes");
124static const arg_def_t pp_disp_mvs = ARG_DEF(
125 NULL, "pp-dbg-mvs", 1, "Draw only selected motion vectors");
126static const arg_def_t mfqe = ARG_DEF(
127 NULL, "mfqe", 0, "Enable multiframe quality enhancement");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400128
John Koleszarc6b90392012-07-13 15:21:29 -0700129static const arg_def_t *vp8_pp_args[] = {
130 &addnoise_level, &deblock, &demacroblock_level, &pp_debug_info,
John Koleszar3707c342012-11-06 10:09:36 -0800131 &pp_disp_ref_frame, &pp_disp_mb_modes, &pp_disp_b_modes, &pp_disp_mvs, &mfqe,
John Koleszarc6b90392012-07-13 15:21:29 -0700132 NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -0400133};
134#endif
135
James Zern1e681d92014-08-15 21:00:09 -0700136#if CONFIG_LIBYUV
Tom Finegan8ef2ce82014-12-12 11:57:04 -0800137static INLINE int libyuv_scale(vpx_image_t *src, vpx_image_t *dst,
Jim Bankoskif4529612014-08-12 16:51:07 -0700138 FilterModeEnum mode) {
Yaowu Xu72889a22015-08-14 12:16:07 -0700139#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700140 if (src->fmt == VPX_IMG_FMT_I42016) {
141 assert(dst->fmt == VPX_IMG_FMT_I42016);
142 return I420Scale_16((uint16_t*)src->planes[VPX_PLANE_Y],
143 src->stride[VPX_PLANE_Y]/2,
144 (uint16_t*)src->planes[VPX_PLANE_U],
145 src->stride[VPX_PLANE_U]/2,
146 (uint16_t*)src->planes[VPX_PLANE_V],
147 src->stride[VPX_PLANE_V]/2,
148 src->d_w, src->d_h,
149 (uint16_t*)dst->planes[VPX_PLANE_Y],
150 dst->stride[VPX_PLANE_Y]/2,
151 (uint16_t*)dst->planes[VPX_PLANE_U],
152 dst->stride[VPX_PLANE_U]/2,
153 (uint16_t*)dst->planes[VPX_PLANE_V],
154 dst->stride[VPX_PLANE_V]/2,
155 dst->d_w, dst->d_h,
156 mode);
157 }
158#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800159 assert(src->fmt == VPX_IMG_FMT_I420);
160 assert(dst->fmt == VPX_IMG_FMT_I420);
161 return I420Scale(src->planes[VPX_PLANE_Y], src->stride[VPX_PLANE_Y],
162 src->planes[VPX_PLANE_U], src->stride[VPX_PLANE_U],
163 src->planes[VPX_PLANE_V], src->stride[VPX_PLANE_V],
164 src->d_w, src->d_h,
165 dst->planes[VPX_PLANE_Y], dst->stride[VPX_PLANE_Y],
166 dst->planes[VPX_PLANE_U], dst->stride[VPX_PLANE_U],
167 dst->planes[VPX_PLANE_V], dst->stride[VPX_PLANE_V],
168 dst->d_w, dst->d_h,
169 mode);
170}
James Zern1e681d92014-08-15 21:00:09 -0700171#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800172
James Zern59e7a472015-05-09 10:33:26 -0700173void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700174 int i;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400175
John Koleszarc6b90392012-07-13 15:21:29 -0700176 fprintf(stderr, "Usage: %s <options> filename\n\n"
177 "Options:\n", exec_name);
178 arg_show_usage(stderr, all_args);
John Koleszara9c75972012-11-08 17:09:30 -0800179#if CONFIG_VP8_DECODER
John Koleszarc6b90392012-07-13 15:21:29 -0700180 fprintf(stderr, "\nVP8 Postprocessing Options:\n");
181 arg_show_usage(stderr, vp8_pp_args);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400182#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700183 fprintf(stderr,
184 "\nOutput File Patterns:\n\n"
185 " The -o argument specifies the name of the file(s) to "
186 "write to. If the\n argument does not include any escape "
187 "characters, the output will be\n written to a single file. "
188 "Otherwise, the filename will be calculated by\n expanding "
John Koleszar3707c342012-11-06 10:09:36 -0800189 "the following escape characters:\n");
190 fprintf(stderr,
John Koleszarc6b90392012-07-13 15:21:29 -0700191 "\n\t%%w - Frame width"
192 "\n\t%%h - Frame height"
193 "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
194 "\n\n Pattern arguments are only supported in conjunction "
195 "with the --yv12 and\n --i420 options. If the -o option is "
196 "not specified, the output will be\n directed to stdout.\n"
197 );
198 fprintf(stderr, "\nIncluded decoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400199
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800200 for (i = 0; i < get_vpx_decoder_count(); ++i) {
201 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
John Koleszarc6b90392012-07-13 15:21:29 -0700202 fprintf(stderr, " %-6s - %s\n",
Deb Mukherjeef332c632014-07-15 16:55:12 -0700203 decoder->name, vpx_codec_iface_name(decoder->codec_interface()));
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800204 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400205
John Koleszarc6b90392012-07-13 15:21:29 -0700206 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400207}
208
Dmitry Kovalev0eac7532014-01-13 11:57:55 -0800209static int raw_read_frame(FILE *infile, uint8_t **buffer,
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800210 size_t *bytes_read, size_t *buffer_size) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800211 char raw_hdr[RAW_FRAME_HDR_SZ];
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800212 size_t frame_size = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700213
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800214 if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
215 if (!feof(infile))
216 warn("Failed to read RAW frame size\n");
217 } else {
James Zern75d7ee32014-02-04 20:06:28 -0800218 const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
219 const size_t kFrameTooSmallThreshold = 256 * 1024;
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800220 frame_size = mem_get_le32(raw_hdr);
John Koleszarc6b90392012-07-13 15:21:29 -0700221
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800222 if (frame_size > kCorruptFrameThreshold) {
223 warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
224 frame_size = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400225 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400226
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800227 if (frame_size < kFrameTooSmallThreshold) {
228 warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
229 (unsigned int)frame_size);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400230 }
231
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800232 if (frame_size > *buffer_size) {
233 uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
234 if (new_buf) {
235 *buffer = new_buf;
236 *buffer_size = 2 * frame_size;
237 } else {
238 warn("Failed to allocate compressed data buffer\n");
239 frame_size = 0;
240 }
241 }
John Koleszarc6b90392012-07-13 15:21:29 -0700242 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400243
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800244 if (!feof(infile)) {
245 if (fread(*buffer, 1, frame_size, infile) != frame_size) {
246 warn("Failed to read full frame\n");
247 return 1;
248 }
249 *bytes_read = frame_size;
250 }
251
252 return 0;
253}
254
255static int read_frame(struct VpxDecInputContext *input, uint8_t **buf,
256 size_t *bytes_in_buffer, size_t *buffer_size) {
257 switch (input->vpx_input_ctx->file_type) {
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700258#if CONFIG_WEBM_IO
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800259 case FILE_TYPE_WEBM:
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700260 return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700261#endif
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800262 case FILE_TYPE_RAW:
Dmitry Kovalev0eac7532014-01-13 11:57:55 -0800263 return raw_read_frame(input->vpx_input_ctx->file,
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800264 buf, bytes_in_buffer, buffer_size);
265 case FILE_TYPE_IVF:
Dmitry Kovalev0eac7532014-01-13 11:57:55 -0800266 return ivf_read_frame(input->vpx_input_ctx->file,
Dmitry Kovalevf0fde242014-01-13 10:54:57 -0800267 buf, bytes_in_buffer, buffer_size);
268 default:
269 return 1;
270 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400271}
272
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800273static void update_image_md5(const vpx_image_t *img, const int planes[3],
274 MD5Context *md5) {
275 int i, y;
276
277 for (i = 0; i < 3; ++i) {
278 const int plane = planes[i];
279 const unsigned char *buf = img->planes[plane];
280 const int stride = img->stride[plane];
Deb Mukherjeebec307a2014-10-20 23:51:58 -0700281 const int w = vpx_img_plane_width(img, plane) *
282 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
Dmitry Kovalev2bdd43d2014-02-12 18:36:36 -0800283 const int h = vpx_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800284
285 for (y = 0; y < h; ++y) {
286 MD5Update(md5, buf, w);
287 buf += stride;
288 }
289 }
290}
291
292static void write_image_file(const vpx_image_t *img, const int planes[3],
293 FILE *file) {
294 int i, y;
Yaowu Xu72889a22015-08-14 12:16:07 -0700295#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700296 const int bytes_per_sample = ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
297#else
298 const int bytes_per_sample = 1;
299#endif
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800300
301 for (i = 0; i < 3; ++i) {
302 const int plane = planes[i];
303 const unsigned char *buf = img->planes[plane];
304 const int stride = img->stride[plane];
Dmitry Kovalev2bdd43d2014-02-12 18:36:36 -0800305 const int w = vpx_img_plane_width(img, plane);
306 const int h = vpx_img_plane_height(img, plane);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800307
308 for (y = 0; y < h; ++y) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700309 fwrite(buf, bytes_per_sample, w, file);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -0800310 buf += stride;
311 }
John Koleszarc6b90392012-07-13 15:21:29 -0700312 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400313}
314
James Zern6a2e0f02015-05-09 10:42:31 -0700315static int file_is_raw(struct VpxInputContext *input) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800316 uint8_t buf[32];
John Koleszarc6b90392012-07-13 15:21:29 -0700317 int is_raw = 0;
318 vpx_codec_stream_info_t si;
John Koleszarcfe3f912010-10-20 10:49:12 -0400319
John Koleszarc6b90392012-07-13 15:21:29 -0700320 si.sz = sizeof(si);
John Koleszar19255b82010-11-23 13:40:31 -0500321
Tom Finegan00a35aa2013-11-14 12:37:42 -0800322 if (fread(buf, 1, 32, input->file) == 32) {
John Koleszarc6b90392012-07-13 15:21:29 -0700323 int i;
John Koleszarcfe3f912010-10-20 10:49:12 -0400324
Tom Finegan00a35aa2013-11-14 12:37:42 -0800325 if (mem_get_le32(buf) < 256 * 1024 * 1024) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800326 for (i = 0; i < get_vpx_decoder_count(); ++i) {
327 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
Deb Mukherjeef332c632014-07-15 16:55:12 -0700328 if (!vpx_codec_peek_stream_info(decoder->codec_interface(),
John Koleszarc6b90392012-07-13 15:21:29 -0700329 buf + 4, 32 - 4, &si)) {
330 is_raw = 1;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800331 input->fourcc = decoder->fourcc;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800332 input->width = si.w;
333 input->height = si.h;
334 input->framerate.numerator = 30;
335 input->framerate.denominator = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700336 break;
337 }
Tom Finegan00a35aa2013-11-14 12:37:42 -0800338 }
339 }
John Koleszarc6b90392012-07-13 15:21:29 -0700340 }
John Koleszarcfe3f912010-10-20 10:49:12 -0400341
Tom Finegan00a35aa2013-11-14 12:37:42 -0800342 rewind(input->file);
John Koleszarc6b90392012-07-13 15:21:29 -0700343 return is_raw;
John Koleszarcfe3f912010-10-20 10:49:12 -0400344}
345
James Zern6a2e0f02015-05-09 10:42:31 -0700346static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
James Yu6d151322014-02-22 14:51:11 +0800347 fprintf(stderr,
348 "%d decoded frames/%d showed frames in %"PRId64" us (%.2f fps)\r",
John Koleszarc6b90392012-07-13 15:21:29 -0700349 frame_in, frame_out, dx_time,
James Yu6d151322014-02-22 14:51:11 +0800350 (double)frame_out * 1000000.0 / (double)dx_time);
John Koleszar5d12e042010-10-21 17:28:34 -0400351}
352
Frank Galligana4f30a52014-02-06 17:13:08 -0800353struct ExternalFrameBuffer {
354 uint8_t* data;
355 size_t size;
356 int in_use;
357};
358
359struct ExternalFrameBufferList {
360 int num_external_frame_buffers;
361 struct ExternalFrameBuffer *ext_fb;
362};
363
364// Callback used by libvpx to request an external frame buffer. |cb_priv|
365// Application private data passed into the set function. |min_size| is the
366// minimum size in bytes needed to decode the next frame. |fb| pointer to the
367// frame buffer.
James Zern6a2e0f02015-05-09 10:42:31 -0700368static int get_vp9_frame_buffer(void *cb_priv, size_t min_size,
369 vpx_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800370 int i;
371 struct ExternalFrameBufferList *const ext_fb_list =
372 (struct ExternalFrameBufferList *)cb_priv;
373 if (ext_fb_list == NULL)
374 return -1;
375
376 // Find a free frame buffer.
377 for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
378 if (!ext_fb_list->ext_fb[i].in_use)
379 break;
380 }
381
382 if (i == ext_fb_list->num_external_frame_buffers)
383 return -1;
384
385 if (ext_fb_list->ext_fb[i].size < min_size) {
386 free(ext_fb_list->ext_fb[i].data);
Frank Galligan175d9df2014-09-11 08:39:38 -0700387 ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
Frank Galligana4f30a52014-02-06 17:13:08 -0800388 if (!ext_fb_list->ext_fb[i].data)
389 return -1;
390
391 ext_fb_list->ext_fb[i].size = min_size;
392 }
393
394 fb->data = ext_fb_list->ext_fb[i].data;
395 fb->size = ext_fb_list->ext_fb[i].size;
396 ext_fb_list->ext_fb[i].in_use = 1;
397
398 // Set the frame buffer's private data to point at the external frame buffer.
399 fb->priv = &ext_fb_list->ext_fb[i];
400 return 0;
401}
402
403// Callback used by libvpx when there are no references to the frame buffer.
404// |cb_priv| user private data passed into the set function. |fb| pointer
405// to the frame buffer.
James Zern6a2e0f02015-05-09 10:42:31 -0700406static int release_vp9_frame_buffer(void *cb_priv,
407 vpx_codec_frame_buffer_t *fb) {
Frank Galligana4f30a52014-02-06 17:13:08 -0800408 struct ExternalFrameBuffer *const ext_fb =
409 (struct ExternalFrameBuffer *)fb->priv;
410 (void)cb_priv;
411 ext_fb->in_use = 0;
412 return 0;
413}
414
James Zern6a2e0f02015-05-09 10:42:31 -0700415static void generate_filename(const char *pattern, char *out, size_t q_len,
416 unsigned int d_w, unsigned int d_h,
417 unsigned int frame_in) {
John Koleszarc6b90392012-07-13 15:21:29 -0700418 const char *p = pattern;
419 char *q = out;
John Koleszar933d44b2010-10-21 20:40:42 -0700420
John Koleszarc6b90392012-07-13 15:21:29 -0700421 do {
422 char *next_pat = strchr(p, '%');
John Koleszar933d44b2010-10-21 20:40:42 -0700423
John Koleszarc6b90392012-07-13 15:21:29 -0700424 if (p == next_pat) {
425 size_t pat_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700426
John Koleszar3707c342012-11-06 10:09:36 -0800427 /* parse the pattern */
John Koleszarc6b90392012-07-13 15:21:29 -0700428 q[q_len - 1] = '\0';
429 switch (p[1]) {
430 case 'w':
431 snprintf(q, q_len - 1, "%d", d_w);
432 break;
433 case 'h':
434 snprintf(q, q_len - 1, "%d", d_h);
435 break;
436 case '1':
437 snprintf(q, q_len - 1, "%d", frame_in);
438 break;
439 case '2':
440 snprintf(q, q_len - 1, "%02d", frame_in);
441 break;
442 case '3':
443 snprintf(q, q_len - 1, "%03d", frame_in);
444 break;
445 case '4':
446 snprintf(q, q_len - 1, "%04d", frame_in);
447 break;
448 case '5':
449 snprintf(q, q_len - 1, "%05d", frame_in);
450 break;
451 case '6':
452 snprintf(q, q_len - 1, "%06d", frame_in);
453 break;
454 case '7':
455 snprintf(q, q_len - 1, "%07d", frame_in);
456 break;
457 case '8':
458 snprintf(q, q_len - 1, "%08d", frame_in);
459 break;
460 case '9':
461 snprintf(q, q_len - 1, "%09d", frame_in);
462 break;
463 default:
464 die("Unrecognized pattern %%%c\n", p[1]);
Jim Bankoski5c418912014-08-01 07:11:43 -0700465 break;
John Koleszarc6b90392012-07-13 15:21:29 -0700466 }
John Koleszar933d44b2010-10-21 20:40:42 -0700467
John Koleszarc6b90392012-07-13 15:21:29 -0700468 pat_len = strlen(q);
469 if (pat_len >= q_len - 1)
470 die("Output filename too long.\n");
471 q += pat_len;
472 p += 2;
473 q_len -= pat_len;
474 } else {
475 size_t copy_len;
John Koleszar933d44b2010-10-21 20:40:42 -0700476
John Koleszar3707c342012-11-06 10:09:36 -0800477 /* copy the next segment */
John Koleszarc6b90392012-07-13 15:21:29 -0700478 if (!next_pat)
479 copy_len = strlen(p);
480 else
481 copy_len = next_pat - p;
John Koleszar933d44b2010-10-21 20:40:42 -0700482
John Koleszarc6b90392012-07-13 15:21:29 -0700483 if (copy_len >= q_len - 1)
484 die("Output filename too long.\n");
John Koleszar933d44b2010-10-21 20:40:42 -0700485
John Koleszarc6b90392012-07-13 15:21:29 -0700486 memcpy(q, p, copy_len);
487 q[copy_len] = '\0';
488 q += copy_len;
489 p += copy_len;
490 q_len -= copy_len;
491 }
492 } while (*p);
John Koleszar933d44b2010-10-21 20:40:42 -0700493}
494
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800495static int is_single_file(const char *outfile_pattern) {
496 const char *p = outfile_pattern;
497
498 do {
499 p = strchr(p, '%');
500 if (p && p[1] >= '1' && p[1] <= '9')
501 return 0; // pattern contains sequence number, so it's not unique
502 if (p)
503 p++;
504 } while (p);
505
506 return 1;
507}
508
509static void print_md5(unsigned char digest[16], const char *filename) {
510 int i;
511
512 for (i = 0; i < 16; ++i)
513 printf("%02x", digest[i]);
514 printf(" %s\n", filename);
515}
516
517static FILE *open_outfile(const char *name) {
518 if (strcmp("-", name) == 0) {
519 set_binary_mode(stdout);
520 return stdout;
521 } else {
522 FILE *file = fopen(name, "wb");
523 if (!file)
James Zern6b993f02015-02-05 21:06:29 -0800524 fatal("Failed to open output file '%s'", name);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800525 return file;
526 }
527}
528
Yaowu Xu72889a22015-08-14 12:16:07 -0700529#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700530static int img_shifted_realloc_required(const vpx_image_t *img,
531 const vpx_image_t *shifted,
532 vpx_img_fmt_t required_fmt) {
533 return img->d_w != shifted->d_w ||
534 img->d_h != shifted->d_h ||
535 required_fmt != shifted->fmt;
536}
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700537#endif
538
James Zern6a2e0f02015-05-09 10:42:31 -0700539static int main_loop(int argc, const char **argv_) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800540 vpx_codec_ctx_t decoder;
John Koleszarc6b90392012-07-13 15:21:29 -0700541 char *fn = NULL;
Alex Converse2eccbd02014-02-13 17:02:50 -0800542 int i;
John Koleszarc6b90392012-07-13 15:21:29 -0700543 uint8_t *buf = NULL;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800544 size_t bytes_in_buffer = 0, buffer_size = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700545 FILE *infile;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800546 int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
hkuang1f841c32015-03-06 13:03:18 -0800547 int do_md5 = 0, progress = 0, frame_parallel = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700548 int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700549 int arg_skip = 0;
John Koleszar3707c342012-11-06 10:09:36 -0800550 int ec_enabled = 0;
James Zern95853db2014-07-01 19:02:15 -0700551 int keep_going = 0;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800552 const VpxInterface *interface = NULL;
553 const VpxInterface *fourcc_interface = NULL;
James Yu6d151322014-02-22 14:51:11 +0800554 uint64_t dx_time = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700555 struct arg arg;
556 char **argv, **argi, **argj;
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800557
John Koleszarc6b90392012-07-13 15:21:29 -0700558 int single_file;
559 int use_y4m = 1;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700560 int opt_yv12 = 0;
561 int opt_i420 = 0;
Jim Bankoski5c418912014-08-01 07:11:43 -0700562 vpx_codec_dec_cfg_t cfg = {0, 0, 0};
Yaowu Xu72889a22015-08-14 12:16:07 -0700563#if CONFIG_VP9_HIGHBITDEPTH
James Zernfcf16092015-10-06 22:56:34 -0700564 unsigned int output_bit_depth = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700565#endif
John Koleszara9c75972012-11-08 17:09:30 -0800566#if CONFIG_VP8_DECODER
John Koleszarc6b90392012-07-13 15:21:29 -0700567 vp8_postproc_cfg_t vp8_pp_cfg = {0};
568 int vp8_dbg_color_ref_frame = 0;
569 int vp8_dbg_color_mb_modes = 0;
570 int vp8_dbg_color_b_modes = 0;
571 int vp8_dbg_display_mv = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400572#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700573 int frames_corrupted = 0;
574 int dec_flags = 0;
John Koleszar83f2a112013-03-11 12:30:29 -0700575 int do_scale = 0;
John Koleszar7d8fc262013-02-25 16:58:47 -0800576 vpx_image_t *scaled_img = NULL;
Yaowu Xu72889a22015-08-14 12:16:07 -0700577#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700578 vpx_image_t *img_shifted = NULL;
579#endif
hkuang1f841c32015-03-06 13:03:18 -0800580 int frame_avail, got_data, flush_decoder = 0;
Frank Galligana4f30a52014-02-06 17:13:08 -0800581 int num_external_frame_buffers = 0;
Jim Bankoski5c418912014-08-01 07:11:43 -0700582 struct ExternalFrameBufferList ext_fb_list = {0, NULL};
John Koleszar0ea50ce2010-05-18 11:58:33 -0400583
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800584 const char *outfile_pattern = NULL;
585 char outfile_name[PATH_MAX] = {0};
586 FILE *outfile = NULL;
587
588 MD5Context md5_ctx;
589 unsigned char md5_digest[16];
590
Jim Bankoski5c418912014-08-01 07:11:43 -0700591 struct VpxDecInputContext input = {NULL, NULL};
592 struct VpxInputContext vpx_input_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700593#if CONFIG_WEBM_IO
Jim Bankoski7ae32692014-08-19 06:44:10 -0700594 struct WebmInputContext webm_ctx;
595 memset(&(webm_ctx), 0, sizeof(webm_ctx));
Tom Finegan2abe2d42013-11-18 14:39:51 -0800596 input.webm_ctx = &webm_ctx;
James Zernb6cce762014-05-10 17:44:12 -0700597#endif
598 input.vpx_input_ctx = &vpx_input_ctx;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800599
John Koleszarc6b90392012-07-13 15:21:29 -0700600 /* Parse command line */
601 exec_name = argv_[0];
602 argv = argv_dup(argc - 1, argv_ + 1);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400603
John Koleszarc6b90392012-07-13 15:21:29 -0700604 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
605 memset(&arg, 0, sizeof(arg));
606 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400607
John Koleszarc6b90392012-07-13 15:21:29 -0700608 if (arg_match(&arg, &codecarg, argi)) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800609 interface = get_vpx_decoder_by_name(arg.val);
610 if (!interface)
611 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszar9596a4c2013-06-12 16:08:35 -0700612 } else if (arg_match(&arg, &looparg, argi)) {
613 // no-op
John Koleszarc6b90392012-07-13 15:21:29 -0700614 } else if (arg_match(&arg, &outputfile, argi))
615 outfile_pattern = arg.val;
616 else if (arg_match(&arg, &use_yv12, argi)) {
617 use_y4m = 0;
618 flipuv = 1;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700619 opt_yv12 = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700620 } else if (arg_match(&arg, &use_i420, argi)) {
621 use_y4m = 0;
622 flipuv = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700623 opt_i420 = 1;
624 } else if (arg_match(&arg, &rawvideo, argi)) {
625 use_y4m = 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700626 } else if (arg_match(&arg, &flipuvarg, argi))
627 flipuv = 1;
628 else if (arg_match(&arg, &noblitarg, argi))
629 noblit = 1;
630 else if (arg_match(&arg, &progressarg, argi))
631 progress = 1;
632 else if (arg_match(&arg, &limitarg, argi))
633 stop_after = arg_parse_uint(&arg);
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700634 else if (arg_match(&arg, &skiparg, argi))
635 arg_skip = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700636 else if (arg_match(&arg, &postprocarg, argi))
637 postproc = 1;
638 else if (arg_match(&arg, &md5arg, argi))
639 do_md5 = 1;
640 else if (arg_match(&arg, &summaryarg, argi))
641 summary = 1;
642 else if (arg_match(&arg, &threadsarg, argi))
643 cfg.threads = arg_parse_uint(&arg);
Jingning Han3ee6db62015-08-05 19:00:31 -0700644#if CONFIG_VP9_DECODER || CONFIG_VP10_DECODER
hkuang1f841c32015-03-06 13:03:18 -0800645 else if (arg_match(&arg, &frameparallelarg, argi))
646 frame_parallel = 1;
647#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700648 else if (arg_match(&arg, &verbosearg, argi))
649 quiet = 0;
John Koleszar7d8fc262013-02-25 16:58:47 -0800650 else if (arg_match(&arg, &scalearg, argi))
651 do_scale = 1;
Frank Galligana4f30a52014-02-06 17:13:08 -0800652 else if (arg_match(&arg, &fb_arg, argi))
653 num_external_frame_buffers = arg_parse_uint(&arg);
James Zern7690d882014-08-22 18:01:28 -0700654 else if (arg_match(&arg, &continuearg, argi))
655 keep_going = 1;
Yaowu Xu72889a22015-08-14 12:16:07 -0700656#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700657 else if (arg_match(&arg, &outbitdeptharg, argi)) {
658 output_bit_depth = arg_parse_uint(&arg);
659 }
660#endif
John Koleszara9c75972012-11-08 17:09:30 -0800661#if CONFIG_VP8_DECODER
John Koleszarc6b90392012-07-13 15:21:29 -0700662 else if (arg_match(&arg, &addnoise_level, argi)) {
663 postproc = 1;
664 vp8_pp_cfg.post_proc_flag |= VP8_ADDNOISE;
665 vp8_pp_cfg.noise_level = arg_parse_uint(&arg);
666 } else if (arg_match(&arg, &demacroblock_level, argi)) {
667 postproc = 1;
668 vp8_pp_cfg.post_proc_flag |= VP8_DEMACROBLOCK;
669 vp8_pp_cfg.deblocking_level = arg_parse_uint(&arg);
670 } else if (arg_match(&arg, &deblock, argi)) {
671 postproc = 1;
672 vp8_pp_cfg.post_proc_flag |= VP8_DEBLOCK;
John Koleszar3707c342012-11-06 10:09:36 -0800673 } else if (arg_match(&arg, &mfqe, argi)) {
674 postproc = 1;
675 vp8_pp_cfg.post_proc_flag |= VP8_MFQE;
John Koleszarc6b90392012-07-13 15:21:29 -0700676 } else if (arg_match(&arg, &pp_debug_info, argi)) {
677 unsigned int level = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400678
John Koleszarc6b90392012-07-13 15:21:29 -0700679 postproc = 1;
680 vp8_pp_cfg.post_proc_flag &= ~0x7;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400681
John Koleszarc6b90392012-07-13 15:21:29 -0700682 if (level)
683 vp8_pp_cfg.post_proc_flag |= level;
684 } else if (arg_match(&arg, &pp_disp_ref_frame, argi)) {
685 unsigned int flags = arg_parse_int(&arg);
686 if (flags) {
687 postproc = 1;
688 vp8_dbg_color_ref_frame = flags;
689 }
690 } else if (arg_match(&arg, &pp_disp_mb_modes, argi)) {
691 unsigned int flags = arg_parse_int(&arg);
692 if (flags) {
693 postproc = 1;
694 vp8_dbg_color_mb_modes = flags;
695 }
696 } else if (arg_match(&arg, &pp_disp_b_modes, argi)) {
697 unsigned int flags = arg_parse_int(&arg);
698 if (flags) {
699 postproc = 1;
700 vp8_dbg_color_b_modes = flags;
701 }
702 } else if (arg_match(&arg, &pp_disp_mvs, argi)) {
703 unsigned int flags = arg_parse_int(&arg);
704 if (flags) {
705 postproc = 1;
706 vp8_dbg_display_mv = flags;
707 }
John Koleszar3707c342012-11-06 10:09:36 -0800708 } else if (arg_match(&arg, &error_concealment, argi)) {
709 ec_enabled = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700710 }
James Zern7690d882014-08-22 18:01:28 -0700711#endif // CONFIG_VP8_DECODER
John Koleszarcfe3f912010-10-20 10:49:12 -0400712 else
John Koleszarc6b90392012-07-13 15:21:29 -0700713 argj++;
714 }
715
716 /* Check for unrecognized options */
717 for (argi = argv; *argi; argi++)
718 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
719 die("Error: Unrecognized option %s\n", *argi);
720
721 /* Handle non-option arguments */
722 fn = argv[0];
723
Hanno Böckb5d0a202015-02-24 15:48:21 -0800724 if (!fn) {
725 free(argv);
John Koleszarc6b90392012-07-13 15:21:29 -0700726 usage_exit();
Hanno Böckb5d0a202015-02-24 15:48:21 -0800727 }
John Koleszarc6b90392012-07-13 15:21:29 -0700728 /* Open file */
729 infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
730
731 if (!infile) {
James Zern6b993f02015-02-05 21:06:29 -0800732 fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
John Koleszarc6b90392012-07-13 15:21:29 -0700733 }
734#if CONFIG_OS_SUPPORT
735 /* Make sure we don't dump to the terminal, unless forced to with -o - */
736 if (!outfile_pattern && isatty(fileno(stdout)) && !do_md5 && !noblit) {
737 fprintf(stderr,
738 "Not dumping raw video to your terminal. Use '-o -' to "
739 "override.\n");
740 return EXIT_FAILURE;
741 }
742#endif
Tom Finegan00a35aa2013-11-14 12:37:42 -0800743 input.vpx_input_ctx->file = infile;
744 if (file_is_ivf(input.vpx_input_ctx))
745 input.vpx_input_ctx->file_type = FILE_TYPE_IVF;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700746#if CONFIG_WEBM_IO
Tom Finegan2abe2d42013-11-18 14:39:51 -0800747 else if (file_is_webm(input.webm_ctx, input.vpx_input_ctx))
Tom Finegan00a35aa2013-11-14 12:37:42 -0800748 input.vpx_input_ctx->file_type = FILE_TYPE_WEBM;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700749#endif
Tom Finegan00a35aa2013-11-14 12:37:42 -0800750 else if (file_is_raw(input.vpx_input_ctx))
751 input.vpx_input_ctx->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700752 else {
753 fprintf(stderr, "Unrecognized input file type.\n");
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700754#if !CONFIG_WEBM_IO
755 fprintf(stderr, "vpxdec was built without WebM container support.\n");
756#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700757 return EXIT_FAILURE;
758 }
759
John Koleszarc6b90392012-07-13 15:21:29 -0700760 outfile_pattern = outfile_pattern ? outfile_pattern : "-";
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800761 single_file = is_single_file(outfile_pattern);
John Koleszarc6b90392012-07-13 15:21:29 -0700762
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800763 if (!noblit && single_file) {
764 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
Tom Finegan00a35aa2013-11-14 12:37:42 -0800765 vpx_input_ctx.width, vpx_input_ctx.height, 0);
Dmitry Kovalev347b3062014-01-22 09:04:07 -0800766 if (do_md5)
767 MD5Init(&md5_ctx);
768 else
769 outfile = open_outfile(outfile_name);
John Koleszarc6b90392012-07-13 15:21:29 -0700770 }
771
772 if (use_y4m && !noblit) {
John Koleszarc6b90392012-07-13 15:21:29 -0700773 if (!single_file) {
774 fprintf(stderr, "YUV4MPEG2 not supported with output patterns,"
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700775 " try --i420 or --yv12 or --rawvideo.\n");
John Koleszarc6b90392012-07-13 15:21:29 -0700776 return EXIT_FAILURE;
777 }
778
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700779#if CONFIG_WEBM_IO
Dmitry Kovalev5ab63582014-01-17 17:02:37 -0800780 if (vpx_input_ctx.file_type == FILE_TYPE_WEBM) {
Tom Finegan2abe2d42013-11-18 14:39:51 -0800781 if (webm_guess_framerate(input.webm_ctx, input.vpx_input_ctx)) {
John Koleszarc6b90392012-07-13 15:21:29 -0700782 fprintf(stderr, "Failed to guess framerate -- error parsing "
783 "webm file?\n");
Timothy B. Terriberry7f9db412010-05-26 19:36:20 -0400784 return EXIT_FAILURE;
John Koleszarc6b90392012-07-13 15:21:29 -0700785 }
Dmitry Kovalev5ab63582014-01-17 17:02:37 -0800786 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700787#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700788 }
789
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800790 fourcc_interface = get_vpx_decoder_by_fourcc(vpx_input_ctx.fourcc);
791 if (interface && fourcc_interface && interface != fourcc_interface)
792 warn("Header indicates codec: %s\n", fourcc_interface->name);
793 else
794 interface = fourcc_interface;
John Koleszarc6b90392012-07-13 15:21:29 -0700795
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800796 if (!interface)
797 interface = get_vpx_decoder_by_index(0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400798
John Koleszar3707c342012-11-06 10:09:36 -0800799 dec_flags = (postproc ? VPX_CODEC_USE_POSTPROC : 0) |
hkuang1f841c32015-03-06 13:03:18 -0800800 (ec_enabled ? VPX_CODEC_USE_ERROR_CONCEALMENT : 0) |
801 (frame_parallel ? VPX_CODEC_USE_FRAME_THREADING : 0);
Deb Mukherjeef332c632014-07-15 16:55:12 -0700802 if (vpx_codec_dec_init(&decoder, interface->codec_interface(),
803 &cfg, dec_flags)) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800804 fprintf(stderr, "Failed to initialize decoder: %s\n",
805 vpx_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -0700806 return EXIT_FAILURE;
807 }
John Koleszar933d44b2010-10-21 20:40:42 -0700808
John Koleszarc6b90392012-07-13 15:21:29 -0700809 if (!quiet)
810 fprintf(stderr, "%s\n", decoder.name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400811
John Koleszara9c75972012-11-08 17:09:30 -0800812#if CONFIG_VP8_DECODER
John Koleszarc6b90392012-07-13 15:21:29 -0700813 if (vp8_pp_cfg.post_proc_flag
814 && vpx_codec_control(&decoder, VP8_SET_POSTPROC, &vp8_pp_cfg)) {
James Zern3a070ba2015-06-05 16:18:49 -0700815 fprintf(stderr, "Failed to configure postproc: %s\n",
816 vpx_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -0700817 return EXIT_FAILURE;
818 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400819
John Koleszarc6b90392012-07-13 15:21:29 -0700820 if (vp8_dbg_color_ref_frame
James Zern3a070ba2015-06-05 16:18:49 -0700821 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_REF_FRAME,
822 vp8_dbg_color_ref_frame)) {
823 fprintf(stderr, "Failed to configure reference block visualizer: %s\n",
824 vpx_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -0700825 return EXIT_FAILURE;
826 }
Fritz Koenig647df002010-11-04 16:03:36 -0700827
John Koleszarc6b90392012-07-13 15:21:29 -0700828 if (vp8_dbg_color_mb_modes
James Zern3a070ba2015-06-05 16:18:49 -0700829 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_MB_MODES,
830 vp8_dbg_color_mb_modes)) {
831 fprintf(stderr, "Failed to configure macro block visualizer: %s\n",
832 vpx_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -0700833 return EXIT_FAILURE;
834 }
Fritz Koenig647df002010-11-04 16:03:36 -0700835
John Koleszarc6b90392012-07-13 15:21:29 -0700836 if (vp8_dbg_color_b_modes
James Zern3a070ba2015-06-05 16:18:49 -0700837 && vpx_codec_control(&decoder, VP8_SET_DBG_COLOR_B_MODES,
838 vp8_dbg_color_b_modes)) {
839 fprintf(stderr, "Failed to configure block visualizer: %s\n",
840 vpx_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -0700841 return EXIT_FAILURE;
842 }
Fritz Koenig647df002010-11-04 16:03:36 -0700843
John Koleszarc6b90392012-07-13 15:21:29 -0700844 if (vp8_dbg_display_mv
James Zern3a070ba2015-06-05 16:18:49 -0700845 && vpx_codec_control(&decoder, VP8_SET_DBG_DISPLAY_MV,
846 vp8_dbg_display_mv)) {
847 fprintf(stderr, "Failed to configure motion vector visualizer: %s\n",
848 vpx_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -0700849 return EXIT_FAILURE;
850 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400851#endif
852
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700853
Tom Finegan00a35aa2013-11-14 12:37:42 -0800854 if (arg_skip)
Adrian Grange475d1d62013-11-20 09:49:33 -0800855 fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700856 while (arg_skip) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800857 if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size))
Yaowu Xuc394ffc2012-10-11 15:12:35 -0700858 break;
859 arg_skip--;
860 }
861
Frank Galligana4f30a52014-02-06 17:13:08 -0800862 if (num_external_frame_buffers > 0) {
863 ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
864 ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
865 num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
866 if (vpx_codec_set_frame_buffer_functions(
867 &decoder, get_vp9_frame_buffer, release_vp9_frame_buffer,
868 &ext_fb_list)) {
869 fprintf(stderr, "Failed to configure external frame buffers: %s\n",
870 vpx_codec_error(&decoder));
871 return EXIT_FAILURE;
872 }
873 }
874
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400875 frame_avail = 1;
876 got_data = 0;
877
John Koleszarc6b90392012-07-13 15:21:29 -0700878 /* Decode file */
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400879 while (frame_avail || got_data) {
John Koleszarc6b90392012-07-13 15:21:29 -0700880 vpx_codec_iter_t iter = NULL;
881 vpx_image_t *img;
882 struct vpx_usec_timer timer;
Johann5370c6f2015-03-18 11:01:28 -0700883 int corrupted = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400884
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400885 frame_avail = 0;
886 if (!stop_after || frame_in < stop_after) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800887 if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400888 frame_avail = 1;
889 frame_in++;
890
891 vpx_usec_timer_start(&timer);
892
Jim Bankoski24025c02014-02-14 07:27:10 -0800893 if (vpx_codec_decode(&decoder, buf, (unsigned int)bytes_in_buffer,
894 NULL, 0)) {
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400895 const char *detail = vpx_codec_error_detail(&decoder);
Tom Fineganf225b1e2013-11-18 15:50:58 -0800896 warn("Failed to decode frame %d: %s",
897 frame_in, vpx_codec_error(&decoder));
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400898
899 if (detail)
Tom Fineganf225b1e2013-11-18 15:50:58 -0800900 warn("Additional information: %s", detail);
James Zern95853db2014-07-01 19:02:15 -0700901 if (!keep_going)
902 goto fail;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400903 }
904
905 vpx_usec_timer_mark(&timer);
James Yu6d151322014-02-22 14:51:11 +0800906 dx_time += vpx_usec_timer_elapsed(&timer);
hkuang1f841c32015-03-06 13:03:18 -0800907 } else {
908 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400909 }
hkuang1f841c32015-03-06 13:03:18 -0800910 } else {
911 flush_decoder = 1;
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400912 }
913
John Koleszarc6b90392012-07-13 15:21:29 -0700914 vpx_usec_timer_start(&timer);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400915
hkuang1f841c32015-03-06 13:03:18 -0800916 if (flush_decoder) {
917 // Flush the decoder in frame parallel decode.
918 if (vpx_codec_decode(&decoder, NULL, 0, NULL, 0)) {
919 warn("Failed to flush decoder: %s", vpx_codec_error(&decoder));
920 }
921 }
922
Scott LaVarnwayc3ae2122013-06-07 12:39:03 -0400923 got_data = 0;
924 if ((img = vpx_codec_get_frame(&decoder, &iter))) {
925 ++frame_out;
926 got_data = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400927 }
928
John Koleszarc6b90392012-07-13 15:21:29 -0700929 vpx_usec_timer_mark(&timer);
John Koleszar3707c342012-11-06 10:09:36 -0800930 dx_time += (unsigned int)vpx_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -0700931
hkuang1f841c32015-03-06 13:03:18 -0800932 if (!frame_parallel &&
933 vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted)) {
Tom Fineganf225b1e2013-11-18 15:50:58 -0800934 warn("Failed VP8_GET_FRAME_CORRUPTED: %s", vpx_codec_error(&decoder));
James Zernfcbf3e42014-12-17 19:20:00 -0800935 if (!keep_going)
936 goto fail;
John Koleszarc6b90392012-07-13 15:21:29 -0700937 }
938 frames_corrupted += corrupted;
939
John Koleszarc6b90392012-07-13 15:21:29 -0700940 if (progress)
941 show_progress(frame_in, frame_out, dx_time);
942
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800943 if (!noblit && img) {
944 const int PLANES_YUV[] = {VPX_PLANE_Y, VPX_PLANE_U, VPX_PLANE_V};
945 const int PLANES_YVU[] = {VPX_PLANE_Y, VPX_PLANE_V, VPX_PLANE_U};
946 const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
John Koleszarda584362013-05-06 15:52:06 -0700947
Dmitry Kovalevf2acb452014-01-23 17:20:34 -0800948 if (do_scale) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800949 if (frame_out == 1) {
950 // If the output frames are to be scaled to a fixed display size then
951 // use the width and height specified in the container. If either of
952 // these is set to 0, use the display size set in the first frame
Adrian Grange82f6af62014-01-22 09:36:07 -0800953 // header. If that is unavailable, use the raw decoded size of the
954 // first decoded frame.
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400955 int render_width = vpx_input_ctx.width;
956 int render_height = vpx_input_ctx.height;
957 if (!render_width || !render_height) {
958 int render_size[2];
Adrian Grange482c39c2013-12-13 10:15:03 -0800959 if (vpx_codec_control(&decoder, VP9D_GET_DISPLAY_SIZE,
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400960 render_size)) {
Adrian Grange482c39c2013-12-13 10:15:03 -0800961 // As last resort use size of first frame as display size.
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400962 render_width = img->d_w;
963 render_height = img->d_h;
Adrian Grange482c39c2013-12-13 10:15:03 -0800964 } else {
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400965 render_width = render_size[0];
966 render_height = render_size[1];
Adrian Grange482c39c2013-12-13 10:15:03 -0800967 }
Adrian Grange2117fe02013-11-19 14:01:44 -0800968 }
Ronald S. Bultje36ffe642015-09-25 21:34:29 -0400969 scaled_img = vpx_img_alloc(NULL, img->fmt, render_width,
970 render_height, 16);
Deb Mukherjee449e5f22014-07-11 11:43:31 -0700971 scaled_img->bit_depth = img->bit_depth;
John Koleszar7d8fc262013-02-25 16:58:47 -0800972 }
Adrian Grange482c39c2013-12-13 10:15:03 -0800973
Dmitry Kovalev3c054812014-01-17 12:19:19 -0800974 if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
Deb Mukherjee47031c02014-05-16 18:52:01 -0700975#if CONFIG_LIBYUV
Tom Finegan8ef2ce82014-12-12 11:57:04 -0800976 libyuv_scale(img, scaled_img, kFilterBox);
John Koleszar7d8fc262013-02-25 16:58:47 -0800977 img = scaled_img;
Deb Mukherjee47031c02014-05-16 18:52:01 -0700978#else
979 fprintf(stderr, "Failed to scale output frame: %s.\n"
980 "Scaling is disabled in this configuration. "
981 "To enable scaling, configure with --enable-libyuv\n",
982 vpx_codec_error(&decoder));
983 return EXIT_FAILURE;
984#endif
John Koleszar7d8fc262013-02-25 16:58:47 -0800985 }
986 }
Yaowu Xu72889a22015-08-14 12:16:07 -0700987#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700988 // Default to codec bit depth if output bit depth not set
Ronald S. Bultjeeba342a2015-09-11 16:55:19 -0400989 if (!output_bit_depth && single_file && !do_md5) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700990 output_bit_depth = img->bit_depth;
991 }
992 // Shift up or down if necessary
Ronald S. Bultjeeba342a2015-09-11 16:55:19 -0400993 if (output_bit_depth != 0 && output_bit_depth != img->bit_depth) {
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700994 const vpx_img_fmt_t shifted_fmt = output_bit_depth == 8 ?
995 img->fmt ^ (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) :
996 img->fmt | VPX_IMG_FMT_HIGHBITDEPTH;
997 if (img_shifted &&
998 img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
999 vpx_img_free(img_shifted);
1000 img_shifted = NULL;
1001 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001002 if (!img_shifted) {
Deb Mukherjee9ed23de2014-09-25 15:46:50 -07001003 img_shifted = vpx_img_alloc(NULL, shifted_fmt,
1004 img->d_w, img->d_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001005 img_shifted->bit_depth = output_bit_depth;
1006 }
1007 if (output_bit_depth > img->bit_depth) {
Deb Mukherjee7a2a6112014-10-06 20:46:11 -07001008 vpx_img_upshift(img_shifted, img,
1009 output_bit_depth - img->bit_depth);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001010 } else {
Deb Mukherjee7a2a6112014-10-06 20:46:11 -07001011 vpx_img_downshift(img_shifted, img,
1012 img->bit_depth - output_bit_depth);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001013 }
1014 img = img_shifted;
1015 }
1016#endif
Dmitry Kovalev3c054812014-01-17 12:19:19 -08001017
Dmitry Kovalevf2acb452014-01-23 17:20:34 -08001018 if (single_file) {
Dmitry Kovalev5260b392014-01-28 11:12:58 -08001019 if (use_y4m) {
1020 char buf[Y4M_BUFFER_SIZE] = {0};
1021 size_t len = 0;
Deb Mukherjeea30774c2014-10-01 12:17:37 -07001022 if (img->fmt == VPX_IMG_FMT_I440 || img->fmt == VPX_IMG_FMT_I44016) {
1023 fprintf(stderr, "Cannot produce y4m output for 440 sampling.\n");
1024 goto fail;
1025 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -08001026 if (frame_out == 1) {
1027 // Y4M file header
1028 len = y4m_write_file_header(buf, sizeof(buf),
1029 vpx_input_ctx.width,
1030 vpx_input_ctx.height,
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001031 &vpx_input_ctx.framerate,
Deb Mukherjee449e5f22014-07-11 11:43:31 -07001032 img->fmt, img->bit_depth);
Dmitry Kovalev5260b392014-01-28 11:12:58 -08001033 if (do_md5) {
Jim Bankoski24025c02014-02-14 07:27:10 -08001034 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -08001035 } else {
1036 fputs(buf, outfile);
1037 }
1038 }
1039
1040 // Y4M frame header
1041 len = y4m_write_frame_header(buf, sizeof(buf));
1042 if (do_md5) {
Jim Bankoski24025c02014-02-14 07:27:10 -08001043 MD5Update(&md5_ctx, (md5byte *)buf, (unsigned int)len);
Dmitry Kovalev5260b392014-01-28 11:12:58 -08001044 } else {
1045 fputs(buf, outfile);
1046 }
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001047 } else {
1048 if (frame_out == 1) {
1049 // Check if --yv12 or --i420 options are consistent with the
1050 // bit-stream decoded
1051 if (opt_i420) {
1052 if (img->fmt != VPX_IMG_FMT_I420 &&
1053 img->fmt != VPX_IMG_FMT_I42016) {
1054 fprintf(stderr, "Cannot produce i420 output for bit-stream.\n");
1055 goto fail;
1056 }
1057 }
1058 if (opt_yv12) {
1059 if ((img->fmt != VPX_IMG_FMT_I420 &&
1060 img->fmt != VPX_IMG_FMT_YV12) || img->bit_depth != 8) {
1061 fprintf(stderr, "Cannot produce yv12 output for bit-stream.\n");
1062 goto fail;
1063 }
1064 }
1065 }
Dmitry Kovalev5260b392014-01-28 11:12:58 -08001066 }
1067
Dmitry Kovalevf2acb452014-01-23 17:20:34 -08001068 if (do_md5) {
1069 update_image_md5(img, planes, &md5_ctx);
Dmitry Kovalevcf46bc72014-01-17 10:57:55 -08001070 } else {
Dmitry Kovalevf2acb452014-01-23 17:20:34 -08001071 write_image_file(img, planes, outfile);
1072 }
1073 } else {
1074 generate_filename(outfile_pattern, outfile_name, PATH_MAX,
1075 img->d_w, img->d_h, frame_in);
1076 if (do_md5) {
1077 MD5Init(&md5_ctx);
1078 update_image_md5(img, planes, &md5_ctx);
1079 MD5Final(md5_digest, &md5_ctx);
1080 print_md5(md5_digest, outfile_name);
1081 } else {
1082 outfile = open_outfile(outfile_name);
1083 write_image_file(img, planes, outfile);
1084 fclose(outfile);
John Koleszarc6b90392012-07-13 15:21:29 -07001085 }
John Koleszarc6b90392012-07-13 15:21:29 -07001086 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001087 }
John Koleszarc6b90392012-07-13 15:21:29 -07001088 }
1089
1090 if (summary || progress) {
1091 show_progress(frame_in, frame_out, dx_time);
1092 fprintf(stderr, "\n");
1093 }
1094
1095 if (frames_corrupted)
1096 fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
John Koleszar4226f0c2011-04-27 12:04:48 -04001097
John Koleszar0ea50ce2010-05-18 11:58:33 -04001098fail:
1099
John Koleszarc6b90392012-07-13 15:21:29 -07001100 if (vpx_codec_destroy(&decoder)) {
Adrian Granged427fab2013-11-21 17:19:04 -08001101 fprintf(stderr, "Failed to destroy decoder: %s\n",
1102 vpx_codec_error(&decoder));
John Koleszarc6b90392012-07-13 15:21:29 -07001103 return EXIT_FAILURE;
1104 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001105
Dmitry Kovalev347b3062014-01-22 09:04:07 -08001106 if (!noblit && single_file) {
1107 if (do_md5) {
1108 MD5Final(md5_digest, &md5_ctx);
1109 print_md5(md5_digest, outfile_name);
1110 } else {
1111 fclose(outfile);
1112 }
1113 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001114
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001115#if CONFIG_WEBM_IO
Tom Finegan2abe2d42013-11-18 14:39:51 -08001116 if (input.vpx_input_ctx->file_type == FILE_TYPE_WEBM)
1117 webm_free(input.webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001118#endif
1119
1120 if (input.vpx_input_ctx->file_type != FILE_TYPE_WEBM)
John Koleszarc6b90392012-07-13 15:21:29 -07001121 free(buf);
Tom Finegan2abe2d42013-11-18 14:39:51 -08001122
Adrian Granged427fab2013-11-21 17:19:04 -08001123 if (scaled_img) vpx_img_free(scaled_img);
Yaowu Xu72889a22015-08-14 12:16:07 -07001124#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001125 if (img_shifted) vpx_img_free(img_shifted);
1126#endif
Adrian Granged427fab2013-11-21 17:19:04 -08001127
Frank Galligana4f30a52014-02-06 17:13:08 -08001128 for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1129 free(ext_fb_list.ext_fb[i].data);
1130 }
1131 free(ext_fb_list.ext_fb);
1132
John Koleszarc6b90392012-07-13 15:21:29 -07001133 fclose(infile);
1134 free(argv);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001135
John Koleszarc6b90392012-07-13 15:21:29 -07001136 return frames_corrupted ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001137}
John Koleszar9596a4c2013-06-12 16:08:35 -07001138
1139int main(int argc, const char **argv_) {
1140 unsigned int loops = 1, i;
1141 char **argv, **argi, **argj;
1142 struct arg arg;
1143 int error = 0;
1144
1145 argv = argv_dup(argc - 1, argv_ + 1);
1146 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1147 memset(&arg, 0, sizeof(arg));
1148 arg.argv_step = 1;
1149
1150 if (arg_match(&arg, &looparg, argi)) {
1151 loops = arg_parse_uint(&arg);
1152 break;
1153 }
1154 }
1155 free(argv);
1156 for (i = 0; !error && i < loops; i++)
1157 error = main_loop(argc, argv_);
1158 return error;
1159}