-#define ARG_CTRL_CNT_MAX 10
-
-
-int main(int argc, const char **argv_)
-{
- vpx_codec_ctx_t encoder;
- const char *in_fn = NULL, *out_fn = NULL, *stats_fn = NULL;
- int i;
- FILE *infile, *outfile;
- vpx_codec_enc_cfg_t cfg;
- vpx_codec_err_t res;
- int pass, one_pass_only = 0;
- stats_io_t stats;
- vpx_image_t raw;
- const struct codec_item *codec = codecs;
- int frame_avail, got_data;
-
- struct arg arg;
- char **argv, **argi, **argj;
- int arg_usage = 0, arg_passes = 1, arg_deadline = 0;
- int arg_ctrls[ARG_CTRL_CNT_MAX][2], arg_ctrl_cnt = 0;
- int arg_limit = 0;
- static const arg_def_t **ctrl_args = no_args;
- static const int *ctrl_args_map = NULL;
- int verbose = 0, show_psnr = 0;
- int arg_use_i420 = 1;
- int arg_have_timebase = 0;
- unsigned long cx_time = 0;
- unsigned int file_type, fourcc;
- y4m_input y4m;
-
- exec_name = argv_[0];
-
- if (argc < 3)
- usage_exit();
-
-
- /* First parse the codec and usage values, because we want to apply other
- * parameters on top of the default configuration provided by the codec.
- */
- argv = argv_dup(argc - 1, argv_ + 1);
-
- for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
- {
- arg.argv_step = 1;
-
- if (arg_match(&arg, &codecarg, argi))
- {
- int j, k = -1;
-
- for (j = 0; j < sizeof(codecs) / sizeof(codecs[0]); j++)
- if (!strcmp(codecs[j].name, arg.val))
- k = j;
-
- if (k >= 0)
- codec = codecs + k;
- else
- die("Error: Unrecognized argument (%s) to --codec\n",
- arg.val);
-
- }
- else if (arg_match(&arg, &passes, argi))
- {
- arg_passes = arg_parse_uint(&arg);
-
- if (arg_passes < 1 || arg_passes > 2)
- die("Error: Invalid number of passes (%d)\n", arg_passes);
- }
- else if (arg_match(&arg, &pass_arg, argi))
- {
- one_pass_only = arg_parse_uint(&arg);
-
- if (one_pass_only < 1 || one_pass_only > 2)
- die("Error: Invalid pass selected (%d)\n", one_pass_only);
- }
- else if (arg_match(&arg, &fpf_name, argi))
- stats_fn = arg.val;
- else if (arg_match(&arg, &usage, argi))
- arg_usage = arg_parse_uint(&arg);
- else if (arg_match(&arg, &deadline, argi))
- arg_deadline = arg_parse_uint(&arg);
- else if (arg_match(&arg, &best_dl, argi))
- arg_deadline = VPX_DL_BEST_QUALITY;
- else if (arg_match(&arg, &good_dl, argi))
- arg_deadline = VPX_DL_GOOD_QUALITY;
- else if (arg_match(&arg, &rt_dl, argi))
- arg_deadline = VPX_DL_REALTIME;
- else if (arg_match(&arg, &use_yv12, argi))
- {
- arg_use_i420 = 0;
- }
- else if (arg_match(&arg, &use_i420, argi))
- {
- arg_use_i420 = 1;
- }
- else if (arg_match(&arg, &verbosearg, argi))
- verbose = 1;
- else if (arg_match(&arg, &limit, argi))
- arg_limit = arg_parse_uint(&arg);
- else if (arg_match(&arg, &psnrarg, argi))
- show_psnr = 1;
- else
- argj++;
- }
-
- /* Ensure that --passes and --pass are consistent. If --pass is set and --passes=2,
- * ensure --fpf was set.
- */
- if (one_pass_only)
- {
- /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
- if (one_pass_only > arg_passes)
- {
- fprintf(stderr, "Warning: Assuming --pass=%d implies --passes=%d\n",
- one_pass_only, one_pass_only);
- arg_passes = one_pass_only;
- }
-
- if (arg_passes == 2 && !stats_fn)
- die("Must specify --fpf when --pass=%d and --passes=2\n", one_pass_only);
- }
-
- /* Populate encoder configuration */
- res = vpx_codec_enc_config_default(codec->iface, &cfg, arg_usage);
-
- if (res)
- {
- fprintf(stderr, "Failed to get config: %s\n",
- vpx_codec_err_to_string(res));
- return EXIT_FAILURE;
- }
-
- /* Now parse the remainder of the parameters. */
- for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
- {
- arg.argv_step = 1;
-
- if (0);
- else if (arg_match(&arg, &threads, argi))
- cfg.g_threads = arg_parse_uint(&arg);
- else if (arg_match(&arg, &profile, argi))
- cfg.g_profile = arg_parse_uint(&arg);
- else if (arg_match(&arg, &width, argi))
- cfg.g_w = arg_parse_uint(&arg);
- else if (arg_match(&arg, &height, argi))
- cfg.g_h = arg_parse_uint(&arg);
- else if (arg_match(&arg, &timebase, argi))
- {
- cfg.g_timebase = arg_parse_rational(&arg);
- arg_have_timebase = 1;
- }
- else if (arg_match(&arg, &error_resilient, argi))
- cfg.g_error_resilient = arg_parse_uint(&arg);
- else if (arg_match(&arg, &lag_in_frames, argi))
- cfg.g_lag_in_frames = arg_parse_uint(&arg);
- else if (arg_match(&arg, &dropframe_thresh, argi))
- cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
- else if (arg_match(&arg, &resize_allowed, argi))
- cfg.rc_resize_allowed = arg_parse_uint(&arg);
- else if (arg_match(&arg, &resize_up_thresh, argi))
- cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
- else if (arg_match(&arg, &resize_down_thresh, argi))
- cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
- else if (arg_match(&arg, &resize_down_thresh, argi))
- cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
- else if (arg_match(&arg, &end_usage, argi))
- cfg.rc_end_usage = arg_parse_uint(&arg);
- else if (arg_match(&arg, &target_bitrate, argi))
- cfg.rc_target_bitrate = arg_parse_uint(&arg);
- else if (arg_match(&arg, &min_quantizer, argi))
- cfg.rc_min_quantizer = arg_parse_uint(&arg);
- else if (arg_match(&arg, &max_quantizer, argi))
- cfg.rc_max_quantizer = arg_parse_uint(&arg);
- else if (arg_match(&arg, &undershoot_pct, argi))
- cfg.rc_undershoot_pct = arg_parse_uint(&arg);
- else if (arg_match(&arg, &overshoot_pct, argi))
- cfg.rc_overshoot_pct = arg_parse_uint(&arg);
- else if (arg_match(&arg, &buf_sz, argi))
- cfg.rc_buf_sz = arg_parse_uint(&arg);
- else if (arg_match(&arg, &buf_initial_sz, argi))
- cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
- else if (arg_match(&arg, &buf_optimal_sz, argi))
- cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
- else if (arg_match(&arg, &bias_pct, argi))
- {
- cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
-
- if (arg_passes < 2)
- fprintf(stderr,
- "Warning: option %s ignored in one-pass mode.\n",
- arg.name);
- }
- else if (arg_match(&arg, &minsection_pct, argi))
- {
- cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
-
- if (arg_passes < 2)
- fprintf(stderr,
- "Warning: option %s ignored in one-pass mode.\n",
- arg.name);
- }
- else if (arg_match(&arg, &maxsection_pct, argi))
- {
- cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
-
- if (arg_passes < 2)
- fprintf(stderr,
- "Warning: option %s ignored in one-pass mode.\n",
- arg.name);
- }
- else if (arg_match(&arg, &kf_min_dist, argi))
- cfg.kf_min_dist = arg_parse_uint(&arg);
- else if (arg_match(&arg, &kf_max_dist, argi))
- cfg.kf_max_dist = arg_parse_uint(&arg);
- else if (arg_match(&arg, &kf_disabled, argi))
- cfg.kf_mode = VPX_KF_DISABLED;
- else
- argj++;
- }
-
- /* Handle codec specific options */
-#if CONFIG_VP8_ENCODER
-
- if (codec->iface == &vpx_codec_vp8_cx_algo
-#if CONFIG_EXPERIMENTAL
- || codec->iface == &vpx_codec_vp8x_cx_algo
-#endif
- )
- {
- ctrl_args = vp8_args;
- ctrl_args_map = vp8_arg_ctrl_map;
- }
-
-#endif
-
- for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
- {
- int match = 0;
-
- arg.argv_step = 1;
-
- for (i = 0; ctrl_args[i]; i++)
- {
- if (arg_match(&arg, ctrl_args[i], argi))
- {
- match = 1;
-
- if (arg_ctrl_cnt < ARG_CTRL_CNT_MAX)
- {
- arg_ctrls[arg_ctrl_cnt][0] = ctrl_args_map[i];
- arg_ctrls[arg_ctrl_cnt][1] = arg_parse_int(&arg);
- arg_ctrl_cnt++;
- }
- }
- }
-
- if (!match)
- argj++;
- }
-
- /* Check for unrecognized options */
- for (argi = argv; *argi; argi++)
- if (argi[0][0] == '-' && argi[0][1])
- die("Error: Unrecognized option %s\n", *argi);
-
- /* Handle non-option arguments */
- in_fn = argv[0];
- out_fn = argv[1];
-
- if (!in_fn || !out_fn)
- usage_exit();
-
- memset(&stats, 0, sizeof(stats));
-
- for (pass = one_pass_only ? one_pass_only - 1 : 0; pass < arg_passes; pass++)
- {
- int frames_in = 0, frames_out = 0;
- unsigned long nbytes = 0;
- struct detect_buffer detect;
-
- /* Parse certain options from the input file, if possible */
- infile = strcmp(in_fn, "-") ? fopen(in_fn, "rb") : stdin;
-
- if (!infile)
- {
- fprintf(stderr, "Failed to open input file\n");
- return EXIT_FAILURE;
- }
-
- fread(detect.buf, 1, 4, infile);
- detect.valid = 0;
-
- if (file_is_y4m(infile, &y4m, detect.buf))
- {
- if (y4m_input_open(&y4m, infile, detect.buf, 4) >= 0)
- {
- file_type = FILE_TYPE_Y4M;
- cfg.g_w = y4m.pic_w;
- cfg.g_h = y4m.pic_h;
- /* Use the frame rate from the file only if none was specified
- * on the command-line.
- */
- if (!arg_have_timebase)
- {
- cfg.g_timebase.num = y4m.fps_d;
- cfg.g_timebase.den = y4m.fps_n;
- /* And don't reset it in the second pass.*/
- arg_have_timebase = 1;
- }
- arg_use_i420 = 0;
- }
- else
- {
- fprintf(stderr, "Unsupported Y4M stream.\n");
- return EXIT_FAILURE;
- }
- }
- else if (file_is_ivf(infile, &fourcc, &cfg.g_w, &cfg.g_h, detect.buf))
- {
- file_type = FILE_TYPE_IVF;
- switch (fourcc)
- {
- case 0x32315659:
- arg_use_i420 = 0;
- break;
- case 0x30323449:
- arg_use_i420 = 1;
- break;
- default:
- fprintf(stderr, "Unsupported fourcc (%08x) in IVF\n", fourcc);
- return EXIT_FAILURE;
- }
- }
- else
- {
- file_type = FILE_TYPE_RAW;
- detect.valid = 1;
- }
-#define SHOW(field) fprintf(stderr, " %-28s = %d\n", #field, cfg.field)
-
- if (verbose && pass == 0)
- {
- fprintf(stderr, "Codec: %s\n", vpx_codec_iface_name(codec->iface));
- fprintf(stderr, "Source file: %s Format: %s\n", in_fn,
- arg_use_i420 ? "I420" : "YV12");
- fprintf(stderr, "Destination file: %s\n", out_fn);
- fprintf(stderr, "Encoder parameters:\n");
-
- SHOW(g_usage);
- SHOW(g_threads);
- SHOW(g_profile);
- SHOW(g_w);
- SHOW(g_h);
- SHOW(g_timebase.num);
- SHOW(g_timebase.den);
- SHOW(g_error_resilient);
- SHOW(g_pass);
- SHOW(g_lag_in_frames);
- SHOW(rc_dropframe_thresh);
- SHOW(rc_resize_allowed);
- SHOW(rc_resize_up_thresh);
- SHOW(rc_resize_down_thresh);
- SHOW(rc_end_usage);
- SHOW(rc_target_bitrate);
- SHOW(rc_min_quantizer);
- SHOW(rc_max_quantizer);
- SHOW(rc_undershoot_pct);
- SHOW(rc_overshoot_pct);
- SHOW(rc_buf_sz);
- SHOW(rc_buf_initial_sz);
- SHOW(rc_buf_optimal_sz);
- SHOW(rc_2pass_vbr_bias_pct);
- SHOW(rc_2pass_vbr_minsection_pct);
- SHOW(rc_2pass_vbr_maxsection_pct);
- SHOW(kf_mode);
- SHOW(kf_min_dist);
- SHOW(kf_max_dist);
- }
-
- if(pass == (one_pass_only ? one_pass_only - 1 : 0)) {
- if (file_type == FILE_TYPE_Y4M)
- /*The Y4M reader does its own allocation.
- Just initialize this here to avoid problems if we never read any
- frames.*/
- memset(&raw, 0, sizeof(raw));
- else
- vpx_img_alloc(&raw, arg_use_i420 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_YV12,
- cfg.g_w, cfg.g_h, 1);
-
- // This was added so that ivfenc will create monotically increasing
- // timestamps. Since we create new timestamps for alt-reference frames
- // we need to make room in the series of timestamps. Since there can
- // only be 1 alt-ref frame ( current bitstream) multiplying by 2
- // gives us enough room.
- cfg.g_timebase.den *= 2;
- }
-
- outfile = strcmp(out_fn, "-") ? fopen(out_fn, "wb") : stdout;
-
- if (!outfile)
- {
- fprintf(stderr, "Failed to open output file\n");
- return EXIT_FAILURE;
- }
-
- if (stats_fn)
- {
- if (!stats_open_file(&stats, stats_fn, pass))
- {
- fprintf(stderr, "Failed to open statistics store\n");
- return EXIT_FAILURE;
- }
- }
- else
- {
- if (!stats_open_mem(&stats, pass))
- {
- fprintf(stderr, "Failed to open statistics store\n");
- return EXIT_FAILURE;
- }
- }
-
- cfg.g_pass = arg_passes == 2
- ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS
- : VPX_RC_ONE_PASS;
-#if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
-
- if (pass)
- {
- cfg.rc_twopass_stats_in = stats_get(&stats);
- }
-
-#endif
-
- write_ivf_file_header(outfile, &cfg, codec->fourcc, 0);
-
-
- /* Construct Encoder Context */
- vpx_codec_enc_init(&encoder, codec->iface, &cfg,
- show_psnr ? VPX_CODEC_USE_PSNR : 0);
- ctx_exit_on_error(&encoder, "Failed to initialize encoder");
-
- /* Note that we bypass the vpx_codec_control wrapper macro because
- * we're being clever to store the control IDs in an array. Real
- * applications will want to make use of the enumerations directly
- */
- for (i = 0; i < arg_ctrl_cnt; i++)
- {
- if (vpx_codec_control_(&encoder, arg_ctrls[i][0], arg_ctrls[i][1]))
- fprintf(stderr, "Error: Tried to set control %d = %d\n",
- arg_ctrls[i][0], arg_ctrls[i][1]);
-
- ctx_exit_on_error(&encoder, "Failed to control codec");
- }
-
- frame_avail = 1;
- got_data = 0;
-
- while (frame_avail || got_data)
- {
- vpx_codec_iter_t iter = NULL;
- const vpx_codec_cx_pkt_t *pkt;
- struct vpx_usec_timer timer;
-
- if (!arg_limit || frames_in < arg_limit)
- {
- frame_avail = read_frame(infile, &raw, file_type, &y4m,
- &detect);
-
- if (frame_avail)
- frames_in++;
-
- fprintf(stderr,
- "\rPass %d/%d frame %4d/%-4d %7ldB \033[K", pass + 1,
- arg_passes, frames_in, frames_out, nbytes);
- }
- else
- frame_avail = 0;
-
- vpx_usec_timer_start(&timer);
-
- // since we halved our timebase we need to double the timestamps
- // and duration we pass in.
- vpx_codec_encode(&encoder, frame_avail ? &raw : NULL, (frames_in - 1) * 2,
- 2, 0, arg_deadline);
- vpx_usec_timer_mark(&timer);
- cx_time += vpx_usec_timer_elapsed(&timer);
- ctx_exit_on_error(&encoder, "Failed to encode frame");
- got_data = 0;
-
- while ((pkt = vpx_codec_get_cx_data(&encoder, &iter)))
- {
- got_data = 1;
-
- switch (pkt->kind)
- {
- case VPX_CODEC_CX_FRAME_PKT:
- frames_out++;
- fprintf(stderr, " %6luF",
- (unsigned long)pkt->data.frame.sz);
- write_ivf_frame_header(outfile, pkt);
- fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile);
- nbytes += pkt->data.raw.sz;
- break;
- case VPX_CODEC_STATS_PKT:
- frames_out++;
- fprintf(stderr, " %6luS",
- (unsigned long)pkt->data.twopass_stats.sz);
- stats_write(&stats,
- pkt->data.twopass_stats.buf,
- pkt->data.twopass_stats.sz);
- nbytes += pkt->data.raw.sz;
- break;
- case VPX_CODEC_PSNR_PKT:
-
- if (show_psnr)
- {
- int i;
-
- for (i = 0; i < 4; i++)
- fprintf(stderr, "%.3lf ", pkt->data.psnr.psnr[i]);
- }
-
- break;
- default:
- break;
- }
- }
-
- fflush(stdout);
- }
-
- /* this bitrate calc is simplified and relies on the fact that this
- * application uses 1/timebase for framerate.
- */
- fprintf(stderr,
- "\rPass %d/%d frame %4d/%-4d %7ldB %7ldb/f %7"PRId64"b/s"
- " %7lu %s (%.2f fps)\033[K", pass + 1,
- arg_passes, frames_in, frames_out, nbytes, nbytes * 8 / frames_in,
- nbytes * 8 *(int64_t)cfg.g_timebase.den/2/ cfg.g_timebase.num / frames_in,
- cx_time > 9999999 ? cx_time / 1000 : cx_time,
- cx_time > 9999999 ? "ms" : "us",
- (float)frames_in * 1000000.0 / (float)cx_time);
-
- vpx_codec_destroy(&encoder);
-
- fclose(infile);
-
- if (!fseek(outfile, 0, SEEK_SET))
- write_ivf_file_header(outfile, &cfg, codec->fourcc, frames_out);
-
- fclose(outfile);
- stats_close(&stats);
- fprintf(stderr, "\n");
-
- if (one_pass_only)
- break;
- }