libxcoder  3.5.1
ni_bitstream_logan.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * This file is part of Kvazaar HEVC encoder.
3  *
4  * Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice, this
14  * list of conditions and the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  *
17  * * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  ****************************************************************************/
32 
33 /*!*****************************************************************************
34 * \file ni_bitstream_logan.h
35 *
36 * \brief Utility functions to operate on bits in a bitstream
37 *
38 *******************************************************************************/
39 
40 #pragma once
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #ifdef _WIN32
47  #ifdef XCODER_DLL
48  #ifdef LIB_EXPORTS
49  #define LIB_API_BITSTREAM __declspec(dllexport)
50  #else
51  #define LIB_API_BITSTREAM __declspec(dllimport)
52  #endif
53  #else
54  #define LIB_API_BITSTREAM
55  #endif
56 #elif defined(__linux__) || defined(__APPLE__)
57  #define LIB_API_BITSTREAM
58 #endif
59 
60 #ifndef QUADRA
61 
62 // the following is for bitstream put operations
63 #define NI_DATA_CHUNK_SIZE 4096
64 
65 typedef struct ni_data_chunk_t
66 {
67  // buffer for the data
69 
70  // number of bytes filled in this chunk
71  uint32_t len;
72 
73  // next chunk in the list
76 
77 // bitstream writer and operations
78 typedef struct _ni_bitstream_writer_t
79 {
80  // total number of complete bytes
81  uint32_t len;
82 
83  // pointer to the first chunk of data, or NULL
85 
86  // pointer to the last chunk of data, or NULL
88 
89  // the incomplete byte
90  uint8_t data;
91 
92  // number of bits in the incomplete byte
93  uint8_t cur_bit;
95 
96 // bitstream writer init
98 
99 // get the number of bits written to bitstream so far
100 uint64_t ni_bs_writer_tell(const ni_bitstream_writer_t *const stream);
101 
102 // write a specified number (<= 32) of bits to bitstream
103 void ni_bs_writer_put(ni_bitstream_writer_t *stream, uint32_t data,
104  uint8_t bits);
105 
106 // write unsigned/signed Exp-Golomb bit string to bitstream, 2^32-2 at most
107 void ni_bs_writer_put_ue(ni_bitstream_writer_t *stream, uint32_t data);
108 void ni_bs_writer_put_se(ni_bitstream_writer_t *stream, int32_t data);
109 
110 // align the bitstream with zero
112 
113 // copy bitstream data to dst
114 void ni_bs_writer_copy(uint8_t *dst, const ni_bitstream_writer_t *stream);
115 
116 // clear and reset bitstream
118 
119 // the following is for bitstream get operations
120 
121 // bitstream reader and operations
123 {
124  const uint8_t *buf; // data
125  int byte_offset; // byte offset of the current read position
126  int bit_offset; // bit offset of the current read position
127  int size_in_bits; // number of total bits in data
129 
130 // bitstream reader init
131 LIB_API_BITSTREAM void ni_bitstream_reader_init(ni_bitstream_reader_t *br,
132  const uint8_t *data,
133  int bit_size);
134 
135 // return number of bits already parsed
136 LIB_API_BITSTREAM int ni_bs_reader_bits_count(ni_bitstream_reader_t *br);
137 
138 // return number of bits left to parse
139 LIB_API_BITSTREAM int ni_bs_reader_get_bits_left(ni_bitstream_reader_t *br);
140 
141 // skip a number of bits ahead in the bitstream reader
142 LIB_API_BITSTREAM void ni_bs_reader_skip_bits(ni_bitstream_reader_t *br, int n);
143 
144 // read bits (up to 32) from the bitstream reader, after reader init
145 LIB_API_BITSTREAM uint32_t ni_bs_reader_get_bits(ni_bitstream_reader_t *br, int n);
146 
147 // read an unsigned Exp-Golomb code ue(v)
148 LIB_API_BITSTREAM uint32_t ni_bs_reader_get_ue(ni_bitstream_reader_t *br);
149 
150 // read a signed Exp-Golomb code se(v)
151 LIB_API_BITSTREAM int32_t ni_bs_reader_get_se(ni_bitstream_reader_t *br);
152 
153 #endif // #ifndef QUADRA
154 
155 #ifdef __cplusplus
156 }
157 #endif
LIB_API_BITSTREAM int ni_bs_reader_bits_count(ni_bitstream_reader_t *br)
return the number of bits already parsed in stream
LIB_API_BITSTREAM int32_t ni_bs_reader_get_se(ni_bitstream_reader_t *br)
read a signed Exp-Golomb code se(v)
void ni_bs_writer_put_ue(ni_bitstream_writer_t *stream, uint32_t data)
write unsigned Exp-Golomb bit string to bitstream, 2^32-2 at most.
void ni_bitstream_writer_init(ni_bitstream_writer_t *stream)
init a bitstream writer
void ni_bs_writer_copy(uint8_t *dst, const ni_bitstream_writer_t *stream)
copy bitstream data to dst Note: caller must ensure sufficient space in dst
#define NI_DATA_CHUNK_SIZE
LIB_API_BITSTREAM uint32_t ni_bs_reader_get_bits(ni_bitstream_reader_t *br, int n)
read bits (up to 32) from the bitstream reader, after reader init
void ni_bs_writer_align_zero(ni_bitstream_writer_t *stream)
align the bitstream with zero
LIB_API_BITSTREAM void ni_bitstream_reader_init(ni_bitstream_reader_t *br, const uint8_t *data, int bit_size)
init a bitstream reader Note: bitstream_reader takes reading ownership of the data
void ni_bs_writer_clear(ni_bitstream_writer_t *stream)
clear and reset bitstream
struct ni_data_chunk_t ni_data_chunk_t
uint64_t ni_bs_writer_tell(const ni_bitstream_writer_t *const stream)
return the number of bits written to bitstream so far
struct _ni_bitstream_writer_t ni_bitstream_writer_t
void ni_bs_writer_put(ni_bitstream_writer_t *stream, uint32_t data, uint8_t bits)
write a specified number (<= 32) of bits to bitstream, buffer individual bits until a full byte is ma...
struct _ni_bitstream_reader_t ni_bitstream_reader_t
LIB_API_BITSTREAM int ni_bs_reader_get_bits_left(ni_bitstream_reader_t *br)
return the number of bits left to parse in stream
void ni_bs_writer_put_se(ni_bitstream_writer_t *stream, int32_t data)
LIB_API_BITSTREAM void ni_bs_reader_skip_bits(ni_bitstream_reader_t *br, int n)
skip a number of bits ahead in the bitstream reader
LIB_API_BITSTREAM uint32_t ni_bs_reader_get_ue(ni_bitstream_reader_t *br)
read an unsigned Exp-Golomb code ue(v)
ni_data_chunk_t * first
ni_data_chunk_t * last
struct ni_data_chunk_t * next
uint8_t data[NI_DATA_CHUNK_SIZE]