19 #ifndef AVUTIL_INTREADWRITE_H
20 #define AVUTIL_INTREADWRITE_H
23 #include "libavutil/avconfig.h"
55 #ifdef HAVE_AV_CONFIG_H
81 # if defined(AV_RN16) && !defined(AV_RB16)
82 # define AV_RB16(p) AV_RN16(p)
83 # elif !defined(AV_RN16) && defined(AV_RB16)
84 # define AV_RN16(p) AV_RB16(p)
87 # if defined(AV_WN16) && !defined(AV_WB16)
88 # define AV_WB16(p, v) AV_WN16(p, v)
89 # elif !defined(AV_WN16) && defined(AV_WB16)
90 # define AV_WN16(p, v) AV_WB16(p, v)
93 # if defined(AV_RN24) && !defined(AV_RB24)
94 # define AV_RB24(p) AV_RN24(p)
95 # elif !defined(AV_RN24) && defined(AV_RB24)
96 # define AV_RN24(p) AV_RB24(p)
99 # if defined(AV_WN24) && !defined(AV_WB24)
100 # define AV_WB24(p, v) AV_WN24(p, v)
101 # elif !defined(AV_WN24) && defined(AV_WB24)
102 # define AV_WN24(p, v) AV_WB24(p, v)
105 # if defined(AV_RN32) && !defined(AV_RB32)
106 # define AV_RB32(p) AV_RN32(p)
107 # elif !defined(AV_RN32) && defined(AV_RB32)
108 # define AV_RN32(p) AV_RB32(p)
111 # if defined(AV_WN32) && !defined(AV_WB32)
112 # define AV_WB32(p, v) AV_WN32(p, v)
113 # elif !defined(AV_WN32) && defined(AV_WB32)
114 # define AV_WN32(p, v) AV_WB32(p, v)
117 # if defined(AV_RN48) && !defined(AV_RB48)
118 # define AV_RB48(p) AV_RN48(p)
119 # elif !defined(AV_RN48) && defined(AV_RB48)
120 # define AV_RN48(p) AV_RB48(p)
123 # if defined(AV_WN48) && !defined(AV_WB48)
124 # define AV_WB48(p, v) AV_WN48(p, v)
125 # elif !defined(AV_WN48) && defined(AV_WB48)
126 # define AV_WN48(p, v) AV_WB48(p, v)
129 # if defined(AV_RN64) && !defined(AV_RB64)
130 # define AV_RB64(p) AV_RN64(p)
131 # elif !defined(AV_RN64) && defined(AV_RB64)
132 # define AV_RN64(p) AV_RB64(p)
135 # if defined(AV_WN64) && !defined(AV_WB64)
136 # define AV_WB64(p, v) AV_WN64(p, v)
137 # elif !defined(AV_WN64) && defined(AV_WB64)
138 # define AV_WN64(p, v) AV_WB64(p, v)
143 # if defined(AV_RN16) && !defined(AV_RL16)
144 # define AV_RL16(p) AV_RN16(p)
145 # elif !defined(AV_RN16) && defined(AV_RL16)
146 # define AV_RN16(p) AV_RL16(p)
149 # if defined(AV_WN16) && !defined(AV_WL16)
150 # define AV_WL16(p, v) AV_WN16(p, v)
151 # elif !defined(AV_WN16) && defined(AV_WL16)
152 # define AV_WN16(p, v) AV_WL16(p, v)
155 # if defined(AV_RN24) && !defined(AV_RL24)
156 # define AV_RL24(p) AV_RN24(p)
157 # elif !defined(AV_RN24) && defined(AV_RL24)
158 # define AV_RN24(p) AV_RL24(p)
161 # if defined(AV_WN24) && !defined(AV_WL24)
162 # define AV_WL24(p, v) AV_WN24(p, v)
163 # elif !defined(AV_WN24) && defined(AV_WL24)
164 # define AV_WN24(p, v) AV_WL24(p, v)
167 # if defined(AV_RN32) && !defined(AV_RL32)
168 # define AV_RL32(p) AV_RN32(p)
169 # elif !defined(AV_RN32) && defined(AV_RL32)
170 # define AV_RN32(p) AV_RL32(p)
173 # if defined(AV_WN32) && !defined(AV_WL32)
174 # define AV_WL32(p, v) AV_WN32(p, v)
175 # elif !defined(AV_WN32) && defined(AV_WL32)
176 # define AV_WN32(p, v) AV_WL32(p, v)
179 # if defined(AV_RN48) && !defined(AV_RL48)
180 # define AV_RL48(p) AV_RN48(p)
181 # elif !defined(AV_RN48) && defined(AV_RL48)
182 # define AV_RN48(p) AV_RL48(p)
185 # if defined(AV_WN48) && !defined(AV_WL48)
186 # define AV_WL48(p, v) AV_WN48(p, v)
187 # elif !defined(AV_WN48) && defined(AV_WL48)
188 # define AV_WN48(p, v) AV_WL48(p, v)
191 # if defined(AV_RN64) && !defined(AV_RL64)
192 # define AV_RL64(p) AV_RN64(p)
193 # elif !defined(AV_RN64) && defined(AV_RL64)
194 # define AV_RN64(p) AV_RL64(p)
197 # if defined(AV_WN64) && !defined(AV_WL64)
198 # define AV_WL64(p, v) AV_WN64(p, v)
199 # elif !defined(AV_WN64) && defined(AV_WL64)
200 # define AV_WN64(p, v) AV_WL64(p, v)
210 #if defined(__GNUC__) && !defined(__TI_COMPILER_VERSION__)
212 union unaligned_64 { uint64_t l; } __attribute__((packed))
av_alias;
213 union unaligned_32 { uint32_t l; } __attribute__((packed))
av_alias;
214 union unaligned_16 { uint16_t l; } __attribute__((packed))
av_alias;
216 # define AV_RN(s, p) (((const union unaligned_##s *) (p))->l)
217 # define AV_WN(s, p, v) ((((union unaligned_##s *) (p))->l) = (v))
219 #elif defined(__DECC)
221 # define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
222 # define AV_WN(s, p, v) (*((__unaligned uint##s##_t*)(p)) = (v))
224 #elif AV_HAVE_FAST_UNALIGNED
226 # define AV_RN(s, p) (((const av_alias##s*)(p))->u##s)
227 # define AV_WN(s, p, v) (((av_alias##s*)(p))->u##s = (v))
232 # define AV_RB16(x) \
233 ((((const uint8_t*)(x))[0] << 8) | \
234 ((const uint8_t*)(x))[1])
237 # define AV_WB16(p, darg) do { \
238 unsigned d = (darg); \
239 ((uint8_t*)(p))[1] = (d); \
240 ((uint8_t*)(p))[0] = (d)>>8; \
245 # define AV_RL16(x) \
246 ((((const uint8_t*)(x))[1] << 8) | \
247 ((const uint8_t*)(x))[0])
250 # define AV_WL16(p, darg) do { \
251 unsigned d = (darg); \
252 ((uint8_t*)(p))[0] = (d); \
253 ((uint8_t*)(p))[1] = (d)>>8; \
258 # define AV_RB32(x) \
259 (((uint32_t)((const uint8_t*)(x))[0] << 24) | \
260 (((const uint8_t*)(x))[1] << 16) | \
261 (((const uint8_t*)(x))[2] << 8) | \
262 ((const uint8_t*)(x))[3])
265 # define AV_WB32(p, darg) do { \
266 unsigned d = (darg); \
267 ((uint8_t*)(p))[3] = (d); \
268 ((uint8_t*)(p))[2] = (d)>>8; \
269 ((uint8_t*)(p))[1] = (d)>>16; \
270 ((uint8_t*)(p))[0] = (d)>>24; \
275 # define AV_RL32(x) \
276 (((uint32_t)((const uint8_t*)(x))[3] << 24) | \
277 (((const uint8_t*)(x))[2] << 16) | \
278 (((const uint8_t*)(x))[1] << 8) | \
279 ((const uint8_t*)(x))[0])
282 # define AV_WL32(p, darg) do { \
283 unsigned d = (darg); \
284 ((uint8_t*)(p))[0] = (d); \
285 ((uint8_t*)(p))[1] = (d)>>8; \
286 ((uint8_t*)(p))[2] = (d)>>16; \
287 ((uint8_t*)(p))[3] = (d)>>24; \
292 # define AV_RB64(x) \
293 (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
294 ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
295 ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
296 ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
297 ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
298 ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
299 ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
300 (uint64_t)((const uint8_t*)(x))[7])
303 # define AV_WB64(p, darg) do { \
304 uint64_t d = (darg); \
305 ((uint8_t*)(p))[7] = (d); \
306 ((uint8_t*)(p))[6] = (d)>>8; \
307 ((uint8_t*)(p))[5] = (d)>>16; \
308 ((uint8_t*)(p))[4] = (d)>>24; \
309 ((uint8_t*)(p))[3] = (d)>>32; \
310 ((uint8_t*)(p))[2] = (d)>>40; \
311 ((uint8_t*)(p))[1] = (d)>>48; \
312 ((uint8_t*)(p))[0] = (d)>>56; \
317 # define AV_RL64(x) \
318 (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
319 ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
320 ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
321 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
322 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
323 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
324 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
325 (uint64_t)((const uint8_t*)(x))[0])
328 # define AV_WL64(p, darg) do { \
329 uint64_t d = (darg); \
330 ((uint8_t*)(p))[0] = (d); \
331 ((uint8_t*)(p))[1] = (d)>>8; \
332 ((uint8_t*)(p))[2] = (d)>>16; \
333 ((uint8_t*)(p))[3] = (d)>>24; \
334 ((uint8_t*)(p))[4] = (d)>>32; \
335 ((uint8_t*)(p))[5] = (d)>>40; \
336 ((uint8_t*)(p))[6] = (d)>>48; \
337 ((uint8_t*)(p))[7] = (d)>>56; \
341 #if AV_HAVE_BIGENDIAN
342 # define AV_RN(s, p) AV_RB##s(p)
343 # define AV_WN(s, p, v) AV_WB##s(p, v)
345 # define AV_RN(s, p) AV_RL##s(p)
346 # define AV_WN(s, p, v) AV_WL##s(p, v)
352 # define AV_RN16(p) AV_RN(16, p)
356 # define AV_RN32(p) AV_RN(32, p)
360 # define AV_RN64(p) AV_RN(64, p)
364 # define AV_WN16(p, v) AV_WN(16, p, v)
368 # define AV_WN32(p, v) AV_WN(32, p, v)
372 # define AV_WN64(p, v) AV_WN(64, p, v)
375 #if AV_HAVE_BIGENDIAN
376 # define AV_RB(s, p) AV_RN##s(p)
377 # define AV_WB(s, p, v) AV_WN##s(p, v)
378 # define AV_RL(s, p) av_bswap##s(AV_RN##s(p))
379 # define AV_WL(s, p, v) AV_WN##s(p, av_bswap##s(v))
381 # define AV_RB(s, p) av_bswap##s(AV_RN##s(p))
382 # define AV_WB(s, p, v) AV_WN##s(p, av_bswap##s(v))
383 # define AV_RL(s, p) AV_RN##s(p)
384 # define AV_WL(s, p, v) AV_WN##s(p, v)
387 #define AV_RB8(x) (((const uint8_t*)(x))[0])
388 #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
390 #define AV_RL8(x) AV_RB8(x)
391 #define AV_WL8(p, d) AV_WB8(p, d)
394 # define AV_RB16(p) AV_RB(16, p)
397 # define AV_WB16(p, v) AV_WB(16, p, v)
401 # define AV_RL16(p) AV_RL(16, p)
404 # define AV_WL16(p, v) AV_WL(16, p, v)
408 # define AV_RB32(p) AV_RB(32, p)
411 # define AV_WB32(p, v) AV_WB(32, p, v)
415 # define AV_RL32(p) AV_RL(32, p)
418 # define AV_WL32(p, v) AV_WL(32, p, v)
422 # define AV_RB64(p) AV_RB(64, p)
425 # define AV_WB64(p, v) AV_WB(64, p, v)
429 # define AV_RL64(p) AV_RL(64, p)
432 # define AV_WL64(p, v) AV_WL(64, p, v)
436 # define AV_RB24(x) \
437 ((((const uint8_t*)(x))[0] << 16) | \
438 (((const uint8_t*)(x))[1] << 8) | \
439 ((const uint8_t*)(x))[2])
442 # define AV_WB24(p, d) do { \
443 ((uint8_t*)(p))[2] = (d); \
444 ((uint8_t*)(p))[1] = (d)>>8; \
445 ((uint8_t*)(p))[0] = (d)>>16; \
450 # define AV_RL24(x) \
451 ((((const uint8_t*)(x))[2] << 16) | \
452 (((const uint8_t*)(x))[1] << 8) | \
453 ((const uint8_t*)(x))[0])
456 # define AV_WL24(p, d) do { \
457 ((uint8_t*)(p))[0] = (d); \
458 ((uint8_t*)(p))[1] = (d)>>8; \
459 ((uint8_t*)(p))[2] = (d)>>16; \
464 # define AV_RB48(x) \
465 (((uint64_t)((const uint8_t*)(x))[0] << 40) | \
466 ((uint64_t)((const uint8_t*)(x))[1] << 32) | \
467 ((uint64_t)((const uint8_t*)(x))[2] << 24) | \
468 ((uint64_t)((const uint8_t*)(x))[3] << 16) | \
469 ((uint64_t)((const uint8_t*)(x))[4] << 8) | \
470 (uint64_t)((const uint8_t*)(x))[5])
473 # define AV_WB48(p, darg) do { \
474 uint64_t d = (darg); \
475 ((uint8_t*)(p))[5] = (d); \
476 ((uint8_t*)(p))[4] = (d)>>8; \
477 ((uint8_t*)(p))[3] = (d)>>16; \
478 ((uint8_t*)(p))[2] = (d)>>24; \
479 ((uint8_t*)(p))[1] = (d)>>32; \
480 ((uint8_t*)(p))[0] = (d)>>40; \
485 # define AV_RL48(x) \
486 (((uint64_t)((const uint8_t*)(x))[5] << 40) | \
487 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
488 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
489 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
490 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
491 (uint64_t)((const uint8_t*)(x))[0])
494 # define AV_WL48(p, darg) do { \
495 uint64_t d = (darg); \
496 ((uint8_t*)(p))[0] = (d); \
497 ((uint8_t*)(p))[1] = (d)>>8; \
498 ((uint8_t*)(p))[2] = (d)>>16; \
499 ((uint8_t*)(p))[3] = (d)>>24; \
500 ((uint8_t*)(p))[4] = (d)>>32; \
501 ((uint8_t*)(p))[5] = (d)>>40; \
510 #define AV_RNA(s, p) (((const av_alias##s*)(p))->u##s)
511 #define AV_WNA(s, p, v) (((av_alias##s*)(p))->u##s = (v))
514 # define AV_RN16A(p) AV_RNA(16, p)
518 # define AV_RN32A(p) AV_RNA(32, p)
522 # define AV_RN64A(p) AV_RNA(64, p)
526 # define AV_WN16A(p, v) AV_WNA(16, p, v)
530 # define AV_WN32A(p, v) AV_WNA(32, p, v)
534 # define AV_WN64A(p, v) AV_WNA(64, p, v)
542 #define AV_COPYU(n, d, s) AV_WN##n(d, AV_RN##n(s));
545 # define AV_COPY16U(d, s) AV_COPYU(16, d, s)
549 # define AV_COPY32U(d, s) AV_COPYU(32, d, s)
553 # define AV_COPY64U(d, s) AV_COPYU(64, d, s)
557 # define AV_COPY128U(d, s) \
560 AV_COPY64U((char *)(d) + 8, (const char *)(s) + 8); \
570 #define AV_COPY(n, d, s) \
571 (((av_alias##n*)(d))->u##n = ((const av_alias##n*)(s))->u##n)
574 # define AV_COPY16(d, s) AV_COPY(16, d, s)
578 # define AV_COPY32(d, s) AV_COPY(32, d, s)
582 # define AV_COPY64(d, s) AV_COPY(64, d, s)
586 # define AV_COPY128(d, s) \
589 AV_COPY64((char*)(d)+8, (char*)(s)+8); \
593 #define AV_SWAP(n, a, b) FFSWAP(av_alias##n, *(av_alias##n*)(a), *(av_alias##n*)(b))
596 # define AV_SWAP64(a, b) AV_SWAP(64, a, b)
599 #define AV_ZERO(n, d) (((av_alias##n*)(d))->u##n = 0)
602 # define AV_ZERO16(d) AV_ZERO(16, d)
606 # define AV_ZERO32(d) AV_ZERO(32, d)
610 # define AV_ZERO64(d) AV_ZERO(64, d)
614 # define AV_ZERO128(d) \
617 AV_ZERO64((char*)(d)+8); \