forked from i-rinat/libvdpau-va-gl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitstream.c
145 lines (122 loc) · 3.05 KB
/
bitstream.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2013 Rinat Ibragimov
*
* This file is part of libvdpau-va-gl
*
* libvdpau-va-gl distributed under the terms of LGPLv3. See COPYING for details.
*/
#include "bitstream.h"
#include <assert.h>
#include <string.h>
inline
void
rbsp_attach_buffer(rbsp_state_t *state, const uint8_t *buf, size_t byte_count)
{
state->buf_ptr = buf;
state->byte_count = byte_count;
state->cur_ptr = buf;
state->bit_ptr = 7;
state->zeros_in_row = 0;
state->bits_eaten = 0;
}
rbsp_state_t
rbsp_copy_state(rbsp_state_t *state)
{
return *state;
}
inline
int
rbsp_navigate_to_nal_unit(rbsp_state_t *state)
{
int found = 1;
int window[3] = {-1, -1, -1};
do {
if (state->cur_ptr >= state->buf_ptr + state->byte_count) {
found = 0; // no bytes left, no nal unit found
break;
}
int c = *state->cur_ptr++;
window[0] = window[1];
window[1] = window[2];
window[2] = c;
} while (0 != window[0] || 0 != window[1] || 1 != window[2]);
if (found)
return (int)(state->cur_ptr - state->buf_ptr);
return -1;
}
inline
void
rbsp_reset_bit_counter(rbsp_state_t *state)
{
state->bits_eaten = 0;
}
inline
int
rbsp_consume_byte(rbsp_state_t *state)
{
if (state->cur_ptr >= state->buf_ptr + state->byte_count)
return -1;
uint8_t c = *state->cur_ptr++;
if (0 == c) state->zeros_in_row ++;
else state->zeros_in_row = 0;
if (state->zeros_in_row >= 2) {
uint8_t epb = *state->cur_ptr++;
if (0 != epb) state->zeros_in_row = 0;
// if epb is not actually have 0x03 value, it's not an emulation prevention
if (0x03 != epb) state->cur_ptr--; // so rewind
}
return c;
}
inline
int
rbsp_consume_bit(rbsp_state_t *state)
{
assert (state->cur_ptr < state->buf_ptr + state->byte_count);
int value = !!(*state->cur_ptr & (1 << state->bit_ptr));
if (state->bit_ptr > 0) {
state->bit_ptr --;
} else {
state->bit_ptr = 7;
rbsp_consume_byte(state); // handles emulation prevention bytes
}
state->bits_eaten += 1;
return value;
}
inline
unsigned int
rbsp_get_u(rbsp_state_t *state, int bitcount)
{
unsigned int value = 0;
for (int k = 0; k < bitcount; k ++)
value = (value << 1) + rbsp_consume_bit(state);
return value;
}
inline
unsigned int
rbsp_get_uev(rbsp_state_t *state)
{
int zerobit_count = -1;
int current_bit = 0;
do {
zerobit_count ++;
current_bit = rbsp_consume_bit(state);
} while (0 == current_bit);
if (0 == zerobit_count) return 0;
return (1 << zerobit_count) - 1 + rbsp_get_u(state, zerobit_count);
}
inline
int
rbsp_get_sev(rbsp_state_t *state)
{
int zerobit_count = -1;
int current_bit = 0;
do {
zerobit_count ++;
current_bit = rbsp_consume_bit(state);
} while (0 == current_bit);
if (0 == zerobit_count) return 0;
int value = (1 << zerobit_count) + rbsp_get_u(state, zerobit_count);
if (value & 1)
return -value/2;
return value/2;
}