Merge "Stop gating non420 features with a configure flag."
[aom.git] / tools_common.c
blobf0e160697b25bd20f4ae1b764e210808ea0c5497
1 /*
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.
9 */
11 #include "tools_common.h"
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
18 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
19 #include "vpx/vp8cx.h"
20 #endif
22 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
23 #include "vpx/vp8dx.h"
24 #endif
26 #if defined(_WIN32) || defined(__OS2__)
27 #include <io.h>
28 #include <fcntl.h>
30 #ifdef __OS2__
31 #define _setmode setmode
32 #define _fileno fileno
33 #define _O_BINARY O_BINARY
34 #endif
35 #endif
37 #define LOG_ERROR(label) do {\
38 const char *l = label;\
39 va_list ap;\
40 va_start(ap, fmt);\
41 if (l)\
42 fprintf(stderr, "%s: ", l);\
43 vfprintf(stderr, fmt, ap);\
44 fprintf(stderr, "\n");\
45 va_end(ap);\
46 } while (0)
49 FILE *set_binary_mode(FILE *stream) {
50 (void)stream;
51 #if defined(_WIN32) || defined(__OS2__)
52 _setmode(_fileno(stream), _O_BINARY);
53 #endif
54 return stream;
57 void die(const char *fmt, ...) {
58 LOG_ERROR(NULL);
59 usage_exit();
62 void fatal(const char *fmt, ...) {
63 LOG_ERROR("Fatal");
64 exit(EXIT_FAILURE);
67 void warn(const char *fmt, ...) {
68 LOG_ERROR("Warning");
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));
75 if (detail)
76 printf(" %s\n", detail);
77 exit(EXIT_FAILURE);
80 uint16_t mem_get_le16(const void *data) {
81 uint16_t val;
82 const uint8_t *mem = (const uint8_t*)data;
84 val = mem[1] << 8;
85 val |= mem[0];
86 return val;
89 uint32_t mem_get_le32(const void *data) {
90 uint32_t val;
91 const uint8_t *mem = (const uint8_t*)data;
93 val = mem[3] << 24;
94 val |= mem[2] << 16;
95 val |= mem[1] << 8;
96 val |= mem[0];
97 return val;
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;
103 int plane = 0;
104 int shortread = 0;
106 for (plane = 0; plane < 3; ++plane) {
107 uint8_t *ptr;
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);
110 int r;
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
114 * the data on disk.
116 switch (plane) {
117 case 1:
118 ptr = yuv_frame->planes[
119 yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V : VPX_PLANE_U];
120 break;
121 case 2:
122 ptr = yuv_frame->planes[
123 yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U : VPX_PLANE_V];
124 break;
125 default:
126 ptr = yuv_frame->planes[plane];
129 for (r = 0; r < h; ++r) {
130 size_t needed = w;
131 size_t buf_position = 0;
132 const size_t left = detect->buf_read - detect->position;
133 if (left > 0) {
134 const size_t more = (left < needed) ? left : needed;
135 memcpy(ptr, detect->buf + detect->position, more);
136 buf_position = more;
137 needed -= more;
138 detect->position += more;
140 if (needed > 0) {
141 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
144 ptr += yuv_frame->stride[plane];
148 return shortread;
151 static const VpxInterface vpx_encoders[] = {
152 #if CONFIG_VP8_ENCODER
153 {"vp8", VP8_FOURCC, &vpx_codec_vp8_cx},
154 #endif
156 #if CONFIG_VP9_ENCODER
157 {"vp9", VP9_FOURCC, &vpx_codec_vp9_cx},
158 #endif
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) {
170 int i;
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)
175 return encoder;
178 return NULL;
181 static const VpxInterface vpx_decoders[] = {
182 #if CONFIG_VP8_DECODER
183 {"vp8", VP8_FOURCC, &vpx_codec_vp8_dx},
184 #endif
186 #if CONFIG_VP9_DECODER
187 {"vp9", VP9_FOURCC, &vpx_codec_vp9_dx},
188 #endif
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) {
200 int i;
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)
205 return decoder;
208 return NULL;
211 const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) {
212 int i;
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)
217 return decoder;
220 return NULL;
223 // TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part
224 // of vpx_image_t support
225 int vpx_img_plane_width(const vpx_image_t *img, int plane) {
226 if (plane > 0 && img->x_chroma_shift > 0)
227 return (img->d_w + 1) >> img->x_chroma_shift;
228 else
229 return img->d_w;
232 int vpx_img_plane_height(const vpx_image_t *img, int plane) {
233 if (plane > 0 && img->y_chroma_shift > 0)
234 return (img->d_h + 1) >> img->y_chroma_shift;
235 else
236 return img->d_h;
239 void vpx_img_write(const vpx_image_t *img, FILE *file) {
240 int plane;
242 for (plane = 0; plane < 3; ++plane) {
243 const unsigned char *buf = img->planes[plane];
244 const int stride = img->stride[plane];
245 const int w = vpx_img_plane_width(img, plane);
246 const int h = vpx_img_plane_height(img, plane);
247 int y;
249 for (y = 0; y < h; ++y) {
250 fwrite(buf, 1, w, file);
251 buf += stride;
256 int vpx_img_read(vpx_image_t *img, FILE *file) {
257 int plane;
259 for (plane = 0; plane < 3; ++plane) {
260 unsigned char *buf = img->planes[plane];
261 const int stride = img->stride[plane];
262 const int w = vpx_img_plane_width(img, plane);
263 const int h = vpx_img_plane_height(img, plane);
264 int y;
266 for (y = 0; y < h; ++y) {
267 if (fread(buf, 1, w, file) != w)
268 return 0;
269 buf += stride;
273 return 1;