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.2 2002/08/11 22:36:35 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 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;
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 /* Get A/52 frame header */
159 GetChunk( &p_dec->bit_stream, p_header, 7 );
160 if( p_dec->p_fifo->b_die ) break;
162 /* Check if frame is valid and get frame info */
163 i_frame_size = SyncInfo( p_header, &i_flags, &i_rate,
168 msg_Warn( p_dec->p_fifo, "a52_syncinfo failed" );
172 if( (p_dec->p_aout_input != NULL) &&
173 ( (p_dec->output_format.i_rate != i_rate) ) )
175 /* Parameters changed - this should not happen. */
176 aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
177 p_dec->p_aout_input = NULL;
180 /* Creating the audio input if not created yet. */
181 if( p_dec->p_aout_input == NULL )
183 p_dec->output_format.i_rate = i_rate;
184 /* p_dec->output_format.i_channels = i_channels; */
185 p_dec->p_aout_input = aout_InputNew( p_dec->p_fifo,
187 &p_dec->output_format );
189 if ( p_dec->p_aout_input == NULL )
191 p_dec->p_fifo->b_error = 1;
196 /* Set the Presentation Time Stamp */
197 CurrentPTS( &p_dec->bit_stream, &pts, NULL );
207 /* We've just started the stream, wait for the first PTS. */
208 GetChunk( &p_dec->bit_stream, p_junk, i_frame_size - 7 );
212 p_buffer = aout_BufferNew( p_dec->p_aout, p_dec->p_aout_input, 1 );
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 /* The first two bytes store the length of the frame - this is
221 pi_length = (u16 *)p_buffer->p_buffer;
222 *pi_length = i_frame_size;
224 /* Get the whole frame. */
225 memcpy( p_buffer->p_buffer + sizeof(u16), p_header, 7 );
226 GetChunk( &p_dec->bit_stream, p_buffer->p_buffer + 7 + sizeof(u16),
228 if( p_dec->p_fifo->b_die ) break;
230 /* Send the buffer to the mixer. */
231 aout_BufferPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
234 /* If b_error is set, the spdif thread enters the error loop */
235 if( p_dec->p_fifo->b_error )
237 DecoderError( p_dec->p_fifo );
240 /* End of the spdif decoder thread */
246 /****************************************************************************
247 * InitThread: initialize thread data and create output fifo
248 ****************************************************************************/
249 static int InitThread( spdif_thread_t * p_dec, decoder_fifo_t * p_fifo )
251 /* Initialize the thread properties */
252 p_dec->p_aout = NULL;
253 p_dec->p_aout_input = NULL;
254 p_dec->p_fifo = p_fifo;
255 p_dec->output_format.i_format = AOUT_FMT_A52;
256 p_dec->output_format.i_channels = -1;
258 /* Init the Bitstream */
259 InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
265 /*****************************************************************************
266 * EndThread : spdif thread destruction
267 *****************************************************************************/
268 static void EndThread( spdif_thread_t * p_dec )
270 if ( p_dec->p_aout_input != NULL )
272 aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
278 /****************************************************************************
279 * Local structures and tables
280 ****************************************************************************/
281 typedef struct sync_frame_s
297 typedef struct frame_size_s
303 typedef struct info_s
311 static const frame_size_t p_frame_size_code[64] =
313 { 32 ,{64 ,69 ,96 } },
314 { 32 ,{64 ,70 ,96 } },
315 { 40 ,{80 ,87 ,120 } },
316 { 40 ,{80 ,88 ,120 } },
317 { 48 ,{96 ,104 ,144 } },
318 { 48 ,{96 ,105 ,144 } },
319 { 56 ,{112 ,121 ,168 } },
320 { 56 ,{112 ,122 ,168 } },
321 { 64 ,{128 ,139 ,192 } },
322 { 64 ,{128 ,140 ,192 } },
323 { 80 ,{160 ,174 ,240 } },
324 { 80 ,{160 ,175 ,240 } },
325 { 96 ,{192 ,208 ,288 } },
326 { 96 ,{192 ,209 ,288 } },
327 { 112 ,{224 ,243 ,336 } },
328 { 112 ,{224 ,244 ,336 } },
329 { 128 ,{256 ,278 ,384 } },
330 { 128 ,{256 ,279 ,384 } },
331 { 160 ,{320 ,348 ,480 } },
332 { 160 ,{320 ,349 ,480 } },
333 { 192 ,{384 ,417 ,576 } },
334 { 192 ,{384 ,418 ,576 } },
335 { 224 ,{448 ,487 ,672 } },
336 { 224 ,{448 ,488 ,672 } },
337 { 256 ,{512 ,557 ,768 } },
338 { 256 ,{512 ,558 ,768 } },
339 { 320 ,{640 ,696 ,960 } },
340 { 320 ,{640 ,697 ,960 } },
341 { 384 ,{768 ,835 ,1152 } },
342 { 384 ,{768 ,836 ,1152 } },
343 { 448 ,{896 ,975 ,1344 } },
344 { 448 ,{896 ,976 ,1344 } },
345 { 512 ,{1024 ,1114 ,1536 } },
346 { 512 ,{1024 ,1115 ,1536 } },
347 { 576 ,{1152 ,1253 ,1728 } },
348 { 576 ,{1152 ,1254 ,1728 } },
349 { 640 ,{1280 ,1393 ,1920 } },
350 { 640 ,{1280 ,1394 ,1920 } }
353 /****************************************************************************
354 * SyncInfo: parse A52 sync info
355 ****************************************************************************
356 * NB : i_flags is unused, this is just to mimick liba52's a52_syncinfo
357 * Returns the frame size
358 ****************************************************************************/
359 static int SyncInfo( const byte_t * p_buffer, int * pi_flags, int * pi_rate,
362 static const int p_sample_rates[4] = { 48000, 44100, 32000, -1 };
363 int i_frame_rate_code;
364 int i_frame_size_code;
365 const sync_frame_t * p_sync_frame;
367 p_sync_frame = (const sync_frame_t *)p_buffer;
369 /* Compute frame rate */
370 i_frame_rate_code = (p_sync_frame->syncinfo.code >> 6) & 0x03;
371 *pi_rate = p_sample_rates[i_frame_rate_code];
372 if ( *pi_rate == -1 )
377 if ( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
382 /* Compute frame size */
383 i_frame_size_code = p_sync_frame->syncinfo.code & 0x3f;
384 *pi_bitrate = p_frame_size_code[i_frame_size_code].i_bit_rate;
386 return ( 2 * p_frame_size_code[i_frame_size_code]
387 .i_frame_size[i_frame_rate_code] );