FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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 
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  return 0; \
75  return bytestream2_get_ ## name ## u(g); \
76 } \
77 static av_always_inline type bytestream2_peek_ ## name(GetByteContext *g) \
78 { \
79  if (g->buffer_end - g->buffer < bytes) \
80  return 0; \
81  return read(g->buffer); \
82 }
83 
84 DEF(uint64_t, le64, 8, AV_RL64, AV_WL64)
85 DEF(unsigned int, le32, 4, AV_RL32, AV_WL32)
86 DEF(unsigned int, le24, 3, AV_RL24, AV_WL24)
87 DEF(unsigned int, le16, 2, AV_RL16, AV_WL16)
88 DEF(uint64_t, be64, 8, AV_RB64, AV_WB64)
89 DEF(unsigned int, be32, 4, AV_RB32, AV_WB32)
90 DEF(unsigned int, be24, 3, AV_RB24, AV_WB24)
91 DEF(unsigned int, be16, 2, AV_RB16, AV_WB16)
92 DEF(unsigned int, byte, 1, AV_RB8 , AV_WB8)
93 
94 #if HAVE_BIGENDIAN
95 # define bytestream2_get_ne16 bytestream2_get_be16
96 # define bytestream2_get_ne24 bytestream2_get_be24
97 # define bytestream2_get_ne32 bytestream2_get_be32
98 # define bytestream2_get_ne64 bytestream2_get_be64
99 # define bytestream2_get_ne16u bytestream2_get_be16u
100 # define bytestream2_get_ne24u bytestream2_get_be24u
101 # define bytestream2_get_ne32u bytestream2_get_be32u
102 # define bytestream2_get_ne64u bytestream2_get_be64u
103 # define bytestream2_put_ne16 bytestream2_put_be16
104 # define bytestream2_put_ne24 bytestream2_put_be24
105 # define bytestream2_put_ne32 bytestream2_put_be32
106 # define bytestream2_put_ne64 bytestream2_put_be64
107 # define bytestream2_peek_ne16 bytestream2_peek_be16
108 # define bytestream2_peek_ne24 bytestream2_peek_be24
109 # define bytestream2_peek_ne32 bytestream2_peek_be32
110 # define bytestream2_peek_ne64 bytestream2_peek_be64
111 #else
112 # define bytestream2_get_ne16 bytestream2_get_le16
113 # define bytestream2_get_ne24 bytestream2_get_le24
114 # define bytestream2_get_ne32 bytestream2_get_le32
115 # define bytestream2_get_ne64 bytestream2_get_le64
116 # define bytestream2_get_ne16u bytestream2_get_le16u
117 # define bytestream2_get_ne24u bytestream2_get_le24u
118 # define bytestream2_get_ne32u bytestream2_get_le32u
119 # define bytestream2_get_ne64u bytestream2_get_le64u
120 # define bytestream2_put_ne16 bytestream2_put_le16
121 # define bytestream2_put_ne24 bytestream2_put_le24
122 # define bytestream2_put_ne32 bytestream2_put_le32
123 # define bytestream2_put_ne64 bytestream2_put_le64
124 # define bytestream2_peek_ne16 bytestream2_peek_le16
125 # define bytestream2_peek_ne24 bytestream2_peek_le24
126 # define bytestream2_peek_ne32 bytestream2_peek_le32
127 # define bytestream2_peek_ne64 bytestream2_peek_le64
128 #endif
129 
131  const uint8_t *buf,
132  int buf_size)
133 {
134  g->buffer = buf;
135  g->buffer_start = buf;
136  g->buffer_end = buf + buf_size;
137 }
138 
140  uint8_t *buf,
141  int buf_size)
142 {
143  p->buffer = buf;
144  p->buffer_start = buf;
145  p->buffer_end = buf + buf_size;
146  p->eof = 0;
147 }
148 
150 {
151  return g->buffer_end - g->buffer;
152 }
153 
155 {
156  return p->buffer_end - p->buffer;
157 }
158 
160  unsigned int size)
161 {
162  g->buffer += FFMIN(g->buffer_end - g->buffer, size);
163 }
164 
166  unsigned int size)
167 {
168  g->buffer += size;
169 }
170 
172  unsigned int size)
173 {
174  int size2;
175  if (p->eof)
176  return;
177  size2 = FFMIN(p->buffer_end - p->buffer, size);
178  if (size2 != size)
179  p->eof = 1;
180  p->buffer += size2;
181 }
182 
184 {
185  return (int)(g->buffer - g->buffer_start);
186 }
187 
189 {
190  return (int)(p->buffer - p->buffer_start);
191 }
192 
194 {
195  return (int)(g->buffer_end - g->buffer_start);
196 }
197 
199 {
200  return (int)(p->buffer_end - p->buffer_start);
201 }
202 
204  int offset,
205  int whence)
206 {
207  switch (whence) {
208  case SEEK_CUR:
209  offset = av_clip(offset, -(g->buffer - g->buffer_start),
210  g->buffer_end - g->buffer);
211  g->buffer += offset;
212  break;
213  case SEEK_END:
214  offset = av_clip(offset, -(g->buffer_end - g->buffer_start), 0);
215  g->buffer = g->buffer_end + offset;
216  break;
217  case SEEK_SET:
218  offset = av_clip(offset, 0, g->buffer_end - g->buffer_start);
219  g->buffer = g->buffer_start + offset;
220  break;
221  default:
222  return AVERROR(EINVAL);
223  }
224  return bytestream2_tell(g);
225 }
226 
228  int offset,
229  int whence)
230 {
231  p->eof = 0;
232  switch (whence) {
233  case SEEK_CUR:
234  if (p->buffer_end - p->buffer < offset)
235  p->eof = 1;
236  offset = av_clip(offset, -(p->buffer - p->buffer_start),
237  p->buffer_end - p->buffer);
238  p->buffer += offset;
239  break;
240  case SEEK_END:
241  if (offset > 0)
242  p->eof = 1;
243  offset = av_clip(offset, -(p->buffer_end - p->buffer_start), 0);
244  p->buffer = p->buffer_end + offset;
245  break;
246  case SEEK_SET:
247  if (p->buffer_end - p->buffer_start < offset)
248  p->eof = 1;
249  offset = av_clip(offset, 0, p->buffer_end - p->buffer_start);
250  p->buffer = p->buffer_start + offset;
251  break;
252  default:
253  return AVERROR(EINVAL);
254  }
255  return bytestream2_tell_p(p);
256 }
257 
259  uint8_t *dst,
260  unsigned int size)
261 {
262  int size2 = FFMIN(g->buffer_end - g->buffer, size);
263  memcpy(dst, g->buffer, size2);
264  g->buffer += size2;
265  return size2;
266 }
267 
269  uint8_t *dst,
270  unsigned int size)
271 {
272  memcpy(dst, g->buffer, size);
273  g->buffer += size;
274  return size;
275 }
276 
278  const uint8_t *src,
279  unsigned int size)
280 {
281  int size2;
282  if (p->eof)
283  return 0;
284  size2 = FFMIN(p->buffer_end - p->buffer, size);
285  if (size2 != size)
286  p->eof = 1;
287  memcpy(p->buffer, src, size2);
288  p->buffer += size2;
289  return size2;
290 }
291 
293  const uint8_t *src,
294  unsigned int size)
295 {
296  memcpy(p->buffer, src, size);
297  p->buffer += size;
298  return size;
299 }
300 
302  const uint8_t c,
303  unsigned int size)
304 {
305  int size2;
306  if (p->eof)
307  return;
308  size2 = FFMIN(p->buffer_end - p->buffer, size);
309  if (size2 != size)
310  p->eof = 1;
311  memset(p->buffer, c, size2);
312  p->buffer += size2;
313 }
314 
316  const uint8_t c,
317  unsigned int size)
318 {
319  memset(p->buffer, c, size);
320  p->buffer += size;
321 }
322 
324 {
325  return p->eof;
326 }
327 
328 static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b,
329  uint8_t *dst,
330  unsigned int size)
331 {
332  memcpy(dst, *b, size);
333  (*b) += size;
334  return size;
335 }
336 
338  const uint8_t *src,
339  unsigned int size)
340 {
341  memcpy(*b, src, size);
342  (*b) += size;
343 }
344 
345 #endif /* AVCODEC_BYTESTREAM_H */