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