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