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_ENCODER || CONFIG_VP9_ENCODER
19 #include "vpx/vp8cx.h"
22 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
23 #include "vpx/vp8dx.h"
26 #if defined(_WIN32) || defined(__OS2__)
31 #define _setmode setmode
32 #define _fileno fileno
33 #define _O_BINARY O_BINARY
37 #define LOG_ERROR(label) do {\
38 const char *l = label;\
42 fprintf(stderr, "%s: ", l);\
43 vfprintf(stderr, fmt, ap);\
44 fprintf(stderr, "\n");\
49 FILE *set_binary_mode(FILE *stream) {
51 #if defined(_WIN32) || defined(__OS2__)
52 _setmode(_fileno(stream), _O_BINARY);
57 void die(const char *fmt, ...) {
62 void fatal(const char *fmt, ...) {
67 void warn(const char *fmt, ...) {
71 void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
72 const char *detail = vpx_codec_error_detail(ctx);
74 printf("%s: %s\n", s, vpx_codec_error(ctx));
76 printf(" %s\n", detail);
80 uint16_t mem_get_le16(const void *data) {
82 const uint8_t *mem = (const uint8_t*)data;
89 uint32_t mem_get_le32(const void *data) {
91 const uint8_t *mem = (const uint8_t*)data;
100 int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame) {
101 FILE *f = input_ctx->file;
102 struct FileTypeDetectionBuffer *detect = &input_ctx->detect;
106 for (plane = 0; plane < 3; ++plane) {
108 const int w = (plane ? (1 + yuv_frame->d_w) / 2 : yuv_frame->d_w);
109 const int h = (plane ? (1 + yuv_frame->d_h) / 2 : yuv_frame->d_h);
112 /* Determine the correct plane based on the image format. The for-loop
113 * always counts in Y,U,V order, but this may not match the order of
118 ptr = yuv_frame->planes[
119 yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V : VPX_PLANE_U];
122 ptr = yuv_frame->planes[
123 yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U : VPX_PLANE_V];
126 ptr = yuv_frame->planes[plane];
129 for (r = 0; r < h; ++r) {
131 size_t buf_position = 0;
132 const size_t left = detect->buf_read - detect->position;
134 const size_t more = (left < needed) ? left : needed;
135 memcpy(ptr, detect->buf + detect->position, more);
138 detect->position += more;
141 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
144 ptr += yuv_frame->stride[plane];
151 static const VpxInterface vpx_encoders[] = {
152 #if CONFIG_VP8_ENCODER
153 {"vp8", VP8_FOURCC, &vpx_codec_vp8_cx},
156 #if CONFIG_VP9_ENCODER
157 {"vp9", VP9_FOURCC, &vpx_codec_vp9_cx},
161 int get_vpx_encoder_count() {
162 return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]);
165 const VpxInterface *get_vpx_encoder_by_index(int i) {
166 return &vpx_encoders[i];
169 const VpxInterface *get_vpx_encoder_by_name(const char *name) {
172 for (i = 0; i < get_vpx_encoder_count(); ++i) {
173 const VpxInterface *encoder = get_vpx_encoder_by_index(i);
174 if (strcmp(encoder->name, name) == 0)
181 static const VpxInterface vpx_decoders[] = {
182 #if CONFIG_VP8_DECODER
183 {"vp8", VP8_FOURCC, &vpx_codec_vp8_dx},
186 #if CONFIG_VP9_DECODER
187 {"vp9", VP9_FOURCC, &vpx_codec_vp9_dx},
191 int get_vpx_decoder_count() {
192 return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]);
195 const VpxInterface *get_vpx_decoder_by_index(int i) {
196 return &vpx_decoders[i];
199 const VpxInterface *get_vpx_decoder_by_name(const char *name) {
202 for (i = 0; i < get_vpx_decoder_count(); ++i) {
203 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
204 if (strcmp(decoder->name, name) == 0)
211 const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) {
214 for (i = 0; i < get_vpx_decoder_count(); ++i) {
215 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
216 if (decoder->fourcc == fourcc)
223 void vpx_img_write(const vpx_image_t *img, FILE *file) {
226 for (plane = 0; plane < 3; ++plane) {
227 const unsigned char *buf = img->planes[plane];
228 const int stride = img->stride[plane];
229 const int w = plane ? (img->d_w + 1) >> 1 : img->d_w;
230 const int h = plane ? (img->d_h + 1) >> 1 : img->d_h;
232 for (y = 0; y < h; ++y) {
233 fwrite(buf, 1, w, file);
239 int vpx_img_read(vpx_image_t *img, FILE *file) {
242 for (plane = 0; plane < 3; ++plane) {
243 unsigned char *buf = img->planes[plane];
244 const int stride = img->stride[plane];
245 const int w = plane ? (img->d_w + 1) >> 1 : img->d_w;
246 const int h = plane ? (img->d_h + 1) >> 1 : img->d_h;
249 for (y = 0; y < h; ++y) {
250 if (fread(buf, 1, w, file) != w)