1 /*****************************************************************************
2 * spdif.c: A52 pass-through to external decoder with enabled soundcard
3 *****************************************************************************
4 * Copyright (C) 2001-2002 VideoLAN
5 * $Id: spdif.c,v 1.3 2002/08/11 23:26:28 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_SIZE 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',' ') )
108 p_fifo->pf_run = RunDecoder;
112 /****************************************************************************
113 * RunDecoder: the whole thing
114 ****************************************************************************
115 * This function is called just after the thread is launched.
116 ****************************************************************************/
117 static int RunDecoder( decoder_fifo_t *p_fifo )
119 spdif_thread_t * p_dec;
120 mtime_t last_date = 0;
122 /* Allocate the memory needed to store the thread's structure */
123 p_dec = malloc( sizeof(spdif_thread_t) );
126 msg_Err( p_fifo, "out of memory" );
127 DecoderError( p_fifo );
131 if ( InitThread( p_dec, p_fifo ) )
134 msg_Err( p_fifo, "could not initialize thread" );
135 DecoderError( p_fifo );
140 /* liba52 decoder thread's main loop */
141 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
143 int i_frame_size, i_flags, i_rate, i_bit_rate;
145 /* Temporary buffer to store the raw frame to be decoded */
147 aout_buffer_t * p_buffer;
149 /* Look for sync word - should be 0x0b77 */
150 RealignBits( &p_dec->bit_stream );
151 while( (ShowBits( &p_dec->bit_stream, 16 ) ) != 0x0b77 &&
152 (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error))
154 RemoveBits( &p_dec->bit_stream, 8 );
157 /* Get A/52 frame header */
158 GetChunk( &p_dec->bit_stream, p_header, 7 );
159 if( p_dec->p_fifo->b_die ) break;
161 /* Check if frame is valid and get frame info */
162 i_frame_size = SyncInfo( p_header, &i_flags, &i_rate,
167 msg_Warn( p_dec->p_fifo, "a52_syncinfo failed" );
171 if( (p_dec->p_aout_input != NULL) &&
172 ( (p_dec->output_format.i_rate != i_rate) ) )
174 /* Parameters changed - this should not happen. */
175 aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
176 p_dec->p_aout_input = NULL;
179 /* Creating the audio input if not created yet. */
180 if( p_dec->p_aout_input == NULL )
182 p_dec->output_format.i_rate = i_rate;
183 /* p_dec->output_format.i_channels = i_channels; */
184 p_dec->p_aout_input = aout_InputNew( p_dec->p_fifo,
186 &p_dec->output_format );
188 if ( p_dec->p_aout_input == NULL )
190 p_dec->p_fifo->b_error = 1;
195 /* Set the Presentation Time Stamp */
196 CurrentPTS( &p_dec->bit_stream, &pts, NULL );
206 /* We've just started the stream, wait for the first PTS. */
207 GetChunk( &p_dec->bit_stream, p_junk, i_frame_size - 7 );
211 p_buffer = aout_BufferNew( p_dec->p_aout, p_dec->p_aout_input,
213 if ( p_buffer == NULL ) return -1;
214 p_buffer->start_date = last_date;
215 last_date += (mtime_t)(A52_FRAME_SIZE * 1000000)
216 / p_dec->output_format.i_rate;
217 p_buffer->end_date = last_date;
219 /* Get the whole frame. */
220 memcpy( p_buffer->p_buffer + sizeof(u16), p_header, 7 );
221 GetChunk( &p_dec->bit_stream, p_buffer->p_buffer + 7 + sizeof(u16),
223 if( p_dec->p_fifo->b_die ) break;
225 /* Send the buffer to the mixer. */
226 aout_BufferPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
229 /* If b_error is set, the spdif thread enters the error loop */
230 if( p_dec->p_fifo->b_error )
232 DecoderError( p_dec->p_fifo );
235 /* End of the spdif decoder thread */
241 /****************************************************************************
242 * InitThread: initialize thread data and create output fifo
243 ****************************************************************************/
244 static int InitThread( spdif_thread_t * p_dec, decoder_fifo_t * p_fifo )
246 /* Initialize the thread properties */
247 p_dec->p_aout = NULL;
248 p_dec->p_aout_input = NULL;
249 p_dec->p_fifo = p_fifo;
250 p_dec->output_format.i_format = AOUT_FMT_A52;
251 p_dec->output_format.i_channels = -1;
253 /* Init the Bitstream */
254 InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
260 /*****************************************************************************
261 * EndThread : spdif thread destruction
262 *****************************************************************************/
263 static void EndThread( spdif_thread_t * p_dec )
265 if ( p_dec->p_aout_input != NULL )
267 aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
273 /****************************************************************************
274 * Local structures and tables
275 ****************************************************************************/
276 typedef struct sync_frame_s
292 typedef struct frame_size_s
298 typedef struct info_s
306 static const frame_size_t p_frame_size_code[64] =
308 { 32 ,{64 ,69 ,96 } },
309 { 32 ,{64 ,70 ,96 } },
310 { 40 ,{80 ,87 ,120 } },
311 { 40 ,{80 ,88 ,120 } },
312 { 48 ,{96 ,104 ,144 } },
313 { 48 ,{96 ,105 ,144 } },
314 { 56 ,{112 ,121 ,168 } },
315 { 56 ,{112 ,122 ,168 } },
316 { 64 ,{128 ,139 ,192 } },
317 { 64 ,{128 ,140 ,192 } },
318 { 80 ,{160 ,174 ,240 } },
319 { 80 ,{160 ,175 ,240 } },
320 { 96 ,{192 ,208 ,288 } },
321 { 96 ,{192 ,209 ,288 } },
322 { 112 ,{224 ,243 ,336 } },
323 { 112 ,{224 ,244 ,336 } },
324 { 128 ,{256 ,278 ,384 } },
325 { 128 ,{256 ,279 ,384 } },
326 { 160 ,{320 ,348 ,480 } },
327 { 160 ,{320 ,349 ,480 } },
328 { 192 ,{384 ,417 ,576 } },
329 { 192 ,{384 ,418 ,576 } },
330 { 224 ,{448 ,487 ,672 } },
331 { 224 ,{448 ,488 ,672 } },
332 { 256 ,{512 ,557 ,768 } },
333 { 256 ,{512 ,558 ,768 } },
334 { 320 ,{640 ,696 ,960 } },
335 { 320 ,{640 ,697 ,960 } },
336 { 384 ,{768 ,835 ,1152 } },
337 { 384 ,{768 ,836 ,1152 } },
338 { 448 ,{896 ,975 ,1344 } },
339 { 448 ,{896 ,976 ,1344 } },
340 { 512 ,{1024 ,1114 ,1536 } },
341 { 512 ,{1024 ,1115 ,1536 } },
342 { 576 ,{1152 ,1253 ,1728 } },
343 { 576 ,{1152 ,1254 ,1728 } },
344 { 640 ,{1280 ,1393 ,1920 } },
345 { 640 ,{1280 ,1394 ,1920 } }
348 /****************************************************************************
349 * SyncInfo: parse A52 sync info
350 ****************************************************************************
351 * NB : i_flags is unused, this is just to mimick liba52's a52_syncinfo
352 * Returns the frame size
353 ****************************************************************************/
354 static int SyncInfo( const byte_t * p_buffer, int * pi_flags, int * pi_rate,
357 static const int p_sample_rates[4] = { 48000, 44100, 32000, -1 };
358 int i_frame_rate_code;
359 int i_frame_size_code;
360 const sync_frame_t * p_sync_frame;
362 p_sync_frame = (const sync_frame_t *)p_buffer;
364 /* Compute frame rate */
365 i_frame_rate_code = (p_sync_frame->syncinfo.code >> 6) & 0x03;
366 *pi_rate = p_sample_rates[i_frame_rate_code];
367 if ( *pi_rate == -1 )
372 if ( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
377 /* Compute frame size */
378 i_frame_size_code = p_sync_frame->syncinfo.code & 0x3f;
379 *pi_bitrate = p_frame_size_code[i_frame_size_code].i_bit_rate;
381 return ( 2 * p_frame_size_code[i_frame_size_code]
382 .i_frame_size[i_frame_rate_code] );