]> git.sesse.net Git - vlc/blob - modules/codec/spdif.c
* S/PDIF output should now be working (untested, though).
[vlc] / modules / codec / spdif.c
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 $
6  *
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>
11  *
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.
16  * 
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>                                              /* memcpy() */
33 #include <fcntl.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/decoder.h>
37 #include <vlc/aout.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 #define A52_FRAME_SIZE 1536 
44
45 /*****************************************************************************
46  * spdif_thread_t : A52 pass-through thread descriptor
47  *****************************************************************************/
48 typedef struct spdif_thread_s
49 {
50     /*
51      * Thread properties
52      */
53     vlc_thread_t        thread_id;                /* id for thread functions */
54
55     /*
56      * Input properties
57      */
58     decoder_fifo_t *    p_fifo;                /* stores the PES stream data */
59
60     /* The bit stream structure handles the PES stream at the bit level */
61     bit_stream_t        bit_stream;
62
63     /*
64      * Output properties
65      */
66     aout_instance_t *   p_aout; /* opaque */
67     aout_input_t *      p_aout_input; /* opaque */
68     audio_sample_format_t output_format;
69 } spdif_thread_t;
70
71 /****************************************************************************
72  * Local prototypes
73  ****************************************************************************/
74 static int  OpenDecoder    ( vlc_object_t * );
75 static int  RunDecoder     ( decoder_fifo_t * );
76
77 static int  InitThread     ( spdif_thread_t *, decoder_fifo_t * );
78 static void EndThread      ( spdif_thread_t * );
79
80 int         SyncInfo       ( const byte_t *, int *, int *, int * );
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 vlc_module_begin();
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" );
91 vlc_module_end();
92
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 
97  * to chose.
98  *****************************************************************************/
99 static int OpenDecoder( vlc_object_t *p_this ) 
100 {   
101     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
102
103     if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ') )
104     {   
105         return VLC_EGENERIC; 
106     }
107
108     p_fifo->pf_run = RunDecoder;
109     return VLC_SUCCESS;
110 }
111
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 )
118 {
119     spdif_thread_t * p_dec;
120     mtime_t last_date = 0;
121     
122     /* Allocate the memory needed to store the thread's structure */
123     p_dec = malloc( sizeof(spdif_thread_t) );
124     if( p_dec == NULL )
125     {
126         msg_Err( p_fifo, "out of memory" );
127         DecoderError( p_fifo );
128         return -1;
129     }
130   
131     if ( InitThread( p_dec, p_fifo ) )
132     {
133         
134         msg_Err( p_fifo, "could not initialize thread" );
135         DecoderError( p_fifo );
136         free( p_dec );
137         return -1;
138     }
139
140     /* liba52 decoder thread's main loop */
141     while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
142     {
143         int i_frame_size, i_flags, i_rate, i_bit_rate;
144         mtime_t pts;
145         /* Temporary buffer to store the raw frame to be decoded */
146         byte_t p_header[7];
147         aout_buffer_t * p_buffer;
148         u16 * pi_length;
149
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))
154         {
155             RemoveBits( &p_dec->bit_stream, 8 );
156         }
157
158         /* Get A/52 frame header */
159         GetChunk( &p_dec->bit_stream, p_header, 7 );
160         if( p_dec->p_fifo->b_die ) break;
161
162         /* Check if frame is valid and get frame info */
163         i_frame_size = SyncInfo( p_header, &i_flags, &i_rate,
164                                  &i_bit_rate );
165
166         if( !i_frame_size )
167         {
168             msg_Warn( p_dec->p_fifo, "a52_syncinfo failed" );
169             continue;
170         }
171
172         if( (p_dec->p_aout_input != NULL) &&
173             ( (p_dec->output_format.i_rate != i_rate) ) )
174         {
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;
178         }
179
180         /* Creating the audio input if not created yet. */
181         if( p_dec->p_aout_input == NULL )
182         {
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,
186                                                  &p_dec->p_aout,
187                                                  &p_dec->output_format );
188
189             if ( p_dec->p_aout_input == NULL )
190             {
191                 p_dec->p_fifo->b_error = 1;
192                 break;
193             }
194         }
195
196         /* Set the Presentation Time Stamp */
197         CurrentPTS( &p_dec->bit_stream, &pts, NULL );
198         if ( pts != 0 )
199         {
200             last_date = pts;
201         }
202
203         if ( !last_date )
204         {
205             byte_t p_junk[3840];
206
207             /* We've just started the stream, wait for the first PTS. */
208             GetChunk( &p_dec->bit_stream, p_junk, i_frame_size - 7 );
209             continue;
210         }
211
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;
218
219         /* The first two bytes store the length of the frame - this is
220          * a bit kludgy. */
221         pi_length = (u16 *)p_buffer->p_buffer;
222         *pi_length = i_frame_size;
223
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),
227                   i_frame_size - 7 );
228         if( p_dec->p_fifo->b_die ) break;
229
230         /* Send the buffer to the mixer. */
231         aout_BufferPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
232     }
233
234     /* If b_error is set, the spdif thread enters the error loop */
235     if( p_dec->p_fifo->b_error )
236     {
237         DecoderError( p_dec->p_fifo );
238     }
239
240     /* End of the spdif decoder thread */
241     EndThread( p_dec );
242     
243     return 0;
244 }
245
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 )
250 {
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;
257
258     /* Init the Bitstream */
259     InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
260                    NULL, NULL );
261
262     return 0;
263 }
264
265 /*****************************************************************************
266  * EndThread : spdif thread destruction
267  *****************************************************************************/
268 static void EndThread( spdif_thread_t * p_dec )
269 {
270     if ( p_dec->p_aout_input != NULL )
271     {
272         aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
273     }
274
275     free( p_dec );
276 }
277
278 /****************************************************************************
279  * Local structures and tables
280  ****************************************************************************/
281 typedef struct sync_frame_s
282 {
283     struct syncinfo
284     {
285         u8      syncword[2];
286         u8      crc1[2];
287         u8      code;
288     } syncinfo;
289
290     struct bsi
291     {
292         u8      bsidmod;
293         u8      acmod;
294     } bsi;
295 } sync_frame_t;
296
297 typedef struct frame_size_s
298 {
299     u16     i_bit_rate;
300     u16     i_frame_size[3];
301 } frame_size_t;                
302
303 typedef struct info_s
304 {
305     int i_bit_rate;
306     int i_frame_size;
307     int i_sample_rate;
308     int i_bs_mod;
309 } info_t;
310
311 static const frame_size_t p_frame_size_code[64] =
312 {
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 } }
351 };
352
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,
360                      int * pi_bitrate )
361 {
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;
366
367     p_sync_frame = (const sync_frame_t *)p_buffer;
368
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 )
373     {
374         return 0;
375     }
376
377     if ( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
378     {
379         return 0;
380     }
381
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;
385
386     return ( 2 * p_frame_size_code[i_frame_size_code]
387                    .i_frame_size[i_frame_rate_code] );
388 }