1 /*****************************************************************************
2 * araw.c: Pseudo audio decoder; for raw pcm data
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: araw.c,v 1.4 2002/10/24 09:37:48 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc/decoder.h>
30 #include <vlc/input.h>
32 #include <stdlib.h> /* malloc(), free() */
33 #include <string.h> /* strdup() */
35 /*****************************************************************************
37 *****************************************************************************/
38 typedef struct waveformatex_s
46 u16 i_size; /* the extra size in bytes */
47 u8 *p_data; /* The extra data */
50 typedef struct adec_thread_s
52 waveformatex_t format;
54 /* The bit stream structure handles the PES stream at the bit level */
55 // bit_stream_t bit_stream;
57 /* Input properties */
58 decoder_fifo_t *p_fifo;
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;
70 static int OpenDecoder ( vlc_object_t * );
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 * );
77 /*****************************************************************************
79 *****************************************************************************/
82 set_description( _("Pseudo Raw Audio decoder") );
83 set_capability( "decoder", 50 );
84 set_callbacks( OpenDecoder, NULL );
88 static int i_channels_maps[6] =
91 AOUT_CHAN_MONO, AOUT_CHAN_STEREO,
92 AOUT_CHAN_3F, AOUT_CHAN_2F2R,
96 /*****************************************************************************
97 * OpenDecoder: probe the decoder and return score
98 *****************************************************************************
99 * Tries to launch a decoder and return score so that the interface is able
101 *****************************************************************************/
102 static int OpenDecoder( vlc_object_t *p_this )
104 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
106 switch( p_fifo->i_fourcc )
108 case VLC_FOURCC('a','r','a','w'):
109 case VLC_FOURCC('t','w','o','s'): /* for mov file */
110 p_fifo->pf_run = RunDecoder;
119 /*****************************************************************************
120 * RunDecoder: this function is called just after the thread is created
121 *****************************************************************************/
122 static int RunDecoder( decoder_fifo_t *p_fifo )
124 adec_thread_t *p_adec;
127 if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
129 msg_Err( p_fifo, "out of memory" );
130 DecoderError( p_fifo );
133 memset( p_adec, 0, sizeof( adec_thread_t ) );
135 p_adec->p_fifo = p_fifo;
137 if( InitThread( p_adec ) != 0 )
139 DecoderError( p_fifo );
143 while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
145 DecodeThread( p_adec );
149 if( ( b_error = p_adec->p_fifo->b_error ) )
151 DecoderError( p_adec->p_fifo );
164 #define FREE( p ) if( p ) free( p ); p = NULL
165 #define GetWLE( p ) \
166 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
168 #define GetDWLE( p ) \
169 ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
170 ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
172 static void GetWaveFormatEx( waveformatex_t *p_wh,
176 p_wh->i_formattag = GetWLE( p_data );
177 p_wh->i_channels = GetWLE( p_data + 2 );
178 p_wh->i_samplespersec = GetDWLE( p_data + 4 );
179 p_wh->i_avgbytespersec= GetDWLE( p_data + 8 );
180 p_wh->i_blockalign = GetWLE( p_data + 12 );
181 p_wh->i_bitspersample = GetWLE( p_data + 14 );
182 if( p_wh->i_formattag != 1 )
184 p_wh->i_size = GetWLE( p_data + 16 );
188 p_wh->p_data = malloc( p_wh->i_size );
189 memcpy( p_wh->p_data, p_data + 18, p_wh->i_size );
194 /*****************************************************************************
195 * InitThread: initialize data before entering main loop
196 *****************************************************************************/
198 static int InitThread( adec_thread_t * p_adec )
201 if( p_adec->p_fifo->p_demux_data )
203 GetWaveFormatEx( &p_adec->format,
204 (u8*)p_adec->p_fifo->p_demux_data );
205 /* fixing some values */
206 if( p_adec->format.i_formattag == 1 && !p_adec->format.i_blockalign )
208 p_adec->format.i_blockalign = p_adec->format.i_channels *
209 ( ( p_adec->format.i_bitspersample + 7 ) / 8 );
214 msg_Err( p_adec->p_fifo, "unknown raw format" );
218 msg_Dbg( p_adec->p_fifo,
219 "raw format: samplerate:%dHz channels:%d bits/sample:%d blockalign:%d",
220 p_adec->format.i_samplespersec,
221 p_adec->format.i_channels,
222 p_adec->format.i_bitspersample, p_adec->format.i_blockalign );
224 /* Initialize the thread properties */
225 switch( ( p_adec->format.i_bitspersample + 7 ) / 8 )
228 p_adec->output_format.i_format = VLC_FOURCC('s','1','6','l');
231 p_adec->output_format.i_format = VLC_FOURCC('s','2','4','l');
234 p_adec->output_format.i_format = VLC_FOURCC('s','3','2','l');
239 msg_Err( p_adec->p_fifo, "bad parameters(bits/sample)" );
242 p_adec->output_format.i_rate = p_adec->format.i_samplespersec;
244 if( p_adec->format.i_channels <= 0 ||
245 p_adec->format.i_channels > 5 )
247 msg_Err( p_adec->p_fifo, "bad channels count(1-5)" );
251 p_adec->output_format.i_channels =
252 i_channels_maps[p_adec->format.i_channels];
253 p_adec->p_aout = NULL;
254 p_adec->p_aout_input = NULL;
256 /* **** Create a new audio output **** */
257 aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
258 p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
260 &p_adec->output_format );
261 if( !p_adec->p_aout_input )
263 msg_Err( p_adec->p_fifo, "cannot create aout" );
267 /* Init the BitStream */
268 // InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
274 static void GetPESData( u8 *p_buf, int i_max, pes_packet_t *p_pes )
279 data_packet_t *p_data;
282 p_data = p_pes->p_first;
283 while( p_data != NULL && i_count < i_max )
286 i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start, i_max - i_count );
291 p_data->p_payload_start,
295 p_data = p_data->p_next;
300 if( i_count < i_max )
302 memset( p_buf, 0, i_max - i_count );
306 /*****************************************************************************
307 * DecodeThread: decodes a frame
308 *****************************************************************************/
309 static void DecodeThread( adec_thread_t *p_adec )
311 aout_buffer_t *p_aout_buffer;
312 int i_samples; // per channels
317 /* **** get samples count **** */
318 if( input_ExtractPES( p_adec->p_fifo, &p_pes ) < 0 )
320 p_adec->p_fifo->b_error = 1;
323 i_size = p_pes->i_pes_size;
325 if( p_adec->format.i_blockalign > 0 )
327 i_size -= i_size % p_adec->format.i_blockalign;
329 i_size = __MAX( i_size, p_adec->format.i_blockalign );
331 if( !i_size || !p_pes )
333 msg_Err( p_adec->p_fifo, "infinite loop..." );
337 ( ( p_adec->format.i_bitspersample + 7 ) / 8 ) /
338 p_adec->format.i_channels;
340 // msg_Warn( p_adec->p_fifo, "got %d samples (%d bytes)", i_samples, i_size );
341 p_adec->pts = p_pes->i_pts;
343 /* **** Now we can output these samples **** */
345 if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
347 aout_DateSet( &p_adec->date, p_adec->pts );
349 else if( !aout_DateGet( &p_adec->date ) )
354 p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
355 p_adec->p_aout_input,
359 msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
360 p_adec->p_fifo->b_error = 1;
364 p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
365 p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
368 GetPESData( p_aout_buffer->p_buffer, p_aout_buffer->i_nb_bytes, p_pes );
370 aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
373 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
377 /*****************************************************************************
378 * EndThread : faad decoder thread destruction
379 *****************************************************************************/
380 static void EndThread (adec_thread_t *p_adec)
382 if( p_adec->p_aout_input )
384 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
387 FREE( p_adec->format.p_data );
389 msg_Dbg( p_adec->p_fifo, "raw audio decoder closed" );