]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/audio.c
835d5b8cc371793e5f1821838b29793782204311
[vlc] / modules / codec / ffmpeg / audio.c
1 /*****************************************************************************
2  * audio.c: audio decoder using ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: audio.c,v 1.12 2003/01/11 18:10:49 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 <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31 #include <vlc/aout.h>
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
34
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>                                              /* getpid() */
37 #endif
38
39 #include <errno.h>
40 #include <string.h>
41
42 #ifdef HAVE_SYS_TIMES_H
43 #   include <sys/times.h>
44 #endif
45 #include "codecs.h"
46 #include "aout_internal.h"
47
48 #include "avcodec.h"                                            /* ffmpeg */
49
50 //#include "postprocessing/postprocessing.h"
51 #include "ffmpeg.h"
52 #include "audio.h"
53
54 /*
55  * Local prototypes
56  */
57 int      E_( InitThread_Audio )   ( adec_thread_t * );
58 void     E_( EndThread_Audio )    ( adec_thread_t * );
59 void     E_( DecodeThread_Audio ) ( adec_thread_t * );
60
61 static unsigned int pi_channels_maps[6] =
62 {
63     0,
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
69 };  
70
71 /*****************************************************************************
72  * locales Functions
73  *****************************************************************************/
74
75 static void ffmpeg_GetWaveFormatEx( waveformatex_t *p_wh,
76                                     u8 *p_data )
77 {
78     WAVEFORMATEX *p_wfdata = (WAVEFORMATEX*)p_data;
79     
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;
87
88     if( p_wh->i_size )
89     {
90         p_wh->p_data = malloc( p_wh->i_size );
91         memcpy( p_wh->p_data, 
92                 p_data + sizeof(WAVEFORMATEX) , 
93                 p_wh->i_size );
94     }
95 }
96
97
98 /*****************************************************************************
99  *
100  * Functions that initialize, decode and end the decoding process
101  *
102  * Functions exported for ffmpeg.c
103  *   * E_( InitThread_Audio )
104  *   * E_( DecodeThread_Audio )
105  *   * E_( EndThread_Video_Audio )
106  *****************************************************************************/
107
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.
114  *
115  * ffmpeg codec will be open, some memory allocated.
116  *****************************************************************************/
117 int E_( InitThread_Audio )( adec_thread_t *p_adec )
118 {
119     WAVEFORMATEX *p_wf;
120
121     if( ( p_wf = p_adec->p_fifo->p_waveformatex ) != NULL )
122     {
123         ffmpeg_GetWaveFormatEx( &p_adec->format,
124                                 (uint8_t*)p_wf );
125     }
126     else
127     {
128         msg_Warn( p_adec->p_fifo, "audio informations missing" );
129     }
130
131     /* ***** Fill p_context with init values ***** */
132     p_adec->p_context->sample_rate = p_adec->format.i_samplespersec;
133     p_adec->p_context->channels = p_adec->format.i_nb_channels;
134 #if LIBAVCODEC_BUILD >= 4618
135     p_adec->p_context->block_align = p_adec->format.i_blockalign;
136 #endif
137     p_adec->p_context->bit_rate = p_adec->format.i_avgbytespersec * 8;
138
139     if( ( p_adec->p_context->extradata_size = p_adec->format.i_size ) > 0 )
140     {
141         p_adec->p_context->extradata = 
142             malloc( p_adec->format.i_size );
143
144         memcpy( p_adec->p_context->extradata,
145                 p_adec->format.p_data,
146                 p_adec->format.i_size );
147     }
148
149     /* ***** Open the codec ***** */ 
150     if (avcodec_open(p_adec->p_context, p_adec->p_codec) < 0)
151     {
152         msg_Err( p_adec->p_fifo,
153                  "cannot open codec (%s)",
154                  p_adec->psz_namecodec );
155         return( -1 );
156     }
157     else
158     {
159         msg_Dbg( p_adec->p_fifo,
160                  "ffmpeg codec (%s) started",
161                  p_adec->psz_namecodec );
162     }
163
164     p_adec->p_output = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
165
166
167     p_adec->output_format.i_format = AOUT_FMT_S16_NE;
168     p_adec->output_format.i_rate = p_adec->format.i_samplespersec;
169     p_adec->output_format.i_physical_channels
170         = p_adec->output_format.i_original_channels
171         = p_adec->format.i_nb_channels;
172
173     p_adec->p_aout = NULL;
174     p_adec->p_aout_input = NULL;
175
176     return( 0 );
177 }
178
179
180 /*****************************************************************************
181  * DecodeThread: Called for decode one frame
182  *****************************************************************************/
183 void  E_( DecodeThread_Audio )( adec_thread_t *p_adec )
184 {
185     pes_packet_t    *p_pes;
186     aout_buffer_t   *p_aout_buffer;
187
188     int     i_samplesperchannel;
189     int     i_output_size;
190     int     i_frame_size;
191     int     i_used;
192
193     do
194     {
195         input_ExtractPES( p_adec->p_fifo, &p_pes );
196         if( !p_pes )
197         {
198             p_adec->p_fifo->b_error = 1;
199             return;
200         }
201         p_adec->pts = p_pes->i_pts;
202         i_frame_size = p_pes->i_pes_size;
203
204         if( i_frame_size > 0 )
205         {
206             uint8_t *p_last;
207             int     i_need;
208
209
210             i_need = i_frame_size + 16 + p_adec->i_buffer;
211             if( p_adec->i_buffer_size < i_need )
212             {
213                 p_last = p_adec->p_buffer;
214                 p_adec->p_buffer = malloc( i_need );
215                 p_adec->i_buffer_size = i_need;
216                 if( p_adec->i_buffer > 0 )
217                 {
218                     memcpy( p_adec->p_buffer, p_last, p_adec->i_buffer );
219                 }
220                 FREE( p_last );
221             }
222             i_frame_size =
223                 E_( GetPESData )( p_adec->p_buffer + p_adec->i_buffer,
224                                   i_frame_size,
225                                   p_pes );
226             /* make ffmpeg happier but I'm not sure it's needed for audio */
227             memset( p_adec->p_buffer + p_adec->i_buffer + i_frame_size,
228                     0,
229                     16 );
230         }
231         input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
232     } while( i_frame_size <= 0 );
233
234
235     i_frame_size += p_adec->i_buffer;
236
237 usenextdata:
238     i_used = avcodec_decode_audio( p_adec->p_context,
239                                    (int16_t*)p_adec->p_output,
240                                    &i_output_size,
241                                    p_adec->p_buffer,
242                                    i_frame_size );
243     if( i_used < 0 )
244     {
245         msg_Warn( p_adec->p_fifo,
246                   "cannot decode one frame (%d bytes)",
247                   i_frame_size );
248         p_adec->i_buffer = 0;
249         return;
250     }
251     else if( i_used < i_frame_size )
252     {
253         memmove( p_adec->p_buffer,
254                  p_adec->p_buffer + i_used,
255                  p_adec->i_buffer_size - i_used );
256
257         p_adec->i_buffer = i_frame_size - i_used;
258     }
259     else
260     {
261         p_adec->i_buffer = 0;
262     }
263
264     i_frame_size -= i_used;
265
266 //    msg_Dbg( p_adec->p_fifo, "frame size:%d buffer used:%d", i_frame_size, i_used );
267     if( i_output_size <= 0 )
268     {
269          msg_Warn( p_adec->p_fifo, 
270                   "decoded %d samples bytes",
271                   i_output_size );
272     }
273
274     if( p_adec->p_context->channels <= 0 || 
275         p_adec->p_context->channels > 5 )
276     {
277         msg_Warn( p_adec->p_fifo,
278                   "invalid channels count %d",
279                   p_adec->p_context->channels );
280     }
281
282     /* **** Now we can output these samples **** */
283     i_samplesperchannel = i_output_size / 2
284                            / aout_FormatNbChannels( &p_adec->output_format );
285     /* **** First check if we have a valid output **** */
286     if( ( p_adec->p_aout_input == NULL )||
287         ( p_adec->output_format.i_original_channels != 
288                     pi_channels_maps[p_adec->p_context->channels] ) )
289     {
290         if( p_adec->p_aout_input != NULL )
291         {
292             /* **** Delete the old **** */
293             aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
294         }
295
296         /* **** Create a new audio output **** */
297         p_adec->output_format.i_physical_channels = 
298             p_adec->output_format.i_original_channels = 
299                 pi_channels_maps[p_adec->p_context->channels];
300
301         aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
302         p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
303                                             &p_adec->p_aout,
304                                             &p_adec->output_format );
305     }
306
307     if( !p_adec->p_aout_input )
308     {
309         msg_Err( p_adec->p_fifo, "cannot create aout" );
310         return;
311     }
312
313     if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
314     {
315         aout_DateSet( &p_adec->date, p_adec->pts );
316     }
317     else if( !aout_DateGet( &p_adec->date ) )
318     {
319         return;
320     }
321
322     p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
323                                        p_adec->p_aout_input,
324                                        i_samplesperchannel );
325     if( !p_aout_buffer )
326     {
327         msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
328         p_adec->p_fifo->b_error = 1;
329         return;
330     }
331
332     p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
333     p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
334                                                   i_samplesperchannel );
335     memcpy( p_aout_buffer->p_buffer,
336             p_adec->p_output,
337             p_aout_buffer->i_nb_bytes );
338
339     aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
340
341     if( i_frame_size > 0 )
342     {
343         goto usenextdata;
344     }
345
346     return;
347 }
348
349
350 /*****************************************************************************
351  * EndThread: thread destruction
352  *****************************************************************************
353  * This function is called when the thread ends after a sucessful
354  * initialization.
355  *****************************************************************************/
356 void E_( EndThread_Audio )( adec_thread_t *p_adec )
357 {
358     FREE( p_adec->format.p_data );
359
360     if( p_adec->p_aout_input )
361     {
362         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
363     }
364     
365 }
366