1 /*****************************************************************************
2 * mpeg4audio.c: parse and packetize an MPEG 4 audio stream
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
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
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
35 #include <vlc_codec.h>
36 #include <vlc_block.h>
38 #include <vlc_codecs.h>
39 #include <vlc_input.h>
42 #include "vlc_block_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
74 int i_frame_length
; // 1024 or 960
78 #define LATM_MAX_EXTRA_SIZE 64
84 int i_frame_length_type
;
85 int i_frame_length
; // type 1
86 int i_frame_length_index
; // type 3 4 5 6 7
90 /* Raw configuration */
92 uint8_t extra
[LATM_MAX_EXTRA_SIZE
];
96 #define LATM_MAX_LAYER (8)
97 #define LATM_MAX_PROGRAM (16)
100 int b_same_time_framing
;
104 int pi_layers
[LATM_MAX_PROGRAM
];
106 int pi_stream
[LATM_MAX_PROGRAM
][LATM_MAX_LAYER
];
109 latm_stream_t stream
[LATM_MAX_PROGRAM
*LATM_MAX_LAYER
];
112 int i_crc
; /* -1 if not set */
123 block_bytestream_t bytestream
;
128 audio_date_t end_date
;
132 unsigned int i_channels
;
133 unsigned int i_rate
, i_frame_length
, i_header_size
;
138 vlc_bool_t b_latm_cfg
;
159 static const int pi_sample_rates
[16] =
161 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
162 16000, 12000, 11025, 8000, 7350, 0, 0, 0
165 #define ADTS_HEADER_SIZE 9
166 #define LOAS_HEADER_SIZE 3
168 /****************************************************************************
170 ****************************************************************************/
171 static int OpenPacketizer( vlc_object_t
* );
172 static void ClosePacketizer( vlc_object_t
* );
174 static block_t
*PacketizeRawBlock ( decoder_t
*, block_t
** );
175 static block_t
*PacketizeStreamBlock( decoder_t
*, block_t
** );
177 /*****************************************************************************
179 *****************************************************************************/
181 set_category( CAT_SOUT
);
182 set_subcategory( SUBCAT_SOUT_PACKETIZER
);
183 set_description( _("MPEG4 audio packetizer") );
184 set_capability( "packetizer", 50 );
185 set_callbacks( OpenPacketizer
, ClosePacketizer
);
188 /*****************************************************************************
189 * OpenPacketizer: probe the packetizer and return score
190 *****************************************************************************/
191 static int OpenPacketizer( vlc_object_t
*p_this
)
193 decoder_t
*p_dec
= (decoder_t
*)p_this
;
194 decoder_sys_t
*p_sys
;
196 if( p_dec
->fmt_in
.i_codec
!= VLC_FOURCC( 'm', 'p', '4', 'a' ) )
201 /* Allocate the memory needed to store the decoder's structure */
202 if( ( p_dec
->p_sys
= p_sys
=
203 (decoder_sys_t
*)malloc(sizeof(decoder_sys_t
)) ) == NULL
)
205 msg_Err( p_dec
, "out of memory" );
210 p_sys
->i_state
= STATE_NOSYNC
;
211 aout_DateSet( &p_sys
->end_date
, 0 );
212 p_sys
->bytestream
= block_BytestreamInit( p_dec
);
213 p_sys
->i_input_rate
= INPUT_RATE_DEFAULT
;
214 p_sys
->b_latm_cfg
= VLC_FALSE
;
216 /* Set output properties */
217 p_dec
->fmt_out
.i_cat
= AUDIO_ES
;
218 p_dec
->fmt_out
.i_codec
= VLC_FOURCC('m','p','4','a');
220 msg_Dbg( p_dec
, "running MPEG4 audio packetizer" );
222 if( p_dec
->fmt_in
.i_extra
> 0 )
224 uint8_t *p_config
= (uint8_t*)p_dec
->fmt_in
.p_extra
;
227 i_index
= ( ( p_config
[0] << 1 ) | ( p_config
[1] >> 7 ) ) & 0x0f;
228 if( i_index
!= 0x0f )
230 p_dec
->fmt_out
.audio
.i_rate
= pi_sample_rates
[i_index
];
231 p_dec
->fmt_out
.audio
.i_frame_length
=
232 (( p_config
[1] >> 2 ) & 0x01) ? 960 : 1024;
236 p_dec
->fmt_out
.audio
.i_rate
= ( ( p_config
[1] & 0x7f ) << 17 ) |
237 ( p_config
[2] << 9 ) | ( p_config
[3] << 1 ) |
238 ( p_config
[4] >> 7 );
239 p_dec
->fmt_out
.audio
.i_frame_length
=
240 (( p_config
[4] >> 2 ) & 0x01) ? 960 : 1024;
243 msg_Dbg( p_dec
, "AAC %dHz %d samples/frame",
244 p_dec
->fmt_out
.audio
.i_rate
,
245 p_dec
->fmt_out
.audio
.i_frame_length
);
247 aout_DateInit( &p_sys
->end_date
, p_dec
->fmt_out
.audio
.i_rate
);
249 p_dec
->fmt_out
.audio
.i_channels
= p_dec
->fmt_in
.audio
.i_channels
;
250 p_dec
->fmt_out
.i_extra
= p_dec
->fmt_in
.i_extra
;
251 p_dec
->fmt_out
.p_extra
= malloc( p_dec
->fmt_in
.i_extra
);
252 memcpy( p_dec
->fmt_out
.p_extra
, p_dec
->fmt_in
.p_extra
,
253 p_dec
->fmt_in
.i_extra
);
256 p_dec
->pf_packetize
= PacketizeRawBlock
;
257 p_sys
->i_type
= TYPE_RAW
;
261 msg_Dbg( p_dec
, "no decoder specific info, must be an ADTS or LOAS stream" );
263 aout_DateInit( &p_sys
->end_date
, p_dec
->fmt_in
.audio
.i_rate
);
265 /* We will try to create a AAC Config from adts/loas */
266 p_dec
->fmt_out
.i_extra
= 0;
267 p_dec
->fmt_out
.p_extra
= NULL
;
270 p_dec
->pf_packetize
= PacketizeStreamBlock
;
271 p_sys
->i_type
= TYPE_NONE
;
277 /****************************************************************************
278 * PacketizeRawBlock: the whole thing
279 ****************************************************************************
280 * This function must be fed with complete frames.
281 ****************************************************************************/
282 static block_t
*PacketizeRawBlock( decoder_t
*p_dec
, block_t
**pp_block
)
284 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
287 if( !pp_block
|| !*pp_block
) return NULL
;
289 if( (*pp_block
)->i_flags
&(BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
291 //aout_DateSet( &p_sys->end_date, 0 );
292 block_Release( *pp_block
);
297 *pp_block
= NULL
; /* Don't reuse this block */
299 if( !aout_DateGet( &p_sys
->end_date
) && !p_block
->i_pts
)
301 /* We've just started the stream, wait for the first PTS. */
302 block_Release( p_block
);
305 else if( p_block
->i_pts
!= 0 &&
306 p_block
->i_pts
!= aout_DateGet( &p_sys
->end_date
) )
308 aout_DateSet( &p_sys
->end_date
, p_block
->i_pts
);
311 p_block
->i_pts
= p_block
->i_dts
= aout_DateGet( &p_sys
->end_date
);
313 p_block
->i_length
= aout_DateIncrement( &p_sys
->end_date
,
314 p_dec
->fmt_out
.audio
.i_frame_length
* p_sys
->i_input_rate
/ INPUT_RATE_DEFAULT
) - p_block
->i_pts
;
319 /****************************************************************************
321 ****************************************************************************/
322 static int ADTSSyncInfo( decoder_t
* p_dec
, const byte_t
* p_buf
,
323 unsigned int * pi_channels
,
324 unsigned int * pi_sample_rate
,
325 unsigned int * pi_frame_length
,
326 unsigned int * pi_header_size
)
328 int i_id
, i_profile
, i_sample_rate_idx
, i_frame_size
;
331 /* Fixed header between frames */
332 i_id
= ( (p_buf
[1] >> 3) & 0x01 ) ? 2 : 4;
333 b_crc
= !(p_buf
[1] & 0x01);
334 i_profile
= p_buf
[2] >> 6;
335 i_sample_rate_idx
= (p_buf
[2] >> 2) & 0x0f;
336 *pi_sample_rate
= pi_sample_rates
[i_sample_rate_idx
];
337 *pi_channels
= ((p_buf
[2] & 0x01) << 2) | ((p_buf
[3] >> 6) & 0x03);
339 /* Variable header */
340 i_frame_size
= ((p_buf
[3] & 0x03) << 11) | (p_buf
[4] << 3) |
341 ((p_buf
[5] >> 5) & 0x7);
342 //i_raw_blocks_in_frame = (p_buf[6] & 0x02) + 1;
344 if( !*pi_sample_rate
|| !*pi_channels
|| !i_frame_size
)
350 *pi_frame_length
= 1024;
352 /* Build the decoder specific info header */
353 if( !p_dec
->fmt_out
.i_extra
)
355 p_dec
->fmt_out
.i_extra
= 2;
356 p_dec
->fmt_out
.p_extra
= malloc( 2 );
357 ((uint8_t *)p_dec
->fmt_out
.p_extra
)[0] =
358 (i_profile
+ 1) << 3 | (i_sample_rate_idx
>> 1);
359 ((uint8_t *)p_dec
->fmt_out
.p_extra
)[1] =
360 ((i_sample_rate_idx
& 0x01) << 7) | (*pi_channels
<<3);
363 /* ADTS header length */
364 *pi_header_size
= b_crc
? 9 : 7;
366 return i_frame_size
- *pi_header_size
;
368 /****************************************************************************
370 ****************************************************************************/
371 static int LOASSyncInfo( decoder_t
*p_dec
, uint8_t p_header
[LOAS_HEADER_SIZE
], unsigned int *pi_header_size
)
374 return ( ( p_header
[1] & 0x1f ) << 8 ) + p_header
[2];
376 static int Mpeg4GAProgramConfigElement( bs_t
*s
)
378 /* TODO compute channels count ? */
379 int i_tag
= bs_read( s
, 4 );
382 bs_skip( s
, 2 + 4 ); // object type + sampling index
383 int i_num_front
= bs_read( s
, 4 );
384 int i_num_side
= bs_read( s
, 4 );
385 int i_num_back
= bs_read( s
, 4 );
386 int i_num_lfe
= bs_read( s
, 2 );
387 int i_num_assoc_data
= bs_read( s
, 3 );
388 int i_num_valid_cc
= bs_read( s
, 4 );
391 bs_skip( s
, 4 ); // mono downmix
393 bs_skip( s
, 4 ); // stereo downmix
395 bs_skip( s
, 2+1 ); // matrix downmix + pseudo_surround
397 bs_skip( s
, i_num_front
* (1+4) );
398 bs_skip( s
, i_num_side
* (1+4) );
399 bs_skip( s
, i_num_back
* (1+4) );
400 bs_skip( s
, i_num_lfe
* (4) );
401 bs_skip( s
, i_num_assoc_data
* (4) );
402 bs_skip( s
, i_num_valid_cc
* (5) );
404 int i_comment
= bs_read( s
, 8 );
405 bs_skip( s
, i_comment
* 8 );
408 static int Mpeg4GASpecificConfig( mpeg4_cfg_t
*p_cfg
, bs_t
*s
)
410 p_cfg
->i_frame_length
= bs_read1(s
) ? 960 : 1024;
412 if( bs_read1( s
) ) // depend on core coder
413 bs_skip( s
, 14 ); // core coder delay
415 int i_extension_flag
= bs_read1( s
);
416 if( p_cfg
->i_channel
== 0 )
418 Mpeg4GAProgramConfigElement( s
);
420 if( p_cfg
->i_object_type
== 6 || p_cfg
->i_object_type
== 20 )
421 bs_skip( s
, 3 ); // layer
423 if( i_extension_flag
)
425 if( p_cfg
->i_object_type
== 22 )
427 bs_skip( s
, 5 + 11 ); // numOfSubFrame + layer length
429 if( p_cfg
->i_object_type
== 17 || p_cfg
->i_object_type
== 19 ||
430 p_cfg
->i_object_type
== 20 || p_cfg
->i_object_type
== 23 )
432 bs_skip( s
, 1+1+1 ); // ER data : section scale spectral */
434 if( bs_read1( s
) ) // extension 3
435 fprintf( stderr
, "Mpeg4GASpecificConfig: error 1\n" );
439 static int Mpeg4ReadAudioObjectType( bs_t
*s
)
441 int i_type
= bs_read( s
, 5 );
443 i_type
+= bs_read( s
, 6 );
446 static int Mpeg4ReadAudioSamplerate( bs_t
*s
)
448 int i_index
= bs_read( s
, 4 );
449 if( i_index
!= 0x0f )
450 return pi_sample_rates
[i_index
];
451 return bs_read( s
, 24 );
453 static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t
*p_cfg
, int *pi_extra
, uint8_t *p_extra
, bs_t
*s
, int i_max_size
)
456 static const char *ppsz_otype
[] = {
458 "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
461 "Reserved", "Reserved",
463 "Main Synthetic", "Wavetables Synthesis", "General MIDI",
464 "Algorithmic Synthesis and Audio FX",
467 "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
468 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
470 "Reserved", "Reserved", "Escape",
471 "Layer 1", "Layer 2", "Layer 3",
475 const int i_pos_start
= bs_pos( s
);
480 memset( p_cfg
, 0, sizeof(*p_cfg
) );
483 p_cfg
->i_object_type
= Mpeg4ReadAudioObjectType( s
);
484 p_cfg
->i_samplerate
= Mpeg4ReadAudioSamplerate( s
);
486 p_cfg
->i_channel
= bs_read( s
, 4 );
487 if( p_cfg
->i_channel
== 7 )
488 p_cfg
->i_channel
= 8; // 7.1
489 else if( p_cfg
->i_channel
>= 8 )
490 p_cfg
->i_channel
= -1;
493 p_cfg
->extension
.i_object_type
= 0;
494 p_cfg
->extension
.i_samplerate
= 0;
495 if( p_cfg
->i_object_type
== 5 )
498 p_cfg
->extension
.i_object_type
= p_cfg
->i_object_type
;
499 p_cfg
->extension
.i_samplerate
= Mpeg4ReadAudioSamplerate( s
);
501 p_cfg
->i_object_type
= Mpeg4ReadAudioObjectType( s
);
504 switch( p_cfg
->i_object_type
)
506 case 1: case 2: case 3: case 4:
508 case 17: case 19: case 20: case 21: case 22: case 23:
509 Mpeg4GASpecificConfig( p_cfg
, s
);
512 // CelpSpecificConfig();
515 // HvxcSpecificConfig();
518 // TTSSSpecificConfig();
520 case 13: case 14: case 15: case 16:
521 // StructuredAudioSpecificConfig();
524 // ERCelpSpecificConfig();
527 // ERHvxcSpecificConfig();
530 // ParametricSpecificConfig();
533 // SSCSpecificConfig();
535 case 32: case 33: case 34:
536 // MPEG_1_2_SpecificConfig();
539 // DSTSpecificConfig();
545 switch( p_cfg
->i_object_type
)
547 case 17: case 19: case 20: case 21: case 22: case 23:
548 case 24: case 25: case 26: case 27:
550 int epConfig
= bs_read( s
, 2 );
551 if( epConfig
== 2 || epConfig
== 3 )
553 //ErrorProtectionSpecificConfig();
557 int directMapping
= bs_read1( s
);
568 if( p_cfg
->extension
.i_object_type
!= 5 && i_max_size
> 0 && i_max_size
- (bs_pos(s
) - i_pos_start
) >= 16 &&
569 bs_read( s
, 11 ) == 0x2b7 )
571 p_cfg
->extension
.i_object_type
= Mpeg4ReadAudioObjectType( s
);
572 if( p_cfg
->extension
.i_object_type
== 5 )
574 p_cfg
->i_sbr
= bs_read1( s
);
575 if( p_cfg
->i_sbr
== 1 )
576 p_cfg
->extension
.i_samplerate
= Mpeg4ReadAudioSamplerate( s
);
580 //fprintf( stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
581 // ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr );
583 i_bits
= bs_pos(s
) - i_pos_start
;
585 *pi_extra
= ( i_bits
+ 7 ) / 8;
586 for( i
= 0; i
< __MIN( LATM_MAX_EXTRA_SIZE
, *pi_extra
); i
++ )
588 const int i_read
= __MIN( 8, i_bits
- 8*i
);
589 p_extra
[i
] = bs_read( &s_sav
, i_read
) << (8-i_read
);
594 static int LatmGetValue( bs_t
*s
)
596 int i_bytes
= bs_read( s
, 2 );
599 for( i
= 0; i
< i_bytes
; i
++ )
600 v
= (v
<< 8) + bs_read( s
, 8 );
605 static int LatmReadStreamMuxConfiguration( latm_mux_t
*m
, bs_t
*s
)
611 i_mux_version
= bs_read( s
, 1 );
614 i_mux_versionA
= bs_read( s
, 1 );
616 if( i_mux_versionA
!= 0 ) /* support only A=0 */
619 memset( m
, 0, sizeof(*m
) );
621 if( i_mux_versionA
== 0 )
623 if( i_mux_version
== 1 )
625 LatmGetValue(s
); /* taraBufferFullness */
629 m
->b_same_time_framing
= bs_read1( s
);
630 m
->i_sub_frames
= 1 + bs_read( s
, 6 );
631 m
->i_programs
= 1 + bs_read( s
, 4 );
633 for( i_program
= 0; i_program
< m
->i_programs
; i_program
++ )
637 m
->pi_layers
[i_program
] = 1+bs_read( s
, 3 );
639 for( i_layer
= 0; i_layer
< m
->pi_layers
[i_program
]; i_layer
++ )
641 latm_stream_t
*st
= &m
->stream
[m
->i_streams
];
642 vlc_bool_t b_previous_cfg
;
644 m
->pi_stream
[i_program
][i_layer
] = m
->i_streams
;
645 st
->i_program
= i_program
;
646 st
->i_layer
= i_layer
;
648 b_previous_cfg
= VLC_FALSE
;
649 if( i_program
!= 0 || i_layer
!= 0 )
650 b_previous_cfg
= bs_read1( s
);
654 assert( m
->i_streams
> 0 );
655 st
->cfg
= m
->stream
[m
->i_streams
-1].cfg
;
660 if( i_mux_version
== 1 )
661 i_cfg_size
= LatmGetValue(s
);
662 i_cfg_size
-= Mpeg4ReadAudioSpecificInfo( &st
->cfg
, &st
->i_extra
, st
->extra
, s
, i_cfg_size
);
664 bs_skip( s
, i_cfg_size
);
667 st
->i_frame_length_type
= bs_read( s
, 3 );
668 switch( st
->i_frame_length_type
)
672 bs_skip( s
, 8 ); /* latmBufferFullnes */
673 if( !m
->b_same_time_framing
)
675 if( st
->cfg
.i_object_type
== 6 || st
->cfg
.i_object_type
== 20 ||
676 st
->cfg
.i_object_type
== 8 || st
->cfg
.i_object_type
== 24 )
678 bs_skip( s
, 6 ); /* eFrameOffset */
684 st
->i_frame_length
= bs_read( s
, 9 );
686 case 3: case 4: case 5:
687 st
->i_frame_length_index
= bs_read( s
, 6 ); // celp
690 st
->i_frame_length_index
= bs_read( s
, 1 ); // hvxc
702 if( i_mux_version
== 1 )
704 m
->i_other_data
= LatmGetValue( s
);
710 b_continue
= bs_read1(s
);
711 m
->i_other_data
= (m
->i_other_data
<< 8) + bs_read( s
, 8 );
712 } while( b_continue
);
719 m
->i_crc
= bs_read( s
, 8 );
724 static int LOASParse( decoder_t
*p_dec
, uint8_t *p_buffer
, int i_buffer
)
726 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
729 int i_accumulated
= 0;
731 bs_init( &s
, p_buffer
, i_buffer
);
733 /* Read the stream mux configuration if present */
734 if( !bs_read1( &s
) )
736 if( !LatmReadStreamMuxConfiguration( &p_sys
->latm
, &s
) && p_sys
->latm
.i_streams
> 0 )
738 const latm_stream_t
*st
= &p_sys
->latm
.stream
[0];
740 p_sys
->i_channels
= st
->cfg
.i_channel
;
741 p_sys
->i_rate
= st
->cfg
.i_samplerate
;
742 p_sys
->i_frame_length
= st
->cfg
.i_frame_length
;
744 /* FIXME And if it changes ? */
745 if( !p_dec
->fmt_out
.i_extra
&& st
->i_extra
> 0 )
747 p_dec
->fmt_out
.i_extra
= st
->i_extra
;
748 p_dec
->fmt_out
.p_extra
= malloc( st
->i_extra
);
749 memcpy( p_dec
->fmt_out
.p_extra
, st
->extra
, st
->i_extra
);
752 p_sys
->b_latm_cfg
= VLC_TRUE
;
755 /* Wait for the configuration */
756 if( !p_sys
->b_latm_cfg
)
759 /* FIXME do we need to split the subframe into independant packet ? */
760 if( p_sys
->latm
.i_sub_frames
> 1 )
761 msg_Err( p_dec
, "latm sub frames not yet supported, please send a sample" );
763 for( i_sub
= 0; i_sub
< p_sys
->latm
.i_sub_frames
; i_sub
++ )
765 int pi_payload
[LATM_MAX_PROGRAM
][LATM_MAX_LAYER
];
766 if( p_sys
->latm
.b_same_time_framing
)
770 for( i_program
= 0; i_program
< p_sys
->latm
.i_programs
; i_program
++ )
773 for( i_layer
= 0; i_layer
< p_sys
->latm
.pi_layers
[i_program
]; i_layer
++ )
775 latm_stream_t
*st
= &p_sys
->latm
.stream
[p_sys
->latm
.pi_stream
[i_program
][i_layer
]];
776 if( st
->i_frame_length_type
== 0 )
781 int i_tmp
= bs_read( &s
, 8 );
786 pi_payload
[i_program
][i_layer
] = i_payload
;
788 else if( st
->i_frame_length_type
== 1 )
790 pi_payload
[i_program
][i_layer
] = st
->i_frame_length
/ 8; /* XXX not correct */
792 else if( st
->i_frame_length_type
== 3 || st
->i_frame_length_type
== 5 || st
->i_frame_length_type
== 7 )
794 bs_skip( &s
, 2 ); // muxSlotLengthCoded
795 pi_payload
[i_program
][i_layer
] = 0; /* TODO */
799 pi_payload
[i_program
][i_layer
] = 0; /* TODO */
804 for( i_program
= 0; i_program
< p_sys
->latm
.i_programs
; i_program
++ )
808 for( i_layer
= 0; i_layer
< p_sys
->latm
.pi_layers
[i_program
]; i_layer
++ )
810 /* XXX we only extract 1 stream */
811 if( i_program
!= 0 || i_layer
!= 0 )
814 if( pi_payload
[i_program
][i_layer
] <= 0 )
817 /* FIXME that's slow (and a bit ugly to write in place) */
818 for( i
= 0; i
< pi_payload
[i_program
][i_layer
]; i
++ )
819 p_buffer
[i_accumulated
++] = bs_read( &s
, 8 );
825 const int i_chunks
= bs_read( &s
, 4 );
830 msg_Err( p_dec
, "latm without same time frameing not yet supported, please send a sample" );
832 for( i_chunk
= 0; i_chunk
< i_chunks
; i_chunk
++ )
834 const int streamIndex
= bs_read( &s
, 4 );
835 latm_stream_t
*st
= &p_sys
->latm
.stream
[streamIndex
];
836 const int i_program
= st
->i_program
;
837 const int i_layer
= st
->i_layer
;
839 pi_program
[i_chunk
] = i_program
;
840 pi_layer
[i_chunk
] = i_layer
;
842 if( st
->i_frame_length_type
== 0 )
847 int i_tmp
= bs_read( &s
, 8 );
852 pi_payload
[i_program
][i_layer
] = i_payload
;
853 bs_skip( &s
, 1 ); // auEndFlag
855 else if( st
->i_frame_length_type
== 1 )
857 pi_payload
[i_program
][i_layer
] = st
->i_frame_length
/ 8; /* XXX not correct */
859 else if( st
->i_frame_length_type
== 3 || st
->i_frame_length_type
== 5 || st
->i_frame_length_type
== 7 )
861 bs_read( &s
, 2 ); // muxSlotLengthCoded
867 for( i_chunk
= 0; i_chunk
< i_chunks
; i_chunk
++ )
869 //const int i_program = pi_program[i_chunk];
870 //const int i_layer = pi_layer[i_chunk];
877 if( p_sys
->latm
.i_other_data
> 0 )
879 /* Other data XXX we just ignore them */
883 return i_accumulated
;
886 /****************************************************************************
887 * PacketizeStreamBlock: ADTS/LOAS packetizer
888 ****************************************************************************/
889 static void SetupOutput( decoder_t
*p_dec
, block_t
*p_block
);
890 static block_t
*PacketizeStreamBlock( decoder_t
*p_dec
, block_t
**pp_block
)
892 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
893 uint8_t p_header
[ADTS_HEADER_SIZE
+ LOAS_HEADER_SIZE
];
894 block_t
*p_out_buffer
;
897 if( !pp_block
|| !*pp_block
) return NULL
;
899 if( (*pp_block
)->i_flags
&(BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
901 if( (*pp_block
)->i_flags
&BLOCK_FLAG_CORRUPTED
)
903 p_sys
->i_state
= STATE_NOSYNC
;
904 block_BytestreamFlush( &p_sys
->bytestream
);
906 //aout_DateSet( &p_sys->end_date, 0 );
907 block_Release( *pp_block
);
911 if( !aout_DateGet( &p_sys
->end_date
) && !(*pp_block
)->i_pts
)
913 /* We've just started the stream, wait for the first PTS. */
914 block_Release( *pp_block
);
918 if( (*pp_block
)->i_rate
> 0 )
919 p_sys
->i_input_rate
= (*pp_block
)->i_rate
;
921 block_BytestreamPush( &p_sys
->bytestream
, *pp_block
);
925 switch( p_sys
->i_state
)
929 while( block_PeekBytes( &p_sys
->bytestream
, p_header
, 2 )
932 /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
933 if( p_header
[0] == 0xff && (p_header
[1] & 0xf6) == 0xf0 )
935 if( p_sys
->i_type
!= TYPE_ADTS
)
936 msg_Dbg( p_dec
, "detected ADTS format" );
938 p_sys
->i_state
= STATE_SYNC
;
939 p_sys
->i_type
= TYPE_ADTS
;
942 else if( p_header
[0] == 0x56 && (p_header
[1] & 0xe0) == 0xe0 )
944 if( p_sys
->i_type
!= TYPE_LOAS
)
945 msg_Dbg( p_dec
, "detected LOAS format" );
947 p_sys
->i_state
= STATE_SYNC
;
948 p_sys
->i_type
= TYPE_LOAS
;
951 block_SkipByte( &p_sys
->bytestream
);
953 if( p_sys
->i_state
!= STATE_SYNC
)
955 block_BytestreamFlush( &p_sys
->bytestream
);
962 /* New frame, set the Presentation Time Stamp */
963 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
;
964 if( p_sys
->i_pts
!= 0 &&
965 p_sys
->i_pts
!= aout_DateGet( &p_sys
->end_date
) )
967 aout_DateSet( &p_sys
->end_date
, p_sys
->i_pts
);
969 p_sys
->i_state
= STATE_HEADER
;
973 if( p_sys
->i_type
== TYPE_ADTS
)
975 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
976 if( block_PeekBytes( &p_sys
->bytestream
, p_header
,
977 ADTS_HEADER_SIZE
) != VLC_SUCCESS
)
983 /* Check if frame is valid and get frame info */
984 p_sys
->i_frame_size
= ADTSSyncInfo( p_dec
, p_header
,
987 &p_sys
->i_frame_length
,
988 &p_sys
->i_header_size
);
992 assert( p_sys
->i_type
== TYPE_LOAS
);
993 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
994 if( block_PeekBytes( &p_sys
->bytestream
, p_header
,
995 LOAS_HEADER_SIZE
) != VLC_SUCCESS
)
1001 /* Check if frame is valid and get frame info */
1002 p_sys
->i_frame_size
= LOASSyncInfo( p_dec
, p_header
, &p_sys
->i_header_size
);
1005 if( p_sys
->i_frame_size
<= 0 )
1007 msg_Dbg( p_dec
, "emulated sync word" );
1008 block_SkipByte( &p_sys
->bytestream
);
1009 p_sys
->i_state
= STATE_NOSYNC
;
1013 p_sys
->i_state
= STATE_NEXT_SYNC
;
1015 case STATE_NEXT_SYNC
:
1016 /* TODO: If p_block == NULL, flush the buffer without checking the
1019 /* Check if next expected frame contains the sync word */
1020 if( block_PeekOffsetBytes( &p_sys
->bytestream
, p_sys
->i_frame_size
1021 + p_sys
->i_header_size
, p_header
, 2 )
1024 /* Need more data */
1028 assert( p_sys
->i_type
== TYPE_ADTS
|| p_sys
->i_type
== TYPE_LOAS
);
1029 if( ( p_sys
->i_type
== TYPE_ADTS
&& ( p_header
[0] != 0xff || (p_header
[1] & 0xf6) != 0xf0 ) ) ||
1030 ( p_sys
->i_type
== TYPE_LOAS
&& ( p_header
[0] != 0x56 || (p_header
[1] & 0xe0) != 0xe0 ) ) )
1032 msg_Dbg( p_dec
, "emulated sync word "
1033 "(no sync on following frame)" );
1034 p_sys
->i_state
= STATE_NOSYNC
;
1035 block_SkipByte( &p_sys
->bytestream
);
1039 p_sys
->i_state
= STATE_SEND_DATA
;
1042 case STATE_GET_DATA
:
1043 /* Make sure we have enough data.
1044 * (Not useful if we went through NEXT_SYNC) */
1045 if( block_WaitBytes( &p_sys
->bytestream
, p_sys
->i_frame_size
+
1046 p_sys
->i_header_size
) != VLC_SUCCESS
)
1048 /* Need more data */
1051 p_sys
->i_state
= STATE_SEND_DATA
;
1053 case STATE_SEND_DATA
:
1054 /* When we reach this point we already know we have enough
1055 * data available. */
1057 p_out_buffer
= block_New( p_dec
, p_sys
->i_frame_size
);
1060 //p_dec->b_error = VLC_TRUE;
1063 p_buf
= p_out_buffer
->p_buffer
;
1065 /* Skip the ADTS/LOAS header */
1066 block_SkipBytes( &p_sys
->bytestream
, p_sys
->i_header_size
);
1068 if( p_sys
->i_type
== TYPE_ADTS
)
1070 /* Copy the whole frame into the buffer */
1071 block_GetBytes( &p_sys
->bytestream
, p_buf
, p_sys
->i_frame_size
);
1075 assert( p_sys
->i_type
== TYPE_LOAS
);
1076 /* Copy the whole frame into the buffer and parse/extract it */
1077 block_GetBytes( &p_sys
->bytestream
, p_buf
, p_sys
->i_frame_size
);
1078 p_out_buffer
->i_buffer
= LOASParse( p_dec
, p_buf
, p_sys
->i_frame_size
);
1079 if( p_out_buffer
->i_buffer
<= 0 )
1081 if( !p_sys
->b_latm_cfg
)
1082 msg_Warn( p_dec
, "waiting for header" );
1084 block_Release( p_out_buffer
);
1085 p_out_buffer
= NULL
;
1086 p_sys
->i_state
= STATE_NOSYNC
;
1090 SetupOutput( p_dec
, p_out_buffer
);
1091 /* Make sure we don't reuse the same pts twice */
1092 if( p_sys
->i_pts
== p_sys
->bytestream
.p_block
->i_pts
)
1093 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
= 0;
1095 /* So p_block doesn't get re-added several times */
1096 *pp_block
= block_BytestreamPop( &p_sys
->bytestream
);
1098 p_sys
->i_state
= STATE_NOSYNC
;
1100 return p_out_buffer
;
1108 /*****************************************************************************
1110 *****************************************************************************/
1111 static void SetupOutput( decoder_t
*p_dec
, block_t
*p_block
)
1113 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1115 if( p_dec
->fmt_out
.audio
.i_rate
!= p_sys
->i_rate
)
1117 msg_Info( p_dec
, "AAC channels: %d samplerate: %d",
1118 p_sys
->i_channels
, p_sys
->i_rate
);
1120 aout_DateInit( &p_sys
->end_date
, p_sys
->i_rate
);
1121 aout_DateSet( &p_sys
->end_date
, p_sys
->i_pts
);
1124 p_dec
->fmt_out
.audio
.i_rate
= p_sys
->i_rate
;
1125 p_dec
->fmt_out
.audio
.i_channels
= p_sys
->i_channels
;
1126 p_dec
->fmt_out
.audio
.i_bytes_per_frame
= p_sys
->i_frame_size
;
1127 p_dec
->fmt_out
.audio
.i_frame_length
= p_sys
->i_frame_length
;
1130 p_dec
->fmt_out
.audio
.i_original_channels
= p_sys
->i_channels_conf
;
1131 p_dec
->fmt_out
.audio
.i_physical_channels
=
1132 p_sys
->i_channels_conf
& AOUT_CHAN_PHYSMASK
;
1135 p_block
->i_pts
= p_block
->i_dts
= aout_DateGet( &p_sys
->end_date
);
1137 p_block
->i_length
= aout_DateIncrement( &p_sys
->end_date
,
1138 p_sys
->i_frame_length
* p_sys
->i_input_rate
/ INPUT_RATE_DEFAULT
) -
1142 /*****************************************************************************
1143 * ClosePacketizer: clean up the packetizer
1144 *****************************************************************************/
1145 static void ClosePacketizer( vlc_object_t
*p_this
)
1147 decoder_t
*p_dec
= (decoder_t
*)p_this
;
1148 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1150 block_BytestreamRelease( &p_sys
->bytestream
);
1152 free( p_dec
->p_sys
);