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