blob: fb43d09d78f441f5244f282a71dcd53e6b9590ab [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)");
clang-format6c4d83e2016-08-08 19:03:30 -0700158static const arg_def_t good_dl =
159 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
clang-format6c4d83e2016-08-08 19:03:30 -0700160static const arg_def_t quietarg =
161 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
162static const arg_def_t verbosearg =
163 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
164static const arg_def_t psnrarg =
165 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800166
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800167static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700168 { "off", TEST_DECODE_OFF },
169 { "fatal", TEST_DECODE_FATAL },
170 { "warn", TEST_DECODE_WARN },
171 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800172};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700173static const arg_def_t recontest = ARG_DEF_ENUM(
174 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700175static const arg_def_t framerate =
176 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
177static const arg_def_t use_webm =
178 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
179static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
180static const arg_def_t out_part =
181 ARG_DEF("P", "output-partitions", 0,
182 "Makes encoder output partitions. Requires IVF output!");
183static const arg_def_t q_hist_n =
184 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
185static const arg_def_t rate_hist_n =
186 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
187static const arg_def_t disable_warnings =
188 ARG_DEF(NULL, "disable-warnings", 0,
189 "Disable warnings about potentially incorrect encode settings.");
190static const arg_def_t disable_warning_prompt =
191 ARG_DEF("y", "disable-warning-prompt", 0,
192 "Display warnings, but do not prompt user to continue.");
Alex Conversed66bd222014-02-21 10:52:09 -0800193
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200194#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700195static const arg_def_t test16bitinternalarg = ARG_DEF(
196 NULL, "test-16bit-internal", 0, "Force use of 16 bit internal buffer");
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400197
198static const struct arg_enum_list bitdepth_enum[] = {
199 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
200};
201
202static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
203 "b", "bit-depth", 1,
204 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
205 bitdepth_enum);
206static const arg_def_t inbitdeptharg =
207 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700208#endif
Tom Finegan44dd3272013-11-20 17:18:28 -0800209
clang-format6c4d83e2016-08-08 19:03:30 -0700210static const arg_def_t *main_args[] = { &debugmode,
211 &outputfile,
212 &codecarg,
213 &passes,
214 &pass_arg,
215 &fpf_name,
216 &limit,
217 &skip,
218 &deadline,
clang-format6c4d83e2016-08-08 19:03:30 -0700219 &good_dl,
clang-format6c4d83e2016-08-08 19:03:30 -0700220 &quietarg,
221 &verbosearg,
222 &psnrarg,
223 &use_webm,
224 &use_ivf,
225 &out_part,
226 &q_hist_n,
227 &rate_hist_n,
228 &disable_warnings,
229 &disable_warning_prompt,
230 &recontest,
231 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400232
clang-format6c4d83e2016-08-08 19:03:30 -0700233static const arg_def_t usage =
234 ARG_DEF("u", "usage", 1, "Usage profile number to use");
235static const arg_def_t threads =
236 ARG_DEF("t", "threads", 1, "Max number of threads to use");
237static const arg_def_t profile =
238 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700239static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
240static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700241#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700242static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700243 { "mono", STEREO_FORMAT_MONO },
244 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
245 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
246 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
247 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
248 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700249};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700250static const arg_def_t stereo_mode = ARG_DEF_ENUM(
251 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700252#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700253static const arg_def_t timebase = ARG_DEF(
254 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700255static const arg_def_t error_resilient =
256 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
257static const arg_def_t lag_in_frames =
258 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400259
clang-format6c4d83e2016-08-08 19:03:30 -0700260static const arg_def_t *global_args[] = { &use_yv12,
261 &use_i420,
262 &use_i422,
263 &use_i444,
264 &use_i440,
265 &usage,
266 &threads,
267 &profile,
268 &width,
269 &height,
James Zerndba73762014-05-10 17:44:12 -0700270#if CONFIG_WEBM_IO
clang-format6c4d83e2016-08-08 19:03:30 -0700271 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700272#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700273 &timebase,
274 &framerate,
275 &error_resilient,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200276#if CONFIG_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700277 &test16bitinternalarg,
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400278 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700279#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700280 &lag_in_frames,
281 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400282
clang-format6c4d83e2016-08-08 19:03:30 -0700283static const arg_def_t dropframe_thresh =
284 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
285static const arg_def_t resize_allowed =
286 ARG_DEF(NULL, "resize-allowed", 1, "Spatial resampling enabled (bool)");
287static const arg_def_t resize_width =
288 ARG_DEF(NULL, "resize-width", 1, "Width of encoded frame");
289static const arg_def_t resize_height =
290 ARG_DEF(NULL, "resize-height", 1, "Height of encoded frame");
291static const arg_def_t resize_up_thresh =
292 ARG_DEF(NULL, "resize-up", 1, "Upscale threshold (buf %)");
293static const arg_def_t resize_down_thresh =
294 ARG_DEF(NULL, "resize-down", 1, "Downscale threshold (buf %)");
Yaowu Xuf883b422016-08-30 14:01:10 -0700295static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
296 { "cbr", AOM_CBR },
297 { "cq", AOM_CQ },
298 { "q", AOM_Q },
clang-format6c4d83e2016-08-08 19:03:30 -0700299 { NULL, 0 } };
300static const arg_def_t end_usage =
301 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
302static const arg_def_t target_bitrate =
303 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
304static const arg_def_t min_quantizer =
305 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
306static const arg_def_t max_quantizer =
307 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
308static const arg_def_t undershoot_pct =
309 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
310static const arg_def_t overshoot_pct =
311 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
312static const arg_def_t buf_sz =
313 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
314static const arg_def_t buf_initial_sz =
315 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
316static const arg_def_t buf_optimal_sz =
317 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
John Koleszarc6b90392012-07-13 15:21:29 -0700318static const arg_def_t *rc_args[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700319 &dropframe_thresh, &resize_allowed, &resize_width, &resize_height,
320 &resize_up_thresh, &resize_down_thresh, &end_usage, &target_bitrate,
321 &min_quantizer, &max_quantizer, &undershoot_pct, &overshoot_pct,
322 &buf_sz, &buf_initial_sz, &buf_optimal_sz, NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -0400323};
324
clang-format6c4d83e2016-08-08 19:03:30 -0700325static const arg_def_t bias_pct =
326 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
327static const arg_def_t minsection_pct =
328 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
329static const arg_def_t maxsection_pct =
330 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
331static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
332 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400333
clang-format6c4d83e2016-08-08 19:03:30 -0700334static const arg_def_t kf_min_dist =
335 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
336static const arg_def_t kf_max_dist =
337 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
338static const arg_def_t kf_disabled =
339 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
340static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
341 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400342
clang-format6c4d83e2016-08-08 19:03:30 -0700343static const arg_def_t noise_sens =
344 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
345static const arg_def_t sharpness =
346 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
347static const arg_def_t static_thresh =
348 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
349static const arg_def_t auto_altref =
350 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
351static const arg_def_t arnr_maxframes =
352 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
353static const arg_def_t arnr_strength =
354 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500355static const struct arg_enum_list tuning_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700356 { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500357};
clang-format6c4d83e2016-08-08 19:03:30 -0700358static const arg_def_t tune_ssim =
359 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
360static const arg_def_t cq_level =
361 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
362static const arg_def_t max_intra_rate_pct =
363 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800364
Yaowu Xuf883b422016-08-30 14:01:10 -0700365#if CONFIG_AV1_ENCODER
366static const arg_def_t cpu_used_av1 =
clang-format6c4d83e2016-08-08 19:03:30 -0700367 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (-8..8)");
368static const arg_def_t tile_cols =
369 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
370static const arg_def_t tile_rows =
371 ARG_DEF(NULL, "tile-rows", 1,
372 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800373#if CONFIG_DEPENDENT_HORZTILES
374static const arg_def_t tile_dependent_rows =
375 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
376#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800377#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800378static const arg_def_t tile_loopfilter = ARG_DEF(
379 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800380#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700381static const arg_def_t lossless =
382 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700383#if CONFIG_AOM_QM
384static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800385 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700386 "Enable quantisation matrices (0: false (default), 1: true)");
387static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800388 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700389static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800390 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700391#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000392#if CONFIG_TILE_GROUPS
Yaowu Xu859a5272016-11-10 15:32:21 -0800393static const arg_def_t num_tg = ARG_DEF(
394 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000395static const arg_def_t mtu_size =
396 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800397 "MTU size for a tile group, default is 0 (no MTU targeting), "
398 "overrides maximum number of tile groups");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000399#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800400#if CONFIG_TEMPMV_SIGNALING
401static const arg_def_t disable_tempmv = ARG_DEF(
402 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
403#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700404static const arg_def_t frame_parallel_decoding =
405 ARG_DEF(NULL, "frame-parallel", 1,
406 "Enable frame parallel decodability features "
407 "(0: false (default), 1: true)");
Thomase28d92b2016-09-20 12:07:11 +0100408#if CONFIG_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100409static const arg_def_t aq_mode = ARG_DEF(
410 NULL, "aq-mode", 1,
411 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100412 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200413#else
Thomase28d92b2016-09-20 12:07:11 +0100414static const arg_def_t aq_mode = ARG_DEF(
415 NULL, "aq-mode", 1,
416 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100417 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200418#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700419static const arg_def_t frame_periodic_boost =
420 ARG_DEF(NULL, "frame-boost", 1,
421 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700422static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
423 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700424static const arg_def_t max_inter_rate_pct =
425 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700426static const arg_def_t min_gf_interval = ARG_DEF(
427 NULL, "min-gf-interval", 1,
428 "min gf/arf frame interval (default 0, indicating in-built behavior)");
429static const arg_def_t max_gf_interval = ARG_DEF(
430 NULL, "max-gf-interval", 1,
431 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800432
Yaowu Xufc996362015-02-10 15:03:05 -0800433static const struct arg_enum_list color_space_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700434 { "unknown", AOM_CS_UNKNOWN },
435 { "bt601", AOM_CS_BT_601 },
436 { "bt709", AOM_CS_BT_709 },
437 { "smpte170", AOM_CS_SMPTE_170 },
438 { "smpte240", AOM_CS_SMPTE_240 },
439 { "bt2020", AOM_CS_BT_2020 },
440 { "reserved", AOM_CS_RESERVED },
441 { "sRGB", AOM_CS_SRGB },
Yaowu Xufc996362015-02-10 15:03:05 -0800442 { NULL, 0 }
443};
444
clang-format6c4d83e2016-08-08 19:03:30 -0700445static const arg_def_t input_color_space =
446 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
447 color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800448
Alex Converse219f6452014-08-06 11:01:18 -0700449static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700450 { "default", AOM_CONTENT_DEFAULT },
451 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700452 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700453};
454
455static const arg_def_t tune_content = ARG_DEF_ENUM(
456 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700457#endif
Alex Converse219f6452014-08-06 11:01:18 -0700458
Yaowu Xuf883b422016-08-30 14:01:10 -0700459#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000460#if CONFIG_EXT_PARTITION
461static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700462 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
463 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
464 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700465 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000466};
467static const arg_def_t superblock_size = ARG_DEF_ENUM(
468 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
469#endif // CONFIG_EXT_PARTITION
470
Yaowu Xuf883b422016-08-30 14:01:10 -0700471static const arg_def_t *av1_args[] = { &cpu_used_av1,
472 &auto_altref,
473 &sharpness,
474 &static_thresh,
475 &tile_cols,
476 &tile_rows,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800477#if CONFIG_DEPENDENT_HORZTILES
478 &tile_dependent_rows,
479#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800480#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800481 &tile_loopfilter,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800482#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700483 &arnr_maxframes,
484 &arnr_strength,
Yaowu Xuf883b422016-08-30 14:01:10 -0700485 &tune_ssim,
486 &cq_level,
487 &max_intra_rate_pct,
488 &max_inter_rate_pct,
489 &gf_cbr_boost_pct,
490 &lossless,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800491#if CONFIG_AOM_QM
492 &enable_qm,
493 &qm_min,
494 &qm_max,
495#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700496 &frame_parallel_decoding,
497 &aq_mode,
498 &frame_periodic_boost,
499 &noise_sens,
500 &tune_content,
501 &input_color_space,
502 &min_gf_interval,
503 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000504#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700505 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000506#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000507#if CONFIG_TILE_GROUPS
508 &num_tg,
509 &mtu_size,
510#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800511#if CONFIG_TEMPMV_SIGNALING
512 &disable_tempmv,
513#endif
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200514#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700515 &bitdeptharg,
516 &inbitdeptharg,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200517#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700518 NULL };
519static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
520 AOME_SET_ENABLEAUTOALTREF,
521 AOME_SET_SHARPNESS,
522 AOME_SET_STATIC_THRESHOLD,
523 AV1E_SET_TILE_COLUMNS,
524 AV1E_SET_TILE_ROWS,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800525#if CONFIG_DEPENDENT_HORZTILES
526 AV1E_SET_TILE_DEPENDENT_ROWS,
527#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800528#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800529 AV1E_SET_TILE_LOOPFILTER,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800530#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700531 AOME_SET_ARNR_MAXFRAMES,
532 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700533 AOME_SET_TUNING,
534 AOME_SET_CQ_LEVEL,
535 AOME_SET_MAX_INTRA_BITRATE_PCT,
536 AV1E_SET_MAX_INTER_BITRATE_PCT,
537 AV1E_SET_GF_CBR_BOOST_PCT,
538 AV1E_SET_LOSSLESS,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800539#if CONFIG_AOM_QM
540 AV1E_SET_ENABLE_QM,
541 AV1E_SET_QM_MIN,
542 AV1E_SET_QM_MAX,
543#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700544 AV1E_SET_FRAME_PARALLEL_DECODING,
545 AV1E_SET_AQ_MODE,
546 AV1E_SET_FRAME_PERIODIC_BOOST,
547 AV1E_SET_NOISE_SENSITIVITY,
548 AV1E_SET_TUNE_CONTENT,
549 AV1E_SET_COLOR_SPACE,
550 AV1E_SET_MIN_GF_INTERVAL,
551 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000552#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700553 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000554#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000555#if CONFIG_TILE_GROUPS
556 AV1E_SET_NUM_TG,
557 AV1E_SET_MTU,
558#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800559#if CONFIG_TEMPMV_SIGNALING
560 AV1E_SET_DISABLE_TEMPMV,
561#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700562 0 };
hui su82331e02015-08-16 18:21:56 -0700563#endif
564
John Koleszar0ea50ce2010-05-18 11:58:33 -0400565static const arg_def_t *no_args[] = { NULL };
566
James Zern59e7a472015-05-09 10:33:26 -0700567void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700568 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700569 const int num_encoder = get_aom_encoder_count();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400570
John Koleszarc6b90392012-07-13 15:21:29 -0700571 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
572 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400573
John Koleszarc6b90392012-07-13 15:21:29 -0700574 fprintf(stderr, "\nOptions:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700575 arg_show_usage(stderr, main_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700576 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700577 arg_show_usage(stderr, global_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700578 fprintf(stderr, "\nRate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700579 arg_show_usage(stderr, rc_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700580 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700581 arg_show_usage(stderr, rc_twopass_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700582 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700583 arg_show_usage(stderr, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700584#if CONFIG_AV1_ENCODER
585 fprintf(stderr, "\nAV1 Specific Options:\n");
586 arg_show_usage(stderr, av1_args);
hui su82331e02015-08-16 18:21:56 -0700587#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700588 fprintf(stderr,
589 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700590 " The desired precision of timestamps in the output, expressed\n"
591 " in fractional seconds. Default is 1/1000.\n");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800592 fprintf(stderr, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400593
Yaowu Xuf990b352015-06-01 09:13:59 -0700594 for (i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700595 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700596 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
597 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700598 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800599 }
Yaowu Xuf990b352015-06-01 09:13:59 -0700600 fprintf(stderr, "\n ");
601 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400602
John Koleszarc6b90392012-07-13 15:21:29 -0700603 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400604}
605
clang-format6c4d83e2016-08-08 19:03:30 -0700606#define mmin(a, b) ((a) < (b) ? (a) : (b))
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700607
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200608#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700609static void find_mismatch_high(const aom_image_t *const img1,
610 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700611 int uloc[4], int vloc[4]) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700612 uint16_t *plane1, *plane2;
613 uint32_t stride1, stride2;
614 const uint32_t bsize = 64;
615 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
616 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
617 const uint32_t c_w =
618 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
619 const uint32_t c_h =
620 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
621 int match = 1;
622 uint32_t i, j;
623 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700624 plane1 = (uint16_t *)img1->planes[AOM_PLANE_Y];
625 plane2 = (uint16_t *)img2->planes[AOM_PLANE_Y];
626 stride1 = img1->stride[AOM_PLANE_Y] / 2;
627 stride2 = img2->stride[AOM_PLANE_Y] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700628 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
629 for (j = 0; match && j < img1->d_w; j += bsize) {
630 int k, l;
631 const int si = mmin(i + bsize, img1->d_h) - i;
632 const int sj = mmin(j + bsize, img1->d_w) - j;
633 for (k = 0; match && k < si; ++k) {
634 for (l = 0; match && l < sj; ++l) {
635 if (*(plane1 + (i + k) * stride1 + j + l) !=
636 *(plane2 + (i + k) * stride2 + j + l)) {
637 yloc[0] = i + k;
638 yloc[1] = j + l;
639 yloc[2] = *(plane1 + (i + k) * stride1 + j + l);
640 yloc[3] = *(plane2 + (i + k) * stride2 + j + l);
641 match = 0;
642 break;
643 }
644 }
645 }
646 }
647 }
648
649 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700650 plane1 = (uint16_t *)img1->planes[AOM_PLANE_U];
651 plane2 = (uint16_t *)img2->planes[AOM_PLANE_U];
652 stride1 = img1->stride[AOM_PLANE_U] / 2;
653 stride2 = img2->stride[AOM_PLANE_U] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700654 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
655 for (j = 0; match && j < c_w; j += bsizex) {
656 int k, l;
657 const int si = mmin(i + bsizey, c_h - i);
658 const int sj = mmin(j + bsizex, c_w - j);
659 for (k = 0; match && k < si; ++k) {
660 for (l = 0; match && l < sj; ++l) {
661 if (*(plane1 + (i + k) * stride1 + j + l) !=
662 *(plane2 + (i + k) * stride2 + j + l)) {
663 uloc[0] = i + k;
664 uloc[1] = j + l;
665 uloc[2] = *(plane1 + (i + k) * stride1 + j + l);
666 uloc[3] = *(plane2 + (i + k) * stride2 + j + l);
667 match = 0;
668 break;
669 }
670 }
671 }
672 }
673 }
674
675 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700676 plane1 = (uint16_t *)img1->planes[AOM_PLANE_V];
677 plane2 = (uint16_t *)img2->planes[AOM_PLANE_V];
678 stride1 = img1->stride[AOM_PLANE_V] / 2;
679 stride2 = img2->stride[AOM_PLANE_V] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700680 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
681 for (j = 0; match && j < c_w; j += bsizex) {
682 int k, l;
683 const int si = mmin(i + bsizey, c_h - i);
684 const int sj = mmin(j + bsizex, c_w - j);
685 for (k = 0; match && k < si; ++k) {
686 for (l = 0; match && l < sj; ++l) {
687 if (*(plane1 + (i + k) * stride1 + j + l) !=
688 *(plane2 + (i + k) * stride2 + j + l)) {
689 vloc[0] = i + k;
690 vloc[1] = j + l;
691 vloc[2] = *(plane1 + (i + k) * stride1 + j + l);
692 vloc[3] = *(plane2 + (i + k) * stride2 + j + l);
693 match = 0;
694 break;
695 }
696 }
697 }
698 }
699 }
700}
701#endif
702
Yaowu Xuf883b422016-08-30 14:01:10 -0700703static void find_mismatch(const aom_image_t *const img1,
704 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700705 int uloc[4], int vloc[4]) {
James Zern6253e8e2014-01-31 16:10:43 -0800706 const uint32_t bsize = 64;
707 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
708 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
709 const uint32_t c_w =
710 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
711 const uint32_t c_h =
712 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
713 int match = 1;
714 uint32_t i, j;
Deb Mukherjee23144d22013-03-12 14:21:08 -0700715 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -0800716 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
717 for (j = 0; match && j < img1->d_w; j += bsize) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800718 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800719 const int si = mmin(i + bsize, img1->d_h) - i;
720 const int sj = mmin(j + bsize, img1->d_w) - j;
721 for (k = 0; match && k < si; ++k) {
722 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700723 if (*(img1->planes[AOM_PLANE_Y] +
724 (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
725 *(img2->planes[AOM_PLANE_Y] +
726 (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800727 yloc[0] = i + k;
728 yloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 yloc[2] = *(img1->planes[AOM_PLANE_Y] +
730 (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
731 yloc[3] = *(img2->planes[AOM_PLANE_Y] +
732 (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800733 match = 0;
734 break;
735 }
736 }
James Zernd04112d2014-01-31 15:49:17 -0800737 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800738 }
739 }
Jingning Han12bf0792013-04-06 10:00:53 -0700740
Deb Mukherjee23144d22013-03-12 14:21:08 -0700741 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700742 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700743 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800744 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800745 const int si = mmin(i + bsizey, c_h - i);
746 const int sj = mmin(j + bsizex, c_w - j);
747 for (k = 0; match && k < si; ++k) {
748 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700749 if (*(img1->planes[AOM_PLANE_U] +
750 (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
751 *(img2->planes[AOM_PLANE_U] +
752 (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800753 uloc[0] = i + k;
754 uloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 uloc[2] = *(img1->planes[AOM_PLANE_U] +
756 (i + k) * img1->stride[AOM_PLANE_U] + j + l);
757 uloc[3] = *(img2->planes[AOM_PLANE_U] +
758 (i + k) * img2->stride[AOM_PLANE_U] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800759 match = 0;
760 break;
761 }
762 }
James Zernd04112d2014-01-31 15:49:17 -0800763 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800764 }
765 }
Deb Mukherjee23144d22013-03-12 14:21:08 -0700766 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700767 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700768 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800769 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800770 const int si = mmin(i + bsizey, c_h - i);
771 const int sj = mmin(j + bsizex, c_w - j);
772 for (k = 0; match && k < si; ++k) {
773 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700774 if (*(img1->planes[AOM_PLANE_V] +
775 (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
776 *(img2->planes[AOM_PLANE_V] +
777 (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800778 vloc[0] = i + k;
779 vloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700780 vloc[2] = *(img1->planes[AOM_PLANE_V] +
781 (i + k) * img1->stride[AOM_PLANE_V] + j + l);
782 vloc[3] = *(img2->planes[AOM_PLANE_V] +
783 (i + k) * img2->stride[AOM_PLANE_V] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800784 match = 0;
785 break;
786 }
787 }
James Zernd04112d2014-01-31 15:49:17 -0800788 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800789 }
790 }
791}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400792
Yaowu Xuf883b422016-08-30 14:01:10 -0700793static int compare_img(const aom_image_t *const img1,
794 const aom_image_t *const img2) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700795 uint32_t l_w = img1->d_w;
clang-format6c4d83e2016-08-08 19:03:30 -0700796 uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
James Zern6253e8e2014-01-31 16:10:43 -0800797 const uint32_t c_h =
798 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
799 uint32_t i;
John Koleszarc6b90392012-07-13 15:21:29 -0700800 int match = 1;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800801
John Koleszarc6b90392012-07-13 15:21:29 -0700802 match &= (img1->fmt == img2->fmt);
hkuang3671e622014-01-22 14:58:59 -0800803 match &= (img1->d_w == img2->d_w);
804 match &= (img1->d_h == img2->d_h);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200805#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700806 if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700807 l_w *= 2;
808 c_w *= 2;
809 }
810#endif
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800811
James Zernd04112d2014-01-31 15:49:17 -0800812 for (i = 0; i < img1->d_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700813 match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
814 img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700815 l_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800816
James Zernd04112d2014-01-31 15:49:17 -0800817 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700818 match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
819 img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
John Koleszarbeae5012013-05-08 16:07:38 -0700820 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800821
James Zernd04112d2014-01-31 15:49:17 -0800822 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700823 match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
824 img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
John Koleszarbeae5012013-05-08 16:07:38 -0700825 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800826
John Koleszarc6b90392012-07-13 15:21:29 -0700827 return match;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800828}
829
clang-format6c4d83e2016-08-08 19:03:30 -0700830#define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
Yaowu Xuf883b422016-08-30 14:01:10 -0700831#if CONFIG_AV1_ENCODER
832#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800833#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800834
James Zerndba73762014-05-10 17:44:12 -0700835#if !CONFIG_WEBM_IO
836typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700837struct WebmOutputContext {
838 int debug;
839};
James Zerndba73762014-05-10 17:44:12 -0700840#endif
841
John Koleszar9e50ed72012-02-15 12:39:38 -0800842/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800843struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700844 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700845 const char *out_fn;
846 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700847#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700848 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700849#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700850 stereo_format_t stereo_fmt;
851 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
852 int arg_ctrl_cnt;
853 int write_webm;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200854#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700855 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700856 int use_16bit_internal;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700857#endif
John Koleszar9e50ed72012-02-15 12:39:38 -0800858};
859
John Koleszar6ad3b742012-11-06 12:02:42 -0800860struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700861 int index;
862 struct stream_state *next;
863 struct stream_config config;
864 FILE *file;
865 struct rate_hist *rate_hist;
866 struct WebmOutputContext webm_ctx;
867 uint64_t psnr_sse_total;
868 uint64_t psnr_samples_total;
869 double psnr_totals[4];
870 int psnr_count;
871 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700872 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700873 unsigned int frames_out;
874 uint64_t cx_time;
875 size_t nbytes;
876 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700877#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700878 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700879#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700880 struct aom_image *img;
881 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700882 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800883};
884
clang-format6c4d83e2016-08-08 19:03:30 -0700885static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700886 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800887 if (rat->den < 0) {
888 rat->num *= -1;
889 rat->den *= -1;
890 }
John Koleszar1b27e932012-04-25 17:08:56 -0700891
clang-format6c4d83e2016-08-08 19:03:30 -0700892 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700893
clang-format6c4d83e2016-08-08 19:03:30 -0700894 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700895}
896
Yaowu Xuf883b422016-08-30 14:01:10 -0700897static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700898 char **argi, **argj;
899 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700900 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700901
clang-format6c4d83e2016-08-08 19:03:30 -0700902 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800903
John Koleszar6ad3b742012-11-06 12:02:42 -0800904 /* Initialize default parameters */
905 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700906 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700907 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700908 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700909 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700910 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400911
John Koleszarc6b90392012-07-13 15:21:29 -0700912 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
913 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400914
John Koleszarc6b90392012-07-13 15:21:29 -0700915 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700916 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800917 if (!global->codec)
918 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700919 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800920 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400921
John Koleszar6ad3b742012-11-06 12:02:42 -0800922 if (global->passes < 1 || global->passes > 2)
923 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700924 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800925 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400926
John Koleszar6ad3b742012-11-06 12:02:42 -0800927 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700928 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800929 } else if (arg_match(&arg, &usage, argi))
930 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700931 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800932 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700933 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700934 global->deadline = AOM_DL_GOOD_QUALITY;
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)
Thomas Daede80826142017-03-20 15:44:24 -0700991 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800992#else
993 global->passes = 1;
994#endif
995 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800996}
997
Yaowu Xuf883b422016-08-30 14:01:10 -0700998static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800999 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -07001000 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
1001 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -08001002
clang-format6c4d83e2016-08-08 19:03:30 -07001003 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -08001004
John Koleszar25b6e9f2013-02-12 21:17:56 -08001005 if (!fseeko(input->file, 0, SEEK_END)) {
1006 /* Input file is seekable. Figure out how long it is, so we can get
1007 * progress info.
1008 */
1009 input->length = ftello(input->file);
1010 rewind(input->file);
1011 }
1012
Frank Galligan09acd262015-06-01 10:20:58 -07001013 /* Default to 1:1 pixel aspect ratio. */
1014 input->pixel_aspect_ratio.numerator = 1;
1015 input->pixel_aspect_ratio.denominator = 1;
1016
John Koleszar6ad3b742012-11-06 12:02:42 -08001017 /* For RAW input sources, these bytes will applied on the first frame
1018 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -07001019 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001020 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1021 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001022
clang-format6c4d83e2016-08-08 19:03:30 -07001023 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -07001024 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
1025 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001026 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001027 input->width = input->y4m.pic_w;
1028 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -07001029 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1030 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001031 input->framerate.numerator = input->y4m.fps_n;
1032 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -07001033 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001034 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -08001035 } else
1036 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -08001037 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -08001038 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -08001039 } else {
1040 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001041 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001042}
1043
Yaowu Xuf883b422016-08-30 14:01:10 -07001044static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001045 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -07001046 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001047}
1048
Yaowu Xuf883b422016-08-30 14:01:10 -07001049static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -08001050 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001051 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001052
John Koleszar6ad3b742012-11-06 12:02:42 -08001053 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001054 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001055 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001056 }
1057
John Koleszar6ad3b742012-11-06 12:02:42 -08001058 if (prev) {
1059 memcpy(stream, prev, sizeof(*stream));
1060 stream->index++;
1061 prev->next = stream;
1062 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001063 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001064
John Koleszar6ad3b742012-11-06 12:02:42 -08001065 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -07001066 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -07001067 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -07001068 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001069
John Koleszar6ad3b742012-11-06 12:02:42 -08001070 /* Change the default timebase to a high enough value so that the
1071 * encoder will always create strictly increasing timestamps.
1072 */
1073 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001074
John Koleszar6ad3b742012-11-06 12:02:42 -08001075 /* Never use the library's default resolution, require it be parsed
1076 * from the file or set on the command line.
1077 */
1078 stream->config.cfg.g_w = 0;
1079 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001080
John Koleszar6ad3b742012-11-06 12:02:42 -08001081 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -08001082 stream->config.write_webm = 1;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001083#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -07001084 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001085 stream->webm_ctx.last_pts_ns = -1;
1086 stream->webm_ctx.writer = NULL;
1087 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001088#endif
Johann87c40b32012-03-01 16:12:53 -08001089
John Koleszar6ad3b742012-11-06 12:02:42 -08001090 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001091 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -08001092 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001093
John Koleszar6ad3b742012-11-06 12:02:42 -08001094 /* Output files must be specified for each stream */
1095 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001096
John Koleszar6ad3b742012-11-06 12:02:42 -08001097 stream->next = NULL;
1098 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001099}
1100
Yaowu Xuf883b422016-08-30 14:01:10 -07001101static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001102 struct stream_state *stream, char **argv) {
1103 char **argi, **argj;
1104 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001105 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001106 static const int *ctrl_args_map = NULL;
1107 struct stream_config *config = &stream->config;
1108 int eos_mark_found = 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001109#if CONFIG_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -07001110 int test_16bit_internal = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001111#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001112
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001113 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001114 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001115#if CONFIG_AV1_ENCODER
1116 } else if (strcmp(global->codec->name, "av1") == 0) {
1117 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1118 // Consider to expand this set for AV1 encoder control.
1119 ctrl_args = av1_args;
1120 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001121#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001122 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001123
John Koleszarc6b90392012-07-13 15:21:29 -07001124 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001125 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001126
John Koleszar6ad3b742012-11-06 12:02:42 -08001127 /* Once we've found an end-of-stream marker (--) we want to continue
1128 * shifting arguments but not consuming them.
1129 */
1130 if (eos_mark_found) {
1131 argj++;
1132 continue;
1133 } else if (!strcmp(*argj, "--")) {
1134 eos_mark_found = 1;
1135 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001136 }
1137
Adrian Granged2401802015-02-13 14:51:32 -08001138 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001139 config->out_fn = arg.val;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001140 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001141 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001142#if CONFIG_FP_MB_STATS
1143 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1144 config->fpmb_stats_fn = arg.val;
1145#endif
Johannb50e5182015-01-30 15:05:14 -08001146 } else if (arg_match(&arg, &use_webm, argi)) {
1147#if CONFIG_WEBM_IO
1148 config->write_webm = 1;
1149#else
1150 die("Error: --webm specified but webm is disabled.");
1151#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001152 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001153 config->write_webm = 0;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001154 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001155 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001156 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001157 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001158 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001159 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001160 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001161 config->cfg.g_h = arg_parse_uint(&arg);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001162#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001163 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1164 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1165 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1166 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1167#endif
James Zerndba73762014-05-10 17:44:12 -07001168#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001169 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001170 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001171#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001172 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001173 config->cfg.g_timebase = arg_parse_rational(&arg);
1174 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001175 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001176 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001177 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001178 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001179 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001180 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001181 } else if (arg_match(&arg, &resize_allowed, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001182 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001183 } else if (arg_match(&arg, &resize_width, argi)) {
1184 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
1185 } else if (arg_match(&arg, &resize_height, argi)) {
1186 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001187 } else if (arg_match(&arg, &resize_up_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001188 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001189 } else if (arg_match(&arg, &resize_down_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001190 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001191 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001192 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001193 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001194 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001195 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001196 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001197 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001198 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001199 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001200 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001201 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001202 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001203 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001204 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001205 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001206 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001207 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001208 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001209 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001210 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001211 if (global->passes < 2)
1212 warn("option %s ignored in one-pass mode.\n", arg.name);
1213 } else if (arg_match(&arg, &minsection_pct, argi)) {
1214 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1215
1216 if (global->passes < 2)
1217 warn("option %s ignored in one-pass mode.\n", arg.name);
1218 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1219 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1220
1221 if (global->passes < 2)
1222 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001223 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001224 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001225 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001226 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001227 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001228 config->cfg.kf_mode = AOM_KF_DISABLED;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001229#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001230 } else if (arg_match(&arg, &test16bitinternalarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001231 if (strcmp(global->codec->name, "av1") == 0 ||
1232 strcmp(global->codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001233 test_16bit_internal = 1;
1234 }
1235#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001236 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001237 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001238 for (i = 0; ctrl_args[i]; i++) {
1239 if (arg_match(&arg, ctrl_args[i], argi)) {
1240 int j;
1241 match = 1;
1242
1243 /* Point either to the next free element or the first
1244 * instance of this control.
1245 */
1246 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001247 if (ctrl_args_map != NULL &&
1248 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001249 break;
1250
1251 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001252 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001253 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001254 config->arg_ctrls[j][0] = ctrl_args_map[i];
1255 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001256 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001257 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001258 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001259 }
clang-format6c4d83e2016-08-08 19:03:30 -07001260 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001261 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001262 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001263#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001264 if (strcmp(global->codec->name, "av1") == 0 ||
1265 strcmp(global->codec->name, "av1") == 0) {
clang-format6c4d83e2016-08-08 19:03:30 -07001266 config->use_16bit_internal =
1267 test_16bit_internal | (config->cfg.g_profile > 1);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001268 }
1269#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001270 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001271}
1272
clang-format6c4d83e2016-08-08 19:03:30 -07001273#define FOREACH_STREAM(func) \
1274 do { \
1275 struct stream_state *stream; \
Tom Finegan249366b2013-11-25 12:05:19 -08001276 for (stream = streams; stream; stream = stream->next) { \
clang-format6c4d83e2016-08-08 19:03:30 -07001277 func; \
1278 } \
Tom Finegan44dd3272013-11-20 17:18:28 -08001279 } while (0)
John Koleszar9e50ed72012-02-15 12:39:38 -08001280
Alex Conversed66bd222014-02-21 10:52:09 -08001281static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001282 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001283 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001284 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001285
John Koleszar6ad3b742012-11-06 12:02:42 -08001286 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001287 fatal(
1288 "Stream %d: Specify stream dimensions with --width (-w) "
1289 " and --height (-h)",
1290 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001291
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001292 // Check that the codec bit depth is greater than the input bit depth.
1293 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001294 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001295 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1296 stream->index, (int)stream->config.cfg.g_bit_depth,
1297 stream->config.cfg.g_input_bit_depth);
1298 }
1299
John Koleszar6ad3b742012-11-06 12:02:42 -08001300 for (streami = stream; streami; streami = streami->next) {
1301 /* All streams require output files */
1302 if (!streami->config.out_fn)
1303 fatal("Stream %d: Output file is required (specify with -o)",
1304 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001305
John Koleszar6ad3b742012-11-06 12:02:42 -08001306 /* Check for two streams outputting to the same file */
1307 if (streami != stream) {
1308 const char *a = stream->config.out_fn;
1309 const char *b = streami->config.out_fn;
1310 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1311 fatal("Stream %d: duplicate output file (from stream %d)",
1312 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001313 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001314
1315 /* Check for two streams sharing a stats file. */
1316 if (streami != stream) {
1317 const char *a = stream->config.stats_fn;
1318 const char *b = streami->config.stats_fn;
1319 if (a && b && !strcmp(a, b))
1320 fatal("Stream %d: duplicate stats file (from stream %d)",
1321 streami->index, stream->index);
1322 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001323
1324#if CONFIG_FP_MB_STATS
1325 /* Check for two streams sharing a mb stats file. */
1326 if (streami != stream) {
1327 const char *a = stream->config.fpmb_stats_fn;
1328 const char *b = streami->config.fpmb_stats_fn;
1329 if (a && b && !strcmp(a, b))
1330 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1331 streami->index, stream->index);
1332 }
1333#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001334 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001335}
1336
clang-format6c4d83e2016-08-08 19:03:30 -07001337static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001338 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001339 if (!stream->config.cfg.g_w) {
1340 if (!stream->config.cfg.g_h)
1341 stream->config.cfg.g_w = w;
1342 else
1343 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1344 }
1345 if (!stream->config.cfg.g_h) {
1346 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1347 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001348}
1349
clang-format6c4d83e2016-08-08 19:03:30 -07001350static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001351 switch (t) {
1352 case FILE_TYPE_RAW: return "RAW";
1353 case FILE_TYPE_Y4M: return "Y4M";
1354 default: return "Other";
1355 }
1356}
1357
Yaowu Xuf883b422016-08-30 14:01:10 -07001358static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001359 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001360 case AOM_IMG_FMT_I420: return "I420";
1361 case AOM_IMG_FMT_I422: return "I422";
1362 case AOM_IMG_FMT_I444: return "I444";
1363 case AOM_IMG_FMT_I440: return "I440";
1364 case AOM_IMG_FMT_YV12: return "YV12";
1365 case AOM_IMG_FMT_I42016: return "I42016";
1366 case AOM_IMG_FMT_I42216: return "I42216";
1367 case AOM_IMG_FMT_I44416: return "I44416";
1368 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001369 default: return "Other";
1370 }
1371}
Ralph Giles061a16d2012-01-05 15:05:05 -06001372
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001373static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001374 struct AvxEncoderConfig *global,
1375 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001376#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001377 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001378
John Koleszar6ad3b742012-11-06 12:02:42 -08001379 if (stream->index == 0) {
1380 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001381 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001382 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001383 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001384 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001385 }
1386 if (stream->next || stream->index)
1387 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1388 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1389 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001390
John Koleszar6ad3b742012-11-06 12:02:42 -08001391 SHOW(g_usage);
1392 SHOW(g_threads);
1393 SHOW(g_profile);
1394 SHOW(g_w);
1395 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001396 SHOW(g_bit_depth);
1397 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001398 SHOW(g_timebase.num);
1399 SHOW(g_timebase.den);
1400 SHOW(g_error_resilient);
1401 SHOW(g_pass);
1402 SHOW(g_lag_in_frames);
1403 SHOW(rc_dropframe_thresh);
1404 SHOW(rc_resize_allowed);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001405 SHOW(rc_scaled_width);
1406 SHOW(rc_scaled_height);
John Koleszar6ad3b742012-11-06 12:02:42 -08001407 SHOW(rc_resize_up_thresh);
1408 SHOW(rc_resize_down_thresh);
1409 SHOW(rc_end_usage);
1410 SHOW(rc_target_bitrate);
1411 SHOW(rc_min_quantizer);
1412 SHOW(rc_max_quantizer);
1413 SHOW(rc_undershoot_pct);
1414 SHOW(rc_overshoot_pct);
1415 SHOW(rc_buf_sz);
1416 SHOW(rc_buf_initial_sz);
1417 SHOW(rc_buf_optimal_sz);
1418 SHOW(rc_2pass_vbr_bias_pct);
1419 SHOW(rc_2pass_vbr_minsection_pct);
1420 SHOW(rc_2pass_vbr_maxsection_pct);
1421 SHOW(kf_mode);
1422 SHOW(kf_min_dist);
1423 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001424}
1425
John Koleszar9e50ed72012-02-15 12:39:38 -08001426static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001427 struct AvxEncoderConfig *global,
1428 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001429 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001430 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001431
Yaowu Xuf883b422016-08-30 14:01:10 -07001432 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001433
John Koleszar6ad3b742012-11-06 12:02:42 -08001434 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001435
clang-format6c4d83e2016-08-08 19:03:30 -07001436 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001437
John Koleszar6ad3b742012-11-06 12:02:42 -08001438 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1439 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001440
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001441#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001442 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001443 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001444 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1445 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001446 }
James Zernc8e5a772016-02-11 18:53:50 -08001447#else
1448 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001449#endif
1450
1451 if (!stream->config.write_webm) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001452 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1453 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001454}
1455
John Koleszar9e50ed72012-02-15 12:39:38 -08001456static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001457 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001458 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001459
Yaowu Xuf883b422016-08-30 14:01:10 -07001460 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001461
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001462#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001463 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001464 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001465 }
1466#endif
1467
1468 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001469 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001470 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001471 stream->frames_out);
1472 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001473
John Koleszar6ad3b742012-11-06 12:02:42 -08001474 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001475}
1476
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001477static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001478 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001479 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001480 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001481 fatal("Failed to open statistics store");
1482 } else {
1483 if (!stats_open_mem(&stream->stats, pass))
1484 fatal("Failed to open statistics store");
1485 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001486
Pengchong Jinf349b072014-07-14 09:13:38 -07001487#if CONFIG_FP_MB_STATS
1488 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001489 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1490 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001491 fatal("Failed to open mb statistics store");
1492 } else {
1493 if (!stats_open_mem(&stream->fpmb_stats, pass))
1494 fatal("Failed to open mb statistics store");
1495 }
1496#endif
1497
John Koleszar6ad3b742012-11-06 12:02:42 -08001498 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001499 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1500 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001501 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001502 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001503#if CONFIG_FP_MB_STATS
1504 stream->config.cfg.rc_firstpass_mb_stats_in =
1505 stats_get(&stream->fpmb_stats);
1506#endif
1507 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001508
John Koleszar6ad3b742012-11-06 12:02:42 -08001509 stream->cx_time = 0;
1510 stream->nbytes = 0;
1511 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001512}
1513
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001514static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001515 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001516 int i;
1517 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001518
Yaowu Xuf883b422016-08-30 14:01:10 -07001519 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1520 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001521#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001522 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001523#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001524
John Koleszar6ad3b742012-11-06 12:02:42 -08001525 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001526 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001527 &stream->config.cfg, flags);
1528 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001529
Yaowu Xuf883b422016-08-30 14:01:10 -07001530 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001531 * we're being clever to store the control IDs in an array. Real
1532 * applications will want to make use of the enumerations directly
1533 */
1534 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1535 int ctrl = stream->config.arg_ctrls[i][0];
1536 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001537 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001538 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001539
John Koleszar6ad3b742012-11-06 12:02:42 -08001540 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1541 }
1542
John Koleszar5ebe94f2012-12-23 07:20:10 -08001543#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001544 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001545 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1546 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
1547 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001548
Yaowu Xuf883b422016-08-30 14:01:10 -07001549#if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
1550 if (strcmp(global->codec->name, "av1") == 0) {
1551 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001552 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1553
Yaowu Xuf883b422016-08-30 14:01:10 -07001554 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001555 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1556 }
1557#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001558 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001559#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001560}
1561
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001562static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001563 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001564 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001565 aom_codec_pts_t frame_start, next_frame_start;
1566 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1567 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001568
clang-format6c4d83e2016-08-08 19:03:30 -07001569 frame_start =
1570 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1571 cfg->g_timebase.num / global->framerate.num;
1572 next_frame_start =
1573 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1574 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001575
clang-format6c4d83e2016-08-08 19:03:30 -07001576/* Scale if necessary */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001577#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001578 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001579 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001580 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001581 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001582 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1583 exit(EXIT_FAILURE);
1584 }
1585#if CONFIG_LIBYUV
1586 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001587 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001588 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001589 }
clang-format6c4d83e2016-08-08 19:03:30 -07001590 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001591 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1592 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1593 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1594 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1595 stream->img->stride[AOM_PLANE_Y] / 2,
1596 (uint16 *)stream->img->planes[AOM_PLANE_U],
1597 stream->img->stride[AOM_PLANE_U] / 2,
1598 (uint16 *)stream->img->planes[AOM_PLANE_V],
1599 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001600 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001601 img = stream->img;
1602#else
clang-format6c4d83e2016-08-08 19:03:30 -07001603 stream->encoder.err = 1;
1604 ctx_exit_on_error(&stream->encoder,
1605 "Stream %d: Failed to encode frame.\n"
1606 "Scaling disabled in this configuration. \n"
1607 "To enable, configure with --enable-libyuv\n",
1608 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001609#endif
1610 }
1611 }
1612#endif
John Koleszar34882b92012-03-01 12:50:40 -08001613 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001614 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001615 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1616 exit(EXIT_FAILURE);
1617 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001618#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001619 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001620 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001621 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001622 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001623 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1624 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1625 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1626 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1627 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1628 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001629 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001630 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001631#else
1632 stream->encoder.err = 1;
1633 ctx_exit_on_error(&stream->encoder,
1634 "Stream %d: Failed to encode frame.\n"
1635 "Scaling disabled in this configuration. \n"
1636 "To enable, configure with --enable-libyuv\n",
1637 stream->index);
1638#endif
John Koleszar34882b92012-03-01 12:50:40 -08001639 }
1640
Yaowu Xuf883b422016-08-30 14:01:10 -07001641 aom_usec_timer_start(&timer);
1642 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001643 (unsigned long)(next_frame_start - frame_start), 0,
1644 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001645 aom_usec_timer_mark(&timer);
1646 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001647 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1648 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001649}
1650
John Koleszar6ad3b742012-11-06 12:02:42 -08001651static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001652 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001653 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001654
Yaowu Xuf883b422016-08-30 14:01:10 -07001655 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001656 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1657 stream->counts[q]++;
1658 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001659}
1660
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001661static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001662 struct AvxEncoderConfig *global, int *got_data) {
1663 const aom_codec_cx_pkt_t *pkt;
1664 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1665 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001666
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001667 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001668 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001669 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001670 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001671
John Koleszar6ad3b742012-11-06 12:02:42 -08001672 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001673 case AOM_CODEC_CX_FRAME_PKT:
1674 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001675 stream->frames_out++;
1676 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001677 if (!global->quiet)
1678 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001679
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001680 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001681#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001682 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001683 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001684 }
1685#endif
1686 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001687 if (pkt->data.frame.partition_id <= 0) {
1688 ivf_header_pos = ftello(stream->file);
1689 fsize = pkt->data.frame.sz;
1690
Dmitry Kovalev373b0f92014-01-29 17:57:21 -08001691 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001692 } else {
1693 fsize += pkt->data.frame.sz;
1694
Yaowu Xuf883b422016-08-30 14:01:10 -07001695 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
James Zernb6430362017-01-14 14:18:01 -08001696 const FileOffset currpos = ftello(stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001697 fseeko(stream->file, ivf_header_pos, SEEK_SET);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001698 ivf_write_frame_size(stream->file, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001699 fseeko(stream->file, currpos, SEEK_SET);
1700 }
1701 }
1702
clang-format6c4d83e2016-08-08 19:03:30 -07001703 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1704 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001705 }
1706 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001707
John Koleszar9e50ed72012-02-15 12:39:38 -08001708 *got_data = 1;
John Koleszar5ebe94f2012-12-23 07:20:10 -08001709#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001710 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001711 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001712 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001713 if (stream->decoder.err) {
1714 warn_or_exit_on_error(&stream->decoder,
1715 global->test_decode == TEST_DECODE_FATAL,
1716 "Failed to decode frame %d in stream %d",
1717 stream->frames_out + 1, stream->index);
1718 stream->mismatch_seen = stream->frames_out + 1;
1719 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001720 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001721#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001722 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001723 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001724 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001725 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001726 pkt->data.twopass_stats.sz);
1727 stream->nbytes += pkt->data.raw.sz;
1728 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001729#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001730 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001731 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001732 pkt->data.firstpass_mb_stats.sz);
1733 stream->nbytes += pkt->data.raw.sz;
1734 break;
1735#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001736 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001737
1738 if (global->show_psnr) {
1739 int i;
1740
1741 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1742 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1743 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001744 if (!global->quiet)
1745 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001746 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1747 }
1748 stream->psnr_count++;
1749 }
1750
1751 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001752 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001753 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001754 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001755}
1756
clang-format6c4d83e2016-08-08 19:03:30 -07001757static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001758 int i;
1759 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001760
clang-format6c4d83e2016-08-08 19:03:30 -07001761 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001762
John Koleszar6ad3b742012-11-06 12:02:42 -08001763 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001764 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001765 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001766 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001767
John Koleszar6ad3b742012-11-06 12:02:42 -08001768 for (i = 0; i < 4; i++) {
1769 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1770 }
1771 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001772}
1773
John Koleszar25b6e9f2013-02-12 21:17:56 -08001774static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001775 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001776}
1777
clang-format6c4d83e2016-08-08 19:03:30 -07001778static void test_decode(struct stream_state *stream,
John Koleszarb3c350a2013-03-13 12:15:43 -07001779 enum TestDecodeFatality fatal,
Yaowu Xuf883b422016-08-30 14:01:10 -07001780 const AvxInterface *codec) {
1781 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001782
clang-format6c4d83e2016-08-08 19:03:30 -07001783 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001784
John Koleszarb3c350a2013-03-13 12:15:43 -07001785 /* Get the internal reference frame */
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001786 if (strcmp(codec->name, "vp8") == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001787 struct aom_ref_frame ref_enc, ref_dec;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001788 const unsigned int frame_width = (stream->config.cfg.g_w + 15) & ~15;
1789 const unsigned int frame_height = (stream->config.cfg.g_h + 15) & ~15;
1790 aom_img_alloc(&ref_enc.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001791 enc_img = ref_enc.img;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001792 aom_img_alloc(&ref_dec.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001793 dec_img = ref_dec.img;
1794
Yaowu Xuf883b422016-08-30 14:01:10 -07001795 ref_enc.frame_type = AOM_LAST_FRAME;
1796 ref_dec.frame_type = AOM_LAST_FRAME;
1797 aom_codec_control(&stream->encoder, AOM_COPY_REFERENCE, &ref_enc);
1798 aom_codec_control(&stream->decoder, AOM_COPY_REFERENCE, &ref_dec);
John Koleszarb3c350a2013-03-13 12:15:43 -07001799 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001800 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1801 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001802
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001803#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001804 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1805 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1806 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1807 aom_image_t enc_hbd_img;
1808 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001809 enc_img.d_w, enc_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001810 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001811 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001812 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001813 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1814 aom_image_t dec_hbd_img;
1815 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001816 dec_img.d_w, dec_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001817 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001818 dec_img = dec_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001819 }
1820 }
1821#endif
John Koleszarb3c350a2013-03-13 12:15:43 -07001822 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001823 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001824 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001825
John Koleszarb3c350a2013-03-13 12:15:43 -07001826 if (!compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001827 int y[4], u[4], v[4];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001828#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001829 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001830 find_mismatch_high(&enc_img, &dec_img, y, u, v);
1831 } else {
1832 find_mismatch(&enc_img, &dec_img, y, u, v);
1833 }
1834#else
John Koleszarb3c350a2013-03-13 12:15:43 -07001835 find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001836#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001837 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001838 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001839 "Stream %d: Encode/decode mismatch on frame %d at"
1840 " Y[%d, %d] {%d/%d},"
1841 " U[%d, %d] {%d/%d},"
1842 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001843 stream->index, stream->frames_out, y[0], y[1], y[2],
1844 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 -08001845 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001846 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001847
Yaowu Xuf883b422016-08-30 14:01:10 -07001848 aom_img_free(&enc_img);
1849 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001850}
John Koleszarefd54f82012-02-13 16:52:18 -08001851
John Koleszar25b6e9f2013-02-12 21:17:56 -08001852static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001853 int64_t hours;
1854 int64_t mins;
1855 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001856
1857 if (etl >= 0) {
1858 hours = etl / 3600;
1859 etl -= hours * 3600;
1860 mins = etl / 60;
1861 etl -= mins * 60;
1862 secs = etl;
1863
clang-format6c4d83e2016-08-08 19:03:30 -07001864 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1865 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001866 } else {
1867 fprintf(stderr, "[%3s unknown] ", label);
1868 }
1869}
1870
Tom Finegan44dd3272013-11-20 17:18:28 -08001871int main(int argc, const char **argv_) {
1872 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001873 aom_image_t raw;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001874#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001875 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001876 int allocated_raw_shift = 0;
1877 int use_16bit_internal = 0;
1878 int input_shift = 0;
1879#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001880 int frame_avail, got_data;
1881
Yaowu Xuf883b422016-08-30 14:01:10 -07001882 struct AvxInputContext input;
1883 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001884 struct stream_state *streams = NULL;
1885 char **argv, **argi;
1886 uint64_t cx_time = 0;
1887 int stream_cnt = 0;
1888 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001889 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001890
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001891 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001892 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001893
clang-format6c4d83e2016-08-08 19:03:30 -07001894 if (argc < 3) usage_exit();
John Koleszar6ad3b742012-11-06 12:02:42 -08001895
1896 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001897 input.framerate.numerator = 30;
1898 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001899 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001900 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001901
1902 /* First parse the global configuration values, because we want to apply
1903 * other parameters on top of the default configuration provided by the
1904 * codec.
1905 */
1906 argv = argv_dup(argc - 1, argv_ + 1);
1907 parse_global_config(&global, argv);
1908
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001909 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001910 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1911 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1912 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1913 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1914 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001915 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001916
John Koleszar6ad3b742012-11-06 12:02:42 -08001917 {
1918 /* Now parse each stream's parameters. Using a local scope here
1919 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1920 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001921 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001922 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001923
John Koleszar6ad3b742012-11-06 12:02:42 -08001924 do {
1925 stream = new_stream(&global, stream);
1926 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001927 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001928 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001929 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001930
John Koleszarc6b90392012-07-13 15:21:29 -07001931 /* Check for unrecognized options */
1932 for (argi = argv; *argi; argi++)
1933 if (argi[0][0] == '-' && argi[0][1])
1934 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001935
clang-format6c4d83e2016-08-08 19:03:30 -07001936 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1937 &stream->config.cfg););
Tom Finegan249366b2013-11-25 12:05:19 -08001938
John Koleszarc6b90392012-07-13 15:21:29 -07001939 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001940 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001941
clang-format6c4d83e2016-08-08 19:03:30 -07001942 if (!input.filename) usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04001943
John Koleszar8dd82872013-05-06 11:01:35 -07001944 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001945 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001946
John Koleszar6ad3b742012-11-06 12:02:42 -08001947 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001948 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001949 int64_t estimated_time_left = -1;
1950 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001951 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001952
John Koleszar6ad3b742012-11-06 12:02:42 -08001953 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001954
John Koleszar6ad3b742012-11-06 12:02:42 -08001955 /* If the input file doesn't specify its w/h (raw files), try to get
1956 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001957 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001958 if (!input.width || !input.height) {
1959 FOREACH_STREAM({
1960 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1961 input.width = stream->config.cfg.g_w;
1962 input.height = stream->config.cfg.g_h;
1963 break;
1964 }
1965 });
1966 }
John Koleszarc6b90392012-07-13 15:21:29 -07001967
John Koleszar6ad3b742012-11-06 12:02:42 -08001968 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001969 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001970 fatal(
1971 "Specify stream dimensions with --width (-w) "
1972 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001973
1974 /* If input file does not specify bit-depth but input-bit-depth parameter
1975 * exists, assume that to be the input bit-depth. However, if the
1976 * input-bit-depth paramter does not exist, assume the input bit-depth
1977 * to be the same as the codec bit-depth.
1978 */
1979 if (!input.bit_depth) {
1980 FOREACH_STREAM({
1981 if (stream->config.cfg.g_input_bit_depth)
1982 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1983 else
1984 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1985 (int)stream->config.cfg.g_bit_depth;
1986 });
Yaowu Xuf883b422016-08-30 14:01:10 -07001987 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001988 } else {
clang-format6c4d83e2016-08-08 19:03:30 -07001989 FOREACH_STREAM(
1990 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001991 }
1992
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001993#if CONFIG_HIGHBITDEPTH
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001994 FOREACH_STREAM({
Thomas Daede8ec53b22016-09-19 13:24:51 -07001995 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
1996 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
1997 was selected. */
1998 switch (stream->config.cfg.g_profile) {
1999 case 0:
2000 stream->config.cfg.g_profile = 1;
2001 profile_updated = 1;
2002 break;
2003 case 2:
2004 stream->config.cfg.g_profile = 3;
2005 profile_updated = 1;
2006 break;
2007 default: break;
2008 }
2009 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002010 /* Automatically set the codec bit depth to match the input bit depth.
2011 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002012 if (stream->config.cfg.g_input_bit_depth >
2013 (unsigned int)stream->config.cfg.g_bit_depth) {
2014 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2015 }
2016 if (stream->config.cfg.g_bit_depth > 8) {
2017 switch (stream->config.cfg.g_profile) {
2018 case 0:
2019 stream->config.cfg.g_profile = 2;
2020 profile_updated = 1;
2021 break;
2022 case 1:
2023 stream->config.cfg.g_profile = 3;
2024 profile_updated = 1;
2025 break;
2026 default: break;
2027 }
2028 }
2029 if (stream->config.cfg.g_profile > 1) {
2030 stream->config.use_16bit_internal = 1;
2031 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002032 if (profile_updated && !global.quiet) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002033 fprintf(stderr,
2034 "Warning: automatically upgrading to profile %d to "
2035 "match input format.\n",
2036 stream->config.cfg.g_profile);
2037 }
2038 });
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002039#else
2040 FOREACH_STREAM({
2041 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2042 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2043 was selected. */
2044 switch (stream->config.cfg.g_profile) {
2045 case 0:
2046 stream->config.cfg.g_profile = 1;
2047 profile_updated = 1;
2048 break;
2049 case 2:
2050 stream->config.cfg.g_profile = 3;
2051 profile_updated = 1;
2052 break;
2053 default: break;
2054 }
2055 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002056 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002057 fprintf(stderr,
2058 "Warning: automatically upgrading to profile %d to "
2059 "match input format.\n",
2060 stream->config.cfg.g_profile);
2061 }
2062 });
2063#endif
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002064
Tom Finegan00a35aa2013-11-14 12:37:42 -08002065 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
Alex Conversed66bd222014-02-21 10:52:09 -08002066 FOREACH_STREAM(validate_stream_config(stream, &global));
John Koleszar77e6b452010-11-03 13:58:40 -04002067
John Koleszar6ad3b742012-11-06 12:02:42 -08002068 /* Ensure that --passes and --pass are consistent. If --pass is set and
2069 * --passes=2, ensure --fpf was set.
2070 */
2071 if (global.pass && global.passes == 2)
clang-format6c4d83e2016-08-08 19:03:30 -07002072 FOREACH_STREAM({
2073 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002074 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002075 " and --passes=2\n",
2076 stream->index, global.pass);
2077 });
John Koleszar3245d462010-06-10 17:10:12 -04002078
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002079#if !CONFIG_WEBM_IO
2080 FOREACH_STREAM({
Ronald S. Bultje39775072015-12-16 13:35:43 -05002081 if (stream->config.write_webm) {
2082 stream->config.write_webm = 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002083 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002084 "aomenc was compiled without WebM container support."
clang-format6c4d83e2016-08-08 19:03:30 -07002085 "Producing IVF output");
Ronald S. Bultje39775072015-12-16 13:35:43 -05002086 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002087 });
2088#endif
2089
John Koleszar6ad3b742012-11-06 12:02:42 -08002090 /* Use the frame rate from the file only if none was specified
2091 * on the command-line.
2092 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002093 if (!global.have_framerate) {
2094 global.framerate.num = input.framerate.numerator;
2095 global.framerate.den = input.framerate.denominator;
Ronald S. Bultje9134e9f2016-01-18 14:03:45 -05002096 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
2097 stream->config.cfg.g_timebase.num = global.framerate.den);
Tom Finegan00a35aa2013-11-14 12:37:42 -08002098 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002099
John Koleszar6ad3b742012-11-06 12:02:42 -08002100 /* Show configuration */
2101 if (global.verbose && pass == 0)
2102 FOREACH_STREAM(show_stream_config(stream, &global, &input));
2103
2104 if (pass == (global.pass ? global.pass - 1 : 0)) {
2105 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002106 /*The Y4M reader does its own allocation.
2107 Just initialize this here to avoid problems if we never read any
2108 frames.*/
2109 memset(&raw, 0, sizeof(raw));
2110 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002111 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002112
clang-format6c4d83e2016-08-08 19:03:30 -07002113 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
2114 &stream->config.cfg, &global.framerate));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002115 }
2116
John Koleszar6ad3b742012-11-06 12:02:42 -08002117 FOREACH_STREAM(setup_pass(stream, &global, pass));
clang-format6c4d83e2016-08-08 19:03:30 -07002118 FOREACH_STREAM(
2119 open_output_file(stream, &global, &input.pixel_aspect_ratio));
John Koleszar6ad3b742012-11-06 12:02:42 -08002120 FOREACH_STREAM(initialize_encoder(stream, &global));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002121
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002122#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002123 if (strcmp(global.codec->name, "av1") == 0 ||
2124 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002125 // Check to see if at least one stream uses 16 bit internal.
2126 // Currently assume that the bit_depths for all streams using
2127 // highbitdepth are the same.
2128 FOREACH_STREAM({
2129 if (stream->config.use_16bit_internal) {
2130 use_16bit_internal = 1;
2131 }
2132 if (stream->config.cfg.g_profile == 0) {
2133 input_shift = 0;
2134 } else {
2135 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07002136 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002137 }
2138 });
2139 }
2140#endif
2141
John Koleszarc6b90392012-07-13 15:21:29 -07002142 frame_avail = 1;
2143 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002144
John Koleszarc6b90392012-07-13 15:21:29 -07002145 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002146 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002147
John Koleszar6ad3b742012-11-06 12:02:42 -08002148 if (!global.limit || frames_in < global.limit) {
2149 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002150
clang-format6c4d83e2016-08-08 19:03:30 -07002151 if (frame_avail) frames_in++;
2152 seen_frames =
2153 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002154
John Koleszar6ad3b742012-11-06 12:02:42 -08002155 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002156 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002157 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2158
John Koleszar6ad3b742012-11-06 12:02:42 -08002159 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002160 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2161 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002162 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002163 fprintf(stderr, "frame %4d ", frames_in);
2164
clang-format6c4d83e2016-08-08 19:03:30 -07002165 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002166 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002167 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002168 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002169 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002170 }
2171
hui su251e1512016-10-03 15:48:43 -07002172 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002173 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002174 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002175
John Koleszar6ad3b742012-11-06 12:02:42 -08002176 if (frames_in > global.skip_frames) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002177#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002178 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002179 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2180 assert(use_16bit_internal);
2181 // Input bit depth and stream bit depth do not match, so up
2182 // shift frame to stream bit depth
2183 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002184 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002185 input.width, input.height, 32);
2186 allocated_raw_shift = 1;
2187 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002188 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002189 frame_to_encode = &raw_shift;
2190 } else {
2191 frame_to_encode = &raw;
2192 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002193 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002194 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002195 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002196 FOREACH_STREAM({
2197 if (stream->config.use_16bit_internal)
2198 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002199 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002200 else
2201 assert(0);
2202 });
2203 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002204 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002205 FOREACH_STREAM(encode_frame(stream, &global,
2206 frame_avail ? frame_to_encode : NULL,
2207 frames_in));
2208 }
2209#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002210 aom_usec_timer_start(&timer);
clang-format6c4d83e2016-08-08 19:03:30 -07002211 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
John Koleszar6ad3b742012-11-06 12:02:42 -08002212 frames_in));
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002213#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002214 aom_usec_timer_mark(&timer);
2215 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002216
John Koleszar6ad3b742012-11-06 12:02:42 -08002217 FOREACH_STREAM(update_quantizer_histogram(stream));
John Koleszarc6b90392012-07-13 15:21:29 -07002218
John Koleszar0ea50ce2010-05-18 11:58:33 -04002219 got_data = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002220 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002221
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002222 if (!got_data && input.length && streams != NULL &&
2223 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002224 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002225 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002226 int64_t remaining;
2227 int64_t rate;
2228
2229 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002230 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002231
2232 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002233 remaining = 1000 * (global.limit - global.skip_frames -
2234 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002235 } else {
James Zerne3578af2014-04-17 10:47:08 -07002236 const int64_t input_pos = ftello(input.file);
2237 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002238 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002239
2240 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002241 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002242 }
2243
clang-format6c4d83e2016-08-08 19:03:30 -07002244 average_rate =
2245 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002246 estimated_time_left = average_rate ? remaining / average_rate : -1;
2247 }
2248
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002249 if (got_data && global.test_decode != TEST_DECODE_OFF)
John Koleszarb3c350a2013-03-13 12:15:43 -07002250 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
John Koleszarc6b90392012-07-13 15:21:29 -07002251 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002252
John Koleszarc6b90392012-07-13 15:21:29 -07002253 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002254 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002255 }
2256
clang-format6c4d83e2016-08-08 19:03:30 -07002257 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002258
Deb Mukherjeea160d722014-09-30 21:56:33 -07002259 if (!global.quiet) {
clang-format6c4d83e2016-08-08 19:03:30 -07002260 FOREACH_STREAM(fprintf(
2261 stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2262 "b/f %7" PRId64 "b/s"
2263 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2264 pass + 1, global.passes, frames_in, stream->frames_out,
2265 (int64_t)stream->nbytes,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002266 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
clang-format6c4d83e2016-08-08 19:03:30 -07002267 seen_frames
2268 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2269 global.framerate.den / seen_frames
2270 : 0,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002271 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2272 stream->cx_time > 9999999 ? "ms" : "us",
2273 usec_to_fps(stream->cx_time, seen_frames)));
2274 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002275
Deb Mukherjeea160d722014-09-30 21:56:33 -07002276 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002277 if (global.codec->fourcc == AV1_FOURCC) {
Deb Mukherjeea160d722014-09-30 21:56:33 -07002278 FOREACH_STREAM(
2279 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2280 } else {
2281 FOREACH_STREAM(show_psnr(stream, 255.0));
2282 }
2283 }
John Koleszarc6b90392012-07-13 15:21:29 -07002284
Yaowu Xuf883b422016-08-30 14:01:10 -07002285 FOREACH_STREAM(aom_codec_destroy(&stream->encoder));
John Koleszar6ad3b742012-11-06 12:02:42 -08002286
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002287 if (global.test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002288 FOREACH_STREAM(aom_codec_destroy(&stream->decoder));
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002289 }
2290
John Koleszar6ad3b742012-11-06 12:02:42 -08002291 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002292
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002293 if (global.test_decode == TEST_DECODE_FATAL) {
2294 FOREACH_STREAM(res |= stream->mismatch_seen);
2295 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002296 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002297
John Koleszar6ad3b742012-11-06 12:02:42 -08002298 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
John Koleszarc6b90392012-07-13 15:21:29 -07002299
Pengchong Jinf349b072014-07-14 09:13:38 -07002300#if CONFIG_FP_MB_STATS
2301 FOREACH_STREAM(stats_close(&stream->fpmb_stats, global.passes - 1));
2302#endif
2303
clang-format6c4d83e2016-08-08 19:03:30 -07002304 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002305 }
2306
John Koleszar6ad3b742012-11-06 12:02:42 -08002307 if (global.show_q_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002308 FOREACH_STREAM(
2309 show_q_histogram(stream->counts, global.show_q_hist_buckets));
John Koleszar6ad3b742012-11-06 12:02:42 -08002310
2311 if (global.show_rate_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002312 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
John Koleszar6ad3b742012-11-06 12:02:42 -08002313 global.show_rate_hist_buckets));
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08002314 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
John Koleszar6ad3b742012-11-06 12:02:42 -08002315
Ronald S. Bultje31214972012-10-11 10:13:48 -07002316#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002317 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2318 * to match some existing utilities.
2319 */
Yaowu Xu47e784e2013-10-16 16:29:28 -07002320 if (!(global.pass == 1 && global.passes == 2))
2321 FOREACH_STREAM({
2322 FILE *f = fopen("opsnr.stt", "a");
2323 if (stream->mismatch_seen) {
2324 fprintf(f, "First mismatch occurred in frame %d\n",
2325 stream->mismatch_seen);
2326 } else {
2327 fprintf(f, "No mismatch detected in recon buffers\n");
2328 }
2329 fclose(f);
2330 });
Ronald S. Bultje31214972012-10-11 10:13:48 -07002331#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002332
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002333#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002334 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002335#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002336 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002337 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002338 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002339 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002340}