1 /*****************************************************************************
2 * lpcm_adec.c: lpcm decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: lpcm_adec.c,v 1.17 2002/06/01 18:04:49 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Henri Fallon <henri@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <string.h> /* memcpy(), memset() */
29 #include <stdlib.h> /* malloc(), free() */
33 #include <vlc/decoder.h>
36 # include <unistd.h> /* getpid() */
39 #include "lpcm_adec.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int decoder_Probe ( u8 * );
45 static int decoder_Run ( decoder_fifo_t * );
46 void DecodeFrame ( lpcmdec_thread_t * );
47 static int InitThread ( lpcmdec_thread_t * );
48 static void EndThread ( lpcmdec_thread_t * );
51 /*****************************************************************************
53 *****************************************************************************/
54 void _M( adec_getfunctions )( function_list_t * p_function_list )
56 p_function_list->functions.dec.pf_probe = decoder_Probe;
57 p_function_list->functions.dec.pf_run = decoder_Run;
60 /*****************************************************************************
61 * Build configuration tree.
62 *****************************************************************************/
67 SET_DESCRIPTION( _("linear PCM audio decoder") )
68 ADD_CAPABILITY( DECODER, 100 )
72 _M( adec_getfunctions )( &p_module->p_functions->dec );
75 MODULE_DEACTIVATE_START
76 MODULE_DEACTIVATE_STOP
78 /*****************************************************************************
79 * decoder_Probe: probe the decoder and return score
80 *****************************************************************************/
81 static int decoder_Probe( u8 *pi_type )
83 return ( *pi_type == LPCM_AUDIO_ES ) ? 0 : -1;
86 /*****************************************************************************
87 * decoder_Run: the lpcm decoder
88 *****************************************************************************/
89 static int decoder_Run( decoder_fifo_t * p_fifo )
91 lpcmdec_thread_t * p_lpcmdec;
93 /* Allocate the memory needed to store the thread's structure */
94 if( (p_lpcmdec = (lpcmdec_thread_t *)malloc (sizeof(lpcmdec_thread_t)) )
97 msg_Err( p_fifo, "out of memory" );
98 DecoderError( p_fifo );
103 * Initialize the thread properties
105 p_lpcmdec->p_fifo = p_fifo;
107 if( InitThread( p_lpcmdec ) )
109 DecoderError( p_fifo );
114 /* lpcm decoder thread's main loop */
115 while ((!p_lpcmdec->p_fifo->b_die) && (!p_lpcmdec->p_fifo->b_error))
117 DecodeFrame(p_lpcmdec);
120 /* If b_error is set, the lpcm decoder thread enters the error loop */
121 if (p_lpcmdec->p_fifo->b_error)
123 DecoderError( p_lpcmdec->p_fifo );
126 /* End of the lpcm decoder thread */
127 EndThread (p_lpcmdec);
132 /*****************************************************************************
133 * InitThread : initialize an lpcm decoder thread
134 *****************************************************************************/
135 static int InitThread (lpcmdec_thread_t * p_lpcmdec)
138 /* Init the BitStream */
139 InitBitstream( &p_lpcmdec->bit_stream, p_lpcmdec->p_fifo,
142 /* Creating the audio output fifo */
143 p_lpcmdec->p_aout_fifo =
144 aout_CreateFifo( p_lpcmdec->p_fifo, AOUT_FIFO_PCM,
145 2, 48000, LPCMDEC_FRAME_SIZE / 2, NULL );
146 if ( p_lpcmdec->p_aout_fifo == NULL )
153 /*****************************************************************************
154 * DecodeFrame: decodes a frame.
155 *****************************************************************************/
156 void DecodeFrame( lpcmdec_thread_t * p_lpcmdec )
158 byte_t * buffer,p_temp[LPCMDEC_FRAME_SIZE];
162 CurrentPTS( &p_lpcmdec->bit_stream,
163 &p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame],
165 if( !p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] )
167 p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] =
171 buffer = ((byte_t *)p_lpcmdec->p_aout_fifo->buffer) +
172 (p_lpcmdec->p_aout_fifo->i_end_frame * LPCMDEC_FRAME_SIZE);
174 RemoveBits32(&p_lpcmdec->bit_stream);
175 byte1 = GetBits(&p_lpcmdec->bit_stream, 8) ;
176 byte2 = GetBits(&p_lpcmdec->bit_stream, 8) ;
178 /* I only have 2 test streams. As far as I understand
179 * after the RemoveBits and the 2 GetBits, we should be exactly
180 * where we whant : the sync word : 0x0180.
181 * If not, we got and find it. */
182 while( ( byte1 != 0x01 || byte2 != 0x80 ) && (!p_lpcmdec->p_fifo->b_die)
183 && (!p_lpcmdec->p_fifo->b_error) )
186 byte2 = GetBits(&p_lpcmdec->bit_stream, 8);
189 GetChunk( &p_lpcmdec->bit_stream, p_temp, LPCMDEC_FRAME_SIZE);
190 if( p_lpcmdec->p_fifo->b_die ) return;
192 for( i_loop = 0; i_loop < LPCMDEC_FRAME_SIZE/2; i_loop++ )
194 buffer[2*i_loop]=p_temp[2*i_loop+1];
195 buffer[2*i_loop+1]=p_temp[2*i_loop];
198 vlc_mutex_lock (&p_lpcmdec->p_aout_fifo->data_lock);
199 p_lpcmdec->p_aout_fifo->i_end_frame =
200 (p_lpcmdec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
201 vlc_cond_signal (&p_lpcmdec->p_aout_fifo->data_wait);
202 vlc_mutex_unlock (&p_lpcmdec->p_aout_fifo->data_lock);
205 /*****************************************************************************
206 * EndThread : lpcm decoder thread destruction
207 *****************************************************************************/
208 static void EndThread( lpcmdec_thread_t * p_lpcmdec )
210 /* If the audio output fifo was created, we destroy it */
211 if( p_lpcmdec->p_aout_fifo != NULL )
213 aout_DestroyFifo( p_lpcmdec->p_aout_fifo );
215 /* Make sure the output thread leaves the NextFrame() function */
216 vlc_mutex_lock( &(p_lpcmdec->p_aout_fifo->data_lock) );
217 vlc_cond_signal( &(p_lpcmdec->p_aout_fifo->data_wait) );
218 vlc_mutex_unlock( &(p_lpcmdec->p_aout_fifo->data_lock) );
221 /* Destroy descriptor */