build: Generalize yasm/nasm-related variable names
[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         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
132 static av_always_inline void bytestream2_init(GetByteContext *g,
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
141 static av_always_inline void bytestream2_init_writer(PutByteContext *p,
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
151 static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
152 {
153     return g->buffer_end - g->buffer;
154 }
155
156 static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
157 {
158     return p->buffer_end - p->buffer;
159 }
160
161 static av_always_inline void bytestream2_skip(GetByteContext *g,
162                                               unsigned int size)
163 {
164     g->buffer += FFMIN(g->buffer_end - g->buffer, size);
165 }
166
167 static av_always_inline void bytestream2_skipu(GetByteContext *g,
168                                                unsigned int size)
169 {
170     g->buffer += size;
171 }
172
173 static av_always_inline void bytestream2_skip_p(PutByteContext *p,
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
185 static av_always_inline int bytestream2_tell(GetByteContext *g)
186 {
187     return (int)(g->buffer - g->buffer_start);
188 }
189
190 static av_always_inline int bytestream2_tell_p(PutByteContext *p)
191 {
192     return (int)(p->buffer - p->buffer_start);
193 }
194
195 static av_always_inline int bytestream2_size(GetByteContext *g)
196 {
197     return (int)(g->buffer_end - g->buffer_start);
198 }
199
200 static av_always_inline int bytestream2_size_p(PutByteContext *p)
201 {
202     return (int)(p->buffer_end - p->buffer_start);
203 }
204
205 static av_always_inline int bytestream2_seek(GetByteContext *g,
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
229 static av_always_inline int bytestream2_seek_p(PutByteContext *p,
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
260 static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g,
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
270 static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g,
271                                                              uint8_t *dst,
272                                                              unsigned int size)
273 {
274     memcpy(dst, g->buffer, size);
275     g->buffer += size;
276     return size;
277 }
278
279 static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p,
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
294 static av_always_inline unsigned int bytestream2_put_bufferu(PutByteContext *p,
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
303 static av_always_inline void bytestream2_set_buffer(PutByteContext *p,
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
317 static av_always_inline void bytestream2_set_bufferu(PutByteContext *p,
318                                                      const uint8_t c,
319                                                      unsigned int size)
320 {
321     memset(p->buffer, c, size);
322     p->buffer += size;
323 }
324
325 static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
326 {
327     return p->eof;
328 }
329
330 static av_always_inline unsigned int bytestream2_copy_bufferu(PutByteContext *p,
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
340 static av_always_inline unsigned int bytestream2_copy_buffer(PutByteContext *p,
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
365 static av_always_inline void bytestream_put_buffer(uint8_t **b,
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 */