]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/audio.c
Simplify FLAC extradata (streaminfo) parsing
[vlc] / modules / codec / avcodec / audio.c
1 /*****************************************************************************
2  * audio.c: audio decoder using libavcodec library
3  *****************************************************************************
4  * Copyright (C) 1999-2003 VLC authors and 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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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 <assert.h>
33
34 #include <vlc_common.h>
35 #include <vlc_aout.h>
36 #include <vlc_codec.h>
37 #include <vlc_avcodec.h>
38
39 #include <libavcodec/avcodec.h>
40 #include <libavutil/mem.h>
41
42 #include <libavutil/audioconvert.h>
43
44 #include "avcodec.h"
45
46 /*****************************************************************************
47  * decoder_sys_t : decoder descriptor
48  *****************************************************************************/
49 struct decoder_sys_t
50 {
51     AVCODEC_COMMON_MEMBERS
52
53     /*
54      * Output properties
55      */
56     audio_sample_format_t aout_format;
57     date_t                end_date;
58
59     /* */
60     int     i_reject_count;
61
62     /* */
63     bool    b_extract;
64     int     pi_extraction[AOUT_CHAN_MAX];
65     int     i_previous_channels;
66     int64_t i_previous_layout;
67 };
68
69 #define BLOCK_FLAG_PRIVATE_REALLOCATED (1 << BLOCK_FLAG_PRIVATE_SHIFT)
70
71 static void SetupOutputFormat( decoder_t *p_dec, bool b_trust );
72 static int GetAudioBuf( struct AVCodecContext *, AVFrame * );
73
74 static void InitDecoderConfig( decoder_t *p_dec, AVCodecContext *p_context )
75 {
76     if( p_dec->fmt_in.i_extra > 0 )
77     {
78         const uint8_t * const p_src = p_dec->fmt_in.p_extra;
79
80         int i_offset = 0;
81         int i_size = p_dec->fmt_in.i_extra;
82
83         if( p_dec->fmt_in.i_codec == VLC_CODEC_ALAC )
84         {
85             static const uint8_t p_pattern[] = { 0, 0, 0, 36, 'a', 'l', 'a', 'c' };
86             /* Find alac atom XXX it is a bit ugly */
87             for( i_offset = 0; i_offset < p_dec->fmt_in.i_extra - sizeof(p_pattern); i_offset++ )
88             {
89                 if( !memcmp( &p_src[i_offset], p_pattern, sizeof(p_pattern) ) )
90                     break;
91             }
92             i_size = __MIN( p_dec->fmt_in.i_extra - i_offset, 36 );
93             if( i_size < 36 )
94                 i_size = 0;
95         }
96
97         if( i_size > 0 )
98         {
99             p_context->extradata =
100                 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
101             if( p_context->extradata )
102             {
103                 uint8_t *p_dst = p_context->extradata;
104
105                 p_context->extradata_size = i_size;
106
107                 memcpy( &p_dst[0],            &p_src[i_offset], i_size );
108                 memset( &p_dst[i_size], 0, FF_INPUT_BUFFER_PADDING_SIZE );
109             }
110         }
111     }
112     else
113     {
114         p_context->extradata_size = 0;
115         p_context->extradata = NULL;
116     }
117 }
118
119 /*****************************************************************************
120  * InitAudioDec: initialize audio decoder
121  *****************************************************************************
122  * The avcodec codec will be opened, some memory allocated.
123  *****************************************************************************/
124 int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
125                       AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
126 {
127     decoder_sys_t *p_sys;
128
129     /* Allocate the memory needed to store the decoder's structure */
130     if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
131     {
132         return VLC_ENOMEM;
133     }
134
135     p_codec->type = AVMEDIA_TYPE_AUDIO;
136     p_context->codec_type = AVMEDIA_TYPE_AUDIO;
137     p_context->codec_id = i_codec_id;
138     p_context->get_buffer = GetAudioBuf;
139     p_sys->p_context = p_context;
140     p_sys->p_codec = p_codec;
141     p_sys->i_codec_id = i_codec_id;
142     p_sys->psz_namecodec = psz_namecodec;
143     p_sys->b_delayed_open = true;
144
145     // Initialize decoder extradata
146     InitDecoderConfig( p_dec, p_context);
147
148     /* ***** Open the codec ***** */
149     if( ffmpeg_OpenCodec( p_dec ) < 0 )
150     {
151         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
152         free( p_sys->p_context->extradata );
153         free( p_sys );
154         return VLC_EGENERIC;
155     }
156
157     p_sys->i_reject_count = 0;
158     p_sys->b_extract = false;
159     p_sys->i_previous_channels = 0;
160     p_sys->i_previous_layout = 0;
161
162     /* */
163     p_dec->fmt_out.i_cat = AUDIO_ES;
164     /* Try to set as much information as possible but do not trust it */
165     SetupOutputFormat( p_dec, false );
166
167     date_Set( &p_sys->end_date, 0 );
168     if( p_dec->fmt_out.audio.i_rate )
169         date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
170     else if( p_dec->fmt_in.audio.i_rate )
171         date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
172
173     return VLC_SUCCESS;
174 }
175
176 /**
177  * Allocates decoded audio buffer for libavcodec to use.
178  */
179 static int GetAudioBuf( AVCodecContext *ctx, AVFrame *buf )
180 {
181     block_t *block;
182     bool planar = av_sample_fmt_is_planar( ctx->sample_fmt );
183     unsigned channels = planar ? 1 : ctx->channels;
184     unsigned planes = planar ? ctx->channels : 1;
185
186     int bytes = av_samples_get_buffer_size( &buf->linesize[0], channels,
187                                             buf->nb_samples, ctx->sample_fmt,
188                                             16 );
189     assert( bytes >= 0 );
190     block = block_Alloc( bytes * planes );
191     if( unlikely(block == NULL) )
192         return AVERROR(ENOMEM);
193
194     block->i_nb_samples = buf->nb_samples;
195     buf->opaque = block;
196
197     if( planes > AV_NUM_DATA_POINTERS )
198     {
199         uint8_t **ext = malloc( sizeof( *ext ) * planes );
200         if( unlikely(ext == NULL) )
201         {
202             block_Release( block );
203             return AVERROR(ENOMEM);
204         }
205         buf->extended_data = ext;
206     }
207     else
208         buf->extended_data = buf->data;
209
210     uint8_t *buffer = block->p_buffer;
211     for( unsigned i = 0; i < planes; i++ )
212     {
213         buf->linesize[i] = buf->linesize[0];
214         buf->extended_data[i] = buffer;
215         buffer += bytes;
216     }
217
218     return 0;
219 }
220
221 /*****************************************************************************
222  * DecodeAudio: Called to decode one frame
223  *****************************************************************************/
224 block_t * DecodeAudio ( decoder_t *p_dec, block_t **pp_block )
225 {
226     decoder_sys_t *p_sys = p_dec->p_sys;
227     AVCodecContext *ctx = p_sys->p_context;
228
229     if( !pp_block || !*pp_block )
230         return NULL;
231
232     block_t *p_block = *pp_block;
233
234     if( !ctx->extradata_size && p_dec->fmt_in.i_extra && p_sys->b_delayed_open)
235     {
236         InitDecoderConfig( p_dec, ctx );
237         if( ffmpeg_OpenCodec( p_dec ) )
238             msg_Err( p_dec, "Cannot open decoder %s", p_sys->psz_namecodec );
239     }
240
241     if( p_sys->b_delayed_open )
242         goto end;
243
244     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
245     {
246         avcodec_flush_buffers( ctx );
247         date_Set( &p_sys->end_date, 0 );
248
249         if( p_sys->i_codec_id == AV_CODEC_ID_MP2 || p_sys->i_codec_id == AV_CODEC_ID_MP3 )
250             p_sys->i_reject_count = 3;
251
252         goto end;
253     }
254
255     /* We've just started the stream, wait for the first PTS. */
256     if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID )
257         goto end;
258
259     if( p_block->i_buffer <= 0 )
260         goto end;
261
262     if( (p_block->i_flags & BLOCK_FLAG_PRIVATE_REALLOCATED) == 0 )
263     {
264         p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
265         if( !p_block )
266             return NULL;
267         p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
268         memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
269
270         p_block->i_flags |= BLOCK_FLAG_PRIVATE_REALLOCATED;
271     }
272
273     AVFrame frame;
274     memset( &frame, 0, sizeof( frame ) );
275
276     for( int got_frame = 0; !got_frame; )
277     {
278         if( p_block->i_buffer == 0 )
279             goto end;
280
281         AVPacket pkt;
282         av_init_packet( &pkt );
283         pkt.data = p_block->p_buffer;
284         pkt.size = p_block->i_buffer;
285
286         int used = avcodec_decode_audio4( ctx, &frame, &got_frame, &pkt );
287         if( used < 0 )
288         {
289             msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
290                       p_block->i_buffer );
291             goto end;
292         }
293
294         assert( p_block->i_buffer >= (unsigned)used );
295         p_block->p_buffer += used;
296         p_block->i_buffer -= used;
297     }
298
299     if( ctx->channels <= 0 || ctx->channels > 8 || ctx->sample_rate <= 0 )
300     {
301         msg_Warn( p_dec, "invalid audio properties channels count %d, sample rate %d",
302                   ctx->channels, ctx->sample_rate );
303         goto end;
304     }
305
306     if( p_dec->fmt_out.audio.i_rate != (unsigned int)ctx->sample_rate )
307         date_Init( &p_sys->end_date, ctx->sample_rate, 1 );
308
309     if( p_block->i_pts > VLC_TS_INVALID &&
310         p_block->i_pts > date_Get( &p_sys->end_date ) )
311     {
312         date_Set( &p_sys->end_date, p_block->i_pts );
313     }
314
315     if( p_block->i_buffer == 0 )
316     {   /* Done with this buffer */
317         block_Release( p_block );
318         *pp_block = NULL;
319     }
320
321     /* NOTE WELL: Beyond this point, p_block now refers to the DECODED block */
322     p_block = frame.opaque;
323     SetupOutputFormat( p_dec, true );
324
325     /* Silent unwanted samples */
326     if( p_sys->i_reject_count > 0 )
327     {
328         memset( p_block->p_buffer, 0, p_block->i_buffer );
329         p_sys->i_reject_count--;
330     }
331
332     block_t *p_buffer = decoder_NewAudioBuffer( p_dec, p_block->i_nb_samples );
333     if (!p_buffer)
334         return NULL;
335     assert( p_block->i_nb_samples >= (unsigned)frame.nb_samples );
336     assert( p_block->i_nb_samples == p_buffer->i_nb_samples );
337     p_block->i_buffer = p_buffer->i_buffer; /* drop buffer padding */
338
339     /* Interleave audio if required */
340     if( av_sample_fmt_is_planar( ctx->sample_fmt ) )
341     {
342         aout_Interleave( p_buffer->p_buffer, p_block->p_buffer,
343                          p_block->i_nb_samples, ctx->channels,
344                          p_dec->fmt_out.audio.i_format );
345         if( ctx->channels > AV_NUM_DATA_POINTERS )
346             free( frame.extended_data );
347         block_Release( p_block );
348         p_block = p_buffer;
349     }
350     else /* FIXME: improve decoder_NewAudioBuffer(), avoid useless buffer... */
351         block_Release( p_buffer );
352
353     if (p_sys->b_extract)
354     {   /* TODO: do not drop channels... at least not here */
355         p_buffer = block_Alloc( p_dec->fmt_out.audio.i_bytes_per_frame
356                                 * frame.nb_samples );
357         if( unlikely(p_buffer == NULL) )
358         {
359             block_Release( p_block );
360             return NULL;
361         }
362         aout_ChannelExtract( p_buffer->p_buffer,
363                              p_dec->fmt_out.audio.i_channels,
364                              p_block->p_buffer, ctx->channels,
365                              frame.nb_samples, p_sys->pi_extraction,
366                              p_dec->fmt_out.audio.i_bitspersample );
367         block_Release( p_block );
368         p_block = p_buffer;
369     }
370
371     p_block->i_nb_samples = frame.nb_samples;
372     p_block->i_buffer = frame.nb_samples
373                         * p_dec->fmt_out.audio.i_bytes_per_frame;
374     p_block->i_pts = date_Get( &p_sys->end_date );
375     p_block->i_length = date_Increment( &p_sys->end_date, frame.nb_samples )
376                         - p_block->i_pts;
377     return p_block;
378
379 end:
380     block_Release(p_block);
381     *pp_block = NULL;
382     return NULL;
383 }
384
385 /*****************************************************************************
386  *
387  *****************************************************************************/
388
389 vlc_fourcc_t GetVlcAudioFormat( int fmt )
390 {
391     static const vlc_fourcc_t fcc[] = {
392         [AV_SAMPLE_FMT_U8]    = VLC_CODEC_U8,
393         [AV_SAMPLE_FMT_S16]   = VLC_CODEC_S16N,
394         [AV_SAMPLE_FMT_S32]   = VLC_CODEC_S32N,
395         [AV_SAMPLE_FMT_FLT]   = VLC_CODEC_FL32,
396         [AV_SAMPLE_FMT_DBL]   = VLC_CODEC_FL64,
397         [AV_SAMPLE_FMT_U8P]   = VLC_CODEC_U8,
398         [AV_SAMPLE_FMT_S16P]  = VLC_CODEC_S16N,
399         [AV_SAMPLE_FMT_S32P]  = VLC_CODEC_S32N,
400         [AV_SAMPLE_FMT_FLTP]  = VLC_CODEC_FL32,
401         [AV_SAMPLE_FMT_DBLP]  = VLC_CODEC_FL64,
402     };
403     if( (sizeof(fcc) / sizeof(fcc[0])) > (unsigned)fmt )
404         return fcc[fmt];
405     return VLC_CODEC_S16N;
406 }
407
408 static const uint64_t pi_channels_map[][2] =
409 {
410     { AV_CH_FRONT_LEFT,        AOUT_CHAN_LEFT },
411     { AV_CH_FRONT_RIGHT,       AOUT_CHAN_RIGHT },
412     { AV_CH_FRONT_CENTER,      AOUT_CHAN_CENTER },
413     { AV_CH_LOW_FREQUENCY,     AOUT_CHAN_LFE },
414     { AV_CH_BACK_LEFT,         AOUT_CHAN_REARLEFT },
415     { AV_CH_BACK_RIGHT,        AOUT_CHAN_REARRIGHT },
416     { AV_CH_FRONT_LEFT_OF_CENTER, 0 },
417     { AV_CH_FRONT_RIGHT_OF_CENTER, 0 },
418     { AV_CH_BACK_CENTER,       AOUT_CHAN_REARCENTER },
419     { AV_CH_SIDE_LEFT,         AOUT_CHAN_MIDDLELEFT },
420     { AV_CH_SIDE_RIGHT,        AOUT_CHAN_MIDDLERIGHT },
421     { AV_CH_TOP_CENTER,        0 },
422     { AV_CH_TOP_FRONT_LEFT,    0 },
423     { AV_CH_TOP_FRONT_CENTER,  0 },
424     { AV_CH_TOP_FRONT_RIGHT,   0 },
425     { AV_CH_TOP_BACK_LEFT,     0 },
426     { AV_CH_TOP_BACK_CENTER,   0 },
427     { AV_CH_TOP_BACK_RIGHT,    0 },
428     { AV_CH_STEREO_LEFT,       0 },
429     { AV_CH_STEREO_RIGHT,      0 },
430 };
431
432 static void SetupOutputFormat( decoder_t *p_dec, bool b_trust )
433 {
434     decoder_sys_t *p_sys = p_dec->p_sys;
435
436     p_dec->fmt_out.i_codec = GetVlcAudioFormat( p_sys->p_context->sample_fmt );
437     p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
438     p_dec->fmt_out.audio.i_rate = p_sys->p_context->sample_rate;
439
440     /* */
441     if( p_sys->i_previous_channels == p_sys->p_context->channels &&
442         p_sys->i_previous_layout == p_sys->p_context->channel_layout )
443         return;
444     if( b_trust )
445     {
446         p_sys->i_previous_channels = p_sys->p_context->channels;
447         p_sys->i_previous_layout = p_sys->p_context->channel_layout;
448     }
449
450     /* Specified order
451      * FIXME should we use fmt_in.audio.i_physical_channels or not ?
452      */
453     const unsigned i_order_max = 8 * sizeof(p_sys->p_context->channel_layout);
454     uint32_t pi_order_src[i_order_max];
455     int i_channels_src = 0;
456
457     if( p_sys->p_context->channel_layout )
458     {
459         for( unsigned i = 0; i < sizeof(pi_channels_map)/sizeof(*pi_channels_map); i++ )
460         {
461             if( p_sys->p_context->channel_layout & pi_channels_map[i][0] )
462                 pi_order_src[i_channels_src++] = pi_channels_map[i][1];
463         }
464     }
465     else
466     {
467         /* Create default order  */
468         if( b_trust )
469             msg_Warn( p_dec, "Physical channel configuration not set : guessing" );
470         for( unsigned int i = 0; i < __MIN( i_order_max, (unsigned)p_sys->p_context->channels ); i++ )
471         {
472             if( i < sizeof(pi_channels_map)/sizeof(*pi_channels_map) )
473                 pi_order_src[i_channels_src++] = pi_channels_map[i][1];
474         }
475     }
476     if( i_channels_src != p_sys->p_context->channels && b_trust )
477         msg_Err( p_dec, "Channel layout not understood" );
478
479     uint32_t i_layout_dst;
480     int      i_channels_dst;
481     p_sys->b_extract = aout_CheckChannelExtraction( p_sys->pi_extraction,
482                                                     &i_layout_dst, &i_channels_dst,
483                                                     NULL, pi_order_src, i_channels_src );
484     if( i_channels_dst != i_channels_src && b_trust )
485         msg_Warn( p_dec, "%d channels are dropped", i_channels_src - i_channels_dst );
486
487     p_dec->fmt_out.audio.i_physical_channels =
488     p_dec->fmt_out.audio.i_original_channels = i_layout_dst;
489     aout_FormatPrepare( &p_dec->fmt_out.audio );
490 }
491