xref: /aosp_15_r20/external/libultrahdr/lib/include/ultrahdr/ultrahdr.h (revision 89a0ef05262152531a00a15832a2d3b1e3990773)
1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ULTRAHDR_ULTRAHDR_H
18 #define ULTRAHDR_ULTRAHDR_H
19 
20 #include <string>
21 
22 namespace ultrahdr {
23 
24 #define JPEGR_CHECK(x)                \
25   {                                   \
26     status_t status = (x);            \
27     if ((status) != JPEGR_NO_ERROR) { \
28       return status;                  \
29     }                                 \
30   }
31 
32 // TODO (dichenzhang): rename these to "ULTRAHDR".
33 typedef enum {
34   JPEGR_NO_ERROR = 0,
35   JPEGR_UNKNOWN_ERROR = -1,
36 
37   JPEGR_IO_ERROR_BASE = -10000,
38   ERROR_JPEGR_BAD_PTR = JPEGR_IO_ERROR_BASE - 1,
39   ERROR_JPEGR_UNSUPPORTED_WIDTH_HEIGHT = JPEGR_IO_ERROR_BASE - 2,
40   ERROR_JPEGR_INVALID_COLORGAMUT = JPEGR_IO_ERROR_BASE - 3,
41   ERROR_JPEGR_INVALID_STRIDE = JPEGR_IO_ERROR_BASE - 4,
42   ERROR_JPEGR_INVALID_TRANS_FUNC = JPEGR_IO_ERROR_BASE - 5,
43   ERROR_JPEGR_RESOLUTION_MISMATCH = JPEGR_IO_ERROR_BASE - 6,
44   ERROR_JPEGR_INVALID_QUALITY_FACTOR = JPEGR_IO_ERROR_BASE - 7,
45   ERROR_JPEGR_INVALID_DISPLAY_BOOST = JPEGR_IO_ERROR_BASE - 8,
46   ERROR_JPEGR_INVALID_OUTPUT_FORMAT = JPEGR_IO_ERROR_BASE - 9,
47   ERROR_JPEGR_BAD_METADATA = JPEGR_IO_ERROR_BASE - 10,
48   ERROR_JPEGR_INVALID_CROPPING_PARAMETERS = JPEGR_IO_ERROR_BASE - 11,
49   ERROR_JPEGR_INVALID_GAMMA = JPEGR_IO_ERROR_BASE - 12,
50   ERROR_JPEGR_INVALID_ENC_PRESET = JPEGR_IO_ERROR_BASE - 13,
51   ERROR_JPEGR_INVALID_TARGET_DISP_PEAK_BRIGHTNESS = JPEGR_IO_ERROR_BASE - 14,
52 
53   JPEGR_RUNTIME_ERROR_BASE = -20000,
54   ERROR_JPEGR_ENCODE_ERROR = JPEGR_RUNTIME_ERROR_BASE - 1,
55   ERROR_JPEGR_DECODE_ERROR = JPEGR_RUNTIME_ERROR_BASE - 2,
56   ERROR_JPEGR_GAIN_MAP_IMAGE_NOT_FOUND = JPEGR_RUNTIME_ERROR_BASE - 3,
57   ERROR_JPEGR_BUFFER_TOO_SMALL = JPEGR_RUNTIME_ERROR_BASE - 4,
58   ERROR_JPEGR_METADATA_ERROR = JPEGR_RUNTIME_ERROR_BASE - 5,
59   ERROR_JPEGR_NO_IMAGES_FOUND = JPEGR_RUNTIME_ERROR_BASE - 6,
60   ERROR_JPEGR_MULTIPLE_EXIFS_RECEIVED = JPEGR_RUNTIME_ERROR_BASE - 7,
61   ERROR_JPEGR_UNSUPPORTED_MAP_SCALE_FACTOR = JPEGR_RUNTIME_ERROR_BASE - 8,
62   ERROR_JPEGR_GAIN_MAP_SIZE_ERROR = JPEGR_RUNTIME_ERROR_BASE - 9,
63 
64   ERROR_JPEGR_UNSUPPORTED_FEATURE = -30000,
65 } status_t;
66 
67 // Color gamuts for image data
68 typedef enum {
69   ULTRAHDR_COLORGAMUT_UNSPECIFIED = -1,
70   ULTRAHDR_COLORGAMUT_BT709,
71   ULTRAHDR_COLORGAMUT_P3,
72   ULTRAHDR_COLORGAMUT_BT2100,
73   ULTRAHDR_COLORGAMUT_MAX = ULTRAHDR_COLORGAMUT_BT2100,
74 } ultrahdr_color_gamut;
75 
76 // Transfer functions for image data
77 // TODO: TF LINEAR is deprecated, remove this enum and the code surrounding it.
78 typedef enum {
79   ULTRAHDR_TF_UNSPECIFIED = -1,
80   ULTRAHDR_TF_LINEAR = 0,
81   ULTRAHDR_TF_HLG = 1,
82   ULTRAHDR_TF_PQ = 2,
83   ULTRAHDR_TF_SRGB = 3,
84   ULTRAHDR_TF_MAX = ULTRAHDR_TF_SRGB,
85 } ultrahdr_transfer_function;
86 
87 // Target output formats for decoder
88 typedef enum {
89   ULTRAHDR_OUTPUT_UNSPECIFIED = -1,
90   ULTRAHDR_OUTPUT_SDR,         // SDR in RGBA_8888 color format
91   ULTRAHDR_OUTPUT_HDR_LINEAR,  // HDR in F16 color format (linear)
92   ULTRAHDR_OUTPUT_HDR_PQ,      // HDR in RGBA_1010102 color format (PQ transfer function)
93   ULTRAHDR_OUTPUT_HDR_HLG,     // HDR in RGBA_1010102 color format (HLG transfer function)
94   ULTRAHDR_OUTPUT_MAX = ULTRAHDR_OUTPUT_HDR_HLG,
95 } ultrahdr_output_format;
96 
97 /*
98  * Holds information for gain map related metadata.
99  *
100  * Not: all values stored in linear. This differs from the metadata encoding in XMP, where
101  * maxContentBoost (aka gainMapMax), minContentBoost (aka gainMapMin), hdrCapacityMin, and
102  * hdrCapacityMax are stored in log2 space.
103  */
104 struct ultrahdr_metadata_struct {
105   // Ultra HDR format version
106   std::string version;
107   // Max Content Boost for the map
108   float maxContentBoost;
109   // Min Content Boost for the map
110   float minContentBoost;
111   // Gamma of the map data
112   float gamma;
113   // Offset for SDR data in map calculations
114   float offsetSdr;
115   // Offset for HDR data in map calculations
116   float offsetHdr;
117   // HDR capacity to apply the map at all
118   float hdrCapacityMin;
119   // HDR capacity to apply the map completely
120   float hdrCapacityMax;
121 };
122 
123 /*
124  * Holds information for uncompressed image or gain map.
125  */
126 struct jpegr_uncompressed_struct {
127   // Pointer to the data location.
128   void* data;
129   // Width of the gain map or the luma plane of the image in pixels.
130   unsigned int width;
131   // Height of the gain map or the luma plane of the image in pixels.
132   unsigned int height;
133   // Color gamut.
134   ultrahdr_color_gamut colorGamut;
135 
136   // Values below are optional
137   // Pointer to chroma data, if it's NULL, chroma plane is considered to be immediately
138   // after the luma plane.
139   void* chroma_data = nullptr;
140   // Stride of Y plane in number of pixels. 0 indicates the member is uninitialized. If
141   // non-zero this value must be larger than or equal to luma width. If stride is
142   // uninitialized then it is assumed to be equal to luma width.
143   unsigned int luma_stride = 0;
144   // Stride of UV plane in number of pixels.
145   // 1. If this handle points to P010 image then this value must be larger than
146   //    or equal to luma width.
147   // 2. If this handle points to 420 image then this value must be larger than
148   //    or equal to (luma width / 2).
149   // NOTE: if chroma_data is nullptr, chroma_stride is irrelevant. Just as the way,
150   // chroma_data is derived from luma ptr, chroma stride is derived from luma stride.
151   unsigned int chroma_stride = 0;
152   // Pixel format.
153   uhdr_img_fmt_t pixelFormat = UHDR_IMG_FMT_UNSPECIFIED;
154   // Color range.
155   uhdr_color_range_t colorRange = UHDR_CR_UNSPECIFIED;
156 };
157 
158 /*
159  * Holds information for compressed image or gain map.
160  */
161 struct jpegr_compressed_struct {
162   // Pointer to the data location.
163   void* data;
164   // Used data length in bytes.
165   size_t length;
166   // Maximum available data length in bytes.
167   size_t maxLength;
168   // Color gamut.
169   ultrahdr_color_gamut colorGamut;
170 };
171 
172 /*
173  * Holds information for EXIF metadata.
174  */
175 struct jpegr_exif_struct {
176   // Pointer to the data location.
177   void* data;
178   // Data length;
179   size_t length;
180 };
181 
182 typedef struct jpegr_uncompressed_struct* jr_uncompressed_ptr;
183 typedef struct jpegr_compressed_struct* jr_compressed_ptr;
184 typedef struct jpegr_exif_struct* jr_exif_ptr;
185 typedef struct ultrahdr_metadata_struct* ultrahdr_metadata_ptr;
186 
187 }  // namespace ultrahdr
188 
189 #endif  // ULTRAHDR_ULTRAHDR_H
190