]> git.sesse.net Git - vlc/blob - plugins/lpcm_adec/lpcm_adec.c
* ALL: got rid of p_object->p_this which is now useless.
[vlc] / plugins / lpcm_adec / lpcm_adec.c
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 $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Henri Fallon <henri@videolan.org>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <string.h>                                    /* memcpy(), memset() */
29 #include <stdlib.h>                                      /* malloc(), free() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/aout.h>
33 #include <vlc/decoder.h>
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>                                           /* getpid() */
37 #endif
38
39 #include "lpcm_adec.h"
40
41 /*****************************************************************************
42  * Local prototypes
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 * );
49
50
51 /*****************************************************************************
52  * Capabilities
53  *****************************************************************************/
54 void _M( adec_getfunctions )( function_list_t * p_function_list )
55 {
56     p_function_list->functions.dec.pf_probe = decoder_Probe;
57     p_function_list->functions.dec.pf_run   = decoder_Run;
58 }
59
60 /*****************************************************************************
61  * Build configuration tree.
62  *****************************************************************************/
63 MODULE_CONFIG_START
64 MODULE_CONFIG_STOP
65
66 MODULE_INIT_START
67     SET_DESCRIPTION( _("linear PCM audio decoder") )
68     ADD_CAPABILITY( DECODER, 100 )
69 MODULE_INIT_STOP
70
71 MODULE_ACTIVATE_START
72     _M( adec_getfunctions )( &p_module->p_functions->dec );
73 MODULE_ACTIVATE_STOP
74
75 MODULE_DEACTIVATE_START
76 MODULE_DEACTIVATE_STOP
77
78 /*****************************************************************************
79  * decoder_Probe: probe the decoder and return score
80  *****************************************************************************/
81 static int decoder_Probe( u8 *pi_type )
82 {
83     return ( *pi_type == LPCM_AUDIO_ES ) ? 0 : -1;
84 }
85
86 /*****************************************************************************
87  * decoder_Run: the lpcm decoder
88  *****************************************************************************/
89 static int decoder_Run( decoder_fifo_t * p_fifo )
90 {
91     lpcmdec_thread_t *   p_lpcmdec;
92
93     /* Allocate the memory needed to store the thread's structure */
94     if( (p_lpcmdec = (lpcmdec_thread_t *)malloc (sizeof(lpcmdec_thread_t)) )
95             == NULL) 
96     {
97         msg_Err( p_fifo, "out of memory" );
98         DecoderError( p_fifo );
99         return( -1 );
100     }
101
102     /*
103      * Initialize the thread properties
104      */
105     p_lpcmdec->p_fifo = p_fifo;
106
107     if( InitThread( p_lpcmdec ) )
108     {
109         DecoderError( p_fifo );
110         free( p_lpcmdec );
111         return( -1 );
112     }
113
114     /* lpcm decoder thread's main loop */
115     while ((!p_lpcmdec->p_fifo->b_die) && (!p_lpcmdec->p_fifo->b_error))
116     {
117         DecodeFrame(p_lpcmdec);
118     }
119
120     /* If b_error is set, the lpcm decoder thread enters the error loop */
121     if (p_lpcmdec->p_fifo->b_error)
122     {
123         DecoderError( p_lpcmdec->p_fifo );
124     }
125
126     /* End of the lpcm decoder thread */
127     EndThread (p_lpcmdec);
128
129     return( 0 );
130 }
131
132 /*****************************************************************************
133  * InitThread : initialize an lpcm decoder thread
134  *****************************************************************************/
135 static int InitThread (lpcmdec_thread_t * p_lpcmdec)
136 {
137
138     /* Init the BitStream */
139     InitBitstream( &p_lpcmdec->bit_stream, p_lpcmdec->p_fifo,
140                    NULL, NULL);
141
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 )
147     {
148         return( -1 );
149     }
150     return( 0 );
151 }
152
153 /*****************************************************************************
154  * DecodeFrame: decodes a frame.
155  *****************************************************************************/
156 void DecodeFrame( lpcmdec_thread_t * p_lpcmdec )
157 {
158     byte_t * buffer,p_temp[LPCMDEC_FRAME_SIZE];
159     int i_loop;
160     byte_t byte1, byte2;
161
162     CurrentPTS( &p_lpcmdec->bit_stream,
163         &p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame],
164         NULL );
165     if( !p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] )
166     {
167         p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->i_end_frame] =
168             LAST_MDATE;
169     }
170
171     buffer = ((byte_t *)p_lpcmdec->p_aout_fifo->buffer) + 
172               (p_lpcmdec->p_aout_fifo->i_end_frame * LPCMDEC_FRAME_SIZE);
173
174     RemoveBits32(&p_lpcmdec->bit_stream);
175     byte1 = GetBits(&p_lpcmdec->bit_stream, 8) ;
176     byte2 = GetBits(&p_lpcmdec->bit_stream, 8) ;
177     
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) )
184     {
185         byte1 = byte2;
186         byte2 = GetBits(&p_lpcmdec->bit_stream, 8);
187     }
188     
189     GetChunk( &p_lpcmdec->bit_stream, p_temp, LPCMDEC_FRAME_SIZE);
190     if( p_lpcmdec->p_fifo->b_die ) return;
191
192     for( i_loop = 0; i_loop < LPCMDEC_FRAME_SIZE/2; i_loop++ )
193     {
194         buffer[2*i_loop]=p_temp[2*i_loop+1];
195         buffer[2*i_loop+1]=p_temp[2*i_loop];
196     }
197     
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);
203 }
204
205 /*****************************************************************************
206  * EndThread : lpcm decoder thread destruction
207  *****************************************************************************/
208 static void EndThread( lpcmdec_thread_t * p_lpcmdec )
209 {
210     /* If the audio output fifo was created, we destroy it */
211     if( p_lpcmdec->p_aout_fifo != NULL ) 
212     {
213         aout_DestroyFifo( p_lpcmdec->p_aout_fifo );
214
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) );
219     }
220
221     /* Destroy descriptor */
222     free( p_lpcmdec );
223 }