blob: 237551023da06b7f0d9a9d46a04f15bd65cd730f [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
John Koleszar0ea50ce2010-05-18 11:58:33 -0400272
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700273static const arg_def_t *global_args[] = { &use_yv12,
274 &use_i420,
275 &use_i422,
276 &use_i444,
277 &use_i440,
278 &usage,
279 &threads,
280 &profile,
281 &width,
282 &height,
James Zerndba73762014-05-10 17:44:12 -0700283#if CONFIG_WEBM_IO
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700284 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700285#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700286 &timebase,
287 &framerate,
288 &error_resilient,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200289#if CONFIG_HIGHBITDEPTH
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700290 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700291#endif
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700292 &lag_in_frames,
293#if CONFIG_EXT_TILE
294 &large_scale_tile,
295#endif // CONFIG_EXT_TILE
296 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400297
clang-format6c4d83e2016-08-08 19:03:30 -0700298static const arg_def_t dropframe_thresh =
299 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
Debargha Mukherjee29e40a62017-06-14 09:37:12 -0700300static const arg_def_t resize_mode =
301 ARG_DEF(NULL, "resize-mode", 1, "Frame resize mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700302static const arg_def_t resize_denominator =
303 ARG_DEF(NULL, "resize-denominator", 1, "Frame resize denominator");
304static const arg_def_t resize_kf_denominator = ARG_DEF(
305 NULL, "resize-kf-denominator", 1, "Frame resize keyframe denominator");
Urvang Joshi94ad3702017-12-06 11:38:08 -0800306#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpsonc4e78942017-04-10 14:59:00 -0700307static const arg_def_t superres_mode =
308 ARG_DEF(NULL, "superres-mode", 1, "Frame super-resolution mode");
Urvang Joshide71d142017-10-05 12:12:15 -0700309static const arg_def_t superres_denominator = ARG_DEF(
310 NULL, "superres-denominator", 1, "Frame super-resolution denominator");
311static const arg_def_t superres_kf_denominator =
312 ARG_DEF(NULL, "superres-kf-denominator", 1,
313 "Frame super-resolution keyframe denominator");
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700314static const arg_def_t superres_qthresh = ARG_DEF(
315 NULL, "superres-qthresh", 1, "Frame super-resolution qindex threshold");
316static const arg_def_t superres_kf_qthresh =
317 ARG_DEF(NULL, "superres-kf-qthresh", 1,
318 "Frame super-resolution keyframe qindex threshold");
Urvang Joshi94ad3702017-12-06 11:38:08 -0800319#endif // CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700320static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
321 { "cbr", AOM_CBR },
322 { "cq", AOM_CQ },
323 { "q", AOM_Q },
324 { NULL, 0 } };
clang-format6c4d83e2016-08-08 19:03:30 -0700325static const arg_def_t end_usage =
326 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
327static const arg_def_t target_bitrate =
328 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
329static const arg_def_t min_quantizer =
330 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
331static const arg_def_t max_quantizer =
332 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
333static const arg_def_t undershoot_pct =
334 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
335static const arg_def_t overshoot_pct =
336 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
337static const arg_def_t buf_sz =
338 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
339static const arg_def_t buf_initial_sz =
340 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
341static const arg_def_t buf_optimal_sz =
342 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700343static const arg_def_t *rc_args[] = { &dropframe_thresh,
344 &resize_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700345 &resize_denominator,
346 &resize_kf_denominator,
Urvang Joshi94ad3702017-12-06 11:38:08 -0800347#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700348 &superres_mode,
Urvang Joshide71d142017-10-05 12:12:15 -0700349 &superres_denominator,
350 &superres_kf_denominator,
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700351 &superres_qthresh,
352 &superres_kf_qthresh,
Urvang Joshi94ad3702017-12-06 11:38:08 -0800353#endif // CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpson7a3f4b32017-06-23 10:32:37 -0700354 &end_usage,
355 &target_bitrate,
356 &min_quantizer,
357 &max_quantizer,
358 &undershoot_pct,
359 &overshoot_pct,
360 &buf_sz,
361 &buf_initial_sz,
362 &buf_optimal_sz,
363 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400364
clang-format6c4d83e2016-08-08 19:03:30 -0700365static const arg_def_t bias_pct =
366 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
367static const arg_def_t minsection_pct =
368 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
369static const arg_def_t maxsection_pct =
370 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
371static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
372 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400373
clang-format6c4d83e2016-08-08 19:03:30 -0700374static const arg_def_t kf_min_dist =
375 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
376static const arg_def_t kf_max_dist =
377 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
378static const arg_def_t kf_disabled =
379 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
380static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
381 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400382
clang-format6c4d83e2016-08-08 19:03:30 -0700383static const arg_def_t noise_sens =
384 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
385static const arg_def_t sharpness =
386 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
387static const arg_def_t static_thresh =
388 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
389static const arg_def_t auto_altref =
390 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
391static const arg_def_t arnr_maxframes =
392 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
393static const arg_def_t arnr_strength =
394 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500395static const struct arg_enum_list tuning_enum[] = {
Yushin Chod808bfc2017-08-10 15:54:36 -0700396 { "psnr", AOM_TUNE_PSNR },
397 { "ssim", AOM_TUNE_SSIM },
398#ifdef CONFIG_DIST_8X8
399 { "cdef-dist", AOM_TUNE_CDEF_DIST },
400 { "daala-dist", AOM_TUNE_DAALA_DIST },
401#endif
402 { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500403};
Yushin Chod808bfc2017-08-10 15:54:36 -0700404static const arg_def_t tune_metric =
405 ARG_DEF_ENUM(NULL, "tune", 1, "Distortion metric tuned with", tuning_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700406static const arg_def_t cq_level =
407 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
408static const arg_def_t max_intra_rate_pct =
409 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800410
Yaowu Xuf883b422016-08-30 14:01:10 -0700411#if CONFIG_AV1_ENCODER
412static const arg_def_t cpu_used_av1 =
Yaowu Xu39737592017-04-21 16:39:09 -0700413 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (0..8)");
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800414static const arg_def_t dev_sf_av1 =
415 ARG_DEF(NULL, "dev-sf", 1, "Dev Speed (0..255)");
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700416#if CONFIG_EXT_TILE
417static const arg_def_t single_tile_decoding =
418 ARG_DEF(NULL, "single-tile-decoding", 1,
419 "Single tile decoding (0: off (default), 1: on)");
420#endif // CONFIG_EXT_TILE
clang-format6c4d83e2016-08-08 19:03:30 -0700421static const arg_def_t tile_cols =
422 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
423static const arg_def_t tile_rows =
424 ARG_DEF(NULL, "tile-rows", 1,
425 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200426#if CONFIG_MAX_TILE
427static const arg_def_t tile_width =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200428 ARG_DEF(NULL, "tile-width", 1, "Tile widths (comma separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200429static const arg_def_t tile_height =
Dominic Symes26ad0b22017-10-01 16:35:13 +0200430 ARG_DEF(NULL, "tile-height", 1, "Tile heights (command separated)");
Dominic Symesf58f1112017-09-25 12:47:40 +0200431#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800432#if CONFIG_DEPENDENT_HORZTILES
433static const arg_def_t tile_dependent_rows =
434 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
435#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800436#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800437static const arg_def_t tile_loopfilter = ARG_DEF(
438 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800439#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700440static const arg_def_t lossless =
441 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100442static const arg_def_t enable_cdef =
443 ARG_DEF(NULL, "enable-cdef", 1,
444 "Enable the constrained directional enhancement filter (0: false, "
445 "1: true (default))");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700446#if CONFIG_AOM_QM
447static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800448 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700449 "Enable quantisation matrices (0: false (default), 1: true)");
450static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800451 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700452static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800453 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700454#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700455#if CONFIG_DIST_8X8
456static const arg_def_t enable_dist_8x8 =
457 ARG_DEF(NULL, "enable-dist-8x8", 1,
458 "Enable dist-8x8 (0: false (default), 1: true)");
459#endif // CONFIG_DIST_8X8
Yaowu Xu859a5272016-11-10 15:32:21 -0800460static const arg_def_t num_tg = ARG_DEF(
461 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000462static const arg_def_t mtu_size =
463 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800464 "MTU size for a tile group, default is 0 (no MTU targeting), "
465 "overrides maximum number of tile groups");
Fangwen Fu8d164de2016-12-14 13:40:54 -0800466#if CONFIG_TEMPMV_SIGNALING
467static const arg_def_t disable_tempmv = ARG_DEF(
468 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
469#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700470static const arg_def_t frame_parallel_decoding =
471 ARG_DEF(NULL, "frame-parallel", 1,
472 "Enable frame parallel decodability features "
473 "(0: false (default), 1: true)");
Thomas Davies3ab20b42017-09-19 10:30:53 +0100474#if !CONFIG_EXT_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100475static const arg_def_t aq_mode = ARG_DEF(
476 NULL, "aq-mode", 1,
477 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100478 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200479#else
Thomase28d92b2016-09-20 12:07:11 +0100480static const arg_def_t aq_mode = ARG_DEF(
481 NULL, "aq-mode", 1,
482 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100483 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200484#endif
Fangwen Fu6160df22017-04-24 09:45:51 -0700485#if CONFIG_EXT_DELTA_Q
486static const arg_def_t deltaq_mode = ARG_DEF(
487 NULL, "deltaq-mode", 1,
488 "Delta qindex mode (0: off (default), 1: deltaq 2: deltaq + deltalf)");
489#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700490static const arg_def_t frame_periodic_boost =
491 ARG_DEF(NULL, "frame-boost", 1,
492 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700493static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
494 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700495static const arg_def_t max_inter_rate_pct =
496 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700497static const arg_def_t min_gf_interval = ARG_DEF(
498 NULL, "min-gf-interval", 1,
499 "min gf/arf frame interval (default 0, indicating in-built behavior)");
500static const arg_def_t max_gf_interval = ARG_DEF(
501 NULL, "max-gf-interval", 1,
502 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800503
Yaowu Xufc996362015-02-10 15:03:05 -0800504static const struct arg_enum_list color_space_enum[] = {
Imdad Sardharwalla317002f2017-12-05 16:24:56 +0000505 { "unknown", AOM_CS_UNKNOWN },
506 { "bt601", AOM_CS_BT_601 },
507 { "bt709", AOM_CS_BT_709 },
508 { "smpte170", AOM_CS_SMPTE_170 },
509 { "smpte240", AOM_CS_SMPTE_240 },
510 { "bt2020ncl", AOM_CS_BT_2020_NCL },
511 { "bt2020cl", AOM_CS_BT_2020_CL },
512 { "sRGB", AOM_CS_SRGB },
513 { "ICtCp", AOM_CS_ICTCP },
514 { "monochrome", AOM_CS_MONOCHROME },
515 { NULL, 0 }
Yaowu Xufc996362015-02-10 15:03:05 -0800516};
517
clang-format6c4d83e2016-08-08 19:03:30 -0700518static const arg_def_t input_color_space =
clang-format4eafefe2017-09-04 12:51:20 -0700519 ARG_DEF_ENUM(NULL, "color-space", 1,
520 "The color space of input content:", color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800521
anorkin76fb1262017-03-22 15:12:12 -0700522static const struct arg_enum_list transfer_function_enum[] = {
523 { "unknown", AOM_TF_UNKNOWN },
524 { "bt709", AOM_TF_BT_709 },
525 { "pq", AOM_TF_PQ },
526 { "hlg", AOM_TF_HLG },
527 { NULL, 0 }
528};
529
530static const arg_def_t input_transfer_function = ARG_DEF_ENUM(
clang-format4eafefe2017-09-04 12:51:20 -0700531 NULL, "transfer-function", 1,
532 "The transfer function of input content:", transfer_function_enum);
anorkin76fb1262017-03-22 15:12:12 -0700533
534static const struct arg_enum_list chroma_sample_position_enum[] = {
535 { "unknown", AOM_CSP_UNKNOWN },
536 { "vertical", AOM_CSP_VERTICAL },
537 { "colocated", AOM_CSP_COLOCATED },
538 { NULL, 0 }
539};
540
541static const arg_def_t input_chroma_sample_position =
542 ARG_DEF_ENUM(NULL, "chroma-sample-position", 1,
543 "The chroma sample position when chroma 4:2:0 is signaled:",
544 chroma_sample_position_enum);
anorkin76fb1262017-03-22 15:12:12 -0700545
Alex Converse219f6452014-08-06 11:01:18 -0700546static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700547 { "default", AOM_CONTENT_DEFAULT },
548 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700549 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700550};
551
552static const arg_def_t tune_content = ARG_DEF_ENUM(
553 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700554#endif
Alex Converse219f6452014-08-06 11:01:18 -0700555
Yaowu Xuf883b422016-08-30 14:01:10 -0700556#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000557#if CONFIG_EXT_PARTITION
558static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700559 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
560 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
561 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700562 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000563};
564static const arg_def_t superblock_size = ARG_DEF_ENUM(
565 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
566#endif // CONFIG_EXT_PARTITION
567
Yaowu Xuf883b422016-08-30 14:01:10 -0700568static const arg_def_t *av1_args[] = { &cpu_used_av1,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800569 &dev_sf_av1,
Yaowu Xuf883b422016-08-30 14:01:10 -0700570 &auto_altref,
571 &sharpness,
572 &static_thresh,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700573#if CONFIG_EXT_TILE
574 &single_tile_decoding,
575#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700576 &tile_cols,
577 &tile_rows,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800578#if CONFIG_DEPENDENT_HORZTILES
579 &tile_dependent_rows,
580#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800581#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800582 &tile_loopfilter,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800583#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700584 &arnr_maxframes,
585 &arnr_strength,
Yushin Chod808bfc2017-08-10 15:54:36 -0700586 &tune_metric,
Yaowu Xuf883b422016-08-30 14:01:10 -0700587 &cq_level,
588 &max_intra_rate_pct,
589 &max_inter_rate_pct,
590 &gf_cbr_boost_pct,
591 &lossless,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100592 &enable_cdef,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800593#if CONFIG_AOM_QM
594 &enable_qm,
595 &qm_min,
596 &qm_max,
597#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700598#if CONFIG_DIST_8X8
599 &enable_dist_8x8,
600#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700601 &frame_parallel_decoding,
602 &aq_mode,
Fangwen Fu6160df22017-04-24 09:45:51 -0700603#if CONFIG_EXT_DELTA_Q
604 &deltaq_mode,
605#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700606 &frame_periodic_boost,
607 &noise_sens,
608 &tune_content,
609 &input_color_space,
anorkin76fb1262017-03-22 15:12:12 -0700610 &input_transfer_function,
611 &input_chroma_sample_position,
Yaowu Xuf883b422016-08-30 14:01:10 -0700612 &min_gf_interval,
613 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000614#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700615 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000616#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000617 &num_tg,
618 &mtu_size,
Fangwen Fu8d164de2016-12-14 13:40:54 -0800619#if CONFIG_TEMPMV_SIGNALING
620 &disable_tempmv,
621#endif
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200622#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700623 &bitdeptharg,
624 &inbitdeptharg,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200625#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700626 NULL };
627static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
Jingning Hanb49c6ae2017-11-27 18:14:05 -0800628 AOME_SET_DEVSF,
Yaowu Xuf883b422016-08-30 14:01:10 -0700629 AOME_SET_ENABLEAUTOALTREF,
630 AOME_SET_SHARPNESS,
631 AOME_SET_STATIC_THRESHOLD,
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700632#if CONFIG_EXT_TILE
633 AV1E_SET_SINGLE_TILE_DECODING,
634#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700635 AV1E_SET_TILE_COLUMNS,
636 AV1E_SET_TILE_ROWS,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800637#if CONFIG_DEPENDENT_HORZTILES
638 AV1E_SET_TILE_DEPENDENT_ROWS,
639#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800640#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800641 AV1E_SET_TILE_LOOPFILTER,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800642#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700643 AOME_SET_ARNR_MAXFRAMES,
644 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700645 AOME_SET_TUNING,
646 AOME_SET_CQ_LEVEL,
647 AOME_SET_MAX_INTRA_BITRATE_PCT,
648 AV1E_SET_MAX_INTER_BITRATE_PCT,
649 AV1E_SET_GF_CBR_BOOST_PCT,
650 AV1E_SET_LOSSLESS,
Steinar Midtskogene44c6222017-11-09 13:22:09 +0100651 AV1E_SET_ENABLE_CDEF,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800652#if CONFIG_AOM_QM
653 AV1E_SET_ENABLE_QM,
654 AV1E_SET_QM_MIN,
655 AV1E_SET_QM_MAX,
656#endif
Yushin Chod808bfc2017-08-10 15:54:36 -0700657#if CONFIG_DIST_8X8
658 AV1E_SET_ENABLE_DIST_8X8,
659#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700660 AV1E_SET_FRAME_PARALLEL_DECODING,
661 AV1E_SET_AQ_MODE,
Fangwen Fu6160df22017-04-24 09:45:51 -0700662#if CONFIG_EXT_DELTA_Q
663 AV1E_SET_DELTAQ_MODE,
664#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700665 AV1E_SET_FRAME_PERIODIC_BOOST,
666 AV1E_SET_NOISE_SENSITIVITY,
667 AV1E_SET_TUNE_CONTENT,
668 AV1E_SET_COLOR_SPACE,
anorkin76fb1262017-03-22 15:12:12 -0700669 AV1E_SET_TRANSFER_FUNCTION,
670 AV1E_SET_CHROMA_SAMPLE_POSITION,
Yaowu Xuf883b422016-08-30 14:01:10 -0700671 AV1E_SET_MIN_GF_INTERVAL,
672 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000673#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700674 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000675#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000676 AV1E_SET_NUM_TG,
677 AV1E_SET_MTU,
Fangwen Fu8d164de2016-12-14 13:40:54 -0800678#if CONFIG_TEMPMV_SIGNALING
679 AV1E_SET_DISABLE_TEMPMV,
680#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700681 0 };
hui su82331e02015-08-16 18:21:56 -0700682#endif
683
John Koleszar0ea50ce2010-05-18 11:58:33 -0400684static const arg_def_t *no_args[] = { NULL };
685
James Zernfd8c78c2017-11-27 16:37:33 -0800686void show_help(FILE *fout, int shorthelp) {
687 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename \n",
John Koleszarc6b90392012-07-13 15:21:29 -0700688 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400689
James Zernfd8c78c2017-11-27 16:37:33 -0800690 if (shorthelp) {
691 fprintf(fout, "Use --help to see the full list of options.\n");
692 return;
693 }
694
695 fprintf(fout, "\nOptions:\n");
696 arg_show_usage(fout, main_args);
697 fprintf(fout, "\nEncoder Global Options:\n");
698 arg_show_usage(fout, global_args);
699 fprintf(fout, "\nRate Control Options:\n");
700 arg_show_usage(fout, rc_args);
701 fprintf(fout, "\nTwopass Rate Control Options:\n");
702 arg_show_usage(fout, rc_twopass_args);
703 fprintf(fout, "\nKeyframe Placement Options:\n");
704 arg_show_usage(fout, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700705#if CONFIG_AV1_ENCODER
James Zernfd8c78c2017-11-27 16:37:33 -0800706 fprintf(fout, "\nAV1 Specific Options:\n");
707 arg_show_usage(fout, av1_args);
hui su82331e02015-08-16 18:21:56 -0700708#endif
James Zernfd8c78c2017-11-27 16:37:33 -0800709 fprintf(fout,
clang-format6c4d83e2016-08-08 19:03:30 -0700710 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700711 " The desired precision of timestamps in the output, expressed\n"
712 " in fractional seconds. Default is 1/1000.\n");
James Zernfd8c78c2017-11-27 16:37:33 -0800713 fprintf(fout, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400714
James Zernfd8c78c2017-11-27 16:37:33 -0800715 const int num_encoder = get_aom_encoder_count();
716 for (int i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700717 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700718 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
James Zernfd8c78c2017-11-27 16:37:33 -0800719 fprintf(fout, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700720 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800721 }
James Zernfd8c78c2017-11-27 16:37:33 -0800722 fprintf(fout, "\n ");
723 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
724}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400725
James Zernfd8c78c2017-11-27 16:37:33 -0800726void usage_exit(void) {
727 show_help(stderr, 1);
John Koleszarc6b90392012-07-13 15:21:29 -0700728 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400729}
730
Yaowu Xuf883b422016-08-30 14:01:10 -0700731#if CONFIG_AV1_ENCODER
732#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800733#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800734
James Zerndba73762014-05-10 17:44:12 -0700735#if !CONFIG_WEBM_IO
736typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700737struct WebmOutputContext {
738 int debug;
739};
James Zerndba73762014-05-10 17:44:12 -0700740#endif
741
John Koleszar9e50ed72012-02-15 12:39:38 -0800742/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800743struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700744 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700745 const char *out_fn;
746 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700747#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700748 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700749#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700750 stereo_format_t stereo_fmt;
751 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
752 int arg_ctrl_cnt;
753 int write_webm;
Cyril Concolato6c788832017-10-30 16:30:35 -0700754#if CONFIG_OBU_NO_IVF
755 int write_ivf;
756#endif
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700757 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700758 int use_16bit_internal;
John Koleszar9e50ed72012-02-15 12:39:38 -0800759};
760
John Koleszar6ad3b742012-11-06 12:02:42 -0800761struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700762 int index;
763 struct stream_state *next;
764 struct stream_config config;
765 FILE *file;
766 struct rate_hist *rate_hist;
767 struct WebmOutputContext webm_ctx;
768 uint64_t psnr_sse_total;
769 uint64_t psnr_samples_total;
770 double psnr_totals[4];
771 int psnr_count;
772 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700773 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700774 unsigned int frames_out;
775 uint64_t cx_time;
776 size_t nbytes;
777 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700778#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700779 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700780#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700781 struct aom_image *img;
782 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700783 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800784};
785
clang-format6c4d83e2016-08-08 19:03:30 -0700786static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700787 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800788 if (rat->den < 0) {
789 rat->num *= -1;
790 rat->den *= -1;
791 }
John Koleszar1b27e932012-04-25 17:08:56 -0700792
clang-format6c4d83e2016-08-08 19:03:30 -0700793 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700794
clang-format6c4d83e2016-08-08 19:03:30 -0700795 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700796}
797
Yaowu Xuf883b422016-08-30 14:01:10 -0700798static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700799 char **argi, **argj;
800 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700801 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700802
clang-format6c4d83e2016-08-08 19:03:30 -0700803 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800804
John Koleszar6ad3b742012-11-06 12:02:42 -0800805 /* Initialize default parameters */
806 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700807 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700808 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700809 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700810 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700811 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400812
John Koleszarc6b90392012-07-13 15:21:29 -0700813 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
814 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400815
James Zernfd8c78c2017-11-27 16:37:33 -0800816 if (arg_match(&arg, &help, argi)) {
817 show_help(stdout, 0);
818 exit(EXIT_SUCCESS);
819 } else if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700820 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800821 if (!global->codec)
822 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700823 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800824 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400825
John Koleszar6ad3b742012-11-06 12:02:42 -0800826 if (global->passes < 1 || global->passes > 2)
827 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700828 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800829 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400830
John Koleszar6ad3b742012-11-06 12:02:42 -0800831 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700832 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800833 } else if (arg_match(&arg, &usage, argi))
834 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700835 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800836 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700837 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700838 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar6ad3b742012-11-06 12:02:42 -0800839 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700840 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800841 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700842 global->color_type = I420;
843 else if (arg_match(&arg, &use_i422, argi))
844 global->color_type = I422;
845 else if (arg_match(&arg, &use_i444, argi))
846 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700847 else if (arg_match(&arg, &use_i440, argi))
848 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800849 else if (arg_match(&arg, &quietarg, argi))
850 global->quiet = 1;
851 else if (arg_match(&arg, &verbosearg, argi))
852 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700853 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800854 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700855 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800856 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700857 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800858 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700859 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800860 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700861 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800862 global->framerate = arg_parse_rational(&arg);
863 validate_positive_rational(arg.name, &global->framerate);
864 global->have_framerate = 1;
865 } else if (arg_match(&arg, &out_part, argi))
866 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700867 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800868 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700869 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800870 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700871 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800872 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800873 else if (arg_match(&arg, &disable_warnings, argi))
874 global->disable_warnings = 1;
875 else if (arg_match(&arg, &disable_warning_prompt, argi))
876 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800877 else
John Koleszarc6b90392012-07-13 15:21:29 -0700878 argj++;
879 }
880
John Koleszar6ad3b742012-11-06 12:02:42 -0800881 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700882 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800883 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700884 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
885 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800886 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800887 }
John Koleszarc6b90392012-07-13 15:21:29 -0700888 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800889 /* Validate global config */
890 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700891#if CONFIG_AV1_ENCODER
892 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800893 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700894 if (global->codec != NULL && global->codec->name != NULL)
Thomas Daede80826142017-03-20 15:44:24 -0700895 global->passes = (strcmp(global->codec->name, "av1") == 0) ? 2 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800896#else
897 global->passes = 1;
898#endif
899 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800900}
901
Yaowu Xuf883b422016-08-30 14:01:10 -0700902static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800903 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700904 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
905 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -0800906
clang-format6c4d83e2016-08-08 19:03:30 -0700907 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -0800908
John Koleszar25b6e9f2013-02-12 21:17:56 -0800909 if (!fseeko(input->file, 0, SEEK_END)) {
910 /* Input file is seekable. Figure out how long it is, so we can get
911 * progress info.
912 */
913 input->length = ftello(input->file);
914 rewind(input->file);
915 }
916
Frank Galligan09acd262015-06-01 10:20:58 -0700917 /* Default to 1:1 pixel aspect ratio. */
918 input->pixel_aspect_ratio.numerator = 1;
919 input->pixel_aspect_ratio.denominator = 1;
920
John Koleszar6ad3b742012-11-06 12:02:42 -0800921 /* For RAW input sources, these bytes will applied on the first frame
922 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -0700923 */
John Koleszar6ad3b742012-11-06 12:02:42 -0800924 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
925 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400926
clang-format6c4d83e2016-08-08 19:03:30 -0700927 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -0700928 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
929 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800930 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800931 input->width = input->y4m.pic_w;
932 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -0700933 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
934 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800935 input->framerate.numerator = input->y4m.fps_n;
936 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -0700937 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -0700938 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -0800939 } else
940 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -0800941 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -0800942 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -0800943 } else {
944 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -0700945 }
John Koleszar6ad3b742012-11-06 12:02:42 -0800946}
947
Yaowu Xuf883b422016-08-30 14:01:10 -0700948static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800949 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -0700950 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -0800951}
952
Yaowu Xuf883b422016-08-30 14:01:10 -0700953static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -0800954 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800955 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -0800956
John Koleszar6ad3b742012-11-06 12:02:42 -0800957 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700958 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800959 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700960 }
961
John Koleszar6ad3b742012-11-06 12:02:42 -0800962 if (prev) {
963 memcpy(stream, prev, sizeof(*stream));
964 stream->index++;
965 prev->next = stream;
966 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700967 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -0800968
John Koleszar6ad3b742012-11-06 12:02:42 -0800969 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -0700970 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -0700971 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -0700972 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -0800973
John Koleszar6ad3b742012-11-06 12:02:42 -0800974 /* Change the default timebase to a high enough value so that the
975 * encoder will always create strictly increasing timestamps.
976 */
977 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -0800978
John Koleszar6ad3b742012-11-06 12:02:42 -0800979 /* Never use the library's default resolution, require it be parsed
980 * from the file or set on the command line.
981 */
982 stream->config.cfg.g_w = 0;
983 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -0800984
John Koleszar6ad3b742012-11-06 12:02:42 -0800985 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -0800986 stream->config.write_webm = 1;
Cyril Concolato6c788832017-10-30 16:30:35 -0700987#if CONFIG_OBU_NO_IVF
988 stream->config.write_ivf = 0;
989#endif
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700990#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -0700991 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700992 stream->webm_ctx.last_pts_ns = -1;
993 stream->webm_ctx.writer = NULL;
994 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -0700995#endif
Johann87c40b32012-03-01 16:12:53 -0800996
John Koleszar6ad3b742012-11-06 12:02:42 -0800997 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700998 stream->webm_ctx.debug = global->debug;
John Koleszar6ad3b742012-11-06 12:02:42 -0800999 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001000
John Koleszar6ad3b742012-11-06 12:02:42 -08001001 /* Output files must be specified for each stream */
1002 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001003
John Koleszar6ad3b742012-11-06 12:02:42 -08001004 stream->next = NULL;
1005 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001006}
1007
Yaowu Xuf883b422016-08-30 14:01:10 -07001008static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001009 struct stream_state *stream, char **argv) {
1010 char **argi, **argj;
1011 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001012 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001013 static const int *ctrl_args_map = NULL;
1014 struct stream_config *config = &stream->config;
1015 int eos_mark_found = 0;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001016 int webm_forced = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001017
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001018 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001019 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001020#if CONFIG_AV1_ENCODER
1021 } else if (strcmp(global->codec->name, "av1") == 0) {
1022 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1023 // Consider to expand this set for AV1 encoder control.
1024 ctrl_args = av1_args;
1025 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001026#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001027 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001028
John Koleszarc6b90392012-07-13 15:21:29 -07001029 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001030 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001031
John Koleszar6ad3b742012-11-06 12:02:42 -08001032 /* Once we've found an end-of-stream marker (--) we want to continue
1033 * shifting arguments but not consuming them.
1034 */
1035 if (eos_mark_found) {
1036 argj++;
1037 continue;
1038 } else if (!strcmp(*argj, "--")) {
1039 eos_mark_found = 1;
1040 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001041 }
1042
Adrian Granged2401802015-02-13 14:51:32 -08001043 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001044 config->out_fn = arg.val;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001045 if (!webm_forced) {
1046 const size_t out_fn_len = strlen(config->out_fn);
1047 if (out_fn_len >= 4 &&
1048 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
1049 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001050#if CONFIG_OBU_NO_IVF
1051 config->write_ivf = 1;
1052#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001053 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001054#if CONFIG_OBU_NO_IVF
1055 else if (out_fn_len >= 4 &&
1056 !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
1057 config->write_webm = 0;
1058 config->write_ivf = 0;
1059 }
1060#endif
Tristan Matthewsed858d62017-07-24 15:33:44 -04001061 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001062 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001063 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001064#if CONFIG_FP_MB_STATS
1065 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1066 config->fpmb_stats_fn = arg.val;
1067#endif
Johannb50e5182015-01-30 15:05:14 -08001068 } else if (arg_match(&arg, &use_webm, argi)) {
1069#if CONFIG_WEBM_IO
1070 config->write_webm = 1;
Tristan Matthewsed858d62017-07-24 15:33:44 -04001071 webm_forced = 1;
Johannb50e5182015-01-30 15:05:14 -08001072#else
1073 die("Error: --webm specified but webm is disabled.");
1074#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001075 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001076 config->write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07001077#if CONFIG_OBU_NO_IVF
1078 config->write_ivf = 1;
1079#endif
1080#if CONFIG_OBU_NO_IVF
1081 } else if (arg_match(&arg, &use_obu, argi)) {
1082 config->write_webm = 0;
1083 config->write_ivf = 0;
1084#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001085 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001086 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001087 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001088 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001089 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001090 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001091 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001092 config->cfg.g_h = arg_parse_uint(&arg);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001093#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001094 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1095 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1096 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1097 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1098#endif
James Zerndba73762014-05-10 17:44:12 -07001099#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001100 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001101 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001102#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001103 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001104 config->cfg.g_timebase = arg_parse_rational(&arg);
1105 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001106 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001107 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001108 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001109 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001110#if CONFIG_EXT_TILE
1111 } else if (arg_match(&arg, &large_scale_tile, argi)) {
1112 config->cfg.large_scale_tile = arg_parse_uint(&arg);
1113#endif // CONFIG_EXT_TILE
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001114 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001115 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001116 } else if (arg_match(&arg, &resize_mode, argi)) {
1117 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001118 } else if (arg_match(&arg, &resize_denominator, argi)) {
1119 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1120 } else if (arg_match(&arg, &resize_kf_denominator, argi)) {
1121 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001122#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001123 } else if (arg_match(&arg, &superres_mode, argi)) {
1124 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
Urvang Joshide71d142017-10-05 12:12:15 -07001125 } else if (arg_match(&arg, &superres_denominator, argi)) {
1126 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1127 } else if (arg_match(&arg, &superres_kf_denominator, argi)) {
1128 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001129 } else if (arg_match(&arg, &superres_qthresh, argi)) {
1130 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1131 } else if (arg_match(&arg, &superres_kf_qthresh, argi)) {
1132 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001133#endif // CONFIG_HORZONLY_FRAME_SUPERRES
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001134 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001135 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001136 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001137 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001138 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001139 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001140 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001141 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001142 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001143 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001144 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001145 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001146 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001147 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001148 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001149 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001150 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001151 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001152 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001153 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001154 if (global->passes < 2)
1155 warn("option %s ignored in one-pass mode.\n", arg.name);
1156 } else if (arg_match(&arg, &minsection_pct, argi)) {
1157 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1158
1159 if (global->passes < 2)
1160 warn("option %s ignored in one-pass mode.\n", arg.name);
1161 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1162 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1163
1164 if (global->passes < 2)
1165 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001166 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001167 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001168 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001169 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001170 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001171 config->cfg.kf_mode = AOM_KF_DISABLED;
Dominic Symes26ad0b22017-10-01 16:35:13 +02001172#if CONFIG_MAX_TILE
1173 } else if (arg_match(&arg, &tile_width, argi)) {
1174 config->cfg.tile_width_count =
1175 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1176 } else if (arg_match(&arg, &tile_height, argi)) {
1177 config->cfg.tile_height_count =
1178 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1179#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001180 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001181 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001182 for (i = 0; ctrl_args[i]; i++) {
1183 if (arg_match(&arg, ctrl_args[i], argi)) {
1184 int j;
1185 match = 1;
1186
1187 /* Point either to the next free element or the first
Cyril Concolato6c788832017-10-30 16:30:35 -07001188 * instance of this control.
1189 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001190 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001191 if (ctrl_args_map != NULL &&
1192 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001193 break;
1194
1195 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001196 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001197 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001198 config->arg_ctrls[j][0] = ctrl_args_map[i];
1199 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001200 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001201 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001202 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001203 }
clang-format6c4d83e2016-08-08 19:03:30 -07001204 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001205 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001206 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001207#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan14af5b92017-05-12 14:27:30 +02001208 config->use_16bit_internal =
Sebastien Alaiwan0b2e4032017-06-05 12:47:04 +02001209 config->cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001210#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001211 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001212}
1213
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001214#define FOREACH_STREAM(iterator, list) \
1215 for (struct stream_state *iterator = list; iterator; \
1216 iterator = iterator->next)
John Koleszar9e50ed72012-02-15 12:39:38 -08001217
Alex Conversed66bd222014-02-21 10:52:09 -08001218static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001219 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001220 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001221 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001222
John Koleszar6ad3b742012-11-06 12:02:42 -08001223 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001224 fatal(
1225 "Stream %d: Specify stream dimensions with --width (-w) "
1226 " and --height (-h)",
1227 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001228
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001229 // Check that the codec bit depth is greater than the input bit depth.
1230 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001231 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001232 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1233 stream->index, (int)stream->config.cfg.g_bit_depth,
1234 stream->config.cfg.g_input_bit_depth);
1235 }
1236
John Koleszar6ad3b742012-11-06 12:02:42 -08001237 for (streami = stream; streami; streami = streami->next) {
1238 /* All streams require output files */
1239 if (!streami->config.out_fn)
1240 fatal("Stream %d: Output file is required (specify with -o)",
1241 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001242
John Koleszar6ad3b742012-11-06 12:02:42 -08001243 /* Check for two streams outputting to the same file */
1244 if (streami != stream) {
1245 const char *a = stream->config.out_fn;
1246 const char *b = streami->config.out_fn;
1247 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1248 fatal("Stream %d: duplicate output file (from stream %d)",
1249 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001250 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001251
1252 /* Check for two streams sharing a stats file. */
1253 if (streami != stream) {
1254 const char *a = stream->config.stats_fn;
1255 const char *b = streami->config.stats_fn;
1256 if (a && b && !strcmp(a, b))
1257 fatal("Stream %d: duplicate stats file (from stream %d)",
1258 streami->index, stream->index);
1259 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001260
1261#if CONFIG_FP_MB_STATS
1262 /* Check for two streams sharing a mb stats file. */
1263 if (streami != stream) {
1264 const char *a = stream->config.fpmb_stats_fn;
1265 const char *b = streami->config.fpmb_stats_fn;
1266 if (a && b && !strcmp(a, b))
1267 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1268 streami->index, stream->index);
1269 }
1270#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001271 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001272}
1273
clang-format6c4d83e2016-08-08 19:03:30 -07001274static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001275 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001276 if (!stream->config.cfg.g_w) {
1277 if (!stream->config.cfg.g_h)
1278 stream->config.cfg.g_w = w;
1279 else
1280 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1281 }
1282 if (!stream->config.cfg.g_h) {
1283 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1284 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001285}
1286
clang-format6c4d83e2016-08-08 19:03:30 -07001287static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001288 switch (t) {
1289 case FILE_TYPE_RAW: return "RAW";
1290 case FILE_TYPE_Y4M: return "Y4M";
1291 default: return "Other";
1292 }
1293}
1294
Yaowu Xuf883b422016-08-30 14:01:10 -07001295static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001296 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001297 case AOM_IMG_FMT_I420: return "I420";
1298 case AOM_IMG_FMT_I422: return "I422";
1299 case AOM_IMG_FMT_I444: return "I444";
1300 case AOM_IMG_FMT_I440: return "I440";
1301 case AOM_IMG_FMT_YV12: return "YV12";
1302 case AOM_IMG_FMT_I42016: return "I42016";
1303 case AOM_IMG_FMT_I42216: return "I42216";
1304 case AOM_IMG_FMT_I44416: return "I44416";
1305 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001306 default: return "Other";
1307 }
1308}
Ralph Giles061a16d2012-01-05 15:05:05 -06001309
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001310static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001311 struct AvxEncoderConfig *global,
1312 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001313#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001314 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001315
John Koleszar6ad3b742012-11-06 12:02:42 -08001316 if (stream->index == 0) {
1317 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001318 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001319 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001320 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001321 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001322 }
1323 if (stream->next || stream->index)
1324 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1325 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
Sebastien Alaiwan27511922017-06-05 15:25:46 +02001326 fprintf(stderr, "Coding path: %s\n",
1327 stream->config.use_16bit_internal ? "HBD" : "LBD");
John Koleszar6ad3b742012-11-06 12:02:42 -08001328 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001329
John Koleszar6ad3b742012-11-06 12:02:42 -08001330 SHOW(g_usage);
1331 SHOW(g_threads);
1332 SHOW(g_profile);
1333 SHOW(g_w);
1334 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001335 SHOW(g_bit_depth);
1336 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001337 SHOW(g_timebase.num);
1338 SHOW(g_timebase.den);
1339 SHOW(g_error_resilient);
1340 SHOW(g_pass);
1341 SHOW(g_lag_in_frames);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001342#if CONFIG_EXT_TILE
1343 SHOW(large_scale_tile);
1344#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001345 SHOW(rc_dropframe_thresh);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07001346 SHOW(rc_resize_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001347 SHOW(rc_resize_denominator);
1348 SHOW(rc_resize_kf_denominator);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001349#if CONFIG_HORZONLY_FRAME_SUPERRES
Fergus Simpsonc4e78942017-04-10 14:59:00 -07001350 SHOW(rc_superres_mode);
Urvang Joshide71d142017-10-05 12:12:15 -07001351 SHOW(rc_superres_denominator);
1352 SHOW(rc_superres_kf_denominator);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07001353 SHOW(rc_superres_qthresh);
1354 SHOW(rc_superres_kf_qthresh);
Urvang Joshi94ad3702017-12-06 11:38:08 -08001355#endif // CONFIG_HORZONLY_FRAME_SUPERRES
John Koleszar6ad3b742012-11-06 12:02:42 -08001356 SHOW(rc_end_usage);
1357 SHOW(rc_target_bitrate);
1358 SHOW(rc_min_quantizer);
1359 SHOW(rc_max_quantizer);
1360 SHOW(rc_undershoot_pct);
1361 SHOW(rc_overshoot_pct);
1362 SHOW(rc_buf_sz);
1363 SHOW(rc_buf_initial_sz);
1364 SHOW(rc_buf_optimal_sz);
1365 SHOW(rc_2pass_vbr_bias_pct);
1366 SHOW(rc_2pass_vbr_minsection_pct);
1367 SHOW(rc_2pass_vbr_maxsection_pct);
1368 SHOW(kf_mode);
1369 SHOW(kf_min_dist);
1370 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001371}
1372
John Koleszar9e50ed72012-02-15 12:39:38 -08001373static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001374 struct AvxEncoderConfig *global,
1375 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001376 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001377 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001378
Yaowu Xuf883b422016-08-30 14:01:10 -07001379 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001380
John Koleszar6ad3b742012-11-06 12:02:42 -08001381 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001382
clang-format6c4d83e2016-08-08 19:03:30 -07001383 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001384
John Koleszar6ad3b742012-11-06 12:02:42 -08001385 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1386 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001387
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001388#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001389 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001390 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001391 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1392 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001393 }
James Zernc8e5a772016-02-11 18:53:50 -08001394#else
1395 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001396#endif
1397
Cyril Concolato6c788832017-10-30 16:30:35 -07001398 if (!stream->config.write_webm
1399#if CONFIG_OBU_NO_IVF
1400 && stream->config.write_ivf
1401#endif
1402 ) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001403 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1404 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001405}
1406
John Koleszar9e50ed72012-02-15 12:39:38 -08001407static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001408 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001409 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001410
Yaowu Xuf883b422016-08-30 14:01:10 -07001411 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001412
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001413#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001414 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001415 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001416 }
1417#endif
1418
Cyril Concolato6c788832017-10-30 16:30:35 -07001419 if (!stream->config.write_webm
1420#if CONFIG_OBU_NO_IVF
1421 && stream->config.write_ivf
1422#endif
1423 ) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001424 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001425 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001426 stream->frames_out);
1427 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001428
John Koleszar6ad3b742012-11-06 12:02:42 -08001429 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001430}
1431
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001432static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001433 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001434 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001435 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001436 fatal("Failed to open statistics store");
1437 } else {
1438 if (!stats_open_mem(&stream->stats, pass))
1439 fatal("Failed to open statistics store");
1440 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001441
Pengchong Jinf349b072014-07-14 09:13:38 -07001442#if CONFIG_FP_MB_STATS
1443 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001444 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1445 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001446 fatal("Failed to open mb statistics store");
1447 } else {
1448 if (!stats_open_mem(&stream->fpmb_stats, pass))
1449 fatal("Failed to open mb statistics store");
1450 }
1451#endif
1452
John Koleszar6ad3b742012-11-06 12:02:42 -08001453 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001454 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1455 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001456 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001457 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001458#if CONFIG_FP_MB_STATS
1459 stream->config.cfg.rc_firstpass_mb_stats_in =
1460 stats_get(&stream->fpmb_stats);
1461#endif
1462 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001463
John Koleszar6ad3b742012-11-06 12:02:42 -08001464 stream->cx_time = 0;
1465 stream->nbytes = 0;
1466 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001467}
1468
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001469static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001470 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001471 int i;
1472 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001473
Yaowu Xuf883b422016-08-30 14:01:10 -07001474 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1475 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001476#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001477 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001478#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001479
John Koleszar6ad3b742012-11-06 12:02:42 -08001480 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001481 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001482 &stream->config.cfg, flags);
1483 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001484
Yaowu Xuf883b422016-08-30 14:01:10 -07001485 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001486 * we're being clever to store the control IDs in an array. Real
1487 * applications will want to make use of the enumerations directly
1488 */
1489 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1490 int ctrl = stream->config.arg_ctrls[i][0];
1491 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001492 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001493 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001494
John Koleszar6ad3b742012-11-06 12:02:42 -08001495 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1496 }
1497
Tom Fineganba02c242017-05-16 15:01:54 -07001498#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001499 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001500 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
Imdad Sardharwalla730c8052017-11-30 17:39:15 +00001501 aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, 0 };
Yaowu Xuf883b422016-08-30 14:01:10 -07001502 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001503
Tom Fineganba02c242017-05-16 15:01:54 -07001504#if CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -07001505 if (strcmp(global->codec->name, "av1") == 0) {
1506 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001507 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1508
Yaowu Xuf883b422016-08-30 14:01:10 -07001509 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001510 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1511 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001512#endif // CONFIG_EXT_TILE
John Koleszar6ad3b742012-11-06 12:02:42 -08001513 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001514#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001515}
1516
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001517static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001518 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001519 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001520 aom_codec_pts_t frame_start, next_frame_start;
1521 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1522 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001523
clang-format6c4d83e2016-08-08 19:03:30 -07001524 frame_start =
1525 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1526 cfg->g_timebase.num / global->framerate.num;
1527 next_frame_start =
1528 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1529 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001530
clang-format6c4d83e2016-08-08 19:03:30 -07001531/* Scale if necessary */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001532#if CONFIG_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001533 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001534 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001535 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001536 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001537 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1538 exit(EXIT_FAILURE);
1539 }
1540#if CONFIG_LIBYUV
1541 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001542 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001543 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001544 }
clang-format6c4d83e2016-08-08 19:03:30 -07001545 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001546 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1547 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1548 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1549 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1550 stream->img->stride[AOM_PLANE_Y] / 2,
1551 (uint16 *)stream->img->planes[AOM_PLANE_U],
1552 stream->img->stride[AOM_PLANE_U] / 2,
1553 (uint16 *)stream->img->planes[AOM_PLANE_V],
1554 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001555 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001556 img = stream->img;
1557#else
clang-format6c4d83e2016-08-08 19:03:30 -07001558 stream->encoder.err = 1;
1559 ctx_exit_on_error(&stream->encoder,
1560 "Stream %d: Failed to encode frame.\n"
1561 "Scaling disabled in this configuration. \n"
1562 "To enable, configure with --enable-libyuv\n",
1563 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001564#endif
1565 }
1566 }
1567#endif
John Koleszar34882b92012-03-01 12:50:40 -08001568 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001569 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001570 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1571 exit(EXIT_FAILURE);
1572 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001573#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001574 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001575 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001576 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001577 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001578 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1579 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1580 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1581 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1582 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1583 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001584 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001585 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001586#else
1587 stream->encoder.err = 1;
1588 ctx_exit_on_error(&stream->encoder,
1589 "Stream %d: Failed to encode frame.\n"
1590 "Scaling disabled in this configuration. \n"
1591 "To enable, configure with --enable-libyuv\n",
1592 stream->index);
1593#endif
John Koleszar34882b92012-03-01 12:50:40 -08001594 }
1595
Yaowu Xuf883b422016-08-30 14:01:10 -07001596 aom_usec_timer_start(&timer);
1597 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001598 (unsigned long)(next_frame_start - frame_start), 0,
1599 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001600 aom_usec_timer_mark(&timer);
1601 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001602 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1603 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001604}
1605
John Koleszar6ad3b742012-11-06 12:02:42 -08001606static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001607 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001608 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001609
Yaowu Xuf883b422016-08-30 14:01:10 -07001610 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001611 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1612 stream->counts[q]++;
1613 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001614}
1615
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001616static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001617 struct AvxEncoderConfig *global, int *got_data) {
1618 const aom_codec_cx_pkt_t *pkt;
1619 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1620 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001621
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001622 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001623 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001624 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001625 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001626
John Koleszar6ad3b742012-11-06 12:02:42 -08001627 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001628 case AOM_CODEC_CX_FRAME_PKT:
1629 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001630 stream->frames_out++;
1631 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001632 if (!global->quiet)
1633 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001634
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001635 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001636#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001637 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001638 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001639 }
1640#endif
1641 if (!stream->config.write_webm) {
Cyril Concolato6c788832017-10-30 16:30:35 -07001642#if CONFIG_OBU_NO_IVF
1643 if (stream->config.write_ivf) {
1644#endif
1645 if (pkt->data.frame.partition_id <= 0) {
1646 ivf_header_pos = ftello(stream->file);
1647 fsize = pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001648
Cyril Concolato6c788832017-10-30 16:30:35 -07001649 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1650 } else {
1651 fsize += pkt->data.frame.sz;
John Koleszar6ad3b742012-11-06 12:02:42 -08001652
Cyril Concolato6c788832017-10-30 16:30:35 -07001653 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1654 const FileOffset currpos = ftello(stream->file);
1655 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1656 ivf_write_frame_size(stream->file, fsize);
1657 fseeko(stream->file, currpos, SEEK_SET);
1658 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001659 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001660#if CONFIG_OBU_NO_IVF
John Koleszar6ad3b742012-11-06 12:02:42 -08001661 }
Cyril Concolato6c788832017-10-30 16:30:35 -07001662#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001663
clang-format6c4d83e2016-08-08 19:03:30 -07001664 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1665 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001666 }
1667 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001668
John Koleszar9e50ed72012-02-15 12:39:38 -08001669 *got_data = 1;
Tom Fineganba02c242017-05-16 15:01:54 -07001670#if CONFIG_AV1_DECODER
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001671 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001672 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001673 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001674 if (stream->decoder.err) {
1675 warn_or_exit_on_error(&stream->decoder,
1676 global->test_decode == TEST_DECODE_FATAL,
1677 "Failed to decode frame %d in stream %d",
1678 stream->frames_out + 1, stream->index);
1679 stream->mismatch_seen = stream->frames_out + 1;
1680 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001681 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001682#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001683 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001684 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001685 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001686 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001687 pkt->data.twopass_stats.sz);
1688 stream->nbytes += pkt->data.raw.sz;
1689 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001690#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001691 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001692 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001693 pkt->data.firstpass_mb_stats.sz);
1694 stream->nbytes += pkt->data.raw.sz;
1695 break;
1696#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001697 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001698
1699 if (global->show_psnr) {
1700 int i;
1701
1702 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1703 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1704 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001705 if (!global->quiet)
1706 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001707 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1708 }
1709 stream->psnr_count++;
1710 }
1711
1712 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001713 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001714 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001715 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001716}
1717
clang-format6c4d83e2016-08-08 19:03:30 -07001718static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001719 int i;
1720 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001721
clang-format6c4d83e2016-08-08 19:03:30 -07001722 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001723
John Koleszar6ad3b742012-11-06 12:02:42 -08001724 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001725 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001726 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001727 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001728
John Koleszar6ad3b742012-11-06 12:02:42 -08001729 for (i = 0; i < 4; i++) {
1730 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1731 }
1732 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001733}
1734
John Koleszar25b6e9f2013-02-12 21:17:56 -08001735static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001736 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001737}
1738
clang-format6c4d83e2016-08-08 19:03:30 -07001739static void test_decode(struct stream_state *stream,
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00001740 enum TestDecodeFatality fatal, int is_mono) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001741 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001742
clang-format6c4d83e2016-08-08 19:03:30 -07001743 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001744
John Koleszarb3c350a2013-03-13 12:15:43 -07001745 /* Get the internal reference frame */
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001746 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1747 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001748
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001749#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001750 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1751 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1752 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1753 aom_image_t enc_hbd_img;
1754 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1755 enc_img.d_w, enc_img.d_h, 16);
1756 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1757 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001758 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001759 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1760 aom_image_t dec_hbd_img;
1761 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1762 dec_img.d_w, dec_img.d_h, 16);
1763 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1764 dec_img = dec_hbd_img;
1765 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001766 }
Sebastien Alaiwan34c9d8b2017-08-07 09:56:47 +02001767#endif
1768
John Koleszar6ad3b742012-11-06 12:02:42 -08001769 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001770 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001771
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00001772 if (!aom_compare_img(&enc_img, &dec_img, is_mono ? 1 : 3)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001773 int y[4], u[4], v[4];
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001774#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001775 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001776 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001777 } else {
Urvang Joshi09c293e2017-04-20 17:56:27 -07001778 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001779 }
1780#else
Urvang Joshi09c293e2017-04-20 17:56:27 -07001781 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001782#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001783 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001784 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001785 "Stream %d: Encode/decode mismatch on frame %d at"
1786 " Y[%d, %d] {%d/%d},"
1787 " U[%d, %d] {%d/%d},"
1788 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001789 stream->index, stream->frames_out, y[0], y[1], y[2],
1790 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 -08001791 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001792 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001793
Yaowu Xuf883b422016-08-30 14:01:10 -07001794 aom_img_free(&enc_img);
1795 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001796}
John Koleszarefd54f82012-02-13 16:52:18 -08001797
John Koleszar25b6e9f2013-02-12 21:17:56 -08001798static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001799 int64_t hours;
1800 int64_t mins;
1801 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001802
1803 if (etl >= 0) {
1804 hours = etl / 3600;
1805 etl -= hours * 3600;
1806 mins = etl / 60;
1807 etl -= mins * 60;
1808 secs = etl;
1809
clang-format6c4d83e2016-08-08 19:03:30 -07001810 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1811 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001812 } else {
1813 fprintf(stderr, "[%3s unknown] ", label);
1814 }
1815}
1816
Tom Finegan44dd3272013-11-20 17:18:28 -08001817int main(int argc, const char **argv_) {
1818 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001819 aom_image_t raw;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001820#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001821 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001822 int allocated_raw_shift = 0;
1823 int use_16bit_internal = 0;
1824 int input_shift = 0;
1825#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001826 int frame_avail, got_data;
1827
Yaowu Xuf883b422016-08-30 14:01:10 -07001828 struct AvxInputContext input;
1829 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001830 struct stream_state *streams = NULL;
1831 char **argv, **argi;
1832 uint64_t cx_time = 0;
1833 int stream_cnt = 0;
1834 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001835 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001836
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001837 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001838 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001839
John Koleszar6ad3b742012-11-06 12:02:42 -08001840 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001841 input.framerate.numerator = 30;
1842 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001843 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001844 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001845
1846 /* First parse the global configuration values, because we want to apply
1847 * other parameters on top of the default configuration provided by the
1848 * codec.
1849 */
1850 argv = argv_dup(argc - 1, argv_ + 1);
1851 parse_global_config(&global, argv);
1852
James Zernfd8c78c2017-11-27 16:37:33 -08001853 if (argc < 3) usage_exit();
1854
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001855 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001856 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1857 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1858 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1859 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1860 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001861 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001862
John Koleszar6ad3b742012-11-06 12:02:42 -08001863 {
1864 /* Now parse each stream's parameters. Using a local scope here
1865 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1866 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001867 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001868 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001869
John Koleszar6ad3b742012-11-06 12:02:42 -08001870 do {
1871 stream = new_stream(&global, stream);
1872 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001873 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001874 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001875 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001876
John Koleszarc6b90392012-07-13 15:21:29 -07001877 /* Check for unrecognized options */
1878 for (argi = argv; *argi; argi++)
1879 if (argi[0][0] == '-' && argi[0][1])
1880 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001881
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001882 FOREACH_STREAM(stream, streams) {
1883 check_encoder_config(global.disable_warning_prompt, &global,
1884 &stream->config.cfg);
1885 }
Tom Finegan249366b2013-11-25 12:05:19 -08001886
John Koleszarc6b90392012-07-13 15:21:29 -07001887 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001888 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001889
James Zernfd8c78c2017-11-27 16:37:33 -08001890 if (!input.filename) {
1891 fprintf(stderr, "No input file specified!\n");
1892 usage_exit();
1893 }
John Koleszar53291892010-10-22 14:48:21 -04001894
John Koleszar8dd82872013-05-06 11:01:35 -07001895 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001896 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001897
John Koleszar6ad3b742012-11-06 12:02:42 -08001898 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001899 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001900 int64_t estimated_time_left = -1;
1901 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001902 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001903
John Koleszar6ad3b742012-11-06 12:02:42 -08001904 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001905
John Koleszar6ad3b742012-11-06 12:02:42 -08001906 /* If the input file doesn't specify its w/h (raw files), try to get
1907 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001908 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001909 if (!input.width || !input.height) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001910 FOREACH_STREAM(stream, streams) {
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001911 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1912 input.width = stream->config.cfg.g_w;
1913 input.height = stream->config.cfg.g_h;
1914 break;
1915 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001916 };
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001917 }
John Koleszarc6b90392012-07-13 15:21:29 -07001918
John Koleszar6ad3b742012-11-06 12:02:42 -08001919 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001920 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001921 fatal(
1922 "Specify stream dimensions with --width (-w) "
1923 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001924
1925 /* If input file does not specify bit-depth but input-bit-depth parameter
1926 * exists, assume that to be the input bit-depth. However, if the
1927 * input-bit-depth paramter does not exist, assume the input bit-depth
1928 * to be the same as the codec bit-depth.
1929 */
1930 if (!input.bit_depth) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001931 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001932 if (stream->config.cfg.g_input_bit_depth)
1933 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1934 else
1935 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1936 (int)stream->config.cfg.g_bit_depth;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001937 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001938 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001939 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001940 FOREACH_STREAM(stream, streams) {
1941 stream->config.cfg.g_input_bit_depth = input.bit_depth;
1942 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001943 }
1944
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001945 FOREACH_STREAM(stream, streams) {
Thomas Daede8ec53b22016-09-19 13:24:51 -07001946 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
1947 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
1948 was selected. */
1949 switch (stream->config.cfg.g_profile) {
1950 case 0:
1951 stream->config.cfg.g_profile = 1;
1952 profile_updated = 1;
1953 break;
1954 case 2:
1955 stream->config.cfg.g_profile = 3;
1956 profile_updated = 1;
1957 break;
1958 default: break;
1959 }
1960 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07001961 /* Automatically set the codec bit depth to match the input bit depth.
1962 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001963 if (stream->config.cfg.g_input_bit_depth >
1964 (unsigned int)stream->config.cfg.g_bit_depth) {
1965 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
1966 }
1967 if (stream->config.cfg.g_bit_depth > 8) {
1968 switch (stream->config.cfg.g_profile) {
1969 case 0:
1970 stream->config.cfg.g_profile = 2;
1971 profile_updated = 1;
1972 break;
1973 case 1:
1974 stream->config.cfg.g_profile = 3;
1975 profile_updated = 1;
1976 break;
1977 default: break;
1978 }
1979 }
1980 if (stream->config.cfg.g_profile > 1) {
Sebastien Alaiwanfadc7cb2017-06-08 22:10:43 +02001981 if (!CONFIG_HIGHBITDEPTH) fatal("Unsupported profile.");
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001982 stream->config.use_16bit_internal = 1;
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001983 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08001984 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001985 fprintf(stderr,
1986 "Warning: automatically upgrading to profile %d to "
1987 "match input format.\n",
1988 stream->config.cfg.g_profile);
1989 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001990 }
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001991
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02001992 FOREACH_STREAM(stream, streams) {
1993 set_stream_dimensions(stream, input.width, input.height);
1994 }
1995 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
John Koleszar77e6b452010-11-03 13:58:40 -04001996
John Koleszar6ad3b742012-11-06 12:02:42 -08001997 /* Ensure that --passes and --pass are consistent. If --pass is set and
1998 * --passes=2, ensure --fpf was set.
1999 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002000 if (global.pass && global.passes == 2) {
2001 FOREACH_STREAM(stream, streams) {
clang-format6c4d83e2016-08-08 19:03:30 -07002002 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002003 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002004 " and --passes=2\n",
2005 stream->index, global.pass);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002006 }
2007 }
John Koleszar3245d462010-06-10 17:10:12 -04002008
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002009#if !CONFIG_WEBM_IO
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002010 FOREACH_STREAM(stream, streams) {
Ronald S. Bultje39775072015-12-16 13:35:43 -05002011 if (stream->config.write_webm) {
2012 stream->config.write_webm = 0;
Cyril Concolato6c788832017-10-30 16:30:35 -07002013#if CONFIG_OBU_NO_IVF
2014 stream->config.write_ivf = 0;
2015#endif
clang-format6c4d83e2016-08-08 19:03:30 -07002016 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002017 "aomenc was compiled without WebM container support."
Cyril Concolato6c788832017-10-30 16:30:35 -07002018#if CONFIG_OBU_NO_IVF
2019 "Producing OBU output");
2020#else
clang-format6c4d83e2016-08-08 19:03:30 -07002021 "Producing IVF output");
Cyril Concolato6c788832017-10-30 16:30:35 -07002022#endif
Ronald S. Bultje39775072015-12-16 13:35:43 -05002023 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002024 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002025#endif
2026
John Koleszar6ad3b742012-11-06 12:02:42 -08002027 /* Use the frame rate from the file only if none was specified
2028 * on the command-line.
2029 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002030 if (!global.have_framerate) {
2031 global.framerate.num = input.framerate.numerator;
2032 global.framerate.den = input.framerate.denominator;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002033 FOREACH_STREAM(stream, streams) {
2034 stream->config.cfg.g_timebase.den = global.framerate.num;
2035 stream->config.cfg.g_timebase.num = global.framerate.den;
2036 }
Tom Finegan00a35aa2013-11-14 12:37:42 -08002037 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002038
John Koleszar6ad3b742012-11-06 12:02:42 -08002039 /* Show configuration */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002040 if (global.verbose && pass == 0) {
2041 FOREACH_STREAM(stream, streams) {
2042 show_stream_config(stream, &global, &input);
2043 }
2044 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002045
2046 if (pass == (global.pass ? global.pass - 1 : 0)) {
2047 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002048 /*The Y4M reader does its own allocation.
2049 Just initialize this here to avoid problems if we never read any
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002050 frames.*/
John Koleszarc6b90392012-07-13 15:21:29 -07002051 memset(&raw, 0, sizeof(raw));
2052 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002053 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002054
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002055 FOREACH_STREAM(stream, streams) {
2056 stream->rate_hist =
2057 init_rate_histogram(&stream->config.cfg, &global.framerate);
2058 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002059 }
2060
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002061 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2062 FOREACH_STREAM(stream, streams) {
2063 open_output_file(stream, &global, &input.pixel_aspect_ratio);
2064 }
2065 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002066
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002067#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002068 if (strcmp(global.codec->name, "av1") == 0 ||
2069 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002070 // Check to see if at least one stream uses 16 bit internal.
2071 // Currently assume that the bit_depths for all streams using
2072 // highbitdepth are the same.
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002073 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002074 if (stream->config.use_16bit_internal) {
2075 use_16bit_internal = 1;
2076 }
2077 if (stream->config.cfg.g_profile == 0) {
2078 input_shift = 0;
2079 } else {
2080 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07002081 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002082 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002083 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002084 }
2085#endif
2086
John Koleszarc6b90392012-07-13 15:21:29 -07002087 frame_avail = 1;
2088 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002089
John Koleszarc6b90392012-07-13 15:21:29 -07002090 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002091 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002092
John Koleszar6ad3b742012-11-06 12:02:42 -08002093 if (!global.limit || frames_in < global.limit) {
2094 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002095
clang-format6c4d83e2016-08-08 19:03:30 -07002096 if (frame_avail) frames_in++;
2097 seen_frames =
2098 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002099
John Koleszar6ad3b742012-11-06 12:02:42 -08002100 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002101 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002102 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2103
John Koleszar6ad3b742012-11-06 12:02:42 -08002104 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002105 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2106 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002107 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002108 fprintf(stderr, "frame %4d ", frames_in);
2109
clang-format6c4d83e2016-08-08 19:03:30 -07002110 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002111 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002112 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002113 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002114 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002115 }
2116
hui su251e1512016-10-03 15:48:43 -07002117 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002118 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002119 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002120
John Koleszar6ad3b742012-11-06 12:02:42 -08002121 if (frames_in > global.skip_frames) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002122#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002123 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002124 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2125 assert(use_16bit_internal);
2126 // Input bit depth and stream bit depth do not match, so up
2127 // shift frame to stream bit depth
2128 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002129 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002130 input.width, input.height, 32);
2131 allocated_raw_shift = 1;
2132 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002133 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002134 frame_to_encode = &raw_shift;
2135 } else {
2136 frame_to_encode = &raw;
2137 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002138 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002139 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002140 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002141 FOREACH_STREAM(stream, streams) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002142 if (stream->config.use_16bit_internal)
2143 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002144 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002145 else
2146 assert(0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002147 };
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002148 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002149 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002150 FOREACH_STREAM(stream, streams) {
2151 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2152 frames_in);
2153 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002154 }
2155#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002156 aom_usec_timer_start(&timer);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002157 FOREACH_STREAM(stream, streams) {
2158 encode_frame(stream, &global, frame_avail ? &raw : NULL, frames_in);
2159 }
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002160#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002161 aom_usec_timer_mark(&timer);
2162 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002163
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002164 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
John Koleszarc6b90392012-07-13 15:21:29 -07002165
John Koleszar0ea50ce2010-05-18 11:58:33 -04002166 got_data = 0;
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002167 FOREACH_STREAM(stream, streams) {
2168 get_cx_data(stream, &global, &got_data);
2169 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002170
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002171 if (!got_data && input.length && streams != NULL &&
2172 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002173 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002174 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002175 int64_t remaining;
2176 int64_t rate;
2177
2178 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002179 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002180
2181 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002182 remaining = 1000 * (global.limit - global.skip_frames -
2183 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002184 } else {
James Zerne3578af2014-04-17 10:47:08 -07002185 const int64_t input_pos = ftello(input.file);
2186 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002187 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002188
2189 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002190 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002191 }
2192
clang-format6c4d83e2016-08-08 19:03:30 -07002193 average_rate =
2194 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002195 estimated_time_left = average_rate ? remaining / average_rate : -1;
2196 }
2197
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002198 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2199 FOREACH_STREAM(stream, streams) {
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00002200 test_decode(stream, global.test_decode,
2201 stream->config.cfg.monochrome);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002202 }
2203 }
John Koleszarc6b90392012-07-13 15:21:29 -07002204 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002205
John Koleszarc6b90392012-07-13 15:21:29 -07002206 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002207 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002208 }
2209
clang-format6c4d83e2016-08-08 19:03:30 -07002210 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002211
Deb Mukherjeea160d722014-09-30 21:56:33 -07002212 if (!global.quiet) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002213 FOREACH_STREAM(stream, streams) {
2214 fprintf(stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2215 "b/f %7" PRId64
2216 "b/s"
2217 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2218 pass + 1, global.passes, frames_in, stream->frames_out,
2219 (int64_t)stream->nbytes,
2220 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2221 seen_frames
2222 ? (int64_t)stream->nbytes * 8 *
2223 (int64_t)global.framerate.num / global.framerate.den /
2224 seen_frames
2225 : 0,
2226 stream->cx_time > 9999999 ? stream->cx_time / 1000
2227 : stream->cx_time,
2228 stream->cx_time > 9999999 ? "ms" : "us",
2229 usec_to_fps(stream->cx_time, seen_frames));
2230 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002231 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002232
Deb Mukherjeea160d722014-09-30 21:56:33 -07002233 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002234 if (global.codec->fourcc == AV1_FOURCC) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002235 FOREACH_STREAM(stream, streams) {
2236 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1);
2237 }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002238 } else {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002239 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0); }
Deb Mukherjeea160d722014-09-30 21:56:33 -07002240 }
2241 }
John Koleszarc6b90392012-07-13 15:21:29 -07002242
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002243 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002244
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002245 if (global.test_decode != TEST_DECODE_OFF) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002246 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002247 }
2248
John Koleszar6ad3b742012-11-06 12:02:42 -08002249 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002250
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002251 if (global.test_decode == TEST_DECODE_FATAL) {
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002252 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002253 }
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002254 FOREACH_STREAM(stream, streams) {
2255 close_output_file(stream, global.codec->fourcc);
2256 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002257
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002258 FOREACH_STREAM(stream, streams) {
2259 stats_close(&stream->stats, global.passes - 1);
2260 }
John Koleszarc6b90392012-07-13 15:21:29 -07002261
Pengchong Jinf349b072014-07-14 09:13:38 -07002262#if CONFIG_FP_MB_STATS
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002263 FOREACH_STREAM(stream, streams) {
2264 stats_close(&stream->fpmb_stats, global.passes - 1);
2265 }
Pengchong Jinf349b072014-07-14 09:13:38 -07002266#endif
2267
clang-format6c4d83e2016-08-08 19:03:30 -07002268 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002269 }
2270
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002271 if (global.show_q_hist_buckets) {
2272 FOREACH_STREAM(stream, streams) {
2273 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2274 }
2275 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002276
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002277 if (global.show_rate_hist_buckets) {
2278 FOREACH_STREAM(stream, streams) {
2279 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2280 global.show_rate_hist_buckets);
2281 }
2282 }
2283 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
John Koleszar6ad3b742012-11-06 12:02:42 -08002284
Ronald S. Bultje31214972012-10-11 10:13:48 -07002285#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002286 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2287 * to match some existing utilities.
2288 */
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002289 if (!(global.pass == 1 && global.passes == 2)) {
2290 FOREACH_STREAM(stream, streams) {
Yaowu Xu47e784e2013-10-16 16:29:28 -07002291 FILE *f = fopen("opsnr.stt", "a");
2292 if (stream->mismatch_seen) {
2293 fprintf(f, "First mismatch occurred in frame %d\n",
2294 stream->mismatch_seen);
2295 } else {
2296 fprintf(f, "No mismatch detected in recon buffers\n");
2297 }
2298 fclose(f);
Sebastien Alaiwan0b993842017-06-09 10:28:29 +02002299 }
2300 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002301#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002302
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002303#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07002304 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002305#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002306 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002307 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002308 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002309 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002310}