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.
13 #include "./aom_config.h"
24 #include "third_party/libyuv/include/libyuv/scale.h"
27 #include "aom/aom_encoder.h"
29 #include "aom/aom_decoder.h"
34 #include "./tools_common.h"
36 #if CONFIG_AV1_ENCODER
37 #include "aom/aomcx.h"
39 #if CONFIG_AV1_DECODER
40 #include "aom/aomdx.h"
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"
50 #include "./webmenc.h"
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
,
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
) {
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
, ...) {
86 warn_or_exit_on_errorv(ctx
, 1, s
, ap
);
90 static void warn_or_exit_on_error(aom_codec_ctx_t
*ctx
, int fatal
,
95 warn_or_exit_on_errorv(ctx
, fatal
, s
, 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
;
104 if (input_ctx
->file_type
== FILE_TYPE_Y4M
) {
105 if (y4m_input_fetch_frame(y4m
, f
, img
) < 1) return 0;
107 shortread
= read_yuv_frame(input_ctx
, img
);
113 static int file_is_y4m(const char detect
[4]) {
114 if (memcmp(detect
, "YUV4", 4) == 0) {
120 static int fourcc_is_ivf(const char detect
[4]) {
121 if (memcmp(detect
, "DKIF", 4) == 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");
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
},
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");
203 static const arg_def_t
*main_args
[] = { &debugmode
,
224 &disable_warning_prompt
,
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");
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
},
245 static const arg_def_t stereo_mode
= ARG_DEF_ENUM(
246 NULL
, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum
);
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
,
271 #if CONFIG_AOM_HIGHBITDEPTH
272 &test16bitinternalarg
,
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
},
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
,
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)");
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");
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(
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
},
411 static const arg_def_t input_color_space
=
412 ARG_DEF_ENUM(NULL
, "color-space", 1, "The color space of input content:",
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(
422 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
424 static const arg_def_t inbitdeptharg
=
425 ARG_DEF(NULL
, "input-bit-depth", 1, "Bit depth of input");
428 static const struct arg_enum_list tune_content_enum
[] = {
429 { "default", AOM_CONTENT_DEFAULT
},
430 { "screen", AOM_CONTENT_SCREEN
},
434 static const arg_def_t tune_content
= ARG_DEF_ENUM(
435 NULL
, "tune-content", 1, "Tune content type", tune_content_enum
);
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
},
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
,
465 &frame_parallel_decoding
,
467 &frame_periodic_boost
,
473 #if CONFIG_EXT_PARTITION
475 #endif // CONFIG_EXT_PARTITION
476 #if CONFIG_AOM_HIGHBITDEPTH
479 #endif // CONFIG_AOM_HIGHBITDEPTH
481 static const int av1_arg_ctrl_map
[] = { AOME_SET_CPUUSED
,
482 AOME_SET_ENABLEAUTOALTREF
,
484 AOME_SET_STATIC_THRESHOLD
,
485 AV1E_SET_TILE_COLUMNS
,
487 AOME_SET_ARNR_MAXFRAMES
,
488 AOME_SET_ARNR_STRENGTH
,
492 AOME_SET_MAX_INTRA_BITRATE_PCT
,
493 AV1E_SET_MAX_INTER_BITRATE_PCT
,
494 AV1E_SET_GF_CBR_BOOST_PCT
,
496 AV1E_SET_FRAME_PARALLEL_DECODING
,
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
510 static const arg_def_t
*no_args
[] = { NULL
};
512 void usage_exit(void) {
514 const int num_encoder
= get_aom_encoder_count();
516 fprintf(stderr
, "Usage: %s <options> -o dst_filename src_filename \n",
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
);
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");
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
;
563 (img1
->d_w
+ img1
->x_chroma_shift
) >> img1
->x_chroma_shift
;
565 (img1
->d_h
+ img1
->y_chroma_shift
) >> img1
->y_chroma_shift
;
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
) {
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
)) {
584 yloc
[2] = *(plane1
+ (i
+ k
) * stride1
+ j
+ l
);
585 yloc
[3] = *(plane2
+ (i
+ k
) * stride2
+ j
+ l
);
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
) {
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
)) {
610 uloc
[2] = *(plane1
+ (i
+ k
) * stride1
+ j
+ l
);
611 uloc
[3] = *(plane2
+ (i
+ k
) * stride2
+ j
+ l
);
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
) {
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
)) {
636 vloc
[2] = *(plane1
+ (i
+ k
) * stride1
+ j
+ l
);
637 vloc
[3] = *(plane2
+ (i
+ k
) * stride2
+ j
+ l
);
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
;
655 (img1
->d_w
+ img1
->x_chroma_shift
) >> img1
->x_chroma_shift
;
657 (img1
->d_h
+ img1
->y_chroma_shift
) >> img1
->y_chroma_shift
;
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
) {
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
)) {
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
);
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
) {
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
)) {
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
);
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
) {
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
)) {
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
);
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
;
743 (img1
->d_h
+ img1
->y_chroma_shift
) >> img1
->y_chroma_shift
;
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
) {
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
],
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
],
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
],
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)
781 typedef int stereo_format_t
;
782 struct WebmOutputContext
{
787 /* Per-stream configuration */
788 struct stream_config
{
789 struct aom_codec_enc_cfg cfg
;
791 const char *stats_fn
;
792 #if CONFIG_FP_MB_STATS
793 const char *fpmb_stats_fn
;
795 stereo_format_t stereo_fmt
;
796 int arg_ctrls
[ARG_CTRL_CNT_MAX
][2];
799 #if CONFIG_AOM_HIGHBITDEPTH
800 // whether to use 16bit internal buffers
801 int use_16bit_internal
;
805 struct stream_state
{
807 struct stream_state
*next
;
808 struct stream_config config
;
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];
817 aom_codec_ctx_t encoder
;
818 unsigned int frames_out
;
822 #if CONFIG_FP_MB_STATS
823 stats_io_t fpmb_stats
;
825 struct aom_image
*img
;
826 aom_codec_ctx_t decoder
;
830 static void validate_positive_rational(const char *msg
,
831 struct aom_rational
*rat
) {
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
) {
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);
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
) {
860 if (arg_match(&arg
, &codecarg
, argi
)) {
861 global
->codec
= get_aom_encoder_by_name(arg
.val
);
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
))
896 else if (arg_match(&arg
, &verbosearg
, argi
))
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
))
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;
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
,
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
939 if (global
->codec
!= NULL
&& global
->codec
->name
!= NULL
)
940 global
->passes
= (strcmp(global
->codec
->name
, "av1") == 0 &&
941 global
->deadline
!= AOM_DL_REALTIME
)
949 if (global
->deadline
== AOM_DL_REALTIME
&& global
->passes
> 1) {
950 warn("Enforcing one-pass encoding in realtime mode\n");
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
966 input
->length
= ftello(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
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
;
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.");
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.");
1016 memcpy(stream
, prev
, sizeof(*stream
));
1018 prev
->next
= stream
;
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;
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
;
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
;
1062 static int parse_stream_params(struct AvxEncoderConfig
*global
,
1063 struct stream_state
*stream
, char **argv
) {
1064 char **argi
, **argj
;
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;
1074 // Handle codec specific options
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
;
1085 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
) {
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
) {
1094 } else if (!strcmp(*argj
, "--")) {
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
;
1107 } else if (arg_match(&arg
, &use_webm
, argi
)) {
1109 config
->write_webm
= 1;
1111 die("Error: --webm specified but webm is disabled.");
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
);
1130 } else if (arg_match(&arg
, &stereo_mode
, argi
)) {
1131 config
->stereo_fmt
= arg_parse_enum_or_int(&arg
);
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;
1204 for (i
= 0; ctrl_args
[i
]; i
++) {
1205 if (arg_match(&arg
, ctrl_args
[i
], argi
)) {
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
])
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
++;
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);
1236 return eos_mark_found
;
1239 #define FOREACH_STREAM(func) \
1241 struct stream_state *stream; \
1242 for (stream = streams; stream; stream = stream->next) { \
1247 static void validate_stream_config(const struct stream_state
*stream
,
1248 const struct AvxEncoderConfig
*global
) {
1249 const struct stream_state
*streami
;
1252 if (!stream
->config
.cfg
.g_w
|| !stream
->config
.cfg
.g_h
)
1254 "Stream %d: Specify stream dimensions with --width (-w) "
1255 " and --height (-h)",
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)",
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
);
1303 static void set_stream_dimensions(struct stream_state
*stream
, unsigned int w
,
1305 if (!stream
->config
.cfg
.g_w
) {
1306 if (!stream
->config
.cfg
.g_h
)
1307 stream
->config
.cfg
.g_w
= w
;
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
) {
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
) {
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");
1363 SHOW(g_input_bit_depth
);
1364 SHOW(g_timebase
.num
);
1365 SHOW(g_timebase
.den
);
1366 SHOW(g_error_resilient
);
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
);
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
);
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
);
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.");
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
);
1415 (void)pixel_aspect_ratio
;
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;
1430 if (stream
->config
.write_webm
) {
1431 write_webm_file_footer(&stream
->webm_ctx
);
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");
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
,
1458 fatal("Failed to open mb statistics store");
1460 if (!stats_open_mem(&stream
->fpmb_stats
, pass
))
1461 fatal("Failed to open mb statistics store");
1465 stream
->config
.cfg
.g_pass
= global
->passes
== 2
1466 ? pass
? AOM_RC_LAST_PASS
: AOM_RC_FIRST_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
);
1476 stream
->cx_time
= 0;
1478 stream
->frames_out
= 0;
1481 static void initialize_encoder(struct stream_state
*stream
,
1482 struct AvxEncoderConfig
*global
) {
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;
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");
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");
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
;
1537 (cfg
->g_timebase
.den
* (int64_t)(frames_in
- 1) * global
->framerate
.den
) /
1538 cfg
->g_timebase
.num
/ global
->framerate
.num
;
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
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
);
1555 aom_img_alloc(NULL
, AOM_IMG_FMT_I42016
, cfg
->g_w
, cfg
->g_h
, 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
);
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",
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
);
1588 aom_img_alloc(NULL
, AOM_IMG_FMT_I420
, cfg
->g_w
, cfg
->g_h
, 16);
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
);
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",
1608 aom_usec_timer_start(&timer
);
1609 aom_codec_encode(&stream
->encoder
, img
, frame_start
,
1610 (unsigned long)(next_frame_start
- frame_start
), 0,
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",
1618 static void update_quantizer_histogram(struct stream_state
*stream
) {
1619 if (stream
->config
.cfg
.g_pass
!= AOM_RC_FIRST_PASS
) {
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
;
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
++;
1645 fprintf(stderr
, " %6luF", (unsigned long)pkt
->data
.frame
.sz
);
1647 update_rate_histogram(stream
->rate_hist
, cfg
, pkt
);
1649 if (stream
->config
.write_webm
) {
1650 write_webm_block(&stream
->webm_ctx
, cfg
, pkt
);
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
);
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
,
1673 stream
->nbytes
+= pkt
->data
.raw
.sz
;
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;
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
;
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
;
1703 case AOM_CODEC_PSNR_PKT
:
1705 if (global
->show_psnr
) {
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
++) {
1712 fprintf(stderr
, "%.3f ", pkt
->data
.psnr
.psnr
[i
]);
1713 stream
->psnr_totals
[i
] += pkt
->data
.psnr
.psnr
[i
];
1715 stream
->psnr_count
++;
1724 static void show_psnr(struct stream_state
*stream
, double peak
) {
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
;
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
);
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
;
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
);
1801 find_mismatch(&enc_img
, &dec_img
, y
, u
, v
);
1804 find_mismatch(&enc_img
, &dec_img
, y
, u
, v
);
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
) {
1828 etl
-= hours
* 3600;
1833 fprintf(stderr
, "[%3s %2" PRId64
":%02" PRId64
":%02" PRId64
"] ", label
,
1836 fprintf(stderr
, "[%3s unknown] ", label
);
1840 int main(int argc
, const char **argv_
) {
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;
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;
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
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
1890 struct stream_state
*stream
= NULL
;
1893 stream
= new_stream(&global
, stream
);
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
) {
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
;
1936 /* Update stream configurations from the input file's parameters */
1937 if (!input
.width
|| !input
.height
)
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
) {
1949 if (stream
->config
.cfg
.g_input_bit_depth
)
1950 input
.bit_depth
= stream
->config
.cfg
.g_input_bit_depth
;
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
;
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)
1969 if (!stream
->config
.stats_fn
)
1971 "Stream %d: Must specify --fpf when --pass=%d"
1972 " and --passes=2\n",
1973 stream
->index
, global
.pass
);
1978 if (stream
->config
.write_webm
) {
1979 stream
->config
.write_webm
= 0;
1981 "aomenc was compiled without WebM container support."
1982 "Producing IVF output");
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
2006 memset(&raw
, 0, sizeof(raw
));
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
));
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.
2026 if (stream
->config
.use_16bit_internal
) {
2027 use_16bit_internal
= 1;
2029 if (stream
->config
.cfg
.g_profile
== 0) {
2032 input_shift
= (int)stream
->config
.cfg
.g_bit_depth
-
2033 stream
->config
.cfg
.g_input_bit_depth
;
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
++;
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
);
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
);
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
;
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
);
2093 if (stream
->config
.use_16bit_internal
)
2094 encode_frame(stream
, &global
,
2095 frame_avail
? frame_to_encode
: NULL
, frames_in
);
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
,
2106 aom_usec_timer_start(&timer
);
2107 FOREACH_STREAM(encode_frame(stream
, &global
, frame_avail
? &raw
: NULL
,
2110 aom_usec_timer_mark(&timer
);
2111 cx_time
+= aom_usec_timer_elapsed(&timer
);
2113 FOREACH_STREAM(update_quantizer_histogram(stream
));
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
) {
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
);
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
;
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
));
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,
2164 ? (int64_t)stream
->nbytes
* 8 * (int64_t)global
.framerate
.num
/
2165 global
.framerate
.den
/ seen_frames
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
) {
2175 show_psnr(stream
, (1 << stream
->config
.cfg
.g_input_bit_depth
) - 1));
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));
2200 if (global
.pass
) break;
2203 if (global
.show_q_hist_buckets
)
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))
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
);
2223 fprintf(f
, "No mismatch detected in recon buffers\n");
2229 #if CONFIG_AOM_HIGHBITDEPTH
2230 if (allocated_raw_shift
) aom_img_free(&raw_shift
);
2235 return res
? EXIT_FAILURE
: EXIT_SUCCESS
;