]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/audio.c
34ef406848b777a30a5f3e4154d896103da18106
[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.15 2003/04/17 10:58:30 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 /*****************************************************************************
76  *
77  * Functions that initialize, decode and end the decoding process
78  *
79  * Functions exported for ffmpeg.c
80  *   * E_( InitThread_Audio )
81  *   * E_( DecodeThread_Audio )
82  *   * E_( EndThread_Video_Audio )
83  *****************************************************************************/
84
85 /*****************************************************************************
86  * InitThread: initialize vdec output thread
87  *****************************************************************************
88  * This function is called from decoder_Run and performs the second step
89  * of the initialization. It returns 0 on success. Note that the thread's
90  * flag are not modified inside this function.
91  *
92  * ffmpeg codec will be open, some memory allocated.
93  *****************************************************************************/
94 int E_( InitThread_Audio )( adec_thread_t *p_adec )
95 {
96     WAVEFORMATEX wf, *p_wf;
97
98     if( ( p_wf = p_adec->p_fifo->p_waveformatex ) == NULL )
99     {
100         msg_Warn( p_adec->p_fifo, "audio informations missing" );
101         p_wf = &wf;
102         memset( p_wf, 0, sizeof( WAVEFORMATEX ) );
103     }
104
105     /* ***** Fill p_context with init values ***** */
106     p_adec->p_context->sample_rate = p_wf->nSamplesPerSec;
107     p_adec->p_context->channels = p_wf->nChannels;
108     p_adec->p_context->block_align = p_wf->nBlockAlign;
109     p_adec->p_context->bit_rate = p_wf->nAvgBytesPerSec * 8;
110
111     if( ( p_adec->p_context->extradata_size = p_wf->cbSize ) > 0 )
112     {
113         p_adec->p_context->extradata =
114             malloc( p_wf->cbSize );
115
116         memcpy( p_adec->p_context->extradata,
117                 &p_wf[1],
118                 p_wf->cbSize);
119     }
120
121     /* ***** Open the codec ***** */
122     if (avcodec_open(p_adec->p_context, p_adec->p_codec) < 0)
123     {
124         msg_Err( p_adec->p_fifo,
125                  "cannot open codec (%s)",
126                  p_adec->psz_namecodec );
127         return( -1 );
128     }
129     else
130     {
131         msg_Dbg( p_adec->p_fifo,
132                  "ffmpeg codec (%s) started",
133                  p_adec->psz_namecodec );
134     }
135
136     p_adec->p_output = malloc( 3 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
137
138
139     p_adec->output_format.i_format = AOUT_FMT_S16_NE;
140     p_adec->output_format.i_rate = p_wf->nSamplesPerSec;
141     p_adec->output_format.i_physical_channels
142         = p_adec->output_format.i_original_channels
143         = pi_channels_maps[p_wf->nChannels];
144
145     p_adec->p_aout = NULL;
146     p_adec->p_aout_input = NULL;
147
148     return( 0 );
149 }
150
151
152 /*****************************************************************************
153  * DecodeThread: Called for decode one frame
154  *****************************************************************************/
155 void  E_( DecodeThread_Audio )( adec_thread_t *p_adec )
156 {
157     pes_packet_t    *p_pes;
158     aout_buffer_t   *p_aout_buffer;
159
160     int     i_samplesperchannel;
161     int     i_output_size;
162     int     i_frame_size;
163     int     i_used;
164
165     do
166     {
167         input_ExtractPES( p_adec->p_fifo, &p_pes );
168         if( !p_pes )
169         {
170             p_adec->p_fifo->b_error = 1;
171             return;
172         }
173         p_adec->pts = p_pes->i_pts;
174         i_frame_size = p_pes->i_pes_size;
175
176         if( i_frame_size > 0 )
177         {
178             uint8_t *p_last;
179             int     i_need;
180
181
182             i_need = i_frame_size + 16 + p_adec->i_buffer;
183             if( p_adec->i_buffer_size < i_need )
184             {
185                 p_last = p_adec->p_buffer;
186                 p_adec->p_buffer = malloc( i_need );
187                 p_adec->i_buffer_size = i_need;
188                 if( p_adec->i_buffer > 0 )
189                 {
190                     memcpy( p_adec->p_buffer, p_last, p_adec->i_buffer );
191                 }
192                 FREE( p_last );
193             }
194             i_frame_size =
195                 E_( GetPESData )( p_adec->p_buffer + p_adec->i_buffer,
196                                   i_frame_size,
197                                   p_pes );
198             /* make ffmpeg happier but I'm not sure it's needed for audio */
199             memset( p_adec->p_buffer + p_adec->i_buffer + i_frame_size,
200                     0,
201                     16 );
202         }
203         input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
204     } while( i_frame_size <= 0 );
205
206
207     i_frame_size += p_adec->i_buffer;
208
209 usenextdata:
210     i_used = avcodec_decode_audio( p_adec->p_context,
211                                    (int16_t*)p_adec->p_output,
212                                    &i_output_size,
213                                    p_adec->p_buffer,
214                                    i_frame_size );
215     if( i_used < 0 )
216     {
217         msg_Warn( p_adec->p_fifo,
218                   "cannot decode one frame (%d bytes)",
219                   i_frame_size );
220         p_adec->i_buffer = 0;
221         return;
222     }
223     else if( i_used < i_frame_size )
224     {
225         memmove( p_adec->p_buffer,
226                  p_adec->p_buffer + i_used,
227                  p_adec->i_buffer_size - i_used );
228
229         p_adec->i_buffer = i_frame_size - i_used;
230     }
231     else
232     {
233         p_adec->i_buffer = 0;
234     }
235
236     i_frame_size -= i_used;
237
238 //    msg_Dbg( p_adec->p_fifo, "frame size:%d buffer used:%d", i_frame_size, i_used );
239     if( i_output_size <= 0 )
240     {
241          msg_Warn( p_adec->p_fifo, 
242                   "decoded %d samples bytes",
243                   i_output_size );
244     }
245
246     if( p_adec->p_context->channels <= 0 ||
247         p_adec->p_context->channels > 5 )
248     {
249         msg_Warn( p_adec->p_fifo,
250                   "invalid channels count %d",
251                   p_adec->p_context->channels );
252     }
253
254     /* **** Now we can output these samples **** */
255     i_samplesperchannel = i_output_size / 2
256                            / aout_FormatNbChannels( &p_adec->output_format );
257     /* **** First check if we have a valid output **** */
258     if( ( p_adec->p_aout_input == NULL )||
259         ( p_adec->output_format.i_original_channels !=
260                     pi_channels_maps[p_adec->p_context->channels] ) )
261     {
262         if( p_adec->p_aout_input != NULL )
263         {
264             /* **** Delete the old **** */
265             aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
266         }
267
268         /* **** Create a new audio output **** */
269         p_adec->output_format.i_physical_channels =
270             p_adec->output_format.i_original_channels =
271                 pi_channels_maps[p_adec->p_context->channels];
272
273         aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
274         p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
275                                             &p_adec->p_aout,
276                                             &p_adec->output_format );
277     }
278
279     if( !p_adec->p_aout_input )
280     {
281         msg_Err( p_adec->p_fifo, "cannot create aout" );
282         return;
283     }
284
285     if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
286     {
287         aout_DateSet( &p_adec->date, p_adec->pts );
288     }
289     else if( !aout_DateGet( &p_adec->date ) )
290     {
291         return;
292     }
293
294     p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
295                                        p_adec->p_aout_input,
296                                        i_samplesperchannel );
297     if( !p_aout_buffer )
298     {
299         msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
300         p_adec->p_fifo->b_error = 1;
301         return;
302     }
303
304     p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
305     p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
306                                                   i_samplesperchannel );
307     memcpy( p_aout_buffer->p_buffer,
308             p_adec->p_output,
309             p_aout_buffer->i_nb_bytes );
310
311     aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
312
313     if( i_frame_size > 0 )
314     {
315         goto usenextdata;
316     }
317
318     return;
319 }
320
321
322 /*****************************************************************************
323  * EndThread: thread destruction
324  *****************************************************************************
325  * This function is called when the thread ends after a sucessful
326  * initialization.
327  *****************************************************************************/
328 void E_( EndThread_Audio )( adec_thread_t *p_adec )
329 {
330 //    FREE( p_adec->format.p_data );
331     FREE( p_adec->p_output );
332
333     if( p_adec->p_aout_input )
334     {
335         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
336     }
337
338 }
339