enc_fast_lossless.h (4619B)
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 #ifndef LIB_JXL_ENC_FAST_LOSSLESS_H_ 7 #define LIB_JXL_ENC_FAST_LOSSLESS_H_ 8 #include <stdlib.h> 9 10 // FJXL_STANDALONE=1 for a stand-alone jxl encoder 11 // FJXL_STANDALONE=0 for use in libjxl to encode frames (but no image header) 12 #ifndef FJXL_STANDALONE 13 #define FJXL_STANDALONE 0 14 #endif 15 16 #if !FJXL_STANDALONE 17 #include <jxl/encode.h> 18 #endif 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 #if FJXL_STANDALONE 25 // Simplified version of the streaming input source from jxl/encode.h 26 // We only need this part to wrap the full image buffer in the standalone mode 27 // and this way we don't need to depend on the jxl headers. 28 struct JxlChunkedFrameInputSource { 29 void* opaque; 30 const void* (*get_color_channel_data_at)(void* opaque, size_t xpos, 31 size_t ypos, size_t xsize, 32 size_t ysize, size_t* row_offset); 33 void (*release_buffer)(void* opaque, const void* buf); 34 }; 35 // The standalone version does not use this struct, but we define it here so 36 // that we don't have to clutter all the function signatures with defines. 37 struct JxlEncoderOutputProcessorWrapper { 38 int unused; 39 }; 40 #endif 41 42 // Simple encoding API. 43 44 // A FJxlParallelRunner must call fun(opaque, i) for all i from 0 to count. It 45 // may do so in parallel. 46 typedef void(FJxlParallelRunner)(void* runner_opaque, void* opaque, 47 void fun(void*, size_t), size_t count); 48 49 #if FJXL_STANDALONE 50 // You may pass `nullptr` as a runner: encoding will be sequential. 51 size_t JxlFastLosslessEncode(const unsigned char* rgba, size_t width, 52 size_t row_stride, size_t height, size_t nb_chans, 53 size_t bitdepth, int big_endian, int effort, 54 unsigned char** output, void* runner_opaque, 55 FJxlParallelRunner runner); 56 #endif 57 58 // More complex API for cases in which you may want to allocate your own buffer 59 // and other advanced use cases. 60 61 // Opaque struct that represents an intermediate state of the computation. 62 struct JxlFastLosslessFrameState; 63 64 // Returned JxlFastLosslessFrameState must be freed by calling 65 // JxlFastLosslessFreeFrameState. 66 JxlFastLosslessFrameState* JxlFastLosslessPrepareFrame( 67 JxlChunkedFrameInputSource input, size_t width, size_t height, 68 size_t nb_chans, size_t bitdepth, int big_endian, int effort, int oneshot); 69 70 #if !FJXL_STANDALONE 71 class JxlEncoderOutputProcessorWrapper; 72 #endif 73 74 void JxlFastLosslessProcessFrame( 75 JxlFastLosslessFrameState* frame_state, bool is_last, void* runner_opaque, 76 FJxlParallelRunner runner, 77 JxlEncoderOutputProcessorWrapper* output_processor); 78 79 // Prepare the (image/frame) header. You may encode animations by concatenating 80 // the output of multiple frames, of which the first one has add_image_header = 81 // 1 and subsequent ones have add_image_header = 0, and all frames but the last 82 // one have is_last = 0. 83 // (when FJXL_STANDALONE=0, add_image_header has to be 0) 84 void JxlFastLosslessPrepareHeader(JxlFastLosslessFrameState* frame, 85 int add_image_header, int is_last); 86 87 // Upper bound on the required output size, including any padding that may be 88 // required by JxlFastLosslessWriteOutput. Cannot be called before 89 // JxlFastLosslessPrepareHeader. 90 size_t JxlFastLosslessMaxRequiredOutput(const JxlFastLosslessFrameState* frame); 91 92 // Actual size of the frame once it is encoded. This is not identical to 93 // JxlFastLosslessMaxRequiredOutput because JxlFastLosslessWriteOutput may 94 // require extra padding. 95 size_t JxlFastLosslessOutputSize(const JxlFastLosslessFrameState* frame); 96 97 // Writes the frame to the given output buffer. Returns the number of bytes that 98 // were written, which is at least 1 unless the entire output has been written 99 // already. It is required that `output_size >= 32` when calling this function. 100 // This function must be called repeatedly until it returns 0. 101 size_t JxlFastLosslessWriteOutput(JxlFastLosslessFrameState* frame, 102 unsigned char* output, size_t output_size); 103 104 // Frees the provided frame state. 105 void JxlFastLosslessFreeFrameState(JxlFastLosslessFrameState* frame); 106 107 #ifdef __cplusplus 108 } // extern "C" 109 #endif 110 111 #if !FJXL_STANDALONE 112 void JxlFastLosslessOutputFrame( 113 JxlFastLosslessFrameState* frame_state, 114 JxlEncoderOutputProcessorWrapper* output_process); 115 #endif 116 117 #endif // LIB_JXL_ENC_FAST_LOSSLESS_H_