-
Notifications
You must be signed in to change notification settings - Fork 4
/
bs.h
238 lines (204 loc) · 5.83 KB
/
bs.h
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* h264bitstream - a library for reading and writing H.264 video
* Copyright (C) 2005-2007 Auroras Entertainment, LLC
* Copyright (C) 2008-2011 Avail-TVN
*
* Written by Alex Izvorski <aizvorski@gmail.com> and Alex Giladi <alex.giladi@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _H264_BS_H
#define _H264_BS_H 1
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
uint8_t* start;
uint8_t* p;
uint8_t* end;
int bits_left;
} bs_t;
#define _OPTIMIZE_BS_ 1
#if ( _OPTIMIZE_BS_ > 0 )
#ifndef FAST_U8
#define FAST_U8
#endif
#endif
static bs_t* bs_new(uint8_t* buf, size_t size);
static void bs_free(bs_t* b);
static bs_t* bs_clone( bs_t* dest, const bs_t* src );
static bs_t* bs_init(bs_t* b, uint8_t* buf, size_t size);
static uint32_t bs_byte_aligned(bs_t* b);
static int bs_eof(bs_t* b);
static int bs_overrun(bs_t* b);
static int bs_pos(bs_t* b);
static void bs_write_u1(bs_t* b, uint32_t v);
static void bs_write_u(bs_t* b, int n, uint32_t v);
static void bs_write_f(bs_t* b, int n, uint32_t v);
static void bs_write_u8(bs_t* b, uint32_t v);
static void bs_write_ue(bs_t* b, uint32_t v);
static void bs_write_se(bs_t* b, int32_t v);
static int bs_write_bytes(bs_t* b, uint8_t* buf, int len);
// IMPLEMENTATION
static inline bs_t* bs_init(bs_t* b, uint8_t* buf, size_t size)
{
b->start = buf;
b->p = buf;
b->end = buf + size;
b->bits_left = 8;
return b;
}
static inline bs_t* bs_new(uint8_t* buf, size_t size)
{
bs_t* b = (bs_t*)malloc(sizeof(bs_t));
bs_init(b, buf, size);
return b;
}
static inline void bs_free(bs_t* b)
{
free(b);
}
static inline bs_t* bs_clone(bs_t* dest, const bs_t* src)
{
dest->start = src->p;
dest->p = src->p;
dest->end = src->end;
dest->bits_left = src->bits_left;
return dest;
}
static inline uint32_t bs_byte_aligned(bs_t* b)
{
return (b->bits_left == 8);
}
static inline int bs_eof(bs_t* b) { if (b->p >= b->end) { return 1; } else { return 0; } }
static inline int bs_overrun(bs_t* b) { if (b->p > b->end) { return 1; } else { return 0; } }
static inline int bs_pos(bs_t* b) { if (b->p > b->end) { return (b->end - b->start); } else { return (b->p - b->start); } }
static inline int bs_bytes_left(bs_t* b) { return (b->end - b->p); }
static inline void bs_write_u1(bs_t* b, uint32_t v)
{
b->bits_left--;
if (! bs_eof(b))
{
// FIXME this is slow, but we must clear bit first
// is it better to memset(0) the whole buffer during bs_init() instead?
// if we don't do either, we introduce pretty nasty bugs
(*(b->p)) &= ~(0x01 << b->bits_left);
(*(b->p)) |= ((v & 0x01) << b->bits_left);
}
if (b->bits_left == 0) { b->p ++; b->bits_left = 8; }
}
static inline void bs_write_u(bs_t* b, int n, uint32_t v)
{
int i;
for (i = 0; i < n; i++)
{
bs_write_u1(b, (v >> ( n - i - 1 ))&0x01 );
}
}
static inline void bs_write_f(bs_t* b, int n, uint32_t v) { bs_write_u(b, n, v); }
static inline void bs_write_u8(bs_t* b, uint32_t v)
{
#ifdef FAST_U8
if (b->bits_left == 8 && ! bs_eof(b)) // can do fast write
{
b->p[0] = v;
b->p++;
return;
}
#endif
bs_write_u(b, 8, v);
}
static inline void bs_write_ue(bs_t* b, uint32_t v)
{
static const int len_table[256] =
{
1,
1,
2,2,
3,3,3,3,
4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
};
int len;
if (v == 0)
{
bs_write_u1(b, 1);
}
else
{
v++;
if (v >= 0x01000000)
{
len = 24 + len_table[ v >> 24 ];
}
else if(v >= 0x00010000)
{
len = 16 + len_table[ v >> 16 ];
}
else if(v >= 0x00000100)
{
len = 8 + len_table[ v >> 8 ];
}
else
{
len = len_table[ v ];
}
bs_write_u(b, 2*len-1, v);
}
}
static inline void bs_write_se(bs_t* b, int32_t v)
{
if (v <= 0)
{
bs_write_ue(b, -v*2);
}
else
{
bs_write_ue(b, v*2 - 1);
}
}
static inline int bs_write_bytes(bs_t* b, uint8_t* buf, int len)
{
int actual_len = len;
if (b->end - b->p < actual_len) { actual_len = b->end - b->p; }
if (actual_len < 0) { actual_len = 0; }
memcpy(b->p, buf, actual_len);
if (len < 0) { len = 0; }
b->p += len;
return actual_len;
}
#define bs_print_state(b) fprintf( stderr, "%s:%d@%s: b->p=0x%02hhX, b->left = %d\n", __FILE__, __LINE__, __FUNCTION__, *b->p, b->bits_left )
#ifdef __cplusplus
}
#endif
#endif