]> git.sesse.net Git - vlc/blob - modules/codec/faad.c
* faad: drop packets with b_discontinuity set.
[vlc] / modules / codec / faad.c
1 /*****************************************************************************
2  * decoder.c: AAC decoder using libfaad2
3  *****************************************************************************
4  * Copyright (C) 2001, 2003 VideoLAN
5  * $Id: faad.c,v 1.6 2003/11/27 20:51:31 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25 #include <vlc/aout.h>
26 #include <vlc/decoder.h>
27
28 #include <faad.h>
29
30 /*****************************************************************************
31  * Module descriptor
32  *****************************************************************************/
33 static int  Open( vlc_object_t * );
34 static void Close( vlc_object_t * );
35
36 vlc_module_begin();
37     set_description( _("AAC audio decoder (using libfaad2)") );
38     set_capability( "decoder", 60 );
39     set_callbacks( Open, Close );
40 vlc_module_end();
41
42
43 /****************************************************************************
44  * Local prototypes
45  ****************************************************************************/
46 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
47
48 struct decoder_sys_t
49 {
50     /* faad handler */
51     faacDecHandle *hfaad;
52
53     /* samples */
54     audio_date_t date;
55
56     /* temporary buffer */
57     uint8_t *p_buffer;
58     int     i_buffer;
59     int     i_buffer_size;
60 };
61
62 static unsigned int pi_channels_maps[7] =
63 {
64     0,
65     AOUT_CHAN_CENTER,
66     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
67     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
68     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT |
69         AOUT_CHAN_REARRIGHT,
70     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
71         AOUT_CHAN_REARRIGHT,
72      /* FIXME */
73     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
74         AOUT_CHAN_REARRIGHT | AOUT_CHAN_REARCENTER
75 };
76
77 /*****************************************************************************
78  * OpenDecoder: probe the decoder and return score
79  *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
81 {
82     decoder_t *p_dec = (decoder_t*)p_this;
83     decoder_sys_t *p_sys = p_dec->p_sys;
84     faacDecConfiguration *cfg;
85
86     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','4','a') )
87     {
88         return VLC_EGENERIC;
89     }
90
91     /* Allocate the memory needed to store the decoder's structure */
92     if( ( p_dec->p_sys = p_sys =
93           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
94     {
95         msg_Err( p_dec, "out of memory" );
96         return VLC_EGENERIC;
97     }
98
99     /* Open a faad context */
100     if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
101     {
102         msg_Err( p_dec, "Cannot initialize faad" );
103         return VLC_EGENERIC;
104     }
105
106     /* Misc init */
107     aout_DateSet( &p_sys->date, 0 );
108     p_dec->fmt_out.i_cat = AUDIO_ES;
109     p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
110     p_dec->pf_decode_audio = DecodeBlock;
111
112     if( p_dec->fmt_in.i_extra > 0 )
113     {
114         /* We have a decoder config so init the handle */
115         unsigned long i_rate;
116         unsigned char i_channels;
117
118         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
119                           p_dec->fmt_in.i_extra,
120                           &i_rate, &i_channels ) < 0 )
121         {
122             return VLC_EGENERIC;
123         }
124
125         p_dec->fmt_out.audio.i_rate = i_rate;
126         p_dec->fmt_out.audio.i_channels = i_channels;
127         p_dec->fmt_out.audio.i_physical_channels =
128             p_dec->fmt_out.audio.i_original_channels =
129                 pi_channels_maps[i_channels];
130
131         aout_DateInit( &p_sys->date, i_rate );
132     }
133     else
134     {
135         /* Will be initalised from first frame */
136         p_dec->fmt_out.audio.i_rate = 0;
137         p_dec->fmt_out.audio.i_channels = 0;
138         p_dec->fmt_out.audio.i_physical_channels =
139             p_dec->fmt_out.audio.i_original_channels = 0;
140     }
141
142     /* Set the faad config */
143     cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
144     cfg->outputFormat = FAAD_FMT_FLOAT;
145     faacDecSetConfiguration( p_sys->hfaad, cfg );
146
147     /* buffer */
148     p_sys->i_buffer = 0;
149     p_sys->i_buffer_size = 10000;
150     p_sys->p_buffer = malloc( p_sys->i_buffer_size );
151
152     return VLC_SUCCESS;
153 }
154
155 /*****************************************************************************
156  * DecodeBlock:
157  *****************************************************************************/
158 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
159 {
160     decoder_sys_t *p_sys = p_dec->p_sys;
161     block_t *p_block;
162
163     if( !pp_block || !*pp_block ) return NULL;
164
165     p_block = *pp_block;
166
167     if( p_block->b_discontinuity )
168     {
169         block_Release( p_block );
170         return NULL;
171     }
172
173     /* Append the block to the temporary buffer */
174     if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
175     {
176         p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
177         p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
178     }
179
180     if( p_block->i_buffer )
181     {
182         memcpy( &p_sys->p_buffer[p_sys->i_buffer],
183                 p_block->p_buffer, p_block->i_buffer );
184         p_sys->i_buffer += p_block->i_buffer;
185         p_block->i_buffer = 0;
186     }
187
188     if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
189     {
190         unsigned long i_rate;
191         unsigned char i_channels;
192
193         /* Init faad with the first frame */
194         if( faacDecInit( p_sys->hfaad,
195                          p_sys->p_buffer, p_sys->i_buffer,
196                          &i_rate, &i_channels ) < 0 )
197         {
198             block_Release( p_block );
199             return NULL;
200         }
201
202         p_dec->fmt_out.audio.i_rate = i_rate;
203         p_dec->fmt_out.audio.i_channels = i_channels;
204         p_dec->fmt_out.audio.i_physical_channels =
205             p_dec->fmt_out.audio.i_original_channels =
206                 pi_channels_maps[i_channels];
207
208         aout_DateInit( &p_sys->date, i_rate );
209     }
210
211     if( p_block->i_pts != 0 && p_block->i_pts != aout_DateGet( &p_sys->date ) )
212     {
213         aout_DateSet( &p_sys->date, p_block->i_pts );
214     }
215     else if( !aout_DateGet( &p_sys->date ) )
216     {
217         /* We've just started the stream, wait for the first PTS. */
218         block_Release( p_block );
219         p_sys->i_buffer = 0;
220         return NULL;
221     }
222
223     /* Decode all data */
224     if( p_sys->i_buffer )
225     {
226         void *samples;
227         faacDecFrameInfo frame;
228         aout_buffer_t *p_out;
229
230         samples = faacDecDecode( p_sys->hfaad, &frame,
231                                  p_sys->p_buffer, p_sys->i_buffer );
232
233         if( frame.error > 0 )
234         {
235             msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
236
237             /* Flush the buffer */
238             p_sys->i_buffer = 0;
239             block_Release( p_block );
240             return NULL;
241         }
242
243         if( frame.channels <= 0 || frame.channels > 6 )
244         {
245             msg_Warn( p_dec, "invalid channels count" );
246
247             /* Flush the buffer */
248             p_sys->i_buffer = 0;
249             block_Release( p_block );
250             return NULL;
251         }
252
253         if( frame.samples <= 0 )
254         {
255             msg_Warn( p_dec, "decoded zero samples" );
256
257             /* Flush the buffer */
258             p_sys->i_buffer = 0;
259             block_Release( p_block );
260             return NULL;
261         }
262
263         /* We decoded a valid frame */
264         if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
265         {
266             aout_DateInit( &p_sys->date, frame.samplerate );
267             aout_DateSet( &p_sys->date, p_block->i_pts );
268         }
269         p_block->i_pts = 0;  /* PTS is valid only once */
270
271         p_dec->fmt_out.audio.i_rate = frame.samplerate;
272         p_dec->fmt_out.audio.i_channels = frame.channels;
273         p_dec->fmt_out.audio.i_physical_channels =
274             p_dec->fmt_out.audio.i_original_channels =
275                 pi_channels_maps[frame.channels];
276
277         p_out = p_dec->pf_aout_buffer_new( p_dec,
278                                            frame.samples / frame.channels );
279         if( p_out == NULL )
280         {
281             p_sys->i_buffer = 0;
282             block_Release( p_block );
283             return NULL;
284         }
285
286         p_out->start_date = aout_DateGet( &p_sys->date );
287         p_out->end_date = aout_DateIncrement( &p_sys->date,
288                                               frame.samples / frame.channels );
289
290         memcpy( p_out->p_buffer, samples, p_out->i_nb_bytes );
291
292         p_sys->i_buffer -= frame.bytesconsumed;
293         if( p_sys->i_buffer > 0 )
294         {
295             memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
296                      p_sys->i_buffer );
297         }
298
299         return p_out;
300     }
301
302     block_Release( p_block );
303     return NULL;
304 }
305
306 /*****************************************************************************
307  * Close:
308  *****************************************************************************/
309 static void Close( vlc_object_t *p_this )
310 {
311     decoder_t *p_dec = (decoder_t *)p_this;
312     decoder_sys_t *p_sys = p_dec->p_sys;
313
314     faacDecClose( p_sys->hfaad );
315     free( p_sys );
316 }