blob: d607f8680cda2b9df539e797f0b37a1bcb032546 [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");
Debargha Mukherjee67adf422018-01-24 16:08:23 -0800160static const arg_def_t good_dl =
161 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
clang-format6c4d83e2016-08-08 19:03:30 -0700162static const arg_def_t quietarg =
163 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
164static const arg_def_t verbosearg =
165 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
166static const arg_def_t psnrarg =
167 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100168#if CONFIG_FILEOPTIONS
169static const arg_def_t use_cfg = ARG_DEF("c", "cfg", 1, "Config file to use");
170static const arg_def_t ext_partition =
171 ARG_DEF(NULL, "ext-partition", 1, "corresponds to CONFIG_EXT_PARTITION");
172#endif
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800173
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800174static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700175 { "off", TEST_DECODE_OFF },
176 { "fatal", TEST_DECODE_FATAL },
177 { "warn", TEST_DECODE_WARN },
178 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800179};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700180static const arg_def_t recontest = ARG_DEF_ENUM(
181 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700182static const arg_def_t framerate =
183 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
184static const arg_def_t use_webm =
185 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
186static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
Cyril Concolato6c788832017-10-30 16:30:35 -0700187#if CONFIG_OBU_NO_IVF
188static const arg_def_t use_obu = ARG_DEF(NULL, "obu", 0, "Output OBU");
189#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700190static const arg_def_t out_part =
191 ARG_DEF("P", "output-partitions", 0,
192 "Makes encoder output partitions. Requires IVF output!");
193static const arg_def_t q_hist_n =
194 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
195static const arg_def_t rate_hist_n =
196 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
197static const arg_def_t disable_warnings =
198 ARG_DEF(NULL, "disable-warnings", 0,
199 "Disable warnings about potentially incorrect encode settings.");
200static const arg_def_t disable_warning_prompt =
201 ARG_DEF("y", "disable-warning-prompt", 0,
202 "Display warnings, but do not prompt user to continue.");
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400203static const struct arg_enum_list bitdepth_enum[] = {
204 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
205};
206
207static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
208 "b", "bit-depth", 1,
209 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
210 bitdepth_enum);
211static const arg_def_t inbitdeptharg =
212 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
James Zernfd8c78c2017-11-27 16:37:33 -0800213static const arg_def_t *main_args[] = { &help,
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100214#if CONFIG_FILEOPTIONS
215 &use_cfg,
216#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800217 &debugmode,
clang-format6c4d83e2016-08-08 19:03:30 -0700218 &outputfile,
219 &codecarg,
220 &passes,
221 &pass_arg,
222 &fpf_name,
223 &limit,
224 &skip,
Debargha Mukherjee67adf422018-01-24 16:08:23 -0800225 &good_dl,
clang-format6c4d83e2016-08-08 19:03:30 -0700226 &quietarg,
227 &verbosearg,
228 &psnrarg,
229 &use_webm,
230 &use_ivf,
Cyril Concolato6c788832017-10-30 16:30:35 -0700231#if CONFIG_OBU_NO_IVF
232 &use_obu,
233#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700234 &out_part,
235 &q_hist_n,
236 &rate_hist_n,
237 &disable_warnings,
238 &disable_warning_prompt,
239 &recontest,
240 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400241
clang-format6c4d83e2016-08-08 19:03:30 -0700242static const arg_def_t usage =
243 ARG_DEF("u", "usage", 1, "Usage profile number to use");
244static const arg_def_t threads =
245 ARG_DEF("t", "threads", 1, "Max number of threads to use");
246static const arg_def_t profile =
247 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700248static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
249static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700250#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700251static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700252 { "mono", STEREO_FORMAT_MONO },
253 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
254 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
255 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
256 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
257 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700258};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700259static const arg_def_t stereo_mode = ARG_DEF_ENUM(
260 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700261#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700262static const arg_def_t timebase = ARG_DEF(
263 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700264static const arg_def_t error_resilient =
265 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
266static const arg_def_t lag_in_frames =
267 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700268#if CONFIG_EXT_TILE
269static const arg_def_t large_scale_tile =
270 ARG_DEF(NULL, "large-scale-tile", 1,
271 "Large scale tile coding (0: off (default), 1: on)");
272#endif // CONFIG_EXT_TILE
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800273#if CONFIG_MONO_VIDEO
274static const arg_def_t monochrome =
275 ARG_DEF(NULL, "monochrome", 0, "Monochrome video (no chroma planes)");
276#endif // CONFIG_MONO_VIDEO
John Koleszar0ea50ce2010-05-18 11:58:33 -0400277
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700278static const arg_def_t *global_args[] = { &use_yv12,
279 &use_i420,
280 &use_i422,
281 &use_i444,
282 &use_i440,
283 &usage,
284 &threads,
285 &profile,
286 &width,
287 &height,
James Zerndba73762014-05-10 17:44:12 -0700288#if CONFIG_WEBM_IO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700289 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700290#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700291 &timebase,
292 &framerate,
293 &error_resilient,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700294 &bitdeptharg,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700295 &lag_in_frames,
296#if CONFIG_EXT_TILE
297 &large_scale_tile,
298#endif // CONFIG_EXT_TILE
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800299#if CONFIG_MONO_VIDEO
300 &monochrome,
301#endif // CONFIG_MONO_VIDEO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700302 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400303
clang-format6c4d83e2016-08-08 19:03:30 -0700304static const arg_def_t dropframe_thresh =
305 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700306static const arg_def_t resize_mode =
307 ARG_DEF(NULL, "resize-mode", 1, "Frame resize mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700308static const arg_def_t resize_denominator =
309 ARG_DEF(NULL, "resize-denominator", 1, "Frame resize denominator");
310static const arg_def_t resize_kf_denominator = ARG_DEF(
311 NULL, "resize-kf-denominator", 1, "Frame resize keyframe denominator");
Urvang Joshi94ad3702017-12-06 11:38:08 -0800312#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700313static const arg_def_t superres_mode =
314 ARG_DEF(NULL, "superres-mode", 1, "Frame super-resolution mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700315static const arg_def_t superres_denominator = ARG_DEF(
316 NULL, "superres-denominator", 1, "Frame super-resolution denominator");
317static const arg_def_t superres_kf_denominator =
318 ARG_DEF(NULL, "superres-kf-denominator", 1,
319 "Frame super-resolution keyframe denominator");
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700320static const arg_def_t superres_qthresh = ARG_DEF(
321 NULL, "superres-qthresh", 1, "Frame super-resolution qindex threshold");
322static const arg_def_t superres_kf_qthresh =
323 ARG_DEF(NULL, "superres-kf-qthresh", 1,
324 "Frame super-resolution keyframe qindex threshold");
Urvang Joshi94ad3702017-12-06 11:38:08 -0800325#endif // CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700326static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
327 { "cbr", AOM_CBR },
328 { "cq", AOM_CQ },
329 { "q", AOM_Q },
330 { NULL, 0 } };
clang-format6c4d83e2016-08-08 19:03:30 -0700331static const arg_def_t end_usage =
332 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
333static const arg_def_t target_bitrate =
334 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
335static const arg_def_t min_quantizer =
336 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
337static const arg_def_t max_quantizer =
338 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
339static const arg_def_t undershoot_pct =
340 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
341static const arg_def_t overshoot_pct =
342 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
343static const arg_def_t buf_sz =
344 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
345static const arg_def_t buf_initial_sz =
346 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
347static const arg_def_t buf_optimal_sz =
348 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700349static const arg_def_t *rc_args[] = { &dropframe_thresh,
350 &resize_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700351 &resize_denominator,
352 &resize_kf_denominator,
Urvang Joshi94ad3702017-12-06 11:38:08 -0800353#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700354 &superres_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700355 &superres_denominator,
356 &superres_kf_denominator,
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700357 &superres_qthresh,
358 &superres_kf_qthresh,
Urvang Joshi94ad3702017-12-06 11:38:08 -0800359#endif // CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700360 &end_usage,
361 &target_bitrate,
362 &min_quantizer,
363 &max_quantizer,
364 &undershoot_pct,
365 &overshoot_pct,
366 &buf_sz,
367 &buf_initial_sz,
368 &buf_optimal_sz,
369 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400370
clang-format6c4d83e2016-08-08 19:03:30 -0700371static const arg_def_t bias_pct =
372 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
373static const arg_def_t minsection_pct =
374 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
375static const arg_def_t maxsection_pct =
376 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
377static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
378 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400379
clang-format6c4d83e2016-08-08 19:03:30 -0700380static const arg_def_t kf_min_dist =
381 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
382static const arg_def_t kf_max_dist =
383 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
384static const arg_def_t kf_disabled =
385 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
386static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
387 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400388
clang-format6c4d83e2016-08-08 19:03:30 -0700389static const arg_def_t noise_sens =
390 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
391static const arg_def_t sharpness =
392 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
393static const arg_def_t static_thresh =
394 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
395static const arg_def_t auto_altref =
396 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
397static const arg_def_t arnr_maxframes =
398 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
399static const arg_def_t arnr_strength =
400 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500401static const struct arg_enum_list tuning_enum[] = {
Yushin Chod808bfc2017-08-10 15:54:36 -0700402 { "psnr", AOM_TUNE_PSNR },
403 { "ssim", AOM_TUNE_SSIM },
404#ifdef CONFIG_DIST_8X8
405 { "cdef-dist", AOM_TUNE_CDEF_DIST },
406 { "daala-dist", AOM_TUNE_DAALA_DIST },
407#endif
408 { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500409};
Yushin Chod808bfc2017-08-10 15:54:36 -0700410static const arg_def_t tune_metric =
411 ARG_DEF_ENUM(NULL, "tune", 1, "Distortion metric tuned with", tuning_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700412static const arg_def_t cq_level =
413 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
414static const arg_def_t max_intra_rate_pct =
415 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800416
Yaowu Xuf883b422016-08-30 14:01:10 -0700417#if CONFIG_AV1_ENCODER
418static const arg_def_t cpu_used_av1 =
Yaowu Xu39737592017-04-21 16:39:09 -0700419 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800420static const arg_def_t dev_sf_av1 =
421 ARG_DEF(NULL, "dev-sf", 1, "Dev Speed (0..255)");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700422#if CONFIG_EXT_TILE
423static const arg_def_t single_tile_decoding =
424 ARG_DEF(NULL, "single-tile-decoding", 1,
425 "Single tile decoding (0: off (default), 1: on)");
426#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700427static const arg_def_t tile_cols =
428 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
429static const arg_def_t tile_rows =
430 ARG_DEF(NULL, "tile-rows", 1,
431 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200432#if CONFIG_MAX_TILE
433static const arg_def_t tile_width =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200434 ARG_DEF(NULL, "tile-width", 1, "Tile widths (comma separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200435static const arg_def_t tile_height =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200436 ARG_DEF(NULL, "tile-height", 1, "Tile heights (command separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200437#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800438#if CONFIG_DEPENDENT_HORZTILES
439static const arg_def_t tile_dependent_rows =
440 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
441#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800442#if CONFIG_LOOPFILTERING_ACROSS_TILES
Lei7bb501d2017-12-13 15:10:34 -0800443#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
444static const arg_def_t tile_loopfilter_v =
445 ARG_DEF(NULL, "tile-loopfilter-v", 1,
446 "Enable loop filter across vertical tile boundary");
447static const arg_def_t tile_loopfilter_h =
448 ARG_DEF(NULL, "tile-loopfilter-h", 1,
449 "Enable loop filter across horizontal tile boundary");
450#else
Ryan Lei7386eda2016-12-08 21:08:31 -0800451static const arg_def_t tile_loopfilter = ARG_DEF(
452 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Lei7bb501d2017-12-13 15:10:34 -0800453#endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800454#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700455static const arg_def_t lossless =
456 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100457static const arg_def_t enable_cdef =
458 ARG_DEF(NULL, "enable-cdef", 1,
459 "Enable the constrained directional enhancement filter (0: false, "
460 "1: true (default))");
Sebastien Alaiwan1ed20242018-02-20 14:47:18 +0100461static const arg_def_t enable_restoration =
462 ARG_DEF(NULL, "enable-restoration", 1,
463 "Enable the loop restoration filter (0: false, "
464 "1: true (default))");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700465#if CONFIG_AOM_QM
466static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800467 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700468 "Enable quantisation matrices (0: false (default), 1: true)");
469static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800470 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700471static const arg_def_t qm_max = ARG_DEF(
Yaowu Xubada8232018-01-30 17:21:30 -0800472 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 15");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700473#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700474#if CONFIG_DIST_8X8
475static const arg_def_t enable_dist_8x8 =
476 ARG_DEF(NULL, "enable-dist-8x8", 1,
477 "Enable dist-8x8 (0: false (default), 1: true)");
478#endif // CONFIG_DIST_8X8
Yaowu Xu859a5272016-11-10 15:32:21 -0800479static const arg_def_t num_tg = ARG_DEF(
480 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000481static const arg_def_t mtu_size =
482 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800483 "MTU size for a tile group, default is 0 (no MTU targeting), "
484 "overrides maximum number of tile groups");
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800485#if CONFIG_TIMING_INFO_IN_SEQ_HEADERS
486static const struct arg_enum_list timing_info_enum[] = {
487 { "unspecified", AOM_TIMING_UNSPECIFIED },
488 { "constant", AOM_TIMING_EQUAL },
489 { NULL, 0 }
490};
491static const arg_def_t timing_info =
492 ARG_DEF_ENUM(NULL, "timing-info", 1,
493 "Signal timing info in the bitstream:", timing_info_enum);
494#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800495#if CONFIG_FILM_GRAIN
496static const arg_def_t film_grain_test =
497 ARG_DEF(NULL, "film-grain-test", 1,
498 "Film grain test vectors (0: none (default), 1: test-1 2: test-2, "
499 "... 16: test-16)");
500#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800501static const arg_def_t disable_tempmv = ARG_DEF(
502 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
Yaowu Xud59fb482016-09-30 15:39:53 -0700503static const arg_def_t frame_parallel_decoding =
504 ARG_DEF(NULL, "frame-parallel", 1,
505 "Enable frame parallel decodability features "
506 "(0: false (default), 1: true)");
Thomas Davies3ab20b42017-09-19 10:30:53 +0100507#if !CONFIG_EXT_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100508static const arg_def_t aq_mode = ARG_DEF(
509 NULL, "aq-mode", 1,
510 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100511 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200512#else
Thomase28d92b2016-09-20 12:07:11 +0100513static const arg_def_t aq_mode = ARG_DEF(
514 NULL, "aq-mode", 1,
515 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100516 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200517#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700518#if CONFIG_EXT_DELTA_Q
519static const arg_def_t deltaq_mode = ARG_DEF(
520 NULL, "deltaq-mode", 1,
521 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
522#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700523static const arg_def_t frame_periodic_boost =
524 ARG_DEF(NULL, "frame-boost", 1,
525 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700526static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
527 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700528static const arg_def_t max_inter_rate_pct =
529 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700530static const arg_def_t min_gf_interval = ARG_DEF(
531 NULL, "min-gf-interval", 1,
532 "min gf/arf frame interval (default 0, indicating in-built behavior)");
533static const arg_def_t max_gf_interval = ARG_DEF(
534 NULL, "max-gf-interval", 1,
535 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800536
Andrey Norkin9e694632017-12-21 18:50:57 -0800537#if CONFIG_CICP
538static const struct arg_enum_list color_primaries_enum[] = {
539 { "bt709", AOM_CICP_CP_BT_709 },
540 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
541 { "bt601", AOM_CICP_CP_BT_601 },
542 { "bt470m", AOM_CICP_CP_BT_470_M },
543 { "bt470bg", AOM_CICP_CP_BT_470_B_G },
544 { "smpte240", AOM_CICP_CP_SMPTE_240 },
545 { "film", AOM_CICP_CP_GENERIC_FILM },
546 { "bt2020", AOM_CICP_CP_BT_2020 },
547 { "xyz", AOM_CICP_CP_XYZ },
548 { "smpte431", AOM_CICP_CP_SMPTE_431 },
549 { "smpte432", AOM_CICP_CP_SMPTE_432 },
550 { "ebu3213", AOM_CICP_CP_EBU_3213 },
551 { NULL, 0 }
552};
553
554static const arg_def_t input_color_primaries = ARG_DEF_ENUM(
555 NULL, "color-primaries", 1,
556 "Color primaries (CICP) of input content:", color_primaries_enum);
557
558static const struct arg_enum_list transfer_characteristics_enum[] = {
559 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
560 { "bt709", AOM_CICP_TC_BT_709 },
561 { "bt470m", AOM_CICP_TC_BT_470_M },
562 { "bt470bg", AOM_CICP_TC_BT_470_B_G },
563 { "bt601", AOM_CICP_TC_BT_601 },
564 { "smpte240", AOM_CICP_TC_SMPTE_240 },
565 { "lin", AOM_CICP_TC_LINEAR },
566 { "log100", AOM_CICP_TC_LOG_100 },
567 { "log100sq10", AOM_CICP_TC_LOG_100_SQRT10 },
568 { "iec61966", AOM_CICP_TC_IEC_61966 },
569 { "bt1361", AOM_CICP_TC_BT_1361 },
570 { "srgb", AOM_CICP_TC_SRGB },
571 { "bt2020-10bit", AOM_CICP_TC_BT_2020_10_BIT },
572 { "bt2020-12bit", AOM_CICP_TC_BT_2020_12_BIT },
573 { "smpte2084", AOM_CICP_TC_SMPTE_2084 },
574 { "hlg", AOM_CICP_TC_HLG },
575 { "smpte428", AOM_CICP_TC_SMPTE_428 },
576 { NULL, 0 }
577};
578
579static const arg_def_t input_transfer_characteristics =
580 ARG_DEF_ENUM(NULL, "transfer-characteristics", 1,
581 "Transfer characteristics (CICP) of input content:",
582 transfer_characteristics_enum);
583
584static const struct arg_enum_list matrix_coefficients_enum[] = {
585 { "identity", AOM_CICP_MC_IDENTITY },
586 { "bt709", AOM_CICP_MC_BT_709 },
587 { "unspecified", AOM_CICP_MC_UNSPECIFIED },
588 { "fcc73", AOM_CICP_MC_FCC },
589 { "bt470bg", AOM_CICP_MC_BT_470_B_G },
590 { "bt601", AOM_CICP_MC_BT_601 },
591 { "smpte240", AOM_CICP_CP_SMPTE_240 },
592 { "ycgco", AOM_CICP_MC_SMPTE_YCGCO },
593 { "bt2020ncl", AOM_CICP_MC_BT_2020_NCL },
594 { "bt2020cl", AOM_CICP_MC_BT_2020_CL },
595 { "smpte2085", AOM_CICP_MC_SMPTE_2085 },
596 { "chromncl", AOM_CICP_MC_CHROMAT_NCL },
597 { "chromcl", AOM_CICP_MC_CHROMAT_CL },
598 { "ictcp", AOM_CICP_MC_ICTCP },
599 { NULL, 0 }
600};
601
602static const arg_def_t input_matrix_coefficients = ARG_DEF_ENUM(
603 NULL, "matrix-coefficients", 1,
604 "Matrix coefficients (CICP) of input content:", matrix_coefficients_enum);
605
606#else
Yaowu Xufc996362015-02-10 15:03:05 -0800607static const struct arg_enum_list color_space_enum[] = {
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800608 { "unspecified", AOM_CS_UNKNOWN }, { "bt601", AOM_CS_BT_601 },
609 { "bt709", AOM_CS_BT_709 }, { "smpte170", AOM_CS_SMPTE_170 },
610 { "smpte240", AOM_CS_SMPTE_240 }, { "bt2020ncl", AOM_CS_BT_2020_NCL },
611 { "bt2020cl", AOM_CS_BT_2020_CL }, { "sRGB", AOM_CS_SRGB },
612 { "ictcp", AOM_CS_ICTCP }, { NULL, 0 }
Yaowu Xufc996362015-02-10 15:03:05 -0800613};
614
clang-format6c4d83e2016-08-08 19:03:30 -0700615static const arg_def_t input_color_space =
clang-format4eafefe2017-09-04 12:51:20 -0700616 ARG_DEF_ENUM(NULL, "color-space", 1,
617 "The color space of input content:", color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800618
anorkin76fb1262017-03-22 15:12:12 -0700619static const struct arg_enum_list transfer_function_enum[] = {
620 { "unknown", AOM_TF_UNKNOWN },
621 { "bt709", AOM_TF_BT_709 },
622 { "pq", AOM_TF_PQ },
623 { "hlg", AOM_TF_HLG },
624 { NULL, 0 }
625};
626
627static const arg_def_t input_transfer_function = ARG_DEF_ENUM(
clang-format4eafefe2017-09-04 12:51:20 -0700628 NULL, "transfer-function", 1,
629 "The transfer function of input content:", transfer_function_enum);
anorkin76fb1262017-03-22 15:12:12 -0700630
Andrey Norkin9e694632017-12-21 18:50:57 -0800631#endif
anorkin76fb1262017-03-22 15:12:12 -0700632static const struct arg_enum_list chroma_sample_position_enum[] = {
633 { "unknown", AOM_CSP_UNKNOWN },
634 { "vertical", AOM_CSP_VERTICAL },
635 { "colocated", AOM_CSP_COLOCATED },
636 { NULL, 0 }
637};
638
639static const arg_def_t input_chroma_sample_position =
640 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
641 "The chroma sample position when chroma 4:2:0 is signaled:",
642 chroma_sample_position_enum);
anorkin76fb1262017-03-22 15:12:12 -0700643
Alex Converse219f6452014-08-06 11:01:18 -0700644static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700645 { "default", AOM_CONTENT_DEFAULT },
646 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700647 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700648};
649
650static const arg_def_t tune_content = ARG_DEF_ENUM(
651 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
652
Hui Su1cb1c002018-02-05 18:21:20 -0800653#if CONFIG_CDF_UPDATE_MODE
654static const arg_def_t cdf_update_mode =
655 ARG_DEF(NULL, "cdf-update-mode", 1,
656 "CDF update rate for entropy coding "
657 "(0: off; 1: update all the time(default); "
658 "2: update half the time; "
659 "3: update quarter of the time)");
660#endif // CONFIG_CDF_UPDATE_MODE
661
Geza Lore454989f2016-03-24 13:56:05 +0000662#if CONFIG_EXT_PARTITION
663static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700664 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
665 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
666 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700667 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000668};
669static const arg_def_t superblock_size = ARG_DEF_ENUM(
670 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
671#endif // CONFIG_EXT_PARTITION
672
Yaowu Xuf883b422016-08-30 14:01:10 -0700673static const arg_def_t *av1_args[] = { &cpu_used_av1,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800674 &dev_sf_av1,
Yaowu Xuf883b422016-08-30 14:01:10 -0700675 &auto_altref,
676 &sharpness,
677 &static_thresh,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700678#if CONFIG_EXT_TILE
679 &single_tile_decoding,
680#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700681 &tile_cols,
682 &tile_rows,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800683#if CONFIG_DEPENDENT_HORZTILES
684 &tile_dependent_rows,
685#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800686#if CONFIG_LOOPFILTERING_ACROSS_TILES
Lei7bb501d2017-12-13 15:10:34 -0800687#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
688 &tile_loopfilter_v,
689 &tile_loopfilter_h,
690#else
Ryan Lei7386eda2016-12-08 21:08:31 -0800691 &tile_loopfilter,
Lei7bb501d2017-12-13 15:10:34 -0800692#endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800693#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700694 &arnr_maxframes,
695 &arnr_strength,
Yushin Chod808bfc2017-08-10 15:54:36 -0700696 &tune_metric,
Yaowu Xuf883b422016-08-30 14:01:10 -0700697 &cq_level,
698 &max_intra_rate_pct,
699 &max_inter_rate_pct,
700 &gf_cbr_boost_pct,
701 &lossless,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100702 &enable_cdef,
Sebastien Alaiwan1ed20242018-02-20 14:47:18 +0100703 &enable_restoration,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800704#if CONFIG_AOM_QM
705 &enable_qm,
706 &qm_min,
707 &qm_max,
708#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700709#if CONFIG_DIST_8X8
710 &enable_dist_8x8,
711#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700712 &frame_parallel_decoding,
713 &aq_mode,
Fangwen Fu6160df22017-04-24 09:45:51 -0700714#if CONFIG_EXT_DELTA_Q
715 &deltaq_mode,
716#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700717 &frame_periodic_boost,
718 &noise_sens,
719 &tune_content,
Hui Su1cb1c002018-02-05 18:21:20 -0800720#if CONFIG_CDF_UPDATE_MODE
721 &cdf_update_mode,
722#endif // CONFIG_CDF_UPDATE_MODE
Andrey Norkin9e694632017-12-21 18:50:57 -0800723#if CONFIG_CICP
724 &input_color_primaries,
725 &input_transfer_characteristics,
726 &input_matrix_coefficients,
727#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700728 &input_color_space,
anorkin76fb1262017-03-22 15:12:12 -0700729 &input_transfer_function,
Andrey Norkin9e694632017-12-21 18:50:57 -0800730#endif
anorkin76fb1262017-03-22 15:12:12 -0700731 &input_chroma_sample_position,
Yaowu Xuf883b422016-08-30 14:01:10 -0700732 &min_gf_interval,
733 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000734#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700735 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000736#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000737 &num_tg,
738 &mtu_size,
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800739#if CONFIG_TIMING_INFO_IN_SEQ_HEADERS
740 &timing_info,
741#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800742#if CONFIG_FILM_GRAIN
743 &film_grain_test,
744#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800745 &disable_tempmv,
Yaowu Xuf883b422016-08-30 14:01:10 -0700746 &bitdeptharg,
747 &inbitdeptharg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700748 NULL };
749static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800750 AOME_SET_DEVSF,
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 AOME_SET_ENABLEAUTOALTREF,
752 AOME_SET_SHARPNESS,
753 AOME_SET_STATIC_THRESHOLD,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700754#if CONFIG_EXT_TILE
755 AV1E_SET_SINGLE_TILE_DECODING,
756#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700757 AV1E_SET_TILE_COLUMNS,
758 AV1E_SET_TILE_ROWS,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800759#if CONFIG_DEPENDENT_HORZTILES
760 AV1E_SET_TILE_DEPENDENT_ROWS,
761#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800762#if CONFIG_LOOPFILTERING_ACROSS_TILES
Lei7bb501d2017-12-13 15:10:34 -0800763#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
764 AV1E_SET_TILE_LOOPFILTER_V,
765 AV1E_SET_TILE_LOOPFILTER_H,
766#else
Ryan Lei7386eda2016-12-08 21:08:31 -0800767 AV1E_SET_TILE_LOOPFILTER,
Lei7bb501d2017-12-13 15:10:34 -0800768#endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800769#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 AOME_SET_ARNR_MAXFRAMES,
771 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700772 AOME_SET_TUNING,
773 AOME_SET_CQ_LEVEL,
774 AOME_SET_MAX_INTRA_BITRATE_PCT,
775 AV1E_SET_MAX_INTER_BITRATE_PCT,
776 AV1E_SET_GF_CBR_BOOST_PCT,
777 AV1E_SET_LOSSLESS,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100778 AV1E_SET_ENABLE_CDEF,
Sebastien Alaiwan1ed20242018-02-20 14:47:18 +0100779 AV1E_SET_ENABLE_RESTORATION,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800780#if CONFIG_AOM_QM
781 AV1E_SET_ENABLE_QM,
782 AV1E_SET_QM_MIN,
783 AV1E_SET_QM_MAX,
784#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700785#if CONFIG_DIST_8X8
786 AV1E_SET_ENABLE_DIST_8X8,
787#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700788 AV1E_SET_FRAME_PARALLEL_DECODING,
789 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700790#if CONFIG_EXT_DELTA_Q
791 AV1E_SET_DELTAQ_MODE,
792#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700793 AV1E_SET_FRAME_PERIODIC_BOOST,
794 AV1E_SET_NOISE_SENSITIVITY,
795 AV1E_SET_TUNE_CONTENT,
Hui Su1cb1c002018-02-05 18:21:20 -0800796#if CONFIG_CDF_UPDATE_MODE
797 AV1E_SET_CDF_UPDATE_MODE,
798#endif // CONFIG_CDF_UPDATE_MODE
Andrey Norkin9e694632017-12-21 18:50:57 -0800799#if CONFIG_CICP
800 AV1E_SET_COLOR_PRIMARIES,
801 AV1E_SET_TRANSFER_CHARACTERISTICS,
802 AV1E_SET_MATRIX_COEFFICIENTS,
803#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700804 AV1E_SET_COLOR_SPACE,
anorkin76fb1262017-03-22 15:12:12 -0700805 AV1E_SET_TRANSFER_FUNCTION,
Andrey Norkin9e694632017-12-21 18:50:57 -0800806#endif
anorkin76fb1262017-03-22 15:12:12 -0700807 AV1E_SET_CHROMA_SAMPLE_POSITION,
Yaowu Xuf883b422016-08-30 14:01:10 -0700808 AV1E_SET_MIN_GF_INTERVAL,
809 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000810#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700811 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000812#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000813 AV1E_SET_NUM_TG,
814 AV1E_SET_MTU,
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800815#if CONFIG_TIMING_INFO_IN_SEQ_HEADERS
816 AV1E_SET_TIMING_INFO,
817#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800818#if CONFIG_FILM_GRAIN
819 AV1E_SET_FILM_GRAIN_TEST_VECTOR,
820#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800821 AV1E_SET_DISABLE_TEMPMV,
Jingning Hana446f552018-02-22 15:42:12 -0800822 AV1E_SET_ENABLE_DF,
Yaowu Xuf883b422016-08-30 14:01:10 -0700823 0 };
Hui Su1cb1c002018-02-05 18:21:20 -0800824#endif // CONFIG_AV1_ENCODER
hui su82331e02015-08-16 18:21:56 -0700825
John Koleszar0ea50ce2010-05-18 11:58:33 -0400826static const arg_def_t *no_args[] = { NULL };
827
James Zernfd8c78c2017-11-27 16:37:33 -0800828void show_help(FILE *fout, int shorthelp) {
829 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename \n",
John Koleszarc6b90392012-07-13 15:21:29 -0700830 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400831
James Zernfd8c78c2017-11-27 16:37:33 -0800832 if (shorthelp) {
833 fprintf(fout, "Use --help to see the full list of options.\n");
834 return;
835 }
836
837 fprintf(fout, "\nOptions:\n");
838 arg_show_usage(fout, main_args);
839 fprintf(fout, "\nEncoder Global Options:\n");
840 arg_show_usage(fout, global_args);
841 fprintf(fout, "\nRate Control Options:\n");
842 arg_show_usage(fout, rc_args);
843 fprintf(fout, "\nTwopass Rate Control Options:\n");
844 arg_show_usage(fout, rc_twopass_args);
845 fprintf(fout, "\nKeyframe Placement Options:\n");
846 arg_show_usage(fout, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700847#if CONFIG_AV1_ENCODER
James Zernfd8c78c2017-11-27 16:37:33 -0800848 fprintf(fout, "\nAV1 Specific Options:\n");
849 arg_show_usage(fout, av1_args);
hui su82331e02015-08-16 18:21:56 -0700850#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800851 fprintf(fout,
clang-format6c4d83e2016-08-08 19:03:30 -0700852 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700853 " The desired precision of timestamps in the output, expressed\n"
854 " in fractional seconds. Default is 1/1000.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800855 fprintf(fout, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400856
James Zernfd8c78c2017-11-27 16:37:33 -0800857 const int num_encoder = get_aom_encoder_count();
858 for (int i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700859 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700860 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
James Zernfd8c78c2017-11-27 16:37:33 -0800861 fprintf(fout, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700862 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800863 }
James Zernfd8c78c2017-11-27 16:37:33 -0800864 fprintf(fout, "\n ");
865 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
866}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400867
James Zernfd8c78c2017-11-27 16:37:33 -0800868void usage_exit(void) {
869 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700870 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400871}
872
Yaowu Xuf883b422016-08-30 14:01:10 -0700873#if CONFIG_AV1_ENCODER
874#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800875#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800876
James Zerndba73762014-05-10 17:44:12 -0700877#if !CONFIG_WEBM_IO
878typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700879struct WebmOutputContext {
880 int debug;
881};
James Zerndba73762014-05-10 17:44:12 -0700882#endif
883
John Koleszar9e50ed72012-02-15 12:39:38 -0800884/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800885struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700886 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700887 const char *out_fn;
888 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700889#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700890 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700891#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700892 stereo_format_t stereo_fmt;
893 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
894 int arg_ctrl_cnt;
895 int write_webm;
Cyril Concolato6c788832017-10-30 16:30:35 -0700896#if CONFIG_OBU_NO_IVF
897 int write_ivf;
898#endif
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700899 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700900 int use_16bit_internal;
John Koleszar9e50ed72012-02-15 12:39:38 -0800901};
902
John Koleszar6ad3b742012-11-06 12:02:42 -0800903struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700904 int index;
905 struct stream_state *next;
906 struct stream_config config;
907 FILE *file;
908 struct rate_hist *rate_hist;
909 struct WebmOutputContext webm_ctx;
910 uint64_t psnr_sse_total;
911 uint64_t psnr_samples_total;
912 double psnr_totals[4];
913 int psnr_count;
914 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700915 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700916 unsigned int frames_out;
917 uint64_t cx_time;
918 size_t nbytes;
919 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700920#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700921 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700922#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700923 struct aom_image *img;
924 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700925 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800926};
927
clang-format6c4d83e2016-08-08 19:03:30 -0700928static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700929 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800930 if (rat->den < 0) {
931 rat->num *= -1;
932 rat->den *= -1;
933 }
John Koleszar1b27e932012-04-25 17:08:56 -0700934
clang-format6c4d83e2016-08-08 19:03:30 -0700935 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700936
clang-format6c4d83e2016-08-08 19:03:30 -0700937 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700938}
939
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100940static void parse_global_config(struct AvxEncoderConfig *global, int *argc,
941 char ***argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700942 char **argi, **argj;
943 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700944 const int num_encoder = get_aom_encoder_count();
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100945 char **argv_local = (char **)*argv;
946#if CONFIG_FILEOPTIONS
947 int argc_local = *argc;
948#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700949 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800950
John Koleszar6ad3b742012-11-06 12:02:42 -0800951 /* Initialize default parameters */
952 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700953 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700954 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700955 global->color_type = I420;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400956
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100957#if CONFIG_FILEOPTIONS
958 const char *cfg = NULL;
959 int cfg_included = 0;
960#endif
961 for (argi = argj = argv_local; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -0700962 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400963
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100964#if CONFIG_FILEOPTIONS
965 if (arg_match(&arg, &use_cfg, argi)) {
966 if (cfg_included) continue;
967 cfg = arg.val;
968
969 arg_cfg(&argc_local, &argv_local, cfg);
970
971 *argj = *argi = *argv_local;
972 argj = argi = argv_local;
973 *argv = argv_local;
974 cfg_included = 1;
975 continue;
976 }
977#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800978 if (arg_match(&arg, &help, argi)) {
979 show_help(stdout, 0);
980 exit(EXIT_SUCCESS);
981 } else if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700982 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800983 if (!global->codec)
984 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700985 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800986 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400987
John Koleszar6ad3b742012-11-06 12:02:42 -0800988 if (global->passes < 1 || global->passes > 2)
989 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700990 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800991 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400992
John Koleszar6ad3b742012-11-06 12:02:42 -0800993 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700994 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800995 } else if (arg_match(&arg, &usage, argi))
996 global->usage = arg_parse_uint(&arg);
Debargha Mukherjee67adf422018-01-24 16:08:23 -0800997 else if (arg_match(&arg, &good_dl, argi))
998 warn("Deprecated --good option! Ignoring\n");
John Koleszar6ad3b742012-11-06 12:02:42 -0800999 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001000 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -08001001 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001002 global->color_type = I420;
1003 else if (arg_match(&arg, &use_i422, argi))
1004 global->color_type = I422;
1005 else if (arg_match(&arg, &use_i444, argi))
1006 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -07001007 else if (arg_match(&arg, &use_i440, argi))
1008 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -08001009 else if (arg_match(&arg, &quietarg, argi))
1010 global->quiet = 1;
1011 else if (arg_match(&arg, &verbosearg, argi))
1012 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -07001013 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001014 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -07001015 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001016 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -07001017 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001018 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -07001019 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001020 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -07001021 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001022 global->framerate = arg_parse_rational(&arg);
1023 validate_positive_rational(arg.name, &global->framerate);
1024 global->have_framerate = 1;
1025 } else if (arg_match(&arg, &out_part, argi))
1026 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -07001027 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001028 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -07001029 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001030 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -07001031 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001032 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -08001033 else if (arg_match(&arg, &disable_warnings, argi))
1034 global->disable_warnings = 1;
1035 else if (arg_match(&arg, &disable_warning_prompt, argi))
1036 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -08001037 else
John Koleszarc6b90392012-07-13 15:21:29 -07001038 argj++;
1039 }
1040
John Koleszar6ad3b742012-11-06 12:02:42 -08001041 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -07001042 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -08001043 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -07001044 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
1045 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -08001046 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -08001047 }
John Koleszarc6b90392012-07-13 15:21:29 -07001048 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001049 /* Validate global config */
1050 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001051#if CONFIG_AV1_ENCODER
1052 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001053 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001054 if (global->codec != NULL && global->codec->name != NULL)
Thomas Daede80826142017-03-20 15:44:24 -07001055 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001056#else
1057 global->passes = 1;
1058#endif
1059 }
John Koleszar732cb9a2012-02-14 12:30:17 -08001060}
1061
Yaowu Xuf883b422016-08-30 14:01:10 -07001062static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001063 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -07001064 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
1065 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -08001066
clang-format6c4d83e2016-08-08 19:03:30 -07001067 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -08001068
John Koleszar25b6e9f2013-02-12 21:17:56 -08001069 if (!fseeko(input->file, 0, SEEK_END)) {
1070 /* Input file is seekable. Figure out how long it is, so we can get
1071 * progress info.
1072 */
1073 input->length = ftello(input->file);
1074 rewind(input->file);
1075 }
1076
Frank Galligan09acd262015-06-01 10:20:58 -07001077 /* Default to 1:1 pixel aspect ratio. */
1078 input->pixel_aspect_ratio.numerator = 1;
1079 input->pixel_aspect_ratio.denominator = 1;
1080
John Koleszar6ad3b742012-11-06 12:02:42 -08001081 /* For RAW input sources, these bytes will applied on the first frame
1082 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -07001083 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001084 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1085 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001086
clang-format6c4d83e2016-08-08 19:03:30 -07001087 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -07001088 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
1089 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001090 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001091 input->width = input->y4m.pic_w;
1092 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -07001093 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1094 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001095 input->framerate.numerator = input->y4m.fps_n;
1096 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -07001097 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001098 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -08001099 } else
1100 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -08001101 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -08001102 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -08001103 } else {
1104 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001105 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001106}
1107
Yaowu Xuf883b422016-08-30 14:01:10 -07001108static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001109 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -07001110 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001111}
1112
Yaowu Xuf883b422016-08-30 14:01:10 -07001113static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -08001114 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001115 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001116
John Koleszar6ad3b742012-11-06 12:02:42 -08001117 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001118 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001119 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001120 }
1121
John Koleszar6ad3b742012-11-06 12:02:42 -08001122 if (prev) {
1123 memcpy(stream, prev, sizeof(*stream));
1124 stream->index++;
1125 prev->next = stream;
1126 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001127 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001128
John Koleszar6ad3b742012-11-06 12:02:42 -08001129 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -07001130 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -07001131 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -07001132 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001133
John Koleszar6ad3b742012-11-06 12:02:42 -08001134 /* Change the default timebase to a high enough value so that the
1135 * encoder will always create strictly increasing timestamps.
1136 */
1137 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001138
John Koleszar6ad3b742012-11-06 12:02:42 -08001139 /* Never use the library's default resolution, require it be parsed
1140 * from the file or set on the command line.
1141 */
1142 stream->config.cfg.g_w = 0;
1143 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001144
John Koleszar6ad3b742012-11-06 12:02:42 -08001145 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -08001146 stream->config.write_webm = 1;
Cyril Concolato6c788832017-10-30 16:30:35 -07001147#if CONFIG_OBU_NO_IVF
1148 stream->config.write_ivf = 0;
1149#endif
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001150#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -07001151 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001152 stream->webm_ctx.last_pts_ns = -1;
1153 stream->webm_ctx.writer = NULL;
1154 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001155#endif
Johann87c40b32012-03-01 16:12:53 -08001156
John Koleszar6ad3b742012-11-06 12:02:42 -08001157 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001158 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -08001159 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001160
John Koleszar6ad3b742012-11-06 12:02:42 -08001161 /* Output files must be specified for each stream */
1162 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001163
John Koleszar6ad3b742012-11-06 12:02:42 -08001164 stream->next = NULL;
1165 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001166}
1167
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +00001168static void set_config_arg_ctrls(struct stream_config *config, int key,
1169 const struct arg *arg) {
1170 int j;
1171 /* Point either to the next free element or the first instance of this
1172 * control.
1173 */
1174 for (j = 0; j < config->arg_ctrl_cnt; j++)
1175 if (config->arg_ctrls[j][0] == key) break;
1176
1177 /* Update/insert */
1178 assert(j < (int)ARG_CTRL_CNT_MAX);
1179 config->arg_ctrls[j][0] = key;
1180 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
1181 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1182}
1183
Yaowu Xuf883b422016-08-30 14:01:10 -07001184static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001185 struct stream_state *stream, char **argv) {
1186 char **argi, **argj;
1187 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001188 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001189 static const int *ctrl_args_map = NULL;
1190 struct stream_config *config = &stream->config;
1191 int eos_mark_found = 0;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001192 int webm_forced = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001193
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001194 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001195 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001196#if CONFIG_AV1_ENCODER
1197 } else if (strcmp(global->codec->name, "av1") == 0) {
1198 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1199 // Consider to expand this set for AV1 encoder control.
1200 ctrl_args = av1_args;
1201 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001202#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001203 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001204
John Koleszarc6b90392012-07-13 15:21:29 -07001205 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001206 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001207
John Koleszar6ad3b742012-11-06 12:02:42 -08001208 /* Once we've found an end-of-stream marker (--) we want to continue
1209 * shifting arguments but not consuming them.
1210 */
1211 if (eos_mark_found) {
1212 argj++;
1213 continue;
1214 } else if (!strcmp(*argj, "--")) {
1215 eos_mark_found = 1;
1216 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001217 }
1218
Adrian Granged2401802015-02-13 14:51:32 -08001219 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001220 config->out_fn = arg.val;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001221 if (!webm_forced) {
1222 const size_t out_fn_len = strlen(config->out_fn);
1223 if (out_fn_len >= 4 &&
1224 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
1225 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001226#if CONFIG_OBU_NO_IVF
1227 config->write_ivf = 1;
1228#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001229 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001230#if CONFIG_OBU_NO_IVF
1231 else if (out_fn_len >= 4 &&
1232 !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
1233 config->write_webm = 0;
1234 config->write_ivf = 0;
1235 }
1236#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001237 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001238 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001239 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001240#if CONFIG_FP_MB_STATS
1241 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1242 config->fpmb_stats_fn = arg.val;
1243#endif
Johannb50e5182015-01-30 15:05:14 -08001244 } else if (arg_match(&arg, &use_webm, argi)) {
1245#if CONFIG_WEBM_IO
1246 config->write_webm = 1;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001247 webm_forced = 1;
Johannb50e5182015-01-30 15:05:14 -08001248#else
1249 die("Error: --webm specified but webm is disabled.");
1250#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001251 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001252 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001253#if CONFIG_OBU_NO_IVF
1254 config->write_ivf = 1;
1255#endif
1256#if CONFIG_OBU_NO_IVF
1257 } else if (arg_match(&arg, &use_obu, argi)) {
1258 config->write_webm = 0;
1259 config->write_ivf = 0;
1260#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001261 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001262 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001263 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001264 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001265 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001266 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001267 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001268 config->cfg.g_h = arg_parse_uint(&arg);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001269 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1270 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1271 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1272 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
James Zerndba73762014-05-10 17:44:12 -07001273#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001274 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001275 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001276#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001277 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001278 config->cfg.g_timebase = arg_parse_rational(&arg);
1279 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001280 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001281 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001282 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001283 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001284#if CONFIG_EXT_TILE
1285 } else if (arg_match(&arg, &large_scale_tile, argi)) {
1286 config->cfg.large_scale_tile = arg_parse_uint(&arg);
1287#endif // CONFIG_EXT_TILE
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001288#if CONFIG_MONO_VIDEO
1289 } else if (arg_match(&arg, &monochrome, argi)) {
1290 config->cfg.monochrome = 1;
1291#endif // CONFIG_MONO_VIDEO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001292 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001293 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001294 } else if (arg_match(&arg, &resize_mode, argi)) {
1295 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001296 } else if (arg_match(&arg, &resize_denominator, argi)) {
1297 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1298 } else if (arg_match(&arg, &resize_kf_denominator, argi)) {
1299 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001300#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001301 } else if (arg_match(&arg, &superres_mode, argi)) {
1302 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001303 } else if (arg_match(&arg, &superres_denominator, argi)) {
1304 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1305 } else if (arg_match(&arg, &superres_kf_denominator, argi)) {
1306 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001307 } else if (arg_match(&arg, &superres_qthresh, argi)) {
1308 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1309 } else if (arg_match(&arg, &superres_kf_qthresh, argi)) {
1310 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001311#endif // CONFIG_HORZONLY_FRAME_SUPERRES
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001312 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001313 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001314 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001315 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001316 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001317 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001318 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001319 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001320 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001321 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001322 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001323 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001324 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001325 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001326 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001327 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001328 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001329 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001330 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001331 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001332 if (global->passes < 2)
1333 warn("option %s ignored in one-pass mode.\n", arg.name);
1334 } else if (arg_match(&arg, &minsection_pct, argi)) {
1335 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1336
1337 if (global->passes < 2)
1338 warn("option %s ignored in one-pass mode.\n", arg.name);
1339 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1340 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1341
1342 if (global->passes < 2)
1343 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001344 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001345 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001346 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001347 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001348 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001349 config->cfg.kf_mode = AOM_KF_DISABLED;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001350#if CONFIG_MAX_TILE
1351 } else if (arg_match(&arg, &tile_width, argi)) {
1352 config->cfg.tile_width_count =
1353 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1354 } else if (arg_match(&arg, &tile_height, argi)) {
1355 config->cfg.tile_height_count =
1356 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1357#endif
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01001358#if CONFIG_FILEOPTIONS
1359 } else if (arg_match(&arg, &ext_partition, argi)) {
1360 config->cfg.cfg.ext_partition = !!arg_parse_uint(&arg) > 0;
1361#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001362 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001363 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001364 for (i = 0; ctrl_args[i]; i++) {
1365 if (arg_match(&arg, ctrl_args[i], argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001366 match = 1;
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +00001367 if (ctrl_args_map) {
1368 set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001369 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001370 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001371 }
clang-format6c4d83e2016-08-08 19:03:30 -07001372 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001373 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001374 }
Sebastien Alaiwan14af5b92017-05-12 14:27:30 +02001375 config->use_16bit_internal =
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +02001376 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
John Koleszar6ad3b742012-11-06 12:02:42 -08001377 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001378}
1379
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001380#define FOREACH_STREAM(iterator, list) \
1381 for (struct stream_state *iterator = list; iterator; \
1382 iterator = iterator->next)
John Koleszar9e50ed72012-02-15 12:39:38 -08001383
Alex Conversed66bd222014-02-21 10:52:09 -08001384static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001385 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001386 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001387 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001388
John Koleszar6ad3b742012-11-06 12:02:42 -08001389 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001390 fatal(
1391 "Stream %d: Specify stream dimensions with --width (-w) "
1392 " and --height (-h)",
1393 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001394
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001395 // Check that the codec bit depth is greater than the input bit depth.
1396 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001397 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001398 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1399 stream->index, (int)stream->config.cfg.g_bit_depth,
1400 stream->config.cfg.g_input_bit_depth);
1401 }
1402
John Koleszar6ad3b742012-11-06 12:02:42 -08001403 for (streami = stream; streami; streami = streami->next) {
1404 /* All streams require output files */
1405 if (!streami->config.out_fn)
1406 fatal("Stream %d: Output file is required (specify with -o)",
1407 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001408
John Koleszar6ad3b742012-11-06 12:02:42 -08001409 /* Check for two streams outputting to the same file */
1410 if (streami != stream) {
1411 const char *a = stream->config.out_fn;
1412 const char *b = streami->config.out_fn;
1413 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1414 fatal("Stream %d: duplicate output file (from stream %d)",
1415 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001416 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001417
1418 /* Check for two streams sharing a stats file. */
1419 if (streami != stream) {
1420 const char *a = stream->config.stats_fn;
1421 const char *b = streami->config.stats_fn;
1422 if (a && b && !strcmp(a, b))
1423 fatal("Stream %d: duplicate stats file (from stream %d)",
1424 streami->index, stream->index);
1425 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001426
1427#if CONFIG_FP_MB_STATS
1428 /* Check for two streams sharing a mb stats file. */
1429 if (streami != stream) {
1430 const char *a = stream->config.fpmb_stats_fn;
1431 const char *b = streami->config.fpmb_stats_fn;
1432 if (a && b && !strcmp(a, b))
1433 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1434 streami->index, stream->index);
1435 }
1436#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001437 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001438}
1439
clang-format6c4d83e2016-08-08 19:03:30 -07001440static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001441 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001442 if (!stream->config.cfg.g_w) {
1443 if (!stream->config.cfg.g_h)
1444 stream->config.cfg.g_w = w;
1445 else
1446 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1447 }
1448 if (!stream->config.cfg.g_h) {
1449 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1450 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001451}
1452
clang-format6c4d83e2016-08-08 19:03:30 -07001453static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001454 switch (t) {
1455 case FILE_TYPE_RAW: return "RAW";
1456 case FILE_TYPE_Y4M: return "Y4M";
1457 default: return "Other";
1458 }
1459}
1460
Yaowu Xuf883b422016-08-30 14:01:10 -07001461static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001462 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001463 case AOM_IMG_FMT_I420: return "I420";
1464 case AOM_IMG_FMT_I422: return "I422";
1465 case AOM_IMG_FMT_I444: return "I444";
1466 case AOM_IMG_FMT_I440: return "I440";
1467 case AOM_IMG_FMT_YV12: return "YV12";
1468 case AOM_IMG_FMT_I42016: return "I42016";
1469 case AOM_IMG_FMT_I42216: return "I42216";
1470 case AOM_IMG_FMT_I44416: return "I44416";
1471 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001472 default: return "Other";
1473 }
1474}
Ralph Giles061a16d2012-01-05 15:05:05 -06001475
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001476static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001477 struct AvxEncoderConfig *global,
1478 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001479#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001480 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001481
John Koleszar6ad3b742012-11-06 12:02:42 -08001482 if (stream->index == 0) {
1483 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001484 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001485 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001486 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001487 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001488 }
1489 if (stream->next || stream->index)
1490 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1491 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
Sebastien Alaiwan27511922017-06-05 15:25:46 +02001492 fprintf(stderr, "Coding path: %s\n",
1493 stream->config.use_16bit_internal ? "HBD" : "LBD");
John Koleszar6ad3b742012-11-06 12:02:42 -08001494 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001495
John Koleszar6ad3b742012-11-06 12:02:42 -08001496 SHOW(g_usage);
1497 SHOW(g_threads);
1498 SHOW(g_profile);
1499 SHOW(g_w);
1500 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001501 SHOW(g_bit_depth);
1502 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001503 SHOW(g_timebase.num);
1504 SHOW(g_timebase.den);
1505 SHOW(g_error_resilient);
1506 SHOW(g_pass);
1507 SHOW(g_lag_in_frames);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001508#if CONFIG_EXT_TILE
1509 SHOW(large_scale_tile);
1510#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001511 SHOW(rc_dropframe_thresh);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001512 SHOW(rc_resize_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001513 SHOW(rc_resize_denominator);
1514 SHOW(rc_resize_kf_denominator);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001515#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001516 SHOW(rc_superres_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001517 SHOW(rc_superres_denominator);
1518 SHOW(rc_superres_kf_denominator);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001519 SHOW(rc_superres_qthresh);
1520 SHOW(rc_superres_kf_qthresh);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001521#endif // CONFIG_HORZONLY_FRAME_SUPERRES
John Koleszar6ad3b742012-11-06 12:02:42 -08001522 SHOW(rc_end_usage);
1523 SHOW(rc_target_bitrate);
1524 SHOW(rc_min_quantizer);
1525 SHOW(rc_max_quantizer);
1526 SHOW(rc_undershoot_pct);
1527 SHOW(rc_overshoot_pct);
1528 SHOW(rc_buf_sz);
1529 SHOW(rc_buf_initial_sz);
1530 SHOW(rc_buf_optimal_sz);
1531 SHOW(rc_2pass_vbr_bias_pct);
1532 SHOW(rc_2pass_vbr_minsection_pct);
1533 SHOW(rc_2pass_vbr_maxsection_pct);
1534 SHOW(kf_mode);
1535 SHOW(kf_min_dist);
1536 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001537}
1538
John Koleszar9e50ed72012-02-15 12:39:38 -08001539static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001540 struct AvxEncoderConfig *global,
1541 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001542 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001543 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001544
Yaowu Xuf883b422016-08-30 14:01:10 -07001545 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001546
John Koleszar6ad3b742012-11-06 12:02:42 -08001547 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001548
clang-format6c4d83e2016-08-08 19:03:30 -07001549 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001550
John Koleszar6ad3b742012-11-06 12:02:42 -08001551 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1552 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001553
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001554#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001555 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001556 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001557 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1558 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001559 }
James Zernc8e5a772016-02-11 18:53:50 -08001560#else
1561 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001562#endif
1563
Cyril Concolato6c788832017-10-30 16:30:35 -07001564 if (!stream->config.write_webm
1565#if CONFIG_OBU_NO_IVF
1566 && stream->config.write_ivf
1567#endif
Johann3c30fb42018-02-08 14:33:20 -08001568 ) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001569 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1570 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001571}
1572
John Koleszar9e50ed72012-02-15 12:39:38 -08001573static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001574 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001575 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001576
Yaowu Xuf883b422016-08-30 14:01:10 -07001577 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001578
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001579#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001580 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001581 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001582 }
1583#endif
1584
Cyril Concolato6c788832017-10-30 16:30:35 -07001585 if (!stream->config.write_webm
1586#if CONFIG_OBU_NO_IVF
1587 && stream->config.write_ivf
1588#endif
Johann3c30fb42018-02-08 14:33:20 -08001589 ) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001590 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001591 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001592 stream->frames_out);
1593 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001594
John Koleszar6ad3b742012-11-06 12:02:42 -08001595 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001596}
1597
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001598static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001599 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001600 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001601 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001602 fatal("Failed to open statistics store");
1603 } else {
1604 if (!stats_open_mem(&stream->stats, pass))
1605 fatal("Failed to open statistics store");
1606 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001607
Pengchong Jinf349b072014-07-14 09:13:38 -07001608#if CONFIG_FP_MB_STATS
1609 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001610 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1611 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001612 fatal("Failed to open mb statistics store");
1613 } else {
1614 if (!stats_open_mem(&stream->fpmb_stats, pass))
1615 fatal("Failed to open mb statistics store");
1616 }
1617#endif
1618
John Koleszar6ad3b742012-11-06 12:02:42 -08001619 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001620 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1621 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001622 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001623 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001624#if CONFIG_FP_MB_STATS
1625 stream->config.cfg.rc_firstpass_mb_stats_in =
1626 stats_get(&stream->fpmb_stats);
1627#endif
1628 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001629
John Koleszar6ad3b742012-11-06 12:02:42 -08001630 stream->cx_time = 0;
1631 stream->nbytes = 0;
1632 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001633}
1634
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001635static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001636 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001637 int i;
1638 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001639
Yaowu Xuf883b422016-08-30 14:01:10 -07001640 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1641 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001642 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001643
John Koleszar6ad3b742012-11-06 12:02:42 -08001644 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001645 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001646 &stream->config.cfg, flags);
1647 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001648
Yaowu Xuf883b422016-08-30 14:01:10 -07001649 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001650 * we're being clever to store the control IDs in an array. Real
1651 * applications will want to make use of the enumerations directly
1652 */
1653 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1654 int ctrl = stream->config.arg_ctrls[i][0];
1655 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001656 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001657 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001658
John Koleszar6ad3b742012-11-06 12:02:42 -08001659 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1660 }
1661
Tom Fineganba02c242017-05-16 15:01:54 -07001662#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001663 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001664 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01001665 aom_codec_dec_cfg_t cfg = {
1666 0, 0, 0, CONFIG_LOWBITDEPTH, { CONFIG_EXT_PARTITION }
1667 };
Yaowu Xuf883b422016-08-30 14:01:10 -07001668 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001669
Tom Fineganba02c242017-05-16 15:01:54 -07001670#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07001671 if (strcmp(global->codec->name, "av1") == 0) {
Yunqing Wang8ae64a92018-01-12 12:26:44 -08001672 aom_codec_control(&stream->decoder, AV1_SET_TILE_MODE,
1673 stream->config.cfg.large_scale_tile);
1674 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_mode");
1675
Yaowu Xuf883b422016-08-30 14:01:10 -07001676 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001677 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1678
Yaowu Xuf883b422016-08-30 14:01:10 -07001679 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001680 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1681 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001682#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001683 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001684#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001685}
1686
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001687static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001688 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001689 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001690 aom_codec_pts_t frame_start, next_frame_start;
1691 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1692 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001693
clang-format6c4d83e2016-08-08 19:03:30 -07001694 frame_start =
1695 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1696 cfg->g_timebase.num / global->framerate.num;
1697 next_frame_start =
1698 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1699 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001700
Yaowu Xud3e7c682017-12-21 14:08:25 -08001701 /* Scale if necessary */
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001702 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001703 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001704 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001705 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001706 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1707 exit(EXIT_FAILURE);
1708 }
1709#if CONFIG_LIBYUV
1710 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001711 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001712 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001713 }
clang-format6c4d83e2016-08-08 19:03:30 -07001714 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001715 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1716 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1717 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1718 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1719 stream->img->stride[AOM_PLANE_Y] / 2,
1720 (uint16 *)stream->img->planes[AOM_PLANE_U],
1721 stream->img->stride[AOM_PLANE_U] / 2,
1722 (uint16 *)stream->img->planes[AOM_PLANE_V],
1723 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001724 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001725 img = stream->img;
1726#else
clang-format6c4d83e2016-08-08 19:03:30 -07001727 stream->encoder.err = 1;
1728 ctx_exit_on_error(&stream->encoder,
1729 "Stream %d: Failed to encode frame.\n"
Johanne07a6752018-01-10 12:47:44 -08001730 "libyuv is required for scaling but is currently "
1731 "disabled.\n"
1732 "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1733 "cmake.\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001734 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001735#endif
1736 }
1737 }
John Koleszar34882b92012-03-01 12:50:40 -08001738 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001739 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001740 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1741 exit(EXIT_FAILURE);
1742 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001743#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001744 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001745 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001746 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001747 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001748 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1749 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1750 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1751 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1752 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1753 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001754 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001755 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001756#else
1757 stream->encoder.err = 1;
1758 ctx_exit_on_error(&stream->encoder,
1759 "Stream %d: Failed to encode frame.\n"
1760 "Scaling disabled in this configuration. \n"
1761 "To enable, configure with --enable-libyuv\n",
1762 stream->index);
1763#endif
John Koleszar34882b92012-03-01 12:50:40 -08001764 }
1765
Yaowu Xuf883b422016-08-30 14:01:10 -07001766 aom_usec_timer_start(&timer);
1767 aom_codec_encode(&stream->encoder, img, frame_start,
Sean DuBois47cc2552018-01-23 07:44:16 +00001768 (uint32_t)(next_frame_start - frame_start), 0);
Yaowu Xuf883b422016-08-30 14:01:10 -07001769 aom_usec_timer_mark(&timer);
1770 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001771 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1772 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001773}
1774
John Koleszar6ad3b742012-11-06 12:02:42 -08001775static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001776 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001777 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001778
Yaowu Xuf883b422016-08-30 14:01:10 -07001779 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001780 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1781 stream->counts[q]++;
1782 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001783}
1784
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001785static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001786 struct AvxEncoderConfig *global, int *got_data) {
1787 const aom_codec_cx_pkt_t *pkt;
1788 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1789 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001790
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001791 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001792 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001793 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001794 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001795
John Koleszar6ad3b742012-11-06 12:02:42 -08001796 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001797 case AOM_CODEC_CX_FRAME_PKT:
1798 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001799 stream->frames_out++;
1800 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001801 if (!global->quiet)
1802 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001803
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001804 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001805#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001806 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001807 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001808 }
1809#endif
1810 if (!stream->config.write_webm) {
Cyril Concolato6c788832017-10-30 16:30:35 -07001811#if CONFIG_OBU_NO_IVF
1812 if (stream->config.write_ivf) {
1813#endif
1814 if (pkt->data.frame.partition_id <= 0) {
1815 ivf_header_pos = ftello(stream->file);
1816 fsize = pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001817
Cyril Concolato6c788832017-10-30 16:30:35 -07001818 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1819 } else {
1820 fsize += pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001821
Cyril Concolato6c788832017-10-30 16:30:35 -07001822 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1823 const FileOffset currpos = ftello(stream->file);
1824 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1825 ivf_write_frame_size(stream->file, fsize);
1826 fseeko(stream->file, currpos, SEEK_SET);
1827 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001828 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001829#if CONFIG_OBU_NO_IVF
John Koleszar6ad3b742012-11-06 12:02:42 -08001830 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001831#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001832
clang-format6c4d83e2016-08-08 19:03:30 -07001833 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1834 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001835 }
1836 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001837
John Koleszar9e50ed72012-02-15 12:39:38 -08001838 *got_data = 1;
Tom Fineganba02c242017-05-16 15:01:54 -07001839#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001840 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001841 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Sean DuBois47cc2552018-01-23 07:44:16 +00001842 (unsigned int)pkt->data.frame.sz, NULL);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001843 if (stream->decoder.err) {
1844 warn_or_exit_on_error(&stream->decoder,
1845 global->test_decode == TEST_DECODE_FATAL,
1846 "Failed to decode frame %d in stream %d",
1847 stream->frames_out + 1, stream->index);
1848 stream->mismatch_seen = stream->frames_out + 1;
1849 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001850 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001851#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001852 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001853 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001854 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001855 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001856 pkt->data.twopass_stats.sz);
1857 stream->nbytes += pkt->data.raw.sz;
1858 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001859#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001860 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001861 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001862 pkt->data.firstpass_mb_stats.sz);
1863 stream->nbytes += pkt->data.raw.sz;
1864 break;
1865#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001866 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001867
1868 if (global->show_psnr) {
1869 int i;
1870
1871 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1872 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1873 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001874 if (!global->quiet)
1875 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001876 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1877 }
1878 stream->psnr_count++;
1879 }
1880
1881 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001882 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001883 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001884 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001885}
1886
clang-format6c4d83e2016-08-08 19:03:30 -07001887static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001888 int i;
1889 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001890
clang-format6c4d83e2016-08-08 19:03:30 -07001891 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001892
John Koleszar6ad3b742012-11-06 12:02:42 -08001893 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001894 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001895 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001896 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001897
John Koleszar6ad3b742012-11-06 12:02:42 -08001898 for (i = 0; i < 4; i++) {
1899 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1900 }
1901 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001902}
1903
John Koleszar25b6e9f2013-02-12 21:17:56 -08001904static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001905 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001906}
1907
clang-format6c4d83e2016-08-08 19:03:30 -07001908static void test_decode(struct stream_state *stream,
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001909 enum TestDecodeFatality fatal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001910 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001911
clang-format6c4d83e2016-08-08 19:03:30 -07001912 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001913
John Koleszarb3c350a2013-03-13 12:15:43 -07001914 /* Get the internal reference frame */
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001915 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1916 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001917
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001918 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1919 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1920 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1921 aom_image_t enc_hbd_img;
1922 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1923 enc_img.d_w, enc_img.d_h, 16);
1924 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1925 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001926 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001927 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1928 aom_image_t dec_hbd_img;
1929 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1930 dec_img.d_w, dec_img.d_h, 16);
1931 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1932 dec_img = dec_hbd_img;
1933 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001934 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001935
John Koleszar6ad3b742012-11-06 12:02:42 -08001936 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001937 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001938
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001939 if (!aom_compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001940 int y[4], u[4], v[4];
Yaowu Xuf883b422016-08-30 14:01:10 -07001941 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001942 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001943 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001944 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001945 }
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001946 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001947 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001948 "Stream %d: Encode/decode mismatch on frame %d at"
1949 " Y[%d, %d] {%d/%d},"
1950 " U[%d, %d] {%d/%d},"
1951 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001952 stream->index, stream->frames_out, y[0], y[1], y[2],
1953 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 -08001954 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001955 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001956
Yaowu Xuf883b422016-08-30 14:01:10 -07001957 aom_img_free(&enc_img);
1958 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001959}
John Koleszarefd54f82012-02-13 16:52:18 -08001960
John Koleszar25b6e9f2013-02-12 21:17:56 -08001961static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001962 int64_t hours;
1963 int64_t mins;
1964 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001965
1966 if (etl >= 0) {
1967 hours = etl / 3600;
1968 etl -= hours * 3600;
1969 mins = etl / 60;
1970 etl -= mins * 60;
1971 secs = etl;
1972
clang-format6c4d83e2016-08-08 19:03:30 -07001973 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1974 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001975 } else {
1976 fprintf(stderr, "[%3s unknown] ", label);
1977 }
1978}
1979
Tom Finegan44dd3272013-11-20 17:18:28 -08001980int main(int argc, const char **argv_) {
1981 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001982 aom_image_t raw;
Yaowu Xuf883b422016-08-30 14:01:10 -07001983 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001984 int allocated_raw_shift = 0;
1985 int use_16bit_internal = 0;
1986 int input_shift = 0;
Tom Finegan44dd3272013-11-20 17:18:28 -08001987 int frame_avail, got_data;
1988
Yaowu Xuf883b422016-08-30 14:01:10 -07001989 struct AvxInputContext input;
1990 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001991 struct stream_state *streams = NULL;
1992 char **argv, **argi;
1993 uint64_t cx_time = 0;
1994 int stream_cnt = 0;
1995 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001996 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001997
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001998 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001999 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08002000
John Koleszar6ad3b742012-11-06 12:02:42 -08002001 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002002 input.framerate.numerator = 30;
2003 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07002004 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07002005 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002006
2007 /* First parse the global configuration values, because we want to apply
2008 * other parameters on top of the default configuration provided by the
2009 * codec.
2010 */
2011 argv = argv_dup(argc - 1, argv_ + 1);
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002012 parse_global_config(&global, &argc, &argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002013
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002014#if CONFIG_FILEOPTIONS
2015 if (argc < 2) usage_exit();
2016#else
James Zernfd8c78c2017-11-27 16:37:33 -08002017 if (argc < 3) usage_exit();
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002018#endif
James Zernfd8c78c2017-11-27 16:37:33 -08002019
Deb Mukherjee090f4d42014-07-16 09:37:13 -07002020 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002021 case I420: input.fmt = AOM_IMG_FMT_I420; break;
2022 case I422: input.fmt = AOM_IMG_FMT_I422; break;
2023 case I444: input.fmt = AOM_IMG_FMT_I444; break;
2024 case I440: input.fmt = AOM_IMG_FMT_I440; break;
2025 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07002026 }
Tom Finegan44dd3272013-11-20 17:18:28 -08002027
John Koleszar6ad3b742012-11-06 12:02:42 -08002028 {
2029 /* Now parse each stream's parameters. Using a local scope here
2030 * due to the use of 'stream' as loop variable in FOREACH_STREAM
2031 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08002032 */
John Koleszar6ad3b742012-11-06 12:02:42 -08002033 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002034
John Koleszar6ad3b742012-11-06 12:02:42 -08002035 do {
2036 stream = new_stream(&global, stream);
2037 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07002038 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08002039 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07002040 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002041
John Koleszarc6b90392012-07-13 15:21:29 -07002042 /* Check for unrecognized options */
2043 for (argi = argv; *argi; argi++)
2044 if (argi[0][0] == '-' && argi[0][1])
2045 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002046
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002047 FOREACH_STREAM(stream, streams) {
2048 check_encoder_config(global.disable_warning_prompt, &global,
2049 &stream->config.cfg);
2050 }
Tom Finegan249366b2013-11-25 12:05:19 -08002051
John Koleszarc6b90392012-07-13 15:21:29 -07002052 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002053 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04002054
James Zernfd8c78c2017-11-27 16:37:33 -08002055 if (!input.filename) {
2056 fprintf(stderr, "No input file specified!\n");
2057 usage_exit();
2058 }
John Koleszar53291892010-10-22 14:48:21 -04002059
John Koleszar8dd82872013-05-06 11:01:35 -07002060 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07002061 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07002062
John Koleszar6ad3b742012-11-06 12:02:42 -08002063 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002064 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002065 int64_t estimated_time_left = -1;
2066 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07002067 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002068
John Koleszar6ad3b742012-11-06 12:02:42 -08002069 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002070
John Koleszar6ad3b742012-11-06 12:02:42 -08002071 /* If the input file doesn't specify its w/h (raw files), try to get
2072 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07002073 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07002074 if (!input.width || !input.height) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002075 FOREACH_STREAM(stream, streams) {
Deb Mukherjeea349ee32014-10-13 14:27:53 -07002076 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
2077 input.width = stream->config.cfg.g_w;
2078 input.height = stream->config.cfg.g_h;
2079 break;
2080 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002081 };
Deb Mukherjeea349ee32014-10-13 14:27:53 -07002082 }
John Koleszarc6b90392012-07-13 15:21:29 -07002083
John Koleszar6ad3b742012-11-06 12:02:42 -08002084 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002085 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07002086 fatal(
2087 "Specify stream dimensions with --width (-w) "
2088 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002089
2090 /* If input file does not specify bit-depth but input-bit-depth parameter
2091 * exists, assume that to be the input bit-depth. However, if the
2092 * input-bit-depth paramter does not exist, assume the input bit-depth
2093 * to be the same as the codec bit-depth.
2094 */
2095 if (!input.bit_depth) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002096 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002097 if (stream->config.cfg.g_input_bit_depth)
2098 input.bit_depth = stream->config.cfg.g_input_bit_depth;
2099 else
2100 input.bit_depth = stream->config.cfg.g_input_bit_depth =
2101 (int)stream->config.cfg.g_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002102 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002103 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002104 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002105 FOREACH_STREAM(stream, streams) {
2106 stream->config.cfg.g_input_bit_depth = input.bit_depth;
2107 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002108 }
2109
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002110 FOREACH_STREAM(stream, streams) {
Thomas Daede8ec53b22016-09-19 13:24:51 -07002111 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2112 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2113 was selected. */
2114 switch (stream->config.cfg.g_profile) {
2115 case 0:
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002116 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2117 input.fmt == AOM_IMG_FMT_I44416)) {
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002118 stream->config.cfg.g_profile = 1;
2119 profile_updated = 1;
2120 } else if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2121 input.fmt == AOM_IMG_FMT_I42216) {
2122 stream->config.cfg.g_profile = 2;
2123 profile_updated = 1;
2124 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002125 break;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002126 case 1:
2127 if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2128 input.fmt == AOM_IMG_FMT_I42216) {
2129 stream->config.cfg.g_profile = 2;
2130 profile_updated = 1;
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002131 } else if (input.bit_depth < 12 &&
2132 (input.fmt == AOM_IMG_FMT_I420 ||
2133 input.fmt == AOM_IMG_FMT_I42016)) {
2134 stream->config.cfg.g_profile = 0;
2135 profile_updated = 1;
2136 }
2137 break;
2138 case 2:
2139 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2140 input.fmt == AOM_IMG_FMT_I44416)) {
2141 stream->config.cfg.g_profile = 1;
2142 profile_updated = 1;
2143 } else if (input.bit_depth < 12 &&
2144 (input.fmt == AOM_IMG_FMT_I420 ||
2145 input.fmt == AOM_IMG_FMT_I42016)) {
2146 stream->config.cfg.g_profile = 0;
2147 profile_updated = 1;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002148 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002149 break;
2150 default: break;
2151 }
2152 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002153 /* Automatically set the codec bit depth to match the input bit depth.
2154 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002155 if (stream->config.cfg.g_input_bit_depth >
2156 (unsigned int)stream->config.cfg.g_bit_depth) {
2157 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2158 }
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002159 if (stream->config.cfg.g_bit_depth > 10) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002160 switch (stream->config.cfg.g_profile) {
2161 case 0:
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002162 case 1:
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002163 stream->config.cfg.g_profile = 2;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002164 profile_updated = 1;
2165 break;
2166 default: break;
2167 }
2168 }
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002169 if (stream->config.cfg.g_bit_depth > 8) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002170 stream->config.use_16bit_internal = 1;
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002171 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002172 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002173 fprintf(stderr,
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002174 "Warning: automatically updating to profile %d to "
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002175 "match input format.\n",
2176 stream->config.cfg.g_profile);
2177 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002178 }
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002179
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002180 FOREACH_STREAM(stream, streams) {
2181 set_stream_dimensions(stream, input.width, input.height);
2182 }
2183 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
John Koleszar77e6b452010-11-03 13:58:40 -04002184
John Koleszar6ad3b742012-11-06 12:02:42 -08002185 /* Ensure that --passes and --pass are consistent. If --pass is set and
2186 * --passes=2, ensure --fpf was set.
2187 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002188 if (global.pass && global.passes == 2) {
2189 FOREACH_STREAM(stream, streams) {
clang-format6c4d83e2016-08-08 19:03:30 -07002190 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002191 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002192 " and --passes=2\n",
2193 stream->index, global.pass);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002194 }
2195 }
John Koleszar3245d462010-06-10 17:10:12 -04002196
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002197#if !CONFIG_WEBM_IO
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002198 FOREACH_STREAM(stream, streams) {
Ronald S. Bultje39775072015-12-16 13:35:43 -05002199 if (stream->config.write_webm) {
2200 stream->config.write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07002201#if CONFIG_OBU_NO_IVF
2202 stream->config.write_ivf = 0;
2203#endif
clang-format6c4d83e2016-08-08 19:03:30 -07002204 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002205 "aomenc was compiled without WebM container support."
Cyril Concolato6c788832017-10-30 16:30:35 -07002206#if CONFIG_OBU_NO_IVF
2207 "Producing OBU output");
2208#else
clang-format6c4d83e2016-08-08 19:03:30 -07002209 "Producing IVF output");
Cyril Concolato6c788832017-10-30 16:30:35 -07002210#endif
Ronald S. Bultje39775072015-12-16 13:35:43 -05002211 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002212 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002213#endif
2214
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002215/* Use the frame rate from the file only if none was specified
2216 * on the command-line.
2217 */
2218#if CONFIG_TIMING_INFO_IN_SEQ_HEADERS
2219 if (!global.have_framerate) {
2220 global.framerate.num = input.framerate.numerator;
2221 global.framerate.den = input.framerate.denominator;
2222 }
2223 FOREACH_STREAM(stream, streams) {
2224 stream->config.cfg.g_timebase.den = global.framerate.num;
2225 stream->config.cfg.g_timebase.num = global.framerate.den;
2226 }
2227#else
Tom Finegan00a35aa2013-11-14 12:37:42 -08002228 if (!global.have_framerate) {
2229 global.framerate.num = input.framerate.numerator;
2230 global.framerate.den = input.framerate.denominator;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002231 FOREACH_STREAM(stream, streams) {
2232 stream->config.cfg.g_timebase.den = global.framerate.num;
2233 stream->config.cfg.g_timebase.num = global.framerate.den;
2234 }
Tom Finegan00a35aa2013-11-14 12:37:42 -08002235 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002236#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08002237 /* Show configuration */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002238 if (global.verbose && pass == 0) {
2239 FOREACH_STREAM(stream, streams) {
2240 show_stream_config(stream, &global, &input);
2241 }
2242 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002243
2244 if (pass == (global.pass ? global.pass - 1 : 0)) {
2245 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002246 /*The Y4M reader does its own allocation.
2247 Just initialize this here to avoid problems if we never read any
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002248 frames.*/
John Koleszarc6b90392012-07-13 15:21:29 -07002249 memset(&raw, 0, sizeof(raw));
2250 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002251 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002252
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002253 FOREACH_STREAM(stream, streams) {
2254 stream->rate_hist =
2255 init_rate_histogram(&stream->config.cfg, &global.framerate);
2256 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002257 }
2258
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002259 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2260 FOREACH_STREAM(stream, streams) {
2261 open_output_file(stream, &global, &input.pixel_aspect_ratio);
2262 }
2263 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
Yaowu Xuf883b422016-08-30 14:01:10 -07002264 if (strcmp(global.codec->name, "av1") == 0 ||
2265 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002266 // Check to see if at least one stream uses 16 bit internal.
2267 // Currently assume that the bit_depths for all streams using
2268 // highbitdepth are the same.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002269 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002270 if (stream->config.use_16bit_internal) {
2271 use_16bit_internal = 1;
2272 }
Yaowu Xu913867b2018-01-23 18:27:59 -08002273 input_shift = (int)stream->config.cfg.g_bit_depth -
2274 stream->config.cfg.g_input_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002275 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002276 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002277
John Koleszarc6b90392012-07-13 15:21:29 -07002278 frame_avail = 1;
2279 got_data = 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002280
John Koleszarc6b90392012-07-13 15:21:29 -07002281 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002282 struct aom_usec_timer timer;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002283
John Koleszar6ad3b742012-11-06 12:02:42 -08002284 if (!global.limit || frames_in < global.limit) {
2285 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002286
clang-format6c4d83e2016-08-08 19:03:30 -07002287 if (frame_avail) frames_in++;
2288 seen_frames =
2289 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002290
John Koleszar6ad3b742012-11-06 12:02:42 -08002291 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002292 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002293 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2294
John Koleszar6ad3b742012-11-06 12:02:42 -08002295 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002296 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2297 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002298 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002299 fprintf(stderr, "frame %4d ", frames_in);
2300
clang-format6c4d83e2016-08-08 19:03:30 -07002301 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002302 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002303 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002304 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002305 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002306 }
2307
hui su251e1512016-10-03 15:48:43 -07002308 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002309 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002310 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002311
John Koleszar6ad3b742012-11-06 12:02:42 -08002312 if (frames_in > global.skip_frames) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002313 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002314 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2315 assert(use_16bit_internal);
2316 // Input bit depth and stream bit depth do not match, so up
2317 // shift frame to stream bit depth
2318 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002319 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002320 input.width, input.height, 32);
2321 allocated_raw_shift = 1;
2322 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002323 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002324 frame_to_encode = &raw_shift;
2325 } else {
2326 frame_to_encode = &raw;
2327 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002328 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002329 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002330 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002331 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002332 if (stream->config.use_16bit_internal)
2333 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002334 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002335 else
2336 assert(0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002337 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002338 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002339 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002340 FOREACH_STREAM(stream, streams) {
2341 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2342 frames_in);
2343 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002344 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002345 aom_usec_timer_mark(&timer);
2346 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002347
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002348 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
John Koleszarc6b90392012-07-13 15:21:29 -07002349
John Koleszar0ea50ce2010-05-18 11:58:33 -04002350 got_data = 0;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002351 FOREACH_STREAM(stream, streams) {
2352 get_cx_data(stream, &global, &got_data);
2353 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002354
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002355 if (!got_data && input.length && streams != NULL &&
2356 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002357 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002358 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002359 int64_t remaining;
2360 int64_t rate;
2361
2362 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002363 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002364
2365 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002366 remaining = 1000 * (global.limit - global.skip_frames -
2367 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002368 } else {
James Zerne3578af2014-04-17 10:47:08 -07002369 const int64_t input_pos = ftello(input.file);
2370 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002371 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002372
2373 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002374 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002375 }
2376
clang-format6c4d83e2016-08-08 19:03:30 -07002377 average_rate =
2378 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002379 estimated_time_left = average_rate ? remaining / average_rate : -1;
2380 }
2381
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002382 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2383 FOREACH_STREAM(stream, streams) {
Imdad Sardharwallab5def022017-12-14 11:20:24 +00002384 test_decode(stream, global.test_decode);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002385 }
2386 }
John Koleszarc6b90392012-07-13 15:21:29 -07002387 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002388
John Koleszarc6b90392012-07-13 15:21:29 -07002389 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002390 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002391 }
2392
clang-format6c4d83e2016-08-08 19:03:30 -07002393 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002394
Deb Mukherjeea160d722014-09-30 21:56:33 -07002395 if (!global.quiet) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002396 FOREACH_STREAM(stream, streams) {
Johann3c30fb42018-02-08 14:33:20 -08002397 fprintf(stderr,
2398 "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2399 "b/f %7" PRId64
2400 "b/s"
2401 " %7" PRId64 " %s (%.2f fps)\033[K\n",
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002402 pass + 1, global.passes, frames_in, stream->frames_out,
2403 (int64_t)stream->nbytes,
2404 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
Johann3c30fb42018-02-08 14:33:20 -08002405 seen_frames ? (int64_t)stream->nbytes * 8 *
2406 (int64_t)global.framerate.num /
2407 global.framerate.den / seen_frames
2408 : 0,
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002409 stream->cx_time > 9999999 ? stream->cx_time / 1000
2410 : stream->cx_time,
2411 stream->cx_time > 9999999 ? "ms" : "us",
2412 usec_to_fps(stream->cx_time, seen_frames));
2413 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002414 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002415
Deb Mukherjeea160d722014-09-30 21:56:33 -07002416 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002417 if (global.codec->fourcc == AV1_FOURCC) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002418 FOREACH_STREAM(stream, streams) {
2419 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2420 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002421 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002422 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002423 }
2424 }
John Koleszarc6b90392012-07-13 15:21:29 -07002425
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002426 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002427
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002428 if (global.test_decode != TEST_DECODE_OFF) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002429 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002430 }
2431
John Koleszar6ad3b742012-11-06 12:02:42 -08002432 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002433
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002434 if (global.test_decode == TEST_DECODE_FATAL) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002435 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002436 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002437 FOREACH_STREAM(stream, streams) {
2438 close_output_file(stream, global.codec->fourcc);
2439 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002440
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002441 FOREACH_STREAM(stream, streams) {
2442 stats_close(&stream->stats, global.passes - 1);
2443 }
John Koleszarc6b90392012-07-13 15:21:29 -07002444
Pengchong Jinf349b072014-07-14 09:13:38 -07002445#if CONFIG_FP_MB_STATS
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002446 FOREACH_STREAM(stream, streams) {
2447 stats_close(&stream->fpmb_stats, global.passes - 1);
2448 }
Pengchong Jinf349b072014-07-14 09:13:38 -07002449#endif
2450
clang-format6c4d83e2016-08-08 19:03:30 -07002451 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002452 }
2453
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002454 if (global.show_q_hist_buckets) {
2455 FOREACH_STREAM(stream, streams) {
2456 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2457 }
2458 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002459
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002460 if (global.show_rate_hist_buckets) {
2461 FOREACH_STREAM(stream, streams) {
2462 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2463 global.show_rate_hist_buckets);
2464 }
2465 }
2466 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002467
Ronald S. Bultje31214972012-10-11 10:13:48 -07002468#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002469 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2470 * to match some existing utilities.
2471 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002472 if (!(global.pass == 1 && global.passes == 2)) {
2473 FOREACH_STREAM(stream, streams) {
Yaowu Xu47e784e2013-10-16 16:29:28 -07002474 FILE *f = fopen("opsnr.stt", "a");
2475 if (stream->mismatch_seen) {
2476 fprintf(f, "First mismatch occurred in frame %d\n",
2477 stream->mismatch_seen);
2478 } else {
2479 fprintf(f, "No mismatch detected in recon buffers\n");
2480 }
2481 fclose(f);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002482 }
2483 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002484#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002485
Yaowu Xuf883b422016-08-30 14:01:10 -07002486 if (allocated_raw_shift) aom_img_free(&raw_shift);
Yaowu Xuf883b422016-08-30 14:01:10 -07002487 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002488 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002489 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002490 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002491}