]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/audio.c
Make PIX_FMT_RGBA dependend on ffmpeg with swscale. Patch by Remi Duraffort <ivoire...
[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 #include <vlc/vlc.h>
29 #include <vlc_aout.h>
30 #include <vlc_codec.h>
31 #include <vlc_input.h>
32
33 /* ffmpeg header */
34 #ifdef HAVE_FFMPEG_AVCODEC_H
35 #   include <ffmpeg/avcodec.h>
36 #else
37 #   include <avcodec.h>
38 #endif
39
40 #include "ffmpeg.h"
41
42 static unsigned int pi_channels_maps[7] =
43 {
44     0,
45     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
46     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
47     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
48      | AOUT_CHAN_REARRIGHT,
49     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
50      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
51     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
52      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
53 };
54
55 /*****************************************************************************
56  * decoder_sys_t : decoder descriptor
57  *****************************************************************************/
58 struct decoder_sys_t
59 {
60     FFMPEG_COMMON_MEMBERS
61
62     /* Temporary buffer for libavcodec */
63     uint8_t *p_output;
64
65     /*
66      * Output properties
67      */
68     audio_sample_format_t aout_format;
69     audio_date_t          end_date;
70
71     /*
72      *
73      */
74     uint8_t *p_samples;
75     int     i_samples;
76
77     /* */
78     int     i_reject_count;
79
80     int i_input_rate;
81 };
82
83 /*****************************************************************************
84  * InitAudioDec: initialize audio decoder
85  *****************************************************************************
86  * The ffmpeg codec will be opened, some memory allocated.
87  *****************************************************************************/
88 int E_(InitAudioDec)( decoder_t *p_dec, AVCodecContext *p_context,
89                       AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
90 {
91     decoder_sys_t *p_sys;
92
93     /* Allocate the memory needed to store the decoder's structure */
94     if( ( p_dec->p_sys = p_sys =
95           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
96     {
97         msg_Err( p_dec, "out of memory" );
98         return VLC_ENOMEM;
99     }
100
101     p_sys->p_context = p_context;
102     p_sys->p_codec = p_codec;
103     p_sys->i_codec_id = i_codec_id;
104     p_sys->psz_namecodec = psz_namecodec;
105
106     /* ***** Fill p_context with init values ***** */
107     p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
108     p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
109     p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
110     p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
111     p_sys->p_context->bits_per_sample = p_dec->fmt_in.audio.i_bitspersample;
112
113     if( ( p_sys->p_context->extradata_size = p_dec->fmt_in.i_extra ) > 0 )
114     {
115         int i_offset = 0;
116
117         if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
118             i_offset = 8;
119
120         p_sys->p_context->extradata_size -= i_offset;
121         p_sys->p_context->extradata =
122             malloc( p_sys->p_context->extradata_size +
123                     FF_INPUT_BUFFER_PADDING_SIZE );
124         memcpy( p_sys->p_context->extradata,
125                 (char*)p_dec->fmt_in.p_extra + i_offset,
126                 p_sys->p_context->extradata_size );
127         memset( (char*)p_sys->p_context->extradata +
128                 p_sys->p_context->extradata_size, 0,
129                 FF_INPUT_BUFFER_PADDING_SIZE );
130     }
131     else
132         p_sys->p_context->extradata = NULL;
133
134     /* ***** Open the codec ***** */
135     vlc_mutex_t *lock = var_AcquireMutex( "avcodec" );
136     if( lock == NULL )
137     {
138         free( p_sys->p_context->extradata );
139         free( p_sys );
140         return VLC_ENOMEM;
141     }
142
143     if (avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0)
144     {
145         vlc_mutex_unlock( lock );
146         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
147         free( p_sys->p_context->extradata );
148         free( p_sys );
149         return VLC_EGENERIC;
150     }
151     vlc_mutex_unlock( lock );
152
153     msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
154
155     p_sys->p_output = malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
156     p_sys->p_samples = NULL;
157     p_sys->i_samples = 0;
158     p_sys->i_reject_count = 0;
159     p_sys->i_input_rate = INPUT_RATE_DEFAULT;
160
161     aout_DateSet( &p_sys->end_date, 0 );
162     if( p_dec->fmt_in.audio.i_rate )
163         aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
164
165     /* Set output properties */
166     p_dec->fmt_out.i_cat = AUDIO_ES;
167     p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
168     p_dec->fmt_out.audio.i_bitspersample = 16;
169
170     return VLC_SUCCESS;
171 }
172
173 /*****************************************************************************
174  * SplitBuffer: Needed because aout really doesn't like big audio chunk and
175  * wma produces easily > 30000 samples...
176  *****************************************************************************/
177 static aout_buffer_t *SplitBuffer( decoder_t *p_dec )
178 {
179     decoder_sys_t *p_sys = p_dec->p_sys;
180     int i_samples = __MIN( p_sys->i_samples, 4096 );
181     aout_buffer_t *p_buffer;
182
183     if( i_samples == 0 ) return NULL;
184
185     if( ( p_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples ) ) == NULL )
186     {
187         msg_Err( p_dec, "cannot get aout buffer" );
188         return NULL;
189     }
190
191     p_buffer->start_date = aout_DateGet( &p_sys->end_date );
192     p_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
193                                              i_samples * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
194
195     memcpy( p_buffer->p_buffer, p_sys->p_samples, p_buffer->i_nb_bytes );
196
197     p_sys->p_samples += p_buffer->i_nb_bytes;
198     p_sys->i_samples -= i_samples;
199
200     return p_buffer;
201 }
202
203 /*****************************************************************************
204  * DecodeAudio: Called to decode one frame
205  *****************************************************************************/
206 aout_buffer_t *E_( DecodeAudio )( decoder_t *p_dec, block_t **pp_block )
207 {
208     decoder_sys_t *p_sys = p_dec->p_sys;
209     int i_used, i_output;
210     aout_buffer_t *p_buffer;
211     block_t *p_block;
212
213     if( !pp_block || !*pp_block ) return NULL;
214
215     p_block = *pp_block;
216
217     if( p_block->i_rate > 0 )
218         p_sys->i_input_rate = p_block->i_rate;
219
220     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
221     {
222         block_Release( p_block );
223         avcodec_flush_buffers( p_sys->p_context );
224
225         if( p_sys->i_codec_id == CODEC_ID_MP2 || p_sys->i_codec_id == CODEC_ID_MP3 )
226             p_sys->i_reject_count = 3;
227         return NULL;
228     }
229
230     if( p_sys->i_samples > 0 )
231     {
232         /* More data */
233         p_buffer = SplitBuffer( p_dec );
234         if( !p_buffer ) block_Release( p_block );
235         return p_buffer;
236     }
237
238     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
239     {
240         /* We've just started the stream, wait for the first PTS. */
241         block_Release( p_block );
242         return NULL;
243     }
244
245     if( p_block->i_buffer <= 0 )
246     {
247         block_Release( p_block );
248         return NULL;
249     }
250     if( p_block->i_buffer > AVCODEC_MAX_AUDIO_FRAME_SIZE )
251     {
252         /* Grow output buffer if necessary (eg. for PCM data) */
253         p_sys->p_output = realloc(p_sys->p_output, p_block->i_buffer);
254     }
255
256     *pp_block = p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
257     if( !p_block )
258         return NULL;
259     p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
260     memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
261
262 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(0<<8)+0)
263     i_output = __MAX( AVCODEC_MAX_AUDIO_FRAME_SIZE, p_block->i_buffer );
264     i_used = avcodec_decode_audio2( p_sys->p_context,
265                                    (int16_t*)p_sys->p_output, &i_output,
266                                    p_block->p_buffer, p_block->i_buffer );
267 #else
268     i_used = avcodec_decode_audio( p_sys->p_context,
269                                    (int16_t*)p_sys->p_output, &i_output,
270                                    p_block->p_buffer, p_block->i_buffer );
271 #endif
272
273     if( i_used < 0 || i_output < 0 )
274     {
275         if( i_used < 0 )
276             msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
277                       p_block->i_buffer );
278
279         block_Release( p_block );
280         return NULL;
281     }
282     else if( i_used > p_block->i_buffer )
283     {
284         i_used = p_block->i_buffer;
285     }
286
287     p_block->i_buffer -= i_used;
288     p_block->p_buffer += i_used;
289
290     if( p_sys->p_context->channels <= 0 || p_sys->p_context->channels > 6 )
291     {
292         msg_Warn( p_dec, "invalid channels count %d",
293                   p_sys->p_context->channels );
294         block_Release( p_block );
295         return NULL;
296     }
297
298     if( p_dec->fmt_out.audio.i_rate != (unsigned int)p_sys->p_context->sample_rate )
299     {
300         aout_DateInit( &p_sys->end_date, p_sys->p_context->sample_rate );
301         aout_DateSet( &p_sys->end_date, p_block->i_pts );
302     }
303
304     /* **** Set audio output parameters **** */
305     p_dec->fmt_out.audio.i_rate     = p_sys->p_context->sample_rate;
306     p_dec->fmt_out.audio.i_channels = p_sys->p_context->channels;
307     p_dec->fmt_out.audio.i_original_channels =
308         p_dec->fmt_out.audio.i_physical_channels =
309             pi_channels_maps[p_sys->p_context->channels];
310
311     if( p_block->i_pts != 0 &&
312         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
313     {
314         aout_DateSet( &p_sys->end_date, p_block->i_pts );
315     }
316     p_block->i_pts = 0;
317
318     /* **** Now we can output these samples **** */
319     p_sys->i_samples = i_output / sizeof(int16_t) / p_sys->p_context->channels;
320     p_sys->p_samples = p_sys->p_output;
321
322     /* Silent unwanted samples */
323     if( p_sys->i_reject_count > 0 )
324     {
325         memset( p_sys->p_output, 0, i_output );
326         p_sys->i_reject_count--;
327     }
328
329     p_buffer = SplitBuffer( p_dec );
330     if( !p_buffer ) block_Release( p_block );
331     return p_buffer;
332 }
333
334 /*****************************************************************************
335  * EndAudioDec: audio decoder destruction
336  *****************************************************************************/
337 void E_(EndAudioDec)( decoder_t *p_dec )
338 {
339     decoder_sys_t *p_sys = p_dec->p_sys;
340
341     if( p_sys->p_output ) free( p_sys->p_output );
342 }