1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*****************************************************************************
* mpeg_adec.c: MPEG audio decoder thread
*****************************************************************************
* Copyright (C) 1999, 2000 VideoLAN
* $Id: mpeg_adec.c,v 1.2 2001/11/15 17:39:12 sam Exp $
*
* Authors: Michel Kaempf <maxx@via.ecp.fr>
* Michel Lespinasse <walken@via.ecp.fr>
* Samuel Hocevar <sam@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#define MODULE_NAME mpeg_adec
#include "modules_inner.h"
/*****************************************************************************
* Preamble
*****************************************************************************/
#include "defs.h"
#include <stdlib.h> /* malloc(), free() */
#include "config.h"
#include "common.h" /* boolean_t, byte_t */
#include "threads.h"
#include "mtime.h"
#include "intf_msg.h"
#include "audio_output.h" /* aout_fifo_t (for audio_decoder.h) */
#include "modules.h"
#include "modules_export.h"
#include "stream_control.h"
#include "input_ext-dec.h"
#include "mpeg_adec_generic.h"
#include "mpeg_adec.h"
#define ADEC_FRAME_SIZE (2*1152)
/*****************************************************************************
* Local Prototypes
*****************************************************************************/
static int adec_Probe( probedata_t * );
static int adec_RunThread ( decoder_config_t * );
static void adec_EndThread ( adec_thread_t * );
static void adec_ErrorThread ( adec_thread_t * );
static void adec_Decode( adec_thread_t * );
/*****************************************************************************
* Capabilities
*****************************************************************************/
void _M( adec_getfunctions )( function_list_t * p_function_list )
{
p_function_list->pf_probe = adec_Probe;
p_function_list->functions.dec.pf_RunThread = adec_RunThread;
}
/*****************************************************************************
* Build configuration tree.
*****************************************************************************/
MODULE_CONFIG_START
ADD_WINDOW( "Configuration for mpeg audio decoder module" )
ADD_COMMENT( "Nothing to configure" )
MODULE_CONFIG_STOP
MODULE_INIT_START
p_module->i_capabilities = MODULE_CAPABILITY_DEC;
p_module->psz_longname = "Mpeg I layer 1/2 audio decoder";
MODULE_INIT_STOP
MODULE_ACTIVATE_START
_M( adec_getfunctions )( &p_module->p_functions->dec );
MODULE_ACTIVATE_STOP
MODULE_DEACTIVATE_START
MODULE_DEACTIVATE_STOP
/*****************************************************************************
* adec_Probe: probe the decoder and return score
*****************************************************************************/
static int adec_Probe( probedata_t *p_data )
{
if( p_data->i_type == MPEG1_AUDIO_ES || p_data->i_type == MPEG2_AUDIO_ES )
return( 100 );
else
return( 0 );
}
/*****************************************************************************
* adec_RunThread: initialize, go inside main loop, detroy
*****************************************************************************/
static int adec_RunThread ( decoder_config_t * p_config )
{
adec_thread_t * p_adec;
intf_DbgMsg("mpeg_adec debug: thread launched, initializing.");
/* Allocate the memory needed to store the thread's structure */
if ( (p_adec = (adec_thread_t *)malloc (sizeof(adec_thread_t))) == NULL )
{
intf_ErrMsg ( "adec error: not enough memory for"
" adec_CreateThread() to create the new thread" );
return 0;
}
/*
* Initialize the thread properties
*/
p_adec->p_config = p_config;
p_adec->p_fifo = p_config->p_decoder_fifo;
/*
* Initilize the banks
*/
p_adec->bank_0.actual = p_adec->bank_0.v1;
p_adec->bank_0.pos = 0;
p_adec->bank_1.actual = p_adec->bank_1.v1;
p_adec->bank_1.pos = 0;
/*
* Initialize bit stream
*/
p_adec->p_config->pf_init_bit_stream( &p_adec->bit_stream,
p_adec->p_config->p_decoder_fifo, NULL, NULL );
/* Create the audio output fifo */
p_adec->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
ADEC_FRAME_SIZE, NULL );
if ( p_adec->p_aout_fifo == NULL )
{
intf_ErrMsg("mpeg_adec error: cannot create audio output fifo");
return -1;
}
intf_DbgMsg("mpeg_adec debug: thread initialized, decoding begins.");
p_adec->i_sync = 0;
/* Audio decoder thread's main loop */
while( (!p_adec->p_fifo->b_die) && (!p_adec->p_fifo->b_error) )
{
adec_Decode( p_adec );
}
/* If b_error is set, the audio decoder thread enters the error loop */
if( p_adec->p_fifo->b_error )
{
adec_ErrorThread( p_adec );
}
/* End of the audio decoder thread */
adec_EndThread( p_adec );
return( 0 );
}
/*
* Following finctions are local to this module
*/
/*****************************************************************************
* adec_Decode: decodes a mpeg frame
*****************************************************************************/
static void adec_Decode( adec_thread_t * p_adec )
{
s16 * buffer;
adec_sync_info_t sync_info;
if( ! adec_SyncFrame (p_adec, &sync_info) )
{
p_adec->i_sync = 1;
p_adec->p_aout_fifo->l_rate = sync_info.sample_rate;
buffer = ((s16 *)p_adec->p_aout_fifo->buffer)
+ (p_adec->p_aout_fifo->l_end_frame * ADEC_FRAME_SIZE);
if( DECODER_FIFO_START( *p_adec->p_fifo)->i_pts )
{
p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] =
DECODER_FIFO_START( *p_adec->p_fifo )->i_pts;
DECODER_FIFO_START(*p_adec->p_fifo)->i_pts = 0;
}
else
{
p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] =
LAST_MDATE;
}
if( adec_DecodeFrame (p_adec, buffer) )
{
/* Ouch, failed decoding... We'll have to resync */
p_adec->i_sync = 0;
}
else
{
vlc_mutex_lock (&p_adec->p_aout_fifo->data_lock);
p_adec->p_aout_fifo->l_end_frame =
(p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
vlc_cond_signal (&p_adec->p_aout_fifo->data_wait);
vlc_mutex_unlock (&p_adec->p_aout_fifo->data_lock);
}
}
}
/*****************************************************************************
* adec_ErrorThread : audio decoder's RunThread() error loop
*****************************************************************************
* This function is called when an error occured during thread main's loop. The
* thread can still receive feed, but must be ready to terminate as soon as
* possible.
*****************************************************************************/
static void adec_ErrorThread ( adec_thread_t *p_adec )
{
/* We take the lock, because we are going to read/write the start/end
* indexes of the decoder fifo */
vlc_mutex_lock ( &p_adec->p_fifo->data_lock );
/* Wait until a `die' order is sent */
while ( !p_adec->p_fifo->b_die )
{
/* Trash all received PES packets */
while ( !DECODER_FIFO_ISEMPTY(*p_adec->p_fifo) )
{
p_adec->p_fifo->pf_delete_pes ( p_adec->p_fifo->p_packets_mgt,
DECODER_FIFO_START(*p_adec->p_fifo) );
DECODER_FIFO_INCSTART ( *p_adec->p_fifo );
}
/* Waiting for the input thread to put new PES packets in the fifo */
vlc_cond_wait ( &p_adec->p_fifo->data_wait, &p_adec->p_fifo->data_lock );
}
/* We can release the lock before leaving */
vlc_mutex_unlock ( &p_adec->p_fifo->data_lock );
}
/*****************************************************************************
* adec_EndThread : audio decoder thread destruction
*****************************************************************************
* This function is called when the thread ends after a sucessful
* initialization.
*****************************************************************************/
static void adec_EndThread ( adec_thread_t *p_adec )
{
intf_DbgMsg ( "adec debug: destroying audio decoder thread %p", p_adec );
/* If the audio output fifo was created, we destroy it */
if ( p_adec->p_aout_fifo != NULL )
{
aout_DestroyFifo ( p_adec->p_aout_fifo );
/* Make sure the output thread leaves the NextFrame() function */
vlc_mutex_lock (&(p_adec->p_aout_fifo->data_lock));
vlc_cond_signal (&(p_adec->p_aout_fifo->data_wait));
vlc_mutex_unlock (&(p_adec->p_aout_fifo->data_lock));
}
/* Destroy descriptor */
free( p_adec );
intf_DbgMsg ("adec debug: audio decoder thread %p destroyed", p_adec);
}