intreadwrite.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. */
  18. #ifndef AVUTIL_INTREADWRITE_H
  19. #define AVUTIL_INTREADWRITE_H
  20. #include <stdint.h>
  21. #include "libavutil/avconfig.h"
  22. #include "attributes.h"
  23. #include "bswap.h"
  24. typedef union {
  25. uint64_t u64;
  26. uint32_t u32[2];
  27. uint16_t u16[4];
  28. uint8_t u8 [8];
  29. double f64;
  30. float f32[2];
  31. } av_alias av_alias64;
  32. typedef union {
  33. uint32_t u32;
  34. uint16_t u16[2];
  35. uint8_t u8 [4];
  36. float f32;
  37. } av_alias av_alias32;
  38. typedef union {
  39. uint16_t u16;
  40. uint8_t u8 [2];
  41. } av_alias av_alias16;
  42. /*
  43. * Arch-specific headers can provide any combination of
  44. * AV_[RW][BLN](16|24|32|64) and AV_(COPY|SWAP|ZERO)(64|128) macros.
  45. * Preprocessor symbols must be defined, even if these are implemented
  46. * as inline functions.
  47. */
  48. #ifdef HAVE_AV_CONFIG_H
  49. #include "config.h"
  50. #if ARCH_ARM
  51. # include "arm/intreadwrite.h"
  52. #elif ARCH_AVR32
  53. # include "avr32/intreadwrite.h"
  54. #elif ARCH_MIPS
  55. # include "mips/intreadwrite.h"
  56. #elif ARCH_PPC
  57. # include "ppc/intreadwrite.h"
  58. #elif ARCH_TOMI
  59. # include "tomi/intreadwrite.h"
  60. #elif ARCH_X86
  61. # include "x86/intreadwrite.h"
  62. #endif
  63. #endif /* HAVE_AV_CONFIG_H */
  64. /*
  65. * Map AV_RNXX <-> AV_R[BL]XX for all variants provided by per-arch headers.
  66. */
  67. #if AV_HAVE_BIGENDIAN
  68. # if defined(AV_RN16) && !defined(AV_RB16)
  69. # define AV_RB16(p) AV_RN16(p)
  70. # elif !defined(AV_RN16) && defined(AV_RB16)
  71. # define AV_RN16(p) AV_RB16(p)
  72. # endif
  73. # if defined(AV_WN16) && !defined(AV_WB16)
  74. # define AV_WB16(p, v) AV_WN16(p, v)
  75. # elif !defined(AV_WN16) && defined(AV_WB16)
  76. # define AV_WN16(p, v) AV_WB16(p, v)
  77. # endif
  78. # if defined(AV_RN24) && !defined(AV_RB24)
  79. # define AV_RB24(p) AV_RN24(p)
  80. # elif !defined(AV_RN24) && defined(AV_RB24)
  81. # define AV_RN24(p) AV_RB24(p)
  82. # endif
  83. # if defined(AV_WN24) && !defined(AV_WB24)
  84. # define AV_WB24(p, v) AV_WN24(p, v)
  85. # elif !defined(AV_WN24) && defined(AV_WB24)
  86. # define AV_WN24(p, v) AV_WB24(p, v)
  87. # endif
  88. # if defined(AV_RN32) && !defined(AV_RB32)
  89. # define AV_RB32(p) AV_RN32(p)
  90. # elif !defined(AV_RN32) && defined(AV_RB32)
  91. # define AV_RN32(p) AV_RB32(p)
  92. # endif
  93. # if defined(AV_WN32) && !defined(AV_WB32)
  94. # define AV_WB32(p, v) AV_WN32(p, v)
  95. # elif !defined(AV_WN32) && defined(AV_WB32)
  96. # define AV_WN32(p, v) AV_WB32(p, v)
  97. # endif
  98. # if defined(AV_RN64) && !defined(AV_RB64)
  99. # define AV_RB64(p) AV_RN64(p)
  100. # elif !defined(AV_RN64) && defined(AV_RB64)
  101. # define AV_RN64(p) AV_RB64(p)
  102. # endif
  103. # if defined(AV_WN64) && !defined(AV_WB64)
  104. # define AV_WB64(p, v) AV_WN64(p, v)
  105. # elif !defined(AV_WN64) && defined(AV_WB64)
  106. # define AV_WN64(p, v) AV_WB64(p, v)
  107. # endif
  108. #else /* AV_HAVE_BIGENDIAN */
  109. # if defined(AV_RN16) && !defined(AV_RL16)
  110. # define AV_RL16(p) AV_RN16(p)
  111. # elif !defined(AV_RN16) && defined(AV_RL16)
  112. # define AV_RN16(p) AV_RL16(p)
  113. # endif
  114. # if defined(AV_WN16) && !defined(AV_WL16)
  115. # define AV_WL16(p, v) AV_WN16(p, v)
  116. # elif !defined(AV_WN16) && defined(AV_WL16)
  117. # define AV_WN16(p, v) AV_WL16(p, v)
  118. # endif
  119. # if defined(AV_RN24) && !defined(AV_RL24)
  120. # define AV_RL24(p) AV_RN24(p)
  121. # elif !defined(AV_RN24) && defined(AV_RL24)
  122. # define AV_RN24(p) AV_RL24(p)
  123. # endif
  124. # if defined(AV_WN24) && !defined(AV_WL24)
  125. # define AV_WL24(p, v) AV_WN24(p, v)
  126. # elif !defined(AV_WN24) && defined(AV_WL24)
  127. # define AV_WN24(p, v) AV_WL24(p, v)
  128. # endif
  129. # if defined(AV_RN32) && !defined(AV_RL32)
  130. # define AV_RL32(p) AV_RN32(p)
  131. # elif !defined(AV_RN32) && defined(AV_RL32)
  132. # define AV_RN32(p) AV_RL32(p)
  133. # endif
  134. # if defined(AV_WN32) && !defined(AV_WL32)
  135. # define AV_WL32(p, v) AV_WN32(p, v)
  136. # elif !defined(AV_WN32) && defined(AV_WL32)
  137. # define AV_WN32(p, v) AV_WL32(p, v)
  138. # endif
  139. # if defined(AV_RN64) && !defined(AV_RL64)
  140. # define AV_RL64(p) AV_RN64(p)
  141. # elif !defined(AV_RN64) && defined(AV_RL64)
  142. # define AV_RN64(p) AV_RL64(p)
  143. # endif
  144. # if defined(AV_WN64) && !defined(AV_WL64)
  145. # define AV_WL64(p, v) AV_WN64(p, v)
  146. # elif !defined(AV_WN64) && defined(AV_WL64)
  147. # define AV_WN64(p, v) AV_WL64(p, v)
  148. # endif
  149. #endif /* !AV_HAVE_BIGENDIAN */
  150. /*
  151. * Define AV_[RW]N helper macros to simplify definitions not provided
  152. * by per-arch headers.
  153. */
  154. #if defined(__GNUC__) && !defined(__TI_COMPILER_VERSION__)
  155. union unaligned_64 { uint64_t l; } __attribute__((packed)) av_alias;
  156. union unaligned_32 { uint32_t l; } __attribute__((packed)) av_alias;
  157. union unaligned_16 { uint16_t l; } __attribute__((packed)) av_alias;
  158. # define AV_RN(s, p) (((const union unaligned_##s *) (p))->l)
  159. # define AV_WN(s, p, v) ((((union unaligned_##s *) (p))->l) = (v))
  160. #elif defined(__DECC)
  161. # define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
  162. # define AV_WN(s, p, v) (*((__unaligned uint##s##_t*)(p)) = (v))
  163. #elif AV_HAVE_FAST_UNALIGNED
  164. # define AV_RN(s, p) (((const av_alias##s*)(p))->u##s)
  165. # define AV_WN(s, p, v) (((av_alias##s*)(p))->u##s = (v))
  166. #else
  167. #ifndef AV_RB16
  168. # define AV_RB16(x) \
  169. ((((const uint8_t*)(x))[0] << 8) | \
  170. ((const uint8_t*)(x))[1])
  171. #endif
  172. #ifndef AV_WB16
  173. # define AV_WB16(p, darg) do { \
  174. unsigned d = (darg); \
  175. ((uint8_t*)(p))[1] = (d); \
  176. ((uint8_t*)(p))[0] = (d)>>8; \
  177. } while(0)
  178. #endif
  179. #ifndef AV_RL16
  180. # define AV_RL16(x) \
  181. ((((const uint8_t*)(x))[1] << 8) | \
  182. ((const uint8_t*)(x))[0])
  183. #endif
  184. #ifndef AV_WL16
  185. # define AV_WL16(p, darg) do { \
  186. unsigned d = (darg); \
  187. ((uint8_t*)(p))[0] = (d); \
  188. ((uint8_t*)(p))[1] = (d)>>8; \
  189. } while(0)
  190. #endif
  191. #ifndef AV_RB32
  192. # define AV_RB32(x) \
  193. (((uint32_t)((const uint8_t*)(x))[0] << 24) | \
  194. (((const uint8_t*)(x))[1] << 16) | \
  195. (((const uint8_t*)(x))[2] << 8) | \
  196. ((const uint8_t*)(x))[3])
  197. #endif
  198. #ifndef AV_WB32
  199. # define AV_WB32(p, darg) do { \
  200. unsigned d = (darg); \
  201. ((uint8_t*)(p))[3] = (d); \
  202. ((uint8_t*)(p))[2] = (d)>>8; \
  203. ((uint8_t*)(p))[1] = (d)>>16; \
  204. ((uint8_t*)(p))[0] = (d)>>24; \
  205. } while(0)
  206. #endif
  207. #ifndef AV_RL32
  208. # define AV_RL32(x) \
  209. (((uint32_t)((const uint8_t*)(x))[3] << 24) | \
  210. (((const uint8_t*)(x))[2] << 16) | \
  211. (((const uint8_t*)(x))[1] << 8) | \
  212. ((const uint8_t*)(x))[0])
  213. #endif
  214. #ifndef AV_WL32
  215. # define AV_WL32(p, darg) do { \
  216. unsigned d = (darg); \
  217. ((uint8_t*)(p))[0] = (d); \
  218. ((uint8_t*)(p))[1] = (d)>>8; \
  219. ((uint8_t*)(p))[2] = (d)>>16; \
  220. ((uint8_t*)(p))[3] = (d)>>24; \
  221. } while(0)
  222. #endif
  223. #ifndef AV_RB64
  224. # define AV_RB64(x) \
  225. (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
  226. ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
  227. ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
  228. ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
  229. ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
  230. ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
  231. ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
  232. (uint64_t)((const uint8_t*)(x))[7])
  233. #endif
  234. #ifndef AV_WB64
  235. # define AV_WB64(p, darg) do { \
  236. uint64_t d = (darg); \
  237. ((uint8_t*)(p))[7] = (d); \
  238. ((uint8_t*)(p))[6] = (d)>>8; \
  239. ((uint8_t*)(p))[5] = (d)>>16; \
  240. ((uint8_t*)(p))[4] = (d)>>24; \
  241. ((uint8_t*)(p))[3] = (d)>>32; \
  242. ((uint8_t*)(p))[2] = (d)>>40; \
  243. ((uint8_t*)(p))[1] = (d)>>48; \
  244. ((uint8_t*)(p))[0] = (d)>>56; \
  245. } while(0)
  246. #endif
  247. #ifndef AV_RL64
  248. # define AV_RL64(x) \
  249. (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
  250. ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
  251. ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
  252. ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
  253. ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
  254. ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
  255. ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
  256. (uint64_t)((const uint8_t*)(x))[0])
  257. #endif
  258. #ifndef AV_WL64
  259. # define AV_WL64(p, darg) do { \
  260. uint64_t d = (darg); \
  261. ((uint8_t*)(p))[0] = (d); \
  262. ((uint8_t*)(p))[1] = (d)>>8; \
  263. ((uint8_t*)(p))[2] = (d)>>16; \
  264. ((uint8_t*)(p))[3] = (d)>>24; \
  265. ((uint8_t*)(p))[4] = (d)>>32; \
  266. ((uint8_t*)(p))[5] = (d)>>40; \
  267. ((uint8_t*)(p))[6] = (d)>>48; \
  268. ((uint8_t*)(p))[7] = (d)>>56; \
  269. } while(0)
  270. #endif
  271. #if AV_HAVE_BIGENDIAN
  272. # define AV_RN(s, p) AV_RB##s(p)
  273. # define AV_WN(s, p, v) AV_WB##s(p, v)
  274. #else
  275. # define AV_RN(s, p) AV_RL##s(p)
  276. # define AV_WN(s, p, v) AV_WL##s(p, v)
  277. #endif
  278. #endif /* HAVE_FAST_UNALIGNED */
  279. #ifndef AV_RN16
  280. # define AV_RN16(p) AV_RN(16, p)
  281. #endif
  282. #ifndef AV_RN32
  283. # define AV_RN32(p) AV_RN(32, p)
  284. #endif
  285. #ifndef AV_RN64
  286. # define AV_RN64(p) AV_RN(64, p)
  287. #endif
  288. #ifndef AV_WN16
  289. # define AV_WN16(p, v) AV_WN(16, p, v)
  290. #endif
  291. #ifndef AV_WN32
  292. # define AV_WN32(p, v) AV_WN(32, p, v)
  293. #endif
  294. #ifndef AV_WN64
  295. # define AV_WN64(p, v) AV_WN(64, p, v)
  296. #endif
  297. #if AV_HAVE_BIGENDIAN
  298. # define AV_RB(s, p) AV_RN##s(p)
  299. # define AV_WB(s, p, v) AV_WN##s(p, v)
  300. # define AV_RL(s, p) av_bswap##s(AV_RN##s(p))
  301. # define AV_WL(s, p, v) AV_WN##s(p, av_bswap##s(v))
  302. #else
  303. # define AV_RB(s, p) av_bswap##s(AV_RN##s(p))
  304. # define AV_WB(s, p, v) AV_WN##s(p, av_bswap##s(v))
  305. # define AV_RL(s, p) AV_RN##s(p)
  306. # define AV_WL(s, p, v) AV_WN##s(p, v)
  307. #endif
  308. #define AV_RB8(x) (((const uint8_t*)(x))[0])
  309. #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
  310. #define AV_RL8(x) AV_RB8(x)
  311. #define AV_WL8(p, d) AV_WB8(p, d)
  312. #ifndef AV_RB16
  313. # define AV_RB16(p) AV_RB(16, p)
  314. #endif
  315. #ifndef AV_WB16
  316. # define AV_WB16(p, v) AV_WB(16, p, v)
  317. #endif
  318. #ifndef AV_RL16
  319. # define AV_RL16(p) AV_RL(16, p)
  320. #endif
  321. #ifndef AV_WL16
  322. # define AV_WL16(p, v) AV_WL(16, p, v)
  323. #endif
  324. #ifndef AV_RB32
  325. # define AV_RB32(p) AV_RB(32, p)
  326. #endif
  327. #ifndef AV_WB32
  328. # define AV_WB32(p, v) AV_WB(32, p, v)
  329. #endif
  330. #ifndef AV_RL32
  331. # define AV_RL32(p) AV_RL(32, p)
  332. #endif
  333. #ifndef AV_WL32
  334. # define AV_WL32(p, v) AV_WL(32, p, v)
  335. #endif
  336. #ifndef AV_RB64
  337. # define AV_RB64(p) AV_RB(64, p)
  338. #endif
  339. #ifndef AV_WB64
  340. # define AV_WB64(p, v) AV_WB(64, p, v)
  341. #endif
  342. #ifndef AV_RL64
  343. # define AV_RL64(p) AV_RL(64, p)
  344. #endif
  345. #ifndef AV_WL64
  346. # define AV_WL64(p, v) AV_WL(64, p, v)
  347. #endif
  348. #ifndef AV_RB24
  349. # define AV_RB24(x) \
  350. ((((const uint8_t*)(x))[0] << 16) | \
  351. (((const uint8_t*)(x))[1] << 8) | \
  352. ((const uint8_t*)(x))[2])
  353. #endif
  354. #ifndef AV_WB24
  355. # define AV_WB24(p, d) do { \
  356. ((uint8_t*)(p))[2] = (d); \
  357. ((uint8_t*)(p))[1] = (d)>>8; \
  358. ((uint8_t*)(p))[0] = (d)>>16; \
  359. } while(0)
  360. #endif
  361. #ifndef AV_RL24
  362. # define AV_RL24(x) \
  363. ((((const uint8_t*)(x))[2] << 16) | \
  364. (((const uint8_t*)(x))[1] << 8) | \
  365. ((const uint8_t*)(x))[0])
  366. #endif
  367. #ifndef AV_WL24
  368. # define AV_WL24(p, d) do { \
  369. ((uint8_t*)(p))[0] = (d); \
  370. ((uint8_t*)(p))[1] = (d)>>8; \
  371. ((uint8_t*)(p))[2] = (d)>>16; \
  372. } while(0)
  373. #endif
  374. /*
  375. * The AV_[RW]NA macros access naturally aligned data
  376. * in a type-safe way.
  377. */
  378. #define AV_RNA(s, p) (((const av_alias##s*)(p))->u##s)
  379. #define AV_WNA(s, p, v) (((av_alias##s*)(p))->u##s = (v))
  380. #ifndef AV_RN16A
  381. # define AV_RN16A(p) AV_RNA(16, p)
  382. #endif
  383. #ifndef AV_RN32A
  384. # define AV_RN32A(p) AV_RNA(32, p)
  385. #endif
  386. #ifndef AV_RN64A
  387. # define AV_RN64A(p) AV_RNA(64, p)
  388. #endif
  389. #ifndef AV_WN16A
  390. # define AV_WN16A(p, v) AV_WNA(16, p, v)
  391. #endif
  392. #ifndef AV_WN32A
  393. # define AV_WN32A(p, v) AV_WNA(32, p, v)
  394. #endif
  395. #ifndef AV_WN64A
  396. # define AV_WN64A(p, v) AV_WNA(64, p, v)
  397. #endif
  398. /* Parameters for AV_COPY*, AV_SWAP*, AV_ZERO* must be
  399. * naturally aligned. They may be implemented using MMX,
  400. * so emms_c() must be called before using any float code
  401. * afterwards.
  402. */
  403. #define AV_COPY(n, d, s) \
  404. (((av_alias##n*)(d))->u##n = ((const av_alias##n*)(s))->u##n)
  405. #ifndef AV_COPY16
  406. # define AV_COPY16(d, s) AV_COPY(16, d, s)
  407. #endif
  408. #ifndef AV_COPY32
  409. # define AV_COPY32(d, s) AV_COPY(32, d, s)
  410. #endif
  411. #ifndef AV_COPY64
  412. # define AV_COPY64(d, s) AV_COPY(64, d, s)
  413. #endif
  414. #ifndef AV_COPY128
  415. # define AV_COPY128(d, s) \
  416. do { \
  417. AV_COPY64(d, s); \
  418. AV_COPY64((char*)(d)+8, (char*)(s)+8); \
  419. } while(0)
  420. #endif
  421. #define AV_SWAP(n, a, b) FFSWAP(av_alias##n, *(av_alias##n*)(a), *(av_alias##n*)(b))
  422. #ifndef AV_SWAP64
  423. # define AV_SWAP64(a, b) AV_SWAP(64, a, b)
  424. #endif
  425. #define AV_ZERO(n, d) (((av_alias##n*)(d))->u##n = 0)
  426. #ifndef AV_ZERO16
  427. # define AV_ZERO16(d) AV_ZERO(16, d)
  428. #endif
  429. #ifndef AV_ZERO32
  430. # define AV_ZERO32(d) AV_ZERO(32, d)
  431. #endif
  432. #ifndef AV_ZERO64
  433. # define AV_ZERO64(d) AV_ZERO(64, d)
  434. #endif
  435. #ifndef AV_ZERO128
  436. # define AV_ZERO128(d) \
  437. do { \
  438. AV_ZERO64(d); \
  439. AV_ZERO64((char*)(d)+8); \
  440. } while(0)
  441. #endif
  442. #endif /* AVUTIL_INTREADWRITE_H */