1 /*
2 * Bytestream functions
3 * copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@free.fr>
4 * Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifndef AVCODEC_BYTESTREAM_H
24 #define AVCODEC_BYTESTREAM_H
25
26 #include <stdint.h>
27 #include <string.h>
28
32
36
41
42 #define DEF(type, name, bytes, read, write) \
43 static av_always_inline type bytestream_get_ ## name(const uint8_t **b) \
44 { \
45 (*b) += bytes; \
46 return read(*b - bytes); \
47 } \
48 static av_always_inline void bytestream_put_ ## name(uint8_t **b, \
49 const type value) \
50 { \
51 write(*b, value); \
52 (*b) += bytes; \
53 } \
54 static av_always_inline void bytestream2_put_ ## name ## u(PutByteContext *p, \
55 const type value) \
56 { \
57 bytestream_put_ ## name(&p->buffer, value); \
58 } \
59 static av_always_inline void bytestream2_put_ ## name(PutByteContext *p, \
60 const type value) \
61 { \
62 if (!p->eof && (p->buffer_end - p->buffer >= bytes)) { \
63 write(p->buffer, value); \
64 p->buffer += bytes; \
65 } else \
66 p->eof = 1; \
67 } \
68 static av_always_inline type bytestream2_get_ ## name ## u(GetByteContext *g) \
69 { \
70 return bytestream_get_ ## name(&g->buffer); \
71 } \
72 static av_always_inline type bytestream2_get_ ## name(GetByteContext *g) \
73 { \
74 if (g->buffer_end - g->buffer < bytes) { \
75 g->buffer = g->buffer_end; \
76 return 0; \
77 } \
78 return bytestream2_get_ ## name ## u(g); \
79 } \
80 static av_always_inline type bytestream2_peek_ ## name(GetByteContext *g) \
81 { \
82 if (g->buffer_end - g->buffer < bytes) \
83 return 0; \
84 return read(g->buffer); \
85 }
86
96
97 #if AV_HAVE_BIGENDIAN
98 # define bytestream2_get_ne16 bytestream2_get_be16
99 # define bytestream2_get_ne24 bytestream2_get_be24
100 # define bytestream2_get_ne32 bytestream2_get_be32
101 # define bytestream2_get_ne64 bytestream2_get_be64
102 # define bytestream2_get_ne16u bytestream2_get_be16u
103 # define bytestream2_get_ne24u bytestream2_get_be24u
104 # define bytestream2_get_ne32u bytestream2_get_be32u
105 # define bytestream2_get_ne64u bytestream2_get_be64u
106 # define bytestream2_put_ne16 bytestream2_put_be16
107 # define bytestream2_put_ne24 bytestream2_put_be24
108 # define bytestream2_put_ne32 bytestream2_put_be32
109 # define bytestream2_put_ne64 bytestream2_put_be64
110 # define bytestream2_peek_ne16 bytestream2_peek_be16
111 # define bytestream2_peek_ne24 bytestream2_peek_be24
112 # define bytestream2_peek_ne32 bytestream2_peek_be32
113 # define bytestream2_peek_ne64 bytestream2_peek_be64
114 #else
115 # define bytestream2_get_ne16 bytestream2_get_le16
116 # define bytestream2_get_ne24 bytestream2_get_le24
117 # define bytestream2_get_ne32 bytestream2_get_le32
118 # define bytestream2_get_ne64 bytestream2_get_le64
119 # define bytestream2_get_ne16u bytestream2_get_le16u
120 # define bytestream2_get_ne24u bytestream2_get_le24u
121 # define bytestream2_get_ne32u bytestream2_get_le32u
122 # define bytestream2_get_ne64u bytestream2_get_le64u
123 # define bytestream2_put_ne16 bytestream2_put_le16
124 # define bytestream2_put_ne24 bytestream2_put_le24
125 # define bytestream2_put_ne32 bytestream2_put_le32
126 # define bytestream2_put_ne64 bytestream2_put_le64
127 # define bytestream2_peek_ne16 bytestream2_peek_le16
128 # define bytestream2_peek_ne24 bytestream2_peek_le24
129 # define bytestream2_peek_ne32 bytestream2_peek_le32
130 # define bytestream2_peek_ne64 bytestream2_peek_le64
131 #endif
132
135 int buf_size)
136 {
141 }
142
145 int buf_size)
146 {
152 }
153
155 {
157 }
158
160 {
162 }
163
166 {
168 }
169
172 {
174 }
175
178 {
179 int size2;
181 return;
183 if (size2 != size)
186 }
187
189 {
191 }
192
194 {
196 }
197
199 {
201 }
202
204 {
206 }
207
210 int whence)
211 {
212 switch (whence) {
213 case SEEK_CUR:
217 break;
218 case SEEK_END:
221 break;
222 case SEEK_SET:
225 break;
226 default:
228 }
230 }
231
234 int whence)
235 {
237 switch (whence) {
238 case SEEK_CUR:
244 break;
245 case SEEK_END:
246 if (offset > 0)
250 break;
251 case SEEK_SET:
256 break;
257 default:
259 }
261 }
262
266 {
268 memcpy(dst, g->
buffer, size2);
270 return size2;
271 }
272
276 {
277 memcpy(dst, g->
buffer, size);
280 }
281
285 {
286 int size2;
288 return 0;
290 if (size2 != size)
292 memcpy(p->
buffer, src, size2);
294 return size2;
295 }
296
300 {
301 memcpy(p->
buffer, src, size);
304 }
305
309 {
310 int size2;
312 return;
314 if (size2 != size)
316 memset(p->
buffer, c, size2);
318 }
319
323 {
324 memset(p->
buffer, c, size);
326 }
327
329 {
331 }
332
336 {
341 }
342
346 {
347 int size2;
348
350 return 0;
353 if (size2 != size)
355
357 }
358
362 {
363 memcpy(dst, *b, size);
366 }
367
371 {
372 memcpy(*b, src, size);
374 }
375
376 #endif /* AVCODEC_BYTESTREAM_H */
static av_always_inline void bytestream2_set_bufferu(PutByteContext *p, const uint8_t c, unsigned int size)
static av_always_inline void bytestream2_set_buffer(PutByteContext *p, const uint8_t c, unsigned int size)
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
uint64_t_TMPL AV_WL64 unsigned int_TMPL le32
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define DEF(type, name, bytes, read, write)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL be24
static av_always_inline int bytestream2_size(GetByteContext *g)
static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL be64
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
simple assert() macros that are a bit more flexible than ISO C assert().
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
static const uint8_t offset[127][2]
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL be16
const uint8_t * buffer_end
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL le24
static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset, int whence)
static av_always_inline int bytestream2_tell(GetByteContext *g)
static av_always_inline unsigned int bytestream2_copy_bufferu(PutByteContext *p, GetByteContext *g, unsigned int size)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL AV_RB8
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
static av_always_inline unsigned int bytestream2_put_bufferu(PutByteContext *p, const uint8_t *src, unsigned int size)
const uint8_t * buffer_start
static av_always_inline unsigned int bytestream2_copy_buffer(PutByteContext *p, GetByteContext *g, unsigned int size)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL be32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL le16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
common internal and external API header
static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
static av_always_inline int bytestream2_size_p(PutByteContext *p)