1 /*****************************************************************************
2 * a52old.c: A52 decoder module main file
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: a52old.c,v 1.9 2003/02/20 01:52:45 sigmunau Exp $
7 * Authors: Michel Lespinasse <walken@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* memset() */
32 #include <vlc/decoder.h>
34 #include "aout_internal.h"
37 # include <unistd.h> /* getpid() */
44 #define A52DEC_FRAME_SIZE 1536
46 /*****************************************************************************
48 *****************************************************************************/
49 static int OpenDecoder ( vlc_object_t * );
50 static int RunDecoder ( decoder_fifo_t * );
51 static int InitThread ( a52dec_t * );
52 static void EndThread ( a52dec_t * );
53 static void BitstreamCallback ( bit_stream_t *, vlc_bool_t );
55 /*****************************************************************************
57 *****************************************************************************/
59 add_category_hint( N_("Miscellaneous"), NULL, VLC_TRUE );
60 add_module ( "a52-downmix", "downmix", NULL, NULL,
61 N_("A52 downmix module"), NULL, VLC_TRUE );
62 add_module ( "a52-imdct", "imdct", NULL, NULL,
63 N_("A52 IMDCT module"), NULL, VLC_TRUE );
64 set_description( _("software A52 decoder") );
65 set_capability( "decoder", 50 );
66 set_callbacks( OpenDecoder, NULL );
67 add_shortcut( "a52" );
70 /*****************************************************************************
71 * OpenDecoder: probe the decoder and return score
72 *****************************************************************************
73 * Tries to launch a decoder and return score so that the interface is able
75 *****************************************************************************/
76 static int OpenDecoder( vlc_object_t *p_this )
78 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
80 if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ')
81 && p_fifo->i_fourcc != VLC_FOURCC('a','5','2','b') )
86 p_fifo->pf_run = RunDecoder;
90 /*****************************************************************************
91 * RunDecoder: this function is called just after the thread is created
92 *****************************************************************************/
93 static int RunDecoder( decoder_fifo_t *p_fifo )
96 void * p_orig; /* pointer before memalign */
97 vlc_bool_t b_sync = 0;
100 aout_buffer_t *p_aout_buffer;
101 audio_sample_format_t output_format;
102 audio_date_t end_date;
104 /* Allocate the memory needed to store the thread's structure */
105 p_a52dec = (a52dec_t *)vlc_memalign( &p_orig, 16, sizeof(a52dec_t) );
106 memset( p_a52dec, 0, sizeof( a52dec_t ) );
108 if( p_a52dec == NULL )
110 msg_Err( p_fifo, "out of memory" );
111 DecoderError( p_fifo );
116 * Initialize the thread properties
118 p_a52dec->p_fifo = p_fifo;
119 if( InitThread( p_a52dec ) )
121 msg_Err( p_fifo, "could not initialize thread" );
122 DecoderError( p_fifo );
127 /* A52 decoder thread's main loop */
128 /* FIXME : do we have enough room to store the decoded frames ?? */
129 while ((!p_a52dec->p_fifo->b_die) && (!p_a52dec->p_fifo->b_error))
131 sync_info_t sync_info;
136 #define p_bit_stream (&p_a52dec->bit_stream)
138 /* Go to the next PES packet and jump to sync_ptr */
140 BitstreamNextDataPacket( p_bit_stream );
141 } while( !p_bit_stream->p_decoder_fifo->b_die
142 && !p_bit_stream->p_decoder_fifo->b_error
143 && p_bit_stream->p_data !=
144 p_bit_stream->p_decoder_fifo->p_first->p_first );
145 i_sync_ptr = *(p_bit_stream->p_byte - 2) << 8
146 | *(p_bit_stream->p_byte - 1);
147 p_bit_stream->p_byte += i_sync_ptr;
149 /* Empty the bit FIFO and realign the bit stream */
150 p_bit_stream->fifo.buffer = 0;
151 p_bit_stream->fifo.i_available = 0;
152 AlignWord( p_bit_stream );
157 if (sync_frame (p_a52dec, &sync_info))
163 if( ( p_a52dec->p_aout_input == NULL )||
164 ( output_format.i_rate != sync_info.sample_rate ) )
166 if( p_a52dec->p_aout_input )
168 /* Delete old output */
169 msg_Warn( p_a52dec->p_fifo, "opening a new aout" );
170 aout_DecDelete( p_a52dec->p_aout, p_a52dec->p_aout_input );
173 /* Set output configuration */
174 output_format.i_format = AOUT_FMT_S16_NE;
175 output_format.i_channels = 2; /* FIXME ! */
176 output_format.i_rate = sync_info.sample_rate;
177 aout_DateInit( &end_date, output_format.i_rate );
178 p_a52dec->p_aout_input = aout_DecNew( p_a52dec->p_fifo,
183 if( p_a52dec->p_aout_input == NULL )
185 msg_Err( p_a52dec->p_fifo, "failed to create aout fifo" );
186 p_a52dec->p_fifo->b_error = 1;
190 NextPTS( &p_a52dec->bit_stream, &i_pts, NULL );
191 if( i_pts != 0 && i_pts != aout_DateGet( &end_date ) )
193 aout_DateSet( &end_date, i_pts );
196 if( !aout_DateGet( &end_date ) )
201 p_aout_buffer = aout_DecNewBuffer( p_a52dec->p_aout,
202 p_a52dec->p_aout_input,
206 msg_Err( p_a52dec->p_fifo, "cannot get aout buffer" );
207 p_a52dec->p_fifo->b_error = 1;
211 p_aout_buffer->start_date = aout_DateGet( &end_date );
212 p_aout_buffer->end_date = aout_DateIncrement( &end_date,
215 if (decode_frame (p_a52dec, (s16*)p_aout_buffer->p_buffer))
218 aout_DecDeleteBuffer( p_a52dec->p_aout, p_a52dec->p_aout_input,
224 aout_DecPlay( p_a52dec->p_aout, p_a52dec->p_aout_input,
228 RealignBits(&p_a52dec->bit_stream);
231 /* If b_error is set, the A52 decoder thread enters the error loop */
232 if (p_a52dec->p_fifo->b_error)
234 DecoderError( p_a52dec->p_fifo );
237 /* End of the A52 decoder thread */
238 EndThread (p_a52dec);
245 /*****************************************************************************
246 * InitThread: initialize data before entering main loop
247 *****************************************************************************/
248 static int InitThread( a52dec_t * p_a52dec )
251 * Choose the best downmix module
253 p_a52dec->p_downmix = vlc_object_create( p_a52dec->p_fifo,
254 sizeof( downmix_t ) );
255 p_a52dec->p_downmix->psz_object_name = "downmix";
257 p_a52dec->p_downmix->p_module =
258 module_Need( p_a52dec->p_downmix, "downmix", "$a52-downmix" );
260 if( p_a52dec->p_downmix->p_module == NULL )
262 msg_Err( p_a52dec->p_fifo, "no suitable downmix module" );
263 vlc_object_destroy( p_a52dec->p_downmix );
268 * Choose the best IMDCT module
270 p_a52dec->p_imdct = vlc_object_create( p_a52dec->p_fifo,
273 #define IMDCT p_a52dec->p_imdct
274 p_a52dec->p_imdct->p_module =
275 module_Need( p_a52dec->p_imdct, "imdct", "$a52-imdct" );
277 if( p_a52dec->p_imdct->p_module == NULL )
279 msg_Err( p_a52dec->p_fifo, "no suitable IMDCT module" );
280 vlc_object_destroy( p_a52dec->p_imdct );
281 module_Unneed( p_a52dec->p_downmix, p_a52dec->p_downmix->p_module );
282 vlc_object_destroy( p_a52dec->p_downmix );
286 /* Initialize the A52 decoder structures */
287 p_a52dec->samples = vlc_memalign( &p_a52dec->samples_orig,
288 16, 6 * 256 * sizeof(float) );
290 IMDCT->buf = vlc_memalign( &IMDCT->buf_orig,
291 16, N/4 * sizeof(complex_t) );
292 IMDCT->delay = vlc_memalign( &IMDCT->delay_orig,
293 16, 6 * 256 * sizeof(float) );
294 IMDCT->delay1 = vlc_memalign( &IMDCT->delay1_orig,
295 16, 6 * 256 * sizeof(float) );
296 IMDCT->xcos1 = vlc_memalign( &IMDCT->xcos1_orig,
297 16, N/4 * sizeof(float) );
298 IMDCT->xsin1 = vlc_memalign( &IMDCT->xsin1_orig,
299 16, N/4 * sizeof(float) );
300 IMDCT->xcos2 = vlc_memalign( &IMDCT->xcos2_orig,
301 16, N/8 * sizeof(float) );
302 IMDCT->xsin2 = vlc_memalign( &IMDCT->xsin2_orig,
303 16, N/8 * sizeof(float) );
304 IMDCT->xcos_sin_sse = vlc_memalign( &IMDCT->xcos_sin_sse_orig,
305 16, 128 * 4 * sizeof(float) );
306 IMDCT->w_1 = vlc_memalign( &IMDCT->w_1_orig,
307 16, 1 * sizeof(complex_t) );
308 IMDCT->w_2 = vlc_memalign( &IMDCT->w_2_orig,
309 16, 2 * sizeof(complex_t) );
310 IMDCT->w_4 = vlc_memalign( &IMDCT->w_4_orig,
311 16, 4 * sizeof(complex_t) );
312 IMDCT->w_8 = vlc_memalign( &IMDCT->w_8_orig,
313 16, 8 * sizeof(complex_t) );
314 IMDCT->w_16 = vlc_memalign( &IMDCT->w_16_orig,
315 16, 16 * sizeof(complex_t) );
316 IMDCT->w_32 = vlc_memalign( &IMDCT->w_32_orig,
317 16, 32 * sizeof(complex_t) );
318 IMDCT->w_64 = vlc_memalign( &IMDCT->w_64_orig,
319 16, 64 * sizeof(complex_t) );
322 E_( a52_init )( p_a52dec );
325 * Initialize the output properties
327 p_a52dec->p_aout = NULL;
328 p_a52dec->p_aout_input = NULL;
333 return( InitBitstream( &p_a52dec->bit_stream, p_a52dec->p_fifo,
334 BitstreamCallback, (void *) p_a52dec ) );
337 /*****************************************************************************
338 * EndThread : A52 decoder thread destruction
339 *****************************************************************************/
340 static void EndThread (a52dec_t * p_a52dec)
342 /* If the audio output fifo was created, we destroy it */
343 if( p_a52dec->p_aout_input )
345 aout_DecDelete( p_a52dec->p_aout, p_a52dec->p_aout_input );
348 /* Free allocated structures */
349 #define IMDCT p_a52dec->p_imdct
350 free( IMDCT->w_1_orig );
351 free( IMDCT->w_64_orig );
352 free( IMDCT->w_32_orig );
353 free( IMDCT->w_16_orig );
354 free( IMDCT->w_8_orig );
355 free( IMDCT->w_4_orig );
356 free( IMDCT->w_2_orig );
357 free( IMDCT->xcos_sin_sse_orig );
358 free( IMDCT->xsin2_orig );
359 free( IMDCT->xcos2_orig );
360 free( IMDCT->xsin1_orig );
361 free( IMDCT->xcos1_orig );
362 free( IMDCT->delay1_orig );
363 free( IMDCT->delay_orig );
364 free( IMDCT->buf_orig );
367 free( p_a52dec->samples_orig );
369 /* Unlock the modules */
370 module_Unneed( p_a52dec->p_downmix, p_a52dec->p_downmix->p_module );
371 vlc_object_destroy( p_a52dec->p_downmix );
373 module_Unneed( p_a52dec->p_imdct, p_a52dec->p_imdct->p_module );
374 vlc_object_destroy( p_a52dec->p_imdct );
376 /* Free what's left of the decoder */
377 free( p_a52dec->imdct_orig );
378 CloseBitstream( p_a52dec->bit_stream );
381 /*****************************************************************************
382 * BitstreamCallback: Import parameters from the new data/PES packet
383 *****************************************************************************
384 * This function is called by input's NextDataPacket.
385 *****************************************************************************/
386 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
387 vlc_bool_t b_new_pes )
391 /* Drop special A52 header */
392 /* p_bit_stream->p_byte += 3; */