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