xref: /aosp_15_r20/external/libaom/common/tools_common.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 #ifndef AOM_COMMON_TOOLS_COMMON_H_
12 #define AOM_COMMON_TOOLS_COMMON_H_
13 
14 #include <stdbool.h>
15 #include <stdio.h>
16 
17 #include "config/aom_config.h"
18 
19 #include "aom/aom_codec.h"
20 #include "aom/aom_image.h"
21 #include "aom/aom_integer.h"
22 #include "aom_ports/mem.h"
23 
24 #if CONFIG_AV1_ENCODER
25 #include "common/y4minput.h"
26 #endif
27 
28 #if defined(_MSC_VER)
29 /* MSVS uses _f{seek,tell}i64. */
30 #define fseeko _fseeki64
31 #define ftello _ftelli64
32 typedef int64_t FileOffset;
33 #elif defined(_WIN32)
34 #include <sys/types.h> /* NOLINT*/
35 /* MinGW uses f{seek,tell}o64 for large files. */
36 #define fseeko fseeko64
37 #define ftello ftello64
38 typedef off64_t FileOffset;
39 #elif CONFIG_OS_SUPPORT &&                                                  \
40     !(defined(__ANDROID__) && __ANDROID_API__ < 24 && !defined(__LP64__) && \
41       defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
42 /* POSIX.1 has fseeko and ftello. fseeko and ftello are not available before
43  * Android API level 24. See
44  * https://android.googlesource.com/platform/bionic/+/main/docs/32-bit-abi.md */
45 #include <sys/types.h> /* NOLINT */
46 typedef off_t FileOffset;
47 /* Use 32-bit file operations in WebM file format when building ARM
48  * executables (.axf) with RVCT. */
49 #else
50 #define fseeko fseek
51 #define ftello ftell
52 typedef long FileOffset; /* NOLINT */
53 #endif /* CONFIG_OS_SUPPORT */
54 
55 #if CONFIG_OS_SUPPORT
56 #if defined(_MSC_VER)
57 #include <io.h> /* NOLINT */
58 #define isatty _isatty
59 #define fileno _fileno
60 #else
61 #include <unistd.h> /* NOLINT */
62 #endif              /* _MSC_VER */
63 #endif              /* CONFIG_OS_SUPPORT */
64 
65 #define LITERALU64(hi, lo) ((((uint64_t)hi) << 32) | lo)
66 
67 #ifndef PATH_MAX
68 #define PATH_MAX 512
69 #endif
70 
71 #define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */
72 #define IVF_FILE_HDR_SZ 32
73 
74 #define RAW_FRAME_HDR_SZ sizeof(uint32_t)
75 #define OBU_DETECTION_SZ 34  // See common/obudec.c
76 
77 #define DETECT_BUF_SZ 34  // Max of the above header sizes
78 
79 #define AV1_FOURCC 0x31305641
80 
81 enum VideoFileType {
82   FILE_TYPE_OBU,
83   FILE_TYPE_RAW,
84   FILE_TYPE_IVF,
85   FILE_TYPE_Y4M,
86   FILE_TYPE_WEBM
87 };
88 
89 // The fourcc for large_scale_tile encoding is "LSTC".
90 #define LST_FOURCC 0x4354534c
91 
92 struct FileTypeDetectionBuffer {
93   char buf[DETECT_BUF_SZ];
94   size_t buf_read;
95   size_t position;
96 };
97 
98 struct AvxRational {
99   int numerator;
100   int denominator;
101 };
102 
103 struct AvxInputContext {
104   const char *filename;
105   FILE *file;
106   int64_t length;
107   struct FileTypeDetectionBuffer detect;
108   enum VideoFileType file_type;
109   uint32_t width;
110   uint32_t height;
111   struct AvxRational pixel_aspect_ratio;
112   aom_img_fmt_t fmt;
113   aom_bit_depth_t bit_depth;
114   int only_i420;
115   uint32_t fourcc;
116   struct AvxRational framerate;
117 #if CONFIG_AV1_ENCODER
118   y4m_input y4m;
119 #endif
120   aom_color_range_t color_range;
121 };
122 
123 #ifdef __cplusplus
124 extern "C" {
125 #endif
126 
127 #if defined(__GNUC__)
128 #define AOM_NO_RETURN __attribute__((noreturn))
129 #elif defined(_MSC_VER)
130 #define AOM_NO_RETURN __declspec(noreturn)
131 #else
132 #define AOM_NO_RETURN
133 #endif
134 
135 // Tells the compiler to perform `printf` format string checking if the
136 // compiler supports it; see the 'format' attribute in
137 // <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html>.
138 #define AOM_TOOLS_FORMAT_PRINTF(string_index, first_to_check)
139 #if defined(__has_attribute)
140 #if __has_attribute(format)
141 #undef AOM_TOOLS_FORMAT_PRINTF
142 #define AOM_TOOLS_FORMAT_PRINTF(string_index, first_to_check) \
143   __attribute__((__format__(__printf__, string_index, first_to_check)))
144 #endif
145 #endif
146 
147 /* Sets a stdio stream into binary mode */
148 FILE *set_binary_mode(FILE *stream);
149 
150 AOM_NO_RETURN void die(const char *fmt, ...) AOM_TOOLS_FORMAT_PRINTF(1, 2);
151 AOM_NO_RETURN void fatal(const char *fmt, ...) AOM_TOOLS_FORMAT_PRINTF(1, 2);
152 void aom_tools_warn(const char *fmt, ...) AOM_TOOLS_FORMAT_PRINTF(1, 2);
153 
154 AOM_NO_RETURN void die_codec(aom_codec_ctx_t *ctx, const char *s);
155 
156 /* The tool including this file must define usage_exit() */
157 AOM_NO_RETURN void usage_exit(void);
158 
159 #undef AOM_NO_RETURN
160 
161 // The AOM library can support different encoders / decoders. These
162 // functions provide different ways to lookup / iterate through them.
163 // The return result may be NULL to indicate no codec was found.
164 int get_aom_encoder_count(void);
165 aom_codec_iface_t *get_aom_encoder_by_index(int i);
166 aom_codec_iface_t *get_aom_encoder_by_short_name(const char *name);
167 // If the interface is unknown, returns NULL.
168 const char *get_short_name_by_aom_encoder(aom_codec_iface_t *encoder);
169 // If the interface is unknown, returns 0.
170 uint32_t get_fourcc_by_aom_encoder(aom_codec_iface_t *iface);
171 
172 int get_aom_decoder_count(void);
173 aom_codec_iface_t *get_aom_decoder_by_index(int i);
174 aom_codec_iface_t *get_aom_decoder_by_short_name(const char *name);
175 aom_codec_iface_t *get_aom_decoder_by_fourcc(uint32_t fourcc);
176 const char *get_short_name_by_aom_decoder(aom_codec_iface_t *decoder);
177 // If the interface is unknown, returns 0.
178 uint32_t get_fourcc_by_aom_decoder(aom_codec_iface_t *iface);
179 
180 const char *image_format_to_string(aom_img_fmt_t fmt);
181 
182 int read_yuv_frame(struct AvxInputContext *input_ctx, aom_image_t *yuv_frame);
183 
184 void aom_img_write(const aom_image_t *img, FILE *file);
185 // Returns true on success, false on failure.
186 bool aom_img_read(aom_image_t *img, FILE *file);
187 
188 double sse_to_psnr(double samples, double peak, double mse);
189 void aom_img_upshift(aom_image_t *dst, const aom_image_t *src, int input_shift);
190 void aom_img_downshift(aom_image_t *dst, const aom_image_t *src,
191                        int down_shift);
192 // Returns true on success, false on failure.
193 bool aom_shift_img(unsigned int output_bit_depth, aom_image_t **img_ptr,
194                    aom_image_t **img_shifted_ptr);
195 void aom_img_truncate_16_to_8(aom_image_t *dst, const aom_image_t *src);
196 
197 // Output in NV12 format.
198 void aom_img_write_nv12(const aom_image_t *img, FILE *file);
199 
200 size_t read_from_input(struct AvxInputContext *input_ctx, size_t n,
201                        unsigned char *buf);
202 size_t input_to_detect_buf(struct AvxInputContext *input_ctx, size_t n);
203 size_t buffer_input(struct AvxInputContext *input_ctx, size_t n,
204                     unsigned char *buf, bool buffered);
205 void rewind_detect(struct AvxInputContext *input_ctx);
206 bool input_eof(struct AvxInputContext *input_ctx);
207 
208 #ifdef __cplusplus
209 } /* extern "C" */
210 #endif
211 
212 #endif  // AOM_COMMON_TOOLS_COMMON_H_
213