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 | |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 11 | #include "./vpxenc.h" |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 12 | #include "./vpx_config.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 13 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 14 | #include <assert.h> |
| 15 | #include <limits.h> |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 16 | #include <math.h> |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 17 | #include <stdarg.h> |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 20 | #include <string.h> |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 21 | |
John Koleszar | b749234 | 2010-05-24 11:39:59 -0400 | [diff] [blame] | 22 | #include "vpx/vpx_encoder.h" |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 23 | #if CONFIG_DECODERS |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 24 | #include "vpx/vpx_decoder.h" |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 25 | #endif |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 26 | |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 27 | #include "third_party/libyuv/include/libyuv/scale.h" |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 28 | #include "./args.h" |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 29 | #include "./ivfenc.h" |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 30 | |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 31 | #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER |
John Koleszar | b749234 | 2010-05-24 11:39:59 -0400 | [diff] [blame] | 32 | #include "vpx/vp8cx.h" |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 33 | #endif |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 34 | #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 35 | #include "vpx/vp8dx.h" |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 36 | #endif |
| 37 | |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 38 | #include "./tools_common.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 39 | #include "vpx_ports/mem_ops.h" |
| 40 | #include "vpx_ports/vpx_timer.h" |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 41 | #include "./vpxstats.h" |
Tom Finegan | 249366b | 2013-11-25 12:05:19 -0800 | [diff] [blame] | 42 | #include "./warnings.h" |
Tom Finegan | 78cb2e6 | 2013-11-07 21:28:45 -0800 | [diff] [blame] | 43 | #include "./webmenc.h" |
| 44 | #include "./y4minput.h" |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 45 | |
John Koleszar | 6291dd4 | 2012-06-19 21:05:36 -0700 | [diff] [blame] | 46 | /* Swallow warnings about unused results of fread/fwrite */ |
| 47 | 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] | 48 | FILE *stream) { |
| 49 | return fread(ptr, size, nmemb, stream); |
John Koleszar | 6291dd4 | 2012-06-19 21:05:36 -0700 | [diff] [blame] | 50 | } |
| 51 | #define fread wrap_fread |
| 52 | |
| 53 | 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] | 54 | FILE *stream) { |
| 55 | return fwrite(ptr, size, nmemb, stream); |
John Koleszar | 6291dd4 | 2012-06-19 21:05:36 -0700 | [diff] [blame] | 56 | } |
| 57 | #define fwrite wrap_fwrite |
| 58 | |
| 59 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 60 | static const char *exec_name; |
| 61 | |
Jim Bankoski | 1b16e74 | 2012-07-23 12:32:59 -0700 | [diff] [blame] | 62 | static const struct codec_item { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 63 | char const *name; |
Jim Bankoski | 1b16e74 | 2012-07-23 12:32:59 -0700 | [diff] [blame] | 64 | const vpx_codec_iface_t *(*iface)(void); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 65 | const vpx_codec_iface_t *(*dx_iface)(void); |
Jim Bankoski | 1b16e74 | 2012-07-23 12:32:59 -0700 | [diff] [blame] | 66 | unsigned int fourcc; |
Jim Bankoski | 1b16e74 | 2012-07-23 12:32:59 -0700 | [diff] [blame] | 67 | } codecs[] = { |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 68 | #if CONFIG_VP8_ENCODER && CONFIG_VP8_DECODER |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 69 | {"vp8", &vpx_codec_vp8_cx, &vpx_codec_vp8_dx, VP8_FOURCC}, |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 70 | #elif CONFIG_VP8_ENCODER && !CONFIG_VP8_DECODER |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 71 | {"vp8", &vpx_codec_vp8_cx, NULL, VP8_FOURCC}, |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 72 | #endif |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 73 | #if CONFIG_VP9_ENCODER && CONFIG_VP9_DECODER |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 74 | {"vp9", &vpx_codec_vp9_cx, &vpx_codec_vp9_dx, VP9_FOURCC}, |
John Koleszar | 7b8dfcb | 2012-11-06 16:59:01 -0800 | [diff] [blame] | 75 | #elif CONFIG_VP9_ENCODER && !CONFIG_VP9_DECODER |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 76 | {"vp9", &vpx_codec_vp9_cx, NULL, VP9_FOURCC}, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 77 | #endif |
| 78 | }; |
| 79 | |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 80 | static void warn_or_exit_on_errorv(vpx_codec_ctx_t *ctx, int fatal, |
| 81 | const char *s, va_list ap) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 82 | if (ctx->err) { |
| 83 | const char *detail = vpx_codec_error_detail(ctx); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 84 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 85 | vfprintf(stderr, s, ap); |
| 86 | fprintf(stderr, ": %s\n", vpx_codec_error(ctx)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 87 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 88 | if (detail) |
| 89 | fprintf(stderr, " %s\n", detail); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 90 | |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 91 | if (fatal) |
| 92 | exit(EXIT_FAILURE); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 93 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 96 | static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s, ...) { |
| 97 | va_list ap; |
| 98 | |
| 99 | va_start(ap, s); |
| 100 | warn_or_exit_on_errorv(ctx, 1, s, ap); |
| 101 | va_end(ap); |
| 102 | } |
| 103 | |
| 104 | static void warn_or_exit_on_error(vpx_codec_ctx_t *ctx, int fatal, |
| 105 | const char *s, ...) { |
| 106 | va_list ap; |
| 107 | |
| 108 | va_start(ap, s); |
| 109 | warn_or_exit_on_errorv(ctx, fatal, s, ap); |
| 110 | va_end(ap); |
| 111 | } |
| 112 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 113 | int read_frame(struct VpxInputContext *input_ctx, vpx_image_t *img) { |
| 114 | FILE *f = input_ctx->file; |
| 115 | y4m_input *y4m = &input_ctx->y4m; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 116 | int shortread = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 117 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 118 | if (input_ctx->file_type == FILE_TYPE_Y4M) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 119 | if (y4m_input_fetch_frame(y4m, f, img) < 1) |
| 120 | return 0; |
| 121 | } else { |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 122 | shortread = read_yuv_frame(input_ctx, img); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return !shortread; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Alex Converse | 64b89f1 | 2014-01-06 16:29:09 -0800 | [diff] [blame] | 128 | int file_is_y4m(const char detect[4]) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 129 | if (memcmp(detect, "YUV4", 4) == 0) { |
| 130 | return 1; |
| 131 | } |
| 132 | return 0; |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Alex Converse | 64b89f1 | 2014-01-06 16:29:09 -0800 | [diff] [blame] | 135 | int fourcc_is_ivf(const char detect[4]) { |
| 136 | if (memcmp(detect, "DKIF", 4) == 0) { |
| 137 | return 1; |
| 138 | } |
| 139 | return 0; |
| 140 | } |
John Koleszar | dc66630 | 2010-10-20 12:05:48 -0400 | [diff] [blame] | 141 | |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 142 | /* Murmur hash derived from public domain reference implementation at |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 143 | * http:// sites.google.com/site/murmurhash/ |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 144 | */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 145 | static unsigned int murmur(const void *key, int len, unsigned int seed) { |
| 146 | const unsigned int m = 0x5bd1e995; |
| 147 | const int r = 24; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 148 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 149 | unsigned int h = seed ^ len; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 150 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 151 | const unsigned char *data = (const unsigned char *)key; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 152 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 153 | while (len >= 4) { |
| 154 | unsigned int k; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 155 | |
Yaowu Xu | 014acfa | 2013-09-17 16:31:46 -0700 | [diff] [blame] | 156 | k = (unsigned int)data[0]; |
| 157 | k |= (unsigned int)data[1] << 8; |
| 158 | k |= (unsigned int)data[2] << 16; |
| 159 | k |= (unsigned int)data[3] << 24; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 160 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 161 | k *= m; |
| 162 | k ^= k >> r; |
| 163 | k *= m; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 164 | |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 165 | h *= m; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 166 | h ^= k; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 167 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 168 | data += 4; |
| 169 | len -= 4; |
| 170 | } |
| 171 | |
| 172 | switch (len) { |
| 173 | case 3: |
| 174 | h ^= data[2] << 16; |
| 175 | case 2: |
| 176 | h ^= data[1] << 8; |
| 177 | case 1: |
| 178 | h ^= data[0]; |
| 179 | h *= m; |
| 180 | }; |
| 181 | |
| 182 | h ^= h >> 13; |
| 183 | h *= m; |
| 184 | h ^= h >> 15; |
| 185 | |
| 186 | return h; |
John Koleszar | c7c0c05 | 2010-10-27 10:05:55 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
John Koleszar | 75afcee | 2010-10-26 16:22:22 -0400 | [diff] [blame] | 190 | static const arg_def_t debugmode = ARG_DEF("D", "debug", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 191 | "Debug mode (makes output deterministic)"); |
John Koleszar | 5329189 | 2010-10-22 14:48:21 -0400 | [diff] [blame] | 192 | static const arg_def_t outputfile = ARG_DEF("o", "output", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 193 | "Output filename"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 194 | static const arg_def_t use_yv12 = ARG_DEF(NULL, "yv12", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 195 | "Input file is YV12 "); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 196 | static const arg_def_t use_i420 = ARG_DEF(NULL, "i420", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 197 | "Input file is I420 (default)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 198 | static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 199 | "Codec to use"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 200 | static const arg_def_t passes = ARG_DEF("p", "passes", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 201 | "Number of passes (1/2)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 202 | static const arg_def_t pass_arg = ARG_DEF(NULL, "pass", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 203 | "Pass to execute (1/2)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 204 | static const arg_def_t fpf_name = ARG_DEF(NULL, "fpf", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 205 | "First pass statistics file name"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 206 | static const arg_def_t limit = ARG_DEF(NULL, "limit", 1, |
| 207 | "Stop encoding after n input frames"); |
Yaowu Xu | 03d5d69 | 2012-03-14 07:37:10 -0700 | [diff] [blame] | 208 | static const arg_def_t skip = ARG_DEF(NULL, "skip", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 209 | "Skip the first n input frames"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 210 | static const arg_def_t deadline = ARG_DEF("d", "deadline", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 211 | "Deadline per frame (usec)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 212 | static const arg_def_t best_dl = ARG_DEF(NULL, "best", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 213 | "Use Best Quality Deadline"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 214 | static const arg_def_t good_dl = ARG_DEF(NULL, "good", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 215 | "Use Good Quality Deadline"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 216 | static const arg_def_t rt_dl = ARG_DEF(NULL, "rt", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 217 | "Use Realtime Quality Deadline"); |
James Zern | 1fc3cc8 | 2012-10-25 18:31:35 -0700 | [diff] [blame] | 218 | static const arg_def_t quietarg = ARG_DEF("q", "quiet", 0, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 219 | "Do not print encode progress"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 220 | static const arg_def_t verbosearg = ARG_DEF("v", "verbose", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 221 | "Show encoder parameters"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 222 | static const arg_def_t psnrarg = ARG_DEF(NULL, "psnr", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 223 | "Show PSNR in status line"); |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 224 | |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 225 | static const struct arg_enum_list test_decode_enum[] = { |
| 226 | {"off", TEST_DECODE_OFF}, |
| 227 | {"fatal", TEST_DECODE_FATAL}, |
| 228 | {"warn", TEST_DECODE_WARN}, |
| 229 | {NULL, 0} |
| 230 | }; |
| 231 | static const arg_def_t recontest = ARG_DEF_ENUM(NULL, "test-decode", 1, |
| 232 | "Test encode/decode mismatch", |
| 233 | test_decode_enum); |
John Koleszar | 28f177c | 2010-10-21 16:52:14 -0400 | [diff] [blame] | 234 | static const arg_def_t framerate = ARG_DEF(NULL, "fps", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 235 | "Stream frame rate (rate/scale)"); |
John Koleszar | 456bfb1 | 2010-10-21 16:23:20 -0400 | [diff] [blame] | 236 | static const arg_def_t use_ivf = ARG_DEF(NULL, "ivf", 0, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 237 | "Output IVF (default is WebM)"); |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 238 | 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] | 239 | "Makes encoder output partitions. Requires IVF output!"); |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 240 | 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] | 241 | "Show quantizer histogram (n-buckets)"); |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 242 | 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] | 243 | "Show rate histogram (n-buckets)"); |
Tom Finegan | 44dd327 | 2013-11-20 17:18:28 -0800 | [diff] [blame] | 244 | static const arg_def_t disable_warnings = |
| 245 | ARG_DEF(NULL, "disable-warnings", 0, |
| 246 | "Disable warnings about potentially incorrect encode settings."); |
Tom Finegan | 249366b | 2013-11-25 12:05:19 -0800 | [diff] [blame] | 247 | static const arg_def_t disable_warning_prompt = |
| 248 | ARG_DEF("y", "disable-warning-prompt", 0, |
| 249 | "Display warnings, but do not prompt user to continue."); |
Tom Finegan | 44dd327 | 2013-11-20 17:18:28 -0800 | [diff] [blame] | 250 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 251 | static const arg_def_t *main_args[] = { |
| 252 | &debugmode, |
| 253 | &outputfile, &codecarg, &passes, &pass_arg, &fpf_name, &limit, &skip, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 254 | &deadline, &best_dl, &good_dl, &rt_dl, |
Tom Finegan | 44dd327 | 2013-11-20 17:18:28 -0800 | [diff] [blame] | 255 | &quietarg, &verbosearg, &psnrarg, &use_ivf, &out_part, &q_hist_n, |
Tom Finegan | 249366b | 2013-11-25 12:05:19 -0800 | [diff] [blame] | 256 | &rate_hist_n, &disable_warnings, &disable_warning_prompt, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 257 | NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | static const arg_def_t usage = ARG_DEF("u", "usage", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 261 | "Usage profile number to use"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 262 | static const arg_def_t threads = ARG_DEF("t", "threads", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 263 | "Max number of threads to use"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 264 | static const arg_def_t profile = ARG_DEF(NULL, "profile", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 265 | "Bitstream profile number to use"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 266 | static const arg_def_t width = ARG_DEF("w", "width", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 267 | "Frame width"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 268 | static const arg_def_t height = ARG_DEF("h", "height", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 269 | "Frame height"); |
Alok Ahuja | 72c76ca | 2011-04-21 00:50:07 -0700 | [diff] [blame] | 270 | static const struct arg_enum_list stereo_mode_enum[] = { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 271 | {"mono", STEREO_FORMAT_MONO}, |
| 272 | {"left-right", STEREO_FORMAT_LEFT_RIGHT}, |
| 273 | {"bottom-top", STEREO_FORMAT_BOTTOM_TOP}, |
| 274 | {"top-bottom", STEREO_FORMAT_TOP_BOTTOM}, |
| 275 | {"right-left", STEREO_FORMAT_RIGHT_LEFT}, |
| 276 | {NULL, 0} |
Alok Ahuja | 72c76ca | 2011-04-21 00:50:07 -0700 | [diff] [blame] | 277 | }; |
| 278 | 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] | 279 | "Stereo 3D video format", stereo_mode_enum); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 280 | static const arg_def_t timebase = ARG_DEF(NULL, "timebase", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 281 | "Output timestamp precision (fractional seconds)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 282 | 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] | 283 | "Enable error resiliency features"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 284 | 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] | 285 | "Max number of frames to lag"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 286 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 287 | static const arg_def_t *global_args[] = { |
| 288 | &use_yv12, &use_i420, &usage, &threads, &profile, |
Deb Mukherjee | dfd89f2 | 2013-01-25 11:30:28 -0800 | [diff] [blame] | 289 | &width, &height, &stereo_mode, &timebase, &framerate, |
| 290 | &error_resilient, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 291 | &lag_in_frames, NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | 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] | 295 | "Temporal resampling threshold (buf %)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 296 | 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] | 297 | "Spatial resampling enabled (bool)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 298 | 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] | 299 | "Upscale threshold (buf %)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 300 | 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] | 301 | "Downscale threshold (buf %)"); |
John Koleszar | 500fec2 | 2011-03-21 07:50:42 -0400 | [diff] [blame] | 302 | static const struct arg_enum_list end_usage_enum[] = { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 303 | {"vbr", VPX_VBR}, |
| 304 | {"cbr", VPX_CBR}, |
| 305 | {"cq", VPX_CQ}, |
Deb Mukherjee | e378a89 | 2013-08-29 16:21:44 -0700 | [diff] [blame] | 306 | {"q", VPX_Q}, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 307 | {NULL, 0} |
John Koleszar | 500fec2 | 2011-03-21 07:50:42 -0400 | [diff] [blame] | 308 | }; |
| 309 | 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] | 310 | "Rate control mode", end_usage_enum); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 311 | 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] | 312 | "Bitrate (kbps)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 313 | 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] | 314 | "Minimum (best) quantizer"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 315 | 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] | 316 | "Maximum (worst) quantizer"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 317 | 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] | 318 | "Datarate undershoot (min) target (%)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 319 | 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] | 320 | "Datarate overshoot (max) target (%)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 321 | 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] | 322 | "Client buffer size (ms)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 323 | 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] | 324 | "Client initial buffer size (ms)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 325 | 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] | 326 | "Client optimal buffer size (ms)"); |
| 327 | static const arg_def_t *rc_args[] = { |
| 328 | &dropframe_thresh, &resize_allowed, &resize_up_thresh, &resize_down_thresh, |
| 329 | &end_usage, &target_bitrate, &min_quantizer, &max_quantizer, |
| 330 | &undershoot_pct, &overshoot_pct, &buf_sz, &buf_initial_sz, &buf_optimal_sz, |
| 331 | NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | |
| 335 | 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] | 336 | "CBR/VBR bias (0=CBR, 100=VBR)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 337 | 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] | 338 | "GOP min bitrate (% of target)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 339 | 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] | 340 | "GOP max bitrate (% of target)"); |
| 341 | static const arg_def_t *rc_twopass_args[] = { |
| 342 | &bias_pct, &minsection_pct, &maxsection_pct, NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | |
| 346 | 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] | 347 | "Minimum keyframe interval (frames)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 348 | 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] | 349 | "Maximum keyframe interval (frames)"); |
John Koleszar | b1e36f2 | 2010-06-24 08:41:51 -0400 | [diff] [blame] | 350 | 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] | 351 | "Disable keyframe placement"); |
| 352 | static const arg_def_t *kf_args[] = { |
| 353 | &kf_min_dist, &kf_max_dist, &kf_disabled, NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 357 | 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] | 358 | "Noise sensitivity (frames to blur)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 359 | static const arg_def_t sharpness = ARG_DEF(NULL, "sharpness", 1, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 360 | "Filter sharpness (0-7)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 361 | 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] | 362 | "Motion detection threshold"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 363 | 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] | 364 | "CPU Used (-16..16)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 365 | 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] | 366 | "Enable automatic alt reference frames"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 367 | 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] | 368 | "AltRef Max Frames"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 369 | 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] | 370 | "AltRef Strength"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 371 | 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] | 372 | "AltRef Type"); |
John Koleszar | b0da9b3 | 2010-12-17 09:43:39 -0500 | [diff] [blame] | 373 | static const struct arg_enum_list tuning_enum[] = { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 374 | {"psnr", VP8_TUNE_PSNR}, |
| 375 | {"ssim", VP8_TUNE_SSIM}, |
| 376 | {NULL, 0} |
John Koleszar | b0da9b3 | 2010-12-17 09:43:39 -0500 | [diff] [blame] | 377 | }; |
| 378 | 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] | 379 | "Material to favor", tuning_enum); |
Paul Wilkins | e0846c9 | 2011-01-07 18:29:37 +0000 | [diff] [blame] | 380 | static const arg_def_t cq_level = ARG_DEF(NULL, "cq-level", 1, |
Deb Mukherjee | e378a89 | 2013-08-29 16:21:44 -0700 | [diff] [blame] | 381 | "Constant/Constrained Quality level"); |
John Koleszar | 1654ae9 | 2011-07-28 09:17:32 -0400 | [diff] [blame] | 382 | 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] | 383 | "Max I-frame bitrate (pct)"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 384 | |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 385 | #if CONFIG_VP8_ENCODER |
James Zern | 5248d71 | 2014-01-14 17:56:45 -0800 | [diff] [blame] | 386 | static const arg_def_t token_parts = |
| 387 | ARG_DEF(NULL, "token-parts", 1, "Number of token partitions to use, log2"); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 388 | static const arg_def_t *vp8_args[] = { |
| 389 | &cpu_used, &auto_altref, &noise_sens, &sharpness, &static_thresh, |
| 390 | &token_parts, &arnr_maxframes, &arnr_strength, &arnr_type, |
John Koleszar | 83b1d90 | 2012-11-05 12:37:14 -0800 | [diff] [blame] | 391 | &tune_ssim, &cq_level, &max_intra_rate_pct, |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 392 | NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 393 | }; |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 394 | static const int vp8_arg_ctrl_map[] = { |
| 395 | VP8E_SET_CPUUSED, VP8E_SET_ENABLEAUTOALTREF, |
| 396 | VP8E_SET_NOISE_SENSITIVITY, VP8E_SET_SHARPNESS, VP8E_SET_STATIC_THRESHOLD, |
| 397 | VP8E_SET_TOKEN_PARTITIONS, |
| 398 | VP8E_SET_ARNR_MAXFRAMES, VP8E_SET_ARNR_STRENGTH, VP8E_SET_ARNR_TYPE, |
| 399 | VP8E_SET_TUNING, VP8E_SET_CQ_LEVEL, VP8E_SET_MAX_INTRA_BITRATE_PCT, |
| 400 | 0 |
| 401 | }; |
| 402 | #endif |
| 403 | |
| 404 | #if CONFIG_VP9_ENCODER |
James Zern | 5248d71 | 2014-01-14 17:56:45 -0800 | [diff] [blame] | 405 | static const arg_def_t tile_cols = |
| 406 | ARG_DEF(NULL, "tile-columns", 1, "Number of tile columns to use, log2"); |
| 407 | static const arg_def_t tile_rows = |
| 408 | ARG_DEF(NULL, "tile-rows", 1, "Number of tile rows to use, log2"); |
| 409 | static const arg_def_t lossless = ARG_DEF(NULL, "lossless", 1, "Lossless mode"); |
| 410 | static const arg_def_t frame_parallel_decoding = ARG_DEF( |
| 411 | NULL, "frame-parallel", 1, "Enable frame parallel decodability features"); |
| 412 | static const arg_def_t aq_mode = ARG_DEF( |
| 413 | NULL, "aq-mode", 1, |
| 414 | "Adaptive q mode (0: off (by default), 1: variance 2: complexity)"); |
| 415 | |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 416 | static const arg_def_t *vp9_args[] = { |
| 417 | &cpu_used, &auto_altref, &noise_sens, &sharpness, &static_thresh, |
Ronald S. Bultje | 89a206e | 2013-02-08 11:33:11 -0800 | [diff] [blame] | 418 | &tile_cols, &tile_rows, &arnr_maxframes, &arnr_strength, &arnr_type, |
Yaowu Xu | d262e26 | 2013-02-20 07:27:35 -0800 | [diff] [blame] | 419 | &tune_ssim, &cq_level, &max_intra_rate_pct, &lossless, |
Guillaume Martres | 1708465 | 2013-11-14 19:23:57 +0100 | [diff] [blame] | 420 | &frame_parallel_decoding, &aq_mode, |
John Koleszar | 83b1d90 | 2012-11-05 12:37:14 -0800 | [diff] [blame] | 421 | NULL |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 422 | }; |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 423 | static const int vp9_arg_ctrl_map[] = { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 424 | VP8E_SET_CPUUSED, VP8E_SET_ENABLEAUTOALTREF, |
| 425 | VP8E_SET_NOISE_SENSITIVITY, VP8E_SET_SHARPNESS, VP8E_SET_STATIC_THRESHOLD, |
Ronald S. Bultje | 89a206e | 2013-02-08 11:33:11 -0800 | [diff] [blame] | 426 | VP9E_SET_TILE_COLUMNS, VP9E_SET_TILE_ROWS, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 427 | VP8E_SET_ARNR_MAXFRAMES, VP8E_SET_ARNR_STRENGTH, VP8E_SET_ARNR_TYPE, |
John Koleszar | 83b1d90 | 2012-11-05 12:37:14 -0800 | [diff] [blame] | 428 | VP8E_SET_TUNING, VP8E_SET_CQ_LEVEL, VP8E_SET_MAX_INTRA_BITRATE_PCT, |
Guillaume Martres | 1708465 | 2013-11-14 19:23:57 +0100 | [diff] [blame] | 429 | VP9E_SET_LOSSLESS, VP9E_SET_FRAME_PARALLEL_DECODING, VP9E_SET_AQ_MODE, |
John Koleszar | 83b1d90 | 2012-11-05 12:37:14 -0800 | [diff] [blame] | 430 | 0 |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 431 | }; |
| 432 | #endif |
| 433 | |
| 434 | static const arg_def_t *no_args[] = { NULL }; |
| 435 | |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 436 | void usage_exit() { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 437 | int i; |
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 | fprintf(stderr, "Usage: %s <options> -o dst_filename src_filename \n", |
| 440 | exec_name); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 441 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 442 | fprintf(stderr, "\nOptions:\n"); |
John Koleszar | b9e2761 | 2013-06-25 09:15:07 -0700 | [diff] [blame] | 443 | arg_show_usage(stderr, main_args); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 444 | fprintf(stderr, "\nEncoder Global Options:\n"); |
John Koleszar | b9e2761 | 2013-06-25 09:15:07 -0700 | [diff] [blame] | 445 | arg_show_usage(stderr, global_args); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 446 | fprintf(stderr, "\nRate Control Options:\n"); |
John Koleszar | b9e2761 | 2013-06-25 09:15:07 -0700 | [diff] [blame] | 447 | arg_show_usage(stderr, rc_args); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 448 | fprintf(stderr, "\nTwopass Rate Control Options:\n"); |
John Koleszar | b9e2761 | 2013-06-25 09:15:07 -0700 | [diff] [blame] | 449 | arg_show_usage(stderr, rc_twopass_args); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 450 | fprintf(stderr, "\nKeyframe Placement Options:\n"); |
John Koleszar | b9e2761 | 2013-06-25 09:15:07 -0700 | [diff] [blame] | 451 | arg_show_usage(stderr, kf_args); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 452 | #if CONFIG_VP8_ENCODER |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 453 | fprintf(stderr, "\nVP8 Specific Options:\n"); |
John Koleszar | b9e2761 | 2013-06-25 09:15:07 -0700 | [diff] [blame] | 454 | arg_show_usage(stderr, vp8_args); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 455 | #endif |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 456 | #if CONFIG_VP9_ENCODER |
| 457 | fprintf(stderr, "\nVP9 Specific Options:\n"); |
John Koleszar | b9e2761 | 2013-06-25 09:15:07 -0700 | [diff] [blame] | 458 | arg_show_usage(stderr, vp9_args); |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 459 | #endif |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 460 | fprintf(stderr, "\nStream timebase (--timebase):\n" |
| 461 | " The desired precision of timestamps in the output, expressed\n" |
| 462 | " in fractional seconds. Default is 1/1000.\n"); |
| 463 | fprintf(stderr, "\n" |
| 464 | "Included encoders:\n" |
| 465 | "\n"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 466 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 467 | for (i = 0; i < sizeof(codecs) / sizeof(codecs[0]); i++) |
| 468 | fprintf(stderr, " %-6s - %s\n", |
| 469 | codecs[i].name, |
Jim Bankoski | 1b16e74 | 2012-07-23 12:32:59 -0700 | [diff] [blame] | 470 | vpx_codec_iface_name(codecs[i].iface())); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 471 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 472 | exit(EXIT_FAILURE); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 473 | } |
| 474 | |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 475 | |
| 476 | #define HIST_BAR_MAX 40 |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 477 | struct hist_bucket { |
| 478 | int low, high, count; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 479 | }; |
| 480 | |
| 481 | |
| 482 | static int merge_hist_buckets(struct hist_bucket *bucket, |
| 483 | int *buckets_, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 484 | int max_buckets) { |
| 485 | int small_bucket = 0, merge_bucket = INT_MAX, big_bucket = 0; |
| 486 | int buckets = *buckets_; |
| 487 | int i; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 488 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 489 | /* Find the extrema for this list of buckets */ |
| 490 | big_bucket = small_bucket = 0; |
| 491 | for (i = 0; i < buckets; i++) { |
| 492 | if (bucket[i].count < bucket[small_bucket].count) |
| 493 | small_bucket = i; |
| 494 | if (bucket[i].count > bucket[big_bucket].count) |
| 495 | big_bucket = i; |
| 496 | } |
| 497 | |
| 498 | /* If we have too many buckets, merge the smallest with an adjacent |
| 499 | * bucket. |
| 500 | */ |
| 501 | while (buckets > max_buckets) { |
| 502 | int last_bucket = buckets - 1; |
| 503 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 504 | /* merge the small bucket with an adjacent one. */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 505 | if (small_bucket == 0) |
| 506 | merge_bucket = 1; |
| 507 | else if (small_bucket == last_bucket) |
| 508 | merge_bucket = last_bucket - 1; |
| 509 | else if (bucket[small_bucket - 1].count < bucket[small_bucket + 1].count) |
| 510 | merge_bucket = small_bucket - 1; |
| 511 | else |
| 512 | merge_bucket = small_bucket + 1; |
| 513 | |
| 514 | assert(abs(merge_bucket - small_bucket) <= 1); |
| 515 | assert(small_bucket < buckets); |
| 516 | assert(big_bucket < buckets); |
| 517 | assert(merge_bucket < buckets); |
| 518 | |
| 519 | if (merge_bucket < small_bucket) { |
| 520 | bucket[merge_bucket].high = bucket[small_bucket].high; |
| 521 | bucket[merge_bucket].count += bucket[small_bucket].count; |
| 522 | } else { |
| 523 | bucket[small_bucket].high = bucket[merge_bucket].high; |
| 524 | bucket[small_bucket].count += bucket[merge_bucket].count; |
| 525 | merge_bucket = small_bucket; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 526 | } |
| 527 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 528 | assert(bucket[merge_bucket].low != bucket[merge_bucket].high); |
| 529 | |
| 530 | buckets--; |
| 531 | |
| 532 | /* Remove the merge_bucket from the list, and find the new small |
| 533 | * and big buckets while we're at it |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 534 | */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 535 | big_bucket = small_bucket = 0; |
| 536 | for (i = 0; i < buckets; i++) { |
| 537 | if (i > merge_bucket) |
| 538 | bucket[i] = bucket[i + 1]; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 539 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 540 | if (bucket[i].count < bucket[small_bucket].count) |
| 541 | small_bucket = i; |
| 542 | if (bucket[i].count > bucket[big_bucket].count) |
| 543 | big_bucket = i; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 544 | } |
| 545 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | *buckets_ = buckets; |
| 549 | return bucket[big_bucket].count; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | |
| 553 | static void show_histogram(const struct hist_bucket *bucket, |
| 554 | int buckets, |
| 555 | int total, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 556 | int scale) { |
| 557 | const char *pat1, *pat2; |
| 558 | int i; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 559 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 560 | switch ((int)(log(bucket[buckets - 1].high) / log(10)) + 1) { |
| 561 | case 1: |
| 562 | case 2: |
| 563 | pat1 = "%4d %2s: "; |
| 564 | pat2 = "%4d-%2d: "; |
| 565 | break; |
| 566 | case 3: |
| 567 | pat1 = "%5d %3s: "; |
| 568 | pat2 = "%5d-%3d: "; |
| 569 | break; |
| 570 | case 4: |
| 571 | pat1 = "%6d %4s: "; |
| 572 | pat2 = "%6d-%4d: "; |
| 573 | break; |
| 574 | case 5: |
| 575 | pat1 = "%7d %5s: "; |
| 576 | pat2 = "%7d-%5d: "; |
| 577 | break; |
| 578 | case 6: |
| 579 | pat1 = "%8d %6s: "; |
| 580 | pat2 = "%8d-%6d: "; |
| 581 | break; |
| 582 | case 7: |
| 583 | pat1 = "%9d %7s: "; |
| 584 | pat2 = "%9d-%7d: "; |
| 585 | break; |
| 586 | default: |
| 587 | pat1 = "%12d %10s: "; |
| 588 | pat2 = "%12d-%10d: "; |
| 589 | break; |
| 590 | } |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 591 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 592 | for (i = 0; i < buckets; i++) { |
| 593 | int len; |
| 594 | int j; |
| 595 | float pct; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 596 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 597 | pct = (float)(100.0 * bucket[i].count / total); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 598 | len = HIST_BAR_MAX * bucket[i].count / scale; |
| 599 | if (len < 1) |
| 600 | len = 1; |
| 601 | assert(len <= HIST_BAR_MAX); |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 602 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 603 | if (bucket[i].low == bucket[i].high) |
| 604 | fprintf(stderr, pat1, bucket[i].low, ""); |
| 605 | else |
| 606 | fprintf(stderr, pat2, bucket[i].low, bucket[i].high); |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 607 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 608 | for (j = 0; j < HIST_BAR_MAX; j++) |
| 609 | fprintf(stderr, j < len ? "=" : " "); |
| 610 | fprintf(stderr, "\t%5d (%6.2f%%)\n", bucket[i].count, pct); |
| 611 | } |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 615 | static void show_q_histogram(const int counts[64], int max_buckets) { |
| 616 | struct hist_bucket bucket[64]; |
| 617 | int buckets = 0; |
| 618 | int total = 0; |
| 619 | int scale; |
| 620 | int i; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 621 | |
| 622 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 623 | for (i = 0; i < 64; i++) { |
| 624 | if (counts[i]) { |
| 625 | bucket[buckets].low = bucket[buckets].high = i; |
| 626 | bucket[buckets].count = counts[i]; |
| 627 | buckets++; |
| 628 | total += counts[i]; |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 629 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 630 | } |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 631 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 632 | fprintf(stderr, "\nQuantizer Selection:\n"); |
| 633 | scale = merge_hist_buckets(bucket, &buckets, max_buckets); |
| 634 | show_histogram(bucket, buckets, total, scale); |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 638 | #define RATE_BINS (100) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 639 | struct rate_hist { |
| 640 | int64_t *pts; |
| 641 | int *sz; |
| 642 | int samples; |
| 643 | int frames; |
| 644 | struct hist_bucket bucket[RATE_BINS]; |
| 645 | int total; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 646 | }; |
| 647 | |
| 648 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 649 | static void init_rate_histogram(struct rate_hist *hist, |
John Koleszar | 05239f0 | 2011-06-29 09:53:12 -0400 | [diff] [blame] | 650 | const vpx_codec_enc_cfg_t *cfg, |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 651 | const vpx_rational_t *fps) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 652 | int i; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 653 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 654 | /* Determine the number of samples in the buffer. Use the file's framerate |
| 655 | * to determine the number of frames in rc_buf_sz milliseconds, with an |
| 656 | * adjustment (5/4) to account for alt-refs |
| 657 | */ |
| 658 | 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] | 659 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 660 | /* prevent division by zero */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 661 | if (hist->samples == 0) |
| 662 | hist->samples = 1; |
Tero Rintaluoma | fd41cb8 | 2011-07-15 14:47:44 +0300 | [diff] [blame] | 663 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 664 | hist->pts = calloc(hist->samples, sizeof(*hist->pts)); |
| 665 | hist->sz = calloc(hist->samples, sizeof(*hist->sz)); |
| 666 | for (i = 0; i < RATE_BINS; i++) { |
| 667 | hist->bucket[i].low = INT_MAX; |
| 668 | hist->bucket[i].high = 0; |
| 669 | hist->bucket[i].count = 0; |
| 670 | } |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 674 | static void destroy_rate_histogram(struct rate_hist *hist) { |
| 675 | free(hist->pts); |
| 676 | free(hist->sz); |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | |
| 680 | static void update_rate_histogram(struct rate_hist *hist, |
| 681 | const vpx_codec_enc_cfg_t *cfg, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 682 | const vpx_codec_cx_pkt_t *pkt) { |
| 683 | int i, idx; |
| 684 | int64_t now, then, sum_sz = 0, avg_bitrate; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 685 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 686 | now = pkt->data.frame.pts * 1000 |
| 687 | * (uint64_t)cfg->g_timebase.num / (uint64_t)cfg->g_timebase.den; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 688 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 689 | idx = hist->frames++ % hist->samples; |
| 690 | hist->pts[idx] = now; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 691 | hist->sz[idx] = (int)pkt->data.frame.sz; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 692 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 693 | if (now < cfg->rc_buf_initial_sz) |
| 694 | return; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 695 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 696 | then = now; |
Johann | bb0ca87 | 2011-06-29 14:34:25 -0400 | [diff] [blame] | 697 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 698 | /* Sum the size over the past rc_buf_sz ms */ |
| 699 | for (i = hist->frames; i > 0 && hist->frames - i < hist->samples; i--) { |
| 700 | int i_idx = (i - 1) % hist->samples; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 701 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 702 | then = hist->pts[i_idx]; |
| 703 | if (now - then > cfg->rc_buf_sz) |
| 704 | break; |
| 705 | sum_sz += hist->sz[i_idx]; |
| 706 | } |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 707 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 708 | if (now == then) |
| 709 | return; |
Johann | bb0ca87 | 2011-06-29 14:34:25 -0400 | [diff] [blame] | 710 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 711 | avg_bitrate = sum_sz * 8 * 1000 / (now - then); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 712 | idx = (int)(avg_bitrate * (RATE_BINS / 2) / (cfg->rc_target_bitrate * 1000)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 713 | if (idx < 0) |
| 714 | idx = 0; |
| 715 | if (idx > RATE_BINS - 1) |
| 716 | idx = RATE_BINS - 1; |
| 717 | if (hist->bucket[idx].low > avg_bitrate) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 718 | hist->bucket[idx].low = (int)avg_bitrate; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 719 | if (hist->bucket[idx].high < avg_bitrate) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 720 | hist->bucket[idx].high = (int)avg_bitrate; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 721 | hist->bucket[idx].count++; |
| 722 | hist->total++; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | |
| 726 | static void show_rate_histogram(struct rate_hist *hist, |
| 727 | const vpx_codec_enc_cfg_t *cfg, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 728 | int max_buckets) { |
| 729 | int i, scale; |
| 730 | int buckets = 0; |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 731 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 732 | for (i = 0; i < RATE_BINS; i++) { |
| 733 | if (hist->bucket[i].low == INT_MAX) |
| 734 | continue; |
| 735 | hist->bucket[buckets++] = hist->bucket[i]; |
| 736 | } |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 737 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 738 | fprintf(stderr, "\nRate (over %dms window):\n", cfg->rc_buf_sz); |
| 739 | scale = merge_hist_buckets(hist->bucket, &buckets, max_buckets); |
| 740 | show_histogram(hist->bucket, buckets, hist->total, scale); |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 741 | } |
| 742 | |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 743 | #define mmin(a, b) ((a) < (b) ? (a) : (b)) |
| 744 | static void find_mismatch(vpx_image_t *img1, vpx_image_t *img2, |
Jingning Han | 12bf079 | 2013-04-06 10:00:53 -0700 | [diff] [blame] | 745 | int yloc[4], int uloc[4], int vloc[4]) { |
Paul Wilkins | f759762 | 2013-01-24 11:42:38 +0000 | [diff] [blame] | 746 | const unsigned int bsize = 64; |
John Koleszar | beae501 | 2013-05-08 16:07:38 -0700 | [diff] [blame] | 747 | const unsigned int bsizey = bsize >> img1->y_chroma_shift; |
| 748 | const unsigned int bsizex = bsize >> img1->x_chroma_shift; |
| 749 | const int c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift; |
| 750 | const int c_h = (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift; |
Paul Wilkins | f759762 | 2013-01-24 11:42:38 +0000 | [diff] [blame] | 751 | unsigned int match = 1; |
| 752 | unsigned int i, j; |
Deb Mukherjee | 23144d2 | 2013-03-12 14:21:08 -0700 | [diff] [blame] | 753 | yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 754 | for (i = 0, match = 1; match && i < img1->d_h; i += bsize) { |
| 755 | for (j = 0; match && j < img1->d_w; j += bsize) { |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 756 | int k, l; |
Deb Mukherjee | 01cafaa | 2013-01-15 06:43:35 -0800 | [diff] [blame] | 757 | int si = mmin(i + bsize, img1->d_h) - i; |
| 758 | int sj = mmin(j + bsize, img1->d_w) - j; |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 759 | for (k = 0; match && k < si; k++) |
| 760 | for (l = 0; match && l < sj; l++) { |
| 761 | if (*(img1->planes[VPX_PLANE_Y] + |
| 762 | (i + k) * img1->stride[VPX_PLANE_Y] + j + l) != |
| 763 | *(img2->planes[VPX_PLANE_Y] + |
| 764 | (i + k) * img2->stride[VPX_PLANE_Y] + j + l)) { |
| 765 | yloc[0] = i + k; |
| 766 | yloc[1] = j + l; |
Deb Mukherjee | 23144d2 | 2013-03-12 14:21:08 -0700 | [diff] [blame] | 767 | yloc[2] = *(img1->planes[VPX_PLANE_Y] + |
| 768 | (i + k) * img1->stride[VPX_PLANE_Y] + j + l); |
| 769 | yloc[3] = *(img2->planes[VPX_PLANE_Y] + |
| 770 | (i + k) * img2->stride[VPX_PLANE_Y] + j + l); |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 771 | match = 0; |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | } |
Jingning Han | 12bf079 | 2013-04-06 10:00:53 -0700 | [diff] [blame] | 777 | |
Deb Mukherjee | 23144d2 | 2013-03-12 14:21:08 -0700 | [diff] [blame] | 778 | uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1; |
John Koleszar | beae501 | 2013-05-08 16:07:38 -0700 | [diff] [blame] | 779 | for (i = 0, match = 1; match && i < c_h; i += bsizey) { |
John Koleszar | cc0eeda | 2013-06-11 11:28:08 -0700 | [diff] [blame] | 780 | for (j = 0; match && j < c_w; j += bsizex) { |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 781 | int k, l; |
John Koleszar | beae501 | 2013-05-08 16:07:38 -0700 | [diff] [blame] | 782 | int si = mmin(i + bsizey, c_h - i); |
| 783 | int sj = mmin(j + bsizex, c_w - j); |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 784 | for (k = 0; match && k < si; k++) |
| 785 | for (l = 0; match && l < sj; l++) { |
| 786 | if (*(img1->planes[VPX_PLANE_U] + |
| 787 | (i + k) * img1->stride[VPX_PLANE_U] + j + l) != |
| 788 | *(img2->planes[VPX_PLANE_U] + |
| 789 | (i + k) * img2->stride[VPX_PLANE_U] + j + l)) { |
| 790 | uloc[0] = i + k; |
| 791 | uloc[1] = j + l; |
Deb Mukherjee | 23144d2 | 2013-03-12 14:21:08 -0700 | [diff] [blame] | 792 | uloc[2] = *(img1->planes[VPX_PLANE_U] + |
| 793 | (i + k) * img1->stride[VPX_PLANE_U] + j + l); |
| 794 | uloc[3] = *(img2->planes[VPX_PLANE_U] + |
| 795 | (i + k) * img2->stride[VPX_PLANE_V] + j + l); |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 796 | match = 0; |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | } |
Deb Mukherjee | 23144d2 | 2013-03-12 14:21:08 -0700 | [diff] [blame] | 802 | vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1; |
John Koleszar | beae501 | 2013-05-08 16:07:38 -0700 | [diff] [blame] | 803 | for (i = 0, match = 1; match && i < c_h; i += bsizey) { |
John Koleszar | cc0eeda | 2013-06-11 11:28:08 -0700 | [diff] [blame] | 804 | for (j = 0; match && j < c_w; j += bsizex) { |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 805 | int k, l; |
John Koleszar | beae501 | 2013-05-08 16:07:38 -0700 | [diff] [blame] | 806 | int si = mmin(i + bsizey, c_h - i); |
| 807 | int sj = mmin(j + bsizex, c_w - j); |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 808 | for (k = 0; match && k < si; k++) |
| 809 | for (l = 0; match && l < sj; l++) { |
| 810 | if (*(img1->planes[VPX_PLANE_V] + |
| 811 | (i + k) * img1->stride[VPX_PLANE_V] + j + l) != |
| 812 | *(img2->planes[VPX_PLANE_V] + |
| 813 | (i + k) * img2->stride[VPX_PLANE_V] + j + l)) { |
| 814 | vloc[0] = i + k; |
| 815 | vloc[1] = j + l; |
Deb Mukherjee | 23144d2 | 2013-03-12 14:21:08 -0700 | [diff] [blame] | 816 | vloc[2] = *(img1->planes[VPX_PLANE_V] + |
| 817 | (i + k) * img1->stride[VPX_PLANE_V] + j + l); |
| 818 | vloc[3] = *(img2->planes[VPX_PLANE_V] + |
| 819 | (i + k) * img2->stride[VPX_PLANE_V] + j + l); |
Deb Mukherjee | 0742b1e | 2012-11-15 15:14:38 -0800 | [diff] [blame] | 820 | match = 0; |
| 821 | break; |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 827 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 828 | static int compare_img(vpx_image_t *img1, vpx_image_t *img2) |
| 829 | { |
John Koleszar | beae501 | 2013-05-08 16:07:38 -0700 | [diff] [blame] | 830 | const int c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift; |
| 831 | const int c_h = (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 832 | int match = 1; |
Paul Wilkins | f759762 | 2013-01-24 11:42:38 +0000 | [diff] [blame] | 833 | unsigned int i; |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 834 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 835 | match &= (img1->fmt == img2->fmt); |
hkuang | 3671e62 | 2014-01-22 14:58:59 -0800 | [diff] [blame^] | 836 | match &= (img1->d_w == img2->d_w); |
| 837 | match &= (img1->d_h == img2->d_h); |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 838 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 839 | for (i = 0; i < img1->d_h; i++) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 840 | match &= (memcmp(img1->planes[VPX_PLANE_Y]+i*img1->stride[VPX_PLANE_Y], |
| 841 | img2->planes[VPX_PLANE_Y]+i*img2->stride[VPX_PLANE_Y], |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 842 | img1->d_w) == 0); |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 843 | |
John Koleszar | beae501 | 2013-05-08 16:07:38 -0700 | [diff] [blame] | 844 | for (i = 0; i < c_h; i++) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 845 | match &= (memcmp(img1->planes[VPX_PLANE_U]+i*img1->stride[VPX_PLANE_U], |
| 846 | img2->planes[VPX_PLANE_U]+i*img2->stride[VPX_PLANE_U], |
John Koleszar | beae501 | 2013-05-08 16:07:38 -0700 | [diff] [blame] | 847 | c_w) == 0); |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 848 | |
John Koleszar | beae501 | 2013-05-08 16:07:38 -0700 | [diff] [blame] | 849 | for (i = 0; i < c_h; i++) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 850 | match &= (memcmp(img1->planes[VPX_PLANE_V]+i*img1->stride[VPX_PLANE_U], |
| 851 | img2->planes[VPX_PLANE_V]+i*img2->stride[VPX_PLANE_U], |
John Koleszar | beae501 | 2013-05-08 16:07:38 -0700 | [diff] [blame] | 852 | c_w) == 0); |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 853 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 854 | return match; |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 858 | #define NELEMENTS(x) (sizeof(x)/sizeof(x[0])) |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 859 | #define MAX(x,y) ((x)>(y)?(x):(y)) |
| 860 | #if CONFIG_VP8_ENCODER && !CONFIG_VP9_ENCODER |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 861 | #define ARG_CTRL_CNT_MAX NELEMENTS(vp8_arg_ctrl_map) |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 862 | #elif !CONFIG_VP8_ENCODER && CONFIG_VP9_ENCODER |
| 863 | #define ARG_CTRL_CNT_MAX NELEMENTS(vp9_arg_ctrl_map) |
| 864 | #else |
| 865 | #define ARG_CTRL_CNT_MAX MAX(NELEMENTS(vp8_arg_ctrl_map), \ |
| 866 | NELEMENTS(vp9_arg_ctrl_map)) |
| 867 | #endif |
John Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 868 | |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 869 | /* Per-stream configuration */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 870 | struct stream_config { |
| 871 | struct vpx_codec_enc_cfg cfg; |
| 872 | const char *out_fn; |
| 873 | const char *stats_fn; |
| 874 | stereo_format_t stereo_fmt; |
| 875 | int arg_ctrls[ARG_CTRL_CNT_MAX][2]; |
| 876 | int arg_ctrl_cnt; |
| 877 | int write_webm; |
| 878 | int have_kf_max_dist; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 879 | }; |
| 880 | |
| 881 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 882 | struct stream_state { |
| 883 | int index; |
| 884 | struct stream_state *next; |
| 885 | struct stream_config config; |
| 886 | FILE *file; |
| 887 | struct rate_hist rate_hist; |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 888 | struct EbmlGlobal ebml; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 889 | uint32_t hash; |
| 890 | uint64_t psnr_sse_total; |
| 891 | uint64_t psnr_samples_total; |
| 892 | double psnr_totals[4]; |
| 893 | int psnr_count; |
| 894 | int counts[64]; |
| 895 | vpx_codec_ctx_t encoder; |
| 896 | unsigned int frames_out; |
| 897 | uint64_t cx_time; |
| 898 | size_t nbytes; |
| 899 | stats_io_t stats; |
John Koleszar | 34882b9 | 2012-03-01 12:50:40 -0800 | [diff] [blame] | 900 | struct vpx_image *img; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 901 | vpx_codec_ctx_t decoder; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 902 | int mismatch_seen; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 903 | }; |
| 904 | |
| 905 | |
John Koleszar | 1b27e93 | 2012-04-25 17:08:56 -0700 | [diff] [blame] | 906 | void validate_positive_rational(const char *msg, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 907 | struct vpx_rational *rat) { |
| 908 | if (rat->den < 0) { |
| 909 | rat->num *= -1; |
| 910 | rat->den *= -1; |
| 911 | } |
John Koleszar | 1b27e93 | 2012-04-25 17:08:56 -0700 | [diff] [blame] | 912 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 913 | if (rat->num < 0) |
| 914 | die("Error: %s must be positive\n", msg); |
John Koleszar | 1b27e93 | 2012-04-25 17:08:56 -0700 | [diff] [blame] | 915 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 916 | if (!rat->den) |
| 917 | die("Error: %s has zero denominator\n", msg); |
John Koleszar | 1b27e93 | 2012-04-25 17:08:56 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 921 | static void parse_global_config(struct VpxEncoderConfig *global, char **argv) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 922 | char **argi, **argj; |
| 923 | struct arg arg; |
John Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 924 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 925 | /* Initialize default parameters */ |
| 926 | memset(global, 0, sizeof(*global)); |
| 927 | global->codec = codecs; |
Deb Mukherjee | 0d8723f | 2013-08-19 14:16:26 -0700 | [diff] [blame] | 928 | global->passes = 0; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 929 | global->use_i420 = 1; |
Deb Mukherjee | 0d8723f | 2013-08-19 14:16:26 -0700 | [diff] [blame] | 930 | /* Assign default deadline to good quality */ |
| 931 | global->deadline = VPX_DL_GOOD_QUALITY; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 932 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 933 | for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) { |
| 934 | arg.argv_step = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 935 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 936 | if (arg_match(&arg, &codecarg, argi)) { |
| 937 | int j, k = -1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 938 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 939 | for (j = 0; j < sizeof(codecs) / sizeof(codecs[0]); j++) |
| 940 | if (!strcmp(codecs[j].name, arg.val)) |
| 941 | k = j; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 942 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 943 | if (k >= 0) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 944 | global->codec = codecs + k; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 945 | else |
| 946 | die("Error: Unrecognized argument (%s) to --codec\n", |
| 947 | arg.val); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 948 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 949 | } else if (arg_match(&arg, &passes, argi)) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 950 | global->passes = arg_parse_uint(&arg); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 951 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 952 | if (global->passes < 1 || global->passes > 2) |
| 953 | die("Error: Invalid number of passes (%d)\n", global->passes); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 954 | } else if (arg_match(&arg, &pass_arg, argi)) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 955 | global->pass = arg_parse_uint(&arg); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 956 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 957 | if (global->pass < 1 || global->pass > 2) |
| 958 | die("Error: Invalid pass selected (%d)\n", |
| 959 | global->pass); |
| 960 | } else if (arg_match(&arg, &usage, argi)) |
| 961 | global->usage = arg_parse_uint(&arg); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 962 | else if (arg_match(&arg, &deadline, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 963 | global->deadline = arg_parse_uint(&arg); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 964 | else if (arg_match(&arg, &best_dl, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 965 | global->deadline = VPX_DL_BEST_QUALITY; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 966 | else if (arg_match(&arg, &good_dl, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 967 | global->deadline = VPX_DL_GOOD_QUALITY; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 968 | else if (arg_match(&arg, &rt_dl, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 969 | global->deadline = VPX_DL_REALTIME; |
| 970 | else if (arg_match(&arg, &use_yv12, argi)) |
| 971 | global->use_i420 = 0; |
| 972 | else if (arg_match(&arg, &use_i420, argi)) |
| 973 | global->use_i420 = 1; |
| 974 | else if (arg_match(&arg, &quietarg, argi)) |
| 975 | global->quiet = 1; |
| 976 | else if (arg_match(&arg, &verbosearg, argi)) |
| 977 | global->verbose = 1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 978 | else if (arg_match(&arg, &limit, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 979 | global->limit = arg_parse_uint(&arg); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 980 | else if (arg_match(&arg, &skip, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 981 | global->skip_frames = arg_parse_uint(&arg); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 982 | else if (arg_match(&arg, &psnrarg, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 983 | global->show_psnr = 1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 984 | else if (arg_match(&arg, &recontest, argi)) |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 985 | global->test_decode = arg_parse_enum_or_int(&arg); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 986 | else if (arg_match(&arg, &framerate, argi)) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 987 | global->framerate = arg_parse_rational(&arg); |
| 988 | validate_positive_rational(arg.name, &global->framerate); |
| 989 | global->have_framerate = 1; |
| 990 | } else if (arg_match(&arg, &out_part, argi)) |
| 991 | global->out_part = 1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 992 | else if (arg_match(&arg, &debugmode, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 993 | global->debug = 1; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 994 | else if (arg_match(&arg, &q_hist_n, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 995 | global->show_q_hist_buckets = arg_parse_uint(&arg); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 996 | else if (arg_match(&arg, &rate_hist_n, argi)) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 997 | global->show_rate_hist_buckets = arg_parse_uint(&arg); |
Tom Finegan | 249366b | 2013-11-25 12:05:19 -0800 | [diff] [blame] | 998 | else if (arg_match(&arg, &disable_warnings, argi)) |
| 999 | global->disable_warnings = 1; |
| 1000 | else if (arg_match(&arg, &disable_warning_prompt, argi)) |
| 1001 | global->disable_warning_prompt = 1; |
John Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 1002 | else |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1003 | argj++; |
| 1004 | } |
| 1005 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1006 | /* Validate global config */ |
Deb Mukherjee | 0d8723f | 2013-08-19 14:16:26 -0700 | [diff] [blame] | 1007 | if (global->passes == 0) { |
James Zern | a0fcbcf | 2013-09-17 18:47:25 -0700 | [diff] [blame] | 1008 | #if CONFIG_VP9_ENCODER |
Deb Mukherjee | 0d8723f | 2013-08-19 14:16:26 -0700 | [diff] [blame] | 1009 | // Make default VP9 passes = 2 until there is a better quality 1-pass |
| 1010 | // encoder |
| 1011 | global->passes = (global->codec->iface == vpx_codec_vp9_cx ? 2 : 1); |
James Zern | a0fcbcf | 2013-09-17 18:47:25 -0700 | [diff] [blame] | 1012 | #else |
| 1013 | global->passes = 1; |
| 1014 | #endif |
Deb Mukherjee | 0d8723f | 2013-08-19 14:16:26 -0700 | [diff] [blame] | 1015 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1016 | |
| 1017 | if (global->pass) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1018 | /* DWIM: Assume the user meant passes=2 if pass=2 is specified */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1019 | if (global->pass > global->passes) { |
| 1020 | warn("Assuming --pass=%d implies --passes=%d\n", |
| 1021 | global->pass, global->pass); |
| 1022 | global->passes = global->pass; |
John Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 1023 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1024 | } |
John Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1028 | void open_input_file(struct VpxInputContext *input) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1029 | /* Parse certain options from the input file, if possible */ |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1030 | input->file = strcmp(input->filename, "-") |
| 1031 | ? fopen(input->filename, "rb") : set_binary_mode(stdin); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1032 | |
| 1033 | if (!input->file) |
| 1034 | fatal("Failed to open input file"); |
| 1035 | |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1036 | if (!fseeko(input->file, 0, SEEK_END)) { |
| 1037 | /* Input file is seekable. Figure out how long it is, so we can get |
| 1038 | * progress info. |
| 1039 | */ |
| 1040 | input->length = ftello(input->file); |
| 1041 | rewind(input->file); |
| 1042 | } |
| 1043 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1044 | /* For RAW input sources, these bytes will applied on the first frame |
| 1045 | * in read_frame(). |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1046 | */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1047 | input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file); |
| 1048 | input->detect.position = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1049 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1050 | if (input->detect.buf_read == 4 |
Alex Converse | 64b89f1 | 2014-01-06 16:29:09 -0800 | [diff] [blame] | 1051 | && file_is_y4m(input->detect.buf)) { |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 1052 | if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, |
| 1053 | input->only_i420) >= 0) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1054 | input->file_type = FILE_TYPE_Y4M; |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1055 | input->width = input->y4m.pic_w; |
| 1056 | input->height = input->y4m.pic_h; |
| 1057 | input->framerate.numerator = input->y4m.fps_n; |
| 1058 | input->framerate.denominator = input->y4m.fps_d; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1059 | input->use_i420 = 0; |
| 1060 | } else |
| 1061 | fatal("Unsupported Y4M stream."); |
Alex Converse | 64b89f1 | 2014-01-06 16:29:09 -0800 | [diff] [blame] | 1062 | } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) { |
Tom Finegan | 6c270ed | 2013-11-08 11:32:23 -0800 | [diff] [blame] | 1063 | fatal("IVF is not supported as input."); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1064 | } else { |
| 1065 | input->file_type = FILE_TYPE_RAW; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1066 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1070 | static void close_input_file(struct VpxInputContext *input) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1071 | fclose(input->file); |
| 1072 | if (input->file_type == FILE_TYPE_Y4M) |
| 1073 | y4m_input_close(&input->y4m); |
John Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 1076 | static struct stream_state *new_stream(struct VpxEncoderConfig *global, |
Tom Finegan | 6c270ed | 2013-11-08 11:32:23 -0800 | [diff] [blame] | 1077 | struct stream_state *prev) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1078 | struct stream_state *stream; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1079 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1080 | stream = calloc(1, sizeof(*stream)); |
| 1081 | if (!stream) |
| 1082 | fatal("Failed to allocate new stream."); |
| 1083 | if (prev) { |
| 1084 | memcpy(stream, prev, sizeof(*stream)); |
| 1085 | stream->index++; |
| 1086 | prev->next = stream; |
| 1087 | } else { |
| 1088 | vpx_codec_err_t res; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1089 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1090 | /* Populate encoder configuration */ |
| 1091 | res = vpx_codec_enc_config_default(global->codec->iface(), |
| 1092 | &stream->config.cfg, |
| 1093 | global->usage); |
| 1094 | if (res) |
| 1095 | 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] | 1096 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1097 | /* Change the default timebase to a high enough value so that the |
| 1098 | * encoder will always create strictly increasing timestamps. |
| 1099 | */ |
| 1100 | stream->config.cfg.g_timebase.den = 1000; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1101 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1102 | /* Never use the library's default resolution, require it be parsed |
| 1103 | * from the file or set on the command line. |
| 1104 | */ |
| 1105 | stream->config.cfg.g_w = 0; |
| 1106 | stream->config.cfg.g_h = 0; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1107 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1108 | /* Initialize remaining stream parameters */ |
| 1109 | stream->config.stereo_fmt = STEREO_FORMAT_MONO; |
| 1110 | stream->config.write_webm = 1; |
| 1111 | stream->ebml.last_pts_ms = -1; |
Johann | 87c40b3 | 2012-03-01 16:12:53 -0800 | [diff] [blame] | 1112 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1113 | /* Allows removal of the application version from the EBML tags */ |
| 1114 | stream->ebml.debug = global->debug; |
| 1115 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1116 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1117 | /* Output files must be specified for each stream */ |
| 1118 | stream->config.out_fn = NULL; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1119 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1120 | stream->next = NULL; |
| 1121 | return stream; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 1125 | static int parse_stream_params(struct VpxEncoderConfig *global, |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1126 | struct stream_state *stream, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1127 | char **argv) { |
| 1128 | char **argi, **argj; |
| 1129 | struct arg arg; |
| 1130 | static const arg_def_t **ctrl_args = no_args; |
| 1131 | static const int *ctrl_args_map = NULL; |
| 1132 | struct stream_config *config = &stream->config; |
| 1133 | int eos_mark_found = 0; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1134 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1135 | /* Handle codec specific options */ |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1136 | if (0) { |
| 1137 | #if CONFIG_VP8_ENCODER |
| 1138 | } else if (global->codec->iface == vpx_codec_vp8_cx) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1139 | ctrl_args = vp8_args; |
| 1140 | ctrl_args_map = vp8_arg_ctrl_map; |
John Koleszar | a9c7597 | 2012-11-08 17:09:30 -0800 | [diff] [blame] | 1141 | #endif |
| 1142 | #if CONFIG_VP9_ENCODER |
| 1143 | } else if (global->codec->iface == vpx_codec_vp9_cx) { |
| 1144 | ctrl_args = vp9_args; |
| 1145 | ctrl_args_map = vp9_arg_ctrl_map; |
| 1146 | #endif |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1147 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1148 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1149 | for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1150 | arg.argv_step = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1151 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1152 | /* Once we've found an end-of-stream marker (--) we want to continue |
| 1153 | * shifting arguments but not consuming them. |
| 1154 | */ |
| 1155 | if (eos_mark_found) { |
| 1156 | argj++; |
| 1157 | continue; |
| 1158 | } else if (!strcmp(*argj, "--")) { |
| 1159 | eos_mark_found = 1; |
| 1160 | continue; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1163 | if (0); |
| 1164 | else if (arg_match(&arg, &outputfile, argi)) |
| 1165 | config->out_fn = arg.val; |
| 1166 | else if (arg_match(&arg, &fpf_name, argi)) |
| 1167 | config->stats_fn = arg.val; |
| 1168 | else if (arg_match(&arg, &use_ivf, argi)) |
| 1169 | config->write_webm = 0; |
| 1170 | else if (arg_match(&arg, &threads, argi)) |
| 1171 | config->cfg.g_threads = arg_parse_uint(&arg); |
| 1172 | else if (arg_match(&arg, &profile, argi)) |
| 1173 | config->cfg.g_profile = arg_parse_uint(&arg); |
| 1174 | else if (arg_match(&arg, &width, argi)) |
| 1175 | config->cfg.g_w = arg_parse_uint(&arg); |
| 1176 | else if (arg_match(&arg, &height, argi)) |
| 1177 | config->cfg.g_h = arg_parse_uint(&arg); |
| 1178 | else if (arg_match(&arg, &stereo_mode, argi)) |
| 1179 | config->stereo_fmt = arg_parse_enum_or_int(&arg); |
| 1180 | else if (arg_match(&arg, &timebase, argi)) { |
| 1181 | config->cfg.g_timebase = arg_parse_rational(&arg); |
| 1182 | validate_positive_rational(arg.name, &config->cfg.g_timebase); |
| 1183 | } else if (arg_match(&arg, &error_resilient, argi)) |
| 1184 | config->cfg.g_error_resilient = arg_parse_uint(&arg); |
| 1185 | else if (arg_match(&arg, &lag_in_frames, argi)) |
| 1186 | config->cfg.g_lag_in_frames = arg_parse_uint(&arg); |
| 1187 | else if (arg_match(&arg, &dropframe_thresh, argi)) |
| 1188 | config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg); |
| 1189 | else if (arg_match(&arg, &resize_allowed, argi)) |
| 1190 | config->cfg.rc_resize_allowed = arg_parse_uint(&arg); |
| 1191 | else if (arg_match(&arg, &resize_up_thresh, argi)) |
| 1192 | config->cfg.rc_resize_up_thresh = arg_parse_uint(&arg); |
| 1193 | else if (arg_match(&arg, &resize_down_thresh, argi)) |
| 1194 | config->cfg.rc_resize_down_thresh = arg_parse_uint(&arg); |
| 1195 | else if (arg_match(&arg, &end_usage, argi)) |
| 1196 | config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg); |
| 1197 | else if (arg_match(&arg, &target_bitrate, argi)) |
| 1198 | config->cfg.rc_target_bitrate = arg_parse_uint(&arg); |
| 1199 | else if (arg_match(&arg, &min_quantizer, argi)) |
| 1200 | config->cfg.rc_min_quantizer = arg_parse_uint(&arg); |
| 1201 | else if (arg_match(&arg, &max_quantizer, argi)) |
| 1202 | config->cfg.rc_max_quantizer = arg_parse_uint(&arg); |
| 1203 | else if (arg_match(&arg, &undershoot_pct, argi)) |
| 1204 | config->cfg.rc_undershoot_pct = arg_parse_uint(&arg); |
| 1205 | else if (arg_match(&arg, &overshoot_pct, argi)) |
| 1206 | config->cfg.rc_overshoot_pct = arg_parse_uint(&arg); |
| 1207 | else if (arg_match(&arg, &buf_sz, argi)) |
| 1208 | config->cfg.rc_buf_sz = arg_parse_uint(&arg); |
| 1209 | else if (arg_match(&arg, &buf_initial_sz, argi)) |
| 1210 | config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg); |
| 1211 | else if (arg_match(&arg, &buf_optimal_sz, argi)) |
| 1212 | config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg); |
| 1213 | else if (arg_match(&arg, &bias_pct, argi)) { |
| 1214 | config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1215 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1216 | if (global->passes < 2) |
| 1217 | warn("option %s ignored in one-pass mode.\n", arg.name); |
| 1218 | } else if (arg_match(&arg, &minsection_pct, argi)) { |
| 1219 | config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg); |
| 1220 | |
| 1221 | if (global->passes < 2) |
| 1222 | warn("option %s ignored in one-pass mode.\n", arg.name); |
| 1223 | } else if (arg_match(&arg, &maxsection_pct, argi)) { |
| 1224 | config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg); |
| 1225 | |
| 1226 | if (global->passes < 2) |
| 1227 | warn("option %s ignored in one-pass mode.\n", arg.name); |
| 1228 | } else if (arg_match(&arg, &kf_min_dist, argi)) |
| 1229 | config->cfg.kf_min_dist = arg_parse_uint(&arg); |
| 1230 | else if (arg_match(&arg, &kf_max_dist, argi)) { |
| 1231 | config->cfg.kf_max_dist = arg_parse_uint(&arg); |
| 1232 | config->have_kf_max_dist = 1; |
| 1233 | } else if (arg_match(&arg, &kf_disabled, argi)) |
| 1234 | config->cfg.kf_mode = VPX_KF_DISABLED; |
| 1235 | else { |
| 1236 | int i, match = 0; |
| 1237 | |
| 1238 | for (i = 0; ctrl_args[i]; i++) { |
| 1239 | if (arg_match(&arg, ctrl_args[i], argi)) { |
| 1240 | int j; |
| 1241 | match = 1; |
| 1242 | |
| 1243 | /* Point either to the next free element or the first |
| 1244 | * instance of this control. |
| 1245 | */ |
| 1246 | for (j = 0; j < config->arg_ctrl_cnt; j++) |
| 1247 | if (config->arg_ctrls[j][0] == ctrl_args_map[i]) |
| 1248 | break; |
| 1249 | |
| 1250 | /* Update/insert */ |
| 1251 | assert(j < ARG_CTRL_CNT_MAX); |
| 1252 | if (j < ARG_CTRL_CNT_MAX) { |
| 1253 | config->arg_ctrls[j][0] = ctrl_args_map[i]; |
| 1254 | config->arg_ctrls[j][1] = arg_parse_enum_or_int(&arg); |
| 1255 | if (j == config->arg_ctrl_cnt) |
| 1256 | config->arg_ctrl_cnt++; |
| 1257 | } |
| 1258 | |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1259 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1260 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1261 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1262 | if (!match) |
| 1263 | argj++; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1264 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1265 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1266 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1267 | return eos_mark_found; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | |
Tom Finegan | 44dd327 | 2013-11-20 17:18:28 -0800 | [diff] [blame] | 1271 | #define FOREACH_STREAM(func) \ |
| 1272 | do { \ |
| 1273 | struct stream_state *stream; \ |
Tom Finegan | 249366b | 2013-11-25 12:05:19 -0800 | [diff] [blame] | 1274 | for (stream = streams; stream; stream = stream->next) { \ |
Tom Finegan | 44dd327 | 2013-11-20 17:18:28 -0800 | [diff] [blame] | 1275 | func; \ |
| 1276 | } \ |
| 1277 | } while (0) |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1278 | |
| 1279 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1280 | static void validate_stream_config(struct stream_state *stream) { |
| 1281 | struct stream_state *streami; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1282 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1283 | if (!stream->config.cfg.g_w || !stream->config.cfg.g_h) |
| 1284 | fatal("Stream %d: Specify stream dimensions with --width (-w) " |
| 1285 | " and --height (-h)", stream->index); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1286 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1287 | for (streami = stream; streami; streami = streami->next) { |
| 1288 | /* All streams require output files */ |
| 1289 | if (!streami->config.out_fn) |
| 1290 | fatal("Stream %d: Output file is required (specify with -o)", |
| 1291 | streami->index); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1292 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1293 | /* Check for two streams outputting to the same file */ |
| 1294 | if (streami != stream) { |
| 1295 | const char *a = stream->config.out_fn; |
| 1296 | const char *b = streami->config.out_fn; |
| 1297 | if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul")) |
| 1298 | fatal("Stream %d: duplicate output file (from stream %d)", |
| 1299 | streami->index, stream->index); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1300 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1301 | |
| 1302 | /* Check for two streams sharing a stats file. */ |
| 1303 | if (streami != stream) { |
| 1304 | const char *a = stream->config.stats_fn; |
| 1305 | const char *b = streami->config.stats_fn; |
| 1306 | if (a && b && !strcmp(a, b)) |
| 1307 | fatal("Stream %d: duplicate stats file (from stream %d)", |
| 1308 | streami->index, stream->index); |
| 1309 | } |
| 1310 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | static void set_stream_dimensions(struct stream_state *stream, |
| 1315 | unsigned int w, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1316 | unsigned int h) { |
John Koleszar | 34882b9 | 2012-03-01 12:50:40 -0800 | [diff] [blame] | 1317 | if (!stream->config.cfg.g_w) { |
| 1318 | if (!stream->config.cfg.g_h) |
| 1319 | stream->config.cfg.g_w = w; |
| 1320 | else |
| 1321 | stream->config.cfg.g_w = w * stream->config.cfg.g_h / h; |
| 1322 | } |
| 1323 | if (!stream->config.cfg.g_h) { |
| 1324 | stream->config.cfg.g_h = h * stream->config.cfg.g_w / w; |
| 1325 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 1329 | static void set_default_kf_interval(struct stream_state *stream, |
| 1330 | struct VpxEncoderConfig *global) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1331 | /* Use a max keyframe interval of 5 seconds, if none was |
| 1332 | * specified on the command line. |
| 1333 | */ |
| 1334 | if (!stream->config.have_kf_max_dist) { |
| 1335 | double framerate = (double)global->framerate.num / global->framerate.den; |
| 1336 | if (framerate > 0.0) |
| 1337 | stream->config.cfg.kf_max_dist = (unsigned int)(5.0 * framerate); |
| 1338 | } |
Ralph Giles | 061a16d | 2012-01-05 15:05:05 -0600 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 1342 | static void show_stream_config(struct stream_state *stream, |
| 1343 | struct VpxEncoderConfig *global, |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1344 | struct VpxInputContext *input) { |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1345 | |
| 1346 | #define SHOW(field) \ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1347 | fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field) |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1348 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1349 | if (stream->index == 0) { |
| 1350 | fprintf(stderr, "Codec: %s\n", |
| 1351 | vpx_codec_iface_name(global->codec->iface())); |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1352 | fprintf(stderr, "Source file: %s Format: %s\n", input->filename, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1353 | input->use_i420 ? "I420" : "YV12"); |
| 1354 | } |
| 1355 | if (stream->next || stream->index) |
| 1356 | fprintf(stderr, "\nStream Index: %d\n", stream->index); |
| 1357 | fprintf(stderr, "Destination file: %s\n", stream->config.out_fn); |
| 1358 | fprintf(stderr, "Encoder parameters:\n"); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1359 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1360 | SHOW(g_usage); |
| 1361 | SHOW(g_threads); |
| 1362 | SHOW(g_profile); |
| 1363 | SHOW(g_w); |
| 1364 | SHOW(g_h); |
| 1365 | SHOW(g_timebase.num); |
| 1366 | SHOW(g_timebase.den); |
| 1367 | SHOW(g_error_resilient); |
| 1368 | SHOW(g_pass); |
| 1369 | SHOW(g_lag_in_frames); |
| 1370 | SHOW(rc_dropframe_thresh); |
| 1371 | SHOW(rc_resize_allowed); |
| 1372 | SHOW(rc_resize_up_thresh); |
| 1373 | SHOW(rc_resize_down_thresh); |
| 1374 | SHOW(rc_end_usage); |
| 1375 | SHOW(rc_target_bitrate); |
| 1376 | SHOW(rc_min_quantizer); |
| 1377 | SHOW(rc_max_quantizer); |
| 1378 | SHOW(rc_undershoot_pct); |
| 1379 | SHOW(rc_overshoot_pct); |
| 1380 | SHOW(rc_buf_sz); |
| 1381 | SHOW(rc_buf_initial_sz); |
| 1382 | SHOW(rc_buf_optimal_sz); |
| 1383 | SHOW(rc_2pass_vbr_bias_pct); |
| 1384 | SHOW(rc_2pass_vbr_minsection_pct); |
| 1385 | SHOW(rc_2pass_vbr_maxsection_pct); |
| 1386 | SHOW(kf_mode); |
| 1387 | SHOW(kf_min_dist); |
| 1388 | SHOW(kf_max_dist); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | |
| 1392 | static void open_output_file(struct stream_state *stream, |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 1393 | struct VpxEncoderConfig *global) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1394 | const char *fn = stream->config.out_fn; |
Dmitry Kovalev | 096ab11 | 2014-01-13 15:21:48 -0800 | [diff] [blame] | 1395 | const struct vpx_codec_enc_cfg *const cfg = &stream->config.cfg; |
| 1396 | |
| 1397 | if (cfg->g_pass == VPX_RC_FIRST_PASS) |
| 1398 | return; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1399 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1400 | stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1401 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1402 | if (!stream->file) |
| 1403 | fatal("Failed to open output file"); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1404 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1405 | if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR)) |
| 1406 | fatal("WebM output to pipes not supported."); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1407 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1408 | if (stream->config.write_webm) { |
| 1409 | stream->ebml.stream = stream->file; |
Dmitry Kovalev | 096ab11 | 2014-01-13 15:21:48 -0800 | [diff] [blame] | 1410 | write_webm_file_header(&stream->ebml, cfg, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1411 | &global->framerate, |
John Koleszar | 08b43fe | 2012-11-13 12:14:28 -0800 | [diff] [blame] | 1412 | stream->config.stereo_fmt, |
| 1413 | global->codec->fourcc); |
Dmitry Kovalev | 096ab11 | 2014-01-13 15:21:48 -0800 | [diff] [blame] | 1414 | } else { |
| 1415 | ivf_write_file_header(stream->file, cfg, global->codec->fourcc, 0); |
| 1416 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | static void close_output_file(struct stream_state *stream, |
Dmitry Kovalev | 096ab11 | 2014-01-13 15:21:48 -0800 | [diff] [blame] | 1421 | unsigned int fourcc) { |
| 1422 | const struct vpx_codec_enc_cfg *const cfg = &stream->config.cfg; |
| 1423 | |
| 1424 | if (cfg->g_pass == VPX_RC_FIRST_PASS) |
| 1425 | return; |
| 1426 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1427 | if (stream->config.write_webm) { |
| 1428 | write_webm_file_footer(&stream->ebml, stream->hash); |
| 1429 | free(stream->ebml.cue_list); |
| 1430 | stream->ebml.cue_list = NULL; |
| 1431 | } else { |
| 1432 | if (!fseek(stream->file, 0, SEEK_SET)) |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1433 | ivf_write_file_header(stream->file, &stream->config.cfg, |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1434 | fourcc, |
| 1435 | stream->frames_out); |
| 1436 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1437 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1438 | fclose(stream->file); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 1442 | static void setup_pass(struct stream_state *stream, |
| 1443 | struct VpxEncoderConfig *global, |
| 1444 | int pass) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1445 | if (stream->config.stats_fn) { |
| 1446 | if (!stats_open_file(&stream->stats, stream->config.stats_fn, |
| 1447 | pass)) |
| 1448 | fatal("Failed to open statistics store"); |
| 1449 | } else { |
| 1450 | if (!stats_open_mem(&stream->stats, pass)) |
| 1451 | fatal("Failed to open statistics store"); |
| 1452 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1453 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1454 | stream->config.cfg.g_pass = global->passes == 2 |
| 1455 | ? pass ? VPX_RC_LAST_PASS : VPX_RC_FIRST_PASS |
| 1456 | : VPX_RC_ONE_PASS; |
| 1457 | if (pass) |
| 1458 | stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats); |
Yunqing Wang | aabae97 | 2012-02-29 08:24:53 -0500 | [diff] [blame] | 1459 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1460 | stream->cx_time = 0; |
| 1461 | stream->nbytes = 0; |
| 1462 | stream->frames_out = 0; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 1466 | static void initialize_encoder(struct stream_state *stream, |
| 1467 | struct VpxEncoderConfig *global) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1468 | int i; |
| 1469 | int flags = 0; |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 1470 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1471 | flags |= global->show_psnr ? VPX_CODEC_USE_PSNR : 0; |
| 1472 | flags |= global->out_part ? VPX_CODEC_USE_OUTPUT_PARTITION : 0; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1473 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1474 | /* Construct Encoder Context */ |
| 1475 | vpx_codec_enc_init(&stream->encoder, global->codec->iface(), |
| 1476 | &stream->config.cfg, flags); |
| 1477 | ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder"); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1478 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1479 | /* Note that we bypass the vpx_codec_control wrapper macro because |
| 1480 | * we're being clever to store the control IDs in an array. Real |
| 1481 | * applications will want to make use of the enumerations directly |
| 1482 | */ |
| 1483 | for (i = 0; i < stream->config.arg_ctrl_cnt; i++) { |
| 1484 | int ctrl = stream->config.arg_ctrls[i][0]; |
| 1485 | int value = stream->config.arg_ctrls[i][1]; |
| 1486 | if (vpx_codec_control_(&stream->encoder, ctrl, value)) |
| 1487 | fprintf(stderr, "Error: Tried to set control %d = %d\n", |
| 1488 | ctrl, value); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1489 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1490 | ctx_exit_on_error(&stream->encoder, "Failed to control codec"); |
| 1491 | } |
| 1492 | |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 1493 | #if CONFIG_DECODERS |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 1494 | if (global->test_decode != TEST_DECODE_OFF) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1495 | vpx_codec_dec_init(&stream->decoder, global->codec->dx_iface(), NULL, 0); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1496 | } |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 1497 | #endif |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 1501 | static void encode_frame(struct stream_state *stream, |
| 1502 | struct VpxEncoderConfig *global, |
| 1503 | struct vpx_image *img, |
| 1504 | unsigned int frames_in) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1505 | vpx_codec_pts_t frame_start, next_frame_start; |
| 1506 | struct vpx_codec_enc_cfg *cfg = &stream->config.cfg; |
| 1507 | struct vpx_usec_timer timer; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1508 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1509 | frame_start = (cfg->g_timebase.den * (int64_t)(frames_in - 1) |
| 1510 | * global->framerate.den) |
| 1511 | / cfg->g_timebase.num / global->framerate.num; |
| 1512 | next_frame_start = (cfg->g_timebase.den * (int64_t)(frames_in) |
| 1513 | * global->framerate.den) |
| 1514 | / cfg->g_timebase.num / global->framerate.num; |
John Koleszar | 34882b9 | 2012-03-01 12:50:40 -0800 | [diff] [blame] | 1515 | |
| 1516 | /* Scale if necessary */ |
| 1517 | if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) { |
| 1518 | if (!stream->img) |
| 1519 | stream->img = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, |
| 1520 | cfg->g_w, cfg->g_h, 16); |
| 1521 | I420Scale(img->planes[VPX_PLANE_Y], img->stride[VPX_PLANE_Y], |
| 1522 | img->planes[VPX_PLANE_U], img->stride[VPX_PLANE_U], |
| 1523 | img->planes[VPX_PLANE_V], img->stride[VPX_PLANE_V], |
| 1524 | img->d_w, img->d_h, |
| 1525 | stream->img->planes[VPX_PLANE_Y], |
| 1526 | stream->img->stride[VPX_PLANE_Y], |
| 1527 | stream->img->planes[VPX_PLANE_U], |
| 1528 | stream->img->stride[VPX_PLANE_U], |
| 1529 | stream->img->planes[VPX_PLANE_V], |
| 1530 | stream->img->stride[VPX_PLANE_V], |
| 1531 | stream->img->d_w, stream->img->d_h, |
| 1532 | kFilterBox); |
| 1533 | |
| 1534 | img = stream->img; |
| 1535 | } |
| 1536 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1537 | vpx_usec_timer_start(&timer); |
| 1538 | vpx_codec_encode(&stream->encoder, img, frame_start, |
| 1539 | (unsigned long)(next_frame_start - frame_start), |
| 1540 | 0, global->deadline); |
| 1541 | vpx_usec_timer_mark(&timer); |
| 1542 | stream->cx_time += vpx_usec_timer_elapsed(&timer); |
| 1543 | ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame", |
| 1544 | stream->index); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1548 | static void update_quantizer_histogram(struct stream_state *stream) { |
| 1549 | if (stream->config.cfg.g_pass != VPX_RC_FIRST_PASS) { |
| 1550 | int q; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1551 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1552 | vpx_codec_control(&stream->encoder, VP8E_GET_LAST_QUANTIZER_64, &q); |
| 1553 | ctx_exit_on_error(&stream->encoder, "Failed to read quantizer"); |
| 1554 | stream->counts[q]++; |
| 1555 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 1559 | static void get_cx_data(struct stream_state *stream, |
| 1560 | struct VpxEncoderConfig *global, |
| 1561 | int *got_data) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1562 | const vpx_codec_cx_pkt_t *pkt; |
| 1563 | const struct vpx_codec_enc_cfg *cfg = &stream->config.cfg; |
| 1564 | vpx_codec_iter_t iter = NULL; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1565 | |
Ronald S. Bultje | 88d703c | 2012-11-09 09:07:50 -0800 | [diff] [blame] | 1566 | *got_data = 0; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1567 | while ((pkt = vpx_codec_get_cx_data(&stream->encoder, &iter))) { |
| 1568 | static size_t fsize = 0; |
| 1569 | static off_t ivf_header_pos = 0; |
| 1570 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1571 | switch (pkt->kind) { |
| 1572 | case VPX_CODEC_CX_FRAME_PKT: |
| 1573 | if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) { |
| 1574 | stream->frames_out++; |
| 1575 | } |
Jingning Han | babbd5d | 2013-02-13 09:03:21 -0800 | [diff] [blame] | 1576 | if (!global->quiet) |
| 1577 | fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1578 | |
| 1579 | update_rate_histogram(&stream->rate_hist, cfg, pkt); |
| 1580 | if (stream->config.write_webm) { |
| 1581 | /* Update the hash */ |
| 1582 | if (!stream->ebml.debug) |
| 1583 | stream->hash = murmur(pkt->data.frame.buf, |
| 1584 | (int)pkt->data.frame.sz, |
| 1585 | stream->hash); |
| 1586 | |
| 1587 | write_webm_block(&stream->ebml, cfg, pkt); |
| 1588 | } else { |
| 1589 | if (pkt->data.frame.partition_id <= 0) { |
| 1590 | ivf_header_pos = ftello(stream->file); |
| 1591 | fsize = pkt->data.frame.sz; |
| 1592 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1593 | ivf_write_frame_header(stream->file, pkt); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1594 | } else { |
| 1595 | fsize += pkt->data.frame.sz; |
| 1596 | |
| 1597 | if (!(pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)) { |
| 1598 | off_t currpos = ftello(stream->file); |
| 1599 | fseeko(stream->file, ivf_header_pos, SEEK_SET); |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1600 | ivf_write_frame_size(stream->file, fsize); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1601 | fseeko(stream->file, currpos, SEEK_SET); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | (void) fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, |
| 1606 | stream->file); |
| 1607 | } |
| 1608 | stream->nbytes += pkt->data.raw.sz; |
Attila Nagy | 52cf4dc | 2012-02-09 12:37:03 +0200 | [diff] [blame] | 1609 | |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1610 | *got_data = 1; |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 1611 | #if CONFIG_DECODERS |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 1612 | if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1613 | vpx_codec_decode(&stream->decoder, pkt->data.frame.buf, |
| 1614 | pkt->data.frame.sz, NULL, 0); |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 1615 | if (stream->decoder.err) { |
| 1616 | warn_or_exit_on_error(&stream->decoder, |
| 1617 | global->test_decode == TEST_DECODE_FATAL, |
| 1618 | "Failed to decode frame %d in stream %d", |
| 1619 | stream->frames_out + 1, stream->index); |
| 1620 | stream->mismatch_seen = stream->frames_out + 1; |
| 1621 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1622 | } |
John Koleszar | 5ebe94f | 2012-12-23 07:20:10 -0800 | [diff] [blame] | 1623 | #endif |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1624 | break; |
| 1625 | case VPX_CODEC_STATS_PKT: |
| 1626 | stream->frames_out++; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1627 | stats_write(&stream->stats, |
| 1628 | pkt->data.twopass_stats.buf, |
| 1629 | pkt->data.twopass_stats.sz); |
| 1630 | stream->nbytes += pkt->data.raw.sz; |
| 1631 | break; |
| 1632 | case VPX_CODEC_PSNR_PKT: |
| 1633 | |
| 1634 | if (global->show_psnr) { |
| 1635 | int i; |
| 1636 | |
| 1637 | stream->psnr_sse_total += pkt->data.psnr.sse[0]; |
| 1638 | stream->psnr_samples_total += pkt->data.psnr.samples[0]; |
| 1639 | for (i = 0; i < 4; i++) { |
Jingning Han | babbd5d | 2013-02-13 09:03:21 -0800 | [diff] [blame] | 1640 | if (!global->quiet) |
| 1641 | fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1642 | stream->psnr_totals[i] += pkt->data.psnr.psnr[i]; |
| 1643 | } |
| 1644 | stream->psnr_count++; |
| 1645 | } |
| 1646 | |
| 1647 | break; |
| 1648 | default: |
| 1649 | break; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1650 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1651 | } |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1655 | static void show_psnr(struct stream_state *stream) { |
| 1656 | int i; |
| 1657 | double ovpsnr; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1658 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1659 | if (!stream->psnr_count) |
| 1660 | return; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1661 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1662 | fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index); |
| 1663 | ovpsnr = vp8_mse2psnr((double)stream->psnr_samples_total, 255.0, |
| 1664 | (double)stream->psnr_sse_total); |
| 1665 | fprintf(stderr, " %.3f", ovpsnr); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1666 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1667 | for (i = 0; i < 4; i++) { |
| 1668 | fprintf(stderr, " %.3f", stream->psnr_totals[i] / stream->psnr_count); |
| 1669 | } |
| 1670 | fprintf(stderr, "\n"); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1674 | static float usec_to_fps(uint64_t usec, unsigned int frames) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1675 | return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0); |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1676 | } |
| 1677 | |
John Koleszar | 732cb9a | 2012-02-14 12:30:17 -0800 | [diff] [blame] | 1678 | |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 1679 | static void test_decode(struct stream_state *stream, |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 1680 | enum TestDecodeFatality fatal, |
| 1681 | const struct codec_item *codec) { |
| 1682 | vpx_image_t enc_img, dec_img; |
| 1683 | |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 1684 | if (stream->mismatch_seen) |
| 1685 | return; |
| 1686 | |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 1687 | /* Get the internal reference frame */ |
| 1688 | if (codec->fourcc == VP8_FOURCC) { |
| 1689 | struct vpx_ref_frame ref_enc, ref_dec; |
| 1690 | int width, height; |
| 1691 | |
| 1692 | width = (stream->config.cfg.g_w + 15) & ~15; |
| 1693 | height = (stream->config.cfg.g_h + 15) & ~15; |
| 1694 | vpx_img_alloc(&ref_enc.img, VPX_IMG_FMT_I420, width, height, 1); |
| 1695 | enc_img = ref_enc.img; |
| 1696 | vpx_img_alloc(&ref_dec.img, VPX_IMG_FMT_I420, width, height, 1); |
| 1697 | dec_img = ref_dec.img; |
| 1698 | |
| 1699 | ref_enc.frame_type = VP8_LAST_FRAME; |
| 1700 | ref_dec.frame_type = VP8_LAST_FRAME; |
| 1701 | vpx_codec_control(&stream->encoder, VP8_COPY_REFERENCE, &ref_enc); |
| 1702 | vpx_codec_control(&stream->decoder, VP8_COPY_REFERENCE, &ref_dec); |
| 1703 | } else { |
| 1704 | struct vp9_ref_frame ref; |
| 1705 | |
| 1706 | ref.idx = 0; |
| 1707 | vpx_codec_control(&stream->encoder, VP9_GET_REFERENCE, &ref); |
| 1708 | enc_img = ref.img; |
| 1709 | vpx_codec_control(&stream->decoder, VP9_GET_REFERENCE, &ref); |
| 1710 | dec_img = ref.img; |
| 1711 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1712 | ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame"); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1713 | ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame"); |
John Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 1714 | |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 1715 | if (!compare_img(&enc_img, &dec_img)) { |
Deb Mukherjee | 23144d2 | 2013-03-12 14:21:08 -0700 | [diff] [blame] | 1716 | int y[4], u[4], v[4]; |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 1717 | find_mismatch(&enc_img, &dec_img, y, u, v); |
Ronald S. Bultje | 97dd734 | 2013-03-04 14:12:49 -0800 | [diff] [blame] | 1718 | stream->decoder.err = 1; |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 1719 | warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL, |
Deb Mukherjee | 23144d2 | 2013-03-12 14:21:08 -0700 | [diff] [blame] | 1720 | "Stream %d: Encode/decode mismatch on frame %d at" |
| 1721 | " Y[%d, %d] {%d/%d}," |
| 1722 | " U[%d, %d] {%d/%d}," |
| 1723 | " V[%d, %d] {%d/%d}", |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 1724 | stream->index, stream->frames_out, |
Deb Mukherjee | 23144d2 | 2013-03-12 14:21:08 -0700 | [diff] [blame] | 1725 | y[0], y[1], y[2], y[3], |
| 1726 | u[0], u[1], u[2], u[3], |
| 1727 | v[0], v[1], v[2], v[3]); |
Ronald S. Bultje | 195172c | 2012-11-10 16:22:49 -0800 | [diff] [blame] | 1728 | stream->mismatch_seen = stream->frames_out; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1729 | } |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 1730 | |
| 1731 | vpx_img_free(&enc_img); |
| 1732 | vpx_img_free(&dec_img); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1733 | } |
John Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 1734 | |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1735 | |
| 1736 | static void print_time(const char *label, int64_t etl) { |
| 1737 | int hours, mins, secs; |
| 1738 | |
| 1739 | if (etl >= 0) { |
| 1740 | hours = etl / 3600; |
| 1741 | etl -= hours * 3600; |
| 1742 | mins = etl / 60; |
| 1743 | etl -= mins * 60; |
| 1744 | secs = etl; |
| 1745 | |
| 1746 | fprintf(stderr, "[%3s %2d:%02d:%02d] ", |
| 1747 | label, hours, mins, secs); |
| 1748 | } else { |
| 1749 | fprintf(stderr, "[%3s unknown] ", label); |
| 1750 | } |
| 1751 | } |
| 1752 | |
Tom Finegan | 44dd327 | 2013-11-20 17:18:28 -0800 | [diff] [blame] | 1753 | |
| 1754 | int main(int argc, const char **argv_) { |
| 1755 | int pass; |
| 1756 | vpx_image_t raw; |
| 1757 | int frame_avail, got_data; |
| 1758 | |
| 1759 | struct VpxInputContext input = {0}; |
Tom Finegan | 49dc9ca | 2013-11-21 16:46:40 -0800 | [diff] [blame] | 1760 | struct VpxEncoderConfig global; |
Tom Finegan | 44dd327 | 2013-11-20 17:18:28 -0800 | [diff] [blame] | 1761 | struct stream_state *streams = NULL; |
| 1762 | char **argv, **argi; |
| 1763 | uint64_t cx_time = 0; |
| 1764 | int stream_cnt = 0; |
| 1765 | int res = 0; |
John Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 1766 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1767 | exec_name = argv_[0]; |
John Koleszar | 9e50ed7 | 2012-02-15 12:39:38 -0800 | [diff] [blame] | 1768 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1769 | if (argc < 3) |
| 1770 | usage_exit(); |
| 1771 | |
| 1772 | /* Setup default input stream settings */ |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1773 | input.framerate.numerator = 30; |
| 1774 | input.framerate.denominator = 1; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1775 | input.use_i420 = 1; |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 1776 | input.only_i420 = 1; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1777 | |
| 1778 | /* First parse the global configuration values, because we want to apply |
| 1779 | * other parameters on top of the default configuration provided by the |
| 1780 | * codec. |
| 1781 | */ |
| 1782 | argv = argv_dup(argc - 1, argv_ + 1); |
| 1783 | parse_global_config(&global, argv); |
| 1784 | |
Tom Finegan | 44dd327 | 2013-11-20 17:18:28 -0800 | [diff] [blame] | 1785 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1786 | { |
| 1787 | /* Now parse each stream's parameters. Using a local scope here |
| 1788 | * due to the use of 'stream' as loop variable in FOREACH_STREAM |
| 1789 | * loops |
John Koleszar | efd54f8 | 2012-02-13 16:52:18 -0800 | [diff] [blame] | 1790 | */ |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1791 | struct stream_state *stream = NULL; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1792 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1793 | do { |
| 1794 | stream = new_stream(&global, stream); |
| 1795 | stream_cnt++; |
| 1796 | if (!streams) |
| 1797 | streams = stream; |
| 1798 | } while (parse_stream_params(&global, stream, argv)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1799 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1800 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1801 | /* Check for unrecognized options */ |
| 1802 | for (argi = argv; *argi; argi++) |
| 1803 | if (argi[0][0] == '-' && argi[0][1]) |
| 1804 | die("Error: Unrecognized option %s\n", *argi); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1805 | |
Tom Finegan | 249366b | 2013-11-25 12:05:19 -0800 | [diff] [blame] | 1806 | FOREACH_STREAM(check_encoder_config(global.disable_warning_prompt, |
| 1807 | &global, &stream->config.cfg);); |
| 1808 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1809 | /* Handle non-option arguments */ |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1810 | input.filename = argv[0]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1811 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1812 | if (!input.filename) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1813 | usage_exit(); |
John Koleszar | 5329189 | 2010-10-22 14:48:21 -0400 | [diff] [blame] | 1814 | |
John Koleszar | 8dd8287 | 2013-05-06 11:01:35 -0700 | [diff] [blame] | 1815 | #if CONFIG_NON420 |
| 1816 | /* Decide if other chroma subsamplings than 4:2:0 are supported */ |
| 1817 | if (global.codec->fourcc == VP9_FOURCC) |
| 1818 | input.only_i420 = 0; |
| 1819 | #endif |
| 1820 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1821 | for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) { |
John Koleszar | 2bf563c | 2013-03-11 15:03:00 -0700 | [diff] [blame] | 1822 | int frames_in = 0, seen_frames = 0; |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1823 | int64_t estimated_time_left = -1; |
| 1824 | int64_t average_rate = -1; |
| 1825 | off_t lagged_count = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1826 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1827 | open_input_file(&input); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1828 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1829 | /* If the input file doesn't specify its w/h (raw files), try to get |
| 1830 | * the data from the first stream's configuration. |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1831 | */ |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1832 | if (!input.width || !input.height) |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1833 | FOREACH_STREAM( { |
| 1834 | if (stream->config.cfg.g_w && stream->config.cfg.g_h) { |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1835 | input.width = stream->config.cfg.g_w; |
| 1836 | input.height = stream->config.cfg.g_h; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1837 | break; |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1838 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1839 | }); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1840 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1841 | /* Update stream configurations from the input file's parameters */ |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1842 | if (!input.width || !input.height) |
John Koleszar | 34882b9 | 2012-03-01 12:50:40 -0800 | [diff] [blame] | 1843 | fatal("Specify stream dimensions with --width (-w) " |
| 1844 | " and --height (-h)"); |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1845 | FOREACH_STREAM(set_stream_dimensions(stream, input.width, input.height)); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1846 | FOREACH_STREAM(validate_stream_config(stream)); |
John Koleszar | 77e6b45 | 2010-11-03 13:58:40 -0400 | [diff] [blame] | 1847 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1848 | /* Ensure that --passes and --pass are consistent. If --pass is set and |
| 1849 | * --passes=2, ensure --fpf was set. |
| 1850 | */ |
| 1851 | if (global.pass && global.passes == 2) |
| 1852 | FOREACH_STREAM( { |
| 1853 | if (!stream->config.stats_fn) |
| 1854 | die("Stream %d: Must specify --fpf when --pass=%d" |
| 1855 | " and --passes=2\n", stream->index, global.pass); |
| 1856 | }); |
John Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 1857 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1858 | /* Use the frame rate from the file only if none was specified |
| 1859 | * on the command-line. |
| 1860 | */ |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1861 | if (!global.have_framerate) { |
| 1862 | global.framerate.num = input.framerate.numerator; |
| 1863 | global.framerate.den = input.framerate.denominator; |
| 1864 | } |
Timothy B. Terriberry | 44d8949 | 2010-05-26 18:27:51 -0400 | [diff] [blame] | 1865 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1866 | FOREACH_STREAM(set_default_kf_interval(stream, &global)); |
| 1867 | |
| 1868 | /* Show configuration */ |
| 1869 | if (global.verbose && pass == 0) |
| 1870 | FOREACH_STREAM(show_stream_config(stream, &global, &input)); |
| 1871 | |
| 1872 | if (pass == (global.pass ? global.pass - 1 : 0)) { |
| 1873 | if (input.file_type == FILE_TYPE_Y4M) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1874 | /*The Y4M reader does its own allocation. |
| 1875 | Just initialize this here to avoid problems if we never read any |
| 1876 | frames.*/ |
| 1877 | memset(&raw, 0, sizeof(raw)); |
| 1878 | else |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1879 | vpx_img_alloc(&raw, |
| 1880 | input.use_i420 ? VPX_IMG_FMT_I420 |
| 1881 | : VPX_IMG_FMT_YV12, |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 1882 | input.width, input.height, 32); |
John Koleszar | 05239f0 | 2011-06-29 09:53:12 -0400 | [diff] [blame] | 1883 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1884 | FOREACH_STREAM(init_rate_histogram(&stream->rate_hist, |
| 1885 | &stream->config.cfg, |
| 1886 | &global.framerate)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1887 | } |
| 1888 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1889 | FOREACH_STREAM(setup_pass(stream, &global, pass)); |
James Zern | 8cb0971 | 2013-08-14 18:28:42 -0700 | [diff] [blame] | 1890 | FOREACH_STREAM(open_output_file(stream, &global)); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1891 | FOREACH_STREAM(initialize_encoder(stream, &global)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1892 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1893 | frame_avail = 1; |
| 1894 | got_data = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1895 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1896 | while (frame_avail || got_data) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1897 | struct vpx_usec_timer timer; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1898 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1899 | if (!global.limit || frames_in < global.limit) { |
| 1900 | frame_avail = read_frame(&input, &raw); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1901 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1902 | if (frame_avail) |
| 1903 | frames_in++; |
John Koleszar | 2bf563c | 2013-03-11 15:03:00 -0700 | [diff] [blame] | 1904 | seen_frames = frames_in > global.skip_frames ? |
| 1905 | frames_in - global.skip_frames : 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1906 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1907 | if (!global.quiet) { |
John Koleszar | 2bf563c | 2013-03-11 15:03:00 -0700 | [diff] [blame] | 1908 | float fps = usec_to_fps(cx_time, seen_frames); |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1909 | fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes); |
| 1910 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1911 | if (stream_cnt == 1) |
| 1912 | fprintf(stderr, |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1913 | "frame %4d/%-4d %7"PRId64"B ", |
| 1914 | frames_in, streams->frames_out, (int64_t)streams->nbytes); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1915 | else |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1916 | fprintf(stderr, "frame %4d ", frames_in); |
| 1917 | |
John Koleszar | ebf8b9f | 2013-02-27 11:14:23 -0800 | [diff] [blame] | 1918 | fprintf(stderr, "%7"PRId64" %s %.2f %s ", |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1919 | cx_time > 9999999 ? cx_time / 1000 : cx_time, |
| 1920 | cx_time > 9999999 ? "ms" : "us", |
Yaowu Xu | 014acfa | 2013-09-17 16:31:46 -0700 | [diff] [blame] | 1921 | fps >= 1.0 ? fps : fps * 60, |
| 1922 | fps >= 1.0 ? "fps" : "fpm"); |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1923 | print_time("ETA", estimated_time_left); |
| 1924 | fprintf(stderr, "\033[K"); |
John Koleszar | 3245d46 | 2010-06-10 17:10:12 -0400 | [diff] [blame] | 1925 | } |
| 1926 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1927 | } else |
| 1928 | frame_avail = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1929 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1930 | if (frames_in > global.skip_frames) { |
| 1931 | vpx_usec_timer_start(&timer); |
| 1932 | FOREACH_STREAM(encode_frame(stream, &global, |
| 1933 | frame_avail ? &raw : NULL, |
| 1934 | frames_in)); |
| 1935 | vpx_usec_timer_mark(&timer); |
John Koleszar | ebf8b9f | 2013-02-27 11:14:23 -0800 | [diff] [blame] | 1936 | cx_time += vpx_usec_timer_elapsed(&timer); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1937 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1938 | FOREACH_STREAM(update_quantizer_histogram(stream)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1939 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1940 | got_data = 0; |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1941 | FOREACH_STREAM(get_cx_data(stream, &global, &got_data)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1942 | |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1943 | if (!got_data && input.length && !streams->frames_out) { |
John Koleszar | 2bf563c | 2013-03-11 15:03:00 -0700 | [diff] [blame] | 1944 | lagged_count = global.limit ? seen_frames : ftello(input.file); |
John Koleszar | ebf8b9f | 2013-02-27 11:14:23 -0800 | [diff] [blame] | 1945 | } else if (input.length) { |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1946 | int64_t remaining; |
| 1947 | int64_t rate; |
| 1948 | |
| 1949 | if (global.limit) { |
John Koleszar | 2bf563c | 2013-03-11 15:03:00 -0700 | [diff] [blame] | 1950 | int frame_in_lagged = (seen_frames - lagged_count) * 1000; |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1951 | |
| 1952 | rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0; |
John Koleszar | 2bf563c | 2013-03-11 15:03:00 -0700 | [diff] [blame] | 1953 | remaining = 1000 * (global.limit - global.skip_frames |
| 1954 | - seen_frames + lagged_count); |
John Koleszar | 25b6e9f | 2013-02-12 21:17:56 -0800 | [diff] [blame] | 1955 | } else { |
| 1956 | off_t input_pos = ftello(input.file); |
| 1957 | off_t input_pos_lagged = input_pos - lagged_count; |
| 1958 | int64_t limit = input.length; |
| 1959 | |
| 1960 | rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0; |
| 1961 | remaining = limit - input_pos + lagged_count; |
| 1962 | } |
| 1963 | |
| 1964 | average_rate = (average_rate <= 0) |
| 1965 | ? rate |
| 1966 | : (average_rate * 7 + rate) / 8; |
| 1967 | estimated_time_left = average_rate ? remaining / average_rate : -1; |
| 1968 | } |
| 1969 | |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 1970 | if (got_data && global.test_decode != TEST_DECODE_OFF) |
John Koleszar | b3c350a | 2013-03-13 12:15:43 -0700 | [diff] [blame] | 1971 | FOREACH_STREAM(test_decode(stream, global.test_decode, global.codec)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1972 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1973 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1974 | fflush(stdout); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1975 | } |
| 1976 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1977 | if (stream_cnt > 1) |
| 1978 | fprintf(stderr, "\n"); |
John Koleszar | 3fde996 | 2011-06-20 17:09:29 -0400 | [diff] [blame] | 1979 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1980 | if (!global.quiet) |
| 1981 | FOREACH_STREAM(fprintf( |
| 1982 | stderr, |
| 1983 | "\rPass %d/%d frame %4d/%-4d %7"PRId64"B %7lub/f %7"PRId64"b/s" |
| 1984 | " %7"PRId64" %s (%.2f fps)\033[K\n", pass + 1, |
| 1985 | global.passes, frames_in, stream->frames_out, (int64_t)stream->nbytes, |
John Koleszar | 2bf563c | 2013-03-11 15:03:00 -0700 | [diff] [blame] | 1986 | seen_frames ? (unsigned long)(stream->nbytes * 8 / seen_frames) : 0, |
| 1987 | seen_frames ? (int64_t)stream->nbytes * 8 |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1988 | * (int64_t)global.framerate.num / global.framerate.den |
John Koleszar | 2bf563c | 2013-03-11 15:03:00 -0700 | [diff] [blame] | 1989 | / seen_frames |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1990 | : 0, |
| 1991 | stream->cx_time > 9999999 ? stream->cx_time / 1000 : stream->cx_time, |
| 1992 | stream->cx_time > 9999999 ? "ms" : "us", |
John Koleszar | 2bf563c | 2013-03-11 15:03:00 -0700 | [diff] [blame] | 1993 | usec_to_fps(stream->cx_time, seen_frames)); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1994 | ); |
John Koleszar | c96f8e2 | 2011-06-21 16:42:33 -0400 | [diff] [blame] | 1995 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1996 | if (global.show_psnr) |
| 1997 | FOREACH_STREAM(show_psnr(stream)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 1998 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 1999 | FOREACH_STREAM(vpx_codec_destroy(&stream->encoder)); |
| 2000 | |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 2001 | if (global.test_decode != TEST_DECODE_OFF) { |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2002 | FOREACH_STREAM(vpx_codec_destroy(&stream->decoder)); |
Yaowu Xu | f798f9e | 2012-03-07 12:25:50 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2005 | close_input_file(&input); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2006 | |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 2007 | if (global.test_decode == TEST_DECODE_FATAL) { |
| 2008 | FOREACH_STREAM(res |= stream->mismatch_seen); |
| 2009 | } |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2010 | FOREACH_STREAM(close_output_file(stream, global.codec->fourcc)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2011 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2012 | FOREACH_STREAM(stats_close(&stream->stats, global.passes - 1)); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2013 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2014 | if (global.pass) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2015 | break; |
| 2016 | } |
| 2017 | |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2018 | if (global.show_q_hist_buckets) |
| 2019 | FOREACH_STREAM(show_q_histogram(stream->counts, |
| 2020 | global.show_q_hist_buckets)); |
| 2021 | |
| 2022 | if (global.show_rate_hist_buckets) |
| 2023 | FOREACH_STREAM(show_rate_histogram(&stream->rate_hist, |
| 2024 | &stream->config.cfg, |
| 2025 | global.show_rate_hist_buckets)); |
| 2026 | FOREACH_STREAM(destroy_rate_histogram(&stream->rate_hist)); |
| 2027 | |
Ronald S. Bultje | 3121497 | 2012-10-11 10:13:48 -0700 | [diff] [blame] | 2028 | #if CONFIG_INTERNAL_STATS |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2029 | /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now, |
| 2030 | * to match some existing utilities. |
| 2031 | */ |
Yaowu Xu | 47e784e | 2013-10-16 16:29:28 -0700 | [diff] [blame] | 2032 | if (!(global.pass == 1 && global.passes == 2)) |
| 2033 | FOREACH_STREAM({ |
| 2034 | FILE *f = fopen("opsnr.stt", "a"); |
| 2035 | if (stream->mismatch_seen) { |
| 2036 | fprintf(f, "First mismatch occurred in frame %d\n", |
| 2037 | stream->mismatch_seen); |
| 2038 | } else { |
| 2039 | fprintf(f, "No mismatch detected in recon buffers\n"); |
| 2040 | } |
| 2041 | fclose(f); |
| 2042 | }); |
Ronald S. Bultje | 3121497 | 2012-10-11 10:13:48 -0700 | [diff] [blame] | 2043 | #endif |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2044 | |
| 2045 | vpx_img_free(&raw); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 2046 | free(argv); |
John Koleszar | 6ad3b74 | 2012-11-06 12:02:42 -0800 | [diff] [blame] | 2047 | free(streams); |
Ronald S. Bultje | 9837bf4 | 2013-02-15 16:31:02 -0800 | [diff] [blame] | 2048 | return res ? EXIT_FAILURE : EXIT_SUCCESS; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 2049 | } |