intreadwrite.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. /*
  24. * Arch-specific headers can provide any combination of
  25. * AV_[RW][BLN](16|24|32|64) macros. Preprocessor symbols must be
  26. * defined, even if these are implemented as inline functions.
  27. */
  28. #if ARCH_ARM
  29. # include "arm/intreadwrite.h"
  30. #elif ARCH_AVR32
  31. # include "avr32/intreadwrite.h"
  32. #elif ARCH_MIPS
  33. # include "mips/intreadwrite.h"
  34. #elif ARCH_PPC
  35. # include "ppc/intreadwrite.h"
  36. #endif
  37. /*
  38. * Map AV_RNXX <-> AV_R[BL]XX for all variants provided by per-arch headers.
  39. */
  40. #if HAVE_BIGENDIAN
  41. # if defined(AV_RN16) && !defined(AV_RB16)
  42. # define AV_RB16(p) AV_RN16(p)
  43. # elif !defined(AV_RN16) && defined(AV_RB16)
  44. # define AV_RN16(p) AV_RB16(p)
  45. # endif
  46. # if defined(AV_WN16) && !defined(AV_WB16)
  47. # define AV_WB16(p, v) AV_WN16(p, v)
  48. # elif !defined(AV_WN16) && defined(AV_WB16)
  49. # define AV_WN16(p, v) AV_WB16(p, v)
  50. # endif
  51. # if defined(AV_RN24) && !defined(AV_RB24)
  52. # define AV_RB24(p) AV_RN24(p)
  53. # elif !defined(AV_RN24) && defined(AV_RB24)
  54. # define AV_RN24(p) AV_RB24(p)
  55. # endif
  56. # if defined(AV_WN24) && !defined(AV_WB24)
  57. # define AV_WB24(p, v) AV_WN24(p, v)
  58. # elif !defined(AV_WN24) && defined(AV_WB24)
  59. # define AV_WN24(p, v) AV_WB24(p, v)
  60. # endif
  61. # if defined(AV_RN32) && !defined(AV_RB32)
  62. # define AV_RB32(p) AV_RN32(p)
  63. # elif !defined(AV_RN32) && defined(AV_RB32)
  64. # define AV_RN32(p) AV_RB32(p)
  65. # endif
  66. # if defined(AV_WN32) && !defined(AV_WB32)
  67. # define AV_WB32(p, v) AV_WN32(p, v)
  68. # elif !defined(AV_WN32) && defined(AV_WB32)
  69. # define AV_WN32(p, v) AV_WB32(p, v)
  70. # endif
  71. # if defined(AV_RN64) && !defined(AV_RB64)
  72. # define AV_RB64(p) AV_RN64(p)
  73. # elif !defined(AV_RN64) && defined(AV_RB64)
  74. # define AV_RN64(p) AV_RB64(p)
  75. # endif
  76. # if defined(AV_WN64) && !defined(AV_WB64)
  77. # define AV_WB64(p, v) AV_WN64(p, v)
  78. # elif !defined(AV_WN64) && defined(AV_WB64)
  79. # define AV_WN64(p, v) AV_WB64(p, v)
  80. # endif
  81. #else /* HAVE_BIGENDIAN */
  82. # if defined(AV_RN16) && !defined(AV_RL16)
  83. # define AV_RL16(p) AV_RN16(p)
  84. # elif !defined(AV_RN16) && defined(AV_RL16)
  85. # define AV_RN16(p) AV_RL16(p)
  86. # endif
  87. # if defined(AV_WN16) && !defined(AV_WL16)
  88. # define AV_WL16(p, v) AV_WN16(p, v)
  89. # elif !defined(AV_WN16) && defined(AV_WL16)
  90. # define AV_WN16(p, v) AV_WL16(p, v)
  91. # endif
  92. # if defined(AV_RN24) && !defined(AV_RL24)
  93. # define AV_RL24(p) AV_RN24(p)
  94. # elif !defined(AV_RN24) && defined(AV_RL24)
  95. # define AV_RN24(p) AV_RL24(p)
  96. # endif
  97. # if defined(AV_WN24) && !defined(AV_WL24)
  98. # define AV_WL24(p, v) AV_WN24(p, v)
  99. # elif !defined(AV_WN24) && defined(AV_WL24)
  100. # define AV_WN24(p, v) AV_WL24(p, v)
  101. # endif
  102. # if defined(AV_RN32) && !defined(AV_RL32)
  103. # define AV_RL32(p) AV_RN32(p)
  104. # elif !defined(AV_RN32) && defined(AV_RL32)
  105. # define AV_RN32(p) AV_RL32(p)
  106. # endif
  107. # if defined(AV_WN32) && !defined(AV_WL32)
  108. # define AV_WL32(p, v) AV_WN32(p, v)
  109. # elif !defined(AV_WN32) && defined(AV_WL32)
  110. # define AV_WN32(p, v) AV_WL32(p, v)
  111. # endif
  112. # if defined(AV_RN64) && !defined(AV_RL64)
  113. # define AV_RL64(p) AV_RN64(p)
  114. # elif !defined(AV_RN64) && defined(AV_RL64)
  115. # define AV_RN64(p) AV_RL64(p)
  116. # endif
  117. # if defined(AV_WN64) && !defined(AV_WL64)
  118. # define AV_WL64(p, v) AV_WN64(p, v)
  119. # elif !defined(AV_WN64) && defined(AV_WL64)
  120. # define AV_WN64(p, v) AV_WL64(p, v)
  121. # endif
  122. #endif /* !HAVE_BIGENDIAN */
  123. /*
  124. * Define AV_[RW]N helper macros to simplify definitions not provided
  125. * by per-arch headers.
  126. */
  127. #if HAVE_ATTRIBUTE_PACKED
  128. struct unaligned_64 { uint64_t l; } __attribute__((packed));
  129. struct unaligned_32 { uint32_t l; } __attribute__((packed));
  130. struct unaligned_16 { uint16_t l; } __attribute__((packed));
  131. # define AV_RN(s, p) (((const struct unaligned_##s *) (p))->l)
  132. # define AV_WN(s, p, v) (((struct unaligned_##s *) (p))->l) = (v)
  133. #elif defined(__DECC)
  134. # define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
  135. # define AV_WN(s, p, v) *((__unaligned uint##s##_t*)(p)) = (v)
  136. #elif HAVE_FAST_UNALIGNED
  137. # define AV_RN(s, p) (*((const uint##s##_t*)(p)))
  138. # define AV_WN(s, p, v) *((uint##s##_t*)(p)) = (v)
  139. #else
  140. #ifndef AV_RB16
  141. # define AV_RB16(x) \
  142. ((((const uint8_t*)(x))[0] << 8) | \
  143. ((const uint8_t*)(x))[1])
  144. #endif
  145. #ifndef AV_WB16
  146. # define AV_WB16(p, d) do { \
  147. ((uint8_t*)(p))[1] = (d); \
  148. ((uint8_t*)(p))[0] = (d)>>8; \
  149. } while(0)
  150. #endif
  151. #ifndef AV_RL16
  152. # define AV_RL16(x) \
  153. ((((const uint8_t*)(x))[1] << 8) | \
  154. ((const uint8_t*)(x))[0])
  155. #endif
  156. #ifndef AV_WL16
  157. # define AV_WL16(p, d) do { \
  158. ((uint8_t*)(p))[0] = (d); \
  159. ((uint8_t*)(p))[1] = (d)>>8; \
  160. } while(0)
  161. #endif
  162. #ifndef AV_RB32
  163. # define AV_RB32(x) \
  164. ((((const uint8_t*)(x))[0] << 24) | \
  165. (((const uint8_t*)(x))[1] << 16) | \
  166. (((const uint8_t*)(x))[2] << 8) | \
  167. ((const uint8_t*)(x))[3])
  168. #endif
  169. #ifndef AV_WB32
  170. # define AV_WB32(p, d) do { \
  171. ((uint8_t*)(p))[3] = (d); \
  172. ((uint8_t*)(p))[2] = (d)>>8; \
  173. ((uint8_t*)(p))[1] = (d)>>16; \
  174. ((uint8_t*)(p))[0] = (d)>>24; \
  175. } while(0)
  176. #endif
  177. #ifndef AV_RL32
  178. # define AV_RL32(x) \
  179. ((((const uint8_t*)(x))[3] << 24) | \
  180. (((const uint8_t*)(x))[2] << 16) | \
  181. (((const uint8_t*)(x))[1] << 8) | \
  182. ((const uint8_t*)(x))[0])
  183. #endif
  184. #ifndef AV_WL32
  185. # define AV_WL32(p, d) do { \
  186. ((uint8_t*)(p))[0] = (d); \
  187. ((uint8_t*)(p))[1] = (d)>>8; \
  188. ((uint8_t*)(p))[2] = (d)>>16; \
  189. ((uint8_t*)(p))[3] = (d)>>24; \
  190. } while(0)
  191. #endif
  192. #ifndef AV_RB64
  193. # define AV_RB64(x) \
  194. (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
  195. ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
  196. ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
  197. ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
  198. ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
  199. ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
  200. ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
  201. (uint64_t)((const uint8_t*)(x))[7])
  202. #endif
  203. #ifndef AV_WB64
  204. # define AV_WB64(p, d) do { \
  205. ((uint8_t*)(p))[7] = (d); \
  206. ((uint8_t*)(p))[6] = (d)>>8; \
  207. ((uint8_t*)(p))[5] = (d)>>16; \
  208. ((uint8_t*)(p))[4] = (d)>>24; \
  209. ((uint8_t*)(p))[3] = (d)>>32; \
  210. ((uint8_t*)(p))[2] = (d)>>40; \
  211. ((uint8_t*)(p))[1] = (d)>>48; \
  212. ((uint8_t*)(p))[0] = (d)>>56; \
  213. } while(0)
  214. #endif
  215. #ifndef AV_RL64
  216. # define AV_RL64(x) \
  217. (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
  218. ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
  219. ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
  220. ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
  221. ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
  222. ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
  223. ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
  224. (uint64_t)((const uint8_t*)(x))[0])
  225. #endif
  226. #ifndef AV_WL64
  227. # define AV_WL64(p, d) do { \
  228. ((uint8_t*)(p))[0] = (d); \
  229. ((uint8_t*)(p))[1] = (d)>>8; \
  230. ((uint8_t*)(p))[2] = (d)>>16; \
  231. ((uint8_t*)(p))[3] = (d)>>24; \
  232. ((uint8_t*)(p))[4] = (d)>>32; \
  233. ((uint8_t*)(p))[5] = (d)>>40; \
  234. ((uint8_t*)(p))[6] = (d)>>48; \
  235. ((uint8_t*)(p))[7] = (d)>>56; \
  236. } while(0)
  237. #endif
  238. #if HAVE_BIGENDIAN
  239. # define AV_RN(s, p) AV_RB##s(p)
  240. # define AV_WN(s, p, v) AV_WB##s(p, v)
  241. #else
  242. # define AV_RN(s, p) AV_RL##s(p)
  243. # define AV_WN(s, p, v) AV_WL##s(p, v)
  244. #endif
  245. #endif /* HAVE_FAST_UNALIGNED */
  246. #ifndef AV_RN16
  247. # define AV_RN16(p) AV_RN(16, p)
  248. #endif
  249. #ifndef AV_RN32
  250. # define AV_RN32(p) AV_RN(32, p)
  251. #endif
  252. #ifndef AV_RN64
  253. # define AV_RN64(p) AV_RN(64, p)
  254. #endif
  255. #ifndef AV_WN16
  256. # define AV_WN16(p, v) AV_WN(16, p, v)
  257. #endif
  258. #ifndef AV_WN32
  259. # define AV_WN32(p, v) AV_WN(32, p, v)
  260. #endif
  261. #ifndef AV_WN64
  262. # define AV_WN64(p, v) AV_WN(64, p, v)
  263. #endif
  264. #if HAVE_BIGENDIAN
  265. # define AV_RB(s, p) AV_RN##s(p)
  266. # define AV_WB(s, p, v) AV_WN##s(p, v)
  267. # define AV_RL(s, p) bswap_##s(AV_RN##s(p))
  268. # define AV_WL(s, p, v) AV_WN##s(p, bswap_##s(v))
  269. #else
  270. # define AV_RB(s, p) bswap_##s(AV_RN##s(p))
  271. # define AV_WB(s, p, v) AV_WN##s(p, bswap_##s(v))
  272. # define AV_RL(s, p) AV_RN##s(p)
  273. # define AV_WL(s, p, v) AV_WN##s(p, v)
  274. #endif
  275. #define AV_RB8(x) (((const uint8_t*)(x))[0])
  276. #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
  277. #define AV_RL8(x) AV_RB8(x)
  278. #define AV_WL8(p, d) AV_WB8(p, d)
  279. #ifndef AV_RB16
  280. # define AV_RB16(p) AV_RB(16, p)
  281. #endif
  282. #ifndef AV_WB16
  283. # define AV_WB16(p, v) AV_WB(16, p, v)
  284. #endif
  285. #ifndef AV_RL16
  286. # define AV_RL16(p) AV_RL(16, p)
  287. #endif
  288. #ifndef AV_WL16
  289. # define AV_WL16(p, v) AV_WL(16, p, v)
  290. #endif
  291. #ifndef AV_RB32
  292. # define AV_RB32(p) AV_RB(32, p)
  293. #endif
  294. #ifndef AV_WB32
  295. # define AV_WB32(p, v) AV_WB(32, p, v)
  296. #endif
  297. #ifndef AV_RL32
  298. # define AV_RL32(p) AV_RL(32, p)
  299. #endif
  300. #ifndef AV_WL32
  301. # define AV_WL32(p, v) AV_WL(32, p, v)
  302. #endif
  303. #ifndef AV_RB64
  304. # define AV_RB64(p) AV_RB(64, p)
  305. #endif
  306. #ifndef AV_WB64
  307. # define AV_WB64(p, v) AV_WB(64, p, v)
  308. #endif
  309. #ifndef AV_RL64
  310. # define AV_RL64(p) AV_RL(64, p)
  311. #endif
  312. #ifndef AV_WL64
  313. # define AV_WL64(p, v) AV_WL(64, p, v)
  314. #endif
  315. #ifndef AV_RB24
  316. # define AV_RB24(x) \
  317. ((((const uint8_t*)(x))[0] << 16) | \
  318. (((const uint8_t*)(x))[1] << 8) | \
  319. ((const uint8_t*)(x))[2])
  320. #endif
  321. #ifndef AV_WB24
  322. # define AV_WB24(p, d) do { \
  323. ((uint8_t*)(p))[2] = (d); \
  324. ((uint8_t*)(p))[1] = (d)>>8; \
  325. ((uint8_t*)(p))[0] = (d)>>16; \
  326. } while(0)
  327. #endif
  328. #ifndef AV_RL24
  329. # define AV_RL24(x) \
  330. ((((const uint8_t*)(x))[2] << 16) | \
  331. (((const uint8_t*)(x))[1] << 8) | \
  332. ((const uint8_t*)(x))[0])
  333. #endif
  334. #ifndef AV_WL24
  335. # define AV_WL24(p, d) do { \
  336. ((uint8_t*)(p))[0] = (d); \
  337. ((uint8_t*)(p))[1] = (d)>>8; \
  338. ((uint8_t*)(p))[2] = (d)>>16; \
  339. } while(0)
  340. #endif
  341. #endif /* AVUTIL_INTREADWRITE_H */