swscale: make yuv2yuv1 use named registers.
[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 <string.h>
27 #include "libavutil/common.h"
28 #include "libavutil/intreadwrite.h"
29
30 typedef struct {
31     const uint8_t *buffer, *buffer_end, *buffer_start;
32 } GetByteContext;
33
34 typedef struct {
35     uint8_t *buffer, *buffer_end, *buffer_start;
36     int eof;
37 } PutByteContext;
38
39 #define DEF_T(type, name, bytes, read, write)                             \
40 static av_always_inline type bytestream_get_ ## name(const uint8_t **b){\
41     (*b) += bytes;\
42     return read(*b - bytes);\
43 }\
44 static av_always_inline void bytestream_put_ ##name(uint8_t **b, const type value){\
45     write(*b, value);\
46     (*b) += bytes;\
47 }\
48 static av_always_inline void bytestream2_put_ ## name ## u(PutByteContext *p, const type value)\
49 {\
50     bytestream_put_ ## name(&p->buffer, value);\
51 }\
52 static av_always_inline void bytestream2_put_ ## name(PutByteContext *p, const type value){\
53     if (!p->eof && (p->buffer_end - p->buffer >= bytes)) {\
54         write(p->buffer, value);\
55         p->buffer += bytes;\
56     } else\
57         p->eof = 1;\
58 }\
59 static av_always_inline type bytestream2_get_ ## name ## u(GetByteContext *g)\
60 {\
61     return bytestream_get_ ## name(&g->buffer);\
62 }\
63 static av_always_inline type bytestream2_get_ ## name(GetByteContext *g)\
64 {\
65     if (g->buffer_end - g->buffer < bytes)\
66         return 0;\
67     return bytestream2_get_ ## name ## u(g);\
68 }\
69 static av_always_inline type bytestream2_peek_ ## name(GetByteContext *g)\
70 {\
71     if (g->buffer_end - g->buffer < bytes)\
72         return 0;\
73     return read(g->buffer);\
74 }
75
76 #define DEF(name, bytes, read, write) \
77     DEF_T(unsigned int, name, bytes, read, write)
78 #define DEF64(name, bytes, read, write) \
79     DEF_T(uint64_t, name, bytes, read, write)
80
81 DEF64(le64, 8, AV_RL64, AV_WL64)
82 DEF  (le32, 4, AV_RL32, AV_WL32)
83 DEF  (le24, 3, AV_RL24, AV_WL24)
84 DEF  (le16, 2, AV_RL16, AV_WL16)
85 DEF64(be64, 8, AV_RB64, AV_WB64)
86 DEF  (be32, 4, AV_RB32, AV_WB32)
87 DEF  (be24, 3, AV_RB24, AV_WB24)
88 DEF  (be16, 2, AV_RB16, AV_WB16)
89 DEF  (byte, 1, AV_RB8 , AV_WB8 )
90
91 #undef DEF
92 #undef DEF64
93 #undef DEF_T
94
95 #if HAVE_BIGENDIAN
96 #   define bytestream2_get_ne16  bytestream2_get_be16
97 #   define bytestream2_get_ne24  bytestream2_get_be24
98 #   define bytestream2_get_ne32  bytestream2_get_be32
99 #   define bytestream2_get_ne64  bytestream2_get_be64
100 #   define bytestream2_get_ne16u bytestream2_get_be16u
101 #   define bytestream2_get_ne24u bytestream2_get_be24u
102 #   define bytestream2_get_ne32u bytestream2_get_be32u
103 #   define bytestream2_get_ne64u bytestream2_get_be64u
104 #   define bytestream2_put_ne16  bytestream2_put_be16
105 #   define bytestream2_put_ne24  bytestream2_put_be24
106 #   define bytestream2_put_ne32  bytestream2_put_be32
107 #   define bytestream2_put_ne64  bytestream2_put_be64
108 #   define bytestream2_peek_ne16 bytestream2_peek_be16
109 #   define bytestream2_peek_ne24 bytestream2_peek_be24
110 #   define bytestream2_peek_ne32 bytestream2_peek_be32
111 #   define bytestream2_peek_ne64 bytestream2_peek_be64
112 #else
113 #   define bytestream2_get_ne16  bytestream2_get_le16
114 #   define bytestream2_get_ne24  bytestream2_get_le24
115 #   define bytestream2_get_ne32  bytestream2_get_le32
116 #   define bytestream2_get_ne64  bytestream2_get_le64
117 #   define bytestream2_get_ne16u bytestream2_get_le16u
118 #   define bytestream2_get_ne24u bytestream2_get_le24u
119 #   define bytestream2_get_ne32u bytestream2_get_le32u
120 #   define bytestream2_get_ne64u bytestream2_get_le64u
121 #   define bytestream2_put_ne16  bytestream2_put_le16
122 #   define bytestream2_put_ne24  bytestream2_put_le24
123 #   define bytestream2_put_ne32  bytestream2_put_le32
124 #   define bytestream2_put_ne64  bytestream2_put_le64
125 #   define bytestream2_peek_ne16 bytestream2_peek_le16
126 #   define bytestream2_peek_ne24 bytestream2_peek_le24
127 #   define bytestream2_peek_ne32 bytestream2_peek_le32
128 #   define bytestream2_peek_ne64 bytestream2_peek_le64
129 #endif
130
131 static av_always_inline void bytestream2_init(GetByteContext *g,
132                                               const uint8_t *buf, 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, int buf_size)
141 {
142     p->buffer       = buf;
143     p->buffer_start = buf;
144     p->buffer_end   = buf + buf_size;
145     p->eof          = 0;
146 }
147
148 static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
149 {
150     return g->buffer_end - g->buffer;
151 }
152
153 static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
154 {
155     return p->buffer_end - p->buffer;
156 }
157
158 static av_always_inline void bytestream2_skip(GetByteContext *g,
159                                               unsigned int size)
160 {
161     g->buffer += FFMIN(g->buffer_end - g->buffer, size);
162 }
163
164 static av_always_inline void bytestream2_skip_p(PutByteContext *p,
165                                                 unsigned int size)
166 {
167     int size2;
168     if (p->eof)
169         return;
170     size2 = FFMIN(p->buffer_end - p->buffer, size);
171     if (size2 != size)
172         p->eof = 1;
173     p->buffer += size2;
174 }
175
176 static av_always_inline int bytestream2_tell(GetByteContext *g)
177 {
178     return (int)(g->buffer - g->buffer_start);
179 }
180
181 static av_always_inline int bytestream2_tell_p(PutByteContext *p)
182 {
183     return (int)(p->buffer - p->buffer_start);
184 }
185
186 static av_always_inline int bytestream2_seek(GetByteContext *g, int offset,
187                                              int whence)
188 {
189     switch (whence) {
190     case SEEK_CUR:
191         offset = av_clip(offset, -(g->buffer - g->buffer_start),
192                          g->buffer_end - g->buffer);
193         g->buffer += offset;
194         break;
195     case SEEK_END:
196         offset = av_clip(offset, -(g->buffer_end - g->buffer_start), 0);
197         g->buffer = g->buffer_end + offset;
198         break;
199     case SEEK_SET:
200         offset = av_clip(offset, 0, g->buffer_end - g->buffer_start);
201         g->buffer = g->buffer_start + offset;
202         break;
203     default:
204         return AVERROR(EINVAL);
205     }
206     return bytestream2_tell(g);
207 }
208
209 static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset,
210                                                int whence)
211 {
212     p->eof = 0;
213     switch (whence) {
214     case SEEK_CUR:
215         if (p->buffer_end - p->buffer < offset)
216             p->eof = 1;
217         offset = av_clip(offset, -(p->buffer - p->buffer_start),
218                          p->buffer_end - p->buffer);
219         p->buffer += offset;
220         break;
221     case SEEK_END:
222         if (offset > 0)
223             p->eof = 1;
224         offset = av_clip(offset, -(p->buffer_end - p->buffer_start), 0);
225         p->buffer = p->buffer_end + offset;
226         break;
227     case SEEK_SET:
228         if (p->buffer_end - p->buffer_start < offset)
229             p->eof = 1;
230         offset = av_clip(offset, 0, p->buffer_end - p->buffer_start);
231         p->buffer = p->buffer_start + offset;
232         break;
233     default:
234         return AVERROR(EINVAL);
235     }
236     return bytestream2_tell_p(p);
237 }
238
239 static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g,
240                                                             uint8_t *dst,
241                                                             unsigned int size)
242 {
243     int size2 = FFMIN(g->buffer_end - g->buffer, size);
244     memcpy(dst, g->buffer, size2);
245     g->buffer += size2;
246     return size2;
247 }
248
249 static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p,
250                                                             const uint8_t *src,
251                                                             unsigned int size)
252 {
253     int size2;
254     if (p->eof)
255         return 0;
256     size2 = FFMIN(p->buffer_end - p->buffer, size);
257     if (size2 != size)
258         p->eof = 1;
259     memcpy(p->buffer, src, size2);
260     p->buffer += size2;
261     return size2;
262 }
263
264 static av_always_inline void bytestream2_set_buffer(PutByteContext *p,
265                                                     const uint8_t c,
266                                                     unsigned int size)
267 {
268     int size2;
269     if (p->eof)
270         return;
271     size2 = FFMIN(p->buffer_end - p->buffer, size);
272     if (size2 != size)
273         p->eof = 1;
274     memset(p->buffer, c, size2);
275     p->buffer += size2;
276 }
277
278 static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
279 {
280     return p->eof;
281 }
282
283 static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
284 {
285     memcpy(dst, *b, size);
286     (*b) += size;
287     return size;
288 }
289
290 static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
291 {
292     memcpy(*b, src, size);
293     (*b) += size;
294 }
295
296 #endif /* AVCODEC_BYTESTREAM_H */