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 || CONFIG_VP10_ENCODER
20 #include "vpx/vp8cx.h"
23 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER || CONFIG_VP10_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 = (yuv_frame->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 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];
135 static const VpxInterface vpx_encoders[] = {
136 #if CONFIG_VP10_ENCODER
137 {"vp10", VP10_FOURCC, &vpx_codec_vp10_cx},
140 #if CONFIG_VP8_ENCODER
141 {"vp8", VP8_FOURCC, &vpx_codec_vp8_cx},
144 #if CONFIG_VP9_ENCODER
145 {"vp9", VP9_FOURCC, &vpx_codec_vp9_cx},
149 int get_vpx_encoder_count(void) {
150 return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]);
153 const VpxInterface *get_vpx_encoder_by_index(int i) {
154 return &vpx_encoders[i];
157 const VpxInterface *get_vpx_encoder_by_name(const char *name) {
160 for (i = 0; i < get_vpx_encoder_count(); ++i) {
161 const VpxInterface *encoder = get_vpx_encoder_by_index(i);
162 if (strcmp(encoder->name, name) == 0)
169 #endif // CONFIG_ENCODERS
173 static const VpxInterface vpx_decoders[] = {
174 #if CONFIG_VP8_DECODER
175 {"vp8", VP8_FOURCC, &vpx_codec_vp8_dx},
178 #if CONFIG_VP9_DECODER
179 {"vp9", VP9_FOURCC, &vpx_codec_vp9_dx},
182 #if CONFIG_VP10_DECODER
183 {"vp10", VP10_FOURCC, &vpx_codec_vp10_dx},
187 int get_vpx_decoder_count(void) {
188 return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]);
191 const VpxInterface *get_vpx_decoder_by_index(int i) {
192 return &vpx_decoders[i];
195 const VpxInterface *get_vpx_decoder_by_name(const char *name) {
198 for (i = 0; i < get_vpx_decoder_count(); ++i) {
199 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
200 if (strcmp(decoder->name, name) == 0)
207 const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) {
210 for (i = 0; i < get_vpx_decoder_count(); ++i) {
211 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
212 if (decoder->fourcc == fourcc)
219 #endif // CONFIG_DECODERS
221 // TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part
222 // of vpx_image_t support
223 int vpx_img_plane_width(const vpx_image_t *img, int plane) {
224 if (plane > 0 && img->x_chroma_shift > 0)
225 return (img->d_w + 1) >> img->x_chroma_shift;
230 int vpx_img_plane_height(const vpx_image_t *img, int plane) {
231 if (plane > 0 && img->y_chroma_shift > 0)
232 return (img->d_h + 1) >> img->y_chroma_shift;
237 void vpx_img_write(const vpx_image_t *img, FILE *file) {
240 for (plane = 0; plane < 3; ++plane) {
241 const 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 fwrite(buf, 1, w, file);
255 int vpx_img_read(vpx_image_t *img, FILE *file) {
258 for (plane = 0; plane < 3; ++plane) {
259 unsigned char *buf = img->planes[plane];
260 const int stride = img->stride[plane];
261 const int w = vpx_img_plane_width(img, plane) *
262 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
263 const int h = vpx_img_plane_height(img, plane);
266 for (y = 0; y < h; ++y) {
267 if (fread(buf, 1, w, file) != (size_t)w)
276 // TODO(dkovalev) change sse_to_psnr signature: double -> int64_t
277 double sse_to_psnr(double samples, double peak, double sse) {
278 static const double kMaxPSNR = 100.0;
281 const double psnr = 10.0 * log10(samples * peak * peak / sse);
282 return psnr > kMaxPSNR ? kMaxPSNR : psnr;
288 // TODO(debargha): Consolidate the functions below into a separate file.
289 #if CONFIG_VP9_HIGHBITDEPTH
290 static void highbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
292 // Note the offset is 1 less than half.
293 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
295 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
296 dst->x_chroma_shift != src->x_chroma_shift ||
297 dst->y_chroma_shift != src->y_chroma_shift ||
298 dst->fmt != src->fmt || input_shift < 0) {
299 fatal("Unsupported image conversion");
302 case VPX_IMG_FMT_I42016:
303 case VPX_IMG_FMT_I42216:
304 case VPX_IMG_FMT_I44416:
305 case VPX_IMG_FMT_I44016:
308 fatal("Unsupported image conversion");
311 for (plane = 0; plane < 3; plane++) {
316 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
317 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
319 for (y = 0; y < h; y++) {
321 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
323 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
324 for (x = 0; x < w; x++)
325 *p_dst++ = (*p_src++ << input_shift) + offset;
330 static void lowbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
332 // Note the offset is 1 less than half.
333 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
335 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
336 dst->x_chroma_shift != src->x_chroma_shift ||
337 dst->y_chroma_shift != src->y_chroma_shift ||
338 dst->fmt != src->fmt + VPX_IMG_FMT_HIGHBITDEPTH ||
340 fatal("Unsupported image conversion");
343 case VPX_IMG_FMT_I420:
344 case VPX_IMG_FMT_I422:
345 case VPX_IMG_FMT_I444:
346 case VPX_IMG_FMT_I440:
349 fatal("Unsupported image conversion");
352 for (plane = 0; plane < 3; plane++) {
357 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
358 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
360 for (y = 0; y < h; y++) {
361 uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
363 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
364 for (x = 0; x < w; x++) {
365 *p_dst++ = (*p_src++ << input_shift) + offset;
371 void vpx_img_upshift(vpx_image_t *dst, vpx_image_t *src,
373 if (src->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
374 highbd_img_upshift(dst, src, input_shift);
376 lowbd_img_upshift(dst, src, input_shift);
380 void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src) {
382 if (dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH != src->fmt ||
383 dst->d_w != src->d_w || dst->d_h != src->d_h ||
384 dst->x_chroma_shift != src->x_chroma_shift ||
385 dst->y_chroma_shift != src->y_chroma_shift) {
386 fatal("Unsupported image conversion");
389 case VPX_IMG_FMT_I420:
390 case VPX_IMG_FMT_I422:
391 case VPX_IMG_FMT_I444:
392 case VPX_IMG_FMT_I440:
395 fatal("Unsupported image conversion");
398 for (plane = 0; plane < 3; plane++) {
403 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
404 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
406 for (y = 0; y < h; y++) {
408 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
409 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
410 for (x = 0; x < w; x++) {
411 *p_dst++ = (uint8_t)(*p_src++);
417 static void highbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
420 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
421 dst->x_chroma_shift != src->x_chroma_shift ||
422 dst->y_chroma_shift != src->y_chroma_shift ||
423 dst->fmt != src->fmt || down_shift < 0) {
424 fatal("Unsupported image conversion");
427 case VPX_IMG_FMT_I42016:
428 case VPX_IMG_FMT_I42216:
429 case VPX_IMG_FMT_I44416:
430 case VPX_IMG_FMT_I44016:
433 fatal("Unsupported image conversion");
436 for (plane = 0; plane < 3; plane++) {
441 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
442 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
444 for (y = 0; y < h; y++) {
446 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
448 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
449 for (x = 0; x < w; x++)
450 *p_dst++ = *p_src++ >> down_shift;
455 static void lowbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
458 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
459 dst->x_chroma_shift != src->x_chroma_shift ||
460 dst->y_chroma_shift != src->y_chroma_shift ||
461 src->fmt != dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH ||
463 fatal("Unsupported image conversion");
466 case VPX_IMG_FMT_I420:
467 case VPX_IMG_FMT_I422:
468 case VPX_IMG_FMT_I444:
469 case VPX_IMG_FMT_I440:
472 fatal("Unsupported image conversion");
475 for (plane = 0; plane < 3; plane++) {
480 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
481 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
483 for (y = 0; y < h; y++) {
485 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
486 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
487 for (x = 0; x < w; x++) {
488 *p_dst++ = *p_src++ >> down_shift;
494 void vpx_img_downshift(vpx_image_t *dst, vpx_image_t *src,
496 if (dst->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
497 highbd_img_downshift(dst, src, down_shift);
499 lowbd_img_downshift(dst, src, down_shift);
502 #endif // CONFIG_VP9_HIGHBITDEPTH