bitstream-decode.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**********************************************************************************
  20. $Revision: #1 $
  21. ***********************************************************************************/
  22. /**
  23. @file
  24. Functions for manipulating input bitstreams.
  25. @ingroup codec_internal
  26. */
  27. /**
  28. @addtogroup codec_internal
  29. @{
  30. */
  31. #include "oi_stddefs.h"
  32. #include "oi_bitstream.h"
  33. #include "oi_assert.h"
  34. PRIVATE void OI_BITSTREAM_ReadInit(OI_BITSTREAM *bs,
  35. const OI_BYTE *buffer)
  36. {
  37. bs->value = ((OI_INT32)buffer[0] << 16) | ((OI_INT32)buffer[1] << 8) | (buffer[2]);
  38. bs->ptr.r = buffer + 3;
  39. bs->bitPtr = 8;
  40. }
  41. PRIVATE OI_UINT32 OI_BITSTREAM_ReadUINT(OI_BITSTREAM *bs, OI_UINT bits)
  42. {
  43. OI_UINT32 result;
  44. OI_BITSTREAM_READUINT(result, bits, bs->ptr.r, bs->value, bs->bitPtr);
  45. return result;
  46. }
  47. PRIVATE OI_UINT8 OI_BITSTREAM_ReadUINT4Aligned(OI_BITSTREAM *bs)
  48. {
  49. OI_UINT32 result;
  50. OI_ASSERT(bs->bitPtr < 16);
  51. OI_ASSERT(bs->bitPtr % 4 == 0);
  52. if (bs->bitPtr == 8) {
  53. result = bs->value << 8;
  54. bs->bitPtr = 12;
  55. } else {
  56. result = bs->value << 12;
  57. bs->value = (bs->value << 8) | *bs->ptr.r++;
  58. bs->bitPtr = 8;
  59. }
  60. result >>= 28;
  61. OI_ASSERT(result < (1u << 4));
  62. return (OI_UINT8)result;
  63. }
  64. PRIVATE OI_UINT8 OI_BITSTREAM_ReadUINT8Aligned(OI_BITSTREAM *bs)
  65. {
  66. OI_UINT32 result;
  67. OI_ASSERT(bs->bitPtr == 8);
  68. result = bs->value >> 16;
  69. bs->value = (bs->value << 8) | *bs->ptr.r++;
  70. return (OI_UINT8)result;
  71. }
  72. /**
  73. @}
  74. */