uvgVPCCenc 1.0.0
uvgVPCCenc is an open-source real-time V-PCC encoder library written in C++ from scratch.
Loading...
Searching...
No Matches
bitstream_util.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 * This file is part of uvgVPCCenc V-PCC encoder.
3 *
4 * Copyright (c) 2024, 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 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ****************************************************************************/
32
33#pragma once
34
35#include <cassert>
36#include <cstring>
37#include <cstdint>
38
39#define BITSTREAM_DEBUG false
40
41/* Size of data chunks */
42#define UVG_DATA_CHUNK_SIZE 4096
43
44/* A linked list of chunks of data, used for returning the encoded data */
45typedef struct uvg_data_chunk {
46 /* Buffer for the data */
48
49 /* Number of bytes filled in this chunk */
50 uint32_t len;
51
52 /* Next chunk in the list */
55
56/* A stream of bits */
57typedef struct bitstream_t {
58 /* Total number of complete bytes */
59 uint32_t len;
60
61 /* Pointer to the first chunk, or NULL */
63
64 /* Pointer to the last chunk, or NULL */
66
67 /* The incomplete byte */
68 uint8_t data;
69
70 /* Number of bits in the incomplete byte */
71 uint8_t cur_bit;
73
74/* Initialize a new bitstream */
75void uvg_bitstream_init(bitstream_t *const stream);
76
77/* Allocates a new bitstream chunk */
79
80/* Free a list of chunks */
82
83/* Write a byte to a byte-aligned bitstream */
84void uvg_bitstream_writebyte(bitstream_t *const stream, const uint8_t byte);
85
86/* Write bits to bitstream. Buffers individual bits until they make a full byte */
87void uvg_bitstream_put(bitstream_t *const stream, const uint32_t data, uint8_t bits);
88
89/* Take chunks from a byte-aligned bitstream. Moves ownership of the chunks to the caller and clears the bitstream */
91
92/* Free resources used by a bitstream */
94
95/* Reset stream */
96void uvg_bitstream_clear(bitstream_t *const stream);
97
98/* Get the number of bits written */
99uint64_t uvg_bitstream_tell(const bitstream_t *stream);
100
101/* Write unsigned Exp-Golomb bit string */
102void uvg_bitstream_put_ue(bitstream_t *stream, uint32_t code_num);
103
104/* Calculate amount of bits required to represent a number in Exp-Golomb format */
105size_t uvg_calculate_ue_len(uint32_t number);
106
107/* Add rbsp_trailing_bits syntax element, which aligns the bitstream */
109
110/* Align the bitstream, unless it's already aligned */
111void uvg_bitstream_align(bitstream_t *const stream);
112
113/* Move data from one stream to another. Destination stream must be byte-aligned. Source stream will be cleared */
114void uvg_bitstream_move(bitstream_t *const dst, bitstream_t *const src);
115
122void uvg_bitstream_copy_bytes(bitstream_t *const stream, const uint8_t *bytes, uint32_t len);
123
124/* Get the last (possibly incomplete) byte of the bitstream */
125uint32_t uvg_bitstream_peek_last_byte(bitstream_t *const stream);
126
127/* In debug mode print out some extra info */
128#if BITSTREAM_DEBUG
129/* Counter to keep up with bits written */
130#define WRITE_U(stream, data, bits, name) \
131 { \
132 printf("%-50s u(%d) : %d\n", name, bits, data); \
133 uvg_bitstream_put(stream, data, bits); \
134 }
135#define WRITE_UE(stream, data, name) \
136 { \
137 printf("%-50s ue(v): %d\n", name, data); \
138 uvg_bitstream_put_ue(stream, data); \
139 }
140#else
141#define WRITE_U(stream, data, bits, name) \
142 { \
143 uvg_bitstream_put(stream, data, bits); \
144 }
145#define WRITE_UE(stream, data, name) \
146 { \
147 uvg_bitstream_put_ue(stream, data); \
148 }
149#endif
void uvg_bitstream_put(bitstream_t *const stream, const uint32_t data, uint8_t bits)
Definition bitstream_util.cpp:122
uvg_data_chunk * uvg_bitstream_take_chunks(bitstream_t *stream)
Definition bitstream_util.cpp:139
uint64_t uvg_bitstream_tell(const bitstream_t *stream)
Definition bitstream_util.cpp:161
void uvg_bitstream_put_ue(bitstream_t *stream, uint32_t code_num)
Definition bitstream_util.cpp:166
void uvg_bitstream_add_rbsp_trailing_bits(bitstream_t *const stream)
Definition bitstream_util.cpp:184
#define UVG_DATA_CHUNK_SIZE
Definition bitstream_util.hpp:42
uint32_t uvg_bitstream_peek_last_byte(bitstream_t *const stream)
Definition bitstream_util.cpp:265
void uvg_bitstream_init(bitstream_t *const stream)
Definition bitstream_util.cpp:72
void uvg_bitstream_align(bitstream_t *const stream)
Definition bitstream_util.cpp:191
uvg_data_chunk * uvg_bitstream_alloc_chunk()
Definition bitstream_util.cpp:74
void uvg_bitstream_writebyte(bitstream_t *const stream, const uint8_t byte)
Write a byte to bitstream.
Definition bitstream_util.cpp:99
void uvg_bitstream_free_chunks(uvg_data_chunk *chunk)
Definition bitstream_util.cpp:83
void uvg_bitstream_move(bitstream_t *const dst, bitstream_t *const src)
Definition bitstream_util.cpp:197
void uvg_bitstream_finalize(bitstream_t *stream)
Definition bitstream_util.cpp:147
void uvg_bitstream_copy_bytes(bitstream_t *const stream, const uint8_t *bytes, uint32_t len)
Copy array of bytes to a byte-aligned bitstream.
Definition bitstream_util.cpp:220
size_t uvg_calculate_ue_len(uint32_t number)
Definition bitstream_util.cpp:177
void uvg_bitstream_clear(bitstream_t *const stream)
Definition bitstream_util.cpp:156
Definition bitstream_util.hpp:57
uvg_data_chunk * first
Definition bitstream_util.hpp:62
uint8_t data
Definition bitstream_util.hpp:68
uint32_t len
Definition bitstream_util.hpp:59
uint8_t cur_bit
Definition bitstream_util.hpp:71
uvg_data_chunk * last
Definition bitstream_util.hpp:65
Definition bitstream_util.hpp:45
uint8_t data[UVG_DATA_CHUNK_SIZE]
Definition bitstream_util.hpp:47
struct uvg_data_chunk * next
Definition bitstream_util.hpp:53
uint32_t len
Definition bitstream_util.hpp:50