summaryrefslogtreecommitdiffstats
path: root/encoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'encoder.c')
-rw-r--r--encoder.c96
1 files changed, 43 insertions, 53 deletions
diff --git a/encoder.c b/encoder.c
index ce0ddf2..4b9d7ec 100644
--- a/encoder.c
+++ b/encoder.c
@@ -1,5 +1,5 @@
/* Clzip - LZMA lossless data compressor
- Copyright (C) 2010-2016 Antonio Diaz Diaz.
+ Copyright (C) 2010-2017 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -43,7 +43,7 @@ int LZe_get_match_pairs( struct LZ_encoder * const e, struct Pair * pairs )
const int min_pos = ( e->eb.mb.pos > e->eb.mb.dictionary_size ) ?
e->eb.mb.pos - e->eb.mb.dictionary_size : 0;
const uint8_t * const data = Mb_ptr_to_current_pos( &e->eb.mb );
- int count, key2, key3, key4, newpos;
+ int count, key2, key3, key4, newpos1;
unsigned tmp;
int len_limit = e->match_len_limit;
@@ -90,15 +90,15 @@ int LZe_get_match_pairs( struct LZ_encoder * const e, struct Pair * pairs )
e->eb.mb.prev_positions[key2] = pos1;
e->eb.mb.prev_positions[key3] = pos1;
- newpos = e->eb.mb.prev_positions[key4];
+ newpos1 = e->eb.mb.prev_positions[key4];
e->eb.mb.prev_positions[key4] = pos1;
for( count = e->cycles; ; )
{
int delta;
- if( newpos <= min_pos || --count < 0 ) { *ptr0 = *ptr1 = 0; break; }
+ if( newpos1 <= min_pos || --count < 0 ) { *ptr0 = *ptr1 = 0; break; }
- delta = pos1 - newpos;
+ delta = pos1 - newpos1;
newptr = e->eb.mb.pos_array +
( ( e->eb.mb.cyclic_pos - delta +
( (e->eb.mb.cyclic_pos >= delta) ? 0 : e->eb.mb.dictionary_size + 1 ) ) << 1 );
@@ -120,16 +120,16 @@ int LZe_get_match_pairs( struct LZ_encoder * const e, struct Pair * pairs )
}
if( data[len-delta] < data[len] )
{
- *ptr0 = newpos;
+ *ptr0 = newpos1;
ptr0 = newptr + 1;
- newpos = *ptr0;
+ newpos1 = *ptr0;
len0 = len; if( len1 < len ) len = len1;
}
else
{
- *ptr1 = newpos;
+ *ptr1 = newpos1;
ptr1 = newptr;
- newpos = *ptr1;
+ newpos1 = *ptr1;
len1 = len; if( len0 < len ) len = len0;
}
}
@@ -145,7 +145,7 @@ static void LZe_update_distance_prices( struct LZ_encoder * const e )
const int dis_slot = dis_slots[dis];
const int direct_bits = ( dis_slot >> 1 ) - 1;
const int base = ( 2 | ( dis_slot & 1 ) ) << direct_bits;
- const int price = price_symbol_reversed( e->eb.bm_dis + base - dis_slot - 1,
+ const int price = price_symbol_reversed( e->eb.bm_dis + ( base - dis_slot ),
dis - base, direct_bits );
for( len_state = 0; len_state < len_states; ++len_state )
e->dis_prices[len_state][dis] = price;
@@ -158,9 +158,9 @@ static void LZe_update_distance_prices( struct LZ_encoder * const e )
const Bit_model * const bmds = e->eb.bm_dis_slot[len_state];
int slot = 0;
for( ; slot < end_dis_model; ++slot )
- dsp[slot] = price_symbol( bmds, slot, dis_slot_bits );
+ dsp[slot] = price_symbol6( bmds, slot );
for( ; slot < e->num_dis_slots; ++slot )
- dsp[slot] = price_symbol( bmds, slot, dis_slot_bits ) +
+ dsp[slot] = price_symbol6( bmds, slot ) +
(((( slot >> 1 ) - 1 ) - dis_align_bits ) << price_shift_bits );
for( dis = 0; dis < start_dis_model; ++dis )
@@ -173,16 +173,16 @@ static void LZe_update_distance_prices( struct LZ_encoder * const e )
/* Returns the number of bytes advanced (ahead).
trials[0]..trials[ahead-1] contain the steps to encode.
- ( trials[0].dis == -1 ) means literal.
+ ( trials[0].dis4 == -1 ) means literal.
A match/rep longer or equal than match_len_limit finishes the sequence.
*/
static int LZe_sequence_optimizer( struct LZ_encoder * const e,
const int reps[num_rep_distances],
const State state )
{
- int main_len, num_pairs, i, rep, cur = 0, num_trials, len;
+ int main_len, num_pairs, i, rep, num_trials, len;
+ int rep_index = 0, cur = 0;
int replens[num_rep_distances];
- int rep_index = 0;
if( e->pending_num_pairs > 0 ) /* from previous call */
{
@@ -195,13 +195,13 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
for( i = 0; i < num_rep_distances; ++i )
{
- replens[i] = Mb_true_match_len( &e->eb.mb, 0, reps[i] + 1, max_match_len );
+ replens[i] = Mb_true_match_len( &e->eb.mb, 0, reps[i] + 1 );
if( replens[i] > replens[rep_index] ) rep_index = i;
}
if( replens[rep_index] >= e->match_len_limit )
{
e->trials[0].price = replens[rep_index];
- e->trials[0].dis = rep_index;
+ e->trials[0].dis4 = rep_index;
LZe_move_and_update( e, replens[rep_index] );
return replens[rep_index];
}
@@ -209,7 +209,7 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
if( main_len >= e->match_len_limit )
{
e->trials[0].price = main_len;
- e->trials[0].dis = e->pairs[num_pairs-1].dis + num_rep_distances;
+ e->trials[0].dis4 = e->pairs[num_pairs-1].dis + num_rep_distances;
LZe_move_and_update( e, main_len );
return main_len;
}
@@ -227,7 +227,7 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
e->trials[1].price += LZeb_price_literal( &e->eb, prev_byte, cur_byte );
else
e->trials[1].price += LZeb_price_matched( &e->eb, prev_byte, cur_byte, match_byte );
- e->trials[1].dis = -1; /* literal */
+ e->trials[1].dis4 = -1; /* literal */
if( match_byte == cur_byte )
Tr_update( &e->trials[1], rep_match_price +
@@ -238,7 +238,7 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
if( num_trials < min_match_len )
{
e->trials[0].price = 1;
- e->trials[0].dis = e->trials[1].dis;
+ e->trials[0].dis4 = e->trials[1].dis4;
Mb_move_pos( &e->eb.mb );
return 1;
}
@@ -263,7 +263,7 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
if( main_len > replens[0] )
{
const int normal_match_price = match_price + price0( e->eb.bm_rep[state] );
- i = 0, len = max( replens[0] + 1, min_match_len );
+ int i = 0, len = max( replens[0] + 1, min_match_len );
while( len > e->pairs[i].len ) ++i;
while( true )
{
@@ -304,7 +304,7 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
/* give final values to current trial */
cur_trial = &e->trials[cur];
{
- int dis = cur_trial->dis;
+ const int dis4 = cur_trial->dis4;
int prev_index = cur_trial->prev_index;
const int prev_index2 = cur_trial->prev_index2;
@@ -313,32 +313,24 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
cur_state = e->trials[prev_index].state;
if( prev_index + 1 == cur ) /* len == 1 */
{
- if( dis == 0 ) cur_state = St_set_short_rep( cur_state );
+ if( dis4 == 0 ) cur_state = St_set_short_rep( cur_state );
else cur_state = St_set_char( cur_state ); /* literal */
}
- else if( dis < num_rep_distances ) cur_state = St_set_rep( cur_state );
+ else if( dis4 < num_rep_distances ) cur_state = St_set_rep( cur_state );
else cur_state = St_set_match( cur_state );
}
- else if( prev_index2 == dual_step_trial ) /* dis == 0 */
+ else
{
- --prev_index;
- cur_state = e->trials[prev_index].state;
- cur_state = St_set_char( cur_state );
- cur_state = St_set_rep( cur_state );
- }
- else /* if( prev_index2 >= 0 ) */
- {
- prev_index = prev_index2;
- cur_state = e->trials[prev_index].state;
- if( dis < num_rep_distances ) cur_state = St_set_rep( cur_state );
- else cur_state = St_set_match( cur_state );
- cur_state = St_set_char( cur_state );
- cur_state = St_set_rep( cur_state );
+ if( prev_index2 == dual_step_trial ) /* dis4 == 0 (rep0) */
+ --prev_index;
+ else /* prev_index2 >= 0 */
+ prev_index = prev_index2;
+ cur_state = 8; /* St_set_char_rep(); */
}
cur_trial->state = cur_state;
for( i = 0; i < num_rep_distances; ++i )
cur_trial->reps[i] = e->trials[prev_index].reps[i];
- mtf_reps( dis, cur_trial->reps );
+ mtf_reps( dis4, cur_trial->reps ); /* literal is ignored */
}
pos_state = Mb_data_position( &e->eb.mb ) & pos_state_mask;
@@ -361,7 +353,7 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
match_price = cur_trial->price + price1( e->eb.bm_match[cur_state][pos_state] );
rep_match_price = match_price + price1( e->eb.bm_rep[cur_state] );
- if( match_byte == cur_byte && next_trial->dis != 0 &&
+ if( match_byte == cur_byte && next_trial->dis4 != 0 &&
next_trial->prev_index2 == single_step_trial )
{
const int price = rep_match_price +
@@ -369,7 +361,7 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
if( price <= next_trial->price )
{
next_trial->price = price;
- next_trial->dis = 0;
+ next_trial->dis4 = 0; /* rep0 */
next_trial->prev_index = cur;
}
}
@@ -386,16 +378,16 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
const uint8_t * const data = Mb_ptr_to_current_pos( &e->eb.mb );
const int dis = cur_trial->reps[0] + 1;
const int limit = min( e->match_len_limit + 1, triable_bytes );
- len = 1;
+ int len = 1;
while( len < limit && data[len-dis] == data[len] ) ++len;
if( --len >= min_match_len )
{
const int pos_state2 = ( pos_state + 1 ) & pos_state_mask;
const State state2 = St_set_char( cur_state );
const int price = next_price +
- price1( e->eb.bm_match[state2][pos_state2] ) +
- price1( e->eb.bm_rep[state2] ) +
- LZe_price_rep0_len( e, len, state2, pos_state2 );
+ price1( e->eb.bm_match[state2][pos_state2] ) +
+ price1( e->eb.bm_rep[state2] ) +
+ LZe_price_rep0_len( e, len, state2, pos_state2 );
while( num_trials < cur + 1 + len )
e->trials[++num_trials].price = infinite_price;
Tr_update2( &e->trials[cur+1+len], price, cur + 1 );
@@ -406,8 +398,8 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
for( rep = 0; rep < num_rep_distances; ++rep )
{
const uint8_t * const data = Mb_ptr_to_current_pos( &e->eb.mb );
- int price;
const int dis = cur_trial->reps[rep] + 1;
+ int price;
if( data[0-dis] != data[0] || data[1-dis] != data[1] ) continue;
for( len = min_match_len; len < len_limit; ++len )
@@ -463,7 +455,6 @@ static int LZe_sequence_optimizer( struct LZ_encoder * const e,
for( len = start_len; ; ++len )
{
int price = normal_match_price + LZe_price_pair( e, dis, len, pos_state );
-
Tr_update( &e->trials[cur+len], price, dis + num_rep_distances, cur );
/* try match + literal + rep0 */
@@ -510,7 +501,7 @@ bool LZe_encode_member( struct LZ_encoder * const e,
const int dis_price_count = best ? 1 : 512;
const int align_price_count = best ? 1 : dis_align_size;
const int price_count = ( e->match_len_limit > 36 ) ? 1013 : 4093;
- int price_counter = 0;
+ int price_counter = 0; /* counters may decrement below 0 */
int dis_price_counter = 0;
int align_price_counter = 0;
int ahead, i;
@@ -551,7 +542,6 @@ bool LZe_encode_member( struct LZ_encoder * const e,
}
ahead = LZe_sequence_optimizer( e, reps, state );
- if( ahead <= 0 ) return false; /* can't happen */
price_counter -= ahead;
for( i = 0; ahead > 0; )
@@ -559,7 +549,7 @@ bool LZe_encode_member( struct LZ_encoder * const e,
const int pos_state =
( Mb_data_position( &e->eb.mb ) - ahead ) & pos_state_mask;
const int len = e->trials[i].price;
- const int dis = e->trials[i].dis;
+ int dis = e->trials[i].dis4;
bool bit = ( dis < 0 );
Re_encode_bit( &e->eb.renc, &e->eb.bm_match[state][pos_state], !bit );
@@ -605,9 +595,9 @@ bool LZe_encode_member( struct LZ_encoder * const e,
}
else /* match */
{
- LZeb_encode_pair( &e->eb, dis - num_rep_distances, len, pos_state );
- if( get_slot( dis - num_rep_distances ) >= end_dis_model )
- --align_price_counter;
+ dis -= num_rep_distances;
+ LZeb_encode_pair( &e->eb, dis, len, pos_state );
+ if( dis >= modeled_distances ) --align_price_counter;
--dis_price_counter;
Lp_decrement_counter( &e->match_len_prices, pos_state );
state = St_set_match( state );