John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 10 | */ |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 11 | #ifndef TOOLS_COMMON_H_ |
| 12 | #define TOOLS_COMMON_H_ |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 16 | #include "./aom_config.h" |
| 17 | #include "aom/aom_codec.h" |
| 18 | #include "aom/aom_image.h" |
| 19 | #include "aom/aom_integer.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 20 | #include "aom_ports/msvc.h" |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 21 | |
| 22 | #if CONFIG_ENCODERS |
| 23 | #include "./y4minput.h" |
| 24 | #endif |
Tom Finegan | e657919 | 2013-11-11 06:48:18 -0800 | [diff] [blame] | 25 | |
| 26 | #if defined(_MSC_VER) |
James Zern | dae4cfc | 2014-04-02 20:06:07 -0700 | [diff] [blame] | 27 | /* MSVS uses _f{seek,tell}i64. */ |
Tom Finegan | e657919 | 2013-11-11 06:48:18 -0800 | [diff] [blame] | 28 | #define fseeko _fseeki64 |
| 29 | #define ftello _ftelli64 |
James Zern | b643036 | 2017-01-14 14:18:01 -0800 | [diff] [blame] | 30 | typedef int64_t FileOffset; |
Tom Finegan | e657919 | 2013-11-11 06:48:18 -0800 | [diff] [blame] | 31 | #elif defined(_WIN32) |
James Zern | b643036 | 2017-01-14 14:18:01 -0800 | [diff] [blame] | 32 | #include <sys/types.h> /* NOLINT*/ |
James Zern | e3578af | 2014-04-17 10:47:08 -0700 | [diff] [blame] | 33 | /* MinGW uses f{seek,tell}o64 for large files. */ |
Tom Finegan | e657919 | 2013-11-11 06:48:18 -0800 | [diff] [blame] | 34 | #define fseeko fseeko64 |
| 35 | #define ftello ftello64 |
James Zern | b643036 | 2017-01-14 14:18:01 -0800 | [diff] [blame] | 36 | typedef off64_t FileOffset; |
| 37 | #elif CONFIG_OS_SUPPORT |
| 38 | #include <sys/types.h> /* NOLINT*/ |
| 39 | typedef off_t FileOffset; |
| 40 | /* Use 32-bit file operations in WebM file format when building ARM |
| 41 | * executables (.axf) with RVCT. */ |
| 42 | #else |
| 43 | #define fseeko fseek |
| 44 | #define ftello ftell |
Tom Finegan | 92ca095 | 2017-01-19 14:39:04 -0800 | [diff] [blame] | 45 | typedef long FileOffset; /* NOLINT */ |
James Zern | b643036 | 2017-01-14 14:18:01 -0800 | [diff] [blame] | 46 | #endif /* CONFIG_OS_SUPPORT */ |
Tom Finegan | e657919 | 2013-11-11 06:48:18 -0800 | [diff] [blame] | 47 | |
| 48 | #if CONFIG_OS_SUPPORT |
| 49 | #if defined(_MSC_VER) |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 50 | #include <io.h> /* NOLINT */ |
| 51 | #define isatty _isatty |
| 52 | #define fileno _fileno |
Tom Finegan | e657919 | 2013-11-11 06:48:18 -0800 | [diff] [blame] | 53 | #else |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 54 | #include <unistd.h> /* NOLINT */ |
| 55 | #endif /* _MSC_VER */ |
| 56 | #endif /* CONFIG_OS_SUPPORT */ |
Tom Finegan | e657919 | 2013-11-11 06:48:18 -0800 | [diff] [blame] | 57 | |
Tom Finegan | e657919 | 2013-11-11 06:48:18 -0800 | [diff] [blame] | 58 | #define LITERALU64(hi, lo) ((((uint64_t)hi) << 32) | lo) |
| 59 | |
| 60 | #ifndef PATH_MAX |
| 61 | #define PATH_MAX 512 |
| 62 | #endif |
| 63 | |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 64 | #define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */ |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 65 | #define IVF_FILE_HDR_SZ 32 |
| 66 | |
| 67 | #define RAW_FRAME_HDR_SZ sizeof(uint32_t) |
| 68 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 69 | #define AV1_FOURCC 0x31305641 |
John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 70 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 71 | enum VideoFileType { |
| 72 | FILE_TYPE_RAW, |
| 73 | FILE_TYPE_IVF, |
| 74 | FILE_TYPE_Y4M, |
| 75 | FILE_TYPE_WEBM |
| 76 | }; |
| 77 | |
| 78 | struct FileTypeDetectionBuffer { |
| 79 | char buf[4]; |
| 80 | size_t buf_read; |
| 81 | size_t position; |
| 82 | }; |
| 83 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 84 | struct AvxRational { |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 85 | int numerator; |
| 86 | int denominator; |
| 87 | }; |
| 88 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 89 | struct AvxInputContext { |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 90 | const char *filename; |
| 91 | FILE *file; |
James Zern | e3578af | 2014-04-17 10:47:08 -0700 | [diff] [blame] | 92 | int64_t length; |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 93 | struct FileTypeDetectionBuffer detect; |
| 94 | enum VideoFileType file_type; |
Tom Finegan | 2abe2d4 | 2013-11-18 14:39:51 -0800 | [diff] [blame] | 95 | uint32_t width; |
| 96 | uint32_t height; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 97 | struct AvxRational pixel_aspect_ratio; |
| 98 | aom_img_fmt_t fmt; |
| 99 | aom_bit_depth_t bit_depth; |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 100 | int only_i420; |
Tom Finegan | 2abe2d4 | 2013-11-18 14:39:51 -0800 | [diff] [blame] | 101 | uint32_t fourcc; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 102 | struct AvxRational framerate; |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 103 | #if CONFIG_ENCODERS |
| 104 | y4m_input y4m; |
| 105 | #endif |
| 106 | }; |
| 107 | |
| 108 | #ifdef __cplusplus |
| 109 | extern "C" { |
| 110 | #endif |
| 111 | |
Jim Bankoski | e9b878c | 2014-08-21 11:43:36 -0700 | [diff] [blame] | 112 | #if defined(__GNUC__) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 113 | #define AOM_NO_RETURN __attribute__((noreturn)) |
Jim Bankoski | e9b878c | 2014-08-21 11:43:36 -0700 | [diff] [blame] | 114 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 115 | #define AOM_NO_RETURN |
Jim Bankoski | e9b878c | 2014-08-21 11:43:36 -0700 | [diff] [blame] | 116 | #endif |
| 117 | |
John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 118 | /* Sets a stdio stream into binary mode */ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 119 | FILE *set_binary_mode(FILE *stream); |
John Koleszar | c377bf0 | 2010-11-02 09:11:57 -0400 | [diff] [blame] | 120 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 121 | void die(const char *fmt, ...) AOM_NO_RETURN; |
| 122 | void fatal(const char *fmt, ...) AOM_NO_RETURN; |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 123 | void warn(const char *fmt, ...); |
| 124 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 125 | void die_codec(aom_codec_ctx_t *ctx, const char *s) AOM_NO_RETURN; |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 126 | |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 127 | /* The tool including this file must define usage_exit() */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 128 | void usage_exit(void) AOM_NO_RETURN; |
Jim Bankoski | e9b878c | 2014-08-21 11:43:36 -0700 | [diff] [blame] | 129 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 130 | #undef AOM_NO_RETURN |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 131 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 132 | int read_yuv_frame(struct AvxInputContext *input_ctx, aom_image_t *yuv_frame); |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 133 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 134 | typedef struct AvxInterface { |
Dmitry Kovalev | 70d9664 | 2014-02-11 21:12:23 -0800 | [diff] [blame] | 135 | const char *const name; |
| 136 | const uint32_t fourcc; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 137 | aom_codec_iface_t *(*const codec_interface)(); |
| 138 | } AvxInterface; |
Dmitry Kovalev | 70d9664 | 2014-02-11 21:12:23 -0800 | [diff] [blame] | 139 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 140 | int get_aom_encoder_count(void); |
| 141 | const AvxInterface *get_aom_encoder_by_index(int i); |
| 142 | const AvxInterface *get_aom_encoder_by_name(const char *name); |
Dmitry Kovalev | 70d9664 | 2014-02-11 21:12:23 -0800 | [diff] [blame] | 143 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 144 | int get_aom_decoder_count(void); |
| 145 | const AvxInterface *get_aom_decoder_by_index(int i); |
| 146 | const AvxInterface *get_aom_decoder_by_name(const char *name); |
| 147 | const AvxInterface *get_aom_decoder_by_fourcc(uint32_t fourcc); |
Dmitry Kovalev | 7ec2769 | 2014-01-27 13:40:29 -0800 | [diff] [blame] | 148 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 149 | // TODO(dkovalev): move this function to aom_image.{c, h}, so it will be part |
| 150 | // of aom_image_t support |
| 151 | int aom_img_plane_width(const aom_image_t *img, int plane); |
| 152 | int aom_img_plane_height(const aom_image_t *img, int plane); |
| 153 | void aom_img_write(const aom_image_t *img, FILE *file); |
| 154 | int aom_img_read(aom_image_t *img, FILE *file); |
Dmitry Kovalev | 592936b | 2014-02-07 11:37:39 -0800 | [diff] [blame] | 155 | |
Dmitry Kovalev | 2dad0e1 | 2014-02-27 14:00:41 -0800 | [diff] [blame] | 156 | double sse_to_psnr(double samples, double peak, double mse); |
| 157 | |
Sebastien Alaiwan | 71e8784 | 2017-04-12 16:03:28 +0200 | [diff] [blame] | 158 | #if CONFIG_HIGHBITDEPTH |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 159 | void aom_img_upshift(aom_image_t *dst, aom_image_t *src, int input_shift); |
| 160 | void aom_img_downshift(aom_image_t *dst, aom_image_t *src, int down_shift); |
| 161 | void aom_img_truncate_16_to_8(aom_image_t *dst, aom_image_t *src); |
Deb Mukherjee | 7a2a611 | 2014-10-06 20:46:11 -0700 | [diff] [blame] | 162 | #endif |
| 163 | |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 164 | #ifdef __cplusplus |
clang-format | 6c4d83e | 2016-08-08 19:03:30 -0700 | [diff] [blame] | 165 | } /* extern "C" */ |
Tom Finegan | 00a35aa | 2013-11-14 12:37:42 -0800 | [diff] [blame] | 166 | #endif |
| 167 | |
Tom Finegan | 03848f5 | 2013-11-05 10:02:18 -0800 | [diff] [blame] | 168 | #endif // TOOLS_COMMON_H_ |