]> git.sesse.net Git - vlc/blob - modules/codec/faad/decoder.c
f95e00be22c26334ea48e3b15ae86fc342725e7f
[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.3 2002/09/26 22:40:21 massiot 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
124 #define FREE( p ) if( p ) free( p ); p = NULL
125 #define GetWLE( p ) \
126     ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
127
128 #define GetDWLE( p ) \
129     (  *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
130         ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
131     
132 static void faac_GetWaveFormatEx( waveformatex_t *p_wh,
133                                           u8 *p_data )
134 {
135
136     p_wh->i_formattag     = GetWLE( p_data );
137     p_wh->i_channels      = GetWLE( p_data + 2 );
138     p_wh->i_samplespersec = GetDWLE( p_data + 4 );
139     p_wh->i_avgbytespersec= GetDWLE( p_data + 8 );
140     p_wh->i_blockalign    = GetWLE( p_data + 12 );
141     p_wh->i_bitspersample = GetWLE( p_data + 14 );
142     p_wh->i_size          = GetWLE( p_data + 16 );
143
144     if( p_wh->i_size )
145     {
146         p_wh->p_data = malloc( p_wh->i_size );
147         memcpy( p_wh->p_data, p_data + 18, p_wh->i_size );
148     }
149 }
150
151 /* get the first pes from fifo */
152 static pes_packet_t *__PES_GET( decoder_fifo_t *p_fifo )
153 {
154     pes_packet_t *p_pes;
155
156     vlc_mutex_lock( &p_fifo->data_lock );
157
158     /* if fifo is emty wait */
159     while( !p_fifo->p_first )
160     {
161         if( p_fifo->b_die )
162         {
163             vlc_mutex_unlock( &p_fifo->data_lock );
164             return( NULL );
165         }
166         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
167     }
168     p_pes = p_fifo->p_first;
169
170     vlc_mutex_unlock( &p_fifo->data_lock );
171
172     return( p_pes );
173 }
174
175 /* free the first pes and go to next */
176 static void __PES_NEXT( decoder_fifo_t *p_fifo )
177 {
178     pes_packet_t *p_next;
179
180     vlc_mutex_lock( &p_fifo->data_lock );
181     
182     p_next = p_fifo->p_first->p_next;
183     p_fifo->p_first->p_next = NULL;
184     input_DeletePES( p_fifo->p_packets_mgt, p_fifo->p_first );
185     p_fifo->p_first = p_next;
186     p_fifo->i_depth--;
187
188     if( !p_fifo->p_first )
189     {
190         /* No PES in the fifo */
191         /* pp_last no longer valid */
192         p_fifo->pp_last = &p_fifo->p_first;
193         while( !p_fifo->p_first )
194         {
195             vlc_cond_signal( &p_fifo->data_wait );
196             vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
197         }
198     }
199     vlc_mutex_unlock( &p_fifo->data_lock );
200 }
201
202 static inline void __GetFrame( adec_thread_t *p_adec )
203 {
204     pes_packet_t  *p_pes;
205     data_packet_t *p_data;
206     byte_t        *p_buffer;
207
208     p_pes = __PES_GET( p_adec->p_fifo );
209     if( p_pes->i_pts )
210     {
211         p_adec->pts = p_pes->i_pts;
212     }
213
214     while( ( !p_pes->i_nb_data )||( !p_pes->i_pes_size ) )
215     {
216         __PES_NEXT( p_adec->p_fifo );
217         p_pes = __PES_GET( p_adec->p_fifo );
218     }
219     p_adec->i_framesize = p_pes->i_pes_size;
220     if( p_pes->i_nb_data == 1 )
221     {
222         p_adec->p_framedata = p_pes->p_first->p_payload_start;
223         return;    
224     }
225     /* get a buffer and gather all data packet */
226     if( p_adec->i_buffer_size < p_pes->i_pes_size )
227     {
228         p_adec->i_buffer_size = 3 * p_pes->i_pes_size / 2;
229         if( p_adec->p_buffer )
230         {
231             p_adec->p_buffer = realloc( p_adec->p_buffer,
232                                         p_adec->i_buffer_size );
233         }
234         else
235         {
236             p_adec->p_buffer = malloc( p_adec->i_buffer_size );
237         }
238     }
239     
240     p_buffer = p_adec->p_framedata = p_adec->p_buffer;
241     p_data = p_pes->p_first;
242     do
243     {
244         p_adec->p_fifo->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start, 
245                      p_data->p_payload_end - p_data->p_payload_start );
246         p_buffer += p_data->p_payload_end - p_data->p_payload_start;
247         p_data = p_data->p_next;
248     } while( p_data );
249 }
250
251 static inline void __NextFrame( adec_thread_t *p_adec )
252 {
253     __PES_NEXT( p_adec->p_fifo );
254 }
255
256
257 /*****************************************************************************
258  * InitThread: initialize data before entering main loop
259  *****************************************************************************/
260 static int InitThread( adec_thread_t * p_adec )
261 {
262     int i_status;
263     unsigned long i_rate;
264     unsigned char i_channels;
265             
266     faacDecConfiguration *p_faad_config;
267
268     if( !p_adec->p_fifo->p_demux_data )
269     {
270         msg_Warn( p_adec->p_fifo,
271                   "cannot load stream informations" );
272     }
273     else
274     {
275         faac_GetWaveFormatEx( &p_adec->format,
276                               (u8*)p_adec->p_fifo->p_demux_data );
277     }
278
279     if( !( p_adec->p_handle = faacDecOpen() ) )
280     {
281         msg_Err( p_adec->p_fifo,
282                  "cannot initialize faad" );
283         FREE( p_adec->format.p_data );
284         return( -1 );
285     }
286     
287     if( !p_adec->format.p_data )
288     {
289
290         msg_Warn( p_adec->p_fifo,
291                  "DecoderSpecificInfo missing, trying with first frame" );
292         __GetFrame( p_adec );
293
294         i_status = faacDecInit( p_adec->p_handle,
295                                 p_adec->p_framedata,
296                                 &i_rate,
297                                 &i_channels );
298 //        __NextFrame( p_adec );
299     }
300     else
301     {
302         i_status = faacDecInit2( p_adec->p_handle,
303                                  p_adec->format.p_data,
304                                  p_adec->format.i_size,
305                                  &i_rate,
306                                  &i_channels );
307     }
308
309     if( i_status < 0 )
310     {
311         msg_Err( p_adec->p_fifo,
312                  "failed to initialize faad" );
313         faacDecClose( p_adec->p_handle );
314         return( -1 );
315     }
316     msg_Dbg( p_adec->p_fifo,
317              "faad intitialized, samplerate:%dHz channels:%d",
318              i_rate, 
319              i_channels );
320
321
322     /* set default configuration */
323     p_faad_config = faacDecGetCurrentConfiguration( p_adec->p_handle );
324     p_faad_config->outputFormat = FAAD_FMT_FLOAT;
325     faacDecSetConfiguration( p_adec->p_handle, p_faad_config );
326         
327
328     /* Initialize the thread properties */
329     p_adec->output_format.i_format = AOUT_FMT_FLOAT32;
330     p_adec->output_format.i_rate = i_rate;
331     p_adec->output_format.i_channels = i_channels;
332   
333 #if 0
334     if( !p_adec->format.p_data )
335     {
336         /* Init the BitStream */
337         InitBitstream( &p_adec->bit_stream, p_adec->p_fifo,
338                        NULL, NULL );
339         p_adec->p_framedata = malloc( AAC_MAXCHANNELS * FAAD_MIN_STREAMSIZE );
340         p_adec->i_framesize = 0;
341     }
342 #endif
343
344     return( 0 );
345 }
346
347 /*****************************************************************************
348  * DecodeThread: decodes a frame
349  *****************************************************************************
350  * XXX it will work only for frame based streams like from mp4 file
351  *     but it's the only way to use libfaad2 without having segfault
352  *     after 1 or 2 frames
353  *****************************************************************************/
354 static void DecodeThread( adec_thread_t *p_adec )
355 {
356     aout_buffer_t    *p_aout_buffer;
357
358     void             *p_faad_buffer;
359     faacDecFrameInfo faad_frame;
360
361     /* **** Get a new frames from streams **** */
362     __GetFrame( p_adec );
363     
364     /* **** decode this frame **** */
365     p_faad_buffer = faacDecDecode( p_adec->p_handle,
366                                    &faad_frame,
367                                    p_adec->p_framedata );
368     /* **** switch to the next frame **** */
369     __NextFrame( p_adec );
370
371 #if 0
372     /* 
373      * XXX don't work ! XXX 
374      *
375      * first even without format.p_data stream can be frame based
376      * and it make libfaad segfault
377      *
378      */
379     if( p_adec->format.p_data )
380     {
381         __GetFrame( p_adec );
382         /* Now decode data */
383         p_faad_buffer = faacDecDecode( p_adec->p_handle,
384                                        &faad_frame,
385                                        p_adec->p_framedata );
386         __NextFrame( p_adec );
387
388     }
389     else
390     {
391         int i_count;
392         /* fill buffer */
393         i_count = __MAX( 1, p_adec->output_format.i_channels ) * FAAD_MIN_STREAMSIZE -
394                         p_adec->i_framesize;
395         
396         GetChunk( &p_adec->bit_stream,
397                   p_adec->p_framedata + p_adec->i_framesize,
398                   i_count );
399         p_adec->i_framesize += i_count;
400         p_faad_buffer = faacDecDecode( p_adec->p_handle,
401                                        &faad_frame,
402                                        p_adec->p_framedata );
403         /* update data buffer pos */
404         i_count = __MIN( p_adec->i_framesize,
405                          __MAX( 1, faad_frame.bytesconsumed ) );
406         memcpy( p_adec->p_framedata,
407                 p_adec->p_framedata + i_count,
408                 p_adec->i_framesize - i_count );
409         p_adec->i_framesize -= i_count;
410     }
411 #endif
412
413     /* **** some sanity checks to see if we have samples to out **** */
414     if( faad_frame.error > 0 )
415     {
416         msg_Warn( p_adec->p_fifo, "%s", 
417                   faacDecGetErrorMessage(faad_frame.error) );
418         return;
419     }
420     if( ( faad_frame.channels <= 0 )||
421         ( faad_frame.channels > AAC_MAXCHANNELS) )
422     {
423         msg_Warn( p_adec->p_fifo,
424                   "invalid channels count(%d)", faad_frame.channels );
425         return;
426     }
427     if( faad_frame.samples <= 0 )
428     {
429         msg_Warn( p_adec->p_fifo, "decoded zero sample !" );
430         return;
431     }
432
433 #if 0
434     msg_Dbg( p_adec->p_fifo,
435              "decoded frame samples:%d, channels:%d, consumed:%d",
436              faad_frame.samples,
437              faad_frame.channels,
438              faad_frame.bytesconsumed );
439 #endif
440
441     /* **** Now we can output these samples **** */
442     
443     /* **** First check if we have a valid output **** */
444     if( ( !p_adec->p_aout_input )||
445         ( p_adec->output_format.i_channels != faad_frame.channels ) )
446     {
447         if( p_adec->p_aout_input )
448         {
449             /* **** Delete the old **** */
450             aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
451         }
452
453         /* **** Create a new audio output **** */
454         p_adec->output_format.i_channels = faad_frame.channels;
455         aout_DateInit( &p_adec->date, p_adec->output_format.i_rate );
456         p_adec->p_aout_input = aout_DecNew( p_adec->p_fifo,
457                                             &p_adec->p_aout,
458                                             &p_adec->output_format );
459     }
460
461     if( !p_adec->p_aout_input )
462     {
463         msg_Err( p_adec->p_fifo, "cannot create aout" );
464         return;
465     }
466     
467     if( p_adec->pts != 0 && p_adec->pts != aout_DateGet( &p_adec->date ) )
468     {
469         aout_DateSet( &p_adec->date, p_adec->pts );
470     }
471     else if( !aout_DateGet( &p_adec->date ) )
472     {
473         return;
474     }
475
476     p_aout_buffer = aout_DecNewBuffer( p_adec->p_aout, 
477                                        p_adec->p_aout_input,
478                                        faad_frame.samples / faad_frame.channels );
479     if( !p_aout_buffer )
480     {
481         msg_Err( p_adec->p_fifo, "cannot get aout buffer" );
482         p_adec->p_fifo->b_error = 1;
483         return;
484     }
485     p_aout_buffer->start_date = aout_DateGet( &p_adec->date );
486     p_aout_buffer->end_date = aout_DateIncrement( &p_adec->date,
487                                                   faad_frame.samples /
488                                                       faad_frame.channels );
489     memcpy( p_aout_buffer->p_buffer,
490             p_faad_buffer,
491             p_aout_buffer->i_nb_bytes );
492
493     aout_DecPlay( p_adec->p_aout, p_adec->p_aout_input, p_aout_buffer );
494 }
495
496
497 /*****************************************************************************
498  * EndThread : faad decoder thread destruction
499  *****************************************************************************/
500 static void EndThread (adec_thread_t *p_adec)
501 {
502     if( p_adec->p_aout_input )
503     {
504         aout_DecDelete( p_adec->p_aout, p_adec->p_aout_input );
505     }
506
507     if( p_adec->p_handle )
508     {
509         faacDecClose( p_adec->p_handle );
510     }
511
512     FREE( p_adec->format.p_data );
513     FREE( p_adec->p_buffer );
514
515     msg_Dbg( p_adec->p_fifo, "faad decoder closed" );
516         
517     free( p_adec );
518 }
519