blob: fe3401c7930e915fb0bd6ce9ee1ce6675d3fac2b [file] [log] [blame]
John Koleszarc377bf02010-11-02 09:11:57 -04001/*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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 */
Tom Finegan00a35aa2013-11-14 12:37:42 -080010
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -080011#include <math.h>
Tom Finegan03848f52013-11-05 10:02:18 -080012#include <stdarg.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080013#include <stdio.h>
Tom Finegan03848f52013-11-05 10:02:18 -080014#include <stdlib.h>
Tom Finegan00a35aa2013-11-14 12:37:42 -080015#include <string.h>
Tom Finegan03848f52013-11-05 10:02:18 -080016
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -080017#include "./tools_common.h"
18
Dmitry Kovalev70d96642014-02-11 21:12:23 -080019#if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
20#include "vpx/vp8cx.h"
21#endif
22
Dmitry Kovalev7ec27692014-01-27 13:40:29 -080023#if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
24#include "vpx/vp8dx.h"
25#endif
26
John Koleszar82b1a342012-11-06 12:08:05 -080027#if defined(_WIN32) || defined(__OS2__)
John Koleszarc377bf02010-11-02 09:11:57 -040028#include <io.h>
29#include <fcntl.h>
John Koleszar82b1a342012-11-06 12:08:05 -080030
31#ifdef __OS2__
32#define _setmode setmode
33#define _fileno fileno
34#define _O_BINARY O_BINARY
35#endif
John Koleszarc377bf02010-11-02 09:11:57 -040036#endif
37
Tom Finegan03848f52013-11-05 10:02:18 -080038#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)
48
49
John Koleszarc6b90392012-07-13 15:21:29 -070050FILE *set_binary_mode(FILE *stream) {
51 (void)stream;
John Koleszar82b1a342012-11-06 12:08:05 -080052#if defined(_WIN32) || defined(__OS2__)
John Koleszarc6b90392012-07-13 15:21:29 -070053 _setmode(_fileno(stream), _O_BINARY);
John Koleszarc377bf02010-11-02 09:11:57 -040054#endif
John Koleszarc6b90392012-07-13 15:21:29 -070055 return stream;
John Koleszarc377bf02010-11-02 09:11:57 -040056}
Tom Finegan03848f52013-11-05 10:02:18 -080057
58void die(const char *fmt, ...) {
59 LOG_ERROR(NULL);
60 usage_exit();
61}
62
63void fatal(const char *fmt, ...) {
64 LOG_ERROR("Fatal");
65 exit(EXIT_FAILURE);
66}
67
68void warn(const char *fmt, ...) {
69 LOG_ERROR("Warning");
70}
Tom Finegan00a35aa2013-11-14 12:37:42 -080071
Dmitry Kovalev7ec27692014-01-27 13:40:29 -080072void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
73 const char *detail = vpx_codec_error_detail(ctx);
74
75 printf("%s: %s\n", s, vpx_codec_error(ctx));
76 if (detail)
77 printf(" %s\n", detail);
78 exit(EXIT_FAILURE);
79}
80
Tom Finegan00a35aa2013-11-14 12:37:42 -080081int 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;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070086 const int bytespp = (yuv_frame->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
Tom Finegan00a35aa2013-11-14 12:37:42 -080087
88 for (plane = 0; plane < 3; ++plane) {
89 uint8_t *ptr;
Deb Mukherjee449e5f22014-07-11 11:43:31 -070090 const int w = vpx_img_plane_width(yuv_frame, plane);
91 const int h = vpx_img_plane_height(yuv_frame, plane);
Tom Finegan00a35aa2013-11-14 12:37:42 -080092 int r;
93
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.
97 */
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];
109 }
110
111 for (r = 0; r < h; ++r) {
Deb Mukherjee449e5f22014-07-11 11:43:31 -0700112 size_t needed = w * bytespp;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800113 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;
121 }
122 if (needed > 0) {
123 shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
124 }
125
126 ptr += yuv_frame->stride[plane];
127 }
128 }
129
130 return shortread;
131}
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800132
James Zern8465c932015-08-10 16:45:49 -0700133#if CONFIG_ENCODERS
134
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800135static const VpxInterface vpx_encoders[] = {
136#if CONFIG_VP8_ENCODER
137 {"vp8", VP8_FOURCC, &vpx_codec_vp8_cx},
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800138#endif
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800139
140#if CONFIG_VP9_ENCODER
141 {"vp9", VP9_FOURCC, &vpx_codec_vp9_cx},
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800142#endif
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800143};
144
James Zern5a73bbd2015-05-09 10:40:51 -0700145int get_vpx_encoder_count(void) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800146 return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]);
147}
148
149const VpxInterface *get_vpx_encoder_by_index(int i) {
150 return &vpx_encoders[i];
151}
152
153const VpxInterface *get_vpx_encoder_by_name(const char *name) {
154 int i;
155
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)
159 return encoder;
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800160 }
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800161
162 return NULL;
163}
164
James Zern8465c932015-08-10 16:45:49 -0700165#endif // CONFIG_ENCODERS
166
167#if CONFIG_DECODERS
168
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800169static const VpxInterface vpx_decoders[] = {
170#if CONFIG_VP8_DECODER
171 {"vp8", VP8_FOURCC, &vpx_codec_vp8_dx},
172#endif
173
174#if CONFIG_VP9_DECODER
175 {"vp9", VP9_FOURCC, &vpx_codec_vp9_dx},
176#endif
177};
178
James Zernd1999cb2015-05-09 10:41:54 -0700179int get_vpx_decoder_count(void) {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800180 return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]);
181}
182
183const VpxInterface *get_vpx_decoder_by_index(int i) {
184 return &vpx_decoders[i];
185}
186
187const VpxInterface *get_vpx_decoder_by_name(const char *name) {
188 int i;
189
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)
193 return decoder;
194 }
195
196 return NULL;
197}
198
199const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) {
200 int i;
201
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)
205 return decoder;
206 }
207
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800208 return NULL;
209}
210
James Zern8465c932015-08-10 16:45:49 -0700211#endif // CONFIG_DECODERS
212
Dmitry Kovalev2bdd43d2014-02-12 18:36:36 -0800213// TODO(dkovalev): move this function to vpx_image.{c, h}, so it will be part
214// of vpx_image_t support
215int 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;
218 else
219 return img->d_w;
220}
221
222int 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;
225 else
226 return img->d_h;
227}
228
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800229void vpx_img_write(const vpx_image_t *img, FILE *file) {
Dmitry Kovalev2bdd43d2014-02-12 18:36:36 -0800230 int plane;
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800231
232 for (plane = 0; plane < 3; ++plane) {
233 const unsigned char *buf = img->planes[plane];
234 const int stride = img->stride[plane];
Deb Mukherjee9ed23de2014-09-25 15:46:50 -0700235 const int w = vpx_img_plane_width(img, plane) *
236 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
Dmitry Kovalev2bdd43d2014-02-12 18:36:36 -0800237 const int h = vpx_img_plane_height(img, plane);
238 int y;
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800239
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800240 for (y = 0; y < h; ++y) {
241 fwrite(buf, 1, w, file);
242 buf += stride;
243 }
244 }
245}
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800246
247int vpx_img_read(vpx_image_t *img, FILE *file) {
248 int plane;
249
250 for (plane = 0; plane < 3; ++plane) {
251 unsigned char *buf = img->planes[plane];
252 const int stride = img->stride[plane];
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700253 const int w = vpx_img_plane_width(img, plane) *
254 ((img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
Dmitry Kovalev2bdd43d2014-02-12 18:36:36 -0800255 const int h = vpx_img_plane_height(img, plane);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800256 int y;
257
258 for (y = 0; y < h; ++y) {
Jim Bankoski201ebe52014-08-07 06:58:13 -0700259 if (fread(buf, 1, w, file) != (size_t)w)
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800260 return 0;
261 buf += stride;
262 }
263 }
264
265 return 1;
266}
267
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -0800268// TODO(dkovalev) change sse_to_psnr signature: double -> int64_t
269double sse_to_psnr(double samples, double peak, double sse) {
270 static const double kMaxPSNR = 100.0;
271
272 if (sse > 0.0) {
273 const double psnr = 10.0 * log10(samples * peak * peak / sse);
274 return psnr > kMaxPSNR ? kMaxPSNR : psnr;
275 } else {
276 return kMaxPSNR;
277 }
278}
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700279
280// TODO(debargha): Consolidate the functions below into a separate file.
281#if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
282static void highbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
283 int input_shift) {
284 // Note the offset is 1 less than half.
285 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
286 int plane;
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700287 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700288 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");
292 }
293 switch (src->fmt) {
294 case VPX_IMG_FMT_I42016:
295 case VPX_IMG_FMT_I42216:
296 case VPX_IMG_FMT_I44416:
297 case VPX_IMG_FMT_I44016:
298 break;
299 default:
300 fatal("Unsupported image conversion");
301 break;
302 }
303 for (plane = 0; plane < 3; plane++) {
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700304 int w = src->d_w;
305 int h = src->d_h;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700306 int x, y;
307 if (plane) {
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700308 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
309 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700310 }
311 for (y = 0; y < h; y++) {
312 uint16_t *p_src =
313 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
314 uint16_t *p_dst =
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;
318 }
319 }
320}
321
322static void lowbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
323 int input_shift) {
324 // Note the offset is 1 less than half.
325 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
326 int plane;
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700327 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700328 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 ||
331 input_shift < 0) {
332 fatal("Unsupported image conversion");
333 }
334 switch (src->fmt) {
335 case VPX_IMG_FMT_I420:
336 case VPX_IMG_FMT_I422:
337 case VPX_IMG_FMT_I444:
338 case VPX_IMG_FMT_I440:
339 break;
340 default:
341 fatal("Unsupported image conversion");
342 break;
343 }
344 for (plane = 0; plane < 3; plane++) {
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700345 int w = src->d_w;
346 int h = src->d_h;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700347 int x, y;
348 if (plane) {
349 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
350 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
351 }
352 for (y = 0; y < h; y++) {
353 uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
354 uint16_t *p_dst =
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;
358 }
359 }
360 }
361}
362
363void vpx_img_upshift(vpx_image_t *dst, vpx_image_t *src,
364 int input_shift) {
365 if (src->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
366 highbd_img_upshift(dst, src, input_shift);
367 } else {
368 lowbd_img_upshift(dst, src, input_shift);
369 }
370}
371
372void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src) {
373 int plane;
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");
379 }
380 switch (dst->fmt) {
381 case VPX_IMG_FMT_I420:
382 case VPX_IMG_FMT_I422:
383 case VPX_IMG_FMT_I444:
384 case VPX_IMG_FMT_I440:
385 break;
386 default:
387 fatal("Unsupported image conversion");
388 break;
389 }
390 for (plane = 0; plane < 3; plane++) {
391 int w = src->d_w;
392 int h = src->d_h;
393 int x, y;
394 if (plane) {
Deb Mukherjee23fc1f72014-10-15 10:01:34 -0700395 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
396 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700397 }
398 for (y = 0; y < h; y++) {
399 uint16_t *p_src =
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++) {
Yaowu Xuc369daf2015-07-07 14:22:07 -0700403 *p_dst++ = (uint8_t)(*p_src++);
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700404 }
405 }
406 }
407}
408
409static void highbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
410 int down_shift) {
411 int plane;
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");
417 }
418 switch (src->fmt) {
419 case VPX_IMG_FMT_I42016:
420 case VPX_IMG_FMT_I42216:
421 case VPX_IMG_FMT_I44416:
422 case VPX_IMG_FMT_I44016:
423 break;
424 default:
425 fatal("Unsupported image conversion");
426 break;
427 }
428 for (plane = 0; plane < 3; plane++) {
429 int w = src->d_w;
430 int h = src->d_h;
431 int x, y;
432 if (plane) {
433 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
434 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
435 }
436 for (y = 0; y < h; y++) {
437 uint16_t *p_src =
438 (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
439 uint16_t *p_dst =
440 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
441 for (x = 0; x < w; x++)
442 *p_dst++ = *p_src++ >> down_shift;
443 }
444 }
445}
446
447static void lowbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
448 int down_shift) {
449 int plane;
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 ||
454 down_shift < 0) {
455 fatal("Unsupported image conversion");
456 }
457 switch (dst->fmt) {
458 case VPX_IMG_FMT_I420:
459 case VPX_IMG_FMT_I422:
460 case VPX_IMG_FMT_I444:
461 case VPX_IMG_FMT_I440:
462 break;
463 default:
464 fatal("Unsupported image conversion");
465 break;
466 }
467 for (plane = 0; plane < 3; plane++) {
468 int w = src->d_w;
469 int h = src->d_h;
470 int x, y;
471 if (plane) {
472 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
473 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
474 }
475 for (y = 0; y < h; y++) {
476 uint16_t *p_src =
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;
481 }
482 }
483 }
484}
485
486void vpx_img_downshift(vpx_image_t *dst, vpx_image_t *src,
487 int down_shift) {
488 if (dst->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
489 highbd_img_downshift(dst, src, down_shift);
490 } else {
491 lowbd_img_downshift(dst, src, down_shift);
492 }
493}
494#endif // CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH