Welcome to mirror list, hosted at ThFree Co, Russian Federation.

byteio.h « libdirac_byteio « libdirac « DiracSplitter « parser « filters « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 806d98a55b37f907aa92aa1c57387471c6038023 (plain)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* ***** BEGIN LICENSE BLOCK *****
*
* $Id: byteio.h,v 1.11 2009/01/21 05:18:09 asuraparaju Exp $ $Name:  $
*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is BBC Research and Development code.
*
* The Initial Developer of the Original Code is the British Broadcasting
* Corporation.
* Portions created by the Initial Developer are Copyright (C) 2004.
* All Rights Reserved.
*
* Contributor(s): Andrew Kennedy (Original Author),
*                 Anuradha Suraparaju
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
* Public License Version 2.1 (the "LGPL"), in which case the provisions of
* the GPL or the LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the either
* the GPL or LGPL and not to allow others to use your version of this file
* under the MPL, indicate your decision by deleting the provisions above
* and replace them with the notice and other provisions required by the GPL
* or LGPL. If you do not delete the provisions above, a recipient may use
* your version of this file under the terms of any one of the MPL, the GPL
* or the LGPL.
* ***** END LICENSE BLOCK ***** */

/**
* Definition of class ByteIO.
*/
#ifndef byteio_h
#define byteio_h

// SYSTEM INCLUDES
#include <iostream>             // IO classes
#include <sstream>              // IO classes
#include <iomanip>              // setw
#include <climits>              // CHAR_BIT

//LOCAL INCLUDEs
#include <libdirac_byteio/dirac_byte_stats.h>   // stores stats

namespace dirac
{

// BIT DEFS
#define BIT_ZERO 0
#define BIT_ONE 1

// most significant bit in a character
#define MS_BIT                (1 << (CHAR_BIT - 1))

/* array index for character containing bit */
//#define BIT_IN_CHAR(bit)      (1 << (CHAR_BIT-1-bit))
#define BIT_IN_CHAR(bit)      (1 << bit)


/**
* Class ByteIO - top-level class for reading/writing bytes to a stream
*/
class ByteIO
{
public:

    /**
    * Default constructor
    *@param new_stream <B>Has Creates & owns data buffer </B>
    */
    ByteIO(bool new_stream = true);

    /**
    * Constructor
    *@param stream_data Copies data buffer details
    */
    ByteIO(const ByteIO& stream_data);

    /**
    * Destructor
    */
    virtual ~ByteIO();

    /**
    * Gathers byte-stream statistics
    *@param dirac_byte_stats Collates byte information
    */
    virtual void CollateByteStats(DiracByteStats& dirac_byte_stats)
    {
        dirac_byte_stats.Clear();
    }

    /**
    * Get bytes in Dirac-bytestream format
    */
    virtual const std::string GetBytes();

    /**
    * Get position of read stream pointer
    */
    int GetReadBytePosition() const
    {
        return mp_stream->tellg();
    };


    /**
    *Gets size (in bytes)
    */
    virtual int GetSize() const;

    /**
    * Copies stream source/destination info
    *@param byte_io Byte source/destination
    */
    void SetByteParams(const ByteIO& byte_io);

    /**
    * Sync input for byte-alignment
    */
    void ByteAlignOutput();

    /**
            * Ouputs an unsigned integer in interleaved exp Golomb format
            *@param value Integer to be output
            */
    //void OutputVarLengthUint(const unsigned int& value);
    void WriteUint(unsigned int value);

    /**
    * Sets input size in bits. Read is limited by this
    */
    void SetBitsLeft(int left_bits)
    {
        m_bits_left = left_bits;
    }

    /**
    * Sets input size in bits. Read is limited by this
    */
    int BitsLeft(void)
    {
        return m_bits_left;
    }

protected:

    inline bool CanRead() const
    {
        return(!mp_stream->eof());
    }

    inline bool GetBit(unsigned char& c, int pos) const
    {
        return (c & BIT_IN_CHAR(pos));
    }

    inline void SetBit(unsigned char& c, int pos) const
    {
        c |= BIT_IN_CHAR(pos);
    }

    inline void SetBits(unsigned char& c, unsigned char bits) const
    {
        c |= bits;
    }

    /**
    * Sync input for byte-alignment
    */
    void ByteAlignInput();


    /**
    * Reads boolean value
    */
    bool ReadBool();

    /**
    * Reads boolean value - bounded i/o
    */
    bool ReadBoolB();

    /**
    * Reads next bit
    */
    int ReadBit();

    /**
    * Reads next bit - bounded i/o
    */
    int ReadBitB();

    /**
    * Reads next 'count' bits
    *@param count number of bits to be read
    *@return unsigned interger read
    */
    unsigned int ReadNBits(int count);

    /**
    * Reads from stream
    *@param data Start of char buffer
    *@param count Number of bytes to read
    */
    void InputBytes(char* data, int count)
    {
        //int j=mp_stream->tellg();
        mp_stream->read(data, count);

        //int h=mp_stream->tellg();
    }

    /**
    * Flushes the bounde input
    */
    void FlushInputB();

    /**
    * Reads a signed integer in interleaved exp-Golomb format
    *return Signed integer read
    */
    //int InputVarLengthInt();
    int ReadSint();

    /**
    * Reads a signed integer in interleaved exp-Golomb format from bounded input
    *return Signed integer read
    */
    int ReadSintB();

    /**
    * Reads an unsigned integer in interleaved exp Golomb format
    *@return Unsigned Integer read
    */
    //unsigned int InputVarLengthUint();
    unsigned int ReadUint();

    /**
    * Reads an unsigned integer in interleaved exp Golomb format from bounded input
    *@return Unsigned Integer read
    */
    //unsigned int InputVarLengthUint();
    unsigned int ReadUintB();

    /**
    * Reads a fixed length unsigned integer from the stream in big endian
    *@param byte_size Number of bytes in fixed length integer
    *@return Unsigned Integer read
    */
    //inline unsigned int InputFixedLengthUint(const int byte_size) {
    inline unsigned int ReadUintLit(const int byte_size)
    {
        unsigned int val = 0;
        for(int i = 0; i < byte_size; ++i)
        {
            val <<= 8;
            val += (unsigned char)mp_stream->get();
        }
        m_num_bytes += byte_size;
        return val;
    }

    /**
    * Reads a byte from the stream
    */
    inline unsigned char InputUnByte()
    {
        m_num_bytes++ ;
        return mp_stream->get();
    }

    /**
    * Reads a series of bytes from a stream
    */
    inline std::string InputUnString(const int count)
    {
        std::string str;
        for(int index = 0; index < count; ++index)
            str.push_back(InputUnByte());
        return str;
    }

    /**
    * Outputs a bit
    *@param bit 1/0 Output
    */
    void WriteBit(const bool& bit);

    /**
    * Outputs an unsigned integer
    *@param val Integer to be output
    *@return number of bits written
    */
    int WriteNBits(unsigned int val);

    /**
    * Outputs an n bit integer
    *@param val   Unsigned Integer to be output
    *@param count number of bits to be written
    */
    void WriteNBits(unsigned int val, int count);



    /**
    * Outputs a series of bytes
    */
    void OutputBytes(const std::string& bytes)
    {
        int cur_pos = mp_stream->tellg();
        mp_stream->str(mp_stream->str() + bytes);
        m_num_bytes += bytes.size();
        //   *mp_stream << bytes;
        mp_stream->seekg(std::max(cur_pos, 0), std::ios_base::beg);
    }

    /**
            * Outputs current byte contents
            */
    inline void OutputCurrentByte()
    {
        if(m_current_pos)
        {
            *mp_stream << (m_current_byte);
            ++m_num_bytes;
            m_current_pos = 0;
            m_current_byte = 0;
        }
    };

    /**
    * Outputs an integer in Golomb signed integer format
    *@param val Integer to be output
    */
    //void OutputVarLengthInt(const int val);
    void WriteSint(int val);

    /**
    * Output unsigned int value in big endian format
    * @param value Integer to be output
    * @param length number of bytes in val to output
    */
    //inline void OutputFixedLengthUint(const unsigned int& value, const int& length)
    inline void WriteUintLit(const unsigned int& value, const int& length)
    {
        for(int i = length - 1; i >= 0 ; --i)
        {
            unsigned char cp = (value >> (i * 8)) & 0xff;
            *mp_stream << cp;
        }
        m_num_bytes += length;
    }

    /**
    * Removes portion of byte-stream no longer required
    *@param count Number of bytes to be removed from beginning of stream
    */
    void RemoveRedundantBytes(const int count);

    inline void SeekGet(const int offset, std::ios_base::seekdir dir)
    {
        mp_stream->seekg(offset, dir);
    }

    /**
           * Input/output steam
           */
    std::stringstream*    mp_stream;


private:

    /**
    * ArithCodec can see internals for getting/setting bits
    */
    friend class ArithCodecBase;

    /**
    * VLC entropy coder can see internals for getting/setting bits
    */
    friend class ArithCodecToVLCAdapter;

    /**
    * Char used for temporary storage of op data bits
    */
    unsigned char m_current_byte;

    /**
    * Used to set individual bit within the current header byte
    */
    int m_current_pos;

    /**
    * Number of bytes processed
    */
    int m_num_bytes;

    /**
    * stream alloc flag
    */
    bool m_new_stream;

    /**
    * num bits left to read
    */
    int m_bits_left;
protected:


};



} // namespace dirac

#endif