* block: added
[vlc.git] / modules / codec / a52.c
1 /*****************************************************************************
2  * a52.c: parse A/52 audio sync info and packetize the stream
3  *****************************************************************************
4  * Copyright (C) 2001-2002 VideoLAN
5  * $Id$
6  *
7  * Authors: St├ęphane Borel <stef@via.ecp.fr>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc/decoder.h>
31
32 #include "vlc_block_helper.h"
33
34 #define A52_HEADER_SIZE 7
35
36 /*****************************************************************************
37  * decoder_sys_t : decoder descriptor
38  *****************************************************************************/
39 struct decoder_sys_t
40 {
41     /* Module mode */
42     vlc_bool_t b_packetizer;
43
44     /*
45      * Input properties
46      */
47     int i_state;
48
49     block_bytestream_t bytestream;
50
51     /*
52      * Common properties
53      */
54     audio_date_t   end_date;
55
56     mtime_t i_pts;
57     int i_frame_size, i_bit_rate;
58     unsigned int i_rate, i_channels, i_channels_conf;
59
60 };
61
62 enum {
63
64     STATE_NOSYNC,
65     STATE_SYNC,
66     STATE_HEADER,
67     STATE_NEXT_SYNC,
68     STATE_GET_DATA,
69     STATE_SEND_DATA
70 };
71
72 /****************************************************************************
73  * Local prototypes
74  ****************************************************************************/
75 static int  OpenDecoder   ( vlc_object_t * );
76 static int  OpenPacketizer( vlc_object_t * );
77 static void CloseDecoder  ( vlc_object_t * );
78 static void *DecodeBlock  ( decoder_t *, block_t ** );
79
80 static int  SyncInfo      ( const byte_t *, int *, int *, int *,int * );
81
82 static uint8_t       *GetOutBuffer ( decoder_t *, void ** );
83 static aout_buffer_t *GetAoutBuffer( decoder_t * );
84 static block_t       *GetSoutBuffer( decoder_t * );
85
86 /*****************************************************************************
87  * Module descriptor
88  *****************************************************************************/
89 vlc_module_begin();
90     set_description( _("A/52 parser") );
91     set_capability( "decoder", 100 );
92     set_callbacks( OpenDecoder, CloseDecoder );
93     set_category( CAT_INPUT );
94     set_subcategory( SUBCAT_INPUT_ACODEC );
95
96     add_submodule();
97     set_description( _("A/52 audio packetizer") );
98     set_capability( "packetizer", 10 );
99     set_callbacks( OpenPacketizer, CloseDecoder );
100 vlc_module_end();
101
102 /*****************************************************************************
103  * OpenDecoder: probe the decoder and return score
104  *****************************************************************************/
105 static int OpenDecoder( vlc_object_t *p_this )
106 {
107     decoder_t *p_dec = (decoder_t*)p_this;
108     decoder_sys_t *p_sys;
109
110     if( p_dec->fmt_in.i_codec != VLC_FOURCC('a','5','2',' ')
111           && p_dec->fmt_in.i_codec != VLC_FOURCC('a','5','2','b') )
112     {
113         return VLC_EGENERIC;
114     }
115
116     /* Allocate the memory needed to store the decoder's structure */
117     if( ( p_dec->p_sys = p_sys =
118           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
119     {
120         msg_Err( p_dec, "out of memory" );
121         return VLC_EGENERIC;
122     }
123
124     /* Misc init */
125     p_sys->b_packetizer = VLC_FALSE;
126     p_sys->i_state = STATE_NOSYNC;
127     aout_DateSet( &p_sys->end_date, 0 );
128
129     p_sys->bytestream = block_BytestreamInit( p_dec );
130
131     /* Set output properties */
132     p_dec->fmt_out.i_cat = AUDIO_ES;
133     p_dec->fmt_out.i_codec = VLC_FOURCC('a','5','2',' ');
134     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
135
136     /* Set callback */
137     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
138         DecodeBlock;
139     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
140         DecodeBlock;
141
142     return VLC_SUCCESS;
143 }
144
145 static int OpenPacketizer( vlc_object_t *p_this )
146 {
147     decoder_t *p_dec = (decoder_t*)p_this;
148
149     int i_ret = OpenDecoder( p_this );
150
151     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
152
153     return i_ret;
154 }
155
156 /****************************************************************************
157  * DecodeBlock: the whole thing
158  ****************************************************************************
159  * This function is called just after the thread is launched.
160  ****************************************************************************/
161 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
162 {
163     decoder_sys_t *p_sys = p_dec->p_sys;
164     uint8_t p_header[A52_HEADER_SIZE];
165     uint8_t *p_buf;
166     void *p_out_buffer;
167
168     if( !pp_block || !*pp_block ) return NULL;
169
170     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
171     {
172         /* We've just started the stream, wait for the first PTS. */
173         block_Release( *pp_block );
174         return NULL;
175     }
176
177     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
178     {
179         p_sys->i_state = STATE_NOSYNC;
180     }
181
182     block_BytestreamPush( &p_sys->bytestream, *pp_block );
183
184     while( 1 )
185     {
186         switch( p_sys->i_state )
187         {
188         case STATE_NOSYNC:
189             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
190                    == VLC_SUCCESS )
191             {
192                 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
193                 {
194                     p_sys->i_state = STATE_SYNC;
195                     break;
196                 }
197                 block_SkipByte( &p_sys->bytestream );
198             }
199             if( p_sys->i_state != STATE_SYNC )
200             {
201                 block_BytestreamFlush( &p_sys->bytestream );
202
203                 /* Need more data */
204                 return NULL;
205             }
206
207         case STATE_SYNC:
208             /* New frame, set the Presentation Time Stamp */
209             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
210             if( p_sys->i_pts != 0 &&
211                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
212             {
213                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
214             }
215             p_sys->i_state = STATE_HEADER;
216
217         case STATE_HEADER:
218             /* Get A/52 frame header (A52_HEADER_SIZE bytes) */
219             if( block_PeekBytes( &p_sys->bytestream, p_header,
220                                  A52_HEADER_SIZE ) != VLC_SUCCESS )
221             {
222                 /* Need more data */
223                 return NULL;
224             }
225
226             /* Check if frame is valid and get frame info */
227             p_sys->i_frame_size = SyncInfo( p_header,
228                                             &p_sys->i_channels,
229                                             &p_sys->i_channels_conf,
230                                             &p_sys->i_rate,
231                                             &p_sys->i_bit_rate );
232             if( !p_sys->i_frame_size )
233             {
234                 msg_Dbg( p_dec, "emulated sync word" );
235                 block_SkipByte( &p_sys->bytestream );
236                 p_sys->i_state = STATE_NOSYNC;
237                 break;
238             }
239             p_sys->i_state = STATE_NEXT_SYNC;
240
241         case STATE_NEXT_SYNC:
242             /* TODO: If pp_block == NULL, flush the buffer without checking the
243              * next sync word */
244
245             /* Check if next expected frame contains the sync word */
246             if( block_PeekOffsetBytes( &p_sys->bytestream,
247                                        p_sys->i_frame_size, p_header, 2 )
248                 != VLC_SUCCESS )
249             {
250                 /* Need more data */
251                 return NULL;
252             }
253
254             if( p_sys->b_packetizer &&
255                 p_header[0] == 0 && p_header[1] == 0 )
256             {
257                 /* A52 wav files and audio CD's use stuffing */
258                 p_sys->i_state = STATE_GET_DATA;
259                 break;
260             }
261
262             if( p_header[0] != 0x0b || p_header[1] != 0x77 )
263             {
264                 msg_Dbg( p_dec, "emulated sync word "
265                          "(no sync on following frame)" );
266                 p_sys->i_state = STATE_NOSYNC;
267                 block_SkipByte( &p_sys->bytestream );
268                 break;
269             }
270             p_sys->i_state = STATE_SEND_DATA;
271             break;
272
273         case STATE_GET_DATA:
274             /* Make sure we have enough data.
275              * (Not useful if we went through NEXT_SYNC) */
276             if( block_WaitBytes( &p_sys->bytestream,
277                                  p_sys->i_frame_size ) != VLC_SUCCESS )
278             {
279                 /* Need more data */
280                 return NULL;
281             }
282             p_sys->i_state = STATE_SEND_DATA;
283
284         case STATE_SEND_DATA:
285             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
286             {
287                 //p_dec->b_error = VLC_TRUE;
288                 return NULL;
289             }
290
291             /* Copy the whole frame into the buffer. When we reach this point
292              * we already know we have enough data available. */
293             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
294
295             /* Make sure we don't reuse the same pts twice */
296             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
297                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
298
299             /* So p_block doesn't get re-added several times */
300             *pp_block = block_BytestreamPop( &p_sys->bytestream );
301
302             p_sys->i_state = STATE_NOSYNC;
303
304             return p_out_buffer;
305         }
306     }
307
308     return NULL;
309 }
310
311 /*****************************************************************************
312  * CloseDecoder: clean up the decoder
313  *****************************************************************************/
314 static void CloseDecoder( vlc_object_t *p_this )
315 {
316     decoder_t *p_dec = (decoder_t*)p_this;
317     decoder_sys_t *p_sys = p_dec->p_sys;
318
319     block_BytestreamRelease( &p_sys->bytestream );
320
321     free( p_sys );
322 }
323
324 /*****************************************************************************
325  * GetOutBuffer:
326  *****************************************************************************/
327 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
328 {
329     decoder_sys_t *p_sys = p_dec->p_sys;
330     uint8_t *p_buf;
331
332     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
333     {
334         msg_Info( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
335                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
336
337         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
338         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
339     }
340
341     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
342     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
343     p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
344     p_dec->fmt_out.audio.i_frame_length = A52_FRAME_NB;
345
346     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
347     p_dec->fmt_out.audio.i_physical_channels =
348         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
349
350     p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
351
352     if( p_sys->b_packetizer )
353     {
354         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
355         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
356         *pp_out_buffer = p_sout_buffer;
357     }
358     else
359     {
360         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
361         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
362         *pp_out_buffer = p_aout_buffer;
363     }
364
365     return p_buf;
366 }
367
368 /*****************************************************************************
369  * GetAoutBuffer:
370  *****************************************************************************/
371 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
372 {
373     decoder_sys_t *p_sys = p_dec->p_sys;
374     aout_buffer_t *p_buf;
375
376     p_buf = p_dec->pf_aout_buffer_new( p_dec, A52_FRAME_NB  );
377     if( p_buf == NULL ) return NULL;
378
379     p_buf->start_date = aout_DateGet( &p_sys->end_date );
380     p_buf->end_date = aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB );
381
382     return p_buf;
383 }
384
385 /*****************************************************************************
386  * GetSoutBuffer:
387  *****************************************************************************/
388 static block_t *GetSoutBuffer( decoder_t *p_dec )
389 {
390     decoder_sys_t *p_sys = p_dec->p_sys;
391     block_t *p_block;
392
393     p_block = block_New( p_dec, p_sys->i_frame_size );
394     if( p_block == NULL ) return NULL;
395
396     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
397
398     p_block->i_length = aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB ) -
399         p_block->i_pts;
400
401     return p_block;
402 }
403
404 /*****************************************************************************
405  * SyncInfo: parse A/52 sync info
406  *****************************************************************************
407  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
408  * since we don't want to oblige S/PDIF people to use liba52 just to get
409  * their SyncInfo...
410  *****************************************************************************/
411 static int SyncInfo( const byte_t * p_buf,
412                      int * pi_channels, int * pi_channels_conf,
413                      int * pi_sample_rate, int * pi_bit_rate )
414 {
415     static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
416     static const int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
417                                 128, 160, 192, 224, 256, 320, 384, 448,
418                                 512, 576, 640 };
419     static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
420                                       0x04, 0x01, 0x04, 0x01 };
421     int frmsizecod;
422     int bitrate;
423     int half;
424     int acmod;
425
426     if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77))        /* syncword */
427         return 0;
428
429     if (p_buf[5] >= 0x60)                /* bsid >= 12 */
430         return 0;
431     half = halfrate[p_buf[5] >> 3];
432
433     /* acmod, dsurmod and lfeon */
434     acmod = p_buf[6] >> 5;
435     if ( (p_buf[6] & 0xf8) == 0x50 )
436     {
437         /* Dolby surround = stereo + Dolby */
438         *pi_channels = 2;
439         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
440                             | AOUT_CHAN_DOLBYSTEREO;
441     }
442     else switch ( acmod )
443     {
444     case 0x0:
445         /* Dual-mono = stereo + dual-mono */
446         *pi_channels = 2;
447         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
448                             | AOUT_CHAN_DUALMONO;
449         break;
450     case 0x1:
451         /* Mono */
452         *pi_channels = 1;
453         *pi_channels_conf = AOUT_CHAN_CENTER;
454         break;
455     case 0x2:
456         /* Stereo */
457         *pi_channels = 2;
458         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
459         break;
460     case 0x3:
461         /* 3F */
462         *pi_channels = 3;
463         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
464                             | AOUT_CHAN_CENTER;
465         break;
466     case 0x4:
467         /* 2F1R */
468         *pi_channels = 3;
469         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
470                             | AOUT_CHAN_REARCENTER;
471         break;
472     case 0x5:
473         /* 3F1R */
474         *pi_channels = 4;
475         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
476                             | AOUT_CHAN_REARCENTER;
477         break;
478     case 0x6:
479         /* 2F2R */
480         *pi_channels = 4;
481         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
482                             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
483         break;
484     case 0x7:
485         /* 3F2R */
486         *pi_channels = 5;
487         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
488                             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
489         break;
490     default:
491         return 0;
492     }
493
494     if ( p_buf[6] & lfeon[acmod] )
495     {
496         (*pi_channels)++;
497         *pi_channels_conf |= AOUT_CHAN_LFE;
498     }
499
500     frmsizecod = p_buf[4] & 63;
501     if (frmsizecod >= 38)
502         return 0;
503     bitrate = rate [frmsizecod >> 1];
504     *pi_bit_rate = (bitrate * 1000) >> half;
505
506     switch (p_buf[4] & 0xc0) {
507     case 0:
508         *pi_sample_rate = 48000 >> half;
509         return 4 * bitrate;
510     case 0x40:
511         *pi_sample_rate = 44100 >> half;
512         return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
513     case 0x80:
514         *pi_sample_rate = 32000 >> half;
515         return 6 * bitrate;
516     default:
517         return 0;
518     }
519 }