diff options
Diffstat (limited to 'encoder_base.h')
-rw-r--r-- | encoder_base.h | 90 |
1 files changed, 42 insertions, 48 deletions
diff --git a/encoder_base.h b/encoder_base.h index f0240cc..094f679 100644 --- a/encoder_base.h +++ b/encoder_base.h @@ -1,20 +1,20 @@ -/* Lzlib - Compression library for the lzip format - Copyright (C) 2009-2019 Antonio Diaz Diaz. +/* Lzlib - Compression library for the lzip format + Copyright (C) 2009-2024 Antonio Diaz Diaz. - This library is free software. Redistribution and use in source and - binary forms, with or without modification, are permitted provided - that the following conditions are met: + This library is free software. Redistribution and use in source and + binary forms, with or without modification, are permitted provided + that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions, and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions, and the following disclaimer in the + documentation and/or other materials provided with the distribution. - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ enum { price_shift_bits = 6, @@ -142,15 +142,14 @@ static inline int price1( const Bit_model probability ) { return get_price( bit_model_total - probability ); } static inline int price_bit( const Bit_model bm, const bool bit ) - { return ( bit ? price1( bm ) : price0( bm ) ); } + { return bit ? price1( bm ) : price0( bm ); } static inline int price_symbol3( const Bit_model bm[], int symbol ) { - int price; bool bit = symbol & 1; symbol |= 8; symbol >>= 1; - price = price_bit( bm[symbol], bit ); + int price = price_bit( bm[symbol], bit ); bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); return price + price_bit( bm[1], symbol & 1 ); } @@ -158,10 +157,9 @@ static inline int price_symbol3( const Bit_model bm[], int symbol ) static inline int price_symbol6( const Bit_model bm[], unsigned symbol ) { - int price; bool bit = symbol & 1; symbol |= 64; symbol >>= 1; - price = price_bit( bm[symbol], bit ); + int price = price_bit( bm[symbol], bit ); bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); @@ -172,10 +170,9 @@ static inline int price_symbol6( const Bit_model bm[], unsigned symbol ) static inline int price_symbol8( const Bit_model bm[], int symbol ) { - int price; bool bit = symbol & 1; symbol |= 0x100; symbol >>= 1; - price = price_bit( bm[symbol], bit ); + int price = price_bit( bm[symbol], bit ); bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); @@ -197,7 +194,7 @@ static inline int price_symbol_reversed( const Bit_model bm[], int symbol, const bool bit = symbol & 1; symbol >>= 1; price += price_bit( bm[model], bit ); - model = ( model << 1 ) | bit; + model <<= 1; model |= bit; } return price; } @@ -240,7 +237,7 @@ struct Matchfinder_base int pos_array_size; int saved_dictionary_size; /* dictionary_size restored by Mb_reset */ bool at_stream_end; /* stream_pos shows real end of file */ - bool flushing; + bool sync_flush_pending; }; static bool Mb_normalize_pos( struct Matchfinder_base * const mb ); @@ -265,21 +262,22 @@ Mb_data_position( const struct Matchfinder_base * const mb ) { return mb->partial_data_pos + mb->pos; } static inline void Mb_finish( struct Matchfinder_base * const mb ) - { mb->at_stream_end = true; mb->flushing = false; } + { mb->at_stream_end = true; mb->sync_flush_pending = false; } static inline bool Mb_data_finished( const struct Matchfinder_base * const mb ) - { return mb->at_stream_end && !mb->flushing && mb->pos >= mb->stream_pos; } + { return mb->at_stream_end && mb->pos >= mb->stream_pos; } static inline bool Mb_flushing_or_end( const struct Matchfinder_base * const mb ) - { return mb->at_stream_end || mb->flushing; } + { return mb->at_stream_end || mb->sync_flush_pending; } static inline int Mb_free_bytes( const struct Matchfinder_base * const mb ) { if( Mb_flushing_or_end( mb ) ) return 0; return mb->buffer_size - mb->stream_pos; } -static inline bool Mb_enough_available_bytes( const struct Matchfinder_base * const mb ) - { return ( mb->pos + mb->after_size <= mb->stream_pos || - ( Mb_flushing_or_end( mb ) && mb->pos < mb->stream_pos ) ); } +static inline bool +Mb_enough_available_bytes( const struct Matchfinder_base * const mb ) + { return mb->pos + mb->after_size <= mb->stream_pos || + ( Mb_flushing_or_end( mb ) && mb->pos < mb->stream_pos ); } static inline const uint8_t * Mb_ptr_to_current_pos( const struct Matchfinder_base * const mb ) @@ -342,7 +340,6 @@ static inline void Re_shift_low( struct Range_encoder * const renc ) static inline void Re_reset( struct Range_encoder * const renc, const unsigned dictionary_size ) { - int i; Cb_reset( &renc->cb ); renc->low = 0; renc->partial_member_pos = 0; @@ -350,8 +347,7 @@ static inline void Re_reset( struct Range_encoder * const renc, renc->ff_count = 0; renc->cache = 0; Lh_set_dictionary_size( renc->header, dictionary_size ); - for( i = 0; i < Lh_size; ++i ) - Cb_put_byte( &renc->cb, renc->header[i] ); + int i; for( i = 0; i < Lh_size; ++i ) Cb_put_byte( &renc->cb, renc->header[i] ); } static inline bool Re_init( struct Range_encoder * const renc, @@ -400,8 +396,7 @@ static inline void Re_encode( struct Range_encoder * const renc, { renc->range >>= 1; if( symbol & mask ) renc->low += renc->range; - if( renc->range <= 0x00FFFFFFU ) - { renc->range <<= 8; Re_shift_low( renc ); } + if( renc->range <= 0x00FFFFFFU ) { renc->range <<= 8; Re_shift_low( renc ); } } } @@ -426,28 +421,28 @@ static inline void Re_encode_bit( struct Range_encoder * const renc, static inline void Re_encode_tree3( struct Range_encoder * const renc, Bit_model bm[], const int symbol ) { - int model = 1; bool bit = ( symbol >> 2 ) & 1; - Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; + Re_encode_bit( renc, &bm[1], bit ); + int model = 2 | bit; bit = ( symbol >> 1 ) & 1; - Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; + Re_encode_bit( renc, &bm[model], bit ); model <<= 1; model |= bit; Re_encode_bit( renc, &bm[model], symbol & 1 ); } static inline void Re_encode_tree6( struct Range_encoder * const renc, Bit_model bm[], const unsigned symbol ) { - int model = 1; bool bit = ( symbol >> 5 ) & 1; - Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; + Re_encode_bit( renc, &bm[1], bit ); + int model = 2 | bit; bit = ( symbol >> 4 ) & 1; - Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; + Re_encode_bit( renc, &bm[model], bit ); model <<= 1; model |= bit; bit = ( symbol >> 3 ) & 1; - Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; + Re_encode_bit( renc, &bm[model], bit ); model <<= 1; model |= bit; bit = ( symbol >> 2 ) & 1; - Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; + Re_encode_bit( renc, &bm[model], bit ); model <<= 1; model |= bit; bit = ( symbol >> 1 ) & 1; - Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; + Re_encode_bit( renc, &bm[model], bit ); model <<= 1; model |= bit; Re_encode_bit( renc, &bm[model], symbol & 1 ); } @@ -460,7 +455,7 @@ static inline void Re_encode_tree8( struct Range_encoder * const renc, { const bool bit = ( symbol >> i ) & 1; Re_encode_bit( renc, &bm[model], bit ); - model = ( model << 1 ) | bit; + model <<= 1; model |= bit; } } @@ -474,7 +469,7 @@ static inline void Re_encode_tree_reversed( struct Range_encoder * const renc, const bool bit = symbol & 1; symbol >>= 1; Re_encode_bit( renc, &bm[model], bit ); - model = ( model << 1 ) | bit; + model <<= 1; model |= bit; } } @@ -561,7 +556,7 @@ static inline bool LZeb_init( struct LZ_encoder_base * const eb, } static inline bool LZeb_member_finished( const struct LZ_encoder_base * const eb ) - { return ( eb->member_finished && !Cb_used_bytes( &eb->renc.cb ) ); } + { return eb->member_finished && Cb_empty( &eb->renc.cb ); } static inline void LZeb_free( struct LZ_encoder_base * const eb ) { Re_free( &eb->renc ); Mb_free( &eb->mb ); } @@ -580,8 +575,7 @@ static inline int LZeb_price_matched( const struct LZ_encoder_base * const eb, static inline void LZeb_encode_literal( struct LZ_encoder_base * const eb, const uint8_t prev_byte, const uint8_t symbol ) - { Re_encode_tree8( &eb->renc, eb->bm_literal[get_lit_state(prev_byte)], - symbol ); } + { Re_encode_tree8( &eb->renc, eb->bm_literal[get_lit_state(prev_byte)], symbol ); } static inline void LZeb_encode_matched( struct LZ_encoder_base * const eb, const uint8_t prev_byte, const uint8_t symbol, const uint8_t match_byte ) @@ -592,8 +586,8 @@ static inline void LZeb_encode_pair( struct LZ_encoder_base * const eb, const unsigned dis, const int len, const int pos_state ) { - const unsigned dis_slot = get_slot( dis ); Re_encode_len( &eb->renc, &eb->match_len_model, len, pos_state ); + const unsigned dis_slot = get_slot( dis ); Re_encode_tree6( &eb->renc, eb->bm_dis_slot[get_len_state(len)], dis_slot ); if( dis_slot >= start_dis_model ) |