4a91f2b3ee83ce1e65b6690c2a8682789a3fcf15
[vlc.git] / modules / packetizer / a52.c
1 /*****************************************************************************
2  * a52.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: a52.c,v 1.2 2003/03/11 19:02:30 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/aout.h>
30 #include <vlc/decoder.h>
31 #include <vlc/input.h>
32 #include <vlc/sout.h>
33
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <string.h>                                              /* strdup() */
36 #include "codecs.h"                         /* WAVEFORMATEX BITMAPINFOHEADER */
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 typedef struct packetizer_s
41 {
42     /* Input properties */
43     decoder_fifo_t          *p_fifo;
44     bit_stream_t            bit_stream;
45
46     /* Output properties */
47     sout_packetizer_input_t *p_sout_input;
48     sout_packet_format_t    output_format;
49
50     uint64_t                i_samplescount;
51
52 } packetizer_t;
53
54 static int  Open    ( vlc_object_t * );
55 static int  Run     ( decoder_fifo_t * );
56
57 static int  InitThread     ( packetizer_t * );
58 static void PacketizeThread   ( packetizer_t * );
59 static void EndThread      ( packetizer_t * );
60
61 static int SyncInfo( const byte_t *, int *, int *, int * );
62
63 #define FREE( p ) if( p ) free( p ); p = NULL
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68
69 vlc_module_begin();
70     set_description( _("A/52 audio packetizer") );
71     set_capability( "packetizer", 10 );
72     set_callbacks( Open, NULL );
73 vlc_module_end();
74
75
76
77 /*****************************************************************************
78  * OpenDecoder: probe the packetizer and return score
79  *****************************************************************************
80  * Tries to launch a decoder and return score so that the interface is able
81  * to choose.
82  *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
84 {
85     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
86
87     if( p_fifo->i_fourcc != VLC_FOURCC( 'a', '5', '2', ' ') &&
88         p_fifo->i_fourcc != VLC_FOURCC( 'a', '5', '2', 'b') )
89     {
90         return VLC_EGENERIC;
91     }
92
93     p_fifo->pf_run = Run;
94     return VLC_SUCCESS;
95 }
96
97 /*****************************************************************************
98  * RunDecoder: this function is called just after the thread is created
99  *****************************************************************************/
100 static int Run( decoder_fifo_t *p_fifo )
101 {
102     packetizer_t *p_pack;
103     int b_error;
104
105     msg_Info( p_fifo, "Running A/52 packetizer" );
106     if( !( p_pack = malloc( sizeof( packetizer_t ) ) ) )
107     {
108         msg_Err( p_fifo, "out of memory" );
109         DecoderError( p_fifo );
110         return( -1 );
111     }
112     memset( p_pack, 0, sizeof( packetizer_t ) );
113
114     p_pack->p_fifo = p_fifo;
115
116     if( InitThread( p_pack ) != 0 )
117     {
118         DecoderError( p_fifo );
119         return( -1 );
120     }
121
122     while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
123     {
124         PacketizeThread( p_pack );
125     }
126
127
128     if( ( b_error = p_pack->p_fifo->b_error ) )
129     {
130         DecoderError( p_pack->p_fifo );
131     }
132
133     EndThread( p_pack );
134
135     FREE( p_pack );
136
137     if( b_error )
138     {
139         return( -1 );
140     }
141
142     return( 0 );
143 }
144
145
146
147 /*****************************************************************************
148  * InitThread: initialize data before entering main loop
149  *****************************************************************************/
150
151 static int InitThread( packetizer_t *p_pack )
152 {
153
154     p_pack->output_format.i_cat = AUDIO_ES;
155     p_pack->output_format.i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
156
157     p_pack->output_format.p_format = NULL;
158
159     p_pack->p_sout_input = NULL;
160
161     p_pack->i_samplescount = 0;
162
163     if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
164                        NULL, NULL ) != VLC_SUCCESS )
165     {
166         msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
167         return -1;
168     }
169
170     return( 0 );
171 }
172
173 /*****************************************************************************
174  * PacketizeThread: packetize an unit (here copy a complete pes)
175  *****************************************************************************/
176 static void PacketizeThread( packetizer_t *p_pack )
177 {
178     sout_buffer_t   *p_sout_buffer;
179
180
181     uint8_t p_header[7];
182     int i_channels, i_samplerate, i_bitrate;
183     int i_framelength;
184
185     /* search a valid start code */
186     for( ;; )
187     {
188         int i_skip = 0;
189
190         RealignBits( &p_pack->bit_stream );
191         while( ShowBits( &p_pack->bit_stream, 16 ) != 0x0b77 &&
192                !p_pack->p_fifo->b_die && !p_pack->p_fifo->b_error )
193         {
194             RemoveBits( &p_pack->bit_stream, 8 );
195             i_skip++;
196         }
197         if( i_skip )
198         {
199             msg_Warn( p_pack->p_fifo, "trashing %d bytes", i_skip );
200         }
201         if( p_pack->p_fifo->b_die || p_pack->p_fifo->b_error )
202         {
203             return;
204         }
205
206         GetChunk( &p_pack->bit_stream, p_header, 7 );
207         if( p_pack->p_fifo->b_die ) return;
208
209         /* Check if frame is valid and get frame info */
210         i_framelength = SyncInfo( p_header, &i_channels, &i_samplerate,
211                                   &i_bitrate );
212
213         if( !i_framelength )
214         {
215             msg_Warn( p_pack->p_fifo, "invalid header found" );
216             continue;
217         }
218         else
219         {
220 //            msg_Dbg( p_pack->p_fifo, "frame length %d b", i_framelength );
221             break;
222         }
223     }
224
225     if( !p_pack->p_sout_input )
226     {
227         /* add a input for the stream ouput */
228         WAVEFORMATEX    *p_wf;
229
230         p_wf = malloc( sizeof( WAVEFORMATEX ) );
231         p_pack->output_format.p_format = (void*)p_wf;
232
233         p_wf->wFormatTag        = WAVE_FORMAT_A52;
234         p_wf->nChannels         = i_channels;
235         p_wf->nSamplesPerSec    = i_samplerate;
236         p_wf->nAvgBytesPerSec   = 0;
237         p_wf->nBlockAlign       = 1;
238         p_wf->wBitsPerSample    = 0;
239         p_wf->cbSize            = 0;
240
241         p_pack->p_sout_input =
242             sout_InputNew( p_pack->p_fifo,
243                            &p_pack->output_format );
244
245         if( !p_pack->p_sout_input )
246         {
247             msg_Err( p_pack->p_fifo,
248                      "cannot add a new stream" );
249             p_pack->p_fifo->b_error = 1;
250             return;
251         }
252         msg_Info( p_pack->p_fifo,
253                  "A/52 channels:%d samplerate:%d bitrate:%d",
254                  i_channels, i_samplerate, i_bitrate );
255     }
256
257     p_sout_buffer =
258         sout_BufferNew( p_pack->p_sout_input->p_sout, i_framelength );
259     if( !p_sout_buffer )
260     {
261         p_pack->p_fifo->b_error = 1;
262         return;
263     }
264     memcpy( p_sout_buffer->p_buffer, p_header, 7 );
265     p_sout_buffer->i_bitrate = i_bitrate;
266
267     p_sout_buffer->i_pts =
268         p_sout_buffer->i_dts =
269             (uint64_t)1000000 *
270             (uint64_t)p_pack->i_samplescount /
271             (uint64_t)i_samplerate;
272     p_sout_buffer->i_length =
273             (uint64_t)1000000 *
274             (uint64_t)A52_FRAME_NB /
275             (uint64_t)i_samplerate;
276
277     p_pack->i_samplescount += A52_FRAME_NB;
278
279     /* we are already aligned */
280     GetChunk( &p_pack->bit_stream,
281               p_sout_buffer->p_buffer + 7,
282               i_framelength - 7 );
283
284     sout_InputSendBuffer( p_pack->p_sout_input,
285                           p_sout_buffer );
286 }
287
288
289 /*****************************************************************************
290  * EndThread : packetizer thread destruction
291  *****************************************************************************/
292 static void EndThread ( packetizer_t *p_pack)
293 {
294     if( p_pack->p_sout_input )
295     {
296         sout_InputDelete( p_pack->p_sout_input );
297     }
298 }
299
300 /*****************************************************************************
301  * SyncInfo: parse A52 sync info
302  *****************************************************************************
303  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
304  * since we don't want to oblige S/PDIF people to use liba52 just to get
305  * their SyncInfo...
306  * fenrir: I've change it to report channels count
307  *****************************************************************************/
308 static int SyncInfo( const byte_t * p_buf, int * pi_channels,
309                      int * pi_sample_rate, int * pi_bit_rate )
310 {
311     static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
312     static const int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
313                                 128, 160, 192, 224, 256, 320, 384, 448,
314                                 512, 576, 640 };
315     static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
316                                       0x04, 0x01, 0x04, 0x01 };
317     int frmsizecod;
318     int bitrate;
319     int half;
320     int acmod;
321
322     if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77))        /* syncword */
323         return 0;
324
325     if (p_buf[5] >= 0x60)                /* bsid >= 12 */
326         return 0;
327     half = halfrate[p_buf[5] >> 3];
328
329     /* acmod, dsurmod and lfeon */
330     acmod = p_buf[6] >> 5;
331     if ( (p_buf[6] & 0xf8) == 0x50 )
332     {
333         /* Dolby surround = stereo + Dolby */
334 /*        *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
335                         | AOUT_CHAN_DOLBYSTEREO;*/
336         *pi_channels = 2; /* FIXME ???  */
337     }
338     else switch ( acmod )
339     {
340     case 0x0:
341         /* Dual-mono = stereo + dual-mono */
342 //        *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
343 //                        | AOUT_CHAN_DUALMONO;
344         *pi_channels = 2; /* FIXME ???  */
345         break;
346     case 0x1:
347         /* Mono */
348         *pi_channels = 1;
349         break;
350     case 0x2:
351         /* Stereo */
352         *pi_channels = 2;
353         break;
354     case 0x3: /* 3F */
355     case 0x4: /* 2F1R */
356         *pi_channels = 3;
357         break;
358     case 0x5: /* 3F1R */
359     case 0x6: /* 2F2R */
360         *pi_channels = 4;
361         break;
362     case 0x7:
363         /* 3F2R */
364         *pi_channels = 5;
365         break;
366     default:
367         return 0;
368     }
369
370     if ( p_buf[6] & lfeon[acmod] ) *pi_channels += 1;//|= AOUT_CHAN_LFEA;
371
372     frmsizecod = p_buf[4] & 63;
373     if (frmsizecod >= 38)
374         return 0;
375     bitrate = rate [frmsizecod >> 1];
376     *pi_bit_rate = (bitrate * 1000) >> half;
377
378     switch (p_buf[4] & 0xc0) {
379     case 0:
380         *pi_sample_rate = 48000 >> half;
381         return 4 * bitrate;
382     case 0x40:
383         *pi_sample_rate = 44100 >> half;
384         return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
385     case 0x80:
386         *pi_sample_rate = 32000 >> half;
387         return 6 * bitrate;
388     default:
389         return 0;
390     }
391 }
392
393