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 <stdio.h> 15 16 #include "config/aom_config.h" 17 18 #include "aom/aom_codec.h" 19 #include "aom/aom_image.h" 20 #include "aom/aom_integer.h" 21 #include "aom_ports/mem.h" 22 #include "aom_ports/msvc.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 #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 AV1_FOURCC 0x31305641 72 73 enum VideoFileType { 74 FILE_TYPE_OBU, 75 FILE_TYPE_RAW, 76 FILE_TYPE_IVF, 77 FILE_TYPE_Y4M, 78 FILE_TYPE_WEBM 79 }; 80 81 // Used in lightfield example. 82 enum { 83 YUV1D, // 1D tile output for conformance test. 84 YUV, // Tile output in YUV format. 85 NV12, // Tile output in NV12 format. 86 } UENUM1BYTE(OUTPUT_FORMAT); 87 88 // The fourcc for large_scale_tile encoding is "LSTC". 89 #define LST_FOURCC 0x4354534c 90 91 struct FileTypeDetectionBuffer { 92 char buf[4]; 93 size_t buf_read; 94 size_t position; 95 }; 96 97 struct AvxRational { 98 int numerator; 99 int denominator; 100 }; 101 102 struct AvxInputContext { 103 const char *filename; 104 FILE *file; 105 int64_t length; 106 struct FileTypeDetectionBuffer detect; 107 enum VideoFileType file_type; 108 uint32_t width; 109 uint32_t height; 110 struct AvxRational pixel_aspect_ratio; 111 aom_img_fmt_t fmt; 112 aom_bit_depth_t bit_depth; 113 int only_i420; 114 uint32_t fourcc; 115 struct AvxRational framerate; 116 #if CONFIG_AV1_ENCODER 117 y4m_input y4m; 118 #endif 119 }; 120 121 #ifdef __cplusplus 122 extern "C" { 123 #endif 124 125 #if defined(__GNUC__) 126 #define AOM_NO_RETURN __attribute__((noreturn)) 127 #else 128 #define AOM_NO_RETURN 129 #endif 130 131 /* Sets a stdio stream into binary mode */ 132 FILE *set_binary_mode(FILE *stream); 133 134 void die(const char *fmt, ...) AOM_NO_RETURN; 135 void fatal(const char *fmt, ...) AOM_NO_RETURN; 136 void warn(const char *fmt, ...); 137 138 void die_codec(aom_codec_ctx_t *ctx, const char *s) AOM_NO_RETURN; 139 140 /* The tool including this file must define usage_exit() */ 141 void usage_exit(void) AOM_NO_RETURN; 142 143 #undef AOM_NO_RETURN 144 145 int read_yuv_frame(struct AvxInputContext *input_ctx, aom_image_t *yuv_frame); 146 147 /////////////////////////////////////////////////////////////////////////////// 148 // A description of the interfaces used to access the AOM codecs 149 /////////////////////////////////////////////////////////////////////////////// 150 // 151 // There are three levels of interfaces used to access the AOM codec: the 152 // AVXInterface, the aom_codec_iface, and the aom_codec_ctx. Each of these 153 // is described in detail here. 154 // 155 // 156 // 1. AVXInterface 157 // (Related files: common/tools_common.c, common/tools_common.h) 158 // 159 // The high-level interface to the AVx encoders / decoders. Each AvxInterface 160 // contains the name of the codec (e.g., "av1"), the four character code 161 // associated with it, and a function pointer to the actual interface (see the 162 // documentation on aom_codec_iface_t for more info). This API 163 // is meant for lookup / iteration over all known codecs. 164 // 165 // For the encoder, call get_aom_encoder_by_name(...) if you know the name 166 // (e.g., "av1"); to iterate over all known encoders, use 167 // get_aom_encoder_count() and get_aom_encoder_by_index(i). To get the 168 // encoder specifically for large scale tile encoding, use 169 // get_aom_lst_encoder(). 170 // 171 // For the decoder, similar functions are available. There is also a 172 // get_aom_decoder_by_fourcc(fourcc) to get the decoder based on the four 173 // character codes. 174 // 175 // The main purpose of the AVXInterface is to get a reference to the 176 // aom_codec_interface_t, pointed to by its codec_interface variable. 177 // 178 // 179 // 2. aom_codec_iface_t 180 // (Related files: aom/aom_codec.h, aom/src/aom_codec.c, 181 // aom/internal/aom_codec_internal.h, av1/av1_cx_iface.c, 182 // av1/av1_dx_iface.c) 183 // 184 // Used to initialize the codec context, which contains the configuration for 185 // for modifying the encoder/decoder during run-time. See the documentation of 186 // aom/aom_codec.h for more details. For the most part, users will call the 187 // helper functions listed there, such as aom_codec_iface_name, 188 // aom_codec_get_caps, etc., to interact with it. 189 // 190 // The main purpose of the aom_codec_iface_t is to provide a way to generate 191 // a default codec config, find out what capabilities the implementation has, 192 // and create an aom_codec_ctx_t (which is actually used to interact with the 193 // codec). 194 // 195 // Note that the implementations of the aom_codec_iface_t are located in 196 // av1/av1_cx_iface.c and av1/av1_dx_iface.c 197 // 198 // 199 // 3. aom_codec_ctx_t 200 // (Related files: aom/aom_codec.h, av1/av1_cx_iface.c, av1/av1_dx_iface.c, 201 // aom/aomcx.h, aom/aomdx.h, aom/src/aom_encoder.c, aom/src/aom_decoder.c) 202 // 203 // The actual interface between user code and the codec. It stores the name 204 // of the codec, a pointer back to the aom_codec_iface_t that initialized it, 205 // initialization flags, a config for either encoder or the decoder, and a 206 // pointer to internal data. 207 // 208 // The codec is configured / queried through calls to aom_codec_control, 209 // which takes a control code (listed in aomcx.h and aomdx.h) and a parameter. 210 // In the case of "getter" control codes, the parameter is modified to have 211 // the requested value; in the case of "setter" control codes, the codec's 212 // configuration is changed based on the parameter. Note that a aom_codec_err_t 213 // is returned, which indicates if the operation was successful or not. 214 // 215 // Note that for the encoder, the aom_codec_alg_priv_t points to the 216 // the aom_codec_alg_priv structure in av1/av1_cx_iface.c, and for the decoder, 217 // the struct in av1/av1_dx_iface.c. Variables such as AV1_COMP cpi are stored 218 // here and also used in the core algorithm. 219 // 220 // At the end, aom_codec_destroy should be called for each initialized 221 // aom_codec_ctx_t. 222 223 typedef struct AvxInterface { 224 const char *const name; 225 const uint32_t fourcc; 226 // Pointer to a function of zero arguments that returns an aom_codec_iface_t 227 // pointer. E.g.: 228 // aom_codec_iface_t *codec = interface->codec_interface(); 229 aom_codec_iface_t *(*const codec_interface)(); 230 } AvxInterface; 231 232 int get_aom_encoder_count(void); 233 // Lookup the interface by index -- it must be the case that 234 // i < get_aom_encoder_count() 235 const AvxInterface *get_aom_encoder_by_index(int i); 236 // Lookup the interface by name -- returns NULL if no match. 237 const AvxInterface *get_aom_encoder_by_name(const char *name); 238 const AvxInterface *get_aom_lst_encoder(void); 239 240 int get_aom_decoder_count(void); 241 const AvxInterface *get_aom_decoder_by_index(int i); 242 const AvxInterface *get_aom_decoder_by_name(const char *name); 243 // Lookup the interface by the fourcc -- returns NULL if no match. 244 const AvxInterface *get_aom_decoder_by_fourcc(uint32_t fourcc); 245 246 void aom_img_write(const aom_image_t *img, FILE *file); 247 int aom_img_read(aom_image_t *img, FILE *file); 248 249 double sse_to_psnr(double samples, double peak, double mse); 250 void aom_img_upshift(aom_image_t *dst, const aom_image_t *src, int input_shift); 251 void aom_img_downshift(aom_image_t *dst, const aom_image_t *src, 252 int down_shift); 253 void aom_shift_img(unsigned int output_bit_depth, aom_image_t **img_ptr, 254 aom_image_t **img_shifted_ptr); 255 void aom_img_truncate_16_to_8(aom_image_t *dst, const aom_image_t *src); 256 257 // Output in NV12 format. 258 void aom_img_write_nv12(const aom_image_t *img, FILE *file); 259 260 #ifdef __cplusplus 261 } /* extern "C" */ 262 #endif 263 264 #endif // AOM_COMMON_TOOLS_COMMON_H_ 265