]> git.sesse.net Git - vlc/blob - modules/codec/spdif.c
fdf3ccc94e76f423407e8274c2a81869f2a714d4
[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.3 2002/08/11 23:26:28 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 static 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
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))
153         {
154             RemoveBits( &p_dec->bit_stream, 8 );
155         }
156
157         /* Get A/52 frame header */
158         GetChunk( &p_dec->bit_stream, p_header, 7 );
159         if( p_dec->p_fifo->b_die ) break;
160
161         /* Check if frame is valid and get frame info */
162         i_frame_size = SyncInfo( p_header, &i_flags, &i_rate,
163                                  &i_bit_rate );
164
165         if( !i_frame_size )
166         {
167             msg_Warn( p_dec->p_fifo, "a52_syncinfo failed" );
168             continue;
169         }
170
171         if( (p_dec->p_aout_input != NULL) &&
172             ( (p_dec->output_format.i_rate != i_rate) ) )
173         {
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;
177         }
178
179         /* Creating the audio input if not created yet. */
180         if( p_dec->p_aout_input == NULL )
181         {
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,
185                                                  &p_dec->p_aout,
186                                                  &p_dec->output_format );
187
188             if ( p_dec->p_aout_input == NULL )
189             {
190                 p_dec->p_fifo->b_error = 1;
191                 break;
192             }
193         }
194
195         /* Set the Presentation Time Stamp */
196         CurrentPTS( &p_dec->bit_stream, &pts, NULL );
197         if ( pts != 0 )
198         {
199             last_date = pts;
200         }
201
202         if ( !last_date )
203         {
204             byte_t p_junk[3840];
205
206             /* We've just started the stream, wait for the first PTS. */
207             GetChunk( &p_dec->bit_stream, p_junk, i_frame_size - 7 );
208             continue;
209         }
210
211         p_buffer = aout_BufferNew( p_dec->p_aout, p_dec->p_aout_input,
212                                    i_frame_size );
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         /* 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),
222                   i_frame_size - 7 );
223         if( p_dec->p_fifo->b_die ) break;
224
225         /* Send the buffer to the mixer. */
226         aout_BufferPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
227     }
228
229     /* If b_error is set, the spdif thread enters the error loop */
230     if( p_dec->p_fifo->b_error )
231     {
232         DecoderError( p_dec->p_fifo );
233     }
234
235     /* End of the spdif decoder thread */
236     EndThread( p_dec );
237     
238     return 0;
239 }
240
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 )
245 {
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;
252
253     /* Init the Bitstream */
254     InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
255                    NULL, NULL );
256
257     return 0;
258 }
259
260 /*****************************************************************************
261  * EndThread : spdif thread destruction
262  *****************************************************************************/
263 static void EndThread( spdif_thread_t * p_dec )
264 {
265     if ( p_dec->p_aout_input != NULL )
266     {
267         aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
268     }
269
270     free( p_dec );
271 }
272
273 /****************************************************************************
274  * Local structures and tables
275  ****************************************************************************/
276 typedef struct sync_frame_s
277 {
278     struct syncinfo
279     {
280         u8      syncword[2];
281         u8      crc1[2];
282         u8      code;
283     } syncinfo;
284
285     struct bsi
286     {
287         u8      bsidmod;
288         u8      acmod;
289     } bsi;
290 } sync_frame_t;
291
292 typedef struct frame_size_s
293 {
294     u16     i_bit_rate;
295     u16     i_frame_size[3];
296 } frame_size_t;                
297
298 typedef struct info_s
299 {
300     int i_bit_rate;
301     int i_frame_size;
302     int i_sample_rate;
303     int i_bs_mod;
304 } info_t;
305
306 static const frame_size_t p_frame_size_code[64] =
307 {
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 } }
346 };
347
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,
355                      int * pi_bitrate )
356 {
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;
361
362     p_sync_frame = (const sync_frame_t *)p_buffer;
363
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 )
368     {
369         return 0;
370     }
371
372     if ( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
373     {
374         return 0;
375     }
376
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;
380
381     return ( 2 * p_frame_size_code[i_frame_size_code]
382                    .i_frame_size[i_frame_rate_code] );
383 }