/* zero all the stats structures */ void stats_zero(void) { int dir; unsigned i; char *fname; fname = format("%s/stats", cache_dir); x_unlink(fname); free(fname); for (dir = 0; dir <= 0xF; dir++) { struct counters *counters = counters_init(STATS_END); fname = format("%s/%1x/stats", cache_dir, dir); if (lockfile_acquire(fname, lock_staleness_limit)) { stats_read(fname, counters); for (i = 0; stats_info[i].message; i++) { if (!(stats_info[i].flags & FLAG_NOZERO)) { counters->data[stats_info[i].stat] = 0; } } stats_write(fname, counters); lockfile_release(fname); } counters_free(counters); free(fname); } }
// Zero all the stats structures. void stats_zero(void) { assert(conf); char *fname = format("%s/stats", conf->cache_dir); x_unlink(fname); free(fname); time_t timestamp = time(NULL); for (int dir = 0; dir <= 0xF; dir++) { struct counters *counters = counters_init(STATS_END); struct stat st; fname = format("%s/%1x/stats", conf->cache_dir, dir); if (stat(fname, &st) != 0) { // No point in trying to reset the stats file if it doesn't exist. free(fname); continue; } if (lockfile_acquire(fname, lock_staleness_limit)) { stats_read(fname, counters); for (unsigned i = 0; stats_info[i].message; i++) { if (!(stats_info[i].flags & FLAG_NOZERO)) { counters->data[stats_info[i].stat] = 0; } } counters->data[STATS_ZEROTIMESTAMP] = timestamp; stats_write(fname, counters); lockfile_release(fname); } counters_free(counters); free(fname); } }
// Count directory cleanup run. void stats_add_cleanup(const char *dir, unsigned count) { struct counters *counters = counters_init(STATS_END); char *statsfile = format("%s/stats", dir); if (lockfile_acquire(statsfile, lock_staleness_limit)) { stats_read(statsfile, counters); counters->data[STATS_NUMCLEANUPS] += count; stats_write(statsfile, counters); lockfile_release(statsfile); } free(statsfile); counters_free(counters); }
// Set the per-directory sizes. void stats_set_sizes(const char *dir, unsigned num_files, uint64_t total_size) { struct counters *counters = counters_init(STATS_END); char *statsfile = format("%s/stats", dir); if (lockfile_acquire(statsfile, lock_staleness_limit)) { stats_read(statsfile, counters); counters->data[STATS_NUMFILES] = num_files; counters->data[STATS_TOTALSIZE] = total_size / 1024; stats_write(statsfile, counters); lockfile_release(statsfile); } free(statsfile); counters_free(counters); }
/* set the per directory limits */ int stats_set_limits(long maxfiles, long maxsize) { int dir; if (maxfiles != -1) { maxfiles /= 16; } if (maxsize != -1) { maxsize /= 16; } if (create_dir(cache_dir) != 0) { return 1; } /* set the limits in each directory */ for (dir = 0; dir <= 0xF; dir++) { char *fname, *cdir; cdir = format("%s/%1x", cache_dir, dir); if (create_dir(cdir) != 0) { free(cdir); return 1; } fname = format("%s/stats", cdir); free(cdir); if (lockfile_acquire(fname, lock_staleness_limit)) { struct counters *counters = counters_init(STATS_END); stats_read(fname, counters); if (maxfiles != -1) { counters->data[STATS_MAXFILES] = maxfiles; } if (maxsize != -1) { counters->data[STATS_MAXSIZE] = maxsize; } stats_write(fname, counters); lockfile_release(fname); counters_free(counters); } free(fname); } return 0; }
int main(int argc, char **argv) { int err = 0; lua_State *L; dictionary *d = 0; setup_signal_handler(); message_handle_missing(MESSAGE_MISSING_REPLACE); /* parse arguments again, to override ini file */ err = parse_args(argc, argv); if (err != 0) { return (err > 0) ? 0 : err; } d = parse_config(inifile); if (!d) { log_error("could not open ini configuration %s\n", inifile); } locale_init(); L = lua_init(d); game_init(); bind_monsters(L); err = eressea_run(L, luafile); if (err) { log_error("script %s failed with code %d\n", luafile, err); return err; } game_done(); lua_done(L); log_close(); stats_write(stdout, ""); stats_close(); if (d) { iniparser_freedict(d); } return 0; }
// Write counter updates in counter_updates to disk. void stats_flush(void) { assert(conf); if (!conf->stats) { return; } if (!counter_updates) { return; } bool should_flush = false; for (int i = 0; i < STATS_END; ++i) { if (counter_updates->data[i] > 0) { should_flush = true; break; } } if (!should_flush) { return; } if (!stats_file) { char *stats_dir; // A NULL stats_file means that we didn't get past calculate_object_hash(), // so we just choose one of stats files in the 16 subdirectories. stats_dir = format("%s/%x", conf->cache_dir, hash_from_int(getpid()) % 16); stats_file = format("%s/stats", stats_dir); free(stats_dir); } if (!lockfile_acquire(stats_file, lock_staleness_limit)) { return; } struct counters *counters = counters_init(STATS_END); stats_read(stats_file, counters); for (int i = 0; i < STATS_END; ++i) { counters->data[i] += counter_updates->data[i]; } stats_write(stats_file, counters); lockfile_release(stats_file); if (!str_eq(conf->log_file, "") || conf->debug) { for (int i = 0; i < STATS_END; ++i) { if (counter_updates->data[stats_info[i].stat] != 0 && !(stats_info[i].flags & FLAG_NOZERO)) { cc_log("Result: %s", stats_info[i].message); } } } char *subdir = dirname(stats_file); bool need_cleanup = false; if (conf->max_files != 0 && counters->data[STATS_NUMFILES] > conf->max_files / 16) { cc_log("Need to clean up %s since it holds %u files (limit: %u files)", subdir, counters->data[STATS_NUMFILES], conf->max_files / 16); need_cleanup = true; } if (conf->max_size != 0 && counters->data[STATS_TOTALSIZE] > conf->max_size / 1024 / 16) { cc_log("Need to clean up %s since it holds %u KiB (limit: %lu KiB)", subdir, counters->data[STATS_TOTALSIZE], (unsigned long)conf->max_size / 1024 / 16); need_cleanup = true; } if (need_cleanup) { clean_up_dir(conf, subdir, conf->limit_multiple); } free(subdir); counters_free(counters); }
/* * Write counter updates in counter_updates to disk. */ void stats_flush(void) { struct counters *counters; bool need_cleanup = false; bool should_flush = false; int i; assert(conf); if (!conf->stats) { return; } if (!counter_updates) { return; } for (i = 0; i < STATS_END; ++i) { if (counter_updates->data[i] > 0) { should_flush = true; break; } } if (!should_flush) { return; } if (!stats_file) { char *stats_dir; /* * A NULL stats_file means that we didn't get past calculate_object_hash(), * so we just choose one of stats files in the 16 subdirectories. */ stats_dir = format("%s/%x", conf->cache_dir, hash_from_int(getpid()) % 16); stats_file = format("%s/stats", stats_dir); free(stats_dir); } if (!lockfile_acquire(stats_file, lock_staleness_limit)) { return; } counters = counters_init(STATS_END); stats_read(stats_file, counters); for (i = 0; i < STATS_END; ++i) { counters->data[i] += counter_updates->data[i]; } stats_write(stats_file, counters); lockfile_release(stats_file); if (!str_eq(conf->log_file, "")) { for (i = 0; i < STATS_END; ++i) { if (counter_updates->data[stats_info[i].stat] != 0 && !(stats_info[i].flags & FLAG_NOZERO)) { cc_log("Result: %s", stats_info[i].message); } } } if (conf->max_files != 0 && counters->data[STATS_NUMFILES] > conf->max_files / 16) { need_cleanup = true; } if (conf->max_size != 0 && counters->data[STATS_TOTALSIZE] > conf->max_size / 1024 / 16) { need_cleanup = true; } if (need_cleanup) { char *p = dirname(stats_file); cleanup_dir(conf, p); free(p); } counters_free(counters); }
/* * Write counter updates in counter_updates to disk. */ void stats_flush(void) { struct counters *counters; bool need_cleanup = false; bool should_flush = false; int i; extern char *cache_logfile; if (getenv("CCACHE_NOSTATS")) return; init_counter_updates(); for (i = 0; i < STATS_END; ++i) { if (counter_updates->data[i] > 0) { should_flush = true; break; } } if (!should_flush) return; if (!stats_file) { char *stats_dir; /* * A NULL stats_file means that we didn't get past calculate_object_hash(), * so we just choose one of stats files in the 16 subdirectories. */ if (!cache_dir) return; stats_dir = format("%s/%x", cache_dir, hash_from_int(getpid()) % 16); stats_file = format("%s/stats", stats_dir); create_dir(stats_dir); free(stats_dir); } if (!lockfile_acquire(stats_file, lock_staleness_limit)) { return; } counters = counters_init(STATS_END); stats_read(stats_file, counters); for (i = 0; i < STATS_END; ++i) { counters->data[i] += counter_updates->data[i]; } stats_write(stats_file, counters); lockfile_release(stats_file); if (cache_logfile) { for (i = 0; i < STATS_END; ++i) { if (counter_updates->data[stats_info[i].stat] != 0 && !(stats_info[i].flags & FLAG_NOZERO)) { cc_log("Result: %s", stats_info[i].message); } } } if (counters->data[STATS_MAXFILES] != 0 && counters->data[STATS_NUMFILES] > counters->data[STATS_MAXFILES]) { need_cleanup = true; } if (counters->data[STATS_MAXSIZE] != 0 && counters->data[STATS_TOTALSIZE] > counters->data[STATS_MAXSIZE]) { need_cleanup = true; } if (need_cleanup) { char *p = dirname(stats_file); cleanup_dir(p, counters->data[STATS_MAXFILES], counters->data[STATS_MAXSIZE]); free(p); } }
int main(int argc, const char **argv) { AppInput app_input = {0}; VpxVideoWriter *writer = NULL; VpxVideoInfo info = {0}; vpx_codec_ctx_t codec; vpx_codec_enc_cfg_t enc_cfg; SvcContext svc_ctx; uint32_t i; uint32_t frame_cnt = 0; vpx_image_t raw; vpx_codec_err_t res; int pts = 0; /* PTS starts at 0 */ int frame_duration = 1; /* 1 timebase tick per frame */ FILE *infile = NULL; int end_of_stream = 0; int frames_received = 0; memset(&svc_ctx, 0, sizeof(svc_ctx)); svc_ctx.log_print = 1; exec_name = argv[0]; parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg); // Allocate image buffer if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32)) die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h); if (!(infile = fopen(app_input.input_filename, "rb"))) die("Failed to open %s for reading\n", app_input.input_filename); // Initialize codec if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) != VPX_CODEC_OK) die("Failed to initialize encoder\n"); info.codec_fourcc = VP9_FOURCC; info.time_base.numerator = enc_cfg.g_timebase.num; info.time_base.denominator = enc_cfg.g_timebase.den; if (vpx_svc_get_layer_resolution(&svc_ctx, svc_ctx.spatial_layers - 1, (unsigned int *)&info.frame_width, (unsigned int *)&info.frame_height) != VPX_CODEC_OK) { die("Failed to get output resolution"); } if (!(app_input.passes == 2 && app_input.pass == 1)) { // We don't save the bitstream for the 1st pass on two pass rate control writer = vpx_video_writer_open(app_input.output_filename, kContainerIVF, &info); if (!writer) die("Failed to open %s for writing\n", app_input.output_filename); } // skip initial frames for (i = 0; i < app_input.frames_to_skip; ++i) vpx_img_read(&raw, infile); // Encode frames while (!end_of_stream) { vpx_codec_iter_t iter = NULL; const vpx_codec_cx_pkt_t *cx_pkt; if (frame_cnt >= app_input.frames_to_code || !vpx_img_read(&raw, infile)) { // We need one extra vpx_svc_encode call at end of stream to flush // encoder and get remaining data end_of_stream = 1; } res = vpx_svc_encode(&svc_ctx, &codec, (end_of_stream ? NULL : &raw), pts, frame_duration, VPX_DL_GOOD_QUALITY); printf("%s", vpx_svc_get_message(&svc_ctx)); if (res != VPX_CODEC_OK) { die_codec(&codec, "Failed to encode frame"); } while ((cx_pkt = vpx_codec_get_cx_data(&codec, &iter)) != NULL) { switch (cx_pkt->kind) { case VPX_CODEC_CX_FRAME_PKT: { if (cx_pkt->data.frame.sz > 0) vpx_video_writer_write_frame(writer, cx_pkt->data.frame.buf, cx_pkt->data.frame.sz, cx_pkt->data.frame.pts); printf("SVC frame: %d, kf: %d, size: %d, pts: %d\n", frames_received, !!(cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY), (int)cx_pkt->data.frame.sz, (int)cx_pkt->data.frame.pts); ++frames_received; break; } case VPX_CODEC_STATS_PKT: { stats_write(&app_input.rc_stats, cx_pkt->data.twopass_stats.buf, cx_pkt->data.twopass_stats.sz); break; } default: { break; } } } if (!end_of_stream) { ++frame_cnt; pts += frame_duration; } } printf("Processed %d frames\n", frame_cnt); fclose(infile); if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); if (app_input.passes == 2) stats_close(&app_input.rc_stats, 1); if (writer) { vpx_video_writer_close(writer); } vpx_img_free(&raw); // display average size, psnr printf("%s", vpx_svc_dump_statistics(&svc_ctx)); vpx_svc_release(&svc_ctx); return EXIT_SUCCESS; }
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) { 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; } 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; } vpx_img_free(&raw); free(argv); return EXIT_SUCCESS; }