bswap.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 libavutil/bswap.h
  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. #ifndef bswap_16
  41. static av_always_inline av_const uint16_t bswap_16(uint16_t x)
  42. {
  43. x= (x>>8) | (x<<8);
  44. return x;
  45. }
  46. #endif
  47. #ifndef bswap_32
  48. static av_always_inline av_const uint32_t bswap_32(uint32_t x)
  49. {
  50. x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
  51. x= (x>>16) | (x<<16);
  52. return x;
  53. }
  54. #endif
  55. #ifndef bswap_64
  56. static inline uint64_t av_const bswap_64(uint64_t x)
  57. {
  58. #if 0
  59. x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
  60. x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
  61. return (x>>32) | (x<<32);
  62. #else
  63. union {
  64. uint64_t ll;
  65. uint32_t l[2];
  66. } w, r;
  67. w.ll = x;
  68. r.l[0] = bswap_32 (w.l[1]);
  69. r.l[1] = bswap_32 (w.l[0]);
  70. return r.ll;
  71. #endif
  72. }
  73. #endif
  74. // be2me ... big-endian to machine-endian
  75. // le2me ... little-endian to machine-endian
  76. #if HAVE_BIGENDIAN
  77. #define be2me_16(x) (x)
  78. #define be2me_32(x) (x)
  79. #define be2me_64(x) (x)
  80. #define le2me_16(x) bswap_16(x)
  81. #define le2me_32(x) bswap_32(x)
  82. #define le2me_64(x) bswap_64(x)
  83. #else
  84. #define be2me_16(x) bswap_16(x)
  85. #define be2me_32(x) bswap_32(x)
  86. #define be2me_64(x) bswap_64(x)
  87. #define le2me_16(x) (x)
  88. #define le2me_32(x) (x)
  89. #define le2me_64(x) (x)
  90. #endif
  91. #endif /* AVUTIL_BSWAP_H */