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