blob: 2886c07a76f3d7b25825ae04fa2b81001ff963d6 [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 =
James Zern1a79a7e2018-03-08 18:17:18 -0800171 ARG_DEF(NULL, "ext-partition", 1, "corresponds to extended partitions");
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100172#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");
Imdad Sardharwalla102c8652018-02-23 16:35:13 +0000250static const arg_def_t forced_max_frame_width = ARG_DEF(
251 NULL, "forced_max_frame_width", 0, "Maximum frame width value to force");
252static const arg_def_t forced_max_frame_height = ARG_DEF(
253 NULL, "forced_max_frame_height", 0, "Maximum frame height value to force");
James Zerndba73762014-05-10 17:44:12 -0700254#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700255static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700256 { "mono", STEREO_FORMAT_MONO },
257 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
258 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
259 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
260 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
261 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700262};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700263static const arg_def_t stereo_mode = ARG_DEF_ENUM(
264 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700265#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700266static const arg_def_t timebase = ARG_DEF(
267 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700268static const arg_def_t error_resilient =
269 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
270static const arg_def_t lag_in_frames =
271 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700272static const arg_def_t large_scale_tile =
273 ARG_DEF(NULL, "large-scale-tile", 1,
274 "Large scale tile coding (0: off (default), 1: on)");
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800275#if CONFIG_MONO_VIDEO
276static const arg_def_t monochrome =
277 ARG_DEF(NULL, "monochrome", 0, "Monochrome video (no chroma planes)");
278#endif // CONFIG_MONO_VIDEO
John Koleszar0ea50ce2010-05-18 11:58:33 -0400279
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700280static const arg_def_t *global_args[] = { &use_yv12,
281 &use_i420,
282 &use_i422,
283 &use_i444,
284 &use_i440,
285 &usage,
286 &threads,
287 &profile,
288 &width,
289 &height,
Imdad Sardharwalla102c8652018-02-23 16:35:13 +0000290 &forced_max_frame_width,
291 &forced_max_frame_height,
James Zerndba73762014-05-10 17:44:12 -0700292#if CONFIG_WEBM_IO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700293 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700294#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700295 &timebase,
296 &framerate,
297 &error_resilient,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700298 &bitdeptharg,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700299 &lag_in_frames,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700300 &large_scale_tile,
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800301#if CONFIG_MONO_VIDEO
302 &monochrome,
303#endif // CONFIG_MONO_VIDEO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700304 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400305
clang-format6c4d83e2016-08-08 19:03:30 -0700306static const arg_def_t dropframe_thresh =
307 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700308static const arg_def_t resize_mode =
309 ARG_DEF(NULL, "resize-mode", 1, "Frame resize mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700310static const arg_def_t resize_denominator =
311 ARG_DEF(NULL, "resize-denominator", 1, "Frame resize denominator");
312static const arg_def_t resize_kf_denominator = ARG_DEF(
313 NULL, "resize-kf-denominator", 1, "Frame resize keyframe denominator");
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700314static const arg_def_t superres_mode =
315 ARG_DEF(NULL, "superres-mode", 1, "Frame super-resolution mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700316static const arg_def_t superres_denominator = ARG_DEF(
317 NULL, "superres-denominator", 1, "Frame super-resolution denominator");
318static const arg_def_t superres_kf_denominator =
319 ARG_DEF(NULL, "superres-kf-denominator", 1,
320 "Frame super-resolution keyframe denominator");
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700321static const arg_def_t superres_qthresh = ARG_DEF(
322 NULL, "superres-qthresh", 1, "Frame super-resolution qindex threshold");
323static const arg_def_t superres_kf_qthresh =
324 ARG_DEF(NULL, "superres-kf-qthresh", 1,
325 "Frame super-resolution keyframe qindex threshold");
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,
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700353 &superres_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700354 &superres_denominator,
355 &superres_kf_denominator,
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700356 &superres_qthresh,
357 &superres_kf_qthresh,
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700358 &end_usage,
359 &target_bitrate,
360 &min_quantizer,
361 &max_quantizer,
362 &undershoot_pct,
363 &overshoot_pct,
364 &buf_sz,
365 &buf_initial_sz,
366 &buf_optimal_sz,
367 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400368
clang-format6c4d83e2016-08-08 19:03:30 -0700369static const arg_def_t bias_pct =
370 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
371static const arg_def_t minsection_pct =
372 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
373static const arg_def_t maxsection_pct =
374 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
375static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
376 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400377
clang-format6c4d83e2016-08-08 19:03:30 -0700378static const arg_def_t kf_min_dist =
379 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
380static const arg_def_t kf_max_dist =
381 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
382static const arg_def_t kf_disabled =
383 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
384static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
385 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400386
clang-format6c4d83e2016-08-08 19:03:30 -0700387static const arg_def_t noise_sens =
388 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
389static const arg_def_t sharpness =
390 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
391static const arg_def_t static_thresh =
392 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
393static const arg_def_t auto_altref =
394 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
395static const arg_def_t arnr_maxframes =
396 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
397static const arg_def_t arnr_strength =
398 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500399static const struct arg_enum_list tuning_enum[] = {
Yushin Chod808bfc2017-08-10 15:54:36 -0700400 { "psnr", AOM_TUNE_PSNR },
401 { "ssim", AOM_TUNE_SSIM },
402#ifdef CONFIG_DIST_8X8
403 { "cdef-dist", AOM_TUNE_CDEF_DIST },
404 { "daala-dist", AOM_TUNE_DAALA_DIST },
405#endif
406 { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500407};
Yushin Chod808bfc2017-08-10 15:54:36 -0700408static const arg_def_t tune_metric =
409 ARG_DEF_ENUM(NULL, "tune", 1, "Distortion metric tuned with", tuning_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700410static const arg_def_t cq_level =
411 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
412static const arg_def_t max_intra_rate_pct =
413 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800414
Yaowu Xuf883b422016-08-30 14:01:10 -0700415#if CONFIG_AV1_ENCODER
416static const arg_def_t cpu_used_av1 =
Yaowu Xu39737592017-04-21 16:39:09 -0700417 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800418static const arg_def_t dev_sf_av1 =
419 ARG_DEF(NULL, "dev-sf", 1, "Dev Speed (0..255)");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700420static const arg_def_t single_tile_decoding =
421 ARG_DEF(NULL, "single-tile-decoding", 1,
422 "Single tile decoding (0: off (default), 1: on)");
clang-format6c4d83e2016-08-08 19:03:30 -0700423static const arg_def_t tile_cols =
424 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
425static const arg_def_t tile_rows =
426 ARG_DEF(NULL, "tile-rows", 1,
427 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200428#if CONFIG_MAX_TILE
429static const arg_def_t tile_width =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200430 ARG_DEF(NULL, "tile-width", 1, "Tile widths (comma separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200431static const arg_def_t tile_height =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200432 ARG_DEF(NULL, "tile-height", 1, "Tile heights (command separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200433#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800434#if CONFIG_DEPENDENT_HORZTILES
435static const arg_def_t tile_dependent_rows =
436 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
437#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800438#if CONFIG_LOOPFILTERING_ACROSS_TILES
Lei7bb501d2017-12-13 15:10:34 -0800439#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
440static const arg_def_t tile_loopfilter_v =
441 ARG_DEF(NULL, "tile-loopfilter-v", 1,
442 "Enable loop filter across vertical tile boundary");
443static const arg_def_t tile_loopfilter_h =
444 ARG_DEF(NULL, "tile-loopfilter-h", 1,
445 "Enable loop filter across horizontal tile boundary");
446#else
Ryan Lei7386eda2016-12-08 21:08:31 -0800447static const arg_def_t tile_loopfilter = ARG_DEF(
448 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Lei7bb501d2017-12-13 15:10:34 -0800449#endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800450#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700451static const arg_def_t lossless =
452 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100453static const arg_def_t enable_cdef =
454 ARG_DEF(NULL, "enable-cdef", 1,
455 "Enable the constrained directional enhancement filter (0: false, "
456 "1: true (default))");
Sebastien Alaiwan1ed20242018-02-20 14:47:18 +0100457static const arg_def_t enable_restoration =
458 ARG_DEF(NULL, "enable-restoration", 1,
459 "Enable the loop restoration filter (0: false, "
460 "1: true (default))");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700461static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800462 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700463 "Enable quantisation matrices (0: false (default), 1: true)");
464static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800465 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700466static const arg_def_t qm_max = ARG_DEF(
Yaowu Xubada8232018-01-30 17:21:30 -0800467 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 15");
Yushin Chod808bfc2017-08-10 15:54:36 -0700468#if CONFIG_DIST_8X8
469static const arg_def_t enable_dist_8x8 =
470 ARG_DEF(NULL, "enable-dist-8x8", 1,
471 "Enable dist-8x8 (0: false (default), 1: true)");
472#endif // CONFIG_DIST_8X8
Yaowu Xu859a5272016-11-10 15:32:21 -0800473static const arg_def_t num_tg = ARG_DEF(
474 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000475static const arg_def_t mtu_size =
476 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800477 "MTU size for a tile group, default is 0 (no MTU targeting), "
478 "overrides maximum number of tile groups");
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800479static const struct arg_enum_list timing_info_enum[] = {
480 { "unspecified", AOM_TIMING_UNSPECIFIED },
481 { "constant", AOM_TIMING_EQUAL },
482 { NULL, 0 }
483};
484static const arg_def_t timing_info =
485 ARG_DEF_ENUM(NULL, "timing-info", 1,
486 "Signal timing info in the bitstream:", timing_info_enum);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800487#if CONFIG_FILM_GRAIN
488static const arg_def_t film_grain_test =
489 ARG_DEF(NULL, "film-grain-test", 1,
490 "Film grain test vectors (0: none (default), 1: test-1 2: test-2, "
491 "... 16: test-16)");
492#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800493static const arg_def_t disable_tempmv = ARG_DEF(
494 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
Yaowu Xud59fb482016-09-30 15:39:53 -0700495static const arg_def_t frame_parallel_decoding =
496 ARG_DEF(NULL, "frame-parallel", 1,
497 "Enable frame parallel decodability features "
498 "(0: false (default), 1: true)");
Thomas Davies3ab20b42017-09-19 10:30:53 +0100499#if !CONFIG_EXT_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100500static const arg_def_t aq_mode = ARG_DEF(
501 NULL, "aq-mode", 1,
502 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100503 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200504#else
Thomase28d92b2016-09-20 12:07:11 +0100505static const arg_def_t aq_mode = ARG_DEF(
506 NULL, "aq-mode", 1,
507 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100508 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200509#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700510#if CONFIG_EXT_DELTA_Q
511static const arg_def_t deltaq_mode = ARG_DEF(
512 NULL, "deltaq-mode", 1,
513 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
514#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700515static const arg_def_t frame_periodic_boost =
516 ARG_DEF(NULL, "frame-boost", 1,
517 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700518static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
519 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700520static const arg_def_t max_inter_rate_pct =
521 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700522static const arg_def_t min_gf_interval = ARG_DEF(
523 NULL, "min-gf-interval", 1,
524 "min gf/arf frame interval (default 0, indicating in-built behavior)");
525static const arg_def_t max_gf_interval = ARG_DEF(
526 NULL, "max-gf-interval", 1,
527 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800528
Andrey Norkin9e694632017-12-21 18:50:57 -0800529static const struct arg_enum_list color_primaries_enum[] = {
530 { "bt709", AOM_CICP_CP_BT_709 },
531 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
532 { "bt601", AOM_CICP_CP_BT_601 },
533 { "bt470m", AOM_CICP_CP_BT_470_M },
534 { "bt470bg", AOM_CICP_CP_BT_470_B_G },
535 { "smpte240", AOM_CICP_CP_SMPTE_240 },
536 { "film", AOM_CICP_CP_GENERIC_FILM },
537 { "bt2020", AOM_CICP_CP_BT_2020 },
538 { "xyz", AOM_CICP_CP_XYZ },
539 { "smpte431", AOM_CICP_CP_SMPTE_431 },
540 { "smpte432", AOM_CICP_CP_SMPTE_432 },
541 { "ebu3213", AOM_CICP_CP_EBU_3213 },
542 { NULL, 0 }
543};
544
545static const arg_def_t input_color_primaries = ARG_DEF_ENUM(
546 NULL, "color-primaries", 1,
547 "Color primaries (CICP) of input content:", color_primaries_enum);
548
549static const struct arg_enum_list transfer_characteristics_enum[] = {
550 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
551 { "bt709", AOM_CICP_TC_BT_709 },
552 { "bt470m", AOM_CICP_TC_BT_470_M },
553 { "bt470bg", AOM_CICP_TC_BT_470_B_G },
554 { "bt601", AOM_CICP_TC_BT_601 },
555 { "smpte240", AOM_CICP_TC_SMPTE_240 },
556 { "lin", AOM_CICP_TC_LINEAR },
557 { "log100", AOM_CICP_TC_LOG_100 },
558 { "log100sq10", AOM_CICP_TC_LOG_100_SQRT10 },
559 { "iec61966", AOM_CICP_TC_IEC_61966 },
560 { "bt1361", AOM_CICP_TC_BT_1361 },
561 { "srgb", AOM_CICP_TC_SRGB },
562 { "bt2020-10bit", AOM_CICP_TC_BT_2020_10_BIT },
563 { "bt2020-12bit", AOM_CICP_TC_BT_2020_12_BIT },
564 { "smpte2084", AOM_CICP_TC_SMPTE_2084 },
565 { "hlg", AOM_CICP_TC_HLG },
566 { "smpte428", AOM_CICP_TC_SMPTE_428 },
567 { NULL, 0 }
568};
569
570static const arg_def_t input_transfer_characteristics =
571 ARG_DEF_ENUM(NULL, "transfer-characteristics", 1,
572 "Transfer characteristics (CICP) of input content:",
573 transfer_characteristics_enum);
574
575static const struct arg_enum_list matrix_coefficients_enum[] = {
576 { "identity", AOM_CICP_MC_IDENTITY },
577 { "bt709", AOM_CICP_MC_BT_709 },
578 { "unspecified", AOM_CICP_MC_UNSPECIFIED },
579 { "fcc73", AOM_CICP_MC_FCC },
580 { "bt470bg", AOM_CICP_MC_BT_470_B_G },
581 { "bt601", AOM_CICP_MC_BT_601 },
582 { "smpte240", AOM_CICP_CP_SMPTE_240 },
583 { "ycgco", AOM_CICP_MC_SMPTE_YCGCO },
584 { "bt2020ncl", AOM_CICP_MC_BT_2020_NCL },
585 { "bt2020cl", AOM_CICP_MC_BT_2020_CL },
586 { "smpte2085", AOM_CICP_MC_SMPTE_2085 },
587 { "chromncl", AOM_CICP_MC_CHROMAT_NCL },
588 { "chromcl", AOM_CICP_MC_CHROMAT_CL },
589 { "ictcp", AOM_CICP_MC_ICTCP },
590 { NULL, 0 }
591};
592
593static const arg_def_t input_matrix_coefficients = ARG_DEF_ENUM(
594 NULL, "matrix-coefficients", 1,
595 "Matrix coefficients (CICP) of input content:", matrix_coefficients_enum);
596
anorkin76fb1262017-03-22 15:12:12 -0700597static const struct arg_enum_list chroma_sample_position_enum[] = {
598 { "unknown", AOM_CSP_UNKNOWN },
599 { "vertical", AOM_CSP_VERTICAL },
600 { "colocated", AOM_CSP_COLOCATED },
601 { NULL, 0 }
602};
603
604static const arg_def_t input_chroma_sample_position =
605 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
606 "The chroma sample position when chroma 4:2:0 is signaled:",
607 chroma_sample_position_enum);
anorkin76fb1262017-03-22 15:12:12 -0700608
Alex Converse219f6452014-08-06 11:01:18 -0700609static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700610 { "default", AOM_CONTENT_DEFAULT },
611 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700612 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700613};
614
615static const arg_def_t tune_content = ARG_DEF_ENUM(
616 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
617
Hui Su1cb1c002018-02-05 18:21:20 -0800618#if CONFIG_CDF_UPDATE_MODE
619static const arg_def_t cdf_update_mode =
620 ARG_DEF(NULL, "cdf-update-mode", 1,
Hui Su483a8452018-02-26 12:28:48 -0800621 "CDF update mode for entropy coding "
622 "(0: no CDF update; 1: update CDF on all frames(default); "
623 "2: selectively update CDF on some frames");
Hui Su1cb1c002018-02-05 18:21:20 -0800624#endif // CONFIG_CDF_UPDATE_MODE
625
Geza Lore454989f2016-03-24 13:56:05 +0000626static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700627 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
628 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
629 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700630 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000631};
632static const arg_def_t superblock_size = ARG_DEF_ENUM(
633 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
Geza Lore454989f2016-03-24 13:56:05 +0000634
Yaowu Xuf883b422016-08-30 14:01:10 -0700635static const arg_def_t *av1_args[] = { &cpu_used_av1,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800636 &dev_sf_av1,
Yaowu Xuf883b422016-08-30 14:01:10 -0700637 &auto_altref,
638 &sharpness,
639 &static_thresh,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700640 &single_tile_decoding,
Yaowu Xuf883b422016-08-30 14:01:10 -0700641 &tile_cols,
642 &tile_rows,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800643#if CONFIG_DEPENDENT_HORZTILES
644 &tile_dependent_rows,
645#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800646#if CONFIG_LOOPFILTERING_ACROSS_TILES
Lei7bb501d2017-12-13 15:10:34 -0800647#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
648 &tile_loopfilter_v,
649 &tile_loopfilter_h,
650#else
Ryan Lei7386eda2016-12-08 21:08:31 -0800651 &tile_loopfilter,
Lei7bb501d2017-12-13 15:10:34 -0800652#endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800653#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 &arnr_maxframes,
655 &arnr_strength,
Yushin Chod808bfc2017-08-10 15:54:36 -0700656 &tune_metric,
Yaowu Xuf883b422016-08-30 14:01:10 -0700657 &cq_level,
658 &max_intra_rate_pct,
659 &max_inter_rate_pct,
660 &gf_cbr_boost_pct,
661 &lossless,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100662 &enable_cdef,
Sebastien Alaiwan1ed20242018-02-20 14:47:18 +0100663 &enable_restoration,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800664 &enable_qm,
665 &qm_min,
666 &qm_max,
Yushin Chod808bfc2017-08-10 15:54:36 -0700667#if CONFIG_DIST_8X8
668 &enable_dist_8x8,
669#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700670 &frame_parallel_decoding,
671 &aq_mode,
Fangwen Fu6160df22017-04-24 09:45:51 -0700672#if CONFIG_EXT_DELTA_Q
673 &deltaq_mode,
674#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700675 &frame_periodic_boost,
676 &noise_sens,
677 &tune_content,
Hui Su1cb1c002018-02-05 18:21:20 -0800678#if CONFIG_CDF_UPDATE_MODE
679 &cdf_update_mode,
680#endif // CONFIG_CDF_UPDATE_MODE
Andrey Norkin9e694632017-12-21 18:50:57 -0800681 &input_color_primaries,
682 &input_transfer_characteristics,
683 &input_matrix_coefficients,
anorkin76fb1262017-03-22 15:12:12 -0700684 &input_chroma_sample_position,
Yaowu Xuf883b422016-08-30 14:01:10 -0700685 &min_gf_interval,
686 &max_gf_interval,
Yaowu Xuf883b422016-08-30 14:01:10 -0700687 &superblock_size,
Thomas Daviesaf6df172016-11-09 14:04:18 +0000688 &num_tg,
689 &mtu_size,
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800690 &timing_info,
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800691#if CONFIG_FILM_GRAIN
692 &film_grain_test,
693#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800694 &disable_tempmv,
Yaowu Xuf883b422016-08-30 14:01:10 -0700695 &bitdeptharg,
696 &inbitdeptharg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700697 NULL };
698static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800699 AOME_SET_DEVSF,
Yaowu Xuf883b422016-08-30 14:01:10 -0700700 AOME_SET_ENABLEAUTOALTREF,
701 AOME_SET_SHARPNESS,
702 AOME_SET_STATIC_THRESHOLD,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700703 AV1E_SET_SINGLE_TILE_DECODING,
Yaowu Xuf883b422016-08-30 14:01:10 -0700704 AV1E_SET_TILE_COLUMNS,
705 AV1E_SET_TILE_ROWS,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800706#if CONFIG_DEPENDENT_HORZTILES
707 AV1E_SET_TILE_DEPENDENT_ROWS,
708#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800709#if CONFIG_LOOPFILTERING_ACROSS_TILES
Lei7bb501d2017-12-13 15:10:34 -0800710#if CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
711 AV1E_SET_TILE_LOOPFILTER_V,
712 AV1E_SET_TILE_LOOPFILTER_H,
713#else
Ryan Lei7386eda2016-12-08 21:08:31 -0800714 AV1E_SET_TILE_LOOPFILTER,
Lei7bb501d2017-12-13 15:10:34 -0800715#endif // CONFIG_LOOPFILTERING_ACROSS_TILES_EXT
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800716#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700717 AOME_SET_ARNR_MAXFRAMES,
718 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700719 AOME_SET_TUNING,
720 AOME_SET_CQ_LEVEL,
721 AOME_SET_MAX_INTRA_BITRATE_PCT,
722 AV1E_SET_MAX_INTER_BITRATE_PCT,
723 AV1E_SET_GF_CBR_BOOST_PCT,
724 AV1E_SET_LOSSLESS,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100725 AV1E_SET_ENABLE_CDEF,
Sebastien Alaiwan1ed20242018-02-20 14:47:18 +0100726 AV1E_SET_ENABLE_RESTORATION,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800727 AV1E_SET_ENABLE_QM,
728 AV1E_SET_QM_MIN,
729 AV1E_SET_QM_MAX,
Yushin Chod808bfc2017-08-10 15:54:36 -0700730#if CONFIG_DIST_8X8
731 AV1E_SET_ENABLE_DIST_8X8,
732#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700733 AV1E_SET_FRAME_PARALLEL_DECODING,
734 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700735#if CONFIG_EXT_DELTA_Q
736 AV1E_SET_DELTAQ_MODE,
737#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700738 AV1E_SET_FRAME_PERIODIC_BOOST,
739 AV1E_SET_NOISE_SENSITIVITY,
740 AV1E_SET_TUNE_CONTENT,
Hui Su1cb1c002018-02-05 18:21:20 -0800741#if CONFIG_CDF_UPDATE_MODE
742 AV1E_SET_CDF_UPDATE_MODE,
743#endif // CONFIG_CDF_UPDATE_MODE
Andrey Norkin9e694632017-12-21 18:50:57 -0800744 AV1E_SET_COLOR_PRIMARIES,
745 AV1E_SET_TRANSFER_CHARACTERISTICS,
746 AV1E_SET_MATRIX_COEFFICIENTS,
anorkin76fb1262017-03-22 15:12:12 -0700747 AV1E_SET_CHROMA_SAMPLE_POSITION,
Yaowu Xuf883b422016-08-30 14:01:10 -0700748 AV1E_SET_MIN_GF_INTERVAL,
749 AV1E_SET_MAX_GF_INTERVAL,
Yaowu Xuf883b422016-08-30 14:01:10 -0700750 AV1E_SET_SUPERBLOCK_SIZE,
Thomas Daviesaf6df172016-11-09 14:04:18 +0000751 AV1E_SET_NUM_TG,
752 AV1E_SET_MTU,
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800753 AV1E_SET_TIMING_INFO,
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800754#if CONFIG_FILM_GRAIN
755 AV1E_SET_FILM_GRAIN_TEST_VECTOR,
756#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800757 AV1E_SET_DISABLE_TEMPMV,
Jingning Hana446f552018-02-22 15:42:12 -0800758 AV1E_SET_ENABLE_DF,
Cheng Chenfecd9a72018-03-08 15:23:51 -0800759 AV1E_SET_ENABLE_ORDER_HINT,
Cheng Chene0c918a2018-02-22 19:38:31 -0800760 AV1E_SET_ENABLE_JNT_COMP,
Yaowu Xuf883b422016-08-30 14:01:10 -0700761 0 };
Hui Su1cb1c002018-02-05 18:21:20 -0800762#endif // CONFIG_AV1_ENCODER
hui su82331e02015-08-16 18:21:56 -0700763
John Koleszar0ea50ce2010-05-18 11:58:33 -0400764static const arg_def_t *no_args[] = { NULL };
765
James Zernfd8c78c2017-11-27 16:37:33 -0800766void show_help(FILE *fout, int shorthelp) {
767 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename \n",
John Koleszarc6b90392012-07-13 15:21:29 -0700768 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400769
James Zernfd8c78c2017-11-27 16:37:33 -0800770 if (shorthelp) {
771 fprintf(fout, "Use --help to see the full list of options.\n");
772 return;
773 }
774
775 fprintf(fout, "\nOptions:\n");
776 arg_show_usage(fout, main_args);
777 fprintf(fout, "\nEncoder Global Options:\n");
778 arg_show_usage(fout, global_args);
779 fprintf(fout, "\nRate Control Options:\n");
780 arg_show_usage(fout, rc_args);
781 fprintf(fout, "\nTwopass Rate Control Options:\n");
782 arg_show_usage(fout, rc_twopass_args);
783 fprintf(fout, "\nKeyframe Placement Options:\n");
784 arg_show_usage(fout, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700785#if CONFIG_AV1_ENCODER
James Zernfd8c78c2017-11-27 16:37:33 -0800786 fprintf(fout, "\nAV1 Specific Options:\n");
787 arg_show_usage(fout, av1_args);
hui su82331e02015-08-16 18:21:56 -0700788#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800789 fprintf(fout,
clang-format6c4d83e2016-08-08 19:03:30 -0700790 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700791 " The desired precision of timestamps in the output, expressed\n"
792 " in fractional seconds. Default is 1/1000.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800793 fprintf(fout, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400794
James Zernfd8c78c2017-11-27 16:37:33 -0800795 const int num_encoder = get_aom_encoder_count();
796 for (int i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700797 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700798 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
James Zernfd8c78c2017-11-27 16:37:33 -0800799 fprintf(fout, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700800 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800801 }
James Zernfd8c78c2017-11-27 16:37:33 -0800802 fprintf(fout, "\n ");
803 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
804}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400805
James Zernfd8c78c2017-11-27 16:37:33 -0800806void usage_exit(void) {
807 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700808 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400809}
810
Yaowu Xuf883b422016-08-30 14:01:10 -0700811#if CONFIG_AV1_ENCODER
812#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800813#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800814
James Zerndba73762014-05-10 17:44:12 -0700815#if !CONFIG_WEBM_IO
816typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700817struct WebmOutputContext {
818 int debug;
819};
James Zerndba73762014-05-10 17:44:12 -0700820#endif
821
John Koleszar9e50ed72012-02-15 12:39:38 -0800822/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800823struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700824 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700825 const char *out_fn;
826 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700827#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700828 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700829#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700830 stereo_format_t stereo_fmt;
831 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
832 int arg_ctrl_cnt;
833 int write_webm;
Cyril Concolato6c788832017-10-30 16:30:35 -0700834#if CONFIG_OBU_NO_IVF
835 int write_ivf;
836#endif
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700837 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700838 int use_16bit_internal;
John Koleszar9e50ed72012-02-15 12:39:38 -0800839};
840
John Koleszar6ad3b742012-11-06 12:02:42 -0800841struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700842 int index;
843 struct stream_state *next;
844 struct stream_config config;
845 FILE *file;
846 struct rate_hist *rate_hist;
847 struct WebmOutputContext webm_ctx;
848 uint64_t psnr_sse_total;
849 uint64_t psnr_samples_total;
850 double psnr_totals[4];
851 int psnr_count;
852 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700853 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700854 unsigned int frames_out;
855 uint64_t cx_time;
856 size_t nbytes;
857 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700858#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700859 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700860#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700861 struct aom_image *img;
862 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700863 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800864};
865
clang-format6c4d83e2016-08-08 19:03:30 -0700866static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700867 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800868 if (rat->den < 0) {
869 rat->num *= -1;
870 rat->den *= -1;
871 }
John Koleszar1b27e932012-04-25 17:08:56 -0700872
clang-format6c4d83e2016-08-08 19:03:30 -0700873 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700874
clang-format6c4d83e2016-08-08 19:03:30 -0700875 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700876}
877
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100878static void parse_global_config(struct AvxEncoderConfig *global, int *argc,
879 char ***argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700880 char **argi, **argj;
881 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700882 const int num_encoder = get_aom_encoder_count();
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100883 char **argv_local = (char **)*argv;
884#if CONFIG_FILEOPTIONS
885 int argc_local = *argc;
886#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700887 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800888
John Koleszar6ad3b742012-11-06 12:02:42 -0800889 /* Initialize default parameters */
890 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700891 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700892 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700893 global->color_type = I420;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400894
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100895#if CONFIG_FILEOPTIONS
896 const char *cfg = NULL;
897 int cfg_included = 0;
898#endif
899 for (argi = argj = argv_local; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -0700900 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400901
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100902#if CONFIG_FILEOPTIONS
903 if (arg_match(&arg, &use_cfg, argi)) {
904 if (cfg_included) continue;
905 cfg = arg.val;
906
907 arg_cfg(&argc_local, &argv_local, cfg);
908
909 *argj = *argi = *argv_local;
910 argj = argi = argv_local;
911 *argv = argv_local;
912 cfg_included = 1;
913 continue;
914 }
915#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800916 if (arg_match(&arg, &help, argi)) {
917 show_help(stdout, 0);
918 exit(EXIT_SUCCESS);
919 } else if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700920 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800921 if (!global->codec)
922 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700923 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800924 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400925
John Koleszar6ad3b742012-11-06 12:02:42 -0800926 if (global->passes < 1 || global->passes > 2)
927 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700928 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800929 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400930
John Koleszar6ad3b742012-11-06 12:02:42 -0800931 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700932 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800933 } else if (arg_match(&arg, &usage, argi))
934 global->usage = arg_parse_uint(&arg);
Debargha Mukherjee67adf422018-01-24 16:08:23 -0800935 else if (arg_match(&arg, &good_dl, argi))
936 warn("Deprecated --good option! Ignoring\n");
John Koleszar6ad3b742012-11-06 12:02:42 -0800937 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700938 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800939 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700940 global->color_type = I420;
941 else if (arg_match(&arg, &use_i422, argi))
942 global->color_type = I422;
943 else if (arg_match(&arg, &use_i444, argi))
944 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700945 else if (arg_match(&arg, &use_i440, argi))
946 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800947 else if (arg_match(&arg, &quietarg, argi))
948 global->quiet = 1;
949 else if (arg_match(&arg, &verbosearg, argi))
950 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700951 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800952 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700953 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800954 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700955 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800956 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700957 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800958 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700959 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800960 global->framerate = arg_parse_rational(&arg);
961 validate_positive_rational(arg.name, &global->framerate);
962 global->have_framerate = 1;
963 } else if (arg_match(&arg, &out_part, argi))
964 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700965 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800966 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700967 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800968 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700969 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800970 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800971 else if (arg_match(&arg, &disable_warnings, argi))
972 global->disable_warnings = 1;
973 else if (arg_match(&arg, &disable_warning_prompt, argi))
974 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800975 else
John Koleszarc6b90392012-07-13 15:21:29 -0700976 argj++;
977 }
978
John Koleszar6ad3b742012-11-06 12:02:42 -0800979 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700980 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800981 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700982 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
983 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800984 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800985 }
John Koleszarc6b90392012-07-13 15:21:29 -0700986 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800987 /* Validate global config */
988 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700989#if CONFIG_AV1_ENCODER
990 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800991 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700992 if (global->codec != NULL && global->codec->name != NULL)
Thomas Daede80826142017-03-20 15:44:24 -0700993 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800994#else
995 global->passes = 1;
996#endif
997 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800998}
999
Yaowu Xuf883b422016-08-30 14:01:10 -07001000static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001001 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -07001002 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
1003 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -08001004
clang-format6c4d83e2016-08-08 19:03:30 -07001005 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -08001006
John Koleszar25b6e9f2013-02-12 21:17:56 -08001007 if (!fseeko(input->file, 0, SEEK_END)) {
1008 /* Input file is seekable. Figure out how long it is, so we can get
1009 * progress info.
1010 */
1011 input->length = ftello(input->file);
1012 rewind(input->file);
1013 }
1014
Frank Galligan09acd262015-06-01 10:20:58 -07001015 /* Default to 1:1 pixel aspect ratio. */
1016 input->pixel_aspect_ratio.numerator = 1;
1017 input->pixel_aspect_ratio.denominator = 1;
1018
John Koleszar6ad3b742012-11-06 12:02:42 -08001019 /* For RAW input sources, these bytes will applied on the first frame
1020 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -07001021 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001022 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1023 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001024
clang-format6c4d83e2016-08-08 19:03:30 -07001025 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -07001026 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
1027 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001028 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001029 input->width = input->y4m.pic_w;
1030 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -07001031 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1032 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001033 input->framerate.numerator = input->y4m.fps_n;
1034 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -07001035 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001036 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -08001037 } else
1038 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -08001039 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -08001040 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -08001041 } else {
1042 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001043 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001044}
1045
Yaowu Xuf883b422016-08-30 14:01:10 -07001046static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001047 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -07001048 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001049}
1050
Yaowu Xuf883b422016-08-30 14:01:10 -07001051static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -08001052 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001053 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001054
John Koleszar6ad3b742012-11-06 12:02:42 -08001055 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001056 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001057 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001058 }
1059
John Koleszar6ad3b742012-11-06 12:02:42 -08001060 if (prev) {
1061 memcpy(stream, prev, sizeof(*stream));
1062 stream->index++;
1063 prev->next = stream;
1064 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001065 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001066
John Koleszar6ad3b742012-11-06 12:02:42 -08001067 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -07001068 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -07001069 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -07001070 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001071
John Koleszar6ad3b742012-11-06 12:02:42 -08001072 /* Change the default timebase to a high enough value so that the
1073 * encoder will always create strictly increasing timestamps.
1074 */
1075 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001076
John Koleszar6ad3b742012-11-06 12:02:42 -08001077 /* Never use the library's default resolution, require it be parsed
1078 * from the file or set on the command line.
1079 */
1080 stream->config.cfg.g_w = 0;
1081 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001082
John Koleszar6ad3b742012-11-06 12:02:42 -08001083 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -08001084 stream->config.write_webm = 1;
Cyril Concolato6c788832017-10-30 16:30:35 -07001085#if CONFIG_OBU_NO_IVF
1086 stream->config.write_ivf = 0;
1087#endif
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001088#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -07001089 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001090 stream->webm_ctx.last_pts_ns = -1;
1091 stream->webm_ctx.writer = NULL;
1092 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001093#endif
Johann87c40b32012-03-01 16:12:53 -08001094
John Koleszar6ad3b742012-11-06 12:02:42 -08001095 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001096 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -08001097 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001098
John Koleszar6ad3b742012-11-06 12:02:42 -08001099 /* Output files must be specified for each stream */
1100 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001101
John Koleszar6ad3b742012-11-06 12:02:42 -08001102 stream->next = NULL;
1103 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001104}
1105
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +00001106static void set_config_arg_ctrls(struct stream_config *config, int key,
1107 const struct arg *arg) {
1108 int j;
1109 /* Point either to the next free element or the first instance of this
1110 * control.
1111 */
1112 for (j = 0; j < config->arg_ctrl_cnt; j++)
1113 if (config->arg_ctrls[j][0] == key) break;
1114
1115 /* Update/insert */
1116 assert(j < (int)ARG_CTRL_CNT_MAX);
1117 config->arg_ctrls[j][0] = key;
1118 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
1119 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1120}
1121
Yaowu Xuf883b422016-08-30 14:01:10 -07001122static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001123 struct stream_state *stream, char **argv) {
1124 char **argi, **argj;
1125 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001126 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001127 static const int *ctrl_args_map = NULL;
1128 struct stream_config *config = &stream->config;
1129 int eos_mark_found = 0;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001130 int webm_forced = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001131
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001132 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001133 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001134#if CONFIG_AV1_ENCODER
1135 } else if (strcmp(global->codec->name, "av1") == 0) {
1136 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1137 // Consider to expand this set for AV1 encoder control.
1138 ctrl_args = av1_args;
1139 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001140#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001141 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001142
John Koleszarc6b90392012-07-13 15:21:29 -07001143 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001144 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001145
John Koleszar6ad3b742012-11-06 12:02:42 -08001146 /* Once we've found an end-of-stream marker (--) we want to continue
1147 * shifting arguments but not consuming them.
1148 */
1149 if (eos_mark_found) {
1150 argj++;
1151 continue;
1152 } else if (!strcmp(*argj, "--")) {
1153 eos_mark_found = 1;
1154 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001155 }
1156
Adrian Granged2401802015-02-13 14:51:32 -08001157 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001158 config->out_fn = arg.val;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001159 if (!webm_forced) {
1160 const size_t out_fn_len = strlen(config->out_fn);
1161 if (out_fn_len >= 4 &&
1162 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
1163 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001164#if CONFIG_OBU_NO_IVF
1165 config->write_ivf = 1;
1166#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001167 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001168#if CONFIG_OBU_NO_IVF
1169 else if (out_fn_len >= 4 &&
1170 !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
1171 config->write_webm = 0;
1172 config->write_ivf = 0;
1173 }
1174#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001175 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001176 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001177 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001178#if CONFIG_FP_MB_STATS
1179 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1180 config->fpmb_stats_fn = arg.val;
1181#endif
Johannb50e5182015-01-30 15:05:14 -08001182 } else if (arg_match(&arg, &use_webm, argi)) {
1183#if CONFIG_WEBM_IO
1184 config->write_webm = 1;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001185 webm_forced = 1;
Johannb50e5182015-01-30 15:05:14 -08001186#else
1187 die("Error: --webm specified but webm is disabled.");
1188#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001189 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001190 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001191#if CONFIG_OBU_NO_IVF
1192 config->write_ivf = 1;
1193#endif
1194#if CONFIG_OBU_NO_IVF
1195 } else if (arg_match(&arg, &use_obu, argi)) {
1196 config->write_webm = 0;
1197 config->write_ivf = 0;
1198#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001199 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001200 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001201 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001202 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001203 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001204 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001205 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001206 config->cfg.g_h = arg_parse_uint(&arg);
Imdad Sardharwalla102c8652018-02-23 16:35:13 +00001207 } else if (arg_match(&arg, &forced_max_frame_width, argi)) {
1208 config->cfg.g_forced_max_frame_width = arg_parse_uint(&arg);
1209 } else if (arg_match(&arg, &forced_max_frame_height, argi)) {
1210 config->cfg.g_forced_max_frame_height = arg_parse_uint(&arg);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001211 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1212 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1213 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1214 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
James Zerndba73762014-05-10 17:44:12 -07001215#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001216 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001217 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001218#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001219 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001220 config->cfg.g_timebase = arg_parse_rational(&arg);
1221 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001222 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001223 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001224 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001225 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001226 } else if (arg_match(&arg, &large_scale_tile, argi)) {
1227 config->cfg.large_scale_tile = arg_parse_uint(&arg);
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001228#if CONFIG_MONO_VIDEO
1229 } else if (arg_match(&arg, &monochrome, argi)) {
1230 config->cfg.monochrome = 1;
1231#endif // CONFIG_MONO_VIDEO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001232 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001233 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001234 } else if (arg_match(&arg, &resize_mode, argi)) {
1235 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001236 } else if (arg_match(&arg, &resize_denominator, argi)) {
1237 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1238 } else if (arg_match(&arg, &resize_kf_denominator, argi)) {
1239 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001240 } else if (arg_match(&arg, &superres_mode, argi)) {
1241 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001242 } else if (arg_match(&arg, &superres_denominator, argi)) {
1243 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1244 } else if (arg_match(&arg, &superres_kf_denominator, argi)) {
1245 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001246 } else if (arg_match(&arg, &superres_qthresh, argi)) {
1247 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1248 } else if (arg_match(&arg, &superres_kf_qthresh, argi)) {
1249 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001250 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001251 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001252 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001253 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001254 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001255 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001256 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001257 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001258 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001259 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001260 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001261 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001262 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001263 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001264 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001265 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001266 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001267 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001268 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001269 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001270 if (global->passes < 2)
1271 warn("option %s ignored in one-pass mode.\n", arg.name);
1272 } else if (arg_match(&arg, &minsection_pct, argi)) {
1273 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1274
1275 if (global->passes < 2)
1276 warn("option %s ignored in one-pass mode.\n", arg.name);
1277 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1278 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1279
1280 if (global->passes < 2)
1281 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001282 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001283 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001284 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001285 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001286 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001287 config->cfg.kf_mode = AOM_KF_DISABLED;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001288#if CONFIG_MAX_TILE
1289 } else if (arg_match(&arg, &tile_width, argi)) {
1290 config->cfg.tile_width_count =
1291 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1292 } else if (arg_match(&arg, &tile_height, argi)) {
1293 config->cfg.tile_height_count =
1294 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1295#endif
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01001296#if CONFIG_FILEOPTIONS
1297 } else if (arg_match(&arg, &ext_partition, argi)) {
1298 config->cfg.cfg.ext_partition = !!arg_parse_uint(&arg) > 0;
1299#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001300 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001301 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001302 for (i = 0; ctrl_args[i]; i++) {
1303 if (arg_match(&arg, ctrl_args[i], argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001304 match = 1;
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +00001305 if (ctrl_args_map) {
1306 set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001307 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001308 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001309 }
clang-format6c4d83e2016-08-08 19:03:30 -07001310 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001311 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001312 }
Sebastien Alaiwan14af5b92017-05-12 14:27:30 +02001313 config->use_16bit_internal =
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +02001314 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
John Koleszar6ad3b742012-11-06 12:02:42 -08001315 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001316}
1317
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001318#define FOREACH_STREAM(iterator, list) \
1319 for (struct stream_state *iterator = list; iterator; \
1320 iterator = iterator->next)
John Koleszar9e50ed72012-02-15 12:39:38 -08001321
Alex Conversed66bd222014-02-21 10:52:09 -08001322static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001323 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001324 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001325 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001326
John Koleszar6ad3b742012-11-06 12:02:42 -08001327 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001328 fatal(
1329 "Stream %d: Specify stream dimensions with --width (-w) "
1330 " and --height (-h)",
1331 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001332
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001333 // Check that the codec bit depth is greater than the input bit depth.
1334 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001335 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001336 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1337 stream->index, (int)stream->config.cfg.g_bit_depth,
1338 stream->config.cfg.g_input_bit_depth);
1339 }
1340
John Koleszar6ad3b742012-11-06 12:02:42 -08001341 for (streami = stream; streami; streami = streami->next) {
1342 /* All streams require output files */
1343 if (!streami->config.out_fn)
1344 fatal("Stream %d: Output file is required (specify with -o)",
1345 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001346
John Koleszar6ad3b742012-11-06 12:02:42 -08001347 /* Check for two streams outputting to the same file */
1348 if (streami != stream) {
1349 const char *a = stream->config.out_fn;
1350 const char *b = streami->config.out_fn;
1351 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1352 fatal("Stream %d: duplicate output file (from stream %d)",
1353 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001354 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001355
1356 /* Check for two streams sharing a stats file. */
1357 if (streami != stream) {
1358 const char *a = stream->config.stats_fn;
1359 const char *b = streami->config.stats_fn;
1360 if (a && b && !strcmp(a, b))
1361 fatal("Stream %d: duplicate stats file (from stream %d)",
1362 streami->index, stream->index);
1363 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001364
1365#if CONFIG_FP_MB_STATS
1366 /* Check for two streams sharing a mb stats file. */
1367 if (streami != stream) {
1368 const char *a = stream->config.fpmb_stats_fn;
1369 const char *b = streami->config.fpmb_stats_fn;
1370 if (a && b && !strcmp(a, b))
1371 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1372 streami->index, stream->index);
1373 }
1374#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001375 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001376}
1377
clang-format6c4d83e2016-08-08 19:03:30 -07001378static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001379 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001380 if (!stream->config.cfg.g_w) {
1381 if (!stream->config.cfg.g_h)
1382 stream->config.cfg.g_w = w;
1383 else
1384 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1385 }
1386 if (!stream->config.cfg.g_h) {
1387 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1388 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001389}
1390
clang-format6c4d83e2016-08-08 19:03:30 -07001391static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001392 switch (t) {
1393 case FILE_TYPE_RAW: return "RAW";
1394 case FILE_TYPE_Y4M: return "Y4M";
1395 default: return "Other";
1396 }
1397}
1398
Yaowu Xuf883b422016-08-30 14:01:10 -07001399static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001400 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001401 case AOM_IMG_FMT_I420: return "I420";
1402 case AOM_IMG_FMT_I422: return "I422";
1403 case AOM_IMG_FMT_I444: return "I444";
1404 case AOM_IMG_FMT_I440: return "I440";
1405 case AOM_IMG_FMT_YV12: return "YV12";
1406 case AOM_IMG_FMT_I42016: return "I42016";
1407 case AOM_IMG_FMT_I42216: return "I42216";
1408 case AOM_IMG_FMT_I44416: return "I44416";
1409 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001410 default: return "Other";
1411 }
1412}
Ralph Giles061a16d2012-01-05 15:05:05 -06001413
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001414static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001415 struct AvxEncoderConfig *global,
1416 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001417#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001418 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001419
John Koleszar6ad3b742012-11-06 12:02:42 -08001420 if (stream->index == 0) {
1421 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001422 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001423 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001424 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001425 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001426 }
1427 if (stream->next || stream->index)
1428 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1429 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
Sebastien Alaiwan27511922017-06-05 15:25:46 +02001430 fprintf(stderr, "Coding path: %s\n",
1431 stream->config.use_16bit_internal ? "HBD" : "LBD");
John Koleszar6ad3b742012-11-06 12:02:42 -08001432 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001433
John Koleszar6ad3b742012-11-06 12:02:42 -08001434 SHOW(g_usage);
1435 SHOW(g_threads);
1436 SHOW(g_profile);
1437 SHOW(g_w);
1438 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001439 SHOW(g_bit_depth);
1440 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001441 SHOW(g_timebase.num);
1442 SHOW(g_timebase.den);
1443 SHOW(g_error_resilient);
1444 SHOW(g_pass);
1445 SHOW(g_lag_in_frames);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001446 SHOW(large_scale_tile);
John Koleszar6ad3b742012-11-06 12:02:42 -08001447 SHOW(rc_dropframe_thresh);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001448 SHOW(rc_resize_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001449 SHOW(rc_resize_denominator);
1450 SHOW(rc_resize_kf_denominator);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001451 SHOW(rc_superres_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001452 SHOW(rc_superres_denominator);
1453 SHOW(rc_superres_kf_denominator);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001454 SHOW(rc_superres_qthresh);
1455 SHOW(rc_superres_kf_qthresh);
John Koleszar6ad3b742012-11-06 12:02:42 -08001456 SHOW(rc_end_usage);
1457 SHOW(rc_target_bitrate);
1458 SHOW(rc_min_quantizer);
1459 SHOW(rc_max_quantizer);
1460 SHOW(rc_undershoot_pct);
1461 SHOW(rc_overshoot_pct);
1462 SHOW(rc_buf_sz);
1463 SHOW(rc_buf_initial_sz);
1464 SHOW(rc_buf_optimal_sz);
1465 SHOW(rc_2pass_vbr_bias_pct);
1466 SHOW(rc_2pass_vbr_minsection_pct);
1467 SHOW(rc_2pass_vbr_maxsection_pct);
1468 SHOW(kf_mode);
1469 SHOW(kf_min_dist);
1470 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001471}
1472
John Koleszar9e50ed72012-02-15 12:39:38 -08001473static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001474 struct AvxEncoderConfig *global,
1475 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001476 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001477 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001478
Yaowu Xuf883b422016-08-30 14:01:10 -07001479 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001480
John Koleszar6ad3b742012-11-06 12:02:42 -08001481 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001482
clang-format6c4d83e2016-08-08 19:03:30 -07001483 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001484
John Koleszar6ad3b742012-11-06 12:02:42 -08001485 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1486 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001487
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001488#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001489 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001490 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001491 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1492 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001493 }
James Zernc8e5a772016-02-11 18:53:50 -08001494#else
1495 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001496#endif
1497
Cyril Concolato6c788832017-10-30 16:30:35 -07001498 if (!stream->config.write_webm
1499#if CONFIG_OBU_NO_IVF
1500 && stream->config.write_ivf
1501#endif
Johann3c30fb42018-02-08 14:33:20 -08001502 ) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001503 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1504 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001505}
1506
John Koleszar9e50ed72012-02-15 12:39:38 -08001507static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001508 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001509 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001510
Yaowu Xuf883b422016-08-30 14:01:10 -07001511 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001512
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001513#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001514 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001515 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001516 }
1517#endif
1518
Cyril Concolato6c788832017-10-30 16:30:35 -07001519 if (!stream->config.write_webm
1520#if CONFIG_OBU_NO_IVF
1521 && stream->config.write_ivf
1522#endif
Johann3c30fb42018-02-08 14:33:20 -08001523 ) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001524 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001525 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001526 stream->frames_out);
1527 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001528
John Koleszar6ad3b742012-11-06 12:02:42 -08001529 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001530}
1531
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001532static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001533 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001534 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001535 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001536 fatal("Failed to open statistics store");
1537 } else {
1538 if (!stats_open_mem(&stream->stats, pass))
1539 fatal("Failed to open statistics store");
1540 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001541
Pengchong Jinf349b072014-07-14 09:13:38 -07001542#if CONFIG_FP_MB_STATS
1543 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001544 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1545 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001546 fatal("Failed to open mb statistics store");
1547 } else {
1548 if (!stats_open_mem(&stream->fpmb_stats, pass))
1549 fatal("Failed to open mb statistics store");
1550 }
1551#endif
1552
John Koleszar6ad3b742012-11-06 12:02:42 -08001553 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001554 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1555 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001556 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001557 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001558#if CONFIG_FP_MB_STATS
1559 stream->config.cfg.rc_firstpass_mb_stats_in =
1560 stats_get(&stream->fpmb_stats);
1561#endif
1562 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001563
John Koleszar6ad3b742012-11-06 12:02:42 -08001564 stream->cx_time = 0;
1565 stream->nbytes = 0;
1566 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001567}
1568
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001569static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001570 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001571 int i;
1572 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001573
Yaowu Xuf883b422016-08-30 14:01:10 -07001574 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1575 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001576 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001577
John Koleszar6ad3b742012-11-06 12:02:42 -08001578 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001579 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001580 &stream->config.cfg, flags);
1581 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001582
Yaowu Xuf883b422016-08-30 14:01:10 -07001583 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001584 * we're being clever to store the control IDs in an array. Real
1585 * applications will want to make use of the enumerations directly
1586 */
1587 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1588 int ctrl = stream->config.arg_ctrls[i][0];
1589 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001590 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001591 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001592
John Koleszar6ad3b742012-11-06 12:02:42 -08001593 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1594 }
1595
Tom Fineganba02c242017-05-16 15:01:54 -07001596#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001597 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001598 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
Debargha Mukherjee2ccf4b92018-02-27 17:30:46 -08001599 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, { 1 } };
Yaowu Xuf883b422016-08-30 14:01:10 -07001600 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001601
Yaowu Xuf883b422016-08-30 14:01:10 -07001602 if (strcmp(global->codec->name, "av1") == 0) {
Yunqing Wang8ae64a92018-01-12 12:26:44 -08001603 aom_codec_control(&stream->decoder, AV1_SET_TILE_MODE,
1604 stream->config.cfg.large_scale_tile);
1605 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_mode");
1606
Yaowu Xuf883b422016-08-30 14:01:10 -07001607 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001608 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1609
Yaowu Xuf883b422016-08-30 14:01:10 -07001610 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001611 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1612 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001613 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001614#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001615}
1616
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001617static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001618 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001619 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001620 aom_codec_pts_t frame_start, next_frame_start;
1621 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1622 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001623
clang-format6c4d83e2016-08-08 19:03:30 -07001624 frame_start =
1625 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1626 cfg->g_timebase.num / global->framerate.num;
1627 next_frame_start =
1628 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1629 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001630
Yaowu Xud3e7c682017-12-21 14:08:25 -08001631 /* Scale if necessary */
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001632 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001633 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001634 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001635 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001636 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1637 exit(EXIT_FAILURE);
1638 }
1639#if CONFIG_LIBYUV
1640 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001641 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001642 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001643 }
clang-format6c4d83e2016-08-08 19:03:30 -07001644 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001645 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1646 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1647 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1648 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1649 stream->img->stride[AOM_PLANE_Y] / 2,
1650 (uint16 *)stream->img->planes[AOM_PLANE_U],
1651 stream->img->stride[AOM_PLANE_U] / 2,
1652 (uint16 *)stream->img->planes[AOM_PLANE_V],
1653 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001654 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001655 img = stream->img;
1656#else
clang-format6c4d83e2016-08-08 19:03:30 -07001657 stream->encoder.err = 1;
1658 ctx_exit_on_error(&stream->encoder,
1659 "Stream %d: Failed to encode frame.\n"
Johanne07a6752018-01-10 12:47:44 -08001660 "libyuv is required for scaling but is currently "
1661 "disabled.\n"
1662 "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1663 "cmake.\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001664 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001665#endif
1666 }
1667 }
John Koleszar34882b92012-03-01 12:50:40 -08001668 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001669 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001670 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1671 exit(EXIT_FAILURE);
1672 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001673#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001674 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001675 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001676 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001677 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001678 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1679 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1680 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1681 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1682 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1683 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001684 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001685 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001686#else
1687 stream->encoder.err = 1;
1688 ctx_exit_on_error(&stream->encoder,
1689 "Stream %d: Failed to encode frame.\n"
1690 "Scaling disabled in this configuration. \n"
1691 "To enable, configure with --enable-libyuv\n",
1692 stream->index);
1693#endif
John Koleszar34882b92012-03-01 12:50:40 -08001694 }
1695
Yaowu Xuf883b422016-08-30 14:01:10 -07001696 aom_usec_timer_start(&timer);
1697 aom_codec_encode(&stream->encoder, img, frame_start,
Sean DuBois47cc2552018-01-23 07:44:16 +00001698 (uint32_t)(next_frame_start - frame_start), 0);
Yaowu Xuf883b422016-08-30 14:01:10 -07001699 aom_usec_timer_mark(&timer);
1700 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001701 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1702 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001703}
1704
John Koleszar6ad3b742012-11-06 12:02:42 -08001705static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001706 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001707 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001708
Yaowu Xuf883b422016-08-30 14:01:10 -07001709 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001710 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1711 stream->counts[q]++;
1712 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001713}
1714
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001715static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001716 struct AvxEncoderConfig *global, int *got_data) {
1717 const aom_codec_cx_pkt_t *pkt;
1718 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1719 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001720
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001721 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001722 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001723 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001724 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001725
John Koleszar6ad3b742012-11-06 12:02:42 -08001726 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001727 case AOM_CODEC_CX_FRAME_PKT:
1728 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001729 stream->frames_out++;
1730 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001731 if (!global->quiet)
1732 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001733
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001734 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001735#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001736 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001737 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001738 }
1739#endif
1740 if (!stream->config.write_webm) {
Cyril Concolato6c788832017-10-30 16:30:35 -07001741#if CONFIG_OBU_NO_IVF
1742 if (stream->config.write_ivf) {
1743#endif
1744 if (pkt->data.frame.partition_id <= 0) {
1745 ivf_header_pos = ftello(stream->file);
1746 fsize = pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001747
Cyril Concolato6c788832017-10-30 16:30:35 -07001748 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1749 } else {
1750 fsize += pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001751
Cyril Concolato6c788832017-10-30 16:30:35 -07001752 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1753 const FileOffset currpos = ftello(stream->file);
1754 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1755 ivf_write_frame_size(stream->file, fsize);
1756 fseeko(stream->file, currpos, SEEK_SET);
1757 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001758 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001759#if CONFIG_OBU_NO_IVF
John Koleszar6ad3b742012-11-06 12:02:42 -08001760 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001761#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001762
clang-format6c4d83e2016-08-08 19:03:30 -07001763 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1764 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001765 }
1766 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001767
John Koleszar9e50ed72012-02-15 12:39:38 -08001768 *got_data = 1;
Tom Fineganba02c242017-05-16 15:01:54 -07001769#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001770 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001771 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Sean DuBois47cc2552018-01-23 07:44:16 +00001772 (unsigned int)pkt->data.frame.sz, NULL);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001773 if (stream->decoder.err) {
1774 warn_or_exit_on_error(&stream->decoder,
1775 global->test_decode == TEST_DECODE_FATAL,
1776 "Failed to decode frame %d in stream %d",
1777 stream->frames_out + 1, stream->index);
1778 stream->mismatch_seen = stream->frames_out + 1;
1779 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001780 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001781#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001782 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001783 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001784 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001785 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001786 pkt->data.twopass_stats.sz);
1787 stream->nbytes += pkt->data.raw.sz;
1788 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001789#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001790 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001791 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001792 pkt->data.firstpass_mb_stats.sz);
1793 stream->nbytes += pkt->data.raw.sz;
1794 break;
1795#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001796 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001797
1798 if (global->show_psnr) {
1799 int i;
1800
1801 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1802 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1803 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001804 if (!global->quiet)
1805 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001806 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1807 }
1808 stream->psnr_count++;
1809 }
1810
1811 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001812 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001813 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001814 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001815}
1816
clang-format6c4d83e2016-08-08 19:03:30 -07001817static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001818 int i;
1819 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001820
clang-format6c4d83e2016-08-08 19:03:30 -07001821 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001822
John Koleszar6ad3b742012-11-06 12:02:42 -08001823 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001824 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001825 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001826 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001827
John Koleszar6ad3b742012-11-06 12:02:42 -08001828 for (i = 0; i < 4; i++) {
1829 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1830 }
1831 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001832}
1833
John Koleszar25b6e9f2013-02-12 21:17:56 -08001834static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001835 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001836}
1837
clang-format6c4d83e2016-08-08 19:03:30 -07001838static void test_decode(struct stream_state *stream,
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001839 enum TestDecodeFatality fatal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001840 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001841
clang-format6c4d83e2016-08-08 19:03:30 -07001842 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001843
John Koleszarb3c350a2013-03-13 12:15:43 -07001844 /* Get the internal reference frame */
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001845 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1846 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001847
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001848 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1849 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1850 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1851 aom_image_t enc_hbd_img;
1852 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1853 enc_img.d_w, enc_img.d_h, 16);
1854 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1855 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001856 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001857 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1858 aom_image_t dec_hbd_img;
1859 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1860 dec_img.d_w, dec_img.d_h, 16);
1861 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1862 dec_img = dec_hbd_img;
1863 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001864 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001865
John Koleszar6ad3b742012-11-06 12:02:42 -08001866 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001867 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001868
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001869 if (!aom_compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001870 int y[4], u[4], v[4];
Yaowu Xuf883b422016-08-30 14:01:10 -07001871 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001872 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001873 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001874 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001875 }
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001876 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001877 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001878 "Stream %d: Encode/decode mismatch on frame %d at"
1879 " Y[%d, %d] {%d/%d},"
1880 " U[%d, %d] {%d/%d},"
1881 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001882 stream->index, stream->frames_out, y[0], y[1], y[2],
1883 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 -08001884 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001885 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001886
Yaowu Xuf883b422016-08-30 14:01:10 -07001887 aom_img_free(&enc_img);
1888 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001889}
John Koleszarefd54f82012-02-13 16:52:18 -08001890
John Koleszar25b6e9f2013-02-12 21:17:56 -08001891static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001892 int64_t hours;
1893 int64_t mins;
1894 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001895
1896 if (etl >= 0) {
1897 hours = etl / 3600;
1898 etl -= hours * 3600;
1899 mins = etl / 60;
1900 etl -= mins * 60;
1901 secs = etl;
1902
clang-format6c4d83e2016-08-08 19:03:30 -07001903 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1904 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001905 } else {
1906 fprintf(stderr, "[%3s unknown] ", label);
1907 }
1908}
1909
Tom Finegan44dd3272013-11-20 17:18:28 -08001910int main(int argc, const char **argv_) {
1911 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001912 aom_image_t raw;
Yaowu Xuf883b422016-08-30 14:01:10 -07001913 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001914 int allocated_raw_shift = 0;
1915 int use_16bit_internal = 0;
1916 int input_shift = 0;
Tom Finegan44dd3272013-11-20 17:18:28 -08001917 int frame_avail, got_data;
1918
Yaowu Xuf883b422016-08-30 14:01:10 -07001919 struct AvxInputContext input;
1920 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001921 struct stream_state *streams = NULL;
1922 char **argv, **argi;
1923 uint64_t cx_time = 0;
1924 int stream_cnt = 0;
1925 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001926 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001927
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001928 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001929 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001930
John Koleszar6ad3b742012-11-06 12:02:42 -08001931 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001932 input.framerate.numerator = 30;
1933 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001934 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001935 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001936
1937 /* First parse the global configuration values, because we want to apply
1938 * other parameters on top of the default configuration provided by the
1939 * codec.
1940 */
1941 argv = argv_dup(argc - 1, argv_ + 1);
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01001942 parse_global_config(&global, &argc, &argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08001943
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01001944#if CONFIG_FILEOPTIONS
1945 if (argc < 2) usage_exit();
1946#else
James Zernfd8c78c2017-11-27 16:37:33 -08001947 if (argc < 3) usage_exit();
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01001948#endif
James Zernfd8c78c2017-11-27 16:37:33 -08001949
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001950 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001951 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1952 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1953 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1954 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1955 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001956 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001957
John Koleszar6ad3b742012-11-06 12:02:42 -08001958 {
1959 /* Now parse each stream's parameters. Using a local scope here
1960 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1961 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001962 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001963 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001964
John Koleszar6ad3b742012-11-06 12:02:42 -08001965 do {
1966 stream = new_stream(&global, stream);
1967 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001968 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001969 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001970 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001971
John Koleszarc6b90392012-07-13 15:21:29 -07001972 /* Check for unrecognized options */
1973 for (argi = argv; *argi; argi++)
1974 if (argi[0][0] == '-' && argi[0][1])
1975 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001976
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001977 FOREACH_STREAM(stream, streams) {
1978 check_encoder_config(global.disable_warning_prompt, &global,
1979 &stream->config.cfg);
1980 }
Tom Finegan249366b2013-11-25 12:05:19 -08001981
John Koleszarc6b90392012-07-13 15:21:29 -07001982 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001983 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001984
James Zernfd8c78c2017-11-27 16:37:33 -08001985 if (!input.filename) {
1986 fprintf(stderr, "No input file specified!\n");
1987 usage_exit();
1988 }
John Koleszar53291892010-10-22 14:48:21 -04001989
John Koleszar8dd82872013-05-06 11:01:35 -07001990 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001991 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001992
John Koleszar6ad3b742012-11-06 12:02:42 -08001993 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001994 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001995 int64_t estimated_time_left = -1;
1996 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001997 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001998
John Koleszar6ad3b742012-11-06 12:02:42 -08001999 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002000
John Koleszar6ad3b742012-11-06 12:02:42 -08002001 /* If the input file doesn't specify its w/h (raw files), try to get
2002 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07002003 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07002004 if (!input.width || !input.height) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002005 FOREACH_STREAM(stream, streams) {
Deb Mukherjeea349ee32014-10-13 14:27:53 -07002006 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
2007 input.width = stream->config.cfg.g_w;
2008 input.height = stream->config.cfg.g_h;
2009 break;
2010 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002011 };
Deb Mukherjeea349ee32014-10-13 14:27:53 -07002012 }
John Koleszarc6b90392012-07-13 15:21:29 -07002013
John Koleszar6ad3b742012-11-06 12:02:42 -08002014 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002015 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07002016 fatal(
2017 "Specify stream dimensions with --width (-w) "
2018 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002019
2020 /* If input file does not specify bit-depth but input-bit-depth parameter
2021 * exists, assume that to be the input bit-depth. However, if the
2022 * input-bit-depth paramter does not exist, assume the input bit-depth
2023 * to be the same as the codec bit-depth.
2024 */
2025 if (!input.bit_depth) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002026 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002027 if (stream->config.cfg.g_input_bit_depth)
2028 input.bit_depth = stream->config.cfg.g_input_bit_depth;
2029 else
2030 input.bit_depth = stream->config.cfg.g_input_bit_depth =
2031 (int)stream->config.cfg.g_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002032 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002033 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002034 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002035 FOREACH_STREAM(stream, streams) {
2036 stream->config.cfg.g_input_bit_depth = input.bit_depth;
2037 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002038 }
2039
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002040 FOREACH_STREAM(stream, streams) {
Thomas Daede8ec53b22016-09-19 13:24:51 -07002041 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2042 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2043 was selected. */
2044 switch (stream->config.cfg.g_profile) {
2045 case 0:
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002046 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2047 input.fmt == AOM_IMG_FMT_I44416)) {
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002048 stream->config.cfg.g_profile = 1;
2049 profile_updated = 1;
2050 } else if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2051 input.fmt == AOM_IMG_FMT_I42216) {
2052 stream->config.cfg.g_profile = 2;
2053 profile_updated = 1;
2054 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002055 break;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002056 case 1:
2057 if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2058 input.fmt == AOM_IMG_FMT_I42216) {
2059 stream->config.cfg.g_profile = 2;
2060 profile_updated = 1;
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002061 } else if (input.bit_depth < 12 &&
2062 (input.fmt == AOM_IMG_FMT_I420 ||
2063 input.fmt == AOM_IMG_FMT_I42016)) {
2064 stream->config.cfg.g_profile = 0;
2065 profile_updated = 1;
2066 }
2067 break;
2068 case 2:
2069 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2070 input.fmt == AOM_IMG_FMT_I44416)) {
2071 stream->config.cfg.g_profile = 1;
2072 profile_updated = 1;
2073 } else if (input.bit_depth < 12 &&
2074 (input.fmt == AOM_IMG_FMT_I420 ||
2075 input.fmt == AOM_IMG_FMT_I42016)) {
2076 stream->config.cfg.g_profile = 0;
2077 profile_updated = 1;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002078 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002079 break;
2080 default: break;
2081 }
2082 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002083 /* Automatically set the codec bit depth to match the input bit depth.
2084 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002085 if (stream->config.cfg.g_input_bit_depth >
2086 (unsigned int)stream->config.cfg.g_bit_depth) {
2087 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2088 }
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002089 if (stream->config.cfg.g_bit_depth > 10) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002090 switch (stream->config.cfg.g_profile) {
2091 case 0:
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002092 case 1:
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002093 stream->config.cfg.g_profile = 2;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002094 profile_updated = 1;
2095 break;
2096 default: break;
2097 }
2098 }
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002099 if (stream->config.cfg.g_bit_depth > 8) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002100 stream->config.use_16bit_internal = 1;
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002101 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002102 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002103 fprintf(stderr,
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002104 "Warning: automatically updating to profile %d to "
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002105 "match input format.\n",
2106 stream->config.cfg.g_profile);
2107 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002108 }
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002109
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002110 FOREACH_STREAM(stream, streams) {
2111 set_stream_dimensions(stream, input.width, input.height);
2112 }
2113 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
John Koleszar77e6b452010-11-03 13:58:40 -04002114
John Koleszar6ad3b742012-11-06 12:02:42 -08002115 /* Ensure that --passes and --pass are consistent. If --pass is set and
2116 * --passes=2, ensure --fpf was set.
2117 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002118 if (global.pass && global.passes == 2) {
2119 FOREACH_STREAM(stream, streams) {
clang-format6c4d83e2016-08-08 19:03:30 -07002120 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002121 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002122 " and --passes=2\n",
2123 stream->index, global.pass);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002124 }
2125 }
John Koleszar3245d462010-06-10 17:10:12 -04002126
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002127#if !CONFIG_WEBM_IO
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002128 FOREACH_STREAM(stream, streams) {
Ronald S. Bultje39775072015-12-16 13:35:43 -05002129 if (stream->config.write_webm) {
2130 stream->config.write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07002131#if CONFIG_OBU_NO_IVF
2132 stream->config.write_ivf = 0;
2133#endif
clang-format6c4d83e2016-08-08 19:03:30 -07002134 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002135 "aomenc was compiled without WebM container support."
Cyril Concolato6c788832017-10-30 16:30:35 -07002136#if CONFIG_OBU_NO_IVF
2137 "Producing OBU output");
2138#else
clang-format6c4d83e2016-08-08 19:03:30 -07002139 "Producing IVF output");
Cyril Concolato6c788832017-10-30 16:30:35 -07002140#endif
Ronald S. Bultje39775072015-12-16 13:35:43 -05002141 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002142 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002143#endif
2144
Debargha Mukherjee4a707a82018-03-02 11:19:43 -08002145 /* Use the frame rate from the file only if none was specified
2146 * on the command-line.
2147 */
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002148 if (!global.have_framerate) {
2149 global.framerate.num = input.framerate.numerator;
2150 global.framerate.den = input.framerate.denominator;
2151 }
2152 FOREACH_STREAM(stream, streams) {
2153 stream->config.cfg.g_timebase.den = global.framerate.num;
2154 stream->config.cfg.g_timebase.num = global.framerate.den;
2155 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002156 /* Show configuration */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002157 if (global.verbose && pass == 0) {
2158 FOREACH_STREAM(stream, streams) {
2159 show_stream_config(stream, &global, &input);
2160 }
2161 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002162
2163 if (pass == (global.pass ? global.pass - 1 : 0)) {
2164 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002165 /*The Y4M reader does its own allocation.
2166 Just initialize this here to avoid problems if we never read any
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002167 frames.*/
John Koleszarc6b90392012-07-13 15:21:29 -07002168 memset(&raw, 0, sizeof(raw));
2169 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002170 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002171
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002172 FOREACH_STREAM(stream, streams) {
2173 stream->rate_hist =
2174 init_rate_histogram(&stream->config.cfg, &global.framerate);
2175 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002176 }
2177
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002178 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2179 FOREACH_STREAM(stream, streams) {
2180 open_output_file(stream, &global, &input.pixel_aspect_ratio);
2181 }
2182 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
Yaowu Xuf883b422016-08-30 14:01:10 -07002183 if (strcmp(global.codec->name, "av1") == 0 ||
2184 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002185 // Check to see if at least one stream uses 16 bit internal.
2186 // Currently assume that the bit_depths for all streams using
2187 // highbitdepth are the same.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002188 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002189 if (stream->config.use_16bit_internal) {
2190 use_16bit_internal = 1;
2191 }
Yaowu Xu913867b2018-01-23 18:27:59 -08002192 input_shift = (int)stream->config.cfg.g_bit_depth -
2193 stream->config.cfg.g_input_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002194 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002195 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002196
John Koleszarc6b90392012-07-13 15:21:29 -07002197 frame_avail = 1;
2198 got_data = 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002199
John Koleszarc6b90392012-07-13 15:21:29 -07002200 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002201 struct aom_usec_timer timer;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002202
John Koleszar6ad3b742012-11-06 12:02:42 -08002203 if (!global.limit || frames_in < global.limit) {
2204 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002205
clang-format6c4d83e2016-08-08 19:03:30 -07002206 if (frame_avail) frames_in++;
2207 seen_frames =
2208 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002209
John Koleszar6ad3b742012-11-06 12:02:42 -08002210 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002211 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002212 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2213
John Koleszar6ad3b742012-11-06 12:02:42 -08002214 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002215 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2216 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002217 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002218 fprintf(stderr, "frame %4d ", frames_in);
2219
clang-format6c4d83e2016-08-08 19:03:30 -07002220 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002221 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002222 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002223 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002224 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002225 }
2226
hui su251e1512016-10-03 15:48:43 -07002227 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002228 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002229 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002230
John Koleszar6ad3b742012-11-06 12:02:42 -08002231 if (frames_in > global.skip_frames) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002232 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002233 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2234 assert(use_16bit_internal);
2235 // Input bit depth and stream bit depth do not match, so up
2236 // shift frame to stream bit depth
2237 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002238 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002239 input.width, input.height, 32);
2240 allocated_raw_shift = 1;
2241 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002242 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002243 frame_to_encode = &raw_shift;
2244 } else {
2245 frame_to_encode = &raw;
2246 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002247 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002248 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002249 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002250 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002251 if (stream->config.use_16bit_internal)
2252 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002253 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002254 else
2255 assert(0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002256 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002257 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002258 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002259 FOREACH_STREAM(stream, streams) {
2260 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2261 frames_in);
2262 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002263 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002264 aom_usec_timer_mark(&timer);
2265 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002266
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002267 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
John Koleszarc6b90392012-07-13 15:21:29 -07002268
John Koleszar0ea50ce2010-05-18 11:58:33 -04002269 got_data = 0;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002270 FOREACH_STREAM(stream, streams) {
2271 get_cx_data(stream, &global, &got_data);
2272 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002273
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002274 if (!got_data && input.length && streams != NULL &&
2275 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002276 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002277 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002278 int64_t remaining;
2279 int64_t rate;
2280
2281 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002282 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002283
2284 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002285 remaining = 1000 * (global.limit - global.skip_frames -
2286 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002287 } else {
James Zerne3578af2014-04-17 10:47:08 -07002288 const int64_t input_pos = ftello(input.file);
2289 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002290 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002291
2292 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002293 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002294 }
2295
clang-format6c4d83e2016-08-08 19:03:30 -07002296 average_rate =
2297 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002298 estimated_time_left = average_rate ? remaining / average_rate : -1;
2299 }
2300
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002301 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2302 FOREACH_STREAM(stream, streams) {
Imdad Sardharwallab5def022017-12-14 11:20:24 +00002303 test_decode(stream, global.test_decode);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002304 }
2305 }
John Koleszarc6b90392012-07-13 15:21:29 -07002306 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002307
John Koleszarc6b90392012-07-13 15:21:29 -07002308 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002309 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002310 }
2311
clang-format6c4d83e2016-08-08 19:03:30 -07002312 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002313
Deb Mukherjeea160d722014-09-30 21:56:33 -07002314 if (!global.quiet) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002315 FOREACH_STREAM(stream, streams) {
Johann3c30fb42018-02-08 14:33:20 -08002316 fprintf(stderr,
2317 "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2318 "b/f %7" PRId64
2319 "b/s"
2320 " %7" PRId64 " %s (%.2f fps)\033[K\n",
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002321 pass + 1, global.passes, frames_in, stream->frames_out,
2322 (int64_t)stream->nbytes,
2323 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
Johann3c30fb42018-02-08 14:33:20 -08002324 seen_frames ? (int64_t)stream->nbytes * 8 *
2325 (int64_t)global.framerate.num /
2326 global.framerate.den / seen_frames
2327 : 0,
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002328 stream->cx_time > 9999999 ? stream->cx_time / 1000
2329 : stream->cx_time,
2330 stream->cx_time > 9999999 ? "ms" : "us",
2331 usec_to_fps(stream->cx_time, seen_frames));
2332 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002333 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002334
Deb Mukherjeea160d722014-09-30 21:56:33 -07002335 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002336 if (global.codec->fourcc == AV1_FOURCC) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002337 FOREACH_STREAM(stream, streams) {
2338 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2339 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002340 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002341 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002342 }
2343 }
John Koleszarc6b90392012-07-13 15:21:29 -07002344
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002345 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002346
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002347 if (global.test_decode != TEST_DECODE_OFF) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002348 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002349 }
2350
John Koleszar6ad3b742012-11-06 12:02:42 -08002351 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002352
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002353 if (global.test_decode == TEST_DECODE_FATAL) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002354 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002355 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002356 FOREACH_STREAM(stream, streams) {
2357 close_output_file(stream, global.codec->fourcc);
2358 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002359
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002360 FOREACH_STREAM(stream, streams) {
2361 stats_close(&stream->stats, global.passes - 1);
2362 }
John Koleszarc6b90392012-07-13 15:21:29 -07002363
Pengchong Jinf349b072014-07-14 09:13:38 -07002364#if CONFIG_FP_MB_STATS
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002365 FOREACH_STREAM(stream, streams) {
2366 stats_close(&stream->fpmb_stats, global.passes - 1);
2367 }
Pengchong Jinf349b072014-07-14 09:13:38 -07002368#endif
2369
clang-format6c4d83e2016-08-08 19:03:30 -07002370 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002371 }
2372
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002373 if (global.show_q_hist_buckets) {
2374 FOREACH_STREAM(stream, streams) {
2375 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2376 }
2377 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002378
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002379 if (global.show_rate_hist_buckets) {
2380 FOREACH_STREAM(stream, streams) {
2381 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2382 global.show_rate_hist_buckets);
2383 }
2384 }
2385 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002386
Ronald S. Bultje31214972012-10-11 10:13:48 -07002387#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002388 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2389 * to match some existing utilities.
2390 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002391 if (!(global.pass == 1 && global.passes == 2)) {
2392 FOREACH_STREAM(stream, streams) {
Yaowu Xu47e784e2013-10-16 16:29:28 -07002393 FILE *f = fopen("opsnr.stt", "a");
2394 if (stream->mismatch_seen) {
2395 fprintf(f, "First mismatch occurred in frame %d\n",
2396 stream->mismatch_seen);
2397 } else {
2398 fprintf(f, "No mismatch detected in recon buffers\n");
2399 }
2400 fclose(f);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002401 }
2402 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002403#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002404
Yaowu Xuf883b422016-08-30 14:01:10 -07002405 if (allocated_raw_shift) aom_img_free(&raw_shift);
Yaowu Xuf883b422016-08-30 14:01:10 -07002406 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002407 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002408 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002409 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002410}