blob: 44d8480881a53cd318f00ae4cf27f8ba1c481d5f [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#include "./aomenc.h"
13#include "./aom_config.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040014
Tom Finegan00a35aa2013-11-14 12:37:42 -080015#include <assert.h>
16#include <limits.h>
Tom Finegan78cb2e62013-11-07 21:28:45 -080017#include <math.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080018#include <stdarg.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040019#include <stdio.h>
20#include <stdlib.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040021#include <string.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080022
James Zern58058812014-08-15 21:00:31 -070023#if CONFIG_LIBYUV
24#include "third_party/libyuv/include/libyuv/scale.h"
25#endif
26
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom/aom_encoder.h"
Tom Fineganba02c242017-05-16 15:01:54 -070028#if CONFIG_AV1_DECODER
Yaowu Xuf883b422016-08-30 14:01:10 -070029#include "aom/aom_decoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080030#endif
John Koleszar6ad3b742012-11-06 12:02:42 -080031
Tom Finegan00a35aa2013-11-14 12:37:42 -080032#include "./args.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080033#include "./ivfenc.h"
Tom Finegan7a691f12014-02-10 14:55:25 -080034#include "./tools_common.h"
Urvang Joshi09c293e2017-04-20 17:56:27 -070035#include "examples/encoder_util.h"
Tom Finegan03848f52013-11-05 10:02:18 -080036
Yaowu Xuf883b422016-08-30 14:01:10 -070037#if CONFIG_AV1_ENCODER
38#include "aom/aomcx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080039#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070040#if CONFIG_AV1_DECODER
41#include "aom/aomdx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080042#endif
43
Yaowu Xuf883b422016-08-30 14:01:10 -070044#include "./aomstats.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080045#include "./rate_hist.h"
Tom Finegan249366b2013-11-25 12:05:19 -080046#include "./warnings.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080047#include "aom/aom_integer.h"
hui suefdad1f2017-05-16 18:01:25 -070048#include "aom_dsp/aom_dsp_common.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080049#include "aom_ports/aom_timer.h"
50#include "aom_ports/mem_ops.h"
James Zerndba73762014-05-10 17:44:12 -070051#if CONFIG_WEBM_IO
Tom Finegan78cb2e62013-11-07 21:28:45 -080052#include "./webmenc.h"
James Zerndba73762014-05-10 17:44:12 -070053#endif
Tom Finegan78cb2e62013-11-07 21:28:45 -080054#include "./y4minput.h"
Tom Finegan03848f52013-11-05 10:02:18 -080055
John Koleszar6291dd42012-06-19 21:05:36 -070056/* Swallow warnings about unused results of fread/fwrite */
clang-format6c4d83e2016-08-08 19:03:30 -070057static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
John Koleszar6ad3b742012-11-06 12:02:42 -080058 return fread(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070059}
60#define fread wrap_fread
61
62static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080063 FILE *stream) {
64 return fwrite(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070065}
66#define fwrite wrap_fwrite
67
John Koleszar0ea50ce2010-05-18 11:58:33 -040068static const char *exec_name;
69
Yaowu Xuf883b422016-08-30 14:01:10 -070070static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080071 const char *s, va_list ap) {
John Koleszarc6b90392012-07-13 15:21:29 -070072 if (ctx->err) {
Yaowu Xuf883b422016-08-30 14:01:10 -070073 const char *detail = aom_codec_error_detail(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -040074
John Koleszar6ad3b742012-11-06 12:02:42 -080075 vfprintf(stderr, s, ap);
Yaowu Xuf883b422016-08-30 14:01:10 -070076 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -040077
clang-format6c4d83e2016-08-08 19:03:30 -070078 if (detail) fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -040079
clang-format6c4d83e2016-08-08 19:03:30 -070080 if (fatal) exit(EXIT_FAILURE);
John Koleszarc6b90392012-07-13 15:21:29 -070081 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040082}
83
Yaowu Xuf883b422016-08-30 14:01:10 -070084static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080085 va_list ap;
86
87 va_start(ap, s);
88 warn_or_exit_on_errorv(ctx, 1, s, ap);
89 va_end(ap);
90}
91
Yaowu Xuf883b422016-08-30 14:01:10 -070092static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080093 const char *s, ...) {
94 va_list ap;
95
96 va_start(ap, s);
97 warn_or_exit_on_errorv(ctx, fatal, s, ap);
98 va_end(ap);
99}
100
Yaowu Xuf883b422016-08-30 14:01:10 -0700101static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800102 FILE *f = input_ctx->file;
103 y4m_input *y4m = &input_ctx->y4m;
John Koleszarc6b90392012-07-13 15:21:29 -0700104 int shortread = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400105
Tom Finegan00a35aa2013-11-14 12:37:42 -0800106 if (input_ctx->file_type == FILE_TYPE_Y4M) {
clang-format6c4d83e2016-08-08 19:03:30 -0700107 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700108 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800109 shortread = read_yuv_frame(input_ctx, img);
John Koleszarc6b90392012-07-13 15:21:29 -0700110 }
111
112 return !shortread;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400113}
114
James Zern5c337fd2015-05-09 10:42:58 -0700115static int file_is_y4m(const char detect[4]) {
John Koleszarc6b90392012-07-13 15:21:29 -0700116 if (memcmp(detect, "YUV4", 4) == 0) {
117 return 1;
118 }
119 return 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400120}
121
James Zern5c337fd2015-05-09 10:42:58 -0700122static int fourcc_is_ivf(const char detect[4]) {
Alex Converse64b89f12014-01-06 16:29:09 -0800123 if (memcmp(detect, "DKIF", 4) == 0) {
124 return 1;
125 }
126 return 0;
127}
John Koleszardc666302010-10-20 12:05:48 -0400128
James Zernfd8c78c2017-11-27 16:37:33 -0800129static const arg_def_t help =
130 ARG_DEF(NULL, "help", 0, "Show usage options and exit");
clang-format6c4d83e2016-08-08 19:03:30 -0700131static const arg_def_t debugmode =
132 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
133static const arg_def_t outputfile =
134 ARG_DEF("o", "output", 1, "Output filename");
135static const arg_def_t use_yv12 =
136 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
137static const arg_def_t use_i420 =
138 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
139static const arg_def_t use_i422 =
140 ARG_DEF(NULL, "i422", 0, "Input file is I422");
141static const arg_def_t use_i444 =
142 ARG_DEF(NULL, "i444", 0, "Input file is I444");
143static const arg_def_t use_i440 =
144 ARG_DEF(NULL, "i440", 0, "Input file is I440");
145static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
146static const arg_def_t passes =
147 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
148static const arg_def_t pass_arg =
149 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
150static const arg_def_t fpf_name =
151 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700152#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700153static const arg_def_t fpmbf_name =
154 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700155#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700156static const arg_def_t limit =
157 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
158static const arg_def_t skip =
159 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
160static const arg_def_t deadline =
161 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
clang-format6c4d83e2016-08-08 19:03:30 -0700162static const arg_def_t good_dl =
163 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
clang-format6c4d83e2016-08-08 19:03:30 -0700164static const arg_def_t quietarg =
165 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
166static const arg_def_t verbosearg =
167 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
168static const arg_def_t psnrarg =
169 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800170
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800171static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700172 { "off", TEST_DECODE_OFF },
173 { "fatal", TEST_DECODE_FATAL },
174 { "warn", TEST_DECODE_WARN },
175 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800176};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700177static const arg_def_t recontest = ARG_DEF_ENUM(
178 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700179static const arg_def_t framerate =
180 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
181static const arg_def_t use_webm =
182 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
183static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
Cyril Concolato6c788832017-10-30 16:30:35 -0700184#if CONFIG_OBU_NO_IVF
185static const arg_def_t use_obu = ARG_DEF(NULL, "obu", 0, "Output OBU");
186#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700187static const arg_def_t out_part =
188 ARG_DEF("P", "output-partitions", 0,
189 "Makes encoder output partitions. Requires IVF output!");
190static const arg_def_t q_hist_n =
191 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
192static const arg_def_t rate_hist_n =
193 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
194static const arg_def_t disable_warnings =
195 ARG_DEF(NULL, "disable-warnings", 0,
196 "Disable warnings about potentially incorrect encode settings.");
197static const arg_def_t disable_warning_prompt =
198 ARG_DEF("y", "disable-warning-prompt", 0,
199 "Display warnings, but do not prompt user to continue.");
Alex Conversed66bd222014-02-21 10:52:09 -0800200
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200201#if CONFIG_HIGHBITDEPTH
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400202static const struct arg_enum_list bitdepth_enum[] = {
203 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
204};
205
206static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
207 "b", "bit-depth", 1,
208 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
209 bitdepth_enum);
210static const arg_def_t inbitdeptharg =
211 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700212#endif
Tom Finegan44dd3272013-11-20 17:18:28 -0800213
James Zernfd8c78c2017-11-27 16:37:33 -0800214static const arg_def_t *main_args[] = { &help,
215 &debugmode,
clang-format6c4d83e2016-08-08 19:03:30 -0700216 &outputfile,
217 &codecarg,
218 &passes,
219 &pass_arg,
220 &fpf_name,
221 &limit,
222 &skip,
223 &deadline,
clang-format6c4d83e2016-08-08 19:03:30 -0700224 &good_dl,
clang-format6c4d83e2016-08-08 19:03:30 -0700225 &quietarg,
226 &verbosearg,
227 &psnrarg,
228 &use_webm,
229 &use_ivf,
Cyril Concolato6c788832017-10-30 16:30:35 -0700230#if CONFIG_OBU_NO_IVF
231 &use_obu,
232#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700233 &out_part,
234 &q_hist_n,
235 &rate_hist_n,
236 &disable_warnings,
237 &disable_warning_prompt,
238 &recontest,
239 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400240
clang-format6c4d83e2016-08-08 19:03:30 -0700241static const arg_def_t usage =
242 ARG_DEF("u", "usage", 1, "Usage profile number to use");
243static const arg_def_t threads =
244 ARG_DEF("t", "threads", 1, "Max number of threads to use");
245static const arg_def_t profile =
246 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700247static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
248static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700249#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700250static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700251 { "mono", STEREO_FORMAT_MONO },
252 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
253 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
254 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
255 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
256 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700257};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700258static const arg_def_t stereo_mode = ARG_DEF_ENUM(
259 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700260#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700261static const arg_def_t timebase = ARG_DEF(
262 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700263static const arg_def_t error_resilient =
264 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
265static const arg_def_t lag_in_frames =
266 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700267#if CONFIG_EXT_TILE
268static const arg_def_t large_scale_tile =
269 ARG_DEF(NULL, "large-scale-tile", 1,
270 "Large scale tile coding (0: off (default), 1: on)");
271#endif // CONFIG_EXT_TILE
Rupert Swarbricke0b15992017-11-09 15:04:32 +0000272#if CONFIG_MONO_VIDEO
273static const arg_def_t monochrome =
274 ARG_DEF(NULL, "monochrome", 0, "Monochrome video (no chroma planes)");
275#endif // MONOCHROME
John Koleszar0ea50ce2010-05-18 11:58:33 -0400276
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700277static const arg_def_t *global_args[] = { &use_yv12,
278 &use_i420,
279 &use_i422,
280 &use_i444,
281 &use_i440,
282 &usage,
283 &threads,
284 &profile,
285 &width,
286 &height,
James Zerndba73762014-05-10 17:44:12 -0700287#if CONFIG_WEBM_IO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700288 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700289#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700290 &timebase,
291 &framerate,
292 &error_resilient,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200293#if CONFIG_HIGHBITDEPTH
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700294 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700295#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700296 &lag_in_frames,
297#if CONFIG_EXT_TILE
298 &large_scale_tile,
299#endif // CONFIG_EXT_TILE
Rupert Swarbricke0b15992017-11-09 15:04:32 +0000300#if CONFIG_MONO_VIDEO
301 &monochrome,
302#endif // CONFIG_MONO_VIDEO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700303 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400304
clang-format6c4d83e2016-08-08 19:03:30 -0700305static const arg_def_t dropframe_thresh =
306 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700307static const arg_def_t resize_mode =
308 ARG_DEF(NULL, "resize-mode", 1, "Frame resize mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700309static const arg_def_t resize_denominator =
310 ARG_DEF(NULL, "resize-denominator", 1, "Frame resize denominator");
311static const arg_def_t resize_kf_denominator = ARG_DEF(
312 NULL, "resize-kf-denominator", 1, "Frame resize keyframe denominator");
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700313#if CONFIG_FRAME_SUPERRES
314static const arg_def_t superres_mode =
315 ARG_DEF(NULL, "superres-mode", 1, "Frame super-resolution mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700316static const arg_def_t superres_denominator = ARG_DEF(
317 NULL, "superres-denominator", 1, "Frame super-resolution denominator");
318static const arg_def_t superres_kf_denominator =
319 ARG_DEF(NULL, "superres-kf-denominator", 1,
320 "Frame super-resolution keyframe denominator");
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700321static const arg_def_t superres_qthresh = ARG_DEF(
322 NULL, "superres-qthresh", 1, "Frame super-resolution qindex threshold");
323static const arg_def_t superres_kf_qthresh =
324 ARG_DEF(NULL, "superres-kf-qthresh", 1,
325 "Frame super-resolution keyframe qindex threshold");
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700326#endif // CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700327static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
328 { "cbr", AOM_CBR },
329 { "cq", AOM_CQ },
330 { "q", AOM_Q },
331 { NULL, 0 } };
clang-format6c4d83e2016-08-08 19:03:30 -0700332static const arg_def_t end_usage =
333 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
334static const arg_def_t target_bitrate =
335 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
336static const arg_def_t min_quantizer =
337 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
338static const arg_def_t max_quantizer =
339 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
340static const arg_def_t undershoot_pct =
341 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
342static const arg_def_t overshoot_pct =
343 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
344static const arg_def_t buf_sz =
345 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
346static const arg_def_t buf_initial_sz =
347 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
348static const arg_def_t buf_optimal_sz =
349 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700350static const arg_def_t *rc_args[] = { &dropframe_thresh,
351 &resize_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700352 &resize_denominator,
353 &resize_kf_denominator,
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700354#if CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700355 &superres_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700356 &superres_denominator,
357 &superres_kf_denominator,
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700358 &superres_qthresh,
359 &superres_kf_qthresh,
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700360#endif // CONFIG_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700361 &end_usage,
362 &target_bitrate,
363 &min_quantizer,
364 &max_quantizer,
365 &undershoot_pct,
366 &overshoot_pct,
367 &buf_sz,
368 &buf_initial_sz,
369 &buf_optimal_sz,
370 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400371
clang-format6c4d83e2016-08-08 19:03:30 -0700372static const arg_def_t bias_pct =
373 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
374static const arg_def_t minsection_pct =
375 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
376static const arg_def_t maxsection_pct =
377 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
378static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
379 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400380
clang-format6c4d83e2016-08-08 19:03:30 -0700381static const arg_def_t kf_min_dist =
382 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
383static const arg_def_t kf_max_dist =
384 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
385static const arg_def_t kf_disabled =
386 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
387static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
388 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400389
clang-format6c4d83e2016-08-08 19:03:30 -0700390static const arg_def_t noise_sens =
391 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
392static const arg_def_t sharpness =
393 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
394static const arg_def_t static_thresh =
395 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
396static const arg_def_t auto_altref =
397 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
398static const arg_def_t arnr_maxframes =
399 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
400static const arg_def_t arnr_strength =
401 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500402static const struct arg_enum_list tuning_enum[] = {
Yushin Chod808bfc2017-08-10 15:54:36 -0700403 { "psnr", AOM_TUNE_PSNR },
404 { "ssim", AOM_TUNE_SSIM },
405#ifdef CONFIG_DIST_8X8
406 { "cdef-dist", AOM_TUNE_CDEF_DIST },
407 { "daala-dist", AOM_TUNE_DAALA_DIST },
408#endif
409 { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500410};
Yushin Chod808bfc2017-08-10 15:54:36 -0700411static const arg_def_t tune_metric =
412 ARG_DEF_ENUM(NULL, "tune", 1, "Distortion metric tuned with", tuning_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700413static const arg_def_t cq_level =
414 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
415static const arg_def_t max_intra_rate_pct =
416 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800417
Yaowu Xuf883b422016-08-30 14:01:10 -0700418#if CONFIG_AV1_ENCODER
419static const arg_def_t cpu_used_av1 =
Yaowu Xu39737592017-04-21 16:39:09 -0700420 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800421static const arg_def_t dev_sf_av1 =
422 ARG_DEF(NULL, "dev-sf", 1, "Dev Speed (0..255)");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700423#if CONFIG_EXT_TILE
424static const arg_def_t single_tile_decoding =
425 ARG_DEF(NULL, "single-tile-decoding", 1,
426 "Single tile decoding (0: off (default), 1: on)");
427#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700428static const arg_def_t tile_cols =
429 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
430static const arg_def_t tile_rows =
431 ARG_DEF(NULL, "tile-rows", 1,
432 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200433#if CONFIG_MAX_TILE
434static const arg_def_t tile_width =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200435 ARG_DEF(NULL, "tile-width", 1, "Tile widths (comma separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200436static const arg_def_t tile_height =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200437 ARG_DEF(NULL, "tile-height", 1, "Tile heights (command separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200438#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800439#if CONFIG_DEPENDENT_HORZTILES
440static const arg_def_t tile_dependent_rows =
441 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
442#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800443#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800444static const arg_def_t tile_loopfilter = ARG_DEF(
445 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800446#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700447static const arg_def_t lossless =
448 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100449static const arg_def_t enable_cdef =
450 ARG_DEF(NULL, "enable-cdef", 1,
451 "Enable the constrained directional enhancement filter (0: false, "
452 "1: true (default))");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700453#if CONFIG_AOM_QM
454static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800455 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700456 "Enable quantisation matrices (0: false (default), 1: true)");
457static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800458 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700459static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800460 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700461#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700462#if CONFIG_DIST_8X8
463static const arg_def_t enable_dist_8x8 =
464 ARG_DEF(NULL, "enable-dist-8x8", 1,
465 "Enable dist-8x8 (0: false (default), 1: true)");
466#endif // CONFIG_DIST_8X8
Yaowu Xu859a5272016-11-10 15:32:21 -0800467static const arg_def_t num_tg = ARG_DEF(
468 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000469static const arg_def_t mtu_size =
470 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800471 "MTU size for a tile group, default is 0 (no MTU targeting), "
472 "overrides maximum number of tile groups");
Fangwen Fu8d164de2016-12-14 13:40:54 -0800473#if CONFIG_TEMPMV_SIGNALING
474static const arg_def_t disable_tempmv = ARG_DEF(
475 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
476#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700477static const arg_def_t frame_parallel_decoding =
478 ARG_DEF(NULL, "frame-parallel", 1,
479 "Enable frame parallel decodability features "
480 "(0: false (default), 1: true)");
Thomas Davies3ab20b42017-09-19 10:30:53 +0100481#if !CONFIG_EXT_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100482static const arg_def_t aq_mode = ARG_DEF(
483 NULL, "aq-mode", 1,
484 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100485 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200486#else
Thomase28d92b2016-09-20 12:07:11 +0100487static const arg_def_t aq_mode = ARG_DEF(
488 NULL, "aq-mode", 1,
489 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100490 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200491#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700492#if CONFIG_EXT_DELTA_Q
493static const arg_def_t deltaq_mode = ARG_DEF(
494 NULL, "deltaq-mode", 1,
495 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
496#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700497static const arg_def_t frame_periodic_boost =
498 ARG_DEF(NULL, "frame-boost", 1,
499 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700500static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
501 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700502static const arg_def_t max_inter_rate_pct =
503 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700504static const arg_def_t min_gf_interval = ARG_DEF(
505 NULL, "min-gf-interval", 1,
506 "min gf/arf frame interval (default 0, indicating in-built behavior)");
507static const arg_def_t max_gf_interval = ARG_DEF(
508 NULL, "max-gf-interval", 1,
509 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800510
Yaowu Xufc996362015-02-10 15:03:05 -0800511static const struct arg_enum_list color_space_enum[] = {
Tom Finegan01d43e12017-08-17 09:21:42 -0700512 { "unknown", AOM_CS_UNKNOWN }, { "bt601", AOM_CS_BT_601 },
513 { "bt709", AOM_CS_BT_709 }, { "smpte170", AOM_CS_SMPTE_170 },
514 { "smpte240", AOM_CS_SMPTE_240 }, { "bt2020ncl", AOM_CS_BT_2020_NCL },
515 { "bt2020cl", AOM_CS_BT_2020_CL }, { "sRGB", AOM_CS_SRGB },
516 { "ICtCp", AOM_CS_ICTCP }, { NULL, 0 }
Yaowu Xufc996362015-02-10 15:03:05 -0800517};
518
clang-format6c4d83e2016-08-08 19:03:30 -0700519static const arg_def_t input_color_space =
clang-format4eafefe2017-09-04 12:51:20 -0700520 ARG_DEF_ENUM(NULL, "color-space", 1,
521 "The color space of input content:", color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800522
anorkin76fb1262017-03-22 15:12:12 -0700523static const struct arg_enum_list transfer_function_enum[] = {
524 { "unknown", AOM_TF_UNKNOWN },
525 { "bt709", AOM_TF_BT_709 },
526 { "pq", AOM_TF_PQ },
527 { "hlg", AOM_TF_HLG },
528 { NULL, 0 }
529};
530
531static const arg_def_t input_transfer_function = ARG_DEF_ENUM(
clang-format4eafefe2017-09-04 12:51:20 -0700532 NULL, "transfer-function", 1,
533 "The transfer function of input content:", transfer_function_enum);
anorkin76fb1262017-03-22 15:12:12 -0700534
535static const struct arg_enum_list chroma_sample_position_enum[] = {
536 { "unknown", AOM_CSP_UNKNOWN },
537 { "vertical", AOM_CSP_VERTICAL },
538 { "colocated", AOM_CSP_COLOCATED },
539 { NULL, 0 }
540};
541
542static const arg_def_t input_chroma_sample_position =
543 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
544 "The chroma sample position when chroma 4:2:0 is signaled:",
545 chroma_sample_position_enum);
anorkin76fb1262017-03-22 15:12:12 -0700546
Alex Converse219f6452014-08-06 11:01:18 -0700547static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700548 { "default", AOM_CONTENT_DEFAULT },
549 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700550 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700551};
552
553static const arg_def_t tune_content = ARG_DEF_ENUM(
554 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700555#endif
Alex Converse219f6452014-08-06 11:01:18 -0700556
Yaowu Xuf883b422016-08-30 14:01:10 -0700557#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000558#if CONFIG_EXT_PARTITION
559static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700560 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
561 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
562 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700563 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000564};
565static const arg_def_t superblock_size = ARG_DEF_ENUM(
566 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
567#endif // CONFIG_EXT_PARTITION
568
Yaowu Xuf883b422016-08-30 14:01:10 -0700569static const arg_def_t *av1_args[] = { &cpu_used_av1,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800570 &dev_sf_av1,
Yaowu Xuf883b422016-08-30 14:01:10 -0700571 &auto_altref,
572 &sharpness,
573 &static_thresh,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700574#if CONFIG_EXT_TILE
575 &single_tile_decoding,
576#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700577 &tile_cols,
578 &tile_rows,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800579#if CONFIG_DEPENDENT_HORZTILES
580 &tile_dependent_rows,
581#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800582#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800583 &tile_loopfilter,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800584#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700585 &arnr_maxframes,
586 &arnr_strength,
Yushin Chod808bfc2017-08-10 15:54:36 -0700587 &tune_metric,
Yaowu Xuf883b422016-08-30 14:01:10 -0700588 &cq_level,
589 &max_intra_rate_pct,
590 &max_inter_rate_pct,
591 &gf_cbr_boost_pct,
592 &lossless,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100593 &enable_cdef,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800594#if CONFIG_AOM_QM
595 &enable_qm,
596 &qm_min,
597 &qm_max,
598#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700599#if CONFIG_DIST_8X8
600 &enable_dist_8x8,
601#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700602 &frame_parallel_decoding,
603 &aq_mode,
Fangwen Fu6160df22017-04-24 09:45:51 -0700604#if CONFIG_EXT_DELTA_Q
605 &deltaq_mode,
606#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700607 &frame_periodic_boost,
608 &noise_sens,
609 &tune_content,
610 &input_color_space,
anorkin76fb1262017-03-22 15:12:12 -0700611 &input_transfer_function,
612 &input_chroma_sample_position,
Yaowu Xuf883b422016-08-30 14:01:10 -0700613 &min_gf_interval,
614 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000615#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700616 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000617#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000618 &num_tg,
619 &mtu_size,
Fangwen Fu8d164de2016-12-14 13:40:54 -0800620#if CONFIG_TEMPMV_SIGNALING
621 &disable_tempmv,
622#endif
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200623#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700624 &bitdeptharg,
625 &inbitdeptharg,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200626#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700627 NULL };
628static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800629 AOME_SET_DEVSF,
Yaowu Xuf883b422016-08-30 14:01:10 -0700630 AOME_SET_ENABLEAUTOALTREF,
631 AOME_SET_SHARPNESS,
632 AOME_SET_STATIC_THRESHOLD,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700633#if CONFIG_EXT_TILE
634 AV1E_SET_SINGLE_TILE_DECODING,
635#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700636 AV1E_SET_TILE_COLUMNS,
637 AV1E_SET_TILE_ROWS,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800638#if CONFIG_DEPENDENT_HORZTILES
639 AV1E_SET_TILE_DEPENDENT_ROWS,
640#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800641#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800642 AV1E_SET_TILE_LOOPFILTER,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800643#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700644 AOME_SET_ARNR_MAXFRAMES,
645 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700646 AOME_SET_TUNING,
647 AOME_SET_CQ_LEVEL,
648 AOME_SET_MAX_INTRA_BITRATE_PCT,
649 AV1E_SET_MAX_INTER_BITRATE_PCT,
650 AV1E_SET_GF_CBR_BOOST_PCT,
651 AV1E_SET_LOSSLESS,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100652 AV1E_SET_ENABLE_CDEF,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800653#if CONFIG_AOM_QM
654 AV1E_SET_ENABLE_QM,
655 AV1E_SET_QM_MIN,
656 AV1E_SET_QM_MAX,
657#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700658#if CONFIG_DIST_8X8
659 AV1E_SET_ENABLE_DIST_8X8,
660#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700661 AV1E_SET_FRAME_PARALLEL_DECODING,
662 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700663#if CONFIG_EXT_DELTA_Q
664 AV1E_SET_DELTAQ_MODE,
665#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700666 AV1E_SET_FRAME_PERIODIC_BOOST,
667 AV1E_SET_NOISE_SENSITIVITY,
668 AV1E_SET_TUNE_CONTENT,
669 AV1E_SET_COLOR_SPACE,
anorkin76fb1262017-03-22 15:12:12 -0700670 AV1E_SET_TRANSFER_FUNCTION,
671 AV1E_SET_CHROMA_SAMPLE_POSITION,
Yaowu Xuf883b422016-08-30 14:01:10 -0700672 AV1E_SET_MIN_GF_INTERVAL,
673 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000674#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700675 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000676#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000677 AV1E_SET_NUM_TG,
678 AV1E_SET_MTU,
Fangwen Fu8d164de2016-12-14 13:40:54 -0800679#if CONFIG_TEMPMV_SIGNALING
680 AV1E_SET_DISABLE_TEMPMV,
681#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700682 0 };
hui su82331e02015-08-16 18:21:56 -0700683#endif
684
John Koleszar0ea50ce2010-05-18 11:58:33 -0400685static const arg_def_t *no_args[] = { NULL };
686
James Zernfd8c78c2017-11-27 16:37:33 -0800687void show_help(FILE *fout, int shorthelp) {
688 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename \n",
John Koleszarc6b90392012-07-13 15:21:29 -0700689 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400690
James Zernfd8c78c2017-11-27 16:37:33 -0800691 if (shorthelp) {
692 fprintf(fout, "Use --help to see the full list of options.\n");
693 return;
694 }
695
696 fprintf(fout, "\nOptions:\n");
697 arg_show_usage(fout, main_args);
698 fprintf(fout, "\nEncoder Global Options:\n");
699 arg_show_usage(fout, global_args);
700 fprintf(fout, "\nRate Control Options:\n");
701 arg_show_usage(fout, rc_args);
702 fprintf(fout, "\nTwopass Rate Control Options:\n");
703 arg_show_usage(fout, rc_twopass_args);
704 fprintf(fout, "\nKeyframe Placement Options:\n");
705 arg_show_usage(fout, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700706#if CONFIG_AV1_ENCODER
James Zernfd8c78c2017-11-27 16:37:33 -0800707 fprintf(fout, "\nAV1 Specific Options:\n");
708 arg_show_usage(fout, av1_args);
hui su82331e02015-08-16 18:21:56 -0700709#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800710 fprintf(fout,
clang-format6c4d83e2016-08-08 19:03:30 -0700711 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700712 " The desired precision of timestamps in the output, expressed\n"
713 " in fractional seconds. Default is 1/1000.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800714 fprintf(fout, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400715
James Zernfd8c78c2017-11-27 16:37:33 -0800716 const int num_encoder = get_aom_encoder_count();
717 for (int i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700718 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700719 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
James Zernfd8c78c2017-11-27 16:37:33 -0800720 fprintf(fout, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700721 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800722 }
James Zernfd8c78c2017-11-27 16:37:33 -0800723 fprintf(fout, "\n ");
724 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
725}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400726
James Zernfd8c78c2017-11-27 16:37:33 -0800727void usage_exit(void) {
728 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700729 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400730}
731
Yaowu Xuf883b422016-08-30 14:01:10 -0700732#if CONFIG_AV1_ENCODER
733#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800734#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800735
James Zerndba73762014-05-10 17:44:12 -0700736#if !CONFIG_WEBM_IO
737typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700738struct WebmOutputContext {
739 int debug;
740};
James Zerndba73762014-05-10 17:44:12 -0700741#endif
742
John Koleszar9e50ed72012-02-15 12:39:38 -0800743/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800744struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700745 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700746 const char *out_fn;
747 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700748#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700749 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700750#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700751 stereo_format_t stereo_fmt;
752 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
753 int arg_ctrl_cnt;
754 int write_webm;
Cyril Concolato6c788832017-10-30 16:30:35 -0700755#if CONFIG_OBU_NO_IVF
756 int write_ivf;
757#endif
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700758 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700759 int use_16bit_internal;
John Koleszar9e50ed72012-02-15 12:39:38 -0800760};
761
John Koleszar6ad3b742012-11-06 12:02:42 -0800762struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700763 int index;
764 struct stream_state *next;
765 struct stream_config config;
766 FILE *file;
767 struct rate_hist *rate_hist;
768 struct WebmOutputContext webm_ctx;
769 uint64_t psnr_sse_total;
770 uint64_t psnr_samples_total;
771 double psnr_totals[4];
772 int psnr_count;
773 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700774 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700775 unsigned int frames_out;
776 uint64_t cx_time;
777 size_t nbytes;
778 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700779#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700780 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700781#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700782 struct aom_image *img;
783 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700784 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800785};
786
clang-format6c4d83e2016-08-08 19:03:30 -0700787static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700788 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800789 if (rat->den < 0) {
790 rat->num *= -1;
791 rat->den *= -1;
792 }
John Koleszar1b27e932012-04-25 17:08:56 -0700793
clang-format6c4d83e2016-08-08 19:03:30 -0700794 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700795
clang-format6c4d83e2016-08-08 19:03:30 -0700796 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700797}
798
Yaowu Xuf883b422016-08-30 14:01:10 -0700799static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700800 char **argi, **argj;
801 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700802 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700803
clang-format6c4d83e2016-08-08 19:03:30 -0700804 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800805
John Koleszar6ad3b742012-11-06 12:02:42 -0800806 /* Initialize default parameters */
807 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700808 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700809 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700810 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700811 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700812 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400813
John Koleszarc6b90392012-07-13 15:21:29 -0700814 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
815 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400816
James Zernfd8c78c2017-11-27 16:37:33 -0800817 if (arg_match(&arg, &help, argi)) {
818 show_help(stdout, 0);
819 exit(EXIT_SUCCESS);
820 } else if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700821 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800822 if (!global->codec)
823 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700824 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800825 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400826
John Koleszar6ad3b742012-11-06 12:02:42 -0800827 if (global->passes < 1 || global->passes > 2)
828 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700829 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800830 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400831
John Koleszar6ad3b742012-11-06 12:02:42 -0800832 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700833 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800834 } else if (arg_match(&arg, &usage, argi))
835 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700836 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800837 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700838 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700839 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar6ad3b742012-11-06 12:02:42 -0800840 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700841 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800842 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700843 global->color_type = I420;
844 else if (arg_match(&arg, &use_i422, argi))
845 global->color_type = I422;
846 else if (arg_match(&arg, &use_i444, argi))
847 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700848 else if (arg_match(&arg, &use_i440, argi))
849 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800850 else if (arg_match(&arg, &quietarg, argi))
851 global->quiet = 1;
852 else if (arg_match(&arg, &verbosearg, argi))
853 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700854 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800855 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700856 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800857 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700858 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800859 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700860 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800861 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700862 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800863 global->framerate = arg_parse_rational(&arg);
864 validate_positive_rational(arg.name, &global->framerate);
865 global->have_framerate = 1;
866 } else if (arg_match(&arg, &out_part, argi))
867 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700868 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800869 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700870 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800871 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700872 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800873 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800874 else if (arg_match(&arg, &disable_warnings, argi))
875 global->disable_warnings = 1;
876 else if (arg_match(&arg, &disable_warning_prompt, argi))
877 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800878 else
John Koleszarc6b90392012-07-13 15:21:29 -0700879 argj++;
880 }
881
John Koleszar6ad3b742012-11-06 12:02:42 -0800882 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700883 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800884 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700885 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
886 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800887 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800888 }
John Koleszarc6b90392012-07-13 15:21:29 -0700889 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800890 /* Validate global config */
891 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700892#if CONFIG_AV1_ENCODER
893 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800894 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700895 if (global->codec != NULL && global->codec->name != NULL)
Thomas Daede80826142017-03-20 15:44:24 -0700896 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800897#else
898 global->passes = 1;
899#endif
900 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800901}
902
Yaowu Xuf883b422016-08-30 14:01:10 -0700903static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800904 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700905 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
906 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -0800907
clang-format6c4d83e2016-08-08 19:03:30 -0700908 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -0800909
John Koleszar25b6e9f2013-02-12 21:17:56 -0800910 if (!fseeko(input->file, 0, SEEK_END)) {
911 /* Input file is seekable. Figure out how long it is, so we can get
912 * progress info.
913 */
914 input->length = ftello(input->file);
915 rewind(input->file);
916 }
917
Frank Galligan09acd262015-06-01 10:20:58 -0700918 /* Default to 1:1 pixel aspect ratio. */
919 input->pixel_aspect_ratio.numerator = 1;
920 input->pixel_aspect_ratio.denominator = 1;
921
John Koleszar6ad3b742012-11-06 12:02:42 -0800922 /* For RAW input sources, these bytes will applied on the first frame
923 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -0700924 */
John Koleszar6ad3b742012-11-06 12:02:42 -0800925 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
926 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400927
clang-format6c4d83e2016-08-08 19:03:30 -0700928 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -0700929 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
930 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800931 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800932 input->width = input->y4m.pic_w;
933 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -0700934 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
935 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800936 input->framerate.numerator = input->y4m.fps_n;
937 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -0700938 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -0700939 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -0800940 } else
941 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -0800942 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -0800943 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -0800944 } else {
945 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700946 }
John Koleszar6ad3b742012-11-06 12:02:42 -0800947}
948
Yaowu Xuf883b422016-08-30 14:01:10 -0700949static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800950 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -0700951 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -0800952}
953
Yaowu Xuf883b422016-08-30 14:01:10 -0700954static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -0800955 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800956 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800957
John Koleszar6ad3b742012-11-06 12:02:42 -0800958 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700959 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800960 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700961 }
962
John Koleszar6ad3b742012-11-06 12:02:42 -0800963 if (prev) {
964 memcpy(stream, prev, sizeof(*stream));
965 stream->index++;
966 prev->next = stream;
967 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700968 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -0800969
John Koleszar6ad3b742012-11-06 12:02:42 -0800970 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -0700971 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -0700972 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -0700973 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -0800974
John Koleszar6ad3b742012-11-06 12:02:42 -0800975 /* Change the default timebase to a high enough value so that the
976 * encoder will always create strictly increasing timestamps.
977 */
978 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -0800979
John Koleszar6ad3b742012-11-06 12:02:42 -0800980 /* Never use the library's default resolution, require it be parsed
981 * from the file or set on the command line.
982 */
983 stream->config.cfg.g_w = 0;
984 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800985
John Koleszar6ad3b742012-11-06 12:02:42 -0800986 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -0800987 stream->config.write_webm = 1;
Cyril Concolato6c788832017-10-30 16:30:35 -0700988#if CONFIG_OBU_NO_IVF
989 stream->config.write_ivf = 0;
990#endif
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700991#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -0700992 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700993 stream->webm_ctx.last_pts_ns = -1;
994 stream->webm_ctx.writer = NULL;
995 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700996#endif
Johann87c40b32012-03-01 16:12:53 -0800997
John Koleszar6ad3b742012-11-06 12:02:42 -0800998 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700999 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -08001000 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001001
John Koleszar6ad3b742012-11-06 12:02:42 -08001002 /* Output files must be specified for each stream */
1003 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001004
John Koleszar6ad3b742012-11-06 12:02:42 -08001005 stream->next = NULL;
1006 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001007}
1008
Yaowu Xuf883b422016-08-30 14:01:10 -07001009static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001010 struct stream_state *stream, char **argv) {
1011 char **argi, **argj;
1012 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001013 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001014 static const int *ctrl_args_map = NULL;
1015 struct stream_config *config = &stream->config;
1016 int eos_mark_found = 0;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001017 int webm_forced = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001018
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001019 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001020 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001021#if CONFIG_AV1_ENCODER
1022 } else if (strcmp(global->codec->name, "av1") == 0) {
1023 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1024 // Consider to expand this set for AV1 encoder control.
1025 ctrl_args = av1_args;
1026 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001027#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001028 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001029
John Koleszarc6b90392012-07-13 15:21:29 -07001030 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001031 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001032
John Koleszar6ad3b742012-11-06 12:02:42 -08001033 /* Once we've found an end-of-stream marker (--) we want to continue
1034 * shifting arguments but not consuming them.
1035 */
1036 if (eos_mark_found) {
1037 argj++;
1038 continue;
1039 } else if (!strcmp(*argj, "--")) {
1040 eos_mark_found = 1;
1041 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001042 }
1043
Adrian Granged2401802015-02-13 14:51:32 -08001044 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001045 config->out_fn = arg.val;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001046 if (!webm_forced) {
1047 const size_t out_fn_len = strlen(config->out_fn);
1048 if (out_fn_len >= 4 &&
1049 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
1050 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001051#if CONFIG_OBU_NO_IVF
1052 config->write_ivf = 1;
1053#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001054 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001055#if CONFIG_OBU_NO_IVF
1056 else if (out_fn_len >= 4 &&
1057 !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
1058 config->write_webm = 0;
1059 config->write_ivf = 0;
1060 }
1061#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001062 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001063 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001064 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001065#if CONFIG_FP_MB_STATS
1066 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1067 config->fpmb_stats_fn = arg.val;
1068#endif
Johannb50e5182015-01-30 15:05:14 -08001069 } else if (arg_match(&arg, &use_webm, argi)) {
1070#if CONFIG_WEBM_IO
1071 config->write_webm = 1;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001072 webm_forced = 1;
Johannb50e5182015-01-30 15:05:14 -08001073#else
1074 die("Error: --webm specified but webm is disabled.");
1075#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001076 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001077 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001078#if CONFIG_OBU_NO_IVF
1079 config->write_ivf = 1;
1080#endif
1081#if CONFIG_OBU_NO_IVF
1082 } else if (arg_match(&arg, &use_obu, argi)) {
1083 config->write_webm = 0;
1084 config->write_ivf = 0;
1085#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001086 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001087 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001088 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001089 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001090 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001091 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001092 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001093 config->cfg.g_h = arg_parse_uint(&arg);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001094#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001095 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1096 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1097 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1098 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1099#endif
James Zerndba73762014-05-10 17:44:12 -07001100#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001101 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001102 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001103#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001104 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001105 config->cfg.g_timebase = arg_parse_rational(&arg);
1106 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001107 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001108 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001109 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001110 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001111#if CONFIG_EXT_TILE
1112 } else if (arg_match(&arg, &large_scale_tile, argi)) {
1113 config->cfg.large_scale_tile = arg_parse_uint(&arg);
1114#endif // CONFIG_EXT_TILE
Rupert Swarbricke0b15992017-11-09 15:04:32 +00001115#if CONFIG_MONO_VIDEO
1116 } else if (arg_match(&arg, &monochrome, argi)) {
1117 config->cfg.monochrome = 1;
1118#endif // CONFIG_MONO_VIDEO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001119 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001120 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001121 } else if (arg_match(&arg, &resize_mode, argi)) {
1122 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001123 } else if (arg_match(&arg, &resize_denominator, argi)) {
1124 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1125 } else if (arg_match(&arg, &resize_kf_denominator, argi)) {
1126 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001127#if CONFIG_FRAME_SUPERRES
1128 } else if (arg_match(&arg, &superres_mode, argi)) {
1129 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001130 } else if (arg_match(&arg, &superres_denominator, argi)) {
1131 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1132 } else if (arg_match(&arg, &superres_kf_denominator, argi)) {
1133 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001134 } else if (arg_match(&arg, &superres_qthresh, argi)) {
1135 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1136 } else if (arg_match(&arg, &superres_kf_qthresh, argi)) {
1137 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001138#endif // CONFIG_FRAME_SUPERRES
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001139 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001140 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001141 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001142 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001143 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001144 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001145 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001146 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001147 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001148 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001149 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001150 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001151 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001152 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001153 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001154 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001155 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001156 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001157 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001158 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001159 if (global->passes < 2)
1160 warn("option %s ignored in one-pass mode.\n", arg.name);
1161 } else if (arg_match(&arg, &minsection_pct, argi)) {
1162 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1163
1164 if (global->passes < 2)
1165 warn("option %s ignored in one-pass mode.\n", arg.name);
1166 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1167 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1168
1169 if (global->passes < 2)
1170 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001171 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001172 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001173 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001174 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001175 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001176 config->cfg.kf_mode = AOM_KF_DISABLED;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001177#if CONFIG_MAX_TILE
1178 } else if (arg_match(&arg, &tile_width, argi)) {
1179 config->cfg.tile_width_count =
1180 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1181 } else if (arg_match(&arg, &tile_height, argi)) {
1182 config->cfg.tile_height_count =
1183 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1184#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001185 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001186 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001187 for (i = 0; ctrl_args[i]; i++) {
1188 if (arg_match(&arg, ctrl_args[i], argi)) {
1189 int j;
1190 match = 1;
1191
1192 /* Point either to the next free element or the first
Cyril Concolato6c788832017-10-30 16:30:35 -07001193 * instance of this control.
1194 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001195 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001196 if (ctrl_args_map != NULL &&
1197 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001198 break;
1199
1200 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001201 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001202 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001203 config->arg_ctrls[j][0] = ctrl_args_map[i];
1204 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001205 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001206 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001207 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001208 }
clang-format6c4d83e2016-08-08 19:03:30 -07001209 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001210 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001211 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001212#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan14af5b92017-05-12 14:27:30 +02001213 config->use_16bit_internal =
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +02001214 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001215#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001216 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001217}
1218
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001219#define FOREACH_STREAM(iterator, list) \
1220 for (struct stream_state *iterator = list; iterator; \
1221 iterator = iterator->next)
John Koleszar9e50ed72012-02-15 12:39:38 -08001222
Alex Conversed66bd222014-02-21 10:52:09 -08001223static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001224 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001225 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001226 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001227
John Koleszar6ad3b742012-11-06 12:02:42 -08001228 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001229 fatal(
1230 "Stream %d: Specify stream dimensions with --width (-w) "
1231 " and --height (-h)",
1232 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001233
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001234 // Check that the codec bit depth is greater than the input bit depth.
1235 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001236 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001237 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1238 stream->index, (int)stream->config.cfg.g_bit_depth,
1239 stream->config.cfg.g_input_bit_depth);
1240 }
1241
John Koleszar6ad3b742012-11-06 12:02:42 -08001242 for (streami = stream; streami; streami = streami->next) {
1243 /* All streams require output files */
1244 if (!streami->config.out_fn)
1245 fatal("Stream %d: Output file is required (specify with -o)",
1246 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001247
John Koleszar6ad3b742012-11-06 12:02:42 -08001248 /* Check for two streams outputting to the same file */
1249 if (streami != stream) {
1250 const char *a = stream->config.out_fn;
1251 const char *b = streami->config.out_fn;
1252 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1253 fatal("Stream %d: duplicate output file (from stream %d)",
1254 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001255 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001256
1257 /* Check for two streams sharing a stats file. */
1258 if (streami != stream) {
1259 const char *a = stream->config.stats_fn;
1260 const char *b = streami->config.stats_fn;
1261 if (a && b && !strcmp(a, b))
1262 fatal("Stream %d: duplicate stats file (from stream %d)",
1263 streami->index, stream->index);
1264 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001265
1266#if CONFIG_FP_MB_STATS
1267 /* Check for two streams sharing a mb stats file. */
1268 if (streami != stream) {
1269 const char *a = stream->config.fpmb_stats_fn;
1270 const char *b = streami->config.fpmb_stats_fn;
1271 if (a && b && !strcmp(a, b))
1272 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1273 streami->index, stream->index);
1274 }
1275#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001276 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001277}
1278
clang-format6c4d83e2016-08-08 19:03:30 -07001279static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001280 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001281 if (!stream->config.cfg.g_w) {
1282 if (!stream->config.cfg.g_h)
1283 stream->config.cfg.g_w = w;
1284 else
1285 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1286 }
1287 if (!stream->config.cfg.g_h) {
1288 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1289 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001290}
1291
clang-format6c4d83e2016-08-08 19:03:30 -07001292static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001293 switch (t) {
1294 case FILE_TYPE_RAW: return "RAW";
1295 case FILE_TYPE_Y4M: return "Y4M";
1296 default: return "Other";
1297 }
1298}
1299
Yaowu Xuf883b422016-08-30 14:01:10 -07001300static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001301 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001302 case AOM_IMG_FMT_I420: return "I420";
1303 case AOM_IMG_FMT_I422: return "I422";
1304 case AOM_IMG_FMT_I444: return "I444";
1305 case AOM_IMG_FMT_I440: return "I440";
1306 case AOM_IMG_FMT_YV12: return "YV12";
1307 case AOM_IMG_FMT_I42016: return "I42016";
1308 case AOM_IMG_FMT_I42216: return "I42216";
1309 case AOM_IMG_FMT_I44416: return "I44416";
1310 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001311 default: return "Other";
1312 }
1313}
Ralph Giles061a16d2012-01-05 15:05:05 -06001314
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001315static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001316 struct AvxEncoderConfig *global,
1317 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001318#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001319 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001320
John Koleszar6ad3b742012-11-06 12:02:42 -08001321 if (stream->index == 0) {
1322 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001323 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001324 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001325 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001326 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001327 }
1328 if (stream->next || stream->index)
1329 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1330 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
Sebastien Alaiwan27511922017-06-05 15:25:46 +02001331 fprintf(stderr, "Coding path: %s\n",
1332 stream->config.use_16bit_internal ? "HBD" : "LBD");
John Koleszar6ad3b742012-11-06 12:02:42 -08001333 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001334
John Koleszar6ad3b742012-11-06 12:02:42 -08001335 SHOW(g_usage);
1336 SHOW(g_threads);
1337 SHOW(g_profile);
1338 SHOW(g_w);
1339 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001340 SHOW(g_bit_depth);
1341 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001342 SHOW(g_timebase.num);
1343 SHOW(g_timebase.den);
1344 SHOW(g_error_resilient);
1345 SHOW(g_pass);
1346 SHOW(g_lag_in_frames);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001347#if CONFIG_EXT_TILE
1348 SHOW(large_scale_tile);
1349#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001350 SHOW(rc_dropframe_thresh);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001351 SHOW(rc_resize_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001352 SHOW(rc_resize_denominator);
1353 SHOW(rc_resize_kf_denominator);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001354#if CONFIG_FRAME_SUPERRES
1355 SHOW(rc_superres_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001356 SHOW(rc_superres_denominator);
1357 SHOW(rc_superres_kf_denominator);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001358 SHOW(rc_superres_qthresh);
1359 SHOW(rc_superres_kf_qthresh);
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001360#endif // CONFIG_FRAME_SUPERRES
John Koleszar6ad3b742012-11-06 12:02:42 -08001361 SHOW(rc_end_usage);
1362 SHOW(rc_target_bitrate);
1363 SHOW(rc_min_quantizer);
1364 SHOW(rc_max_quantizer);
1365 SHOW(rc_undershoot_pct);
1366 SHOW(rc_overshoot_pct);
1367 SHOW(rc_buf_sz);
1368 SHOW(rc_buf_initial_sz);
1369 SHOW(rc_buf_optimal_sz);
1370 SHOW(rc_2pass_vbr_bias_pct);
1371 SHOW(rc_2pass_vbr_minsection_pct);
1372 SHOW(rc_2pass_vbr_maxsection_pct);
1373 SHOW(kf_mode);
1374 SHOW(kf_min_dist);
1375 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001376}
1377
John Koleszar9e50ed72012-02-15 12:39:38 -08001378static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001379 struct AvxEncoderConfig *global,
1380 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001381 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001382 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001383
Yaowu Xuf883b422016-08-30 14:01:10 -07001384 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001385
John Koleszar6ad3b742012-11-06 12:02:42 -08001386 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001387
clang-format6c4d83e2016-08-08 19:03:30 -07001388 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001389
John Koleszar6ad3b742012-11-06 12:02:42 -08001390 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1391 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001392
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001393#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001394 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001395 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001396 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1397 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001398 }
James Zernc8e5a772016-02-11 18:53:50 -08001399#else
1400 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001401#endif
1402
Cyril Concolato6c788832017-10-30 16:30:35 -07001403 if (!stream->config.write_webm
1404#if CONFIG_OBU_NO_IVF
1405 && stream->config.write_ivf
1406#endif
1407 ) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001408 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1409 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001410}
1411
John Koleszar9e50ed72012-02-15 12:39:38 -08001412static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001413 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001414 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001415
Yaowu Xuf883b422016-08-30 14:01:10 -07001416 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001417
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001418#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001419 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001420 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001421 }
1422#endif
1423
Cyril Concolato6c788832017-10-30 16:30:35 -07001424 if (!stream->config.write_webm
1425#if CONFIG_OBU_NO_IVF
1426 && stream->config.write_ivf
1427#endif
1428 ) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001429 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001430 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001431 stream->frames_out);
1432 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001433
John Koleszar6ad3b742012-11-06 12:02:42 -08001434 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001435}
1436
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001437static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001438 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001439 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001440 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001441 fatal("Failed to open statistics store");
1442 } else {
1443 if (!stats_open_mem(&stream->stats, pass))
1444 fatal("Failed to open statistics store");
1445 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001446
Pengchong Jinf349b072014-07-14 09:13:38 -07001447#if CONFIG_FP_MB_STATS
1448 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001449 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1450 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001451 fatal("Failed to open mb statistics store");
1452 } else {
1453 if (!stats_open_mem(&stream->fpmb_stats, pass))
1454 fatal("Failed to open mb statistics store");
1455 }
1456#endif
1457
John Koleszar6ad3b742012-11-06 12:02:42 -08001458 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001459 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1460 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001461 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001462 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001463#if CONFIG_FP_MB_STATS
1464 stream->config.cfg.rc_firstpass_mb_stats_in =
1465 stats_get(&stream->fpmb_stats);
1466#endif
1467 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001468
John Koleszar6ad3b742012-11-06 12:02:42 -08001469 stream->cx_time = 0;
1470 stream->nbytes = 0;
1471 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001472}
1473
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001474static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001475 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001476 int i;
1477 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001478
Yaowu Xuf883b422016-08-30 14:01:10 -07001479 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1480 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001481#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001482 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001483#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001484
John Koleszar6ad3b742012-11-06 12:02:42 -08001485 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001486 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001487 &stream->config.cfg, flags);
1488 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001489
Yaowu Xuf883b422016-08-30 14:01:10 -07001490 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001491 * we're being clever to store the control IDs in an array. Real
1492 * applications will want to make use of the enumerations directly
1493 */
1494 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1495 int ctrl = stream->config.arg_ctrls[i][0];
1496 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001497 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001498 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001499
John Koleszar6ad3b742012-11-06 12:02:42 -08001500 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1501 }
1502
Tom Fineganba02c242017-05-16 15:01:54 -07001503#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001504 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001505 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
Imdad Sardharwalla730c8052017-11-30 17:39:15 +00001506 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, 0 };
Yaowu Xuf883b422016-08-30 14:01:10 -07001507 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001508
Tom Fineganba02c242017-05-16 15:01:54 -07001509#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07001510 if (strcmp(global->codec->name, "av1") == 0) {
1511 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001512 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1513
Yaowu Xuf883b422016-08-30 14:01:10 -07001514 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001515 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1516 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001517#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001518 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001519#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001520}
1521
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001522static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001523 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001524 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001525 aom_codec_pts_t frame_start, next_frame_start;
1526 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1527 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001528
clang-format6c4d83e2016-08-08 19:03:30 -07001529 frame_start =
1530 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1531 cfg->g_timebase.num / global->framerate.num;
1532 next_frame_start =
1533 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1534 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001535
clang-format6c4d83e2016-08-08 19:03:30 -07001536/* Scale if necessary */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001537#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001538 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001539 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001540 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001541 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001542 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1543 exit(EXIT_FAILURE);
1544 }
1545#if CONFIG_LIBYUV
1546 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001547 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001548 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001549 }
clang-format6c4d83e2016-08-08 19:03:30 -07001550 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001551 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1552 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1553 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1554 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1555 stream->img->stride[AOM_PLANE_Y] / 2,
1556 (uint16 *)stream->img->planes[AOM_PLANE_U],
1557 stream->img->stride[AOM_PLANE_U] / 2,
1558 (uint16 *)stream->img->planes[AOM_PLANE_V],
1559 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001560 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001561 img = stream->img;
1562#else
clang-format6c4d83e2016-08-08 19:03:30 -07001563 stream->encoder.err = 1;
1564 ctx_exit_on_error(&stream->encoder,
1565 "Stream %d: Failed to encode frame.\n"
1566 "Scaling disabled in this configuration. \n"
1567 "To enable, configure with --enable-libyuv\n",
1568 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001569#endif
1570 }
1571 }
1572#endif
John Koleszar34882b92012-03-01 12:50:40 -08001573 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001574 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001575 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1576 exit(EXIT_FAILURE);
1577 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001578#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001579 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001580 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001581 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001582 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001583 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1584 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1585 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1586 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1587 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1588 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001589 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001590 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001591#else
1592 stream->encoder.err = 1;
1593 ctx_exit_on_error(&stream->encoder,
1594 "Stream %d: Failed to encode frame.\n"
1595 "Scaling disabled in this configuration. \n"
1596 "To enable, configure with --enable-libyuv\n",
1597 stream->index);
1598#endif
John Koleszar34882b92012-03-01 12:50:40 -08001599 }
1600
Yaowu Xuf883b422016-08-30 14:01:10 -07001601 aom_usec_timer_start(&timer);
1602 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001603 (unsigned long)(next_frame_start - frame_start), 0,
1604 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001605 aom_usec_timer_mark(&timer);
1606 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001607 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1608 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001609}
1610
John Koleszar6ad3b742012-11-06 12:02:42 -08001611static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001612 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001613 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001614
Yaowu Xuf883b422016-08-30 14:01:10 -07001615 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001616 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1617 stream->counts[q]++;
1618 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001619}
1620
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001621static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001622 struct AvxEncoderConfig *global, int *got_data) {
1623 const aom_codec_cx_pkt_t *pkt;
1624 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1625 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001626
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001627 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001628 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001629 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001630 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001631
John Koleszar6ad3b742012-11-06 12:02:42 -08001632 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001633 case AOM_CODEC_CX_FRAME_PKT:
1634 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001635 stream->frames_out++;
1636 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001637 if (!global->quiet)
1638 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001639
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001640 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001641#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001642 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001643 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001644 }
1645#endif
1646 if (!stream->config.write_webm) {
Cyril Concolato6c788832017-10-30 16:30:35 -07001647#if CONFIG_OBU_NO_IVF
1648 if (stream->config.write_ivf) {
1649#endif
1650 if (pkt->data.frame.partition_id <= 0) {
1651 ivf_header_pos = ftello(stream->file);
1652 fsize = pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001653
Cyril Concolato6c788832017-10-30 16:30:35 -07001654 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1655 } else {
1656 fsize += pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001657
Cyril Concolato6c788832017-10-30 16:30:35 -07001658 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1659 const FileOffset currpos = ftello(stream->file);
1660 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1661 ivf_write_frame_size(stream->file, fsize);
1662 fseeko(stream->file, currpos, SEEK_SET);
1663 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001664 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001665#if CONFIG_OBU_NO_IVF
John Koleszar6ad3b742012-11-06 12:02:42 -08001666 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001667#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001668
clang-format6c4d83e2016-08-08 19:03:30 -07001669 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1670 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001671 }
1672 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001673
John Koleszar9e50ed72012-02-15 12:39:38 -08001674 *got_data = 1;
Tom Fineganba02c242017-05-16 15:01:54 -07001675#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001676 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001677 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001678 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001679 if (stream->decoder.err) {
1680 warn_or_exit_on_error(&stream->decoder,
1681 global->test_decode == TEST_DECODE_FATAL,
1682 "Failed to decode frame %d in stream %d",
1683 stream->frames_out + 1, stream->index);
1684 stream->mismatch_seen = stream->frames_out + 1;
1685 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001686 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001687#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001688 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001689 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001690 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001691 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001692 pkt->data.twopass_stats.sz);
1693 stream->nbytes += pkt->data.raw.sz;
1694 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001695#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001696 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001697 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001698 pkt->data.firstpass_mb_stats.sz);
1699 stream->nbytes += pkt->data.raw.sz;
1700 break;
1701#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001702 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001703
1704 if (global->show_psnr) {
1705 int i;
1706
1707 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1708 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1709 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001710 if (!global->quiet)
1711 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001712 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1713 }
1714 stream->psnr_count++;
1715 }
1716
1717 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001718 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001719 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001720 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001721}
1722
clang-format6c4d83e2016-08-08 19:03:30 -07001723static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001724 int i;
1725 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001726
clang-format6c4d83e2016-08-08 19:03:30 -07001727 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001728
John Koleszar6ad3b742012-11-06 12:02:42 -08001729 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001730 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001731 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001732 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001733
John Koleszar6ad3b742012-11-06 12:02:42 -08001734 for (i = 0; i < 4; i++) {
1735 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1736 }
1737 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001738}
1739
John Koleszar25b6e9f2013-02-12 21:17:56 -08001740static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001741 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001742}
1743
clang-format6c4d83e2016-08-08 19:03:30 -07001744static void test_decode(struct stream_state *stream,
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00001745 enum TestDecodeFatality fatal, int is_mono) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001746 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001747
clang-format6c4d83e2016-08-08 19:03:30 -07001748 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001749
John Koleszarb3c350a2013-03-13 12:15:43 -07001750 /* Get the internal reference frame */
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001751 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1752 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001753
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001754#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001755 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1756 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1757 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1758 aom_image_t enc_hbd_img;
1759 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1760 enc_img.d_w, enc_img.d_h, 16);
1761 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1762 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001763 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001764 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1765 aom_image_t dec_hbd_img;
1766 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1767 dec_img.d_w, dec_img.d_h, 16);
1768 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1769 dec_img = dec_hbd_img;
1770 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001771 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001772#endif
1773
John Koleszar6ad3b742012-11-06 12:02:42 -08001774 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001775 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001776
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00001777 if (!aom_compare_img(&enc_img, &dec_img, is_mono ? 1 : 3)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001778 int y[4], u[4], v[4];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001779#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001780 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001781 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001782 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001783 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001784 }
1785#else
Urvang Joshi09c293e2017-04-20 17:56:27 -07001786 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001787#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001788 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001789 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001790 "Stream %d: Encode/decode mismatch on frame %d at"
1791 " Y[%d, %d] {%d/%d},"
1792 " U[%d, %d] {%d/%d},"
1793 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001794 stream->index, stream->frames_out, y[0], y[1], y[2],
1795 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 -08001796 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001797 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001798
Yaowu Xuf883b422016-08-30 14:01:10 -07001799 aom_img_free(&enc_img);
1800 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001801}
John Koleszarefd54f82012-02-13 16:52:18 -08001802
John Koleszar25b6e9f2013-02-12 21:17:56 -08001803static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001804 int64_t hours;
1805 int64_t mins;
1806 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001807
1808 if (etl >= 0) {
1809 hours = etl / 3600;
1810 etl -= hours * 3600;
1811 mins = etl / 60;
1812 etl -= mins * 60;
1813 secs = etl;
1814
clang-format6c4d83e2016-08-08 19:03:30 -07001815 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1816 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001817 } else {
1818 fprintf(stderr, "[%3s unknown] ", label);
1819 }
1820}
1821
Tom Finegan44dd3272013-11-20 17:18:28 -08001822int main(int argc, const char **argv_) {
1823 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001824 aom_image_t raw;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001825#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001826 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001827 int allocated_raw_shift = 0;
1828 int use_16bit_internal = 0;
1829 int input_shift = 0;
1830#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001831 int frame_avail, got_data;
1832
Yaowu Xuf883b422016-08-30 14:01:10 -07001833 struct AvxInputContext input;
1834 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001835 struct stream_state *streams = NULL;
1836 char **argv, **argi;
1837 uint64_t cx_time = 0;
1838 int stream_cnt = 0;
1839 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001840 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001841
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001842 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001843 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001844
John Koleszar6ad3b742012-11-06 12:02:42 -08001845 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001846 input.framerate.numerator = 30;
1847 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001848 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001849 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001850
1851 /* First parse the global configuration values, because we want to apply
1852 * other parameters on top of the default configuration provided by the
1853 * codec.
1854 */
1855 argv = argv_dup(argc - 1, argv_ + 1);
1856 parse_global_config(&global, argv);
1857
James Zernfd8c78c2017-11-27 16:37:33 -08001858 if (argc < 3) usage_exit();
1859
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001860 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001861 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1862 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1863 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1864 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1865 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001866 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001867
John Koleszar6ad3b742012-11-06 12:02:42 -08001868 {
1869 /* Now parse each stream's parameters. Using a local scope here
1870 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1871 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001872 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001873 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001874
John Koleszar6ad3b742012-11-06 12:02:42 -08001875 do {
1876 stream = new_stream(&global, stream);
1877 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001878 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001879 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001880 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001881
John Koleszarc6b90392012-07-13 15:21:29 -07001882 /* Check for unrecognized options */
1883 for (argi = argv; *argi; argi++)
1884 if (argi[0][0] == '-' && argi[0][1])
1885 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001886
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001887 FOREACH_STREAM(stream, streams) {
1888 check_encoder_config(global.disable_warning_prompt, &global,
1889 &stream->config.cfg);
1890 }
Tom Finegan249366b2013-11-25 12:05:19 -08001891
John Koleszarc6b90392012-07-13 15:21:29 -07001892 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001893 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001894
James Zernfd8c78c2017-11-27 16:37:33 -08001895 if (!input.filename) {
1896 fprintf(stderr, "No input file specified!\n");
1897 usage_exit();
1898 }
John Koleszar53291892010-10-22 14:48:21 -04001899
John Koleszar8dd82872013-05-06 11:01:35 -07001900 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001901 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001902
John Koleszar6ad3b742012-11-06 12:02:42 -08001903 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001904 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001905 int64_t estimated_time_left = -1;
1906 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001907 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001908
John Koleszar6ad3b742012-11-06 12:02:42 -08001909 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001910
John Koleszar6ad3b742012-11-06 12:02:42 -08001911 /* If the input file doesn't specify its w/h (raw files), try to get
1912 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001913 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001914 if (!input.width || !input.height) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001915 FOREACH_STREAM(stream, streams) {
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001916 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1917 input.width = stream->config.cfg.g_w;
1918 input.height = stream->config.cfg.g_h;
1919 break;
1920 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001921 };
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001922 }
John Koleszarc6b90392012-07-13 15:21:29 -07001923
John Koleszar6ad3b742012-11-06 12:02:42 -08001924 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001925 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001926 fatal(
1927 "Specify stream dimensions with --width (-w) "
1928 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001929
1930 /* If input file does not specify bit-depth but input-bit-depth parameter
1931 * exists, assume that to be the input bit-depth. However, if the
1932 * input-bit-depth paramter does not exist, assume the input bit-depth
1933 * to be the same as the codec bit-depth.
1934 */
1935 if (!input.bit_depth) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001936 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001937 if (stream->config.cfg.g_input_bit_depth)
1938 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1939 else
1940 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1941 (int)stream->config.cfg.g_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001942 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001943 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001944 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001945 FOREACH_STREAM(stream, streams) {
1946 stream->config.cfg.g_input_bit_depth = input.bit_depth;
1947 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001948 }
1949
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001950 FOREACH_STREAM(stream, streams) {
Thomas Daede8ec53b22016-09-19 13:24:51 -07001951 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
1952 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
1953 was selected. */
1954 switch (stream->config.cfg.g_profile) {
1955 case 0:
1956 stream->config.cfg.g_profile = 1;
1957 profile_updated = 1;
1958 break;
1959 case 2:
1960 stream->config.cfg.g_profile = 3;
1961 profile_updated = 1;
1962 break;
1963 default: break;
1964 }
1965 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07001966 /* Automatically set the codec bit depth to match the input bit depth.
1967 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001968 if (stream->config.cfg.g_input_bit_depth >
1969 (unsigned int)stream->config.cfg.g_bit_depth) {
1970 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
1971 }
1972 if (stream->config.cfg.g_bit_depth > 8) {
1973 switch (stream->config.cfg.g_profile) {
1974 case 0:
1975 stream->config.cfg.g_profile = 2;
1976 profile_updated = 1;
1977 break;
1978 case 1:
1979 stream->config.cfg.g_profile = 3;
1980 profile_updated = 1;
1981 break;
1982 default: break;
1983 }
1984 }
1985 if (stream->config.cfg.g_profile > 1) {
Sebastien Alaiwanfadc7cb2017-06-08 22:10:43 +02001986 if (!CONFIG_HIGHBITDEPTH) fatal("Unsupported profile.");
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001987 stream->config.use_16bit_internal = 1;
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001988 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08001989 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001990 fprintf(stderr,
1991 "Warning: automatically upgrading to profile %d to "
1992 "match input format.\n",
1993 stream->config.cfg.g_profile);
1994 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001995 }
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001996
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001997 FOREACH_STREAM(stream, streams) {
1998 set_stream_dimensions(stream, input.width, input.height);
1999 }
2000 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
John Koleszar77e6b452010-11-03 13:58:40 -04002001
John Koleszar6ad3b742012-11-06 12:02:42 -08002002 /* Ensure that --passes and --pass are consistent. If --pass is set and
2003 * --passes=2, ensure --fpf was set.
2004 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002005 if (global.pass && global.passes == 2) {
2006 FOREACH_STREAM(stream, streams) {
clang-format6c4d83e2016-08-08 19:03:30 -07002007 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002008 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002009 " and --passes=2\n",
2010 stream->index, global.pass);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002011 }
2012 }
John Koleszar3245d462010-06-10 17:10:12 -04002013
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002014#if !CONFIG_WEBM_IO
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002015 FOREACH_STREAM(stream, streams) {
Ronald S. Bultje39775072015-12-16 13:35:43 -05002016 if (stream->config.write_webm) {
2017 stream->config.write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07002018#if CONFIG_OBU_NO_IVF
2019 stream->config.write_ivf = 0;
2020#endif
clang-format6c4d83e2016-08-08 19:03:30 -07002021 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002022 "aomenc was compiled without WebM container support."
Cyril Concolato6c788832017-10-30 16:30:35 -07002023#if CONFIG_OBU_NO_IVF
2024 "Producing OBU output");
2025#else
clang-format6c4d83e2016-08-08 19:03:30 -07002026 "Producing IVF output");
Cyril Concolato6c788832017-10-30 16:30:35 -07002027#endif
Ronald S. Bultje39775072015-12-16 13:35:43 -05002028 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002029 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002030#endif
2031
John Koleszar6ad3b742012-11-06 12:02:42 -08002032 /* Use the frame rate from the file only if none was specified
2033 * on the command-line.
2034 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002035 if (!global.have_framerate) {
2036 global.framerate.num = input.framerate.numerator;
2037 global.framerate.den = input.framerate.denominator;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002038 FOREACH_STREAM(stream, streams) {
2039 stream->config.cfg.g_timebase.den = global.framerate.num;
2040 stream->config.cfg.g_timebase.num = global.framerate.den;
2041 }
Tom Finegan00a35aa2013-11-14 12:37:42 -08002042 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002043
John Koleszar6ad3b742012-11-06 12:02:42 -08002044 /* Show configuration */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002045 if (global.verbose && pass == 0) {
2046 FOREACH_STREAM(stream, streams) {
2047 show_stream_config(stream, &global, &input);
2048 }
2049 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002050
2051 if (pass == (global.pass ? global.pass - 1 : 0)) {
2052 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002053 /*The Y4M reader does its own allocation.
2054 Just initialize this here to avoid problems if we never read any
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002055 frames.*/
John Koleszarc6b90392012-07-13 15:21:29 -07002056 memset(&raw, 0, sizeof(raw));
2057 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002058 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002059
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002060 FOREACH_STREAM(stream, streams) {
2061 stream->rate_hist =
2062 init_rate_histogram(&stream->config.cfg, &global.framerate);
2063 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002064 }
2065
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002066 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2067 FOREACH_STREAM(stream, streams) {
2068 open_output_file(stream, &global, &input.pixel_aspect_ratio);
2069 }
2070 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002071
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002072#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002073 if (strcmp(global.codec->name, "av1") == 0 ||
2074 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002075 // Check to see if at least one stream uses 16 bit internal.
2076 // Currently assume that the bit_depths for all streams using
2077 // highbitdepth are the same.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002078 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002079 if (stream->config.use_16bit_internal) {
2080 use_16bit_internal = 1;
2081 }
2082 if (stream->config.cfg.g_profile == 0) {
2083 input_shift = 0;
2084 } else {
2085 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07002086 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002087 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002088 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002089 }
2090#endif
2091
John Koleszarc6b90392012-07-13 15:21:29 -07002092 frame_avail = 1;
2093 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002094
John Koleszarc6b90392012-07-13 15:21:29 -07002095 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002096 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002097
John Koleszar6ad3b742012-11-06 12:02:42 -08002098 if (!global.limit || frames_in < global.limit) {
2099 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002100
clang-format6c4d83e2016-08-08 19:03:30 -07002101 if (frame_avail) frames_in++;
2102 seen_frames =
2103 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002104
John Koleszar6ad3b742012-11-06 12:02:42 -08002105 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002106 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002107 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2108
John Koleszar6ad3b742012-11-06 12:02:42 -08002109 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002110 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2111 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002112 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002113 fprintf(stderr, "frame %4d ", frames_in);
2114
clang-format6c4d83e2016-08-08 19:03:30 -07002115 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002116 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002117 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002118 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002119 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002120 }
2121
hui su251e1512016-10-03 15:48:43 -07002122 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002123 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002124 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002125
John Koleszar6ad3b742012-11-06 12:02:42 -08002126 if (frames_in > global.skip_frames) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002127#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002128 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002129 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2130 assert(use_16bit_internal);
2131 // Input bit depth and stream bit depth do not match, so up
2132 // shift frame to stream bit depth
2133 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002134 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002135 input.width, input.height, 32);
2136 allocated_raw_shift = 1;
2137 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002138 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002139 frame_to_encode = &raw_shift;
2140 } else {
2141 frame_to_encode = &raw;
2142 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002143 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002144 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002145 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002146 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002147 if (stream->config.use_16bit_internal)
2148 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002149 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002150 else
2151 assert(0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002152 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002153 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002154 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002155 FOREACH_STREAM(stream, streams) {
2156 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2157 frames_in);
2158 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002159 }
2160#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002161 aom_usec_timer_start(&timer);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002162 FOREACH_STREAM(stream, streams) {
2163 encode_frame(stream, &global, frame_avail ? &raw : NULL, frames_in);
2164 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002165#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002166 aom_usec_timer_mark(&timer);
2167 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002168
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002169 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
John Koleszarc6b90392012-07-13 15:21:29 -07002170
John Koleszar0ea50ce2010-05-18 11:58:33 -04002171 got_data = 0;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002172 FOREACH_STREAM(stream, streams) {
2173 get_cx_data(stream, &global, &got_data);
2174 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002175
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002176 if (!got_data && input.length && streams != NULL &&
2177 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002178 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002179 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002180 int64_t remaining;
2181 int64_t rate;
2182
2183 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002184 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002185
2186 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002187 remaining = 1000 * (global.limit - global.skip_frames -
2188 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002189 } else {
James Zerne3578af2014-04-17 10:47:08 -07002190 const int64_t input_pos = ftello(input.file);
2191 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002192 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002193
2194 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002195 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002196 }
2197
clang-format6c4d83e2016-08-08 19:03:30 -07002198 average_rate =
2199 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002200 estimated_time_left = average_rate ? remaining / average_rate : -1;
2201 }
2202
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002203 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2204 FOREACH_STREAM(stream, streams) {
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00002205 test_decode(stream, global.test_decode,
2206 stream->config.cfg.monochrome);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002207 }
2208 }
John Koleszarc6b90392012-07-13 15:21:29 -07002209 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002210
John Koleszarc6b90392012-07-13 15:21:29 -07002211 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002212 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002213 }
2214
clang-format6c4d83e2016-08-08 19:03:30 -07002215 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002216
Deb Mukherjeea160d722014-09-30 21:56:33 -07002217 if (!global.quiet) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002218 FOREACH_STREAM(stream, streams) {
2219 fprintf(stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2220 "b/f %7" PRId64
2221 "b/s"
2222 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2223 pass + 1, global.passes, frames_in, stream->frames_out,
2224 (int64_t)stream->nbytes,
2225 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2226 seen_frames
2227 ? (int64_t)stream->nbytes * 8 *
2228 (int64_t)global.framerate.num / global.framerate.den /
2229 seen_frames
2230 : 0,
2231 stream->cx_time > 9999999 ? stream->cx_time / 1000
2232 : stream->cx_time,
2233 stream->cx_time > 9999999 ? "ms" : "us",
2234 usec_to_fps(stream->cx_time, seen_frames));
2235 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002236 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002237
Deb Mukherjeea160d722014-09-30 21:56:33 -07002238 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002239 if (global.codec->fourcc == AV1_FOURCC) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002240 FOREACH_STREAM(stream, streams) {
2241 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2242 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002243 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002244 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002245 }
2246 }
John Koleszarc6b90392012-07-13 15:21:29 -07002247
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002248 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002249
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002250 if (global.test_decode != TEST_DECODE_OFF) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002251 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002252 }
2253
John Koleszar6ad3b742012-11-06 12:02:42 -08002254 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002255
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002256 if (global.test_decode == TEST_DECODE_FATAL) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002257 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002258 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002259 FOREACH_STREAM(stream, streams) {
2260 close_output_file(stream, global.codec->fourcc);
2261 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002262
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002263 FOREACH_STREAM(stream, streams) {
2264 stats_close(&stream->stats, global.passes - 1);
2265 }
John Koleszarc6b90392012-07-13 15:21:29 -07002266
Pengchong Jinf349b072014-07-14 09:13:38 -07002267#if CONFIG_FP_MB_STATS
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002268 FOREACH_STREAM(stream, streams) {
2269 stats_close(&stream->fpmb_stats, global.passes - 1);
2270 }
Pengchong Jinf349b072014-07-14 09:13:38 -07002271#endif
2272
clang-format6c4d83e2016-08-08 19:03:30 -07002273 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002274 }
2275
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002276 if (global.show_q_hist_buckets) {
2277 FOREACH_STREAM(stream, streams) {
2278 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2279 }
2280 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002281
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002282 if (global.show_rate_hist_buckets) {
2283 FOREACH_STREAM(stream, streams) {
2284 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2285 global.show_rate_hist_buckets);
2286 }
2287 }
2288 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002289
Ronald S. Bultje31214972012-10-11 10:13:48 -07002290#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002291 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2292 * to match some existing utilities.
2293 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002294 if (!(global.pass == 1 && global.passes == 2)) {
2295 FOREACH_STREAM(stream, streams) {
Yaowu Xu47e784e2013-10-16 16:29:28 -07002296 FILE *f = fopen("opsnr.stt", "a");
2297 if (stream->mismatch_seen) {
2298 fprintf(f, "First mismatch occurred in frame %d\n",
2299 stream->mismatch_seen);
2300 } else {
2301 fprintf(f, "No mismatch detected in recon buffers\n");
2302 }
2303 fclose(f);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002304 }
2305 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002306#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002307
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002308#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002309 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002310#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002311 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002312 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002313 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002314 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002315}