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) 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_VP8_ENCODER
137 {"vp8", VP8_FOURCC, &vpx_codec_vp8_cx},
140 #if CONFIG_VP9_ENCODER
141 {"vp9", VP9_FOURCC, &vpx_codec_vp9_cx},
145 int get_vpx_encoder_count(void) {
146 return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]);
149 const VpxInterface *get_vpx_encoder_by_index(int i) {
150 return &vpx_encoders[i];
153 const VpxInterface *get_vpx_encoder_by_name(const char *name) {
156 for (i = 0; i < get_vpx_encoder_count(); ++i) {
157 const VpxInterface *encoder = get_vpx_encoder_by_index(i);
158 if (strcmp(encoder->name, name) == 0)
165 #endif // CONFIG_ENCODERS
169 static const VpxInterface vpx_decoders[] = {
170 #if CONFIG_VP8_DECODER
171 {"vp8", VP8_FOURCC, &vpx_codec_vp8_dx},
174 #if CONFIG_VP9_DECODER
175 {"vp9", VP9_FOURCC, &vpx_codec_vp9_dx},
179 int get_vpx_decoder_count(void) {
180 return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]);
183 const VpxInterface *get_vpx_decoder_by_index(int i) {
184 return &vpx_decoders[i];
187 const VpxInterface *get_vpx_decoder_by_name(const char *name) {
190 for (i = 0; i < get_vpx_decoder_count(); ++i) {
191 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
192 if (strcmp(decoder->name, name) == 0)
199 const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) {
202 for (i = 0; i < get_vpx_decoder_count(); ++i) {
203 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
204 if (decoder->fourcc == fourcc)
211 #endif // CONFIG_DECODERS
213 // TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part
214 // of vpx_image_t support
215 int vpx_img_plane_width(const vpx_image_t *img, int plane) {
216 if (plane > 0 && img->x_chroma_shift > 0)
217 return (img->d_w + 1) >> img->x_chroma_shift;
222 int vpx_img_plane_height(const vpx_image_t *img, int plane) {
223 if (plane > 0 && img->y_chroma_shift > 0)
224 return (img->d_h + 1) >> img->y_chroma_shift;
229 void vpx_img_write(const vpx_image_t *img, FILE *file) {
232 for (plane = 0; plane < 3; ++plane) {
233 const unsigned char *buf = img->planes[plane];
234 const int stride = img->stride[plane];
235 const int w = vpx_img_plane_width(img, plane) *
236 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
237 const int h = vpx_img_plane_height(img, plane);
240 for (y = 0; y < h; ++y) {
241 fwrite(buf, 1, w, file);
247 int vpx_img_read(vpx_image_t *img, FILE *file) {
250 for (plane = 0; plane < 3; ++plane) {
251 unsigned char *buf = img->planes[plane];
252 const int stride = img->stride[plane];
253 const int w = vpx_img_plane_width(img, plane) *
254 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
255 const int h = vpx_img_plane_height(img, plane);
258 for (y = 0; y < h; ++y) {
259 if (fread(buf, 1, w, file) != (size_t)w)
268 // TODO(dkovalev) change sse_to_psnr signature: double -> int64_t
269 double sse_to_psnr(double samples, double peak, double sse) {
270 static const double kMaxPSNR = 100.0;
273 const double psnr = 10.0 * log10(samples * peak * peak / sse);
274 return psnr > kMaxPSNR ? kMaxPSNR : psnr;
280 // TODO(debargha): Consolidate the functions below into a separate file.
281 #if CONFIG_VP9_HIGHBITDEPTH
282 static void highbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
284 // Note the offset is 1 less than half.
285 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
287 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
288 dst->x_chroma_shift != src->x_chroma_shift ||
289 dst->y_chroma_shift != src->y_chroma_shift ||
290 dst->fmt != src->fmt || input_shift < 0) {
291 fatal("Unsupported image conversion");
294 case VPX_IMG_FMT_I42016:
295 case VPX_IMG_FMT_I42216:
296 case VPX_IMG_FMT_I44416:
297 case VPX_IMG_FMT_I44016:
300 fatal("Unsupported image conversion");
303 for (plane = 0; plane < 3; plane++) {
308 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
309 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
311 for (y = 0; y < h; y++) {
313 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
315 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
316 for (x = 0; x < w; x++)
317 *p_dst++ = (*p_src++ << input_shift) + offset;
322 static void lowbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
324 // Note the offset is 1 less than half.
325 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
327 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
328 dst->x_chroma_shift != src->x_chroma_shift ||
329 dst->y_chroma_shift != src->y_chroma_shift ||
330 dst->fmt != src->fmt + VPX_IMG_FMT_HIGHBITDEPTH ||
332 fatal("Unsupported image conversion");
335 case VPX_IMG_FMT_I420:
336 case VPX_IMG_FMT_I422:
337 case VPX_IMG_FMT_I444:
338 case VPX_IMG_FMT_I440:
341 fatal("Unsupported image conversion");
344 for (plane = 0; plane < 3; plane++) {
349 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
350 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
352 for (y = 0; y < h; y++) {
353 uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
355 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
356 for (x = 0; x < w; x++) {
357 *p_dst++ = (*p_src++ << input_shift) + offset;
363 void vpx_img_upshift(vpx_image_t *dst, vpx_image_t *src,
365 if (src->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
366 highbd_img_upshift(dst, src, input_shift);
368 lowbd_img_upshift(dst, src, input_shift);
372 void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src) {
374 if (dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH != src->fmt ||
375 dst->d_w != src->d_w || dst->d_h != src->d_h ||
376 dst->x_chroma_shift != src->x_chroma_shift ||
377 dst->y_chroma_shift != src->y_chroma_shift) {
378 fatal("Unsupported image conversion");
381 case VPX_IMG_FMT_I420:
382 case VPX_IMG_FMT_I422:
383 case VPX_IMG_FMT_I444:
384 case VPX_IMG_FMT_I440:
387 fatal("Unsupported image conversion");
390 for (plane = 0; plane < 3; plane++) {
395 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
396 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
398 for (y = 0; y < h; y++) {
400 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
401 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
402 for (x = 0; x < w; x++) {
403 *p_dst++ = (uint8_t)(*p_src++);
409 static void highbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
412 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
413 dst->x_chroma_shift != src->x_chroma_shift ||
414 dst->y_chroma_shift != src->y_chroma_shift ||
415 dst->fmt != src->fmt || down_shift < 0) {
416 fatal("Unsupported image conversion");
419 case VPX_IMG_FMT_I42016:
420 case VPX_IMG_FMT_I42216:
421 case VPX_IMG_FMT_I44416:
422 case VPX_IMG_FMT_I44016:
425 fatal("Unsupported image conversion");
428 for (plane = 0; plane < 3; plane++) {
433 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
434 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
436 for (y = 0; y < h; y++) {
438 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
440 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
441 for (x = 0; x < w; x++)
442 *p_dst++ = *p_src++ >> down_shift;
447 static void lowbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
450 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
451 dst->x_chroma_shift != src->x_chroma_shift ||
452 dst->y_chroma_shift != src->y_chroma_shift ||
453 src->fmt != dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH ||
455 fatal("Unsupported image conversion");
458 case VPX_IMG_FMT_I420:
459 case VPX_IMG_FMT_I422:
460 case VPX_IMG_FMT_I444:
461 case VPX_IMG_FMT_I440:
464 fatal("Unsupported image conversion");
467 for (plane = 0; plane < 3; plane++) {
472 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
473 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
475 for (y = 0; y < h; y++) {
477 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
478 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
479 for (x = 0; x < w; x++) {
480 *p_dst++ = *p_src++ >> down_shift;
486 void vpx_img_downshift(vpx_image_t *dst, vpx_image_t *src,
488 if (dst->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
489 highbd_img_downshift(dst, src, down_shift);
491 lowbd_img_downshift(dst, src, down_shift);
494 #endif // CONFIG_VP9_HIGHBITDEPTH