summaryrefslogtreecommitdiffstats
path: root/encoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'encoder.c')
-rw-r--r--encoder.c73
1 files changed, 40 insertions, 33 deletions
diff --git a/encoder.c b/encoder.c
index a46893a..bcbdf6a 100644
--- a/encoder.c
+++ b/encoder.c
@@ -50,7 +50,8 @@ void Mf_init( struct Matchfinder * const matchfinder,
matchfinder->stream_pos = 0;
matchfinder->infd_ = infd;
matchfinder->match_len_limit_ = len_limit;
- matchfinder->prev_positions = (int32_t *)malloc( mf_num_prev_positions * sizeof (int32_t) );
+ matchfinder->prev_positions =
+ (int32_t *)malloc( mf_num_prev_positions * sizeof (int32_t) );
if( !matchfinder->prev_positions )
{
show_error( "not enough memory. Try a smaller dictionary size", 0, false );
@@ -71,7 +72,8 @@ void Mf_init( struct Matchfinder * const matchfinder,
if( !matchfinder->at_stream_end && matchfinder->buffer_size < buffer_size_limit )
{
matchfinder->buffer_size = buffer_size_limit;
- matchfinder->buffer = (uint8_t *)realloc( matchfinder->buffer, matchfinder->buffer_size * sizeof (uint8_t) );
+ matchfinder->buffer =
+ (uint8_t *)realloc( matchfinder->buffer, matchfinder->buffer_size );
if( !matchfinder->buffer )
{
show_error( "not enough memory. Try a smaller dictionary size", 0, false );
@@ -85,7 +87,8 @@ void Mf_init( struct Matchfinder * const matchfinder,
else matchfinder->dictionary_size_ = dict_size;
matchfinder->pos_limit = matchfinder->buffer_size;
if( !matchfinder->at_stream_end ) matchfinder->pos_limit -= mf_after_size;
- matchfinder->prev_pos_tree = (int32_t *)malloc( 2 * matchfinder->dictionary_size_ * sizeof (int32_t) );
+ matchfinder->prev_pos_tree =
+ (int32_t *)malloc( 2 * matchfinder->dictionary_size_ * sizeof (int32_t) );
if( !matchfinder->prev_pos_tree )
{
show_error( "not enough memory. Try a smaller dictionary size", 0, false );
@@ -153,10 +156,11 @@ int Mf_longest_match_len( struct Matchfinder * const matchfinder,
const uint8_t * const data = matchfinder->buffer + matchfinder->pos;
const int key2 = mf_num_prev_positions4 + mf_num_prev_positions3 +
( ( (int)data[0] << 8 ) | data[1] );
- const int tmp = crc32[data[0]] ^ data[1] ^ ( (int)data[2] << 8 );
- const int key3 = mf_num_prev_positions4 + ( tmp & ( mf_num_prev_positions3 - 1 ) );
- const int key4 = ( tmp ^ ( crc32[data[3]] << 5 ) ) &
- ( mf_num_prev_positions4 - 1 );
+ const uint32_t tmp = crc32[data[0]] ^ data[1] ^ ( (uint32_t)data[2] << 8 );
+ const int key3 = mf_num_prev_positions4 +
+ (int)( tmp & ( mf_num_prev_positions3 - 1 ) );
+ const int key4 = (int)( ( tmp ^ ( crc32[data[3]] << 5 ) ) &
+ ( mf_num_prev_positions4 - 1 ) );
if( distances )
{
@@ -292,9 +296,11 @@ void LZe_fill_distance_prices( struct LZ_encoder * const encoder )
}
-// Return value: ( dis == -1 ) && ( len == 1 ) means literal
-int LZe_best_pair_sequence( struct LZ_encoder * const encoder, const int reps[num_rep_distances],
- const State state )
+// Return value == number of bytes advanced (ahead).
+// trials[0]..trials[retval-1] contain the steps to encode.
+// ( trials[0].dis == -1 && trials[0].price == 1 ) means literal.
+int LZe_sequence_optimizer( struct LZ_encoder * const encoder,
+ const int reps[num_rep_distances], const State state )
{
int main_len;
if( encoder->longest_match_found > 0 ) // from previous call
@@ -328,15 +334,14 @@ int LZe_best_pair_sequence( struct LZ_encoder * const encoder, const int reps[nu
return main_len;
}
- encoder->trials[0].state = state;
- for( int i = 0; i < num_rep_distances; ++i ) encoder->trials[0].reps[i] = reps[i];
-
+ {
+ const int pos_state = Mf_data_position( encoder->matchfinder ) & pos_state_mask;
const uint8_t prev_byte = Mf_peek( encoder->matchfinder, -1 );
const uint8_t cur_byte = Mf_peek( encoder->matchfinder, 0 );
const uint8_t match_byte = Mf_peek( encoder->matchfinder, -reps[0]-1 );
- unsigned int position = Mf_data_position( encoder->matchfinder );
- const int pos_state = position & pos_state_mask;
+ encoder->trials[0].state = state;
+ for( int i = 0; i < num_rep_distances; ++i ) encoder->trials[0].reps[i] = reps[i];
encoder->trials[1].dis = -1;
encoder->trials[1].prev_index = 0;
encoder->trials[1].price = price0( encoder->bm_match[state][pos_state] );
@@ -385,6 +390,7 @@ int LZe_best_pair_sequence( struct LZ_encoder * const encoder, const int reps[nu
Tr_update( &encoder->trials[len], rep, 0, price +
Lee_price( &encoder->rep_match_len_encoder, len, pos_state ) );
}
+ }
int cur = 0;
int num_trials = main_len;
@@ -392,7 +398,7 @@ int LZe_best_pair_sequence( struct LZ_encoder * const encoder, const int reps[nu
while( true )
{
- if( ++cur >= num_trials )
+ if( ++cur >= num_trials ) // no more initialized trials
{
LZe_backward( encoder, cur );
return cur;
@@ -424,10 +430,11 @@ int LZe_best_pair_sequence( struct LZ_encoder * const encoder, const int reps[nu
LZe_mtf_reps( cur_trial->dis, cur_trial->reps );
}
+ const int pos_state = Mf_data_position( encoder->matchfinder ) & pos_state_mask;
const uint8_t prev_byte = Mf_peek( encoder->matchfinder, -1 );
const uint8_t cur_byte = Mf_peek( encoder->matchfinder, 0 );
const uint8_t match_byte = Mf_peek( encoder->matchfinder, -cur_trial->reps[0]-1 );
- const int pos_state = ++position & pos_state_mask;
+
int next_price = cur_trial->price + price0( encoder->bm_match[cur_trial->state][pos_state] );
if( St_is_char( cur_trial->state ) )
next_price += Lie_price_symbol( &encoder->literal_encoder, prev_byte, cur_byte );
@@ -472,7 +479,7 @@ int LZe_best_pair_sequence( struct LZ_encoder * const encoder, const int reps[nu
if( newlen <= len_limit &&
( newlen > min_match_len ||
( newlen == min_match_len &&
- encoder->match_distances[newlen] < modeled_distances ) ) )
+ encoder->match_distances[min_match_len] < modeled_distances ) ) )
{
const int normal_match_price = match_price +
price0( encoder->bm_rep[cur_trial->state] );
@@ -488,19 +495,19 @@ int LZe_best_pair_sequence( struct LZ_encoder * const encoder, const int reps[nu
}
- // End Of Stream mark => (dis == 0xFFFFFFFF, len == min_match_len)
+ // End Of Stream mark => (dis == 0xFFFFFFFFU, len == min_match_len)
void LZe_full_flush( struct LZ_encoder * const encoder, const State state )
{
- const int pos_state = ( Mf_data_position( encoder->matchfinder ) ) & pos_state_mask;
+ const int pos_state = Mf_data_position( encoder->matchfinder ) & pos_state_mask;
Re_encode_bit( &encoder->range_encoder, &encoder->bm_match[state][pos_state], 1 );
Re_encode_bit( &encoder->range_encoder, &encoder->bm_rep[state], 0 );
- LZe_encode_pair( encoder, 0xFFFFFFFF, min_match_len, pos_state );
+ LZe_encode_pair( encoder, 0xFFFFFFFFU, min_match_len, pos_state );
Re_flush( &encoder->range_encoder );
File_trailer trailer;
Ft_set_data_crc( trailer, LZe_crc( encoder ) );
Ft_set_data_size( trailer, Mf_data_position( encoder->matchfinder ) );
- Ft_set_member_size( trailer, LZe_member_position( encoder ) + sizeof (File_trailer) );
- for( unsigned int i = 0; i < sizeof (File_trailer); ++i )
+ Ft_set_member_size( trailer, LZe_member_position( encoder ) + Ft_size );
+ for( int i = 0; i < Ft_size; ++i )
Re_put_byte( &encoder->range_encoder, trailer[i] );
Re_flush_data( &encoder->range_encoder );
}
@@ -510,7 +517,7 @@ void LZe_init( struct LZ_encoder * const encoder, struct Matchfinder * const mf,
const File_header header, const int outfd )
{
encoder->longest_match_found = 0;
- encoder->crc_ = 0xFFFFFFFF;
+ encoder->crc_ = 0xFFFFFFFFU;
for( int i = 0; i < St_states; ++i )
{
@@ -541,17 +548,17 @@ void LZe_init( struct LZ_encoder * const encoder, struct Matchfinder * const mf,
LZe_fill_align_prices( encoder );
- for( unsigned int i = 0; i < sizeof (File_header); ++i )
+ for( int i = 0; i < Fh_size; ++i )
Re_put_byte( &encoder->range_encoder, header[i] );
}
bool LZe_encode_member( struct LZ_encoder * const encoder, const long long member_size )
{
- if( LZe_member_position( encoder ) != sizeof (File_header) )
+ if( LZe_member_position( encoder ) != Fh_size )
return false; // can be called only once
const long long member_size_limit =
- member_size - sizeof (File_trailer) - lze_max_marker_size;
+ member_size - Ft_size - lze_max_marker_size;
int fill_counter = 0;
int rep_distances[num_rep_distances];
State state = 0;
@@ -576,22 +583,23 @@ bool LZe_encode_member( struct LZ_encoder * const encoder, const long long membe
if( fill_counter <= 0 )
{ LZe_fill_distance_prices( encoder ); fill_counter = 512; }
- int ahead = LZe_best_pair_sequence( encoder, rep_distances, state );
+ int ahead = LZe_sequence_optimizer( encoder, rep_distances, state );
if( ahead <= 0 ) return false;
fill_counter -= ahead;
for( int i = 0; ; )
{
const int pos_state = ( Mf_data_position( encoder->matchfinder ) - ahead ) & pos_state_mask;
- int dis = encoder->trials[i].dis;
+ const int dis = encoder->trials[i].dis;
const int len = encoder->trials[i].price;
bool bit = ( dis < 0 && len == 1 );
Re_encode_bit( &encoder->range_encoder, &encoder->bm_match[state][pos_state], !bit );
- if( bit )
+ if( bit ) // literal byte
{
const uint8_t prev_byte = Mf_peek( encoder->matchfinder, -ahead-1 );
const uint8_t cur_byte = Mf_peek( encoder->matchfinder, -ahead );
+ CRC32_update_byte( &encoder->crc_, cur_byte );
if( St_is_char( state ) )
Lie_encode( &encoder->literal_encoder, &encoder->range_encoder, prev_byte, cur_byte );
else
@@ -601,8 +609,9 @@ bool LZe_encode_member( struct LZ_encoder * const encoder, const long long membe
}
St_set_char( &state );
}
- else
+ else // match or repeated match
{
+ CRC32_update_buf( &encoder->crc_, Mf_ptr_to_current_pos( encoder->matchfinder ) - ahead, len );
LZe_mtf_reps( dis, rep_distances );
bit = ( dis < num_rep_distances );
Re_encode_bit( &encoder->range_encoder, &encoder->bm_rep[state], bit );
@@ -631,8 +640,6 @@ bool LZe_encode_member( struct LZ_encoder * const encoder, const long long membe
St_set_match( &state );
}
}
- for( int j = 0; j < len; ++j )
- CRC32_update_byte( &encoder->crc_, Mf_peek( encoder->matchfinder, j-ahead ) );
ahead -= len; i += len;
if( LZe_member_position( encoder ) >= member_size_limit )
{