intreadwrite.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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|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. * Define AV_[RW]N helper macros to simplify definitions not provided
  37. * by per-arch headers.
  38. */
  39. #if HAVE_ATTRIBUTE_PACKED
  40. struct unaligned_64 { uint64_t l; } __attribute__((packed));
  41. struct unaligned_32 { uint32_t l; } __attribute__((packed));
  42. struct unaligned_16 { uint16_t l; } __attribute__((packed));
  43. # define AV_RN(s, p) (((const struct unaligned_##s *) (p))->l)
  44. # define AV_WN(s, p, v) (((struct unaligned_##s *) (p))->l) = (v)
  45. #elif defined(__DECC)
  46. # define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
  47. # define AV_WN(s, p, v) *((__unaligned uint##s##_t*)(p)) = (v)
  48. #elif HAVE_FAST_UNALIGNED
  49. # define AV_RN(s, p) (*((const uint##s##_t*)(p)))
  50. # define AV_WN(s, p, v) *((uint##s##_t*)(p)) = (v)
  51. #else
  52. #ifndef AV_RB16
  53. #define AV_RB16(x) ((((const uint8_t*)(x))[0] << 8) | \
  54. ((const uint8_t*)(x))[1])
  55. #endif
  56. #ifndef AV_WB16
  57. #define AV_WB16(p, d) do { \
  58. ((uint8_t*)(p))[1] = (d); \
  59. ((uint8_t*)(p))[0] = (d)>>8; } while(0)
  60. #endif
  61. #ifndef AV_RL16
  62. #define AV_RL16(x) ((((const uint8_t*)(x))[1] << 8) | \
  63. ((const uint8_t*)(x))[0])
  64. #endif
  65. #ifndef AV_WL16
  66. #define AV_WL16(p, d) do { \
  67. ((uint8_t*)(p))[0] = (d); \
  68. ((uint8_t*)(p))[1] = (d)>>8; } while(0)
  69. #endif
  70. #ifndef AV_RB32
  71. #define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \
  72. (((const uint8_t*)(x))[1] << 16) | \
  73. (((const uint8_t*)(x))[2] << 8) | \
  74. ((const uint8_t*)(x))[3])
  75. #endif
  76. #ifndef AV_WB32
  77. #define AV_WB32(p, d) do { \
  78. ((uint8_t*)(p))[3] = (d); \
  79. ((uint8_t*)(p))[2] = (d)>>8; \
  80. ((uint8_t*)(p))[1] = (d)>>16; \
  81. ((uint8_t*)(p))[0] = (d)>>24; } while(0)
  82. #endif
  83. #ifndef AV_RL32
  84. #define AV_RL32(x) ((((const uint8_t*)(x))[3] << 24) | \
  85. (((const uint8_t*)(x))[2] << 16) | \
  86. (((const uint8_t*)(x))[1] << 8) | \
  87. ((const uint8_t*)(x))[0])
  88. #endif
  89. #ifndef AV_WL32
  90. #define AV_WL32(p, d) do { \
  91. ((uint8_t*)(p))[0] = (d); \
  92. ((uint8_t*)(p))[1] = (d)>>8; \
  93. ((uint8_t*)(p))[2] = (d)>>16; \
  94. ((uint8_t*)(p))[3] = (d)>>24; } while(0)
  95. #endif
  96. #ifndef AV_RB64
  97. #define AV_RB64(x) (((uint64_t)((const uint8_t*)(x))[0] << 56) | \
  98. ((uint64_t)((const uint8_t*)(x))[1] << 48) | \
  99. ((uint64_t)((const uint8_t*)(x))[2] << 40) | \
  100. ((uint64_t)((const uint8_t*)(x))[3] << 32) | \
  101. ((uint64_t)((const uint8_t*)(x))[4] << 24) | \
  102. ((uint64_t)((const uint8_t*)(x))[5] << 16) | \
  103. ((uint64_t)((const uint8_t*)(x))[6] << 8) | \
  104. (uint64_t)((const uint8_t*)(x))[7])
  105. #endif
  106. #ifndef AV_WB64
  107. #define AV_WB64(p, d) do { \
  108. ((uint8_t*)(p))[7] = (d); \
  109. ((uint8_t*)(p))[6] = (d)>>8; \
  110. ((uint8_t*)(p))[5] = (d)>>16; \
  111. ((uint8_t*)(p))[4] = (d)>>24; \
  112. ((uint8_t*)(p))[3] = (d)>>32; \
  113. ((uint8_t*)(p))[2] = (d)>>40; \
  114. ((uint8_t*)(p))[1] = (d)>>48; \
  115. ((uint8_t*)(p))[0] = (d)>>56; } while(0)
  116. #endif
  117. #ifndef AV_RL64
  118. #define AV_RL64(x) (((uint64_t)((const uint8_t*)(x))[7] << 56) | \
  119. ((uint64_t)((const uint8_t*)(x))[6] << 48) | \
  120. ((uint64_t)((const uint8_t*)(x))[5] << 40) | \
  121. ((uint64_t)((const uint8_t*)(x))[4] << 32) | \
  122. ((uint64_t)((const uint8_t*)(x))[3] << 24) | \
  123. ((uint64_t)((const uint8_t*)(x))[2] << 16) | \
  124. ((uint64_t)((const uint8_t*)(x))[1] << 8) | \
  125. (uint64_t)((const uint8_t*)(x))[0])
  126. #endif
  127. #ifndef AV_WL64
  128. #define AV_WL64(p, d) do { \
  129. ((uint8_t*)(p))[0] = (d); \
  130. ((uint8_t*)(p))[1] = (d)>>8; \
  131. ((uint8_t*)(p))[2] = (d)>>16; \
  132. ((uint8_t*)(p))[3] = (d)>>24; \
  133. ((uint8_t*)(p))[4] = (d)>>32; \
  134. ((uint8_t*)(p))[5] = (d)>>40; \
  135. ((uint8_t*)(p))[6] = (d)>>48; \
  136. ((uint8_t*)(p))[7] = (d)>>56; } while(0)
  137. #endif
  138. #if HAVE_BIGENDIAN
  139. # define AV_RN(s, p) AV_RB##s(p)
  140. # define AV_WN(s, p, v) AV_WB##s(p, v)
  141. #else
  142. # define AV_RN(s, p) AV_RL##s(p)
  143. # define AV_WN(s, p, v) AV_WL##s(p, v)
  144. #endif
  145. #endif /* HAVE_FAST_UNALIGNED */
  146. #ifndef AV_RN16
  147. # define AV_RN16(p) AV_RN(16, p)
  148. #endif
  149. #ifndef AV_RN32
  150. # define AV_RN32(p) AV_RN(32, p)
  151. #endif
  152. #ifndef AV_RN64
  153. # define AV_RN64(p) AV_RN(64, p)
  154. #endif
  155. #ifndef AV_WN16
  156. # define AV_WN16(p, v) AV_WN(16, p, v)
  157. #endif
  158. #ifndef AV_WN32
  159. # define AV_WN32(p, v) AV_WN(32, p, v)
  160. #endif
  161. #ifndef AV_WN64
  162. # define AV_WN64(p, v) AV_WN(64, p, v)
  163. #endif
  164. #if HAVE_BIGENDIAN
  165. # define AV_RB(s, p) AV_RN(s, p)
  166. # define AV_WB(s, p, v) AV_WN(s, p, v)
  167. # define AV_RL(s, p) bswap_##s(AV_RN(s, p))
  168. # define AV_WL(s, p, v) AV_WN(s, p, bswap_##s(v))
  169. #else
  170. # define AV_RB(s, p) bswap_##s(AV_RN(s, p))
  171. # define AV_WB(s, p, v) AV_WN(s, p, bswap_##s(v))
  172. # define AV_RL(s, p) AV_RN(s, p)
  173. # define AV_WL(s, p, v) AV_WN(s, p, v)
  174. #endif
  175. #define AV_RB8(x) (((const uint8_t*)(x))[0])
  176. #define AV_WB8(p, d) do { ((uint8_t*)(p))[0] = (d); } while(0)
  177. #define AV_RL8(x) AV_RB8(x)
  178. #define AV_WL8(p, d) AV_WB8(p, d)
  179. #ifndef AV_RB16
  180. # define AV_RB16(p) AV_RB(16, p)
  181. #endif
  182. #ifndef AV_WB16
  183. # define AV_WB16(p, v) AV_WB(16, p, v)
  184. #endif
  185. #ifndef AV_RL16
  186. # define AV_RL16(p) AV_RL(16, p)
  187. #endif
  188. #ifndef AV_WL16
  189. # define AV_WL16(p, v) AV_WL(16, p, v)
  190. #endif
  191. #ifndef AV_RB32
  192. # define AV_RB32(p) AV_RB(32, p)
  193. #endif
  194. #ifndef AV_WB32
  195. # define AV_WB32(p, v) AV_WB(32, p, v)
  196. #endif
  197. #ifndef AV_RL32
  198. # define AV_RL32(p) AV_RL(32, p)
  199. #endif
  200. #ifndef AV_WL32
  201. # define AV_WL32(p, v) AV_WL(32, p, v)
  202. #endif
  203. #ifndef AV_RB64
  204. # define AV_RB64(p) AV_RB(64, p)
  205. #endif
  206. #ifndef AV_WB64
  207. # define AV_WB64(p, v) AV_WB(64, p, v)
  208. #endif
  209. #ifndef AV_RL64
  210. # define AV_RL64(p) AV_RL(64, p)
  211. #endif
  212. #ifndef AV_WL64
  213. # define AV_WL64(p, v) AV_WL(64, p, v)
  214. #endif
  215. #define AV_RB24(x) ((((const uint8_t*)(x))[0] << 16) | \
  216. (((const uint8_t*)(x))[1] << 8) | \
  217. ((const uint8_t*)(x))[2])
  218. #define AV_WB24(p, d) do { \
  219. ((uint8_t*)(p))[2] = (d); \
  220. ((uint8_t*)(p))[1] = (d)>>8; \
  221. ((uint8_t*)(p))[0] = (d)>>16; } while(0)
  222. #define AV_RL24(x) ((((const uint8_t*)(x))[2] << 16) | \
  223. (((const uint8_t*)(x))[1] << 8) | \
  224. ((const uint8_t*)(x))[0])
  225. #define AV_WL24(p, d) do { \
  226. ((uint8_t*)(p))[0] = (d); \
  227. ((uint8_t*)(p))[1] = (d)>>8; \
  228. ((uint8_t*)(p))[2] = (d)>>16; } while(0)
  229. #endif /* AVUTIL_INTREADWRITE_H */