19 #ifndef AVUTIL_INTREADWRITE_H
20 #define AVUTIL_INTREADWRITE_H
23 #include "libavutil/avconfig.h"
63 #ifdef HAVE_AV_CONFIG_H
87 # if defined(AV_RN16) && !defined(AV_RB16)
88 # define AV_RB16(p) AV_RN16(p)
89 # elif !defined(AV_RN16) && defined(AV_RB16)
90 # define AV_RN16(p) AV_RB16(p)
93 # if defined(AV_WN16) && !defined(AV_WB16)
94 # define AV_WB16(p, v) AV_WN16(p, v)
95 # elif !defined(AV_WN16) && defined(AV_WB16)
96 # define AV_WN16(p, v) AV_WB16(p, v)
99 # if defined(AV_RN24) && !defined(AV_RB24)
100 # define AV_RB24(p) AV_RN24(p)
101 # elif !defined(AV_RN24) && defined(AV_RB24)
102 # define AV_RN24(p) AV_RB24(p)
105 # if defined(AV_WN24) && !defined(AV_WB24)
106 # define AV_WB24(p, v) AV_WN24(p, v)
107 # elif !defined(AV_WN24) && defined(AV_WB24)
108 # define AV_WN24(p, v) AV_WB24(p, v)
111 # if defined(AV_RN32) && !defined(AV_RB32)
112 # define AV_RB32(p) AV_RN32(p)
113 # elif !defined(AV_RN32) && defined(AV_RB32)
114 # define AV_RN32(p) AV_RB32(p)
117 # if defined(AV_WN32) && !defined(AV_WB32)
118 # define AV_WB32(p, v) AV_WN32(p, v)
119 # elif !defined(AV_WN32) && defined(AV_WB32)
120 # define AV_WN32(p, v) AV_WB32(p, v)
123 # if defined(AV_RN48) && !defined(AV_RB48)
124 # define AV_RB48(p) AV_RN48(p)
125 # elif !defined(AV_RN48) && defined(AV_RB48)
126 # define AV_RN48(p) AV_RB48(p)
129 # if defined(AV_WN48) && !defined(AV_WB48)
130 # define AV_WB48(p, v) AV_WN48(p, v)
131 # elif !defined(AV_WN48) && defined(AV_WB48)
132 # define AV_WN48(p, v) AV_WB48(p, v)
135 # if defined(AV_RN64) && !defined(AV_RB64)
136 # define AV_RB64(p) AV_RN64(p)
137 # elif !defined(AV_RN64) && defined(AV_RB64)
138 # define AV_RN64(p) AV_RB64(p)
141 # if defined(AV_WN64) && !defined(AV_WB64)
142 # define AV_WB64(p, v) AV_WN64(p, v)
143 # elif !defined(AV_WN64) && defined(AV_WB64)
144 # define AV_WN64(p, v) AV_WB64(p, v)
149 # if defined(AV_RN16) && !defined(AV_RL16)
150 # define AV_RL16(p) AV_RN16(p)
151 # elif !defined(AV_RN16) && defined(AV_RL16)
152 # define AV_RN16(p) AV_RL16(p)
155 # if defined(AV_WN16) && !defined(AV_WL16)
156 # define AV_WL16(p, v) AV_WN16(p, v)
157 # elif !defined(AV_WN16) && defined(AV_WL16)
158 # define AV_WN16(p, v) AV_WL16(p, v)
161 # if defined(AV_RN24) && !defined(AV_RL24)
162 # define AV_RL24(p) AV_RN24(p)
163 # elif !defined(AV_RN24) && defined(AV_RL24)
164 # define AV_RN24(p) AV_RL24(p)
167 # if defined(AV_WN24) && !defined(AV_WL24)
168 # define AV_WL24(p, v) AV_WN24(p, v)
169 # elif !defined(AV_WN24) && defined(AV_WL24)
170 # define AV_WN24(p, v) AV_WL24(p, v)
173 # if defined(AV_RN32) && !defined(AV_RL32)
174 # define AV_RL32(p) AV_RN32(p)
175 # elif !defined(AV_RN32) && defined(AV_RL32)
176 # define AV_RN32(p) AV_RL32(p)
179 # if defined(AV_WN32) && !defined(AV_WL32)
180 # define AV_WL32(p, v) AV_WN32(p, v)
181 # elif !defined(AV_WN32) && defined(AV_WL32)
182 # define AV_WN32(p, v) AV_WL32(p, v)
185 # if defined(AV_RN48) && !defined(AV_RL48)
186 # define AV_RL48(p) AV_RN48(p)
187 # elif !defined(AV_RN48) && defined(AV_RL48)
188 # define AV_RN48(p) AV_RL48(p)
191 # if defined(AV_WN48) && !defined(AV_WL48)
192 # define AV_WL48(p, v) AV_WN48(p, v)
193 # elif !defined(AV_WN48) && defined(AV_WL48)
194 # define AV_WN48(p, v) AV_WL48(p, v)
197 # if defined(AV_RN64) && !defined(AV_RL64)
198 # define AV_RL64(p) AV_RN64(p)
199 # elif !defined(AV_RN64) && defined(AV_RL64)
200 # define AV_RN64(p) AV_RL64(p)
203 # if defined(AV_WN64) && !defined(AV_WL64)
204 # define AV_WL64(p, v) AV_WN64(p, v)
205 # elif !defined(AV_WN64) && defined(AV_WL64)
206 # define AV_WN64(p, v) AV_WL64(p, v)
216 #if defined(__GNUC__) || defined(__clang__)
222 # define AV_RN(s, p) (((const union unaligned_##s *) (p))->l)
223 # define AV_WN(s, p, v) ((((union unaligned_##s *) (p))->l) = (v))
225 #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_X64) || defined(_M_ARM64)) && AV_HAVE_FAST_UNALIGNED
227 # define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
228 # define AV_WN(s, p, v) (*((__unaligned uint##s##_t*)(p)) = (v))
230 #elif AV_HAVE_FAST_UNALIGNED
232 # define AV_RN(s, p) (((const av_alias##s*)(p))->u##s)
233 # define AV_WN(s, p, v) (((av_alias##s*)(p))->u##s = (v))
238 # define AV_RB16(x) \
239 ((((const uint8_t*)(x))[0] << 8) | \
240 ((const uint8_t*)(x))[1])
243 # define AV_WB16(p, val) do { \
244 uint16_t d = (val); \
245 ((uint8_t*)(p))[1] = (d); \
246 ((uint8_t*)(p))[0] = (d)>>8; \
251 # define AV_RL16(x) \
252 ((((const uint8_t*)(x))[1] << 8) | \
253 ((const uint8_t*)(x))[0])
256 # define AV_WL16(p, val) do { \
257 uint16_t d = (val); \
258 ((uint8_t*)(p))[0] = (d); \
259 ((uint8_t*)(p))[1] = (d)>>8; \
264 # define AV_RB32(x) \
265 (((uint32_t)((const uint8_t*)(x))[0] << 24) | \
266 (((const uint8_t*)(x))[1] << 16) | \
267 (((const uint8_t*)(x))[2] << 8) | \
268 ((const uint8_t*)(x))[3])
271 # define AV_WB32(p, val) do { \
272 uint32_t d = (val); \
273 ((uint8_t*)(p))[3] = (d); \
274 ((uint8_t*)(p))[2] = (d)>>8; \
275 ((uint8_t*)(p))[1] = (d)>>16; \
276 ((uint8_t*)(p))[0] = (d)>>24; \
281 # define AV_RL32(x) \
282 (((uint32_t)((const uint8_t*)(x))[3] << 24) | \
283 (((const uint8_t*)(x))[2] << 16) | \
284 (((const uint8_t*)(x))[1] << 8) | \
285 ((const uint8_t*)(x))[0])
288 # define AV_WL32(p, val) do { \
289 uint32_t d = (val); \
290 ((uint8_t*)(p))[0] = (d); \
291 ((uint8_t*)(p))[1] = (d)>>8; \
292 ((uint8_t*)(p))[2] = (d)>>16; \
293 ((uint8_t*)(p))[3] = (d)>>24; \
298 # define AV_RB64(x) \
299 (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
300 ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
301 ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
302 ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
303 ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
304 ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
305 ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
306 (uint64_t)((const uint8_t*)(x))[7])
309 # define AV_WB64(p, val) do { \
310 uint64_t d = (val); \
311 ((uint8_t*)(p))[7] = (d); \
312 ((uint8_t*)(p))[6] = (d)>>8; \
313 ((uint8_t*)(p))[5] = (d)>>16; \
314 ((uint8_t*)(p))[4] = (d)>>24; \
315 ((uint8_t*)(p))[3] = (d)>>32; \
316 ((uint8_t*)(p))[2] = (d)>>40; \
317 ((uint8_t*)(p))[1] = (d)>>48; \
318 ((uint8_t*)(p))[0] = (d)>>56; \
323 # define AV_RL64(x) \
324 (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
325 ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
326 ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
327 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
328 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
329 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
330 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
331 (uint64_t)((const uint8_t*)(x))[0])
334 # define AV_WL64(p, val) do { \
335 uint64_t d = (val); \
336 ((uint8_t*)(p))[0] = (d); \
337 ((uint8_t*)(p))[1] = (d)>>8; \
338 ((uint8_t*)(p))[2] = (d)>>16; \
339 ((uint8_t*)(p))[3] = (d)>>24; \
340 ((uint8_t*)(p))[4] = (d)>>32; \
341 ((uint8_t*)(p))[5] = (d)>>40; \
342 ((uint8_t*)(p))[6] = (d)>>48; \
343 ((uint8_t*)(p))[7] = (d)>>56; \
347 #if AV_HAVE_BIGENDIAN
348 # define AV_RN(s, p) AV_RB##s(p)
349 # define AV_WN(s, p, v) AV_WB##s(p, v)
351 # define AV_RN(s, p) AV_RL##s(p)
352 # define AV_WN(s, p, v) AV_WL##s(p, v)
358 # define AV_RN16(p) AV_RN(16, p)
362 # define AV_RN32(p) AV_RN(32, p)
366 # define AV_RN64(p) AV_RN(64, p)
370 # define AV_WN16(p, v) AV_WN(16, p, v)
374 # define AV_WN32(p, v) AV_WN(32, p, v)
378 # define AV_WN64(p, v) AV_WN(64, p, v)
381 #if AV_HAVE_BIGENDIAN
382 # define AV_RB(s, p) AV_RN##s(p)
383 # define AV_WB(s, p, v) AV_WN##s(p, v)
384 # define AV_RL(s, p) av_bswap##s(AV_RN##s(p))
385 # define AV_WL(s, p, v) AV_WN##s(p, av_bswap##s(v))
387 # define AV_RB(s, p) av_bswap##s(AV_RN##s(p))
388 # define AV_WB(s, p, v) AV_WN##s(p, av_bswap##s(v))
389 # define AV_RL(s, p) AV_RN##s(p)
390 # define AV_WL(s, p, v) AV_WN##s(p, v)
393 #define AV_RB8(x) (((const uint8_t*)(x))[0])
394 #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
396 #define AV_RL8(x) AV_RB8(x)
397 #define AV_WL8(p, d) AV_WB8(p, d)
400 # define AV_RB16(p) AV_RB(16, p)
403 # define AV_WB16(p, v) AV_WB(16, p, v)
407 # define AV_RL16(p) AV_RL(16, p)
410 # define AV_WL16(p, v) AV_WL(16, p, v)
414 # define AV_RB32(p) AV_RB(32, p)
417 # define AV_WB32(p, v) AV_WB(32, p, v)
421 # define AV_RL32(p) AV_RL(32, p)
424 # define AV_WL32(p, v) AV_WL(32, p, v)
428 # define AV_RB64(p) AV_RB(64, p)
431 # define AV_WB64(p, v) AV_WB(64, p, v)
435 # define AV_RL64(p) AV_RL(64, p)
438 # define AV_WL64(p, v) AV_WL(64, p, v)
442 # define AV_RB24(x) \
443 ((((const uint8_t*)(x))[0] << 16) | \
444 (((const uint8_t*)(x))[1] << 8) | \
445 ((const uint8_t*)(x))[2])
448 # define AV_WB24(p, d) do { \
449 ((uint8_t*)(p))[2] = (d); \
450 ((uint8_t*)(p))[1] = (d)>>8; \
451 ((uint8_t*)(p))[0] = (d)>>16; \
456 # define AV_RL24(x) \
457 ((((const uint8_t*)(x))[2] << 16) | \
458 (((const uint8_t*)(x))[1] << 8) | \
459 ((const uint8_t*)(x))[0])
462 # define AV_WL24(p, d) do { \
463 ((uint8_t*)(p))[0] = (d); \
464 ((uint8_t*)(p))[1] = (d)>>8; \
465 ((uint8_t*)(p))[2] = (d)>>16; \
470 # define AV_RB48(x) \
471 (((uint64_t)((const uint8_t*)(x))[0] << 40) | \
472 ((uint64_t)((const uint8_t*)(x))[1] << 32) | \
473 ((uint64_t)((const uint8_t*)(x))[2] << 24) | \
474 ((uint64_t)((const uint8_t*)(x))[3] << 16) | \
475 ((uint64_t)((const uint8_t*)(x))[4] << 8) | \
476 (uint64_t)((const uint8_t*)(x))[5])
479 # define AV_WB48(p, darg) do { \
480 uint64_t d = (darg); \
481 ((uint8_t*)(p))[5] = (d); \
482 ((uint8_t*)(p))[4] = (d)>>8; \
483 ((uint8_t*)(p))[3] = (d)>>16; \
484 ((uint8_t*)(p))[2] = (d)>>24; \
485 ((uint8_t*)(p))[1] = (d)>>32; \
486 ((uint8_t*)(p))[0] = (d)>>40; \
491 # define AV_RL48(x) \
492 (((uint64_t)((const uint8_t*)(x))[5] << 40) | \
493 ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
494 ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
495 ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
496 ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
497 (uint64_t)((const uint8_t*)(x))[0])
500 # define AV_WL48(p, darg) do { \
501 uint64_t d = (darg); \
502 ((uint8_t*)(p))[0] = (d); \
503 ((uint8_t*)(p))[1] = (d)>>8; \
504 ((uint8_t*)(p))[2] = (d)>>16; \
505 ((uint8_t*)(p))[3] = (d)>>24; \
506 ((uint8_t*)(p))[4] = (d)>>32; \
507 ((uint8_t*)(p))[5] = (d)>>40; \
516 #define AV_RNA(s, p) (((const av_alias##s*)(p))->u##s)
517 #define AV_WNA(s, p, v) (((av_alias##s*)(p))->u##s = (v))
520 # define AV_RN16A(p) AV_RNA(16, p)
524 # define AV_RN32A(p) AV_RNA(32, p)
528 # define AV_RN64A(p) AV_RNA(64, p)
532 # define AV_WN16A(p, v) AV_WNA(16, p, v)
536 # define AV_WN32A(p, v) AV_WNA(32, p, v)
540 # define AV_WN64A(p, v) AV_WNA(64, p, v)
543 #if AV_HAVE_BIGENDIAN
544 # define AV_RLA(s, p) av_bswap##s(AV_RN##s##A(p))
545 # define AV_WLA(s, p, v) AV_WN##s##A(p, av_bswap##s(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)
552 # define AV_RL64A(p) AV_RLA(64, p)
555 # define AV_WL64A(p, v) AV_WLA(64, p, v)
563 #define AV_COPYU(n, d, s) AV_WN##n(d, AV_RN##n(s));
566 # define AV_COPY16U(d, s) AV_COPYU(16, d, s)
570 # define AV_COPY32U(d, s) AV_COPYU(32, d, s)
574 # define AV_COPY64U(d, s) AV_COPYU(64, d, s)
578 # define AV_COPY128U(d, s) \
581 AV_COPY64U((char *)(d) + 8, (const char *)(s) + 8); \
591 #define AV_COPY(n, d, s) \
592 (((av_alias##n*)(d))->u##n = ((const av_alias##n*)(s))->u##n)
595 # define AV_COPY16(d, s) AV_COPY(16, d, s)
599 # define AV_COPY32(d, s) AV_COPY(32, d, s)
603 # define AV_COPY64(d, s) AV_COPY(64, d, s)
607 # define AV_COPY128(d, s) \
610 AV_COPY64((char*)(d)+8, (char*)(s)+8); \
614 #define AV_SWAP(n, a, b) FFSWAP(av_alias##n, *(av_alias##n*)(a), *(av_alias##n*)(b))
617 # define AV_SWAP64(a, b) AV_SWAP(64, a, b)
620 #define AV_ZERO(n, d) (((av_alias##n*)(d))->u##n = 0)
623 # define AV_ZERO16(d) AV_ZERO(16, d)
627 # define AV_ZERO32(d) AV_ZERO(32, d)
631 # define AV_ZERO64(d) AV_ZERO(64, d)
635 # define AV_ZERO128(d) \
638 AV_ZERO64((char*)(d)+8); \