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