]> git.sesse.net Git - vlc/blob - modules/codec/faad/decoder.c
* modules/*: sanitization of the modules description strings.
[vlc] / modules / codec / faad / decoder.c
1 /*****************************************************************************
2  * decoder.c: AAC decoder using libfaad2
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: decoder.c,v 1.22 2003/03/30 18:14:36 gbazin 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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/aout.h>
29 #include <vlc/decoder.h>
30 #include <vlc/input.h>
31
32 #include <stdlib.h>                                      /* malloc(), free() */
33 #include <string.h>                                              /* strdup() */
34
35 #include <faad.h>
36 #include "codecs.h"
37 #include "decoder.h"
38
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  OpenDecoder    ( vlc_object_t * );
44
45 static int  RunDecoder     ( decoder_fifo_t * );
46 static int  InitThread     ( adec_thread_t * );
47 static void DecodeThread   ( adec_thread_t * );
48 static void EndThread      ( adec_thread_t * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53
54 vlc_module_begin();
55     set_description( _("AAC audio decoder (using libfaad2)") );
56     set_capability( "decoder", 60 );
57     set_callbacks( OpenDecoder, NULL );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * OpenDecoder: probe the decoder and return score
62  *****************************************************************************
63  * Tries to launch a decoder and return score so that the interface is able
64  * to choose.
65  *****************************************************************************/
66 static int OpenDecoder( vlc_object_t *p_this )
67 {
68     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
69
70     if( p_fifo->i_fourcc != VLC_FOURCC('m','p','4','a') )
71     {
72         return VLC_EGENERIC;
73     }
74
75     p_fifo->pf_run = RunDecoder;
76     return VLC_SUCCESS;
77 }
78
79 /*****************************************************************************
80  * RunDecoder: this function is called just after the thread is created
81  *****************************************************************************/
82 static int RunDecoder( decoder_fifo_t *p_fifo )
83 {
84     adec_thread_t *p_adec;
85     int b_error;
86
87     if( !( p_adec = malloc( sizeof( adec_thread_t ) ) ) )
88     {
89         msg_Err( p_fifo, "out of memory" );
90         DecoderError( p_fifo );
91         return( -1 );
92     }
93     memset( p_adec, 0, sizeof( adec_thread_t ) );
94
95     p_adec->p_fifo = p_fifo;
96
97     if( InitThread( p_adec ) != 0 )
98     {
99         DecoderError( p_fifo );
100         return( -1 );
101     }
102
103     while( ( !p_adec->p_fifo->b_die )&&( !p_adec->p_fifo->b_error ) )
104     {
105         DecodeThread( p_adec );
106     }
107
108
109     if( ( b_error = p_adec->p_fifo->b_error ) )
110     {
111         DecoderError( p_adec->p_fifo );
112     }
113
114     EndThread( p_adec );
115     if( b_error )
116     {
117         return( -1 );
118     }
119
120     return( 0 );
121 }
122
123 static unsigned int pi_channels_maps[6] =
124 {
125     0,
126     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
127     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
128     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
129     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
130      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
131 };
132
133 #define FREE( p ) if( p != NULL ) free( p ); p = NULL
134 #define GetWLE( p ) \
135     ( *(uint8_t*)(p) + ( *((uint8_t*)(p)+1) << 8 ) )
136
137 #define GetDWLE( p ) \
138     (  *(uint8_t*)(p) + ( *((uint8_t*)(p)+1) << 8 ) + \
139         ( *((uint8_t*)(p)+2) << 16 ) + ( *((uint8_t*)(p)+3) << 24 ) )
140
141
142 static void GetPESData( uint8_t *p_buf, int i_max, pes_packet_t *p_pes )
143 {
144     int i_copy;
145     int i_count;
146
147     data_packet_t   *p_data;
148
149     i_count = 0;
150     p_data = p_pes->p_first;
151     while( p_data != NULL && i_count < i_max )
152     {
153
154         i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
155                         i_max - i_count );
156
157         if( i_copy > 0 )
158         {
159             memcpy( p_buf,
160                     p_data->p_payload_start,
161                     i_copy );
162         }
163
164         p_data = p_data->p_next;
165         i_count += i_copy;
166         p_buf   += i_copy;
167     }
168
169     if( i_count < i_max )
170     {
171         memset( p_buf, 0, i_max - i_count );
172     }
173 }
174
175 /*****************************************************************************
176  * InitThread: initialize data before entering main loop
177  *****************************************************************************/
178 static int InitThread( adec_thread_t * p_adec )
179 {
180     WAVEFORMATEX    wf, *p_wf;
181     int i_status;
182     unsigned long   i_rate;
183     unsigned char   i_nb_channels;
184
185     faacDecConfiguration *p_faad_config;
186
187     if( ( p_wf = (WAVEFORMATEX*)p_adec->p_fifo->p_waveformatex ) == NULL )
188     {
189         msg_Warn( p_adec->p_fifo,
190                   "cannot load stream informations" );
191         p_wf = &wf;
192         memset( p_wf, 0, sizeof( WAVEFORMATEX ) );
193     }
194
195     p_adec->p_buffer = NULL;
196     p_adec->i_buffer = 0;
197
198
199     if( !( p_adec->p_handle = faacDecOpen() ) )
200     {
201         msg_Err( p_adec->p_fifo,
202                  "cannot initialize faad" );
203         return( -1 );
204     }
205
206     if( p_wf->cbSize <= 0 )
207     {
208         int i_frame_size;
209         pes_packet_t *p_pes;
210
211         msg_Warn( p_adec->p_fifo,
212                  "DecoderSpecificInfo missing, trying with first frame" );
213         // gather first frame
214         do
215         {
216             input_ExtractPES( p_adec->p_fifo, &p_pes );
217             if( !p_pes )
218             {
219                 return( -1 );
220             }
221             i_frame_size = p_pes->i_pes_size;
222
223             if( i_frame_size > 0 )
224             {
225                 if( p_adec->i_buffer < i_frame_size + 16 )
226                 {
227                     FREE( p_adec->p_buffer );
228                     p_adec->p_buffer = malloc( i_frame_size + 16 );
229                     p_adec->i_buffer = i_frame_size + 16;
230                 }
231
232                 GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
233             }
234             else
235             {
236                 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
237             }
238         } while( i_frame_size <= 0 );
239
240
241         i_status = faacDecInit( p_adec->p_handle,
242                                 p_adec->p_buffer,
243                                 i_frame_size,
244                                 &i_rate,
245                                 &i_nb_channels );
246     }
247     else
248     {
249         i_status = faacDecInit2( p_adec->p_handle,
250                                  (uint8_t*)&p_wf[1],
251                                  p_wf->cbSize,
252                                  &i_rate,
253                                  &i_nb_channels );
254     }
255
256     if( i_status < 0 )
257     {
258         msg_Err( p_adec->p_fifo,
259                  "failed to initialize faad" );
260         //faacDecClose( p_adec->p_handle );
261         return( -1 );
262     }
263     msg_Dbg( p_adec->p_fifo,
264              "faad intitialized, samplerate: %ldHz channels: %d",
265              i_rate, 
266              i_nb_channels );
267
268
269     /* set default configuration */
270     p_faad_config = faacDecGetCurrentConfiguration( p_adec->p_handle );
271     p_faad_config->outputFormat = FAAD_FMT_FLOAT;
272     faacDecSetConfiguration( p_adec->p_handle, p_faad_config );
273
274
275     /* Initialize the thread properties */
276     p_adec->output_format.i_format = VLC_FOURCC('f','l','3','2');
277     p_adec->output_format.i_rate = i_rate;
278     p_adec->output_format.i_physical_channels =
279         p_adec->output_format.i_original_channels =
280             pi_channels_maps[i_nb_channels];
281     p_adec->p_aout = NULL;
282     p_adec->p_aout_input = NULL;
283
284     p_adec->pts = 0;
285
286     return( 0 );
287 }
288
289 /*****************************************************************************
290  * DecodeThread: decodes a frame
291  *****************************************************************************
292  * XXX it will work only for frame based streams like from mp4 file
293  *     but it's the only way to use libfaad2 without having segfault
294  *     after 1 or 2 frames
295  *****************************************************************************/
296 static void DecodeThread( adec_thread_t *p_adec )
297 {
298     aout_buffer_t    *p_aout_buffer;
299
300     void             *p_faad_buffer;
301     faacDecFrameInfo faad_frame;
302     int  i_frame_size;
303     pes_packet_t *p_pes;
304
305     /* **** Get a new frames from streams **** */
306     do
307     {
308         input_ExtractPES( p_adec->p_fifo, &p_pes );
309         if( !p_pes )
310         {
311             p_adec->p_fifo->b_error = 1;
312             return;
313         }
314         if( p_pes->i_pts != 0 )
315         {
316             p_adec->pts = p_pes->i_pts;
317         }
318         i_frame_size = p_pes->i_pes_size;
319
320         if( i_frame_size > 0 )
321         {
322             if( p_adec->i_buffer < i_frame_size + 16 )
323             {
324                 FREE( p_adec->p_buffer );
325                 p_adec->p_buffer = malloc( i_frame_size + 16 );
326                 p_adec->i_buffer = i_frame_size + 16;
327             }
328
329             GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
330         }
331         input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
332     } while( i_frame_size <= 0 );
333
334     /* **** decode this frame **** */
335     p_faad_buffer = faacDecDecode( p_adec->p_handle,
336                                    &faad_frame,
337                                    p_adec->p_buffer,
338                                    i_frame_size );
339
340     /* **** some sanity checks to see if we have samples to out **** */
341     if( faad_frame.error > 0 )
342     {
343         msg_Warn( p_adec->p_fifo, "%s",
344                   faacDecGetErrorMessage(faad_frame.error) );
345         return;
346     }
347     if( ( faad_frame.channels <= 0 )||
348         ( faad_frame.channels > AAC_MAXCHANNELS) ||
349         ( faad_frame.channels > 5 ) )
350     {
351         msg_Warn( p_adec->p_fifo,
352                   "invalid channels count(%d)", faad_frame.channels );
353         return;
354     }
355     if( faad_frame.samples <= 0 )
356     {
357         msg_Warn( p_adec->p_fifo, "decoded zero sample !" );
358         return;
359     }
360
361 #if 0
362     msg_Dbg( p_adec->p_fifo,
363              "decoded frame samples:%d, channels:%d, consumed:%d",
364              faad_frame.samples,
365              faad_frame.channels,
366              faad_frame.bytesconsumed );
367 #endif
368
369     /* **** Now we can output these samples **** */
370
371     /* **** First check if we have a valid output **** */
372     if( ( !p_adec->p_aout_input )||
373         ( p_adec->output_format.i_original_channels !=
374              pi_channels_maps[faad_frame.channels] ) )
375     {
376         if( p_adec->p_aout_input )
377         {
378             /* **** Delete the old **** */
379             aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
380         }
381
382         /* **** Create a new audio output **** */
383         p_adec->output_format.i_physical_channels =
384             p_adec->output_format.i_original_channels =
385                 pi_channels_maps[faad_frame.channels];
386
387         aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
388         p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
389                                             &p_adec->p_aout,
390                                             &p_adec->output_format );
391     }
392
393     if( !p_adec->p_aout_input )
394     {
395         msg_Err( p_adec->p_fifo, "cannot create aout" );
396         return;
397     }
398
399     if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
400     {
401         aout_DateSet( &p_adec->date, p_adec->pts );
402     }
403     else if( !aout_DateGet( &p_adec->date ) )
404     {
405         return;
406     }
407
408     p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout,
409                                        p_adec->p_aout_input,
410                                        faad_frame.samples / faad_frame.channels );
411     if( !p_aout_buffer )
412     {
413         msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
414         p_adec->p_fifo->b_error = 1;
415         return;
416     }
417     p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
418     p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
419                                                   faad_frame.samples /
420                                                       faad_frame.channels );
421     memcpy( p_aout_buffer->p_buffer,
422             p_faad_buffer,
423             p_aout_buffer->i_nb_bytes );
424
425     aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
426 }
427
428
429 /*****************************************************************************
430  * EndThread : faad decoder thread destruction
431  *****************************************************************************/
432 static void EndThread (adec_thread_t *p_adec)
433 {
434     if( p_adec->p_aout_input )
435     {
436         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
437     }
438
439     if( p_adec->p_handle )
440     {
441         faacDecClose( p_adec->p_handle );
442     }
443
444     FREE( p_adec->p_buffer );
445
446     msg_Dbg( p_adec->p_fifo, "faad decoder closed" );
447
448     free( p_adec );
449 }
450