File: bitReader.h

package info (click to toggle)
libyami 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,152 kB
  • sloc: cpp: 44,247; ansic: 1,255; makefile: 728; lisp: 479; sh: 21; python: 19
file content (117 lines) | stat: -rw-r--r-- 3,251 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2016 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef bitReader_h
#define bitReader_h

#include <stdint.h>
#include <algorithm> /*std::min*/

namespace YamiParser {

class BitReader {
public:
    static const uint32_t CACHEBYTES;
    BitReader(const uint8_t* data, uint32_t size);
    virtual ~BitReader() {}

    /* Read specified bits(<= 8*sizeof(uint32_t)) as a uint32_t to v */
    /* if not enough data, it will return false, eat all data and keep v untouched */
    bool read(uint32_t& v, uint32_t nbits);
    /* Read specified bits(<= 8*sizeof(uint32_t)) as a uint32_t return value */
    /* will return 0 if not enough data*/
    uint32_t read(uint32_t nbits);

    /*TODO: change to this to read, and change read to readBits */
    template <class T>
    inline bool readT(T& v, uint32_t nbits);

    template <class T>
    inline bool readT(T& v);

    inline bool readT(bool& v);

    /*read the next nbits bits from the bitstream but not advance the bitstream pointer*/
    uint32_t peek(uint32_t nbits) const;

    /* this version will check read beyond boundary */
    template <class T>
    bool peek(T& v, uint32_t nbits) const;

    bool skip(uint32_t nbits);

    /* Get the total bits that had been read from bitstream,
     * For the subclass NalReader, this pos already removed the Emulation Prevent Byte*/
    uint64_t getPos() const
    {
        return m_pos;
    }

    uint64_t getRemainingBitsCount() const
    {
        return m_bitsInCache + (static_cast<uint64_t>(m_size - m_loadBytes) << 3);
    }

    bool end() const
    {
        return !m_bitsInCache && (m_size == m_loadBytes);
    }

protected:
    virtual void loadDataToCache(uint32_t nbytes);

    const uint8_t* m_stream; /*a pointer to source data*/
    uint32_t m_size; /*the size of source data in bytes*/
    unsigned long int m_cache; /*the buffer which load less than or equal to 8 bytes*/
    uint32_t m_loadBytes; /*the total bytes of data read from source data*/
    uint32_t m_bitsInCache; /*the remaining bits in cache*/
    uint64_t m_pos; /*current pos, for NalReader, it already removed emulation prevent byte*/
private:
    inline uint32_t extractBitsFromCache(uint32_t nbits);
    inline void reload();
};

template <class T>
bool BitReader::readT(T& v, uint32_t nbits)
{
    uint32_t tmp;
    if (!read(tmp, nbits))
        return false;
    v = tmp;
    return true;
}

template <class T>
bool BitReader::readT(T& v)
{
    return readT(v, sizeof(v) << 3);
}

bool BitReader::readT(bool& v)
{
    return readT(v, 1);
}

template <class T>
bool BitReader::peek(T& v, uint32_t nbits) const
{
    BitReader tmp(*this);
    return tmp.readT(v, nbits);
}

} /*namespace YamiParser*/

#endif