blob: ab6e10aa964b679212fc4bba132f7a7bd42691dd [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar3245d462010-06-10 17:10:12 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * 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
John Koleszar3245d462010-06-10 17:10:12 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
11
12/* This is a simple program that encodes YV12 files and generates ivf
13 * files using the new interface.
14 */
Frank Galligan3419e4d2010-06-10 16:59:21 -040015#if defined(_WIN32)
John Koleszar1df03142010-05-20 14:44:18 -040016#define USE_POSIX_MMAP 0
17#else
18#define USE_POSIX_MMAP 1
19#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -040020
21#include <stdio.h>
22#include <stdlib.h>
23#include <stdarg.h>
24#include <string.h>
John Koleszarb7492342010-05-24 11:39:59 -040025#include "vpx/vpx_encoder.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040026#if USE_POSIX_MMAP
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/mman.h>
30#include <fcntl.h>
31#include <unistd.h>
32#endif
John Koleszarb7492342010-05-24 11:39:59 -040033#include "vpx/vp8cx.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040034#include "vpx_ports/mem_ops.h"
35#include "vpx_ports/vpx_timer.h"
Timothy B. Terriberry44d89492010-05-26 18:27:51 -040036#include "y4minput.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040037
38static const char *exec_name;
39
40static const struct codec_item
41{
42 char const *name;
43 const vpx_codec_iface_t *iface;
44 unsigned int fourcc;
45} codecs[] =
46{
47#if CONFIG_VP8_ENCODER
48 {"vp8", &vpx_codec_vp8_cx_algo, 0x30385056},
49#endif
50};
51
52static void usage_exit();
53
54void die(const char *fmt, ...)
55{
56 va_list ap;
57 va_start(ap, fmt);
John Koleszar3245d462010-06-10 17:10:12 -040058 vfprintf(stderr, fmt, ap);
59 fprintf(stderr, "\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -040060 usage_exit();
61}
62
63static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s)
64{
65 if (ctx->err)
66 {
67 const char *detail = vpx_codec_error_detail(ctx);
68
John Koleszar3245d462010-06-10 17:10:12 -040069 fprintf(stderr, "%s: %s\n", s, vpx_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -040070
71 if (detail)
John Koleszar3245d462010-06-10 17:10:12 -040072 fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -040073
74 exit(EXIT_FAILURE);
75 }
76}
77
78/* This structure is used to abstract the different ways of handling
79 * first pass statistics.
80 */
81typedef struct
82{
83 vpx_fixed_buf_t buf;
84 int pass;
85 FILE *file;
86 char *buf_ptr;
87 size_t buf_alloc_sz;
88} stats_io_t;
89
90int stats_open_file(stats_io_t *stats, const char *fpf, int pass)
91{
92 int res;
93
94 stats->pass = pass;
95
96 if (pass == 0)
97 {
98 stats->file = fopen(fpf, "wb");
99 stats->buf.sz = 0;
100 stats->buf.buf = NULL,
101 res = (stats->file != NULL);
102 }
103 else
104 {
105#if 0
106#elif USE_POSIX_MMAP
107 struct stat stat_buf;
108 int fd;
109
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,
115 fd, 0);
116 res = (stats->buf.buf != NULL);
117#else
118 size_t nbytes;
119
120 stats->file = fopen(fpf, "rb");
121
122 if (fseek(stats->file, 0, SEEK_END))
123 {
124 fprintf(stderr, "First-pass stats file must be seekable!\n");
125 exit(EXIT_FAILURE);
126 }
127
128 stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
129 rewind(stats->file);
130
131 stats->buf.buf = malloc(stats->buf_alloc_sz);
132
133 if (!stats->buf.buf)
134 {
135 fprintf(stderr, "Failed to allocate first-pass stats buffer (%d bytes)\n",
136 stats->buf_alloc_sz);
137 exit(EXIT_FAILURE);
138 }
139
140 nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
141 res = (nbytes == stats->buf.sz);
142#endif
143 }
144
145 return res;
146}
147
148int stats_open_mem(stats_io_t *stats, int pass)
149{
150 int res;
151 stats->pass = pass;
152
153 if (!pass)
154 {
155 stats->buf.sz = 0;
156 stats->buf_alloc_sz = 64 * 1024;
157 stats->buf.buf = malloc(stats->buf_alloc_sz);
158 }
159
160 stats->buf_ptr = stats->buf.buf;
161 res = (stats->buf.buf != NULL);
162 return res;
163}
164
165
166void stats_close(stats_io_t *stats)
167{
168 if (stats->file)
169 {
170 if (stats->pass == 1)
171 {
172#if 0
173#elif USE_POSIX_MMAP
174 munmap(stats->buf.buf, stats->buf.sz);
175#else
176 free(stats->buf.buf);
177#endif
178 }
179
180 fclose(stats->file);
181 stats->file = NULL;
182 }
183 else
184 {
185 if (stats->pass == 1)
186 free(stats->buf.buf);
187 }
188}
189
190void stats_write(stats_io_t *stats, const void *pkt, size_t len)
191{
192 if (stats->file)
193 {
194 fwrite(pkt, 1, len, stats->file);
195 }
196 else
197 {
198 if (stats->buf.sz + len > stats->buf_alloc_sz)
199 {
200 size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
201 char *new_ptr = realloc(stats->buf.buf, new_sz);
202
203 if (new_ptr)
204 {
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;
208 } /* else ... */
209 }
210
211 memcpy(stats->buf_ptr, pkt, len);
212 stats->buf.sz += len;
213 stats->buf_ptr += len;
214 }
215}
216
217vpx_fixed_buf_t stats_get(stats_io_t *stats)
218{
219 return stats->buf;
220}
221
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400222enum video_file_type
223{
224 FILE_TYPE_RAW,
225 FILE_TYPE_IVF,
226 FILE_TYPE_Y4M
227};
228
John Koleszar3245d462010-06-10 17:10:12 -0400229struct detect_buffer {
230 char buf[4];
231 int valid;
232};
233
234
John Koleszar0ea50ce2010-05-18 11:58:33 -0400235#define IVF_FRAME_HDR_SZ (4+8) /* 4 byte size + 8 byte timestamp */
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400236static int read_frame(FILE *f, vpx_image_t *img, unsigned int file_type,
John Koleszar3245d462010-06-10 17:10:12 -0400237 y4m_input *y4m, struct detect_buffer *detect)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400238{
239 int plane = 0;
240
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400241 if (file_type == FILE_TYPE_Y4M)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400242 {
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400243 if (y4m_input_fetch_frame(y4m, f, img) < 0)
244 return 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400245 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400246 else
John Koleszar0ea50ce2010-05-18 11:58:33 -0400247 {
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400248 if (file_type == FILE_TYPE_IVF)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400249 {
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400250 char junk[IVF_FRAME_HDR_SZ];
251
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
254 * layout.
255 */
256 fread(junk, 1, IVF_FRAME_HDR_SZ, f);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400257 }
258
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400259 for (plane = 0; plane < 3; plane++)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400260 {
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400261 unsigned char *ptr;
262 int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
263 int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
264 int r;
265
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
268 * the data on disk.
269 */
270 switch (plane)
271 {
272 case 1:
273 ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12? VPX_PLANE_V : VPX_PLANE_U];
274 break;
275 case 2:
276 ptr = img->planes[img->fmt==VPX_IMG_FMT_YV12?VPX_PLANE_U : VPX_PLANE_V];
277 break;
278 default:
279 ptr = img->planes[plane];
280 }
281
282 for (r = 0; r < h; r++)
283 {
John Koleszar3245d462010-06-10 17:10:12 -0400284 if (detect->valid)
285 {
286 memcpy(ptr, detect->buf, 4);
287 fread(ptr+4, 1, w-4, f);
288 detect->valid = 0;
289 }
290 else
291 fread(ptr, 1, w, f);
292
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400293 ptr += img->stride[plane];
294 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400295 }
296 }
297
298 return !feof(f);
299}
300
301
Tom Finegane703af92010-06-16 13:24:55 -0400302unsigned int file_is_y4m(FILE *infile,
John Koleszar3245d462010-06-10 17:10:12 -0400303 y4m_input *y4m,
304 char detect[4])
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400305{
Tom Finegane703af92010-06-16 13:24:55 -0400306 if(memcmp(detect, "YUV4", 4) == 0)
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400307 {
308 return 1;
309 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400310 return 0;
311}
312
John Koleszar0ea50ce2010-05-18 11:58:33 -0400313#define IVF_FILE_HDR_SZ (32)
314unsigned int file_is_ivf(FILE *infile,
315 unsigned int *fourcc,
316 unsigned int *width,
John Koleszar3245d462010-06-10 17:10:12 -0400317 unsigned int *height,
318 char detect[4])
John Koleszar0ea50ce2010-05-18 11:58:33 -0400319{
320 char raw_hdr[IVF_FILE_HDR_SZ];
321 int is_ivf = 0;
322
John Koleszar3245d462010-06-10 17:10:12 -0400323 if(memcmp(detect, "DKIF", 4) != 0)
324 return 0;
325
John Koleszar0ea50ce2010-05-18 11:58:33 -0400326 /* See write_ivf_file_header() for more documentation on the file header
327 * layout.
328 */
John Koleszar3245d462010-06-10 17:10:12 -0400329 if (fread(raw_hdr + 4, 1, IVF_FILE_HDR_SZ - 4, infile)
330 == IVF_FILE_HDR_SZ - 4)
John Koleszar0ea50ce2010-05-18 11:58:33 -0400331 {
John Koleszar0ea50ce2010-05-18 11:58:33 -0400332 {
333 is_ivf = 1;
334
335 if (mem_get_le16(raw_hdr + 4) != 0)
336 fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
337 " decode properly.");
338
339 *fourcc = mem_get_le32(raw_hdr + 8);
340 }
341 }
342
343 if (is_ivf)
344 {
345 *width = mem_get_le16(raw_hdr + 12);
346 *height = mem_get_le16(raw_hdr + 14);
347 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400348
349 return is_ivf;
350}
351
352
353static void write_ivf_file_header(FILE *outfile,
354 const vpx_codec_enc_cfg_t *cfg,
355 unsigned int fourcc,
356 int frame_cnt)
357{
358 char header[32];
359
360 if (cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
361 return;
362
363 header[0] = 'D';
364 header[1] = 'K';
365 header[2] = 'I';
366 header[3] = 'F';
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 */
376
377 fwrite(header, 1, 32, outfile);
378}
379
380
381static void write_ivf_frame_header(FILE *outfile,
382 const vpx_codec_cx_pkt_t *pkt)
383{
384 char header[12];
385 vpx_codec_pts_t pts;
386
387 if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
388 return;
389
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);
394
395 fwrite(header, 1, 12, outfile);
396}
397
398#include "args.h"
399
400static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
401 "Input file is YV12 ");
402static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
403 "Input file is I420 (default)");
404static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
405 "Codec to use");
406static const arg_def_t passes = ARG_DEF("p", "passes", 1,
407 "Number of passes (1/2)");
408static const arg_def_t pass_arg = ARG_DEF(NULL, "pass", 1,
409 "Pass to execute (1/2)");
410static const arg_def_t fpf_name = ARG_DEF(NULL, "fpf", 1,
411 "First pass statistics file name");
412static const arg_def_t limit = ARG_DEF(NULL, "limit", 1,
413 "Stop encoding after n input frames");
414static const arg_def_t deadline = ARG_DEF("d", "deadline", 1,
415 "Deadline per frame (usec)");
416static const arg_def_t best_dl = ARG_DEF(NULL, "best", 0,
417 "Use Best Quality Deadline");
418static const arg_def_t good_dl = ARG_DEF(NULL, "good", 0,
419 "Use Good Quality Deadline");
420static const arg_def_t rt_dl = ARG_DEF(NULL, "rt", 0,
421 "Use Realtime Quality Deadline");
422static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0,
423 "Show encoder parameters");
424static const arg_def_t psnrarg = ARG_DEF(NULL, "psnr", 0,
425 "Show PSNR in status line");
Frank Galligan1258cf62010-10-06 12:51:00 -0400426static const arg_def_t framerate = ARG_DEF(NULL, "framerate", 1,
427 "Stream frame rate (rate/scale)");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400428static const arg_def_t *main_args[] =
429{
430 &codecarg, &passes, &pass_arg, &fpf_name, &limit, &deadline, &best_dl, &good_dl, &rt_dl,
Frank Galligan1258cf62010-10-06 12:51:00 -0400431 &verbosearg, &psnrarg, &framerate,
John Koleszar0ea50ce2010-05-18 11:58:33 -0400432 NULL
433};
434
435static const arg_def_t usage = ARG_DEF("u", "usage", 1,
436 "Usage profile number to use");
437static const arg_def_t threads = ARG_DEF("t", "threads", 1,
438 "Max number of threads to use");
439static const arg_def_t profile = ARG_DEF(NULL, "profile", 1,
440 "Bitstream profile number to use");
441static const arg_def_t width = ARG_DEF("w", "width", 1,
442 "Frame width");
443static const arg_def_t height = ARG_DEF("h", "height", 1,
444 "Frame height");
445static const arg_def_t timebase = ARG_DEF(NULL, "timebase", 1,
446 "Stream timebase (frame duration)");
447static const arg_def_t error_resilient = ARG_DEF(NULL, "error-resilient", 1,
448 "Enable error resiliency features");
449static const arg_def_t lag_in_frames = ARG_DEF(NULL, "lag-in-frames", 1,
450 "Max number of frames to lag");
451
452static const arg_def_t *global_args[] =
453{
454 &use_yv12, &use_i420, &usage, &threads, &profile,
Frank Galligan1258cf62010-10-06 12:51:00 -0400455 &width, &height, &timebase, &framerate, &error_resilient,
John Koleszar0ea50ce2010-05-18 11:58:33 -0400456 &lag_in_frames, NULL
457};
458
459static const arg_def_t dropframe_thresh = ARG_DEF(NULL, "drop-frame", 1,
460 "Temporal resampling threshold (buf %)");
461static const arg_def_t resize_allowed = ARG_DEF(NULL, "resize-allowed", 1,
462 "Spatial resampling enabled (bool)");
463static const arg_def_t resize_up_thresh = ARG_DEF(NULL, "resize-up", 1,
464 "Upscale threshold (buf %)");
465static const arg_def_t resize_down_thresh = ARG_DEF(NULL, "resize-down", 1,
466 "Downscale threshold (buf %)");
467static const arg_def_t end_usage = ARG_DEF(NULL, "end-usage", 1,
468 "VBR=0 | CBR=1");
469static const arg_def_t target_bitrate = ARG_DEF(NULL, "target-bitrate", 1,
470 "Bitrate (kbps)");
471static const arg_def_t min_quantizer = ARG_DEF(NULL, "min-q", 1,
472 "Minimum (best) quantizer");
473static const arg_def_t max_quantizer = ARG_DEF(NULL, "max-q", 1,
474 "Maximum (worst) quantizer");
475static const arg_def_t undershoot_pct = ARG_DEF(NULL, "undershoot-pct", 1,
476 "Datarate undershoot (min) target (%)");
477static const arg_def_t overshoot_pct = ARG_DEF(NULL, "overshoot-pct", 1,
478 "Datarate overshoot (max) target (%)");
479static const arg_def_t buf_sz = ARG_DEF(NULL, "buf-sz", 1,
480 "Client buffer size (ms)");
481static const arg_def_t buf_initial_sz = ARG_DEF(NULL, "buf-initial-sz", 1,
482 "Client initial buffer size (ms)");
483static const arg_def_t buf_optimal_sz = ARG_DEF(NULL, "buf-optimal-sz", 1,
484 "Client optimal buffer size (ms)");
485static const arg_def_t *rc_args[] =
486{
487 &dropframe_thresh, &resize_allowed, &resize_up_thresh, &resize_down_thresh,
488 &end_usage, &target_bitrate, &min_quantizer, &max_quantizer,
489 &undershoot_pct, &overshoot_pct, &buf_sz, &buf_initial_sz, &buf_optimal_sz,
490 NULL
491};
492
493
494static const arg_def_t bias_pct = ARG_DEF(NULL, "bias-pct", 1,
495 "CBR/VBR bias (0=CBR, 100=VBR)");
496static const arg_def_t minsection_pct = ARG_DEF(NULL, "minsection-pct", 1,
497 "GOP min bitrate (% of target)");
498static const arg_def_t maxsection_pct = ARG_DEF(NULL, "maxsection-pct", 1,
499 "GOP max bitrate (% of target)");
500static const arg_def_t *rc_twopass_args[] =
501{
502 &bias_pct, &minsection_pct, &maxsection_pct, NULL
503};
504
505
506static const arg_def_t kf_min_dist = ARG_DEF(NULL, "kf-min-dist", 1,
507 "Minimum keyframe interval (frames)");
508static const arg_def_t kf_max_dist = ARG_DEF(NULL, "kf-max-dist", 1,
509 "Maximum keyframe interval (frames)");
John Koleszarb1e36f22010-06-24 08:41:51 -0400510static const arg_def_t kf_disabled = ARG_DEF(NULL, "disable-kf", 0,
511 "Disable keyframe placement");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400512static const arg_def_t *kf_args[] =
513{
John Koleszarb1e36f22010-06-24 08:41:51 -0400514 &kf_min_dist, &kf_max_dist, &kf_disabled, NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -0400515};
516
517
518#if CONFIG_VP8_ENCODER
519static const arg_def_t noise_sens = ARG_DEF(NULL, "noise-sensitivity", 1,
520 "Noise sensitivity (frames to blur)");
521static const arg_def_t sharpness = ARG_DEF(NULL, "sharpness", 1,
522 "Filter sharpness (0-7)");
523static const arg_def_t static_thresh = ARG_DEF(NULL, "static-thresh", 1,
524 "Motion detection threshold");
525#endif
526
527#if CONFIG_VP8_ENCODER
528static const arg_def_t cpu_used = ARG_DEF(NULL, "cpu-used", 1,
529 "CPU Used (-16..16)");
530#endif
531
532
533#if CONFIG_VP8_ENCODER
534static const arg_def_t token_parts = ARG_DEF(NULL, "token-parts", 1,
535 "Number of token partitions to use, log2");
536static const arg_def_t auto_altref = ARG_DEF(NULL, "auto-alt-ref", 1,
537 "Enable automatic alt reference frames");
538static const arg_def_t arnr_maxframes = ARG_DEF(NULL, "arnr-maxframes", 1,
539 "alt_ref Max Frames");
540static const arg_def_t arnr_strength = ARG_DEF(NULL, "arnr-strength", 1,
541 "alt_ref Strength");
542static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1,
543 "alt_ref Type");
544
545static const arg_def_t *vp8_args[] =
546{
547 &cpu_used, &auto_altref, &noise_sens, &sharpness, &static_thresh,
548 &token_parts, &arnr_maxframes, &arnr_strength, &arnr_type, NULL
549};
550static const int vp8_arg_ctrl_map[] =
551{
552 VP8E_SET_CPUUSED, VP8E_SET_ENABLEAUTOALTREF,
553 VP8E_SET_NOISE_SENSITIVITY, VP8E_SET_SHARPNESS, VP8E_SET_STATIC_THRESHOLD,
554 VP8E_SET_TOKEN_PARTITIONS,
555 VP8E_SET_ARNR_MAXFRAMES, VP8E_SET_ARNR_STRENGTH , VP8E_SET_ARNR_TYPE, 0
556};
557#endif
558
559static const arg_def_t *no_args[] = { NULL };
560
561static void usage_exit()
562{
563 int i;
564
John Koleszar3245d462010-06-10 17:10:12 -0400565 fprintf(stderr, "Usage: %s <options> src_filename dst_filename\n", exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400566
John Koleszar78f2d3e2010-10-11 09:41:14 -0400567 fprintf(stderr, "\nOptions:\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400568 arg_show_usage(stdout, main_args);
John Koleszar78f2d3e2010-10-11 09:41:14 -0400569 fprintf(stderr, "\nEncoder Global Options:\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400570 arg_show_usage(stdout, global_args);
John Koleszar78f2d3e2010-10-11 09:41:14 -0400571 fprintf(stderr, "\nRate Control Options:\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400572 arg_show_usage(stdout, rc_args);
John Koleszar78f2d3e2010-10-11 09:41:14 -0400573 fprintf(stderr, "\nTwopass Rate Control Options:\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400574 arg_show_usage(stdout, rc_twopass_args);
John Koleszar78f2d3e2010-10-11 09:41:14 -0400575 fprintf(stderr, "\nKeyframe Placement Options:\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400576 arg_show_usage(stdout, kf_args);
577#if CONFIG_VP8_ENCODER
John Koleszar78f2d3e2010-10-11 09:41:14 -0400578 fprintf(stderr, "\nVP8 Specific Options:\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400579 arg_show_usage(stdout, vp8_args);
580#endif
John Koleszar3245d462010-06-10 17:10:12 -0400581 fprintf(stderr, "\n"
John Koleszar0ea50ce2010-05-18 11:58:33 -0400582 "Included encoders:\n"
583 "\n");
584
585 for (i = 0; i < sizeof(codecs) / sizeof(codecs[0]); i++)
John Koleszar3245d462010-06-10 17:10:12 -0400586 fprintf(stderr, " %-6s - %s\n",
John Koleszar0ea50ce2010-05-18 11:58:33 -0400587 codecs[i].name,
588 vpx_codec_iface_name(codecs[i].iface));
589
590 exit(EXIT_FAILURE);
591}
592
593#define ARG_CTRL_CNT_MAX 10
594
595
596int main(int argc, const char **argv_)
597{
598 vpx_codec_ctx_t encoder;
599 const char *in_fn = NULL, *out_fn = NULL, *stats_fn = NULL;
600 int i;
601 FILE *infile, *outfile;
602 vpx_codec_enc_cfg_t cfg;
603 vpx_codec_err_t res;
604 int pass, one_pass_only = 0;
605 stats_io_t stats;
606 vpx_image_t raw;
607 const struct codec_item *codec = codecs;
608 int frame_avail, got_data;
609
610 struct arg arg;
611 char **argv, **argi, **argj;
612 int arg_usage = 0, arg_passes = 1, arg_deadline = 0;
613 int arg_ctrls[ARG_CTRL_CNT_MAX][2], arg_ctrl_cnt = 0;
614 int arg_limit = 0;
615 static const arg_def_t **ctrl_args = no_args;
616 static const int *ctrl_args_map = NULL;
617 int verbose = 0, show_psnr = 0;
618 int arg_use_i420 = 1;
619 unsigned long cx_time = 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400620 unsigned int file_type, fourcc;
621 y4m_input y4m;
Frank Galligan1258cf62010-10-06 12:51:00 -0400622 struct vpx_rational arg_framerate = {30, 1};
623 int arg_have_framerate = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400624
625 exec_name = argv_[0];
626
627 if (argc < 3)
628 usage_exit();
629
630
631 /* First parse the codec and usage values, because we want to apply other
632 * parameters on top of the default configuration provided by the codec.
633 */
634 argv = argv_dup(argc - 1, argv_ + 1);
635
636 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
637 {
638 arg.argv_step = 1;
639
640 if (arg_match(&arg, &codecarg, argi))
641 {
642 int j, k = -1;
643
644 for (j = 0; j < sizeof(codecs) / sizeof(codecs[0]); j++)
645 if (!strcmp(codecs[j].name, arg.val))
646 k = j;
647
648 if (k >= 0)
649 codec = codecs + k;
650 else
651 die("Error: Unrecognized argument (%s) to --codec\n",
652 arg.val);
653
654 }
655 else if (arg_match(&arg, &passes, argi))
656 {
657 arg_passes = arg_parse_uint(&arg);
658
659 if (arg_passes < 1 || arg_passes > 2)
660 die("Error: Invalid number of passes (%d)\n", arg_passes);
661 }
662 else if (arg_match(&arg, &pass_arg, argi))
663 {
664 one_pass_only = arg_parse_uint(&arg);
665
666 if (one_pass_only < 1 || one_pass_only > 2)
667 die("Error: Invalid pass selected (%d)\n", one_pass_only);
668 }
669 else if (arg_match(&arg, &fpf_name, argi))
670 stats_fn = arg.val;
671 else if (arg_match(&arg, &usage, argi))
672 arg_usage = arg_parse_uint(&arg);
673 else if (arg_match(&arg, &deadline, argi))
674 arg_deadline = arg_parse_uint(&arg);
675 else if (arg_match(&arg, &best_dl, argi))
676 arg_deadline = VPX_DL_BEST_QUALITY;
677 else if (arg_match(&arg, &good_dl, argi))
678 arg_deadline = VPX_DL_GOOD_QUALITY;
679 else if (arg_match(&arg, &rt_dl, argi))
680 arg_deadline = VPX_DL_REALTIME;
681 else if (arg_match(&arg, &use_yv12, argi))
682 {
683 arg_use_i420 = 0;
684 }
685 else if (arg_match(&arg, &use_i420, argi))
686 {
687 arg_use_i420 = 1;
688 }
689 else if (arg_match(&arg, &verbosearg, argi))
690 verbose = 1;
691 else if (arg_match(&arg, &limit, argi))
692 arg_limit = arg_parse_uint(&arg);
693 else if (arg_match(&arg, &psnrarg, argi))
694 show_psnr = 1;
Frank Galligan1258cf62010-10-06 12:51:00 -0400695 else if (arg_match(&arg, &framerate, argi))
696 {
697 arg_framerate = arg_parse_rational(&arg);
698 arg_have_framerate = 1;
699 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400700 else
701 argj++;
702 }
703
704 /* Ensure that --passes and --pass are consistent. If --pass is set and --passes=2,
705 * ensure --fpf was set.
706 */
707 if (one_pass_only)
708 {
709 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
710 if (one_pass_only > arg_passes)
711 {
John Koleszar3245d462010-06-10 17:10:12 -0400712 fprintf(stderr, "Warning: Assuming --pass=%d implies --passes=%d\n",
John Koleszar0ea50ce2010-05-18 11:58:33 -0400713 one_pass_only, one_pass_only);
714 arg_passes = one_pass_only;
715 }
716
717 if (arg_passes == 2 && !stats_fn)
718 die("Must specify --fpf when --pass=%d and --passes=2\n", one_pass_only);
719 }
720
721 /* Populate encoder configuration */
722 res = vpx_codec_enc_config_default(codec->iface, &cfg, arg_usage);
723
724 if (res)
725 {
John Koleszar3245d462010-06-10 17:10:12 -0400726 fprintf(stderr, "Failed to get config: %s\n",
727 vpx_codec_err_to_string(res));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400728 return EXIT_FAILURE;
729 }
730
Frank Galligan1258cf62010-10-06 12:51:00 -0400731 /* Change the default timebase to a high enough value so that the encoder
732 * will always create strictly increasing timestamps.
733 */
734 cfg.g_timebase.den = 1000;
735
John Koleszar0ea50ce2010-05-18 11:58:33 -0400736 /* Now parse the remainder of the parameters. */
737 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
738 {
739 arg.argv_step = 1;
740
741 if (0);
742 else if (arg_match(&arg, &threads, argi))
743 cfg.g_threads = arg_parse_uint(&arg);
744 else if (arg_match(&arg, &profile, argi))
745 cfg.g_profile = arg_parse_uint(&arg);
746 else if (arg_match(&arg, &width, argi))
747 cfg.g_w = arg_parse_uint(&arg);
748 else if (arg_match(&arg, &height, argi))
749 cfg.g_h = arg_parse_uint(&arg);
750 else if (arg_match(&arg, &timebase, argi))
751 cfg.g_timebase = arg_parse_rational(&arg);
752 else if (arg_match(&arg, &error_resilient, argi))
753 cfg.g_error_resilient = arg_parse_uint(&arg);
754 else if (arg_match(&arg, &lag_in_frames, argi))
755 cfg.g_lag_in_frames = arg_parse_uint(&arg);
756 else if (arg_match(&arg, &dropframe_thresh, argi))
757 cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
758 else if (arg_match(&arg, &resize_allowed, argi))
759 cfg.rc_resize_allowed = arg_parse_uint(&arg);
760 else if (arg_match(&arg, &resize_up_thresh, argi))
761 cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
762 else if (arg_match(&arg, &resize_down_thresh, argi))
763 cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
764 else if (arg_match(&arg, &resize_down_thresh, argi))
765 cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
766 else if (arg_match(&arg, &end_usage, argi))
767 cfg.rc_end_usage = arg_parse_uint(&arg);
768 else if (arg_match(&arg, &target_bitrate, argi))
769 cfg.rc_target_bitrate = arg_parse_uint(&arg);
770 else if (arg_match(&arg, &min_quantizer, argi))
771 cfg.rc_min_quantizer = arg_parse_uint(&arg);
772 else if (arg_match(&arg, &max_quantizer, argi))
773 cfg.rc_max_quantizer = arg_parse_uint(&arg);
774 else if (arg_match(&arg, &undershoot_pct, argi))
775 cfg.rc_undershoot_pct = arg_parse_uint(&arg);
776 else if (arg_match(&arg, &overshoot_pct, argi))
777 cfg.rc_overshoot_pct = arg_parse_uint(&arg);
778 else if (arg_match(&arg, &buf_sz, argi))
779 cfg.rc_buf_sz = arg_parse_uint(&arg);
780 else if (arg_match(&arg, &buf_initial_sz, argi))
781 cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
782 else if (arg_match(&arg, &buf_optimal_sz, argi))
783 cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
784 else if (arg_match(&arg, &bias_pct, argi))
785 {
786 cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
787
788 if (arg_passes < 2)
John Koleszar3245d462010-06-10 17:10:12 -0400789 fprintf(stderr,
790 "Warning: option %s ignored in one-pass mode.\n",
791 arg.name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400792 }
793 else if (arg_match(&arg, &minsection_pct, argi))
794 {
795 cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
796
797 if (arg_passes < 2)
John Koleszar3245d462010-06-10 17:10:12 -0400798 fprintf(stderr,
799 "Warning: option %s ignored in one-pass mode.\n",
800 arg.name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400801 }
802 else if (arg_match(&arg, &maxsection_pct, argi))
803 {
804 cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
805
806 if (arg_passes < 2)
John Koleszar3245d462010-06-10 17:10:12 -0400807 fprintf(stderr,
808 "Warning: option %s ignored in one-pass mode.\n",
809 arg.name);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400810 }
811 else if (arg_match(&arg, &kf_min_dist, argi))
812 cfg.kf_min_dist = arg_parse_uint(&arg);
813 else if (arg_match(&arg, &kf_max_dist, argi))
814 cfg.kf_max_dist = arg_parse_uint(&arg);
John Koleszarb1e36f22010-06-24 08:41:51 -0400815 else if (arg_match(&arg, &kf_disabled, argi))
816 cfg.kf_mode = VPX_KF_DISABLED;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400817 else
818 argj++;
819 }
820
821 /* Handle codec specific options */
822#if CONFIG_VP8_ENCODER
823
824 if (codec->iface == &vpx_codec_vp8_cx_algo)
825 {
826 ctrl_args = vp8_args;
827 ctrl_args_map = vp8_arg_ctrl_map;
828 }
829
830#endif
831
832 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step)
833 {
834 int match = 0;
835
836 arg.argv_step = 1;
837
838 for (i = 0; ctrl_args[i]; i++)
839 {
840 if (arg_match(&arg, ctrl_args[i], argi))
841 {
842 match = 1;
843
844 if (arg_ctrl_cnt < ARG_CTRL_CNT_MAX)
845 {
846 arg_ctrls[arg_ctrl_cnt][0] = ctrl_args_map[i];
847 arg_ctrls[arg_ctrl_cnt][1] = arg_parse_int(&arg);
848 arg_ctrl_cnt++;
849 }
850 }
851 }
852
853 if (!match)
854 argj++;
855 }
856
857 /* Check for unrecognized options */
858 for (argi = argv; *argi; argi++)
John Koleszar3245d462010-06-10 17:10:12 -0400859 if (argi[0][0] == '-' && argi[0][1])
John Koleszar0ea50ce2010-05-18 11:58:33 -0400860 die("Error: Unrecognized option %s\n", *argi);
861
862 /* Handle non-option arguments */
863 in_fn = argv[0];
864 out_fn = argv[1];
865
866 if (!in_fn || !out_fn)
867 usage_exit();
868
John Koleszar0ea50ce2010-05-18 11:58:33 -0400869 memset(&stats, 0, sizeof(stats));
870
871 for (pass = one_pass_only ? one_pass_only - 1 : 0; pass < arg_passes; pass++)
872 {
873 int frames_in = 0, frames_out = 0;
874 unsigned long nbytes = 0;
John Koleszar3245d462010-06-10 17:10:12 -0400875 struct detect_buffer detect;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400876
John Koleszar3245d462010-06-10 17:10:12 -0400877 /* Parse certain options from the input file, if possible */
878 infile = strcmp(in_fn, "-") ? fopen(in_fn, "rb") : stdin;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400879
880 if (!infile)
881 {
John Koleszar3245d462010-06-10 17:10:12 -0400882 fprintf(stderr, "Failed to open input file\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400883 return EXIT_FAILURE;
884 }
885
John Koleszar3245d462010-06-10 17:10:12 -0400886 fread(detect.buf, 1, 4, infile);
887 detect.valid = 0;
888
889 if (file_is_y4m(infile, &y4m, detect.buf))
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400890 {
Tom Finegane703af92010-06-16 13:24:55 -0400891 if (y4m_input_open(&y4m, infile, detect.buf, 4) >= 0)
John Koleszar3245d462010-06-10 17:10:12 -0400892 {
Tom Finegane703af92010-06-16 13:24:55 -0400893 file_type = FILE_TYPE_Y4M;
894 cfg.g_w = y4m.pic_w;
895 cfg.g_h = y4m.pic_h;
Frank Galligan1258cf62010-10-06 12:51:00 -0400896
Tom Finegane703af92010-06-16 13:24:55 -0400897 /* Use the frame rate from the file only if none was specified
898 * on the command-line.
899 */
Frank Galligan1258cf62010-10-06 12:51:00 -0400900 if (!arg_have_framerate)
Tom Finegane703af92010-06-16 13:24:55 -0400901 {
Frank Galligan1258cf62010-10-06 12:51:00 -0400902 arg_framerate.num = y4m.fps_n;
903 arg_framerate.den = y4m.fps_d;
Tom Finegane703af92010-06-16 13:24:55 -0400904 }
Frank Galligan1258cf62010-10-06 12:51:00 -0400905
Tom Finegane703af92010-06-16 13:24:55 -0400906 arg_use_i420 = 0;
John Koleszar3245d462010-06-10 17:10:12 -0400907 }
Tom Finegane703af92010-06-16 13:24:55 -0400908 else
909 {
910 fprintf(stderr, "Unsupported Y4M stream.\n");
911 return EXIT_FAILURE;
912 }
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400913 }
John Koleszar3245d462010-06-10 17:10:12 -0400914 else if (file_is_ivf(infile, &fourcc, &cfg.g_w, &cfg.g_h, detect.buf))
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400915 {
John Koleszar3245d462010-06-10 17:10:12 -0400916 file_type = FILE_TYPE_IVF;
917 switch (fourcc)
918 {
919 case 0x32315659:
920 arg_use_i420 = 0;
921 break;
922 case 0x30323449:
923 arg_use_i420 = 1;
924 break;
925 default:
926 fprintf(stderr, "Unsupported fourcc (%08x) in IVF\n", fourcc);
927 return EXIT_FAILURE;
928 }
929 }
930 else
931 {
932 file_type = FILE_TYPE_RAW;
933 detect.valid = 1;
934 }
935#define SHOW(field) fprintf(stderr, " %-28s = %d\n", #field, cfg.field)
936
937 if (verbose && pass == 0)
938 {
939 fprintf(stderr, "Codec: %s\n", vpx_codec_iface_name(codec->iface));
940 fprintf(stderr, "Source file: %s Format: %s\n", in_fn,
941 arg_use_i420 ? "I420" : "YV12");
942 fprintf(stderr, "Destination file: %s\n", out_fn);
943 fprintf(stderr, "Encoder parameters:\n");
944
945 SHOW(g_usage);
946 SHOW(g_threads);
947 SHOW(g_profile);
948 SHOW(g_w);
949 SHOW(g_h);
950 SHOW(g_timebase.num);
951 SHOW(g_timebase.den);
952 SHOW(g_error_resilient);
953 SHOW(g_pass);
954 SHOW(g_lag_in_frames);
955 SHOW(rc_dropframe_thresh);
956 SHOW(rc_resize_allowed);
957 SHOW(rc_resize_up_thresh);
958 SHOW(rc_resize_down_thresh);
959 SHOW(rc_end_usage);
960 SHOW(rc_target_bitrate);
961 SHOW(rc_min_quantizer);
962 SHOW(rc_max_quantizer);
963 SHOW(rc_undershoot_pct);
964 SHOW(rc_overshoot_pct);
965 SHOW(rc_buf_sz);
966 SHOW(rc_buf_initial_sz);
967 SHOW(rc_buf_optimal_sz);
968 SHOW(rc_2pass_vbr_bias_pct);
969 SHOW(rc_2pass_vbr_minsection_pct);
970 SHOW(rc_2pass_vbr_maxsection_pct);
971 SHOW(kf_mode);
972 SHOW(kf_min_dist);
973 SHOW(kf_max_dist);
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400974 }
975
John Koleszardf2c62d2010-06-14 08:33:54 -0400976 if(pass == (one_pass_only ? one_pass_only - 1 : 0)) {
John Koleszar3245d462010-06-10 17:10:12 -0400977 if (file_type == FILE_TYPE_Y4M)
978 /*The Y4M reader does its own allocation.
979 Just initialize this here to avoid problems if we never read any
980 frames.*/
981 memset(&raw, 0, sizeof(raw));
982 else
983 vpx_img_alloc(&raw, arg_use_i420 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_YV12,
984 cfg.g_w, cfg.g_h, 1);
John Koleszar3245d462010-06-10 17:10:12 -0400985 }
986
987 outfile = strcmp(out_fn, "-") ? fopen(out_fn, "wb") : stdout;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400988
989 if (!outfile)
990 {
John Koleszar3245d462010-06-10 17:10:12 -0400991 fprintf(stderr, "Failed to open output file\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400992 return EXIT_FAILURE;
993 }
994
995 if (stats_fn)
996 {
997 if (!stats_open_file(&stats, stats_fn, pass))
998 {
John Koleszar3245d462010-06-10 17:10:12 -0400999 fprintf(stderr, "Failed to open statistics store\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001000 return EXIT_FAILURE;
1001 }
1002 }
1003 else
1004 {
1005 if (!stats_open_mem(&stats, pass))
1006 {
John Koleszar3245d462010-06-10 17:10:12 -04001007 fprintf(stderr, "Failed to open statistics store\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001008 return EXIT_FAILURE;
1009 }
1010 }
1011
1012 cfg.g_pass = arg_passes == 2
1013 ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS
1014 : VPX_RC_ONE_PASS;
1015#if VPX_ENCODER_ABI_VERSION > (1 + VPX_CODEC_ABI_VERSION)
1016
1017 if (pass)
1018 {
1019 cfg.rc_twopass_stats_in = stats_get(&stats);
1020 }
1021
1022#endif
1023
1024 write_ivf_file_header(outfile, &cfg, codec->fourcc, 0);
1025
1026
1027 /* Construct Encoder Context */
John Koleszar0ea50ce2010-05-18 11:58:33 -04001028 vpx_codec_enc_init(&encoder, codec->iface, &cfg,
1029 show_psnr ? VPX_CODEC_USE_PSNR : 0);
1030 ctx_exit_on_error(&encoder, "Failed to initialize encoder");
1031
1032 /* Note that we bypass the vpx_codec_control wrapper macro because
1033 * we're being clever to store the control IDs in an array. Real
1034 * applications will want to make use of the enumerations directly
1035 */
1036 for (i = 0; i < arg_ctrl_cnt; i++)
1037 {
1038 if (vpx_codec_control_(&encoder, arg_ctrls[i][0], arg_ctrls[i][1]))
John Koleszar3245d462010-06-10 17:10:12 -04001039 fprintf(stderr, "Error: Tried to set control %d = %d\n",
1040 arg_ctrls[i][0], arg_ctrls[i][1]);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001041
1042 ctx_exit_on_error(&encoder, "Failed to control codec");
1043 }
1044
1045 frame_avail = 1;
1046 got_data = 0;
1047
1048 while (frame_avail || got_data)
1049 {
1050 vpx_codec_iter_t iter = NULL;
1051 const vpx_codec_cx_pkt_t *pkt;
1052 struct vpx_usec_timer timer;
Frank Galligan1258cf62010-10-06 12:51:00 -04001053 int64_t frame_start;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001054
1055 if (!arg_limit || frames_in < arg_limit)
1056 {
John Koleszar3245d462010-06-10 17:10:12 -04001057 frame_avail = read_frame(infile, &raw, file_type, &y4m,
1058 &detect);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001059
1060 if (frame_avail)
1061 frames_in++;
1062
John Koleszar3245d462010-06-10 17:10:12 -04001063 fprintf(stderr,
1064 "\rPass %d/%d frame %4d/%-4d %7ldB \033[K", pass + 1,
1065 arg_passes, frames_in, frames_out, nbytes);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001066 }
1067 else
1068 frame_avail = 0;
1069
1070 vpx_usec_timer_start(&timer);
1071
Frank Galligan1258cf62010-10-06 12:51:00 -04001072 frame_start = (cfg.g_timebase.den * (int64_t)(frames_in - 1)
1073 * arg_framerate.den) / cfg.g_timebase.num / arg_framerate.num;
1074 vpx_codec_encode(&encoder, frame_avail ? &raw : NULL, frame_start,
1075 cfg.g_timebase.den * arg_framerate.den
1076 / cfg.g_timebase.num / arg_framerate.num,
1077 0, arg_deadline);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001078 vpx_usec_timer_mark(&timer);
1079 cx_time += vpx_usec_timer_elapsed(&timer);
1080 ctx_exit_on_error(&encoder, "Failed to encode frame");
1081 got_data = 0;
1082
1083 while ((pkt = vpx_codec_get_cx_data(&encoder, &iter)))
1084 {
1085 got_data = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001086
1087 switch (pkt->kind)
1088 {
1089 case VPX_CODEC_CX_FRAME_PKT:
1090 frames_out++;
John Koleszar3245d462010-06-10 17:10:12 -04001091 fprintf(stderr, " %6luF",
1092 (unsigned long)pkt->data.frame.sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001093 write_ivf_frame_header(outfile, pkt);
1094 fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile);
Yaowu Xua7bb3362010-05-27 10:32:40 -07001095 nbytes += pkt->data.raw.sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001096 break;
1097 case VPX_CODEC_STATS_PKT:
1098 frames_out++;
John Koleszar3245d462010-06-10 17:10:12 -04001099 fprintf(stderr, " %6luS",
John Koleszar0ea50ce2010-05-18 11:58:33 -04001100 (unsigned long)pkt->data.twopass_stats.sz);
1101 stats_write(&stats,
1102 pkt->data.twopass_stats.buf,
1103 pkt->data.twopass_stats.sz);
Yaowu Xua7bb3362010-05-27 10:32:40 -07001104 nbytes += pkt->data.raw.sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001105 break;
1106 case VPX_CODEC_PSNR_PKT:
1107
1108 if (show_psnr)
1109 {
1110 int i;
1111
1112 for (i = 0; i < 4; i++)
John Koleszar3245d462010-06-10 17:10:12 -04001113 fprintf(stderr, "%.3lf ", pkt->data.psnr.psnr[i]);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001114 }
1115
1116 break;
1117 default:
1118 break;
1119 }
1120 }
1121
1122 fflush(stdout);
1123 }
1124
John Koleszar3245d462010-06-10 17:10:12 -04001125 fprintf(stderr,
1126 "\rPass %d/%d frame %4d/%-4d %7ldB %7ldb/f %7"PRId64"b/s"
John Koleszar0ea50ce2010-05-18 11:58:33 -04001127 " %7lu %s (%.2f fps)\033[K", pass + 1,
1128 arg_passes, frames_in, frames_out, nbytes, nbytes * 8 / frames_in,
Frank Galligan1258cf62010-10-06 12:51:00 -04001129 nbytes * 8 *(int64_t)arg_framerate.num / arg_framerate.den / frames_in,
John Koleszar0ea50ce2010-05-18 11:58:33 -04001130 cx_time > 9999999 ? cx_time / 1000 : cx_time,
1131 cx_time > 9999999 ? "ms" : "us",
1132 (float)frames_in * 1000000.0 / (float)cx_time);
1133
1134 vpx_codec_destroy(&encoder);
1135
1136 fclose(infile);
1137
1138 if (!fseek(outfile, 0, SEEK_SET))
1139 write_ivf_file_header(outfile, &cfg, codec->fourcc, frames_out);
1140
1141 fclose(outfile);
1142 stats_close(&stats);
John Koleszar3245d462010-06-10 17:10:12 -04001143 fprintf(stderr, "\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001144
1145 if (one_pass_only)
1146 break;
1147 }
1148
1149 vpx_img_free(&raw);
1150 free(argv);
1151 return EXIT_SUCCESS;
1152}