1 /*****************************************************************************
2 * audio.c: audio decoder using ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: audio.c,v 1.9 2002/12/30 17:28:31 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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
36 #include <unistd.h> /* getpid() */
42 #ifdef HAVE_SYS_TIMES_H
43 # include <sys/times.h>
46 #include "aout_internal.h"
48 #include "avcodec.h" /* ffmpeg */
50 //#include "postprocessing/postprocessing.h"
57 int E_( InitThread_Audio ) ( adec_thread_t * );
58 void E_( EndThread_Audio ) ( adec_thread_t * );
59 void E_( DecodeThread_Audio ) ( adec_thread_t * );
61 static unsigned int pi_channels_maps[6] =
64 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
65 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
66 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
67 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
68 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
71 /*****************************************************************************
73 *****************************************************************************/
75 static void ffmpeg_GetWaveFormatEx( waveformatex_t *p_wh,
78 WAVEFORMATEX *p_wfdata = (WAVEFORMATEX*)p_data;
80 p_wh->i_formattag = p_wfdata->wFormatTag;
81 p_wh->i_nb_channels = p_wfdata->nChannels;
82 p_wh->i_samplespersec = p_wfdata->nSamplesPerSec;
83 p_wh->i_avgbytespersec= p_wfdata->nAvgBytesPerSec;
84 p_wh->i_blockalign = p_wfdata->nBlockAlign;
85 p_wh->i_bitspersample = p_wfdata->wBitsPerSample;
86 p_wh->i_size = p_wfdata->cbSize;
90 p_wh->p_data = malloc( p_wh->i_size );
92 p_data + sizeof(WAVEFORMATEX) ,
98 /*****************************************************************************
100 * Functions that initialize, decode and end the decoding process
102 * Functions exported for ffmpeg.c
103 * * E_( InitThread_Audio )
104 * * E_( DecodeThread_Audio )
105 * * E_( EndThread_Video_Audio )
106 *****************************************************************************/
108 /*****************************************************************************
109 * InitThread: initialize vdec output thread
110 *****************************************************************************
111 * This function is called from decoder_Run and performs the second step
112 * of the initialization. It returns 0 on success. Note that the thread's
113 * flag are not modified inside this function.
115 * ffmpeg codec will be open, some memory allocated.
116 *****************************************************************************/
117 int E_( InitThread_Audio )( adec_thread_t *p_adec )
119 if( p_adec->p_fifo->p_demux_data )
121 ffmpeg_GetWaveFormatEx( &p_adec->format,
122 (u8*)p_adec->p_fifo->p_demux_data );
126 msg_Warn( p_adec->p_fifo, "audio informations missing" );
129 /* ***** Fill p_context with init values ***** */
130 p_adec->p_context->sample_rate = p_adec->format.i_samplespersec;
131 p_adec->p_context->channels = p_adec->format.i_nb_channels;
132 #if LIBAVCODEC_BUILD >= 4618
133 p_adec->p_context->block_align = p_adec->format.i_blockalign;
135 p_adec->p_context->bit_rate = p_adec->format.i_avgbytespersec * 8;
137 if( ( p_adec->p_context->extradata_size = p_adec->format.i_size ) > 0 )
139 p_adec->p_context->extradata =
140 malloc( p_adec->format.i_size );
142 memcpy( p_adec->p_context->extradata,
143 p_adec->format.p_data,
144 p_adec->format.i_size );
147 /* ***** Open the codec ***** */
148 if (avcodec_open(p_adec->p_context, p_adec->p_codec) < 0)
150 msg_Err( p_adec->p_fifo,
151 "cannot open codec (%s)",
152 p_adec->psz_namecodec );
157 msg_Dbg( p_adec->p_fifo,
158 "ffmpeg codec (%s) started",
159 p_adec->psz_namecodec );
162 p_adec->p_output = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
165 p_adec->output_format.i_format = AOUT_FMT_S16_NE;
166 p_adec->output_format.i_rate = p_adec->format.i_samplespersec;
167 p_adec->output_format.i_physical_channels
168 = p_adec->output_format.i_original_channels
169 = p_adec->format.i_nb_channels;
171 p_adec->p_aout = NULL;
172 p_adec->p_aout_input = NULL;
178 /*****************************************************************************
179 * DecodeThread: Called for decode one frame
180 *****************************************************************************/
181 void E_( DecodeThread_Audio )( adec_thread_t *p_adec )
184 aout_buffer_t *p_aout_buffer;
186 int i_samplesperchannel;
193 input_ExtractPES( p_adec->p_fifo, &p_pes );
196 p_adec->p_fifo->b_error = 1;
199 p_adec->pts = p_pes->i_pts;
200 i_frame_size = p_pes->i_pes_size;
202 if( i_frame_size > 0 )
204 if( p_adec->i_buffer_size < i_frame_size + 16 )
206 FREE( p_adec->p_buffer );
207 p_adec->p_buffer = malloc( i_frame_size + 16 );
208 p_adec->i_buffer_size = i_frame_size + 16;
211 E_( GetPESData )( p_adec->p_buffer, p_adec->i_buffer_size, p_pes );
213 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
214 } while( i_frame_size <= 0 );
217 i_status = avcodec_decode_audio( p_adec->p_context,
218 (s16*)p_adec->p_output,
224 msg_Warn( p_adec->p_fifo,
225 "cannot decode one frame (%d bytes)",
230 if( i_output_size <= 0 )
232 msg_Warn( p_adec->p_fifo,
233 "decoded %d samples bytes",
237 if( p_adec->p_context->channels <= 0 ||
238 p_adec->p_context->channels > 5 )
240 msg_Warn( p_adec->p_fifo,
241 "invalid channels count %d",
242 p_adec->p_context->channels );
245 /* **** Now we can output these samples **** */
246 i_samplesperchannel = i_output_size / 2
247 / aout_FormatNbChannels( &p_adec->output_format );
248 /* **** First check if we have a valid output **** */
249 if( ( p_adec->p_aout_input == NULL )||
250 ( p_adec->output_format.i_original_channels !=
251 pi_channels_maps[p_adec->p_context->channels] ) )
253 if( p_adec->p_aout_input != NULL )
255 /* **** Delete the old **** */
256 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
259 /* **** Create a new audio output **** */
260 p_adec->output_format.i_physical_channels =
261 p_adec->output_format.i_original_channels =
262 pi_channels_maps[p_adec->p_context->channels];
263 p_adec->output_format.i_bytes_per_frame =
264 p_adec->p_context->channels * sizeof(int16_t);
265 p_adec->output_format.i_frame_length = 1;
267 aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
268 p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
270 &p_adec->output_format );
273 if( !p_adec->p_aout_input )
275 msg_Err( p_adec->p_fifo, "cannot create aout" );
279 if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
281 aout_DateSet( &p_adec->date, p_adec->pts );
283 else if( !aout_DateGet( &p_adec->date ) )
288 p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
289 p_adec->p_aout_input,
290 i_samplesperchannel );
293 msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
294 p_adec->p_fifo->b_error = 1;
298 p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
299 p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
300 i_samplesperchannel );
301 memcpy( p_aout_buffer->p_buffer,
303 p_aout_buffer->i_nb_bytes );
305 aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
311 /*****************************************************************************
312 * EndThread: thread destruction
313 *****************************************************************************
314 * This function is called when the thread ends after a sucessful
316 *****************************************************************************/
317 void E_( EndThread_Audio )( adec_thread_t *p_adec )
319 FREE( p_adec->format.p_data );
321 if( p_adec->p_aout_input )
323 aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );