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