resolved terminate_stream into shrink_to_fit and transfer_to_span.

transferal of stream to span not required in assemble_codestream anymore.
assemble_codestream and bwc_compress return the size of the compressed data.
user provided buffer is used.
This commit is contained in:
Gregor Weiss 2024-07-02 17:58:41 +02:00
parent 0673dafb64
commit 1c41147b73
Signed by: Gregor Weiss
GPG key ID: 61E170A8BBFE5756
7 changed files with 76 additions and 82 deletions

View file

@ -117,8 +117,10 @@
//==========|==========================|======================|======|======|===================== //==========|==========================|======================|======|======|=====================
uchar get_bit (bitstream *const stream); uchar get_bit (bitstream *const stream);
//==========|==========================|======================|======|======|===================== //==========|==========================|======================|======|======|=====================
uchar terminate_stream (bitstream *stream, uchar shrink_to_fit (bitstream *const stream);
bwc_span *const packed_stream); //==========|==========================|======================|======|======|=====================
uchar transfer_to_span (bitstream *const stream,
bwc_span *const span);
//==========|==========================|======================|======|======|===================== //==========|==========================|======================|======|======|=====================
void release_packed_stream (bwc_span *const stream); void release_packed_stream (bwc_span *const stream);
#endif #endif

View file

@ -97,7 +97,7 @@
uchar codestream_write_com (bwc_span *const header, uchar codestream_write_com (bwc_span *const header,
bwc_span *const com); bwc_span *const com);
//==========|==========================|======================|======|======|===================== //==========|==========================|======================|======|======|=====================
bwc_span* assemble_codestream (bwc_codec *const codec, size_t assemble_codestream (bwc_codec *const codec,
bwc_stream *const stream); bwc_stream *const stream);
//==========|==========================|======================|======|======|===================== //==========|==========================|======================|======|======|=====================
bwc_codec* parse_codestream (bwc_codec *const codec, bwc_codec* parse_codestream (bwc_codec *const codec,

View file

@ -149,7 +149,7 @@
bwc_stream *const data, bwc_stream *const data,
char *const rate_control); char *const rate_control);
//==========|==========================|======================|======|=======|==================== //==========|==========================|======================|======|=======|====================
uchar bwc_compress (bwc_codec *const field, size_t bwc_compress (bwc_codec *const field,
bwc_stream *const data); bwc_stream *const data);
//==========|==========================|======================|======|=======|==================== //==========|==========================|======================|======|=======|====================
uchar bwc_create_decompression (bwc_codec *const codec, uchar bwc_create_decompression (bwc_codec *const codec,

View file

@ -798,74 +798,74 @@ flush_stream(bitstream *const stream)
} }
/*----------------------------------------------------------------------------------------------------------*\ /*----------------------------------------------------------------------------------------------------------*\
! FUNCTION NAME: void *test(void) !
! -------------- !
! !
! DESCRIPTION: ! ! DESCRIPTION: !
! ------------ ! ! ------------ !
! DESCRIPTION NEEDED ! ! Shrinks the bitstream memory to the actually filled range. !
! !
! PARAMETERS: !
! ----------- !
! Variable Type Description !
! -------- ---- ----------- !
! - - - !
! ! ! !
! RETURN VALUE: ! ! RETURN VALUE: !
! ------------- ! ! ------------- !
! Type Description ! ! Returns 0 if successfull and 1 if memory could not be resized. !
! ---- ----------- !
! - - !
! !
! DEVELOPMENT HISTORY: !
! -------------------- !
! !
! Date Author Change Id Release Description Of Change !
! ---- ------ --------- ------- --------------------- !
! - Patrick Vogler B87D120 V 0.1.0 function created !
! ! ! !
\*----------------------------------------------------------------------------------------------------------*/ \*----------------------------------------------------------------------------------------------------------*/
uchar uchar
terminate_stream(bitstream *stream, bwc_span *const packed_stream) shrink_to_fit(bitstream *const stream)
{ {
/*-----------------------*\ /*-----------------------*\
! DEFINE ASSERTIONS: ! ! DEFINE ASSERTIONS: !
\*-----------------------*/ \*-----------------------*/
assert(stream); assert(stream);
// TODO: this seems dangerous because the bitstream is generated from provided (non-owning) pointer or allocated (owning). if(stream->error)
// If the former is the case a user might get reallocations on his/her pointer.
// This risk is eminent now as bwc_stream data.out is now a user provided buffer pointer.
if(packed_stream)
{ {
if(stream->error) return 1;
}
else if(stream->L > stream->Lmax)
{
stream->Lmax = stream->L;
stream->memory = realloc(stream->memory, stream->Lmax);
if(!stream->memory)
{ {
// memory allocation error
fprintf(stderr, MEMERROR);
stream->Lmax = 0;
return 1; return 1;
} }
else if(stream->L != stream->Lmax) }
{ return 0;
stream->Lmax = stream->L; }
stream->memory = realloc(stream->memory, stream->Lmax);
if(!stream->memory)
{
// memory allocation error
fprintf(stderr, MEMERROR);
stream->Lmax = 0;
return 1;
}
}
packed_stream->memory = stream->memory; /*----------------------------------------------------------------------------------------------------------*\
packed_stream->access = stream->memory; ! DESCRIPTION: !
packed_stream->size = stream->L; ! ------------ !
packed_stream->position = 0; ! Swap memory pointer and size to span. Invalidates stream pointers. !
} ! !
else ! RETURN VALUE: !
! ------------- !
! Returns 0 if successfull and 1 if stream had previous memory error. !
! !
\*----------------------------------------------------------------------------------------------------------*/
uchar
transfer_to_span(bitstream *const stream, bwc_span *const span)
{
/*-----------------------*\
! DEFINE ASSERTIONS: !
\*-----------------------*/
assert(stream);
assert(span);
if(stream->error)
{ {
free(stream->memory); return 1;
} }
free(stream); span->memory = stream->memory;
span->access = stream->memory;
span->size = stream->L;
span->position = 0;
stream->memory = NULL;
stream->L = 0;
return 0; return 0;
} }

View file

@ -1335,12 +1335,13 @@ parse_body(bwc_codec *const field, bitstream *const stream)
! 13.06.2019 Patrick Vogler B87D120 V 0.1.0 function created ! ! 13.06.2019 Patrick Vogler B87D120 V 0.1.0 function created !
! ! ! !
\*----------------------------------------------------------------------------------------------------------*/ \*----------------------------------------------------------------------------------------------------------*/
bwc_span* size_t
assemble_codestream(bwc_codec *const field, bwc_stream *const data) assemble_codestream(bwc_codec *const field, bwc_stream *const data)
{ {
/*-----------------------*\ /*-----------------------*\
! DEFINE INT VARIABLES: ! ! DEFINE INT VARIABLES: !
\*-----------------------*/ \*-----------------------*/
size_t compressed_size;
uint64 i; uint64 i;
/*-----------------------*\ /*-----------------------*\
@ -1349,7 +1350,6 @@ assemble_codestream(bwc_codec *const field, bwc_stream *const data)
bwc_gl_ctrl *control; bwc_gl_ctrl *control;
bwc_tile *tile; bwc_tile *tile;
bitstream *stream; bitstream *stream;
bwc_span *packed_stream;
/*-----------------------*\ /*-----------------------*\
! DEFINE ASSERTIONS: ! ! DEFINE ASSERTIONS: !
@ -1385,7 +1385,7 @@ assemble_codestream(bwc_codec *const field, bwc_stream *const data)
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
if(sequence_packets(field, tile)) if(sequence_packets(field, tile))
{ {
return NULL; return 0;
} }
control->codestreamSize += tile->control.header_size + control->codestreamSize += tile->control.header_size +
tile->control.body_size; tile->control.body_size;
@ -1414,24 +1414,12 @@ assemble_codestream(bwc_codec *const field, bwc_stream *const data)
assemble_tile(field, tile, stream); assemble_tile(field, tile, stream);
} }
emit_symbol(&(*stream), EOC, 2); emit_symbol(stream, EOC, 2);
packed_stream = calloc(1, sizeof(bwc_span)); compressed_size = stream->L;
if(!packed_stream) free(stream);
{
// memory allocation error
fprintf(stderr, MEMERROR);
return NULL;
}
if(terminate_stream(stream, packed_stream)) return compressed_size;
{
return NULL;
}
else
{
return packed_stream;
}
} }
/*----------------------------------------------------------------------------------------------------------*\ /*----------------------------------------------------------------------------------------------------------*\

View file

@ -3652,12 +3652,13 @@ bwc_create_compression(bwc_codec *codec, bwc_stream *stream, char *rate_control)
! 15.03.2018 Patrick Vogler B87D120 V 0.1.0 function created ! ! 15.03.2018 Patrick Vogler B87D120 V 0.1.0 function created !
! ! ! !
\*----------------------------------------------------------------------------------------------------------*/ \*----------------------------------------------------------------------------------------------------------*/
uchar size_t
bwc_compress(bwc_codec *const field, bwc_stream *const data) bwc_compress(bwc_codec *const field, bwc_stream *const data)
{ {
/*-----------------------*\ /*-----------------------*\
! DEFINE INT VARIABLES: ! ! DEFINE INT VARIABLES: !
\*-----------------------*/ \*-----------------------*/
size_t compressed_size = 0;
uint64 buff_size = 0; uint64 buff_size = 0;
uint64 i; uint64 i;
uint16 p; uint16 p;
@ -3731,7 +3732,7 @@ bwc_compress(bwc_codec *const field, bwc_stream *const data)
{ {
// memory allocation error // memory allocation error
fprintf(stderr, MEMERROR); fprintf(stderr, MEMERROR);
return 1; return 0;
} }
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
@ -3812,7 +3813,7 @@ bwc_compress(bwc_codec *const field, bwc_stream *const data)
if(forward_wavelet_transform(field, parameter)) if(forward_wavelet_transform(field, parameter))
{ {
free(working_buffer); free(working_buffer);
return 1; return 0;
} }
#ifdef BWC_PROFILE #ifdef BWC_PROFILE
#if defined (_OPENMP) #if defined (_OPENMP)
@ -3837,7 +3838,7 @@ bwc_compress(bwc_codec *const field, bwc_stream *const data)
if(t1_encode(field, tile, parameter)) if(t1_encode(field, tile, parameter))
{ {
free(working_buffer); free(working_buffer);
return 1; return 0;
} }
#ifdef BWC_PROFILE #ifdef BWC_PROFILE
#if defined (_OPENMP) #if defined (_OPENMP)
@ -3868,7 +3869,7 @@ bwc_compress(bwc_codec *const field, bwc_stream *const data)
if(t2_encode(field, tile)) if(t2_encode(field, tile))
{ {
free(working_buffer); free(working_buffer);
return 1; return 0;
} }
#ifdef BWC_PROFILE #ifdef BWC_PROFILE
#if defined (_OPENMP) #if defined (_OPENMP)
@ -3890,11 +3891,11 @@ bwc_compress(bwc_codec *const field, bwc_stream *const data)
start = (double)clock(); start = (double)clock();
#endif #endif
#endif #endif
data->codestream.data = assemble_codestream(field, data); compressed_size = assemble_codestream(field, data);
if(!data->codestream.data) if(compressed_size == 0)
{ {
free(working_buffer); free(working_buffer);
return 1; return 0;
} }
#ifdef BWC_PROFILE #ifdef BWC_PROFILE
#if defined (_OPENMP) #if defined (_OPENMP)
@ -3922,7 +3923,7 @@ bwc_compress(bwc_codec *const field, bwc_stream *const data)
#endif #endif
nfs = (uint64)(info->nX * info->nY * info->nZ * info->nTS * info->nPar * info->data_prec); nfs = (uint64)(info->nX * info->nY * info->nZ * info->nTS * info->nPar * info->data_prec);
css = (uint64)data->codestream.data->size; css = (uint64)compressed_size;
cpr = (double)nfs/css; cpr = (double)nfs/css;
@ -3944,7 +3945,7 @@ bwc_compress(bwc_codec *const field, bwc_stream *const data)
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! Return to the function caller. ! ! Return to the function caller. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
return 0; return compressed_size;
} }
/*----------------------------------------------------------------------------------------------------------*\ /*----------------------------------------------------------------------------------------------------------*\

View file

@ -652,7 +652,8 @@ create_packet(bwc_codec *const field, bwc_tile *const tile,
} }
k = header->L; k = header->L;
terminate_stream(header, NULL); free(header->memory);
free(header);
return k; return k;
} }
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
@ -663,11 +664,13 @@ create_packet(bwc_codec *const field, bwc_tile *const tile,
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
else else
{ {
if(terminate_stream(header, &packet->header)) if(shrink_to_fit(header))
{ {
// memory allocation error // memory allocation error
return -1; return -1;
} }
transfer_to_span(header, &packet->header);
free(header);
packet->size = packet->body.size + packet->header.size; packet->size = packet->body.size + packet->header.size;