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