]> git.sesse.net Git - vlc/blob - plugins/mpeg_adec/mpeg_adec.c
653bbb55dcc5b9277141389579a109292578e3f7
[vlc] / plugins / mpeg_adec / mpeg_adec.c
1 /*****************************************************************************
2  * mpeg_adec.c: MPEG audio decoder thread
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: mpeg_adec.c,v 1.26 2002/07/23 00:39:17 sam Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Michel Lespinasse <walken@via.ecp.fr>
9  *          Samuel Hocevar <sam@via.ecp.fr>
10  *          Cyril Deguet <asmax@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                      /* malloc(), free() */
31 #include <string.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/decoder.h>
35 #include <vlc/aout.h>
36
37 #include "mpeg_adec_generic.h"
38 #include "mpeg_adec.h"
39
40 #define ADEC_FRAME_SIZE (2*1152)
41
42 /*****************************************************************************
43  * Local Prototypes
44  *****************************************************************************/
45 static int   decoder_Probe ( vlc_fourcc_t * );
46 static int   decoder_Run   ( decoder_fifo_t * );
47 static void  EndThread     ( adec_thread_t * );
48 static void  DecodeThread  ( adec_thread_t * );
49
50 /*****************************************************************************
51  * Capabilities
52  *****************************************************************************/
53 void _M( adec_getfunctions )( function_list_t * p_function_list )
54 {
55     p_function_list->functions.dec.pf_probe = decoder_Probe;
56     p_function_list->functions.dec.pf_run   = decoder_Run;
57 }
58
59 /*****************************************************************************
60  * Build configuration tree.
61  *****************************************************************************/
62 MODULE_CONFIG_START
63 MODULE_CONFIG_STOP
64
65 MODULE_INIT_START
66     SET_DESCRIPTION( _("MPEG I/II layer 1/2 audio decoder") )
67     ADD_CAPABILITY( DECODER, 50 )
68     ADD_REQUIREMENT( FPU )
69     ADD_SHORTCUT( "builtin" )
70 MODULE_INIT_STOP
71
72 MODULE_ACTIVATE_START
73     _M( adec_getfunctions )( &p_module->p_functions->dec );
74 MODULE_ACTIVATE_STOP
75
76 MODULE_DEACTIVATE_START
77 MODULE_DEACTIVATE_STOP
78
79 /*****************************************************************************
80  * decoder_Probe: probe the decoder and return score
81  *****************************************************************************/
82 static int decoder_Probe( vlc_fourcc_t *pi_type )
83 {
84     return *pi_type == VLC_FOURCC('m','p','g','a') ? 0 : -1;
85 }
86
87 /*****************************************************************************
88  * decoder_Run: initialize, go inside main loop, detroy
89  *****************************************************************************/
90 static int decoder_Run ( decoder_fifo_t * p_fifo )
91 {
92     adec_thread_t   * p_adec;
93     
94     /* Allocate the memory needed to store the thread's structure */
95     if ( (p_adec = (adec_thread_t *)malloc (sizeof(adec_thread_t))) == NULL ) 
96     {
97         msg_Err( p_fifo, "out of memory" );
98         DecoderError( p_fifo );
99         return 0;
100     }
101     
102     /*
103      * Initialize the thread properties
104      */
105     p_adec->p_fifo = p_fifo;
106
107     /* 
108      * Initilize the banks
109      */
110     p_adec->bank_0.actual = p_adec->bank_0.v1;
111     p_adec->bank_0.pos = 0;
112     p_adec->bank_1.actual = p_adec->bank_1.v1;
113     p_adec->bank_1.pos = 0;
114     
115     /*
116      * Initialize bit stream 
117      */
118     InitBitstream( &p_adec->bit_stream, p_adec->p_fifo, NULL, NULL );
119
120     /* We do not create the audio output fifo now, but
121        it will be created when the first frame is received */
122     p_adec->p_aout_fifo = NULL;
123
124     p_adec->i_sync = 0;
125
126     /* Audio decoder thread's main loop */
127     while( (!p_adec->p_fifo->b_die) && (!p_adec->p_fifo->b_error) )
128     {
129         DecodeThread( p_adec );
130     }
131     
132     /* If b_error is set, the audio decoder thread enters the error loop */
133     if( p_adec->p_fifo->b_error ) 
134     {
135         DecoderError( p_adec->p_fifo );
136     }
137
138     /* End of the audio decoder thread */
139     EndThread( p_adec );
140
141     return( 0 );
142 }
143
144 /*
145  * Following functions are local to this module
146  */
147
148 /*****************************************************************************
149  * DecodeThread: decodes a mpeg frame
150  *****************************************************************************/
151 static void DecodeThread( adec_thread_t * p_adec )
152 {
153     s16 *p_buffer;
154     adec_sync_info_t sync_info;
155
156     if( ! adec_SyncFrame (p_adec, &sync_info) )
157     {
158         
159         /* TODO: check if audio type has changed */
160         
161         /* Create the output fifo if it doesn't exist yet */
162         if( p_adec->p_aout_fifo == NULL )
163         {
164             int i_channels;
165             
166             if( !config_GetInt( p_adec->p_fifo, "mono" ) )
167             {
168                 msg_Dbg( p_adec->p_fifo, "setting stereo output" );
169                 i_channels = 2;
170             }
171             else if( sync_info.b_stereo )
172             {
173                 i_channels = 2;
174             }
175             else
176             {
177                 i_channels = 1;
178             }
179             p_adec->p_aout_fifo =
180                aout_CreateFifo( p_adec->p_fifo, AOUT_FIFO_PCM, i_channels,
181                                 sync_info.sample_rate, ADEC_FRAME_SIZE, NULL );
182             if( p_adec->p_aout_fifo == NULL)
183             {
184                 msg_Err( p_adec->p_fifo, "failed to create aout fifo" );
185                 p_adec->p_fifo->b_error = 1;
186                 return;
187             }
188         }
189
190         p_adec->i_sync = 1;
191
192         p_buffer = ((s16 *)p_adec->p_aout_fifo->buffer)
193                     + (p_adec->p_aout_fifo->i_end_frame * ADEC_FRAME_SIZE);
194
195         CurrentPTS( &p_adec->bit_stream,
196             &p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->i_end_frame],
197             NULL );
198         if( !p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->i_end_frame] )
199         {
200             p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->i_end_frame] =
201                 LAST_MDATE;
202         }
203
204         if( adec_DecodeFrame (p_adec, p_buffer) )
205         {
206             /* Ouch, failed decoding... We'll have to resync */
207             p_adec->i_sync = 0;
208         }
209         else
210         {
211             vlc_mutex_lock (&p_adec->p_aout_fifo->data_lock);
212             p_adec->p_aout_fifo->i_end_frame =
213                 (p_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
214             vlc_cond_signal (&p_adec->p_aout_fifo->data_wait);
215             vlc_mutex_unlock (&p_adec->p_aout_fifo->data_lock);
216         }
217     }
218 }
219
220 /*****************************************************************************
221  * EndThread : audio decoder thread destruction
222  *****************************************************************************
223  * This function is called when the thread ends after a sucessful
224  * initialization.
225  *****************************************************************************/
226 static void EndThread ( adec_thread_t *p_adec )
227 {
228     /* If the audio output fifo was created, we destroy it */
229     if ( p_adec->p_aout_fifo != NULL ) 
230     {
231         aout_DestroyFifo ( p_adec->p_aout_fifo );
232
233         /* Make sure the output thread leaves the NextFrame() function */
234         vlc_mutex_lock (&(p_adec->p_aout_fifo->data_lock));
235         vlc_cond_signal (&(p_adec->p_aout_fifo->data_wait));
236         vlc_mutex_unlock (&(p_adec->p_aout_fifo->data_lock));
237     }
238     /* Destroy descriptor */
239     free( p_adec );
240 }
241