]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/audio.c
vout: remove unimplemented GET_OPENGL controls
[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     uint64_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 block_t *DecodeAudio( decoder_t *, block_t ** );
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 < i_size - (int)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                 av_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 static int OpenAudioCodec( decoder_t *p_dec )
120 {
121     decoder_sys_t *p_sys = p_dec->p_sys;
122
123     if( p_sys->p_context->extradata_size <= 0 )
124     {
125         if( p_sys->p_codec->id == AV_CODEC_ID_VORBIS ||
126             ( p_sys->p_codec->id == AV_CODEC_ID_AAC &&
127               !p_dec->fmt_in.b_packetized ) )
128         {
129             msg_Warn( p_dec, "waiting for extra data for codec %s",
130                       p_sys->p_codec->name );
131             return 1;
132         }
133     }
134
135     p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
136     p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
137     p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
138     p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
139     p_sys->p_context->bits_per_coded_sample =
140                                            p_dec->fmt_in.audio.i_bitspersample;
141
142     if( p_sys->p_codec->id == AV_CODEC_ID_ADPCM_G726 &&
143         p_sys->p_context->bit_rate > 0 &&
144         p_sys->p_context->sample_rate >  0)
145         p_sys->p_context->bits_per_coded_sample = p_sys->p_context->bit_rate
146                                                / p_sys->p_context->sample_rate;
147
148     return ffmpeg_OpenCodec( p_dec );
149 }
150
151 /**
152  * Allocates decoded audio buffer for libavcodec to use.
153  */
154 #if (LIBAVCODEC_VERSION_MAJOR >= 55)
155 typedef struct
156 {
157     block_t self;
158     AVFrame *frame;
159 } vlc_av_frame_t;
160
161 static void vlc_av_frame_Release(block_t *block)
162 {
163     vlc_av_frame_t *b = (void *)block;
164
165     av_frame_free(&b->frame);
166     free(b);
167 }
168
169 static block_t *vlc_av_frame_Wrap(AVFrame *frame)
170 {
171     for (unsigned i = 1; i < AV_NUM_DATA_POINTERS; i++)
172         assert(frame->linesize[i] == 0); /* only packed frame supported */
173
174     if (av_frame_make_writable(frame)) /* TODO: read-only block_t */
175         return NULL;
176
177     vlc_av_frame_t *b = malloc(sizeof (*b));
178     if (unlikely(b == NULL))
179         return NULL;
180
181     block_t *block = &b->self;
182
183     block_Init(block, frame->extended_data[0], frame->linesize[0]);
184     block->i_nb_samples = frame->nb_samples;
185     block->pf_release = vlc_av_frame_Release;
186     b->frame = frame;
187     return block;
188 }
189 #else
190 static int GetAudioBuf( AVCodecContext *ctx, AVFrame *buf )
191 {
192     block_t *block;
193     bool planar = av_sample_fmt_is_planar( ctx->sample_fmt );
194     unsigned channels = planar ? 1 : ctx->channels;
195     unsigned planes = planar ? ctx->channels : 1;
196
197     int bytes = av_samples_get_buffer_size( &buf->linesize[0], channels,
198                                             buf->nb_samples, ctx->sample_fmt,
199                                             16 );
200     assert( bytes >= 0 );
201     block = block_Alloc( bytes * planes );
202     if( unlikely(block == NULL) )
203         return AVERROR(ENOMEM);
204
205     block->i_nb_samples = buf->nb_samples;
206     buf->opaque = block;
207
208     if( planes > AV_NUM_DATA_POINTERS )
209     {
210         uint8_t **ext = malloc( sizeof( *ext ) * planes );
211         if( unlikely(ext == NULL) )
212         {
213             block_Release( block );
214             return AVERROR(ENOMEM);
215         }
216         buf->extended_data = ext;
217     }
218     else
219         buf->extended_data = buf->data;
220
221     uint8_t *buffer = block->p_buffer;
222     for( unsigned i = 0; i < planes; i++ )
223     {
224         buf->linesize[i] = buf->linesize[0];
225         buf->extended_data[i] = buffer;
226         buffer += bytes;
227     }
228
229     return 0;
230 }
231 #endif
232
233 /*****************************************************************************
234  * InitAudioDec: initialize audio decoder
235  *****************************************************************************
236  * The avcodec codec will be opened, some memory allocated.
237  *****************************************************************************/
238 int InitAudioDec( decoder_t *p_dec, AVCodecContext *p_context,
239                   const AVCodec *p_codec )
240 {
241     decoder_sys_t *p_sys;
242
243     /* Allocate the memory needed to store the decoder's structure */
244     if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
245     {
246         return VLC_ENOMEM;
247     }
248
249 #if (LIBAVCODEC_VERSION_MAJOR >= 55)
250     p_context->refcounted_frames = true;
251 #else
252     p_context->get_buffer = GetAudioBuf;
253 #endif
254     p_sys->p_context = p_context;
255     p_sys->p_codec = p_codec;
256     p_sys->b_delayed_open = true;
257
258     // Initialize decoder extradata
259     InitDecoderConfig( p_dec, p_context);
260
261     /* ***** Open the codec ***** */
262     if( OpenAudioCodec( p_dec ) < 0 )
263     {
264         av_free( p_context->extradata );
265         free( p_sys );
266         return VLC_EGENERIC;
267     }
268
269     p_sys->i_reject_count = 0;
270     p_sys->b_extract = false;
271     p_sys->i_previous_channels = 0;
272     p_sys->i_previous_layout = 0;
273
274     /* */
275     p_dec->fmt_out.i_cat = AUDIO_ES;
276     /* Try to set as much information as possible but do not trust it */
277     SetupOutputFormat( p_dec, false );
278
279     date_Set( &p_sys->end_date, 0 );
280     if( p_dec->fmt_out.audio.i_rate )
281         date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
282     else if( p_dec->fmt_in.audio.i_rate )
283         date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
284
285     p_dec->pf_decode_audio = DecodeAudio;
286     return VLC_SUCCESS;
287 }
288
289 /*****************************************************************************
290  * DecodeAudio: Called to decode one frame
291  *****************************************************************************/
292 static block_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
293 {
294     decoder_sys_t *p_sys = p_dec->p_sys;
295     AVCodecContext *ctx = p_sys->p_context;
296
297     if( !pp_block || !*pp_block )
298         return NULL;
299
300     block_t *p_block = *pp_block;
301
302     if( !ctx->extradata_size && p_dec->fmt_in.i_extra && p_sys->b_delayed_open)
303     {
304         InitDecoderConfig( p_dec, ctx );
305         OpenAudioCodec( p_dec );
306     }
307
308     if( p_sys->b_delayed_open )
309         goto end;
310
311     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
312     {
313         avcodec_flush_buffers( ctx );
314         date_Set( &p_sys->end_date, 0 );
315
316         if( ctx->codec_id == AV_CODEC_ID_MP2 ||
317             ctx->codec_id == AV_CODEC_ID_MP3 )
318             p_sys->i_reject_count = 3;
319
320         goto end;
321     }
322
323     /* We've just started the stream, wait for the first PTS. */
324     if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID )
325         goto end;
326
327     if( p_block->i_buffer <= 0 )
328         goto end;
329
330     if( (p_block->i_flags & BLOCK_FLAG_PRIVATE_REALLOCATED) == 0 )
331     {
332         p_block = block_Realloc( p_block, 0, p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
333         if( !p_block )
334             return NULL;
335         *pp_block = p_block;
336         p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
337         memset( &p_block->p_buffer[p_block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE );
338
339         p_block->i_flags |= BLOCK_FLAG_PRIVATE_REALLOCATED;
340     }
341
342 #if (LIBAVCODEC_VERSION_MAJOR >= 55)
343     AVFrame *frame = av_frame_alloc();
344     if (unlikely(frame == NULL))
345         goto end;
346 #else
347     AVFrame *frame = &(AVFrame) { };
348 #endif
349
350     for( int got_frame = 0; !got_frame; )
351     {
352         if( p_block->i_buffer == 0 )
353             goto end;
354
355         AVPacket pkt;
356         av_init_packet( &pkt );
357         pkt.data = p_block->p_buffer;
358         pkt.size = p_block->i_buffer;
359
360         int used = avcodec_decode_audio4( ctx, frame, &got_frame, &pkt );
361         if( used < 0 )
362         {
363             msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
364                       p_block->i_buffer );
365             goto end;
366         }
367
368         assert( p_block->i_buffer >= (unsigned)used );
369         p_block->p_buffer += used;
370         p_block->i_buffer -= used;
371     }
372
373     if( ctx->channels <= 0 || ctx->channels > 8 || ctx->sample_rate <= 0 )
374     {
375         msg_Warn( p_dec, "invalid audio properties channels count %d, sample rate %d",
376                   ctx->channels, ctx->sample_rate );
377         goto end;
378     }
379
380     if( p_dec->fmt_out.audio.i_rate != (unsigned int)ctx->sample_rate )
381         date_Init( &p_sys->end_date, ctx->sample_rate, 1 );
382
383     if( p_block->i_pts > VLC_TS_INVALID &&
384         p_block->i_pts > date_Get( &p_sys->end_date ) )
385     {
386         date_Set( &p_sys->end_date, p_block->i_pts );
387     }
388
389     if( p_block->i_buffer == 0 )
390     {   /* Done with this buffer */
391         block_Release( p_block );
392         p_block = NULL;
393         *pp_block = NULL;
394     }
395
396 #if (LIBAVCODEC_VERSION_MAJOR < 55)
397     /* NOTE WELL: Beyond this point, p_block refers to the DECODED block! */
398     p_block = frame->opaque;
399 #endif
400     SetupOutputFormat( p_dec, true );
401     if( decoder_UpdateAudioFormat( p_dec ) )
402         goto drop;
403
404     /* Interleave audio if required */
405     if( av_sample_fmt_is_planar( ctx->sample_fmt ) )
406 #if (LIBAVCODEC_VERSION_MAJOR >= 55)
407     {
408         p_block = block_Alloc(frame->linesize[0] * ctx->channels);
409         if (unlikely(p_block == NULL))
410             goto drop;
411
412         const void *planes[ctx->channels];
413         for (int i = 0; i < ctx->channels; i++)
414             planes[i] = frame->extended_data[i];
415
416         aout_Interleave(p_block->p_buffer, planes, frame->nb_samples,
417                         ctx->channels, p_dec->fmt_out.audio.i_format);
418         p_block->i_nb_samples = frame->nb_samples;
419         av_frame_free(&frame);
420     }
421     else
422     {
423         p_block = vlc_av_frame_Wrap(frame);
424         if (unlikely(p_block == NULL))
425             goto drop;
426     }
427 #else
428     {
429         block_t *p_buffer = block_Alloc( p_block->i_buffer );
430         if( unlikely(p_buffer == NULL) )
431             goto drop;
432
433         const void *planes[ctx->channels];
434         for( int i = 0; i < ctx->channels; i++)
435             planes[i] = frame->extended_data[i];
436
437         aout_Interleave( p_buffer->p_buffer, planes, frame->nb_samples,
438                          ctx->channels, p_dec->fmt_out.audio.i_format );
439         if( ctx->channels > AV_NUM_DATA_POINTERS )
440             free( frame->extended_data );
441         block_Release( p_block );
442         p_block = p_buffer;
443     }
444     p_block->i_nb_samples = frame->nb_samples;
445 #endif
446
447     if (p_sys->b_extract)
448     {   /* TODO: do not drop channels... at least not here */
449         block_t *p_buffer = block_Alloc( p_dec->fmt_out.audio.i_bytes_per_frame
450                                          * p_block->i_nb_samples );
451         if( unlikely(p_buffer == NULL) )
452             goto drop;
453         aout_ChannelExtract( p_buffer->p_buffer,
454                              p_dec->fmt_out.audio.i_channels,
455                              p_block->p_buffer, ctx->channels,
456                              p_block->i_nb_samples, p_sys->pi_extraction,
457                              p_dec->fmt_out.audio.i_bitspersample );
458         p_buffer->i_nb_samples = p_block->i_nb_samples;
459         block_Release( p_block );
460         p_block = p_buffer;
461     }
462
463     /* Silent unwanted samples */
464     if( p_sys->i_reject_count > 0 )
465     {
466         memset( p_block->p_buffer, 0, p_block->i_buffer );
467         p_sys->i_reject_count--;
468     }
469
470     p_block->i_buffer = p_block->i_nb_samples
471                         * p_dec->fmt_out.audio.i_bytes_per_frame;
472     p_block->i_pts = date_Get( &p_sys->end_date );
473     p_block->i_length = date_Increment( &p_sys->end_date,
474                                       p_block->i_nb_samples ) - p_block->i_pts;
475     return p_block;
476
477 end:
478     *pp_block = NULL;
479 drop:
480     if( p_block != NULL )
481         block_Release(p_block);
482     return NULL;
483 }
484
485 /*****************************************************************************
486  *
487  *****************************************************************************/
488
489 vlc_fourcc_t GetVlcAudioFormat( int fmt )
490 {
491     static const vlc_fourcc_t fcc[] = {
492         [AV_SAMPLE_FMT_U8]    = VLC_CODEC_U8,
493         [AV_SAMPLE_FMT_S16]   = VLC_CODEC_S16N,
494         [AV_SAMPLE_FMT_S32]   = VLC_CODEC_S32N,
495         [AV_SAMPLE_FMT_FLT]   = VLC_CODEC_FL32,
496         [AV_SAMPLE_FMT_DBL]   = VLC_CODEC_FL64,
497         [AV_SAMPLE_FMT_U8P]   = VLC_CODEC_U8,
498         [AV_SAMPLE_FMT_S16P]  = VLC_CODEC_S16N,
499         [AV_SAMPLE_FMT_S32P]  = VLC_CODEC_S32N,
500         [AV_SAMPLE_FMT_FLTP]  = VLC_CODEC_FL32,
501         [AV_SAMPLE_FMT_DBLP]  = VLC_CODEC_FL64,
502     };
503     if( (sizeof(fcc) / sizeof(fcc[0])) > (unsigned)fmt )
504         return fcc[fmt];
505     return VLC_CODEC_S16N;
506 }
507
508 static const uint64_t pi_channels_map[][2] =
509 {
510     { AV_CH_FRONT_LEFT,        AOUT_CHAN_LEFT },
511     { AV_CH_FRONT_RIGHT,       AOUT_CHAN_RIGHT },
512     { AV_CH_FRONT_CENTER,      AOUT_CHAN_CENTER },
513     { AV_CH_LOW_FREQUENCY,     AOUT_CHAN_LFE },
514     { AV_CH_BACK_LEFT,         AOUT_CHAN_REARLEFT },
515     { AV_CH_BACK_RIGHT,        AOUT_CHAN_REARRIGHT },
516     { AV_CH_FRONT_LEFT_OF_CENTER, 0 },
517     { AV_CH_FRONT_RIGHT_OF_CENTER, 0 },
518     { AV_CH_BACK_CENTER,       AOUT_CHAN_REARCENTER },
519     { AV_CH_SIDE_LEFT,         AOUT_CHAN_MIDDLELEFT },
520     { AV_CH_SIDE_RIGHT,        AOUT_CHAN_MIDDLERIGHT },
521     { AV_CH_TOP_CENTER,        0 },
522     { AV_CH_TOP_FRONT_LEFT,    0 },
523     { AV_CH_TOP_FRONT_CENTER,  0 },
524     { AV_CH_TOP_FRONT_RIGHT,   0 },
525     { AV_CH_TOP_BACK_LEFT,     0 },
526     { AV_CH_TOP_BACK_CENTER,   0 },
527     { AV_CH_TOP_BACK_RIGHT,    0 },
528     { AV_CH_STEREO_LEFT,       0 },
529     { AV_CH_STEREO_RIGHT,      0 },
530 };
531
532 static void SetupOutputFormat( decoder_t *p_dec, bool b_trust )
533 {
534     decoder_sys_t *p_sys = p_dec->p_sys;
535
536     p_dec->fmt_out.i_codec = GetVlcAudioFormat( p_sys->p_context->sample_fmt );
537     p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
538     p_dec->fmt_out.audio.i_rate = p_sys->p_context->sample_rate;
539
540     /* */
541     if( p_sys->i_previous_channels == p_sys->p_context->channels &&
542         p_sys->i_previous_layout == p_sys->p_context->channel_layout )
543         return;
544     if( b_trust )
545     {
546         p_sys->i_previous_channels = p_sys->p_context->channels;
547         p_sys->i_previous_layout = p_sys->p_context->channel_layout;
548     }
549
550     /* Specified order
551      * FIXME should we use fmt_in.audio.i_physical_channels or not ?
552      */
553     const unsigned i_order_max = 8 * sizeof(p_sys->p_context->channel_layout);
554     uint32_t pi_order_src[i_order_max];
555     int i_channels_src = 0;
556
557     if( p_sys->p_context->channel_layout )
558     {
559         for( unsigned i = 0; i < sizeof(pi_channels_map)/sizeof(*pi_channels_map); i++ )
560         {
561             if( p_sys->p_context->channel_layout & pi_channels_map[i][0] )
562                 pi_order_src[i_channels_src++] = pi_channels_map[i][1];
563         }
564     }
565     else
566     {
567         /* Create default order  */
568         if( b_trust )
569             msg_Warn( p_dec, "Physical channel configuration not set : guessing" );
570         for( unsigned int i = 0; i < __MIN( i_order_max, (unsigned)p_sys->p_context->channels ); i++ )
571         {
572             if( i < sizeof(pi_channels_map)/sizeof(*pi_channels_map) )
573                 pi_order_src[i_channels_src++] = pi_channels_map[i][1];
574         }
575     }
576     if( i_channels_src != p_sys->p_context->channels && b_trust )
577         msg_Err( p_dec, "Channel layout not understood" );
578
579     uint32_t i_layout_dst;
580     int      i_channels_dst;
581     p_sys->b_extract = aout_CheckChannelExtraction( p_sys->pi_extraction,
582                                                     &i_layout_dst, &i_channels_dst,
583                                                     NULL, pi_order_src, i_channels_src );
584     if( i_channels_dst != i_channels_src && b_trust )
585         msg_Warn( p_dec, "%d channels are dropped", i_channels_src - i_channels_dst );
586
587     p_dec->fmt_out.audio.i_physical_channels =
588     p_dec->fmt_out.audio.i_original_channels = i_layout_dst;
589     aout_FormatPrepare( &p_dec->fmt_out.audio );
590 }
591