Move VP9 SSIM metrics to vpx_dsp.
[aom.git] / tools_common.c
blob8d356af3f54d56b1b6e087ab4705ea39fb22f858
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 <math.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
17 #include "./tools_common.h"
19 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
20 #include "vpx/vp8cx.h"
21 #endif
23 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
24 #include "vpx/vp8dx.h"
25 #endif
27 #if defined(_WIN32) || defined(__OS2__)
28 #include <io.h>
29 #include <fcntl.h>
31 #ifdef __OS2__
32 #define _setmode setmode
33 #define _fileno fileno
34 #define _O_BINARY O_BINARY
35 #endif
36 #endif
38 #define LOG_ERROR(label) do {\
39 const char *l = label;\
40 va_list ap;\
41 va_start(ap, fmt);\
42 if (l)\
43 fprintf(stderr, "%s: ", l);\
44 vfprintf(stderr, fmt, ap);\
45 fprintf(stderr, "\n");\
46 va_end(ap);\
47 } while (0)
50 FILE *set_binary_mode(FILE *stream) {
51 (void)stream;
52 #if defined(_WIN32) || defined(__OS2__)
53 _setmode(_fileno(stream), _O_BINARY);
54 #endif
55 return stream;
58 void die(const char *fmt, ...) {
59 LOG_ERROR(NULL);
60 usage_exit();
63 void fatal(const char *fmt, ...) {
64 LOG_ERROR("Fatal");
65 exit(EXIT_FAILURE);
68 void warn(const char *fmt, ...) {
69 LOG_ERROR("Warning");
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));
76 if (detail)
77 printf(" %s\n", detail);
78 exit(EXIT_FAILURE);
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;
84 int plane = 0;
85 int shortread = 0;
86 const int bytespp = (yuv_frame->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
88 for (plane = 0; plane < 3; ++plane) {
89 uint8_t *ptr;
90 const int w = vpx_img_plane_width(yuv_frame, plane);
91 const int h = vpx_img_plane_height(yuv_frame, plane);
92 int r;
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
96 * the data on disk.
98 switch (plane) {
99 case 1:
100 ptr = yuv_frame->planes[
101 yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V : VPX_PLANE_U];
102 break;
103 case 2:
104 ptr = yuv_frame->planes[
105 yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U : VPX_PLANE_V];
106 break;
107 default:
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;
115 if (left > 0) {
116 const size_t more = (left < needed) ? left : needed;
117 memcpy(ptr, detect->buf + detect->position, more);
118 buf_position = more;
119 needed -= more;
120 detect->position += more;
122 if (needed > 0) {
123 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
126 ptr += yuv_frame->stride[plane];
130 return shortread;
133 static const VpxInterface vpx_encoders[] = {
134 #if CONFIG_VP8_ENCODER
135 {"vp8", VP8_FOURCC, &vpx_codec_vp8_cx},
136 #endif
138 #if CONFIG_VP9_ENCODER
139 {"vp9", VP9_FOURCC, &vpx_codec_vp9_cx},
140 #endif
143 int get_vpx_encoder_count(void) {
144 return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]);
147 const VpxInterface *get_vpx_encoder_by_index(int i) {
148 return &vpx_encoders[i];
151 const VpxInterface *get_vpx_encoder_by_name(const char *name) {
152 int i;
154 for (i = 0; i < get_vpx_encoder_count(); ++i) {
155 const VpxInterface *encoder = get_vpx_encoder_by_index(i);
156 if (strcmp(encoder->name, name) == 0)
157 return encoder;
160 return NULL;
163 static const VpxInterface vpx_decoders[] = {
164 #if CONFIG_VP8_DECODER
165 {"vp8", VP8_FOURCC, &vpx_codec_vp8_dx},
166 #endif
168 #if CONFIG_VP9_DECODER
169 {"vp9", VP9_FOURCC, &vpx_codec_vp9_dx},
170 #endif
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) {
178 return &vpx_decoders[i];
181 const VpxInterface *get_vpx_decoder_by_name(const char *name) {
182 int i;
184 for (i = 0; i < get_vpx_decoder_count(); ++i) {
185 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
186 if (strcmp(decoder->name, name) == 0)
187 return decoder;
190 return NULL;
193 const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) {
194 int i;
196 for (i = 0; i < get_vpx_decoder_count(); ++i) {
197 const VpxInterface *const decoder = get_vpx_decoder_by_index(i);
198 if (decoder->fourcc == fourcc)
199 return decoder;
202 return NULL;
205 // TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part
206 // of vpx_image_t support
207 int vpx_img_plane_width(const vpx_image_t *img, int plane) {
208 if (plane > 0 && img->x_chroma_shift > 0)
209 return (img->d_w + 1) >> img->x_chroma_shift;
210 else
211 return img->d_w;
214 int vpx_img_plane_height(const vpx_image_t *img, int plane) {
215 if (plane > 0 && img->y_chroma_shift > 0)
216 return (img->d_h + 1) >> img->y_chroma_shift;
217 else
218 return img->d_h;
221 void vpx_img_write(const vpx_image_t *img, FILE *file) {
222 int plane;
224 for (plane = 0; plane < 3; ++plane) {
225 const unsigned char *buf = img->planes[plane];
226 const int stride = img->stride[plane];
227 const int w = vpx_img_plane_width(img, plane) *
228 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
229 const int h = vpx_img_plane_height(img, plane);
230 int y;
232 for (y = 0; y < h; ++y) {
233 fwrite(buf, 1, w, file);
234 buf += stride;
239 int vpx_img_read(vpx_image_t *img, FILE *file) {
240 int plane;
242 for (plane = 0; plane < 3; ++plane) {
243 unsigned char *buf = img->planes[plane];
244 const int stride = img->stride[plane];
245 const int w = vpx_img_plane_width(img, plane) *
246 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
247 const int h = vpx_img_plane_height(img, plane);
248 int y;
250 for (y = 0; y < h; ++y) {
251 if (fread(buf, 1, w, file) != (size_t)w)
252 return 0;
253 buf += stride;
257 return 1;
260 // TODO(dkovalev) change sse_to_psnr signature: double -> int64_t
261 double sse_to_psnr(double samples, double peak, double sse) {
262 static const double kMaxPSNR = 100.0;
264 if (sse > 0.0) {
265 const double psnr = 10.0 * log10(samples * peak * peak / sse);
266 return psnr > kMaxPSNR ? kMaxPSNR : psnr;
267 } else {
268 return kMaxPSNR;
272 // TODO(debargha): Consolidate the functions below into a separate file.
273 #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
274 static void highbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
275 int input_shift) {
276 // Note the offset is 1 less than half.
277 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
278 int plane;
279 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
280 dst->x_chroma_shift != src->x_chroma_shift ||
281 dst->y_chroma_shift != src->y_chroma_shift ||
282 dst->fmt != src->fmt || input_shift < 0) {
283 fatal("Unsupported image conversion");
285 switch (src->fmt) {
286 case VPX_IMG_FMT_I42016:
287 case VPX_IMG_FMT_I42216:
288 case VPX_IMG_FMT_I44416:
289 case VPX_IMG_FMT_I44016:
290 break;
291 default:
292 fatal("Unsupported image conversion");
293 break;
295 for (plane = 0; plane < 3; plane++) {
296 int w = src->d_w;
297 int h = src->d_h;
298 int x, y;
299 if (plane) {
300 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
301 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
303 for (y = 0; y < h; y++) {
304 uint16_t *p_src =
305 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
306 uint16_t *p_dst =
307 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
308 for (x = 0; x < w; x++)
309 *p_dst++ = (*p_src++ << input_shift) + offset;
314 static void lowbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
315 int input_shift) {
316 // Note the offset is 1 less than half.
317 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
318 int plane;
319 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
320 dst->x_chroma_shift != src->x_chroma_shift ||
321 dst->y_chroma_shift != src->y_chroma_shift ||
322 dst->fmt != src->fmt + VPX_IMG_FMT_HIGHBITDEPTH ||
323 input_shift < 0) {
324 fatal("Unsupported image conversion");
326 switch (src->fmt) {
327 case VPX_IMG_FMT_I420:
328 case VPX_IMG_FMT_I422:
329 case VPX_IMG_FMT_I444:
330 case VPX_IMG_FMT_I440:
331 break;
332 default:
333 fatal("Unsupported image conversion");
334 break;
336 for (plane = 0; plane < 3; plane++) {
337 int w = src->d_w;
338 int h = src->d_h;
339 int x, y;
340 if (plane) {
341 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
342 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
344 for (y = 0; y < h; y++) {
345 uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
346 uint16_t *p_dst =
347 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
348 for (x = 0; x < w; x++) {
349 *p_dst++ = (*p_src++ << input_shift) + offset;
355 void vpx_img_upshift(vpx_image_t *dst, vpx_image_t *src,
356 int input_shift) {
357 if (src->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
358 highbd_img_upshift(dst, src, input_shift);
359 } else {
360 lowbd_img_upshift(dst, src, input_shift);
364 void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src) {
365 int plane;
366 if (dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH != src->fmt ||
367 dst->d_w != src->d_w || dst->d_h != src->d_h ||
368 dst->x_chroma_shift != src->x_chroma_shift ||
369 dst->y_chroma_shift != src->y_chroma_shift) {
370 fatal("Unsupported image conversion");
372 switch (dst->fmt) {
373 case VPX_IMG_FMT_I420:
374 case VPX_IMG_FMT_I422:
375 case VPX_IMG_FMT_I444:
376 case VPX_IMG_FMT_I440:
377 break;
378 default:
379 fatal("Unsupported image conversion");
380 break;
382 for (plane = 0; plane < 3; plane++) {
383 int w = src->d_w;
384 int h = src->d_h;
385 int x, y;
386 if (plane) {
387 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
388 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
390 for (y = 0; y < h; y++) {
391 uint16_t *p_src =
392 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
393 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
394 for (x = 0; x < w; x++) {
395 *p_dst++ = (uint8_t)(*p_src++);
401 static void highbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
402 int down_shift) {
403 int plane;
404 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
405 dst->x_chroma_shift != src->x_chroma_shift ||
406 dst->y_chroma_shift != src->y_chroma_shift ||
407 dst->fmt != src->fmt || down_shift < 0) {
408 fatal("Unsupported image conversion");
410 switch (src->fmt) {
411 case VPX_IMG_FMT_I42016:
412 case VPX_IMG_FMT_I42216:
413 case VPX_IMG_FMT_I44416:
414 case VPX_IMG_FMT_I44016:
415 break;
416 default:
417 fatal("Unsupported image conversion");
418 break;
420 for (plane = 0; plane < 3; plane++) {
421 int w = src->d_w;
422 int h = src->d_h;
423 int x, y;
424 if (plane) {
425 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
426 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
428 for (y = 0; y < h; y++) {
429 uint16_t *p_src =
430 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
431 uint16_t *p_dst =
432 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
433 for (x = 0; x < w; x++)
434 *p_dst++ = *p_src++ >> down_shift;
439 static void lowbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
440 int down_shift) {
441 int plane;
442 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
443 dst->x_chroma_shift != src->x_chroma_shift ||
444 dst->y_chroma_shift != src->y_chroma_shift ||
445 src->fmt != dst->fmt + VPX_IMG_FMT_HIGHBITDEPTH ||
446 down_shift < 0) {
447 fatal("Unsupported image conversion");
449 switch (dst->fmt) {
450 case VPX_IMG_FMT_I420:
451 case VPX_IMG_FMT_I422:
452 case VPX_IMG_FMT_I444:
453 case VPX_IMG_FMT_I440:
454 break;
455 default:
456 fatal("Unsupported image conversion");
457 break;
459 for (plane = 0; plane < 3; plane++) {
460 int w = src->d_w;
461 int h = src->d_h;
462 int x, y;
463 if (plane) {
464 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
465 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
467 for (y = 0; y < h; y++) {
468 uint16_t *p_src =
469 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
470 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
471 for (x = 0; x < w; x++) {
472 *p_dst++ = *p_src++ >> down_shift;
478 void vpx_img_downshift(vpx_image_t *dst, vpx_image_t *src,
479 int down_shift) {
480 if (dst->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
481 highbd_img_downshift(dst, src, down_shift);
482 } else {
483 lowbd_img_downshift(dst, src, down_shift);
486 #endif // CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH