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