aom_image.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. /*!\file
  12. * \brief Describes the aom image descriptor and associated operations
  13. *
  14. */
  15. #ifndef AOM_AOM_AOM_IMAGE_H_
  16. #define AOM_AOM_AOM_IMAGE_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include "aom/aom_integer.h"
  21. /*!\brief Current ABI version number
  22. *
  23. * \internal
  24. * If this file is altered in any way that changes the ABI, this value
  25. * must be bumped. Examples include, but are not limited to, changing
  26. * types, removing or reassigning enums, adding/removing/rearranging
  27. * fields to structures
  28. */
  29. #define AOM_IMAGE_ABI_VERSION (9) /**<\hideinitializer*/
  30. #define AOM_IMG_FMT_PLANAR 0x100 /**< Image is a planar format. */
  31. #define AOM_IMG_FMT_UV_FLIP 0x200 /**< V plane precedes U in memory. */
  32. /** 0x400 used to signal alpha channel, skipping for backwards compatibility. */
  33. #define AOM_IMG_FMT_HIGHBITDEPTH 0x800 /**< Image uses 16bit framebuffer. */
  34. /*!\brief List of supported image formats */
  35. typedef enum aom_img_fmt {
  36. AOM_IMG_FMT_NONE,
  37. AOM_IMG_FMT_YV12 =
  38. AOM_IMG_FMT_PLANAR | AOM_IMG_FMT_UV_FLIP | 1, /**< planar YVU */
  39. AOM_IMG_FMT_I420 = AOM_IMG_FMT_PLANAR | 2,
  40. AOM_IMG_FMT_AOMYV12 = AOM_IMG_FMT_PLANAR | AOM_IMG_FMT_UV_FLIP |
  41. 3, /** < planar 4:2:0 format with aom color space */
  42. AOM_IMG_FMT_AOMI420 = AOM_IMG_FMT_PLANAR | 4,
  43. AOM_IMG_FMT_I422 = AOM_IMG_FMT_PLANAR | 5,
  44. AOM_IMG_FMT_I444 = AOM_IMG_FMT_PLANAR | 6,
  45. /*!\brief Allows detection of the presence of AOM_IMG_FMT_NV12 at compile time.
  46. */
  47. #define AOM_HAVE_IMG_FMT_NV12 1
  48. AOM_IMG_FMT_NV12 =
  49. AOM_IMG_FMT_PLANAR | 7, /**< 4:2:0 with U and V interleaved */
  50. AOM_IMG_FMT_I42016 = AOM_IMG_FMT_I420 | AOM_IMG_FMT_HIGHBITDEPTH,
  51. AOM_IMG_FMT_YV1216 = AOM_IMG_FMT_YV12 | AOM_IMG_FMT_HIGHBITDEPTH,
  52. AOM_IMG_FMT_I42216 = AOM_IMG_FMT_I422 | AOM_IMG_FMT_HIGHBITDEPTH,
  53. AOM_IMG_FMT_I44416 = AOM_IMG_FMT_I444 | AOM_IMG_FMT_HIGHBITDEPTH,
  54. } aom_img_fmt_t; /**< alias for enum aom_img_fmt */
  55. /*!\brief List of supported color primaries */
  56. typedef enum aom_color_primaries {
  57. AOM_CICP_CP_RESERVED_0 = 0, /**< For future use */
  58. AOM_CICP_CP_BT_709 = 1, /**< BT.709 */
  59. AOM_CICP_CP_UNSPECIFIED = 2, /**< Unspecified */
  60. AOM_CICP_CP_RESERVED_3 = 3, /**< For future use */
  61. AOM_CICP_CP_BT_470_M = 4, /**< BT.470 System M (historical) */
  62. AOM_CICP_CP_BT_470_B_G = 5, /**< BT.470 System B, G (historical) */
  63. AOM_CICP_CP_BT_601 = 6, /**< BT.601 */
  64. AOM_CICP_CP_SMPTE_240 = 7, /**< SMPTE 240 */
  65. AOM_CICP_CP_GENERIC_FILM =
  66. 8, /**< Generic film (color filters using illuminant C) */
  67. AOM_CICP_CP_BT_2020 = 9, /**< BT.2020, BT.2100 */
  68. AOM_CICP_CP_XYZ = 10, /**< SMPTE 428 (CIE 1921 XYZ) */
  69. AOM_CICP_CP_SMPTE_431 = 11, /**< SMPTE RP 431-2 */
  70. AOM_CICP_CP_SMPTE_432 = 12, /**< SMPTE EG 432-1 */
  71. AOM_CICP_CP_RESERVED_13 = 13, /**< For future use (values 13 - 21) */
  72. AOM_CICP_CP_EBU_3213 = 22, /**< EBU Tech. 3213-E */
  73. AOM_CICP_CP_RESERVED_23 = 23 /**< For future use (values 23 - 255) */
  74. } aom_color_primaries_t; /**< alias for enum aom_color_primaries */
  75. /*!\brief List of supported transfer functions */
  76. typedef enum aom_transfer_characteristics {
  77. AOM_CICP_TC_RESERVED_0 = 0, /**< For future use */
  78. AOM_CICP_TC_BT_709 = 1, /**< BT.709 */
  79. AOM_CICP_TC_UNSPECIFIED = 2, /**< Unspecified */
  80. AOM_CICP_TC_RESERVED_3 = 3, /**< For future use */
  81. AOM_CICP_TC_BT_470_M = 4, /**< BT.470 System M (historical) */
  82. AOM_CICP_TC_BT_470_B_G = 5, /**< BT.470 System B, G (historical) */
  83. AOM_CICP_TC_BT_601 = 6, /**< BT.601 */
  84. AOM_CICP_TC_SMPTE_240 = 7, /**< SMPTE 240 M */
  85. AOM_CICP_TC_LINEAR = 8, /**< Linear */
  86. AOM_CICP_TC_LOG_100 = 9, /**< Logarithmic (100 : 1 range) */
  87. AOM_CICP_TC_LOG_100_SQRT10 =
  88. 10, /**< Logarithmic (100 * Sqrt(10) : 1 range) */
  89. AOM_CICP_TC_IEC_61966 = 11, /**< IEC 61966-2-4 */
  90. AOM_CICP_TC_BT_1361 = 12, /**< BT.1361 */
  91. AOM_CICP_TC_SRGB = 13, /**< sRGB or sYCC*/
  92. AOM_CICP_TC_BT_2020_10_BIT = 14, /**< BT.2020 10-bit systems */
  93. AOM_CICP_TC_BT_2020_12_BIT = 15, /**< BT.2020 12-bit systems */
  94. AOM_CICP_TC_SMPTE_2084 = 16, /**< SMPTE ST 2084, ITU BT.2100 PQ */
  95. AOM_CICP_TC_SMPTE_428 = 17, /**< SMPTE ST 428 */
  96. AOM_CICP_TC_HLG = 18, /**< BT.2100 HLG, ARIB STD-B67 */
  97. AOM_CICP_TC_RESERVED_19 = 19 /**< For future use (values 19-255) */
  98. } aom_transfer_characteristics_t; /**< alias for enum
  99. aom_transfer_characteristics */
  100. /*!\brief List of supported matrix coefficients */
  101. typedef enum aom_matrix_coefficients {
  102. AOM_CICP_MC_IDENTITY = 0, /**< Identity matrix */
  103. AOM_CICP_MC_BT_709 = 1, /**< BT.709 */
  104. AOM_CICP_MC_UNSPECIFIED = 2, /**< Unspecified */
  105. AOM_CICP_MC_RESERVED_3 = 3, /**< For future use */
  106. AOM_CICP_MC_FCC = 4, /**< US FCC 73.628 */
  107. AOM_CICP_MC_BT_470_B_G = 5, /**< BT.470 System B, G (historical) */
  108. AOM_CICP_MC_BT_601 = 6, /**< BT.601 */
  109. AOM_CICP_MC_SMPTE_240 = 7, /**< SMPTE 240 M */
  110. AOM_CICP_MC_SMPTE_YCGCO = 8, /**< YCgCo */
  111. AOM_CICP_MC_BT_2020_NCL =
  112. 9, /**< BT.2020 non-constant luminance, BT.2100 YCbCr */
  113. AOM_CICP_MC_BT_2020_CL = 10, /**< BT.2020 constant luminance */
  114. AOM_CICP_MC_SMPTE_2085 = 11, /**< SMPTE ST 2085 YDzDx */
  115. AOM_CICP_MC_CHROMAT_NCL =
  116. 12, /**< Chromaticity-derived non-constant luminance */
  117. AOM_CICP_MC_CHROMAT_CL = 13, /**< Chromaticity-derived constant luminance */
  118. AOM_CICP_MC_ICTCP = 14, /**< BT.2100 ICtCp */
  119. AOM_CICP_MC_RESERVED_15 = 15 /**< For future use (values 15-255) */
  120. } aom_matrix_coefficients_t; /**< alias for enum aom_matrix_coefficients */
  121. /*!\brief List of supported color range */
  122. typedef enum aom_color_range {
  123. AOM_CR_STUDIO_RANGE = 0, /**<- Y [16..235], UV [16..240] (bit depth 8) */
  124. /**<- Y [64..940], UV [64..960] (bit depth 10) */
  125. /**<- Y [256..3760], UV [256..3840] (bit depth 12) */
  126. AOM_CR_FULL_RANGE = 1 /**<- YUV/RGB [0..255] (bit depth 8) */
  127. /**<- YUV/RGB [0..1023] (bit depth 10) */
  128. /**<- YUV/RGB [0..4095] (bit depth 12) */
  129. } aom_color_range_t; /**< alias for enum aom_color_range */
  130. /*!\brief List of chroma sample positions */
  131. typedef enum aom_chroma_sample_position {
  132. AOM_CSP_UNKNOWN = 0, /**< Unknown */
  133. AOM_CSP_VERTICAL = 1, /**< Horizontally co-located with luma(0, 0)*/
  134. /**< sample, between two vertical samples */
  135. AOM_CSP_COLOCATED = 2, /**< Co-located with luma(0, 0) sample */
  136. AOM_CSP_RESERVED = 3 /**< Reserved value */
  137. } aom_chroma_sample_position_t; /**< alias for enum aom_chroma_sample_position
  138. */
  139. /*!\brief List of insert flags for Metadata
  140. *
  141. * These flags control how the library treats metadata during encode.
  142. *
  143. * While encoding, when metadata is added to an aom_image via
  144. * aom_img_add_metadata(), the flag passed along with the metadata will
  145. * determine where the metadata OBU will be placed in the encoded OBU stream.
  146. * Metadata will be emitted into the output stream within the next temporal unit
  147. * if it satisfies the specified insertion flag.
  148. *
  149. * During decoding, when the library encounters a metadata OBU, it is always
  150. * flagged as AOM_MIF_ANY_FRAME and emitted with the next output aom_image.
  151. */
  152. typedef enum aom_metadata_insert_flags {
  153. AOM_MIF_NON_KEY_FRAME = 0, /**< Adds metadata if it's not keyframe */
  154. AOM_MIF_KEY_FRAME = 1, /**< Adds metadata only if it's a keyframe */
  155. AOM_MIF_ANY_FRAME = 2 /**< Adds metadata to any type of frame */
  156. } aom_metadata_insert_flags_t;
  157. /*!\brief Array of aom_metadata structs for an image. */
  158. typedef struct aom_metadata_array aom_metadata_array_t;
  159. /*!\brief Metadata payload. */
  160. typedef struct aom_metadata {
  161. uint32_t type; /**< Metadata type */
  162. uint8_t *payload; /**< Metadata payload data */
  163. size_t sz; /**< Metadata payload size */
  164. aom_metadata_insert_flags_t insert_flag; /**< Metadata insertion flag */
  165. } aom_metadata_t;
  166. /**\brief Image Descriptor */
  167. typedef struct aom_image {
  168. aom_img_fmt_t fmt; /**< Image Format */
  169. aom_color_primaries_t cp; /**< CICP Color Primaries */
  170. aom_transfer_characteristics_t tc; /**< CICP Transfer Characteristics */
  171. aom_matrix_coefficients_t mc; /**< CICP Matrix Coefficients */
  172. int monochrome; /**< Whether image is monochrome */
  173. aom_chroma_sample_position_t csp; /**< chroma sample position */
  174. aom_color_range_t range; /**< Color Range */
  175. /* Image storage dimensions */
  176. unsigned int w; /**< Stored image width */
  177. unsigned int h; /**< Stored image height */
  178. unsigned int bit_depth; /**< Stored image bit-depth */
  179. /* Image display dimensions */
  180. unsigned int d_w; /**< Displayed image width */
  181. unsigned int d_h; /**< Displayed image height */
  182. /* Image intended rendering dimensions */
  183. unsigned int r_w; /**< Intended rendering image width */
  184. unsigned int r_h; /**< Intended rendering image height */
  185. /* Chroma subsampling info */
  186. unsigned int x_chroma_shift; /**< subsampling order, X */
  187. unsigned int y_chroma_shift; /**< subsampling order, Y */
  188. /* Image data pointers. */
  189. #define AOM_PLANE_PACKED 0 /**< To be used for all packed formats */
  190. #define AOM_PLANE_Y 0 /**< Y (Luminance) plane */
  191. #define AOM_PLANE_U 1 /**< U (Chroma) plane */
  192. #define AOM_PLANE_V 2 /**< V (Chroma) plane */
  193. /* planes[AOM_PLANE_V] = NULL and stride[AOM_PLANE_V] = 0 when fmt ==
  194. * AOM_IMG_FMT_NV12 */
  195. unsigned char *planes[3]; /**< pointer to the top left pixel for each plane */
  196. int stride[3]; /**< stride between rows for each plane */
  197. size_t sz; /**< data size */
  198. int bps; /**< bits per sample (for packed formats) */
  199. int temporal_id; /**< Temporal layer Id of image */
  200. int spatial_id; /**< Spatial layer Id of image */
  201. /*!\brief The following member may be set by the application to associate
  202. * data with this image.
  203. */
  204. void *user_priv;
  205. /* The following members should be treated as private. */
  206. unsigned char *img_data; /**< private */
  207. int img_data_owner; /**< private */
  208. int self_allocd; /**< private */
  209. aom_metadata_array_t
  210. *metadata; /**< Metadata payloads associated with the image. */
  211. void *fb_priv; /**< Frame buffer data associated with the image. */
  212. } aom_image_t; /**< alias for struct aom_image */
  213. /*!\brief Open a descriptor, allocating storage for the underlying image
  214. *
  215. * Returns a descriptor for storing an image of the given format. The
  216. * storage for the image is allocated on the heap.
  217. *
  218. * \param[in] img Pointer to storage for descriptor. If this parameter
  219. * is NULL, the storage for the descriptor will be
  220. * allocated on the heap.
  221. * \param[in] fmt Format for the image
  222. * \param[in] d_w Width of the image. Must not exceed 0x08000000
  223. * (2^27).
  224. * \param[in] d_h Height of the image. Must not exceed 0x08000000
  225. * (2^27).
  226. * \param[in] align Alignment, in bytes, of the image buffer and
  227. * each row in the image (stride). Must not exceed
  228. * 65536.
  229. *
  230. * \return Returns a pointer to the initialized image descriptor. If the img
  231. * parameter is non-null, the value of the img parameter will be
  232. * returned.
  233. */
  234. aom_image_t *aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt,
  235. unsigned int d_w, unsigned int d_h,
  236. unsigned int align);
  237. /*!\brief Open a descriptor, using existing storage for the underlying image
  238. *
  239. * Returns a descriptor for storing an image of the given format. The
  240. * storage for the image has been allocated elsewhere, and a descriptor is
  241. * desired to "wrap" that storage.
  242. *
  243. * \param[in] img Pointer to storage for descriptor. If this parameter
  244. * is NULL, the storage for the descriptor will be
  245. * allocated on the heap.
  246. * \param[in] fmt Format for the image
  247. * \param[in] d_w Width of the image. Must not exceed 0x08000000
  248. * (2^27).
  249. * \param[in] d_h Height of the image. Must not exceed 0x08000000
  250. * (2^27).
  251. * \param[in] align Alignment, in bytes, of each row in the image
  252. * (stride). Must not exceed 65536.
  253. * \param[in] img_data Storage to use for the image
  254. *
  255. * \return Returns a pointer to the initialized image descriptor. If the img
  256. * parameter is non-null, the value of the img parameter will be
  257. * returned.
  258. */
  259. aom_image_t *aom_img_wrap(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w,
  260. unsigned int d_h, unsigned int align,
  261. unsigned char *img_data);
  262. /*!\brief Open a descriptor, allocating storage for the underlying image with a
  263. * border
  264. *
  265. * Returns a descriptor for storing an image of the given format and its
  266. * borders. The storage for the image is allocated on the heap.
  267. *
  268. * \param[in] img Pointer to storage for descriptor. If this parameter
  269. * is NULL, the storage for the descriptor will be
  270. * allocated on the heap.
  271. * \param[in] fmt Format for the image
  272. * \param[in] d_w Width of the image. Must not exceed 0x08000000
  273. * (2^27).
  274. * \param[in] d_h Height of the image. Must not exceed 0x08000000
  275. * (2^27).
  276. * \param[in] align Alignment, in bytes, of the image buffer and
  277. * each row in the image (stride). Must not exceed
  278. * 65536.
  279. * \param[in] size_align Alignment, in pixels, of the image width and height.
  280. * Must not exceed 65536.
  281. * \param[in] border A border that is padded on four sides of the image.
  282. * Must not exceed 65536.
  283. *
  284. * \return Returns a pointer to the initialized image descriptor. If the img
  285. * parameter is non-null, the value of the img parameter will be
  286. * returned.
  287. */
  288. aom_image_t *aom_img_alloc_with_border(aom_image_t *img, aom_img_fmt_t fmt,
  289. unsigned int d_w, unsigned int d_h,
  290. unsigned int align,
  291. unsigned int size_align,
  292. unsigned int border);
  293. /*!\brief Set the rectangle identifying the displayed portion of the image
  294. *
  295. * Updates the displayed rectangle (aka viewport) on the image surface to
  296. * match the specified coordinates and size. Specifically, sets img->d_w,
  297. * img->d_h, and elements of the img->planes[] array.
  298. *
  299. * \param[in] img Image descriptor
  300. * \param[in] x leftmost column
  301. * \param[in] y topmost row
  302. * \param[in] w width
  303. * \param[in] h height
  304. * \param[in] border A border that is padded on four sides of the image.
  305. *
  306. * \return 0 if the requested rectangle is valid, nonzero (-1) otherwise.
  307. */
  308. int aom_img_set_rect(aom_image_t *img, unsigned int x, unsigned int y,
  309. unsigned int w, unsigned int h, unsigned int border);
  310. /*!\brief Flip the image vertically (top for bottom)
  311. *
  312. * Adjusts the image descriptor's pointers and strides to make the image
  313. * be referenced upside-down.
  314. *
  315. * \param[in] img Image descriptor
  316. */
  317. void aom_img_flip(aom_image_t *img);
  318. /*!\brief Close an image descriptor
  319. *
  320. * Frees all allocated storage associated with an image descriptor.
  321. *
  322. * \param[in] img Image descriptor
  323. */
  324. void aom_img_free(aom_image_t *img);
  325. /*!\brief Get the width of a plane
  326. *
  327. * Get the width of a plane of an image
  328. *
  329. * \param[in] img Image descriptor
  330. * \param[in] plane Plane index
  331. */
  332. int aom_img_plane_width(const aom_image_t *img, int plane);
  333. /*!\brief Get the height of a plane
  334. *
  335. * Get the height of a plane of an image
  336. *
  337. * \param[in] img Image descriptor
  338. * \param[in] plane Plane index
  339. */
  340. int aom_img_plane_height(const aom_image_t *img, int plane);
  341. /*!\brief Add metadata to image.
  342. *
  343. * Adds metadata to aom_image_t.
  344. * Function makes a copy of the provided data parameter.
  345. * Metadata insertion point is controlled by insert_flag.
  346. *
  347. * \param[in] img Image descriptor
  348. * \param[in] type Metadata type
  349. * \param[in] data Metadata contents
  350. * \param[in] sz Metadata contents size
  351. * \param[in] insert_flag Metadata insert flag
  352. *
  353. * \return Returns 0 on success. If img or data is NULL, sz is 0, or memory
  354. * allocation fails, it returns -1.
  355. */
  356. int aom_img_add_metadata(aom_image_t *img, uint32_t type, const uint8_t *data,
  357. size_t sz, aom_metadata_insert_flags_t insert_flag);
  358. /*!\brief Return a metadata payload stored within the image metadata array.
  359. *
  360. * Gets the metadata (aom_metadata_t) at the indicated index in the image
  361. * metadata array.
  362. *
  363. * \param[in] img Pointer to image descriptor to get metadata from
  364. * \param[in] index Metadata index to get from metadata array
  365. *
  366. * \return Returns a const pointer to the selected metadata, if img and/or index
  367. * is invalid, it returns NULL.
  368. */
  369. const aom_metadata_t *aom_img_get_metadata(const aom_image_t *img,
  370. size_t index);
  371. /*!\brief Return the number of metadata blocks within the image.
  372. *
  373. * Gets the number of metadata blocks contained within the provided image
  374. * metadata array.
  375. *
  376. * \param[in] img Pointer to image descriptor to get metadata number
  377. * from.
  378. *
  379. * \return Returns the size of the metadata array. If img or metadata is NULL,
  380. * it returns 0.
  381. */
  382. size_t aom_img_num_metadata(const aom_image_t *img);
  383. /*!\brief Remove metadata from image.
  384. *
  385. * Removes all metadata in image metadata list and sets metadata list pointer
  386. * to NULL.
  387. *
  388. * \param[in] img Image descriptor
  389. */
  390. void aom_img_remove_metadata(aom_image_t *img);
  391. /*!\brief Allocate memory for aom_metadata struct.
  392. *
  393. * Allocates storage for the metadata payload, sets its type and copies the
  394. * payload data into the aom_metadata struct. A metadata payload buffer of size
  395. * sz is allocated and sz bytes are copied from data into the payload buffer.
  396. *
  397. * \param[in] type Metadata type
  398. * \param[in] data Metadata data pointer
  399. * \param[in] sz Metadata size
  400. * \param[in] insert_flag Metadata insert flag
  401. *
  402. * \return Returns the newly allocated aom_metadata struct. If data is NULL,
  403. * sz is 0, or memory allocation fails, it returns NULL.
  404. */
  405. aom_metadata_t *aom_img_metadata_alloc(uint32_t type, const uint8_t *data,
  406. size_t sz,
  407. aom_metadata_insert_flags_t insert_flag);
  408. /*!\brief Free metadata struct.
  409. *
  410. * Free metadata struct and its buffer.
  411. *
  412. * \param[in] metadata Metadata struct pointer
  413. */
  414. void aom_img_metadata_free(aom_metadata_t *metadata);
  415. #ifdef __cplusplus
  416. } // extern "C"
  417. #endif
  418. #endif // AOM_AOM_AOM_IMAGE_H_