blob: 9362202779580034ac1ed2f5cdffb161e94f3ec8 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#include "./aomenc.h"
13#include "./aom_config.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040014
Tom Finegan00a35aa2013-11-14 12:37:42 -080015#include <assert.h>
16#include <limits.h>
Tom Finegan78cb2e62013-11-07 21:28:45 -080017#include <math.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080018#include <stdarg.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040019#include <stdio.h>
20#include <stdlib.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040021#include <string.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080022
James Zern58058812014-08-15 21:00:31 -070023#if CONFIG_LIBYUV
24#include "third_party/libyuv/include/libyuv/scale.h"
25#endif
26
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom/aom_encoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080028#if CONFIG_DECODERS
Yaowu Xuf883b422016-08-30 14:01:10 -070029#include "aom/aom_decoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080030#endif
John Koleszar6ad3b742012-11-06 12:02:42 -080031
Tom Finegan00a35aa2013-11-14 12:37:42 -080032#include "./args.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080033#include "./ivfenc.h"
Tom Finegan7a691f12014-02-10 14:55:25 -080034#include "./tools_common.h"
Tom Finegan03848f52013-11-05 10:02:18 -080035
Yaowu Xuf883b422016-08-30 14:01:10 -070036#if CONFIG_AV1_ENCODER
37#include "aom/aomcx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080038#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070039#if CONFIG_AV1_DECODER
40#include "aom/aomdx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080041#endif
42
Yaowu Xuf883b422016-08-30 14:01:10 -070043#include "./aomstats.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080044#include "./rate_hist.h"
Tom Finegan249366b2013-11-25 12:05:19 -080045#include "./warnings.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080046#include "aom/aom_integer.h"
47#include "aom_ports/aom_timer.h"
48#include "aom_ports/mem_ops.h"
James Zerndba73762014-05-10 17:44:12 -070049#if CONFIG_WEBM_IO
Tom Finegan78cb2e62013-11-07 21:28:45 -080050#include "./webmenc.h"
James Zerndba73762014-05-10 17:44:12 -070051#endif
Tom Finegan78cb2e62013-11-07 21:28:45 -080052#include "./y4minput.h"
Tom Finegan03848f52013-11-05 10:02:18 -080053
John Koleszar6291dd42012-06-19 21:05:36 -070054/* Swallow warnings about unused results of fread/fwrite */
clang-format6c4d83e2016-08-08 19:03:30 -070055static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
John Koleszar6ad3b742012-11-06 12:02:42 -080056 return fread(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070057}
58#define fread wrap_fread
59
60static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080061 FILE *stream) {
62 return fwrite(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070063}
64#define fwrite wrap_fwrite
65
John Koleszar0ea50ce2010-05-18 11:58:33 -040066static const char *exec_name;
67
Yaowu Xuf883b422016-08-30 14:01:10 -070068static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080069 const char *s, va_list ap) {
John Koleszarc6b90392012-07-13 15:21:29 -070070 if (ctx->err) {
Yaowu Xuf883b422016-08-30 14:01:10 -070071 const char *detail = aom_codec_error_detail(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -040072
John Koleszar6ad3b742012-11-06 12:02:42 -080073 vfprintf(stderr, s, ap);
Yaowu Xuf883b422016-08-30 14:01:10 -070074 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -040075
clang-format6c4d83e2016-08-08 19:03:30 -070076 if (detail) fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -040077
clang-format6c4d83e2016-08-08 19:03:30 -070078 if (fatal) exit(EXIT_FAILURE);
John Koleszarc6b90392012-07-13 15:21:29 -070079 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040080}
81
Yaowu Xuf883b422016-08-30 14:01:10 -070082static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080083 va_list ap;
84
85 va_start(ap, s);
86 warn_or_exit_on_errorv(ctx, 1, s, ap);
87 va_end(ap);
88}
89
Yaowu Xuf883b422016-08-30 14:01:10 -070090static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080091 const char *s, ...) {
92 va_list ap;
93
94 va_start(ap, s);
95 warn_or_exit_on_errorv(ctx, fatal, s, ap);
96 va_end(ap);
97}
98
Yaowu Xuf883b422016-08-30 14:01:10 -070099static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800100 FILE *f = input_ctx->file;
101 y4m_input *y4m = &input_ctx->y4m;
John Koleszarc6b90392012-07-13 15:21:29 -0700102 int shortread = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400103
Tom Finegan00a35aa2013-11-14 12:37:42 -0800104 if (input_ctx->file_type == FILE_TYPE_Y4M) {
clang-format6c4d83e2016-08-08 19:03:30 -0700105 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700106 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800107 shortread = read_yuv_frame(input_ctx, img);
John Koleszarc6b90392012-07-13 15:21:29 -0700108 }
109
110 return !shortread;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400111}
112
James Zern5c337fd2015-05-09 10:42:58 -0700113static int file_is_y4m(const char detect[4]) {
John Koleszarc6b90392012-07-13 15:21:29 -0700114 if (memcmp(detect, "YUV4", 4) == 0) {
115 return 1;
116 }
117 return 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400118}
119
James Zern5c337fd2015-05-09 10:42:58 -0700120static int fourcc_is_ivf(const char detect[4]) {
Alex Converse64b89f12014-01-06 16:29:09 -0800121 if (memcmp(detect, "DKIF", 4) == 0) {
122 return 1;
123 }
124 return 0;
125}
John Koleszardc666302010-10-20 12:05:48 -0400126
clang-format6c4d83e2016-08-08 19:03:30 -0700127static const arg_def_t debugmode =
128 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
129static const arg_def_t outputfile =
130 ARG_DEF("o", "output", 1, "Output filename");
131static const arg_def_t use_yv12 =
132 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
133static const arg_def_t use_i420 =
134 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
135static const arg_def_t use_i422 =
136 ARG_DEF(NULL, "i422", 0, "Input file is I422");
137static const arg_def_t use_i444 =
138 ARG_DEF(NULL, "i444", 0, "Input file is I444");
139static const arg_def_t use_i440 =
140 ARG_DEF(NULL, "i440", 0, "Input file is I440");
141static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
142static const arg_def_t passes =
143 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
144static const arg_def_t pass_arg =
145 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
146static const arg_def_t fpf_name =
147 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700148#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700149static const arg_def_t fpmbf_name =
150 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700151#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700152static const arg_def_t limit =
153 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
154static const arg_def_t skip =
155 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
156static const arg_def_t deadline =
157 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
clang-format6c4d83e2016-08-08 19:03:30 -0700158static const arg_def_t good_dl =
159 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
160static const arg_def_t rt_dl =
161 ARG_DEF(NULL, "rt", 0, "Use Realtime Quality Deadline");
162static const arg_def_t quietarg =
163 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
164static const arg_def_t verbosearg =
165 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
166static const arg_def_t psnrarg =
167 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800168
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800169static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700170 { "off", TEST_DECODE_OFF },
171 { "fatal", TEST_DECODE_FATAL },
172 { "warn", TEST_DECODE_WARN },
173 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800174};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700175static const arg_def_t recontest = ARG_DEF_ENUM(
176 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700177static const arg_def_t framerate =
178 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
179static const arg_def_t use_webm =
180 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
181static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
182static const arg_def_t out_part =
183 ARG_DEF("P", "output-partitions", 0,
184 "Makes encoder output partitions. Requires IVF output!");
185static const arg_def_t q_hist_n =
186 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
187static const arg_def_t rate_hist_n =
188 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
189static const arg_def_t disable_warnings =
190 ARG_DEF(NULL, "disable-warnings", 0,
191 "Disable warnings about potentially incorrect encode settings.");
192static const arg_def_t disable_warning_prompt =
193 ARG_DEF("y", "disable-warning-prompt", 0,
194 "Display warnings, but do not prompt user to continue.");
Alex Conversed66bd222014-02-21 10:52:09 -0800195
Yaowu Xuf883b422016-08-30 14:01:10 -0700196#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700197static const arg_def_t test16bitinternalarg = ARG_DEF(
198 NULL, "test-16bit-internal", 0, "Force use of 16 bit internal buffer");
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400199
200static const struct arg_enum_list bitdepth_enum[] = {
201 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
202};
203
204static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
205 "b", "bit-depth", 1,
206 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
207 bitdepth_enum);
208static const arg_def_t inbitdeptharg =
209 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700210#endif
Tom Finegan44dd3272013-11-20 17:18:28 -0800211
clang-format6c4d83e2016-08-08 19:03:30 -0700212static const arg_def_t *main_args[] = { &debugmode,
213 &outputfile,
214 &codecarg,
215 &passes,
216 &pass_arg,
217 &fpf_name,
218 &limit,
219 &skip,
220 &deadline,
clang-format6c4d83e2016-08-08 19:03:30 -0700221 &good_dl,
222 &rt_dl,
223 &quietarg,
224 &verbosearg,
225 &psnrarg,
226 &use_webm,
227 &use_ivf,
228 &out_part,
229 &q_hist_n,
230 &rate_hist_n,
231 &disable_warnings,
232 &disable_warning_prompt,
233 &recontest,
234 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400235
clang-format6c4d83e2016-08-08 19:03:30 -0700236static const arg_def_t usage =
237 ARG_DEF("u", "usage", 1, "Usage profile number to use");
238static const arg_def_t threads =
239 ARG_DEF("t", "threads", 1, "Max number of threads to use");
240static const arg_def_t profile =
241 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700242static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
243static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700244#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700245static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700246 { "mono", STEREO_FORMAT_MONO },
247 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
248 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
249 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
250 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
251 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700252};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700253static const arg_def_t stereo_mode = ARG_DEF_ENUM(
254 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700255#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700256static const arg_def_t timebase = ARG_DEF(
257 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700258static const arg_def_t error_resilient =
259 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
260static const arg_def_t lag_in_frames =
261 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400262
clang-format6c4d83e2016-08-08 19:03:30 -0700263static const arg_def_t *global_args[] = { &use_yv12,
264 &use_i420,
265 &use_i422,
266 &use_i444,
267 &use_i440,
268 &usage,
269 &threads,
270 &profile,
271 &width,
272 &height,
James Zerndba73762014-05-10 17:44:12 -0700273#if CONFIG_WEBM_IO
clang-format6c4d83e2016-08-08 19:03:30 -0700274 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700275#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700276 &timebase,
277 &framerate,
278 &error_resilient,
Yaowu Xuf883b422016-08-30 14:01:10 -0700279#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700280 &test16bitinternalarg,
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400281 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700282#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700283 &lag_in_frames,
284 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400285
clang-format6c4d83e2016-08-08 19:03:30 -0700286static const arg_def_t dropframe_thresh =
287 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
288static const arg_def_t resize_allowed =
289 ARG_DEF(NULL, "resize-allowed", 1, "Spatial resampling enabled (bool)");
290static const arg_def_t resize_width =
291 ARG_DEF(NULL, "resize-width", 1, "Width of encoded frame");
292static const arg_def_t resize_height =
293 ARG_DEF(NULL, "resize-height", 1, "Height of encoded frame");
294static const arg_def_t resize_up_thresh =
295 ARG_DEF(NULL, "resize-up", 1, "Upscale threshold (buf %)");
296static const arg_def_t resize_down_thresh =
297 ARG_DEF(NULL, "resize-down", 1, "Downscale threshold (buf %)");
Yaowu Xuf883b422016-08-30 14:01:10 -0700298static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
299 { "cbr", AOM_CBR },
300 { "cq", AOM_CQ },
301 { "q", AOM_Q },
clang-format6c4d83e2016-08-08 19:03:30 -0700302 { NULL, 0 } };
303static const arg_def_t end_usage =
304 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
305static const arg_def_t target_bitrate =
306 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
307static const arg_def_t min_quantizer =
308 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
309static const arg_def_t max_quantizer =
310 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
311static const arg_def_t undershoot_pct =
312 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
313static const arg_def_t overshoot_pct =
314 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
315static const arg_def_t buf_sz =
316 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
317static const arg_def_t buf_initial_sz =
318 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
319static const arg_def_t buf_optimal_sz =
320 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
John Koleszarc6b90392012-07-13 15:21:29 -0700321static const arg_def_t *rc_args[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700322 &dropframe_thresh, &resize_allowed, &resize_width, &resize_height,
323 &resize_up_thresh, &resize_down_thresh, &end_usage, &target_bitrate,
324 &min_quantizer, &max_quantizer, &undershoot_pct, &overshoot_pct,
325 &buf_sz, &buf_initial_sz, &buf_optimal_sz, NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -0400326};
327
clang-format6c4d83e2016-08-08 19:03:30 -0700328static const arg_def_t bias_pct =
329 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
330static const arg_def_t minsection_pct =
331 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
332static const arg_def_t maxsection_pct =
333 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
334static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
335 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400336
clang-format6c4d83e2016-08-08 19:03:30 -0700337static const arg_def_t kf_min_dist =
338 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
339static const arg_def_t kf_max_dist =
340 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
341static const arg_def_t kf_disabled =
342 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
343static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
344 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400345
clang-format6c4d83e2016-08-08 19:03:30 -0700346static const arg_def_t noise_sens =
347 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
348static const arg_def_t sharpness =
349 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
350static const arg_def_t static_thresh =
351 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
352static const arg_def_t auto_altref =
353 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
354static const arg_def_t arnr_maxframes =
355 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
356static const arg_def_t arnr_strength =
357 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500358static const struct arg_enum_list tuning_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700359 { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500360};
clang-format6c4d83e2016-08-08 19:03:30 -0700361static const arg_def_t tune_ssim =
362 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
363static const arg_def_t cq_level =
364 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
365static const arg_def_t max_intra_rate_pct =
366 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800367
Yaowu Xuf883b422016-08-30 14:01:10 -0700368#if CONFIG_AV1_ENCODER
369static const arg_def_t cpu_used_av1 =
clang-format6c4d83e2016-08-08 19:03:30 -0700370 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (-8..8)");
371static const arg_def_t tile_cols =
372 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
373static const arg_def_t tile_rows =
374 ARG_DEF(NULL, "tile-rows", 1,
375 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800376#if CONFIG_DEPENDENT_HORZTILES
377static const arg_def_t tile_dependent_rows =
378 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
379#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800380#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800381static const arg_def_t tile_loopfilter = ARG_DEF(
382 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800383#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700384static const arg_def_t lossless =
385 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700386#if CONFIG_AOM_QM
387static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800388 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700389 "Enable quantisation matrices (0: false (default), 1: true)");
390static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800391 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700392static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800393 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700394#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000395#if CONFIG_TILE_GROUPS
Yaowu Xu859a5272016-11-10 15:32:21 -0800396static const arg_def_t num_tg = ARG_DEF(
397 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000398static const arg_def_t mtu_size =
399 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800400 "MTU size for a tile group, default is 0 (no MTU targeting), "
401 "overrides maximum number of tile groups");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000402#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800403#if CONFIG_TEMPMV_SIGNALING
404static const arg_def_t disable_tempmv = ARG_DEF(
405 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
406#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700407static const arg_def_t frame_parallel_decoding =
408 ARG_DEF(NULL, "frame-parallel", 1,
409 "Enable frame parallel decodability features "
410 "(0: false (default), 1: true)");
Thomase28d92b2016-09-20 12:07:11 +0100411#if CONFIG_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100412static const arg_def_t aq_mode = ARG_DEF(
413 NULL, "aq-mode", 1,
414 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100415 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200416#else
Thomase28d92b2016-09-20 12:07:11 +0100417static const arg_def_t aq_mode = ARG_DEF(
418 NULL, "aq-mode", 1,
419 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100420 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200421#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700422static const arg_def_t frame_periodic_boost =
423 ARG_DEF(NULL, "frame-boost", 1,
424 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700425static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
426 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700427static const arg_def_t max_inter_rate_pct =
428 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700429static const arg_def_t min_gf_interval = ARG_DEF(
430 NULL, "min-gf-interval", 1,
431 "min gf/arf frame interval (default 0, indicating in-built behavior)");
432static const arg_def_t max_gf_interval = ARG_DEF(
433 NULL, "max-gf-interval", 1,
434 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800435
Yaowu Xufc996362015-02-10 15:03:05 -0800436static const struct arg_enum_list color_space_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700437 { "unknown", AOM_CS_UNKNOWN },
438 { "bt601", AOM_CS_BT_601 },
439 { "bt709", AOM_CS_BT_709 },
440 { "smpte170", AOM_CS_SMPTE_170 },
441 { "smpte240", AOM_CS_SMPTE_240 },
442 { "bt2020", AOM_CS_BT_2020 },
443 { "reserved", AOM_CS_RESERVED },
444 { "sRGB", AOM_CS_SRGB },
Yaowu Xufc996362015-02-10 15:03:05 -0800445 { NULL, 0 }
446};
447
clang-format6c4d83e2016-08-08 19:03:30 -0700448static const arg_def_t input_color_space =
449 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
450 color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800451
Alex Converse219f6452014-08-06 11:01:18 -0700452static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700453 { "default", AOM_CONTENT_DEFAULT },
454 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700455 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700456};
457
458static const arg_def_t tune_content = ARG_DEF_ENUM(
459 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700460#endif
Alex Converse219f6452014-08-06 11:01:18 -0700461
Yaowu Xuf883b422016-08-30 14:01:10 -0700462#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000463#if CONFIG_EXT_PARTITION
464static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700465 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
466 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
467 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700468 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000469};
470static const arg_def_t superblock_size = ARG_DEF_ENUM(
471 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
472#endif // CONFIG_EXT_PARTITION
473
Yaowu Xuf883b422016-08-30 14:01:10 -0700474static const arg_def_t *av1_args[] = { &cpu_used_av1,
475 &auto_altref,
476 &sharpness,
477 &static_thresh,
478 &tile_cols,
479 &tile_rows,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800480#if CONFIG_DEPENDENT_HORZTILES
481 &tile_dependent_rows,
482#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800483#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800484 &tile_loopfilter,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800485#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700486 &arnr_maxframes,
487 &arnr_strength,
Yaowu Xuf883b422016-08-30 14:01:10 -0700488 &tune_ssim,
489 &cq_level,
490 &max_intra_rate_pct,
491 &max_inter_rate_pct,
492 &gf_cbr_boost_pct,
493 &lossless,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800494#if CONFIG_AOM_QM
495 &enable_qm,
496 &qm_min,
497 &qm_max,
498#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700499 &frame_parallel_decoding,
500 &aq_mode,
501 &frame_periodic_boost,
502 &noise_sens,
503 &tune_content,
504 &input_color_space,
505 &min_gf_interval,
506 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000507#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700508 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000509#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000510#if CONFIG_TILE_GROUPS
511 &num_tg,
512 &mtu_size,
513#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800514#if CONFIG_TEMPMV_SIGNALING
515 &disable_tempmv,
516#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700517#if CONFIG_AOM_HIGHBITDEPTH
518 &bitdeptharg,
519 &inbitdeptharg,
520#endif // CONFIG_AOM_HIGHBITDEPTH
521 NULL };
522static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
523 AOME_SET_ENABLEAUTOALTREF,
524 AOME_SET_SHARPNESS,
525 AOME_SET_STATIC_THRESHOLD,
526 AV1E_SET_TILE_COLUMNS,
527 AV1E_SET_TILE_ROWS,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800528#if CONFIG_DEPENDENT_HORZTILES
529 AV1E_SET_TILE_DEPENDENT_ROWS,
530#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800531#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800532 AV1E_SET_TILE_LOOPFILTER,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800533#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700534 AOME_SET_ARNR_MAXFRAMES,
535 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700536 AOME_SET_TUNING,
537 AOME_SET_CQ_LEVEL,
538 AOME_SET_MAX_INTRA_BITRATE_PCT,
539 AV1E_SET_MAX_INTER_BITRATE_PCT,
540 AV1E_SET_GF_CBR_BOOST_PCT,
541 AV1E_SET_LOSSLESS,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800542#if CONFIG_AOM_QM
543 AV1E_SET_ENABLE_QM,
544 AV1E_SET_QM_MIN,
545 AV1E_SET_QM_MAX,
546#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700547 AV1E_SET_FRAME_PARALLEL_DECODING,
548 AV1E_SET_AQ_MODE,
549 AV1E_SET_FRAME_PERIODIC_BOOST,
550 AV1E_SET_NOISE_SENSITIVITY,
551 AV1E_SET_TUNE_CONTENT,
552 AV1E_SET_COLOR_SPACE,
553 AV1E_SET_MIN_GF_INTERVAL,
554 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000555#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700556 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000557#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000558#if CONFIG_TILE_GROUPS
559 AV1E_SET_NUM_TG,
560 AV1E_SET_MTU,
561#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800562#if CONFIG_TEMPMV_SIGNALING
563 AV1E_SET_DISABLE_TEMPMV,
564#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700565 0 };
hui su82331e02015-08-16 18:21:56 -0700566#endif
567
John Koleszar0ea50ce2010-05-18 11:58:33 -0400568static const arg_def_t *no_args[] = { NULL };
569
James Zern59e7a472015-05-09 10:33:26 -0700570void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700571 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700572 const int num_encoder = get_aom_encoder_count();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400573
John Koleszarc6b90392012-07-13 15:21:29 -0700574 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
575 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400576
John Koleszarc6b90392012-07-13 15:21:29 -0700577 fprintf(stderr, "\nOptions:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700578 arg_show_usage(stderr, main_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700579 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700580 arg_show_usage(stderr, global_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700581 fprintf(stderr, "\nRate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700582 arg_show_usage(stderr, rc_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700583 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700584 arg_show_usage(stderr, rc_twopass_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700585 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700586 arg_show_usage(stderr, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700587#if CONFIG_AV1_ENCODER
588 fprintf(stderr, "\nAV1 Specific Options:\n");
589 arg_show_usage(stderr, av1_args);
hui su82331e02015-08-16 18:21:56 -0700590#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700591 fprintf(stderr,
592 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700593 " The desired precision of timestamps in the output, expressed\n"
594 " in fractional seconds. Default is 1/1000.\n");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800595 fprintf(stderr, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400596
Yaowu Xuf990b352015-06-01 09:13:59 -0700597 for (i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700598 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700599 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
600 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700601 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800602 }
Yaowu Xuf990b352015-06-01 09:13:59 -0700603 fprintf(stderr, "\n ");
604 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400605
John Koleszarc6b90392012-07-13 15:21:29 -0700606 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400607}
608
clang-format6c4d83e2016-08-08 19:03:30 -0700609#define mmin(a, b) ((a) < (b) ? (a) : (b))
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700610
Yaowu Xuf883b422016-08-30 14:01:10 -0700611#if CONFIG_AOM_HIGHBITDEPTH
612static void find_mismatch_high(const aom_image_t *const img1,
613 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700614 int uloc[4], int vloc[4]) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700615 uint16_t *plane1, *plane2;
616 uint32_t stride1, stride2;
617 const uint32_t bsize = 64;
618 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
619 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
620 const uint32_t c_w =
621 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
622 const uint32_t c_h =
623 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
624 int match = 1;
625 uint32_t i, j;
626 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700627 plane1 = (uint16_t *)img1->planes[AOM_PLANE_Y];
628 plane2 = (uint16_t *)img2->planes[AOM_PLANE_Y];
629 stride1 = img1->stride[AOM_PLANE_Y] / 2;
630 stride2 = img2->stride[AOM_PLANE_Y] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700631 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
632 for (j = 0; match && j < img1->d_w; j += bsize) {
633 int k, l;
634 const int si = mmin(i + bsize, img1->d_h) - i;
635 const int sj = mmin(j + bsize, img1->d_w) - j;
636 for (k = 0; match && k < si; ++k) {
637 for (l = 0; match && l < sj; ++l) {
638 if (*(plane1 + (i + k) * stride1 + j + l) !=
639 *(plane2 + (i + k) * stride2 + j + l)) {
640 yloc[0] = i + k;
641 yloc[1] = j + l;
642 yloc[2] = *(plane1 + (i + k) * stride1 + j + l);
643 yloc[3] = *(plane2 + (i + k) * stride2 + j + l);
644 match = 0;
645 break;
646 }
647 }
648 }
649 }
650 }
651
652 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700653 plane1 = (uint16_t *)img1->planes[AOM_PLANE_U];
654 plane2 = (uint16_t *)img2->planes[AOM_PLANE_U];
655 stride1 = img1->stride[AOM_PLANE_U] / 2;
656 stride2 = img2->stride[AOM_PLANE_U] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700657 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
658 for (j = 0; match && j < c_w; j += bsizex) {
659 int k, l;
660 const int si = mmin(i + bsizey, c_h - i);
661 const int sj = mmin(j + bsizex, c_w - j);
662 for (k = 0; match && k < si; ++k) {
663 for (l = 0; match && l < sj; ++l) {
664 if (*(plane1 + (i + k) * stride1 + j + l) !=
665 *(plane2 + (i + k) * stride2 + j + l)) {
666 uloc[0] = i + k;
667 uloc[1] = j + l;
668 uloc[2] = *(plane1 + (i + k) * stride1 + j + l);
669 uloc[3] = *(plane2 + (i + k) * stride2 + j + l);
670 match = 0;
671 break;
672 }
673 }
674 }
675 }
676 }
677
678 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700679 plane1 = (uint16_t *)img1->planes[AOM_PLANE_V];
680 plane2 = (uint16_t *)img2->planes[AOM_PLANE_V];
681 stride1 = img1->stride[AOM_PLANE_V] / 2;
682 stride2 = img2->stride[AOM_PLANE_V] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700683 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
684 for (j = 0; match && j < c_w; j += bsizex) {
685 int k, l;
686 const int si = mmin(i + bsizey, c_h - i);
687 const int sj = mmin(j + bsizex, c_w - j);
688 for (k = 0; match && k < si; ++k) {
689 for (l = 0; match && l < sj; ++l) {
690 if (*(plane1 + (i + k) * stride1 + j + l) !=
691 *(plane2 + (i + k) * stride2 + j + l)) {
692 vloc[0] = i + k;
693 vloc[1] = j + l;
694 vloc[2] = *(plane1 + (i + k) * stride1 + j + l);
695 vloc[3] = *(plane2 + (i + k) * stride2 + j + l);
696 match = 0;
697 break;
698 }
699 }
700 }
701 }
702 }
703}
704#endif
705
Yaowu Xuf883b422016-08-30 14:01:10 -0700706static void find_mismatch(const aom_image_t *const img1,
707 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700708 int uloc[4], int vloc[4]) {
James Zern6253e8e2014-01-31 16:10:43 -0800709 const uint32_t bsize = 64;
710 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
711 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
712 const uint32_t c_w =
713 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
714 const uint32_t c_h =
715 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
716 int match = 1;
717 uint32_t i, j;
Deb Mukherjee23144d22013-03-12 14:21:08 -0700718 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -0800719 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
720 for (j = 0; match && j < img1->d_w; j += bsize) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800721 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800722 const int si = mmin(i + bsize, img1->d_h) - i;
723 const int sj = mmin(j + bsize, img1->d_w) - j;
724 for (k = 0; match && k < si; ++k) {
725 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700726 if (*(img1->planes[AOM_PLANE_Y] +
727 (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
728 *(img2->planes[AOM_PLANE_Y] +
729 (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800730 yloc[0] = i + k;
731 yloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700732 yloc[2] = *(img1->planes[AOM_PLANE_Y] +
733 (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
734 yloc[3] = *(img2->planes[AOM_PLANE_Y] +
735 (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800736 match = 0;
737 break;
738 }
739 }
James Zernd04112d2014-01-31 15:49:17 -0800740 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800741 }
742 }
Jingning Han12bf0792013-04-06 10:00:53 -0700743
Deb Mukherjee23144d22013-03-12 14:21:08 -0700744 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700745 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700746 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800747 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800748 const int si = mmin(i + bsizey, c_h - i);
749 const int sj = mmin(j + bsizex, c_w - j);
750 for (k = 0; match && k < si; ++k) {
751 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700752 if (*(img1->planes[AOM_PLANE_U] +
753 (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
754 *(img2->planes[AOM_PLANE_U] +
755 (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800756 uloc[0] = i + k;
757 uloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700758 uloc[2] = *(img1->planes[AOM_PLANE_U] +
759 (i + k) * img1->stride[AOM_PLANE_U] + j + l);
760 uloc[3] = *(img2->planes[AOM_PLANE_U] +
761 (i + k) * img2->stride[AOM_PLANE_U] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800762 match = 0;
763 break;
764 }
765 }
James Zernd04112d2014-01-31 15:49:17 -0800766 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800767 }
768 }
Deb Mukherjee23144d22013-03-12 14:21:08 -0700769 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700770 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700771 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800772 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800773 const int si = mmin(i + bsizey, c_h - i);
774 const int sj = mmin(j + bsizex, c_w - j);
775 for (k = 0; match && k < si; ++k) {
776 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700777 if (*(img1->planes[AOM_PLANE_V] +
778 (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
779 *(img2->planes[AOM_PLANE_V] +
780 (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800781 vloc[0] = i + k;
782 vloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700783 vloc[2] = *(img1->planes[AOM_PLANE_V] +
784 (i + k) * img1->stride[AOM_PLANE_V] + j + l);
785 vloc[3] = *(img2->planes[AOM_PLANE_V] +
786 (i + k) * img2->stride[AOM_PLANE_V] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800787 match = 0;
788 break;
789 }
790 }
James Zernd04112d2014-01-31 15:49:17 -0800791 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800792 }
793 }
794}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400795
Yaowu Xuf883b422016-08-30 14:01:10 -0700796static int compare_img(const aom_image_t *const img1,
797 const aom_image_t *const img2) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700798 uint32_t l_w = img1->d_w;
clang-format6c4d83e2016-08-08 19:03:30 -0700799 uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
James Zern6253e8e2014-01-31 16:10:43 -0800800 const uint32_t c_h =
801 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
802 uint32_t i;
John Koleszarc6b90392012-07-13 15:21:29 -0700803 int match = 1;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800804
John Koleszarc6b90392012-07-13 15:21:29 -0700805 match &= (img1->fmt == img2->fmt);
hkuang3671e622014-01-22 14:58:59 -0800806 match &= (img1->d_w == img2->d_w);
807 match &= (img1->d_h == img2->d_h);
Yaowu Xuf883b422016-08-30 14:01:10 -0700808#if CONFIG_AOM_HIGHBITDEPTH
809 if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700810 l_w *= 2;
811 c_w *= 2;
812 }
813#endif
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800814
James Zernd04112d2014-01-31 15:49:17 -0800815 for (i = 0; i < img1->d_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700816 match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
817 img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700818 l_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800819
James Zernd04112d2014-01-31 15:49:17 -0800820 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700821 match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
822 img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
John Koleszarbeae5012013-05-08 16:07:38 -0700823 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800824
James Zernd04112d2014-01-31 15:49:17 -0800825 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700826 match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
827 img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
John Koleszarbeae5012013-05-08 16:07:38 -0700828 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800829
John Koleszarc6b90392012-07-13 15:21:29 -0700830 return match;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800831}
832
clang-format6c4d83e2016-08-08 19:03:30 -0700833#define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
Yaowu Xuf883b422016-08-30 14:01:10 -0700834#if CONFIG_AV1_ENCODER
835#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800836#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800837
James Zerndba73762014-05-10 17:44:12 -0700838#if !CONFIG_WEBM_IO
839typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700840struct WebmOutputContext {
841 int debug;
842};
James Zerndba73762014-05-10 17:44:12 -0700843#endif
844
John Koleszar9e50ed72012-02-15 12:39:38 -0800845/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800846struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700847 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700848 const char *out_fn;
849 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700850#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700851 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700852#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700853 stereo_format_t stereo_fmt;
854 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
855 int arg_ctrl_cnt;
856 int write_webm;
Yaowu Xuf883b422016-08-30 14:01:10 -0700857#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700858 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700859 int use_16bit_internal;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700860#endif
John Koleszar9e50ed72012-02-15 12:39:38 -0800861};
862
John Koleszar6ad3b742012-11-06 12:02:42 -0800863struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700864 int index;
865 struct stream_state *next;
866 struct stream_config config;
867 FILE *file;
868 struct rate_hist *rate_hist;
869 struct WebmOutputContext webm_ctx;
870 uint64_t psnr_sse_total;
871 uint64_t psnr_samples_total;
872 double psnr_totals[4];
873 int psnr_count;
874 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700875 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700876 unsigned int frames_out;
877 uint64_t cx_time;
878 size_t nbytes;
879 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700880#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700881 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700882#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700883 struct aom_image *img;
884 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700885 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800886};
887
clang-format6c4d83e2016-08-08 19:03:30 -0700888static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700889 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800890 if (rat->den < 0) {
891 rat->num *= -1;
892 rat->den *= -1;
893 }
John Koleszar1b27e932012-04-25 17:08:56 -0700894
clang-format6c4d83e2016-08-08 19:03:30 -0700895 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700896
clang-format6c4d83e2016-08-08 19:03:30 -0700897 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700898}
899
Yaowu Xuf883b422016-08-30 14:01:10 -0700900static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700901 char **argi, **argj;
902 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700903 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700904
clang-format6c4d83e2016-08-08 19:03:30 -0700905 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800906
John Koleszar6ad3b742012-11-06 12:02:42 -0800907 /* Initialize default parameters */
908 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700909 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700910 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700911 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700912 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700913 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400914
John Koleszarc6b90392012-07-13 15:21:29 -0700915 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
916 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400917
John Koleszarc6b90392012-07-13 15:21:29 -0700918 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700919 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800920 if (!global->codec)
921 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700922 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800923 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400924
John Koleszar6ad3b742012-11-06 12:02:42 -0800925 if (global->passes < 1 || global->passes > 2)
926 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700927 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800928 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400929
John Koleszar6ad3b742012-11-06 12:02:42 -0800930 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700931 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800932 } else if (arg_match(&arg, &usage, argi))
933 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700934 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800935 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700936 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700937 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -0700938 else if (arg_match(&arg, &rt_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700939 global->deadline = AOM_DL_REALTIME;
John Koleszar6ad3b742012-11-06 12:02:42 -0800940 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700941 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800942 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700943 global->color_type = I420;
944 else if (arg_match(&arg, &use_i422, argi))
945 global->color_type = I422;
946 else if (arg_match(&arg, &use_i444, argi))
947 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700948 else if (arg_match(&arg, &use_i440, argi))
949 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800950 else if (arg_match(&arg, &quietarg, argi))
951 global->quiet = 1;
952 else if (arg_match(&arg, &verbosearg, argi))
953 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700954 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800955 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700956 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800957 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700958 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800959 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700960 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800961 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700962 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800963 global->framerate = arg_parse_rational(&arg);
964 validate_positive_rational(arg.name, &global->framerate);
965 global->have_framerate = 1;
966 } else if (arg_match(&arg, &out_part, argi))
967 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700968 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800969 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700970 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800971 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700972 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800973 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800974 else if (arg_match(&arg, &disable_warnings, argi))
975 global->disable_warnings = 1;
976 else if (arg_match(&arg, &disable_warning_prompt, argi))
977 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800978 else
John Koleszarc6b90392012-07-13 15:21:29 -0700979 argj++;
980 }
981
John Koleszar6ad3b742012-11-06 12:02:42 -0800982 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700983 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800984 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700985 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
986 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800987 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800988 }
John Koleszarc6b90392012-07-13 15:21:29 -0700989 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800990 /* Validate global config */
991 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700992#if CONFIG_AV1_ENCODER
993 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800994 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700995 if (global->codec != NULL && global->codec->name != NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -0700996 global->passes = (strcmp(global->codec->name, "av1") == 0 &&
997 global->deadline != AOM_DL_REALTIME)
clang-format6c4d83e2016-08-08 19:03:30 -0700998 ? 2
999 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001000#else
1001 global->passes = 1;
1002#endif
1003 }
1004
Yaowu Xuf883b422016-08-30 14:01:10 -07001005 if (global->deadline == AOM_DL_REALTIME && global->passes > 1) {
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001006 warn("Enforcing one-pass encoding in realtime mode\n");
1007 global->passes = 1;
1008 }
John Koleszar732cb9a2012-02-14 12:30:17 -08001009}
1010
Yaowu Xuf883b422016-08-30 14:01:10 -07001011static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001012 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -07001013 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
1014 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -08001015
clang-format6c4d83e2016-08-08 19:03:30 -07001016 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -08001017
John Koleszar25b6e9f2013-02-12 21:17:56 -08001018 if (!fseeko(input->file, 0, SEEK_END)) {
1019 /* Input file is seekable. Figure out how long it is, so we can get
1020 * progress info.
1021 */
1022 input->length = ftello(input->file);
1023 rewind(input->file);
1024 }
1025
Frank Galligan09acd262015-06-01 10:20:58 -07001026 /* Default to 1:1 pixel aspect ratio. */
1027 input->pixel_aspect_ratio.numerator = 1;
1028 input->pixel_aspect_ratio.denominator = 1;
1029
John Koleszar6ad3b742012-11-06 12:02:42 -08001030 /* For RAW input sources, these bytes will applied on the first frame
1031 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -07001032 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001033 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1034 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001035
clang-format6c4d83e2016-08-08 19:03:30 -07001036 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -07001037 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
1038 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001039 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001040 input->width = input->y4m.pic_w;
1041 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -07001042 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1043 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001044 input->framerate.numerator = input->y4m.fps_n;
1045 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -07001046 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001047 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -08001048 } else
1049 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -08001050 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -08001051 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -08001052 } else {
1053 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001054 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001055}
1056
Yaowu Xuf883b422016-08-30 14:01:10 -07001057static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001058 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -07001059 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001060}
1061
Yaowu Xuf883b422016-08-30 14:01:10 -07001062static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -08001063 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001064 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001065
John Koleszar6ad3b742012-11-06 12:02:42 -08001066 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001067 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001068 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001069 }
1070
John Koleszar6ad3b742012-11-06 12:02:42 -08001071 if (prev) {
1072 memcpy(stream, prev, sizeof(*stream));
1073 stream->index++;
1074 prev->next = stream;
1075 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001076 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001077
John Koleszar6ad3b742012-11-06 12:02:42 -08001078 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -07001079 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -07001080 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -07001081 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001082
John Koleszar6ad3b742012-11-06 12:02:42 -08001083 /* Change the default timebase to a high enough value so that the
1084 * encoder will always create strictly increasing timestamps.
1085 */
1086 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001087
John Koleszar6ad3b742012-11-06 12:02:42 -08001088 /* Never use the library's default resolution, require it be parsed
1089 * from the file or set on the command line.
1090 */
1091 stream->config.cfg.g_w = 0;
1092 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001093
John Koleszar6ad3b742012-11-06 12:02:42 -08001094 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -08001095 stream->config.write_webm = 1;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001096#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -07001097 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001098 stream->webm_ctx.last_pts_ns = -1;
1099 stream->webm_ctx.writer = NULL;
1100 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001101#endif
Johann87c40b32012-03-01 16:12:53 -08001102
John Koleszar6ad3b742012-11-06 12:02:42 -08001103 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001104 stream->webm_ctx.debug = global->debug;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001105
1106 /* Default lag_in_frames is 0 in realtime mode */
Yaowu Xuf883b422016-08-30 14:01:10 -07001107 if (global->deadline == AOM_DL_REALTIME)
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001108 stream->config.cfg.g_lag_in_frames = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001109 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001110
John Koleszar6ad3b742012-11-06 12:02:42 -08001111 /* Output files must be specified for each stream */
1112 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001113
John Koleszar6ad3b742012-11-06 12:02:42 -08001114 stream->next = NULL;
1115 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001116}
1117
Yaowu Xuf883b422016-08-30 14:01:10 -07001118static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001119 struct stream_state *stream, char **argv) {
1120 char **argi, **argj;
1121 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001122 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001123 static const int *ctrl_args_map = NULL;
1124 struct stream_config *config = &stream->config;
1125 int eos_mark_found = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001126#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -07001127 int test_16bit_internal = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001128#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001129
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001130 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001131 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001132#if CONFIG_AV1_ENCODER
1133 } else if (strcmp(global->codec->name, "av1") == 0) {
1134 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1135 // Consider to expand this set for AV1 encoder control.
1136 ctrl_args = av1_args;
1137 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001138#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001139 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001140
John Koleszarc6b90392012-07-13 15:21:29 -07001141 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001142 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001143
John Koleszar6ad3b742012-11-06 12:02:42 -08001144 /* Once we've found an end-of-stream marker (--) we want to continue
1145 * shifting arguments but not consuming them.
1146 */
1147 if (eos_mark_found) {
1148 argj++;
1149 continue;
1150 } else if (!strcmp(*argj, "--")) {
1151 eos_mark_found = 1;
1152 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001153 }
1154
Adrian Granged2401802015-02-13 14:51:32 -08001155 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001156 config->out_fn = arg.val;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001157 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001158 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001159#if CONFIG_FP_MB_STATS
1160 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1161 config->fpmb_stats_fn = arg.val;
1162#endif
Johannb50e5182015-01-30 15:05:14 -08001163 } else if (arg_match(&arg, &use_webm, argi)) {
1164#if CONFIG_WEBM_IO
1165 config->write_webm = 1;
1166#else
1167 die("Error: --webm specified but webm is disabled.");
1168#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001169 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001170 config->write_webm = 0;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001171 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001172 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001173 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001174 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001175 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001176 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001177 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001178 config->cfg.g_h = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -07001179#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001180 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1181 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1182 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1183 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1184#endif
James Zerndba73762014-05-10 17:44:12 -07001185#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001186 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001187 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001188#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001189 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001190 config->cfg.g_timebase = arg_parse_rational(&arg);
1191 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001192 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001193 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001194 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001195 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -07001196 if (global->deadline == AOM_DL_REALTIME &&
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001197 config->cfg.g_lag_in_frames != 0) {
1198 warn("non-zero %s option ignored in realtime mode.\n", arg.name);
1199 config->cfg.g_lag_in_frames = 0;
1200 }
1201 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001202 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001203 } else if (arg_match(&arg, &resize_allowed, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001204 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001205 } else if (arg_match(&arg, &resize_width, argi)) {
1206 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
1207 } else if (arg_match(&arg, &resize_height, argi)) {
1208 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001209 } else if (arg_match(&arg, &resize_up_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001210 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001211 } else if (arg_match(&arg, &resize_down_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001212 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001213 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001214 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001215 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001216 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001217 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001218 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001219 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001220 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001221 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001222 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001223 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001224 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001225 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001226 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001227 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001228 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001229 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001230 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001231 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001232 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001233 if (global->passes < 2)
1234 warn("option %s ignored in one-pass mode.\n", arg.name);
1235 } else if (arg_match(&arg, &minsection_pct, argi)) {
1236 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1237
1238 if (global->passes < 2)
1239 warn("option %s ignored in one-pass mode.\n", arg.name);
1240 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1241 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1242
1243 if (global->passes < 2)
1244 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001245 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001246 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001247 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001248 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001249 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001250 config->cfg.kf_mode = AOM_KF_DISABLED;
1251#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001252 } else if (arg_match(&arg, &test16bitinternalarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001253 if (strcmp(global->codec->name, "av1") == 0 ||
1254 strcmp(global->codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001255 test_16bit_internal = 1;
1256 }
1257#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001258 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001259 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001260 for (i = 0; ctrl_args[i]; i++) {
1261 if (arg_match(&arg, ctrl_args[i], argi)) {
1262 int j;
1263 match = 1;
1264
1265 /* Point either to the next free element or the first
1266 * instance of this control.
1267 */
1268 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001269 if (ctrl_args_map != NULL &&
1270 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001271 break;
1272
1273 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001274 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001275 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001276 config->arg_ctrls[j][0] = ctrl_args_map[i];
1277 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001278 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001279 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001280 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001281 }
clang-format6c4d83e2016-08-08 19:03:30 -07001282 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001283 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001284 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001285#if CONFIG_AOM_HIGHBITDEPTH
1286 if (strcmp(global->codec->name, "av1") == 0 ||
1287 strcmp(global->codec->name, "av1") == 0) {
clang-format6c4d83e2016-08-08 19:03:30 -07001288 config->use_16bit_internal =
1289 test_16bit_internal | (config->cfg.g_profile > 1);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001290 }
1291#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001292 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001293}
1294
clang-format6c4d83e2016-08-08 19:03:30 -07001295#define FOREACH_STREAM(func) \
1296 do { \
1297 struct stream_state *stream; \
Tom Finegan249366b2013-11-25 12:05:19 -08001298 for (stream = streams; stream; stream = stream->next) { \
clang-format6c4d83e2016-08-08 19:03:30 -07001299 func; \
1300 } \
Tom Finegan44dd3272013-11-20 17:18:28 -08001301 } while (0)
John Koleszar9e50ed72012-02-15 12:39:38 -08001302
Alex Conversed66bd222014-02-21 10:52:09 -08001303static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001304 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001305 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001306 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001307
John Koleszar6ad3b742012-11-06 12:02:42 -08001308 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001309 fatal(
1310 "Stream %d: Specify stream dimensions with --width (-w) "
1311 " and --height (-h)",
1312 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001313
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001314 // Check that the codec bit depth is greater than the input bit depth.
1315 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001316 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001317 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1318 stream->index, (int)stream->config.cfg.g_bit_depth,
1319 stream->config.cfg.g_input_bit_depth);
1320 }
1321
John Koleszar6ad3b742012-11-06 12:02:42 -08001322 for (streami = stream; streami; streami = streami->next) {
1323 /* All streams require output files */
1324 if (!streami->config.out_fn)
1325 fatal("Stream %d: Output file is required (specify with -o)",
1326 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001327
John Koleszar6ad3b742012-11-06 12:02:42 -08001328 /* Check for two streams outputting to the same file */
1329 if (streami != stream) {
1330 const char *a = stream->config.out_fn;
1331 const char *b = streami->config.out_fn;
1332 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1333 fatal("Stream %d: duplicate output file (from stream %d)",
1334 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001335 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001336
1337 /* Check for two streams sharing a stats file. */
1338 if (streami != stream) {
1339 const char *a = stream->config.stats_fn;
1340 const char *b = streami->config.stats_fn;
1341 if (a && b && !strcmp(a, b))
1342 fatal("Stream %d: duplicate stats file (from stream %d)",
1343 streami->index, stream->index);
1344 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001345
1346#if CONFIG_FP_MB_STATS
1347 /* Check for two streams sharing a mb stats file. */
1348 if (streami != stream) {
1349 const char *a = stream->config.fpmb_stats_fn;
1350 const char *b = streami->config.fpmb_stats_fn;
1351 if (a && b && !strcmp(a, b))
1352 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1353 streami->index, stream->index);
1354 }
1355#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001356 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001357}
1358
clang-format6c4d83e2016-08-08 19:03:30 -07001359static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001360 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001361 if (!stream->config.cfg.g_w) {
1362 if (!stream->config.cfg.g_h)
1363 stream->config.cfg.g_w = w;
1364 else
1365 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1366 }
1367 if (!stream->config.cfg.g_h) {
1368 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1369 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001370}
1371
clang-format6c4d83e2016-08-08 19:03:30 -07001372static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001373 switch (t) {
1374 case FILE_TYPE_RAW: return "RAW";
1375 case FILE_TYPE_Y4M: return "Y4M";
1376 default: return "Other";
1377 }
1378}
1379
Yaowu Xuf883b422016-08-30 14:01:10 -07001380static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001381 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001382 case AOM_IMG_FMT_I420: return "I420";
1383 case AOM_IMG_FMT_I422: return "I422";
1384 case AOM_IMG_FMT_I444: return "I444";
1385 case AOM_IMG_FMT_I440: return "I440";
1386 case AOM_IMG_FMT_YV12: return "YV12";
1387 case AOM_IMG_FMT_I42016: return "I42016";
1388 case AOM_IMG_FMT_I42216: return "I42216";
1389 case AOM_IMG_FMT_I44416: return "I44416";
1390 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001391 default: return "Other";
1392 }
1393}
Ralph Giles061a16d2012-01-05 15:05:05 -06001394
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001395static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001396 struct AvxEncoderConfig *global,
1397 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001398#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001399 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001400
John Koleszar6ad3b742012-11-06 12:02:42 -08001401 if (stream->index == 0) {
1402 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001403 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001404 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001405 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001406 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001407 }
1408 if (stream->next || stream->index)
1409 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1410 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1411 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001412
John Koleszar6ad3b742012-11-06 12:02:42 -08001413 SHOW(g_usage);
1414 SHOW(g_threads);
1415 SHOW(g_profile);
1416 SHOW(g_w);
1417 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001418 SHOW(g_bit_depth);
1419 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001420 SHOW(g_timebase.num);
1421 SHOW(g_timebase.den);
1422 SHOW(g_error_resilient);
1423 SHOW(g_pass);
1424 SHOW(g_lag_in_frames);
1425 SHOW(rc_dropframe_thresh);
1426 SHOW(rc_resize_allowed);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001427 SHOW(rc_scaled_width);
1428 SHOW(rc_scaled_height);
John Koleszar6ad3b742012-11-06 12:02:42 -08001429 SHOW(rc_resize_up_thresh);
1430 SHOW(rc_resize_down_thresh);
1431 SHOW(rc_end_usage);
1432 SHOW(rc_target_bitrate);
1433 SHOW(rc_min_quantizer);
1434 SHOW(rc_max_quantizer);
1435 SHOW(rc_undershoot_pct);
1436 SHOW(rc_overshoot_pct);
1437 SHOW(rc_buf_sz);
1438 SHOW(rc_buf_initial_sz);
1439 SHOW(rc_buf_optimal_sz);
1440 SHOW(rc_2pass_vbr_bias_pct);
1441 SHOW(rc_2pass_vbr_minsection_pct);
1442 SHOW(rc_2pass_vbr_maxsection_pct);
1443 SHOW(kf_mode);
1444 SHOW(kf_min_dist);
1445 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001446}
1447
John Koleszar9e50ed72012-02-15 12:39:38 -08001448static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001449 struct AvxEncoderConfig *global,
1450 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001451 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001452 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001453
Yaowu Xuf883b422016-08-30 14:01:10 -07001454 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001455
John Koleszar6ad3b742012-11-06 12:02:42 -08001456 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001457
clang-format6c4d83e2016-08-08 19:03:30 -07001458 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001459
John Koleszar6ad3b742012-11-06 12:02:42 -08001460 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1461 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001462
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001463#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001464 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001465 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001466 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1467 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001468 }
James Zernc8e5a772016-02-11 18:53:50 -08001469#else
1470 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001471#endif
1472
1473 if (!stream->config.write_webm) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001474 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1475 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001476}
1477
John Koleszar9e50ed72012-02-15 12:39:38 -08001478static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001479 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001480 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001481
Yaowu Xuf883b422016-08-30 14:01:10 -07001482 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001483
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001484#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001485 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001486 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001487 }
1488#endif
1489
1490 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001491 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001492 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001493 stream->frames_out);
1494 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001495
John Koleszar6ad3b742012-11-06 12:02:42 -08001496 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001497}
1498
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001499static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001500 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001501 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001502 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001503 fatal("Failed to open statistics store");
1504 } else {
1505 if (!stats_open_mem(&stream->stats, pass))
1506 fatal("Failed to open statistics store");
1507 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001508
Pengchong Jinf349b072014-07-14 09:13:38 -07001509#if CONFIG_FP_MB_STATS
1510 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001511 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1512 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001513 fatal("Failed to open mb statistics store");
1514 } else {
1515 if (!stats_open_mem(&stream->fpmb_stats, pass))
1516 fatal("Failed to open mb statistics store");
1517 }
1518#endif
1519
John Koleszar6ad3b742012-11-06 12:02:42 -08001520 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001521 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1522 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001523 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001524 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001525#if CONFIG_FP_MB_STATS
1526 stream->config.cfg.rc_firstpass_mb_stats_in =
1527 stats_get(&stream->fpmb_stats);
1528#endif
1529 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001530
John Koleszar6ad3b742012-11-06 12:02:42 -08001531 stream->cx_time = 0;
1532 stream->nbytes = 0;
1533 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001534}
1535
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001536static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001537 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001538 int i;
1539 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001540
Yaowu Xuf883b422016-08-30 14:01:10 -07001541 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1542 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
1543#if CONFIG_AOM_HIGHBITDEPTH
1544 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001545#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001546
John Koleszar6ad3b742012-11-06 12:02:42 -08001547 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001548 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001549 &stream->config.cfg, flags);
1550 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001551
Yaowu Xuf883b422016-08-30 14:01:10 -07001552 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001553 * we're being clever to store the control IDs in an array. Real
1554 * applications will want to make use of the enumerations directly
1555 */
1556 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1557 int ctrl = stream->config.arg_ctrls[i][0];
1558 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001559 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001560 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001561
John Koleszar6ad3b742012-11-06 12:02:42 -08001562 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1563 }
1564
John Koleszar5ebe94f2012-12-23 07:20:10 -08001565#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001566 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001567 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1568 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
1569 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001570
Yaowu Xuf883b422016-08-30 14:01:10 -07001571#if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
1572 if (strcmp(global->codec->name, "av1") == 0) {
1573 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001574 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1575
Yaowu Xuf883b422016-08-30 14:01:10 -07001576 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001577 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1578 }
1579#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001580 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001581#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001582}
1583
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001584static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001585 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001586 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001587 aom_codec_pts_t frame_start, next_frame_start;
1588 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1589 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001590
clang-format6c4d83e2016-08-08 19:03:30 -07001591 frame_start =
1592 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1593 cfg->g_timebase.num / global->framerate.num;
1594 next_frame_start =
1595 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1596 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001597
clang-format6c4d83e2016-08-08 19:03:30 -07001598/* Scale if necessary */
Yaowu Xuf883b422016-08-30 14:01:10 -07001599#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001600 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001601 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001602 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001603 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001604 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1605 exit(EXIT_FAILURE);
1606 }
1607#if CONFIG_LIBYUV
1608 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001609 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001610 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001611 }
clang-format6c4d83e2016-08-08 19:03:30 -07001612 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001613 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1614 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1615 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1616 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1617 stream->img->stride[AOM_PLANE_Y] / 2,
1618 (uint16 *)stream->img->planes[AOM_PLANE_U],
1619 stream->img->stride[AOM_PLANE_U] / 2,
1620 (uint16 *)stream->img->planes[AOM_PLANE_V],
1621 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001622 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001623 img = stream->img;
1624#else
clang-format6c4d83e2016-08-08 19:03:30 -07001625 stream->encoder.err = 1;
1626 ctx_exit_on_error(&stream->encoder,
1627 "Stream %d: Failed to encode frame.\n"
1628 "Scaling disabled in this configuration. \n"
1629 "To enable, configure with --enable-libyuv\n",
1630 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001631#endif
1632 }
1633 }
1634#endif
John Koleszar34882b92012-03-01 12:50:40 -08001635 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001636 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001637 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1638 exit(EXIT_FAILURE);
1639 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001640#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001641 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001642 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001643 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001644 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001645 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1646 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1647 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1648 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1649 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1650 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001651 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001652 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001653#else
1654 stream->encoder.err = 1;
1655 ctx_exit_on_error(&stream->encoder,
1656 "Stream %d: Failed to encode frame.\n"
1657 "Scaling disabled in this configuration. \n"
1658 "To enable, configure with --enable-libyuv\n",
1659 stream->index);
1660#endif
John Koleszar34882b92012-03-01 12:50:40 -08001661 }
1662
Yaowu Xuf883b422016-08-30 14:01:10 -07001663 aom_usec_timer_start(&timer);
1664 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001665 (unsigned long)(next_frame_start - frame_start), 0,
1666 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001667 aom_usec_timer_mark(&timer);
1668 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001669 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1670 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001671}
1672
John Koleszar6ad3b742012-11-06 12:02:42 -08001673static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001674 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001675 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001676
Yaowu Xuf883b422016-08-30 14:01:10 -07001677 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001678 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1679 stream->counts[q]++;
1680 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001681}
1682
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001683static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001684 struct AvxEncoderConfig *global, int *got_data) {
1685 const aom_codec_cx_pkt_t *pkt;
1686 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1687 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001688
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001689 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001690 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001691 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001692 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001693
John Koleszar6ad3b742012-11-06 12:02:42 -08001694 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001695 case AOM_CODEC_CX_FRAME_PKT:
1696 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001697 stream->frames_out++;
1698 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001699 if (!global->quiet)
1700 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001701
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001702 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001703#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001704 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001705 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001706 }
1707#endif
1708 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001709 if (pkt->data.frame.partition_id <= 0) {
1710 ivf_header_pos = ftello(stream->file);
1711 fsize = pkt->data.frame.sz;
1712
Dmitry Kovalev373b0f92014-01-29 17:57:21 -08001713 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001714 } else {
1715 fsize += pkt->data.frame.sz;
1716
Yaowu Xuf883b422016-08-30 14:01:10 -07001717 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
James Zernb6430362017-01-14 14:18:01 -08001718 const FileOffset currpos = ftello(stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001719 fseeko(stream->file, ivf_header_pos, SEEK_SET);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001720 ivf_write_frame_size(stream->file, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001721 fseeko(stream->file, currpos, SEEK_SET);
1722 }
1723 }
1724
clang-format6c4d83e2016-08-08 19:03:30 -07001725 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1726 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001727 }
1728 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001729
John Koleszar9e50ed72012-02-15 12:39:38 -08001730 *got_data = 1;
John Koleszar5ebe94f2012-12-23 07:20:10 -08001731#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001732 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001733 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001734 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001735 if (stream->decoder.err) {
1736 warn_or_exit_on_error(&stream->decoder,
1737 global->test_decode == TEST_DECODE_FATAL,
1738 "Failed to decode frame %d in stream %d",
1739 stream->frames_out + 1, stream->index);
1740 stream->mismatch_seen = stream->frames_out + 1;
1741 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001742 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001743#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001744 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001745 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001746 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001747 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001748 pkt->data.twopass_stats.sz);
1749 stream->nbytes += pkt->data.raw.sz;
1750 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001751#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001752 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001753 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001754 pkt->data.firstpass_mb_stats.sz);
1755 stream->nbytes += pkt->data.raw.sz;
1756 break;
1757#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001758 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001759
1760 if (global->show_psnr) {
1761 int i;
1762
1763 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1764 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1765 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001766 if (!global->quiet)
1767 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001768 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1769 }
1770 stream->psnr_count++;
1771 }
1772
1773 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001774 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001775 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001776 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001777}
1778
clang-format6c4d83e2016-08-08 19:03:30 -07001779static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001780 int i;
1781 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001782
clang-format6c4d83e2016-08-08 19:03:30 -07001783 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001784
John Koleszar6ad3b742012-11-06 12:02:42 -08001785 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001786 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001787 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001788 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001789
John Koleszar6ad3b742012-11-06 12:02:42 -08001790 for (i = 0; i < 4; i++) {
1791 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1792 }
1793 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001794}
1795
John Koleszar25b6e9f2013-02-12 21:17:56 -08001796static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001797 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001798}
1799
clang-format6c4d83e2016-08-08 19:03:30 -07001800static void test_decode(struct stream_state *stream,
John Koleszarb3c350a2013-03-13 12:15:43 -07001801 enum TestDecodeFatality fatal,
Yaowu Xuf883b422016-08-30 14:01:10 -07001802 const AvxInterface *codec) {
1803 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001804
clang-format6c4d83e2016-08-08 19:03:30 -07001805 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001806
John Koleszarb3c350a2013-03-13 12:15:43 -07001807 /* Get the internal reference frame */
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001808 if (strcmp(codec->name, "vp8") == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001809 struct aom_ref_frame ref_enc, ref_dec;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001810 const unsigned int frame_width = (stream->config.cfg.g_w + 15) & ~15;
1811 const unsigned int frame_height = (stream->config.cfg.g_h + 15) & ~15;
1812 aom_img_alloc(&ref_enc.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001813 enc_img = ref_enc.img;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001814 aom_img_alloc(&ref_dec.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001815 dec_img = ref_dec.img;
1816
Yaowu Xuf883b422016-08-30 14:01:10 -07001817 ref_enc.frame_type = AOM_LAST_FRAME;
1818 ref_dec.frame_type = AOM_LAST_FRAME;
1819 aom_codec_control(&stream->encoder, AOM_COPY_REFERENCE, &ref_enc);
1820 aom_codec_control(&stream->decoder, AOM_COPY_REFERENCE, &ref_dec);
John Koleszarb3c350a2013-03-13 12:15:43 -07001821 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001822 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1823 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001824
Yaowu Xuf883b422016-08-30 14:01:10 -07001825#if CONFIG_AOM_HIGHBITDEPTH
1826 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1827 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1828 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1829 aom_image_t enc_hbd_img;
1830 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001831 enc_img.d_w, enc_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001832 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001833 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001834 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001835 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1836 aom_image_t dec_hbd_img;
1837 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001838 dec_img.d_w, dec_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001839 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001840 dec_img = dec_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001841 }
1842 }
1843#endif
John Koleszarb3c350a2013-03-13 12:15:43 -07001844 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001845 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001846 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001847
John Koleszarb3c350a2013-03-13 12:15:43 -07001848 if (!compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001849 int y[4], u[4], v[4];
Yaowu Xuf883b422016-08-30 14:01:10 -07001850#if CONFIG_AOM_HIGHBITDEPTH
1851 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001852 find_mismatch_high(&enc_img, &dec_img, y, u, v);
1853 } else {
1854 find_mismatch(&enc_img, &dec_img, y, u, v);
1855 }
1856#else
John Koleszarb3c350a2013-03-13 12:15:43 -07001857 find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001858#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001859 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001860 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001861 "Stream %d: Encode/decode mismatch on frame %d at"
1862 " Y[%d, %d] {%d/%d},"
1863 " U[%d, %d] {%d/%d},"
1864 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001865 stream->index, stream->frames_out, y[0], y[1], y[2],
1866 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 -08001867 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001868 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001869
Yaowu Xuf883b422016-08-30 14:01:10 -07001870 aom_img_free(&enc_img);
1871 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001872}
John Koleszarefd54f82012-02-13 16:52:18 -08001873
John Koleszar25b6e9f2013-02-12 21:17:56 -08001874static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001875 int64_t hours;
1876 int64_t mins;
1877 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001878
1879 if (etl >= 0) {
1880 hours = etl / 3600;
1881 etl -= hours * 3600;
1882 mins = etl / 60;
1883 etl -= mins * 60;
1884 secs = etl;
1885
clang-format6c4d83e2016-08-08 19:03:30 -07001886 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1887 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001888 } else {
1889 fprintf(stderr, "[%3s unknown] ", label);
1890 }
1891}
1892
Tom Finegan44dd3272013-11-20 17:18:28 -08001893int main(int argc, const char **argv_) {
1894 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001895 aom_image_t raw;
1896#if CONFIG_AOM_HIGHBITDEPTH
1897 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001898 int allocated_raw_shift = 0;
1899 int use_16bit_internal = 0;
1900 int input_shift = 0;
1901#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001902 int frame_avail, got_data;
1903
Yaowu Xuf883b422016-08-30 14:01:10 -07001904 struct AvxInputContext input;
1905 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001906 struct stream_state *streams = NULL;
1907 char **argv, **argi;
1908 uint64_t cx_time = 0;
1909 int stream_cnt = 0;
1910 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001911 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001912
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001913 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001914 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001915
clang-format6c4d83e2016-08-08 19:03:30 -07001916 if (argc < 3) usage_exit();
John Koleszar6ad3b742012-11-06 12:02:42 -08001917
1918 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001919 input.framerate.numerator = 30;
1920 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001921 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001922 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001923
1924 /* First parse the global configuration values, because we want to apply
1925 * other parameters on top of the default configuration provided by the
1926 * codec.
1927 */
1928 argv = argv_dup(argc - 1, argv_ + 1);
1929 parse_global_config(&global, argv);
1930
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001931 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001932 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1933 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1934 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1935 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1936 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001937 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001938
John Koleszar6ad3b742012-11-06 12:02:42 -08001939 {
1940 /* Now parse each stream's parameters. Using a local scope here
1941 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1942 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001943 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001944 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001945
John Koleszar6ad3b742012-11-06 12:02:42 -08001946 do {
1947 stream = new_stream(&global, stream);
1948 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001949 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001950 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001951 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001952
John Koleszarc6b90392012-07-13 15:21:29 -07001953 /* Check for unrecognized options */
1954 for (argi = argv; *argi; argi++)
1955 if (argi[0][0] == '-' && argi[0][1])
1956 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001957
clang-format6c4d83e2016-08-08 19:03:30 -07001958 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1959 &stream->config.cfg););
Tom Finegan249366b2013-11-25 12:05:19 -08001960
John Koleszarc6b90392012-07-13 15:21:29 -07001961 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001962 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001963
clang-format6c4d83e2016-08-08 19:03:30 -07001964 if (!input.filename) usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04001965
John Koleszar8dd82872013-05-06 11:01:35 -07001966 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001967 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001968
John Koleszar6ad3b742012-11-06 12:02:42 -08001969 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001970 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001971 int64_t estimated_time_left = -1;
1972 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001973 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001974
John Koleszar6ad3b742012-11-06 12:02:42 -08001975 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001976
John Koleszar6ad3b742012-11-06 12:02:42 -08001977 /* If the input file doesn't specify its w/h (raw files), try to get
1978 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001979 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001980 if (!input.width || !input.height) {
1981 FOREACH_STREAM({
1982 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1983 input.width = stream->config.cfg.g_w;
1984 input.height = stream->config.cfg.g_h;
1985 break;
1986 }
1987 });
1988 }
John Koleszarc6b90392012-07-13 15:21:29 -07001989
John Koleszar6ad3b742012-11-06 12:02:42 -08001990 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001991 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001992 fatal(
1993 "Specify stream dimensions with --width (-w) "
1994 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001995
1996 /* If input file does not specify bit-depth but input-bit-depth parameter
1997 * exists, assume that to be the input bit-depth. However, if the
1998 * input-bit-depth paramter does not exist, assume the input bit-depth
1999 * to be the same as the codec bit-depth.
2000 */
2001 if (!input.bit_depth) {
2002 FOREACH_STREAM({
2003 if (stream->config.cfg.g_input_bit_depth)
2004 input.bit_depth = stream->config.cfg.g_input_bit_depth;
2005 else
2006 input.bit_depth = stream->config.cfg.g_input_bit_depth =
2007 (int)stream->config.cfg.g_bit_depth;
2008 });
Yaowu Xuf883b422016-08-30 14:01:10 -07002009 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002010 } else {
clang-format6c4d83e2016-08-08 19:03:30 -07002011 FOREACH_STREAM(
2012 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002013 }
2014
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002015#if CONFIG_AOM_HIGHBITDEPTH
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002016 FOREACH_STREAM({
Thomas Daede8ec53b22016-09-19 13:24:51 -07002017 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2018 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2019 was selected. */
2020 switch (stream->config.cfg.g_profile) {
2021 case 0:
2022 stream->config.cfg.g_profile = 1;
2023 profile_updated = 1;
2024 break;
2025 case 2:
2026 stream->config.cfg.g_profile = 3;
2027 profile_updated = 1;
2028 break;
2029 default: break;
2030 }
2031 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002032 /* Automatically set the codec bit depth to match the input bit depth.
2033 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002034 if (stream->config.cfg.g_input_bit_depth >
2035 (unsigned int)stream->config.cfg.g_bit_depth) {
2036 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2037 }
2038 if (stream->config.cfg.g_bit_depth > 8) {
2039 switch (stream->config.cfg.g_profile) {
2040 case 0:
2041 stream->config.cfg.g_profile = 2;
2042 profile_updated = 1;
2043 break;
2044 case 1:
2045 stream->config.cfg.g_profile = 3;
2046 profile_updated = 1;
2047 break;
2048 default: break;
2049 }
2050 }
2051 if (stream->config.cfg.g_profile > 1) {
2052 stream->config.use_16bit_internal = 1;
2053 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002054 if (profile_updated && !global.quiet) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002055 fprintf(stderr,
2056 "Warning: automatically upgrading to profile %d to "
2057 "match input format.\n",
2058 stream->config.cfg.g_profile);
2059 }
2060 });
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002061#else
2062 FOREACH_STREAM({
2063 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2064 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2065 was selected. */
2066 switch (stream->config.cfg.g_profile) {
2067 case 0:
2068 stream->config.cfg.g_profile = 1;
2069 profile_updated = 1;
2070 break;
2071 case 2:
2072 stream->config.cfg.g_profile = 3;
2073 profile_updated = 1;
2074 break;
2075 default: break;
2076 }
2077 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002078 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002079 fprintf(stderr,
2080 "Warning: automatically upgrading to profile %d to "
2081 "match input format.\n",
2082 stream->config.cfg.g_profile);
2083 }
2084 });
2085#endif
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002086
Tom Finegan00a35aa2013-11-14 12:37:42 -08002087 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
Alex Conversed66bd222014-02-21 10:52:09 -08002088 FOREACH_STREAM(validate_stream_config(stream, &global));
John Koleszar77e6b452010-11-03 13:58:40 -04002089
John Koleszar6ad3b742012-11-06 12:02:42 -08002090 /* Ensure that --passes and --pass are consistent. If --pass is set and
2091 * --passes=2, ensure --fpf was set.
2092 */
2093 if (global.pass && global.passes == 2)
clang-format6c4d83e2016-08-08 19:03:30 -07002094 FOREACH_STREAM({
2095 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002096 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002097 " and --passes=2\n",
2098 stream->index, global.pass);
2099 });
John Koleszar3245d462010-06-10 17:10:12 -04002100
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002101#if !CONFIG_WEBM_IO
2102 FOREACH_STREAM({
Ronald S. Bultje39775072015-12-16 13:35:43 -05002103 if (stream->config.write_webm) {
2104 stream->config.write_webm = 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002105 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002106 "aomenc was compiled without WebM container support."
clang-format6c4d83e2016-08-08 19:03:30 -07002107 "Producing IVF output");
Ronald S. Bultje39775072015-12-16 13:35:43 -05002108 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002109 });
2110#endif
2111
John Koleszar6ad3b742012-11-06 12:02:42 -08002112 /* Use the frame rate from the file only if none was specified
2113 * on the command-line.
2114 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002115 if (!global.have_framerate) {
2116 global.framerate.num = input.framerate.numerator;
2117 global.framerate.den = input.framerate.denominator;
Ronald S. Bultje9134e9f2016-01-18 14:03:45 -05002118 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
2119 stream->config.cfg.g_timebase.num = global.framerate.den);
Tom Finegan00a35aa2013-11-14 12:37:42 -08002120 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002121
John Koleszar6ad3b742012-11-06 12:02:42 -08002122 /* Show configuration */
2123 if (global.verbose && pass == 0)
2124 FOREACH_STREAM(show_stream_config(stream, &global, &input));
2125
2126 if (pass == (global.pass ? global.pass - 1 : 0)) {
2127 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002128 /*The Y4M reader does its own allocation.
2129 Just initialize this here to avoid problems if we never read any
2130 frames.*/
2131 memset(&raw, 0, sizeof(raw));
2132 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002133 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002134
clang-format6c4d83e2016-08-08 19:03:30 -07002135 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
2136 &stream->config.cfg, &global.framerate));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002137 }
2138
John Koleszar6ad3b742012-11-06 12:02:42 -08002139 FOREACH_STREAM(setup_pass(stream, &global, pass));
clang-format6c4d83e2016-08-08 19:03:30 -07002140 FOREACH_STREAM(
2141 open_output_file(stream, &global, &input.pixel_aspect_ratio));
John Koleszar6ad3b742012-11-06 12:02:42 -08002142 FOREACH_STREAM(initialize_encoder(stream, &global));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002143
Yaowu Xuf883b422016-08-30 14:01:10 -07002144#if CONFIG_AOM_HIGHBITDEPTH
2145 if (strcmp(global.codec->name, "av1") == 0 ||
2146 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002147 // Check to see if at least one stream uses 16 bit internal.
2148 // Currently assume that the bit_depths for all streams using
2149 // highbitdepth are the same.
2150 FOREACH_STREAM({
2151 if (stream->config.use_16bit_internal) {
2152 use_16bit_internal = 1;
2153 }
2154 if (stream->config.cfg.g_profile == 0) {
2155 input_shift = 0;
2156 } else {
2157 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07002158 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002159 }
2160 });
2161 }
2162#endif
2163
John Koleszarc6b90392012-07-13 15:21:29 -07002164 frame_avail = 1;
2165 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002166
John Koleszarc6b90392012-07-13 15:21:29 -07002167 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002168 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002169
John Koleszar6ad3b742012-11-06 12:02:42 -08002170 if (!global.limit || frames_in < global.limit) {
2171 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002172
clang-format6c4d83e2016-08-08 19:03:30 -07002173 if (frame_avail) frames_in++;
2174 seen_frames =
2175 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002176
John Koleszar6ad3b742012-11-06 12:02:42 -08002177 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002178 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002179 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2180
John Koleszar6ad3b742012-11-06 12:02:42 -08002181 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002182 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2183 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002184 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002185 fprintf(stderr, "frame %4d ", frames_in);
2186
clang-format6c4d83e2016-08-08 19:03:30 -07002187 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002188 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002189 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002190 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002191 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002192 }
2193
hui su251e1512016-10-03 15:48:43 -07002194 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002195 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002196 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002197
John Koleszar6ad3b742012-11-06 12:02:42 -08002198 if (frames_in > global.skip_frames) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002199#if CONFIG_AOM_HIGHBITDEPTH
2200 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002201 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2202 assert(use_16bit_internal);
2203 // Input bit depth and stream bit depth do not match, so up
2204 // shift frame to stream bit depth
2205 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002206 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002207 input.width, input.height, 32);
2208 allocated_raw_shift = 1;
2209 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002210 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002211 frame_to_encode = &raw_shift;
2212 } else {
2213 frame_to_encode = &raw;
2214 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002215 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002216 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002217 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002218 FOREACH_STREAM({
2219 if (stream->config.use_16bit_internal)
2220 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002221 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002222 else
2223 assert(0);
2224 });
2225 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002226 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002227 FOREACH_STREAM(encode_frame(stream, &global,
2228 frame_avail ? frame_to_encode : NULL,
2229 frames_in));
2230 }
2231#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002232 aom_usec_timer_start(&timer);
clang-format6c4d83e2016-08-08 19:03:30 -07002233 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
John Koleszar6ad3b742012-11-06 12:02:42 -08002234 frames_in));
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002235#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002236 aom_usec_timer_mark(&timer);
2237 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002238
John Koleszar6ad3b742012-11-06 12:02:42 -08002239 FOREACH_STREAM(update_quantizer_histogram(stream));
John Koleszarc6b90392012-07-13 15:21:29 -07002240
John Koleszar0ea50ce2010-05-18 11:58:33 -04002241 got_data = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002242 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002243
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002244 if (!got_data && input.length && streams != NULL &&
2245 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002246 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002247 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002248 int64_t remaining;
2249 int64_t rate;
2250
2251 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002252 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002253
2254 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002255 remaining = 1000 * (global.limit - global.skip_frames -
2256 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002257 } else {
James Zerne3578af2014-04-17 10:47:08 -07002258 const int64_t input_pos = ftello(input.file);
2259 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002260 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002261
2262 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002263 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002264 }
2265
clang-format6c4d83e2016-08-08 19:03:30 -07002266 average_rate =
2267 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002268 estimated_time_left = average_rate ? remaining / average_rate : -1;
2269 }
2270
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002271 if (got_data && global.test_decode != TEST_DECODE_OFF)
John Koleszarb3c350a2013-03-13 12:15:43 -07002272 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
John Koleszarc6b90392012-07-13 15:21:29 -07002273 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002274
John Koleszarc6b90392012-07-13 15:21:29 -07002275 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002276 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002277 }
2278
clang-format6c4d83e2016-08-08 19:03:30 -07002279 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002280
Deb Mukherjeea160d722014-09-30 21:56:33 -07002281 if (!global.quiet) {
clang-format6c4d83e2016-08-08 19:03:30 -07002282 FOREACH_STREAM(fprintf(
2283 stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2284 "b/f %7" PRId64 "b/s"
2285 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2286 pass + 1, global.passes, frames_in, stream->frames_out,
2287 (int64_t)stream->nbytes,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002288 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
clang-format6c4d83e2016-08-08 19:03:30 -07002289 seen_frames
2290 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2291 global.framerate.den / seen_frames
2292 : 0,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002293 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2294 stream->cx_time > 9999999 ? "ms" : "us",
2295 usec_to_fps(stream->cx_time, seen_frames)));
2296 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002297
Deb Mukherjeea160d722014-09-30 21:56:33 -07002298 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002299 if (global.codec->fourcc == AV1_FOURCC) {
Deb Mukherjeea160d722014-09-30 21:56:33 -07002300 FOREACH_STREAM(
2301 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2302 } else {
2303 FOREACH_STREAM(show_psnr(stream, 255.0));
2304 }
2305 }
John Koleszarc6b90392012-07-13 15:21:29 -07002306
Yaowu Xuf883b422016-08-30 14:01:10 -07002307 FOREACH_STREAM(aom_codec_destroy(&stream->encoder));
John Koleszar6ad3b742012-11-06 12:02:42 -08002308
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002309 if (global.test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002310 FOREACH_STREAM(aom_codec_destroy(&stream->decoder));
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002311 }
2312
John Koleszar6ad3b742012-11-06 12:02:42 -08002313 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002314
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002315 if (global.test_decode == TEST_DECODE_FATAL) {
2316 FOREACH_STREAM(res |= stream->mismatch_seen);
2317 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002318 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002319
John Koleszar6ad3b742012-11-06 12:02:42 -08002320 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
John Koleszarc6b90392012-07-13 15:21:29 -07002321
Pengchong Jinf349b072014-07-14 09:13:38 -07002322#if CONFIG_FP_MB_STATS
2323 FOREACH_STREAM(stats_close(&stream->fpmb_stats, global.passes - 1));
2324#endif
2325
clang-format6c4d83e2016-08-08 19:03:30 -07002326 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002327 }
2328
John Koleszar6ad3b742012-11-06 12:02:42 -08002329 if (global.show_q_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002330 FOREACH_STREAM(
2331 show_q_histogram(stream->counts, global.show_q_hist_buckets));
John Koleszar6ad3b742012-11-06 12:02:42 -08002332
2333 if (global.show_rate_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002334 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
John Koleszar6ad3b742012-11-06 12:02:42 -08002335 global.show_rate_hist_buckets));
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08002336 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
John Koleszar6ad3b742012-11-06 12:02:42 -08002337
Ronald S. Bultje31214972012-10-11 10:13:48 -07002338#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002339 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2340 * to match some existing utilities.
2341 */
Yaowu Xu47e784e2013-10-16 16:29:28 -07002342 if (!(global.pass == 1 && global.passes == 2))
2343 FOREACH_STREAM({
2344 FILE *f = fopen("opsnr.stt", "a");
2345 if (stream->mismatch_seen) {
2346 fprintf(f, "First mismatch occurred in frame %d\n",
2347 stream->mismatch_seen);
2348 } else {
2349 fprintf(f, "No mismatch detected in recon buffers\n");
2350 }
2351 fclose(f);
2352 });
Ronald S. Bultje31214972012-10-11 10:13:48 -07002353#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002354
Yaowu Xuf883b422016-08-30 14:01:10 -07002355#if CONFIG_AOM_HIGHBITDEPTH
2356 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002357#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002358 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002359 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002360 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002361 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002362}