2 * This file is part of FFmpeg.
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.
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.
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
19 #ifndef AVUTIL_INTREADWRITE_H
20 #define AVUTIL_INTREADWRITE_H
27 * Arch-specific headers can provide any combination of
28 * AV_[RW][BLN](16|24|32|64) and AV_(COPY|SWAP|ZERO)(64|128) macros.
29 * Preprocessor symbols must be defined, even if these are implemented
30 * as inline functions.
34 # include "arm/intreadwrite.h"
36 # include "avr32/intreadwrite.h"
38 # include "mips/intreadwrite.h"
40 # include "ppc/intreadwrite.h"
42 # include "x86/intreadwrite.h"
46 * Map AV_RNXX <-> AV_R[BL]XX for all variants provided by per-arch headers.
51 # if defined(AV_RN16) && !defined(AV_RB16)
52 # define AV_RB16(p) AV_RN16(p)
53 # elif !defined(AV_RN16) && defined(AV_RB16)
54 # define AV_RN16(p) AV_RB16(p)
57 # if defined(AV_WN16) && !defined(AV_WB16)
58 # define AV_WB16(p, v) AV_WN16(p, v)
59 # elif !defined(AV_WN16) && defined(AV_WB16)
60 # define AV_WN16(p, v) AV_WB16(p, v)
63 # if defined(AV_RN24) && !defined(AV_RB24)
64 # define AV_RB24(p) AV_RN24(p)
65 # elif !defined(AV_RN24) && defined(AV_RB24)
66 # define AV_RN24(p) AV_RB24(p)
69 # if defined(AV_WN24) && !defined(AV_WB24)
70 # define AV_WB24(p, v) AV_WN24(p, v)
71 # elif !defined(AV_WN24) && defined(AV_WB24)
72 # define AV_WN24(p, v) AV_WB24(p, v)
75 # if defined(AV_RN32) && !defined(AV_RB32)
76 # define AV_RB32(p) AV_RN32(p)
77 # elif !defined(AV_RN32) && defined(AV_RB32)
78 # define AV_RN32(p) AV_RB32(p)
81 # if defined(AV_WN32) && !defined(AV_WB32)
82 # define AV_WB32(p, v) AV_WN32(p, v)
83 # elif !defined(AV_WN32) && defined(AV_WB32)
84 # define AV_WN32(p, v) AV_WB32(p, v)
87 # if defined(AV_RN64) && !defined(AV_RB64)
88 # define AV_RB64(p) AV_RN64(p)
89 # elif !defined(AV_RN64) && defined(AV_RB64)
90 # define AV_RN64(p) AV_RB64(p)
93 # if defined(AV_WN64) && !defined(AV_WB64)
94 # define AV_WB64(p, v) AV_WN64(p, v)
95 # elif !defined(AV_WN64) && defined(AV_WB64)
96 # define AV_WN64(p, v) AV_WB64(p, v)
99 #else /* HAVE_BIGENDIAN */
101 # if defined(AV_RN16) && !defined(AV_RL16)
102 # define AV_RL16(p) AV_RN16(p)
103 # elif !defined(AV_RN16) && defined(AV_RL16)
104 # define AV_RN16(p) AV_RL16(p)
107 # if defined(AV_WN16) && !defined(AV_WL16)
108 # define AV_WL16(p, v) AV_WN16(p, v)
109 # elif !defined(AV_WN16) && defined(AV_WL16)
110 # define AV_WN16(p, v) AV_WL16(p, v)
113 # if defined(AV_RN24) && !defined(AV_RL24)
114 # define AV_RL24(p) AV_RN24(p)
115 # elif !defined(AV_RN24) && defined(AV_RL24)
116 # define AV_RN24(p) AV_RL24(p)
119 # if defined(AV_WN24) && !defined(AV_WL24)
120 # define AV_WL24(p, v) AV_WN24(p, v)
121 # elif !defined(AV_WN24) && defined(AV_WL24)
122 # define AV_WN24(p, v) AV_WL24(p, v)
125 # if defined(AV_RN32) && !defined(AV_RL32)
126 # define AV_RL32(p) AV_RN32(p)
127 # elif !defined(AV_RN32) && defined(AV_RL32)
128 # define AV_RN32(p) AV_RL32(p)
131 # if defined(AV_WN32) && !defined(AV_WL32)
132 # define AV_WL32(p, v) AV_WN32(p, v)
133 # elif !defined(AV_WN32) && defined(AV_WL32)
134 # define AV_WN32(p, v) AV_WL32(p, v)
137 # if defined(AV_RN64) && !defined(AV_RL64)
138 # define AV_RL64(p) AV_RN64(p)
139 # elif !defined(AV_RN64) && defined(AV_RL64)
140 # define AV_RN64(p) AV_RL64(p)
143 # if defined(AV_WN64) && !defined(AV_WL64)
144 # define AV_WL64(p, v) AV_WN64(p, v)
145 # elif !defined(AV_WN64) && defined(AV_WL64)
146 # define AV_WN64(p, v) AV_WL64(p, v)
149 #endif /* !HAVE_BIGENDIAN */
152 * Define AV_[RW]N helper macros to simplify definitions not provided
153 * by per-arch headers.
156 #if HAVE_ATTRIBUTE_PACKED
158 struct unaligned_64 { uint64_t l; } __attribute__((packed));
159 struct unaligned_32 { uint32_t l; } __attribute__((packed));
160 struct unaligned_16 { uint16_t l; } __attribute__((packed));
162 # define AV_RN(s, p) (((const struct unaligned_##s *) (p))->l)
163 # define AV_WN(s, p, v) ((((struct unaligned_##s *) (p))->l) = (v))
165 #elif defined(__DECC)
167 # define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
168 # define AV_WN(s, p, v) (*((__unaligned uint##s##_t*)(p)) = (v))
170 #elif HAVE_FAST_UNALIGNED
172 # define AV_RN(s, p) (*((const uint##s##_t*)(p)))
173 # define AV_WN(s, p, v) (*((uint##s##_t*)(p)) = (v))
178 # define AV_RB16(x) \
179 ((((const uint8_t*)(x))[0] << 8) | \
180 ((const uint8_t*)(x))[1])
183 # define AV_WB16(p, d) do { \
184 ((uint8_t*)(p))[1] = (d); \
185 ((uint8_t*)(p))[0] = (d)>>8; \
190 # define AV_RL16(x) \
191 ((((const uint8_t*)(x))[1] << 8) | \
192 ((const uint8_t*)(x))[0])
195 # define AV_WL16(p, d) do { \
196 ((uint8_t*)(p))[0] = (d); \
197 ((uint8_t*)(p))[1] = (d)>>8; \
202 # define AV_RB32(x) \
203 ((((const uint8_t*)(x))[0] << 24) | \
204 (((const uint8_t*)(x))[1] << 16) | \
205 (((const uint8_t*)(x))[2] << 8) | \
206 ((const uint8_t*)(x))[3])
209 # define AV_WB32(p, d) do { \
210 ((uint8_t*)(p))[3] = (d); \
211 ((uint8_t*)(p))[2] = (d)>>8; \
212 ((uint8_t*)(p))[1] = (d)>>16; \
213 ((uint8_t*)(p))[0] = (d)>>24; \
218 # define AV_RL32(x) \
219 ((((const uint8_t*)(x))[3] << 24) | \
220 (((const uint8_t*)(x))[2] << 16) | \
221 (((const uint8_t*)(x))[1] << 8) | \
222 ((const uint8_t*)(x))[0])
225 # define AV_WL32(p, d) do { \
226 ((uint8_t*)(p))[0] = (d); \
227 ((uint8_t*)(p))[1] = (d)>>8; \
228 ((uint8_t*)(p))[2] = (d)>>16; \
229 ((uint8_t*)(p))[3] = (d)>>24; \
234 # define AV_RB64(x) \
235 (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
236 ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
237 ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
238 ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
239 ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
240 ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
241 ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
242 (uint64_t)((const uint8_t*)(x))[7])
245 # define AV_WB64(p, d) do { \
246 ((uint8_t*)(p))[7] = (d); \
247 ((uint8_t*)(p))[6] = (d)>>8; \
248 ((uint8_t*)(p))[5] = (d)>>16; \
249 ((uint8_t*)(p))[4] = (d)>>24; \
250 ((uint8_t*)(p))[3] = (d)>>32; \
251 ((uint8_t*)(p))[2] = (d)>>40; \
252 ((uint8_t*)(p))[1] = (d)>>48; \
253 ((uint8_t*)(p))[0] = (d)>>56; \
258 # define AV_RL64(x) \
259 (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
260 ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
261 ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
262 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
263 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
264 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
265 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
266 (uint64_t)((const uint8_t*)(x))[0])
269 # define AV_WL64(p, d) do { \
270 ((uint8_t*)(p))[0] = (d); \
271 ((uint8_t*)(p))[1] = (d)>>8; \
272 ((uint8_t*)(p))[2] = (d)>>16; \
273 ((uint8_t*)(p))[3] = (d)>>24; \
274 ((uint8_t*)(p))[4] = (d)>>32; \
275 ((uint8_t*)(p))[5] = (d)>>40; \
276 ((uint8_t*)(p))[6] = (d)>>48; \
277 ((uint8_t*)(p))[7] = (d)>>56; \
282 # define AV_RN(s, p) AV_RB##s(p)
283 # define AV_WN(s, p, v) AV_WB##s(p, v)
285 # define AV_RN(s, p) AV_RL##s(p)
286 # define AV_WN(s, p, v) AV_WL##s(p, v)
289 #endif /* HAVE_FAST_UNALIGNED */
292 # define AV_RN16(p) AV_RN(16, p)
296 # define AV_RN32(p) AV_RN(32, p)
300 # define AV_RN64(p) AV_RN(64, p)
304 # define AV_WN16(p, v) AV_WN(16, p, v)
308 # define AV_WN32(p, v) AV_WN(32, p, v)
312 # define AV_WN64(p, v) AV_WN(64, p, v)
316 # define AV_RB(s, p) AV_RN##s(p)
317 # define AV_WB(s, p, v) AV_WN##s(p, v)
318 # define AV_RL(s, p) bswap_##s(AV_RN##s(p))
319 # define AV_WL(s, p, v) AV_WN##s(p, bswap_##s(v))
321 # define AV_RB(s, p) bswap_##s(AV_RN##s(p))
322 # define AV_WB(s, p, v) AV_WN##s(p, bswap_##s(v))
323 # define AV_RL(s, p) AV_RN##s(p)
324 # define AV_WL(s, p, v) AV_WN##s(p, v)
327 #define AV_RB8(x) (((const uint8_t*)(x))[0])
328 #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
330 #define AV_RL8(x) AV_RB8(x)
331 #define AV_WL8(p, d) AV_WB8(p, d)
334 # define AV_RB16(p) AV_RB(16, p)
337 # define AV_WB16(p, v) AV_WB(16, p, v)
341 # define AV_RL16(p) AV_RL(16, p)
344 # define AV_WL16(p, v) AV_WL(16, p, v)
348 # define AV_RB32(p) AV_RB(32, p)
351 # define AV_WB32(p, v) AV_WB(32, p, v)
355 # define AV_RL32(p) AV_RL(32, p)
358 # define AV_WL32(p, v) AV_WL(32, p, v)
362 # define AV_RB64(p) AV_RB(64, p)
365 # define AV_WB64(p, v) AV_WB(64, p, v)
369 # define AV_RL64(p) AV_RL(64, p)
372 # define AV_WL64(p, v) AV_WL(64, p, v)
376 # define AV_RB24(x) \
377 ((((const uint8_t*)(x))[0] << 16) | \
378 (((const uint8_t*)(x))[1] << 8) | \
379 ((const uint8_t*)(x))[2])
382 # define AV_WB24(p, d) do { \
383 ((uint8_t*)(p))[2] = (d); \
384 ((uint8_t*)(p))[1] = (d)>>8; \
385 ((uint8_t*)(p))[0] = (d)>>16; \
390 # define AV_RL24(x) \
391 ((((const uint8_t*)(x))[2] << 16) | \
392 (((const uint8_t*)(x))[1] << 8) | \
393 ((const uint8_t*)(x))[0])
396 # define AV_WL24(p, d) do { \
397 ((uint8_t*)(p))[0] = (d); \
398 ((uint8_t*)(p))[1] = (d)>>8; \
399 ((uint8_t*)(p))[2] = (d)>>16; \
403 /* Parameters for AV_COPY*, AV_SWAP*, AV_ZERO* must be
404 * naturally aligned. They may be implemented using MMX,
405 * so emms_c() must be called before using any float code
409 #define AV_COPY(n, d, s) (*(uint##n##_t*)(d) = *(const uint##n##_t*)(s))
412 # define AV_COPY32(d, s) AV_COPY(32, d, s)
416 # define AV_COPY64(d, s) AV_COPY(64, d, s)
420 # define AV_COPY128(d, s) \
423 AV_COPY64((char*)(d)+8, (char*)(s)+8); \
427 #define AV_SWAP(n, a, b) FFSWAP(uint##n##_t, *(uint##n##_t*)(a), *(uint##n##_t*)(b))
430 # define AV_SWAP64(a, b) AV_SWAP(64, a, b)
433 #define AV_ZERO(n, d) (*(uint##n##_t*)(d) = 0)
436 # define AV_ZERO64(d) AV_ZERO(64, d)
440 # define AV_ZERO128(d) \
443 AV_ZERO64((char*)(d)+8); \
447 #endif /* AVUTIL_INTREADWRITE_H */