00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #define USE_POSIX_MMAP HAVE_SYS_MMAN_H
00015
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <stdarg.h>
00019 #include <string.h>
00020 #include "vpx_encoder.h"
00021 #if USE_POSIX_MMAP
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 #include <sys/mman.h>
00025 #include <fcntl.h>
00026 #include <unistd.h>
00027 #endif
00028 #if CONFIG_VP8_ENCODER
00029 #include "vp8cx.h"
00030 #endif
00031 #include "vpx_ports/mem_ops.h"
00032 #include "vpx_ports/vpx_timer.h"
00033
00034 static const char *exec_name;
00035
00036 static const struct codec_item
00037 {
00038 char const *name;
00039 const vpx_codec_iface_t *iface;
00040 unsigned int fourcc;
00041 } codecs[] =
00042 {
00043 #if CONFIG_VP8_ENCODER
00044 {"vp8", &vpx_codec_vp8_cx_algo, 0x30385056},
00045 #endif
00046 };
00047
00048 static void usage_exit();
00049
00050 void die(const char *fmt, ...)
00051 {
00052 va_list ap;
00053 va_start(ap, fmt);
00054 vprintf(fmt, ap);
00055 printf("\n");
00056 usage_exit();
00057 }
00058
00059 static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s)
00060 {
00061 if (ctx->err)
00062 {
00063 const char *detail = vpx_codec_error_detail(ctx);
00064
00065 printf("%s: %s\n", s, vpx_codec_error(ctx));
00066
00067 if (detail)
00068 printf(" %s\n", detail);
00069
00070 exit(EXIT_FAILURE);
00071 }
00072 }
00073
00074
00075
00076
00077 typedef struct
00078 {
00079 vpx_fixed_buf_t buf;
00080 int pass;
00081 FILE *file;
00082 char *buf_ptr;
00083 size_t buf_alloc_sz;
00084 } stats_io_t;
00085
00086 int stats_open_file(stats_io_t *stats, const char *fpf, int pass)
00087 {
00088 int res;
00089
00090 stats->pass = pass;
00091
00092 if (pass == 0)
00093 {
00094 stats->file = fopen(fpf, "wb");
00095 stats->buf.sz = 0;
00096 stats->buf.buf = NULL,
00097 res = (stats->file != NULL);
00098 }
00099 else
00100 {
00101 #if 0
00102 #elif USE_POSIX_MMAP
00103 struct stat stat_buf;
00104 int fd;
00105
00106 fd = open(fpf, O_RDONLY);
00107 stats->file = fdopen(fd, "rb");
00108 fstat(fd, &stat_buf);
00109 stats->buf.sz = stat_buf.st_size;
00110 stats->buf.buf = mmap(NULL, stats->buf.sz, PROT_READ, MAP_PRIVATE,
00111 fd, 0);
00112 res = (stats->buf.buf != NULL);
00113 #else
00114 size_t nbytes;
00115
00116 stats->file = fopen(fpf, "rb");
00117
00118 if (fseek(stats->file, 0, SEEK_END))
00119 {
00120 fprintf(stderr, "First-pass stats file must be seekable!\n");
00121 exit(EXIT_FAILURE);
00122 }
00123
00124 stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
00125 rewind(stats->file);
00126
00127 stats->buf.buf = malloc(stats->buf_alloc_sz);
00128
00129 if (!stats->buf.buf)
00130 {
00131 fprintf(stderr, "Failed to allocate first-pass stats buffer (%d bytes)\n",
00132 stats->buf_alloc_sz);
00133 exit(EXIT_FAILURE);
00134 }
00135
00136 nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
00137 res = (nbytes == stats->buf.sz);
00138 #endif
00139 }
00140
00141 return res;
00142 }
00143
00144 int stats_open_mem(stats_io_t *stats, int pass)
00145 {
00146 int res;
00147 stats->pass = pass;
00148
00149 if (!pass)
00150 {
00151 stats->buf.sz = 0;
00152 stats->buf_alloc_sz = 64 * 1024;
00153 stats->buf.buf = malloc(stats->buf_alloc_sz);
00154 }
00155
00156 stats->buf_ptr = stats->buf.buf;
00157 res = (stats->buf.buf != NULL);
00158 return res;
00159 }
00160
00161
00162 void stats_close(stats_io_t *stats)
00163 {
00164 if (stats->file)
00165 {
00166 if (stats->pass == 1)
00167 {
00168 #if 0
00169 #elif USE_POSIX_MMAP
00170 munmap(stats->buf.buf, stats->buf.sz);
00171 #else
00172 free(stats->buf.buf);
00173 #endif
00174 }
00175
00176 fclose(stats->file);
00177 stats->file = NULL;
00178 }
00179 else
00180 {
00181 if (stats->pass == 1)
00182 free(stats->buf.buf);
00183 }
00184 }
00185
00186 void stats_write(stats_io_t *stats, const void *pkt, size_t len)
00187 {
00188 if (stats->file)
00189 {
00190 fwrite(pkt, 1, len, stats->file);
00191 }
00192 else
00193 {
00194 if (stats->buf.sz + len > stats->buf_alloc_sz)
00195 {
00196 size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
00197 char *new_ptr = realloc(stats->buf.buf, new_sz);
00198
00199 if (new_ptr)
00200 {
00201 stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
00202 stats->buf.buf = new_ptr;
00203 stats->buf_alloc_sz = new_sz;
00204 }
00205 }
00206
00207 memcpy(stats->buf_ptr, pkt, len);
00208 stats->buf.sz += len;
00209 stats->buf_ptr += len;
00210 }
00211 }
00212
00213 vpx_fixed_buf_t stats_get(stats_io_t *stats)
00214 {
00215 return stats->buf;
00216 }
00217
00218 #define IVF_FRAME_HDR_SZ (4+8)
00219 static int read_frame(FILE *f, vpx_image_t *img, unsigned int is_ivf)
00220 {
00221 int plane = 0;
00222
00223 if (is_ivf)
00224 {
00225 char junk[IVF_FRAME_HDR_SZ];
00226
00227
00228
00229
00230
00231 fread(junk, 1, IVF_FRAME_HDR_SZ, f);
00232 }
00233
00234 for (plane = 0; plane < 3; plane++)
00235 {
00236 unsigned char *ptr;
00237 int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
00238 int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
00239 int r;
00240
00241
00242
00243
00244
00245 switch (plane)
00246 {
00247 case 1:
00248 ptr = img->planes[img->fmt==IMG_FMT_YV12? PLANE_V : PLANE_U];
00249 break;
00250 case 2:
00251 ptr = img->planes[img->fmt==IMG_FMT_YV12?PLANE_U : PLANE_V];
00252 break;
00253 default:
00254 ptr = img->planes[plane];
00255 }
00256
00257 for (r = 0; r < h; r++)
00258 {
00259 fread(ptr, 1, w, f);
00260 ptr += img->stride[plane];
00261 }
00262 }
00263
00264 return !feof(f);
00265 }
00266
00267
00268 #define IVF_FILE_HDR_SZ (32)
00269 unsigned int file_is_ivf(FILE *infile,
00270 unsigned int *fourcc,
00271 unsigned int *width,
00272 unsigned int *height)
00273 {
00274 char raw_hdr[IVF_FILE_HDR_SZ];
00275 int is_ivf = 0;
00276
00277
00278
00279
00280 if (fread(raw_hdr, 1, IVF_FILE_HDR_SZ, infile) == IVF_FILE_HDR_SZ)
00281 {
00282 if (raw_hdr[0] == 'D' && raw_hdr[1] == 'K'
00283 && raw_hdr[2] == 'I' && raw_hdr[3] == 'F')
00284 {
00285 is_ivf = 1;
00286
00287 if (mem_get_le16(raw_hdr + 4) != 0)
00288 fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
00289 " decode properly.");
00290
00291 *fourcc = mem_get_le32(raw_hdr + 8);
00292 }
00293 }
00294
00295 if (is_ivf)
00296 {
00297 *width = mem_get_le16(raw_hdr + 12);
00298 *height = mem_get_le16(raw_hdr + 14);
00299 }
00300 else
00301 rewind(infile);
00302
00303 return is_ivf;
00304 }
00305
00306
00307 static void write_ivf_file_header(FILE *outfile,
00308 const vpx_codec_enc_cfg_t *cfg,
00309 unsigned int fourcc,
00310 int frame_cnt)
00311 {
00312 char header[32];
00313
00314 if (cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
00315 return;
00316
00317 header[0] = 'D';
00318 header[1] = 'K';
00319 header[2] = 'I';
00320 header[3] = 'F';
00321 mem_put_le16(header + 4, 0);
00322 mem_put_le16(header + 6, 32);
00323 mem_put_le32(header + 8, fourcc);
00324 mem_put_le16(header + 12, cfg->g_w);
00325 mem_put_le16(header + 14, cfg->g_h);
00326 mem_put_le32(header + 16, cfg->g_timebase.den);
00327 mem_put_le32(header + 20, cfg->g_timebase.num);
00328 mem_put_le32(header + 24, frame_cnt);
00329 mem_put_le32(header + 28, 0);
00330
00331 fwrite(header, 1, 32, outfile);
00332 }
00333
00334
00335 static void write_ivf_frame_header(FILE *outfile,
00336 const vpx_codec_cx_pkt_t *pkt)
00337 {
00338 char header[12];
00339 vpx_codec_pts_t pts;
00340
00341 if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
00342 return;
00343
00344 pts = pkt->data.frame.pts;
00345 mem_put_le32(header, pkt->data.frame.sz);
00346 mem_put_le32(header + 4, pts & 0xFFFFFFFF);
00347 mem_put_le32(header + 8, pts >> 32);
00348
00349 fwrite(header, 1, 12, outfile);
00350 }
00351
00352 #include "args.h"
00353
00354 static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
00355 "Input file is YV12 ");
00356 static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
00357 "Input file is I420 (default)");
00358 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
00359 "Codec to use");
00360 static const arg_def_t passes = ARG_DEF("p", "passes", 1,
00361 "Number of passes (1/2)");
00362 static const arg_def_t pass_arg = ARG_DEF(NULL, "pass", 1,
00363 "Pass to execute (1/2)");
00364 static const arg_def_t fpf_name = ARG_DEF(NULL, "fpf", 1,
00365 "First pass statistics file name");
00366 static const arg_def_t limit = ARG_DEF(NULL, "limit", 1,
00367 "Stop encoding after n input frames");
00368 static const arg_def_t deadline = ARG_DEF("d", "deadline", 1,
00369 "Deadline per frame (usec)");
00370 static const arg_def_t best_dl = ARG_DEF(NULL, "best", 0,
00371 "Use Best Quality Deadline");
00372 static const arg_def_t good_dl = ARG_DEF(NULL, "good", 0,
00373 "Use Good Quality Deadline");
00374 static const arg_def_t rt_dl = ARG_DEF(NULL, "rt", 0,
00375 "Use Realtime Quality Deadline");
00376 static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0,
00377 "Show encoder parameters");
00378 static const arg_def_t psnrarg = ARG_DEF(NULL, "psnr", 0,
00379 "Show PSNR in status line");
00380 static const arg_def_t *main_args[] =
00381 {
00382 &codecarg, &passes, &pass_arg, &fpf_name, &limit, &deadline, &best_dl, &good_dl, &rt_dl,
00383 &verbosearg, &psnrarg,
00384 NULL
00385 };
00386
00387 static const arg_def_t usage = ARG_DEF("u", "usage", 1,
00388 "Usage profile number to use");
00389 static const arg_def_t threads = ARG_DEF("t", "threads", 1,
00390 "Max number of threads to use");
00391 static const arg_def_t profile = ARG_DEF(NULL, "profile", 1,
00392 "Bitstream profile number to use");
00393 static const arg_def_t width = ARG_DEF("w", "width", 1,
00394 "Frame width");
00395 static const arg_def_t height = ARG_DEF("h", "height", 1,
00396 "Frame height");
00397 static const arg_def_t timebase = ARG_DEF(NULL, "timebase", 1,
00398 "Stream timebase (frame duration)");
00399 static const arg_def_t error_resilient = ARG_DEF(NULL, "error-resilient", 1,
00400 "Enable error resiliency features");
00401 static const arg_def_t lag_in_frames = ARG_DEF(NULL, "lag-in-frames", 1,
00402 "Max number of frames to lag");
00403
00404 static const arg_def_t *global_args[] =
00405 {
00406 &use_yv12, &use_i420, &usage, &threads, &profile,
00407 &width, &height, &timebase, &error_resilient,
00408 &lag_in_frames, NULL
00409 };
00410
00411 static const arg_def_t dropframe_thresh = ARG_DEF(NULL, "drop-frame", 1,
00412 "Temporal resampling threshold (buf %)");
00413 static const arg_def_t resize_allowed = ARG_DEF(NULL, "resize-allowed", 1,
00414 "Spatial resampling enabled (bool)");
00415 static const arg_def_t resize_up_thresh = ARG_DEF(NULL, "resize-up", 1,
00416 "Upscale threshold (buf %)");
00417 static const arg_def_t resize_down_thresh = ARG_DEF(NULL, "resize-down", 1,
00418 "Downscale threshold (buf %)");
00419 static const arg_def_t end_usage = ARG_DEF(NULL, "end-usage", 1,
00420 "VBR=0 | CBR=1");
00421 static const arg_def_t target_bitrate = ARG_DEF(NULL, "target-bitrate", 1,
00422 "Bitrate (kbps)");
00423 static const arg_def_t min_quantizer = ARG_DEF(NULL, "min-q", 1,
00424 "Minimum (best) quantizer");
00425 static const arg_def_t max_quantizer = ARG_DEF(NULL, "max-q", 1,
00426 "Maximum (worst) quantizer");
00427 static const arg_def_t undershoot_pct = ARG_DEF(NULL, "undershoot-pct", 1,
00428 "Datarate undershoot (min) target (%)");
00429 static const arg_def_t overshoot_pct = ARG_DEF(NULL, "overshoot-pct", 1,
00430 "Datarate overshoot (max) target (%)");
00431 static const arg_def_t buf_sz = ARG_DEF(NULL, "buf-sz", 1,
00432 "Client buffer size (ms)");
00433 static const arg_def_t buf_initial_sz = ARG_DEF(NULL, "buf-initial-sz", 1,
00434 "Client initial buffer size (ms)");
00435 static const arg_def_t buf_optimal_sz = ARG_DEF(NULL, "buf-optimal-sz", 1,
00436 "Client optimal buffer size (ms)");
00437 static const arg_def_t *rc_args[] =
00438 {
00439 &dropframe_thresh, &resize_allowed, &resize_up_thresh, &resize_down_thresh,
00440 &end_usage, &target_bitrate, &min_quantizer, &max_quantizer,
00441 &undershoot_pct, &overshoot_pct, &buf_sz, &buf_initial_sz, &buf_optimal_sz,
00442 NULL
00443 };
00444
00445
00446 static const arg_def_t bias_pct = ARG_DEF(NULL, "bias-pct", 1,
00447 "CBR/VBR bias (0=CBR, 100=VBR)");
00448 static const arg_def_t minsection_pct = ARG_DEF(NULL, "minsection-pct", 1,
00449 "GOP min bitrate (% of target)");
00450 static const arg_def_t maxsection_pct = ARG_DEF(NULL, "maxsection-pct", 1,
00451 "GOP max bitrate (% of target)");
00452 static const arg_def_t *rc_twopass_args[] =
00453 {
00454 &bias_pct, &minsection_pct, &maxsection_pct, NULL
00455 };
00456
00457
00458 static const arg_def_t kf_min_dist = ARG_DEF(NULL, "kf-min-dist", 1,
00459 "Minimum keyframe interval (frames)");
00460 static const arg_def_t kf_max_dist = ARG_DEF(NULL, "kf-max-dist", 1,
00461 "Maximum keyframe interval (frames)");
00462 static const arg_def_t *kf_args[] =
00463 {
00464 &kf_min_dist, &kf_max_dist, NULL
00465 };
00466
00467
00468 #if CONFIG_VP8_ENCODER
00469 static const arg_def_t noise_sens = ARG_DEF(NULL, "noise-sensitivity", 1,
00470 "Noise sensitivity (frames to blur)");
00471 static const arg_def_t sharpness = ARG_DEF(NULL, "sharpness", 1,
00472 "Filter sharpness (0-7)");
00473 static const arg_def_t static_thresh = ARG_DEF(NULL, "static-thresh", 1,
00474 "Motion detection threshold");
00475 #endif
00476
00477 #if CONFIG_VP8_ENCODER
00478 static const arg_def_t cpu_used = ARG_DEF(NULL, "cpu-used", 1,
00479 "CPU Used (-16..16)");
00480 #endif
00481
00482
00483 #if CONFIG_VP8_ENCODER
00484 static const arg_def_t token_parts = ARG_DEF(NULL, "token-parts", 1,
00485 "Number of token partitions to use, log2");
00486 static const arg_def_t auto_altref = ARG_DEF(NULL, "auto-alt-ref", 1,
00487 "Enable automatic alt reference frames");
00488 static const arg_def_t arnr_maxframes = ARG_DEF(NULL, "arnr-maxframes", 1,
00489 "alt_ref Max Frames");
00490 static const arg_def_t arnr_strength = ARG_DEF(NULL, "arnr-strength", 1,
00491 "alt_ref Strength");
00492 static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1,
00493 "alt_ref Type");
00494
00495 static const arg_def_t *vp8_args[] =
00496 {
00497 &cpu_used, &auto_altref, &noise_sens, &sharpness, &static_thresh,
00498 &token_parts, &arnr_maxframes, &arnr_strength, &arnr_type, NULL
00499 };
00500 static const int vp8_arg_ctrl_map[] =
00501 {
00502 VP8E_SET_CPUUSED, VP8E_SET_ENABLEAUTOALTREF,
00503 VP8E_SET_NOISE_SENSITIVITY, VP8E_SET_SHARPNESS, VP8E_SET_STATIC_THRESHOLD,
00504 VP8E_SET_TOKEN_PARTITIONS,
00505 VP8E_SET_ARNR_MAXFRAMES, VP8E_SET_ARNR_STRENGTH , VP8E_SET_ARNR_TYPE, 0
00506 };
00507 #endif
00508
00509 static const arg_def_t *no_args[] = { NULL };
00510
00511 static void usage_exit()
00512 {
00513 int i;
00514
00515 printf("Usage: %s <options> src_filename dst_filename\n", exec_name);
00516
00517 printf("\n_options:\n");
00518 arg_show_usage(stdout, main_args);
00519 printf("\n_encoder Global Options:\n");
00520 arg_show_usage(stdout, global_args);
00521 printf("\n_rate Control Options:\n");
00522 arg_show_usage(stdout, rc_args);
00523 printf("\n_twopass Rate Control Options:\n");
00524 arg_show_usage(stdout, rc_twopass_args);
00525 printf("\n_keyframe Placement Options:\n");
00526 arg_show_usage(stdout, kf_args);
00527 #if CONFIG_VP8_ENCODER
00528 printf("\n_vp8 Specific Options:\n");
00529 arg_show_usage(stdout, vp8_args);
00530 #endif
00531 printf("\n"
00532 "Included encoders:\n"
00533 "\n");
00534
00535 for (i = 0; i < sizeof(codecs) / sizeof(codecs[0]); i++)
00536 printf(" %-6s - %s\n",
00537 codecs[i].name,
00538 vpx_codec_iface_name(codecs[i].iface));
00539
00540 exit(EXIT_FAILURE);
00541 }
00542
00543 #define ARG_CTRL_CNT_MAX 10
00544
00545
00546 int main(int argc, const char **argv_)
00547 {
00548 vpx_codec_ctx_t encoder;
00549 const char *in_fn = NULL, *out_fn = NULL, *stats_fn = NULL;
00550 int i;
00551 FILE *infile, *outfile;
00552 vpx_codec_enc_cfg_t cfg;
00553 vpx_codec_err_t res;
00554 int pass, one_pass_only = 0;
00555 stats_io_t stats;
00556 vpx_image_t raw;
00557 const struct codec_item *codec = codecs;
00558 int frame_avail, got_data;
00559
00560 struct arg arg;
00561 char **argv, **argi, **argj;
00562 int arg_usage = 0, arg_passes = 1, arg_deadline = 0;
00563 int arg_ctrls[ARG_CTRL_CNT_MAX][2], arg_ctrl_cnt = 0;
00564 int arg_limit = 0;
00565 static const arg_def_t **ctrl_args = no_args;
00566 static const int *ctrl_args_map = NULL;
00567 int verbose = 0, show_psnr = 0;
00568 int arg_use_i420 = 1;
00569 unsigned long cx_time = 0;
00570 unsigned int is_ivf, fourcc;
00571
00572 exec_name = argv_[0];
00573
00574 if (argc < 3)
00575 usage_exit();
00576
00577
00578
00579
00580
00581 argv = argv_dup(argc - 1, argv_ + 1);
00582
00583 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
00584 {
00585 arg.argv_step = 1;
00586
00587 if (arg_match(&arg, &codecarg, argi))
00588 {
00589 int j, k = -1;
00590
00591 for (j = 0; j < sizeof(codecs) / sizeof(codecs[0]); j++)
00592 if (!strcmp(codecs[j].name, arg.val))
00593 k = j;
00594
00595 if (k >= 0)
00596 codec = codecs + k;
00597 else
00598 die("Error: Unrecognized argument (%s) to --codec\n",
00599 arg.val);
00600
00601 }
00602 else if (arg_match(&arg, &passes, argi))
00603 {
00604 arg_passes = arg_parse_uint(&arg);
00605
00606 if (arg_passes < 1 || arg_passes > 2)
00607 die("Error: Invalid number of passes (%d)\n", arg_passes);
00608 }
00609 else if (arg_match(&arg, &pass_arg, argi))
00610 {
00611 one_pass_only = arg_parse_uint(&arg);
00612
00613 if (one_pass_only < 1 || one_pass_only > 2)
00614 die("Error: Invalid pass selected (%d)\n", one_pass_only);
00615 }
00616 else if (arg_match(&arg, &fpf_name, argi))
00617 stats_fn = arg.val;
00618 else if (arg_match(&arg, &usage, argi))
00619 arg_usage = arg_parse_uint(&arg);
00620 else if (arg_match(&arg, &deadline, argi))
00621 arg_deadline = arg_parse_uint(&arg);
00622 else if (arg_match(&arg, &best_dl, argi))
00623 arg_deadline = VPX_DL_BEST_QUALITY;
00624 else if (arg_match(&arg, &good_dl, argi))
00625 arg_deadline = VPX_DL_GOOD_QUALITY;
00626 else if (arg_match(&arg, &rt_dl, argi))
00627 arg_deadline = VPX_DL_REALTIME;
00628 else if (arg_match(&arg, &use_yv12, argi))
00629 {
00630 arg_use_i420 = 0;
00631 }
00632 else if (arg_match(&arg, &use_i420, argi))
00633 {
00634 arg_use_i420 = 1;
00635 }
00636 else if (arg_match(&arg, &verbosearg, argi))
00637 verbose = 1;
00638 else if (arg_match(&arg, &limit, argi))
00639 arg_limit = arg_parse_uint(&arg);
00640 else if (arg_match(&arg, &psnrarg, argi))
00641 show_psnr = 1;
00642 else
00643 argj++;
00644 }
00645
00646
00647
00648
00649 if (one_pass_only)
00650 {
00651
00652 if (one_pass_only > arg_passes)
00653 {
00654 printf("Warning: Assuming --pass=%d implies --passes=%d\n",
00655 one_pass_only, one_pass_only);
00656 arg_passes = one_pass_only;
00657 }
00658
00659 if (arg_passes == 2 && !stats_fn)
00660 die("Must specify --fpf when --pass=%d and --passes=2\n", one_pass_only);
00661 }
00662
00663
00664 res = vpx_codec_enc_config_default(codec->iface, &cfg, arg_usage);
00665
00666 if (res)
00667 {
00668 printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
00669 return EXIT_FAILURE;
00670 }
00671
00672
00673 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
00674 {
00675 arg.argv_step = 1;
00676
00677 if (0);
00678 else if (arg_match(&arg, &threads, argi))
00679 cfg.g_threads = arg_parse_uint(&arg);
00680 else if (arg_match(&arg, &profile, argi))
00681 cfg.g_profile = arg_parse_uint(&arg);
00682 else if (arg_match(&arg, &width, argi))
00683 cfg.g_w = arg_parse_uint(&arg);
00684 else if (arg_match(&arg, &height, argi))
00685 cfg.g_h = arg_parse_uint(&arg);
00686 else if (arg_match(&arg, &timebase, argi))
00687 cfg.g_timebase = arg_parse_rational(&arg);
00688 else if (arg_match(&arg, &error_resilient, argi))
00689 cfg.g_error_resilient = arg_parse_uint(&arg);
00690 else if (arg_match(&arg, &lag_in_frames, argi))
00691 cfg.g_lag_in_frames = arg_parse_uint(&arg);
00692 else if (arg_match(&arg, &dropframe_thresh, argi))
00693 cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
00694 else if (arg_match(&arg, &resize_allowed, argi))
00695 cfg.rc_resize_allowed = arg_parse_uint(&arg);
00696 else if (arg_match(&arg, &resize_up_thresh, argi))
00697 cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
00698 else if (arg_match(&arg, &resize_down_thresh, argi))
00699 cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
00700 else if (arg_match(&arg, &resize_down_thresh, argi))
00701 cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
00702 else if (arg_match(&arg, &end_usage, argi))
00703 cfg.rc_end_usage = arg_parse_uint(&arg);
00704 else if (arg_match(&arg, &target_bitrate, argi))
00705 cfg.rc_target_bitrate = arg_parse_uint(&arg);
00706 else if (arg_match(&arg, &min_quantizer, argi))
00707 cfg.rc_min_quantizer = arg_parse_uint(&arg);
00708 else if (arg_match(&arg, &max_quantizer, argi))
00709 cfg.rc_max_quantizer = arg_parse_uint(&arg);
00710 else if (arg_match(&arg, &undershoot_pct, argi))
00711 cfg.rc_undershoot_pct = arg_parse_uint(&arg);
00712 else if (arg_match(&arg, &overshoot_pct, argi))
00713 cfg.rc_overshoot_pct = arg_parse_uint(&arg);
00714 else if (arg_match(&arg, &buf_sz, argi))
00715 cfg.rc_buf_sz = arg_parse_uint(&arg);
00716 else if (arg_match(&arg, &buf_initial_sz, argi))
00717 cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
00718 else if (arg_match(&arg, &buf_optimal_sz, argi))
00719 cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
00720 else if (arg_match(&arg, &bias_pct, argi))
00721 {
00722 cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
00723
00724 if (arg_passes < 2)
00725 printf("Warning: option %s ignored in one-pass mode.\n",
00726 arg.name);
00727 }
00728 else if (arg_match(&arg, &minsection_pct, argi))
00729 {
00730 cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
00731
00732 if (arg_passes < 2)
00733 printf("Warning: option %s ignored in one-pass mode.\n",
00734 arg.name);
00735 }
00736 else if (arg_match(&arg, &maxsection_pct, argi))
00737 {
00738 cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
00739
00740 if (arg_passes < 2)
00741 printf("Warning: option %s ignored in one-pass mode.\n",
00742 arg.name);
00743 }
00744 else if (arg_match(&arg, &kf_min_dist, argi))
00745 cfg.kf_min_dist = arg_parse_uint(&arg);
00746 else if (arg_match(&arg, &kf_max_dist, argi))
00747 cfg.kf_max_dist = arg_parse_uint(&arg);
00748 else
00749 argj++;
00750 }
00751
00752
00753 #if CONFIG_VP8_ENCODER
00754
00755 if (codec->iface == &vpx_codec_vp8_cx_algo)
00756 {
00757 ctrl_args = vp8_args;
00758 ctrl_args_map = vp8_arg_ctrl_map;
00759 }
00760
00761 #endif
00762
00763 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
00764 {
00765 int match = 0;
00766
00767 arg.argv_step = 1;
00768
00769 for (i = 0; ctrl_args[i]; i++)
00770 {
00771 if (arg_match(&arg, ctrl_args[i], argi))
00772 {
00773 match = 1;
00774
00775 if (arg_ctrl_cnt < ARG_CTRL_CNT_MAX)
00776 {
00777 arg_ctrls[arg_ctrl_cnt][0] = ctrl_args_map[i];
00778 arg_ctrls[arg_ctrl_cnt][1] = arg_parse_int(&arg);
00779 arg_ctrl_cnt++;
00780 }
00781 }
00782 }
00783
00784 if (!match)
00785 argj++;
00786 }
00787
00788
00789 for (argi = argv; *argi; argi++)
00790 if (argi[0][0] == '-')
00791 die("Error: Unrecognized option %s\n", *argi);
00792
00793
00794 in_fn = argv[0];
00795 out_fn = argv[1];
00796
00797 if (!in_fn || !out_fn)
00798 usage_exit();
00799
00800
00801 infile = fopen(in_fn, "rb");
00802
00803 if (!infile)
00804 {
00805 printf("Failed to open input file");
00806 return EXIT_FAILURE;
00807 }
00808
00809 is_ivf = file_is_ivf(infile, &fourcc, &cfg.g_w, &cfg.g_h);
00810
00811 if (is_ivf)
00812 {
00813 switch (fourcc)
00814 {
00815 case 0x32315659:
00816 arg_use_i420 = 0;
00817 break;
00818 case 0x30323449:
00819 arg_use_i420 = 1;
00820 break;
00821 default:
00822 printf("Unsupported fourcc (%08x) in IVF\n", fourcc);
00823 return EXIT_FAILURE;
00824 }
00825 }
00826
00827 fclose(infile);
00828
00829
00830 #define SHOW(field) printf(" %-28s = %d\n", #field, cfg.field)
00831
00832 if (verbose)
00833 {
00834 printf("Codec: %s\n", vpx_codec_iface_name(codec->iface));
00835 printf("Source file: %s Format: %s\n", in_fn, arg_use_i420 ? "I420" : "YV12");
00836 printf("Destination file: %s\n", out_fn);
00837 printf("Encoder parameters:\n");
00838
00839 SHOW(g_usage);
00840 SHOW(g_threads);
00841 SHOW(g_profile);
00842 SHOW(g_w);
00843 SHOW(g_h);
00844 SHOW(g_timebase.num);
00845 SHOW(g_timebase.den);
00846 SHOW(g_error_resilient);
00847 SHOW(g_pass);
00848 SHOW(g_lag_in_frames);
00849 SHOW(rc_dropframe_thresh);
00850 SHOW(rc_resize_allowed);
00851 SHOW(rc_resize_up_thresh);
00852 SHOW(rc_resize_down_thresh);
00853 SHOW(rc_end_usage);
00854 SHOW(rc_target_bitrate);
00855 SHOW(rc_min_quantizer);
00856 SHOW(rc_max_quantizer);
00857 SHOW(rc_undershoot_pct);
00858 SHOW(rc_overshoot_pct);
00859 SHOW(rc_buf_sz);
00860 SHOW(rc_buf_initial_sz);
00861 SHOW(rc_buf_optimal_sz);
00862 SHOW(rc_2pass_vbr_bias_pct);
00863 SHOW(rc_2pass_vbr_minsection_pct);
00864 SHOW(rc_2pass_vbr_maxsection_pct);
00865 SHOW(kf_mode);
00866 SHOW(kf_min_dist);
00867 SHOW(kf_max_dist);
00868 }
00869
00870 vpx_img_alloc(&raw, arg_use_i420 ? IMG_FMT_I420 : IMG_FMT_YV12,
00871 cfg.g_w, cfg.g_h, 1);
00872
00873
00874
00875
00876
00877
00878 cfg.g_timebase.den *= 2;
00879
00880 memset(&stats, 0, sizeof(stats));
00881
00882 for (pass = one_pass_only ? one_pass_only - 1 : 0; pass < arg_passes; pass++)
00883 {
00884 int frames_in = 0, frames_out = 0;
00885 unsigned long nbytes = 0;
00886
00887 infile = fopen(in_fn, "rb");
00888
00889 if (!infile)
00890 {
00891 printf("Failed to open input file");
00892 return EXIT_FAILURE;
00893 }
00894
00895 outfile = fopen(out_fn, "wb");
00896
00897 if (!outfile)
00898 {
00899 printf("Failed to open output file");
00900 return EXIT_FAILURE;
00901 }
00902
00903 if (stats_fn)
00904 {
00905 if (!stats_open_file(&stats, stats_fn, pass))
00906 {
00907 printf("Failed to open statistics store\n");
00908 return EXIT_FAILURE;
00909 }
00910 }
00911 else
00912 {
00913 if (!stats_open_mem(&stats, pass))
00914 {
00915 printf("Failed to open statistics store\n");
00916 return EXIT_FAILURE;
00917 }
00918 }
00919
00920 cfg.g_pass = arg_passes == 2
00921 ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS
00922 : VPX_RC_ONE_PASS;
00923 #if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
00924
00925 if (pass)
00926 {
00927 cfg.rc_twopass_stats_in = stats_get(&stats);
00928 }
00929
00930 #endif
00931
00932 write_ivf_file_header(outfile, &cfg, codec->fourcc, 0);
00933
00934
00935
00936 if (cfg.kf_min_dist == cfg.kf_max_dist)
00937 cfg.kf_mode = VPX_KF_FIXED;
00938
00939 vpx_codec_enc_init(&encoder, codec->iface, &cfg,
00940 show_psnr ? VPX_CODEC_USE_PSNR : 0);
00941 ctx_exit_on_error(&encoder, "Failed to initialize encoder");
00942
00943
00944
00945
00946
00947 for (i = 0; i < arg_ctrl_cnt; i++)
00948 {
00949 if (vpx_codec_control_(&encoder, arg_ctrls[i][0], arg_ctrls[i][1]))
00950 printf("Error: Tried to set control %d = %d\n",
00951 arg_ctrls[i][0], arg_ctrls[i][1]);
00952
00953 ctx_exit_on_error(&encoder, "Failed to control codec");
00954 }
00955
00956 frame_avail = 1;
00957 got_data = 0;
00958
00959 while (frame_avail || got_data)
00960 {
00961 vpx_codec_iter_t iter = NULL;
00962 const vpx_codec_cx_pkt_t *pkt;
00963 struct vpx_usec_timer timer;
00964
00965 if (!arg_limit || frames_in < arg_limit)
00966 {
00967 frame_avail = read_frame(infile, &raw, is_ivf);
00968
00969 if (frame_avail)
00970 frames_in++;
00971
00972 printf("\rPass %d/%d frame %4d/%-4d %7ldB \033[K", pass + 1,
00973 arg_passes, frames_in, frames_out, nbytes);
00974 }
00975 else
00976 frame_avail = 0;
00977
00978 vpx_usec_timer_start(&timer);
00979
00980
00981
00982 vpx_codec_encode(&encoder, frame_avail ? &raw : NULL, (frames_in - 1) * 2,
00983 2, 0, arg_deadline);
00984 vpx_usec_timer_mark(&timer);
00985 cx_time += vpx_usec_timer_elapsed(&timer);
00986 ctx_exit_on_error(&encoder, "Failed to encode frame");
00987 got_data = 0;
00988
00989 while ((pkt = vpx_codec_get_cx_data(&encoder, &iter)))
00990 {
00991 got_data = 1;
00992 nbytes += pkt->data.raw.sz;
00993
00994 switch (pkt->kind)
00995 {
00996 case VPX_CODEC_CX_FRAME_PKT:
00997 frames_out++;
00998 printf(" %6luF",
00999 (unsigned long)pkt->data.frame.sz);
01000 write_ivf_frame_header(outfile, pkt);
01001 fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile);
01002 break;
01003 case VPX_CODEC_STATS_PKT:
01004 frames_out++;
01005 printf(" %6luS",
01006 (unsigned long)pkt->data.twopass_stats.sz);
01007 stats_write(&stats,
01008 pkt->data.twopass_stats.buf,
01009 pkt->data.twopass_stats.sz);
01010 break;
01011 case VPX_CODEC_PSNR_PKT:
01012
01013 if (show_psnr)
01014 {
01015 int i;
01016
01017 for (i = 0; i < 4; i++)
01018 printf("%.3lf ", pkt->data.psnr.psnr[i]);
01019 }
01020
01021 break;
01022 default:
01023 break;
01024 }
01025 }
01026
01027 fflush(stdout);
01028 }
01029
01030
01031
01032
01033 printf("\rPass %d/%d frame %4d/%-4d %7ldB %7ldb/f %7"PRId64"b/s"
01034 " %7lu %s (%.2f fps)\033[K", pass + 1,
01035 arg_passes, frames_in, frames_out, nbytes, nbytes * 8 / frames_in,
01036 nbytes * 8 *(int64_t)cfg.g_timebase.den / cfg.g_timebase.num / frames_in,
01037 cx_time > 9999999 ? cx_time / 1000 : cx_time,
01038 cx_time > 9999999 ? "ms" : "us",
01039 (float)frames_in * 1000000.0 / (float)cx_time);
01040
01041 vpx_codec_destroy(&encoder);
01042
01043 fclose(infile);
01044
01045 if (!fseek(outfile, 0, SEEK_SET))
01046 write_ivf_file_header(outfile, &cfg, codec->fourcc, frames_out);
01047
01048 fclose(outfile);
01049 stats_close(&stats);
01050 printf("\n");
01051
01052 if (one_pass_only)
01053 break;
01054 }
01055
01056 vpx_img_free(&raw);
01057 free(argv);
01058 return EXIT_SUCCESS;
01059 }