blob: 6eeb0b83ffae6d73c1d02c36bf903bbb041bf49e [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#include "./aomenc.h"
13#include "./aom_config.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040014
Tom Finegan00a35aa2013-11-14 12:37:42 -080015#include <assert.h>
16#include <limits.h>
Tom Finegan78cb2e62013-11-07 21:28:45 -080017#include <math.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080018#include <stdarg.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040019#include <stdio.h>
20#include <stdlib.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040021#include <string.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080022
James Zern58058812014-08-15 21:00:31 -070023#if CONFIG_LIBYUV
24#include "third_party/libyuv/include/libyuv/scale.h"
25#endif
26
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom/aom_encoder.h"
Tom Fineganba02c242017-05-16 15:01:54 -070028#if CONFIG_AV1_DECODER
Yaowu Xuf883b422016-08-30 14:01:10 -070029#include "aom/aom_decoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080030#endif
John Koleszar6ad3b742012-11-06 12:02:42 -080031
Tom Finegan00a35aa2013-11-14 12:37:42 -080032#include "./args.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080033#include "./ivfenc.h"
Tom Finegan7a691f12014-02-10 14:55:25 -080034#include "./tools_common.h"
Urvang Joshi09c293e2017-04-20 17:56:27 -070035#include "examples/encoder_util.h"
Tom Finegan03848f52013-11-05 10:02:18 -080036
Yaowu Xuf883b422016-08-30 14:01:10 -070037#if CONFIG_AV1_ENCODER
38#include "aom/aomcx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080039#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070040#if CONFIG_AV1_DECODER
41#include "aom/aomdx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080042#endif
43
Yaowu Xuf883b422016-08-30 14:01:10 -070044#include "./aomstats.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080045#include "./rate_hist.h"
Tom Finegan249366b2013-11-25 12:05:19 -080046#include "./warnings.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080047#include "aom/aom_integer.h"
hui suefdad1f2017-05-16 18:01:25 -070048#include "aom_dsp/aom_dsp_common.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080049#include "aom_ports/aom_timer.h"
50#include "aom_ports/mem_ops.h"
James Zerndba73762014-05-10 17:44:12 -070051#if CONFIG_WEBM_IO
Tom Finegan78cb2e62013-11-07 21:28:45 -080052#include "./webmenc.h"
James Zerndba73762014-05-10 17:44:12 -070053#endif
Tom Finegan78cb2e62013-11-07 21:28:45 -080054#include "./y4minput.h"
Tom Finegan03848f52013-11-05 10:02:18 -080055
John Koleszar6291dd42012-06-19 21:05:36 -070056/* Swallow warnings about unused results of fread/fwrite */
clang-format6c4d83e2016-08-08 19:03:30 -070057static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
John Koleszar6ad3b742012-11-06 12:02:42 -080058 return fread(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070059}
60#define fread wrap_fread
61
62static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080063 FILE *stream) {
64 return fwrite(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070065}
66#define fwrite wrap_fwrite
67
John Koleszar0ea50ce2010-05-18 11:58:33 -040068static const char *exec_name;
69
Yaowu Xuf883b422016-08-30 14:01:10 -070070static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080071 const char *s, va_list ap) {
John Koleszarc6b90392012-07-13 15:21:29 -070072 if (ctx->err) {
Yaowu Xuf883b422016-08-30 14:01:10 -070073 const char *detail = aom_codec_error_detail(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -040074
John Koleszar6ad3b742012-11-06 12:02:42 -080075 vfprintf(stderr, s, ap);
Yaowu Xuf883b422016-08-30 14:01:10 -070076 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -040077
clang-format6c4d83e2016-08-08 19:03:30 -070078 if (detail) fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -040079
clang-format6c4d83e2016-08-08 19:03:30 -070080 if (fatal) exit(EXIT_FAILURE);
John Koleszarc6b90392012-07-13 15:21:29 -070081 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040082}
83
Yaowu Xuf883b422016-08-30 14:01:10 -070084static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080085 va_list ap;
86
87 va_start(ap, s);
88 warn_or_exit_on_errorv(ctx, 1, s, ap);
89 va_end(ap);
90}
91
Yaowu Xuf883b422016-08-30 14:01:10 -070092static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080093 const char *s, ...) {
94 va_list ap;
95
96 va_start(ap, s);
97 warn_or_exit_on_errorv(ctx, fatal, s, ap);
98 va_end(ap);
99}
100
Yaowu Xuf883b422016-08-30 14:01:10 -0700101static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800102 FILE *f = input_ctx->file;
103 y4m_input *y4m = &input_ctx->y4m;
John Koleszarc6b90392012-07-13 15:21:29 -0700104 int shortread = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400105
Tom Finegan00a35aa2013-11-14 12:37:42 -0800106 if (input_ctx->file_type == FILE_TYPE_Y4M) {
clang-format6c4d83e2016-08-08 19:03:30 -0700107 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700108 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800109 shortread = read_yuv_frame(input_ctx, img);
John Koleszarc6b90392012-07-13 15:21:29 -0700110 }
111
112 return !shortread;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400113}
114
James Zern5c337fd2015-05-09 10:42:58 -0700115static int file_is_y4m(const char detect[4]) {
John Koleszarc6b90392012-07-13 15:21:29 -0700116 if (memcmp(detect, "YUV4", 4) == 0) {
117 return 1;
118 }
119 return 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400120}
121
James Zern5c337fd2015-05-09 10:42:58 -0700122static int fourcc_is_ivf(const char detect[4]) {
Alex Converse64b89f12014-01-06 16:29:09 -0800123 if (memcmp(detect, "DKIF", 4) == 0) {
124 return 1;
125 }
126 return 0;
127}
John Koleszardc666302010-10-20 12:05:48 -0400128
clang-format6c4d83e2016-08-08 19:03:30 -0700129static const arg_def_t debugmode =
130 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
131static const arg_def_t outputfile =
132 ARG_DEF("o", "output", 1, "Output filename");
133static const arg_def_t use_yv12 =
134 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
135static const arg_def_t use_i420 =
136 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
137static const arg_def_t use_i422 =
138 ARG_DEF(NULL, "i422", 0, "Input file is I422");
139static const arg_def_t use_i444 =
140 ARG_DEF(NULL, "i444", 0, "Input file is I444");
141static const arg_def_t use_i440 =
142 ARG_DEF(NULL, "i440", 0, "Input file is I440");
143static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
144static const arg_def_t passes =
145 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
146static const arg_def_t pass_arg =
147 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
148static const arg_def_t fpf_name =
149 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700150#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700151static const arg_def_t fpmbf_name =
152 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700153#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700154static const arg_def_t limit =
155 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
156static const arg_def_t skip =
157 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
158static const arg_def_t deadline =
159 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
clang-format6c4d83e2016-08-08 19:03:30 -0700160static const arg_def_t good_dl =
161 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
clang-format6c4d83e2016-08-08 19:03:30 -0700162static const arg_def_t quietarg =
163 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
164static const arg_def_t verbosearg =
165 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
166static const arg_def_t psnrarg =
167 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800168
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800169static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700170 { "off", TEST_DECODE_OFF },
171 { "fatal", TEST_DECODE_FATAL },
172 { "warn", TEST_DECODE_WARN },
173 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800174};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700175static const arg_def_t recontest = ARG_DEF_ENUM(
176 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700177static const arg_def_t framerate =
178 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
179static const arg_def_t use_webm =
180 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
181static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
182static const arg_def_t out_part =
183 ARG_DEF("P", "output-partitions", 0,
184 "Makes encoder output partitions. Requires IVF output!");
185static const arg_def_t q_hist_n =
186 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
187static const arg_def_t rate_hist_n =
188 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
189static const arg_def_t disable_warnings =
190 ARG_DEF(NULL, "disable-warnings", 0,
191 "Disable warnings about potentially incorrect encode settings.");
192static const arg_def_t disable_warning_prompt =
193 ARG_DEF("y", "disable-warning-prompt", 0,
194 "Display warnings, but do not prompt user to continue.");
Alex Conversed66bd222014-02-21 10:52:09 -0800195
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200196#if CONFIG_HIGHBITDEPTH
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400197static const struct arg_enum_list bitdepth_enum[] = {
198 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
199};
200
201static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
202 "b", "bit-depth", 1,
203 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
204 bitdepth_enum);
205static const arg_def_t inbitdeptharg =
206 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700207#endif
Tom Finegan44dd3272013-11-20 17:18:28 -0800208
clang-format6c4d83e2016-08-08 19:03:30 -0700209static const arg_def_t *main_args[] = { &debugmode,
210 &outputfile,
211 &codecarg,
212 &passes,
213 &pass_arg,
214 &fpf_name,
215 &limit,
216 &skip,
217 &deadline,
clang-format6c4d83e2016-08-08 19:03:30 -0700218 &good_dl,
clang-format6c4d83e2016-08-08 19:03:30 -0700219 &quietarg,
220 &verbosearg,
221 &psnrarg,
222 &use_webm,
223 &use_ivf,
224 &out_part,
225 &q_hist_n,
226 &rate_hist_n,
227 &disable_warnings,
228 &disable_warning_prompt,
229 &recontest,
230 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400231
clang-format6c4d83e2016-08-08 19:03:30 -0700232static const arg_def_t usage =
233 ARG_DEF("u", "usage", 1, "Usage profile number to use");
234static const arg_def_t threads =
235 ARG_DEF("t", "threads", 1, "Max number of threads to use");
236static const arg_def_t profile =
237 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700238static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
239static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700240#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700241static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700242 { "mono", STEREO_FORMAT_MONO },
243 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
244 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
245 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
246 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
247 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700248};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700249static const arg_def_t stereo_mode = ARG_DEF_ENUM(
250 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700251#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700252static const arg_def_t timebase = ARG_DEF(
253 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700254static const arg_def_t error_resilient =
255 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
256static const arg_def_t lag_in_frames =
257 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400258
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +0200259static const arg_def_t *global_args[] = {
260 &use_yv12, &use_i420, &use_i422, &use_i444, &use_i440,
261 &usage, &threads, &profile, &width, &height,
James Zerndba73762014-05-10 17:44:12 -0700262#if CONFIG_WEBM_IO
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +0200263 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700264#endif
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +0200265 &timebase, &framerate, &error_resilient,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200266#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +0200267 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700268#endif
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +0200269 &lag_in_frames, NULL
270};
John Koleszar0ea50ce2010-05-18 11:58:33 -0400271
clang-format6c4d83e2016-08-08 19:03:30 -0700272static const arg_def_t dropframe_thresh =
273 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700274static const arg_def_t resize_mode =
275 ARG_DEF(NULL, "resize-mode", 1, "Frame resize mode");
276static const arg_def_t resize_numerator =
277 ARG_DEF(NULL, "resize-numerator", 1, "Frame resize numerator");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700278static const arg_def_t resize_kf_numerator =
279 ARG_DEF(NULL, "resize-kf-numerator", 1, "Frame resize keyframe numerator");
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700280#if CONFIG_FRAME_SUPERRES
281static const arg_def_t superres_mode =
282 ARG_DEF(NULL, "superres-mode", 1, "Frame super-resolution mode");
283static const arg_def_t superres_numerator =
284 ARG_DEF(NULL, "superres-numerator", 1, "Frame super-resolution numerator");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700285static const arg_def_t superres_kf_numerator =
286 ARG_DEF(NULL, "superres-kf-numerator", 1,
287 "Frame super-resolution keyframe numerator");
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700288#endif // CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700289static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
290 { "cbr", AOM_CBR },
291 { "cq", AOM_CQ },
292 { "q", AOM_Q },
293 { NULL, 0 } };
clang-format6c4d83e2016-08-08 19:03:30 -0700294static const arg_def_t end_usage =
295 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
296static const arg_def_t target_bitrate =
297 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
298static const arg_def_t min_quantizer =
299 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
300static const arg_def_t max_quantizer =
301 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
302static const arg_def_t undershoot_pct =
303 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
304static const arg_def_t overshoot_pct =
305 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
306static const arg_def_t buf_sz =
307 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
308static const arg_def_t buf_initial_sz =
309 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
310static const arg_def_t buf_optimal_sz =
311 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700312static const arg_def_t *rc_args[] = { &dropframe_thresh,
313 &resize_mode,
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700314 &resize_numerator,
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700315 &resize_kf_numerator,
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700316#if CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700317 &superres_mode,
318 &superres_numerator,
319 &superres_kf_numerator,
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700320#endif // CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700321 &end_usage,
322 &target_bitrate,
323 &min_quantizer,
324 &max_quantizer,
325 &undershoot_pct,
326 &overshoot_pct,
327 &buf_sz,
328 &buf_initial_sz,
329 &buf_optimal_sz,
330 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400331
clang-format6c4d83e2016-08-08 19:03:30 -0700332static const arg_def_t bias_pct =
333 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
334static const arg_def_t minsection_pct =
335 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
336static const arg_def_t maxsection_pct =
337 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
338static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
339 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400340
clang-format6c4d83e2016-08-08 19:03:30 -0700341static const arg_def_t kf_min_dist =
342 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
343static const arg_def_t kf_max_dist =
344 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
345static const arg_def_t kf_disabled =
346 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
347static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
348 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400349
clang-format6c4d83e2016-08-08 19:03:30 -0700350static const arg_def_t noise_sens =
351 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
352static const arg_def_t sharpness =
353 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
354static const arg_def_t static_thresh =
355 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
356static const arg_def_t auto_altref =
357 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
358static const arg_def_t arnr_maxframes =
359 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
360static const arg_def_t arnr_strength =
361 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500362static const struct arg_enum_list tuning_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700363 { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500364};
clang-format6c4d83e2016-08-08 19:03:30 -0700365static const arg_def_t tune_ssim =
366 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
367static const arg_def_t cq_level =
368 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
369static const arg_def_t max_intra_rate_pct =
370 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800371
Yaowu Xuf883b422016-08-30 14:01:10 -0700372#if CONFIG_AV1_ENCODER
373static const arg_def_t cpu_used_av1 =
Yaowu Xu39737592017-04-21 16:39:09 -0700374 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
clang-format6c4d83e2016-08-08 19:03:30 -0700375static const arg_def_t tile_cols =
376 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
377static const arg_def_t tile_rows =
378 ARG_DEF(NULL, "tile-rows", 1,
379 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800380#if CONFIG_EXT_TILE
381static const arg_def_t tile_encoding_mode =
382 ARG_DEF(NULL, "tile-encoding-mode", 1,
383 "Tile encoding mode (0: normal"
384 " (default), 1: vr)");
385#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800386#if CONFIG_DEPENDENT_HORZTILES
387static const arg_def_t tile_dependent_rows =
388 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
389#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800390#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800391static const arg_def_t tile_loopfilter = ARG_DEF(
392 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800393#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700394static const arg_def_t lossless =
395 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700396#if CONFIG_AOM_QM
397static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800398 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700399 "Enable quantisation matrices (0: false (default), 1: true)");
400static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800401 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700402static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800403 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700404#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000405#if CONFIG_TILE_GROUPS
Yaowu Xu859a5272016-11-10 15:32:21 -0800406static const arg_def_t num_tg = ARG_DEF(
407 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000408static const arg_def_t mtu_size =
409 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800410 "MTU size for a tile group, default is 0 (no MTU targeting), "
411 "overrides maximum number of tile groups");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000412#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800413#if CONFIG_TEMPMV_SIGNALING
414static const arg_def_t disable_tempmv = ARG_DEF(
415 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
416#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700417static const arg_def_t frame_parallel_decoding =
418 ARG_DEF(NULL, "frame-parallel", 1,
419 "Enable frame parallel decodability features "
420 "(0: false (default), 1: true)");
Fangwen Fu6160df22017-04-24 09:45:51 -0700421#if CONFIG_DELTA_Q && !CONFIG_EXT_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100422static const arg_def_t aq_mode = ARG_DEF(
423 NULL, "aq-mode", 1,
424 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100425 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200426#else
Thomase28d92b2016-09-20 12:07:11 +0100427static const arg_def_t aq_mode = ARG_DEF(
428 NULL, "aq-mode", 1,
429 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100430 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200431#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700432#if CONFIG_EXT_DELTA_Q
433static const arg_def_t deltaq_mode = ARG_DEF(
434 NULL, "deltaq-mode", 1,
435 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
436#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700437static const arg_def_t frame_periodic_boost =
438 ARG_DEF(NULL, "frame-boost", 1,
439 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700440static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
441 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700442static const arg_def_t max_inter_rate_pct =
443 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700444static const arg_def_t min_gf_interval = ARG_DEF(
445 NULL, "min-gf-interval", 1,
446 "min gf/arf frame interval (default 0, indicating in-built behavior)");
447static const arg_def_t max_gf_interval = ARG_DEF(
448 NULL, "max-gf-interval", 1,
449 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800450
Yaowu Xufc996362015-02-10 15:03:05 -0800451static const struct arg_enum_list color_space_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700452 { "unknown", AOM_CS_UNKNOWN },
453 { "bt601", AOM_CS_BT_601 },
454 { "bt709", AOM_CS_BT_709 },
455 { "smpte170", AOM_CS_SMPTE_170 },
456 { "smpte240", AOM_CS_SMPTE_240 },
anorkin76fb1262017-03-22 15:12:12 -0700457#if CONFIG_COLORSPACE_HEADERS
458 { "bt2020ncl", AOM_CS_BT_2020_NCL },
459 { "bt2020cl", AOM_CS_BT_2020_CL },
460 { "sRGB", AOM_CS_SRGB },
461 { "ICtCp", AOM_CS_ICTCP },
462#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700463 { "bt2020", AOM_CS_BT_2020 },
464 { "reserved", AOM_CS_RESERVED },
465 { "sRGB", AOM_CS_SRGB },
anorkin76fb1262017-03-22 15:12:12 -0700466#endif
Yaowu Xufc996362015-02-10 15:03:05 -0800467 { NULL, 0 }
468};
469
clang-format6c4d83e2016-08-08 19:03:30 -0700470static const arg_def_t input_color_space =
471 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
472 color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800473
anorkin76fb1262017-03-22 15:12:12 -0700474#if CONFIG_COLORSPACE_HEADERS
475static const struct arg_enum_list transfer_function_enum[] = {
476 { "unknown", AOM_TF_UNKNOWN },
477 { "bt709", AOM_TF_BT_709 },
478 { "pq", AOM_TF_PQ },
479 { "hlg", AOM_TF_HLG },
480 { NULL, 0 }
481};
482
483static const arg_def_t input_transfer_function = ARG_DEF_ENUM(
484 NULL, "transfer-function", 1, "The transfer function of input content:",
485 transfer_function_enum);
486
487static const struct arg_enum_list chroma_sample_position_enum[] = {
488 { "unknown", AOM_CSP_UNKNOWN },
489 { "vertical", AOM_CSP_VERTICAL },
490 { "colocated", AOM_CSP_COLOCATED },
491 { NULL, 0 }
492};
493
494static const arg_def_t input_chroma_sample_position =
495 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
496 "The chroma sample position when chroma 4:2:0 is signaled:",
497 chroma_sample_position_enum);
498#endif
499
Alex Converse219f6452014-08-06 11:01:18 -0700500static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700501 { "default", AOM_CONTENT_DEFAULT },
502 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700503 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700504};
505
506static const arg_def_t tune_content = ARG_DEF_ENUM(
507 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700508#endif
Alex Converse219f6452014-08-06 11:01:18 -0700509
Yaowu Xuf883b422016-08-30 14:01:10 -0700510#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000511#if CONFIG_EXT_PARTITION
512static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700513 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
514 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
515 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700516 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000517};
518static const arg_def_t superblock_size = ARG_DEF_ENUM(
519 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
520#endif // CONFIG_EXT_PARTITION
521
Yaowu Xuf883b422016-08-30 14:01:10 -0700522static const arg_def_t *av1_args[] = { &cpu_used_av1,
523 &auto_altref,
524 &sharpness,
525 &static_thresh,
526 &tile_cols,
527 &tile_rows,
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800528#if CONFIG_EXT_TILE
529 &tile_encoding_mode,
530#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800531#if CONFIG_DEPENDENT_HORZTILES
532 &tile_dependent_rows,
533#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800534#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800535 &tile_loopfilter,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800536#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700537 &arnr_maxframes,
538 &arnr_strength,
Yaowu Xuf883b422016-08-30 14:01:10 -0700539 &tune_ssim,
540 &cq_level,
541 &max_intra_rate_pct,
542 &max_inter_rate_pct,
543 &gf_cbr_boost_pct,
544 &lossless,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800545#if CONFIG_AOM_QM
546 &enable_qm,
547 &qm_min,
548 &qm_max,
549#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700550 &frame_parallel_decoding,
551 &aq_mode,
Fangwen Fu6160df22017-04-24 09:45:51 -0700552#if CONFIG_EXT_DELTA_Q
553 &deltaq_mode,
554#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700555 &frame_periodic_boost,
556 &noise_sens,
557 &tune_content,
558 &input_color_space,
anorkin76fb1262017-03-22 15:12:12 -0700559#if CONFIG_COLORSPACE_HEADERS
560 &input_transfer_function,
561 &input_chroma_sample_position,
562#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700563 &min_gf_interval,
564 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000565#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700566 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000567#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000568#if CONFIG_TILE_GROUPS
569 &num_tg,
570 &mtu_size,
571#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800572#if CONFIG_TEMPMV_SIGNALING
573 &disable_tempmv,
574#endif
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200575#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700576 &bitdeptharg,
577 &inbitdeptharg,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200578#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700579 NULL };
580static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
581 AOME_SET_ENABLEAUTOALTREF,
582 AOME_SET_SHARPNESS,
583 AOME_SET_STATIC_THRESHOLD,
584 AV1E_SET_TILE_COLUMNS,
585 AV1E_SET_TILE_ROWS,
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800586#if CONFIG_EXT_TILE
587 AV1E_SET_TILE_ENCODING_MODE,
588#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800589#if CONFIG_DEPENDENT_HORZTILES
590 AV1E_SET_TILE_DEPENDENT_ROWS,
591#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800592#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800593 AV1E_SET_TILE_LOOPFILTER,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800594#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700595 AOME_SET_ARNR_MAXFRAMES,
596 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700597 AOME_SET_TUNING,
598 AOME_SET_CQ_LEVEL,
599 AOME_SET_MAX_INTRA_BITRATE_PCT,
600 AV1E_SET_MAX_INTER_BITRATE_PCT,
601 AV1E_SET_GF_CBR_BOOST_PCT,
602 AV1E_SET_LOSSLESS,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800603#if CONFIG_AOM_QM
604 AV1E_SET_ENABLE_QM,
605 AV1E_SET_QM_MIN,
606 AV1E_SET_QM_MAX,
607#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700608 AV1E_SET_FRAME_PARALLEL_DECODING,
609 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700610#if CONFIG_EXT_DELTA_Q
611 AV1E_SET_DELTAQ_MODE,
612#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700613 AV1E_SET_FRAME_PERIODIC_BOOST,
614 AV1E_SET_NOISE_SENSITIVITY,
615 AV1E_SET_TUNE_CONTENT,
616 AV1E_SET_COLOR_SPACE,
anorkin76fb1262017-03-22 15:12:12 -0700617#if CONFIG_COLORSPACE_HEADERS
618 AV1E_SET_TRANSFER_FUNCTION,
619 AV1E_SET_CHROMA_SAMPLE_POSITION,
620#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700621 AV1E_SET_MIN_GF_INTERVAL,
622 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000623#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700624 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000625#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000626#if CONFIG_TILE_GROUPS
627 AV1E_SET_NUM_TG,
628 AV1E_SET_MTU,
629#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800630#if CONFIG_TEMPMV_SIGNALING
631 AV1E_SET_DISABLE_TEMPMV,
632#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700633 0 };
hui su82331e02015-08-16 18:21:56 -0700634#endif
635
John Koleszar0ea50ce2010-05-18 11:58:33 -0400636static const arg_def_t *no_args[] = { NULL };
637
James Zern59e7a472015-05-09 10:33:26 -0700638void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700639 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700640 const int num_encoder = get_aom_encoder_count();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400641
John Koleszarc6b90392012-07-13 15:21:29 -0700642 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
643 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400644
John Koleszarc6b90392012-07-13 15:21:29 -0700645 fprintf(stderr, "\nOptions:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700646 arg_show_usage(stderr, main_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700647 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700648 arg_show_usage(stderr, global_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700649 fprintf(stderr, "\nRate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700650 arg_show_usage(stderr, rc_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700651 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700652 arg_show_usage(stderr, rc_twopass_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700653 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700654 arg_show_usage(stderr, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700655#if CONFIG_AV1_ENCODER
656 fprintf(stderr, "\nAV1 Specific Options:\n");
657 arg_show_usage(stderr, av1_args);
hui su82331e02015-08-16 18:21:56 -0700658#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700659 fprintf(stderr,
660 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700661 " The desired precision of timestamps in the output, expressed\n"
662 " in fractional seconds. Default is 1/1000.\n");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800663 fprintf(stderr, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400664
Yaowu Xuf990b352015-06-01 09:13:59 -0700665 for (i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700666 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700667 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
668 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700669 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800670 }
Yaowu Xuf990b352015-06-01 09:13:59 -0700671 fprintf(stderr, "\n ");
672 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400673
John Koleszarc6b90392012-07-13 15:21:29 -0700674 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400675}
676
Yaowu Xuf883b422016-08-30 14:01:10 -0700677#if CONFIG_AV1_ENCODER
678#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800679#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800680
James Zerndba73762014-05-10 17:44:12 -0700681#if !CONFIG_WEBM_IO
682typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700683struct WebmOutputContext {
684 int debug;
685};
James Zerndba73762014-05-10 17:44:12 -0700686#endif
687
John Koleszar9e50ed72012-02-15 12:39:38 -0800688/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800689struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700690 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700691 const char *out_fn;
692 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700693#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700694 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700695#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700696 stereo_format_t stereo_fmt;
697 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
698 int arg_ctrl_cnt;
699 int write_webm;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700700 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700701 int use_16bit_internal;
John Koleszar9e50ed72012-02-15 12:39:38 -0800702};
703
John Koleszar6ad3b742012-11-06 12:02:42 -0800704struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700705 int index;
706 struct stream_state *next;
707 struct stream_config config;
708 FILE *file;
709 struct rate_hist *rate_hist;
710 struct WebmOutputContext webm_ctx;
711 uint64_t psnr_sse_total;
712 uint64_t psnr_samples_total;
713 double psnr_totals[4];
714 int psnr_count;
715 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700716 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700717 unsigned int frames_out;
718 uint64_t cx_time;
719 size_t nbytes;
720 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700721#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700722 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700723#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700724 struct aom_image *img;
725 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700726 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800727};
728
clang-format6c4d83e2016-08-08 19:03:30 -0700729static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700730 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800731 if (rat->den < 0) {
732 rat->num *= -1;
733 rat->den *= -1;
734 }
John Koleszar1b27e932012-04-25 17:08:56 -0700735
clang-format6c4d83e2016-08-08 19:03:30 -0700736 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700737
clang-format6c4d83e2016-08-08 19:03:30 -0700738 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700739}
740
Yaowu Xuf883b422016-08-30 14:01:10 -0700741static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700742 char **argi, **argj;
743 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700744 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700745
clang-format6c4d83e2016-08-08 19:03:30 -0700746 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800747
John Koleszar6ad3b742012-11-06 12:02:42 -0800748 /* Initialize default parameters */
749 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700750 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700751 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700752 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700753 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700754 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400755
John Koleszarc6b90392012-07-13 15:21:29 -0700756 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
757 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400758
John Koleszarc6b90392012-07-13 15:21:29 -0700759 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700760 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800761 if (!global->codec)
762 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700763 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800764 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400765
John Koleszar6ad3b742012-11-06 12:02:42 -0800766 if (global->passes < 1 || global->passes > 2)
767 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700768 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800769 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400770
John Koleszar6ad3b742012-11-06 12:02:42 -0800771 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700772 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800773 } else if (arg_match(&arg, &usage, argi))
774 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700775 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800776 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700777 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700778 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar6ad3b742012-11-06 12:02:42 -0800779 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700780 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800781 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700782 global->color_type = I420;
783 else if (arg_match(&arg, &use_i422, argi))
784 global->color_type = I422;
785 else if (arg_match(&arg, &use_i444, argi))
786 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700787 else if (arg_match(&arg, &use_i440, argi))
788 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800789 else if (arg_match(&arg, &quietarg, argi))
790 global->quiet = 1;
791 else if (arg_match(&arg, &verbosearg, argi))
792 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700793 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800794 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700795 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800796 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700797 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800798 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700799 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800800 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700801 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800802 global->framerate = arg_parse_rational(&arg);
803 validate_positive_rational(arg.name, &global->framerate);
804 global->have_framerate = 1;
805 } else if (arg_match(&arg, &out_part, argi))
806 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700807 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800808 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700809 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800810 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700811 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800812 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800813 else if (arg_match(&arg, &disable_warnings, argi))
814 global->disable_warnings = 1;
815 else if (arg_match(&arg, &disable_warning_prompt, argi))
816 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800817 else
John Koleszarc6b90392012-07-13 15:21:29 -0700818 argj++;
819 }
820
John Koleszar6ad3b742012-11-06 12:02:42 -0800821 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700822 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800823 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700824 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
825 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800826 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800827 }
John Koleszarc6b90392012-07-13 15:21:29 -0700828 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800829 /* Validate global config */
830 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700831#if CONFIG_AV1_ENCODER
832 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800833 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700834 if (global->codec != NULL && global->codec->name != NULL)
Thomas Daede80826142017-03-20 15:44:24 -0700835 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800836#else
837 global->passes = 1;
838#endif
839 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800840}
841
Yaowu Xuf883b422016-08-30 14:01:10 -0700842static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800843 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700844 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
845 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -0800846
clang-format6c4d83e2016-08-08 19:03:30 -0700847 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -0800848
John Koleszar25b6e9f2013-02-12 21:17:56 -0800849 if (!fseeko(input->file, 0, SEEK_END)) {
850 /* Input file is seekable. Figure out how long it is, so we can get
851 * progress info.
852 */
853 input->length = ftello(input->file);
854 rewind(input->file);
855 }
856
Frank Galligan09acd262015-06-01 10:20:58 -0700857 /* Default to 1:1 pixel aspect ratio. */
858 input->pixel_aspect_ratio.numerator = 1;
859 input->pixel_aspect_ratio.denominator = 1;
860
John Koleszar6ad3b742012-11-06 12:02:42 -0800861 /* For RAW input sources, these bytes will applied on the first frame
862 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -0700863 */
John Koleszar6ad3b742012-11-06 12:02:42 -0800864 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
865 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400866
clang-format6c4d83e2016-08-08 19:03:30 -0700867 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -0700868 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
869 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800870 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800871 input->width = input->y4m.pic_w;
872 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -0700873 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
874 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800875 input->framerate.numerator = input->y4m.fps_n;
876 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -0700877 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -0700878 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -0800879 } else
880 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -0800881 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -0800882 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -0800883 } else {
884 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700885 }
John Koleszar6ad3b742012-11-06 12:02:42 -0800886}
887
Yaowu Xuf883b422016-08-30 14:01:10 -0700888static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800889 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -0700890 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -0800891}
892
Yaowu Xuf883b422016-08-30 14:01:10 -0700893static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -0800894 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800895 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800896
John Koleszar6ad3b742012-11-06 12:02:42 -0800897 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700898 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800899 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700900 }
901
John Koleszar6ad3b742012-11-06 12:02:42 -0800902 if (prev) {
903 memcpy(stream, prev, sizeof(*stream));
904 stream->index++;
905 prev->next = stream;
906 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700907 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -0800908
John Koleszar6ad3b742012-11-06 12:02:42 -0800909 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -0700910 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -0700911 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -0700912 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -0800913
John Koleszar6ad3b742012-11-06 12:02:42 -0800914 /* Change the default timebase to a high enough value so that the
915 * encoder will always create strictly increasing timestamps.
916 */
917 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -0800918
John Koleszar6ad3b742012-11-06 12:02:42 -0800919 /* Never use the library's default resolution, require it be parsed
920 * from the file or set on the command line.
921 */
922 stream->config.cfg.g_w = 0;
923 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800924
John Koleszar6ad3b742012-11-06 12:02:42 -0800925 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -0800926 stream->config.write_webm = 1;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700927#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -0700928 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700929 stream->webm_ctx.last_pts_ns = -1;
930 stream->webm_ctx.writer = NULL;
931 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700932#endif
Johann87c40b32012-03-01 16:12:53 -0800933
John Koleszar6ad3b742012-11-06 12:02:42 -0800934 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700935 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -0800936 }
John Koleszar9e50ed72012-02-15 12:39:38 -0800937
John Koleszar6ad3b742012-11-06 12:02:42 -0800938 /* Output files must be specified for each stream */
939 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -0800940
John Koleszar6ad3b742012-11-06 12:02:42 -0800941 stream->next = NULL;
942 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800943}
944
Yaowu Xuf883b422016-08-30 14:01:10 -0700945static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -0700946 struct stream_state *stream, char **argv) {
947 char **argi, **argj;
948 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -0800949 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -0700950 static const int *ctrl_args_map = NULL;
951 struct stream_config *config = &stream->config;
952 int eos_mark_found = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800953
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800954 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -0800955 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700956#if CONFIG_AV1_ENCODER
957 } else if (strcmp(global->codec->name, "av1") == 0) {
958 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
959 // Consider to expand this set for AV1 encoder control.
960 ctrl_args = av1_args;
961 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -0700962#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700963 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400964
John Koleszarc6b90392012-07-13 15:21:29 -0700965 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -0700966 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400967
John Koleszar6ad3b742012-11-06 12:02:42 -0800968 /* Once we've found an end-of-stream marker (--) we want to continue
969 * shifting arguments but not consuming them.
970 */
971 if (eos_mark_found) {
972 argj++;
973 continue;
974 } else if (!strcmp(*argj, "--")) {
975 eos_mark_found = 1;
976 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -0800977 }
978
Adrian Granged2401802015-02-13 14:51:32 -0800979 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800980 config->out_fn = arg.val;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800981 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800982 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -0700983#if CONFIG_FP_MB_STATS
984 } else if (arg_match(&arg, &fpmbf_name, argi)) {
985 config->fpmb_stats_fn = arg.val;
986#endif
Johannb50e5182015-01-30 15:05:14 -0800987 } else if (arg_match(&arg, &use_webm, argi)) {
988#if CONFIG_WEBM_IO
989 config->write_webm = 1;
990#else
991 die("Error: --webm specified but webm is disabled.");
992#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800993 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800994 config->write_webm = 0;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800995 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800996 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800997 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800998 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800999 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001000 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001001 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001002 config->cfg.g_h = arg_parse_uint(&arg);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001003#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001004 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1005 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1006 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1007 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1008#endif
James Zerndba73762014-05-10 17:44:12 -07001009#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001010 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001011 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001012#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001013 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001014 config->cfg.g_timebase = arg_parse_rational(&arg);
1015 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001016 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001017 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001018 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001019 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001020 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001021 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001022 } else if (arg_match(&arg, &resize_mode, argi)) {
1023 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
1024 } else if (arg_match(&arg, &resize_numerator, argi)) {
1025 config->cfg.rc_resize_numerator = arg_parse_uint(&arg);
Fergus Simpson7a3f4b32017-06-23 10:32:37 -07001026 } else if (arg_match(&arg, &resize_kf_numerator, argi)) {
1027 config->cfg.rc_resize_kf_numerator = arg_parse_uint(&arg);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001028#if CONFIG_FRAME_SUPERRES
1029 } else if (arg_match(&arg, &superres_mode, argi)) {
1030 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
1031 } else if (arg_match(&arg, &superres_numerator, argi)) {
1032 config->cfg.rc_superres_numerator = arg_parse_uint(&arg);
Fergus Simpson7a3f4b32017-06-23 10:32:37 -07001033 } else if (arg_match(&arg, &superres_kf_numerator, argi)) {
1034 config->cfg.rc_superres_kf_numerator = arg_parse_uint(&arg);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001035#endif // CONFIG_FRAME_SUPERRES
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001036 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001037 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001038 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001039 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001040 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001041 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001042 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001043 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001044 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001045 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001046 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001047 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001048 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001049 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001050 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001051 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001052 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001053 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001054 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001055 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001056 if (global->passes < 2)
1057 warn("option %s ignored in one-pass mode.\n", arg.name);
1058 } else if (arg_match(&arg, &minsection_pct, argi)) {
1059 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1060
1061 if (global->passes < 2)
1062 warn("option %s ignored in one-pass mode.\n", arg.name);
1063 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1064 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1065
1066 if (global->passes < 2)
1067 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001068 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001069 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001070 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001071 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001072 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001073 config->cfg.kf_mode = AOM_KF_DISABLED;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001074 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001075 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001076 for (i = 0; ctrl_args[i]; i++) {
1077 if (arg_match(&arg, ctrl_args[i], argi)) {
1078 int j;
1079 match = 1;
1080
1081 /* Point either to the next free element or the first
1082 * instance of this control.
1083 */
1084 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001085 if (ctrl_args_map != NULL &&
1086 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001087 break;
1088
1089 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001090 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001091 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001092 config->arg_ctrls[j][0] = ctrl_args_map[i];
1093 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001094 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001095 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001096 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001097 }
clang-format6c4d83e2016-08-08 19:03:30 -07001098 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001099 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001100 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001101#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan14af5b92017-05-12 14:27:30 +02001102 config->use_16bit_internal =
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +02001103 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001104#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001105 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001106}
1107
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001108#define FOREACH_STREAM(iterator, list) \
1109 for (struct stream_state *iterator = list; iterator; \
1110 iterator = iterator->next)
John Koleszar9e50ed72012-02-15 12:39:38 -08001111
Alex Conversed66bd222014-02-21 10:52:09 -08001112static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001113 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001114 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001115 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001116
John Koleszar6ad3b742012-11-06 12:02:42 -08001117 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001118 fatal(
1119 "Stream %d: Specify stream dimensions with --width (-w) "
1120 " and --height (-h)",
1121 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001122
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001123 // Check that the codec bit depth is greater than the input bit depth.
1124 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001125 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001126 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1127 stream->index, (int)stream->config.cfg.g_bit_depth,
1128 stream->config.cfg.g_input_bit_depth);
1129 }
1130
John Koleszar6ad3b742012-11-06 12:02:42 -08001131 for (streami = stream; streami; streami = streami->next) {
1132 /* All streams require output files */
1133 if (!streami->config.out_fn)
1134 fatal("Stream %d: Output file is required (specify with -o)",
1135 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001136
John Koleszar6ad3b742012-11-06 12:02:42 -08001137 /* Check for two streams outputting to the same file */
1138 if (streami != stream) {
1139 const char *a = stream->config.out_fn;
1140 const char *b = streami->config.out_fn;
1141 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1142 fatal("Stream %d: duplicate output file (from stream %d)",
1143 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001144 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001145
1146 /* Check for two streams sharing a stats file. */
1147 if (streami != stream) {
1148 const char *a = stream->config.stats_fn;
1149 const char *b = streami->config.stats_fn;
1150 if (a && b && !strcmp(a, b))
1151 fatal("Stream %d: duplicate stats file (from stream %d)",
1152 streami->index, stream->index);
1153 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001154
1155#if CONFIG_FP_MB_STATS
1156 /* Check for two streams sharing a mb stats file. */
1157 if (streami != stream) {
1158 const char *a = stream->config.fpmb_stats_fn;
1159 const char *b = streami->config.fpmb_stats_fn;
1160 if (a && b && !strcmp(a, b))
1161 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1162 streami->index, stream->index);
1163 }
1164#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001165 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001166}
1167
clang-format6c4d83e2016-08-08 19:03:30 -07001168static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001169 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001170 if (!stream->config.cfg.g_w) {
1171 if (!stream->config.cfg.g_h)
1172 stream->config.cfg.g_w = w;
1173 else
1174 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1175 }
1176 if (!stream->config.cfg.g_h) {
1177 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1178 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001179}
1180
clang-format6c4d83e2016-08-08 19:03:30 -07001181static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001182 switch (t) {
1183 case FILE_TYPE_RAW: return "RAW";
1184 case FILE_TYPE_Y4M: return "Y4M";
1185 default: return "Other";
1186 }
1187}
1188
Yaowu Xuf883b422016-08-30 14:01:10 -07001189static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001190 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001191 case AOM_IMG_FMT_I420: return "I420";
1192 case AOM_IMG_FMT_I422: return "I422";
1193 case AOM_IMG_FMT_I444: return "I444";
1194 case AOM_IMG_FMT_I440: return "I440";
1195 case AOM_IMG_FMT_YV12: return "YV12";
1196 case AOM_IMG_FMT_I42016: return "I42016";
1197 case AOM_IMG_FMT_I42216: return "I42216";
1198 case AOM_IMG_FMT_I44416: return "I44416";
1199 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001200 default: return "Other";
1201 }
1202}
Ralph Giles061a16d2012-01-05 15:05:05 -06001203
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001204static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001205 struct AvxEncoderConfig *global,
1206 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001207#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001208 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001209
John Koleszar6ad3b742012-11-06 12:02:42 -08001210 if (stream->index == 0) {
1211 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001212 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001213 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001214 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001215 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001216 }
1217 if (stream->next || stream->index)
1218 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1219 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
Sebastien Alaiwan27511922017-06-05 15:25:46 +02001220 fprintf(stderr, "Coding path: %s\n",
1221 stream->config.use_16bit_internal ? "HBD" : "LBD");
John Koleszar6ad3b742012-11-06 12:02:42 -08001222 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001223
John Koleszar6ad3b742012-11-06 12:02:42 -08001224 SHOW(g_usage);
1225 SHOW(g_threads);
1226 SHOW(g_profile);
1227 SHOW(g_w);
1228 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001229 SHOW(g_bit_depth);
1230 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001231 SHOW(g_timebase.num);
1232 SHOW(g_timebase.den);
1233 SHOW(g_error_resilient);
1234 SHOW(g_pass);
1235 SHOW(g_lag_in_frames);
1236 SHOW(rc_dropframe_thresh);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001237 SHOW(rc_resize_mode);
1238 SHOW(rc_resize_numerator);
Fergus Simpson7a3f4b32017-06-23 10:32:37 -07001239 SHOW(rc_resize_kf_numerator);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001240#if CONFIG_FRAME_SUPERRES
1241 SHOW(rc_superres_mode);
1242 SHOW(rc_superres_numerator);
Fergus Simpson7a3f4b32017-06-23 10:32:37 -07001243 SHOW(rc_superres_kf_numerator);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001244#endif // CONFIG_FRAME_SUPERRES
John Koleszar6ad3b742012-11-06 12:02:42 -08001245 SHOW(rc_end_usage);
1246 SHOW(rc_target_bitrate);
1247 SHOW(rc_min_quantizer);
1248 SHOW(rc_max_quantizer);
1249 SHOW(rc_undershoot_pct);
1250 SHOW(rc_overshoot_pct);
1251 SHOW(rc_buf_sz);
1252 SHOW(rc_buf_initial_sz);
1253 SHOW(rc_buf_optimal_sz);
1254 SHOW(rc_2pass_vbr_bias_pct);
1255 SHOW(rc_2pass_vbr_minsection_pct);
1256 SHOW(rc_2pass_vbr_maxsection_pct);
1257 SHOW(kf_mode);
1258 SHOW(kf_min_dist);
1259 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001260}
1261
John Koleszar9e50ed72012-02-15 12:39:38 -08001262static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001263 struct AvxEncoderConfig *global,
1264 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001265 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001266 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001267
Yaowu Xuf883b422016-08-30 14:01:10 -07001268 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001269
John Koleszar6ad3b742012-11-06 12:02:42 -08001270 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001271
clang-format6c4d83e2016-08-08 19:03:30 -07001272 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001273
John Koleszar6ad3b742012-11-06 12:02:42 -08001274 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1275 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001276
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001277#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001278 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001279 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001280 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1281 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001282 }
James Zernc8e5a772016-02-11 18:53:50 -08001283#else
1284 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001285#endif
1286
1287 if (!stream->config.write_webm) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001288 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1289 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001290}
1291
John Koleszar9e50ed72012-02-15 12:39:38 -08001292static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001293 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001294 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001295
Yaowu Xuf883b422016-08-30 14:01:10 -07001296 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001297
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001298#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001299 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001300 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001301 }
1302#endif
1303
1304 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001305 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001306 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001307 stream->frames_out);
1308 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001309
John Koleszar6ad3b742012-11-06 12:02:42 -08001310 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001311}
1312
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001313static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001314 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001315 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001316 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001317 fatal("Failed to open statistics store");
1318 } else {
1319 if (!stats_open_mem(&stream->stats, pass))
1320 fatal("Failed to open statistics store");
1321 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001322
Pengchong Jinf349b072014-07-14 09:13:38 -07001323#if CONFIG_FP_MB_STATS
1324 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001325 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1326 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001327 fatal("Failed to open mb statistics store");
1328 } else {
1329 if (!stats_open_mem(&stream->fpmb_stats, pass))
1330 fatal("Failed to open mb statistics store");
1331 }
1332#endif
1333
John Koleszar6ad3b742012-11-06 12:02:42 -08001334 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001335 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1336 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001337 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001338 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001339#if CONFIG_FP_MB_STATS
1340 stream->config.cfg.rc_firstpass_mb_stats_in =
1341 stats_get(&stream->fpmb_stats);
1342#endif
1343 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001344
John Koleszar6ad3b742012-11-06 12:02:42 -08001345 stream->cx_time = 0;
1346 stream->nbytes = 0;
1347 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001348}
1349
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001350static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001351 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001352 int i;
1353 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001354
Yaowu Xuf883b422016-08-30 14:01:10 -07001355 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1356 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001357#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001358 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001359#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001360
John Koleszar6ad3b742012-11-06 12:02:42 -08001361 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001362 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001363 &stream->config.cfg, flags);
1364 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001365
Yaowu Xuf883b422016-08-30 14:01:10 -07001366 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001367 * we're being clever to store the control IDs in an array. Real
1368 * applications will want to make use of the enumerations directly
1369 */
1370 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1371 int ctrl = stream->config.arg_ctrls[i][0];
1372 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001373 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001374 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001375
John Koleszar6ad3b742012-11-06 12:02:42 -08001376 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1377 }
1378
Tom Fineganba02c242017-05-16 15:01:54 -07001379#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001380 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001381 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +02001382 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH };
Yaowu Xuf883b422016-08-30 14:01:10 -07001383 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001384
Tom Fineganba02c242017-05-16 15:01:54 -07001385#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07001386 if (strcmp(global->codec->name, "av1") == 0) {
1387 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001388 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1389
Yaowu Xuf883b422016-08-30 14:01:10 -07001390 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001391 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1392 }
1393#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001394 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001395#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001396}
1397
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001398static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001399 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001400 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001401 aom_codec_pts_t frame_start, next_frame_start;
1402 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1403 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001404
clang-format6c4d83e2016-08-08 19:03:30 -07001405 frame_start =
1406 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1407 cfg->g_timebase.num / global->framerate.num;
1408 next_frame_start =
1409 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1410 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001411
clang-format6c4d83e2016-08-08 19:03:30 -07001412/* Scale if necessary */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001413#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001414 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001415 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001416 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001417 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001418 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1419 exit(EXIT_FAILURE);
1420 }
1421#if CONFIG_LIBYUV
1422 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001423 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001424 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001425 }
clang-format6c4d83e2016-08-08 19:03:30 -07001426 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001427 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1428 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1429 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1430 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1431 stream->img->stride[AOM_PLANE_Y] / 2,
1432 (uint16 *)stream->img->planes[AOM_PLANE_U],
1433 stream->img->stride[AOM_PLANE_U] / 2,
1434 (uint16 *)stream->img->planes[AOM_PLANE_V],
1435 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001436 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001437 img = stream->img;
1438#else
clang-format6c4d83e2016-08-08 19:03:30 -07001439 stream->encoder.err = 1;
1440 ctx_exit_on_error(&stream->encoder,
1441 "Stream %d: Failed to encode frame.\n"
1442 "Scaling disabled in this configuration. \n"
1443 "To enable, configure with --enable-libyuv\n",
1444 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001445#endif
1446 }
1447 }
1448#endif
John Koleszar34882b92012-03-01 12:50:40 -08001449 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001450 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001451 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1452 exit(EXIT_FAILURE);
1453 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001454#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001455 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001456 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001457 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001458 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001459 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1460 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1461 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1462 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1463 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1464 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001465 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001466 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001467#else
1468 stream->encoder.err = 1;
1469 ctx_exit_on_error(&stream->encoder,
1470 "Stream %d: Failed to encode frame.\n"
1471 "Scaling disabled in this configuration. \n"
1472 "To enable, configure with --enable-libyuv\n",
1473 stream->index);
1474#endif
John Koleszar34882b92012-03-01 12:50:40 -08001475 }
1476
Yaowu Xuf883b422016-08-30 14:01:10 -07001477 aom_usec_timer_start(&timer);
1478 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001479 (unsigned long)(next_frame_start - frame_start), 0,
1480 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001481 aom_usec_timer_mark(&timer);
1482 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001483 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1484 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001485}
1486
John Koleszar6ad3b742012-11-06 12:02:42 -08001487static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001488 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001489 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001490
Yaowu Xuf883b422016-08-30 14:01:10 -07001491 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001492 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1493 stream->counts[q]++;
1494 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001495}
1496
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001497static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001498 struct AvxEncoderConfig *global, int *got_data) {
1499 const aom_codec_cx_pkt_t *pkt;
1500 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1501 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001502
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001503 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001504 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001505 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001506 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001507
John Koleszar6ad3b742012-11-06 12:02:42 -08001508 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001509 case AOM_CODEC_CX_FRAME_PKT:
1510 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001511 stream->frames_out++;
1512 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001513 if (!global->quiet)
1514 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001515
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001516 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001517#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001518 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001519 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001520 }
1521#endif
1522 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001523 if (pkt->data.frame.partition_id <= 0) {
1524 ivf_header_pos = ftello(stream->file);
1525 fsize = pkt->data.frame.sz;
1526
Dmitry Kovalev373b0f92014-01-29 17:57:21 -08001527 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001528 } else {
1529 fsize += pkt->data.frame.sz;
1530
Yaowu Xuf883b422016-08-30 14:01:10 -07001531 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
James Zernb6430362017-01-14 14:18:01 -08001532 const FileOffset currpos = ftello(stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001533 fseeko(stream->file, ivf_header_pos, SEEK_SET);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001534 ivf_write_frame_size(stream->file, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001535 fseeko(stream->file, currpos, SEEK_SET);
1536 }
1537 }
1538
clang-format6c4d83e2016-08-08 19:03:30 -07001539 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1540 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001541 }
1542 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001543
John Koleszar9e50ed72012-02-15 12:39:38 -08001544 *got_data = 1;
Tom Fineganba02c242017-05-16 15:01:54 -07001545#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001546 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001547 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001548 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001549 if (stream->decoder.err) {
1550 warn_or_exit_on_error(&stream->decoder,
1551 global->test_decode == TEST_DECODE_FATAL,
1552 "Failed to decode frame %d in stream %d",
1553 stream->frames_out + 1, stream->index);
1554 stream->mismatch_seen = stream->frames_out + 1;
1555 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001556 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001557#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001558 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001559 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001560 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001561 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001562 pkt->data.twopass_stats.sz);
1563 stream->nbytes += pkt->data.raw.sz;
1564 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001565#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001566 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001567 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001568 pkt->data.firstpass_mb_stats.sz);
1569 stream->nbytes += pkt->data.raw.sz;
1570 break;
1571#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001572 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001573
1574 if (global->show_psnr) {
1575 int i;
1576
1577 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1578 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1579 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001580 if (!global->quiet)
1581 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001582 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1583 }
1584 stream->psnr_count++;
1585 }
1586
1587 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001588 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001589 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001590 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001591}
1592
clang-format6c4d83e2016-08-08 19:03:30 -07001593static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001594 int i;
1595 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001596
clang-format6c4d83e2016-08-08 19:03:30 -07001597 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001598
John Koleszar6ad3b742012-11-06 12:02:42 -08001599 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001600 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001601 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001602 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001603
John Koleszar6ad3b742012-11-06 12:02:42 -08001604 for (i = 0; i < 4; i++) {
1605 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1606 }
1607 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001608}
1609
John Koleszar25b6e9f2013-02-12 21:17:56 -08001610static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001611 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001612}
1613
clang-format6c4d83e2016-08-08 19:03:30 -07001614static void test_decode(struct stream_state *stream,
John Koleszarb3c350a2013-03-13 12:15:43 -07001615 enum TestDecodeFatality fatal,
Yaowu Xuf883b422016-08-30 14:01:10 -07001616 const AvxInterface *codec) {
1617 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001618
clang-format6c4d83e2016-08-08 19:03:30 -07001619 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001620
John Koleszarb3c350a2013-03-13 12:15:43 -07001621 /* Get the internal reference frame */
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001622 if (strcmp(codec->name, "vp8") == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001623 struct aom_ref_frame ref_enc, ref_dec;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001624 const unsigned int frame_width = (stream->config.cfg.g_w + 15) & ~15;
1625 const unsigned int frame_height = (stream->config.cfg.g_h + 15) & ~15;
1626 aom_img_alloc(&ref_enc.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001627 enc_img = ref_enc.img;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001628 aom_img_alloc(&ref_dec.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001629 dec_img = ref_dec.img;
1630
Yaowu Xuf883b422016-08-30 14:01:10 -07001631 ref_enc.frame_type = AOM_LAST_FRAME;
1632 ref_dec.frame_type = AOM_LAST_FRAME;
1633 aom_codec_control(&stream->encoder, AOM_COPY_REFERENCE, &ref_enc);
1634 aom_codec_control(&stream->decoder, AOM_COPY_REFERENCE, &ref_dec);
John Koleszarb3c350a2013-03-13 12:15:43 -07001635 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001636 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1637 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001638
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001639#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001640 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1641 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1642 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1643 aom_image_t enc_hbd_img;
1644 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001645 enc_img.d_w, enc_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001646 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001647 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001648 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001649 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1650 aom_image_t dec_hbd_img;
1651 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001652 dec_img.d_w, dec_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001653 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001654 dec_img = dec_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001655 }
1656 }
1657#endif
John Koleszarb3c350a2013-03-13 12:15:43 -07001658 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001659 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001660 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001661
Urvang Joshi09c293e2017-04-20 17:56:27 -07001662 if (!aom_compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001663 int y[4], u[4], v[4];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001664#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001665 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001666 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001667 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001668 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001669 }
1670#else
Urvang Joshi09c293e2017-04-20 17:56:27 -07001671 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001672#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001673 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001674 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001675 "Stream %d: Encode/decode mismatch on frame %d at"
1676 " Y[%d, %d] {%d/%d},"
1677 " U[%d, %d] {%d/%d},"
1678 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001679 stream->index, stream->frames_out, y[0], y[1], y[2],
1680 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 -08001681 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001682 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001683
Yaowu Xuf883b422016-08-30 14:01:10 -07001684 aom_img_free(&enc_img);
1685 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001686}
John Koleszarefd54f82012-02-13 16:52:18 -08001687
John Koleszar25b6e9f2013-02-12 21:17:56 -08001688static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001689 int64_t hours;
1690 int64_t mins;
1691 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001692
1693 if (etl >= 0) {
1694 hours = etl / 3600;
1695 etl -= hours * 3600;
1696 mins = etl / 60;
1697 etl -= mins * 60;
1698 secs = etl;
1699
clang-format6c4d83e2016-08-08 19:03:30 -07001700 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1701 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001702 } else {
1703 fprintf(stderr, "[%3s unknown] ", label);
1704 }
1705}
1706
Tom Finegan44dd3272013-11-20 17:18:28 -08001707int main(int argc, const char **argv_) {
1708 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001709 aom_image_t raw;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001710#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001711 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001712 int allocated_raw_shift = 0;
1713 int use_16bit_internal = 0;
1714 int input_shift = 0;
1715#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001716 int frame_avail, got_data;
1717
Yaowu Xuf883b422016-08-30 14:01:10 -07001718 struct AvxInputContext input;
1719 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001720 struct stream_state *streams = NULL;
1721 char **argv, **argi;
1722 uint64_t cx_time = 0;
1723 int stream_cnt = 0;
1724 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001725 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001726
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001727 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001728 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001729
clang-format6c4d83e2016-08-08 19:03:30 -07001730 if (argc < 3) usage_exit();
John Koleszar6ad3b742012-11-06 12:02:42 -08001731
1732 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001733 input.framerate.numerator = 30;
1734 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001735 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001736 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001737
1738 /* First parse the global configuration values, because we want to apply
1739 * other parameters on top of the default configuration provided by the
1740 * codec.
1741 */
1742 argv = argv_dup(argc - 1, argv_ + 1);
1743 parse_global_config(&global, argv);
1744
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001745 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001746 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1747 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1748 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1749 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1750 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001751 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001752
John Koleszar6ad3b742012-11-06 12:02:42 -08001753 {
1754 /* Now parse each stream's parameters. Using a local scope here
1755 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1756 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001757 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001758 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001759
John Koleszar6ad3b742012-11-06 12:02:42 -08001760 do {
1761 stream = new_stream(&global, stream);
1762 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001763 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001764 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001765 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001766
John Koleszarc6b90392012-07-13 15:21:29 -07001767 /* Check for unrecognized options */
1768 for (argi = argv; *argi; argi++)
1769 if (argi[0][0] == '-' && argi[0][1])
1770 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001771
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001772 FOREACH_STREAM(stream, streams) {
1773 check_encoder_config(global.disable_warning_prompt, &global,
1774 &stream->config.cfg);
1775 }
Tom Finegan249366b2013-11-25 12:05:19 -08001776
John Koleszarc6b90392012-07-13 15:21:29 -07001777 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001778 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001779
clang-format6c4d83e2016-08-08 19:03:30 -07001780 if (!input.filename) usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04001781
John Koleszar8dd82872013-05-06 11:01:35 -07001782 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001783 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001784
John Koleszar6ad3b742012-11-06 12:02:42 -08001785 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001786 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001787 int64_t estimated_time_left = -1;
1788 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001789 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001790
John Koleszar6ad3b742012-11-06 12:02:42 -08001791 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001792
John Koleszar6ad3b742012-11-06 12:02:42 -08001793 /* If the input file doesn't specify its w/h (raw files), try to get
1794 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001795 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001796 if (!input.width || !input.height) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001797 FOREACH_STREAM(stream, streams) {
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001798 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1799 input.width = stream->config.cfg.g_w;
1800 input.height = stream->config.cfg.g_h;
1801 break;
1802 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001803 };
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001804 }
John Koleszarc6b90392012-07-13 15:21:29 -07001805
John Koleszar6ad3b742012-11-06 12:02:42 -08001806 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001807 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001808 fatal(
1809 "Specify stream dimensions with --width (-w) "
1810 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001811
1812 /* If input file does not specify bit-depth but input-bit-depth parameter
1813 * exists, assume that to be the input bit-depth. However, if the
1814 * input-bit-depth paramter does not exist, assume the input bit-depth
1815 * to be the same as the codec bit-depth.
1816 */
1817 if (!input.bit_depth) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001818 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001819 if (stream->config.cfg.g_input_bit_depth)
1820 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1821 else
1822 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1823 (int)stream->config.cfg.g_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001824 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001825 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001826 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001827 FOREACH_STREAM(stream, streams) {
1828 stream->config.cfg.g_input_bit_depth = input.bit_depth;
1829 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001830 }
1831
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001832 FOREACH_STREAM(stream, streams) {
Thomas Daede8ec53b22016-09-19 13:24:51 -07001833 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
1834 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
1835 was selected. */
1836 switch (stream->config.cfg.g_profile) {
1837 case 0:
1838 stream->config.cfg.g_profile = 1;
1839 profile_updated = 1;
1840 break;
1841 case 2:
1842 stream->config.cfg.g_profile = 3;
1843 profile_updated = 1;
1844 break;
1845 default: break;
1846 }
1847 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07001848 /* Automatically set the codec bit depth to match the input bit depth.
1849 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001850 if (stream->config.cfg.g_input_bit_depth >
1851 (unsigned int)stream->config.cfg.g_bit_depth) {
1852 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
1853 }
1854 if (stream->config.cfg.g_bit_depth > 8) {
1855 switch (stream->config.cfg.g_profile) {
1856 case 0:
1857 stream->config.cfg.g_profile = 2;
1858 profile_updated = 1;
1859 break;
1860 case 1:
1861 stream->config.cfg.g_profile = 3;
1862 profile_updated = 1;
1863 break;
1864 default: break;
1865 }
1866 }
1867 if (stream->config.cfg.g_profile > 1) {
Sebastien Alaiwanfadc7cb2017-06-08 22:10:43 +02001868 if (!CONFIG_HIGHBITDEPTH) fatal("Unsupported profile.");
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001869 stream->config.use_16bit_internal = 1;
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001870 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08001871 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001872 fprintf(stderr,
1873 "Warning: automatically upgrading to profile %d to "
1874 "match input format.\n",
1875 stream->config.cfg.g_profile);
1876 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001877 }
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001878
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001879 FOREACH_STREAM(stream, streams) {
1880 set_stream_dimensions(stream, input.width, input.height);
1881 }
1882 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
John Koleszar77e6b452010-11-03 13:58:40 -04001883
John Koleszar6ad3b742012-11-06 12:02:42 -08001884 /* Ensure that --passes and --pass are consistent. If --pass is set and
1885 * --passes=2, ensure --fpf was set.
1886 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001887 if (global.pass && global.passes == 2) {
1888 FOREACH_STREAM(stream, streams) {
clang-format6c4d83e2016-08-08 19:03:30 -07001889 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07001890 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07001891 " and --passes=2\n",
1892 stream->index, global.pass);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001893 }
1894 }
John Koleszar3245d462010-06-10 17:10:12 -04001895
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001896#if !CONFIG_WEBM_IO
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001897 FOREACH_STREAM(stream, streams) {
Ronald S. Bultje39775072015-12-16 13:35:43 -05001898 if (stream->config.write_webm) {
1899 stream->config.write_webm = 0;
clang-format6c4d83e2016-08-08 19:03:30 -07001900 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07001901 "aomenc was compiled without WebM container support."
clang-format6c4d83e2016-08-08 19:03:30 -07001902 "Producing IVF output");
Ronald S. Bultje39775072015-12-16 13:35:43 -05001903 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001904 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001905#endif
1906
John Koleszar6ad3b742012-11-06 12:02:42 -08001907 /* Use the frame rate from the file only if none was specified
1908 * on the command-line.
1909 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001910 if (!global.have_framerate) {
1911 global.framerate.num = input.framerate.numerator;
1912 global.framerate.den = input.framerate.denominator;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001913 FOREACH_STREAM(stream, streams) {
1914 stream->config.cfg.g_timebase.den = global.framerate.num;
1915 stream->config.cfg.g_timebase.num = global.framerate.den;
1916 }
Tom Finegan00a35aa2013-11-14 12:37:42 -08001917 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04001918
John Koleszar6ad3b742012-11-06 12:02:42 -08001919 /* Show configuration */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001920 if (global.verbose && pass == 0) {
1921 FOREACH_STREAM(stream, streams) {
1922 show_stream_config(stream, &global, &input);
1923 }
1924 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001925
1926 if (pass == (global.pass ? global.pass - 1 : 0)) {
1927 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07001928 /*The Y4M reader does its own allocation.
1929 Just initialize this here to avoid problems if we never read any
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001930 frames.*/
John Koleszarc6b90392012-07-13 15:21:29 -07001931 memset(&raw, 0, sizeof(raw));
1932 else
Yaowu Xuf883b422016-08-30 14:01:10 -07001933 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04001934
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001935 FOREACH_STREAM(stream, streams) {
1936 stream->rate_hist =
1937 init_rate_histogram(&stream->config.cfg, &global.framerate);
1938 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001939 }
1940
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001941 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
1942 FOREACH_STREAM(stream, streams) {
1943 open_output_file(stream, &global, &input.pixel_aspect_ratio);
1944 }
1945 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001946
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001947#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001948 if (strcmp(global.codec->name, "av1") == 0 ||
1949 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001950 // Check to see if at least one stream uses 16 bit internal.
1951 // Currently assume that the bit_depths for all streams using
1952 // highbitdepth are the same.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001953 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001954 if (stream->config.use_16bit_internal) {
1955 use_16bit_internal = 1;
1956 }
1957 if (stream->config.cfg.g_profile == 0) {
1958 input_shift = 0;
1959 } else {
1960 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07001961 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001962 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001963 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001964 }
1965#endif
1966
John Koleszarc6b90392012-07-13 15:21:29 -07001967 frame_avail = 1;
1968 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001969
John Koleszarc6b90392012-07-13 15:21:29 -07001970 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001971 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001972
John Koleszar6ad3b742012-11-06 12:02:42 -08001973 if (!global.limit || frames_in < global.limit) {
1974 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001975
clang-format6c4d83e2016-08-08 19:03:30 -07001976 if (frame_avail) frames_in++;
1977 seen_frames =
1978 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001979
John Koleszar6ad3b742012-11-06 12:02:42 -08001980 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001981 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001982 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
1983
John Koleszar6ad3b742012-11-06 12:02:42 -08001984 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07001985 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
1986 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08001987 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08001988 fprintf(stderr, "frame %4d ", frames_in);
1989
clang-format6c4d83e2016-08-08 19:03:30 -07001990 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08001991 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07001992 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07001993 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08001994 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04001995 }
1996
hui su251e1512016-10-03 15:48:43 -07001997 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07001998 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07001999 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002000
John Koleszar6ad3b742012-11-06 12:02:42 -08002001 if (frames_in > global.skip_frames) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002002#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002003 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002004 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2005 assert(use_16bit_internal);
2006 // Input bit depth and stream bit depth do not match, so up
2007 // shift frame to stream bit depth
2008 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002009 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002010 input.width, input.height, 32);
2011 allocated_raw_shift = 1;
2012 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002013 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002014 frame_to_encode = &raw_shift;
2015 } else {
2016 frame_to_encode = &raw;
2017 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002018 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002019 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002020 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002021 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002022 if (stream->config.use_16bit_internal)
2023 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002024 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002025 else
2026 assert(0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002027 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002028 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002029 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002030 FOREACH_STREAM(stream, streams) {
2031 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2032 frames_in);
2033 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002034 }
2035#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002036 aom_usec_timer_start(&timer);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002037 FOREACH_STREAM(stream, streams) {
2038 encode_frame(stream, &global, frame_avail ? &raw : NULL, frames_in);
2039 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002040#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002041 aom_usec_timer_mark(&timer);
2042 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002043
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002044 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
John Koleszarc6b90392012-07-13 15:21:29 -07002045
John Koleszar0ea50ce2010-05-18 11:58:33 -04002046 got_data = 0;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002047 FOREACH_STREAM(stream, streams) {
2048 get_cx_data(stream, &global, &got_data);
2049 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002050
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002051 if (!got_data && input.length && streams != NULL &&
2052 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002053 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002054 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002055 int64_t remaining;
2056 int64_t rate;
2057
2058 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002059 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002060
2061 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002062 remaining = 1000 * (global.limit - global.skip_frames -
2063 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002064 } else {
James Zerne3578af2014-04-17 10:47:08 -07002065 const int64_t input_pos = ftello(input.file);
2066 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002067 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002068
2069 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002070 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002071 }
2072
clang-format6c4d83e2016-08-08 19:03:30 -07002073 average_rate =
2074 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002075 estimated_time_left = average_rate ? remaining / average_rate : -1;
2076 }
2077
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002078 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2079 FOREACH_STREAM(stream, streams) {
2080 test_decode(stream, global.test_decode, global.codec);
2081 }
2082 }
John Koleszarc6b90392012-07-13 15:21:29 -07002083 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002084
John Koleszarc6b90392012-07-13 15:21:29 -07002085 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002086 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002087 }
2088
clang-format6c4d83e2016-08-08 19:03:30 -07002089 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002090
Deb Mukherjeea160d722014-09-30 21:56:33 -07002091 if (!global.quiet) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002092 FOREACH_STREAM(stream, streams) {
2093 fprintf(stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2094 "b/f %7" PRId64
2095 "b/s"
2096 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2097 pass + 1, global.passes, frames_in, stream->frames_out,
2098 (int64_t)stream->nbytes,
2099 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2100 seen_frames
2101 ? (int64_t)stream->nbytes * 8 *
2102 (int64_t)global.framerate.num / global.framerate.den /
2103 seen_frames
2104 : 0,
2105 stream->cx_time > 9999999 ? stream->cx_time / 1000
2106 : stream->cx_time,
2107 stream->cx_time > 9999999 ? "ms" : "us",
2108 usec_to_fps(stream->cx_time, seen_frames));
2109 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002110 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002111
Deb Mukherjeea160d722014-09-30 21:56:33 -07002112 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002113 if (global.codec->fourcc == AV1_FOURCC) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002114 FOREACH_STREAM(stream, streams) {
2115 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2116 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002117 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002118 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002119 }
2120 }
John Koleszarc6b90392012-07-13 15:21:29 -07002121
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002122 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002123
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002124 if (global.test_decode != TEST_DECODE_OFF) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002125 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002126 }
2127
John Koleszar6ad3b742012-11-06 12:02:42 -08002128 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002129
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002130 if (global.test_decode == TEST_DECODE_FATAL) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002131 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002132 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002133 FOREACH_STREAM(stream, streams) {
2134 close_output_file(stream, global.codec->fourcc);
2135 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002136
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002137 FOREACH_STREAM(stream, streams) {
2138 stats_close(&stream->stats, global.passes - 1);
2139 }
John Koleszarc6b90392012-07-13 15:21:29 -07002140
Pengchong Jinf349b072014-07-14 09:13:38 -07002141#if CONFIG_FP_MB_STATS
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002142 FOREACH_STREAM(stream, streams) {
2143 stats_close(&stream->fpmb_stats, global.passes - 1);
2144 }
Pengchong Jinf349b072014-07-14 09:13:38 -07002145#endif
2146
clang-format6c4d83e2016-08-08 19:03:30 -07002147 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002148 }
2149
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002150 if (global.show_q_hist_buckets) {
2151 FOREACH_STREAM(stream, streams) {
2152 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2153 }
2154 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002155
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002156 if (global.show_rate_hist_buckets) {
2157 FOREACH_STREAM(stream, streams) {
2158 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2159 global.show_rate_hist_buckets);
2160 }
2161 }
2162 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002163
Ronald S. Bultje31214972012-10-11 10:13:48 -07002164#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002165 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2166 * to match some existing utilities.
2167 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002168 if (!(global.pass == 1 && global.passes == 2)) {
2169 FOREACH_STREAM(stream, streams) {
Yaowu Xu47e784e2013-10-16 16:29:28 -07002170 FILE *f = fopen("opsnr.stt", "a");
2171 if (stream->mismatch_seen) {
2172 fprintf(f, "First mismatch occurred in frame %d\n",
2173 stream->mismatch_seen);
2174 } else {
2175 fprintf(f, "No mismatch detected in recon buffers\n");
2176 }
2177 fclose(f);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002178 }
2179 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002180#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002181
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002182#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002183 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002184#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002185 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002186 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002187 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002188 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002189}