blob: 0ed4714602d5d56042ceab7a686977dcaddcfd23 [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
clang-format6c4d83e2016-08-08 19:03:30 -0700129static const arg_def_t debugmode =
130 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
131static const arg_def_t outputfile =
132 ARG_DEF("o", "output", 1, "Output filename");
133static const arg_def_t use_yv12 =
134 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
135static const arg_def_t use_i420 =
136 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
137static const arg_def_t use_i422 =
138 ARG_DEF(NULL, "i422", 0, "Input file is I422");
139static const arg_def_t use_i444 =
140 ARG_DEF(NULL, "i444", 0, "Input file is I444");
141static const arg_def_t use_i440 =
142 ARG_DEF(NULL, "i440", 0, "Input file is I440");
143static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
144static const arg_def_t passes =
145 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
146static const arg_def_t pass_arg =
147 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
148static const arg_def_t fpf_name =
149 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700150#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700151static const arg_def_t fpmbf_name =
152 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700153#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700154static const arg_def_t limit =
155 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
156static const arg_def_t skip =
157 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
158static const arg_def_t deadline =
159 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
clang-format6c4d83e2016-08-08 19:03:30 -0700160static 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");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800168
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800169static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700170 { "off", TEST_DECODE_OFF },
171 { "fatal", TEST_DECODE_FATAL },
172 { "warn", TEST_DECODE_WARN },
173 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800174};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700175static const arg_def_t recontest = ARG_DEF_ENUM(
176 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700177static const arg_def_t framerate =
178 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
179static const arg_def_t use_webm =
180 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
181static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
182static const arg_def_t out_part =
183 ARG_DEF("P", "output-partitions", 0,
184 "Makes encoder output partitions. Requires IVF output!");
185static const arg_def_t q_hist_n =
186 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
187static const arg_def_t rate_hist_n =
188 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
189static const arg_def_t disable_warnings =
190 ARG_DEF(NULL, "disable-warnings", 0,
191 "Disable warnings about potentially incorrect encode settings.");
192static const arg_def_t disable_warning_prompt =
193 ARG_DEF("y", "disable-warning-prompt", 0,
194 "Display warnings, but do not prompt user to continue.");
Alex Conversed66bd222014-02-21 10:52:09 -0800195
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200196#if CONFIG_HIGHBITDEPTH
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400197static const struct arg_enum_list bitdepth_enum[] = {
198 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
199};
200
201static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
202 "b", "bit-depth", 1,
203 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
204 bitdepth_enum);
205static const arg_def_t inbitdeptharg =
206 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700207#endif
Tom Finegan44dd3272013-11-20 17:18:28 -0800208
clang-format6c4d83e2016-08-08 19:03:30 -0700209static const arg_def_t *main_args[] = { &debugmode,
210 &outputfile,
211 &codecarg,
212 &passes,
213 &pass_arg,
214 &fpf_name,
215 &limit,
216 &skip,
217 &deadline,
clang-format6c4d83e2016-08-08 19:03:30 -0700218 &good_dl,
clang-format6c4d83e2016-08-08 19:03:30 -0700219 &quietarg,
220 &verbosearg,
221 &psnrarg,
222 &use_webm,
223 &use_ivf,
224 &out_part,
225 &q_hist_n,
226 &rate_hist_n,
227 &disable_warnings,
228 &disable_warning_prompt,
229 &recontest,
230 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400231
clang-format6c4d83e2016-08-08 19:03:30 -0700232static const arg_def_t usage =
233 ARG_DEF("u", "usage", 1, "Usage profile number to use");
234static const arg_def_t threads =
235 ARG_DEF("t", "threads", 1, "Max number of threads to use");
236static const arg_def_t profile =
237 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700238static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
239static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700240#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700241static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700242 { "mono", STEREO_FORMAT_MONO },
243 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
244 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
245 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
246 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
247 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700248};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700249static const arg_def_t stereo_mode = ARG_DEF_ENUM(
250 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700251#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700252static const arg_def_t timebase = ARG_DEF(
253 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700254static const arg_def_t error_resilient =
255 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
256static const arg_def_t lag_in_frames =
257 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700258#if CONFIG_EXT_TILE
259static const arg_def_t large_scale_tile =
260 ARG_DEF(NULL, "large-scale-tile", 1,
261 "Large scale tile coding (0: off (default), 1: on)");
262#endif // CONFIG_EXT_TILE
John Koleszar0ea50ce2010-05-18 11:58:33 -0400263
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700264static const arg_def_t *global_args[] = { &use_yv12,
265 &use_i420,
266 &use_i422,
267 &use_i444,
268 &use_i440,
269 &usage,
270 &threads,
271 &profile,
272 &width,
273 &height,
James Zerndba73762014-05-10 17:44:12 -0700274#if CONFIG_WEBM_IO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700275 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700276#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700277 &timebase,
278 &framerate,
279 &error_resilient,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200280#if CONFIG_HIGHBITDEPTH
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700281 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700282#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700283 &lag_in_frames,
284#if CONFIG_EXT_TILE
285 &large_scale_tile,
286#endif // CONFIG_EXT_TILE
287 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400288
clang-format6c4d83e2016-08-08 19:03:30 -0700289static const arg_def_t dropframe_thresh =
290 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700291static const arg_def_t resize_mode =
292 ARG_DEF(NULL, "resize-mode", 1, "Frame resize mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700293static const arg_def_t resize_denominator =
294 ARG_DEF(NULL, "resize-denominator", 1, "Frame resize denominator");
295static const arg_def_t resize_kf_denominator = ARG_DEF(
296 NULL, "resize-kf-denominator", 1, "Frame resize keyframe denominator");
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700297#if CONFIG_FRAME_SUPERRES
298static const arg_def_t superres_mode =
299 ARG_DEF(NULL, "superres-mode", 1, "Frame super-resolution mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700300static const arg_def_t superres_denominator = ARG_DEF(
301 NULL, "superres-denominator", 1, "Frame super-resolution denominator");
302static const arg_def_t superres_kf_denominator =
303 ARG_DEF(NULL, "superres-kf-denominator", 1,
304 "Frame super-resolution keyframe denominator");
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700305static const arg_def_t superres_qthresh = ARG_DEF(
306 NULL, "superres-qthresh", 1, "Frame super-resolution qindex threshold");
307static const arg_def_t superres_kf_qthresh =
308 ARG_DEF(NULL, "superres-kf-qthresh", 1,
309 "Frame super-resolution keyframe qindex threshold");
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700310#endif // CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700311static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
312 { "cbr", AOM_CBR },
313 { "cq", AOM_CQ },
314 { "q", AOM_Q },
315 { NULL, 0 } };
clang-format6c4d83e2016-08-08 19:03:30 -0700316static const arg_def_t end_usage =
317 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
318static const arg_def_t target_bitrate =
319 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
320static const arg_def_t min_quantizer =
321 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
322static const arg_def_t max_quantizer =
323 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
324static const arg_def_t undershoot_pct =
325 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
326static const arg_def_t overshoot_pct =
327 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
328static const arg_def_t buf_sz =
329 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
330static const arg_def_t buf_initial_sz =
331 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
332static const arg_def_t buf_optimal_sz =
333 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700334static const arg_def_t *rc_args[] = { &dropframe_thresh,
335 &resize_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700336 &resize_denominator,
337 &resize_kf_denominator,
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700338#if CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700339 &superres_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700340 &superres_denominator,
341 &superres_kf_denominator,
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700342 &superres_qthresh,
343 &superres_kf_qthresh,
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700344#endif // CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700345 &end_usage,
346 &target_bitrate,
347 &min_quantizer,
348 &max_quantizer,
349 &undershoot_pct,
350 &overshoot_pct,
351 &buf_sz,
352 &buf_initial_sz,
353 &buf_optimal_sz,
354 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400355
clang-format6c4d83e2016-08-08 19:03:30 -0700356static const arg_def_t bias_pct =
357 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
358static const arg_def_t minsection_pct =
359 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
360static const arg_def_t maxsection_pct =
361 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
362static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
363 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400364
clang-format6c4d83e2016-08-08 19:03:30 -0700365static const arg_def_t kf_min_dist =
366 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
367static const arg_def_t kf_max_dist =
368 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
369static const arg_def_t kf_disabled =
370 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
371static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
372 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400373
clang-format6c4d83e2016-08-08 19:03:30 -0700374static const arg_def_t noise_sens =
375 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
376static const arg_def_t sharpness =
377 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
378static const arg_def_t static_thresh =
379 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
380static const arg_def_t auto_altref =
381 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
382static const arg_def_t arnr_maxframes =
383 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
384static const arg_def_t arnr_strength =
385 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500386static const struct arg_enum_list tuning_enum[] = {
Yushin Chod808bfc2017-08-10 15:54:36 -0700387 { "psnr", AOM_TUNE_PSNR },
388 { "ssim", AOM_TUNE_SSIM },
389#ifdef CONFIG_DIST_8X8
390 { "cdef-dist", AOM_TUNE_CDEF_DIST },
391 { "daala-dist", AOM_TUNE_DAALA_DIST },
392#endif
393 { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500394};
Yushin Chod808bfc2017-08-10 15:54:36 -0700395static const arg_def_t tune_metric =
396 ARG_DEF_ENUM(NULL, "tune", 1, "Distortion metric tuned with", tuning_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700397static const arg_def_t cq_level =
398 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
399static const arg_def_t max_intra_rate_pct =
400 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800401
Yaowu Xuf883b422016-08-30 14:01:10 -0700402#if CONFIG_AV1_ENCODER
403static const arg_def_t cpu_used_av1 =
Yaowu Xu39737592017-04-21 16:39:09 -0700404 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700405#if CONFIG_EXT_TILE
406static const arg_def_t single_tile_decoding =
407 ARG_DEF(NULL, "single-tile-decoding", 1,
408 "Single tile decoding (0: off (default), 1: on)");
409#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700410static const arg_def_t tile_cols =
411 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
412static const arg_def_t tile_rows =
413 ARG_DEF(NULL, "tile-rows", 1,
414 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200415#if CONFIG_MAX_TILE
416static const arg_def_t tile_width =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200417 ARG_DEF(NULL, "tile-width", 1, "Tile widths (comma separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200418static const arg_def_t tile_height =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200419 ARG_DEF(NULL, "tile-height", 1, "Tile heights (command separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200420#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800421#if CONFIG_DEPENDENT_HORZTILES
422static const arg_def_t tile_dependent_rows =
423 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
424#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800425#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800426static const arg_def_t tile_loopfilter = ARG_DEF(
427 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800428#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700429static const arg_def_t lossless =
430 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100431#if CONFIG_CDEF
432static const arg_def_t enable_cdef =
433 ARG_DEF(NULL, "enable-cdef", 1,
434 "Enable the constrained directional enhancement filter (0: false, "
435 "1: true (default))");
436#endif
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700437#if CONFIG_AOM_QM
438static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800439 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700440 "Enable quantisation matrices (0: false (default), 1: true)");
441static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800442 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700443static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800444 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700445#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700446#if CONFIG_DIST_8X8
447static const arg_def_t enable_dist_8x8 =
448 ARG_DEF(NULL, "enable-dist-8x8", 1,
449 "Enable dist-8x8 (0: false (default), 1: true)");
450#endif // CONFIG_DIST_8X8
Yaowu Xu859a5272016-11-10 15:32:21 -0800451static const arg_def_t num_tg = ARG_DEF(
452 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000453static const arg_def_t mtu_size =
454 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800455 "MTU size for a tile group, default is 0 (no MTU targeting), "
456 "overrides maximum number of tile groups");
Fangwen Fu8d164de2016-12-14 13:40:54 -0800457#if CONFIG_TEMPMV_SIGNALING
458static const arg_def_t disable_tempmv = ARG_DEF(
459 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
460#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700461static const arg_def_t frame_parallel_decoding =
462 ARG_DEF(NULL, "frame-parallel", 1,
463 "Enable frame parallel decodability features "
464 "(0: false (default), 1: true)");
Thomas Davies3ab20b42017-09-19 10:30:53 +0100465#if !CONFIG_EXT_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100466static const arg_def_t aq_mode = ARG_DEF(
467 NULL, "aq-mode", 1,
468 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100469 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200470#else
Thomase28d92b2016-09-20 12:07:11 +0100471static const arg_def_t aq_mode = ARG_DEF(
472 NULL, "aq-mode", 1,
473 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100474 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200475#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700476#if CONFIG_EXT_DELTA_Q
477static const arg_def_t deltaq_mode = ARG_DEF(
478 NULL, "deltaq-mode", 1,
479 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
480#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700481static const arg_def_t frame_periodic_boost =
482 ARG_DEF(NULL, "frame-boost", 1,
483 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700484static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
485 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700486static const arg_def_t max_inter_rate_pct =
487 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700488static const arg_def_t min_gf_interval = ARG_DEF(
489 NULL, "min-gf-interval", 1,
490 "min gf/arf frame interval (default 0, indicating in-built behavior)");
491static const arg_def_t max_gf_interval = ARG_DEF(
492 NULL, "max-gf-interval", 1,
493 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800494
Yaowu Xufc996362015-02-10 15:03:05 -0800495static const struct arg_enum_list color_space_enum[] = {
Tom Finegan01d43e12017-08-17 09:21:42 -0700496 { "unknown", AOM_CS_UNKNOWN }, { "bt601", AOM_CS_BT_601 },
497 { "bt709", AOM_CS_BT_709 }, { "smpte170", AOM_CS_SMPTE_170 },
498 { "smpte240", AOM_CS_SMPTE_240 }, { "bt2020ncl", AOM_CS_BT_2020_NCL },
499 { "bt2020cl", AOM_CS_BT_2020_CL }, { "sRGB", AOM_CS_SRGB },
500 { "ICtCp", AOM_CS_ICTCP }, { NULL, 0 }
Yaowu Xufc996362015-02-10 15:03:05 -0800501};
502
clang-format6c4d83e2016-08-08 19:03:30 -0700503static const arg_def_t input_color_space =
clang-format4eafefe2017-09-04 12:51:20 -0700504 ARG_DEF_ENUM(NULL, "color-space", 1,
505 "The color space of input content:", color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800506
anorkin76fb1262017-03-22 15:12:12 -0700507static const struct arg_enum_list transfer_function_enum[] = {
508 { "unknown", AOM_TF_UNKNOWN },
509 { "bt709", AOM_TF_BT_709 },
510 { "pq", AOM_TF_PQ },
511 { "hlg", AOM_TF_HLG },
512 { NULL, 0 }
513};
514
515static const arg_def_t input_transfer_function = ARG_DEF_ENUM(
clang-format4eafefe2017-09-04 12:51:20 -0700516 NULL, "transfer-function", 1,
517 "The transfer function of input content:", transfer_function_enum);
anorkin76fb1262017-03-22 15:12:12 -0700518
519static const struct arg_enum_list chroma_sample_position_enum[] = {
520 { "unknown", AOM_CSP_UNKNOWN },
521 { "vertical", AOM_CSP_VERTICAL },
522 { "colocated", AOM_CSP_COLOCATED },
523 { NULL, 0 }
524};
525
526static const arg_def_t input_chroma_sample_position =
527 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
528 "The chroma sample position when chroma 4:2:0 is signaled:",
529 chroma_sample_position_enum);
anorkin76fb1262017-03-22 15:12:12 -0700530
Alex Converse219f6452014-08-06 11:01:18 -0700531static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700532 { "default", AOM_CONTENT_DEFAULT },
533 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700534 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700535};
536
537static const arg_def_t tune_content = ARG_DEF_ENUM(
538 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700539#endif
Alex Converse219f6452014-08-06 11:01:18 -0700540
Yaowu Xuf883b422016-08-30 14:01:10 -0700541#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000542#if CONFIG_EXT_PARTITION
543static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700544 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
545 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
546 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700547 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000548};
549static const arg_def_t superblock_size = ARG_DEF_ENUM(
550 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
551#endif // CONFIG_EXT_PARTITION
552
Yaowu Xuf883b422016-08-30 14:01:10 -0700553static const arg_def_t *av1_args[] = { &cpu_used_av1,
554 &auto_altref,
555 &sharpness,
556 &static_thresh,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700557#if CONFIG_EXT_TILE
558 &single_tile_decoding,
559#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700560 &tile_cols,
561 &tile_rows,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800562#if CONFIG_DEPENDENT_HORZTILES
563 &tile_dependent_rows,
564#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800565#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800566 &tile_loopfilter,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800567#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700568 &arnr_maxframes,
569 &arnr_strength,
Yushin Chod808bfc2017-08-10 15:54:36 -0700570 &tune_metric,
Yaowu Xuf883b422016-08-30 14:01:10 -0700571 &cq_level,
572 &max_intra_rate_pct,
573 &max_inter_rate_pct,
574 &gf_cbr_boost_pct,
575 &lossless,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100576#if CONFIG_CDEF
577 &enable_cdef,
578#endif
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800579#if CONFIG_AOM_QM
580 &enable_qm,
581 &qm_min,
582 &qm_max,
583#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700584#if CONFIG_DIST_8X8
585 &enable_dist_8x8,
586#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700587 &frame_parallel_decoding,
588 &aq_mode,
Fangwen Fu6160df22017-04-24 09:45:51 -0700589#if CONFIG_EXT_DELTA_Q
590 &deltaq_mode,
591#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700592 &frame_periodic_boost,
593 &noise_sens,
594 &tune_content,
595 &input_color_space,
anorkin76fb1262017-03-22 15:12:12 -0700596 &input_transfer_function,
597 &input_chroma_sample_position,
Yaowu Xuf883b422016-08-30 14:01:10 -0700598 &min_gf_interval,
599 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000600#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700601 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000602#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000603 &num_tg,
604 &mtu_size,
Fangwen Fu8d164de2016-12-14 13:40:54 -0800605#if CONFIG_TEMPMV_SIGNALING
606 &disable_tempmv,
607#endif
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200608#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700609 &bitdeptharg,
610 &inbitdeptharg,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200611#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700612 NULL };
613static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
614 AOME_SET_ENABLEAUTOALTREF,
615 AOME_SET_SHARPNESS,
616 AOME_SET_STATIC_THRESHOLD,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700617#if CONFIG_EXT_TILE
618 AV1E_SET_SINGLE_TILE_DECODING,
619#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700620 AV1E_SET_TILE_COLUMNS,
621 AV1E_SET_TILE_ROWS,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800622#if CONFIG_DEPENDENT_HORZTILES
623 AV1E_SET_TILE_DEPENDENT_ROWS,
624#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800625#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800626 AV1E_SET_TILE_LOOPFILTER,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800627#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700628 AOME_SET_ARNR_MAXFRAMES,
629 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700630 AOME_SET_TUNING,
631 AOME_SET_CQ_LEVEL,
632 AOME_SET_MAX_INTRA_BITRATE_PCT,
633 AV1E_SET_MAX_INTER_BITRATE_PCT,
634 AV1E_SET_GF_CBR_BOOST_PCT,
635 AV1E_SET_LOSSLESS,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100636#if CONFIG_CDEF
637 AV1E_SET_ENABLE_CDEF,
638#endif
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800639#if CONFIG_AOM_QM
640 AV1E_SET_ENABLE_QM,
641 AV1E_SET_QM_MIN,
642 AV1E_SET_QM_MAX,
643#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700644#if CONFIG_DIST_8X8
645 AV1E_SET_ENABLE_DIST_8X8,
646#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700647 AV1E_SET_FRAME_PARALLEL_DECODING,
648 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700649#if CONFIG_EXT_DELTA_Q
650 AV1E_SET_DELTAQ_MODE,
651#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700652 AV1E_SET_FRAME_PERIODIC_BOOST,
653 AV1E_SET_NOISE_SENSITIVITY,
654 AV1E_SET_TUNE_CONTENT,
655 AV1E_SET_COLOR_SPACE,
anorkin76fb1262017-03-22 15:12:12 -0700656 AV1E_SET_TRANSFER_FUNCTION,
657 AV1E_SET_CHROMA_SAMPLE_POSITION,
Yaowu Xuf883b422016-08-30 14:01:10 -0700658 AV1E_SET_MIN_GF_INTERVAL,
659 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000660#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700661 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000662#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000663 AV1E_SET_NUM_TG,
664 AV1E_SET_MTU,
Fangwen Fu8d164de2016-12-14 13:40:54 -0800665#if CONFIG_TEMPMV_SIGNALING
666 AV1E_SET_DISABLE_TEMPMV,
667#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700668 0 };
hui su82331e02015-08-16 18:21:56 -0700669#endif
670
John Koleszar0ea50ce2010-05-18 11:58:33 -0400671static const arg_def_t *no_args[] = { NULL };
672
James Zern59e7a472015-05-09 10:33:26 -0700673void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700674 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700675 const int num_encoder = get_aom_encoder_count();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400676
John Koleszarc6b90392012-07-13 15:21:29 -0700677 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
678 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400679
John Koleszarc6b90392012-07-13 15:21:29 -0700680 fprintf(stderr, "\nOptions:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700681 arg_show_usage(stderr, main_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700682 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700683 arg_show_usage(stderr, global_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700684 fprintf(stderr, "\nRate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700685 arg_show_usage(stderr, rc_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700686 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700687 arg_show_usage(stderr, rc_twopass_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700688 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700689 arg_show_usage(stderr, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700690#if CONFIG_AV1_ENCODER
691 fprintf(stderr, "\nAV1 Specific Options:\n");
692 arg_show_usage(stderr, av1_args);
hui su82331e02015-08-16 18:21:56 -0700693#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700694 fprintf(stderr,
695 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700696 " The desired precision of timestamps in the output, expressed\n"
697 " in fractional seconds. Default is 1/1000.\n");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800698 fprintf(stderr, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400699
Yaowu Xuf990b352015-06-01 09:13:59 -0700700 for (i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700701 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700702 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
703 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700704 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800705 }
Yaowu Xuf990b352015-06-01 09:13:59 -0700706 fprintf(stderr, "\n ");
707 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400708
John Koleszarc6b90392012-07-13 15:21:29 -0700709 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400710}
711
Yaowu Xuf883b422016-08-30 14:01:10 -0700712#if CONFIG_AV1_ENCODER
713#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800714#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800715
James Zerndba73762014-05-10 17:44:12 -0700716#if !CONFIG_WEBM_IO
717typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700718struct WebmOutputContext {
719 int debug;
720};
James Zerndba73762014-05-10 17:44:12 -0700721#endif
722
John Koleszar9e50ed72012-02-15 12:39:38 -0800723/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800724struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700725 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700726 const char *out_fn;
727 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700728#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700729 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700730#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700731 stereo_format_t stereo_fmt;
732 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
733 int arg_ctrl_cnt;
734 int write_webm;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700735 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700736 int use_16bit_internal;
John Koleszar9e50ed72012-02-15 12:39:38 -0800737};
738
John Koleszar6ad3b742012-11-06 12:02:42 -0800739struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700740 int index;
741 struct stream_state *next;
742 struct stream_config config;
743 FILE *file;
744 struct rate_hist *rate_hist;
745 struct WebmOutputContext webm_ctx;
746 uint64_t psnr_sse_total;
747 uint64_t psnr_samples_total;
748 double psnr_totals[4];
749 int psnr_count;
750 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700752 unsigned int frames_out;
753 uint64_t cx_time;
754 size_t nbytes;
755 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700756#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700757 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700758#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700759 struct aom_image *img;
760 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700761 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800762};
763
clang-format6c4d83e2016-08-08 19:03:30 -0700764static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700765 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800766 if (rat->den < 0) {
767 rat->num *= -1;
768 rat->den *= -1;
769 }
John Koleszar1b27e932012-04-25 17:08:56 -0700770
clang-format6c4d83e2016-08-08 19:03:30 -0700771 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700772
clang-format6c4d83e2016-08-08 19:03:30 -0700773 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700774}
775
Yaowu Xuf883b422016-08-30 14:01:10 -0700776static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700777 char **argi, **argj;
778 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700779 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700780
clang-format6c4d83e2016-08-08 19:03:30 -0700781 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800782
John Koleszar6ad3b742012-11-06 12:02:42 -0800783 /* Initialize default parameters */
784 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700785 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700786 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700787 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700788 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400790
John Koleszarc6b90392012-07-13 15:21:29 -0700791 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
792 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400793
John Koleszarc6b90392012-07-13 15:21:29 -0700794 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700795 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800796 if (!global->codec)
797 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700798 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800799 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400800
John Koleszar6ad3b742012-11-06 12:02:42 -0800801 if (global->passes < 1 || global->passes > 2)
802 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700803 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800804 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400805
John Koleszar6ad3b742012-11-06 12:02:42 -0800806 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700807 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800808 } else if (arg_match(&arg, &usage, argi))
809 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700810 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800811 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700812 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700813 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar6ad3b742012-11-06 12:02:42 -0800814 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700815 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800816 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700817 global->color_type = I420;
818 else if (arg_match(&arg, &use_i422, argi))
819 global->color_type = I422;
820 else if (arg_match(&arg, &use_i444, argi))
821 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700822 else if (arg_match(&arg, &use_i440, argi))
823 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800824 else if (arg_match(&arg, &quietarg, argi))
825 global->quiet = 1;
826 else if (arg_match(&arg, &verbosearg, argi))
827 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700828 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800829 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700830 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800831 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700832 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800833 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700834 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800835 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700836 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800837 global->framerate = arg_parse_rational(&arg);
838 validate_positive_rational(arg.name, &global->framerate);
839 global->have_framerate = 1;
840 } else if (arg_match(&arg, &out_part, argi))
841 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700842 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800843 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700844 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800845 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700846 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800847 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800848 else if (arg_match(&arg, &disable_warnings, argi))
849 global->disable_warnings = 1;
850 else if (arg_match(&arg, &disable_warning_prompt, argi))
851 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800852 else
John Koleszarc6b90392012-07-13 15:21:29 -0700853 argj++;
854 }
855
John Koleszar6ad3b742012-11-06 12:02:42 -0800856 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700857 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800858 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700859 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
860 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800861 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800862 }
John Koleszarc6b90392012-07-13 15:21:29 -0700863 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800864 /* Validate global config */
865 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700866#if CONFIG_AV1_ENCODER
867 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800868 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700869 if (global->codec != NULL && global->codec->name != NULL)
Thomas Daede80826142017-03-20 15:44:24 -0700870 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800871#else
872 global->passes = 1;
873#endif
874 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800875}
876
Yaowu Xuf883b422016-08-30 14:01:10 -0700877static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800878 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700879 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
880 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -0800881
clang-format6c4d83e2016-08-08 19:03:30 -0700882 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -0800883
John Koleszar25b6e9f2013-02-12 21:17:56 -0800884 if (!fseeko(input->file, 0, SEEK_END)) {
885 /* Input file is seekable. Figure out how long it is, so we can get
886 * progress info.
887 */
888 input->length = ftello(input->file);
889 rewind(input->file);
890 }
891
Frank Galligan09acd262015-06-01 10:20:58 -0700892 /* Default to 1:1 pixel aspect ratio. */
893 input->pixel_aspect_ratio.numerator = 1;
894 input->pixel_aspect_ratio.denominator = 1;
895
John Koleszar6ad3b742012-11-06 12:02:42 -0800896 /* For RAW input sources, these bytes will applied on the first frame
897 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -0700898 */
John Koleszar6ad3b742012-11-06 12:02:42 -0800899 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
900 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400901
clang-format6c4d83e2016-08-08 19:03:30 -0700902 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -0700903 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
904 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800905 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800906 input->width = input->y4m.pic_w;
907 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -0700908 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
909 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800910 input->framerate.numerator = input->y4m.fps_n;
911 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -0700912 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -0700913 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -0800914 } else
915 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -0800916 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -0800917 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -0800918 } else {
919 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700920 }
John Koleszar6ad3b742012-11-06 12:02:42 -0800921}
922
Yaowu Xuf883b422016-08-30 14:01:10 -0700923static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800924 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -0700925 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -0800926}
927
Yaowu Xuf883b422016-08-30 14:01:10 -0700928static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -0800929 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800930 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800931
John Koleszar6ad3b742012-11-06 12:02:42 -0800932 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700933 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800934 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700935 }
936
John Koleszar6ad3b742012-11-06 12:02:42 -0800937 if (prev) {
938 memcpy(stream, prev, sizeof(*stream));
939 stream->index++;
940 prev->next = stream;
941 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700942 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -0800943
John Koleszar6ad3b742012-11-06 12:02:42 -0800944 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -0700945 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -0700946 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -0700947 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -0800948
John Koleszar6ad3b742012-11-06 12:02:42 -0800949 /* Change the default timebase to a high enough value so that the
950 * encoder will always create strictly increasing timestamps.
951 */
952 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -0800953
John Koleszar6ad3b742012-11-06 12:02:42 -0800954 /* Never use the library's default resolution, require it be parsed
955 * from the file or set on the command line.
956 */
957 stream->config.cfg.g_w = 0;
958 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800959
John Koleszar6ad3b742012-11-06 12:02:42 -0800960 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -0800961 stream->config.write_webm = 1;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700962#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -0700963 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700964 stream->webm_ctx.last_pts_ns = -1;
965 stream->webm_ctx.writer = NULL;
966 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700967#endif
Johann87c40b32012-03-01 16:12:53 -0800968
John Koleszar6ad3b742012-11-06 12:02:42 -0800969 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700970 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -0800971 }
John Koleszar9e50ed72012-02-15 12:39:38 -0800972
John Koleszar6ad3b742012-11-06 12:02:42 -0800973 /* Output files must be specified for each stream */
974 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -0800975
John Koleszar6ad3b742012-11-06 12:02:42 -0800976 stream->next = NULL;
977 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800978}
979
Yaowu Xuf883b422016-08-30 14:01:10 -0700980static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -0700981 struct stream_state *stream, char **argv) {
982 char **argi, **argj;
983 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -0800984 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -0700985 static const int *ctrl_args_map = NULL;
986 struct stream_config *config = &stream->config;
987 int eos_mark_found = 0;
Tristan Matthewsed858d62017-07-24 15:33:44 -0400988 int webm_forced = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800989
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800990 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -0800991 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700992#if CONFIG_AV1_ENCODER
993 } else if (strcmp(global->codec->name, "av1") == 0) {
994 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
995 // Consider to expand this set for AV1 encoder control.
996 ctrl_args = av1_args;
997 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -0700998#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700999 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001000
John Koleszarc6b90392012-07-13 15:21:29 -07001001 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001002 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001003
John Koleszar6ad3b742012-11-06 12:02:42 -08001004 /* Once we've found an end-of-stream marker (--) we want to continue
1005 * shifting arguments but not consuming them.
1006 */
1007 if (eos_mark_found) {
1008 argj++;
1009 continue;
1010 } else if (!strcmp(*argj, "--")) {
1011 eos_mark_found = 1;
1012 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001013 }
1014
Adrian Granged2401802015-02-13 14:51:32 -08001015 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001016 config->out_fn = arg.val;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001017 if (!webm_forced) {
1018 const size_t out_fn_len = strlen(config->out_fn);
1019 if (out_fn_len >= 4 &&
1020 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
1021 config->write_webm = 0;
1022 }
1023 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001024 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001025 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001026#if CONFIG_FP_MB_STATS
1027 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1028 config->fpmb_stats_fn = arg.val;
1029#endif
Johannb50e5182015-01-30 15:05:14 -08001030 } else if (arg_match(&arg, &use_webm, argi)) {
1031#if CONFIG_WEBM_IO
1032 config->write_webm = 1;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001033 webm_forced = 1;
Johannb50e5182015-01-30 15:05:14 -08001034#else
1035 die("Error: --webm specified but webm is disabled.");
1036#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001037 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001038 config->write_webm = 0;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001039 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001040 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001041 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001042 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001043 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001044 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001045 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001046 config->cfg.g_h = arg_parse_uint(&arg);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001047#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001048 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1049 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1050 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1051 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1052#endif
James Zerndba73762014-05-10 17:44:12 -07001053#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001054 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001055 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001056#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001057 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001058 config->cfg.g_timebase = arg_parse_rational(&arg);
1059 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001060 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001061 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001062 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001063 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001064#if CONFIG_EXT_TILE
1065 } else if (arg_match(&arg, &large_scale_tile, argi)) {
1066 config->cfg.large_scale_tile = arg_parse_uint(&arg);
1067#endif // CONFIG_EXT_TILE
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001068 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001069 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001070 } else if (arg_match(&arg, &resize_mode, argi)) {
1071 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001072 } else if (arg_match(&arg, &resize_denominator, argi)) {
1073 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1074 } else if (arg_match(&arg, &resize_kf_denominator, argi)) {
1075 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001076#if CONFIG_FRAME_SUPERRES
1077 } else if (arg_match(&arg, &superres_mode, argi)) {
1078 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001079 } else if (arg_match(&arg, &superres_denominator, argi)) {
1080 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1081 } else if (arg_match(&arg, &superres_kf_denominator, argi)) {
1082 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001083 } else if (arg_match(&arg, &superres_qthresh, argi)) {
1084 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1085 } else if (arg_match(&arg, &superres_kf_qthresh, argi)) {
1086 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001087#endif // CONFIG_FRAME_SUPERRES
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001088 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001089 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001090 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001091 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001092 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001093 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001094 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001095 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001096 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001097 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001098 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001099 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001100 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001101 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001102 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001103 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001104 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001105 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001106 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001107 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001108 if (global->passes < 2)
1109 warn("option %s ignored in one-pass mode.\n", arg.name);
1110 } else if (arg_match(&arg, &minsection_pct, argi)) {
1111 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1112
1113 if (global->passes < 2)
1114 warn("option %s ignored in one-pass mode.\n", arg.name);
1115 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1116 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1117
1118 if (global->passes < 2)
1119 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001120 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001121 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001122 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001123 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001124 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001125 config->cfg.kf_mode = AOM_KF_DISABLED;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001126#if CONFIG_MAX_TILE
1127 } else if (arg_match(&arg, &tile_width, argi)) {
1128 config->cfg.tile_width_count =
1129 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1130 } else if (arg_match(&arg, &tile_height, argi)) {
1131 config->cfg.tile_height_count =
1132 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1133#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001134 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001135 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001136 for (i = 0; ctrl_args[i]; i++) {
1137 if (arg_match(&arg, ctrl_args[i], argi)) {
1138 int j;
1139 match = 1;
1140
1141 /* Point either to the next free element or the first
1142 * instance of this control.
1143 */
1144 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001145 if (ctrl_args_map != NULL &&
1146 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001147 break;
1148
1149 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001150 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001151 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001152 config->arg_ctrls[j][0] = ctrl_args_map[i];
1153 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001154 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001155 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001156 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001157 }
clang-format6c4d83e2016-08-08 19:03:30 -07001158 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001159 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001160 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001161#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan14af5b92017-05-12 14:27:30 +02001162 config->use_16bit_internal =
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +02001163 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001164#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001165 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001166}
1167
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001168#define FOREACH_STREAM(iterator, list) \
1169 for (struct stream_state *iterator = list; iterator; \
1170 iterator = iterator->next)
John Koleszar9e50ed72012-02-15 12:39:38 -08001171
Alex Conversed66bd222014-02-21 10:52:09 -08001172static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001173 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001174 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001175 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001176
John Koleszar6ad3b742012-11-06 12:02:42 -08001177 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001178 fatal(
1179 "Stream %d: Specify stream dimensions with --width (-w) "
1180 " and --height (-h)",
1181 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001182
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001183 // Check that the codec bit depth is greater than the input bit depth.
1184 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001185 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001186 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1187 stream->index, (int)stream->config.cfg.g_bit_depth,
1188 stream->config.cfg.g_input_bit_depth);
1189 }
1190
John Koleszar6ad3b742012-11-06 12:02:42 -08001191 for (streami = stream; streami; streami = streami->next) {
1192 /* All streams require output files */
1193 if (!streami->config.out_fn)
1194 fatal("Stream %d: Output file is required (specify with -o)",
1195 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001196
John Koleszar6ad3b742012-11-06 12:02:42 -08001197 /* Check for two streams outputting to the same file */
1198 if (streami != stream) {
1199 const char *a = stream->config.out_fn;
1200 const char *b = streami->config.out_fn;
1201 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1202 fatal("Stream %d: duplicate output file (from stream %d)",
1203 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001204 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001205
1206 /* Check for two streams sharing a stats file. */
1207 if (streami != stream) {
1208 const char *a = stream->config.stats_fn;
1209 const char *b = streami->config.stats_fn;
1210 if (a && b && !strcmp(a, b))
1211 fatal("Stream %d: duplicate stats file (from stream %d)",
1212 streami->index, stream->index);
1213 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001214
1215#if CONFIG_FP_MB_STATS
1216 /* Check for two streams sharing a mb stats file. */
1217 if (streami != stream) {
1218 const char *a = stream->config.fpmb_stats_fn;
1219 const char *b = streami->config.fpmb_stats_fn;
1220 if (a && b && !strcmp(a, b))
1221 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1222 streami->index, stream->index);
1223 }
1224#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001225 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001226}
1227
clang-format6c4d83e2016-08-08 19:03:30 -07001228static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001229 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001230 if (!stream->config.cfg.g_w) {
1231 if (!stream->config.cfg.g_h)
1232 stream->config.cfg.g_w = w;
1233 else
1234 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1235 }
1236 if (!stream->config.cfg.g_h) {
1237 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1238 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001239}
1240
clang-format6c4d83e2016-08-08 19:03:30 -07001241static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001242 switch (t) {
1243 case FILE_TYPE_RAW: return "RAW";
1244 case FILE_TYPE_Y4M: return "Y4M";
1245 default: return "Other";
1246 }
1247}
1248
Yaowu Xuf883b422016-08-30 14:01:10 -07001249static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001250 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001251 case AOM_IMG_FMT_I420: return "I420";
1252 case AOM_IMG_FMT_I422: return "I422";
1253 case AOM_IMG_FMT_I444: return "I444";
1254 case AOM_IMG_FMT_I440: return "I440";
1255 case AOM_IMG_FMT_YV12: return "YV12";
1256 case AOM_IMG_FMT_I42016: return "I42016";
1257 case AOM_IMG_FMT_I42216: return "I42216";
1258 case AOM_IMG_FMT_I44416: return "I44416";
1259 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001260 default: return "Other";
1261 }
1262}
Ralph Giles061a16d2012-01-05 15:05:05 -06001263
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001264static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001265 struct AvxEncoderConfig *global,
1266 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001267#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001268 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001269
John Koleszar6ad3b742012-11-06 12:02:42 -08001270 if (stream->index == 0) {
1271 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001272 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001273 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001274 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001275 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001276 }
1277 if (stream->next || stream->index)
1278 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1279 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
Sebastien Alaiwan27511922017-06-05 15:25:46 +02001280 fprintf(stderr, "Coding path: %s\n",
1281 stream->config.use_16bit_internal ? "HBD" : "LBD");
John Koleszar6ad3b742012-11-06 12:02:42 -08001282 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001283
John Koleszar6ad3b742012-11-06 12:02:42 -08001284 SHOW(g_usage);
1285 SHOW(g_threads);
1286 SHOW(g_profile);
1287 SHOW(g_w);
1288 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001289 SHOW(g_bit_depth);
1290 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001291 SHOW(g_timebase.num);
1292 SHOW(g_timebase.den);
1293 SHOW(g_error_resilient);
1294 SHOW(g_pass);
1295 SHOW(g_lag_in_frames);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001296#if CONFIG_EXT_TILE
1297 SHOW(large_scale_tile);
1298#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001299 SHOW(rc_dropframe_thresh);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001300 SHOW(rc_resize_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001301 SHOW(rc_resize_denominator);
1302 SHOW(rc_resize_kf_denominator);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001303#if CONFIG_FRAME_SUPERRES
1304 SHOW(rc_superres_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001305 SHOW(rc_superres_denominator);
1306 SHOW(rc_superres_kf_denominator);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001307 SHOW(rc_superres_qthresh);
1308 SHOW(rc_superres_kf_qthresh);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001309#endif // CONFIG_FRAME_SUPERRES
John Koleszar6ad3b742012-11-06 12:02:42 -08001310 SHOW(rc_end_usage);
1311 SHOW(rc_target_bitrate);
1312 SHOW(rc_min_quantizer);
1313 SHOW(rc_max_quantizer);
1314 SHOW(rc_undershoot_pct);
1315 SHOW(rc_overshoot_pct);
1316 SHOW(rc_buf_sz);
1317 SHOW(rc_buf_initial_sz);
1318 SHOW(rc_buf_optimal_sz);
1319 SHOW(rc_2pass_vbr_bias_pct);
1320 SHOW(rc_2pass_vbr_minsection_pct);
1321 SHOW(rc_2pass_vbr_maxsection_pct);
1322 SHOW(kf_mode);
1323 SHOW(kf_min_dist);
1324 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001325}
1326
John Koleszar9e50ed72012-02-15 12:39:38 -08001327static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001328 struct AvxEncoderConfig *global,
1329 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001330 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001331 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001332
Yaowu Xuf883b422016-08-30 14:01:10 -07001333 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001334
John Koleszar6ad3b742012-11-06 12:02:42 -08001335 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001336
clang-format6c4d83e2016-08-08 19:03:30 -07001337 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001338
John Koleszar6ad3b742012-11-06 12:02:42 -08001339 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1340 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001341
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001342#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001343 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001344 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001345 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1346 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001347 }
James Zernc8e5a772016-02-11 18:53:50 -08001348#else
1349 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001350#endif
1351
1352 if (!stream->config.write_webm) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001353 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1354 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001355}
1356
John Koleszar9e50ed72012-02-15 12:39:38 -08001357static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001358 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001359 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001360
Yaowu Xuf883b422016-08-30 14:01:10 -07001361 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001362
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001363#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001364 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001365 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001366 }
1367#endif
1368
1369 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001370 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001371 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001372 stream->frames_out);
1373 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001374
John Koleszar6ad3b742012-11-06 12:02:42 -08001375 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001376}
1377
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001378static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001379 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001380 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001381 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001382 fatal("Failed to open statistics store");
1383 } else {
1384 if (!stats_open_mem(&stream->stats, pass))
1385 fatal("Failed to open statistics store");
1386 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001387
Pengchong Jinf349b072014-07-14 09:13:38 -07001388#if CONFIG_FP_MB_STATS
1389 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001390 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1391 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001392 fatal("Failed to open mb statistics store");
1393 } else {
1394 if (!stats_open_mem(&stream->fpmb_stats, pass))
1395 fatal("Failed to open mb statistics store");
1396 }
1397#endif
1398
John Koleszar6ad3b742012-11-06 12:02:42 -08001399 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001400 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1401 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001402 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001403 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001404#if CONFIG_FP_MB_STATS
1405 stream->config.cfg.rc_firstpass_mb_stats_in =
1406 stats_get(&stream->fpmb_stats);
1407#endif
1408 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001409
John Koleszar6ad3b742012-11-06 12:02:42 -08001410 stream->cx_time = 0;
1411 stream->nbytes = 0;
1412 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001413}
1414
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001415static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001416 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001417 int i;
1418 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001419
Yaowu Xuf883b422016-08-30 14:01:10 -07001420 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1421 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001422#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001423 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001424#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001425
John Koleszar6ad3b742012-11-06 12:02:42 -08001426 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001427 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001428 &stream->config.cfg, flags);
1429 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001430
Yaowu Xuf883b422016-08-30 14:01:10 -07001431 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001432 * we're being clever to store the control IDs in an array. Real
1433 * applications will want to make use of the enumerations directly
1434 */
1435 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1436 int ctrl = stream->config.arg_ctrls[i][0];
1437 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001438 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001439 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001440
John Koleszar6ad3b742012-11-06 12:02:42 -08001441 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1442 }
1443
Tom Fineganba02c242017-05-16 15:01:54 -07001444#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001445 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001446 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +02001447 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH };
Yaowu Xuf883b422016-08-30 14:01:10 -07001448 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001449
Tom Fineganba02c242017-05-16 15:01:54 -07001450#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07001451 if (strcmp(global->codec->name, "av1") == 0) {
1452 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001453 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1454
Yaowu Xuf883b422016-08-30 14:01:10 -07001455 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001456 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1457 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001458#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001459 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001460#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001461}
1462
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001463static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001464 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001465 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001466 aom_codec_pts_t frame_start, next_frame_start;
1467 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1468 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001469
clang-format6c4d83e2016-08-08 19:03:30 -07001470 frame_start =
1471 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1472 cfg->g_timebase.num / global->framerate.num;
1473 next_frame_start =
1474 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1475 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001476
clang-format6c4d83e2016-08-08 19:03:30 -07001477/* Scale if necessary */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001478#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001479 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001480 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001481 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001482 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001483 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1484 exit(EXIT_FAILURE);
1485 }
1486#if CONFIG_LIBYUV
1487 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001488 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001489 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001490 }
clang-format6c4d83e2016-08-08 19:03:30 -07001491 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001492 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1493 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1494 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1495 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1496 stream->img->stride[AOM_PLANE_Y] / 2,
1497 (uint16 *)stream->img->planes[AOM_PLANE_U],
1498 stream->img->stride[AOM_PLANE_U] / 2,
1499 (uint16 *)stream->img->planes[AOM_PLANE_V],
1500 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001501 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001502 img = stream->img;
1503#else
clang-format6c4d83e2016-08-08 19:03:30 -07001504 stream->encoder.err = 1;
1505 ctx_exit_on_error(&stream->encoder,
1506 "Stream %d: Failed to encode frame.\n"
1507 "Scaling disabled in this configuration. \n"
1508 "To enable, configure with --enable-libyuv\n",
1509 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001510#endif
1511 }
1512 }
1513#endif
John Koleszar34882b92012-03-01 12:50:40 -08001514 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001515 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001516 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1517 exit(EXIT_FAILURE);
1518 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001519#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001520 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001521 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001522 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001523 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001524 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1525 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1526 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1527 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1528 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1529 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001530 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001531 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001532#else
1533 stream->encoder.err = 1;
1534 ctx_exit_on_error(&stream->encoder,
1535 "Stream %d: Failed to encode frame.\n"
1536 "Scaling disabled in this configuration. \n"
1537 "To enable, configure with --enable-libyuv\n",
1538 stream->index);
1539#endif
John Koleszar34882b92012-03-01 12:50:40 -08001540 }
1541
Yaowu Xuf883b422016-08-30 14:01:10 -07001542 aom_usec_timer_start(&timer);
1543 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001544 (unsigned long)(next_frame_start - frame_start), 0,
1545 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001546 aom_usec_timer_mark(&timer);
1547 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001548 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1549 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001550}
1551
John Koleszar6ad3b742012-11-06 12:02:42 -08001552static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001553 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001554 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001555
Yaowu Xuf883b422016-08-30 14:01:10 -07001556 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001557 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1558 stream->counts[q]++;
1559 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001560}
1561
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001562static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001563 struct AvxEncoderConfig *global, int *got_data) {
1564 const aom_codec_cx_pkt_t *pkt;
1565 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1566 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001567
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001568 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001569 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001570 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001571 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001572
John Koleszar6ad3b742012-11-06 12:02:42 -08001573 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001574 case AOM_CODEC_CX_FRAME_PKT:
1575 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001576 stream->frames_out++;
1577 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001578 if (!global->quiet)
1579 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001580
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001581 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001582#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001583 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001584 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001585 }
1586#endif
1587 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001588 if (pkt->data.frame.partition_id <= 0) {
1589 ivf_header_pos = ftello(stream->file);
1590 fsize = pkt->data.frame.sz;
1591
Dmitry Kovalev373b0f92014-01-29 17:57:21 -08001592 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001593 } else {
1594 fsize += pkt->data.frame.sz;
1595
Yaowu Xuf883b422016-08-30 14:01:10 -07001596 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
James Zernb6430362017-01-14 14:18:01 -08001597 const FileOffset currpos = ftello(stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001598 fseeko(stream->file, ivf_header_pos, SEEK_SET);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001599 ivf_write_frame_size(stream->file, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001600 fseeko(stream->file, currpos, SEEK_SET);
1601 }
1602 }
1603
clang-format6c4d83e2016-08-08 19:03:30 -07001604 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1605 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001606 }
1607 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001608
John Koleszar9e50ed72012-02-15 12:39:38 -08001609 *got_data = 1;
Tom Fineganba02c242017-05-16 15:01:54 -07001610#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001611 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001612 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001613 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001614 if (stream->decoder.err) {
1615 warn_or_exit_on_error(&stream->decoder,
1616 global->test_decode == TEST_DECODE_FATAL,
1617 "Failed to decode frame %d in stream %d",
1618 stream->frames_out + 1, stream->index);
1619 stream->mismatch_seen = stream->frames_out + 1;
1620 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001621 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001622#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001623 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001624 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001625 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001626 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001627 pkt->data.twopass_stats.sz);
1628 stream->nbytes += pkt->data.raw.sz;
1629 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001630#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001631 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001632 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001633 pkt->data.firstpass_mb_stats.sz);
1634 stream->nbytes += pkt->data.raw.sz;
1635 break;
1636#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001637 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001638
1639 if (global->show_psnr) {
1640 int i;
1641
1642 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1643 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1644 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001645 if (!global->quiet)
1646 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001647 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1648 }
1649 stream->psnr_count++;
1650 }
1651
1652 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001653 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001654 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001655 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001656}
1657
clang-format6c4d83e2016-08-08 19:03:30 -07001658static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001659 int i;
1660 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001661
clang-format6c4d83e2016-08-08 19:03:30 -07001662 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001663
John Koleszar6ad3b742012-11-06 12:02:42 -08001664 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001665 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001666 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001667 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001668
John Koleszar6ad3b742012-11-06 12:02:42 -08001669 for (i = 0; i < 4; i++) {
1670 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1671 }
1672 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001673}
1674
John Koleszar25b6e9f2013-02-12 21:17:56 -08001675static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001676 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001677}
1678
clang-format6c4d83e2016-08-08 19:03:30 -07001679static void test_decode(struct stream_state *stream,
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001680 enum TestDecodeFatality fatal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001681 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001682
clang-format6c4d83e2016-08-08 19:03:30 -07001683 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001684
John Koleszarb3c350a2013-03-13 12:15:43 -07001685 /* Get the internal reference frame */
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001686 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1687 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001688
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001689#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001690 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1691 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1692 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1693 aom_image_t enc_hbd_img;
1694 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1695 enc_img.d_w, enc_img.d_h, 16);
1696 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1697 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001698 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001699 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1700 aom_image_t dec_hbd_img;
1701 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1702 dec_img.d_w, dec_img.d_h, 16);
1703 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1704 dec_img = dec_hbd_img;
1705 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001706 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001707#endif
1708
John Koleszar6ad3b742012-11-06 12:02:42 -08001709 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001710 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001711
Urvang Joshi09c293e2017-04-20 17:56:27 -07001712 if (!aom_compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001713 int y[4], u[4], v[4];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001714#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001715 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001716 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001717 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001718 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001719 }
1720#else
Urvang Joshi09c293e2017-04-20 17:56:27 -07001721 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001722#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001723 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001724 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001725 "Stream %d: Encode/decode mismatch on frame %d at"
1726 " Y[%d, %d] {%d/%d},"
1727 " U[%d, %d] {%d/%d},"
1728 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001729 stream->index, stream->frames_out, y[0], y[1], y[2],
1730 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 -08001731 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001732 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001733
Yaowu Xuf883b422016-08-30 14:01:10 -07001734 aom_img_free(&enc_img);
1735 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001736}
John Koleszarefd54f82012-02-13 16:52:18 -08001737
John Koleszar25b6e9f2013-02-12 21:17:56 -08001738static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001739 int64_t hours;
1740 int64_t mins;
1741 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001742
1743 if (etl >= 0) {
1744 hours = etl / 3600;
1745 etl -= hours * 3600;
1746 mins = etl / 60;
1747 etl -= mins * 60;
1748 secs = etl;
1749
clang-format6c4d83e2016-08-08 19:03:30 -07001750 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1751 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001752 } else {
1753 fprintf(stderr, "[%3s unknown] ", label);
1754 }
1755}
1756
Tom Finegan44dd3272013-11-20 17:18:28 -08001757int main(int argc, const char **argv_) {
1758 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001759 aom_image_t raw;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001760#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001761 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001762 int allocated_raw_shift = 0;
1763 int use_16bit_internal = 0;
1764 int input_shift = 0;
1765#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001766 int frame_avail, got_data;
1767
Yaowu Xuf883b422016-08-30 14:01:10 -07001768 struct AvxInputContext input;
1769 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001770 struct stream_state *streams = NULL;
1771 char **argv, **argi;
1772 uint64_t cx_time = 0;
1773 int stream_cnt = 0;
1774 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001775 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001776
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001777 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001778 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001779
clang-format6c4d83e2016-08-08 19:03:30 -07001780 if (argc < 3) usage_exit();
John Koleszar6ad3b742012-11-06 12:02:42 -08001781
1782 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001783 input.framerate.numerator = 30;
1784 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001785 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001786 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001787
1788 /* First parse the global configuration values, because we want to apply
1789 * other parameters on top of the default configuration provided by the
1790 * codec.
1791 */
1792 argv = argv_dup(argc - 1, argv_ + 1);
1793 parse_global_config(&global, argv);
1794
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001795 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001796 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1797 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1798 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1799 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1800 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001801 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001802
John Koleszar6ad3b742012-11-06 12:02:42 -08001803 {
1804 /* Now parse each stream's parameters. Using a local scope here
1805 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1806 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001807 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001808 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001809
John Koleszar6ad3b742012-11-06 12:02:42 -08001810 do {
1811 stream = new_stream(&global, stream);
1812 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001813 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001814 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001815 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001816
John Koleszarc6b90392012-07-13 15:21:29 -07001817 /* Check for unrecognized options */
1818 for (argi = argv; *argi; argi++)
1819 if (argi[0][0] == '-' && argi[0][1])
1820 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001821
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001822 FOREACH_STREAM(stream, streams) {
1823 check_encoder_config(global.disable_warning_prompt, &global,
1824 &stream->config.cfg);
1825 }
Tom Finegan249366b2013-11-25 12:05:19 -08001826
John Koleszarc6b90392012-07-13 15:21:29 -07001827 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001828 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001829
clang-format6c4d83e2016-08-08 19:03:30 -07001830 if (!input.filename) usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04001831
John Koleszar8dd82872013-05-06 11:01:35 -07001832 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001833 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001834
John Koleszar6ad3b742012-11-06 12:02:42 -08001835 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001836 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001837 int64_t estimated_time_left = -1;
1838 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001839 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001840
John Koleszar6ad3b742012-11-06 12:02:42 -08001841 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001842
John Koleszar6ad3b742012-11-06 12:02:42 -08001843 /* If the input file doesn't specify its w/h (raw files), try to get
1844 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001845 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001846 if (!input.width || !input.height) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001847 FOREACH_STREAM(stream, streams) {
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001848 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1849 input.width = stream->config.cfg.g_w;
1850 input.height = stream->config.cfg.g_h;
1851 break;
1852 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001853 };
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001854 }
John Koleszarc6b90392012-07-13 15:21:29 -07001855
John Koleszar6ad3b742012-11-06 12:02:42 -08001856 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001857 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001858 fatal(
1859 "Specify stream dimensions with --width (-w) "
1860 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001861
1862 /* If input file does not specify bit-depth but input-bit-depth parameter
1863 * exists, assume that to be the input bit-depth. However, if the
1864 * input-bit-depth paramter does not exist, assume the input bit-depth
1865 * to be the same as the codec bit-depth.
1866 */
1867 if (!input.bit_depth) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001868 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001869 if (stream->config.cfg.g_input_bit_depth)
1870 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1871 else
1872 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1873 (int)stream->config.cfg.g_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001874 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001875 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001876 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001877 FOREACH_STREAM(stream, streams) {
1878 stream->config.cfg.g_input_bit_depth = input.bit_depth;
1879 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001880 }
1881
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001882 FOREACH_STREAM(stream, streams) {
Thomas Daede8ec53b22016-09-19 13:24:51 -07001883 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
1884 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
1885 was selected. */
1886 switch (stream->config.cfg.g_profile) {
1887 case 0:
1888 stream->config.cfg.g_profile = 1;
1889 profile_updated = 1;
1890 break;
1891 case 2:
1892 stream->config.cfg.g_profile = 3;
1893 profile_updated = 1;
1894 break;
1895 default: break;
1896 }
1897 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07001898 /* Automatically set the codec bit depth to match the input bit depth.
1899 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001900 if (stream->config.cfg.g_input_bit_depth >
1901 (unsigned int)stream->config.cfg.g_bit_depth) {
1902 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
1903 }
1904 if (stream->config.cfg.g_bit_depth > 8) {
1905 switch (stream->config.cfg.g_profile) {
1906 case 0:
1907 stream->config.cfg.g_profile = 2;
1908 profile_updated = 1;
1909 break;
1910 case 1:
1911 stream->config.cfg.g_profile = 3;
1912 profile_updated = 1;
1913 break;
1914 default: break;
1915 }
1916 }
1917 if (stream->config.cfg.g_profile > 1) {
Sebastien Alaiwanfadc7cb2017-06-08 22:10:43 +02001918 if (!CONFIG_HIGHBITDEPTH) fatal("Unsupported profile.");
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001919 stream->config.use_16bit_internal = 1;
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001920 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08001921 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001922 fprintf(stderr,
1923 "Warning: automatically upgrading to profile %d to "
1924 "match input format.\n",
1925 stream->config.cfg.g_profile);
1926 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001927 }
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001928
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001929 FOREACH_STREAM(stream, streams) {
1930 set_stream_dimensions(stream, input.width, input.height);
1931 }
1932 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
John Koleszar77e6b452010-11-03 13:58:40 -04001933
John Koleszar6ad3b742012-11-06 12:02:42 -08001934 /* Ensure that --passes and --pass are consistent. If --pass is set and
1935 * --passes=2, ensure --fpf was set.
1936 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001937 if (global.pass && global.passes == 2) {
1938 FOREACH_STREAM(stream, streams) {
clang-format6c4d83e2016-08-08 19:03:30 -07001939 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07001940 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07001941 " and --passes=2\n",
1942 stream->index, global.pass);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001943 }
1944 }
John Koleszar3245d462010-06-10 17:10:12 -04001945
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001946#if !CONFIG_WEBM_IO
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001947 FOREACH_STREAM(stream, streams) {
Ronald S. Bultje39775072015-12-16 13:35:43 -05001948 if (stream->config.write_webm) {
1949 stream->config.write_webm = 0;
clang-format6c4d83e2016-08-08 19:03:30 -07001950 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07001951 "aomenc was compiled without WebM container support."
clang-format6c4d83e2016-08-08 19:03:30 -07001952 "Producing IVF output");
Ronald S. Bultje39775072015-12-16 13:35:43 -05001953 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001954 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001955#endif
1956
John Koleszar6ad3b742012-11-06 12:02:42 -08001957 /* Use the frame rate from the file only if none was specified
1958 * on the command-line.
1959 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001960 if (!global.have_framerate) {
1961 global.framerate.num = input.framerate.numerator;
1962 global.framerate.den = input.framerate.denominator;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001963 FOREACH_STREAM(stream, streams) {
1964 stream->config.cfg.g_timebase.den = global.framerate.num;
1965 stream->config.cfg.g_timebase.num = global.framerate.den;
1966 }
Tom Finegan00a35aa2013-11-14 12:37:42 -08001967 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04001968
John Koleszar6ad3b742012-11-06 12:02:42 -08001969 /* Show configuration */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001970 if (global.verbose && pass == 0) {
1971 FOREACH_STREAM(stream, streams) {
1972 show_stream_config(stream, &global, &input);
1973 }
1974 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001975
1976 if (pass == (global.pass ? global.pass - 1 : 0)) {
1977 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07001978 /*The Y4M reader does its own allocation.
1979 Just initialize this here to avoid problems if we never read any
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001980 frames.*/
John Koleszarc6b90392012-07-13 15:21:29 -07001981 memset(&raw, 0, sizeof(raw));
1982 else
Yaowu Xuf883b422016-08-30 14:01:10 -07001983 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04001984
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001985 FOREACH_STREAM(stream, streams) {
1986 stream->rate_hist =
1987 init_rate_histogram(&stream->config.cfg, &global.framerate);
1988 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001989 }
1990
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001991 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
1992 FOREACH_STREAM(stream, streams) {
1993 open_output_file(stream, &global, &input.pixel_aspect_ratio);
1994 }
1995 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001996
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001997#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001998 if (strcmp(global.codec->name, "av1") == 0 ||
1999 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002000 // Check to see if at least one stream uses 16 bit internal.
2001 // Currently assume that the bit_depths for all streams using
2002 // highbitdepth are the same.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002003 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002004 if (stream->config.use_16bit_internal) {
2005 use_16bit_internal = 1;
2006 }
2007 if (stream->config.cfg.g_profile == 0) {
2008 input_shift = 0;
2009 } else {
2010 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07002011 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002012 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002013 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002014 }
2015#endif
2016
John Koleszarc6b90392012-07-13 15:21:29 -07002017 frame_avail = 1;
2018 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002019
John Koleszarc6b90392012-07-13 15:21:29 -07002020 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002021 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002022
John Koleszar6ad3b742012-11-06 12:02:42 -08002023 if (!global.limit || frames_in < global.limit) {
2024 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002025
clang-format6c4d83e2016-08-08 19:03:30 -07002026 if (frame_avail) frames_in++;
2027 seen_frames =
2028 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002029
John Koleszar6ad3b742012-11-06 12:02:42 -08002030 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002031 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002032 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2033
John Koleszar6ad3b742012-11-06 12:02:42 -08002034 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002035 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2036 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002037 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002038 fprintf(stderr, "frame %4d ", frames_in);
2039
clang-format6c4d83e2016-08-08 19:03:30 -07002040 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002041 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002042 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002043 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002044 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002045 }
2046
hui su251e1512016-10-03 15:48:43 -07002047 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002048 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002049 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002050
John Koleszar6ad3b742012-11-06 12:02:42 -08002051 if (frames_in > global.skip_frames) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002052#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002053 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002054 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2055 assert(use_16bit_internal);
2056 // Input bit depth and stream bit depth do not match, so up
2057 // shift frame to stream bit depth
2058 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002059 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002060 input.width, input.height, 32);
2061 allocated_raw_shift = 1;
2062 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002063 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002064 frame_to_encode = &raw_shift;
2065 } else {
2066 frame_to_encode = &raw;
2067 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002068 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002069 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002070 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002071 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002072 if (stream->config.use_16bit_internal)
2073 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002074 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002075 else
2076 assert(0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002077 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002078 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002079 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002080 FOREACH_STREAM(stream, streams) {
2081 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2082 frames_in);
2083 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002084 }
2085#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002086 aom_usec_timer_start(&timer);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002087 FOREACH_STREAM(stream, streams) {
2088 encode_frame(stream, &global, frame_avail ? &raw : NULL, frames_in);
2089 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002090#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002091 aom_usec_timer_mark(&timer);
2092 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002093
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002094 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
John Koleszarc6b90392012-07-13 15:21:29 -07002095
John Koleszar0ea50ce2010-05-18 11:58:33 -04002096 got_data = 0;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002097 FOREACH_STREAM(stream, streams) {
2098 get_cx_data(stream, &global, &got_data);
2099 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002100
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002101 if (!got_data && input.length && streams != NULL &&
2102 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002103 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002104 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002105 int64_t remaining;
2106 int64_t rate;
2107
2108 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002109 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002110
2111 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002112 remaining = 1000 * (global.limit - global.skip_frames -
2113 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002114 } else {
James Zerne3578af2014-04-17 10:47:08 -07002115 const int64_t input_pos = ftello(input.file);
2116 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002117 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002118
2119 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002120 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002121 }
2122
clang-format6c4d83e2016-08-08 19:03:30 -07002123 average_rate =
2124 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002125 estimated_time_left = average_rate ? remaining / average_rate : -1;
2126 }
2127
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002128 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2129 FOREACH_STREAM(stream, streams) {
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02002130 test_decode(stream, global.test_decode);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002131 }
2132 }
John Koleszarc6b90392012-07-13 15:21:29 -07002133 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002134
John Koleszarc6b90392012-07-13 15:21:29 -07002135 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002136 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002137 }
2138
clang-format6c4d83e2016-08-08 19:03:30 -07002139 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002140
Deb Mukherjeea160d722014-09-30 21:56:33 -07002141 if (!global.quiet) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002142 FOREACH_STREAM(stream, streams) {
2143 fprintf(stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2144 "b/f %7" PRId64
2145 "b/s"
2146 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2147 pass + 1, global.passes, frames_in, stream->frames_out,
2148 (int64_t)stream->nbytes,
2149 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2150 seen_frames
2151 ? (int64_t)stream->nbytes * 8 *
2152 (int64_t)global.framerate.num / global.framerate.den /
2153 seen_frames
2154 : 0,
2155 stream->cx_time > 9999999 ? stream->cx_time / 1000
2156 : stream->cx_time,
2157 stream->cx_time > 9999999 ? "ms" : "us",
2158 usec_to_fps(stream->cx_time, seen_frames));
2159 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002160 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002161
Deb Mukherjeea160d722014-09-30 21:56:33 -07002162 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002163 if (global.codec->fourcc == AV1_FOURCC) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002164 FOREACH_STREAM(stream, streams) {
2165 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2166 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002167 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002168 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002169 }
2170 }
John Koleszarc6b90392012-07-13 15:21:29 -07002171
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002172 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002173
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002174 if (global.test_decode != TEST_DECODE_OFF) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002175 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002176 }
2177
John Koleszar6ad3b742012-11-06 12:02:42 -08002178 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002179
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002180 if (global.test_decode == TEST_DECODE_FATAL) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002181 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002182 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002183 FOREACH_STREAM(stream, streams) {
2184 close_output_file(stream, global.codec->fourcc);
2185 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002186
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002187 FOREACH_STREAM(stream, streams) {
2188 stats_close(&stream->stats, global.passes - 1);
2189 }
John Koleszarc6b90392012-07-13 15:21:29 -07002190
Pengchong Jinf349b072014-07-14 09:13:38 -07002191#if CONFIG_FP_MB_STATS
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002192 FOREACH_STREAM(stream, streams) {
2193 stats_close(&stream->fpmb_stats, global.passes - 1);
2194 }
Pengchong Jinf349b072014-07-14 09:13:38 -07002195#endif
2196
clang-format6c4d83e2016-08-08 19:03:30 -07002197 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002198 }
2199
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002200 if (global.show_q_hist_buckets) {
2201 FOREACH_STREAM(stream, streams) {
2202 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2203 }
2204 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002205
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002206 if (global.show_rate_hist_buckets) {
2207 FOREACH_STREAM(stream, streams) {
2208 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2209 global.show_rate_hist_buckets);
2210 }
2211 }
2212 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002213
Ronald S. Bultje31214972012-10-11 10:13:48 -07002214#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002215 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2216 * to match some existing utilities.
2217 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002218 if (!(global.pass == 1 && global.passes == 2)) {
2219 FOREACH_STREAM(stream, streams) {
Yaowu Xu47e784e2013-10-16 16:29:28 -07002220 FILE *f = fopen("opsnr.stt", "a");
2221 if (stream->mismatch_seen) {
2222 fprintf(f, "First mismatch occurred in frame %d\n",
2223 stream->mismatch_seen);
2224 } else {
2225 fprintf(f, "No mismatch detected in recon buffers\n");
2226 }
2227 fclose(f);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002228 }
2229 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002230#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002231
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002232#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002233 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002234#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002235 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002236 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002237 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002238 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002239}