xref: /aosp_15_r20/external/libvpx/tools_common.h (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 #ifndef VPX_TOOLS_COMMON_H_
11 #define VPX_TOOLS_COMMON_H_
12 
13 #include <stdio.h>
14 
15 #include "./vpx_config.h"
16 #include "vpx/vpx_codec.h"
17 #include "vpx/vpx_image.h"
18 #include "vpx/vpx_integer.h"
19 
20 #if CONFIG_ENCODERS
21 #include "./y4minput.h"
22 #endif
23 
24 #if defined(_MSC_VER)
25 /* MSVS uses _f{seek,tell}i64. */
26 #define fseeko _fseeki64
27 #define ftello _ftelli64
28 typedef int64_t FileOffset;
29 #elif defined(_WIN32)
30 /* MinGW uses f{seek,tell}o64 for large files. */
31 #define fseeko fseeko64
32 #define ftello ftello64
33 typedef off64_t FileOffset;
34 #elif CONFIG_OS_SUPPORT &&                                                  \
35     !(defined(__ANDROID__) && __ANDROID_API__ < 24 && !defined(__LP64__) && \
36       defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
37 /* POSIX.1 has fseeko and ftello. fseeko and ftello are not available before
38  * Android API level 24. See
39  * https://android.googlesource.com/platform/bionic/+/main/docs/32-bit-abi.md */
40 #include <sys/types.h> /* NOLINT */
41 typedef off_t FileOffset;
42 /* Use 32-bit file operations in WebM file format when building ARM
43  * executables (.axf) with RVCT. */
44 #else
45 #define fseeko fseek
46 #define ftello ftell
47 typedef long FileOffset; /* NOLINT */
48 #endif /* CONFIG_OS_SUPPORT */
49 
50 #if CONFIG_OS_SUPPORT
51 #if defined(_MSC_VER)
52 #include <io.h> /* NOLINT */
53 #define isatty _isatty
54 #define fileno _fileno
55 #else
56 #include <unistd.h> /* NOLINT */
57 #endif              /* _MSC_VER */
58 #endif              /* CONFIG_OS_SUPPORT */
59 
60 #define LITERALU64(hi, lo) ((((uint64_t)hi) << 32) | lo)
61 
62 #ifndef PATH_MAX
63 #define PATH_MAX 512
64 #endif
65 
66 #define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */
67 #define IVF_FILE_HDR_SZ 32
68 
69 #define RAW_FRAME_HDR_SZ sizeof(uint32_t)
70 
71 #define VP8_FOURCC 0x30385056
72 #define VP9_FOURCC 0x30395056
73 
74 enum VideoFileType {
75   FILE_TYPE_RAW,
76   FILE_TYPE_IVF,
77   FILE_TYPE_Y4M,
78   FILE_TYPE_WEBM
79 };
80 
81 struct FileTypeDetectionBuffer {
82   char buf[4];
83   size_t buf_read;
84   size_t position;
85 };
86 
87 struct VpxRational {
88   int numerator;
89   int denominator;
90 };
91 
92 struct VpxInputContext {
93   const char *filename;
94   FILE *file;
95   int64_t length;
96   struct FileTypeDetectionBuffer detect;
97   enum VideoFileType file_type;
98   uint32_t width;
99   uint32_t height;
100   struct VpxRational pixel_aspect_ratio;
101   vpx_img_fmt_t fmt;
102   vpx_bit_depth_t bit_depth;
103   int only_i420;
104   uint32_t fourcc;
105   struct VpxRational framerate;
106 #if CONFIG_ENCODERS
107   y4m_input y4m;
108 #endif
109 };
110 
111 #ifdef __cplusplus
112 extern "C" {
113 #endif
114 
115 #if defined(__GNUC__)
116 #define VPX_NO_RETURN __attribute__((noreturn))
117 #elif defined(_MSC_VER)
118 #define VPX_NO_RETURN __declspec(noreturn)
119 #else
120 #define VPX_NO_RETURN
121 #endif
122 
123 // Tells the compiler to perform `printf` format string checking if the
124 // compiler supports it; see the 'format' attribute in
125 // <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html>.
126 #define VPX_TOOLS_FORMAT_PRINTF(string_index, first_to_check)
127 #if defined(__has_attribute)
128 #if __has_attribute(format)
129 #undef VPX_TOOLS_FORMAT_PRINTF
130 #define VPX_TOOLS_FORMAT_PRINTF(string_index, first_to_check) \
131   __attribute__((__format__(__printf__, string_index, first_to_check)))
132 #endif
133 #endif
134 
135 /* Sets a stdio stream into binary mode */
136 FILE *set_binary_mode(FILE *stream);
137 
138 VPX_NO_RETURN void die(const char *fmt, ...) VPX_TOOLS_FORMAT_PRINTF(1, 2);
139 VPX_NO_RETURN void fatal(const char *fmt, ...) VPX_TOOLS_FORMAT_PRINTF(1, 2);
140 void warn(const char *fmt, ...) VPX_TOOLS_FORMAT_PRINTF(1, 2);
141 
142 VPX_NO_RETURN void die_codec(vpx_codec_ctx_t *ctx, const char *s);
143 
144 /* The tool including this file must define usage_exit() */
145 VPX_NO_RETURN void usage_exit(void);
146 
147 #undef VPX_NO_RETURN
148 
149 int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame);
150 
151 typedef struct VpxInterface {
152   const char *name;
153   uint32_t fourcc;
154   vpx_codec_iface_t *(*codec_interface)(void);
155 } VpxInterface;
156 
157 int get_vpx_encoder_count(void);
158 const VpxInterface *get_vpx_encoder_by_index(int i);
159 const VpxInterface *get_vpx_encoder_by_name(const char *name);
160 
161 int get_vpx_decoder_count(void);
162 const VpxInterface *get_vpx_decoder_by_index(int i);
163 const VpxInterface *get_vpx_decoder_by_name(const char *name);
164 const VpxInterface *get_vpx_decoder_by_fourcc(uint32_t fourcc);
165 
166 int vpx_img_plane_width(const vpx_image_t *img, int plane);
167 int vpx_img_plane_height(const vpx_image_t *img, int plane);
168 void vpx_img_write(const vpx_image_t *img, FILE *file);
169 int vpx_img_read(vpx_image_t *img, FILE *file);
170 
171 double sse_to_psnr(double samples, double peak, double mse);
172 
173 #if CONFIG_ENCODERS
174 int read_frame(struct VpxInputContext *input_ctx, vpx_image_t *img);
175 int file_is_y4m(const char detect[4]);
176 int fourcc_is_ivf(const char detect[4]);
177 void open_input_file(struct VpxInputContext *input);
178 void close_input_file(struct VpxInputContext *input);
179 #endif
180 
181 #if CONFIG_VP9_HIGHBITDEPTH
182 void vpx_img_upshift(vpx_image_t *dst, vpx_image_t *src, int input_shift);
183 void vpx_img_downshift(vpx_image_t *dst, vpx_image_t *src, int down_shift);
184 void vpx_img_truncate_16_to_8(vpx_image_t *dst, vpx_image_t *src);
185 #endif
186 
187 int compare_img(const vpx_image_t *const img1, const vpx_image_t *const img2);
188 #if CONFIG_VP9_HIGHBITDEPTH
189 void find_mismatch_high(const vpx_image_t *const img1,
190                         const vpx_image_t *const img2, int yloc[4], int uloc[4],
191                         int vloc[4]);
192 #endif
193 void find_mismatch(const vpx_image_t *const img1, const vpx_image_t *const img2,
194                    int yloc[4], int uloc[4], int vloc[4]);
195 
196 #ifdef __cplusplus
197 } /* extern "C" */
198 #endif
199 
200 #endif  // VPX_TOOLS_COMMON_H_
201