LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
Data Structures | Macros | Functions | Variables

Implement predictor-aware ZSTD frame streaming. More...

Collaboration diagram for ZSTD streaming codec:

Data Structures

struct  ZSTDState
 Own ZSTD streams predictor prefix and output cursor. More...
 

Macros

#define LSTATE_INIT_DECODE   0x01
 Bit 0x01 of the codec state field: decoding has been set up.
 
#define LSTATE_INIT_ENCODE   0x02
 Bit 0x02 of the codec state field: encoding has been set up.
 
#define GetZSTDState(tif)   ((ZSTDState *)(tif)->tif_data)
 Cast tif->tif_data to the ZSTDState.
 
#define ZSTDDecoderState(tif)   GetZSTDState(tif)
 Alias of GetZSTDState(), returning the ZSTDState for decoding.
 
#define ZSTDEncoderState(tif)   GetZSTDState(tif)
 Alias of GetZSTDState(), returning the ZSTDState for encoding.
 

Functions

static int ZSTDFixupTags (TIFF *tif)
 Accept ZSTD tag fixup without changes.
 
static int ZSTDSetupDecode (TIFF *tif)
 Switch ZSTD state into decode mode.
 
static int ZSTDPreDecode (TIFF *tif, uint16_t s)
 Create or reset the ZSTD decompression stream for a strile.
 
static int ZSTDDecode (TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
 Decompress cached ZSTD input into caller output.
 
static int ZSTDSetupEncode (TIFF *tif)
 Switch ZSTD state into encode mode.
 
static int ZSTDPreEncode (TIFF *tif, uint16_t s)
 Create and initialise a ZSTD compression stream for a strile.
 
static int ZSTDEncode (TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
 Compress input through the active ZSTD stream.
 
static int ZSTDPostEncode (TIFF *tif)
 Finish the ZSTD frame and flush its remaining output.
 
static void ZSTDCleanup (TIFF *tif)
 Release ZSTD streams state and chained codec methods.
 
static int ZSTDVSetField (TIFF *tif, uint32_t tag, va_list ap)
 Store ZSTD compression level or delegate another tag assignment.
 
static int ZSTDVGetField (TIFF *tif, uint32_t tag, va_list ap)
 Retrieve ZSTD compression level or delegate another tag query.
 
static uint64_t ZSTDGetMaxCompressionRatio (TIFF *tif)
 Report the ZSTD expansion allocation heuristic.
 
int TIFFInitZSTD (TIFF *tif, int scheme)
 Allocate ZSTD state and install predictor-aware streaming methods.
 

Variables

static const TIFFField ZSTDFields []
 Define the ZSTD compression-level pseudo-tag.
 

Detailed Description

Implement predictor-aware ZSTD frame streaming.

Macro Definition Documentation

◆ GetZSTDState

#define GetZSTDState (   tif)    ((ZSTDState *)(tif)->tif_data)

Cast tif->tif_data to the ZSTDState.

tif is evaluated once. Requires the codec to be initialised (tif_data non-NULL).

Parameters
[in]tifHandle whose codec state is returned.

Definition at line 89 of file tif_zstd.c.

◆ LSTATE_INIT_DECODE

#define LSTATE_INIT_DECODE   0x01

Bit 0x01 of the codec state field: decoding has been set up.

Set by ZSTDSetupDecode(). Each setup first tears down the other mode, if active, and clears the field.

Definition at line 80 of file tif_zstd.c.

◆ LSTATE_INIT_ENCODE

#define LSTATE_INIT_ENCODE   0x02

Bit 0x02 of the codec state field: encoding has been set up.

Set by ZSTDSetupEncode(). Each setup first tears down the other mode, if active, and clears the field.

Definition at line 81 of file tif_zstd.c.

◆ ZSTDDecoderState

#define ZSTDDecoderState (   tif)    GetZSTDState(tif)

Alias of GetZSTDState(), returning the ZSTDState for decoding.

tif is evaluated once. Requires the codec to be initialised.

Parameters
[in]tifHandle whose codec state is returned.

Definition at line 90 of file tif_zstd.c.

◆ ZSTDEncoderState

#define ZSTDEncoderState (   tif)    GetZSTDState(tif)

Alias of GetZSTDState(), returning the ZSTDState for encoding.

tif is evaluated once. Requires the codec to be initialised.

Parameters
[in]tifHandle whose codec state is returned.

Definition at line 91 of file tif_zstd.c.

Function Documentation

◆ TIFFInitZSTD()

int TIFFInitZSTD ( TIFF *  tif,
int  scheme 
)
extern

Allocate ZSTD state and install predictor-aware streaming methods.

Requires cleaned-up previous codec state and COMPRESSION_ZSTD. Merges pseudo-tag metadata, allocates handle-owned state, saves tag hooks, defaults level to nine, and installs stream/cleanup methods. Attempts PredictorInit without propagating its status. Field/allocation failure diagnoses; merged metadata or other changes are not rolled back. Dependency streams are created lazily. No pixel I/O.

Parameters
[in,out]tifNon-NULL handle whose previous codec state has been cleaned up; receives the new codec state. Serialise operations.
[in]schemeCOMPRESSION_ZSTD, asserted in debug builds.
Returns
One after state/method installation, zero on field merge or outer allocation failure.

Definition at line 603 of file tif_zstd.c.

References _TIFFmallocExt(), _TIFFMergeFields(), COMPRESSION_ZSTD, GetZSTDState, tiff::tif_cleanup, tiff::tif_data, tiff::tif_decoderow, tiff::tif_decodestrip, tiff::tif_decodetile, tiff::tif_encoderow, tiff::tif_encodestrip, tiff::tif_encodetile, tiff::tif_fixuptags, tiff::tif_getmaxcompressionratio, tiff::tif_postencode, tiff::tif_predecode, tiff::tif_preencode, tiff::tif_setupdecode, tiff::tif_setupencode, tiff::tif_tagmethods, TIFFArrayCount, TIFFErrorExtR(), TIFFPredictorInit(), TIFFTagMethods::vgetfield, TIFFTagMethods::vsetfield, ZSTDCleanup(), ZSTDDecode(), ZSTDEncode(), ZSTDFields, ZSTDFixupTags(), ZSTDGetMaxCompressionRatio(), ZSTDPostEncode(), ZSTDPreDecode(), ZSTDPreEncode(), ZSTDSetupDecode(), ZSTDSetupEncode(), ZSTDVGetField(), and ZSTDVSetField().

Here is the call graph for this function:

◆ ZSTDCleanup()

static void ZSTDCleanup ( TIFF *  tif)
static

Release ZSTD streams state and chained codec methods.

Restores predictor and saved tag hooks, frees dependency streams with their matching ZSTD free functions, frees outer Ext state, clears tif_data and installs default callbacks. Invalidates state/stream views. Does not finish frames, flush payload, close the handle or return a status.

Parameters
[in,out]tifNon-NULL live ZSTD handle with valid predictor-prefixed state; serialise operations.

Definition at line 475 of file tif_zstd.c.

References _TIFFfreeExt(), _TIFFSetDefaultCompressionState(), ZSTDState::cstream, ZSTDState::dstream, GetZSTDState, tiff::tif_data, tiff::tif_tagmethods, TIFFPredictorCleanup(), TIFFTagMethods::vgetfield, ZSTDState::vgetparent, TIFFTagMethods::vsetfield, and ZSTDState::vsetparent.

Referenced by TIFFInitZSTD().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ZSTDDecode()

static int ZSTDDecode ( TIFF *  tif,
uint8_t *  op,
tmsize_t  occ,
uint16_t  s 
)
static

Decompress cached ZSTD input into caller output.

Requires decode mode and initialised DStream. Repeatedly decompresses until output fills, frame ends or input exhausts. On dependency error or short output zeroes the unfilled tail and diagnoses, without committing local raw cursor consumption; stream/output may already change. On success advances rawcp and reduces rawcc. Does not require consuming a complete frame when the requested output fills. No allocation owned by caller, client I/O or ownership transfer.

Parameters
[in,out]tifNon-NULL live ZSTD handle with valid predictor-prefixed state; serialise operations.
[out]opBorrowed writable occ-byte decoded output, disjoint from raw input/state.
[in]occNonnegative decoded byte extent representable by size_t.
[in]sUnused sample-plane argument.
Returns
One when the requested output is filled, zero on dependency/short-output failure.

Definition at line 212 of file tif_zstd.c.

References LSTATE_INIT_DECODE, TIFFDirectory::td_row, tiff::tif_dir, tiff::tif_rawcc, tiff::tif_rawcp, TIFFErrorExtR(), and ZSTDDecoderState.

Referenced by TIFFInitZSTD().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ZSTDEncode()

static int ZSTDEncode ( TIFF *  tif,
uint8_t *  bp,
tmsize_t  cc,
uint16_t  s 
)
static

Compress input through the active ZSTD stream.

Requires successful preencode and live positive-capacity raw storage; a state not initialised for encoding is diagnosed and rejected. Consumes borrowed input synchronously, flushing full output buffers via TIFFFlushData1. Dependency/flush failures diagnose where applicable and leave partial stream, cache and file progress. Input bytes are not changed; no ownership transfer or rollback.

Parameters
[in,out]tifNon-NULL live ZSTD handle with valid predictor-prefixed state; serialise operations.
[in]bpBorrowed readable cc-byte input, not retained or modified.
[in]ccNonnegative input byte extent representable by size_t.
[in]sUnused sample-plane argument.
Returns
One when input has been consumed, zero on wrong codec state or dependency/flush failure.

Definition at line 366 of file tif_zstd.c.

References LSTATE_INIT_ENCODE, tiff::tif_rawcc, tiff::tif_rawcp, tiff::tif_rawdatasize, TIFFErrorExtR(), TIFFFlushData1(), and ZSTDEncoderState.

Referenced by TIFFInitZSTD().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ZSTDFixupTags()

static int ZSTDFixupTags ( TIFF *  tif)
static

Accept ZSTD tag fixup without changes.

No allocation, I/O, diagnostics, state or ownership effects.

Parameters
[in]tifUnused handle.
Returns
One unconditionally.

Definition at line 105 of file tif_zstd.c.

Referenced by TIFFInitZSTD().

Here is the caller graph for this function:

◆ ZSTDGetMaxCompressionRatio()

static uint64_t ZSTDGetMaxCompressionRatio ( TIFF *  tif)
static

Report the ZSTD expansion allocation heuristic.

Returns a fixed estimate without handle inspection, I/O, diagnostics, allocation or state changes; not a universal bound.

Parameters
[in]tifUnused handle.
Returns
Thirty-three thousand.

Definition at line 595 of file tif_zstd.c.

Referenced by TIFFInitZSTD().

Here is the caller graph for this function:

◆ ZSTDPostEncode()

static int ZSTDPostEncode ( TIFF *  tif)
static

Finish the ZSTD frame and flush its remaining output.

Repeatedly ends the active stream and flushes produced bytes. Dependency or I/O failure can leave an incomplete frame and changed state/file bytes. Requires successful preencode and valid raw capacity; a state not initialised for encoding is diagnosed and rejected. Does not write the IFD or promise durability; no ownership transfer.

Parameters
[in,out]tifNon-NULL live ZSTD handle with valid predictor-prefixed state; serialise operations.
Returns
One after frame completion and buffer flushes, zero on wrong codec state or dependency/I/O failure.

Definition at line 428 of file tif_zstd.c.

References LSTATE_INIT_ENCODE, tiff::tif_rawcc, tiff::tif_rawcp, TIFFErrorExtR(), TIFFFlushData1(), and ZSTDEncoderState.

Referenced by TIFFInitZSTD().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ZSTDPreDecode()

static int ZSTDPreDecode ( TIFF *  tif,
uint16_t  s 
)
static

Create or reset the ZSTD decompression stream for a strile.

Ensures decode setup, lazily creates a dependency-owned DStream and resets it. Stream remains codec-owned for cleanup even after init failure. Diagnoses dependency allocation/init errors; no pixel reads or caller storage ownership transfer.

Parameters
[in,out]tifNon-NULL live ZSTD handle with valid predictor-prefixed state; serialise operations.
[in]sUnused sample-plane argument.
Returns
One after reset, zero on dependency allocation/init failure.

Definition at line 158 of file tif_zstd.c.

References LSTATE_INIT_DECODE, tiff::tif_setupdecode, TIFFErrorExtR(), and ZSTDDecoderState.

Referenced by TIFFInitZSTD().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ZSTDPreEncode()

static int ZSTDPreEncode ( TIFF *  tif,
uint16_t  s 
)
static

Create and initialise a ZSTD compression stream for a strile.

Ensures encode mode, lazily creates CStream, initialises it at the cached compression level and attaches the raw output buffer. Requires positive usable raw capacity. Dependency stream remains codec-owned on later failure; diagnoses dependency allocation/init errors. No payload flush or input ownership transfer.

Parameters
[in,out]tifNon-NULL live ZSTD handle with valid predictor-prefixed state; serialise operations.
[in]sUnused sample-plane argument.
Returns
One after initialisation, zero on dependency allocation/init failure.

Definition at line 308 of file tif_zstd.c.

References LSTATE_INIT_ENCODE, tiff::tif_rawdata, tiff::tif_rawdatasize, tiff::tif_setupencode, TIFFErrorExtR(), and ZSTDEncoderState.

Referenced by TIFFInitZSTD().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ZSTDSetupDecode()

static int ZSTDSetupDecode ( TIFF *  tif)
static

Switch ZSTD state into decode mode.

Frees any previous compression stream and clears its pointer, then sets decode flags. Invalidates compression-stream views; no raw/pixel I/O, diagnostics or output allocation. Requires initialised state.

Parameters
[in,out]tifNon-NULL live ZSTD handle with valid predictor-prefixed state; serialise operations.
Returns
One unconditionally.

Definition at line 123 of file tif_zstd.c.

References ZSTDState::cstream, LSTATE_INIT_DECODE, LSTATE_INIT_ENCODE, ZSTDState::state, and ZSTDDecoderState.

Referenced by TIFFInitZSTD().

Here is the caller graph for this function:

◆ ZSTDSetupEncode()

static int ZSTDSetupEncode ( TIFF *  tif)
static

Switch ZSTD state into encode mode.

Frees any previous decompression stream and clears its pointer, then sets encode flags. No pixel I/O or caller-owned output allocation; dependency views are invalidated.

Parameters
[in,out]tifNon-NULL live ZSTD handle with valid predictor-prefixed state; serialise operations.
Returns
One unconditionally.

Definition at line 273 of file tif_zstd.c.

References ZSTDState::dstream, LSTATE_INIT_DECODE, LSTATE_INIT_ENCODE, ZSTDState::state, and ZSTDEncoderState.

Referenced by TIFFInitZSTD().

Here is the caller graph for this function:

◆ ZSTDVGetField()

static int ZSTDVGetField ( TIFF *  tif,
uint32_t  tag,
va_list  ap 
)
static

Retrieve ZSTD compression level or delegate another tag query.

TIFFTAG_ZSTD_LEVEL consumes a non-NULL int* and writes the cached level. Other tags follow parent variadic contracts. No allocation or ownership transfer for the level.

Parameters
[in,out]tifNon-NULL live ZSTD handle with valid predictor-prefixed state; serialise operations.
[in]tagPseudo-tag or delegated tag identifier.
[in,out]apLive tag-specific va_list consumed synchronously; not retained.
Returns
One for level retrieval, otherwise parent status.

Definition at line 558 of file tif_zstd.c.

References ZSTDState::compression_level, GetZSTDState, TIFFTAG_ZSTD_LEVEL, and ZSTDState::vgetparent.

Referenced by TIFFInitZSTD().

Here is the caller graph for this function:

◆ ZSTDVSetField()

static int ZSTDVSetField ( TIFF *  tif,
uint32_t  tag,
va_list  ap 
)
static

Store ZSTD compression level or delegate another tag assignment.

TIFFTAG_ZSTD_LEVEL consumes int, stores it even outside 1..ZSTD_maxCLevel(), warns for that range and returns success. Stream reinitialisation applies it later. Other tags follow parent variadic contracts; no ownership transfer for the level. Callbacks/diagnostics can mutate state.

Parameters
[in,out]tifNon-NULL live ZSTD handle with valid predictor-prefixed state; serialise operations.
[in]tagPseudo-tag or delegated tag identifier.
[in,out]apLive tag-specific va_list consumed synchronously; not retained.
Returns
One for level assignment including warned values, otherwise parent status.

Definition at line 520 of file tif_zstd.c.

References GetZSTDState, TIFFTAG_ZSTD_LEVEL, and TIFFWarningExtR().

Referenced by TIFFInitZSTD().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ ZSTDFields

const TIFFField ZSTDFields[]
static
Initial value:
= {
FALSE, "ZSTD compression_level", NULL},
}
#define FIELD_PSEUDO
Share bit zero with FIELD_IGNORE for pseudo-tags.
Definition tif_dir.h:620
@ TIFF_SETGET_INT
Promoted int scalar representation.
Definition tif_dir.h:762
#define TRUE
Represent the legacy internal true value.
Definition tiffiop.h:131
#define FALSE
Represent the legacy internal false value.
Definition tiffiop.h:136
#define TIFF_ANY
Match any data type when searching for a field descriptor.
Definition tiffio.h:1289
#define TIFFTAG_ZSTD_LEVEL
Pseudo-tag: ZSTD compression level.
Definition tiff.h:1641

Define the ZSTD compression-level pseudo-tag.

Immutable library-lifetime metadata merged into a handle; no caller ownership transfer.

Definition at line 580 of file tif_zstd.c.

Referenced by TIFFInitZSTD().