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 *****************************************************************************/
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
75 int i_frame_length; // 1024 or 960
79 #define LATM_MAX_EXTRA_SIZE 64
85 int i_frame_length_type;
86 int i_frame_length; // type 1
87 int i_frame_length_index; // type 3 4 5 6 7
91 /* Raw configuration */
93 uint8_t extra[LATM_MAX_EXTRA_SIZE];
97 #define LATM_MAX_LAYER (8)
98 #define LATM_MAX_PROGRAM (16)
101 int b_same_time_framing;
105 int pi_layers[LATM_MAX_PROGRAM];
107 int pi_stream[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
110 latm_stream_t stream[LATM_MAX_PROGRAM*LATM_MAX_LAYER];
113 int i_crc; /* -1 if not set */
124 block_bytestream_t bytestream;
133 unsigned int i_channels;
134 unsigned int i_rate, i_frame_length, i_header_size;
150 static const int pi_sample_rates[16] =
152 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
153 16000, 12000, 11025, 8000, 7350, 0, 0, 0
156 #define ADTS_HEADER_SIZE 9
157 #define LOAS_HEADER_SIZE 3
159 /****************************************************************************
161 ****************************************************************************/
162 static int OpenPacketizer( vlc_object_t * );
163 static void ClosePacketizer( vlc_object_t * );
165 static block_t *PacketizeRawBlock ( decoder_t *, block_t ** );
166 static block_t *PacketizeStreamBlock( decoder_t *, block_t ** );
168 /*****************************************************************************
170 *****************************************************************************/
172 set_category( CAT_SOUT )
173 set_subcategory( SUBCAT_SOUT_PACKETIZER )
174 set_description( N_("MPEG4 audio packetizer") )
175 set_capability( "packetizer", 50 )
176 set_callbacks( OpenPacketizer, ClosePacketizer )
179 /*****************************************************************************
180 * OpenPacketizer: probe the packetizer and return score
181 *****************************************************************************/
182 static int OpenPacketizer( vlc_object_t *p_this )
184 decoder_t *p_dec = (decoder_t*)p_this;
185 decoder_sys_t *p_sys;
187 if( p_dec->fmt_in.i_codec != VLC_CODEC_MP4A )
192 /* Allocate the memory needed to store the decoder's structure */
193 if( ( p_dec->p_sys = p_sys =
194 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
198 p_sys->i_state = STATE_NOSYNC;
199 date_Set( &p_sys->end_date, 0 );
200 block_BytestreamInit( &p_sys->bytestream );
201 p_sys->b_latm_cfg = false;
203 /* Set output properties */
204 p_dec->fmt_out.i_cat = AUDIO_ES;
205 p_dec->fmt_out.i_codec = VLC_CODEC_MP4A;
207 msg_Dbg( p_dec, "running MPEG4 audio packetizer" );
209 if( p_dec->fmt_in.i_extra > 0 )
211 uint8_t *p_config = (uint8_t*)p_dec->fmt_in.p_extra;
214 i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) ) & 0x0f;
215 if( i_index != 0x0f )
217 p_dec->fmt_out.audio.i_rate = pi_sample_rates[i_index];
218 p_dec->fmt_out.audio.i_frame_length =
219 (( p_config[1] >> 2 ) & 0x01) ? 960 : 1024;
223 p_dec->fmt_out.audio.i_rate = ( ( p_config[1] & 0x7f ) << 17 ) |
224 ( p_config[2] << 9 ) | ( p_config[3] << 1 ) |
225 ( p_config[4] >> 7 );
226 p_dec->fmt_out.audio.i_frame_length =
227 (( p_config[4] >> 2 ) & 0x01) ? 960 : 1024;
230 p_dec->fmt_out.audio.i_channels =
231 (p_config[i_index == 0x0f ? 4 : 1] >> 3) & 0x0f;
233 msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
234 p_dec->fmt_out.audio.i_rate,
235 p_dec->fmt_out.audio.i_frame_length );
237 date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
239 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
240 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
241 if( !p_dec->fmt_out.p_extra )
243 p_dec->fmt_out.i_extra = 0;
246 memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
247 p_dec->fmt_in.i_extra );
250 p_dec->pf_packetize = PacketizeRawBlock;
251 p_sys->i_type = TYPE_RAW;
255 msg_Dbg( p_dec, "no decoder specific info, must be an ADTS or LOAS stream" );
257 date_Init( &p_sys->end_date, p_dec->fmt_in.audio.i_rate, 1 );
259 /* We will try to create a AAC Config from adts/loas */
260 p_dec->fmt_out.i_extra = 0;
261 p_dec->fmt_out.p_extra = NULL;
264 p_dec->pf_packetize = PacketizeStreamBlock;
265 p_sys->i_type = TYPE_NONE;
271 /****************************************************************************
272 * PacketizeRawBlock: the whole thing
273 ****************************************************************************
274 * This function must be fed with complete frames.
275 ****************************************************************************/
276 static block_t *PacketizeRawBlock( decoder_t *p_dec, block_t **pp_block )
278 decoder_sys_t *p_sys = p_dec->p_sys;
281 if( !pp_block || !*pp_block ) return NULL;
283 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
285 date_Set( &p_sys->end_date, 0 );
286 block_Release( *pp_block );
291 *pp_block = NULL; /* Don't reuse this block */
293 if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID )
295 /* We've just started the stream, wait for the first PTS. */
296 block_Release( p_block );
299 else if( p_block->i_pts > VLC_TS_INVALID &&
300 p_block->i_pts != date_Get( &p_sys->end_date ) )
302 date_Set( &p_sys->end_date, p_block->i_pts );
305 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
307 p_block->i_length = date_Increment( &p_sys->end_date,
308 p_dec->fmt_out.audio.i_frame_length ) - p_block->i_pts;
313 /****************************************************************************
315 ****************************************************************************/
316 static int ADTSSyncInfo( decoder_t * p_dec, const uint8_t * p_buf,
317 unsigned int * pi_channels,
318 unsigned int * pi_sample_rate,
319 unsigned int * pi_frame_length,
320 unsigned int * pi_header_size )
322 int i_profile, i_sample_rate_idx, i_frame_size;
325 /* Fixed header between frames */
326 //int i_id = ( (p_buf[1] >> 3) & 0x01) ? 2 : 4; /* MPEG-2 or 4 */
327 b_crc = !(p_buf[1] & 0x01);
328 i_profile = p_buf[2] >> 6;
329 i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
330 *pi_sample_rate = pi_sample_rates[i_sample_rate_idx];
331 //private_bit = (p_buf[2] >> 1) & 0x01;
332 *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
333 //original_copy = (p_buf[3] >> 5) & 0x01;
334 //home = (p_buf[3] >> 4) & 0x01;
336 /* Variable header */
337 //copyright_id_bit = (p_buf[3] >> 3) & 0x01;
338 //copyright_id_start = (p_buf[3] >> 2) & 0x01;
339 i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
340 ((p_buf[5] >> 5) /*& 0x7*/);
341 //uint16_t buffer_fullness = ((p_buf[5] & 0x1f) << 6) | (p_buf[6] >> 2);
342 unsigned short i_raw_blocks_in_frame = p_buf[6] & 0x03;
344 if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
346 msg_Warn( p_dec, "Invalid ADTS header" );
350 *pi_frame_length = 1024;
352 if( i_raw_blocks_in_frame == 0 )
356 msg_Warn( p_dec, "ADTS CRC not supported" );
357 //uint16_t crc = (p_buf[7] << 8) | p_buf[8];
362 msg_Err( p_dec, "Multiple blocks per frame in ADTS not supported" );
366 const uint8_t *p_pos = p_buf + 7;
368 uint16_t i_block_pos[3];
371 for( i = 0 ; i < i_raw_blocks_in_frame ; i++ )
372 { /* the 1st block's position is known ... */
373 i_block_pos[i] = (*p_pos << 8) | *(p_pos+1);
376 crc_block = (*p_pos << 8) | *(p_pos+1);
379 for( i = 0 ; i <= i_raw_blocks_in_frame ; i++ )
384 msg_Err( p_dec, "ADTS CRC not supported" );
385 //uint16_t crc = (*p_pos << 8) | *(p_pos+1);
393 /* Build the decoder specific info header */
394 if( !p_dec->fmt_out.i_extra )
396 p_dec->fmt_out.p_extra = malloc( 2 );
397 if( !p_dec->fmt_out.p_extra )
399 p_dec->fmt_out.i_extra = 0;
402 p_dec->fmt_out.i_extra = 2;
403 ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
404 (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
405 ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
406 ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
409 /* ADTS header length */
410 *pi_header_size = b_crc ? 9 : 7;
412 return i_frame_size - *pi_header_size;
415 /****************************************************************************
417 ****************************************************************************/
418 static int LOASSyncInfo( uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_header_size )
421 return ( ( p_header[1] & 0x1f ) << 8 ) + p_header[2];
424 static int Mpeg4GAProgramConfigElement( bs_t *s )
426 /* TODO compute channels count ? */
427 int i_tag = bs_read( s, 4 );
430 bs_skip( s, 2 + 4 ); // object type + sampling index
431 int i_num_front = bs_read( s, 4 );
432 int i_num_side = bs_read( s, 4 );
433 int i_num_back = bs_read( s, 4 );
434 int i_num_lfe = bs_read( s, 2 );
435 int i_num_assoc_data = bs_read( s, 3 );
436 int i_num_valid_cc = bs_read( s, 4 );
439 bs_skip( s, 4 ); // mono downmix
441 bs_skip( s, 4 ); // stereo downmix
443 bs_skip( s, 2+1 ); // matrix downmix + pseudo_surround
445 bs_skip( s, i_num_front * (1+4) );
446 bs_skip( s, i_num_side * (1+4) );
447 bs_skip( s, i_num_back * (1+4) );
448 bs_skip( s, i_num_lfe * (4) );
449 bs_skip( s, i_num_assoc_data * (4) );
450 bs_skip( s, i_num_valid_cc * (5) );
452 int i_comment = bs_read( s, 8 );
453 bs_skip( s, i_comment * 8 );
457 static int Mpeg4GASpecificConfig( mpeg4_cfg_t *p_cfg, bs_t *s )
459 p_cfg->i_frame_length = bs_read1(s) ? 960 : 1024;
461 if( bs_read1( s ) ) // depend on core coder
462 bs_skip( s, 14 ); // core coder delay
464 int i_extension_flag = bs_read1( s );
465 if( p_cfg->i_channel == 0 )
467 Mpeg4GAProgramConfigElement( s );
469 if( p_cfg->i_object_type == 6 || p_cfg->i_object_type == 20 )
470 bs_skip( s, 3 ); // layer
472 if( i_extension_flag )
474 if( p_cfg->i_object_type == 22 )
476 bs_skip( s, 5 + 11 ); // numOfSubFrame + layer length
478 if( p_cfg->i_object_type == 17 || p_cfg->i_object_type == 19 ||
479 p_cfg->i_object_type == 20 || p_cfg->i_object_type == 23 )
481 bs_skip( s, 1+1+1 ); // ER data : section scale spectral */
483 if( bs_read1( s ) ) // extension 3
484 fprintf( stderr, "Mpeg4GASpecificConfig: error 1\n" );
489 static int Mpeg4ReadAudioObjectType( bs_t *s )
491 int i_type = bs_read( s, 5 );
493 i_type = 32 + bs_read( s, 6 );
497 static int Mpeg4ReadAudioSamplerate( bs_t *s )
499 int i_index = bs_read( s, 4 );
500 if( i_index != 0x0f )
501 return pi_sample_rates[i_index];
502 return bs_read( s, 24 );
505 static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_t *p_extra, bs_t *s, int i_max_size )
508 static const char *ppsz_otype[] = {
510 "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
513 "Reserved", "Reserved",
515 "Main Synthetic", "Wavetables Synthesis", "General MIDI",
516 "Algorithmic Synthesis and Audio FX",
519 "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
520 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
522 "PS", "Reserved", "Escape",
523 "Layer 1", "Layer 2", "Layer 3",
527 const int i_pos_start = bs_pos( s );
532 memset( p_cfg, 0, sizeof(*p_cfg) );
535 p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
536 p_cfg->i_samplerate = Mpeg4ReadAudioSamplerate( s );
538 p_cfg->i_channel = bs_read( s, 4 );
539 if( p_cfg->i_channel == 7 )
540 p_cfg->i_channel = 8; // 7.1
541 else if( p_cfg->i_channel >= 8 )
542 p_cfg->i_channel = -1;
546 p_cfg->extension.i_object_type = 0;
547 p_cfg->extension.i_samplerate = 0;
548 if( p_cfg->i_object_type == 5 || p_cfg->i_object_type == 29 )
551 if( p_cfg->i_object_type == 29 )
553 p_cfg->extension.i_object_type = 5;
554 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
556 p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
559 switch( p_cfg->i_object_type )
561 case 1: case 2: case 3: case 4:
563 case 17: case 19: case 20: case 21: case 22: case 23:
564 Mpeg4GASpecificConfig( p_cfg, s );
567 // CelpSpecificConfig();
570 // HvxcSpecificConfig();
573 // TTSSSpecificConfig();
575 case 13: case 14: case 15: case 16:
576 // StructuredAudioSpecificConfig();
579 // ERCelpSpecificConfig();
582 // ERHvxcSpecificConfig();
585 // ParametricSpecificConfig();
588 // SSCSpecificConfig();
590 case 32: case 33: case 34:
591 // MPEG_1_2_SpecificConfig();
594 // DSTSpecificConfig();
597 // ALSSpecificConfig();
603 switch( p_cfg->i_object_type )
605 case 17: case 19: case 20: case 21: case 22: case 23:
606 case 24: case 25: case 26: case 27:
608 int epConfig = bs_read( s, 2 );
609 if( epConfig == 2 || epConfig == 3 )
611 //ErrorProtectionSpecificConfig();
615 int directMapping = bs_read1( s );
627 if( p_cfg->extension.i_object_type != 5 && i_max_size > 0 && i_max_size - (bs_pos(s) - i_pos_start) >= 16 &&
628 bs_read( s, 11 ) == 0x2b7 )
630 p_cfg->extension.i_object_type = Mpeg4ReadAudioObjectType( s );
631 if( p_cfg->extension.i_object_type == 5 )
633 p_cfg->i_sbr = bs_read1( s );
634 if( p_cfg->i_sbr == 1 )
636 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
637 if( i_max_size > 0 && i_max_size - (bs_pos(s) - i_pos_start) >= 12 && bs_read( s, 11 ) == 0x548 )
639 p_cfg->i_ps = bs_read1( s );
645 //fprintf( stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
646 // ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr );
648 i_bits = bs_pos(s) - i_pos_start;
650 *pi_extra = __MIN( ( i_bits + 7 ) / 8, LATM_MAX_EXTRA_SIZE );
651 for( i = 0; i < *pi_extra; i++ )
653 const int i_read = __MIN( 8, i_bits - 8*i );
654 p_extra[i] = bs_read( &s_sav, i_read ) << (8-i_read);
659 static int LatmGetValue( bs_t *s )
661 int i_bytes = bs_read( s, 2 );
664 for( i = 0; i < i_bytes; i++ )
665 v = (v << 8) + bs_read( s, 8 );
670 static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
676 i_mux_version = bs_read( s, 1 );
679 i_mux_versionA = bs_read( s, 1 );
681 if( i_mux_versionA != 0 ) /* support only A=0 */
684 memset( m, 0, sizeof(*m) );
686 if( i_mux_versionA == 0 )
688 if( i_mux_version == 1 )
690 LatmGetValue(s); /* taraBufferFullness */
694 m->b_same_time_framing = bs_read1( s );
695 m->i_sub_frames = 1 + bs_read( s, 6 );
696 m->i_programs = 1 + bs_read( s, 4 );
698 for( i_program = 0; i_program < m->i_programs; i_program++ )
702 m->pi_layers[i_program] = 1+bs_read( s, 3 );
704 for( i_layer = 0; i_layer < m->pi_layers[i_program]; i_layer++ )
706 latm_stream_t *st = &m->stream[m->i_streams];
709 m->pi_stream[i_program][i_layer] = m->i_streams;
710 st->i_program = i_program;
711 st->i_layer = i_layer;
713 b_previous_cfg = false;
714 if( i_program != 0 || i_layer != 0 )
715 b_previous_cfg = bs_read1( s );
719 assert( m->i_streams > 0 );
720 st->cfg = m->stream[m->i_streams-1].cfg;
725 if( i_mux_version == 1 )
726 i_cfg_size = LatmGetValue(s);
727 i_cfg_size -= Mpeg4ReadAudioSpecificInfo( &st->cfg, &st->i_extra, st->extra, s, i_cfg_size );
729 bs_skip( s, i_cfg_size );
732 st->i_frame_length_type = bs_read( s, 3 );
733 switch( st->i_frame_length_type )
737 bs_skip( s, 8 ); /* latmBufferFullnes */
738 if( !m->b_same_time_framing )
740 if( st->cfg.i_object_type == 6 || st->cfg.i_object_type == 20 ||
741 st->cfg.i_object_type == 8 || st->cfg.i_object_type == 24 )
743 bs_skip( s, 6 ); /* eFrameOffset */
749 st->i_frame_length = bs_read( s, 9 );
751 case 3: case 4: case 5:
752 st->i_frame_length_index = bs_read( s, 6 ); // celp
755 st->i_frame_length_index = bs_read( s, 1 ); // hvxc
767 if( i_mux_version == 1 )
769 m->i_other_data = LatmGetValue( s );
775 b_continue = bs_read1(s);
776 m->i_other_data = (m->i_other_data << 8) + bs_read( s, 8 );
777 } while( b_continue );
784 m->i_crc = bs_read( s, 8 );
789 static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
791 decoder_sys_t *p_sys = p_dec->p_sys;
794 int i_accumulated = 0;
796 bs_init( &s, p_buffer, i_buffer );
798 /* Read the stream mux configuration if present */
799 if( !bs_read1( &s ) )
801 if( !LatmReadStreamMuxConfiguration( &p_sys->latm, &s ) &&
802 p_sys->latm.i_streams > 0 )
804 const latm_stream_t *st = &p_sys->latm.stream[0];
806 p_sys->i_channels = st->cfg.i_channel;
807 p_sys->i_rate = st->cfg.i_samplerate;
808 p_sys->i_frame_length = st->cfg.i_frame_length;
810 /* FIXME And if it changes ? */
811 if( !p_dec->fmt_out.i_extra && st->i_extra > 0 )
813 p_dec->fmt_out.i_extra = st->i_extra;
814 p_dec->fmt_out.p_extra = malloc( st->i_extra );
815 if( !p_dec->fmt_out.p_extra )
817 p_dec->fmt_out.i_extra = 0;
820 memcpy( p_dec->fmt_out.p_extra, st->extra, st->i_extra );
823 p_sys->b_latm_cfg = true;
826 /* Wait for the configuration */
827 if( !p_sys->b_latm_cfg )
830 /* FIXME do we need to split the subframe into independent packet ? */
831 if( p_sys->latm.i_sub_frames > 1 )
832 msg_Err( p_dec, "latm sub frames not yet supported, please send a sample" );
834 for( i_sub = 0; i_sub < p_sys->latm.i_sub_frames; i_sub++ )
836 int pi_payload[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
837 if( p_sys->latm.b_same_time_framing )
841 for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
844 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
846 latm_stream_t *st = &p_sys->latm.stream[p_sys->latm.pi_stream[i_program][i_layer]];
847 if( st->i_frame_length_type == 0 )
852 int i_tmp = bs_read( &s, 8 );
857 pi_payload[i_program][i_layer] = i_payload;
859 else if( st->i_frame_length_type == 1 )
861 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
863 else if( ( st->i_frame_length_type == 3 ) ||
864 ( st->i_frame_length_type == 5 ) ||
865 ( st->i_frame_length_type == 7 ) )
867 bs_skip( &s, 2 ); // muxSlotLengthCoded
868 pi_payload[i_program][i_layer] = 0; /* TODO */
872 pi_payload[i_program][i_layer] = 0; /* TODO */
877 for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
881 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
883 /* XXX we only extract 1 stream */
884 if( i_program != 0 || i_layer != 0 )
887 if( pi_payload[i_program][i_layer] <= 0 )
890 /* FIXME that's slow (and a bit ugly to write in place) */
891 for( i = 0; i < pi_payload[i_program][i_layer]; i++ )
892 p_buffer[i_accumulated++] = bs_read( &s, 8 );
898 const int i_chunks = bs_read( &s, 4 );
903 msg_Err( p_dec, "latm without same time frameing not yet supported, please send a sample" );
905 for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
907 const int streamIndex = bs_read( &s, 4 );
908 latm_stream_t *st = &p_sys->latm.stream[streamIndex];
909 const int i_program = st->i_program;
910 const int i_layer = st->i_layer;
912 pi_program[i_chunk] = i_program;
913 pi_layer[i_chunk] = i_layer;
915 if( st->i_frame_length_type == 0 )
920 int i_tmp = bs_read( &s, 8 );
925 pi_payload[i_program][i_layer] = i_payload;
926 bs_skip( &s, 1 ); // auEndFlag
928 else if( st->i_frame_length_type == 1 )
930 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 ) )
936 bs_read( &s, 2 ); // muxSlotLengthCoded
942 for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
944 //const int i_program = pi_program[i_chunk];
945 //const int i_layer = pi_layer[i_chunk];
952 if( p_sys->latm.i_other_data > 0 )
954 /* Other data XXX we just ignore them */
958 return i_accumulated;
961 /****************************************************************************
962 * PacketizeStreamBlock: ADTS/LOAS packetizer
963 ****************************************************************************/
964 static void SetupOutput( decoder_t *p_dec, block_t *p_block );
965 static block_t *PacketizeStreamBlock( decoder_t *p_dec, block_t **pp_block )
967 decoder_sys_t *p_sys = p_dec->p_sys;
968 uint8_t p_header[ADTS_HEADER_SIZE + LOAS_HEADER_SIZE];
969 block_t *p_out_buffer;
972 if( !pp_block || !*pp_block ) return NULL;
974 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
976 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
978 p_sys->i_state = STATE_NOSYNC;
979 block_BytestreamEmpty( &p_sys->bytestream );
981 date_Set( &p_sys->end_date, 0 );
982 block_Release( *pp_block );
986 if( !date_Get( &p_sys->end_date ) && (*pp_block)->i_pts <= VLC_TS_INVALID )
988 /* We've just started the stream, wait for the first PTS. */
989 block_Release( *pp_block );
993 block_BytestreamPush( &p_sys->bytestream, *pp_block );
997 switch( p_sys->i_state )
1001 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
1004 /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
1005 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
1007 if( p_sys->i_type != TYPE_ADTS )
1008 msg_Dbg( p_dec, "detected ADTS format" );
1010 p_sys->i_state = STATE_SYNC;
1011 p_sys->i_type = TYPE_ADTS;
1014 else if( p_header[0] == 0x56 && (p_header[1] & 0xe0) == 0xe0 )
1016 if( p_sys->i_type != TYPE_LOAS )
1017 msg_Dbg( p_dec, "detected LOAS format" );
1019 p_sys->i_state = STATE_SYNC;
1020 p_sys->i_type = TYPE_LOAS;
1023 block_SkipByte( &p_sys->bytestream );
1025 if( p_sys->i_state != STATE_SYNC )
1027 block_BytestreamFlush( &p_sys->bytestream );
1029 /* Need more data */
1034 /* New frame, set the Presentation Time Stamp */
1035 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
1036 if( p_sys->i_pts > VLC_TS_INVALID &&
1037 p_sys->i_pts != date_Get( &p_sys->end_date ) )
1039 date_Set( &p_sys->end_date, p_sys->i_pts );
1041 p_sys->i_state = STATE_HEADER;
1045 if( p_sys->i_type == TYPE_ADTS )
1047 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
1048 if( block_PeekBytes( &p_sys->bytestream, p_header,
1049 ADTS_HEADER_SIZE ) != VLC_SUCCESS )
1051 /* Need more data */
1055 /* Check if frame is valid and get frame info */
1056 p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
1059 &p_sys->i_frame_length,
1060 &p_sys->i_header_size );
1064 assert( p_sys->i_type == TYPE_LOAS );
1065 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
1066 if( block_PeekBytes( &p_sys->bytestream, p_header,
1067 LOAS_HEADER_SIZE ) != VLC_SUCCESS )
1069 /* Need more data */
1073 /* Check if frame is valid and get frame info */
1074 p_sys->i_frame_size = LOASSyncInfo( p_header, &p_sys->i_header_size );
1077 if( p_sys->i_frame_size <= 0 )
1079 msg_Dbg( p_dec, "emulated sync word" );
1080 block_SkipByte( &p_sys->bytestream );
1081 p_sys->i_state = STATE_NOSYNC;
1085 p_sys->i_state = STATE_NEXT_SYNC;
1087 case STATE_NEXT_SYNC:
1088 /* TODO: If p_block == NULL, flush the buffer without checking the
1090 if( p_sys->bytestream.p_block == NULL )
1092 p_sys->i_state = STATE_NOSYNC;
1093 block_BytestreamFlush( &p_sys->bytestream );
1097 /* Check if next expected frame contains the sync word */
1098 if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
1099 + p_sys->i_header_size, p_header, 2 )
1102 /* Need more data */
1106 assert( (p_sys->i_type == TYPE_ADTS) || (p_sys->i_type == TYPE_LOAS) );
1107 if( ( ( p_sys->i_type == TYPE_ADTS ) &&
1108 ( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 ) ) ||
1109 ( ( p_sys->i_type == TYPE_LOAS ) &&
1110 ( p_header[0] != 0x56 || (p_header[1] & 0xe0) != 0xe0 ) ) )
1112 msg_Dbg( p_dec, "emulated sync word "
1113 "(no sync on following frame)" );
1114 p_sys->i_state = STATE_NOSYNC;
1115 block_SkipByte( &p_sys->bytestream );
1119 p_sys->i_state = STATE_SEND_DATA;
1122 case STATE_GET_DATA:
1123 /* Make sure we have enough data.
1124 * (Not useful if we went through NEXT_SYNC) */
1125 if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
1126 p_sys->i_header_size) != VLC_SUCCESS )
1128 /* Need more data */
1131 p_sys->i_state = STATE_SEND_DATA;
1133 case STATE_SEND_DATA:
1134 /* When we reach this point we already know we have enough
1135 * data available. */
1137 p_out_buffer = block_New( p_dec, p_sys->i_frame_size );
1140 //p_dec->b_error = true;
1143 p_buf = p_out_buffer->p_buffer;
1145 /* Skip the ADTS/LOAS header */
1146 block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
1148 if( p_sys->i_type == TYPE_ADTS )
1150 /* Copy the whole frame into the buffer */
1151 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1155 assert( p_sys->i_type == TYPE_LOAS );
1156 /* Copy the whole frame into the buffer and parse/extract it */
1157 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1158 p_out_buffer->i_buffer = LOASParse( p_dec, p_buf, p_sys->i_frame_size );
1159 if( p_out_buffer->i_buffer <= 0 )
1161 if( !p_sys->b_latm_cfg )
1162 msg_Warn( p_dec, "waiting for header" );
1164 block_Release( p_out_buffer );
1165 p_out_buffer = NULL;
1166 p_sys->i_state = STATE_NOSYNC;
1170 SetupOutput( p_dec, p_out_buffer );
1171 /* Make sure we don't reuse the same pts twice */
1172 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
1173 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
1175 /* So p_block doesn't get re-added several times */
1176 *pp_block = block_BytestreamPop( &p_sys->bytestream );
1178 p_sys->i_state = STATE_NOSYNC;
1180 return p_out_buffer;
1187 /*****************************************************************************
1189 *****************************************************************************/
1190 static void SetupOutput( decoder_t *p_dec, block_t *p_block )
1192 decoder_sys_t *p_sys = p_dec->p_sys;
1194 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
1196 msg_Info( p_dec, "AAC channels: %d samplerate: %d",
1197 p_sys->i_channels, p_sys->i_rate );
1199 const mtime_t i_end_date = date_Get( &p_sys->end_date );
1200 date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
1201 date_Set( &p_sys->end_date, i_end_date );
1204 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
1205 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
1206 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
1207 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
1210 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
1211 p_dec->fmt_out.audio.i_physical_channels =
1212 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
1215 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
1218 date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
1221 /*****************************************************************************
1222 * ClosePacketizer: clean up the packetizer
1223 *****************************************************************************/
1224 static void ClosePacketizer( vlc_object_t *p_this )
1226 decoder_t *p_dec = (decoder_t *)p_this;
1227 decoder_sys_t *p_sys = p_dec->p_sys;
1229 block_BytestreamRelease( &p_sys->bytestream );
1231 free( p_dec->p_sys );