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