tools_common.h 6.3 KB

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