intreadwrite.h 12 KB

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