1 /*****************************************************************************
2 * spdif.c: A/52 pass-through to external decoder with enabled soundcard
3 *****************************************************************************
4 * Copyright (C) 2001-2002 VideoLAN
5 * $Id: spdif.c,v 1.8 2002/08/30 22:22:24 massiot Exp $
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Juha Yrjola <jyrjola@cc.hut.fi>
9 * German Gomez Garcia <german@piraos.com>
10 * Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <string.h> /* memcpy() */
36 #include <vlc/decoder.h>
43 #define A52_FRAME_NB 1536
45 /*****************************************************************************
46 * spdif_thread_t : A52 pass-through thread descriptor
47 *****************************************************************************/
48 typedef struct spdif_thread_s
53 vlc_thread_t thread_id; /* id for thread functions */
58 decoder_fifo_t * p_fifo; /* stores the PES stream data */
60 /* The bit stream structure handles the PES stream at the bit level */
61 bit_stream_t bit_stream;
66 aout_instance_t * p_aout; /* opaque */
67 aout_input_t * p_aout_input; /* opaque */
68 audio_sample_format_t output_format;
71 /****************************************************************************
73 ****************************************************************************/
74 static int OpenDecoder ( vlc_object_t * );
75 static int RunDecoder ( decoder_fifo_t * );
77 static int InitThread ( spdif_thread_t *, decoder_fifo_t * );
78 static void EndThread ( spdif_thread_t * );
80 static int SyncInfo ( const byte_t *, int *, int *, int * );
82 /*****************************************************************************
84 *****************************************************************************/
86 set_description( _("SPDIF pass-through A52 decoder") );
87 set_capability( "decoder", 0 );
88 set_callbacks( OpenDecoder, NULL );
89 add_shortcut( "pass_through" );
90 add_shortcut( "pass" );
93 /*****************************************************************************
94 * OpenDecoder: probe the decoder and return score
95 *****************************************************************************
96 * Tries to launch a decoder and return score so that the interface is able
98 *****************************************************************************/
99 static int OpenDecoder( vlc_object_t *p_this )
101 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
103 if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ')
104 && p_fifo->i_fourcc != VLC_FOURCC('a','5','2','b') )
109 p_fifo->pf_run = RunDecoder;
113 /****************************************************************************
114 * RunDecoder: the whole thing
115 ****************************************************************************
116 * This function is called just after the thread is launched.
117 ****************************************************************************/
118 static int RunDecoder( decoder_fifo_t *p_fifo )
120 spdif_thread_t * p_dec;
121 audio_date_t end_date;
123 /* Allocate the memory needed to store the thread's structure */
124 p_dec = malloc( sizeof(spdif_thread_t) );
127 msg_Err( p_fifo, "out of memory" );
128 DecoderError( p_fifo );
132 if ( InitThread( p_dec, p_fifo ) )
135 msg_Err( p_fifo, "could not initialize thread" );
136 DecoderError( p_fifo );
141 /* decoder thread's main loop */
142 while ( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
144 int i_frame_size, i_flags, i_rate, i_bit_rate;
146 /* Temporary buffer to store the raw frame to be decoded */
148 aout_buffer_t * p_buffer;
150 /* Look for sync word - should be 0x0b77 */
151 RealignBits( &p_dec->bit_stream );
152 while ( (ShowBits( &p_dec->bit_stream, 16 ) ) != 0x0b77 &&
153 (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error))
155 RemoveBits( &p_dec->bit_stream, 8 );
158 /* Set the Presentation Time Stamp */
159 NextPTS( &p_dec->bit_stream, &pts, NULL );
160 if ( pts != 0 && pts != aout_DateGet( &end_date ) )
162 aout_DateSet( &end_date, pts );
165 /* Get A/52 frame header */
166 GetChunk( &p_dec->bit_stream, p_header, 7 );
167 if( p_dec->p_fifo->b_die ) break;
169 /* Check if frame is valid and get frame info */
170 i_frame_size = SyncInfo( p_header, &i_flags, &i_rate,
175 msg_Warn( p_dec->p_fifo, "a52_syncinfo failed" );
179 if( (p_dec->p_aout_input != NULL) &&
180 ( (p_dec->output_format.i_rate != i_rate)
181 || (p_dec->output_format.i_bytes_per_frame != i_frame_size) ) )
183 /* Parameters changed - this should not happen. */
184 aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
185 p_dec->p_aout_input = NULL;
188 /* Creating the audio input if not created yet. */
189 if( p_dec->p_aout_input == NULL )
191 p_dec->output_format.i_rate = i_rate;
192 p_dec->output_format.i_bytes_per_frame = i_frame_size;
193 p_dec->output_format.i_frame_length = A52_FRAME_NB;
194 aout_DateInit( &end_date, i_rate );
195 p_dec->p_aout_input = aout_InputNew( p_dec->p_fifo,
197 &p_dec->output_format );
199 if ( p_dec->p_aout_input == NULL )
201 p_dec->p_fifo->b_error = 1;
206 if ( !aout_DateGet( &end_date ) )
210 /* We've just started the stream, wait for the first PTS. */
211 GetChunk( &p_dec->bit_stream, p_junk, i_frame_size - 7 );
215 p_buffer = aout_BufferNew( p_dec->p_aout, p_dec->p_aout_input,
217 if ( p_buffer == NULL ) return -1;
218 p_buffer->start_date = aout_DateGet( &end_date );
219 p_buffer->end_date = aout_DateIncrement( &end_date,
222 /* Get the whole frame. */
223 memcpy( p_buffer->p_buffer, p_header, 7 );
224 GetChunk( &p_dec->bit_stream, p_buffer->p_buffer + 7,
226 if( p_dec->p_fifo->b_die ) break;
228 /* Send the buffer to the mixer. */
229 aout_BufferPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
232 /* If b_error is set, the spdif thread enters the error loop */
233 if( p_dec->p_fifo->b_error )
235 DecoderError( p_dec->p_fifo );
238 /* End of the spdif decoder thread */
244 /****************************************************************************
245 * InitThread: initialize thread data and create output fifo
246 ****************************************************************************/
247 static int InitThread( spdif_thread_t * p_dec, decoder_fifo_t * p_fifo )
249 /* Initialize the thread properties */
250 p_dec->p_aout = NULL;
251 p_dec->p_aout_input = NULL;
252 p_dec->p_fifo = p_fifo;
253 p_dec->output_format.i_format = AOUT_FMT_A52;
254 p_dec->output_format.i_channels = -1;
256 /* Init the Bitstream */
257 InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
263 /*****************************************************************************
264 * EndThread : spdif thread destruction
265 *****************************************************************************/
266 static void EndThread( spdif_thread_t * p_dec )
268 if ( p_dec->p_aout_input != NULL )
270 aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
276 /****************************************************************************
277 * Local structures and tables
278 ****************************************************************************/
279 typedef struct sync_frame_s
295 typedef struct frame_size_s
301 typedef struct info_s
309 static const frame_size_t p_frame_size_code[64] =
311 { 32 ,{64 ,69 ,96 } },
312 { 32 ,{64 ,70 ,96 } },
313 { 40 ,{80 ,87 ,120 } },
314 { 40 ,{80 ,88 ,120 } },
315 { 48 ,{96 ,104 ,144 } },
316 { 48 ,{96 ,105 ,144 } },
317 { 56 ,{112 ,121 ,168 } },
318 { 56 ,{112 ,122 ,168 } },
319 { 64 ,{128 ,139 ,192 } },
320 { 64 ,{128 ,140 ,192 } },
321 { 80 ,{160 ,174 ,240 } },
322 { 80 ,{160 ,175 ,240 } },
323 { 96 ,{192 ,208 ,288 } },
324 { 96 ,{192 ,209 ,288 } },
325 { 112 ,{224 ,243 ,336 } },
326 { 112 ,{224 ,244 ,336 } },
327 { 128 ,{256 ,278 ,384 } },
328 { 128 ,{256 ,279 ,384 } },
329 { 160 ,{320 ,348 ,480 } },
330 { 160 ,{320 ,349 ,480 } },
331 { 192 ,{384 ,417 ,576 } },
332 { 192 ,{384 ,418 ,576 } },
333 { 224 ,{448 ,487 ,672 } },
334 { 224 ,{448 ,488 ,672 } },
335 { 256 ,{512 ,557 ,768 } },
336 { 256 ,{512 ,558 ,768 } },
337 { 320 ,{640 ,696 ,960 } },
338 { 320 ,{640 ,697 ,960 } },
339 { 384 ,{768 ,835 ,1152 } },
340 { 384 ,{768 ,836 ,1152 } },
341 { 448 ,{896 ,975 ,1344 } },
342 { 448 ,{896 ,976 ,1344 } },
343 { 512 ,{1024 ,1114 ,1536 } },
344 { 512 ,{1024 ,1115 ,1536 } },
345 { 576 ,{1152 ,1253 ,1728 } },
346 { 576 ,{1152 ,1254 ,1728 } },
347 { 640 ,{1280 ,1393 ,1920 } },
348 { 640 ,{1280 ,1394 ,1920 } }
351 /****************************************************************************
352 * SyncInfo: parse A52 sync info
353 ****************************************************************************
354 * NB : i_flags is unused, this is just to mimick liba52's a52_syncinfo
355 * Returns the frame size
356 ****************************************************************************/
357 static int SyncInfo( const byte_t * p_buffer, int * pi_flags, int * pi_rate,
360 static const int p_sample_rates[4] = { 48000, 44100, 32000, -1 };
361 int i_frame_rate_code;
362 int i_frame_size_code;
363 const sync_frame_t * p_sync_frame;
365 p_sync_frame = (const sync_frame_t *)p_buffer;
367 /* Compute frame rate */
368 i_frame_rate_code = (p_sync_frame->syncinfo.code >> 6) & 0x03;
369 *pi_rate = p_sample_rates[i_frame_rate_code];
370 if ( *pi_rate == -1 )
375 if ( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
380 /* Compute frame size */
381 i_frame_size_code = p_sync_frame->syncinfo.code & 0x3f;
382 *pi_bitrate = p_frame_size_code[i_frame_size_code].i_bit_rate;
384 return ( 2 * p_frame_size_code[i_frame_size_code]
385 .i_frame_size[i_frame_rate_code] );