1 /*****************************************************************************
2 * ac3_decoder.c: core ac3 decoder
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ac3_decoder.c,v 1.8 2002/06/01 12:31:58 sam Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Michel Lespinasse <walken@zoy.org>
9 * Aaron Holtzman <aholtzma@engr.uvic.ca>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <string.h> /* memcpy() */
32 #include <vlc/decoder.h>
34 #include "ac3_imdct.h"
35 #include "ac3_downmix.h"
36 #include "ac3_adec.h" /* ac3dec_thread_t */
38 #include "ac3_internal.h"
40 static const float cmixlev_lut[4] = { 0.707, 0.595, 0.500, 0.707 };
41 static const float smixlev_lut[4] = { 0.707, 0.500, 0.0 , 0.500 };
43 int _M( ac3_init )(ac3dec_t * p_ac3dec)
45 p_ac3dec->mantissa.lfsr_state = 1; /* dither_gen initialization */
46 _M( imdct_init )(p_ac3dec->imdct) ;
51 int ac3_decode_frame (ac3dec_t * p_ac3dec, s16 * buffer)
55 if (parse_bsi (p_ac3dec))
57 msg_Warn( p_ac3dec->p_fifo, "parse error" );
58 parse_auxdata (p_ac3dec);
62 /* compute downmix parameters
63 * downmix to tow channels for now */
64 p_ac3dec->dm_par.clev = 0.0;
65 p_ac3dec->dm_par.slev = 0.0;
66 p_ac3dec->dm_par.unit = 1.0;
67 if (p_ac3dec->bsi.acmod & 0x1) /* have center */
68 p_ac3dec->dm_par.clev = cmixlev_lut[p_ac3dec->bsi.cmixlev];
70 if (p_ac3dec->bsi.acmod & 0x4) /* have surround channels */
71 p_ac3dec->dm_par.slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
73 p_ac3dec->dm_par.unit /= 1.0 + p_ac3dec->dm_par.clev + p_ac3dec->dm_par.slev;
74 p_ac3dec->dm_par.clev *= p_ac3dec->dm_par.unit;
75 p_ac3dec->dm_par.slev *= p_ac3dec->dm_par.unit;
77 for (i = 0; i < 6; i++) {
78 /* Initialize freq/time sample storage */
79 memset(p_ac3dec->samples, 0, sizeof(float) * 256 *
80 (p_ac3dec->bsi.nfchans + p_ac3dec->bsi.lfeon));
83 if( p_ac3dec->p_fifo->b_die || p_ac3dec->p_fifo->b_error )
88 if( parse_audblk( p_ac3dec, i ) )
90 msg_Warn( p_ac3dec->p_fifo, "audioblock error" );
91 parse_auxdata( p_ac3dec );
95 if( p_ac3dec->p_fifo->b_die || p_ac3dec->p_fifo->b_error )
100 if( exponent_unpack( p_ac3dec ) )
102 msg_Warn( p_ac3dec->p_fifo, "unpack error" );
103 parse_auxdata( p_ac3dec );
107 bit_allocate (p_ac3dec);
108 mantissa_unpack (p_ac3dec);
110 if( p_ac3dec->p_fifo->b_die || p_ac3dec->p_fifo->b_error )
115 if (p_ac3dec->bsi.acmod == 0x2)
120 imdct (p_ac3dec, buffer);
125 parse_auxdata (p_ac3dec);