John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 5 | * 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 Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
Christian Duvivier | a116815 | 2012-08-14 14:12:00 -0700 | [diff] [blame] | 11 | #include "vpx_config.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 12 | |
Tero Rintaluoma | 11a222f | 2011-01-24 11:21:40 +0200 | [diff] [blame] | 13 | #if defined(_WIN32) || !CONFIG_OS_SUPPORT |
John Koleszar | 1df0314 | 2010-05-20 14:44:18 -0400 | [diff] [blame] | 14 | #define USE_POSIX_MMAP 0 |
| 15 | #else |
| 16 | #define USE_POSIX_MMAP 1 |
| 17 | #endif |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <string.h> |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 23 | #include <limits.h> |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 24 | #include <assert.h> |
John Koleszar | b749234 | 2010-05-24 11:39:59 -0400 | [diff] [blame] | 25 | #include "vpx/vpx_encoder.h" |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 26 | #if CONFIG_DECODERS |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 27 | #include "vpx/vpx_decoder.h" |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 28 | #endif |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 29 | #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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 36 | |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 37 | #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER |
John Koleszar | b749234 | 2010-05-24 11:39:59 -0400 | [diff] [blame] | 38 | #include "vpx/vp8cx.h" |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 39 | #endif |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 40 | #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 41 | #include "vpx/vp8dx.h" |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 42 | #endif |
| 43 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 44 | #include "vpx_ports/mem_ops.h" |
| 45 | #include "vpx_ports/vpx_timer.h" |
John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 46 | #include "tools_common.h" |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 47 | #include "y4minput.h" |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 48 | #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 */ |
| 54 | typedef __int64 off_t; |
| 55 | #define fseeko _fseeki64 |
| 56 | #define ftello _ftelli64 |
| 57 | #elif defined(_WIN32) |
James Zern | 87953d11 | 2011-09-06 12:21:19 -0700 | [diff] [blame] | 58 | /* MinGW defines off_t as long |
| 59 | and uses f{seek,tell}o64/off64_t for large files */ |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 60 | #define fseeko fseeko64 |
| 61 | #define ftello ftello64 |
James Zern | 87953d11 | 2011-09-06 12:21:19 -0700 | [diff] [blame] | 62 | #define off_t off64_t |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 63 | #endif |
| 64 | |
John Koleszar | 6291dd4 | 2012-06-19 21:05:36 -0700 | [diff] [blame] | 65 | #define LITERALU64(hi,lo) ((((uint64_t)hi)<<32)|lo) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 66 | |
Tero Rintaluoma | 11a222f | 2011-01-24 11:21:40 +0200 | [diff] [blame] | 67 | /* 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 |
| 70 | typedef long off_t; |
| 71 | #define fseeko fseek |
| 72 | #define ftello ftell |
| 73 | #endif |
| 74 | |
John Koleszar | 6291dd4 | 2012-06-19 21:05:36 -0700 | [diff] [blame] | 75 | /* Swallow warnings about unused results of fread/fwrite */ |
| 76 | static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 77 | FILE *stream) { |
| 78 | return fread(ptr, size, nmemb, stream); |
John Koleszar | 6291dd4 | 2012-06-19 21:05:36 -0700 | [diff] [blame] | 79 | } |
| 80 | #define fread wrap_fread |
| 81 | |
| 82 | static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 83 | FILE *stream) { |
| 84 | return fwrite(ptr, size, nmemb, stream); |
John Koleszar | 6291dd4 | 2012-06-19 21:05:36 -0700 | [diff] [blame] | 85 | } |
| 86 | #define fwrite wrap_fwrite |
| 87 | |
| 88 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 89 | static const char *exec_name; |
| 90 | |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 91 | #define VP8_FOURCC (0x00385056) |
| 92 | #define VP9_FOURCC (0x00395056) |
Jim Bankoski | 1b16e74 | 2012-07-23 12:32:59 -0700 | [diff] [blame] | 93 | static const struct codec_item { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 94 | char const *name; |
Jim Bankoski | 1b16e74 | 2012-07-23 12:32:59 -0700 | [diff] [blame] | 95 | const vpx_codec_iface_t *(*iface)(void); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 96 | const vpx_codec_iface_t *(*dx_iface)(void); |
Jim Bankoski | 1b16e74 | 2012-07-23 12:32:59 -0700 | [diff] [blame] | 97 | unsigned int fourcc; |
Jim Bankoski | 1b16e74 | 2012-07-23 12:32:59 -0700 | [diff] [blame] | 98 | } codecs[] = { |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 99 | #if CONFIG_VP8_ENCODER && CONFIG_VP8_DECODER |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 100 | {"vp8", &vpx_codec_vp8_cx, &vpx_codec_vp8_dx, VP8_FOURCC}, |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 101 | #elif CONFIG_VP8_ENCODER && !CONFIG_VP8_DECODER |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 102 | {"vp8", &vpx_codec_vp8_cx, NULL, VP8_FOURCC}, |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 103 | #endif |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 104 | #if CONFIG_VP9_ENCODER && CONFIG_VP9_DECODER |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 105 | {"vp9", &vpx_codec_vp9_cx, &vpx_codec_vp9_dx, VP9_FOURCC}, |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 106 | #elif CONFIG_VP9_ENCODER && !CONFIG_VP9_DECODER |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 107 | {"vp9", &vpx_codec_vp9_cx, NULL, VP9_FOURCC}, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 108 | #endif |
| 109 | }; |
| 110 | |
| 111 | static void usage_exit(); |
| 112 | |
John Koleszar | c535025 | 2012-02-14 09:37:44 -0800 | [diff] [blame] | 113 | #define LOG_ERROR(label) do \ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 114 | {\ |
John Koleszar | c535025 | 2012-02-14 09:37:44 -0800 | [diff] [blame] | 115 | const char *l=label;\ |
| 116 | va_list ap;\ |
| 117 | va_start(ap, fmt);\ |
| 118 | if(l)\ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 119 | fprintf(stderr, "%s: ", l);\ |
John Koleszar | c535025 | 2012-02-14 09:37:44 -0800 | [diff] [blame] | 120 | vfprintf(stderr, fmt, ap);\ |
| 121 | fprintf(stderr, "\n");\ |
| 122 | va_end(ap);\ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 123 | } while(0) |
John Koleszar | c535025 | 2012-02-14 09:37:44 -0800 | [diff] [blame] | 124 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 125 | void die(const char *fmt, ...) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 126 | LOG_ERROR(NULL); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 127 | usage_exit(); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 128 | } |
| 129 | |
John Koleszar | c535025 | 2012-02-14 09:37:44 -0800 | [diff] [blame] | 130 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 131 | void fatal(const char *fmt, ...) { |
| 132 | LOG_ERROR("Fatal"); |
| 133 | exit(EXIT_FAILURE); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | void warn(const char *fmt, ...) { |
| 138 | LOG_ERROR("Warning"); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s, ...) { |
| 143 | va_list ap; |
| 144 | |
| 145 | va_start(ap, s); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 146 | if (ctx->err) { |
| 147 | const char *detail = vpx_codec_error_detail(ctx); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 148 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 149 | vfprintf(stderr, s, ap); |
| 150 | fprintf(stderr, ": %s\n", vpx_codec_error(ctx)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 151 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 152 | if (detail) |
| 153 | fprintf(stderr, " %s\n", detail); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 154 | |
John Koleszar | c535025 | 2012-02-14 09:37:44 -0800 | [diff] [blame] | 155 | exit(EXIT_FAILURE); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 156 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* This structure is used to abstract the different ways of handling |
| 160 | * first pass statistics. |
| 161 | */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 162 | typedef struct { |
| 163 | vpx_fixed_buf_t buf; |
| 164 | int pass; |
| 165 | FILE *file; |
| 166 | char *buf_ptr; |
| 167 | size_t buf_alloc_sz; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 168 | } stats_io_t; |
| 169 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 170 | int stats_open_file(stats_io_t *stats, const char *fpf, int pass) { |
| 171 | int res; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 172 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 173 | stats->pass = pass; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 174 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 175 | 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 181 | #if 0 |
| 182 | #elif USE_POSIX_MMAP |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 183 | struct stat stat_buf; |
| 184 | int fd; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 185 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 186 | 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 192 | res = (stats->buf.buf != NULL); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 193 | #else |
| 194 | size_t nbytes; |
| 195 | |
| 196 | stats->file = fopen(fpf, "rb"); |
| 197 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 198 | if (fseek(stats->file, 0, SEEK_END)) |
| 199 | fatal("First-pass stats file must be seekable!"); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 200 | |
| 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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 206 | if (!stats->buf.buf) |
| 207 | fatal("Failed to allocate first-pass stats buffer (%lu bytes)", |
| 208 | (unsigned long)stats->buf_alloc_sz); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 209 | |
| 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 | |
| 218 | int 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 234 | void stats_close(stats_io_t *stats, int last_pass) { |
| 235 | if (stats->file) { |
| 236 | if (stats->pass == last_pass) { |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 237 | #if 0 |
| 238 | #elif USE_POSIX_MMAP |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 239 | munmap(stats->buf.buf, stats->buf.sz); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 240 | #else |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 241 | free(stats->buf.buf); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 242 | #endif |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 243 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 244 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 245 | fclose(stats->file); |
| 246 | stats->file = NULL; |
| 247 | } else { |
| 248 | if (stats->pass == last_pass) |
| 249 | free(stats->buf.buf); |
| 250 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 251 | } |
| 252 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 253 | void stats_write(stats_io_t *stats, const void *pkt, size_t len) { |
| 254 | if (stats->file) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 255 | (void) fwrite(pkt, 1, len, stats->file); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 256 | } 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 260 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 261 | 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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 265 | } else |
| 266 | fatal("Failed to realloc firstpass stats buffer."); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 267 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 268 | |
| 269 | memcpy(stats->buf_ptr, pkt, len); |
| 270 | stats->buf.sz += len; |
| 271 | stats->buf_ptr += len; |
| 272 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 273 | } |
| 274 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 275 | vpx_fixed_buf_t stats_get(stats_io_t *stats) { |
| 276 | return stats->buf; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Alok Ahuja | 72c76ca | 2011-04-21 00:50:07 -0700 | [diff] [blame] | 279 | /* Stereo 3D packed frame format */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 280 | typedef 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 Ahuja | 72c76ca | 2011-04-21 00:50:07 -0700 | [diff] [blame] | 286 | } stereo_format_t; |
| 287 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 288 | enum video_file_type { |
| 289 | FILE_TYPE_RAW, |
| 290 | FILE_TYPE_IVF, |
| 291 | FILE_TYPE_Y4M |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 292 | }; |
| 293 | |
John Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 294 | struct detect_buffer { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 295 | char buf[4]; |
| 296 | size_t buf_read; |
| 297 | size_t position; |
John Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 301 | struct 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 Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | |
| 314 | #define IVF_FRAME_HDR_SZ (4+8) /* 4 byte size + 8 byte timestamp */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 315 | static 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 320 | int plane = 0; |
| 321 | int shortread = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 322 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 323 | 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. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 329 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 330 | /* 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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 334 | (void) fread(junk, 1, IVF_FRAME_HDR_SZ, f); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 335 | } |
| 336 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 337 | 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
Tom Finegan | e703af9 | 2010-06-16 13:24:55 -0400 | [diff] [blame] | 382 | unsigned int file_is_y4m(FILE *infile, |
John Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 383 | y4m_input *y4m, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 384 | char detect[4]) { |
| 385 | if (memcmp(detect, "YUV4", 4) == 0) { |
| 386 | return 1; |
| 387 | } |
| 388 | return 0; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 389 | } |
| 390 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 391 | #define IVF_FILE_HDR_SZ (32) |
John Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 392 | unsigned int file_is_ivf(struct input_state *input, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 393 | unsigned int *fourcc) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 394 | char raw_hdr[IVF_FILE_HDR_SZ]; |
| 395 | int is_ivf = 0; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 396 | FILE *infile = input->file; |
| 397 | unsigned int *width = &input->w; |
| 398 | unsigned int *height = &input->h; |
| 399 | struct detect_buffer *detect = &input->detect; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 400 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 401 | if (memcmp(detect->buf, "DKIF", 4) != 0) |
| 402 | return 0; |
John Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 403 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 404 | /* 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 409 | { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 410 | is_ivf = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 411 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 412 | if (mem_get_le16(raw_hdr + 4) != 0) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 413 | warn("Unrecognized IVF version! This file may not decode " |
| 414 | "properly."); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 415 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 416 | *fourcc = mem_get_le32(raw_hdr + 8); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 417 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 418 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 419 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 420 | 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 425 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 426 | return is_ivf; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | |
| 430 | static void write_ivf_file_header(FILE *outfile, |
| 431 | const vpx_codec_enc_cfg_t *cfg, |
| 432 | unsigned int fourcc, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 433 | int frame_cnt) { |
| 434 | char header[32]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 435 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 436 | if (cfg->g_pass != VPX_RC_ONE_PASS && cfg->g_pass != VPX_RC_LAST_PASS) |
| 437 | return; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 438 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 439 | 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 452 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 453 | (void) fwrite(header, 1, 32, outfile); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
| 457 | static void write_ivf_frame_header(FILE *outfile, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 458 | const vpx_codec_cx_pkt_t *pkt) { |
| 459 | char header[12]; |
| 460 | vpx_codec_pts_t pts; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 461 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 462 | if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) |
| 463 | return; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 464 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 465 | pts = pkt->data.frame.pts; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 466 | mem_put_le32(header, (int)pkt->data.frame.sz); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 467 | mem_put_le32(header + 4, pts & 0xFFFFFFFF); |
| 468 | mem_put_le32(header + 8, pts >> 32); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 469 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 470 | (void) fwrite(header, 1, 12, outfile); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 471 | } |
| 472 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 473 | static 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 Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 477 | } |
| 478 | |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 479 | |
| 480 | typedef off_t EbmlLoc; |
| 481 | |
| 482 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 483 | struct cue_entry { |
| 484 | unsigned int time; |
| 485 | uint64_t loc; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 486 | }; |
| 487 | |
| 488 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 489 | struct EbmlGlobal { |
| 490 | int debug; |
John Koleszar | 75afcee | 2010-10-26 16:22:22 -0400 | [diff] [blame] | 491 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 492 | FILE *stream; |
| 493 | int64_t last_pts_ms; |
| 494 | vpx_rational_t framerate; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 495 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 496 | /* 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 503 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 504 | /* This pointer is to a specific element to be serialized */ |
| 505 | off_t track_id_pos; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 506 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 507 | /* These pointers are to the size field of the element */ |
| 508 | EbmlLoc startSegment; |
| 509 | EbmlLoc startCluster; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 510 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 511 | uint32_t cluster_timecode; |
| 512 | int cluster_open; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 513 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 514 | struct cue_entry *cue_list; |
| 515 | unsigned int cues; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 516 | |
| 517 | }; |
| 518 | |
| 519 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 520 | void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 521 | (void) fwrite(buffer_in, 1, len, glob->stream); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 522 | } |
| 523 | |
Yunqing Wang | c156a68 | 2011-07-12 14:34:51 -0400 | [diff] [blame] | 524 | #define WRITE_BUFFER(s) \ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 525 | for(i = len-1; i>=0; i--)\ |
| 526 | { \ |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 527 | x = (char)(*(const s *)buffer_in >> (i * CHAR_BIT)); \ |
Yunqing Wang | c156a68 | 2011-07-12 14:34:51 -0400 | [diff] [blame] | 528 | Ebml_Write(glob, &x, 1); \ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 529 | } |
| 530 | void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, int buffer_size, unsigned long len) { |
| 531 | char x; |
| 532 | int i; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 533 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 534 | /* 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 Wang | c156a68 | 2011-07-12 14:34:51 -0400 | [diff] [blame] | 556 | } |
| 557 | #undef WRITE_BUFFER |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 558 | |
James Zern | 3a975d9 | 2011-07-25 13:26:18 -0700 | [diff] [blame] | 559 | /* Need a fixed size serializer for the track ID. libmkv provides a 64 bit |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 560 | * one, but not a 32 bit one. |
| 561 | */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 562 | static 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 Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 570 | static void |
| 571 | Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 572 | unsigned long class_id) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 573 | /* 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 576 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 577 | Ebml_WriteID(glob, class_id); |
| 578 | *ebmlLoc = ftello(glob->stream); |
| 579 | Ebml_Serialize(glob, &unknownLen, sizeof(unknownLen), 8); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static void |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 583 | Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc) { |
| 584 | off_t pos; |
| 585 | uint64_t size; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 586 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 587 | /* Save the current stream pointer */ |
| 588 | pos = ftello(glob->stream); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 589 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 590 | /* Calculate the size of this element */ |
| 591 | size = pos - *ebmlLoc - 8; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 592 | size |= LITERALU64(0x01000000, 0x00000000); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 593 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 594 | /* 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 597 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 598 | /* Reset the stream pointer */ |
| 599 | fseeko(glob->stream, pos, SEEK_SET); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | |
| 603 | static void |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 604 | write_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 | |
| 614 | static void |
| 615 | write_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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 628 | EbmlLoc start; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 629 | |
| 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 634 | Ebml_EndSubElement(ebml, &start); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 635 | } |
| 636 | { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 637 | /* segment info */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 638 | EbmlLoc startInfo; |
| 639 | uint64_t frame_time; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 640 | char version_string[64]; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 641 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 642 | /* 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 650 | } |
John Koleszar | fa3e530 | 2011-09-22 13:24:33 -0400 | [diff] [blame] | 651 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 652 | 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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 658 | (double)(ebml->last_pts_ms + frame_time)); |
| 659 | Ebml_SerializeString(ebml, 0x4D80, version_string); |
| 660 | Ebml_SerializeString(ebml, 0x5741, version_string); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 661 | Ebml_EndSubElement(ebml, &startInfo); |
| 662 | } |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | |
| 666 | static void |
| 667 | write_webm_file_header(EbmlGlobal *glob, |
| 668 | const vpx_codec_enc_cfg_t *cfg, |
Alok Ahuja | 72c76ca | 2011-04-21 00:50:07 -0700 | [diff] [blame] | 669 | const struct vpx_rational *fps, |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 670 | stereo_format_t stereo_fmt, |
| 671 | unsigned int fourcc) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 672 | { |
| 673 | EbmlLoc start; |
| 674 | Ebml_StartSubElement(glob, &start, EBML); |
| 675 | Ebml_SerializeUnsigned(glob, EBMLVersion, 1); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 676 | 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 682 | Ebml_EndSubElement(glob, &start); |
| 683 | } |
| 684 | { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 685 | Ebml_StartSubElement(glob, &glob->startSegment, Segment); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 686 | glob->position_reference = ftello(glob->stream); |
| 687 | glob->framerate = *fps; |
| 688 | write_webm_seek_info(glob); |
| 689 | |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 690 | { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 691 | 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 698 | EbmlLoc start; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 699 | 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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 703 | Ebml_SerializeUnsigned(glob, TrackType, 1); |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 704 | Ebml_SerializeString(glob, CodecID, |
| 705 | fourcc == VP8_FOURCC ? "V_VP8" : "V_VP9"); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 706 | { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 707 | unsigned int pixelWidth = cfg->g_w; |
| 708 | unsigned int pixelHeight = cfg->g_h; |
| 709 | float frameRate = (float)fps->num / (float)fps->den; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 710 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 711 | 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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 717 | Ebml_EndSubElement(glob, &videoStart); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 718 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 719 | Ebml_EndSubElement(glob, &start); /* Track Entry */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 720 | } |
| 721 | Ebml_EndSubElement(glob, &trackStart); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 722 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 723 | /* segment element is open */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 724 | } |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | |
| 728 | static void |
| 729 | write_webm_block(EbmlGlobal *glob, |
| 730 | const vpx_codec_enc_cfg_t *cfg, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 731 | 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 738 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 739 | /* 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 745 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 746 | /* Calculate the relative time of this block */ |
| 747 | if (pts_ms - glob->cluster_timecode > SHRT_MAX) |
| 748 | start_cluster = 1; |
| 749 | else |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 750 | block_timecode = (unsigned short)pts_ms - glob->cluster_timecode; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 751 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 752 | 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 756 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 757 | /* Open the new cluster */ |
| 758 | block_timecode = 0; |
| 759 | glob->cluster_open = 1; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 760 | glob->cluster_timecode = (uint32_t)pts_ms; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 761 | glob->cluster_pos = ftello(glob->stream); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 762 | Ebml_StartSubElement(glob, &glob->startCluster, Cluster); /* cluster */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 763 | Ebml_SerializeUnsigned(glob, Timecode, glob->cluster_timecode); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 764 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 765 | /* Save a cue point if this is a keyframe. */ |
| 766 | if (is_keyframe) { |
| 767 | struct cue_entry *cue, *new_cue_list; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 768 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 769 | 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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 773 | else |
| 774 | fatal("Failed to realloc cue list."); |
John Koleszar | 26b6a3b | 2011-03-29 21:44:19 -0400 | [diff] [blame] | 775 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 776 | cue = &glob->cue_list[glob->cues]; |
| 777 | cue->time = glob->cluster_timecode; |
| 778 | cue->loc = glob->cluster_pos; |
| 779 | glob->cues++; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 780 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 781 | } |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 782 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 783 | /* Write the Simple Block */ |
| 784 | Ebml_WriteID(glob, SimpleBlock); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 785 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 786 | block_length = (unsigned long)pkt->data.frame.sz + 4; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 787 | block_length |= 0x10000000; |
| 788 | Ebml_Serialize(glob, &block_length, sizeof(block_length), 4); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 789 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 790 | track_number = 1; |
| 791 | track_number |= 0x80; |
| 792 | Ebml_Write(glob, &track_number, 1); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 793 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 794 | Ebml_Serialize(glob, &block_timecode, sizeof(block_timecode), 2); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 795 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 796 | 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 802 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 803 | Ebml_Write(glob, pkt->data.frame.buf, (unsigned long)pkt->data.frame.sz); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | |
| 807 | static void |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 808 | write_webm_file_footer(EbmlGlobal *glob, long hash) { |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 809 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 810 | if (glob->cluster_open) |
| 811 | Ebml_EndSubElement(glob, &glob->startCluster); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 812 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 813 | { |
| 814 | EbmlLoc start; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 815 | unsigned int i; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 816 | |
| 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 Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 825 | EbmlLoc start; |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 826 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 827 | Ebml_SerializeUnsigned(glob, CueTime, cue->time); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 828 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 829 | Ebml_StartSubElement(glob, &start, CueTrackPositions); |
| 830 | Ebml_SerializeUnsigned(glob, CueTrack, 1); |
| 831 | Ebml_SerializeUnsigned64(glob, CueClusterPosition, |
| 832 | cue->loc - glob->position_reference); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 833 | Ebml_EndSubElement(glob, &start); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 834 | } |
| 835 | Ebml_EndSubElement(glob, &start); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 836 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 837 | Ebml_EndSubElement(glob, &start); |
| 838 | } |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 839 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 840 | Ebml_EndSubElement(glob, &glob->startSegment); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 841 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 842 | /* Patch up the seek info block */ |
| 843 | write_webm_seek_info(glob); |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 844 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 845 | /* 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 Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 848 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 849 | fseeko(glob->stream, 0, SEEK_END); |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 853 | /* Murmur hash derived from public domain reference implementation at |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 854 | * http:// sites.google.com/site/murmurhash/ |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 855 | */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 856 | static unsigned int murmur(const void *key, int len, unsigned int seed) { |
| 857 | const unsigned int m = 0x5bd1e995; |
| 858 | const int r = 24; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 859 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 860 | unsigned int h = seed ^ len; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 861 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 862 | const unsigned char *data = (const unsigned char *)key; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 863 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 864 | while (len >= 4) { |
| 865 | unsigned int k; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 866 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 867 | k = data[0]; |
| 868 | k |= data[1] << 8; |
| 869 | k |= data[2] << 16; |
| 870 | k |= data[3] << 24; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 871 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 872 | k *= m; |
| 873 | k ^= k >> r; |
| 874 | k *= m; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 875 | |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 876 | h *= m; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 877 | h ^= k; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 878 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 879 | 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 Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 898 | } |
| 899 | |
Frank Galligan | 3d84da6 | 2010-10-27 11:28:56 -0400 | [diff] [blame] | 900 | #include "math.h" |
Yaowu Xu | 8cf28d3 | 2012-03-07 09:05:26 -0800 | [diff] [blame] | 901 | #define MAX_PSNR 100 |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 902 | static double vp8_mse2psnr(double Samples, double Peak, double Mse) { |
| 903 | double psnr; |
Frank Galligan | 3d84da6 | 2010-10-27 11:28:56 -0400 | [diff] [blame] | 904 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 905 | if ((double)Mse > 0.0) |
| 906 | psnr = 10.0 * log10(Peak * Peak * Samples / Mse); |
| 907 | else |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 908 | psnr = MAX_PSNR; /* Limit to prevent / 0 */ |
Frank Galligan | 3d84da6 | 2010-10-27 11:28:56 -0400 | [diff] [blame] | 909 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 910 | if (psnr > MAX_PSNR) |
| 911 | psnr = MAX_PSNR; |
Frank Galligan | 3d84da6 | 2010-10-27 11:28:56 -0400 | [diff] [blame] | 912 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 913 | return psnr; |
Frank Galligan | 3d84da6 | 2010-10-27 11:28:56 -0400 | [diff] [blame] | 914 | } |
| 915 | |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 916 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 917 | #include "args.h" |
John Koleszar | 75afcee | 2010-10-26 16:22:22 -0400 | [diff] [blame] | 918 | static const arg_def_t debugmode = ARG_DEF("D", "debug", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 919 | "Debug mode (makes output deterministic)"); |
John Koleszar | 5329189 | 2010-10-22 14:48:21 -0400 | [diff] [blame] | 920 | static const arg_def_t outputfile = ARG_DEF("o", "output", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 921 | "Output filename"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 922 | static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 923 | "Input file is YV12 "); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 924 | static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 925 | "Input file is I420 (default)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 926 | static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 927 | "Codec to use"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 928 | static const arg_def_t passes = ARG_DEF("p", "passes", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 929 | "Number of passes (1/2)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 930 | static const arg_def_t pass_arg = ARG_DEF(NULL, "pass", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 931 | "Pass to execute (1/2)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 932 | static const arg_def_t fpf_name = ARG_DEF(NULL, "fpf", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 933 | "First pass statistics file name"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 934 | static const arg_def_t limit = ARG_DEF(NULL, "limit", 1, |
| 935 | "Stop encoding after n input frames"); |
Yaowu Xu | 03d5d69 | 2012-03-14 07:37:10 -0700 | [diff] [blame] | 936 | static const arg_def_t skip = ARG_DEF(NULL, "skip", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 937 | "Skip the first n input frames"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 938 | static const arg_def_t deadline = ARG_DEF("d", "deadline", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 939 | "Deadline per frame (usec)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 940 | static const arg_def_t best_dl = ARG_DEF(NULL, "best", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 941 | "Use Best Quality Deadline"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 942 | static const arg_def_t good_dl = ARG_DEF(NULL, "good", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 943 | "Use Good Quality Deadline"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 944 | static const arg_def_t rt_dl = ARG_DEF(NULL, "rt", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 945 | "Use Realtime Quality Deadline"); |
James Zern | 1fc3cc8 | 2012-10-25 18:31:35 -0700 | [diff] [blame] | 946 | static const arg_def_t quietarg = ARG_DEF("q", "quiet", 0, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 947 | "Do not print encode progress"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 948 | static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 949 | "Show encoder parameters"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 950 | static const arg_def_t psnrarg = ARG_DEF(NULL, "psnr", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 951 | "Show PSNR in status line"); |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 952 | static const arg_def_t recontest = ARG_DEF(NULL, "test-decode", 0, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 953 | "Test encode/decode mismatch"); |
John Koleszar | 28f177c | 2010-10-21 16:52:14 -0400 | [diff] [blame] | 954 | static const arg_def_t framerate = ARG_DEF(NULL, "fps", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 955 | "Stream frame rate (rate/scale)"); |
John Koleszar | 456bfb1 | 2010-10-21 16:23:20 -0400 | [diff] [blame] | 956 | static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 957 | "Output IVF (default is WebM)"); |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 958 | static const arg_def_t out_part = ARG_DEF("P", "output-partitions", 0, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 959 | "Makes encoder output partitions. Requires IVF output!"); |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 960 | static const arg_def_t q_hist_n = ARG_DEF(NULL, "q-hist", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 961 | "Show quantizer histogram (n-buckets)"); |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 962 | static const arg_def_t rate_hist_n = ARG_DEF(NULL, "rate-hist", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 963 | "Show rate histogram (n-buckets)"); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 964 | static const arg_def_t *main_args[] = { |
| 965 | &debugmode, |
| 966 | &outputfile, &codecarg, &passes, &pass_arg, &fpf_name, &limit, &skip, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 967 | &deadline, &best_dl, &good_dl, &rt_dl, |
| 968 | &quietarg, &verbosearg, &psnrarg, &use_ivf, &out_part, &q_hist_n, &rate_hist_n, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 969 | NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 970 | }; |
| 971 | |
| 972 | static const arg_def_t usage = ARG_DEF("u", "usage", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 973 | "Usage profile number to use"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 974 | static const arg_def_t threads = ARG_DEF("t", "threads", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 975 | "Max number of threads to use"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 976 | static const arg_def_t profile = ARG_DEF(NULL, "profile", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 977 | "Bitstream profile number to use"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 978 | static const arg_def_t width = ARG_DEF("w", "width", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 979 | "Frame width"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 980 | static const arg_def_t height = ARG_DEF("h", "height", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 981 | "Frame height"); |
Alok Ahuja | 72c76ca | 2011-04-21 00:50:07 -0700 | [diff] [blame] | 982 | static const struct arg_enum_list stereo_mode_enum[] = { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 983 | {"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 Ahuja | 72c76ca | 2011-04-21 00:50:07 -0700 | [diff] [blame] | 989 | }; |
| 990 | static const arg_def_t stereo_mode = ARG_DEF_ENUM(NULL, "stereo-mode", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 991 | "Stereo 3D video format", stereo_mode_enum); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 992 | static const arg_def_t timebase = ARG_DEF(NULL, "timebase", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 993 | "Output timestamp precision (fractional seconds)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 994 | static const arg_def_t error_resilient = ARG_DEF(NULL, "error-resilient", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 995 | "Enable error resiliency features"); |
Deb Mukherjee | dfd89f2 | 2013-01-25 11:30:28 -0800 | [diff] [blame^] | 996 | #if CONFIG_VP9_ENCODER |
| 997 | static const arg_def_t frame_parallel_decoding = ARG_DEF( |
| 998 | NULL, "frame-parallel", 1, "Enable frame parallel decodability features"); |
| 999 | #endif |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1000 | static const arg_def_t lag_in_frames = ARG_DEF(NULL, "lag-in-frames", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1001 | "Max number of frames to lag"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1002 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1003 | static const arg_def_t *global_args[] = { |
| 1004 | &use_yv12, &use_i420, &usage, &threads, &profile, |
Deb Mukherjee | dfd89f2 | 2013-01-25 11:30:28 -0800 | [diff] [blame^] | 1005 | &width, &height, &stereo_mode, &timebase, &framerate, |
| 1006 | &error_resilient, |
| 1007 | #if CONFIG_VP9_ENCODER |
| 1008 | &frame_parallel_decoding, |
| 1009 | #endif |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1010 | &lag_in_frames, NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1011 | }; |
| 1012 | |
| 1013 | static const arg_def_t dropframe_thresh = ARG_DEF(NULL, "drop-frame", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1014 | "Temporal resampling threshold (buf %)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1015 | static const arg_def_t resize_allowed = ARG_DEF(NULL, "resize-allowed", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1016 | "Spatial resampling enabled (bool)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1017 | static const arg_def_t resize_up_thresh = ARG_DEF(NULL, "resize-up", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1018 | "Upscale threshold (buf %)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1019 | static const arg_def_t resize_down_thresh = ARG_DEF(NULL, "resize-down", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1020 | "Downscale threshold (buf %)"); |
John Koleszar | 500fec2 | 2011-03-21 07:50:42 -0400 | [diff] [blame] | 1021 | static const struct arg_enum_list end_usage_enum[] = { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1022 | {"vbr", VPX_VBR}, |
| 1023 | {"cbr", VPX_CBR}, |
| 1024 | {"cq", VPX_CQ}, |
| 1025 | {NULL, 0} |
John Koleszar | 500fec2 | 2011-03-21 07:50:42 -0400 | [diff] [blame] | 1026 | }; |
| 1027 | static const arg_def_t end_usage = ARG_DEF_ENUM(NULL, "end-usage", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1028 | "Rate control mode", end_usage_enum); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1029 | static const arg_def_t target_bitrate = ARG_DEF(NULL, "target-bitrate", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1030 | "Bitrate (kbps)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1031 | static const arg_def_t min_quantizer = ARG_DEF(NULL, "min-q", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1032 | "Minimum (best) quantizer"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1033 | static const arg_def_t max_quantizer = ARG_DEF(NULL, "max-q", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1034 | "Maximum (worst) quantizer"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1035 | static const arg_def_t undershoot_pct = ARG_DEF(NULL, "undershoot-pct", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1036 | "Datarate undershoot (min) target (%)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1037 | static const arg_def_t overshoot_pct = ARG_DEF(NULL, "overshoot-pct", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1038 | "Datarate overshoot (max) target (%)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1039 | static const arg_def_t buf_sz = ARG_DEF(NULL, "buf-sz", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1040 | "Client buffer size (ms)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1041 | static const arg_def_t buf_initial_sz = ARG_DEF(NULL, "buf-initial-sz", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1042 | "Client initial buffer size (ms)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1043 | static const arg_def_t buf_optimal_sz = ARG_DEF(NULL, "buf-optimal-sz", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1044 | "Client optimal buffer size (ms)"); |
| 1045 | static 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1050 | }; |
| 1051 | |
| 1052 | |
| 1053 | static const arg_def_t bias_pct = ARG_DEF(NULL, "bias-pct", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1054 | "CBR/VBR bias (0=CBR, 100=VBR)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1055 | static const arg_def_t minsection_pct = ARG_DEF(NULL, "minsection-pct", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1056 | "GOP min bitrate (% of target)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1057 | static const arg_def_t maxsection_pct = ARG_DEF(NULL, "maxsection-pct", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1058 | "GOP max bitrate (% of target)"); |
| 1059 | static const arg_def_t *rc_twopass_args[] = { |
| 1060 | &bias_pct, &minsection_pct, &maxsection_pct, NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1061 | }; |
| 1062 | |
| 1063 | |
| 1064 | static const arg_def_t kf_min_dist = ARG_DEF(NULL, "kf-min-dist", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1065 | "Minimum keyframe interval (frames)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1066 | static const arg_def_t kf_max_dist = ARG_DEF(NULL, "kf-max-dist", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1067 | "Maximum keyframe interval (frames)"); |
John Koleszar | b1e36f2 | 2010-06-24 08:41:51 -0400 | [diff] [blame] | 1068 | static const arg_def_t kf_disabled = ARG_DEF(NULL, "disable-kf", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1069 | "Disable keyframe placement"); |
| 1070 | static const arg_def_t *kf_args[] = { |
| 1071 | &kf_min_dist, &kf_max_dist, &kf_disabled, NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1072 | }; |
| 1073 | |
| 1074 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1075 | static const arg_def_t noise_sens = ARG_DEF(NULL, "noise-sensitivity", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1076 | "Noise sensitivity (frames to blur)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1077 | static const arg_def_t sharpness = ARG_DEF(NULL, "sharpness", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1078 | "Filter sharpness (0-7)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1079 | static const arg_def_t static_thresh = ARG_DEF(NULL, "static-thresh", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1080 | "Motion detection threshold"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1081 | static const arg_def_t cpu_used = ARG_DEF(NULL, "cpu-used", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1082 | "CPU Used (-16..16)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1083 | static const arg_def_t token_parts = ARG_DEF(NULL, "token-parts", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1084 | "Number of token partitions to use, log2"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1085 | static const arg_def_t auto_altref = ARG_DEF(NULL, "auto-alt-ref", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1086 | "Enable automatic alt reference frames"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1087 | static const arg_def_t arnr_maxframes = ARG_DEF(NULL, "arnr-maxframes", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1088 | "AltRef Max Frames"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1089 | static const arg_def_t arnr_strength = ARG_DEF(NULL, "arnr-strength", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1090 | "AltRef Strength"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1091 | static const arg_def_t arnr_type = ARG_DEF(NULL, "arnr-type", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1092 | "AltRef Type"); |
John Koleszar | b0da9b3 | 2010-12-17 09:43:39 -0500 | [diff] [blame] | 1093 | static const struct arg_enum_list tuning_enum[] = { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1094 | {"psnr", VP8_TUNE_PSNR}, |
| 1095 | {"ssim", VP8_TUNE_SSIM}, |
| 1096 | {NULL, 0} |
John Koleszar | b0da9b3 | 2010-12-17 09:43:39 -0500 | [diff] [blame] | 1097 | }; |
| 1098 | static const arg_def_t tune_ssim = ARG_DEF_ENUM(NULL, "tune", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1099 | "Material to favor", tuning_enum); |
Paul Wilkins | e0846c9 | 2011-01-07 18:29:37 +0000 | [diff] [blame] | 1100 | static const arg_def_t cq_level = ARG_DEF(NULL, "cq-level", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1101 | "Constrained Quality Level"); |
John Koleszar | 1654ae9 | 2011-07-28 09:17:32 -0400 | [diff] [blame] | 1102 | static const arg_def_t max_intra_rate_pct = ARG_DEF(NULL, "max-intra-rate", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1103 | "Max I-frame bitrate (pct)"); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1104 | #if CONFIG_LOSSLESS |
| 1105 | static const arg_def_t lossless = ARG_DEF(NULL, "lossless", 1, "Lossless mode"); |
| 1106 | #endif |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1107 | |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1108 | #if CONFIG_VP8_ENCODER |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1109 | static 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 Koleszar | 83b1d90 | 2012-11-05 12:37:14 -0800 | [diff] [blame] | 1112 | &tune_ssim, &cq_level, &max_intra_rate_pct, |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1113 | NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1114 | }; |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1115 | static 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 |
| 1126 | static 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 Koleszar | 83b1d90 | 2012-11-05 12:37:14 -0800 | [diff] [blame] | 1130 | #if CONFIG_LOSSLESS |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1131 | &lossless, |
John Koleszar | 83b1d90 | 2012-11-05 12:37:14 -0800 | [diff] [blame] | 1132 | #endif |
| 1133 | NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1134 | }; |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1135 | static const int vp9_arg_ctrl_map[] = { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1136 | 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 Koleszar | 83b1d90 | 2012-11-05 12:37:14 -0800 | [diff] [blame] | 1140 | 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1145 | }; |
| 1146 | #endif |
| 1147 | |
| 1148 | static const arg_def_t *no_args[] = { NULL }; |
| 1149 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1150 | static void usage_exit() { |
| 1151 | int i; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1152 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1153 | fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n", |
| 1154 | exec_name); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1155 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1156 | 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1166 | #if CONFIG_VP8_ENCODER |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1167 | fprintf(stderr, "\nVP8 Specific Options:\n"); |
| 1168 | arg_show_usage(stdout, vp8_args); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1169 | #endif |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1170 | #if CONFIG_VP9_ENCODER |
| 1171 | fprintf(stderr, "\nVP9 Specific Options:\n"); |
| 1172 | arg_show_usage(stdout, vp9_args); |
| 1173 | #endif |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1174 | 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1180 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1181 | for (i = 0; i < sizeof(codecs) / sizeof(codecs[0]); i++) |
| 1182 | fprintf(stderr, " %-6s - %s\n", |
| 1183 | codecs[i].name, |
Jim Bankoski | 1b16e74 | 2012-07-23 12:32:59 -0700 | [diff] [blame] | 1184 | vpx_codec_iface_name(codecs[i].iface())); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1185 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1186 | exit(EXIT_FAILURE); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1187 | } |
| 1188 | |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1189 | |
| 1190 | #define HIST_BAR_MAX 40 |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1191 | struct hist_bucket { |
| 1192 | int low, high, count; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1193 | }; |
| 1194 | |
| 1195 | |
| 1196 | static int merge_hist_buckets(struct hist_bucket *bucket, |
| 1197 | int *buckets_, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1198 | int max_buckets) { |
| 1199 | int small_bucket = 0, merge_bucket = INT_MAX, big_bucket = 0; |
| 1200 | int buckets = *buckets_; |
| 1201 | int i; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1202 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1203 | /* 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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1218 | /* merge the small bucket with an adjacent one. */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1219 | 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 Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1240 | } |
| 1241 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1242 | 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 Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1248 | */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1249 | big_bucket = small_bucket = 0; |
| 1250 | for (i = 0; i < buckets; i++) { |
| 1251 | if (i > merge_bucket) |
| 1252 | bucket[i] = bucket[i + 1]; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1253 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1254 | 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 Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1258 | } |
| 1259 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | *buckets_ = buckets; |
| 1263 | return bucket[big_bucket].count; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | |
| 1267 | static void show_histogram(const struct hist_bucket *bucket, |
| 1268 | int buckets, |
| 1269 | int total, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1270 | int scale) { |
| 1271 | const char *pat1, *pat2; |
| 1272 | int i; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1273 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1274 | 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 Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1305 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1306 | for (i = 0; i < buckets; i++) { |
| 1307 | int len; |
| 1308 | int j; |
| 1309 | float pct; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1310 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1311 | pct = (float)(100.0 * bucket[i].count / total); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1312 | len = HIST_BAR_MAX * bucket[i].count / scale; |
| 1313 | if (len < 1) |
| 1314 | len = 1; |
| 1315 | assert(len <= HIST_BAR_MAX); |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1316 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1317 | 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 Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1321 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1322 | 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 Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1329 | static 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 Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1335 | |
| 1336 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1337 | 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 Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1343 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1344 | } |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1345 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1346 | fprintf(stderr, "\nQuantizer Selection:\n"); |
| 1347 | scale = merge_hist_buckets(bucket, &buckets, max_buckets); |
| 1348 | show_histogram(bucket, buckets, total, scale); |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1352 | #define RATE_BINS (100) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1353 | struct 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 Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1360 | }; |
| 1361 | |
| 1362 | |
| 1363 | static void init_rate_histogram(struct rate_hist *hist, |
John Koleszar | 05239f0 | 2011-06-29 09:53:12 -0400 | [diff] [blame] | 1364 | const vpx_codec_enc_cfg_t *cfg, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1365 | const vpx_rational_t *fps) { |
| 1366 | int i; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1367 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1368 | /* 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 Rintaluoma | fd41cb8 | 2011-07-15 14:47:44 +0300 | [diff] [blame] | 1373 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1374 | /* prevent division by zero */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1375 | if (hist->samples == 0) |
| 1376 | hist->samples = 1; |
Tero Rintaluoma | fd41cb8 | 2011-07-15 14:47:44 +0300 | [diff] [blame] | 1377 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1378 | 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 Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1388 | static void destroy_rate_histogram(struct rate_hist *hist) { |
| 1389 | free(hist->pts); |
| 1390 | free(hist->sz); |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | |
| 1394 | static void update_rate_histogram(struct rate_hist *hist, |
| 1395 | const vpx_codec_enc_cfg_t *cfg, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1396 | const vpx_codec_cx_pkt_t *pkt) { |
| 1397 | int i, idx; |
| 1398 | int64_t now, then, sum_sz = 0, avg_bitrate; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1399 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1400 | now = pkt->data.frame.pts * 1000 |
| 1401 | * (uint64_t)cfg->g_timebase.num / (uint64_t)cfg->g_timebase.den; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1402 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1403 | idx = hist->frames++ % hist->samples; |
| 1404 | hist->pts[idx] = now; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1405 | hist->sz[idx] = (int)pkt->data.frame.sz; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1406 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1407 | if (now < cfg->rc_buf_initial_sz) |
| 1408 | return; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1409 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1410 | then = now; |
Johann | bb0ca87 | 2011-06-29 14:34:25 -0400 | [diff] [blame] | 1411 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1412 | /* 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 Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1415 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1416 | then = hist->pts[i_idx]; |
| 1417 | if (now - then > cfg->rc_buf_sz) |
| 1418 | break; |
| 1419 | sum_sz += hist->sz[i_idx]; |
| 1420 | } |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1421 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1422 | if (now == then) |
| 1423 | return; |
Johann | bb0ca87 | 2011-06-29 14:34:25 -0400 | [diff] [blame] | 1424 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1425 | avg_bitrate = sum_sz * 8 * 1000 / (now - then); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1426 | idx = (int)(avg_bitrate * (RATE_BINS / 2) / (cfg->rc_target_bitrate * 1000)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1427 | 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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1432 | hist->bucket[idx].low = (int)avg_bitrate; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1433 | if (hist->bucket[idx].high < avg_bitrate) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1434 | hist->bucket[idx].high = (int)avg_bitrate; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1435 | hist->bucket[idx].count++; |
| 1436 | hist->total++; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | static void show_rate_histogram(struct rate_hist *hist, |
| 1441 | const vpx_codec_enc_cfg_t *cfg, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1442 | int max_buckets) { |
| 1443 | int i, scale; |
| 1444 | int buckets = 0; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1445 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1446 | 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 Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1451 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1452 | 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 Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1455 | } |
| 1456 | |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 1457 | #define mmin(a, b) ((a) < (b) ? (a) : (b)) |
| 1458 | static void find_mismatch(vpx_image_t *img1, vpx_image_t *img2, |
| 1459 | int yloc[2], int uloc[2], int vloc[2]) { |
Paul Wilkins | f759762 | 2013-01-24 11:42:38 +0000 | [diff] [blame] | 1460 | const unsigned int bsize = 64; |
| 1461 | const unsigned int bsize2 = bsize >> 1; |
| 1462 | unsigned int match = 1; |
| 1463 | unsigned int i, j; |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 1464 | yloc[0] = yloc[1] = -1; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 1465 | for (i = 0, match = 1; match && i < img1->d_h; i += bsize) { |
| 1466 | for (j = 0; match && j < img1->d_w; j += bsize) { |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 1467 | int k, l; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 1468 | int si = mmin(i + bsize, img1->d_h) - i; |
| 1469 | int sj = mmin(j + bsize, img1->d_w) - j; |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 1470 | 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 Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 1485 | 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 Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 1487 | int k, l; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 1488 | int si = mmin(i + bsize2, (img1->d_h + 1) / 2) - i; |
| 1489 | int sj = mmin(j + bsize2, (img1->d_w + 1) / 2) - j; |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 1490 | 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 Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 1505 | 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 Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 1507 | int k, l; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 1508 | int si = mmin(i + bsize2, (img1->d_h + 1) / 2) - i; |
| 1509 | int sj = mmin(j + bsize2, (img1->d_w + 1) / 2) - j; |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 1510 | 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1525 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1526 | static int compare_img(vpx_image_t *img1, vpx_image_t *img2) |
| 1527 | { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1528 | int match = 1; |
Paul Wilkins | f759762 | 2013-01-24 11:42:38 +0000 | [diff] [blame] | 1529 | unsigned int i; |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 1530 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1531 | match &= (img1->fmt == img2->fmt); |
| 1532 | match &= (img1->w == img2->w); |
| 1533 | match &= (img1->h == img2->h); |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 1534 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1535 | for (i = 0; i < img1->d_h; i++) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1536 | 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1538 | img1->d_w) == 0); |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 1539 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1540 | 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 Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 1544 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1545 | 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 Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 1549 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1550 | return match; |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1554 | #define NELEMENTS(x) (sizeof(x)/sizeof(x[0])) |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1555 | #define MAX(x,y) ((x)>(y)?(x):(y)) |
| 1556 | #if CONFIG_VP8_ENCODER && !CONFIG_VP9_ENCODER |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1557 | #define ARG_CTRL_CNT_MAX NELEMENTS(vp8_arg_ctrl_map) |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1558 | #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 Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 1564 | |
| 1565 | /* Configuration elements common to all streams */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1566 | struct 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 Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 1585 | }; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1586 | |
| 1587 | |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1588 | /* Per-stream configuration */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1589 | struct 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1598 | }; |
| 1599 | |
| 1600 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1601 | struct 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1623 | }; |
| 1624 | |
| 1625 | |
John Koleszar | 1b27e93 | 2012-04-25 17:08:56 -0700 | [diff] [blame] | 1626 | void validate_positive_rational(const char *msg, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1627 | struct vpx_rational *rat) { |
| 1628 | if (rat->den < 0) { |
| 1629 | rat->num *= -1; |
| 1630 | rat->den *= -1; |
| 1631 | } |
John Koleszar | 1b27e93 | 2012-04-25 17:08:56 -0700 | [diff] [blame] | 1632 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1633 | if (rat->num < 0) |
| 1634 | die("Error: %s must be positive\n", msg); |
John Koleszar | 1b27e93 | 2012-04-25 17:08:56 -0700 | [diff] [blame] | 1635 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1636 | if (!rat->den) |
| 1637 | die("Error: %s has zero denominator\n", msg); |
John Koleszar | 1b27e93 | 2012-04-25 17:08:56 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1641 | static void parse_global_config(struct global_config *global, char **argv) { |
| 1642 | char **argi, **argj; |
| 1643 | struct arg arg; |
John Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 1644 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1645 | /* Initialize default parameters */ |
| 1646 | memset(global, 0, sizeof(*global)); |
| 1647 | global->codec = codecs; |
| 1648 | global->passes = 1; |
| 1649 | global->use_i420 = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1650 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1651 | for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) { |
| 1652 | arg.argv_step = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1653 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1654 | if (arg_match(&arg, &codecarg, argi)) { |
| 1655 | int j, k = -1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1656 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1657 | for (j = 0; j < sizeof(codecs) / sizeof(codecs[0]); j++) |
| 1658 | if (!strcmp(codecs[j].name, arg.val)) |
| 1659 | k = j; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1660 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1661 | if (k >= 0) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1662 | global->codec = codecs + k; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1663 | else |
| 1664 | die("Error: Unrecognized argument (%s) to --codec\n", |
| 1665 | arg.val); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1666 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1667 | } else if (arg_match(&arg, &passes, argi)) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1668 | global->passes = arg_parse_uint(&arg); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1669 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1670 | if (global->passes < 1 || global->passes > 2) |
| 1671 | die("Error: Invalid number of passes (%d)\n", global->passes); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1672 | } else if (arg_match(&arg, &pass_arg, argi)) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1673 | global->pass = arg_parse_uint(&arg); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1674 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1675 | 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1680 | else if (arg_match(&arg, &deadline, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1681 | global->deadline = arg_parse_uint(&arg); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1682 | else if (arg_match(&arg, &best_dl, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1683 | global->deadline = VPX_DL_BEST_QUALITY; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1684 | else if (arg_match(&arg, &good_dl, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1685 | global->deadline = VPX_DL_GOOD_QUALITY; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1686 | else if (arg_match(&arg, &rt_dl, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1687 | 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1696 | else if (arg_match(&arg, &limit, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1697 | global->limit = arg_parse_uint(&arg); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1698 | else if (arg_match(&arg, &skip, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1699 | global->skip_frames = arg_parse_uint(&arg); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1700 | else if (arg_match(&arg, &psnrarg, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1701 | global->show_psnr = 1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1702 | else if (arg_match(&arg, &recontest, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1703 | global->test_decode = 1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1704 | else if (arg_match(&arg, &framerate, argi)) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1705 | 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1710 | else if (arg_match(&arg, &debugmode, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1711 | global->debug = 1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1712 | else if (arg_match(&arg, &q_hist_n, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1713 | global->show_q_hist_buckets = arg_parse_uint(&arg); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1714 | else if (arg_match(&arg, &rate_hist_n, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1715 | global->show_rate_hist_buckets = arg_parse_uint(&arg); |
John Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 1716 | else |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1717 | argj++; |
| 1718 | } |
| 1719 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1720 | /* Validate global config */ |
| 1721 | |
| 1722 | if (global->pass) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1723 | /* DWIM: Assume the user meant passes=2 if pass=2 is specified */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1724 | 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 Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 1728 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1729 | } |
John Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1733 | void open_input_file(struct input_state *input) { |
| 1734 | unsigned int fourcc; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1735 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1736 | /* 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1745 | */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1746 | input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file); |
| 1747 | input->detect.position = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1748 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1749 | 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1774 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | |
| 1778 | static 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 Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 1782 | } |
| 1783 | |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1784 | static struct stream_state *new_stream(struct global_config *global, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1785 | struct stream_state *prev) { |
| 1786 | struct stream_state *stream; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1787 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1788 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1797 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1798 | /* 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1804 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1805 | /* 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1809 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1810 | /* 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1815 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1816 | /* 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; |
Johann | 87c40b3 | 2012-03-01 16:12:53 -0800 | [diff] [blame] | 1820 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1821 | /* Allows removal of the application version from the EBML tags */ |
| 1822 | stream->ebml.debug = global->debug; |
| 1823 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1824 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1825 | /* Output files must be specified for each stream */ |
| 1826 | stream->config.out_fn = NULL; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1827 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1828 | stream->next = NULL; |
| 1829 | return stream; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | |
| 1833 | static int parse_stream_params(struct global_config *global, |
| 1834 | struct stream_state *stream, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1835 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1842 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1843 | /* Handle codec specific options */ |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1844 | if (0) { |
| 1845 | #if CONFIG_VP8_ENCODER |
| 1846 | } else if (global->codec->iface == vpx_codec_vp8_cx) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1847 | ctrl_args = vp8_args; |
| 1848 | ctrl_args_map = vp8_arg_ctrl_map; |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1849 | #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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1855 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1856 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1857 | for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1858 | arg.argv_step = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1859 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1860 | /* 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1871 | 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 Mukherjee | dfd89f2 | 2013-01-25 11:30:28 -0800 | [diff] [blame^] | 1893 | #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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1897 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1927 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1928 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1971 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1972 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1973 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1974 | if (!match) |
| 1975 | argj++; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1976 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1977 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1978 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1979 | return eos_mark_found; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | |
| 1983 | #define FOREACH_STREAM(func)\ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1984 | do\ |
| 1985 | {\ |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1986 | struct stream_state *stream;\ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1987 | \ |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1988 | for(stream = streams; stream; stream = stream->next)\ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1989 | func;\ |
| 1990 | }while(0) |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1991 | |
| 1992 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1993 | static void validate_stream_config(struct stream_state *stream) { |
| 1994 | struct stream_state *streami; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1995 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1996 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1999 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2000 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2005 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2006 | /* 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2013 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2014 | |
| 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | |
| 2027 | static void set_stream_dimensions(struct stream_state *stream, |
| 2028 | unsigned int w, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2029 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | |
Ralph Giles | 061a16d | 2012-01-05 15:05:05 -0600 | [diff] [blame] | 2038 | static void set_default_kf_interval(struct stream_state *stream, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2039 | 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 Giles | 061a16d | 2012-01-05 15:05:05 -0600 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2051 | static void show_stream_config(struct stream_state *stream, |
| 2052 | struct global_config *global, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2053 | struct input_state *input) { |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2054 | |
| 2055 | #define SHOW(field) \ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2056 | fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field) |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2057 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2058 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2068 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2069 | 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 Mukherjee | dfd89f2 | 2013-01-25 11:30:28 -0800 | [diff] [blame^] | 2077 | #if CONFIG_VP9_ENCODER |
| 2078 | SHOW(g_frame_parallel_decoding); |
| 2079 | #endif |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2080 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | |
| 2104 | static void open_output_file(struct stream_state *stream, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2105 | struct global_config *global) { |
| 2106 | const char *fn = stream->config.out_fn; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2107 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2108 | stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2109 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2110 | if (!stream->file) |
| 2111 | fatal("Failed to open output file"); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2112 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2113 | if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR)) |
| 2114 | fatal("WebM output to pipes not supported."); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2115 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2116 | 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 Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 2120 | stream->config.stereo_fmt, |
| 2121 | global->codec->fourcc); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2122 | } else |
| 2123 | write_ivf_file_header(stream->file, &stream->config.cfg, |
| 2124 | global->codec->fourcc, 0); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | |
| 2128 | static void close_output_file(struct stream_state *stream, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2129 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2140 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2141 | fclose(stream->file); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | |
| 2145 | static void setup_pass(struct stream_state *stream, |
| 2146 | struct global_config *global, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2147 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2156 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2157 | 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 Wang | aabae97 | 2012-02-29 08:24:53 -0500 | [diff] [blame] | 2162 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2163 | stream->cx_time = 0; |
| 2164 | stream->nbytes = 0; |
| 2165 | stream->frames_out = 0; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | |
| 2169 | static void initialize_encoder(struct stream_state *stream, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2170 | struct global_config *global) { |
| 2171 | int i; |
| 2172 | int flags = 0; |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 2173 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2174 | flags |= global->show_psnr ? VPX_CODEC_USE_PSNR : 0; |
| 2175 | flags |= global->out_part ? VPX_CODEC_USE_OUTPUT_PARTITION : 0; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2176 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2177 | /* 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2181 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2182 | /* 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2192 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2193 | ctx_exit_on_error(&stream->encoder, "Failed to control codec"); |
| 2194 | } |
| 2195 | |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 2196 | #if CONFIG_DECODERS |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2197 | 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 Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 2209 | #endif |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | |
| 2213 | static void encode_frame(struct stream_state *stream, |
| 2214 | struct global_config *global, |
| 2215 | struct vpx_image *img, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2216 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2220 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2221 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2238 | static void update_quantizer_histogram(struct stream_state *stream) { |
| 2239 | if (stream->config.cfg.g_pass != VPX_RC_FIRST_PASS) { |
| 2240 | int q; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2241 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2242 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | |
| 2249 | static void get_cx_data(struct stream_state *stream, |
| 2250 | struct global_config *global, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2251 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2255 | |
Ronald S. Bultje | 88d703c | 2012-11-09 09:07:50 -0800 | [diff] [blame] | 2256 | *got_data = 0; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2257 | 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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2261 | 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 Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 2300 | |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2301 | *got_data = 1; |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 2302 | #if CONFIG_DECODERS |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2303 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2307 | } |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 2308 | #endif |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2309 | break; |
| 2310 | case VPX_CODEC_STATS_PKT: |
| 2311 | stream->frames_out++; |
John Koleszar | 16810c1 | 2012-12-21 15:20:41 -0800 | [diff] [blame] | 2312 | if (!global->quiet) |
| 2313 | fprintf(stderr, " %6luS", |
| 2314 | (unsigned long)pkt->data.twopass_stats.sz); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2315 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2338 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2339 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2343 | static void show_psnr(struct stream_state *stream) { |
| 2344 | int i; |
| 2345 | double ovpsnr; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2346 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2347 | if (!stream->psnr_count) |
| 2348 | return; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2349 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2350 | 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 Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2354 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2355 | for (i = 0; i < 4; i++) { |
| 2356 | fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count); |
| 2357 | } |
| 2358 | fprintf(stderr, "\n"); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2362 | float usec_to_fps(uint64_t usec, unsigned int frames) { |
| 2363 | return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2364 | } |
| 2365 | |
John Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 2366 | |
Ronald S. Bultje | 195172c | 2012-11-10 16:22:49 -0800 | [diff] [blame] | 2367 | static void test_decode(struct stream_state *stream) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2368 | 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 Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 2372 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2373 | if (!stream->mismatch_seen |
| 2374 | && !compare_img(&stream->ref_enc.img, &stream->ref_dec.img)) { |
| 2375 | /* TODO(jkoleszar): make fatal. */ |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 2376 | 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. Bultje | 195172c | 2012-11-10 16:22:49 -0800 | [diff] [blame] | 2383 | stream->mismatch_seen = stream->frames_out; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2384 | } |
| 2385 | } |
John Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 2386 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2387 | int main(int argc, const char **argv_) { |
| 2388 | int pass; |
| 2389 | vpx_image_t raw; |
| 2390 | int frame_avail, got_data; |
John Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 2391 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2392 | 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 Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 2398 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2399 | exec_name = argv_[0]; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 2400 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2401 | 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 Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 2420 | */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2421 | struct stream_state *stream = NULL; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2422 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2423 | 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2429 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2430 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2431 | /* 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 Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2435 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2436 | /* Handle non-option arguments */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2437 | input.fn = argv[0]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2438 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2439 | if (!input.fn) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2440 | usage_exit(); |
John Koleszar | 5329189 | 2010-10-22 14:48:21 -0400 | [diff] [blame] | 2441 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2442 | for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) { |
| 2443 | int frames_in = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2444 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2445 | open_input_file(&input); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2446 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2447 | /* 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2449 | */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2450 | 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2456 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2457 | }); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2458 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2459 | /* 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 Koleszar | 77e6b45 | 2010-11-03 13:58:40 -0400 | [diff] [blame] | 2462 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2463 | /* 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 Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 2472 | |
John Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 2473 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2474 | /* 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. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 2479 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2480 | 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2488 | /*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 Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2493 | vpx_img_alloc(&raw, |
| 2494 | input.use_i420 ? VPX_IMG_FMT_I420 |
| 2495 | : VPX_IMG_FMT_YV12, |
| 2496 | input.w, input.h, 32); |
John Koleszar | 05239f0 | 2011-06-29 09:53:12 -0400 | [diff] [blame] | 2497 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2498 | FOREACH_STREAM(init_rate_histogram(&stream->rate_hist, |
| 2499 | &stream->config.cfg, |
| 2500 | &global.framerate)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2501 | } |
| 2502 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2503 | FOREACH_STREAM(open_output_file(stream, &global)); |
| 2504 | FOREACH_STREAM(setup_pass(stream, &global, pass)); |
| 2505 | FOREACH_STREAM(initialize_encoder(stream, &global)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2506 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2507 | frame_avail = 1; |
| 2508 | got_data = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2509 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2510 | while (frame_avail || got_data) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2511 | struct vpx_usec_timer timer; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2512 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2513 | if (!global.limit || frames_in < global.limit) { |
| 2514 | frame_avail = read_frame(&input, &raw); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2515 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2516 | if (frame_avail) |
| 2517 | frames_in++; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2518 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2519 | 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 Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 2532 | } |
| 2533 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2534 | } else |
| 2535 | frame_avail = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2536 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2537 | 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 Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2544 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2545 | FOREACH_STREAM(update_quantizer_histogram(stream)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2546 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2547 | got_data = 0; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2548 | FOREACH_STREAM(get_cx_data(stream, &global, &got_data)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2549 | |
Yaowu Xu | 606c647 | 2012-11-08 16:22:54 -0800 | [diff] [blame] | 2550 | if (got_data && global.test_decode) |
Ronald S. Bultje | 195172c | 2012-11-10 16:22:49 -0800 | [diff] [blame] | 2551 | FOREACH_STREAM(test_decode(stream)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2552 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2553 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2554 | fflush(stdout); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2555 | } |
| 2556 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2557 | if (stream_cnt > 1) |
| 2558 | fprintf(stderr, "\n"); |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 2559 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2560 | 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 Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 2575 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2576 | if (global.show_psnr) |
| 2577 | FOREACH_STREAM(show_psnr(stream)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2578 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2579 | 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 Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 2585 | } |
| 2586 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2587 | close_input_file(&input); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2588 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2589 | FOREACH_STREAM(close_output_file(stream, global.codec->fourcc)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2590 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2591 | FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2592 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2593 | if (global.pass) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2594 | break; |
| 2595 | } |
| 2596 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2597 | 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. Bultje | 3121497 | 2012-10-11 10:13:48 -0700 | [diff] [blame] | 2607 | #if CONFIG_INTERNAL_STATS |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2608 | /* 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. Bultje | 3121497 | 2012-10-11 10:13:48 -0700 | [diff] [blame] | 2612 | FILE *f = fopen("opsnr.stt", "a"); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2613 | if (stream->mismatch_seen) { |
Ronald S. Bultje | 3121497 | 2012-10-11 10:13:48 -0700 | [diff] [blame] | 2614 | fprintf(f, "First mismatch occurred in frame %d\n", |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2615 | stream->mismatch_seen); |
| 2616 | } else { |
| 2617 | fprintf(f, "No mismatch detected in recon buffers\n"); |
Ronald S. Bultje | 3121497 | 2012-10-11 10:13:48 -0700 | [diff] [blame] | 2618 | } |
Ronald S. Bultje | 3121497 | 2012-10-11 10:13:48 -0700 | [diff] [blame] | 2619 | fclose(f); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2620 | }); |
Ronald S. Bultje | 3121497 | 2012-10-11 10:13:48 -0700 | [diff] [blame] | 2621 | #endif |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2622 | |
| 2623 | vpx_img_free(&raw); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2624 | free(argv); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2625 | free(streams); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2626 | return EXIT_SUCCESS; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2627 | } |