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