1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Model-independent compression and decompression module.

use std::boxed::Box;
use super::Error;
use super::Result;
use super::bitio::BitRead;
use super::bitio::BitWrite;
use super::model::Model;

/// The current state of the encoder and decoder.
pub struct Codec {
    /// The start of the active range.
    low: u64,
    /// The end of the active range.
    high: u64,
    /// Number of pending code bits to putput when encoding.
    /// The current symbol value being decoded.
    pending: u64,
    /// Number of trailing bits to output for unambigous encoding.
    /// Number of leading bits to read when decoding.
    extra: usize,
    /// The probability model being used.
    model: Box<Model>,
}

impl Codec {
    /// Creates and initializes the codec for encoding or decoding.
    pub fn new(m: Box<Model>) -> Codec {
        Codec {
            low: { m.parameters().code_min },
            high: { m.parameters().code_max },
            pending: 0,
            extra: { m.parameters().code_bits },
            model: m,
        }
    }

    /// Outputs a bit and the preceeding pending bits, if any.
    fn put_bit(&mut self, bit: bool, output: &mut BitWrite) -> Result<()> {
        try!(output.write_bits(if bit { 1 } else { 0 }, 1));
        while self.pending > 0 {
            try!(output.write_bits(if bit { 0 } else { 1 }, 1));
            self.pending -= 1;
        }
        return Ok(());
    }

    /// Inputs a bit.
    fn get_bit(&mut self, input: &mut BitRead) -> Result<()> {
        self.pending = (self.pending << 1) | try!(input.read_bits(1)) as u64;
        Ok(())
    }

    /// Compresses a symbol and outputs some bits depending on the state of the codec.
    pub fn compress_symbol(&mut self, symbol: usize, output: &mut BitWrite) -> Result<()> {
        let count = self.model.total_frequency();
        let (low, high) = try!(self.model.get_frequency(symbol));
        let range = self.high - self.low + 1;
        self.high = self.low + (range * high / count) - 1;
        self.low = self.low + (range * low / count);

        loop {
           if self.high < { self.model.parameters().code_half } {
               try!(self.put_bit(false, output));

               if symbol == { self.model.parameters().symbol_eof } {
                   self.extra -= 1;
               }
           } else if self.low >= { self.model.parameters().code_half } {
               try!(self.put_bit(true, output));

               if symbol == { self.model.parameters().symbol_eof } {
                   self.extra -= 1;
               }
           } else if self.low >= { self.model.parameters().code_one_fourth } && self.high < { self.model.parameters().code_three_fourths } {
               self.pending += 1;
               self.low -= { self.model.parameters().code_one_fourth };
               self.high -= { self.model.parameters().code_one_fourth };

               if symbol == { self.model.parameters().symbol_eof } {
                   self.extra -= 1;
               }
           } else {
               break;
           }

           self.high = ((self.high << 1) + 1) & { self.model.parameters().code_max };
           self.low = (self.low << 1) & { self.model.parameters().code_max };
        }

        if symbol == { self.model.parameters().symbol_eof } {
            while self.extra > 0 {
                let mask = self.low & { self.model.parameters().code_half };
                try!(self.put_bit(mask != 0, output));
                self.low = (self.low << 1) & { self.model.parameters().code_max };
                self.extra -= 1;
            }
            try!(output.flush_bits());
        }
        return Ok(());
    }

    /// Compresses an entire byte stream outputting the EOF symbol and all bits for unambigous encoding.
    pub fn compress_stream(&mut self, input: &mut BitRead, output: &mut BitWrite) -> Result<()> {
        loop {
            let symbol = match input.read_bits({ self.model.parameters().symbol_bits }) {
                Ok(b) => b as usize,
                Err(Error::Eof) => { self.model.parameters().symbol_eof },
                Err(e) => { return Err(e); }
            };

            try!(self.compress_symbol(symbol, output));

            if symbol == { self.model.parameters().symbol_eof } {
                break;
            }
        }

        return Ok(());
    }

    /// Decompresses a symbol reading some bits until the symbol can be decoded.
    pub fn decompress_symbol(&mut self, input: &mut BitRead) -> Result<usize> {
        while self.extra > 0 {
            try!(self.get_bit(input));
            self.extra -= 1;
        }

        let range = self.high - self.low + 1;
        let count = self.model.total_frequency();
        let value = ((self.pending - self.low + 1) * count - 1) / range;
        let (symbol, low, high) = try!(self.model.get_symbol(value));
        self.high = self.low + (range * high / count) - 1;
        self.low = self.low + (range * low / count);

        if symbol == { self.model.parameters().symbol_eof } {
            return Ok(symbol);
        }

        loop {
            if self.high < { self.model.parameters().code_half } {
                // do nothing
            } else if self.low >= { self.model.parameters().code_half } {
                self.pending -= { self.model.parameters().code_half };
                self.low -= { self.model.parameters().code_half };
                self.high -= { self.model.parameters().code_half };
            } else if self.low >= { self.model.parameters().code_one_fourth } && self.high < { self.model.parameters().code_three_fourths } {
                self.pending -= { self.model.parameters().code_one_fourth };
                self.low -= { self.model.parameters().code_one_fourth };
                self.high -= { self.model.parameters().code_one_fourth };
            } else {
                break;
            }

            self.low = self.low << 1;
            self.high = (self.high << 1) + 1;
            try!(self.get_bit(input));
        }

        return Ok(symbol);
    }

    /// Decompresses a whole bit stream until the EOF symbol is found.
    pub fn decompress_stream(&mut self, input: &mut BitRead, output: &mut BitWrite) -> Result<()> {
        loop {
            let symbol = try!(self.decompress_symbol(input));

            if symbol == { self.model.parameters().symbol_eof } {
                break;
            } else {
                try!(output.write_bits(symbol, { self.model.parameters().symbol_bits }));
            }
        }

        return Ok(());
    }
}