oi_bitstream.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014 The Android Open Source Project
  4. * Copyright 2003 - 2004 Open Interface North America, Inc. All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. ******************************************************************************/
  19. #ifndef _OI_BITSTREAM_H
  20. #define _OI_BITSTREAM_H
  21. /**********************************************************************************
  22. $Revision: #1 $
  23. ***********************************************************************************/
  24. /**
  25. @file
  26. Function prototypes and macro definitions for manipulating input and output
  27. bitstreams.
  28. @ingroup codec_internal
  29. */
  30. /**
  31. @addtogroup codec_internal
  32. @{
  33. */
  34. #include "oi_codec_sbc_private.h"
  35. #include "oi_stddefs.h"
  36. INLINE void OI_BITSTREAM_ReadInit(OI_BITSTREAM *bs, const OI_BYTE *buffer);
  37. INLINE void OI_BITSTREAM_WriteInit(OI_BITSTREAM *bs, OI_BYTE *buffer);
  38. INLINE OI_UINT32 OI_BITSTREAM_ReadUINT(OI_BITSTREAM *bs, OI_UINT bits);
  39. INLINE OI_UINT8 OI_BITSTREAM_ReadUINT4Aligned(OI_BITSTREAM *bs);
  40. INLINE OI_UINT8 OI_BITSTREAM_ReadUINT8Aligned(OI_BITSTREAM *bs);
  41. INLINE void OI_BITSTREAM_WriteUINT(OI_BITSTREAM *bs,
  42. OI_UINT16 value,
  43. OI_UINT bits);
  44. /*
  45. * Use knowledge that the bitstream is aligned to optimize the write of a byte
  46. */
  47. PRIVATE void OI_BITSTREAM_WriteUINT8Aligned(OI_BITSTREAM *bs,
  48. OI_UINT8 datum);
  49. /*
  50. * Use knowledge that the bitstream is aligned to optimize the write pair of nibbles
  51. */
  52. PRIVATE void OI_BITSTREAM_Write2xUINT4Aligned(OI_BITSTREAM *bs,
  53. OI_UINT8 datum1,
  54. OI_UINT8 datum2);
  55. /** Internally the bitstream looks ahead in the stream. When
  56. * OI_SBC_ReadScalefactors() goes to temporarily break the abstraction, it will
  57. * need to know where the "logical" pointer is in the stream.
  58. */
  59. #define OI_BITSTREAM_GetWritePtr(bs) ((bs)->ptr.w - 3)
  60. #define OI_BITSTREAM_GetReadPtr(bs) ((bs)->ptr.r - 3)
  61. /** This is declared here as a macro because decoder.c breaks the bitsream
  62. * encapsulation for efficiency reasons.
  63. */
  64. #define OI_BITSTREAM_READUINT(result, bits, ptr, value, bitPtr) \
  65. do { \
  66. OI_ASSERT((bits) <= 16); \
  67. OI_ASSERT((bitPtr) < 16); \
  68. OI_ASSERT((bitPtr) >= 8); \
  69. \
  70. result = (value) << (bitPtr); \
  71. result >>= 32 - (bits); \
  72. \
  73. bitPtr += (bits); \
  74. while (bitPtr >= 16) { \
  75. value = ((value) << 8) | *ptr++; \
  76. bitPtr -= 8; \
  77. } \
  78. OI_ASSERT((bits == 0) || (result < (1u << (bits)))); \
  79. } while (0)
  80. #define OI_BITSTREAM_WRITEUINT(ptr, value, bitPtr, datum, bits) \
  81. do {\
  82. bitPtr -= bits;\
  83. value |= datum << bitPtr;\
  84. \
  85. while (bitPtr <= 16) {\
  86. bitPtr += 8;\
  87. *ptr++ = (OI_UINT8)(value >> 24);\
  88. value <<= 8;\
  89. }\
  90. } while (0)
  91. #define OI_BITSTREAM_WRITEFLUSH(ptr, value, bitPtr) \
  92. do {\
  93. while (bitPtr < 32) {\
  94. bitPtr += 8;\
  95. *ptr++ = (OI_UINT8)(value >> 24);\
  96. value <<= 8;\
  97. }\
  98. } while (0)
  99. /**
  100. @}
  101. */
  102. #endif /* _OI_BITSTREAM_H */