]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/audio.c
* ALL: final improvements to the decoders/packetizers api.
[vlc] / modules / codec / ffmpeg / audio.c
1 /*****************************************************************************
2  * audio.c: audio decoder using ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2003 VideoLAN
5  * $Id: audio.c,v 1.22 2003/11/16 21:07:30 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
34
35 #include "codecs.h"
36 #include "aout_internal.h"
37
38 /* ffmpeg header */
39 #ifdef HAVE_FFMPEG_AVCODEC_H
40 #   include <ffmpeg/avcodec.h>
41 #else
42 #   include <avcodec.h>
43 #endif
44
45 #include "ffmpeg.h"
46
47 static unsigned int pi_channels_maps[6] =
48 {
49     0,
50     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
51     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
52     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
53      | AOUT_CHAN_REARRIGHT,
54     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
55      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
56 };
57
58 /*****************************************************************************
59  * decoder_sys_t : decoder descriptor
60  *****************************************************************************/
61 struct decoder_sys_t
62 {
63     /* Common part between video and audio decoder */
64     int i_cat;
65     int i_codec_id;
66     char *psz_namecodec;
67     AVCodecContext      *p_context;
68     AVCodec             *p_codec;
69
70     /* Temporary buffer for libavcodec */
71     uint8_t *p_output;
72
73     /*
74      * Output properties
75      */
76     audio_sample_format_t aout_format;
77     audio_date_t          end_date;
78 };
79
80 /*****************************************************************************
81  * InitAudioDec: initialize audio decoder
82  *****************************************************************************
83  * The ffmpeg codec will be opened, some memory allocated.
84  *****************************************************************************/
85 int E_(InitAudioDec)( decoder_t *p_dec, AVCodecContext *p_context,
86                       AVCodec *p_codec, int i_codec_id, char *psz_namecodec )
87 {
88     decoder_sys_t *p_sys;
89
90     /* Allocate the memory needed to store the decoder's structure */
91     if( ( p_dec->p_sys = p_sys =
92           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
93     {
94         msg_Err( p_dec, "out of memory" );
95         return VLC_EGENERIC;
96     }
97
98     p_dec->p_sys->p_context = p_context;
99     p_dec->p_sys->p_codec = p_codec;
100     p_dec->p_sys->i_codec_id = i_codec_id;
101     p_dec->p_sys->psz_namecodec = psz_namecodec;
102
103     /* ***** Fill p_context with init values ***** */
104     p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
105     p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
106     p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
107     p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
108
109     if( ( p_sys->p_context->extradata_size = p_dec->fmt_in.i_extra ) > 0 )
110     {
111         p_sys->p_context->extradata =
112             malloc( p_dec->fmt_in.i_extra + FF_INPUT_BUFFER_PADDING_SIZE );
113         memcpy( p_sys->p_context->extradata,
114                 p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
115         memset( p_sys->p_context->extradata + p_dec->fmt_in.i_extra, 0,
116                 FF_INPUT_BUFFER_PADDING_SIZE );
117     }
118
119     /* ***** Open the codec ***** */
120     if (avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0)
121     {
122         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
123         return VLC_EGENERIC;
124     }
125     else
126     {
127         msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
128     }
129
130     p_sys->p_output = malloc( 3 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
131
132     aout_DateSet( &p_sys->end_date, 0 );
133
134     /* Set output properties */
135     p_dec->fmt_out.i_cat = AUDIO_ES;
136     p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
137
138     return VLC_SUCCESS;
139 }
140
141 /*****************************************************************************
142  * DecodeAudio: Called to decode one frame
143  *****************************************************************************/
144 aout_buffer_t *E_( DecodeAudio )( decoder_t *p_dec, block_t **pp_block )
145 {
146     decoder_sys_t *p_sys = p_dec->p_sys;
147     int i_used, i_output, i_samples;
148     uint8_t *p_samples;
149     aout_buffer_t *p_buffer;
150     block_t *p_block;
151
152     if( !pp_block || !*pp_block ) return NULL;
153
154     p_block = *pp_block;
155
156     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
157     {
158         /* We've just started the stream, wait for the first PTS. */
159         block_Release( p_block );
160         return NULL;
161     }
162
163     if( !p_block->i_buffer )
164     {
165         block_Release( p_block );
166         return NULL;
167     }
168
169     i_used = avcodec_decode_audio( p_sys->p_context,
170                                    (int16_t*)p_sys->p_output, &i_output,
171                                    p_block->p_buffer, p_block->i_buffer );
172
173     if( i_used < 0 )//|| i_output == 0 )
174     {
175         if( i_used < 0 )
176             msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
177                       p_block->i_buffer );
178
179         block_Release( p_block );
180         return NULL;
181     }
182
183     p_block->i_buffer -= i_used;
184     p_block->p_buffer += i_used;
185
186     if( p_sys->p_context->channels <= 0 || p_sys->p_context->channels > 6 )
187     {
188         msg_Warn( p_dec, "invalid channels count %d",
189                   p_sys->p_context->channels );
190         block_Release( p_block );
191         return NULL;
192     }
193
194     if( p_dec->fmt_out.audio.i_rate != p_sys->p_context->sample_rate )
195     {
196         aout_DateInit( &p_sys->end_date, p_sys->p_context->sample_rate );
197         aout_DateSet( &p_sys->end_date, p_block->i_pts );
198     }
199
200     /* **** Set audio output parameters **** */
201     p_dec->fmt_out.audio.i_rate     = p_sys->p_context->sample_rate;
202     p_dec->fmt_out.audio.i_channels = p_sys->p_context->channels;
203     p_dec->fmt_out.audio.i_original_channels =
204         p_dec->fmt_out.audio.i_physical_channels =
205             pi_channels_maps[p_sys->p_context->channels];
206
207     if( p_block->i_pts != 0 &&
208         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
209     {
210         aout_DateSet( &p_sys->end_date, p_block->i_pts );
211         p_block->i_pts = 0;
212     }
213
214     /* **** Now we can output these samples **** */
215     i_samples = i_output / 2 / p_sys->p_context->channels;
216     p_samples = p_sys->p_output;
217
218     p_buffer = p_dec->pf_aout_buffer_new( p_dec, i_samples );
219     if( !p_buffer )
220     {
221         msg_Err( p_dec, "cannot get aout buffer" );
222         block_Release( p_block );
223         return NULL;
224     }
225
226     p_buffer->start_date = aout_DateGet( &p_sys->end_date );
227     p_buffer->end_date = aout_DateIncrement( &p_sys->end_date, i_samples );
228
229     memcpy( p_buffer->p_buffer, p_samples, p_buffer->i_nb_bytes );
230
231     return p_buffer;
232 }
233
234 /*****************************************************************************
235  * EndAudioDec: audio decoder destruction
236  *****************************************************************************/
237 void E_(EndAudioDec)( decoder_t *p_dec )
238 {
239     decoder_sys_t *p_sys = p_dec->p_sys;
240
241     if( p_sys->p_output ) free( p_sys->p_output );
242 }