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