blob: 2bd9de6601b5d6b6afc51763817994974d097c8b [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 };
Tarek AMARAc9813852018-03-05 18:40:18 -0500386static const arg_def_t sframe_dist =
387 ARG_DEF(NULL, "sframe-dist", 1, "S-Frame interval (frames)");
388static const arg_def_t sframe_mode =
389 ARG_DEF(NULL, "sframe-mode", 1, "S-Frame insertion mode (1..2)");
clang-format6c4d83e2016-08-08 19:03:30 -0700390static const arg_def_t noise_sens =
391 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
392static const arg_def_t sharpness =
393 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
394static const arg_def_t static_thresh =
395 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
396static const arg_def_t auto_altref =
397 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
398static const arg_def_t arnr_maxframes =
399 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
400static const arg_def_t arnr_strength =
401 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500402static const struct arg_enum_list tuning_enum[] = {
Yushin Chod808bfc2017-08-10 15:54:36 -0700403 { "psnr", AOM_TUNE_PSNR },
404 { "ssim", AOM_TUNE_SSIM },
405#ifdef CONFIG_DIST_8X8
406 { "cdef-dist", AOM_TUNE_CDEF_DIST },
407 { "daala-dist", AOM_TUNE_DAALA_DIST },
408#endif
409 { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500410};
Yushin Chod808bfc2017-08-10 15:54:36 -0700411static const arg_def_t tune_metric =
412 ARG_DEF_ENUM(NULL, "tune", 1, "Distortion metric tuned with", tuning_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700413static const arg_def_t cq_level =
414 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
415static const arg_def_t max_intra_rate_pct =
416 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800417
Yaowu Xuf883b422016-08-30 14:01:10 -0700418#if CONFIG_AV1_ENCODER
419static const arg_def_t cpu_used_av1 =
Yaowu Xu39737592017-04-21 16:39:09 -0700420 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800421static const arg_def_t dev_sf_av1 =
422 ARG_DEF(NULL, "dev-sf", 1, "Dev Speed (0..255)");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700423static const arg_def_t single_tile_decoding =
424 ARG_DEF(NULL, "single-tile-decoding", 1,
425 "Single tile decoding (0: off (default), 1: on)");
clang-format6c4d83e2016-08-08 19:03:30 -0700426static const arg_def_t tile_cols =
427 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
428static const arg_def_t tile_rows =
429 ARG_DEF(NULL, "tile-rows", 1,
430 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200431static const arg_def_t tile_width =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200432 ARG_DEF(NULL, "tile-width", 1, "Tile widths (comma separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200433static const arg_def_t tile_height =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200434 ARG_DEF(NULL, "tile-height", 1, "Tile heights (command separated)");
clang-format6c4d83e2016-08-08 19:03:30 -0700435static const arg_def_t lossless =
436 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100437static const arg_def_t enable_cdef =
438 ARG_DEF(NULL, "enable-cdef", 1,
439 "Enable the constrained directional enhancement filter (0: false, "
440 "1: true (default))");
Sebastien Alaiwan1ed20242018-02-20 14:47:18 +0100441static const arg_def_t enable_restoration =
442 ARG_DEF(NULL, "enable-restoration", 1,
443 "Enable the loop restoration filter (0: false, "
444 "1: true (default))");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700445static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800446 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700447 "Enable quantisation matrices (0: false (default), 1: true)");
448static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800449 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700450static const arg_def_t qm_max = ARG_DEF(
Yaowu Xubada8232018-01-30 17:21:30 -0800451 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 15");
Yushin Chod808bfc2017-08-10 15:54:36 -0700452#if CONFIG_DIST_8X8
453static const arg_def_t enable_dist_8x8 =
454 ARG_DEF(NULL, "enable-dist-8x8", 1,
455 "Enable dist-8x8 (0: false (default), 1: true)");
456#endif // CONFIG_DIST_8X8
Yaowu Xu859a5272016-11-10 15:32:21 -0800457static const arg_def_t num_tg = ARG_DEF(
458 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000459static const arg_def_t mtu_size =
460 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800461 "MTU size for a tile group, default is 0 (no MTU targeting), "
462 "overrides maximum number of tile groups");
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800463static const struct arg_enum_list timing_info_enum[] = {
464 { "unspecified", AOM_TIMING_UNSPECIFIED },
465 { "constant", AOM_TIMING_EQUAL },
466 { NULL, 0 }
467};
468static const arg_def_t timing_info =
469 ARG_DEF_ENUM(NULL, "timing-info", 1,
470 "Signal timing info in the bitstream:", timing_info_enum);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800471#if CONFIG_FILM_GRAIN
472static const arg_def_t film_grain_test =
473 ARG_DEF(NULL, "film-grain-test", 1,
474 "Film grain test vectors (0: none (default), 1: test-1 2: test-2, "
475 "... 16: test-16)");
476#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800477static const arg_def_t disable_tempmv = ARG_DEF(
478 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
Yaowu Xud59fb482016-09-30 15:39:53 -0700479static const arg_def_t frame_parallel_decoding =
480 ARG_DEF(NULL, "frame-parallel", 1,
481 "Enable frame parallel decodability features "
482 "(0: false (default), 1: true)");
Thomas Davies3ab20b42017-09-19 10:30:53 +0100483#if !CONFIG_EXT_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100484static const arg_def_t aq_mode = ARG_DEF(
485 NULL, "aq-mode", 1,
486 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100487 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200488#else
Thomase28d92b2016-09-20 12:07:11 +0100489static const arg_def_t aq_mode = ARG_DEF(
490 NULL, "aq-mode", 1,
491 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100492 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200493#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700494#if CONFIG_EXT_DELTA_Q
495static const arg_def_t deltaq_mode = ARG_DEF(
496 NULL, "deltaq-mode", 1,
497 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
498#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700499static const arg_def_t frame_periodic_boost =
500 ARG_DEF(NULL, "frame-boost", 1,
501 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700502static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
503 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700504static const arg_def_t max_inter_rate_pct =
505 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700506static const arg_def_t min_gf_interval = ARG_DEF(
507 NULL, "min-gf-interval", 1,
508 "min gf/arf frame interval (default 0, indicating in-built behavior)");
509static const arg_def_t max_gf_interval = ARG_DEF(
510 NULL, "max-gf-interval", 1,
511 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800512
Andrey Norkin9e694632017-12-21 18:50:57 -0800513static const struct arg_enum_list color_primaries_enum[] = {
514 { "bt709", AOM_CICP_CP_BT_709 },
515 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
516 { "bt601", AOM_CICP_CP_BT_601 },
517 { "bt470m", AOM_CICP_CP_BT_470_M },
518 { "bt470bg", AOM_CICP_CP_BT_470_B_G },
519 { "smpte240", AOM_CICP_CP_SMPTE_240 },
520 { "film", AOM_CICP_CP_GENERIC_FILM },
521 { "bt2020", AOM_CICP_CP_BT_2020 },
522 { "xyz", AOM_CICP_CP_XYZ },
523 { "smpte431", AOM_CICP_CP_SMPTE_431 },
524 { "smpte432", AOM_CICP_CP_SMPTE_432 },
525 { "ebu3213", AOM_CICP_CP_EBU_3213 },
526 { NULL, 0 }
527};
528
529static const arg_def_t input_color_primaries = ARG_DEF_ENUM(
530 NULL, "color-primaries", 1,
531 "Color primaries (CICP) of input content:", color_primaries_enum);
532
533static const struct arg_enum_list transfer_characteristics_enum[] = {
534 { "unspecified", AOM_CICP_CP_UNSPECIFIED },
535 { "bt709", AOM_CICP_TC_BT_709 },
536 { "bt470m", AOM_CICP_TC_BT_470_M },
537 { "bt470bg", AOM_CICP_TC_BT_470_B_G },
538 { "bt601", AOM_CICP_TC_BT_601 },
539 { "smpte240", AOM_CICP_TC_SMPTE_240 },
540 { "lin", AOM_CICP_TC_LINEAR },
541 { "log100", AOM_CICP_TC_LOG_100 },
542 { "log100sq10", AOM_CICP_TC_LOG_100_SQRT10 },
543 { "iec61966", AOM_CICP_TC_IEC_61966 },
544 { "bt1361", AOM_CICP_TC_BT_1361 },
545 { "srgb", AOM_CICP_TC_SRGB },
546 { "bt2020-10bit", AOM_CICP_TC_BT_2020_10_BIT },
547 { "bt2020-12bit", AOM_CICP_TC_BT_2020_12_BIT },
548 { "smpte2084", AOM_CICP_TC_SMPTE_2084 },
549 { "hlg", AOM_CICP_TC_HLG },
550 { "smpte428", AOM_CICP_TC_SMPTE_428 },
551 { NULL, 0 }
552};
553
554static const arg_def_t input_transfer_characteristics =
555 ARG_DEF_ENUM(NULL, "transfer-characteristics", 1,
556 "Transfer characteristics (CICP) of input content:",
557 transfer_characteristics_enum);
558
559static const struct arg_enum_list matrix_coefficients_enum[] = {
560 { "identity", AOM_CICP_MC_IDENTITY },
561 { "bt709", AOM_CICP_MC_BT_709 },
562 { "unspecified", AOM_CICP_MC_UNSPECIFIED },
563 { "fcc73", AOM_CICP_MC_FCC },
564 { "bt470bg", AOM_CICP_MC_BT_470_B_G },
565 { "bt601", AOM_CICP_MC_BT_601 },
566 { "smpte240", AOM_CICP_CP_SMPTE_240 },
567 { "ycgco", AOM_CICP_MC_SMPTE_YCGCO },
568 { "bt2020ncl", AOM_CICP_MC_BT_2020_NCL },
569 { "bt2020cl", AOM_CICP_MC_BT_2020_CL },
570 { "smpte2085", AOM_CICP_MC_SMPTE_2085 },
571 { "chromncl", AOM_CICP_MC_CHROMAT_NCL },
572 { "chromcl", AOM_CICP_MC_CHROMAT_CL },
573 { "ictcp", AOM_CICP_MC_ICTCP },
574 { NULL, 0 }
575};
576
577static const arg_def_t input_matrix_coefficients = ARG_DEF_ENUM(
578 NULL, "matrix-coefficients", 1,
579 "Matrix coefficients (CICP) of input content:", matrix_coefficients_enum);
580
anorkin76fb1262017-03-22 15:12:12 -0700581static const struct arg_enum_list chroma_sample_position_enum[] = {
582 { "unknown", AOM_CSP_UNKNOWN },
583 { "vertical", AOM_CSP_VERTICAL },
584 { "colocated", AOM_CSP_COLOCATED },
585 { NULL, 0 }
586};
587
588static const arg_def_t input_chroma_sample_position =
589 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
590 "The chroma sample position when chroma 4:2:0 is signaled:",
591 chroma_sample_position_enum);
anorkin76fb1262017-03-22 15:12:12 -0700592
Alex Converse219f6452014-08-06 11:01:18 -0700593static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700594 { "default", AOM_CONTENT_DEFAULT },
595 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700596 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700597};
598
599static const arg_def_t tune_content = ARG_DEF_ENUM(
600 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
601
Hui Su1cb1c002018-02-05 18:21:20 -0800602#if CONFIG_CDF_UPDATE_MODE
603static const arg_def_t cdf_update_mode =
604 ARG_DEF(NULL, "cdf-update-mode", 1,
Hui Su483a8452018-02-26 12:28:48 -0800605 "CDF update mode for entropy coding "
606 "(0: no CDF update; 1: update CDF on all frames(default); "
607 "2: selectively update CDF on some frames");
Hui Su1cb1c002018-02-05 18:21:20 -0800608#endif // CONFIG_CDF_UPDATE_MODE
609
Geza Lore454989f2016-03-24 13:56:05 +0000610static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700611 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
612 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
613 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700614 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000615};
616static const arg_def_t superblock_size = ARG_DEF_ENUM(
617 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
Geza Lore454989f2016-03-24 13:56:05 +0000618
Yaowu Xuf883b422016-08-30 14:01:10 -0700619static const arg_def_t *av1_args[] = { &cpu_used_av1,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800620 &dev_sf_av1,
Yaowu Xuf883b422016-08-30 14:01:10 -0700621 &auto_altref,
622 &sharpness,
623 &static_thresh,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700624 &single_tile_decoding,
Yaowu Xuf883b422016-08-30 14:01:10 -0700625 &tile_cols,
626 &tile_rows,
627 &arnr_maxframes,
628 &arnr_strength,
Yushin Chod808bfc2017-08-10 15:54:36 -0700629 &tune_metric,
Yaowu Xuf883b422016-08-30 14:01:10 -0700630 &cq_level,
631 &max_intra_rate_pct,
632 &max_inter_rate_pct,
633 &gf_cbr_boost_pct,
634 &lossless,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100635 &enable_cdef,
Sebastien Alaiwan1ed20242018-02-20 14:47:18 +0100636 &enable_restoration,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800637 &enable_qm,
638 &qm_min,
639 &qm_max,
Yushin Chod808bfc2017-08-10 15:54:36 -0700640#if CONFIG_DIST_8X8
641 &enable_dist_8x8,
642#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700643 &frame_parallel_decoding,
644 &aq_mode,
Fangwen Fu6160df22017-04-24 09:45:51 -0700645#if CONFIG_EXT_DELTA_Q
646 &deltaq_mode,
647#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700648 &frame_periodic_boost,
649 &noise_sens,
650 &tune_content,
Hui Su1cb1c002018-02-05 18:21:20 -0800651#if CONFIG_CDF_UPDATE_MODE
652 &cdf_update_mode,
653#endif // CONFIG_CDF_UPDATE_MODE
Andrey Norkin9e694632017-12-21 18:50:57 -0800654 &input_color_primaries,
655 &input_transfer_characteristics,
656 &input_matrix_coefficients,
anorkin76fb1262017-03-22 15:12:12 -0700657 &input_chroma_sample_position,
Yaowu Xuf883b422016-08-30 14:01:10 -0700658 &min_gf_interval,
659 &max_gf_interval,
Yaowu Xuf883b422016-08-30 14:01:10 -0700660 &superblock_size,
Thomas Daviesaf6df172016-11-09 14:04:18 +0000661 &num_tg,
662 &mtu_size,
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800663 &timing_info,
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800664#if CONFIG_FILM_GRAIN
665 &film_grain_test,
666#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800667 &disable_tempmv,
Yaowu Xuf883b422016-08-30 14:01:10 -0700668 &bitdeptharg,
669 &inbitdeptharg,
Tarek AMARAc9813852018-03-05 18:40:18 -0500670 &sframe_dist,
671 &sframe_mode,
Yaowu Xuf883b422016-08-30 14:01:10 -0700672 NULL };
673static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800674 AOME_SET_DEVSF,
Yaowu Xuf883b422016-08-30 14:01:10 -0700675 AOME_SET_ENABLEAUTOALTREF,
676 AOME_SET_SHARPNESS,
677 AOME_SET_STATIC_THRESHOLD,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700678 AV1E_SET_SINGLE_TILE_DECODING,
Yaowu Xuf883b422016-08-30 14:01:10 -0700679 AV1E_SET_TILE_COLUMNS,
680 AV1E_SET_TILE_ROWS,
681 AOME_SET_ARNR_MAXFRAMES,
682 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700683 AOME_SET_TUNING,
684 AOME_SET_CQ_LEVEL,
685 AOME_SET_MAX_INTRA_BITRATE_PCT,
686 AV1E_SET_MAX_INTER_BITRATE_PCT,
687 AV1E_SET_GF_CBR_BOOST_PCT,
688 AV1E_SET_LOSSLESS,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100689 AV1E_SET_ENABLE_CDEF,
Sebastien Alaiwan1ed20242018-02-20 14:47:18 +0100690 AV1E_SET_ENABLE_RESTORATION,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800691 AV1E_SET_ENABLE_QM,
692 AV1E_SET_QM_MIN,
693 AV1E_SET_QM_MAX,
Yushin Chod808bfc2017-08-10 15:54:36 -0700694#if CONFIG_DIST_8X8
695 AV1E_SET_ENABLE_DIST_8X8,
696#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700697 AV1E_SET_FRAME_PARALLEL_DECODING,
698 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700699#if CONFIG_EXT_DELTA_Q
700 AV1E_SET_DELTAQ_MODE,
701#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700702 AV1E_SET_FRAME_PERIODIC_BOOST,
703 AV1E_SET_NOISE_SENSITIVITY,
704 AV1E_SET_TUNE_CONTENT,
Hui Su1cb1c002018-02-05 18:21:20 -0800705#if CONFIG_CDF_UPDATE_MODE
706 AV1E_SET_CDF_UPDATE_MODE,
707#endif // CONFIG_CDF_UPDATE_MODE
Andrey Norkin9e694632017-12-21 18:50:57 -0800708 AV1E_SET_COLOR_PRIMARIES,
709 AV1E_SET_TRANSFER_CHARACTERISTICS,
710 AV1E_SET_MATRIX_COEFFICIENTS,
anorkin76fb1262017-03-22 15:12:12 -0700711 AV1E_SET_CHROMA_SAMPLE_POSITION,
Yaowu Xuf883b422016-08-30 14:01:10 -0700712 AV1E_SET_MIN_GF_INTERVAL,
713 AV1E_SET_MAX_GF_INTERVAL,
Yaowu Xuf883b422016-08-30 14:01:10 -0700714 AV1E_SET_SUPERBLOCK_SIZE,
Thomas Daviesaf6df172016-11-09 14:04:18 +0000715 AV1E_SET_NUM_TG,
716 AV1E_SET_MTU,
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800717 AV1E_SET_TIMING_INFO,
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800718#if CONFIG_FILM_GRAIN
719 AV1E_SET_FILM_GRAIN_TEST_VECTOR,
720#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800721 AV1E_SET_DISABLE_TEMPMV,
Jingning Hana446f552018-02-22 15:42:12 -0800722 AV1E_SET_ENABLE_DF,
Cheng Chenfecd9a72018-03-08 15:23:51 -0800723 AV1E_SET_ENABLE_ORDER_HINT,
Cheng Chene0c918a2018-02-22 19:38:31 -0800724 AV1E_SET_ENABLE_JNT_COMP,
Urvang Joshi2c92b072018-03-19 17:23:31 -0700725 AV1E_SET_ENABLE_SUPERRES,
Yaowu Xuf883b422016-08-30 14:01:10 -0700726 0 };
Hui Su1cb1c002018-02-05 18:21:20 -0800727#endif // CONFIG_AV1_ENCODER
hui su82331e02015-08-16 18:21:56 -0700728
John Koleszar0ea50ce2010-05-18 11:58:33 -0400729static const arg_def_t *no_args[] = { NULL };
730
James Zernfd8c78c2017-11-27 16:37:33 -0800731void show_help(FILE *fout, int shorthelp) {
732 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename \n",
John Koleszarc6b90392012-07-13 15:21:29 -0700733 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400734
James Zernfd8c78c2017-11-27 16:37:33 -0800735 if (shorthelp) {
736 fprintf(fout, "Use --help to see the full list of options.\n");
737 return;
738 }
739
740 fprintf(fout, "\nOptions:\n");
741 arg_show_usage(fout, main_args);
742 fprintf(fout, "\nEncoder Global Options:\n");
743 arg_show_usage(fout, global_args);
744 fprintf(fout, "\nRate Control Options:\n");
745 arg_show_usage(fout, rc_args);
746 fprintf(fout, "\nTwopass Rate Control Options:\n");
747 arg_show_usage(fout, rc_twopass_args);
748 fprintf(fout, "\nKeyframe Placement Options:\n");
749 arg_show_usage(fout, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700750#if CONFIG_AV1_ENCODER
James Zernfd8c78c2017-11-27 16:37:33 -0800751 fprintf(fout, "\nAV1 Specific Options:\n");
752 arg_show_usage(fout, av1_args);
hui su82331e02015-08-16 18:21:56 -0700753#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800754 fprintf(fout,
clang-format6c4d83e2016-08-08 19:03:30 -0700755 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700756 " The desired precision of timestamps in the output, expressed\n"
757 " in fractional seconds. Default is 1/1000.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800758 fprintf(fout, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400759
James Zernfd8c78c2017-11-27 16:37:33 -0800760 const int num_encoder = get_aom_encoder_count();
761 for (int i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700762 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700763 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
James Zernfd8c78c2017-11-27 16:37:33 -0800764 fprintf(fout, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700765 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800766 }
James Zernfd8c78c2017-11-27 16:37:33 -0800767 fprintf(fout, "\n ");
768 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
769}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400770
James Zernfd8c78c2017-11-27 16:37:33 -0800771void usage_exit(void) {
772 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700773 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400774}
775
Yaowu Xuf883b422016-08-30 14:01:10 -0700776#if CONFIG_AV1_ENCODER
777#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800778#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800779
James Zerndba73762014-05-10 17:44:12 -0700780#if !CONFIG_WEBM_IO
781typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700782struct WebmOutputContext {
783 int debug;
784};
James Zerndba73762014-05-10 17:44:12 -0700785#endif
786
John Koleszar9e50ed72012-02-15 12:39:38 -0800787/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800788struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700790 const char *out_fn;
791 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700792#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700793 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700794#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700795 stereo_format_t stereo_fmt;
796 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
797 int arg_ctrl_cnt;
798 int write_webm;
Cyril Concolato6c788832017-10-30 16:30:35 -0700799#if CONFIG_OBU_NO_IVF
800 int write_ivf;
801#endif
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700802 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700803 int use_16bit_internal;
John Koleszar9e50ed72012-02-15 12:39:38 -0800804};
805
John Koleszar6ad3b742012-11-06 12:02:42 -0800806struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700807 int index;
808 struct stream_state *next;
809 struct stream_config config;
810 FILE *file;
811 struct rate_hist *rate_hist;
812 struct WebmOutputContext webm_ctx;
813 uint64_t psnr_sse_total;
814 uint64_t psnr_samples_total;
815 double psnr_totals[4];
816 int psnr_count;
817 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700818 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700819 unsigned int frames_out;
820 uint64_t cx_time;
821 size_t nbytes;
822 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700823#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700824 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700825#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700826 struct aom_image *img;
827 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700828 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800829};
830
clang-format6c4d83e2016-08-08 19:03:30 -0700831static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700832 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800833 if (rat->den < 0) {
834 rat->num *= -1;
835 rat->den *= -1;
836 }
John Koleszar1b27e932012-04-25 17:08:56 -0700837
clang-format6c4d83e2016-08-08 19:03:30 -0700838 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700839
clang-format6c4d83e2016-08-08 19:03:30 -0700840 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700841}
842
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100843static void parse_global_config(struct AvxEncoderConfig *global, int *argc,
844 char ***argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700845 char **argi, **argj;
846 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700847 const int num_encoder = get_aom_encoder_count();
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100848 char **argv_local = (char **)*argv;
849#if CONFIG_FILEOPTIONS
850 int argc_local = *argc;
851#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700852 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800853
John Koleszar6ad3b742012-11-06 12:02:42 -0800854 /* Initialize default parameters */
855 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700856 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700857 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700858 global->color_type = I420;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400859
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100860#if CONFIG_FILEOPTIONS
861 const char *cfg = NULL;
862 int cfg_included = 0;
863#endif
864 for (argi = argj = argv_local; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -0700865 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400866
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100867#if CONFIG_FILEOPTIONS
868 if (arg_match(&arg, &use_cfg, argi)) {
869 if (cfg_included) continue;
870 cfg = arg.val;
871
872 arg_cfg(&argc_local, &argv_local, cfg);
873
874 *argj = *argi = *argv_local;
875 argj = argi = argv_local;
876 *argv = argv_local;
877 cfg_included = 1;
878 continue;
879 }
880#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800881 if (arg_match(&arg, &help, argi)) {
882 show_help(stdout, 0);
883 exit(EXIT_SUCCESS);
884 } else if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700885 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800886 if (!global->codec)
887 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700888 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800889 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400890
John Koleszar6ad3b742012-11-06 12:02:42 -0800891 if (global->passes < 1 || global->passes > 2)
892 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700893 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800894 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400895
John Koleszar6ad3b742012-11-06 12:02:42 -0800896 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700897 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800898 } else if (arg_match(&arg, &usage, argi))
899 global->usage = arg_parse_uint(&arg);
Debargha Mukherjee67adf422018-01-24 16:08:23 -0800900 else if (arg_match(&arg, &good_dl, argi))
901 warn("Deprecated --good option! Ignoring\n");
John Koleszar6ad3b742012-11-06 12:02:42 -0800902 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700903 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800904 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700905 global->color_type = I420;
906 else if (arg_match(&arg, &use_i422, argi))
907 global->color_type = I422;
908 else if (arg_match(&arg, &use_i444, argi))
909 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700910 else if (arg_match(&arg, &use_i440, argi))
911 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800912 else if (arg_match(&arg, &quietarg, argi))
913 global->quiet = 1;
914 else if (arg_match(&arg, &verbosearg, argi))
915 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700916 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800917 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700918 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800919 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700920 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800921 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700922 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800923 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700924 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800925 global->framerate = arg_parse_rational(&arg);
926 validate_positive_rational(arg.name, &global->framerate);
927 global->have_framerate = 1;
928 } else if (arg_match(&arg, &out_part, argi))
929 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700930 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800931 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700932 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800933 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700934 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800935 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800936 else if (arg_match(&arg, &disable_warnings, argi))
937 global->disable_warnings = 1;
938 else if (arg_match(&arg, &disable_warning_prompt, argi))
939 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800940 else
John Koleszarc6b90392012-07-13 15:21:29 -0700941 argj++;
942 }
943
John Koleszar6ad3b742012-11-06 12:02:42 -0800944 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700945 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800946 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700947 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
948 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800949 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800950 }
John Koleszarc6b90392012-07-13 15:21:29 -0700951 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800952 /* Validate global config */
953 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700954#if CONFIG_AV1_ENCODER
955 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800956 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700957 if (global->codec != NULL && global->codec->name != NULL)
Thomas Daede80826142017-03-20 15:44:24 -0700958 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800959#else
960 global->passes = 1;
961#endif
962 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800963}
964
Yaowu Xuf883b422016-08-30 14:01:10 -0700965static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800966 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700967 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
968 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -0800969
clang-format6c4d83e2016-08-08 19:03:30 -0700970 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -0800971
John Koleszar25b6e9f2013-02-12 21:17:56 -0800972 if (!fseeko(input->file, 0, SEEK_END)) {
973 /* Input file is seekable. Figure out how long it is, so we can get
974 * progress info.
975 */
976 input->length = ftello(input->file);
977 rewind(input->file);
978 }
979
Frank Galligan09acd262015-06-01 10:20:58 -0700980 /* Default to 1:1 pixel aspect ratio. */
981 input->pixel_aspect_ratio.numerator = 1;
982 input->pixel_aspect_ratio.denominator = 1;
983
John Koleszar6ad3b742012-11-06 12:02:42 -0800984 /* For RAW input sources, these bytes will applied on the first frame
985 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -0700986 */
John Koleszar6ad3b742012-11-06 12:02:42 -0800987 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
988 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400989
clang-format6c4d83e2016-08-08 19:03:30 -0700990 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -0700991 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
992 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800993 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800994 input->width = input->y4m.pic_w;
995 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -0700996 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
997 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800998 input->framerate.numerator = input->y4m.fps_n;
999 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -07001000 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001001 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -08001002 } else
1003 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -08001004 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -08001005 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -08001006 } else {
1007 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001008 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001009}
1010
Yaowu Xuf883b422016-08-30 14:01:10 -07001011static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001012 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -07001013 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001014}
1015
Yaowu Xuf883b422016-08-30 14:01:10 -07001016static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -08001017 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001018 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001019
John Koleszar6ad3b742012-11-06 12:02:42 -08001020 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001021 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001022 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001023 }
1024
John Koleszar6ad3b742012-11-06 12:02:42 -08001025 if (prev) {
1026 memcpy(stream, prev, sizeof(*stream));
1027 stream->index++;
1028 prev->next = stream;
1029 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001030 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001031
John Koleszar6ad3b742012-11-06 12:02:42 -08001032 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -07001033 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -07001034 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -07001035 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001036
John Koleszar6ad3b742012-11-06 12:02:42 -08001037 /* Change the default timebase to a high enough value so that the
1038 * encoder will always create strictly increasing timestamps.
1039 */
1040 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001041
John Koleszar6ad3b742012-11-06 12:02:42 -08001042 /* Never use the library's default resolution, require it be parsed
1043 * from the file or set on the command line.
1044 */
1045 stream->config.cfg.g_w = 0;
1046 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001047
John Koleszar6ad3b742012-11-06 12:02:42 -08001048 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -08001049 stream->config.write_webm = 1;
Cyril Concolato6c788832017-10-30 16:30:35 -07001050#if CONFIG_OBU_NO_IVF
1051 stream->config.write_ivf = 0;
1052#endif
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001053#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -07001054 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001055 stream->webm_ctx.last_pts_ns = -1;
1056 stream->webm_ctx.writer = NULL;
1057 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001058#endif
Johann87c40b32012-03-01 16:12:53 -08001059
John Koleszar6ad3b742012-11-06 12:02:42 -08001060 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001061 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -08001062 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001063
John Koleszar6ad3b742012-11-06 12:02:42 -08001064 /* Output files must be specified for each stream */
1065 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001066
John Koleszar6ad3b742012-11-06 12:02:42 -08001067 stream->next = NULL;
1068 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001069}
1070
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +00001071static void set_config_arg_ctrls(struct stream_config *config, int key,
1072 const struct arg *arg) {
1073 int j;
1074 /* Point either to the next free element or the first instance of this
1075 * control.
1076 */
1077 for (j = 0; j < config->arg_ctrl_cnt; j++)
1078 if (config->arg_ctrls[j][0] == key) break;
1079
1080 /* Update/insert */
1081 assert(j < (int)ARG_CTRL_CNT_MAX);
1082 config->arg_ctrls[j][0] = key;
1083 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
1084 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1085}
1086
Yaowu Xuf883b422016-08-30 14:01:10 -07001087static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001088 struct stream_state *stream, char **argv) {
1089 char **argi, **argj;
1090 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001091 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001092 static const int *ctrl_args_map = NULL;
1093 struct stream_config *config = &stream->config;
1094 int eos_mark_found = 0;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001095 int webm_forced = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001096
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001097 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001098 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001099#if CONFIG_AV1_ENCODER
1100 } else if (strcmp(global->codec->name, "av1") == 0) {
1101 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1102 // Consider to expand this set for AV1 encoder control.
1103 ctrl_args = av1_args;
1104 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001105#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001106 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001107
John Koleszarc6b90392012-07-13 15:21:29 -07001108 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001109 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001110
John Koleszar6ad3b742012-11-06 12:02:42 -08001111 /* Once we've found an end-of-stream marker (--) we want to continue
1112 * shifting arguments but not consuming them.
1113 */
1114 if (eos_mark_found) {
1115 argj++;
1116 continue;
1117 } else if (!strcmp(*argj, "--")) {
1118 eos_mark_found = 1;
1119 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001120 }
1121
Adrian Granged2401802015-02-13 14:51:32 -08001122 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001123 config->out_fn = arg.val;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001124 if (!webm_forced) {
1125 const size_t out_fn_len = strlen(config->out_fn);
1126 if (out_fn_len >= 4 &&
1127 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
1128 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001129#if CONFIG_OBU_NO_IVF
1130 config->write_ivf = 1;
1131#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001132 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001133#if CONFIG_OBU_NO_IVF
1134 else if (out_fn_len >= 4 &&
1135 !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
1136 config->write_webm = 0;
1137 config->write_ivf = 0;
1138 }
1139#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001140 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001141 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001142 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001143#if CONFIG_FP_MB_STATS
1144 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1145 config->fpmb_stats_fn = arg.val;
1146#endif
Johannb50e5182015-01-30 15:05:14 -08001147 } else if (arg_match(&arg, &use_webm, argi)) {
1148#if CONFIG_WEBM_IO
1149 config->write_webm = 1;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001150 webm_forced = 1;
Johannb50e5182015-01-30 15:05:14 -08001151#else
1152 die("Error: --webm specified but webm is disabled.");
1153#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001154 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001155 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001156#if CONFIG_OBU_NO_IVF
1157 config->write_ivf = 1;
1158#endif
1159#if CONFIG_OBU_NO_IVF
1160 } else if (arg_match(&arg, &use_obu, argi)) {
1161 config->write_webm = 0;
1162 config->write_ivf = 0;
1163#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001164 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001165 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001166 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001167 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001168 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001169 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001170 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001171 config->cfg.g_h = arg_parse_uint(&arg);
Imdad Sardharwalla102c8652018-02-23 16:35:13 +00001172 } else if (arg_match(&arg, &forced_max_frame_width, argi)) {
1173 config->cfg.g_forced_max_frame_width = arg_parse_uint(&arg);
1174 } else if (arg_match(&arg, &forced_max_frame_height, argi)) {
1175 config->cfg.g_forced_max_frame_height = arg_parse_uint(&arg);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001176 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1177 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1178 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1179 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
James Zerndba73762014-05-10 17:44:12 -07001180#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001181 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001182 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001183#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001184 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001185 config->cfg.g_timebase = arg_parse_rational(&arg);
1186 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001187 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001188 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001189 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001190 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001191 } else if (arg_match(&arg, &large_scale_tile, argi)) {
1192 config->cfg.large_scale_tile = arg_parse_uint(&arg);
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001193#if CONFIG_MONO_VIDEO
1194 } else if (arg_match(&arg, &monochrome, argi)) {
1195 config->cfg.monochrome = 1;
1196#endif // CONFIG_MONO_VIDEO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001197 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001198 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001199 } else if (arg_match(&arg, &resize_mode, argi)) {
1200 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001201 } else if (arg_match(&arg, &resize_denominator, argi)) {
1202 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1203 } else if (arg_match(&arg, &resize_kf_denominator, argi)) {
1204 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001205 } else if (arg_match(&arg, &superres_mode, argi)) {
1206 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001207 } else if (arg_match(&arg, &superres_denominator, argi)) {
1208 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1209 } else if (arg_match(&arg, &superres_kf_denominator, argi)) {
1210 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001211 } else if (arg_match(&arg, &superres_qthresh, argi)) {
1212 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1213 } else if (arg_match(&arg, &superres_kf_qthresh, argi)) {
1214 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001215 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001216 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001217 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001218 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001219 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001220 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001221 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001222 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001223 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001224 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001225 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001226 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001227 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001228 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001229 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001230 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001231 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001232 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001233 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001234 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001235 if (global->passes < 2)
1236 warn("option %s ignored in one-pass mode.\n", arg.name);
1237 } else if (arg_match(&arg, &minsection_pct, argi)) {
1238 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1239
1240 if (global->passes < 2)
1241 warn("option %s ignored in one-pass mode.\n", arg.name);
1242 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1243 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1244
1245 if (global->passes < 2)
1246 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001247 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001248 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001249 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001250 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001251 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001252 config->cfg.kf_mode = AOM_KF_DISABLED;
Tarek AMARAc9813852018-03-05 18:40:18 -05001253 } else if (arg_match(&arg, &sframe_dist, argi)) {
1254 config->cfg.sframe_dist = arg_parse_uint(&arg);
1255 } else if (arg_match(&arg, &sframe_mode, argi)) {
1256 config->cfg.sframe_mode = arg_parse_uint(&arg);
Dominic Symes26ad0b22017-10-01 16:35:13 +02001257 } else if (arg_match(&arg, &tile_width, argi)) {
1258 config->cfg.tile_width_count =
1259 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1260 } else if (arg_match(&arg, &tile_height, argi)) {
1261 config->cfg.tile_height_count =
1262 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01001263#if CONFIG_FILEOPTIONS
1264 } else if (arg_match(&arg, &ext_partition, argi)) {
1265 config->cfg.cfg.ext_partition = !!arg_parse_uint(&arg) > 0;
1266#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001267 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001268 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001269 for (i = 0; ctrl_args[i]; i++) {
1270 if (arg_match(&arg, ctrl_args[i], argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001271 match = 1;
Imdad Sardharwalla3ab64bd2017-12-13 15:03:53 +00001272 if (ctrl_args_map) {
1273 set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001274 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001275 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001276 }
clang-format6c4d83e2016-08-08 19:03:30 -07001277 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001278 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001279 }
Sebastien Alaiwan14af5b92017-05-12 14:27:30 +02001280 config->use_16bit_internal =
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +02001281 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
John Koleszar6ad3b742012-11-06 12:02:42 -08001282 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001283}
1284
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001285#define FOREACH_STREAM(iterator, list) \
1286 for (struct stream_state *iterator = list; iterator; \
1287 iterator = iterator->next)
John Koleszar9e50ed72012-02-15 12:39:38 -08001288
Alex Conversed66bd222014-02-21 10:52:09 -08001289static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001290 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001291 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001292 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001293
John Koleszar6ad3b742012-11-06 12:02:42 -08001294 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001295 fatal(
1296 "Stream %d: Specify stream dimensions with --width (-w) "
1297 " and --height (-h)",
1298 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001299
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001300 // Check that the codec bit depth is greater than the input bit depth.
1301 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001302 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001303 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1304 stream->index, (int)stream->config.cfg.g_bit_depth,
1305 stream->config.cfg.g_input_bit_depth);
1306 }
1307
John Koleszar6ad3b742012-11-06 12:02:42 -08001308 for (streami = stream; streami; streami = streami->next) {
1309 /* All streams require output files */
1310 if (!streami->config.out_fn)
1311 fatal("Stream %d: Output file is required (specify with -o)",
1312 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001313
John Koleszar6ad3b742012-11-06 12:02:42 -08001314 /* Check for two streams outputting to the same file */
1315 if (streami != stream) {
1316 const char *a = stream->config.out_fn;
1317 const char *b = streami->config.out_fn;
1318 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1319 fatal("Stream %d: duplicate output file (from stream %d)",
1320 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001321 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001322
1323 /* Check for two streams sharing a stats file. */
1324 if (streami != stream) {
1325 const char *a = stream->config.stats_fn;
1326 const char *b = streami->config.stats_fn;
1327 if (a && b && !strcmp(a, b))
1328 fatal("Stream %d: duplicate stats file (from stream %d)",
1329 streami->index, stream->index);
1330 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001331
1332#if CONFIG_FP_MB_STATS
1333 /* Check for two streams sharing a mb stats file. */
1334 if (streami != stream) {
1335 const char *a = stream->config.fpmb_stats_fn;
1336 const char *b = streami->config.fpmb_stats_fn;
1337 if (a && b && !strcmp(a, b))
1338 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1339 streami->index, stream->index);
1340 }
1341#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001342 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001343}
1344
clang-format6c4d83e2016-08-08 19:03:30 -07001345static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001346 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001347 if (!stream->config.cfg.g_w) {
1348 if (!stream->config.cfg.g_h)
1349 stream->config.cfg.g_w = w;
1350 else
1351 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1352 }
1353 if (!stream->config.cfg.g_h) {
1354 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1355 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001356}
1357
clang-format6c4d83e2016-08-08 19:03:30 -07001358static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001359 switch (t) {
1360 case FILE_TYPE_RAW: return "RAW";
1361 case FILE_TYPE_Y4M: return "Y4M";
1362 default: return "Other";
1363 }
1364}
1365
Yaowu Xuf883b422016-08-30 14:01:10 -07001366static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001367 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001368 case AOM_IMG_FMT_I420: return "I420";
1369 case AOM_IMG_FMT_I422: return "I422";
1370 case AOM_IMG_FMT_I444: return "I444";
1371 case AOM_IMG_FMT_I440: return "I440";
1372 case AOM_IMG_FMT_YV12: return "YV12";
1373 case AOM_IMG_FMT_I42016: return "I42016";
1374 case AOM_IMG_FMT_I42216: return "I42216";
1375 case AOM_IMG_FMT_I44416: return "I44416";
1376 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001377 default: return "Other";
1378 }
1379}
Ralph Giles061a16d2012-01-05 15:05:05 -06001380
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001381static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001382 struct AvxEncoderConfig *global,
1383 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001384#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001385 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001386
John Koleszar6ad3b742012-11-06 12:02:42 -08001387 if (stream->index == 0) {
1388 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001389 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001390 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001391 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001392 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001393 }
1394 if (stream->next || stream->index)
1395 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1396 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
Sebastien Alaiwan27511922017-06-05 15:25:46 +02001397 fprintf(stderr, "Coding path: %s\n",
1398 stream->config.use_16bit_internal ? "HBD" : "LBD");
John Koleszar6ad3b742012-11-06 12:02:42 -08001399 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001400
John Koleszar6ad3b742012-11-06 12:02:42 -08001401 SHOW(g_usage);
1402 SHOW(g_threads);
1403 SHOW(g_profile);
1404 SHOW(g_w);
1405 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001406 SHOW(g_bit_depth);
1407 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001408 SHOW(g_timebase.num);
1409 SHOW(g_timebase.den);
1410 SHOW(g_error_resilient);
1411 SHOW(g_pass);
1412 SHOW(g_lag_in_frames);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001413 SHOW(large_scale_tile);
John Koleszar6ad3b742012-11-06 12:02:42 -08001414 SHOW(rc_dropframe_thresh);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001415 SHOW(rc_resize_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001416 SHOW(rc_resize_denominator);
1417 SHOW(rc_resize_kf_denominator);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001418 SHOW(rc_superres_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001419 SHOW(rc_superres_denominator);
1420 SHOW(rc_superres_kf_denominator);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001421 SHOW(rc_superres_qthresh);
1422 SHOW(rc_superres_kf_qthresh);
John Koleszar6ad3b742012-11-06 12:02:42 -08001423 SHOW(rc_end_usage);
1424 SHOW(rc_target_bitrate);
1425 SHOW(rc_min_quantizer);
1426 SHOW(rc_max_quantizer);
1427 SHOW(rc_undershoot_pct);
1428 SHOW(rc_overshoot_pct);
1429 SHOW(rc_buf_sz);
1430 SHOW(rc_buf_initial_sz);
1431 SHOW(rc_buf_optimal_sz);
1432 SHOW(rc_2pass_vbr_bias_pct);
1433 SHOW(rc_2pass_vbr_minsection_pct);
1434 SHOW(rc_2pass_vbr_maxsection_pct);
1435 SHOW(kf_mode);
1436 SHOW(kf_min_dist);
1437 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001438}
1439
John Koleszar9e50ed72012-02-15 12:39:38 -08001440static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001441 struct AvxEncoderConfig *global,
1442 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001443 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001444 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001445
Yaowu Xuf883b422016-08-30 14:01:10 -07001446 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001447
John Koleszar6ad3b742012-11-06 12:02:42 -08001448 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001449
clang-format6c4d83e2016-08-08 19:03:30 -07001450 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001451
John Koleszar6ad3b742012-11-06 12:02:42 -08001452 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1453 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001454
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001455#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001456 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001457 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001458 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1459 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001460 }
James Zernc8e5a772016-02-11 18:53:50 -08001461#else
1462 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001463#endif
1464
Cyril Concolato6c788832017-10-30 16:30:35 -07001465 if (!stream->config.write_webm
1466#if CONFIG_OBU_NO_IVF
1467 && stream->config.write_ivf
1468#endif
Johann3c30fb42018-02-08 14:33:20 -08001469 ) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001470 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1471 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001472}
1473
John Koleszar9e50ed72012-02-15 12:39:38 -08001474static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001475 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001476 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001477
Yaowu Xuf883b422016-08-30 14:01:10 -07001478 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001479
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001480#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001481 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001482 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001483 }
1484#endif
1485
Cyril Concolato6c788832017-10-30 16:30:35 -07001486 if (!stream->config.write_webm
1487#if CONFIG_OBU_NO_IVF
1488 && stream->config.write_ivf
1489#endif
Johann3c30fb42018-02-08 14:33:20 -08001490 ) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001491 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001492 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001493 stream->frames_out);
1494 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001495
John Koleszar6ad3b742012-11-06 12:02:42 -08001496 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001497}
1498
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001499static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001500 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001501 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001502 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001503 fatal("Failed to open statistics store");
1504 } else {
1505 if (!stats_open_mem(&stream->stats, pass))
1506 fatal("Failed to open statistics store");
1507 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001508
Pengchong Jinf349b072014-07-14 09:13:38 -07001509#if CONFIG_FP_MB_STATS
1510 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001511 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1512 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001513 fatal("Failed to open mb statistics store");
1514 } else {
1515 if (!stats_open_mem(&stream->fpmb_stats, pass))
1516 fatal("Failed to open mb statistics store");
1517 }
1518#endif
1519
John Koleszar6ad3b742012-11-06 12:02:42 -08001520 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001521 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1522 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001523 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001524 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001525#if CONFIG_FP_MB_STATS
1526 stream->config.cfg.rc_firstpass_mb_stats_in =
1527 stats_get(&stream->fpmb_stats);
1528#endif
1529 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001530
John Koleszar6ad3b742012-11-06 12:02:42 -08001531 stream->cx_time = 0;
1532 stream->nbytes = 0;
1533 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001534}
1535
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001536static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001537 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001538 int i;
1539 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001540
Yaowu Xuf883b422016-08-30 14:01:10 -07001541 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1542 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001543 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001544
John Koleszar6ad3b742012-11-06 12:02:42 -08001545 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001546 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001547 &stream->config.cfg, flags);
1548 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001549
Yaowu Xuf883b422016-08-30 14:01:10 -07001550 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001551 * we're being clever to store the control IDs in an array. Real
1552 * applications will want to make use of the enumerations directly
1553 */
1554 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1555 int ctrl = stream->config.arg_ctrls[i][0];
1556 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001557 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001558 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001559
John Koleszar6ad3b742012-11-06 12:02:42 -08001560 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1561 }
1562
Tom Fineganba02c242017-05-16 15:01:54 -07001563#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001564 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001565 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
Debargha Mukherjee2ccf4b92018-02-27 17:30:46 -08001566 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, { 1 } };
Yaowu Xuf883b422016-08-30 14:01:10 -07001567 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001568
Yaowu Xuf883b422016-08-30 14:01:10 -07001569 if (strcmp(global->codec->name, "av1") == 0) {
Yunqing Wang8ae64a92018-01-12 12:26:44 -08001570 aom_codec_control(&stream->decoder, AV1_SET_TILE_MODE,
1571 stream->config.cfg.large_scale_tile);
1572 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_mode");
1573
Yaowu Xuf883b422016-08-30 14:01:10 -07001574 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001575 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1576
Yaowu Xuf883b422016-08-30 14:01:10 -07001577 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001578 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1579 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001580 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001581#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001582}
1583
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001584static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001585 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001586 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001587 aom_codec_pts_t frame_start, next_frame_start;
1588 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1589 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001590
clang-format6c4d83e2016-08-08 19:03:30 -07001591 frame_start =
1592 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1593 cfg->g_timebase.num / global->framerate.num;
1594 next_frame_start =
1595 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1596 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001597
Yaowu Xud3e7c682017-12-21 14:08:25 -08001598 /* Scale if necessary */
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001599 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001600 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001601 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001602 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001603 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1604 exit(EXIT_FAILURE);
1605 }
1606#if CONFIG_LIBYUV
1607 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001608 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001609 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001610 }
clang-format6c4d83e2016-08-08 19:03:30 -07001611 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001612 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1613 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1614 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1615 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1616 stream->img->stride[AOM_PLANE_Y] / 2,
1617 (uint16 *)stream->img->planes[AOM_PLANE_U],
1618 stream->img->stride[AOM_PLANE_U] / 2,
1619 (uint16 *)stream->img->planes[AOM_PLANE_V],
1620 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001621 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001622 img = stream->img;
1623#else
clang-format6c4d83e2016-08-08 19:03:30 -07001624 stream->encoder.err = 1;
1625 ctx_exit_on_error(&stream->encoder,
1626 "Stream %d: Failed to encode frame.\n"
Johanne07a6752018-01-10 12:47:44 -08001627 "libyuv is required for scaling but is currently "
1628 "disabled.\n"
1629 "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1630 "cmake.\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001631 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001632#endif
1633 }
1634 }
John Koleszar34882b92012-03-01 12:50:40 -08001635 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001636 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001637 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1638 exit(EXIT_FAILURE);
1639 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001640#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001641 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001642 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001643 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001644 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001645 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1646 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1647 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1648 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1649 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1650 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001651 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001652 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001653#else
1654 stream->encoder.err = 1;
1655 ctx_exit_on_error(&stream->encoder,
1656 "Stream %d: Failed to encode frame.\n"
1657 "Scaling disabled in this configuration. \n"
1658 "To enable, configure with --enable-libyuv\n",
1659 stream->index);
1660#endif
John Koleszar34882b92012-03-01 12:50:40 -08001661 }
1662
Yaowu Xuf883b422016-08-30 14:01:10 -07001663 aom_usec_timer_start(&timer);
1664 aom_codec_encode(&stream->encoder, img, frame_start,
Sean DuBois47cc2552018-01-23 07:44:16 +00001665 (uint32_t)(next_frame_start - frame_start), 0);
Yaowu Xuf883b422016-08-30 14:01:10 -07001666 aom_usec_timer_mark(&timer);
1667 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001668 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1669 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001670}
1671
John Koleszar6ad3b742012-11-06 12:02:42 -08001672static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001673 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001674 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001675
Yaowu Xuf883b422016-08-30 14:01:10 -07001676 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001677 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1678 stream->counts[q]++;
1679 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001680}
1681
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001682static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001683 struct AvxEncoderConfig *global, int *got_data) {
1684 const aom_codec_cx_pkt_t *pkt;
1685 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1686 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001687
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001688 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001689 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001690 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001691 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001692
John Koleszar6ad3b742012-11-06 12:02:42 -08001693 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001694 case AOM_CODEC_CX_FRAME_PKT:
1695 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001696 stream->frames_out++;
1697 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001698 if (!global->quiet)
1699 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001700
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001701 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001702#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001703 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001704 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001705 }
1706#endif
1707 if (!stream->config.write_webm) {
Cyril Concolato6c788832017-10-30 16:30:35 -07001708#if CONFIG_OBU_NO_IVF
1709 if (stream->config.write_ivf) {
1710#endif
1711 if (pkt->data.frame.partition_id <= 0) {
1712 ivf_header_pos = ftello(stream->file);
1713 fsize = pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001714
Cyril Concolato6c788832017-10-30 16:30:35 -07001715 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1716 } else {
1717 fsize += pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001718
Cyril Concolato6c788832017-10-30 16:30:35 -07001719 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1720 const FileOffset currpos = ftello(stream->file);
1721 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1722 ivf_write_frame_size(stream->file, fsize);
1723 fseeko(stream->file, currpos, SEEK_SET);
1724 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001725 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001726#if CONFIG_OBU_NO_IVF
John Koleszar6ad3b742012-11-06 12:02:42 -08001727 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001728#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001729
clang-format6c4d83e2016-08-08 19:03:30 -07001730 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1731 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001732 }
1733 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001734
John Koleszar9e50ed72012-02-15 12:39:38 -08001735 *got_data = 1;
Tom Fineganba02c242017-05-16 15:01:54 -07001736#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001737 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001738 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Sean DuBois47cc2552018-01-23 07:44:16 +00001739 (unsigned int)pkt->data.frame.sz, NULL);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001740 if (stream->decoder.err) {
1741 warn_or_exit_on_error(&stream->decoder,
1742 global->test_decode == TEST_DECODE_FATAL,
1743 "Failed to decode frame %d in stream %d",
1744 stream->frames_out + 1, stream->index);
1745 stream->mismatch_seen = stream->frames_out + 1;
1746 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001747 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001748#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001749 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001750 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001751 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001752 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001753 pkt->data.twopass_stats.sz);
1754 stream->nbytes += pkt->data.raw.sz;
1755 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001756#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001757 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001758 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001759 pkt->data.firstpass_mb_stats.sz);
1760 stream->nbytes += pkt->data.raw.sz;
1761 break;
1762#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001763 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001764
1765 if (global->show_psnr) {
1766 int i;
1767
1768 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1769 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1770 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001771 if (!global->quiet)
1772 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001773 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1774 }
1775 stream->psnr_count++;
1776 }
1777
1778 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001779 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001780 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001781 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001782}
1783
clang-format6c4d83e2016-08-08 19:03:30 -07001784static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001785 int i;
1786 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001787
clang-format6c4d83e2016-08-08 19:03:30 -07001788 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001789
John Koleszar6ad3b742012-11-06 12:02:42 -08001790 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001791 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001792 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001793 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001794
John Koleszar6ad3b742012-11-06 12:02:42 -08001795 for (i = 0; i < 4; i++) {
1796 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1797 }
1798 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001799}
1800
John Koleszar25b6e9f2013-02-12 21:17:56 -08001801static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001802 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001803}
1804
clang-format6c4d83e2016-08-08 19:03:30 -07001805static void test_decode(struct stream_state *stream,
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001806 enum TestDecodeFatality fatal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001807 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001808
clang-format6c4d83e2016-08-08 19:03:30 -07001809 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001810
John Koleszarb3c350a2013-03-13 12:15:43 -07001811 /* Get the internal reference frame */
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001812 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1813 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001814
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001815 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1816 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1817 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1818 aom_image_t enc_hbd_img;
1819 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1820 enc_img.d_w, enc_img.d_h, 16);
1821 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1822 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001823 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001824 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1825 aom_image_t dec_hbd_img;
1826 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1827 dec_img.d_w, dec_img.d_h, 16);
1828 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1829 dec_img = dec_hbd_img;
1830 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001831 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001832
John Koleszar6ad3b742012-11-06 12:02:42 -08001833 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001834 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001835
Imdad Sardharwallab5def022017-12-14 11:20:24 +00001836 if (!aom_compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001837 int y[4], u[4], v[4];
Yaowu Xuf883b422016-08-30 14:01:10 -07001838 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001839 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001840 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001841 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001842 }
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001843 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001844 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001845 "Stream %d: Encode/decode mismatch on frame %d at"
1846 " Y[%d, %d] {%d/%d},"
1847 " U[%d, %d] {%d/%d},"
1848 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001849 stream->index, stream->frames_out, y[0], y[1], y[2],
1850 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 -08001851 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001852 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001853
Yaowu Xuf883b422016-08-30 14:01:10 -07001854 aom_img_free(&enc_img);
1855 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001856}
John Koleszarefd54f82012-02-13 16:52:18 -08001857
John Koleszar25b6e9f2013-02-12 21:17:56 -08001858static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001859 int64_t hours;
1860 int64_t mins;
1861 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001862
1863 if (etl >= 0) {
1864 hours = etl / 3600;
1865 etl -= hours * 3600;
1866 mins = etl / 60;
1867 etl -= mins * 60;
1868 secs = etl;
1869
clang-format6c4d83e2016-08-08 19:03:30 -07001870 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1871 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001872 } else {
1873 fprintf(stderr, "[%3s unknown] ", label);
1874 }
1875}
1876
Tom Finegan44dd3272013-11-20 17:18:28 -08001877int main(int argc, const char **argv_) {
1878 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001879 aom_image_t raw;
Yaowu Xuf883b422016-08-30 14:01:10 -07001880 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001881 int allocated_raw_shift = 0;
1882 int use_16bit_internal = 0;
1883 int input_shift = 0;
Tom Finegan44dd3272013-11-20 17:18:28 -08001884 int frame_avail, got_data;
1885
Yaowu Xuf883b422016-08-30 14:01:10 -07001886 struct AvxInputContext input;
1887 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001888 struct stream_state *streams = NULL;
1889 char **argv, **argi;
1890 uint64_t cx_time = 0;
1891 int stream_cnt = 0;
1892 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001893 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001894
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001895 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001896 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001897
John Koleszar6ad3b742012-11-06 12:02:42 -08001898 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001899 input.framerate.numerator = 30;
1900 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001901 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001902 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001903
1904 /* First parse the global configuration values, because we want to apply
1905 * other parameters on top of the default configuration provided by the
1906 * codec.
1907 */
1908 argv = argv_dup(argc - 1, argv_ + 1);
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01001909 parse_global_config(&global, &argc, &argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08001910
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01001911#if CONFIG_FILEOPTIONS
1912 if (argc < 2) usage_exit();
1913#else
James Zernfd8c78c2017-11-27 16:37:33 -08001914 if (argc < 3) usage_exit();
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01001915#endif
James Zernfd8c78c2017-11-27 16:37:33 -08001916
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001917 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001918 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1919 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1920 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1921 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1922 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001923 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001924
John Koleszar6ad3b742012-11-06 12:02:42 -08001925 {
1926 /* Now parse each stream's parameters. Using a local scope here
1927 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1928 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001929 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001930 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001931
John Koleszar6ad3b742012-11-06 12:02:42 -08001932 do {
1933 stream = new_stream(&global, stream);
1934 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001935 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001936 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001937 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001938
John Koleszarc6b90392012-07-13 15:21:29 -07001939 /* Check for unrecognized options */
1940 for (argi = argv; *argi; argi++)
1941 if (argi[0][0] == '-' && argi[0][1])
1942 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001943
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001944 FOREACH_STREAM(stream, streams) {
1945 check_encoder_config(global.disable_warning_prompt, &global,
1946 &stream->config.cfg);
1947 }
Tom Finegan249366b2013-11-25 12:05:19 -08001948
John Koleszarc6b90392012-07-13 15:21:29 -07001949 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001950 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001951
James Zernfd8c78c2017-11-27 16:37:33 -08001952 if (!input.filename) {
1953 fprintf(stderr, "No input file specified!\n");
1954 usage_exit();
1955 }
John Koleszar53291892010-10-22 14:48:21 -04001956
John Koleszar8dd82872013-05-06 11:01:35 -07001957 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001958 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001959
John Koleszar6ad3b742012-11-06 12:02:42 -08001960 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001961 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001962 int64_t estimated_time_left = -1;
1963 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001964 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001965
John Koleszar6ad3b742012-11-06 12:02:42 -08001966 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001967
John Koleszar6ad3b742012-11-06 12:02:42 -08001968 /* If the input file doesn't specify its w/h (raw files), try to get
1969 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001970 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001971 if (!input.width || !input.height) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001972 FOREACH_STREAM(stream, streams) {
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001973 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1974 input.width = stream->config.cfg.g_w;
1975 input.height = stream->config.cfg.g_h;
1976 break;
1977 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001978 };
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001979 }
John Koleszarc6b90392012-07-13 15:21:29 -07001980
John Koleszar6ad3b742012-11-06 12:02:42 -08001981 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001982 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001983 fatal(
1984 "Specify stream dimensions with --width (-w) "
1985 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001986
1987 /* If input file does not specify bit-depth but input-bit-depth parameter
1988 * exists, assume that to be the input bit-depth. However, if the
1989 * input-bit-depth paramter does not exist, assume the input bit-depth
1990 * to be the same as the codec bit-depth.
1991 */
1992 if (!input.bit_depth) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001993 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001994 if (stream->config.cfg.g_input_bit_depth)
1995 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1996 else
1997 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1998 (int)stream->config.cfg.g_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001999 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002000 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002001 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002002 FOREACH_STREAM(stream, streams) {
2003 stream->config.cfg.g_input_bit_depth = input.bit_depth;
2004 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002005 }
2006
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002007 FOREACH_STREAM(stream, streams) {
Thomas Daede8ec53b22016-09-19 13:24:51 -07002008 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2009 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2010 was selected. */
2011 switch (stream->config.cfg.g_profile) {
2012 case 0:
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002013 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2014 input.fmt == AOM_IMG_FMT_I44416)) {
Yaowu Xuc1fdb4b2018-03-16 08:44:13 -07002015 if (!stream->config.cfg.monochrome) {
2016 stream->config.cfg.g_profile = 1;
2017 profile_updated = 1;
2018 }
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002019 } else if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2020 input.fmt == AOM_IMG_FMT_I42216) {
2021 stream->config.cfg.g_profile = 2;
2022 profile_updated = 1;
2023 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002024 break;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002025 case 1:
2026 if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2027 input.fmt == AOM_IMG_FMT_I42216) {
2028 stream->config.cfg.g_profile = 2;
2029 profile_updated = 1;
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002030 } else if (input.bit_depth < 12 &&
2031 (input.fmt == AOM_IMG_FMT_I420 ||
2032 input.fmt == AOM_IMG_FMT_I42016)) {
2033 stream->config.cfg.g_profile = 0;
2034 profile_updated = 1;
2035 }
2036 break;
2037 case 2:
2038 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2039 input.fmt == AOM_IMG_FMT_I44416)) {
2040 stream->config.cfg.g_profile = 1;
2041 profile_updated = 1;
2042 } else if (input.bit_depth < 12 &&
2043 (input.fmt == AOM_IMG_FMT_I420 ||
2044 input.fmt == AOM_IMG_FMT_I42016)) {
2045 stream->config.cfg.g_profile = 0;
2046 profile_updated = 1;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002047 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002048 break;
2049 default: break;
2050 }
2051 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002052 /* Automatically set the codec bit depth to match the input bit depth.
2053 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002054 if (stream->config.cfg.g_input_bit_depth >
2055 (unsigned int)stream->config.cfg.g_bit_depth) {
2056 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2057 }
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002058 if (stream->config.cfg.g_bit_depth > 10) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002059 switch (stream->config.cfg.g_profile) {
2060 case 0:
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002061 case 1:
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002062 stream->config.cfg.g_profile = 2;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002063 profile_updated = 1;
2064 break;
2065 default: break;
2066 }
2067 }
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002068 if (stream->config.cfg.g_bit_depth > 8) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002069 stream->config.use_16bit_internal = 1;
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002070 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002071 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002072 fprintf(stderr,
Debargha Mukherjeefc321aa2018-01-12 16:26:47 -08002073 "Warning: automatically updating to profile %d to "
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002074 "match input format.\n",
2075 stream->config.cfg.g_profile);
2076 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002077 }
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002078
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002079 FOREACH_STREAM(stream, streams) {
2080 set_stream_dimensions(stream, input.width, input.height);
2081 }
2082 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
John Koleszar77e6b452010-11-03 13:58:40 -04002083
John Koleszar6ad3b742012-11-06 12:02:42 -08002084 /* Ensure that --passes and --pass are consistent. If --pass is set and
2085 * --passes=2, ensure --fpf was set.
2086 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002087 if (global.pass && global.passes == 2) {
2088 FOREACH_STREAM(stream, streams) {
clang-format6c4d83e2016-08-08 19:03:30 -07002089 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002090 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002091 " and --passes=2\n",
2092 stream->index, global.pass);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002093 }
2094 }
John Koleszar3245d462010-06-10 17:10:12 -04002095
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002096#if !CONFIG_WEBM_IO
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002097 FOREACH_STREAM(stream, streams) {
Ronald S. Bultje39775072015-12-16 13:35:43 -05002098 if (stream->config.write_webm) {
2099 stream->config.write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07002100#if CONFIG_OBU_NO_IVF
2101 stream->config.write_ivf = 0;
2102#endif
clang-format6c4d83e2016-08-08 19:03:30 -07002103 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002104 "aomenc was compiled without WebM container support."
Cyril Concolato6c788832017-10-30 16:30:35 -07002105#if CONFIG_OBU_NO_IVF
2106 "Producing OBU output");
2107#else
clang-format6c4d83e2016-08-08 19:03:30 -07002108 "Producing IVF output");
Cyril Concolato6c788832017-10-30 16:30:35 -07002109#endif
Ronald S. Bultje39775072015-12-16 13:35:43 -05002110 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002111 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002112#endif
2113
Debargha Mukherjee4a707a82018-03-02 11:19:43 -08002114 /* Use the frame rate from the file only if none was specified
2115 * on the command-line.
2116 */
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002117 if (!global.have_framerate) {
2118 global.framerate.num = input.framerate.numerator;
2119 global.framerate.den = input.framerate.denominator;
2120 }
2121 FOREACH_STREAM(stream, streams) {
2122 stream->config.cfg.g_timebase.den = global.framerate.num;
2123 stream->config.cfg.g_timebase.num = global.framerate.den;
2124 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002125 /* Show configuration */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002126 if (global.verbose && pass == 0) {
2127 FOREACH_STREAM(stream, streams) {
2128 show_stream_config(stream, &global, &input);
2129 }
2130 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002131
2132 if (pass == (global.pass ? global.pass - 1 : 0)) {
2133 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002134 /*The Y4M reader does its own allocation.
2135 Just initialize this here to avoid problems if we never read any
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002136 frames.*/
John Koleszarc6b90392012-07-13 15:21:29 -07002137 memset(&raw, 0, sizeof(raw));
2138 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002139 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002140
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002141 FOREACH_STREAM(stream, streams) {
2142 stream->rate_hist =
2143 init_rate_histogram(&stream->config.cfg, &global.framerate);
2144 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002145 }
2146
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002147 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2148 FOREACH_STREAM(stream, streams) {
2149 open_output_file(stream, &global, &input.pixel_aspect_ratio);
2150 }
2151 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
Yaowu Xuf883b422016-08-30 14:01:10 -07002152 if (strcmp(global.codec->name, "av1") == 0 ||
2153 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002154 // Check to see if at least one stream uses 16 bit internal.
2155 // Currently assume that the bit_depths for all streams using
2156 // highbitdepth are the same.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002157 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002158 if (stream->config.use_16bit_internal) {
2159 use_16bit_internal = 1;
2160 }
Yaowu Xu913867b2018-01-23 18:27:59 -08002161 input_shift = (int)stream->config.cfg.g_bit_depth -
2162 stream->config.cfg.g_input_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002163 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002164 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002165
John Koleszarc6b90392012-07-13 15:21:29 -07002166 frame_avail = 1;
2167 got_data = 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002168
John Koleszarc6b90392012-07-13 15:21:29 -07002169 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002170 struct aom_usec_timer timer;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002171
John Koleszar6ad3b742012-11-06 12:02:42 -08002172 if (!global.limit || frames_in < global.limit) {
2173 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002174
clang-format6c4d83e2016-08-08 19:03:30 -07002175 if (frame_avail) frames_in++;
2176 seen_frames =
2177 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002178
John Koleszar6ad3b742012-11-06 12:02:42 -08002179 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002180 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002181 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2182
John Koleszar6ad3b742012-11-06 12:02:42 -08002183 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002184 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2185 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002186 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002187 fprintf(stderr, "frame %4d ", frames_in);
2188
clang-format6c4d83e2016-08-08 19:03:30 -07002189 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002190 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002191 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002192 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002193 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002194 }
2195
hui su251e1512016-10-03 15:48:43 -07002196 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002197 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002198 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002199
John Koleszar6ad3b742012-11-06 12:02:42 -08002200 if (frames_in > global.skip_frames) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002201 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002202 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2203 assert(use_16bit_internal);
2204 // Input bit depth and stream bit depth do not match, so up
2205 // shift frame to stream bit depth
2206 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002207 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002208 input.width, input.height, 32);
2209 allocated_raw_shift = 1;
2210 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002211 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002212 frame_to_encode = &raw_shift;
2213 } else {
2214 frame_to_encode = &raw;
2215 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002216 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002217 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002218 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002219 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002220 if (stream->config.use_16bit_internal)
2221 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002222 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002223 else
2224 assert(0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002225 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002226 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002227 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002228 FOREACH_STREAM(stream, streams) {
2229 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2230 frames_in);
2231 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002232 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002233 aom_usec_timer_mark(&timer);
2234 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002235
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002236 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
John Koleszarc6b90392012-07-13 15:21:29 -07002237
John Koleszar0ea50ce2010-05-18 11:58:33 -04002238 got_data = 0;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002239 FOREACH_STREAM(stream, streams) {
2240 get_cx_data(stream, &global, &got_data);
2241 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002242
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002243 if (!got_data && input.length && streams != NULL &&
2244 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002245 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002246 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002247 int64_t remaining;
2248 int64_t rate;
2249
2250 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002251 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002252
2253 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002254 remaining = 1000 * (global.limit - global.skip_frames -
2255 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002256 } else {
James Zerne3578af2014-04-17 10:47:08 -07002257 const int64_t input_pos = ftello(input.file);
2258 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002259 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002260
2261 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002262 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002263 }
2264
clang-format6c4d83e2016-08-08 19:03:30 -07002265 average_rate =
2266 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002267 estimated_time_left = average_rate ? remaining / average_rate : -1;
2268 }
2269
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002270 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2271 FOREACH_STREAM(stream, streams) {
Imdad Sardharwallab5def022017-12-14 11:20:24 +00002272 test_decode(stream, global.test_decode);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002273 }
2274 }
John Koleszarc6b90392012-07-13 15:21:29 -07002275 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002276
John Koleszarc6b90392012-07-13 15:21:29 -07002277 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002278 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002279 }
2280
clang-format6c4d83e2016-08-08 19:03:30 -07002281 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002282
Deb Mukherjeea160d722014-09-30 21:56:33 -07002283 if (!global.quiet) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002284 FOREACH_STREAM(stream, streams) {
Johann3c30fb42018-02-08 14:33:20 -08002285 fprintf(stderr,
2286 "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2287 "b/f %7" PRId64
2288 "b/s"
2289 " %7" PRId64 " %s (%.2f fps)\033[K\n",
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002290 pass + 1, global.passes, frames_in, stream->frames_out,
2291 (int64_t)stream->nbytes,
2292 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
Johann3c30fb42018-02-08 14:33:20 -08002293 seen_frames ? (int64_t)stream->nbytes * 8 *
2294 (int64_t)global.framerate.num /
2295 global.framerate.den / seen_frames
2296 : 0,
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002297 stream->cx_time > 9999999 ? stream->cx_time / 1000
2298 : stream->cx_time,
2299 stream->cx_time > 9999999 ? "ms" : "us",
2300 usec_to_fps(stream->cx_time, seen_frames));
2301 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002302 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002303
Deb Mukherjeea160d722014-09-30 21:56:33 -07002304 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002305 if (global.codec->fourcc == AV1_FOURCC) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002306 FOREACH_STREAM(stream, streams) {
2307 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2308 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002309 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002310 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002311 }
2312 }
John Koleszarc6b90392012-07-13 15:21:29 -07002313
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002314 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002315
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002316 if (global.test_decode != TEST_DECODE_OFF) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002317 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002318 }
2319
John Koleszar6ad3b742012-11-06 12:02:42 -08002320 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002321
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002322 if (global.test_decode == TEST_DECODE_FATAL) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002323 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002324 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002325 FOREACH_STREAM(stream, streams) {
2326 close_output_file(stream, global.codec->fourcc);
2327 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002328
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002329 FOREACH_STREAM(stream, streams) {
2330 stats_close(&stream->stats, global.passes - 1);
2331 }
John Koleszarc6b90392012-07-13 15:21:29 -07002332
Pengchong Jinf349b072014-07-14 09:13:38 -07002333#if CONFIG_FP_MB_STATS
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002334 FOREACH_STREAM(stream, streams) {
2335 stats_close(&stream->fpmb_stats, global.passes - 1);
2336 }
Pengchong Jinf349b072014-07-14 09:13:38 -07002337#endif
2338
clang-format6c4d83e2016-08-08 19:03:30 -07002339 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002340 }
2341
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002342 if (global.show_q_hist_buckets) {
2343 FOREACH_STREAM(stream, streams) {
2344 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2345 }
2346 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002347
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002348 if (global.show_rate_hist_buckets) {
2349 FOREACH_STREAM(stream, streams) {
2350 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2351 global.show_rate_hist_buckets);
2352 }
2353 }
2354 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002355
Ronald S. Bultje31214972012-10-11 10:13:48 -07002356#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002357 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2358 * to match some existing utilities.
2359 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002360 if (!(global.pass == 1 && global.passes == 2)) {
2361 FOREACH_STREAM(stream, streams) {
Yaowu Xu47e784e2013-10-16 16:29:28 -07002362 FILE *f = fopen("opsnr.stt", "a");
2363 if (stream->mismatch_seen) {
2364 fprintf(f, "First mismatch occurred in frame %d\n",
2365 stream->mismatch_seen);
2366 } else {
2367 fprintf(f, "No mismatch detected in recon buffers\n");
2368 }
2369 fclose(f);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002370 }
2371 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002372#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002373
Yaowu Xuf883b422016-08-30 14:01:10 -07002374 if (allocated_raw_shift) aom_img_free(&raw_shift);
Yaowu Xuf883b422016-08-30 14:01:10 -07002375 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002376 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002377 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002378 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002379}