demux: libmp4: add and parse 3DDS uuid
[vlc.git] / modules / packetizer / mpeg4audio.c
bloba6391230421fa20310f53fd37e398120d1d000c8
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$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
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>
39 #include <vlc_block_helper.h>
40 #include "packetizer_helper.h"
42 #include <assert.h>
44 /* AAC Config in ES:
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)
57 /*****************************************************************************
58 * decoder_sys_t : decoder descriptor
59 *****************************************************************************/
60 typedef struct
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
68 struct
70 int i_object_type;
71 int i_samplerate;
72 int i_channel;
73 } extension;
75 /* GASpecific */
76 int i_frame_length; // 1024 or 960
78 } mpeg4_asc_t;
80 #define LATM_MAX_EXTRA_SIZE 64
81 typedef struct
83 int i_program;
84 int i_layer;
86 int i_frame_length_type;
87 int i_frame_length; // type 1
88 int i_frame_length_index; // type 3 4 5 6 7
90 mpeg4_asc_t cfg;
92 /* Raw configuration */
93 int i_extra;
94 uint8_t extra[LATM_MAX_EXTRA_SIZE];
96 } latm_stream_t;
98 #define LATM_MAX_LAYER (8)
99 #define LATM_MAX_PROGRAM (16)
100 typedef struct
102 int b_same_time_framing;
103 int i_sub_frames;
104 int i_programs;
106 int pi_layers[LATM_MAX_PROGRAM];
108 int pi_stream[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
110 int i_streams;
111 latm_stream_t stream[LATM_MAX_PROGRAM*LATM_MAX_LAYER];
113 uint32_t i_other_data;
114 int i_crc; /* -1 if not set */
115 } latm_mux_t;
117 struct decoder_sys_t
120 * Input properties
122 int i_state;
123 int i_type;
125 block_bytestream_t bytestream;
128 * Common properties
130 date_t end_date;
131 mtime_t i_pts;
132 bool b_discontuinity;
134 int i_frame_size;
135 unsigned int i_channels;
136 unsigned int i_rate, i_frame_length, i_header_size;
138 int i_input_rate;
140 /* LOAS */
141 bool b_latm_cfg;
142 latm_mux_t latm;
144 int i_warnings;
147 enum
149 WARN_CRC_UNSUPPORTED = 1
152 #define WARN_ONCE(warn, msg) do{\
153 if( (p_dec->p_sys->i_warnings & warn) == 0 )\
155 p_dec->p_sys->i_warnings |= warn;\
156 msg_Warn( p_dec, msg );\
158 } while(0)
160 enum {
161 TYPE_UNKNOWN, /* AAC samples with[out] headers */
162 TYPE_UNKNOWN_NONRAW, /* [un]packetized ADTS or LOAS */
163 TYPE_RAW, /* RAW AAC frames */
164 TYPE_ADTS,
165 TYPE_LOAS
168 static const int pi_sample_rates[16] =
170 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
171 16000, 12000, 11025, 8000, 7350, 0, 0, 0
174 #define ADTS_HEADER_SIZE 9
175 #define LOAS_HEADER_SIZE 3
177 /****************************************************************************
178 * Local prototypes
179 ****************************************************************************/
180 static int OpenPacketizer(vlc_object_t *);
181 static void ClosePacketizer(vlc_object_t *);
183 static block_t *Packetize (decoder_t *, block_t **);
184 static void Flush( decoder_t * );
186 static int Mpeg4ReadAudioSpecificConfig(bs_t *s, mpeg4_asc_t *p_cfg, bool);
188 /*****************************************************************************
189 * Module descriptor
190 *****************************************************************************/
191 vlc_module_begin ()
192 set_category(CAT_SOUT)
193 set_subcategory(SUBCAT_SOUT_PACKETIZER)
194 set_description(N_("MPEG4 audio packetizer"))
195 set_capability("packetizer", 50)
196 set_callbacks(OpenPacketizer, ClosePacketizer)
197 vlc_module_end ()
199 /*****************************************************************************
200 * OpenPacketizer: probe the packetizer and return score
201 *****************************************************************************/
202 static int OpenPacketizer(vlc_object_t *p_this)
204 decoder_t *p_dec = (decoder_t*)p_this;
205 decoder_sys_t *p_sys;
207 if (p_dec->fmt_in.i_codec != VLC_CODEC_MP4A)
208 return VLC_EGENERIC;
210 /* Allocate the memory needed to store the decoder's structure */
211 if ((p_dec->p_sys = p_sys = (decoder_sys_t *)malloc(sizeof(decoder_sys_t))) == NULL)
212 return VLC_ENOMEM;
214 /* Misc init */
215 p_sys->i_state = STATE_NOSYNC;
216 p_sys->b_discontuinity = false;
217 date_Set(&p_sys->end_date, 0);
218 block_BytestreamInit(&p_sys->bytestream);
219 p_sys->b_latm_cfg = false;
220 p_sys->i_warnings = 0;
222 /* Set output properties */
223 p_dec->fmt_out.i_codec = VLC_CODEC_MP4A;
225 msg_Dbg(p_dec, "running MPEG4 audio packetizer");
228 * We need to handle 3 cases.
229 * Case 1 : RAW AAC samples without sync header
230 * The demuxer shouldn't need packetizer, see next case.
231 * Case 2 : AAC samples with ADTS or LOAS/LATM header
232 * Some mux (avi) can't distinguish the both
233 * cases above, and then forwards to packetizer
234 * which should check for header and rewire to case below
235 * Case 3 : Non packetized ADTS or LOAS/LATM
236 * The demuxer needs to set original_codec for hardwiring
239 switch (p_dec->fmt_in.i_original_fourcc)
241 case VLC_FOURCC('L','A','T','M'):
242 p_sys->i_type = TYPE_LOAS;
243 msg_Dbg(p_dec, "LOAS/LATM Mode");
244 break;
246 case VLC_FOURCC('A','D','T','S'):
247 p_sys->i_type = TYPE_ADTS;
248 msg_Dbg(p_dec, "ADTS Mode");
249 break;
251 case VLC_FOURCC('H','E','A','D'):
252 p_sys->i_type = TYPE_UNKNOWN_NONRAW;
253 break;
255 default:
256 p_sys->i_type = TYPE_UNKNOWN;
257 break;
260 /* Some mux (avi) do send RAW AAC without extradata,
261 and LATM can be sent with out-of-band audioconfig,
262 (avformat sets m4a extradata in both cases)
263 so we can't rely on extradata to guess multiplexing */
264 p_dec->fmt_out.audio.i_rate = p_dec->fmt_in.audio.i_rate;
266 if(p_dec->fmt_in.i_extra)
268 mpeg4_asc_t asc;
269 bs_t s;
270 bs_init(&s, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra);
271 if(Mpeg4ReadAudioSpecificConfig(&s, &asc, true) == VLC_SUCCESS)
273 p_dec->fmt_out.audio.i_rate = asc.i_samplerate;
274 p_dec->fmt_out.audio.i_frame_length = asc.i_frame_length;
275 p_dec->fmt_out.audio.i_channels = asc.i_channel;
277 msg_Dbg(p_dec, "%sAAC%s %dHz %d samples/frame",
278 (asc.i_sbr) ? "HE-" : "",
279 (asc.i_ps) ? "v2" : "",
280 (asc.i_sbr) ? p_dec->fmt_out.audio.i_rate << 1
281 : p_dec->fmt_out.audio.i_rate,
282 p_dec->fmt_out.audio.i_frame_length);
285 p_dec->fmt_out.p_extra = malloc(p_dec->fmt_in.i_extra);
286 if (!p_dec->fmt_out.p_extra)
287 return VLC_ENOMEM;
288 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
289 memcpy(p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
290 p_dec->fmt_in.i_extra);
292 /* else() We will try to create a AAC Config from adts/loas */
294 date_Init(&p_sys->end_date, p_dec->fmt_out.audio.i_rate ?
295 p_dec->fmt_out.audio.i_rate : 48000, 1);
297 /* Set callbacks */
298 p_dec->pf_packetize = Packetize;
299 p_dec->pf_flush = Flush;
301 return VLC_SUCCESS;
304 /*****************************************************************************
305 * ClosePacketizer: clean up the packetizer
306 *****************************************************************************/
307 static void ClosePacketizer(vlc_object_t *p_this)
309 decoder_t *p_dec = (decoder_t *)p_this;
310 decoder_sys_t *p_sys = p_dec->p_sys;
312 block_BytestreamRelease(&p_sys->bytestream);
313 free(p_sys);
316 /****************************************************************************
317 * ForwardRawBlock:
318 ****************************************************************************
319 * This function must be fed with complete frames.
320 ****************************************************************************/
321 static block_t *ForwardRawBlock(decoder_t *p_dec, block_t **pp_block)
323 decoder_sys_t *p_sys = p_dec->p_sys;
324 block_t *p_block;
326 if (!pp_block || !*pp_block)
327 return NULL;
329 p_block = *pp_block;
330 *pp_block = NULL; /* Don't reuse this block */
332 if (p_block->i_pts > VLC_TS_INVALID &&
333 p_block->i_pts != date_Get(&p_sys->end_date))
335 if(date_Get(&p_sys->end_date) > VLC_TS_INVALID)
336 p_sys->b_discontuinity = true;
337 date_Set(&p_sys->end_date, p_block->i_pts);
340 p_block->i_pts = p_block->i_dts = date_Get(&p_sys->end_date);
342 /* Might not be known due to missing extradata,
343 will be set to block pts above */
344 if(p_dec->fmt_out.audio.i_frame_length)
345 p_block->i_length = date_Increment(&p_sys->end_date,
346 p_dec->fmt_out.audio.i_frame_length) - p_block->i_pts;
348 return p_block;
351 /****************************************************************************
352 * ADTS helpers
353 ****************************************************************************/
354 static int ADTSSyncInfo(decoder_t * p_dec, const uint8_t * p_buf,
355 unsigned int * pi_channels,
356 unsigned int * pi_sample_rate,
357 unsigned int * pi_frame_length,
358 unsigned int * pi_header_size)
360 int i_profile, i_sample_rate_idx, i_frame_size;
361 bool b_crc;
363 /* Fixed header between frames */
364 //int i_id = ((p_buf[1] >> 3) & 0x01) ? 2 : 4; /* MPEG-2 or 4 */
365 b_crc = !(p_buf[1] & 0x01);
366 i_profile = p_buf[2] >> 6;
367 i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
368 *pi_sample_rate = pi_sample_rates[i_sample_rate_idx];
369 //private_bit = (p_buf[2] >> 1) & 0x01;
370 *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
371 if (*pi_channels == 0) /* workaround broken streams */
372 *pi_channels = 2;
373 //original_copy = (p_buf[3] >> 5) & 0x01;
374 //home = (p_buf[3] >> 4) & 0x01;
376 /* Variable header */
377 //copyright_id_bit = (p_buf[3] >> 3) & 0x01;
378 //copyright_id_start = (p_buf[3] >> 2) & 0x01;
379 i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
380 ((p_buf[5] >> 5) /*& 0x7*/);
381 //uint16_t buffer_fullness = ((p_buf[5] & 0x1f) << 6) | (p_buf[6] >> 2);
382 unsigned short i_raw_blocks_in_frame = p_buf[6] & 0x03;
384 if (!*pi_sample_rate || !i_frame_size) {
385 msg_Warn(p_dec, "Invalid ADTS header");
386 return 0;
389 *pi_frame_length = 1024;
391 if (i_raw_blocks_in_frame == 0) {
392 if (b_crc) {
393 WARN_ONCE(WARN_CRC_UNSUPPORTED, "ADTS CRC not supported");
394 //uint16_t crc = (p_buf[7] << 8) | p_buf[8];
396 } else {
397 msg_Err(p_dec, "Multiple blocks per frame in ADTS not supported");
398 return 0;
399 #if 0
400 int i;
401 const uint8_t *p_pos = p_buf + 7;
402 uint16_t crc_block;
403 uint16_t i_block_pos[3];
404 if (b_crc) {
405 for (i = 0 ; i < i_raw_blocks_in_frame ; i++) {
406 /* the 1st block's position is known ... */
407 i_block_pos[i] = (*p_pos << 8) | *(p_pos+1);
408 p_pos += 2;
410 crc_block = (*p_pos << 8) | *(p_pos+1);
411 p_pos += 2;
413 for (i = 0 ; i <= i_raw_blocks_in_frame ; i++) {
414 //read 1 block
415 if (b_crc) {
416 WARN_ONCE(WARN_CRC_UNSUPPORTED, "ADTS CRC not supported");
417 //uint16_t crc = (*p_pos << 8) | *(p_pos+1);
418 //p_pos += 2;
421 #endif
425 /* Build the decoder specific info header */
426 if (!p_dec->fmt_out.i_extra) {
427 p_dec->fmt_out.p_extra = malloc(2);
428 if (!p_dec->fmt_out.p_extra) {
429 p_dec->fmt_out.i_extra = 0;
430 return 0;
432 p_dec->fmt_out.i_extra = 2;
433 ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
434 (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
435 ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
436 ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
439 /* ADTS header length */
440 *pi_header_size = b_crc ? 9 : 7;
442 return i_frame_size - *pi_header_size;
445 /****************************************************************************
446 * LOAS helpers
447 ****************************************************************************/
448 static int LOASSyncInfo(uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_header_size)
450 *pi_header_size = 3;
451 return ((p_header[1] & 0x1f) << 8) + p_header[2];
454 static int Mpeg4GAProgramConfigElement(bs_t *s)
456 /* TODO compute channels count ? */
457 int i_tag = bs_read(s, 4);
458 if (i_tag != 0x05)
459 return -1;
460 bs_skip(s, 2 + 4); // object type + sampling index
461 int i_num_front = bs_read(s, 4);
462 int i_num_side = bs_read(s, 4);
463 int i_num_back = bs_read(s, 4);
464 int i_num_lfe = bs_read(s, 2);
465 int i_num_assoc_data = bs_read(s, 3);
466 int i_num_valid_cc = bs_read(s, 4);
468 if (bs_read1(s))
469 bs_skip(s, 4); // mono downmix
470 if (bs_read1(s))
471 bs_skip(s, 4); // stereo downmix
472 if (bs_read1(s))
473 bs_skip(s, 2+1); // matrix downmix + pseudo_surround
475 bs_skip(s, i_num_front * (1+4));
476 bs_skip(s, i_num_side * (1+4));
477 bs_skip(s, i_num_back * (1+4));
478 bs_skip(s, i_num_lfe * (4));
479 bs_skip(s, i_num_assoc_data * (4));
480 bs_skip(s, i_num_valid_cc * (5));
481 bs_align(s);
482 int i_comment = bs_read(s, 8);
483 bs_skip(s, i_comment * 8);
484 return 0;
487 static int Mpeg4GASpecificConfig(mpeg4_asc_t *p_cfg, bs_t *s)
489 p_cfg->i_frame_length = bs_read1(s) ? 960 : 1024;
490 if(p_cfg->i_object_type == 23) /* 14496-3 4.5.1.1 */
491 p_cfg->i_frame_length >>= 1;
492 else if(p_cfg->i_object_type == 3)
493 p_cfg->i_frame_length = 256;
495 if (bs_read1(s)) // depend on core coder
496 bs_skip(s, 14); // core coder delay
498 int i_extension_flag = bs_read1(s);
499 if (p_cfg->i_channel == 0)
500 Mpeg4GAProgramConfigElement(s);
501 if (p_cfg->i_object_type == 6 || p_cfg->i_object_type == 20)
502 bs_skip(s, 3); // layer
504 if (i_extension_flag) {
505 if (p_cfg->i_object_type == 22)
506 bs_skip(s, 5 + 11); // numOfSubFrame + layer length
507 if (p_cfg->i_object_type == 17 || p_cfg->i_object_type == 19 ||
508 p_cfg->i_object_type == 20 || p_cfg->i_object_type == 23)
509 bs_skip(s, 1+1+1); // ER data : section scale spectral */
510 if (bs_read1(s)) // extension 3
511 fprintf(stderr, "Mpeg4GASpecificConfig: error 1\n");
513 return 0;
516 static int Mpeg4ReadAudioObjectType(bs_t *s)
518 int i_type = bs_read(s, 5);
519 if (i_type == 31)
520 i_type = 32 + bs_read(s, 6);
521 return i_type;
524 static int Mpeg4ReadAudioSamplerate(bs_t *s)
526 int i_index = bs_read(s, 4);
527 if (i_index != 0x0f)
528 return pi_sample_rates[i_index];
529 return bs_read(s, 24);
532 static int Mpeg4ReadAudioChannelConfiguration(bs_t *s)
534 int i_channel = bs_read(s, 4);
535 if (i_channel == 7)
536 i_channel = 8; // 7.1
537 else if (i_channel >= 8)
538 i_channel = -1;
539 return i_channel;
542 static int Mpeg4ReadAudioSpecificConfig(bs_t *s, mpeg4_asc_t *p_cfg, bool b_withext)
544 #if 0
545 static const char *ppsz_otype[] = {
546 "NULL",
547 "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
548 "TwinVQ",
549 "CELP", "HVXC",
550 "Reserved", "Reserved",
551 "TTSI",
552 "Main Synthetic", "Wavetables Synthesis", "General MIDI",
553 "Algorithmic Synthesis and Audio FX",
554 "ER AAC LC",
555 "Reserved",
556 "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
557 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
558 "SSC",
559 "PS", "Reserved", "Escape",
560 "Layer 1", "Layer 2", "Layer 3",
561 "DST",
563 #endif
564 memset(p_cfg, 0, sizeof(*p_cfg));
566 p_cfg->i_object_type = Mpeg4ReadAudioObjectType(s);
567 p_cfg->i_samplerate = Mpeg4ReadAudioSamplerate(s);
568 p_cfg->i_channel = Mpeg4ReadAudioChannelConfiguration(s);
570 p_cfg->i_sbr = -1;
571 p_cfg->i_ps = -1;
572 p_cfg->extension.i_object_type = 0;
573 p_cfg->extension.i_samplerate = 0;
574 p_cfg->extension.i_channel = -1;
575 if (p_cfg->i_object_type == 5 || p_cfg->i_object_type == 29) {
576 p_cfg->i_sbr = 1;
577 if (p_cfg->i_object_type == 29)
578 p_cfg->i_ps = 1;
579 p_cfg->extension.i_object_type = 5;
580 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate(s);
582 p_cfg->i_object_type = Mpeg4ReadAudioObjectType(s);
583 if(p_cfg->i_object_type == 22)
584 p_cfg->extension.i_channel = Mpeg4ReadAudioChannelConfiguration(s);
587 switch(p_cfg->i_object_type)
589 case 1: case 2: case 3: case 4:
590 case 6: case 7:
591 case 17: case 19: case 20: case 21: case 22: case 23:
592 Mpeg4GASpecificConfig(p_cfg, s);
593 break;
594 case 8:
595 // CelpSpecificConfig();
596 case 9:
597 // HvxcSpecificConfig();
598 case 12:
599 // TTSSSpecificConfig();
600 case 13: case 14: case 15: case 16:
601 // StructuredAudioSpecificConfig();
602 case 24:
603 // ERCelpSpecificConfig();
604 case 25:
605 // ERHvxcSpecificConfig();
606 case 26: case 27:
607 // ParametricSpecificConfig();
608 case 28:
609 // SSCSpecificConfig();
610 case 32: case 33: case 34:
611 // MPEG_1_2_SpecificConfig();
612 case 35:
613 // DSTSpecificConfig();
614 case 36:
615 // ALSSpecificConfig();
616 case 37: case 38:
617 // SLSSpecificConfig();
618 case 39:
619 // ELDSpecificConfig();
620 case 40: case 41:
621 // SymbolicMusicSpecificConfig();
622 default:
623 // error
624 return VLC_EGENERIC;
627 switch(p_cfg->i_object_type)
629 case 17: case 19: case 20: case 21: case 22: case 23:
630 case 24: case 25: case 26: case 27: case 39:
632 int epConfig = bs_read(s, 2);
633 if (epConfig == 2 || epConfig == 3)
634 //ErrorProtectionSpecificConfig();
635 if (epConfig == 3)
636 if (bs_read1(s)) {
637 // TODO : directMapping
639 break;
641 default:
642 break;
645 if (b_withext && p_cfg->extension.i_object_type != 5 && bs_remain(s) >= 16 &&
646 bs_read(s, 11) == 0x2b7) {
647 p_cfg->extension.i_object_type = Mpeg4ReadAudioObjectType(s);
648 if (p_cfg->extension.i_object_type == 5)
650 p_cfg->i_sbr = bs_read1(s);
651 if (p_cfg->i_sbr == 1) {
652 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate(s);
653 if (bs_remain(s) >= 12 && bs_read(s, 11) == 0x548)
654 p_cfg->i_ps = bs_read1(s);
657 else if (p_cfg->extension.i_object_type == 22)
659 p_cfg->i_sbr = bs_read1(s);
660 if(p_cfg->i_sbr)
661 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate(s);
662 p_cfg->extension.i_channel = Mpeg4ReadAudioChannelConfiguration(s);
666 //fprintf(stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
667 // ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr);
669 return VLC_SUCCESS;
672 static uint32_t LatmGetValue(bs_t *s)
674 uint32_t v = 0;
675 for (int i = 1 + bs_read(s, 2); i > 0; i--)
676 v = (v << 8) + bs_read(s, 8);
677 return v;
680 static uint32_t AudioSpecificConfigBitsToBytes(bs_t *s, uint32_t i_bits, uint8_t *p_data)
682 uint32_t i_extra = __MIN((i_bits + 7) / 8, LATM_MAX_EXTRA_SIZE);
683 for (uint32_t i = 0; i < i_extra; i++) {
684 const uint32_t i_read = __MIN(8, i_bits - 8*i);
685 p_data[i] = bs_read(s, i_read) << (8-i_read);
687 return i_extra;
690 static int LatmReadStreamMuxConfiguration(latm_mux_t *m, bs_t *s)
692 int i_mux_version;
693 int i_mux_versionA;
695 i_mux_version = bs_read(s, 1);
696 i_mux_versionA = 0;
697 if (i_mux_version)
698 i_mux_versionA = bs_read(s, 1);
700 if (i_mux_versionA != 0) /* support only A=0 */
701 return -1;
703 memset(m, 0, sizeof(*m));
705 if (i_mux_versionA == 0)
706 if (i_mux_version == 1)
707 LatmGetValue(s); /* taraBufferFullness */
709 m->b_same_time_framing = bs_read1(s);
710 m->i_sub_frames = 1 + bs_read(s, 6);
711 m->i_programs = 1 + bs_read(s, 4);
713 for (int i_program = 0; i_program < m->i_programs; i_program++) {
714 m->pi_layers[i_program] = 1+bs_read(s, 3);
716 for (int i_layer = 0; i_layer < m->pi_layers[i_program]; i_layer++) {
717 latm_stream_t *st = &m->stream[m->i_streams];
718 bool b_previous_cfg;
720 m->pi_stream[i_program][i_layer] = m->i_streams;
721 st->i_program = i_program;
722 st->i_layer = i_layer;
724 b_previous_cfg = false;
725 if (i_program != 0 || i_layer != 0)
726 b_previous_cfg = bs_read1(s);
728 if (b_previous_cfg) {
729 assert(m->i_streams > 0);
730 st->cfg = m->stream[m->i_streams-1].cfg;
731 } else {
732 uint32_t asc_size = 0;
733 if(i_mux_version > 0)
734 asc_size = LatmGetValue(s);
735 bs_t asc_bs = *s;
736 Mpeg4ReadAudioSpecificConfig(&asc_bs, &st->cfg, i_mux_version > 0);
737 if (i_mux_version == 0)
738 asc_size = bs_pos(&asc_bs) - bs_pos(s);
739 asc_bs = *s;
740 st->i_extra = AudioSpecificConfigBitsToBytes(&asc_bs, asc_size, st->extra);
741 bs_skip(s, asc_size);
744 st->i_frame_length_type = bs_read(s, 3);
745 switch(st->i_frame_length_type)
747 case 0:
749 bs_skip(s, 8); /* latmBufferFullnes */
750 if (!m->b_same_time_framing)
751 if (st->cfg.i_object_type == 6 || st->cfg.i_object_type == 20 ||
752 st->cfg.i_object_type == 8 || st->cfg.i_object_type == 24)
753 bs_skip(s, 6); /* eFrameOffset */
754 break;
756 case 1:
757 st->i_frame_length = bs_read(s, 9);
758 break;
759 case 3: case 4: case 5:
760 st->i_frame_length_index = bs_read(s, 6); // celp
761 break;
762 case 6: case 7:
763 st->i_frame_length_index = bs_read(s, 1); // hvxc
764 default:
765 break;
767 /* Next stream */
768 m->i_streams++;
772 /* other data */
773 if (bs_read1(s)) {
774 if (i_mux_version == 1)
775 m->i_other_data = LatmGetValue(s);
776 else {
777 int b_continue;
778 do {
779 b_continue = bs_read1(s);
780 m->i_other_data = (m->i_other_data << 8) + bs_read(s, 8);
781 } while (b_continue);
785 /* crc */
786 m->i_crc = -1;
787 if (bs_read1(s))
788 m->i_crc = bs_read(s, 8);
790 return 0;
793 static int LOASParse(decoder_t *p_dec, uint8_t *p_buffer, int i_buffer)
795 decoder_sys_t *p_sys = p_dec->p_sys;
796 bs_t s;
797 int i_accumulated = 0;
799 bs_init(&s, p_buffer, i_buffer);
801 /* Read the stream mux configuration if present */
802 if (!bs_read1(&s) && !LatmReadStreamMuxConfiguration(&p_sys->latm, &s) &&
803 p_sys->latm.i_streams > 0) {
804 const latm_stream_t *st = &p_sys->latm.stream[0];
806 if(st->cfg.i_samplerate <= 0 || st->cfg.i_channel <=0 || st->cfg.i_frame_length <= 0)
807 return 0;
809 p_sys->i_channels = st->cfg.i_channel;
810 p_sys->i_rate = st->cfg.i_samplerate;
811 p_sys->i_frame_length = st->cfg.i_frame_length;
813 if (p_sys->i_channels && p_sys->i_rate && p_sys->i_frame_length > 0)
815 if(p_dec->fmt_out.i_extra != st->i_extra ||
816 (p_dec->fmt_out.i_extra > 0 &&
817 memcmp(p_dec->fmt_out.p_extra, st->extra, st->i_extra)) )
819 if(p_dec->fmt_out.i_extra)
820 free(p_dec->fmt_out.p_extra);
821 p_dec->fmt_out.p_extra = malloc(st->i_extra);
822 if(p_dec->fmt_out.p_extra)
824 p_dec->fmt_out.i_extra = st->i_extra;
825 memcpy(p_dec->fmt_out.p_extra, st->extra, st->i_extra);
826 p_sys->b_latm_cfg = true;
828 else
830 p_dec->fmt_out.i_extra = 0;
831 p_sys->b_latm_cfg = false;
837 /* Wait for the configuration */
838 if (!p_sys->b_latm_cfg)
840 /* WAVE_FORMAT_MPEG_LOAS, configuration provided as AAC header :/ */
841 if( p_dec->fmt_in.i_extra > 0 &&
842 p_sys->i_channels && p_sys->i_rate && p_sys->i_frame_length )
844 p_sys->b_latm_cfg = true;
846 else return 0;
849 /* FIXME do we need to split the subframe into independent packet ? */
850 if (p_sys->latm.i_sub_frames > 1)
851 msg_Err(p_dec, "latm sub frames not yet supported, please send a sample");
853 for (int i_sub = 0; i_sub < p_sys->latm.i_sub_frames; i_sub++) {
854 int pi_payload[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
855 if (p_sys->latm.b_same_time_framing) {
856 /* Payload length */
857 for (int i_program = 0; i_program < p_sys->latm.i_programs; i_program++) {
858 for (int i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++) {
859 latm_stream_t *st = &p_sys->latm.stream[p_sys->latm.pi_stream[i_program][i_layer]];
860 if (st->i_frame_length_type == 0) {
861 int i_payload = 0;
862 for (;;) {
863 int i_tmp = bs_read(&s, 8);
864 i_payload += i_tmp;
865 if (i_tmp != 255)
866 break;
868 pi_payload[i_program][i_layer] = i_payload;
869 } else if (st->i_frame_length_type == 1) {
870 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
871 } else if ((st->i_frame_length_type == 3) ||
872 (st->i_frame_length_type == 5) ||
873 (st->i_frame_length_type == 7)) {
874 bs_skip(&s, 2); // muxSlotLengthCoded
875 pi_payload[i_program][i_layer] = 0; /* TODO */
876 } else {
877 pi_payload[i_program][i_layer] = 0; /* TODO */
882 /* Payload Data */
883 for (int i_program = 0; i_program < p_sys->latm.i_programs; i_program++) {
884 for (int i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++) {
885 /* XXX we only extract 1 stream */
886 if (i_program != 0 || i_layer != 0)
887 break;
889 if (pi_payload[i_program][i_layer] <= 0)
890 continue;
892 /* FIXME that's slow (and a bit ugly to write in place) */
893 for (int i = 0; i < pi_payload[i_program][i_layer]; i++) {
894 if (i_accumulated >= i_buffer)
895 return 0;
896 p_buffer[i_accumulated++] = bs_read(&s, 8);
900 } else {
901 const int i_chunks = bs_read(&s, 4);
902 #if 0
903 int pi_program[16];
904 int pi_layer[16];
905 #endif
907 msg_Err(p_dec, "latm without same time frameing not yet supported, please send a sample");
909 for (int i_chunk = 0; i_chunk < i_chunks; i_chunk++) {
910 const int streamIndex = bs_read(&s, 4);
911 latm_stream_t *st = &p_sys->latm.stream[streamIndex];
912 const int i_program = st->i_program;
913 const int i_layer = st->i_layer;
915 #if 0
916 pi_program[i_chunk] = i_program;
917 pi_layer[i_chunk] = i_layer;
918 #endif
920 if (st->i_frame_length_type == 0) {
921 int i_payload = 0;
922 for (;;) {
923 int i_tmp = bs_read(&s, 8);
924 i_payload += i_tmp;
925 if (i_tmp != 255)
926 break;
928 pi_payload[i_program][i_layer] = i_payload;
929 bs_skip(&s, 1); // auEndFlag
930 } else if (st->i_frame_length_type == 1) {
931 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
932 } else if ((st->i_frame_length_type == 3) ||
933 (st->i_frame_length_type == 5) ||
934 (st->i_frame_length_type == 7)) {
935 bs_read(&s, 2); // muxSlotLengthCoded
938 #if 0
939 for (int i_chunk = 0; i_chunk < i_chunks; i_chunk++) {
940 //const int i_program = pi_program[i_chunk];
941 //const int i_layer = pi_layer[i_chunk];
943 /* TODO ? Payload */
945 #endif
949 #if 0
950 if (p_sys->latm.i_other_data > 0)
951 ; // TODO
952 #endif
953 bs_align(&s);
955 return i_accumulated;
958 /*****************************************************************************
960 *****************************************************************************/
961 static void SetupOutput(decoder_t *p_dec, block_t *p_block)
963 decoder_sys_t *p_sys = p_dec->p_sys;
965 if (p_dec->fmt_out.audio.i_rate != p_sys->i_rate && p_sys->i_rate > 0)
967 msg_Info(p_dec, "AAC channels: %d samplerate: %d",
968 p_sys->i_channels, p_sys->i_rate);
970 const mtime_t i_end_date = date_Get(&p_sys->end_date);
971 date_Init(&p_sys->end_date, p_sys->i_rate, 1);
972 date_Set(&p_sys->end_date, i_end_date);
975 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
976 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
977 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
978 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
980 #if 0
981 p_dec->fmt_out.audio.i_physical_channels = p_sys->i_channels_conf;
982 #endif
984 p_block->i_pts = p_block->i_dts = date_Get(&p_sys->end_date);
986 p_block->i_length =
987 date_Increment(&p_sys->end_date, p_sys->i_frame_length) - p_block->i_pts;
990 /*****************************************************************************
991 * FlushStreamBlock:
992 *****************************************************************************/
993 static void Flush(decoder_t *p_dec)
995 decoder_sys_t *p_sys = p_dec->p_sys;
997 p_sys->i_state = STATE_NOSYNC;
998 block_BytestreamEmpty(&p_sys->bytestream);
999 date_Set(&p_sys->end_date, VLC_TS_INVALID);
1000 p_sys->b_discontuinity = true;
1003 static inline bool HasADTSHeader( const uint8_t *p_header )
1005 return p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0;
1008 static inline bool HasLoasHeader( const uint8_t *p_header )
1010 return p_header[0] == 0x56 && (p_header[1] & 0xe0) == 0xe0;
1013 /****************************************************************************
1014 * PacketizeStreamBlock: ADTS/LOAS packetizer
1015 ****************************************************************************/
1016 static block_t *PacketizeStreamBlock(decoder_t *p_dec, block_t **pp_block)
1018 decoder_sys_t *p_sys = p_dec->p_sys;
1019 uint8_t p_header[ADTS_HEADER_SIZE + LOAS_HEADER_SIZE];
1020 block_t *p_out_buffer;
1021 uint8_t *p_buf;
1023 block_t *p_block = pp_block ? *pp_block : NULL;
1025 if(p_block)
1027 block_BytestreamPush(&p_sys->bytestream, p_block);
1028 *pp_block = NULL;
1031 for (;;) switch(p_sys->i_state) {
1032 case STATE_NOSYNC:
1033 while (block_PeekBytes(&p_sys->bytestream, p_header, 2) == VLC_SUCCESS) {
1034 /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
1035 if ((p_sys->i_type == TYPE_ADTS || p_sys->i_type == TYPE_UNKNOWN_NONRAW) &&
1036 HasADTSHeader( p_header ) )
1038 if (p_sys->i_type != TYPE_ADTS)
1039 msg_Dbg(p_dec, "detected ADTS format");
1041 p_sys->i_state = STATE_SYNC;
1042 p_sys->i_type = TYPE_ADTS;
1043 break;
1045 else if ((p_sys->i_type == TYPE_LOAS || p_sys->i_type == TYPE_UNKNOWN_NONRAW) &&
1046 HasLoasHeader( p_header ) )
1048 if (p_sys->i_type != TYPE_LOAS)
1049 msg_Dbg(p_dec, "detected LOAS format");
1051 p_sys->i_state = STATE_SYNC;
1052 p_sys->i_type = TYPE_LOAS;
1053 break;
1055 block_SkipByte(&p_sys->bytestream);
1057 if (p_sys->i_state != STATE_SYNC) {
1058 block_BytestreamFlush(&p_sys->bytestream);
1060 /* Need more data */
1061 return NULL;
1064 case STATE_SYNC:
1065 /* New frame, set the Presentation Time Stamp */
1066 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
1067 if (p_sys->i_pts > VLC_TS_INVALID &&
1068 p_sys->i_pts != date_Get(&p_sys->end_date))
1069 date_Set(&p_sys->end_date, p_sys->i_pts);
1070 p_sys->i_state = STATE_HEADER;
1071 break;
1073 case STATE_HEADER:
1074 if (p_sys->i_type == TYPE_ADTS) {
1075 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
1076 if (block_PeekBytes(&p_sys->bytestream, p_header,
1077 ADTS_HEADER_SIZE) != VLC_SUCCESS)
1078 return NULL; /* Need more data */
1080 /* Check if frame is valid and get frame info */
1081 p_sys->i_frame_size = ADTSSyncInfo(p_dec, p_header,
1082 &p_sys->i_channels,
1083 &p_sys->i_rate,
1084 &p_sys->i_frame_length,
1085 &p_sys->i_header_size);
1086 } else {
1087 assert(p_sys->i_type == TYPE_LOAS);
1088 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
1089 if (block_PeekBytes(&p_sys->bytestream, p_header,
1090 LOAS_HEADER_SIZE) != VLC_SUCCESS)
1091 return NULL; /* Need more data */
1093 /* Check if frame is valid and get frame info */
1094 p_sys->i_frame_size = LOASSyncInfo(p_header, &p_sys->i_header_size);
1097 if (p_sys->i_frame_size <= 0) {
1098 msg_Dbg(p_dec, "emulated sync word");
1099 block_SkipByte(&p_sys->bytestream);
1100 p_sys->i_state = STATE_NOSYNC;
1101 break;
1104 p_sys->i_state = STATE_NEXT_SYNC;
1106 case STATE_NEXT_SYNC:
1107 if (p_sys->bytestream.p_block == NULL) {
1108 p_sys->i_state = STATE_NOSYNC;
1109 block_BytestreamFlush(&p_sys->bytestream);
1110 return NULL;
1113 /* Check if next expected frame contains the sync word */
1114 if (block_PeekOffsetBytes(&p_sys->bytestream, p_sys->i_frame_size
1115 + p_sys->i_header_size, p_header, 2) != VLC_SUCCESS)
1117 if(p_block == NULL) /* drain */
1119 p_sys->i_state = STATE_SEND_DATA;
1120 break;
1122 return NULL; /* Need more data */
1125 assert((p_sys->i_type == TYPE_ADTS) || (p_sys->i_type == TYPE_LOAS));
1126 if ( (p_sys->i_type == TYPE_ADTS && !HasADTSHeader( p_header )) ||
1127 (p_sys->i_type == TYPE_LOAS && !HasLoasHeader( p_header )) )
1129 /* Check spacial padding case. Failing if need more bytes is ok since
1130 that should have been sent as a whole block */
1131 if( block_PeekOffsetBytes(&p_sys->bytestream,
1132 p_sys->i_frame_size + p_sys->i_header_size,
1133 p_header, 3) == VLC_SUCCESS &&
1134 p_header[0] == 0x00 &&
1135 ((p_sys->i_type == TYPE_ADTS && HasADTSHeader( &p_header[1] )) ||
1136 (p_sys->i_type == TYPE_LOAS && !HasLoasHeader( &p_header[1] ))))
1138 p_sys->i_state = STATE_SEND_DATA;
1140 else
1142 msg_Dbg(p_dec, "emulated sync word (no sync on following frame)"
1143 " 0x%"PRIx8" 0x%"PRIx8, p_header[0], p_header[1] );
1144 p_sys->i_state = STATE_NOSYNC;
1145 block_SkipByte(&p_sys->bytestream);
1147 break;
1150 p_sys->i_state = STATE_SEND_DATA;
1151 break;
1153 case STATE_GET_DATA:
1154 /* Make sure we have enough data.
1155 * (Not useful if we went through NEXT_SYNC) */
1156 if (block_WaitBytes(&p_sys->bytestream, p_sys->i_frame_size +
1157 p_sys->i_header_size) != VLC_SUCCESS)
1158 return NULL; /* Need more data */
1159 p_sys->i_state = STATE_SEND_DATA;
1161 case STATE_SEND_DATA:
1162 /* When we reach this point we already know we have enough
1163 * data available. */
1165 p_out_buffer = block_Alloc(p_sys->i_frame_size);
1166 if (!p_out_buffer) {
1167 return NULL;
1169 p_buf = p_out_buffer->p_buffer;
1171 /* Skip the ADTS/LOAS header */
1172 block_SkipBytes(&p_sys->bytestream, p_sys->i_header_size);
1174 /* Copy the whole frame into the buffer */
1175 block_GetBytes(&p_sys->bytestream, p_buf, p_sys->i_frame_size);
1176 if (p_sys->i_type != TYPE_ADTS) { /* parse/extract the whole frame */
1177 assert(p_sys->i_type == TYPE_LOAS);
1178 p_out_buffer->i_buffer = LOASParse(p_dec, p_buf, p_sys->i_frame_size);
1179 if (p_out_buffer->i_buffer <= 0)
1181 if (!p_sys->b_latm_cfg)
1182 msg_Warn(p_dec, "waiting for header");
1184 block_Release(p_out_buffer);
1185 p_out_buffer = NULL;
1186 p_sys->i_state = STATE_NOSYNC;
1187 break;
1190 SetupOutput(p_dec, p_out_buffer);
1191 /* Make sure we don't reuse the same pts twice */
1192 if (p_sys->i_pts == p_sys->bytestream.p_block->i_pts)
1193 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
1195 /* So p_block doesn't get re-added several times */
1196 if( pp_block )
1197 *pp_block = block_BytestreamPop(&p_sys->bytestream);
1199 p_sys->i_state = STATE_NOSYNC;
1201 return p_out_buffer;
1204 return NULL;
1207 /****************************************************************************
1208 * Packetize: just forwards raw blocks, or packetizes LOAS/ADTS
1209 * and strips headers
1210 ****************************************************************************/
1211 static block_t *Packetize(decoder_t *p_dec, block_t **pp_block)
1213 decoder_sys_t *p_sys = p_dec->p_sys;
1214 block_t *p_block = pp_block ? *pp_block : NULL;
1216 if(p_block)
1218 if (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED))
1220 if(p_sys->i_type == TYPE_ADTS || p_sys->i_type == TYPE_LOAS)
1222 /* First always drain complete blocks before discontinuity */
1223 block_t *p_drain = PacketizeStreamBlock(p_dec, NULL);
1224 if(p_drain)
1225 return p_drain;
1228 Flush(p_dec);
1230 if (p_block->i_flags & BLOCK_FLAG_CORRUPTED)
1232 block_Release(p_block);
1233 return NULL;
1237 if (!date_Get(&p_sys->end_date) && p_block->i_pts <= VLC_TS_INVALID)
1239 /* We've just started the stream, wait for the first PTS. */
1240 block_Release(p_block);
1241 return NULL;
1245 if(p_block && p_sys->i_type == TYPE_UNKNOWN)
1247 p_sys->i_type = TYPE_RAW;
1248 if(p_block->i_buffer > 1)
1250 if(p_block->p_buffer[0] == 0xff && (p_block->p_buffer[1] & 0xf6) == 0xf0)
1252 p_sys->i_type = TYPE_ADTS;
1254 else if(p_block->p_buffer[0] == 0x56 && (p_block->p_buffer[1] & 0xe0) == 0xe0)
1256 p_sys->i_type = TYPE_LOAS;
1261 if(p_sys->i_type == TYPE_RAW)
1262 p_block = ForwardRawBlock(p_dec, pp_block);
1263 else
1264 p_block = PacketizeStreamBlock(p_dec, pp_block);
1266 if(p_block && p_sys->b_discontuinity)
1268 p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
1269 p_sys->b_discontuinity = false;
1272 return p_block;