openexr_decode.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. ** SPDX-License-Identifier: BSD-3-Clause
  3. ** Copyright Contributors to the OpenEXR Project.
  4. */
  5. #ifndef OPENEXR_CORE_DECODE_H
  6. #define OPENEXR_CORE_DECODE_H
  7. #include "openexr_chunkio.h"
  8. #include "openexr_coding.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /** @file */
  13. /** Can be bit-wise or'ed into the decode_flags in the decode pipeline.
  14. *
  15. * Indicates that the sample count table should be decoded to a an
  16. * individual sample count list (n, m, o, ...), with an extra int at
  17. * the end containing the total samples.
  18. *
  19. * Without this (i.e. a value of 0 in that bit), indicates the sample
  20. * count table should be decoded to a cumulative list (n, n+m, n+m+o,
  21. * ...), which is the on-disk representation.
  22. */
  23. #define EXR_DECODE_SAMPLE_COUNTS_AS_INDIVIDUAL ((uint16_t) (1 << 0))
  24. /** Can be bit-wise or'ed into the decode_flags in the decode pipeline.
  25. *
  26. * Indicates that the data in the channel pointers to decode to is not
  27. * a direct pointer, but instead is a pointer-to-pointers. In this
  28. * mode, the user_pixel_stride and user_line_stride are used to
  29. * advance the pointer offsets for each pixel in the output, but the
  30. * user_bytes_per_element and user_data_type are used to put
  31. * (successive) entries into each destination pointer (if not `NULL`).
  32. *
  33. * So each channel pointer must then point to an array of
  34. * chunk.width * chunk.height pointers.
  35. *
  36. * With this, you can only extract desired pixels (although all the
  37. * pixels must be initially decompressed) to handle such operations
  38. * like proxying where you might want to read every other pixel.
  39. *
  40. * If this is NOT set (0), the default unpacking routine assumes the
  41. * data will be planar and contiguous (each channel is a separate
  42. * memory block), ignoring user_line_stride and user_pixel_stride.
  43. */
  44. #define EXR_DECODE_NON_IMAGE_DATA_AS_POINTERS ((uint16_t) (1 << 1))
  45. /**
  46. * When reading non-image data (i.e. deep), only read the sample table.
  47. */
  48. #define EXR_DECODE_SAMPLE_DATA_ONLY ((uint16_t) (1 << 2))
  49. /**
  50. * Struct meant to be used on a per-thread basis for reading exr data
  51. *
  52. * As should be obvious, this structure is NOT thread safe, but rather
  53. * meant to be used by separate threads, which can all be accessing
  54. * the same context concurrently.
  55. */
  56. typedef struct _exr_decode_pipeline
  57. {
  58. /** The output channel information for this chunk.
  59. *
  60. * User is expected to fill the channel pointers for the desired
  61. * output channels (any that are `NULL` will be skipped) if you are
  62. * going to use exr_decoding_choose_default_routines(). If all that is
  63. * desired is to read and decompress the data, this can be left
  64. * uninitialized.
  65. *
  66. * Describes the channel information. This information is
  67. * allocated dynamically during exr_decoding_initialize().
  68. */
  69. exr_coding_channel_info_t* channels;
  70. int16_t channel_count;
  71. /** Decode flags to control the behavior. */
  72. uint16_t decode_flags;
  73. /** Copy of the parameters given to the initialize/update for
  74. * convenience.
  75. */
  76. int part_index;
  77. exr_const_context_t context;
  78. exr_chunk_info_t chunk;
  79. /** Can be used by the user to pass custom context data through
  80. * the decode pipeline.
  81. */
  82. void* decoding_user_data;
  83. /** The (compressed) buffer.
  84. *
  85. * If `NULL`, will be allocated during the run of the pipeline.
  86. *
  87. * If the caller wishes to take control of the buffer, simple
  88. * adopt the pointer and set it to `NULL` here. Be cognizant of any
  89. * custom allocators.
  90. */
  91. void* packed_buffer;
  92. /** Used when re-using the same decode pipeline struct to know if
  93. * chunk is changed size whether current buffer is large enough.
  94. */
  95. size_t packed_alloc_size;
  96. /** The decompressed buffer (unpacked_size from the chunk block
  97. * info), but still packed into storage order, only needed for
  98. * compressed files.
  99. *
  100. * If `NULL`, will be allocated during the run of the pipeline when
  101. * needed.
  102. *
  103. * If the caller wishes to take control of the buffer, simple
  104. * adopt the pointer and set it to `NULL` here. Be cognizant of any
  105. * custom allocators.
  106. */
  107. void* unpacked_buffer;
  108. /** Used when re-using the same decode pipeline struct to know if
  109. * chunk is changed size whether current buffer is large enough.
  110. */
  111. size_t unpacked_alloc_size;
  112. /** For deep or other non-image data: packed sample table
  113. * (compressed, raw on disk representation).
  114. */
  115. void* packed_sample_count_table;
  116. size_t packed_sample_count_alloc_size;
  117. /** Usable, native sample count table. Depending on the flag set
  118. * above, will be decoded to either a cumulative list (n, n+m,
  119. * n+m+o, ...), or an individual table (n, m, o, ...). As an
  120. * optimization, if the latter individual count table is chosen,
  121. * an extra int32_t will be allocated at the end of the table to
  122. * contain the total count of samples, so the table will be n+1
  123. * samples in size.
  124. */
  125. int32_t* sample_count_table;
  126. size_t sample_count_alloc_size;
  127. /** A scratch buffer of unpacked_size for intermediate results.
  128. *
  129. * If `NULL`, will be allocated during the run of the pipeline when
  130. * needed.
  131. *
  132. * If the caller wishes to take control of the buffer, simple
  133. * adopt the pointer and set it to `NULL` here. Be cognizant of any
  134. * custom allocators.
  135. */
  136. void* scratch_buffer_1;
  137. /** Used when re-using the same decode pipeline struct to know if
  138. * chunk is changed size whether current buffer is large enough.
  139. */
  140. size_t scratch_alloc_size_1;
  141. /** Some decompression routines may need a second scratch buffer (zlib).
  142. *
  143. * If `NULL`, will be allocated during the run of the pipeline when
  144. * needed.
  145. *
  146. * If the caller wishes to take control of the buffer, simple
  147. * adopt the pointer and set it to `NULL` here. Be cognizant of any
  148. * custom allocators.
  149. */
  150. void* scratch_buffer_2;
  151. /** Used when re-using the same decode pipeline struct to know if
  152. * chunk is changed size whether current buffer is large enough.
  153. */
  154. size_t scratch_alloc_size_2;
  155. /** Enable a custom allocator for the different buffers (if
  156. * decoding on a GPU). If `NULL`, will use the allocator from the
  157. * context.
  158. */
  159. void* (*alloc_fn) (exr_transcoding_pipeline_buffer_id_t, size_t);
  160. /** Enable a custom allocator for the different buffers (if
  161. * decoding on a GPU). If `NULL`, will use the allocator from the
  162. * context.
  163. */
  164. void (*free_fn) (exr_transcoding_pipeline_buffer_id_t, void*);
  165. /** Function chosen to read chunk data from the context.
  166. *
  167. * Initialized to a default generic read routine, may be updated
  168. * based on channel information when
  169. * exr_decoding_choose_default_routines() is called. This is done such that
  170. * if the file is uncompressed and the output channel data is
  171. * planar and the same type, the read function can read straight
  172. * into the output channels, getting closer to a zero-copy
  173. * operation. Otherwise a more traditional read, decompress, then
  174. * unpack pipeline will be used with a default reader.
  175. *
  176. * This is allowed to be overridden, but probably is not necessary
  177. * in most scenarios.
  178. */
  179. exr_result_t (*read_fn) (struct _exr_decode_pipeline* pipeline);
  180. /** Function chosen based on the compression type of the part to
  181. * decompress data.
  182. *
  183. * If the user has a custom decompression method for the
  184. * compression on this part, this can be changed after
  185. * initialization.
  186. *
  187. * If only compressed data is desired, then assign this to `NULL`
  188. * after initialization.
  189. */
  190. exr_result_t (*decompress_fn) (struct _exr_decode_pipeline* pipeline);
  191. /** Function which can be provided if you have bespoke handling for
  192. * non-image data and need to re-allocate the data to handle the
  193. * about-to-be unpacked data.
  194. *
  195. * If left `NULL`, will assume the memory pointed to by the channel
  196. * pointers is sufficient.
  197. */
  198. exr_result_t (*realloc_nonimage_data_fn) (
  199. struct _exr_decode_pipeline* pipeline);
  200. /** Function chosen based on the output layout of the channels of the part to
  201. * decompress data.
  202. *
  203. * This will be `NULL` after initialization, until the user
  204. * specifies a custom routine, or initializes the channel data and
  205. * calls exr_decoding_choose_default_routines().
  206. *
  207. * If only compressed data is desired, then leave or assign this
  208. * to `NULL` after initialization.
  209. */
  210. exr_result_t (*unpack_and_convert_fn) (
  211. struct _exr_decode_pipeline* pipeline);
  212. /** Small stash of channel info values. This is faster than calling
  213. * malloc when the channel count in the part is small (RGBAZ),
  214. * which is super common, however if there are a large number of
  215. * channels, it will allocate space for that, so do not rely on
  216. * this being used.
  217. */
  218. exr_coding_channel_info_t _quick_chan_store[5];
  219. } exr_decode_pipeline_t;
  220. /** @brief Simple macro to initialize an empty decode pipeline. */
  221. #define EXR_DECODE_PIPELINE_INITIALIZER \
  222. { \
  223. 0 \
  224. }
  225. /** Initialize the decoding pipeline structure with the channel info
  226. * for the specified part, and the first block to be read.
  227. *
  228. * NB: The decode->unpack_and_convert_fn field will be `NULL` after this. If that
  229. * stage is desired, initialize the channel output information and
  230. * call exr_decoding_choose_default_routines().
  231. */
  232. EXR_EXPORT
  233. exr_result_t exr_decoding_initialize (
  234. exr_const_context_t ctxt,
  235. int part_index,
  236. const exr_chunk_info_t* cinfo,
  237. exr_decode_pipeline_t* decode);
  238. /** Given an initialized decode pipeline, find appropriate functions
  239. * to read and shuffle/convert data into the defined channel outputs.
  240. *
  241. * Calling this is not required if custom routines will be used, or if
  242. * just the raw compressed data is desired. Although in that scenario,
  243. * it is probably easier to just read the chunk directly using
  244. * exr_read_chunk().
  245. */
  246. EXR_EXPORT
  247. exr_result_t exr_decoding_choose_default_routines (
  248. exr_const_context_t ctxt, int part_index, exr_decode_pipeline_t* decode);
  249. /** Given a decode pipeline previously initialized, update it for the
  250. * new chunk to be read.
  251. *
  252. * In this manner, memory buffers can be re-used to avoid continual
  253. * malloc/free calls. Further, it allows the previous choices for
  254. * the various functions to be quickly re-used.
  255. */
  256. EXR_EXPORT
  257. exr_result_t exr_decoding_update (
  258. exr_const_context_t ctxt,
  259. int part_index,
  260. const exr_chunk_info_t* cinfo,
  261. exr_decode_pipeline_t* decode);
  262. /** Execute the decoding pipeline. */
  263. EXR_EXPORT
  264. exr_result_t exr_decoding_run (
  265. exr_const_context_t ctxt, int part_index, exr_decode_pipeline_t* decode);
  266. /** Free any intermediate memory in the decoding pipeline.
  267. *
  268. * This does *not* free any pointers referred to in the channel info
  269. * areas, but rather only the intermediate buffers and memory needed
  270. * for the structure itself.
  271. */
  272. EXR_EXPORT
  273. exr_result_t
  274. exr_decoding_destroy (exr_const_context_t ctxt, exr_decode_pipeline_t* decode);
  275. #ifdef __cplusplus
  276. } /* extern "C" */
  277. #endif
  278. #endif /* OPENEXR_CORE_DECODE_H */