]> git.sesse.net Git - vlc/blob - plugins/ac3_adec/ac3_decoder.c
* ./plugins/text/rc.c: added a safety lock.
[vlc] / plugins / ac3_adec / ac3_decoder.c
1 /*****************************************************************************
2  * ac3_decoder.c: core ac3 decoder
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: ac3_decoder.c,v 1.7 2002/04/23 23:44:36 fenrir Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Michel Lespinasse <walken@zoy.org>
9  *          Aaron Holtzman <aholtzma@engr.uvic.ca>
10  *
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.
15  * 
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <string.h>                                              /* memcpy() */
30
31 #include <videolan/vlc.h>
32
33 #include "audio_output.h"
34
35 #include "stream_control.h"
36 #include "input_ext-dec.h"
37
38 #include "ac3_imdct.h"
39 #include "ac3_downmix.h"
40 #include "ac3_decoder.h"
41 #include "ac3_adec.h"                                     /* ac3dec_thread_t */
42
43 #include "ac3_internal.h"
44
45 static const float cmixlev_lut[4] = { 0.707, 0.595, 0.500, 0.707 };
46 static const float smixlev_lut[4] = { 0.707, 0.500, 0.0  , 0.500 };
47
48 int _M( ac3_init )(ac3dec_t * p_ac3dec)
49 {
50     p_ac3dec->mantissa.lfsr_state = 1;          /* dither_gen initialization */
51     _M( imdct_init )(p_ac3dec->imdct) ;
52     
53     return 0;
54 }
55
56 int ac3_decode_frame (ac3dec_t * p_ac3dec, s16 * buffer)
57 {
58     int i;
59     ac3dec_thread_t * p_ac3thread = (ac3dec_thread_t *) p_ac3dec->bit_stream.p_callback_arg;
60     
61     if (parse_bsi (p_ac3dec))
62     {
63         intf_WarnMsg (3,"ac3dec warn: error during parsing");
64         parse_auxdata (p_ac3dec);
65         return 1;
66     }
67     
68     /* compute downmix parameters
69      * downmix to tow channels for now */
70     p_ac3dec->dm_par.clev = 0.0;
71     p_ac3dec->dm_par.slev = 0.0; 
72     p_ac3dec->dm_par.unit = 1.0;
73     if (p_ac3dec->bsi.acmod & 0x1)    /* have center */
74         p_ac3dec->dm_par.clev = cmixlev_lut[p_ac3dec->bsi.cmixlev];
75
76     if (p_ac3dec->bsi.acmod & 0x4)    /* have surround channels */
77         p_ac3dec->dm_par.slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
78
79     p_ac3dec->dm_par.unit /= 1.0 + p_ac3dec->dm_par.clev + p_ac3dec->dm_par.slev;
80     p_ac3dec->dm_par.clev *= p_ac3dec->dm_par.unit;
81     p_ac3dec->dm_par.slev *= p_ac3dec->dm_par.unit;
82
83     for (i = 0; i < 6; i++) {
84         /* Initialize freq/time sample storage */
85         memset(p_ac3dec->samples, 0, sizeof(float) * 256 * 
86                 (p_ac3dec->bsi.nfchans + p_ac3dec->bsi.lfeon));
87
88
89         if( p_ac3thread->p_fifo->b_die || p_ac3thread->p_fifo->b_error )
90         {        
91             return 1;
92         }
93  
94         if( parse_audblk( p_ac3dec, i ) )
95         {
96             intf_WarnMsg( 3, "ac3dec warning: error during audioblock" );
97             parse_auxdata( p_ac3dec );
98             return 1;
99         }
100
101         if( p_ac3thread->p_fifo->b_die || p_ac3thread->p_fifo->b_error )
102         {        
103             return 1;
104         }
105
106         if( exponent_unpack( p_ac3dec ) )
107         {
108             intf_WarnMsg( 3, "ac3dec warning: error during unpack" );
109             parse_auxdata( p_ac3dec );
110             return 1;
111         }
112
113         bit_allocate (p_ac3dec);
114         mantissa_unpack (p_ac3dec);
115
116         if( p_ac3thread->p_fifo->b_die || p_ac3thread->p_fifo->b_error )
117         {        
118             return 1;
119         }
120         
121         if  (p_ac3dec->bsi.acmod == 0x2)
122         {
123             rematrix (p_ac3dec);
124         }
125
126         imdct (p_ac3dec, buffer);
127
128         buffer += 2 * 256;
129     }
130
131     parse_auxdata (p_ac3dec);
132
133     return 0;
134 }
135