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