Merge "set interp_filter to SWITCHABLE_FILTER for intra block"
[platform/upstream/libvpx.git] / tools_common.c
1 /*
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  */
10
11 #include <math.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "./tools_common.h"
18
19 #if CONFIG_VP8_ENCODER || CONFIG_VP9_ENCODER
20 #include "vpx/vp8cx.h"
21 #endif
22
23 #if CONFIG_VP8_DECODER || CONFIG_VP9_DECODER
24 #include "vpx/vp8dx.h"
25 #endif
26
27 #if defined(_WIN32) || defined(__OS2__)
28 #include <io.h>
29 #include <fcntl.h>
30
31 #ifdef __OS2__
32 #define _setmode    setmode
33 #define _fileno     fileno
34 #define _O_BINARY   O_BINARY
35 #endif
36 #endif
37
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)
48
49
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;
56 }
57
58 void die(const char *fmt, ...) {
59   LOG_ERROR(NULL);
60   usage_exit();
61 }
62
63 void fatal(const char *fmt, ...) {
64   LOG_ERROR("Fatal");
65   exit(EXIT_FAILURE);
66 }
67
68 void warn(const char *fmt, ...) {
69   LOG_ERROR("Warning");
70 }
71
72 void 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
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;
87
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;
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) {
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;
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 }
132
133 #if CONFIG_ENCODERS
134
135 static const VpxInterface vpx_encoders[] = {
136 #if CONFIG_VP8_ENCODER
137   {"vp8", VP8_FOURCC, &vpx_codec_vp8_cx},
138 #endif
139
140 #if CONFIG_VP9_ENCODER
141   {"vp9", VP9_FOURCC, &vpx_codec_vp9_cx},
142 #endif
143 };
144
145 int get_vpx_encoder_count(void) {
146   return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]);
147 }
148
149 const VpxInterface *get_vpx_encoder_by_index(int i) {
150   return &vpx_encoders[i];
151 }
152
153 const 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;
160   }
161
162   return NULL;
163 }
164
165 #endif  // CONFIG_ENCODERS
166
167 #if CONFIG_DECODERS
168
169 static 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
179 int get_vpx_decoder_count(void) {
180   return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]);
181 }
182
183 const VpxInterface *get_vpx_decoder_by_index(int i) {
184   return &vpx_decoders[i];
185 }
186
187 const 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
199 const 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
208   return NULL;
209 }
210
211 #endif  // CONFIG_DECODERS
212
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;
218   else
219     return img->d_w;
220 }
221
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;
225   else
226     return img->d_h;
227 }
228
229 void vpx_img_write(const vpx_image_t *img, FILE *file) {
230   int plane;
231
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);
238     int y;
239
240     for (y = 0; y < h; ++y) {
241       fwrite(buf, 1, w, file);
242       buf += stride;
243     }
244   }
245 }
246
247 int 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];
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);
256     int y;
257
258     for (y = 0; y < h; ++y) {
259       if (fread(buf, 1, w, file) != (size_t)w)
260         return 0;
261       buf += stride;
262     }
263   }
264
265   return 1;
266 }
267
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;
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 }
279
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,
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;
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");
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++) {
304     int w = src->d_w;
305     int h = src->d_h;
306     int x, y;
307     if (plane) {
308       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
309       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
310     }
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
322 static 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;
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 ||
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++) {
345     int w = src->d_w;
346     int h = src->d_h;
347     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
363 void 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
372 void 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) {
395       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
396       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
397     }
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++) {
403         *p_dst++ = (uint8_t)(*p_src++);
404       }
405     }
406   }
407 }
408
409 static 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
447 static 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
486 void 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_HIGHBITDEPTH