]> git.sesse.net Git - vlc/blob - modules/packetizer/mpeg4audio.c
Check malloc return values and small cleanup.
[vlc] / modules / packetizer / mpeg4audio.c
1 /*****************************************************************************
2  * mpeg4audio.c: parse and packetize an MPEG 4 audio stream
3  *****************************************************************************
4  * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_aout.h>
35 #include <vlc_codec.h>
36 #include <vlc_block.h>
37 #include <vlc_sout.h>
38 #include <vlc_codecs.h>
39 #include <vlc_input.h>
40 #include <vlc_bits.h>
41
42 #include "vlc_block_helper.h"
43
44 /* AAC Config in ES:
45  *
46  * AudioObjectType          5 bits
47  * samplingFrequencyIndex   4 bits
48  * if (samplingFrequencyIndex == 0xF)
49  *  samplingFrequency   24 bits
50  * channelConfiguration     4 bits
51  * GA_SpecificConfig
52  *  FrameLengthFlag         1 bit 1024 or 960
53  *  DependsOnCoreCoder      1 bit (always 0)
54  *  ExtensionFlag           1 bit (always 0)
55  */
56
57 /*****************************************************************************
58  * decoder_sys_t : decoder descriptor
59  *****************************************************************************/
60 typedef struct
61 {
62     int i_object_type;
63     int i_samplerate;
64     int i_channel;
65     int i_sbr;          // 0: no sbr, 1: sbr, -1: unknown
66
67     struct
68     {
69         int i_object_type;
70         int i_samplerate;
71     } extension;
72
73     /* GASpecific */
74     int i_frame_length;   // 1024 or 960
75
76 } mpeg4_cfg_t;
77
78 #define LATM_MAX_EXTRA_SIZE 64
79 typedef struct
80 {
81     int i_program;
82     int i_layer;
83
84     int i_frame_length_type;
85     int i_frame_length;         // type 1
86     int i_frame_length_index;   // type 3 4 5 6 7
87
88     mpeg4_cfg_t cfg;
89
90     /* Raw configuration */
91     int     i_extra;
92     uint8_t extra[LATM_MAX_EXTRA_SIZE];
93
94 } latm_stream_t;
95
96 #define LATM_MAX_LAYER (8)
97 #define LATM_MAX_PROGRAM (16)
98 typedef struct
99 {
100     int b_same_time_framing;
101     int i_sub_frames;
102     int i_programs;
103
104     int pi_layers[LATM_MAX_PROGRAM];
105
106     int pi_stream[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
107
108     int i_streams;
109     latm_stream_t stream[LATM_MAX_PROGRAM*LATM_MAX_LAYER];
110
111     int i_other_data;
112     int i_crc;  /* -1 if not set */
113 } latm_mux_t;
114
115 struct decoder_sys_t
116 {
117     /*
118      * Input properties
119      */
120     int i_state;
121     int i_type;
122
123     block_bytestream_t bytestream;
124
125     /*
126      * Common properties
127      */
128     audio_date_t end_date;
129     mtime_t i_pts;
130
131     int i_frame_size;
132     unsigned int i_channels;
133     unsigned int i_rate, i_frame_length, i_header_size;
134
135     int i_input_rate;
136
137     /* LOAS */
138     vlc_bool_t b_latm_cfg;
139     latm_mux_t latm;
140 };
141
142 enum {
143     STATE_NOSYNC,
144     STATE_SYNC,
145     STATE_HEADER,
146     STATE_NEXT_SYNC,
147     STATE_GET_DATA,
148     STATE_SEND_DATA
149 };
150
151 enum {
152     TYPE_NONE,
153     TYPE_RAW,
154     TYPE_ADTS,
155     TYPE_LOAS
156 };
157
158 static const int pi_sample_rates[16] =
159 {
160     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
161     16000, 12000, 11025, 8000,  7350,  0,     0,     0
162 };
163
164 #define ADTS_HEADER_SIZE 9
165 #define LOAS_HEADER_SIZE 3
166
167 /****************************************************************************
168  * Local prototypes
169  ****************************************************************************/
170 static int  OpenPacketizer( vlc_object_t * );
171 static void ClosePacketizer( vlc_object_t * );
172
173 static block_t *PacketizeRawBlock    ( decoder_t *, block_t ** );
174 static block_t *PacketizeStreamBlock( decoder_t *, block_t ** );
175
176 /*****************************************************************************
177  * Module descriptor
178  *****************************************************************************/
179 vlc_module_begin();
180     set_category( CAT_SOUT );
181     set_subcategory( SUBCAT_SOUT_PACKETIZER );
182     set_description( _("MPEG4 audio packetizer") );
183     set_capability( "packetizer", 50 );
184     set_callbacks( OpenPacketizer, ClosePacketizer );
185 vlc_module_end();
186
187 /*****************************************************************************
188  * OpenPacketizer: probe the packetizer and return score
189  *****************************************************************************/
190 static int OpenPacketizer( vlc_object_t *p_this )
191 {
192     decoder_t *p_dec = (decoder_t*)p_this;
193     decoder_sys_t *p_sys;
194
195     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', '4', 'a' ) )
196     {
197         return VLC_EGENERIC;
198     }
199
200     /* Allocate the memory needed to store the decoder's structure */
201     if( ( p_dec->p_sys = p_sys =
202           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
203     {
204         msg_Err( p_dec, "out of memory" );
205         return VLC_EGENERIC;
206     }
207
208     /* Misc init */
209     p_sys->i_state = STATE_NOSYNC;
210     aout_DateSet( &p_sys->end_date, 0 );
211     p_sys->bytestream = block_BytestreamInit();
212     p_sys->i_input_rate = INPUT_RATE_DEFAULT;
213     p_sys->b_latm_cfg = VLC_FALSE;
214
215     /* Set output properties */
216     p_dec->fmt_out.i_cat = AUDIO_ES;
217     p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','4','a');
218
219     msg_Dbg( p_dec, "running MPEG4 audio packetizer" );
220
221     if( p_dec->fmt_in.i_extra > 0 )
222     {
223         uint8_t *p_config = (uint8_t*)p_dec->fmt_in.p_extra;
224         int     i_index;
225
226         i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) ) & 0x0f;
227         if( i_index != 0x0f )
228         {
229             p_dec->fmt_out.audio.i_rate = pi_sample_rates[i_index];
230             p_dec->fmt_out.audio.i_frame_length =
231                 (( p_config[1] >> 2 ) & 0x01) ? 960 : 1024;
232         }
233         else
234         {
235             p_dec->fmt_out.audio.i_rate = ( ( p_config[1] & 0x7f ) << 17 ) |
236                 ( p_config[2] << 9 ) | ( p_config[3] << 1 ) |
237                 ( p_config[4] >> 7 );
238             p_dec->fmt_out.audio.i_frame_length =
239                 (( p_config[4] >> 2 ) & 0x01) ? 960 : 1024;
240         }
241
242         msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
243                  p_dec->fmt_out.audio.i_rate,
244                  p_dec->fmt_out.audio.i_frame_length );
245
246         aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
247
248         p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
249         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
250         p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
251         if( !p_dec->fmt_out.p_extra )
252         {
253             p_dec->fmt_out.i_extra = 0;
254             return VLC_ENOMEM;
255         }
256         memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
257                 p_dec->fmt_in.i_extra );
258
259         /* Set callback */
260         p_dec->pf_packetize = PacketizeRawBlock;
261         p_sys->i_type = TYPE_RAW;
262     }
263     else
264     {
265         msg_Dbg( p_dec, "no decoder specific info, must be an ADTS or LOAS stream" );
266
267         aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
268
269         /* We will try to create a AAC Config from adts/loas */
270         p_dec->fmt_out.i_extra = 0;
271         p_dec->fmt_out.p_extra = NULL;
272
273         /* Set callback */
274         p_dec->pf_packetize = PacketizeStreamBlock;
275         p_sys->i_type = TYPE_NONE;
276     }
277
278     return VLC_SUCCESS;
279 }
280
281 /****************************************************************************
282  * PacketizeRawBlock: the whole thing
283  ****************************************************************************
284  * This function must be fed with complete frames.
285  ****************************************************************************/
286 static block_t *PacketizeRawBlock( decoder_t *p_dec, block_t **pp_block )
287 {
288     decoder_sys_t *p_sys = p_dec->p_sys;
289     block_t *p_block;
290
291     if( !pp_block || !*pp_block ) return NULL;
292
293     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
294     {
295         //aout_DateSet( &p_sys->end_date, 0 );
296         block_Release( *pp_block );
297         return NULL;
298     }
299
300     p_block = *pp_block;
301     *pp_block = NULL; /* Don't reuse this block */
302
303     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
304     {
305         /* We've just started the stream, wait for the first PTS. */
306         block_Release( p_block );
307         return NULL;
308     }
309     else if( p_block->i_pts != 0 &&
310              p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
311     {
312         aout_DateSet( &p_sys->end_date, p_block->i_pts );
313     }
314
315     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
316
317     p_block->i_length = aout_DateIncrement( &p_sys->end_date,
318         p_dec->fmt_out.audio.i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) - p_block->i_pts;
319
320     return p_block;
321 }
322
323 /****************************************************************************
324  * ADTS helpers
325  ****************************************************************************/
326 static int ADTSSyncInfo( decoder_t * p_dec, const byte_t * p_buf,
327                          unsigned int * pi_channels,
328                          unsigned int * pi_sample_rate,
329                          unsigned int * pi_frame_length,
330                          unsigned int * pi_header_size )
331 {
332     int i_id, i_profile, i_sample_rate_idx, i_frame_size;
333     vlc_bool_t b_crc;
334
335     /* Fixed header between frames */
336     i_id = ( (p_buf[1] >> 3) & 0x01 ) ? 2 : 4;
337     b_crc = !(p_buf[1] & 0x01);
338     i_profile = p_buf[2] >> 6;
339     i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
340     *pi_sample_rate = pi_sample_rates[i_sample_rate_idx];
341     *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
342
343     /* Variable header */
344     i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
345                    ((p_buf[5] >> 5) & 0x7);
346     //i_raw_blocks_in_frame = (p_buf[6] & 0x02) + 1;
347
348     if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
349     {
350         return 0;
351     }
352
353     /* Fixme */
354     *pi_frame_length = 1024;
355
356     /* Build the decoder specific info header */
357     if( !p_dec->fmt_out.i_extra )
358     {
359         p_dec->fmt_out.p_extra = malloc( 2 );
360         if( !p_dec->fmt_out.p_extra )
361         {
362             p_dec->fmt_out.i_extra = 0;
363             return 0;
364         }
365         p_dec->fmt_out.i_extra = 2;
366         ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
367             (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
368         ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
369             ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
370     }
371
372     /* ADTS header length */
373     *pi_header_size = b_crc ? 9 : 7;
374
375     return i_frame_size - *pi_header_size;
376 }
377
378 /****************************************************************************
379  * LOAS helpers
380  ****************************************************************************/
381 static int LOASSyncInfo( uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_header_size )
382 {
383     *pi_header_size = 3;
384     return ( ( p_header[1] & 0x1f ) << 8 ) + p_header[2];
385 }
386
387 static int Mpeg4GAProgramConfigElement( bs_t *s )
388 {
389     /* TODO compute channels count ? */
390     int i_tag = bs_read( s, 4 );
391     if( i_tag != 0x05 )
392         return -1;
393     bs_skip( s, 2 + 4 ); // object type + sampling index
394     int i_num_front = bs_read( s, 4 );
395     int i_num_side = bs_read( s, 4 );
396     int i_num_back = bs_read( s, 4 );
397     int i_num_lfe = bs_read( s, 2 );
398     int i_num_assoc_data = bs_read( s, 3 );
399     int i_num_valid_cc = bs_read( s, 4 );
400
401     if( bs_read1(s) )
402         bs_skip( s, 4 ); // mono downmix
403     if( bs_read1(s) )
404         bs_skip( s, 4 ); // stereo downmix
405     if( bs_read1(s) )
406         bs_skip( s, 2+1 ); // matrix downmix + pseudo_surround
407
408     bs_skip( s, i_num_front * (1+4) );
409     bs_skip( s, i_num_side * (1+4) );
410     bs_skip( s, i_num_back * (1+4) );
411     bs_skip( s, i_num_lfe * (4) );
412     bs_skip( s, i_num_assoc_data * (4) );
413     bs_skip( s, i_num_valid_cc * (5) );
414     bs_align( s );
415     int i_comment = bs_read( s, 8 );
416     bs_skip( s, i_comment * 8 );
417     return 0;
418 }
419
420 static int Mpeg4GASpecificConfig( mpeg4_cfg_t *p_cfg, bs_t *s )
421 {
422     p_cfg->i_frame_length = bs_read1(s) ? 960 : 1024;
423
424     if( bs_read1( s ) )     // depend on core coder
425         bs_skip( s, 14 );   // core coder delay
426
427     int i_extension_flag = bs_read1( s );
428     if( p_cfg->i_channel == 0 )
429     {
430         Mpeg4GAProgramConfigElement( s );
431     }
432     if( p_cfg->i_object_type == 6 || p_cfg->i_object_type == 20 )
433         bs_skip( s, 3 );    // layer
434
435     if( i_extension_flag )
436     {
437         if( p_cfg->i_object_type == 22 )
438         {
439             bs_skip( s, 5 + 11 );   // numOfSubFrame + layer length
440         }
441         if( p_cfg->i_object_type == 17 || p_cfg->i_object_type == 19 ||
442             p_cfg->i_object_type == 20 || p_cfg->i_object_type == 23 )
443         {
444             bs_skip( s, 1+1+1 );    // ER data : section scale spectral */
445         }
446         if( bs_read1( s ) )     // extension 3
447             fprintf( stderr, "Mpeg4GASpecificConfig: error 1\n" );
448     }
449     return 0;
450 }
451
452 static int Mpeg4ReadAudioObjectType( bs_t *s )
453 {
454     int i_type = bs_read( s, 5 );
455     if( i_type == 0x1f )
456         i_type += bs_read( s, 6 );
457     return i_type;
458 }
459
460 static int Mpeg4ReadAudioSamplerate( bs_t *s )
461 {
462     int i_index = bs_read( s, 4 );
463     if( i_index != 0x0f )
464         return pi_sample_rates[i_index];
465     return bs_read( s, 24 );
466 }
467
468 static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_t *p_extra, bs_t *s, int i_max_size )
469 {
470 #if 0
471     static const char *ppsz_otype[] = {
472         "NULL",
473         "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
474         "TwinVQ",
475         "CELP", "HVXC",
476         "Reserved", "Reserved",
477         "TTSI",
478         "Main Synthetic", "Wavetables Synthesis", "General MIDI",
479         "Algorithmic Synthesis and Audio FX",
480         "ER AAC LC",
481         "Reserved",
482         "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
483         "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
484         "SSC",
485         "Reserved", "Reserved", "Escape",
486         "Layer 1", "Layer 2", "Layer 3",
487         "DST",
488     };
489 #endif
490     const int i_pos_start = bs_pos( s );
491     bs_t s_sav = *s;
492     int i_bits;
493     int i;
494
495     memset( p_cfg, 0, sizeof(*p_cfg) );
496     *pi_extra = 0;
497
498     p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
499     p_cfg->i_samplerate = Mpeg4ReadAudioSamplerate( s );
500
501     p_cfg->i_channel = bs_read( s, 4 );
502     if( p_cfg->i_channel == 7 )
503         p_cfg->i_channel = 8; // 7.1
504     else if( p_cfg->i_channel >= 8 )
505         p_cfg->i_channel = -1;
506
507     p_cfg->i_sbr = -1;
508     p_cfg->extension.i_object_type = 0;
509     p_cfg->extension.i_samplerate = 0;
510     if( p_cfg->i_object_type == 5 )
511     {
512         p_cfg->i_sbr = 1;
513         p_cfg->extension.i_object_type = p_cfg->i_object_type;
514         p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
515
516         p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
517     }
518
519     switch( p_cfg->i_object_type )
520     {
521     case 1: case 2: case 3: case 4:
522     case 6: case 7:
523     case 17: case 19: case 20: case 21: case 22: case 23:
524         Mpeg4GASpecificConfig( p_cfg, s );
525         break;
526     case 8:
527         // CelpSpecificConfig();
528         break;
529     case 9:
530         // HvxcSpecificConfig();
531         break;
532     case 12:
533         // TTSSSpecificConfig();
534         break;
535     case 13: case 14: case 15: case 16:
536         // StructuredAudioSpecificConfig();
537         break;
538     case 24:
539         // ERCelpSpecificConfig();
540         break;
541     case 25:
542         // ERHvxcSpecificConfig();
543         break;
544     case 26: case 27:
545         // ParametricSpecificConfig();
546         break;
547     case 28:
548         // SSCSpecificConfig();
549         break;
550     case 32: case 33: case 34:
551         // MPEG_1_2_SpecificConfig();
552         break;
553     case 35:
554         // DSTSpecificConfig();
555         break;
556     default:
557         // error
558         break;
559     }
560     switch( p_cfg->i_object_type )
561     {
562     case 17: case 19: case 20: case 21: case 22: case 23:
563     case 24: case 25: case 26: case 27:
564     {
565         int epConfig = bs_read( s, 2 );
566         if( epConfig == 2 || epConfig == 3 )
567         {
568             //ErrorProtectionSpecificConfig();
569         }
570         if( epConfig == 3 )
571         {
572             int directMapping = bs_read1( s );
573             if( directMapping )
574             {
575                 // tbd ...
576             }
577         }
578         break;
579     }
580     default:
581         break;
582     }
583
584     if( p_cfg->extension.i_object_type != 5 && i_max_size > 0 && i_max_size - (bs_pos(s) - i_pos_start) >= 16 && 
585         bs_read( s, 11 ) == 0x2b7 )
586     {
587         p_cfg->extension.i_object_type = Mpeg4ReadAudioObjectType( s );
588         if( p_cfg->extension.i_object_type == 5 )
589         {
590             p_cfg->i_sbr  = bs_read1( s );
591             if( p_cfg->i_sbr == 1 )
592                 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
593         }
594     }
595
596     //fprintf( stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
597     //         ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr );
598
599     i_bits = bs_pos(s) - i_pos_start;
600
601     *pi_extra = ( i_bits + 7 ) / 8;
602     for( i = 0; i < __MIN( LATM_MAX_EXTRA_SIZE, *pi_extra ); i++ )
603     {
604         const int i_read = __MIN( 8, i_bits - 8*i );
605         p_extra[i] = bs_read( &s_sav, i_read ) << (8-i_read);
606     }
607     return i_bits;
608 }
609
610 static int LatmGetValue( bs_t *s )
611 {
612     int i_bytes = bs_read( s, 2 );
613     int v = 0;
614     int i;
615     for( i = 0; i < i_bytes; i++ )
616         v = (v << 8) + bs_read( s, 8 );
617
618     return v;
619 }
620
621 static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
622 {
623     int i_mux_version;
624     int i_mux_versionA;
625     int i_program;
626
627     i_mux_version = bs_read( s, 1 );
628     i_mux_versionA = 0;
629     if( i_mux_version )
630         i_mux_versionA = bs_read( s, 1 );
631
632     if( i_mux_versionA != 0 ) /* support only A=0 */
633         return -1;
634
635     memset( m, 0, sizeof(*m) );
636
637     if( i_mux_versionA == 0 )
638     {
639         if( i_mux_version == 1 )
640         {
641             LatmGetValue(s); /* taraBufferFullness */
642         }
643     }
644
645     m->b_same_time_framing = bs_read1( s );
646     m->i_sub_frames = 1 + bs_read( s, 6 );
647     m->i_programs = 1 + bs_read( s, 4 );
648
649     for( i_program = 0; i_program < m->i_programs; i_program++ )
650     {
651         int i_layer;
652
653         m->pi_layers[i_program] = 1+bs_read( s, 3 );
654
655         for( i_layer = 0; i_layer < m->pi_layers[i_program]; i_layer++ )
656         {
657             latm_stream_t *st = &m->stream[m->i_streams];
658             vlc_bool_t b_previous_cfg;
659
660             m->pi_stream[i_program][i_layer] = m->i_streams;
661             st->i_program = i_program;
662             st->i_layer = i_layer;
663
664             b_previous_cfg = VLC_FALSE;
665             if( i_program != 0 || i_layer != 0 )
666                 b_previous_cfg = bs_read1( s );
667
668             if( b_previous_cfg )
669             {
670                 assert( m->i_streams > 0 );
671                 st->cfg = m->stream[m->i_streams-1].cfg;
672             }
673             else
674             {
675                 int i_cfg_size = 0;
676                 if( i_mux_version == 1 )
677                     i_cfg_size = LatmGetValue(s);
678                 i_cfg_size -= Mpeg4ReadAudioSpecificInfo( &st->cfg, &st->i_extra, st->extra, s, i_cfg_size );
679                 if( i_cfg_size > 0 )
680                     bs_skip( s, i_cfg_size );
681             }
682
683             st->i_frame_length_type = bs_read( s, 3 );
684             switch( st->i_frame_length_type )
685             {
686             case 0:
687             {
688                 bs_skip( s, 8 ); /* latmBufferFullnes */
689                 if( !m->b_same_time_framing )
690                 {
691                     if( st->cfg.i_object_type == 6 || st->cfg.i_object_type == 20 ||
692                         st->cfg.i_object_type == 8 || st->cfg.i_object_type == 24 )
693                     {
694                         bs_skip( s, 6 ); /* eFrameOffset */
695                     }
696                 }
697                 break;
698             }
699             case 1:
700                 st->i_frame_length = bs_read( s, 9 );
701                 break;
702             case 3: case 4: case 5:
703                 st->i_frame_length_index = bs_read( s, 6 ); // celp
704                 break;
705             case 6: case 7:
706                 st->i_frame_length_index = bs_read( s, 1 ); // hvxc
707             default:
708                 break;
709             }
710             /* Next stream */
711             m->i_streams++;
712         }
713     }
714
715     /* other data */
716     if( bs_read1( s ) )
717     {
718         if( i_mux_version == 1 )
719         {
720             m->i_other_data = LatmGetValue( s );
721         }
722         else
723         {
724             int b_continue;
725             do {
726                 b_continue = bs_read1(s);
727                 m->i_other_data = (m->i_other_data << 8) + bs_read( s, 8 );
728             } while( b_continue );
729         }
730     }
731
732     /* crc */
733     m->i_crc = -1;
734     if( bs_read1( s ) )
735         m->i_crc = bs_read( s, 8 );
736
737     return 0;
738 }
739
740 static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
741 {
742     decoder_sys_t *p_sys = p_dec->p_sys;
743     bs_t s;
744     int i_sub;
745     int i_accumulated = 0;
746
747     bs_init( &s, p_buffer, i_buffer );
748
749     /* Read the stream mux configuration if present */
750     if( !bs_read1( &s ) )
751     {
752         if( !LatmReadStreamMuxConfiguration( &p_sys->latm, &s ) &&
753             p_sys->latm.i_streams > 0 )
754         {
755             const latm_stream_t *st = &p_sys->latm.stream[0];
756
757             p_sys->i_channels = st->cfg.i_channel;
758             p_sys->i_rate = st->cfg.i_samplerate;
759             p_sys->i_frame_length = st->cfg.i_frame_length;
760
761             /* FIXME And if it changes ? */
762             if( !p_dec->fmt_out.i_extra && st->i_extra > 0 )
763             {
764                 p_dec->fmt_out.i_extra = st->i_extra;
765                 p_dec->fmt_out.p_extra = malloc( st->i_extra );
766                 if( !p_dec->fmt_out.p_extra )
767                 {
768                     p_dec->fmt_out.i_extra = 0;
769                     return 0;
770                 }
771                 memcpy( p_dec->fmt_out.p_extra, st->extra, st->i_extra );
772             }
773
774             p_sys->b_latm_cfg = VLC_TRUE;
775         }
776     }
777     /* Wait for the configuration */
778     if( !p_sys->b_latm_cfg )
779         return 0;
780
781     /* FIXME do we need to split the subframe into independant packet ? */
782     if( p_sys->latm.i_sub_frames > 1 )
783         msg_Err( p_dec, "latm sub frames not yet supported, please send a sample" );
784
785     for( i_sub = 0; i_sub < p_sys->latm.i_sub_frames; i_sub++ )
786     {
787         int pi_payload[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
788         if( p_sys->latm.b_same_time_framing )
789         {
790             int i_program;
791             /* Payload length */
792             for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
793             {
794                 int i_layer;
795                 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
796                 {
797                     latm_stream_t *st = &p_sys->latm.stream[p_sys->latm.pi_stream[i_program][i_layer]];
798                     if( st->i_frame_length_type == 0 )
799                     {
800                         int i_payload = 0;
801                         for( ;; )
802                         {
803                             int i_tmp = bs_read( &s, 8 );
804                             i_payload += i_tmp;
805                             if( i_tmp != 255 )
806                                 break;
807                         }
808                         pi_payload[i_program][i_layer] = i_payload;
809                     }
810                     else if( st->i_frame_length_type == 1 )
811                     {
812                         pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
813                     }
814                     else if( ( st->i_frame_length_type == 3 ) ||
815                              ( st->i_frame_length_type == 5 ) ||
816                              ( st->i_frame_length_type == 7 ) )
817                     {
818                         bs_skip( &s, 2 ); // muxSlotLengthCoded
819                         pi_payload[i_program][i_layer] = 0; /* TODO */
820                     }
821                     else
822                     {
823                         pi_payload[i_program][i_layer] = 0; /* TODO */
824                     }
825                 }
826             }
827             /* Payload Data */
828             for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
829             {
830                 int i_layer;
831                 int i;
832                 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
833                 {
834                     /* XXX we only extract 1 stream */
835                     if( i_program != 0 || i_layer != 0 )
836                         break;
837
838                     if( pi_payload[i_program][i_layer] <= 0 )
839                         continue;
840
841                     /* FIXME that's slow (and a bit ugly to write in place) */
842                     for( i = 0; i < pi_payload[i_program][i_layer]; i++ )
843                         p_buffer[i_accumulated++] = bs_read( &s, 8 );
844                 }
845             }
846         }
847         else
848         {
849             const int i_chunks = bs_read( &s, 4 );
850             int pi_program[16];
851             int pi_layer[16];
852             int i_chunk;
853
854             msg_Err( p_dec, "latm without same time frameing not yet supported, please send a sample" );
855
856             for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
857             {
858                 const int streamIndex = bs_read( &s, 4 );
859                 latm_stream_t *st = &p_sys->latm.stream[streamIndex];
860                 const int i_program = st->i_program;
861                 const int i_layer = st->i_layer;
862
863                 pi_program[i_chunk] = i_program;
864                 pi_layer[i_chunk] = i_layer;
865
866                 if( st->i_frame_length_type == 0 )
867                 {
868                     int i_payload = 0;
869                     for( ;; )
870                     {
871                         int i_tmp = bs_read( &s, 8 );
872                         i_payload += i_tmp;
873                         if( i_tmp != 255 )
874                             break;
875                     }
876                     pi_payload[i_program][i_layer] = i_payload;
877                     bs_skip( &s, 1 ); // auEndFlag
878                 }
879                 else if( st->i_frame_length_type == 1 )
880                 {
881                     pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
882                 }
883                 else if( ( st->i_frame_length_type == 3 ) ||
884                          ( st->i_frame_length_type == 5 ) ||
885                          ( st->i_frame_length_type == 7 ) )
886                 {
887                     bs_read( &s, 2 ); // muxSlotLengthCoded
888                 }
889                 else
890                 {
891                 }
892             }
893             for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
894             {
895                 //const int i_program = pi_program[i_chunk];
896                 //const int i_layer = pi_layer[i_chunk];
897
898                 /* TODO ? Payload */
899             }
900         }
901     }
902
903     if( p_sys->latm.i_other_data > 0 )
904     {
905         /* Other data XXX we just ignore them */
906     }
907     bs_align( &s );
908
909     return i_accumulated;
910 }
911
912 /****************************************************************************
913  * PacketizeStreamBlock: ADTS/LOAS packetizer
914  ****************************************************************************/
915 static void SetupOutput( decoder_t *p_dec, block_t *p_block );
916 static block_t *PacketizeStreamBlock( decoder_t *p_dec, block_t **pp_block )
917 {
918     decoder_sys_t *p_sys = p_dec->p_sys;
919     uint8_t p_header[ADTS_HEADER_SIZE + LOAS_HEADER_SIZE];
920     block_t *p_out_buffer;
921     uint8_t *p_buf;
922
923     if( !pp_block || !*pp_block ) return NULL;
924
925     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
926     {
927         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
928         {
929             p_sys->i_state = STATE_NOSYNC;
930             block_BytestreamFlush( &p_sys->bytestream );
931         }
932         //aout_DateSet( &p_sys->end_date, 0 );
933         block_Release( *pp_block );
934         return NULL;
935     }
936
937     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
938     {
939         /* We've just started the stream, wait for the first PTS. */
940         block_Release( *pp_block );
941         return NULL;
942     }
943
944     if( (*pp_block)->i_rate > 0 )
945         p_sys->i_input_rate = (*pp_block)->i_rate;
946
947     block_BytestreamPush( &p_sys->bytestream, *pp_block );
948
949     for( ;; )
950     {
951         switch( p_sys->i_state )
952         {
953
954         case STATE_NOSYNC:
955             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
956                    == VLC_SUCCESS )
957             {
958                 /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
959                 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
960                 {
961                     if( p_sys->i_type != TYPE_ADTS )
962                         msg_Dbg( p_dec, "detected ADTS format" );
963
964                     p_sys->i_state = STATE_SYNC;
965                     p_sys->i_type = TYPE_ADTS;
966                     break;
967                 }
968                 else if( p_header[0] == 0x56 && (p_header[1] & 0xe0) == 0xe0 )
969                 {
970                     if( p_sys->i_type != TYPE_LOAS )
971                         msg_Dbg( p_dec, "detected LOAS format" );
972
973                     p_sys->i_state = STATE_SYNC;
974                     p_sys->i_type = TYPE_LOAS;
975                     break;
976                 }
977                 block_SkipByte( &p_sys->bytestream );
978             }
979             if( p_sys->i_state != STATE_SYNC )
980             {
981                 block_BytestreamFlush( &p_sys->bytestream );
982
983                 /* Need more data */
984                 return NULL;
985             }
986
987         case STATE_SYNC:
988             /* New frame, set the Presentation Time Stamp */
989             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
990             if( p_sys->i_pts != 0 &&
991                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
992             {
993                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
994             }
995             p_sys->i_state = STATE_HEADER;
996             break;
997
998         case STATE_HEADER:
999             if( p_sys->i_type == TYPE_ADTS )
1000             {
1001                 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
1002                 if( block_PeekBytes( &p_sys->bytestream, p_header,
1003                                      ADTS_HEADER_SIZE ) != VLC_SUCCESS )
1004                 {
1005                     /* Need more data */
1006                     return NULL;
1007                 }
1008
1009                 /* Check if frame is valid and get frame info */
1010                 p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
1011                                                     &p_sys->i_channels,
1012                                                     &p_sys->i_rate,
1013                                                     &p_sys->i_frame_length,
1014                                                     &p_sys->i_header_size );
1015             }
1016             else
1017             {
1018                 assert( p_sys->i_type == TYPE_LOAS );
1019                 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
1020                 if( block_PeekBytes( &p_sys->bytestream, p_header,
1021                                      LOAS_HEADER_SIZE ) != VLC_SUCCESS )
1022                 {
1023                     /* Need more data */
1024                     return NULL;
1025                 }
1026
1027                 /* Check if frame is valid and get frame info */
1028                 p_sys->i_frame_size = LOASSyncInfo( p_header, &p_sys->i_header_size );
1029             }
1030
1031             if( p_sys->i_frame_size <= 0 )
1032             {
1033                 msg_Dbg( p_dec, "emulated sync word" );
1034                 block_SkipByte( &p_sys->bytestream );
1035                 p_sys->i_state = STATE_NOSYNC;
1036                 break;
1037             }
1038
1039             p_sys->i_state = STATE_NEXT_SYNC;
1040
1041         case STATE_NEXT_SYNC:
1042             /* TODO: If p_block == NULL, flush the buffer without checking the
1043              * next sync word */
1044             if( p_sys->bytestream.p_block == NULL )
1045             {
1046                 p_sys->i_state = STATE_NOSYNC;
1047                 block_BytestreamFlush( &p_sys->bytestream );
1048                 return NULL;
1049             }
1050
1051             /* Check if next expected frame contains the sync word */
1052             if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
1053                                        + p_sys->i_header_size, p_header, 2 )
1054                 != VLC_SUCCESS )
1055             {
1056                 /* Need more data */
1057                 return NULL;
1058             }
1059
1060             assert( (p_sys->i_type == TYPE_ADTS) || (p_sys->i_type == TYPE_LOAS) );
1061             if( ( ( p_sys->i_type == TYPE_ADTS ) &&
1062                   ( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 ) ) ||
1063                 ( ( p_sys->i_type == TYPE_LOAS ) &&
1064                   ( p_header[0] != 0x56 || (p_header[1] & 0xe0) != 0xe0 ) ) )
1065             {
1066                 msg_Dbg( p_dec, "emulated sync word "
1067                          "(no sync on following frame)" );
1068                 p_sys->i_state = STATE_NOSYNC;
1069                 block_SkipByte( &p_sys->bytestream );
1070                 break;
1071             }
1072
1073             p_sys->i_state = STATE_SEND_DATA;
1074             break;
1075
1076         case STATE_GET_DATA:
1077             /* Make sure we have enough data.
1078              * (Not useful if we went through NEXT_SYNC) */
1079             if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
1080                                  p_sys->i_header_size) != VLC_SUCCESS )
1081             {
1082                 /* Need more data */
1083                 return NULL;
1084             }
1085             p_sys->i_state = STATE_SEND_DATA;
1086
1087         case STATE_SEND_DATA:
1088             /* When we reach this point we already know we have enough
1089              * data available. */
1090
1091             p_out_buffer = block_New( p_dec, p_sys->i_frame_size );
1092             if( !p_out_buffer )
1093             {
1094                 //p_dec->b_error = VLC_TRUE;
1095                 return NULL;
1096             }
1097             p_buf = p_out_buffer->p_buffer;
1098
1099             /* Skip the ADTS/LOAS header */
1100             block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
1101
1102             if( p_sys->i_type == TYPE_ADTS )
1103             {
1104                 /* Copy the whole frame into the buffer */
1105                 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1106             }
1107             else
1108             {
1109                 assert( p_sys->i_type == TYPE_LOAS );
1110                 /* Copy the whole frame into the buffer and parse/extract it */
1111                 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1112                 p_out_buffer->i_buffer = LOASParse( p_dec, p_buf, p_sys->i_frame_size );
1113                 if( p_out_buffer->i_buffer <= 0 )
1114                 {
1115                     if( !p_sys->b_latm_cfg )
1116                         msg_Warn( p_dec, "waiting for header" );
1117
1118                     block_Release( p_out_buffer );
1119                     p_out_buffer = NULL;
1120                     p_sys->i_state = STATE_NOSYNC;
1121                     break;
1122                 }
1123             }
1124             SetupOutput( p_dec, p_out_buffer );
1125             /* Make sure we don't reuse the same pts twice */
1126             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
1127                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
1128
1129             /* So p_block doesn't get re-added several times */
1130             *pp_block = block_BytestreamPop( &p_sys->bytestream );
1131
1132             p_sys->i_state = STATE_NOSYNC;
1133
1134             return p_out_buffer;
1135         }
1136     }
1137
1138     return NULL;
1139 }
1140
1141 /*****************************************************************************
1142  * SetupBuffer:
1143  *****************************************************************************/
1144 static void SetupOutput( decoder_t *p_dec, block_t *p_block )
1145 {
1146     decoder_sys_t *p_sys = p_dec->p_sys;
1147
1148     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
1149     {
1150         msg_Info( p_dec, "AAC channels: %d samplerate: %d",
1151                   p_sys->i_channels, p_sys->i_rate );
1152
1153         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
1154         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
1155     }
1156
1157     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
1158     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
1159     p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
1160     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
1161
1162 #if 0
1163     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
1164     p_dec->fmt_out.audio.i_physical_channels =
1165         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
1166 #endif
1167
1168     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
1169
1170     p_block->i_length = aout_DateIncrement( &p_sys->end_date,
1171                             p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
1172                                 p_block->i_pts;
1173 }
1174
1175 /*****************************************************************************
1176  * ClosePacketizer: clean up the packetizer
1177  *****************************************************************************/
1178 static void ClosePacketizer( vlc_object_t *p_this )
1179 {
1180     decoder_t *p_dec = (decoder_t *)p_this;
1181     decoder_sys_t *p_sys = p_dec->p_sys;
1182
1183     block_BytestreamRelease( &p_sys->bytestream );
1184
1185     free( p_dec->p_sys );
1186 }