Merge "Add some const qualifiers where applicable"
[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)               \
39   do {                                 \
40     const char *l = label;             \
41     va_list ap;                        \
42     va_start(ap, fmt);                 \
43     if (l) fprintf(stderr, "%s: ", l); \
44     vfprintf(stderr, fmt, ap);         \
45     fprintf(stderr, "\n");             \
46     va_end(ap);                        \
47   } while (0)
48
49 FILE *set_binary_mode(FILE *stream) {
50   (void)stream;
51 #if defined(_WIN32) || defined(__OS2__)
52   _setmode(_fileno(stream), _O_BINARY);
53 #endif
54   return stream;
55 }
56
57 void die(const char *fmt, ...) {
58   LOG_ERROR(NULL);
59   usage_exit();
60 }
61
62 void fatal(const char *fmt, ...) {
63   LOG_ERROR("Fatal");
64   exit(EXIT_FAILURE);
65 }
66
67 void warn(const char *fmt, ...) { LOG_ERROR("Warning"); }
68
69 void die_codec(vpx_codec_ctx_t *ctx, const char *s) {
70   const char *detail = vpx_codec_error_detail(ctx);
71
72   printf("%s: %s\n", s, vpx_codec_error(ctx));
73   if (detail) printf("    %s\n", detail);
74   exit(EXIT_FAILURE);
75 }
76
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;
80   int plane = 0;
81   int shortread = 0;
82   const int bytespp = (yuv_frame->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
83
84   for (plane = 0; plane < 3; ++plane) {
85     uint8_t *ptr;
86     const int w = vpx_img_plane_width(yuv_frame, plane);
87     const int h = vpx_img_plane_height(yuv_frame, plane);
88     int r;
89
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
92      * the data on disk.
93      */
94     switch (plane) {
95       case 1:
96         ptr =
97             yuv_frame->planes[yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V
98                                                                  : VPX_PLANE_U];
99         break;
100       case 2:
101         ptr =
102             yuv_frame->planes[yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U
103                                                                  : VPX_PLANE_V];
104         break;
105       default: ptr = yuv_frame->planes[plane];
106     }
107
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;
112       if (left > 0) {
113         const size_t more = (left < needed) ? left : needed;
114         memcpy(ptr, detect->buf + detect->position, more);
115         buf_position = more;
116         needed -= more;
117         detect->position += more;
118       }
119       if (needed > 0) {
120         shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
121       }
122
123       ptr += yuv_frame->stride[plane];
124     }
125   }
126
127   return shortread;
128 }
129
130 #if CONFIG_ENCODERS
131
132 static const VpxInterface vpx_encoders[] = {
133 #if CONFIG_VP8_ENCODER
134   { "vp8", VP8_FOURCC, &vpx_codec_vp8_cx },
135 #endif
136
137 #if CONFIG_VP9_ENCODER
138   { "vp9", VP9_FOURCC, &vpx_codec_vp9_cx },
139 #endif
140 };
141
142 int get_vpx_encoder_count(void) {
143   return sizeof(vpx_encoders) / sizeof(vpx_encoders[0]);
144 }
145
146 const VpxInterface *get_vpx_encoder_by_index(int i) { return &vpx_encoders[i]; }
147
148 const VpxInterface *get_vpx_encoder_by_name(const char *name) {
149   int i;
150
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;
154   }
155
156   return NULL;
157 }
158
159 #endif  // CONFIG_ENCODERS
160
161 #if CONFIG_DECODERS
162
163 static const VpxInterface vpx_decoders[] = {
164 #if CONFIG_VP8_DECODER
165   { "vp8", VP8_FOURCC, &vpx_codec_vp8_dx },
166 #endif
167
168 #if CONFIG_VP9_DECODER
169   { "vp9", VP9_FOURCC, &vpx_codec_vp9_dx },
170 #endif
171 };
172
173 int get_vpx_decoder_count(void) {
174   return sizeof(vpx_decoders) / sizeof(vpx_decoders[0]);
175 }
176
177 const VpxInterface *get_vpx_decoder_by_index(int i) { return &vpx_decoders[i]; }
178
179 const VpxInterface *get_vpx_decoder_by_name(const char *name) {
180   int i;
181
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;
185   }
186
187   return NULL;
188 }
189
190 const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc) {
191   int i;
192
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;
196   }
197
198   return NULL;
199 }
200
201 #endif  // CONFIG_DECODERS
202
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;
206   else
207     return img->d_w;
208 }
209
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;
213   else
214     return img->d_h;
215 }
216
217 void vpx_img_write(const vpx_image_t *img, FILE *file) {
218   int plane;
219
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);
226     int y;
227
228     for (y = 0; y < h; ++y) {
229       fwrite(buf, 1, w, file);
230       buf += stride;
231     }
232   }
233 }
234
235 int vpx_img_read(vpx_image_t *img, FILE *file) {
236   int plane;
237
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);
244     int y;
245
246     for (y = 0; y < h; ++y) {
247       if (fread(buf, 1, w, file) != (size_t)w) return 0;
248       buf += stride;
249     }
250   }
251
252   return 1;
253 }
254
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;
258
259   if (sse > 0.0) {
260     const double psnr = 10.0 * log10(samples * peak * peak / sse);
261     return psnr > kMaxPSNR ? kMaxPSNR : psnr;
262   } else {
263     return kMaxPSNR;
264   }
265 }
266
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,
270                                int input_shift) {
271   // Note the offset is 1 less than half.
272   const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
273   int plane;
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 ||
277       input_shift < 0) {
278     fatal("Unsupported image conversion");
279   }
280   switch (src->fmt) {
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;
286   }
287   for (plane = 0; plane < 3; plane++) {
288     int w = src->d_w;
289     int h = src->d_h;
290     int x, y;
291     if (plane) {
292       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
293       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
294     }
295     for (y = 0; y < h; y++) {
296       uint16_t *p_src =
297           (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
298       uint16_t *p_dst =
299           (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
300       for (x = 0; x < w; x++) *p_dst++ = (*p_src++ << input_shift) + offset;
301     }
302   }
303 }
304
305 static void lowbd_img_upshift(vpx_image_t *dst, vpx_image_t *src,
306                               int input_shift) {
307   // Note the offset is 1 less than half.
308   const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
309   int plane;
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");
315   }
316   switch (src->fmt) {
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;
322   }
323   for (plane = 0; plane < 3; plane++) {
324     int w = src->d_w;
325     int h = src->d_h;
326     int x, y;
327     if (plane) {
328       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
329       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
330     }
331     for (y = 0; y < h; y++) {
332       uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
333       uint16_t *p_dst =
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;
337       }
338     }
339   }
340 }
341
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);
345   } else {
346     lowbd_img_upshift(dst, src, input_shift);
347   }
348 }
349
350 void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src) {
351   int plane;
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");
356   }
357   switch (dst->fmt) {
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;
363   }
364   for (plane = 0; plane < 3; plane++) {
365     int w = src->d_w;
366     int h = src->d_h;
367     int x, y;
368     if (plane) {
369       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
370       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
371     }
372     for (y = 0; y < h; y++) {
373       uint16_t *p_src =
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++);
378       }
379     }
380   }
381 }
382
383 static void highbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
384                                  int down_shift) {
385   int plane;
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 ||
389       down_shift < 0) {
390     fatal("Unsupported image conversion");
391   }
392   switch (src->fmt) {
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;
398   }
399   for (plane = 0; plane < 3; plane++) {
400     int w = src->d_w;
401     int h = src->d_h;
402     int x, y;
403     if (plane) {
404       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
405       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
406     }
407     for (y = 0; y < h; y++) {
408       uint16_t *p_src =
409           (uint16_t *)(src->planes[plane] + y * src->stride[plane]);
410       uint16_t *p_dst =
411           (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
412       for (x = 0; x < w; x++) *p_dst++ = *p_src++ >> down_shift;
413     }
414   }
415 }
416
417 static void lowbd_img_downshift(vpx_image_t *dst, vpx_image_t *src,
418                                 int down_shift) {
419   int plane;
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");
425   }
426   switch (dst->fmt) {
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;
432   }
433   for (plane = 0; plane < 3; plane++) {
434     int w = src->d_w;
435     int h = src->d_h;
436     int x, y;
437     if (plane) {
438       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
439       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
440     }
441     for (y = 0; y < h; y++) {
442       uint16_t *p_src =
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;
447       }
448     }
449   }
450 }
451
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);
455   } else {
456     lowbd_img_downshift(dst, src, down_shift);
457   }
458 }
459 #endif  // CONFIG_VP9_HIGHBITDEPTH