]> git.sesse.net Git - vlc/blob - modules/codec/araw.c
* araw.c : pseudo pcm decoder
[vlc] / modules / codec / araw.c
1 /*****************************************************************************
2  * araw.c: Pseudo audio decoder; for raw pcm data
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: araw.c,v 1.1 2002/10/14 21:59:44 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *      
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/aout.h>
29 #include <vlc/decoder.h>
30 #include <vlc/input.h>
31
32 #include <stdlib.h>                                      /* malloc(), free() */
33 #include <string.h>                                              /* strdup() */
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 typedef struct waveformatex_s
39 {
40     u16 i_formattag;
41     u16 i_channels;
42     u32 i_samplespersec;
43     u32 i_avgbytespersec;
44     u16 i_blockalign;
45     u16 i_bitspersample;
46     u16 i_size; /* the extra size in bytes */
47     u8  *p_data; /* The extra data */
48 } waveformatex_t;
49
50 typedef struct adec_thread_s
51 {
52     waveformatex_t  format;
53
54     /* The bit stream structure handles the PES stream at the bit level */
55     bit_stream_t        bit_stream;
56
57     /* Input properties */
58     decoder_fifo_t *p_fifo;
59     
60     /* Output properties */
61     aout_instance_t *   p_aout;       /* opaque */
62     aout_input_t *      p_aout_input; /* opaque */
63     audio_sample_format_t output_format;
64
65     audio_date_t        date;
66     mtime_t             pts;
67
68 } adec_thread_t;
69
70 static int  OpenDecoder    ( vlc_object_t * );
71
72 static int  RunDecoder     ( decoder_fifo_t * );
73 static int  InitThread     ( adec_thread_t * );
74 static void DecodeThread   ( adec_thread_t * );
75 static void EndThread      ( adec_thread_t * );
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80
81 vlc_module_begin();
82     set_description( _("Pseudo Raw Audio decoder") );
83     set_capability( "decoder", 50 );
84     set_callbacks( OpenDecoder, NULL );
85 vlc_module_end();
86
87 /*****************************************************************************
88  * OpenDecoder: probe the decoder and return score
89  *****************************************************************************
90  * Tries to launch a decoder and return score so that the interface is able
91  * to choose.
92  *****************************************************************************/
93 static int OpenDecoder( vlc_object_t *p_this )
94 {
95     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
96     
97     switch( p_fifo->i_fourcc )
98     {   
99         case VLC_FOURCC('a','r','a','w'):
100         case VLC_FOURCC('t','w','o','s'): /* for mov file */
101             p_fifo->pf_run = RunDecoder;
102             return VLC_SUCCESS;
103             
104         default:
105             return VLC_EGENERIC;
106     }
107
108 }
109
110 /*****************************************************************************
111  * RunDecoder: this function is called just after the thread is created
112  *****************************************************************************/
113 static int RunDecoder( decoder_fifo_t *p_fifo )
114 {
115     adec_thread_t *p_adec;
116     int b_error;
117
118     if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
119     {
120         msg_Err( p_fifo, "out of memory" );
121         DecoderError( p_fifo );
122         return( -1 );
123     }
124     memset( p_adec, 0, sizeof( adec_thread_t ) );
125     
126     p_adec->p_fifo = p_fifo;
127
128     if( InitThread( p_adec ) != 0 )
129     {
130         DecoderError( p_fifo );
131         return( -1 );
132     }
133
134     while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
135     {
136         DecodeThread( p_adec );
137     }
138
139
140     if( ( b_error = p_adec->p_fifo->b_error ) )
141     {
142         DecoderError( p_adec->p_fifo );
143     }
144
145     EndThread( p_adec );
146     if( b_error )
147     {
148         return( -1 );
149     }
150
151     return( 0 );
152 }
153
154
155 #define FREE( p ) if( p ) free( p ); p = NULL
156 #define GetWLE( p ) \
157     ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
158
159 #define GetDWLE( p ) \
160     (  *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
161         ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
162     
163 static void GetWaveFormatEx( waveformatex_t *p_wh,
164                              u8 *p_data )
165 {
166
167     p_wh->i_formattag     = GetWLE( p_data );
168     p_wh->i_channels      = GetWLE( p_data + 2 );
169     p_wh->i_samplespersec = GetDWLE( p_data + 4 );
170     p_wh->i_avgbytespersec= GetDWLE( p_data + 8 );
171     p_wh->i_blockalign    = GetWLE( p_data + 12 );
172     p_wh->i_bitspersample = GetWLE( p_data + 14 );
173     if( p_wh->i_formattag != 1 )
174     {
175         p_wh->i_size          = GetWLE( p_data + 16 );
176
177         if( p_wh->i_size )
178         {
179             p_wh->p_data = malloc( p_wh->i_size );
180             memcpy( p_wh->p_data, p_data + 18, p_wh->i_size );
181         }
182     }
183 }
184
185 /* get the first pes from fifo */
186 static pes_packet_t *PESGetFirst( decoder_fifo_t *p_fifo )
187 {
188     pes_packet_t *p_pes;
189
190     vlc_mutex_lock( &p_fifo->data_lock );
191
192     /* if fifo is empty wait */ 
193     while( !p_fifo->p_first )
194     {
195         if( p_fifo->b_die )
196         {
197             vlc_mutex_unlock( &p_fifo->data_lock );
198             return NULL;
199         }
200         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
201     }
202     p_pes = p_fifo->p_first;
203
204     vlc_mutex_unlock( &p_fifo->data_lock );
205
206     return p_pes;
207 }
208 static int PESGetSize( pes_packet_t *p_pes )
209 {
210     data_packet_t *p_data;
211     int i_size = 0;
212
213     if( !p_pes )
214     {
215         return( 0 );
216     }
217
218     for( p_data = p_pes->p_first; p_data != NULL; p_data = p_data->p_next )
219     {
220         i_size += p_data->p_payload_end - p_data->p_payload_start;
221     }
222
223     return( i_size );
224 }
225
226 /*****************************************************************************
227  * InitThread: initialize data before entering main loop
228  *****************************************************************************/
229 static int InitThread( adec_thread_t * p_adec )
230 {
231
232     if( p_adec->p_fifo->p_demux_data )
233     {
234         GetWaveFormatEx( &p_adec->format,
235                          (u8*)p_adec->p_fifo->p_demux_data );
236         /* fixing some values */
237         if( p_adec->format.i_formattag == 1 && !p_adec->format.i_blockalign )
238         {
239             p_adec->format.i_blockalign = p_adec->format.i_channels * 
240                     ( ( p_adec->format.i_bitspersample + 7 ) / 8 );
241         }
242     }
243     else
244     {
245         msg_Err( p_adec->p_fifo, "unknown raw format" );
246         return( -1 );
247     }
248
249     msg_Dbg( p_adec->p_fifo,
250              "raw format: samplerate:%dHz channels:%d bits/sample:%d blockalign:%d",
251              p_adec->format.i_samplespersec,
252              p_adec->format.i_channels,
253              p_adec->format.i_bitspersample, p_adec->format.i_blockalign );
254
255     /* Initialize the thread properties */
256     switch( ( p_adec->format.i_bitspersample + 7 ) / 8 )
257     {
258         case( 2 ):
259             p_adec->output_format.i_format = VLC_FOURCC('s','1','6','l');
260             break;
261         case( 3 ):
262             p_adec->output_format.i_format = VLC_FOURCC('s','2','4','l');
263             break;
264         case( 4 ):
265             p_adec->output_format.i_format = VLC_FOURCC('s','3','2','l');
266             break;
267
268         case( 1 ):
269         default:
270             msg_Err( p_adec->p_fifo, "bad parameters(bits/sample)" );
271             return( -1 );
272     }
273     p_adec->output_format.i_rate = p_adec->format.i_samplespersec;
274     p_adec->output_format.i_channels = p_adec->format.i_channels;
275     p_adec->p_aout = NULL;
276     p_adec->p_aout_input = NULL;
277
278     /* **** Create a new audio output **** */
279     aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
280     p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
281                                         &p_adec->p_aout,
282                                         &p_adec->output_format );
283     if( !p_adec->p_aout_input )
284     {
285         msg_Err( p_adec->p_fifo, "cannot create aout" );
286         return( -1 );
287     }
288
289     /* Init the BitStream */
290     InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
291                    NULL, NULL );
292
293     return( 0 );
294 }
295
296 /*****************************************************************************
297  * DecodeThread: decodes a frame
298  *****************************************************************************/
299 static void DecodeThread( adec_thread_t *p_adec )
300 {
301     aout_buffer_t   *p_aout_buffer;
302     int             i_samples; // per channels
303     int             i_size;
304     pes_packet_t    *p_pes;
305
306     /* **** get samples count **** */
307     p_pes = PESGetFirst( p_adec->p_fifo );
308     
309     i_size = PESGetSize( p_pes );
310     if( p_adec->format.i_blockalign > 0 )
311     {
312         i_size -= i_size % p_adec->format.i_blockalign;
313     }
314     i_size = __MAX( i_size, p_adec->format.i_blockalign );
315
316     if( !i_size || !p_pes )
317     {
318         msg_Err( p_adec->p_fifo, "infinite loop..." );
319         return;
320     }
321     i_samples = i_size / 
322                 ( ( p_adec->format.i_bitspersample + 7 ) / 8 ) / 
323                 p_adec->format.i_channels;
324
325 //    msg_Warn( p_adec->p_fifo, "got %d samples (%d bytes)", i_samples, i_size );
326     p_adec->pts = p_pes->i_pts;
327         
328     /* **** Now we can output these samples **** */
329     
330     if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
331     {
332         aout_DateSet( &p_adec->date, p_adec->pts );
333     }
334     else if( !aout_DateGet( &p_adec->date ) )
335     {
336         return;
337     }
338
339     p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout, 
340                                        p_adec->p_aout_input,
341                                        i_samples );
342     if( !p_aout_buffer )
343     {
344         msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
345         p_adec->p_fifo->b_error = 1;
346         return;
347     }
348     
349     p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
350     p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
351                                                   i_samples );
352     GetChunk( &p_adec->bit_stream,
353               p_aout_buffer->p_buffer,
354               p_aout_buffer->i_nb_bytes );
355
356     aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
357 }
358
359
360 /*****************************************************************************
361  * EndThread : faad decoder thread destruction
362  *****************************************************************************/
363 static void EndThread (adec_thread_t *p_adec)
364 {
365     if( p_adec->p_aout_input )
366     {
367         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
368     }
369
370     FREE( p_adec->format.p_data );
371
372     msg_Dbg( p_adec->p_fifo, "raw audio decoder closed" );
373         
374     free( p_adec );
375 }
376
377