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