1 /*****************************************************************************
2 * lpcm_adec.c: lpcm decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: lpcm_adec.c,v 1.20 2002/07/31 20:56:51 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 OpenDecoder ( vlc_object_t * );
45 static int RunDecoder ( decoder_fifo_t * );
47 void DecodeFrame ( lpcmdec_thread_t * );
48 static int InitThread ( lpcmdec_thread_t * );
49 static void EndThread ( lpcmdec_thread_t * );
51 /*****************************************************************************
53 *****************************************************************************/
55 set_description( _("linear PCM audio decoder") );
56 set_capability( "decoder", 100 );
57 set_callbacks( OpenDecoder, NULL );
60 /*****************************************************************************
61 * OpenDecoder: probe the decoder and return score
62 *****************************************************************************/
63 static int OpenDecoder( vlc_object_t *p_this )
65 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
67 if( p_fifo->i_fourcc != VLC_FOURCC('l','p','c','m') )
72 p_fifo->pf_run = RunDecoder;
76 /*****************************************************************************
77 * RunDecoder: the lpcm decoder
78 *****************************************************************************/
79 static int RunDecoder( decoder_fifo_t * p_fifo )
81 lpcmdec_thread_t * p_lpcmdec;
83 /* Allocate the memory needed to store the thread's structure */
84 if( (p_lpcmdec = (lpcmdec_thread_t *)malloc (sizeof(lpcmdec_thread_t)) )
87 msg_Err( p_fifo, "out of memory" );
88 DecoderError( p_fifo );
93 * Initialize the thread properties
95 p_lpcmdec->p_fifo = p_fifo;
97 if( InitThread( p_lpcmdec ) )
99 DecoderError( p_fifo );
104 /* lpcm decoder thread's main loop */
105 while ((!p_lpcmdec->p_fifo->b_die) && (!p_lpcmdec->p_fifo->b_error))
107 DecodeFrame(p_lpcmdec);
110 /* If b_error is set, the lpcm decoder thread enters the error loop */
111 if (p_lpcmdec->p_fifo->b_error)
113 DecoderError( p_lpcmdec->p_fifo );
116 /* End of the lpcm decoder thread */
117 EndThread (p_lpcmdec);
122 /*****************************************************************************
123 * InitThread : initialize an lpcm decoder thread
124 *****************************************************************************/
125 static int InitThread (lpcmdec_thread_t * p_lpcmdec)
128 /* Init the BitStream */
129 InitBitstream( &p_lpcmdec->bit_stream, p_lpcmdec->p_fifo,
132 /* Creating the audio output fifo */
133 p_lpcmdec->p_aout_fifo =
134 aout_CreateFifo( p_lpcmdec->p_fifo, AOUT_FIFO_PCM,
135 2, 48000, LPCMDEC_FRAME_SIZE / 2, NULL );
136 if ( p_lpcmdec->p_aout_fifo == NULL )
143 /*****************************************************************************
144 * DecodeFrame: decodes a frame.
145 *****************************************************************************/
146 void DecodeFrame( lpcmdec_thread_t * p_lpcmdec )
148 byte_t * buffer,p_temp[LPCMDEC_FRAME_SIZE];
152 CurrentPTS( &p_lpcmdec->bit_stream,
153 &p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame],
155 if( !p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] )
157 p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] =
161 buffer = ((byte_t *)p_lpcmdec->p_aout_fifo->buffer) +
162 (p_lpcmdec->p_aout_fifo->i_end_frame * LPCMDEC_FRAME_SIZE);
164 RemoveBits32(&p_lpcmdec->bit_stream);
166 byte1 = GetBits(&p_lpcmdec->bit_stream, 8) ;
167 byte2 = GetBits(&p_lpcmdec->bit_stream, 8) ;
169 /* I only have 2 test streams. As far as I understand
170 * after the RemoveBits and the 2 GetBits, we should be exactly
171 * where we whant : the sync word : 0x0180.
172 * If not, we got and find it. */
173 while( ( byte1 != 0x01 || byte2 != 0x80 ) && (!p_lpcmdec->p_fifo->b_die)
174 && (!p_lpcmdec->p_fifo->b_error) )
177 byte2 = GetBits(&p_lpcmdec->bit_stream, 8);
181 while( ( !p_lpcmdec->p_fifo->b_die ) &&
182 ( !p_lpcmdec->p_fifo->b_error ) &&
185 while( ( !p_lpcmdec->p_fifo->b_die ) &&
186 ( !p_lpcmdec->p_fifo->b_error ) &&
187 ( GetBits( &p_lpcmdec->bit_stream, 8 ) != 0x01 ) );
188 b_sync = ( ShowBits( &p_lpcmdec->bit_stream, 8 ) == 0x80 );
190 RemoveBits( &p_lpcmdec->bit_stream, 8 );
193 GetChunk( &p_lpcmdec->bit_stream, p_temp, LPCMDEC_FRAME_SIZE);
194 if( p_lpcmdec->p_fifo->b_die || p_lpcmdec->p_fifo->b_error ) return;
196 for( i_loop = 0; i_loop < LPCMDEC_FRAME_SIZE/2; i_loop++ )
198 buffer[2*i_loop]=p_temp[2*i_loop+1];
199 buffer[2*i_loop+1]=p_temp[2*i_loop];
202 vlc_mutex_lock (&p_lpcmdec->p_aout_fifo->data_lock);
203 p_lpcmdec->p_aout_fifo->i_end_frame =
204 (p_lpcmdec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
205 vlc_cond_signal (&p_lpcmdec->p_aout_fifo->data_wait);
206 vlc_mutex_unlock (&p_lpcmdec->p_aout_fifo->data_lock);
209 /*****************************************************************************
210 * EndThread : lpcm decoder thread destruction
211 *****************************************************************************/
212 static void EndThread( lpcmdec_thread_t * p_lpcmdec )
214 /* If the audio output fifo was created, we destroy it */
215 if( p_lpcmdec->p_aout_fifo != NULL )
217 aout_DestroyFifo( p_lpcmdec->p_aout_fifo );
219 /* Make sure the output thread leaves the NextFrame() function */
220 vlc_mutex_lock( &(p_lpcmdec->p_aout_fifo->data_lock) );
221 vlc_cond_signal( &(p_lpcmdec->p_aout_fifo->data_wait) );
222 vlc_mutex_unlock( &(p_lpcmdec->p_aout_fifo->data_lock) );
225 /* Destroy descriptor */