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