You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

561 lines
15KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_X64)) && AV_HAVE_FAST_UNALIGNED
  164. # define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
  165. # define AV_WN(s, p, v) (*((__unaligned uint##s##_t*)(p)) = (v))
  166. #elif AV_HAVE_FAST_UNALIGNED
  167. # define AV_RN(s, p) (((const av_alias##s*)(p))->u##s)
  168. # define AV_WN(s, p, v) (((av_alias##s*)(p))->u##s = (v))
  169. #else
  170. #ifndef AV_RB16
  171. # define AV_RB16(x) \
  172. ((((const uint8_t*)(x))[0] << 8) | \
  173. ((const uint8_t*)(x))[1])
  174. #endif
  175. #ifndef AV_WB16
  176. # define AV_WB16(p, val) do { \
  177. uint16_t d = val; \
  178. ((uint8_t*)(p))[1] = (d); \
  179. ((uint8_t*)(p))[0] = (d)>>8; \
  180. } while(0)
  181. #endif
  182. #ifndef AV_RL16
  183. # define AV_RL16(x) \
  184. ((((const uint8_t*)(x))[1] << 8) | \
  185. ((const uint8_t*)(x))[0])
  186. #endif
  187. #ifndef AV_WL16
  188. # define AV_WL16(p, val) do { \
  189. uint16_t d = val; \
  190. ((uint8_t*)(p))[0] = (d); \
  191. ((uint8_t*)(p))[1] = (d)>>8; \
  192. } while(0)
  193. #endif
  194. #ifndef AV_RB32
  195. # define AV_RB32(x) \
  196. (((uint32_t)((const uint8_t*)(x))[0] << 24) | \
  197. (((const uint8_t*)(x))[1] << 16) | \
  198. (((const uint8_t*)(x))[2] << 8) | \
  199. ((const uint8_t*)(x))[3])
  200. #endif
  201. #ifndef AV_WB32
  202. # define AV_WB32(p, val) do { \
  203. uint32_t d = val; \
  204. ((uint8_t*)(p))[3] = (d); \
  205. ((uint8_t*)(p))[2] = (d)>>8; \
  206. ((uint8_t*)(p))[1] = (d)>>16; \
  207. ((uint8_t*)(p))[0] = (d)>>24; \
  208. } while(0)
  209. #endif
  210. #ifndef AV_RL32
  211. # define AV_RL32(x) \
  212. (((uint32_t)((const uint8_t*)(x))[3] << 24) | \
  213. (((const uint8_t*)(x))[2] << 16) | \
  214. (((const uint8_t*)(x))[1] << 8) | \
  215. ((const uint8_t*)(x))[0])
  216. #endif
  217. #ifndef AV_WL32
  218. # define AV_WL32(p, val) do { \
  219. uint32_t d = val; \
  220. ((uint8_t*)(p))[0] = (d); \
  221. ((uint8_t*)(p))[1] = (d)>>8; \
  222. ((uint8_t*)(p))[2] = (d)>>16; \
  223. ((uint8_t*)(p))[3] = (d)>>24; \
  224. } while(0)
  225. #endif
  226. #ifndef AV_RB64
  227. # define AV_RB64(x) \
  228. (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
  229. ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
  230. ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
  231. ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
  232. ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
  233. ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
  234. ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
  235. (uint64_t)((const uint8_t*)(x))[7])
  236. #endif
  237. #ifndef AV_WB64
  238. # define AV_WB64(p, val) do { \
  239. uint64_t d = val; \
  240. ((uint8_t*)(p))[7] = (d); \
  241. ((uint8_t*)(p))[6] = (d)>>8; \
  242. ((uint8_t*)(p))[5] = (d)>>16; \
  243. ((uint8_t*)(p))[4] = (d)>>24; \
  244. ((uint8_t*)(p))[3] = (d)>>32; \
  245. ((uint8_t*)(p))[2] = (d)>>40; \
  246. ((uint8_t*)(p))[1] = (d)>>48; \
  247. ((uint8_t*)(p))[0] = (d)>>56; \
  248. } while(0)
  249. #endif
  250. #ifndef AV_RL64
  251. # define AV_RL64(x) \
  252. (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
  253. ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
  254. ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
  255. ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
  256. ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
  257. ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
  258. ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
  259. (uint64_t)((const uint8_t*)(x))[0])
  260. #endif
  261. #ifndef AV_WL64
  262. # define AV_WL64(p, val) do { \
  263. uint64_t d = val; \
  264. ((uint8_t*)(p))[0] = (d); \
  265. ((uint8_t*)(p))[1] = (d)>>8; \
  266. ((uint8_t*)(p))[2] = (d)>>16; \
  267. ((uint8_t*)(p))[3] = (d)>>24; \
  268. ((uint8_t*)(p))[4] = (d)>>32; \
  269. ((uint8_t*)(p))[5] = (d)>>40; \
  270. ((uint8_t*)(p))[6] = (d)>>48; \
  271. ((uint8_t*)(p))[7] = (d)>>56; \
  272. } while(0)
  273. #endif
  274. #if AV_HAVE_BIGENDIAN
  275. # define AV_RN(s, p) AV_RB##s(p)
  276. # define AV_WN(s, p, v) AV_WB##s(p, v)
  277. #else
  278. # define AV_RN(s, p) AV_RL##s(p)
  279. # define AV_WN(s, p, v) AV_WL##s(p, v)
  280. #endif
  281. #endif /* HAVE_FAST_UNALIGNED */
  282. #ifndef AV_RN16
  283. # define AV_RN16(p) AV_RN(16, p)
  284. #endif
  285. #ifndef AV_RN32
  286. # define AV_RN32(p) AV_RN(32, p)
  287. #endif
  288. #ifndef AV_RN64
  289. # define AV_RN64(p) AV_RN(64, p)
  290. #endif
  291. #ifndef AV_WN16
  292. # define AV_WN16(p, v) AV_WN(16, p, v)
  293. #endif
  294. #ifndef AV_WN32
  295. # define AV_WN32(p, v) AV_WN(32, p, v)
  296. #endif
  297. #ifndef AV_WN64
  298. # define AV_WN64(p, v) AV_WN(64, p, v)
  299. #endif
  300. #if AV_HAVE_BIGENDIAN
  301. # define AV_RB(s, p) AV_RN##s(p)
  302. # define AV_WB(s, p, v) AV_WN##s(p, v)
  303. # define AV_RL(s, p) av_bswap##s(AV_RN##s(p))
  304. # define AV_WL(s, p, v) AV_WN##s(p, av_bswap##s(v))
  305. #else
  306. # define AV_RB(s, p) av_bswap##s(AV_RN##s(p))
  307. # define AV_WB(s, p, v) AV_WN##s(p, av_bswap##s(v))
  308. # define AV_RL(s, p) AV_RN##s(p)
  309. # define AV_WL(s, p, v) AV_WN##s(p, v)
  310. #endif
  311. #define AV_RB8(x) (((const uint8_t*)(x))[0])
  312. #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
  313. #define AV_RL8(x) AV_RB8(x)
  314. #define AV_WL8(p, d) AV_WB8(p, d)
  315. #ifndef AV_RB16
  316. # define AV_RB16(p) AV_RB(16, p)
  317. #endif
  318. #ifndef AV_WB16
  319. # define AV_WB16(p, v) AV_WB(16, p, v)
  320. #endif
  321. #ifndef AV_RL16
  322. # define AV_RL16(p) AV_RL(16, p)
  323. #endif
  324. #ifndef AV_WL16
  325. # define AV_WL16(p, v) AV_WL(16, p, v)
  326. #endif
  327. #ifndef AV_RB32
  328. # define AV_RB32(p) AV_RB(32, p)
  329. #endif
  330. #ifndef AV_WB32
  331. # define AV_WB32(p, v) AV_WB(32, p, v)
  332. #endif
  333. #ifndef AV_RL32
  334. # define AV_RL32(p) AV_RL(32, p)
  335. #endif
  336. #ifndef AV_WL32
  337. # define AV_WL32(p, v) AV_WL(32, p, v)
  338. #endif
  339. #ifndef AV_RB64
  340. # define AV_RB64(p) AV_RB(64, p)
  341. #endif
  342. #ifndef AV_WB64
  343. # define AV_WB64(p, v) AV_WB(64, p, v)
  344. #endif
  345. #ifndef AV_RL64
  346. # define AV_RL64(p) AV_RL(64, p)
  347. #endif
  348. #ifndef AV_WL64
  349. # define AV_WL64(p, v) AV_WL(64, p, v)
  350. #endif
  351. #ifndef AV_RB24
  352. # define AV_RB24(x) \
  353. ((((const uint8_t*)(x))[0] << 16) | \
  354. (((const uint8_t*)(x))[1] << 8) | \
  355. ((const uint8_t*)(x))[2])
  356. #endif
  357. #ifndef AV_WB24
  358. # define AV_WB24(p, d) do { \
  359. ((uint8_t*)(p))[2] = (d); \
  360. ((uint8_t*)(p))[1] = (d)>>8; \
  361. ((uint8_t*)(p))[0] = (d)>>16; \
  362. } while(0)
  363. #endif
  364. #ifndef AV_RL24
  365. # define AV_RL24(x) \
  366. ((((const uint8_t*)(x))[2] << 16) | \
  367. (((const uint8_t*)(x))[1] << 8) | \
  368. ((const uint8_t*)(x))[0])
  369. #endif
  370. #ifndef AV_WL24
  371. # define AV_WL24(p, d) do { \
  372. ((uint8_t*)(p))[0] = (d); \
  373. ((uint8_t*)(p))[1] = (d)>>8; \
  374. ((uint8_t*)(p))[2] = (d)>>16; \
  375. } while(0)
  376. #endif
  377. /*
  378. * The AV_[RW]NA macros access naturally aligned data
  379. * in a type-safe way.
  380. */
  381. #define AV_RNA(s, p) (((const av_alias##s*)(p))->u##s)
  382. #define AV_WNA(s, p, v) (((av_alias##s*)(p))->u##s = (v))
  383. #ifndef AV_RN16A
  384. # define AV_RN16A(p) AV_RNA(16, p)
  385. #endif
  386. #ifndef AV_RN32A
  387. # define AV_RN32A(p) AV_RNA(32, p)
  388. #endif
  389. #ifndef AV_RN64A
  390. # define AV_RN64A(p) AV_RNA(64, p)
  391. #endif
  392. #ifndef AV_WN16A
  393. # define AV_WN16A(p, v) AV_WNA(16, p, v)
  394. #endif
  395. #ifndef AV_WN32A
  396. # define AV_WN32A(p, v) AV_WNA(32, p, v)
  397. #endif
  398. #ifndef AV_WN64A
  399. # define AV_WN64A(p, v) AV_WNA(64, p, v)
  400. #endif
  401. /*
  402. * The AV_COPYxxU macros are suitable for copying data to/from unaligned
  403. * memory locations.
  404. */
  405. #define AV_COPYU(n, d, s) AV_WN##n(d, AV_RN##n(s));
  406. #ifndef AV_COPY16U
  407. # define AV_COPY16U(d, s) AV_COPYU(16, d, s)
  408. #endif
  409. #ifndef AV_COPY32U
  410. # define AV_COPY32U(d, s) AV_COPYU(32, d, s)
  411. #endif
  412. #ifndef AV_COPY64U
  413. # define AV_COPY64U(d, s) AV_COPYU(64, d, s)
  414. #endif
  415. #ifndef AV_COPY128U
  416. # define AV_COPY128U(d, s) \
  417. do { \
  418. AV_COPY64U(d, s); \
  419. AV_COPY64U((char *)(d) + 8, (const char *)(s) + 8); \
  420. } while(0)
  421. #endif
  422. /* Parameters for AV_COPY*, AV_SWAP*, AV_ZERO* must be
  423. * naturally aligned. They may be implemented using MMX,
  424. * so emms_c() must be called before using any float code
  425. * afterwards.
  426. */
  427. #define AV_COPY(n, d, s) \
  428. (((av_alias##n*)(d))->u##n = ((const av_alias##n*)(s))->u##n)
  429. #ifndef AV_COPY16
  430. # define AV_COPY16(d, s) AV_COPY(16, d, s)
  431. #endif
  432. #ifndef AV_COPY32
  433. # define AV_COPY32(d, s) AV_COPY(32, d, s)
  434. #endif
  435. #ifndef AV_COPY64
  436. # define AV_COPY64(d, s) AV_COPY(64, d, s)
  437. #endif
  438. #ifndef AV_COPY128
  439. # define AV_COPY128(d, s) \
  440. do { \
  441. AV_COPY64(d, s); \
  442. AV_COPY64((char*)(d)+8, (char*)(s)+8); \
  443. } while(0)
  444. #endif
  445. #define AV_SWAP(n, a, b) FFSWAP(av_alias##n, *(av_alias##n*)(a), *(av_alias##n*)(b))
  446. #ifndef AV_SWAP64
  447. # define AV_SWAP64(a, b) AV_SWAP(64, a, b)
  448. #endif
  449. #define AV_ZERO(n, d) (((av_alias##n*)(d))->u##n = 0)
  450. #ifndef AV_ZERO16
  451. # define AV_ZERO16(d) AV_ZERO(16, d)
  452. #endif
  453. #ifndef AV_ZERO32
  454. # define AV_ZERO32(d) AV_ZERO(32, d)
  455. #endif
  456. #ifndef AV_ZERO64
  457. # define AV_ZERO64(d) AV_ZERO(64, d)
  458. #endif
  459. #ifndef AV_ZERO128
  460. # define AV_ZERO128(d) \
  461. do { \
  462. AV_ZERO64(d); \
  463. AV_ZERO64((char*)(d)+8); \
  464. } while(0)
  465. #endif
  466. #endif /* AVUTIL_INTREADWRITE_H */