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