]> git.sesse.net Git - vlc/blob - modules/codec/a52.c
* ALL: Introduction of a new api for decoders.
[vlc] / modules / codec / a52.c
1 /*****************************************************************************
2  * a52.c: A/52 basic parser
3  *****************************************************************************
4  * Copyright (C) 2001-2002 VideoLAN
5  * $Id: a52.c,v 1.23 2003/09/02 20:19:25 gbazin 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 <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>                                              /* memcpy() */
32 #include <fcntl.h>
33
34 #include <vlc/vlc.h>
35 #include <vlc/decoder.h>
36 #include <vlc/input.h>
37 #include <vlc/aout.h>
38 #include <vlc/sout.h>
39
40 #ifdef HAVE_UNISTD_H
41 #   include <unistd.h>
42 #endif
43
44 #define A52_HEADER_SIZE 7
45
46 /*****************************************************************************
47  * decoder_sys_t : decoder descriptor
48  *****************************************************************************/
49 struct decoder_sys_t
50 {
51     /* Module mode */
52     vlc_bool_t b_packetizer;
53
54     /*
55      * Input properties
56      */
57     int     i_state;
58
59     uint8_t p_header[A52_HEADER_SIZE];
60     int     i_header;
61
62     mtime_t pts;
63
64     int     i_frame_size;
65
66     /*
67      * Decoder output properties
68      */
69     aout_instance_t *     p_aout;                                  /* opaque */
70     aout_input_t *        p_aout_input;                            /* opaque */
71     audio_sample_format_t aout_format;
72
73     aout_buffer_t *       p_aout_buffer; /* current aout buffer being filled */
74
75     /*
76      * Packetizer output properties
77      */
78     sout_packetizer_input_t *p_sout_input;
79     sout_format_t           sout_format;
80     sout_buffer_t *         p_sout_buffer;            /* current sout buffer */
81
82     /*
83      * Common properties
84      */
85     uint8_t               *p_out_buffer;                    /* output buffer */
86     int                   i_out_buffer;         /* position in output buffer */
87     audio_date_t          end_date;
88 };
89
90 enum {
91
92     STATE_NOSYNC,
93     STATE_PARTIAL_SYNC,
94     STATE_SYNC,
95     STATE_HEADER,
96     STATE_DATA
97 };
98
99 /****************************************************************************
100  * Local prototypes
101  ****************************************************************************/
102 static int  OpenDecoder   ( vlc_object_t * );
103 static int  OpenPacketizer( vlc_object_t * );
104
105 static int  InitDecoder   ( decoder_t * );
106 static int  RunDecoder    ( decoder_t *, block_t * );
107 static int  EndDecoder    ( decoder_t * );
108
109 static int  SyncInfo      ( const byte_t *, int *, int *, int *,int * );
110
111 static int GetOutBuffer ( decoder_t *, uint8_t ** );
112 static int GetAoutBuffer( decoder_t *, aout_buffer_t ** );
113 static int GetSoutBuffer( decoder_t *, sout_buffer_t ** );
114 static int SendOutBuffer( decoder_t * );
115
116 /*****************************************************************************
117  * Module descriptor
118  *****************************************************************************/
119 vlc_module_begin();
120     set_description( _("A/52 parser") );
121     set_capability( "decoder", 100 );
122     set_callbacks( OpenDecoder, NULL );
123
124     add_submodule();
125     set_description( _("A/52 audio packetizer") );
126     set_capability( "packetizer", 10 );
127     set_callbacks( OpenPacketizer, NULL );
128 vlc_module_end();
129
130 /*****************************************************************************
131  * OpenDecoder: probe the decoder and return score
132  *****************************************************************************/
133 static int OpenDecoder( vlc_object_t *p_this )
134 {
135     decoder_t *p_dec = (decoder_t*)p_this;
136
137     if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ')
138          && p_dec->p_fifo->i_fourcc != VLC_FOURCC('a','5','2','b') )
139     {
140         return VLC_EGENERIC;
141     }
142
143     p_dec->pf_init = InitDecoder;
144     p_dec->pf_decode = RunDecoder;
145     p_dec->pf_end = EndDecoder;
146
147     /* Allocate the memory needed to store the decoder's structure */
148     if( ( p_dec->p_sys =
149           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
150     {
151         msg_Err( p_dec, "out of memory" );
152         return VLC_EGENERIC;
153     }
154     p_dec->p_sys->b_packetizer = VLC_FALSE;
155
156     return VLC_SUCCESS;
157 }
158
159 static int OpenPacketizer( vlc_object_t *p_this )
160 {
161     decoder_t *p_dec = (decoder_t*)p_this;
162
163     int i_ret = OpenDecoder( p_this );
164
165     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
166
167     return i_ret;
168 }
169
170 /*****************************************************************************
171  * InitDecoder: Initalize the decoder
172  *****************************************************************************/
173 static int InitDecoder( decoder_t *p_dec )
174 {
175     p_dec->p_sys->i_state = STATE_NOSYNC;
176
177     p_dec->p_sys->p_out_buffer = NULL;
178     p_dec->p_sys->i_out_buffer = 0;
179     aout_DateSet( &p_dec->p_sys->end_date, 0 );
180
181     p_dec->p_sys->p_aout = NULL;
182     p_dec->p_sys->p_aout_input = NULL;
183     p_dec->p_sys->p_aout_buffer = NULL;
184     p_dec->p_sys->aout_format.i_format = VLC_FOURCC('a','5','2',' ');
185
186     p_dec->p_sys->p_sout_input = NULL;
187     p_dec->p_sys->p_sout_buffer = NULL;
188     p_dec->p_sys->sout_format.i_cat = AUDIO_ES;
189     p_dec->p_sys->sout_format.i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
190
191     return VLC_SUCCESS;
192 }
193
194 /****************************************************************************
195  * RunDecoder: the whole thing
196  ****************************************************************************
197  * This function is called just after the thread is launched.
198  ****************************************************************************/
199 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
200 {
201     decoder_sys_t *p_sys = p_dec->p_sys;
202     int i_block_pos = 0;
203     mtime_t i_pts = p_block->i_pts;
204
205     while( i_block_pos < p_block->i_buffer )
206     {
207         switch( p_sys->i_state )
208         {
209         case STATE_NOSYNC:
210             /* Look for sync word - should be 0x0b77 */
211             while( i_block_pos < p_block->i_buffer &&
212                    p_block->p_buffer[i_block_pos] != 0x0b )
213             {
214                 i_block_pos++;
215             }
216
217             if( i_block_pos < p_block->i_buffer )
218             {
219                 p_sys->i_state = STATE_PARTIAL_SYNC;
220                 i_block_pos++;
221                 p_sys->p_header[0] = 0x0b;
222                 break;
223             }
224             break;
225
226         case STATE_PARTIAL_SYNC:
227             if( p_block->p_buffer[i_block_pos] == 0x77 )
228             {
229                 p_sys->i_state = STATE_SYNC;
230                 i_block_pos++;
231                 p_sys->p_header[1] = 0x77;
232                 p_sys->i_header = 2;
233             }
234             else
235             {
236                 p_sys->i_state = STATE_NOSYNC;
237             }
238             break;
239
240         case STATE_SYNC:
241             /* New frame, set the Presentation Time Stamp */
242             p_sys->pts = i_pts; i_pts = 0;
243             if( p_sys->pts != 0 &&
244                 p_sys->pts != aout_DateGet( &p_sys->end_date ) )
245             {
246                 aout_DateSet( &p_sys->end_date, p_sys->pts );
247             }
248             p_sys->i_state = STATE_HEADER;
249             break;
250
251         case STATE_HEADER:
252             /* Get A/52 frame header (A52_HEADER_SIZE bytes) */
253             if( p_sys->i_header < A52_HEADER_SIZE )
254             {
255                 int i_size = __MIN( A52_HEADER_SIZE - p_sys->i_header,
256                                     p_block->i_buffer - i_block_pos );
257
258                 memcpy( p_sys->p_header + p_sys->i_header,
259                         p_block->p_buffer + i_block_pos, i_size );
260                 i_block_pos += i_size;
261                 p_sys->i_header += i_size;
262             }
263
264             if( p_sys->i_header < A52_HEADER_SIZE )
265                 break;
266
267             if( GetOutBuffer( p_dec, &p_sys->p_out_buffer )
268                 != VLC_SUCCESS )
269             {
270                 block_Release( p_block );
271                 return VLC_EGENERIC;
272             }
273
274             if( !p_sys->p_out_buffer )
275             {
276                 p_sys->i_state = STATE_NOSYNC;
277                 break;
278             }
279
280             memcpy( p_sys->p_out_buffer, p_sys->p_header, A52_HEADER_SIZE );
281             p_sys->i_out_buffer = A52_HEADER_SIZE;
282             p_sys->i_state = STATE_DATA;
283             break;
284
285         case STATE_DATA:
286             /* Copy the whole A52 frame into the aout buffer */
287             if( p_sys->i_out_buffer < p_sys->i_frame_size )
288             {
289                 int i_size = __MIN( p_sys->i_frame_size - p_sys->i_out_buffer,
290                                     p_block->i_buffer - i_block_pos );
291
292                 memcpy( p_sys->p_out_buffer + p_sys->i_out_buffer,
293                         p_block->p_buffer + i_block_pos, i_size );
294                 i_block_pos += i_size;
295                 p_sys->i_out_buffer += i_size;
296             }
297
298             if( p_sys->i_out_buffer < p_sys->i_frame_size )
299                 break; /* Need more data */
300
301             SendOutBuffer( p_dec );
302
303             p_sys->i_state = STATE_NOSYNC;
304             break;
305         }
306     }
307
308     block_Release( p_block );
309     return VLC_SUCCESS;
310 }
311
312 /*****************************************************************************
313  * EndDecoder: clean up the decoder
314  *****************************************************************************/
315 static int EndDecoder( decoder_t *p_dec )
316 {
317     if( p_dec->p_sys->p_aout_input != NULL )
318     {
319         if( p_dec->p_sys->p_aout_buffer )
320         {
321             aout_DecDeleteBuffer( p_dec->p_sys->p_aout,
322                                   p_dec->p_sys->p_aout_input,
323                                   p_dec->p_sys->p_aout_buffer );
324         }
325
326         aout_DecDelete( p_dec->p_sys->p_aout, p_dec->p_sys->p_aout_input );
327     }
328
329     if( p_dec->p_sys->p_sout_input != NULL )
330     {
331         if( p_dec->p_sys->p_sout_buffer )
332         {
333             sout_BufferDelete( p_dec->p_sys->p_sout_input->p_sout,
334                                p_dec->p_sys->p_sout_buffer );
335         }
336
337         sout_InputDelete( p_dec->p_sys->p_sout_input );
338     }
339
340     free( p_dec->p_sys );
341
342     return VLC_SUCCESS;
343 }
344
345 /*****************************************************************************
346  * GetOutBuffer:
347  *****************************************************************************/
348 static int GetOutBuffer ( decoder_t *p_dec, uint8_t **pp_out_buffer )
349 {
350     decoder_sys_t *p_sys = p_dec->p_sys;
351     int i_ret;
352
353     if( p_sys->b_packetizer )
354     {
355         i_ret= GetSoutBuffer( p_dec, &p_sys->p_sout_buffer );
356         *pp_out_buffer =
357             p_sys->p_sout_buffer ? p_sys->p_sout_buffer->p_buffer : NULL;
358     }
359     else
360     {
361         i_ret = GetAoutBuffer( p_dec, &p_sys->p_aout_buffer );
362         *pp_out_buffer =
363             p_sys->p_aout_buffer ? p_sys->p_aout_buffer->p_buffer : NULL;
364     }
365
366     return i_ret;
367 }
368
369 /*****************************************************************************
370  * GetAoutBuffer:
371  *****************************************************************************/
372 static int GetAoutBuffer( decoder_t *p_dec, aout_buffer_t **pp_buffer )
373 {
374     int i_bit_rate;
375     unsigned int i_rate, i_channels, i_channels_conf;
376
377     decoder_sys_t *p_sys = p_dec->p_sys;
378
379     /* Check if frame is valid and get frame info */
380     p_sys->i_frame_size = SyncInfo( p_sys->p_header,
381                                     &i_channels, &i_channels_conf,
382                                     &i_rate, &i_bit_rate );
383
384     if( !p_sys->i_frame_size )
385     {
386         msg_Warn( p_dec, "a52 syncinfo failed" );
387         *pp_buffer = NULL;
388         return VLC_SUCCESS;
389     }
390
391     if( p_sys->p_aout_input != NULL && ( p_sys->aout_format.i_rate != i_rate
392         || p_sys->aout_format.i_original_channels != i_channels_conf
393         || (int)p_sys->aout_format.i_bytes_per_frame != p_sys->i_frame_size ) )
394     {
395         /* Parameters changed - this should not happen. */
396         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
397         p_sys->p_aout_input = NULL;
398     }
399
400     /* Creating the audio input if not created yet. */
401     if( p_sys->p_aout_input == NULL )
402     {
403         p_sys->aout_format.i_rate = i_rate;
404         p_sys->aout_format.i_original_channels = i_channels_conf;
405         p_sys->aout_format.i_physical_channels
406             = i_channels_conf & AOUT_CHAN_PHYSMASK;
407         p_sys->aout_format.i_bytes_per_frame = p_sys->i_frame_size;
408         p_sys->aout_format.i_frame_length = A52_FRAME_NB;
409         aout_DateInit( &p_sys->end_date, i_rate );
410         aout_DateSet( &p_sys->end_date, p_sys->pts );
411         p_sys->p_aout_input = aout_DecNew( p_dec,
412                                            &p_sys->p_aout,
413                                            &p_sys->aout_format );
414
415         if ( p_sys->p_aout_input == NULL )
416         {
417             *pp_buffer = NULL;
418             return VLC_SUCCESS;
419         }
420     }
421
422     if( !aout_DateGet( &p_sys->end_date ) )
423     {
424         /* We've just started the stream, wait for the first PTS. */
425         *pp_buffer = NULL;
426         return VLC_SUCCESS;
427     }
428
429     *pp_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
430                                     A52_FRAME_NB );
431     if( *pp_buffer == NULL )
432     {
433         return VLC_SUCCESS;
434     }
435
436     (*pp_buffer)->start_date = aout_DateGet( &p_sys->end_date );
437     (*pp_buffer)->end_date =
438          aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB );
439
440     return VLC_SUCCESS;
441 }
442
443 /*****************************************************************************
444  * GetSoutBuffer:
445  *****************************************************************************/
446 static int GetSoutBuffer( decoder_t *p_dec, sout_buffer_t **pp_buffer )
447 {
448     int i_bit_rate;
449     unsigned int i_rate, i_channels, i_channels_conf;
450
451     decoder_sys_t *p_sys = p_dec->p_sys;
452
453     /* Check if frame is valid and get frame info */
454     p_sys->i_frame_size = SyncInfo( p_sys->p_header,
455                                     &i_channels, &i_channels_conf,
456                                     &i_rate, &i_bit_rate );
457
458     if( !p_sys->i_frame_size )
459     {
460         msg_Warn( p_dec, "a52 syncinfo failed" );
461         *pp_buffer = NULL;
462         return VLC_SUCCESS;
463     }
464
465     if( p_sys->p_sout_input != NULL &&
466         ( p_sys->sout_format.i_sample_rate != (int)i_rate
467           || p_sys->sout_format.i_channels != (int)i_channels ) )
468     {
469         /* Parameters changed - this should not happen. */
470     }
471
472     /* Creating the sout input if not created yet. */
473     if( p_sys->p_sout_input == NULL )
474     {
475         p_sys->sout_format.i_sample_rate = i_rate;
476         p_sys->sout_format.i_channels    = i_channels;
477         p_sys->sout_format.i_block_align = 0;
478         p_sys->sout_format.i_bitrate     = i_bit_rate;
479         p_sys->sout_format.i_extra_data  = 0;
480         p_sys->sout_format.p_extra_data  = NULL;
481
482         aout_DateInit( &p_sys->end_date, i_rate );
483         aout_DateSet( &p_sys->end_date, p_sys->pts );
484
485         p_sys->p_sout_input = sout_InputNew( p_dec,
486                                              &p_sys->sout_format );
487
488         if( p_sys->p_sout_input == NULL )
489         {
490             msg_Err( p_dec, "cannot add a new stream" );
491             *pp_buffer = NULL;
492             return VLC_EGENERIC;
493         }
494         msg_Info( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
495                   i_channels, i_rate, i_bit_rate );
496     }
497
498     if( !aout_DateGet( &p_sys->end_date ) )
499     {
500         /* We've just started the stream, wait for the first PTS. */
501         *pp_buffer = NULL;
502         return VLC_SUCCESS;
503     }
504
505     *pp_buffer = sout_BufferNew( p_sys->p_sout_input->p_sout,
506                                  p_sys->i_frame_size );
507     if( *pp_buffer == NULL )
508     {
509         return VLC_SUCCESS;
510     }
511
512     (*pp_buffer)->i_pts =
513         (*pp_buffer)->i_dts = aout_DateGet( &p_sys->end_date );
514
515     (*pp_buffer)->i_length =
516         aout_DateIncrement( &p_sys->end_date, A52_FRAME_NB )
517         - (*pp_buffer)->i_pts;
518
519     return VLC_SUCCESS;
520 }
521
522 /*****************************************************************************
523  * SendOutBuffer:
524  *****************************************************************************/
525 static int SendOutBuffer( decoder_t *p_dec )
526 {
527     decoder_sys_t *p_sys = p_dec->p_sys;
528
529     if( p_sys->b_packetizer )
530     {
531         sout_InputSendBuffer( p_sys->p_sout_input, p_sys->p_sout_buffer );
532         p_sys->p_sout_buffer = NULL;
533     }
534     else
535     {
536         /* We have all we need, send the buffer to the aout core. */
537         aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input,
538                       p_sys->p_aout_buffer );
539         p_sys->p_aout_buffer = NULL;
540     }
541
542     return VLC_SUCCESS;
543 }
544
545 /*****************************************************************************
546  * SyncInfo: parse A/52 sync info
547  *****************************************************************************
548  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
549  * since we don't want to oblige S/PDIF people to use liba52 just to get
550  * their SyncInfo...
551  *****************************************************************************/
552 static int SyncInfo( const byte_t * p_buf,
553                      int * pi_channels, int * pi_channels_conf,
554                      int * pi_sample_rate, int * pi_bit_rate )
555 {
556     static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
557     static const int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
558                                 128, 160, 192, 224, 256, 320, 384, 448,
559                                 512, 576, 640 };
560     static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
561                                       0x04, 0x01, 0x04, 0x01 };
562     int frmsizecod;
563     int bitrate;
564     int half;
565     int acmod;
566
567     if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77))        /* syncword */
568         return 0;
569
570     if (p_buf[5] >= 0x60)                /* bsid >= 12 */
571         return 0;
572     half = halfrate[p_buf[5] >> 3];
573
574     /* acmod, dsurmod and lfeon */
575     acmod = p_buf[6] >> 5;
576     if ( (p_buf[6] & 0xf8) == 0x50 )
577     {
578         /* Dolby surround = stereo + Dolby */
579         *pi_channels = 2;
580         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
581                             | AOUT_CHAN_DOLBYSTEREO;
582     }
583     else switch ( acmod )
584     {
585     case 0x0:
586         /* Dual-mono = stereo + dual-mono */
587         *pi_channels = 2;
588         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
589                             | AOUT_CHAN_DUALMONO;
590         break;
591     case 0x1:
592         /* Mono */
593         *pi_channels = 1;
594         *pi_channels_conf = AOUT_CHAN_CENTER;
595         break;
596     case 0x2:
597         /* Stereo */
598         *pi_channels = 2;
599         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
600         break;
601     case 0x3:
602         /* 3F */
603         *pi_channels = 3;
604         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
605                             | AOUT_CHAN_CENTER;
606         break;
607     case 0x4:
608         /* 2F1R */
609         *pi_channels = 3;
610         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
611                             | AOUT_CHAN_REARCENTER;
612         break;
613     case 0x5:
614         /* 3F1R */
615         *pi_channels = 4;
616         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
617                             | AOUT_CHAN_REARCENTER;
618         break;
619     case 0x6:
620         /* 2F2R */
621         *pi_channels = 4;
622         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
623                             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
624         break;
625     case 0x7:
626         /* 3F2R */
627         *pi_channels = 5;
628         *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
629                             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
630         break;
631     default:
632         return 0;
633     }
634
635     if ( p_buf[6] & lfeon[acmod] )
636     {
637         (*pi_channels)++;
638         *pi_channels_conf |= AOUT_CHAN_LFE;
639     }
640
641     frmsizecod = p_buf[4] & 63;
642     if (frmsizecod >= 38)
643         return 0;
644     bitrate = rate [frmsizecod >> 1];
645     *pi_bit_rate = (bitrate * 1000) >> half;
646
647     switch (p_buf[4] & 0xc0) {
648     case 0:
649         *pi_sample_rate = 48000 >> half;
650         return 4 * bitrate;
651     case 0x40:
652         *pi_sample_rate = 44100 >> half;
653         return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
654     case 0x80:
655         *pi_sample_rate = 32000 >> half;
656         return 6 * bitrate;
657     default:
658         return 0;
659     }
660 }