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