2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
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.
12 /* This is a simple program that encodes YV12 files and generates ivf
13 * files using the new interface.
16 #define USE_POSIX_MMAP 0
18 #define USE_POSIX_MMAP 1
25 #include "vpx/vpx_encoder.h"
27 #include <sys/types.h>
33 #include "vpx/vp8cx.h"
34 #include "vpx_ports/mem_ops.h"
35 #include "vpx_ports/vpx_timer.h"
38 static const char *exec_name
;
40 static const struct codec_item
43 const vpx_codec_iface_t
*iface
;
47 #if CONFIG_VP8_ENCODER
48 {"vp8", &vpx_codec_vp8_cx_algo
, 0x30385056},
52 static void usage_exit();
54 void die(const char *fmt
, ...)
58 vfprintf(stderr
, fmt
, ap
);
59 fprintf(stderr
, "\n");
63 static void ctx_exit_on_error(vpx_codec_ctx_t
*ctx
, const char *s
)
67 const char *detail
= vpx_codec_error_detail(ctx
);
69 fprintf(stderr
, "%s: %s\n", s
, vpx_codec_error(ctx
));
72 fprintf(stderr
, " %s\n", detail
);
78 /* This structure is used to abstract the different ways of handling
79 * first pass statistics.
90 int stats_open_file(stats_io_t
*stats
, const char *fpf
, int pass
)
98 stats
->file
= fopen(fpf
, "wb");
100 stats
->buf
.buf
= NULL
,
101 res
= (stats
->file
!= NULL
);
107 struct stat stat_buf
;
110 fd
= open(fpf
, O_RDONLY
);
111 stats
->file
= fdopen(fd
, "rb");
112 fstat(fd
, &stat_buf
);
113 stats
->buf
.sz
= stat_buf
.st_size
;
114 stats
->buf
.buf
= mmap(NULL
, stats
->buf
.sz
, PROT_READ
, MAP_PRIVATE
,
116 res
= (stats
->buf
.buf
!= NULL
);
120 stats
->file
= fopen(fpf
, "rb");
122 if (fseek(stats
->file
, 0, SEEK_END
))
124 fprintf(stderr
, "First-pass stats file must be seekable!\n");
128 stats
->buf
.sz
= stats
->buf_alloc_sz
= ftell(stats
->file
);
131 stats
->buf
.buf
= malloc(stats
->buf_alloc_sz
);
135 fprintf(stderr
, "Failed to allocate first-pass stats buffer (%d bytes)\n",
136 stats
->buf_alloc_sz
);
140 nbytes
= fread(stats
->buf
.buf
, 1, stats
->buf
.sz
, stats
->file
);
141 res
= (nbytes
== stats
->buf
.sz
);
148 int stats_open_mem(stats_io_t
*stats
, int pass
)
156 stats
->buf_alloc_sz
= 64 * 1024;
157 stats
->buf
.buf
= malloc(stats
->buf_alloc_sz
);
160 stats
->buf_ptr
= stats
->buf
.buf
;
161 res
= (stats
->buf
.buf
!= NULL
);
166 void stats_close(stats_io_t
*stats
)
170 if (stats
->pass
== 1)
174 munmap(stats
->buf
.buf
, stats
->buf
.sz
);
176 free(stats
->buf
.buf
);
185 if (stats
->pass
== 1)
186 free(stats
->buf
.buf
);
190 void stats_write(stats_io_t
*stats
, const void *pkt
, size_t len
)
194 fwrite(pkt
, 1, len
, stats
->file
);
198 if (stats
->buf
.sz
+ len
> stats
->buf_alloc_sz
)
200 size_t new_sz
= stats
->buf_alloc_sz
+ 64 * 1024;
201 char *new_ptr
= realloc(stats
->buf
.buf
, new_sz
);
205 stats
->buf_ptr
= new_ptr
+ (stats
->buf_ptr
- (char *)stats
->buf
.buf
);
206 stats
->buf
.buf
= new_ptr
;
207 stats
->buf_alloc_sz
= new_sz
;
211 memcpy(stats
->buf_ptr
, pkt
, len
);
212 stats
->buf
.sz
+= len
;
213 stats
->buf_ptr
+= len
;
217 vpx_fixed_buf_t
stats_get(stats_io_t
*stats
)
229 struct detect_buffer
{
235 #define IVF_FRAME_HDR_SZ (4+8) /* 4 byte size + 8 byte timestamp */
236 static int read_frame(FILE *f
, vpx_image_t
*img
, unsigned int file_type
,
237 y4m_input
*y4m
, struct detect_buffer
*detect
)
241 if (file_type
== FILE_TYPE_Y4M
)
243 if (y4m_input_fetch_frame(y4m
, f
, img
) < 0)
248 if (file_type
== FILE_TYPE_IVF
)
250 char junk
[IVF_FRAME_HDR_SZ
];
252 /* Skip the frame header. We know how big the frame should be. See
253 * write_ivf_frame_header() for documentation on the frame header
256 fread(junk
, 1, IVF_FRAME_HDR_SZ
, f
);
259 for (plane
= 0; plane
< 3; plane
++)
262 int w
= (plane
? (1 + img
->d_w
) / 2 : img
->d_w
);
263 int h
= (plane
? (1 + img
->d_h
) / 2 : img
->d_h
);
266 /* Determine the correct plane based on the image format. The for-loop
267 * always counts in Y,U,V order, but this may not match the order of
273 ptr
= img
->planes
[img
->fmt
==VPX_IMG_FMT_YV12
? VPX_PLANE_V
: VPX_PLANE_U
];
276 ptr
= img
->planes
[img
->fmt
==VPX_IMG_FMT_YV12
?VPX_PLANE_U
: VPX_PLANE_V
];
279 ptr
= img
->planes
[plane
];
282 for (r
= 0; r
< h
; r
++)
286 memcpy(ptr
, detect
->buf
, 4);
287 fread(ptr
+4, 1, w
-4, f
);
293 ptr
+= img
->stride
[plane
];
302 unsigned int file_is_y4m(FILE *infile
,
306 if(memcmp(detect
, "YUV4", 4) == 0)
313 #define IVF_FILE_HDR_SZ (32)
314 unsigned int file_is_ivf(FILE *infile
,
315 unsigned int *fourcc
,
317 unsigned int *height
,
320 char raw_hdr
[IVF_FILE_HDR_SZ
];
323 if(memcmp(detect
, "DKIF", 4) != 0)
326 /* See write_ivf_file_header() for more documentation on the file header
329 if (fread(raw_hdr
+ 4, 1, IVF_FILE_HDR_SZ
- 4, infile
)
330 == IVF_FILE_HDR_SZ
- 4)
335 if (mem_get_le16(raw_hdr
+ 4) != 0)
336 fprintf(stderr
, "Error: Unrecognized IVF version! This file may not"
337 " decode properly.");
339 *fourcc
= mem_get_le32(raw_hdr
+ 8);
345 *width
= mem_get_le16(raw_hdr
+ 12);
346 *height
= mem_get_le16(raw_hdr
+ 14);
353 static void write_ivf_file_header(FILE *outfile
,
354 const vpx_codec_enc_cfg_t
*cfg
,
360 if (cfg
->g_pass
!= VPX_RC_ONE_PASS
&& cfg
->g_pass
!= VPX_RC_LAST_PASS
)
367 mem_put_le16(header
+ 4, 0); /* version */
368 mem_put_le16(header
+ 6, 32); /* headersize */
369 mem_put_le32(header
+ 8, fourcc
); /* headersize */
370 mem_put_le16(header
+ 12, cfg
->g_w
); /* width */
371 mem_put_le16(header
+ 14, cfg
->g_h
); /* height */
372 mem_put_le32(header
+ 16, cfg
->g_timebase
.den
); /* rate */
373 mem_put_le32(header
+ 20, cfg
->g_timebase
.num
); /* scale */
374 mem_put_le32(header
+ 24, frame_cnt
); /* length */
375 mem_put_le32(header
+ 28, 0); /* unused */
377 fwrite(header
, 1, 32, outfile
);
381 static void write_ivf_frame_header(FILE *outfile
,
382 const vpx_codec_cx_pkt_t
*pkt
)
387 if (pkt
->kind
!= VPX_CODEC_CX_FRAME_PKT
)
390 pts
= pkt
->data
.frame
.pts
;
391 mem_put_le32(header
, pkt
->data
.frame
.sz
);
392 mem_put_le32(header
+ 4, pts
& 0xFFFFFFFF);
393 mem_put_le32(header
+ 8, pts
>> 32);
395 fwrite(header
, 1, 12, outfile
);
400 static const arg_def_t use_yv12
= ARG_DEF(NULL
, "yv12", 0,
401 "Input file is YV12 ");
402 static const arg_def_t use_i420
= ARG_DEF(NULL
, "i420", 0,
403 "Input file is I420 (default)");
404 static const arg_def_t codecarg
= ARG_DEF(NULL
, "codec", 1,
406 static const arg_def_t passes
= ARG_DEF("p", "passes", 1,
407 "Number of passes (1/2)");
408 static const arg_def_t pass_arg
= ARG_DEF(NULL
, "pass", 1,
409 "Pass to execute (1/2)");
410 static const arg_def_t fpf_name
= ARG_DEF(NULL
, "fpf", 1,
411 "First pass statistics file name");
412 static const arg_def_t limit
= ARG_DEF(NULL
, "limit", 1,
413 "Stop encoding after n input frames");
414 static const arg_def_t deadline
= ARG_DEF("d", "deadline", 1,
415 "Deadline per frame (usec)");
416 static const arg_def_t best_dl
= ARG_DEF(NULL
, "best", 0,
417 "Use Best Quality Deadline");
418 static const arg_def_t good_dl
= ARG_DEF(NULL
, "good", 0,
419 "Use Good Quality Deadline");
420 static const arg_def_t rt_dl
= ARG_DEF(NULL
, "rt", 0,
421 "Use Realtime Quality Deadline");
422 static const arg_def_t verbosearg
= ARG_DEF("v", "verbose", 0,
423 "Show encoder parameters");
424 static const arg_def_t psnrarg
= ARG_DEF(NULL
, "psnr", 0,
425 "Show PSNR in status line");
426 static const arg_def_t
*main_args
[] =
428 &codecarg
, &passes
, &pass_arg
, &fpf_name
, &limit
, &deadline
, &best_dl
, &good_dl
, &rt_dl
,
429 &verbosearg
, &psnrarg
,
433 static const arg_def_t usage
= ARG_DEF("u", "usage", 1,
434 "Usage profile number to use");
435 static const arg_def_t threads
= ARG_DEF("t", "threads", 1,
436 "Max number of threads to use");
437 static const arg_def_t profile
= ARG_DEF(NULL
, "profile", 1,
438 "Bitstream profile number to use");
439 static const arg_def_t width
= ARG_DEF("w", "width", 1,
441 static const arg_def_t height
= ARG_DEF("h", "height", 1,
443 static const arg_def_t timebase
= ARG_DEF(NULL
, "timebase", 1,
444 "Stream timebase (frame duration)");
445 static const arg_def_t error_resilient
= ARG_DEF(NULL
, "error-resilient", 1,
446 "Enable error resiliency features");
447 static const arg_def_t lag_in_frames
= ARG_DEF(NULL
, "lag-in-frames", 1,
448 "Max number of frames to lag");
450 static const arg_def_t
*global_args
[] =
452 &use_yv12
, &use_i420
, &usage
, &threads
, &profile
,
453 &width
, &height
, &timebase
, &error_resilient
,
457 static const arg_def_t dropframe_thresh
= ARG_DEF(NULL
, "drop-frame", 1,
458 "Temporal resampling threshold (buf %)");
459 static const arg_def_t resize_allowed
= ARG_DEF(NULL
, "resize-allowed", 1,
460 "Spatial resampling enabled (bool)");
461 static const arg_def_t resize_up_thresh
= ARG_DEF(NULL
, "resize-up", 1,
462 "Upscale threshold (buf %)");
463 static const arg_def_t resize_down_thresh
= ARG_DEF(NULL
, "resize-down", 1,
464 "Downscale threshold (buf %)");
465 static const arg_def_t end_usage
= ARG_DEF(NULL
, "end-usage", 1,
467 static const arg_def_t target_bitrate
= ARG_DEF(NULL
, "target-bitrate", 1,
469 static const arg_def_t min_quantizer
= ARG_DEF(NULL
, "min-q", 1,
470 "Minimum (best) quantizer");
471 static const arg_def_t max_quantizer
= ARG_DEF(NULL
, "max-q", 1,
472 "Maximum (worst) quantizer");
473 static const arg_def_t undershoot_pct
= ARG_DEF(NULL
, "undershoot-pct", 1,
474 "Datarate undershoot (min) target (%)");
475 static const arg_def_t overshoot_pct
= ARG_DEF(NULL
, "overshoot-pct", 1,
476 "Datarate overshoot (max) target (%)");
477 static const arg_def_t buf_sz
= ARG_DEF(NULL
, "buf-sz", 1,
478 "Client buffer size (ms)");
479 static const arg_def_t buf_initial_sz
= ARG_DEF(NULL
, "buf-initial-sz", 1,
480 "Client initial buffer size (ms)");
481 static const arg_def_t buf_optimal_sz
= ARG_DEF(NULL
, "buf-optimal-sz", 1,
482 "Client optimal buffer size (ms)");
483 static const arg_def_t
*rc_args
[] =
485 &dropframe_thresh
, &resize_allowed
, &resize_up_thresh
, &resize_down_thresh
,
486 &end_usage
, &target_bitrate
, &min_quantizer
, &max_quantizer
,
487 &undershoot_pct
, &overshoot_pct
, &buf_sz
, &buf_initial_sz
, &buf_optimal_sz
,
492 static const arg_def_t bias_pct
= ARG_DEF(NULL
, "bias-pct", 1,
493 "CBR/VBR bias (0=CBR, 100=VBR)");
494 static const arg_def_t minsection_pct
= ARG_DEF(NULL
, "minsection-pct", 1,
495 "GOP min bitrate (% of target)");
496 static const arg_def_t maxsection_pct
= ARG_DEF(NULL
, "maxsection-pct", 1,
497 "GOP max bitrate (% of target)");
498 static const arg_def_t
*rc_twopass_args
[] =
500 &bias_pct
, &minsection_pct
, &maxsection_pct
, NULL
504 static const arg_def_t kf_min_dist
= ARG_DEF(NULL
, "kf-min-dist", 1,
505 "Minimum keyframe interval (frames)");
506 static const arg_def_t kf_max_dist
= ARG_DEF(NULL
, "kf-max-dist", 1,
507 "Maximum keyframe interval (frames)");
508 static const arg_def_t kf_disabled
= ARG_DEF(NULL
, "disable-kf", 0,
509 "Disable keyframe placement");
510 static const arg_def_t
*kf_args
[] =
512 &kf_min_dist
, &kf_max_dist
, &kf_disabled
, NULL
516 #if CONFIG_VP8_ENCODER
517 static const arg_def_t noise_sens
= ARG_DEF(NULL
, "noise-sensitivity", 1,
518 "Noise sensitivity (frames to blur)");
519 static const arg_def_t sharpness
= ARG_DEF(NULL
, "sharpness", 1,
520 "Filter sharpness (0-7)");
521 static const arg_def_t static_thresh
= ARG_DEF(NULL
, "static-thresh", 1,
522 "Motion detection threshold");
525 #if CONFIG_VP8_ENCODER
526 static const arg_def_t cpu_used
= ARG_DEF(NULL
, "cpu-used", 1,
527 "CPU Used (-16..16)");
531 #if CONFIG_VP8_ENCODER
532 static const arg_def_t token_parts
= ARG_DEF(NULL
, "token-parts", 1,
533 "Number of token partitions to use, log2");
534 static const arg_def_t auto_altref
= ARG_DEF(NULL
, "auto-alt-ref", 1,
535 "Enable automatic alt reference frames");
536 static const arg_def_t arnr_maxframes
= ARG_DEF(NULL
, "arnr-maxframes", 1,
537 "alt_ref Max Frames");
538 static const arg_def_t arnr_strength
= ARG_DEF(NULL
, "arnr-strength", 1,
540 static const arg_def_t arnr_type
= ARG_DEF(NULL
, "arnr-type", 1,
543 static const arg_def_t
*vp8_args
[] =
545 &cpu_used
, &auto_altref
, &noise_sens
, &sharpness
, &static_thresh
,
546 &token_parts
, &arnr_maxframes
, &arnr_strength
, &arnr_type
, NULL
548 static const int vp8_arg_ctrl_map
[] =
550 VP8E_SET_CPUUSED
, VP8E_SET_ENABLEAUTOALTREF
,
551 VP8E_SET_NOISE_SENSITIVITY
, VP8E_SET_SHARPNESS
, VP8E_SET_STATIC_THRESHOLD
,
552 VP8E_SET_TOKEN_PARTITIONS
,
553 VP8E_SET_ARNR_MAXFRAMES
, VP8E_SET_ARNR_STRENGTH
, VP8E_SET_ARNR_TYPE
, 0
557 static const arg_def_t
*no_args
[] = { NULL
};
559 static void usage_exit()
563 fprintf(stderr
, "Usage: %s <options> src_filename dst_filename\n", exec_name
);
565 fprintf(stderr
, "\n_options:\n");
566 arg_show_usage(stdout
, main_args
);
567 fprintf(stderr
, "\n_encoder Global Options:\n");
568 arg_show_usage(stdout
, global_args
);
569 fprintf(stderr
, "\n_rate Control Options:\n");
570 arg_show_usage(stdout
, rc_args
);
571 fprintf(stderr
, "\n_twopass Rate Control Options:\n");
572 arg_show_usage(stdout
, rc_twopass_args
);
573 fprintf(stderr
, "\n_keyframe Placement Options:\n");
574 arg_show_usage(stdout
, kf_args
);
575 #if CONFIG_VP8_ENCODER
576 fprintf(stderr
, "\n_vp8 Specific Options:\n");
577 arg_show_usage(stdout
, vp8_args
);
580 "Included encoders:\n"
583 for (i
= 0; i
< sizeof(codecs
) / sizeof(codecs
[0]); i
++)
584 fprintf(stderr
, " %-6s - %s\n",
586 vpx_codec_iface_name(codecs
[i
].iface
));
591 #define ARG_CTRL_CNT_MAX 10
594 int main(int argc
, const char **argv_
)
596 vpx_codec_ctx_t encoder
;
597 const char *in_fn
= NULL
, *out_fn
= NULL
, *stats_fn
= NULL
;
599 FILE *infile
, *outfile
;
600 vpx_codec_enc_cfg_t cfg
;
602 int pass
, one_pass_only
= 0;
605 const struct codec_item
*codec
= codecs
;
606 int frame_avail
, got_data
;
609 char **argv
, **argi
, **argj
;
610 int arg_usage
= 0, arg_passes
= 1, arg_deadline
= 0;
611 int arg_ctrls
[ARG_CTRL_CNT_MAX
][2], arg_ctrl_cnt
= 0;
613 static const arg_def_t
**ctrl_args
= no_args
;
614 static const int *ctrl_args_map
= NULL
;
615 int verbose
= 0, show_psnr
= 0;
616 int arg_use_i420
= 1;
617 int arg_have_timebase
= 0;
618 unsigned long cx_time
= 0;
619 unsigned int file_type
, fourcc
;
622 exec_name
= argv_
[0];
628 /* First parse the codec and usage values, because we want to apply other
629 * parameters on top of the default configuration provided by the codec.
631 argv
= argv_dup(argc
- 1, argv_
+ 1);
633 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
)
637 if (arg_match(&arg
, &codecarg
, argi
))
641 for (j
= 0; j
< sizeof(codecs
) / sizeof(codecs
[0]); j
++)
642 if (!strcmp(codecs
[j
].name
, arg
.val
))
648 die("Error: Unrecognized argument (%s) to --codec\n",
652 else if (arg_match(&arg
, &passes
, argi
))
654 arg_passes
= arg_parse_uint(&arg
);
656 if (arg_passes
< 1 || arg_passes
> 2)
657 die("Error: Invalid number of passes (%d)\n", arg_passes
);
659 else if (arg_match(&arg
, &pass_arg
, argi
))
661 one_pass_only
= arg_parse_uint(&arg
);
663 if (one_pass_only
< 1 || one_pass_only
> 2)
664 die("Error: Invalid pass selected (%d)\n", one_pass_only
);
666 else if (arg_match(&arg
, &fpf_name
, argi
))
668 else if (arg_match(&arg
, &usage
, argi
))
669 arg_usage
= arg_parse_uint(&arg
);
670 else if (arg_match(&arg
, &deadline
, argi
))
671 arg_deadline
= arg_parse_uint(&arg
);
672 else if (arg_match(&arg
, &best_dl
, argi
))
673 arg_deadline
= VPX_DL_BEST_QUALITY
;
674 else if (arg_match(&arg
, &good_dl
, argi
))
675 arg_deadline
= VPX_DL_GOOD_QUALITY
;
676 else if (arg_match(&arg
, &rt_dl
, argi
))
677 arg_deadline
= VPX_DL_REALTIME
;
678 else if (arg_match(&arg
, &use_yv12
, argi
))
682 else if (arg_match(&arg
, &use_i420
, argi
))
686 else if (arg_match(&arg
, &verbosearg
, argi
))
688 else if (arg_match(&arg
, &limit
, argi
))
689 arg_limit
= arg_parse_uint(&arg
);
690 else if (arg_match(&arg
, &psnrarg
, argi
))
696 /* Ensure that --passes and --pass are consistent. If --pass is set and --passes=2,
697 * ensure --fpf was set.
701 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
702 if (one_pass_only
> arg_passes
)
704 fprintf(stderr
, "Warning: Assuming --pass=%d implies --passes=%d\n",
705 one_pass_only
, one_pass_only
);
706 arg_passes
= one_pass_only
;
709 if (arg_passes
== 2 && !stats_fn
)
710 die("Must specify --fpf when --pass=%d and --passes=2\n", one_pass_only
);
713 /* Populate encoder configuration */
714 res
= vpx_codec_enc_config_default(codec
->iface
, &cfg
, arg_usage
);
718 fprintf(stderr
, "Failed to get config: %s\n",
719 vpx_codec_err_to_string(res
));
723 /* Now parse the remainder of the parameters. */
724 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
)
729 else if (arg_match(&arg
, &threads
, argi
))
730 cfg
.g_threads
= arg_parse_uint(&arg
);
731 else if (arg_match(&arg
, &profile
, argi
))
732 cfg
.g_profile
= arg_parse_uint(&arg
);
733 else if (arg_match(&arg
, &width
, argi
))
734 cfg
.g_w
= arg_parse_uint(&arg
);
735 else if (arg_match(&arg
, &height
, argi
))
736 cfg
.g_h
= arg_parse_uint(&arg
);
737 else if (arg_match(&arg
, &timebase
, argi
))
739 cfg
.g_timebase
= arg_parse_rational(&arg
);
740 arg_have_timebase
= 1;
742 else if (arg_match(&arg
, &error_resilient
, argi
))
743 cfg
.g_error_resilient
= arg_parse_uint(&arg
);
744 else if (arg_match(&arg
, &lag_in_frames
, argi
))
745 cfg
.g_lag_in_frames
= arg_parse_uint(&arg
);
746 else if (arg_match(&arg
, &dropframe_thresh
, argi
))
747 cfg
.rc_dropframe_thresh
= arg_parse_uint(&arg
);
748 else if (arg_match(&arg
, &resize_allowed
, argi
))
749 cfg
.rc_resize_allowed
= arg_parse_uint(&arg
);
750 else if (arg_match(&arg
, &resize_up_thresh
, argi
))
751 cfg
.rc_resize_up_thresh
= arg_parse_uint(&arg
);
752 else if (arg_match(&arg
, &resize_down_thresh
, argi
))
753 cfg
.rc_resize_down_thresh
= arg_parse_uint(&arg
);
754 else if (arg_match(&arg
, &resize_down_thresh
, argi
))
755 cfg
.rc_resize_down_thresh
= arg_parse_uint(&arg
);
756 else if (arg_match(&arg
, &end_usage
, argi
))
757 cfg
.rc_end_usage
= arg_parse_uint(&arg
);
758 else if (arg_match(&arg
, &target_bitrate
, argi
))
759 cfg
.rc_target_bitrate
= arg_parse_uint(&arg
);
760 else if (arg_match(&arg
, &min_quantizer
, argi
))
761 cfg
.rc_min_quantizer
= arg_parse_uint(&arg
);
762 else if (arg_match(&arg
, &max_quantizer
, argi
))
763 cfg
.rc_max_quantizer
= arg_parse_uint(&arg
);
764 else if (arg_match(&arg
, &undershoot_pct
, argi
))
765 cfg
.rc_undershoot_pct
= arg_parse_uint(&arg
);
766 else if (arg_match(&arg
, &overshoot_pct
, argi
))
767 cfg
.rc_overshoot_pct
= arg_parse_uint(&arg
);
768 else if (arg_match(&arg
, &buf_sz
, argi
))
769 cfg
.rc_buf_sz
= arg_parse_uint(&arg
);
770 else if (arg_match(&arg
, &buf_initial_sz
, argi
))
771 cfg
.rc_buf_initial_sz
= arg_parse_uint(&arg
);
772 else if (arg_match(&arg
, &buf_optimal_sz
, argi
))
773 cfg
.rc_buf_optimal_sz
= arg_parse_uint(&arg
);
774 else if (arg_match(&arg
, &bias_pct
, argi
))
776 cfg
.rc_2pass_vbr_bias_pct
= arg_parse_uint(&arg
);
780 "Warning: option %s ignored in one-pass mode.\n",
783 else if (arg_match(&arg
, &minsection_pct
, argi
))
785 cfg
.rc_2pass_vbr_minsection_pct
= arg_parse_uint(&arg
);
789 "Warning: option %s ignored in one-pass mode.\n",
792 else if (arg_match(&arg
, &maxsection_pct
, argi
))
794 cfg
.rc_2pass_vbr_maxsection_pct
= arg_parse_uint(&arg
);
798 "Warning: option %s ignored in one-pass mode.\n",
801 else if (arg_match(&arg
, &kf_min_dist
, argi
))
802 cfg
.kf_min_dist
= arg_parse_uint(&arg
);
803 else if (arg_match(&arg
, &kf_max_dist
, argi
))
804 cfg
.kf_max_dist
= arg_parse_uint(&arg
);
805 else if (arg_match(&arg
, &kf_disabled
, argi
))
806 cfg
.kf_mode
= VPX_KF_DISABLED
;
811 /* Handle codec specific options */
812 #if CONFIG_VP8_ENCODER
814 if (codec
->iface
== &vpx_codec_vp8_cx_algo
)
816 ctrl_args
= vp8_args
;
817 ctrl_args_map
= vp8_arg_ctrl_map
;
822 for (argi
= argj
= argv
; (*argj
= *argi
); argi
+= arg
.argv_step
)
828 for (i
= 0; ctrl_args
[i
]; i
++)
830 if (arg_match(&arg
, ctrl_args
[i
], argi
))
834 if (arg_ctrl_cnt
< ARG_CTRL_CNT_MAX
)
836 arg_ctrls
[arg_ctrl_cnt
][0] = ctrl_args_map
[i
];
837 arg_ctrls
[arg_ctrl_cnt
][1] = arg_parse_int(&arg
);
847 /* Check for unrecognized options */
848 for (argi
= argv
; *argi
; argi
++)
849 if (argi
[0][0] == '-' && argi
[0][1])
850 die("Error: Unrecognized option %s\n", *argi
);
852 /* Handle non-option arguments */
856 if (!in_fn
|| !out_fn
)
859 memset(&stats
, 0, sizeof(stats
));
861 for (pass
= one_pass_only
? one_pass_only
- 1 : 0; pass
< arg_passes
; pass
++)
863 int frames_in
= 0, frames_out
= 0;
864 unsigned long nbytes
= 0;
865 struct detect_buffer detect
;
867 /* Parse certain options from the input file, if possible */
868 infile
= strcmp(in_fn
, "-") ? fopen(in_fn
, "rb") : stdin
;
872 fprintf(stderr
, "Failed to open input file\n");
876 fread(detect
.buf
, 1, 4, infile
);
879 if (file_is_y4m(infile
, &y4m
, detect
.buf
))
881 if (y4m_input_open(&y4m
, infile
, detect
.buf
, 4) >= 0)
883 file_type
= FILE_TYPE_Y4M
;
886 /* Use the frame rate from the file only if none was specified
887 * on the command-line.
889 if (!arg_have_timebase
)
891 cfg
.g_timebase
.num
= y4m
.fps_d
;
892 cfg
.g_timebase
.den
= y4m
.fps_n
;
893 /* And don't reset it in the second pass.*/
894 arg_have_timebase
= 1;
900 fprintf(stderr
, "Unsupported Y4M stream.\n");
904 else if (file_is_ivf(infile
, &fourcc
, &cfg
.g_w
, &cfg
.g_h
, detect
.buf
))
906 file_type
= FILE_TYPE_IVF
;
916 fprintf(stderr
, "Unsupported fourcc (%08x) in IVF\n", fourcc
);
922 file_type
= FILE_TYPE_RAW
;
925 #define SHOW(field) fprintf(stderr, " %-28s = %d\n", #field, cfg.field)
927 if (verbose
&& pass
== 0)
929 fprintf(stderr
, "Codec: %s\n", vpx_codec_iface_name(codec
->iface
));
930 fprintf(stderr
, "Source file: %s Format: %s\n", in_fn
,
931 arg_use_i420
? "I420" : "YV12");
932 fprintf(stderr
, "Destination file: %s\n", out_fn
);
933 fprintf(stderr
, "Encoder parameters:\n");
940 SHOW(g_timebase
.num
);
941 SHOW(g_timebase
.den
);
942 SHOW(g_error_resilient
);
944 SHOW(g_lag_in_frames
);
945 SHOW(rc_dropframe_thresh
);
946 SHOW(rc_resize_allowed
);
947 SHOW(rc_resize_up_thresh
);
948 SHOW(rc_resize_down_thresh
);
950 SHOW(rc_target_bitrate
);
951 SHOW(rc_min_quantizer
);
952 SHOW(rc_max_quantizer
);
953 SHOW(rc_undershoot_pct
);
954 SHOW(rc_overshoot_pct
);
956 SHOW(rc_buf_initial_sz
);
957 SHOW(rc_buf_optimal_sz
);
958 SHOW(rc_2pass_vbr_bias_pct
);
959 SHOW(rc_2pass_vbr_minsection_pct
);
960 SHOW(rc_2pass_vbr_maxsection_pct
);
966 if(pass
== (one_pass_only
? one_pass_only
- 1 : 0)) {
967 if (file_type
== FILE_TYPE_Y4M
)
968 /*The Y4M reader does its own allocation.
969 Just initialize this here to avoid problems if we never read any
971 memset(&raw
, 0, sizeof(raw
));
973 vpx_img_alloc(&raw
, arg_use_i420
? VPX_IMG_FMT_I420
: VPX_IMG_FMT_YV12
,
974 cfg
.g_w
, cfg
.g_h
, 1);
976 // This was added so that ivfenc will create monotically increasing
977 // timestamps. Since we create new timestamps for alt-reference frames
978 // we need to make room in the series of timestamps. Since there can
979 // only be 1 alt-ref frame ( current bitstream) multiplying by 2
980 // gives us enough room.
981 cfg
.g_timebase
.den
*= 2;
984 outfile
= strcmp(out_fn
, "-") ? fopen(out_fn
, "wb") : stdout
;
988 fprintf(stderr
, "Failed to open output file\n");
994 if (!stats_open_file(&stats
, stats_fn
, pass
))
996 fprintf(stderr
, "Failed to open statistics store\n");
1002 if (!stats_open_mem(&stats
, pass
))
1004 fprintf(stderr
, "Failed to open statistics store\n");
1005 return EXIT_FAILURE
;
1009 cfg
.g_pass
= arg_passes
== 2
1010 ? pass
? VPX_RC_LAST_PASS
: VPX_RC_FIRST_PASS
1012 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1016 cfg
.rc_twopass_stats_in
= stats_get(&stats
);
1021 write_ivf_file_header(outfile
, &cfg
, codec
->fourcc
, 0);
1024 /* Construct Encoder Context */
1025 vpx_codec_enc_init(&encoder
, codec
->iface
, &cfg
,
1026 show_psnr
? VPX_CODEC_USE_PSNR
: 0);
1027 ctx_exit_on_error(&encoder
, "Failed to initialize encoder");
1029 /* Note that we bypass the vpx_codec_control wrapper macro because
1030 * we're being clever to store the control IDs in an array. Real
1031 * applications will want to make use of the enumerations directly
1033 for (i
= 0; i
< arg_ctrl_cnt
; i
++)
1035 if (vpx_codec_control_(&encoder
, arg_ctrls
[i
][0], arg_ctrls
[i
][1]))
1036 fprintf(stderr
, "Error: Tried to set control %d = %d\n",
1037 arg_ctrls
[i
][0], arg_ctrls
[i
][1]);
1039 ctx_exit_on_error(&encoder
, "Failed to control codec");
1045 while (frame_avail
|| got_data
)
1047 vpx_codec_iter_t iter
= NULL
;
1048 const vpx_codec_cx_pkt_t
*pkt
;
1049 struct vpx_usec_timer timer
;
1051 if (!arg_limit
|| frames_in
< arg_limit
)
1053 frame_avail
= read_frame(infile
, &raw
, file_type
, &y4m
,
1060 "\rPass %d/%d frame %4d/%-4d %7ldB \033[K", pass
+ 1,
1061 arg_passes
, frames_in
, frames_out
, nbytes
);
1066 vpx_usec_timer_start(&timer
);
1068 // since we halved our timebase we need to double the timestamps
1069 // and duration we pass in.
1070 vpx_codec_encode(&encoder
, frame_avail
? &raw
: NULL
, (frames_in
- 1) * 2,
1071 2, 0, arg_deadline
);
1072 vpx_usec_timer_mark(&timer
);
1073 cx_time
+= vpx_usec_timer_elapsed(&timer
);
1074 ctx_exit_on_error(&encoder
, "Failed to encode frame");
1077 while ((pkt
= vpx_codec_get_cx_data(&encoder
, &iter
)))
1083 case VPX_CODEC_CX_FRAME_PKT
:
1085 fprintf(stderr
, " %6luF",
1086 (unsigned long)pkt
->data
.frame
.sz
);
1087 write_ivf_frame_header(outfile
, pkt
);
1088 fwrite(pkt
->data
.frame
.buf
, 1, pkt
->data
.frame
.sz
, outfile
);
1089 nbytes
+= pkt
->data
.raw
.sz
;
1091 case VPX_CODEC_STATS_PKT
:
1093 fprintf(stderr
, " %6luS",
1094 (unsigned long)pkt
->data
.twopass_stats
.sz
);
1096 pkt
->data
.twopass_stats
.buf
,
1097 pkt
->data
.twopass_stats
.sz
);
1098 nbytes
+= pkt
->data
.raw
.sz
;
1100 case VPX_CODEC_PSNR_PKT
:
1106 for (i
= 0; i
< 4; i
++)
1107 fprintf(stderr
, "%.3lf ", pkt
->data
.psnr
.psnr
[i
]);
1119 /* this bitrate calc is simplified and relies on the fact that this
1120 * application uses 1/timebase for framerate.
1123 "\rPass %d/%d frame %4d/%-4d %7ldB %7ldb/f %7"PRId64
"b/s"
1124 " %7lu %s (%.2f fps)\033[K", pass
+ 1,
1125 arg_passes
, frames_in
, frames_out
, nbytes
, nbytes
* 8 / frames_in
,
1126 nbytes
* 8 *(int64_t)cfg
.g_timebase
.den
/2/ cfg
.g_timebase
.num
/ frames_in
,
1127 cx_time
> 9999999 ? cx_time
/ 1000 : cx_time
,
1128 cx_time
> 9999999 ? "ms" : "us",
1129 (float)frames_in
* 1000000.0 / (float)cx_time
);
1131 vpx_codec_destroy(&encoder
);
1135 if (!fseek(outfile
, 0, SEEK_SET
))
1136 write_ivf_file_header(outfile
, &cfg
, codec
->fourcc
, frames_out
);
1139 stats_close(&stats
);
1140 fprintf(stderr
, "\n");
1148 return EXIT_SUCCESS
;