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