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.
11 #include "tools_common.h"
18 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
19 #include "vpx/vp8dx.h"
22 #if defined(_WIN32) || defined(__OS2__)
27 #define _setmode setmode
28 #define _fileno fileno
29 #define _O_BINARY O_BINARY
33 #define LOG_ERROR(label) do {\
34 const char *l = label;\
38 fprintf(stderr, "%s: ", l);\
39 vfprintf(stderr, fmt, ap);\
40 fprintf(stderr, "\n");\
45 FILE *set_binary_mode(FILE *stream) {
47 #if defined(_WIN32) || defined(__OS2__)
48 _setmode(_fileno(stream), _O_BINARY);
53 void die(const char *fmt, ...) {
58 void fatal(const char *fmt, ...) {
63 void warn(const char *fmt, ...) {
67 void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
68 const char *detail = vpx_codec_error_detail(ctx);
70 printf("%s: %s\n", s, vpx_codec_error(ctx));
72 printf(" %s\n", detail);
76 uint16_t mem_get_le16(const void *data) {
78 const uint8_t *mem = (const uint8_t*)data;
85 uint32_t mem_get_le32(const void *data) {
87 const uint8_t *mem = (const uint8_t*)data;
96 int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame) {
97 FILE *f = input_ctx->file;
98 struct FileTypeDetectionBuffer *detect = &input_ctx->detect;
102 for (plane = 0; plane < 3; ++plane) {
104 const int w = (plane ? (1 + yuv_frame->d_w) / 2 : yuv_frame->d_w);
105 const int h = (plane ? (1 + yuv_frame->d_h) / 2 : yuv_frame->d_h);
108 /* Determine the correct plane based on the image format. The for-loop
109 * always counts in Y,U,V order, but this may not match the order of
114 ptr = yuv_frame->planes[
115 yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V : VPX_PLANE_U];
118 ptr = yuv_frame->planes[
119 yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U : VPX_PLANE_V];
122 ptr = yuv_frame->planes[plane];
125 for (r = 0; r < h; ++r) {
127 size_t buf_position = 0;
128 const size_t left = detect->buf_read - detect->position;
130 const size_t more = (left < needed) ? left : needed;
131 memcpy(ptr, detect->buf + detect->position, more);
134 detect->position += more;
137 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
140 ptr += yuv_frame->stride[plane];
147 vpx_codec_iface_t *get_codec_interface(unsigned int fourcc) {
149 #if CONFIG_VP8_DECODER
151 return vpx_codec_vp8_dx();
153 #if CONFIG_VP9_DECODER
155 return vpx_codec_vp9_dx();
163 void vpx_img_write(const vpx_image_t *img, FILE *file) {
166 for (plane = 0; plane < 3; ++plane) {
167 const unsigned char *buf = img->planes[plane];
168 const int stride = img->stride[plane];
169 const int w = plane ? (img->d_w + 1) >> 1 : img->d_w;
170 const int h = plane ? (img->d_h + 1) >> 1 : img->d_h;
171 for (y = 0; y < h; ++y) {
172 fwrite(buf, 1, w, file);