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