summaryrefslogtreecommitdiff
path: root/audio_codec/libflac/intreadwrite.h (plain)
blob: 57eaf30f7073e1c918e391065769d7de8a000680
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#ifndef AVUTIL_INTREADWRITE_H
20#define AVUTIL_INTREADWRITE_H
21#ifndef __MW__
22#include <stdint.h>
23#endif
24#include "bswap.h"
25
26/*
27 * Map AV_RNXX <-> AV_R[BL]XX for all variants provided by per-arch headers.
28 */
29
30#if HAVE_BIGENDIAN
31
32# if defined(AV_RN16) && !defined(AV_RB16)
33# define AV_RB16(p) AV_RN16(p)
34# elif !defined(AV_RN16) && defined(AV_RB16)
35# define AV_RN16(p) AV_RB16(p)
36# endif
37
38# if defined(AV_WN16) && !defined(AV_WB16)
39# define AV_WB16(p, v) AV_WN16(p, v)
40# elif !defined(AV_WN16) && defined(AV_WB16)
41# define AV_WN16(p, v) AV_WB16(p, v)
42# endif
43
44# if defined(AV_RN24) && !defined(AV_RB24)
45# define AV_RB24(p) AV_RN24(p)
46# elif !defined(AV_RN24) && defined(AV_RB24)
47# define AV_RN24(p) AV_RB24(p)
48# endif
49
50# if defined(AV_WN24) && !defined(AV_WB24)
51# define AV_WB24(p, v) AV_WN24(p, v)
52# elif !defined(AV_WN24) && defined(AV_WB24)
53# define AV_WN24(p, v) AV_WB24(p, v)
54# endif
55
56# if defined(AV_RN32) && !defined(AV_RB32)
57# define AV_RB32(p) AV_RN32(p)
58# elif !defined(AV_RN32) && defined(AV_RB32)
59# define AV_RN32(p) AV_RB32(p)
60# endif
61
62# if defined(AV_WN32) && !defined(AV_WB32)
63# define AV_WB32(p, v) AV_WN32(p, v)
64# elif !defined(AV_WN32) && defined(AV_WB32)
65# define AV_WN32(p, v) AV_WB32(p, v)
66# endif
67
68# if defined(AV_RN64) && !defined(AV_RB64)
69# define AV_RB64(p) AV_RN64(p)
70# elif !defined(AV_RN64) && defined(AV_RB64)
71# define AV_RN64(p) AV_RB64(p)
72# endif
73
74# if defined(AV_WN64) && !defined(AV_WB64)
75# define AV_WB64(p, v) AV_WN64(p, v)
76# elif !defined(AV_WN64) && defined(AV_WB64)
77# define AV_WN64(p, v) AV_WB64(p, v)
78# endif
79
80#else /* HAVE_BIGENDIAN */
81
82# if defined(AV_RN16) && !defined(AV_RL16)
83# define AV_RL16(p) AV_RN16(p)
84# elif !defined(AV_RN16) && defined(AV_RL16)
85# define AV_RN16(p) AV_RL16(p)
86# endif
87
88# if defined(AV_WN16) && !defined(AV_WL16)
89# define AV_WL16(p, v) AV_WN16(p, v)
90# elif !defined(AV_WN16) && defined(AV_WL16)
91# define AV_WN16(p, v) AV_WL16(p, v)
92# endif
93
94# if defined(AV_RN24) && !defined(AV_RL24)
95# define AV_RL24(p) AV_RN24(p)
96# elif !defined(AV_RN24) && defined(AV_RL24)
97# define AV_RN24(p) AV_RL24(p)
98# endif
99
100# if defined(AV_WN24) && !defined(AV_WL24)
101# define AV_WL24(p, v) AV_WN24(p, v)
102# elif !defined(AV_WN24) && defined(AV_WL24)
103# define AV_WN24(p, v) AV_WL24(p, v)
104# endif
105
106# if defined(AV_RN32) && !defined(AV_RL32)
107# define AV_RL32(p) AV_RN32(p)
108# elif !defined(AV_RN32) && defined(AV_RL32)
109# define AV_RN32(p) AV_RL32(p)
110# endif
111
112# if defined(AV_WN32) && !defined(AV_WL32)
113# define AV_WL32(p, v) AV_WN32(p, v)
114# elif !defined(AV_WN32) && defined(AV_WL32)
115# define AV_WN32(p, v) AV_WL32(p, v)
116# endif
117
118# if defined(AV_RN64) && !defined(AV_RL64)
119# define AV_RL64(p) AV_RN64(p)
120# elif !defined(AV_RN64) && defined(AV_RL64)
121# define AV_RN64(p) AV_RL64(p)
122# endif
123
124# if defined(AV_WN64) && !defined(AV_WL64)
125# define AV_WL64(p, v) AV_WN64(p, v)
126# elif !defined(AV_WN64) && defined(AV_WL64)
127# define AV_WN64(p, v) AV_WL64(p, v)
128# endif
129
130#endif /* !HAVE_BIGENDIAN */
131
132/*
133 * Define AV_[RW]N helper macros to simplify definitions not provided
134 * by per-arch headers.
135 */
136
137#if HAVE_ATTRIBUTE_PACKED
138
139struct unaligned_64 {
140 uint64_t l;
141} __attribute__((packed));
142struct unaligned_32 {
143 uint32_t l;
144} __attribute__((packed));
145struct unaligned_16 {
146 uint16_t l;
147} __attribute__((packed));
148
149# define AV_RN(s, p) (((const struct unaligned_##s *) (p))->l)
150# define AV_WN(s, p, v) (((struct unaligned_##s *) (p))->l) = (v)
151
152#elif defined(__DECC)
153
154# define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
155# define AV_WN(s, p, v) *((__unaligned uint##s##_t*)(p)) = (v)
156
157#elif HAVE_FAST_UNALIGNED
158
159# define AV_RN(s, p) (*((const uint##s##_t*)(p)))
160# define AV_WN(s, p, v) *((uint##s##_t*)(p)) = (v)
161
162#else
163
164#ifndef AV_RB16
165# define AV_RB16(x) \
166 ((((const uint8_t*)(x))[0] << 8) | \
167 ((const uint8_t*)(x))[1])
168#endif
169#ifndef AV_WB16
170# define AV_WB16(p, d) do { \
171 ((uint8_t*)(p))[1] = (d); \
172 ((uint8_t*)(p))[0] = (d)>>8; \
173 } while(0)
174#endif
175
176#ifndef AV_RL16
177# define AV_RL16(x) \
178 ((((const uint8_t*)(x))[1] << 8) | \
179 ((const uint8_t*)(x))[0])
180#endif
181#ifndef AV_WL16
182# define AV_WL16(p, d) do { \
183 ((uint8_t*)(p))[0] = (d); \
184 ((uint8_t*)(p))[1] = (d)>>8; \
185 } while(0)
186#endif
187
188#ifndef AV_RB32
189# define AV_RB32(x) \
190 ((((const uint8_t*)(x))[0] << 24) | \
191 (((const uint8_t*)(x))[1] << 16) | \
192 (((const uint8_t*)(x))[2] << 8) | \
193 ((const uint8_t*)(x))[3])
194#endif
195#ifndef AV_WB32
196# define AV_WB32(p, d) do { \
197 ((uint8_t*)(p))[3] = (d); \
198 ((uint8_t*)(p))[2] = (d)>>8; \
199 ((uint8_t*)(p))[1] = (d)>>16; \
200 ((uint8_t*)(p))[0] = (d)>>24; \
201 } while(0)
202#endif
203
204#ifndef AV_RL32
205# define AV_RL32(x) \
206 ((((const uint8_t*)(x))[3] << 24) | \
207 (((const uint8_t*)(x))[2] << 16) | \
208 (((const uint8_t*)(x))[1] << 8) | \
209 ((const uint8_t*)(x))[0])
210#endif
211#ifndef AV_WL32
212# define AV_WL32(p, d) do { \
213 ((uint8_t*)(p))[0] = (d); \
214 ((uint8_t*)(p))[1] = (d)>>8; \
215 ((uint8_t*)(p))[2] = (d)>>16; \
216 ((uint8_t*)(p))[3] = (d)>>24; \
217 } while(0)
218#endif
219
220#ifndef AV_RB64
221# define AV_RB64(x) \
222 (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
223 ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
224 ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
225 ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
226 ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
227 ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
228 ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
229 (uint64_t)((const uint8_t*)(x))[7])
230#endif
231#ifndef AV_WB64
232# define AV_WB64(p, d) do { \
233 ((uint8_t*)(p))[7] = (d); \
234 ((uint8_t*)(p))[6] = (d)>>8; \
235 ((uint8_t*)(p))[5] = (d)>>16; \
236 ((uint8_t*)(p))[4] = (d)>>24; \
237 ((uint8_t*)(p))[3] = (d)>>32; \
238 ((uint8_t*)(p))[2] = (d)>>40; \
239 ((uint8_t*)(p))[1] = (d)>>48; \
240 ((uint8_t*)(p))[0] = (d)>>56; \
241 } while(0)
242#endif
243
244#ifndef AV_RL64
245# define AV_RL64(x) \
246 (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
247 ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
248 ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
249 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
250 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
251 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
252 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
253 (uint64_t)((const uint8_t*)(x))[0])
254#endif
255#ifndef AV_WL64
256# define AV_WL64(p, d) do { \
257 ((uint8_t*)(p))[0] = (d); \
258 ((uint8_t*)(p))[1] = (d)>>8; \
259 ((uint8_t*)(p))[2] = (d)>>16; \
260 ((uint8_t*)(p))[3] = (d)>>24; \
261 ((uint8_t*)(p))[4] = (d)>>32; \
262 ((uint8_t*)(p))[5] = (d)>>40; \
263 ((uint8_t*)(p))[6] = (d)>>48; \
264 ((uint8_t*)(p))[7] = (d)>>56; \
265 } while(0)
266#endif
267
268#if HAVE_BIGENDIAN
269# define AV_RN(s, p) AV_RB##s(p)
270# define AV_WN(s, p, v) AV_WB##s(p, v)
271#else
272# define AV_RN(s, p) AV_RL##s(p)
273# define AV_WN(s, p, v) AV_WL##s(p, v)
274#endif
275
276#endif /* HAVE_FAST_UNALIGNED */
277
278#ifndef AV_RN16
279# define AV_RN16(p) AV_RN(16, p)
280#endif
281
282#ifndef AV_RN32
283# define AV_RN32(p) AV_RN(32, p)
284#endif
285
286#ifndef AV_RN64
287# define AV_RN64(p) AV_RN(64, p)
288#endif
289
290#ifndef AV_WN16
291# define AV_WN16(p, v) AV_WN(16, p, v)
292#endif
293
294#ifndef AV_WN32
295# define AV_WN32(p, v) AV_WN(32, p, v)
296#endif
297
298#ifndef AV_WN64
299# define AV_WN64(p, v) AV_WN(64, p, v)
300#endif
301
302#if HAVE_BIGENDIAN
303# define AV_RB(s, p) AV_RN##s(p)
304# define AV_WB(s, p, v) AV_WN##s(p, v)
305# define AV_RL(s, p) bswap_##s(AV_RN##s(p))
306# define AV_WL(s, p, v) AV_WN##s(p, bswap_##s(v))
307#else
308# define AV_RB(s, p) bswap_##s(AV_RN##s(p))
309# define AV_WB(s, p, v) AV_WN##s(p, bswap_##s(v))
310# define AV_RL(s, p) AV_RN##s(p)
311# define AV_WL(s, p, v) AV_WN##s(p, v)
312#endif
313
314#define AV_RB8(x) (((const uint8_t*)(x))[0])
315#define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
316
317#define AV_RL8(x) AV_RB8(x)
318#define AV_WL8(p, d) AV_WB8(p, d)
319
320#ifndef AV_RB16
321# define AV_RB16(p) AV_RB(16, p)
322#endif
323#ifndef AV_WB16
324# define AV_WB16(p, v) AV_WB(16, p, v)
325#endif
326
327#ifndef AV_RL16
328# define AV_RL16(p) AV_RL(16, p)
329#endif
330#ifndef AV_WL16
331# define AV_WL16(p, v) AV_WL(16, p, v)
332#endif
333
334#ifndef AV_RB32
335# define AV_RB32(p) AV_RB(32, p)
336#endif
337#ifndef AV_WB32
338# define AV_WB32(p, v) AV_WB(32, p, v)
339#endif
340
341#ifndef AV_RL32
342# define AV_RL32(p) AV_RL(32, p)
343#endif
344#ifndef AV_WL32
345# define AV_WL32(p, v) AV_WL(32, p, v)
346#endif
347
348#ifndef AV_RB64
349# define AV_RB64(p) AV_RB(64, p)
350#endif
351#ifndef AV_WB64
352# define AV_WB64(p, v) AV_WB(64, p, v)
353#endif
354
355#ifndef AV_RL64
356# define AV_RL64(p) AV_RL(64, p)
357#endif
358#ifndef AV_WL64
359# define AV_WL64(p, v) AV_WL(64, p, v)
360#endif
361
362#ifndef AV_RB24
363# define AV_RB24(x) \
364 ((((const uint8_t*)(x))[0] << 16) | \
365 (((const uint8_t*)(x))[1] << 8) | \
366 ((const uint8_t*)(x))[2])
367#endif
368#ifndef AV_WB24
369# define AV_WB24(p, d) do { \
370 ((uint8_t*)(p))[2] = (d); \
371 ((uint8_t*)(p))[1] = (d)>>8; \
372 ((uint8_t*)(p))[0] = (d)>>16; \
373 } while(0)
374#endif
375
376#ifndef AV_RL24
377# define AV_RL24(x) \
378 ((((const uint8_t*)(x))[2] << 16) | \
379 (((const uint8_t*)(x))[1] << 8) | \
380 ((const uint8_t*)(x))[0])
381#endif
382#ifndef AV_WL24
383# define AV_WL24(p, d) do { \
384 ((uint8_t*)(p))[0] = (d); \
385 ((uint8_t*)(p))[1] = (d)>>8; \
386 ((uint8_t*)(p))[2] = (d)>>16; \
387 } while(0)
388#endif
389
390#endif /* AVUTIL_INTREADWRITE_H */
391