Merge "Remove unused buffer allocation functions" into nextgenv2
[aom.git] / aomenc.c
blob2f856d2bfc9b0b7b3dd0959aafd672ea1b585b45
1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 * 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.
12 #include "./aomenc.h"
13 #include "./aom_config.h"
15 #include <assert.h>
16 #include <limits.h>
17 #include <math.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #if CONFIG_LIBYUV
24 #include "third_party/libyuv/include/libyuv/scale.h"
25 #endif
27 #include "aom/aom_encoder.h"
28 #if CONFIG_DECODERS
29 #include "aom/aom_decoder.h"
30 #endif
32 #include "./args.h"
33 #include "./ivfenc.h"
34 #include "./tools_common.h"
36 #if CONFIG_AV1_ENCODER
37 #include "aom/aomcx.h"
38 #endif
39 #if CONFIG_AV1_DECODER
40 #include "aom/aomdx.h"
41 #endif
43 #include "aom/aom_integer.h"
44 #include "aom_ports/mem_ops.h"
45 #include "aom_ports/aom_timer.h"
46 #include "./rate_hist.h"
47 #include "./aomstats.h"
48 #include "./warnings.h"
49 #if CONFIG_WEBM_IO
50 #include "./webmenc.h"
51 #endif
52 #include "./y4minput.h"
54 /* Swallow warnings about unused results of fread/fwrite */
55 static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
56 return fread(ptr, size, nmemb, stream);
58 #define fread wrap_fread
60 static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
61 FILE *stream) {
62 return fwrite(ptr, size, nmemb, stream);
64 #define fwrite wrap_fwrite
66 static const char *exec_name;
68 static void warn_or_exit_on_errorv(aom_codec_ctx_t *ctx, int fatal,
69 const char *s, va_list ap) {
70 if (ctx->err) {
71 const char *detail = aom_codec_error_detail(ctx);
73 vfprintf(stderr, s, ap);
74 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
76 if (detail) fprintf(stderr, " %s\n", detail);
78 if (fatal) exit(EXIT_FAILURE);
82 static void ctx_exit_on_error(aom_codec_ctx_t *ctx, const char *s, ...) {
83 va_list ap;
85 va_start(ap, s);
86 warn_or_exit_on_errorv(ctx, 1, s, ap);
87 va_end(ap);
90 static void warn_or_exit_on_error(aom_codec_ctx_t *ctx, int fatal,
91 const char *s, ...) {
92 va_list ap;
94 va_start(ap, s);
95 warn_or_exit_on_errorv(ctx, fatal, s, ap);
96 va_end(ap);
99 static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
100 FILE *f = input_ctx->file;
101 y4m_input *y4m = &input_ctx->y4m;
102 int shortread = 0;
104 if (input_ctx->file_type == FILE_TYPE_Y4M) {
105 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
106 } else {
107 shortread = read_yuv_frame(input_ctx, img);
110 return !shortread;
113 static int file_is_y4m(const char detect[4]) {
114 if (memcmp(detect, "YUV4", 4) == 0) {
115 return 1;
117 return 0;
120 static int fourcc_is_ivf(const char detect[4]) {
121 if (memcmp(detect, "DKIF", 4) == 0) {
122 return 1;
124 return 0;
127 static const arg_def_t debugmode =
128 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
129 static const arg_def_t outputfile =
130 ARG_DEF("o", "output", 1, "Output filename");
131 static const arg_def_t use_yv12 =
132 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
133 static const arg_def_t use_i420 =
134 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
135 static const arg_def_t use_i422 =
136 ARG_DEF(NULL, "i422", 0, "Input file is I422");
137 static const arg_def_t use_i444 =
138 ARG_DEF(NULL, "i444", 0, "Input file is I444");
139 static const arg_def_t use_i440 =
140 ARG_DEF(NULL, "i440", 0, "Input file is I440");
141 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
142 static const arg_def_t passes =
143 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
144 static const arg_def_t pass_arg =
145 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
146 static const arg_def_t fpf_name =
147 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
148 #if CONFIG_FP_MB_STATS
149 static const arg_def_t fpmbf_name =
150 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
151 #endif
152 static const arg_def_t limit =
153 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
154 static const arg_def_t skip =
155 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
156 static const arg_def_t deadline =
157 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
158 static const arg_def_t best_dl =
159 ARG_DEF(NULL, "best", 0, "Use Best Quality Deadline");
160 static const arg_def_t good_dl =
161 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
162 static const arg_def_t rt_dl =
163 ARG_DEF(NULL, "rt", 0, "Use Realtime Quality Deadline");
164 static const arg_def_t quietarg =
165 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
166 static const arg_def_t verbosearg =
167 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
168 static const arg_def_t psnrarg =
169 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
171 static const struct arg_enum_list test_decode_enum[] = {
172 { "off", TEST_DECODE_OFF },
173 { "fatal", TEST_DECODE_FATAL },
174 { "warn", TEST_DECODE_WARN },
175 { NULL, 0 }
177 static const arg_def_t recontest = ARG_DEF_ENUM(
178 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
179 static const arg_def_t framerate =
180 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
181 static const arg_def_t use_webm =
182 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
183 static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
184 static const arg_def_t out_part =
185 ARG_DEF("P", "output-partitions", 0,
186 "Makes encoder output partitions. Requires IVF output!");
187 static const arg_def_t q_hist_n =
188 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
189 static const arg_def_t rate_hist_n =
190 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
191 static const arg_def_t disable_warnings =
192 ARG_DEF(NULL, "disable-warnings", 0,
193 "Disable warnings about potentially incorrect encode settings.");
194 static 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.");
198 #if CONFIG_AOM_HIGHBITDEPTH
199 static const arg_def_t test16bitinternalarg = ARG_DEF(
200 NULL, "test-16bit-internal", 0, "Force use of 16 bit internal buffer");
201 #endif
203 static const arg_def_t *main_args[] = { &debugmode,
204 &outputfile,
205 &codecarg,
206 &passes,
207 &pass_arg,
208 &fpf_name,
209 &limit,
210 &skip,
211 &deadline,
212 &best_dl,
213 &good_dl,
214 &rt_dl,
215 &quietarg,
216 &verbosearg,
217 &psnrarg,
218 &use_webm,
219 &use_ivf,
220 &out_part,
221 &q_hist_n,
222 &rate_hist_n,
223 &disable_warnings,
224 &disable_warning_prompt,
225 &recontest,
226 NULL };
228 static const arg_def_t usage =
229 ARG_DEF("u", "usage", 1, "Usage profile number to use");
230 static const arg_def_t threads =
231 ARG_DEF("t", "threads", 1, "Max number of threads to use");
232 static const arg_def_t profile =
233 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
234 static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
235 static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
236 #if CONFIG_WEBM_IO
237 static const struct arg_enum_list stereo_mode_enum[] = {
238 { "mono", STEREO_FORMAT_MONO },
239 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
240 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
241 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
242 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
243 { NULL, 0 }
245 static const arg_def_t stereo_mode = ARG_DEF_ENUM(
246 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
247 #endif
248 static const arg_def_t timebase = ARG_DEF(
249 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
250 static const arg_def_t error_resilient =
251 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
252 static const arg_def_t lag_in_frames =
253 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
255 static const arg_def_t *global_args[] = { &use_yv12,
256 &use_i420,
257 &use_i422,
258 &use_i444,
259 &use_i440,
260 &usage,
261 &threads,
262 &profile,
263 &width,
264 &height,
265 #if CONFIG_WEBM_IO
266 &stereo_mode,
267 #endif
268 &timebase,
269 &framerate,
270 &error_resilient,
271 #if CONFIG_AOM_HIGHBITDEPTH
272 &test16bitinternalarg,
273 #endif
274 &lag_in_frames,
275 NULL };
277 static const arg_def_t dropframe_thresh =
278 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
279 static const arg_def_t resize_allowed =
280 ARG_DEF(NULL, "resize-allowed", 1, "Spatial resampling enabled (bool)");
281 static const arg_def_t resize_width =
282 ARG_DEF(NULL, "resize-width", 1, "Width of encoded frame");
283 static const arg_def_t resize_height =
284 ARG_DEF(NULL, "resize-height", 1, "Height of encoded frame");
285 static const arg_def_t resize_up_thresh =
286 ARG_DEF(NULL, "resize-up", 1, "Upscale threshold (buf %)");
287 static const arg_def_t resize_down_thresh =
288 ARG_DEF(NULL, "resize-down", 1, "Downscale threshold (buf %)");
289 static const struct arg_enum_list end_usage_enum[] = { { "vbr", AOM_VBR },
290 { "cbr", AOM_CBR },
291 { "cq", AOM_CQ },
292 { "q", AOM_Q },
293 { NULL, 0 } };
294 static const arg_def_t end_usage =
295 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
296 static const arg_def_t target_bitrate =
297 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
298 static const arg_def_t min_quantizer =
299 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
300 static const arg_def_t max_quantizer =
301 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
302 static const arg_def_t undershoot_pct =
303 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
304 static const arg_def_t overshoot_pct =
305 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
306 static const arg_def_t buf_sz =
307 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
308 static const arg_def_t buf_initial_sz =
309 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
310 static const arg_def_t buf_optimal_sz =
311 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
312 static const arg_def_t *rc_args[] = {
313 &dropframe_thresh, &resize_allowed, &resize_width, &resize_height,
314 &resize_up_thresh, &resize_down_thresh, &end_usage, &target_bitrate,
315 &min_quantizer, &max_quantizer, &undershoot_pct, &overshoot_pct,
316 &buf_sz, &buf_initial_sz, &buf_optimal_sz, NULL
319 static const arg_def_t bias_pct =
320 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
321 static const arg_def_t minsection_pct =
322 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
323 static const arg_def_t maxsection_pct =
324 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
325 static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
326 &maxsection_pct, NULL };
328 static const arg_def_t kf_min_dist =
329 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
330 static const arg_def_t kf_max_dist =
331 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
332 static const arg_def_t kf_disabled =
333 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
334 static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
335 NULL };
337 static const arg_def_t noise_sens =
338 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
339 static const arg_def_t sharpness =
340 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
341 static const arg_def_t static_thresh =
342 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
343 static const arg_def_t auto_altref =
344 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
345 static const arg_def_t arnr_maxframes =
346 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
347 static const arg_def_t arnr_strength =
348 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
349 static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1, "AltRef type");
350 static const struct arg_enum_list tuning_enum[] = {
351 { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
353 static const arg_def_t tune_ssim =
354 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
355 static const arg_def_t cq_level =
356 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
357 static const arg_def_t max_intra_rate_pct =
358 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
360 #if CONFIG_AV1_ENCODER
361 static const arg_def_t cpu_used_av1 =
362 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (-8..8)");
363 static const arg_def_t tile_cols =
364 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
365 static const arg_def_t tile_rows =
366 ARG_DEF(NULL, "tile-rows", 1,
367 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
368 static const arg_def_t lossless =
369 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
370 #if CONFIG_AOM_QM
371 static const arg_def_t enable_qm =
372 ARG_DEF(NULL, "enable_qm", 1,
373 "Enable quantisation matrices (0: false (default), 1: true)");
374 static const arg_def_t qm_min = ARG_DEF(
375 NULL, "qm_min", 1, "Min quant matrix flatness (0..15), default is 8");
376 static const arg_def_t qm_max = ARG_DEF(
377 NULL, "qm_max", 1, "Max quant matrix flatness (0..15), default is 16");
378 #endif
379 static const arg_def_t frame_parallel_decoding = ARG_DEF(
380 NULL, "frame-parallel", 1, "Enable frame parallel decodability features");
381 static const arg_def_t aq_mode = ARG_DEF(
382 NULL, "aq-mode", 1,
383 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
384 "3: cyclic refresh, 4: equator360)");
385 static const arg_def_t frame_periodic_boost =
386 ARG_DEF(NULL, "frame-boost", 1,
387 "Enable frame periodic boost (0: off (default), 1: on)");
388 static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
389 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
390 static const arg_def_t max_inter_rate_pct =
391 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
392 static const arg_def_t min_gf_interval = ARG_DEF(
393 NULL, "min-gf-interval", 1,
394 "min gf/arf frame interval (default 0, indicating in-built behavior)");
395 static const arg_def_t max_gf_interval = ARG_DEF(
396 NULL, "max-gf-interval", 1,
397 "max gf/arf frame interval (default 0, indicating in-built behavior)");
399 static const struct arg_enum_list color_space_enum[] = {
400 { "unknown", AOM_CS_UNKNOWN },
401 { "bt601", AOM_CS_BT_601 },
402 { "bt709", AOM_CS_BT_709 },
403 { "smpte170", AOM_CS_SMPTE_170 },
404 { "smpte240", AOM_CS_SMPTE_240 },
405 { "bt2020", AOM_CS_BT_2020 },
406 { "reserved", AOM_CS_RESERVED },
407 { "sRGB", AOM_CS_SRGB },
408 { NULL, 0 }
411 static const arg_def_t input_color_space =
412 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
413 color_space_enum);
415 #if CONFIG_AOM_HIGHBITDEPTH
416 static const struct arg_enum_list bitdepth_enum[] = {
417 { "8", AOM_BITS_8 }, { "10", AOM_BITS_10 }, { "12", AOM_BITS_12 }, { NULL, 0 }
420 static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
421 "b", "bit-depth", 1,
422 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
423 bitdepth_enum);
424 static const arg_def_t inbitdeptharg =
425 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
426 #endif
428 static const struct arg_enum_list tune_content_enum[] = {
429 { "default", AOM_CONTENT_DEFAULT },
430 { "screen", AOM_CONTENT_SCREEN },
431 { NULL, 0 }
434 static const arg_def_t tune_content = ARG_DEF_ENUM(
435 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
436 #endif
438 #if CONFIG_AV1_ENCODER
439 #if CONFIG_EXT_PARTITION
440 static const struct arg_enum_list superblock_size_enum[] = {
441 { "dynamic", AOM_SUPERBLOCK_SIZE_DYNAMIC },
442 { "64", AOM_SUPERBLOCK_SIZE_64X64 },
443 { "128", AOM_SUPERBLOCK_SIZE_128X128 },
444 { NULL, 0 }
446 static const arg_def_t superblock_size = ARG_DEF_ENUM(
447 NULL, "sb-size", 1, "Superblock size to use", superblock_size_enum);
448 #endif // CONFIG_EXT_PARTITION
450 static const arg_def_t *av1_args[] = { &cpu_used_av1,
451 &auto_altref,
452 &sharpness,
453 &static_thresh,
454 &tile_cols,
455 &tile_rows,
456 &arnr_maxframes,
457 &arnr_strength,
458 &arnr_type,
459 &tune_ssim,
460 &cq_level,
461 &max_intra_rate_pct,
462 &max_inter_rate_pct,
463 &gf_cbr_boost_pct,
464 &lossless,
465 &frame_parallel_decoding,
466 &aq_mode,
467 &frame_periodic_boost,
468 &noise_sens,
469 &tune_content,
470 &input_color_space,
471 &min_gf_interval,
472 &max_gf_interval,
473 #if CONFIG_EXT_PARTITION
474 &superblock_size,
475 #endif // CONFIG_EXT_PARTITION
476 #if CONFIG_AOM_HIGHBITDEPTH
477 &bitdeptharg,
478 &inbitdeptharg,
479 #endif // CONFIG_AOM_HIGHBITDEPTH
480 NULL };
481 static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
482 AOME_SET_ENABLEAUTOALTREF,
483 AOME_SET_SHARPNESS,
484 AOME_SET_STATIC_THRESHOLD,
485 AV1E_SET_TILE_COLUMNS,
486 AV1E_SET_TILE_ROWS,
487 AOME_SET_ARNR_MAXFRAMES,
488 AOME_SET_ARNR_STRENGTH,
489 AOME_SET_ARNR_TYPE,
490 AOME_SET_TUNING,
491 AOME_SET_CQ_LEVEL,
492 AOME_SET_MAX_INTRA_BITRATE_PCT,
493 AV1E_SET_MAX_INTER_BITRATE_PCT,
494 AV1E_SET_GF_CBR_BOOST_PCT,
495 AV1E_SET_LOSSLESS,
496 AV1E_SET_FRAME_PARALLEL_DECODING,
497 AV1E_SET_AQ_MODE,
498 AV1E_SET_FRAME_PERIODIC_BOOST,
499 AV1E_SET_NOISE_SENSITIVITY,
500 AV1E_SET_TUNE_CONTENT,
501 AV1E_SET_COLOR_SPACE,
502 AV1E_SET_MIN_GF_INTERVAL,
503 AV1E_SET_MAX_GF_INTERVAL,
504 #if CONFIG_EXT_PARTITION
505 AV1E_SET_SUPERBLOCK_SIZE,
506 #endif // CONFIG_EXT_PARTITION
507 0 };
508 #endif
510 static const arg_def_t *no_args[] = { NULL };
512 void usage_exit(void) {
513 int i;
514 const int num_encoder = get_aom_encoder_count();
516 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
517 exec_name);
519 fprintf(stderr, "\nOptions:\n");
520 arg_show_usage(stderr, main_args);
521 fprintf(stderr, "\nEncoder Global Options:\n");
522 arg_show_usage(stderr, global_args);
523 fprintf(stderr, "\nRate Control Options:\n");
524 arg_show_usage(stderr, rc_args);
525 fprintf(stderr, "\nTwopass Rate Control Options:\n");
526 arg_show_usage(stderr, rc_twopass_args);
527 fprintf(stderr, "\nKeyframe Placement Options:\n");
528 arg_show_usage(stderr, kf_args);
529 #if CONFIG_AV1_ENCODER
530 fprintf(stderr, "\nAV1 Specific Options:\n");
531 arg_show_usage(stderr, av1_args);
532 #endif
533 fprintf(stderr,
534 "\nStream timebase (--timebase):\n"
535 " The desired precision of timestamps in the output, expressed\n"
536 " in fractional seconds. Default is 1/1000.\n");
537 fprintf(stderr, "\nIncluded encoders:\n\n");
539 for (i = 0; i < num_encoder; ++i) {
540 const AvxInterface *const encoder = get_aom_encoder_by_index(i);
541 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
542 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
543 aom_codec_iface_name(encoder->codec_interface()), defstr);
545 fprintf(stderr, "\n ");
546 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
548 exit(EXIT_FAILURE);
551 #define mmin(a, b) ((a) < (b) ? (a) : (b))
553 #if CONFIG_AOM_HIGHBITDEPTH
554 static void find_mismatch_high(const aom_image_t *const img1,
555 const aom_image_t *const img2, int yloc[4],
556 int uloc[4], int vloc[4]) {
557 uint16_t *plane1, *plane2;
558 uint32_t stride1, stride2;
559 const uint32_t bsize = 64;
560 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
561 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
562 const uint32_t c_w =
563 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
564 const uint32_t c_h =
565 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
566 int match = 1;
567 uint32_t i, j;
568 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
569 plane1 = (uint16_t *)img1->planes[AOM_PLANE_Y];
570 plane2 = (uint16_t *)img2->planes[AOM_PLANE_Y];
571 stride1 = img1->stride[AOM_PLANE_Y] / 2;
572 stride2 = img2->stride[AOM_PLANE_Y] / 2;
573 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
574 for (j = 0; match && j < img1->d_w; j += bsize) {
575 int k, l;
576 const int si = mmin(i + bsize, img1->d_h) - i;
577 const int sj = mmin(j + bsize, img1->d_w) - j;
578 for (k = 0; match && k < si; ++k) {
579 for (l = 0; match && l < sj; ++l) {
580 if (*(plane1 + (i + k) * stride1 + j + l) !=
581 *(plane2 + (i + k) * stride2 + j + l)) {
582 yloc[0] = i + k;
583 yloc[1] = j + l;
584 yloc[2] = *(plane1 + (i + k) * stride1 + j + l);
585 yloc[3] = *(plane2 + (i + k) * stride2 + j + l);
586 match = 0;
587 break;
594 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
595 plane1 = (uint16_t *)img1->planes[AOM_PLANE_U];
596 plane2 = (uint16_t *)img2->planes[AOM_PLANE_U];
597 stride1 = img1->stride[AOM_PLANE_U] / 2;
598 stride2 = img2->stride[AOM_PLANE_U] / 2;
599 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
600 for (j = 0; match && j < c_w; j += bsizex) {
601 int k, l;
602 const int si = mmin(i + bsizey, c_h - i);
603 const int sj = mmin(j + bsizex, c_w - j);
604 for (k = 0; match && k < si; ++k) {
605 for (l = 0; match && l < sj; ++l) {
606 if (*(plane1 + (i + k) * stride1 + j + l) !=
607 *(plane2 + (i + k) * stride2 + j + l)) {
608 uloc[0] = i + k;
609 uloc[1] = j + l;
610 uloc[2] = *(plane1 + (i + k) * stride1 + j + l);
611 uloc[3] = *(plane2 + (i + k) * stride2 + j + l);
612 match = 0;
613 break;
620 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
621 plane1 = (uint16_t *)img1->planes[AOM_PLANE_V];
622 plane2 = (uint16_t *)img2->planes[AOM_PLANE_V];
623 stride1 = img1->stride[AOM_PLANE_V] / 2;
624 stride2 = img2->stride[AOM_PLANE_V] / 2;
625 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
626 for (j = 0; match && j < c_w; j += bsizex) {
627 int k, l;
628 const int si = mmin(i + bsizey, c_h - i);
629 const int sj = mmin(j + bsizex, c_w - j);
630 for (k = 0; match && k < si; ++k) {
631 for (l = 0; match && l < sj; ++l) {
632 if (*(plane1 + (i + k) * stride1 + j + l) !=
633 *(plane2 + (i + k) * stride2 + j + l)) {
634 vloc[0] = i + k;
635 vloc[1] = j + l;
636 vloc[2] = *(plane1 + (i + k) * stride1 + j + l);
637 vloc[3] = *(plane2 + (i + k) * stride2 + j + l);
638 match = 0;
639 break;
646 #endif
648 static void find_mismatch(const aom_image_t *const img1,
649 const aom_image_t *const img2, int yloc[4],
650 int uloc[4], int vloc[4]) {
651 const uint32_t bsize = 64;
652 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
653 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
654 const uint32_t c_w =
655 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
656 const uint32_t c_h =
657 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
658 int match = 1;
659 uint32_t i, j;
660 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
661 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
662 for (j = 0; match && j < img1->d_w; j += bsize) {
663 int k, l;
664 const int si = mmin(i + bsize, img1->d_h) - i;
665 const int sj = mmin(j + bsize, img1->d_w) - j;
666 for (k = 0; match && k < si; ++k) {
667 for (l = 0; match && l < sj; ++l) {
668 if (*(img1->planes[AOM_PLANE_Y] +
669 (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
670 *(img2->planes[AOM_PLANE_Y] +
671 (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
672 yloc[0] = i + k;
673 yloc[1] = j + l;
674 yloc[2] = *(img1->planes[AOM_PLANE_Y] +
675 (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
676 yloc[3] = *(img2->planes[AOM_PLANE_Y] +
677 (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
678 match = 0;
679 break;
686 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
687 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
688 for (j = 0; match && j < c_w; j += bsizex) {
689 int k, l;
690 const int si = mmin(i + bsizey, c_h - i);
691 const int sj = mmin(j + bsizex, c_w - j);
692 for (k = 0; match && k < si; ++k) {
693 for (l = 0; match && l < sj; ++l) {
694 if (*(img1->planes[AOM_PLANE_U] +
695 (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
696 *(img2->planes[AOM_PLANE_U] +
697 (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
698 uloc[0] = i + k;
699 uloc[1] = j + l;
700 uloc[2] = *(img1->planes[AOM_PLANE_U] +
701 (i + k) * img1->stride[AOM_PLANE_U] + j + l);
702 uloc[3] = *(img2->planes[AOM_PLANE_U] +
703 (i + k) * img2->stride[AOM_PLANE_U] + j + l);
704 match = 0;
705 break;
711 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
712 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
713 for (j = 0; match && j < c_w; j += bsizex) {
714 int k, l;
715 const int si = mmin(i + bsizey, c_h - i);
716 const int sj = mmin(j + bsizex, c_w - j);
717 for (k = 0; match && k < si; ++k) {
718 for (l = 0; match && l < sj; ++l) {
719 if (*(img1->planes[AOM_PLANE_V] +
720 (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
721 *(img2->planes[AOM_PLANE_V] +
722 (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
723 vloc[0] = i + k;
724 vloc[1] = j + l;
725 vloc[2] = *(img1->planes[AOM_PLANE_V] +
726 (i + k) * img1->stride[AOM_PLANE_V] + j + l);
727 vloc[3] = *(img2->planes[AOM_PLANE_V] +
728 (i + k) * img2->stride[AOM_PLANE_V] + j + l);
729 match = 0;
730 break;
738 static int compare_img(const aom_image_t *const img1,
739 const aom_image_t *const img2) {
740 uint32_t l_w = img1->d_w;
741 uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
742 const uint32_t c_h =
743 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
744 uint32_t i;
745 int match = 1;
747 match &= (img1->fmt == img2->fmt);
748 match &= (img1->d_w == img2->d_w);
749 match &= (img1->d_h == img2->d_h);
750 #if CONFIG_AOM_HIGHBITDEPTH
751 if (img1->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
752 l_w *= 2;
753 c_w *= 2;
755 #endif
757 for (i = 0; i < img1->d_h; ++i)
758 match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
759 img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
760 l_w) == 0);
762 for (i = 0; i < c_h; ++i)
763 match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
764 img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
765 c_w) == 0);
767 for (i = 0; i < c_h; ++i)
768 match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
769 img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
770 c_w) == 0);
772 return match;
775 #define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
776 #if CONFIG_AV1_ENCODER
777 #define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
778 #endif
780 #if !CONFIG_WEBM_IO
781 typedef int stereo_format_t;
782 struct WebmOutputContext {
783 int debug;
785 #endif
787 /* Per-stream configuration */
788 struct stream_config {
789 struct aom_codec_enc_cfg cfg;
790 const char *out_fn;
791 const char *stats_fn;
792 #if CONFIG_FP_MB_STATS
793 const char *fpmb_stats_fn;
794 #endif
795 stereo_format_t stereo_fmt;
796 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
797 int arg_ctrl_cnt;
798 int write_webm;
799 #if CONFIG_AOM_HIGHBITDEPTH
800 // whether to use 16bit internal buffers
801 int use_16bit_internal;
802 #endif
805 struct stream_state {
806 int index;
807 struct stream_state *next;
808 struct stream_config config;
809 FILE *file;
810 struct rate_hist *rate_hist;
811 struct WebmOutputContext webm_ctx;
812 uint64_t psnr_sse_total;
813 uint64_t psnr_samples_total;
814 double psnr_totals[4];
815 int psnr_count;
816 int counts[64];
817 aom_codec_ctx_t encoder;
818 unsigned int frames_out;
819 uint64_t cx_time;
820 size_t nbytes;
821 stats_io_t stats;
822 #if CONFIG_FP_MB_STATS
823 stats_io_t fpmb_stats;
824 #endif
825 struct aom_image *img;
826 aom_codec_ctx_t decoder;
827 int mismatch_seen;
830 static void validate_positive_rational(const char *msg,
831 struct aom_rational *rat) {
832 if (rat->den < 0) {
833 rat->num *= -1;
834 rat->den *= -1;
837 if (rat->num < 0) die("Error: %s must be positive\n", msg);
839 if (!rat->den) die("Error: %s has zero denominator\n", msg);
842 static void parse_global_config(struct AvxEncoderConfig *global, char **argv) {
843 char **argi, **argj;
844 struct arg arg;
845 const int num_encoder = get_aom_encoder_count();
847 if (num_encoder < 1) die("Error: no valid encoder available\n");
849 /* Initialize default parameters */
850 memset(global, 0, sizeof(*global));
851 global->codec = get_aom_encoder_by_index(num_encoder - 1);
852 global->passes = 0;
853 global->color_type = I420;
854 /* Assign default deadline to good quality */
855 global->deadline = AOM_DL_GOOD_QUALITY;
857 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
858 arg.argv_step = 1;
860 if (arg_match(&arg, &codecarg, argi)) {
861 global->codec = get_aom_encoder_by_name(arg.val);
862 if (!global->codec)
863 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
864 } else if (arg_match(&arg, &passes, argi)) {
865 global->passes = arg_parse_uint(&arg);
867 if (global->passes < 1 || global->passes > 2)
868 die("Error: Invalid number of passes (%d)\n", global->passes);
869 } else if (arg_match(&arg, &pass_arg, argi)) {
870 global->pass = arg_parse_uint(&arg);
872 if (global->pass < 1 || global->pass > 2)
873 die("Error: Invalid pass selected (%d)\n", global->pass);
874 } else if (arg_match(&arg, &usage, argi))
875 global->usage = arg_parse_uint(&arg);
876 else if (arg_match(&arg, &deadline, argi))
877 global->deadline = arg_parse_uint(&arg);
878 else if (arg_match(&arg, &best_dl, argi))
879 global->deadline = AOM_DL_BEST_QUALITY;
880 else if (arg_match(&arg, &good_dl, argi))
881 global->deadline = AOM_DL_GOOD_QUALITY;
882 else if (arg_match(&arg, &rt_dl, argi))
883 global->deadline = AOM_DL_REALTIME;
884 else if (arg_match(&arg, &use_yv12, argi))
885 global->color_type = YV12;
886 else if (arg_match(&arg, &use_i420, argi))
887 global->color_type = I420;
888 else if (arg_match(&arg, &use_i422, argi))
889 global->color_type = I422;
890 else if (arg_match(&arg, &use_i444, argi))
891 global->color_type = I444;
892 else if (arg_match(&arg, &use_i440, argi))
893 global->color_type = I440;
894 else if (arg_match(&arg, &quietarg, argi))
895 global->quiet = 1;
896 else if (arg_match(&arg, &verbosearg, argi))
897 global->verbose = 1;
898 else if (arg_match(&arg, &limit, argi))
899 global->limit = arg_parse_uint(&arg);
900 else if (arg_match(&arg, &skip, argi))
901 global->skip_frames = arg_parse_uint(&arg);
902 else if (arg_match(&arg, &psnrarg, argi))
903 global->show_psnr = 1;
904 else if (arg_match(&arg, &recontest, argi))
905 global->test_decode = arg_parse_enum_or_int(&arg);
906 else if (arg_match(&arg, &framerate, argi)) {
907 global->framerate = arg_parse_rational(&arg);
908 validate_positive_rational(arg.name, &global->framerate);
909 global->have_framerate = 1;
910 } else if (arg_match(&arg, &out_part, argi))
911 global->out_part = 1;
912 else if (arg_match(&arg, &debugmode, argi))
913 global->debug = 1;
914 else if (arg_match(&arg, &q_hist_n, argi))
915 global->show_q_hist_buckets = arg_parse_uint(&arg);
916 else if (arg_match(&arg, &rate_hist_n, argi))
917 global->show_rate_hist_buckets = arg_parse_uint(&arg);
918 else if (arg_match(&arg, &disable_warnings, argi))
919 global->disable_warnings = 1;
920 else if (arg_match(&arg, &disable_warning_prompt, argi))
921 global->disable_warning_prompt = 1;
922 else
923 argj++;
926 if (global->pass) {
927 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
928 if (global->pass > global->passes) {
929 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
930 global->pass);
931 global->passes = global->pass;
934 /* Validate global config */
935 if (global->passes == 0) {
936 #if CONFIG_AV1_ENCODER
937 // Make default AV1 passes = 2 until there is a better quality 1-pass
938 // encoder
939 if (global->codec != NULL && global->codec->name != NULL)
940 global->passes = (strcmp(global->codec->name, "av1") == 0 &&
941 global->deadline != AOM_DL_REALTIME)
943 : 1;
944 #else
945 global->passes = 1;
946 #endif
949 if (global->deadline == AOM_DL_REALTIME && global->passes > 1) {
950 warn("Enforcing one-pass encoding in realtime mode\n");
951 global->passes = 1;
955 static void open_input_file(struct AvxInputContext *input) {
956 /* Parse certain options from the input file, if possible */
957 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
958 : set_binary_mode(stdin);
960 if (!input->file) fatal("Failed to open input file");
962 if (!fseeko(input->file, 0, SEEK_END)) {
963 /* Input file is seekable. Figure out how long it is, so we can get
964 * progress info.
966 input->length = ftello(input->file);
967 rewind(input->file);
970 /* Default to 1:1 pixel aspect ratio. */
971 input->pixel_aspect_ratio.numerator = 1;
972 input->pixel_aspect_ratio.denominator = 1;
974 /* For RAW input sources, these bytes will applied on the first frame
975 * in read_frame().
977 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
978 input->detect.position = 0;
980 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
981 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
982 input->only_i420) >= 0) {
983 input->file_type = FILE_TYPE_Y4M;
984 input->width = input->y4m.pic_w;
985 input->height = input->y4m.pic_h;
986 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
987 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
988 input->framerate.numerator = input->y4m.fps_n;
989 input->framerate.denominator = input->y4m.fps_d;
990 input->fmt = input->y4m.aom_fmt;
991 input->bit_depth = input->y4m.bit_depth;
992 } else
993 fatal("Unsupported Y4M stream.");
994 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
995 fatal("IVF is not supported as input.");
996 } else {
997 input->file_type = FILE_TYPE_RAW;
1001 static void close_input_file(struct AvxInputContext *input) {
1002 fclose(input->file);
1003 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
1006 static struct stream_state *new_stream(struct AvxEncoderConfig *global,
1007 struct stream_state *prev) {
1008 struct stream_state *stream;
1010 stream = calloc(1, sizeof(*stream));
1011 if (stream == NULL) {
1012 fatal("Failed to allocate new stream.");
1015 if (prev) {
1016 memcpy(stream, prev, sizeof(*stream));
1017 stream->index++;
1018 prev->next = stream;
1019 } else {
1020 aom_codec_err_t res;
1022 /* Populate encoder configuration */
1023 res = aom_codec_enc_config_default(global->codec->codec_interface(),
1024 &stream->config.cfg, global->usage);
1025 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
1027 /* Change the default timebase to a high enough value so that the
1028 * encoder will always create strictly increasing timestamps.
1030 stream->config.cfg.g_timebase.den = 1000;
1032 /* Never use the library's default resolution, require it be parsed
1033 * from the file or set on the command line.
1035 stream->config.cfg.g_w = 0;
1036 stream->config.cfg.g_h = 0;
1038 /* Initialize remaining stream parameters */
1039 stream->config.write_webm = 1;
1040 #if CONFIG_WEBM_IO
1041 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
1042 stream->webm_ctx.last_pts_ns = -1;
1043 stream->webm_ctx.writer = NULL;
1044 stream->webm_ctx.segment = NULL;
1045 #endif
1047 /* Allows removal of the application version from the EBML tags */
1048 stream->webm_ctx.debug = global->debug;
1050 /* Default lag_in_frames is 0 in realtime mode */
1051 if (global->deadline == AOM_DL_REALTIME)
1052 stream->config.cfg.g_lag_in_frames = 0;
1055 /* Output files must be specified for each stream */
1056 stream->config.out_fn = NULL;
1058 stream->next = NULL;
1059 return stream;
1062 static int parse_stream_params(struct AvxEncoderConfig *global,
1063 struct stream_state *stream, char **argv) {
1064 char **argi, **argj;
1065 struct arg arg;
1066 static const arg_def_t **ctrl_args = no_args;
1067 static const int *ctrl_args_map = NULL;
1068 struct stream_config *config = &stream->config;
1069 int eos_mark_found = 0;
1070 #if CONFIG_AOM_HIGHBITDEPTH
1071 int test_16bit_internal = 0;
1072 #endif
1074 // Handle codec specific options
1075 if (0) {
1076 #if CONFIG_AV1_ENCODER
1077 } else if (strcmp(global->codec->name, "av1") == 0) {
1078 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
1079 // Consider to expand this set for AV1 encoder control.
1080 ctrl_args = av1_args;
1081 ctrl_args_map = av1_arg_ctrl_map;
1082 #endif
1085 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1086 arg.argv_step = 1;
1088 /* Once we've found an end-of-stream marker (--) we want to continue
1089 * shifting arguments but not consuming them.
1091 if (eos_mark_found) {
1092 argj++;
1093 continue;
1094 } else if (!strcmp(*argj, "--")) {
1095 eos_mark_found = 1;
1096 continue;
1099 if (arg_match(&arg, &outputfile, argi)) {
1100 config->out_fn = arg.val;
1101 } else if (arg_match(&arg, &fpf_name, argi)) {
1102 config->stats_fn = arg.val;
1103 #if CONFIG_FP_MB_STATS
1104 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1105 config->fpmb_stats_fn = arg.val;
1106 #endif
1107 } else if (arg_match(&arg, &use_webm, argi)) {
1108 #if CONFIG_WEBM_IO
1109 config->write_webm = 1;
1110 #else
1111 die("Error: --webm specified but webm is disabled.");
1112 #endif
1113 } else if (arg_match(&arg, &use_ivf, argi)) {
1114 config->write_webm = 0;
1115 } else if (arg_match(&arg, &threads, argi)) {
1116 config->cfg.g_threads = arg_parse_uint(&arg);
1117 } else if (arg_match(&arg, &profile, argi)) {
1118 config->cfg.g_profile = arg_parse_uint(&arg);
1119 } else if (arg_match(&arg, &width, argi)) {
1120 config->cfg.g_w = arg_parse_uint(&arg);
1121 } else if (arg_match(&arg, &height, argi)) {
1122 config->cfg.g_h = arg_parse_uint(&arg);
1123 #if CONFIG_AOM_HIGHBITDEPTH
1124 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1125 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1126 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1127 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1128 #endif
1129 #if CONFIG_WEBM_IO
1130 } else if (arg_match(&arg, &stereo_mode, argi)) {
1131 config->stereo_fmt = arg_parse_enum_or_int(&arg);
1132 #endif
1133 } else if (arg_match(&arg, &timebase, argi)) {
1134 config->cfg.g_timebase = arg_parse_rational(&arg);
1135 validate_positive_rational(arg.name, &config->cfg.g_timebase);
1136 } else if (arg_match(&arg, &error_resilient, argi)) {
1137 config->cfg.g_error_resilient = arg_parse_uint(&arg);
1138 } else if (arg_match(&arg, &lag_in_frames, argi)) {
1139 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
1140 if (global->deadline == AOM_DL_REALTIME &&
1141 config->cfg.g_lag_in_frames != 0) {
1142 warn("non-zero %s option ignored in realtime mode.\n", arg.name);
1143 config->cfg.g_lag_in_frames = 0;
1145 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
1146 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
1147 } else if (arg_match(&arg, &resize_allowed, argi)) {
1148 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
1149 } else if (arg_match(&arg, &resize_width, argi)) {
1150 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
1151 } else if (arg_match(&arg, &resize_height, argi)) {
1152 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
1153 } else if (arg_match(&arg, &resize_up_thresh, argi)) {
1154 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
1155 } else if (arg_match(&arg, &resize_down_thresh, argi)) {
1156 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
1157 } else if (arg_match(&arg, &end_usage, argi)) {
1158 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1159 } else if (arg_match(&arg, &target_bitrate, argi)) {
1160 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
1161 } else if (arg_match(&arg, &min_quantizer, argi)) {
1162 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
1163 } else if (arg_match(&arg, &max_quantizer, argi)) {
1164 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
1165 } else if (arg_match(&arg, &undershoot_pct, argi)) {
1166 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1167 } else if (arg_match(&arg, &overshoot_pct, argi)) {
1168 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1169 } else if (arg_match(&arg, &buf_sz, argi)) {
1170 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1171 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
1172 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1173 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
1174 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1175 } else if (arg_match(&arg, &bias_pct, argi)) {
1176 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1177 if (global->passes < 2)
1178 warn("option %s ignored in one-pass mode.\n", arg.name);
1179 } else if (arg_match(&arg, &minsection_pct, argi)) {
1180 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1182 if (global->passes < 2)
1183 warn("option %s ignored in one-pass mode.\n", arg.name);
1184 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1185 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1187 if (global->passes < 2)
1188 warn("option %s ignored in one-pass mode.\n", arg.name);
1189 } else if (arg_match(&arg, &kf_min_dist, argi)) {
1190 config->cfg.kf_min_dist = arg_parse_uint(&arg);
1191 } else if (arg_match(&arg, &kf_max_dist, argi)) {
1192 config->cfg.kf_max_dist = arg_parse_uint(&arg);
1193 } else if (arg_match(&arg, &kf_disabled, argi)) {
1194 config->cfg.kf_mode = AOM_KF_DISABLED;
1195 #if CONFIG_AOM_HIGHBITDEPTH
1196 } else if (arg_match(&arg, &test16bitinternalarg, argi)) {
1197 if (strcmp(global->codec->name, "av1") == 0 ||
1198 strcmp(global->codec->name, "av1") == 0) {
1199 test_16bit_internal = 1;
1201 #endif
1202 } else {
1203 int i, match = 0;
1204 for (i = 0; ctrl_args[i]; i++) {
1205 if (arg_match(&arg, ctrl_args[i], argi)) {
1206 int j;
1207 match = 1;
1209 /* Point either to the next free element or the first
1210 * instance of this control.
1212 for (j = 0; j < config->arg_ctrl_cnt; j++)
1213 if (ctrl_args_map != NULL &&
1214 config->arg_ctrls[j][0] == ctrl_args_map[i])
1215 break;
1217 /* Update/insert */
1218 assert(j < (int)ARG_CTRL_CNT_MAX);
1219 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
1220 config->arg_ctrls[j][0] = ctrl_args_map[i];
1221 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
1222 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1226 if (!match) argj++;
1229 #if CONFIG_AOM_HIGHBITDEPTH
1230 if (strcmp(global->codec->name, "av1") == 0 ||
1231 strcmp(global->codec->name, "av1") == 0) {
1232 config->use_16bit_internal =
1233 test_16bit_internal | (config->cfg.g_profile > 1);
1235 #endif
1236 return eos_mark_found;
1239 #define FOREACH_STREAM(func) \
1240 do { \
1241 struct stream_state *stream; \
1242 for (stream = streams; stream; stream = stream->next) { \
1243 func; \
1245 } while (0)
1247 static void validate_stream_config(const struct stream_state *stream,
1248 const struct AvxEncoderConfig *global) {
1249 const struct stream_state *streami;
1250 (void)global;
1252 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1253 fatal(
1254 "Stream %d: Specify stream dimensions with --width (-w) "
1255 " and --height (-h)",
1256 stream->index);
1258 // Check that the codec bit depth is greater than the input bit depth.
1259 if (stream->config.cfg.g_input_bit_depth >
1260 (unsigned int)stream->config.cfg.g_bit_depth) {
1261 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1262 stream->index, (int)stream->config.cfg.g_bit_depth,
1263 stream->config.cfg.g_input_bit_depth);
1266 for (streami = stream; streami; streami = streami->next) {
1267 /* All streams require output files */
1268 if (!streami->config.out_fn)
1269 fatal("Stream %d: Output file is required (specify with -o)",
1270 streami->index);
1272 /* Check for two streams outputting to the same file */
1273 if (streami != stream) {
1274 const char *a = stream->config.out_fn;
1275 const char *b = streami->config.out_fn;
1276 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1277 fatal("Stream %d: duplicate output file (from stream %d)",
1278 streami->index, stream->index);
1281 /* Check for two streams sharing a stats file. */
1282 if (streami != stream) {
1283 const char *a = stream->config.stats_fn;
1284 const char *b = streami->config.stats_fn;
1285 if (a && b && !strcmp(a, b))
1286 fatal("Stream %d: duplicate stats file (from stream %d)",
1287 streami->index, stream->index);
1290 #if CONFIG_FP_MB_STATS
1291 /* Check for two streams sharing a mb stats file. */
1292 if (streami != stream) {
1293 const char *a = stream->config.fpmb_stats_fn;
1294 const char *b = streami->config.fpmb_stats_fn;
1295 if (a && b && !strcmp(a, b))
1296 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1297 streami->index, stream->index);
1299 #endif
1303 static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
1304 unsigned int h) {
1305 if (!stream->config.cfg.g_w) {
1306 if (!stream->config.cfg.g_h)
1307 stream->config.cfg.g_w = w;
1308 else
1309 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1311 if (!stream->config.cfg.g_h) {
1312 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1316 static const char *file_type_to_string(enum VideoFileType t) {
1317 switch (t) {
1318 case FILE_TYPE_RAW: return "RAW";
1319 case FILE_TYPE_Y4M: return "Y4M";
1320 default: return "Other";
1324 static const char *image_format_to_string(aom_img_fmt_t f) {
1325 switch (f) {
1326 case AOM_IMG_FMT_I420: return "I420";
1327 case AOM_IMG_FMT_I422: return "I422";
1328 case AOM_IMG_FMT_I444: return "I444";
1329 case AOM_IMG_FMT_I440: return "I440";
1330 case AOM_IMG_FMT_YV12: return "YV12";
1331 case AOM_IMG_FMT_I42016: return "I42016";
1332 case AOM_IMG_FMT_I42216: return "I42216";
1333 case AOM_IMG_FMT_I44416: return "I44416";
1334 case AOM_IMG_FMT_I44016: return "I44016";
1335 default: return "Other";
1339 static void show_stream_config(struct stream_state *stream,
1340 struct AvxEncoderConfig *global,
1341 struct AvxInputContext *input) {
1342 #define SHOW(field) \
1343 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
1345 if (stream->index == 0) {
1346 fprintf(stderr, "Codec: %s\n",
1347 aom_codec_iface_name(global->codec->codec_interface()));
1348 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
1349 input->filename, file_type_to_string(input->file_type),
1350 image_format_to_string(input->fmt));
1352 if (stream->next || stream->index)
1353 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1354 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1355 fprintf(stderr, "Encoder parameters:\n");
1357 SHOW(g_usage);
1358 SHOW(g_threads);
1359 SHOW(g_profile);
1360 SHOW(g_w);
1361 SHOW(g_h);
1362 SHOW(g_bit_depth);
1363 SHOW(g_input_bit_depth);
1364 SHOW(g_timebase.num);
1365 SHOW(g_timebase.den);
1366 SHOW(g_error_resilient);
1367 SHOW(g_pass);
1368 SHOW(g_lag_in_frames);
1369 SHOW(rc_dropframe_thresh);
1370 SHOW(rc_resize_allowed);
1371 SHOW(rc_scaled_width);
1372 SHOW(rc_scaled_height);
1373 SHOW(rc_resize_up_thresh);
1374 SHOW(rc_resize_down_thresh);
1375 SHOW(rc_end_usage);
1376 SHOW(rc_target_bitrate);
1377 SHOW(rc_min_quantizer);
1378 SHOW(rc_max_quantizer);
1379 SHOW(rc_undershoot_pct);
1380 SHOW(rc_overshoot_pct);
1381 SHOW(rc_buf_sz);
1382 SHOW(rc_buf_initial_sz);
1383 SHOW(rc_buf_optimal_sz);
1384 SHOW(rc_2pass_vbr_bias_pct);
1385 SHOW(rc_2pass_vbr_minsection_pct);
1386 SHOW(rc_2pass_vbr_maxsection_pct);
1387 SHOW(kf_mode);
1388 SHOW(kf_min_dist);
1389 SHOW(kf_max_dist);
1392 static void open_output_file(struct stream_state *stream,
1393 struct AvxEncoderConfig *global,
1394 const struct AvxRational *pixel_aspect_ratio) {
1395 const char *fn = stream->config.out_fn;
1396 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1398 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1400 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
1402 if (!stream->file) fatal("Failed to open output file");
1404 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1405 fatal("WebM output to pipes not supported.");
1407 #if CONFIG_WEBM_IO
1408 if (stream->config.write_webm) {
1409 stream->webm_ctx.stream = stream->file;
1410 write_webm_file_header(&stream->webm_ctx, cfg, &global->framerate,
1411 stream->config.stereo_fmt, global->codec->fourcc,
1412 pixel_aspect_ratio);
1414 #else
1415 (void)pixel_aspect_ratio;
1416 #endif
1418 if (!stream->config.write_webm) {
1419 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1423 static void close_output_file(struct stream_state *stream,
1424 unsigned int fourcc) {
1425 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1427 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1429 #if CONFIG_WEBM_IO
1430 if (stream->config.write_webm) {
1431 write_webm_file_footer(&stream->webm_ctx);
1433 #endif
1435 if (!stream->config.write_webm) {
1436 if (!fseek(stream->file, 0, SEEK_SET))
1437 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
1438 stream->frames_out);
1441 fclose(stream->file);
1444 static void setup_pass(struct stream_state *stream,
1445 struct AvxEncoderConfig *global, int pass) {
1446 if (stream->config.stats_fn) {
1447 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
1448 fatal("Failed to open statistics store");
1449 } else {
1450 if (!stats_open_mem(&stream->stats, pass))
1451 fatal("Failed to open statistics store");
1454 #if CONFIG_FP_MB_STATS
1455 if (stream->config.fpmb_stats_fn) {
1456 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1457 pass))
1458 fatal("Failed to open mb statistics store");
1459 } else {
1460 if (!stats_open_mem(&stream->fpmb_stats, pass))
1461 fatal("Failed to open mb statistics store");
1463 #endif
1465 stream->config.cfg.g_pass = global->passes == 2
1466 ? pass ? AOM_RC_LAST_PASS : AOM_RC_FIRST_PASS
1467 : AOM_RC_ONE_PASS;
1468 if (pass) {
1469 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
1470 #if CONFIG_FP_MB_STATS
1471 stream->config.cfg.rc_firstpass_mb_stats_in =
1472 stats_get(&stream->fpmb_stats);
1473 #endif
1476 stream->cx_time = 0;
1477 stream->nbytes = 0;
1478 stream->frames_out = 0;
1481 static void initialize_encoder(struct stream_state *stream,
1482 struct AvxEncoderConfig *global) {
1483 int i;
1484 int flags = 0;
1486 flags |= global->show_psnr ? AOM_CODEC_USE_PSNR : 0;
1487 flags |= global->out_part ? AOM_CODEC_USE_OUTPUT_PARTITION : 0;
1488 #if CONFIG_AOM_HIGHBITDEPTH
1489 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
1490 #endif
1492 /* Construct Encoder Context */
1493 aom_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
1494 &stream->config.cfg, flags);
1495 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
1497 /* Note that we bypass the aom_codec_control wrapper macro because
1498 * we're being clever to store the control IDs in an array. Real
1499 * applications will want to make use of the enumerations directly
1501 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1502 int ctrl = stream->config.arg_ctrls[i][0];
1503 int value = stream->config.arg_ctrls[i][1];
1504 if (aom_codec_control_(&stream->encoder, ctrl, value))
1505 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
1507 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1510 #if CONFIG_DECODERS
1511 if (global->test_decode != TEST_DECODE_OFF) {
1512 const AvxInterface *decoder = get_aom_decoder_by_name(global->codec->name);
1513 aom_codec_dec_cfg_t cfg = { 0, 0, 0 };
1514 aom_codec_dec_init(&stream->decoder, decoder->codec_interface(), &cfg, 0);
1516 #if CONFIG_AV1_DECODER && CONFIG_EXT_TILE
1517 if (strcmp(global->codec->name, "av1") == 0) {
1518 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_ROW, -1);
1519 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1521 aom_codec_control(&stream->decoder, AV1_SET_DECODE_TILE_COL, -1);
1522 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1524 #endif
1526 #endif
1529 static void encode_frame(struct stream_state *stream,
1530 struct AvxEncoderConfig *global, struct aom_image *img,
1531 unsigned int frames_in) {
1532 aom_codec_pts_t frame_start, next_frame_start;
1533 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1534 struct aom_usec_timer timer;
1536 frame_start =
1537 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1538 cfg->g_timebase.num / global->framerate.num;
1539 next_frame_start =
1540 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1541 cfg->g_timebase.num / global->framerate.num;
1543 /* Scale if necessary */
1544 #if CONFIG_AOM_HIGHBITDEPTH
1545 if (img) {
1546 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
1547 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1548 if (img->fmt != AOM_IMG_FMT_I42016) {
1549 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1550 exit(EXIT_FAILURE);
1552 #if CONFIG_LIBYUV
1553 if (!stream->img) {
1554 stream->img =
1555 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
1557 I420Scale_16(
1558 (uint16 *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1559 (uint16 *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1560 (uint16 *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1561 img->d_w, img->d_h, (uint16 *)stream->img->planes[AOM_PLANE_Y],
1562 stream->img->stride[AOM_PLANE_Y] / 2,
1563 (uint16 *)stream->img->planes[AOM_PLANE_U],
1564 stream->img->stride[AOM_PLANE_U] / 2,
1565 (uint16 *)stream->img->planes[AOM_PLANE_V],
1566 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
1567 stream->img->d_h, kFilterBox);
1568 img = stream->img;
1569 #else
1570 stream->encoder.err = 1;
1571 ctx_exit_on_error(&stream->encoder,
1572 "Stream %d: Failed to encode frame.\n"
1573 "Scaling disabled in this configuration. \n"
1574 "To enable, configure with --enable-libyuv\n",
1575 stream->index);
1576 #endif
1579 #endif
1580 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1581 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
1582 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1583 exit(EXIT_FAILURE);
1585 #if CONFIG_LIBYUV
1586 if (!stream->img)
1587 stream->img =
1588 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
1589 I420Scale(
1590 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1591 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1592 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1593 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1594 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1595 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
1596 stream->img->d_w, stream->img->d_h, kFilterBox);
1597 img = stream->img;
1598 #else
1599 stream->encoder.err = 1;
1600 ctx_exit_on_error(&stream->encoder,
1601 "Stream %d: Failed to encode frame.\n"
1602 "Scaling disabled in this configuration. \n"
1603 "To enable, configure with --enable-libyuv\n",
1604 stream->index);
1605 #endif
1608 aom_usec_timer_start(&timer);
1609 aom_codec_encode(&stream->encoder, img, frame_start,
1610 (unsigned long)(next_frame_start - frame_start), 0,
1611 global->deadline);
1612 aom_usec_timer_mark(&timer);
1613 stream->cx_time += aom_usec_timer_elapsed(&timer);
1614 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1615 stream->index);
1618 static void update_quantizer_histogram(struct stream_state *stream) {
1619 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
1620 int q;
1622 aom_codec_control(&stream->encoder, AOME_GET_LAST_QUANTIZER_64, &q);
1623 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1624 stream->counts[q]++;
1628 static void get_cx_data(struct stream_state *stream,
1629 struct AvxEncoderConfig *global, int *got_data) {
1630 const aom_codec_cx_pkt_t *pkt;
1631 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1632 aom_codec_iter_t iter = NULL;
1634 *got_data = 0;
1635 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
1636 static size_t fsize = 0;
1637 static int64_t ivf_header_pos = 0;
1639 switch (pkt->kind) {
1640 case AOM_CODEC_CX_FRAME_PKT:
1641 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1642 stream->frames_out++;
1644 if (!global->quiet)
1645 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
1647 update_rate_histogram(stream->rate_hist, cfg, pkt);
1648 #if CONFIG_WEBM_IO
1649 if (stream->config.write_webm) {
1650 write_webm_block(&stream->webm_ctx, cfg, pkt);
1652 #endif
1653 if (!stream->config.write_webm) {
1654 if (pkt->data.frame.partition_id <= 0) {
1655 ivf_header_pos = ftello(stream->file);
1656 fsize = pkt->data.frame.sz;
1658 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1659 } else {
1660 fsize += pkt->data.frame.sz;
1662 if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
1663 const int64_t currpos = ftello(stream->file);
1664 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1665 ivf_write_frame_size(stream->file, fsize);
1666 fseeko(stream->file, currpos, SEEK_SET);
1670 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1671 stream->file);
1673 stream->nbytes += pkt->data.raw.sz;
1675 *got_data = 1;
1676 #if CONFIG_DECODERS
1677 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
1678 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
1679 (unsigned int)pkt->data.frame.sz, NULL, 0);
1680 if (stream->decoder.err) {
1681 warn_or_exit_on_error(&stream->decoder,
1682 global->test_decode == TEST_DECODE_FATAL,
1683 "Failed to decode frame %d in stream %d",
1684 stream->frames_out + 1, stream->index);
1685 stream->mismatch_seen = stream->frames_out + 1;
1688 #endif
1689 break;
1690 case AOM_CODEC_STATS_PKT:
1691 stream->frames_out++;
1692 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
1693 pkt->data.twopass_stats.sz);
1694 stream->nbytes += pkt->data.raw.sz;
1695 break;
1696 #if CONFIG_FP_MB_STATS
1697 case AOM_CODEC_FPMB_STATS_PKT:
1698 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
1699 pkt->data.firstpass_mb_stats.sz);
1700 stream->nbytes += pkt->data.raw.sz;
1701 break;
1702 #endif
1703 case AOM_CODEC_PSNR_PKT:
1705 if (global->show_psnr) {
1706 int i;
1708 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1709 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1710 for (i = 0; i < 4; i++) {
1711 if (!global->quiet)
1712 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
1713 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1715 stream->psnr_count++;
1718 break;
1719 default: break;
1724 static void show_psnr(struct stream_state *stream, double peak) {
1725 int i;
1726 double ovpsnr;
1728 if (!stream->psnr_count) return;
1730 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1731 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
1732 (double)stream->psnr_sse_total);
1733 fprintf(stderr, " %.3f", ovpsnr);
1735 for (i = 0; i < 4; i++) {
1736 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1738 fprintf(stderr, "\n");
1741 static float usec_to_fps(uint64_t usec, unsigned int frames) {
1742 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
1745 static void test_decode(struct stream_state *stream,
1746 enum TestDecodeFatality fatal,
1747 const AvxInterface *codec) {
1748 aom_image_t enc_img, dec_img;
1750 if (stream->mismatch_seen) return;
1752 /* Get the internal reference frame */
1753 if (strcmp(codec->name, "vp8") == 0) {
1754 struct aom_ref_frame ref_enc, ref_dec;
1755 int width, height;
1757 width = (stream->config.cfg.g_w + 15) & ~15;
1758 height = (stream->config.cfg.g_h + 15) & ~15;
1759 aom_img_alloc(&ref_enc.img, AOM_IMG_FMT_I420, width, height, 1);
1760 enc_img = ref_enc.img;
1761 aom_img_alloc(&ref_dec.img, AOM_IMG_FMT_I420, width, height, 1);
1762 dec_img = ref_dec.img;
1764 ref_enc.frame_type = AOM_LAST_FRAME;
1765 ref_dec.frame_type = AOM_LAST_FRAME;
1766 aom_codec_control(&stream->encoder, AOM_COPY_REFERENCE, &ref_enc);
1767 aom_codec_control(&stream->decoder, AOM_COPY_REFERENCE, &ref_dec);
1768 } else {
1769 aom_codec_control(&stream->encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
1770 aom_codec_control(&stream->decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
1772 #if CONFIG_AOM_HIGHBITDEPTH
1773 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1774 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1775 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1776 aom_image_t enc_hbd_img;
1777 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1778 enc_img.d_w, enc_img.d_h, 16);
1779 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1780 enc_img = enc_hbd_img;
1782 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1783 aom_image_t dec_hbd_img;
1784 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1785 dec_img.d_w, dec_img.d_h, 16);
1786 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1787 dec_img = dec_hbd_img;
1790 #endif
1792 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
1793 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
1795 if (!compare_img(&enc_img, &dec_img)) {
1796 int y[4], u[4], v[4];
1797 #if CONFIG_AOM_HIGHBITDEPTH
1798 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1799 find_mismatch_high(&enc_img, &dec_img, y, u, v);
1800 } else {
1801 find_mismatch(&enc_img, &dec_img, y, u, v);
1803 #else
1804 find_mismatch(&enc_img, &dec_img, y, u, v);
1805 #endif
1806 stream->decoder.err = 1;
1807 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
1808 "Stream %d: Encode/decode mismatch on frame %d at"
1809 " Y[%d, %d] {%d/%d},"
1810 " U[%d, %d] {%d/%d},"
1811 " V[%d, %d] {%d/%d}",
1812 stream->index, stream->frames_out, y[0], y[1], y[2],
1813 y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
1814 stream->mismatch_seen = stream->frames_out;
1817 aom_img_free(&enc_img);
1818 aom_img_free(&dec_img);
1821 static void print_time(const char *label, int64_t etl) {
1822 int64_t hours;
1823 int64_t mins;
1824 int64_t secs;
1826 if (etl >= 0) {
1827 hours = etl / 3600;
1828 etl -= hours * 3600;
1829 mins = etl / 60;
1830 etl -= mins * 60;
1831 secs = etl;
1833 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1834 hours, mins, secs);
1835 } else {
1836 fprintf(stderr, "[%3s unknown] ", label);
1840 int main(int argc, const char **argv_) {
1841 int pass;
1842 aom_image_t raw;
1843 #if CONFIG_AOM_HIGHBITDEPTH
1844 aom_image_t raw_shift;
1845 int allocated_raw_shift = 0;
1846 int use_16bit_internal = 0;
1847 int input_shift = 0;
1848 #endif
1849 int frame_avail, got_data;
1851 struct AvxInputContext input;
1852 struct AvxEncoderConfig global;
1853 struct stream_state *streams = NULL;
1854 char **argv, **argi;
1855 uint64_t cx_time = 0;
1856 int stream_cnt = 0;
1857 int res = 0;
1859 memset(&input, 0, sizeof(input));
1860 exec_name = argv_[0];
1862 if (argc < 3) usage_exit();
1864 /* Setup default input stream settings */
1865 input.framerate.numerator = 30;
1866 input.framerate.denominator = 1;
1867 input.only_i420 = 1;
1868 input.bit_depth = 0;
1870 /* First parse the global configuration values, because we want to apply
1871 * other parameters on top of the default configuration provided by the
1872 * codec.
1874 argv = argv_dup(argc - 1, argv_ + 1);
1875 parse_global_config(&global, argv);
1877 switch (global.color_type) {
1878 case I420: input.fmt = AOM_IMG_FMT_I420; break;
1879 case I422: input.fmt = AOM_IMG_FMT_I422; break;
1880 case I444: input.fmt = AOM_IMG_FMT_I444; break;
1881 case I440: input.fmt = AOM_IMG_FMT_I440; break;
1882 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
1886 /* Now parse each stream's parameters. Using a local scope here
1887 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1888 * loops
1890 struct stream_state *stream = NULL;
1892 do {
1893 stream = new_stream(&global, stream);
1894 stream_cnt++;
1895 if (!streams) streams = stream;
1896 } while (parse_stream_params(&global, stream, argv));
1899 /* Check for unrecognized options */
1900 for (argi = argv; *argi; argi++)
1901 if (argi[0][0] == '-' && argi[0][1])
1902 die("Error: Unrecognized option %s\n", *argi);
1904 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1905 &stream->config.cfg););
1907 /* Handle non-option arguments */
1908 input.filename = argv[0];
1910 if (!input.filename) usage_exit();
1912 /* Decide if other chroma subsamplings than 4:2:0 are supported */
1913 if (global.codec->fourcc == AV1_FOURCC) input.only_i420 = 0;
1915 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
1916 int frames_in = 0, seen_frames = 0;
1917 int64_t estimated_time_left = -1;
1918 int64_t average_rate = -1;
1919 int64_t lagged_count = 0;
1921 open_input_file(&input);
1923 /* If the input file doesn't specify its w/h (raw files), try to get
1924 * the data from the first stream's configuration.
1926 if (!input.width || !input.height) {
1927 FOREACH_STREAM({
1928 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1929 input.width = stream->config.cfg.g_w;
1930 input.height = stream->config.cfg.g_h;
1931 break;
1936 /* Update stream configurations from the input file's parameters */
1937 if (!input.width || !input.height)
1938 fatal(
1939 "Specify stream dimensions with --width (-w) "
1940 " and --height (-h)");
1942 /* If input file does not specify bit-depth but input-bit-depth parameter
1943 * exists, assume that to be the input bit-depth. However, if the
1944 * input-bit-depth paramter does not exist, assume the input bit-depth
1945 * to be the same as the codec bit-depth.
1947 if (!input.bit_depth) {
1948 FOREACH_STREAM({
1949 if (stream->config.cfg.g_input_bit_depth)
1950 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1951 else
1952 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1953 (int)stream->config.cfg.g_bit_depth;
1955 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
1956 } else {
1957 FOREACH_STREAM(
1958 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
1961 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
1962 FOREACH_STREAM(validate_stream_config(stream, &global));
1964 /* Ensure that --passes and --pass are consistent. If --pass is set and
1965 * --passes=2, ensure --fpf was set.
1967 if (global.pass && global.passes == 2)
1968 FOREACH_STREAM({
1969 if (!stream->config.stats_fn)
1970 die(
1971 "Stream %d: Must specify --fpf when --pass=%d"
1972 " and --passes=2\n",
1973 stream->index, global.pass);
1976 #if !CONFIG_WEBM_IO
1977 FOREACH_STREAM({
1978 if (stream->config.write_webm) {
1979 stream->config.write_webm = 0;
1980 warn(
1981 "aomenc was compiled without WebM container support."
1982 "Producing IVF output");
1985 #endif
1987 /* Use the frame rate from the file only if none was specified
1988 * on the command-line.
1990 if (!global.have_framerate) {
1991 global.framerate.num = input.framerate.numerator;
1992 global.framerate.den = input.framerate.denominator;
1993 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
1994 stream->config.cfg.g_timebase.num = global.framerate.den);
1997 /* Show configuration */
1998 if (global.verbose && pass == 0)
1999 FOREACH_STREAM(show_stream_config(stream, &global, &input));
2001 if (pass == (global.pass ? global.pass - 1 : 0)) {
2002 if (input.file_type == FILE_TYPE_Y4M)
2003 /*The Y4M reader does its own allocation.
2004 Just initialize this here to avoid problems if we never read any
2005 frames.*/
2006 memset(&raw, 0, sizeof(raw));
2007 else
2008 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
2010 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
2011 &stream->config.cfg, &global.framerate));
2014 FOREACH_STREAM(setup_pass(stream, &global, pass));
2015 FOREACH_STREAM(
2016 open_output_file(stream, &global, &input.pixel_aspect_ratio));
2017 FOREACH_STREAM(initialize_encoder(stream, &global));
2019 #if CONFIG_AOM_HIGHBITDEPTH
2020 if (strcmp(global.codec->name, "av1") == 0 ||
2021 strcmp(global.codec->name, "av1") == 0) {
2022 // Check to see if at least one stream uses 16 bit internal.
2023 // Currently assume that the bit_depths for all streams using
2024 // highbitdepth are the same.
2025 FOREACH_STREAM({
2026 if (stream->config.use_16bit_internal) {
2027 use_16bit_internal = 1;
2029 if (stream->config.cfg.g_profile == 0) {
2030 input_shift = 0;
2031 } else {
2032 input_shift = (int)stream->config.cfg.g_bit_depth -
2033 stream->config.cfg.g_input_bit_depth;
2037 #endif
2039 frame_avail = 1;
2040 got_data = 0;
2042 while (frame_avail || got_data) {
2043 struct aom_usec_timer timer;
2045 if (!global.limit || frames_in < global.limit) {
2046 frame_avail = read_frame(&input, &raw);
2048 if (frame_avail) frames_in++;
2049 seen_frames =
2050 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
2052 if (!global.quiet) {
2053 float fps = usec_to_fps(cx_time, seen_frames);
2054 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2056 if (stream_cnt == 1)
2057 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2058 streams->frames_out, (int64_t)streams->nbytes);
2059 else
2060 fprintf(stderr, "frame %4d ", frames_in);
2062 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
2063 cx_time > 9999999 ? cx_time / 1000 : cx_time,
2064 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
2065 fps >= 1.0 ? "fps" : "fpm");
2066 print_time("ETA", estimated_time_left);
2069 } else
2070 frame_avail = 0;
2072 if (frames_in > global.skip_frames) {
2073 #if CONFIG_AOM_HIGHBITDEPTH
2074 aom_image_t *frame_to_encode;
2075 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2076 assert(use_16bit_internal);
2077 // Input bit depth and stream bit depth do not match, so up
2078 // shift frame to stream bit depth
2079 if (!allocated_raw_shift) {
2080 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
2081 input.width, input.height, 32);
2082 allocated_raw_shift = 1;
2084 aom_img_upshift(&raw_shift, &raw, input_shift);
2085 frame_to_encode = &raw_shift;
2086 } else {
2087 frame_to_encode = &raw;
2089 aom_usec_timer_start(&timer);
2090 if (use_16bit_internal) {
2091 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
2092 FOREACH_STREAM({
2093 if (stream->config.use_16bit_internal)
2094 encode_frame(stream, &global,
2095 frame_avail ? frame_to_encode : NULL, frames_in);
2096 else
2097 assert(0);
2099 } else {
2100 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
2101 FOREACH_STREAM(encode_frame(stream, &global,
2102 frame_avail ? frame_to_encode : NULL,
2103 frames_in));
2105 #else
2106 aom_usec_timer_start(&timer);
2107 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
2108 frames_in));
2109 #endif
2110 aom_usec_timer_mark(&timer);
2111 cx_time += aom_usec_timer_elapsed(&timer);
2113 FOREACH_STREAM(update_quantizer_histogram(stream));
2115 got_data = 0;
2116 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
2118 if (!got_data && input.length && streams != NULL &&
2119 !streams->frames_out) {
2120 lagged_count = global.limit ? seen_frames : ftello(input.file);
2121 } else if (input.length) {
2122 int64_t remaining;
2123 int64_t rate;
2125 if (global.limit) {
2126 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
2128 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
2129 remaining = 1000 * (global.limit - global.skip_frames -
2130 seen_frames + lagged_count);
2131 } else {
2132 const int64_t input_pos = ftello(input.file);
2133 const int64_t input_pos_lagged = input_pos - lagged_count;
2134 const int64_t limit = input.length;
2136 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
2137 remaining = limit - input_pos + lagged_count;
2140 average_rate =
2141 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
2142 estimated_time_left = average_rate ? remaining / average_rate : -1;
2145 if (got_data && global.test_decode != TEST_DECODE_OFF)
2146 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
2149 fflush(stdout);
2150 if (!global.quiet) fprintf(stderr, "\033[K");
2153 if (stream_cnt > 1) fprintf(stderr, "\n");
2155 if (!global.quiet) {
2156 FOREACH_STREAM(fprintf(
2157 stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2158 "b/f %7" PRId64 "b/s"
2159 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2160 pass + 1, global.passes, frames_in, stream->frames_out,
2161 (int64_t)stream->nbytes,
2162 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2163 seen_frames
2164 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2165 global.framerate.den / seen_frames
2166 : 0,
2167 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2168 stream->cx_time > 9999999 ? "ms" : "us",
2169 usec_to_fps(stream->cx_time, seen_frames)));
2172 if (global.show_psnr) {
2173 if (global.codec->fourcc == AV1_FOURCC) {
2174 FOREACH_STREAM(
2175 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2176 } else {
2177 FOREACH_STREAM(show_psnr(stream, 255.0));
2181 FOREACH_STREAM(aom_codec_destroy(&stream->encoder));
2183 if (global.test_decode != TEST_DECODE_OFF) {
2184 FOREACH_STREAM(aom_codec_destroy(&stream->decoder));
2187 close_input_file(&input);
2189 if (global.test_decode == TEST_DECODE_FATAL) {
2190 FOREACH_STREAM(res |= stream->mismatch_seen);
2192 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
2194 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
2196 #if CONFIG_FP_MB_STATS
2197 FOREACH_STREAM(stats_close(&stream->fpmb_stats, global.passes - 1));
2198 #endif
2200 if (global.pass) break;
2203 if (global.show_q_hist_buckets)
2204 FOREACH_STREAM(
2205 show_q_histogram(stream->counts, global.show_q_hist_buckets));
2207 if (global.show_rate_hist_buckets)
2208 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2209 global.show_rate_hist_buckets));
2210 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
2212 #if CONFIG_INTERNAL_STATS
2213 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2214 * to match some existing utilities.
2216 if (!(global.pass == 1 && global.passes == 2))
2217 FOREACH_STREAM({
2218 FILE *f = fopen("opsnr.stt", "a");
2219 if (stream->mismatch_seen) {
2220 fprintf(f, "First mismatch occurred in frame %d\n",
2221 stream->mismatch_seen);
2222 } else {
2223 fprintf(f, "No mismatch detected in recon buffers\n");
2225 fclose(f);
2227 #endif
2229 #if CONFIG_AOM_HIGHBITDEPTH
2230 if (allocated_raw_shift) aom_img_free(&raw_shift);
2231 #endif
2232 aom_img_free(&raw);
2233 free(argv);
2234 free(streams);
2235 return res ? EXIT_FAILURE : EXIT_SUCCESS;