blob: 4ddb15bab070300299aef7bc84e22dc382a57867 [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
Yaowu Xuf883b422016-08-30 14:01:10 -070012#include "./aomenc.h"
13#include "./aom_config.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040014
Tom Finegan00a35aa2013-11-14 12:37:42 -080015#include <assert.h>
16#include <limits.h>
Tom Finegan78cb2e62013-11-07 21:28:45 -080017#include <math.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080018#include <stdarg.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040019#include <stdio.h>
20#include <stdlib.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040021#include <string.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080022
James Zern58058812014-08-15 21:00:31 -070023#if CONFIG_LIBYUV
24#include "third_party/libyuv/include/libyuv/scale.h"
25#endif
26
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom/aom_encoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080028#if CONFIG_DECODERS
Yaowu Xuf883b422016-08-30 14:01:10 -070029#include "aom/aom_decoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080030#endif
John Koleszar6ad3b742012-11-06 12:02:42 -080031
Tom Finegan00a35aa2013-11-14 12:37:42 -080032#include "./args.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080033#include "./ivfenc.h"
Tom Finegan7a691f12014-02-10 14:55:25 -080034#include "./tools_common.h"
Tom Finegan03848f52013-11-05 10:02:18 -080035
Yaowu Xuf883b422016-08-30 14:01:10 -070036#if CONFIG_AV1_ENCODER
37#include "aom/aomcx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080038#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070039#if CONFIG_AV1_DECODER
40#include "aom/aomdx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080041#endif
42
Yaowu Xuf883b422016-08-30 14:01:10 -070043#include "./aomstats.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080044#include "./rate_hist.h"
Tom Finegan249366b2013-11-25 12:05:19 -080045#include "./warnings.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080046#include "aom/aom_integer.h"
47#include "aom_ports/aom_timer.h"
48#include "aom_ports/mem_ops.h"
James Zerndba73762014-05-10 17:44:12 -070049#if CONFIG_WEBM_IO
Tom Finegan78cb2e62013-11-07 21:28:45 -080050#include "./webmenc.h"
James Zerndba73762014-05-10 17:44:12 -070051#endif
Tom Finegan78cb2e62013-11-07 21:28:45 -080052#include "./y4minput.h"
Tom Finegan03848f52013-11-05 10:02:18 -080053
John Koleszar6291dd42012-06-19 21:05:36 -070054/* Swallow warnings about unused results of fread/fwrite */
clang-format6c4d83e2016-08-08 19:03:30 -070055static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
John Koleszar6ad3b742012-11-06 12:02:42 -080056 return fread(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070057}
58#define fread wrap_fread
59
60static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080061 FILE *stream) {
62 return fwrite(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070063}
64#define fwrite wrap_fwrite
65
John Koleszar0ea50ce2010-05-18 11:58:33 -040066static const char *exec_name;
67
Yaowu Xuf883b422016-08-30 14:01:10 -070068static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080069 const char *s, va_list ap) {
John Koleszarc6b90392012-07-13 15:21:29 -070070 if (ctx->err) {
Yaowu Xuf883b422016-08-30 14:01:10 -070071 const char *detail = aom_codec_error_detail(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -040072
John Koleszar6ad3b742012-11-06 12:02:42 -080073 vfprintf(stderr, s, ap);
Yaowu Xuf883b422016-08-30 14:01:10 -070074 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -040075
clang-format6c4d83e2016-08-08 19:03:30 -070076 if (detail) fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -040077
clang-format6c4d83e2016-08-08 19:03:30 -070078 if (fatal) exit(EXIT_FAILURE);
John Koleszarc6b90392012-07-13 15:21:29 -070079 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040080}
81
Yaowu Xuf883b422016-08-30 14:01:10 -070082static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080083 va_list ap;
84
85 va_start(ap, s);
86 warn_or_exit_on_errorv(ctx, 1, s, ap);
87 va_end(ap);
88}
89
Yaowu Xuf883b422016-08-30 14:01:10 -070090static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080091 const char *s, ...) {
92 va_list ap;
93
94 va_start(ap, s);
95 warn_or_exit_on_errorv(ctx, fatal, s, ap);
96 va_end(ap);
97}
98
Yaowu Xuf883b422016-08-30 14:01:10 -070099static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800100 FILE *f = input_ctx->file;
101 y4m_input *y4m = &input_ctx->y4m;
John Koleszarc6b90392012-07-13 15:21:29 -0700102 int shortread = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400103
Tom Finegan00a35aa2013-11-14 12:37:42 -0800104 if (input_ctx->file_type == FILE_TYPE_Y4M) {
clang-format6c4d83e2016-08-08 19:03:30 -0700105 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700106 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800107 shortread = read_yuv_frame(input_ctx, img);
John Koleszarc6b90392012-07-13 15:21:29 -0700108 }
109
110 return !shortread;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400111}
112
James Zern5c337fd2015-05-09 10:42:58 -0700113static int file_is_y4m(const char detect[4]) {
John Koleszarc6b90392012-07-13 15:21:29 -0700114 if (memcmp(detect, "YUV4", 4) == 0) {
115 return 1;
116 }
117 return 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400118}
119
James Zern5c337fd2015-05-09 10:42:58 -0700120static int fourcc_is_ivf(const char detect[4]) {
Alex Converse64b89f12014-01-06 16:29:09 -0800121 if (memcmp(detect, "DKIF", 4) == 0) {
122 return 1;
123 }
124 return 0;
125}
John Koleszardc666302010-10-20 12:05:48 -0400126
clang-format6c4d83e2016-08-08 19:03:30 -0700127static const arg_def_t debugmode =
128 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
129static const arg_def_t outputfile =
130 ARG_DEF("o", "output", 1, "Output filename");
131static const arg_def_t use_yv12 =
132 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
133static const arg_def_t use_i420 =
134 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
135static const arg_def_t use_i422 =
136 ARG_DEF(NULL, "i422", 0, "Input file is I422");
137static const arg_def_t use_i444 =
138 ARG_DEF(NULL, "i444", 0, "Input file is I444");
139static const arg_def_t use_i440 =
140 ARG_DEF(NULL, "i440", 0, "Input file is I440");
141static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
142static const arg_def_t passes =
143 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
144static const arg_def_t pass_arg =
145 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
146static const arg_def_t fpf_name =
147 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700148#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700149static const arg_def_t fpmbf_name =
150 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700151#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700152static const arg_def_t limit =
153 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
154static const arg_def_t skip =
155 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
156static const arg_def_t deadline =
157 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
158static const arg_def_t best_dl =
159 ARG_DEF(NULL, "best", 0, "Use Best Quality Deadline");
160static const arg_def_t good_dl =
161 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
162static const arg_def_t rt_dl =
163 ARG_DEF(NULL, "rt", 0, "Use Realtime Quality Deadline");
164static const arg_def_t quietarg =
165 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
166static const arg_def_t verbosearg =
167 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
168static const arg_def_t psnrarg =
169 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800170
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800171static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700172 { "off", TEST_DECODE_OFF },
173 { "fatal", TEST_DECODE_FATAL },
174 { "warn", TEST_DECODE_WARN },
175 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800176};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700177static const arg_def_t recontest = ARG_DEF_ENUM(
178 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700179static const arg_def_t framerate =
180 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
181static const arg_def_t use_webm =
182 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
183static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
184static const arg_def_t out_part =
185 ARG_DEF("P", "output-partitions", 0,
186 "Makes encoder output partitions. Requires IVF output!");
187static const arg_def_t q_hist_n =
188 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
189static const arg_def_t rate_hist_n =
190 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
191static const arg_def_t disable_warnings =
192 ARG_DEF(NULL, "disable-warnings", 0,
193 "Disable warnings about potentially incorrect encode settings.");
194static const arg_def_t disable_warning_prompt =
195 ARG_DEF("y", "disable-warning-prompt", 0,
196 "Display warnings, but do not prompt user to continue.");
Alex Conversed66bd222014-02-21 10:52:09 -0800197
Yaowu Xuf883b422016-08-30 14:01:10 -0700198#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700199static const arg_def_t test16bitinternalarg = ARG_DEF(
200 NULL, "test-16bit-internal", 0, "Force use of 16 bit internal buffer");
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400201
202static const struct arg_enum_list bitdepth_enum[] = {
203 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
204};
205
206static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
207 "b", "bit-depth", 1,
208 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
209 bitdepth_enum);
210static const arg_def_t inbitdeptharg =
211 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700212#endif
Tom Finegan44dd3272013-11-20 17:18:28 -0800213
clang-format6c4d83e2016-08-08 19:03:30 -0700214static const arg_def_t *main_args[] = { &debugmode,
215 &outputfile,
216 &codecarg,
217 &passes,
218 &pass_arg,
219 &fpf_name,
220 &limit,
221 &skip,
222 &deadline,
223 &best_dl,
224 &good_dl,
225 &rt_dl,
226 &quietarg,
227 &verbosearg,
228 &psnrarg,
229 &use_webm,
230 &use_ivf,
231 &out_part,
232 &q_hist_n,
233 &rate_hist_n,
234 &disable_warnings,
235 &disable_warning_prompt,
236 &recontest,
237 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400238
clang-format6c4d83e2016-08-08 19:03:30 -0700239static const arg_def_t usage =
240 ARG_DEF("u", "usage", 1, "Usage profile number to use");
241static const arg_def_t threads =
242 ARG_DEF("t", "threads", 1, "Max number of threads to use");
243static const arg_def_t profile =
244 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700245static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
246static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700247#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700248static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700249 { "mono", STEREO_FORMAT_MONO },
250 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
251 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
252 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
253 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
254 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700255};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700256static const arg_def_t stereo_mode = ARG_DEF_ENUM(
257 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700258#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700259static const arg_def_t timebase = ARG_DEF(
260 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700261static const arg_def_t error_resilient =
262 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
263static const arg_def_t lag_in_frames =
264 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400265
clang-format6c4d83e2016-08-08 19:03:30 -0700266static const arg_def_t *global_args[] = { &use_yv12,
267 &use_i420,
268 &use_i422,
269 &use_i444,
270 &use_i440,
271 &usage,
272 &threads,
273 &profile,
274 &width,
275 &height,
James Zerndba73762014-05-10 17:44:12 -0700276#if CONFIG_WEBM_IO
clang-format6c4d83e2016-08-08 19:03:30 -0700277 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700278#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700279 &timebase,
280 &framerate,
281 &error_resilient,
Yaowu Xuf883b422016-08-30 14:01:10 -0700282#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700283 &test16bitinternalarg,
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400284 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700285#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700286 &lag_in_frames,
287 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400288
clang-format6c4d83e2016-08-08 19:03:30 -0700289static const arg_def_t dropframe_thresh =
290 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
291static const arg_def_t resize_allowed =
292 ARG_DEF(NULL, "resize-allowed", 1, "Spatial resampling enabled (bool)");
293static const arg_def_t resize_width =
294 ARG_DEF(NULL, "resize-width", 1, "Width of encoded frame");
295static const arg_def_t resize_height =
296 ARG_DEF(NULL, "resize-height", 1, "Height of encoded frame");
297static const arg_def_t resize_up_thresh =
298 ARG_DEF(NULL, "resize-up", 1, "Upscale threshold (buf %)");
299static const arg_def_t resize_down_thresh =
300 ARG_DEF(NULL, "resize-down", 1, "Downscale threshold (buf %)");
Yaowu Xuf883b422016-08-30 14:01:10 -0700301static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
302 { "cbr", AOM_CBR },
303 { "cq", AOM_CQ },
304 { "q", AOM_Q },
clang-format6c4d83e2016-08-08 19:03:30 -0700305 { NULL, 0 } };
306static const arg_def_t end_usage =
307 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
308static const arg_def_t target_bitrate =
309 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
310static const arg_def_t min_quantizer =
311 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
312static const arg_def_t max_quantizer =
313 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
314static const arg_def_t undershoot_pct =
315 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
316static const arg_def_t overshoot_pct =
317 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
318static const arg_def_t buf_sz =
319 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
320static const arg_def_t buf_initial_sz =
321 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
322static const arg_def_t buf_optimal_sz =
323 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
John Koleszarc6b90392012-07-13 15:21:29 -0700324static const arg_def_t *rc_args[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700325 &dropframe_thresh, &resize_allowed, &resize_width, &resize_height,
326 &resize_up_thresh, &resize_down_thresh, &end_usage, &target_bitrate,
327 &min_quantizer, &max_quantizer, &undershoot_pct, &overshoot_pct,
328 &buf_sz, &buf_initial_sz, &buf_optimal_sz, NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -0400329};
330
clang-format6c4d83e2016-08-08 19:03:30 -0700331static const arg_def_t bias_pct =
332 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
333static const arg_def_t minsection_pct =
334 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
335static const arg_def_t maxsection_pct =
336 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
337static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
338 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400339
clang-format6c4d83e2016-08-08 19:03:30 -0700340static const arg_def_t kf_min_dist =
341 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
342static const arg_def_t kf_max_dist =
343 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
344static const arg_def_t kf_disabled =
345 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
346static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
347 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400348
clang-format6c4d83e2016-08-08 19:03:30 -0700349static const arg_def_t noise_sens =
350 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
351static const arg_def_t sharpness =
352 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
353static const arg_def_t static_thresh =
354 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
355static const arg_def_t auto_altref =
356 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
357static const arg_def_t arnr_maxframes =
358 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
359static const arg_def_t arnr_strength =
360 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500361static const struct arg_enum_list tuning_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700362 { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500363};
clang-format6c4d83e2016-08-08 19:03:30 -0700364static const arg_def_t tune_ssim =
365 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
366static const arg_def_t cq_level =
367 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
368static const arg_def_t max_intra_rate_pct =
369 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800370
Yaowu Xuf883b422016-08-30 14:01:10 -0700371#if CONFIG_AV1_ENCODER
372static const arg_def_t cpu_used_av1 =
clang-format6c4d83e2016-08-08 19:03:30 -0700373 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (-8..8)");
374static const arg_def_t tile_cols =
375 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
376static const arg_def_t tile_rows =
377 ARG_DEF(NULL, "tile-rows", 1,
378 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Ryan Lei7386eda2016-12-08 21:08:31 -0800379#if CONFIG_DEBLOCKING_ACROSS_TILES
380static const arg_def_t tile_loopfilter = ARG_DEF(
381 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
382#endif // CONFIG_DEBLOCKING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700383static const arg_def_t lossless =
384 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700385#if CONFIG_AOM_QM
386static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800387 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700388 "Enable quantisation matrices (0: false (default), 1: true)");
389static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800390 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700391static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800392 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700393#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000394#if CONFIG_TILE_GROUPS
Yaowu Xu859a5272016-11-10 15:32:21 -0800395static const arg_def_t num_tg = ARG_DEF(
396 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000397static const arg_def_t mtu_size =
398 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800399 "MTU size for a tile group, default is 0 (no MTU targeting), "
400 "overrides maximum number of tile groups");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000401#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800402#if CONFIG_TEMPMV_SIGNALING
403static const arg_def_t disable_tempmv = ARG_DEF(
404 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
405#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700406static const arg_def_t frame_parallel_decoding =
407 ARG_DEF(NULL, "frame-parallel", 1,
408 "Enable frame parallel decodability features "
409 "(0: false (default), 1: true)");
Thomase28d92b2016-09-20 12:07:11 +0100410#if CONFIG_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100411static const arg_def_t aq_mode = ARG_DEF(
412 NULL, "aq-mode", 1,
413 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100414 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200415#else
Thomase28d92b2016-09-20 12:07:11 +0100416static const arg_def_t aq_mode = ARG_DEF(
417 NULL, "aq-mode", 1,
418 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100419 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200420#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700421static const arg_def_t frame_periodic_boost =
422 ARG_DEF(NULL, "frame-boost", 1,
423 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700424static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
425 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700426static const arg_def_t max_inter_rate_pct =
427 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700428static const arg_def_t min_gf_interval = ARG_DEF(
429 NULL, "min-gf-interval", 1,
430 "min gf/arf frame interval (default 0, indicating in-built behavior)");
431static const arg_def_t max_gf_interval = ARG_DEF(
432 NULL, "max-gf-interval", 1,
433 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800434
Yaowu Xufc996362015-02-10 15:03:05 -0800435static const struct arg_enum_list color_space_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700436 { "unknown", AOM_CS_UNKNOWN },
437 { "bt601", AOM_CS_BT_601 },
438 { "bt709", AOM_CS_BT_709 },
439 { "smpte170", AOM_CS_SMPTE_170 },
440 { "smpte240", AOM_CS_SMPTE_240 },
441 { "bt2020", AOM_CS_BT_2020 },
442 { "reserved", AOM_CS_RESERVED },
443 { "sRGB", AOM_CS_SRGB },
Yaowu Xufc996362015-02-10 15:03:05 -0800444 { NULL, 0 }
445};
446
clang-format6c4d83e2016-08-08 19:03:30 -0700447static const arg_def_t input_color_space =
448 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
449 color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800450
Alex Converse219f6452014-08-06 11:01:18 -0700451static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700452 { "default", AOM_CONTENT_DEFAULT },
453 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700454 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700455};
456
457static const arg_def_t tune_content = ARG_DEF_ENUM(
458 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700459#endif
Alex Converse219f6452014-08-06 11:01:18 -0700460
Yaowu Xuf883b422016-08-30 14:01:10 -0700461#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000462#if CONFIG_EXT_PARTITION
463static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700464 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
465 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
466 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700467 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000468};
469static const arg_def_t superblock_size = ARG_DEF_ENUM(
470 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
471#endif // CONFIG_EXT_PARTITION
472
Yaowu Xuf883b422016-08-30 14:01:10 -0700473static const arg_def_t *av1_args[] = { &cpu_used_av1,
474 &auto_altref,
475 &sharpness,
476 &static_thresh,
477 &tile_cols,
478 &tile_rows,
Ryan Lei7386eda2016-12-08 21:08:31 -0800479#if CONFIG_DEBLOCKING_ACROSS_TILES
480 &tile_loopfilter,
481#endif // CONFIG_DEBLOCKING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700482 &arnr_maxframes,
483 &arnr_strength,
Yaowu Xuf883b422016-08-30 14:01:10 -0700484 &tune_ssim,
485 &cq_level,
486 &max_intra_rate_pct,
487 &max_inter_rate_pct,
488 &gf_cbr_boost_pct,
489 &lossless,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800490#if CONFIG_AOM_QM
491 &enable_qm,
492 &qm_min,
493 &qm_max,
494#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700495 &frame_parallel_decoding,
496 &aq_mode,
497 &frame_periodic_boost,
498 &noise_sens,
499 &tune_content,
500 &input_color_space,
501 &min_gf_interval,
502 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000503#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700504 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000505#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000506#if CONFIG_TILE_GROUPS
507 &num_tg,
508 &mtu_size,
509#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800510#if CONFIG_TEMPMV_SIGNALING
511 &disable_tempmv,
512#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700513#if CONFIG_AOM_HIGHBITDEPTH
514 &bitdeptharg,
515 &inbitdeptharg,
516#endif // CONFIG_AOM_HIGHBITDEPTH
517 NULL };
518static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
519 AOME_SET_ENABLEAUTOALTREF,
520 AOME_SET_SHARPNESS,
521 AOME_SET_STATIC_THRESHOLD,
522 AV1E_SET_TILE_COLUMNS,
523 AV1E_SET_TILE_ROWS,
Ryan Lei7386eda2016-12-08 21:08:31 -0800524#if CONFIG_DEBLOCKING_ACROSS_TILES
525 AV1E_SET_TILE_LOOPFILTER,
526#endif // CONFIG_DEBLOCKING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700527 AOME_SET_ARNR_MAXFRAMES,
528 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700529 AOME_SET_TUNING,
530 AOME_SET_CQ_LEVEL,
531 AOME_SET_MAX_INTRA_BITRATE_PCT,
532 AV1E_SET_MAX_INTER_BITRATE_PCT,
533 AV1E_SET_GF_CBR_BOOST_PCT,
534 AV1E_SET_LOSSLESS,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800535#if CONFIG_AOM_QM
536 AV1E_SET_ENABLE_QM,
537 AV1E_SET_QM_MIN,
538 AV1E_SET_QM_MAX,
539#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700540 AV1E_SET_FRAME_PARALLEL_DECODING,
541 AV1E_SET_AQ_MODE,
542 AV1E_SET_FRAME_PERIODIC_BOOST,
543 AV1E_SET_NOISE_SENSITIVITY,
544 AV1E_SET_TUNE_CONTENT,
545 AV1E_SET_COLOR_SPACE,
546 AV1E_SET_MIN_GF_INTERVAL,
547 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000548#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700549 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000550#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000551#if CONFIG_TILE_GROUPS
552 AV1E_SET_NUM_TG,
553 AV1E_SET_MTU,
554#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800555#if CONFIG_TEMPMV_SIGNALING
556 AV1E_SET_DISABLE_TEMPMV,
557#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700558 0 };
hui su82331e02015-08-16 18:21:56 -0700559#endif
560
John Koleszar0ea50ce2010-05-18 11:58:33 -0400561static const arg_def_t *no_args[] = { NULL };
562
James Zern59e7a472015-05-09 10:33:26 -0700563void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700564 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700565 const int num_encoder = get_aom_encoder_count();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400566
John Koleszarc6b90392012-07-13 15:21:29 -0700567 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
568 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400569
John Koleszarc6b90392012-07-13 15:21:29 -0700570 fprintf(stderr, "\nOptions:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700571 arg_show_usage(stderr, main_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700572 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700573 arg_show_usage(stderr, global_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700574 fprintf(stderr, "\nRate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700575 arg_show_usage(stderr, rc_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700576 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700577 arg_show_usage(stderr, rc_twopass_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700578 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700579 arg_show_usage(stderr, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700580#if CONFIG_AV1_ENCODER
581 fprintf(stderr, "\nAV1 Specific Options:\n");
582 arg_show_usage(stderr, av1_args);
hui su82331e02015-08-16 18:21:56 -0700583#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700584 fprintf(stderr,
585 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700586 " The desired precision of timestamps in the output, expressed\n"
587 " in fractional seconds. Default is 1/1000.\n");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800588 fprintf(stderr, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400589
Yaowu Xuf990b352015-06-01 09:13:59 -0700590 for (i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700591 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700592 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
593 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700594 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800595 }
Yaowu Xuf990b352015-06-01 09:13:59 -0700596 fprintf(stderr, "\n ");
597 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400598
John Koleszarc6b90392012-07-13 15:21:29 -0700599 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400600}
601
clang-format6c4d83e2016-08-08 19:03:30 -0700602#define mmin(a, b) ((a) < (b) ? (a) : (b))
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700603
Yaowu Xuf883b422016-08-30 14:01:10 -0700604#if CONFIG_AOM_HIGHBITDEPTH
605static void find_mismatch_high(const aom_image_t *const img1,
606 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700607 int uloc[4], int vloc[4]) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700608 uint16_t *plane1, *plane2;
609 uint32_t stride1, stride2;
610 const uint32_t bsize = 64;
611 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
612 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
613 const uint32_t c_w =
614 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
615 const uint32_t c_h =
616 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
617 int match = 1;
618 uint32_t i, j;
619 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700620 plane1 = (uint16_t *)img1->planes[AOM_PLANE_Y];
621 plane2 = (uint16_t *)img2->planes[AOM_PLANE_Y];
622 stride1 = img1->stride[AOM_PLANE_Y] / 2;
623 stride2 = img2->stride[AOM_PLANE_Y] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700624 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
625 for (j = 0; match && j < img1->d_w; j += bsize) {
626 int k, l;
627 const int si = mmin(i + bsize, img1->d_h) - i;
628 const int sj = mmin(j + bsize, img1->d_w) - j;
629 for (k = 0; match && k < si; ++k) {
630 for (l = 0; match && l < sj; ++l) {
631 if (*(plane1 + (i + k) * stride1 + j + l) !=
632 *(plane2 + (i + k) * stride2 + j + l)) {
633 yloc[0] = i + k;
634 yloc[1] = j + l;
635 yloc[2] = *(plane1 + (i + k) * stride1 + j + l);
636 yloc[3] = *(plane2 + (i + k) * stride2 + j + l);
637 match = 0;
638 break;
639 }
640 }
641 }
642 }
643 }
644
645 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700646 plane1 = (uint16_t *)img1->planes[AOM_PLANE_U];
647 plane2 = (uint16_t *)img2->planes[AOM_PLANE_U];
648 stride1 = img1->stride[AOM_PLANE_U] / 2;
649 stride2 = img2->stride[AOM_PLANE_U] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700650 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
651 for (j = 0; match && j < c_w; j += bsizex) {
652 int k, l;
653 const int si = mmin(i + bsizey, c_h - i);
654 const int sj = mmin(j + bsizex, c_w - j);
655 for (k = 0; match && k < si; ++k) {
656 for (l = 0; match && l < sj; ++l) {
657 if (*(plane1 + (i + k) * stride1 + j + l) !=
658 *(plane2 + (i + k) * stride2 + j + l)) {
659 uloc[0] = i + k;
660 uloc[1] = j + l;
661 uloc[2] = *(plane1 + (i + k) * stride1 + j + l);
662 uloc[3] = *(plane2 + (i + k) * stride2 + j + l);
663 match = 0;
664 break;
665 }
666 }
667 }
668 }
669 }
670
671 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700672 plane1 = (uint16_t *)img1->planes[AOM_PLANE_V];
673 plane2 = (uint16_t *)img2->planes[AOM_PLANE_V];
674 stride1 = img1->stride[AOM_PLANE_V] / 2;
675 stride2 = img2->stride[AOM_PLANE_V] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700676 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
677 for (j = 0; match && j < c_w; j += bsizex) {
678 int k, l;
679 const int si = mmin(i + bsizey, c_h - i);
680 const int sj = mmin(j + bsizex, c_w - j);
681 for (k = 0; match && k < si; ++k) {
682 for (l = 0; match && l < sj; ++l) {
683 if (*(plane1 + (i + k) * stride1 + j + l) !=
684 *(plane2 + (i + k) * stride2 + j + l)) {
685 vloc[0] = i + k;
686 vloc[1] = j + l;
687 vloc[2] = *(plane1 + (i + k) * stride1 + j + l);
688 vloc[3] = *(plane2 + (i + k) * stride2 + j + l);
689 match = 0;
690 break;
691 }
692 }
693 }
694 }
695 }
696}
697#endif
698
Yaowu Xuf883b422016-08-30 14:01:10 -0700699static void find_mismatch(const aom_image_t *const img1,
700 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700701 int uloc[4], int vloc[4]) {
James Zern6253e8e2014-01-31 16:10:43 -0800702 const uint32_t bsize = 64;
703 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
704 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
705 const uint32_t c_w =
706 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
707 const uint32_t c_h =
708 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
709 int match = 1;
710 uint32_t i, j;
Deb Mukherjee23144d22013-03-12 14:21:08 -0700711 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -0800712 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
713 for (j = 0; match && j < img1->d_w; j += bsize) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800714 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800715 const int si = mmin(i + bsize, img1->d_h) - i;
716 const int sj = mmin(j + bsize, img1->d_w) - j;
717 for (k = 0; match && k < si; ++k) {
718 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700719 if (*(img1->planes[AOM_PLANE_Y] +
720 (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
721 *(img2->planes[AOM_PLANE_Y] +
722 (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800723 yloc[0] = i + k;
724 yloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700725 yloc[2] = *(img1->planes[AOM_PLANE_Y] +
726 (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
727 yloc[3] = *(img2->planes[AOM_PLANE_Y] +
728 (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800729 match = 0;
730 break;
731 }
732 }
James Zernd04112d2014-01-31 15:49:17 -0800733 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800734 }
735 }
Jingning Han12bf0792013-04-06 10:00:53 -0700736
Deb Mukherjee23144d22013-03-12 14:21:08 -0700737 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700738 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700739 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800740 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800741 const int si = mmin(i + bsizey, c_h - i);
742 const int sj = mmin(j + bsizex, c_w - j);
743 for (k = 0; match && k < si; ++k) {
744 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700745 if (*(img1->planes[AOM_PLANE_U] +
746 (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
747 *(img2->planes[AOM_PLANE_U] +
748 (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800749 uloc[0] = i + k;
750 uloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 uloc[2] = *(img1->planes[AOM_PLANE_U] +
752 (i + k) * img1->stride[AOM_PLANE_U] + j + l);
753 uloc[3] = *(img2->planes[AOM_PLANE_U] +
754 (i + k) * img2->stride[AOM_PLANE_U] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800755 match = 0;
756 break;
757 }
758 }
James Zernd04112d2014-01-31 15:49:17 -0800759 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800760 }
761 }
Deb Mukherjee23144d22013-03-12 14:21:08 -0700762 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700763 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700764 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800765 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800766 const int si = mmin(i + bsizey, c_h - i);
767 const int sj = mmin(j + bsizex, c_w - j);
768 for (k = 0; match && k < si; ++k) {
769 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 if (*(img1->planes[AOM_PLANE_V] +
771 (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
772 *(img2->planes[AOM_PLANE_V] +
773 (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800774 vloc[0] = i + k;
775 vloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700776 vloc[2] = *(img1->planes[AOM_PLANE_V] +
777 (i + k) * img1->stride[AOM_PLANE_V] + j + l);
778 vloc[3] = *(img2->planes[AOM_PLANE_V] +
779 (i + k) * img2->stride[AOM_PLANE_V] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800780 match = 0;
781 break;
782 }
783 }
James Zernd04112d2014-01-31 15:49:17 -0800784 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800785 }
786 }
787}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400788
Yaowu Xuf883b422016-08-30 14:01:10 -0700789static int compare_img(const aom_image_t *const img1,
790 const aom_image_t *const img2) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700791 uint32_t l_w = img1->d_w;
clang-format6c4d83e2016-08-08 19:03:30 -0700792 uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
James Zern6253e8e2014-01-31 16:10:43 -0800793 const uint32_t c_h =
794 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
795 uint32_t i;
John Koleszarc6b90392012-07-13 15:21:29 -0700796 int match = 1;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800797
John Koleszarc6b90392012-07-13 15:21:29 -0700798 match &= (img1->fmt == img2->fmt);
hkuang3671e622014-01-22 14:58:59 -0800799 match &= (img1->d_w == img2->d_w);
800 match &= (img1->d_h == img2->d_h);
Yaowu Xuf883b422016-08-30 14:01:10 -0700801#if CONFIG_AOM_HIGHBITDEPTH
802 if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700803 l_w *= 2;
804 c_w *= 2;
805 }
806#endif
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800807
James Zernd04112d2014-01-31 15:49:17 -0800808 for (i = 0; i < img1->d_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700809 match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
810 img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700811 l_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800812
James Zernd04112d2014-01-31 15:49:17 -0800813 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700814 match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
815 img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
John Koleszarbeae5012013-05-08 16:07:38 -0700816 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800817
James Zernd04112d2014-01-31 15:49:17 -0800818 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700819 match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
820 img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
John Koleszarbeae5012013-05-08 16:07:38 -0700821 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800822
John Koleszarc6b90392012-07-13 15:21:29 -0700823 return match;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800824}
825
clang-format6c4d83e2016-08-08 19:03:30 -0700826#define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
Yaowu Xuf883b422016-08-30 14:01:10 -0700827#if CONFIG_AV1_ENCODER
828#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800829#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800830
James Zerndba73762014-05-10 17:44:12 -0700831#if !CONFIG_WEBM_IO
832typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700833struct WebmOutputContext {
834 int debug;
835};
James Zerndba73762014-05-10 17:44:12 -0700836#endif
837
John Koleszar9e50ed72012-02-15 12:39:38 -0800838/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800839struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700840 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700841 const char *out_fn;
842 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700843#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700844 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700845#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700846 stereo_format_t stereo_fmt;
847 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
848 int arg_ctrl_cnt;
849 int write_webm;
Yaowu Xuf883b422016-08-30 14:01:10 -0700850#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700851 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700852 int use_16bit_internal;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700853#endif
John Koleszar9e50ed72012-02-15 12:39:38 -0800854};
855
John Koleszar6ad3b742012-11-06 12:02:42 -0800856struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700857 int index;
858 struct stream_state *next;
859 struct stream_config config;
860 FILE *file;
861 struct rate_hist *rate_hist;
862 struct WebmOutputContext webm_ctx;
863 uint64_t psnr_sse_total;
864 uint64_t psnr_samples_total;
865 double psnr_totals[4];
866 int psnr_count;
867 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700868 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700869 unsigned int frames_out;
870 uint64_t cx_time;
871 size_t nbytes;
872 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700873#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700874 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700875#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700876 struct aom_image *img;
877 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700878 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800879};
880
clang-format6c4d83e2016-08-08 19:03:30 -0700881static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700882 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800883 if (rat->den < 0) {
884 rat->num *= -1;
885 rat->den *= -1;
886 }
John Koleszar1b27e932012-04-25 17:08:56 -0700887
clang-format6c4d83e2016-08-08 19:03:30 -0700888 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700889
clang-format6c4d83e2016-08-08 19:03:30 -0700890 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700891}
892
Yaowu Xuf883b422016-08-30 14:01:10 -0700893static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700894 char **argi, **argj;
895 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700896 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700897
clang-format6c4d83e2016-08-08 19:03:30 -0700898 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800899
John Koleszar6ad3b742012-11-06 12:02:42 -0800900 /* Initialize default parameters */
901 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700902 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700903 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700904 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700905 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700906 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400907
John Koleszarc6b90392012-07-13 15:21:29 -0700908 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
909 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400910
John Koleszarc6b90392012-07-13 15:21:29 -0700911 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700912 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800913 if (!global->codec)
914 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700915 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800916 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400917
John Koleszar6ad3b742012-11-06 12:02:42 -0800918 if (global->passes < 1 || global->passes > 2)
919 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700920 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800921 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400922
John Koleszar6ad3b742012-11-06 12:02:42 -0800923 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700924 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800925 } else if (arg_match(&arg, &usage, argi))
926 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700927 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800928 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700929 else if (arg_match(&arg, &best_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700930 global->deadline = AOM_DL_BEST_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -0700931 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700932 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -0700933 else if (arg_match(&arg, &rt_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700934 global->deadline = AOM_DL_REALTIME;
John Koleszar6ad3b742012-11-06 12:02:42 -0800935 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700936 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800937 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700938 global->color_type = I420;
939 else if (arg_match(&arg, &use_i422, argi))
940 global->color_type = I422;
941 else if (arg_match(&arg, &use_i444, argi))
942 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700943 else if (arg_match(&arg, &use_i440, argi))
944 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800945 else if (arg_match(&arg, &quietarg, argi))
946 global->quiet = 1;
947 else if (arg_match(&arg, &verbosearg, argi))
948 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700949 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800950 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700951 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800952 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700953 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800954 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700955 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800956 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700957 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800958 global->framerate = arg_parse_rational(&arg);
959 validate_positive_rational(arg.name, &global->framerate);
960 global->have_framerate = 1;
961 } else if (arg_match(&arg, &out_part, argi))
962 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700963 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800964 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700965 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800966 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700967 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800968 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800969 else if (arg_match(&arg, &disable_warnings, argi))
970 global->disable_warnings = 1;
971 else if (arg_match(&arg, &disable_warning_prompt, argi))
972 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800973 else
John Koleszarc6b90392012-07-13 15:21:29 -0700974 argj++;
975 }
976
John Koleszar6ad3b742012-11-06 12:02:42 -0800977 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700978 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800979 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700980 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
981 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800982 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800983 }
John Koleszarc6b90392012-07-13 15:21:29 -0700984 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800985 /* Validate global config */
986 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700987#if CONFIG_AV1_ENCODER
988 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800989 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700990 if (global->codec != NULL && global->codec->name != NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -0700991 global->passes = (strcmp(global->codec->name, "av1") == 0 &&
992 global->deadline != AOM_DL_REALTIME)
clang-format6c4d83e2016-08-08 19:03:30 -0700993 ? 2
994 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800995#else
996 global->passes = 1;
997#endif
998 }
999
Yaowu Xuf883b422016-08-30 14:01:10 -07001000 if (global->deadline == AOM_DL_REALTIME && global->passes > 1) {
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001001 warn("Enforcing one-pass encoding in realtime mode\n");
1002 global->passes = 1;
1003 }
John Koleszar732cb9a2012-02-14 12:30:17 -08001004}
1005
Yaowu Xuf883b422016-08-30 14:01:10 -07001006static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001007 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -07001008 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
1009 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -08001010
clang-format6c4d83e2016-08-08 19:03:30 -07001011 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -08001012
John Koleszar25b6e9f2013-02-12 21:17:56 -08001013 if (!fseeko(input->file, 0, SEEK_END)) {
1014 /* Input file is seekable. Figure out how long it is, so we can get
1015 * progress info.
1016 */
1017 input->length = ftello(input->file);
1018 rewind(input->file);
1019 }
1020
Frank Galligan09acd262015-06-01 10:20:58 -07001021 /* Default to 1:1 pixel aspect ratio. */
1022 input->pixel_aspect_ratio.numerator = 1;
1023 input->pixel_aspect_ratio.denominator = 1;
1024
John Koleszar6ad3b742012-11-06 12:02:42 -08001025 /* For RAW input sources, these bytes will applied on the first frame
1026 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -07001027 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001028 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1029 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001030
clang-format6c4d83e2016-08-08 19:03:30 -07001031 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -07001032 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
1033 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001034 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001035 input->width = input->y4m.pic_w;
1036 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -07001037 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1038 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001039 input->framerate.numerator = input->y4m.fps_n;
1040 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -07001041 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001042 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -08001043 } else
1044 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -08001045 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -08001046 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -08001047 } else {
1048 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001049 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001050}
1051
Yaowu Xuf883b422016-08-30 14:01:10 -07001052static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001053 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -07001054 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001055}
1056
Yaowu Xuf883b422016-08-30 14:01:10 -07001057static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -08001058 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001059 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001060
John Koleszar6ad3b742012-11-06 12:02:42 -08001061 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001062 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001063 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001064 }
1065
John Koleszar6ad3b742012-11-06 12:02:42 -08001066 if (prev) {
1067 memcpy(stream, prev, sizeof(*stream));
1068 stream->index++;
1069 prev->next = stream;
1070 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001071 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001072
John Koleszar6ad3b742012-11-06 12:02:42 -08001073 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -07001074 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -07001075 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -07001076 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001077
John Koleszar6ad3b742012-11-06 12:02:42 -08001078 /* Change the default timebase to a high enough value so that the
1079 * encoder will always create strictly increasing timestamps.
1080 */
1081 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001082
John Koleszar6ad3b742012-11-06 12:02:42 -08001083 /* Never use the library's default resolution, require it be parsed
1084 * from the file or set on the command line.
1085 */
1086 stream->config.cfg.g_w = 0;
1087 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001088
John Koleszar6ad3b742012-11-06 12:02:42 -08001089 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -08001090 stream->config.write_webm = 1;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001091#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -07001092 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001093 stream->webm_ctx.last_pts_ns = -1;
1094 stream->webm_ctx.writer = NULL;
1095 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001096#endif
Johann87c40b32012-03-01 16:12:53 -08001097
John Koleszar6ad3b742012-11-06 12:02:42 -08001098 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001099 stream->webm_ctx.debug = global->debug;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001100
1101 /* Default lag_in_frames is 0 in realtime mode */
Yaowu Xuf883b422016-08-30 14:01:10 -07001102 if (global->deadline == AOM_DL_REALTIME)
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001103 stream->config.cfg.g_lag_in_frames = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001104 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001105
John Koleszar6ad3b742012-11-06 12:02:42 -08001106 /* Output files must be specified for each stream */
1107 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001108
John Koleszar6ad3b742012-11-06 12:02:42 -08001109 stream->next = NULL;
1110 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001111}
1112
Yaowu Xuf883b422016-08-30 14:01:10 -07001113static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001114 struct stream_state *stream, char **argv) {
1115 char **argi, **argj;
1116 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001117 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001118 static const int *ctrl_args_map = NULL;
1119 struct stream_config *config = &stream->config;
1120 int eos_mark_found = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001121#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -07001122 int test_16bit_internal = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001123#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001124
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001125 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001126 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001127#if CONFIG_AV1_ENCODER
1128 } else if (strcmp(global->codec->name, "av1") == 0) {
1129 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1130 // Consider to expand this set for AV1 encoder control.
1131 ctrl_args = av1_args;
1132 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001133#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001134 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001135
John Koleszarc6b90392012-07-13 15:21:29 -07001136 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001137 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001138
John Koleszar6ad3b742012-11-06 12:02:42 -08001139 /* Once we've found an end-of-stream marker (--) we want to continue
1140 * shifting arguments but not consuming them.
1141 */
1142 if (eos_mark_found) {
1143 argj++;
1144 continue;
1145 } else if (!strcmp(*argj, "--")) {
1146 eos_mark_found = 1;
1147 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001148 }
1149
Adrian Granged2401802015-02-13 14:51:32 -08001150 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001151 config->out_fn = arg.val;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001152 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001153 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001154#if CONFIG_FP_MB_STATS
1155 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1156 config->fpmb_stats_fn = arg.val;
1157#endif
Johannb50e5182015-01-30 15:05:14 -08001158 } else if (arg_match(&arg, &use_webm, argi)) {
1159#if CONFIG_WEBM_IO
1160 config->write_webm = 1;
1161#else
1162 die("Error: --webm specified but webm is disabled.");
1163#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001164 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001165 config->write_webm = 0;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001166 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001167 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001168 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001169 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001170 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001171 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001172 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001173 config->cfg.g_h = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -07001174#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001175 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1176 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1177 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1178 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1179#endif
James Zerndba73762014-05-10 17:44:12 -07001180#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001181 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001182 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001183#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001184 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001185 config->cfg.g_timebase = arg_parse_rational(&arg);
1186 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001187 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001188 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001189 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001190 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -07001191 if (global->deadline == AOM_DL_REALTIME &&
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001192 config->cfg.g_lag_in_frames != 0) {
1193 warn("non-zero %s option ignored in realtime mode.\n", arg.name);
1194 config->cfg.g_lag_in_frames = 0;
1195 }
1196 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001197 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001198 } else if (arg_match(&arg, &resize_allowed, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001199 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001200 } else if (arg_match(&arg, &resize_width, argi)) {
1201 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
1202 } else if (arg_match(&arg, &resize_height, argi)) {
1203 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001204 } else if (arg_match(&arg, &resize_up_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001205 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001206 } else if (arg_match(&arg, &resize_down_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001207 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001208 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001209 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001210 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001211 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001212 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001213 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001214 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001215 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001216 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001217 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001218 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001219 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001220 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001221 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001222 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001223 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001224 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001225 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001226 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001227 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001228 if (global->passes < 2)
1229 warn("option %s ignored in one-pass mode.\n", arg.name);
1230 } else if (arg_match(&arg, &minsection_pct, argi)) {
1231 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1232
1233 if (global->passes < 2)
1234 warn("option %s ignored in one-pass mode.\n", arg.name);
1235 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1236 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1237
1238 if (global->passes < 2)
1239 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001240 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001241 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001242 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001243 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001244 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001245 config->cfg.kf_mode = AOM_KF_DISABLED;
1246#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001247 } else if (arg_match(&arg, &test16bitinternalarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001248 if (strcmp(global->codec->name, "av1") == 0 ||
1249 strcmp(global->codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001250 test_16bit_internal = 1;
1251 }
1252#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001253 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001254 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001255 for (i = 0; ctrl_args[i]; i++) {
1256 if (arg_match(&arg, ctrl_args[i], argi)) {
1257 int j;
1258 match = 1;
1259
1260 /* Point either to the next free element or the first
1261 * instance of this control.
1262 */
1263 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001264 if (ctrl_args_map != NULL &&
1265 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001266 break;
1267
1268 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001269 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001270 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001271 config->arg_ctrls[j][0] = ctrl_args_map[i];
1272 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001273 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001274 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001275 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001276 }
clang-format6c4d83e2016-08-08 19:03:30 -07001277 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001278 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001279 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001280#if CONFIG_AOM_HIGHBITDEPTH
1281 if (strcmp(global->codec->name, "av1") == 0 ||
1282 strcmp(global->codec->name, "av1") == 0) {
clang-format6c4d83e2016-08-08 19:03:30 -07001283 config->use_16bit_internal =
1284 test_16bit_internal | (config->cfg.g_profile > 1);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001285 }
1286#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001287 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001288}
1289
clang-format6c4d83e2016-08-08 19:03:30 -07001290#define FOREACH_STREAM(func) \
1291 do { \
1292 struct stream_state *stream; \
Tom Finegan249366b2013-11-25 12:05:19 -08001293 for (stream = streams; stream; stream = stream->next) { \
clang-format6c4d83e2016-08-08 19:03:30 -07001294 func; \
1295 } \
Tom Finegan44dd3272013-11-20 17:18:28 -08001296 } while (0)
John Koleszar9e50ed72012-02-15 12:39:38 -08001297
Alex Conversed66bd222014-02-21 10:52:09 -08001298static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001299 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001300 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001301 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001302
John Koleszar6ad3b742012-11-06 12:02:42 -08001303 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001304 fatal(
1305 "Stream %d: Specify stream dimensions with --width (-w) "
1306 " and --height (-h)",
1307 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001308
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001309 // Check that the codec bit depth is greater than the input bit depth.
1310 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001311 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001312 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1313 stream->index, (int)stream->config.cfg.g_bit_depth,
1314 stream->config.cfg.g_input_bit_depth);
1315 }
1316
John Koleszar6ad3b742012-11-06 12:02:42 -08001317 for (streami = stream; streami; streami = streami->next) {
1318 /* All streams require output files */
1319 if (!streami->config.out_fn)
1320 fatal("Stream %d: Output file is required (specify with -o)",
1321 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001322
John Koleszar6ad3b742012-11-06 12:02:42 -08001323 /* Check for two streams outputting to the same file */
1324 if (streami != stream) {
1325 const char *a = stream->config.out_fn;
1326 const char *b = streami->config.out_fn;
1327 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1328 fatal("Stream %d: duplicate output file (from stream %d)",
1329 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001330 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001331
1332 /* Check for two streams sharing a stats file. */
1333 if (streami != stream) {
1334 const char *a = stream->config.stats_fn;
1335 const char *b = streami->config.stats_fn;
1336 if (a && b && !strcmp(a, b))
1337 fatal("Stream %d: duplicate stats file (from stream %d)",
1338 streami->index, stream->index);
1339 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001340
1341#if CONFIG_FP_MB_STATS
1342 /* Check for two streams sharing a mb stats file. */
1343 if (streami != stream) {
1344 const char *a = stream->config.fpmb_stats_fn;
1345 const char *b = streami->config.fpmb_stats_fn;
1346 if (a && b && !strcmp(a, b))
1347 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1348 streami->index, stream->index);
1349 }
1350#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001351 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001352}
1353
clang-format6c4d83e2016-08-08 19:03:30 -07001354static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001355 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001356 if (!stream->config.cfg.g_w) {
1357 if (!stream->config.cfg.g_h)
1358 stream->config.cfg.g_w = w;
1359 else
1360 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1361 }
1362 if (!stream->config.cfg.g_h) {
1363 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1364 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001365}
1366
clang-format6c4d83e2016-08-08 19:03:30 -07001367static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001368 switch (t) {
1369 case FILE_TYPE_RAW: return "RAW";
1370 case FILE_TYPE_Y4M: return "Y4M";
1371 default: return "Other";
1372 }
1373}
1374
Yaowu Xuf883b422016-08-30 14:01:10 -07001375static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001376 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001377 case AOM_IMG_FMT_I420: return "I420";
1378 case AOM_IMG_FMT_I422: return "I422";
1379 case AOM_IMG_FMT_I444: return "I444";
1380 case AOM_IMG_FMT_I440: return "I440";
1381 case AOM_IMG_FMT_YV12: return "YV12";
1382 case AOM_IMG_FMT_I42016: return "I42016";
1383 case AOM_IMG_FMT_I42216: return "I42216";
1384 case AOM_IMG_FMT_I44416: return "I44416";
1385 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001386 default: return "Other";
1387 }
1388}
Ralph Giles061a16d2012-01-05 15:05:05 -06001389
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001390static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001391 struct AvxEncoderConfig *global,
1392 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001393#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001394 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001395
John Koleszar6ad3b742012-11-06 12:02:42 -08001396 if (stream->index == 0) {
1397 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001398 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001399 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001400 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001401 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001402 }
1403 if (stream->next || stream->index)
1404 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1405 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1406 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001407
John Koleszar6ad3b742012-11-06 12:02:42 -08001408 SHOW(g_usage);
1409 SHOW(g_threads);
1410 SHOW(g_profile);
1411 SHOW(g_w);
1412 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001413 SHOW(g_bit_depth);
1414 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001415 SHOW(g_timebase.num);
1416 SHOW(g_timebase.den);
1417 SHOW(g_error_resilient);
1418 SHOW(g_pass);
1419 SHOW(g_lag_in_frames);
1420 SHOW(rc_dropframe_thresh);
1421 SHOW(rc_resize_allowed);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001422 SHOW(rc_scaled_width);
1423 SHOW(rc_scaled_height);
John Koleszar6ad3b742012-11-06 12:02:42 -08001424 SHOW(rc_resize_up_thresh);
1425 SHOW(rc_resize_down_thresh);
1426 SHOW(rc_end_usage);
1427 SHOW(rc_target_bitrate);
1428 SHOW(rc_min_quantizer);
1429 SHOW(rc_max_quantizer);
1430 SHOW(rc_undershoot_pct);
1431 SHOW(rc_overshoot_pct);
1432 SHOW(rc_buf_sz);
1433 SHOW(rc_buf_initial_sz);
1434 SHOW(rc_buf_optimal_sz);
1435 SHOW(rc_2pass_vbr_bias_pct);
1436 SHOW(rc_2pass_vbr_minsection_pct);
1437 SHOW(rc_2pass_vbr_maxsection_pct);
1438 SHOW(kf_mode);
1439 SHOW(kf_min_dist);
1440 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001441}
1442
John Koleszar9e50ed72012-02-15 12:39:38 -08001443static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001444 struct AvxEncoderConfig *global,
1445 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001446 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001447 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001448
Yaowu Xuf883b422016-08-30 14:01:10 -07001449 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001450
John Koleszar6ad3b742012-11-06 12:02:42 -08001451 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001452
clang-format6c4d83e2016-08-08 19:03:30 -07001453 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001454
John Koleszar6ad3b742012-11-06 12:02:42 -08001455 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1456 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001457
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001458#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001459 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001460 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001461 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1462 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001463 }
James Zernc8e5a772016-02-11 18:53:50 -08001464#else
1465 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001466#endif
1467
1468 if (!stream->config.write_webm) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001469 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1470 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001471}
1472
John Koleszar9e50ed72012-02-15 12:39:38 -08001473static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001474 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001475 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001476
Yaowu Xuf883b422016-08-30 14:01:10 -07001477 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001478
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001479#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001480 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001481 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001482 }
1483#endif
1484
1485 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001486 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001487 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001488 stream->frames_out);
1489 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001490
John Koleszar6ad3b742012-11-06 12:02:42 -08001491 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001492}
1493
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001494static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001495 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001496 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001497 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001498 fatal("Failed to open statistics store");
1499 } else {
1500 if (!stats_open_mem(&stream->stats, pass))
1501 fatal("Failed to open statistics store");
1502 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001503
Pengchong Jinf349b072014-07-14 09:13:38 -07001504#if CONFIG_FP_MB_STATS
1505 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001506 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1507 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001508 fatal("Failed to open mb statistics store");
1509 } else {
1510 if (!stats_open_mem(&stream->fpmb_stats, pass))
1511 fatal("Failed to open mb statistics store");
1512 }
1513#endif
1514
John Koleszar6ad3b742012-11-06 12:02:42 -08001515 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001516 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1517 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001518 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001519 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001520#if CONFIG_FP_MB_STATS
1521 stream->config.cfg.rc_firstpass_mb_stats_in =
1522 stats_get(&stream->fpmb_stats);
1523#endif
1524 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001525
John Koleszar6ad3b742012-11-06 12:02:42 -08001526 stream->cx_time = 0;
1527 stream->nbytes = 0;
1528 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001529}
1530
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001531static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001532 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001533 int i;
1534 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001535
Yaowu Xuf883b422016-08-30 14:01:10 -07001536 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1537 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
1538#if CONFIG_AOM_HIGHBITDEPTH
1539 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001540#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001541
John Koleszar6ad3b742012-11-06 12:02:42 -08001542 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001543 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001544 &stream->config.cfg, flags);
1545 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001546
Yaowu Xuf883b422016-08-30 14:01:10 -07001547 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001548 * we're being clever to store the control IDs in an array. Real
1549 * applications will want to make use of the enumerations directly
1550 */
1551 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1552 int ctrl = stream->config.arg_ctrls[i][0];
1553 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001554 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001555 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001556
John Koleszar6ad3b742012-11-06 12:02:42 -08001557 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1558 }
1559
John Koleszar5ebe94f2012-12-23 07:20:10 -08001560#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001561 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001562 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1563 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
1564 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001565
Yaowu Xuf883b422016-08-30 14:01:10 -07001566#if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
1567 if (strcmp(global->codec->name, "av1") == 0) {
1568 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001569 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1570
Yaowu Xuf883b422016-08-30 14:01:10 -07001571 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001572 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1573 }
1574#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001575 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001576#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001577}
1578
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001579static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001580 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001581 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001582 aom_codec_pts_t frame_start, next_frame_start;
1583 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1584 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001585
clang-format6c4d83e2016-08-08 19:03:30 -07001586 frame_start =
1587 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1588 cfg->g_timebase.num / global->framerate.num;
1589 next_frame_start =
1590 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1591 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001592
clang-format6c4d83e2016-08-08 19:03:30 -07001593/* Scale if necessary */
Yaowu Xuf883b422016-08-30 14:01:10 -07001594#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001595 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001596 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001597 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001598 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001599 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1600 exit(EXIT_FAILURE);
1601 }
1602#if CONFIG_LIBYUV
1603 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001604 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001605 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001606 }
clang-format6c4d83e2016-08-08 19:03:30 -07001607 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001608 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1609 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1610 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1611 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1612 stream->img->stride[AOM_PLANE_Y] / 2,
1613 (uint16 *)stream->img->planes[AOM_PLANE_U],
1614 stream->img->stride[AOM_PLANE_U] / 2,
1615 (uint16 *)stream->img->planes[AOM_PLANE_V],
1616 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001617 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001618 img = stream->img;
1619#else
clang-format6c4d83e2016-08-08 19:03:30 -07001620 stream->encoder.err = 1;
1621 ctx_exit_on_error(&stream->encoder,
1622 "Stream %d: Failed to encode frame.\n"
1623 "Scaling disabled in this configuration. \n"
1624 "To enable, configure with --enable-libyuv\n",
1625 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001626#endif
1627 }
1628 }
1629#endif
John Koleszar34882b92012-03-01 12:50:40 -08001630 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001631 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001632 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1633 exit(EXIT_FAILURE);
1634 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001635#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001636 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001637 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001638 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001639 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001640 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1641 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1642 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1643 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1644 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1645 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001646 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001647 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001648#else
1649 stream->encoder.err = 1;
1650 ctx_exit_on_error(&stream->encoder,
1651 "Stream %d: Failed to encode frame.\n"
1652 "Scaling disabled in this configuration. \n"
1653 "To enable, configure with --enable-libyuv\n",
1654 stream->index);
1655#endif
John Koleszar34882b92012-03-01 12:50:40 -08001656 }
1657
Yaowu Xuf883b422016-08-30 14:01:10 -07001658 aom_usec_timer_start(&timer);
1659 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001660 (unsigned long)(next_frame_start - frame_start), 0,
1661 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001662 aom_usec_timer_mark(&timer);
1663 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001664 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1665 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001666}
1667
John Koleszar6ad3b742012-11-06 12:02:42 -08001668static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001669 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001670 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001671
Yaowu Xuf883b422016-08-30 14:01:10 -07001672 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001673 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1674 stream->counts[q]++;
1675 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001676}
1677
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001678static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001679 struct AvxEncoderConfig *global, int *got_data) {
1680 const aom_codec_cx_pkt_t *pkt;
1681 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1682 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001683
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001684 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001685 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001686 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001687 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001688
John Koleszar6ad3b742012-11-06 12:02:42 -08001689 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001690 case AOM_CODEC_CX_FRAME_PKT:
1691 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001692 stream->frames_out++;
1693 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001694 if (!global->quiet)
1695 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001696
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001697 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001698#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001699 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001700 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001701 }
1702#endif
1703 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001704 if (pkt->data.frame.partition_id <= 0) {
1705 ivf_header_pos = ftello(stream->file);
1706 fsize = pkt->data.frame.sz;
1707
Dmitry Kovalev373b0f92014-01-29 17:57:21 -08001708 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001709 } else {
1710 fsize += pkt->data.frame.sz;
1711
Yaowu Xuf883b422016-08-30 14:01:10 -07001712 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
James Zernb6430362017-01-14 14:18:01 -08001713 const FileOffset currpos = ftello(stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001714 fseeko(stream->file, ivf_header_pos, SEEK_SET);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001715 ivf_write_frame_size(stream->file, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001716 fseeko(stream->file, currpos, SEEK_SET);
1717 }
1718 }
1719
clang-format6c4d83e2016-08-08 19:03:30 -07001720 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1721 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001722 }
1723 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001724
John Koleszar9e50ed72012-02-15 12:39:38 -08001725 *got_data = 1;
John Koleszar5ebe94f2012-12-23 07:20:10 -08001726#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001727 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001728 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001729 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001730 if (stream->decoder.err) {
1731 warn_or_exit_on_error(&stream->decoder,
1732 global->test_decode == TEST_DECODE_FATAL,
1733 "Failed to decode frame %d in stream %d",
1734 stream->frames_out + 1, stream->index);
1735 stream->mismatch_seen = stream->frames_out + 1;
1736 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001737 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001738#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001739 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001740 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001741 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001742 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001743 pkt->data.twopass_stats.sz);
1744 stream->nbytes += pkt->data.raw.sz;
1745 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001746#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001747 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001748 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001749 pkt->data.firstpass_mb_stats.sz);
1750 stream->nbytes += pkt->data.raw.sz;
1751 break;
1752#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001753 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001754
1755 if (global->show_psnr) {
1756 int i;
1757
1758 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1759 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1760 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001761 if (!global->quiet)
1762 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001763 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1764 }
1765 stream->psnr_count++;
1766 }
1767
1768 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001769 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001770 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001771 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001772}
1773
clang-format6c4d83e2016-08-08 19:03:30 -07001774static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001775 int i;
1776 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001777
clang-format6c4d83e2016-08-08 19:03:30 -07001778 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001779
John Koleszar6ad3b742012-11-06 12:02:42 -08001780 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001781 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001782 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001783 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001784
John Koleszar6ad3b742012-11-06 12:02:42 -08001785 for (i = 0; i < 4; i++) {
1786 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1787 }
1788 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001789}
1790
John Koleszar25b6e9f2013-02-12 21:17:56 -08001791static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001792 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001793}
1794
clang-format6c4d83e2016-08-08 19:03:30 -07001795static void test_decode(struct stream_state *stream,
John Koleszarb3c350a2013-03-13 12:15:43 -07001796 enum TestDecodeFatality fatal,
Yaowu Xuf883b422016-08-30 14:01:10 -07001797 const AvxInterface *codec) {
1798 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001799
clang-format6c4d83e2016-08-08 19:03:30 -07001800 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001801
John Koleszarb3c350a2013-03-13 12:15:43 -07001802 /* Get the internal reference frame */
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001803 if (strcmp(codec->name, "vp8") == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001804 struct aom_ref_frame ref_enc, ref_dec;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001805 const unsigned int frame_width = (stream->config.cfg.g_w + 15) & ~15;
1806 const unsigned int frame_height = (stream->config.cfg.g_h + 15) & ~15;
1807 aom_img_alloc(&ref_enc.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001808 enc_img = ref_enc.img;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001809 aom_img_alloc(&ref_dec.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001810 dec_img = ref_dec.img;
1811
Yaowu Xuf883b422016-08-30 14:01:10 -07001812 ref_enc.frame_type = AOM_LAST_FRAME;
1813 ref_dec.frame_type = AOM_LAST_FRAME;
1814 aom_codec_control(&stream->encoder, AOM_COPY_REFERENCE, &ref_enc);
1815 aom_codec_control(&stream->decoder, AOM_COPY_REFERENCE, &ref_dec);
John Koleszarb3c350a2013-03-13 12:15:43 -07001816 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001817 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1818 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001819
Yaowu Xuf883b422016-08-30 14:01:10 -07001820#if CONFIG_AOM_HIGHBITDEPTH
1821 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1822 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1823 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1824 aom_image_t enc_hbd_img;
1825 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001826 enc_img.d_w, enc_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001827 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001828 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001829 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001830 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1831 aom_image_t dec_hbd_img;
1832 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001833 dec_img.d_w, dec_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001834 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001835 dec_img = dec_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001836 }
1837 }
1838#endif
John Koleszarb3c350a2013-03-13 12:15:43 -07001839 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001840 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001841 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001842
John Koleszarb3c350a2013-03-13 12:15:43 -07001843 if (!compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001844 int y[4], u[4], v[4];
Yaowu Xuf883b422016-08-30 14:01:10 -07001845#if CONFIG_AOM_HIGHBITDEPTH
1846 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001847 find_mismatch_high(&enc_img, &dec_img, y, u, v);
1848 } else {
1849 find_mismatch(&enc_img, &dec_img, y, u, v);
1850 }
1851#else
John Koleszarb3c350a2013-03-13 12:15:43 -07001852 find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001853#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001854 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001855 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001856 "Stream %d: Encode/decode mismatch on frame %d at"
1857 " Y[%d, %d] {%d/%d},"
1858 " U[%d, %d] {%d/%d},"
1859 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001860 stream->index, stream->frames_out, y[0], y[1], y[2],
1861 y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
Ronald S. Bultje195172c2012-11-10 16:22:49 -08001862 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001863 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001864
Yaowu Xuf883b422016-08-30 14:01:10 -07001865 aom_img_free(&enc_img);
1866 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001867}
John Koleszarefd54f82012-02-13 16:52:18 -08001868
John Koleszar25b6e9f2013-02-12 21:17:56 -08001869static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001870 int64_t hours;
1871 int64_t mins;
1872 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001873
1874 if (etl >= 0) {
1875 hours = etl / 3600;
1876 etl -= hours * 3600;
1877 mins = etl / 60;
1878 etl -= mins * 60;
1879 secs = etl;
1880
clang-format6c4d83e2016-08-08 19:03:30 -07001881 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1882 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001883 } else {
1884 fprintf(stderr, "[%3s unknown] ", label);
1885 }
1886}
1887
Tom Finegan44dd3272013-11-20 17:18:28 -08001888int main(int argc, const char **argv_) {
1889 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001890 aom_image_t raw;
1891#if CONFIG_AOM_HIGHBITDEPTH
1892 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001893 int allocated_raw_shift = 0;
1894 int use_16bit_internal = 0;
1895 int input_shift = 0;
1896#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001897 int frame_avail, got_data;
1898
Yaowu Xuf883b422016-08-30 14:01:10 -07001899 struct AvxInputContext input;
1900 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001901 struct stream_state *streams = NULL;
1902 char **argv, **argi;
1903 uint64_t cx_time = 0;
1904 int stream_cnt = 0;
1905 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001906 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001907
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001908 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001909 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001910
clang-format6c4d83e2016-08-08 19:03:30 -07001911 if (argc < 3) usage_exit();
John Koleszar6ad3b742012-11-06 12:02:42 -08001912
1913 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001914 input.framerate.numerator = 30;
1915 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001916 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001917 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001918
1919 /* First parse the global configuration values, because we want to apply
1920 * other parameters on top of the default configuration provided by the
1921 * codec.
1922 */
1923 argv = argv_dup(argc - 1, argv_ + 1);
1924 parse_global_config(&global, argv);
1925
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001926 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001927 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1928 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1929 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1930 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1931 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001932 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001933
John Koleszar6ad3b742012-11-06 12:02:42 -08001934 {
1935 /* Now parse each stream's parameters. Using a local scope here
1936 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1937 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001938 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001939 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001940
John Koleszar6ad3b742012-11-06 12:02:42 -08001941 do {
1942 stream = new_stream(&global, stream);
1943 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001944 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001945 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001946 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001947
John Koleszarc6b90392012-07-13 15:21:29 -07001948 /* Check for unrecognized options */
1949 for (argi = argv; *argi; argi++)
1950 if (argi[0][0] == '-' && argi[0][1])
1951 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001952
clang-format6c4d83e2016-08-08 19:03:30 -07001953 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1954 &stream->config.cfg););
Tom Finegan249366b2013-11-25 12:05:19 -08001955
John Koleszarc6b90392012-07-13 15:21:29 -07001956 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001957 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001958
clang-format6c4d83e2016-08-08 19:03:30 -07001959 if (!input.filename) usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04001960
John Koleszar8dd82872013-05-06 11:01:35 -07001961 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001962 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001963
John Koleszar6ad3b742012-11-06 12:02:42 -08001964 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001965 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001966 int64_t estimated_time_left = -1;
1967 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001968 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001969
John Koleszar6ad3b742012-11-06 12:02:42 -08001970 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001971
John Koleszar6ad3b742012-11-06 12:02:42 -08001972 /* If the input file doesn't specify its w/h (raw files), try to get
1973 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001974 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001975 if (!input.width || !input.height) {
1976 FOREACH_STREAM({
1977 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1978 input.width = stream->config.cfg.g_w;
1979 input.height = stream->config.cfg.g_h;
1980 break;
1981 }
1982 });
1983 }
John Koleszarc6b90392012-07-13 15:21:29 -07001984
John Koleszar6ad3b742012-11-06 12:02:42 -08001985 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001986 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001987 fatal(
1988 "Specify stream dimensions with --width (-w) "
1989 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001990
1991 /* If input file does not specify bit-depth but input-bit-depth parameter
1992 * exists, assume that to be the input bit-depth. However, if the
1993 * input-bit-depth paramter does not exist, assume the input bit-depth
1994 * to be the same as the codec bit-depth.
1995 */
1996 if (!input.bit_depth) {
1997 FOREACH_STREAM({
1998 if (stream->config.cfg.g_input_bit_depth)
1999 input.bit_depth = stream->config.cfg.g_input_bit_depth;
2000 else
2001 input.bit_depth = stream->config.cfg.g_input_bit_depth =
2002 (int)stream->config.cfg.g_bit_depth;
2003 });
Yaowu Xuf883b422016-08-30 14:01:10 -07002004 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002005 } else {
clang-format6c4d83e2016-08-08 19:03:30 -07002006 FOREACH_STREAM(
2007 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002008 }
2009
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002010#if CONFIG_AOM_HIGHBITDEPTH
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002011 FOREACH_STREAM({
Thomas Daede8ec53b22016-09-19 13:24:51 -07002012 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2013 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2014 was selected. */
2015 switch (stream->config.cfg.g_profile) {
2016 case 0:
2017 stream->config.cfg.g_profile = 1;
2018 profile_updated = 1;
2019 break;
2020 case 2:
2021 stream->config.cfg.g_profile = 3;
2022 profile_updated = 1;
2023 break;
2024 default: break;
2025 }
2026 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002027 /* Automatically set the codec bit depth to match the input bit depth.
2028 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002029 if (stream->config.cfg.g_input_bit_depth >
2030 (unsigned int)stream->config.cfg.g_bit_depth) {
2031 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2032 }
2033 if (stream->config.cfg.g_bit_depth > 8) {
2034 switch (stream->config.cfg.g_profile) {
2035 case 0:
2036 stream->config.cfg.g_profile = 2;
2037 profile_updated = 1;
2038 break;
2039 case 1:
2040 stream->config.cfg.g_profile = 3;
2041 profile_updated = 1;
2042 break;
2043 default: break;
2044 }
2045 }
2046 if (stream->config.cfg.g_profile > 1) {
2047 stream->config.use_16bit_internal = 1;
2048 }
2049 if (profile_updated) {
2050 fprintf(stderr,
2051 "Warning: automatically upgrading to profile %d to "
2052 "match input format.\n",
2053 stream->config.cfg.g_profile);
2054 }
2055 });
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002056#else
2057 FOREACH_STREAM({
2058 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2059 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2060 was selected. */
2061 switch (stream->config.cfg.g_profile) {
2062 case 0:
2063 stream->config.cfg.g_profile = 1;
2064 profile_updated = 1;
2065 break;
2066 case 2:
2067 stream->config.cfg.g_profile = 3;
2068 profile_updated = 1;
2069 break;
2070 default: break;
2071 }
2072 }
2073 if (profile_updated) {
2074 fprintf(stderr,
2075 "Warning: automatically upgrading to profile %d to "
2076 "match input format.\n",
2077 stream->config.cfg.g_profile);
2078 }
2079 });
2080#endif
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002081
Tom Finegan00a35aa2013-11-14 12:37:42 -08002082 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
Alex Conversed66bd222014-02-21 10:52:09 -08002083 FOREACH_STREAM(validate_stream_config(stream, &global));
John Koleszar77e6b452010-11-03 13:58:40 -04002084
John Koleszar6ad3b742012-11-06 12:02:42 -08002085 /* Ensure that --passes and --pass are consistent. If --pass is set and
2086 * --passes=2, ensure --fpf was set.
2087 */
2088 if (global.pass && global.passes == 2)
clang-format6c4d83e2016-08-08 19:03:30 -07002089 FOREACH_STREAM({
2090 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002091 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002092 " and --passes=2\n",
2093 stream->index, global.pass);
2094 });
John Koleszar3245d462010-06-10 17:10:12 -04002095
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002096#if !CONFIG_WEBM_IO
2097 FOREACH_STREAM({
Ronald S. Bultje39775072015-12-16 13:35:43 -05002098 if (stream->config.write_webm) {
2099 stream->config.write_webm = 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002100 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002101 "aomenc was compiled without WebM container support."
clang-format6c4d83e2016-08-08 19:03:30 -07002102 "Producing IVF output");
Ronald S. Bultje39775072015-12-16 13:35:43 -05002103 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002104 });
2105#endif
2106
John Koleszar6ad3b742012-11-06 12:02:42 -08002107 /* Use the frame rate from the file only if none was specified
2108 * on the command-line.
2109 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002110 if (!global.have_framerate) {
2111 global.framerate.num = input.framerate.numerator;
2112 global.framerate.den = input.framerate.denominator;
Ronald S. Bultje9134e9f2016-01-18 14:03:45 -05002113 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
2114 stream->config.cfg.g_timebase.num = global.framerate.den);
Tom Finegan00a35aa2013-11-14 12:37:42 -08002115 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002116
John Koleszar6ad3b742012-11-06 12:02:42 -08002117 /* Show configuration */
2118 if (global.verbose && pass == 0)
2119 FOREACH_STREAM(show_stream_config(stream, &global, &input));
2120
2121 if (pass == (global.pass ? global.pass - 1 : 0)) {
2122 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002123 /*The Y4M reader does its own allocation.
2124 Just initialize this here to avoid problems if we never read any
2125 frames.*/
2126 memset(&raw, 0, sizeof(raw));
2127 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002128 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002129
clang-format6c4d83e2016-08-08 19:03:30 -07002130 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
2131 &stream->config.cfg, &global.framerate));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002132 }
2133
John Koleszar6ad3b742012-11-06 12:02:42 -08002134 FOREACH_STREAM(setup_pass(stream, &global, pass));
clang-format6c4d83e2016-08-08 19:03:30 -07002135 FOREACH_STREAM(
2136 open_output_file(stream, &global, &input.pixel_aspect_ratio));
John Koleszar6ad3b742012-11-06 12:02:42 -08002137 FOREACH_STREAM(initialize_encoder(stream, &global));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002138
Yaowu Xuf883b422016-08-30 14:01:10 -07002139#if CONFIG_AOM_HIGHBITDEPTH
2140 if (strcmp(global.codec->name, "av1") == 0 ||
2141 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002142 // Check to see if at least one stream uses 16 bit internal.
2143 // Currently assume that the bit_depths for all streams using
2144 // highbitdepth are the same.
2145 FOREACH_STREAM({
2146 if (stream->config.use_16bit_internal) {
2147 use_16bit_internal = 1;
2148 }
2149 if (stream->config.cfg.g_profile == 0) {
2150 input_shift = 0;
2151 } else {
2152 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07002153 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002154 }
2155 });
2156 }
2157#endif
2158
John Koleszarc6b90392012-07-13 15:21:29 -07002159 frame_avail = 1;
2160 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002161
John Koleszarc6b90392012-07-13 15:21:29 -07002162 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002163 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002164
John Koleszar6ad3b742012-11-06 12:02:42 -08002165 if (!global.limit || frames_in < global.limit) {
2166 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002167
clang-format6c4d83e2016-08-08 19:03:30 -07002168 if (frame_avail) frames_in++;
2169 seen_frames =
2170 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002171
John Koleszar6ad3b742012-11-06 12:02:42 -08002172 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002173 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002174 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2175
John Koleszar6ad3b742012-11-06 12:02:42 -08002176 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002177 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2178 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002179 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002180 fprintf(stderr, "frame %4d ", frames_in);
2181
clang-format6c4d83e2016-08-08 19:03:30 -07002182 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002183 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002184 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002185 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002186 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002187 }
2188
hui su251e1512016-10-03 15:48:43 -07002189 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002190 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002191 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002192
John Koleszar6ad3b742012-11-06 12:02:42 -08002193 if (frames_in > global.skip_frames) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002194#if CONFIG_AOM_HIGHBITDEPTH
2195 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002196 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2197 assert(use_16bit_internal);
2198 // Input bit depth and stream bit depth do not match, so up
2199 // shift frame to stream bit depth
2200 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002201 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002202 input.width, input.height, 32);
2203 allocated_raw_shift = 1;
2204 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002205 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002206 frame_to_encode = &raw_shift;
2207 } else {
2208 frame_to_encode = &raw;
2209 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002210 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002211 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002212 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002213 FOREACH_STREAM({
2214 if (stream->config.use_16bit_internal)
2215 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002216 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002217 else
2218 assert(0);
2219 });
2220 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002221 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002222 FOREACH_STREAM(encode_frame(stream, &global,
2223 frame_avail ? frame_to_encode : NULL,
2224 frames_in));
2225 }
2226#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002227 aom_usec_timer_start(&timer);
clang-format6c4d83e2016-08-08 19:03:30 -07002228 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
John Koleszar6ad3b742012-11-06 12:02:42 -08002229 frames_in));
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002230#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002231 aom_usec_timer_mark(&timer);
2232 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002233
John Koleszar6ad3b742012-11-06 12:02:42 -08002234 FOREACH_STREAM(update_quantizer_histogram(stream));
John Koleszarc6b90392012-07-13 15:21:29 -07002235
John Koleszar0ea50ce2010-05-18 11:58:33 -04002236 got_data = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002237 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002238
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002239 if (!got_data && input.length && streams != NULL &&
2240 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002241 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002242 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002243 int64_t remaining;
2244 int64_t rate;
2245
2246 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002247 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002248
2249 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002250 remaining = 1000 * (global.limit - global.skip_frames -
2251 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002252 } else {
James Zerne3578af2014-04-17 10:47:08 -07002253 const int64_t input_pos = ftello(input.file);
2254 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002255 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002256
2257 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002258 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002259 }
2260
clang-format6c4d83e2016-08-08 19:03:30 -07002261 average_rate =
2262 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002263 estimated_time_left = average_rate ? remaining / average_rate : -1;
2264 }
2265
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002266 if (got_data && global.test_decode != TEST_DECODE_OFF)
John Koleszarb3c350a2013-03-13 12:15:43 -07002267 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
John Koleszarc6b90392012-07-13 15:21:29 -07002268 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002269
John Koleszarc6b90392012-07-13 15:21:29 -07002270 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002271 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002272 }
2273
clang-format6c4d83e2016-08-08 19:03:30 -07002274 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002275
Deb Mukherjeea160d722014-09-30 21:56:33 -07002276 if (!global.quiet) {
clang-format6c4d83e2016-08-08 19:03:30 -07002277 FOREACH_STREAM(fprintf(
2278 stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2279 "b/f %7" PRId64 "b/s"
2280 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2281 pass + 1, global.passes, frames_in, stream->frames_out,
2282 (int64_t)stream->nbytes,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002283 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
clang-format6c4d83e2016-08-08 19:03:30 -07002284 seen_frames
2285 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2286 global.framerate.den / seen_frames
2287 : 0,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002288 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2289 stream->cx_time > 9999999 ? "ms" : "us",
2290 usec_to_fps(stream->cx_time, seen_frames)));
2291 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002292
Deb Mukherjeea160d722014-09-30 21:56:33 -07002293 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002294 if (global.codec->fourcc == AV1_FOURCC) {
Deb Mukherjeea160d722014-09-30 21:56:33 -07002295 FOREACH_STREAM(
2296 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2297 } else {
2298 FOREACH_STREAM(show_psnr(stream, 255.0));
2299 }
2300 }
John Koleszarc6b90392012-07-13 15:21:29 -07002301
Yaowu Xuf883b422016-08-30 14:01:10 -07002302 FOREACH_STREAM(aom_codec_destroy(&stream->encoder));
John Koleszar6ad3b742012-11-06 12:02:42 -08002303
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002304 if (global.test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002305 FOREACH_STREAM(aom_codec_destroy(&stream->decoder));
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002306 }
2307
John Koleszar6ad3b742012-11-06 12:02:42 -08002308 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002309
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002310 if (global.test_decode == TEST_DECODE_FATAL) {
2311 FOREACH_STREAM(res |= stream->mismatch_seen);
2312 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002313 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002314
John Koleszar6ad3b742012-11-06 12:02:42 -08002315 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
John Koleszarc6b90392012-07-13 15:21:29 -07002316
Pengchong Jinf349b072014-07-14 09:13:38 -07002317#if CONFIG_FP_MB_STATS
2318 FOREACH_STREAM(stats_close(&stream->fpmb_stats, global.passes - 1));
2319#endif
2320
clang-format6c4d83e2016-08-08 19:03:30 -07002321 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002322 }
2323
John Koleszar6ad3b742012-11-06 12:02:42 -08002324 if (global.show_q_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002325 FOREACH_STREAM(
2326 show_q_histogram(stream->counts, global.show_q_hist_buckets));
John Koleszar6ad3b742012-11-06 12:02:42 -08002327
2328 if (global.show_rate_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002329 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
John Koleszar6ad3b742012-11-06 12:02:42 -08002330 global.show_rate_hist_buckets));
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08002331 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
John Koleszar6ad3b742012-11-06 12:02:42 -08002332
Ronald S. Bultje31214972012-10-11 10:13:48 -07002333#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002334 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2335 * to match some existing utilities.
2336 */
Yaowu Xu47e784e2013-10-16 16:29:28 -07002337 if (!(global.pass == 1 && global.passes == 2))
2338 FOREACH_STREAM({
2339 FILE *f = fopen("opsnr.stt", "a");
2340 if (stream->mismatch_seen) {
2341 fprintf(f, "First mismatch occurred in frame %d\n",
2342 stream->mismatch_seen);
2343 } else {
2344 fprintf(f, "No mismatch detected in recon buffers\n");
2345 }
2346 fclose(f);
2347 });
Ronald S. Bultje31214972012-10-11 10:13:48 -07002348#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002349
Yaowu Xuf883b422016-08-30 14:01:10 -07002350#if CONFIG_AOM_HIGHBITDEPTH
2351 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002352#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002353 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002354 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002355 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002356 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002357}