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