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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*****************************************************************************
* ac3_spdif.c: ac3 pass-through to external decoder with enabled soundcard
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: ac3_spdif.c,v 1.9 2001/12/30 05:38:44 sam Exp $
*
* Authors: Stphane Borel <stef@via.ecp.fr>
* Juha Yrjola <jyrjola@cc.hut.fi>
* German Gomez Garcia <german@piraos.com>
*
* 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 ac3_spdif
#include "modules_inner.h"
/*****************************************************************************
* Preamble
*****************************************************************************/
#include "defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* memcpy() */
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "common.h"
#include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
#include "threads.h"
#include "mtime.h"
#include "audio_output.h"
#include "stream_control.h"
#include "input_ext-dec.h"
#include "ac3_spdif.h"
#include "ac3_iec958.h"
#include "modules.h"
#include "modules_export.h"
#define FRAME_NB 8
/****************************************************************************
* Local Prototypes
****************************************************************************/
static int decoder_Probe ( probedata_t * );
static int decoder_Run ( decoder_config_t * );
static int InitThread ( ac3_spdif_thread_t * );
static void EndThread ( ac3_spdif_thread_t * );
static void BitstreamCallback ( bit_stream_t *, boolean_t );
/*****************************************************************************
* Capabilities
*****************************************************************************/
void _M( adec_getfunctions )( function_list_t * p_function_list )
{
p_function_list->pf_probe = decoder_Probe;
p_function_list->functions.dec.pf_run = decoder_Run;
}
/*****************************************************************************
* Build configuration tree.
*****************************************************************************/
MODULE_CONFIG_START
ADD_WINDOW( "Configuration for ac3 spdif decoder module" )
ADD_COMMENT( "Nothing to configure" )
MODULE_CONFIG_STOP
MODULE_INIT_START
p_module->i_capabilities = MODULE_CAPABILITY_DEC;
p_module->psz_longname = "Ac3 SPDIF decoder for AC3 pass-through";
MODULE_INIT_STOP
MODULE_ACTIVATE_START
_M( adec_getfunctions )( &p_module->p_functions->dec );
MODULE_ACTIVATE_STOP
MODULE_DEACTIVATE_START
MODULE_DEACTIVATE_STOP
/*****************************************************************************
* decoder_Probe: probe the decoder and return score
*****************************************************************************
* Tries to launch a decoder and return score so that the interface is able
* to chose.
*****************************************************************************/
static int decoder_Probe( probedata_t *p_data )
{
if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) &&
p_data->i_type == AC3_AUDIO_ES )
return( 100 );
else
return( 0 );
}
/****************************************************************************
* decoder_Run: the whole thing
****************************************************************************
* This function is called just after the thread is launched.
****************************************************************************/
static int decoder_Run( decoder_config_t * p_config )
{
ac3_spdif_thread_t * p_spdif;
mtime_t i_frame_time;
boolean_t b_sync;
/* PTS of the current frame */
mtime_t i_current_pts = 0;
intf_DbgMsg( "spdif debug: ac3_spdif thread created, initializing." );
/* Allocate the memory needed to store the thread's structure */
p_spdif = malloc( sizeof(ac3_spdif_thread_t) );
if( p_spdif == NULL )
{
intf_ErrMsg ( "spdif error: not enough memory "
"for spdif_CreateThread() to create the new thread");
DecoderError( p_config->p_decoder_fifo );
return( -1 );
}
p_spdif->p_config = p_config;
if (InitThread( p_spdif ) )
{
intf_ErrMsg( "spdif error: could not initialize thread" );
DecoderError( p_config->p_decoder_fifo );
free( p_spdif );
return( -1 );
}
/* Compute the theorical duration of an ac3 frame */
i_frame_time = 1000000 * AC3_FRAME_SIZE /
p_spdif->ac3_info.i_sample_rate;
intf_DbgMsg( "spdif debug: ac3_spdif thread (%p) initialized", p_spdif );
while( !p_spdif->p_fifo->b_die && !p_spdif->p_fifo->b_error )
{
/* Handle the dates */
if( p_spdif->i_real_pts )
{
mtime_t i_delta = p_spdif->i_real_pts - i_current_pts -
i_frame_time;
if( i_delta > i_frame_time || i_delta < -i_frame_time )
{
intf_WarnMsg( 3, "spdif warning: date discontinuity (%d)",
i_delta );
}
i_current_pts = p_spdif->i_real_pts;
p_spdif->i_real_pts = 0;
}
else
{
i_current_pts += i_frame_time;
}
/* if we're late here the output won't have to play the frame */
if( i_current_pts > mdate() )
{
p_spdif->p_aout_fifo->date[p_spdif->p_aout_fifo->l_end_frame] =
i_current_pts;
/* Write in the first free packet of aout fifo */
p_spdif->p_iec = ((u8*)(p_spdif->p_aout_fifo->buffer) +
(p_spdif->p_aout_fifo->l_end_frame * SPDIF_FRAME_SIZE ));
/* Build burst to be sent to hardware decoder */
ac3_iec958_build_burst( p_spdif );
vlc_mutex_lock (&p_spdif->p_aout_fifo->data_lock);
p_spdif->p_aout_fifo->l_end_frame =
(p_spdif->p_aout_fifo->l_end_frame + 1 ) & AOUT_FIFO_SIZE;
vlc_mutex_unlock (&p_spdif->p_aout_fifo->data_lock);
}
/* Find syncword again in case of stream discontinuity */
/* Here we have p_spdif->i_pts == 0
* Therefore a non-zero value after a call to GetBits() means the PES
* has changed. */
b_sync = 0;
while( !b_sync )
{
while( GetBits( &p_spdif->bit_stream, 8 ) != 0x0b );
p_spdif->i_real_pts = p_spdif->i_pts;
p_spdif->i_pts = 0;
b_sync = ( ShowBits( &p_spdif->bit_stream, 8 ) == 0x77 );
}
RemoveBits( &p_spdif->bit_stream, 8 );
/* Read data from bitstream */
GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + 2,
p_spdif->ac3_info.i_frame_size - 2 );
}
/* If b_error is set, the ac3 spdif thread enters the error loop */
if( p_spdif->p_fifo->b_error )
{
DecoderError( p_spdif->p_fifo );
}
/* End of the ac3 decoder thread */
EndThread( p_spdif );
return( 0 );
}
/****************************************************************************
* InitThread: initialize thread data and create output fifo
****************************************************************************/
static int InitThread( ac3_spdif_thread_t * p_spdif )
{
boolean_t b_sync = 0;
/* Temporary buffer to store ac3 frames to be transformed */
p_spdif->p_ac3 = malloc( SPDIF_FRAME_SIZE );
if( p_spdif->p_ac3 == NULL )
{
free( p_spdif->p_ac3 );
return( -1 );
}
/*
* Initialize the thread properties
*/
p_spdif->p_fifo = p_spdif->p_config->p_decoder_fifo;
p_spdif->p_config->pf_init_bit_stream(
&p_spdif->bit_stream,
p_spdif->p_config->p_decoder_fifo,
BitstreamCallback, (void*)p_spdif );
/* Creating the audio output fifo */
p_spdif->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_SPDIF_FIFO, 1, 48000, 0,
SPDIF_FRAME_SIZE, NULL );
if( p_spdif->p_aout_fifo == NULL )
{
return( -1 );
}
intf_WarnMsg( 3, "spdif: aout fifo #%d created",
p_spdif->p_aout_fifo->i_fifo );
/* Sync word */
p_spdif->p_ac3[0] = 0x0b;
p_spdif->p_ac3[1] = 0x77;
/* Find syncword */
while( !b_sync )
{
while( GetBits( &p_spdif->bit_stream, 8 ) != 0x0b );
p_spdif->i_real_pts = p_spdif->i_pts;
p_spdif->i_pts = 0;
b_sync = ( ShowBits( &p_spdif->bit_stream, 8 ) == 0x77 );
}
RemoveBits( &p_spdif->bit_stream, 8 );
/* Check stream properties */
if( ac3_iec958_parse_syncinfo( p_spdif ) < 0 )
{
intf_ErrMsg( "spdif error: stream not valid");
aout_DestroyFifo( p_spdif->p_aout_fifo );
return( -1 );
}
/* Check that we can handle the rate
* FIXME: we should check that we have the same rate for all fifos
* but all rates should be supported by the decoder (32, 44.1, 48) */
if( p_spdif->ac3_info.i_sample_rate != 48000 )
{
intf_ErrMsg( "spdif error: Only 48000 Hz streams supported");
aout_DestroyFifo( p_spdif->p_aout_fifo );
return -1;
}
p_spdif->p_aout_fifo->l_rate = p_spdif->ac3_info.i_sample_rate;
GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + sizeof(sync_frame_t),
p_spdif->ac3_info.i_frame_size - sizeof(sync_frame_t) );
return( 0 );
}
/*****************************************************************************
* EndThread : ac3 spdif thread destruction
*****************************************************************************/
static void EndThread( ac3_spdif_thread_t * p_spdif )
{
intf_DbgMsg( "spdif debug: destroying thread %p", p_spdif );
/* If the audio output fifo was created, we destroy it */
if( p_spdif->p_aout_fifo != NULL )
{
aout_DestroyFifo( p_spdif->p_aout_fifo );
/* Make sure the output thread leaves the NextFrame() function */
vlc_mutex_lock( &(p_spdif->p_aout_fifo->data_lock ) );
vlc_cond_signal( &(p_spdif->p_aout_fifo->data_wait ) );
vlc_mutex_unlock( &(p_spdif->p_aout_fifo->data_lock ) );
}
/* Destroy descriptor */
free( p_spdif->p_ac3 );
free( p_spdif );
intf_DbgMsg ("spdif debug: thread %p destroyed", p_spdif );
}
/*****************************************************************************
* BitstreamCallback: Import parameters from the new data/PES packet
*****************************************************************************
* This function is called by input's NextDataPacket.
*****************************************************************************/
static void BitstreamCallback ( bit_stream_t * p_bit_stream,
boolean_t b_new_pes)
{
ac3_spdif_thread_t * p_spdif;
if( b_new_pes )
{
p_spdif = (ac3_spdif_thread_t *)p_bit_stream->p_callback_arg;
p_bit_stream->p_byte += 3;
p_spdif->i_pts =
p_bit_stream->p_decoder_fifo->p_first->i_pts;
p_bit_stream->p_decoder_fifo->p_first->i_pts = 0;
}
}