1 /*****************************************************************************
2 * mpeg4audio.c: parse and packetize an MPEG 4 audio stream
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
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 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_block.h>
39 #include <vlc_block_helper.h>
40 #include "packetizer_helper.h"
46 * AudioObjectType 5 bits
47 * samplingFrequencyIndex 4 bits
48 * if (samplingFrequencyIndex == 0xF)
49 * samplingFrequency 24 bits
50 * channelConfiguration 4 bits
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 *****************************************************************************/
65 int i_sbr
; // 0: no sbr, 1: sbr, -1: unknown
66 int i_ps
; // 0: no ps, 1: ps, -1: unknown
76 int i_frame_length
; // 1024 or 960
80 #define LATM_MAX_EXTRA_SIZE 64
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
92 /* Raw configuration */
94 uint8_t extra
[LATM_MAX_EXTRA_SIZE
];
98 #define LATM_MAX_LAYER (8)
99 #define LATM_MAX_PROGRAM (16)
102 int b_same_time_framing
;
106 int pi_layers
[LATM_MAX_PROGRAM
];
108 int pi_stream
[LATM_MAX_PROGRAM
][LATM_MAX_LAYER
];
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 */
125 block_bytestream_t bytestream
;
132 bool b_discontuinity
;
135 unsigned int i_channels
;
136 unsigned int i_rate
, i_frame_length
, i_header_size
;
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 );\
161 TYPE_UNKNOWN
, /* AAC samples with[out] headers */
162 TYPE_UNKNOWN_NONRAW
, /* [un]packetized ADTS or LOAS */
163 TYPE_RAW
, /* RAW AAC frames */
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 /****************************************************************************
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 /*****************************************************************************
190 *****************************************************************************/
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
)
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
)
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
)
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");
246 case VLC_FOURCC('A','D','T','S'):
247 p_sys
->i_type
= TYPE_ADTS
;
248 msg_Dbg(p_dec
, "ADTS Mode");
251 case VLC_FOURCC('H','E','A','D'):
252 p_sys
->i_type
= TYPE_UNKNOWN_NONRAW
;
256 p_sys
->i_type
= TYPE_UNKNOWN
;
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
)
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
)
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);
298 p_dec
->pf_packetize
= Packetize
;
299 p_dec
->pf_flush
= Flush
;
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
);
316 /****************************************************************************
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
;
326 if (!pp_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
;
351 /****************************************************************************
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
;
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 */
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");
389 *pi_frame_length
= 1024;
391 if (i_raw_blocks_in_frame
== 0) {
393 WARN_ONCE(WARN_CRC_UNSUPPORTED
, "ADTS CRC not supported");
394 //uint16_t crc = (p_buf[7] << 8) | p_buf[8];
397 msg_Err(p_dec
, "Multiple blocks per frame in ADTS not supported");
401 const uint8_t *p_pos
= p_buf
+ 7;
403 uint16_t i_block_pos
[3];
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);
410 crc_block
= (*p_pos
<< 8) | *(p_pos
+1);
413 for (i
= 0 ; i
<= i_raw_blocks_in_frame
; i
++) {
416 WARN_ONCE(WARN_CRC_UNSUPPORTED
, "ADTS CRC not supported");
417 //uint16_t crc = (*p_pos << 8) | *(p_pos+1);
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;
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 /****************************************************************************
447 ****************************************************************************/
448 static int LOASSyncInfo(uint8_t p_header
[LOAS_HEADER_SIZE
], unsigned int *pi_header_size
)
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);
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);
469 bs_skip(s
, 4); // mono downmix
471 bs_skip(s
, 4); // stereo downmix
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));
482 int i_comment
= bs_read(s
, 8);
483 bs_skip(s
, i_comment
* 8);
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");
516 static int Mpeg4ReadAudioObjectType(bs_t
*s
)
518 int i_type
= bs_read(s
, 5);
520 i_type
= 32 + bs_read(s
, 6);
524 static int Mpeg4ReadAudioSamplerate(bs_t
*s
)
526 int i_index
= bs_read(s
, 4);
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);
536 i_channel
= 8; // 7.1
537 else if (i_channel
>= 8)
542 static int Mpeg4ReadAudioSpecificConfig(bs_t
*s
, mpeg4_asc_t
*p_cfg
, bool b_withext
)
545 static const char *ppsz_otype
[] = {
547 "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
550 "Reserved", "Reserved",
552 "Main Synthetic", "Wavetables Synthesis", "General MIDI",
553 "Algorithmic Synthesis and Audio FX",
556 "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
557 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
559 "PS", "Reserved", "Escape",
560 "Layer 1", "Layer 2", "Layer 3",
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
);
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) {
577 if (p_cfg
->i_object_type
== 29)
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:
591 case 17: case 19: case 20: case 21: case 22: case 23:
592 Mpeg4GASpecificConfig(p_cfg
, s
);
595 // CelpSpecificConfig();
597 // HvxcSpecificConfig();
599 // TTSSSpecificConfig();
600 case 13: case 14: case 15: case 16:
601 // StructuredAudioSpecificConfig();
603 // ERCelpSpecificConfig();
605 // ERHvxcSpecificConfig();
607 // ParametricSpecificConfig();
609 // SSCSpecificConfig();
610 case 32: case 33: case 34:
611 // MPEG_1_2_SpecificConfig();
613 // DSTSpecificConfig();
615 // ALSSpecificConfig();
617 // SLSSpecificConfig();
619 // ELDSpecificConfig();
621 // SymbolicMusicSpecificConfig();
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();
637 // TODO : directMapping
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
);
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);
672 static uint32_t LatmGetValue(bs_t
*s
)
675 for (int i
= 1 + bs_read(s
, 2); i
> 0; i
--)
676 v
= (v
<< 8) + bs_read(s
, 8);
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
);
690 static int LatmReadStreamMuxConfiguration(latm_mux_t
*m
, bs_t
*s
)
695 i_mux_version
= bs_read(s
, 1);
698 i_mux_versionA
= bs_read(s
, 1);
700 if (i_mux_versionA
!= 0) /* support only A=0 */
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
];
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
;
732 uint32_t asc_size
= 0;
733 if(i_mux_version
> 0)
734 asc_size
= LatmGetValue(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
);
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
)
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 */
757 st
->i_frame_length
= bs_read(s
, 9);
759 case 3: case 4: case 5:
760 st
->i_frame_length_index
= bs_read(s
, 6); // celp
763 st
->i_frame_length_index
= bs_read(s
, 1); // hvxc
774 if (i_mux_version
== 1)
775 m
->i_other_data
= LatmGetValue(s
);
779 b_continue
= bs_read1(s
);
780 m
->i_other_data
= (m
->i_other_data
<< 8) + bs_read(s
, 8);
781 } while (b_continue
);
788 m
->i_crc
= bs_read(s
, 8);
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
;
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)
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;
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;
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
) {
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) {
863 int i_tmp
= bs_read(&s
, 8);
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 */
877 pi_payload
[i_program
][i_layer
] = 0; /* TODO */
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)
889 if (pi_payload
[i_program
][i_layer
] <= 0)
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
)
896 p_buffer
[i_accumulated
++] = bs_read(&s
, 8);
901 const int i_chunks
= bs_read(&s
, 4);
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
;
916 pi_program
[i_chunk
] = i_program
;
917 pi_layer
[i_chunk
] = i_layer
;
920 if (st
->i_frame_length_type
== 0) {
923 int i_tmp
= bs_read(&s
, 8);
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
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];
950 if (p_sys
->latm
.i_other_data
> 0)
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
;
981 p_dec
->fmt_out
.audio
.i_physical_channels
= p_sys
->i_channels_conf
;
984 p_block
->i_pts
= p_block
->i_dts
= date_Get(&p_sys
->end_date
);
987 date_Increment(&p_sys
->end_date
, p_sys
->i_frame_length
) - p_block
->i_pts
;
990 /*****************************************************************************
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
;
1023 block_t
*p_block
= pp_block
? *pp_block
: NULL
;
1027 block_BytestreamPush(&p_sys
->bytestream
, p_block
);
1031 for (;;) switch(p_sys
->i_state
) {
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
;
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
;
1055 block_SkipByte(&p_sys
->bytestream
);
1057 if (p_sys
->i_state
!= STATE_SYNC
) {
1058 block_BytestreamFlush(&p_sys
->bytestream
);
1060 /* Need more data */
1066 /* New frame, set the Presentation Time Stamp */
1067 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
;
1068 if (p_sys
->i_pts
> VLC_TS_INVALID
&&
1069 p_sys
->i_pts
!= date_Get(&p_sys
->end_date
))
1070 date_Set(&p_sys
->end_date
, p_sys
->i_pts
);
1071 p_sys
->i_state
= STATE_HEADER
;
1075 if (p_sys
->i_type
== TYPE_ADTS
) {
1076 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
1077 if (block_PeekBytes(&p_sys
->bytestream
, p_header
,
1078 ADTS_HEADER_SIZE
) != VLC_SUCCESS
)
1079 return NULL
; /* Need more data */
1081 /* Check if frame is valid and get frame info */
1082 p_sys
->i_frame_size
= ADTSSyncInfo(p_dec
, p_header
,
1085 &p_sys
->i_frame_length
,
1086 &p_sys
->i_header_size
);
1088 assert(p_sys
->i_type
== TYPE_LOAS
);
1089 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
1090 if (block_PeekBytes(&p_sys
->bytestream
, p_header
,
1091 LOAS_HEADER_SIZE
) != VLC_SUCCESS
)
1092 return NULL
; /* Need more data */
1094 /* Check if frame is valid and get frame info */
1095 p_sys
->i_frame_size
= LOASSyncInfo(p_header
, &p_sys
->i_header_size
);
1098 if (p_sys
->i_frame_size
<= 0) {
1099 msg_Dbg(p_dec
, "emulated sync word");
1100 block_SkipByte(&p_sys
->bytestream
);
1101 p_sys
->i_state
= STATE_NOSYNC
;
1105 p_sys
->i_state
= STATE_NEXT_SYNC
;
1108 case STATE_NEXT_SYNC
:
1109 if (p_sys
->bytestream
.p_block
== NULL
) {
1110 p_sys
->i_state
= STATE_NOSYNC
;
1111 block_BytestreamFlush(&p_sys
->bytestream
);
1115 /* Check if next expected frame contains the sync word */
1116 if (block_PeekOffsetBytes(&p_sys
->bytestream
, p_sys
->i_frame_size
1117 + p_sys
->i_header_size
, p_header
, 2) != VLC_SUCCESS
)
1119 if(p_block
== NULL
) /* drain */
1121 p_sys
->i_state
= STATE_SEND_DATA
;
1124 return NULL
; /* Need more data */
1127 assert((p_sys
->i_type
== TYPE_ADTS
) || (p_sys
->i_type
== TYPE_LOAS
));
1128 if ( (p_sys
->i_type
== TYPE_ADTS
&& !HasADTSHeader( p_header
)) ||
1129 (p_sys
->i_type
== TYPE_LOAS
&& !HasLoasHeader( p_header
)) )
1131 /* Check spacial padding case. Failing if need more bytes is ok since
1132 that should have been sent as a whole block */
1133 if( block_PeekOffsetBytes(&p_sys
->bytestream
,
1134 p_sys
->i_frame_size
+ p_sys
->i_header_size
,
1135 p_header
, 3) == VLC_SUCCESS
&&
1136 p_header
[0] == 0x00 &&
1137 ((p_sys
->i_type
== TYPE_ADTS
&& HasADTSHeader( &p_header
[1] )) ||
1138 (p_sys
->i_type
== TYPE_LOAS
&& !HasLoasHeader( &p_header
[1] ))))
1140 p_sys
->i_state
= STATE_SEND_DATA
;
1144 msg_Dbg(p_dec
, "emulated sync word (no sync on following frame)"
1145 " 0x%"PRIx8
" 0x%"PRIx8
, p_header
[0], p_header
[1] );
1146 p_sys
->i_state
= STATE_NOSYNC
;
1147 block_SkipByte(&p_sys
->bytestream
);
1152 p_sys
->i_state
= STATE_SEND_DATA
;
1155 case STATE_GET_DATA
:
1156 /* Make sure we have enough data.
1157 * (Not useful if we went through NEXT_SYNC) */
1158 if (block_WaitBytes(&p_sys
->bytestream
, p_sys
->i_frame_size
+
1159 p_sys
->i_header_size
) != VLC_SUCCESS
)
1160 return NULL
; /* Need more data */
1161 p_sys
->i_state
= STATE_SEND_DATA
;
1164 case STATE_SEND_DATA
:
1165 /* When we reach this point we already know we have enough
1166 * data available. */
1168 p_out_buffer
= block_Alloc(p_sys
->i_frame_size
);
1169 if (!p_out_buffer
) {
1172 p_buf
= p_out_buffer
->p_buffer
;
1174 /* Skip the ADTS/LOAS header */
1175 block_SkipBytes(&p_sys
->bytestream
, p_sys
->i_header_size
);
1177 /* Copy the whole frame into the buffer */
1178 block_GetBytes(&p_sys
->bytestream
, p_buf
, p_sys
->i_frame_size
);
1179 if (p_sys
->i_type
!= TYPE_ADTS
) { /* parse/extract the whole frame */
1180 assert(p_sys
->i_type
== TYPE_LOAS
);
1181 p_out_buffer
->i_buffer
= LOASParse(p_dec
, p_buf
, p_sys
->i_frame_size
);
1182 if (p_out_buffer
->i_buffer
<= 0)
1184 if (!p_sys
->b_latm_cfg
)
1185 msg_Warn(p_dec
, "waiting for header");
1187 block_Release(p_out_buffer
);
1188 p_out_buffer
= NULL
;
1189 p_sys
->i_state
= STATE_NOSYNC
;
1193 SetupOutput(p_dec
, p_out_buffer
);
1194 /* Make sure we don't reuse the same pts twice */
1195 if (p_sys
->i_pts
== p_sys
->bytestream
.p_block
->i_pts
)
1196 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
= VLC_TS_INVALID
;
1198 /* So p_block doesn't get re-added several times */
1200 *pp_block
= block_BytestreamPop(&p_sys
->bytestream
);
1202 p_sys
->i_state
= STATE_NOSYNC
;
1204 return p_out_buffer
;
1210 /****************************************************************************
1211 * Packetize: just forwards raw blocks, or packetizes LOAS/ADTS
1212 * and strips headers
1213 ****************************************************************************/
1214 static block_t
*Packetize(decoder_t
*p_dec
, block_t
**pp_block
)
1216 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1217 block_t
*p_block
= pp_block
? *pp_block
: NULL
;
1221 if (p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
))
1223 if(p_sys
->i_type
== TYPE_ADTS
|| p_sys
->i_type
== TYPE_LOAS
)
1225 /* First always drain complete blocks before discontinuity */
1226 block_t
*p_drain
= PacketizeStreamBlock(p_dec
, NULL
);
1233 if (p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
1235 block_Release(p_block
);
1240 if (!date_Get(&p_sys
->end_date
) && p_block
->i_pts
<= VLC_TS_INVALID
)
1242 /* We've just started the stream, wait for the first PTS. */
1243 block_Release(p_block
);
1248 if(p_block
&& p_sys
->i_type
== TYPE_UNKNOWN
)
1250 p_sys
->i_type
= TYPE_RAW
;
1251 if(p_block
->i_buffer
> 1)
1253 if(p_block
->p_buffer
[0] == 0xff && (p_block
->p_buffer
[1] & 0xf6) == 0xf0)
1255 p_sys
->i_type
= TYPE_ADTS
;
1257 else if(p_block
->p_buffer
[0] == 0x56 && (p_block
->p_buffer
[1] & 0xe0) == 0xe0)
1259 p_sys
->i_type
= TYPE_LOAS
;
1264 if(p_sys
->i_type
== TYPE_RAW
)
1265 p_block
= ForwardRawBlock(p_dec
, pp_block
);
1267 p_block
= PacketizeStreamBlock(p_dec
, pp_block
);
1269 if(p_block
&& p_sys
->b_discontuinity
)
1271 p_block
->i_flags
|= BLOCK_FLAG_DISCONTINUITY
;
1272 p_sys
->b_discontuinity
= false;