~bratsche/vlc/vlc-notify-add-actions-with-server-support

« back to all changes in this revision

Viewing changes to modules/codec/avcodec/audio.c

  • Committer: Bazaar Package Importer
  • Date: 2008-11-28 09:41:28 UTC
  • Revision ID: jamesw@ubuntu.com-20081128094128-qc1zhxqwlf2ov8cl
Tags: upstream-ubuntu-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

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