2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license and patent
5 * grant that can be found in the LICENSE file in the root of the source
6 * tree. All contributing project authors may be found in the AUTHORS
7 * file in the root of the source tree.
11 /* This is a simple program that encodes YV12 files and generates ivf
12 * files using the new interface.
14 #define USE_POSIX_MMAP HAVE_SYS_MMAN_H
20 #include "vpx_encoder.h"
22 #include <sys/types.h>
28 #if CONFIG_VP8_ENCODER
31 #include "vpx_ports/mem_ops.h"
32 #include "vpx_ports/vpx_timer.h"
34 static const char *exec_name
;
36 static const struct codec_item
39 const vpx_codec_iface_t
*iface
;
43 #if CONFIG_VP8_ENCODER
44 {"vp8", &vpx_codec_vp8_cx_algo
, 0x30385056},
48 static void usage_exit();
50 void die(const char *fmt
, ...)
59 static void ctx_exit_on_error(vpx_codec_ctx_t
*ctx
, const char *s
)
63 const char *detail
= vpx_codec_error_detail(ctx
);
65 printf("%s: %s\n", s
, vpx_codec_error(ctx
));
68 printf(" %s\n", detail
);
74 /* This structure is used to abstract the different ways of handling
75 * first pass statistics.
86 int stats_open_file(stats_io_t
*stats
, const char *fpf
, int pass
)
94 stats
->file
= fopen(fpf
, "wb");
96 stats
->buf
.buf
= NULL
,
97 res
= (stats
->file
!= NULL
);
103 struct stat stat_buf
;
106 fd
= open(fpf
, O_RDONLY
);
107 stats
->file
= fdopen(fd
, "rb");
108 fstat(fd
, &stat_buf
);
109 stats
->buf
.sz
= stat_buf
.st_size
;
110 stats
->buf
.buf
= mmap(NULL
, stats
->buf
.sz
, PROT_READ
, MAP_PRIVATE
,
112 res
= (stats
->buf
.buf
!= NULL
);
116 stats
->file
= fopen(fpf
, "rb");
118 if (fseek(stats
->file
, 0, SEEK_END
))
120 fprintf(stderr
, "First-pass stats file must be seekable!\n");
124 stats
->buf
.sz
= stats
->buf_alloc_sz
= ftell(stats
->file
);
127 stats
->buf
.buf
= malloc(stats
->buf_alloc_sz
);
131 fprintf(stderr
, "Failed to allocate first-pass stats buffer (%d bytes)\n",
132 stats
->buf_alloc_sz
);
136 nbytes
= fread(stats
->buf
.buf
, 1, stats
->buf
.sz
, stats
->file
);
137 res
= (nbytes
== stats
->buf
.sz
);
144 int stats_open_mem(stats_io_t
*stats
, int pass
)
152 stats
->buf_alloc_sz
= 64 * 1024;
153 stats
->buf
.buf
= malloc(stats
->buf_alloc_sz
);
156 stats
->buf_ptr
= stats
->buf
.buf
;
157 res
= (stats
->buf
.buf
!= NULL
);
162 void stats_close(stats_io_t
*stats
)
166 if (stats
->pass
== 1)
170 munmap(stats
->buf
.buf
, stats
->buf
.sz
);
172 free(stats
->buf
.buf
);
181 if (stats
->pass
== 1)
182 free(stats
->buf
.buf
);
186 void stats_write(stats_io_t
*stats
, const void *pkt
, size_t len
)
190 fwrite(pkt
, 1, len
, stats
->file
);
194 if (stats
->buf
.sz
+ len
> stats
->buf_alloc_sz
)
196 size_t new_sz
= stats
->buf_alloc_sz
+ 64 * 1024;
197 char *new_ptr
= realloc(stats
->buf
.buf
, new_sz
);
201 stats
->buf_ptr
= new_ptr
+ (stats
->buf_ptr
- (char *)stats
->buf
.buf
);
202 stats
->buf
.buf
= new_ptr
;
203 stats
->buf_alloc_sz
= new_sz
;
207 memcpy(stats
->buf_ptr
, pkt
, len
);
208 stats
->buf
.sz
+= len
;
209 stats
->buf_ptr
+= len
;
213 vpx_fixed_buf_t
stats_get(stats_io_t
*stats
)
218 #define IVF_FRAME_HDR_SZ (4+8) /* 4 byte size + 8 byte timestamp */
219 static int read_frame(FILE *f
, vpx_image_t
*img
, unsigned int is_ivf
)
225 char junk
[IVF_FRAME_HDR_SZ
];
227 /* Skip the frame header. We know how big the frame should be. See
228 * write_ivf_frame_header() for documentation on the frame header
231 fread(junk
, 1, IVF_FRAME_HDR_SZ
, f
);
234 for (plane
= 0; plane
< 3; plane
++)
237 int w
= (plane
? (1 + img
->d_w
) / 2 : img
->d_w
);
238 int h
= (plane
? (1 + img
->d_h
) / 2 : img
->d_h
);
241 /* Determine the correct plane based on the image format. The for-loop
242 * always counts in Y,U,V order, but this may not match the order of
248 ptr
= img
->planes
[img
->fmt
==IMG_FMT_YV12
? PLANE_V
: PLANE_U
];
251 ptr
= img
->planes
[img
->fmt
==IMG_FMT_YV12
?PLANE_U
: PLANE_V
];
254 ptr
= img
->planes
[plane
];
257 for (r
= 0; r
< h
; r
++)
260 ptr
+= img
->stride
[plane
];
268 #define IVF_FILE_HDR_SZ (32)
269 unsigned int file_is_ivf(FILE *infile
,
270 unsigned int *fourcc
,
272 unsigned int *height
)
274 char raw_hdr
[IVF_FILE_HDR_SZ
];
277 /* See write_ivf_file_header() for more documentation on the file header
280 if (fread(raw_hdr
, 1, IVF_FILE_HDR_SZ
, infile
) == IVF_FILE_HDR_SZ
)
282 if (raw_hdr
[0] == 'D' && raw_hdr
[1] == 'K'
283 && raw_hdr
[2] == 'I' && raw_hdr
[3] == 'F')
287 if (mem_get_le16(raw_hdr
+ 4) != 0)
288 fprintf(stderr
, "Error: Unrecognized IVF version! This file may not"
289 " decode properly.");
291 *fourcc
= mem_get_le32(raw_hdr
+ 8);
297 *width
= mem_get_le16(raw_hdr
+ 12);
298 *height
= mem_get_le16(raw_hdr
+ 14);
307 static void write_ivf_file_header(FILE *outfile
,
308 const vpx_codec_enc_cfg_t
*cfg
,
314 if (cfg
->g_pass
!= VPX_RC_ONE_PASS
&& cfg
->g_pass
!= VPX_RC_LAST_PASS
)
321 mem_put_le16(header
+ 4, 0); /* version */
322 mem_put_le16(header
+ 6, 32); /* headersize */
323 mem_put_le32(header
+ 8, fourcc
); /* headersize */
324 mem_put_le16(header
+ 12, cfg
->g_w
); /* width */
325 mem_put_le16(header
+ 14, cfg
->g_h
); /* height */
326 mem_put_le32(header
+ 16, cfg
->g_timebase
.den
); /* rate */
327 mem_put_le32(header
+ 20, cfg
->g_timebase
.num
); /* scale */
328 mem_put_le32(header
+ 24, frame_cnt
); /* length */
329 mem_put_le32(header
+ 28, 0); /* unused */
331 fwrite(header
, 1, 32, outfile
);
335 static void write_ivf_frame_header(FILE *outfile
,
336 const vpx_codec_cx_pkt_t
*pkt
)
341 if (pkt
->kind
!= VPX_CODEC_CX_FRAME_PKT
)
344 pts
= pkt
->data
.frame
.pts
;
345 mem_put_le32(header
, pkt
->data
.frame
.sz
);
346 mem_put_le32(header
+ 4, pts
& 0xFFFFFFFF);
347 mem_put_le32(header
+ 8, pts
>> 32);
349 fwrite(header
, 1, 12, outfile
);
354 static const arg_def_t use_yv12
= ARG_DEF(NULL
, "yv12", 0,
355 "Input file is YV12 ");
356 static const arg_def_t use_i420
= ARG_DEF(NULL
, "i420", 0,
357 "Input file is I420 (default)");
358 static const arg_def_t codecarg
= ARG_DEF(NULL
, "codec", 1,
360 static const arg_def_t passes
= ARG_DEF("p", "passes", 1,
361 "Number of passes (1/2)");
362 static const arg_def_t pass_arg
= ARG_DEF(NULL
, "pass", 1,
363 "Pass to execute (1/2)");
364 static const arg_def_t fpf_name
= ARG_DEF(NULL
, "fpf", 1,
365 "First pass statistics file name");
366 static const arg_def_t limit
= ARG_DEF(NULL
, "limit", 1,
367 "Stop encoding after n input frames");
368 static const arg_def_t deadline
= ARG_DEF("d", "deadline", 1,
369 "Deadline per frame (usec)");
370 static const arg_def_t best_dl
= ARG_DEF(NULL
, "best", 0,
371 "Use Best Quality Deadline");
372 static const arg_def_t good_dl
= ARG_DEF(NULL
, "good", 0,
373 "Use Good Quality Deadline");
374 static const arg_def_t rt_dl
= ARG_DEF(NULL
, "rt", 0,
375 "Use Realtime Quality Deadline");
376 static const arg_def_t verbosearg
= ARG_DEF("v", "verbose", 0,
377 "Show encoder parameters");
378 static const arg_def_t psnrarg
= ARG_DEF(NULL
, "psnr", 0,
379 "Show PSNR in status line");
380 static const arg_def_t
*main_args
[] =
382 &codecarg
, &passes
, &pass_arg
, &fpf_name
, &limit
, &deadline
, &best_dl
, &good_dl
, &rt_dl
,
383 &verbosearg
, &psnrarg
,
387 static const arg_def_t usage
= ARG_DEF("u", "usage", 1,
388 "Usage profile number to use");
389 static const arg_def_t threads
= ARG_DEF("t", "threads", 1,
390 "Max number of threads to use");
391 static const arg_def_t profile
= ARG_DEF(NULL
, "profile", 1,
392 "Bitstream profile number to use");
393 static const arg_def_t width
= ARG_DEF("w", "width", 1,
395 static const arg_def_t height
= ARG_DEF("h", "height", 1,
397 static const arg_def_t timebase
= ARG_DEF(NULL
, "timebase", 1,
398 "Stream timebase (frame duration)");
399 static const arg_def_t error_resilient
= ARG_DEF(NULL
, "error-resilient", 1,
400 "Enable error resiliency features");
401 static const arg_def_t lag_in_frames
= ARG_DEF(NULL
, "lag-in-frames", 1,
402 "Max number of frames to lag");
404 static const arg_def_t
*global_args
[] =
406 &use_yv12
, &use_i420
, &usage
, &threads
, &profile
,
407 &width
, &height
, &timebase
, &error_resilient
,
411 static const arg_def_t dropframe_thresh
= ARG_DEF(NULL
, "drop-frame", 1,
412 "Temporal resampling threshold (buf %)");
413 static const arg_def_t resize_allowed
= ARG_DEF(NULL
, "resize-allowed", 1,
414 "Spatial resampling enabled (bool)");
415 static const arg_def_t resize_up_thresh
= ARG_DEF(NULL
, "resize-up", 1,
416 "Upscale threshold (buf %)");
417 static const arg_def_t resize_down_thresh
= ARG_DEF(NULL
, "resize-down", 1,
418 "Downscale threshold (buf %)");
419 static const arg_def_t end_usage
= ARG_DEF(NULL
, "end-usage", 1,
421 static const arg_def_t target_bitrate
= ARG_DEF(NULL
, "target-bitrate", 1,
423 static const arg_def_t min_quantizer
= ARG_DEF(NULL
, "min-q", 1,
424 "Minimum (best) quantizer");
425 static const arg_def_t max_quantizer
= ARG_DEF(NULL
, "max-q", 1,
426 "Maximum (worst) quantizer");
427 static const arg_def_t undershoot_pct
= ARG_DEF(NULL
, "undershoot-pct", 1,
428 "Datarate undershoot (min) target (%)");
429 static const arg_def_t overshoot_pct
= ARG_DEF(NULL
, "overshoot-pct", 1,
430 "Datarate overshoot (max) target (%)");
431 static const arg_def_t buf_sz
= ARG_DEF(NULL
, "buf-sz", 1,
432 "Client buffer size (ms)");
433 static const arg_def_t buf_initial_sz
= ARG_DEF(NULL
, "buf-initial-sz", 1,
434 "Client initial buffer size (ms)");
435 static const arg_def_t buf_optimal_sz
= ARG_DEF(NULL
, "buf-optimal-sz", 1,
436 "Client optimal buffer size (ms)");
437 static const arg_def_t
*rc_args
[] =
439 &dropframe_thresh
, &resize_allowed
, &resize_up_thresh
, &resize_down_thresh
,
440 &end_usage
, &target_bitrate
, &min_quantizer
, &max_quantizer
,
441 &undershoot_pct
, &overshoot_pct
, &buf_sz
, &buf_initial_sz
, &buf_optimal_sz
,
446 static const arg_def_t bias_pct
= ARG_DEF(NULL
, "bias-pct", 1,
447 "CBR/VBR bias (0=CBR, 100=VBR)");
448 static const arg_def_t minsection_pct
= ARG_DEF(NULL
, "minsection-pct", 1,
449 "GOP min bitrate (% of target)");
450 static const arg_def_t maxsection_pct
= ARG_DEF(NULL
, "maxsection-pct", 1,
451 "GOP max bitrate (% of target)");
452 static const arg_def_t
*rc_twopass_args
[] =
454 &bias_pct
, &minsection_pct
, &maxsection_pct
, NULL
458 static const arg_def_t kf_min_dist
= ARG_DEF(NULL
, "kf-min-dist", 1,
459 "Minimum keyframe interval (frames)");
460 static const arg_def_t kf_max_dist
= ARG_DEF(NULL
, "kf-max-dist", 1,
461 "Maximum keyframe interval (frames)");
462 static const arg_def_t
*kf_args
[] =
464 &kf_min_dist
, &kf_max_dist
, NULL
468 #if CONFIG_VP8_ENCODER
469 static const arg_def_t noise_sens
= ARG_DEF(NULL
, "noise-sensitivity", 1,
470 "Noise sensitivity (frames to blur)");
471 static const arg_def_t sharpness
= ARG_DEF(NULL
, "sharpness", 1,
472 "Filter sharpness (0-7)");
473 static const arg_def_t static_thresh
= ARG_DEF(NULL
, "static-thresh", 1,
474 "Motion detection threshold");
477 #if CONFIG_VP8_ENCODER
478 static const arg_def_t cpu_used
= ARG_DEF(NULL
, "cpu-used", 1,
479 "CPU Used (-16..16)");
483 #if CONFIG_VP8_ENCODER
484 static const arg_def_t token_parts
= ARG_DEF(NULL
, "token-parts", 1,
485 "Number of token partitions to use, log2");
486 static const arg_def_t auto_altref
= ARG_DEF(NULL
, "auto-alt-ref", 1,
487 "Enable automatic alt reference frames");
488 static const arg_def_t arnr_maxframes
= ARG_DEF(NULL
, "arnr-maxframes", 1,
489 "alt_ref Max Frames");
490 static const arg_def_t arnr_strength
= ARG_DEF(NULL
, "arnr-strength", 1,
492 static const arg_def_t arnr_type
= ARG_DEF(NULL
, "arnr-type", 1,
495 static const arg_def_t
*vp8_args
[] =
497 &cpu_used
, &auto_altref
, &noise_sens
, &sharpness
, &static_thresh
,
498 &token_parts
, &arnr_maxframes
, &arnr_strength
, &arnr_type
, NULL
500 static const int vp8_arg_ctrl_map
[] =
502 VP8E_SET_CPUUSED
, VP8E_SET_ENABLEAUTOALTREF
,
503 VP8E_SET_NOISE_SENSITIVITY
, VP8E_SET_SHARPNESS
, VP8E_SET_STATIC_THRESHOLD
,
504 VP8E_SET_TOKEN_PARTITIONS
,
505 VP8E_SET_ARNR_MAXFRAMES
, VP8E_SET_ARNR_STRENGTH
, VP8E_SET_ARNR_TYPE
, 0
509 static const arg_def_t
*no_args
[] = { NULL
};
511 static void usage_exit()
515 printf("Usage: %s <options> src_filename dst_filename\n", exec_name
);
517 printf("\n_options:\n");
518 arg_show_usage(stdout
, main_args
);
519 printf("\n_encoder Global Options:\n");
520 arg_show_usage(stdout
, global_args
);
521 printf("\n_rate Control Options:\n");
522 arg_show_usage(stdout
, rc_args
);
523 printf("\n_twopass Rate Control Options:\n");
524 arg_show_usage(stdout
, rc_twopass_args
);
525 printf("\n_keyframe Placement Options:\n");
526 arg_show_usage(stdout
, kf_args
);
527 #if CONFIG_VP8_ENCODER
528 printf("\n_vp8 Specific Options:\n");
529 arg_show_usage(stdout
, vp8_args
);
532 "Included encoders:\n"
535 for (i
= 0; i
< sizeof(codecs
) / sizeof(codecs
[0]); i
++)
536 printf(" %-6s - %s\n",
538 vpx_codec_iface_name(codecs
[i
].iface
));
543 #define ARG_CTRL_CNT_MAX 10
546 int main(int argc
, const char **argv_
)
548 vpx_codec_ctx_t encoder
;
549 const char *in_fn
= NULL
, *out_fn
= NULL
, *stats_fn
= NULL
;
551 FILE *infile
, *outfile
;
552 vpx_codec_enc_cfg_t cfg
;
554 int pass
, one_pass_only
= 0;
557 const struct codec_item
*codec
= codecs
;
558 int frame_avail
, got_data
;
561 char **argv
, **argi
, **argj
;
562 int arg_usage
= 0, arg_passes
= 1, arg_deadline
= 0;
563 int arg_ctrls
[ARG_CTRL_CNT_MAX
][2], arg_ctrl_cnt
= 0;
565 static const arg_def_t
**ctrl_args
= no_args
;
566 static const int *ctrl_args_map
= NULL
;
567 int verbose
= 0, show_psnr
= 0;
568 int arg_use_i420
= 1;
569 unsigned long cx_time
= 0;
570 unsigned int is_ivf
, fourcc
;
572 exec_name
= argv_
[0];
578 /* First parse the codec and usage values, because we want to apply other
579 * parameters on top of the default configuration provided by the codec.
581 argv
= argv_dup(argc
- 1, argv_
+ 1);
583 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
)
587 if (arg_match(&arg
, &codecarg
, argi
))
591 for (j
= 0; j
< sizeof(codecs
) / sizeof(codecs
[0]); j
++)
592 if (!strcmp(codecs
[j
].name
, arg
.val
))
598 die("Error: Unrecognized argument (%s) to --codec\n",
602 else if (arg_match(&arg
, &passes
, argi
))
604 arg_passes
= arg_parse_uint(&arg
);
606 if (arg_passes
< 1 || arg_passes
> 2)
607 die("Error: Invalid number of passes (%d)\n", arg_passes
);
609 else if (arg_match(&arg
, &pass_arg
, argi
))
611 one_pass_only
= arg_parse_uint(&arg
);
613 if (one_pass_only
< 1 || one_pass_only
> 2)
614 die("Error: Invalid pass selected (%d)\n", one_pass_only
);
616 else if (arg_match(&arg
, &fpf_name
, argi
))
618 else if (arg_match(&arg
, &usage
, argi
))
619 arg_usage
= arg_parse_uint(&arg
);
620 else if (arg_match(&arg
, &deadline
, argi
))
621 arg_deadline
= arg_parse_uint(&arg
);
622 else if (arg_match(&arg
, &best_dl
, argi
))
623 arg_deadline
= VPX_DL_BEST_QUALITY
;
624 else if (arg_match(&arg
, &good_dl
, argi
))
625 arg_deadline
= VPX_DL_GOOD_QUALITY
;
626 else if (arg_match(&arg
, &rt_dl
, argi
))
627 arg_deadline
= VPX_DL_REALTIME
;
628 else if (arg_match(&arg
, &use_yv12
, argi
))
632 else if (arg_match(&arg
, &use_i420
, argi
))
636 else if (arg_match(&arg
, &verbosearg
, argi
))
638 else if (arg_match(&arg
, &limit
, argi
))
639 arg_limit
= arg_parse_uint(&arg
);
640 else if (arg_match(&arg
, &psnrarg
, argi
))
646 /* Ensure that --passes and --pass are consistent. If --pass is set and --passes=2,
647 * ensure --fpf was set.
651 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
652 if (one_pass_only
> arg_passes
)
654 printf("Warning: Assuming --pass=%d implies --passes=%d\n",
655 one_pass_only
, one_pass_only
);
656 arg_passes
= one_pass_only
;
659 if (arg_passes
== 2 && !stats_fn
)
660 die("Must specify --fpf when --pass=%d and --passes=2\n", one_pass_only
);
663 /* Populate encoder configuration */
664 res
= vpx_codec_enc_config_default(codec
->iface
, &cfg
, arg_usage
);
668 printf("Failed to get config: %s\n", vpx_codec_err_to_string(res
));
672 /* Now parse the remainder of the parameters. */
673 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
)
678 else if (arg_match(&arg
, &threads
, argi
))
679 cfg
.g_threads
= arg_parse_uint(&arg
);
680 else if (arg_match(&arg
, &profile
, argi
))
681 cfg
.g_profile
= arg_parse_uint(&arg
);
682 else if (arg_match(&arg
, &width
, argi
))
683 cfg
.g_w
= arg_parse_uint(&arg
);
684 else if (arg_match(&arg
, &height
, argi
))
685 cfg
.g_h
= arg_parse_uint(&arg
);
686 else if (arg_match(&arg
, &timebase
, argi
))
687 cfg
.g_timebase
= arg_parse_rational(&arg
);
688 else if (arg_match(&arg
, &error_resilient
, argi
))
689 cfg
.g_error_resilient
= arg_parse_uint(&arg
);
690 else if (arg_match(&arg
, &lag_in_frames
, argi
))
691 cfg
.g_lag_in_frames
= arg_parse_uint(&arg
);
692 else if (arg_match(&arg
, &dropframe_thresh
, argi
))
693 cfg
.rc_dropframe_thresh
= arg_parse_uint(&arg
);
694 else if (arg_match(&arg
, &resize_allowed
, argi
))
695 cfg
.rc_resize_allowed
= arg_parse_uint(&arg
);
696 else if (arg_match(&arg
, &resize_up_thresh
, argi
))
697 cfg
.rc_resize_up_thresh
= arg_parse_uint(&arg
);
698 else if (arg_match(&arg
, &resize_down_thresh
, argi
))
699 cfg
.rc_resize_down_thresh
= arg_parse_uint(&arg
);
700 else if (arg_match(&arg
, &resize_down_thresh
, argi
))
701 cfg
.rc_resize_down_thresh
= arg_parse_uint(&arg
);
702 else if (arg_match(&arg
, &end_usage
, argi
))
703 cfg
.rc_end_usage
= arg_parse_uint(&arg
);
704 else if (arg_match(&arg
, &target_bitrate
, argi
))
705 cfg
.rc_target_bitrate
= arg_parse_uint(&arg
);
706 else if (arg_match(&arg
, &min_quantizer
, argi
))
707 cfg
.rc_min_quantizer
= arg_parse_uint(&arg
);
708 else if (arg_match(&arg
, &max_quantizer
, argi
))
709 cfg
.rc_max_quantizer
= arg_parse_uint(&arg
);
710 else if (arg_match(&arg
, &undershoot_pct
, argi
))
711 cfg
.rc_undershoot_pct
= arg_parse_uint(&arg
);
712 else if (arg_match(&arg
, &overshoot_pct
, argi
))
713 cfg
.rc_overshoot_pct
= arg_parse_uint(&arg
);
714 else if (arg_match(&arg
, &buf_sz
, argi
))
715 cfg
.rc_buf_sz
= arg_parse_uint(&arg
);
716 else if (arg_match(&arg
, &buf_initial_sz
, argi
))
717 cfg
.rc_buf_initial_sz
= arg_parse_uint(&arg
);
718 else if (arg_match(&arg
, &buf_optimal_sz
, argi
))
719 cfg
.rc_buf_optimal_sz
= arg_parse_uint(&arg
);
720 else if (arg_match(&arg
, &bias_pct
, argi
))
722 cfg
.rc_2pass_vbr_bias_pct
= arg_parse_uint(&arg
);
725 printf("Warning: option %s ignored in one-pass mode.\n",
728 else if (arg_match(&arg
, &minsection_pct
, argi
))
730 cfg
.rc_2pass_vbr_minsection_pct
= arg_parse_uint(&arg
);
733 printf("Warning: option %s ignored in one-pass mode.\n",
736 else if (arg_match(&arg
, &maxsection_pct
, argi
))
738 cfg
.rc_2pass_vbr_maxsection_pct
= arg_parse_uint(&arg
);
741 printf("Warning: option %s ignored in one-pass mode.\n",
744 else if (arg_match(&arg
, &kf_min_dist
, argi
))
745 cfg
.kf_min_dist
= arg_parse_uint(&arg
);
746 else if (arg_match(&arg
, &kf_max_dist
, argi
))
747 cfg
.kf_max_dist
= arg_parse_uint(&arg
);
752 /* Handle codec specific options */
753 #if CONFIG_VP8_ENCODER
755 if (codec
->iface
== &vpx_codec_vp8_cx_algo
)
757 ctrl_args
= vp8_args
;
758 ctrl_args_map
= vp8_arg_ctrl_map
;
763 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
)
769 for (i
= 0; ctrl_args
[i
]; i
++)
771 if (arg_match(&arg
, ctrl_args
[i
], argi
))
775 if (arg_ctrl_cnt
< ARG_CTRL_CNT_MAX
)
777 arg_ctrls
[arg_ctrl_cnt
][0] = ctrl_args_map
[i
];
778 arg_ctrls
[arg_ctrl_cnt
][1] = arg_parse_int(&arg
);
788 /* Check for unrecognized options */
789 for (argi
= argv
; *argi
; argi
++)
790 if (argi
[0][0] == '-')
791 die("Error: Unrecognized option %s\n", *argi
);
793 /* Handle non-option arguments */
797 if (!in_fn
|| !out_fn
)
800 /* Parse certain options from the input file, if possible */
801 infile
= fopen(in_fn
, "rb");
805 printf("Failed to open input file");
809 is_ivf
= file_is_ivf(infile
, &fourcc
, &cfg
.g_w
, &cfg
.g_h
);
822 printf("Unsupported fourcc (%08x) in IVF\n", fourcc
);
830 #define SHOW(field) printf(" %-28s = %d\n", #field, cfg.field)
834 printf("Codec: %s\n", vpx_codec_iface_name(codec
->iface
));
835 printf("Source file: %s Format: %s\n", in_fn
, arg_use_i420
? "I420" : "YV12");
836 printf("Destination file: %s\n", out_fn
);
837 printf("Encoder parameters:\n");
844 SHOW(g_timebase
.num
);
845 SHOW(g_timebase
.den
);
846 SHOW(g_error_resilient
);
848 SHOW(g_lag_in_frames
);
849 SHOW(rc_dropframe_thresh
);
850 SHOW(rc_resize_allowed
);
851 SHOW(rc_resize_up_thresh
);
852 SHOW(rc_resize_down_thresh
);
854 SHOW(rc_target_bitrate
);
855 SHOW(rc_min_quantizer
);
856 SHOW(rc_max_quantizer
);
857 SHOW(rc_undershoot_pct
);
858 SHOW(rc_overshoot_pct
);
860 SHOW(rc_buf_initial_sz
);
861 SHOW(rc_buf_optimal_sz
);
862 SHOW(rc_2pass_vbr_bias_pct
);
863 SHOW(rc_2pass_vbr_minsection_pct
);
864 SHOW(rc_2pass_vbr_maxsection_pct
);
870 vpx_img_alloc(&raw
, arg_use_i420
? IMG_FMT_I420
: IMG_FMT_YV12
,
871 cfg
.g_w
, cfg
.g_h
, 1);
873 // This was added so that ivfenc will create monotically increasing
874 // timestamps. Since we create new timestamps for alt-reference frames
875 // we need to make room in the series of timestamps. Since there can
876 // only be 1 alt-ref frame ( current bitstream) multiplying by 2
877 // gives us enough room.
878 cfg
.g_timebase
.den
*= 2;
880 memset(&stats
, 0, sizeof(stats
));
882 for (pass
= one_pass_only
? one_pass_only
- 1 : 0; pass
< arg_passes
; pass
++)
884 int frames_in
= 0, frames_out
= 0;
885 unsigned long nbytes
= 0;
887 infile
= fopen(in_fn
, "rb");
891 printf("Failed to open input file");
895 outfile
= fopen(out_fn
, "wb");
899 printf("Failed to open output file");
905 if (!stats_open_file(&stats
, stats_fn
, pass
))
907 printf("Failed to open statistics store\n");
913 if (!stats_open_mem(&stats
, pass
))
915 printf("Failed to open statistics store\n");
920 cfg
.g_pass
= arg_passes
== 2
921 ? pass
? VPX_RC_LAST_PASS
: VPX_RC_FIRST_PASS
923 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
927 cfg
.rc_twopass_stats_in
= stats_get(&stats
);
932 write_ivf_file_header(outfile
, &cfg
, codec
->fourcc
, 0);
935 /* Construct Encoder Context */
936 if (cfg
.kf_min_dist
== cfg
.kf_max_dist
)
937 cfg
.kf_mode
= VPX_KF_FIXED
;
939 vpx_codec_enc_init(&encoder
, codec
->iface
, &cfg
,
940 show_psnr
? VPX_CODEC_USE_PSNR
: 0);
941 ctx_exit_on_error(&encoder
, "Failed to initialize encoder");
943 /* Note that we bypass the vpx_codec_control wrapper macro because
944 * we're being clever to store the control IDs in an array. Real
945 * applications will want to make use of the enumerations directly
947 for (i
= 0; i
< arg_ctrl_cnt
; i
++)
949 if (vpx_codec_control_(&encoder
, arg_ctrls
[i
][0], arg_ctrls
[i
][1]))
950 printf("Error: Tried to set control %d = %d\n",
951 arg_ctrls
[i
][0], arg_ctrls
[i
][1]);
953 ctx_exit_on_error(&encoder
, "Failed to control codec");
959 while (frame_avail
|| got_data
)
961 vpx_codec_iter_t iter
= NULL
;
962 const vpx_codec_cx_pkt_t
*pkt
;
963 struct vpx_usec_timer timer
;
965 if (!arg_limit
|| frames_in
< arg_limit
)
967 frame_avail
= read_frame(infile
, &raw
, is_ivf
);
972 printf("\rPass %d/%d frame %4d/%-4d %7ldB \033[K", pass
+ 1,
973 arg_passes
, frames_in
, frames_out
, nbytes
);
978 vpx_usec_timer_start(&timer
);
980 // since we halved our timebase we need to double the timestamps
981 // and duration we pass in.
982 vpx_codec_encode(&encoder
, frame_avail
? &raw
: NULL
, (frames_in
- 1) * 2,
984 vpx_usec_timer_mark(&timer
);
985 cx_time
+= vpx_usec_timer_elapsed(&timer
);
986 ctx_exit_on_error(&encoder
, "Failed to encode frame");
989 while ((pkt
= vpx_codec_get_cx_data(&encoder
, &iter
)))
992 nbytes
+= pkt
->data
.raw
.sz
;
996 case VPX_CODEC_CX_FRAME_PKT
:
999 (unsigned long)pkt
->data
.frame
.sz
);
1000 write_ivf_frame_header(outfile
, pkt
);
1001 fwrite(pkt
->data
.frame
.buf
, 1, pkt
->data
.frame
.sz
, outfile
);
1003 case VPX_CODEC_STATS_PKT
:
1006 (unsigned long)pkt
->data
.twopass_stats
.sz
);
1008 pkt
->data
.twopass_stats
.buf
,
1009 pkt
->data
.twopass_stats
.sz
);
1011 case VPX_CODEC_PSNR_PKT
:
1017 for (i
= 0; i
< 4; i
++)
1018 printf("%.3lf ", pkt
->data
.psnr
.psnr
[i
]);
1030 /* this bitrate calc is simplified and relies on the fact that this
1031 * application uses 1/timebase for framerate.
1033 printf("\rPass %d/%d frame %4d/%-4d %7ldB %7ldb/f %7"PRId64
"b/s"
1034 " %7lu %s (%.2f fps)\033[K", pass
+ 1,
1035 arg_passes
, frames_in
, frames_out
, nbytes
, nbytes
* 8 / frames_in
,
1036 nbytes
* 8 *(int64_t)cfg
.g_timebase
.den
/ cfg
.g_timebase
.num
/ frames_in
,
1037 cx_time
> 9999999 ? cx_time
/ 1000 : cx_time
,
1038 cx_time
> 9999999 ? "ms" : "us",
1039 (float)frames_in
* 1000000.0 / (float)cx_time
);
1041 vpx_codec_destroy(&encoder
);
1045 if (!fseek(outfile
, 0, SEEK_SET
))
1046 write_ivf_file_header(outfile
, &cfg
, codec
->fourcc
, frames_out
);
1049 stats_close(&stats
);
1058 return EXIT_SUCCESS
;