]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder.c
c3fd6d48e2c8dd007cc5e870e6cbc92211059d87
[vlc] / src / ac3_decoder / ac3_decoder.c
1 /*****************************************************************************
2  * ac3_decoder.c: core ac3 decoder
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: ac3_decoder.c,v 1.30 2001/04/30 21:04:20 reno 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 #include "defs.h"
27
28 #include "config.h"
29 #include "common.h"
30 #include "threads.h"
31 #include "mtime.h"
32
33 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
34
35 #include "stream_control.h"
36 #include "input_ext-dec.h"
37 #include "audio_output.h"
38
39
40 #include "ac3_decoder.h"
41 #include "ac3_decoder_thread.h"
42
43 #include "ac3_internal.h"
44
45 #include <stdio.h>
46
47 void imdct_init (imdct_t * p_imdct);
48 void downmix_init (downmix_t * p_downmix);
49
50 static float cmixlev_lut[4] = { 0.707, 0.595, 0.500, 0.707 };
51 static float smixlev_lut[4] = { 0.707, 0.500, 0.0  , 0.500 };
52
53 int ac3_init (ac3dec_t * p_ac3dec)
54 {
55 //    p_ac3dec->bit_stream.buffer = 0;
56 //    p_ac3dec->bit_stream.i_available = 0;
57     p_ac3dec->mantissa.lfsr_state = 1;       /* dither_gen initialization */
58     imdct_init(&p_ac3dec->imdct);
59     downmix_init(&p_ac3dec->downmix);
60     
61     return 0;
62 }
63
64 int ac3_decode_frame (ac3dec_t * p_ac3dec, s16 * buffer)
65 {
66     int i;
67     ac3dec_thread_t * p_ac3dec_t = (ac3dec_thread_t *) p_ac3dec->bit_stream.p_callback_arg;
68     
69     if (parse_bsi (p_ac3dec))
70     {
71         intf_WarnMsg (1,"Error during ac3parsing");
72         parse_auxdata (p_ac3dec);
73         return 1;
74     }
75     
76         /* compute downmix parameters
77          * downmix to tow channels for now */
78         p_ac3dec->dm_par.clev = 0.0;
79     p_ac3dec->dm_par.slev = 0.0; 
80     p_ac3dec->dm_par.unit = 1.0;
81         if (p_ac3dec->bsi.acmod & 0x1)  /* have center */
82             p_ac3dec->dm_par.clev = cmixlev_lut[p_ac3dec->bsi.cmixlev];
83
84         if (p_ac3dec->bsi.acmod & 0x4)  /* have surround channels */
85                 p_ac3dec->dm_par.slev = smixlev_lut[p_ac3dec->bsi.surmixlev];
86
87     p_ac3dec->dm_par.unit /= 1.0 + p_ac3dec->dm_par.clev + p_ac3dec->dm_par.slev;
88         p_ac3dec->dm_par.clev *= p_ac3dec->dm_par.unit;
89         p_ac3dec->dm_par.slev *= p_ac3dec->dm_par.unit;
90
91     for (i = 0; i < 6; i++) {
92         /* Initialize freq/time sample storage */
93         memset(p_ac3dec->samples, 0, sizeof(float) * 256 * 
94                 (p_ac3dec->bsi.nfchans + p_ac3dec->bsi.lfeon));
95
96
97         if ((p_ac3dec_t->p_fifo->b_die) && (p_ac3dec_t->p_fifo->b_error))
98         {        
99             return 1;
100         }
101  
102         if (parse_audblk (p_ac3dec, i))
103         {
104             intf_WarnMsg (1,"Error during ac3audioblock");
105             parse_auxdata (p_ac3dec);
106             return 1;
107         }
108
109         if ((p_ac3dec_t->p_fifo->b_die) && (p_ac3dec_t->p_fifo->b_error))
110         {        
111             return 1;
112         }
113
114         if (exponent_unpack (p_ac3dec))
115         {
116             intf_WarnMsg (1,"Error during ac3unpack");
117             parse_auxdata (p_ac3dec);
118             return 1;
119         }
120         bit_allocate (p_ac3dec);
121         mantissa_unpack (p_ac3dec);
122
123         if ((p_ac3dec_t->p_fifo->b_die) && (p_ac3dec_t->p_fifo->b_error))
124         {        
125             return 1;
126         }
127         
128         if  (p_ac3dec->bsi.acmod == 0x2)
129             rematrix (p_ac3dec);
130         imdct (p_ac3dec, buffer);
131
132         buffer += 2*256;
133     }
134
135     parse_auxdata (p_ac3dec);
136
137     return 0;
138 }