blob: f30ca61ca8eb773666de80746643e4b3853e67bb [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)");
John Koleszarb0da9b32010-12-17 09:43:39 -0500361static const struct arg_enum_list tuning_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700362 { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500363};
clang-format6c4d83e2016-08-08 19:03:30 -0700364static const arg_def_t tune_ssim =
365 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
366static const arg_def_t cq_level =
367 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
368static const arg_def_t max_intra_rate_pct =
369 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800370
Yaowu Xuf883b422016-08-30 14:01:10 -0700371#if CONFIG_AV1_ENCODER
372static const arg_def_t cpu_used_av1 =
clang-format6c4d83e2016-08-08 19:03:30 -0700373 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (-8..8)");
374static const arg_def_t tile_cols =
375 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
376static const arg_def_t tile_rows =
377 ARG_DEF(NULL, "tile-rows", 1,
378 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800379#if CONFIG_DEPENDENT_HORZTILES
380static const arg_def_t tile_dependent_rows =
381 ARG_DEF(NULL, "tile-dependent-rows", 1, "Enable dependent Tile rows");
382#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800383#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800384static const arg_def_t tile_loopfilter = ARG_DEF(
385 NULL, "tile-loopfilter", 1, "Enable loop filter across tile boundary");
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800386#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
clang-format6c4d83e2016-08-08 19:03:30 -0700387static const arg_def_t lossless =
388 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700389#if CONFIG_AOM_QM
390static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800391 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700392 "Enable quantisation matrices (0: false (default), 1: true)");
393static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800394 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700395static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800396 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700397#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000398#if CONFIG_TILE_GROUPS
Yaowu Xu859a5272016-11-10 15:32:21 -0800399static const arg_def_t num_tg = ARG_DEF(
400 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000401static const arg_def_t mtu_size =
402 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800403 "MTU size for a tile group, default is 0 (no MTU targeting), "
404 "overrides maximum number of tile groups");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000405#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800406#if CONFIG_TEMPMV_SIGNALING
407static const arg_def_t disable_tempmv = ARG_DEF(
408 NULL, "disable-tempmv", 1, "Disable temporal mv prediction (default is 0)");
409#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700410static const arg_def_t frame_parallel_decoding =
411 ARG_DEF(NULL, "frame-parallel", 1,
412 "Enable frame parallel decodability features "
413 "(0: false (default), 1: true)");
Thomase28d92b2016-09-20 12:07:11 +0100414#if CONFIG_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100415static const arg_def_t aq_mode = ARG_DEF(
416 NULL, "aq-mode", 1,
417 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100418 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200419#else
Thomase28d92b2016-09-20 12:07:11 +0100420static const arg_def_t aq_mode = ARG_DEF(
421 NULL, "aq-mode", 1,
422 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100423 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200424#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700425static const arg_def_t frame_periodic_boost =
426 ARG_DEF(NULL, "frame-boost", 1,
427 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700428static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
429 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700430static const arg_def_t max_inter_rate_pct =
431 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700432static const arg_def_t min_gf_interval = ARG_DEF(
433 NULL, "min-gf-interval", 1,
434 "min gf/arf frame interval (default 0, indicating in-built behavior)");
435static const arg_def_t max_gf_interval = ARG_DEF(
436 NULL, "max-gf-interval", 1,
437 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800438
Yaowu Xufc996362015-02-10 15:03:05 -0800439static const struct arg_enum_list color_space_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700440 { "unknown", AOM_CS_UNKNOWN },
441 { "bt601", AOM_CS_BT_601 },
442 { "bt709", AOM_CS_BT_709 },
443 { "smpte170", AOM_CS_SMPTE_170 },
444 { "smpte240", AOM_CS_SMPTE_240 },
445 { "bt2020", AOM_CS_BT_2020 },
446 { "reserved", AOM_CS_RESERVED },
447 { "sRGB", AOM_CS_SRGB },
Yaowu Xufc996362015-02-10 15:03:05 -0800448 { NULL, 0 }
449};
450
clang-format6c4d83e2016-08-08 19:03:30 -0700451static const arg_def_t input_color_space =
452 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
453 color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800454
Alex Converse219f6452014-08-06 11:01:18 -0700455static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700456 { "default", AOM_CONTENT_DEFAULT },
457 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700458 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700459};
460
461static const arg_def_t tune_content = ARG_DEF_ENUM(
462 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700463#endif
Alex Converse219f6452014-08-06 11:01:18 -0700464
Yaowu Xuf883b422016-08-30 14:01:10 -0700465#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000466#if CONFIG_EXT_PARTITION
467static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700468 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
469 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
470 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700471 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000472};
473static const arg_def_t superblock_size = ARG_DEF_ENUM(
474 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
475#endif // CONFIG_EXT_PARTITION
476
Yaowu Xuf883b422016-08-30 14:01:10 -0700477static const arg_def_t *av1_args[] = { &cpu_used_av1,
478 &auto_altref,
479 &sharpness,
480 &static_thresh,
481 &tile_cols,
482 &tile_rows,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800483#if CONFIG_DEPENDENT_HORZTILES
484 &tile_dependent_rows,
485#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800486#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800487 &tile_loopfilter,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800488#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700489 &arnr_maxframes,
490 &arnr_strength,
Yaowu Xuf883b422016-08-30 14:01:10 -0700491 &tune_ssim,
492 &cq_level,
493 &max_intra_rate_pct,
494 &max_inter_rate_pct,
495 &gf_cbr_boost_pct,
496 &lossless,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800497#if CONFIG_AOM_QM
498 &enable_qm,
499 &qm_min,
500 &qm_max,
501#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700502 &frame_parallel_decoding,
503 &aq_mode,
504 &frame_periodic_boost,
505 &noise_sens,
506 &tune_content,
507 &input_color_space,
508 &min_gf_interval,
509 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000510#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700511 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000512#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000513#if CONFIG_TILE_GROUPS
514 &num_tg,
515 &mtu_size,
516#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800517#if CONFIG_TEMPMV_SIGNALING
518 &disable_tempmv,
519#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700520#if CONFIG_AOM_HIGHBITDEPTH
521 &bitdeptharg,
522 &inbitdeptharg,
523#endif // CONFIG_AOM_HIGHBITDEPTH
524 NULL };
525static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
526 AOME_SET_ENABLEAUTOALTREF,
527 AOME_SET_SHARPNESS,
528 AOME_SET_STATIC_THRESHOLD,
529 AV1E_SET_TILE_COLUMNS,
530 AV1E_SET_TILE_ROWS,
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800531#if CONFIG_DEPENDENT_HORZTILES
532 AV1E_SET_TILE_DEPENDENT_ROWS,
533#endif
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800534#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800535 AV1E_SET_TILE_LOOPFILTER,
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800536#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuf883b422016-08-30 14:01:10 -0700537 AOME_SET_ARNR_MAXFRAMES,
538 AOME_SET_ARNR_STRENGTH,
Yaowu Xuf883b422016-08-30 14:01:10 -0700539 AOME_SET_TUNING,
540 AOME_SET_CQ_LEVEL,
541 AOME_SET_MAX_INTRA_BITRATE_PCT,
542 AV1E_SET_MAX_INTER_BITRATE_PCT,
543 AV1E_SET_GF_CBR_BOOST_PCT,
544 AV1E_SET_LOSSLESS,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800545#if CONFIG_AOM_QM
546 AV1E_SET_ENABLE_QM,
547 AV1E_SET_QM_MIN,
548 AV1E_SET_QM_MAX,
549#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700550 AV1E_SET_FRAME_PARALLEL_DECODING,
551 AV1E_SET_AQ_MODE,
552 AV1E_SET_FRAME_PERIODIC_BOOST,
553 AV1E_SET_NOISE_SENSITIVITY,
554 AV1E_SET_TUNE_CONTENT,
555 AV1E_SET_COLOR_SPACE,
556 AV1E_SET_MIN_GF_INTERVAL,
557 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000558#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700559 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000560#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000561#if CONFIG_TILE_GROUPS
562 AV1E_SET_NUM_TG,
563 AV1E_SET_MTU,
564#endif
Fangwen Fu8d164de2016-12-14 13:40:54 -0800565#if CONFIG_TEMPMV_SIGNALING
566 AV1E_SET_DISABLE_TEMPMV,
567#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700568 0 };
hui su82331e02015-08-16 18:21:56 -0700569#endif
570
John Koleszar0ea50ce2010-05-18 11:58:33 -0400571static const arg_def_t *no_args[] = { NULL };
572
James Zern59e7a472015-05-09 10:33:26 -0700573void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700574 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700575 const int num_encoder = get_aom_encoder_count();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400576
John Koleszarc6b90392012-07-13 15:21:29 -0700577 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
578 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400579
John Koleszarc6b90392012-07-13 15:21:29 -0700580 fprintf(stderr, "\nOptions:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700581 arg_show_usage(stderr, main_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700582 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700583 arg_show_usage(stderr, global_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700584 fprintf(stderr, "\nRate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700585 arg_show_usage(stderr, rc_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700586 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700587 arg_show_usage(stderr, rc_twopass_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700588 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700589 arg_show_usage(stderr, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700590#if CONFIG_AV1_ENCODER
591 fprintf(stderr, "\nAV1 Specific Options:\n");
592 arg_show_usage(stderr, av1_args);
hui su82331e02015-08-16 18:21:56 -0700593#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700594 fprintf(stderr,
595 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700596 " The desired precision of timestamps in the output, expressed\n"
597 " in fractional seconds. Default is 1/1000.\n");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800598 fprintf(stderr, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400599
Yaowu Xuf990b352015-06-01 09:13:59 -0700600 for (i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700601 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700602 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
603 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700604 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800605 }
Yaowu Xuf990b352015-06-01 09:13:59 -0700606 fprintf(stderr, "\n ");
607 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400608
John Koleszarc6b90392012-07-13 15:21:29 -0700609 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400610}
611
clang-format6c4d83e2016-08-08 19:03:30 -0700612#define mmin(a, b) ((a) < (b) ? (a) : (b))
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700613
Yaowu Xuf883b422016-08-30 14:01:10 -0700614#if CONFIG_AOM_HIGHBITDEPTH
615static void find_mismatch_high(const aom_image_t *const img1,
616 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700617 int uloc[4], int vloc[4]) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700618 uint16_t *plane1, *plane2;
619 uint32_t stride1, stride2;
620 const uint32_t bsize = 64;
621 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
622 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
623 const uint32_t c_w =
624 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
625 const uint32_t c_h =
626 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
627 int match = 1;
628 uint32_t i, j;
629 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700630 plane1 = (uint16_t *)img1->planes[AOM_PLANE_Y];
631 plane2 = (uint16_t *)img2->planes[AOM_PLANE_Y];
632 stride1 = img1->stride[AOM_PLANE_Y] / 2;
633 stride2 = img2->stride[AOM_PLANE_Y] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700634 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
635 for (j = 0; match && j < img1->d_w; j += bsize) {
636 int k, l;
637 const int si = mmin(i + bsize, img1->d_h) - i;
638 const int sj = mmin(j + bsize, img1->d_w) - j;
639 for (k = 0; match && k < si; ++k) {
640 for (l = 0; match && l < sj; ++l) {
641 if (*(plane1 + (i + k) * stride1 + j + l) !=
642 *(plane2 + (i + k) * stride2 + j + l)) {
643 yloc[0] = i + k;
644 yloc[1] = j + l;
645 yloc[2] = *(plane1 + (i + k) * stride1 + j + l);
646 yloc[3] = *(plane2 + (i + k) * stride2 + j + l);
647 match = 0;
648 break;
649 }
650 }
651 }
652 }
653 }
654
655 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700656 plane1 = (uint16_t *)img1->planes[AOM_PLANE_U];
657 plane2 = (uint16_t *)img2->planes[AOM_PLANE_U];
658 stride1 = img1->stride[AOM_PLANE_U] / 2;
659 stride2 = img2->stride[AOM_PLANE_U] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700660 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
661 for (j = 0; match && j < c_w; j += bsizex) {
662 int k, l;
663 const int si = mmin(i + bsizey, c_h - i);
664 const int sj = mmin(j + bsizex, c_w - j);
665 for (k = 0; match && k < si; ++k) {
666 for (l = 0; match && l < sj; ++l) {
667 if (*(plane1 + (i + k) * stride1 + j + l) !=
668 *(plane2 + (i + k) * stride2 + j + l)) {
669 uloc[0] = i + k;
670 uloc[1] = j + l;
671 uloc[2] = *(plane1 + (i + k) * stride1 + j + l);
672 uloc[3] = *(plane2 + (i + k) * stride2 + j + l);
673 match = 0;
674 break;
675 }
676 }
677 }
678 }
679 }
680
681 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700682 plane1 = (uint16_t *)img1->planes[AOM_PLANE_V];
683 plane2 = (uint16_t *)img2->planes[AOM_PLANE_V];
684 stride1 = img1->stride[AOM_PLANE_V] / 2;
685 stride2 = img2->stride[AOM_PLANE_V] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700686 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
687 for (j = 0; match && j < c_w; j += bsizex) {
688 int k, l;
689 const int si = mmin(i + bsizey, c_h - i);
690 const int sj = mmin(j + bsizex, c_w - j);
691 for (k = 0; match && k < si; ++k) {
692 for (l = 0; match && l < sj; ++l) {
693 if (*(plane1 + (i + k) * stride1 + j + l) !=
694 *(plane2 + (i + k) * stride2 + j + l)) {
695 vloc[0] = i + k;
696 vloc[1] = j + l;
697 vloc[2] = *(plane1 + (i + k) * stride1 + j + l);
698 vloc[3] = *(plane2 + (i + k) * stride2 + j + l);
699 match = 0;
700 break;
701 }
702 }
703 }
704 }
705 }
706}
707#endif
708
Yaowu Xuf883b422016-08-30 14:01:10 -0700709static void find_mismatch(const aom_image_t *const img1,
710 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700711 int uloc[4], int vloc[4]) {
James Zern6253e8e2014-01-31 16:10:43 -0800712 const uint32_t bsize = 64;
713 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
714 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
715 const uint32_t c_w =
716 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
717 const uint32_t c_h =
718 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
719 int match = 1;
720 uint32_t i, j;
Deb Mukherjee23144d22013-03-12 14:21:08 -0700721 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -0800722 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
723 for (j = 0; match && j < img1->d_w; j += bsize) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800724 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800725 const int si = mmin(i + bsize, img1->d_h) - i;
726 const int sj = mmin(j + bsize, img1->d_w) - j;
727 for (k = 0; match && k < si; ++k) {
728 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 if (*(img1->planes[AOM_PLANE_Y] +
730 (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
731 *(img2->planes[AOM_PLANE_Y] +
732 (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800733 yloc[0] = i + k;
734 yloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700735 yloc[2] = *(img1->planes[AOM_PLANE_Y] +
736 (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
737 yloc[3] = *(img2->planes[AOM_PLANE_Y] +
738 (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800739 match = 0;
740 break;
741 }
742 }
James Zernd04112d2014-01-31 15:49:17 -0800743 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800744 }
745 }
Jingning Han12bf0792013-04-06 10:00:53 -0700746
Deb Mukherjee23144d22013-03-12 14:21:08 -0700747 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700748 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700749 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800750 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800751 const int si = mmin(i + bsizey, c_h - i);
752 const int sj = mmin(j + bsizex, c_w - j);
753 for (k = 0; match && k < si; ++k) {
754 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 if (*(img1->planes[AOM_PLANE_U] +
756 (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
757 *(img2->planes[AOM_PLANE_U] +
758 (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800759 uloc[0] = i + k;
760 uloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700761 uloc[2] = *(img1->planes[AOM_PLANE_U] +
762 (i + k) * img1->stride[AOM_PLANE_U] + j + l);
763 uloc[3] = *(img2->planes[AOM_PLANE_U] +
764 (i + k) * img2->stride[AOM_PLANE_U] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800765 match = 0;
766 break;
767 }
768 }
James Zernd04112d2014-01-31 15:49:17 -0800769 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800770 }
771 }
Deb Mukherjee23144d22013-03-12 14:21:08 -0700772 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700773 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700774 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800775 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800776 const int si = mmin(i + bsizey, c_h - i);
777 const int sj = mmin(j + bsizex, c_w - j);
778 for (k = 0; match && k < si; ++k) {
779 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700780 if (*(img1->planes[AOM_PLANE_V] +
781 (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
782 *(img2->planes[AOM_PLANE_V] +
783 (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800784 vloc[0] = i + k;
785 vloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700786 vloc[2] = *(img1->planes[AOM_PLANE_V] +
787 (i + k) * img1->stride[AOM_PLANE_V] + j + l);
788 vloc[3] = *(img2->planes[AOM_PLANE_V] +
789 (i + k) * img2->stride[AOM_PLANE_V] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800790 match = 0;
791 break;
792 }
793 }
James Zernd04112d2014-01-31 15:49:17 -0800794 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800795 }
796 }
797}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400798
Yaowu Xuf883b422016-08-30 14:01:10 -0700799static int compare_img(const aom_image_t *const img1,
800 const aom_image_t *const img2) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700801 uint32_t l_w = img1->d_w;
clang-format6c4d83e2016-08-08 19:03:30 -0700802 uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
James Zern6253e8e2014-01-31 16:10:43 -0800803 const uint32_t c_h =
804 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
805 uint32_t i;
John Koleszarc6b90392012-07-13 15:21:29 -0700806 int match = 1;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800807
John Koleszarc6b90392012-07-13 15:21:29 -0700808 match &= (img1->fmt == img2->fmt);
hkuang3671e622014-01-22 14:58:59 -0800809 match &= (img1->d_w == img2->d_w);
810 match &= (img1->d_h == img2->d_h);
Yaowu Xuf883b422016-08-30 14:01:10 -0700811#if CONFIG_AOM_HIGHBITDEPTH
812 if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700813 l_w *= 2;
814 c_w *= 2;
815 }
816#endif
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800817
James Zernd04112d2014-01-31 15:49:17 -0800818 for (i = 0; i < img1->d_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700819 match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
820 img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700821 l_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800822
James Zernd04112d2014-01-31 15:49:17 -0800823 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700824 match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
825 img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
John Koleszarbeae5012013-05-08 16:07:38 -0700826 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800827
James Zernd04112d2014-01-31 15:49:17 -0800828 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700829 match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
830 img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
John Koleszarbeae5012013-05-08 16:07:38 -0700831 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800832
John Koleszarc6b90392012-07-13 15:21:29 -0700833 return match;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800834}
835
clang-format6c4d83e2016-08-08 19:03:30 -0700836#define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
Yaowu Xuf883b422016-08-30 14:01:10 -0700837#if CONFIG_AV1_ENCODER
838#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800839#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800840
James Zerndba73762014-05-10 17:44:12 -0700841#if !CONFIG_WEBM_IO
842typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700843struct WebmOutputContext {
844 int debug;
845};
James Zerndba73762014-05-10 17:44:12 -0700846#endif
847
John Koleszar9e50ed72012-02-15 12:39:38 -0800848/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800849struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700850 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700851 const char *out_fn;
852 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700853#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700854 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700855#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700856 stereo_format_t stereo_fmt;
857 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
858 int arg_ctrl_cnt;
859 int write_webm;
Yaowu Xuf883b422016-08-30 14:01:10 -0700860#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700861 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700862 int use_16bit_internal;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700863#endif
John Koleszar9e50ed72012-02-15 12:39:38 -0800864};
865
John Koleszar6ad3b742012-11-06 12:02:42 -0800866struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700867 int index;
868 struct stream_state *next;
869 struct stream_config config;
870 FILE *file;
871 struct rate_hist *rate_hist;
872 struct WebmOutputContext webm_ctx;
873 uint64_t psnr_sse_total;
874 uint64_t psnr_samples_total;
875 double psnr_totals[4];
876 int psnr_count;
877 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700878 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700879 unsigned int frames_out;
880 uint64_t cx_time;
881 size_t nbytes;
882 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700883#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700884 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700885#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700886 struct aom_image *img;
887 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700888 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800889};
890
clang-format6c4d83e2016-08-08 19:03:30 -0700891static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700892 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800893 if (rat->den < 0) {
894 rat->num *= -1;
895 rat->den *= -1;
896 }
John Koleszar1b27e932012-04-25 17:08:56 -0700897
clang-format6c4d83e2016-08-08 19:03:30 -0700898 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700899
clang-format6c4d83e2016-08-08 19:03:30 -0700900 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700901}
902
Yaowu Xuf883b422016-08-30 14:01:10 -0700903static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700904 char **argi, **argj;
905 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700906 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700907
clang-format6c4d83e2016-08-08 19:03:30 -0700908 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800909
John Koleszar6ad3b742012-11-06 12:02:42 -0800910 /* Initialize default parameters */
911 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700912 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700913 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700914 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700915 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700916 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400917
John Koleszarc6b90392012-07-13 15:21:29 -0700918 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
919 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400920
John Koleszarc6b90392012-07-13 15:21:29 -0700921 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700922 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800923 if (!global->codec)
924 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700925 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800926 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400927
John Koleszar6ad3b742012-11-06 12:02:42 -0800928 if (global->passes < 1 || global->passes > 2)
929 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700930 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800931 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400932
John Koleszar6ad3b742012-11-06 12:02:42 -0800933 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700934 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800935 } else if (arg_match(&arg, &usage, argi))
936 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700937 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800938 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700939 else if (arg_match(&arg, &best_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700940 global->deadline = AOM_DL_BEST_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -0700941 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700942 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -0700943 else if (arg_match(&arg, &rt_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700944 global->deadline = AOM_DL_REALTIME;
John Koleszar6ad3b742012-11-06 12:02:42 -0800945 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700946 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800947 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700948 global->color_type = I420;
949 else if (arg_match(&arg, &use_i422, argi))
950 global->color_type = I422;
951 else if (arg_match(&arg, &use_i444, argi))
952 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700953 else if (arg_match(&arg, &use_i440, argi))
954 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800955 else if (arg_match(&arg, &quietarg, argi))
956 global->quiet = 1;
957 else if (arg_match(&arg, &verbosearg, argi))
958 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700959 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800960 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700961 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800962 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700963 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800964 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700965 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800966 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700967 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800968 global->framerate = arg_parse_rational(&arg);
969 validate_positive_rational(arg.name, &global->framerate);
970 global->have_framerate = 1;
971 } else if (arg_match(&arg, &out_part, argi))
972 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700973 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800974 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700975 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800976 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700977 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800978 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800979 else if (arg_match(&arg, &disable_warnings, argi))
980 global->disable_warnings = 1;
981 else if (arg_match(&arg, &disable_warning_prompt, argi))
982 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800983 else
John Koleszarc6b90392012-07-13 15:21:29 -0700984 argj++;
985 }
986
John Koleszar6ad3b742012-11-06 12:02:42 -0800987 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700988 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800989 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700990 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
991 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800992 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800993 }
John Koleszarc6b90392012-07-13 15:21:29 -0700994 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800995 /* Validate global config */
996 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700997#if CONFIG_AV1_ENCODER
998 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800999 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001000 if (global->codec != NULL && global->codec->name != NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -07001001 global->passes = (strcmp(global->codec->name, "av1") == 0 &&
1002 global->deadline != AOM_DL_REALTIME)
clang-format6c4d83e2016-08-08 19:03:30 -07001003 ? 2
1004 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001005#else
1006 global->passes = 1;
1007#endif
1008 }
1009
Yaowu Xuf883b422016-08-30 14:01:10 -07001010 if (global->deadline == AOM_DL_REALTIME && global->passes > 1) {
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001011 warn("Enforcing one-pass encoding in realtime mode\n");
1012 global->passes = 1;
1013 }
John Koleszar732cb9a2012-02-14 12:30:17 -08001014}
1015
Yaowu Xuf883b422016-08-30 14:01:10 -07001016static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001017 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -07001018 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
1019 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -08001020
clang-format6c4d83e2016-08-08 19:03:30 -07001021 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -08001022
John Koleszar25b6e9f2013-02-12 21:17:56 -08001023 if (!fseeko(input->file, 0, SEEK_END)) {
1024 /* Input file is seekable. Figure out how long it is, so we can get
1025 * progress info.
1026 */
1027 input->length = ftello(input->file);
1028 rewind(input->file);
1029 }
1030
Frank Galligan09acd262015-06-01 10:20:58 -07001031 /* Default to 1:1 pixel aspect ratio. */
1032 input->pixel_aspect_ratio.numerator = 1;
1033 input->pixel_aspect_ratio.denominator = 1;
1034
John Koleszar6ad3b742012-11-06 12:02:42 -08001035 /* For RAW input sources, these bytes will applied on the first frame
1036 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -07001037 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001038 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1039 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001040
clang-format6c4d83e2016-08-08 19:03:30 -07001041 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -07001042 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
1043 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001044 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001045 input->width = input->y4m.pic_w;
1046 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -07001047 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1048 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001049 input->framerate.numerator = input->y4m.fps_n;
1050 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -07001051 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001052 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -08001053 } else
1054 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -08001055 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -08001056 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -08001057 } else {
1058 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001059 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001060}
1061
Yaowu Xuf883b422016-08-30 14:01:10 -07001062static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001063 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -07001064 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001065}
1066
Yaowu Xuf883b422016-08-30 14:01:10 -07001067static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -08001068 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001069 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001070
John Koleszar6ad3b742012-11-06 12:02:42 -08001071 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001072 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001073 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001074 }
1075
John Koleszar6ad3b742012-11-06 12:02:42 -08001076 if (prev) {
1077 memcpy(stream, prev, sizeof(*stream));
1078 stream->index++;
1079 prev->next = stream;
1080 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001081 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001082
John Koleszar6ad3b742012-11-06 12:02:42 -08001083 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -07001084 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -07001085 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -07001086 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001087
John Koleszar6ad3b742012-11-06 12:02:42 -08001088 /* Change the default timebase to a high enough value so that the
1089 * encoder will always create strictly increasing timestamps.
1090 */
1091 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001092
John Koleszar6ad3b742012-11-06 12:02:42 -08001093 /* Never use the library's default resolution, require it be parsed
1094 * from the file or set on the command line.
1095 */
1096 stream->config.cfg.g_w = 0;
1097 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001098
John Koleszar6ad3b742012-11-06 12:02:42 -08001099 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -08001100 stream->config.write_webm = 1;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001101#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -07001102 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001103 stream->webm_ctx.last_pts_ns = -1;
1104 stream->webm_ctx.writer = NULL;
1105 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001106#endif
Johann87c40b32012-03-01 16:12:53 -08001107
John Koleszar6ad3b742012-11-06 12:02:42 -08001108 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001109 stream->webm_ctx.debug = global->debug;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001110
1111 /* Default lag_in_frames is 0 in realtime mode */
Yaowu Xuf883b422016-08-30 14:01:10 -07001112 if (global->deadline == AOM_DL_REALTIME)
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001113 stream->config.cfg.g_lag_in_frames = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001114 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001115
John Koleszar6ad3b742012-11-06 12:02:42 -08001116 /* Output files must be specified for each stream */
1117 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001118
John Koleszar6ad3b742012-11-06 12:02:42 -08001119 stream->next = NULL;
1120 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001121}
1122
Yaowu Xuf883b422016-08-30 14:01:10 -07001123static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001124 struct stream_state *stream, char **argv) {
1125 char **argi, **argj;
1126 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001127 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001128 static const int *ctrl_args_map = NULL;
1129 struct stream_config *config = &stream->config;
1130 int eos_mark_found = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001131#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -07001132 int test_16bit_internal = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001133#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001134
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001135 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001136 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001137#if CONFIG_AV1_ENCODER
1138 } else if (strcmp(global->codec->name, "av1") == 0) {
1139 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1140 // Consider to expand this set for AV1 encoder control.
1141 ctrl_args = av1_args;
1142 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001143#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001144 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001145
John Koleszarc6b90392012-07-13 15:21:29 -07001146 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001147 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001148
John Koleszar6ad3b742012-11-06 12:02:42 -08001149 /* Once we've found an end-of-stream marker (--) we want to continue
1150 * shifting arguments but not consuming them.
1151 */
1152 if (eos_mark_found) {
1153 argj++;
1154 continue;
1155 } else if (!strcmp(*argj, "--")) {
1156 eos_mark_found = 1;
1157 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001158 }
1159
Adrian Granged2401802015-02-13 14:51:32 -08001160 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001161 config->out_fn = arg.val;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001162 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001163 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001164#if CONFIG_FP_MB_STATS
1165 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1166 config->fpmb_stats_fn = arg.val;
1167#endif
Johannb50e5182015-01-30 15:05:14 -08001168 } else if (arg_match(&arg, &use_webm, argi)) {
1169#if CONFIG_WEBM_IO
1170 config->write_webm = 1;
1171#else
1172 die("Error: --webm specified but webm is disabled.");
1173#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001174 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001175 config->write_webm = 0;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001176 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001177 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001178 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001179 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001180 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001181 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001182 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001183 config->cfg.g_h = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -07001184#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001185 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1186 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1187 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1188 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1189#endif
James Zerndba73762014-05-10 17:44:12 -07001190#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001191 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001192 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001193#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001194 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001195 config->cfg.g_timebase = arg_parse_rational(&arg);
1196 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001197 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001198 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001199 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001200 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -07001201 if (global->deadline == AOM_DL_REALTIME &&
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001202 config->cfg.g_lag_in_frames != 0) {
1203 warn("non-zero %s option ignored in realtime mode.\n", arg.name);
1204 config->cfg.g_lag_in_frames = 0;
1205 }
1206 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001207 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001208 } else if (arg_match(&arg, &resize_allowed, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001209 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001210 } else if (arg_match(&arg, &resize_width, argi)) {
1211 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
1212 } else if (arg_match(&arg, &resize_height, argi)) {
1213 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001214 } else if (arg_match(&arg, &resize_up_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001215 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001216 } else if (arg_match(&arg, &resize_down_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001217 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001218 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001219 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001220 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001221 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001222 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001223 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001224 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001225 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001226 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001227 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001228 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001229 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001230 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001231 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001232 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001233 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001234 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001235 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001236 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001237 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001238 if (global->passes < 2)
1239 warn("option %s ignored in one-pass mode.\n", arg.name);
1240 } else if (arg_match(&arg, &minsection_pct, argi)) {
1241 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1242
1243 if (global->passes < 2)
1244 warn("option %s ignored in one-pass mode.\n", arg.name);
1245 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1246 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1247
1248 if (global->passes < 2)
1249 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001250 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001251 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001252 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001253 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001254 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001255 config->cfg.kf_mode = AOM_KF_DISABLED;
1256#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001257 } else if (arg_match(&arg, &test16bitinternalarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001258 if (strcmp(global->codec->name, "av1") == 0 ||
1259 strcmp(global->codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001260 test_16bit_internal = 1;
1261 }
1262#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001263 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001264 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001265 for (i = 0; ctrl_args[i]; i++) {
1266 if (arg_match(&arg, ctrl_args[i], argi)) {
1267 int j;
1268 match = 1;
1269
1270 /* Point either to the next free element or the first
1271 * instance of this control.
1272 */
1273 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001274 if (ctrl_args_map != NULL &&
1275 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001276 break;
1277
1278 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001279 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001280 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001281 config->arg_ctrls[j][0] = ctrl_args_map[i];
1282 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001283 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001284 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001285 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001286 }
clang-format6c4d83e2016-08-08 19:03:30 -07001287 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001288 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001289 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001290#if CONFIG_AOM_HIGHBITDEPTH
1291 if (strcmp(global->codec->name, "av1") == 0 ||
1292 strcmp(global->codec->name, "av1") == 0) {
clang-format6c4d83e2016-08-08 19:03:30 -07001293 config->use_16bit_internal =
1294 test_16bit_internal | (config->cfg.g_profile > 1);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001295 }
1296#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001297 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001298}
1299
clang-format6c4d83e2016-08-08 19:03:30 -07001300#define FOREACH_STREAM(func) \
1301 do { \
1302 struct stream_state *stream; \
Tom Finegan249366b2013-11-25 12:05:19 -08001303 for (stream = streams; stream; stream = stream->next) { \
clang-format6c4d83e2016-08-08 19:03:30 -07001304 func; \
1305 } \
Tom Finegan44dd3272013-11-20 17:18:28 -08001306 } while (0)
John Koleszar9e50ed72012-02-15 12:39:38 -08001307
Alex Conversed66bd222014-02-21 10:52:09 -08001308static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001309 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001310 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001311 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001312
John Koleszar6ad3b742012-11-06 12:02:42 -08001313 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001314 fatal(
1315 "Stream %d: Specify stream dimensions with --width (-w) "
1316 " and --height (-h)",
1317 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001318
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001319 // Check that the codec bit depth is greater than the input bit depth.
1320 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001321 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001322 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1323 stream->index, (int)stream->config.cfg.g_bit_depth,
1324 stream->config.cfg.g_input_bit_depth);
1325 }
1326
John Koleszar6ad3b742012-11-06 12:02:42 -08001327 for (streami = stream; streami; streami = streami->next) {
1328 /* All streams require output files */
1329 if (!streami->config.out_fn)
1330 fatal("Stream %d: Output file is required (specify with -o)",
1331 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001332
John Koleszar6ad3b742012-11-06 12:02:42 -08001333 /* Check for two streams outputting to the same file */
1334 if (streami != stream) {
1335 const char *a = stream->config.out_fn;
1336 const char *b = streami->config.out_fn;
1337 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1338 fatal("Stream %d: duplicate output file (from stream %d)",
1339 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001340 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001341
1342 /* Check for two streams sharing a stats file. */
1343 if (streami != stream) {
1344 const char *a = stream->config.stats_fn;
1345 const char *b = streami->config.stats_fn;
1346 if (a && b && !strcmp(a, b))
1347 fatal("Stream %d: duplicate stats file (from stream %d)",
1348 streami->index, stream->index);
1349 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001350
1351#if CONFIG_FP_MB_STATS
1352 /* Check for two streams sharing a mb stats file. */
1353 if (streami != stream) {
1354 const char *a = stream->config.fpmb_stats_fn;
1355 const char *b = streami->config.fpmb_stats_fn;
1356 if (a && b && !strcmp(a, b))
1357 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1358 streami->index, stream->index);
1359 }
1360#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001361 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001362}
1363
clang-format6c4d83e2016-08-08 19:03:30 -07001364static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001365 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001366 if (!stream->config.cfg.g_w) {
1367 if (!stream->config.cfg.g_h)
1368 stream->config.cfg.g_w = w;
1369 else
1370 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1371 }
1372 if (!stream->config.cfg.g_h) {
1373 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1374 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001375}
1376
clang-format6c4d83e2016-08-08 19:03:30 -07001377static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001378 switch (t) {
1379 case FILE_TYPE_RAW: return "RAW";
1380 case FILE_TYPE_Y4M: return "Y4M";
1381 default: return "Other";
1382 }
1383}
1384
Yaowu Xuf883b422016-08-30 14:01:10 -07001385static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001386 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001387 case AOM_IMG_FMT_I420: return "I420";
1388 case AOM_IMG_FMT_I422: return "I422";
1389 case AOM_IMG_FMT_I444: return "I444";
1390 case AOM_IMG_FMT_I440: return "I440";
1391 case AOM_IMG_FMT_YV12: return "YV12";
1392 case AOM_IMG_FMT_I42016: return "I42016";
1393 case AOM_IMG_FMT_I42216: return "I42216";
1394 case AOM_IMG_FMT_I44416: return "I44416";
1395 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001396 default: return "Other";
1397 }
1398}
Ralph Giles061a16d2012-01-05 15:05:05 -06001399
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001400static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001401 struct AvxEncoderConfig *global,
1402 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001403#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001404 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001405
John Koleszar6ad3b742012-11-06 12:02:42 -08001406 if (stream->index == 0) {
1407 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001408 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001409 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001410 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001411 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001412 }
1413 if (stream->next || stream->index)
1414 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1415 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1416 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001417
John Koleszar6ad3b742012-11-06 12:02:42 -08001418 SHOW(g_usage);
1419 SHOW(g_threads);
1420 SHOW(g_profile);
1421 SHOW(g_w);
1422 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001423 SHOW(g_bit_depth);
1424 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001425 SHOW(g_timebase.num);
1426 SHOW(g_timebase.den);
1427 SHOW(g_error_resilient);
1428 SHOW(g_pass);
1429 SHOW(g_lag_in_frames);
1430 SHOW(rc_dropframe_thresh);
1431 SHOW(rc_resize_allowed);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001432 SHOW(rc_scaled_width);
1433 SHOW(rc_scaled_height);
John Koleszar6ad3b742012-11-06 12:02:42 -08001434 SHOW(rc_resize_up_thresh);
1435 SHOW(rc_resize_down_thresh);
1436 SHOW(rc_end_usage);
1437 SHOW(rc_target_bitrate);
1438 SHOW(rc_min_quantizer);
1439 SHOW(rc_max_quantizer);
1440 SHOW(rc_undershoot_pct);
1441 SHOW(rc_overshoot_pct);
1442 SHOW(rc_buf_sz);
1443 SHOW(rc_buf_initial_sz);
1444 SHOW(rc_buf_optimal_sz);
1445 SHOW(rc_2pass_vbr_bias_pct);
1446 SHOW(rc_2pass_vbr_minsection_pct);
1447 SHOW(rc_2pass_vbr_maxsection_pct);
1448 SHOW(kf_mode);
1449 SHOW(kf_min_dist);
1450 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001451}
1452
John Koleszar9e50ed72012-02-15 12:39:38 -08001453static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001454 struct AvxEncoderConfig *global,
1455 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001456 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001457 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001458
Yaowu Xuf883b422016-08-30 14:01:10 -07001459 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001460
John Koleszar6ad3b742012-11-06 12:02:42 -08001461 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001462
clang-format6c4d83e2016-08-08 19:03:30 -07001463 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001464
John Koleszar6ad3b742012-11-06 12:02:42 -08001465 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1466 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001467
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001468#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001469 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001470 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001471 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1472 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001473 }
James Zernc8e5a772016-02-11 18:53:50 -08001474#else
1475 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001476#endif
1477
1478 if (!stream->config.write_webm) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001479 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1480 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001481}
1482
John Koleszar9e50ed72012-02-15 12:39:38 -08001483static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001484 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001485 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001486
Yaowu Xuf883b422016-08-30 14:01:10 -07001487 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001488
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001489#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001490 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001491 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001492 }
1493#endif
1494
1495 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001496 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001497 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001498 stream->frames_out);
1499 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001500
John Koleszar6ad3b742012-11-06 12:02:42 -08001501 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001502}
1503
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001504static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001505 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001506 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001507 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001508 fatal("Failed to open statistics store");
1509 } else {
1510 if (!stats_open_mem(&stream->stats, pass))
1511 fatal("Failed to open statistics store");
1512 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001513
Pengchong Jinf349b072014-07-14 09:13:38 -07001514#if CONFIG_FP_MB_STATS
1515 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001516 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1517 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001518 fatal("Failed to open mb statistics store");
1519 } else {
1520 if (!stats_open_mem(&stream->fpmb_stats, pass))
1521 fatal("Failed to open mb statistics store");
1522 }
1523#endif
1524
John Koleszar6ad3b742012-11-06 12:02:42 -08001525 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001526 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1527 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001528 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001529 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001530#if CONFIG_FP_MB_STATS
1531 stream->config.cfg.rc_firstpass_mb_stats_in =
1532 stats_get(&stream->fpmb_stats);
1533#endif
1534 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001535
John Koleszar6ad3b742012-11-06 12:02:42 -08001536 stream->cx_time = 0;
1537 stream->nbytes = 0;
1538 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001539}
1540
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001541static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001542 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001543 int i;
1544 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001545
Yaowu Xuf883b422016-08-30 14:01:10 -07001546 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1547 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
1548#if CONFIG_AOM_HIGHBITDEPTH
1549 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001550#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001551
John Koleszar6ad3b742012-11-06 12:02:42 -08001552 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001553 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001554 &stream->config.cfg, flags);
1555 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001556
Yaowu Xuf883b422016-08-30 14:01:10 -07001557 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001558 * we're being clever to store the control IDs in an array. Real
1559 * applications will want to make use of the enumerations directly
1560 */
1561 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1562 int ctrl = stream->config.arg_ctrls[i][0];
1563 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001564 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001565 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001566
John Koleszar6ad3b742012-11-06 12:02:42 -08001567 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1568 }
1569
John Koleszar5ebe94f2012-12-23 07:20:10 -08001570#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001571 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001572 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1573 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
1574 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001575
Yaowu Xuf883b422016-08-30 14:01:10 -07001576#if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
1577 if (strcmp(global->codec->name, "av1") == 0) {
1578 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001579 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1580
Yaowu Xuf883b422016-08-30 14:01:10 -07001581 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001582 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1583 }
1584#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001585 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001586#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001587}
1588
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001589static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001590 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001591 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001592 aom_codec_pts_t frame_start, next_frame_start;
1593 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1594 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001595
clang-format6c4d83e2016-08-08 19:03:30 -07001596 frame_start =
1597 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1598 cfg->g_timebase.num / global->framerate.num;
1599 next_frame_start =
1600 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1601 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001602
clang-format6c4d83e2016-08-08 19:03:30 -07001603/* Scale if necessary */
Yaowu Xuf883b422016-08-30 14:01:10 -07001604#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001605 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001606 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001607 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001608 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001609 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1610 exit(EXIT_FAILURE);
1611 }
1612#if CONFIG_LIBYUV
1613 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001614 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001615 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001616 }
clang-format6c4d83e2016-08-08 19:03:30 -07001617 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001618 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1619 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1620 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1621 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1622 stream->img->stride[AOM_PLANE_Y] / 2,
1623 (uint16 *)stream->img->planes[AOM_PLANE_U],
1624 stream->img->stride[AOM_PLANE_U] / 2,
1625 (uint16 *)stream->img->planes[AOM_PLANE_V],
1626 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001627 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001628 img = stream->img;
1629#else
clang-format6c4d83e2016-08-08 19:03:30 -07001630 stream->encoder.err = 1;
1631 ctx_exit_on_error(&stream->encoder,
1632 "Stream %d: Failed to encode frame.\n"
1633 "Scaling disabled in this configuration. \n"
1634 "To enable, configure with --enable-libyuv\n",
1635 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001636#endif
1637 }
1638 }
1639#endif
John Koleszar34882b92012-03-01 12:50:40 -08001640 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001641 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001642 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1643 exit(EXIT_FAILURE);
1644 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001645#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001646 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001647 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001648 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001649 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001650 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1651 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1652 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1653 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1654 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1655 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001656 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001657 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001658#else
1659 stream->encoder.err = 1;
1660 ctx_exit_on_error(&stream->encoder,
1661 "Stream %d: Failed to encode frame.\n"
1662 "Scaling disabled in this configuration. \n"
1663 "To enable, configure with --enable-libyuv\n",
1664 stream->index);
1665#endif
John Koleszar34882b92012-03-01 12:50:40 -08001666 }
1667
Yaowu Xuf883b422016-08-30 14:01:10 -07001668 aom_usec_timer_start(&timer);
1669 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001670 (unsigned long)(next_frame_start - frame_start), 0,
1671 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001672 aom_usec_timer_mark(&timer);
1673 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001674 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1675 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001676}
1677
John Koleszar6ad3b742012-11-06 12:02:42 -08001678static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001679 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001680 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001681
Yaowu Xuf883b422016-08-30 14:01:10 -07001682 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001683 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1684 stream->counts[q]++;
1685 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001686}
1687
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001688static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001689 struct AvxEncoderConfig *global, int *got_data) {
1690 const aom_codec_cx_pkt_t *pkt;
1691 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1692 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001693
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001694 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001695 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001696 static size_t fsize = 0;
James Zernb6430362017-01-14 14:18:01 -08001697 static FileOffset ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001698
John Koleszar6ad3b742012-11-06 12:02:42 -08001699 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001700 case AOM_CODEC_CX_FRAME_PKT:
1701 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001702 stream->frames_out++;
1703 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001704 if (!global->quiet)
1705 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001706
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001707 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001708#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001709 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001710 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001711 }
1712#endif
1713 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001714 if (pkt->data.frame.partition_id <= 0) {
1715 ivf_header_pos = ftello(stream->file);
1716 fsize = pkt->data.frame.sz;
1717
Dmitry Kovalev373b0f92014-01-29 17:57:21 -08001718 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001719 } else {
1720 fsize += pkt->data.frame.sz;
1721
Yaowu Xuf883b422016-08-30 14:01:10 -07001722 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
James Zernb6430362017-01-14 14:18:01 -08001723 const FileOffset currpos = ftello(stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001724 fseeko(stream->file, ivf_header_pos, SEEK_SET);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001725 ivf_write_frame_size(stream->file, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001726 fseeko(stream->file, currpos, SEEK_SET);
1727 }
1728 }
1729
clang-format6c4d83e2016-08-08 19:03:30 -07001730 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1731 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001732 }
1733 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001734
John Koleszar9e50ed72012-02-15 12:39:38 -08001735 *got_data = 1;
John Koleszar5ebe94f2012-12-23 07:20:10 -08001736#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001737 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001738 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001739 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001740 if (stream->decoder.err) {
1741 warn_or_exit_on_error(&stream->decoder,
1742 global->test_decode == TEST_DECODE_FATAL,
1743 "Failed to decode frame %d in stream %d",
1744 stream->frames_out + 1, stream->index);
1745 stream->mismatch_seen = stream->frames_out + 1;
1746 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001747 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001748#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001749 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001750 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001751 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001752 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001753 pkt->data.twopass_stats.sz);
1754 stream->nbytes += pkt->data.raw.sz;
1755 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001756#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001757 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001758 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001759 pkt->data.firstpass_mb_stats.sz);
1760 stream->nbytes += pkt->data.raw.sz;
1761 break;
1762#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001763 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001764
1765 if (global->show_psnr) {
1766 int i;
1767
1768 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1769 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1770 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001771 if (!global->quiet)
1772 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001773 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1774 }
1775 stream->psnr_count++;
1776 }
1777
1778 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001779 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001780 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001781 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001782}
1783
clang-format6c4d83e2016-08-08 19:03:30 -07001784static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001785 int i;
1786 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001787
clang-format6c4d83e2016-08-08 19:03:30 -07001788 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001789
John Koleszar6ad3b742012-11-06 12:02:42 -08001790 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001791 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001792 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001793 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001794
John Koleszar6ad3b742012-11-06 12:02:42 -08001795 for (i = 0; i < 4; i++) {
1796 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1797 }
1798 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001799}
1800
John Koleszar25b6e9f2013-02-12 21:17:56 -08001801static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001802 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001803}
1804
clang-format6c4d83e2016-08-08 19:03:30 -07001805static void test_decode(struct stream_state *stream,
John Koleszarb3c350a2013-03-13 12:15:43 -07001806 enum TestDecodeFatality fatal,
Yaowu Xuf883b422016-08-30 14:01:10 -07001807 const AvxInterface *codec) {
1808 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001809
clang-format6c4d83e2016-08-08 19:03:30 -07001810 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001811
John Koleszarb3c350a2013-03-13 12:15:43 -07001812 /* Get the internal reference frame */
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001813 if (strcmp(codec->name, "vp8") == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001814 struct aom_ref_frame ref_enc, ref_dec;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001815 const unsigned int frame_width = (stream->config.cfg.g_w + 15) & ~15;
1816 const unsigned int frame_height = (stream->config.cfg.g_h + 15) & ~15;
1817 aom_img_alloc(&ref_enc.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001818 enc_img = ref_enc.img;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001819 aom_img_alloc(&ref_dec.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001820 dec_img = ref_dec.img;
1821
Yaowu Xuf883b422016-08-30 14:01:10 -07001822 ref_enc.frame_type = AOM_LAST_FRAME;
1823 ref_dec.frame_type = AOM_LAST_FRAME;
1824 aom_codec_control(&stream->encoder, AOM_COPY_REFERENCE, &ref_enc);
1825 aom_codec_control(&stream->decoder, AOM_COPY_REFERENCE, &ref_dec);
John Koleszarb3c350a2013-03-13 12:15:43 -07001826 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001827 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1828 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001829
Yaowu Xuf883b422016-08-30 14:01:10 -07001830#if CONFIG_AOM_HIGHBITDEPTH
1831 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1832 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1833 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1834 aom_image_t enc_hbd_img;
1835 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001836 enc_img.d_w, enc_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001837 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001838 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001839 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001840 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1841 aom_image_t dec_hbd_img;
1842 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001843 dec_img.d_w, dec_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001844 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001845 dec_img = dec_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001846 }
1847 }
1848#endif
John Koleszarb3c350a2013-03-13 12:15:43 -07001849 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001850 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001851 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001852
John Koleszarb3c350a2013-03-13 12:15:43 -07001853 if (!compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001854 int y[4], u[4], v[4];
Yaowu Xuf883b422016-08-30 14:01:10 -07001855#if CONFIG_AOM_HIGHBITDEPTH
1856 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001857 find_mismatch_high(&enc_img, &dec_img, y, u, v);
1858 } else {
1859 find_mismatch(&enc_img, &dec_img, y, u, v);
1860 }
1861#else
John Koleszarb3c350a2013-03-13 12:15:43 -07001862 find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001863#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001864 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001865 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001866 "Stream %d: Encode/decode mismatch on frame %d at"
1867 " Y[%d, %d] {%d/%d},"
1868 " U[%d, %d] {%d/%d},"
1869 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001870 stream->index, stream->frames_out, y[0], y[1], y[2],
1871 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 -08001872 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001873 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001874
Yaowu Xuf883b422016-08-30 14:01:10 -07001875 aom_img_free(&enc_img);
1876 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001877}
John Koleszarefd54f82012-02-13 16:52:18 -08001878
John Koleszar25b6e9f2013-02-12 21:17:56 -08001879static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001880 int64_t hours;
1881 int64_t mins;
1882 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001883
1884 if (etl >= 0) {
1885 hours = etl / 3600;
1886 etl -= hours * 3600;
1887 mins = etl / 60;
1888 etl -= mins * 60;
1889 secs = etl;
1890
clang-format6c4d83e2016-08-08 19:03:30 -07001891 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1892 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001893 } else {
1894 fprintf(stderr, "[%3s unknown] ", label);
1895 }
1896}
1897
Tom Finegan44dd3272013-11-20 17:18:28 -08001898int main(int argc, const char **argv_) {
1899 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001900 aom_image_t raw;
1901#if CONFIG_AOM_HIGHBITDEPTH
1902 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001903 int allocated_raw_shift = 0;
1904 int use_16bit_internal = 0;
1905 int input_shift = 0;
1906#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001907 int frame_avail, got_data;
1908
Yaowu Xuf883b422016-08-30 14:01:10 -07001909 struct AvxInputContext input;
1910 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001911 struct stream_state *streams = NULL;
1912 char **argv, **argi;
1913 uint64_t cx_time = 0;
1914 int stream_cnt = 0;
1915 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001916 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001917
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001918 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001919 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001920
clang-format6c4d83e2016-08-08 19:03:30 -07001921 if (argc < 3) usage_exit();
John Koleszar6ad3b742012-11-06 12:02:42 -08001922
1923 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001924 input.framerate.numerator = 30;
1925 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001926 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001927 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001928
1929 /* First parse the global configuration values, because we want to apply
1930 * other parameters on top of the default configuration provided by the
1931 * codec.
1932 */
1933 argv = argv_dup(argc - 1, argv_ + 1);
1934 parse_global_config(&global, argv);
1935
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001936 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001937 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1938 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1939 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1940 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1941 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001942 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001943
John Koleszar6ad3b742012-11-06 12:02:42 -08001944 {
1945 /* Now parse each stream's parameters. Using a local scope here
1946 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1947 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001948 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001949 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001950
John Koleszar6ad3b742012-11-06 12:02:42 -08001951 do {
1952 stream = new_stream(&global, stream);
1953 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001954 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001955 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001956 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001957
John Koleszarc6b90392012-07-13 15:21:29 -07001958 /* Check for unrecognized options */
1959 for (argi = argv; *argi; argi++)
1960 if (argi[0][0] == '-' && argi[0][1])
1961 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001962
clang-format6c4d83e2016-08-08 19:03:30 -07001963 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1964 &stream->config.cfg););
Tom Finegan249366b2013-11-25 12:05:19 -08001965
John Koleszarc6b90392012-07-13 15:21:29 -07001966 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001967 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001968
clang-format6c4d83e2016-08-08 19:03:30 -07001969 if (!input.filename) usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04001970
John Koleszar8dd82872013-05-06 11:01:35 -07001971 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001972 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001973
John Koleszar6ad3b742012-11-06 12:02:42 -08001974 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001975 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001976 int64_t estimated_time_left = -1;
1977 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001978 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001979
John Koleszar6ad3b742012-11-06 12:02:42 -08001980 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001981
John Koleszar6ad3b742012-11-06 12:02:42 -08001982 /* If the input file doesn't specify its w/h (raw files), try to get
1983 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001984 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001985 if (!input.width || !input.height) {
1986 FOREACH_STREAM({
1987 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1988 input.width = stream->config.cfg.g_w;
1989 input.height = stream->config.cfg.g_h;
1990 break;
1991 }
1992 });
1993 }
John Koleszarc6b90392012-07-13 15:21:29 -07001994
John Koleszar6ad3b742012-11-06 12:02:42 -08001995 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001996 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001997 fatal(
1998 "Specify stream dimensions with --width (-w) "
1999 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002000
2001 /* If input file does not specify bit-depth but input-bit-depth parameter
2002 * exists, assume that to be the input bit-depth. However, if the
2003 * input-bit-depth paramter does not exist, assume the input bit-depth
2004 * to be the same as the codec bit-depth.
2005 */
2006 if (!input.bit_depth) {
2007 FOREACH_STREAM({
2008 if (stream->config.cfg.g_input_bit_depth)
2009 input.bit_depth = stream->config.cfg.g_input_bit_depth;
2010 else
2011 input.bit_depth = stream->config.cfg.g_input_bit_depth =
2012 (int)stream->config.cfg.g_bit_depth;
2013 });
Yaowu Xuf883b422016-08-30 14:01:10 -07002014 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002015 } else {
clang-format6c4d83e2016-08-08 19:03:30 -07002016 FOREACH_STREAM(
2017 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002018 }
2019
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002020#if CONFIG_AOM_HIGHBITDEPTH
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002021 FOREACH_STREAM({
Thomas Daede8ec53b22016-09-19 13:24:51 -07002022 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2023 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2024 was selected. */
2025 switch (stream->config.cfg.g_profile) {
2026 case 0:
2027 stream->config.cfg.g_profile = 1;
2028 profile_updated = 1;
2029 break;
2030 case 2:
2031 stream->config.cfg.g_profile = 3;
2032 profile_updated = 1;
2033 break;
2034 default: break;
2035 }
2036 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002037 /* Automatically set the codec bit depth to match the input bit depth.
2038 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002039 if (stream->config.cfg.g_input_bit_depth >
2040 (unsigned int)stream->config.cfg.g_bit_depth) {
2041 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2042 }
2043 if (stream->config.cfg.g_bit_depth > 8) {
2044 switch (stream->config.cfg.g_profile) {
2045 case 0:
2046 stream->config.cfg.g_profile = 2;
2047 profile_updated = 1;
2048 break;
2049 case 1:
2050 stream->config.cfg.g_profile = 3;
2051 profile_updated = 1;
2052 break;
2053 default: break;
2054 }
2055 }
2056 if (stream->config.cfg.g_profile > 1) {
2057 stream->config.use_16bit_internal = 1;
2058 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002059 if (profile_updated && !global.quiet) {
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002060 fprintf(stderr,
2061 "Warning: automatically upgrading to profile %d to "
2062 "match input format.\n",
2063 stream->config.cfg.g_profile);
2064 }
2065 });
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002066#else
2067 FOREACH_STREAM({
2068 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2069 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2070 was selected. */
2071 switch (stream->config.cfg.g_profile) {
2072 case 0:
2073 stream->config.cfg.g_profile = 1;
2074 profile_updated = 1;
2075 break;
2076 case 2:
2077 stream->config.cfg.g_profile = 3;
2078 profile_updated = 1;
2079 break;
2080 default: break;
2081 }
2082 }
Urvang Joshi51c048e2017-02-16 12:43:52 -08002083 if (profile_updated && !global.quiet) {
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002084 fprintf(stderr,
2085 "Warning: automatically upgrading to profile %d to "
2086 "match input format.\n",
2087 stream->config.cfg.g_profile);
2088 }
2089 });
2090#endif
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002091
Tom Finegan00a35aa2013-11-14 12:37:42 -08002092 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
Alex Conversed66bd222014-02-21 10:52:09 -08002093 FOREACH_STREAM(validate_stream_config(stream, &global));
John Koleszar77e6b452010-11-03 13:58:40 -04002094
John Koleszar6ad3b742012-11-06 12:02:42 -08002095 /* Ensure that --passes and --pass are consistent. If --pass is set and
2096 * --passes=2, ensure --fpf was set.
2097 */
2098 if (global.pass && global.passes == 2)
clang-format6c4d83e2016-08-08 19:03:30 -07002099 FOREACH_STREAM({
2100 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002101 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002102 " and --passes=2\n",
2103 stream->index, global.pass);
2104 });
John Koleszar3245d462010-06-10 17:10:12 -04002105
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002106#if !CONFIG_WEBM_IO
2107 FOREACH_STREAM({
Ronald S. Bultje39775072015-12-16 13:35:43 -05002108 if (stream->config.write_webm) {
2109 stream->config.write_webm = 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002110 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002111 "aomenc was compiled without WebM container support."
clang-format6c4d83e2016-08-08 19:03:30 -07002112 "Producing IVF output");
Ronald S. Bultje39775072015-12-16 13:35:43 -05002113 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002114 });
2115#endif
2116
John Koleszar6ad3b742012-11-06 12:02:42 -08002117 /* Use the frame rate from the file only if none was specified
2118 * on the command-line.
2119 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002120 if (!global.have_framerate) {
2121 global.framerate.num = input.framerate.numerator;
2122 global.framerate.den = input.framerate.denominator;
Ronald S. Bultje9134e9f2016-01-18 14:03:45 -05002123 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
2124 stream->config.cfg.g_timebase.num = global.framerate.den);
Tom Finegan00a35aa2013-11-14 12:37:42 -08002125 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002126
John Koleszar6ad3b742012-11-06 12:02:42 -08002127 /* Show configuration */
2128 if (global.verbose && pass == 0)
2129 FOREACH_STREAM(show_stream_config(stream, &global, &input));
2130
2131 if (pass == (global.pass ? global.pass - 1 : 0)) {
2132 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002133 /*The Y4M reader does its own allocation.
2134 Just initialize this here to avoid problems if we never read any
2135 frames.*/
2136 memset(&raw, 0, sizeof(raw));
2137 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002138 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002139
clang-format6c4d83e2016-08-08 19:03:30 -07002140 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
2141 &stream->config.cfg, &global.framerate));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002142 }
2143
John Koleszar6ad3b742012-11-06 12:02:42 -08002144 FOREACH_STREAM(setup_pass(stream, &global, pass));
clang-format6c4d83e2016-08-08 19:03:30 -07002145 FOREACH_STREAM(
2146 open_output_file(stream, &global, &input.pixel_aspect_ratio));
John Koleszar6ad3b742012-11-06 12:02:42 -08002147 FOREACH_STREAM(initialize_encoder(stream, &global));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002148
Yaowu Xuf883b422016-08-30 14:01:10 -07002149#if CONFIG_AOM_HIGHBITDEPTH
2150 if (strcmp(global.codec->name, "av1") == 0 ||
2151 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002152 // Check to see if at least one stream uses 16 bit internal.
2153 // Currently assume that the bit_depths for all streams using
2154 // highbitdepth are the same.
2155 FOREACH_STREAM({
2156 if (stream->config.use_16bit_internal) {
2157 use_16bit_internal = 1;
2158 }
2159 if (stream->config.cfg.g_profile == 0) {
2160 input_shift = 0;
2161 } else {
2162 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07002163 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002164 }
2165 });
2166 }
2167#endif
2168
John Koleszarc6b90392012-07-13 15:21:29 -07002169 frame_avail = 1;
2170 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002171
John Koleszarc6b90392012-07-13 15:21:29 -07002172 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002173 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002174
John Koleszar6ad3b742012-11-06 12:02:42 -08002175 if (!global.limit || frames_in < global.limit) {
2176 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002177
clang-format6c4d83e2016-08-08 19:03:30 -07002178 if (frame_avail) frames_in++;
2179 seen_frames =
2180 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002181
John Koleszar6ad3b742012-11-06 12:02:42 -08002182 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002183 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002184 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2185
John Koleszar6ad3b742012-11-06 12:02:42 -08002186 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002187 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2188 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002189 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002190 fprintf(stderr, "frame %4d ", frames_in);
2191
clang-format6c4d83e2016-08-08 19:03:30 -07002192 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002193 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002194 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002195 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002196 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002197 }
2198
hui su251e1512016-10-03 15:48:43 -07002199 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002200 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002201 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002202
John Koleszar6ad3b742012-11-06 12:02:42 -08002203 if (frames_in > global.skip_frames) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002204#if CONFIG_AOM_HIGHBITDEPTH
2205 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002206 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2207 assert(use_16bit_internal);
2208 // Input bit depth and stream bit depth do not match, so up
2209 // shift frame to stream bit depth
2210 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002211 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002212 input.width, input.height, 32);
2213 allocated_raw_shift = 1;
2214 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002215 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002216 frame_to_encode = &raw_shift;
2217 } else {
2218 frame_to_encode = &raw;
2219 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002220 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002221 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002222 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002223 FOREACH_STREAM({
2224 if (stream->config.use_16bit_internal)
2225 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002226 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002227 else
2228 assert(0);
2229 });
2230 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002231 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002232 FOREACH_STREAM(encode_frame(stream, &global,
2233 frame_avail ? frame_to_encode : NULL,
2234 frames_in));
2235 }
2236#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002237 aom_usec_timer_start(&timer);
clang-format6c4d83e2016-08-08 19:03:30 -07002238 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
John Koleszar6ad3b742012-11-06 12:02:42 -08002239 frames_in));
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002240#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002241 aom_usec_timer_mark(&timer);
2242 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002243
John Koleszar6ad3b742012-11-06 12:02:42 -08002244 FOREACH_STREAM(update_quantizer_histogram(stream));
John Koleszarc6b90392012-07-13 15:21:29 -07002245
John Koleszar0ea50ce2010-05-18 11:58:33 -04002246 got_data = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002247 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002248
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002249 if (!got_data && input.length && streams != NULL &&
2250 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002251 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002252 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002253 int64_t remaining;
2254 int64_t rate;
2255
2256 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002257 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002258
2259 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002260 remaining = 1000 * (global.limit - global.skip_frames -
2261 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002262 } else {
James Zerne3578af2014-04-17 10:47:08 -07002263 const int64_t input_pos = ftello(input.file);
2264 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002265 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002266
2267 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002268 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002269 }
2270
clang-format6c4d83e2016-08-08 19:03:30 -07002271 average_rate =
2272 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002273 estimated_time_left = average_rate ? remaining / average_rate : -1;
2274 }
2275
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002276 if (got_data && global.test_decode != TEST_DECODE_OFF)
John Koleszarb3c350a2013-03-13 12:15:43 -07002277 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
John Koleszarc6b90392012-07-13 15:21:29 -07002278 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002279
John Koleszarc6b90392012-07-13 15:21:29 -07002280 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002281 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002282 }
2283
clang-format6c4d83e2016-08-08 19:03:30 -07002284 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002285
Deb Mukherjeea160d722014-09-30 21:56:33 -07002286 if (!global.quiet) {
clang-format6c4d83e2016-08-08 19:03:30 -07002287 FOREACH_STREAM(fprintf(
2288 stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2289 "b/f %7" PRId64 "b/s"
2290 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2291 pass + 1, global.passes, frames_in, stream->frames_out,
2292 (int64_t)stream->nbytes,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002293 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
clang-format6c4d83e2016-08-08 19:03:30 -07002294 seen_frames
2295 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2296 global.framerate.den / seen_frames
2297 : 0,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002298 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2299 stream->cx_time > 9999999 ? "ms" : "us",
2300 usec_to_fps(stream->cx_time, seen_frames)));
2301 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002302
Deb Mukherjeea160d722014-09-30 21:56:33 -07002303 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002304 if (global.codec->fourcc == AV1_FOURCC) {
Deb Mukherjeea160d722014-09-30 21:56:33 -07002305 FOREACH_STREAM(
2306 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2307 } else {
2308 FOREACH_STREAM(show_psnr(stream, 255.0));
2309 }
2310 }
John Koleszarc6b90392012-07-13 15:21:29 -07002311
Yaowu Xuf883b422016-08-30 14:01:10 -07002312 FOREACH_STREAM(aom_codec_destroy(&stream->encoder));
John Koleszar6ad3b742012-11-06 12:02:42 -08002313
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002314 if (global.test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002315 FOREACH_STREAM(aom_codec_destroy(&stream->decoder));
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002316 }
2317
John Koleszar6ad3b742012-11-06 12:02:42 -08002318 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002319
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002320 if (global.test_decode == TEST_DECODE_FATAL) {
2321 FOREACH_STREAM(res |= stream->mismatch_seen);
2322 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002323 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002324
John Koleszar6ad3b742012-11-06 12:02:42 -08002325 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
John Koleszarc6b90392012-07-13 15:21:29 -07002326
Pengchong Jinf349b072014-07-14 09:13:38 -07002327#if CONFIG_FP_MB_STATS
2328 FOREACH_STREAM(stats_close(&stream->fpmb_stats, global.passes - 1));
2329#endif
2330
clang-format6c4d83e2016-08-08 19:03:30 -07002331 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002332 }
2333
John Koleszar6ad3b742012-11-06 12:02:42 -08002334 if (global.show_q_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002335 FOREACH_STREAM(
2336 show_q_histogram(stream->counts, global.show_q_hist_buckets));
John Koleszar6ad3b742012-11-06 12:02:42 -08002337
2338 if (global.show_rate_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002339 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
John Koleszar6ad3b742012-11-06 12:02:42 -08002340 global.show_rate_hist_buckets));
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08002341 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
John Koleszar6ad3b742012-11-06 12:02:42 -08002342
Ronald S. Bultje31214972012-10-11 10:13:48 -07002343#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002344 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2345 * to match some existing utilities.
2346 */
Yaowu Xu47e784e2013-10-16 16:29:28 -07002347 if (!(global.pass == 1 && global.passes == 2))
2348 FOREACH_STREAM({
2349 FILE *f = fopen("opsnr.stt", "a");
2350 if (stream->mismatch_seen) {
2351 fprintf(f, "First mismatch occurred in frame %d\n",
2352 stream->mismatch_seen);
2353 } else {
2354 fprintf(f, "No mismatch detected in recon buffers\n");
2355 }
2356 fclose(f);
2357 });
Ronald S. Bultje31214972012-10-11 10:13:48 -07002358#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002359
Yaowu Xuf883b422016-08-30 14:01:10 -07002360#if CONFIG_AOM_HIGHBITDEPTH
2361 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002362#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002363 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002364 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002365 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002366 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002367}