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.

119 lines
3.0KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * byte swapping routines
  23. */
  24. #ifndef AVUTIL_BSWAP_H
  25. #define AVUTIL_BSWAP_H
  26. #include <stdint.h>
  27. #include "config.h"
  28. #include "attributes.h"
  29. #if ARCH_ARM
  30. # include "arm/bswap.h"
  31. #elif ARCH_AVR32
  32. # include "avr32/bswap.h"
  33. #elif ARCH_BFIN
  34. # include "bfin/bswap.h"
  35. #elif ARCH_SH4
  36. # include "sh4/bswap.h"
  37. #elif ARCH_X86
  38. # include "x86/bswap.h"
  39. #endif
  40. #define AV_BSWAP16C(x) (((x) << 8 & 0xff00) | ((x) >> 8 & 0x00ff))
  41. #define AV_BSWAP32C(x) (AV_BSWAP16C(x) << 16 | AV_BSWAP16C((x) >> 16))
  42. #define AV_BSWAP64C(x) (AV_BSWAP32C(x) << 32 | AV_BSWAP32C((x) >> 32))
  43. #define AV_BSWAPC(s, x) AV_BSWAP##s##C(x)
  44. #ifndef bswap_16
  45. static av_always_inline av_const uint16_t bswap_16(uint16_t x)
  46. {
  47. x= (x>>8) | (x<<8);
  48. return x;
  49. }
  50. #endif
  51. #ifndef bswap_32
  52. static av_always_inline av_const uint32_t bswap_32(uint32_t x)
  53. {
  54. x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
  55. x= (x>>16) | (x<<16);
  56. return x;
  57. }
  58. #endif
  59. #ifndef bswap_64
  60. static inline uint64_t av_const bswap_64(uint64_t x)
  61. {
  62. #if 0
  63. x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
  64. x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
  65. return (x>>32) | (x<<32);
  66. #else
  67. union {
  68. uint64_t ll;
  69. uint32_t l[2];
  70. } w, r;
  71. w.ll = x;
  72. r.l[0] = bswap_32 (w.l[1]);
  73. r.l[1] = bswap_32 (w.l[0]);
  74. return r.ll;
  75. #endif
  76. }
  77. #endif
  78. // be2me ... big-endian to machine-endian
  79. // le2me ... little-endian to machine-endian
  80. #if HAVE_BIGENDIAN
  81. #define be2me_16(x) (x)
  82. #define be2me_32(x) (x)
  83. #define be2me_64(x) (x)
  84. #define le2me_16(x) bswap_16(x)
  85. #define le2me_32(x) bswap_32(x)
  86. #define le2me_64(x) bswap_64(x)
  87. #define AV_BE2MEC(s, x) (x)
  88. #define AV_LE2MEC(s, x) AV_BSWAPC(s, x)
  89. #else
  90. #define be2me_16(x) bswap_16(x)
  91. #define be2me_32(x) bswap_32(x)
  92. #define be2me_64(x) bswap_64(x)
  93. #define le2me_16(x) (x)
  94. #define le2me_32(x) (x)
  95. #define le2me_64(x) (x)
  96. #define AV_BE2MEC(s, x) AV_BSWAPC(s, x)
  97. #define AV_LE2MEC(s, x) (x)
  98. #endif
  99. #define AV_BE2ME16C(x) AV_BE2MEC(16, x)
  100. #define AV_BE2ME32C(x) AV_BE2MEC(32, x)
  101. #define AV_BE2ME64C(x) AV_BE2MEC(64, x)
  102. #define AV_LE2ME16C(x) AV_LE2MEC(16, x)
  103. #define AV_LE2ME32C(x) AV_LE2MEC(32, x)
  104. #define AV_LE2ME64C(x) AV_LE2MEC(64, x)
  105. #endif /* AVUTIL_BSWAP_H */