blob: e6f935174fccc0a13119f5923254530160c3b8a7 [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
Christian Duviviera1168152012-08-14 14:12:00 -070011#include "vpx_config.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040012
Tero Rintaluoma11a222f2011-01-24 11:21:40 +020013#if defined(_WIN32) || !CONFIG_OS_SUPPORT
John Koleszar1df03142010-05-20 14:44:18 -040014#define USE_POSIX_MMAP 0
15#else
16#define USE_POSIX_MMAP 1
17#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <stdarg.h>
22#include <string.h>
John Koleszardc666302010-10-20 12:05:48 -040023#include <limits.h>
John Koleszar3fde9962011-06-20 17:09:29 -040024#include <assert.h>
John Koleszarb7492342010-05-24 11:39:59 -040025#include "vpx/vpx_encoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080026#if CONFIG_DECODERS
John Koleszar6ad3b742012-11-06 12:02:42 -080027#include "vpx/vpx_decoder.h"
John Koleszar5ebe94f2012-12-23 07:20:10 -080028#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -040029#if USE_POSIX_MMAP
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/mman.h>
33#include <fcntl.h>
34#include <unistd.h>
35#endif
John Koleszar6ad3b742012-11-06 12:02:42 -080036
John Koleszar7b8dfcb2012-11-06 16:59:01 -080037#if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
John Koleszarb7492342010-05-24 11:39:59 -040038#include "vpx/vp8cx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080039#endif
John Koleszar7b8dfcb2012-11-06 16:59:01 -080040#if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
Yaowu Xuf798f9e2012-03-07 12:25:50 -080041#include "vpx/vp8dx.h"
John Koleszar6ad3b742012-11-06 12:02:42 -080042#endif
43
John Koleszar0ea50ce2010-05-18 11:58:33 -040044#include "vpx_ports/mem_ops.h"
45#include "vpx_ports/vpx_timer.h"
John Koleszarc377bf02010-11-02 09:11:57 -040046#include "tools_common.h"
Timothy B. Terriberry44d89492010-05-26 18:27:51 -040047#include "y4minput.h"
John Koleszardc666302010-10-20 12:05:48 -040048#include "libmkv/EbmlWriter.h"
49#include "libmkv/EbmlIDs.h"
50
51/* Need special handling of these functions on Windows */
52#if defined(_MSC_VER)
53/* MSVS doesn't define off_t, and uses _f{seek,tell}i64 */
54typedef __int64 off_t;
55#define fseeko _fseeki64
56#define ftello _ftelli64
57#elif defined(_WIN32)
James Zern87953d112011-09-06 12:21:19 -070058/* MinGW defines off_t as long
59 and uses f{seek,tell}o64/off64_t for large files */
John Koleszardc666302010-10-20 12:05:48 -040060#define fseeko fseeko64
61#define ftello ftello64
James Zern87953d112011-09-06 12:21:19 -070062#define off_t off64_t
John Koleszardc666302010-10-20 12:05:48 -040063#endif
64
John Koleszar6291dd42012-06-19 21:05:36 -070065#define LITERALU64(hi,lo) ((((uint64_t)hi)<<32)|lo)
John Koleszar0ea50ce2010-05-18 11:58:33 -040066
Tero Rintaluoma11a222f2011-01-24 11:21:40 +020067/* We should use 32-bit file operations in WebM file format
68 * when building ARM executable file (.axf) with RVCT */
69#if !CONFIG_OS_SUPPORT
70typedef long off_t;
71#define fseeko fseek
72#define ftello ftell
73#endif
74
John Koleszar6291dd42012-06-19 21:05:36 -070075/* Swallow warnings about unused results of fread/fwrite */
76static size_t wrap_fread(void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080077 FILE *stream) {
78 return fread(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070079}
80#define fread wrap_fread
81
82static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
John Koleszar6ad3b742012-11-06 12:02:42 -080083 FILE *stream) {
84 return fwrite(ptr, size, nmemb, stream);
John Koleszar6291dd42012-06-19 21:05:36 -070085}
86#define fwrite wrap_fwrite
87
88
John Koleszar0ea50ce2010-05-18 11:58:33 -040089static const char *exec_name;
90
John Koleszar08b43fe2012-11-13 12:14:28 -080091#define VP8_FOURCC (0x00385056)
92#define VP9_FOURCC (0x00395056)
Jim Bankoski1b16e742012-07-23 12:32:59 -070093static const struct codec_item {
John Koleszar6ad3b742012-11-06 12:02:42 -080094 char const *name;
Jim Bankoski1b16e742012-07-23 12:32:59 -070095 const vpx_codec_iface_t *(*iface)(void);
John Koleszar6ad3b742012-11-06 12:02:42 -080096 const vpx_codec_iface_t *(*dx_iface)(void);
Jim Bankoski1b16e742012-07-23 12:32:59 -070097 unsigned int fourcc;
Jim Bankoski1b16e742012-07-23 12:32:59 -070098} codecs[] = {
John Koleszar7b8dfcb2012-11-06 16:59:01 -080099#if CONFIG_VP8_ENCODER && CONFIG_VP8_DECODER
John Koleszar08b43fe2012-11-13 12:14:28 -0800100 {"vp8", &vpx_codec_vp8_cx, &vpx_codec_vp8_dx, VP8_FOURCC},
John Koleszara9c75972012-11-08 17:09:30 -0800101#elif CONFIG_VP8_ENCODER && !CONFIG_VP8_DECODER
John Koleszar08b43fe2012-11-13 12:14:28 -0800102 {"vp8", &vpx_codec_vp8_cx, NULL, VP8_FOURCC},
John Koleszar7b8dfcb2012-11-06 16:59:01 -0800103#endif
John Koleszar6ad3b742012-11-06 12:02:42 -0800104#if CONFIG_VP9_ENCODER && CONFIG_VP9_DECODER
John Koleszar08b43fe2012-11-13 12:14:28 -0800105 {"vp9", &vpx_codec_vp9_cx, &vpx_codec_vp9_dx, VP9_FOURCC},
John Koleszar7b8dfcb2012-11-06 16:59:01 -0800106#elif CONFIG_VP9_ENCODER && !CONFIG_VP9_DECODER
John Koleszar08b43fe2012-11-13 12:14:28 -0800107 {"vp9", &vpx_codec_vp9_cx, NULL, VP9_FOURCC},
John Koleszar0ea50ce2010-05-18 11:58:33 -0400108#endif
109};
110
111static void usage_exit();
112
John Koleszarc5350252012-02-14 09:37:44 -0800113#define LOG_ERROR(label) do \
John Koleszar6ad3b742012-11-06 12:02:42 -0800114 {\
John Koleszarc5350252012-02-14 09:37:44 -0800115 const char *l=label;\
116 va_list ap;\
117 va_start(ap, fmt);\
118 if(l)\
John Koleszar6ad3b742012-11-06 12:02:42 -0800119 fprintf(stderr, "%s: ", l);\
John Koleszarc5350252012-02-14 09:37:44 -0800120 vfprintf(stderr, fmt, ap);\
121 fprintf(stderr, "\n");\
122 va_end(ap);\
John Koleszar6ad3b742012-11-06 12:02:42 -0800123 } while(0)
John Koleszarc5350252012-02-14 09:37:44 -0800124
John Koleszarc6b90392012-07-13 15:21:29 -0700125void die(const char *fmt, ...) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800126 LOG_ERROR(NULL);
John Koleszarc6b90392012-07-13 15:21:29 -0700127 usage_exit();
John Koleszar0ea50ce2010-05-18 11:58:33 -0400128}
129
John Koleszarc5350252012-02-14 09:37:44 -0800130
John Koleszar6ad3b742012-11-06 12:02:42 -0800131void fatal(const char *fmt, ...) {
132 LOG_ERROR("Fatal");
133 exit(EXIT_FAILURE);
134}
135
136
137void warn(const char *fmt, ...) {
138 LOG_ERROR("Warning");
139}
140
141
142static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s, ...) {
143 va_list ap;
144
145 va_start(ap, s);
John Koleszarc6b90392012-07-13 15:21:29 -0700146 if (ctx->err) {
147 const char *detail = vpx_codec_error_detail(ctx);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400148
John Koleszar6ad3b742012-11-06 12:02:42 -0800149 vfprintf(stderr, s, ap);
150 fprintf(stderr, ": %s\n", vpx_codec_error(ctx));
John Koleszar0ea50ce2010-05-18 11:58:33 -0400151
John Koleszarc6b90392012-07-13 15:21:29 -0700152 if (detail)
153 fprintf(stderr, " %s\n", detail);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400154
John Koleszarc5350252012-02-14 09:37:44 -0800155 exit(EXIT_FAILURE);
John Koleszarc6b90392012-07-13 15:21:29 -0700156 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400157}
158
159/* This structure is used to abstract the different ways of handling
160 * first pass statistics.
161 */
John Koleszarc6b90392012-07-13 15:21:29 -0700162typedef struct {
163 vpx_fixed_buf_t buf;
164 int pass;
165 FILE *file;
166 char *buf_ptr;
167 size_t buf_alloc_sz;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400168} stats_io_t;
169
John Koleszarc6b90392012-07-13 15:21:29 -0700170int stats_open_file(stats_io_t *stats, const char *fpf, int pass) {
171 int res;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400172
John Koleszarc6b90392012-07-13 15:21:29 -0700173 stats->pass = pass;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400174
John Koleszarc6b90392012-07-13 15:21:29 -0700175 if (pass == 0) {
176 stats->file = fopen(fpf, "wb");
177 stats->buf.sz = 0;
178 stats->buf.buf = NULL,
179 res = (stats->file != NULL);
180 } else {
John Koleszar0ea50ce2010-05-18 11:58:33 -0400181#if 0
182#elif USE_POSIX_MMAP
John Koleszarc6b90392012-07-13 15:21:29 -0700183 struct stat stat_buf;
184 int fd;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400185
John Koleszarc6b90392012-07-13 15:21:29 -0700186 fd = open(fpf, O_RDONLY);
187 stats->file = fdopen(fd, "rb");
188 fstat(fd, &stat_buf);
189 stats->buf.sz = stat_buf.st_size;
190 stats->buf.buf = mmap(NULL, stats->buf.sz, PROT_READ, MAP_PRIVATE,
191 fd, 0);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400192 res = (stats->buf.buf != NULL);
John Koleszarc6b90392012-07-13 15:21:29 -0700193#else
194 size_t nbytes;
195
196 stats->file = fopen(fpf, "rb");
197
John Koleszar6ad3b742012-11-06 12:02:42 -0800198 if (fseek(stats->file, 0, SEEK_END))
199 fatal("First-pass stats file must be seekable!");
John Koleszarc6b90392012-07-13 15:21:29 -0700200
201 stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
202 rewind(stats->file);
203
204 stats->buf.buf = malloc(stats->buf_alloc_sz);
205
John Koleszar6ad3b742012-11-06 12:02:42 -0800206 if (!stats->buf.buf)
207 fatal("Failed to allocate first-pass stats buffer (%lu bytes)",
208 (unsigned long)stats->buf_alloc_sz);
John Koleszarc6b90392012-07-13 15:21:29 -0700209
210 nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
211 res = (nbytes == stats->buf.sz);
212#endif
213 }
214
215 return res;
216}
217
218int stats_open_mem(stats_io_t *stats, int pass) {
219 int res;
220 stats->pass = pass;
221
222 if (!pass) {
223 stats->buf.sz = 0;
224 stats->buf_alloc_sz = 64 * 1024;
225 stats->buf.buf = malloc(stats->buf_alloc_sz);
226 }
227
228 stats->buf_ptr = stats->buf.buf;
229 res = (stats->buf.buf != NULL);
230 return res;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400231}
232
233
John Koleszarc6b90392012-07-13 15:21:29 -0700234void stats_close(stats_io_t *stats, int last_pass) {
235 if (stats->file) {
236 if (stats->pass == last_pass) {
John Koleszar0ea50ce2010-05-18 11:58:33 -0400237#if 0
238#elif USE_POSIX_MMAP
John Koleszarc6b90392012-07-13 15:21:29 -0700239 munmap(stats->buf.buf, stats->buf.sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400240#else
John Koleszarc6b90392012-07-13 15:21:29 -0700241 free(stats->buf.buf);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400242#endif
John Koleszarc6b90392012-07-13 15:21:29 -0700243 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400244
John Koleszarc6b90392012-07-13 15:21:29 -0700245 fclose(stats->file);
246 stats->file = NULL;
247 } else {
248 if (stats->pass == last_pass)
249 free(stats->buf.buf);
250 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400251}
252
John Koleszarc6b90392012-07-13 15:21:29 -0700253void stats_write(stats_io_t *stats, const void *pkt, size_t len) {
254 if (stats->file) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800255 (void) fwrite(pkt, 1, len, stats->file);
John Koleszarc6b90392012-07-13 15:21:29 -0700256 } else {
257 if (stats->buf.sz + len > stats->buf_alloc_sz) {
258 size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
259 char *new_ptr = realloc(stats->buf.buf, new_sz);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400260
John Koleszarc6b90392012-07-13 15:21:29 -0700261 if (new_ptr) {
262 stats->buf_ptr = new_ptr + (stats->buf_ptr - (char *)stats->buf.buf);
263 stats->buf.buf = new_ptr;
264 stats->buf_alloc_sz = new_sz;
John Koleszar6ad3b742012-11-06 12:02:42 -0800265 } else
266 fatal("Failed to realloc firstpass stats buffer.");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400267 }
John Koleszarc6b90392012-07-13 15:21:29 -0700268
269 memcpy(stats->buf_ptr, pkt, len);
270 stats->buf.sz += len;
271 stats->buf_ptr += len;
272 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400273}
274
John Koleszarc6b90392012-07-13 15:21:29 -0700275vpx_fixed_buf_t stats_get(stats_io_t *stats) {
276 return stats->buf;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400277}
278
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700279/* Stereo 3D packed frame format */
John Koleszarc6b90392012-07-13 15:21:29 -0700280typedef enum stereo_format {
281 STEREO_FORMAT_MONO = 0,
282 STEREO_FORMAT_LEFT_RIGHT = 1,
283 STEREO_FORMAT_BOTTOM_TOP = 2,
284 STEREO_FORMAT_TOP_BOTTOM = 3,
285 STEREO_FORMAT_RIGHT_LEFT = 11
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700286} stereo_format_t;
287
John Koleszarc6b90392012-07-13 15:21:29 -0700288enum video_file_type {
289 FILE_TYPE_RAW,
290 FILE_TYPE_IVF,
291 FILE_TYPE_Y4M
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400292};
293
John Koleszar3245d462010-06-10 17:10:12 -0400294struct detect_buffer {
John Koleszarc6b90392012-07-13 15:21:29 -0700295 char buf[4];
296 size_t buf_read;
297 size_t position;
John Koleszar3245d462010-06-10 17:10:12 -0400298};
299
300
John Koleszar6ad3b742012-11-06 12:02:42 -0800301struct input_state {
302 char *fn;
303 FILE *file;
304 y4m_input y4m;
305 struct detect_buffer detect;
306 enum video_file_type file_type;
307 unsigned int w;
308 unsigned int h;
309 struct vpx_rational framerate;
310 int use_i420;
John Koleszar732cb9a2012-02-14 12:30:17 -0800311};
312
313
314#define IVF_FRAME_HDR_SZ (4+8) /* 4 byte size + 8 byte timestamp */
John Koleszar6ad3b742012-11-06 12:02:42 -0800315static int read_frame(struct input_state *input, vpx_image_t *img) {
316 FILE *f = input->file;
317 enum video_file_type file_type = input->file_type;
318 y4m_input *y4m = &input->y4m;
319 struct detect_buffer *detect = &input->detect;
John Koleszarc6b90392012-07-13 15:21:29 -0700320 int plane = 0;
321 int shortread = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400322
John Koleszarc6b90392012-07-13 15:21:29 -0700323 if (file_type == FILE_TYPE_Y4M) {
324 if (y4m_input_fetch_frame(y4m, f, img) < 1)
325 return 0;
326 } else {
327 if (file_type == FILE_TYPE_IVF) {
328 char junk[IVF_FRAME_HDR_SZ];
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400329
John Koleszarc6b90392012-07-13 15:21:29 -0700330 /* Skip the frame header. We know how big the frame should be. See
331 * write_ivf_frame_header() for documentation on the frame header
332 * layout.
333 */
John Koleszar6ad3b742012-11-06 12:02:42 -0800334 (void) fread(junk, 1, IVF_FRAME_HDR_SZ, f);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400335 }
336
John Koleszarc6b90392012-07-13 15:21:29 -0700337 for (plane = 0; plane < 3; plane++) {
338 unsigned char *ptr;
339 int w = (plane ? (1 + img->d_w) / 2 : img->d_w);
340 int h = (plane ? (1 + img->d_h) / 2 : img->d_h);
341 int r;
342
343 /* Determine the correct plane based on the image format. The for-loop
344 * always counts in Y,U,V order, but this may not match the order of
345 * the data on disk.
346 */
347 switch (plane) {
348 case 1:
349 ptr = img->planes[img->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V : VPX_PLANE_U];
350 break;
351 case 2:
352 ptr = img->planes[img->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U : VPX_PLANE_V];
353 break;
354 default:
355 ptr = img->planes[plane];
356 }
357
358 for (r = 0; r < h; r++) {
359 size_t needed = w;
360 size_t buf_position = 0;
361 const size_t left = detect->buf_read - detect->position;
362 if (left > 0) {
363 const size_t more = (left < needed) ? left : needed;
364 memcpy(ptr, detect->buf + detect->position, more);
365 buf_position = more;
366 needed -= more;
367 detect->position += more;
368 }
369 if (needed > 0) {
370 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
371 }
372
373 ptr += img->stride[plane];
374 }
375 }
376 }
377
378 return !shortread;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400379}
380
381
Tom Finegane703af92010-06-16 13:24:55 -0400382unsigned int file_is_y4m(FILE *infile,
John Koleszar3245d462010-06-10 17:10:12 -0400383 y4m_input *y4m,
John Koleszarc6b90392012-07-13 15:21:29 -0700384 char detect[4]) {
385 if (memcmp(detect, "YUV4", 4) == 0) {
386 return 1;
387 }
388 return 0;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -0400389}
390
John Koleszar0ea50ce2010-05-18 11:58:33 -0400391#define IVF_FILE_HDR_SZ (32)
John Koleszar732cb9a2012-02-14 12:30:17 -0800392unsigned int file_is_ivf(struct input_state *input,
John Koleszar6ad3b742012-11-06 12:02:42 -0800393 unsigned int *fourcc) {
John Koleszarc6b90392012-07-13 15:21:29 -0700394 char raw_hdr[IVF_FILE_HDR_SZ];
395 int is_ivf = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -0800396 FILE *infile = input->file;
397 unsigned int *width = &input->w;
398 unsigned int *height = &input->h;
399 struct detect_buffer *detect = &input->detect;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400400
John Koleszarc6b90392012-07-13 15:21:29 -0700401 if (memcmp(detect->buf, "DKIF", 4) != 0)
402 return 0;
John Koleszar3245d462010-06-10 17:10:12 -0400403
John Koleszarc6b90392012-07-13 15:21:29 -0700404 /* See write_ivf_file_header() for more documentation on the file header
405 * layout.
406 */
407 if (fread(raw_hdr + 4, 1, IVF_FILE_HDR_SZ - 4, infile)
408 == IVF_FILE_HDR_SZ - 4) {
John Koleszar0ea50ce2010-05-18 11:58:33 -0400409 {
John Koleszarc6b90392012-07-13 15:21:29 -0700410 is_ivf = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400411
John Koleszarc6b90392012-07-13 15:21:29 -0700412 if (mem_get_le16(raw_hdr + 4) != 0)
John Koleszar6ad3b742012-11-06 12:02:42 -0800413 warn("Unrecognized IVF version! This file may not decode "
414 "properly.");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400415
John Koleszarc6b90392012-07-13 15:21:29 -0700416 *fourcc = mem_get_le32(raw_hdr + 8);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400417 }
John Koleszarc6b90392012-07-13 15:21:29 -0700418 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400419
John Koleszarc6b90392012-07-13 15:21:29 -0700420 if (is_ivf) {
421 *width = mem_get_le16(raw_hdr + 12);
422 *height = mem_get_le16(raw_hdr + 14);
423 detect->position = 4;
424 }
John Koleszar0ea50ce2010-05-18 11:58:33 -0400425
John Koleszarc6b90392012-07-13 15:21:29 -0700426 return is_ivf;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400427}
428
429
430static void write_ivf_file_header(FILE *outfile,
431 const vpx_codec_enc_cfg_t *cfg,
432 unsigned int fourcc,
John Koleszarc6b90392012-07-13 15:21:29 -0700433 int frame_cnt) {
434 char header[32];
John Koleszar0ea50ce2010-05-18 11:58:33 -0400435
John Koleszarc6b90392012-07-13 15:21:29 -0700436 if (cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS)
437 return;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400438
John Koleszarc6b90392012-07-13 15:21:29 -0700439 header[0] = 'D';
440 header[1] = 'K';
441 header[2] = 'I';
442 header[3] = 'F';
443 mem_put_le16(header + 4, 0); /* version */
444 mem_put_le16(header + 6, 32); /* headersize */
445 mem_put_le32(header + 8, fourcc); /* headersize */
446 mem_put_le16(header + 12, cfg->g_w); /* width */
447 mem_put_le16(header + 14, cfg->g_h); /* height */
448 mem_put_le32(header + 16, cfg->g_timebase.den); /* rate */
449 mem_put_le32(header + 20, cfg->g_timebase.num); /* scale */
450 mem_put_le32(header + 24, frame_cnt); /* length */
451 mem_put_le32(header + 28, 0); /* unused */
John Koleszar0ea50ce2010-05-18 11:58:33 -0400452
John Koleszar6ad3b742012-11-06 12:02:42 -0800453 (void) fwrite(header, 1, 32, outfile);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400454}
455
456
457static void write_ivf_frame_header(FILE *outfile,
John Koleszarc6b90392012-07-13 15:21:29 -0700458 const vpx_codec_cx_pkt_t *pkt) {
459 char header[12];
460 vpx_codec_pts_t pts;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400461
John Koleszarc6b90392012-07-13 15:21:29 -0700462 if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
463 return;
John Koleszar0ea50ce2010-05-18 11:58:33 -0400464
John Koleszarc6b90392012-07-13 15:21:29 -0700465 pts = pkt->data.frame.pts;
John Koleszar6ad3b742012-11-06 12:02:42 -0800466 mem_put_le32(header, (int)pkt->data.frame.sz);
John Koleszarc6b90392012-07-13 15:21:29 -0700467 mem_put_le32(header + 4, pts & 0xFFFFFFFF);
468 mem_put_le32(header + 8, pts >> 32);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400469
John Koleszar6ad3b742012-11-06 12:02:42 -0800470 (void) fwrite(header, 1, 12, outfile);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400471}
472
John Koleszar6ad3b742012-11-06 12:02:42 -0800473static void write_ivf_frame_size(FILE *outfile, size_t size) {
474 char header[4];
475 mem_put_le32(header, (int)size);
476 (void) fwrite(header, 1, 4, outfile);
Attila Nagy52cf4dc2012-02-09 12:37:03 +0200477}
478
John Koleszardc666302010-10-20 12:05:48 -0400479
480typedef off_t EbmlLoc;
481
482
John Koleszarc6b90392012-07-13 15:21:29 -0700483struct cue_entry {
484 unsigned int time;
485 uint64_t loc;
John Koleszardc666302010-10-20 12:05:48 -0400486};
487
488
John Koleszarc6b90392012-07-13 15:21:29 -0700489struct EbmlGlobal {
490 int debug;
John Koleszar75afcee2010-10-26 16:22:22 -0400491
John Koleszarc6b90392012-07-13 15:21:29 -0700492 FILE *stream;
493 int64_t last_pts_ms;
494 vpx_rational_t framerate;
John Koleszardc666302010-10-20 12:05:48 -0400495
John Koleszarc6b90392012-07-13 15:21:29 -0700496 /* These pointers are to the start of an element */
497 off_t position_reference;
498 off_t seek_info_pos;
499 off_t segment_info_pos;
500 off_t track_pos;
501 off_t cue_pos;
502 off_t cluster_pos;
John Koleszardc666302010-10-20 12:05:48 -0400503
John Koleszarc6b90392012-07-13 15:21:29 -0700504 /* This pointer is to a specific element to be serialized */
505 off_t track_id_pos;
John Koleszarc7c0c052010-10-27 10:05:55 -0400506
John Koleszarc6b90392012-07-13 15:21:29 -0700507 /* These pointers are to the size field of the element */
508 EbmlLoc startSegment;
509 EbmlLoc startCluster;
John Koleszardc666302010-10-20 12:05:48 -0400510
John Koleszarc6b90392012-07-13 15:21:29 -0700511 uint32_t cluster_timecode;
512 int cluster_open;
John Koleszardc666302010-10-20 12:05:48 -0400513
John Koleszarc6b90392012-07-13 15:21:29 -0700514 struct cue_entry *cue_list;
515 unsigned int cues;
John Koleszardc666302010-10-20 12:05:48 -0400516
517};
518
519
John Koleszarc6b90392012-07-13 15:21:29 -0700520void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800521 (void) fwrite(buffer_in, 1, len, glob->stream);
John Koleszardc666302010-10-20 12:05:48 -0400522}
523
Yunqing Wangc156a682011-07-12 14:34:51 -0400524#define WRITE_BUFFER(s) \
John Koleszarc6b90392012-07-13 15:21:29 -0700525 for(i = len-1; i>=0; i--)\
526 { \
Yaowu Xud71ba032012-08-17 10:05:35 -0700527 x = (char)(*(const s *)buffer_in >> (i * CHAR_BIT)); \
Yunqing Wangc156a682011-07-12 14:34:51 -0400528 Ebml_Write(glob, &x, 1); \
John Koleszarc6b90392012-07-13 15:21:29 -0700529 }
530void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, int buffer_size, unsigned long len) {
531 char x;
532 int i;
John Koleszardc666302010-10-20 12:05:48 -0400533
John Koleszarc6b90392012-07-13 15:21:29 -0700534 /* buffer_size:
535 * 1 - int8_t;
536 * 2 - int16_t;
537 * 3 - int32_t;
538 * 4 - int64_t;
539 */
540 switch (buffer_size) {
541 case 1:
542 WRITE_BUFFER(int8_t)
543 break;
544 case 2:
545 WRITE_BUFFER(int16_t)
546 break;
547 case 4:
548 WRITE_BUFFER(int32_t)
549 break;
550 case 8:
551 WRITE_BUFFER(int64_t)
552 break;
553 default:
554 break;
555 }
Yunqing Wangc156a682011-07-12 14:34:51 -0400556}
557#undef WRITE_BUFFER
John Koleszardc666302010-10-20 12:05:48 -0400558
James Zern3a975d92011-07-25 13:26:18 -0700559/* Need a fixed size serializer for the track ID. libmkv provides a 64 bit
John Koleszarc7c0c052010-10-27 10:05:55 -0400560 * one, but not a 32 bit one.
561 */
John Koleszarc6b90392012-07-13 15:21:29 -0700562static void Ebml_SerializeUnsigned32(EbmlGlobal *glob, unsigned long class_id, uint64_t ui) {
563 unsigned char sizeSerialized = 4 | 0x80;
564 Ebml_WriteID(glob, class_id);
565 Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1);
566 Ebml_Serialize(glob, &ui, sizeof(ui), 4);
John Koleszarc7c0c052010-10-27 10:05:55 -0400567}
568
569
John Koleszardc666302010-10-20 12:05:48 -0400570static void
571Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc,
John Koleszarc6b90392012-07-13 15:21:29 -0700572 unsigned long class_id) {
John Koleszar6ad3b742012-11-06 12:02:42 -0800573 /* todo this is always taking 8 bytes, this may need later optimization */
574 /* this is a key that says length unknown */
575 uint64_t unknownLen = LITERALU64(0x01FFFFFF, 0xFFFFFFFF);
John Koleszardc666302010-10-20 12:05:48 -0400576
John Koleszarc6b90392012-07-13 15:21:29 -0700577 Ebml_WriteID(glob, class_id);
578 *ebmlLoc = ftello(glob->stream);
579 Ebml_Serialize(glob, &unknownLen, sizeof(unknownLen), 8);
John Koleszardc666302010-10-20 12:05:48 -0400580}
581
582static void
John Koleszarc6b90392012-07-13 15:21:29 -0700583Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc) {
584 off_t pos;
585 uint64_t size;
John Koleszardc666302010-10-20 12:05:48 -0400586
John Koleszarc6b90392012-07-13 15:21:29 -0700587 /* Save the current stream pointer */
588 pos = ftello(glob->stream);
John Koleszardc666302010-10-20 12:05:48 -0400589
John Koleszarc6b90392012-07-13 15:21:29 -0700590 /* Calculate the size of this element */
591 size = pos - *ebmlLoc - 8;
John Koleszar6ad3b742012-11-06 12:02:42 -0800592 size |= LITERALU64(0x01000000, 0x00000000);
John Koleszardc666302010-10-20 12:05:48 -0400593
John Koleszarc6b90392012-07-13 15:21:29 -0700594 /* Seek back to the beginning of the element and write the new size */
595 fseeko(glob->stream, *ebmlLoc, SEEK_SET);
596 Ebml_Serialize(glob, &size, sizeof(size), 8);
John Koleszardc666302010-10-20 12:05:48 -0400597
John Koleszarc6b90392012-07-13 15:21:29 -0700598 /* Reset the stream pointer */
599 fseeko(glob->stream, pos, SEEK_SET);
John Koleszardc666302010-10-20 12:05:48 -0400600}
601
602
603static void
John Koleszarc6b90392012-07-13 15:21:29 -0700604write_webm_seek_element(EbmlGlobal *ebml, unsigned long id, off_t pos) {
605 uint64_t offset = pos - ebml->position_reference;
606 EbmlLoc start;
607 Ebml_StartSubElement(ebml, &start, Seek);
608 Ebml_SerializeBinary(ebml, SeekID, id);
609 Ebml_SerializeUnsigned64(ebml, SeekPosition, offset);
610 Ebml_EndSubElement(ebml, &start);
611}
612
613
614static void
615write_webm_seek_info(EbmlGlobal *ebml) {
616
617 off_t pos;
618
619 /* Save the current stream pointer */
620 pos = ftello(ebml->stream);
621
622 if (ebml->seek_info_pos)
623 fseeko(ebml->stream, ebml->seek_info_pos, SEEK_SET);
624 else
625 ebml->seek_info_pos = pos;
626
627 {
John Koleszardc666302010-10-20 12:05:48 -0400628 EbmlLoc start;
John Koleszarc6b90392012-07-13 15:21:29 -0700629
630 Ebml_StartSubElement(ebml, &start, SeekHead);
631 write_webm_seek_element(ebml, Tracks, ebml->track_pos);
632 write_webm_seek_element(ebml, Cues, ebml->cue_pos);
633 write_webm_seek_element(ebml, Info, ebml->segment_info_pos);
John Koleszardc666302010-10-20 12:05:48 -0400634 Ebml_EndSubElement(ebml, &start);
John Koleszarc6b90392012-07-13 15:21:29 -0700635 }
636 {
John Koleszar6ad3b742012-11-06 12:02:42 -0800637 /* segment info */
John Koleszarc6b90392012-07-13 15:21:29 -0700638 EbmlLoc startInfo;
639 uint64_t frame_time;
John Koleszar6ad3b742012-11-06 12:02:42 -0800640 char version_string[64];
John Koleszardc666302010-10-20 12:05:48 -0400641
John Koleszar6ad3b742012-11-06 12:02:42 -0800642 /* Assemble version string */
643 if (ebml->debug)
644 strcpy(version_string, "vpxenc");
645 else {
646 strcpy(version_string, "vpxenc ");
647 strncat(version_string,
648 vpx_codec_version_str(),
649 sizeof(version_string) - 1 - strlen(version_string));
John Koleszardc666302010-10-20 12:05:48 -0400650 }
John Koleszarfa3e5302011-09-22 13:24:33 -0400651
John Koleszarc6b90392012-07-13 15:21:29 -0700652 frame_time = (uint64_t)1000 * ebml->framerate.den
653 / ebml->framerate.num;
654 ebml->segment_info_pos = ftello(ebml->stream);
655 Ebml_StartSubElement(ebml, &startInfo, Info);
656 Ebml_SerializeUnsigned(ebml, TimecodeScale, 1000000);
657 Ebml_SerializeFloat(ebml, Segment_Duration,
John Koleszar6ad3b742012-11-06 12:02:42 -0800658 (double)(ebml->last_pts_ms + frame_time));
659 Ebml_SerializeString(ebml, 0x4D80, version_string);
660 Ebml_SerializeString(ebml, 0x5741, version_string);
John Koleszarc6b90392012-07-13 15:21:29 -0700661 Ebml_EndSubElement(ebml, &startInfo);
662 }
John Koleszardc666302010-10-20 12:05:48 -0400663}
664
665
666static void
667write_webm_file_header(EbmlGlobal *glob,
668 const vpx_codec_enc_cfg_t *cfg,
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700669 const struct vpx_rational *fps,
John Koleszar08b43fe2012-11-13 12:14:28 -0800670 stereo_format_t stereo_fmt,
671 unsigned int fourcc) {
John Koleszarc6b90392012-07-13 15:21:29 -0700672 {
673 EbmlLoc start;
674 Ebml_StartSubElement(glob, &start, EBML);
675 Ebml_SerializeUnsigned(glob, EBMLVersion, 1);
John Koleszar6ad3b742012-11-06 12:02:42 -0800676 Ebml_SerializeUnsigned(glob, EBMLReadVersion, 1);
677 Ebml_SerializeUnsigned(glob, EBMLMaxIDLength, 4);
678 Ebml_SerializeUnsigned(glob, EBMLMaxSizeLength, 8);
679 Ebml_SerializeString(glob, DocType, "webm");
680 Ebml_SerializeUnsigned(glob, DocTypeVersion, 2);
681 Ebml_SerializeUnsigned(glob, DocTypeReadVersion, 2);
John Koleszarc6b90392012-07-13 15:21:29 -0700682 Ebml_EndSubElement(glob, &start);
683 }
684 {
John Koleszar6ad3b742012-11-06 12:02:42 -0800685 Ebml_StartSubElement(glob, &glob->startSegment, Segment);
John Koleszarc6b90392012-07-13 15:21:29 -0700686 glob->position_reference = ftello(glob->stream);
687 glob->framerate = *fps;
688 write_webm_seek_info(glob);
689
John Koleszardc666302010-10-20 12:05:48 -0400690 {
John Koleszarc6b90392012-07-13 15:21:29 -0700691 EbmlLoc trackStart;
692 glob->track_pos = ftello(glob->stream);
693 Ebml_StartSubElement(glob, &trackStart, Tracks);
694 {
695 unsigned int trackNumber = 1;
696 uint64_t trackID = 0;
697
John Koleszardc666302010-10-20 12:05:48 -0400698 EbmlLoc start;
John Koleszarc6b90392012-07-13 15:21:29 -0700699 Ebml_StartSubElement(glob, &start, TrackEntry);
700 Ebml_SerializeUnsigned(glob, TrackNumber, trackNumber);
701 glob->track_id_pos = ftello(glob->stream);
702 Ebml_SerializeUnsigned32(glob, TrackUID, trackID);
John Koleszar6ad3b742012-11-06 12:02:42 -0800703 Ebml_SerializeUnsigned(glob, TrackType, 1);
John Koleszar08b43fe2012-11-13 12:14:28 -0800704 Ebml_SerializeString(glob, CodecID,
705 fourcc == VP8_FOURCC ? "V_VP8" : "V_VP9");
John Koleszardc666302010-10-20 12:05:48 -0400706 {
John Koleszarc6b90392012-07-13 15:21:29 -0700707 unsigned int pixelWidth = cfg->g_w;
708 unsigned int pixelHeight = cfg->g_h;
709 float frameRate = (float)fps->num / (float)fps->den;
John Koleszardc666302010-10-20 12:05:48 -0400710
John Koleszarc6b90392012-07-13 15:21:29 -0700711 EbmlLoc videoStart;
712 Ebml_StartSubElement(glob, &videoStart, Video);
713 Ebml_SerializeUnsigned(glob, PixelWidth, pixelWidth);
714 Ebml_SerializeUnsigned(glob, PixelHeight, pixelHeight);
715 Ebml_SerializeUnsigned(glob, StereoMode, stereo_fmt);
716 Ebml_SerializeFloat(glob, FrameRate, frameRate);
John Koleszar6ad3b742012-11-06 12:02:42 -0800717 Ebml_EndSubElement(glob, &videoStart);
John Koleszardc666302010-10-20 12:05:48 -0400718 }
John Koleszar6ad3b742012-11-06 12:02:42 -0800719 Ebml_EndSubElement(glob, &start); /* Track Entry */
John Koleszarc6b90392012-07-13 15:21:29 -0700720 }
721 Ebml_EndSubElement(glob, &trackStart);
John Koleszardc666302010-10-20 12:05:48 -0400722 }
John Koleszar6ad3b742012-11-06 12:02:42 -0800723 /* segment element is open */
John Koleszarc6b90392012-07-13 15:21:29 -0700724 }
John Koleszardc666302010-10-20 12:05:48 -0400725}
726
727
728static void
729write_webm_block(EbmlGlobal *glob,
730 const vpx_codec_enc_cfg_t *cfg,
John Koleszarc6b90392012-07-13 15:21:29 -0700731 const vpx_codec_cx_pkt_t *pkt) {
732 unsigned long block_length;
733 unsigned char track_number;
734 unsigned short block_timecode = 0;
735 unsigned char flags;
736 int64_t pts_ms;
737 int start_cluster = 0, is_keyframe;
John Koleszardc666302010-10-20 12:05:48 -0400738
John Koleszarc6b90392012-07-13 15:21:29 -0700739 /* Calculate the PTS of this frame in milliseconds */
740 pts_ms = pkt->data.frame.pts * 1000
741 * (uint64_t)cfg->g_timebase.num / (uint64_t)cfg->g_timebase.den;
742 if (pts_ms <= glob->last_pts_ms)
743 pts_ms = glob->last_pts_ms + 1;
744 glob->last_pts_ms = pts_ms;
John Koleszardc666302010-10-20 12:05:48 -0400745
John Koleszarc6b90392012-07-13 15:21:29 -0700746 /* Calculate the relative time of this block */
747 if (pts_ms - glob->cluster_timecode > SHRT_MAX)
748 start_cluster = 1;
749 else
John Koleszar6ad3b742012-11-06 12:02:42 -0800750 block_timecode = (unsigned short)pts_ms - glob->cluster_timecode;
John Koleszardc666302010-10-20 12:05:48 -0400751
John Koleszarc6b90392012-07-13 15:21:29 -0700752 is_keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY);
753 if (start_cluster || is_keyframe) {
754 if (glob->cluster_open)
755 Ebml_EndSubElement(glob, &glob->startCluster);
John Koleszardc666302010-10-20 12:05:48 -0400756
John Koleszarc6b90392012-07-13 15:21:29 -0700757 /* Open the new cluster */
758 block_timecode = 0;
759 glob->cluster_open = 1;
John Koleszar6ad3b742012-11-06 12:02:42 -0800760 glob->cluster_timecode = (uint32_t)pts_ms;
John Koleszarc6b90392012-07-13 15:21:29 -0700761 glob->cluster_pos = ftello(glob->stream);
John Koleszar6ad3b742012-11-06 12:02:42 -0800762 Ebml_StartSubElement(glob, &glob->startCluster, Cluster); /* cluster */
John Koleszarc6b90392012-07-13 15:21:29 -0700763 Ebml_SerializeUnsigned(glob, Timecode, glob->cluster_timecode);
John Koleszardc666302010-10-20 12:05:48 -0400764
John Koleszarc6b90392012-07-13 15:21:29 -0700765 /* Save a cue point if this is a keyframe. */
766 if (is_keyframe) {
767 struct cue_entry *cue, *new_cue_list;
John Koleszardc666302010-10-20 12:05:48 -0400768
John Koleszarc6b90392012-07-13 15:21:29 -0700769 new_cue_list = realloc(glob->cue_list,
770 (glob->cues + 1) * sizeof(struct cue_entry));
771 if (new_cue_list)
772 glob->cue_list = new_cue_list;
John Koleszar6ad3b742012-11-06 12:02:42 -0800773 else
774 fatal("Failed to realloc cue list.");
John Koleszar26b6a3b2011-03-29 21:44:19 -0400775
John Koleszarc6b90392012-07-13 15:21:29 -0700776 cue = &glob->cue_list[glob->cues];
777 cue->time = glob->cluster_timecode;
778 cue->loc = glob->cluster_pos;
779 glob->cues++;
John Koleszardc666302010-10-20 12:05:48 -0400780 }
John Koleszarc6b90392012-07-13 15:21:29 -0700781 }
John Koleszardc666302010-10-20 12:05:48 -0400782
John Koleszarc6b90392012-07-13 15:21:29 -0700783 /* Write the Simple Block */
784 Ebml_WriteID(glob, SimpleBlock);
John Koleszardc666302010-10-20 12:05:48 -0400785
John Koleszar6ad3b742012-11-06 12:02:42 -0800786 block_length = (unsigned long)pkt->data.frame.sz + 4;
John Koleszarc6b90392012-07-13 15:21:29 -0700787 block_length |= 0x10000000;
788 Ebml_Serialize(glob, &block_length, sizeof(block_length), 4);
John Koleszardc666302010-10-20 12:05:48 -0400789
John Koleszarc6b90392012-07-13 15:21:29 -0700790 track_number = 1;
791 track_number |= 0x80;
792 Ebml_Write(glob, &track_number, 1);
John Koleszardc666302010-10-20 12:05:48 -0400793
John Koleszarc6b90392012-07-13 15:21:29 -0700794 Ebml_Serialize(glob, &block_timecode, sizeof(block_timecode), 2);
John Koleszardc666302010-10-20 12:05:48 -0400795
John Koleszarc6b90392012-07-13 15:21:29 -0700796 flags = 0;
797 if (is_keyframe)
798 flags |= 0x80;
799 if (pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE)
800 flags |= 0x08;
801 Ebml_Write(glob, &flags, 1);
John Koleszardc666302010-10-20 12:05:48 -0400802
John Koleszar6ad3b742012-11-06 12:02:42 -0800803 Ebml_Write(glob, pkt->data.frame.buf, (unsigned long)pkt->data.frame.sz);
John Koleszardc666302010-10-20 12:05:48 -0400804}
805
806
807static void
John Koleszarc6b90392012-07-13 15:21:29 -0700808write_webm_file_footer(EbmlGlobal *glob, long hash) {
John Koleszardc666302010-10-20 12:05:48 -0400809
John Koleszarc6b90392012-07-13 15:21:29 -0700810 if (glob->cluster_open)
811 Ebml_EndSubElement(glob, &glob->startCluster);
John Koleszardc666302010-10-20 12:05:48 -0400812
John Koleszarc6b90392012-07-13 15:21:29 -0700813 {
814 EbmlLoc start;
John Koleszar6ad3b742012-11-06 12:02:42 -0800815 unsigned int i;
John Koleszarc6b90392012-07-13 15:21:29 -0700816
817 glob->cue_pos = ftello(glob->stream);
818 Ebml_StartSubElement(glob, &start, Cues);
819 for (i = 0; i < glob->cues; i++) {
820 struct cue_entry *cue = &glob->cue_list[i];
821 EbmlLoc start;
822
823 Ebml_StartSubElement(glob, &start, CuePoint);
824 {
John Koleszardc666302010-10-20 12:05:48 -0400825 EbmlLoc start;
John Koleszardc666302010-10-20 12:05:48 -0400826
John Koleszarc6b90392012-07-13 15:21:29 -0700827 Ebml_SerializeUnsigned(glob, CueTime, cue->time);
John Koleszardc666302010-10-20 12:05:48 -0400828
John Koleszarc6b90392012-07-13 15:21:29 -0700829 Ebml_StartSubElement(glob, &start, CueTrackPositions);
830 Ebml_SerializeUnsigned(glob, CueTrack, 1);
831 Ebml_SerializeUnsigned64(glob, CueClusterPosition,
832 cue->loc - glob->position_reference);
John Koleszardc666302010-10-20 12:05:48 -0400833 Ebml_EndSubElement(glob, &start);
John Koleszarc6b90392012-07-13 15:21:29 -0700834 }
835 Ebml_EndSubElement(glob, &start);
John Koleszardc666302010-10-20 12:05:48 -0400836 }
John Koleszarc6b90392012-07-13 15:21:29 -0700837 Ebml_EndSubElement(glob, &start);
838 }
John Koleszardc666302010-10-20 12:05:48 -0400839
John Koleszarc6b90392012-07-13 15:21:29 -0700840 Ebml_EndSubElement(glob, &glob->startSegment);
John Koleszardc666302010-10-20 12:05:48 -0400841
John Koleszarc6b90392012-07-13 15:21:29 -0700842 /* Patch up the seek info block */
843 write_webm_seek_info(glob);
John Koleszarc7c0c052010-10-27 10:05:55 -0400844
John Koleszarc6b90392012-07-13 15:21:29 -0700845 /* Patch up the track id */
846 fseeko(glob->stream, glob->track_id_pos, SEEK_SET);
847 Ebml_SerializeUnsigned32(glob, TrackUID, glob->debug ? 0xDEADBEEF : hash);
John Koleszarc7c0c052010-10-27 10:05:55 -0400848
John Koleszarc6b90392012-07-13 15:21:29 -0700849 fseeko(glob->stream, 0, SEEK_END);
John Koleszardc666302010-10-20 12:05:48 -0400850}
851
852
John Koleszarc7c0c052010-10-27 10:05:55 -0400853/* Murmur hash derived from public domain reference implementation at
John Koleszarc6b90392012-07-13 15:21:29 -0700854 * http:// sites.google.com/site/murmurhash/
John Koleszarc7c0c052010-10-27 10:05:55 -0400855 */
John Koleszarc6b90392012-07-13 15:21:29 -0700856static unsigned int murmur(const void *key, int len, unsigned int seed) {
857 const unsigned int m = 0x5bd1e995;
858 const int r = 24;
John Koleszarc7c0c052010-10-27 10:05:55 -0400859
John Koleszarc6b90392012-07-13 15:21:29 -0700860 unsigned int h = seed ^ len;
John Koleszarc7c0c052010-10-27 10:05:55 -0400861
John Koleszarc6b90392012-07-13 15:21:29 -0700862 const unsigned char *data = (const unsigned char *)key;
John Koleszarc7c0c052010-10-27 10:05:55 -0400863
John Koleszarc6b90392012-07-13 15:21:29 -0700864 while (len >= 4) {
865 unsigned int k;
John Koleszarc7c0c052010-10-27 10:05:55 -0400866
John Koleszarc6b90392012-07-13 15:21:29 -0700867 k = data[0];
868 k |= data[1] << 8;
869 k |= data[2] << 16;
870 k |= data[3] << 24;
John Koleszarc7c0c052010-10-27 10:05:55 -0400871
John Koleszarc6b90392012-07-13 15:21:29 -0700872 k *= m;
873 k ^= k >> r;
874 k *= m;
John Koleszarc7c0c052010-10-27 10:05:55 -0400875
John Koleszarc7c0c052010-10-27 10:05:55 -0400876 h *= m;
John Koleszarc6b90392012-07-13 15:21:29 -0700877 h ^= k;
John Koleszarc7c0c052010-10-27 10:05:55 -0400878
John Koleszarc6b90392012-07-13 15:21:29 -0700879 data += 4;
880 len -= 4;
881 }
882
883 switch (len) {
884 case 3:
885 h ^= data[2] << 16;
886 case 2:
887 h ^= data[1] << 8;
888 case 1:
889 h ^= data[0];
890 h *= m;
891 };
892
893 h ^= h >> 13;
894 h *= m;
895 h ^= h >> 15;
896
897 return h;
John Koleszarc7c0c052010-10-27 10:05:55 -0400898}
899
Frank Galligan3d84da62010-10-27 11:28:56 -0400900#include "math.h"
Yaowu Xu8cf28d32012-03-07 09:05:26 -0800901#define MAX_PSNR 100
John Koleszarc6b90392012-07-13 15:21:29 -0700902static double vp8_mse2psnr(double Samples, double Peak, double Mse) {
903 double psnr;
Frank Galligan3d84da62010-10-27 11:28:56 -0400904
John Koleszarc6b90392012-07-13 15:21:29 -0700905 if ((double)Mse > 0.0)
906 psnr = 10.0 * log10(Peak * Peak * Samples / Mse);
907 else
John Koleszar6ad3b742012-11-06 12:02:42 -0800908 psnr = MAX_PSNR; /* Limit to prevent / 0 */
Frank Galligan3d84da62010-10-27 11:28:56 -0400909
John Koleszarc6b90392012-07-13 15:21:29 -0700910 if (psnr > MAX_PSNR)
911 psnr = MAX_PSNR;
Frank Galligan3d84da62010-10-27 11:28:56 -0400912
John Koleszarc6b90392012-07-13 15:21:29 -0700913 return psnr;
Frank Galligan3d84da62010-10-27 11:28:56 -0400914}
915
John Koleszarc7c0c052010-10-27 10:05:55 -0400916
John Koleszar0ea50ce2010-05-18 11:58:33 -0400917#include "args.h"
John Koleszar75afcee2010-10-26 16:22:22 -0400918static const arg_def_t debugmode = ARG_DEF("D", "debug", 0,
John Koleszarc6b90392012-07-13 15:21:29 -0700919 "Debug mode (makes output deterministic)");
John Koleszar53291892010-10-22 14:48:21 -0400920static const arg_def_t outputfile = ARG_DEF("o", "output", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700921 "Output filename");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400922static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0,
John Koleszarc6b90392012-07-13 15:21:29 -0700923 "Input file is YV12 ");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400924static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0,
John Koleszarc6b90392012-07-13 15:21:29 -0700925 "Input file is I420 (default)");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400926static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700927 "Codec to use");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400928static const arg_def_t passes = ARG_DEF("p", "passes", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700929 "Number of passes (1/2)");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400930static const arg_def_t pass_arg = ARG_DEF(NULL, "pass", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700931 "Pass to execute (1/2)");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400932static const arg_def_t fpf_name = ARG_DEF(NULL, "fpf", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700933 "First pass statistics file name");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400934static const arg_def_t limit = ARG_DEF(NULL, "limit", 1,
935 "Stop encoding after n input frames");
Yaowu Xu03d5d692012-03-14 07:37:10 -0700936static const arg_def_t skip = ARG_DEF(NULL, "skip", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700937 "Skip the first n input frames");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400938static const arg_def_t deadline = ARG_DEF("d", "deadline", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700939 "Deadline per frame (usec)");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400940static const arg_def_t best_dl = ARG_DEF(NULL, "best", 0,
John Koleszarc6b90392012-07-13 15:21:29 -0700941 "Use Best Quality Deadline");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400942static const arg_def_t good_dl = ARG_DEF(NULL, "good", 0,
John Koleszarc6b90392012-07-13 15:21:29 -0700943 "Use Good Quality Deadline");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400944static const arg_def_t rt_dl = ARG_DEF(NULL, "rt", 0,
John Koleszarc6b90392012-07-13 15:21:29 -0700945 "Use Realtime Quality Deadline");
James Zern1fc3cc82012-10-25 18:31:35 -0700946static const arg_def_t quietarg = ARG_DEF("q", "quiet", 0,
John Koleszar6ad3b742012-11-06 12:02:42 -0800947 "Do not print encode progress");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400948static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0,
John Koleszarc6b90392012-07-13 15:21:29 -0700949 "Show encoder parameters");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400950static const arg_def_t psnrarg = ARG_DEF(NULL, "psnr", 0,
John Koleszarc6b90392012-07-13 15:21:29 -0700951 "Show PSNR in status line");
Yaowu Xuf798f9e2012-03-07 12:25:50 -0800952static const arg_def_t recontest = ARG_DEF(NULL, "test-decode", 0,
John Koleszar6ad3b742012-11-06 12:02:42 -0800953 "Test encode/decode mismatch");
John Koleszar28f177c2010-10-21 16:52:14 -0400954static const arg_def_t framerate = ARG_DEF(NULL, "fps", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700955 "Stream frame rate (rate/scale)");
John Koleszar456bfb12010-10-21 16:23:20 -0400956static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0,
John Koleszarc6b90392012-07-13 15:21:29 -0700957 "Output IVF (default is WebM)");
Attila Nagy52cf4dc2012-02-09 12:37:03 +0200958static const arg_def_t out_part = ARG_DEF("P", "output-partitions", 0,
John Koleszar6ad3b742012-11-06 12:02:42 -0800959 "Makes encoder output partitions. Requires IVF output!");
John Koleszar3fde9962011-06-20 17:09:29 -0400960static const arg_def_t q_hist_n = ARG_DEF(NULL, "q-hist", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700961 "Show quantizer histogram (n-buckets)");
John Koleszarc96f8e22011-06-21 16:42:33 -0400962static const arg_def_t rate_hist_n = ARG_DEF(NULL, "rate-hist", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700963 "Show rate histogram (n-buckets)");
John Koleszarc6b90392012-07-13 15:21:29 -0700964static const arg_def_t *main_args[] = {
965 &debugmode,
966 &outputfile, &codecarg, &passes, &pass_arg, &fpf_name, &limit, &skip,
John Koleszar6ad3b742012-11-06 12:02:42 -0800967 &deadline, &best_dl, &good_dl, &rt_dl,
968 &quietarg, &verbosearg, &psnrarg, &use_ivf, &out_part, &q_hist_n, &rate_hist_n,
John Koleszarc6b90392012-07-13 15:21:29 -0700969 NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -0400970};
971
972static const arg_def_t usage = ARG_DEF("u", "usage", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700973 "Usage profile number to use");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400974static const arg_def_t threads = ARG_DEF("t", "threads", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700975 "Max number of threads to use");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400976static const arg_def_t profile = ARG_DEF(NULL, "profile", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700977 "Bitstream profile number to use");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400978static const arg_def_t width = ARG_DEF("w", "width", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700979 "Frame width");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400980static const arg_def_t height = ARG_DEF("h", "height", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700981 "Frame height");
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700982static const struct arg_enum_list stereo_mode_enum[] = {
John Koleszarc6b90392012-07-13 15:21:29 -0700983 {"mono", STEREO_FORMAT_MONO},
984 {"left-right", STEREO_FORMAT_LEFT_RIGHT},
985 {"bottom-top", STEREO_FORMAT_BOTTOM_TOP},
986 {"top-bottom", STEREO_FORMAT_TOP_BOTTOM},
987 {"right-left", STEREO_FORMAT_RIGHT_LEFT},
988 {NULL, 0}
Alok Ahuja72c76ca2011-04-21 00:50:07 -0700989};
990static const arg_def_t stereo_mode = ARG_DEF_ENUM(NULL, "stereo-mode", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700991 "Stereo 3D video format", stereo_mode_enum);
John Koleszar0ea50ce2010-05-18 11:58:33 -0400992static const arg_def_t timebase = ARG_DEF(NULL, "timebase", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700993 "Output timestamp precision (fractional seconds)");
John Koleszar0ea50ce2010-05-18 11:58:33 -0400994static const arg_def_t error_resilient = ARG_DEF(NULL, "error-resilient", 1,
John Koleszarc6b90392012-07-13 15:21:29 -0700995 "Enable error resiliency features");
Deb Mukherjeedfd89f22013-01-25 11:30:28 -0800996#if CONFIG_VP9_ENCODER
997static const arg_def_t frame_parallel_decoding = ARG_DEF(
998 NULL, "frame-parallel", 1, "Enable frame parallel decodability features");
999#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -04001000static const arg_def_t lag_in_frames = ARG_DEF(NULL, "lag-in-frames", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001001 "Max number of frames to lag");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001002
John Koleszarc6b90392012-07-13 15:21:29 -07001003static const arg_def_t *global_args[] = {
1004 &use_yv12, &use_i420, &usage, &threads, &profile,
Deb Mukherjeedfd89f22013-01-25 11:30:28 -08001005 &width, &height, &stereo_mode, &timebase, &framerate,
1006 &error_resilient,
1007#if CONFIG_VP9_ENCODER
1008 &frame_parallel_decoding,
1009#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001010 &lag_in_frames, NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -04001011};
1012
1013static const arg_def_t dropframe_thresh = ARG_DEF(NULL, "drop-frame", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001014 "Temporal resampling threshold (buf %)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001015static const arg_def_t resize_allowed = ARG_DEF(NULL, "resize-allowed", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001016 "Spatial resampling enabled (bool)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001017static const arg_def_t resize_up_thresh = ARG_DEF(NULL, "resize-up", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001018 "Upscale threshold (buf %)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001019static const arg_def_t resize_down_thresh = ARG_DEF(NULL, "resize-down", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001020 "Downscale threshold (buf %)");
John Koleszar500fec22011-03-21 07:50:42 -04001021static const struct arg_enum_list end_usage_enum[] = {
John Koleszarc6b90392012-07-13 15:21:29 -07001022 {"vbr", VPX_VBR},
1023 {"cbr", VPX_CBR},
1024 {"cq", VPX_CQ},
1025 {NULL, 0}
John Koleszar500fec22011-03-21 07:50:42 -04001026};
1027static const arg_def_t end_usage = ARG_DEF_ENUM(NULL, "end-usage", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001028 "Rate control mode", end_usage_enum);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001029static const arg_def_t target_bitrate = ARG_DEF(NULL, "target-bitrate", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001030 "Bitrate (kbps)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001031static const arg_def_t min_quantizer = ARG_DEF(NULL, "min-q", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001032 "Minimum (best) quantizer");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001033static const arg_def_t max_quantizer = ARG_DEF(NULL, "max-q", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001034 "Maximum (worst) quantizer");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001035static const arg_def_t undershoot_pct = ARG_DEF(NULL, "undershoot-pct", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001036 "Datarate undershoot (min) target (%)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001037static const arg_def_t overshoot_pct = ARG_DEF(NULL, "overshoot-pct", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001038 "Datarate overshoot (max) target (%)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001039static const arg_def_t buf_sz = ARG_DEF(NULL, "buf-sz", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001040 "Client buffer size (ms)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001041static const arg_def_t buf_initial_sz = ARG_DEF(NULL, "buf-initial-sz", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001042 "Client initial buffer size (ms)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001043static const arg_def_t buf_optimal_sz = ARG_DEF(NULL, "buf-optimal-sz", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001044 "Client optimal buffer size (ms)");
1045static const arg_def_t *rc_args[] = {
1046 &dropframe_thresh, &resize_allowed, &resize_up_thresh, &resize_down_thresh,
1047 &end_usage, &target_bitrate, &min_quantizer, &max_quantizer,
1048 &undershoot_pct, &overshoot_pct, &buf_sz, &buf_initial_sz, &buf_optimal_sz,
1049 NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -04001050};
1051
1052
1053static const arg_def_t bias_pct = ARG_DEF(NULL, "bias-pct", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001054 "CBR/VBR bias (0=CBR, 100=VBR)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001055static const arg_def_t minsection_pct = ARG_DEF(NULL, "minsection-pct", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001056 "GOP min bitrate (% of target)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001057static const arg_def_t maxsection_pct = ARG_DEF(NULL, "maxsection-pct", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001058 "GOP max bitrate (% of target)");
1059static const arg_def_t *rc_twopass_args[] = {
1060 &bias_pct, &minsection_pct, &maxsection_pct, NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -04001061};
1062
1063
1064static const arg_def_t kf_min_dist = ARG_DEF(NULL, "kf-min-dist", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001065 "Minimum keyframe interval (frames)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001066static const arg_def_t kf_max_dist = ARG_DEF(NULL, "kf-max-dist", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001067 "Maximum keyframe interval (frames)");
John Koleszarb1e36f22010-06-24 08:41:51 -04001068static const arg_def_t kf_disabled = ARG_DEF(NULL, "disable-kf", 0,
John Koleszarc6b90392012-07-13 15:21:29 -07001069 "Disable keyframe placement");
1070static const arg_def_t *kf_args[] = {
1071 &kf_min_dist, &kf_max_dist, &kf_disabled, NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -04001072};
1073
1074
John Koleszar0ea50ce2010-05-18 11:58:33 -04001075static const arg_def_t noise_sens = ARG_DEF(NULL, "noise-sensitivity", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001076 "Noise sensitivity (frames to blur)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001077static const arg_def_t sharpness = ARG_DEF(NULL, "sharpness", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001078 "Filter sharpness (0-7)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001079static const arg_def_t static_thresh = ARG_DEF(NULL, "static-thresh", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001080 "Motion detection threshold");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001081static const arg_def_t cpu_used = ARG_DEF(NULL, "cpu-used", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001082 "CPU Used (-16..16)");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001083static const arg_def_t token_parts = ARG_DEF(NULL, "token-parts", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001084 "Number of token partitions to use, log2");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001085static const arg_def_t auto_altref = ARG_DEF(NULL, "auto-alt-ref", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001086 "Enable automatic alt reference frames");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001087static const arg_def_t arnr_maxframes = ARG_DEF(NULL, "arnr-maxframes", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001088 "AltRef Max Frames");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001089static const arg_def_t arnr_strength = ARG_DEF(NULL, "arnr-strength", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001090 "AltRef Strength");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001091static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001092 "AltRef Type");
John Koleszarb0da9b32010-12-17 09:43:39 -05001093static const struct arg_enum_list tuning_enum[] = {
John Koleszarc6b90392012-07-13 15:21:29 -07001094 {"psnr", VP8_TUNE_PSNR},
1095 {"ssim", VP8_TUNE_SSIM},
1096 {NULL, 0}
John Koleszarb0da9b32010-12-17 09:43:39 -05001097};
1098static const arg_def_t tune_ssim = ARG_DEF_ENUM(NULL, "tune", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001099 "Material to favor", tuning_enum);
Paul Wilkinse0846c92011-01-07 18:29:37 +00001100static const arg_def_t cq_level = ARG_DEF(NULL, "cq-level", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001101 "Constrained Quality Level");
John Koleszar1654ae92011-07-28 09:17:32 -04001102static const arg_def_t max_intra_rate_pct = ARG_DEF(NULL, "max-intra-rate", 1,
John Koleszarc6b90392012-07-13 15:21:29 -07001103 "Max I-frame bitrate (pct)");
John Koleszar6ad3b742012-11-06 12:02:42 -08001104#if CONFIG_LOSSLESS
1105static const arg_def_t lossless = ARG_DEF(NULL, "lossless", 1, "Lossless mode");
1106#endif
John Koleszar0ea50ce2010-05-18 11:58:33 -04001107
John Koleszara9c75972012-11-08 17:09:30 -08001108#if CONFIG_VP8_ENCODER
John Koleszarc6b90392012-07-13 15:21:29 -07001109static const arg_def_t *vp8_args[] = {
1110 &cpu_used, &auto_altref, &noise_sens, &sharpness, &static_thresh,
1111 &token_parts, &arnr_maxframes, &arnr_strength, &arnr_type,
John Koleszar83b1d902012-11-05 12:37:14 -08001112 &tune_ssim, &cq_level, &max_intra_rate_pct,
John Koleszara9c75972012-11-08 17:09:30 -08001113 NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -04001114};
John Koleszara9c75972012-11-08 17:09:30 -08001115static const int vp8_arg_ctrl_map[] = {
1116 VP8E_SET_CPUUSED, VP8E_SET_ENABLEAUTOALTREF,
1117 VP8E_SET_NOISE_SENSITIVITY, VP8E_SET_SHARPNESS, VP8E_SET_STATIC_THRESHOLD,
1118 VP8E_SET_TOKEN_PARTITIONS,
1119 VP8E_SET_ARNR_MAXFRAMES, VP8E_SET_ARNR_STRENGTH, VP8E_SET_ARNR_TYPE,
1120 VP8E_SET_TUNING, VP8E_SET_CQ_LEVEL, VP8E_SET_MAX_INTRA_BITRATE_PCT,
1121 0
1122};
1123#endif
1124
1125#if CONFIG_VP9_ENCODER
1126static const arg_def_t *vp9_args[] = {
1127 &cpu_used, &auto_altref, &noise_sens, &sharpness, &static_thresh,
1128 &token_parts, &arnr_maxframes, &arnr_strength, &arnr_type,
1129 &tune_ssim, &cq_level, &max_intra_rate_pct,
John Koleszar83b1d902012-11-05 12:37:14 -08001130#if CONFIG_LOSSLESS
John Koleszar6ad3b742012-11-06 12:02:42 -08001131 &lossless,
John Koleszar83b1d902012-11-05 12:37:14 -08001132#endif
1133 NULL
John Koleszar0ea50ce2010-05-18 11:58:33 -04001134};
John Koleszara9c75972012-11-08 17:09:30 -08001135static const int vp9_arg_ctrl_map[] = {
John Koleszarc6b90392012-07-13 15:21:29 -07001136 VP8E_SET_CPUUSED, VP8E_SET_ENABLEAUTOALTREF,
1137 VP8E_SET_NOISE_SENSITIVITY, VP8E_SET_SHARPNESS, VP8E_SET_STATIC_THRESHOLD,
1138 VP8E_SET_TOKEN_PARTITIONS,
1139 VP8E_SET_ARNR_MAXFRAMES, VP8E_SET_ARNR_STRENGTH, VP8E_SET_ARNR_TYPE,
John Koleszar83b1d902012-11-05 12:37:14 -08001140 VP8E_SET_TUNING, VP8E_SET_CQ_LEVEL, VP8E_SET_MAX_INTRA_BITRATE_PCT,
1141#if CONFIG_LOSSLESS
1142 VP9E_SET_LOSSLESS,
1143#endif
1144 0
John Koleszar0ea50ce2010-05-18 11:58:33 -04001145};
1146#endif
1147
1148static const arg_def_t *no_args[] = { NULL };
1149
John Koleszarc6b90392012-07-13 15:21:29 -07001150static void usage_exit() {
1151 int i;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001152
John Koleszarc6b90392012-07-13 15:21:29 -07001153 fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n",
1154 exec_name);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001155
John Koleszarc6b90392012-07-13 15:21:29 -07001156 fprintf(stderr, "\nOptions:\n");
1157 arg_show_usage(stdout, main_args);
1158 fprintf(stderr, "\nEncoder Global Options:\n");
1159 arg_show_usage(stdout, global_args);
1160 fprintf(stderr, "\nRate Control Options:\n");
1161 arg_show_usage(stdout, rc_args);
1162 fprintf(stderr, "\nTwopass Rate Control Options:\n");
1163 arg_show_usage(stdout, rc_twopass_args);
1164 fprintf(stderr, "\nKeyframe Placement Options:\n");
1165 arg_show_usage(stdout, kf_args);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001166#if CONFIG_VP8_ENCODER
John Koleszarc6b90392012-07-13 15:21:29 -07001167 fprintf(stderr, "\nVP8 Specific Options:\n");
1168 arg_show_usage(stdout, vp8_args);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001169#endif
John Koleszara9c75972012-11-08 17:09:30 -08001170#if CONFIG_VP9_ENCODER
1171 fprintf(stderr, "\nVP9 Specific Options:\n");
1172 arg_show_usage(stdout, vp9_args);
1173#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001174 fprintf(stderr, "\nStream timebase (--timebase):\n"
1175 " The desired precision of timestamps in the output, expressed\n"
1176 " in fractional seconds. Default is 1/1000.\n");
1177 fprintf(stderr, "\n"
1178 "Included encoders:\n"
1179 "\n");
John Koleszar0ea50ce2010-05-18 11:58:33 -04001180
John Koleszarc6b90392012-07-13 15:21:29 -07001181 for (i = 0; i < sizeof(codecs) / sizeof(codecs[0]); i++)
1182 fprintf(stderr, " %-6s - %s\n",
1183 codecs[i].name,
Jim Bankoski1b16e742012-07-23 12:32:59 -07001184 vpx_codec_iface_name(codecs[i].iface()));
John Koleszar0ea50ce2010-05-18 11:58:33 -04001185
John Koleszarc6b90392012-07-13 15:21:29 -07001186 exit(EXIT_FAILURE);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001187}
1188
John Koleszar3fde9962011-06-20 17:09:29 -04001189
1190#define HIST_BAR_MAX 40
John Koleszarc6b90392012-07-13 15:21:29 -07001191struct hist_bucket {
1192 int low, high, count;
John Koleszar3fde9962011-06-20 17:09:29 -04001193};
1194
1195
1196static int merge_hist_buckets(struct hist_bucket *bucket,
1197 int *buckets_,
John Koleszarc6b90392012-07-13 15:21:29 -07001198 int max_buckets) {
1199 int small_bucket = 0, merge_bucket = INT_MAX, big_bucket = 0;
1200 int buckets = *buckets_;
1201 int i;
John Koleszar3fde9962011-06-20 17:09:29 -04001202
John Koleszarc6b90392012-07-13 15:21:29 -07001203 /* Find the extrema for this list of buckets */
1204 big_bucket = small_bucket = 0;
1205 for (i = 0; i < buckets; i++) {
1206 if (bucket[i].count < bucket[small_bucket].count)
1207 small_bucket = i;
1208 if (bucket[i].count > bucket[big_bucket].count)
1209 big_bucket = i;
1210 }
1211
1212 /* If we have too many buckets, merge the smallest with an adjacent
1213 * bucket.
1214 */
1215 while (buckets > max_buckets) {
1216 int last_bucket = buckets - 1;
1217
John Koleszar6ad3b742012-11-06 12:02:42 -08001218 /* merge the small bucket with an adjacent one. */
John Koleszarc6b90392012-07-13 15:21:29 -07001219 if (small_bucket == 0)
1220 merge_bucket = 1;
1221 else if (small_bucket == last_bucket)
1222 merge_bucket = last_bucket - 1;
1223 else if (bucket[small_bucket - 1].count < bucket[small_bucket + 1].count)
1224 merge_bucket = small_bucket - 1;
1225 else
1226 merge_bucket = small_bucket + 1;
1227
1228 assert(abs(merge_bucket - small_bucket) <= 1);
1229 assert(small_bucket < buckets);
1230 assert(big_bucket < buckets);
1231 assert(merge_bucket < buckets);
1232
1233 if (merge_bucket < small_bucket) {
1234 bucket[merge_bucket].high = bucket[small_bucket].high;
1235 bucket[merge_bucket].count += bucket[small_bucket].count;
1236 } else {
1237 bucket[small_bucket].high = bucket[merge_bucket].high;
1238 bucket[small_bucket].count += bucket[merge_bucket].count;
1239 merge_bucket = small_bucket;
John Koleszar3fde9962011-06-20 17:09:29 -04001240 }
1241
John Koleszarc6b90392012-07-13 15:21:29 -07001242 assert(bucket[merge_bucket].low != bucket[merge_bucket].high);
1243
1244 buckets--;
1245
1246 /* Remove the merge_bucket from the list, and find the new small
1247 * and big buckets while we're at it
John Koleszar3fde9962011-06-20 17:09:29 -04001248 */
John Koleszarc6b90392012-07-13 15:21:29 -07001249 big_bucket = small_bucket = 0;
1250 for (i = 0; i < buckets; i++) {
1251 if (i > merge_bucket)
1252 bucket[i] = bucket[i + 1];
John Koleszar3fde9962011-06-20 17:09:29 -04001253
John Koleszarc6b90392012-07-13 15:21:29 -07001254 if (bucket[i].count < bucket[small_bucket].count)
1255 small_bucket = i;
1256 if (bucket[i].count > bucket[big_bucket].count)
1257 big_bucket = i;
John Koleszar3fde9962011-06-20 17:09:29 -04001258 }
1259
John Koleszarc6b90392012-07-13 15:21:29 -07001260 }
1261
1262 *buckets_ = buckets;
1263 return bucket[big_bucket].count;
John Koleszar3fde9962011-06-20 17:09:29 -04001264}
1265
1266
1267static void show_histogram(const struct hist_bucket *bucket,
1268 int buckets,
1269 int total,
John Koleszarc6b90392012-07-13 15:21:29 -07001270 int scale) {
1271 const char *pat1, *pat2;
1272 int i;
John Koleszar3fde9962011-06-20 17:09:29 -04001273
John Koleszarc6b90392012-07-13 15:21:29 -07001274 switch ((int)(log(bucket[buckets - 1].high) / log(10)) + 1) {
1275 case 1:
1276 case 2:
1277 pat1 = "%4d %2s: ";
1278 pat2 = "%4d-%2d: ";
1279 break;
1280 case 3:
1281 pat1 = "%5d %3s: ";
1282 pat2 = "%5d-%3d: ";
1283 break;
1284 case 4:
1285 pat1 = "%6d %4s: ";
1286 pat2 = "%6d-%4d: ";
1287 break;
1288 case 5:
1289 pat1 = "%7d %5s: ";
1290 pat2 = "%7d-%5d: ";
1291 break;
1292 case 6:
1293 pat1 = "%8d %6s: ";
1294 pat2 = "%8d-%6d: ";
1295 break;
1296 case 7:
1297 pat1 = "%9d %7s: ";
1298 pat2 = "%9d-%7d: ";
1299 break;
1300 default:
1301 pat1 = "%12d %10s: ";
1302 pat2 = "%12d-%10d: ";
1303 break;
1304 }
John Koleszarc96f8e22011-06-21 16:42:33 -04001305
John Koleszarc6b90392012-07-13 15:21:29 -07001306 for (i = 0; i < buckets; i++) {
1307 int len;
1308 int j;
1309 float pct;
John Koleszar3fde9962011-06-20 17:09:29 -04001310
John Koleszar6ad3b742012-11-06 12:02:42 -08001311 pct = (float)(100.0 * bucket[i].count / total);
John Koleszarc6b90392012-07-13 15:21:29 -07001312 len = HIST_BAR_MAX * bucket[i].count / scale;
1313 if (len < 1)
1314 len = 1;
1315 assert(len <= HIST_BAR_MAX);
John Koleszar3fde9962011-06-20 17:09:29 -04001316
John Koleszarc6b90392012-07-13 15:21:29 -07001317 if (bucket[i].low == bucket[i].high)
1318 fprintf(stderr, pat1, bucket[i].low, "");
1319 else
1320 fprintf(stderr, pat2, bucket[i].low, bucket[i].high);
John Koleszar3fde9962011-06-20 17:09:29 -04001321
John Koleszarc6b90392012-07-13 15:21:29 -07001322 for (j = 0; j < HIST_BAR_MAX; j++)
1323 fprintf(stderr, j < len ? "=" : " ");
1324 fprintf(stderr, "\t%5d (%6.2f%%)\n", bucket[i].count, pct);
1325 }
John Koleszar3fde9962011-06-20 17:09:29 -04001326}
1327
1328
John Koleszarc6b90392012-07-13 15:21:29 -07001329static void show_q_histogram(const int counts[64], int max_buckets) {
1330 struct hist_bucket bucket[64];
1331 int buckets = 0;
1332 int total = 0;
1333 int scale;
1334 int i;
John Koleszar3fde9962011-06-20 17:09:29 -04001335
1336
John Koleszarc6b90392012-07-13 15:21:29 -07001337 for (i = 0; i < 64; i++) {
1338 if (counts[i]) {
1339 bucket[buckets].low = bucket[buckets].high = i;
1340 bucket[buckets].count = counts[i];
1341 buckets++;
1342 total += counts[i];
John Koleszar3fde9962011-06-20 17:09:29 -04001343 }
John Koleszarc6b90392012-07-13 15:21:29 -07001344 }
John Koleszar3fde9962011-06-20 17:09:29 -04001345
John Koleszarc6b90392012-07-13 15:21:29 -07001346 fprintf(stderr, "\nQuantizer Selection:\n");
1347 scale = merge_hist_buckets(bucket, &buckets, max_buckets);
1348 show_histogram(bucket, buckets, total, scale);
John Koleszar3fde9962011-06-20 17:09:29 -04001349}
1350
1351
John Koleszarc96f8e22011-06-21 16:42:33 -04001352#define RATE_BINS (100)
John Koleszarc6b90392012-07-13 15:21:29 -07001353struct rate_hist {
1354 int64_t *pts;
1355 int *sz;
1356 int samples;
1357 int frames;
1358 struct hist_bucket bucket[RATE_BINS];
1359 int total;
John Koleszarc96f8e22011-06-21 16:42:33 -04001360};
1361
1362
1363static void init_rate_histogram(struct rate_hist *hist,
John Koleszar05239f02011-06-29 09:53:12 -04001364 const vpx_codec_enc_cfg_t *cfg,
John Koleszarc6b90392012-07-13 15:21:29 -07001365 const vpx_rational_t *fps) {
1366 int i;
John Koleszarc96f8e22011-06-21 16:42:33 -04001367
John Koleszarc6b90392012-07-13 15:21:29 -07001368 /* Determine the number of samples in the buffer. Use the file's framerate
1369 * to determine the number of frames in rc_buf_sz milliseconds, with an
1370 * adjustment (5/4) to account for alt-refs
1371 */
1372 hist->samples = cfg->rc_buf_sz * 5 / 4 * fps->num / fps->den / 1000;
Tero Rintaluomafd41cb82011-07-15 14:47:44 +03001373
John Koleszar6ad3b742012-11-06 12:02:42 -08001374 /* prevent division by zero */
John Koleszarc6b90392012-07-13 15:21:29 -07001375 if (hist->samples == 0)
1376 hist->samples = 1;
Tero Rintaluomafd41cb82011-07-15 14:47:44 +03001377
John Koleszarc6b90392012-07-13 15:21:29 -07001378 hist->pts = calloc(hist->samples, sizeof(*hist->pts));
1379 hist->sz = calloc(hist->samples, sizeof(*hist->sz));
1380 for (i = 0; i < RATE_BINS; i++) {
1381 hist->bucket[i].low = INT_MAX;
1382 hist->bucket[i].high = 0;
1383 hist->bucket[i].count = 0;
1384 }
John Koleszarc96f8e22011-06-21 16:42:33 -04001385}
1386
1387
John Koleszarc6b90392012-07-13 15:21:29 -07001388static void destroy_rate_histogram(struct rate_hist *hist) {
1389 free(hist->pts);
1390 free(hist->sz);
John Koleszarc96f8e22011-06-21 16:42:33 -04001391}
1392
1393
1394static void update_rate_histogram(struct rate_hist *hist,
1395 const vpx_codec_enc_cfg_t *cfg,
John Koleszarc6b90392012-07-13 15:21:29 -07001396 const vpx_codec_cx_pkt_t *pkt) {
1397 int i, idx;
1398 int64_t now, then, sum_sz = 0, avg_bitrate;
John Koleszarc96f8e22011-06-21 16:42:33 -04001399
John Koleszarc6b90392012-07-13 15:21:29 -07001400 now = pkt->data.frame.pts * 1000
1401 * (uint64_t)cfg->g_timebase.num / (uint64_t)cfg->g_timebase.den;
John Koleszarc96f8e22011-06-21 16:42:33 -04001402
John Koleszarc6b90392012-07-13 15:21:29 -07001403 idx = hist->frames++ % hist->samples;
1404 hist->pts[idx] = now;
John Koleszar6ad3b742012-11-06 12:02:42 -08001405 hist->sz[idx] = (int)pkt->data.frame.sz;
John Koleszarc96f8e22011-06-21 16:42:33 -04001406
John Koleszarc6b90392012-07-13 15:21:29 -07001407 if (now < cfg->rc_buf_initial_sz)
1408 return;
John Koleszarc96f8e22011-06-21 16:42:33 -04001409
John Koleszarc6b90392012-07-13 15:21:29 -07001410 then = now;
Johannbb0ca872011-06-29 14:34:25 -04001411
John Koleszarc6b90392012-07-13 15:21:29 -07001412 /* Sum the size over the past rc_buf_sz ms */
1413 for (i = hist->frames; i > 0 && hist->frames - i < hist->samples; i--) {
1414 int i_idx = (i - 1) % hist->samples;
John Koleszarc96f8e22011-06-21 16:42:33 -04001415
John Koleszarc6b90392012-07-13 15:21:29 -07001416 then = hist->pts[i_idx];
1417 if (now - then > cfg->rc_buf_sz)
1418 break;
1419 sum_sz += hist->sz[i_idx];
1420 }
John Koleszarc96f8e22011-06-21 16:42:33 -04001421
John Koleszarc6b90392012-07-13 15:21:29 -07001422 if (now == then)
1423 return;
Johannbb0ca872011-06-29 14:34:25 -04001424
John Koleszarc6b90392012-07-13 15:21:29 -07001425 avg_bitrate = sum_sz * 8 * 1000 / (now - then);
John Koleszar6ad3b742012-11-06 12:02:42 -08001426 idx = (int)(avg_bitrate * (RATE_BINS / 2) / (cfg->rc_target_bitrate * 1000));
John Koleszarc6b90392012-07-13 15:21:29 -07001427 if (idx < 0)
1428 idx = 0;
1429 if (idx > RATE_BINS - 1)
1430 idx = RATE_BINS - 1;
1431 if (hist->bucket[idx].low > avg_bitrate)
John Koleszar6ad3b742012-11-06 12:02:42 -08001432 hist->bucket[idx].low = (int)avg_bitrate;
John Koleszarc6b90392012-07-13 15:21:29 -07001433 if (hist->bucket[idx].high < avg_bitrate)
John Koleszar6ad3b742012-11-06 12:02:42 -08001434 hist->bucket[idx].high = (int)avg_bitrate;
John Koleszarc6b90392012-07-13 15:21:29 -07001435 hist->bucket[idx].count++;
1436 hist->total++;
John Koleszarc96f8e22011-06-21 16:42:33 -04001437}
1438
1439
1440static void show_rate_histogram(struct rate_hist *hist,
1441 const vpx_codec_enc_cfg_t *cfg,
John Koleszarc6b90392012-07-13 15:21:29 -07001442 int max_buckets) {
1443 int i, scale;
1444 int buckets = 0;
John Koleszarc96f8e22011-06-21 16:42:33 -04001445
John Koleszarc6b90392012-07-13 15:21:29 -07001446 for (i = 0; i < RATE_BINS; i++) {
1447 if (hist->bucket[i].low == INT_MAX)
1448 continue;
1449 hist->bucket[buckets++] = hist->bucket[i];
1450 }
John Koleszarc96f8e22011-06-21 16:42:33 -04001451
John Koleszarc6b90392012-07-13 15:21:29 -07001452 fprintf(stderr, "\nRate (over %dms window):\n", cfg->rc_buf_sz);
1453 scale = merge_hist_buckets(hist->bucket, &buckets, max_buckets);
1454 show_histogram(hist->bucket, buckets, hist->total, scale);
John Koleszarc96f8e22011-06-21 16:42:33 -04001455}
1456
Deb Mukherjee0742b1e2012-11-15 15:14:38 -08001457#define mmin(a, b) ((a) < (b) ? (a) : (b))
1458static void find_mismatch(vpx_image_t *img1, vpx_image_t *img2,
1459 int yloc[2], int uloc[2], int vloc[2]) {
Paul Wilkinsf7597622013-01-24 11:42:38 +00001460 const unsigned int bsize = 64;
1461 const unsigned int bsize2 = bsize >> 1;
1462 unsigned int match = 1;
1463 unsigned int i, j;
Deb Mukherjee0742b1e2012-11-15 15:14:38 -08001464 yloc[0] = yloc[1] = -1;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -08001465 for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
1466 for (j = 0; match && j < img1->d_w; j += bsize) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -08001467 int k, l;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -08001468 int si = mmin(i + bsize, img1->d_h) - i;
1469 int sj = mmin(j + bsize, img1->d_w) - j;
Deb Mukherjee0742b1e2012-11-15 15:14:38 -08001470 for (k = 0; match && k < si; k++)
1471 for (l = 0; match && l < sj; l++) {
1472 if (*(img1->planes[VPX_PLANE_Y] +
1473 (i + k) * img1->stride[VPX_PLANE_Y] + j + l) !=
1474 *(img2->planes[VPX_PLANE_Y] +
1475 (i + k) * img2->stride[VPX_PLANE_Y] + j + l)) {
1476 yloc[0] = i + k;
1477 yloc[1] = j + l;
1478 match = 0;
1479 break;
1480 }
1481 }
1482 }
1483 }
1484 uloc[0] = uloc[1] = -1;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -08001485 for (i = 0, match = 1; match && i < (img1->d_h + 1) / 2; i += bsize2) {
1486 for (j = 0; j < match && (img1->d_w + 1) / 2; j += bsize2) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -08001487 int k, l;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -08001488 int si = mmin(i + bsize2, (img1->d_h + 1) / 2) - i;
1489 int sj = mmin(j + bsize2, (img1->d_w + 1) / 2) - j;
Deb Mukherjee0742b1e2012-11-15 15:14:38 -08001490 for (k = 0; match && k < si; k++)
1491 for (l = 0; match && l < sj; l++) {
1492 if (*(img1->planes[VPX_PLANE_U] +
1493 (i + k) * img1->stride[VPX_PLANE_U] + j + l) !=
1494 *(img2->planes[VPX_PLANE_U] +
1495 (i + k) * img2->stride[VPX_PLANE_U] + j + l)) {
1496 uloc[0] = i + k;
1497 uloc[1] = j + l;
1498 match = 0;
1499 break;
1500 }
1501 }
1502 }
1503 }
1504 vloc[0] = vloc[1] = -1;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -08001505 for (i = 0, match = 1; match && i < (img1->d_h + 1) / 2; i += bsize2) {
1506 for (j = 0; j < match && (img1->d_w + 1) / 2; j += bsize2) {
Deb Mukherjee0742b1e2012-11-15 15:14:38 -08001507 int k, l;
Deb Mukherjee01cafaa2013-01-15 06:43:35 -08001508 int si = mmin(i + bsize2, (img1->d_h + 1) / 2) - i;
1509 int sj = mmin(j + bsize2, (img1->d_w + 1) / 2) - j;
Deb Mukherjee0742b1e2012-11-15 15:14:38 -08001510 for (k = 0; match && k < si; k++)
1511 for (l = 0; match && l < sj; l++) {
1512 if (*(img1->planes[VPX_PLANE_V] +
1513 (i + k) * img1->stride[VPX_PLANE_V] + j + l) !=
1514 *(img2->planes[VPX_PLANE_V] +
1515 (i + k) * img2->stride[VPX_PLANE_V] + j + l)) {
1516 vloc[0] = i + k;
1517 vloc[1] = j + l;
1518 match = 0;
1519 break;
1520 }
1521 }
1522 }
1523 }
1524}
John Koleszar0ea50ce2010-05-18 11:58:33 -04001525
John Koleszar6ad3b742012-11-06 12:02:42 -08001526static int compare_img(vpx_image_t *img1, vpx_image_t *img2)
1527{
John Koleszarc6b90392012-07-13 15:21:29 -07001528 int match = 1;
Paul Wilkinsf7597622013-01-24 11:42:38 +00001529 unsigned int i;
Yaowu Xuf798f9e2012-03-07 12:25:50 -08001530
John Koleszarc6b90392012-07-13 15:21:29 -07001531 match &= (img1->fmt == img2->fmt);
1532 match &= (img1->w == img2->w);
1533 match &= (img1->h == img2->h);
Yaowu Xuf798f9e2012-03-07 12:25:50 -08001534
John Koleszarc6b90392012-07-13 15:21:29 -07001535 for (i = 0; i < img1->d_h; i++)
John Koleszar6ad3b742012-11-06 12:02:42 -08001536 match &= (memcmp(img1->planes[VPX_PLANE_Y]+i*img1->stride[VPX_PLANE_Y],
1537 img2->planes[VPX_PLANE_Y]+i*img2->stride[VPX_PLANE_Y],
John Koleszarc6b90392012-07-13 15:21:29 -07001538 img1->d_w) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -08001539
John Koleszar6ad3b742012-11-06 12:02:42 -08001540 for (i = 0; i < img1->d_h/2; i++)
1541 match &= (memcmp(img1->planes[VPX_PLANE_U]+i*img1->stride[VPX_PLANE_U],
1542 img2->planes[VPX_PLANE_U]+i*img2->stride[VPX_PLANE_U],
1543 (img1->d_w + 1) / 2) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -08001544
John Koleszar6ad3b742012-11-06 12:02:42 -08001545 for (i = 0; i < img1->d_h/2; i++)
1546 match &= (memcmp(img1->planes[VPX_PLANE_V]+i*img1->stride[VPX_PLANE_U],
1547 img2->planes[VPX_PLANE_V]+i*img2->stride[VPX_PLANE_U],
1548 (img1->d_w + 1) / 2) == 0);
Yaowu Xuf798f9e2012-03-07 12:25:50 -08001549
John Koleszarc6b90392012-07-13 15:21:29 -07001550 return match;
Yaowu Xuf798f9e2012-03-07 12:25:50 -08001551}
1552
1553
John Koleszar6ad3b742012-11-06 12:02:42 -08001554#define NELEMENTS(x) (sizeof(x)/sizeof(x[0]))
John Koleszara9c75972012-11-08 17:09:30 -08001555#define MAX(x,y) ((x)>(y)?(x):(y))
1556#if CONFIG_VP8_ENCODER && !CONFIG_VP9_ENCODER
John Koleszar6ad3b742012-11-06 12:02:42 -08001557#define ARG_CTRL_CNT_MAX NELEMENTS(vp8_arg_ctrl_map)
John Koleszara9c75972012-11-08 17:09:30 -08001558#elif !CONFIG_VP8_ENCODER && CONFIG_VP9_ENCODER
1559#define ARG_CTRL_CNT_MAX NELEMENTS(vp9_arg_ctrl_map)
1560#else
1561#define ARG_CTRL_CNT_MAX MAX(NELEMENTS(vp8_arg_ctrl_map), \
1562 NELEMENTS(vp9_arg_ctrl_map))
1563#endif
John Koleszarefd54f82012-02-13 16:52:18 -08001564
1565/* Configuration elements common to all streams */
John Koleszar6ad3b742012-11-06 12:02:42 -08001566struct global_config {
1567 const struct codec_item *codec;
1568 int passes;
1569 int pass;
1570 int usage;
1571 int deadline;
1572 int use_i420;
1573 int quiet;
1574 int verbose;
1575 int limit;
1576 int skip_frames;
1577 int show_psnr;
1578 int test_decode;
1579 int have_framerate;
1580 struct vpx_rational framerate;
1581 int out_part;
1582 int debug;
1583 int show_q_hist_buckets;
1584 int show_rate_hist_buckets;
John Koleszarefd54f82012-02-13 16:52:18 -08001585};
John Koleszar0ea50ce2010-05-18 11:58:33 -04001586
1587
John Koleszar9e50ed72012-02-15 12:39:38 -08001588/* Per-stream configuration */
John Koleszar6ad3b742012-11-06 12:02:42 -08001589struct stream_config {
1590 struct vpx_codec_enc_cfg cfg;
1591 const char *out_fn;
1592 const char *stats_fn;
1593 stereo_format_t stereo_fmt;
1594 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
1595 int arg_ctrl_cnt;
1596 int write_webm;
1597 int have_kf_max_dist;
John Koleszar9e50ed72012-02-15 12:39:38 -08001598};
1599
1600
John Koleszar6ad3b742012-11-06 12:02:42 -08001601struct stream_state {
1602 int index;
1603 struct stream_state *next;
1604 struct stream_config config;
1605 FILE *file;
1606 struct rate_hist rate_hist;
1607 EbmlGlobal ebml;
1608 uint32_t hash;
1609 uint64_t psnr_sse_total;
1610 uint64_t psnr_samples_total;
1611 double psnr_totals[4];
1612 int psnr_count;
1613 int counts[64];
1614 vpx_codec_ctx_t encoder;
1615 unsigned int frames_out;
1616 uint64_t cx_time;
1617 size_t nbytes;
1618 stats_io_t stats;
1619 vpx_codec_ctx_t decoder;
1620 vpx_ref_frame_t ref_enc;
1621 vpx_ref_frame_t ref_dec;
1622 int mismatch_seen;
John Koleszar9e50ed72012-02-15 12:39:38 -08001623};
1624
1625
John Koleszar1b27e932012-04-25 17:08:56 -07001626void validate_positive_rational(const char *msg,
John Koleszar6ad3b742012-11-06 12:02:42 -08001627 struct vpx_rational *rat) {
1628 if (rat->den < 0) {
1629 rat->num *= -1;
1630 rat->den *= -1;
1631 }
John Koleszar1b27e932012-04-25 17:08:56 -07001632
John Koleszar6ad3b742012-11-06 12:02:42 -08001633 if (rat->num < 0)
1634 die("Error: %s must be positive\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -07001635
John Koleszar6ad3b742012-11-06 12:02:42 -08001636 if (!rat->den)
1637 die("Error: %s has zero denominator\n", msg);
John Koleszar1b27e932012-04-25 17:08:56 -07001638}
1639
1640
John Koleszar6ad3b742012-11-06 12:02:42 -08001641static void parse_global_config(struct global_config *global, char **argv) {
1642 char **argi, **argj;
1643 struct arg arg;
John Koleszarefd54f82012-02-13 16:52:18 -08001644
John Koleszar6ad3b742012-11-06 12:02:42 -08001645 /* Initialize default parameters */
1646 memset(global, 0, sizeof(*global));
1647 global->codec = codecs;
1648 global->passes = 1;
1649 global->use_i420 = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001650
John Koleszarc6b90392012-07-13 15:21:29 -07001651 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1652 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001653
John Koleszarc6b90392012-07-13 15:21:29 -07001654 if (arg_match(&arg, &codecarg, argi)) {
1655 int j, k = -1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001656
John Koleszarc6b90392012-07-13 15:21:29 -07001657 for (j = 0; j < sizeof(codecs) / sizeof(codecs[0]); j++)
1658 if (!strcmp(codecs[j].name, arg.val))
1659 k = j;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001660
John Koleszarc6b90392012-07-13 15:21:29 -07001661 if (k >= 0)
John Koleszar6ad3b742012-11-06 12:02:42 -08001662 global->codec = codecs + k;
John Koleszarc6b90392012-07-13 15:21:29 -07001663 else
1664 die("Error: Unrecognized argument (%s) to --codec\n",
1665 arg.val);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001666
John Koleszarc6b90392012-07-13 15:21:29 -07001667 } else if (arg_match(&arg, &passes, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001668 global->passes = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001669
John Koleszar6ad3b742012-11-06 12:02:42 -08001670 if (global->passes < 1 || global->passes > 2)
1671 die("Error: Invalid number of passes (%d)\n", global->passes);
John Koleszarc6b90392012-07-13 15:21:29 -07001672 } else if (arg_match(&arg, &pass_arg, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001673 global->pass = arg_parse_uint(&arg);
John Koleszar0ea50ce2010-05-18 11:58:33 -04001674
John Koleszar6ad3b742012-11-06 12:02:42 -08001675 if (global->pass < 1 || global->pass > 2)
1676 die("Error: Invalid pass selected (%d)\n",
1677 global->pass);
1678 } else if (arg_match(&arg, &usage, argi))
1679 global->usage = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -07001680 else if (arg_match(&arg, &deadline, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001681 global->deadline = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -07001682 else if (arg_match(&arg, &best_dl, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001683 global->deadline = VPX_DL_BEST_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -07001684 else if (arg_match(&arg, &good_dl, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001685 global->deadline = VPX_DL_GOOD_QUALITY;
John Koleszarc6b90392012-07-13 15:21:29 -07001686 else if (arg_match(&arg, &rt_dl, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001687 global->deadline = VPX_DL_REALTIME;
1688 else if (arg_match(&arg, &use_yv12, argi))
1689 global->use_i420 = 0;
1690 else if (arg_match(&arg, &use_i420, argi))
1691 global->use_i420 = 1;
1692 else if (arg_match(&arg, &quietarg, argi))
1693 global->quiet = 1;
1694 else if (arg_match(&arg, &verbosearg, argi))
1695 global->verbose = 1;
John Koleszarc6b90392012-07-13 15:21:29 -07001696 else if (arg_match(&arg, &limit, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001697 global->limit = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -07001698 else if (arg_match(&arg, &skip, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001699 global->skip_frames = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -07001700 else if (arg_match(&arg, &psnrarg, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001701 global->show_psnr = 1;
John Koleszarc6b90392012-07-13 15:21:29 -07001702 else if (arg_match(&arg, &recontest, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001703 global->test_decode = 1;
John Koleszarc6b90392012-07-13 15:21:29 -07001704 else if (arg_match(&arg, &framerate, argi)) {
John Koleszar6ad3b742012-11-06 12:02:42 -08001705 global->framerate = arg_parse_rational(&arg);
1706 validate_positive_rational(arg.name, &global->framerate);
1707 global->have_framerate = 1;
1708 } else if (arg_match(&arg, &out_part, argi))
1709 global->out_part = 1;
John Koleszarc6b90392012-07-13 15:21:29 -07001710 else if (arg_match(&arg, &debugmode, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001711 global->debug = 1;
John Koleszarc6b90392012-07-13 15:21:29 -07001712 else if (arg_match(&arg, &q_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001713 global->show_q_hist_buckets = arg_parse_uint(&arg);
John Koleszarc6b90392012-07-13 15:21:29 -07001714 else if (arg_match(&arg, &rate_hist_n, argi))
John Koleszar6ad3b742012-11-06 12:02:42 -08001715 global->show_rate_hist_buckets = arg_parse_uint(&arg);
John Koleszar732cb9a2012-02-14 12:30:17 -08001716 else
John Koleszarc6b90392012-07-13 15:21:29 -07001717 argj++;
1718 }
1719
John Koleszar6ad3b742012-11-06 12:02:42 -08001720 /* Validate global config */
1721
1722 if (global->pass) {
John Koleszarc6b90392012-07-13 15:21:29 -07001723 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
John Koleszar6ad3b742012-11-06 12:02:42 -08001724 if (global->pass > global->passes) {
1725 warn("Assuming --pass=%d implies --passes=%d\n",
1726 global->pass, global->pass);
1727 global->passes = global->pass;
John Koleszar732cb9a2012-02-14 12:30:17 -08001728 }
John Koleszarc6b90392012-07-13 15:21:29 -07001729 }
John Koleszar732cb9a2012-02-14 12:30:17 -08001730}
1731
1732
John Koleszar6ad3b742012-11-06 12:02:42 -08001733void open_input_file(struct input_state *input) {
1734 unsigned int fourcc;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001735
John Koleszar6ad3b742012-11-06 12:02:42 -08001736 /* Parse certain options from the input file, if possible */
1737 input->file = strcmp(input->fn, "-") ? fopen(input->fn, "rb")
1738 : set_binary_mode(stdin);
1739
1740 if (!input->file)
1741 fatal("Failed to open input file");
1742
1743 /* For RAW input sources, these bytes will applied on the first frame
1744 * in read_frame().
John Koleszarc6b90392012-07-13 15:21:29 -07001745 */
John Koleszar6ad3b742012-11-06 12:02:42 -08001746 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
1747 input->detect.position = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001748
John Koleszar6ad3b742012-11-06 12:02:42 -08001749 if (input->detect.buf_read == 4
1750 && file_is_y4m(input->file, &input->y4m, input->detect.buf)) {
1751 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4) >= 0) {
1752 input->file_type = FILE_TYPE_Y4M;
1753 input->w = input->y4m.pic_w;
1754 input->h = input->y4m.pic_h;
1755 input->framerate.num = input->y4m.fps_n;
1756 input->framerate.den = input->y4m.fps_d;
1757 input->use_i420 = 0;
1758 } else
1759 fatal("Unsupported Y4M stream.");
1760 } else if (input->detect.buf_read == 4 && file_is_ivf(input, &fourcc)) {
1761 input->file_type = FILE_TYPE_IVF;
1762 switch (fourcc) {
1763 case 0x32315659:
1764 input->use_i420 = 0;
1765 break;
1766 case 0x30323449:
1767 input->use_i420 = 1;
1768 break;
1769 default:
1770 fatal("Unsupported fourcc (%08x) in IVF", fourcc);
1771 }
1772 } else {
1773 input->file_type = FILE_TYPE_RAW;
John Koleszarc6b90392012-07-13 15:21:29 -07001774 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001775}
1776
1777
1778static void close_input_file(struct input_state *input) {
1779 fclose(input->file);
1780 if (input->file_type == FILE_TYPE_Y4M)
1781 y4m_input_close(&input->y4m);
John Koleszar732cb9a2012-02-14 12:30:17 -08001782}
1783
John Koleszar9e50ed72012-02-15 12:39:38 -08001784static struct stream_state *new_stream(struct global_config *global,
John Koleszar6ad3b742012-11-06 12:02:42 -08001785 struct stream_state *prev) {
1786 struct stream_state *stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001787
John Koleszar6ad3b742012-11-06 12:02:42 -08001788 stream = calloc(1, sizeof(*stream));
1789 if (!stream)
1790 fatal("Failed to allocate new stream.");
1791 if (prev) {
1792 memcpy(stream, prev, sizeof(*stream));
1793 stream->index++;
1794 prev->next = stream;
1795 } else {
1796 vpx_codec_err_t res;
John Koleszar9e50ed72012-02-15 12:39:38 -08001797
John Koleszar6ad3b742012-11-06 12:02:42 -08001798 /* Populate encoder configuration */
1799 res = vpx_codec_enc_config_default(global->codec->iface(),
1800 &stream->config.cfg,
1801 global->usage);
1802 if (res)
1803 fatal("Failed to get config: %s\n", vpx_codec_err_to_string(res));
John Koleszar9e50ed72012-02-15 12:39:38 -08001804
John Koleszar6ad3b742012-11-06 12:02:42 -08001805 /* Change the default timebase to a high enough value so that the
1806 * encoder will always create strictly increasing timestamps.
1807 */
1808 stream->config.cfg.g_timebase.den = 1000;
John Koleszar9e50ed72012-02-15 12:39:38 -08001809
John Koleszar6ad3b742012-11-06 12:02:42 -08001810 /* Never use the library's default resolution, require it be parsed
1811 * from the file or set on the command line.
1812 */
1813 stream->config.cfg.g_w = 0;
1814 stream->config.cfg.g_h = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001815
John Koleszar6ad3b742012-11-06 12:02:42 -08001816 /* Initialize remaining stream parameters */
1817 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
1818 stream->config.write_webm = 1;
1819 stream->ebml.last_pts_ms = -1;
Johann87c40b32012-03-01 16:12:53 -08001820
John Koleszar6ad3b742012-11-06 12:02:42 -08001821 /* Allows removal of the application version from the EBML tags */
1822 stream->ebml.debug = global->debug;
1823 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001824
John Koleszar6ad3b742012-11-06 12:02:42 -08001825 /* Output files must be specified for each stream */
1826 stream->config.out_fn = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08001827
John Koleszar6ad3b742012-11-06 12:02:42 -08001828 stream->next = NULL;
1829 return stream;
John Koleszar9e50ed72012-02-15 12:39:38 -08001830}
1831
1832
1833static int parse_stream_params(struct global_config *global,
1834 struct stream_state *stream,
John Koleszar6ad3b742012-11-06 12:02:42 -08001835 char **argv) {
1836 char **argi, **argj;
1837 struct arg arg;
1838 static const arg_def_t **ctrl_args = no_args;
1839 static const int *ctrl_args_map = NULL;
1840 struct stream_config *config = &stream->config;
1841 int eos_mark_found = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08001842
John Koleszarc6b90392012-07-13 15:21:29 -07001843 /* Handle codec specific options */
John Koleszara9c75972012-11-08 17:09:30 -08001844 if (0) {
1845#if CONFIG_VP8_ENCODER
1846 } else if (global->codec->iface == vpx_codec_vp8_cx) {
John Koleszarc6b90392012-07-13 15:21:29 -07001847 ctrl_args = vp8_args;
1848 ctrl_args_map = vp8_arg_ctrl_map;
John Koleszara9c75972012-11-08 17:09:30 -08001849#endif
1850#if CONFIG_VP9_ENCODER
1851 } else if (global->codec->iface == vpx_codec_vp9_cx) {
1852 ctrl_args = vp9_args;
1853 ctrl_args_map = vp9_arg_ctrl_map;
1854#endif
John Koleszarc6b90392012-07-13 15:21:29 -07001855 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04001856
John Koleszarc6b90392012-07-13 15:21:29 -07001857 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
John Koleszarc6b90392012-07-13 15:21:29 -07001858 arg.argv_step = 1;
John Koleszar0ea50ce2010-05-18 11:58:33 -04001859
John Koleszar6ad3b742012-11-06 12:02:42 -08001860 /* Once we've found an end-of-stream marker (--) we want to continue
1861 * shifting arguments but not consuming them.
1862 */
1863 if (eos_mark_found) {
1864 argj++;
1865 continue;
1866 } else if (!strcmp(*argj, "--")) {
1867 eos_mark_found = 1;
1868 continue;
John Koleszar9e50ed72012-02-15 12:39:38 -08001869 }
1870
John Koleszar6ad3b742012-11-06 12:02:42 -08001871 if (0);
1872 else if (arg_match(&arg, &outputfile, argi))
1873 config->out_fn = arg.val;
1874 else if (arg_match(&arg, &fpf_name, argi))
1875 config->stats_fn = arg.val;
1876 else if (arg_match(&arg, &use_ivf, argi))
1877 config->write_webm = 0;
1878 else if (arg_match(&arg, &threads, argi))
1879 config->cfg.g_threads = arg_parse_uint(&arg);
1880 else if (arg_match(&arg, &profile, argi))
1881 config->cfg.g_profile = arg_parse_uint(&arg);
1882 else if (arg_match(&arg, &width, argi))
1883 config->cfg.g_w = arg_parse_uint(&arg);
1884 else if (arg_match(&arg, &height, argi))
1885 config->cfg.g_h = arg_parse_uint(&arg);
1886 else if (arg_match(&arg, &stereo_mode, argi))
1887 config->stereo_fmt = arg_parse_enum_or_int(&arg);
1888 else if (arg_match(&arg, &timebase, argi)) {
1889 config->cfg.g_timebase = arg_parse_rational(&arg);
1890 validate_positive_rational(arg.name, &config->cfg.g_timebase);
1891 } else if (arg_match(&arg, &error_resilient, argi))
1892 config->cfg.g_error_resilient = arg_parse_uint(&arg);
Deb Mukherjeedfd89f22013-01-25 11:30:28 -08001893#if CONFIG_VP9_ENCODER
1894 else if (arg_match(&arg, &frame_parallel_decoding, argi))
1895 config->cfg.g_frame_parallel_decoding = arg_parse_uint(&arg);
1896#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08001897 else if (arg_match(&arg, &lag_in_frames, argi))
1898 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
1899 else if (arg_match(&arg, &dropframe_thresh, argi))
1900 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
1901 else if (arg_match(&arg, &resize_allowed, argi))
1902 config->cfg.rc_resize_allowed = arg_parse_uint(&arg);
1903 else if (arg_match(&arg, &resize_up_thresh, argi))
1904 config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg);
1905 else if (arg_match(&arg, &resize_down_thresh, argi))
1906 config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg);
1907 else if (arg_match(&arg, &end_usage, argi))
1908 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1909 else if (arg_match(&arg, &target_bitrate, argi))
1910 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
1911 else if (arg_match(&arg, &min_quantizer, argi))
1912 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
1913 else if (arg_match(&arg, &max_quantizer, argi))
1914 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
1915 else if (arg_match(&arg, &undershoot_pct, argi))
1916 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1917 else if (arg_match(&arg, &overshoot_pct, argi))
1918 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1919 else if (arg_match(&arg, &buf_sz, argi))
1920 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1921 else if (arg_match(&arg, &buf_initial_sz, argi))
1922 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1923 else if (arg_match(&arg, &buf_optimal_sz, argi))
1924 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1925 else if (arg_match(&arg, &bias_pct, argi)) {
1926 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
John Koleszar9e50ed72012-02-15 12:39:38 -08001927
John Koleszar6ad3b742012-11-06 12:02:42 -08001928 if (global->passes < 2)
1929 warn("option %s ignored in one-pass mode.\n", arg.name);
1930 } else if (arg_match(&arg, &minsection_pct, argi)) {
1931 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1932
1933 if (global->passes < 2)
1934 warn("option %s ignored in one-pass mode.\n", arg.name);
1935 } else if (arg_match(&arg, &maxsection_pct, argi)) {
1936 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1937
1938 if (global->passes < 2)
1939 warn("option %s ignored in one-pass mode.\n", arg.name);
1940 } else if (arg_match(&arg, &kf_min_dist, argi))
1941 config->cfg.kf_min_dist = arg_parse_uint(&arg);
1942 else if (arg_match(&arg, &kf_max_dist, argi)) {
1943 config->cfg.kf_max_dist = arg_parse_uint(&arg);
1944 config->have_kf_max_dist = 1;
1945 } else if (arg_match(&arg, &kf_disabled, argi))
1946 config->cfg.kf_mode = VPX_KF_DISABLED;
1947 else {
1948 int i, match = 0;
1949
1950 for (i = 0; ctrl_args[i]; i++) {
1951 if (arg_match(&arg, ctrl_args[i], argi)) {
1952 int j;
1953 match = 1;
1954
1955 /* Point either to the next free element or the first
1956 * instance of this control.
1957 */
1958 for (j = 0; j < config->arg_ctrl_cnt; j++)
1959 if (config->arg_ctrls[j][0] == ctrl_args_map[i])
1960 break;
1961
1962 /* Update/insert */
1963 assert(j < ARG_CTRL_CNT_MAX);
1964 if (j < ARG_CTRL_CNT_MAX) {
1965 config->arg_ctrls[j][0] = ctrl_args_map[i];
1966 config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg);
1967 if (j == config->arg_ctrl_cnt)
1968 config->arg_ctrl_cnt++;
1969 }
1970
John Koleszar9e50ed72012-02-15 12:39:38 -08001971 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001972 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001973
John Koleszar6ad3b742012-11-06 12:02:42 -08001974 if (!match)
1975 argj++;
John Koleszar9e50ed72012-02-15 12:39:38 -08001976 }
John Koleszar6ad3b742012-11-06 12:02:42 -08001977 }
John Koleszar9e50ed72012-02-15 12:39:38 -08001978
John Koleszar6ad3b742012-11-06 12:02:42 -08001979 return eos_mark_found;
John Koleszar9e50ed72012-02-15 12:39:38 -08001980}
1981
1982
1983#define FOREACH_STREAM(func)\
John Koleszar6ad3b742012-11-06 12:02:42 -08001984 do\
1985 {\
John Koleszar9e50ed72012-02-15 12:39:38 -08001986 struct stream_state *stream;\
John Koleszar6ad3b742012-11-06 12:02:42 -08001987 \
John Koleszar9e50ed72012-02-15 12:39:38 -08001988 for(stream = streams; stream; stream = stream->next)\
John Koleszar6ad3b742012-11-06 12:02:42 -08001989 func;\
1990 }while(0)
John Koleszar9e50ed72012-02-15 12:39:38 -08001991
1992
John Koleszar6ad3b742012-11-06 12:02:42 -08001993static void validate_stream_config(struct stream_state *stream) {
1994 struct stream_state *streami;
John Koleszar9e50ed72012-02-15 12:39:38 -08001995
John Koleszar6ad3b742012-11-06 12:02:42 -08001996 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1997 fatal("Stream %d: Specify stream dimensions with --width (-w) "
1998 " and --height (-h)", stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08001999
John Koleszar6ad3b742012-11-06 12:02:42 -08002000 for (streami = stream; streami; streami = streami->next) {
2001 /* All streams require output files */
2002 if (!streami->config.out_fn)
2003 fatal("Stream %d: Output file is required (specify with -o)",
2004 streami->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08002005
John Koleszar6ad3b742012-11-06 12:02:42 -08002006 /* Check for two streams outputting to the same file */
2007 if (streami != stream) {
2008 const char *a = stream->config.out_fn;
2009 const char *b = streami->config.out_fn;
2010 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
2011 fatal("Stream %d: duplicate output file (from stream %d)",
2012 streami->index, stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08002013 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002014
2015 /* Check for two streams sharing a stats file. */
2016 if (streami != stream) {
2017 const char *a = stream->config.stats_fn;
2018 const char *b = streami->config.stats_fn;
2019 if (a && b && !strcmp(a, b))
2020 fatal("Stream %d: duplicate stats file (from stream %d)",
2021 streami->index, stream->index);
2022 }
2023 }
John Koleszar9e50ed72012-02-15 12:39:38 -08002024}
2025
2026
2027static void set_stream_dimensions(struct stream_state *stream,
2028 unsigned int w,
John Koleszar6ad3b742012-11-06 12:02:42 -08002029 unsigned int h) {
2030 if ((stream->config.cfg.g_w && stream->config.cfg.g_w != w)
2031 || (stream->config.cfg.g_h && stream->config.cfg.g_h != h))
2032 fatal("Stream %d: Resizing not yet supported", stream->index);
2033 stream->config.cfg.g_w = w;
2034 stream->config.cfg.g_h = h;
John Koleszar9e50ed72012-02-15 12:39:38 -08002035}
2036
2037
Ralph Giles061a16d2012-01-05 15:05:05 -06002038static void set_default_kf_interval(struct stream_state *stream,
John Koleszar6ad3b742012-11-06 12:02:42 -08002039 struct global_config *global) {
2040 /* Use a max keyframe interval of 5 seconds, if none was
2041 * specified on the command line.
2042 */
2043 if (!stream->config.have_kf_max_dist) {
2044 double framerate = (double)global->framerate.num / global->framerate.den;
2045 if (framerate > 0.0)
2046 stream->config.cfg.kf_max_dist = (unsigned int)(5.0 * framerate);
2047 }
Ralph Giles061a16d2012-01-05 15:05:05 -06002048}
2049
2050
John Koleszar9e50ed72012-02-15 12:39:38 -08002051static void show_stream_config(struct stream_state *stream,
2052 struct global_config *global,
John Koleszar6ad3b742012-11-06 12:02:42 -08002053 struct input_state *input) {
John Koleszar9e50ed72012-02-15 12:39:38 -08002054
2055#define SHOW(field) \
John Koleszar6ad3b742012-11-06 12:02:42 -08002056 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
John Koleszar9e50ed72012-02-15 12:39:38 -08002057
John Koleszar6ad3b742012-11-06 12:02:42 -08002058 if (stream->index == 0) {
2059 fprintf(stderr, "Codec: %s\n",
2060 vpx_codec_iface_name(global->codec->iface()));
2061 fprintf(stderr, "Source file: %s Format: %s\n", input->fn,
2062 input->use_i420 ? "I420" : "YV12");
2063 }
2064 if (stream->next || stream->index)
2065 fprintf(stderr, "\nStream Index: %d\n", stream->index);
2066 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
2067 fprintf(stderr, "Encoder parameters:\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08002068
John Koleszar6ad3b742012-11-06 12:02:42 -08002069 SHOW(g_usage);
2070 SHOW(g_threads);
2071 SHOW(g_profile);
2072 SHOW(g_w);
2073 SHOW(g_h);
2074 SHOW(g_timebase.num);
2075 SHOW(g_timebase.den);
2076 SHOW(g_error_resilient);
Deb Mukherjeedfd89f22013-01-25 11:30:28 -08002077#if CONFIG_VP9_ENCODER
2078 SHOW(g_frame_parallel_decoding);
2079#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08002080 SHOW(g_pass);
2081 SHOW(g_lag_in_frames);
2082 SHOW(rc_dropframe_thresh);
2083 SHOW(rc_resize_allowed);
2084 SHOW(rc_resize_up_thresh);
2085 SHOW(rc_resize_down_thresh);
2086 SHOW(rc_end_usage);
2087 SHOW(rc_target_bitrate);
2088 SHOW(rc_min_quantizer);
2089 SHOW(rc_max_quantizer);
2090 SHOW(rc_undershoot_pct);
2091 SHOW(rc_overshoot_pct);
2092 SHOW(rc_buf_sz);
2093 SHOW(rc_buf_initial_sz);
2094 SHOW(rc_buf_optimal_sz);
2095 SHOW(rc_2pass_vbr_bias_pct);
2096 SHOW(rc_2pass_vbr_minsection_pct);
2097 SHOW(rc_2pass_vbr_maxsection_pct);
2098 SHOW(kf_mode);
2099 SHOW(kf_min_dist);
2100 SHOW(kf_max_dist);
John Koleszar9e50ed72012-02-15 12:39:38 -08002101}
2102
2103
2104static void open_output_file(struct stream_state *stream,
John Koleszar6ad3b742012-11-06 12:02:42 -08002105 struct global_config *global) {
2106 const char *fn = stream->config.out_fn;
John Koleszar9e50ed72012-02-15 12:39:38 -08002107
John Koleszar6ad3b742012-11-06 12:02:42 -08002108 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
John Koleszar9e50ed72012-02-15 12:39:38 -08002109
John Koleszar6ad3b742012-11-06 12:02:42 -08002110 if (!stream->file)
2111 fatal("Failed to open output file");
John Koleszar9e50ed72012-02-15 12:39:38 -08002112
John Koleszar6ad3b742012-11-06 12:02:42 -08002113 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
2114 fatal("WebM output to pipes not supported.");
John Koleszar9e50ed72012-02-15 12:39:38 -08002115
John Koleszar6ad3b742012-11-06 12:02:42 -08002116 if (stream->config.write_webm) {
2117 stream->ebml.stream = stream->file;
2118 write_webm_file_header(&stream->ebml, &stream->config.cfg,
2119 &global->framerate,
John Koleszar08b43fe2012-11-13 12:14:28 -08002120 stream->config.stereo_fmt,
2121 global->codec->fourcc);
John Koleszar6ad3b742012-11-06 12:02:42 -08002122 } else
2123 write_ivf_file_header(stream->file, &stream->config.cfg,
2124 global->codec->fourcc, 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08002125}
2126
2127
2128static void close_output_file(struct stream_state *stream,
John Koleszar6ad3b742012-11-06 12:02:42 -08002129 unsigned int fourcc) {
2130 if (stream->config.write_webm) {
2131 write_webm_file_footer(&stream->ebml, stream->hash);
2132 free(stream->ebml.cue_list);
2133 stream->ebml.cue_list = NULL;
2134 } else {
2135 if (!fseek(stream->file, 0, SEEK_SET))
2136 write_ivf_file_header(stream->file, &stream->config.cfg,
2137 fourcc,
2138 stream->frames_out);
2139 }
John Koleszar9e50ed72012-02-15 12:39:38 -08002140
John Koleszar6ad3b742012-11-06 12:02:42 -08002141 fclose(stream->file);
John Koleszar9e50ed72012-02-15 12:39:38 -08002142}
2143
2144
2145static void setup_pass(struct stream_state *stream,
2146 struct global_config *global,
John Koleszar6ad3b742012-11-06 12:02:42 -08002147 int pass) {
2148 if (stream->config.stats_fn) {
2149 if (!stats_open_file(&stream->stats, stream->config.stats_fn,
2150 pass))
2151 fatal("Failed to open statistics store");
2152 } else {
2153 if (!stats_open_mem(&stream->stats, pass))
2154 fatal("Failed to open statistics store");
2155 }
John Koleszar9e50ed72012-02-15 12:39:38 -08002156
John Koleszar6ad3b742012-11-06 12:02:42 -08002157 stream->config.cfg.g_pass = global->passes == 2
2158 ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS
2159 : VPX_RC_ONE_PASS;
2160 if (pass)
2161 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
Yunqing Wangaabae972012-02-29 08:24:53 -05002162
John Koleszar6ad3b742012-11-06 12:02:42 -08002163 stream->cx_time = 0;
2164 stream->nbytes = 0;
2165 stream->frames_out = 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08002166}
2167
2168
2169static void initialize_encoder(struct stream_state *stream,
John Koleszar6ad3b742012-11-06 12:02:42 -08002170 struct global_config *global) {
2171 int i;
2172 int flags = 0;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02002173
John Koleszar6ad3b742012-11-06 12:02:42 -08002174 flags |= global->show_psnr ? VPX_CODEC_USE_PSNR : 0;
2175 flags |= global->out_part ? VPX_CODEC_USE_OUTPUT_PARTITION : 0;
John Koleszar9e50ed72012-02-15 12:39:38 -08002176
John Koleszar6ad3b742012-11-06 12:02:42 -08002177 /* Construct Encoder Context */
2178 vpx_codec_enc_init(&stream->encoder, global->codec->iface(),
2179 &stream->config.cfg, flags);
2180 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
John Koleszar9e50ed72012-02-15 12:39:38 -08002181
John Koleszar6ad3b742012-11-06 12:02:42 -08002182 /* Note that we bypass the vpx_codec_control wrapper macro because
2183 * we're being clever to store the control IDs in an array. Real
2184 * applications will want to make use of the enumerations directly
2185 */
2186 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
2187 int ctrl = stream->config.arg_ctrls[i][0];
2188 int value = stream->config.arg_ctrls[i][1];
2189 if (vpx_codec_control_(&stream->encoder, ctrl, value))
2190 fprintf(stderr, "Error: Tried to set control %d = %d\n",
2191 ctrl, value);
John Koleszar9e50ed72012-02-15 12:39:38 -08002192
John Koleszar6ad3b742012-11-06 12:02:42 -08002193 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
2194 }
2195
John Koleszar5ebe94f2012-12-23 07:20:10 -08002196#if CONFIG_DECODERS
John Koleszar6ad3b742012-11-06 12:02:42 -08002197 if (global->test_decode) {
2198 int width, height;
2199
2200 vpx_codec_dec_init(&stream->decoder, global->codec->dx_iface(), NULL, 0);
2201
2202 width = (stream->config.cfg.g_w + 15) & ~15;
2203 height = (stream->config.cfg.g_h + 15) & ~15;
2204 vpx_img_alloc(&stream->ref_enc.img, VPX_IMG_FMT_I420, width, height, 1);
2205 vpx_img_alloc(&stream->ref_dec.img, VPX_IMG_FMT_I420, width, height, 1);
2206 stream->ref_enc.frame_type = VP8_LAST_FRAME;
2207 stream->ref_dec.frame_type = VP8_LAST_FRAME;
2208 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08002209#endif
John Koleszar9e50ed72012-02-15 12:39:38 -08002210}
2211
2212
2213static void encode_frame(struct stream_state *stream,
2214 struct global_config *global,
2215 struct vpx_image *img,
John Koleszar6ad3b742012-11-06 12:02:42 -08002216 unsigned int frames_in) {
2217 vpx_codec_pts_t frame_start, next_frame_start;
2218 struct vpx_codec_enc_cfg *cfg = &stream->config.cfg;
2219 struct vpx_usec_timer timer;
John Koleszar9e50ed72012-02-15 12:39:38 -08002220
John Koleszar6ad3b742012-11-06 12:02:42 -08002221 frame_start = (cfg->g_timebase.den * (int64_t)(frames_in - 1)
2222 * global->framerate.den)
2223 / cfg->g_timebase.num / global->framerate.num;
2224 next_frame_start = (cfg->g_timebase.den * (int64_t)(frames_in)
2225 * global->framerate.den)
2226 / cfg->g_timebase.num / global->framerate.num;
2227 vpx_usec_timer_start(&timer);
2228 vpx_codec_encode(&stream->encoder, img, frame_start,
2229 (unsigned long)(next_frame_start - frame_start),
2230 0, global->deadline);
2231 vpx_usec_timer_mark(&timer);
2232 stream->cx_time += vpx_usec_timer_elapsed(&timer);
2233 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
2234 stream->index);
John Koleszar9e50ed72012-02-15 12:39:38 -08002235}
2236
2237
John Koleszar6ad3b742012-11-06 12:02:42 -08002238static void update_quantizer_histogram(struct stream_state *stream) {
2239 if (stream->config.cfg.g_pass != VPX_RC_FIRST_PASS) {
2240 int q;
John Koleszar9e50ed72012-02-15 12:39:38 -08002241
John Koleszar6ad3b742012-11-06 12:02:42 -08002242 vpx_codec_control(&stream->encoder, VP8E_GET_LAST_QUANTIZER_64, &q);
2243 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
2244 stream->counts[q]++;
2245 }
John Koleszar9e50ed72012-02-15 12:39:38 -08002246}
2247
2248
2249static void get_cx_data(struct stream_state *stream,
2250 struct global_config *global,
John Koleszar6ad3b742012-11-06 12:02:42 -08002251 int *got_data) {
2252 const vpx_codec_cx_pkt_t *pkt;
2253 const struct vpx_codec_enc_cfg *cfg = &stream->config.cfg;
2254 vpx_codec_iter_t iter = NULL;
John Koleszar9e50ed72012-02-15 12:39:38 -08002255
Ronald S. Bultje88d703c2012-11-09 09:07:50 -08002256 *got_data = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002257 while ((pkt = vpx_codec_get_cx_data(&stream->encoder, &iter))) {
2258 static size_t fsize = 0;
2259 static off_t ivf_header_pos = 0;
2260
John Koleszar6ad3b742012-11-06 12:02:42 -08002261 switch (pkt->kind) {
2262 case VPX_CODEC_CX_FRAME_PKT:
2263 if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
2264 stream->frames_out++;
2265 }
2266 if (!global->quiet)
2267 fprintf(stderr, " %6luF",
2268 (unsigned long)pkt->data.frame.sz);
2269
2270 update_rate_histogram(&stream->rate_hist, cfg, pkt);
2271 if (stream->config.write_webm) {
2272 /* Update the hash */
2273 if (!stream->ebml.debug)
2274 stream->hash = murmur(pkt->data.frame.buf,
2275 (int)pkt->data.frame.sz,
2276 stream->hash);
2277
2278 write_webm_block(&stream->ebml, cfg, pkt);
2279 } else {
2280 if (pkt->data.frame.partition_id <= 0) {
2281 ivf_header_pos = ftello(stream->file);
2282 fsize = pkt->data.frame.sz;
2283
2284 write_ivf_frame_header(stream->file, pkt);
2285 } else {
2286 fsize += pkt->data.frame.sz;
2287
2288 if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) {
2289 off_t currpos = ftello(stream->file);
2290 fseeko(stream->file, ivf_header_pos, SEEK_SET);
2291 write_ivf_frame_size(stream->file, fsize);
2292 fseeko(stream->file, currpos, SEEK_SET);
2293 }
2294 }
2295
2296 (void) fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
2297 stream->file);
2298 }
2299 stream->nbytes += pkt->data.raw.sz;
Attila Nagy52cf4dc2012-02-09 12:37:03 +02002300
John Koleszar9e50ed72012-02-15 12:39:38 -08002301 *got_data = 1;
John Koleszar5ebe94f2012-12-23 07:20:10 -08002302#if CONFIG_DECODERS
John Koleszar6ad3b742012-11-06 12:02:42 -08002303 if (global->test_decode) {
2304 vpx_codec_decode(&stream->decoder, pkt->data.frame.buf,
2305 pkt->data.frame.sz, NULL, 0);
2306 ctx_exit_on_error(&stream->decoder, "Failed to decode frame");
John Koleszar9e50ed72012-02-15 12:39:38 -08002307 }
John Koleszar5ebe94f2012-12-23 07:20:10 -08002308#endif
John Koleszar6ad3b742012-11-06 12:02:42 -08002309 break;
2310 case VPX_CODEC_STATS_PKT:
2311 stream->frames_out++;
John Koleszar16810c12012-12-21 15:20:41 -08002312 if (!global->quiet)
2313 fprintf(stderr, " %6luS",
2314 (unsigned long)pkt->data.twopass_stats.sz);
John Koleszar6ad3b742012-11-06 12:02:42 -08002315 stats_write(&stream->stats,
2316 pkt->data.twopass_stats.buf,
2317 pkt->data.twopass_stats.sz);
2318 stream->nbytes += pkt->data.raw.sz;
2319 break;
2320 case VPX_CODEC_PSNR_PKT:
2321
2322 if (global->show_psnr) {
2323 int i;
2324
2325 stream->psnr_sse_total += pkt->data.psnr.sse[0];
2326 stream->psnr_samples_total += pkt->data.psnr.samples[0];
2327 for (i = 0; i < 4; i++) {
2328 if (!global->quiet)
2329 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
2330 stream->psnr_totals[i] += pkt->data.psnr.psnr[i];
2331 }
2332 stream->psnr_count++;
2333 }
2334
2335 break;
2336 default:
2337 break;
John Koleszar9e50ed72012-02-15 12:39:38 -08002338 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002339 }
John Koleszar9e50ed72012-02-15 12:39:38 -08002340}
2341
2342
John Koleszar6ad3b742012-11-06 12:02:42 -08002343static void show_psnr(struct stream_state *stream) {
2344 int i;
2345 double ovpsnr;
John Koleszar9e50ed72012-02-15 12:39:38 -08002346
John Koleszar6ad3b742012-11-06 12:02:42 -08002347 if (!stream->psnr_count)
2348 return;
John Koleszar9e50ed72012-02-15 12:39:38 -08002349
John Koleszar6ad3b742012-11-06 12:02:42 -08002350 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
2351 ovpsnr = vp8_mse2psnr((double)stream->psnr_samples_total, 255.0,
2352 (double)stream->psnr_sse_total);
2353 fprintf(stderr, " %.3f", ovpsnr);
John Koleszar9e50ed72012-02-15 12:39:38 -08002354
John Koleszar6ad3b742012-11-06 12:02:42 -08002355 for (i = 0; i < 4; i++) {
2356 fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count);
2357 }
2358 fprintf(stderr, "\n");
John Koleszar9e50ed72012-02-15 12:39:38 -08002359}
2360
2361
John Koleszar6ad3b742012-11-06 12:02:42 -08002362float usec_to_fps(uint64_t usec, unsigned int frames) {
2363 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
John Koleszar9e50ed72012-02-15 12:39:38 -08002364}
2365
John Koleszar732cb9a2012-02-14 12:30:17 -08002366
Ronald S. Bultje195172c2012-11-10 16:22:49 -08002367static void test_decode(struct stream_state *stream) {
John Koleszar6ad3b742012-11-06 12:02:42 -08002368 vpx_codec_control(&stream->encoder, VP8_COPY_REFERENCE, &stream->ref_enc);
2369 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
2370 vpx_codec_control(&stream->decoder, VP8_COPY_REFERENCE, &stream->ref_dec);
2371 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
John Koleszarefd54f82012-02-13 16:52:18 -08002372
John Koleszar6ad3b742012-11-06 12:02:42 -08002373 if (!stream->mismatch_seen
2374 && !compare_img(&stream->ref_enc.img, &stream->ref_dec.img)) {
2375 /* TODO(jkoleszar): make fatal. */
Deb Mukherjee0742b1e2012-11-15 15:14:38 -08002376 int y[2], u[2], v[2];
2377 find_mismatch(&stream->ref_enc.img, &stream->ref_dec.img,
2378 y, u, v);
2379 warn("Stream %d: Encode/decode mismatch on frame %d"
2380 " at Y[%d, %d], U[%d, %d], V[%d, %d]",
2381 stream->index, stream->frames_out,
2382 y[0], y[1], u[0], u[1], v[0], v[1]);
Ronald S. Bultje195172c2012-11-10 16:22:49 -08002383 stream->mismatch_seen = stream->frames_out;
John Koleszar6ad3b742012-11-06 12:02:42 -08002384 }
2385}
John Koleszarefd54f82012-02-13 16:52:18 -08002386
John Koleszar6ad3b742012-11-06 12:02:42 -08002387int main(int argc, const char **argv_) {
2388 int pass;
2389 vpx_image_t raw;
2390 int frame_avail, got_data;
John Koleszarefd54f82012-02-13 16:52:18 -08002391
John Koleszar6ad3b742012-11-06 12:02:42 -08002392 struct input_state input = {0};
2393 struct global_config global;
2394 struct stream_state *streams = NULL;
2395 char **argv, **argi;
2396 unsigned long cx_time = 0;
2397 int stream_cnt = 0;
John Koleszarefd54f82012-02-13 16:52:18 -08002398
John Koleszar6ad3b742012-11-06 12:02:42 -08002399 exec_name = argv_[0];
John Koleszar9e50ed72012-02-15 12:39:38 -08002400
John Koleszar6ad3b742012-11-06 12:02:42 -08002401 if (argc < 3)
2402 usage_exit();
2403
2404 /* Setup default input stream settings */
2405 input.framerate.num = 30;
2406 input.framerate.den = 1;
2407 input.use_i420 = 1;
2408
2409 /* First parse the global configuration values, because we want to apply
2410 * other parameters on top of the default configuration provided by the
2411 * codec.
2412 */
2413 argv = argv_dup(argc - 1, argv_ + 1);
2414 parse_global_config(&global, argv);
2415
2416 {
2417 /* Now parse each stream's parameters. Using a local scope here
2418 * due to the use of 'stream' as loop variable in FOREACH_STREAM
2419 * loops
John Koleszarefd54f82012-02-13 16:52:18 -08002420 */
John Koleszar6ad3b742012-11-06 12:02:42 -08002421 struct stream_state *stream = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002422
John Koleszar6ad3b742012-11-06 12:02:42 -08002423 do {
2424 stream = new_stream(&global, stream);
2425 stream_cnt++;
2426 if (!streams)
2427 streams = stream;
2428 } while (parse_stream_params(&global, stream, argv));
John Koleszarc6b90392012-07-13 15:21:29 -07002429 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002430
John Koleszarc6b90392012-07-13 15:21:29 -07002431 /* Check for unrecognized options */
2432 for (argi = argv; *argi; argi++)
2433 if (argi[0][0] == '-' && argi[0][1])
2434 die("Error: Unrecognized option %s\n", *argi);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002435
John Koleszarc6b90392012-07-13 15:21:29 -07002436 /* Handle non-option arguments */
John Koleszar6ad3b742012-11-06 12:02:42 -08002437 input.fn = argv[0];
John Koleszar0ea50ce2010-05-18 11:58:33 -04002438
John Koleszar6ad3b742012-11-06 12:02:42 -08002439 if (!input.fn)
John Koleszarc6b90392012-07-13 15:21:29 -07002440 usage_exit();
John Koleszar53291892010-10-22 14:48:21 -04002441
John Koleszar6ad3b742012-11-06 12:02:42 -08002442 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
2443 int frames_in = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002444
John Koleszar6ad3b742012-11-06 12:02:42 -08002445 open_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002446
John Koleszar6ad3b742012-11-06 12:02:42 -08002447 /* If the input file doesn't specify its w/h (raw files), try to get
2448 * the data from the first stream's configuration.
John Koleszarc6b90392012-07-13 15:21:29 -07002449 */
John Koleszar6ad3b742012-11-06 12:02:42 -08002450 if (!input.w || !input.h)
2451 FOREACH_STREAM( {
2452 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
2453 input.w = stream->config.cfg.g_w;
2454 input.h = stream->config.cfg.g_h;
2455 break;
John Koleszarc6b90392012-07-13 15:21:29 -07002456 }
John Koleszar6ad3b742012-11-06 12:02:42 -08002457 });
John Koleszarc6b90392012-07-13 15:21:29 -07002458
John Koleszar6ad3b742012-11-06 12:02:42 -08002459 /* Update stream configurations from the input file's parameters */
2460 FOREACH_STREAM(set_stream_dimensions(stream, input.w, input.h));
2461 FOREACH_STREAM(validate_stream_config(stream));
John Koleszar77e6b452010-11-03 13:58:40 -04002462
John Koleszar6ad3b742012-11-06 12:02:42 -08002463 /* Ensure that --passes and --pass are consistent. If --pass is set and
2464 * --passes=2, ensure --fpf was set.
2465 */
2466 if (global.pass && global.passes == 2)
2467 FOREACH_STREAM( {
2468 if (!stream->config.stats_fn)
2469 die("Stream %d: Must specify --fpf when --pass=%d"
2470 " and --passes=2\n", stream->index, global.pass);
2471 });
John Koleszar3245d462010-06-10 17:10:12 -04002472
John Koleszar3245d462010-06-10 17:10:12 -04002473
John Koleszar6ad3b742012-11-06 12:02:42 -08002474 /* Use the frame rate from the file only if none was specified
2475 * on the command-line.
2476 */
2477 if (!global.have_framerate)
2478 global.framerate = input.framerate;
Timothy B. Terriberry44d89492010-05-26 18:27:51 -04002479
John Koleszar6ad3b742012-11-06 12:02:42 -08002480 FOREACH_STREAM(set_default_kf_interval(stream, &global));
2481
2482 /* Show configuration */
2483 if (global.verbose && pass == 0)
2484 FOREACH_STREAM(show_stream_config(stream, &global, &input));
2485
2486 if (pass == (global.pass ? global.pass - 1 : 0)) {
2487 if (input.file_type == FILE_TYPE_Y4M)
John Koleszarc6b90392012-07-13 15:21:29 -07002488 /*The Y4M reader does its own allocation.
2489 Just initialize this here to avoid problems if we never read any
2490 frames.*/
2491 memset(&raw, 0, sizeof(raw));
2492 else
John Koleszar6ad3b742012-11-06 12:02:42 -08002493 vpx_img_alloc(&raw,
2494 input.use_i420 ? VPX_IMG_FMT_I420
2495 : VPX_IMG_FMT_YV12,
2496 input.w, input.h, 32);
John Koleszar05239f02011-06-29 09:53:12 -04002497
John Koleszar6ad3b742012-11-06 12:02:42 -08002498 FOREACH_STREAM(init_rate_histogram(&stream->rate_hist,
2499 &stream->config.cfg,
2500 &global.framerate));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002501 }
2502
John Koleszar6ad3b742012-11-06 12:02:42 -08002503 FOREACH_STREAM(open_output_file(stream, &global));
2504 FOREACH_STREAM(setup_pass(stream, &global, pass));
2505 FOREACH_STREAM(initialize_encoder(stream, &global));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002506
John Koleszarc6b90392012-07-13 15:21:29 -07002507 frame_avail = 1;
2508 got_data = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002509
John Koleszarc6b90392012-07-13 15:21:29 -07002510 while (frame_avail || got_data) {
John Koleszarc6b90392012-07-13 15:21:29 -07002511 struct vpx_usec_timer timer;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002512
John Koleszar6ad3b742012-11-06 12:02:42 -08002513 if (!global.limit || frames_in < global.limit) {
2514 frame_avail = read_frame(&input, &raw);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002515
John Koleszarc6b90392012-07-13 15:21:29 -07002516 if (frame_avail)
2517 frames_in++;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002518
John Koleszar6ad3b742012-11-06 12:02:42 -08002519 if (!global.quiet) {
2520 if (stream_cnt == 1)
2521 fprintf(stderr,
2522 "\rPass %d/%d frame %4d/%-4d %7"PRId64"B \033[K",
2523 pass + 1, global.passes, frames_in,
2524 streams->frames_out, (int64_t)streams->nbytes);
2525 else
2526 fprintf(stderr,
2527 "\rPass %d/%d frame %4d %7lu %s (%.2f fps)\033[K",
2528 pass + 1, global.passes, frames_in,
2529 cx_time > 9999999 ? cx_time / 1000 : cx_time,
2530 cx_time > 9999999 ? "ms" : "us",
2531 usec_to_fps(cx_time, frames_in));
John Koleszar3245d462010-06-10 17:10:12 -04002532 }
2533
John Koleszarc6b90392012-07-13 15:21:29 -07002534 } else
2535 frame_avail = 0;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002536
John Koleszar6ad3b742012-11-06 12:02:42 -08002537 if (frames_in > global.skip_frames) {
2538 vpx_usec_timer_start(&timer);
2539 FOREACH_STREAM(encode_frame(stream, &global,
2540 frame_avail ? &raw : NULL,
2541 frames_in));
2542 vpx_usec_timer_mark(&timer);
2543 cx_time += (unsigned long)vpx_usec_timer_elapsed(&timer);
John Koleszarc6b90392012-07-13 15:21:29 -07002544
John Koleszar6ad3b742012-11-06 12:02:42 -08002545 FOREACH_STREAM(update_quantizer_histogram(stream));
John Koleszarc6b90392012-07-13 15:21:29 -07002546
John Koleszar0ea50ce2010-05-18 11:58:33 -04002547 got_data = 0;
John Koleszar6ad3b742012-11-06 12:02:42 -08002548 FOREACH_STREAM(get_cx_data(stream, &global, &got_data));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002549
Yaowu Xu606c6472012-11-08 16:22:54 -08002550 if (got_data && global.test_decode)
Ronald S. Bultje195172c2012-11-10 16:22:49 -08002551 FOREACH_STREAM(test_decode(stream));
John Koleszarc6b90392012-07-13 15:21:29 -07002552 }
John Koleszar0ea50ce2010-05-18 11:58:33 -04002553
John Koleszarc6b90392012-07-13 15:21:29 -07002554 fflush(stdout);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002555 }
2556
John Koleszar6ad3b742012-11-06 12:02:42 -08002557 if (stream_cnt > 1)
2558 fprintf(stderr, "\n");
John Koleszar3fde9962011-06-20 17:09:29 -04002559
John Koleszar6ad3b742012-11-06 12:02:42 -08002560 if (!global.quiet)
2561 FOREACH_STREAM(fprintf(
2562 stderr,
2563 "\rPass %d/%d frame %4d/%-4d %7"PRId64"B %7lub/f %7"PRId64"b/s"
2564 " %7"PRId64" %s (%.2f fps)\033[K\n", pass + 1,
2565 global.passes, frames_in, stream->frames_out, (int64_t)stream->nbytes,
2566 frames_in ? (unsigned long)(stream->nbytes * 8 / frames_in) : 0,
2567 frames_in ? (int64_t)stream->nbytes * 8
2568 * (int64_t)global.framerate.num / global.framerate.den
2569 / frames_in
2570 : 0,
2571 stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time,
2572 stream->cx_time > 9999999 ? "ms" : "us",
2573 usec_to_fps(stream->cx_time, frames_in));
2574 );
John Koleszarc96f8e22011-06-21 16:42:33 -04002575
John Koleszar6ad3b742012-11-06 12:02:42 -08002576 if (global.show_psnr)
2577 FOREACH_STREAM(show_psnr(stream));
John Koleszarc6b90392012-07-13 15:21:29 -07002578
John Koleszar6ad3b742012-11-06 12:02:42 -08002579 FOREACH_STREAM(vpx_codec_destroy(&stream->encoder));
2580
2581 if (global.test_decode) {
2582 FOREACH_STREAM(vpx_codec_destroy(&stream->decoder));
2583 FOREACH_STREAM(vpx_img_free(&stream->ref_enc.img));
2584 FOREACH_STREAM(vpx_img_free(&stream->ref_dec.img));
Yaowu Xuf798f9e2012-03-07 12:25:50 -08002585 }
2586
John Koleszar6ad3b742012-11-06 12:02:42 -08002587 close_input_file(&input);
John Koleszar0ea50ce2010-05-18 11:58:33 -04002588
John Koleszar6ad3b742012-11-06 12:02:42 -08002589 FOREACH_STREAM(close_output_file(stream, global.codec->fourcc));
John Koleszar0ea50ce2010-05-18 11:58:33 -04002590
John Koleszar6ad3b742012-11-06 12:02:42 -08002591 FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1));
John Koleszarc6b90392012-07-13 15:21:29 -07002592
John Koleszar6ad3b742012-11-06 12:02:42 -08002593 if (global.pass)
John Koleszarc6b90392012-07-13 15:21:29 -07002594 break;
2595 }
2596
John Koleszar6ad3b742012-11-06 12:02:42 -08002597 if (global.show_q_hist_buckets)
2598 FOREACH_STREAM(show_q_histogram(stream->counts,
2599 global.show_q_hist_buckets));
2600
2601 if (global.show_rate_hist_buckets)
2602 FOREACH_STREAM(show_rate_histogram(&stream->rate_hist,
2603 &stream->config.cfg,
2604 global.show_rate_hist_buckets));
2605 FOREACH_STREAM(destroy_rate_histogram(&stream->rate_hist));
2606
Ronald S. Bultje31214972012-10-11 10:13:48 -07002607#if CONFIG_INTERNAL_STATS
John Koleszar6ad3b742012-11-06 12:02:42 -08002608 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2609 * to match some existing utilities.
2610 */
2611 FOREACH_STREAM({
Ronald S. Bultje31214972012-10-11 10:13:48 -07002612 FILE *f = fopen("opsnr.stt", "a");
John Koleszar6ad3b742012-11-06 12:02:42 -08002613 if (stream->mismatch_seen) {
Ronald S. Bultje31214972012-10-11 10:13:48 -07002614 fprintf(f, "First mismatch occurred in frame %d\n",
John Koleszar6ad3b742012-11-06 12:02:42 -08002615 stream->mismatch_seen);
2616 } else {
2617 fprintf(f, "No mismatch detected in recon buffers\n");
Ronald S. Bultje31214972012-10-11 10:13:48 -07002618 }
Ronald S. Bultje31214972012-10-11 10:13:48 -07002619 fclose(f);
John Koleszar6ad3b742012-11-06 12:02:42 -08002620 });
Ronald S. Bultje31214972012-10-11 10:13:48 -07002621#endif
John Koleszarc6b90392012-07-13 15:21:29 -07002622
2623 vpx_img_free(&raw);
John Koleszarc6b90392012-07-13 15:21:29 -07002624 free(argv);
John Koleszar6ad3b742012-11-06 12:02:42 -08002625 free(streams);
John Koleszarc6b90392012-07-13 15:21:29 -07002626 return EXIT_SUCCESS;
John Koleszar0ea50ce2010-05-18 11:58:33 -04002627}