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