intreadwrite.h 8.1 KB

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