Give all anonymously typedeffed structs in headers a name
[ffmpeg.git] / libavcodec / bytestream.h
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 {
33     const uint8_t *buffer, *buffer_end, *buffer_start;
34 } GetByteContext;
35
36 typedef struct PutByteContext {
37     uint8_t *buffer, *buffer_end, *buffer_start;
38     int eof;
39 } PutByteContext;
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
130 static av_always_inline void bytestream2_init(GetByteContext *g,
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
139 static av_always_inline void bytestream2_init_writer(PutByteContext *p,
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
149 static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
150 {
151     return g->buffer_end - g->buffer;
152 }
153
154 static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
155 {
156     return p->buffer_end - p->buffer;
157 }
158
159 static av_always_inline void bytestream2_skip(GetByteContext *g,
160                                               unsigned int size)
161 {
162     g->buffer += FFMIN(g->buffer_end - g->buffer, size);
163 }
164
165 static av_always_inline void bytestream2_skipu(GetByteContext *g,
166                                                unsigned int size)
167 {
168     g->buffer += size;
169 }
170
171 static av_always_inline void bytestream2_skip_p(PutByteContext *p,
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
183 static av_always_inline int bytestream2_tell(GetByteContext *g)
184 {
185     return (int)(g->buffer - g->buffer_start);
186 }
187
188 static av_always_inline int bytestream2_tell_p(PutByteContext *p)
189 {
190     return (int)(p->buffer - p->buffer_start);
191 }
192
193 static av_always_inline int bytestream2_seek(GetByteContext *g,
194                                              int offset,
195                                              int whence)
196 {
197     switch (whence) {
198     case SEEK_CUR:
199         offset     = av_clip(offset, -(g->buffer - g->buffer_start),
200                              g->buffer_end - g->buffer);
201         g->buffer += offset;
202         break;
203     case SEEK_END:
204         offset    = av_clip(offset, -(g->buffer_end - g->buffer_start), 0);
205         g->buffer = g->buffer_end + offset;
206         break;
207     case SEEK_SET:
208         offset    = av_clip(offset, 0, g->buffer_end - g->buffer_start);
209         g->buffer = g->buffer_start + offset;
210         break;
211     default:
212         return AVERROR(EINVAL);
213     }
214     return bytestream2_tell(g);
215 }
216
217 static av_always_inline int bytestream2_seek_p(PutByteContext *p,
218                                                int offset,
219                                                int whence)
220 {
221     p->eof = 0;
222     switch (whence) {
223     case SEEK_CUR:
224         if (p->buffer_end - p->buffer < offset)
225             p->eof = 1;
226         offset     = av_clip(offset, -(p->buffer - p->buffer_start),
227                              p->buffer_end - p->buffer);
228         p->buffer += offset;
229         break;
230     case SEEK_END:
231         if (offset > 0)
232             p->eof = 1;
233         offset    = av_clip(offset, -(p->buffer_end - p->buffer_start), 0);
234         p->buffer = p->buffer_end + offset;
235         break;
236     case SEEK_SET:
237         if (p->buffer_end - p->buffer_start < offset)
238             p->eof = 1;
239         offset    = av_clip(offset, 0, p->buffer_end - p->buffer_start);
240         p->buffer = p->buffer_start + offset;
241         break;
242     default:
243         return AVERROR(EINVAL);
244     }
245     return bytestream2_tell_p(p);
246 }
247
248 static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g,
249                                                             uint8_t *dst,
250                                                             unsigned int size)
251 {
252     int size2 = FFMIN(g->buffer_end - g->buffer, size);
253     memcpy(dst, g->buffer, size2);
254     g->buffer += size2;
255     return size2;
256 }
257
258 static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g,
259                                                              uint8_t *dst,
260                                                              unsigned int size)
261 {
262     memcpy(dst, g->buffer, size);
263     g->buffer += size;
264     return size;
265 }
266
267 static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p,
268                                                             const uint8_t *src,
269                                                             unsigned int size)
270 {
271     int size2;
272     if (p->eof)
273         return 0;
274     size2 = FFMIN(p->buffer_end - p->buffer, size);
275     if (size2 != size)
276         p->eof = 1;
277     memcpy(p->buffer, src, size2);
278     p->buffer += size2;
279     return size2;
280 }
281
282 static av_always_inline unsigned int bytestream2_put_bufferu(PutByteContext *p,
283                                                              const uint8_t *src,
284                                                              unsigned int size)
285 {
286     memcpy(p->buffer, src, size);
287     p->buffer += size;
288     return size;
289 }
290
291 static av_always_inline void bytestream2_set_buffer(PutByteContext *p,
292                                                     const uint8_t c,
293                                                     unsigned int size)
294 {
295     int size2;
296     if (p->eof)
297         return;
298     size2 = FFMIN(p->buffer_end - p->buffer, size);
299     if (size2 != size)
300         p->eof = 1;
301     memset(p->buffer, c, size2);
302     p->buffer += size2;
303 }
304
305 static av_always_inline void bytestream2_set_bufferu(PutByteContext *p,
306                                                      const uint8_t c,
307                                                      unsigned int size)
308 {
309     memset(p->buffer, c, size);
310     p->buffer += size;
311 }
312
313 static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
314 {
315     return p->eof;
316 }
317
318 static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b,
319                                                            uint8_t *dst,
320                                                            unsigned int size)
321 {
322     memcpy(dst, *b, size);
323     (*b) += size;
324     return size;
325 }
326
327 static av_always_inline void bytestream_put_buffer(uint8_t **b,
328                                                    const uint8_t *src,
329                                                    unsigned int size)
330 {
331     memcpy(*b, src, size);
332     (*b) += size;
333 }
334
335 #endif /* AVCODEC_BYTESTREAM_H */