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