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 int vpx_img_plane_width(const vpx_image_t *img, int plane) {
204 if (plane > 0 && img->x_chroma_shift > 0)
205 return (img->d_w + 1) >> img->x_chroma_shift;
210 int vpx_img_plane_height(const vpx_image_t *img, int plane) {
211 if (plane > 0 && img->y_chroma_shift > 0)
212 return (img->d_h + 1) >> img->y_chroma_shift;
217 void vpx_img_write(const vpx_image_t *img, FILE *file) {
220 for (plane = 0; plane < 3; ++plane) {
221 const unsigned char *buf = img->planes[plane];
222 const int stride = img->stride[plane];
223 const int w = vpx_img_plane_width(img, plane) *
224 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
225 const int h = vpx_img_plane_height(img, plane);
228 for (y = 0; y < h; ++y) {
229 fwrite(buf, 1, w, file);
235 int vpx_img_read(vpx_image_t *img, FILE *file) {
238 for (plane = 0; plane < 3; ++plane) {
239 unsigned char *buf = img->planes[plane];
240 const int stride = img->stride[plane];
241 const int w = vpx_img_plane_width(img, plane) *
242 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
243 const int h = vpx_img_plane_height(img, plane);
246 for (y = 0; y < h; ++y) {
247 if (fread(buf, 1, w, file) != (size_t)w) return 0;
255 // TODO(dkovalev) change sse_to_psnr signature: double -> int64_t
256 double sse_to_psnr(double samples, double peak, double sse) {
257 static const double kMaxPSNR = 100.0;
260 const double psnr = 10.0 * log10(samples * peak * peak / sse);
261 return psnr > kMaxPSNR ? kMaxPSNR : psnr;
267 // TODO(debargha): Consolidate the functions below into a separate file.
268 #if CONFIG_VP9_HIGHBITDEPTH
269 static void highbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
271 // Note the offset is 1 less than half.
272 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
274 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
275 dst->x_chroma_shift != src->x_chroma_shift ||
276 dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
278 fatal("Unsupported image conversion");
281 case VPX_IMG_FMT_I42016:
282 case VPX_IMG_FMT_I42216:
283 case VPX_IMG_FMT_I44416:
284 case VPX_IMG_FMT_I44016: break;
285 default: fatal("Unsupported image conversion"); break;
287 for (plane = 0; plane < 3; plane++) {
292 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
293 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
295 for (y = 0; y < h; y++) {
297 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
299 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
300 for (x = 0; x < w; x++) *p_dst++ = (*p_src++ << input_shift) + offset;
305 static void lowbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
307 // Note the offset is 1 less than half.
308 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
310 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
311 dst->x_chroma_shift != src->x_chroma_shift ||
312 dst->y_chroma_shift != src->y_chroma_shift ||
313 dst->fmt != src->fmt + VPX_IMG_FMT_HIGHBITDEPTH || input_shift < 0) {
314 fatal("Unsupported image conversion");
317 case VPX_IMG_FMT_I420:
318 case VPX_IMG_FMT_I422:
319 case VPX_IMG_FMT_I444:
320 case VPX_IMG_FMT_I440: break;
321 default: fatal("Unsupported image conversion"); break;
323 for (plane = 0; plane < 3; plane++) {
328 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
329 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
331 for (y = 0; y < h; y++) {
332 uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
334 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
335 for (x = 0; x < w; x++) {
336 *p_dst++ = (*p_src++ << input_shift) + offset;
342 void vpx_img_upshift(vpx_image_t *dst, vpx_image_t *src, int input_shift) {
343 if (src->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
344 highbd_img_upshift(dst, src, input_shift);
346 lowbd_img_upshift(dst, src, input_shift);
350 void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src) {
352 if (dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH != src->fmt || dst->d_w != src->d_w ||
353 dst->d_h != src->d_h || dst->x_chroma_shift != src->x_chroma_shift ||
354 dst->y_chroma_shift != src->y_chroma_shift) {
355 fatal("Unsupported image conversion");
358 case VPX_IMG_FMT_I420:
359 case VPX_IMG_FMT_I422:
360 case VPX_IMG_FMT_I444:
361 case VPX_IMG_FMT_I440: break;
362 default: fatal("Unsupported image conversion"); break;
364 for (plane = 0; plane < 3; plane++) {
369 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
370 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
372 for (y = 0; y < h; y++) {
374 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
375 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
376 for (x = 0; x < w; x++) {
377 *p_dst++ = (uint8_t)(*p_src++);
383 static void highbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
386 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
387 dst->x_chroma_shift != src->x_chroma_shift ||
388 dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
390 fatal("Unsupported image conversion");
393 case VPX_IMG_FMT_I42016:
394 case VPX_IMG_FMT_I42216:
395 case VPX_IMG_FMT_I44416:
396 case VPX_IMG_FMT_I44016: break;
397 default: fatal("Unsupported image conversion"); break;
399 for (plane = 0; plane < 3; plane++) {
404 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
405 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
407 for (y = 0; y < h; y++) {
409 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
411 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
412 for (x = 0; x < w; x++) *p_dst++ = *p_src++ >> down_shift;
417 static void lowbd_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 src->fmt != dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH || down_shift < 0) {
424 fatal("Unsupported image conversion");
427 case VPX_IMG_FMT_I420:
428 case VPX_IMG_FMT_I422:
429 case VPX_IMG_FMT_I444:
430 case VPX_IMG_FMT_I440: break;
431 default: fatal("Unsupported image conversion"); break;
433 for (plane = 0; plane < 3; plane++) {
438 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
439 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
441 for (y = 0; y < h; y++) {
443 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
444 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
445 for (x = 0; x < w; x++) {
446 *p_dst++ = *p_src++ >> down_shift;
452 void vpx_img_downshift(vpx_image_t *dst, vpx_image_t *src, int down_shift) {
453 if (dst->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
454 highbd_img_downshift(dst, src, down_shift);
456 lowbd_img_downshift(dst, src, down_shift);
459 #endif // CONFIG_VP9_HIGHBITDEPTH