dts_bitstream.c

00001 /*
00002  * bitstream.c
00003  * Copyright (C) 2004 Gildas Bazin <[email protected]>
00004  * Copyright (C) 2000-2003 Michel Lespinasse <[email protected]>
00005  * Copyright (C) 1999-2000 Aaron Holtzman <[email protected]>
00006  *
00007  * This file is part of dtsdec, a free DTS Coherent Acoustics stream decoder.
00008  * See http://www.videolan.org/dtsdec.html for updates.
00009  *
00010  * dtsdec is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * dtsdec is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 
00025 #include "config.h"
00026 
00027 #include <inttypes.h>
00028 
00029 #include "dts.h"
00030 #include "dts_internal.h"
00031 #include "bitstream.h"
00032 
00033 #define BUFFER_SIZE 4096
00034 
00035 void dts_bitstream_init (dts_state_t * state, uint8_t * buf, int word_mode,
00036                          int bigendian_mode)
00037 {
00038     intptr_t align;
00039 
00040     align = (uintptr_t)buf & 3;
00041     state->buffer_start = (uint32_t *) (buf - align);
00042     state->bits_left = 0;
00043     state->current_word = 0;
00044     state->word_mode = word_mode;
00045     state->bigendian_mode = bigendian_mode;
00046     bitstream_get (state, align * 8);
00047 }
00048 #include<stdio.h>
00049 static inline void bitstream_fill_current (dts_state_t * state)
00050 {
00051     uint32_t tmp;
00052 
00053     tmp = *(state->buffer_start++);
00054 
00055     if (state->bigendian_mode)
00056         state->current_word = swab32 (tmp);
00057     else
00058         state->current_word = swable32 (tmp);
00059 
00060     if (!state->word_mode)
00061     {
00062         state->current_word = (state->current_word & 0x00003FFF) |
00063             ((state->current_word & 0x3FFF0000 ) >> 2);
00064     }
00065 }
00066 
00067 /*
00068  * The fast paths for _get is in the
00069  * bitstream.h header file so it can be inlined.
00070  *
00071  * The "bottom half" of this routine is suffixed _bh
00072  *
00073  * -ah
00074  */
00075 
00076 uint32_t dts_bitstream_get_bh (dts_state_t * state, uint32_t num_bits)
00077 {
00078     uint32_t result;
00079 
00080     num_bits -= state->bits_left;
00081 
00082     result = ((state->current_word << (32 - state->bits_left)) >>
00083               (32 - state->bits_left));
00084 
00085     if ( !state->word_mode && num_bits > 28 ) {
00086         bitstream_fill_current (state);
00087         result = (result << 28) | state->current_word;
00088         num_bits -= 28;
00089     }
00090 
00091     bitstream_fill_current (state);
00092 
00093     if ( state->word_mode )
00094     {
00095         if (num_bits != 0)
00096             result = (result << num_bits) |
00097                      (state->current_word >> (32 - num_bits));
00098 
00099         state->bits_left = 32 - num_bits;
00100     }
00101     else
00102     {
00103         if (num_bits != 0)
00104             result = (result << num_bits) |
00105                      (state->current_word >> (28 - num_bits));
00106 
00107         state->bits_left = 28 - num_bits;
00108     }
00109 
00110     return result;
00111 }

Generated on Tue Dec 13 14:47:27 2005 for guliverkli by  doxygen 1.4.5