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