Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
nvjpeg.h825 linesDownload Raw Back to include
1/*
2 * Copyright 2009-2022 NVIDIA Corporation.  All rights reserved.
3 *
4 * NOTICE TO LICENSEE:
5 *
6 * This source code and/or documentation ("Licensed Deliverables") are
7 * subject to NVIDIA intellectual property rights under U.S. and
8 * international Copyright laws.
9 *
10 * These Licensed Deliverables contained herein is PROPRIETARY and
11 * CONFIDENTIAL to NVIDIA and is being provided under the terms and
12 * conditions of a form of NVIDIA software license agreement by and
13 * between NVIDIA and Licensee ("License Agreement") or electronically
14 * accepted by Licensee.  Notwithstanding any terms or conditions to
15 * the contrary in the License Agreement, reproduction or disclosure
16 * of the Licensed Deliverables to any third party without the express
17 * written consent of NVIDIA is prohibited.
18 *
19 * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
20 * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE
21 * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE.  IT IS
22 * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.
23 * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED
24 * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
25 * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
26 * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
27 * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY
28 * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
29 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
30 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
31 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
32 * OF THESE LICENSED DELIVERABLES.
33 *
34 * U.S. Government End Users.  These Licensed Deliverables are a
35 * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT
36 * 1995), consisting of "commercial computer software" and "commercial
37 * computer software documentation" as such terms are used in 48
38 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government
39 * only as a commercial end item.  Consistent with 48 C.F.R.12.212 and
40 * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all
41 * U.S. Government End Users acquire the Licensed Deliverables with
42 * only those rights set forth herein.
43 *
44 * Any use of the Licensed Deliverables in individual and commercial
45 * software must include, in the user documentation and internal
46 * comments to the code, the above Disclaimer and U.S. Government End
47 * Users Notice.
48 */
49
50  
51#ifndef NV_JPEG_HEADER
52#define NV_JPEG_HEADER
53
54#define NVJPEGAPI
55
56
57#include "cuda_runtime_api.h"
58#include "library_types.h"
59
60#include "stdint.h"
61
62#if defined(__cplusplus)
63  extern "C" {
64#endif
65
66// Maximum number of channels nvjpeg decoder supports
67#define NVJPEG_MAX_COMPONENT 4
68
69// nvjpeg version information
70#define NVJPEG_VER_MAJOR 12
71#define NVJPEG_VER_MINOR 3
72#define NVJPEG_VER_PATCH 1
73#define NVJPEG_VER_BUILD 117
74
75/* nvJPEG status enums, returned by nvJPEG API */
76typedef enum
77{
78    NVJPEG_STATUS_SUCCESS                       = 0,
79    NVJPEG_STATUS_NOT_INITIALIZED               = 1,
80    NVJPEG_STATUS_INVALID_PARAMETER             = 2,
81    NVJPEG_STATUS_BAD_JPEG                      = 3,
82    NVJPEG_STATUS_JPEG_NOT_SUPPORTED            = 4,
83    NVJPEG_STATUS_ALLOCATOR_FAILURE             = 5,
84    NVJPEG_STATUS_EXECUTION_FAILED              = 6,
85    NVJPEG_STATUS_ARCH_MISMATCH                 = 7,
86    NVJPEG_STATUS_INTERNAL_ERROR                = 8,
87    NVJPEG_STATUS_IMPLEMENTATION_NOT_SUPPORTED  = 9,
88    NVJPEG_STATUS_INCOMPLETE_BITSTREAM          = 10
89} nvjpegStatus_t;
90
91
92// Enums for EXIF Orientation
93typedef enum nvjpegExifOrientation
94{
95    NVJPEG_ORIENTATION_UNKNOWN = 0,
96    NVJPEG_ORIENTATION_NORMAL = 1,
97    NVJPEG_ORIENTATION_FLIP_HORIZONTAL = 2,
98    NVJPEG_ORIENTATION_ROTATE_180 = 3,
99    NVJPEG_ORIENTATION_FLIP_VERTICAL = 4,
100    NVJPEG_ORIENTATION_TRANSPOSE = 5,
101    NVJPEG_ORIENTATION_ROTATE_90 = 6,
102    NVJPEG_ORIENTATION_TRANSVERSE = 7,
103    NVJPEG_ORIENTATION_ROTATE_270 = 8
104} nvjpegExifOrientation_t;
105
106// Enum identifies image chroma subsampling values stored inside JPEG input stream
107// In the case of NVJPEG_CSS_GRAY only 1 luminance channel is encoded in JPEG input stream
108// Otherwise both chroma planes are present
109typedef enum
110{
111    NVJPEG_CSS_444 = 0,
112    NVJPEG_CSS_422 = 1,
113    NVJPEG_CSS_420 = 2,
114    NVJPEG_CSS_440 = 3,
115    NVJPEG_CSS_411 = 4,
116    NVJPEG_CSS_410 = 5,
117    NVJPEG_CSS_GRAY = 6,
118    NVJPEG_CSS_410V = 7,
119    NVJPEG_CSS_UNKNOWN = -1
120} nvjpegChromaSubsampling_t;
121
122// Parameter of this type specifies what type of output user wants for image decoding
123typedef enum
124{
125    // return decompressed image as it is - write planar output
126    NVJPEG_OUTPUT_UNCHANGED   = 0,
127    // return planar luma and chroma, assuming YCbCr colorspace
128    NVJPEG_OUTPUT_YUV         = 1, 
129    // return luma component only, if YCbCr colorspace, 
130    // or try to convert to grayscale,
131    // writes to 1-st channel of nvjpegImage_t
132    NVJPEG_OUTPUT_Y           = 2,
133    // convert to planar RGB 
134    NVJPEG_OUTPUT_RGB         = 3,
135    // convert to planar BGR
136    NVJPEG_OUTPUT_BGR         = 4, 
137    // convert to interleaved RGB and write to 1-st channel of nvjpegImage_t
138    NVJPEG_OUTPUT_RGBI        = 5, 
139    // convert to interleaved BGR and write to 1-st channel of nvjpegImage_t
140    NVJPEG_OUTPUT_BGRI        = 6,
141    // 16 bit unsigned output in interleaved format
142    NVJPEG_OUTPUT_UNCHANGEDI_U16 = 7,
143    // maximum allowed value
144    NVJPEG_OUTPUT_FORMAT_MAX  = 7
145} nvjpegOutputFormat_t;
146
147// Parameter of this type specifies what type of input user provides for encoding
148typedef enum
149{
150    NVJPEG_INPUT_RGB         = 3, // Input is RGB - will be converted to YCbCr before encoding
151    NVJPEG_INPUT_BGR         = 4, // Input is RGB - will be converted to YCbCr before encoding
152    NVJPEG_INPUT_RGBI        = 5, // Input is interleaved RGB - will be converted to YCbCr before encoding
153    NVJPEG_INPUT_BGRI        = 6  // Input is interleaved RGB - will be converted to YCbCr before encoding
154} nvjpegInputFormat_t;
155
156// Implementation
157// NVJPEG_BACKEND_DEFAULT    : default value
158// NVJPEG_BACKEND_HYBRID     : uses CPU for Huffman decode
159// NVJPEG_BACKEND_GPU_HYBRID : uses GPU assisted Huffman decode. nvjpegDecodeBatched will use GPU decoding for baseline JPEG bitstreams with
160//                             interleaved scan when batch size is bigger than 50
161// NVJPEG_BACKEND_HARDWARE   : supports baseline JPEG bitstream with single scan. 410 and 411 sub-samplings are not supported
162// NVJPEG_BACKEND_GPU_HYBRID_DEVICE : nvjpegDecodeBatched will support bitstream input on device memory
163// NVJPEG_BACKEND_HARDWARE_DEVICE   : nvjpegDecodeBatched will support bitstream input on device memory
164typedef enum 
165{
166    NVJPEG_BACKEND_DEFAULT = 0,
167    NVJPEG_BACKEND_HYBRID  = 1,
168    NVJPEG_BACKEND_GPU_HYBRID = 2,
169    NVJPEG_BACKEND_HARDWARE = 3,
170    NVJPEG_BACKEND_GPU_HYBRID_DEVICE = 4,
171    NVJPEG_BACKEND_HARDWARE_DEVICE = 5,
172    NVJPEG_BACKEND_LOSSLESS_JPEG = 6,
173} nvjpegBackend_t;
174
175// Currently parseable JPEG encodings (SOF markers)
176typedef enum
177{
178    NVJPEG_ENCODING_UNKNOWN                                 = 0x0,
179
180    NVJPEG_ENCODING_BASELINE_DCT                            = 0xc0,
181    NVJPEG_ENCODING_EXTENDED_SEQUENTIAL_DCT_HUFFMAN         = 0xc1,
182    NVJPEG_ENCODING_PROGRESSIVE_DCT_HUFFMAN                 = 0xc2,
183    NVJPEG_ENCODING_LOSSLESS_HUFFMAN                        = 0xc3
184
185} nvjpegJpegEncoding_t;
186
187typedef enum 
188{
189    NVJPEG_SCALE_NONE = 0, // decoded output is not scaled 
190    NVJPEG_SCALE_1_BY_2 = 1, // decoded output width and height is scaled by a factor of 1/2
191    NVJPEG_SCALE_1_BY_4 = 2, // decoded output width and height is scaled by a factor of 1/4
192    NVJPEG_SCALE_1_BY_8 = 3, // decoded output width and height is scaled by a factor of 1/8
193} nvjpegScaleFactor_t;
194
195#define NVJPEG_FLAGS_DEFAULT 0
196#define NVJPEG_FLAGS_HW_DECODE_NO_PIPELINE 1
197#define NVJPEG_FLAGS_ENABLE_MEMORY_POOLS   1<<1
198#define NVJPEG_FLAGS_BITSTREAM_STRICT  1<<2
199#define NVJPEG_FLAGS_REDUCED_MEMORY_DECODE  1<<3
200#define NVJPEG_FLAGS_REDUCED_MEMORY_DECODE_ZERO_COPY  1<<4
201#define NVJPEG_FLAGS_UPSAMPLING_WITH_INTERPOLATION 1<<5
202
203
204// Output descriptor.
205// Data that is written to planes depends on output format
206typedef struct
207{
208    unsigned char * channel[NVJPEG_MAX_COMPONENT];
209    size_t    pitch[NVJPEG_MAX_COMPONENT];
210} nvjpegImage_t;
211
212// Prototype for device memory allocation, modelled after cudaMalloc()
213typedef int (*tDevMalloc)(void**, size_t);
214// Prototype for device memory release
215typedef int (*tDevFree)(void*);
216
217// Prototype for pinned memory allocation, modelled after cudaHostAlloc()
218typedef int (*tPinnedMalloc)(void**, size_t, unsigned int flags);
219// Prototype for device memory release
220typedef int (*tPinnedFree)(void*);
221
222// Memory allocator using mentioned prototypes, provided to nvjpegCreateEx
223// This allocator will be used for all device memory allocations inside library
224// In any way library is doing smart allocations (reallocates memory only if needed)
225typedef struct 
226{
227    tDevMalloc dev_malloc;
228    tDevFree dev_free;
229} nvjpegDevAllocator_t;
230
231// Pinned memory allocator using mentioned prototypes, provided to nvjpegCreate
232// This allocator will be used for all pinned host memory allocations inside library
233// In any way library is doing smart allocations (reallocates memory only if needed)
234typedef struct 
235{
236    tPinnedMalloc pinned_malloc;
237    tPinnedFree pinned_free;
238} nvjpegPinnedAllocator_t;
239
240
241typedef int (*tDevMallocV2)(void* ctx, void **ptr, size_t size, cudaStream_t stream);
242
243typedef int (*tDevFreeV2)(void* ctx, void *ptr, size_t size, cudaStream_t stream);
244
245
246typedef int (*tPinnedMallocV2)(void* ctx, void **ptr, size_t size, cudaStream_t stream);
247
248typedef int (*tPinnedFreeV2)(void* ctx, void *ptr, size_t size, cudaStream_t stream);
249
250typedef struct
251{
252    tDevMallocV2 dev_malloc;
253    tDevFreeV2 dev_free;
254    void *dev_ctx;
255} nvjpegDevAllocatorV2_t;
256
257typedef struct
258{
259    tPinnedMallocV2 pinned_malloc;
260    tPinnedFreeV2 pinned_free;
261    void *pinned_ctx;
262} nvjpegPinnedAllocatorV2_t;
263
264// Opaque library handle identifier.
265struct nvjpegHandle;
266typedef struct nvjpegHandle* nvjpegHandle_t;
267
268// Opaque jpeg decoding state handle identifier - used to store intermediate information between deccding phases
269struct nvjpegJpegState;
270typedef struct nvjpegJpegState* nvjpegJpegState_t;
271
272// returns library's property values, such as MAJOR_VERSION, MINOR_VERSION or PATCH_LEVEL
273nvjpegStatus_t NVJPEGAPI nvjpegGetProperty(libraryPropertyType type, int *value);
274// returns CUDA Toolkit property values that was used for building library, 
275// such as MAJOR_VERSION, MINOR_VERSION or PATCH_LEVEL
276nvjpegStatus_t NVJPEGAPI nvjpegGetCudartProperty(libraryPropertyType type, int *value);
277
278// Initalization of nvjpeg handle. This handle is used for all consecutive calls
279// IN         backend       : Backend to use. Currently Default or Hybrid (which is the same at the moment) is supported.
280// IN         allocator     : Pointer to nvjpegDevAllocator. If NULL - use default cuda calls (cudaMalloc/cudaFree)
281// INT/OUT    handle        : Codec instance, use for other calls
282nvjpegStatus_t NVJPEGAPI nvjpegCreate(nvjpegBackend_t backend, nvjpegDevAllocator_t *dev_allocator, nvjpegHandle_t *handle);
283
284// Initalization of nvjpeg handle with default backend and default memory allocators.
285// INT/OUT    handle        : Codec instance, use for other calls
286nvjpegStatus_t NVJPEGAPI nvjpegCreateSimple(nvjpegHandle_t *handle);
287
288// Initalization of nvjpeg handle with additional parameters. This handle is used for all consecutive nvjpeg calls
289// IN         backend       : Backend to use. Currently Default or Hybrid (which is the same at the moment) is supported.
290// IN         dev_allocator : Pointer to nvjpegDevAllocator. If NULL - use default cuda calls (cudaMalloc/cudaFree)
291// IN         pinned_allocator : Pointer to nvjpegPinnedAllocator. If NULL - use default cuda calls (cudaHostAlloc/cudaFreeHost)
292// IN         flags         : Parameters for the operation. Must be 0.
293// INT/OUT    handle        : Codec instance, use for other calls
294nvjpegStatus_t NVJPEGAPI nvjpegCreateEx(nvjpegBackend_t backend, 
295        nvjpegDevAllocator_t *dev_allocator, 
296        nvjpegPinnedAllocator_t *pinned_allocator, 
297        unsigned int flags,
298        nvjpegHandle_t *handle);
299
300nvjpegStatus_t NVJPEGAPI nvjpegCreateExV2(nvjpegBackend_t backend,
301        nvjpegDevAllocatorV2_t *dev_allocator,
302        nvjpegPinnedAllocatorV2_t *pinned_allocator,
303        unsigned int flags,
304        nvjpegHandle_t *handle);
305
306// Release the handle and resources.
307// IN/OUT     handle: instance handle to release 
308nvjpegStatus_t NVJPEGAPI nvjpegDestroy(nvjpegHandle_t handle);
309
310// Sets padding for device memory allocations. After success on this call any device memory allocation
311// would be padded to the multiple of specified number of bytes. 
312// IN         padding: padding size
313// IN/OUT     handle: instance handle to release 
314nvjpegStatus_t NVJPEGAPI nvjpegSetDeviceMemoryPadding(size_t padding, nvjpegHandle_t handle);
315
316// Retrieves padding for device memory allocations
317// IN/OUT     padding: padding size currently used in handle.
318// IN/OUT     handle: instance handle to release 
319nvjpegStatus_t NVJPEGAPI nvjpegGetDeviceMemoryPadding(size_t *padding, nvjpegHandle_t handle);
320
321// Sets padding for pinned host memory allocations. After success on this call any pinned host memory allocation
322// would be padded to the multiple of specified number of bytes. 
323// IN         padding: padding size
324// IN/OUT     handle: instance handle to release 
325nvjpegStatus_t NVJPEGAPI nvjpegSetPinnedMemoryPadding(size_t padding, nvjpegHandle_t handle);
326
327// Retrieves padding for pinned host memory allocations
328// IN/OUT     padding: padding size currently used in handle.
329// IN/OUT     handle: instance handle to release 
330nvjpegStatus_t NVJPEGAPI nvjpegGetPinnedMemoryPadding(size_t *padding, nvjpegHandle_t handle);
331
332
333nvjpegStatus_t NVJPEGAPI nvjpegGetHardwareDecoderInfo(nvjpegHandle_t handle,
334        unsigned int* num_engines,
335        unsigned int* num_cores_per_engine);
336
337
338// Initalization of decoding state
339// IN         handle        : Library handle
340// INT/OUT    jpeg_handle   : Decoded jpeg image state handle
341nvjpegStatus_t NVJPEGAPI nvjpegJpegStateCreate(nvjpegHandle_t handle, nvjpegJpegState_t *jpeg_handle);
342
343// Release the jpeg image handle.
344// INT/OUT    jpeg_handle   : Decoded jpeg image state handle
345nvjpegStatus_t NVJPEGAPI nvjpegJpegStateDestroy(nvjpegJpegState_t jpeg_handle);
346// 
347// Retrieve the image info, including channel, width and height of each component, and chroma subsampling.
348// If less than NVJPEG_MAX_COMPONENT channels are encoded, then zeros would be set to absent channels information
349// If the image is 3-channel, all three groups are valid.
350// This function is thread safe.
351// IN         handle      : Library handle
352// IN         data        : Pointer to the buffer containing the jpeg stream data to be decoded. 
353// IN         length      : Length of the jpeg image buffer.
354// OUT        nComponent  : Number of componenets of the image, currently only supports 1-channel (grayscale) or 3-channel.
355// OUT        subsampling : Chroma subsampling used in this JPEG, see nvjpegChromaSubsampling_t
356// OUT        widths      : pointer to NVJPEG_MAX_COMPONENT of ints, returns width of each channel. 0 if channel is not encoded  
357// OUT        heights     : pointer to NVJPEG_MAX_COMPONENT of ints, returns height of each channel. 0 if channel is not encoded 
358nvjpegStatus_t NVJPEGAPI nvjpegGetImageInfo(
359        nvjpegHandle_t handle,
360        const unsigned char *data, 
361        size_t length,
362        int *nComponents, 
363        nvjpegChromaSubsampling_t *subsampling,
364        int *widths,
365        int *heights);
366                   
367
368// Decodes single image. The API is back-end agnostic. It will decide on which implementation to use internally
369// Destination buffers should be large enough to be able to store  output of specified format.
370// For each color plane sizes could be retrieved for image using nvjpegGetImageInfo()
371// and minimum required memory buffer for each plane is nPlaneHeight*nPlanePitch where nPlanePitch >= nPlaneWidth for
372// planar output formats and nPlanePitch >= nPlaneWidth*nOutputComponents for interleaved output format.
373// 
374// IN/OUT     handle        : Library handle
375// INT/OUT    jpeg_handle   : Decoded jpeg image state handle
376// IN         data          : Pointer to the buffer containing the jpeg image to be decoded. 
377// IN         length        : Length of the jpeg image buffer.
378// IN         output_format : Output data format. See nvjpegOutputFormat_t for description
379// IN/OUT     destination   : Pointer to structure with information about output buffers. See nvjpegImage_t description.
380// IN/OUT     stream        : CUDA stream where to submit all GPU work
381// 
382// \return NVJPEG_STATUS_SUCCESS if successful
383nvjpegStatus_t NVJPEGAPI nvjpegDecode(
384        nvjpegHandle_t handle,
385        nvjpegJpegState_t jpeg_handle,
386        const unsigned char *data,
387        size_t length, 
388        nvjpegOutputFormat_t output_format,
389        nvjpegImage_t *destination,
390        cudaStream_t stream);
391
392
393//////////////////////////////////////////////
394/////////////// Batch decoding ///////////////
395//////////////////////////////////////////////
396
397// Resets and initizlizes batch decoder for working on the batches of specified size
398// Should be called once for decoding bathes of this specific size, also use to reset failed batches
399// IN/OUT     handle          : Library handle
400// INT/OUT    jpeg_handle     : Decoded jpeg image state handle
401// IN         batch_size      : Size of the batch
402// IN         max_cpu_threads : Maximum number of CPU threads that will be processing this batch
403// IN         output_format   : Output data format. Will be the same for every image in batch
404//
405// \return NVJPEG_STATUS_SUCCESS if successful
406nvjpegStatus_t NVJPEGAPI nvjpegDecodeBatchedInitialize(
407          nvjpegHandle_t handle,
408          nvjpegJpegState_t jpeg_handle,
409          int batch_size,
410          int max_cpu_threads,
411          nvjpegOutputFormat_t output_format);
412
413// Decodes batch of images. Output buffers should be large enough to be able to store 
414// outputs of specified format, see single image decoding description for details. Call to 
415// nvjpegDecodeBatchedInitialize() is required prior to this call, batch size is expected to be the same as 
416// parameter to this batch initialization function.
417// 
418// IN/OUT     handle        : Library handle
419// INT/OUT    jpeg_handle   : Decoded jpeg image state handle
420// IN         data          : Array of size batch_size of pointers to the input buffers containing the jpeg images to be decoded. 
421// IN         lengths       : Array of size batch_size with lengths of the jpeg images' buffers in the batch.
422// IN/OUT     destinations  : Array of size batch_size with pointers to structure with information about output buffers, 
423// IN/OUT     stream        : CUDA stream where to submit all GPU work
424// 
425// \return NVJPEG_STATUS_SUCCESS if successful
426nvjpegStatus_t NVJPEGAPI nvjpegDecodeBatched(
427          nvjpegHandle_t handle,
428          nvjpegJpegState_t jpeg_handle,
429          const unsigned char *const *data,
430          const size_t *lengths, 
431          nvjpegImage_t *destinations,
432          cudaStream_t stream);
433
434// Allocates the internal buffers as a pre-allocation step
435// IN    handle          : Library handle
436// IN    jpeg_handle     : Decoded jpeg image state handle
437// IN    width   : frame width
438// IN    height  : frame height
439// IN    chroma_subsampling   : chroma subsampling of images to be decoded
440// IN    output_format : out format
441
442nvjpegStatus_t NVJPEGAPI nvjpegDecodeBatchedPreAllocate(
443          nvjpegHandle_t handle,
444          nvjpegJpegState_t jpeg_handle,
445          int batch_size,
446          int width,
447          int height,
448          nvjpegChromaSubsampling_t chroma_subsampling,
449          nvjpegOutputFormat_t output_format);
450
451
452// Allocates the internal buffers as a pre-allocation step
453// IN    handle          : Library handle
454// IN    jpeg_handle     : Decoded jpeg image state handle
455// IN    data            : jpeg bitstream containing huffman and quantization tables
456// IN    length          : bitstream size in bytes
457
458nvjpegStatus_t NVJPEGAPI nvjpegDecodeBatchedParseJpegTables(
459          nvjpegHandle_t handle,
460          nvjpegJpegState_t jpeg_handle,
461          const unsigned char *data,
462          const size_t length);
463
464/**********************************************************
465*                        Compression                      *
466**********************************************************/
467struct nvjpegEncoderState;
468typedef struct nvjpegEncoderState* nvjpegEncoderState_t;
469
470nvjpegStatus_t NVJPEGAPI nvjpegEncoderStateCreate(
471        nvjpegHandle_t handle,
472        nvjpegEncoderState_t *encoder_state,
473        cudaStream_t stream);
474
475nvjpegStatus_t NVJPEGAPI nvjpegEncoderStateDestroy(nvjpegEncoderState_t encoder_state);
476
477struct nvjpegEncoderParams;
478typedef struct nvjpegEncoderParams* nvjpegEncoderParams_t;
479
480nvjpegStatus_t NVJPEGAPI nvjpegEncoderParamsCreate(
481        nvjpegHandle_t handle, 
482        nvjpegEncoderParams_t *encoder_params,
483        cudaStream_t stream);
484
485nvjpegStatus_t NVJPEGAPI nvjpegEncoderParamsDestroy(nvjpegEncoderParams_t encoder_params);
486
487nvjpegStatus_t NVJPEGAPI nvjpegEncoderParamsSetQuality(
488        nvjpegEncoderParams_t encoder_params,
489        const int quality,
490        cudaStream_t stream);
491
492nvjpegStatus_t NVJPEGAPI nvjpegEncoderParamsSetEncoding(
493        nvjpegEncoderParams_t encoder_params,
494        nvjpegJpegEncoding_t etype,
495        cudaStream_t stream);
496
497nvjpegStatus_t NVJPEGAPI nvjpegEncoderParamsSetOptimizedHuffman(
498        nvjpegEncoderParams_t encoder_params,
499        const int optimized,
500        cudaStream_t stream);
501
502nvjpegStatus_t NVJPEGAPI nvjpegEncoderParamsSetSamplingFactors(
503        nvjpegEncoderParams_t encoder_params,
504        const nvjpegChromaSubsampling_t chroma_subsampling,
505        cudaStream_t stream);
506
507nvjpegStatus_t NVJPEGAPI nvjpegEncodeGetBufferSize(
508        nvjpegHandle_t handle,
509        const nvjpegEncoderParams_t encoder_params,
510        int image_width,
511        int image_height,
512        size_t *max_stream_length);
513
514nvjpegStatus_t NVJPEGAPI nvjpegEncodeYUV(
515        nvjpegHandle_t handle,
516        nvjpegEncoderState_t encoder_state,
517        const nvjpegEncoderParams_t encoder_params,
518        const nvjpegImage_t *source,
519        nvjpegChromaSubsampling_t chroma_subsampling, 
520        int image_width,
521        int image_height,
522        cudaStream_t stream);
523
524nvjpegStatus_t NVJPEGAPI nvjpegEncodeImage(
525        nvjpegHandle_t handle,
526        nvjpegEncoderState_t encoder_state,
527        const nvjpegEncoderParams_t encoder_params,
528        const nvjpegImage_t *source,
529        nvjpegInputFormat_t input_format, 
530        int image_width,
531        int image_height,
532        cudaStream_t stream);
533
534nvjpegStatus_t NVJPEGAPI nvjpegEncodeRetrieveBitstreamDevice(
535        nvjpegHandle_t handle,
536        nvjpegEncoderState_t encoder_state,
537        unsigned char *data,
538        size_t *length,
539        cudaStream_t stream);
540
541nvjpegStatus_t NVJPEGAPI nvjpegEncodeRetrieveBitstream(
542        nvjpegHandle_t handle,
543        nvjpegEncoderState_t encoder_state,
544        unsigned char *data,
545        size_t *length,
546        cudaStream_t stream);
547
548///////////////////////////////////////////////////////////////////////////////////
549// API v2 //
550///////////////////////////////////////////////////////////////////////////////////
551
552
553///////////////////////////////////////////////////////////////////////////////////
554// NVJPEG buffers //
555///////////////////////////////////////////////////////////////////////////////////
556
557struct nvjpegBufferPinned;
558typedef struct nvjpegBufferPinned* nvjpegBufferPinned_t;
559
560nvjpegStatus_t NVJPEGAPI nvjpegBufferPinnedCreate(nvjpegHandle_t handle, 
561    nvjpegPinnedAllocator_t* pinned_allocator,
562    nvjpegBufferPinned_t* buffer);
563
564nvjpegStatus_t NVJPEGAPI nvjpegBufferPinnedCreateV2(nvjpegHandle_t handle,
565    nvjpegPinnedAllocatorV2_t* pinned_allocator,
566    nvjpegBufferPinned_t* buffer);
567
568nvjpegStatus_t NVJPEGAPI nvjpegBufferPinnedResize(nvjpegBufferPinned_t buffer, size_t size, cudaStream_t stream);
569
570nvjpegStatus_t NVJPEGAPI nvjpegBufferPinnedDestroy(nvjpegBufferPinned_t buffer);
571
572struct nvjpegBufferDevice;
573typedef struct nvjpegBufferDevice* nvjpegBufferDevice_t;
574
575nvjpegStatus_t NVJPEGAPI nvjpegBufferDeviceCreate(nvjpegHandle_t handle, 
576    nvjpegDevAllocator_t* device_allocator, 
577    nvjpegBufferDevice_t* buffer);
578
579nvjpegStatus_t NVJPEGAPI nvjpegBufferDeviceCreateV2(nvjpegHandle_t handle,
580    nvjpegDevAllocatorV2_t* device_allocator,
581    nvjpegBufferDevice_t* buffer);
582
583nvjpegStatus_t NVJPEGAPI nvjpegBufferDeviceResize(nvjpegBufferDevice_t buffer, size_t size, cudaStream_t stream);
584
585nvjpegStatus_t NVJPEGAPI nvjpegBufferDeviceDestroy(nvjpegBufferDevice_t buffer);
586
587// retrieve buffer size and pointer - this allows reusing buffer when decode is not needed
588nvjpegStatus_t NVJPEGAPI nvjpegBufferPinnedRetrieve(nvjpegBufferPinned_t buffer, size_t* size, void** ptr);
589
590nvjpegStatus_t NVJPEGAPI nvjpegBufferDeviceRetrieve(nvjpegBufferDevice_t buffer, size_t* size, void** ptr);
591
592// this allows attaching same memory buffers to different states, allowing to switch implementations
593// without allocating extra memory
594nvjpegStatus_t NVJPEGAPI nvjpegStateAttachPinnedBuffer(nvjpegJpegState_t decoder_state,
595    nvjpegBufferPinned_t pinned_buffer);
596
597nvjpegStatus_t NVJPEGAPI nvjpegStateAttachDeviceBuffer(nvjpegJpegState_t decoder_state,
598    nvjpegBufferDevice_t device_buffer);
599
600///////////////////////////////////////////////////////////////////////////////////
601// JPEG stream parameters //
602///////////////////////////////////////////////////////////////////////////////////
603
604// handle that stores stream information - metadata, encoded image parameters, encoded stream parameters
605// stores everything on CPU side. This allows us parse header separately from implementation
606// and retrieve more information on the stream. Also can be used for transcoding and transfering 
607// metadata to encoder
608struct nvjpegJpegStream;
609typedef struct nvjpegJpegStream* nvjpegJpegStream_t;
610
611nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamCreate(
612    nvjpegHandle_t handle, 
613    nvjpegJpegStream_t *jpeg_stream);
614
615nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamDestroy(nvjpegJpegStream_t jpeg_stream);
616
617nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamParse(
618    nvjpegHandle_t handle,
619    const unsigned char *data, 
620    size_t length,
621    int save_metadata,
622    int save_stream,
623    nvjpegJpegStream_t jpeg_stream);
624
625nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamParseHeader(
626    nvjpegHandle_t handle,
627    const unsigned char *data, 
628    size_t length,
629    nvjpegJpegStream_t jpeg_stream);
630
631nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamParseTables(
632    nvjpegHandle_t handle,
633    const unsigned char *data,
634    size_t length,
635    nvjpegJpegStream_t jpeg_stream);
636
637nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamGetJpegEncoding(
638    nvjpegJpegStream_t jpeg_stream,
639    nvjpegJpegEncoding_t* jpeg_encoding);
640
641nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamGetFrameDimensions(
642    nvjpegJpegStream_t jpeg_stream,
643    unsigned int* width,
644    unsigned int* height);
645
646nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamGetComponentsNum(
647    nvjpegJpegStream_t jpeg_stream,
648    unsigned int* components_num);
649
650nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamGetComponentDimensions(
651    nvjpegJpegStream_t jpeg_stream,
652    unsigned int component,
653    unsigned int* width,
654    unsigned int* height);
655
656nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamGetExifOrientation(
657    nvjpegJpegStream_t jpeg_stream,
658    nvjpegExifOrientation_t *orientation_flag);
659
660nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamGetSamplePrecision(
661    nvjpegJpegStream_t jpeg_stream,
662    unsigned int *precision);
663
664// if encoded is 1 color component then it assumes 4:0:0 (NVJPEG_CSS_GRAY, grayscale)
665// if encoded is 3 color components it tries to assign one of the known subsamplings
666//   based on the components subsampling infromation
667// in case sampling factors are not stadard or number of components is different 
668//   it will return NVJPEG_CSS_UNKNOWN
669nvjpegStatus_t NVJPEGAPI nvjpegJpegStreamGetChromaSubsampling(
670    nvjpegJpegStream_t jpeg_stream,
671    nvjpegChromaSubsampling_t* chroma_subsampling);
672
673///////////////////////////////////////////////////////////////////////////////////
674// Decode parameters //
675///////////////////////////////////////////////////////////////////////////////////
676// decode parameters structure. Used to set decode-related tweaks
677struct nvjpegDecodeParams;
678typedef struct nvjpegDecodeParams* nvjpegDecodeParams_t;
679
680nvjpegStatus_t NVJPEGAPI nvjpegDecodeParamsCreate(
681    nvjpegHandle_t handle, 
682    nvjpegDecodeParams_t *decode_params);
683
684nvjpegStatus_t NVJPEGAPI nvjpegDecodeParamsDestroy(nvjpegDecodeParams_t decode_params);
685
686// set output pixel format - same value as in nvjpegDecode()
687nvjpegStatus_t NVJPEGAPI nvjpegDecodeParamsSetOutputFormat(
688    nvjpegDecodeParams_t decode_params,
689    nvjpegOutputFormat_t output_format);
690
691// set to desired ROI. set to (0, 0, -1, -1) to disable ROI decode (decode whole image)
692nvjpegStatus_t NVJPEGAPI nvjpegDecodeParamsSetROI(
693    nvjpegDecodeParams_t decode_params,
694    int offset_x, int offset_y, int roi_width, int roi_height);
695
696// set to true to allow conversion from CMYK to RGB or YUV that follows simple subtractive scheme
697nvjpegStatus_t NVJPEGAPI nvjpegDecodeParamsSetAllowCMYK(
698    nvjpegDecodeParams_t decode_params,
699    int allow_cmyk);
700
701// works only with the hardware decoder backend
702nvjpegStatus_t NVJPEGAPI nvjpegDecodeParamsSetScaleFactor(
703    nvjpegDecodeParams_t decode_params,
704    nvjpegScaleFactor_t scale_factor);
705
706// set the orientation flag to the decode parameters
707nvjpegStatus_t NVJPEGAPI nvjpegDecodeParamsSetExifOrientation(
708    nvjpegDecodeParams_t decode_params,
709    nvjpegExifOrientation_t orientation);
710
711///////////////////////////////////////////////////////////////////////////////////
712// Decoder helper functions //
713///////////////////////////////////////////////////////////////////////////////////
714
715struct nvjpegJpegDecoder;
716typedef struct nvjpegJpegDecoder* nvjpegJpegDecoder_t;
717
718//creates decoder implementation
719nvjpegStatus_t NVJPEGAPI nvjpegDecoderCreate(nvjpegHandle_t nvjpeg_handle, 
720    nvjpegBackend_t implementation, 
721    nvjpegJpegDecoder_t* decoder_handle);
722
723nvjpegStatus_t NVJPEGAPI nvjpegDecoderDestroy(nvjpegJpegDecoder_t decoder_handle);
724
725// on return sets is_supported value to 0 if decoder is capable to handle jpeg_stream 
726// with specified decode parameters
727nvjpegStatus_t NVJPEGAPI nvjpegDecoderJpegSupported(nvjpegJpegDecoder_t decoder_handle, 
728    nvjpegJpegStream_t jpeg_stream,
729    nvjpegDecodeParams_t decode_params,
730    int* is_supported);
731
732nvjpegStatus_t NVJPEGAPI nvjpegDecodeBatchedSupported(nvjpegHandle_t handle,
733    nvjpegJpegStream_t jpeg_stream,
734    int* is_supported);
735
736nvjpegStatus_t NVJPEGAPI nvjpegDecodeBatchedSupportedEx(nvjpegHandle_t handle,
737    nvjpegJpegStream_t jpeg_stream,
738    nvjpegDecodeParams_t decode_params,
739    int* is_supported);
740
741// creates decoder state 
742nvjpegStatus_t NVJPEGAPI nvjpegDecoderStateCreate(nvjpegHandle_t nvjpeg_handle,
743    nvjpegJpegDecoder_t decoder_handle,
744    nvjpegJpegState_t* decoder_state);
745
746///////////////////////////////////////////////////////////////////////////////////
747// Decode functions //
748///////////////////////////////////////////////////////////////////////////////////
749// takes parsed jpeg as input and performs decoding
750nvjpegStatus_t NVJPEGAPI nvjpegDecodeJpeg(
751    nvjpegHandle_t handle,
752    nvjpegJpegDecoder_t decoder,
753    nvjpegJpegState_t decoder_state,
754    nvjpegJpegStream_t jpeg_bitstream,
755    nvjpegImage_t *destination,
756    nvjpegDecodeParams_t decode_params,
757    cudaStream_t stream);
758
759
760// starts decoding on host and save decode parameters to the state
761nvjpegStatus_t NVJPEGAPI nvjpegDecodeJpegHost(
762    nvjpegHandle_t handle,
763    nvjpegJpegDecoder_t decoder,
764    nvjpegJpegState_t decoder_state,
765    nvjpegDecodeParams_t decode_params,
766    nvjpegJpegStream_t jpeg_stream);
767
768// hybrid stage of decoding image,  involves device async calls
769// note that jpeg stream is a parameter here - because we still might need copy 
770// parts of bytestream to device
771nvjpegStatus_t NVJPEGAPI nvjpegDecodeJpegTransferToDevice(
772    nvjpegHandle_t handle,
773    nvjpegJpegDecoder_t decoder,
774    nvjpegJpegState_t decoder_state,
775    nvjpegJpegStream_t jpeg_stream,
776    cudaStream_t stream);
777
778// finishing async operations on the device
779nvjpegStatus_t NVJPEGAPI nvjpegDecodeJpegDevice(
780    nvjpegHandle_t handle,
781    nvjpegJpegDecoder_t decoder,
782    nvjpegJpegState_t decoder_state,
783    nvjpegImage_t *destination,
784    cudaStream_t stream);
785
786
787nvjpegStatus_t NVJPEGAPI nvjpegDecodeBatchedEx(
788          nvjpegHandle_t handle,
789          nvjpegJpegState_t jpeg_handle,
790          const unsigned char *const *data,
791          const size_t *lengths,
792          nvjpegImage_t *destinations,
793          nvjpegDecodeParams_t *decode_params,
794          cudaStream_t stream);
795
796///////////////////////////////////////////////////////////////////////////////////
797// JPEG Transcoding Functions //
798///////////////////////////////////////////////////////////////////////////////////
799
800// copies metadata (JFIF, APP, EXT, COM markers) from parsed stream
801nvjpegStatus_t nvjpegEncoderParamsCopyMetadata(
802	nvjpegEncoderState_t encoder_state,
803    nvjpegEncoderParams_t encode_params,
804    nvjpegJpegStream_t jpeg_stream,
805    cudaStream_t stream);
806
807// copies quantization tables from parsed stream
808nvjpegStatus_t nvjpegEncoderParamsCopyQuantizationTables(
809    nvjpegEncoderParams_t encode_params,
810    nvjpegJpegStream_t jpeg_stream,
811    cudaStream_t stream);
812
813// copies huffman tables from parsed stream. should require same scans structure
814nvjpegStatus_t nvjpegEncoderParamsCopyHuffmanTables(
815    nvjpegEncoderState_t encoder_state,
816    nvjpegEncoderParams_t encode_params,
817    nvjpegJpegStream_t jpeg_stream,
818    cudaStream_t stream);
819
820#if defined(__cplusplus)
821  }
822#endif
823 
824#endif /* NV_JPEG_HEADER */
825 
codekingpro/portable-devtools · Team Ai