1 /*****************************************************************************
2 * lpcm_decoder_thread.c: lpcm decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: lpcm_adec.c,v 1.14 2002/04/19 13:56:11 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 <stdio.h> /* "intf_msg.h" */
29 #include <string.h> /* memcpy(), memset() */
30 #include <stdlib.h> /* malloc(), free() */
32 #include <videolan/vlc.h>
35 # include <unistd.h> /* getpid() */
38 #include "audio_output.h"
40 #include "stream_control.h"
41 #include "input_ext-dec.h"
43 #include "lpcm_adec.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int decoder_Probe ( u8 * );
49 static int decoder_Run ( decoder_config_t * );
50 void DecodeFrame ( lpcmdec_thread_t * );
51 static int InitThread ( lpcmdec_thread_t * );
52 static void EndThread ( lpcmdec_thread_t * );
55 /*****************************************************************************
57 *****************************************************************************/
58 void _M( adec_getfunctions )( function_list_t * p_function_list )
60 p_function_list->functions.dec.pf_probe = decoder_Probe;
61 p_function_list->functions.dec.pf_run = decoder_Run;
64 /*****************************************************************************
65 * Build configuration tree.
66 *****************************************************************************/
71 SET_DESCRIPTION( _("linear PCM audio decoder") )
72 ADD_CAPABILITY( DECODER, 100 )
76 _M( adec_getfunctions )( &p_module->p_functions->dec );
79 MODULE_DEACTIVATE_START
80 MODULE_DEACTIVATE_STOP
82 /*****************************************************************************
83 * decoder_Probe: probe the decoder and return score
84 *****************************************************************************/
85 static int decoder_Probe( u8 *pi_type )
87 return ( *pi_type == LPCM_AUDIO_ES ) ? 0 : -1;
90 /*****************************************************************************
91 * decoder_Run: the lpcm decoder
92 *****************************************************************************/
93 static int decoder_Run( decoder_config_t * p_config )
95 lpcmdec_thread_t * p_lpcmdec;
97 /* Allocate the memory needed to store the thread's structure */
98 if( (p_lpcmdec = (lpcmdec_thread_t *)malloc (sizeof(lpcmdec_thread_t)) )
101 intf_ErrMsg( "LPCM : error : cannot create lpcmdec_thread_t" );
102 DecoderError( p_config->p_decoder_fifo );
107 * Initialize the thread properties
109 p_lpcmdec->p_config = p_config;
110 p_lpcmdec->p_fifo = p_config->p_decoder_fifo;
112 if( InitThread( p_lpcmdec ) )
114 DecoderError( p_config->p_decoder_fifo );
119 /* lpcm decoder thread's main loop */
120 while ((!p_lpcmdec->p_fifo->b_die) && (!p_lpcmdec->p_fifo->b_error))
122 DecodeFrame(p_lpcmdec);
125 /* If b_error is set, the lpcm decoder thread enters the error loop */
126 if (p_lpcmdec->p_fifo->b_error)
128 DecoderError( p_lpcmdec->p_fifo );
131 /* End of the lpcm decoder thread */
132 EndThread (p_lpcmdec);
137 /*****************************************************************************
138 * InitThread : initialize an lpcm decoder thread
139 *****************************************************************************/
140 static int InitThread (lpcmdec_thread_t * p_lpcmdec)
143 /* Init the BitStream */
144 InitBitstream( &p_lpcmdec->bit_stream, p_lpcmdec->p_config->p_decoder_fifo,
147 /* Creating the audio output fifo */
148 p_lpcmdec->p_aout_fifo = aout_CreateFifo( AOUT_FIFO_PCM, 2, 48000,
149 LPCMDEC_FRAME_SIZE/2, NULL );
150 if ( p_lpcmdec->p_aout_fifo == NULL )
157 /*****************************************************************************
158 * DecodeFrame: decodes a frame.
159 *****************************************************************************/
160 void DecodeFrame( lpcmdec_thread_t * p_lpcmdec )
162 byte_t * buffer,p_temp[LPCMDEC_FRAME_SIZE];
166 CurrentPTS( &p_lpcmdec->bit_stream,
167 &p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame],
169 if( !p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] )
171 p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] =
175 buffer = ((byte_t *)p_lpcmdec->p_aout_fifo->buffer) +
176 (p_lpcmdec->p_aout_fifo->i_end_frame * LPCMDEC_FRAME_SIZE);
178 RemoveBits32(&p_lpcmdec->bit_stream);
179 byte1 = GetBits(&p_lpcmdec->bit_stream, 8) ;
180 byte2 = GetBits(&p_lpcmdec->bit_stream, 8) ;
182 /* I only have 2 test streams. As far as I understand
183 * after the RemoveBits and the 2 GetBits, we should be exactly
184 * where we whant : the sync word : 0x0180.
185 * If not, we got and find it. */
186 while( ( byte1 != 0x01 || byte2 != 0x80 ) && (!p_lpcmdec->p_fifo->b_die)
187 && (!p_lpcmdec->p_fifo->b_error) )
190 byte2 = GetBits(&p_lpcmdec->bit_stream, 8);
193 GetChunk( &p_lpcmdec->bit_stream, p_temp, LPCMDEC_FRAME_SIZE);
195 for( i_loop = 0; i_loop < LPCMDEC_FRAME_SIZE/2; i_loop++ )
197 buffer[2*i_loop]=p_temp[2*i_loop+1];
198 buffer[2*i_loop+1]=p_temp[2*i_loop];
201 vlc_mutex_lock (&p_lpcmdec->p_aout_fifo->data_lock);
202 p_lpcmdec->p_aout_fifo->i_end_frame =
203 (p_lpcmdec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
204 vlc_cond_signal (&p_lpcmdec->p_aout_fifo->data_wait);
205 vlc_mutex_unlock (&p_lpcmdec->p_aout_fifo->data_lock);
208 /*****************************************************************************
209 * EndThread : lpcm decoder thread destruction
210 *****************************************************************************/
211 static void EndThread( lpcmdec_thread_t * p_lpcmdec )
213 /* If the audio output fifo was created, we destroy it */
214 if( p_lpcmdec->p_aout_fifo != NULL )
216 aout_DestroyFifo( p_lpcmdec->p_aout_fifo );
218 /* Make sure the output thread leaves the NextFrame() function */
219 vlc_mutex_lock( &(p_lpcmdec->p_aout_fifo->data_lock) );
220 vlc_cond_signal( &(p_lpcmdec->p_aout_fifo->data_wait) );
221 vlc_mutex_unlock( &(p_lpcmdec->p_aout_fifo->data_lock) );
224 /* Destroy descriptor */