bit_stream_reader.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright (c) 2011, 2012, Simon Howard
  3. Permission to use, copy, modify, and/or distribute this software
  4. for any purpose with or without fee is hereby granted, provided
  5. that the above copyright notice and this permission notice appear
  6. in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  10. AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. //
  17. // Data structure used to read bits from an input source as a stream.
  18. //
  19. // This file is designed to be #included by other source files to
  20. // make a complete decoder.
  21. //
  22. typedef struct {
  23. // Callback function to invoke to read more data from the
  24. // input stream.
  25. LHADecoderCallback callback;
  26. void *callback_data;
  27. // Bits from the input stream that are waiting to be read.
  28. uint32_t bit_buffer;
  29. unsigned int bits;
  30. } BitStreamReader;
  31. // Initialize bit stream reader structure.
  32. static void bit_stream_reader_init(BitStreamReader *reader,
  33. LHADecoderCallback callback,
  34. void *callback_data)
  35. {
  36. reader->callback = callback;
  37. reader->callback_data = callback_data;
  38. reader->bits = 0;
  39. reader->bit_buffer = 0;
  40. }
  41. // Return the next n bits waiting to be read from the input stream,
  42. // without removing any. Returns -1 for failure.
  43. static int peek_bits(BitStreamReader *reader,
  44. unsigned int n)
  45. {
  46. uint8_t buf[4];
  47. unsigned int fill_bytes;
  48. size_t bytes;
  49. if (n == 0) {
  50. return 0;
  51. }
  52. // If there are not enough bits in the buffer to satisfy this
  53. // request, we need to fill up the buffer with more bits.
  54. while (reader->bits < n) {
  55. // Maximum number of bytes we can fill?
  56. fill_bytes = (32 - reader->bits) / 8;
  57. // Read from input and fill bit_buffer.
  58. memset(buf, 0, sizeof(buf));
  59. bytes = reader->callback(buf, fill_bytes,
  60. reader->callback_data);
  61. // End of file?
  62. if (bytes == 0) {
  63. return -1;
  64. }
  65. reader->bit_buffer |= (uint32_t) buf[0] << (24 - reader->bits);
  66. reader->bit_buffer |= (uint32_t) buf[1] << (16 - reader->bits);
  67. reader->bit_buffer |= (uint32_t) buf[2] << (8 - reader->bits);
  68. reader->bit_buffer |= (uint32_t) buf[3];
  69. reader->bits += bytes * 8;
  70. }
  71. return (signed int) (reader->bit_buffer >> (32 - n));
  72. }
  73. // Read a bit from the input stream.
  74. // Returns -1 for failure.
  75. static int read_bits(BitStreamReader *reader,
  76. unsigned int n)
  77. {
  78. int result;
  79. result = peek_bits(reader, n);
  80. if (result >= 0) {
  81. reader->bit_buffer <<= n;
  82. reader->bits -= n;
  83. }
  84. return result;
  85. }
  86. // Read a bit from the input stream.
  87. // Returns -1 for failure.
  88. static int read_bit(BitStreamReader *reader)
  89. {
  90. return read_bits(reader, 1);
  91. }