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) \
40 const char *l = label; \
43 if (l) fprintf(stderr, "%s: ", l); \
44 vfprintf(stderr, fmt, ap); \
45 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, ...) { LOG_ERROR("Warning"); }
69 void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
70 const char *detail = vpx_codec_error_detail(ctx);
72 printf("%s: %s\n", s, vpx_codec_error(ctx));
73 if (detail) printf(" %s\n", detail);
77 int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame) {
78 FILE *f = input_ctx->file;
79 struct FileTypeDetectionBuffer *detect = &input_ctx->detect;
82 const int bytespp = (yuv_frame->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
84 for (plane = 0; plane < 3; ++plane) {
86 const int w = vpx_img_plane_width(yuv_frame, plane);
87 const int h = vpx_img_plane_height(yuv_frame, plane);
90 /* Determine the correct plane based on the image format. The for-loop
91 * always counts in Y,U,V order, but this may not match the order of
97 yuv_frame->planes[yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V
102 yuv_frame->planes[yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U
105 default: ptr = yuv_frame->planes[plane];
108 for (r = 0; r < h; ++r) {
109 size_t needed = w * bytespp;
110 size_t buf_position = 0;
111 const size_t left = detect->buf_read - detect->position;
113 const size_t more = (left < needed) ? left : needed;
114 memcpy(ptr, detect->buf + detect->position, more);
117 detect->position += more;
120 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
123 ptr += yuv_frame->stride[plane];
132 static const VpxInterface vpx_encoders[] = {
133 #if CONFIG_VP8_ENCODER
134 { "vp8", VP8_FOURCC, &vpx_codec_vp8_cx },
137 #if CONFIG_VP9_ENCODER
138 { "vp9", VP9_FOURCC, &vpx_codec_vp9_cx },
142 int get_vpx_encoder_count(void) {
143 return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]);
146 const VpxInterface *get_vpx_encoder_by_index(int i) { return &vpx_encoders[i]; }
148 const VpxInterface *get_vpx_encoder_by_name(const char *name) {
151 for (i = 0; i < get_vpx_encoder_count(); ++i) {
152 const VpxInterface *encoder = get_vpx_encoder_by_index(i);
153 if (strcmp(encoder->name, name) == 0) return encoder;
159 #endif // CONFIG_ENCODERS
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(void) {
174 return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]);
177 const VpxInterface *get_vpx_decoder_by_index(int i) { return &vpx_decoders[i]; }
179 const VpxInterface *get_vpx_decoder_by_name(const char *name) {
182 for (i = 0; i < get_vpx_decoder_count(); ++i) {
183 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
184 if (strcmp(decoder->name, name) == 0) return decoder;
190 const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) {
193 for (i = 0; i < get_vpx_decoder_count(); ++i) {
194 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
195 if (decoder->fourcc == fourcc) return decoder;
201 #endif // CONFIG_DECODERS
203 // TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part
204 // of vpx_image_t support
205 int vpx_img_plane_width(const vpx_image_t *img, int plane) {
206 if (plane > 0 && img->x_chroma_shift > 0)
207 return (img->d_w + 1) >> img->x_chroma_shift;
212 int vpx_img_plane_height(const vpx_image_t *img, int plane) {
213 if (plane > 0 && img->y_chroma_shift > 0)
214 return (img->d_h + 1) >> img->y_chroma_shift;
219 void vpx_img_write(const vpx_image_t *img, FILE *file) {
222 for (plane = 0; plane < 3; ++plane) {
223 const unsigned char *buf = img->planes[plane];
224 const int stride = img->stride[plane];
225 const int w = vpx_img_plane_width(img, plane) *
226 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
227 const int h = vpx_img_plane_height(img, plane);
230 for (y = 0; y < h; ++y) {
231 fwrite(buf, 1, w, file);
237 int vpx_img_read(vpx_image_t *img, FILE *file) {
240 for (plane = 0; plane < 3; ++plane) {
241 unsigned char *buf = img->planes[plane];
242 const int stride = img->stride[plane];
243 const int w = vpx_img_plane_width(img, plane) *
244 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
245 const int h = vpx_img_plane_height(img, plane);
248 for (y = 0; y < h; ++y) {
249 if (fread(buf, 1, w, file) != (size_t)w) return 0;
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;
262 const double psnr = 10.0 * log10(samples * peak * peak / sse);
263 return psnr > kMaxPSNR ? kMaxPSNR : psnr;
269 // TODO(debargha): Consolidate the functions below into a separate file.
270 #if CONFIG_VP9_HIGHBITDEPTH
271 static void highbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
273 // Note the offset is 1 less than half.
274 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
276 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
277 dst->x_chroma_shift != src->x_chroma_shift ||
278 dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
280 fatal("Unsupported image conversion");
283 case VPX_IMG_FMT_I42016:
284 case VPX_IMG_FMT_I42216:
285 case VPX_IMG_FMT_I44416:
286 case VPX_IMG_FMT_I44016: break;
287 default: fatal("Unsupported image conversion"); break;
289 for (plane = 0; plane < 3; plane++) {
294 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
295 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
297 for (y = 0; y < h; y++) {
299 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
301 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
302 for (x = 0; x < w; x++) *p_dst++ = (*p_src++ << input_shift) + offset;
307 static void lowbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
309 // Note the offset is 1 less than half.
310 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
312 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
313 dst->x_chroma_shift != src->x_chroma_shift ||
314 dst->y_chroma_shift != src->y_chroma_shift ||
315 dst->fmt != src->fmt + VPX_IMG_FMT_HIGHBITDEPTH || input_shift < 0) {
316 fatal("Unsupported image conversion");
319 case VPX_IMG_FMT_I420:
320 case VPX_IMG_FMT_I422:
321 case VPX_IMG_FMT_I444:
322 case VPX_IMG_FMT_I440: break;
323 default: fatal("Unsupported image conversion"); break;
325 for (plane = 0; plane < 3; plane++) {
330 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
331 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
333 for (y = 0; y < h; y++) {
334 uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
336 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
337 for (x = 0; x < w; x++) {
338 *p_dst++ = (*p_src++ << input_shift) + offset;
344 void vpx_img_upshift(vpx_image_t *dst, vpx_image_t *src, int input_shift) {
345 if (src->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
346 highbd_img_upshift(dst, src, input_shift);
348 lowbd_img_upshift(dst, src, input_shift);
352 void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src) {
354 if (dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH != src->fmt || dst->d_w != src->d_w ||
355 dst->d_h != src->d_h || dst->x_chroma_shift != src->x_chroma_shift ||
356 dst->y_chroma_shift != src->y_chroma_shift) {
357 fatal("Unsupported image conversion");
360 case VPX_IMG_FMT_I420:
361 case VPX_IMG_FMT_I422:
362 case VPX_IMG_FMT_I444:
363 case VPX_IMG_FMT_I440: break;
364 default: fatal("Unsupported image conversion"); break;
366 for (plane = 0; plane < 3; plane++) {
371 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
372 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
374 for (y = 0; y < h; y++) {
376 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
377 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
378 for (x = 0; x < w; x++) {
379 *p_dst++ = (uint8_t)(*p_src++);
385 static void highbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
388 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
389 dst->x_chroma_shift != src->x_chroma_shift ||
390 dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
392 fatal("Unsupported image conversion");
395 case VPX_IMG_FMT_I42016:
396 case VPX_IMG_FMT_I42216:
397 case VPX_IMG_FMT_I44416:
398 case VPX_IMG_FMT_I44016: break;
399 default: fatal("Unsupported image conversion"); break;
401 for (plane = 0; plane < 3; plane++) {
406 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
407 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
409 for (y = 0; y < h; y++) {
411 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
413 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
414 for (x = 0; x < w; x++) *p_dst++ = *p_src++ >> down_shift;
419 static void lowbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
422 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
423 dst->x_chroma_shift != src->x_chroma_shift ||
424 dst->y_chroma_shift != src->y_chroma_shift ||
425 src->fmt != dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH || down_shift < 0) {
426 fatal("Unsupported image conversion");
429 case VPX_IMG_FMT_I420:
430 case VPX_IMG_FMT_I422:
431 case VPX_IMG_FMT_I444:
432 case VPX_IMG_FMT_I440: break;
433 default: fatal("Unsupported image conversion"); break;
435 for (plane = 0; plane < 3; plane++) {
440 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
441 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
443 for (y = 0; y < h; y++) {
445 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
446 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
447 for (x = 0; x < w; x++) {
448 *p_dst++ = *p_src++ >> down_shift;
454 void vpx_img_downshift(vpx_image_t *dst, vpx_image_t *src, int down_shift) {
455 if (dst->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
456 highbd_img_downshift(dst, src, down_shift);
458 lowbd_img_downshift(dst, src, down_shift);
461 #endif // CONFIG_VP9_HIGHBITDEPTH