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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*************************************************************************
This project implements a complete(!) JPEG (Recommendation ITU-T
T.81 | ISO/IEC 10918-1) codec, plus a library that can be used to
encode and decode JPEG streams.
It also implements ISO/IEC 18477 aka JPEG XT which is an extension
towards intermediate, high-dynamic-range lossy and lossless coding
of JPEG. In specific, it supports ISO/IEC 18477-3/-6/-7/-8 encoding.
Note that only Profiles C and D of ISO/IEC 18477-7 are supported
here. Check the JPEG XT reference software for a full implementation
of ISO/IEC 18477-7.
Copyright (C) 2012-2018 Thomas Richter, University of Stuttgart and
Accusoft. (C) 2019-2020 Thomas Richter, Fraunhofer IIS.
This program is available under two licenses, GPLv3 and the ITU
Software licence Annex A Option 2, RAND conditions.
For the full text of the GPU license option, see README.license.gpl.
For the full text of the ITU license option, see README.license.itu.
You may freely select between these two options.
For the GPL option, please note the following:
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
/*
** This class allows to read individual bits from a stream of bytes.
** This class implements the bytestuffing as required.
**
** $Id: bitstream.hpp,v 1.33 2014/11/12 21:24:45 thor Exp $
**
*/
#ifndef IO_BITSTREAM_HPP
#define IO_BITSTREAM_HPP
/// Includes
#include "tools/environment.hpp"
#include "io/bytestream.hpp"
#include "tools/checksum.hpp"
///
/// Forwards
///
/// class BitStream
template<bool bitstuffing>
class BitStream : public JObject {
//
// The bit-buffer for output.
UBYTE m_ucB;
//
// The bit-buffer for input.
ULONG m_ulB;
//
// The number of bits left.
UBYTE m_ucBits;
//
// Number of bits the next fill operation fills in.
UBYTE m_ucNextBits;
//
// Set if run into a marker.
bool m_bMarker;
//
// Set if run into an EOF.
bool m_bEOF;
//
// The byte stream we read from.
class ByteStream *m_pIO;
//
// The checksum we keep updating.
class Checksum *m_pChk;
//
// Fill the byte-buffer. Implements bit-stuffing and byte-stuffing
// and detects markers and generates appropriate errors.
void Fill(void);
//
// Report an error if not enough bits were available, depending on
// the error flag.
void ReportError(void) NORETURN;
//
public:
BitStream()
{
}
//
~BitStream(void)
{
}
//
void OpenForRead(class ByteStream *io,class Checksum *chk)
{
m_pIO = io;
m_pChk = chk;
m_ulB = 0;
m_ucBits = 0;
m_ucNextBits = 8;
m_bMarker = false;
m_bEOF = false;
}
//
void OpenForWrite(class ByteStream *io,class Checksum *chk)
{
m_pIO = io;
m_pChk = chk;
m_ucB = 0;
m_ucBits = 8;
m_bMarker = false;
m_bEOF = false;
}
//
// Return the environment.
class Environ *EnvironOf(void) const
{
return m_pIO->EnvironOf();
}
//
// Return the underlying bytestream.
class ByteStream *ByteStreamOf(void) const
{
return m_pIO;
}
//
// Return the checksum.
class Checksum *ChecksumOf(void) const
{
return m_pChk;
}
//
// Read n bits at once, read at most 16 bits at once. Return the bits.
template<int n>
ULONG Get(void)
{
ULONG v;
assert(n > 0 && n <= 24);
// The Fill method ensures that there are always at least 16 bits in the buffer.
if (n > m_ucBits) {
Fill();
if (unlikely(n > m_ucBits))
ReportError();
}
v = m_ulB >> (32 - n);
m_ulB <<= n;
m_ucBits -= n;
return v;
}
//
// Get "bits" bits from the stream.
ULONG Get(UBYTE bits)
{
ULONG v;
assert(bits > 0 && bits <= 24);
// The Fill method ensures that there are always at least 16 bits in the buffer.
if (bits > m_ucBits) {
Fill();
if (unlikely(bits > m_ucBits))
ReportError();
}
v = m_ulB >> (32 - bits);
m_ulB <<= bits;
m_ucBits -= bits;
return v;
}
//
// Return the next 16 bits from the stream without actually removing
// them. Bits beyond the EOF are set to zero, but the error is not
// yet reported.
UWORD PeekWord(void)
{
if (m_ucBits < 16)
Fill();
return m_ulB >> 16;
}
//
// Remove n bits without reading them. Prior calls must have ensured
// that this number of bits is actually in the stream.
void SkipBits(UBYTE size)
{
if (unlikely(size > m_ucBits))
ReportError();
m_ulB <<= size;
m_ucBits -= size;
}
//
// Flush the buffer out to the stream. Must be done at the end of the
// coding to ensure that all bits are written out.
void Flush(void)
{
if (m_ucBits < 8) {
// The standard suggests (in an informative note) to fill in
// remaining bits by 1's, which interestingly creates the likelyhood
// of a bitstuffing case. Interestingly, the standard also says that
// a 0xff in front of a marker is a "fill byte" that may be dropped.
// Conclusion is that we may have a 0xff just in front of a marker without
// the byte stuffing. Wierd.
if (!bitstuffing)
m_ucB |= (1 << m_ucBits) - 1;
m_pIO->Put(m_ucB);
if (m_pChk)
m_pChk->Update(m_ucB);
m_ucBits = 8;
if (m_ucB == 0xff) { // stuffing case?
m_pIO->Put(0x00); // stuff a zero byte
if (m_pChk)
m_pChk->Update(0x00);
// Note that this must also happen if we are bitstuffing to avoid a pseudo-0xffff
// marker (JPEG 2000 could have dropped the 0xff here, but we can't).
// Actually, such markers are allowable, or rather might be, but
// be conservative and avoid writing them.
}
m_ucB = 0;
}
}
//
// Skip the bitstuffed zero-bit at the end of a
// line to be able to parse for a marker segment. This covers a
// race-condition in which a zero-byte had to be stuffed at the encoder
// side to avoid a double-0xff appear. This zero-byte is never read
// on the decoder side unless triggered manually since it is not
// part of the stream. If bytestuffing is enabled (not bitstuffing)
// the zero-byte is already removed as part of the refill of the 0xff.
void SkipStuffing(void)
{
if (bitstuffing) {
// Only in case all bits of the byte are read, and we
// need the refill anyhow... trigger it early.
if (m_ucBits == 0 && m_ucNextBits == 7) {
Fill();
}
}
}
//
template<int count>
void Put(ULONG bitbuffer)
{
int n = count;
assert(n > 0 && n <= 32);
// Do we want to output more bits than
// there is room in the buffer?
while(n > m_ucBits) {
// If so, output all bits we can.
n -= m_ucBits; // that many bits go away
m_ucB |= (bitbuffer>>n) & ((1<<m_ucBits)-1); // place them into buffer
// m_ucBits = 0; // superfluous: We've now zero bits space left.
m_pIO->Put(m_ucB);
if (m_pChk)
m_pChk->Update(m_ucB);
m_ucBits = 8;
if (m_ucB == 0xff) { // byte stuffing case?
if (bitstuffing) {
m_ucBits = 7;
} else {
m_pIO->Put(0x00); // stuff a zero byte
if (m_pChk)
m_pChk->Update(0x00);
}
}
m_ucB = 0;
}
// Now, we've more bits space left than we want to put.
// This is the easy case:
// Here, n <= m_ucBits.
m_ucBits -= n; // that many bits space left
m_ucB |= (bitbuffer & ((1<<n)-1)) << m_ucBits;
}
//
// Put "n" bits into the stream.
void Put(UBYTE n,ULONG bitbuffer)
{
assert(n > 0 && n <= 32);
// Do we want to output more bits than
// there is room in the buffer?
while(n > m_ucBits) {
// If so, output all bits we can.
n -= m_ucBits; // that many bits go away
m_ucB |= (bitbuffer>>n) & ((1<<m_ucBits)-1); // place them into buffer
// m_ucBits = 0; // superfluous: We've now zero bits space left.
m_pIO->Put(m_ucB);
if (m_pChk)
m_pChk->Update(m_ucB);
m_ucBits = 8;
if (m_ucB == 0xff) { // byte stuffing case?
if (bitstuffing) {
m_ucBits = 7;
} else {
m_pIO->Put(0x00); // stuff a zero byte
if (m_pChk)
m_pChk->Update(0x00);
}
}
m_ucB = 0;
}
// Now, we've more bits space left than we want to put.
// This is the easy case:
// Here, n <= m_ucBits.
m_ucBits -= n; // that many bits space left
m_ucB |= (bitbuffer & ((1<<n)-1)) << m_ucBits;
}
};
///
///
#endif
|