blob: 31625a2dd56624f9030d436352c218155d14c145 [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar0ea50ce2010-05-18 11:58:33 -040010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#include "./aomenc.h"
13#include "./aom_config.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040014
Tom Finegan00a35aa2013-11-14 12:37:42 -080015#include <assert.h>
16#include <limits.h>
Tom Finegan78cb2e62013-11-07 21:28:45 -080017#include <math.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080018#include <stdarg.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040019#include <stdio.h>
20#include <stdlib.h>
John Koleszar0ea50ce2010-05-18 11:58:33 -040021#include <string.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080022
James Zern58058812014-08-15 21:00:31 -070023#if CONFIG_LIBYUV
24#include "third_party/libyuv/include/libyuv/scale.h"
25#endif
26
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom/aom_encoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080028#if CONFIG_DECODERS
Yaowu Xuf883b422016-08-30 14:01:10 -070029#include "aom/aom_decoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080030#endif
John Koleszar6ad3b742012-11-06 12:02:42 -080031
Tom Finegan00a35aa2013-11-14 12:37:42 -080032#include "./args.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080033#include "./ivfenc.h"
Tom Finegan7a691f12014-02-10 14:55:25 -080034#include "./tools_common.h"
Tom Finegan03848f52013-11-05 10:02:18 -080035
Yaowu Xuf883b422016-08-30 14:01:10 -070036#if CONFIG_AV1_ENCODER
37#include "aom/aomcx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080038#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070039#if CONFIG_AV1_DECODER
40#include "aom/aomdx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080041#endif
42
Yaowu Xuf883b422016-08-30 14:01:10 -070043#include "./aomstats.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080044#include "./rate_hist.h"
Tom Finegan249366b2013-11-25 12:05:19 -080045#include "./warnings.h"
Thomas Daedec0dca3c2016-02-25 17:23:17 -080046#include "aom/aom_integer.h"
47#include "aom_ports/aom_timer.h"
48#include "aom_ports/mem_ops.h"
James Zerndba73762014-05-10 17:44:12 -070049#if CONFIG_WEBM_IO
Tom Finegan78cb2e62013-11-07 21:28:45 -080050#include "./webmenc.h"
James Zerndba73762014-05-10 17:44:12 -070051#endif
Tom Finegan78cb2e62013-11-07 21:28:45 -080052#include "./y4minput.h"
Tom Finegan03848f52013-11-05 10:02:18 -080053
John Koleszar6291dd42012-06-19 21:05:36 -070054/* Swallow warnings about unused results of fread/fwrite */
clang-format6c4d83e2016-08-08 19:03:30 -070055static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
John Koleszar6ad3b742012-11-06 12:02:42 -080056 return fread(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070057}
58#define fread wrap_fread
59
60static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080061 FILE *stream) {
62 return fwrite(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070063}
64#define fwrite wrap_fwrite
65
John Koleszar0ea50ce2010-05-18 11:58:33 -040066static const char *exec_name;
67
Yaowu Xuf883b422016-08-30 14:01:10 -070068static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080069 const char *s, va_list ap) {
John Koleszarc6b90392012-07-13 15:21:29 -070070 if (ctx->err) {
Yaowu Xuf883b422016-08-30 14:01:10 -070071 const char *detail = aom_codec_error_detail(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -040072
John Koleszar6ad3b742012-11-06 12:02:42 -080073 vfprintf(stderr, s, ap);
Yaowu Xuf883b422016-08-30 14:01:10 -070074 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -040075
clang-format6c4d83e2016-08-08 19:03:30 -070076 if (detail) fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -040077
clang-format6c4d83e2016-08-08 19:03:30 -070078 if (fatal) exit(EXIT_FAILURE);
John Koleszarc6b90392012-07-13 15:21:29 -070079 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040080}
81
Yaowu Xuf883b422016-08-30 14:01:10 -070082static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080083 va_list ap;
84
85 va_start(ap, s);
86 warn_or_exit_on_errorv(ctx, 1, s, ap);
87 va_end(ap);
88}
89
Yaowu Xuf883b422016-08-30 14:01:10 -070090static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
Ronald S. Bultje9837bf42013-02-15 16:31:02 -080091 const char *s, ...) {
92 va_list ap;
93
94 va_start(ap, s);
95 warn_or_exit_on_errorv(ctx, fatal, s, ap);
96 va_end(ap);
97}
98
Yaowu Xuf883b422016-08-30 14:01:10 -070099static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800100 FILE *f = input_ctx->file;
101 y4m_input *y4m = &input_ctx->y4m;
John Koleszarc6b90392012-07-13 15:21:29 -0700102 int shortread = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400103
Tom Finegan00a35aa2013-11-14 12:37:42 -0800104 if (input_ctx->file_type == FILE_TYPE_Y4M) {
clang-format6c4d83e2016-08-08 19:03:30 -0700105 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
John Koleszarc6b90392012-07-13 15:21:29 -0700106 } else {
Tom Finegan00a35aa2013-11-14 12:37:42 -0800107 shortread = read_yuv_frame(input_ctx, img);
John Koleszarc6b90392012-07-13 15:21:29 -0700108 }
109
110 return !shortread;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400111}
112
James Zern5c337fd2015-05-09 10:42:58 -0700113static int file_is_y4m(const char detect[4]) {
John Koleszarc6b90392012-07-13 15:21:29 -0700114 if (memcmp(detect, "YUV4", 4) == 0) {
115 return 1;
116 }
117 return 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400118}
119
James Zern5c337fd2015-05-09 10:42:58 -0700120static int fourcc_is_ivf(const char detect[4]) {
Alex Converse64b89f12014-01-06 16:29:09 -0800121 if (memcmp(detect, "DKIF", 4) == 0) {
122 return 1;
123 }
124 return 0;
125}
John Koleszardc666302010-10-20 12:05:48 -0400126
clang-format6c4d83e2016-08-08 19:03:30 -0700127static const arg_def_t debugmode =
128 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
129static const arg_def_t outputfile =
130 ARG_DEF("o", "output", 1, "Output filename");
131static const arg_def_t use_yv12 =
132 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
133static const arg_def_t use_i420 =
134 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
135static const arg_def_t use_i422 =
136 ARG_DEF(NULL, "i422", 0, "Input file is I422");
137static const arg_def_t use_i444 =
138 ARG_DEF(NULL, "i444", 0, "Input file is I444");
139static const arg_def_t use_i440 =
140 ARG_DEF(NULL, "i440", 0, "Input file is I440");
141static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
142static const arg_def_t passes =
143 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
144static const arg_def_t pass_arg =
145 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
146static const arg_def_t fpf_name =
147 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700148#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700149static const arg_def_t fpmbf_name =
150 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
Pengchong Jinf349b072014-07-14 09:13:38 -0700151#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700152static const arg_def_t limit =
153 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
154static const arg_def_t skip =
155 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
156static const arg_def_t deadline =
157 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
158static const arg_def_t best_dl =
159 ARG_DEF(NULL, "best", 0, "Use Best Quality Deadline");
160static const arg_def_t good_dl =
161 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
162static const arg_def_t rt_dl =
163 ARG_DEF(NULL, "rt", 0, "Use Realtime Quality Deadline");
164static const arg_def_t quietarg =
165 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
166static const arg_def_t verbosearg =
167 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
168static const arg_def_t psnrarg =
169 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
Tom Finegan49dc9ca2013-11-21 16:46:40 -0800170
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800171static const struct arg_enum_list test_decode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700172 { "off", TEST_DECODE_OFF },
173 { "fatal", TEST_DECODE_FATAL },
174 { "warn", TEST_DECODE_WARN },
175 { NULL, 0 }
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800176};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700177static const arg_def_t recontest = ARG_DEF_ENUM(
178 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
clang-format6c4d83e2016-08-08 19:03:30 -0700179static const arg_def_t framerate =
180 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
181static const arg_def_t use_webm =
182 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
183static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
184static const arg_def_t out_part =
185 ARG_DEF("P", "output-partitions", 0,
186 "Makes encoder output partitions. Requires IVF output!");
187static const arg_def_t q_hist_n =
188 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
189static const arg_def_t rate_hist_n =
190 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
191static const arg_def_t disable_warnings =
192 ARG_DEF(NULL, "disable-warnings", 0,
193 "Disable warnings about potentially incorrect encode settings.");
194static const arg_def_t disable_warning_prompt =
195 ARG_DEF("y", "disable-warning-prompt", 0,
196 "Display warnings, but do not prompt user to continue.");
Alex Conversed66bd222014-02-21 10:52:09 -0800197
Yaowu Xuf883b422016-08-30 14:01:10 -0700198#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700199static const arg_def_t test16bitinternalarg = ARG_DEF(
200 NULL, "test-16bit-internal", 0, "Force use of 16 bit internal buffer");
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400201
202static const struct arg_enum_list bitdepth_enum[] = {
203 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
204};
205
206static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
207 "b", "bit-depth", 1,
208 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
209 bitdepth_enum);
210static const arg_def_t inbitdeptharg =
211 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700212#endif
Tom Finegan44dd3272013-11-20 17:18:28 -0800213
clang-format6c4d83e2016-08-08 19:03:30 -0700214static const arg_def_t *main_args[] = { &debugmode,
215 &outputfile,
216 &codecarg,
217 &passes,
218 &pass_arg,
219 &fpf_name,
220 &limit,
221 &skip,
222 &deadline,
223 &best_dl,
224 &good_dl,
225 &rt_dl,
226 &quietarg,
227 &verbosearg,
228 &psnrarg,
229 &use_webm,
230 &use_ivf,
231 &out_part,
232 &q_hist_n,
233 &rate_hist_n,
234 &disable_warnings,
235 &disable_warning_prompt,
236 &recontest,
237 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400238
clang-format6c4d83e2016-08-08 19:03:30 -0700239static const arg_def_t usage =
240 ARG_DEF("u", "usage", 1, "Usage profile number to use");
241static const arg_def_t threads =
242 ARG_DEF("t", "threads", 1, "Max number of threads to use");
243static const arg_def_t profile =
244 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700245static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
246static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
James Zerndba73762014-05-10 17:44:12 -0700247#if CONFIG_WEBM_IO
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700248static const struct arg_enum_list stereo_mode_enum[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700249 { "mono", STEREO_FORMAT_MONO },
250 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
251 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
252 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
253 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
254 { NULL, 0 }
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700255};
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700256static const arg_def_t stereo_mode = ARG_DEF_ENUM(
257 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
James Zerndba73762014-05-10 17:44:12 -0700258#endif
Deb Mukherjeea349ee32014-10-13 14:27:53 -0700259static const arg_def_t timebase = ARG_DEF(
260 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
clang-format6c4d83e2016-08-08 19:03:30 -0700261static const arg_def_t error_resilient =
262 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
263static const arg_def_t lag_in_frames =
264 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400265
clang-format6c4d83e2016-08-08 19:03:30 -0700266static const arg_def_t *global_args[] = { &use_yv12,
267 &use_i420,
268 &use_i422,
269 &use_i444,
270 &use_i440,
271 &usage,
272 &threads,
273 &profile,
274 &width,
275 &height,
James Zerndba73762014-05-10 17:44:12 -0700276#if CONFIG_WEBM_IO
clang-format6c4d83e2016-08-08 19:03:30 -0700277 &stereo_mode,
James Zerndba73762014-05-10 17:44:12 -0700278#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700279 &timebase,
280 &framerate,
281 &error_resilient,
Yaowu Xuf883b422016-08-30 14:01:10 -0700282#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -0700283 &test16bitinternalarg,
Nathan E. Egged1b239c2016-07-12 18:03:18 -0400284 &bitdeptharg,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700285#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700286 &lag_in_frames,
287 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400288
clang-format6c4d83e2016-08-08 19:03:30 -0700289static const arg_def_t dropframe_thresh =
290 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
291static const arg_def_t resize_allowed =
292 ARG_DEF(NULL, "resize-allowed", 1, "Spatial resampling enabled (bool)");
293static const arg_def_t resize_width =
294 ARG_DEF(NULL, "resize-width", 1, "Width of encoded frame");
295static const arg_def_t resize_height =
296 ARG_DEF(NULL, "resize-height", 1, "Height of encoded frame");
297static const arg_def_t resize_up_thresh =
298 ARG_DEF(NULL, "resize-up", 1, "Upscale threshold (buf %)");
299static const arg_def_t resize_down_thresh =
300 ARG_DEF(NULL, "resize-down", 1, "Downscale threshold (buf %)");
Yaowu Xuf883b422016-08-30 14:01:10 -0700301static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
302 { "cbr", AOM_CBR },
303 { "cq", AOM_CQ },
304 { "q", AOM_Q },
clang-format6c4d83e2016-08-08 19:03:30 -0700305 { NULL, 0 } };
306static const arg_def_t end_usage =
307 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
308static const arg_def_t target_bitrate =
309 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
310static const arg_def_t min_quantizer =
311 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
312static const arg_def_t max_quantizer =
313 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
314static const arg_def_t undershoot_pct =
315 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
316static const arg_def_t overshoot_pct =
317 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
318static const arg_def_t buf_sz =
319 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
320static const arg_def_t buf_initial_sz =
321 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
322static const arg_def_t buf_optimal_sz =
323 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
John Koleszarc6b90392012-07-13 15:21:29 -0700324static const arg_def_t *rc_args[] = {
clang-format6c4d83e2016-08-08 19:03:30 -0700325 &dropframe_thresh, &resize_allowed, &resize_width, &resize_height,
326 &resize_up_thresh, &resize_down_thresh, &end_usage, &target_bitrate,
327 &min_quantizer, &max_quantizer, &undershoot_pct, &overshoot_pct,
328 &buf_sz, &buf_initial_sz, &buf_optimal_sz, NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -0400329};
330
clang-format6c4d83e2016-08-08 19:03:30 -0700331static const arg_def_t bias_pct =
332 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
333static const arg_def_t minsection_pct =
334 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
335static const arg_def_t maxsection_pct =
336 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
337static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
338 &maxsection_pct, NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400339
clang-format6c4d83e2016-08-08 19:03:30 -0700340static const arg_def_t kf_min_dist =
341 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
342static const arg_def_t kf_max_dist =
343 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
344static const arg_def_t kf_disabled =
345 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
346static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
347 NULL };
John Koleszar0ea50ce2010-05-18 11:58:33 -0400348
clang-format6c4d83e2016-08-08 19:03:30 -0700349static const arg_def_t noise_sens =
350 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
351static const arg_def_t sharpness =
352 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
353static const arg_def_t static_thresh =
354 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
355static const arg_def_t auto_altref =
356 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
357static const arg_def_t arnr_maxframes =
358 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
359static const arg_def_t arnr_strength =
360 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
361static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1, "AltRef type");
John Koleszarb0da9b32010-12-17 09:43:39 -0500362static const struct arg_enum_list tuning_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700363 { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
John Koleszarb0da9b32010-12-17 09:43:39 -0500364};
clang-format6c4d83e2016-08-08 19:03:30 -0700365static const arg_def_t tune_ssim =
366 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
367static const arg_def_t cq_level =
368 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
369static const arg_def_t max_intra_rate_pct =
370 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
John Koleszara9c75972012-11-08 17:09:30 -0800371
Yaowu Xuf883b422016-08-30 14:01:10 -0700372#if CONFIG_AV1_ENCODER
373static const arg_def_t cpu_used_av1 =
clang-format6c4d83e2016-08-08 19:03:30 -0700374 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (-8..8)");
375static const arg_def_t tile_cols =
376 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
377static const arg_def_t tile_rows =
378 ARG_DEF(NULL, "tile-rows", 1,
379 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
380static const arg_def_t lossless =
381 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700382#if CONFIG_AOM_QM
383static const arg_def_t enable_qm =
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800384 ARG_DEF(NULL, "enable-qm", 1,
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700385 "Enable quantisation matrices (0: false (default), 1: true)");
386static const arg_def_t qm_min = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800387 NULL, "qm-min", 1, "Min quant matrix flatness (0..15), default is 8");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700388static const arg_def_t qm_max = ARG_DEF(
Yaowu Xu9dcf5602016-11-09 15:11:22 -0800389 NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
Yaowu Xu0818a7c2016-08-11 09:39:47 -0700390#endif
Thomas Daviesaf6df172016-11-09 14:04:18 +0000391#if CONFIG_TILE_GROUPS
Yaowu Xu859a5272016-11-10 15:32:21 -0800392static const arg_def_t num_tg = ARG_DEF(
393 NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000394static const arg_def_t mtu_size =
395 ARG_DEF(NULL, "mtu-size", 1,
Yaowu Xu859a5272016-11-10 15:32:21 -0800396 "MTU size for a tile group, default is 0 (no MTU targeting), "
397 "overrides maximum number of tile groups");
Thomas Daviesaf6df172016-11-09 14:04:18 +0000398#endif
Yaowu Xud59fb482016-09-30 15:39:53 -0700399static const arg_def_t frame_parallel_decoding =
400 ARG_DEF(NULL, "frame-parallel", 1,
401 "Enable frame parallel decodability features "
402 "(0: false (default), 1: true)");
Thomase28d92b2016-09-20 12:07:11 +0100403#if CONFIG_DELTA_Q
Thomas Daviesf6936102016-09-05 16:51:31 +0100404static const arg_def_t aq_mode = ARG_DEF(
405 NULL, "aq-mode", 1,
406 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100407 "3: cyclic refresh, 4: delta quant)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200408#else
Thomase28d92b2016-09-20 12:07:11 +0100409static const arg_def_t aq_mode = ARG_DEF(
410 NULL, "aq-mode", 1,
411 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
Thomas Daviesf6936102016-09-05 16:51:31 +0100412 "3: cyclic refresh)");
Arild Fuldseth07441162016-08-15 15:07:52 +0200413#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700414static const arg_def_t frame_periodic_boost =
415 ARG_DEF(NULL, "frame-boost", 1,
416 "Enable frame periodic boost (0: off (default), 1: on)");
James Zernf82cfc22015-06-05 16:07:20 -0700417static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
418 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
clang-format6c4d83e2016-08-08 19:03:30 -0700419static const arg_def_t max_inter_rate_pct =
420 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
Debargha Mukherjee98526432015-04-01 16:39:06 -0700421static const arg_def_t min_gf_interval = ARG_DEF(
422 NULL, "min-gf-interval", 1,
423 "min gf/arf frame interval (default 0, indicating in-built behavior)");
424static const arg_def_t max_gf_interval = ARG_DEF(
425 NULL, "max-gf-interval", 1,
426 "max gf/arf frame interval (default 0, indicating in-built behavior)");
James Zern5248d712014-01-14 17:56:45 -0800427
Yaowu Xufc996362015-02-10 15:03:05 -0800428static const struct arg_enum_list color_space_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700429 { "unknown", AOM_CS_UNKNOWN },
430 { "bt601", AOM_CS_BT_601 },
431 { "bt709", AOM_CS_BT_709 },
432 { "smpte170", AOM_CS_SMPTE_170 },
433 { "smpte240", AOM_CS_SMPTE_240 },
434 { "bt2020", AOM_CS_BT_2020 },
435 { "reserved", AOM_CS_RESERVED },
436 { "sRGB", AOM_CS_SRGB },
Yaowu Xufc996362015-02-10 15:03:05 -0800437 { NULL, 0 }
438};
439
clang-format6c4d83e2016-08-08 19:03:30 -0700440static const arg_def_t input_color_space =
441 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
442 color_space_enum);
Yaowu Xufc996362015-02-10 15:03:05 -0800443
Alex Converse219f6452014-08-06 11:01:18 -0700444static const struct arg_enum_list tune_content_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700445 { "default", AOM_CONTENT_DEFAULT },
446 { "screen", AOM_CONTENT_SCREEN },
clang-format6c4d83e2016-08-08 19:03:30 -0700447 { NULL, 0 }
Alex Converse219f6452014-08-06 11:01:18 -0700448};
449
450static const arg_def_t tune_content = ARG_DEF_ENUM(
451 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
hui su82331e02015-08-16 18:21:56 -0700452#endif
Alex Converse219f6452014-08-06 11:01:18 -0700453
Yaowu Xuf883b422016-08-30 14:01:10 -0700454#if CONFIG_AV1_ENCODER
Geza Lore454989f2016-03-24 13:56:05 +0000455#if CONFIG_EXT_PARTITION
456static const struct arg_enum_list superblock_size_enum[] = {
Yaowu Xuf883b422016-08-30 14:01:10 -0700457 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
458 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
459 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
clang-format6c4d83e2016-08-08 19:03:30 -0700460 { NULL, 0 }
Geza Lore454989f2016-03-24 13:56:05 +0000461};
462static const arg_def_t superblock_size = ARG_DEF_ENUM(
463 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
464#endif // CONFIG_EXT_PARTITION
465
Yaowu Xuf883b422016-08-30 14:01:10 -0700466static const arg_def_t *av1_args[] = { &cpu_used_av1,
467 &auto_altref,
468 &sharpness,
469 &static_thresh,
470 &tile_cols,
471 &tile_rows,
472 &arnr_maxframes,
473 &arnr_strength,
474 &arnr_type,
475 &tune_ssim,
476 &cq_level,
477 &max_intra_rate_pct,
478 &max_inter_rate_pct,
479 &gf_cbr_boost_pct,
480 &lossless,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800481#if CONFIG_AOM_QM
482 &enable_qm,
483 &qm_min,
484 &qm_max,
485#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700486 &frame_parallel_decoding,
487 &aq_mode,
488 &frame_periodic_boost,
489 &noise_sens,
490 &tune_content,
491 &input_color_space,
492 &min_gf_interval,
493 &max_gf_interval,
Geza Lore454989f2016-03-24 13:56:05 +0000494#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700495 &superblock_size,
Geza Lore454989f2016-03-24 13:56:05 +0000496#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000497#if CONFIG_TILE_GROUPS
498 &num_tg,
499 &mtu_size,
500#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700501#if CONFIG_AOM_HIGHBITDEPTH
502 &bitdeptharg,
503 &inbitdeptharg,
504#endif // CONFIG_AOM_HIGHBITDEPTH
505 NULL };
506static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
507 AOME_SET_ENABLEAUTOALTREF,
508 AOME_SET_SHARPNESS,
509 AOME_SET_STATIC_THRESHOLD,
510 AV1E_SET_TILE_COLUMNS,
511 AV1E_SET_TILE_ROWS,
512 AOME_SET_ARNR_MAXFRAMES,
513 AOME_SET_ARNR_STRENGTH,
514 AOME_SET_ARNR_TYPE,
515 AOME_SET_TUNING,
516 AOME_SET_CQ_LEVEL,
517 AOME_SET_MAX_INTRA_BITRATE_PCT,
518 AV1E_SET_MAX_INTER_BITRATE_PCT,
519 AV1E_SET_GF_CBR_BOOST_PCT,
520 AV1E_SET_LOSSLESS,
Yaowu Xu6c8ec652016-11-09 13:58:25 -0800521#if CONFIG_AOM_QM
522 AV1E_SET_ENABLE_QM,
523 AV1E_SET_QM_MIN,
524 AV1E_SET_QM_MAX,
525#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700526 AV1E_SET_FRAME_PARALLEL_DECODING,
527 AV1E_SET_AQ_MODE,
528 AV1E_SET_FRAME_PERIODIC_BOOST,
529 AV1E_SET_NOISE_SENSITIVITY,
530 AV1E_SET_TUNE_CONTENT,
531 AV1E_SET_COLOR_SPACE,
532 AV1E_SET_MIN_GF_INTERVAL,
533 AV1E_SET_MAX_GF_INTERVAL,
Geza Lore454989f2016-03-24 13:56:05 +0000534#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700535 AV1E_SET_SUPERBLOCK_SIZE,
Geza Lore454989f2016-03-24 13:56:05 +0000536#endif // CONFIG_EXT_PARTITION
Thomas Daviesaf6df172016-11-09 14:04:18 +0000537#if CONFIG_TILE_GROUPS
538 AV1E_SET_NUM_TG,
539 AV1E_SET_MTU,
540#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700541 0 };
hui su82331e02015-08-16 18:21:56 -0700542#endif
543
John Koleszar0ea50ce2010-05-18 11:58:33 -0400544static const arg_def_t *no_args[] = { NULL };
545
James Zern59e7a472015-05-09 10:33:26 -0700546void usage_exit(void) {
John Koleszarc6b90392012-07-13 15:21:29 -0700547 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700548 const int num_encoder = get_aom_encoder_count();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400549
John Koleszarc6b90392012-07-13 15:21:29 -0700550 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
551 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400552
John Koleszarc6b90392012-07-13 15:21:29 -0700553 fprintf(stderr, "\nOptions:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700554 arg_show_usage(stderr, main_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700555 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700556 arg_show_usage(stderr, global_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700557 fprintf(stderr, "\nRate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700558 arg_show_usage(stderr, rc_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700559 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700560 arg_show_usage(stderr, rc_twopass_args);
John Koleszarc6b90392012-07-13 15:21:29 -0700561 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszarb9e27612013-06-25 09:15:07 -0700562 arg_show_usage(stderr, kf_args);
Yaowu Xuf883b422016-08-30 14:01:10 -0700563#if CONFIG_AV1_ENCODER
564 fprintf(stderr, "\nAV1 Specific Options:\n");
565 arg_show_usage(stderr, av1_args);
hui su82331e02015-08-16 18:21:56 -0700566#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700567 fprintf(stderr,
568 "\nStream timebase (--timebase):\n"
John Koleszarc6b90392012-07-13 15:21:29 -0700569 " The desired precision of timestamps in the output, expressed\n"
570 " in fractional seconds. Default is 1/1000.\n");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800571 fprintf(stderr, "\nIncluded encoders:\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400572
Yaowu Xuf990b352015-06-01 09:13:59 -0700573 for (i = 0; i < num_encoder; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700574 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
clang-format6c4d83e2016-08-08 19:03:30 -0700575 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
576 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
Yaowu Xuf883b422016-08-30 14:01:10 -0700577 aom_codec_iface_name(encoder->codec_interface()), defstr);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800578 }
Yaowu Xuf990b352015-06-01 09:13:59 -0700579 fprintf(stderr, "\n ");
580 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400581
John Koleszarc6b90392012-07-13 15:21:29 -0700582 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400583}
584
clang-format6c4d83e2016-08-08 19:03:30 -0700585#define mmin(a, b) ((a) < (b) ? (a) : (b))
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700586
Yaowu Xuf883b422016-08-30 14:01:10 -0700587#if CONFIG_AOM_HIGHBITDEPTH
588static void find_mismatch_high(const aom_image_t *const img1,
589 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700590 int uloc[4], int vloc[4]) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700591 uint16_t *plane1, *plane2;
592 uint32_t stride1, stride2;
593 const uint32_t bsize = 64;
594 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
595 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
596 const uint32_t c_w =
597 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
598 const uint32_t c_h =
599 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
600 int match = 1;
601 uint32_t i, j;
602 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700603 plane1 = (uint16_t *)img1->planes[AOM_PLANE_Y];
604 plane2 = (uint16_t *)img2->planes[AOM_PLANE_Y];
605 stride1 = img1->stride[AOM_PLANE_Y] / 2;
606 stride2 = img2->stride[AOM_PLANE_Y] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700607 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
608 for (j = 0; match && j < img1->d_w; j += bsize) {
609 int k, l;
610 const int si = mmin(i + bsize, img1->d_h) - i;
611 const int sj = mmin(j + bsize, img1->d_w) - j;
612 for (k = 0; match && k < si; ++k) {
613 for (l = 0; match && l < sj; ++l) {
614 if (*(plane1 + (i + k) * stride1 + j + l) !=
615 *(plane2 + (i + k) * stride2 + j + l)) {
616 yloc[0] = i + k;
617 yloc[1] = j + l;
618 yloc[2] = *(plane1 + (i + k) * stride1 + j + l);
619 yloc[3] = *(plane2 + (i + k) * stride2 + j + l);
620 match = 0;
621 break;
622 }
623 }
624 }
625 }
626 }
627
628 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700629 plane1 = (uint16_t *)img1->planes[AOM_PLANE_U];
630 plane2 = (uint16_t *)img2->planes[AOM_PLANE_U];
631 stride1 = img1->stride[AOM_PLANE_U] / 2;
632 stride2 = img2->stride[AOM_PLANE_U] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700633 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
634 for (j = 0; match && j < c_w; j += bsizex) {
635 int k, l;
636 const int si = mmin(i + bsizey, c_h - i);
637 const int sj = mmin(j + bsizex, c_w - j);
638 for (k = 0; match && k < si; ++k) {
639 for (l = 0; match && l < sj; ++l) {
640 if (*(plane1 + (i + k) * stride1 + j + l) !=
641 *(plane2 + (i + k) * stride2 + j + l)) {
642 uloc[0] = i + k;
643 uloc[1] = j + l;
644 uloc[2] = *(plane1 + (i + k) * stride1 + j + l);
645 uloc[3] = *(plane2 + (i + k) * stride2 + j + l);
646 match = 0;
647 break;
648 }
649 }
650 }
651 }
652 }
653
654 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700655 plane1 = (uint16_t *)img1->planes[AOM_PLANE_V];
656 plane2 = (uint16_t *)img2->planes[AOM_PLANE_V];
657 stride1 = img1->stride[AOM_PLANE_V] / 2;
658 stride2 = img2->stride[AOM_PLANE_V] / 2;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700659 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
660 for (j = 0; match && j < c_w; j += bsizex) {
661 int k, l;
662 const int si = mmin(i + bsizey, c_h - i);
663 const int sj = mmin(j + bsizex, c_w - j);
664 for (k = 0; match && k < si; ++k) {
665 for (l = 0; match && l < sj; ++l) {
666 if (*(plane1 + (i + k) * stride1 + j + l) !=
667 *(plane2 + (i + k) * stride2 + j + l)) {
668 vloc[0] = i + k;
669 vloc[1] = j + l;
670 vloc[2] = *(plane1 + (i + k) * stride1 + j + l);
671 vloc[3] = *(plane2 + (i + k) * stride2 + j + l);
672 match = 0;
673 break;
674 }
675 }
676 }
677 }
678 }
679}
680#endif
681
Yaowu Xuf883b422016-08-30 14:01:10 -0700682static void find_mismatch(const aom_image_t *const img1,
683 const aom_image_t *const img2, int yloc[4],
clang-format6c4d83e2016-08-08 19:03:30 -0700684 int uloc[4], int vloc[4]) {
James Zern6253e8e2014-01-31 16:10:43 -0800685 const uint32_t bsize = 64;
686 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
687 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
688 const uint32_t c_w =
689 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
690 const uint32_t c_h =
691 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
692 int match = 1;
693 uint32_t i, j;
Deb Mukherjee23144d22013-03-12 14:21:08 -0700694 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -0800695 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
696 for (j = 0; match && j < img1->d_w; j += bsize) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800697 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800698 const int si = mmin(i + bsize, img1->d_h) - i;
699 const int sj = mmin(j + bsize, img1->d_w) - j;
700 for (k = 0; match && k < si; ++k) {
701 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700702 if (*(img1->planes[AOM_PLANE_Y] +
703 (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
704 *(img2->planes[AOM_PLANE_Y] +
705 (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800706 yloc[0] = i + k;
707 yloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700708 yloc[2] = *(img1->planes[AOM_PLANE_Y] +
709 (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
710 yloc[3] = *(img2->planes[AOM_PLANE_Y] +
711 (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800712 match = 0;
713 break;
714 }
715 }
James Zernd04112d2014-01-31 15:49:17 -0800716 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800717 }
718 }
Jingning Han12bf0792013-04-06 10:00:53 -0700719
Deb Mukherjee23144d22013-03-12 14:21:08 -0700720 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700721 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700722 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800723 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800724 const int si = mmin(i + bsizey, c_h - i);
725 const int sj = mmin(j + bsizex, c_w - j);
726 for (k = 0; match && k < si; ++k) {
727 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700728 if (*(img1->planes[AOM_PLANE_U] +
729 (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
730 *(img2->planes[AOM_PLANE_U] +
731 (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800732 uloc[0] = i + k;
733 uloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700734 uloc[2] = *(img1->planes[AOM_PLANE_U] +
735 (i + k) * img1->stride[AOM_PLANE_U] + j + l);
736 uloc[3] = *(img2->planes[AOM_PLANE_U] +
737 (i + k) * img2->stride[AOM_PLANE_U] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800738 match = 0;
739 break;
740 }
741 }
James Zernd04112d2014-01-31 15:49:17 -0800742 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800743 }
744 }
Deb Mukherjee23144d22013-03-12 14:21:08 -0700745 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
John Koleszarbeae5012013-05-08 16:07:38 -0700746 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
John Koleszarcc0eeda2013-06-11 11:28:08 -0700747 for (j = 0; match && j < c_w; j += bsizex) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800748 int k, l;
James Zernd04112d2014-01-31 15:49:17 -0800749 const int si = mmin(i + bsizey, c_h - i);
750 const int sj = mmin(j + bsizex, c_w - j);
751 for (k = 0; match && k < si; ++k) {
752 for (l = 0; match && l < sj; ++l) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700753 if (*(img1->planes[AOM_PLANE_V] +
754 (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
755 *(img2->planes[AOM_PLANE_V] +
756 (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800757 vloc[0] = i + k;
758 vloc[1] = j + l;
Yaowu Xuf883b422016-08-30 14:01:10 -0700759 vloc[2] = *(img1->planes[AOM_PLANE_V] +
760 (i + k) * img1->stride[AOM_PLANE_V] + j + l);
761 vloc[3] = *(img2->planes[AOM_PLANE_V] +
762 (i + k) * img2->stride[AOM_PLANE_V] + j + l);
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800763 match = 0;
764 break;
765 }
766 }
James Zernd04112d2014-01-31 15:49:17 -0800767 }
Deb Mukherjee0742b1e2012-11-15 15:14:38 -0800768 }
769 }
770}
John Koleszar0ea50ce2010-05-18 11:58:33 -0400771
Yaowu Xuf883b422016-08-30 14:01:10 -0700772static int compare_img(const aom_image_t *const img1,
773 const aom_image_t *const img2) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700774 uint32_t l_w = img1->d_w;
clang-format6c4d83e2016-08-08 19:03:30 -0700775 uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
James Zern6253e8e2014-01-31 16:10:43 -0800776 const uint32_t c_h =
777 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
778 uint32_t i;
John Koleszarc6b90392012-07-13 15:21:29 -0700779 int match = 1;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800780
John Koleszarc6b90392012-07-13 15:21:29 -0700781 match &= (img1->fmt == img2->fmt);
hkuang3671e622014-01-22 14:58:59 -0800782 match &= (img1->d_w == img2->d_w);
783 match &= (img1->d_h == img2->d_h);
Yaowu Xuf883b422016-08-30 14:01:10 -0700784#if CONFIG_AOM_HIGHBITDEPTH
785 if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700786 l_w *= 2;
787 c_w *= 2;
788 }
789#endif
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800790
James Zernd04112d2014-01-31 15:49:17 -0800791 for (i = 0; i < img1->d_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700792 match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
793 img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700794 l_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800795
James Zernd04112d2014-01-31 15:49:17 -0800796 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700797 match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
798 img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
John Koleszarbeae5012013-05-08 16:07:38 -0700799 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800800
James Zernd04112d2014-01-31 15:49:17 -0800801 for (i = 0; i < c_h; ++i)
Yaowu Xuf883b422016-08-30 14:01:10 -0700802 match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
803 img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
John Koleszarbeae5012013-05-08 16:07:38 -0700804 c_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800805
John Koleszarc6b90392012-07-13 15:21:29 -0700806 return match;
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800807}
808
clang-format6c4d83e2016-08-08 19:03:30 -0700809#define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
Yaowu Xuf883b422016-08-30 14:01:10 -0700810#if CONFIG_AV1_ENCODER
811#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -0800812#endif
John Koleszarefd54f82012-02-13 16:52:18 -0800813
James Zerndba73762014-05-10 17:44:12 -0700814#if !CONFIG_WEBM_IO
815typedef int stereo_format_t;
clang-format6c4d83e2016-08-08 19:03:30 -0700816struct WebmOutputContext {
817 int debug;
818};
James Zerndba73762014-05-10 17:44:12 -0700819#endif
820
John Koleszar9e50ed72012-02-15 12:39:38 -0800821/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -0800822struct stream_config {
Yaowu Xuf883b422016-08-30 14:01:10 -0700823 struct aom_codec_enc_cfg cfg;
clang-format6c4d83e2016-08-08 19:03:30 -0700824 const char *out_fn;
825 const char *stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700826#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700827 const char *fpmb_stats_fn;
Pengchong Jinf349b072014-07-14 09:13:38 -0700828#endif
clang-format6c4d83e2016-08-08 19:03:30 -0700829 stereo_format_t stereo_fmt;
830 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
831 int arg_ctrl_cnt;
832 int write_webm;
Yaowu Xuf883b422016-08-30 14:01:10 -0700833#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700834 // whether to use 16bit internal buffers
clang-format6c4d83e2016-08-08 19:03:30 -0700835 int use_16bit_internal;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700836#endif
John Koleszar9e50ed72012-02-15 12:39:38 -0800837};
838
John Koleszar6ad3b742012-11-06 12:02:42 -0800839struct stream_state {
clang-format6c4d83e2016-08-08 19:03:30 -0700840 int index;
841 struct stream_state *next;
842 struct stream_config config;
843 FILE *file;
844 struct rate_hist *rate_hist;
845 struct WebmOutputContext webm_ctx;
846 uint64_t psnr_sse_total;
847 uint64_t psnr_samples_total;
848 double psnr_totals[4];
849 int psnr_count;
850 int counts[64];
Yaowu Xuf883b422016-08-30 14:01:10 -0700851 aom_codec_ctx_t encoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700852 unsigned int frames_out;
853 uint64_t cx_time;
854 size_t nbytes;
855 stats_io_t stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700856#if CONFIG_FP_MB_STATS
clang-format6c4d83e2016-08-08 19:03:30 -0700857 stats_io_t fpmb_stats;
Pengchong Jinf349b072014-07-14 09:13:38 -0700858#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700859 struct aom_image *img;
860 aom_codec_ctx_t decoder;
clang-format6c4d83e2016-08-08 19:03:30 -0700861 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -0800862};
863
clang-format6c4d83e2016-08-08 19:03:30 -0700864static void validate_positive_rational(const char *msg,
Yaowu Xuf883b422016-08-30 14:01:10 -0700865 struct aom_rational *rat) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800866 if (rat->den < 0) {
867 rat->num *= -1;
868 rat->den *= -1;
869 }
John Koleszar1b27e932012-04-25 17:08:56 -0700870
clang-format6c4d83e2016-08-08 19:03:30 -0700871 if (rat->num < 0) die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700872
clang-format6c4d83e2016-08-08 19:03:30 -0700873 if (!rat->den) die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -0700874}
875
Yaowu Xuf883b422016-08-30 14:01:10 -0700876static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
clang-format6c4d83e2016-08-08 19:03:30 -0700877 char **argi, **argj;
878 struct arg arg;
Yaowu Xuf883b422016-08-30 14:01:10 -0700879 const int num_encoder = get_aom_encoder_count();
Yaowu Xuf990b352015-06-01 09:13:59 -0700880
clang-format6c4d83e2016-08-08 19:03:30 -0700881 if (num_encoder < 1) die("Error: no valid encoder available\n");
John Koleszarefd54f82012-02-13 16:52:18 -0800882
John Koleszar6ad3b742012-11-06 12:02:42 -0800883 /* Initialize default parameters */
884 memset(global, 0, sizeof(*global));
Yaowu Xuf883b422016-08-30 14:01:10 -0700885 global->codec = get_aom_encoder_by_index(num_encoder - 1);
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700886 global->passes = 0;
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700887 global->color_type = I420;
Deb Mukherjee0d8723f2013-08-19 14:16:26 -0700888 /* Assign default deadline to good quality */
Yaowu Xuf883b422016-08-30 14:01:10 -0700889 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400890
John Koleszarc6b90392012-07-13 15:21:29 -0700891 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
892 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400893
John Koleszarc6b90392012-07-13 15:21:29 -0700894 if (arg_match(&arg, &codecarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700895 global->codec = get_aom_encoder_by_name(arg.val);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800896 if (!global->codec)
897 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
John Koleszarc6b90392012-07-13 15:21:29 -0700898 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800899 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400900
John Koleszar6ad3b742012-11-06 12:02:42 -0800901 if (global->passes < 1 || global->passes > 2)
902 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -0700903 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800904 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400905
John Koleszar6ad3b742012-11-06 12:02:42 -0800906 if (global->pass < 1 || global->pass > 2)
clang-format6c4d83e2016-08-08 19:03:30 -0700907 die("Error: Invalid pass selected (%d)\n", global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800908 } else if (arg_match(&arg, &usage, argi))
909 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700910 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800911 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700912 else if (arg_match(&arg, &best_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700913 global->deadline = AOM_DL_BEST_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -0700914 else if (arg_match(&arg, &good_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700915 global->deadline = AOM_DL_GOOD_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -0700916 else if (arg_match(&arg, &rt_dl, argi))
Yaowu Xuf883b422016-08-30 14:01:10 -0700917 global->deadline = AOM_DL_REALTIME;
John Koleszar6ad3b742012-11-06 12:02:42 -0800918 else if (arg_match(&arg, &use_yv12, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700919 global->color_type = YV12;
John Koleszar6ad3b742012-11-06 12:02:42 -0800920 else if (arg_match(&arg, &use_i420, argi))
Deb Mukherjee090f4d42014-07-16 09:37:13 -0700921 global->color_type = I420;
922 else if (arg_match(&arg, &use_i422, argi))
923 global->color_type = I422;
924 else if (arg_match(&arg, &use_i444, argi))
925 global->color_type = I444;
Deb Mukherjeea30774c2014-10-01 12:17:37 -0700926 else if (arg_match(&arg, &use_i440, argi))
927 global->color_type = I440;
John Koleszar6ad3b742012-11-06 12:02:42 -0800928 else if (arg_match(&arg, &quietarg, argi))
929 global->quiet = 1;
930 else if (arg_match(&arg, &verbosearg, argi))
931 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700932 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800933 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700934 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800935 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700936 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800937 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700938 else if (arg_match(&arg, &recontest, argi))
Ronald S. Bultje9837bf42013-02-15 16:31:02 -0800939 global->test_decode = arg_parse_enum_or_int(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700940 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800941 global->framerate = arg_parse_rational(&arg);
942 validate_positive_rational(arg.name, &global->framerate);
943 global->have_framerate = 1;
944 } else if (arg_match(&arg, &out_part, argi))
945 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700946 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800947 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -0700948 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800949 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -0700950 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -0800951 global->show_rate_hist_buckets = arg_parse_uint(&arg);
Tom Finegan249366b2013-11-25 12:05:19 -0800952 else if (arg_match(&arg, &disable_warnings, argi))
953 global->disable_warnings = 1;
954 else if (arg_match(&arg, &disable_warning_prompt, argi))
955 global->disable_warning_prompt = 1;
John Koleszar732cb9a2012-02-14 12:30:17 -0800956 else
John Koleszarc6b90392012-07-13 15:21:29 -0700957 argj++;
958 }
959
John Koleszar6ad3b742012-11-06 12:02:42 -0800960 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -0700961 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -0800962 if (global->pass > global->passes) {
clang-format6c4d83e2016-08-08 19:03:30 -0700963 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
964 global->pass);
John Koleszar6ad3b742012-11-06 12:02:42 -0800965 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -0800966 }
John Koleszarc6b90392012-07-13 15:21:29 -0700967 }
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800968 /* Validate global config */
969 if (global->passes == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700970#if CONFIG_AV1_ENCODER
971 // Make default AV1 passes = 2 until there is a better quality 1-pass
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800972 // encoder
Jim Bankoskic901a4f2014-08-21 07:18:07 -0700973 if (global->codec != NULL && global->codec->name != NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -0700974 global->passes = (strcmp(global->codec->name, "av1") == 0 &&
975 global->deadline != AOM_DL_REALTIME)
clang-format6c4d83e2016-08-08 19:03:30 -0700976 ? 2
977 : 1;
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800978#else
979 global->passes = 1;
980#endif
981 }
982
Yaowu Xuf883b422016-08-30 14:01:10 -0700983 if (global->deadline == AOM_DL_REALTIME && global->passes > 1) {
Deb Mukherjee25f22d22014-02-13 15:34:42 -0800984 warn("Enforcing one-pass encoding in realtime mode\n");
985 global->passes = 1;
986 }
John Koleszar732cb9a2012-02-14 12:30:17 -0800987}
988
Yaowu Xuf883b422016-08-30 14:01:10 -0700989static void open_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800990 /* Parse certain options from the input file, if possible */
clang-format6c4d83e2016-08-08 19:03:30 -0700991 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
992 : set_binary_mode(stdin);
John Koleszar6ad3b742012-11-06 12:02:42 -0800993
clang-format6c4d83e2016-08-08 19:03:30 -0700994 if (!input->file) fatal("Failed to open input file");
John Koleszar6ad3b742012-11-06 12:02:42 -0800995
John Koleszar25b6e9f2013-02-12 21:17:56 -0800996 if (!fseeko(input->file, 0, SEEK_END)) {
997 /* Input file is seekable. Figure out how long it is, so we can get
998 * progress info.
999 */
1000 input->length = ftello(input->file);
1001 rewind(input->file);
1002 }
1003
Frank Galligan09acd262015-06-01 10:20:58 -07001004 /* Default to 1:1 pixel aspect ratio. */
1005 input->pixel_aspect_ratio.numerator = 1;
1006 input->pixel_aspect_ratio.denominator = 1;
1007
John Koleszar6ad3b742012-11-06 12:02:42 -08001008 /* For RAW input sources, these bytes will applied on the first frame
1009 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -07001010 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001011 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1012 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001013
clang-format6c4d83e2016-08-08 19:03:30 -07001014 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
John Koleszar8dd82872013-05-06 11:01:35 -07001015 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
1016 input->only_i420) >= 0) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001017 input->file_type = FILE_TYPE_Y4M;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001018 input->width = input->y4m.pic_w;
1019 input->height = input->y4m.pic_h;
Frank Galligan09acd262015-06-01 10:20:58 -07001020 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1021 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
Tom Finegan00a35aa2013-11-14 12:37:42 -08001022 input->framerate.numerator = input->y4m.fps_n;
1023 input->framerate.denominator = input->y4m.fps_d;
Yaowu Xuf883b422016-08-30 14:01:10 -07001024 input->fmt = input->y4m.aom_fmt;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001025 input->bit_depth = input->y4m.bit_depth;
John Koleszar6ad3b742012-11-06 12:02:42 -08001026 } else
1027 fatal("Unsupported Y4M stream.");
Alex Converse64b89f12014-01-06 16:29:09 -08001028 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
Tom Finegan6c270ed2013-11-08 11:32:23 -08001029 fatal("IVF is not supported as input.");
John Koleszar6ad3b742012-11-06 12:02:42 -08001030 } else {
1031 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001032 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001033}
1034
Yaowu Xuf883b422016-08-30 14:01:10 -07001035static void close_input_file(struct AvxInputContext *input) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001036 fclose(input->file);
clang-format6c4d83e2016-08-08 19:03:30 -07001037 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001038}
1039
Yaowu Xuf883b422016-08-30 14:01:10 -07001040static struct stream_state *new_stream(struct AvxEncoderConfig *global,
Tom Finegan6c270ed2013-11-08 11:32:23 -08001041 struct stream_state *prev) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001042 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001043
John Koleszar6ad3b742012-11-06 12:02:42 -08001044 stream = calloc(1, sizeof(*stream));
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001045 if (stream == NULL) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001046 fatal("Failed to allocate new stream.");
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001047 }
1048
John Koleszar6ad3b742012-11-06 12:02:42 -08001049 if (prev) {
1050 memcpy(stream, prev, sizeof(*stream));
1051 stream->index++;
1052 prev->next = stream;
1053 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001054 aom_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001055
John Koleszar6ad3b742012-11-06 12:02:42 -08001056 /* Populate encoder configuration */
Yaowu Xuf883b422016-08-30 14:01:10 -07001057 res = aom_codec_enc_config_default(global->codec->codec_interface(),
clang-format6c4d83e2016-08-08 19:03:30 -07001058 &stream->config.cfg, global->usage);
Yaowu Xuf883b422016-08-30 14:01:10 -07001059 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001060
John Koleszar6ad3b742012-11-06 12:02:42 -08001061 /* Change the default timebase to a high enough value so that the
1062 * encoder will always create strictly increasing timestamps.
1063 */
1064 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001065
John Koleszar6ad3b742012-11-06 12:02:42 -08001066 /* Never use the library's default resolution, require it be parsed
1067 * from the file or set on the command line.
1068 */
1069 stream->config.cfg.g_w = 0;
1070 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001071
John Koleszar6ad3b742012-11-06 12:02:42 -08001072 /* Initialize remaining stream parameters */
John Koleszar6ad3b742012-11-06 12:02:42 -08001073 stream->config.write_webm = 1;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001074#if CONFIG_WEBM_IO
James Zerndba73762014-05-10 17:44:12 -07001075 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001076 stream->webm_ctx.last_pts_ns = -1;
1077 stream->webm_ctx.writer = NULL;
1078 stream->webm_ctx.segment = NULL;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001079#endif
Johann87c40b32012-03-01 16:12:53 -08001080
John Koleszar6ad3b742012-11-06 12:02:42 -08001081 /* Allows removal of the application version from the EBML tags */
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001082 stream->webm_ctx.debug = global->debug;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001083
1084 /* Default lag_in_frames is 0 in realtime mode */
Yaowu Xuf883b422016-08-30 14:01:10 -07001085 if (global->deadline == AOM_DL_REALTIME)
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001086 stream->config.cfg.g_lag_in_frames = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001087 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001088
John Koleszar6ad3b742012-11-06 12:02:42 -08001089 /* Output files must be specified for each stream */
1090 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001091
John Koleszar6ad3b742012-11-06 12:02:42 -08001092 stream->next = NULL;
1093 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001094}
1095
Yaowu Xuf883b422016-08-30 14:01:10 -07001096static int parse_stream_params(struct AvxEncoderConfig *global,
clang-format6c4d83e2016-08-08 19:03:30 -07001097 struct stream_state *stream, char **argv) {
1098 char **argi, **argj;
1099 struct arg arg;
John Koleszar6ad3b742012-11-06 12:02:42 -08001100 static const arg_def_t **ctrl_args = no_args;
clang-format6c4d83e2016-08-08 19:03:30 -07001101 static const int *ctrl_args_map = NULL;
1102 struct stream_config *config = &stream->config;
1103 int eos_mark_found = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001104#if CONFIG_AOM_HIGHBITDEPTH
clang-format6c4d83e2016-08-08 19:03:30 -07001105 int test_16bit_internal = 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001106#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001107
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001108 // Handle codec specific options
John Koleszara9c75972012-11-08 17:09:30 -08001109 if (0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001110#if CONFIG_AV1_ENCODER
1111 } else if (strcmp(global->codec->name, "av1") == 0) {
1112 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1113 // Consider to expand this set for AV1 encoder control.
1114 ctrl_args = av1_args;
1115 ctrl_args_map = av1_arg_ctrl_map;
Jingning Han3ee6db62015-08-05 19:00:31 -07001116#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001117 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001118
John Koleszarc6b90392012-07-13 15:21:29 -07001119 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001120 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001121
John Koleszar6ad3b742012-11-06 12:02:42 -08001122 /* Once we've found an end-of-stream marker (--) we want to continue
1123 * shifting arguments but not consuming them.
1124 */
1125 if (eos_mark_found) {
1126 argj++;
1127 continue;
1128 } else if (!strcmp(*argj, "--")) {
1129 eos_mark_found = 1;
1130 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001131 }
1132
Adrian Granged2401802015-02-13 14:51:32 -08001133 if (arg_match(&arg, &outputfile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001134 config->out_fn = arg.val;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001135 } else if (arg_match(&arg, &fpf_name, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001136 config->stats_fn = arg.val;
Pengchong Jinf349b072014-07-14 09:13:38 -07001137#if CONFIG_FP_MB_STATS
1138 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1139 config->fpmb_stats_fn = arg.val;
1140#endif
Johannb50e5182015-01-30 15:05:14 -08001141 } else if (arg_match(&arg, &use_webm, argi)) {
1142#if CONFIG_WEBM_IO
1143 config->write_webm = 1;
1144#else
1145 die("Error: --webm specified but webm is disabled.");
1146#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001147 } else if (arg_match(&arg, &use_ivf, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001148 config->write_webm = 0;
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001149 } else if (arg_match(&arg, &threads, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001150 config->cfg.g_threads = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001151 } else if (arg_match(&arg, &profile, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001152 config->cfg.g_profile = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001153 } else if (arg_match(&arg, &width, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001154 config->cfg.g_w = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001155 } else if (arg_match(&arg, &height, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001156 config->cfg.g_h = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -07001157#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001158 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1159 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1160 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1161 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1162#endif
James Zerndba73762014-05-10 17:44:12 -07001163#if CONFIG_WEBM_IO
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001164 } else if (arg_match(&arg, &stereo_mode, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001165 config->stereo_fmt = arg_parse_enum_or_int(&arg);
James Zerndba73762014-05-10 17:44:12 -07001166#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001167 } else if (arg_match(&arg, &timebase, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001168 config->cfg.g_timebase = arg_parse_rational(&arg);
1169 validate_positive_rational(arg.name, &config->cfg.g_timebase);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001170 } else if (arg_match(&arg, &error_resilient, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001171 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001172 } else if (arg_match(&arg, &lag_in_frames, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001173 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
Yaowu Xuf883b422016-08-30 14:01:10 -07001174 if (global->deadline == AOM_DL_REALTIME &&
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001175 config->cfg.g_lag_in_frames != 0) {
1176 warn("non-zero %s option ignored in realtime mode.\n", arg.name);
1177 config->cfg.g_lag_in_frames = 0;
1178 }
1179 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001180 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001181 } else if (arg_match(&arg, &resize_allowed, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001182 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001183 } else if (arg_match(&arg, &resize_width, argi)) {
1184 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
1185 } else if (arg_match(&arg, &resize_height, argi)) {
1186 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001187 } else if (arg_match(&arg, &resize_up_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001188 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001189 } else if (arg_match(&arg, &resize_down_thresh, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001190 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001191 } else if (arg_match(&arg, &end_usage, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001192 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001193 } else if (arg_match(&arg, &target_bitrate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001194 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001195 } else if (arg_match(&arg, &min_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001196 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001197 } else if (arg_match(&arg, &max_quantizer, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001198 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001199 } else if (arg_match(&arg, &undershoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001200 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001201 } else if (arg_match(&arg, &overshoot_pct, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001202 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001203 } else if (arg_match(&arg, &buf_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001204 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001205 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001206 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001207 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001208 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001209 } else if (arg_match(&arg, &bias_pct, argi)) {
clang-format6c4d83e2016-08-08 19:03:30 -07001210 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar6ad3b742012-11-06 12:02:42 -08001211 if (global->passes < 2)
1212 warn("option %s ignored in one-pass mode.\n", arg.name);
1213 } else if (arg_match(&arg, &minsection_pct, argi)) {
1214 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1215
1216 if (global->passes < 2)
1217 warn("option %s ignored in one-pass mode.\n", arg.name);
1218 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1219 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1220
1221 if (global->passes < 2)
1222 warn("option %s ignored in one-pass mode.\n", arg.name);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001223 } else if (arg_match(&arg, &kf_min_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001224 config->cfg.kf_min_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001225 } else if (arg_match(&arg, &kf_max_dist, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001226 config->cfg.kf_max_dist = arg_parse_uint(&arg);
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001227 } else if (arg_match(&arg, &kf_disabled, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001228 config->cfg.kf_mode = AOM_KF_DISABLED;
1229#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001230 } else if (arg_match(&arg, &test16bitinternalarg, argi)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001231 if (strcmp(global->codec->name, "av1") == 0 ||
1232 strcmp(global->codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001233 test_16bit_internal = 1;
1234 }
1235#endif
Deb Mukherjee25f22d22014-02-13 15:34:42 -08001236 } else {
John Koleszar6ad3b742012-11-06 12:02:42 -08001237 int i, match = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001238 for (i = 0; ctrl_args[i]; i++) {
1239 if (arg_match(&arg, ctrl_args[i], argi)) {
1240 int j;
1241 match = 1;
1242
1243 /* Point either to the next free element or the first
1244 * instance of this control.
1245 */
1246 for (j = 0; j < config->arg_ctrl_cnt; j++)
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001247 if (ctrl_args_map != NULL &&
1248 config->arg_ctrls[j][0] == ctrl_args_map[i])
John Koleszar6ad3b742012-11-06 12:02:42 -08001249 break;
1250
1251 /* Update/insert */
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001252 assert(j < (int)ARG_CTRL_CNT_MAX);
Jim Bankoskic901a4f2014-08-21 07:18:07 -07001253 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001254 config->arg_ctrls[j][0] = ctrl_args_map[i];
1255 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
clang-format6c4d83e2016-08-08 19:03:30 -07001256 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
John Koleszar6ad3b742012-11-06 12:02:42 -08001257 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001258 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001259 }
clang-format6c4d83e2016-08-08 19:03:30 -07001260 if (!match) argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001261 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001262 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001263#if CONFIG_AOM_HIGHBITDEPTH
1264 if (strcmp(global->codec->name, "av1") == 0 ||
1265 strcmp(global->codec->name, "av1") == 0) {
clang-format6c4d83e2016-08-08 19:03:30 -07001266 config->use_16bit_internal =
1267 test_16bit_internal | (config->cfg.g_profile > 1);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001268 }
1269#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001270 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001271}
1272
clang-format6c4d83e2016-08-08 19:03:30 -07001273#define FOREACH_STREAM(func) \
1274 do { \
1275 struct stream_state *stream; \
Tom Finegan249366b2013-11-25 12:05:19 -08001276 for (stream = streams; stream; stream = stream->next) { \
clang-format6c4d83e2016-08-08 19:03:30 -07001277 func; \
1278 } \
Tom Finegan44dd3272013-11-20 17:18:28 -08001279 } while (0)
John Koleszar9e50ed72012-02-15 12:39:38 -08001280
Alex Conversed66bd222014-02-21 10:52:09 -08001281static void validate_stream_config(const struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001282 const struct AvxEncoderConfig *global) {
Alex Conversed66bd222014-02-21 10:52:09 -08001283 const struct stream_state *streami;
Johann80b344d2014-12-16 12:22:10 -08001284 (void)global;
John Koleszar9e50ed72012-02-15 12:39:38 -08001285
John Koleszar6ad3b742012-11-06 12:02:42 -08001286 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
clang-format6c4d83e2016-08-08 19:03:30 -07001287 fatal(
1288 "Stream %d: Specify stream dimensions with --width (-w) "
1289 " and --height (-h)",
1290 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001291
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001292 // Check that the codec bit depth is greater than the input bit depth.
1293 if (stream->config.cfg.g_input_bit_depth >
Yaowu Xudbdb87b2014-09-03 17:02:31 -07001294 (unsigned int)stream->config.cfg.g_bit_depth) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001295 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1296 stream->index, (int)stream->config.cfg.g_bit_depth,
1297 stream->config.cfg.g_input_bit_depth);
1298 }
1299
John Koleszar6ad3b742012-11-06 12:02:42 -08001300 for (streami = stream; streami; streami = streami->next) {
1301 /* All streams require output files */
1302 if (!streami->config.out_fn)
1303 fatal("Stream %d: Output file is required (specify with -o)",
1304 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001305
John Koleszar6ad3b742012-11-06 12:02:42 -08001306 /* Check for two streams outputting to the same file */
1307 if (streami != stream) {
1308 const char *a = stream->config.out_fn;
1309 const char *b = streami->config.out_fn;
1310 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1311 fatal("Stream %d: duplicate output file (from stream %d)",
1312 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001313 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001314
1315 /* Check for two streams sharing a stats file. */
1316 if (streami != stream) {
1317 const char *a = stream->config.stats_fn;
1318 const char *b = streami->config.stats_fn;
1319 if (a && b && !strcmp(a, b))
1320 fatal("Stream %d: duplicate stats file (from stream %d)",
1321 streami->index, stream->index);
1322 }
Pengchong Jinf349b072014-07-14 09:13:38 -07001323
1324#if CONFIG_FP_MB_STATS
1325 /* Check for two streams sharing a mb stats file. */
1326 if (streami != stream) {
1327 const char *a = stream->config.fpmb_stats_fn;
1328 const char *b = streami->config.fpmb_stats_fn;
1329 if (a && b && !strcmp(a, b))
1330 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1331 streami->index, stream->index);
1332 }
1333#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001334 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001335}
1336
clang-format6c4d83e2016-08-08 19:03:30 -07001337static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08001338 unsigned int h) {
John Koleszar34882b92012-03-01 12:50:40 -08001339 if (!stream->config.cfg.g_w) {
1340 if (!stream->config.cfg.g_h)
1341 stream->config.cfg.g_w = w;
1342 else
1343 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1344 }
1345 if (!stream->config.cfg.g_h) {
1346 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1347 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001348}
1349
clang-format6c4d83e2016-08-08 19:03:30 -07001350static const char *file_type_to_string(enum VideoFileType t) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001351 switch (t) {
1352 case FILE_TYPE_RAW: return "RAW";
1353 case FILE_TYPE_Y4M: return "Y4M";
1354 default: return "Other";
1355 }
1356}
1357
Yaowu Xuf883b422016-08-30 14:01:10 -07001358static const char *image_format_to_string(aom_img_fmt_t f) {
Alex Converse6c2e88e2014-05-16 12:29:36 -07001359 switch (f) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001360 case AOM_IMG_FMT_I420: return "I420";
1361 case AOM_IMG_FMT_I422: return "I422";
1362 case AOM_IMG_FMT_I444: return "I444";
1363 case AOM_IMG_FMT_I440: return "I440";
1364 case AOM_IMG_FMT_YV12: return "YV12";
1365 case AOM_IMG_FMT_I42016: return "I42016";
1366 case AOM_IMG_FMT_I42216: return "I42216";
1367 case AOM_IMG_FMT_I44416: return "I44416";
1368 case AOM_IMG_FMT_I44016: return "I44016";
Alex Converse6c2e88e2014-05-16 12:29:36 -07001369 default: return "Other";
1370 }
1371}
Ralph Giles061a16d2012-01-05 15:05:05 -06001372
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001373static void show_stream_config(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001374 struct AvxEncoderConfig *global,
1375 struct AvxInputContext *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08001376#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08001377 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08001378
John Koleszar6ad3b742012-11-06 12:02:42 -08001379 if (stream->index == 0) {
1380 fprintf(stderr, "Codec: %s\n",
Yaowu Xuf883b422016-08-30 14:01:10 -07001381 aom_codec_iface_name(global->codec->codec_interface()));
Alex Converse6c2e88e2014-05-16 12:29:36 -07001382 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
clang-format6c4d83e2016-08-08 19:03:30 -07001383 input->filename, file_type_to_string(input->file_type),
Alex Converse6c2e88e2014-05-16 12:29:36 -07001384 image_format_to_string(input->fmt));
John Koleszar6ad3b742012-11-06 12:02:42 -08001385 }
1386 if (stream->next || stream->index)
1387 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1388 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1389 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001390
John Koleszar6ad3b742012-11-06 12:02:42 -08001391 SHOW(g_usage);
1392 SHOW(g_threads);
1393 SHOW(g_profile);
1394 SHOW(g_w);
1395 SHOW(g_h);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001396 SHOW(g_bit_depth);
1397 SHOW(g_input_bit_depth);
John Koleszar6ad3b742012-11-06 12:02:42 -08001398 SHOW(g_timebase.num);
1399 SHOW(g_timebase.den);
1400 SHOW(g_error_resilient);
1401 SHOW(g_pass);
1402 SHOW(g_lag_in_frames);
1403 SHOW(rc_dropframe_thresh);
1404 SHOW(rc_resize_allowed);
Adrian Grangef7bd1272014-04-09 14:51:29 -07001405 SHOW(rc_scaled_width);
1406 SHOW(rc_scaled_height);
John Koleszar6ad3b742012-11-06 12:02:42 -08001407 SHOW(rc_resize_up_thresh);
1408 SHOW(rc_resize_down_thresh);
1409 SHOW(rc_end_usage);
1410 SHOW(rc_target_bitrate);
1411 SHOW(rc_min_quantizer);
1412 SHOW(rc_max_quantizer);
1413 SHOW(rc_undershoot_pct);
1414 SHOW(rc_overshoot_pct);
1415 SHOW(rc_buf_sz);
1416 SHOW(rc_buf_initial_sz);
1417 SHOW(rc_buf_optimal_sz);
1418 SHOW(rc_2pass_vbr_bias_pct);
1419 SHOW(rc_2pass_vbr_minsection_pct);
1420 SHOW(rc_2pass_vbr_maxsection_pct);
1421 SHOW(kf_mode);
1422 SHOW(kf_min_dist);
1423 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08001424}
1425
John Koleszar9e50ed72012-02-15 12:39:38 -08001426static void open_output_file(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001427 struct AvxEncoderConfig *global,
1428 const struct AvxRational *pixel_aspect_ratio) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001429 const char *fn = stream->config.out_fn;
Yaowu Xuf883b422016-08-30 14:01:10 -07001430 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001431
Yaowu Xuf883b422016-08-30 14:01:10 -07001432 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001433
John Koleszar6ad3b742012-11-06 12:02:42 -08001434 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08001435
clang-format6c4d83e2016-08-08 19:03:30 -07001436 if (!stream->file) fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08001437
John Koleszar6ad3b742012-11-06 12:02:42 -08001438 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1439 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08001440
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001441#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001442 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001443 stream->webm_ctx.stream = stream->file;
Urvang Joshid71a2312016-07-14 12:33:48 -07001444 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1445 global->codec->fourcc, pixel_aspect_ratio);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001446 }
James Zernc8e5a772016-02-11 18:53:50 -08001447#else
1448 (void)pixel_aspect_ratio;
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001449#endif
1450
1451 if (!stream->config.write_webm) {
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001452 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1453 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001454}
1455
John Koleszar9e50ed72012-02-15 12:39:38 -08001456static void close_output_file(struct stream_state *stream,
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001457 unsigned int fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001458 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001459
Yaowu Xuf883b422016-08-30 14:01:10 -07001460 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
Dmitry Kovalev096ab112014-01-13 15:21:48 -08001461
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001462#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001463 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001464 write_webm_file_footer(&stream->webm_ctx);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001465 }
1466#endif
1467
1468 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001469 if (!fseek(stream->file, 0, SEEK_SET))
clang-format6c4d83e2016-08-08 19:03:30 -07001470 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
John Koleszar6ad3b742012-11-06 12:02:42 -08001471 stream->frames_out);
1472 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001473
John Koleszar6ad3b742012-11-06 12:02:42 -08001474 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08001475}
1476
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001477static void setup_pass(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001478 struct AvxEncoderConfig *global, int pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001479 if (stream->config.stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001480 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
John Koleszar6ad3b742012-11-06 12:02:42 -08001481 fatal("Failed to open statistics store");
1482 } else {
1483 if (!stats_open_mem(&stream->stats, pass))
1484 fatal("Failed to open statistics store");
1485 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001486
Pengchong Jinf349b072014-07-14 09:13:38 -07001487#if CONFIG_FP_MB_STATS
1488 if (stream->config.fpmb_stats_fn) {
clang-format6c4d83e2016-08-08 19:03:30 -07001489 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1490 pass))
Pengchong Jinf349b072014-07-14 09:13:38 -07001491 fatal("Failed to open mb statistics store");
1492 } else {
1493 if (!stats_open_mem(&stream->fpmb_stats, pass))
1494 fatal("Failed to open mb statistics store");
1495 }
1496#endif
1497
John Koleszar6ad3b742012-11-06 12:02:42 -08001498 stream->config.cfg.g_pass = global->passes == 2
Yaowu Xuf883b422016-08-30 14:01:10 -07001499 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1500 : AOM_RC_ONE_PASS;
Pengchong Jinf349b072014-07-14 09:13:38 -07001501 if (pass) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001502 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Pengchong Jinf349b072014-07-14 09:13:38 -07001503#if CONFIG_FP_MB_STATS
1504 stream->config.cfg.rc_firstpass_mb_stats_in =
1505 stats_get(&stream->fpmb_stats);
1506#endif
1507 }
Yunqing Wangaabae972012-02-29 08:24:53 -05001508
John Koleszar6ad3b742012-11-06 12:02:42 -08001509 stream->cx_time = 0;
1510 stream->nbytes = 0;
1511 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001512}
1513
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001514static void initialize_encoder(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001515 struct AvxEncoderConfig *global) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001516 int i;
1517 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001518
Yaowu Xuf883b422016-08-30 14:01:10 -07001519 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1520 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
1521#if CONFIG_AOM_HIGHBITDEPTH
1522 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001523#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001524
John Koleszar6ad3b742012-11-06 12:02:42 -08001525 /* Construct Encoder Context */
Yaowu Xuf883b422016-08-30 14:01:10 -07001526 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
John Koleszar6ad3b742012-11-06 12:02:42 -08001527 &stream->config.cfg, flags);
1528 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08001529
Yaowu Xuf883b422016-08-30 14:01:10 -07001530 /* Note that we bypass the aom_codec_control wrapper macro because
John Koleszar6ad3b742012-11-06 12:02:42 -08001531 * we're being clever to store the control IDs in an array. Real
1532 * applications will want to make use of the enumerations directly
1533 */
1534 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1535 int ctrl = stream->config.arg_ctrls[i][0];
1536 int value = stream->config.arg_ctrls[i][1];
Yaowu Xuf883b422016-08-30 14:01:10 -07001537 if (aom_codec_control_(&stream->encoder, ctrl, value))
clang-format6c4d83e2016-08-08 19:03:30 -07001538 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08001539
John Koleszar6ad3b742012-11-06 12:02:42 -08001540 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1541 }
1542
John Koleszar5ebe94f2012-12-23 07:20:10 -08001543#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001544 if (global->test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001545 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1546 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
1547 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001548
Yaowu Xuf883b422016-08-30 14:01:10 -07001549#if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
1550 if (strcmp(global->codec->name, "av1") == 0) {
1551 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001552 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1553
Yaowu Xuf883b422016-08-30 14:01:10 -07001554 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
Yunqing Wang8e5e3382016-05-05 16:42:57 -07001555 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1556 }
1557#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001558 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001559#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08001560}
1561
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001562static void encode_frame(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001563 struct AvxEncoderConfig *global, struct aom_image *img,
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001564 unsigned int frames_in) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001565 aom_codec_pts_t frame_start, next_frame_start;
1566 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1567 struct aom_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08001568
clang-format6c4d83e2016-08-08 19:03:30 -07001569 frame_start =
1570 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1571 cfg->g_timebase.num / global->framerate.num;
1572 next_frame_start =
1573 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1574 cfg->g_timebase.num / global->framerate.num;
John Koleszar34882b92012-03-01 12:50:40 -08001575
clang-format6c4d83e2016-08-08 19:03:30 -07001576/* Scale if necessary */
Yaowu Xuf883b422016-08-30 14:01:10 -07001577#if CONFIG_AOM_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001578 if (img) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001579 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001580 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001581 if (img->fmt != AOM_IMG_FMT_I42016) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001582 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1583 exit(EXIT_FAILURE);
1584 }
1585#if CONFIG_LIBYUV
1586 if (!stream->img) {
clang-format6c4d83e2016-08-08 19:03:30 -07001587 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001588 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001589 }
clang-format6c4d83e2016-08-08 19:03:30 -07001590 I420Scale_16(
Yaowu Xuf883b422016-08-30 14:01:10 -07001591 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1592 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1593 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1594 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1595 stream->img->stride[AOM_PLANE_Y] / 2,
1596 (uint16 *)stream->img->planes[AOM_PLANE_U],
1597 stream->img->stride[AOM_PLANE_U] / 2,
1598 (uint16 *)stream->img->planes[AOM_PLANE_V],
1599 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
clang-format6c4d83e2016-08-08 19:03:30 -07001600 stream->img->d_h, kFilterBox);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001601 img = stream->img;
1602#else
clang-format6c4d83e2016-08-08 19:03:30 -07001603 stream->encoder.err = 1;
1604 ctx_exit_on_error(&stream->encoder,
1605 "Stream %d: Failed to encode frame.\n"
1606 "Scaling disabled in this configuration. \n"
1607 "To enable, configure with --enable-libyuv\n",
1608 stream->index);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001609#endif
1610 }
1611 }
1612#endif
John Koleszar34882b92012-03-01 12:50:40 -08001613 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001614 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
Alex Converse2a3092f2014-05-16 18:49:04 -07001615 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1616 exit(EXIT_FAILURE);
1617 }
Deb Mukherjee47031c02014-05-16 18:52:01 -07001618#if CONFIG_LIBYUV
John Koleszar34882b92012-03-01 12:50:40 -08001619 if (!stream->img)
clang-format6c4d83e2016-08-08 19:03:30 -07001620 stream->img =
Yaowu Xuf883b422016-08-30 14:01:10 -07001621 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
clang-format6c4d83e2016-08-08 19:03:30 -07001622 I420Scale(
Yaowu Xuf883b422016-08-30 14:01:10 -07001623 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1624 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1625 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1626 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1627 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1628 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
clang-format6c4d83e2016-08-08 19:03:30 -07001629 stream->img->d_w, stream->img->d_h, kFilterBox);
John Koleszar34882b92012-03-01 12:50:40 -08001630 img = stream->img;
Deb Mukherjee47031c02014-05-16 18:52:01 -07001631#else
1632 stream->encoder.err = 1;
1633 ctx_exit_on_error(&stream->encoder,
1634 "Stream %d: Failed to encode frame.\n"
1635 "Scaling disabled in this configuration. \n"
1636 "To enable, configure with --enable-libyuv\n",
1637 stream->index);
1638#endif
John Koleszar34882b92012-03-01 12:50:40 -08001639 }
1640
Yaowu Xuf883b422016-08-30 14:01:10 -07001641 aom_usec_timer_start(&timer);
1642 aom_codec_encode(&stream->encoder, img, frame_start,
clang-format6c4d83e2016-08-08 19:03:30 -07001643 (unsigned long)(next_frame_start - frame_start), 0,
1644 global->deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -07001645 aom_usec_timer_mark(&timer);
1646 stream->cx_time += aom_usec_timer_elapsed(&timer);
John Koleszar6ad3b742012-11-06 12:02:42 -08001647 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1648 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001649}
1650
John Koleszar6ad3b742012-11-06 12:02:42 -08001651static void update_quantizer_histogram(struct stream_state *stream) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001652 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001653 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08001654
Yaowu Xuf883b422016-08-30 14:01:10 -07001655 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
John Koleszar6ad3b742012-11-06 12:02:42 -08001656 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1657 stream->counts[q]++;
1658 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001659}
1660
Tom Finegan49dc9ca2013-11-21 16:46:40 -08001661static void get_cx_data(struct stream_state *stream,
Yaowu Xuf883b422016-08-30 14:01:10 -07001662 struct AvxEncoderConfig *global, int *got_data) {
1663 const aom_codec_cx_pkt_t *pkt;
1664 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1665 aom_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001666
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08001667 *got_data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001668 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001669 static size_t fsize = 0;
James Zerne3578af2014-04-17 10:47:08 -07001670 static int64_t ivf_header_pos = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001671
John Koleszar6ad3b742012-11-06 12:02:42 -08001672 switch (pkt->kind) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001673 case AOM_CODEC_CX_FRAME_PKT:
1674 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001675 stream->frames_out++;
1676 }
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001677 if (!global->quiet)
1678 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08001679
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08001680 update_rate_histogram(stream->rate_hist, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001681#if CONFIG_WEBM_IO
John Koleszar6ad3b742012-11-06 12:02:42 -08001682 if (stream->config.write_webm) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -07001683 write_webm_block(&stream->webm_ctx, cfg, pkt);
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07001684 }
1685#endif
1686 if (!stream->config.write_webm) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001687 if (pkt->data.frame.partition_id <= 0) {
1688 ivf_header_pos = ftello(stream->file);
1689 fsize = pkt->data.frame.sz;
1690
Dmitry Kovalev373b0f92014-01-29 17:57:21 -08001691 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001692 } else {
1693 fsize += pkt->data.frame.sz;
1694
Yaowu Xuf883b422016-08-30 14:01:10 -07001695 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
James Zerne3578af2014-04-17 10:47:08 -07001696 const int64_t currpos = ftello(stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001697 fseeko(stream->file, ivf_header_pos, SEEK_SET);
Tom Finegan00a35aa2013-11-14 12:37:42 -08001698 ivf_write_frame_size(stream->file, fsize);
John Koleszar6ad3b742012-11-06 12:02:42 -08001699 fseeko(stream->file, currpos, SEEK_SET);
1700 }
1701 }
1702
clang-format6c4d83e2016-08-08 19:03:30 -07001703 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1704 stream->file);
John Koleszar6ad3b742012-11-06 12:02:42 -08001705 }
1706 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02001707
John Koleszar9e50ed72012-02-15 12:39:38 -08001708 *got_data = 1;
John Koleszar5ebe94f2012-12-23 07:20:10 -08001709#if CONFIG_DECODERS
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001710 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001711 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
Tom Finegan7a691f12014-02-10 14:55:25 -08001712 (unsigned int)pkt->data.frame.sz, NULL, 0);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001713 if (stream->decoder.err) {
1714 warn_or_exit_on_error(&stream->decoder,
1715 global->test_decode == TEST_DECODE_FATAL,
1716 "Failed to decode frame %d in stream %d",
1717 stream->frames_out + 1, stream->index);
1718 stream->mismatch_seen = stream->frames_out + 1;
1719 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001720 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08001721#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001722 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07001723 case AOM_CODEC_STATS_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001724 stream->frames_out++;
clang-format6c4d83e2016-08-08 19:03:30 -07001725 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
John Koleszar6ad3b742012-11-06 12:02:42 -08001726 pkt->data.twopass_stats.sz);
1727 stream->nbytes += pkt->data.raw.sz;
1728 break;
Pengchong Jinf349b072014-07-14 09:13:38 -07001729#if CONFIG_FP_MB_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07001730 case AOM_CODEC_FPMB_STATS_PKT:
clang-format6c4d83e2016-08-08 19:03:30 -07001731 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
Pengchong Jinf349b072014-07-14 09:13:38 -07001732 pkt->data.firstpass_mb_stats.sz);
1733 stream->nbytes += pkt->data.raw.sz;
1734 break;
1735#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001736 case AOM_CODEC_PSNR_PKT:
John Koleszar6ad3b742012-11-06 12:02:42 -08001737
1738 if (global->show_psnr) {
1739 int i;
1740
1741 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1742 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1743 for (i = 0; i < 4; i++) {
Jingning Hanbabbd5d2013-02-13 09:03:21 -08001744 if (!global->quiet)
1745 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
John Koleszar6ad3b742012-11-06 12:02:42 -08001746 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1747 }
1748 stream->psnr_count++;
1749 }
1750
1751 break;
clang-format6c4d83e2016-08-08 19:03:30 -07001752 default: break;
John Koleszar9e50ed72012-02-15 12:39:38 -08001753 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001754 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001755}
1756
clang-format6c4d83e2016-08-08 19:03:30 -07001757static void show_psnr(struct stream_state *stream, double peak) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001758 int i;
1759 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08001760
clang-format6c4d83e2016-08-08 19:03:30 -07001761 if (!stream->psnr_count) return;
John Koleszar9e50ed72012-02-15 12:39:38 -08001762
John Koleszar6ad3b742012-11-06 12:02:42 -08001763 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
Deb Mukherjeea160d722014-09-30 21:56:33 -07001764 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -08001765 (double)stream->psnr_sse_total);
John Koleszar6ad3b742012-11-06 12:02:42 -08001766 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08001767
John Koleszar6ad3b742012-11-06 12:02:42 -08001768 for (i = 0; i < 4; i++) {
1769 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1770 }
1771 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08001772}
1773
John Koleszar25b6e9f2013-02-12 21:17:56 -08001774static float usec_to_fps(uint64_t usec, unsigned int frames) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001775 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08001776}
1777
clang-format6c4d83e2016-08-08 19:03:30 -07001778static void test_decode(struct stream_state *stream,
John Koleszarb3c350a2013-03-13 12:15:43 -07001779 enum TestDecodeFatality fatal,
Yaowu Xuf883b422016-08-30 14:01:10 -07001780 const AvxInterface *codec) {
1781 aom_image_t enc_img, dec_img;
John Koleszarb3c350a2013-03-13 12:15:43 -07001782
clang-format6c4d83e2016-08-08 19:03:30 -07001783 if (stream->mismatch_seen) return;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001784
John Koleszarb3c350a2013-03-13 12:15:43 -07001785 /* Get the internal reference frame */
Dmitry Kovalev70d96642014-02-11 21:12:23 -08001786 if (strcmp(codec->name, "vp8") == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001787 struct aom_ref_frame ref_enc, ref_dec;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001788 const unsigned int frame_width = (stream->config.cfg.g_w + 15) & ~15;
1789 const unsigned int frame_height = (stream->config.cfg.g_h + 15) & ~15;
1790 aom_img_alloc(&ref_enc.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001791 enc_img = ref_enc.img;
Urvang Joshi4145bf02016-10-17 14:53:33 -07001792 aom_img_alloc(&ref_dec.img, AOM_IMG_FMT_I420, frame_width, frame_height, 1);
John Koleszarb3c350a2013-03-13 12:15:43 -07001793 dec_img = ref_dec.img;
1794
Yaowu Xuf883b422016-08-30 14:01:10 -07001795 ref_enc.frame_type = AOM_LAST_FRAME;
1796 ref_dec.frame_type = AOM_LAST_FRAME;
1797 aom_codec_control(&stream->encoder, AOM_COPY_REFERENCE, &ref_enc);
1798 aom_codec_control(&stream->decoder, AOM_COPY_REFERENCE, &ref_dec);
John Koleszarb3c350a2013-03-13 12:15:43 -07001799 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001800 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1801 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
John Koleszarb3c350a2013-03-13 12:15:43 -07001802
Yaowu Xuf883b422016-08-30 14:01:10 -07001803#if CONFIG_AOM_HIGHBITDEPTH
1804 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1805 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1806 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1807 aom_image_t enc_hbd_img;
1808 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001809 enc_img.d_w, enc_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001810 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001811 enc_img = enc_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001812 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001813 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1814 aom_image_t dec_hbd_img;
1815 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001816 dec_img.d_w, dec_img.d_h, 16);
Yaowu Xuf883b422016-08-30 14:01:10 -07001817 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
Zoe Liua63147a2016-05-19 16:46:10 -07001818 dec_img = dec_hbd_img;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001819 }
1820 }
1821#endif
John Koleszarb3c350a2013-03-13 12:15:43 -07001822 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001823 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
John Koleszar6ad3b742012-11-06 12:02:42 -08001824 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08001825
John Koleszarb3c350a2013-03-13 12:15:43 -07001826 if (!compare_img(&enc_img, &dec_img)) {
Deb Mukherjee23144d22013-03-12 14:21:08 -07001827 int y[4], u[4], v[4];
Yaowu Xuf883b422016-08-30 14:01:10 -07001828#if CONFIG_AOM_HIGHBITDEPTH
1829 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001830 find_mismatch_high(&enc_img, &dec_img, y, u, v);
1831 } else {
1832 find_mismatch(&enc_img, &dec_img, y, u, v);
1833 }
1834#else
John Koleszarb3c350a2013-03-13 12:15:43 -07001835 find_mismatch(&enc_img, &dec_img, y, u, v);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001836#endif
Ronald S. Bultje97dd7342013-03-04 14:12:49 -08001837 stream->decoder.err = 1;
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08001838 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
Deb Mukherjee23144d22013-03-12 14:21:08 -07001839 "Stream %d: Encode/decode mismatch on frame %d at"
1840 " Y[%d, %d] {%d/%d},"
1841 " U[%d, %d] {%d/%d},"
1842 " V[%d, %d] {%d/%d}",
clang-format6c4d83e2016-08-08 19:03:30 -07001843 stream->index, stream->frames_out, y[0], y[1], y[2],
1844 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 -08001845 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08001846 }
John Koleszarb3c350a2013-03-13 12:15:43 -07001847
Yaowu Xuf883b422016-08-30 14:01:10 -07001848 aom_img_free(&enc_img);
1849 aom_img_free(&dec_img);
John Koleszar6ad3b742012-11-06 12:02:42 -08001850}
John Koleszarefd54f82012-02-13 16:52:18 -08001851
John Koleszar25b6e9f2013-02-12 21:17:56 -08001852static void print_time(const char *label, int64_t etl) {
Tom Finegan7a691f12014-02-10 14:55:25 -08001853 int64_t hours;
1854 int64_t mins;
1855 int64_t secs;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001856
1857 if (etl >= 0) {
1858 hours = etl / 3600;
1859 etl -= hours * 3600;
1860 mins = etl / 60;
1861 etl -= mins * 60;
1862 secs = etl;
1863
clang-format6c4d83e2016-08-08 19:03:30 -07001864 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1865 hours, mins, secs);
John Koleszar25b6e9f2013-02-12 21:17:56 -08001866 } else {
1867 fprintf(stderr, "[%3s unknown] ", label);
1868 }
1869}
1870
Tom Finegan44dd3272013-11-20 17:18:28 -08001871int main(int argc, const char **argv_) {
1872 int pass;
Yaowu Xuf883b422016-08-30 14:01:10 -07001873 aom_image_t raw;
1874#if CONFIG_AOM_HIGHBITDEPTH
1875 aom_image_t raw_shift;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001876 int allocated_raw_shift = 0;
1877 int use_16bit_internal = 0;
1878 int input_shift = 0;
1879#endif
Tom Finegan44dd3272013-11-20 17:18:28 -08001880 int frame_avail, got_data;
1881
Yaowu Xuf883b422016-08-30 14:01:10 -07001882 struct AvxInputContext input;
1883 struct AvxEncoderConfig global;
Tom Finegan44dd3272013-11-20 17:18:28 -08001884 struct stream_state *streams = NULL;
1885 char **argv, **argi;
1886 uint64_t cx_time = 0;
1887 int stream_cnt = 0;
1888 int res = 0;
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001889 int profile_updated = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08001890
Yaowu Xu618e7ef2014-07-11 16:27:21 -07001891 memset(&input, 0, sizeof(input));
John Koleszar6ad3b742012-11-06 12:02:42 -08001892 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08001893
clang-format6c4d83e2016-08-08 19:03:30 -07001894 if (argc < 3) usage_exit();
John Koleszar6ad3b742012-11-06 12:02:42 -08001895
1896 /* Setup default input stream settings */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001897 input.framerate.numerator = 30;
1898 input.framerate.denominator = 1;
John Koleszar8dd82872013-05-06 11:01:35 -07001899 input.only_i420 = 1;
Deb Mukherjee5820c5d2014-06-12 16:53:13 -07001900 input.bit_depth = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08001901
1902 /* First parse the global configuration values, because we want to apply
1903 * other parameters on top of the default configuration provided by the
1904 * codec.
1905 */
1906 argv = argv_dup(argc - 1, argv_ + 1);
1907 parse_global_config(&global, argv);
1908
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001909 switch (global.color_type) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001910 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1911 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1912 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1913 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1914 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
Deb Mukherjee090f4d42014-07-16 09:37:13 -07001915 }
Tom Finegan44dd3272013-11-20 17:18:28 -08001916
John Koleszar6ad3b742012-11-06 12:02:42 -08001917 {
1918 /* Now parse each stream's parameters. Using a local scope here
1919 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1920 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08001921 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001922 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001923
John Koleszar6ad3b742012-11-06 12:02:42 -08001924 do {
1925 stream = new_stream(&global, stream);
1926 stream_cnt++;
clang-format6c4d83e2016-08-08 19:03:30 -07001927 if (!streams) streams = stream;
John Koleszar6ad3b742012-11-06 12:02:42 -08001928 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07001929 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001930
John Koleszarc6b90392012-07-13 15:21:29 -07001931 /* Check for unrecognized options */
1932 for (argi = argv; *argi; argi++)
1933 if (argi[0][0] == '-' && argi[0][1])
1934 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001935
clang-format6c4d83e2016-08-08 19:03:30 -07001936 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1937 &stream->config.cfg););
Tom Finegan249366b2013-11-25 12:05:19 -08001938
John Koleszarc6b90392012-07-13 15:21:29 -07001939 /* Handle non-option arguments */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001940 input.filename = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04001941
clang-format6c4d83e2016-08-08 19:03:30 -07001942 if (!input.filename) usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04001943
John Koleszar8dd82872013-05-06 11:01:35 -07001944 /* Decide if other chroma subsamplings than 4:2:0 are supported */
Yaowu Xuf883b422016-08-30 14:01:10 -07001945 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
John Koleszar8dd82872013-05-06 11:01:35 -07001946
John Koleszar6ad3b742012-11-06 12:02:42 -08001947 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
John Koleszar2bf563c2013-03-11 15:03:00 -07001948 int frames_in = 0, seen_frames = 0;
John Koleszar25b6e9f2013-02-12 21:17:56 -08001949 int64_t estimated_time_left = -1;
1950 int64_t average_rate = -1;
James Zerne3578af2014-04-17 10:47:08 -07001951 int64_t lagged_count = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001952
John Koleszar6ad3b742012-11-06 12:02:42 -08001953 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001954
John Koleszar6ad3b742012-11-06 12:02:42 -08001955 /* If the input file doesn't specify its w/h (raw files), try to get
1956 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07001957 */
Deb Mukherjeea349ee32014-10-13 14:27:53 -07001958 if (!input.width || !input.height) {
1959 FOREACH_STREAM({
1960 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1961 input.width = stream->config.cfg.g_w;
1962 input.height = stream->config.cfg.g_h;
1963 break;
1964 }
1965 });
1966 }
John Koleszarc6b90392012-07-13 15:21:29 -07001967
John Koleszar6ad3b742012-11-06 12:02:42 -08001968 /* Update stream configurations from the input file's parameters */
Tom Finegan00a35aa2013-11-14 12:37:42 -08001969 if (!input.width || !input.height)
clang-format6c4d83e2016-08-08 19:03:30 -07001970 fatal(
1971 "Specify stream dimensions with --width (-w) "
1972 " and --height (-h)");
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001973
1974 /* If input file does not specify bit-depth but input-bit-depth parameter
1975 * exists, assume that to be the input bit-depth. However, if the
1976 * input-bit-depth paramter does not exist, assume the input bit-depth
1977 * to be the same as the codec bit-depth.
1978 */
1979 if (!input.bit_depth) {
1980 FOREACH_STREAM({
1981 if (stream->config.cfg.g_input_bit_depth)
1982 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1983 else
1984 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1985 (int)stream->config.cfg.g_bit_depth;
1986 });
Yaowu Xuf883b422016-08-30 14:01:10 -07001987 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001988 } else {
clang-format6c4d83e2016-08-08 19:03:30 -07001989 FOREACH_STREAM(
1990 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07001991 }
1992
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02001993#if CONFIG_AOM_HIGHBITDEPTH
Thomas Daedec0dca3c2016-02-25 17:23:17 -08001994 FOREACH_STREAM({
Thomas Daede8ec53b22016-09-19 13:24:51 -07001995 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
1996 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
1997 was selected. */
1998 switch (stream->config.cfg.g_profile) {
1999 case 0:
2000 stream->config.cfg.g_profile = 1;
2001 profile_updated = 1;
2002 break;
2003 case 2:
2004 stream->config.cfg.g_profile = 3;
2005 profile_updated = 1;
2006 break;
2007 default: break;
2008 }
2009 }
Thomas Daede8ec53b22016-09-19 13:24:51 -07002010 /* Automatically set the codec bit depth to match the input bit depth.
2011 * Upgrade the profile if required. */
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002012 if (stream->config.cfg.g_input_bit_depth >
2013 (unsigned int)stream->config.cfg.g_bit_depth) {
2014 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2015 }
2016 if (stream->config.cfg.g_bit_depth > 8) {
2017 switch (stream->config.cfg.g_profile) {
2018 case 0:
2019 stream->config.cfg.g_profile = 2;
2020 profile_updated = 1;
2021 break;
2022 case 1:
2023 stream->config.cfg.g_profile = 3;
2024 profile_updated = 1;
2025 break;
2026 default: break;
2027 }
2028 }
2029 if (stream->config.cfg.g_profile > 1) {
2030 stream->config.use_16bit_internal = 1;
2031 }
2032 if (profile_updated) {
2033 fprintf(stderr,
2034 "Warning: automatically upgrading to profile %d to "
2035 "match input format.\n",
2036 stream->config.cfg.g_profile);
2037 }
2038 });
Arild Fuldseth (arilfuld)59622cf2016-10-24 10:37:37 +02002039#else
2040 FOREACH_STREAM({
2041 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016) {
2042 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2043 was selected. */
2044 switch (stream->config.cfg.g_profile) {
2045 case 0:
2046 stream->config.cfg.g_profile = 1;
2047 profile_updated = 1;
2048 break;
2049 case 2:
2050 stream->config.cfg.g_profile = 3;
2051 profile_updated = 1;
2052 break;
2053 default: break;
2054 }
2055 }
2056 if (profile_updated) {
2057 fprintf(stderr,
2058 "Warning: automatically upgrading to profile %d to "
2059 "match input format.\n",
2060 stream->config.cfg.g_profile);
2061 }
2062 });
2063#endif
Thomas Daedec0dca3c2016-02-25 17:23:17 -08002064
Tom Finegan00a35aa2013-11-14 12:37:42 -08002065 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
Alex Conversed66bd222014-02-21 10:52:09 -08002066 FOREACH_STREAM(validate_stream_config(stream, &global));
John Koleszar77e6b452010-11-03 13:58:40 -04002067
John Koleszar6ad3b742012-11-06 12:02:42 -08002068 /* Ensure that --passes and --pass are consistent. If --pass is set and
2069 * --passes=2, ensure --fpf was set.
2070 */
2071 if (global.pass && global.passes == 2)
clang-format6c4d83e2016-08-08 19:03:30 -07002072 FOREACH_STREAM({
2073 if (!stream->config.stats_fn)
clang-format67948d32016-09-07 22:40:40 -07002074 die("Stream %d: Must specify --fpf when --pass=%d"
clang-format6c4d83e2016-08-08 19:03:30 -07002075 " and --passes=2\n",
2076 stream->index, global.pass);
2077 });
John Koleszar3245d462010-06-10 17:10:12 -04002078
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002079#if !CONFIG_WEBM_IO
2080 FOREACH_STREAM({
Ronald S. Bultje39775072015-12-16 13:35:43 -05002081 if (stream->config.write_webm) {
2082 stream->config.write_webm = 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002083 warn(
Yaowu Xuf883b422016-08-30 14:01:10 -07002084 "aomenc was compiled without WebM container support."
clang-format6c4d83e2016-08-08 19:03:30 -07002085 "Producing IVF output");
Ronald S. Bultje39775072015-12-16 13:35:43 -05002086 }
Vignesh Venkatasubramanian0ffa3832014-03-14 08:10:35 -07002087 });
2088#endif
2089
John Koleszar6ad3b742012-11-06 12:02:42 -08002090 /* Use the frame rate from the file only if none was specified
2091 * on the command-line.
2092 */
Tom Finegan00a35aa2013-11-14 12:37:42 -08002093 if (!global.have_framerate) {
2094 global.framerate.num = input.framerate.numerator;
2095 global.framerate.den = input.framerate.denominator;
Ronald S. Bultje9134e9f2016-01-18 14:03:45 -05002096 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
2097 stream->config.cfg.g_timebase.num = global.framerate.den);
Tom Finegan00a35aa2013-11-14 12:37:42 -08002098 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002099
John Koleszar6ad3b742012-11-06 12:02:42 -08002100 /* Show configuration */
2101 if (global.verbose && pass == 0)
2102 FOREACH_STREAM(show_stream_config(stream, &global, &input));
2103
2104 if (pass == (global.pass ? global.pass - 1 : 0)) {
2105 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002106 /*The Y4M reader does its own allocation.
2107 Just initialize this here to avoid problems if we never read any
2108 frames.*/
2109 memset(&raw, 0, sizeof(raw));
2110 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002111 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002112
clang-format6c4d83e2016-08-08 19:03:30 -07002113 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
2114 &stream->config.cfg, &global.framerate));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002115 }
2116
John Koleszar6ad3b742012-11-06 12:02:42 -08002117 FOREACH_STREAM(setup_pass(stream, &global, pass));
clang-format6c4d83e2016-08-08 19:03:30 -07002118 FOREACH_STREAM(
2119 open_output_file(stream, &global, &input.pixel_aspect_ratio));
John Koleszar6ad3b742012-11-06 12:02:42 -08002120 FOREACH_STREAM(initialize_encoder(stream, &global));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002121
Yaowu Xuf883b422016-08-30 14:01:10 -07002122#if CONFIG_AOM_HIGHBITDEPTH
2123 if (strcmp(global.codec->name, "av1") == 0 ||
2124 strcmp(global.codec->name, "av1") == 0) {
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002125 // Check to see if at least one stream uses 16 bit internal.
2126 // Currently assume that the bit_depths for all streams using
2127 // highbitdepth are the same.
2128 FOREACH_STREAM({
2129 if (stream->config.use_16bit_internal) {
2130 use_16bit_internal = 1;
2131 }
2132 if (stream->config.cfg.g_profile == 0) {
2133 input_shift = 0;
2134 } else {
2135 input_shift = (int)stream->config.cfg.g_bit_depth -
clang-format6c4d83e2016-08-08 19:03:30 -07002136 stream->config.cfg.g_input_bit_depth;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002137 }
2138 });
2139 }
2140#endif
2141
John Koleszarc6b90392012-07-13 15:21:29 -07002142 frame_avail = 1;
2143 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002144
John Koleszarc6b90392012-07-13 15:21:29 -07002145 while (frame_avail || got_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002146 struct aom_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002147
John Koleszar6ad3b742012-11-06 12:02:42 -08002148 if (!global.limit || frames_in < global.limit) {
2149 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002150
clang-format6c4d83e2016-08-08 19:03:30 -07002151 if (frame_avail) frames_in++;
2152 seen_frames =
2153 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002154
John Koleszar6ad3b742012-11-06 12:02:42 -08002155 if (!global.quiet) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002156 float fps = usec_to_fps(cx_time, seen_frames);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002157 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2158
John Koleszar6ad3b742012-11-06 12:02:42 -08002159 if (stream_cnt == 1)
clang-format6c4d83e2016-08-08 19:03:30 -07002160 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2161 streams->frames_out, (int64_t)streams->nbytes);
John Koleszar6ad3b742012-11-06 12:02:42 -08002162 else
John Koleszar25b6e9f2013-02-12 21:17:56 -08002163 fprintf(stderr, "frame %4d ", frames_in);
2164
clang-format6c4d83e2016-08-08 19:03:30 -07002165 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
John Koleszar25b6e9f2013-02-12 21:17:56 -08002166 cx_time > 9999999 ? cx_time / 1000 : cx_time,
clang-format6c4d83e2016-08-08 19:03:30 -07002167 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
Yaowu Xu014acfa2013-09-17 16:31:46 -07002168 fps >= 1.0 ? "fps" : "fpm");
John Koleszar25b6e9f2013-02-12 21:17:56 -08002169 print_time("ETA", estimated_time_left);
John Koleszar3245d462010-06-10 17:10:12 -04002170 }
2171
hui su251e1512016-10-03 15:48:43 -07002172 } else {
John Koleszarc6b90392012-07-13 15:21:29 -07002173 frame_avail = 0;
hui su251e1512016-10-03 15:48:43 -07002174 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002175
John Koleszar6ad3b742012-11-06 12:02:42 -08002176 if (frames_in > global.skip_frames) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002177#if CONFIG_AOM_HIGHBITDEPTH
2178 aom_image_t *frame_to_encode;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002179 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2180 assert(use_16bit_internal);
2181 // Input bit depth and stream bit depth do not match, so up
2182 // shift frame to stream bit depth
2183 if (!allocated_raw_shift) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002184 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002185 input.width, input.height, 32);
2186 allocated_raw_shift = 1;
2187 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002188 aom_img_upshift(&raw_shift, &raw, input_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002189 frame_to_encode = &raw_shift;
2190 } else {
2191 frame_to_encode = &raw;
2192 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002193 aom_usec_timer_start(&timer);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002194 if (use_16bit_internal) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002195 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002196 FOREACH_STREAM({
2197 if (stream->config.use_16bit_internal)
2198 encode_frame(stream, &global,
clang-format6c4d83e2016-08-08 19:03:30 -07002199 frame_avail ? frame_to_encode : NULL, frames_in);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002200 else
2201 assert(0);
2202 });
2203 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002204 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002205 FOREACH_STREAM(encode_frame(stream, &global,
2206 frame_avail ? frame_to_encode : NULL,
2207 frames_in));
2208 }
2209#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002210 aom_usec_timer_start(&timer);
clang-format6c4d83e2016-08-08 19:03:30 -07002211 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
John Koleszar6ad3b742012-11-06 12:02:42 -08002212 frames_in));
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002213#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002214 aom_usec_timer_mark(&timer);
2215 cx_time += aom_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002216
John Koleszar6ad3b742012-11-06 12:02:42 -08002217 FOREACH_STREAM(update_quantizer_histogram(stream));
John Koleszarc6b90392012-07-13 15:21:29 -07002218
John Koleszar0ea50ce2010-05-18 11:58:33 -04002219 got_data = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002220 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002221
Jim Bankoskic901a4f2014-08-21 07:18:07 -07002222 if (!got_data && input.length && streams != NULL &&
2223 !streams->frames_out) {
John Koleszar2bf563c2013-03-11 15:03:00 -07002224 lagged_count = global.limit ? seen_frames : ftello(input.file);
John Koleszarebf8b9f2013-02-27 11:14:23 -08002225 } else if (input.length) {
John Koleszar25b6e9f2013-02-12 21:17:56 -08002226 int64_t remaining;
2227 int64_t rate;
2228
2229 if (global.limit) {
James Zerne3578af2014-04-17 10:47:08 -07002230 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002231
2232 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
clang-format6c4d83e2016-08-08 19:03:30 -07002233 remaining = 1000 * (global.limit - global.skip_frames -
2234 seen_frames + lagged_count);
John Koleszar25b6e9f2013-02-12 21:17:56 -08002235 } else {
James Zerne3578af2014-04-17 10:47:08 -07002236 const int64_t input_pos = ftello(input.file);
2237 const int64_t input_pos_lagged = input_pos - lagged_count;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002238 const int64_t input_limit = input.length;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002239
2240 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
Urvang Joshi4145bf02016-10-17 14:53:33 -07002241 remaining = input_limit - input_pos + lagged_count;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002242 }
2243
clang-format6c4d83e2016-08-08 19:03:30 -07002244 average_rate =
2245 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
John Koleszar25b6e9f2013-02-12 21:17:56 -08002246 estimated_time_left = average_rate ? remaining / average_rate : -1;
2247 }
2248
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002249 if (got_data && global.test_decode != TEST_DECODE_OFF)
John Koleszarb3c350a2013-03-13 12:15:43 -07002250 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
John Koleszarc6b90392012-07-13 15:21:29 -07002251 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002252
John Koleszarc6b90392012-07-13 15:21:29 -07002253 fflush(stdout);
clang-format6c4d83e2016-08-08 19:03:30 -07002254 if (!global.quiet) fprintf(stderr, "\033[K");
John Koleszar0ea50ce2010-05-18 11:58:33 -04002255 }
2256
clang-format6c4d83e2016-08-08 19:03:30 -07002257 if (stream_cnt > 1) fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002258
Deb Mukherjeea160d722014-09-30 21:56:33 -07002259 if (!global.quiet) {
clang-format6c4d83e2016-08-08 19:03:30 -07002260 FOREACH_STREAM(fprintf(
2261 stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2262 "b/f %7" PRId64 "b/s"
2263 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2264 pass + 1, global.passes, frames_in, stream->frames_out,
2265 (int64_t)stream->nbytes,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002266 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
clang-format6c4d83e2016-08-08 19:03:30 -07002267 seen_frames
2268 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2269 global.framerate.den / seen_frames
2270 : 0,
Deb Mukherjeea160d722014-09-30 21:56:33 -07002271 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2272 stream->cx_time > 9999999 ? "ms" : "us",
2273 usec_to_fps(stream->cx_time, seen_frames)));
2274 }
John Koleszarc96f8e22011-06-21 16:42:33 -04002275
Deb Mukherjeea160d722014-09-30 21:56:33 -07002276 if (global.show_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002277 if (global.codec->fourcc == AV1_FOURCC) {
Deb Mukherjeea160d722014-09-30 21:56:33 -07002278 FOREACH_STREAM(
2279 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2280 } else {
2281 FOREACH_STREAM(show_psnr(stream, 255.0));
2282 }
2283 }
John Koleszarc6b90392012-07-13 15:21:29 -07002284
Yaowu Xuf883b422016-08-30 14:01:10 -07002285 FOREACH_STREAM(aom_codec_destroy(&stream->encoder));
John Koleszar6ad3b742012-11-06 12:02:42 -08002286
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002287 if (global.test_decode != TEST_DECODE_OFF) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002288 FOREACH_STREAM(aom_codec_destroy(&stream->decoder));
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002289 }
2290
John Koleszar6ad3b742012-11-06 12:02:42 -08002291 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002292
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002293 if (global.test_decode == TEST_DECODE_FATAL) {
2294 FOREACH_STREAM(res |= stream->mismatch_seen);
2295 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002296 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002297
John Koleszar6ad3b742012-11-06 12:02:42 -08002298 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
John Koleszarc6b90392012-07-13 15:21:29 -07002299
Pengchong Jinf349b072014-07-14 09:13:38 -07002300#if CONFIG_FP_MB_STATS
2301 FOREACH_STREAM(stats_close(&stream->fpmb_stats, global.passes - 1));
2302#endif
2303
clang-format6c4d83e2016-08-08 19:03:30 -07002304 if (global.pass) break;
John Koleszarc6b90392012-07-13 15:21:29 -07002305 }
2306
John Koleszar6ad3b742012-11-06 12:02:42 -08002307 if (global.show_q_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002308 FOREACH_STREAM(
2309 show_q_histogram(stream->counts, global.show_q_hist_buckets));
John Koleszar6ad3b742012-11-06 12:02:42 -08002310
2311 if (global.show_rate_hist_buckets)
clang-format6c4d83e2016-08-08 19:03:30 -07002312 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
John Koleszar6ad3b742012-11-06 12:02:42 -08002313 global.show_rate_hist_buckets));
Dmitry Kovalevf11da2b2014-01-29 12:28:29 -08002314 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
John Koleszar6ad3b742012-11-06 12:02:42 -08002315
Ronald S. Bultje31214972012-10-11 10:13:48 -07002316#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002317 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2318 * to match some existing utilities.
2319 */
Yaowu Xu47e784e2013-10-16 16:29:28 -07002320 if (!(global.pass == 1 && global.passes == 2))
2321 FOREACH_STREAM({
2322 FILE *f = fopen("opsnr.stt", "a");
2323 if (stream->mismatch_seen) {
2324 fprintf(f, "First mismatch occurred in frame %d\n",
2325 stream->mismatch_seen);
2326 } else {
2327 fprintf(f, "No mismatch detected in recon buffers\n");
2328 }
2329 fclose(f);
2330 });
Ronald S. Bultje31214972012-10-11 10:13:48 -07002331#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002332
Yaowu Xuf883b422016-08-30 14:01:10 -07002333#if CONFIG_AOM_HIGHBITDEPTH
2334 if (allocated_raw_shift) aom_img_free(&raw_shift);
Deb Mukherjee5acfafb2014-08-26 12:35:15 -07002335#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07002336 aom_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002337 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002338 free(streams);
Ronald S. Bultje9837bf42013-02-15 16:31:02 -08002339 return res ? EXIT_FAILURE : EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002340}