]> git.sesse.net Git - vlc/blob - modules/codec/a52.c
Merge commit 'origin/1.0-bugfix'
[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_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_aout.h>
37 #include <vlc_block_helper.h>
38
39 #include "a52.h"
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  OpenDecoder   ( vlc_object_t * );
45 static int  OpenPacketizer( vlc_object_t * );
46 static void CloseCommon   ( vlc_object_t * );
47
48 vlc_module_begin ()
49     set_description( N_("A/52 parser") )
50     set_capability( "decoder", 100 )
51     set_callbacks( OpenDecoder, CloseCommon )
52     set_category( CAT_INPUT )
53     set_subcategory( SUBCAT_INPUT_ACODEC )
54
55     add_submodule ()
56     set_description( N_("A/52 audio packetizer") )
57     set_capability( "packetizer", 10 )
58     set_callbacks( OpenPacketizer, CloseCommon )
59 vlc_module_end ()
60
61 /*****************************************************************************
62  * decoder_sys_t : decoder descriptor
63  *****************************************************************************/
64
65 struct decoder_sys_t
66 {
67     /* Module mode */
68     bool b_packetizer;
69
70     /*
71      * Input properties
72      */
73     int i_state;
74
75     block_bytestream_t bytestream;
76
77     /*
78      * Common properties
79      */
80     audio_date_t   end_date;
81
82     mtime_t i_pts;
83
84     vlc_a52_header_t frame;
85 };
86
87 enum {
88
89     STATE_NOSYNC,
90     STATE_SYNC,
91     STATE_HEADER,
92     STATE_NEXT_SYNC,
93     STATE_GET_DATA,
94     STATE_SEND_DATA
95 };
96
97 /****************************************************************************
98  * Local prototypes
99  ****************************************************************************/
100 static void *DecodeBlock  ( decoder_t *, block_t ** );
101
102 static uint8_t       *GetOutBuffer ( decoder_t *, void ** );
103 static aout_buffer_t *GetAoutBuffer( decoder_t * );
104 static block_t       *GetSoutBuffer( decoder_t * );
105
106 /*****************************************************************************
107  * OpenCommon: probe the decoder/packetizer and return score
108  *****************************************************************************/
109 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
110 {
111     decoder_t *p_dec = (decoder_t*)p_this;
112     decoder_sys_t *p_sys;
113     vlc_fourcc_t i_codec;
114
115     switch( p_dec->fmt_in.i_codec )
116     {
117     case VLC_CODEC_A52:
118         i_codec = VLC_CODEC_A52;
119         break;
120     case VLC_CODEC_EAC3:
121         /* XXX ugly hack, a52 does not support eac3 so no eac3 pass-through
122          * support */
123         if( !b_packetizer )
124             return VLC_EGENERIC;
125         i_codec = VLC_CODEC_EAC3;
126         break;
127     default:
128         return VLC_EGENERIC;
129     }
130
131     /* Allocate the memory needed to store the decoder's structure */
132     if( ( p_dec->p_sys = p_sys =
133           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
134         return VLC_ENOMEM;
135
136     /* Misc init */
137     p_sys->b_packetizer = b_packetizer;
138     p_sys->i_state = STATE_NOSYNC;
139     aout_DateSet( &p_sys->end_date, 0 );
140
141     p_sys->bytestream = block_BytestreamInit();
142
143     /* Set output properties */
144     p_dec->fmt_out.i_cat = AUDIO_ES;
145     p_dec->fmt_out.i_codec = i_codec;
146     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
147     p_dec->fmt_out.audio.i_bytes_per_frame = 0;
148
149     /* Set callback */
150     if( b_packetizer )
151         p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
152             DecodeBlock;
153     else
154         p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
155             DecodeBlock;
156     return VLC_SUCCESS;
157 }
158
159 static int OpenDecoder( vlc_object_t *p_this )
160 {
161     return OpenCommon( p_this, false );
162 }
163
164 static int OpenPacketizer( vlc_object_t *p_this )
165 {
166     return OpenCommon( p_this, true );
167 }
168
169 /****************************************************************************
170  * DecodeBlock: the whole thing
171  ****************************************************************************
172  * This function is called just after the thread is launched.
173  ****************************************************************************/
174 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
175 {
176     decoder_sys_t *p_sys = p_dec->p_sys;
177     uint8_t p_header[VLC_A52_HEADER_SIZE];
178     uint8_t *p_buf;
179     void *p_out_buffer;
180
181     if( !pp_block || !*pp_block ) return NULL;
182
183     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
184     {
185         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
186         {
187             p_sys->i_state = STATE_NOSYNC;
188             block_BytestreamEmpty( &p_sys->bytestream );
189         }
190         aout_DateSet( &p_sys->end_date, 0 );
191         block_Release( *pp_block );
192         return NULL;
193     }
194
195     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
196     {
197         /* We've just started the stream, wait for the first PTS. */
198         block_Release( *pp_block );
199         return NULL;
200     }
201
202     block_BytestreamPush( &p_sys->bytestream, *pp_block );
203
204     while( 1 )
205     {
206         switch( p_sys->i_state )
207         {
208         case STATE_NOSYNC:
209             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
210                    == VLC_SUCCESS )
211             {
212                 if( p_header[0] == 0x0b && p_header[1] == 0x77 )
213                 {
214                     p_sys->i_state = STATE_SYNC;
215                     break;
216                 }
217                 block_SkipByte( &p_sys->bytestream );
218             }
219             if( p_sys->i_state != STATE_SYNC )
220             {
221                 block_BytestreamFlush( &p_sys->bytestream );
222
223                 /* Need more data */
224                 return NULL;
225             }
226
227         case STATE_SYNC:
228             /* New frame, set the Presentation Time Stamp */
229             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
230             if( p_sys->i_pts != 0 &&
231                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
232             {
233                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
234             }
235             p_sys->i_state = STATE_HEADER;
236
237         case STATE_HEADER:
238             /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */
239             if( block_PeekBytes( &p_sys->bytestream, p_header,
240                                  VLC_A52_HEADER_SIZE ) != VLC_SUCCESS )
241             {
242                 /* Need more data */
243                 return NULL;
244             }
245
246             /* Check if frame is valid and get frame info */
247             if( vlc_a52_header_Parse( &p_sys->frame, p_header, VLC_A52_HEADER_SIZE ) )
248             {
249                 msg_Dbg( p_dec, "emulated sync word" );
250                 block_SkipByte( &p_sys->bytestream );
251                 p_sys->i_state = STATE_NOSYNC;
252                 break;
253             }
254
255             p_sys->i_state = STATE_NEXT_SYNC;
256
257         case STATE_NEXT_SYNC:
258             /* TODO: If pp_block == NULL, flush the buffer without checking the
259              * next sync word */
260
261             /* Check if next expected frame contains the sync word */
262             if( block_PeekOffsetBytes( &p_sys->bytestream,
263                                        p_sys->frame.i_size, p_header, 2 )
264                 != VLC_SUCCESS )
265             {
266                 /* Need more data */
267                 return NULL;
268             }
269
270             if( p_sys->b_packetizer &&
271                 p_header[0] == 0 && p_header[1] == 0 )
272             {
273                 /* A52 wav files and audio CD's use stuffing */
274                 p_sys->i_state = STATE_GET_DATA;
275                 break;
276             }
277
278             if( p_header[0] != 0x0b || p_header[1] != 0x77 )
279             {
280                 msg_Dbg( p_dec, "emulated sync word "
281                          "(no sync on following frame)" );
282                 p_sys->i_state = STATE_NOSYNC;
283                 block_SkipByte( &p_sys->bytestream );
284                 break;
285             }
286             p_sys->i_state = STATE_SEND_DATA;
287             break;
288
289         case STATE_GET_DATA:
290             /* Make sure we have enough data.
291              * (Not useful if we went through NEXT_SYNC) */
292             if( block_WaitBytes( &p_sys->bytestream,
293                                  p_sys->frame.i_size ) != VLC_SUCCESS )
294             {
295                 /* Need more data */
296                 return NULL;
297             }
298             p_sys->i_state = STATE_SEND_DATA;
299
300         case STATE_SEND_DATA:
301             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
302             {
303                 //p_dec->b_error = true;
304                 return NULL;
305             }
306
307             /* Copy the whole frame into the buffer. When we reach this point
308              * we already know we have enough data available. */
309             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->frame.i_size );
310
311             /* Make sure we don't reuse the same pts twice */
312             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
313                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
314
315             /* So p_block doesn't get re-added several times */
316             *pp_block = block_BytestreamPop( &p_sys->bytestream );
317
318             p_sys->i_state = STATE_NOSYNC;
319
320             return p_out_buffer;
321         }
322     }
323
324     return NULL;
325 }
326
327 /*****************************************************************************
328  * CloseCommon: clean up the decoder
329  *****************************************************************************/
330 static void CloseCommon( vlc_object_t *p_this )
331 {
332     decoder_t *p_dec = (decoder_t*)p_this;
333     decoder_sys_t *p_sys = p_dec->p_sys;
334
335     block_BytestreamRelease( &p_sys->bytestream );
336
337     free( p_sys );
338 }
339
340 /*****************************************************************************
341  * GetOutBuffer:
342  *****************************************************************************/
343 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
344 {
345     decoder_sys_t *p_sys = p_dec->p_sys;
346     uint8_t *p_buf;
347
348     if( p_dec->fmt_out.audio.i_rate != p_sys->frame.i_rate )
349     {
350         msg_Info( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
351                   p_sys->frame.i_channels, p_sys->frame.i_rate, p_sys->frame.i_bitrate );
352
353         aout_DateInit( &p_sys->end_date, p_sys->frame.i_rate );
354         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
355     }
356
357     p_dec->fmt_out.audio.i_rate     = p_sys->frame.i_rate;
358     p_dec->fmt_out.audio.i_channels = p_sys->frame.i_channels;
359     if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->frame.i_size )
360         p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->frame.i_size;
361     p_dec->fmt_out.audio.i_frame_length = p_sys->frame.i_samples;
362
363     p_dec->fmt_out.audio.i_original_channels = p_sys->frame.i_channels_conf;
364     p_dec->fmt_out.audio.i_physical_channels =
365         p_sys->frame.i_channels_conf & AOUT_CHAN_PHYSMASK;
366
367     p_dec->fmt_out.i_bitrate = p_sys->frame.i_bitrate;
368
369     if( p_sys->b_packetizer )
370     {
371         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
372         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
373         *pp_out_buffer = p_sout_buffer;
374     }
375     else
376     {
377         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
378         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
379         *pp_out_buffer = p_aout_buffer;
380     }
381
382     return p_buf;
383 }
384
385 /*****************************************************************************
386  * GetAoutBuffer:
387  *****************************************************************************/
388 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
389 {
390     decoder_sys_t *p_sys = p_dec->p_sys;
391     aout_buffer_t *p_buf;
392
393     p_buf = decoder_NewAudioBuffer( p_dec, p_sys->frame.i_samples );
394     if( p_buf == NULL ) return NULL;
395
396     p_buf->start_date = aout_DateGet( &p_sys->end_date );
397     p_buf->end_date = aout_DateIncrement( &p_sys->end_date, p_sys->frame.i_samples );
398
399     return p_buf;
400 }
401
402 /*****************************************************************************
403  * GetSoutBuffer:
404  *****************************************************************************/
405 static block_t *GetSoutBuffer( decoder_t *p_dec )
406 {
407     decoder_sys_t *p_sys = p_dec->p_sys;
408     block_t *p_block;
409
410     p_block = block_New( p_dec, p_sys->frame.i_size );
411     if( p_block == NULL ) return NULL;
412
413     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
414
415     p_block->i_length =
416         aout_DateIncrement( &p_sys->end_date, p_sys->frame.i_samples ) - p_block->i_pts;
417
418     return p_block;
419 }
420