blob: 27372712e2f9e2536bd96e0883c883a9e8e95627 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#include "./aomenc.h"
13#include "./aom_config.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040014
Tom Finegan00a35aa2013-11-14 12:37:42 -080015#include <assert.h>
16#include <limits.h>
Tom Finegan78cb2e62013-11-07 21:28:45 -080017#include <math.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080018#include <stdarg.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040019#include <stdio.h>
20#include <stdlib.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040021#include <string.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080022
James Zern58058812014-08-15 21:00:31 -070023#if CONFIG_LIBYUV
24#include "third_party/libyuv/include/libyuv/scale.h"
25#endif
26
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom/aom_encoder.h"
Tom Fineganba02c242017-05-16 15:01:54 -070028#if CONFIG_AV1_DECODER
Yaowu Xuf883b422016-08-30 14:01:10 -070029#include "aom/aom_decoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080030#endif
John Koleszar6ad3b742012-11-06 12:02:42 -080031
Tom Finegan00a35aa2013-11-14 12:37:42 -080032#include "./args.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080033#include "./ivfenc.h"
Tom Finegan7a691f12014-02-10 14:55:25 -080034#include "./tools_common.h"
Urvang Joshi09c293e2017-04-20 17:56:27 -070035#include "examples/encoder_util.h"
Tom Finegan03848f52013-11-05 10:02:18 -080036
Yaowu Xuf883b422016-08-30 14:01:10 -070037#if CONFIG_AV1_ENCODER
38#include "aom/aomcx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080039#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070040#if CONFIG_AV1_DECODER
41#include "aom/aomdx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080042#endif
43
Yaowu Xuf883b422016-08-30 14:01:10 -070044#include "./aomstats.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080045#include "./rate_hist.h"
Tom Finegan249366b2013-11-25 12:05:19 -080046#include "./warnings.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080047#include "aom/aom_integer.h"
hui suefdad1f2017-05-16 18:01:25 -070048#include "aom_dsp/aom_dsp_common.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080049#include "aom_ports/aom_timer.h"
50#include "aom_ports/mem_ops.h"
James Zerndba73762014-05-10 17:44:12 -070051#if CONFIG_WEBM_IO
Tom Finegan78cb2e62013-11-07 21:28:45 -080052#include "./webmenc.h"
James Zerndba73762014-05-10 17:44:12 -070053#endif
Tom Finegan78cb2e62013-11-07 21:28:45 -080054#include "./y4minput.h"
Tom Finegan03848f52013-11-05 10:02:18 -080055
John Koleszar6291dd42012-06-19 21:05:36 -070056/* Swallow warnings about unused results of fread/fwrite */
clang-format6c4d83e2016-08-08 19:03:30 -070057static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
John Koleszar6ad3b742012-11-06 12:02:42 -080058 return fread(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070059}
60#define fread wrap_fread
61
62static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080063 FILE *stream) {
64 return fwrite(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070065}
66#define fwrite wrap_fwrite
67
John Koleszar0ea50ce2010-05-18 11:58:33 -040068static const char *exec_name;
69
Yaowu Xuf883b422016-08-30 14:01:10 -070070static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080071 const char *s, va_list ap) {
John Koleszarc6b90392012-07-13 15:21:29 -070072 if (ctx->err) {
Yaowu Xuf883b422016-08-30 14:01:10 -070073 const char *detail = aom_codec_error_detail(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -040074
John Koleszar6ad3b742012-11-06 12:02:42 -080075 vfprintf(stderr, s, ap);
Yaowu Xuf883b422016-08-30 14:01:10 -070076 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -040077
clang-format6c4d83e2016-08-08 19:03:30 -070078 if (detail) fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -040079
clang-format6c4d83e2016-08-08 19:03:30 -070080 if (fatal) exit(EXIT_FAILURE);
John Koleszarc6b90392012-07-13 15:21:29 -070081 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040082}
83
Yaowu Xuf883b422016-08-30 14:01:10 -070084static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080085 va_list ap;
86
87 va_start(ap, s);
88 warn_or_exit_on_errorv(ctx, 1, s, ap);
89 va_end(ap);
90}
91
Yaowu Xuf883b422016-08-30 14:01:10 -070092static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080093 const char *s, ...) {
94 va_list ap;
95
96 va_start(ap, s);
97 warn_or_exit_on_errorv(ctx, fatal, s, ap);
98 va_end(ap);
99}
100
Yaowu Xuf883b422016-08-30 14:01:10 -0700101static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800102 FILE *f = input_ctx->file;
103 y4m_input *y4m = &input_ctx->y4m;
John Koleszarc6b90392012-07-13 15:21:29 -0700104 int shortread = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400105
Tom Finegan00a35aa2013-11-14 12:37:42 -0800106 if (input_ctx->file_type == FILE_TYPE_Y4M) {
clang-format6c4d83e2016-08-08 19:03:30 -0700107 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700108 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800109 shortread = read_yuv_frame(input_ctx, img);
John Koleszarc6b90392012-07-13 15:21:29 -0700110 }
111
112 return !shortread;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400113}
114
James Zern5c337fd2015-05-09 10:42:58 -0700115static int file_is_y4m(const char detect[4]) {
John Koleszarc6b90392012-07-13 15:21:29 -0700116 if (memcmp(detect, "YUV4", 4) == 0) {
117 return 1;
118 }
119 return 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400120}
121
James Zern5c337fd2015-05-09 10:42:58 -0700122static int fourcc_is_ivf(const char detect[4]) {
Alex Converse64b89f12014-01-06 16:29:09 -0800123 if (memcmp(detect, "DKIF", 4) == 0) {
124 return 1;
125 }
126 return 0;
127}
John Koleszardc666302010-10-20 12:05:48 -0400128
clang-format6c4d83e2016-08-08 19:03:30 -0700129static const arg_def_t debugmode =
130 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
131static const arg_def_t outputfile =
132 ARG_DEF("o", "output", 1, "Output filename");
133static const arg_def_t use_yv12 =
134 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
135static const arg_def_t use_i420 =
136 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
137static const arg_def_t use_i422 =
138 ARG_DEF(NULL, "i422", 0, "Input file is I422");
139static const arg_def_t use_i444 =
140 ARG_DEF(NULL, "i444", 0, "Input file is I444");
141static const arg_def_t use_i440 =
142 ARG_DEF(NULL, "i440", 0, "Input file is I440");
143static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
144static const arg_def_t passes =
145 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
146static const arg_def_t pass_arg =
147 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
148static const arg_def_t fpf_name =
149 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700150#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700151static const arg_def_t fpmbf_name =
152 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700153#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700154static const arg_def_t limit =
155 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
156static const arg_def_t skip =
157 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
158static const arg_def_t deadline =
159 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
clang-format6c4d83e2016-08-08 19:03:30 -0700160static const arg_def_t good_dl =
161 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
clang-format6c4d83e2016-08-08 19:03:30 -0700162static const arg_def_t quietarg =
163 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
164static const arg_def_t verbosearg =
165 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
166static const arg_def_t psnrarg =
167 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800168
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800169static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700170 { "off", TEST_DECODE_OFF },
171 { "fatal", TEST_DECODE_FATAL },
172 { "warn", TEST_DECODE_WARN },
173 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800174};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700175static const arg_def_t recontest = ARG_DEF_ENUM(
176 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700177static const arg_def_t framerate =
178 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
179static const arg_def_t use_webm =
180 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
181static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
182static const arg_def_t out_part =
183 ARG_DEF("P", "output-partitions", 0,
184 "Makes encoder output partitions. Requires IVF output!");
185static const arg_def_t q_hist_n =
186 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
187static const arg_def_t rate_hist_n =
188 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
189static const arg_def_t disable_warnings =
190 ARG_DEF(NULL, "disable-warnings", 0,
191 "Disable warnings about potentially incorrect encode settings.");
192static const arg_def_t disable_warning_prompt =
193 ARG_DEF("y", "disable-warning-prompt", 0,
194 "Display warnings, but do not prompt user to continue.");
Alex Conversed66bd222014-02-21 10:52:09 -0800195
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200196#if CONFIG_HIGHBITDEPTH
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400197static const struct arg_enum_list bitdepth_enum[] = {
198 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
199};
200
201static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
202 "b", "bit-depth", 1,
203 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
204 bitdepth_enum);
205static const arg_def_t inbitdeptharg =
206 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700207#endif
Tom Finegan44dd3272013-11-20 17:18:28 -0800208
clang-format6c4d83e2016-08-08 19:03:30 -0700209static const arg_def_t *main_args[] = { &debugmode,
210 &outputfile,
211 &codecarg,
212 &passes,
213 &pass_arg,
214 &fpf_name,
215 &limit,
216 &skip,
217 &deadline,
clang-format6c4d83e2016-08-08 19:03:30 -0700218 &good_dl,
clang-format6c4d83e2016-08-08 19:03:30 -0700219 &quietarg,
220 &verbosearg,
221 &psnrarg,
222 &use_webm,
223 &use_ivf,
224 &out_part,
225 &q_hist_n,
226 &rate_hist_n,
227 &disable_warnings,
228 &disable_warning_prompt,
229 &recontest,
230 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400231
clang-format6c4d83e2016-08-08 19:03:30 -0700232static const arg_def_t usage =
233 ARG_DEF("u", "usage", 1, "Usage profile number to use");
234static const arg_def_t threads =
235 ARG_DEF("t", "threads", 1, "Max number of threads to use");
236static const arg_def_t profile =
237 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700238static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
239static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700240#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700241static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700242 { "mono", STEREO_FORMAT_MONO },
243 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
244 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
245 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
246 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
247 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700248};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700249static const arg_def_t stereo_mode = ARG_DEF_ENUM(
250 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700251#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700252static const arg_def_t timebase = ARG_DEF(
253 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700254static const arg_def_t error_resilient =
255 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
256static const arg_def_t lag_in_frames =
257 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700258#if CONFIG_EXT_TILE
259static const arg_def_t large_scale_tile =
260 ARG_DEF(NULL, "large-scale-tile", 1,
261 "Large scale tile coding (0: off (default), 1: on)");
262#endif // CONFIG_EXT_TILE
John Koleszar0ea50ce2010-05-18 11:58:33 -0400263
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700264static const arg_def_t *global_args[] = { &use_yv12,
265 &use_i420,
266 &use_i422,
267 &use_i444,
268 &use_i440,
269 &usage,
270 &threads,
271 &profile,
272 &width,
273 &height,
James Zerndba73762014-05-10 17:44:12 -0700274#if CONFIG_WEBM_IO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700275 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700276#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700277 &timebase,
278 &framerate,
279 &error_resilient,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200280#if CONFIG_HIGHBITDEPTH
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700281 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700282#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700283 &lag_in_frames,
284#if CONFIG_EXT_TILE
285 &large_scale_tile,
286#endif // CONFIG_EXT_TILE
287 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400288
clang-format6c4d83e2016-08-08 19:03:30 -0700289static const arg_def_t dropframe_thresh =
290 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700291static const arg_def_t resize_mode =
292 ARG_DEF(NULL, "resize-mode", 1, "Frame resize mode");
293static const arg_def_t resize_numerator =
294 ARG_DEF(NULL, "resize-numerator", 1, "Frame resize numerator");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700295static const arg_def_t resize_kf_numerator =
296 ARG_DEF(NULL, "resize-kf-numerator", 1, "Frame resize keyframe numerator");
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700297#if CONFIG_FRAME_SUPERRES
298static const arg_def_t superres_mode =
299 ARG_DEF(NULL, "superres-mode", 1, "Frame super-resolution mode");
300static const arg_def_t superres_numerator =
301 ARG_DEF(NULL, "superres-numerator", 1, "Frame super-resolution numerator");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700302static const arg_def_t superres_kf_numerator =
303 ARG_DEF(NULL, "superres-kf-numerator", 1,
304 "Frame super-resolution keyframe numerator");
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700305#endif // CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700306static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
307 { "cbr", AOM_CBR },
308 { "cq", AOM_CQ },
309 { "q", AOM_Q },
310 { NULL, 0 } };
clang-format6c4d83e2016-08-08 19:03:30 -0700311static const arg_def_t end_usage =
312 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
313static const arg_def_t target_bitrate =
314 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
315static const arg_def_t min_quantizer =
316 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
317static const arg_def_t max_quantizer =
318 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
319static const arg_def_t undershoot_pct =
320 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
321static const arg_def_t overshoot_pct =
322 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
323static const arg_def_t buf_sz =
324 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
325static const arg_def_t buf_initial_sz =
326 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
327static const arg_def_t buf_optimal_sz =
328 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700329static const arg_def_t *rc_args[] = { &dropframe_thresh,
330 &resize_mode,
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700331 &resize_numerator,
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700332 &resize_kf_numerator,
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700333#if CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700334 &superres_mode,
335 &superres_numerator,
336 &superres_kf_numerator,
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700337#endif // CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700338 &end_usage,
339 &target_bitrate,
340 &min_quantizer,
341 &max_quantizer,
342 &undershoot_pct,
343 &overshoot_pct,
344 &buf_sz,
345 &buf_initial_sz,
346 &buf_optimal_sz,
347 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400348
clang-format6c4d83e2016-08-08 19:03:30 -0700349static const arg_def_t bias_pct =
350 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
351static const arg_def_t minsection_pct =
352 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
353static const arg_def_t maxsection_pct =
354 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
355static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
356 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400357
clang-format6c4d83e2016-08-08 19:03:30 -0700358static const arg_def_t kf_min_dist =
359 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
360static const arg_def_t kf_max_dist =
361 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
362static const arg_def_t kf_disabled =
363 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
364static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
365 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400366
clang-format6c4d83e2016-08-08 19:03:30 -0700367static const arg_def_t noise_sens =
368 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
369static const arg_def_t sharpness =
370 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
371static const arg_def_t static_thresh =
372 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
373static const arg_def_t auto_altref =
374 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
375static const arg_def_t arnr_maxframes =
376 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
377static const arg_def_t arnr_strength =
378 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500379static const struct arg_enum_list tuning_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700380 { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500381};
clang-format6c4d83e2016-08-08 19:03:30 -0700382static const arg_def_t tune_ssim =
383 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
384static const arg_def_t cq_level =
385 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
386static const arg_def_t max_intra_rate_pct =
387 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800388
Yaowu Xuf883b422016-08-30 14:01:10 -0700389#if CONFIG_AV1_ENCODER
390static const arg_def_t cpu_used_av1 =
Yaowu Xu39737592017-04-21 16:39:09 -0700391 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700392#if CONFIG_EXT_TILE
393static const arg_def_t single_tile_decoding =
394 ARG_DEF(NULL, "single-tile-decoding", 1,
395 "Single tile decoding (0: off (default), 1: on)");
396#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700397static const arg_def_t tile_cols =
398 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
399static const arg_def_t tile_rows =
400 ARG_DEF(NULL, "tile-rows", 1,
401 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800402#if CONFIG_DEPENDENT_HORZTILES
403static const arg_def_t tile_dependent_rows =
404 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
405#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800406#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800407static const arg_def_t tile_loopfilter = ARG_DEF(
408 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800409#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700410static const arg_def_t lossless =
411 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700412#if CONFIG_AOM_QM
413static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800414 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700415 "Enable quantisation matrices (0: false (default), 1: true)");
416static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800417 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700418static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800419 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700420#endif
Yaowu Xu859a5272016-11-10 15:32:21 -0800421static const arg_def_t num_tg = ARG_DEF(
422 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000423static const arg_def_t mtu_size =
424 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800425 "MTU size for a tile group, default is 0 (no MTU targeting), "
426 "overrides maximum number of tile groups");
Fangwen Fu8d164de2016-12-14 13:40:54 -0800427#if CONFIG_TEMPMV_SIGNALING
428static const arg_def_t disable_tempmv = ARG_DEF(
429 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
430#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700431static const arg_def_t frame_parallel_decoding =
432 ARG_DEF(NULL, "frame-parallel", 1,
433 "Enable frame parallel decodability features "
434 "(0: false (default), 1: true)");
Fangwen Fu6160df22017-04-24 09:45:51 -0700435#if CONFIG_DELTA_Q && !CONFIG_EXT_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100436static const arg_def_t aq_mode = ARG_DEF(
437 NULL, "aq-mode", 1,
438 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100439 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200440#else
Thomase28d92b2016-09-20 12:07:11 +0100441static const arg_def_t aq_mode = ARG_DEF(
442 NULL, "aq-mode", 1,
443 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100444 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200445#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700446#if CONFIG_EXT_DELTA_Q
447static const arg_def_t deltaq_mode = ARG_DEF(
448 NULL, "deltaq-mode", 1,
449 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
450#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700451static const arg_def_t frame_periodic_boost =
452 ARG_DEF(NULL, "frame-boost", 1,
453 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700454static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
455 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700456static const arg_def_t max_inter_rate_pct =
457 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700458static const arg_def_t min_gf_interval = ARG_DEF(
459 NULL, "min-gf-interval", 1,
460 "min gf/arf frame interval (default 0, indicating in-built behavior)");
461static const arg_def_t max_gf_interval = ARG_DEF(
462 NULL, "max-gf-interval", 1,
463 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800464
Yaowu Xufc996362015-02-10 15:03:05 -0800465static const struct arg_enum_list color_space_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700466 { "unknown", AOM_CS_UNKNOWN },
467 { "bt601", AOM_CS_BT_601 },
468 { "bt709", AOM_CS_BT_709 },
469 { "smpte170", AOM_CS_SMPTE_170 },
470 { "smpte240", AOM_CS_SMPTE_240 },
anorkin76fb1262017-03-22 15:12:12 -0700471#if CONFIG_COLORSPACE_HEADERS
472 { "bt2020ncl", AOM_CS_BT_2020_NCL },
473 { "bt2020cl", AOM_CS_BT_2020_CL },
474 { "sRGB", AOM_CS_SRGB },
475 { "ICtCp", AOM_CS_ICTCP },
476#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700477 { "bt2020", AOM_CS_BT_2020 },
478 { "reserved", AOM_CS_RESERVED },
479 { "sRGB", AOM_CS_SRGB },
anorkin76fb1262017-03-22 15:12:12 -0700480#endif
Yaowu Xufc996362015-02-10 15:03:05 -0800481 { NULL, 0 }
482};
483
clang-format6c4d83e2016-08-08 19:03:30 -0700484static const arg_def_t input_color_space =
485 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
486 color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800487
anorkin76fb1262017-03-22 15:12:12 -0700488#if CONFIG_COLORSPACE_HEADERS
489static const struct arg_enum_list transfer_function_enum[] = {
490 { "unknown", AOM_TF_UNKNOWN },
491 { "bt709", AOM_TF_BT_709 },
492 { "pq", AOM_TF_PQ },
493 { "hlg", AOM_TF_HLG },
494 { NULL, 0 }
495};
496
497static const arg_def_t input_transfer_function = ARG_DEF_ENUM(
498 NULL, "transfer-function", 1, "The transfer function of input content:",
499 transfer_function_enum);
500
501static const struct arg_enum_list chroma_sample_position_enum[] = {
502 { "unknown", AOM_CSP_UNKNOWN },
503 { "vertical", AOM_CSP_VERTICAL },
504 { "colocated", AOM_CSP_COLOCATED },
505 { NULL, 0 }
506};
507
508static const arg_def_t input_chroma_sample_position =
509 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
510 "The chroma sample position when chroma 4:2:0 is signaled:",
511 chroma_sample_position_enum);
512#endif
513
Alex Converse219f6452014-08-06 11:01:18 -0700514static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700515 { "default", AOM_CONTENT_DEFAULT },
516 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700517 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700518};
519
520static const arg_def_t tune_content = ARG_DEF_ENUM(
521 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700522#endif
Alex Converse219f6452014-08-06 11:01:18 -0700523
Yaowu Xuf883b422016-08-30 14:01:10 -0700524#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000525#if CONFIG_EXT_PARTITION
526static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700527 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
528 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
529 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700530 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000531};
532static const arg_def_t superblock_size = ARG_DEF_ENUM(
533 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
534#endif // CONFIG_EXT_PARTITION
535
Yaowu Xuf883b422016-08-30 14:01:10 -0700536static const arg_def_t *av1_args[] = { &cpu_used_av1,
537 &auto_altref,
538 &sharpness,
539 &static_thresh,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700540#if CONFIG_EXT_TILE
541 &single_tile_decoding,
542#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700543 &tile_cols,
544 &tile_rows,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800545#if CONFIG_DEPENDENT_HORZTILES
546 &tile_dependent_rows,
547#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800548#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800549 &tile_loopfilter,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800550#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700551 &arnr_maxframes,
552 &arnr_strength,
Yaowu Xuf883b422016-08-30 14:01:10 -0700553 &tune_ssim,
554 &cq_level,
555 &max_intra_rate_pct,
556 &max_inter_rate_pct,
557 &gf_cbr_boost_pct,
558 &lossless,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800559#if CONFIG_AOM_QM
560 &enable_qm,
561 &qm_min,
562 &qm_max,
563#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700564 &frame_parallel_decoding,
565 &aq_mode,
Fangwen Fu6160df22017-04-24 09:45:51 -0700566#if CONFIG_EXT_DELTA_Q
567 &deltaq_mode,
568#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700569 &frame_periodic_boost,
570 &noise_sens,
571 &tune_content,
572 &input_color_space,
anorkin76fb1262017-03-22 15:12:12 -0700573#if CONFIG_COLORSPACE_HEADERS
574 &input_transfer_function,
575 &input_chroma_sample_position,
576#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700577 &min_gf_interval,
578 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000579#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700580 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000581#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000582 &num_tg,
583 &mtu_size,
Fangwen Fu8d164de2016-12-14 13:40:54 -0800584#if CONFIG_TEMPMV_SIGNALING
585 &disable_tempmv,
586#endif
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200587#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700588 &bitdeptharg,
589 &inbitdeptharg,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200590#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700591 NULL };
592static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
593 AOME_SET_ENABLEAUTOALTREF,
594 AOME_SET_SHARPNESS,
595 AOME_SET_STATIC_THRESHOLD,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700596#if CONFIG_EXT_TILE
597 AV1E_SET_SINGLE_TILE_DECODING,
598#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700599 AV1E_SET_TILE_COLUMNS,
600 AV1E_SET_TILE_ROWS,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800601#if CONFIG_DEPENDENT_HORZTILES
602 AV1E_SET_TILE_DEPENDENT_ROWS,
603#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800604#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800605 AV1E_SET_TILE_LOOPFILTER,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800606#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700607 AOME_SET_ARNR_MAXFRAMES,
608 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700609 AOME_SET_TUNING,
610 AOME_SET_CQ_LEVEL,
611 AOME_SET_MAX_INTRA_BITRATE_PCT,
612 AV1E_SET_MAX_INTER_BITRATE_PCT,
613 AV1E_SET_GF_CBR_BOOST_PCT,
614 AV1E_SET_LOSSLESS,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800615#if CONFIG_AOM_QM
616 AV1E_SET_ENABLE_QM,
617 AV1E_SET_QM_MIN,
618 AV1E_SET_QM_MAX,
619#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700620 AV1E_SET_FRAME_PARALLEL_DECODING,
621 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700622#if CONFIG_EXT_DELTA_Q
623 AV1E_SET_DELTAQ_MODE,
624#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700625 AV1E_SET_FRAME_PERIODIC_BOOST,
626 AV1E_SET_NOISE_SENSITIVITY,
627 AV1E_SET_TUNE_CONTENT,
628 AV1E_SET_COLOR_SPACE,
anorkin76fb1262017-03-22 15:12:12 -0700629#if CONFIG_COLORSPACE_HEADERS
630 AV1E_SET_TRANSFER_FUNCTION,
631 AV1E_SET_CHROMA_SAMPLE_POSITION,
632#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700633 AV1E_SET_MIN_GF_INTERVAL,
634 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000635#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700636 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000637#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000638 AV1E_SET_NUM_TG,
639 AV1E_SET_MTU,
Fangwen Fu8d164de2016-12-14 13:40:54 -0800640#if CONFIG_TEMPMV_SIGNALING
641 AV1E_SET_DISABLE_TEMPMV,
642#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700643 0 };
hui su82331e02015-08-16 18:21:56 -0700644#endif
645
John Koleszar0ea50ce2010-05-18 11:58:33 -0400646static const arg_def_t *no_args[] = { NULL };
647
James Zern59e7a472015-05-09 10:33:26 -0700648void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700649 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700650 const int num_encoder = get_aom_encoder_count();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400651
John Koleszarc6b90392012-07-13 15:21:29 -0700652 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
653 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400654
John Koleszarc6b90392012-07-13 15:21:29 -0700655 fprintf(stderr, "\nOptions:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700656 arg_show_usage(stderr, main_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700657 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700658 arg_show_usage(stderr, global_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700659 fprintf(stderr, "\nRate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700660 arg_show_usage(stderr, rc_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700661 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700662 arg_show_usage(stderr, rc_twopass_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700663 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700664 arg_show_usage(stderr, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700665#if CONFIG_AV1_ENCODER
666 fprintf(stderr, "\nAV1 Specific Options:\n");
667 arg_show_usage(stderr, av1_args);
hui su82331e02015-08-16 18:21:56 -0700668#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700669 fprintf(stderr,
670 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700671 " The desired precision of timestamps in the output, expressed\n"
672 " in fractional seconds. Default is 1/1000.\n");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800673 fprintf(stderr, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400674
Yaowu Xuf990b352015-06-01 09:13:59 -0700675 for (i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700676 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700677 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
678 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700679 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800680 }
Yaowu Xuf990b352015-06-01 09:13:59 -0700681 fprintf(stderr, "\n ");
682 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400683
John Koleszarc6b90392012-07-13 15:21:29 -0700684 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400685}
686
Yaowu Xuf883b422016-08-30 14:01:10 -0700687#if CONFIG_AV1_ENCODER
688#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800689#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800690
James Zerndba73762014-05-10 17:44:12 -0700691#if !CONFIG_WEBM_IO
692typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700693struct WebmOutputContext {
694 int debug;
695};
James Zerndba73762014-05-10 17:44:12 -0700696#endif
697
John Koleszar9e50ed72012-02-15 12:39:38 -0800698/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800699struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700700 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700701 const char *out_fn;
702 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700703#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700704 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700705#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700706 stereo_format_t stereo_fmt;
707 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
708 int arg_ctrl_cnt;
709 int write_webm;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700710 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700711 int use_16bit_internal;
John Koleszar9e50ed72012-02-15 12:39:38 -0800712};
713
John Koleszar6ad3b742012-11-06 12:02:42 -0800714struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700715 int index;
716 struct stream_state *next;
717 struct stream_config config;
718 FILE *file;
719 struct rate_hist *rate_hist;
720 struct WebmOutputContext webm_ctx;
721 uint64_t psnr_sse_total;
722 uint64_t psnr_samples_total;
723 double psnr_totals[4];
724 int psnr_count;
725 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700726 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700727 unsigned int frames_out;
728 uint64_t cx_time;
729 size_t nbytes;
730 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700731#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700732 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700733#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700734 struct aom_image *img;
735 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700736 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800737};
738
clang-format6c4d83e2016-08-08 19:03:30 -0700739static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700740 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800741 if (rat->den < 0) {
742 rat->num *= -1;
743 rat->den *= -1;
744 }
John Koleszar1b27e932012-04-25 17:08:56 -0700745
clang-format6c4d83e2016-08-08 19:03:30 -0700746 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700747
clang-format6c4d83e2016-08-08 19:03:30 -0700748 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700749}
750
Yaowu Xuf883b422016-08-30 14:01:10 -0700751static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700752 char **argi, **argj;
753 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700754 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700755
clang-format6c4d83e2016-08-08 19:03:30 -0700756 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800757
John Koleszar6ad3b742012-11-06 12:02:42 -0800758 /* Initialize default parameters */
759 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700760 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700761 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700762 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700763 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700764 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400765
John Koleszarc6b90392012-07-13 15:21:29 -0700766 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
767 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400768
John Koleszarc6b90392012-07-13 15:21:29 -0700769 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800771 if (!global->codec)
772 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700773 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800774 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400775
John Koleszar6ad3b742012-11-06 12:02:42 -0800776 if (global->passes < 1 || global->passes > 2)
777 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700778 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800779 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400780
John Koleszar6ad3b742012-11-06 12:02:42 -0800781 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700782 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800783 } else if (arg_match(&arg, &usage, argi))
784 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700785 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800786 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700787 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700788 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar6ad3b742012-11-06 12:02:42 -0800789 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700790 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800791 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700792 global->color_type = I420;
793 else if (arg_match(&arg, &use_i422, argi))
794 global->color_type = I422;
795 else if (arg_match(&arg, &use_i444, argi))
796 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700797 else if (arg_match(&arg, &use_i440, argi))
798 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800799 else if (arg_match(&arg, &quietarg, argi))
800 global->quiet = 1;
801 else if (arg_match(&arg, &verbosearg, argi))
802 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700803 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800804 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700805 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800806 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700807 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800808 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700809 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800810 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700811 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800812 global->framerate = arg_parse_rational(&arg);
813 validate_positive_rational(arg.name, &global->framerate);
814 global->have_framerate = 1;
815 } else if (arg_match(&arg, &out_part, argi))
816 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700817 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800818 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700819 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800820 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700821 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800822 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800823 else if (arg_match(&arg, &disable_warnings, argi))
824 global->disable_warnings = 1;
825 else if (arg_match(&arg, &disable_warning_prompt, argi))
826 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800827 else
John Koleszarc6b90392012-07-13 15:21:29 -0700828 argj++;
829 }
830
John Koleszar6ad3b742012-11-06 12:02:42 -0800831 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700832 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800833 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700834 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
835 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800836 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800837 }
John Koleszarc6b90392012-07-13 15:21:29 -0700838 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800839 /* Validate global config */
840 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700841#if CONFIG_AV1_ENCODER
842 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800843 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700844 if (global->codec != NULL && global->codec->name != NULL)
Thomas Daede80826142017-03-20 15:44:24 -0700845 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800846#else
847 global->passes = 1;
848#endif
849 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800850}
851
Yaowu Xuf883b422016-08-30 14:01:10 -0700852static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800853 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700854 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
855 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -0800856
clang-format6c4d83e2016-08-08 19:03:30 -0700857 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -0800858
John Koleszar25b6e9f2013-02-12 21:17:56 -0800859 if (!fseeko(input->file, 0, SEEK_END)) {
860 /* Input file is seekable. Figure out how long it is, so we can get
861 * progress info.
862 */
863 input->length = ftello(input->file);
864 rewind(input->file);
865 }
866
Frank Galligan09acd262015-06-01 10:20:58 -0700867 /* Default to 1:1 pixel aspect ratio. */
868 input->pixel_aspect_ratio.numerator = 1;
869 input->pixel_aspect_ratio.denominator = 1;
870
John Koleszar6ad3b742012-11-06 12:02:42 -0800871 /* For RAW input sources, these bytes will applied on the first frame
872 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -0700873 */
John Koleszar6ad3b742012-11-06 12:02:42 -0800874 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
875 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400876
clang-format6c4d83e2016-08-08 19:03:30 -0700877 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -0700878 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
879 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800880 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800881 input->width = input->y4m.pic_w;
882 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -0700883 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
884 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800885 input->framerate.numerator = input->y4m.fps_n;
886 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -0700887 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -0700888 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -0800889 } else
890 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -0800891 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -0800892 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -0800893 } else {
894 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700895 }
John Koleszar6ad3b742012-11-06 12:02:42 -0800896}
897
Yaowu Xuf883b422016-08-30 14:01:10 -0700898static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800899 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -0700900 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -0800901}
902
Yaowu Xuf883b422016-08-30 14:01:10 -0700903static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -0800904 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800905 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800906
John Koleszar6ad3b742012-11-06 12:02:42 -0800907 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700908 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800909 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700910 }
911
John Koleszar6ad3b742012-11-06 12:02:42 -0800912 if (prev) {
913 memcpy(stream, prev, sizeof(*stream));
914 stream->index++;
915 prev->next = stream;
916 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700917 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -0800918
John Koleszar6ad3b742012-11-06 12:02:42 -0800919 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -0700920 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -0700921 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -0700922 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -0800923
John Koleszar6ad3b742012-11-06 12:02:42 -0800924 /* Change the default timebase to a high enough value so that the
925 * encoder will always create strictly increasing timestamps.
926 */
927 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -0800928
John Koleszar6ad3b742012-11-06 12:02:42 -0800929 /* Never use the library's default resolution, require it be parsed
930 * from the file or set on the command line.
931 */
932 stream->config.cfg.g_w = 0;
933 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800934
John Koleszar6ad3b742012-11-06 12:02:42 -0800935 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -0800936 stream->config.write_webm = 1;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700937#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -0700938 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700939 stream->webm_ctx.last_pts_ns = -1;
940 stream->webm_ctx.writer = NULL;
941 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700942#endif
Johann87c40b32012-03-01 16:12:53 -0800943
John Koleszar6ad3b742012-11-06 12:02:42 -0800944 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700945 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -0800946 }
John Koleszar9e50ed72012-02-15 12:39:38 -0800947
John Koleszar6ad3b742012-11-06 12:02:42 -0800948 /* Output files must be specified for each stream */
949 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -0800950
John Koleszar6ad3b742012-11-06 12:02:42 -0800951 stream->next = NULL;
952 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800953}
954
Yaowu Xuf883b422016-08-30 14:01:10 -0700955static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -0700956 struct stream_state *stream, char **argv) {
957 char **argi, **argj;
958 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -0800959 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -0700960 static const int *ctrl_args_map = NULL;
961 struct stream_config *config = &stream->config;
962 int eos_mark_found = 0;
Tristan Matthewsed858d62017-07-24 15:33:44 -0400963 int webm_forced = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800964
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800965 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -0800966 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700967#if CONFIG_AV1_ENCODER
968 } else if (strcmp(global->codec->name, "av1") == 0) {
969 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
970 // Consider to expand this set for AV1 encoder control.
971 ctrl_args = av1_args;
972 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -0700973#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700974 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400975
John Koleszarc6b90392012-07-13 15:21:29 -0700976 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -0700977 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400978
John Koleszar6ad3b742012-11-06 12:02:42 -0800979 /* Once we've found an end-of-stream marker (--) we want to continue
980 * shifting arguments but not consuming them.
981 */
982 if (eos_mark_found) {
983 argj++;
984 continue;
985 } else if (!strcmp(*argj, "--")) {
986 eos_mark_found = 1;
987 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -0800988 }
989
Adrian Granged2401802015-02-13 14:51:32 -0800990 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800991 config->out_fn = arg.val;
Tristan Matthewsed858d62017-07-24 15:33:44 -0400992 if (!webm_forced) {
993 const size_t out_fn_len = strlen(config->out_fn);
994 if (out_fn_len >= 4 &&
995 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
996 config->write_webm = 0;
997 }
998 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800999 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001000 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001001#if CONFIG_FP_MB_STATS
1002 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1003 config->fpmb_stats_fn = arg.val;
1004#endif
Johannb50e5182015-01-30 15:05:14 -08001005 } else if (arg_match(&arg, &use_webm, argi)) {
1006#if CONFIG_WEBM_IO
1007 config->write_webm = 1;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001008 webm_forced = 1;
Johannb50e5182015-01-30 15:05:14 -08001009#else
1010 die("Error: --webm specified but webm is disabled.");
1011#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001012 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001013 config->write_webm = 0;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001014 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001015 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001016 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001017 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001018 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001019 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001020 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001021 config->cfg.g_h = arg_parse_uint(&arg);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001022#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001023 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1024 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1025 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1026 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1027#endif
James Zerndba73762014-05-10 17:44:12 -07001028#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001029 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001030 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001031#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001032 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001033 config->cfg.g_timebase = arg_parse_rational(&arg);
1034 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001035 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001036 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001037 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001038 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001039#if CONFIG_EXT_TILE
1040 } else if (arg_match(&arg, &large_scale_tile, argi)) {
1041 config->cfg.large_scale_tile = arg_parse_uint(&arg);
1042#endif // CONFIG_EXT_TILE
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001043 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001044 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001045 } else if (arg_match(&arg, &resize_mode, argi)) {
1046 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
1047 } else if (arg_match(&arg, &resize_numerator, argi)) {
1048 config->cfg.rc_resize_numerator = arg_parse_uint(&arg);
Fergus Simpson7a3f4b32017-06-23 10:32:37 -07001049 } else if (arg_match(&arg, &resize_kf_numerator, argi)) {
1050 config->cfg.rc_resize_kf_numerator = arg_parse_uint(&arg);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001051#if CONFIG_FRAME_SUPERRES
1052 } else if (arg_match(&arg, &superres_mode, argi)) {
1053 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
1054 } else if (arg_match(&arg, &superres_numerator, argi)) {
1055 config->cfg.rc_superres_numerator = arg_parse_uint(&arg);
Fergus Simpson7a3f4b32017-06-23 10:32:37 -07001056 } else if (arg_match(&arg, &superres_kf_numerator, argi)) {
1057 config->cfg.rc_superres_kf_numerator = arg_parse_uint(&arg);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001058#endif // CONFIG_FRAME_SUPERRES
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001059 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001060 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001061 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001062 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001063 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001064 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001065 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001066 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001067 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001068 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001069 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001070 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001071 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001072 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001073 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001074 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001075 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001076 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001077 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001078 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001079 if (global->passes < 2)
1080 warn("option %s ignored in one-pass mode.\n", arg.name);
1081 } else if (arg_match(&arg, &minsection_pct, argi)) {
1082 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1083
1084 if (global->passes < 2)
1085 warn("option %s ignored in one-pass mode.\n", arg.name);
1086 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1087 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1088
1089 if (global->passes < 2)
1090 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001091 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001092 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001093 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001094 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001095 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001096 config->cfg.kf_mode = AOM_KF_DISABLED;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001097 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001098 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001099 for (i = 0; ctrl_args[i]; i++) {
1100 if (arg_match(&arg, ctrl_args[i], argi)) {
1101 int j;
1102 match = 1;
1103
1104 /* Point either to the next free element or the first
1105 * instance of this control.
1106 */
1107 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001108 if (ctrl_args_map != NULL &&
1109 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001110 break;
1111
1112 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001113 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001114 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001115 config->arg_ctrls[j][0] = ctrl_args_map[i];
1116 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001117 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001118 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001119 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001120 }
clang-format6c4d83e2016-08-08 19:03:30 -07001121 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001122 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001123 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001124#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan14af5b92017-05-12 14:27:30 +02001125 config->use_16bit_internal =
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +02001126 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001127#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001128 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001129}
1130
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001131#define FOREACH_STREAM(iterator, list) \
1132 for (struct stream_state *iterator = list; iterator; \
1133 iterator = iterator->next)
John Koleszar9e50ed72012-02-15 12:39:38 -08001134
Alex Conversed66bd222014-02-21 10:52:09 -08001135static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001136 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001137 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001138 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001139
John Koleszar6ad3b742012-11-06 12:02:42 -08001140 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001141 fatal(
1142 "Stream %d: Specify stream dimensions with --width (-w) "
1143 " and --height (-h)",
1144 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001145
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001146 // Check that the codec bit depth is greater than the input bit depth.
1147 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001148 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001149 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1150 stream->index, (int)stream->config.cfg.g_bit_depth,
1151 stream->config.cfg.g_input_bit_depth);
1152 }
1153
John Koleszar6ad3b742012-11-06 12:02:42 -08001154 for (streami = stream; streami; streami = streami->next) {
1155 /* All streams require output files */
1156 if (!streami->config.out_fn)
1157 fatal("Stream %d: Output file is required (specify with -o)",
1158 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001159
John Koleszar6ad3b742012-11-06 12:02:42 -08001160 /* Check for two streams outputting to the same file */
1161 if (streami != stream) {
1162 const char *a = stream->config.out_fn;
1163 const char *b = streami->config.out_fn;
1164 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1165 fatal("Stream %d: duplicate output file (from stream %d)",
1166 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001167 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001168
1169 /* Check for two streams sharing a stats file. */
1170 if (streami != stream) {
1171 const char *a = stream->config.stats_fn;
1172 const char *b = streami->config.stats_fn;
1173 if (a && b && !strcmp(a, b))
1174 fatal("Stream %d: duplicate stats file (from stream %d)",
1175 streami->index, stream->index);
1176 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001177
1178#if CONFIG_FP_MB_STATS
1179 /* Check for two streams sharing a mb stats file. */
1180 if (streami != stream) {
1181 const char *a = stream->config.fpmb_stats_fn;
1182 const char *b = streami->config.fpmb_stats_fn;
1183 if (a && b && !strcmp(a, b))
1184 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1185 streami->index, stream->index);
1186 }
1187#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001188 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001189}
1190
clang-format6c4d83e2016-08-08 19:03:30 -07001191static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001192 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001193 if (!stream->config.cfg.g_w) {
1194 if (!stream->config.cfg.g_h)
1195 stream->config.cfg.g_w = w;
1196 else
1197 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1198 }
1199 if (!stream->config.cfg.g_h) {
1200 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1201 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001202}
1203
clang-format6c4d83e2016-08-08 19:03:30 -07001204static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001205 switch (t) {
1206 case FILE_TYPE_RAW: return "RAW";
1207 case FILE_TYPE_Y4M: return "Y4M";
1208 default: return "Other";
1209 }
1210}
1211
Yaowu Xuf883b422016-08-30 14:01:10 -07001212static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001213 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001214 case AOM_IMG_FMT_I420: return "I420";
1215 case AOM_IMG_FMT_I422: return "I422";
1216 case AOM_IMG_FMT_I444: return "I444";
1217 case AOM_IMG_FMT_I440: return "I440";
1218 case AOM_IMG_FMT_YV12: return "YV12";
1219 case AOM_IMG_FMT_I42016: return "I42016";
1220 case AOM_IMG_FMT_I42216: return "I42216";
1221 case AOM_IMG_FMT_I44416: return "I44416";
1222 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001223 default: return "Other";
1224 }
1225}
Ralph Giles061a16d2012-01-05 15:05:05 -06001226
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001227static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001228 struct AvxEncoderConfig *global,
1229 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001230#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001231 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001232
John Koleszar6ad3b742012-11-06 12:02:42 -08001233 if (stream->index == 0) {
1234 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001235 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001236 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001237 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001238 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001239 }
1240 if (stream->next || stream->index)
1241 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1242 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
Sebastien Alaiwan27511922017-06-05 15:25:46 +02001243 fprintf(stderr, "Coding path: %s\n",
1244 stream->config.use_16bit_internal ? "HBD" : "LBD");
John Koleszar6ad3b742012-11-06 12:02:42 -08001245 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001246
John Koleszar6ad3b742012-11-06 12:02:42 -08001247 SHOW(g_usage);
1248 SHOW(g_threads);
1249 SHOW(g_profile);
1250 SHOW(g_w);
1251 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001252 SHOW(g_bit_depth);
1253 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001254 SHOW(g_timebase.num);
1255 SHOW(g_timebase.den);
1256 SHOW(g_error_resilient);
1257 SHOW(g_pass);
1258 SHOW(g_lag_in_frames);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001259#if CONFIG_EXT_TILE
1260 SHOW(large_scale_tile);
1261#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001262 SHOW(rc_dropframe_thresh);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001263 SHOW(rc_resize_mode);
1264 SHOW(rc_resize_numerator);
Fergus Simpson7a3f4b32017-06-23 10:32:37 -07001265 SHOW(rc_resize_kf_numerator);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001266#if CONFIG_FRAME_SUPERRES
1267 SHOW(rc_superres_mode);
1268 SHOW(rc_superres_numerator);
Fergus Simpson7a3f4b32017-06-23 10:32:37 -07001269 SHOW(rc_superres_kf_numerator);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001270#endif // CONFIG_FRAME_SUPERRES
John Koleszar6ad3b742012-11-06 12:02:42 -08001271 SHOW(rc_end_usage);
1272 SHOW(rc_target_bitrate);
1273 SHOW(rc_min_quantizer);
1274 SHOW(rc_max_quantizer);
1275 SHOW(rc_undershoot_pct);
1276 SHOW(rc_overshoot_pct);
1277 SHOW(rc_buf_sz);
1278 SHOW(rc_buf_initial_sz);
1279 SHOW(rc_buf_optimal_sz);
1280 SHOW(rc_2pass_vbr_bias_pct);
1281 SHOW(rc_2pass_vbr_minsection_pct);
1282 SHOW(rc_2pass_vbr_maxsection_pct);
1283 SHOW(kf_mode);
1284 SHOW(kf_min_dist);
1285 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001286}
1287
John Koleszar9e50ed72012-02-15 12:39:38 -08001288static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001289 struct AvxEncoderConfig *global,
1290 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001291 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001292 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001293
Yaowu Xuf883b422016-08-30 14:01:10 -07001294 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001295
John Koleszar6ad3b742012-11-06 12:02:42 -08001296 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001297
clang-format6c4d83e2016-08-08 19:03:30 -07001298 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001299
John Koleszar6ad3b742012-11-06 12:02:42 -08001300 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1301 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001302
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001303#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001304 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001305 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001306 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1307 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001308 }
James Zernc8e5a772016-02-11 18:53:50 -08001309#else
1310 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001311#endif
1312
1313 if (!stream->config.write_webm) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001314 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1315 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001316}
1317
John Koleszar9e50ed72012-02-15 12:39:38 -08001318static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001319 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001320 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001321
Yaowu Xuf883b422016-08-30 14:01:10 -07001322 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001323
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001324#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001325 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001326 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001327 }
1328#endif
1329
1330 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001331 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001332 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001333 stream->frames_out);
1334 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001335
John Koleszar6ad3b742012-11-06 12:02:42 -08001336 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001337}
1338
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001339static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001340 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001341 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001342 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001343 fatal("Failed to open statistics store");
1344 } else {
1345 if (!stats_open_mem(&stream->stats, pass))
1346 fatal("Failed to open statistics store");
1347 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001348
Pengchong Jinf349b072014-07-14 09:13:38 -07001349#if CONFIG_FP_MB_STATS
1350 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001351 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1352 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001353 fatal("Failed to open mb statistics store");
1354 } else {
1355 if (!stats_open_mem(&stream->fpmb_stats, pass))
1356 fatal("Failed to open mb statistics store");
1357 }
1358#endif
1359
John Koleszar6ad3b742012-11-06 12:02:42 -08001360 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001361 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1362 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001363 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001364 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001365#if CONFIG_FP_MB_STATS
1366 stream->config.cfg.rc_firstpass_mb_stats_in =
1367 stats_get(&stream->fpmb_stats);
1368#endif
1369 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001370
John Koleszar6ad3b742012-11-06 12:02:42 -08001371 stream->cx_time = 0;
1372 stream->nbytes = 0;
1373 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001374}
1375
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001376static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001377 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001378 int i;
1379 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001380
Yaowu Xuf883b422016-08-30 14:01:10 -07001381 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1382 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001383#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001384 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001385#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001386
John Koleszar6ad3b742012-11-06 12:02:42 -08001387 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001388 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001389 &stream->config.cfg, flags);
1390 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001391
Yaowu Xuf883b422016-08-30 14:01:10 -07001392 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001393 * we're being clever to store the control IDs in an array. Real
1394 * applications will want to make use of the enumerations directly
1395 */
1396 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1397 int ctrl = stream->config.arg_ctrls[i][0];
1398 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001399 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001400 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001401
John Koleszar6ad3b742012-11-06 12:02:42 -08001402 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1403 }
1404
Tom Fineganba02c242017-05-16 15:01:54 -07001405#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001406 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001407 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +02001408 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH };
Yaowu Xuf883b422016-08-30 14:01:10 -07001409 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001410
Tom Fineganba02c242017-05-16 15:01:54 -07001411#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07001412 if (strcmp(global->codec->name, "av1") == 0) {
1413 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001414 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1415
Yaowu Xuf883b422016-08-30 14:01:10 -07001416 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001417 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1418 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001419#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001420 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001421#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001422}
1423
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001424static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001425 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001426 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001427 aom_codec_pts_t frame_start, next_frame_start;
1428 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1429 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001430
clang-format6c4d83e2016-08-08 19:03:30 -07001431 frame_start =
1432 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1433 cfg->g_timebase.num / global->framerate.num;
1434 next_frame_start =
1435 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1436 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001437
clang-format6c4d83e2016-08-08 19:03:30 -07001438/* Scale if necessary */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001439#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001440 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001441 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001442 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001443 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001444 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1445 exit(EXIT_FAILURE);
1446 }
1447#if CONFIG_LIBYUV
1448 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001449 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001450 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001451 }
clang-format6c4d83e2016-08-08 19:03:30 -07001452 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001453 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1454 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1455 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1456 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1457 stream->img->stride[AOM_PLANE_Y] / 2,
1458 (uint16 *)stream->img->planes[AOM_PLANE_U],
1459 stream->img->stride[AOM_PLANE_U] / 2,
1460 (uint16 *)stream->img->planes[AOM_PLANE_V],
1461 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001462 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001463 img = stream->img;
1464#else
clang-format6c4d83e2016-08-08 19:03:30 -07001465 stream->encoder.err = 1;
1466 ctx_exit_on_error(&stream->encoder,
1467 "Stream %d: Failed to encode frame.\n"
1468 "Scaling disabled in this configuration. \n"
1469 "To enable, configure with --enable-libyuv\n",
1470 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001471#endif
1472 }
1473 }
1474#endif
John Koleszar34882b92012-03-01 12:50:40 -08001475 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001476 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001477 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1478 exit(EXIT_FAILURE);
1479 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001480#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001481 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001482 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001483 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001484 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001485 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1486 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1487 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1488 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1489 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1490 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001491 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001492 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001493#else
1494 stream->encoder.err = 1;
1495 ctx_exit_on_error(&stream->encoder,
1496 "Stream %d: Failed to encode frame.\n"
1497 "Scaling disabled in this configuration. \n"
1498 "To enable, configure with --enable-libyuv\n",
1499 stream->index);
1500#endif
John Koleszar34882b92012-03-01 12:50:40 -08001501 }
1502
Yaowu Xuf883b422016-08-30 14:01:10 -07001503 aom_usec_timer_start(&timer);
1504 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001505 (unsigned long)(next_frame_start - frame_start), 0,
1506 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001507 aom_usec_timer_mark(&timer);
1508 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001509 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1510 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001511}
1512
John Koleszar6ad3b742012-11-06 12:02:42 -08001513static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001514 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001515 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001516
Yaowu Xuf883b422016-08-30 14:01:10 -07001517 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001518 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1519 stream->counts[q]++;
1520 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001521}
1522
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001523static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001524 struct AvxEncoderConfig *global, int *got_data) {
1525 const aom_codec_cx_pkt_t *pkt;
1526 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1527 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001528
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001529 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001530 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001531 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001532 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001533
John Koleszar6ad3b742012-11-06 12:02:42 -08001534 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001535 case AOM_CODEC_CX_FRAME_PKT:
1536 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001537 stream->frames_out++;
1538 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001539 if (!global->quiet)
1540 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001541
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001542 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001543#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001544 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001545 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001546 }
1547#endif
1548 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001549 if (pkt->data.frame.partition_id <= 0) {
1550 ivf_header_pos = ftello(stream->file);
1551 fsize = pkt->data.frame.sz;
1552
Dmitry Kovalev373b0f92014-01-29 17:57:21 -08001553 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001554 } else {
1555 fsize += pkt->data.frame.sz;
1556
Yaowu Xuf883b422016-08-30 14:01:10 -07001557 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
James Zernb6430362017-01-14 14:18:01 -08001558 const FileOffset currpos = ftello(stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001559 fseeko(stream->file, ivf_header_pos, SEEK_SET);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001560 ivf_write_frame_size(stream->file, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001561 fseeko(stream->file, currpos, SEEK_SET);
1562 }
1563 }
1564
clang-format6c4d83e2016-08-08 19:03:30 -07001565 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1566 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001567 }
1568 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001569
John Koleszar9e50ed72012-02-15 12:39:38 -08001570 *got_data = 1;
Tom Fineganba02c242017-05-16 15:01:54 -07001571#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001572 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001573 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001574 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001575 if (stream->decoder.err) {
1576 warn_or_exit_on_error(&stream->decoder,
1577 global->test_decode == TEST_DECODE_FATAL,
1578 "Failed to decode frame %d in stream %d",
1579 stream->frames_out + 1, stream->index);
1580 stream->mismatch_seen = stream->frames_out + 1;
1581 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001582 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001583#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001584 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001585 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001586 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001587 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001588 pkt->data.twopass_stats.sz);
1589 stream->nbytes += pkt->data.raw.sz;
1590 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001591#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001592 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001593 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001594 pkt->data.firstpass_mb_stats.sz);
1595 stream->nbytes += pkt->data.raw.sz;
1596 break;
1597#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001598 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001599
1600 if (global->show_psnr) {
1601 int i;
1602
1603 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1604 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1605 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001606 if (!global->quiet)
1607 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001608 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1609 }
1610 stream->psnr_count++;
1611 }
1612
1613 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001614 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001615 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001616 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001617}
1618
clang-format6c4d83e2016-08-08 19:03:30 -07001619static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001620 int i;
1621 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001622
clang-format6c4d83e2016-08-08 19:03:30 -07001623 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001624
John Koleszar6ad3b742012-11-06 12:02:42 -08001625 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001626 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001627 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001628 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001629
John Koleszar6ad3b742012-11-06 12:02:42 -08001630 for (i = 0; i < 4; i++) {
1631 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1632 }
1633 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001634}
1635
John Koleszar25b6e9f2013-02-12 21:17:56 -08001636static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001637 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001638}
1639
clang-format6c4d83e2016-08-08 19:03:30 -07001640static void test_decode(struct stream_state *stream,
John Koleszarb3c350a2013-03-13 12:15:43 -07001641 enum TestDecodeFatality fatal,
Yaowu Xuf883b422016-08-30 14:01:10 -07001642 const AvxInterface *codec) {
1643 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001644
clang-format6c4d83e2016-08-08 19:03:30 -07001645 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001646
John Koleszarb3c350a2013-03-13 12:15:43 -07001647 /* Get the internal reference frame */
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001648 if (strcmp(codec->name, "vp8") == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001649 struct aom_ref_frame ref_enc, ref_dec;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001650 const unsigned int frame_width = (stream->config.cfg.g_w + 15) & ~15;
1651 const unsigned int frame_height = (stream->config.cfg.g_h + 15) & ~15;
1652 aom_img_alloc(&ref_enc.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001653 enc_img = ref_enc.img;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001654 aom_img_alloc(&ref_dec.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001655 dec_img = ref_dec.img;
1656
Yaowu Xuf883b422016-08-30 14:01:10 -07001657 ref_enc.frame_type = AOM_LAST_FRAME;
1658 ref_dec.frame_type = AOM_LAST_FRAME;
1659 aom_codec_control(&stream->encoder, AOM_COPY_REFERENCE, &ref_enc);
1660 aom_codec_control(&stream->decoder, AOM_COPY_REFERENCE, &ref_dec);
John Koleszarb3c350a2013-03-13 12:15:43 -07001661 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001662 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1663 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001664
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001665#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001666 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1667 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1668 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1669 aom_image_t enc_hbd_img;
1670 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001671 enc_img.d_w, enc_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001672 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001673 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001674 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001675 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1676 aom_image_t dec_hbd_img;
1677 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001678 dec_img.d_w, dec_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001679 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001680 dec_img = dec_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001681 }
1682 }
1683#endif
John Koleszarb3c350a2013-03-13 12:15:43 -07001684 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001685 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001686 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001687
Urvang Joshi09c293e2017-04-20 17:56:27 -07001688 if (!aom_compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001689 int y[4], u[4], v[4];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001690#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001691 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001692 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001693 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001694 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001695 }
1696#else
Urvang Joshi09c293e2017-04-20 17:56:27 -07001697 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001698#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001699 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001700 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001701 "Stream %d: Encode/decode mismatch on frame %d at"
1702 " Y[%d, %d] {%d/%d},"
1703 " U[%d, %d] {%d/%d},"
1704 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001705 stream->index, stream->frames_out, y[0], y[1], y[2],
1706 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 -08001707 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001708 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001709
Yaowu Xuf883b422016-08-30 14:01:10 -07001710 aom_img_free(&enc_img);
1711 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001712}
John Koleszarefd54f82012-02-13 16:52:18 -08001713
John Koleszar25b6e9f2013-02-12 21:17:56 -08001714static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001715 int64_t hours;
1716 int64_t mins;
1717 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001718
1719 if (etl >= 0) {
1720 hours = etl / 3600;
1721 etl -= hours * 3600;
1722 mins = etl / 60;
1723 etl -= mins * 60;
1724 secs = etl;
1725
clang-format6c4d83e2016-08-08 19:03:30 -07001726 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1727 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001728 } else {
1729 fprintf(stderr, "[%3s unknown] ", label);
1730 }
1731}
1732
Tom Finegan44dd3272013-11-20 17:18:28 -08001733int main(int argc, const char **argv_) {
1734 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001735 aom_image_t raw;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001736#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001737 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001738 int allocated_raw_shift = 0;
1739 int use_16bit_internal = 0;
1740 int input_shift = 0;
1741#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001742 int frame_avail, got_data;
1743
Yaowu Xuf883b422016-08-30 14:01:10 -07001744 struct AvxInputContext input;
1745 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001746 struct stream_state *streams = NULL;
1747 char **argv, **argi;
1748 uint64_t cx_time = 0;
1749 int stream_cnt = 0;
1750 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001751 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001752
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001753 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001754 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001755
clang-format6c4d83e2016-08-08 19:03:30 -07001756 if (argc < 3) usage_exit();
John Koleszar6ad3b742012-11-06 12:02:42 -08001757
1758 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001759 input.framerate.numerator = 30;
1760 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001761 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001762 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001763
1764 /* First parse the global configuration values, because we want to apply
1765 * other parameters on top of the default configuration provided by the
1766 * codec.
1767 */
1768 argv = argv_dup(argc - 1, argv_ + 1);
1769 parse_global_config(&global, argv);
1770
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001771 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001772 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1773 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1774 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1775 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1776 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001777 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001778
John Koleszar6ad3b742012-11-06 12:02:42 -08001779 {
1780 /* Now parse each stream's parameters. Using a local scope here
1781 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1782 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001783 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001784 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001785
John Koleszar6ad3b742012-11-06 12:02:42 -08001786 do {
1787 stream = new_stream(&global, stream);
1788 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001789 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001790 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001791 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001792
John Koleszarc6b90392012-07-13 15:21:29 -07001793 /* Check for unrecognized options */
1794 for (argi = argv; *argi; argi++)
1795 if (argi[0][0] == '-' && argi[0][1])
1796 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001797
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001798 FOREACH_STREAM(stream, streams) {
1799 check_encoder_config(global.disable_warning_prompt, &global,
1800 &stream->config.cfg);
1801 }
Tom Finegan249366b2013-11-25 12:05:19 -08001802
John Koleszarc6b90392012-07-13 15:21:29 -07001803 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001804 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001805
clang-format6c4d83e2016-08-08 19:03:30 -07001806 if (!input.filename) usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04001807
John Koleszar8dd82872013-05-06 11:01:35 -07001808 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001809 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001810
John Koleszar6ad3b742012-11-06 12:02:42 -08001811 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001812 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001813 int64_t estimated_time_left = -1;
1814 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001815 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001816
John Koleszar6ad3b742012-11-06 12:02:42 -08001817 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001818
John Koleszar6ad3b742012-11-06 12:02:42 -08001819 /* If the input file doesn't specify its w/h (raw files), try to get
1820 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001821 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001822 if (!input.width || !input.height) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001823 FOREACH_STREAM(stream, streams) {
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001824 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1825 input.width = stream->config.cfg.g_w;
1826 input.height = stream->config.cfg.g_h;
1827 break;
1828 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001829 };
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001830 }
John Koleszarc6b90392012-07-13 15:21:29 -07001831
John Koleszar6ad3b742012-11-06 12:02:42 -08001832 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001833 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001834 fatal(
1835 "Specify stream dimensions with --width (-w) "
1836 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001837
1838 /* If input file does not specify bit-depth but input-bit-depth parameter
1839 * exists, assume that to be the input bit-depth. However, if the
1840 * input-bit-depth paramter does not exist, assume the input bit-depth
1841 * to be the same as the codec bit-depth.
1842 */
1843 if (!input.bit_depth) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001844 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001845 if (stream->config.cfg.g_input_bit_depth)
1846 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1847 else
1848 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1849 (int)stream->config.cfg.g_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001850 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001851 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001852 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001853 FOREACH_STREAM(stream, streams) {
1854 stream->config.cfg.g_input_bit_depth = input.bit_depth;
1855 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001856 }
1857
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001858 FOREACH_STREAM(stream, streams) {
Thomas Daede8ec53b22016-09-19 13:24:51 -07001859 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
1860 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
1861 was selected. */
1862 switch (stream->config.cfg.g_profile) {
1863 case 0:
1864 stream->config.cfg.g_profile = 1;
1865 profile_updated = 1;
1866 break;
1867 case 2:
1868 stream->config.cfg.g_profile = 3;
1869 profile_updated = 1;
1870 break;
1871 default: break;
1872 }
1873 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07001874 /* Automatically set the codec bit depth to match the input bit depth.
1875 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001876 if (stream->config.cfg.g_input_bit_depth >
1877 (unsigned int)stream->config.cfg.g_bit_depth) {
1878 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
1879 }
1880 if (stream->config.cfg.g_bit_depth > 8) {
1881 switch (stream->config.cfg.g_profile) {
1882 case 0:
1883 stream->config.cfg.g_profile = 2;
1884 profile_updated = 1;
1885 break;
1886 case 1:
1887 stream->config.cfg.g_profile = 3;
1888 profile_updated = 1;
1889 break;
1890 default: break;
1891 }
1892 }
1893 if (stream->config.cfg.g_profile > 1) {
Sebastien Alaiwanfadc7cb2017-06-08 22:10:43 +02001894 if (!CONFIG_HIGHBITDEPTH) fatal("Unsupported profile.");
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001895 stream->config.use_16bit_internal = 1;
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001896 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08001897 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001898 fprintf(stderr,
1899 "Warning: automatically upgrading to profile %d to "
1900 "match input format.\n",
1901 stream->config.cfg.g_profile);
1902 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001903 }
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001904
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001905 FOREACH_STREAM(stream, streams) {
1906 set_stream_dimensions(stream, input.width, input.height);
1907 }
1908 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
John Koleszar77e6b452010-11-03 13:58:40 -04001909
John Koleszar6ad3b742012-11-06 12:02:42 -08001910 /* Ensure that --passes and --pass are consistent. If --pass is set and
1911 * --passes=2, ensure --fpf was set.
1912 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001913 if (global.pass && global.passes == 2) {
1914 FOREACH_STREAM(stream, streams) {
clang-format6c4d83e2016-08-08 19:03:30 -07001915 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07001916 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07001917 " and --passes=2\n",
1918 stream->index, global.pass);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001919 }
1920 }
John Koleszar3245d462010-06-10 17:10:12 -04001921
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001922#if !CONFIG_WEBM_IO
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001923 FOREACH_STREAM(stream, streams) {
Ronald S. Bultje39775072015-12-16 13:35:43 -05001924 if (stream->config.write_webm) {
1925 stream->config.write_webm = 0;
clang-format6c4d83e2016-08-08 19:03:30 -07001926 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07001927 "aomenc was compiled without WebM container support."
clang-format6c4d83e2016-08-08 19:03:30 -07001928 "Producing IVF output");
Ronald S. Bultje39775072015-12-16 13:35:43 -05001929 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001930 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001931#endif
1932
John Koleszar6ad3b742012-11-06 12:02:42 -08001933 /* Use the frame rate from the file only if none was specified
1934 * on the command-line.
1935 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001936 if (!global.have_framerate) {
1937 global.framerate.num = input.framerate.numerator;
1938 global.framerate.den = input.framerate.denominator;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001939 FOREACH_STREAM(stream, streams) {
1940 stream->config.cfg.g_timebase.den = global.framerate.num;
1941 stream->config.cfg.g_timebase.num = global.framerate.den;
1942 }
Tom Finegan00a35aa2013-11-14 12:37:42 -08001943 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04001944
John Koleszar6ad3b742012-11-06 12:02:42 -08001945 /* Show configuration */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001946 if (global.verbose && pass == 0) {
1947 FOREACH_STREAM(stream, streams) {
1948 show_stream_config(stream, &global, &input);
1949 }
1950 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001951
1952 if (pass == (global.pass ? global.pass - 1 : 0)) {
1953 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07001954 /*The Y4M reader does its own allocation.
1955 Just initialize this here to avoid problems if we never read any
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001956 frames.*/
John Koleszarc6b90392012-07-13 15:21:29 -07001957 memset(&raw, 0, sizeof(raw));
1958 else
Yaowu Xuf883b422016-08-30 14:01:10 -07001959 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04001960
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001961 FOREACH_STREAM(stream, streams) {
1962 stream->rate_hist =
1963 init_rate_histogram(&stream->config.cfg, &global.framerate);
1964 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001965 }
1966
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001967 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
1968 FOREACH_STREAM(stream, streams) {
1969 open_output_file(stream, &global, &input.pixel_aspect_ratio);
1970 }
1971 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001972
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001973#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001974 if (strcmp(global.codec->name, "av1") == 0 ||
1975 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001976 // Check to see if at least one stream uses 16 bit internal.
1977 // Currently assume that the bit_depths for all streams using
1978 // highbitdepth are the same.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001979 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001980 if (stream->config.use_16bit_internal) {
1981 use_16bit_internal = 1;
1982 }
1983 if (stream->config.cfg.g_profile == 0) {
1984 input_shift = 0;
1985 } else {
1986 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07001987 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001988 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001989 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001990 }
1991#endif
1992
John Koleszarc6b90392012-07-13 15:21:29 -07001993 frame_avail = 1;
1994 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001995
John Koleszarc6b90392012-07-13 15:21:29 -07001996 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001997 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001998
John Koleszar6ad3b742012-11-06 12:02:42 -08001999 if (!global.limit || frames_in < global.limit) {
2000 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002001
clang-format6c4d83e2016-08-08 19:03:30 -07002002 if (frame_avail) frames_in++;
2003 seen_frames =
2004 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002005
John Koleszar6ad3b742012-11-06 12:02:42 -08002006 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002007 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002008 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2009
John Koleszar6ad3b742012-11-06 12:02:42 -08002010 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002011 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2012 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002013 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002014 fprintf(stderr, "frame %4d ", frames_in);
2015
clang-format6c4d83e2016-08-08 19:03:30 -07002016 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002017 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002018 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002019 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002020 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002021 }
2022
hui su251e1512016-10-03 15:48:43 -07002023 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002024 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002025 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002026
John Koleszar6ad3b742012-11-06 12:02:42 -08002027 if (frames_in > global.skip_frames) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002028#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002029 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002030 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2031 assert(use_16bit_internal);
2032 // Input bit depth and stream bit depth do not match, so up
2033 // shift frame to stream bit depth
2034 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002035 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002036 input.width, input.height, 32);
2037 allocated_raw_shift = 1;
2038 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002039 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002040 frame_to_encode = &raw_shift;
2041 } else {
2042 frame_to_encode = &raw;
2043 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002044 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002045 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002046 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002047 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002048 if (stream->config.use_16bit_internal)
2049 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002050 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002051 else
2052 assert(0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002053 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002054 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002055 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002056 FOREACH_STREAM(stream, streams) {
2057 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2058 frames_in);
2059 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002060 }
2061#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002062 aom_usec_timer_start(&timer);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002063 FOREACH_STREAM(stream, streams) {
2064 encode_frame(stream, &global, frame_avail ? &raw : NULL, frames_in);
2065 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002066#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002067 aom_usec_timer_mark(&timer);
2068 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002069
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002070 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
John Koleszarc6b90392012-07-13 15:21:29 -07002071
John Koleszar0ea50ce2010-05-18 11:58:33 -04002072 got_data = 0;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002073 FOREACH_STREAM(stream, streams) {
2074 get_cx_data(stream, &global, &got_data);
2075 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002076
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002077 if (!got_data && input.length && streams != NULL &&
2078 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002079 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002080 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002081 int64_t remaining;
2082 int64_t rate;
2083
2084 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002085 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002086
2087 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002088 remaining = 1000 * (global.limit - global.skip_frames -
2089 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002090 } else {
James Zerne3578af2014-04-17 10:47:08 -07002091 const int64_t input_pos = ftello(input.file);
2092 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002093 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002094
2095 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002096 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002097 }
2098
clang-format6c4d83e2016-08-08 19:03:30 -07002099 average_rate =
2100 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002101 estimated_time_left = average_rate ? remaining / average_rate : -1;
2102 }
2103
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002104 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2105 FOREACH_STREAM(stream, streams) {
2106 test_decode(stream, global.test_decode, global.codec);
2107 }
2108 }
John Koleszarc6b90392012-07-13 15:21:29 -07002109 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002110
John Koleszarc6b90392012-07-13 15:21:29 -07002111 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002112 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002113 }
2114
clang-format6c4d83e2016-08-08 19:03:30 -07002115 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002116
Deb Mukherjeea160d722014-09-30 21:56:33 -07002117 if (!global.quiet) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002118 FOREACH_STREAM(stream, streams) {
2119 fprintf(stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2120 "b/f %7" PRId64
2121 "b/s"
2122 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2123 pass + 1, global.passes, frames_in, stream->frames_out,
2124 (int64_t)stream->nbytes,
2125 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2126 seen_frames
2127 ? (int64_t)stream->nbytes * 8 *
2128 (int64_t)global.framerate.num / global.framerate.den /
2129 seen_frames
2130 : 0,
2131 stream->cx_time > 9999999 ? stream->cx_time / 1000
2132 : stream->cx_time,
2133 stream->cx_time > 9999999 ? "ms" : "us",
2134 usec_to_fps(stream->cx_time, seen_frames));
2135 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002136 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002137
Deb Mukherjeea160d722014-09-30 21:56:33 -07002138 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002139 if (global.codec->fourcc == AV1_FOURCC) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002140 FOREACH_STREAM(stream, streams) {
2141 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2142 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002143 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002144 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002145 }
2146 }
John Koleszarc6b90392012-07-13 15:21:29 -07002147
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002148 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002149
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002150 if (global.test_decode != TEST_DECODE_OFF) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002151 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002152 }
2153
John Koleszar6ad3b742012-11-06 12:02:42 -08002154 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002155
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002156 if (global.test_decode == TEST_DECODE_FATAL) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002157 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002158 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002159 FOREACH_STREAM(stream, streams) {
2160 close_output_file(stream, global.codec->fourcc);
2161 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002162
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002163 FOREACH_STREAM(stream, streams) {
2164 stats_close(&stream->stats, global.passes - 1);
2165 }
John Koleszarc6b90392012-07-13 15:21:29 -07002166
Pengchong Jinf349b072014-07-14 09:13:38 -07002167#if CONFIG_FP_MB_STATS
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002168 FOREACH_STREAM(stream, streams) {
2169 stats_close(&stream->fpmb_stats, global.passes - 1);
2170 }
Pengchong Jinf349b072014-07-14 09:13:38 -07002171#endif
2172
clang-format6c4d83e2016-08-08 19:03:30 -07002173 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002174 }
2175
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002176 if (global.show_q_hist_buckets) {
2177 FOREACH_STREAM(stream, streams) {
2178 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2179 }
2180 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002181
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002182 if (global.show_rate_hist_buckets) {
2183 FOREACH_STREAM(stream, streams) {
2184 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2185 global.show_rate_hist_buckets);
2186 }
2187 }
2188 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002189
Ronald S. Bultje31214972012-10-11 10:13:48 -07002190#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002191 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2192 * to match some existing utilities.
2193 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002194 if (!(global.pass == 1 && global.passes == 2)) {
2195 FOREACH_STREAM(stream, streams) {
Yaowu Xu47e784e2013-10-16 16:29:28 -07002196 FILE *f = fopen("opsnr.stt", "a");
2197 if (stream->mismatch_seen) {
2198 fprintf(f, "First mismatch occurred in frame %d\n",
2199 stream->mismatch_seen);
2200 } else {
2201 fprintf(f, "No mismatch detected in recon buffers\n");
2202 }
2203 fclose(f);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002204 }
2205 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002206#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002207
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002208#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002209 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002210#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002211 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002212 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002213 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002214 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002215}