]> git.sesse.net Git - vlc/blob - modules/packetizer/a52.c
11cbded978e7dea568fd6e2d3bc096486a1abe49
[vlc] / modules / packetizer / a52.c
1 /*****************************************************************************
2  * a52.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: a52.c,v 1.3 2003/03/31 03:46:11 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     mtime_t                 i_last_pts;
53 } packetizer_t;
54
55 static int  Open    ( vlc_object_t * );
56 static int  Run     ( decoder_fifo_t * );
57
58 static int  InitThread     ( packetizer_t * );
59 static void PacketizeThread   ( packetizer_t * );
60 static void EndThread      ( packetizer_t * );
61
62 static int SyncInfo( const byte_t *, int *, int *, int * );
63
64 #define FREE( p ) if( p ) free( p ); p = NULL
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69
70 vlc_module_begin();
71     set_description( _("A/52 audio packetizer") );
72     set_capability( "packetizer", 10 );
73     set_callbacks( Open, NULL );
74 vlc_module_end();
75
76
77
78 /*****************************************************************************
79  * OpenDecoder: probe the packetizer and return score
80  *****************************************************************************
81  * Tries to launch a decoder and return score so that the interface is able
82  * to choose.
83  *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
85 {
86     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
87
88     if( p_fifo->i_fourcc != VLC_FOURCC( 'a', '5', '2', ' ') &&
89         p_fifo->i_fourcc != VLC_FOURCC( 'a', '5', '2', 'b') )
90     {
91         return VLC_EGENERIC;
92     }
93
94     p_fifo->pf_run = Run;
95     return VLC_SUCCESS;
96 }
97
98 /*****************************************************************************
99  * RunDecoder: this function is called just after the thread is created
100  *****************************************************************************/
101 static int Run( decoder_fifo_t *p_fifo )
102 {
103     packetizer_t *p_pack;
104     int b_error;
105
106     msg_Info( p_fifo, "Running A/52 packetizer" );
107     if( !( p_pack = malloc( sizeof( packetizer_t ) ) ) )
108     {
109         msg_Err( p_fifo, "out of memory" );
110         DecoderError( p_fifo );
111         return( -1 );
112     }
113     memset( p_pack, 0, sizeof( packetizer_t ) );
114
115     p_pack->p_fifo = p_fifo;
116
117     if( InitThread( p_pack ) != 0 )
118     {
119         DecoderError( p_fifo );
120         return( -1 );
121     }
122
123     while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
124     {
125         PacketizeThread( p_pack );
126     }
127
128
129     if( ( b_error = p_pack->p_fifo->b_error ) )
130     {
131         DecoderError( p_pack->p_fifo );
132     }
133
134     EndThread( p_pack );
135
136     FREE( p_pack );
137
138     if( b_error )
139     {
140         return( -1 );
141     }
142
143     return( 0 );
144 }
145
146
147
148 /*****************************************************************************
149  * InitThread: initialize data before entering main loop
150  *****************************************************************************/
151
152 static int InitThread( packetizer_t *p_pack )
153 {
154
155     p_pack->output_format.i_cat = AUDIO_ES;
156     p_pack->output_format.i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
157
158     p_pack->output_format.p_format = NULL;
159
160     p_pack->p_sout_input   = NULL;
161
162     p_pack->i_samplescount = 0;
163     p_pack->i_last_pts     = 0;
164
165     if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
166                        NULL, NULL ) != VLC_SUCCESS )
167     {
168         msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
169         return -1;
170     }
171
172     return( 0 );
173 }
174
175 /*****************************************************************************
176  * PacketizeThread: packetize an unit (here copy a complete pes)
177  *****************************************************************************/
178 static void PacketizeThread( packetizer_t *p_pack )
179 {
180     sout_buffer_t   *p_sout_buffer;
181
182
183     uint8_t p_header[7];
184     int     i_channels, i_samplerate, i_bitrate;
185     int     i_framelength;
186     mtime_t i_pts;
187
188     /* search a valid start code */
189     for( ;; )
190     {
191         int i_skip = 0;
192
193         RealignBits( &p_pack->bit_stream );
194         while( ShowBits( &p_pack->bit_stream, 16 ) != 0x0b77 &&
195                !p_pack->p_fifo->b_die && !p_pack->p_fifo->b_error )
196         {
197             RemoveBits( &p_pack->bit_stream, 8 );
198             i_skip++;
199         }
200         if( i_skip )
201         {
202             msg_Warn( p_pack->p_fifo, "trashing %d bytes", i_skip );
203         }
204         if( p_pack->p_fifo->b_die || p_pack->p_fifo->b_error )
205         {
206             return;
207         }
208
209         /* Set the Presentation Time Stamp */
210         NextPTS( &p_pack->bit_stream, &i_pts, NULL );
211
212         GetChunk( &p_pack->bit_stream, p_header, 7 );
213         if( p_pack->p_fifo->b_die ) return;
214
215         /* Check if frame is valid and get frame info */
216         i_framelength = SyncInfo( p_header, &i_channels, &i_samplerate,
217                                   &i_bitrate );
218
219         if( !i_framelength )
220         {
221             msg_Warn( p_pack->p_fifo, "invalid header found" );
222             continue;
223         }
224         else
225         {
226 //            msg_Dbg( p_pack->p_fifo, "frame length %d b", i_framelength );
227             break;
228         }
229     }
230
231     if( !p_pack->p_sout_input )
232     {
233         /* add a input for the stream ouput */
234         WAVEFORMATEX    *p_wf;
235
236         p_wf = malloc( sizeof( WAVEFORMATEX ) );
237         p_pack->output_format.p_format = (void*)p_wf;
238
239         p_wf->wFormatTag        = WAVE_FORMAT_A52;
240         p_wf->nChannels         = i_channels;
241         p_wf->nSamplesPerSec    = i_samplerate;
242         p_wf->nAvgBytesPerSec   = 0;
243         p_wf->nBlockAlign       = 1;
244         p_wf->wBitsPerSample    = 0;
245         p_wf->cbSize            = 0;
246
247         p_pack->p_sout_input =
248             sout_InputNew( p_pack->p_fifo,
249                            &p_pack->output_format );
250
251         if( !p_pack->p_sout_input )
252         {
253             msg_Err( p_pack->p_fifo,
254                      "cannot add a new stream" );
255             p_pack->p_fifo->b_error = 1;
256             return;
257         }
258         msg_Info( p_pack->p_fifo,
259                  "A/52 channels:%d samplerate:%d bitrate:%d",
260                  i_channels, i_samplerate, i_bitrate );
261     }
262
263     if( i_pts <= 0 && p_pack->i_last_pts <= 0 )
264     {
265         msg_Dbg( p_pack->p_fifo, "need a starting pts" );
266         return;
267     }
268
269     /* fix pts */
270     if( i_pts <= 0 )
271     {
272         i_pts = p_pack->i_last_pts +
273                     (uint64_t)1000000 *
274                     (uint64_t)A52_FRAME_NB /
275                     (uint64_t)i_samplerate;
276     }
277     p_pack->i_last_pts = i_pts;
278
279     p_sout_buffer =
280         sout_BufferNew( p_pack->p_sout_input->p_sout, i_framelength );
281     if( !p_sout_buffer )
282     {
283         p_pack->p_fifo->b_error = 1;
284         return;
285     }
286     memcpy( p_sout_buffer->p_buffer, p_header, 7 );
287     p_sout_buffer->i_bitrate = i_bitrate;
288
289     p_sout_buffer->i_pts =
290         p_sout_buffer->i_dts = i_pts;
291
292     p_sout_buffer->i_length =
293             (uint64_t)1000000 *
294             (uint64_t)A52_FRAME_NB /
295             (uint64_t)i_samplerate;
296
297     p_pack->i_samplescount += A52_FRAME_NB;
298
299     /* we are already aligned */
300     GetChunk( &p_pack->bit_stream,
301               p_sout_buffer->p_buffer + 7,
302               i_framelength - 7 );
303
304     sout_InputSendBuffer( p_pack->p_sout_input,
305                           p_sout_buffer );
306 }
307
308
309 /*****************************************************************************
310  * EndThread : packetizer thread destruction
311  *****************************************************************************/
312 static void EndThread ( packetizer_t *p_pack)
313 {
314     if( p_pack->p_sout_input )
315     {
316         sout_InputDelete( p_pack->p_sout_input );
317     }
318 }
319
320 /*****************************************************************************
321  * SyncInfo: parse A52 sync info
322  *****************************************************************************
323  * This code is borrowed from liba52 by Aaron Holtzman & Michel Lespinasse,
324  * since we don't want to oblige S/PDIF people to use liba52 just to get
325  * their SyncInfo...
326  * fenrir: I've change it to report channels count
327  *****************************************************************************/
328 static int SyncInfo( const byte_t * p_buf, int * pi_channels,
329                      int * pi_sample_rate, int * pi_bit_rate )
330 {
331     static const uint8_t halfrate[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 };
332     static const int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
333                                 128, 160, 192, 224, 256, 320, 384, 448,
334                                 512, 576, 640 };
335     static const uint8_t lfeon[8] = { 0x10, 0x10, 0x04, 0x04,
336                                       0x04, 0x01, 0x04, 0x01 };
337     int frmsizecod;
338     int bitrate;
339     int half;
340     int acmod;
341
342     if ((p_buf[0] != 0x0b) || (p_buf[1] != 0x77))        /* syncword */
343         return 0;
344
345     if (p_buf[5] >= 0x60)                /* bsid >= 12 */
346         return 0;
347     half = halfrate[p_buf[5] >> 3];
348
349     /* acmod, dsurmod and lfeon */
350     acmod = p_buf[6] >> 5;
351     if ( (p_buf[6] & 0xf8) == 0x50 )
352     {
353         /* Dolby surround = stereo + Dolby */
354 /*        *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
355                         | AOUT_CHAN_DOLBYSTEREO;*/
356         *pi_channels = 2; /* FIXME ???  */
357     }
358     else switch ( acmod )
359     {
360     case 0x0:
361         /* Dual-mono = stereo + dual-mono */
362 //        *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
363 //                        | AOUT_CHAN_DUALMONO;
364         *pi_channels = 2; /* FIXME ???  */
365         break;
366     case 0x1:
367         /* Mono */
368         *pi_channels = 1;
369         break;
370     case 0x2:
371         /* Stereo */
372         *pi_channels = 2;
373         break;
374     case 0x3: /* 3F */
375     case 0x4: /* 2F1R */
376         *pi_channels = 3;
377         break;
378     case 0x5: /* 3F1R */
379     case 0x6: /* 2F2R */
380         *pi_channels = 4;
381         break;
382     case 0x7:
383         /* 3F2R */
384         *pi_channels = 5;
385         break;
386     default:
387         return 0;
388     }
389
390     if ( p_buf[6] & lfeon[acmod] ) *pi_channels += 1;//|= AOUT_CHAN_LFEA;
391
392     frmsizecod = p_buf[4] & 63;
393     if (frmsizecod >= 38)
394         return 0;
395     bitrate = rate [frmsizecod >> 1];
396     *pi_bit_rate = (bitrate * 1000) >> half;
397
398     switch (p_buf[4] & 0xc0) {
399     case 0:
400         *pi_sample_rate = 48000 >> half;
401         return 4 * bitrate;
402     case 0x40:
403         *pi_sample_rate = 44100 >> half;
404         return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
405     case 0x80:
406         *pi_sample_rate = 32000 >> half;
407         return 6 * bitrate;
408     default:
409         return 0;
410     }
411 }
412
413