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
24 * Arch-specific headers can provide any combination of
25 * AV_[RW][BLN](16|24|32|64) and AV_(COPY|SWAP|ZERO)(64|128) macros.
26 * Preprocessor symbols must be defined, even if these are implemented
27 * as inline functions.
31 * Map AV_RNXX <-> AV_R[BL]XX for all variants provided by per-arch headers.
33 #define HAVE_BIGENDIAN 0
36 # if defined(AV_RN16) && !defined(AV_RB16)
37 # define AV_RB16(p) AV_RN16(p)
38 # elif !defined(AV_RN16) && defined(AV_RB16)
39 # define AV_RN16(p) AV_RB16(p)
42 # if defined(AV_WN16) && !defined(AV_WB16)
43 # define AV_WB16(p, v) AV_WN16(p, v)
44 # elif !defined(AV_WN16) && defined(AV_WB16)
45 # define AV_WN16(p, v) AV_WB16(p, v)
48 # if defined(AV_RN24) && !defined(AV_RB24)
49 # define AV_RB24(p) AV_RN24(p)
50 # elif !defined(AV_RN24) && defined(AV_RB24)
51 # define AV_RN24(p) AV_RB24(p)
54 # if defined(AV_WN24) && !defined(AV_WB24)
55 # define AV_WB24(p, v) AV_WN24(p, v)
56 # elif !defined(AV_WN24) && defined(AV_WB24)
57 # define AV_WN24(p, v) AV_WB24(p, v)
60 # if defined(AV_RN32) && !defined(AV_RB32)
61 # define AV_RB32(p) AV_RN32(p)
62 # elif !defined(AV_RN32) && defined(AV_RB32)
63 # define AV_RN32(p) AV_RB32(p)
66 # if defined(AV_WN32) && !defined(AV_WB32)
67 # define AV_WB32(p, v) AV_WN32(p, v)
68 # elif !defined(AV_WN32) && defined(AV_WB32)
69 # define AV_WN32(p, v) AV_WB32(p, v)
72 # if defined(AV_RN64) && !defined(AV_RB64)
73 # define AV_RB64(p) AV_RN64(p)
74 # elif !defined(AV_RN64) && defined(AV_RB64)
75 # define AV_RN64(p) AV_RB64(p)
78 # if defined(AV_WN64) && !defined(AV_WB64)
79 # define AV_WB64(p, v) AV_WN64(p, v)
80 # elif !defined(AV_WN64) && defined(AV_WB64)
81 # define AV_WN64(p, v) AV_WB64(p, v)
84 #else /* HAVE_BIGENDIAN */
86 # if defined(AV_RN16) && !defined(AV_RL16)
87 # define AV_RL16(p) AV_RN16(p)
88 # elif !defined(AV_RN16) && defined(AV_RL16)
89 # define AV_RN16(p) AV_RL16(p)
92 # if defined(AV_WN16) && !defined(AV_WL16)
93 # define AV_WL16(p, v) AV_WN16(p, v)
94 # elif !defined(AV_WN16) && defined(AV_WL16)
95 # define AV_WN16(p, v) AV_WL16(p, v)
98 # if defined(AV_RN24) && !defined(AV_RL24)
99 # define AV_RL24(p) AV_RN24(p)
100 # elif !defined(AV_RN24) && defined(AV_RL24)
101 # define AV_RN24(p) AV_RL24(p)
104 # if defined(AV_WN24) && !defined(AV_WL24)
105 # define AV_WL24(p, v) AV_WN24(p, v)
106 # elif !defined(AV_WN24) && defined(AV_WL24)
107 # define AV_WN24(p, v) AV_WL24(p, v)
110 # if defined(AV_RN32) && !defined(AV_RL32)
111 # define AV_RL32(p) AV_RN32(p)
112 # elif !defined(AV_RN32) && defined(AV_RL32)
113 # define AV_RN32(p) AV_RL32(p)
116 # if defined(AV_WN32) && !defined(AV_WL32)
117 # define AV_WL32(p, v) AV_WN32(p, v)
118 # elif !defined(AV_WN32) && defined(AV_WL32)
119 # define AV_WN32(p, v) AV_WL32(p, v)
122 # if defined(AV_RN64) && !defined(AV_RL64)
123 # define AV_RL64(p) AV_RN64(p)
124 # elif !defined(AV_RN64) && defined(AV_RL64)
125 # define AV_RN64(p) AV_RL64(p)
128 # if defined(AV_WN64) && !defined(AV_WL64)
129 # define AV_WL64(p, v) AV_WN64(p, v)
130 # elif !defined(AV_WN64) && defined(AV_WL64)
131 # define AV_WN64(p, v) AV_WL64(p, v)
134 #endif /* !HAVE_BIGENDIAN */
136 #define HAVE_ATTRIBUTE_PACKED 0
137 #define HAVE_FAST_UNALIGNED 0
139 * Define AV_[RW]N helper macros to simplify definitions not provided
140 * by per-arch headers.
143 #if HAVE_ATTRIBUTE_PACKED
145 union unaligned_64
{ uint64_t l
; } __attribute__((packed
)) av_alias
;
146 union unaligned_32
{ uint32_t l
; } __attribute__((packed
)) av_alias
;
147 union unaligned_16
{ uint16_t l
; } __attribute__((packed
)) av_alias
;
149 # define AV_RN(s, p) (((const union unaligned_##s *) (p))->l)
150 # define AV_WN(s, p, v) ((((union unaligned_##s *) (p))->l) = (v))
152 #elif defined(__DECC)
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))
157 #elif HAVE_FAST_UNALIGNED
159 # define AV_RN(s, p) (((const av_alias##s*)(p))->u##s)
160 # define AV_WN(s, p, v) (((av_alias##s*)(p))->u##s = (v))
165 # define AV_RB16(x) \
166 ((((const uint8_t*)(x))[0] << 8) | \
167 ((const uint8_t*)(x))[1])
170 # define AV_WB16(p, d) do { \
171 ((uint8_t*)(p))[1] = (d); \
172 ((uint8_t*)(p))[0] = (d)>>8; \
177 # define AV_RL16(x) \
178 ((((const uint8_t*)(x))[1] << 8) | \
179 ((const uint8_t*)(x))[0])
182 # define AV_WL16(p, d) do { \
183 ((uint8_t*)(p))[0] = (d); \
184 ((uint8_t*)(p))[1] = (d)>>8; \
189 /* Coldfire cpu's support unaligned long reads */
191 #define AV_RB32(x) (*(const uint32_t*)(x))
193 # define AV_RB32(x) \
194 ((((const uint8_t*)(x))[0] << 24) | \
195 (((const uint8_t*)(x))[1] << 16) | \
196 (((const uint8_t*)(x))[2] << 8) | \
197 ((const uint8_t*)(x))[3])
201 # define AV_WB32(p, d) do { \
202 ((uint8_t*)(p))[3] = (d); \
203 ((uint8_t*)(p))[2] = (d)>>8; \
204 ((uint8_t*)(p))[1] = (d)>>16; \
205 ((uint8_t*)(p))[0] = (d)>>24; \
210 # define AV_RL32(x) \
211 ((((const uint8_t*)(x))[3] << 24) | \
212 (((const uint8_t*)(x))[2] << 16) | \
213 (((const uint8_t*)(x))[1] << 8) | \
214 ((const uint8_t*)(x))[0])
217 # define AV_WL32(p, d) do { \
218 ((uint8_t*)(p))[0] = (d); \
219 ((uint8_t*)(p))[1] = (d)>>8; \
220 ((uint8_t*)(p))[2] = (d)>>16; \
221 ((uint8_t*)(p))[3] = (d)>>24; \
226 # define AV_RB64(x) \
227 (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
228 ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
229 ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
230 ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
231 ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
232 ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
233 ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
234 (uint64_t)((const uint8_t*)(x))[7])
237 # define AV_WB64(p, d) do { \
238 ((uint8_t*)(p))[7] = (d); \
239 ((uint8_t*)(p))[6] = (d)>>8; \
240 ((uint8_t*)(p))[5] = (d)>>16; \
241 ((uint8_t*)(p))[4] = (d)>>24; \
242 ((uint8_t*)(p))[3] = (d)>>32; \
243 ((uint8_t*)(p))[2] = (d)>>40; \
244 ((uint8_t*)(p))[1] = (d)>>48; \
245 ((uint8_t*)(p))[0] = (d)>>56; \
250 # define AV_RL64(x) \
251 (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
252 ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
253 ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
254 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
255 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
256 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
257 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
258 (uint64_t)((const uint8_t*)(x))[0])
261 # define AV_WL64(p, d) do { \
262 ((uint8_t*)(p))[0] = (d); \
263 ((uint8_t*)(p))[1] = (d)>>8; \
264 ((uint8_t*)(p))[2] = (d)>>16; \
265 ((uint8_t*)(p))[3] = (d)>>24; \
266 ((uint8_t*)(p))[4] = (d)>>32; \
267 ((uint8_t*)(p))[5] = (d)>>40; \
268 ((uint8_t*)(p))[6] = (d)>>48; \
269 ((uint8_t*)(p))[7] = (d)>>56; \
274 # define AV_RN(s, p) AV_RB##s(p)
275 # define AV_WN(s, p, v) AV_WB##s(p, v)
277 # define AV_RN(s, p) AV_RL##s(p)
278 # define AV_WN(s, p, v) AV_WL##s(p, v)
281 #endif /* HAVE_FAST_UNALIGNED */
284 # define AV_RN16(p) AV_RN(16, p)
288 # define AV_RN32(p) AV_RN(32, p)
292 # define AV_RN64(p) AV_RN(64, p)
296 # define AV_WN16(p, v) AV_WN(16, p, v)
300 # define AV_WN32(p, v) AV_WN(32, p, v)
304 # define AV_WN64(p, v) AV_WN(64, p, v)
308 # define AV_RB(s, p) AV_RN##s(p)
309 # define AV_WB(s, p, v) AV_WN##s(p, v)
310 # define AV_RL(s, p) bswap_##s(AV_RN##s(p))
311 # define AV_WL(s, p, v) AV_WN##s(p, bswap_##s(v))
313 # define AV_RB(s, p) bswap_##s(AV_RN##s(p))
314 # define AV_WB(s, p, v) AV_WN##s(p, bswap_##s(v))
315 # define AV_RL(s, p) AV_RN##s(p)
316 # define AV_WL(s, p, v) AV_WN##s(p, v)
319 #define AV_RB8(x) (((const uint8_t*)(x))[0])
320 #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
322 #define AV_RL8(x) AV_RB8(x)
323 #define AV_WL8(p, d) AV_WB8(p, d)
326 # define AV_RB16(p) AV_RB(16, p)
329 # define AV_WB16(p, v) AV_WB(16, p, v)
333 # define AV_RL16(p) AV_RL(16, p)
336 # define AV_WL16(p, v) AV_WL(16, p, v)
340 # define AV_RB32(p) AV_RB(32, p)
343 # define AV_WB32(p, v) AV_WB(32, p, v)
347 # define AV_RL32(p) AV_RL(32, p)
350 # define AV_WL32(p, v) AV_WL(32, p, v)
354 # define AV_RB64(p) AV_RB(64, p)
357 # define AV_WB64(p, v) AV_WB(64, p, v)
361 # define AV_RL64(p) AV_RL(64, p)
364 # define AV_WL64(p, v) AV_WL(64, p, v)
368 # define AV_RB24(x) \
369 ((((const uint8_t*)(x))[0] << 16) | \
370 (((const uint8_t*)(x))[1] << 8) | \
371 ((const uint8_t*)(x))[2])
374 # define AV_WB24(p, d) do { \
375 ((uint8_t*)(p))[2] = (d); \
376 ((uint8_t*)(p))[1] = (d)>>8; \
377 ((uint8_t*)(p))[0] = (d)>>16; \
382 # define AV_RL24(x) \
383 ((((const uint8_t*)(x))[2] << 16) | \
384 (((const uint8_t*)(x))[1] << 8) | \
385 ((const uint8_t*)(x))[0])
388 # define AV_WL24(p, d) do { \
389 ((uint8_t*)(p))[0] = (d); \
390 ((uint8_t*)(p))[1] = (d)>>8; \
391 ((uint8_t*)(p))[2] = (d)>>16; \
396 * The AV_[RW]NA macros access naturally aligned data
397 * in a type-safe way.
400 #define AV_RNA(s, p) (((const av_alias##s*)(p))->u##s)
401 #define AV_WNA(s, p, v) (((av_alias##s*)(p))->u##s = (v))
404 # define AV_RN16A(p) AV_RNA(16, p)
408 # define AV_RN32A(p) AV_RNA(32, p)
412 # define AV_RN64A(p) AV_RNA(64, p)
416 # define AV_WN16A(p, v) AV_WNA(16, p, v)
420 # define AV_WN32A(p, v) AV_WNA(32, p, v)
424 # define AV_WN64A(p, v) AV_WNA(64, p, v)
427 /* Parameters for AV_COPY*, AV_SWAP*, AV_ZERO* must be
428 * naturally aligned. They may be implemented using MMX,
429 * so emms_c() must be called before using any float code
433 #define AV_COPY(n, d, s) \
434 (((av_alias##n*)(d))->u##n = ((const av_alias##n*)(s))->u##n)
437 # define AV_COPY16(d, s) AV_COPY(16, d, s)
441 # define AV_COPY32(d, s) AV_COPY(32, d, s)
445 # define AV_COPY64(d, s) AV_COPY(64, d, s)
449 # define AV_COPY128(d, s) \
452 AV_COPY64((char*)(d)+8, (char*)(s)+8); \
456 #define AV_SWAP(n, a, b) FFSWAP(av_alias##n, *(av_alias##n*)(a), *(av_alias##n*)(b))
459 # define AV_SWAP64(a, b) AV_SWAP(64, a, b)
462 #define AV_ZERO(n, d) (((av_alias##n*)(d))->u##n = 0)
465 # define AV_ZERO16(d) AV_ZERO(16, d)
469 # define AV_ZERO32(d) AV_ZERO(32, d)
473 # define AV_ZERO64(d) AV_ZERO(64, d)
477 # define AV_ZERO128(d) \
480 AV_ZERO64((char*)(d)+8); \
484 #endif /* AVUTIL_INTREADWRITE_H */