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.

470 lines
13KB

  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 "config.h"
  22. #include "bswap.h"
  23. #include "common.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. #if ARCH_ARM
  49. # include "arm/intreadwrite.h"
  50. #elif ARCH_AVR32
  51. # include "avr32/intreadwrite.h"
  52. #elif ARCH_MIPS
  53. # include "mips/intreadwrite.h"
  54. #elif ARCH_PPC
  55. # include "ppc/intreadwrite.h"
  56. #elif ARCH_X86
  57. # include "x86/intreadwrite.h"
  58. #endif
  59. /*
  60. * Map AV_RNXX <-> AV_R[BL]XX for all variants provided by per-arch headers.
  61. */
  62. #if HAVE_BIGENDIAN
  63. # if defined(AV_RN16) && !defined(AV_RB16)
  64. # define AV_RB16(p) AV_RN16(p)
  65. # elif !defined(AV_RN16) && defined(AV_RB16)
  66. # define AV_RN16(p) AV_RB16(p)
  67. # endif
  68. # if defined(AV_WN16) && !defined(AV_WB16)
  69. # define AV_WB16(p, v) AV_WN16(p, v)
  70. # elif !defined(AV_WN16) && defined(AV_WB16)
  71. # define AV_WN16(p, v) AV_WB16(p, v)
  72. # endif
  73. # if defined(AV_RN24) && !defined(AV_RB24)
  74. # define AV_RB24(p) AV_RN24(p)
  75. # elif !defined(AV_RN24) && defined(AV_RB24)
  76. # define AV_RN24(p) AV_RB24(p)
  77. # endif
  78. # if defined(AV_WN24) && !defined(AV_WB24)
  79. # define AV_WB24(p, v) AV_WN24(p, v)
  80. # elif !defined(AV_WN24) && defined(AV_WB24)
  81. # define AV_WN24(p, v) AV_WB24(p, v)
  82. # endif
  83. # if defined(AV_RN32) && !defined(AV_RB32)
  84. # define AV_RB32(p) AV_RN32(p)
  85. # elif !defined(AV_RN32) && defined(AV_RB32)
  86. # define AV_RN32(p) AV_RB32(p)
  87. # endif
  88. # if defined(AV_WN32) && !defined(AV_WB32)
  89. # define AV_WB32(p, v) AV_WN32(p, v)
  90. # elif !defined(AV_WN32) && defined(AV_WB32)
  91. # define AV_WN32(p, v) AV_WB32(p, v)
  92. # endif
  93. # if defined(AV_RN64) && !defined(AV_RB64)
  94. # define AV_RB64(p) AV_RN64(p)
  95. # elif !defined(AV_RN64) && defined(AV_RB64)
  96. # define AV_RN64(p) AV_RB64(p)
  97. # endif
  98. # if defined(AV_WN64) && !defined(AV_WB64)
  99. # define AV_WB64(p, v) AV_WN64(p, v)
  100. # elif !defined(AV_WN64) && defined(AV_WB64)
  101. # define AV_WN64(p, v) AV_WB64(p, v)
  102. # endif
  103. #else /* HAVE_BIGENDIAN */
  104. # if defined(AV_RN16) && !defined(AV_RL16)
  105. # define AV_RL16(p) AV_RN16(p)
  106. # elif !defined(AV_RN16) && defined(AV_RL16)
  107. # define AV_RN16(p) AV_RL16(p)
  108. # endif
  109. # if defined(AV_WN16) && !defined(AV_WL16)
  110. # define AV_WL16(p, v) AV_WN16(p, v)
  111. # elif !defined(AV_WN16) && defined(AV_WL16)
  112. # define AV_WN16(p, v) AV_WL16(p, v)
  113. # endif
  114. # if defined(AV_RN24) && !defined(AV_RL24)
  115. # define AV_RL24(p) AV_RN24(p)
  116. # elif !defined(AV_RN24) && defined(AV_RL24)
  117. # define AV_RN24(p) AV_RL24(p)
  118. # endif
  119. # if defined(AV_WN24) && !defined(AV_WL24)
  120. # define AV_WL24(p, v) AV_WN24(p, v)
  121. # elif !defined(AV_WN24) && defined(AV_WL24)
  122. # define AV_WN24(p, v) AV_WL24(p, v)
  123. # endif
  124. # if defined(AV_RN32) && !defined(AV_RL32)
  125. # define AV_RL32(p) AV_RN32(p)
  126. # elif !defined(AV_RN32) && defined(AV_RL32)
  127. # define AV_RN32(p) AV_RL32(p)
  128. # endif
  129. # if defined(AV_WN32) && !defined(AV_WL32)
  130. # define AV_WL32(p, v) AV_WN32(p, v)
  131. # elif !defined(AV_WN32) && defined(AV_WL32)
  132. # define AV_WN32(p, v) AV_WL32(p, v)
  133. # endif
  134. # if defined(AV_RN64) && !defined(AV_RL64)
  135. # define AV_RL64(p) AV_RN64(p)
  136. # elif !defined(AV_RN64) && defined(AV_RL64)
  137. # define AV_RN64(p) AV_RL64(p)
  138. # endif
  139. # if defined(AV_WN64) && !defined(AV_WL64)
  140. # define AV_WL64(p, v) AV_WN64(p, v)
  141. # elif !defined(AV_WN64) && defined(AV_WL64)
  142. # define AV_WN64(p, v) AV_WL64(p, v)
  143. # endif
  144. #endif /* !HAVE_BIGENDIAN */
  145. /*
  146. * Define AV_[RW]N helper macros to simplify definitions not provided
  147. * by per-arch headers.
  148. */
  149. #if HAVE_ATTRIBUTE_PACKED
  150. struct unaligned_64 { uint64_t l; } __attribute__((packed));
  151. struct unaligned_32 { uint32_t l; } __attribute__((packed));
  152. struct unaligned_16 { uint16_t l; } __attribute__((packed));
  153. # define AV_RN(s, p) (((const struct unaligned_##s *) (p))->l)
  154. # define AV_WN(s, p, v) ((((struct unaligned_##s *) (p))->l) = (v))
  155. #elif defined(__DECC)
  156. # define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
  157. # define AV_WN(s, p, v) (*((__unaligned uint##s##_t*)(p)) = (v))
  158. #elif HAVE_FAST_UNALIGNED
  159. # define AV_RN(s, p) (*((const uint##s##_t*)(p)))
  160. # define AV_WN(s, p, v) (*((uint##s##_t*)(p)) = (v))
  161. #else
  162. #ifndef AV_RB16
  163. # define AV_RB16(x) \
  164. ((((const uint8_t*)(x))[0] << 8) | \
  165. ((const uint8_t*)(x))[1])
  166. #endif
  167. #ifndef AV_WB16
  168. # define AV_WB16(p, d) do { \
  169. ((uint8_t*)(p))[1] = (d); \
  170. ((uint8_t*)(p))[0] = (d)>>8; \
  171. } while(0)
  172. #endif
  173. #ifndef AV_RL16
  174. # define AV_RL16(x) \
  175. ((((const uint8_t*)(x))[1] << 8) | \
  176. ((const uint8_t*)(x))[0])
  177. #endif
  178. #ifndef AV_WL16
  179. # define AV_WL16(p, d) do { \
  180. ((uint8_t*)(p))[0] = (d); \
  181. ((uint8_t*)(p))[1] = (d)>>8; \
  182. } while(0)
  183. #endif
  184. #ifndef AV_RB32
  185. # define AV_RB32(x) \
  186. ((((const uint8_t*)(x))[0] << 24) | \
  187. (((const uint8_t*)(x))[1] << 16) | \
  188. (((const uint8_t*)(x))[2] << 8) | \
  189. ((const uint8_t*)(x))[3])
  190. #endif
  191. #ifndef AV_WB32
  192. # define AV_WB32(p, d) do { \
  193. ((uint8_t*)(p))[3] = (d); \
  194. ((uint8_t*)(p))[2] = (d)>>8; \
  195. ((uint8_t*)(p))[1] = (d)>>16; \
  196. ((uint8_t*)(p))[0] = (d)>>24; \
  197. } while(0)
  198. #endif
  199. #ifndef AV_RL32
  200. # define AV_RL32(x) \
  201. ((((const uint8_t*)(x))[3] << 24) | \
  202. (((const uint8_t*)(x))[2] << 16) | \
  203. (((const uint8_t*)(x))[1] << 8) | \
  204. ((const uint8_t*)(x))[0])
  205. #endif
  206. #ifndef AV_WL32
  207. # define AV_WL32(p, d) do { \
  208. ((uint8_t*)(p))[0] = (d); \
  209. ((uint8_t*)(p))[1] = (d)>>8; \
  210. ((uint8_t*)(p))[2] = (d)>>16; \
  211. ((uint8_t*)(p))[3] = (d)>>24; \
  212. } while(0)
  213. #endif
  214. #ifndef AV_RB64
  215. # define AV_RB64(x) \
  216. (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
  217. ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
  218. ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
  219. ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
  220. ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
  221. ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
  222. ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
  223. (uint64_t)((const uint8_t*)(x))[7])
  224. #endif
  225. #ifndef AV_WB64
  226. # define AV_WB64(p, d) do { \
  227. ((uint8_t*)(p))[7] = (d); \
  228. ((uint8_t*)(p))[6] = (d)>>8; \
  229. ((uint8_t*)(p))[5] = (d)>>16; \
  230. ((uint8_t*)(p))[4] = (d)>>24; \
  231. ((uint8_t*)(p))[3] = (d)>>32; \
  232. ((uint8_t*)(p))[2] = (d)>>40; \
  233. ((uint8_t*)(p))[1] = (d)>>48; \
  234. ((uint8_t*)(p))[0] = (d)>>56; \
  235. } while(0)
  236. #endif
  237. #ifndef AV_RL64
  238. # define AV_RL64(x) \
  239. (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
  240. ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
  241. ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
  242. ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
  243. ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
  244. ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
  245. ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
  246. (uint64_t)((const uint8_t*)(x))[0])
  247. #endif
  248. #ifndef AV_WL64
  249. # define AV_WL64(p, d) do { \
  250. ((uint8_t*)(p))[0] = (d); \
  251. ((uint8_t*)(p))[1] = (d)>>8; \
  252. ((uint8_t*)(p))[2] = (d)>>16; \
  253. ((uint8_t*)(p))[3] = (d)>>24; \
  254. ((uint8_t*)(p))[4] = (d)>>32; \
  255. ((uint8_t*)(p))[5] = (d)>>40; \
  256. ((uint8_t*)(p))[6] = (d)>>48; \
  257. ((uint8_t*)(p))[7] = (d)>>56; \
  258. } while(0)
  259. #endif
  260. #if HAVE_BIGENDIAN
  261. # define AV_RN(s, p) AV_RB##s(p)
  262. # define AV_WN(s, p, v) AV_WB##s(p, v)
  263. #else
  264. # define AV_RN(s, p) AV_RL##s(p)
  265. # define AV_WN(s, p, v) AV_WL##s(p, v)
  266. #endif
  267. #endif /* HAVE_FAST_UNALIGNED */
  268. #ifndef AV_RN16
  269. # define AV_RN16(p) AV_RN(16, p)
  270. #endif
  271. #ifndef AV_RN32
  272. # define AV_RN32(p) AV_RN(32, p)
  273. #endif
  274. #ifndef AV_RN64
  275. # define AV_RN64(p) AV_RN(64, p)
  276. #endif
  277. #ifndef AV_WN16
  278. # define AV_WN16(p, v) AV_WN(16, p, v)
  279. #endif
  280. #ifndef AV_WN32
  281. # define AV_WN32(p, v) AV_WN(32, p, v)
  282. #endif
  283. #ifndef AV_WN64
  284. # define AV_WN64(p, v) AV_WN(64, p, v)
  285. #endif
  286. #if HAVE_BIGENDIAN
  287. # define AV_RB(s, p) AV_RN##s(p)
  288. # define AV_WB(s, p, v) AV_WN##s(p, v)
  289. # define AV_RL(s, p) bswap_##s(AV_RN##s(p))
  290. # define AV_WL(s, p, v) AV_WN##s(p, bswap_##s(v))
  291. #else
  292. # define AV_RB(s, p) bswap_##s(AV_RN##s(p))
  293. # define AV_WB(s, p, v) AV_WN##s(p, bswap_##s(v))
  294. # define AV_RL(s, p) AV_RN##s(p)
  295. # define AV_WL(s, p, v) AV_WN##s(p, v)
  296. #endif
  297. #define AV_RB8(x) (((const uint8_t*)(x))[0])
  298. #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
  299. #define AV_RL8(x) AV_RB8(x)
  300. #define AV_WL8(p, d) AV_WB8(p, d)
  301. #ifndef AV_RB16
  302. # define AV_RB16(p) AV_RB(16, p)
  303. #endif
  304. #ifndef AV_WB16
  305. # define AV_WB16(p, v) AV_WB(16, p, v)
  306. #endif
  307. #ifndef AV_RL16
  308. # define AV_RL16(p) AV_RL(16, p)
  309. #endif
  310. #ifndef AV_WL16
  311. # define AV_WL16(p, v) AV_WL(16, p, v)
  312. #endif
  313. #ifndef AV_RB32
  314. # define AV_RB32(p) AV_RB(32, p)
  315. #endif
  316. #ifndef AV_WB32
  317. # define AV_WB32(p, v) AV_WB(32, p, v)
  318. #endif
  319. #ifndef AV_RL32
  320. # define AV_RL32(p) AV_RL(32, p)
  321. #endif
  322. #ifndef AV_WL32
  323. # define AV_WL32(p, v) AV_WL(32, p, v)
  324. #endif
  325. #ifndef AV_RB64
  326. # define AV_RB64(p) AV_RB(64, p)
  327. #endif
  328. #ifndef AV_WB64
  329. # define AV_WB64(p, v) AV_WB(64, p, v)
  330. #endif
  331. #ifndef AV_RL64
  332. # define AV_RL64(p) AV_RL(64, p)
  333. #endif
  334. #ifndef AV_WL64
  335. # define AV_WL64(p, v) AV_WL(64, p, v)
  336. #endif
  337. #ifndef AV_RB24
  338. # define AV_RB24(x) \
  339. ((((const uint8_t*)(x))[0] << 16) | \
  340. (((const uint8_t*)(x))[1] << 8) | \
  341. ((const uint8_t*)(x))[2])
  342. #endif
  343. #ifndef AV_WB24
  344. # define AV_WB24(p, d) do { \
  345. ((uint8_t*)(p))[2] = (d); \
  346. ((uint8_t*)(p))[1] = (d)>>8; \
  347. ((uint8_t*)(p))[0] = (d)>>16; \
  348. } while(0)
  349. #endif
  350. #ifndef AV_RL24
  351. # define AV_RL24(x) \
  352. ((((const uint8_t*)(x))[2] << 16) | \
  353. (((const uint8_t*)(x))[1] << 8) | \
  354. ((const uint8_t*)(x))[0])
  355. #endif
  356. #ifndef AV_WL24
  357. # define AV_WL24(p, d) do { \
  358. ((uint8_t*)(p))[0] = (d); \
  359. ((uint8_t*)(p))[1] = (d)>>8; \
  360. ((uint8_t*)(p))[2] = (d)>>16; \
  361. } while(0)
  362. #endif
  363. /* Parameters for AV_COPY*, AV_SWAP*, AV_ZERO* must be
  364. * naturally aligned. They may be implemented using MMX,
  365. * so emms_c() must be called before using any float code
  366. * afterwards.
  367. */
  368. #define AV_COPY(n, d, s) (*(uint##n##_t*)(d) = *(const uint##n##_t*)(s))
  369. #ifndef AV_COPY32
  370. # define AV_COPY32(d, s) AV_COPY(32, d, s)
  371. #endif
  372. #ifndef AV_COPY64
  373. # define AV_COPY64(d, s) AV_COPY(64, d, s)
  374. #endif
  375. #ifndef AV_COPY128
  376. # define AV_COPY128(d, s) \
  377. do { \
  378. AV_COPY64(d, s); \
  379. AV_COPY64((char*)(d)+8, (char*)(s)+8); \
  380. } while(0)
  381. #endif
  382. #define AV_SWAP(n, a, b) FFSWAP(uint##n##_t, *(uint##n##_t*)(a), *(uint##n##_t*)(b))
  383. #ifndef AV_SWAP64
  384. # define AV_SWAP64(a, b) AV_SWAP(64, a, b)
  385. #endif
  386. #define AV_ZERO(n, d) (*(uint##n##_t*)(d) = 0)
  387. #ifndef AV_ZERO64
  388. # define AV_ZERO64(d) AV_ZERO(64, d)
  389. #endif
  390. #ifndef AV_ZERO128
  391. # define AV_ZERO128(d) \
  392. do { \
  393. AV_ZERO64(d); \
  394. AV_ZERO64((char*)(d)+8); \
  395. } while(0)
  396. #endif
  397. #endif /* AVUTIL_INTREADWRITE_H */