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