uvgVPCCenc 1.1.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-present, 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#include <string>
39
40#define BITSTREAM_DEBUG true
41
42/* Size of data chunks */
43#define UVG_DATA_CHUNK_SIZE 4096
44
45/* A linked list of chunks of data, used for returning the encoded data */
46typedef struct uvg_data_chunk {
47 /* Buffer for the data */
49
50 /* Number of bytes filled in this chunk */
51 uint32_t len;
52
53 /* Next chunk in the list */
56
57/* A stream of bits */
58typedef struct bitstream_t {
59 /* Total number of complete bytes */
60 uint32_t len;
61
62 /* Pointer to the first chunk, or NULL */
64
65 /* Pointer to the last chunk, or NULL */
67
68 /* The incomplete byte */
69 uint8_t data;
70
71 /* Number of bits in the incomplete byte */
72 uint8_t cur_bit;
74
75/* Initialize a new bitstream */
76void uvg_bitstream_init(bitstream_t *const stream);
77
78/* Allocates a new bitstream chunk */
80
81/* Free a list of chunks */
83
84/* Write a byte to a byte-aligned bitstream */
85void uvg_bitstream_writebyte(bitstream_t *const stream, const uint8_t byte);
86
87/* Write bits to bitstream. Buffers individual bits until they make a full byte */
88void uvg_bitstream_put(bitstream_t *const stream, const uint32_t data, uint8_t bits);
89
90/* Take chunks from a byte-aligned bitstream. Moves ownership of the chunks to the caller and clears the bitstream */
92
93/* Free resources used by a bitstream */
95
96/* Reset stream */
97void uvg_bitstream_clear(bitstream_t *const stream);
98
99/* Get the number of bits written */
100uint64_t uvg_bitstream_tell(const bitstream_t *stream);
101
102/* Write unsigned Exp-Golomb bit string */
103void uvg_bitstream_put_ue(bitstream_t *stream, uint32_t code_num);
104
105/* Calculate amount of bits required to represent a number in Exp-Golomb format */
106size_t uvg_calculate_ue_len(uint32_t number);
107
108/* Add rbsp_trailing_bits syntax element, which aligns the bitstream */
110
111/* Align the bitstream, unless it's already aligned */
112void uvg_bitstream_align(bitstream_t *const stream);
113
114/* Move data from one stream to another. Destination stream must be byte-aligned. Source stream will be cleared */
115void uvg_bitstream_move(bitstream_t *const dst, bitstream_t *const src);
116
123void uvg_bitstream_copy_bytes(bitstream_t *const stream, const uint8_t *bytes, uint32_t len);
124
125/* Get the last (possibly incomplete) byte of the bitstream */
126uint32_t uvg_bitstream_peek_last_byte(bitstream_t *const stream);
127
128/* In debug mode print out some extra info */ // lf: replaced with file export function
129// #if BITSTREAM_DEBUG
130/* Counter to keep up with bits written */
131// #define WRITE_U(stream, data, bits, name) \
132// { \
133// printf("%-50s u(%d) : %d\n", name, bits, data); \
134// uvg_bitstream_put(stream, data, bits); \
135// }
136// #define WRITE_UE(stream, data, name) \
137// { \
138// printf("%-50s ue(v): %d\n", name, data); \
139// uvg_bitstream_put_ue(stream, data); \
140// }
141// #else
142// #define WRITE_U(stream, data, bits, name) \
143// { \
144// uvg_bitstream_put(stream, data, bits); \
145// }
146// #define WRITE_UE(stream, data, name) \
147// { \
148// uvg_bitstream_put_ue(stream, data); \
149// }
150// #endif
151
152void writeU(bitstream_t *const stream, const uint32_t data, uint8_t bits, std::string name, size_t gofId);
153void writeUE(bitstream_t *const stream, const uint32_t data, std::string name, size_t gofId);
void uvg_bitstream_put(bitstream_t *const stream, const uint32_t data, uint8_t bits)
Definition bitstream_util.cpp:126
uvg_data_chunk * uvg_bitstream_take_chunks(bitstream_t *stream)
Definition bitstream_util.cpp:143
uint64_t uvg_bitstream_tell(const bitstream_t *stream)
Definition bitstream_util.cpp:165
void uvg_bitstream_put_ue(bitstream_t *stream, uint32_t code_num)
Definition bitstream_util.cpp:170
void uvg_bitstream_add_rbsp_trailing_bits(bitstream_t *const stream)
Definition bitstream_util.cpp:210
#define UVG_DATA_CHUNK_SIZE
Definition bitstream_util.hpp:43
uint32_t uvg_bitstream_peek_last_byte(bitstream_t *const stream)
Definition bitstream_util.cpp:291
void uvg_bitstream_init(bitstream_t *const stream)
Definition bitstream_util.cpp:76
void uvg_bitstream_align(bitstream_t *const stream)
Definition bitstream_util.cpp:217
uvg_data_chunk * uvg_bitstream_alloc_chunk()
Definition bitstream_util.cpp:78
void uvg_bitstream_writebyte(bitstream_t *const stream, const uint8_t byte)
Write a byte to bitstream.
Definition bitstream_util.cpp:103
void uvg_bitstream_free_chunks(uvg_data_chunk *chunk)
Definition bitstream_util.cpp:87
void uvg_bitstream_move(bitstream_t *const dst, bitstream_t *const src)
Definition bitstream_util.cpp:223
void writeU(bitstream_t *const stream, const uint32_t data, uint8_t bits, std::string name, size_t gofId)
Definition bitstream_util.cpp:180
void uvg_bitstream_finalize(bitstream_t *stream)
Definition bitstream_util.cpp:151
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:246
size_t uvg_calculate_ue_len(uint32_t number)
Definition bitstream_util.cpp:203
void uvg_bitstream_clear(bitstream_t *const stream)
Definition bitstream_util.cpp:160
void writeUE(bitstream_t *const stream, const uint32_t data, std::string name, size_t gofId)
Definition bitstream_util.cpp:191
Definition bitstream_util.hpp:58
uvg_data_chunk * first
Definition bitstream_util.hpp:63
uint8_t data
Definition bitstream_util.hpp:69
uint32_t len
Definition bitstream_util.hpp:60
uint8_t cur_bit
Definition bitstream_util.hpp:72
uvg_data_chunk * last
Definition bitstream_util.hpp:66
Definition bitstream_util.hpp:46
uint8_t data[UVG_DATA_CHUNK_SIZE]
Definition bitstream_util.hpp:48
struct uvg_data_chunk * next
Definition bitstream_util.hpp:54
uint32_t len
Definition bitstream_util.hpp:51
size_t gofId
Definition uvgvpcc.cpp:89