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