File: bitWriter.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 (67 lines) | stat: -rw-r--r-- 2,012 bytes parent folder | download | duplicates (3)
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
/*
 * 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 bitWriter_h
#define bitWriter_h

#include <stdint.h>
#include <vector>

namespace YamiParser {

#define BIT_WRITER_DEFAULT_BUFFER_SIZE 0x1000

class BitWriter {
public:
    /* size is used to set bit stream buffer capacity,
        a default buffer size may be set to avoid memory reallocated frequently
       */
    BitWriter(uint32_t size = BIT_WRITER_DEFAULT_BUFFER_SIZE);

    /* Write a value with numBits into bitstream */
    bool writeBits(uint32_t value, uint32_t numBits);

    /* Write an array with numBytes into bitstream */
    bool writeBytes(uint8_t* data, uint32_t numBytes);

    /* Pad some zeros to make sure bitsteam byte aligned */
    void writeToBytesAligned(bool bit = false);

    /* get encoded bitstream buffer */
    uint8_t* getBitWriterData();

    /* get encoded bits count
     * it is recommended to call getCodedBitsCount prior to getBitWriterData,
     * because getBitWriterData may pad some bits to make sure byte aligned,
     * then the padded bits will be caculated as coded bits.
     * */
    uint64_t getCodedBitsCount() const
    {
        return static_cast<uint64_t>(m_bs.size() * 8) + m_bitsInCache;
    }

protected:
    void flushCache();

    std::vector<uint8_t> m_bs; /* encoded bitstream buffer */

    long int m_cache; /* a 32 or 64 bits cache buffer */
    uint32_t m_bitsInCache; /* used bits in cache*/
};

} /*namespace YamiParser*/

#endif