blob: 1103716df33a4bef8822c55cee0088a56dc2d13f [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"
Tom Fineganba02c242017-05-16 15:01:54 -070028#if CONFIG_AV1_DECODER
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"
Urvang Joshi09c293e2017-04-20 17:56:27 -070035#include "examples/encoder_util.h"
Tom Finegan03848f52013-11-05 10:02:18 -080036
Yaowu Xuf883b422016-08-30 14:01:10 -070037#if CONFIG_AV1_ENCODER
38#include "aom/aomcx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080039#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070040#if CONFIG_AV1_DECODER
41#include "aom/aomdx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080042#endif
43
Yaowu Xuf883b422016-08-30 14:01:10 -070044#include "./aomstats.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080045#include "./rate_hist.h"
Tom Finegan249366b2013-11-25 12:05:19 -080046#include "./warnings.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080047#include "aom/aom_integer.h"
hui suefdad1f2017-05-16 18:01:25 -070048#include "aom_dsp/aom_dsp_common.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080049#include "aom_ports/aom_timer.h"
50#include "aom_ports/mem_ops.h"
James Zerndba73762014-05-10 17:44:12 -070051#if CONFIG_WEBM_IO
Tom Finegan78cb2e62013-11-07 21:28:45 -080052#include "./webmenc.h"
James Zerndba73762014-05-10 17:44:12 -070053#endif
Tom Finegan78cb2e62013-11-07 21:28:45 -080054#include "./y4minput.h"
Tom Finegan03848f52013-11-05 10:02:18 -080055
John Koleszar6291dd42012-06-19 21:05:36 -070056/* Swallow warnings about unused results of fread/fwrite */
clang-format6c4d83e2016-08-08 19:03:30 -070057static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
John Koleszar6ad3b742012-11-06 12:02:42 -080058 return fread(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070059}
60#define fread wrap_fread
61
62static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080063 FILE *stream) {
64 return fwrite(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070065}
66#define fwrite wrap_fwrite
67
John Koleszar0ea50ce2010-05-18 11:58:33 -040068static const char *exec_name;
69
Yaowu Xuf883b422016-08-30 14:01:10 -070070static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080071 const char *s, va_list ap) {
John Koleszarc6b90392012-07-13 15:21:29 -070072 if (ctx->err) {
Yaowu Xuf883b422016-08-30 14:01:10 -070073 const char *detail = aom_codec_error_detail(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -040074
John Koleszar6ad3b742012-11-06 12:02:42 -080075 vfprintf(stderr, s, ap);
Yaowu Xuf883b422016-08-30 14:01:10 -070076 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -040077
clang-format6c4d83e2016-08-08 19:03:30 -070078 if (detail) fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -040079
clang-format6c4d83e2016-08-08 19:03:30 -070080 if (fatal) exit(EXIT_FAILURE);
John Koleszarc6b90392012-07-13 15:21:29 -070081 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040082}
83
Yaowu Xuf883b422016-08-30 14:01:10 -070084static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080085 va_list ap;
86
87 va_start(ap, s);
88 warn_or_exit_on_errorv(ctx, 1, s, ap);
89 va_end(ap);
90}
91
Yaowu Xuf883b422016-08-30 14:01:10 -070092static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080093 const char *s, ...) {
94 va_list ap;
95
96 va_start(ap, s);
97 warn_or_exit_on_errorv(ctx, fatal, s, ap);
98 va_end(ap);
99}
100
Yaowu Xuf883b422016-08-30 14:01:10 -0700101static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800102 FILE *f = input_ctx->file;
103 y4m_input *y4m = &input_ctx->y4m;
John Koleszarc6b90392012-07-13 15:21:29 -0700104 int shortread = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400105
Tom Finegan00a35aa2013-11-14 12:37:42 -0800106 if (input_ctx->file_type == FILE_TYPE_Y4M) {
clang-format6c4d83e2016-08-08 19:03:30 -0700107 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700108 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800109 shortread = read_yuv_frame(input_ctx, img);
John Koleszarc6b90392012-07-13 15:21:29 -0700110 }
111
112 return !shortread;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400113}
114
James Zern5c337fd2015-05-09 10:42:58 -0700115static int file_is_y4m(const char detect[4]) {
John Koleszarc6b90392012-07-13 15:21:29 -0700116 if (memcmp(detect, "YUV4", 4) == 0) {
117 return 1;
118 }
119 return 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400120}
121
James Zern5c337fd2015-05-09 10:42:58 -0700122static int fourcc_is_ivf(const char detect[4]) {
Alex Converse64b89f12014-01-06 16:29:09 -0800123 if (memcmp(detect, "DKIF", 4) == 0) {
124 return 1;
125 }
126 return 0;
127}
John Koleszardc666302010-10-20 12:05:48 -0400128
James Zernfd8c78c2017-11-27 16:37:33 -0800129static const arg_def_t help =
130 ARG_DEF(NULL, "help", 0, "Show usage options and exit");
clang-format6c4d83e2016-08-08 19:03:30 -0700131static const arg_def_t debugmode =
132 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
133static const arg_def_t outputfile =
134 ARG_DEF("o", "output", 1, "Output filename");
135static const arg_def_t use_yv12 =
136 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
137static const arg_def_t use_i420 =
138 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
139static const arg_def_t use_i422 =
140 ARG_DEF(NULL, "i422", 0, "Input file is I422");
141static const arg_def_t use_i444 =
142 ARG_DEF(NULL, "i444", 0, "Input file is I444");
143static const arg_def_t use_i440 =
144 ARG_DEF(NULL, "i440", 0, "Input file is I440");
145static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
146static const arg_def_t passes =
147 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
148static const arg_def_t pass_arg =
149 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
150static const arg_def_t fpf_name =
151 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700152#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700153static const arg_def_t fpmbf_name =
154 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700155#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700156static const arg_def_t limit =
157 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
158static const arg_def_t skip =
159 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
160static const arg_def_t deadline =
161 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
clang-format6c4d83e2016-08-08 19:03:30 -0700162static const arg_def_t good_dl =
163 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
clang-format6c4d83e2016-08-08 19:03:30 -0700164static const arg_def_t quietarg =
165 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
166static const arg_def_t verbosearg =
167 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
168static const arg_def_t psnrarg =
169 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800170
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800171static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700172 { "off", TEST_DECODE_OFF },
173 { "fatal", TEST_DECODE_FATAL },
174 { "warn", TEST_DECODE_WARN },
175 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800176};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700177static const arg_def_t recontest = ARG_DEF_ENUM(
178 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700179static const arg_def_t framerate =
180 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
181static const arg_def_t use_webm =
182 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
183static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
Cyril Concolato6c788832017-10-30 16:30:35 -0700184#if CONFIG_OBU_NO_IVF
185static const arg_def_t use_obu = ARG_DEF(NULL, "obu", 0, "Output OBU");
186#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700187static const arg_def_t out_part =
188 ARG_DEF("P", "output-partitions", 0,
189 "Makes encoder output partitions. Requires IVF output!");
190static const arg_def_t q_hist_n =
191 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
192static const arg_def_t rate_hist_n =
193 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
194static const arg_def_t disable_warnings =
195 ARG_DEF(NULL, "disable-warnings", 0,
196 "Disable warnings about potentially incorrect encode settings.");
197static const arg_def_t disable_warning_prompt =
198 ARG_DEF("y", "disable-warning-prompt", 0,
199 "Display warnings, but do not prompt user to continue.");
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400200static const struct arg_enum_list bitdepth_enum[] = {
201 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
202};
203
204static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
205 "b", "bit-depth", 1,
206 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
207 bitdepth_enum);
208static const arg_def_t inbitdeptharg =
209 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
James Zernfd8c78c2017-11-27 16:37:33 -0800210static const arg_def_t *main_args[] = { &help,
211 &debugmode,
clang-format6c4d83e2016-08-08 19:03:30 -0700212 &outputfile,
213 &codecarg,
214 &passes,
215 &pass_arg,
216 &fpf_name,
217 &limit,
218 &skip,
219 &deadline,
clang-format6c4d83e2016-08-08 19:03:30 -0700220 &good_dl,
clang-format6c4d83e2016-08-08 19:03:30 -0700221 &quietarg,
222 &verbosearg,
223 &psnrarg,
224 &use_webm,
225 &use_ivf,
Cyril Concolato6c788832017-10-30 16:30:35 -0700226#if CONFIG_OBU_NO_IVF
227 &use_obu,
228#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700229 &out_part,
230 &q_hist_n,
231 &rate_hist_n,
232 &disable_warnings,
233 &disable_warning_prompt,
234 &recontest,
235 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400236
clang-format6c4d83e2016-08-08 19:03:30 -0700237static const arg_def_t usage =
238 ARG_DEF("u", "usage", 1, "Usage profile number to use");
239static const arg_def_t threads =
240 ARG_DEF("t", "threads", 1, "Max number of threads to use");
241static const arg_def_t profile =
242 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700243static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
244static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700245#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700246static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700247 { "mono", STEREO_FORMAT_MONO },
248 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
249 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
250 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
251 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
252 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700253};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700254static const arg_def_t stereo_mode = ARG_DEF_ENUM(
255 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700256#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700257static const arg_def_t timebase = ARG_DEF(
258 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700259static const arg_def_t error_resilient =
260 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
261static const arg_def_t lag_in_frames =
262 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700263#if CONFIG_EXT_TILE
264static const arg_def_t large_scale_tile =
265 ARG_DEF(NULL, "large-scale-tile", 1,
266 "Large scale tile coding (0: off (default), 1: on)");
267#endif // CONFIG_EXT_TILE
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800268#if CONFIG_MONO_VIDEO
269static const arg_def_t monochrome =
270 ARG_DEF(NULL, "monochrome", 0, "Monochrome video (no chroma planes)");
271#endif // CONFIG_MONO_VIDEO
John Koleszar0ea50ce2010-05-18 11:58:33 -0400272
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700273static const arg_def_t *global_args[] = { &use_yv12,
274 &use_i420,
275 &use_i422,
276 &use_i444,
277 &use_i440,
278 &usage,
279 &threads,
280 &profile,
281 &width,
282 &height,
James Zerndba73762014-05-10 17:44:12 -0700283#if CONFIG_WEBM_IO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700284 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700285#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700286 &timebase,
287 &framerate,
288 &error_resilient,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700289 &bitdeptharg,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700290 &lag_in_frames,
291#if CONFIG_EXT_TILE
292 &large_scale_tile,
293#endif // CONFIG_EXT_TILE
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800294#if CONFIG_MONO_VIDEO
295 &monochrome,
296#endif // CONFIG_MONO_VIDEO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700297 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400298
clang-format6c4d83e2016-08-08 19:03:30 -0700299static const arg_def_t dropframe_thresh =
300 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700301static const arg_def_t resize_mode =
302 ARG_DEF(NULL, "resize-mode", 1, "Frame resize mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700303static const arg_def_t resize_denominator =
304 ARG_DEF(NULL, "resize-denominator", 1, "Frame resize denominator");
305static const arg_def_t resize_kf_denominator = ARG_DEF(
306 NULL, "resize-kf-denominator", 1, "Frame resize keyframe denominator");
Urvang Joshi94ad3702017-12-06 11:38:08 -0800307#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700308static const arg_def_t superres_mode =
309 ARG_DEF(NULL, "superres-mode", 1, "Frame super-resolution mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700310static const arg_def_t superres_denominator = ARG_DEF(
311 NULL, "superres-denominator", 1, "Frame super-resolution denominator");
312static const arg_def_t superres_kf_denominator =
313 ARG_DEF(NULL, "superres-kf-denominator", 1,
314 "Frame super-resolution keyframe denominator");
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700315static const arg_def_t superres_qthresh = ARG_DEF(
316 NULL, "superres-qthresh", 1, "Frame super-resolution qindex threshold");
317static const arg_def_t superres_kf_qthresh =
318 ARG_DEF(NULL, "superres-kf-qthresh", 1,
319 "Frame super-resolution keyframe qindex threshold");
Urvang Joshi94ad3702017-12-06 11:38:08 -0800320#endif // CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700321static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
322 { "cbr", AOM_CBR },
323 { "cq", AOM_CQ },
324 { "q", AOM_Q },
325 { NULL, 0 } };
clang-format6c4d83e2016-08-08 19:03:30 -0700326static const arg_def_t end_usage =
327 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
328static const arg_def_t target_bitrate =
329 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
330static const arg_def_t min_quantizer =
331 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
332static const arg_def_t max_quantizer =
333 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
334static const arg_def_t undershoot_pct =
335 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
336static const arg_def_t overshoot_pct =
337 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
338static const arg_def_t buf_sz =
339 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
340static const arg_def_t buf_initial_sz =
341 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
342static const arg_def_t buf_optimal_sz =
343 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700344static const arg_def_t *rc_args[] = { &dropframe_thresh,
345 &resize_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700346 &resize_denominator,
347 &resize_kf_denominator,
Urvang Joshi94ad3702017-12-06 11:38:08 -0800348#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700349 &superres_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700350 &superres_denominator,
351 &superres_kf_denominator,
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700352 &superres_qthresh,
353 &superres_kf_qthresh,
Urvang Joshi94ad3702017-12-06 11:38:08 -0800354#endif // CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700355 &end_usage,
356 &target_bitrate,
357 &min_quantizer,
358 &max_quantizer,
359 &undershoot_pct,
360 &overshoot_pct,
361 &buf_sz,
362 &buf_initial_sz,
363 &buf_optimal_sz,
364 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400365
clang-format6c4d83e2016-08-08 19:03:30 -0700366static const arg_def_t bias_pct =
367 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
368static const arg_def_t minsection_pct =
369 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
370static const arg_def_t maxsection_pct =
371 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
372static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
373 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400374
clang-format6c4d83e2016-08-08 19:03:30 -0700375static const arg_def_t kf_min_dist =
376 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
377static const arg_def_t kf_max_dist =
378 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
379static const arg_def_t kf_disabled =
380 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
381static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
382 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400383
clang-format6c4d83e2016-08-08 19:03:30 -0700384static const arg_def_t noise_sens =
385 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
386static const arg_def_t sharpness =
387 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
388static const arg_def_t static_thresh =
389 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
390static const arg_def_t auto_altref =
391 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
392static const arg_def_t arnr_maxframes =
393 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
394static const arg_def_t arnr_strength =
395 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500396static const struct arg_enum_list tuning_enum[] = {
Yushin Chod808bfc2017-08-10 15:54:36 -0700397 { "psnr", AOM_TUNE_PSNR },
398 { "ssim", AOM_TUNE_SSIM },
399#ifdef CONFIG_DIST_8X8
400 { "cdef-dist", AOM_TUNE_CDEF_DIST },
401 { "daala-dist", AOM_TUNE_DAALA_DIST },
402#endif
403 { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500404};
Yushin Chod808bfc2017-08-10 15:54:36 -0700405static const arg_def_t tune_metric =
406 ARG_DEF_ENUM(NULL, "tune", 1, "Distortion metric tuned with", tuning_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700407static const arg_def_t cq_level =
408 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
409static const arg_def_t max_intra_rate_pct =
410 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800411
Yaowu Xuf883b422016-08-30 14:01:10 -0700412#if CONFIG_AV1_ENCODER
413static const arg_def_t cpu_used_av1 =
Yaowu Xu39737592017-04-21 16:39:09 -0700414 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800415static const arg_def_t dev_sf_av1 =
416 ARG_DEF(NULL, "dev-sf", 1, "Dev Speed (0..255)");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700417#if CONFIG_EXT_TILE
418static const arg_def_t single_tile_decoding =
419 ARG_DEF(NULL, "single-tile-decoding", 1,
420 "Single tile decoding (0: off (default), 1: on)");
421#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700422static const arg_def_t tile_cols =
423 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
424static const arg_def_t tile_rows =
425 ARG_DEF(NULL, "tile-rows", 1,
426 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200427#if CONFIG_MAX_TILE
428static const arg_def_t tile_width =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200429 ARG_DEF(NULL, "tile-width", 1, "Tile widths (comma separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200430static const arg_def_t tile_height =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200431 ARG_DEF(NULL, "tile-height", 1, "Tile heights (command separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200432#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800433#if CONFIG_DEPENDENT_HORZTILES
434static const arg_def_t tile_dependent_rows =
435 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
436#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800437#if CONFIG_LOOPFILTERING_ACROSS_TILES
Lei7bb501d2017-12-13 15:10:34 -0800438#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
439static const arg_def_t tile_loopfilter_v =
440 ARG_DEF(NULL, "tile-loopfilter-v", 1,
441 "Enable loop filter across vertical tile boundary");
442static const arg_def_t tile_loopfilter_h =
443 ARG_DEF(NULL, "tile-loopfilter-h", 1,
444 "Enable loop filter across horizontal tile boundary");
445#else
Ryan Lei7386eda2016-12-08 21:08:31 -0800446static const arg_def_t tile_loopfilter = ARG_DEF(
447 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Lei7bb501d2017-12-13 15:10:34 -0800448#endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800449#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700450static const arg_def_t lossless =
451 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100452static const arg_def_t enable_cdef =
453 ARG_DEF(NULL, "enable-cdef", 1,
454 "Enable the constrained directional enhancement filter (0: false, "
455 "1: true (default))");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700456#if CONFIG_AOM_QM
457static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800458 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700459 "Enable quantisation matrices (0: false (default), 1: true)");
460static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800461 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700462static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800463 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700464#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700465#if CONFIG_DIST_8X8
466static const arg_def_t enable_dist_8x8 =
467 ARG_DEF(NULL, "enable-dist-8x8", 1,
468 "Enable dist-8x8 (0: false (default), 1: true)");
469#endif // CONFIG_DIST_8X8
Yaowu Xu859a5272016-11-10 15:32:21 -0800470static const arg_def_t num_tg = ARG_DEF(
471 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000472static const arg_def_t mtu_size =
473 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800474 "MTU size for a tile group, default is 0 (no MTU targeting), "
475 "overrides maximum number of tile groups");
Fangwen Fu8d164de2016-12-14 13:40:54 -0800476#if CONFIG_TEMPMV_SIGNALING
477static const arg_def_t disable_tempmv = ARG_DEF(
478 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
479#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700480static const arg_def_t frame_parallel_decoding =
481 ARG_DEF(NULL, "frame-parallel", 1,
482 "Enable frame parallel decodability features "
483 "(0: false (default), 1: true)");
Thomas Davies3ab20b42017-09-19 10:30:53 +0100484#if !CONFIG_EXT_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100485static const arg_def_t aq_mode = ARG_DEF(
486 NULL, "aq-mode", 1,
487 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100488 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200489#else
Thomase28d92b2016-09-20 12:07:11 +0100490static const arg_def_t aq_mode = ARG_DEF(
491 NULL, "aq-mode", 1,
492 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100493 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200494#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700495#if CONFIG_EXT_DELTA_Q
496static const arg_def_t deltaq_mode = ARG_DEF(
497 NULL, "deltaq-mode", 1,
498 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
499#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700500static const arg_def_t frame_periodic_boost =
501 ARG_DEF(NULL, "frame-boost", 1,
502 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700503static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
504 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700505static const arg_def_t max_inter_rate_pct =
506 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700507static const arg_def_t min_gf_interval = ARG_DEF(
508 NULL, "min-gf-interval", 1,
509 "min gf/arf frame interval (default 0, indicating in-built behavior)");
510static const arg_def_t max_gf_interval = ARG_DEF(
511 NULL, "max-gf-interval", 1,
512 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800513
Andrey Norkin9e694632017-12-21 18:50:57 -0800514#if CONFIG_CICP
515static const struct arg_enum_list color_primaries_enum[] = {
516 { "bt709", AOM_CICP_CP_BT_709 },
517 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
518 { "bt601", AOM_CICP_CP_BT_601 },
519 { "bt470m", AOM_CICP_CP_BT_470_M },
520 { "bt470bg", AOM_CICP_CP_BT_470_B_G },
521 { "smpte240", AOM_CICP_CP_SMPTE_240 },
522 { "film", AOM_CICP_CP_GENERIC_FILM },
523 { "bt2020", AOM_CICP_CP_BT_2020 },
524 { "xyz", AOM_CICP_CP_XYZ },
525 { "smpte431", AOM_CICP_CP_SMPTE_431 },
526 { "smpte432", AOM_CICP_CP_SMPTE_432 },
527 { "ebu3213", AOM_CICP_CP_EBU_3213 },
528 { NULL, 0 }
529};
530
531static const arg_def_t input_color_primaries = ARG_DEF_ENUM(
532 NULL, "color-primaries", 1,
533 "Color primaries (CICP) of input content:", color_primaries_enum);
534
535static const struct arg_enum_list transfer_characteristics_enum[] = {
536 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
537 { "bt709", AOM_CICP_TC_BT_709 },
538 { "bt470m", AOM_CICP_TC_BT_470_M },
539 { "bt470bg", AOM_CICP_TC_BT_470_B_G },
540 { "bt601", AOM_CICP_TC_BT_601 },
541 { "smpte240", AOM_CICP_TC_SMPTE_240 },
542 { "lin", AOM_CICP_TC_LINEAR },
543 { "log100", AOM_CICP_TC_LOG_100 },
544 { "log100sq10", AOM_CICP_TC_LOG_100_SQRT10 },
545 { "iec61966", AOM_CICP_TC_IEC_61966 },
546 { "bt1361", AOM_CICP_TC_BT_1361 },
547 { "srgb", AOM_CICP_TC_SRGB },
548 { "bt2020-10bit", AOM_CICP_TC_BT_2020_10_BIT },
549 { "bt2020-12bit", AOM_CICP_TC_BT_2020_12_BIT },
550 { "smpte2084", AOM_CICP_TC_SMPTE_2084 },
551 { "hlg", AOM_CICP_TC_HLG },
552 { "smpte428", AOM_CICP_TC_SMPTE_428 },
553 { NULL, 0 }
554};
555
556static const arg_def_t input_transfer_characteristics =
557 ARG_DEF_ENUM(NULL, "transfer-characteristics", 1,
558 "Transfer characteristics (CICP) of input content:",
559 transfer_characteristics_enum);
560
561static const struct arg_enum_list matrix_coefficients_enum[] = {
562 { "identity", AOM_CICP_MC_IDENTITY },
563 { "bt709", AOM_CICP_MC_BT_709 },
564 { "unspecified", AOM_CICP_MC_UNSPECIFIED },
565 { "fcc73", AOM_CICP_MC_FCC },
566 { "bt470bg", AOM_CICP_MC_BT_470_B_G },
567 { "bt601", AOM_CICP_MC_BT_601 },
568 { "smpte240", AOM_CICP_CP_SMPTE_240 },
569 { "ycgco", AOM_CICP_MC_SMPTE_YCGCO },
570 { "bt2020ncl", AOM_CICP_MC_BT_2020_NCL },
571 { "bt2020cl", AOM_CICP_MC_BT_2020_CL },
572 { "smpte2085", AOM_CICP_MC_SMPTE_2085 },
573 { "chromncl", AOM_CICP_MC_CHROMAT_NCL },
574 { "chromcl", AOM_CICP_MC_CHROMAT_CL },
575 { "ictcp", AOM_CICP_MC_ICTCP },
576 { NULL, 0 }
577};
578
579static const arg_def_t input_matrix_coefficients = ARG_DEF_ENUM(
580 NULL, "matrix-coefficients", 1,
581 "Matrix coefficients (CICP) of input content:", matrix_coefficients_enum);
582
583#else
Yaowu Xufc996362015-02-10 15:03:05 -0800584static const struct arg_enum_list color_space_enum[] = {
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800585 { "unspecified", AOM_CS_UNKNOWN }, { "bt601", AOM_CS_BT_601 },
586 { "bt709", AOM_CS_BT_709 }, { "smpte170", AOM_CS_SMPTE_170 },
587 { "smpte240", AOM_CS_SMPTE_240 }, { "bt2020ncl", AOM_CS_BT_2020_NCL },
588 { "bt2020cl", AOM_CS_BT_2020_CL }, { "sRGB", AOM_CS_SRGB },
589 { "ictcp", AOM_CS_ICTCP }, { NULL, 0 }
Yaowu Xufc996362015-02-10 15:03:05 -0800590};
591
clang-format6c4d83e2016-08-08 19:03:30 -0700592static const arg_def_t input_color_space =
clang-format4eafefe2017-09-04 12:51:20 -0700593 ARG_DEF_ENUM(NULL, "color-space", 1,
594 "The color space of input content:", color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800595
anorkin76fb1262017-03-22 15:12:12 -0700596static const struct arg_enum_list transfer_function_enum[] = {
597 { "unknown", AOM_TF_UNKNOWN },
598 { "bt709", AOM_TF_BT_709 },
599 { "pq", AOM_TF_PQ },
600 { "hlg", AOM_TF_HLG },
601 { NULL, 0 }
602};
603
604static const arg_def_t input_transfer_function = ARG_DEF_ENUM(
clang-format4eafefe2017-09-04 12:51:20 -0700605 NULL, "transfer-function", 1,
606 "The transfer function of input content:", transfer_function_enum);
anorkin76fb1262017-03-22 15:12:12 -0700607
Andrey Norkin9e694632017-12-21 18:50:57 -0800608#endif
anorkin76fb1262017-03-22 15:12:12 -0700609static const struct arg_enum_list chroma_sample_position_enum[] = {
610 { "unknown", AOM_CSP_UNKNOWN },
611 { "vertical", AOM_CSP_VERTICAL },
612 { "colocated", AOM_CSP_COLOCATED },
613 { NULL, 0 }
614};
615
616static const arg_def_t input_chroma_sample_position =
617 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
618 "The chroma sample position when chroma 4:2:0 is signaled:",
619 chroma_sample_position_enum);
anorkin76fb1262017-03-22 15:12:12 -0700620
Alex Converse219f6452014-08-06 11:01:18 -0700621static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700622 { "default", AOM_CONTENT_DEFAULT },
623 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700624 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700625};
626
627static const arg_def_t tune_content = ARG_DEF_ENUM(
628 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700629#endif
Alex Converse219f6452014-08-06 11:01:18 -0700630
Yaowu Xuf883b422016-08-30 14:01:10 -0700631#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000632#if CONFIG_EXT_PARTITION
633static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700634 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
635 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
636 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700637 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000638};
639static const arg_def_t superblock_size = ARG_DEF_ENUM(
640 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
641#endif // CONFIG_EXT_PARTITION
642
Yaowu Xuf883b422016-08-30 14:01:10 -0700643static const arg_def_t *av1_args[] = { &cpu_used_av1,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800644 &dev_sf_av1,
Yaowu Xuf883b422016-08-30 14:01:10 -0700645 &auto_altref,
646 &sharpness,
647 &static_thresh,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700648#if CONFIG_EXT_TILE
649 &single_tile_decoding,
650#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700651 &tile_cols,
652 &tile_rows,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800653#if CONFIG_DEPENDENT_HORZTILES
654 &tile_dependent_rows,
655#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800656#if CONFIG_LOOPFILTERING_ACROSS_TILES
Lei7bb501d2017-12-13 15:10:34 -0800657#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
658 &tile_loopfilter_v,
659 &tile_loopfilter_h,
660#else
Ryan Lei7386eda2016-12-08 21:08:31 -0800661 &tile_loopfilter,
Lei7bb501d2017-12-13 15:10:34 -0800662#endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800663#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700664 &arnr_maxframes,
665 &arnr_strength,
Yushin Chod808bfc2017-08-10 15:54:36 -0700666 &tune_metric,
Yaowu Xuf883b422016-08-30 14:01:10 -0700667 &cq_level,
668 &max_intra_rate_pct,
669 &max_inter_rate_pct,
670 &gf_cbr_boost_pct,
671 &lossless,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100672 &enable_cdef,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800673#if CONFIG_AOM_QM
674 &enable_qm,
675 &qm_min,
676 &qm_max,
677#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700678#if CONFIG_DIST_8X8
679 &enable_dist_8x8,
680#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700681 &frame_parallel_decoding,
682 &aq_mode,
Fangwen Fu6160df22017-04-24 09:45:51 -0700683#if CONFIG_EXT_DELTA_Q
684 &deltaq_mode,
685#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700686 &frame_periodic_boost,
687 &noise_sens,
688 &tune_content,
Andrey Norkin9e694632017-12-21 18:50:57 -0800689#if CONFIG_CICP
690 &input_color_primaries,
691 &input_transfer_characteristics,
692 &input_matrix_coefficients,
693#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700694 &input_color_space,
anorkin76fb1262017-03-22 15:12:12 -0700695 &input_transfer_function,
Andrey Norkin9e694632017-12-21 18:50:57 -0800696#endif
anorkin76fb1262017-03-22 15:12:12 -0700697 &input_chroma_sample_position,
Yaowu Xuf883b422016-08-30 14:01:10 -0700698 &min_gf_interval,
699 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000700#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700701 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000702#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000703 &num_tg,
704 &mtu_size,
Fangwen Fu8d164de2016-12-14 13:40:54 -0800705#if CONFIG_TEMPMV_SIGNALING
706 &disable_tempmv,
707#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700708 &bitdeptharg,
709 &inbitdeptharg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700710 NULL };
711static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800712 AOME_SET_DEVSF,
Yaowu Xuf883b422016-08-30 14:01:10 -0700713 AOME_SET_ENABLEAUTOALTREF,
714 AOME_SET_SHARPNESS,
715 AOME_SET_STATIC_THRESHOLD,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700716#if CONFIG_EXT_TILE
717 AV1E_SET_SINGLE_TILE_DECODING,
718#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700719 AV1E_SET_TILE_COLUMNS,
720 AV1E_SET_TILE_ROWS,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800721#if CONFIG_DEPENDENT_HORZTILES
722 AV1E_SET_TILE_DEPENDENT_ROWS,
723#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800724#if CONFIG_LOOPFILTERING_ACROSS_TILES
Lei7bb501d2017-12-13 15:10:34 -0800725#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
726 AV1E_SET_TILE_LOOPFILTER_V,
727 AV1E_SET_TILE_LOOPFILTER_H,
728#else
Ryan Lei7386eda2016-12-08 21:08:31 -0800729 AV1E_SET_TILE_LOOPFILTER,
Lei7bb501d2017-12-13 15:10:34 -0800730#endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800731#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700732 AOME_SET_ARNR_MAXFRAMES,
733 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700734 AOME_SET_TUNING,
735 AOME_SET_CQ_LEVEL,
736 AOME_SET_MAX_INTRA_BITRATE_PCT,
737 AV1E_SET_MAX_INTER_BITRATE_PCT,
738 AV1E_SET_GF_CBR_BOOST_PCT,
739 AV1E_SET_LOSSLESS,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100740 AV1E_SET_ENABLE_CDEF,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800741#if CONFIG_AOM_QM
742 AV1E_SET_ENABLE_QM,
743 AV1E_SET_QM_MIN,
744 AV1E_SET_QM_MAX,
745#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700746#if CONFIG_DIST_8X8
747 AV1E_SET_ENABLE_DIST_8X8,
748#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700749 AV1E_SET_FRAME_PARALLEL_DECODING,
750 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700751#if CONFIG_EXT_DELTA_Q
752 AV1E_SET_DELTAQ_MODE,
753#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700754 AV1E_SET_FRAME_PERIODIC_BOOST,
755 AV1E_SET_NOISE_SENSITIVITY,
756 AV1E_SET_TUNE_CONTENT,
Andrey Norkin9e694632017-12-21 18:50:57 -0800757#if CONFIG_CICP
758 AV1E_SET_COLOR_PRIMARIES,
759 AV1E_SET_TRANSFER_CHARACTERISTICS,
760 AV1E_SET_MATRIX_COEFFICIENTS,
761#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700762 AV1E_SET_COLOR_SPACE,
anorkin76fb1262017-03-22 15:12:12 -0700763 AV1E_SET_TRANSFER_FUNCTION,
Andrey Norkin9e694632017-12-21 18:50:57 -0800764#endif
anorkin76fb1262017-03-22 15:12:12 -0700765 AV1E_SET_CHROMA_SAMPLE_POSITION,
Yaowu Xuf883b422016-08-30 14:01:10 -0700766 AV1E_SET_MIN_GF_INTERVAL,
767 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000768#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700769 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000770#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000771 AV1E_SET_NUM_TG,
772 AV1E_SET_MTU,
Fangwen Fu8d164de2016-12-14 13:40:54 -0800773#if CONFIG_TEMPMV_SIGNALING
774 AV1E_SET_DISABLE_TEMPMV,
775#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700776 0 };
hui su82331e02015-08-16 18:21:56 -0700777#endif
778
John Koleszar0ea50ce2010-05-18 11:58:33 -0400779static const arg_def_t *no_args[] = { NULL };
780
James Zernfd8c78c2017-11-27 16:37:33 -0800781void show_help(FILE *fout, int shorthelp) {
782 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename \n",
John Koleszarc6b90392012-07-13 15:21:29 -0700783 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400784
James Zernfd8c78c2017-11-27 16:37:33 -0800785 if (shorthelp) {
786 fprintf(fout, "Use --help to see the full list of options.\n");
787 return;
788 }
789
790 fprintf(fout, "\nOptions:\n");
791 arg_show_usage(fout, main_args);
792 fprintf(fout, "\nEncoder Global Options:\n");
793 arg_show_usage(fout, global_args);
794 fprintf(fout, "\nRate Control Options:\n");
795 arg_show_usage(fout, rc_args);
796 fprintf(fout, "\nTwopass Rate Control Options:\n");
797 arg_show_usage(fout, rc_twopass_args);
798 fprintf(fout, "\nKeyframe Placement Options:\n");
799 arg_show_usage(fout, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700800#if CONFIG_AV1_ENCODER
James Zernfd8c78c2017-11-27 16:37:33 -0800801 fprintf(fout, "\nAV1 Specific Options:\n");
802 arg_show_usage(fout, av1_args);
hui su82331e02015-08-16 18:21:56 -0700803#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800804 fprintf(fout,
clang-format6c4d83e2016-08-08 19:03:30 -0700805 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700806 " The desired precision of timestamps in the output, expressed\n"
807 " in fractional seconds. Default is 1/1000.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800808 fprintf(fout, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400809
James Zernfd8c78c2017-11-27 16:37:33 -0800810 const int num_encoder = get_aom_encoder_count();
811 for (int i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700812 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700813 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
James Zernfd8c78c2017-11-27 16:37:33 -0800814 fprintf(fout, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800816 }
James Zernfd8c78c2017-11-27 16:37:33 -0800817 fprintf(fout, "\n ");
818 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
819}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400820
James Zernfd8c78c2017-11-27 16:37:33 -0800821void usage_exit(void) {
822 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700823 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400824}
825
Yaowu Xuf883b422016-08-30 14:01:10 -0700826#if CONFIG_AV1_ENCODER
827#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800828#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800829
James Zerndba73762014-05-10 17:44:12 -0700830#if !CONFIG_WEBM_IO
831typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700832struct WebmOutputContext {
833 int debug;
834};
James Zerndba73762014-05-10 17:44:12 -0700835#endif
836
John Koleszar9e50ed72012-02-15 12:39:38 -0800837/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800838struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700839 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700840 const char *out_fn;
841 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700842#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700843 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700844#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700845 stereo_format_t stereo_fmt;
846 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
847 int arg_ctrl_cnt;
848 int write_webm;
Cyril Concolato6c788832017-10-30 16:30:35 -0700849#if CONFIG_OBU_NO_IVF
850 int write_ivf;
851#endif
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700852 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700853 int use_16bit_internal;
John Koleszar9e50ed72012-02-15 12:39:38 -0800854};
855
John Koleszar6ad3b742012-11-06 12:02:42 -0800856struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700857 int index;
858 struct stream_state *next;
859 struct stream_config config;
860 FILE *file;
861 struct rate_hist *rate_hist;
862 struct WebmOutputContext webm_ctx;
863 uint64_t psnr_sse_total;
864 uint64_t psnr_samples_total;
865 double psnr_totals[4];
866 int psnr_count;
867 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700868 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700869 unsigned int frames_out;
870 uint64_t cx_time;
871 size_t nbytes;
872 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700873#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700874 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700875#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700876 struct aom_image *img;
877 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700878 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800879};
880
clang-format6c4d83e2016-08-08 19:03:30 -0700881static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700882 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800883 if (rat->den < 0) {
884 rat->num *= -1;
885 rat->den *= -1;
886 }
John Koleszar1b27e932012-04-25 17:08:56 -0700887
clang-format6c4d83e2016-08-08 19:03:30 -0700888 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700889
clang-format6c4d83e2016-08-08 19:03:30 -0700890 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700891}
892
Yaowu Xuf883b422016-08-30 14:01:10 -0700893static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700894 char **argi, **argj;
895 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700896 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700897
clang-format6c4d83e2016-08-08 19:03:30 -0700898 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800899
John Koleszar6ad3b742012-11-06 12:02:42 -0800900 /* Initialize default parameters */
901 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700902 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700903 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700904 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700905 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700906 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400907
John Koleszarc6b90392012-07-13 15:21:29 -0700908 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
909 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400910
James Zernfd8c78c2017-11-27 16:37:33 -0800911 if (arg_match(&arg, &help, argi)) {
912 show_help(stdout, 0);
913 exit(EXIT_SUCCESS);
914 } else if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700915 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800916 if (!global->codec)
917 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700918 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800919 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400920
John Koleszar6ad3b742012-11-06 12:02:42 -0800921 if (global->passes < 1 || global->passes > 2)
922 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700923 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800924 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400925
John Koleszar6ad3b742012-11-06 12:02:42 -0800926 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700927 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800928 } else if (arg_match(&arg, &usage, argi))
929 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700930 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800931 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700932 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700933 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar6ad3b742012-11-06 12:02:42 -0800934 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700935 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800936 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700937 global->color_type = I420;
938 else if (arg_match(&arg, &use_i422, argi))
939 global->color_type = I422;
940 else if (arg_match(&arg, &use_i444, argi))
941 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700942 else if (arg_match(&arg, &use_i440, argi))
943 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800944 else if (arg_match(&arg, &quietarg, argi))
945 global->quiet = 1;
946 else if (arg_match(&arg, &verbosearg, argi))
947 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700948 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800949 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700950 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800951 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700952 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800953 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700954 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800955 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700956 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800957 global->framerate = arg_parse_rational(&arg);
958 validate_positive_rational(arg.name, &global->framerate);
959 global->have_framerate = 1;
960 } else if (arg_match(&arg, &out_part, argi))
961 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700962 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800963 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700964 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800965 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700966 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800967 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800968 else if (arg_match(&arg, &disable_warnings, argi))
969 global->disable_warnings = 1;
970 else if (arg_match(&arg, &disable_warning_prompt, argi))
971 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800972 else
John Koleszarc6b90392012-07-13 15:21:29 -0700973 argj++;
974 }
975
John Koleszar6ad3b742012-11-06 12:02:42 -0800976 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700977 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800978 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700979 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
980 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800981 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800982 }
John Koleszarc6b90392012-07-13 15:21:29 -0700983 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800984 /* Validate global config */
985 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700986#if CONFIG_AV1_ENCODER
987 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800988 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700989 if (global->codec != NULL && global->codec->name != NULL)
Thomas Daede80826142017-03-20 15:44:24 -0700990 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800991#else
992 global->passes = 1;
993#endif
994 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800995}
996
Yaowu Xuf883b422016-08-30 14:01:10 -0700997static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800998 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700999 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
1000 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -08001001
clang-format6c4d83e2016-08-08 19:03:30 -07001002 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -08001003
John Koleszar25b6e9f2013-02-12 21:17:56 -08001004 if (!fseeko(input->file, 0, SEEK_END)) {
1005 /* Input file is seekable. Figure out how long it is, so we can get
1006 * progress info.
1007 */
1008 input->length = ftello(input->file);
1009 rewind(input->file);
1010 }
1011
Frank Galligan09acd262015-06-01 10:20:58 -07001012 /* Default to 1:1 pixel aspect ratio. */
1013 input->pixel_aspect_ratio.numerator = 1;
1014 input->pixel_aspect_ratio.denominator = 1;
1015
John Koleszar6ad3b742012-11-06 12:02:42 -08001016 /* For RAW input sources, these bytes will applied on the first frame
1017 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -07001018 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001019 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1020 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001021
clang-format6c4d83e2016-08-08 19:03:30 -07001022 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -07001023 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
1024 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001025 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001026 input->width = input->y4m.pic_w;
1027 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -07001028 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1029 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001030 input->framerate.numerator = input->y4m.fps_n;
1031 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -07001032 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001033 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -08001034 } else
1035 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -08001036 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -08001037 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -08001038 } else {
1039 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001040 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001041}
1042
Yaowu Xuf883b422016-08-30 14:01:10 -07001043static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001044 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -07001045 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001046}
1047
Yaowu Xuf883b422016-08-30 14:01:10 -07001048static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -08001049 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001050 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001051
John Koleszar6ad3b742012-11-06 12:02:42 -08001052 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001053 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001054 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001055 }
1056
John Koleszar6ad3b742012-11-06 12:02:42 -08001057 if (prev) {
1058 memcpy(stream, prev, sizeof(*stream));
1059 stream->index++;
1060 prev->next = stream;
1061 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001062 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001063
John Koleszar6ad3b742012-11-06 12:02:42 -08001064 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -07001065 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -07001066 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -07001067 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001068
John Koleszar6ad3b742012-11-06 12:02:42 -08001069 /* Change the default timebase to a high enough value so that the
1070 * encoder will always create strictly increasing timestamps.
1071 */
1072 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001073
John Koleszar6ad3b742012-11-06 12:02:42 -08001074 /* Never use the library's default resolution, require it be parsed
1075 * from the file or set on the command line.
1076 */
1077 stream->config.cfg.g_w = 0;
1078 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001079
John Koleszar6ad3b742012-11-06 12:02:42 -08001080 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -08001081 stream->config.write_webm = 1;
Cyril Concolato6c788832017-10-30 16:30:35 -07001082#if CONFIG_OBU_NO_IVF
1083 stream->config.write_ivf = 0;
1084#endif
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001085#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -07001086 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001087 stream->webm_ctx.last_pts_ns = -1;
1088 stream->webm_ctx.writer = NULL;
1089 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001090#endif
Johann87c40b32012-03-01 16:12:53 -08001091
John Koleszar6ad3b742012-11-06 12:02:42 -08001092 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001093 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -08001094 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001095
John Koleszar6ad3b742012-11-06 12:02:42 -08001096 /* Output files must be specified for each stream */
1097 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001098
John Koleszar6ad3b742012-11-06 12:02:42 -08001099 stream->next = NULL;
1100 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001101}
1102
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +00001103static void set_config_arg_ctrls(struct stream_config *config, int key,
1104 const struct arg *arg) {
1105 int j;
1106 /* Point either to the next free element or the first instance of this
1107 * control.
1108 */
1109 for (j = 0; j < config->arg_ctrl_cnt; j++)
1110 if (config->arg_ctrls[j][0] == key) break;
1111
1112 /* Update/insert */
1113 assert(j < (int)ARG_CTRL_CNT_MAX);
1114 config->arg_ctrls[j][0] = key;
1115 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
1116 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1117}
1118
Yaowu Xuf883b422016-08-30 14:01:10 -07001119static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001120 struct stream_state *stream, char **argv) {
1121 char **argi, **argj;
1122 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001123 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001124 static const int *ctrl_args_map = NULL;
1125 struct stream_config *config = &stream->config;
1126 int eos_mark_found = 0;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001127 int webm_forced = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001128
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001129 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001130 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001131#if CONFIG_AV1_ENCODER
1132 } else if (strcmp(global->codec->name, "av1") == 0) {
1133 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1134 // Consider to expand this set for AV1 encoder control.
1135 ctrl_args = av1_args;
1136 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001137#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001138 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001139
John Koleszarc6b90392012-07-13 15:21:29 -07001140 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001141 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001142
John Koleszar6ad3b742012-11-06 12:02:42 -08001143 /* Once we've found an end-of-stream marker (--) we want to continue
1144 * shifting arguments but not consuming them.
1145 */
1146 if (eos_mark_found) {
1147 argj++;
1148 continue;
1149 } else if (!strcmp(*argj, "--")) {
1150 eos_mark_found = 1;
1151 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001152 }
1153
Adrian Granged2401802015-02-13 14:51:32 -08001154 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001155 config->out_fn = arg.val;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001156 if (!webm_forced) {
1157 const size_t out_fn_len = strlen(config->out_fn);
1158 if (out_fn_len >= 4 &&
1159 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
1160 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001161#if CONFIG_OBU_NO_IVF
1162 config->write_ivf = 1;
1163#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001164 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001165#if CONFIG_OBU_NO_IVF
1166 else if (out_fn_len >= 4 &&
1167 !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
1168 config->write_webm = 0;
1169 config->write_ivf = 0;
1170 }
1171#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001172 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001173 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001174 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001175#if CONFIG_FP_MB_STATS
1176 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1177 config->fpmb_stats_fn = arg.val;
1178#endif
Johannb50e5182015-01-30 15:05:14 -08001179 } else if (arg_match(&arg, &use_webm, argi)) {
1180#if CONFIG_WEBM_IO
1181 config->write_webm = 1;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001182 webm_forced = 1;
Johannb50e5182015-01-30 15:05:14 -08001183#else
1184 die("Error: --webm specified but webm is disabled.");
1185#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001186 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001187 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001188#if CONFIG_OBU_NO_IVF
1189 config->write_ivf = 1;
1190#endif
1191#if CONFIG_OBU_NO_IVF
1192 } else if (arg_match(&arg, &use_obu, argi)) {
1193 config->write_webm = 0;
1194 config->write_ivf = 0;
1195#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001196 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001197 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001198 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001199 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001200 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001201 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001202 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001203 config->cfg.g_h = arg_parse_uint(&arg);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001204 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1205 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1206 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1207 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
James Zerndba73762014-05-10 17:44:12 -07001208#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001209 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001210 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001211#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001212 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001213 config->cfg.g_timebase = arg_parse_rational(&arg);
1214 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001215 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001216 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001217 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001218 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001219#if CONFIG_EXT_TILE
1220 } else if (arg_match(&arg, &large_scale_tile, argi)) {
1221 config->cfg.large_scale_tile = arg_parse_uint(&arg);
1222#endif // CONFIG_EXT_TILE
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001223#if CONFIG_MONO_VIDEO
1224 } else if (arg_match(&arg, &monochrome, argi)) {
1225 config->cfg.monochrome = 1;
1226#endif // CONFIG_MONO_VIDEO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001227 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001228 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001229 } else if (arg_match(&arg, &resize_mode, argi)) {
1230 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001231 } else if (arg_match(&arg, &resize_denominator, argi)) {
1232 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1233 } else if (arg_match(&arg, &resize_kf_denominator, argi)) {
1234 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001235#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001236 } else if (arg_match(&arg, &superres_mode, argi)) {
1237 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001238 } else if (arg_match(&arg, &superres_denominator, argi)) {
1239 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1240 } else if (arg_match(&arg, &superres_kf_denominator, argi)) {
1241 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001242 } else if (arg_match(&arg, &superres_qthresh, argi)) {
1243 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1244 } else if (arg_match(&arg, &superres_kf_qthresh, argi)) {
1245 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001246#endif // CONFIG_HORZONLY_FRAME_SUPERRES
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001247 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001248 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001249 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001250 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001251 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001252 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001253 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001254 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001255 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001256 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001257 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001258 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001259 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001260 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001261 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001262 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001263 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001264 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001265 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001266 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001267 if (global->passes < 2)
1268 warn("option %s ignored in one-pass mode.\n", arg.name);
1269 } else if (arg_match(&arg, &minsection_pct, argi)) {
1270 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1271
1272 if (global->passes < 2)
1273 warn("option %s ignored in one-pass mode.\n", arg.name);
1274 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1275 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1276
1277 if (global->passes < 2)
1278 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001279 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001280 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001281 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001282 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001283 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001284 config->cfg.kf_mode = AOM_KF_DISABLED;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001285#if CONFIG_MAX_TILE
1286 } else if (arg_match(&arg, &tile_width, argi)) {
1287 config->cfg.tile_width_count =
1288 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1289 } else if (arg_match(&arg, &tile_height, argi)) {
1290 config->cfg.tile_height_count =
1291 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1292#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001293 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001294 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001295 for (i = 0; ctrl_args[i]; i++) {
1296 if (arg_match(&arg, ctrl_args[i], argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001297 match = 1;
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +00001298 if (ctrl_args_map) {
1299 set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001300 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001301 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001302 }
clang-format6c4d83e2016-08-08 19:03:30 -07001303 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001304 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001305 }
Sebastien Alaiwan14af5b92017-05-12 14:27:30 +02001306 config->use_16bit_internal =
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +02001307 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
John Koleszar6ad3b742012-11-06 12:02:42 -08001308 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001309}
1310
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001311#define FOREACH_STREAM(iterator, list) \
1312 for (struct stream_state *iterator = list; iterator; \
1313 iterator = iterator->next)
John Koleszar9e50ed72012-02-15 12:39:38 -08001314
Alex Conversed66bd222014-02-21 10:52:09 -08001315static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001316 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001317 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001318 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001319
John Koleszar6ad3b742012-11-06 12:02:42 -08001320 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001321 fatal(
1322 "Stream %d: Specify stream dimensions with --width (-w) "
1323 " and --height (-h)",
1324 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001325
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001326 // Check that the codec bit depth is greater than the input bit depth.
1327 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001328 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001329 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1330 stream->index, (int)stream->config.cfg.g_bit_depth,
1331 stream->config.cfg.g_input_bit_depth);
1332 }
1333
John Koleszar6ad3b742012-11-06 12:02:42 -08001334 for (streami = stream; streami; streami = streami->next) {
1335 /* All streams require output files */
1336 if (!streami->config.out_fn)
1337 fatal("Stream %d: Output file is required (specify with -o)",
1338 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001339
John Koleszar6ad3b742012-11-06 12:02:42 -08001340 /* Check for two streams outputting to the same file */
1341 if (streami != stream) {
1342 const char *a = stream->config.out_fn;
1343 const char *b = streami->config.out_fn;
1344 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1345 fatal("Stream %d: duplicate output file (from stream %d)",
1346 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001347 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001348
1349 /* Check for two streams sharing a stats file. */
1350 if (streami != stream) {
1351 const char *a = stream->config.stats_fn;
1352 const char *b = streami->config.stats_fn;
1353 if (a && b && !strcmp(a, b))
1354 fatal("Stream %d: duplicate stats file (from stream %d)",
1355 streami->index, stream->index);
1356 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001357
1358#if CONFIG_FP_MB_STATS
1359 /* Check for two streams sharing a mb stats file. */
1360 if (streami != stream) {
1361 const char *a = stream->config.fpmb_stats_fn;
1362 const char *b = streami->config.fpmb_stats_fn;
1363 if (a && b && !strcmp(a, b))
1364 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1365 streami->index, stream->index);
1366 }
1367#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001368 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001369}
1370
clang-format6c4d83e2016-08-08 19:03:30 -07001371static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001372 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001373 if (!stream->config.cfg.g_w) {
1374 if (!stream->config.cfg.g_h)
1375 stream->config.cfg.g_w = w;
1376 else
1377 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1378 }
1379 if (!stream->config.cfg.g_h) {
1380 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1381 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001382}
1383
clang-format6c4d83e2016-08-08 19:03:30 -07001384static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001385 switch (t) {
1386 case FILE_TYPE_RAW: return "RAW";
1387 case FILE_TYPE_Y4M: return "Y4M";
1388 default: return "Other";
1389 }
1390}
1391
Yaowu Xuf883b422016-08-30 14:01:10 -07001392static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001393 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001394 case AOM_IMG_FMT_I420: return "I420";
1395 case AOM_IMG_FMT_I422: return "I422";
1396 case AOM_IMG_FMT_I444: return "I444";
1397 case AOM_IMG_FMT_I440: return "I440";
1398 case AOM_IMG_FMT_YV12: return "YV12";
1399 case AOM_IMG_FMT_I42016: return "I42016";
1400 case AOM_IMG_FMT_I42216: return "I42216";
1401 case AOM_IMG_FMT_I44416: return "I44416";
1402 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001403 default: return "Other";
1404 }
1405}
Ralph Giles061a16d2012-01-05 15:05:05 -06001406
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001407static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001408 struct AvxEncoderConfig *global,
1409 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001410#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001411 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001412
John Koleszar6ad3b742012-11-06 12:02:42 -08001413 if (stream->index == 0) {
1414 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001415 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001416 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001417 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001418 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001419 }
1420 if (stream->next || stream->index)
1421 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1422 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
Sebastien Alaiwan27511922017-06-05 15:25:46 +02001423 fprintf(stderr, "Coding path: %s\n",
1424 stream->config.use_16bit_internal ? "HBD" : "LBD");
John Koleszar6ad3b742012-11-06 12:02:42 -08001425 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001426
John Koleszar6ad3b742012-11-06 12:02:42 -08001427 SHOW(g_usage);
1428 SHOW(g_threads);
1429 SHOW(g_profile);
1430 SHOW(g_w);
1431 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001432 SHOW(g_bit_depth);
1433 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001434 SHOW(g_timebase.num);
1435 SHOW(g_timebase.den);
1436 SHOW(g_error_resilient);
1437 SHOW(g_pass);
1438 SHOW(g_lag_in_frames);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001439#if CONFIG_EXT_TILE
1440 SHOW(large_scale_tile);
1441#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001442 SHOW(rc_dropframe_thresh);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001443 SHOW(rc_resize_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001444 SHOW(rc_resize_denominator);
1445 SHOW(rc_resize_kf_denominator);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001446#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001447 SHOW(rc_superres_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001448 SHOW(rc_superres_denominator);
1449 SHOW(rc_superres_kf_denominator);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001450 SHOW(rc_superres_qthresh);
1451 SHOW(rc_superres_kf_qthresh);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001452#endif // CONFIG_HORZONLY_FRAME_SUPERRES
John Koleszar6ad3b742012-11-06 12:02:42 -08001453 SHOW(rc_end_usage);
1454 SHOW(rc_target_bitrate);
1455 SHOW(rc_min_quantizer);
1456 SHOW(rc_max_quantizer);
1457 SHOW(rc_undershoot_pct);
1458 SHOW(rc_overshoot_pct);
1459 SHOW(rc_buf_sz);
1460 SHOW(rc_buf_initial_sz);
1461 SHOW(rc_buf_optimal_sz);
1462 SHOW(rc_2pass_vbr_bias_pct);
1463 SHOW(rc_2pass_vbr_minsection_pct);
1464 SHOW(rc_2pass_vbr_maxsection_pct);
1465 SHOW(kf_mode);
1466 SHOW(kf_min_dist);
1467 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001468}
1469
John Koleszar9e50ed72012-02-15 12:39:38 -08001470static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001471 struct AvxEncoderConfig *global,
1472 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001473 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001474 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001475
Yaowu Xuf883b422016-08-30 14:01:10 -07001476 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001477
John Koleszar6ad3b742012-11-06 12:02:42 -08001478 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001479
clang-format6c4d83e2016-08-08 19:03:30 -07001480 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001481
John Koleszar6ad3b742012-11-06 12:02:42 -08001482 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1483 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001484
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001485#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001486 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001487 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001488 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1489 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001490 }
James Zernc8e5a772016-02-11 18:53:50 -08001491#else
1492 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001493#endif
1494
Cyril Concolato6c788832017-10-30 16:30:35 -07001495 if (!stream->config.write_webm
1496#if CONFIG_OBU_NO_IVF
1497 && stream->config.write_ivf
1498#endif
1499 ) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001500 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1501 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001502}
1503
John Koleszar9e50ed72012-02-15 12:39:38 -08001504static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001505 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001506 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001507
Yaowu Xuf883b422016-08-30 14:01:10 -07001508 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001509
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001510#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001511 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001512 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001513 }
1514#endif
1515
Cyril Concolato6c788832017-10-30 16:30:35 -07001516 if (!stream->config.write_webm
1517#if CONFIG_OBU_NO_IVF
1518 && stream->config.write_ivf
1519#endif
1520 ) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001521 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001522 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001523 stream->frames_out);
1524 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001525
John Koleszar6ad3b742012-11-06 12:02:42 -08001526 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001527}
1528
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001529static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001530 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001531 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001532 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001533 fatal("Failed to open statistics store");
1534 } else {
1535 if (!stats_open_mem(&stream->stats, pass))
1536 fatal("Failed to open statistics store");
1537 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001538
Pengchong Jinf349b072014-07-14 09:13:38 -07001539#if CONFIG_FP_MB_STATS
1540 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001541 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1542 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001543 fatal("Failed to open mb statistics store");
1544 } else {
1545 if (!stats_open_mem(&stream->fpmb_stats, pass))
1546 fatal("Failed to open mb statistics store");
1547 }
1548#endif
1549
John Koleszar6ad3b742012-11-06 12:02:42 -08001550 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001551 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1552 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001553 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001554 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001555#if CONFIG_FP_MB_STATS
1556 stream->config.cfg.rc_firstpass_mb_stats_in =
1557 stats_get(&stream->fpmb_stats);
1558#endif
1559 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001560
John Koleszar6ad3b742012-11-06 12:02:42 -08001561 stream->cx_time = 0;
1562 stream->nbytes = 0;
1563 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001564}
1565
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001566static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001567 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001568 int i;
1569 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001570
Yaowu Xuf883b422016-08-30 14:01:10 -07001571 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1572 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001573 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001574
John Koleszar6ad3b742012-11-06 12:02:42 -08001575 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001576 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001577 &stream->config.cfg, flags);
1578 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001579
Yaowu Xuf883b422016-08-30 14:01:10 -07001580 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001581 * we're being clever to store the control IDs in an array. Real
1582 * applications will want to make use of the enumerations directly
1583 */
1584 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1585 int ctrl = stream->config.arg_ctrls[i][0];
1586 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001587 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001588 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001589
John Koleszar6ad3b742012-11-06 12:02:42 -08001590 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1591 }
1592
Tom Fineganba02c242017-05-16 15:01:54 -07001593#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001594 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001595 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001596 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH };
Yaowu Xuf883b422016-08-30 14:01:10 -07001597 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001598
Tom Fineganba02c242017-05-16 15:01:54 -07001599#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07001600 if (strcmp(global->codec->name, "av1") == 0) {
1601 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001602 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1603
Yaowu Xuf883b422016-08-30 14:01:10 -07001604 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001605 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1606 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001607#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001608 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001609#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001610}
1611
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001612static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001613 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001614 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001615 aom_codec_pts_t frame_start, next_frame_start;
1616 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1617 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001618
clang-format6c4d83e2016-08-08 19:03:30 -07001619 frame_start =
1620 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1621 cfg->g_timebase.num / global->framerate.num;
1622 next_frame_start =
1623 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1624 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001625
Yaowu Xud3e7c682017-12-21 14:08:25 -08001626 /* Scale if necessary */
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001627 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001628 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001629 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001630 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001631 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1632 exit(EXIT_FAILURE);
1633 }
1634#if CONFIG_LIBYUV
1635 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001636 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001637 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001638 }
clang-format6c4d83e2016-08-08 19:03:30 -07001639 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001640 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1641 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1642 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1643 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1644 stream->img->stride[AOM_PLANE_Y] / 2,
1645 (uint16 *)stream->img->planes[AOM_PLANE_U],
1646 stream->img->stride[AOM_PLANE_U] / 2,
1647 (uint16 *)stream->img->planes[AOM_PLANE_V],
1648 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001649 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001650 img = stream->img;
1651#else
clang-format6c4d83e2016-08-08 19:03:30 -07001652 stream->encoder.err = 1;
1653 ctx_exit_on_error(&stream->encoder,
1654 "Stream %d: Failed to encode frame.\n"
Johanne07a6752018-01-10 12:47:44 -08001655 "libyuv is required for scaling but is currently "
1656 "disabled.\n"
1657 "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1658 "cmake.\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001659 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001660#endif
1661 }
1662 }
John Koleszar34882b92012-03-01 12:50:40 -08001663 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001664 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001665 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1666 exit(EXIT_FAILURE);
1667 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001668#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001669 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001670 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001671 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001672 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001673 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1674 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1675 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1676 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1677 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1678 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001679 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001680 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001681#else
1682 stream->encoder.err = 1;
1683 ctx_exit_on_error(&stream->encoder,
1684 "Stream %d: Failed to encode frame.\n"
1685 "Scaling disabled in this configuration. \n"
1686 "To enable, configure with --enable-libyuv\n",
1687 stream->index);
1688#endif
John Koleszar34882b92012-03-01 12:50:40 -08001689 }
1690
Yaowu Xuf883b422016-08-30 14:01:10 -07001691 aom_usec_timer_start(&timer);
1692 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001693 (unsigned long)(next_frame_start - frame_start), 0,
1694 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001695 aom_usec_timer_mark(&timer);
1696 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001697 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1698 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001699}
1700
John Koleszar6ad3b742012-11-06 12:02:42 -08001701static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001702 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001703 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001704
Yaowu Xuf883b422016-08-30 14:01:10 -07001705 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001706 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1707 stream->counts[q]++;
1708 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001709}
1710
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001711static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001712 struct AvxEncoderConfig *global, int *got_data) {
1713 const aom_codec_cx_pkt_t *pkt;
1714 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1715 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001716
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001717 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001718 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001719 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001720 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001721
John Koleszar6ad3b742012-11-06 12:02:42 -08001722 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001723 case AOM_CODEC_CX_FRAME_PKT:
1724 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001725 stream->frames_out++;
1726 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001727 if (!global->quiet)
1728 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001729
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001730 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001731#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001732 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001733 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001734 }
1735#endif
1736 if (!stream->config.write_webm) {
Cyril Concolato6c788832017-10-30 16:30:35 -07001737#if CONFIG_OBU_NO_IVF
1738 if (stream->config.write_ivf) {
1739#endif
1740 if (pkt->data.frame.partition_id <= 0) {
1741 ivf_header_pos = ftello(stream->file);
1742 fsize = pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001743
Cyril Concolato6c788832017-10-30 16:30:35 -07001744 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1745 } else {
1746 fsize += pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001747
Cyril Concolato6c788832017-10-30 16:30:35 -07001748 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1749 const FileOffset currpos = ftello(stream->file);
1750 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1751 ivf_write_frame_size(stream->file, fsize);
1752 fseeko(stream->file, currpos, SEEK_SET);
1753 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001754 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001755#if CONFIG_OBU_NO_IVF
John Koleszar6ad3b742012-11-06 12:02:42 -08001756 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001757#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001758
clang-format6c4d83e2016-08-08 19:03:30 -07001759 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1760 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001761 }
1762 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001763
John Koleszar9e50ed72012-02-15 12:39:38 -08001764 *got_data = 1;
Tom Fineganba02c242017-05-16 15:01:54 -07001765#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001766 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001767 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001768 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001769 if (stream->decoder.err) {
1770 warn_or_exit_on_error(&stream->decoder,
1771 global->test_decode == TEST_DECODE_FATAL,
1772 "Failed to decode frame %d in stream %d",
1773 stream->frames_out + 1, stream->index);
1774 stream->mismatch_seen = stream->frames_out + 1;
1775 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001776 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001777#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001778 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001779 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001780 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001781 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001782 pkt->data.twopass_stats.sz);
1783 stream->nbytes += pkt->data.raw.sz;
1784 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001785#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001786 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001787 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001788 pkt->data.firstpass_mb_stats.sz);
1789 stream->nbytes += pkt->data.raw.sz;
1790 break;
1791#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001792 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001793
1794 if (global->show_psnr) {
1795 int i;
1796
1797 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1798 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1799 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001800 if (!global->quiet)
1801 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001802 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1803 }
1804 stream->psnr_count++;
1805 }
1806
1807 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001808 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001809 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001810 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001811}
1812
clang-format6c4d83e2016-08-08 19:03:30 -07001813static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001814 int i;
1815 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001816
clang-format6c4d83e2016-08-08 19:03:30 -07001817 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001818
John Koleszar6ad3b742012-11-06 12:02:42 -08001819 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001820 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001821 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001822 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001823
John Koleszar6ad3b742012-11-06 12:02:42 -08001824 for (i = 0; i < 4; i++) {
1825 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1826 }
1827 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001828}
1829
John Koleszar25b6e9f2013-02-12 21:17:56 -08001830static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001831 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001832}
1833
clang-format6c4d83e2016-08-08 19:03:30 -07001834static void test_decode(struct stream_state *stream,
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001835 enum TestDecodeFatality fatal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001836 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001837
clang-format6c4d83e2016-08-08 19:03:30 -07001838 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001839
John Koleszarb3c350a2013-03-13 12:15:43 -07001840 /* Get the internal reference frame */
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001841 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1842 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001843
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001844 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1845 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1846 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1847 aom_image_t enc_hbd_img;
1848 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1849 enc_img.d_w, enc_img.d_h, 16);
1850 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1851 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001852 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001853 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1854 aom_image_t dec_hbd_img;
1855 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1856 dec_img.d_w, dec_img.d_h, 16);
1857 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1858 dec_img = dec_hbd_img;
1859 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001860 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001861
John Koleszar6ad3b742012-11-06 12:02:42 -08001862 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001863 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001864
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001865 if (!aom_compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001866 int y[4], u[4], v[4];
Yaowu Xuf883b422016-08-30 14:01:10 -07001867 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001868 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001869 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001870 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001871 }
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001872 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001873 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001874 "Stream %d: Encode/decode mismatch on frame %d at"
1875 " Y[%d, %d] {%d/%d},"
1876 " U[%d, %d] {%d/%d},"
1877 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001878 stream->index, stream->frames_out, y[0], y[1], y[2],
1879 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 -08001880 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001881 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001882
Yaowu Xuf883b422016-08-30 14:01:10 -07001883 aom_img_free(&enc_img);
1884 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001885}
John Koleszarefd54f82012-02-13 16:52:18 -08001886
John Koleszar25b6e9f2013-02-12 21:17:56 -08001887static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001888 int64_t hours;
1889 int64_t mins;
1890 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001891
1892 if (etl >= 0) {
1893 hours = etl / 3600;
1894 etl -= hours * 3600;
1895 mins = etl / 60;
1896 etl -= mins * 60;
1897 secs = etl;
1898
clang-format6c4d83e2016-08-08 19:03:30 -07001899 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1900 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001901 } else {
1902 fprintf(stderr, "[%3s unknown] ", label);
1903 }
1904}
1905
Tom Finegan44dd3272013-11-20 17:18:28 -08001906int main(int argc, const char **argv_) {
1907 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001908 aom_image_t raw;
Yaowu Xuf883b422016-08-30 14:01:10 -07001909 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001910 int allocated_raw_shift = 0;
1911 int use_16bit_internal = 0;
1912 int input_shift = 0;
Tom Finegan44dd3272013-11-20 17:18:28 -08001913 int frame_avail, got_data;
1914
Yaowu Xuf883b422016-08-30 14:01:10 -07001915 struct AvxInputContext input;
1916 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001917 struct stream_state *streams = NULL;
1918 char **argv, **argi;
1919 uint64_t cx_time = 0;
1920 int stream_cnt = 0;
1921 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001922 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001923
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001924 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001925 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001926
John Koleszar6ad3b742012-11-06 12:02:42 -08001927 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001928 input.framerate.numerator = 30;
1929 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001930 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001931 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001932
1933 /* First parse the global configuration values, because we want to apply
1934 * other parameters on top of the default configuration provided by the
1935 * codec.
1936 */
1937 argv = argv_dup(argc - 1, argv_ + 1);
1938 parse_global_config(&global, argv);
1939
James Zernfd8c78c2017-11-27 16:37:33 -08001940 if (argc < 3) usage_exit();
1941
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001942 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001943 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1944 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1945 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1946 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1947 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001948 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001949
John Koleszar6ad3b742012-11-06 12:02:42 -08001950 {
1951 /* Now parse each stream's parameters. Using a local scope here
1952 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1953 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001954 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001955 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001956
John Koleszar6ad3b742012-11-06 12:02:42 -08001957 do {
1958 stream = new_stream(&global, stream);
1959 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001960 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001961 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001962 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001963
John Koleszarc6b90392012-07-13 15:21:29 -07001964 /* Check for unrecognized options */
1965 for (argi = argv; *argi; argi++)
1966 if (argi[0][0] == '-' && argi[0][1])
1967 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001968
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001969 FOREACH_STREAM(stream, streams) {
1970 check_encoder_config(global.disable_warning_prompt, &global,
1971 &stream->config.cfg);
1972 }
Tom Finegan249366b2013-11-25 12:05:19 -08001973
John Koleszarc6b90392012-07-13 15:21:29 -07001974 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001975 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001976
James Zernfd8c78c2017-11-27 16:37:33 -08001977 if (!input.filename) {
1978 fprintf(stderr, "No input file specified!\n");
1979 usage_exit();
1980 }
John Koleszar53291892010-10-22 14:48:21 -04001981
John Koleszar8dd82872013-05-06 11:01:35 -07001982 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001983 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001984
John Koleszar6ad3b742012-11-06 12:02:42 -08001985 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001986 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001987 int64_t estimated_time_left = -1;
1988 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001989 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001990
John Koleszar6ad3b742012-11-06 12:02:42 -08001991 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001992
John Koleszar6ad3b742012-11-06 12:02:42 -08001993 /* If the input file doesn't specify its w/h (raw files), try to get
1994 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001995 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001996 if (!input.width || !input.height) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001997 FOREACH_STREAM(stream, streams) {
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001998 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1999 input.width = stream->config.cfg.g_w;
2000 input.height = stream->config.cfg.g_h;
2001 break;
2002 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002003 };
Deb Mukherjeea349ee32014-10-13 14:27:53 -07002004 }
John Koleszarc6b90392012-07-13 15:21:29 -07002005
John Koleszar6ad3b742012-11-06 12:02:42 -08002006 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002007 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07002008 fatal(
2009 "Specify stream dimensions with --width (-w) "
2010 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002011
2012 /* If input file does not specify bit-depth but input-bit-depth parameter
2013 * exists, assume that to be the input bit-depth. However, if the
2014 * input-bit-depth paramter does not exist, assume the input bit-depth
2015 * to be the same as the codec bit-depth.
2016 */
2017 if (!input.bit_depth) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002018 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002019 if (stream->config.cfg.g_input_bit_depth)
2020 input.bit_depth = stream->config.cfg.g_input_bit_depth;
2021 else
2022 input.bit_depth = stream->config.cfg.g_input_bit_depth =
2023 (int)stream->config.cfg.g_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002024 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002025 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002026 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002027 FOREACH_STREAM(stream, streams) {
2028 stream->config.cfg.g_input_bit_depth = input.bit_depth;
2029 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002030 }
2031
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002032 FOREACH_STREAM(stream, streams) {
Thomas Daede8ec53b22016-09-19 13:24:51 -07002033 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2034 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2035 was selected. */
2036 switch (stream->config.cfg.g_profile) {
2037 case 0:
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002038 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2039 input.fmt == AOM_IMG_FMT_I44416)) {
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002040 stream->config.cfg.g_profile = 1;
2041 profile_updated = 1;
2042 } else if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2043 input.fmt == AOM_IMG_FMT_I42216) {
2044 stream->config.cfg.g_profile = 2;
2045 profile_updated = 1;
2046 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002047 break;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002048 case 1:
2049 if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2050 input.fmt == AOM_IMG_FMT_I42216) {
2051 stream->config.cfg.g_profile = 2;
2052 profile_updated = 1;
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002053 } else if (input.bit_depth < 12 &&
2054 (input.fmt == AOM_IMG_FMT_I420 ||
2055 input.fmt == AOM_IMG_FMT_I42016)) {
2056 stream->config.cfg.g_profile = 0;
2057 profile_updated = 1;
2058 }
2059 break;
2060 case 2:
2061 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2062 input.fmt == AOM_IMG_FMT_I44416)) {
2063 stream->config.cfg.g_profile = 1;
2064 profile_updated = 1;
2065 } else if (input.bit_depth < 12 &&
2066 (input.fmt == AOM_IMG_FMT_I420 ||
2067 input.fmt == AOM_IMG_FMT_I42016)) {
2068 stream->config.cfg.g_profile = 0;
2069 profile_updated = 1;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002070 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002071 break;
2072 default: break;
2073 }
2074 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002075 /* Automatically set the codec bit depth to match the input bit depth.
2076 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002077 if (stream->config.cfg.g_input_bit_depth >
2078 (unsigned int)stream->config.cfg.g_bit_depth) {
2079 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2080 }
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002081 if (stream->config.cfg.g_bit_depth > 10) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002082 switch (stream->config.cfg.g_profile) {
2083 case 0:
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002084 case 1:
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002085 stream->config.cfg.g_profile = 2;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002086 profile_updated = 1;
2087 break;
2088 default: break;
2089 }
2090 }
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002091 if (stream->config.cfg.g_bit_depth > 8) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002092 stream->config.use_16bit_internal = 1;
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002093 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002094 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002095 fprintf(stderr,
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002096 "Warning: automatically updating to profile %d to "
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002097 "match input format.\n",
2098 stream->config.cfg.g_profile);
2099 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002100 }
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002101
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002102 FOREACH_STREAM(stream, streams) {
2103 set_stream_dimensions(stream, input.width, input.height);
2104 }
2105 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
John Koleszar77e6b452010-11-03 13:58:40 -04002106
John Koleszar6ad3b742012-11-06 12:02:42 -08002107 /* Ensure that --passes and --pass are consistent. If --pass is set and
2108 * --passes=2, ensure --fpf was set.
2109 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002110 if (global.pass && global.passes == 2) {
2111 FOREACH_STREAM(stream, streams) {
clang-format6c4d83e2016-08-08 19:03:30 -07002112 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002113 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002114 " and --passes=2\n",
2115 stream->index, global.pass);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002116 }
2117 }
John Koleszar3245d462010-06-10 17:10:12 -04002118
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002119#if !CONFIG_WEBM_IO
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002120 FOREACH_STREAM(stream, streams) {
Ronald S. Bultje39775072015-12-16 13:35:43 -05002121 if (stream->config.write_webm) {
2122 stream->config.write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07002123#if CONFIG_OBU_NO_IVF
2124 stream->config.write_ivf = 0;
2125#endif
clang-format6c4d83e2016-08-08 19:03:30 -07002126 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002127 "aomenc was compiled without WebM container support."
Cyril Concolato6c788832017-10-30 16:30:35 -07002128#if CONFIG_OBU_NO_IVF
2129 "Producing OBU output");
2130#else
clang-format6c4d83e2016-08-08 19:03:30 -07002131 "Producing IVF output");
Cyril Concolato6c788832017-10-30 16:30:35 -07002132#endif
Ronald S. Bultje39775072015-12-16 13:35:43 -05002133 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002134 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002135#endif
2136
John Koleszar6ad3b742012-11-06 12:02:42 -08002137 /* Use the frame rate from the file only if none was specified
2138 * on the command-line.
2139 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002140 if (!global.have_framerate) {
2141 global.framerate.num = input.framerate.numerator;
2142 global.framerate.den = input.framerate.denominator;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002143 FOREACH_STREAM(stream, streams) {
2144 stream->config.cfg.g_timebase.den = global.framerate.num;
2145 stream->config.cfg.g_timebase.num = global.framerate.den;
2146 }
Tom Finegan00a35aa2013-11-14 12:37:42 -08002147 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002148
John Koleszar6ad3b742012-11-06 12:02:42 -08002149 /* Show configuration */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002150 if (global.verbose && pass == 0) {
2151 FOREACH_STREAM(stream, streams) {
2152 show_stream_config(stream, &global, &input);
2153 }
2154 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002155
2156 if (pass == (global.pass ? global.pass - 1 : 0)) {
2157 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002158 /*The Y4M reader does its own allocation.
2159 Just initialize this here to avoid problems if we never read any
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002160 frames.*/
John Koleszarc6b90392012-07-13 15:21:29 -07002161 memset(&raw, 0, sizeof(raw));
2162 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002163 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002164
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002165 FOREACH_STREAM(stream, streams) {
2166 stream->rate_hist =
2167 init_rate_histogram(&stream->config.cfg, &global.framerate);
2168 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002169 }
2170
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002171 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2172 FOREACH_STREAM(stream, streams) {
2173 open_output_file(stream, &global, &input.pixel_aspect_ratio);
2174 }
2175 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
Yaowu Xuf883b422016-08-30 14:01:10 -07002176 if (strcmp(global.codec->name, "av1") == 0 ||
2177 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002178 // Check to see if at least one stream uses 16 bit internal.
2179 // Currently assume that the bit_depths for all streams using
2180 // highbitdepth are the same.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002181 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002182 if (stream->config.use_16bit_internal) {
2183 use_16bit_internal = 1;
2184 }
2185 if (stream->config.cfg.g_profile == 0) {
2186 input_shift = 0;
2187 } else {
2188 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07002189 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002190 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002191 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002192 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002193
John Koleszarc6b90392012-07-13 15:21:29 -07002194 frame_avail = 1;
2195 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002196
John Koleszarc6b90392012-07-13 15:21:29 -07002197 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002198 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002199
John Koleszar6ad3b742012-11-06 12:02:42 -08002200 if (!global.limit || frames_in < global.limit) {
2201 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002202
clang-format6c4d83e2016-08-08 19:03:30 -07002203 if (frame_avail) frames_in++;
2204 seen_frames =
2205 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002206
John Koleszar6ad3b742012-11-06 12:02:42 -08002207 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002208 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002209 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2210
John Koleszar6ad3b742012-11-06 12:02:42 -08002211 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002212 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2213 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002214 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002215 fprintf(stderr, "frame %4d ", frames_in);
2216
clang-format6c4d83e2016-08-08 19:03:30 -07002217 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002218 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002219 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002220 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002221 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002222 }
2223
hui su251e1512016-10-03 15:48:43 -07002224 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002225 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002226 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002227
John Koleszar6ad3b742012-11-06 12:02:42 -08002228 if (frames_in > global.skip_frames) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002229 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002230 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2231 assert(use_16bit_internal);
2232 // Input bit depth and stream bit depth do not match, so up
2233 // shift frame to stream bit depth
2234 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002235 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002236 input.width, input.height, 32);
2237 allocated_raw_shift = 1;
2238 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002239 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002240 frame_to_encode = &raw_shift;
2241 } else {
2242 frame_to_encode = &raw;
2243 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002244 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002245 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002246 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002247 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002248 if (stream->config.use_16bit_internal)
2249 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002250 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002251 else
2252 assert(0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002253 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002254 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002255 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002256 FOREACH_STREAM(stream, streams) {
2257 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2258 frames_in);
2259 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002260 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002261 aom_usec_timer_mark(&timer);
2262 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002263
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002264 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
John Koleszarc6b90392012-07-13 15:21:29 -07002265
John Koleszar0ea50ce2010-05-18 11:58:33 -04002266 got_data = 0;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002267 FOREACH_STREAM(stream, streams) {
2268 get_cx_data(stream, &global, &got_data);
2269 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002270
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002271 if (!got_data && input.length && streams != NULL &&
2272 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002273 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002274 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002275 int64_t remaining;
2276 int64_t rate;
2277
2278 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002279 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002280
2281 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002282 remaining = 1000 * (global.limit - global.skip_frames -
2283 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002284 } else {
James Zerne3578af2014-04-17 10:47:08 -07002285 const int64_t input_pos = ftello(input.file);
2286 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002287 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002288
2289 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002290 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002291 }
2292
clang-format6c4d83e2016-08-08 19:03:30 -07002293 average_rate =
2294 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002295 estimated_time_left = average_rate ? remaining / average_rate : -1;
2296 }
2297
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002298 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2299 FOREACH_STREAM(stream, streams) {
Imdad Sardharwallab5def022017-12-14 11:20:24 +00002300 test_decode(stream, global.test_decode);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002301 }
2302 }
John Koleszarc6b90392012-07-13 15:21:29 -07002303 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002304
John Koleszarc6b90392012-07-13 15:21:29 -07002305 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002306 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002307 }
2308
clang-format6c4d83e2016-08-08 19:03:30 -07002309 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002310
Deb Mukherjeea160d722014-09-30 21:56:33 -07002311 if (!global.quiet) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002312 FOREACH_STREAM(stream, streams) {
2313 fprintf(stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2314 "b/f %7" PRId64
2315 "b/s"
2316 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2317 pass + 1, global.passes, frames_in, stream->frames_out,
2318 (int64_t)stream->nbytes,
2319 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2320 seen_frames
2321 ? (int64_t)stream->nbytes * 8 *
2322 (int64_t)global.framerate.num / global.framerate.den /
2323 seen_frames
2324 : 0,
2325 stream->cx_time > 9999999 ? stream->cx_time / 1000
2326 : stream->cx_time,
2327 stream->cx_time > 9999999 ? "ms" : "us",
2328 usec_to_fps(stream->cx_time, seen_frames));
2329 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002330 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002331
Deb Mukherjeea160d722014-09-30 21:56:33 -07002332 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002333 if (global.codec->fourcc == AV1_FOURCC) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002334 FOREACH_STREAM(stream, streams) {
2335 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2336 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002337 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002338 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002339 }
2340 }
John Koleszarc6b90392012-07-13 15:21:29 -07002341
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002342 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002343
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002344 if (global.test_decode != TEST_DECODE_OFF) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002345 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002346 }
2347
John Koleszar6ad3b742012-11-06 12:02:42 -08002348 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002349
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002350 if (global.test_decode == TEST_DECODE_FATAL) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002351 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002352 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002353 FOREACH_STREAM(stream, streams) {
2354 close_output_file(stream, global.codec->fourcc);
2355 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002356
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002357 FOREACH_STREAM(stream, streams) {
2358 stats_close(&stream->stats, global.passes - 1);
2359 }
John Koleszarc6b90392012-07-13 15:21:29 -07002360
Pengchong Jinf349b072014-07-14 09:13:38 -07002361#if CONFIG_FP_MB_STATS
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002362 FOREACH_STREAM(stream, streams) {
2363 stats_close(&stream->fpmb_stats, global.passes - 1);
2364 }
Pengchong Jinf349b072014-07-14 09:13:38 -07002365#endif
2366
clang-format6c4d83e2016-08-08 19:03:30 -07002367 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002368 }
2369
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002370 if (global.show_q_hist_buckets) {
2371 FOREACH_STREAM(stream, streams) {
2372 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2373 }
2374 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002375
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002376 if (global.show_rate_hist_buckets) {
2377 FOREACH_STREAM(stream, streams) {
2378 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2379 global.show_rate_hist_buckets);
2380 }
2381 }
2382 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002383
Ronald S. Bultje31214972012-10-11 10:13:48 -07002384#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002385 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2386 * to match some existing utilities.
2387 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002388 if (!(global.pass == 1 && global.passes == 2)) {
2389 FOREACH_STREAM(stream, streams) {
Yaowu Xu47e784e2013-10-16 16:29:28 -07002390 FILE *f = fopen("opsnr.stt", "a");
2391 if (stream->mismatch_seen) {
2392 fprintf(f, "First mismatch occurred in frame %d\n",
2393 stream->mismatch_seen);
2394 } else {
2395 fprintf(f, "No mismatch detected in recon buffers\n");
2396 }
2397 fclose(f);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002398 }
2399 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002400#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002401
Yaowu Xuf883b422016-08-30 14:01:10 -07002402 if (allocated_raw_shift) aom_img_free(&raw_shift);
Yaowu Xuf883b422016-08-30 14:01:10 -07002403 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002404 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002405 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002406 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002407}