]> git.sesse.net Git - vlc/blob - modules/packetizer/mpeg4audio.c
4c5b9ca5aa698c2fc32d2ad0d56dac47c0d28603
[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     bool 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 = 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         p_dec->fmt_out.audio.i_channels =
243             (p_config[i_index == 0x0f ? 4 : 1] >> 3) & 0x0f;
244
245         msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
246                  p_dec->fmt_out.audio.i_rate,
247                  p_dec->fmt_out.audio.i_frame_length );
248
249         aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
250
251         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
252         p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
253         if( !p_dec->fmt_out.p_extra )
254         {
255             p_dec->fmt_out.i_extra = 0;
256             return VLC_ENOMEM;
257         }
258         memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
259                 p_dec->fmt_in.i_extra );
260
261         /* Set callback */
262         p_dec->pf_packetize = PacketizeRawBlock;
263         p_sys->i_type = TYPE_RAW;
264     }
265     else
266     {
267         msg_Dbg( p_dec, "no decoder specific info, must be an ADTS or LOAS stream" );
268
269         aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
270
271         /* We will try to create a AAC Config from adts/loas */
272         p_dec->fmt_out.i_extra = 0;
273         p_dec->fmt_out.p_extra = NULL;
274
275         /* Set callback */
276         p_dec->pf_packetize = PacketizeStreamBlock;
277         p_sys->i_type = TYPE_NONE;
278     }
279
280     return VLC_SUCCESS;
281 }
282
283 /****************************************************************************
284  * PacketizeRawBlock: the whole thing
285  ****************************************************************************
286  * This function must be fed with complete frames.
287  ****************************************************************************/
288 static block_t *PacketizeRawBlock( decoder_t *p_dec, block_t **pp_block )
289 {
290     decoder_sys_t *p_sys = p_dec->p_sys;
291     block_t *p_block;
292
293     if( !pp_block || !*pp_block ) return NULL;
294
295     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
296     {
297         //aout_DateSet( &p_sys->end_date, 0 );
298         block_Release( *pp_block );
299         return NULL;
300     }
301
302     p_block = *pp_block;
303     *pp_block = NULL; /* Don't reuse this block */
304
305     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
306     {
307         /* We've just started the stream, wait for the first PTS. */
308         block_Release( p_block );
309         return NULL;
310     }
311     else if( p_block->i_pts != 0 &&
312              p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
313     {
314         aout_DateSet( &p_sys->end_date, p_block->i_pts );
315     }
316
317     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
318
319     p_block->i_length = aout_DateIncrement( &p_sys->end_date,
320         p_dec->fmt_out.audio.i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) - p_block->i_pts;
321
322     return p_block;
323 }
324
325 /****************************************************************************
326  * ADTS helpers
327  ****************************************************************************/
328 static int ADTSSyncInfo( decoder_t * p_dec, const uint8_t * p_buf,
329                          unsigned int * pi_channels,
330                          unsigned int * pi_sample_rate,
331                          unsigned int * pi_frame_length,
332                          unsigned int * pi_header_size )
333 {
334     int i_profile, i_sample_rate_idx, i_frame_size;
335     bool b_crc;
336
337     /* Fixed header between frames */
338     //int i_id = ( (p_buf[1] >> 3) & 0x01) ? 2 : 4; /* MPEG-2 or 4 */
339     b_crc = !(p_buf[1] & 0x01);
340     i_profile = p_buf[2] >> 6;
341     i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
342     *pi_sample_rate = pi_sample_rates[i_sample_rate_idx];
343     //private_bit = (p_buf[2] >> 1) & 0x01;
344     *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
345     //original_copy = (p_buf[3] >> 5) & 0x01;
346     //home = (p_buf[3] >> 4) & 0x01;
347
348     /* Variable header */
349     //copyright_id_bit = (p_buf[3] >> 3) & 0x01;
350     //copyright_id_start = (p_buf[3] >> 2) & 0x01;
351     i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
352                    ((p_buf[5] >> 5) /*& 0x7*/);
353     //uint16_t buffer_fullness = ((p_buf[5] & 0x1f) << 6) | (p_buf[6] >> 2);
354     unsigned short i_raw_blocks_in_frame = p_buf[6] & 0x03;
355
356     if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
357     {
358         msg_Warn( p_dec, "Invalid ADTS header" );
359         return 0;
360     }
361
362     *pi_frame_length = 1024;
363
364     if( i_raw_blocks_in_frame == 0 )
365     {
366         if( b_crc )
367         {
368             msg_Warn( p_dec, "ADTS CRC not supported" );
369             //uint16_t crc = (p_buf[7] << 8) | p_buf[8];
370         }
371     }
372     else
373     {
374         msg_Err( p_dec, "Multiple blocks per frame in ADTS not supported" );
375         return 0;
376 #if 0
377         int i;
378         const uint8_t *p_pos = p_buf + 7;
379         uint16_t crc_block;
380         uint16_t i_block_pos[3];
381         if( b_crc )
382         {
383             for( i = 0 ; i < i_raw_blocks_in_frame ; i++ )
384             {   /* the 1st block's position is known ... */
385                 i_block_pos[i] = (*p_pos << 8) | *(p_pos+1);
386                 p_pos += 2;
387             }
388             crc_block = (*p_pos << 8) | *(p_pos+1);
389             p_pos += 2;
390         }
391         for( i = 0 ; i <= i_raw_blocks_in_frame ; i++ )
392         {
393             //read 1 block
394             if( b_crc )
395             {
396                 msg_Err( p_dec, "ADTS CRC not supported" );
397                 //uint16_t crc = (*p_pos << 8) | *(p_pos+1);
398                 //p_pos += 2;
399             }
400         }
401 #endif
402     }
403
404
405     /* Build the decoder specific info header */
406     if( !p_dec->fmt_out.i_extra )
407     {
408         p_dec->fmt_out.p_extra = malloc( 2 );
409         if( !p_dec->fmt_out.p_extra )
410         {
411             p_dec->fmt_out.i_extra = 0;
412             return 0;
413         }
414         p_dec->fmt_out.i_extra = 2;
415         ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
416             (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
417         ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
418             ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
419     }
420
421     /* ADTS header length */
422     *pi_header_size = b_crc ? 9 : 7;
423
424     return i_frame_size - *pi_header_size;
425 }
426
427 /****************************************************************************
428  * LOAS helpers
429  ****************************************************************************/
430 static int LOASSyncInfo( uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_header_size )
431 {
432     *pi_header_size = 3;
433     return ( ( p_header[1] & 0x1f ) << 8 ) + p_header[2];
434 }
435
436 static int Mpeg4GAProgramConfigElement( bs_t *s )
437 {
438     /* TODO compute channels count ? */
439     int i_tag = bs_read( s, 4 );
440     if( i_tag != 0x05 )
441         return -1;
442     bs_skip( s, 2 + 4 ); // object type + sampling index
443     int i_num_front = bs_read( s, 4 );
444     int i_num_side = bs_read( s, 4 );
445     int i_num_back = bs_read( s, 4 );
446     int i_num_lfe = bs_read( s, 2 );
447     int i_num_assoc_data = bs_read( s, 3 );
448     int i_num_valid_cc = bs_read( s, 4 );
449
450     if( bs_read1(s) )
451         bs_skip( s, 4 ); // mono downmix
452     if( bs_read1(s) )
453         bs_skip( s, 4 ); // stereo downmix
454     if( bs_read1(s) )
455         bs_skip( s, 2+1 ); // matrix downmix + pseudo_surround
456
457     bs_skip( s, i_num_front * (1+4) );
458     bs_skip( s, i_num_side * (1+4) );
459     bs_skip( s, i_num_back * (1+4) );
460     bs_skip( s, i_num_lfe * (4) );
461     bs_skip( s, i_num_assoc_data * (4) );
462     bs_skip( s, i_num_valid_cc * (5) );
463     bs_align( s );
464     int i_comment = bs_read( s, 8 );
465     bs_skip( s, i_comment * 8 );
466     return 0;
467 }
468
469 static int Mpeg4GASpecificConfig( mpeg4_cfg_t *p_cfg, bs_t *s )
470 {
471     p_cfg->i_frame_length = bs_read1(s) ? 960 : 1024;
472
473     if( bs_read1( s ) )     // depend on core coder
474         bs_skip( s, 14 );   // core coder delay
475
476     int i_extension_flag = bs_read1( s );
477     if( p_cfg->i_channel == 0 )
478     {
479         Mpeg4GAProgramConfigElement( s );
480     }
481     if( p_cfg->i_object_type == 6 || p_cfg->i_object_type == 20 )
482         bs_skip( s, 3 );    // layer
483
484     if( i_extension_flag )
485     {
486         if( p_cfg->i_object_type == 22 )
487         {
488             bs_skip( s, 5 + 11 );   // numOfSubFrame + layer length
489         }
490         if( p_cfg->i_object_type == 17 || p_cfg->i_object_type == 19 ||
491             p_cfg->i_object_type == 20 || p_cfg->i_object_type == 23 )
492         {
493             bs_skip( s, 1+1+1 );    // ER data : section scale spectral */
494         }
495         if( bs_read1( s ) )     // extension 3
496             fprintf( stderr, "Mpeg4GASpecificConfig: error 1\n" );
497     }
498     return 0;
499 }
500
501 static int Mpeg4ReadAudioObjectType( bs_t *s )
502 {
503     int i_type = bs_read( s, 5 );
504     if( i_type == 0x1f )
505         i_type += bs_read( s, 6 );
506     return i_type;
507 }
508
509 static int Mpeg4ReadAudioSamplerate( bs_t *s )
510 {
511     int i_index = bs_read( s, 4 );
512     if( i_index != 0x0f )
513         return pi_sample_rates[i_index];
514     return bs_read( s, 24 );
515 }
516
517 static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_t *p_extra, bs_t *s, int i_max_size )
518 {
519 #if 0
520     static const char *ppsz_otype[] = {
521         "NULL",
522         "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
523         "TwinVQ",
524         "CELP", "HVXC",
525         "Reserved", "Reserved",
526         "TTSI",
527         "Main Synthetic", "Wavetables Synthesis", "General MIDI",
528         "Algorithmic Synthesis and Audio FX",
529         "ER AAC LC",
530         "Reserved",
531         "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
532         "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
533         "SSC",
534         "Reserved", "Reserved", "Escape",
535         "Layer 1", "Layer 2", "Layer 3",
536         "DST",
537     };
538 #endif
539     const int i_pos_start = bs_pos( s );
540     bs_t s_sav = *s;
541     int i_bits;
542     int i;
543
544     memset( p_cfg, 0, sizeof(*p_cfg) );
545     *pi_extra = 0;
546
547     p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
548     p_cfg->i_samplerate = Mpeg4ReadAudioSamplerate( s );
549
550     p_cfg->i_channel = bs_read( s, 4 );
551     if( p_cfg->i_channel == 7 )
552         p_cfg->i_channel = 8; // 7.1
553     else if( p_cfg->i_channel >= 8 )
554         p_cfg->i_channel = -1;
555
556     p_cfg->i_sbr = -1;
557     p_cfg->extension.i_object_type = 0;
558     p_cfg->extension.i_samplerate = 0;
559     if( p_cfg->i_object_type == 5 )
560     {
561         p_cfg->i_sbr = 1;
562         p_cfg->extension.i_object_type = p_cfg->i_object_type;
563         p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
564
565         p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
566     }
567
568     switch( p_cfg->i_object_type )
569     {
570     case 1: case 2: case 3: case 4:
571     case 6: case 7:
572     case 17: case 19: case 20: case 21: case 22: case 23:
573         Mpeg4GASpecificConfig( p_cfg, s );
574         break;
575     case 8:
576         // CelpSpecificConfig();
577         break;
578     case 9:
579         // HvxcSpecificConfig();
580         break;
581     case 12:
582         // TTSSSpecificConfig();
583         break;
584     case 13: case 14: case 15: case 16:
585         // StructuredAudioSpecificConfig();
586         break;
587     case 24:
588         // ERCelpSpecificConfig();
589         break;
590     case 25:
591         // ERHvxcSpecificConfig();
592         break;
593     case 26: case 27:
594         // ParametricSpecificConfig();
595         break;
596     case 28:
597         // SSCSpecificConfig();
598         break;
599     case 32: case 33: case 34:
600         // MPEG_1_2_SpecificConfig();
601         break;
602     case 35:
603         // DSTSpecificConfig();
604         break;
605     default:
606         // error
607         break;
608     }
609     switch( p_cfg->i_object_type )
610     {
611     case 17: case 19: case 20: case 21: case 22: case 23:
612     case 24: case 25: case 26: case 27:
613     {
614         int epConfig = bs_read( s, 2 );
615         if( epConfig == 2 || epConfig == 3 )
616         {
617             //ErrorProtectionSpecificConfig();
618         }
619         if( epConfig == 3 )
620         {
621             int directMapping = bs_read1( s );
622             if( directMapping )
623             {
624                 // tbd ...
625             }
626         }
627         break;
628     }
629     default:
630         break;
631     }
632
633     if( p_cfg->extension.i_object_type != 5 && i_max_size > 0 && i_max_size - (bs_pos(s) - i_pos_start) >= 16 && 
634         bs_read( s, 11 ) == 0x2b7 )
635     {
636         p_cfg->extension.i_object_type = Mpeg4ReadAudioObjectType( s );
637         if( p_cfg->extension.i_object_type == 5 )
638         {
639             p_cfg->i_sbr  = bs_read1( s );
640             if( p_cfg->i_sbr == 1 )
641                 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
642         }
643     }
644
645     //fprintf( stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
646     //         ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr );
647
648     i_bits = bs_pos(s) - i_pos_start;
649
650     *pi_extra = ( i_bits + 7 ) / 8;
651     for( i = 0; i < __MIN( LATM_MAX_EXTRA_SIZE, *pi_extra ); i++ )
652     {
653         const int i_read = __MIN( 8, i_bits - 8*i );
654         p_extra[i] = bs_read( &s_sav, i_read ) << (8-i_read);
655     }
656     return i_bits;
657 }
658
659 static int LatmGetValue( bs_t *s )
660 {
661     int i_bytes = bs_read( s, 2 );
662     int v = 0;
663     int i;
664     for( i = 0; i < i_bytes; i++ )
665         v = (v << 8) + bs_read( s, 8 );
666
667     return v;
668 }
669
670 static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
671 {
672     int i_mux_version;
673     int i_mux_versionA;
674     int i_program;
675
676     i_mux_version = bs_read( s, 1 );
677     i_mux_versionA = 0;
678     if( i_mux_version )
679         i_mux_versionA = bs_read( s, 1 );
680
681     if( i_mux_versionA != 0 ) /* support only A=0 */
682         return -1;
683
684     memset( m, 0, sizeof(*m) );
685
686     if( i_mux_versionA == 0 )
687     {
688         if( i_mux_version == 1 )
689         {
690             LatmGetValue(s); /* taraBufferFullness */
691         }
692     }
693
694     m->b_same_time_framing = bs_read1( s );
695     m->i_sub_frames = 1 + bs_read( s, 6 );
696     m->i_programs = 1 + bs_read( s, 4 );
697
698     for( i_program = 0; i_program < m->i_programs; i_program++ )
699     {
700         int i_layer;
701
702         m->pi_layers[i_program] = 1+bs_read( s, 3 );
703
704         for( i_layer = 0; i_layer < m->pi_layers[i_program]; i_layer++ )
705         {
706             latm_stream_t *st = &m->stream[m->i_streams];
707             bool b_previous_cfg;
708
709             m->pi_stream[i_program][i_layer] = m->i_streams;
710             st->i_program = i_program;
711             st->i_layer = i_layer;
712
713             b_previous_cfg = false;
714             if( i_program != 0 || i_layer != 0 )
715                 b_previous_cfg = bs_read1( s );
716
717             if( b_previous_cfg )
718             {
719                 assert( m->i_streams > 0 );
720                 st->cfg = m->stream[m->i_streams-1].cfg;
721             }
722             else
723             {
724                 int i_cfg_size = 0;
725                 if( i_mux_version == 1 )
726                     i_cfg_size = LatmGetValue(s);
727                 i_cfg_size -= Mpeg4ReadAudioSpecificInfo( &st->cfg, &st->i_extra, st->extra, s, i_cfg_size );
728                 if( i_cfg_size > 0 )
729                     bs_skip( s, i_cfg_size );
730             }
731
732             st->i_frame_length_type = bs_read( s, 3 );
733             switch( st->i_frame_length_type )
734             {
735             case 0:
736             {
737                 bs_skip( s, 8 ); /* latmBufferFullnes */
738                 if( !m->b_same_time_framing )
739                 {
740                     if( st->cfg.i_object_type == 6 || st->cfg.i_object_type == 20 ||
741                         st->cfg.i_object_type == 8 || st->cfg.i_object_type == 24 )
742                     {
743                         bs_skip( s, 6 ); /* eFrameOffset */
744                     }
745                 }
746                 break;
747             }
748             case 1:
749                 st->i_frame_length = bs_read( s, 9 );
750                 break;
751             case 3: case 4: case 5:
752                 st->i_frame_length_index = bs_read( s, 6 ); // celp
753                 break;
754             case 6: case 7:
755                 st->i_frame_length_index = bs_read( s, 1 ); // hvxc
756             default:
757                 break;
758             }
759             /* Next stream */
760             m->i_streams++;
761         }
762     }
763
764     /* other data */
765     if( bs_read1( s ) )
766     {
767         if( i_mux_version == 1 )
768         {
769             m->i_other_data = LatmGetValue( s );
770         }
771         else
772         {
773             int b_continue;
774             do {
775                 b_continue = bs_read1(s);
776                 m->i_other_data = (m->i_other_data << 8) + bs_read( s, 8 );
777             } while( b_continue );
778         }
779     }
780
781     /* crc */
782     m->i_crc = -1;
783     if( bs_read1( s ) )
784         m->i_crc = bs_read( s, 8 );
785
786     return 0;
787 }
788
789 static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
790 {
791     decoder_sys_t *p_sys = p_dec->p_sys;
792     bs_t s;
793     int i_sub;
794     int i_accumulated = 0;
795
796     bs_init( &s, p_buffer, i_buffer );
797
798     /* Read the stream mux configuration if present */
799     if( !bs_read1( &s ) )
800     {
801         if( !LatmReadStreamMuxConfiguration( &p_sys->latm, &s ) &&
802             p_sys->latm.i_streams > 0 )
803         {
804             const latm_stream_t *st = &p_sys->latm.stream[0];
805
806             p_sys->i_channels = st->cfg.i_channel;
807             p_sys->i_rate = st->cfg.i_samplerate;
808             p_sys->i_frame_length = st->cfg.i_frame_length;
809
810             /* FIXME And if it changes ? */
811             if( !p_dec->fmt_out.i_extra && st->i_extra > 0 )
812             {
813                 p_dec->fmt_out.i_extra = st->i_extra;
814                 p_dec->fmt_out.p_extra = malloc( st->i_extra );
815                 if( !p_dec->fmt_out.p_extra )
816                 {
817                     p_dec->fmt_out.i_extra = 0;
818                     return 0;
819                 }
820                 memcpy( p_dec->fmt_out.p_extra, st->extra, st->i_extra );
821             }
822
823             p_sys->b_latm_cfg = true;
824         }
825     }
826     /* Wait for the configuration */
827     if( !p_sys->b_latm_cfg )
828         return 0;
829
830     /* FIXME do we need to split the subframe into independant packet ? */
831     if( p_sys->latm.i_sub_frames > 1 )
832         msg_Err( p_dec, "latm sub frames not yet supported, please send a sample" );
833
834     for( i_sub = 0; i_sub < p_sys->latm.i_sub_frames; i_sub++ )
835     {
836         int pi_payload[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
837         if( p_sys->latm.b_same_time_framing )
838         {
839             int i_program;
840             /* Payload length */
841             for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
842             {
843                 int i_layer;
844                 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
845                 {
846                     latm_stream_t *st = &p_sys->latm.stream[p_sys->latm.pi_stream[i_program][i_layer]];
847                     if( st->i_frame_length_type == 0 )
848                     {
849                         int i_payload = 0;
850                         for( ;; )
851                         {
852                             int i_tmp = bs_read( &s, 8 );
853                             i_payload += i_tmp;
854                             if( i_tmp != 255 )
855                                 break;
856                         }
857                         pi_payload[i_program][i_layer] = i_payload;
858                     }
859                     else if( st->i_frame_length_type == 1 )
860                     {
861                         pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
862                     }
863                     else if( ( st->i_frame_length_type == 3 ) ||
864                              ( st->i_frame_length_type == 5 ) ||
865                              ( st->i_frame_length_type == 7 ) )
866                     {
867                         bs_skip( &s, 2 ); // muxSlotLengthCoded
868                         pi_payload[i_program][i_layer] = 0; /* TODO */
869                     }
870                     else
871                     {
872                         pi_payload[i_program][i_layer] = 0; /* TODO */
873                     }
874                 }
875             }
876             /* Payload Data */
877             for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
878             {
879                 int i_layer;
880                 int i;
881                 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
882                 {
883                     /* XXX we only extract 1 stream */
884                     if( i_program != 0 || i_layer != 0 )
885                         break;
886
887                     if( pi_payload[i_program][i_layer] <= 0 )
888                         continue;
889
890                     /* FIXME that's slow (and a bit ugly to write in place) */
891                     for( i = 0; i < pi_payload[i_program][i_layer]; i++ )
892                         p_buffer[i_accumulated++] = bs_read( &s, 8 );
893                 }
894             }
895         }
896         else
897         {
898             const int i_chunks = bs_read( &s, 4 );
899             int pi_program[16];
900             int pi_layer[16];
901             int i_chunk;
902
903             msg_Err( p_dec, "latm without same time frameing not yet supported, please send a sample" );
904
905             for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
906             {
907                 const int streamIndex = bs_read( &s, 4 );
908                 latm_stream_t *st = &p_sys->latm.stream[streamIndex];
909                 const int i_program = st->i_program;
910                 const int i_layer = st->i_layer;
911
912                 pi_program[i_chunk] = i_program;
913                 pi_layer[i_chunk] = i_layer;
914
915                 if( st->i_frame_length_type == 0 )
916                 {
917                     int i_payload = 0;
918                     for( ;; )
919                     {
920                         int i_tmp = bs_read( &s, 8 );
921                         i_payload += i_tmp;
922                         if( i_tmp != 255 )
923                             break;
924                     }
925                     pi_payload[i_program][i_layer] = i_payload;
926                     bs_skip( &s, 1 ); // auEndFlag
927                 }
928                 else if( st->i_frame_length_type == 1 )
929                 {
930                     pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
931                 }
932                 else if( ( st->i_frame_length_type == 3 ) ||
933                          ( st->i_frame_length_type == 5 ) ||
934                          ( st->i_frame_length_type == 7 ) )
935                 {
936                     bs_read( &s, 2 ); // muxSlotLengthCoded
937                 }
938                 else
939                 {
940                 }
941             }
942             for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
943             {
944                 //const int i_program = pi_program[i_chunk];
945                 //const int i_layer = pi_layer[i_chunk];
946
947                 /* TODO ? Payload */
948             }
949         }
950     }
951
952     if( p_sys->latm.i_other_data > 0 )
953     {
954         /* Other data XXX we just ignore them */
955     }
956     bs_align( &s );
957
958     return i_accumulated;
959 }
960
961 /****************************************************************************
962  * PacketizeStreamBlock: ADTS/LOAS packetizer
963  ****************************************************************************/
964 static void SetupOutput( decoder_t *p_dec, block_t *p_block );
965 static block_t *PacketizeStreamBlock( decoder_t *p_dec, block_t **pp_block )
966 {
967     decoder_sys_t *p_sys = p_dec->p_sys;
968     uint8_t p_header[ADTS_HEADER_SIZE + LOAS_HEADER_SIZE];
969     block_t *p_out_buffer;
970     uint8_t *p_buf;
971
972     if( !pp_block || !*pp_block ) return NULL;
973
974     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
975     {
976         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
977         {
978             p_sys->i_state = STATE_NOSYNC;
979             block_BytestreamFlush( &p_sys->bytestream );
980         }
981         //aout_DateSet( &p_sys->end_date, 0 );
982         block_Release( *pp_block );
983         return NULL;
984     }
985
986     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
987     {
988         /* We've just started the stream, wait for the first PTS. */
989         block_Release( *pp_block );
990         return NULL;
991     }
992
993     if( (*pp_block)->i_rate > 0 )
994         p_sys->i_input_rate = (*pp_block)->i_rate;
995
996     block_BytestreamPush( &p_sys->bytestream, *pp_block );
997
998     for( ;; )
999     {
1000         switch( p_sys->i_state )
1001         {
1002
1003         case STATE_NOSYNC:
1004             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
1005                    == VLC_SUCCESS )
1006             {
1007                 /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
1008                 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
1009                 {
1010                     if( p_sys->i_type != TYPE_ADTS )
1011                         msg_Dbg( p_dec, "detected ADTS format" );
1012
1013                     p_sys->i_state = STATE_SYNC;
1014                     p_sys->i_type = TYPE_ADTS;
1015                     break;
1016                 }
1017                 else if( p_header[0] == 0x56 && (p_header[1] & 0xe0) == 0xe0 )
1018                 {
1019                     if( p_sys->i_type != TYPE_LOAS )
1020                         msg_Dbg( p_dec, "detected LOAS format" );
1021
1022                     p_sys->i_state = STATE_SYNC;
1023                     p_sys->i_type = TYPE_LOAS;
1024                     break;
1025                 }
1026                 block_SkipByte( &p_sys->bytestream );
1027             }
1028             if( p_sys->i_state != STATE_SYNC )
1029             {
1030                 block_BytestreamFlush( &p_sys->bytestream );
1031
1032                 /* Need more data */
1033                 return NULL;
1034             }
1035
1036         case STATE_SYNC:
1037             /* New frame, set the Presentation Time Stamp */
1038             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
1039             if( p_sys->i_pts != 0 &&
1040                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
1041             {
1042                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
1043             }
1044             p_sys->i_state = STATE_HEADER;
1045             break;
1046
1047         case STATE_HEADER:
1048             if( p_sys->i_type == TYPE_ADTS )
1049             {
1050                 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
1051                 if( block_PeekBytes( &p_sys->bytestream, p_header,
1052                                      ADTS_HEADER_SIZE ) != VLC_SUCCESS )
1053                 {
1054                     /* Need more data */
1055                     return NULL;
1056                 }
1057
1058                 /* Check if frame is valid and get frame info */
1059                 p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
1060                                                     &p_sys->i_channels,
1061                                                     &p_sys->i_rate,
1062                                                     &p_sys->i_frame_length,
1063                                                     &p_sys->i_header_size );
1064             }
1065             else
1066             {
1067                 assert( p_sys->i_type == TYPE_LOAS );
1068                 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
1069                 if( block_PeekBytes( &p_sys->bytestream, p_header,
1070                                      LOAS_HEADER_SIZE ) != VLC_SUCCESS )
1071                 {
1072                     /* Need more data */
1073                     return NULL;
1074                 }
1075
1076                 /* Check if frame is valid and get frame info */
1077                 p_sys->i_frame_size = LOASSyncInfo( p_header, &p_sys->i_header_size );
1078             }
1079
1080             if( p_sys->i_frame_size <= 0 )
1081             {
1082                 msg_Dbg( p_dec, "emulated sync word" );
1083                 block_SkipByte( &p_sys->bytestream );
1084                 p_sys->i_state = STATE_NOSYNC;
1085                 break;
1086             }
1087
1088             p_sys->i_state = STATE_NEXT_SYNC;
1089
1090         case STATE_NEXT_SYNC:
1091             /* TODO: If p_block == NULL, flush the buffer without checking the
1092              * next sync word */
1093             if( p_sys->bytestream.p_block == NULL )
1094             {
1095                 p_sys->i_state = STATE_NOSYNC;
1096                 block_BytestreamFlush( &p_sys->bytestream );
1097                 return NULL;
1098             }
1099
1100             /* Check if next expected frame contains the sync word */
1101             if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
1102                                        + p_sys->i_header_size, p_header, 2 )
1103                 != VLC_SUCCESS )
1104             {
1105                 /* Need more data */
1106                 return NULL;
1107             }
1108
1109             assert( (p_sys->i_type == TYPE_ADTS) || (p_sys->i_type == TYPE_LOAS) );
1110             if( ( ( p_sys->i_type == TYPE_ADTS ) &&
1111                   ( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 ) ) ||
1112                 ( ( p_sys->i_type == TYPE_LOAS ) &&
1113                   ( p_header[0] != 0x56 || (p_header[1] & 0xe0) != 0xe0 ) ) )
1114             {
1115                 msg_Dbg( p_dec, "emulated sync word "
1116                          "(no sync on following frame)" );
1117                 p_sys->i_state = STATE_NOSYNC;
1118                 block_SkipByte( &p_sys->bytestream );
1119                 break;
1120             }
1121
1122             p_sys->i_state = STATE_SEND_DATA;
1123             break;
1124
1125         case STATE_GET_DATA:
1126             /* Make sure we have enough data.
1127              * (Not useful if we went through NEXT_SYNC) */
1128             if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
1129                                  p_sys->i_header_size) != VLC_SUCCESS )
1130             {
1131                 /* Need more data */
1132                 return NULL;
1133             }
1134             p_sys->i_state = STATE_SEND_DATA;
1135
1136         case STATE_SEND_DATA:
1137             /* When we reach this point we already know we have enough
1138              * data available. */
1139
1140             p_out_buffer = block_New( p_dec, p_sys->i_frame_size );
1141             if( !p_out_buffer )
1142             {
1143                 //p_dec->b_error = true;
1144                 return NULL;
1145             }
1146             p_buf = p_out_buffer->p_buffer;
1147
1148             /* Skip the ADTS/LOAS header */
1149             block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
1150
1151             if( p_sys->i_type == TYPE_ADTS )
1152             {
1153                 /* Copy the whole frame into the buffer */
1154                 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1155             }
1156             else
1157             {
1158                 assert( p_sys->i_type == TYPE_LOAS );
1159                 /* Copy the whole frame into the buffer and parse/extract it */
1160                 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1161                 p_out_buffer->i_buffer = LOASParse( p_dec, p_buf, p_sys->i_frame_size );
1162                 if( p_out_buffer->i_buffer <= 0 )
1163                 {
1164                     if( !p_sys->b_latm_cfg )
1165                         msg_Warn( p_dec, "waiting for header" );
1166
1167                     block_Release( p_out_buffer );
1168                     p_out_buffer = NULL;
1169                     p_sys->i_state = STATE_NOSYNC;
1170                     break;
1171                 }
1172             }
1173             SetupOutput( p_dec, p_out_buffer );
1174             /* Make sure we don't reuse the same pts twice */
1175             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
1176                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
1177
1178             /* So p_block doesn't get re-added several times */
1179             *pp_block = block_BytestreamPop( &p_sys->bytestream );
1180
1181             p_sys->i_state = STATE_NOSYNC;
1182
1183             return p_out_buffer;
1184         }
1185     }
1186
1187     return NULL;
1188 }
1189
1190 /*****************************************************************************
1191  * SetupBuffer:
1192  *****************************************************************************/
1193 static void SetupOutput( decoder_t *p_dec, block_t *p_block )
1194 {
1195     decoder_sys_t *p_sys = p_dec->p_sys;
1196
1197     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
1198     {
1199         msg_Info( p_dec, "AAC channels: %d samplerate: %d",
1200                   p_sys->i_channels, p_sys->i_rate );
1201
1202         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
1203         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
1204     }
1205
1206     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
1207     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
1208     p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
1209     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
1210
1211 #if 0
1212     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
1213     p_dec->fmt_out.audio.i_physical_channels =
1214         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
1215 #endif
1216
1217     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
1218
1219     p_block->i_length = aout_DateIncrement( &p_sys->end_date,
1220                             p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
1221                                 p_block->i_pts;
1222 }
1223
1224 /*****************************************************************************
1225  * ClosePacketizer: clean up the packetizer
1226  *****************************************************************************/
1227 static void ClosePacketizer( vlc_object_t *p_this )
1228 {
1229     decoder_t *p_dec = (decoder_t *)p_this;
1230     decoder_sys_t *p_sys = p_dec->p_sys;
1231
1232     block_BytestreamRelease( &p_sys->bytestream );
1233
1234     free( p_dec->p_sys );
1235 }