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 decoder_sys_t *p_sys = p_dec->p_sys;\
154 if( (p_sys->i_warnings & warn) == 0 )\
156 p_sys->i_warnings |= warn;\
157 msg_Warn( p_dec, msg );\
162 TYPE_UNKNOWN
, /* AAC samples with[out] headers */
163 TYPE_UNKNOWN_NONRAW
, /* [un]packetized ADTS or LOAS */
164 TYPE_RAW
, /* RAW AAC frames */
169 static const int pi_sample_rates
[16] =
171 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
172 16000, 12000, 11025, 8000, 7350, 0, 0, 0
175 #define ADTS_HEADER_SIZE 9
176 #define LOAS_HEADER_SIZE 3
178 /****************************************************************************
180 ****************************************************************************/
181 static int OpenPacketizer(vlc_object_t
*);
182 static void ClosePacketizer(vlc_object_t
*);
184 static block_t
*Packetize (decoder_t
*, block_t
**);
185 static void Flush( decoder_t
* );
187 static int Mpeg4ReadAudioSpecificConfig(bs_t
*s
, mpeg4_asc_t
*p_cfg
, bool);
189 /*****************************************************************************
191 *****************************************************************************/
193 set_category(CAT_SOUT
)
194 set_subcategory(SUBCAT_SOUT_PACKETIZER
)
195 set_description(N_("MPEG4 audio packetizer"))
196 set_capability("packetizer", 50)
197 set_callbacks(OpenPacketizer
, ClosePacketizer
)
200 /*****************************************************************************
201 * OpenPacketizer: probe the packetizer and return score
202 *****************************************************************************/
203 static int OpenPacketizer(vlc_object_t
*p_this
)
205 decoder_t
*p_dec
= (decoder_t
*)p_this
;
206 decoder_sys_t
*p_sys
;
208 if (p_dec
->fmt_in
.i_codec
!= VLC_CODEC_MP4A
)
211 /* Allocate the memory needed to store the decoder's structure */
212 if ((p_dec
->p_sys
= p_sys
= (decoder_sys_t
*)malloc(sizeof(decoder_sys_t
))) == NULL
)
216 p_sys
->i_state
= STATE_NOSYNC
;
217 p_sys
->b_discontuinity
= false;
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);
296 date_Set(&p_sys
->end_date
, VLC_TS_INVALID
);
299 p_dec
->pf_packetize
= Packetize
;
300 p_dec
->pf_flush
= Flush
;
305 /*****************************************************************************
306 * ClosePacketizer: clean up the packetizer
307 *****************************************************************************/
308 static void ClosePacketizer(vlc_object_t
*p_this
)
310 decoder_t
*p_dec
= (decoder_t
*)p_this
;
311 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
313 block_BytestreamRelease(&p_sys
->bytestream
);
317 /****************************************************************************
319 ****************************************************************************
320 * This function must be fed with complete frames.
321 ****************************************************************************/
322 static block_t
*ForwardRawBlock(decoder_t
*p_dec
, block_t
**pp_block
)
324 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
327 if (!pp_block
|| !*pp_block
)
331 *pp_block
= NULL
; /* Don't reuse this block */
333 if (p_block
->i_pts
!= VLC_TS_INVALID
&&
334 p_block
->i_pts
!= date_Get(&p_sys
->end_date
))
336 if(date_Get(&p_sys
->end_date
) != VLC_TS_INVALID
)
337 p_sys
->b_discontuinity
= true;
338 date_Set(&p_sys
->end_date
, p_block
->i_pts
);
341 p_block
->i_pts
= p_block
->i_dts
= date_Get(&p_sys
->end_date
);
343 /* Might not be known due to missing extradata,
344 will be set to block pts above */
345 if(p_dec
->fmt_out
.audio
.i_frame_length
)
346 p_block
->i_length
= date_Increment(&p_sys
->end_date
,
347 p_dec
->fmt_out
.audio
.i_frame_length
) - p_block
->i_pts
;
352 /****************************************************************************
354 ****************************************************************************/
355 static int ADTSSyncInfo(decoder_t
* p_dec
, const uint8_t * p_buf
,
356 unsigned int * pi_channels
,
357 unsigned int * pi_sample_rate
,
358 unsigned int * pi_frame_length
,
359 unsigned int * pi_header_size
)
361 int i_profile
, i_sample_rate_idx
, i_frame_size
;
364 /* Fixed header between frames */
365 //int i_id = ((p_buf[1] >> 3) & 0x01) ? 2 : 4; /* MPEG-2 or 4 */
366 b_crc
= !(p_buf
[1] & 0x01);
367 i_profile
= p_buf
[2] >> 6;
368 i_sample_rate_idx
= (p_buf
[2] >> 2) & 0x0f;
369 *pi_sample_rate
= pi_sample_rates
[i_sample_rate_idx
];
370 //private_bit = (p_buf[2] >> 1) & 0x01;
371 *pi_channels
= ((p_buf
[2] & 0x01) << 2) | ((p_buf
[3] >> 6) & 0x03);
372 if (*pi_channels
== 0) /* workaround broken streams */
374 //original_copy = (p_buf[3] >> 5) & 0x01;
375 //home = (p_buf[3] >> 4) & 0x01;
377 /* Variable header */
378 //copyright_id_bit = (p_buf[3] >> 3) & 0x01;
379 //copyright_id_start = (p_buf[3] >> 2) & 0x01;
380 i_frame_size
= ((p_buf
[3] & 0x03) << 11) | (p_buf
[4] << 3) |
381 ((p_buf
[5] >> 5) /*& 0x7*/);
382 //uint16_t buffer_fullness = ((p_buf[5] & 0x1f) << 6) | (p_buf[6] >> 2);
383 unsigned short i_raw_blocks_in_frame
= p_buf
[6] & 0x03;
385 if (!*pi_sample_rate
|| !i_frame_size
) {
386 msg_Warn(p_dec
, "Invalid ADTS header");
390 *pi_frame_length
= 1024;
392 if (i_raw_blocks_in_frame
== 0) {
394 WARN_ONCE(WARN_CRC_UNSUPPORTED
, "ADTS CRC not supported");
395 //uint16_t crc = (p_buf[7] << 8) | p_buf[8];
398 msg_Err(p_dec
, "Multiple blocks per frame in ADTS not supported");
402 const uint8_t *p_pos
= p_buf
+ 7;
404 uint16_t i_block_pos
[3];
406 for (i
= 0 ; i
< i_raw_blocks_in_frame
; i
++) {
407 /* the 1st block's position is known ... */
408 i_block_pos
[i
] = (*p_pos
<< 8) | *(p_pos
+1);
411 crc_block
= (*p_pos
<< 8) | *(p_pos
+1);
414 for (i
= 0 ; i
<= i_raw_blocks_in_frame
; i
++) {
417 WARN_ONCE(WARN_CRC_UNSUPPORTED
, "ADTS CRC not supported");
418 //uint16_t crc = (*p_pos << 8) | *(p_pos+1);
426 /* Build the decoder specific info header */
427 if (!p_dec
->fmt_out
.i_extra
) {
428 p_dec
->fmt_out
.p_extra
= malloc(2);
429 if (!p_dec
->fmt_out
.p_extra
) {
430 p_dec
->fmt_out
.i_extra
= 0;
433 p_dec
->fmt_out
.i_extra
= 2;
434 ((uint8_t *)p_dec
->fmt_out
.p_extra
)[0] =
435 (i_profile
+ 1) << 3 | (i_sample_rate_idx
>> 1);
436 ((uint8_t *)p_dec
->fmt_out
.p_extra
)[1] =
437 ((i_sample_rate_idx
& 0x01) << 7) | (*pi_channels
<<3);
440 /* ADTS header length */
441 *pi_header_size
= b_crc
? 9 : 7;
443 return i_frame_size
- *pi_header_size
;
446 /****************************************************************************
448 ****************************************************************************/
449 static int LOASSyncInfo(uint8_t p_header
[LOAS_HEADER_SIZE
], unsigned int *pi_header_size
)
452 return ((p_header
[1] & 0x1f) << 8) + p_header
[2];
455 static int Mpeg4GAProgramConfigElement(bs_t
*s
)
457 /* TODO compute channels count ? */
458 int i_tag
= bs_read(s
, 4);
461 bs_skip(s
, 2 + 4); // object type + sampling index
462 int i_num_front
= bs_read(s
, 4);
463 int i_num_side
= bs_read(s
, 4);
464 int i_num_back
= bs_read(s
, 4);
465 int i_num_lfe
= bs_read(s
, 2);
466 int i_num_assoc_data
= bs_read(s
, 3);
467 int i_num_valid_cc
= bs_read(s
, 4);
470 bs_skip(s
, 4); // mono downmix
472 bs_skip(s
, 4); // stereo downmix
474 bs_skip(s
, 2+1); // matrix downmix + pseudo_surround
476 bs_skip(s
, i_num_front
* (1+4));
477 bs_skip(s
, i_num_side
* (1+4));
478 bs_skip(s
, i_num_back
* (1+4));
479 bs_skip(s
, i_num_lfe
* (4));
480 bs_skip(s
, i_num_assoc_data
* (4));
481 bs_skip(s
, i_num_valid_cc
* (5));
483 int i_comment
= bs_read(s
, 8);
484 bs_skip(s
, i_comment
* 8);
488 static int Mpeg4GASpecificConfig(mpeg4_asc_t
*p_cfg
, bs_t
*s
)
490 p_cfg
->i_frame_length
= bs_read1(s
) ? 960 : 1024;
491 if(p_cfg
->i_object_type
== 23) /* 14496-3 4.5.1.1 */
492 p_cfg
->i_frame_length
>>= 1;
493 else if(p_cfg
->i_object_type
== 3)
494 p_cfg
->i_frame_length
= 256;
496 if (bs_read1(s
)) // depend on core coder
497 bs_skip(s
, 14); // core coder delay
499 int i_extension_flag
= bs_read1(s
);
500 if (p_cfg
->i_channel
== 0)
501 Mpeg4GAProgramConfigElement(s
);
502 if (p_cfg
->i_object_type
== 6 || p_cfg
->i_object_type
== 20)
503 bs_skip(s
, 3); // layer
505 if (i_extension_flag
) {
506 if (p_cfg
->i_object_type
== 22)
507 bs_skip(s
, 5 + 11); // numOfSubFrame + layer length
508 if (p_cfg
->i_object_type
== 17 || p_cfg
->i_object_type
== 19 ||
509 p_cfg
->i_object_type
== 20 || p_cfg
->i_object_type
== 23)
510 bs_skip(s
, 1+1+1); // ER data : section scale spectral */
511 if (bs_read1(s
)) // extension 3
512 fprintf(stderr
, "Mpeg4GASpecificConfig: error 1\n");
517 static int Mpeg4ReadAudioObjectType(bs_t
*s
)
519 int i_type
= bs_read(s
, 5);
521 i_type
= 32 + bs_read(s
, 6);
525 static int Mpeg4ReadAudioSamplerate(bs_t
*s
)
527 int i_index
= bs_read(s
, 4);
529 return pi_sample_rates
[i_index
];
530 return bs_read(s
, 24);
533 static int Mpeg4ReadAudioChannelConfiguration(bs_t
*s
)
535 int i_channel
= bs_read(s
, 4);
537 i_channel
= 8; // 7.1
538 else if (i_channel
>= 8)
543 static int Mpeg4ReadAudioSpecificConfig(bs_t
*s
, mpeg4_asc_t
*p_cfg
, bool b_withext
)
546 static const char *ppsz_otype
[] = {
548 "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
551 "Reserved", "Reserved",
553 "Main Synthetic", "Wavetables Synthesis", "General MIDI",
554 "Algorithmic Synthesis and Audio FX",
557 "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
558 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
560 "PS", "Reserved", "Escape",
561 "Layer 1", "Layer 2", "Layer 3",
565 memset(p_cfg
, 0, sizeof(*p_cfg
));
567 p_cfg
->i_object_type
= Mpeg4ReadAudioObjectType(s
);
568 p_cfg
->i_samplerate
= Mpeg4ReadAudioSamplerate(s
);
569 p_cfg
->i_channel
= Mpeg4ReadAudioChannelConfiguration(s
);
573 p_cfg
->extension
.i_object_type
= 0;
574 p_cfg
->extension
.i_samplerate
= 0;
575 p_cfg
->extension
.i_channel
= -1;
576 if (p_cfg
->i_object_type
== 5 || p_cfg
->i_object_type
== 29) {
578 if (p_cfg
->i_object_type
== 29)
580 p_cfg
->extension
.i_object_type
= 5;
581 p_cfg
->extension
.i_samplerate
= Mpeg4ReadAudioSamplerate(s
);
583 p_cfg
->i_object_type
= Mpeg4ReadAudioObjectType(s
);
584 if(p_cfg
->i_object_type
== 22)
585 p_cfg
->extension
.i_channel
= Mpeg4ReadAudioChannelConfiguration(s
);
588 switch(p_cfg
->i_object_type
)
590 case 1: case 2: case 3: case 4:
592 case 17: case 19: case 20: case 21: case 22: case 23:
593 Mpeg4GASpecificConfig(p_cfg
, s
);
596 // CelpSpecificConfig();
598 // HvxcSpecificConfig();
600 // TTSSSpecificConfig();
601 case 13: case 14: case 15: case 16:
602 // StructuredAudioSpecificConfig();
604 // ERCelpSpecificConfig();
606 // ERHvxcSpecificConfig();
608 // ParametricSpecificConfig();
610 // SSCSpecificConfig();
611 case 32: case 33: case 34:
612 // MPEG_1_2_SpecificConfig();
614 // DSTSpecificConfig();
616 // ALSSpecificConfig();
618 // SLSSpecificConfig();
620 // ELDSpecificConfig();
622 // SymbolicMusicSpecificConfig();
628 switch(p_cfg
->i_object_type
)
630 case 17: case 19: case 20: case 21: case 22: case 23:
631 case 24: case 25: case 26: case 27: case 39:
633 int epConfig
= bs_read(s
, 2);
634 if (epConfig
== 2 || epConfig
== 3)
635 //ErrorProtectionSpecificConfig();
638 // TODO : directMapping
646 if (b_withext
&& p_cfg
->extension
.i_object_type
!= 5 && bs_remain(s
) >= 16 &&
647 bs_read(s
, 11) == 0x2b7) {
648 p_cfg
->extension
.i_object_type
= Mpeg4ReadAudioObjectType(s
);
649 if (p_cfg
->extension
.i_object_type
== 5)
651 p_cfg
->i_sbr
= bs_read1(s
);
652 if (p_cfg
->i_sbr
== 1) {
653 p_cfg
->extension
.i_samplerate
= Mpeg4ReadAudioSamplerate(s
);
654 if (bs_remain(s
) >= 12 && bs_read(s
, 11) == 0x548)
655 p_cfg
->i_ps
= bs_read1(s
);
658 else if (p_cfg
->extension
.i_object_type
== 22)
660 p_cfg
->i_sbr
= bs_read1(s
);
662 p_cfg
->extension
.i_samplerate
= Mpeg4ReadAudioSamplerate(s
);
663 p_cfg
->extension
.i_channel
= Mpeg4ReadAudioChannelConfiguration(s
);
667 //fprintf(stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
668 // ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr);
673 static uint32_t LatmGetValue(bs_t
*s
)
676 for (int i
= 1 + bs_read(s
, 2); i
> 0; i
--)
677 v
= (v
<< 8) + bs_read(s
, 8);
681 static uint32_t AudioSpecificConfigBitsToBytes(bs_t
*s
, uint32_t i_bits
, uint8_t *p_data
)
683 uint32_t i_extra
= __MIN((i_bits
+ 7) / 8, LATM_MAX_EXTRA_SIZE
);
684 for (uint32_t i
= 0; i
< i_extra
; i
++) {
685 const uint32_t i_read
= __MIN(8, i_bits
- 8*i
);
686 p_data
[i
] = bs_read(s
, i_read
) << (8-i_read
);
691 static int LatmReadStreamMuxConfiguration(latm_mux_t
*m
, bs_t
*s
)
696 i_mux_version
= bs_read(s
, 1);
699 i_mux_versionA
= bs_read(s
, 1);
701 if (i_mux_versionA
!= 0) /* support only A=0 */
704 memset(m
, 0, sizeof(*m
));
706 if (i_mux_versionA
== 0)
707 if (i_mux_version
== 1)
708 LatmGetValue(s
); /* taraBufferFullness */
710 m
->b_same_time_framing
= bs_read1(s
);
711 m
->i_sub_frames
= 1 + bs_read(s
, 6);
712 m
->i_programs
= 1 + bs_read(s
, 4);
714 for (int i_program
= 0; i_program
< m
->i_programs
; i_program
++) {
715 m
->pi_layers
[i_program
] = 1+bs_read(s
, 3);
717 for (int i_layer
= 0; i_layer
< m
->pi_layers
[i_program
]; i_layer
++) {
718 latm_stream_t
*st
= &m
->stream
[m
->i_streams
];
721 m
->pi_stream
[i_program
][i_layer
] = m
->i_streams
;
722 st
->i_program
= i_program
;
723 st
->i_layer
= i_layer
;
725 b_previous_cfg
= false;
726 if (i_program
!= 0 || i_layer
!= 0)
727 b_previous_cfg
= bs_read1(s
);
729 if (b_previous_cfg
) {
730 assert(m
->i_streams
> 0);
731 st
->cfg
= m
->stream
[m
->i_streams
-1].cfg
;
733 uint32_t asc_size
= 0;
734 if(i_mux_version
> 0)
735 asc_size
= LatmGetValue(s
);
737 Mpeg4ReadAudioSpecificConfig(&asc_bs
, &st
->cfg
, i_mux_version
> 0);
738 if (i_mux_version
== 0)
739 asc_size
= bs_pos(&asc_bs
) - bs_pos(s
);
741 st
->i_extra
= AudioSpecificConfigBitsToBytes(&asc_bs
, asc_size
, st
->extra
);
742 bs_skip(s
, asc_size
);
745 st
->i_frame_length_type
= bs_read(s
, 3);
746 switch(st
->i_frame_length_type
)
750 bs_skip(s
, 8); /* latmBufferFullnes */
751 if (!m
->b_same_time_framing
)
752 if (st
->cfg
.i_object_type
== 6 || st
->cfg
.i_object_type
== 20 ||
753 st
->cfg
.i_object_type
== 8 || st
->cfg
.i_object_type
== 24)
754 bs_skip(s
, 6); /* eFrameOffset */
758 st
->i_frame_length
= bs_read(s
, 9);
760 case 3: case 4: case 5:
761 st
->i_frame_length_index
= bs_read(s
, 6); // celp
764 st
->i_frame_length_index
= bs_read(s
, 1); // hvxc
775 if (i_mux_version
== 1)
776 m
->i_other_data
= LatmGetValue(s
);
780 b_continue
= bs_read1(s
);
781 m
->i_other_data
= (m
->i_other_data
<< 8) + bs_read(s
, 8);
782 } while (b_continue
);
789 m
->i_crc
= bs_read(s
, 8);
794 static int LOASParse(decoder_t
*p_dec
, uint8_t *p_buffer
, int i_buffer
)
796 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
798 int i_accumulated
= 0;
800 bs_init(&s
, p_buffer
, i_buffer
);
802 /* Read the stream mux configuration if present */
803 if (!bs_read1(&s
) && !LatmReadStreamMuxConfiguration(&p_sys
->latm
, &s
) &&
804 p_sys
->latm
.i_streams
> 0) {
805 const latm_stream_t
*st
= &p_sys
->latm
.stream
[0];
807 if(st
->cfg
.i_samplerate
<= 0 || st
->cfg
.i_channel
<=0 || st
->cfg
.i_frame_length
<= 0)
810 p_sys
->i_channels
= st
->cfg
.i_channel
;
811 p_sys
->i_rate
= st
->cfg
.i_samplerate
;
812 p_sys
->i_frame_length
= st
->cfg
.i_frame_length
;
814 if (p_sys
->i_channels
&& p_sys
->i_rate
&& p_sys
->i_frame_length
> 0)
816 if(p_dec
->fmt_out
.i_extra
!= st
->i_extra
||
817 (p_dec
->fmt_out
.i_extra
> 0 &&
818 memcmp(p_dec
->fmt_out
.p_extra
, st
->extra
, st
->i_extra
)) )
820 if(p_dec
->fmt_out
.i_extra
)
821 free(p_dec
->fmt_out
.p_extra
);
822 p_dec
->fmt_out
.p_extra
= malloc(st
->i_extra
);
823 if(p_dec
->fmt_out
.p_extra
)
825 p_dec
->fmt_out
.i_extra
= st
->i_extra
;
826 memcpy(p_dec
->fmt_out
.p_extra
, st
->extra
, st
->i_extra
);
827 p_sys
->b_latm_cfg
= true;
831 p_dec
->fmt_out
.i_extra
= 0;
832 p_sys
->b_latm_cfg
= false;
838 /* Wait for the configuration */
839 if (!p_sys
->b_latm_cfg
)
841 /* WAVE_FORMAT_MPEG_LOAS, configuration provided as AAC header :/ */
842 if( p_dec
->fmt_in
.i_extra
> 0 &&
843 p_sys
->i_channels
&& p_sys
->i_rate
&& p_sys
->i_frame_length
)
845 p_sys
->b_latm_cfg
= true;
850 /* FIXME do we need to split the subframe into independent packet ? */
851 if (p_sys
->latm
.i_sub_frames
> 1)
852 msg_Err(p_dec
, "latm sub frames not yet supported, please send a sample");
854 for (int i_sub
= 0; i_sub
< p_sys
->latm
.i_sub_frames
; i_sub
++) {
855 int pi_payload
[LATM_MAX_PROGRAM
][LATM_MAX_LAYER
];
856 if (p_sys
->latm
.b_same_time_framing
) {
858 for (int i_program
= 0; i_program
< p_sys
->latm
.i_programs
; i_program
++) {
859 for (int i_layer
= 0; i_layer
< p_sys
->latm
.pi_layers
[i_program
]; i_layer
++) {
860 latm_stream_t
*st
= &p_sys
->latm
.stream
[p_sys
->latm
.pi_stream
[i_program
][i_layer
]];
861 if (st
->i_frame_length_type
== 0) {
864 int i_tmp
= bs_read(&s
, 8);
869 pi_payload
[i_program
][i_layer
] = i_payload
;
870 } else if (st
->i_frame_length_type
== 1) {
871 pi_payload
[i_program
][i_layer
] = st
->i_frame_length
/ 8; /* XXX not correct */
872 } else if ((st
->i_frame_length_type
== 3) ||
873 (st
->i_frame_length_type
== 5) ||
874 (st
->i_frame_length_type
== 7)) {
875 bs_skip(&s
, 2); // muxSlotLengthCoded
876 pi_payload
[i_program
][i_layer
] = 0; /* TODO */
878 pi_payload
[i_program
][i_layer
] = 0; /* TODO */
884 for (int i_program
= 0; i_program
< p_sys
->latm
.i_programs
; i_program
++) {
885 for (int i_layer
= 0; i_layer
< p_sys
->latm
.pi_layers
[i_program
]; i_layer
++) {
886 /* XXX we only extract 1 stream */
887 if (i_program
!= 0 || i_layer
!= 0)
890 if (pi_payload
[i_program
][i_layer
] <= 0)
893 /* FIXME that's slow (and a bit ugly to write in place) */
894 for (int i
= 0; i
< pi_payload
[i_program
][i_layer
]; i
++) {
895 if (i_accumulated
>= i_buffer
)
897 p_buffer
[i_accumulated
++] = bs_read(&s
, 8);
902 const int i_chunks
= bs_read(&s
, 4);
908 msg_Err(p_dec
, "latm without same time frameing not yet supported, please send a sample");
910 for (int i_chunk
= 0; i_chunk
< i_chunks
; i_chunk
++) {
911 const int streamIndex
= bs_read(&s
, 4);
912 latm_stream_t
*st
= &p_sys
->latm
.stream
[streamIndex
];
913 const int i_program
= st
->i_program
;
914 const int i_layer
= st
->i_layer
;
917 pi_program
[i_chunk
] = i_program
;
918 pi_layer
[i_chunk
] = i_layer
;
921 if (st
->i_frame_length_type
== 0) {
924 int i_tmp
= bs_read(&s
, 8);
929 pi_payload
[i_program
][i_layer
] = i_payload
;
930 bs_skip(&s
, 1); // auEndFlag
931 } else if (st
->i_frame_length_type
== 1) {
932 pi_payload
[i_program
][i_layer
] = st
->i_frame_length
/ 8; /* XXX not correct */
933 } else if ((st
->i_frame_length_type
== 3) ||
934 (st
->i_frame_length_type
== 5) ||
935 (st
->i_frame_length_type
== 7)) {
936 bs_read(&s
, 2); // muxSlotLengthCoded
940 for (int i_chunk
= 0; i_chunk
< i_chunks
; i_chunk
++) {
941 //const int i_program = pi_program[i_chunk];
942 //const int i_layer = pi_layer[i_chunk];
951 if (p_sys
->latm
.i_other_data
> 0)
956 return i_accumulated
;
959 /*****************************************************************************
961 *****************************************************************************/
962 static void SetupOutput(decoder_t
*p_dec
, block_t
*p_block
)
964 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
966 if (p_dec
->fmt_out
.audio
.i_rate
!= p_sys
->i_rate
&& p_sys
->i_rate
> 0)
968 msg_Info(p_dec
, "AAC channels: %d samplerate: %d",
969 p_sys
->i_channels
, p_sys
->i_rate
);
970 date_Change(&p_sys
->end_date
, p_sys
->i_rate
, 1);
973 p_dec
->fmt_out
.audio
.i_rate
= p_sys
->i_rate
;
974 p_dec
->fmt_out
.audio
.i_channels
= p_sys
->i_channels
;
975 p_dec
->fmt_out
.audio
.i_bytes_per_frame
= p_sys
->i_frame_size
;
976 p_dec
->fmt_out
.audio
.i_frame_length
= p_sys
->i_frame_length
;
979 p_dec
->fmt_out
.audio
.i_physical_channels
= p_sys
->i_channels_conf
;
982 p_block
->i_pts
= p_block
->i_dts
= date_Get(&p_sys
->end_date
);
985 date_Increment(&p_sys
->end_date
, p_sys
->i_frame_length
) - p_block
->i_pts
;
988 /*****************************************************************************
990 *****************************************************************************/
991 static void Flush(decoder_t
*p_dec
)
993 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
995 p_sys
->i_state
= STATE_NOSYNC
;
996 block_BytestreamEmpty(&p_sys
->bytestream
);
997 date_Set(&p_sys
->end_date
, VLC_TS_INVALID
);
998 p_sys
->b_discontuinity
= true;
1001 static inline bool HasADTSHeader( const uint8_t *p_header
)
1003 return p_header
[0] == 0xff && (p_header
[1] & 0xf6) == 0xf0;
1006 static inline bool HasLoasHeader( const uint8_t *p_header
)
1008 return p_header
[0] == 0x56 && (p_header
[1] & 0xe0) == 0xe0;
1011 /****************************************************************************
1012 * PacketizeStreamBlock: ADTS/LOAS packetizer
1013 ****************************************************************************/
1014 static block_t
*PacketizeStreamBlock(decoder_t
*p_dec
, block_t
**pp_block
)
1016 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1017 uint8_t p_header
[ADTS_HEADER_SIZE
+ LOAS_HEADER_SIZE
];
1018 block_t
*p_out_buffer
;
1021 block_t
*p_block
= pp_block
? *pp_block
: NULL
;
1025 block_BytestreamPush(&p_sys
->bytestream
, p_block
);
1029 for (;;) switch(p_sys
->i_state
) {
1031 while (block_PeekBytes(&p_sys
->bytestream
, p_header
, 2) == VLC_SUCCESS
) {
1032 /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
1033 if ((p_sys
->i_type
== TYPE_ADTS
|| p_sys
->i_type
== TYPE_UNKNOWN_NONRAW
) &&
1034 HasADTSHeader( p_header
) )
1036 if (p_sys
->i_type
!= TYPE_ADTS
)
1037 msg_Dbg(p_dec
, "detected ADTS format");
1039 p_sys
->i_state
= STATE_SYNC
;
1040 p_sys
->i_type
= TYPE_ADTS
;
1043 else if ((p_sys
->i_type
== TYPE_LOAS
|| p_sys
->i_type
== TYPE_UNKNOWN_NONRAW
) &&
1044 HasLoasHeader( p_header
) )
1046 if (p_sys
->i_type
!= TYPE_LOAS
)
1047 msg_Dbg(p_dec
, "detected LOAS format");
1049 p_sys
->i_state
= STATE_SYNC
;
1050 p_sys
->i_type
= TYPE_LOAS
;
1053 block_SkipByte(&p_sys
->bytestream
);
1055 if (p_sys
->i_state
!= STATE_SYNC
) {
1056 block_BytestreamFlush(&p_sys
->bytestream
);
1058 /* Need more data */
1064 /* New frame, set the Presentation Time Stamp */
1065 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
;
1066 if (p_sys
->i_pts
!= VLC_TS_INVALID
&&
1067 p_sys
->i_pts
!= date_Get(&p_sys
->end_date
))
1068 date_Set(&p_sys
->end_date
, p_sys
->i_pts
);
1069 p_sys
->i_state
= STATE_HEADER
;
1073 if (p_sys
->i_type
== TYPE_ADTS
) {
1074 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
1075 if (block_PeekBytes(&p_sys
->bytestream
, p_header
,
1076 ADTS_HEADER_SIZE
) != VLC_SUCCESS
)
1077 return NULL
; /* Need more data */
1079 /* Check if frame is valid and get frame info */
1080 p_sys
->i_frame_size
= ADTSSyncInfo(p_dec
, p_header
,
1083 &p_sys
->i_frame_length
,
1084 &p_sys
->i_header_size
);
1086 assert(p_sys
->i_type
== TYPE_LOAS
);
1087 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
1088 if (block_PeekBytes(&p_sys
->bytestream
, p_header
,
1089 LOAS_HEADER_SIZE
) != VLC_SUCCESS
)
1090 return NULL
; /* Need more data */
1092 /* Check if frame is valid and get frame info */
1093 p_sys
->i_frame_size
= LOASSyncInfo(p_header
, &p_sys
->i_header_size
);
1096 if (p_sys
->i_frame_size
<= 0) {
1097 msg_Dbg(p_dec
, "emulated sync word");
1098 block_SkipByte(&p_sys
->bytestream
);
1099 p_sys
->i_state
= STATE_NOSYNC
;
1103 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
);
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
;
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
;
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
);
1150 p_sys
->i_state
= STATE_SEND_DATA
;
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
;
1162 case STATE_SEND_DATA
:
1163 /* When we reach this point we already know we have enough
1164 * data available. */
1166 p_out_buffer
= block_Alloc(p_sys
->i_frame_size
);
1167 if (!p_out_buffer
) {
1170 p_buf
= p_out_buffer
->p_buffer
;
1172 /* Skip the ADTS/LOAS header */
1173 block_SkipBytes(&p_sys
->bytestream
, p_sys
->i_header_size
);
1175 /* Copy the whole frame into the buffer */
1176 block_GetBytes(&p_sys
->bytestream
, p_buf
, p_sys
->i_frame_size
);
1177 if (p_sys
->i_type
!= TYPE_ADTS
) { /* parse/extract the whole frame */
1178 assert(p_sys
->i_type
== TYPE_LOAS
);
1179 p_out_buffer
->i_buffer
= LOASParse(p_dec
, p_buf
, p_sys
->i_frame_size
);
1180 if (p_out_buffer
->i_buffer
<= 0)
1182 if (!p_sys
->b_latm_cfg
)
1183 msg_Warn(p_dec
, "waiting for header");
1185 block_Release(p_out_buffer
);
1186 p_out_buffer
= NULL
;
1187 p_sys
->i_state
= STATE_NOSYNC
;
1191 SetupOutput(p_dec
, p_out_buffer
);
1192 /* Make sure we don't reuse the same pts twice */
1193 if (p_sys
->i_pts
== p_sys
->bytestream
.p_block
->i_pts
)
1194 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
= VLC_TS_INVALID
;
1196 /* So p_block doesn't get re-added several times */
1198 *pp_block
= block_BytestreamPop(&p_sys
->bytestream
);
1200 p_sys
->i_state
= STATE_NOSYNC
;
1202 return p_out_buffer
;
1208 /****************************************************************************
1209 * Packetize: just forwards raw blocks, or packetizes LOAS/ADTS
1210 * and strips headers
1211 ****************************************************************************/
1212 static block_t
*Packetize(decoder_t
*p_dec
, block_t
**pp_block
)
1214 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1215 block_t
*p_block
= pp_block
? *pp_block
: NULL
;
1219 if (p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
))
1221 if(p_sys
->i_type
== TYPE_ADTS
|| p_sys
->i_type
== TYPE_LOAS
)
1223 /* First always drain complete blocks before discontinuity */
1224 block_t
*p_drain
= PacketizeStreamBlock(p_dec
, NULL
);
1231 if (p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
1233 block_Release(p_block
);
1238 if ( p_block
->i_pts
== VLC_TS_INVALID
&&
1239 date_Get(&p_sys
->end_date
) == VLC_TS_INVALID
)
1241 /* We've just started the stream, wait for the first PTS. */
1242 block_Release(p_block
);
1247 if(p_block
&& p_sys
->i_type
== TYPE_UNKNOWN
)
1249 p_sys
->i_type
= TYPE_RAW
;
1250 if(p_block
->i_buffer
> 1)
1252 if(p_block
->p_buffer
[0] == 0xff && (p_block
->p_buffer
[1] & 0xf6) == 0xf0)
1254 p_sys
->i_type
= TYPE_ADTS
;
1256 else if(p_block
->p_buffer
[0] == 0x56 && (p_block
->p_buffer
[1] & 0xe0) == 0xe0)
1258 p_sys
->i_type
= TYPE_LOAS
;
1263 if(p_sys
->i_type
== TYPE_RAW
)
1264 p_block
= ForwardRawBlock(p_dec
, pp_block
);
1266 p_block
= PacketizeStreamBlock(p_dec
, pp_block
);
1268 if(p_block
&& p_sys
->b_discontuinity
)
1270 p_block
->i_flags
|= BLOCK_FLAG_DISCONTINUITY
;
1271 p_sys
->b_discontuinity
= false;