| 131 | } |
| 132 | |
| 133 | void ArithmeticEncoder::done() |
| 134 | { |
| 135 | if (outstream == 0) return; |
| 136 | |
| 137 | U32 init_base = base; // done encoding: set final data bytes |
| 138 | BOOL another_byte = TRUE; |
| 139 | |
| 140 | if (length > 2 * AC__MinLength) { |
| 141 | base += AC__MinLength; // base offset |
| 142 | length = AC__MinLength >> 1; // set new length for 1 more byte |
| 143 | } |
| 144 | else { |
| 145 | base += AC__MinLength >> 1; // base offset |
| 146 | length = AC__MinLength >> 9; // set new length for 2 more bytes |
| 147 | another_byte = FALSE; |
| 148 | } |
| 149 | |
| 150 | if (init_base > base) propagate_carry(); // overflow = carry |
| 151 | renorm_enc_interval(); // renormalization = output last bytes |
| 152 | |
| 153 | if (endbyte != endbuffer) |
| 154 | { |
| 155 | assert(outbyte < outbuffer + AC_BUFFER_SIZE); |
| 156 | outstream->putBytes(outbuffer + AC_BUFFER_SIZE, AC_BUFFER_SIZE); |
| 157 | } |
| 158 | U32 buffer_size = (U32)(outbyte - outbuffer); |
| 159 | if (buffer_size) outstream->putBytes(outbuffer, buffer_size); |
| 160 | |
| 161 | // write two or three zero bytes to be in sync with the decoder's byte reads |
| 162 | outstream->putByte(0); |
| 163 | outstream->putByte(0); |
| 164 | if (another_byte) outstream->putByte(0); |
| 165 | |
| 166 | outstream = 0; |
| 167 | } |
| 168 | |
| 169 | ArithmeticBitModel* ArithmeticEncoder::createBitModel() |
| 170 | { |