blob: f4bf888ce5826987ae21d7acb3b4d9fedbf731a7 [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"
John Koleszar5ebe94f2012-12-23 07:20:10 -080028#if CONFIG_DECODERS
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"
48#include "aom_ports/aom_timer.h"
49#include "aom_ports/mem_ops.h"
James Zerndba73762014-05-10 17:44:12 -070050#if CONFIG_WEBM_IO
Tom Finegan78cb2e62013-11-07 21:28:45 -080051#include "./webmenc.h"
James Zerndba73762014-05-10 17:44:12 -070052#endif
Tom Finegan78cb2e62013-11-07 21:28:45 -080053#include "./y4minput.h"
Tom Finegan03848f52013-11-05 10:02:18 -080054
John Koleszar6291dd42012-06-19 21:05:36 -070055/* Swallow warnings about unused results of fread/fwrite */
clang-format6c4d83e2016-08-08 19:03:30 -070056static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
John Koleszar6ad3b742012-11-06 12:02:42 -080057 return fread(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070058}
59#define fread wrap_fread
60
61static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080062 FILE *stream) {
63 return fwrite(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070064}
65#define fwrite wrap_fwrite
66
John Koleszar0ea50ce2010-05-18 11:58:33 -040067static const char *exec_name;
68
Yaowu Xuf883b422016-08-30 14:01:10 -070069static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080070 const char *s, va_list ap) {
John Koleszarc6b90392012-07-13 15:21:29 -070071 if (ctx->err) {
Yaowu Xuf883b422016-08-30 14:01:10 -070072 const char *detail = aom_codec_error_detail(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -040073
John Koleszar6ad3b742012-11-06 12:02:42 -080074 vfprintf(stderr, s, ap);
Yaowu Xuf883b422016-08-30 14:01:10 -070075 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -040076
clang-format6c4d83e2016-08-08 19:03:30 -070077 if (detail) fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -040078
clang-format6c4d83e2016-08-08 19:03:30 -070079 if (fatal) exit(EXIT_FAILURE);
John Koleszarc6b90392012-07-13 15:21:29 -070080 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040081}
82
Yaowu Xuf883b422016-08-30 14:01:10 -070083static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080084 va_list ap;
85
86 va_start(ap, s);
87 warn_or_exit_on_errorv(ctx, 1, s, ap);
88 va_end(ap);
89}
90
Yaowu Xuf883b422016-08-30 14:01:10 -070091static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080092 const char *s, ...) {
93 va_list ap;
94
95 va_start(ap, s);
96 warn_or_exit_on_errorv(ctx, fatal, s, ap);
97 va_end(ap);
98}
99
Yaowu Xuf883b422016-08-30 14:01:10 -0700100static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800101 FILE *f = input_ctx->file;
102 y4m_input *y4m = &input_ctx->y4m;
John Koleszarc6b90392012-07-13 15:21:29 -0700103 int shortread = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400104
Tom Finegan00a35aa2013-11-14 12:37:42 -0800105 if (input_ctx->file_type == FILE_TYPE_Y4M) {
clang-format6c4d83e2016-08-08 19:03:30 -0700106 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700107 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800108 shortread = read_yuv_frame(input_ctx, img);
John Koleszarc6b90392012-07-13 15:21:29 -0700109 }
110
111 return !shortread;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400112}
113
James Zern5c337fd2015-05-09 10:42:58 -0700114static int file_is_y4m(const char detect[4]) {
John Koleszarc6b90392012-07-13 15:21:29 -0700115 if (memcmp(detect, "YUV4", 4) == 0) {
116 return 1;
117 }
118 return 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400119}
120
James Zern5c337fd2015-05-09 10:42:58 -0700121static int fourcc_is_ivf(const char detect[4]) {
Alex Converse64b89f12014-01-06 16:29:09 -0800122 if (memcmp(detect, "DKIF", 4) == 0) {
123 return 1;
124 }
125 return 0;
126}
John Koleszardc666302010-10-20 12:05:48 -0400127
clang-format6c4d83e2016-08-08 19:03:30 -0700128static const arg_def_t debugmode =
129 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
130static const arg_def_t outputfile =
131 ARG_DEF("o", "output", 1, "Output filename");
132static const arg_def_t use_yv12 =
133 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
134static const arg_def_t use_i420 =
135 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
136static const arg_def_t use_i422 =
137 ARG_DEF(NULL, "i422", 0, "Input file is I422");
138static const arg_def_t use_i444 =
139 ARG_DEF(NULL, "i444", 0, "Input file is I444");
140static const arg_def_t use_i440 =
141 ARG_DEF(NULL, "i440", 0, "Input file is I440");
142static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
143static const arg_def_t passes =
144 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
145static const arg_def_t pass_arg =
146 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
147static const arg_def_t fpf_name =
148 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700149#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700150static const arg_def_t fpmbf_name =
151 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700152#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700153static const arg_def_t limit =
154 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
155static const arg_def_t skip =
156 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
157static const arg_def_t deadline =
158 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
clang-format6c4d83e2016-08-08 19:03:30 -0700159static const arg_def_t good_dl =
160 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
clang-format6c4d83e2016-08-08 19:03:30 -0700161static const arg_def_t quietarg =
162 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
163static const arg_def_t verbosearg =
164 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
165static const arg_def_t psnrarg =
166 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800167
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800168static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700169 { "off", TEST_DECODE_OFF },
170 { "fatal", TEST_DECODE_FATAL },
171 { "warn", TEST_DECODE_WARN },
172 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800173};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700174static const arg_def_t recontest = ARG_DEF_ENUM(
175 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700176static const arg_def_t framerate =
177 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
178static const arg_def_t use_webm =
179 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
180static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
181static const arg_def_t out_part =
182 ARG_DEF("P", "output-partitions", 0,
183 "Makes encoder output partitions. Requires IVF output!");
184static const arg_def_t q_hist_n =
185 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
186static const arg_def_t rate_hist_n =
187 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
188static const arg_def_t disable_warnings =
189 ARG_DEF(NULL, "disable-warnings", 0,
190 "Disable warnings about potentially incorrect encode settings.");
191static const arg_def_t disable_warning_prompt =
192 ARG_DEF("y", "disable-warning-prompt", 0,
193 "Display warnings, but do not prompt user to continue.");
Alex Conversed66bd222014-02-21 10:52:09 -0800194
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200195#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700196static const arg_def_t test16bitinternalarg = ARG_DEF(
197 NULL, "test-16bit-internal", 0, "Force use of 16 bit internal buffer");
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400198
199static const struct arg_enum_list bitdepth_enum[] = {
200 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
201};
202
203static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
204 "b", "bit-depth", 1,
205 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
206 bitdepth_enum);
207static const arg_def_t inbitdeptharg =
208 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700209#endif
Tom Finegan44dd3272013-11-20 17:18:28 -0800210
clang-format6c4d83e2016-08-08 19:03:30 -0700211static const arg_def_t *main_args[] = { &debugmode,
212 &outputfile,
213 &codecarg,
214 &passes,
215 &pass_arg,
216 &fpf_name,
217 &limit,
218 &skip,
219 &deadline,
clang-format6c4d83e2016-08-08 19:03:30 -0700220 &good_dl,
clang-format6c4d83e2016-08-08 19:03:30 -0700221 &quietarg,
222 &verbosearg,
223 &psnrarg,
224 &use_webm,
225 &use_ivf,
226 &out_part,
227 &q_hist_n,
228 &rate_hist_n,
229 &disable_warnings,
230 &disable_warning_prompt,
231 &recontest,
232 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400233
clang-format6c4d83e2016-08-08 19:03:30 -0700234static const arg_def_t usage =
235 ARG_DEF("u", "usage", 1, "Usage profile number to use");
236static const arg_def_t threads =
237 ARG_DEF("t", "threads", 1, "Max number of threads to use");
238static const arg_def_t profile =
239 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700240static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
241static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700242#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700243static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700244 { "mono", STEREO_FORMAT_MONO },
245 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
246 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
247 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
248 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
249 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700250};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700251static const arg_def_t stereo_mode = ARG_DEF_ENUM(
252 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700253#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700254static const arg_def_t timebase = ARG_DEF(
255 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700256static const arg_def_t error_resilient =
257 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
258static const arg_def_t lag_in_frames =
259 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400260
clang-format6c4d83e2016-08-08 19:03:30 -0700261static const arg_def_t *global_args[] = { &use_yv12,
262 &use_i420,
263 &use_i422,
264 &use_i444,
265 &use_i440,
266 &usage,
267 &threads,
268 &profile,
269 &width,
270 &height,
James Zerndba73762014-05-10 17:44:12 -0700271#if CONFIG_WEBM_IO
clang-format6c4d83e2016-08-08 19:03:30 -0700272 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700273#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700274 &timebase,
275 &framerate,
276 &error_resilient,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200277#if CONFIG_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700278 &test16bitinternalarg,
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400279 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700280#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700281 &lag_in_frames,
282 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400283
clang-format6c4d83e2016-08-08 19:03:30 -0700284static const arg_def_t dropframe_thresh =
285 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
286static const arg_def_t resize_allowed =
287 ARG_DEF(NULL, "resize-allowed", 1, "Spatial resampling enabled (bool)");
288static const arg_def_t resize_width =
289 ARG_DEF(NULL, "resize-width", 1, "Width of encoded frame");
290static const arg_def_t resize_height =
291 ARG_DEF(NULL, "resize-height", 1, "Height of encoded frame");
292static const arg_def_t resize_up_thresh =
293 ARG_DEF(NULL, "resize-up", 1, "Upscale threshold (buf %)");
294static const arg_def_t resize_down_thresh =
295 ARG_DEF(NULL, "resize-down", 1, "Downscale threshold (buf %)");
Yaowu Xuf883b422016-08-30 14:01:10 -0700296static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
297 { "cbr", AOM_CBR },
298 { "cq", AOM_CQ },
299 { "q", AOM_Q },
clang-format6c4d83e2016-08-08 19:03:30 -0700300 { NULL, 0 } };
301static const arg_def_t end_usage =
302 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
303static const arg_def_t target_bitrate =
304 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
305static const arg_def_t min_quantizer =
306 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
307static const arg_def_t max_quantizer =
308 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
309static const arg_def_t undershoot_pct =
310 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
311static const arg_def_t overshoot_pct =
312 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
313static const arg_def_t buf_sz =
314 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
315static const arg_def_t buf_initial_sz =
316 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
317static const arg_def_t buf_optimal_sz =
318 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
John Koleszarc6b90392012-07-13 15:21:29 -0700319static const arg_def_t *rc_args[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700320 &dropframe_thresh, &resize_allowed, &resize_width, &resize_height,
321 &resize_up_thresh, &resize_down_thresh, &end_usage, &target_bitrate,
322 &min_quantizer, &max_quantizer, &undershoot_pct, &overshoot_pct,
323 &buf_sz, &buf_initial_sz, &buf_optimal_sz, NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -0400324};
325
clang-format6c4d83e2016-08-08 19:03:30 -0700326static const arg_def_t bias_pct =
327 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
328static const arg_def_t minsection_pct =
329 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
330static const arg_def_t maxsection_pct =
331 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
332static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
333 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400334
clang-format6c4d83e2016-08-08 19:03:30 -0700335static const arg_def_t kf_min_dist =
336 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
337static const arg_def_t kf_max_dist =
338 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
339static const arg_def_t kf_disabled =
340 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
341static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
342 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400343
clang-format6c4d83e2016-08-08 19:03:30 -0700344static const arg_def_t noise_sens =
345 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
346static const arg_def_t sharpness =
347 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
348static const arg_def_t static_thresh =
349 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
350static const arg_def_t auto_altref =
351 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
352static const arg_def_t arnr_maxframes =
353 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
354static const arg_def_t arnr_strength =
355 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500356static const struct arg_enum_list tuning_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700357 { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500358};
clang-format6c4d83e2016-08-08 19:03:30 -0700359static const arg_def_t tune_ssim =
360 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
361static const arg_def_t cq_level =
362 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
363static const arg_def_t max_intra_rate_pct =
364 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800365
Yaowu Xuf883b422016-08-30 14:01:10 -0700366#if CONFIG_AV1_ENCODER
367static const arg_def_t cpu_used_av1 =
Yaowu Xu39737592017-04-21 16:39:09 -0700368 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
clang-format6c4d83e2016-08-08 19:03:30 -0700369static const arg_def_t tile_cols =
370 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
371static const arg_def_t tile_rows =
372 ARG_DEF(NULL, "tile-rows", 1,
373 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800374#if CONFIG_EXT_TILE
375static const arg_def_t tile_encoding_mode =
376 ARG_DEF(NULL, "tile-encoding-mode", 1,
377 "Tile encoding mode (0: normal"
378 " (default), 1: vr)");
379#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800380#if CONFIG_DEPENDENT_HORZTILES
381static const arg_def_t tile_dependent_rows =
382 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
383#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800384#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800385static const arg_def_t tile_loopfilter = ARG_DEF(
386 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800387#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700388static const arg_def_t lossless =
389 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700390#if CONFIG_AOM_QM
391static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800392 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700393 "Enable quantisation matrices (0: false (default), 1: true)");
394static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800395 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700396static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800397 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700398#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000399#if CONFIG_TILE_GROUPS
Yaowu Xu859a5272016-11-10 15:32:21 -0800400static const arg_def_t num_tg = ARG_DEF(
401 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000402static const arg_def_t mtu_size =
403 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800404 "MTU size for a tile group, default is 0 (no MTU targeting), "
405 "overrides maximum number of tile groups");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000406#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800407#if CONFIG_TEMPMV_SIGNALING
408static const arg_def_t disable_tempmv = ARG_DEF(
409 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
410#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700411static const arg_def_t frame_parallel_decoding =
412 ARG_DEF(NULL, "frame-parallel", 1,
413 "Enable frame parallel decodability features "
414 "(0: false (default), 1: true)");
Fangwen Fu6160df22017-04-24 09:45:51 -0700415#if CONFIG_DELTA_Q && !CONFIG_EXT_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100416static const arg_def_t aq_mode = ARG_DEF(
417 NULL, "aq-mode", 1,
418 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100419 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200420#else
Thomase28d92b2016-09-20 12:07:11 +0100421static const arg_def_t aq_mode = ARG_DEF(
422 NULL, "aq-mode", 1,
423 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100424 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200425#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700426#if CONFIG_EXT_DELTA_Q
427static const arg_def_t deltaq_mode = ARG_DEF(
428 NULL, "deltaq-mode", 1,
429 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
430#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700431static const arg_def_t frame_periodic_boost =
432 ARG_DEF(NULL, "frame-boost", 1,
433 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700434static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
435 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700436static const arg_def_t max_inter_rate_pct =
437 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700438static const arg_def_t min_gf_interval = ARG_DEF(
439 NULL, "min-gf-interval", 1,
440 "min gf/arf frame interval (default 0, indicating in-built behavior)");
441static const arg_def_t max_gf_interval = ARG_DEF(
442 NULL, "max-gf-interval", 1,
443 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800444
Yaowu Xufc996362015-02-10 15:03:05 -0800445static const struct arg_enum_list color_space_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700446 { "unknown", AOM_CS_UNKNOWN },
447 { "bt601", AOM_CS_BT_601 },
448 { "bt709", AOM_CS_BT_709 },
449 { "smpte170", AOM_CS_SMPTE_170 },
450 { "smpte240", AOM_CS_SMPTE_240 },
451 { "bt2020", AOM_CS_BT_2020 },
452 { "reserved", AOM_CS_RESERVED },
453 { "sRGB", AOM_CS_SRGB },
Yaowu Xufc996362015-02-10 15:03:05 -0800454 { NULL, 0 }
455};
456
clang-format6c4d83e2016-08-08 19:03:30 -0700457static const arg_def_t input_color_space =
458 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
459 color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800460
Alex Converse219f6452014-08-06 11:01:18 -0700461static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700462 { "default", AOM_CONTENT_DEFAULT },
463 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700464 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700465};
466
467static const arg_def_t tune_content = ARG_DEF_ENUM(
468 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700469#endif
Alex Converse219f6452014-08-06 11:01:18 -0700470
Yaowu Xuf883b422016-08-30 14:01:10 -0700471#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000472#if CONFIG_EXT_PARTITION
473static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700474 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
475 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
476 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700477 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000478};
479static const arg_def_t superblock_size = ARG_DEF_ENUM(
480 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
481#endif // CONFIG_EXT_PARTITION
482
Yaowu Xuf883b422016-08-30 14:01:10 -0700483static const arg_def_t *av1_args[] = { &cpu_used_av1,
484 &auto_altref,
485 &sharpness,
486 &static_thresh,
487 &tile_cols,
488 &tile_rows,
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800489#if CONFIG_EXT_TILE
490 &tile_encoding_mode,
491#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800492#if CONFIG_DEPENDENT_HORZTILES
493 &tile_dependent_rows,
494#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800495#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800496 &tile_loopfilter,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800497#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700498 &arnr_maxframes,
499 &arnr_strength,
Yaowu Xuf883b422016-08-30 14:01:10 -0700500 &tune_ssim,
501 &cq_level,
502 &max_intra_rate_pct,
503 &max_inter_rate_pct,
504 &gf_cbr_boost_pct,
505 &lossless,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800506#if CONFIG_AOM_QM
507 &enable_qm,
508 &qm_min,
509 &qm_max,
510#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700511 &frame_parallel_decoding,
512 &aq_mode,
Fangwen Fu6160df22017-04-24 09:45:51 -0700513#if CONFIG_EXT_DELTA_Q
514 &deltaq_mode,
515#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700516 &frame_periodic_boost,
517 &noise_sens,
518 &tune_content,
519 &input_color_space,
520 &min_gf_interval,
521 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000522#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700523 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000524#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000525#if CONFIG_TILE_GROUPS
526 &num_tg,
527 &mtu_size,
528#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800529#if CONFIG_TEMPMV_SIGNALING
530 &disable_tempmv,
531#endif
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200532#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700533 &bitdeptharg,
534 &inbitdeptharg,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200535#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700536 NULL };
537static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
538 AOME_SET_ENABLEAUTOALTREF,
539 AOME_SET_SHARPNESS,
540 AOME_SET_STATIC_THRESHOLD,
541 AV1E_SET_TILE_COLUMNS,
542 AV1E_SET_TILE_ROWS,
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800543#if CONFIG_EXT_TILE
544 AV1E_SET_TILE_ENCODING_MODE,
545#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800546#if CONFIG_DEPENDENT_HORZTILES
547 AV1E_SET_TILE_DEPENDENT_ROWS,
548#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800549#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800550 AV1E_SET_TILE_LOOPFILTER,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800551#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700552 AOME_SET_ARNR_MAXFRAMES,
553 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700554 AOME_SET_TUNING,
555 AOME_SET_CQ_LEVEL,
556 AOME_SET_MAX_INTRA_BITRATE_PCT,
557 AV1E_SET_MAX_INTER_BITRATE_PCT,
558 AV1E_SET_GF_CBR_BOOST_PCT,
559 AV1E_SET_LOSSLESS,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800560#if CONFIG_AOM_QM
561 AV1E_SET_ENABLE_QM,
562 AV1E_SET_QM_MIN,
563 AV1E_SET_QM_MAX,
564#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700565 AV1E_SET_FRAME_PARALLEL_DECODING,
566 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700567#if CONFIG_EXT_DELTA_Q
568 AV1E_SET_DELTAQ_MODE,
569#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700570 AV1E_SET_FRAME_PERIODIC_BOOST,
571 AV1E_SET_NOISE_SENSITIVITY,
572 AV1E_SET_TUNE_CONTENT,
573 AV1E_SET_COLOR_SPACE,
574 AV1E_SET_MIN_GF_INTERVAL,
575 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000576#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700577 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000578#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000579#if CONFIG_TILE_GROUPS
580 AV1E_SET_NUM_TG,
581 AV1E_SET_MTU,
582#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800583#if CONFIG_TEMPMV_SIGNALING
584 AV1E_SET_DISABLE_TEMPMV,
585#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700586 0 };
hui su82331e02015-08-16 18:21:56 -0700587#endif
588
John Koleszar0ea50ce2010-05-18 11:58:33 -0400589static const arg_def_t *no_args[] = { NULL };
590
James Zern59e7a472015-05-09 10:33:26 -0700591void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700592 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700593 const int num_encoder = get_aom_encoder_count();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400594
John Koleszarc6b90392012-07-13 15:21:29 -0700595 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
596 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400597
John Koleszarc6b90392012-07-13 15:21:29 -0700598 fprintf(stderr, "\nOptions:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700599 arg_show_usage(stderr, main_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700600 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700601 arg_show_usage(stderr, global_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700602 fprintf(stderr, "\nRate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700603 arg_show_usage(stderr, rc_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700604 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700605 arg_show_usage(stderr, rc_twopass_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700606 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700607 arg_show_usage(stderr, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700608#if CONFIG_AV1_ENCODER
609 fprintf(stderr, "\nAV1 Specific Options:\n");
610 arg_show_usage(stderr, av1_args);
hui su82331e02015-08-16 18:21:56 -0700611#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700612 fprintf(stderr,
613 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700614 " The desired precision of timestamps in the output, expressed\n"
615 " in fractional seconds. Default is 1/1000.\n");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800616 fprintf(stderr, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400617
Yaowu Xuf990b352015-06-01 09:13:59 -0700618 for (i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700619 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700620 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
621 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700622 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800623 }
Yaowu Xuf990b352015-06-01 09:13:59 -0700624 fprintf(stderr, "\n ");
625 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400626
John Koleszarc6b90392012-07-13 15:21:29 -0700627 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400628}
629
clang-format6c4d83e2016-08-08 19:03:30 -0700630#define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
Yaowu Xuf883b422016-08-30 14:01:10 -0700631#if CONFIG_AV1_ENCODER
632#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800633#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800634
James Zerndba73762014-05-10 17:44:12 -0700635#if !CONFIG_WEBM_IO
636typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700637struct WebmOutputContext {
638 int debug;
639};
James Zerndba73762014-05-10 17:44:12 -0700640#endif
641
John Koleszar9e50ed72012-02-15 12:39:38 -0800642/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800643struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700644 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700645 const char *out_fn;
646 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700647#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700648 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700649#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700650 stereo_format_t stereo_fmt;
651 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
652 int arg_ctrl_cnt;
653 int write_webm;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200654#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700655 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700656 int use_16bit_internal;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700657#endif
John Koleszar9e50ed72012-02-15 12:39:38 -0800658};
659
John Koleszar6ad3b742012-11-06 12:02:42 -0800660struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700661 int index;
662 struct stream_state *next;
663 struct stream_config config;
664 FILE *file;
665 struct rate_hist *rate_hist;
666 struct WebmOutputContext webm_ctx;
667 uint64_t psnr_sse_total;
668 uint64_t psnr_samples_total;
669 double psnr_totals[4];
670 int psnr_count;
671 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700672 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700673 unsigned int frames_out;
674 uint64_t cx_time;
675 size_t nbytes;
676 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700677#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700678 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700679#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700680 struct aom_image *img;
681 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700682 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800683};
684
clang-format6c4d83e2016-08-08 19:03:30 -0700685static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700686 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800687 if (rat->den < 0) {
688 rat->num *= -1;
689 rat->den *= -1;
690 }
John Koleszar1b27e932012-04-25 17:08:56 -0700691
clang-format6c4d83e2016-08-08 19:03:30 -0700692 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700693
clang-format6c4d83e2016-08-08 19:03:30 -0700694 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700695}
696
Yaowu Xuf883b422016-08-30 14:01:10 -0700697static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700698 char **argi, **argj;
699 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700700 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700701
clang-format6c4d83e2016-08-08 19:03:30 -0700702 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800703
John Koleszar6ad3b742012-11-06 12:02:42 -0800704 /* Initialize default parameters */
705 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700706 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700707 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700708 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700709 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700710 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400711
John Koleszarc6b90392012-07-13 15:21:29 -0700712 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
713 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400714
John Koleszarc6b90392012-07-13 15:21:29 -0700715 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700716 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800717 if (!global->codec)
718 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700719 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800720 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400721
John Koleszar6ad3b742012-11-06 12:02:42 -0800722 if (global->passes < 1 || global->passes > 2)
723 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700724 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800725 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400726
John Koleszar6ad3b742012-11-06 12:02:42 -0800727 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700728 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800729 } else if (arg_match(&arg, &usage, argi))
730 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700731 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800732 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700733 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700734 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar6ad3b742012-11-06 12:02:42 -0800735 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700736 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800737 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700738 global->color_type = I420;
739 else if (arg_match(&arg, &use_i422, argi))
740 global->color_type = I422;
741 else if (arg_match(&arg, &use_i444, argi))
742 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700743 else if (arg_match(&arg, &use_i440, argi))
744 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800745 else if (arg_match(&arg, &quietarg, argi))
746 global->quiet = 1;
747 else if (arg_match(&arg, &verbosearg, argi))
748 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700749 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800750 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700751 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800752 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700753 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800754 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700755 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800756 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700757 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800758 global->framerate = arg_parse_rational(&arg);
759 validate_positive_rational(arg.name, &global->framerate);
760 global->have_framerate = 1;
761 } else if (arg_match(&arg, &out_part, argi))
762 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700763 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800764 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700765 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800766 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700767 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800768 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800769 else if (arg_match(&arg, &disable_warnings, argi))
770 global->disable_warnings = 1;
771 else if (arg_match(&arg, &disable_warning_prompt, argi))
772 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800773 else
John Koleszarc6b90392012-07-13 15:21:29 -0700774 argj++;
775 }
776
John Koleszar6ad3b742012-11-06 12:02:42 -0800777 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700778 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800779 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700780 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
781 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800782 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800783 }
John Koleszarc6b90392012-07-13 15:21:29 -0700784 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800785 /* Validate global config */
786 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700787#if CONFIG_AV1_ENCODER
788 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800789 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700790 if (global->codec != NULL && global->codec->name != NULL)
Thomas Daede80826142017-03-20 15:44:24 -0700791 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800792#else
793 global->passes = 1;
794#endif
795 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800796}
797
Yaowu Xuf883b422016-08-30 14:01:10 -0700798static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800799 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700800 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
801 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -0800802
clang-format6c4d83e2016-08-08 19:03:30 -0700803 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -0800804
John Koleszar25b6e9f2013-02-12 21:17:56 -0800805 if (!fseeko(input->file, 0, SEEK_END)) {
806 /* Input file is seekable. Figure out how long it is, so we can get
807 * progress info.
808 */
809 input->length = ftello(input->file);
810 rewind(input->file);
811 }
812
Frank Galligan09acd262015-06-01 10:20:58 -0700813 /* Default to 1:1 pixel aspect ratio. */
814 input->pixel_aspect_ratio.numerator = 1;
815 input->pixel_aspect_ratio.denominator = 1;
816
John Koleszar6ad3b742012-11-06 12:02:42 -0800817 /* For RAW input sources, these bytes will applied on the first frame
818 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -0700819 */
John Koleszar6ad3b742012-11-06 12:02:42 -0800820 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
821 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400822
clang-format6c4d83e2016-08-08 19:03:30 -0700823 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -0700824 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
825 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800826 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800827 input->width = input->y4m.pic_w;
828 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -0700829 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
830 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800831 input->framerate.numerator = input->y4m.fps_n;
832 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -0700833 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -0700834 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -0800835 } else
836 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -0800837 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -0800838 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -0800839 } else {
840 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700841 }
John Koleszar6ad3b742012-11-06 12:02:42 -0800842}
843
Yaowu Xuf883b422016-08-30 14:01:10 -0700844static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800845 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -0700846 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -0800847}
848
Yaowu Xuf883b422016-08-30 14:01:10 -0700849static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -0800850 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800851 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800852
John Koleszar6ad3b742012-11-06 12:02:42 -0800853 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700854 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800855 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700856 }
857
John Koleszar6ad3b742012-11-06 12:02:42 -0800858 if (prev) {
859 memcpy(stream, prev, sizeof(*stream));
860 stream->index++;
861 prev->next = stream;
862 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700863 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -0800864
John Koleszar6ad3b742012-11-06 12:02:42 -0800865 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -0700866 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -0700867 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -0700868 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -0800869
John Koleszar6ad3b742012-11-06 12:02:42 -0800870 /* Change the default timebase to a high enough value so that the
871 * encoder will always create strictly increasing timestamps.
872 */
873 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -0800874
John Koleszar6ad3b742012-11-06 12:02:42 -0800875 /* Never use the library's default resolution, require it be parsed
876 * from the file or set on the command line.
877 */
878 stream->config.cfg.g_w = 0;
879 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800880
John Koleszar6ad3b742012-11-06 12:02:42 -0800881 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -0800882 stream->config.write_webm = 1;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700883#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -0700884 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700885 stream->webm_ctx.last_pts_ns = -1;
886 stream->webm_ctx.writer = NULL;
887 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700888#endif
Johann87c40b32012-03-01 16:12:53 -0800889
John Koleszar6ad3b742012-11-06 12:02:42 -0800890 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700891 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -0800892 }
John Koleszar9e50ed72012-02-15 12:39:38 -0800893
John Koleszar6ad3b742012-11-06 12:02:42 -0800894 /* Output files must be specified for each stream */
895 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -0800896
John Koleszar6ad3b742012-11-06 12:02:42 -0800897 stream->next = NULL;
898 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800899}
900
Yaowu Xuf883b422016-08-30 14:01:10 -0700901static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -0700902 struct stream_state *stream, char **argv) {
903 char **argi, **argj;
904 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -0800905 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -0700906 static const int *ctrl_args_map = NULL;
907 struct stream_config *config = &stream->config;
908 int eos_mark_found = 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200909#if CONFIG_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700910 int test_16bit_internal = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700911#endif
John Koleszar9e50ed72012-02-15 12:39:38 -0800912
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800913 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -0800914 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700915#if CONFIG_AV1_ENCODER
916 } else if (strcmp(global->codec->name, "av1") == 0) {
917 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
918 // Consider to expand this set for AV1 encoder control.
919 ctrl_args = av1_args;
920 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -0700921#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700922 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400923
John Koleszarc6b90392012-07-13 15:21:29 -0700924 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -0700925 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400926
John Koleszar6ad3b742012-11-06 12:02:42 -0800927 /* Once we've found an end-of-stream marker (--) we want to continue
928 * shifting arguments but not consuming them.
929 */
930 if (eos_mark_found) {
931 argj++;
932 continue;
933 } else if (!strcmp(*argj, "--")) {
934 eos_mark_found = 1;
935 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -0800936 }
937
Adrian Granged2401802015-02-13 14:51:32 -0800938 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800939 config->out_fn = arg.val;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800940 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800941 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -0700942#if CONFIG_FP_MB_STATS
943 } else if (arg_match(&arg, &fpmbf_name, argi)) {
944 config->fpmb_stats_fn = arg.val;
945#endif
Johannb50e5182015-01-30 15:05:14 -0800946 } else if (arg_match(&arg, &use_webm, argi)) {
947#if CONFIG_WEBM_IO
948 config->write_webm = 1;
949#else
950 die("Error: --webm specified but webm is disabled.");
951#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800952 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800953 config->write_webm = 0;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800954 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800955 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800956 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800957 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800958 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800959 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800960 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800961 config->cfg.g_h = arg_parse_uint(&arg);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200962#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700963 } else if (arg_match(&arg, &bitdeptharg, argi)) {
964 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
965 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
966 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
967#endif
James Zerndba73762014-05-10 17:44:12 -0700968#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800969 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800970 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -0700971#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800972 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800973 config->cfg.g_timebase = arg_parse_rational(&arg);
974 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800975 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800976 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800977 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800978 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800979 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800980 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800981 } else if (arg_match(&arg, &resize_allowed, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800982 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
Adrian Grangef7bd1272014-04-09 14:51:29 -0700983 } else if (arg_match(&arg, &resize_width, argi)) {
984 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
985 } else if (arg_match(&arg, &resize_height, argi)) {
986 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800987 } else if (arg_match(&arg, &resize_up_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800988 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800989 } else if (arg_match(&arg, &resize_down_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800990 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800991 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800992 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800993 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800994 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800995 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800996 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800997 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800998 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800999 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001000 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001001 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001002 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001003 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001004 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001005 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001006 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001007 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001008 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001009 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001010 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001011 if (global->passes < 2)
1012 warn("option %s ignored in one-pass mode.\n", arg.name);
1013 } else if (arg_match(&arg, &minsection_pct, argi)) {
1014 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1015
1016 if (global->passes < 2)
1017 warn("option %s ignored in one-pass mode.\n", arg.name);
1018 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1019 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1020
1021 if (global->passes < 2)
1022 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001023 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001024 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001025 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001026 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001027 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001028 config->cfg.kf_mode = AOM_KF_DISABLED;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001029#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001030 } else if (arg_match(&arg, &test16bitinternalarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001031 if (strcmp(global->codec->name, "av1") == 0 ||
1032 strcmp(global->codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001033 test_16bit_internal = 1;
1034 }
1035#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001036 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001037 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001038 for (i = 0; ctrl_args[i]; i++) {
1039 if (arg_match(&arg, ctrl_args[i], argi)) {
1040 int j;
1041 match = 1;
1042
1043 /* Point either to the next free element or the first
1044 * instance of this control.
1045 */
1046 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001047 if (ctrl_args_map != NULL &&
1048 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001049 break;
1050
1051 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001052 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001053 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001054 config->arg_ctrls[j][0] = ctrl_args_map[i];
1055 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001056 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001057 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001058 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001059 }
clang-format6c4d83e2016-08-08 19:03:30 -07001060 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001061 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001062 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001063#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan374a59072017-04-19 14:18:35 +02001064#if CONFIG_LOWBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001065 if (strcmp(global->codec->name, "av1") == 0 ||
1066 strcmp(global->codec->name, "av1") == 0) {
clang-format6c4d83e2016-08-08 19:03:30 -07001067 config->use_16bit_internal =
1068 test_16bit_internal | (config->cfg.g_profile > 1);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001069 }
Sebastien Alaiwan374a59072017-04-19 14:18:35 +02001070#else
1071 config->use_16bit_internal = 1;
1072#endif
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001073#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001074 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001075}
1076
clang-format6c4d83e2016-08-08 19:03:30 -07001077#define FOREACH_STREAM(func) \
1078 do { \
1079 struct stream_state *stream; \
Tom Finegan249366b2013-11-25 12:05:19 -08001080 for (stream = streams; stream; stream = stream->next) { \
clang-format6c4d83e2016-08-08 19:03:30 -07001081 func; \
1082 } \
Tom Finegan44dd3272013-11-20 17:18:28 -08001083 } while (0)
John Koleszar9e50ed72012-02-15 12:39:38 -08001084
Alex Conversed66bd222014-02-21 10:52:09 -08001085static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001086 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001087 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001088 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001089
John Koleszar6ad3b742012-11-06 12:02:42 -08001090 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001091 fatal(
1092 "Stream %d: Specify stream dimensions with --width (-w) "
1093 " and --height (-h)",
1094 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001095
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001096 // Check that the codec bit depth is greater than the input bit depth.
1097 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001098 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001099 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1100 stream->index, (int)stream->config.cfg.g_bit_depth,
1101 stream->config.cfg.g_input_bit_depth);
1102 }
1103
John Koleszar6ad3b742012-11-06 12:02:42 -08001104 for (streami = stream; streami; streami = streami->next) {
1105 /* All streams require output files */
1106 if (!streami->config.out_fn)
1107 fatal("Stream %d: Output file is required (specify with -o)",
1108 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001109
John Koleszar6ad3b742012-11-06 12:02:42 -08001110 /* Check for two streams outputting to the same file */
1111 if (streami != stream) {
1112 const char *a = stream->config.out_fn;
1113 const char *b = streami->config.out_fn;
1114 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1115 fatal("Stream %d: duplicate output file (from stream %d)",
1116 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001117 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001118
1119 /* Check for two streams sharing a stats file. */
1120 if (streami != stream) {
1121 const char *a = stream->config.stats_fn;
1122 const char *b = streami->config.stats_fn;
1123 if (a && b && !strcmp(a, b))
1124 fatal("Stream %d: duplicate stats file (from stream %d)",
1125 streami->index, stream->index);
1126 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001127
1128#if CONFIG_FP_MB_STATS
1129 /* Check for two streams sharing a mb stats file. */
1130 if (streami != stream) {
1131 const char *a = stream->config.fpmb_stats_fn;
1132 const char *b = streami->config.fpmb_stats_fn;
1133 if (a && b && !strcmp(a, b))
1134 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1135 streami->index, stream->index);
1136 }
1137#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001138 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001139}
1140
clang-format6c4d83e2016-08-08 19:03:30 -07001141static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001142 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001143 if (!stream->config.cfg.g_w) {
1144 if (!stream->config.cfg.g_h)
1145 stream->config.cfg.g_w = w;
1146 else
1147 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1148 }
1149 if (!stream->config.cfg.g_h) {
1150 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1151 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001152}
1153
clang-format6c4d83e2016-08-08 19:03:30 -07001154static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001155 switch (t) {
1156 case FILE_TYPE_RAW: return "RAW";
1157 case FILE_TYPE_Y4M: return "Y4M";
1158 default: return "Other";
1159 }
1160}
1161
Yaowu Xuf883b422016-08-30 14:01:10 -07001162static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001163 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001164 case AOM_IMG_FMT_I420: return "I420";
1165 case AOM_IMG_FMT_I422: return "I422";
1166 case AOM_IMG_FMT_I444: return "I444";
1167 case AOM_IMG_FMT_I440: return "I440";
1168 case AOM_IMG_FMT_YV12: return "YV12";
1169 case AOM_IMG_FMT_I42016: return "I42016";
1170 case AOM_IMG_FMT_I42216: return "I42216";
1171 case AOM_IMG_FMT_I44416: return "I44416";
1172 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001173 default: return "Other";
1174 }
1175}
Ralph Giles061a16d2012-01-05 15:05:05 -06001176
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001177static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001178 struct AvxEncoderConfig *global,
1179 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001180#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001181 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001182
John Koleszar6ad3b742012-11-06 12:02:42 -08001183 if (stream->index == 0) {
1184 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001185 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001186 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001187 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001188 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001189 }
1190 if (stream->next || stream->index)
1191 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1192 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1193 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001194
John Koleszar6ad3b742012-11-06 12:02:42 -08001195 SHOW(g_usage);
1196 SHOW(g_threads);
1197 SHOW(g_profile);
1198 SHOW(g_w);
1199 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001200 SHOW(g_bit_depth);
1201 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001202 SHOW(g_timebase.num);
1203 SHOW(g_timebase.den);
1204 SHOW(g_error_resilient);
1205 SHOW(g_pass);
1206 SHOW(g_lag_in_frames);
1207 SHOW(rc_dropframe_thresh);
1208 SHOW(rc_resize_allowed);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001209 SHOW(rc_scaled_width);
1210 SHOW(rc_scaled_height);
John Koleszar6ad3b742012-11-06 12:02:42 -08001211 SHOW(rc_resize_up_thresh);
1212 SHOW(rc_resize_down_thresh);
1213 SHOW(rc_end_usage);
1214 SHOW(rc_target_bitrate);
1215 SHOW(rc_min_quantizer);
1216 SHOW(rc_max_quantizer);
1217 SHOW(rc_undershoot_pct);
1218 SHOW(rc_overshoot_pct);
1219 SHOW(rc_buf_sz);
1220 SHOW(rc_buf_initial_sz);
1221 SHOW(rc_buf_optimal_sz);
1222 SHOW(rc_2pass_vbr_bias_pct);
1223 SHOW(rc_2pass_vbr_minsection_pct);
1224 SHOW(rc_2pass_vbr_maxsection_pct);
1225 SHOW(kf_mode);
1226 SHOW(kf_min_dist);
1227 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001228}
1229
John Koleszar9e50ed72012-02-15 12:39:38 -08001230static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001231 struct AvxEncoderConfig *global,
1232 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001233 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001234 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001235
Yaowu Xuf883b422016-08-30 14:01:10 -07001236 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001237
John Koleszar6ad3b742012-11-06 12:02:42 -08001238 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001239
clang-format6c4d83e2016-08-08 19:03:30 -07001240 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001241
John Koleszar6ad3b742012-11-06 12:02:42 -08001242 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1243 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001244
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001245#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001246 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001247 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001248 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1249 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001250 }
James Zernc8e5a772016-02-11 18:53:50 -08001251#else
1252 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001253#endif
1254
1255 if (!stream->config.write_webm) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001256 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1257 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001258}
1259
John Koleszar9e50ed72012-02-15 12:39:38 -08001260static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001261 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001262 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001263
Yaowu Xuf883b422016-08-30 14:01:10 -07001264 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001265
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001266#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001267 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001268 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001269 }
1270#endif
1271
1272 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001273 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001274 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001275 stream->frames_out);
1276 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001277
John Koleszar6ad3b742012-11-06 12:02:42 -08001278 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001279}
1280
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001281static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001282 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001283 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001284 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001285 fatal("Failed to open statistics store");
1286 } else {
1287 if (!stats_open_mem(&stream->stats, pass))
1288 fatal("Failed to open statistics store");
1289 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001290
Pengchong Jinf349b072014-07-14 09:13:38 -07001291#if CONFIG_FP_MB_STATS
1292 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001293 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1294 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001295 fatal("Failed to open mb statistics store");
1296 } else {
1297 if (!stats_open_mem(&stream->fpmb_stats, pass))
1298 fatal("Failed to open mb statistics store");
1299 }
1300#endif
1301
John Koleszar6ad3b742012-11-06 12:02:42 -08001302 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001303 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1304 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001305 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001306 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001307#if CONFIG_FP_MB_STATS
1308 stream->config.cfg.rc_firstpass_mb_stats_in =
1309 stats_get(&stream->fpmb_stats);
1310#endif
1311 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001312
John Koleszar6ad3b742012-11-06 12:02:42 -08001313 stream->cx_time = 0;
1314 stream->nbytes = 0;
1315 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001316}
1317
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001318static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001319 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001320 int i;
1321 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001322
Yaowu Xuf883b422016-08-30 14:01:10 -07001323 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1324 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001325#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001326 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001327#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001328
John Koleszar6ad3b742012-11-06 12:02:42 -08001329 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001330 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001331 &stream->config.cfg, flags);
1332 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001333
Yaowu Xuf883b422016-08-30 14:01:10 -07001334 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001335 * we're being clever to store the control IDs in an array. Real
1336 * applications will want to make use of the enumerations directly
1337 */
1338 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1339 int ctrl = stream->config.arg_ctrls[i][0];
1340 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001341 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001342 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001343
John Koleszar6ad3b742012-11-06 12:02:42 -08001344 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1345 }
1346
John Koleszar5ebe94f2012-12-23 07:20:10 -08001347#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001348 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001349 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1350 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
1351 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001352
Yaowu Xuf883b422016-08-30 14:01:10 -07001353#if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
1354 if (strcmp(global->codec->name, "av1") == 0) {
1355 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001356 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1357
Yaowu Xuf883b422016-08-30 14:01:10 -07001358 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001359 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1360 }
1361#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001362 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001363#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001364}
1365
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001366static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001367 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001368 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001369 aom_codec_pts_t frame_start, next_frame_start;
1370 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1371 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001372
clang-format6c4d83e2016-08-08 19:03:30 -07001373 frame_start =
1374 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1375 cfg->g_timebase.num / global->framerate.num;
1376 next_frame_start =
1377 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1378 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001379
clang-format6c4d83e2016-08-08 19:03:30 -07001380/* Scale if necessary */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001381#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001382 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001383 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001384 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001385 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001386 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1387 exit(EXIT_FAILURE);
1388 }
1389#if CONFIG_LIBYUV
1390 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001391 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001392 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001393 }
clang-format6c4d83e2016-08-08 19:03:30 -07001394 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001395 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1396 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1397 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1398 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1399 stream->img->stride[AOM_PLANE_Y] / 2,
1400 (uint16 *)stream->img->planes[AOM_PLANE_U],
1401 stream->img->stride[AOM_PLANE_U] / 2,
1402 (uint16 *)stream->img->planes[AOM_PLANE_V],
1403 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001404 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001405 img = stream->img;
1406#else
clang-format6c4d83e2016-08-08 19:03:30 -07001407 stream->encoder.err = 1;
1408 ctx_exit_on_error(&stream->encoder,
1409 "Stream %d: Failed to encode frame.\n"
1410 "Scaling disabled in this configuration. \n"
1411 "To enable, configure with --enable-libyuv\n",
1412 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001413#endif
1414 }
1415 }
1416#endif
John Koleszar34882b92012-03-01 12:50:40 -08001417 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001418 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001419 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1420 exit(EXIT_FAILURE);
1421 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001422#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001423 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001424 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001425 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001426 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001427 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1428 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1429 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1430 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1431 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1432 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001433 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001434 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001435#else
1436 stream->encoder.err = 1;
1437 ctx_exit_on_error(&stream->encoder,
1438 "Stream %d: Failed to encode frame.\n"
1439 "Scaling disabled in this configuration. \n"
1440 "To enable, configure with --enable-libyuv\n",
1441 stream->index);
1442#endif
John Koleszar34882b92012-03-01 12:50:40 -08001443 }
1444
Yaowu Xuf883b422016-08-30 14:01:10 -07001445 aom_usec_timer_start(&timer);
1446 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001447 (unsigned long)(next_frame_start - frame_start), 0,
1448 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001449 aom_usec_timer_mark(&timer);
1450 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001451 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1452 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001453}
1454
John Koleszar6ad3b742012-11-06 12:02:42 -08001455static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001456 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001457 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001458
Yaowu Xuf883b422016-08-30 14:01:10 -07001459 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001460 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1461 stream->counts[q]++;
1462 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001463}
1464
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001465static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001466 struct AvxEncoderConfig *global, int *got_data) {
1467 const aom_codec_cx_pkt_t *pkt;
1468 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1469 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001470
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001471 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001472 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001473 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001474 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001475
John Koleszar6ad3b742012-11-06 12:02:42 -08001476 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001477 case AOM_CODEC_CX_FRAME_PKT:
1478 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001479 stream->frames_out++;
1480 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001481 if (!global->quiet)
1482 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001483
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001484 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001485#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001486 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001487 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001488 }
1489#endif
1490 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001491 if (pkt->data.frame.partition_id <= 0) {
1492 ivf_header_pos = ftello(stream->file);
1493 fsize = pkt->data.frame.sz;
1494
Dmitry Kovalev373b0f92014-01-29 17:57:21 -08001495 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001496 } else {
1497 fsize += pkt->data.frame.sz;
1498
Yaowu Xuf883b422016-08-30 14:01:10 -07001499 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
James Zernb6430362017-01-14 14:18:01 -08001500 const FileOffset currpos = ftello(stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001501 fseeko(stream->file, ivf_header_pos, SEEK_SET);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001502 ivf_write_frame_size(stream->file, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001503 fseeko(stream->file, currpos, SEEK_SET);
1504 }
1505 }
1506
clang-format6c4d83e2016-08-08 19:03:30 -07001507 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1508 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001509 }
1510 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001511
John Koleszar9e50ed72012-02-15 12:39:38 -08001512 *got_data = 1;
John Koleszar5ebe94f2012-12-23 07:20:10 -08001513#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001514 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001515 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001516 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001517 if (stream->decoder.err) {
1518 warn_or_exit_on_error(&stream->decoder,
1519 global->test_decode == TEST_DECODE_FATAL,
1520 "Failed to decode frame %d in stream %d",
1521 stream->frames_out + 1, stream->index);
1522 stream->mismatch_seen = stream->frames_out + 1;
1523 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001524 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001525#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001526 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001527 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001528 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001529 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001530 pkt->data.twopass_stats.sz);
1531 stream->nbytes += pkt->data.raw.sz;
1532 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001533#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001534 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001535 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001536 pkt->data.firstpass_mb_stats.sz);
1537 stream->nbytes += pkt->data.raw.sz;
1538 break;
1539#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001540 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001541
1542 if (global->show_psnr) {
1543 int i;
1544
1545 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1546 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1547 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001548 if (!global->quiet)
1549 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001550 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1551 }
1552 stream->psnr_count++;
1553 }
1554
1555 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001556 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001557 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001558 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001559}
1560
clang-format6c4d83e2016-08-08 19:03:30 -07001561static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001562 int i;
1563 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001564
clang-format6c4d83e2016-08-08 19:03:30 -07001565 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001566
John Koleszar6ad3b742012-11-06 12:02:42 -08001567 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001568 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001569 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001570 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001571
John Koleszar6ad3b742012-11-06 12:02:42 -08001572 for (i = 0; i < 4; i++) {
1573 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1574 }
1575 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001576}
1577
John Koleszar25b6e9f2013-02-12 21:17:56 -08001578static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001579 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001580}
1581
clang-format6c4d83e2016-08-08 19:03:30 -07001582static void test_decode(struct stream_state *stream,
John Koleszarb3c350a2013-03-13 12:15:43 -07001583 enum TestDecodeFatality fatal,
Yaowu Xuf883b422016-08-30 14:01:10 -07001584 const AvxInterface *codec) {
1585 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001586
clang-format6c4d83e2016-08-08 19:03:30 -07001587 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001588
John Koleszarb3c350a2013-03-13 12:15:43 -07001589 /* Get the internal reference frame */
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001590 if (strcmp(codec->name, "vp8") == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001591 struct aom_ref_frame ref_enc, ref_dec;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001592 const unsigned int frame_width = (stream->config.cfg.g_w + 15) & ~15;
1593 const unsigned int frame_height = (stream->config.cfg.g_h + 15) & ~15;
1594 aom_img_alloc(&ref_enc.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001595 enc_img = ref_enc.img;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001596 aom_img_alloc(&ref_dec.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001597 dec_img = ref_dec.img;
1598
Yaowu Xuf883b422016-08-30 14:01:10 -07001599 ref_enc.frame_type = AOM_LAST_FRAME;
1600 ref_dec.frame_type = AOM_LAST_FRAME;
1601 aom_codec_control(&stream->encoder, AOM_COPY_REFERENCE, &ref_enc);
1602 aom_codec_control(&stream->decoder, AOM_COPY_REFERENCE, &ref_dec);
John Koleszarb3c350a2013-03-13 12:15:43 -07001603 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001604 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1605 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001606
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001607#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001608 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1609 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1610 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1611 aom_image_t enc_hbd_img;
1612 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001613 enc_img.d_w, enc_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001614 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001615 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001616 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001617 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1618 aom_image_t dec_hbd_img;
1619 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001620 dec_img.d_w, dec_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001621 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001622 dec_img = dec_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001623 }
1624 }
1625#endif
John Koleszarb3c350a2013-03-13 12:15:43 -07001626 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001627 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001628 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001629
Urvang Joshi09c293e2017-04-20 17:56:27 -07001630 if (!aom_compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001631 int y[4], u[4], v[4];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001632#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001633 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001634 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001635 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001636 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001637 }
1638#else
Urvang Joshi09c293e2017-04-20 17:56:27 -07001639 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001640#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001641 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001642 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001643 "Stream %d: Encode/decode mismatch on frame %d at"
1644 " Y[%d, %d] {%d/%d},"
1645 " U[%d, %d] {%d/%d},"
1646 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001647 stream->index, stream->frames_out, y[0], y[1], y[2],
1648 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 -08001649 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001650 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001651
Yaowu Xuf883b422016-08-30 14:01:10 -07001652 aom_img_free(&enc_img);
1653 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001654}
John Koleszarefd54f82012-02-13 16:52:18 -08001655
John Koleszar25b6e9f2013-02-12 21:17:56 -08001656static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001657 int64_t hours;
1658 int64_t mins;
1659 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001660
1661 if (etl >= 0) {
1662 hours = etl / 3600;
1663 etl -= hours * 3600;
1664 mins = etl / 60;
1665 etl -= mins * 60;
1666 secs = etl;
1667
clang-format6c4d83e2016-08-08 19:03:30 -07001668 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1669 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001670 } else {
1671 fprintf(stderr, "[%3s unknown] ", label);
1672 }
1673}
1674
Tom Finegan44dd3272013-11-20 17:18:28 -08001675int main(int argc, const char **argv_) {
1676 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001677 aom_image_t raw;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001678#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001679 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001680 int allocated_raw_shift = 0;
1681 int use_16bit_internal = 0;
1682 int input_shift = 0;
1683#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001684 int frame_avail, got_data;
1685
Yaowu Xuf883b422016-08-30 14:01:10 -07001686 struct AvxInputContext input;
1687 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001688 struct stream_state *streams = NULL;
1689 char **argv, **argi;
1690 uint64_t cx_time = 0;
1691 int stream_cnt = 0;
1692 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001693 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001694
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001695 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001696 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001697
clang-format6c4d83e2016-08-08 19:03:30 -07001698 if (argc < 3) usage_exit();
John Koleszar6ad3b742012-11-06 12:02:42 -08001699
1700 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001701 input.framerate.numerator = 30;
1702 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001703 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001704 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001705
1706 /* First parse the global configuration values, because we want to apply
1707 * other parameters on top of the default configuration provided by the
1708 * codec.
1709 */
1710 argv = argv_dup(argc - 1, argv_ + 1);
1711 parse_global_config(&global, argv);
1712
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001713 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001714 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1715 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1716 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1717 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1718 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001719 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001720
John Koleszar6ad3b742012-11-06 12:02:42 -08001721 {
1722 /* Now parse each stream's parameters. Using a local scope here
1723 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1724 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001725 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001726 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001727
John Koleszar6ad3b742012-11-06 12:02:42 -08001728 do {
1729 stream = new_stream(&global, stream);
1730 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001731 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001732 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001733 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001734
John Koleszarc6b90392012-07-13 15:21:29 -07001735 /* Check for unrecognized options */
1736 for (argi = argv; *argi; argi++)
1737 if (argi[0][0] == '-' && argi[0][1])
1738 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001739
clang-format6c4d83e2016-08-08 19:03:30 -07001740 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1741 &stream->config.cfg););
Tom Finegan249366b2013-11-25 12:05:19 -08001742
John Koleszarc6b90392012-07-13 15:21:29 -07001743 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001744 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001745
clang-format6c4d83e2016-08-08 19:03:30 -07001746 if (!input.filename) usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04001747
John Koleszar8dd82872013-05-06 11:01:35 -07001748 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001749 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001750
John Koleszar6ad3b742012-11-06 12:02:42 -08001751 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001752 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001753 int64_t estimated_time_left = -1;
1754 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001755 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001756
John Koleszar6ad3b742012-11-06 12:02:42 -08001757 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001758
John Koleszar6ad3b742012-11-06 12:02:42 -08001759 /* If the input file doesn't specify its w/h (raw files), try to get
1760 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001761 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001762 if (!input.width || !input.height) {
1763 FOREACH_STREAM({
1764 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1765 input.width = stream->config.cfg.g_w;
1766 input.height = stream->config.cfg.g_h;
1767 break;
1768 }
1769 });
1770 }
John Koleszarc6b90392012-07-13 15:21:29 -07001771
John Koleszar6ad3b742012-11-06 12:02:42 -08001772 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001773 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001774 fatal(
1775 "Specify stream dimensions with --width (-w) "
1776 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001777
1778 /* If input file does not specify bit-depth but input-bit-depth parameter
1779 * exists, assume that to be the input bit-depth. However, if the
1780 * input-bit-depth paramter does not exist, assume the input bit-depth
1781 * to be the same as the codec bit-depth.
1782 */
1783 if (!input.bit_depth) {
1784 FOREACH_STREAM({
1785 if (stream->config.cfg.g_input_bit_depth)
1786 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1787 else
1788 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1789 (int)stream->config.cfg.g_bit_depth;
1790 });
Yaowu Xuf883b422016-08-30 14:01:10 -07001791 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001792 } else {
clang-format6c4d83e2016-08-08 19:03:30 -07001793 FOREACH_STREAM(
1794 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001795 }
1796
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001797#if CONFIG_HIGHBITDEPTH
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001798 FOREACH_STREAM({
Thomas Daede8ec53b22016-09-19 13:24:51 -07001799 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
1800 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
1801 was selected. */
1802 switch (stream->config.cfg.g_profile) {
1803 case 0:
1804 stream->config.cfg.g_profile = 1;
1805 profile_updated = 1;
1806 break;
1807 case 2:
1808 stream->config.cfg.g_profile = 3;
1809 profile_updated = 1;
1810 break;
1811 default: break;
1812 }
1813 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07001814 /* Automatically set the codec bit depth to match the input bit depth.
1815 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001816 if (stream->config.cfg.g_input_bit_depth >
1817 (unsigned int)stream->config.cfg.g_bit_depth) {
1818 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
1819 }
1820 if (stream->config.cfg.g_bit_depth > 8) {
1821 switch (stream->config.cfg.g_profile) {
1822 case 0:
1823 stream->config.cfg.g_profile = 2;
1824 profile_updated = 1;
1825 break;
1826 case 1:
1827 stream->config.cfg.g_profile = 3;
1828 profile_updated = 1;
1829 break;
1830 default: break;
1831 }
1832 }
1833 if (stream->config.cfg.g_profile > 1) {
1834 stream->config.use_16bit_internal = 1;
1835 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08001836 if (profile_updated && !global.quiet) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001837 fprintf(stderr,
1838 "Warning: automatically upgrading to profile %d to "
1839 "match input format.\n",
1840 stream->config.cfg.g_profile);
1841 }
1842 });
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001843#else
1844 FOREACH_STREAM({
1845 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
1846 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
1847 was selected. */
1848 switch (stream->config.cfg.g_profile) {
1849 case 0:
1850 stream->config.cfg.g_profile = 1;
1851 profile_updated = 1;
1852 break;
1853 case 2:
1854 stream->config.cfg.g_profile = 3;
1855 profile_updated = 1;
1856 break;
1857 default: break;
1858 }
1859 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08001860 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001861 fprintf(stderr,
1862 "Warning: automatically upgrading to profile %d to "
1863 "match input format.\n",
1864 stream->config.cfg.g_profile);
1865 }
1866 });
1867#endif
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001868
Tom Finegan00a35aa2013-11-14 12:37:42 -08001869 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
Alex Conversed66bd222014-02-21 10:52:09 -08001870 FOREACH_STREAM(validate_stream_config(stream, &global));
John Koleszar77e6b452010-11-03 13:58:40 -04001871
John Koleszar6ad3b742012-11-06 12:02:42 -08001872 /* Ensure that --passes and --pass are consistent. If --pass is set and
1873 * --passes=2, ensure --fpf was set.
1874 */
1875 if (global.pass && global.passes == 2)
clang-format6c4d83e2016-08-08 19:03:30 -07001876 FOREACH_STREAM({
1877 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07001878 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07001879 " and --passes=2\n",
1880 stream->index, global.pass);
1881 });
John Koleszar3245d462010-06-10 17:10:12 -04001882
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001883#if !CONFIG_WEBM_IO
1884 FOREACH_STREAM({
Ronald S. Bultje39775072015-12-16 13:35:43 -05001885 if (stream->config.write_webm) {
1886 stream->config.write_webm = 0;
clang-format6c4d83e2016-08-08 19:03:30 -07001887 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07001888 "aomenc was compiled without WebM container support."
clang-format6c4d83e2016-08-08 19:03:30 -07001889 "Producing IVF output");
Ronald S. Bultje39775072015-12-16 13:35:43 -05001890 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001891 });
1892#endif
1893
John Koleszar6ad3b742012-11-06 12:02:42 -08001894 /* Use the frame rate from the file only if none was specified
1895 * on the command-line.
1896 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001897 if (!global.have_framerate) {
1898 global.framerate.num = input.framerate.numerator;
1899 global.framerate.den = input.framerate.denominator;
Ronald S. Bultje9134e9f2016-01-18 14:03:45 -05001900 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
1901 stream->config.cfg.g_timebase.num = global.framerate.den);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001902 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04001903
John Koleszar6ad3b742012-11-06 12:02:42 -08001904 /* Show configuration */
1905 if (global.verbose && pass == 0)
1906 FOREACH_STREAM(show_stream_config(stream, &global, &input));
1907
1908 if (pass == (global.pass ? global.pass - 1 : 0)) {
1909 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07001910 /*The Y4M reader does its own allocation.
1911 Just initialize this here to avoid problems if we never read any
1912 frames.*/
1913 memset(&raw, 0, sizeof(raw));
1914 else
Yaowu Xuf883b422016-08-30 14:01:10 -07001915 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04001916
clang-format6c4d83e2016-08-08 19:03:30 -07001917 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
1918 &stream->config.cfg, &global.framerate));
John Koleszar0ea50ce2010-05-18 11:58:33 -04001919 }
1920
John Koleszar6ad3b742012-11-06 12:02:42 -08001921 FOREACH_STREAM(setup_pass(stream, &global, pass));
clang-format6c4d83e2016-08-08 19:03:30 -07001922 FOREACH_STREAM(
1923 open_output_file(stream, &global, &input.pixel_aspect_ratio));
John Koleszar6ad3b742012-11-06 12:02:42 -08001924 FOREACH_STREAM(initialize_encoder(stream, &global));
John Koleszar0ea50ce2010-05-18 11:58:33 -04001925
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001926#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001927 if (strcmp(global.codec->name, "av1") == 0 ||
1928 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001929 // Check to see if at least one stream uses 16 bit internal.
1930 // Currently assume that the bit_depths for all streams using
1931 // highbitdepth are the same.
1932 FOREACH_STREAM({
1933 if (stream->config.use_16bit_internal) {
1934 use_16bit_internal = 1;
1935 }
1936 if (stream->config.cfg.g_profile == 0) {
1937 input_shift = 0;
1938 } else {
1939 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07001940 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001941 }
1942 });
1943 }
1944#endif
1945
John Koleszarc6b90392012-07-13 15:21:29 -07001946 frame_avail = 1;
1947 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001948
John Koleszarc6b90392012-07-13 15:21:29 -07001949 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001950 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001951
John Koleszar6ad3b742012-11-06 12:02:42 -08001952 if (!global.limit || frames_in < global.limit) {
1953 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001954
clang-format6c4d83e2016-08-08 19:03:30 -07001955 if (frame_avail) frames_in++;
1956 seen_frames =
1957 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001958
John Koleszar6ad3b742012-11-06 12:02:42 -08001959 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001960 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001961 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
1962
John Koleszar6ad3b742012-11-06 12:02:42 -08001963 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07001964 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
1965 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08001966 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08001967 fprintf(stderr, "frame %4d ", frames_in);
1968
clang-format6c4d83e2016-08-08 19:03:30 -07001969 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08001970 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07001971 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07001972 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08001973 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04001974 }
1975
hui su251e1512016-10-03 15:48:43 -07001976 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07001977 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07001978 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001979
John Koleszar6ad3b742012-11-06 12:02:42 -08001980 if (frames_in > global.skip_frames) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001981#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001982 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001983 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
1984 assert(use_16bit_internal);
1985 // Input bit depth and stream bit depth do not match, so up
1986 // shift frame to stream bit depth
1987 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001988 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001989 input.width, input.height, 32);
1990 allocated_raw_shift = 1;
1991 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001992 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001993 frame_to_encode = &raw_shift;
1994 } else {
1995 frame_to_encode = &raw;
1996 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001997 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001998 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001999 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002000 FOREACH_STREAM({
2001 if (stream->config.use_16bit_internal)
2002 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002003 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002004 else
2005 assert(0);
2006 });
2007 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002008 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002009 FOREACH_STREAM(encode_frame(stream, &global,
2010 frame_avail ? frame_to_encode : NULL,
2011 frames_in));
2012 }
2013#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002014 aom_usec_timer_start(&timer);
clang-format6c4d83e2016-08-08 19:03:30 -07002015 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
John Koleszar6ad3b742012-11-06 12:02:42 -08002016 frames_in));
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002017#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002018 aom_usec_timer_mark(&timer);
2019 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002020
John Koleszar6ad3b742012-11-06 12:02:42 -08002021 FOREACH_STREAM(update_quantizer_histogram(stream));
John Koleszarc6b90392012-07-13 15:21:29 -07002022
John Koleszar0ea50ce2010-05-18 11:58:33 -04002023 got_data = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002024 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002025
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002026 if (!got_data && input.length && streams != NULL &&
2027 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002028 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002029 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002030 int64_t remaining;
2031 int64_t rate;
2032
2033 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002034 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002035
2036 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002037 remaining = 1000 * (global.limit - global.skip_frames -
2038 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002039 } else {
James Zerne3578af2014-04-17 10:47:08 -07002040 const int64_t input_pos = ftello(input.file);
2041 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002042 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002043
2044 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002045 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002046 }
2047
clang-format6c4d83e2016-08-08 19:03:30 -07002048 average_rate =
2049 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002050 estimated_time_left = average_rate ? remaining / average_rate : -1;
2051 }
2052
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002053 if (got_data && global.test_decode != TEST_DECODE_OFF)
John Koleszarb3c350a2013-03-13 12:15:43 -07002054 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
John Koleszarc6b90392012-07-13 15:21:29 -07002055 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002056
John Koleszarc6b90392012-07-13 15:21:29 -07002057 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002058 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002059 }
2060
clang-format6c4d83e2016-08-08 19:03:30 -07002061 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002062
Deb Mukherjeea160d722014-09-30 21:56:33 -07002063 if (!global.quiet) {
clang-format6c4d83e2016-08-08 19:03:30 -07002064 FOREACH_STREAM(fprintf(
2065 stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2066 "b/f %7" PRId64 "b/s"
2067 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2068 pass + 1, global.passes, frames_in, stream->frames_out,
2069 (int64_t)stream->nbytes,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002070 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
clang-format6c4d83e2016-08-08 19:03:30 -07002071 seen_frames
2072 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2073 global.framerate.den / seen_frames
2074 : 0,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002075 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2076 stream->cx_time > 9999999 ? "ms" : "us",
2077 usec_to_fps(stream->cx_time, seen_frames)));
2078 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002079
Deb Mukherjeea160d722014-09-30 21:56:33 -07002080 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002081 if (global.codec->fourcc == AV1_FOURCC) {
Deb Mukherjeea160d722014-09-30 21:56:33 -07002082 FOREACH_STREAM(
2083 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2084 } else {
2085 FOREACH_STREAM(show_psnr(stream, 255.0));
2086 }
2087 }
John Koleszarc6b90392012-07-13 15:21:29 -07002088
Yaowu Xuf883b422016-08-30 14:01:10 -07002089 FOREACH_STREAM(aom_codec_destroy(&stream->encoder));
John Koleszar6ad3b742012-11-06 12:02:42 -08002090
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002091 if (global.test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002092 FOREACH_STREAM(aom_codec_destroy(&stream->decoder));
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002093 }
2094
John Koleszar6ad3b742012-11-06 12:02:42 -08002095 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002096
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002097 if (global.test_decode == TEST_DECODE_FATAL) {
2098 FOREACH_STREAM(res |= stream->mismatch_seen);
2099 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002100 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002101
John Koleszar6ad3b742012-11-06 12:02:42 -08002102 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
John Koleszarc6b90392012-07-13 15:21:29 -07002103
Pengchong Jinf349b072014-07-14 09:13:38 -07002104#if CONFIG_FP_MB_STATS
2105 FOREACH_STREAM(stats_close(&stream->fpmb_stats, global.passes - 1));
2106#endif
2107
clang-format6c4d83e2016-08-08 19:03:30 -07002108 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002109 }
2110
John Koleszar6ad3b742012-11-06 12:02:42 -08002111 if (global.show_q_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002112 FOREACH_STREAM(
2113 show_q_histogram(stream->counts, global.show_q_hist_buckets));
John Koleszar6ad3b742012-11-06 12:02:42 -08002114
2115 if (global.show_rate_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002116 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
John Koleszar6ad3b742012-11-06 12:02:42 -08002117 global.show_rate_hist_buckets));
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08002118 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
John Koleszar6ad3b742012-11-06 12:02:42 -08002119
Ronald S. Bultje31214972012-10-11 10:13:48 -07002120#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002121 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2122 * to match some existing utilities.
2123 */
Yaowu Xu47e784e2013-10-16 16:29:28 -07002124 if (!(global.pass == 1 && global.passes == 2))
2125 FOREACH_STREAM({
2126 FILE *f = fopen("opsnr.stt", "a");
2127 if (stream->mismatch_seen) {
2128 fprintf(f, "First mismatch occurred in frame %d\n",
2129 stream->mismatch_seen);
2130 } else {
2131 fprintf(f, "No mismatch detected in recon buffers\n");
2132 }
2133 fclose(f);
2134 });
Ronald S. Bultje31214972012-10-11 10:13:48 -07002135#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002136
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002137#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002138 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002139#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002140 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002141 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002142 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002143 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002144}