1 /*****************************************************************************
2 * ac3_spdif.c: ac3 pass-through to external decoder with enabled soundcard
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: ac3_spdif.c,v 1.28 2002/06/01 18:04:48 sam Exp $
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Juha Yrjola <jyrjola@cc.hut.fi>
9 * German Gomez Garcia <german@piraos.com>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <string.h> /* memcpy() */
35 #include <vlc/decoder.h>
42 #include "ac3_spdif.h"
44 /****************************************************************************
46 ****************************************************************************/
47 static int decoder_Probe ( u8 * );
48 static int decoder_Run ( decoder_fifo_t * );
49 static int InitThread ( ac3_spdif_thread_t * );
50 static void EndThread ( ac3_spdif_thread_t * );
51 static void BitstreamCallback ( bit_stream_t *, vlc_bool_t );
53 int ac3_parse_syncinfo ( struct ac3_spdif_thread_s * );
55 /****************************************************************************
56 * Local structures and tables
57 ****************************************************************************/
58 static const frame_size_t p_frame_size_code[64] =
60 { 32 ,{64 ,69 ,96 } },
61 { 32 ,{64 ,70 ,96 } },
62 { 40 ,{80 ,87 ,120 } },
63 { 40 ,{80 ,88 ,120 } },
64 { 48 ,{96 ,104 ,144 } },
65 { 48 ,{96 ,105 ,144 } },
66 { 56 ,{112 ,121 ,168 } },
67 { 56 ,{112 ,122 ,168 } },
68 { 64 ,{128 ,139 ,192 } },
69 { 64 ,{128 ,140 ,192 } },
70 { 80 ,{160 ,174 ,240 } },
71 { 80 ,{160 ,175 ,240 } },
72 { 96 ,{192 ,208 ,288 } },
73 { 96 ,{192 ,209 ,288 } },
74 { 112 ,{224 ,243 ,336 } },
75 { 112 ,{224 ,244 ,336 } },
76 { 128 ,{256 ,278 ,384 } },
77 { 128 ,{256 ,279 ,384 } },
78 { 160 ,{320 ,348 ,480 } },
79 { 160 ,{320 ,349 ,480 } },
80 { 192 ,{384 ,417 ,576 } },
81 { 192 ,{384 ,418 ,576 } },
82 { 224 ,{448 ,487 ,672 } },
83 { 224 ,{448 ,488 ,672 } },
84 { 256 ,{512 ,557 ,768 } },
85 { 256 ,{512 ,558 ,768 } },
86 { 320 ,{640 ,696 ,960 } },
87 { 320 ,{640 ,697 ,960 } },
88 { 384 ,{768 ,835 ,1152 } },
89 { 384 ,{768 ,836 ,1152 } },
90 { 448 ,{896 ,975 ,1344 } },
91 { 448 ,{896 ,976 ,1344 } },
92 { 512 ,{1024 ,1114 ,1536 } },
93 { 512 ,{1024 ,1115 ,1536 } },
94 { 576 ,{1152 ,1253 ,1728 } },
95 { 576 ,{1152 ,1254 ,1728 } },
96 { 640 ,{1280 ,1393 ,1920 } },
97 { 640 ,{1280 ,1394 ,1920 } }
100 /*****************************************************************************
102 *****************************************************************************/
103 void _M( adec_getfunctions )( function_list_t * p_function_list )
105 p_function_list->functions.dec.pf_probe = decoder_Probe;
106 p_function_list->functions.dec.pf_run = decoder_Run;
109 /*****************************************************************************
110 * Build configuration tree.
111 *****************************************************************************/
116 SET_DESCRIPTION( _("SPDIF pass-through AC3 decoder") )
117 ADD_CAPABILITY( DECODER, 0 )
118 ADD_SHORTCUT( "pass_through" )
119 ADD_SHORTCUT( "pass" )
122 MODULE_ACTIVATE_START
123 _M( adec_getfunctions )( &p_module->p_functions->dec );
126 MODULE_DEACTIVATE_START
127 MODULE_DEACTIVATE_STOP
129 /*****************************************************************************
130 * decoder_Probe: probe the decoder and return score
131 *****************************************************************************
132 * Tries to launch a decoder and return score so that the interface is able
134 *****************************************************************************/
135 static int decoder_Probe( u8 *pi_type )
137 return( *pi_type == AC3_AUDIO_ES ) ? 0 : -1;
141 /****************************************************************************
142 * decoder_Run: the whole thing
143 ****************************************************************************
144 * This function is called just after the thread is launched.
145 ****************************************************************************/
146 static int decoder_Run( decoder_fifo_t * p_fifo )
148 ac3_spdif_thread_t * p_spdif;
149 mtime_t i_frame_time;
151 /* PTS of the current frame */
152 mtime_t i_current_pts = 0;
155 /* Allocate the memory needed to store the thread's structure */
156 p_spdif = malloc( sizeof(ac3_spdif_thread_t) );
158 if( p_spdif == NULL )
160 msg_Err( p_fifo, "out of memory" );
161 DecoderError( p_fifo );
165 p_spdif->p_fifo = p_fifo;
167 if (InitThread( p_spdif ) )
169 msg_Err( p_fifo, "could not initialize thread" );
170 DecoderError( p_fifo );
175 /* Compute the theorical duration of an ac3 frame */
176 i_frame_time = 1000000 * AC3_FRAME_SIZE /
177 p_spdif->ac3_info.i_sample_rate;
178 i_length = p_spdif->ac3_info.i_frame_size;
180 while( !p_spdif->p_fifo->b_die && !p_spdif->p_fifo->b_error )
182 p_spdif->p_ac3[0] = 0x0b;
183 p_spdif->p_ac3[1] = 0x77;
185 /* Handle the dates */
186 if( p_spdif->i_real_pts )
188 mtime_t i_delta = p_spdif->i_real_pts - i_current_pts -
190 if( i_delta > i_frame_time || i_delta < -i_frame_time )
193 "date discontinuity (%d)", i_delta );
195 i_current_pts = p_spdif->i_real_pts;
196 p_spdif->i_real_pts = 0;
200 i_current_pts += i_frame_time;
203 vlc_mutex_lock (&p_spdif->p_aout_fifo->data_lock);
205 p_spdif->p_aout_fifo->date[p_spdif->p_aout_fifo->i_end_frame] =
208 p_spdif->p_aout_fifo->i_end_frame =
209 (p_spdif->p_aout_fifo->i_end_frame + 1 ) & AOUT_FIFO_SIZE;
211 p_spdif->p_ac3 = ((u8*)(p_spdif->p_aout_fifo->buffer)) +
212 (p_spdif->p_aout_fifo->i_end_frame * i_length );
214 vlc_mutex_unlock (&p_spdif->p_aout_fifo->data_lock);
216 /* Find syncword again in case of stream discontinuity */
217 /* Here we have p_spdif->i_pts == 0
218 * Therefore a non-zero value after a call to GetBits() means the PES
221 while( !p_spdif->p_fifo->b_die
222 && !p_spdif->p_fifo->b_error
225 while( !p_spdif->p_fifo->b_die
226 && !p_spdif->p_fifo->b_error
227 && GetBits( &p_spdif->bit_stream, 8 ) != 0x0b );
228 p_spdif->i_real_pts = p_spdif->i_pts;
230 b_sync = ( ShowBits( &p_spdif->bit_stream, 8 ) == 0x77 );
232 RemoveBits( &p_spdif->bit_stream, 8 );
234 /* Read data from bitstream */
235 GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + 2, i_length - 2 );
238 /* If b_error is set, the ac3 spdif thread enters the error loop */
239 if( p_spdif->p_fifo->b_error )
241 DecoderError( p_spdif->p_fifo );
244 /* End of the ac3 decoder thread */
245 EndThread( p_spdif );
250 /****************************************************************************
251 * InitThread: initialize thread data and create output fifo
252 ****************************************************************************/
253 static int InitThread( ac3_spdif_thread_t * p_spdif )
255 vlc_bool_t b_sync = 0;
257 /* Temporary buffer to store first ac3 frame */
258 p_spdif->p_ac3 = malloc( SPDIF_FRAME_SIZE );
260 if( p_spdif->p_ac3 == NULL )
262 free( p_spdif->p_ac3 );
267 * Initialize the thread properties
269 p_spdif->p_fifo = p_spdif->p_fifo;
271 InitBitstream( &p_spdif->bit_stream, p_spdif->p_fifo,
272 BitstreamCallback, (void*)p_spdif );
275 while( !p_spdif->p_fifo->b_die
276 && !p_spdif->p_fifo->b_error
279 while( !p_spdif->p_fifo->b_die
280 && !p_spdif->p_fifo->b_error
281 && GetBits( &p_spdif->bit_stream, 8 ) != 0x0b );
282 p_spdif->i_real_pts = p_spdif->i_pts;
284 b_sync = ( ShowBits( &p_spdif->bit_stream, 8 ) == 0x77 );
287 if( p_spdif->p_fifo->b_die || p_spdif->p_fifo->b_error )
292 RemoveBits( &p_spdif->bit_stream, 8 );
294 /* Check stream properties */
295 if( ac3_parse_syncinfo( p_spdif ) < 0 )
297 msg_Err( p_spdif->p_fifo, "stream not valid" );
302 /* Check that we can handle the rate
303 * FIXME: we should check that we have the same rate for all fifos
304 * but all rates should be supported by the decoder (32, 44.1, 48) */
305 if( p_spdif->ac3_info.i_sample_rate != 48000 )
307 msg_Err( p_spdif->p_fifo,
308 "only 48000 Hz streams tested, expect weird things!" );
311 /* The audio output need to be ready for an ac3 stream */
312 p_spdif->i_previous_format = config_GetInt( p_spdif->p_fifo,
314 config_PutInt( p_spdif->p_fifo, "audio-format", 8 );
316 /* Creating the audio output fifo */
317 p_spdif->p_aout_fifo =
318 aout_CreateFifo( p_spdif->p_fifo, AOUT_FIFO_SPDIF,
319 1, p_spdif->ac3_info.i_sample_rate,
320 p_spdif->ac3_info.i_frame_size, NULL );
322 if( p_spdif->p_aout_fifo == NULL )
327 msg_Dbg( p_spdif->p_fifo, "aout fifo #%d created",
328 p_spdif->p_aout_fifo->i_fifo );
330 /* Put read data into fifo */
331 memcpy( (u8*)(p_spdif->p_aout_fifo->buffer) +
332 (p_spdif->p_aout_fifo->i_end_frame *
333 p_spdif->ac3_info.i_frame_size ),
334 p_spdif->p_ac3, sizeof(sync_frame_t) );
335 free( p_spdif->p_ac3 );
336 p_spdif->p_ac3 = ((u8*)(p_spdif->p_aout_fifo->buffer) +
337 (p_spdif->p_aout_fifo->i_end_frame *
338 p_spdif->ac3_info.i_frame_size ));
340 GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + sizeof(sync_frame_t),
341 p_spdif->ac3_info.i_frame_size - sizeof(sync_frame_t) );
346 /*****************************************************************************
347 * EndThread : ac3 spdif thread destruction
348 *****************************************************************************/
349 static void EndThread( ac3_spdif_thread_t * p_spdif )
351 /* If the audio output fifo was created, we destroy it */
352 if( p_spdif->p_aout_fifo != NULL )
354 aout_DestroyFifo( p_spdif->p_aout_fifo );
356 /* Make sure the output thread leaves the NextFrame() function */
357 vlc_mutex_lock( &(p_spdif->p_aout_fifo->data_lock ) );
358 vlc_cond_signal( &(p_spdif->p_aout_fifo->data_wait ) );
359 vlc_mutex_unlock( &(p_spdif->p_aout_fifo->data_lock ) );
363 /* restore previous setting for output format */
364 config_PutInt( p_spdif->p_fifo, "audio-format",
365 p_spdif->i_previous_format );
367 /* Destroy descriptor */
371 /*****************************************************************************
372 * BitstreamCallback: Import parameters from the new data/PES packet
373 *****************************************************************************
374 * This function is called by input's NextDataPacket.
375 *****************************************************************************/
376 static void BitstreamCallback( bit_stream_t * p_bit_stream,
377 vlc_bool_t b_new_pes )
379 ac3_spdif_thread_t * p_spdif;
383 p_spdif = (ac3_spdif_thread_t *)p_bit_stream->p_callback_arg;
385 /* p_bit_stream->p_byte += 3; */
388 p_bit_stream->p_decoder_fifo->p_first->i_pts;
389 p_bit_stream->p_decoder_fifo->p_first->i_pts = 0;
393 /****************************************************************************
394 * ac3_parse_syncinfo: parse ac3 sync info
395 ****************************************************************************/
396 int ac3_parse_syncinfo( ac3_spdif_thread_t *p_spdif )
398 int p_sample_rates[4] = { 48000, 44100, 32000, -1 };
399 int i_frame_rate_code;
400 int i_frame_size_code;
401 sync_frame_t * p_sync_frame;
403 /* Read sync frame */
404 GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + 2,
405 sizeof(sync_frame_t) - 2 );
406 if( p_spdif->p_fifo->b_die ) return -1;
408 p_sync_frame = (sync_frame_t*)p_spdif->p_ac3;
410 /* Compute frame rate */
411 i_frame_rate_code = (p_sync_frame->syncinfo.code >> 6) & 0x03;
412 p_spdif->ac3_info.i_sample_rate = p_sample_rates[i_frame_rate_code];
413 if( p_spdif->ac3_info.i_sample_rate == -1 )
418 /* Compute frame size */
419 i_frame_size_code = p_sync_frame->syncinfo.code & 0x3f;
420 p_spdif->ac3_info.i_frame_size = 2 *
421 p_frame_size_code[i_frame_size_code].i_frame_size[i_frame_rate_code];
422 p_spdif->ac3_info.i_bit_rate =
423 p_frame_size_code[i_frame_size_code].i_bit_rate;
425 if( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
430 p_spdif->ac3_info.i_bs_mod = p_sync_frame->bsi.bsidmod & 0x7;