1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <assert.h>
13 #include <math.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include "common/tools_common.h"
20 
21 #if CONFIG_AV1_ENCODER
22 #include "aom/aomcx.h"
23 #endif
24 
25 #if CONFIG_AV1_DECODER
26 #include "aom/aomdx.h"
27 #endif
28 
29 #if defined(_WIN32) || defined(__OS2__)
30 #include <io.h>
31 #include <fcntl.h>
32 
33 #ifdef __OS2__
34 #define _setmode setmode
35 #define _fileno fileno
36 #define _O_BINARY O_BINARY
37 #endif
38 #endif
39 
40 #define LOG_ERROR(label)               \
41   do {                                 \
42     const char *l = label;             \
43     va_list ap;                        \
44     va_start(ap, fmt);                 \
45     if (l) fprintf(stderr, "%s: ", l); \
46     vfprintf(stderr, fmt, ap);         \
47     fprintf(stderr, "\n");             \
48     va_end(ap);                        \
49   } while (0)
50 
set_binary_mode(FILE * stream)51 FILE *set_binary_mode(FILE *stream) {
52   (void)stream;
53 #if defined(_WIN32) || defined(__OS2__)
54   _setmode(_fileno(stream), _O_BINARY);
55 #endif
56   return stream;
57 }
58 
die(const char * fmt,...)59 void die(const char *fmt, ...) {
60   LOG_ERROR(NULL);
61   usage_exit();
62 }
63 
fatal(const char * fmt,...)64 void fatal(const char *fmt, ...) {
65   LOG_ERROR("Fatal");
66   exit(EXIT_FAILURE);
67 }
68 
aom_tools_warn(const char * fmt,...)69 void aom_tools_warn(const char *fmt, ...) { LOG_ERROR("Warning"); }
70 
die_codec(aom_codec_ctx_t * ctx,const char * s)71 void die_codec(aom_codec_ctx_t *ctx, const char *s) {
72   const char *detail = aom_codec_error_detail(ctx);
73 
74   printf("%s: %s\n", s, aom_codec_error(ctx));
75   if (detail) printf("    %s\n", detail);
76   exit(EXIT_FAILURE);
77 }
78 
read_yuv_frame(struct AvxInputContext * input_ctx,aom_image_t * yuv_frame)79 int read_yuv_frame(struct AvxInputContext *input_ctx, aom_image_t *yuv_frame) {
80   FILE *f = input_ctx->file;
81   struct FileTypeDetectionBuffer *detect = &input_ctx->detect;
82   int plane = 0;
83   int shortread = 0;
84   const int bytespp = (yuv_frame->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
85 
86   for (plane = 0; plane < 3; ++plane) {
87     uint8_t *ptr;
88     const int w = aom_img_plane_width(yuv_frame, plane);
89     const int h = aom_img_plane_height(yuv_frame, plane);
90     int r;
91 
92     /* Determine the correct plane based on the image format. The for-loop
93      * always counts in Y,U,V order, but this may not match the order of
94      * the data on disk.
95      */
96     switch (plane) {
97       case 1:
98         ptr =
99             yuv_frame->planes[yuv_frame->fmt == AOM_IMG_FMT_YV12 ? AOM_PLANE_V
100                                                                  : AOM_PLANE_U];
101         break;
102       case 2:
103         ptr =
104             yuv_frame->planes[yuv_frame->fmt == AOM_IMG_FMT_YV12 ? AOM_PLANE_U
105                                                                  : AOM_PLANE_V];
106         break;
107       default: ptr = yuv_frame->planes[plane];
108     }
109 
110     for (r = 0; r < h; ++r) {
111       size_t needed = w * bytespp;
112       size_t buf_position = 0;
113       const size_t left = detect->buf_read - detect->position;
114       if (left > 0) {
115         const size_t more = (left < needed) ? left : needed;
116         memcpy(ptr, detect->buf + detect->position, more);
117         buf_position = more;
118         needed -= more;
119         detect->position += more;
120       }
121       if (needed > 0) {
122         shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
123       }
124 
125       ptr += yuv_frame->stride[plane];
126     }
127   }
128 
129   return shortread;
130 }
131 
132 struct CodecInfo {
133   // Pointer to a function of zero arguments that returns an aom_codec_iface_t.
134   aom_codec_iface_t *(*const interface)();
135   char *short_name;
136   uint32_t fourcc;
137 };
138 
139 #if CONFIG_AV1_ENCODER
140 static const struct CodecInfo aom_encoders[] = {
141   { &aom_codec_av1_cx, "av1", AV1_FOURCC },
142 };
143 
get_aom_encoder_count(void)144 int get_aom_encoder_count(void) {
145   return sizeof(aom_encoders) / sizeof(aom_encoders[0]);
146 }
147 
get_aom_encoder_by_index(int i)148 aom_codec_iface_t *get_aom_encoder_by_index(int i) {
149   assert(i >= 0 && i < get_aom_encoder_count());
150   return aom_encoders[i].interface();
151 }
152 
get_aom_encoder_by_short_name(const char * name)153 aom_codec_iface_t *get_aom_encoder_by_short_name(const char *name) {
154   for (int i = 0; i < get_aom_encoder_count(); ++i) {
155     const struct CodecInfo *info = &aom_encoders[i];
156     if (strcmp(info->short_name, name) == 0) return info->interface();
157   }
158   return NULL;
159 }
160 
get_fourcc_by_aom_encoder(aom_codec_iface_t * iface)161 uint32_t get_fourcc_by_aom_encoder(aom_codec_iface_t *iface) {
162   for (int i = 0; i < get_aom_encoder_count(); ++i) {
163     const struct CodecInfo *info = &aom_encoders[i];
164     if (info->interface() == iface) {
165       return info->fourcc;
166     }
167   }
168   return 0;
169 }
170 
get_short_name_by_aom_encoder(aom_codec_iface_t * iface)171 const char *get_short_name_by_aom_encoder(aom_codec_iface_t *iface) {
172   for (int i = 0; i < get_aom_encoder_count(); ++i) {
173     const struct CodecInfo *info = &aom_encoders[i];
174     if (info->interface() == iface) {
175       return info->short_name;
176     }
177   }
178   return NULL;
179 }
180 
181 #endif  // CONFIG_AV1_ENCODER
182 
183 #if CONFIG_AV1_DECODER
184 static const struct CodecInfo aom_decoders[] = {
185   { &aom_codec_av1_dx, "av1", AV1_FOURCC },
186 };
187 
get_aom_decoder_count(void)188 int get_aom_decoder_count(void) {
189   return sizeof(aom_decoders) / sizeof(aom_decoders[0]);
190 }
191 
get_aom_decoder_by_index(int i)192 aom_codec_iface_t *get_aom_decoder_by_index(int i) {
193   assert(i >= 0 && i < get_aom_decoder_count());
194   return aom_decoders[i].interface();
195 }
196 
get_aom_decoder_by_short_name(const char * name)197 aom_codec_iface_t *get_aom_decoder_by_short_name(const char *name) {
198   for (int i = 0; i < get_aom_decoder_count(); ++i) {
199     const struct CodecInfo *info = &aom_decoders[i];
200     if (strcmp(info->short_name, name) == 0) return info->interface();
201   }
202   return NULL;
203 }
204 
get_aom_decoder_by_fourcc(uint32_t fourcc)205 aom_codec_iface_t *get_aom_decoder_by_fourcc(uint32_t fourcc) {
206   for (int i = 0; i < get_aom_decoder_count(); ++i) {
207     const struct CodecInfo *info = &aom_decoders[i];
208     if (info->fourcc == fourcc) return info->interface();
209   }
210   return NULL;
211 }
212 
get_short_name_by_aom_decoder(aom_codec_iface_t * iface)213 const char *get_short_name_by_aom_decoder(aom_codec_iface_t *iface) {
214   for (int i = 0; i < get_aom_decoder_count(); ++i) {
215     const struct CodecInfo *info = &aom_decoders[i];
216     if (info->interface() == iface) {
217       return info->short_name;
218     }
219   }
220   return NULL;
221 }
222 
get_fourcc_by_aom_decoder(aom_codec_iface_t * iface)223 uint32_t get_fourcc_by_aom_decoder(aom_codec_iface_t *iface) {
224   for (int i = 0; i < get_aom_decoder_count(); ++i) {
225     const struct CodecInfo *info = &aom_decoders[i];
226     if (info->interface() == iface) {
227       return info->fourcc;
228     }
229   }
230   return 0;
231 }
232 
233 #endif  // CONFIG_AV1_DECODER
234 
aom_img_write(const aom_image_t * img,FILE * file)235 void aom_img_write(const aom_image_t *img, FILE *file) {
236   int plane;
237 
238   for (plane = 0; plane < 3; ++plane) {
239     const unsigned char *buf = img->planes[plane];
240     const int stride = img->stride[plane];
241     const int w = aom_img_plane_width(img, plane) *
242                   ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
243     const int h = aom_img_plane_height(img, plane);
244     int y;
245 
246     for (y = 0; y < h; ++y) {
247       fwrite(buf, 1, w, file);
248       buf += stride;
249     }
250   }
251 }
252 
aom_img_read(aom_image_t * img,FILE * file)253 int aom_img_read(aom_image_t *img, FILE *file) {
254   int plane;
255 
256   for (plane = 0; plane < 3; ++plane) {
257     unsigned char *buf = img->planes[plane];
258     const int stride = img->stride[plane];
259     const int w = aom_img_plane_width(img, plane) *
260                   ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
261     const int h = aom_img_plane_height(img, plane);
262     int y;
263 
264     for (y = 0; y < h; ++y) {
265       if (fread(buf, 1, w, file) != (size_t)w) return 0;
266       buf += stride;
267     }
268   }
269 
270   return 1;
271 }
272 
273 // TODO(dkovalev) change sse_to_psnr signature: double -> int64_t
sse_to_psnr(double samples,double peak,double sse)274 double sse_to_psnr(double samples, double peak, double sse) {
275   static const double kMaxPSNR = 100.0;
276 
277   if (sse > 0.0) {
278     const double psnr = 10.0 * log10(samples * peak * peak / sse);
279     return psnr > kMaxPSNR ? kMaxPSNR : psnr;
280   } else {
281     return kMaxPSNR;
282   }
283 }
284 
285 // TODO(debargha): Consolidate the functions below into a separate file.
highbd_img_upshift(aom_image_t * dst,const aom_image_t * src,int input_shift)286 static void highbd_img_upshift(aom_image_t *dst, const aom_image_t *src,
287                                int input_shift) {
288   // Note the offset is 1 less than half.
289   const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
290   int plane;
291   if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
292       dst->x_chroma_shift != src->x_chroma_shift ||
293       dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
294       input_shift < 0) {
295     fatal("Unsupported image conversion");
296   }
297   switch (src->fmt) {
298     case AOM_IMG_FMT_I42016:
299     case AOM_IMG_FMT_I42216:
300     case AOM_IMG_FMT_I44416: break;
301     default: fatal("Unsupported image conversion"); 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       const uint16_t *p_src =
313           (const 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++) *p_dst++ = (*p_src++ << input_shift) + offset;
317     }
318   }
319 }
320 
lowbd_img_upshift(aom_image_t * dst,const aom_image_t * src,int input_shift)321 static void lowbd_img_upshift(aom_image_t *dst, const aom_image_t *src,
322                               int input_shift) {
323   // Note the offset is 1 less than half.
324   const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
325   int plane;
326   if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
327       dst->x_chroma_shift != src->x_chroma_shift ||
328       dst->y_chroma_shift != src->y_chroma_shift ||
329       dst->fmt != src->fmt + AOM_IMG_FMT_HIGHBITDEPTH || input_shift < 0) {
330     fatal("Unsupported image conversion");
331   }
332   switch (src->fmt) {
333     case AOM_IMG_FMT_YV12:
334     case AOM_IMG_FMT_I420:
335     case AOM_IMG_FMT_I422:
336     case AOM_IMG_FMT_I444: break;
337     default: fatal("Unsupported image conversion"); break;
338   }
339   for (plane = 0; plane < 3; plane++) {
340     int w = src->d_w;
341     int h = src->d_h;
342     int x, y;
343     if (plane) {
344       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
345       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
346     }
347     for (y = 0; y < h; y++) {
348       const uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
349       uint16_t *p_dst =
350           (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
351       for (x = 0; x < w; x++) {
352         *p_dst++ = (*p_src++ << input_shift) + offset;
353       }
354     }
355   }
356 }
357 
aom_img_upshift(aom_image_t * dst,const aom_image_t * src,int input_shift)358 void aom_img_upshift(aom_image_t *dst, const aom_image_t *src,
359                      int input_shift) {
360   if (src->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
361     highbd_img_upshift(dst, src, input_shift);
362   } else {
363     lowbd_img_upshift(dst, src, input_shift);
364   }
365 }
366 
aom_img_truncate_16_to_8(aom_image_t * dst,const aom_image_t * src)367 void aom_img_truncate_16_to_8(aom_image_t *dst, const aom_image_t *src) {
368   int plane;
369   if (dst->fmt + AOM_IMG_FMT_HIGHBITDEPTH != src->fmt || dst->d_w != src->d_w ||
370       dst->d_h != src->d_h || dst->x_chroma_shift != src->x_chroma_shift ||
371       dst->y_chroma_shift != src->y_chroma_shift) {
372     fatal("Unsupported image conversion");
373   }
374   switch (dst->fmt) {
375     case AOM_IMG_FMT_I420:
376     case AOM_IMG_FMT_I422:
377     case AOM_IMG_FMT_I444: break;
378     default: fatal("Unsupported image conversion"); break;
379   }
380   for (plane = 0; plane < 3; plane++) {
381     int w = src->d_w;
382     int h = src->d_h;
383     int x, y;
384     if (plane) {
385       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
386       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
387     }
388     for (y = 0; y < h; y++) {
389       const uint16_t *p_src =
390           (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
391       uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
392       for (x = 0; x < w; x++) {
393         *p_dst++ = (uint8_t)(*p_src++);
394       }
395     }
396   }
397 }
398 
highbd_img_downshift(aom_image_t * dst,const aom_image_t * src,int down_shift)399 static void highbd_img_downshift(aom_image_t *dst, const aom_image_t *src,
400                                  int down_shift) {
401   int plane;
402   if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
403       dst->x_chroma_shift != src->x_chroma_shift ||
404       dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
405       down_shift < 0) {
406     fatal("Unsupported image conversion");
407   }
408   switch (src->fmt) {
409     case AOM_IMG_FMT_I42016:
410     case AOM_IMG_FMT_I42216:
411     case AOM_IMG_FMT_I44416: break;
412     default: fatal("Unsupported image conversion"); break;
413   }
414   for (plane = 0; plane < 3; plane++) {
415     int w = src->d_w;
416     int h = src->d_h;
417     int x, y;
418     if (plane) {
419       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
420       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
421     }
422     for (y = 0; y < h; y++) {
423       const uint16_t *p_src =
424           (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
425       uint16_t *p_dst =
426           (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
427       for (x = 0; x < w; x++) *p_dst++ = *p_src++ >> down_shift;
428     }
429   }
430 }
431 
lowbd_img_downshift(aom_image_t * dst,const aom_image_t * src,int down_shift)432 static void lowbd_img_downshift(aom_image_t *dst, const aom_image_t *src,
433                                 int down_shift) {
434   int plane;
435   if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
436       dst->x_chroma_shift != src->x_chroma_shift ||
437       dst->y_chroma_shift != src->y_chroma_shift ||
438       src->fmt != dst->fmt + AOM_IMG_FMT_HIGHBITDEPTH || down_shift < 0) {
439     fatal("Unsupported image conversion");
440   }
441   switch (dst->fmt) {
442     case AOM_IMG_FMT_I420:
443     case AOM_IMG_FMT_I422:
444     case AOM_IMG_FMT_I444: break;
445     default: fatal("Unsupported image conversion"); break;
446   }
447   for (plane = 0; plane < 3; plane++) {
448     int w = src->d_w;
449     int h = src->d_h;
450     int x, y;
451     if (plane) {
452       w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
453       h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
454     }
455     for (y = 0; y < h; y++) {
456       const uint16_t *p_src =
457           (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
458       uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
459       for (x = 0; x < w; x++) {
460         *p_dst++ = *p_src++ >> down_shift;
461       }
462     }
463   }
464 }
465 
aom_img_downshift(aom_image_t * dst,const aom_image_t * src,int down_shift)466 void aom_img_downshift(aom_image_t *dst, const aom_image_t *src,
467                        int down_shift) {
468   if (dst->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
469     highbd_img_downshift(dst, src, down_shift);
470   } else {
471     lowbd_img_downshift(dst, src, down_shift);
472   }
473 }
474 
img_shifted_realloc_required(const aom_image_t * img,const aom_image_t * shifted,aom_img_fmt_t required_fmt)475 static int img_shifted_realloc_required(const aom_image_t *img,
476                                         const aom_image_t *shifted,
477                                         aom_img_fmt_t required_fmt) {
478   return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
479          required_fmt != shifted->fmt;
480 }
481 
aom_shift_img(unsigned int output_bit_depth,aom_image_t ** img_ptr,aom_image_t ** img_shifted_ptr)482 void aom_shift_img(unsigned int output_bit_depth, aom_image_t **img_ptr,
483                    aom_image_t **img_shifted_ptr) {
484   aom_image_t *img = *img_ptr;
485   aom_image_t *img_shifted = *img_shifted_ptr;
486 
487   const aom_img_fmt_t shifted_fmt = output_bit_depth == 8
488                                         ? img->fmt & ~AOM_IMG_FMT_HIGHBITDEPTH
489                                         : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
490 
491   if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
492     if (img_shifted &&
493         img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
494       aom_img_free(img_shifted);
495       img_shifted = NULL;
496     }
497     if (img_shifted) {
498       img_shifted->monochrome = img->monochrome;
499     }
500     if (!img_shifted) {
501       img_shifted = aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
502       img_shifted->bit_depth = output_bit_depth;
503       img_shifted->monochrome = img->monochrome;
504       img_shifted->csp = img->csp;
505     }
506     if (output_bit_depth > img->bit_depth) {
507       aom_img_upshift(img_shifted, img, output_bit_depth - img->bit_depth);
508     } else {
509       aom_img_downshift(img_shifted, img, img->bit_depth - output_bit_depth);
510     }
511     *img_shifted_ptr = img_shifted;
512     *img_ptr = img_shifted;
513   }
514 }
515 
516 // Related to I420, NV12 format has one luma "luminance" plane Y and one plane
517 // with U and V values interleaved.
aom_img_write_nv12(const aom_image_t * img,FILE * file)518 void aom_img_write_nv12(const aom_image_t *img, FILE *file) {
519   // Y plane
520   const unsigned char *buf = img->planes[0];
521   int stride = img->stride[0];
522   int w = aom_img_plane_width(img, 0) *
523           ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
524   int h = aom_img_plane_height(img, 0);
525   int x, y;
526 
527   for (y = 0; y < h; ++y) {
528     fwrite(buf, 1, w, file);
529     buf += stride;
530   }
531 
532   // Interleaved U and V plane
533   const unsigned char *ubuf = img->planes[1];
534   const unsigned char *vbuf = img->planes[2];
535   const size_t size = (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
536   stride = img->stride[1];
537   w = aom_img_plane_width(img, 1);
538   h = aom_img_plane_height(img, 1);
539 
540   for (y = 0; y < h; ++y) {
541     for (x = 0; x < w; ++x) {
542       fwrite(ubuf, size, 1, file);
543       fwrite(vbuf, size, 1, file);
544       ubuf += size;
545       vbuf += size;
546     }
547     ubuf += (stride - w * size);
548     vbuf += (stride - w * size);
549   }
550 }
551