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 #endif // CONFIG_AV1_ENCODER
153
154 #if CONFIG_AV1_DECODER
155 static const AvxInterface aom_decoders[] = {
156 { "av1", AV1_FOURCC, &aom_codec_av1_dx },
157 };
158
get_aom_decoder_count(void)159 int get_aom_decoder_count(void) {
160 return sizeof(aom_decoders) / sizeof(aom_decoders[0]);
161 }
162
get_aom_decoder_by_index(int i)163 const AvxInterface *get_aom_decoder_by_index(int i) { return &aom_decoders[i]; }
164
get_aom_decoder_by_name(const char * name)165 const AvxInterface *get_aom_decoder_by_name(const char *name) {
166 int i;
167
168 for (i = 0; i < get_aom_decoder_count(); ++i) {
169 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
170 if (strcmp(decoder->name, name) == 0) return decoder;
171 }
172
173 return NULL;
174 }
175
get_aom_decoder_by_fourcc(uint32_t fourcc)176 const AvxInterface *get_aom_decoder_by_fourcc(uint32_t fourcc) {
177 int i;
178
179 for (i = 0; i < get_aom_decoder_count(); ++i) {
180 const AvxInterface *const decoder = get_aom_decoder_by_index(i);
181 if (decoder->fourcc == fourcc) return decoder;
182 }
183
184 return NULL;
185 }
186 #endif // CONFIG_AV1_DECODER
187
aom_img_write(const aom_image_t * img,FILE * file)188 void aom_img_write(const aom_image_t *img, FILE *file) {
189 int plane;
190
191 for (plane = 0; plane < 3; ++plane) {
192 const unsigned char *buf = img->planes[plane];
193 const int stride = img->stride[plane];
194 const int w = aom_img_plane_width(img, plane) *
195 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
196 const int h = aom_img_plane_height(img, plane);
197 int y;
198
199 for (y = 0; y < h; ++y) {
200 fwrite(buf, 1, w, file);
201 buf += stride;
202 }
203 }
204 }
205
aom_img_read(aom_image_t * img,FILE * file)206 int aom_img_read(aom_image_t *img, FILE *file) {
207 int plane;
208
209 for (plane = 0; plane < 3; ++plane) {
210 unsigned char *buf = img->planes[plane];
211 const int stride = img->stride[plane];
212 const int w = aom_img_plane_width(img, plane) *
213 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
214 const int h = aom_img_plane_height(img, plane);
215 int y;
216
217 for (y = 0; y < h; ++y) {
218 if (fread(buf, 1, w, file) != (size_t)w) return 0;
219 buf += stride;
220 }
221 }
222
223 return 1;
224 }
225
226 // TODO(dkovalev) change sse_to_psnr signature: double -> int64_t
sse_to_psnr(double samples,double peak,double sse)227 double sse_to_psnr(double samples, double peak, double sse) {
228 static const double kMaxPSNR = 100.0;
229
230 if (sse > 0.0) {
231 const double psnr = 10.0 * log10(samples * peak * peak / sse);
232 return psnr > kMaxPSNR ? kMaxPSNR : psnr;
233 } else {
234 return kMaxPSNR;
235 }
236 }
237
238 // 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)239 static void highbd_img_upshift(aom_image_t *dst, const aom_image_t *src,
240 int input_shift) {
241 // Note the offset is 1 less than half.
242 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
243 int plane;
244 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
245 dst->x_chroma_shift != src->x_chroma_shift ||
246 dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
247 input_shift < 0) {
248 fatal("Unsupported image conversion");
249 }
250 switch (src->fmt) {
251 case AOM_IMG_FMT_I42016:
252 case AOM_IMG_FMT_I42216:
253 case AOM_IMG_FMT_I44416: break;
254 default: fatal("Unsupported image conversion"); break;
255 }
256 for (plane = 0; plane < 3; plane++) {
257 int w = src->d_w;
258 int h = src->d_h;
259 int x, y;
260 if (plane) {
261 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
262 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
263 }
264 for (y = 0; y < h; y++) {
265 const uint16_t *p_src =
266 (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
267 uint16_t *p_dst =
268 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
269 for (x = 0; x < w; x++) *p_dst++ = (*p_src++ << input_shift) + offset;
270 }
271 }
272 }
273
lowbd_img_upshift(aom_image_t * dst,const aom_image_t * src,int input_shift)274 static void lowbd_img_upshift(aom_image_t *dst, const aom_image_t *src,
275 int input_shift) {
276 // Note the offset is 1 less than half.
277 const int offset = input_shift > 0 ? (1 << (input_shift - 1)) - 1 : 0;
278 int plane;
279 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
280 dst->x_chroma_shift != src->x_chroma_shift ||
281 dst->y_chroma_shift != src->y_chroma_shift ||
282 dst->fmt != src->fmt + AOM_IMG_FMT_HIGHBITDEPTH || input_shift < 0) {
283 fatal("Unsupported image conversion");
284 }
285 switch (src->fmt) {
286 case AOM_IMG_FMT_I420:
287 case AOM_IMG_FMT_I422:
288 case AOM_IMG_FMT_I444: break;
289 default: fatal("Unsupported image conversion"); break;
290 }
291 for (plane = 0; plane < 3; plane++) {
292 int w = src->d_w;
293 int h = src->d_h;
294 int x, y;
295 if (plane) {
296 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
297 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
298 }
299 for (y = 0; y < h; y++) {
300 const uint8_t *p_src = src->planes[plane] + y * src->stride[plane];
301 uint16_t *p_dst =
302 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
303 for (x = 0; x < w; x++) {
304 *p_dst++ = (*p_src++ << input_shift) + offset;
305 }
306 }
307 }
308 }
309
aom_img_upshift(aom_image_t * dst,const aom_image_t * src,int input_shift)310 void aom_img_upshift(aom_image_t *dst, const aom_image_t *src,
311 int input_shift) {
312 if (src->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
313 highbd_img_upshift(dst, src, input_shift);
314 } else {
315 lowbd_img_upshift(dst, src, input_shift);
316 }
317 }
318
aom_img_truncate_16_to_8(aom_image_t * dst,const aom_image_t * src)319 void aom_img_truncate_16_to_8(aom_image_t *dst, const aom_image_t *src) {
320 int plane;
321 if (dst->fmt + AOM_IMG_FMT_HIGHBITDEPTH != src->fmt || dst->d_w != src->d_w ||
322 dst->d_h != src->d_h || dst->x_chroma_shift != src->x_chroma_shift ||
323 dst->y_chroma_shift != src->y_chroma_shift) {
324 fatal("Unsupported image conversion");
325 }
326 switch (dst->fmt) {
327 case AOM_IMG_FMT_I420:
328 case AOM_IMG_FMT_I422:
329 case AOM_IMG_FMT_I444: break;
330 default: fatal("Unsupported image conversion"); break;
331 }
332 for (plane = 0; plane < 3; plane++) {
333 int w = src->d_w;
334 int h = src->d_h;
335 int x, y;
336 if (plane) {
337 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
338 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
339 }
340 for (y = 0; y < h; y++) {
341 const uint16_t *p_src =
342 (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
343 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
344 for (x = 0; x < w; x++) {
345 *p_dst++ = (uint8_t)(*p_src++);
346 }
347 }
348 }
349 }
350
highbd_img_downshift(aom_image_t * dst,const aom_image_t * src,int down_shift)351 static void highbd_img_downshift(aom_image_t *dst, const aom_image_t *src,
352 int down_shift) {
353 int plane;
354 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
355 dst->x_chroma_shift != src->x_chroma_shift ||
356 dst->y_chroma_shift != src->y_chroma_shift || dst->fmt != src->fmt ||
357 down_shift < 0) {
358 fatal("Unsupported image conversion");
359 }
360 switch (src->fmt) {
361 case AOM_IMG_FMT_I42016:
362 case AOM_IMG_FMT_I42216:
363 case AOM_IMG_FMT_I44416: break;
364 default: fatal("Unsupported image conversion"); break;
365 }
366 for (plane = 0; plane < 3; plane++) {
367 int w = src->d_w;
368 int h = src->d_h;
369 int x, y;
370 if (plane) {
371 w = (w + src->x_chroma_shift) >> src->x_chroma_shift;
372 h = (h + src->y_chroma_shift) >> src->y_chroma_shift;
373 }
374 for (y = 0; y < h; y++) {
375 const uint16_t *p_src =
376 (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
377 uint16_t *p_dst =
378 (uint16_t *)(dst->planes[plane] + y * dst->stride[plane]);
379 for (x = 0; x < w; x++) *p_dst++ = *p_src++ >> down_shift;
380 }
381 }
382 }
383
lowbd_img_downshift(aom_image_t * dst,const aom_image_t * src,int down_shift)384 static void lowbd_img_downshift(aom_image_t *dst, const aom_image_t *src,
385 int down_shift) {
386 int plane;
387 if (dst->d_w != src->d_w || dst->d_h != src->d_h ||
388 dst->x_chroma_shift != src->x_chroma_shift ||
389 dst->y_chroma_shift != src->y_chroma_shift ||
390 src->fmt != dst->fmt + AOM_IMG_FMT_HIGHBITDEPTH || down_shift < 0) {
391 fatal("Unsupported image conversion");
392 }
393 switch (dst->fmt) {
394 case AOM_IMG_FMT_I420:
395 case AOM_IMG_FMT_I422:
396 case AOM_IMG_FMT_I444: 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 const uint16_t *p_src =
409 (const uint16_t *)(src->planes[plane] + y * src->stride[plane]);
410 uint8_t *p_dst = dst->planes[plane] + y * dst->stride[plane];
411 for (x = 0; x < w; x++) {
412 *p_dst++ = *p_src++ >> down_shift;
413 }
414 }
415 }
416 }
417
aom_img_downshift(aom_image_t * dst,const aom_image_t * src,int down_shift)418 void aom_img_downshift(aom_image_t *dst, const aom_image_t *src,
419 int down_shift) {
420 if (dst->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
421 highbd_img_downshift(dst, src, down_shift);
422 } else {
423 lowbd_img_downshift(dst, src, down_shift);
424 }
425 }
426