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