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