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