2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
17 #include "./tools_common.h"
19 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
20 #include "vpx/vp8cx.h"
23 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
24 #include "vpx/vp8dx.h"
27 #if defined(_WIN32) || defined(__OS2__)
32 #define _setmode setmode
33 #define _fileno fileno
34 #define _O_BINARY O_BINARY
38 #define LOG_ERROR(label) do {\
39 const char *l = label;\
43 fprintf(stderr, "%s: ", l);\
44 vfprintf(stderr, fmt, ap);\
45 fprintf(stderr, "\n");\
50 FILE *set_binary_mode(FILE *stream) {
52 #if defined(_WIN32) || defined(__OS2__)
53 _setmode(_fileno(stream), _O_BINARY);
58 void die(const char *fmt, ...) {
63 void fatal(const char *fmt, ...) {
68 void warn(const char *fmt, ...) {
72 void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
73 const char *detail = vpx_codec_error_detail(ctx);
75 printf("%s: %s\n", s, vpx_codec_error(ctx));
77 printf(" %s\n", detail);
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;
86 const int bytespp = (input_ctx->fmt & VPX_IMG_FMT_HIGH) ? 2 : 1;
88 for (plane = 0; plane < 3; ++plane) {
90 const int w = vpx_img_plane_width(yuv_frame, plane);
91 const int h = vpx_img_plane_height(yuv_frame, plane);
94 /* Determine the correct plane based on the image format. The for-loop
95 * always counts in Y,U,V order, but this may not match the order of
100 ptr = yuv_frame->planes[
101 yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V : VPX_PLANE_U];
104 ptr = yuv_frame->planes[
105 yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U : VPX_PLANE_V];
108 ptr = yuv_frame->planes[plane];
111 for (r = 0; r < h; ++r) {
112 size_t needed = w * bytespp;
113 size_t buf_position = 0;
114 const size_t left = detect->buf_read - detect->position;
116 const size_t more = (left < needed) ? left : needed;
117 memcpy(ptr, detect->buf + detect->position, more);
120 detect->position += more;
123 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
126 ptr += yuv_frame->stride[plane];
133 static const VpxInterface vpx_encoders[] = {
134 #if CONFIG_VP8_ENCODER
135 {"vp8", VP8_FOURCC, &vpx_codec_vp8_cx},
138 #if CONFIG_VP9_ENCODER
139 {"vp9", VP9_FOURCC, &vpx_codec_vp9_cx},
143 int get_vpx_encoder_count() {
144 return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]);
147 const VpxInterface *get_vpx_encoder_by_index(int i) {
148 return &vpx_encoders[i];
151 const VpxInterface *get_vpx_encoder_by_name(const char *name) {
154 for (i = 0; i < get_vpx_encoder_count(); ++i) {
155 const VpxInterface *encoder = get_vpx_encoder_by_index(i);
156 if (strcmp(encoder->name, name) == 0)
163 static const VpxInterface vpx_decoders[] = {
164 #if CONFIG_VP8_DECODER
165 {"vp8", VP8_FOURCC, &vpx_codec_vp8_dx},
168 #if CONFIG_VP9_DECODER
169 {"vp9", VP9_FOURCC, &vpx_codec_vp9_dx},
173 int get_vpx_decoder_count() {
174 return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]);
177 const VpxInterface *get_vpx_decoder_by_index(int i) {
178 return &vpx_decoders[i];
181 const VpxInterface *get_vpx_decoder_by_name(const char *name) {
184 for (i = 0; i < get_vpx_decoder_count(); ++i) {
185 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
186 if (strcmp(decoder->name, name) == 0)
193 const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) {
196 for (i = 0; i < get_vpx_decoder_count(); ++i) {
197 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
198 if (decoder->fourcc == fourcc)
205 // TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part
206 // of vpx_image_t support
207 int vpx_img_plane_width(const vpx_image_t *img, int plane) {
208 if (plane > 0 && img->x_chroma_shift > 0)
209 return (img->d_w + 1) >> img->x_chroma_shift;
214 int vpx_img_plane_height(const vpx_image_t *img, int plane) {
215 if (plane > 0 && img->y_chroma_shift > 0)
216 return (img->d_h + 1) >> img->y_chroma_shift;
221 void vpx_img_write(const vpx_image_t *img, FILE *file) {
224 for (plane = 0; plane < 3; ++plane) {
225 const unsigned char *buf = img->planes[plane];
226 const int stride = img->stride[plane];
227 const int w = vpx_img_plane_width(img, plane);
228 const int h = vpx_img_plane_height(img, plane);
231 for (y = 0; y < h; ++y) {
232 fwrite(buf, 1, w, file);
238 int vpx_img_read(vpx_image_t *img, FILE *file) {
241 for (plane = 0; plane < 3; ++plane) {
242 unsigned char *buf = img->planes[plane];
243 const int stride = img->stride[plane];
244 const int w = vpx_img_plane_width(img, plane);
245 const int h = vpx_img_plane_height(img, plane);
248 for (y = 0; y < h; ++y) {
249 if (fread(buf, 1, w, file) != w)
258 // TODO(dkovalev) change sse_to_psnr signature: double -> int64_t
259 double sse_to_psnr(double samples, double peak, double sse) {
260 static const double kMaxPSNR = 100.0;
263 const double psnr = 10.0 * log10(samples * peak * peak / sse);
264 return psnr > kMaxPSNR ? kMaxPSNR : psnr;