John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 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 |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 10 | |
Dmitry Kovalev | 2dad0e1 | 2014-02-27 14:00:41 -0800 | [diff] [blame] | 11 | #include <math.h> |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 12 | #include <stdarg.h> |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 13 | #include <stdio.h> |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 14 | #include <stdlib.h> |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 15 | #include <string.h> |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 16 | |
Dmitry Kovalev | 2dad0e1 | 2014-02-27 14:00:41 -0800 | [diff] [blame] | 17 | #include "./tools_common.h" |
| 18 | |
Dmitry Kovalev | 70d9664 | 2014-02-11 21:12:23 -0800 | [diff] [blame] | 19 | #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER |
| 20 | #include "vpx/vp8cx.h" |
| 21 | #endif |
| 22 | |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 23 | #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER |
| 24 | #include "vpx/vp8dx.h" |
| 25 | #endif |
| 26 | |
John Koleszar | 82b1a34 | 2012-11-06 12:08:05 -0800 | [diff] [blame] | 27 | #if defined(_WIN32) || defined(__OS2__) |
John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 28 | #include <io.h> |
| 29 | #include <fcntl.h> |
John Koleszar | 82b1a34 | 2012-11-06 12:08:05 -0800 | [diff] [blame] | 30 | |
| 31 | #ifdef __OS2__ |
| 32 | #define _setmode setmode |
| 33 | #define _fileno fileno |
| 34 | #define _O_BINARY O_BINARY |
| 35 | #endif |
John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 36 | #endif |
| 37 | |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 38 | #define LOG_ERROR(label) do {\ |
| 39 | const char *l = label;\ |
| 40 | va_list ap;\ |
| 41 | va_start(ap, fmt);\ |
| 42 | if (l)\ |
| 43 | fprintf(stderr, "%s: ", l);\ |
| 44 | vfprintf(stderr, fmt, ap);\ |
| 45 | fprintf(stderr, "\n");\ |
| 46 | va_end(ap);\ |
| 47 | } while (0) |
| 48 | |
| 49 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 50 | FILE *set_binary_mode(FILE *stream) { |
| 51 | (void)stream; |
John Koleszar | 82b1a34 | 2012-11-06 12:08:05 -0800 | [diff] [blame] | 52 | #if defined(_WIN32) || defined(__OS2__) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 53 | _setmode(_fileno(stream), _O_BINARY); |
John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 54 | #endif |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 55 | return stream; |
John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 56 | } |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 57 | |
| 58 | void die(const char *fmt, ...) { |
| 59 | LOG_ERROR(NULL); |
| 60 | usage_exit(); |
| 61 | } |
| 62 | |
| 63 | void fatal(const char *fmt, ...) { |
| 64 | LOG_ERROR("Fatal"); |
| 65 | exit(EXIT_FAILURE); |
| 66 | } |
| 67 | |
| 68 | void warn(const char *fmt, ...) { |
| 69 | LOG_ERROR("Warning"); |
| 70 | } |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 71 | |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 72 | void die_codec(vpx_codec_ctx_t *ctx, const char *s) { |
| 73 | const char *detail = vpx_codec_error_detail(ctx); |
| 74 | |
| 75 | printf("%s: %s\n", s, vpx_codec_error(ctx)); |
| 76 | if (detail) |
| 77 | printf(" %s\n", detail); |
| 78 | exit(EXIT_FAILURE); |
| 79 | } |
| 80 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 81 | int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame) { |
| 82 | FILE *f = input_ctx->file; |
| 83 | struct FileTypeDetectionBuffer *detect = &input_ctx->detect; |
| 84 | int plane = 0; |
| 85 | int shortread = 0; |
| 86 | |
| 87 | for (plane = 0; plane < 3; ++plane) { |
| 88 | uint8_t *ptr; |
| 89 | const int w = (plane ? (1 + yuv_frame->d_w) / 2 : yuv_frame->d_w); |
| 90 | const int h = (plane ? (1 + yuv_frame->d_h) / 2 : yuv_frame->d_h); |
| 91 | int r; |
| 92 | |
| 93 | /* Determine the correct plane based on the image format. The for-loop |
| 94 | * always counts in Y,U,V order, but this may not match the order of |
| 95 | * the data on disk. |
| 96 | */ |
| 97 | switch (plane) { |
| 98 | case 1: |
| 99 | ptr = yuv_frame->planes[ |
| 100 | yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V : VPX_PLANE_U]; |
| 101 | break; |
| 102 | case 2: |
| 103 | ptr = yuv_frame->planes[ |
| 104 | yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U : VPX_PLANE_V]; |
| 105 | break; |
| 106 | default: |
| 107 | ptr = yuv_frame->planes[plane]; |
| 108 | } |
| 109 | |
| 110 | for (r = 0; r < h; ++r) { |
| 111 | size_t needed = w; |
| 112 | size_t buf_position = 0; |
| 113 | const size_t left = detect->buf_read - detect->position; |
| 114 | if (left > 0) { |
| 115 | const size_t more = (left < needed) ? left : needed; |
| 116 | memcpy(ptr, detect->buf + detect->position, more); |
| 117 | buf_position = more; |
| 118 | needed -= more; |
| 119 | detect->position += more; |
| 120 | } |
| 121 | if (needed > 0) { |
| 122 | shortread |= (fread(ptr + buf_position, 1, needed, f) < needed); |
| 123 | } |
| 124 | |
| 125 | ptr += yuv_frame->stride[plane]; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return shortread; |
| 130 | } |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 131 | |
Dmitry Kovalev | 70d9664 | 2014-02-11 21:12:23 -0800 | [diff] [blame] | 132 | static const VpxInterface vpx_encoders[] = { |
| 133 | #if CONFIG_VP8_ENCODER |
| 134 | {"vp8", VP8_FOURCC, &vpx_codec_vp8_cx}, |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 135 | #endif |
Dmitry Kovalev | 70d9664 | 2014-02-11 21:12:23 -0800 | [diff] [blame] | 136 | |
| 137 | #if CONFIG_VP9_ENCODER |
| 138 | {"vp9", VP9_FOURCC, &vpx_codec_vp9_cx}, |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 139 | #endif |
Dmitry Kovalev | 70d9664 | 2014-02-11 21:12:23 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | int get_vpx_encoder_count() { |
| 143 | return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]); |
| 144 | } |
| 145 | |
| 146 | const VpxInterface *get_vpx_encoder_by_index(int i) { |
| 147 | return &vpx_encoders[i]; |
| 148 | } |
| 149 | |
| 150 | const VpxInterface *get_vpx_encoder_by_name(const char *name) { |
| 151 | int i; |
| 152 | |
| 153 | for (i = 0; i < get_vpx_encoder_count(); ++i) { |
| 154 | const VpxInterface *encoder = get_vpx_encoder_by_index(i); |
| 155 | if (strcmp(encoder->name, name) == 0) |
| 156 | return encoder; |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 157 | } |
Dmitry Kovalev | 70d9664 | 2014-02-11 21:12:23 -0800 | [diff] [blame] | 158 | |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | static const VpxInterface vpx_decoders[] = { |
| 163 | #if CONFIG_VP8_DECODER |
| 164 | {"vp8", VP8_FOURCC, &vpx_codec_vp8_dx}, |
| 165 | #endif |
| 166 | |
| 167 | #if CONFIG_VP9_DECODER |
| 168 | {"vp9", VP9_FOURCC, &vpx_codec_vp9_dx}, |
| 169 | #endif |
| 170 | }; |
| 171 | |
| 172 | int get_vpx_decoder_count() { |
| 173 | return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]); |
| 174 | } |
| 175 | |
| 176 | const VpxInterface *get_vpx_decoder_by_index(int i) { |
| 177 | return &vpx_decoders[i]; |
| 178 | } |
| 179 | |
| 180 | const VpxInterface *get_vpx_decoder_by_name(const char *name) { |
| 181 | int i; |
| 182 | |
| 183 | for (i = 0; i < get_vpx_decoder_count(); ++i) { |
| 184 | const VpxInterface *const decoder = get_vpx_decoder_by_index(i); |
| 185 | if (strcmp(decoder->name, name) == 0) |
| 186 | return decoder; |
| 187 | } |
| 188 | |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) { |
| 193 | int i; |
| 194 | |
| 195 | for (i = 0; i < get_vpx_decoder_count(); ++i) { |
| 196 | const VpxInterface *const decoder = get_vpx_decoder_by_index(i); |
| 197 | if (decoder->fourcc == fourcc) |
| 198 | return decoder; |
| 199 | } |
| 200 | |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 201 | return NULL; |
| 202 | } |
| 203 | |
Dmitry Kovalev | 2bdd43d | 2014-02-12 18:36:36 -0800 | [diff] [blame] | 204 | // TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part |
| 205 | // of vpx_image_t support |
| 206 | int vpx_img_plane_width(const vpx_image_t *img, int plane) { |
| 207 | if (plane > 0 && img->x_chroma_shift > 0) |
| 208 | return (img->d_w + 1) >> img->x_chroma_shift; |
| 209 | else |
| 210 | return img->d_w; |
| 211 | } |
| 212 | |
| 213 | int vpx_img_plane_height(const vpx_image_t *img, int plane) { |
| 214 | if (plane > 0 && img->y_chroma_shift > 0) |
| 215 | return (img->d_h + 1) >> img->y_chroma_shift; |
| 216 | else |
| 217 | return img->d_h; |
| 218 | } |
| 219 | |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 220 | void vpx_img_write(const vpx_image_t *img, FILE *file) { |
Dmitry Kovalev | 2bdd43d | 2014-02-12 18:36:36 -0800 | [diff] [blame] | 221 | int plane; |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 222 | |
| 223 | for (plane = 0; plane < 3; ++plane) { |
| 224 | const unsigned char *buf = img->planes[plane]; |
| 225 | const int stride = img->stride[plane]; |
Dmitry Kovalev | 2bdd43d | 2014-02-12 18:36:36 -0800 | [diff] [blame] | 226 | const int w = vpx_img_plane_width(img, plane); |
| 227 | const int h = vpx_img_plane_height(img, plane); |
| 228 | int y; |
Dmitry Kovalev | 592936b | 2014-02-07 11:37:39 -0800 | [diff] [blame] | 229 | |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 230 | for (y = 0; y < h; ++y) { |
| 231 | fwrite(buf, 1, w, file); |
| 232 | buf += stride; |
| 233 | } |
| 234 | } |
| 235 | } |
Dmitry Kovalev | 592936b | 2014-02-07 11:37:39 -0800 | [diff] [blame] | 236 | |
| 237 | int vpx_img_read(vpx_image_t *img, FILE *file) { |
| 238 | int plane; |
| 239 | |
| 240 | for (plane = 0; plane < 3; ++plane) { |
| 241 | unsigned char *buf = img->planes[plane]; |
| 242 | const int stride = img->stride[plane]; |
Dmitry Kovalev | 2bdd43d | 2014-02-12 18:36:36 -0800 | [diff] [blame] | 243 | const int w = vpx_img_plane_width(img, plane); |
| 244 | const int h = vpx_img_plane_height(img, plane); |
Dmitry Kovalev | 592936b | 2014-02-07 11:37:39 -0800 | [diff] [blame] | 245 | int y; |
| 246 | |
| 247 | for (y = 0; y < h; ++y) { |
| 248 | if (fread(buf, 1, w, file) != w) |
| 249 | return 0; |
| 250 | buf += stride; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return 1; |
| 255 | } |
| 256 | |
Dmitry Kovalev | 2dad0e1 | 2014-02-27 14:00:41 -0800 | [diff] [blame] | 257 | // TODO(dkovalev) change sse_to_psnr signature: double -> int64_t |
| 258 | double sse_to_psnr(double samples, double peak, double sse) { |
| 259 | static const double kMaxPSNR = 100.0; |
| 260 | |
| 261 | if (sse > 0.0) { |
| 262 | const double psnr = 10.0 * log10(samples * peak * peak / sse); |
| 263 | return psnr > kMaxPSNR ? kMaxPSNR : psnr; |
| 264 | } else { |
| 265 | return kMaxPSNR; |
| 266 | } |
| 267 | } |