]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/audio.c
Update references to ffmpeg header files to match new directory structure. All ffmpe...
[vlc] / modules / codec / ffmpeg / audio.c
1 /*****************************************************************************
2  * audio.c: audio decoder using ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_aout.h>
34 #include <vlc_codec.h>
35 #include <vlc_input.h>
36
37 /* ffmpeg header */
38 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
39 #   include <libavcodec/avcodec.h>
40 #else
41 #   include <avcodec.h>
42 #endif
43
44 #include "ffmpeg.h"
45
46 static unsigned int pi_channels_maps[7] =
47 {
48     0,
49     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
50     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
51     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
52      | AOUT_CHAN_REARRIGHT,
53     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
54      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
55     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
56      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
57 };
58
59 /*****************************************************************************
60  * decoder_sys_t : decoder descriptor
61  *****************************************************************************/
62 struct decoder_sys_t
63 {
64     FFMPEG_COMMON_MEMBERS
65
66     /* Temporary buffer for libavcodec */
67     uint8_t *p_output;
68
69     /*
70      * Output properties
71      */
72     audio_sample_format_t aout_format;
73     audio_date_t          end_date;
74
75     /*
76      *
77      */
78     uint8_t *p_samples;
79     int     i_samples;
80
81     /* */
82     int     i_reject_count;
83
84     int i_input_rate;
85 };
86
87 /*****************************************************************************
88  * InitAudioDec: initialize audio decoder
89  *****************************************************************************
90  * The ffmpeg codec will be opened, some memory allocated.
91  *****************************************************************************/
92 int E_(InitAudioDec)( decoder_t *p_dec, AVCodecContext *p_context,
93                       AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
94 {
95     decoder_sys_t *p_sys;
96
97     /* Allocate the memory needed to store the decoder's structure */
98     if( ( p_dec->p_sys = p_sys =
99           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
100     {
101         msg_Err( p_dec, "out of memory" );
102         return VLC_ENOMEM;
103     }
104
105     p_sys->p_context = p_context;
106     p_sys->p_codec = p_codec;
107     p_sys->i_codec_id = i_codec_id;
108     p_sys->psz_namecodec = psz_namecodec;
109
110     /* ***** Fill p_context with init values ***** */
111     p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
112     p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
113     p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
114     p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
115     p_sys->p_context->bits_per_sample = p_dec->fmt_in.audio.i_bitspersample;
116
117     if( ( p_sys->p_context->extradata_size = p_dec->fmt_in.i_extra ) > 0 )
118     {
119         int i_offset = 0;
120
121         if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
122             i_offset = 8;
123
124         p_sys->p_context->extradata_size -= i_offset;
125         p_sys->p_context->extradata =
126             malloc( p_sys->p_context->extradata_size +
127                     FF_INPUT_BUFFER_PADDING_SIZE );
128         memcpy( p_sys->p_context->extradata,
129                 (char*)p_dec->fmt_in.p_extra + i_offset,
130                 p_sys->p_context->extradata_size );
131         memset( (char*)p_sys->p_context->extradata +
132                 p_sys->p_context->extradata_size, 0,
133                 FF_INPUT_BUFFER_PADDING_SIZE );
134     }
135     else
136         p_sys->p_context->extradata = NULL;
137
138     /* ***** Open the codec ***** */
139     vlc_mutex_t *lock = var_AcquireMutex( "avcodec" );
140     if( lock == NULL )
141     {
142         free( p_sys->p_context->extradata );
143         free( p_sys );
144         return VLC_ENOMEM;
145     }
146
147     if (avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0)
148     {
149         vlc_mutex_unlock( lock );
150         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
151         free( p_sys->p_context->extradata );
152         free( p_sys );
153         return VLC_EGENERIC;
154     }
155     vlc_mutex_unlock( lock );
156
157     msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
158
159     p_sys->p_output = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
160     p_sys->p_samples = NULL;
161     p_sys->i_samples = 0;
162     p_sys->i_reject_count = 0;
163     p_sys->i_input_rate = INPUT_RATE_DEFAULT;
164
165     aout_DateSet( &p_sys->end_date, 0 );
166     if( p_dec->fmt_in.audio.i_rate )
167         aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
168
169     /* Set output properties */
170     p_dec->fmt_out.i_cat = AUDIO_ES;
171     p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
172     p_dec->fmt_out.audio.i_bitspersample = 16;
173
174     return VLC_SUCCESS;
175 }
176
177 /*****************************************************************************
178  * SplitBuffer: Needed because aout really doesn't like big audio chunk and
179  * wma produces easily > 30000 samples...
180  *****************************************************************************/
181 static aout_buffer_t *SplitBuffer( decoder_t *p_dec )
182 {
183     decoder_sys_t *p_sys = p_dec->p_sys;
184     int i_samples = __MIN( p_sys->i_samples, 4096 );
185     aout_buffer_t *p_buffer;
186
187     if( i_samples == 0 ) return NULL;
188
189     if( ( p_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples ) ) == NULL )
190     {
191         msg_Err( p_dec, "cannot get aout buffer" );
192         return NULL;
193     }
194
195     p_buffer->start_date = aout_DateGet( &p_sys->end_date );
196     p_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
197                                              i_samples * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
198
199     memcpy( p_buffer->p_buffer, p_sys->p_samples, p_buffer->i_nb_bytes );
200
201     p_sys->p_samples += p_buffer->i_nb_bytes;
202     p_sys->i_samples -= i_samples;
203
204     return p_buffer;
205 }
206
207 /*****************************************************************************
208  * DecodeAudio: Called to decode one frame
209  *****************************************************************************/
210 aout_buffer_t *E_( DecodeAudio )( decoder_t *p_dec, block_t **pp_block )
211 {
212     decoder_sys_t *p_sys = p_dec->p_sys;
213     int i_used, i_output;
214     aout_buffer_t *p_buffer;
215     block_t *p_block;
216
217     if( !pp_block || !*pp_block ) return NULL;
218
219     p_block = *pp_block;
220
221     if( p_block->i_rate > 0 )
222         p_sys->i_input_rate = p_block->i_rate;
223
224     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
225     {
226         block_Release( p_block );
227         avcodec_flush_buffers( p_sys->p_context );
228
229         if( p_sys->i_codec_id == CODEC_ID_MP2 || p_sys->i_codec_id == CODEC_ID_MP3 )
230             p_sys->i_reject_count = 3;
231         return NULL;
232     }
233
234     if( p_sys->i_samples > 0 )
235     {
236         /* More data */
237         p_buffer = SplitBuffer( p_dec );
238         if( !p_buffer ) block_Release( p_block );
239         return p_buffer;
240     }
241
242     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
243     {
244         /* We've just started the stream, wait for the first PTS. */
245         block_Release( p_block );
246         return NULL;
247     }
248
249     if( p_block->i_buffer <= 0 )
250     {
251         block_Release( p_block );
252         return NULL;
253     }
254     if( p_block->i_buffer > AVCODEC_MAX_AUDIO_FRAME_SIZE )
255     {
256         /* Grow output buffer if necessary (eg. for PCM data) */
257         p_sys->p_output = realloc(p_sys->p_output, p_block->i_buffer);
258     }
259
260     *pp_block = p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
261     if( !p_block )
262         return NULL;
263     p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
264     memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
265
266 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(0<<8)+0)
267     i_output = __MAX( AVCODEC_MAX_AUDIO_FRAME_SIZE, p_block->i_buffer );
268     i_used = avcodec_decode_audio2( p_sys->p_context,
269                                    (int16_t*)p_sys->p_output, &i_output,
270                                    p_block->p_buffer, p_block->i_buffer );
271 #else
272     i_used = avcodec_decode_audio( p_sys->p_context,
273                                    (int16_t*)p_sys->p_output, &i_output,
274                                    p_block->p_buffer, p_block->i_buffer );
275 #endif
276
277     if( i_used < 0 || i_output < 0 )
278     {
279         if( i_used < 0 )
280             msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
281                       p_block->i_buffer );
282
283         block_Release( p_block );
284         return NULL;
285     }
286     else if( (size_t)i_used > p_block->i_buffer )
287     {
288         i_used = p_block->i_buffer;
289     }
290
291     p_block->i_buffer -= i_used;
292     p_block->p_buffer += i_used;
293
294     if( p_sys->p_context->channels <= 0 || p_sys->p_context->channels > 6 )
295     {
296         msg_Warn( p_dec, "invalid channels count %d",
297                   p_sys->p_context->channels );
298         block_Release( p_block );
299         return NULL;
300     }
301
302     if( p_dec->fmt_out.audio.i_rate != (unsigned int)p_sys->p_context->sample_rate )
303     {
304         aout_DateInit( &p_sys->end_date, p_sys->p_context->sample_rate );
305         aout_DateSet( &p_sys->end_date, p_block->i_pts );
306     }
307
308     /* **** Set audio output parameters **** */
309     p_dec->fmt_out.audio.i_rate     = p_sys->p_context->sample_rate;
310     p_dec->fmt_out.audio.i_channels = p_sys->p_context->channels;
311     p_dec->fmt_out.audio.i_original_channels =
312         p_dec->fmt_out.audio.i_physical_channels =
313             pi_channels_maps[p_sys->p_context->channels];
314
315     if( p_block->i_pts != 0 &&
316         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
317     {
318         aout_DateSet( &p_sys->end_date, p_block->i_pts );
319     }
320     p_block->i_pts = 0;
321
322     /* **** Now we can output these samples **** */
323     p_sys->i_samples = i_output / sizeof(int16_t) / p_sys->p_context->channels;
324     p_sys->p_samples = p_sys->p_output;
325
326     /* Silent unwanted samples */
327     if( p_sys->i_reject_count > 0 )
328     {
329         memset( p_sys->p_output, 0, i_output );
330         p_sys->i_reject_count--;
331     }
332
333     p_buffer = SplitBuffer( p_dec );
334     if( !p_buffer ) block_Release( p_block );
335     return p_buffer;
336 }
337
338 /*****************************************************************************
339  * EndAudioDec: audio decoder destruction
340  *****************************************************************************/
341 void E_(EndAudioDec)( decoder_t *p_dec )
342 {
343     decoder_sys_t *p_sys = p_dec->p_sys;
344
345     if( p_sys->p_output ) free( p_sys->p_output );
346 }