bitstream.c
上传用户:aoeyumen
上传日期:2007-01-06
资源大小:3329k
文件大小:3k
源码类别:

DVD

开发平台:

Unix_Linux

  1. /* 
  2.  *  bitstream.c
  3.  *
  4.  * Copyright (C) Aaron Holtzman - May 1999
  5.  *
  6.  *  This file is part of ac3dec, a free Dolby AC-3 stream decoder.
  7.  *
  8.  *  ac3dec is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  ac3dec is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include "ac3.h"
  26. #include "decode.h"
  27. #include "crc.h"
  28. #include "bitstream.h"
  29. //My vego-matic endian swapping routine
  30. #define SWAP_ENDIAN32(x)  ((((uint_8*)&x)[0] << 24) |  
  31.                          (((uint_8*)&x)[1] << 16) |  
  32.                          (((uint_8*)&x)[2] << 8) |   
  33.                          ((uint_8*)&x)[3])           
  34. static void bitstream_load(bitstream_t *bs);
  35. /* Fetches 1-32 bits from the file opened in bitstream_open */
  36. uint_32
  37. bitstream_get(bitstream_t *bs,uint_32 num_bits)
  38. {
  39. uint_32 result;
  40. uint_32 bits_read;
  41. uint_32 bits_to_go;
  42. if(num_bits == 0)
  43. return 0;
  44. #ifdef DO_NIST
  45.         result = inputstream->get_bits(num_bits);
  46. #else
  47. bits_read = num_bits > bs->bits_left ? bs->bits_left : num_bits; 
  48. result = bs->current_word  >> (32 - bits_read);
  49. bs->current_word <<= bits_read;
  50. bs->bits_left -= bits_read;
  51. if(bs->bits_left == 0)
  52. bitstream_load(bs);
  53. if (bits_read < num_bits)
  54. {
  55. bits_to_go = num_bits - bits_read;
  56. result <<= bits_to_go;
  57. result |= bs->current_word  >> (32 - bits_to_go);
  58. bs->current_word <<= bits_to_go;
  59. bs->bits_left -= bits_to_go;
  60. }
  61. bs->total_bits_read += num_bits;
  62. #endif
  63. crc_process(result,num_bits);
  64. return result;
  65. }
  66. #ifndef DO_NIST
  67. static void
  68. bitstream_load(bitstream_t *bs)
  69. {
  70. int bytes_read;
  71. bytes_read = fread(&bs->current_word,1,4,bs->file);
  72. bs->current_word = SWAP_ENDIAN32(bs->current_word);
  73. bs->bits_left = bytes_read * 8;
  74. //FIXME finishing up the stream isn't done too gracefully
  75. if (bytes_read < 4)
  76. bs->done = 1;
  77. }
  78. /* Opens a bitstream for use in bitstream_get */
  79. bitstream_t*
  80. bitstream_open(FILE *file)
  81. {
  82. bitstream_t *bs;
  83. if(!file)
  84. return 0;
  85. bs = malloc(sizeof(bitstream_t));
  86. if(!bs)
  87. return 0;
  88. bs->done = 0;
  89. /* Read in the first 32 bit word and initialize the structure */
  90. bs->file = file;
  91. bitstream_load(bs);
  92. return bs;
  93. }
  94. int bitstream_done(bitstream_t *bs)
  95. {
  96. return (bs->done);
  97. }
  98. void 
  99. bitstream_close(bitstream_t *bs)
  100. {
  101. if(bs->file)
  102. fclose(bs->file);
  103. free(bs);
  104. }
  105. #endif