]> git.sesse.net Git - vlc/blob - modules/codec/faad/decoder.c
1631c5c1fdc797115e560badbf59e8de5235bd00
[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.13 2002/11/15 01:17:08 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 /*****************************************************************************
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
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 decoder module (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 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     ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
136
137 #define GetDWLE( p ) \
138     (  *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
139         ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
140     
141 static void faac_GetWaveFormatEx( waveformatex_t *p_wh,
142                                           u8 *p_data )
143 {
144
145     p_wh->i_formattag     = GetWLE( p_data );
146     p_wh->i_nb_channels   = GetWLE( p_data + 2 );
147     p_wh->i_samplespersec = GetDWLE( p_data + 4 );
148     p_wh->i_avgbytespersec= GetDWLE( p_data + 8 );
149     p_wh->i_blockalign    = GetWLE( p_data + 12 );
150     p_wh->i_bitspersample = GetWLE( p_data + 14 );
151     p_wh->i_size          = GetWLE( p_data + 16 );
152
153     if( p_wh->i_size )
154     {
155         p_wh->p_data = malloc( p_wh->i_size );
156         memcpy( p_wh->p_data, p_data + 18, p_wh->i_size );
157     }
158 }
159
160 static void GetPESData( u8 *p_buf, int i_max, pes_packet_t *p_pes )
161 {   
162     int i_copy; 
163     int i_count;
164
165     data_packet_t   *p_data;
166
167     i_count = 0;
168     p_data = p_pes->p_first;
169     while( p_data != NULL && i_count < i_max )
170     {
171
172         i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start, 
173                         i_max - i_count );
174
175         if( i_copy > 0 )
176         {
177             memcpy( p_buf,
178                     p_data->p_payload_start,
179                     i_copy );
180         }
181
182         p_data = p_data->p_next;
183         i_count += i_copy;
184         p_buf   += i_copy;
185     }
186
187     if( i_count < i_max )
188     {
189         memset( p_buf, 0, i_max - i_count );
190     }
191 }
192
193 /*****************************************************************************
194  * InitThread: initialize data before entering main loop
195  *****************************************************************************/
196 static int InitThread( adec_thread_t * p_adec )
197 {
198     int i_status;
199     unsigned long i_rate;
200     unsigned char i_nb_channels;
201             
202     faacDecConfiguration *p_faad_config;
203
204     if( !p_adec->p_fifo->p_demux_data )
205     {
206         msg_Warn( p_adec->p_fifo,
207                   "cannot load stream informations" );
208     }
209     else
210     {
211         faac_GetWaveFormatEx( &p_adec->format,
212                               (u8*)p_adec->p_fifo->p_demux_data );
213     }
214
215     p_adec->p_buffer = NULL;
216     p_adec->i_buffer = 0;
217
218
219     if( !( p_adec->p_handle = faacDecOpen() ) )
220     {
221         msg_Err( p_adec->p_fifo,
222                  "cannot initialize faad" );
223         FREE( p_adec->format.p_data );
224         return( -1 );
225     }
226     
227     if( p_adec->format.p_data == NULL )
228     {
229         int i_frame_size;
230         pes_packet_t *p_pes;
231
232         msg_Warn( p_adec->p_fifo,
233                  "DecoderSpecificInfo missing, trying with first frame" );
234         // gather first frame
235         do
236         {
237             input_ExtractPES( p_adec->p_fifo, &p_pes );
238             if( !p_pes )
239             {
240                 return( -1 );
241             }
242             i_frame_size = p_pes->i_pes_size;
243
244             if( i_frame_size > 0 )
245             {
246                 if( p_adec->i_buffer < i_frame_size + 16 )
247                 {
248                     FREE( p_adec->p_buffer );
249                     p_adec->p_buffer = malloc( i_frame_size + 16 );
250                     p_adec->i_buffer = i_frame_size + 16;
251                 }
252                 
253                 GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
254             }
255             else
256             {
257                 input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
258             }
259         } while( i_frame_size <= 0 );
260
261
262         i_status = faacDecInit( p_adec->p_handle,
263                                 p_adec->p_buffer,
264                                 i_frame_size,
265                                 &i_rate,
266                                 &i_nb_channels );
267     }
268     else
269     {
270         i_status = faacDecInit2( p_adec->p_handle,
271                                  p_adec->format.p_data,
272                                  p_adec->format.i_size,
273                                  &i_rate,
274                                  &i_nb_channels );
275     }
276
277     if( i_status < 0 )
278     {
279         msg_Err( p_adec->p_fifo,
280                  "failed to initialize faad" );
281         faacDecClose( p_adec->p_handle );
282         return( -1 );
283     }
284     msg_Dbg( p_adec->p_fifo,
285              "faad intitialized, samplerate:%dHz channels:%d",
286              i_rate, 
287              i_nb_channels );
288
289
290     /* set default configuration */
291     p_faad_config = faacDecGetCurrentConfiguration( p_adec->p_handle );
292     p_faad_config->outputFormat = FAAD_FMT_FLOAT;
293     faacDecSetConfiguration( p_adec->p_handle, p_faad_config );
294         
295
296     /* Initialize the thread properties */
297     p_adec->output_format.i_format = VLC_FOURCC('f','l','3','2');
298     p_adec->output_format.i_rate = i_rate;
299     p_adec->output_format.i_physical_channels =
300         p_adec->output_format.i_original_channels =
301             pi_channels_maps[i_nb_channels];
302     p_adec->p_aout = NULL;
303     p_adec->p_aout_input = NULL;
304
305     p_adec->pts = 0;
306   
307     return( 0 );
308 }
309
310 /*****************************************************************************
311  * DecodeThread: decodes a frame
312  *****************************************************************************
313  * XXX it will work only for frame based streams like from mp4 file
314  *     but it's the only way to use libfaad2 without having segfault
315  *     after 1 or 2 frames
316  *****************************************************************************/
317 static void DecodeThread( adec_thread_t *p_adec )
318 {
319     aout_buffer_t    *p_aout_buffer;
320
321     void             *p_faad_buffer;
322     faacDecFrameInfo faad_frame;
323     int  i_frame_size;
324     pes_packet_t *p_pes;
325
326     /* **** Get a new frames from streams **** */
327     do
328     {
329         input_ExtractPES( p_adec->p_fifo, &p_pes );
330         if( !p_pes )
331         {
332             p_adec->p_fifo->b_error = 1;
333             return;
334         }
335         if( p_pes->i_pts != 0 )
336         {
337             p_adec->pts = p_pes->i_pts;
338         }
339         i_frame_size = p_pes->i_pes_size;
340
341         if( i_frame_size > 0 )
342         {
343             if( p_adec->i_buffer < i_frame_size + 16 )
344             {
345                 FREE( p_adec->p_buffer );
346                 p_adec->p_buffer = malloc( i_frame_size + 16 );
347                 p_adec->i_buffer = i_frame_size + 16;
348             }
349             
350             GetPESData( p_adec->p_buffer, p_adec->i_buffer, p_pes );
351         }
352         input_DeletePES( p_adec->p_fifo->p_packets_mgt, p_pes );
353     } while( i_frame_size <= 0 );
354     
355     /* **** decode this frame **** */
356     p_faad_buffer = faacDecDecode( p_adec->p_handle,
357                                    &faad_frame,
358                                    p_adec->p_buffer,
359                                    i_frame_size );
360
361     /* **** some sanity checks to see if we have samples to out **** */
362     if( faad_frame.error > 0 )
363     {
364         msg_Warn( p_adec->p_fifo, "%s", 
365                   faacDecGetErrorMessage(faad_frame.error) );
366         return;
367     }
368     if( ( faad_frame.channels <= 0 )||
369         ( faad_frame.channels > AAC_MAXCHANNELS) ||
370         ( faad_frame.channels > 5 ) )
371     {
372         msg_Warn( p_adec->p_fifo,
373                   "invalid channels count(%d)", faad_frame.channels );
374         return;
375     }
376     if( faad_frame.samples <= 0 )
377     {
378         msg_Warn( p_adec->p_fifo, "decoded zero sample !" );
379         return;
380     }
381
382 #if 0
383     msg_Dbg( p_adec->p_fifo,
384              "decoded frame samples:%d, channels:%d, consumed:%d",
385              faad_frame.samples,
386              faad_frame.channels,
387              faad_frame.bytesconsumed );
388 #endif
389
390     /* **** Now we can output these samples **** */
391     
392     /* **** First check if we have a valid output **** */
393     if( ( !p_adec->p_aout_input )||
394         ( p_adec->output_format.i_original_channels !=
395              pi_channels_maps[faad_frame.channels] ) )
396     {
397         if( p_adec->p_aout_input )
398         {
399             /* **** Delete the old **** */
400             aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
401         }
402
403         /* **** Create a new audio output **** */
404         p_adec->output_format.i_physical_channels =
405             p_adec->output_format.i_original_channels =
406                 pi_channels_maps[faad_frame.channels];
407
408         aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
409         p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
410                                             &p_adec->p_aout,
411                                             &p_adec->output_format );
412     }
413
414     if( !p_adec->p_aout_input )
415     {
416         msg_Err( p_adec->p_fifo, "cannot create aout" );
417         return;
418     }
419     
420     if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
421     {
422         aout_DateSet( &p_adec->date, p_adec->pts );
423     }
424     else if( !aout_DateGet( &p_adec->date ) )
425     {
426         return;
427     }
428
429     p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout, 
430                                        p_adec->p_aout_input,
431                                        faad_frame.samples / faad_frame.channels );
432     if( !p_aout_buffer )
433     {
434         msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
435         p_adec->p_fifo->b_error = 1;
436         return;
437     }
438     p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
439     p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
440                                                   faad_frame.samples /
441                                                       faad_frame.channels );
442     memcpy( p_aout_buffer->p_buffer,
443             p_faad_buffer,
444             p_aout_buffer->i_nb_bytes );
445
446     aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
447 }
448
449
450 /*****************************************************************************
451  * EndThread : faad decoder thread destruction
452  *****************************************************************************/
453 static void EndThread (adec_thread_t *p_adec)
454 {
455     if( p_adec->p_aout_input )
456     {
457         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
458     }
459
460     if( p_adec->p_handle )
461     {
462         faacDecClose( p_adec->p_handle );
463     }
464
465     FREE( p_adec->format.p_data );
466     FREE( p_adec->p_buffer );
467
468     msg_Dbg( p_adec->p_fifo, "faad decoder closed" );
469         
470     free( p_adec );
471 }
472