Libav
bytestream.h
Go to the documentation of this file.
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 
29 #include "libavutil/common.h"
30 #include "libavutil/intreadwrite.h"
31 
32 typedef struct GetByteContext {
35 
36 typedef struct PutByteContext {
38  int eof;
40 
41 #define DEF(type, name, bytes, read, write) \
42 static av_always_inline type bytestream_get_ ## name(const uint8_t **b) \
43 { \
44  (*b) += bytes; \
45  return read(*b - bytes); \
46 } \
47 static av_always_inline void bytestream_put_ ## name(uint8_t **b, \
48  const type value) \
49 { \
50  write(*b, value); \
51  (*b) += bytes; \
52 } \
53 static av_always_inline void bytestream2_put_ ## name ## u(PutByteContext *p, \
54  const type value) \
55 { \
56  bytestream_put_ ## name(&p->buffer, value); \
57 } \
58 static av_always_inline void bytestream2_put_ ## name(PutByteContext *p, \
59  const type value) \
60 { \
61  if (!p->eof && (p->buffer_end - p->buffer >= bytes)) { \
62  write(p->buffer, value); \
63  p->buffer += bytes; \
64  } else \
65  p->eof = 1; \
66 } \
67 static av_always_inline type bytestream2_get_ ## name ## u(GetByteContext *g) \
68 { \
69  return bytestream_get_ ## name(&g->buffer); \
70 } \
71 static av_always_inline type bytestream2_get_ ## name(GetByteContext *g) \
72 { \
73  if (g->buffer_end - g->buffer < bytes) { \
74  g->buffer = g->buffer_end; \
75  return 0; \
76  } \
77  return bytestream2_get_ ## name ## u(g); \
78 } \
79 static av_always_inline type bytestream2_peek_ ## name(GetByteContext *g) \
80 { \
81  if (g->buffer_end - g->buffer < bytes) \
82  return 0; \
83  return read(g->buffer); \
84 }
85 
86 DEF(uint64_t, le64, 8, AV_RL64, AV_WL64)
87 DEF(unsigned int, le32, 4, AV_RL32, AV_WL32)
88 DEF(unsigned int, le24, 3, AV_RL24, AV_WL24)
89 DEF(unsigned int, le16, 2, AV_RL16, AV_WL16)
90 DEF(uint64_t, be64, 8, AV_RB64, AV_WB64)
91 DEF(unsigned int, be32, 4, AV_RB32, AV_WB32)
92 DEF(unsigned int, be24, 3, AV_RB24, AV_WB24)
93 DEF(unsigned int, be16, 2, AV_RB16, AV_WB16)
94 DEF(unsigned int, byte, 1, AV_RB8 , AV_WB8)
95 
96 #if HAVE_BIGENDIAN
97 # define bytestream2_get_ne16 bytestream2_get_be16
98 # define bytestream2_get_ne24 bytestream2_get_be24
99 # define bytestream2_get_ne32 bytestream2_get_be32
100 # define bytestream2_get_ne64 bytestream2_get_be64
101 # define bytestream2_get_ne16u bytestream2_get_be16u
102 # define bytestream2_get_ne24u bytestream2_get_be24u
103 # define bytestream2_get_ne32u bytestream2_get_be32u
104 # define bytestream2_get_ne64u bytestream2_get_be64u
105 # define bytestream2_put_ne16 bytestream2_put_be16
106 # define bytestream2_put_ne24 bytestream2_put_be24
107 # define bytestream2_put_ne32 bytestream2_put_be32
108 # define bytestream2_put_ne64 bytestream2_put_be64
109 # define bytestream2_peek_ne16 bytestream2_peek_be16
110 # define bytestream2_peek_ne24 bytestream2_peek_be24
111 # define bytestream2_peek_ne32 bytestream2_peek_be32
112 # define bytestream2_peek_ne64 bytestream2_peek_be64
113 #else
114 # define bytestream2_get_ne16 bytestream2_get_le16
115 # define bytestream2_get_ne24 bytestream2_get_le24
116 # define bytestream2_get_ne32 bytestream2_get_le32
117 # define bytestream2_get_ne64 bytestream2_get_le64
118 # define bytestream2_get_ne16u bytestream2_get_le16u
119 # define bytestream2_get_ne24u bytestream2_get_le24u
120 # define bytestream2_get_ne32u bytestream2_get_le32u
121 # define bytestream2_get_ne64u bytestream2_get_le64u
122 # define bytestream2_put_ne16 bytestream2_put_le16
123 # define bytestream2_put_ne24 bytestream2_put_le24
124 # define bytestream2_put_ne32 bytestream2_put_le32
125 # define bytestream2_put_ne64 bytestream2_put_le64
126 # define bytestream2_peek_ne16 bytestream2_peek_le16
127 # define bytestream2_peek_ne24 bytestream2_peek_le24
128 # define bytestream2_peek_ne32 bytestream2_peek_le32
129 # define bytestream2_peek_ne64 bytestream2_peek_le64
130 #endif
131 
133  const uint8_t *buf,
134  int buf_size)
135 {
136  g->buffer = buf;
137  g->buffer_start = buf;
138  g->buffer_end = buf + buf_size;
139 }
140 
142  uint8_t *buf,
143  int buf_size)
144 {
145  p->buffer = buf;
146  p->buffer_start = buf;
147  p->buffer_end = buf + buf_size;
148  p->eof = 0;
149 }
150 
152 {
153  return g->buffer_end - g->buffer;
154 }
155 
157 {
158  return p->buffer_end - p->buffer;
159 }
160 
162  unsigned int size)
163 {
164  g->buffer += FFMIN(g->buffer_end - g->buffer, size);
165 }
166 
168  unsigned int size)
169 {
170  g->buffer += size;
171 }
172 
174  unsigned int size)
175 {
176  int size2;
177  if (p->eof)
178  return;
179  size2 = FFMIN(p->buffer_end - p->buffer, size);
180  if (size2 != size)
181  p->eof = 1;
182  p->buffer += size2;
183 }
184 
186 {
187  return (int)(g->buffer - g->buffer_start);
188 }
189 
191 {
192  return (int)(p->buffer - p->buffer_start);
193 }
194 
196 {
197  return (int)(g->buffer_end - g->buffer_start);
198 }
199 
201 {
202  return (int)(p->buffer_end - p->buffer_start);
203 }
204 
206  int offset,
207  int whence)
208 {
209  switch (whence) {
210  case SEEK_CUR:
211  offset = av_clip(offset, -(g->buffer - g->buffer_start),
212  g->buffer_end - g->buffer);
213  g->buffer += offset;
214  break;
215  case SEEK_END:
216  offset = av_clip(offset, -(g->buffer_end - g->buffer_start), 0);
217  g->buffer = g->buffer_end + offset;
218  break;
219  case SEEK_SET:
220  offset = av_clip(offset, 0, g->buffer_end - g->buffer_start);
221  g->buffer = g->buffer_start + offset;
222  break;
223  default:
224  return AVERROR(EINVAL);
225  }
226  return bytestream2_tell(g);
227 }
228 
230  int offset,
231  int whence)
232 {
233  p->eof = 0;
234  switch (whence) {
235  case SEEK_CUR:
236  if (p->buffer_end - p->buffer < offset)
237  p->eof = 1;
238  offset = av_clip(offset, -(p->buffer - p->buffer_start),
239  p->buffer_end - p->buffer);
240  p->buffer += offset;
241  break;
242  case SEEK_END:
243  if (offset > 0)
244  p->eof = 1;
245  offset = av_clip(offset, -(p->buffer_end - p->buffer_start), 0);
246  p->buffer = p->buffer_end + offset;
247  break;
248  case SEEK_SET:
249  if (p->buffer_end - p->buffer_start < offset)
250  p->eof = 1;
251  offset = av_clip(offset, 0, p->buffer_end - p->buffer_start);
252  p->buffer = p->buffer_start + offset;
253  break;
254  default:
255  return AVERROR(EINVAL);
256  }
257  return bytestream2_tell_p(p);
258 }
259 
261  uint8_t *dst,
262  unsigned int size)
263 {
264  int size2 = FFMIN(g->buffer_end - g->buffer, size);
265  memcpy(dst, g->buffer, size2);
266  g->buffer += size2;
267  return size2;
268 }
269 
271  uint8_t *dst,
272  unsigned int size)
273 {
274  memcpy(dst, g->buffer, size);
275  g->buffer += size;
276  return size;
277 }
278 
280  const uint8_t *src,
281  unsigned int size)
282 {
283  int size2;
284  if (p->eof)
285  return 0;
286  size2 = FFMIN(p->buffer_end - p->buffer, size);
287  if (size2 != size)
288  p->eof = 1;
289  memcpy(p->buffer, src, size2);
290  p->buffer += size2;
291  return size2;
292 }
293 
295  const uint8_t *src,
296  unsigned int size)
297 {
298  memcpy(p->buffer, src, size);
299  p->buffer += size;
300  return size;
301 }
302 
304  const uint8_t c,
305  unsigned int size)
306 {
307  int size2;
308  if (p->eof)
309  return;
310  size2 = FFMIN(p->buffer_end - p->buffer, size);
311  if (size2 != size)
312  p->eof = 1;
313  memset(p->buffer, c, size2);
314  p->buffer += size2;
315 }
316 
318  const uint8_t c,
319  unsigned int size)
320 {
321  memset(p->buffer, c, size);
322  p->buffer += size;
323 }
324 
326 {
327  return p->eof;
328 }
329 
331  GetByteContext *g,
332  unsigned int size)
333 {
334  memcpy(p->buffer, g->buffer, size);
335  p->buffer += size;
336  g->buffer += size;
337  return size;
338 }
339 
341  GetByteContext *g,
342  unsigned int size)
343 {
344  int size2;
345 
346  if (p->eof)
347  return 0;
348  size = FFMIN(g->buffer_end - g->buffer, size);
349  size2 = FFMIN(p->buffer_end - p->buffer, size);
350  if (size2 != size)
351  p->eof = 1;
352 
353  return bytestream2_copy_bufferu(p, g, size2);
354 }
355 
356 static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b,
357  uint8_t *dst,
358  unsigned int size)
359 {
360  memcpy(dst, *b, size);
361  (*b) += size;
362  return size;
363 }
364 
366  const uint8_t *src,
367  unsigned int size)
368 {
369  memcpy(*b, src, size);
370  (*b) += size;
371 }
372 
373 #endif /* AVCODEC_BYTESTREAM_H */
#define AV_RB8(x)
Definition: intreadwrite.h:357
#define AV_WL64(p, d)
Definition: intreadwrite.h:299
static av_always_inline void bytestream2_set_bufferu(PutByteContext *p, const uint8_t c, unsigned int size)
Definition: bytestream.h:317
static av_always_inline void bytestream2_set_buffer(PutByteContext *p, const uint8_t c, unsigned int size)
Definition: bytestream.h:303
int size
#define AV_RB64
Definition: intreadwrite.h:164
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:141
#define AV_WB8(p, d)
Definition: intreadwrite.h:358
#define AV_RB24
Definition: intreadwrite.h:64
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:132
#define AV_RL16
Definition: intreadwrite.h:42
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:270
#define AV_RL64
Definition: intreadwrite.h:173
#define AV_WL24(p, d)
Definition: intreadwrite.h:426
uint8_t
#define AV_WB32(p, d)
Definition: intreadwrite.h:239
#define DEF(type, name, bytes, read, write)
Definition: bytestream.h:41
#define AV_RB32
Definition: intreadwrite.h:130
#define b
Definition: input.c:52
#define AV_WL32(p, d)
Definition: intreadwrite.h:255
const uint8_t * buffer
Definition: bytestream.h:33
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:167
static av_always_inline int bytestream2_size(GetByteContext *g)
Definition: bytestream.h:195
static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
Definition: bytestream.h:156
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:161
#define AV_WB64(p, d)
Definition: intreadwrite.h:275
g
Definition: yuv2rgb.c:535
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:260
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:151
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:190
const uint8_t * buffer_end
Definition: bytestream.h:33
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition: bytestream.h:173
uint8_t * buffer_start
Definition: bytestream.h:37
#define FFMIN(a, b)
Definition: common.h:57
#define AV_WB24(p, d)
Definition: intreadwrite.h:412
#define AV_RL32
Definition: intreadwrite.h:146
static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
Definition: bytestream.h:356
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition: bytestream.h:279
static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset, int whence)
Definition: bytestream.h:229
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:185
static av_always_inline unsigned int bytestream2_copy_bufferu(PutByteContext *p, GetByteContext *g, unsigned int size)
Definition: bytestream.h:330
uint8_t * buffer
Definition: bytestream.h:37
static av_always_inline unsigned int bytestream2_put_bufferu(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition: bytestream.h:294
const uint8_t * buffer_start
Definition: bytestream.h:33
static av_always_inline unsigned int bytestream2_copy_buffer(PutByteContext *p, GetByteContext *g, unsigned int size)
Definition: bytestream.h:340
#define AV_WB16(p, d)
Definition: intreadwrite.h:213
#define AV_RL24
Definition: intreadwrite.h:78
common internal and external API header
static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
Definition: bytestream.h:325
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:365
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:205
#define av_always_inline
Definition: attributes.h:40
#define AV_WL16(p, d)
Definition: intreadwrite.h:225
static av_always_inline int bytestream2_size_p(PutByteContext *p)
Definition: bytestream.h:200
uint8_t * buffer_end
Definition: bytestream.h:37