blob: e32a922d05f9d6441784315af9164d3ca5d85223 [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)");
158static const arg_def_t best_dl =
159 ARG_DEF(NULL, "best", 0, "Use Best Quality Deadline");
160static const arg_def_t good_dl =
161 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
162static const arg_def_t rt_dl =
163 ARG_DEF(NULL, "rt", 0, "Use Realtime Quality Deadline");
164static const arg_def_t quietarg =
165 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
166static const arg_def_t verbosearg =
167 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
168static const arg_def_t psnrarg =
169 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800170
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800171static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700172 { "off", TEST_DECODE_OFF },
173 { "fatal", TEST_DECODE_FATAL },
174 { "warn", TEST_DECODE_WARN },
175 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800176};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700177static const arg_def_t recontest = ARG_DEF_ENUM(
178 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700179static const arg_def_t framerate =
180 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
181static const arg_def_t use_webm =
182 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
183static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
184static const arg_def_t out_part =
185 ARG_DEF("P", "output-partitions", 0,
186 "Makes encoder output partitions. Requires IVF output!");
187static const arg_def_t q_hist_n =
188 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
189static const arg_def_t rate_hist_n =
190 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
191static const arg_def_t disable_warnings =
192 ARG_DEF(NULL, "disable-warnings", 0,
193 "Disable warnings about potentially incorrect encode settings.");
194static const arg_def_t disable_warning_prompt =
195 ARG_DEF("y", "disable-warning-prompt", 0,
196 "Display warnings, but do not prompt user to continue.");
Alex Conversed66bd222014-02-21 10:52:09 -0800197
Yaowu Xuf883b422016-08-30 14:01:10 -0700198#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700199static const arg_def_t test16bitinternalarg = ARG_DEF(
200 NULL, "test-16bit-internal", 0, "Force use of 16 bit internal buffer");
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400201
202static const struct arg_enum_list bitdepth_enum[] = {
203 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
204};
205
206static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
207 "b", "bit-depth", 1,
208 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
209 bitdepth_enum);
210static const arg_def_t inbitdeptharg =
211 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700212#endif
Tom Finegan44dd3272013-11-20 17:18:28 -0800213
clang-format6c4d83e2016-08-08 19:03:30 -0700214static const arg_def_t *main_args[] = { &debugmode,
215 &outputfile,
216 &codecarg,
217 &passes,
218 &pass_arg,
219 &fpf_name,
220 &limit,
221 &skip,
222 &deadline,
223 &best_dl,
224 &good_dl,
225 &rt_dl,
226 &quietarg,
227 &verbosearg,
228 &psnrarg,
229 &use_webm,
230 &use_ivf,
231 &out_part,
232 &q_hist_n,
233 &rate_hist_n,
234 &disable_warnings,
235 &disable_warning_prompt,
236 &recontest,
237 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400238
clang-format6c4d83e2016-08-08 19:03:30 -0700239static const arg_def_t usage =
240 ARG_DEF("u", "usage", 1, "Usage profile number to use");
241static const arg_def_t threads =
242 ARG_DEF("t", "threads", 1, "Max number of threads to use");
243static const arg_def_t profile =
244 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700245static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
246static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700247#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700248static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700249 { "mono", STEREO_FORMAT_MONO },
250 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
251 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
252 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
253 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
254 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700255};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700256static const arg_def_t stereo_mode = ARG_DEF_ENUM(
257 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700258#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700259static const arg_def_t timebase = ARG_DEF(
260 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700261static const arg_def_t error_resilient =
262 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
263static const arg_def_t lag_in_frames =
264 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400265
clang-format6c4d83e2016-08-08 19:03:30 -0700266static const arg_def_t *global_args[] = { &use_yv12,
267 &use_i420,
268 &use_i422,
269 &use_i444,
270 &use_i440,
271 &usage,
272 &threads,
273 &profile,
274 &width,
275 &height,
James Zerndba73762014-05-10 17:44:12 -0700276#if CONFIG_WEBM_IO
clang-format6c4d83e2016-08-08 19:03:30 -0700277 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700278#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700279 &timebase,
280 &framerate,
281 &error_resilient,
Yaowu Xuf883b422016-08-30 14:01:10 -0700282#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700283 &test16bitinternalarg,
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400284 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700285#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700286 &lag_in_frames,
287 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400288
clang-format6c4d83e2016-08-08 19:03:30 -0700289static const arg_def_t dropframe_thresh =
290 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
291static const arg_def_t resize_allowed =
292 ARG_DEF(NULL, "resize-allowed", 1, "Spatial resampling enabled (bool)");
293static const arg_def_t resize_width =
294 ARG_DEF(NULL, "resize-width", 1, "Width of encoded frame");
295static const arg_def_t resize_height =
296 ARG_DEF(NULL, "resize-height", 1, "Height of encoded frame");
297static const arg_def_t resize_up_thresh =
298 ARG_DEF(NULL, "resize-up", 1, "Upscale threshold (buf %)");
299static const arg_def_t resize_down_thresh =
300 ARG_DEF(NULL, "resize-down", 1, "Downscale threshold (buf %)");
Yaowu Xuf883b422016-08-30 14:01:10 -0700301static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
302 { "cbr", AOM_CBR },
303 { "cq", AOM_CQ },
304 { "q", AOM_Q },
clang-format6c4d83e2016-08-08 19:03:30 -0700305 { NULL, 0 } };
306static const arg_def_t end_usage =
307 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
308static const arg_def_t target_bitrate =
309 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
310static const arg_def_t min_quantizer =
311 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
312static const arg_def_t max_quantizer =
313 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
314static const arg_def_t undershoot_pct =
315 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
316static const arg_def_t overshoot_pct =
317 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
318static const arg_def_t buf_sz =
319 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
320static const arg_def_t buf_initial_sz =
321 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
322static const arg_def_t buf_optimal_sz =
323 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
John Koleszarc6b90392012-07-13 15:21:29 -0700324static const arg_def_t *rc_args[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700325 &dropframe_thresh, &resize_allowed, &resize_width, &resize_height,
326 &resize_up_thresh, &resize_down_thresh, &end_usage, &target_bitrate,
327 &min_quantizer, &max_quantizer, &undershoot_pct, &overshoot_pct,
328 &buf_sz, &buf_initial_sz, &buf_optimal_sz, NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -0400329};
330
clang-format6c4d83e2016-08-08 19:03:30 -0700331static const arg_def_t bias_pct =
332 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
333static const arg_def_t minsection_pct =
334 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
335static const arg_def_t maxsection_pct =
336 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
337static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
338 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400339
clang-format6c4d83e2016-08-08 19:03:30 -0700340static const arg_def_t kf_min_dist =
341 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
342static const arg_def_t kf_max_dist =
343 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
344static const arg_def_t kf_disabled =
345 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
346static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
347 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400348
clang-format6c4d83e2016-08-08 19:03:30 -0700349static const arg_def_t noise_sens =
350 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
351static const arg_def_t sharpness =
352 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
353static const arg_def_t static_thresh =
354 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
355static const arg_def_t auto_altref =
356 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
357static const arg_def_t arnr_maxframes =
358 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
359static const arg_def_t arnr_strength =
360 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
361static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1, "AltRef type");
John Koleszarb0da9b32010-12-17 09:43:39 -0500362static const struct arg_enum_list tuning_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700363 { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500364};
clang-format6c4d83e2016-08-08 19:03:30 -0700365static const arg_def_t tune_ssim =
366 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
367static const arg_def_t cq_level =
368 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
369static const arg_def_t max_intra_rate_pct =
370 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800371
Yaowu Xuf883b422016-08-30 14:01:10 -0700372#if CONFIG_AV1_ENCODER
373static const arg_def_t cpu_used_av1 =
clang-format6c4d83e2016-08-08 19:03:30 -0700374 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (-8..8)");
375static const arg_def_t tile_cols =
376 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
377static const arg_def_t tile_rows =
378 ARG_DEF(NULL, "tile-rows", 1,
379 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
380static const arg_def_t lossless =
381 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700382#if CONFIG_AOM_QM
383static const arg_def_t enable_qm =
384 ARG_DEF(NULL, "enable_qm", 1,
385 "Enable quantisation matrices (0: false (default), 1: true)");
386static const arg_def_t qm_min = ARG_DEF(
387 NULL, "qm_min", 1, "Min quant matrix flatness (0..15), default is 8");
388static const arg_def_t qm_max = ARG_DEF(
389 NULL, "qm_max", 1, "Max quant matrix flatness (0..15), default is 16");
390#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700391static const arg_def_t frame_parallel_decoding =
392 ARG_DEF(NULL, "frame-parallel", 1,
393 "Enable frame parallel decodability features "
394 "(0: false (default), 1: true)");
Thomase28d92b2016-09-20 12:07:11 +0100395#if CONFIG_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100396static const arg_def_t aq_mode = ARG_DEF(
397 NULL, "aq-mode", 1,
398 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100399 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200400#else
Thomase28d92b2016-09-20 12:07:11 +0100401static const arg_def_t aq_mode = ARG_DEF(
402 NULL, "aq-mode", 1,
403 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100404 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200405#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700406static const arg_def_t frame_periodic_boost =
407 ARG_DEF(NULL, "frame-boost", 1,
408 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700409static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
410 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700411static const arg_def_t max_inter_rate_pct =
412 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700413static const arg_def_t min_gf_interval = ARG_DEF(
414 NULL, "min-gf-interval", 1,
415 "min gf/arf frame interval (default 0, indicating in-built behavior)");
416static const arg_def_t max_gf_interval = ARG_DEF(
417 NULL, "max-gf-interval", 1,
418 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800419
Yaowu Xufc996362015-02-10 15:03:05 -0800420static const struct arg_enum_list color_space_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700421 { "unknown", AOM_CS_UNKNOWN },
422 { "bt601", AOM_CS_BT_601 },
423 { "bt709", AOM_CS_BT_709 },
424 { "smpte170", AOM_CS_SMPTE_170 },
425 { "smpte240", AOM_CS_SMPTE_240 },
426 { "bt2020", AOM_CS_BT_2020 },
427 { "reserved", AOM_CS_RESERVED },
428 { "sRGB", AOM_CS_SRGB },
Yaowu Xufc996362015-02-10 15:03:05 -0800429 { NULL, 0 }
430};
431
clang-format6c4d83e2016-08-08 19:03:30 -0700432static const arg_def_t input_color_space =
433 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
434 color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800435
Alex Converse219f6452014-08-06 11:01:18 -0700436static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700437 { "default", AOM_CONTENT_DEFAULT },
438 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700439 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700440};
441
442static const arg_def_t tune_content = ARG_DEF_ENUM(
443 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700444#endif
Alex Converse219f6452014-08-06 11:01:18 -0700445
Yaowu Xuf883b422016-08-30 14:01:10 -0700446#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000447#if CONFIG_EXT_PARTITION
448static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700449 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
450 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
451 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700452 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000453};
454static const arg_def_t superblock_size = ARG_DEF_ENUM(
455 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
456#endif // CONFIG_EXT_PARTITION
457
Yaowu Xuf883b422016-08-30 14:01:10 -0700458static const arg_def_t *av1_args[] = { &cpu_used_av1,
459 &auto_altref,
460 &sharpness,
461 &static_thresh,
462 &tile_cols,
463 &tile_rows,
464 &arnr_maxframes,
465 &arnr_strength,
466 &arnr_type,
467 &tune_ssim,
468 &cq_level,
469 &max_intra_rate_pct,
470 &max_inter_rate_pct,
471 &gf_cbr_boost_pct,
472 &lossless,
473 &frame_parallel_decoding,
474 &aq_mode,
475 &frame_periodic_boost,
476 &noise_sens,
477 &tune_content,
478 &input_color_space,
479 &min_gf_interval,
480 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000481#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700482 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000483#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700484#if CONFIG_AOM_HIGHBITDEPTH
485 &bitdeptharg,
486 &inbitdeptharg,
487#endif // CONFIG_AOM_HIGHBITDEPTH
488 NULL };
489static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
490 AOME_SET_ENABLEAUTOALTREF,
491 AOME_SET_SHARPNESS,
492 AOME_SET_STATIC_THRESHOLD,
493 AV1E_SET_TILE_COLUMNS,
494 AV1E_SET_TILE_ROWS,
495 AOME_SET_ARNR_MAXFRAMES,
496 AOME_SET_ARNR_STRENGTH,
497 AOME_SET_ARNR_TYPE,
498 AOME_SET_TUNING,
499 AOME_SET_CQ_LEVEL,
500 AOME_SET_MAX_INTRA_BITRATE_PCT,
501 AV1E_SET_MAX_INTER_BITRATE_PCT,
502 AV1E_SET_GF_CBR_BOOST_PCT,
503 AV1E_SET_LOSSLESS,
504 AV1E_SET_FRAME_PARALLEL_DECODING,
505 AV1E_SET_AQ_MODE,
506 AV1E_SET_FRAME_PERIODIC_BOOST,
507 AV1E_SET_NOISE_SENSITIVITY,
508 AV1E_SET_TUNE_CONTENT,
509 AV1E_SET_COLOR_SPACE,
510 AV1E_SET_MIN_GF_INTERVAL,
511 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000512#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700513 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000514#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700515 0 };
hui su82331e02015-08-16 18:21:56 -0700516#endif
517
John Koleszar0ea50ce2010-05-18 11:58:33 -0400518static const arg_def_t *no_args[] = { NULL };
519
James Zern59e7a472015-05-09 10:33:26 -0700520void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700521 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700522 const int num_encoder = get_aom_encoder_count();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400523
John Koleszarc6b90392012-07-13 15:21:29 -0700524 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
525 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400526
John Koleszarc6b90392012-07-13 15:21:29 -0700527 fprintf(stderr, "\nOptions:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700528 arg_show_usage(stderr, main_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700529 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700530 arg_show_usage(stderr, global_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700531 fprintf(stderr, "\nRate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700532 arg_show_usage(stderr, rc_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700533 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700534 arg_show_usage(stderr, rc_twopass_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700535 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700536 arg_show_usage(stderr, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700537#if CONFIG_AV1_ENCODER
538 fprintf(stderr, "\nAV1 Specific Options:\n");
539 arg_show_usage(stderr, av1_args);
hui su82331e02015-08-16 18:21:56 -0700540#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700541 fprintf(stderr,
542 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700543 " The desired precision of timestamps in the output, expressed\n"
544 " in fractional seconds. Default is 1/1000.\n");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800545 fprintf(stderr, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400546
Yaowu Xuf990b352015-06-01 09:13:59 -0700547 for (i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700548 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700549 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
550 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700551 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800552 }
Yaowu Xuf990b352015-06-01 09:13:59 -0700553 fprintf(stderr, "\n ");
554 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400555
John Koleszarc6b90392012-07-13 15:21:29 -0700556 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400557}
558
clang-format6c4d83e2016-08-08 19:03:30 -0700559#define mmin(a, b) ((a) < (b) ? (a) : (b))
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700560
Yaowu Xuf883b422016-08-30 14:01:10 -0700561#if CONFIG_AOM_HIGHBITDEPTH
562static void find_mismatch_high(const aom_image_t *const img1,
563 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700564 int uloc[4], int vloc[4]) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700565 uint16_t *plane1, *plane2;
566 uint32_t stride1, stride2;
567 const uint32_t bsize = 64;
568 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
569 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
570 const uint32_t c_w =
571 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
572 const uint32_t c_h =
573 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
574 int match = 1;
575 uint32_t i, j;
576 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700577 plane1 = (uint16_t *)img1->planes[AOM_PLANE_Y];
578 plane2 = (uint16_t *)img2->planes[AOM_PLANE_Y];
579 stride1 = img1->stride[AOM_PLANE_Y] / 2;
580 stride2 = img2->stride[AOM_PLANE_Y] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700581 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
582 for (j = 0; match && j < img1->d_w; j += bsize) {
583 int k, l;
584 const int si = mmin(i + bsize, img1->d_h) - i;
585 const int sj = mmin(j + bsize, img1->d_w) - j;
586 for (k = 0; match && k < si; ++k) {
587 for (l = 0; match && l < sj; ++l) {
588 if (*(plane1 + (i + k) * stride1 + j + l) !=
589 *(plane2 + (i + k) * stride2 + j + l)) {
590 yloc[0] = i + k;
591 yloc[1] = j + l;
592 yloc[2] = *(plane1 + (i + k) * stride1 + j + l);
593 yloc[3] = *(plane2 + (i + k) * stride2 + j + l);
594 match = 0;
595 break;
596 }
597 }
598 }
599 }
600 }
601
602 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700603 plane1 = (uint16_t *)img1->planes[AOM_PLANE_U];
604 plane2 = (uint16_t *)img2->planes[AOM_PLANE_U];
605 stride1 = img1->stride[AOM_PLANE_U] / 2;
606 stride2 = img2->stride[AOM_PLANE_U] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700607 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
608 for (j = 0; match && j < c_w; j += bsizex) {
609 int k, l;
610 const int si = mmin(i + bsizey, c_h - i);
611 const int sj = mmin(j + bsizex, c_w - j);
612 for (k = 0; match && k < si; ++k) {
613 for (l = 0; match && l < sj; ++l) {
614 if (*(plane1 + (i + k) * stride1 + j + l) !=
615 *(plane2 + (i + k) * stride2 + j + l)) {
616 uloc[0] = i + k;
617 uloc[1] = j + l;
618 uloc[2] = *(plane1 + (i + k) * stride1 + j + l);
619 uloc[3] = *(plane2 + (i + k) * stride2 + j + l);
620 match = 0;
621 break;
622 }
623 }
624 }
625 }
626 }
627
628 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700629 plane1 = (uint16_t *)img1->planes[AOM_PLANE_V];
630 plane2 = (uint16_t *)img2->planes[AOM_PLANE_V];
631 stride1 = img1->stride[AOM_PLANE_V] / 2;
632 stride2 = img2->stride[AOM_PLANE_V] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700633 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
634 for (j = 0; match && j < c_w; j += bsizex) {
635 int k, l;
636 const int si = mmin(i + bsizey, c_h - i);
637 const int sj = mmin(j + bsizex, c_w - j);
638 for (k = 0; match && k < si; ++k) {
639 for (l = 0; match && l < sj; ++l) {
640 if (*(plane1 + (i + k) * stride1 + j + l) !=
641 *(plane2 + (i + k) * stride2 + j + l)) {
642 vloc[0] = i + k;
643 vloc[1] = j + l;
644 vloc[2] = *(plane1 + (i + k) * stride1 + j + l);
645 vloc[3] = *(plane2 + (i + k) * stride2 + j + l);
646 match = 0;
647 break;
648 }
649 }
650 }
651 }
652 }
653}
654#endif
655
Yaowu Xuf883b422016-08-30 14:01:10 -0700656static void find_mismatch(const aom_image_t *const img1,
657 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700658 int uloc[4], int vloc[4]) {
James Zern6253e8e2014-01-31 16:10:43 -0800659 const uint32_t bsize = 64;
660 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
661 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
662 const uint32_t c_w =
663 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
664 const uint32_t c_h =
665 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
666 int match = 1;
667 uint32_t i, j;
Deb Mukherjee23144d22013-03-12 14:21:08 -0700668 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -0800669 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
670 for (j = 0; match && j < img1->d_w; j += bsize) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800671 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800672 const int si = mmin(i + bsize, img1->d_h) - i;
673 const int sj = mmin(j + bsize, img1->d_w) - j;
674 for (k = 0; match && k < si; ++k) {
675 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700676 if (*(img1->planes[AOM_PLANE_Y] +
677 (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
678 *(img2->planes[AOM_PLANE_Y] +
679 (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800680 yloc[0] = i + k;
681 yloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700682 yloc[2] = *(img1->planes[AOM_PLANE_Y] +
683 (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
684 yloc[3] = *(img2->planes[AOM_PLANE_Y] +
685 (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800686 match = 0;
687 break;
688 }
689 }
James Zernd04112d2014-01-31 15:49:17 -0800690 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800691 }
692 }
Jingning Han12bf0792013-04-06 10:00:53 -0700693
Deb Mukherjee23144d22013-03-12 14:21:08 -0700694 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700695 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700696 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800697 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800698 const int si = mmin(i + bsizey, c_h - i);
699 const int sj = mmin(j + bsizex, c_w - j);
700 for (k = 0; match && k < si; ++k) {
701 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700702 if (*(img1->planes[AOM_PLANE_U] +
703 (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
704 *(img2->planes[AOM_PLANE_U] +
705 (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800706 uloc[0] = i + k;
707 uloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700708 uloc[2] = *(img1->planes[AOM_PLANE_U] +
709 (i + k) * img1->stride[AOM_PLANE_U] + j + l);
710 uloc[3] = *(img2->planes[AOM_PLANE_U] +
711 (i + k) * img2->stride[AOM_PLANE_U] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800712 match = 0;
713 break;
714 }
715 }
James Zernd04112d2014-01-31 15:49:17 -0800716 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800717 }
718 }
Deb Mukherjee23144d22013-03-12 14:21:08 -0700719 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700720 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700721 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800722 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800723 const int si = mmin(i + bsizey, c_h - i);
724 const int sj = mmin(j + bsizex, c_w - j);
725 for (k = 0; match && k < si; ++k) {
726 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700727 if (*(img1->planes[AOM_PLANE_V] +
728 (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
729 *(img2->planes[AOM_PLANE_V] +
730 (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800731 vloc[0] = i + k;
732 vloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700733 vloc[2] = *(img1->planes[AOM_PLANE_V] +
734 (i + k) * img1->stride[AOM_PLANE_V] + j + l);
735 vloc[3] = *(img2->planes[AOM_PLANE_V] +
736 (i + k) * img2->stride[AOM_PLANE_V] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800737 match = 0;
738 break;
739 }
740 }
James Zernd04112d2014-01-31 15:49:17 -0800741 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800742 }
743 }
744}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400745
Yaowu Xuf883b422016-08-30 14:01:10 -0700746static int compare_img(const aom_image_t *const img1,
747 const aom_image_t *const img2) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700748 uint32_t l_w = img1->d_w;
clang-format6c4d83e2016-08-08 19:03:30 -0700749 uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
James Zern6253e8e2014-01-31 16:10:43 -0800750 const uint32_t c_h =
751 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
752 uint32_t i;
John Koleszarc6b90392012-07-13 15:21:29 -0700753 int match = 1;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800754
John Koleszarc6b90392012-07-13 15:21:29 -0700755 match &= (img1->fmt == img2->fmt);
hkuang3671e622014-01-22 14:58:59 -0800756 match &= (img1->d_w == img2->d_w);
757 match &= (img1->d_h == img2->d_h);
Yaowu Xuf883b422016-08-30 14:01:10 -0700758#if CONFIG_AOM_HIGHBITDEPTH
759 if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700760 l_w *= 2;
761 c_w *= 2;
762 }
763#endif
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800764
James Zernd04112d2014-01-31 15:49:17 -0800765 for (i = 0; i < img1->d_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700766 match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
767 img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700768 l_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800769
James Zernd04112d2014-01-31 15:49:17 -0800770 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700771 match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
772 img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
John Koleszarbeae5012013-05-08 16:07:38 -0700773 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800774
James Zernd04112d2014-01-31 15:49:17 -0800775 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700776 match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
777 img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
John Koleszarbeae5012013-05-08 16:07:38 -0700778 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800779
John Koleszarc6b90392012-07-13 15:21:29 -0700780 return match;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800781}
782
clang-format6c4d83e2016-08-08 19:03:30 -0700783#define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
Yaowu Xuf883b422016-08-30 14:01:10 -0700784#if CONFIG_AV1_ENCODER
785#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800786#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800787
James Zerndba73762014-05-10 17:44:12 -0700788#if !CONFIG_WEBM_IO
789typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700790struct WebmOutputContext {
791 int debug;
792};
James Zerndba73762014-05-10 17:44:12 -0700793#endif
794
John Koleszar9e50ed72012-02-15 12:39:38 -0800795/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800796struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700797 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700798 const char *out_fn;
799 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700800#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700801 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700802#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700803 stereo_format_t stereo_fmt;
804 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
805 int arg_ctrl_cnt;
806 int write_webm;
Yaowu Xuf883b422016-08-30 14:01:10 -0700807#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700808 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700809 int use_16bit_internal;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700810#endif
John Koleszar9e50ed72012-02-15 12:39:38 -0800811};
812
John Koleszar6ad3b742012-11-06 12:02:42 -0800813struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700814 int index;
815 struct stream_state *next;
816 struct stream_config config;
817 FILE *file;
818 struct rate_hist *rate_hist;
819 struct WebmOutputContext webm_ctx;
820 uint64_t psnr_sse_total;
821 uint64_t psnr_samples_total;
822 double psnr_totals[4];
823 int psnr_count;
824 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700825 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700826 unsigned int frames_out;
827 uint64_t cx_time;
828 size_t nbytes;
829 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700830#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700831 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700832#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700833 struct aom_image *img;
834 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700835 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800836};
837
clang-format6c4d83e2016-08-08 19:03:30 -0700838static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700839 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800840 if (rat->den < 0) {
841 rat->num *= -1;
842 rat->den *= -1;
843 }
John Koleszar1b27e932012-04-25 17:08:56 -0700844
clang-format6c4d83e2016-08-08 19:03:30 -0700845 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700846
clang-format6c4d83e2016-08-08 19:03:30 -0700847 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700848}
849
Yaowu Xuf883b422016-08-30 14:01:10 -0700850static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700851 char **argi, **argj;
852 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700853 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700854
clang-format6c4d83e2016-08-08 19:03:30 -0700855 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800856
John Koleszar6ad3b742012-11-06 12:02:42 -0800857 /* Initialize default parameters */
858 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700859 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700860 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700861 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700862 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700863 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400864
John Koleszarc6b90392012-07-13 15:21:29 -0700865 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
866 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400867
John Koleszarc6b90392012-07-13 15:21:29 -0700868 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700869 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800870 if (!global->codec)
871 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700872 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800873 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400874
John Koleszar6ad3b742012-11-06 12:02:42 -0800875 if (global->passes < 1 || global->passes > 2)
876 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700877 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800878 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400879
John Koleszar6ad3b742012-11-06 12:02:42 -0800880 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700881 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800882 } else if (arg_match(&arg, &usage, argi))
883 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700884 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800885 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700886 else if (arg_match(&arg, &best_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700887 global->deadline = AOM_DL_BEST_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -0700888 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700889 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -0700890 else if (arg_match(&arg, &rt_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700891 global->deadline = AOM_DL_REALTIME;
John Koleszar6ad3b742012-11-06 12:02:42 -0800892 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700893 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800894 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700895 global->color_type = I420;
896 else if (arg_match(&arg, &use_i422, argi))
897 global->color_type = I422;
898 else if (arg_match(&arg, &use_i444, argi))
899 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700900 else if (arg_match(&arg, &use_i440, argi))
901 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800902 else if (arg_match(&arg, &quietarg, argi))
903 global->quiet = 1;
904 else if (arg_match(&arg, &verbosearg, argi))
905 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700906 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800907 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700908 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800909 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700910 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800911 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700912 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800913 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700914 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800915 global->framerate = arg_parse_rational(&arg);
916 validate_positive_rational(arg.name, &global->framerate);
917 global->have_framerate = 1;
918 } else if (arg_match(&arg, &out_part, argi))
919 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700920 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800921 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700922 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800923 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700924 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800925 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800926 else if (arg_match(&arg, &disable_warnings, argi))
927 global->disable_warnings = 1;
928 else if (arg_match(&arg, &disable_warning_prompt, argi))
929 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800930 else
John Koleszarc6b90392012-07-13 15:21:29 -0700931 argj++;
932 }
933
John Koleszar6ad3b742012-11-06 12:02:42 -0800934 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700935 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800936 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700937 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
938 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800939 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800940 }
John Koleszarc6b90392012-07-13 15:21:29 -0700941 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800942 /* Validate global config */
943 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700944#if CONFIG_AV1_ENCODER
945 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800946 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700947 if (global->codec != NULL && global->codec->name != NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -0700948 global->passes = (strcmp(global->codec->name, "av1") == 0 &&
949 global->deadline != AOM_DL_REALTIME)
clang-format6c4d83e2016-08-08 19:03:30 -0700950 ? 2
951 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800952#else
953 global->passes = 1;
954#endif
955 }
956
Yaowu Xuf883b422016-08-30 14:01:10 -0700957 if (global->deadline == AOM_DL_REALTIME && global->passes > 1) {
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800958 warn("Enforcing one-pass encoding in realtime mode\n");
959 global->passes = 1;
960 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800961}
962
Yaowu Xuf883b422016-08-30 14:01:10 -0700963static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800964 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700965 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
966 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -0800967
clang-format6c4d83e2016-08-08 19:03:30 -0700968 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -0800969
John Koleszar25b6e9f2013-02-12 21:17:56 -0800970 if (!fseeko(input->file, 0, SEEK_END)) {
971 /* Input file is seekable. Figure out how long it is, so we can get
972 * progress info.
973 */
974 input->length = ftello(input->file);
975 rewind(input->file);
976 }
977
Frank Galligan09acd262015-06-01 10:20:58 -0700978 /* Default to 1:1 pixel aspect ratio. */
979 input->pixel_aspect_ratio.numerator = 1;
980 input->pixel_aspect_ratio.denominator = 1;
981
John Koleszar6ad3b742012-11-06 12:02:42 -0800982 /* For RAW input sources, these bytes will applied on the first frame
983 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -0700984 */
John Koleszar6ad3b742012-11-06 12:02:42 -0800985 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
986 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400987
clang-format6c4d83e2016-08-08 19:03:30 -0700988 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -0700989 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
990 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800991 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800992 input->width = input->y4m.pic_w;
993 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -0700994 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
995 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800996 input->framerate.numerator = input->y4m.fps_n;
997 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -0700998 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -0700999 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -08001000 } else
1001 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -08001002 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -08001003 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -08001004 } else {
1005 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001006 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001007}
1008
Yaowu Xuf883b422016-08-30 14:01:10 -07001009static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001010 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -07001011 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001012}
1013
Yaowu Xuf883b422016-08-30 14:01:10 -07001014static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -08001015 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001016 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001017
John Koleszar6ad3b742012-11-06 12:02:42 -08001018 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001019 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001020 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001021 }
1022
John Koleszar6ad3b742012-11-06 12:02:42 -08001023 if (prev) {
1024 memcpy(stream, prev, sizeof(*stream));
1025 stream->index++;
1026 prev->next = stream;
1027 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001028 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001029
John Koleszar6ad3b742012-11-06 12:02:42 -08001030 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -07001031 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -07001032 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -07001033 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001034
John Koleszar6ad3b742012-11-06 12:02:42 -08001035 /* Change the default timebase to a high enough value so that the
1036 * encoder will always create strictly increasing timestamps.
1037 */
1038 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001039
John Koleszar6ad3b742012-11-06 12:02:42 -08001040 /* Never use the library's default resolution, require it be parsed
1041 * from the file or set on the command line.
1042 */
1043 stream->config.cfg.g_w = 0;
1044 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001045
John Koleszar6ad3b742012-11-06 12:02:42 -08001046 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -08001047 stream->config.write_webm = 1;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001048#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -07001049 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001050 stream->webm_ctx.last_pts_ns = -1;
1051 stream->webm_ctx.writer = NULL;
1052 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001053#endif
Johann87c40b32012-03-01 16:12:53 -08001054
John Koleszar6ad3b742012-11-06 12:02:42 -08001055 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001056 stream->webm_ctx.debug = global->debug;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001057
1058 /* Default lag_in_frames is 0 in realtime mode */
Yaowu Xuf883b422016-08-30 14:01:10 -07001059 if (global->deadline == AOM_DL_REALTIME)
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001060 stream->config.cfg.g_lag_in_frames = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001061 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001062
John Koleszar6ad3b742012-11-06 12:02:42 -08001063 /* Output files must be specified for each stream */
1064 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001065
John Koleszar6ad3b742012-11-06 12:02:42 -08001066 stream->next = NULL;
1067 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001068}
1069
Yaowu Xuf883b422016-08-30 14:01:10 -07001070static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001071 struct stream_state *stream, char **argv) {
1072 char **argi, **argj;
1073 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001074 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001075 static const int *ctrl_args_map = NULL;
1076 struct stream_config *config = &stream->config;
1077 int eos_mark_found = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001078#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -07001079 int test_16bit_internal = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001080#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001081
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001082 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001083 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001084#if CONFIG_AV1_ENCODER
1085 } else if (strcmp(global->codec->name, "av1") == 0) {
1086 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1087 // Consider to expand this set for AV1 encoder control.
1088 ctrl_args = av1_args;
1089 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001090#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001091 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001092
John Koleszarc6b90392012-07-13 15:21:29 -07001093 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001094 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001095
John Koleszar6ad3b742012-11-06 12:02:42 -08001096 /* Once we've found an end-of-stream marker (--) we want to continue
1097 * shifting arguments but not consuming them.
1098 */
1099 if (eos_mark_found) {
1100 argj++;
1101 continue;
1102 } else if (!strcmp(*argj, "--")) {
1103 eos_mark_found = 1;
1104 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001105 }
1106
Adrian Granged2401802015-02-13 14:51:32 -08001107 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001108 config->out_fn = arg.val;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001109 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001110 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001111#if CONFIG_FP_MB_STATS
1112 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1113 config->fpmb_stats_fn = arg.val;
1114#endif
Johannb50e5182015-01-30 15:05:14 -08001115 } else if (arg_match(&arg, &use_webm, argi)) {
1116#if CONFIG_WEBM_IO
1117 config->write_webm = 1;
1118#else
1119 die("Error: --webm specified but webm is disabled.");
1120#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001121 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001122 config->write_webm = 0;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001123 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001124 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001125 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001126 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001127 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001128 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001129 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001130 config->cfg.g_h = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -07001131#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001132 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1133 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1134 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1135 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1136#endif
James Zerndba73762014-05-10 17:44:12 -07001137#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001138 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001139 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001140#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001141 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001142 config->cfg.g_timebase = arg_parse_rational(&arg);
1143 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001144 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001145 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001146 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001147 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -07001148 if (global->deadline == AOM_DL_REALTIME &&
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001149 config->cfg.g_lag_in_frames != 0) {
1150 warn("non-zero %s option ignored in realtime mode.\n", arg.name);
1151 config->cfg.g_lag_in_frames = 0;
1152 }
1153 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001154 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001155 } else if (arg_match(&arg, &resize_allowed, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001156 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001157 } else if (arg_match(&arg, &resize_width, argi)) {
1158 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
1159 } else if (arg_match(&arg, &resize_height, argi)) {
1160 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001161 } else if (arg_match(&arg, &resize_up_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001162 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001163 } else if (arg_match(&arg, &resize_down_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001164 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001165 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001166 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001167 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001168 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001169 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001170 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001171 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001172 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001173 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001174 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001175 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001176 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001177 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001178 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001179 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001180 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001181 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001182 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001183 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001184 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001185 if (global->passes < 2)
1186 warn("option %s ignored in one-pass mode.\n", arg.name);
1187 } else if (arg_match(&arg, &minsection_pct, argi)) {
1188 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1189
1190 if (global->passes < 2)
1191 warn("option %s ignored in one-pass mode.\n", arg.name);
1192 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1193 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1194
1195 if (global->passes < 2)
1196 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001197 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001198 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001199 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001200 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001201 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001202 config->cfg.kf_mode = AOM_KF_DISABLED;
1203#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001204 } else if (arg_match(&arg, &test16bitinternalarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001205 if (strcmp(global->codec->name, "av1") == 0 ||
1206 strcmp(global->codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001207 test_16bit_internal = 1;
1208 }
1209#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001210 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001211 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001212 for (i = 0; ctrl_args[i]; i++) {
1213 if (arg_match(&arg, ctrl_args[i], argi)) {
1214 int j;
1215 match = 1;
1216
1217 /* Point either to the next free element or the first
1218 * instance of this control.
1219 */
1220 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001221 if (ctrl_args_map != NULL &&
1222 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001223 break;
1224
1225 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001226 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001227 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001228 config->arg_ctrls[j][0] = ctrl_args_map[i];
1229 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001230 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001231 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001232 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001233 }
clang-format6c4d83e2016-08-08 19:03:30 -07001234 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001235 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001236 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001237#if CONFIG_AOM_HIGHBITDEPTH
1238 if (strcmp(global->codec->name, "av1") == 0 ||
1239 strcmp(global->codec->name, "av1") == 0) {
clang-format6c4d83e2016-08-08 19:03:30 -07001240 config->use_16bit_internal =
1241 test_16bit_internal | (config->cfg.g_profile > 1);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001242 }
1243#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001244 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001245}
1246
clang-format6c4d83e2016-08-08 19:03:30 -07001247#define FOREACH_STREAM(func) \
1248 do { \
1249 struct stream_state *stream; \
Tom Finegan249366b2013-11-25 12:05:19 -08001250 for (stream = streams; stream; stream = stream->next) { \
clang-format6c4d83e2016-08-08 19:03:30 -07001251 func; \
1252 } \
Tom Finegan44dd3272013-11-20 17:18:28 -08001253 } while (0)
John Koleszar9e50ed72012-02-15 12:39:38 -08001254
Alex Conversed66bd222014-02-21 10:52:09 -08001255static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001256 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001257 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001258 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001259
John Koleszar6ad3b742012-11-06 12:02:42 -08001260 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001261 fatal(
1262 "Stream %d: Specify stream dimensions with --width (-w) "
1263 " and --height (-h)",
1264 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001265
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001266 // Check that the codec bit depth is greater than the input bit depth.
1267 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001268 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001269 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1270 stream->index, (int)stream->config.cfg.g_bit_depth,
1271 stream->config.cfg.g_input_bit_depth);
1272 }
1273
John Koleszar6ad3b742012-11-06 12:02:42 -08001274 for (streami = stream; streami; streami = streami->next) {
1275 /* All streams require output files */
1276 if (!streami->config.out_fn)
1277 fatal("Stream %d: Output file is required (specify with -o)",
1278 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001279
John Koleszar6ad3b742012-11-06 12:02:42 -08001280 /* Check for two streams outputting to the same file */
1281 if (streami != stream) {
1282 const char *a = stream->config.out_fn;
1283 const char *b = streami->config.out_fn;
1284 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1285 fatal("Stream %d: duplicate output file (from stream %d)",
1286 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001287 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001288
1289 /* Check for two streams sharing a stats file. */
1290 if (streami != stream) {
1291 const char *a = stream->config.stats_fn;
1292 const char *b = streami->config.stats_fn;
1293 if (a && b && !strcmp(a, b))
1294 fatal("Stream %d: duplicate stats file (from stream %d)",
1295 streami->index, stream->index);
1296 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001297
1298#if CONFIG_FP_MB_STATS
1299 /* Check for two streams sharing a mb stats file. */
1300 if (streami != stream) {
1301 const char *a = stream->config.fpmb_stats_fn;
1302 const char *b = streami->config.fpmb_stats_fn;
1303 if (a && b && !strcmp(a, b))
1304 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1305 streami->index, stream->index);
1306 }
1307#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001308 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001309}
1310
clang-format6c4d83e2016-08-08 19:03:30 -07001311static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001312 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001313 if (!stream->config.cfg.g_w) {
1314 if (!stream->config.cfg.g_h)
1315 stream->config.cfg.g_w = w;
1316 else
1317 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1318 }
1319 if (!stream->config.cfg.g_h) {
1320 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1321 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001322}
1323
clang-format6c4d83e2016-08-08 19:03:30 -07001324static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001325 switch (t) {
1326 case FILE_TYPE_RAW: return "RAW";
1327 case FILE_TYPE_Y4M: return "Y4M";
1328 default: return "Other";
1329 }
1330}
1331
Yaowu Xuf883b422016-08-30 14:01:10 -07001332static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001333 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001334 case AOM_IMG_FMT_I420: return "I420";
1335 case AOM_IMG_FMT_I422: return "I422";
1336 case AOM_IMG_FMT_I444: return "I444";
1337 case AOM_IMG_FMT_I440: return "I440";
1338 case AOM_IMG_FMT_YV12: return "YV12";
1339 case AOM_IMG_FMT_I42016: return "I42016";
1340 case AOM_IMG_FMT_I42216: return "I42216";
1341 case AOM_IMG_FMT_I44416: return "I44416";
1342 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001343 default: return "Other";
1344 }
1345}
Ralph Giles061a16d2012-01-05 15:05:05 -06001346
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001347static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001348 struct AvxEncoderConfig *global,
1349 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001350#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001351 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001352
John Koleszar6ad3b742012-11-06 12:02:42 -08001353 if (stream->index == 0) {
1354 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001355 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001356 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001357 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001358 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001359 }
1360 if (stream->next || stream->index)
1361 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1362 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1363 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001364
John Koleszar6ad3b742012-11-06 12:02:42 -08001365 SHOW(g_usage);
1366 SHOW(g_threads);
1367 SHOW(g_profile);
1368 SHOW(g_w);
1369 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001370 SHOW(g_bit_depth);
1371 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001372 SHOW(g_timebase.num);
1373 SHOW(g_timebase.den);
1374 SHOW(g_error_resilient);
1375 SHOW(g_pass);
1376 SHOW(g_lag_in_frames);
1377 SHOW(rc_dropframe_thresh);
1378 SHOW(rc_resize_allowed);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001379 SHOW(rc_scaled_width);
1380 SHOW(rc_scaled_height);
John Koleszar6ad3b742012-11-06 12:02:42 -08001381 SHOW(rc_resize_up_thresh);
1382 SHOW(rc_resize_down_thresh);
1383 SHOW(rc_end_usage);
1384 SHOW(rc_target_bitrate);
1385 SHOW(rc_min_quantizer);
1386 SHOW(rc_max_quantizer);
1387 SHOW(rc_undershoot_pct);
1388 SHOW(rc_overshoot_pct);
1389 SHOW(rc_buf_sz);
1390 SHOW(rc_buf_initial_sz);
1391 SHOW(rc_buf_optimal_sz);
1392 SHOW(rc_2pass_vbr_bias_pct);
1393 SHOW(rc_2pass_vbr_minsection_pct);
1394 SHOW(rc_2pass_vbr_maxsection_pct);
1395 SHOW(kf_mode);
1396 SHOW(kf_min_dist);
1397 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001398}
1399
John Koleszar9e50ed72012-02-15 12:39:38 -08001400static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001401 struct AvxEncoderConfig *global,
1402 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001403 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001404 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001405
Yaowu Xuf883b422016-08-30 14:01:10 -07001406 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001407
John Koleszar6ad3b742012-11-06 12:02:42 -08001408 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001409
clang-format6c4d83e2016-08-08 19:03:30 -07001410 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001411
John Koleszar6ad3b742012-11-06 12:02:42 -08001412 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1413 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001414
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001415#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001416 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001417 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001418 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1419 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001420 }
James Zernc8e5a772016-02-11 18:53:50 -08001421#else
1422 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001423#endif
1424
1425 if (!stream->config.write_webm) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001426 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1427 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001428}
1429
John Koleszar9e50ed72012-02-15 12:39:38 -08001430static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001431 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001432 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001433
Yaowu Xuf883b422016-08-30 14:01:10 -07001434 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001435
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001436#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001437 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001438 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001439 }
1440#endif
1441
1442 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001443 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001444 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001445 stream->frames_out);
1446 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001447
John Koleszar6ad3b742012-11-06 12:02:42 -08001448 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001449}
1450
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001451static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001452 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001453 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001454 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001455 fatal("Failed to open statistics store");
1456 } else {
1457 if (!stats_open_mem(&stream->stats, pass))
1458 fatal("Failed to open statistics store");
1459 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001460
Pengchong Jinf349b072014-07-14 09:13:38 -07001461#if CONFIG_FP_MB_STATS
1462 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001463 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1464 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001465 fatal("Failed to open mb statistics store");
1466 } else {
1467 if (!stats_open_mem(&stream->fpmb_stats, pass))
1468 fatal("Failed to open mb statistics store");
1469 }
1470#endif
1471
John Koleszar6ad3b742012-11-06 12:02:42 -08001472 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001473 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1474 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001475 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001476 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001477#if CONFIG_FP_MB_STATS
1478 stream->config.cfg.rc_firstpass_mb_stats_in =
1479 stats_get(&stream->fpmb_stats);
1480#endif
1481 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001482
John Koleszar6ad3b742012-11-06 12:02:42 -08001483 stream->cx_time = 0;
1484 stream->nbytes = 0;
1485 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001486}
1487
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001488static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001489 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001490 int i;
1491 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001492
Yaowu Xuf883b422016-08-30 14:01:10 -07001493 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1494 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
1495#if CONFIG_AOM_HIGHBITDEPTH
1496 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001497#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001498
John Koleszar6ad3b742012-11-06 12:02:42 -08001499 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001500 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001501 &stream->config.cfg, flags);
1502 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001503
Yaowu Xuf883b422016-08-30 14:01:10 -07001504 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001505 * we're being clever to store the control IDs in an array. Real
1506 * applications will want to make use of the enumerations directly
1507 */
1508 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1509 int ctrl = stream->config.arg_ctrls[i][0];
1510 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001511 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001512 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001513
John Koleszar6ad3b742012-11-06 12:02:42 -08001514 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1515 }
1516
John Koleszar5ebe94f2012-12-23 07:20:10 -08001517#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001518 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001519 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1520 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
1521 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001522
Yaowu Xuf883b422016-08-30 14:01:10 -07001523#if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
1524 if (strcmp(global->codec->name, "av1") == 0) {
1525 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001526 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1527
Yaowu Xuf883b422016-08-30 14:01:10 -07001528 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001529 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1530 }
1531#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001532 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001533#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001534}
1535
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001536static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001537 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001538 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001539 aom_codec_pts_t frame_start, next_frame_start;
1540 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1541 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001542
clang-format6c4d83e2016-08-08 19:03:30 -07001543 frame_start =
1544 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1545 cfg->g_timebase.num / global->framerate.num;
1546 next_frame_start =
1547 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1548 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001549
clang-format6c4d83e2016-08-08 19:03:30 -07001550/* Scale if necessary */
Yaowu Xuf883b422016-08-30 14:01:10 -07001551#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001552 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001553 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001554 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001555 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001556 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1557 exit(EXIT_FAILURE);
1558 }
1559#if CONFIG_LIBYUV
1560 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001561 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001562 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001563 }
clang-format6c4d83e2016-08-08 19:03:30 -07001564 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001565 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1566 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1567 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1568 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1569 stream->img->stride[AOM_PLANE_Y] / 2,
1570 (uint16 *)stream->img->planes[AOM_PLANE_U],
1571 stream->img->stride[AOM_PLANE_U] / 2,
1572 (uint16 *)stream->img->planes[AOM_PLANE_V],
1573 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001574 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001575 img = stream->img;
1576#else
clang-format6c4d83e2016-08-08 19:03:30 -07001577 stream->encoder.err = 1;
1578 ctx_exit_on_error(&stream->encoder,
1579 "Stream %d: Failed to encode frame.\n"
1580 "Scaling disabled in this configuration. \n"
1581 "To enable, configure with --enable-libyuv\n",
1582 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001583#endif
1584 }
1585 }
1586#endif
John Koleszar34882b92012-03-01 12:50:40 -08001587 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001588 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001589 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1590 exit(EXIT_FAILURE);
1591 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001592#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001593 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001594 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001595 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001596 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001597 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1598 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1599 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1600 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1601 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1602 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001603 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001604 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001605#else
1606 stream->encoder.err = 1;
1607 ctx_exit_on_error(&stream->encoder,
1608 "Stream %d: Failed to encode frame.\n"
1609 "Scaling disabled in this configuration. \n"
1610 "To enable, configure with --enable-libyuv\n",
1611 stream->index);
1612#endif
John Koleszar34882b92012-03-01 12:50:40 -08001613 }
1614
Yaowu Xuf883b422016-08-30 14:01:10 -07001615 aom_usec_timer_start(&timer);
1616 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001617 (unsigned long)(next_frame_start - frame_start), 0,
1618 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001619 aom_usec_timer_mark(&timer);
1620 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001621 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1622 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001623}
1624
John Koleszar6ad3b742012-11-06 12:02:42 -08001625static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001626 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001627 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001628
Yaowu Xuf883b422016-08-30 14:01:10 -07001629 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001630 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1631 stream->counts[q]++;
1632 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001633}
1634
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001635static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001636 struct AvxEncoderConfig *global, int *got_data) {
1637 const aom_codec_cx_pkt_t *pkt;
1638 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1639 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001640
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001641 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001642 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001643 static size_t fsize = 0;
James Zerne3578af2014-04-17 10:47:08 -07001644 static int64_t ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001645
John Koleszar6ad3b742012-11-06 12:02:42 -08001646 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001647 case AOM_CODEC_CX_FRAME_PKT:
1648 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001649 stream->frames_out++;
1650 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001651 if (!global->quiet)
1652 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001653
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001654 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001655#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001656 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001657 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001658 }
1659#endif
1660 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001661 if (pkt->data.frame.partition_id <= 0) {
1662 ivf_header_pos = ftello(stream->file);
1663 fsize = pkt->data.frame.sz;
1664
Dmitry Kovalev373b0f92014-01-29 17:57:21 -08001665 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001666 } else {
1667 fsize += pkt->data.frame.sz;
1668
Yaowu Xuf883b422016-08-30 14:01:10 -07001669 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
James Zerne3578af2014-04-17 10:47:08 -07001670 const int64_t currpos = ftello(stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001671 fseeko(stream->file, ivf_header_pos, SEEK_SET);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001672 ivf_write_frame_size(stream->file, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001673 fseeko(stream->file, currpos, SEEK_SET);
1674 }
1675 }
1676
clang-format6c4d83e2016-08-08 19:03:30 -07001677 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1678 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001679 }
1680 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001681
John Koleszar9e50ed72012-02-15 12:39:38 -08001682 *got_data = 1;
John Koleszar5ebe94f2012-12-23 07:20:10 -08001683#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001684 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001685 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001686 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001687 if (stream->decoder.err) {
1688 warn_or_exit_on_error(&stream->decoder,
1689 global->test_decode == TEST_DECODE_FATAL,
1690 "Failed to decode frame %d in stream %d",
1691 stream->frames_out + 1, stream->index);
1692 stream->mismatch_seen = stream->frames_out + 1;
1693 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001694 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001695#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001696 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001697 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001698 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001699 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001700 pkt->data.twopass_stats.sz);
1701 stream->nbytes += pkt->data.raw.sz;
1702 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001703#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001704 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001705 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001706 pkt->data.firstpass_mb_stats.sz);
1707 stream->nbytes += pkt->data.raw.sz;
1708 break;
1709#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001710 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001711
1712 if (global->show_psnr) {
1713 int i;
1714
1715 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1716 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1717 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001718 if (!global->quiet)
1719 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001720 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1721 }
1722 stream->psnr_count++;
1723 }
1724
1725 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001726 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001727 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001728 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001729}
1730
clang-format6c4d83e2016-08-08 19:03:30 -07001731static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001732 int i;
1733 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001734
clang-format6c4d83e2016-08-08 19:03:30 -07001735 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001736
John Koleszar6ad3b742012-11-06 12:02:42 -08001737 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001738 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001739 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001740 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001741
John Koleszar6ad3b742012-11-06 12:02:42 -08001742 for (i = 0; i < 4; i++) {
1743 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1744 }
1745 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001746}
1747
John Koleszar25b6e9f2013-02-12 21:17:56 -08001748static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001749 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001750}
1751
clang-format6c4d83e2016-08-08 19:03:30 -07001752static void test_decode(struct stream_state *stream,
John Koleszarb3c350a2013-03-13 12:15:43 -07001753 enum TestDecodeFatality fatal,
Yaowu Xuf883b422016-08-30 14:01:10 -07001754 const AvxInterface *codec) {
1755 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001756
clang-format6c4d83e2016-08-08 19:03:30 -07001757 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001758
John Koleszarb3c350a2013-03-13 12:15:43 -07001759 /* Get the internal reference frame */
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001760 if (strcmp(codec->name, "vp8") == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001761 struct aom_ref_frame ref_enc, ref_dec;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001762 const unsigned int frame_width = (stream->config.cfg.g_w + 15) & ~15;
1763 const unsigned int frame_height = (stream->config.cfg.g_h + 15) & ~15;
1764 aom_img_alloc(&ref_enc.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001765 enc_img = ref_enc.img;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001766 aom_img_alloc(&ref_dec.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001767 dec_img = ref_dec.img;
1768
Yaowu Xuf883b422016-08-30 14:01:10 -07001769 ref_enc.frame_type = AOM_LAST_FRAME;
1770 ref_dec.frame_type = AOM_LAST_FRAME;
1771 aom_codec_control(&stream->encoder, AOM_COPY_REFERENCE, &ref_enc);
1772 aom_codec_control(&stream->decoder, AOM_COPY_REFERENCE, &ref_dec);
John Koleszarb3c350a2013-03-13 12:15:43 -07001773 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001774 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1775 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001776
Yaowu Xuf883b422016-08-30 14:01:10 -07001777#if CONFIG_AOM_HIGHBITDEPTH
1778 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1779 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1780 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1781 aom_image_t enc_hbd_img;
1782 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001783 enc_img.d_w, enc_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001784 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001785 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001786 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001787 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1788 aom_image_t dec_hbd_img;
1789 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001790 dec_img.d_w, dec_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001791 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001792 dec_img = dec_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001793 }
1794 }
1795#endif
John Koleszarb3c350a2013-03-13 12:15:43 -07001796 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001797 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001798 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001799
John Koleszarb3c350a2013-03-13 12:15:43 -07001800 if (!compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001801 int y[4], u[4], v[4];
Yaowu Xuf883b422016-08-30 14:01:10 -07001802#if CONFIG_AOM_HIGHBITDEPTH
1803 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001804 find_mismatch_high(&enc_img, &dec_img, y, u, v);
1805 } else {
1806 find_mismatch(&enc_img, &dec_img, y, u, v);
1807 }
1808#else
John Koleszarb3c350a2013-03-13 12:15:43 -07001809 find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001810#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001811 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001812 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001813 "Stream %d: Encode/decode mismatch on frame %d at"
1814 " Y[%d, %d] {%d/%d},"
1815 " U[%d, %d] {%d/%d},"
1816 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001817 stream->index, stream->frames_out, y[0], y[1], y[2],
1818 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 -08001819 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001820 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001821
Yaowu Xuf883b422016-08-30 14:01:10 -07001822 aom_img_free(&enc_img);
1823 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001824}
John Koleszarefd54f82012-02-13 16:52:18 -08001825
John Koleszar25b6e9f2013-02-12 21:17:56 -08001826static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001827 int64_t hours;
1828 int64_t mins;
1829 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001830
1831 if (etl >= 0) {
1832 hours = etl / 3600;
1833 etl -= hours * 3600;
1834 mins = etl / 60;
1835 etl -= mins * 60;
1836 secs = etl;
1837
clang-format6c4d83e2016-08-08 19:03:30 -07001838 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1839 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001840 } else {
1841 fprintf(stderr, "[%3s unknown] ", label);
1842 }
1843}
1844
Tom Finegan44dd3272013-11-20 17:18:28 -08001845int main(int argc, const char **argv_) {
1846 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001847 aom_image_t raw;
1848#if CONFIG_AOM_HIGHBITDEPTH
1849 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001850 int allocated_raw_shift = 0;
1851 int use_16bit_internal = 0;
1852 int input_shift = 0;
1853#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001854 int frame_avail, got_data;
1855
Yaowu Xuf883b422016-08-30 14:01:10 -07001856 struct AvxInputContext input;
1857 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001858 struct stream_state *streams = NULL;
1859 char **argv, **argi;
1860 uint64_t cx_time = 0;
1861 int stream_cnt = 0;
1862 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001863 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001864
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001865 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001866 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001867
clang-format6c4d83e2016-08-08 19:03:30 -07001868 if (argc < 3) usage_exit();
John Koleszar6ad3b742012-11-06 12:02:42 -08001869
1870 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001871 input.framerate.numerator = 30;
1872 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001873 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001874 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001875
1876 /* First parse the global configuration values, because we want to apply
1877 * other parameters on top of the default configuration provided by the
1878 * codec.
1879 */
1880 argv = argv_dup(argc - 1, argv_ + 1);
1881 parse_global_config(&global, argv);
1882
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001883 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001884 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1885 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1886 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1887 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1888 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001889 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001890
John Koleszar6ad3b742012-11-06 12:02:42 -08001891 {
1892 /* Now parse each stream's parameters. Using a local scope here
1893 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1894 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001895 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001896 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001897
John Koleszar6ad3b742012-11-06 12:02:42 -08001898 do {
1899 stream = new_stream(&global, stream);
1900 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001901 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001902 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001903 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001904
John Koleszarc6b90392012-07-13 15:21:29 -07001905 /* Check for unrecognized options */
1906 for (argi = argv; *argi; argi++)
1907 if (argi[0][0] == '-' && argi[0][1])
1908 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001909
clang-format6c4d83e2016-08-08 19:03:30 -07001910 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1911 &stream->config.cfg););
Tom Finegan249366b2013-11-25 12:05:19 -08001912
John Koleszarc6b90392012-07-13 15:21:29 -07001913 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001914 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001915
clang-format6c4d83e2016-08-08 19:03:30 -07001916 if (!input.filename) usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04001917
John Koleszar8dd82872013-05-06 11:01:35 -07001918 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001919 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001920
John Koleszar6ad3b742012-11-06 12:02:42 -08001921 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001922 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001923 int64_t estimated_time_left = -1;
1924 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001925 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001926
John Koleszar6ad3b742012-11-06 12:02:42 -08001927 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001928
John Koleszar6ad3b742012-11-06 12:02:42 -08001929 /* If the input file doesn't specify its w/h (raw files), try to get
1930 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001931 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001932 if (!input.width || !input.height) {
1933 FOREACH_STREAM({
1934 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1935 input.width = stream->config.cfg.g_w;
1936 input.height = stream->config.cfg.g_h;
1937 break;
1938 }
1939 });
1940 }
John Koleszarc6b90392012-07-13 15:21:29 -07001941
John Koleszar6ad3b742012-11-06 12:02:42 -08001942 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001943 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001944 fatal(
1945 "Specify stream dimensions with --width (-w) "
1946 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001947
1948 /* If input file does not specify bit-depth but input-bit-depth parameter
1949 * exists, assume that to be the input bit-depth. However, if the
1950 * input-bit-depth paramter does not exist, assume the input bit-depth
1951 * to be the same as the codec bit-depth.
1952 */
1953 if (!input.bit_depth) {
1954 FOREACH_STREAM({
1955 if (stream->config.cfg.g_input_bit_depth)
1956 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1957 else
1958 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1959 (int)stream->config.cfg.g_bit_depth;
1960 });
Yaowu Xuf883b422016-08-30 14:01:10 -07001961 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001962 } else {
clang-format6c4d83e2016-08-08 19:03:30 -07001963 FOREACH_STREAM(
1964 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001965 }
1966
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001967 FOREACH_STREAM({
Thomas Daede8ec53b22016-09-19 13:24:51 -07001968 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
1969 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
1970 was selected. */
1971 switch (stream->config.cfg.g_profile) {
1972 case 0:
1973 stream->config.cfg.g_profile = 1;
1974 profile_updated = 1;
1975 break;
1976 case 2:
1977 stream->config.cfg.g_profile = 3;
1978 profile_updated = 1;
1979 break;
1980 default: break;
1981 }
1982 }
1983#if CONFIG_AOM_HIGHBITDEPTH
1984 /* Automatically set the codec bit depth to match the input bit depth.
1985 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001986 if (stream->config.cfg.g_input_bit_depth >
1987 (unsigned int)stream->config.cfg.g_bit_depth) {
1988 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
1989 }
1990 if (stream->config.cfg.g_bit_depth > 8) {
1991 switch (stream->config.cfg.g_profile) {
1992 case 0:
1993 stream->config.cfg.g_profile = 2;
1994 profile_updated = 1;
1995 break;
1996 case 1:
1997 stream->config.cfg.g_profile = 3;
1998 profile_updated = 1;
1999 break;
2000 default: break;
2001 }
2002 }
2003 if (stream->config.cfg.g_profile > 1) {
2004 stream->config.use_16bit_internal = 1;
2005 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002006#endif
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002007 if (profile_updated) {
2008 fprintf(stderr,
2009 "Warning: automatically upgrading to profile %d to "
2010 "match input format.\n",
2011 stream->config.cfg.g_profile);
2012 }
2013 });
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002014
Tom Finegan00a35aa2013-11-14 12:37:42 -08002015 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
Alex Conversed66bd222014-02-21 10:52:09 -08002016 FOREACH_STREAM(validate_stream_config(stream, &global));
John Koleszar77e6b452010-11-03 13:58:40 -04002017
John Koleszar6ad3b742012-11-06 12:02:42 -08002018 /* Ensure that --passes and --pass are consistent. If --pass is set and
2019 * --passes=2, ensure --fpf was set.
2020 */
2021 if (global.pass && global.passes == 2)
clang-format6c4d83e2016-08-08 19:03:30 -07002022 FOREACH_STREAM({
2023 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002024 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002025 " and --passes=2\n",
2026 stream->index, global.pass);
2027 });
John Koleszar3245d462010-06-10 17:10:12 -04002028
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002029#if !CONFIG_WEBM_IO
2030 FOREACH_STREAM({
Ronald S. Bultje39775072015-12-16 13:35:43 -05002031 if (stream->config.write_webm) {
2032 stream->config.write_webm = 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002033 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002034 "aomenc was compiled without WebM container support."
clang-format6c4d83e2016-08-08 19:03:30 -07002035 "Producing IVF output");
Ronald S. Bultje39775072015-12-16 13:35:43 -05002036 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002037 });
2038#endif
2039
John Koleszar6ad3b742012-11-06 12:02:42 -08002040 /* Use the frame rate from the file only if none was specified
2041 * on the command-line.
2042 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002043 if (!global.have_framerate) {
2044 global.framerate.num = input.framerate.numerator;
2045 global.framerate.den = input.framerate.denominator;
Ronald S. Bultje9134e9f2016-01-18 14:03:45 -05002046 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
2047 stream->config.cfg.g_timebase.num = global.framerate.den);
Tom Finegan00a35aa2013-11-14 12:37:42 -08002048 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002049
John Koleszar6ad3b742012-11-06 12:02:42 -08002050 /* Show configuration */
2051 if (global.verbose && pass == 0)
2052 FOREACH_STREAM(show_stream_config(stream, &global, &input));
2053
2054 if (pass == (global.pass ? global.pass - 1 : 0)) {
2055 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002056 /*The Y4M reader does its own allocation.
2057 Just initialize this here to avoid problems if we never read any
2058 frames.*/
2059 memset(&raw, 0, sizeof(raw));
2060 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002061 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002062
clang-format6c4d83e2016-08-08 19:03:30 -07002063 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
2064 &stream->config.cfg, &global.framerate));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002065 }
2066
John Koleszar6ad3b742012-11-06 12:02:42 -08002067 FOREACH_STREAM(setup_pass(stream, &global, pass));
clang-format6c4d83e2016-08-08 19:03:30 -07002068 FOREACH_STREAM(
2069 open_output_file(stream, &global, &input.pixel_aspect_ratio));
John Koleszar6ad3b742012-11-06 12:02:42 -08002070 FOREACH_STREAM(initialize_encoder(stream, &global));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002071
Yaowu Xuf883b422016-08-30 14:01:10 -07002072#if CONFIG_AOM_HIGHBITDEPTH
2073 if (strcmp(global.codec->name, "av1") == 0 ||
2074 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002075 // Check to see if at least one stream uses 16 bit internal.
2076 // Currently assume that the bit_depths for all streams using
2077 // highbitdepth are the same.
2078 FOREACH_STREAM({
2079 if (stream->config.use_16bit_internal) {
2080 use_16bit_internal = 1;
2081 }
2082 if (stream->config.cfg.g_profile == 0) {
2083 input_shift = 0;
2084 } else {
2085 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07002086 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002087 }
2088 });
2089 }
2090#endif
2091
John Koleszarc6b90392012-07-13 15:21:29 -07002092 frame_avail = 1;
2093 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002094
John Koleszarc6b90392012-07-13 15:21:29 -07002095 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002096 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002097
John Koleszar6ad3b742012-11-06 12:02:42 -08002098 if (!global.limit || frames_in < global.limit) {
2099 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002100
clang-format6c4d83e2016-08-08 19:03:30 -07002101 if (frame_avail) frames_in++;
2102 seen_frames =
2103 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002104
John Koleszar6ad3b742012-11-06 12:02:42 -08002105 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002106 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002107 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2108
John Koleszar6ad3b742012-11-06 12:02:42 -08002109 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002110 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2111 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002112 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002113 fprintf(stderr, "frame %4d ", frames_in);
2114
clang-format6c4d83e2016-08-08 19:03:30 -07002115 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002116 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002117 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002118 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002119 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002120 }
2121
hui su251e1512016-10-03 15:48:43 -07002122 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002123 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002124 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002125
John Koleszar6ad3b742012-11-06 12:02:42 -08002126 if (frames_in > global.skip_frames) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002127#if CONFIG_AOM_HIGHBITDEPTH
2128 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002129 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2130 assert(use_16bit_internal);
2131 // Input bit depth and stream bit depth do not match, so up
2132 // shift frame to stream bit depth
2133 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002134 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002135 input.width, input.height, 32);
2136 allocated_raw_shift = 1;
2137 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002138 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002139 frame_to_encode = &raw_shift;
2140 } else {
2141 frame_to_encode = &raw;
2142 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002143 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002144 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002145 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002146 FOREACH_STREAM({
2147 if (stream->config.use_16bit_internal)
2148 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002149 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002150 else
2151 assert(0);
2152 });
2153 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002154 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002155 FOREACH_STREAM(encode_frame(stream, &global,
2156 frame_avail ? frame_to_encode : NULL,
2157 frames_in));
2158 }
2159#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002160 aom_usec_timer_start(&timer);
clang-format6c4d83e2016-08-08 19:03:30 -07002161 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
John Koleszar6ad3b742012-11-06 12:02:42 -08002162 frames_in));
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002163#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002164 aom_usec_timer_mark(&timer);
2165 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002166
John Koleszar6ad3b742012-11-06 12:02:42 -08002167 FOREACH_STREAM(update_quantizer_histogram(stream));
John Koleszarc6b90392012-07-13 15:21:29 -07002168
John Koleszar0ea50ce2010-05-18 11:58:33 -04002169 got_data = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002170 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002171
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002172 if (!got_data && input.length && streams != NULL &&
2173 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002174 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002175 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002176 int64_t remaining;
2177 int64_t rate;
2178
2179 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002180 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002181
2182 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002183 remaining = 1000 * (global.limit - global.skip_frames -
2184 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002185 } else {
James Zerne3578af2014-04-17 10:47:08 -07002186 const int64_t input_pos = ftello(input.file);
2187 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002188 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002189
2190 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002191 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002192 }
2193
clang-format6c4d83e2016-08-08 19:03:30 -07002194 average_rate =
2195 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002196 estimated_time_left = average_rate ? remaining / average_rate : -1;
2197 }
2198
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002199 if (got_data && global.test_decode != TEST_DECODE_OFF)
John Koleszarb3c350a2013-03-13 12:15:43 -07002200 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
John Koleszarc6b90392012-07-13 15:21:29 -07002201 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002202
John Koleszarc6b90392012-07-13 15:21:29 -07002203 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002204 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002205 }
2206
clang-format6c4d83e2016-08-08 19:03:30 -07002207 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002208
Deb Mukherjeea160d722014-09-30 21:56:33 -07002209 if (!global.quiet) {
clang-format6c4d83e2016-08-08 19:03:30 -07002210 FOREACH_STREAM(fprintf(
2211 stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2212 "b/f %7" PRId64 "b/s"
2213 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2214 pass + 1, global.passes, frames_in, stream->frames_out,
2215 (int64_t)stream->nbytes,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002216 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
clang-format6c4d83e2016-08-08 19:03:30 -07002217 seen_frames
2218 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2219 global.framerate.den / seen_frames
2220 : 0,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002221 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2222 stream->cx_time > 9999999 ? "ms" : "us",
2223 usec_to_fps(stream->cx_time, seen_frames)));
2224 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002225
Deb Mukherjeea160d722014-09-30 21:56:33 -07002226 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002227 if (global.codec->fourcc == AV1_FOURCC) {
Deb Mukherjeea160d722014-09-30 21:56:33 -07002228 FOREACH_STREAM(
2229 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2230 } else {
2231 FOREACH_STREAM(show_psnr(stream, 255.0));
2232 }
2233 }
John Koleszarc6b90392012-07-13 15:21:29 -07002234
Yaowu Xuf883b422016-08-30 14:01:10 -07002235 FOREACH_STREAM(aom_codec_destroy(&stream->encoder));
John Koleszar6ad3b742012-11-06 12:02:42 -08002236
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002237 if (global.test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002238 FOREACH_STREAM(aom_codec_destroy(&stream->decoder));
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002239 }
2240
John Koleszar6ad3b742012-11-06 12:02:42 -08002241 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002242
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002243 if (global.test_decode == TEST_DECODE_FATAL) {
2244 FOREACH_STREAM(res |= stream->mismatch_seen);
2245 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002246 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002247
John Koleszar6ad3b742012-11-06 12:02:42 -08002248 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
John Koleszarc6b90392012-07-13 15:21:29 -07002249
Pengchong Jinf349b072014-07-14 09:13:38 -07002250#if CONFIG_FP_MB_STATS
2251 FOREACH_STREAM(stats_close(&stream->fpmb_stats, global.passes - 1));
2252#endif
2253
clang-format6c4d83e2016-08-08 19:03:30 -07002254 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002255 }
2256
John Koleszar6ad3b742012-11-06 12:02:42 -08002257 if (global.show_q_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002258 FOREACH_STREAM(
2259 show_q_histogram(stream->counts, global.show_q_hist_buckets));
John Koleszar6ad3b742012-11-06 12:02:42 -08002260
2261 if (global.show_rate_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002262 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
John Koleszar6ad3b742012-11-06 12:02:42 -08002263 global.show_rate_hist_buckets));
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08002264 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
John Koleszar6ad3b742012-11-06 12:02:42 -08002265
Ronald S. Bultje31214972012-10-11 10:13:48 -07002266#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002267 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2268 * to match some existing utilities.
2269 */
Yaowu Xu47e784e2013-10-16 16:29:28 -07002270 if (!(global.pass == 1 && global.passes == 2))
2271 FOREACH_STREAM({
2272 FILE *f = fopen("opsnr.stt", "a");
2273 if (stream->mismatch_seen) {
2274 fprintf(f, "First mismatch occurred in frame %d\n",
2275 stream->mismatch_seen);
2276 } else {
2277 fprintf(f, "No mismatch detected in recon buffers\n");
2278 }
2279 fclose(f);
2280 });
Ronald S. Bultje31214972012-10-11 10:13:48 -07002281#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002282
Yaowu Xuf883b422016-08-30 14:01:10 -07002283#if CONFIG_AOM_HIGHBITDEPTH
2284 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002285#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002286 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002287 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002288 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002289 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002290}