19 #ifndef AVUTIL_INTREADWRITE_H
20 #define AVUTIL_INTREADWRITE_H
23 #include "libavutil/avconfig.h"
63 #ifdef HAVE_AV_CONFIG_H
85 # if defined(AV_RN16) && !defined(AV_RB16)
86 # define AV_RB16(p) AV_RN16(p)
87 # elif !defined(AV_RN16) && defined(AV_RB16)
88 # define AV_RN16(p) AV_RB16(p)
91 # if defined(AV_WN16) && !defined(AV_WB16)
92 # define AV_WB16(p, v) AV_WN16(p, v)
93 # elif !defined(AV_WN16) && defined(AV_WB16)
94 # define AV_WN16(p, v) AV_WB16(p, v)
97 # if defined(AV_RN24) && !defined(AV_RB24)
98 # define AV_RB24(p) AV_RN24(p)
99 # elif !defined(AV_RN24) && defined(AV_RB24)
100 # define AV_RN24(p) AV_RB24(p)
103 # if defined(AV_WN24) && !defined(AV_WB24)
104 # define AV_WB24(p, v) AV_WN24(p, v)
105 # elif !defined(AV_WN24) && defined(AV_WB24)
106 # define AV_WN24(p, v) AV_WB24(p, v)
109 # if defined(AV_RN32) && !defined(AV_RB32)
110 # define AV_RB32(p) AV_RN32(p)
111 # elif !defined(AV_RN32) && defined(AV_RB32)
112 # define AV_RN32(p) AV_RB32(p)
115 # if defined(AV_WN32) && !defined(AV_WB32)
116 # define AV_WB32(p, v) AV_WN32(p, v)
117 # elif !defined(AV_WN32) && defined(AV_WB32)
118 # define AV_WN32(p, v) AV_WB32(p, v)
121 # if defined(AV_RN48) && !defined(AV_RB48)
122 # define AV_RB48(p) AV_RN48(p)
123 # elif !defined(AV_RN48) && defined(AV_RB48)
124 # define AV_RN48(p) AV_RB48(p)
127 # if defined(AV_WN48) && !defined(AV_WB48)
128 # define AV_WB48(p, v) AV_WN48(p, v)
129 # elif !defined(AV_WN48) && defined(AV_WB48)
130 # define AV_WN48(p, v) AV_WB48(p, v)
133 # if defined(AV_RN64) && !defined(AV_RB64)
134 # define AV_RB64(p) AV_RN64(p)
135 # elif !defined(AV_RN64) && defined(AV_RB64)
136 # define AV_RN64(p) AV_RB64(p)
139 # if defined(AV_WN64) && !defined(AV_WB64)
140 # define AV_WB64(p, v) AV_WN64(p, v)
141 # elif !defined(AV_WN64) && defined(AV_WB64)
142 # define AV_WN64(p, v) AV_WB64(p, v)
147 # if defined(AV_RN16) && !defined(AV_RL16)
148 # define AV_RL16(p) AV_RN16(p)
149 # elif !defined(AV_RN16) && defined(AV_RL16)
150 # define AV_RN16(p) AV_RL16(p)
153 # if defined(AV_WN16) && !defined(AV_WL16)
154 # define AV_WL16(p, v) AV_WN16(p, v)
155 # elif !defined(AV_WN16) && defined(AV_WL16)
156 # define AV_WN16(p, v) AV_WL16(p, v)
159 # if defined(AV_RN24) && !defined(AV_RL24)
160 # define AV_RL24(p) AV_RN24(p)
161 # elif !defined(AV_RN24) && defined(AV_RL24)
162 # define AV_RN24(p) AV_RL24(p)
165 # if defined(AV_WN24) && !defined(AV_WL24)
166 # define AV_WL24(p, v) AV_WN24(p, v)
167 # elif !defined(AV_WN24) && defined(AV_WL24)
168 # define AV_WN24(p, v) AV_WL24(p, v)
171 # if defined(AV_RN32) && !defined(AV_RL32)
172 # define AV_RL32(p) AV_RN32(p)
173 # elif !defined(AV_RN32) && defined(AV_RL32)
174 # define AV_RN32(p) AV_RL32(p)
177 # if defined(AV_WN32) && !defined(AV_WL32)
178 # define AV_WL32(p, v) AV_WN32(p, v)
179 # elif !defined(AV_WN32) && defined(AV_WL32)
180 # define AV_WN32(p, v) AV_WL32(p, v)
183 # if defined(AV_RN48) && !defined(AV_RL48)
184 # define AV_RL48(p) AV_RN48(p)
185 # elif !defined(AV_RN48) && defined(AV_RL48)
186 # define AV_RN48(p) AV_RL48(p)
189 # if defined(AV_WN48) && !defined(AV_WL48)
190 # define AV_WL48(p, v) AV_WN48(p, v)
191 # elif !defined(AV_WN48) && defined(AV_WL48)
192 # define AV_WN48(p, v) AV_WL48(p, v)
195 # if defined(AV_RN64) && !defined(AV_RL64)
196 # define AV_RL64(p) AV_RN64(p)
197 # elif !defined(AV_RN64) && defined(AV_RL64)
198 # define AV_RN64(p) AV_RL64(p)
201 # if defined(AV_WN64) && !defined(AV_WL64)
202 # define AV_WL64(p, v) AV_WN64(p, v)
203 # elif !defined(AV_WN64) && defined(AV_WL64)
204 # define AV_WN64(p, v) AV_WL64(p, v)
214 #if defined(__GNUC__) || defined(__clang__)
220 # define AV_RN(s, p) (((const union unaligned_##s *) (p))->l)
221 # define AV_WN(s, p, v) ((((union unaligned_##s *) (p))->l) = (v))
223 #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_X64) || defined(_M_ARM64)) && AV_HAVE_FAST_UNALIGNED
225 # define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
226 # define AV_WN(s, p, v) (*((__unaligned uint##s##_t*)(p)) = (v))
228 #elif AV_HAVE_FAST_UNALIGNED
230 # define AV_RN(s, p) (((const av_alias##s*)(p))->u##s)
231 # define AV_WN(s, p, v) (((av_alias##s*)(p))->u##s = (v))
236 # define AV_RB16(x) \
237 ((((const uint8_t*)(x))[0] << 8) | \
238 ((const uint8_t*)(x))[1])
241 # define AV_WB16(p, val) do { \
242 uint16_t d = (val); \
243 ((uint8_t*)(p))[1] = (d); \
244 ((uint8_t*)(p))[0] = (d)>>8; \
249 # define AV_RL16(x) \
250 ((((const uint8_t*)(x))[1] << 8) | \
251 ((const uint8_t*)(x))[0])
254 # define AV_WL16(p, val) do { \
255 uint16_t d = (val); \
256 ((uint8_t*)(p))[0] = (d); \
257 ((uint8_t*)(p))[1] = (d)>>8; \
262 # define AV_RB32(x) \
263 (((uint32_t)((const uint8_t*)(x))[0] << 24) | \
264 (((const uint8_t*)(x))[1] << 16) | \
265 (((const uint8_t*)(x))[2] << 8) | \
266 ((const uint8_t*)(x))[3])
269 # define AV_WB32(p, val) do { \
270 uint32_t d = (val); \
271 ((uint8_t*)(p))[3] = (d); \
272 ((uint8_t*)(p))[2] = (d)>>8; \
273 ((uint8_t*)(p))[1] = (d)>>16; \
274 ((uint8_t*)(p))[0] = (d)>>24; \
279 # define AV_RL32(x) \
280 (((uint32_t)((const uint8_t*)(x))[3] << 24) | \
281 (((const uint8_t*)(x))[2] << 16) | \
282 (((const uint8_t*)(x))[1] << 8) | \
283 ((const uint8_t*)(x))[0])
286 # define AV_WL32(p, val) do { \
287 uint32_t d = (val); \
288 ((uint8_t*)(p))[0] = (d); \
289 ((uint8_t*)(p))[1] = (d)>>8; \
290 ((uint8_t*)(p))[2] = (d)>>16; \
291 ((uint8_t*)(p))[3] = (d)>>24; \
296 # define AV_RB64(x) \
297 (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
298 ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
299 ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
300 ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
301 ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
302 ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
303 ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
304 (uint64_t)((const uint8_t*)(x))[7])
307 # define AV_WB64(p, val) do { \
308 uint64_t d = (val); \
309 ((uint8_t*)(p))[7] = (d); \
310 ((uint8_t*)(p))[6] = (d)>>8; \
311 ((uint8_t*)(p))[5] = (d)>>16; \
312 ((uint8_t*)(p))[4] = (d)>>24; \
313 ((uint8_t*)(p))[3] = (d)>>32; \
314 ((uint8_t*)(p))[2] = (d)>>40; \
315 ((uint8_t*)(p))[1] = (d)>>48; \
316 ((uint8_t*)(p))[0] = (d)>>56; \
321 # define AV_RL64(x) \
322 (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
323 ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
324 ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
325 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
326 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
327 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
328 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
329 (uint64_t)((const uint8_t*)(x))[0])
332 # define AV_WL64(p, val) do { \
333 uint64_t d = (val); \
334 ((uint8_t*)(p))[0] = (d); \
335 ((uint8_t*)(p))[1] = (d)>>8; \
336 ((uint8_t*)(p))[2] = (d)>>16; \
337 ((uint8_t*)(p))[3] = (d)>>24; \
338 ((uint8_t*)(p))[4] = (d)>>32; \
339 ((uint8_t*)(p))[5] = (d)>>40; \
340 ((uint8_t*)(p))[6] = (d)>>48; \
341 ((uint8_t*)(p))[7] = (d)>>56; \
345 #if AV_HAVE_BIGENDIAN
346 # define AV_RN(s, p) AV_RB##s(p)
347 # define AV_WN(s, p, v) AV_WB##s(p, v)
349 # define AV_RN(s, p) AV_RL##s(p)
350 # define AV_WN(s, p, v) AV_WL##s(p, v)
356 # define AV_RN16(p) AV_RN(16, p)
360 # define AV_RN32(p) AV_RN(32, p)
364 # define AV_RN64(p) AV_RN(64, p)
368 # define AV_WN16(p, v) AV_WN(16, p, v)
372 # define AV_WN32(p, v) AV_WN(32, p, v)
376 # define AV_WN64(p, v) AV_WN(64, p, v)
379 #if AV_HAVE_BIGENDIAN
380 # define AV_RB(s, p) AV_RN##s(p)
381 # define AV_WB(s, p, v) AV_WN##s(p, v)
382 # define AV_RL(s, p) av_bswap##s(AV_RN##s(p))
383 # define AV_WL(s, p, v) AV_WN##s(p, av_bswap##s(v))
385 # define AV_RB(s, p) av_bswap##s(AV_RN##s(p))
386 # define AV_WB(s, p, v) AV_WN##s(p, av_bswap##s(v))
387 # define AV_RL(s, p) AV_RN##s(p)
388 # define AV_WL(s, p, v) AV_WN##s(p, v)
391 #define AV_RB8(x) (((const uint8_t*)(x))[0])
392 #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
394 #define AV_RL8(x) AV_RB8(x)
395 #define AV_WL8(p, d) AV_WB8(p, d)
398 # define AV_RB16(p) AV_RB(16, p)
401 # define AV_WB16(p, v) AV_WB(16, p, v)
405 # define AV_RL16(p) AV_RL(16, p)
408 # define AV_WL16(p, v) AV_WL(16, p, v)
412 # define AV_RB32(p) AV_RB(32, p)
415 # define AV_WB32(p, v) AV_WB(32, p, v)
419 # define AV_RL32(p) AV_RL(32, p)
422 # define AV_WL32(p, v) AV_WL(32, p, v)
426 # define AV_RB64(p) AV_RB(64, p)
429 # define AV_WB64(p, v) AV_WB(64, p, v)
433 # define AV_RL64(p) AV_RL(64, p)
436 # define AV_WL64(p, v) AV_WL(64, p, v)
440 # define AV_RB24(x) \
441 ((((const uint8_t*)(x))[0] << 16) | \
442 (((const uint8_t*)(x))[1] << 8) | \
443 ((const uint8_t*)(x))[2])
446 # define AV_WB24(p, d) do { \
447 ((uint8_t*)(p))[2] = (d); \
448 ((uint8_t*)(p))[1] = (d)>>8; \
449 ((uint8_t*)(p))[0] = (d)>>16; \
454 # define AV_RL24(x) \
455 ((((const uint8_t*)(x))[2] << 16) | \
456 (((const uint8_t*)(x))[1] << 8) | \
457 ((const uint8_t*)(x))[0])
460 # define AV_WL24(p, d) do { \
461 ((uint8_t*)(p))[0] = (d); \
462 ((uint8_t*)(p))[1] = (d)>>8; \
463 ((uint8_t*)(p))[2] = (d)>>16; \
468 # define AV_RB48(x) \
469 (((uint64_t)((const uint8_t*)(x))[0] << 40) | \
470 ((uint64_t)((const uint8_t*)(x))[1] << 32) | \
471 ((uint64_t)((const uint8_t*)(x))[2] << 24) | \
472 ((uint64_t)((const uint8_t*)(x))[3] << 16) | \
473 ((uint64_t)((const uint8_t*)(x))[4] << 8) | \
474 (uint64_t)((const uint8_t*)(x))[5])
477 # define AV_WB48(p, darg) do { \
478 uint64_t d = (darg); \
479 ((uint8_t*)(p))[5] = (d); \
480 ((uint8_t*)(p))[4] = (d)>>8; \
481 ((uint8_t*)(p))[3] = (d)>>16; \
482 ((uint8_t*)(p))[2] = (d)>>24; \
483 ((uint8_t*)(p))[1] = (d)>>32; \
484 ((uint8_t*)(p))[0] = (d)>>40; \
489 # define AV_RL48(x) \
490 (((uint64_t)((const uint8_t*)(x))[5] << 40) | \
491 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
492 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
493 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
494 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
495 (uint64_t)((const uint8_t*)(x))[0])
498 # define AV_WL48(p, darg) do { \
499 uint64_t d = (darg); \
500 ((uint8_t*)(p))[0] = (d); \
501 ((uint8_t*)(p))[1] = (d)>>8; \
502 ((uint8_t*)(p))[2] = (d)>>16; \
503 ((uint8_t*)(p))[3] = (d)>>24; \
504 ((uint8_t*)(p))[4] = (d)>>32; \
505 ((uint8_t*)(p))[5] = (d)>>40; \
514 #define AV_RNA(s, p) (((const av_alias##s*)(p))->u##s)
515 #define AV_WNA(s, p, v) (((av_alias##s*)(p))->u##s = (v))
518 # define AV_RN16A(p) AV_RNA(16, p)
522 # define AV_RN32A(p) AV_RNA(32, p)
526 # define AV_RN64A(p) AV_RNA(64, p)
530 # define AV_WN16A(p, v) AV_WNA(16, p, v)
534 # define AV_WN32A(p, v) AV_WNA(32, p, v)
538 # define AV_WN64A(p, v) AV_WNA(64, p, v)
541 #if AV_HAVE_BIGENDIAN
542 # define AV_RLA(s, p) av_bswap##s(AV_RN##s##A(p))
543 # define AV_WLA(s, p, v) AV_WN##s##A(p, av_bswap##s(v))
544 # define AV_RBA(s, p) AV_RN##s##A(p)
545 # define AV_WBA(s, p, v) AV_WN##s##A(p, v)
547 # define AV_RLA(s, p) AV_RN##s##A(p)
548 # define AV_WLA(s, p, v) AV_WN##s##A(p, v)
549 # define AV_RBA(s, p) av_bswap##s(AV_RN##s##A(p))
550 # define AV_WBA(s, p, v) AV_WN##s##A(p, av_bswap##s(v))
554 # define AV_RL16A(p) AV_RLA(16, p)
557 # define AV_WL16A(p, v) AV_WLA(16, p, v)
561 # define AV_RB16A(p) AV_RBA(16, p)
564 # define AV_WB16A(p, v) AV_WBA(16, p, v)
568 # define AV_RL32A(p) AV_RLA(32, p)
571 # define AV_WL32A(p, v) AV_WLA(32, p, v)
575 # define AV_RB32A(p) AV_RBA(32, p)
578 # define AV_WB32A(p, v) AV_WBA(32, p, v)
582 # define AV_RL64A(p) AV_RLA(64, p)
585 # define AV_WL64A(p, v) AV_WLA(64, p, v)
589 # define AV_RB64A(p) AV_RBA(64, p)
592 # define AV_WB64A(p, v) AV_WBA(64, p, v)
600 #define AV_COPYU(n, d, s) AV_WN##n(d, AV_RN##n(s));
603 # define AV_COPY16U(d, s) AV_COPYU(16, d, s)
607 # define AV_COPY32U(d, s) AV_COPYU(32, d, s)
611 # define AV_COPY64U(d, s) AV_COPYU(64, d, s)
615 # define AV_COPY128U(d, s) \
618 AV_COPY64U((char *)(d) + 8, (const char *)(s) + 8); \
626 #define AV_COPY(n, d, s) \
627 (((av_alias##n*)(d))->u##n = ((const av_alias##n*)(s))->u##n)
630 # define AV_COPY16(d, s) AV_COPY(16, d, s)
634 # define AV_COPY32(d, s) AV_COPY(32, d, s)
638 # define AV_COPY64(d, s) AV_COPY(64, d, s)
642 # define AV_COPY128(d, s) \
645 AV_COPY64((char*)(d)+8, (char*)(s)+8); \
649 #define AV_SWAP(n, a, b) FFSWAP(av_alias##n, *(av_alias##n*)(a), *(av_alias##n*)(b))
652 # define AV_SWAP64(a, b) AV_SWAP(64, a, b)
655 #define AV_ZERO(n, d) (((av_alias##n*)(d))->u##n = 0)
658 # define AV_ZERO16(d) AV_ZERO(16, d)
662 # define AV_ZERO32(d) AV_ZERO(32, d)
666 # define AV_ZERO64(d) AV_ZERO(64, d)
670 # define AV_ZERO128(d) \
673 AV_ZERO64((char*)(d)+8); \