WebM Codec SDK: vpxenc

WebM Codec SDK

vpxenc
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "./vpxenc.h"
12 #include "./vpx_config.h"
13
14 #include <assert.h>
15 #include <limits.h>
16 #include <math.h>
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #if CONFIG_LIBYUV
23 #include "third_party/libyuv/include/libyuv/scale.h"
24 #endif
25
26 #include "vpx/vpx_encoder.h"
27 #if CONFIG_DECODERS
28 #include "vpx/vpx_decoder.h"
29 #endif
30
31 #include "./args.h"
32 #include "./ivfenc.h"
33 #include "./tools_common.h"
34
35 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
36 #include "vpx/vp8cx.h"
37 #endif
38 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
39 #include "vpx/vp8dx.h"
40 #endif
41
42 #include "vpx/vpx_integer.h"
43 #include "vpx_ports/mem_ops.h"
44 #include "vpx_ports/vpx_timer.h"
45 #include "./rate_hist.h"
46 #include "./vpxstats.h"
47 #include "./warnings.h"
48 #if CONFIG_WEBM_IO
49 #include "./webmenc.h"
50 #endif
51 #include "./y4minput.h"
52
53 /* Swallow warnings about unused results of fread/fwrite */
54 static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
55 return fread(ptr, size, nmemb, stream);
56 }
57 #define fread wrap_fread
58
59 static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
60 FILE *stream) {
61 return fwrite(ptr, size, nmemb, stream);
62 }
63 #define fwrite wrap_fwrite
64
65 static const char *exec_name;
66
67 static void warn_or_exit_on_errorv(vpx_codec_ctx_t *ctx, int fatal,
68 const char *s, va_list ap) {
69 if (ctx->err) {
70 const char *detail = vpx_codec_error_detail(ctx);
71
72 vfprintf(stderr, s, ap);
73 fprintf(stderr, ": %s\n", vpx_codec_error(ctx));
74
75 if (detail) fprintf(stderr, " %s\n", detail);
76
77 if (fatal) exit(EXIT_FAILURE);
78 }
79 }
80
81 static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s, ...) {
82 va_list ap;
83
84 va_start(ap, s);
85 warn_or_exit_on_errorv(ctx, 1, s, ap);
86 va_end(ap);
87 }
88
89 static void warn_or_exit_on_error(vpx_codec_ctx_t *ctx, int fatal,
90 const char *s, ...) {
91 va_list ap;
92
93 va_start(ap, s);
94 warn_or_exit_on_errorv(ctx, fatal, s, ap);
95 va_end(ap);
96 }
97
98 static int read_frame(struct VpxInputContext *input_ctx, vpx_image_t *img) {
99 FILE *f = input_ctx->file;
100 y4m_input *y4m = &input_ctx->y4m;
101 int shortread = 0;
102
103 if (input_ctx->file_type == FILE_TYPE_Y4M) {
104 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
105 } else {
106 shortread = read_yuv_frame(input_ctx, img);
107 }
108
109 return !shortread;
110 }
111
112 static int file_is_y4m(const char detect[4]) {
113 if (memcmp(detect, "YUV4", 4) == 0) {
114 return 1;
115 }
116 return 0;
117 }
118
119 static int fourcc_is_ivf(const char detect[4]) {
120 if (memcmp(detect, "DKIF", 4) == 0) {
121 return 1;
122 }
123 return 0;
124 }
125
126 static const arg_def_t debugmode =
127 ARG_DEF("D", "debug", 0, "Debug mode (makes output deterministic)");
128 static const arg_def_t outputfile =
129 ARG_DEF("o", "output", 1, "Output filename");
130 static const arg_def_t use_yv12 =
131 ARG_DEF(NULL, "yv12", 0, "Input file is YV12 ");
132 static const arg_def_t use_i420 =
133 ARG_DEF(NULL, "i420", 0, "Input file is I420 (default)");
134 static const arg_def_t use_i422 =
135 ARG_DEF(NULL, "i422", 0, "Input file is I422");
136 static const arg_def_t use_i444 =
137 ARG_DEF(NULL, "i444", 0, "Input file is I444");
138 static const arg_def_t use_i440 =
139 ARG_DEF(NULL, "i440", 0, "Input file is I440");
140 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
141 static const arg_def_t passes =
142 ARG_DEF("p", "passes", 1, "Number of passes (1/2)");
143 static const arg_def_t pass_arg =
144 ARG_DEF(NULL, "pass", 1, "Pass to execute (1/2)");
145 static const arg_def_t fpf_name =
146 ARG_DEF(NULL, "fpf", 1, "First pass statistics file name");
147 #if CONFIG_FP_MB_STATS
148 static const arg_def_t fpmbf_name =
149 ARG_DEF(NULL, "fpmbf", 1, "First pass block statistics file name");
150 #endif
151 static const arg_def_t limit =
152 ARG_DEF(NULL, "limit", 1, "Stop encoding after n input frames");
153 static const arg_def_t skip =
154 ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
155 static const arg_def_t deadline =
156 ARG_DEF("d", "deadline", 1, "Deadline per frame (usec)");
157 static const arg_def_t best_dl =
158 ARG_DEF(NULL, "best", 0, "Use Best Quality Deadline");
159 static const arg_def_t good_dl =
160 ARG_DEF(NULL, "good", 0, "Use Good Quality Deadline");
161 static const arg_def_t rt_dl =
162 ARG_DEF(NULL, "rt", 0, "Use Realtime Quality Deadline");
163 static const arg_def_t quietarg =
164 ARG_DEF("q", "quiet", 0, "Do not print encode progress");
165 static const arg_def_t verbosearg =
166 ARG_DEF("v", "verbose", 0, "Show encoder parameters");
167 static const arg_def_t psnrarg =
168 ARG_DEF(NULL, "psnr", 0, "Show PSNR in status line");
169
170 static const struct arg_enum_list test_decode_enum[] = {
171 { "off", TEST_DECODE_OFF },
172 { "fatal", TEST_DECODE_FATAL },
173 { "warn", TEST_DECODE_WARN },
174 { NULL, 0 }
175 };
176 static const arg_def_t recontest = ARG_DEF_ENUM(
177 NULL, "test-decode", 1, "Test encode/decode mismatch", test_decode_enum);
178 static const arg_def_t framerate =
179 ARG_DEF(NULL, "fps", 1, "Stream frame rate (rate/scale)");
180 static const arg_def_t use_webm =
181 ARG_DEF(NULL, "webm", 0, "Output WebM (default when WebM IO is enabled)");
182 static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, "Output IVF");
183 static const arg_def_t out_part =
184 ARG_DEF("P", "output-partitions", 0,
185 "Makes encoder output partitions. Requires IVF output!");
186 static const arg_def_t q_hist_n =
187 ARG_DEF(NULL, "q-hist", 1, "Show quantizer histogram (n-buckets)");
188 static const arg_def_t rate_hist_n =
189 ARG_DEF(NULL, "rate-hist", 1, "Show rate histogram (n-buckets)");
190 static const arg_def_t disable_warnings =
191 ARG_DEF(NULL, "disable-warnings", 0,
192 "Disable warnings about potentially incorrect encode settings.");
193 static const arg_def_t disable_warning_prompt =
194 ARG_DEF("y", "disable-warning-prompt", 0,
195 "Display warnings, but do not prompt user to continue.");
196
197 #if CONFIG_VP9_HIGHBITDEPTH
198 static const arg_def_t test16bitinternalarg = ARG_DEF(
199 NULL, "test-16bit-internal", 0, "Force use of 16 bit internal buffer");
200 #endif
201
202 static const arg_def_t *main_args[] = { &debugmode,
203 &outputfile,
204 &codecarg,
205 &passes,
206 &pass_arg,
207 &fpf_name,
208 &limit,
209 &skip,
210 &deadline,
211 &best_dl,
212 &good_dl,
213 &rt_dl,
214 &quietarg,
215 &verbosearg,
216 &psnrarg,
217 &use_webm,
218 &use_ivf,
219 &out_part,
220 &q_hist_n,
221 &rate_hist_n,
222 &disable_warnings,
223 &disable_warning_prompt,
224 &recontest,
225 NULL };
226
227 static const arg_def_t usage =
228 ARG_DEF("u", "usage", 1, "Usage profile number to use");
229 static const arg_def_t threads =
230 ARG_DEF("t", "threads", 1, "Max number of threads to use");
231 static const arg_def_t profile =
232 ARG_DEF(NULL, "profile", 1, "Bitstream profile number to use");
233 static const arg_def_t width = ARG_DEF("w", "width", 1, "Frame width");
234 static const arg_def_t height = ARG_DEF("h", "height", 1, "Frame height");
235 #if CONFIG_WEBM_IO
236 static const struct arg_enum_list stereo_mode_enum[] = {
237 { "mono", STEREO_FORMAT_MONO },
238 { "left-right", STEREO_FORMAT_LEFT_RIGHT },
239 { "bottom-top", STEREO_FORMAT_BOTTOM_TOP },
240 { "top-bottom", STEREO_FORMAT_TOP_BOTTOM },
241 { "right-left", STEREO_FORMAT_RIGHT_LEFT },
242 { NULL, 0 }
243 };
244 static const arg_def_t stereo_mode = ARG_DEF_ENUM(
245 NULL, "stereo-mode", 1, "Stereo 3D video format", stereo_mode_enum);
246 #endif
247 static const arg_def_t timebase = ARG_DEF(
248 NULL, "timebase", 1, "Output timestamp precision (fractional seconds)");
249 static const arg_def_t error_resilient =
250 ARG_DEF(NULL, "error-resilient", 1, "Enable error resiliency features");
251 static const arg_def_t lag_in_frames =
252 ARG_DEF(NULL, "lag-in-frames", 1, "Max number of frames to lag");
253
254 static const arg_def_t *global_args[] = { &use_yv12,
255 &use_i420,
256 &use_i422,
257 &use_i444,
258 &use_i440,
259 &usage,
260 &threads,
261 &profile,
262 &width,
263 &height,
264 #if CONFIG_WEBM_IO
265 &stereo_mode,
266 #endif
267 &timebase,
268 &framerate,
269 &error_resilient,
270 #if CONFIG_VP9_HIGHBITDEPTH
271 &test16bitinternalarg,
272 #endif
273 &lag_in_frames,
274 NULL };
275
276 static const arg_def_t dropframe_thresh =
277 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
278 static const arg_def_t resize_allowed =
279 ARG_DEF(NULL, "resize-allowed", 1, "Spatial resampling enabled (bool)");
280 static const arg_def_t resize_width =
281 ARG_DEF(NULL, "resize-width", 1, "Width of encoded frame");
282 static const arg_def_t resize_height =
283 ARG_DEF(NULL, "resize-height", 1, "Height of encoded frame");
284 static const arg_def_t resize_up_thresh =
285 ARG_DEF(NULL, "resize-up", 1, "Upscale threshold (buf %)");
286 static const arg_def_t resize_down_thresh =
287 ARG_DEF(NULL, "resize-down", 1, "Downscale threshold (buf %)");
288 static const struct arg_enum_list end_usage_enum[] = { { "vbr", VPX_VBR },
289 { "cbr", VPX_CBR },
290 { "cq", VPX_CQ },
291 { "q", VPX_Q },
292 { NULL, 0 } };
293 static const arg_def_t end_usage =
294 ARG_DEF_ENUM(NULL, "end-usage", 1, "Rate control mode", end_usage_enum);
295 static const arg_def_t target_bitrate =
296 ARG_DEF(NULL, "target-bitrate", 1, "Bitrate (kbps)");
297 static const arg_def_t min_quantizer =
298 ARG_DEF(NULL, "min-q", 1, "Minimum (best) quantizer");
299 static const arg_def_t max_quantizer =
300 ARG_DEF(NULL, "max-q", 1, "Maximum (worst) quantizer");
301 static const arg_def_t undershoot_pct =
302 ARG_DEF(NULL, "undershoot-pct", 1, "Datarate undershoot (min) target (%)");
303 static const arg_def_t overshoot_pct =
304 ARG_DEF(NULL, "overshoot-pct", 1, "Datarate overshoot (max) target (%)");
305 static const arg_def_t buf_sz =
306 ARG_DEF(NULL, "buf-sz", 1, "Client buffer size (ms)");
307 static const arg_def_t buf_initial_sz =
308 ARG_DEF(NULL, "buf-initial-sz", 1, "Client initial buffer size (ms)");
309 static const arg_def_t buf_optimal_sz =
310 ARG_DEF(NULL, "buf-optimal-sz", 1, "Client optimal buffer size (ms)");
311 static const arg_def_t *rc_args[] = {
312 &dropframe_thresh, &resize_allowed, &resize_width, &resize_height,
313 &resize_up_thresh, &resize_down_thresh, &end_usage, &target_bitrate,
314 &min_quantizer, &max_quantizer, &undershoot_pct, &overshoot_pct,
315 &buf_sz, &buf_initial_sz, &buf_optimal_sz, NULL
316 };
317
318 static const arg_def_t bias_pct =
319 ARG_DEF(NULL, "bias-pct", 1, "CBR/VBR bias (0=CBR, 100=VBR)");
320 static const arg_def_t minsection_pct =
321 ARG_DEF(NULL, "minsection-pct", 1, "GOP min bitrate (% of target)");
322 static const arg_def_t maxsection_pct =
323 ARG_DEF(NULL, "maxsection-pct", 1, "GOP max bitrate (% of target)");
324 static const arg_def_t *rc_twopass_args[] = { &bias_pct, &minsection_pct,
325 &maxsection_pct, NULL };
326
327 static const arg_def_t kf_min_dist =
328 ARG_DEF(NULL, "kf-min-dist", 1, "Minimum keyframe interval (frames)");
329 static const arg_def_t kf_max_dist =
330 ARG_DEF(NULL, "kf-max-dist", 1, "Maximum keyframe interval (frames)");
331 static const arg_def_t kf_disabled =
332 ARG_DEF(NULL, "disable-kf", 0, "Disable keyframe placement");
333 static const arg_def_t *kf_args[] = { &kf_min_dist, &kf_max_dist, &kf_disabled,
334 NULL };
335
336 static const arg_def_t noise_sens =
337 ARG_DEF(NULL, "noise-sensitivity", 1, "Noise sensitivity (frames to blur)");
338 static const arg_def_t sharpness =
339 ARG_DEF(NULL, "sharpness", 1, "Loop filter sharpness (0..7)");
340 static const arg_def_t static_thresh =
341 ARG_DEF(NULL, "static-thresh", 1, "Motion detection threshold");
342 static const arg_def_t auto_altref =
343 ARG_DEF(NULL, "auto-alt-ref", 1, "Enable automatic alt reference frames");
344 static const arg_def_t arnr_maxframes =
345 ARG_DEF(NULL, "arnr-maxframes", 1, "AltRef max frames (0..15)");
346 static const arg_def_t arnr_strength =
347 ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
348 static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1, "AltRef type");
349 static const struct arg_enum_list tuning_enum[] = {
350 { "psnr", VP8_TUNE_PSNR }, { "ssim", VP8_TUNE_SSIM }, { NULL, 0 }
351 };
352 static const arg_def_t tune_ssim =
353 ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
354 static const arg_def_t cq_level =
355 ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
356 static const arg_def_t max_intra_rate_pct =
357 ARG_DEF(NULL, "max-intra-rate", 1, "Max I-frame bitrate (pct)");
358 static const arg_def_t gf_cbr_boost_pct = ARG_DEF(
359 NULL, "gf-cbr-boost", 1, "Boost for Golden Frame in CBR mode (pct)");
360
361 #if CONFIG_VP8_ENCODER
362 static const arg_def_t cpu_used_vp8 =
363 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (-16..16)");
364 static const arg_def_t token_parts =
365 ARG_DEF(NULL, "token-parts", 1, "Number of token partitions to use, log2");
366 static const arg_def_t screen_content_mode =
367 ARG_DEF(NULL, "screen-content-mode", 1, "Screen content mode");
368 static const arg_def_t *vp8_args[] = { &cpu_used_vp8,
369 &auto_altref,
370 &noise_sens,
371 &sharpness,
372 &static_thresh,
373 &token_parts,
374 &arnr_maxframes,
375 &arnr_strength,
376 &arnr_type,
377 &tune_ssim,
378 &cq_level,
379 &max_intra_rate_pct,
380 &gf_cbr_boost_pct,
381 &screen_content_mode,
382 NULL };
383 static const int vp8_arg_ctrl_map[] = { VP8E_SET_CPUUSED,
384 VP8E_SET_ENABLEAUTOALTREF,
385 VP8E_SET_NOISE_SENSITIVITY,
386 VP8E_SET_SHARPNESS,
387 VP8E_SET_STATIC_THRESHOLD,
388 VP8E_SET_TOKEN_PARTITIONS,
389 VP8E_SET_ARNR_MAXFRAMES,
390 VP8E_SET_ARNR_STRENGTH,
391 VP8E_SET_ARNR_TYPE,
392 VP8E_SET_TUNING,
393 VP8E_SET_CQ_LEVEL,
394 VP8E_SET_MAX_INTRA_BITRATE_PCT,
395 VP8E_SET_GF_CBR_BOOST_PCT,
396 VP8E_SET_SCREEN_CONTENT_MODE,
397 0 };
398 #endif
399
400 #if CONFIG_VP9_ENCODER
401 static const arg_def_t cpu_used_vp9 =
402 ARG_DEF(NULL, "cpu-used", 1, "CPU Used (-8..8)");
403 static const arg_def_t tile_cols =
404 ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2");
405 static const arg_def_t tile_rows =
406 ARG_DEF(NULL, "tile-rows", 1,
407 "Number of tile rows to use, log2 (set to 0 while threads > 1)");
408 static const arg_def_t lossless =
409 ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
410 static const arg_def_t frame_parallel_decoding = ARG_DEF(
411 NULL, "frame-parallel", 1, "Enable frame parallel decodability features");
412 static const arg_def_t aq_mode = ARG_DEF(
413 NULL, "aq-mode", 1,
414 "Adaptive quantization mode (0: off (default), 1: variance 2: complexity, "
415 "3: cyclic refresh, 4: equator360)");
416 static const arg_def_t alt_ref_aq = ARG_DEF(NULL, "alt-ref-aq", 1,
417 "Special adaptive quantization for "
418 "the alternate reference frames.");
419 static const arg_def_t frame_periodic_boost =
420 ARG_DEF(NULL, "frame-boost", 1,
421 "Enable frame periodic boost (0: off (default), 1: on)");
422 static const arg_def_t max_inter_rate_pct =
423 ARG_DEF(NULL, "max-inter-rate", 1, "Max P-frame bitrate (pct)");
424 static const arg_def_t min_gf_interval = ARG_DEF(
425 NULL, "min-gf-interval", 1,
426 "min gf/arf frame interval (default 0, indicating in-built behavior)");
427 static const arg_def_t max_gf_interval = ARG_DEF(
428 NULL, "max-gf-interval", 1,
429 "max gf/arf frame interval (default 0, indicating in-built behavior)");
430
431 static const struct arg_enum_list color_space_enum[] = {
432 { "unknown", VPX_CS_UNKNOWN },
433 { "bt601", VPX_CS_BT_601 },
434 { "bt709", VPX_CS_BT_709 },
435 { "smpte170", VPX_CS_SMPTE_170 },
436 { "smpte240", VPX_CS_SMPTE_240 },
437 { "bt2020", VPX_CS_BT_2020 },
438 { "reserved", VPX_CS_RESERVED },
439 { "sRGB", VPX_CS_SRGB },
440 { NULL, 0 }
441 };
442
443 static const arg_def_t input_color_space =
444 ARG_DEF_ENUM(NULL, "color-space", 1, "The color space of input content:",
445 color_space_enum);
446
447 #if CONFIG_VP9_HIGHBITDEPTH
448 static const struct arg_enum_list bitdepth_enum[] = {
449 { "8", VPX_BITS_8 }, { "10", VPX_BITS_10 }, { "12", VPX_BITS_12 }, { NULL, 0 }
450 };
451
452 static const arg_def_t bitdeptharg = ARG_DEF_ENUM(
453 "b", "bit-depth", 1,
454 "Bit depth for codec (8 for version <=1, 10 or 12 for version 2)",
455 bitdepth_enum);
456 static const arg_def_t inbitdeptharg =
457 ARG_DEF(NULL, "input-bit-depth", 1, "Bit depth of input");
458 #endif
459
460 static const struct arg_enum_list tune_content_enum[] = {
461 { "default", VP9E_CONTENT_DEFAULT },
462 { "screen", VP9E_CONTENT_SCREEN },
463 { NULL, 0 }
464 };
465
466 static const arg_def_t tune_content = ARG_DEF_ENUM(
467 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
468
469 static const arg_def_t target_level = ARG_DEF(
470 NULL, "target-level", 1,
471 "Target level (255: off (default); 0: only keep level stats; 10: level 1.0;"
472 " 11: level 1.1; ... 62: level 6.2)");
473 #endif
474
475 #if CONFIG_VP9_ENCODER
476 static const arg_def_t *vp9_args[] = { &cpu_used_vp9,
477 &auto_altref,
478 &sharpness,
479 &static_thresh,
480 &tile_cols,
481 &tile_rows,
482 &arnr_maxframes,
483 &arnr_strength,
484 &arnr_type,
485 &tune_ssim,
486 &cq_level,
487 &max_intra_rate_pct,
488 &max_inter_rate_pct,
489 &gf_cbr_boost_pct,
490 &lossless,
491 &frame_parallel_decoding,
492 &aq_mode,
493 &alt_ref_aq,
494 &frame_periodic_boost,
495 &noise_sens,
496 &tune_content,
497 &input_color_space,
498 &min_gf_interval,
499 &max_gf_interval,
500 &target_level,
501 #if CONFIG_VP9_HIGHBITDEPTH
502 &bitdeptharg,
503 &inbitdeptharg,
504 #endif // CONFIG_VP9_HIGHBITDEPTH
505 NULL };
506 static const int vp9_arg_ctrl_map[] = { VP8E_SET_CPUUSED,
507 VP8E_SET_ENABLEAUTOALTREF,
508 VP8E_SET_SHARPNESS,
509 VP8E_SET_STATIC_THRESHOLD,
510 VP9E_SET_TILE_COLUMNS,
511 VP9E_SET_TILE_ROWS,
512 VP8E_SET_ARNR_MAXFRAMES,
513 VP8E_SET_ARNR_STRENGTH,
514 VP8E_SET_ARNR_TYPE,
515 VP8E_SET_TUNING,
516 VP8E_SET_CQ_LEVEL,
517 VP8E_SET_MAX_INTRA_BITRATE_PCT,
518 VP9E_SET_MAX_INTER_BITRATE_PCT,
519 VP9E_SET_GF_CBR_BOOST_PCT,
520 VP9E_SET_LOSSLESS,
521 VP9E_SET_FRAME_PARALLEL_DECODING,
522 VP9E_SET_AQ_MODE,
523 VP9E_SET_ALT_REF_AQ,
524 VP9E_SET_FRAME_PERIODIC_BOOST,
525 VP9E_SET_NOISE_SENSITIVITY,
526 VP9E_SET_TUNE_CONTENT,
527 VP9E_SET_COLOR_SPACE,
528 VP9E_SET_MIN_GF_INTERVAL,
529 VP9E_SET_MAX_GF_INTERVAL,
530 VP9E_SET_TARGET_LEVEL,
531 0 };
532 #endif
533
534 static const arg_def_t *no_args[] = { NULL };
535
536 void usage_exit(void) {
537 int i;
538 const int num_encoder = get_vpx_encoder_count();
539
540 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
541 exec_name);
542
543 fprintf(stderr, "\nOptions:\n");
544 arg_show_usage(stderr, main_args);
545 fprintf(stderr, "\nEncoder Global Options:\n");
546 arg_show_usage(stderr, global_args);
547 fprintf(stderr, "\nRate Control Options:\n");
548 arg_show_usage(stderr, rc_args);
549 fprintf(stderr, "\nTwopass Rate Control Options:\n");
550 arg_show_usage(stderr, rc_twopass_args);
551 fprintf(stderr, "\nKeyframe Placement Options:\n");
552 arg_show_usage(stderr, kf_args);
553 #if CONFIG_VP8_ENCODER
554 fprintf(stderr, "\nVP8 Specific Options:\n");
555 arg_show_usage(stderr, vp8_args);
556 #endif
557 #if CONFIG_VP9_ENCODER
558 fprintf(stderr, "\nVP9 Specific Options:\n");
559 arg_show_usage(stderr, vp9_args);
560 #endif
561 fprintf(stderr,
562 "\nStream timebase (--timebase):\n"
563 " The desired precision of timestamps in the output, expressed\n"
564 " in fractional seconds. Default is 1/1000.\n");
565 fprintf(stderr, "\nIncluded encoders:\n\n");
566
567 for (i = 0; i < num_encoder; ++i) {
568 const VpxInterface *const encoder = get_vpx_encoder_by_index(i);
569 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
570 fprintf(stderr, " %-6s - %s %s\n", encoder->name,
571 vpx_codec_iface_name(encoder->codec_interface()), defstr);
572 }
573 fprintf(stderr, "\n ");
574 fprintf(stderr, "Use --codec to switch to a non-default encoder.\n\n");
575
576 exit(EXIT_FAILURE);
577 }
578
579 #define mmin(a, b) ((a) < (b) ? (a) : (b))
580
581 #if CONFIG_VP9_HIGHBITDEPTH
582 static void find_mismatch_high(const vpx_image_t *const img1,
583 const vpx_image_t *const img2, int yloc[4],
584 int uloc[4], int vloc[4]) {
585 uint16_t *plane1, *plane2;
586 uint32_t stride1, stride2;
587 const uint32_t bsize = 64;
588 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
589 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
590 const uint32_t c_w =
591 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
592 const uint32_t c_h =
593 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
594 int match = 1;
595 uint32_t i, j;
596 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
597 plane1 = (uint16_t *)img1->planes[VPX_PLANE_Y];
598 plane2 = (uint16_t *)img2->planes[VPX_PLANE_Y];
599 stride1 = img1->stride[VPX_PLANE_Y] / 2;
600 stride2 = img2->stride[VPX_PLANE_Y] / 2;
601 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
602 for (j = 0; match && j < img1->d_w; j += bsize) {
603 int k, l;
604 const int si = mmin(i + bsize, img1->d_h) - i;
605 const int sj = mmin(j + bsize, img1->d_w) - j;
606 for (k = 0; match && k < si; ++k) {
607 for (l = 0; match && l < sj; ++l) {
608 if (*(plane1 + (i + k) * stride1 + j + l) !=
609 *(plane2 + (i + k) * stride2 + j + l)) {
610 yloc[0] = i + k;
611 yloc[1] = j + l;
612 yloc[2] = *(plane1 + (i + k) * stride1 + j + l);
613 yloc[3] = *(plane2 + (i + k) * stride2 + j + l);
614 match = 0;
615 break;
616 }
617 }
618 }
619 }
620 }
621
622 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
623 plane1 = (uint16_t *)img1->planes[VPX_PLANE_U];
624 plane2 = (uint16_t *)img2->planes[VPX_PLANE_U];
625 stride1 = img1->stride[VPX_PLANE_U] / 2;
626 stride2 = img2->stride[VPX_PLANE_U] / 2;
627 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
628 for (j = 0; match && j < c_w; j += bsizex) {
629 int k, l;
630 const int si = mmin(i + bsizey, c_h - i);
631 const int sj = mmin(j + bsizex, c_w - j);
632 for (k = 0; match && k < si; ++k) {
633 for (l = 0; match && l < sj; ++l) {
634 if (*(plane1 + (i + k) * stride1 + j + l) !=
635 *(plane2 + (i + k) * stride2 + j + l)) {
636 uloc[0] = i + k;
637 uloc[1] = j + l;
638 uloc[2] = *(plane1 + (i + k) * stride1 + j + l);
639 uloc[3] = *(plane2 + (i + k) * stride2 + j + l);
640 match = 0;
641 break;
642 }
643 }
644 }
645 }
646 }
647
648 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
649 plane1 = (uint16_t *)img1->planes[VPX_PLANE_V];
650 plane2 = (uint16_t *)img2->planes[VPX_PLANE_V];
651 stride1 = img1->stride[VPX_PLANE_V] / 2;
652 stride2 = img2->stride[VPX_PLANE_V] / 2;
653 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
654 for (j = 0; match && j < c_w; j += bsizex) {
655 int k, l;
656 const int si = mmin(i + bsizey, c_h - i);
657 const int sj = mmin(j + bsizex, c_w - j);
658 for (k = 0; match && k < si; ++k) {
659 for (l = 0; match && l < sj; ++l) {
660 if (*(plane1 + (i + k) * stride1 + j + l) !=
661 *(plane2 + (i + k) * stride2 + j + l)) {
662 vloc[0] = i + k;
663 vloc[1] = j + l;
664 vloc[2] = *(plane1 + (i + k) * stride1 + j + l);
665 vloc[3] = *(plane2 + (i + k) * stride2 + j + l);
666 match = 0;
667 break;
668 }
669 }
670 }
671 }
672 }
673 }
674 #endif
675
676 static void find_mismatch(const vpx_image_t *const img1,
677 const vpx_image_t *const img2, int yloc[4],
678 int uloc[4], int vloc[4]) {
679 const uint32_t bsize = 64;
680 const uint32_t bsizey = bsize >> img1->y_chroma_shift;
681 const uint32_t bsizex = bsize >> img1->x_chroma_shift;
682 const uint32_t c_w =
683 (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
684 const uint32_t c_h =
685 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
686 int match = 1;
687 uint32_t i, j;
688 yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
689 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
690 for (j = 0; match && j < img1->d_w; j += bsize) {
691 int k, l;
692 const int si = mmin(i + bsize, img1->d_h) - i;
693 const int sj = mmin(j + bsize, img1->d_w) - j;
694 for (k = 0; match && k < si; ++k) {
695 for (l = 0; match && l < sj; ++l) {
696 if (*(img1->planes[VPX_PLANE_Y] +
697 (i + k) * img1->stride[VPX_PLANE_Y] + j + l) !=
698 *(img2->planes[VPX_PLANE_Y] +
699 (i + k) * img2->stride[VPX_PLANE_Y] + j + l)) {
700 yloc[0] = i + k;
701 yloc[1] = j + l;
702 yloc[2] = *(img1->planes[VPX_PLANE_Y] +
703 (i + k) * img1->stride[VPX_PLANE_Y] + j + l);
704 yloc[3] = *(img2->planes[VPX_PLANE_Y] +
705 (i + k) * img2->stride[VPX_PLANE_Y] + j + l);
706 match = 0;
707 break;
708 }
709 }
710 }
711 }
712 }
713
714 uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
715 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
716 for (j = 0; match && j < c_w; j += bsizex) {
717 int k, l;
718 const int si = mmin(i + bsizey, c_h - i);
719 const int sj = mmin(j + bsizex, c_w - j);
720 for (k = 0; match && k < si; ++k) {
721 for (l = 0; match && l < sj; ++l) {
722 if (*(img1->planes[VPX_PLANE_U] +
723 (i + k) * img1->stride[VPX_PLANE_U] + j + l) !=
724 *(img2->planes[VPX_PLANE_U] +
725 (i + k) * img2->stride[VPX_PLANE_U] + j + l)) {
726 uloc[0] = i + k;
727 uloc[1] = j + l;
728 uloc[2] = *(img1->planes[VPX_PLANE_U] +
729 (i + k) * img1->stride[VPX_PLANE_U] + j + l);
730 uloc[3] = *(img2->planes[VPX_PLANE_U] +
731 (i + k) * img2->stride[VPX_PLANE_U] + j + l);
732 match = 0;
733 break;
734 }
735 }
736 }
737 }
738 }
739 vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
740 for (i = 0, match = 1; match && i < c_h; i += bsizey) {
741 for (j = 0; match && j < c_w; j += bsizex) {
742 int k, l;
743 const int si = mmin(i + bsizey, c_h - i);
744 const int sj = mmin(j + bsizex, c_w - j);
745 for (k = 0; match && k < si; ++k) {
746 for (l = 0; match && l < sj; ++l) {
747 if (*(img1->planes[VPX_PLANE_V] +
748 (i + k) * img1->stride[VPX_PLANE_V] + j + l) !=
749 *(img2->planes[VPX_PLANE_V] +
750 (i + k) * img2->stride[VPX_PLANE_V] + j + l)) {
751 vloc[0] = i + k;
752 vloc[1] = j + l;
753 vloc[2] = *(img1->planes[VPX_PLANE_V] +
754 (i + k) * img1->stride[VPX_PLANE_V] + j + l);
755 vloc[3] = *(img2->planes[VPX_PLANE_V] +
756 (i + k) * img2->stride[VPX_PLANE_V] + j + l);
757 match = 0;
758 break;
759 }
760 }
761 }
762 }
763 }
764 }
765
766 static int compare_img(const vpx_image_t *const img1,
767 const vpx_image_t *const img2) {
768 uint32_t l_w = img1->d_w;
769 uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
770 const uint32_t c_h =
771 (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
772 uint32_t i;
773 int match = 1;
774
775 match &= (img1->fmt == img2->fmt);
776 match &= (img1->d_w == img2->d_w);
777 match &= (img1->d_h == img2->d_h);
778 #if CONFIG_VP9_HIGHBITDEPTH
779 if (img1->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
780 l_w *= 2;
781 c_w *= 2;
782 }
783 #endif
784
785 for (i = 0; i < img1->d_h; ++i)
786 match &= (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y],
787 img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y],
788 l_w) == 0);
789
790 for (i = 0; i < c_h; ++i)
791 match &= (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U],
792 img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U],
793 c_w) == 0);
794
795 for (i = 0; i < c_h; ++i)
796 match &= (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_V],
797 img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_V],
798 c_w) == 0);
799
800 return match;
801 }
802
803 #define NELEMENTS(x) (sizeof(x) / sizeof(x[0]))
804 #if CONFIG_VP9_ENCODER
805 #define ARG_CTRL_CNT_MAX NELEMENTS(vp9_arg_ctrl_map)
806 #else
807 #define ARG_CTRL_CNT_MAX NELEMENTS(vp8_arg_ctrl_map)
808 #endif
809
810 #if !CONFIG_WEBM_IO
811 typedef int stereo_format_t;
812 struct WebmOutputContext {
813 int debug;
814 };
815 #endif
816
817 /* Per-stream configuration */
818 struct stream_config {
819 struct vpx_codec_enc_cfg cfg;
820 const char *out_fn;
821 const char *stats_fn;
822 #if CONFIG_FP_MB_STATS
823 const char *fpmb_stats_fn;
824 #endif
825 stereo_format_t stereo_fmt;
826 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
827 int arg_ctrl_cnt;
828 int write_webm;
829 #if CONFIG_VP9_HIGHBITDEPTH
830 // whether to use 16bit internal buffers
831 int use_16bit_internal;
832 #endif
833 };
834
835 struct stream_state {
836 int index;
837 struct stream_state *next;
838 struct stream_config config;
839 FILE *file;
840 struct rate_hist *rate_hist;
841 struct WebmOutputContext webm_ctx;
842 uint64_t psnr_sse_total;
843 uint64_t psnr_samples_total;
844 double psnr_totals[4];
845 int psnr_count;
846 int counts[64];
847 vpx_codec_ctx_t encoder;
848 unsigned int frames_out;
849 uint64_t cx_time;
850 size_t nbytes;
851 stats_io_t stats;
852 #if CONFIG_FP_MB_STATS
853 stats_io_t fpmb_stats;
854 #endif
855 struct vpx_image *img;
856 vpx_codec_ctx_t decoder;
857 int mismatch_seen;
858 };
859
860 static void validate_positive_rational(const char *msg,
861 struct vpx_rational *rat) {
862 if (rat->den < 0) {
863 rat->num *= -1;
864 rat->den *= -1;
865 }
866
867 if (rat->num < 0) die("Error: %s must be positive\n", msg);
868
869 if (!rat->den) die("Error: %s has zero denominator\n", msg);
870 }
871
872 static void parse_global_config(struct VpxEncoderConfig *global, char **argv) {
873 char **argi, **argj;
874 struct arg arg;
875 const int num_encoder = get_vpx_encoder_count();
876
877 if (num_encoder < 1) die("Error: no valid encoder available\n");
878
879 /* Initialize default parameters */
880 memset(global, 0, sizeof(*global));
881 global->codec = get_vpx_encoder_by_index(num_encoder - 1);
882 global->passes = 0;
883 global->color_type = I420;
884 /* Assign default deadline to good quality */
885 global->deadline = VPX_DL_GOOD_QUALITY;
886
887 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
888 arg.argv_step = 1;
889
890 if (arg_match(&arg, &codecarg, argi)) {
891 global->codec = get_vpx_encoder_by_name(arg.val);
892 if (!global->codec)
893 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
894 } else if (arg_match(&arg, &passes, argi)) {
895 global->passes = arg_parse_uint(&arg);
896
897 if (global->passes < 1 || global->passes > 2)
898 die("Error: Invalid number of passes (%d)\n", global->passes);
899 } else if (arg_match(&arg, &pass_arg, argi)) {
900 global->pass = arg_parse_uint(&arg);
901
902 if (global->pass < 1 || global->pass > 2)
903 die("Error: Invalid pass selected (%d)\n", global->pass);
904 } else if (arg_match(&arg, &usage, argi))
905 global->usage = arg_parse_uint(&arg);
906 else if (arg_match(&arg, &deadline, argi))
907 global->deadline = arg_parse_uint(&arg);
908 else if (arg_match(&arg, &best_dl, argi))
909 global->deadline = VPX_DL_BEST_QUALITY;
910 else if (arg_match(&arg, &good_dl, argi))
911 global->deadline = VPX_DL_GOOD_QUALITY;
912 else if (arg_match(&arg, &rt_dl, argi))
913 global->deadline = VPX_DL_REALTIME;
914 else if (arg_match(&arg, &use_yv12, argi))
915 global->color_type = YV12;
916 else if (arg_match(&arg, &use_i420, argi))
917 global->color_type = I420;
918 else if (arg_match(&arg, &use_i422, argi))
919 global->color_type = I422;
920 else if (arg_match(&arg, &use_i444, argi))
921 global->color_type = I444;
922 else if (arg_match(&arg, &use_i440, argi))
923 global->color_type = I440;
924 else if (arg_match(&arg, &quietarg, argi))
925 global->quiet = 1;
926 else if (arg_match(&arg, &verbosearg, argi))
927 global->verbose = 1;
928 else if (arg_match(&arg, &limit, argi))
929 global->limit = arg_parse_uint(&arg);
930 else if (arg_match(&arg, &skip, argi))
931 global->skip_frames = arg_parse_uint(&arg);
932 else if (arg_match(&arg, &psnrarg, argi))
933 global->show_psnr = 1;
934 else if (arg_match(&arg, &recontest, argi))
935 global->test_decode = arg_parse_enum_or_int(&arg);
936 else if (arg_match(&arg, &framerate, argi)) {
937 global->framerate = arg_parse_rational(&arg);
938 validate_positive_rational(arg.name, &global->framerate);
939 global->have_framerate = 1;
940 } else if (arg_match(&arg, &out_part, argi))
941 global->out_part = 1;
942 else if (arg_match(&arg, &debugmode, argi))
943 global->debug = 1;
944 else if (arg_match(&arg, &q_hist_n, argi))
945 global->show_q_hist_buckets = arg_parse_uint(&arg);
946 else if (arg_match(&arg, &rate_hist_n, argi))
947 global->show_rate_hist_buckets = arg_parse_uint(&arg);
948 else if (arg_match(&arg, &disable_warnings, argi))
949 global->disable_warnings = 1;
950 else if (arg_match(&arg, &disable_warning_prompt, argi))
951 global->disable_warning_prompt = 1;
952 else
953 argj++;
954 }
955
956 if (global->pass) {
957 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
958 if (global->pass > global->passes) {
959 warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
960 global->pass);
961 global->passes = global->pass;
962 }
963 }
964 /* Validate global config */
965 if (global->passes == 0) {
966 #if CONFIG_VP9_ENCODER
967 // Make default VP9 passes = 2 until there is a better quality 1-pass
968 // encoder
969 if (global->codec != NULL && global->codec->name != NULL)
970 global->passes = (strcmp(global->codec->name, "vp9") == 0 &&
971 global->deadline != VPX_DL_REALTIME)
972 ? 2
973 : 1;
974 #else
975 global->passes = 1;
976 #endif
977 }
978
979 if (global->deadline == VPX_DL_REALTIME && global->passes > 1) {
980 warn("Enforcing one-pass encoding in realtime mode\n");
981 global->passes = 1;
982 }
983 }
984
985 static void open_input_file(struct VpxInputContext *input) {
986 /* Parse certain options from the input file, if possible */
987 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
988 : set_binary_mode(stdin);
989
990 if (!input->file) fatal("Failed to open input file");
991
992 if (!fseeko(input->file, 0, SEEK_END)) {
993 /* Input file is seekable. Figure out how long it is, so we can get
994 * progress info.
995 */
996 input->length = ftello(input->file);
997 rewind(input->file);
998 }
999
1000 /* Default to 1:1 pixel aspect ratio. */
1001 input->pixel_aspect_ratio.numerator = 1;
1002 input->pixel_aspect_ratio.denominator = 1;
1003
1004 /* For RAW input sources, these bytes will applied on the first frame
1005 * in read_frame().
1006 */
1007 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1008 input->detect.position = 0;
1009
1010 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
1011 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4,
1012 input->only_i420) >= 0) {
1013 input->file_type = FILE_TYPE_Y4M;
1014 input->width = input->y4m.pic_w;
1015 input->height = input->y4m.pic_h;
1016 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
1017 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
1018 input->framerate.numerator = input->y4m.fps_n;
1019 input->framerate.denominator = input->y4m.fps_d;
1020 input->fmt = input->y4m.vpx_fmt;
1021 input->bit_depth = input->y4m.bit_depth;
1022 } else
1023 fatal("Unsupported Y4M stream.");
1024 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
1025 fatal("IVF is not supported as input.");
1026 } else {
1027 input->file_type = FILE_TYPE_RAW;
1028 }
1029 }
1030
1031 static void close_input_file(struct VpxInputContext *input) {
1032 fclose(input->file);
1033 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
1034 }
1035
1036 static struct stream_state *new_stream(struct VpxEncoderConfig *global,
1037 struct stream_state *prev) {
1038 struct stream_state *stream;
1039
1040 stream = calloc(1, sizeof(*stream));
1041 if (stream == NULL) {
1042 fatal("Failed to allocate new stream.");
1043 }
1044
1045 if (prev) {
1046 memcpy(stream, prev, sizeof(*stream));
1047 stream->index++;
1048 prev->next = stream;
1049 } else {
1050 vpx_codec_err_t res;
1051
1052 /* Populate encoder configuration */
1053 res = vpx_codec_enc_config_default(global->codec->codec_interface(),
1054 &stream->config.cfg, global->usage);
1055 if (res) fatal("Failed to get config: %s\n", vpx_codec_err_to_string(res));
1056
1057 /* Change the default timebase to a high enough value so that the
1058 * encoder will always create strictly increasing timestamps.
1059 */
1060 stream->config.cfg.g_timebase.den = 1000;
1061
1062 /* Never use the library's default resolution, require it be parsed
1063 * from the file or set on the command line.
1064 */
1065 stream->config.cfg.g_w = 0;
1066 stream->config.cfg.g_h = 0;
1067
1068 /* Initialize remaining stream parameters */
1069 stream->config.write_webm = 1;
1070 #if CONFIG_WEBM_IO
1071 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
1072 stream->webm_ctx.last_pts_ns = -1;
1073 stream->webm_ctx.writer = NULL;
1074 stream->webm_ctx.segment = NULL;
1075 #endif
1076
1077 /* Allows removal of the application version from the EBML tags */
1078 stream->webm_ctx.debug = global->debug;
1079
1080 /* Default lag_in_frames is 0 in realtime mode CBR mode*/
1081 if (global->deadline == VPX_DL_REALTIME &&
1082 stream->config.cfg.rc_end_usage == 1)
1083 stream->config.cfg.g_lag_in_frames = 0;
1084 }
1085
1086 /* Output files must be specified for each stream */
1087 stream->config.out_fn = NULL;
1088
1089 stream->next = NULL;
1090 return stream;
1091 }
1092
1093 static int parse_stream_params(struct VpxEncoderConfig *global,
1094 struct stream_state *stream, char **argv) {
1095 char **argi, **argj;
1096 struct arg arg;
1097 static const arg_def_t **ctrl_args = no_args;
1098 static const int *ctrl_args_map = NULL;
1099 struct stream_config *config = &stream->config;
1100 int eos_mark_found = 0;
1101 #if CONFIG_VP9_HIGHBITDEPTH
1102 int test_16bit_internal = 0;
1103 #endif
1104
1105 // Handle codec specific options
1106 if (0) {
1107 #if CONFIG_VP8_ENCODER
1108 } else if (strcmp(global->codec->name, "vp8") == 0) {
1109 ctrl_args = vp8_args;
1110 ctrl_args_map = vp8_arg_ctrl_map;
1111 #endif
1112 #if CONFIG_VP9_ENCODER
1113 } else if (strcmp(global->codec->name, "vp9") == 0) {
1114 ctrl_args = vp9_args;
1115 ctrl_args_map = vp9_arg_ctrl_map;
1116 #endif
1117 }
1118
1119 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1120 arg.argv_step = 1;
1121
1122 /* Once we've found an end-of-stream marker (--) we want to continue
1123 * shifting arguments but not consuming them.
1124 */
1125 if (eos_mark_found) {
1126 argj++;
1127 continue;
1128 } else if (!strcmp(*argj, "--")) {
1129 eos_mark_found = 1;
1130 continue;
1131 }
1132
1133 if (arg_match(&arg, &outputfile, argi)) {
1134 config->out_fn = arg.val;
1135 } else if (arg_match(&arg, &fpf_name, argi)) {
1136 config->stats_fn = arg.val;
1137 #if CONFIG_FP_MB_STATS
1138 } else if (arg_match(&arg, &fpmbf_name, argi)) {
1139 config->fpmb_stats_fn = arg.val;
1140 #endif
1141 } else if (arg_match(&arg, &use_webm, argi)) {
1142 #if CONFIG_WEBM_IO
1143 config->write_webm = 1;
1144 #else
1145 die("Error: --webm specified but webm is disabled.");
1146 #endif
1147 } else if (arg_match(&arg, &use_ivf, argi)) {
1148 config->write_webm = 0;
1149 } else if (arg_match(&arg, &threads, argi)) {
1150 config->cfg.g_threads = arg_parse_uint(&arg);
1151 } else if (arg_match(&arg, &profile, argi)) {
1152 config->cfg.g_profile = arg_parse_uint(&arg);
1153 } else if (arg_match(&arg, &width, argi)) {
1154 config->cfg.g_w = arg_parse_uint(&arg);
1155 } else if (arg_match(&arg, &height, argi)) {
1156 config->cfg.g_h = arg_parse_uint(&arg);
1157 #if CONFIG_VP9_HIGHBITDEPTH
1158 } else if (arg_match(&arg, &bitdeptharg, argi)) {
1159 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1160 } else if (arg_match(&arg, &inbitdeptharg, argi)) {
1161 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1162 #endif
1163 #if CONFIG_WEBM_IO
1164 } else if (arg_match(&arg, &stereo_mode, argi)) {
1165 config->stereo_fmt = arg_parse_enum_or_int(&arg);
1166 #endif
1167 } else if (arg_match(&arg, &timebase, argi)) {
1168 config->cfg.g_timebase = arg_parse_rational(&arg);
1169 validate_positive_rational(arg.name, &config->cfg.g_timebase);
1170 } else if (arg_match(&arg, &error_resilient, argi)) {
1171 config->cfg.g_error_resilient = arg_parse_uint(&arg);
1172 } else if (arg_match(&arg, &end_usage, argi)) {
1173 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1174 } else if (arg_match(&arg, &lag_in_frames, argi)) {
1175 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
1176 if (global->deadline == VPX_DL_REALTIME &&
1177 config->cfg.rc_end_usage == VPX_CBR &&
1178 config->cfg.g_lag_in_frames != 0) {
1179 warn("non-zero %s option ignored in realtime CBR mode.\n", arg.name);
1180 config->cfg.g_lag_in_frames = 0;
1181 }
1182 } else if (arg_match(&arg, &dropframe_thresh, argi)) {
1183 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
1184 } else if (arg_match(&arg, &resize_allowed, argi)) {
1185 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
1186 } else if (arg_match(&arg, &resize_width, argi)) {
1187 config->cfg.rc_scaled_width = arg_parse_uint(&arg);
1188 } else if (arg_match(&arg, &resize_height, argi)) {
1189 config->cfg.rc_scaled_height = arg_parse_uint(&arg);
1190 } else if (arg_match(&arg, &resize_up_thresh, argi)) {
1191 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
1192 } else if (arg_match(&arg, &resize_down_thresh, argi)) {
1193 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
1194 } else if (arg_match(&arg, &end_usage, argi)) {
1195 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1196 } else if (arg_match(&arg, &target_bitrate, argi)) {
1197 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
1198 } else if (arg_match(&arg, &min_quantizer, argi)) {
1199 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
1200 } else if (arg_match(&arg, &max_quantizer, argi)) {
1201 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
1202 } else if (arg_match(&arg, &undershoot_pct, argi)) {
1203 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1204 } else if (arg_match(&arg, &overshoot_pct, argi)) {
1205 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1206 } else if (arg_match(&arg, &buf_sz, argi)) {
1207 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1208 } else if (arg_match(&arg, &buf_initial_sz, argi)) {
1209 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1210 } else if (arg_match(&arg, &buf_optimal_sz, argi)) {
1211 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1212 } else if (arg_match(&arg, &bias_pct, argi)) {
1213 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1214 if (global->passes < 2)
1215 warn("option %s ignored in one-pass mode.\n", arg.name);
1216 } else if (arg_match(&arg, &minsection_pct, argi)) {
1217 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1218
1219 if (global->passes < 2)
1220 warn("option %s ignored in one-pass mode.\n", arg.name);
1221 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1222 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1223
1224 if (global->passes < 2)
1225 warn("option %s ignored in one-pass mode.\n", arg.name);
1226 } else if (arg_match(&arg, &kf_min_dist, argi)) {
1227 config->cfg.kf_min_dist = arg_parse_uint(&arg);
1228 } else if (arg_match(&arg, &kf_max_dist, argi)) {
1229 config->cfg.kf_max_dist = arg_parse_uint(&arg);
1230 } else if (arg_match(&arg, &kf_disabled, argi)) {
1231 config->cfg.kf_mode = VPX_KF_DISABLED;
1232 #if CONFIG_VP9_HIGHBITDEPTH
1233 } else if (arg_match(&arg, &test16bitinternalarg, argi)) {
1234 if (strcmp(global->codec->name, "vp9") == 0) {
1235 test_16bit_internal = 1;
1236 }
1237 #endif
1238 } else {
1239 int i, match = 0;
1240 for (i = 0; ctrl_args[i]; i++) {
1241 if (arg_match(&arg, ctrl_args[i], argi)) {
1242 int j;
1243 match = 1;
1244
1245 /* Point either to the next free element or the first
1246 * instance of this control.
1247 */
1248 for (j = 0; j < config->arg_ctrl_cnt; j++)
1249 if (ctrl_args_map != NULL &&
1250 config->arg_ctrls[j][0] == ctrl_args_map[i])
1251 break;
1252
1253 /* Update/insert */
1254 assert(j < (int)ARG_CTRL_CNT_MAX);
1255 if (ctrl_args_map != NULL && j < (int)ARG_CTRL_CNT_MAX) {
1256 config->arg_ctrls[j][0] = ctrl_args_map[i];
1257 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
1258 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
1259 }
1260 }
1261 }
1262 if (!match) argj++;
1263 }
1264 }
1265 #if CONFIG_VP9_HIGHBITDEPTH
1266 if (strcmp(global->codec->name, "vp9") == 0) {
1267 config->use_16bit_internal =
1268 test_16bit_internal | (config->cfg.g_profile > 1);
1269 }
1270 #endif
1271 return eos_mark_found;
1272 }
1273
1274 #define FOREACH_STREAM(func) \
1275 do { \
1276 struct stream_state *stream; \
1277 for (stream = streams; stream; stream = stream->next) { \
1278 func; \
1279 } \
1280 } while (0)
1281
1282 static void validate_stream_config(const struct stream_state *stream,
1283 const struct VpxEncoderConfig *global) {
1284 const struct stream_state *streami;
1285 (void)global;
1286
1287 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1288 fatal(
1289 "Stream %d: Specify stream dimensions with --width (-w) "
1290 " and --height (-h)",
1291 stream->index);
1292
1293 // Check that the codec bit depth is greater than the input bit depth.
1294 if (stream->config.cfg.g_input_bit_depth >
1295 (unsigned int)stream->config.cfg.g_bit_depth) {
1296 fatal("Stream %d: codec bit depth (%d) less than input bit depth (%d)",
1297 stream->index, (int)stream->config.cfg.g_bit_depth,
1298 stream->config.cfg.g_input_bit_depth);
1299 }
1300
1301 for (streami = stream; streami; streami = streami->next) {
1302 /* All streams require output files */
1303 if (!streami->config.out_fn)
1304 fatal("Stream %d: Output file is required (specify with -o)",
1305 streami->index);
1306
1307 /* Check for two streams outputting to the same file */
1308 if (streami != stream) {
1309 const char *a = stream->config.out_fn;
1310 const char *b = streami->config.out_fn;
1311 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1312 fatal("Stream %d: duplicate output file (from stream %d)",
1313 streami->index, stream->index);
1314 }
1315
1316 /* Check for two streams sharing a stats file. */
1317 if (streami != stream) {
1318 const char *a = stream->config.stats_fn;
1319 const char *b = streami->config.stats_fn;
1320 if (a && b && !strcmp(a, b))
1321 fatal("Stream %d: duplicate stats file (from stream %d)",
1322 streami->index, stream->index);
1323 }
1324
1325 #if CONFIG_FP_MB_STATS
1326 /* Check for two streams sharing a mb stats file. */
1327 if (streami != stream) {
1328 const char *a = stream->config.fpmb_stats_fn;
1329 const char *b = streami->config.fpmb_stats_fn;
1330 if (a && b && !strcmp(a, b))
1331 fatal("Stream %d: duplicate mb stats file (from stream %d)",
1332 streami->index, stream->index);
1333 }
1334 #endif
1335 }
1336 }
1337
1338 static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
1339 unsigned int h) {
1340 if (!stream->config.cfg.g_w) {
1341 if (!stream->config.cfg.g_h)
1342 stream->config.cfg.g_w = w;
1343 else
1344 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1345 }
1346 if (!stream->config.cfg.g_h) {
1347 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1348 }
1349 }
1350
1351 static const char *file_type_to_string(enum VideoFileType t) {
1352 switch (t) {
1353 case FILE_TYPE_RAW: return "RAW";
1354 case FILE_TYPE_Y4M: return "Y4M";
1355 default: return "Other";
1356 }
1357 }
1358
1359 static const char *image_format_to_string(vpx_img_fmt_t f) {
1360 switch (f) {
1361 case VPX_IMG_FMT_I420: return "I420";
1362 case VPX_IMG_FMT_I422: return "I422";
1363 case VPX_IMG_FMT_I444: return "I444";
1364 case VPX_IMG_FMT_I440: return "I440";
1365 case VPX_IMG_FMT_YV12: return "YV12";
1366 case VPX_IMG_FMT_I42016: return "I42016";
1367 case VPX_IMG_FMT_I42216: return "I42216";
1368 case VPX_IMG_FMT_I44416: return "I44416";
1369 case VPX_IMG_FMT_I44016: return "I44016";
1370 default: return "Other";
1371 }
1372 }
1373
1374 static void show_stream_config(struct stream_state *stream,
1375 struct VpxEncoderConfig *global,
1376 struct VpxInputContext *input) {
1377 #define SHOW(field) \
1378 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
1379
1380 if (stream->index == 0) {
1381 fprintf(stderr, "Codec: %s\n",
1382 vpx_codec_iface_name(global->codec->codec_interface()));
1383 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
1384 input->filename, file_type_to_string(input->file_type),
1385 image_format_to_string(input->fmt));
1386 }
1387 if (stream->next || stream->index)
1388 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1389 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1390 fprintf(stderr, "Encoder parameters:\n");
1391
1392 SHOW(g_usage);
1393 SHOW(g_threads);
1394 SHOW(g_profile);
1395 SHOW(g_w);
1396 SHOW(g_h);
1397 SHOW(g_bit_depth);
1398 SHOW(g_input_bit_depth);
1399 SHOW(g_timebase.num);
1400 SHOW(g_timebase.den);
1401 SHOW(g_error_resilient);
1402 SHOW(g_pass);
1403 SHOW(g_lag_in_frames);
1404 SHOW(rc_dropframe_thresh);
1405 SHOW(rc_resize_allowed);
1406 SHOW(rc_scaled_width);
1407 SHOW(rc_scaled_height);
1408 SHOW(rc_resize_up_thresh);
1409 SHOW(rc_resize_down_thresh);
1410 SHOW(rc_end_usage);
1411 SHOW(rc_target_bitrate);
1412 SHOW(rc_min_quantizer);
1413 SHOW(rc_max_quantizer);
1414 SHOW(rc_undershoot_pct);
1415 SHOW(rc_overshoot_pct);
1416 SHOW(rc_buf_sz);
1417 SHOW(rc_buf_initial_sz);
1418 SHOW(rc_buf_optimal_sz);
1419 SHOW(rc_2pass_vbr_bias_pct);
1420 SHOW(rc_2pass_vbr_minsection_pct);
1421 SHOW(rc_2pass_vbr_maxsection_pct);
1422 SHOW(kf_mode);
1423 SHOW(kf_min_dist);
1424 SHOW(kf_max_dist);
1425 }
1426
1427 static void open_output_file(struct stream_state *stream,
1428 struct VpxEncoderConfig *global,
1429 const struct VpxRational *pixel_aspect_ratio) {
1430 const char *fn = stream->config.out_fn;
1431 const struct vpx_codec_enc_cfg *const cfg = &stream->config.cfg;
1432
1433 if (cfg->g_pass == VPX_RC_FIRST_PASS) return;
1434
1435 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
1436
1437 if (!stream->file) fatal("Failed to open output file");
1438
1439 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1440 fatal("WebM output to pipes not supported.");
1441
1442 #if CONFIG_WEBM_IO
1443 if (stream->config.write_webm) {
1444 stream->webm_ctx.stream = stream->file;
1445 write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
1446 global->codec->fourcc, pixel_aspect_ratio);
1447 }
1448 #else
1449 (void)pixel_aspect_ratio;
1450 #endif
1451
1452 if (!stream->config.write_webm) {
1453 ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0);
1454 }
1455 }
1456
1457 static void close_output_file(struct stream_state *stream,
1458 unsigned int fourcc) {
1459 const struct vpx_codec_enc_cfg *const cfg = &stream->config.cfg;
1460
1461 if (cfg->g_pass == VPX_RC_FIRST_PASS) return;
1462
1463 #if CONFIG_WEBM_IO
1464 if (stream->config.write_webm) {
1465 write_webm_file_footer(&stream->webm_ctx);
1466 }
1467 #endif
1468
1469 if (!stream->config.write_webm) {
1470 if (!fseek(stream->file, 0, SEEK_SET))
1471 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
1472 stream->frames_out);
1473 }
1474
1475 fclose(stream->file);
1476 }
1477
1478 static void setup_pass(struct stream_state *stream,
1479 struct VpxEncoderConfig *global, int pass) {
1480 if (stream->config.stats_fn) {
1481 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
1482 fatal("Failed to open statistics store");
1483 } else {
1484 if (!stats_open_mem(&stream->stats, pass))
1485 fatal("Failed to open statistics store");
1486 }
1487
1488 #if CONFIG_FP_MB_STATS
1489 if (stream->config.fpmb_stats_fn) {
1490 if (!stats_open_file(&stream->fpmb_stats, stream->config.fpmb_stats_fn,
1491 pass))
1492 fatal("Failed to open mb statistics store");
1493 } else {
1494 if (!stats_open_mem(&stream->fpmb_stats, pass))
1495 fatal("Failed to open mb statistics store");
1496 }
1497 #endif
1498
1499 stream->config.cfg.g_pass = global->passes == 2
1500 ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS
1501 : VPX_RC_ONE_PASS;
1502 if (pass) {
1503 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
1504 #if CONFIG_FP_MB_STATS
1505 stream->config.cfg.rc_firstpass_mb_stats_in =
1506 stats_get(&stream->fpmb_stats);
1507 #endif
1508 }
1509
1510 stream->cx_time = 0;
1511 stream->nbytes = 0;
1512 stream->frames_out = 0;
1513 }
1514
1515 static void initialize_encoder(struct stream_state *stream,
1516 struct VpxEncoderConfig *global) {
1517 int i;
1518 int flags = 0;
1519
1520 flags |= global->show_psnr ? VPX_CODEC_USE_PSNR : 0;
1521 flags |= global->out_part ? VPX_CODEC_USE_OUTPUT_PARTITION : 0;
1522 #if CONFIG_VP9_HIGHBITDEPTH
1523 flags |= stream->config.use_16bit_internal ? VPX_CODEC_USE_HIGHBITDEPTH : 0;
1524 #endif
1525
1526 /* Construct Encoder Context */
1527 vpx_codec_enc_init(&stream->encoder, global->codec->codec_interface(),
1528 &stream->config.cfg, flags);
1529 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
1530
1531 /* Note that we bypass the vpx_codec_control wrapper macro because
1532 * we're being clever to store the control IDs in an array. Real
1533 * applications will want to make use of the enumerations directly
1534 */
1535 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1536 int ctrl = stream->config.arg_ctrls[i][0];
1537 int value = stream->config.arg_ctrls[i][1];
1538 if (vpx_codec_control_(&stream->encoder, ctrl, value))
1539 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
1540
1541 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1542 }
1543
1544 #if CONFIG_DECODERS
1545 if (global->test_decode != TEST_DECODE_OFF) {
1546 const VpxInterface *decoder = get_vpx_decoder_by_name(global->codec->name);
1547 vpx_codec_dec_init(&stream->decoder, decoder->codec_interface(), NULL, 0);
1548 }
1549 #endif
1550 }
1551
1552 static void encode_frame(struct stream_state *stream,
1553 struct VpxEncoderConfig *global, struct vpx_image *img,
1554 unsigned int frames_in) {
1555 vpx_codec_pts_t frame_start, next_frame_start;
1556 struct vpx_codec_enc_cfg *cfg = &stream->config.cfg;
1557 struct vpx_usec_timer timer;
1558
1559 frame_start =
1560 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1561 cfg->g_timebase.num / global->framerate.num;
1562 next_frame_start =
1563 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1564 cfg->g_timebase.num / global->framerate.num;
1565
1566 /* Scale if necessary */
1567 #if CONFIG_VP9_HIGHBITDEPTH
1568 if (img) {
1569 if ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) &&
1570 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1571 if (img->fmt != VPX_IMG_FMT_I42016) {
1572 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1573 exit(EXIT_FAILURE);
1574 }
1575 #if CONFIG_LIBYUV
1576 if (!stream->img) {
1577 stream->img =
1578 vpx_img_alloc(NULL, VPX_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
1579 }
1580 I420Scale_16(
1581 (uint16 *)img->planes[VPX_PLANE_Y], img->stride[VPX_PLANE_Y] / 2,
1582 (uint16 *)img->planes[VPX_PLANE_U], img->stride[VPX_PLANE_U] / 2,
1583 (uint16 *)img->planes[VPX_PLANE_V], img->stride[VPX_PLANE_V] / 2,
1584 img->d_w, img->d_h, (uint16 *)stream->img->planes[VPX_PLANE_Y],
1585 stream->img->stride[VPX_PLANE_Y] / 2,
1586 (uint16 *)stream->img->planes[VPX_PLANE_U],
1587 stream->img->stride[VPX_PLANE_U] / 2,
1588 (uint16 *)stream->img->planes[VPX_PLANE_V],
1589 stream->img->stride[VPX_PLANE_V] / 2, stream->img->d_w,
1590 stream->img->d_h, kFilterBox);
1591 img = stream->img;
1592 #else
1593 stream->encoder.err = 1;
1594 ctx_exit_on_error(&stream->encoder,
1595 "Stream %d: Failed to encode frame.\n"
1596 "Scaling disabled in this configuration. \n"
1597 "To enable, configure with --enable-libyuv\n",
1598 stream->index);
1599 #endif
1600 }
1601 }
1602 #endif
1603 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1604 if (img->fmt != VPX_IMG_FMT_I420 && img->fmt != VPX_IMG_FMT_YV12) {
1605 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1606 exit(EXIT_FAILURE);
1607 }
1608 #if CONFIG_LIBYUV
1609 if (!stream->img)
1610 stream->img =
1611 vpx_img_alloc(NULL, VPX_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
1612 I420Scale(
1613 img->planes[VPX_PLANE_Y], img->stride[VPX_PLANE_Y],
1614 img->planes[VPX_PLANE_U], img->stride[VPX_PLANE_U],
1615 img->planes[VPX_PLANE_V], img->stride[VPX_PLANE_V], img->d_w, img->d_h,
1616 stream->img->planes[VPX_PLANE_Y], stream->img->stride[VPX_PLANE_Y],
1617 stream->img->planes[VPX_PLANE_U], stream->img->stride[VPX_PLANE_U],
1618 stream->img->planes[VPX_PLANE_V], stream->img->stride[VPX_PLANE_V],
1619 stream->img->d_w, stream->img->d_h, kFilterBox);
1620 img = stream->img;
1621 #else
1622 stream->encoder.err = 1;
1623 ctx_exit_on_error(&stream->encoder,
1624 "Stream %d: Failed to encode frame.\n"
1625 "Scaling disabled in this configuration. \n"
1626 "To enable, configure with --enable-libyuv\n",
1627 stream->index);
1628 #endif
1629 }
1630
1631 vpx_usec_timer_start(&timer);
1632 vpx_codec_encode(&stream->encoder, img, frame_start,
1633 (unsigned long)(next_frame_start - frame_start), 0,
1634 global->deadline);
1635 vpx_usec_timer_mark(&timer);
1636 stream->cx_time += vpx_usec_timer_elapsed(&timer);
1637 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1638 stream->index);
1639 }
1640
1641 static void update_quantizer_histogram(struct stream_state *stream) {
1642 if (stream->config.cfg.g_pass != VPX_RC_FIRST_PASS) {
1643 int q;
1644
1645 vpx_codec_control(&stream->encoder, VP8E_GET_LAST_QUANTIZER_64, &q);
1646 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1647 stream->counts[q]++;
1648 }
1649 }
1650
1651 static void get_cx_data(struct stream_state *stream,
1652 struct VpxEncoderConfig *global, int *got_data) {
1653 const vpx_codec_cx_pkt_t *pkt;
1654 const struct vpx_codec_enc_cfg *cfg = &stream->config.cfg;
1655 vpx_codec_iter_t iter = NULL;
1656
1657 *got_data = 0;
1658 while ((pkt = vpx_codec_get_cx_data(&stream->encoder, &iter))) {
1659 static size_t fsize = 0;
1660 static int64_t ivf_header_pos = 0;
1661
1662 switch (pkt->kind) {
1663 case VPX_CODEC_CX_FRAME_PKT:
1664 if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
1665 stream->frames_out++;
1666 }
1667 if (!global->quiet)
1668 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
1669
1670 update_rate_histogram(stream->rate_hist, cfg, pkt);
1671 #if CONFIG_WEBM_IO
1672 if (stream->config.write_webm) {
1673 write_webm_block(&stream->webm_ctx, cfg, pkt);
1674 }
1675 #endif
1676 if (!stream->config.write_webm) {
1677 if (pkt->data.frame.partition_id <= 0) {
1678 ivf_header_pos = ftello(stream->file);
1679 fsize = pkt->data.frame.sz;
1680
1681 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1682 } else {
1683 fsize += pkt->data.frame.sz;
1684
1685 if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
1686 const int64_t currpos = ftello(stream->file);
1687 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1688 ivf_write_frame_size(stream->file, fsize);
1689 fseeko(stream->file, currpos, SEEK_SET);
1690 }
1691 }
1692
1693 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1694 stream->file);
1695 }
1696 stream->nbytes += pkt->data.raw.sz;
1697
1698 *got_data = 1;
1699 #if CONFIG_DECODERS
1700 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
1701 vpx_codec_decode(&stream->decoder, pkt->data.frame.buf,
1702 (unsigned int)pkt->data.frame.sz, NULL, 0);
1703 if (stream->decoder.err) {
1704 warn_or_exit_on_error(&stream->decoder,
1705 global->test_decode == TEST_DECODE_FATAL,
1706 "Failed to decode frame %d in stream %d",
1707 stream->frames_out + 1, stream->index);
1708 stream->mismatch_seen = stream->frames_out + 1;
1709 }
1710 }
1711 #endif
1712 break;
1713 case VPX_CODEC_STATS_PKT:
1714 stream->frames_out++;
1715 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
1716 pkt->data.twopass_stats.sz);
1717 stream->nbytes += pkt->data.raw.sz;
1718 break;
1719 #if CONFIG_FP_MB_STATS
1720 case VPX_CODEC_FPMB_STATS_PKT:
1721 stats_write(&stream->fpmb_stats, pkt->data.firstpass_mb_stats.buf,
1722 pkt->data.firstpass_mb_stats.sz);
1723 stream->nbytes += pkt->data.raw.sz;
1724 break;
1725 #endif
1726 case VPX_CODEC_PSNR_PKT:
1727
1728 if (global->show_psnr) {
1729 int i;
1730
1731 stream->psnr_sse_total += pkt->data.psnr.sse[0];
1732 stream->psnr_samples_total += pkt->data.psnr.samples[0];
1733 for (i = 0; i < 4; i++) {
1734 if (!global->quiet)
1735 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
1736 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
1737 }
1738 stream->psnr_count++;
1739 }
1740
1741 break;
1742 default: break;
1743 }
1744 }
1745 }
1746
1747 static void show_psnr(struct stream_state *stream, double peak) {
1748 int i;
1749 double ovpsnr;
1750
1751 if (!stream->psnr_count) return;
1752
1753 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1754 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total, peak,
1755 (double)stream->psnr_sse_total);
1756 fprintf(stderr, " %.3f", ovpsnr);
1757
1758 for (i = 0; i < 4; i++) {
1759 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
1760 }
1761 fprintf(stderr, "\n");
1762 }
1763
1764 static float usec_to_fps(uint64_t usec, unsigned int frames) {
1765 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
1766 }
1767
1768 static void test_decode(struct stream_state *stream,
1769 enum TestDecodeFatality fatal,
1770 const VpxInterface *codec) {
1771 vpx_image_t enc_img, dec_img;
1772
1773 if (stream->mismatch_seen) return;
1774
1775 /* Get the internal reference frame */
1776 if (strcmp(codec->name, "vp8") == 0) {
1777 struct vpx_ref_frame ref_enc, ref_dec;
1778 int width, height;
1779
1780 width = (stream->config.cfg.g_w + 15) & ~15;
1781 height = (stream->config.cfg.g_h + 15) & ~15;
1782 vpx_img_alloc(&ref_enc.img, VPX_IMG_FMT_I420, width, height, 1);
1783 enc_img = ref_enc.img;
1784 vpx_img_alloc(&ref_dec.img, VPX_IMG_FMT_I420, width, height, 1);
1785 dec_img = ref_dec.img;
1786
1787 ref_enc.frame_type = VP8_LAST_FRAME;
1788 ref_dec.frame_type = VP8_LAST_FRAME;
1789 vpx_codec_control(&stream->encoder, VP8_COPY_REFERENCE, &ref_enc);
1790 vpx_codec_control(&stream->decoder, VP8_COPY_REFERENCE, &ref_dec);
1791 } else {
1792 struct vp9_ref_frame ref_enc, ref_dec;
1793
1794 ref_enc.idx = 0;
1795 ref_dec.idx = 0;
1796 vpx_codec_control(&stream->encoder, VP9_GET_REFERENCE, &ref_enc);
1797 enc_img = ref_enc.img;
1798 vpx_codec_control(&stream->decoder, VP9_GET_REFERENCE, &ref_dec);
1799 dec_img = ref_dec.img;
1800 #if CONFIG_VP9_HIGHBITDEPTH
1801 if ((enc_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) !=
1802 (dec_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH)) {
1803 if (enc_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
1804 vpx_img_alloc(&enc_img, enc_img.fmt - VPX_IMG_FMT_HIGHBITDEPTH,
1805 enc_img.d_w, enc_img.d_h, 16);
1806 vpx_img_truncate_16_to_8(&enc_img, &ref_enc.img);
1807 }
1808 if (dec_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
1809 vpx_img_alloc(&dec_img, dec_img.fmt - VPX_IMG_FMT_HIGHBITDEPTH,
1810 dec_img.d_w, dec_img.d_h, 16);
1811 vpx_img_truncate_16_to_8(&dec_img, &ref_dec.img);
1812 }
1813 }
1814 #endif
1815 }
1816 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
1817 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
1818
1819 if (!compare_img(&enc_img, &dec_img)) {
1820 int y[4], u[4], v[4];
1821 #if CONFIG_VP9_HIGHBITDEPTH
1822 if (enc_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
1823 find_mismatch_high(&enc_img, &dec_img, y, u, v);
1824 } else {
1825 find_mismatch(&enc_img, &dec_img, y, u, v);
1826 }
1827 #else
1828 find_mismatch(&enc_img, &dec_img, y, u, v);
1829 #endif
1830 stream->decoder.err = 1;
1831 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
1832 "Stream %d: Encode/decode mismatch on frame %d at"
1833 " Y[%d, %d] {%d/%d},"
1834 " U[%d, %d] {%d/%d},"
1835 " V[%d, %d] {%d/%d}",
1836 stream->index, stream->frames_out, y[0], y[1], y[2],
1837 y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
1838 stream->mismatch_seen = stream->frames_out;
1839 }
1840
1841 vpx_img_free(&enc_img);
1842 vpx_img_free(&dec_img);
1843 }
1844
1845 static void print_time(const char *label, int64_t etl) {
1846 int64_t hours;
1847 int64_t mins;
1848 int64_t secs;
1849
1850 if (etl >= 0) {
1851 hours = etl / 3600;
1852 etl -= hours * 3600;
1853 mins = etl / 60;
1854 etl -= mins * 60;
1855 secs = etl;
1856
1857 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1858 hours, mins, secs);
1859 } else {
1860 fprintf(stderr, "[%3s unknown] ", label);
1861 }
1862 }
1863
1864 int main(int argc, const char **argv_) {
1865 int pass;
1866 vpx_image_t raw;
1867 #if CONFIG_VP9_HIGHBITDEPTH
1868 vpx_image_t raw_shift;
1869 int allocated_raw_shift = 0;
1870 int use_16bit_internal = 0;
1871 int input_shift = 0;
1872 #endif
1873 int frame_avail, got_data;
1874
1875 struct VpxInputContext input;
1876 struct VpxEncoderConfig global;
1877 struct stream_state *streams = NULL;
1878 char **argv, **argi;
1879 uint64_t cx_time = 0;
1880 int stream_cnt = 0;
1881 int res = 0;
1882
1883 memset(&input, 0, sizeof(input));
1884 exec_name = argv_[0];
1885
1886 if (argc < 3) usage_exit();
1887
1888 /* Setup default input stream settings */
1889 input.framerate.numerator = 30;
1890 input.framerate.denominator = 1;
1891 input.only_i420 = 1;
1892 input.bit_depth = 0;
1893
1894 /* First parse the global configuration values, because we want to apply
1895 * other parameters on top of the default configuration provided by the
1896 * codec.
1897 */
1898 argv = argv_dup(argc - 1, argv_ + 1);
1899 parse_global_config(&global, argv);
1900
1901 switch (global.color_type) {
1902 case I420: input.fmt = VPX_IMG_FMT_I420; break;
1903 case I422: input.fmt = VPX_IMG_FMT_I422; break;
1904 case I444: input.fmt = VPX_IMG_FMT_I444; break;
1905 case I440: input.fmt = VPX_IMG_FMT_I440; break;
1906 case YV12: input.fmt = VPX_IMG_FMT_YV12; break;
1907 }
1908
1909 {
1910 /* Now parse each stream's parameters. Using a local scope here
1911 * due to the use of 'stream' as loop variable in FOREACH_STREAM
1912 * loops
1913 */
1914 struct stream_state *stream = NULL;
1915
1916 do {
1917 stream = new_stream(&global, stream);
1918 stream_cnt++;
1919 if (!streams) streams = stream;
1920 } while (parse_stream_params(&global, stream, argv));
1921 }
1922
1923 /* Check for unrecognized options */
1924 for (argi = argv; *argi; argi++)
1925 if (argi[0][0] == '-' && argi[0][1])
1926 die("Error: Unrecognized option %s\n", *argi);
1927
1928 FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, &global,
1929 &stream->config.cfg););
1930
1931 /* Handle non-option arguments */
1932 input.filename = argv[0];
1933
1934 if (!input.filename) usage_exit();
1935
1936 /* Decide if other chroma subsamplings than 4:2:0 are supported */
1937 if (global.codec->fourcc == VP9_FOURCC) input.only_i420 = 0;
1938
1939 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
1940 int frames_in = 0, seen_frames = 0;
1941 int64_t estimated_time_left = -1;
1942 int64_t average_rate = -1;
1943 int64_t lagged_count = 0;
1944
1945 open_input_file(&input);
1946
1947 /* If the input file doesn't specify its w/h (raw files), try to get
1948 * the data from the first stream's configuration.
1949 */
1950 if (!input.width || !input.height) {
1951 FOREACH_STREAM({
1952 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
1953 input.width = stream->config.cfg.g_w;
1954 input.height = stream->config.cfg.g_h;
1955 break;
1956 }
1957 });
1958 }
1959
1960 /* Update stream configurations from the input file's parameters */
1961 if (!input.width || !input.height)
1962 fatal(
1963 "Specify stream dimensions with --width (-w) "
1964 " and --height (-h)");
1965
1966 /* If input file does not specify bit-depth but input-bit-depth parameter
1967 * exists, assume that to be the input bit-depth. However, if the
1968 * input-bit-depth paramter does not exist, assume the input bit-depth
1969 * to be the same as the codec bit-depth.
1970 */
1971 if (!input.bit_depth) {
1972 FOREACH_STREAM({
1973 if (stream->config.cfg.g_input_bit_depth)
1974 input.bit_depth = stream->config.cfg.g_input_bit_depth;
1975 else
1976 input.bit_depth = stream->config.cfg.g_input_bit_depth =
1977 (int)stream->config.cfg.g_bit_depth;
1978 });
1979 if (input.bit_depth > 8) input.fmt |= VPX_IMG_FMT_HIGHBITDEPTH;
1980 } else {
1981 FOREACH_STREAM(
1982 { stream->config.cfg.g_input_bit_depth = input.bit_depth; });
1983 }
1984
1985 FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height));
1986 FOREACH_STREAM(validate_stream_config(stream, &global));
1987
1988 /* Ensure that --passes and --pass are consistent. If --pass is set and
1989 * --passes=2, ensure --fpf was set.
1990 */
1991 if (global.pass && global.passes == 2)
1992 FOREACH_STREAM({
1993 if (!stream->config.stats_fn)
1994 die("Stream %d: Must specify --fpf when --pass=%d"
1995 " and --passes=2\n",
1996 stream->index, global.pass);
1997 });
1998
1999 #if !CONFIG_WEBM_IO
2000 FOREACH_STREAM({
2001 if (stream->config.write_webm) {
2002 stream->config.write_webm = 0;
2003 warn(
2004 "vpxenc was compiled without WebM container support."
2005 "Producing IVF output");
2006 }
2007 });
2008 #endif
2009
2010 /* Use the frame rate from the file only if none was specified
2011 * on the command-line.
2012 */
2013 if (!global.have_framerate) {
2014 global.framerate.num = input.framerate.numerator;
2015 global.framerate.den = input.framerate.denominator;
2016 FOREACH_STREAM(stream->config.cfg.g_timebase.den = global.framerate.num;
2017 stream->config.cfg.g_timebase.num = global.framerate.den);
2018 }
2019
2020 /* Show configuration */
2021 if (global.verbose && pass == 0)
2022 FOREACH_STREAM(show_stream_config(stream, &global, &input));
2023
2024 if (pass == (global.pass ? global.pass - 1 : 0)) {
2025 if (input.file_type == FILE_TYPE_Y4M)
2026 /*The Y4M reader does its own allocation.
2027 Just initialize this here to avoid problems if we never read any
2028 frames.*/
2029 memset(&raw, 0, sizeof(raw));
2030 else
2031 vpx_img_alloc(&raw, input.fmt, input.width, input.height, 32);
2032
2033 FOREACH_STREAM(stream->rate_hist = init_rate_histogram(
2034 &stream->config.cfg, &global.framerate));
2035 }
2036
2037 FOREACH_STREAM(setup_pass(stream, &global, pass));
2038 FOREACH_STREAM(
2039 open_output_file(stream, &global, &input.pixel_aspect_ratio));
2040 FOREACH_STREAM(initialize_encoder(stream, &global));
2041
2042 #if CONFIG_VP9_HIGHBITDEPTH
2043 if (strcmp(global.codec->name, "vp9") == 0) {
2044 // Check to see if at least one stream uses 16 bit internal.
2045 // Currently assume that the bit_depths for all streams using
2046 // highbitdepth are the same.
2047 FOREACH_STREAM({
2048 if (stream->config.use_16bit_internal) {
2049 use_16bit_internal = 1;
2050 }
2051 if (stream->config.cfg.g_profile == 0) {
2052 input_shift = 0;
2053 } else {
2054 input_shift = (int)stream->config.cfg.g_bit_depth -
2055 stream->config.cfg.g_input_bit_depth;
2056 }
2057 });
2058 }
2059 #endif
2060
2061 frame_avail = 1;
2062 got_data = 0;
2063
2064 while (frame_avail || got_data) {
2065 struct vpx_usec_timer timer;
2066
2067 if (!global.limit || frames_in < global.limit) {
2068 frame_avail = read_frame(&input, &raw);
2069
2070 if (frame_avail) frames_in++;
2071 seen_frames =
2072 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
2073
2074 if (!global.quiet) {
2075 float fps = usec_to_fps(cx_time, seen_frames);
2076 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2077
2078 if (stream_cnt == 1)
2079 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2080 streams->frames_out, (int64_t)streams->nbytes);
2081 else
2082 fprintf(stderr, "frame %4d ", frames_in);
2083
2084 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
2085 cx_time > 9999999 ? cx_time / 1000 : cx_time,
2086 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
2087 fps >= 1.0 ? "fps" : "fpm");
2088 print_time("ETA", estimated_time_left);
2089 }
2090
2091 } else
2092 frame_avail = 0;
2093
2094 if (frames_in > global.skip_frames) {
2095 #if CONFIG_VP9_HIGHBITDEPTH
2096 vpx_image_t *frame_to_encode;
2097 if (input_shift || (use_16bit_internal && input.bit_depth == 8)) {
2098 assert(use_16bit_internal);
2099 // Input bit depth and stream bit depth do not match, so up
2100 // shift frame to stream bit depth
2101 if (!allocated_raw_shift) {
2102 vpx_img_alloc(&raw_shift, raw.fmt | VPX_IMG_FMT_HIGHBITDEPTH,
2103 input.width, input.height, 32);
2104 allocated_raw_shift = 1;
2105 }
2106 vpx_img_upshift(&raw_shift, &raw, input_shift);
2107 frame_to_encode = &raw_shift;
2108 } else {
2109 frame_to_encode = &raw;
2110 }
2111 vpx_usec_timer_start(&timer);
2112 if (use_16bit_internal) {
2113 assert(frame_to_encode->fmt & VPX_IMG_FMT_HIGHBITDEPTH);
2114 FOREACH_STREAM({
2115 if (stream->config.use_16bit_internal)
2116 encode_frame(stream, &global,
2117 frame_avail ? frame_to_encode : NULL, frames_in);
2118 else
2119 assert(0);
2120 });
2121 } else {
2122 assert((frame_to_encode->fmt & VPX_IMG_FMT_HIGHBITDEPTH) == 0);
2123 FOREACH_STREAM(encode_frame(stream, &global,
2124 frame_avail ? frame_to_encode : NULL,
2125 frames_in));
2126 }
2127 #else
2128 vpx_usec_timer_start(&timer);
2129 FOREACH_STREAM(encode_frame(stream, &global, frame_avail ? &raw : NULL,
2130 frames_in));
2131 #endif
2132 vpx_usec_timer_mark(&timer);
2133 cx_time += vpx_usec_timer_elapsed(&timer);
2134
2135 FOREACH_STREAM(update_quantizer_histogram(stream));
2136
2137 got_data = 0;
2138 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
2139
2140 if (!got_data && input.length && streams != NULL &&
2141 !streams->frames_out) {
2142 lagged_count = global.limit ? seen_frames : ftello(input.file);
2143 } else if (input.length) {
2144 int64_t remaining;
2145 int64_t rate;
2146
2147 if (global.limit) {
2148 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
2149
2150 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
2151 remaining = 1000 * (global.limit - global.skip_frames -
2152 seen_frames + lagged_count);
2153 } else {
2154 const int64_t input_pos = ftello(input.file);
2155 const int64_t input_pos_lagged = input_pos - lagged_count;
2156 const int64_t limit = input.length;
2157
2158 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
2159 remaining = limit - input_pos + lagged_count;
2160 }
2161
2162 average_rate =
2163 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
2164 estimated_time_left = average_rate ? remaining / average_rate : -1;
2165 }
2166
2167 if (got_data && global.test_decode != TEST_DECODE_OFF)
2168 FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec));
2169 }
2170
2171 fflush(stdout);
2172 if (!global.quiet) fprintf(stderr, "\033[K");
2173 }
2174
2175 if (stream_cnt > 1) fprintf(stderr, "\n");
2176
2177 if (!global.quiet) {
2178 FOREACH_STREAM(fprintf(
2179 stderr, "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2180 "b/f %7" PRId64 "b/s"
2181 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2182 pass + 1, global.passes, frames_in, stream->frames_out,
2183 (int64_t)stream->nbytes,
2184 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0,
2185 seen_frames
2186 ? (int64_t)stream->nbytes * 8 * (int64_t)global.framerate.num /
2187 global.framerate.den / seen_frames
2188 : 0,
2189 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2190 stream->cx_time > 9999999 ? "ms" : "us",
2191 usec_to_fps(stream->cx_time, seen_frames)));
2192 }
2193
2194 if (global.show_psnr) {
2195 if (global.codec->fourcc == VP9_FOURCC) {
2196 FOREACH_STREAM(
2197 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1));
2198 } else {
2199 FOREACH_STREAM(show_psnr(stream, 255.0));
2200 }
2201 }
2202
2203 FOREACH_STREAM(vpx_codec_destroy(&stream->encoder));
2204
2205 if (global.test_decode != TEST_DECODE_OFF) {
2206 FOREACH_STREAM(vpx_codec_destroy(&stream->decoder));
2207 }
2208
2209 close_input_file(&input);
2210
2211 if (global.test_decode == TEST_DECODE_FATAL) {
2212 FOREACH_STREAM(res |= stream->mismatch_seen);
2213 }
2214 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
2215
2216 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
2217
2218 #if CONFIG_FP_MB_STATS
2219 FOREACH_STREAM(stats_close(&stream->fpmb_stats, global.passes - 1));
2220 #endif
2221
2222 if (global.pass) break;
2223 }
2224
2225 if (global.show_q_hist_buckets)
2226 FOREACH_STREAM(
2227 show_q_histogram(stream->counts, global.show_q_hist_buckets));
2228
2229 if (global.show_rate_hist_buckets)
2230 FOREACH_STREAM(show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2231 global.show_rate_hist_buckets));
2232 FOREACH_STREAM(destroy_rate_histogram(stream->rate_hist));
2233
2234 #if CONFIG_INTERNAL_STATS
2235 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2236 * to match some existing utilities.
2237 */
2238 if (!(global.pass == 1 && global.passes == 2))
2239 FOREACH_STREAM({
2240 FILE *f = fopen("opsnr.stt", "a");
2241 if (stream->mismatch_seen) {
2242 fprintf(f, "First mismatch occurred in frame %d\n",
2243 stream->mismatch_seen);
2244 } else {
2245 fprintf(f, "No mismatch detected in recon buffers\n");
2246 }
2247 fclose(f);
2248 });
2249 #endif
2250
2251 #if CONFIG_VP9_HIGHBITDEPTH
2252 if (allocated_raw_shift) vpx_img_free(&raw_shift);
2253 #endif
2254 vpx_img_free(&raw);
2255 free(argv);
2256 free(streams);
2257 return res ? EXIT_FAILURE : EXIT_SUCCESS;
2258 }
vpx_rational
Rational Number.
Definition: vpx_encoder.h:235
VPX_IMG_FMT_I44016
Definition: vpx_image.h:66
vpx_codec_cx_pkt::twopass_stats
vpx_fixed_buf_t twopass_stats
Definition: vpx_encoder.h:191
VP8E_SET_CPUUSED
Codec control function to set encoder internal speed settings.
Definition: vp8cx.h:155
VPX_Q
Definition: vpx_encoder.h:252
vpx_image
Image Descriptor.
Definition: vpx_image.h:88
vpx_decoder.h
Describes the decoder algorithm interface to applications.
vpx_encoder.h
Describes the encoder algorithm interface to applications.
vpx_codec_iface_name
const char * vpx_codec_iface_name(vpx_codec_iface_t *iface)
Return the name for a given interface.
VPX_CS_BT_709
Definition: vpx_image.h:73
VPX_IMG_FMT_YV12
Definition: vpx_image.h:53
vpx_codec_err_to_string
const char * vpx_codec_err_to_string(vpx_codec_err_t err)
Convert error number to printable string.
VP9E_SET_TUNE_CONTENT
Codec control function to set content type.
Definition: vp8cx.h:449
vpx_codec_enc_cfg::g_timebase
struct vpx_rational g_timebase
Stream timebase units.
Definition: vpx_encoder.h:363
VPX_CBR
Definition: vpx_encoder.h:250
VP9E_SET_NOISE_SENSITIVITY
Codec control function to set noise sensitivity.
Definition: vp8cx.h:414
VPX_CS_RESERVED
Definition: vpx_image.h:77
VPX_CS_SRGB
Definition: vpx_image.h:78
VPX_CS_SMPTE_240
Definition: vpx_image.h:75
vpx_rational::den
int den
Definition: vpx_encoder.h:237
vpx_codec_encode
vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img, vpx_codec_pts_t pts, unsigned long duration, vpx_enc_frame_flags_t flags, unsigned long deadline)
Encode a frame.
VPX_CODEC_STATS_PKT
Definition: vpx_encoder.h:158
vp8dx.h
Provides definitions for using VP8 or VP9 within the vpx Decoder interface.
vpx_codec_enc_cfg
Encoder configuration structure.
Definition: vpx_encoder.h:285
VP8E_SET_TUNING
Codec control function to set visual tuning.
Definition: vp8cx.h:226
VP8E_SET_CQ_LEVEL
Codec control function to set constrained quality level.
Definition: vp8cx.h:236
VP8E_SET_ARNR_TYPE
Definition: vp8cx.h:220
VPX_CODEC_PSNR_PKT
Definition: vpx_encoder.h:160
VPX_PLANE_Y
#define VPX_PLANE_Y
Definition: vpx_image.h:112
VP8E_SET_MAX_INTRA_BITRATE_PCT
Codec control function to set Max data rate for Intra frames.
Definition: vp8cx.h:251
VPX_CODEC_USE_HIGHBITDEPTH
#define VPX_CODEC_USE_HIGHBITDEPTH
Definition: vpx_encoder.h:100
vpx_codec_cx_pkt
Encoder output packet.
Definition: vpx_encoder.h:175
vpx_fixed_buf::buf
void * buf
Definition: vpx_encoder.h:107
VPX_PLANE_V
#define VPX_PLANE_V
Definition: vpx_image.h:114
VPX_RC_FIRST_PASS
Definition: vpx_encoder.h:243
VPX_RC_LAST_PASS
Definition: vpx_encoder.h:244
vpx_image::x_chroma_shift
unsigned int x_chroma_shift
Definition: vpx_image.h:107
vpx_image::y_chroma_shift
unsigned int y_chroma_shift
Definition: vpx_image.h:108
VP9E_SET_TILE_COLUMNS
Codec control function to set number of tile columns.
Definition: vp8cx.h:344
VPX_IMG_FMT_HIGHBITDEPTH
#define VPX_IMG_FMT_HIGHBITDEPTH
Definition: vpx_image.h:35
vpx_codec_cx_pkt::frame
struct vpx_codec_cx_pkt::@1::@2 frame
VP8_COPY_REFERENCE
Definition: vp8.h:48
VPX_IMG_FMT_I44416
Definition: vpx_image.h:65
vpx_img_alloc
vpx_image_t * vpx_img_alloc(vpx_image_t *img, vpx_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
VPX_IMG_FMT_I420
Definition: vpx_image.h:55
vpx_image::d_w
unsigned int d_w
Definition: vpx_image.h:99
vpx_codec_dec_init
#define vpx_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_dec_init_ver()
Definition: vpx_decoder.h:144
VP9E_SET_TARGET_LEVEL
Codec control function to set target level.
Definition: vp8cx.h:548
vpx_codec_enc_cfg::g_w
unsigned int g_w
Width of the frame.
Definition: vpx_encoder.h:324
VPX_IMG_FMT_I42216
Definition: vpx_image.h:64
VP9E_SET_AQ_MODE
Codec control function to set adaptive quantization mode.
Definition: vp8cx.h:391
VP9E_SET_COLOR_SPACE
Codec control function to set color space info.
Definition: vp8cx.h:480
vpx_codec_decode
vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, const uint8_t *data, unsigned int data_sz, void *user_priv, long deadline)
Decode data.
vpx_codec_enc_cfg::g_h
unsigned int g_h
Height of the frame.
Definition: vpx_encoder.h:333
vpx_img_fmt_t
enum vpx_img_fmt vpx_img_fmt_t
List of supported image formats.
vpx_image::stride
int stride[4]
Definition: vpx_image.h:117
vpx_codec_cx_pkt::kind
enum vpx_codec_cx_pkt_kind kind
Definition: vpx_encoder.h:176
VP9E_SET_LOSSLESS
Codec control function to set lossless encoding mode.
Definition: vp8cx.h:321
vpx_codec_cx_pkt::raw
vpx_fixed_buf_t raw
Definition: vpx_encoder.h:198
VP8E_GET_LAST_QUANTIZER_64
Codec control function to get last quantizer chosen by the encoder.
Definition: vp8cx.h:205
VPX_CS_SMPTE_170
Definition: vpx_image.h:74
VP8E_SET_GF_CBR_BOOST_PCT
Boost percentage for Golden Frame in CBR mode.
Definition: vp8cx.h:579
vpx_img_free
void vpx_img_free(vpx_image_t *img)
Close an image descriptor.
VPX_CODEC_USE_OUTPUT_PARTITION
#define VPX_CODEC_USE_OUTPUT_PARTITION
Make the encoder output one partition at a time.
Definition: vpx_encoder.h:99
vpx_image::fmt
vpx_img_fmt_t fmt
Definition: vpx_image.h:89
VPX_IMG_FMT_I422
Definition: vpx_image.h:59
vpx_image::planes
unsigned char * planes[4]
Definition: vpx_image.h:116
VPX_CS_BT_601
Definition: vpx_image.h:72
VP8E_SET_TOKEN_PARTITIONS
Codec control function to set the number of token partitions.
Definition: vp8cx.h:188
VPX_DL_REALTIME
#define VPX_DL_REALTIME
deadline parameter analogous to VPx REALTIME mode.
Definition: vpx_encoder.h:842
vpx_rational::num
int num
Definition: vpx_encoder.h:236
VP8E_SET_NOISE_SENSITIVITY
control function to set noise sensitivity
Definition: vp8cx.h:170
VPX_BITS_12
Definition: vpx_codec.h:215
VP9E_SET_GF_CBR_BOOST_PCT
Boost percentage for Golden Frame in CBR mode.
Definition: vp8cx.h:287
VPX_DL_BEST_QUALITY
#define VPX_DL_BEST_QUALITY
deadline parameter analogous to VPx BEST QUALITY mode.
Definition: vpx_encoder.h:846
VPX_VBR
Definition: vpx_encoder.h:249
vpx_codec_enc_cfg::g_pass
enum vpx_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: vpx_encoder.h:378
vpx_codec_cx_pkt::psnr
double psnr[4]
Definition: vpx_encoder.h:196
VPX_CODEC_USE_PSNR
#define VPX_CODEC_USE_PSNR
Initialization-time Feature Enabling.
Definition: vpx_encoder.h:97
VPX_DL_GOOD_QUALITY
#define VPX_DL_GOOD_QUALITY
deadline parameter analogous to VPx GOOD QUALITY mode.
Definition: vpx_encoder.h:844
vpx_codec_cx_pkt::firstpass_mb_stats
vpx_fixed_buf_t firstpass_mb_stats
Definition: vpx_encoder.h:192
vpx_codec_error_detail
const char * vpx_codec_error_detail(vpx_codec_ctx_t *ctx)
Retrieve detailed error information for codec context.
vp8cx.h
Provides definitions for using VP8 or VP9 encoder algorithm within the vpx Codec Interface.
VPX_PLANE_U
#define VPX_PLANE_U
Definition: vpx_image.h:113
vpx_codec_enc_init
#define vpx_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for vpx_codec_enc_init_ver()
Definition: vpx_encoder.h:753
VP8E_SET_SCREEN_CONTENT_MODE
Codec control function to set encoder screen content mode.
Definition: vp8cx.h:306
vpx_codec_err_t
vpx_codec_err_t
Algorithm return codes.
Definition: vpx_codec.h:89
vpx_codec_get_cx_data
const vpx_codec_cx_pkt_t * vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter)
Encoded data iterator.
vpx_codec_cx_pkt::data
union vpx_codec_cx_pkt::@1 data
VP8E_SET_ARNR_MAXFRAMES
Codec control function to set the max no of frames to create arf.
Definition: vp8cx.h:211
vp9_ref_frame
VP9 specific reference frame data struct.
Definition: vp8.h:119
VPX_KF_DISABLED
Definition: vpx_encoder.h:266
VPX_IMG_FMT_I42016
Definition: vpx_image.h:63
VP8E_SET_ARNR_STRENGTH
Codec control function to set the filter strength for the arf.
Definition: vp8cx.h:217
VP9E_SET_FRAME_PERIODIC_BOOST
Codec control function to enable/disable periodic Q boost.
Definition: vp8cx.h:406
VPX_CODEC_FPMB_STATS_PKT
Definition: vpx_encoder.h:159
vpx_codec_pts_t
int64_t vpx_codec_pts_t
Time Stamp Type.
Definition: vpx_encoder.h:116
VPX_IMG_FMT_I444
Definition: vpx_image.h:60
vpx_codec_enc_config_default
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg, unsigned int reserved)
Get a default configuration.
VP8E_SET_ENABLEAUTOALTREF
Codec control function to enable automatic set and use alf frames.
Definition: vp8cx.h:161
vpx_codec_control_
vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx, int ctrl_id,...)
Control algorithm.
VP9E_SET_MIN_GF_INTERVAL
Codec control function to set minimum interval between GF/ARF frames.
Definition: vp8cx.h:500
vpx_ref_frame
reference frame data struct
Definition: vp8.h:110
VP9E_SET_MAX_GF_INTERVAL
Codec control function to set minimum interval between GF/ARF frames.
Definition: vp8cx.h:508
VPX_CQ
Definition: vpx_encoder.h:251
vp9_ref_frame::idx
int idx
Definition: vp8.h:120
vpx_codec_control
#define vpx_codec_control(ctx, id, data)
vpx_codec_control wrapper macro
Definition: vpx_codec.h:399
vpx_codec_destroy
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx)
Destroy a codec instance.
VP9E_SET_FRAME_PARALLEL_DECODING
Codec control function to enable frame parallel decoding feature.
Definition: vp8cx.h:378
vpx_image::d_h
unsigned int d_h
Definition: vpx_image.h:100
VPX_CS_UNKNOWN
Definition: vpx_image.h:71
vpx_fixed_buf::sz
size_t sz
Definition: vpx_encoder.h:108
VP9E_SET_MAX_INTER_BITRATE_PCT
Codec control function to set max data rate for Inter frames.
Definition: vp8cx.h:272
VPX_BITS_8
Definition: vpx_codec.h:213
vpx_codec_ctx::err
vpx_codec_err_t err
Definition: vpx_codec.h:195
VP9_GET_REFERENCE
Definition: vp8.h:59
VP8E_SET_STATIC_THRESHOLD
Codec control function to set the threshold for MBs treated static.
Definition: vp8cx.h:182
vpx_codec_error
const char * vpx_codec_error(vpx_codec_ctx_t *ctx)
Retrieve error synopsis for codec context.
VPX_CS_BT_2020
Definition: vpx_image.h:76
VPX_IMG_FMT_I440
Definition: vpx_image.h:61
VPX_BITS_10
Definition: vpx_codec.h:214
VP9E_SET_TILE_ROWS
Codec control function to set number of tile rows.
Definition: vp8cx.h:364
vpx_codec_iter_t
const void * vpx_codec_iter_t
Iterator.
Definition: vpx_codec.h:182
VP8E_SET_SHARPNESS
Codec control function to set sharpness.
Definition: vp8cx.h:176
VPX_CODEC_CX_FRAME_PKT
Definition: vpx_encoder.h:157
VP9E_SET_ALT_REF_AQ
Codec control function to enable/disable special mode for altref adaptive quantization. You can use it with –aq-mode concurrently.
Definition: vp8cx.h:564
VPX_FRAME_IS_FRAGMENT
#define VPX_FRAME_IS_FRAGMENT
this is a fragment of the encoded frame
Definition: vpx_encoder.h:133
VPX_RC_ONE_PASS
Definition: vpx_encoder.h:242
vpx_codec_ctx
Codec context structure.
Definition: vpx_codec.h:192