codestream_header.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* Copyright (c) the JPEG XL Project Authors. All rights reserved.
  2. *
  3. * Use of this source code is governed by a BSD-style
  4. * license that can be found in the LICENSE file.
  5. */
  6. /** @addtogroup libjxl_metadata
  7. * @{
  8. * @file codestream_header.h
  9. * @brief Definitions of structs and enums for the metadata from the JPEG XL
  10. * codestream headers (signature, metadata, preview dimensions, ...), excluding
  11. * color encoding which is in color_encoding.h.
  12. */
  13. #ifndef JXL_CODESTREAM_HEADER_H_
  14. #define JXL_CODESTREAM_HEADER_H_
  15. #include <jxl/types.h>
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /** Image orientation metadata.
  22. * Values 1..8 match the EXIF definitions.
  23. * The name indicates the operation to perform to transform from the encoded
  24. * image to the display image.
  25. */
  26. typedef enum {
  27. JXL_ORIENT_IDENTITY = 1,
  28. JXL_ORIENT_FLIP_HORIZONTAL = 2,
  29. JXL_ORIENT_ROTATE_180 = 3,
  30. JXL_ORIENT_FLIP_VERTICAL = 4,
  31. JXL_ORIENT_TRANSPOSE = 5,
  32. JXL_ORIENT_ROTATE_90_CW = 6,
  33. JXL_ORIENT_ANTI_TRANSPOSE = 7,
  34. JXL_ORIENT_ROTATE_90_CCW = 8,
  35. } JxlOrientation;
  36. /** Given type of an extra channel.
  37. */
  38. typedef enum {
  39. JXL_CHANNEL_ALPHA,
  40. JXL_CHANNEL_DEPTH,
  41. JXL_CHANNEL_SPOT_COLOR,
  42. JXL_CHANNEL_SELECTION_MASK,
  43. JXL_CHANNEL_BLACK,
  44. JXL_CHANNEL_CFA,
  45. JXL_CHANNEL_THERMAL,
  46. JXL_CHANNEL_RESERVED0,
  47. JXL_CHANNEL_RESERVED1,
  48. JXL_CHANNEL_RESERVED2,
  49. JXL_CHANNEL_RESERVED3,
  50. JXL_CHANNEL_RESERVED4,
  51. JXL_CHANNEL_RESERVED5,
  52. JXL_CHANNEL_RESERVED6,
  53. JXL_CHANNEL_RESERVED7,
  54. JXL_CHANNEL_UNKNOWN,
  55. JXL_CHANNEL_OPTIONAL
  56. } JxlExtraChannelType;
  57. /** The codestream preview header */
  58. typedef struct {
  59. /** Preview width in pixels */
  60. uint32_t xsize;
  61. /** Preview height in pixels */
  62. uint32_t ysize;
  63. } JxlPreviewHeader;
  64. /** The codestream animation header, optionally present in the beginning of
  65. * the codestream, and if it is it applies to all animation frames, unlike @ref
  66. * JxlFrameHeader which applies to an individual frame.
  67. */
  68. typedef struct {
  69. /** Numerator of ticks per second of a single animation frame time unit */
  70. uint32_t tps_numerator;
  71. /** Denominator of ticks per second of a single animation frame time unit */
  72. uint32_t tps_denominator;
  73. /** Amount of animation loops, or 0 to repeat infinitely */
  74. uint32_t num_loops;
  75. /** Whether animation time codes are present at animation frames in the
  76. * codestream */
  77. JXL_BOOL have_timecodes;
  78. } JxlAnimationHeader;
  79. /** Basic image information. This information is available from the file
  80. * signature and first part of the codestream header.
  81. */
  82. typedef struct {
  83. /* TODO(lode): need additional fields for (transcoded) JPEG? For reusable
  84. * fields orientation must be read from Exif APP1. For has_icc_profile: must
  85. * look up where ICC profile is guaranteed to be in a JPEG file to be able to
  86. * indicate this. */
  87. /* TODO(lode): make struct packed, and/or make this opaque struct with getter
  88. * functions (still separate struct from opaque decoder) */
  89. /** Whether the codestream is embedded in the container format. If true,
  90. * metadata information and extensions may be available in addition to the
  91. * codestream.
  92. */
  93. JXL_BOOL have_container;
  94. /** Width of the image in pixels, before applying orientation.
  95. */
  96. uint32_t xsize;
  97. /** Height of the image in pixels, before applying orientation.
  98. */
  99. uint32_t ysize;
  100. /** Original image color channel bit depth.
  101. */
  102. uint32_t bits_per_sample;
  103. /** Original image color channel floating point exponent bits, or 0 if they
  104. * are unsigned integer. For example, if the original data is half-precision
  105. * (binary16) floating point, bits_per_sample is 16 and
  106. * exponent_bits_per_sample is 5, and so on for other floating point
  107. * precisions.
  108. */
  109. uint32_t exponent_bits_per_sample;
  110. /** Upper bound on the intensity level present in the image in nits. For
  111. * unsigned integer pixel encodings, this is the brightness of the largest
  112. * representable value. The image does not necessarily contain a pixel
  113. * actually this bright. An encoder is allowed to set 255 for SDR images
  114. * without computing a histogram.
  115. * Leaving this set to its default of 0 lets libjxl choose a sensible default
  116. * value based on the color encoding.
  117. */
  118. float intensity_target;
  119. /** Lower bound on the intensity level present in the image. This may be
  120. * loose, i.e. lower than the actual darkest pixel. When tone mapping, a
  121. * decoder will map [min_nits, intensity_target] to the display range.
  122. */
  123. float min_nits;
  124. /** See the description of @see linear_below.
  125. */
  126. JXL_BOOL relative_to_max_display;
  127. /** The tone mapping will leave unchanged (linear mapping) any pixels whose
  128. * brightness is strictly below this. The interpretation depends on
  129. * relative_to_max_display. If true, this is a ratio [0, 1] of the maximum
  130. * display brightness [nits], otherwise an absolute brightness [nits].
  131. */
  132. float linear_below;
  133. /** Whether the data in the codestream is encoded in the original color
  134. * profile that is attached to the codestream metadata header, or is
  135. * encoded in an internally supported absolute color space (which the decoder
  136. * can always convert to linear or non-linear sRGB or to XYB). If the original
  137. * profile is used, the decoder outputs pixel data in the color space matching
  138. * that profile, but doesn't convert it to any other color space. If the
  139. * original profile is not used, the decoder only outputs the data as sRGB
  140. * (linear if outputting to floating point, nonlinear with standard sRGB
  141. * transfer function if outputting to unsigned integers) but will not convert
  142. * it to to the original color profile. The decoder also does not convert to
  143. * the target display color profile. To convert the pixel data produced by
  144. * the decoder to the original color profile, one of the JxlDecoderGetColor*
  145. * functions needs to be called with
  146. * ::JXL_COLOR_PROFILE_TARGET_DATA to get the color profile of the decoder
  147. * output, and then an external CMS can be used for conversion. Note that for
  148. * lossy compression, this should be set to false for most use cases, and if
  149. * needed, the image should be converted to the original color profile after
  150. * decoding, as described above.
  151. */
  152. JXL_BOOL uses_original_profile;
  153. /** Indicates a preview image exists near the beginning of the codestream.
  154. * The preview itself or its dimensions are not included in the basic info.
  155. */
  156. JXL_BOOL have_preview;
  157. /** Indicates animation frames exist in the codestream. The animation
  158. * information is not included in the basic info.
  159. */
  160. JXL_BOOL have_animation;
  161. /** Image orientation, value 1-8 matching the values used by JEITA CP-3451C
  162. * (Exif version 2.3).
  163. */
  164. JxlOrientation orientation;
  165. /** Number of color channels encoded in the image, this is either 1 for
  166. * grayscale data, or 3 for colored data. This count does not include
  167. * the alpha channel or other extra channels. To check presence of an alpha
  168. * channel, such as in the case of RGBA color, check alpha_bits != 0.
  169. * If and only if this is 1, the @ref JxlColorSpace in the @ref
  170. * JxlColorEncoding is
  171. * ::JXL_COLOR_SPACE_GRAY.
  172. */
  173. uint32_t num_color_channels;
  174. /** Number of additional image channels. This includes the main alpha channel,
  175. * but can also include additional channels such as depth, additional alpha
  176. * channels, spot colors, and so on. Information about the extra channels
  177. * can be queried with @ref JxlDecoderGetExtraChannelInfo. The main alpha
  178. * channel, if it exists, also has its information available in the
  179. * alpha_bits, alpha_exponent_bits and alpha_premultiplied fields in this @ref
  180. * JxlBasicInfo.
  181. */
  182. uint32_t num_extra_channels;
  183. /** Bit depth of the encoded alpha channel, or 0 if there is no alpha channel.
  184. * If present, matches the alpha_bits value of the JxlExtraChannelInfo
  185. * associated with this alpha channel.
  186. */
  187. uint32_t alpha_bits;
  188. /** Alpha channel floating point exponent bits, or 0 if they are unsigned. If
  189. * present, matches the alpha_bits value of the JxlExtraChannelInfo associated
  190. * with this alpha channel. integer.
  191. */
  192. uint32_t alpha_exponent_bits;
  193. /** Whether the alpha channel is premultiplied. Only used if there is a main
  194. * alpha channel. Matches the alpha_premultiplied value of the
  195. * JxlExtraChannelInfo associated with this alpha channel.
  196. */
  197. JXL_BOOL alpha_premultiplied;
  198. /** Dimensions of encoded preview image, only used if have_preview is
  199. * JXL_TRUE.
  200. */
  201. JxlPreviewHeader preview;
  202. /** Animation header with global animation properties for all frames, only
  203. * used if have_animation is JXL_TRUE.
  204. */
  205. JxlAnimationHeader animation;
  206. /** Intrinsic width of the image.
  207. * The intrinsic size can be different from the actual size in pixels
  208. * (as given by xsize and ysize) and it denotes the recommended dimensions
  209. * for displaying the image, i.e. applications are advised to resample the
  210. * decoded image to the intrinsic dimensions.
  211. */
  212. uint32_t intrinsic_xsize;
  213. /** Intrinsic height of the image.
  214. * The intrinsic size can be different from the actual size in pixels
  215. * (as given by xsize and ysize) and it denotes the recommended dimensions
  216. * for displaying the image, i.e. applications are advised to resample the
  217. * decoded image to the intrinsic dimensions.
  218. */
  219. uint32_t intrinsic_ysize;
  220. /** Padding for forwards-compatibility, in case more fields are exposed
  221. * in a future version of the library.
  222. */
  223. uint8_t padding[100];
  224. } JxlBasicInfo;
  225. /** Information for a single extra channel.
  226. */
  227. typedef struct {
  228. /** Given type of an extra channel.
  229. */
  230. JxlExtraChannelType type;
  231. /** Total bits per sample for this channel.
  232. */
  233. uint32_t bits_per_sample;
  234. /** Floating point exponent bits per channel, or 0 if they are unsigned
  235. * integer.
  236. */
  237. uint32_t exponent_bits_per_sample;
  238. /** The exponent the channel is downsampled by on each axis.
  239. * TODO(lode): expand this comment to match the JPEG XL specification,
  240. * specify how to upscale, how to round the size computation, and to which
  241. * extra channels this field applies.
  242. */
  243. uint32_t dim_shift;
  244. /** Length of the extra channel name in bytes, or 0 if no name.
  245. * Excludes null termination character.
  246. */
  247. uint32_t name_length;
  248. /** Whether alpha channel uses premultiplied alpha. Only applicable if
  249. * type is JXL_CHANNEL_ALPHA.
  250. */
  251. JXL_BOOL alpha_premultiplied;
  252. /** Spot color of the current spot channel in linear RGBA. Only applicable if
  253. * type is JXL_CHANNEL_SPOT_COLOR.
  254. */
  255. float spot_color[4];
  256. /** Only applicable if type is JXL_CHANNEL_CFA.
  257. * TODO(lode): add comment about the meaning of this field.
  258. */
  259. uint32_t cfa_channel;
  260. } JxlExtraChannelInfo;
  261. /* TODO(lode): add API to get the codestream header extensions. */
  262. /** Extensions in the codestream header. */
  263. typedef struct {
  264. /** Extension bits. */
  265. uint64_t extensions;
  266. } JxlHeaderExtensions;
  267. /** Frame blend modes.
  268. * When decoding, if coalescing is enabled (default), this can be ignored.
  269. */
  270. typedef enum {
  271. JXL_BLEND_REPLACE = 0,
  272. JXL_BLEND_ADD = 1,
  273. JXL_BLEND_BLEND = 2,
  274. JXL_BLEND_MULADD = 3,
  275. JXL_BLEND_MUL = 4,
  276. } JxlBlendMode;
  277. /** The information about blending the color channels or a single extra channel.
  278. * When decoding, if coalescing is enabled (default), this can be ignored and
  279. * the blend mode is considered to be JXL_BLEND_REPLACE.
  280. * When encoding, these settings apply to the pixel data given to the encoder.
  281. */
  282. typedef struct {
  283. /** Blend mode.
  284. */
  285. JxlBlendMode blendmode;
  286. /** Reference frame ID to use as the 'bottom' layer (0-3).
  287. */
  288. uint32_t source;
  289. /** Which extra channel to use as the 'alpha' channel for blend modes
  290. * JXL_BLEND_BLEND and JXL_BLEND_MULADD.
  291. */
  292. uint32_t alpha;
  293. /** Clamp values to [0,1] for the purpose of blending.
  294. */
  295. JXL_BOOL clamp;
  296. } JxlBlendInfo;
  297. /** The information about layers.
  298. * When decoding, if coalescing is enabled (default), this can be ignored.
  299. * When encoding, these settings apply to the pixel data given to the encoder,
  300. * the encoder could choose an internal representation that differs.
  301. */
  302. typedef struct {
  303. /** Whether cropping is applied for this frame. When decoding, if false,
  304. * crop_x0 and crop_y0 are set to zero, and xsize and ysize to the main
  305. * image dimensions. When encoding and this is false, those fields are
  306. * ignored. When decoding, if coalescing is enabled (default), this is always
  307. * false, regardless of the internal encoding in the JPEG XL codestream.
  308. */
  309. JXL_BOOL have_crop;
  310. /** Horizontal offset of the frame (can be negative).
  311. */
  312. int32_t crop_x0;
  313. /** Vertical offset of the frame (can be negative).
  314. */
  315. int32_t crop_y0;
  316. /** Width of the frame (number of columns).
  317. */
  318. uint32_t xsize;
  319. /** Height of the frame (number of rows).
  320. */
  321. uint32_t ysize;
  322. /** The blending info for the color channels. Blending info for extra channels
  323. * has to be retrieved separately using JxlDecoderGetExtraChannelBlendInfo.
  324. */
  325. JxlBlendInfo blend_info;
  326. /** After blending, save the frame as reference frame with this ID (0-3).
  327. * Special case: if the frame duration is nonzero, ID 0 means "will not be
  328. * referenced in the future". This value is not used for the last frame.
  329. * When encoding, ID 3 is reserved to frames that are generated internally by
  330. * the encoder, and should not be used by applications.
  331. */
  332. uint32_t save_as_reference;
  333. } JxlLayerInfo;
  334. /** The header of one displayed frame or non-coalesced layer. */
  335. typedef struct {
  336. /** How long to wait after rendering in ticks. The duration in seconds of a
  337. * tick is given by tps_numerator and tps_denominator in @ref
  338. * JxlAnimationHeader.
  339. */
  340. uint32_t duration;
  341. /** SMPTE timecode of the current frame in form 0xHHMMSSFF, or 0. The bits are
  342. * interpreted from most-significant to least-significant as hour, minute,
  343. * second, and frame. If timecode is nonzero, it is strictly larger than that
  344. * of a previous frame with nonzero duration. These values are only available
  345. * if have_timecodes in @ref JxlAnimationHeader is ::JXL_TRUE.
  346. * This value is only used if have_timecodes in @ref JxlAnimationHeader is
  347. * ::JXL_TRUE.
  348. */
  349. uint32_t timecode;
  350. /** Length of the frame name in bytes, or 0 if no name.
  351. * Excludes null termination character. This value is set by the decoder.
  352. * For the encoder, this value is ignored and @ref JxlEncoderSetFrameName is
  353. * used instead to set the name and the length.
  354. */
  355. uint32_t name_length;
  356. /** Indicates this is the last animation frame. This value is set by the
  357. * decoder to indicate no further frames follow. For the encoder, it is not
  358. * required to set this value and it is ignored, @ref JxlEncoderCloseFrames is
  359. * used to indicate the last frame to the encoder instead.
  360. */
  361. JXL_BOOL is_last;
  362. /** Information about the layer in case of no coalescing.
  363. */
  364. JxlLayerInfo layer_info;
  365. } JxlFrameHeader;
  366. #ifdef __cplusplus
  367. }
  368. #endif
  369. #endif /* JXL_CODESTREAM_HEADER_H_ */
  370. /** @}*/