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 *****************************************************************************/
31 #include <vlc_codec.h>
32 #include <vlc_block.h>
34 #include <vlc_codecs.h>
35 #include <vlc_input.h>
38 #include "vlc_block_helper.h"
42 * AudioObjectType 5 bits
43 * samplingFrequencyIndex 4 bits
44 * if (samplingFrequencyIndex == 0xF)
45 * samplingFrequency 24 bits
46 * channelConfiguration 4 bits
48 * FrameLengthFlag 1 bit 1024 or 960
49 * DependsOnCoreCoder 1 bit (always 0)
50 * ExtensionFlag 1 bit (always 0)
53 /*****************************************************************************
54 * decoder_sys_t : decoder descriptor
55 *****************************************************************************/
61 int i_sbr; // 0: no sbr, 1: sbr, -1: unknown
70 int i_frame_length; // 1024 or 960
74 #define LATM_MAX_EXTRA_SIZE 64
80 int i_frame_length_type;
81 int i_frame_length; // type 1
82 int i_frame_length_index; // type 3 4 5 6 7
86 /* Raw configuration */
88 uint8_t extra[LATM_MAX_EXTRA_SIZE];
92 #define LATM_MAX_LAYER (8)
93 #define LATM_MAX_PROGRAM (16)
96 int b_same_time_framing;
100 int pi_layers[LATM_MAX_PROGRAM];
102 int pi_stream[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
105 latm_stream_t stream[LATM_MAX_PROGRAM*LATM_MAX_LAYER];
108 int i_crc; /* -1 if not set */
119 block_bytestream_t bytestream;
124 audio_date_t end_date;
128 unsigned int i_channels;
129 unsigned int i_rate, i_frame_length, i_header_size;
134 vlc_bool_t b_latm_cfg;
155 static const int pi_sample_rates[16] =
157 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
158 16000, 12000, 11025, 8000, 7350, 0, 0, 0
161 #define ADTS_HEADER_SIZE 9
162 #define LOAS_HEADER_SIZE 3
164 /****************************************************************************
166 ****************************************************************************/
167 static int OpenPacketizer( vlc_object_t * );
168 static void ClosePacketizer( vlc_object_t * );
170 static block_t *PacketizeRawBlock ( decoder_t *, block_t ** );
171 static block_t *PacketizeStreamBlock( decoder_t *, block_t ** );
173 /*****************************************************************************
175 *****************************************************************************/
177 set_category( CAT_SOUT );
178 set_subcategory( SUBCAT_SOUT_PACKETIZER );
179 set_description( _("MPEG4 audio packetizer") );
180 set_capability( "packetizer", 50 );
181 set_callbacks( OpenPacketizer, ClosePacketizer );
184 /*****************************************************************************
185 * OpenPacketizer: probe the packetizer and return score
186 *****************************************************************************/
187 static int OpenPacketizer( vlc_object_t *p_this )
189 decoder_t *p_dec = (decoder_t*)p_this;
190 decoder_sys_t *p_sys;
192 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', '4', 'a' ) )
197 /* Allocate the memory needed to store the decoder's structure */
198 if( ( p_dec->p_sys = p_sys =
199 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
201 msg_Err( p_dec, "out of memory" );
206 p_sys->i_state = STATE_NOSYNC;
207 aout_DateSet( &p_sys->end_date, 0 );
208 p_sys->bytestream = block_BytestreamInit( p_dec );
209 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
210 p_sys->b_latm_cfg = VLC_FALSE;
212 /* Set output properties */
213 p_dec->fmt_out.i_cat = AUDIO_ES;
214 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','4','a');
216 msg_Dbg( p_dec, "running MPEG4 audio packetizer" );
218 if( p_dec->fmt_in.i_extra > 0 )
220 uint8_t *p_config = (uint8_t*)p_dec->fmt_in.p_extra;
223 i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) ) & 0x0f;
224 if( i_index != 0x0f )
226 p_dec->fmt_out.audio.i_rate = pi_sample_rates[i_index];
227 p_dec->fmt_out.audio.i_frame_length =
228 (( p_config[1] >> 2 ) & 0x01) ? 960 : 1024;
232 p_dec->fmt_out.audio.i_rate = ( ( p_config[1] & 0x7f ) << 17 ) |
233 ( p_config[2] << 9 ) | ( p_config[3] << 1 ) |
234 ( p_config[4] >> 7 );
235 p_dec->fmt_out.audio.i_frame_length =
236 (( p_config[4] >> 2 ) & 0x01) ? 960 : 1024;
239 msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
240 p_dec->fmt_out.audio.i_rate,
241 p_dec->fmt_out.audio.i_frame_length );
243 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
245 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
246 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
247 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
248 memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
249 p_dec->fmt_in.i_extra );
252 p_dec->pf_packetize = PacketizeRawBlock;
253 p_sys->i_type = TYPE_RAW;
257 msg_Dbg( p_dec, "no decoder specific info, must be an ADTS or LOAS stream" );
259 aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
261 /* We will try to create a AAC Config from adts/loas */
262 p_dec->fmt_out.i_extra = 0;
263 p_dec->fmt_out.p_extra = NULL;
266 p_dec->pf_packetize = PacketizeStreamBlock;
267 p_sys->i_type = TYPE_NONE;
273 /****************************************************************************
274 * PacketizeRawBlock: the whole thing
275 ****************************************************************************
276 * This function must be fed with complete frames.
277 ****************************************************************************/
278 static block_t *PacketizeRawBlock( decoder_t *p_dec, block_t **pp_block )
280 decoder_sys_t *p_sys = p_dec->p_sys;
283 if( !pp_block || !*pp_block ) return NULL;
285 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
287 //aout_DateSet( &p_sys->end_date, 0 );
288 block_Release( *pp_block );
293 *pp_block = NULL; /* Don't reuse this block */
295 if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
297 /* We've just started the stream, wait for the first PTS. */
298 block_Release( p_block );
301 else if( p_block->i_pts != 0 &&
302 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
304 aout_DateSet( &p_sys->end_date, p_block->i_pts );
307 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
309 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
310 p_dec->fmt_out.audio.i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) - p_block->i_pts;
315 /****************************************************************************
317 ****************************************************************************/
318 static int ADTSSyncInfo( decoder_t * p_dec, const byte_t * p_buf,
319 unsigned int * pi_channels,
320 unsigned int * pi_sample_rate,
321 unsigned int * pi_frame_length,
322 unsigned int * pi_header_size )
324 int i_id, i_profile, i_sample_rate_idx, i_frame_size;
327 /* Fixed header between frames */
328 i_id = ( (p_buf[1] >> 3) & 0x01 ) ? 2 : 4;
329 b_crc = !(p_buf[1] & 0x01);
330 i_profile = p_buf[2] >> 6;
331 i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
332 *pi_sample_rate = pi_sample_rates[i_sample_rate_idx];
333 *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
335 /* Variable header */
336 i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
337 ((p_buf[5] >> 5) & 0x7);
338 //i_raw_blocks_in_frame = (p_buf[6] & 0x02) + 1;
340 if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
346 *pi_frame_length = 1024;
348 /* Build the decoder specific info header */
349 if( !p_dec->fmt_out.i_extra )
351 p_dec->fmt_out.i_extra = 2;
352 p_dec->fmt_out.p_extra = malloc( 2 );
353 ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
354 (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
355 ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
356 ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
359 /* ADTS header length */
360 *pi_header_size = b_crc ? 9 : 7;
362 return i_frame_size - *pi_header_size;
364 /****************************************************************************
366 ****************************************************************************/
367 static int LOASSyncInfo( decoder_t *p_dec, uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_header_size )
370 return ( ( p_header[1] & 0x1f ) << 8 ) + p_header[2];
372 static int Mpeg4GAProgramConfigElement( bs_t *s )
374 /* TODO compute channels count ? */
375 int i_tag = bs_read( s, 4 );
378 bs_skip( s, 2 + 4 ); // object type + sampling index
379 int i_num_front = bs_read( s, 4 );
380 int i_num_side = bs_read( s, 4 );
381 int i_num_back = bs_read( s, 4 );
382 int i_num_lfe = bs_read( s, 2 );
383 int i_num_assoc_data = bs_read( s, 3 );
384 int i_num_valid_cc = bs_read( s, 4 );
387 bs_skip( s, 4 ); // mono downmix
389 bs_skip( s, 4 ); // stereo downmix
391 bs_skip( s, 2+1 ); // matrix downmix + pseudo_surround
393 bs_skip( s, i_num_front * (1+4) );
394 bs_skip( s, i_num_side * (1+4) );
395 bs_skip( s, i_num_back * (1+4) );
396 bs_skip( s, i_num_lfe * (4) );
397 bs_skip( s, i_num_assoc_data * (4) );
398 bs_skip( s, i_num_valid_cc * (5) );
400 int i_comment = bs_read( s, 8 );
401 bs_skip( s, i_comment * 8 );
404 static int Mpeg4GASpecificConfig( mpeg4_cfg_t *p_cfg, bs_t *s )
406 p_cfg->i_frame_length = bs_read1(s) ? 960 : 1024;
408 if( bs_read1( s ) ) // depend on core coder
409 bs_skip( s, 14 ); // core coder delay
411 int i_extension_flag = bs_read1( s );
412 if( p_cfg->i_channel == 0 )
414 Mpeg4GAProgramConfigElement( s );
416 if( p_cfg->i_object_type == 6 || p_cfg->i_object_type == 20 )
417 bs_skip( s, 3 ); // layer
419 if( i_extension_flag )
421 if( p_cfg->i_object_type == 22 )
423 bs_skip( s, 5 + 11 ); // numOfSubFrame + layer length
425 if( p_cfg->i_object_type == 17 || p_cfg->i_object_type == 19 ||
426 p_cfg->i_object_type == 20 || p_cfg->i_object_type == 23 )
428 bs_skip( s, 1+1+1 ); // ER data : section scale spectral */
430 if( bs_read1( s ) ) // extension 3
431 fprintf( stderr, "Mpeg4GASpecificConfig: error 1\n" );
435 static int Mpeg4ReadAudioObjectType( bs_t *s )
437 int i_type = bs_read( s, 5 );
439 i_type += bs_read( s, 6 );
442 static int Mpeg4ReadAudioSamplerate( bs_t *s )
444 int i_index = bs_read( s, 4 );
445 if( i_index != 0x0f )
446 return pi_sample_rates[i_index];
447 return bs_read( s, 24 );
449 static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_t *p_extra, bs_t *s, int i_max_size )
452 static const char *ppsz_otype[] = {
454 "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
457 "Reserved", "Reserved",
459 "Main Synthetic", "Wavetables Synthesis", "General MIDI",
460 "Algorithmic Synthesis and Audio FX",
463 "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
464 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
466 "Reserved", "Reserved", "Escape",
467 "Layer 1", "Layer 2", "Layer 3",
471 const int i_pos_start = bs_pos( s );
476 memset( p_cfg, 0, sizeof(*p_cfg) );
479 p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
480 p_cfg->i_samplerate = Mpeg4ReadAudioSamplerate( s );
482 p_cfg->i_channel = bs_read( s, 4 );
483 if( p_cfg->i_channel == 7 )
484 p_cfg->i_channel = 8; // 7.1
485 else if( p_cfg->i_channel >= 8 )
486 p_cfg->i_channel = -1;
489 p_cfg->extension.i_object_type = 0;
490 p_cfg->extension.i_samplerate = 0;
491 if( p_cfg->i_object_type == 5 )
494 p_cfg->extension.i_object_type = p_cfg->i_object_type;
495 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
497 p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
500 switch( p_cfg->i_object_type )
502 case 1: case 2: case 3: case 4:
504 case 17: case 19: case 20: case 21: case 22: case 23:
505 Mpeg4GASpecificConfig( p_cfg, s );
508 // CelpSpecificConfig();
511 // HvxcSpecificConfig();
514 // TTSSSpecificConfig();
516 case 13: case 14: case 15: case 16:
517 // StructuredAudioSpecificConfig();
520 // ERCelpSpecificConfig();
523 // ERHvxcSpecificConfig();
526 // ParametricSpecificConfig();
529 // SSCSpecificConfig();
531 case 32: case 33: case 34:
532 // MPEG_1_2_SpecificConfig();
535 // DSTSpecificConfig();
541 switch( p_cfg->i_object_type )
543 case 17: case 19: case 20: case 21: case 22: case 23:
544 case 24: case 25: case 26: case 27:
546 int epConfig = bs_read( s, 2 );
547 if( epConfig == 2 || epConfig == 3 )
549 //ErrorProtectionSpecificConfig();
553 int directMapping = bs_read1( s );
564 if( p_cfg->extension.i_object_type != 5 && i_max_size > 0 && i_max_size - (bs_pos(s) - i_pos_start) >= 16 &&
565 bs_read( s, 11 ) == 0x2b7 )
567 p_cfg->extension.i_object_type = Mpeg4ReadAudioObjectType( s );
568 if( p_cfg->extension.i_object_type == 5 )
570 p_cfg->i_sbr = bs_read1( s );
571 if( p_cfg->i_sbr == 1 )
572 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
576 //fprintf( stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
577 // ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr );
579 i_bits = bs_pos(s) - i_pos_start;
581 *pi_extra = ( i_bits + 7 ) / 8;
582 for( i = 0; i < __MIN( LATM_MAX_EXTRA_SIZE, *pi_extra ); i++ )
584 const int i_read = __MIN( 8, i_bits - 8*i );
585 p_extra[i] = bs_read( &s_sav, i_read ) << (8-i_read);
590 static int LatmGetValue( bs_t *s )
592 int i_bytes = bs_read( s, 2 );
595 for( i = 0; i < i_bytes; i++ )
596 v = (v << 8) + bs_read( s, 8 );
601 static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
607 i_mux_version = bs_read( s, 1 );
610 i_mux_versionA = bs_read( s, 1 );
612 if( i_mux_versionA != 0 ) /* support only A=0 */
615 memset( m, 0, sizeof(*m) );
617 if( i_mux_versionA == 0 )
619 if( i_mux_version == 1 )
621 LatmGetValue(s); /* taraBufferFullness */
625 m->b_same_time_framing = bs_read1( s );
626 m->i_sub_frames = 1 + bs_read( s, 6 );
627 m->i_programs = 1 + bs_read( s, 4 );
629 for( i_program = 0; i_program < m->i_programs; i_program++ )
633 m->pi_layers[i_program] = 1+bs_read( s, 3 );
635 for( i_layer = 0; i_layer < m->pi_layers[i_program]; i_layer++ )
637 latm_stream_t *st = &m->stream[m->i_streams];
638 vlc_bool_t b_previous_cfg;
640 m->pi_stream[i_program][i_layer] = m->i_streams;
641 st->i_program = i_program;
642 st->i_layer = i_layer;
644 b_previous_cfg = VLC_FALSE;
645 if( i_program != 0 || i_layer != 0 )
646 b_previous_cfg = bs_read1( s );
650 assert( m->i_streams > 0 );
651 st->cfg = m->stream[m->i_streams-1].cfg;
656 if( i_mux_version == 1 )
657 i_cfg_size = LatmGetValue(s);
658 i_cfg_size -= Mpeg4ReadAudioSpecificInfo( &st->cfg, &st->i_extra, st->extra, s, i_cfg_size );
660 bs_skip( s, i_cfg_size );
663 st->i_frame_length_type = bs_read( s, 3 );
664 switch( st->i_frame_length_type )
668 bs_skip( s, 8 ); /* latmBufferFullnes */
669 if( !m->b_same_time_framing )
671 if( st->cfg.i_object_type == 6 || st->cfg.i_object_type == 20 ||
672 st->cfg.i_object_type == 8 || st->cfg.i_object_type == 24 )
674 bs_skip( s, 6 ); /* eFrameOffset */
680 st->i_frame_length = bs_read( s, 9 );
682 case 3: case 4: case 5:
683 st->i_frame_length_index = bs_read( s, 6 ); // celp
686 st->i_frame_length_index = bs_read( s, 1 ); // hvxc
698 if( i_mux_version == 1 )
700 m->i_other_data = LatmGetValue( s );
706 b_continue = bs_read1(s);
707 m->i_other_data = (m->i_other_data << 8) + bs_read( s, 8 );
708 } while( b_continue );
715 m->i_crc = bs_read( s, 8 );
720 static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
722 decoder_sys_t *p_sys = p_dec->p_sys;
725 int i_accumulated = 0;
727 bs_init( &s, p_buffer, i_buffer );
729 /* Read the stream mux configuration if present */
730 if( !bs_read1( &s ) )
732 if( !LatmReadStreamMuxConfiguration( &p_sys->latm, &s ) && p_sys->latm.i_streams > 0 )
734 const latm_stream_t *st = &p_sys->latm.stream[0];
736 p_sys->i_channels = st->cfg.i_channel;
737 p_sys->i_rate = st->cfg.i_samplerate;
738 p_sys->i_frame_length = st->cfg.i_frame_length;
740 /* FIXME And if it changes ? */
741 if( !p_dec->fmt_out.i_extra && st->i_extra > 0 )
743 p_dec->fmt_out.i_extra = st->i_extra;
744 p_dec->fmt_out.p_extra = malloc( st->i_extra );
745 memcpy( p_dec->fmt_out.p_extra, st->extra, st->i_extra );
748 p_sys->b_latm_cfg = VLC_TRUE;
751 /* Wait for the configuration */
752 if( !p_sys->b_latm_cfg )
755 /* FIXME do we need to split the subframe into independant packet ? */
756 if( p_sys->latm.i_sub_frames > 1 )
757 msg_Err( p_dec, "latm sub frames not yet supported, please send a sample" );
759 for( i_sub = 0; i_sub < p_sys->latm.i_sub_frames; i_sub++ )
761 int pi_payload[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
762 if( p_sys->latm.b_same_time_framing )
766 for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
769 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
771 latm_stream_t *st = &p_sys->latm.stream[p_sys->latm.pi_stream[i_program][i_layer]];
772 if( st->i_frame_length_type == 0 )
777 int i_tmp = bs_read( &s, 8 );
782 pi_payload[i_program][i_layer] = i_payload;
784 else if( st->i_frame_length_type == 1 )
786 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
788 else if( st->i_frame_length_type == 3 || st->i_frame_length_type == 5 || st->i_frame_length_type == 7 )
790 bs_skip( &s, 2 ); // muxSlotLengthCoded
791 pi_payload[i_program][i_layer] = 0; /* TODO */
795 pi_payload[i_program][i_layer] = 0; /* TODO */
800 for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
804 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
806 /* XXX we only extract 1 stream */
807 if( i_program != 0 || i_layer != 0 )
810 if( pi_payload[i_program][i_layer] <= 0 )
813 /* FIXME that's slow (and a bit ugly to write in place) */
814 for( i = 0; i < pi_payload[i_program][i_layer]; i++ )
815 p_buffer[i_accumulated++] = bs_read( &s, 8 );
821 const int i_chunks = bs_read( &s, 4 );
826 msg_Err( p_dec, "latm without same time frameing not yet supported, please send a sample" );
828 for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
830 const int streamIndex = bs_read( &s, 4 );
831 latm_stream_t *st = &p_sys->latm.stream[streamIndex];
832 const int i_program = st->i_program;
833 const int i_layer = st->i_layer;
835 pi_program[i_chunk] = i_program;
836 pi_layer[i_chunk] = i_layer;
838 if( st->i_frame_length_type == 0 )
843 int i_tmp = bs_read( &s, 8 );
848 pi_payload[i_program][i_layer] = i_payload;
849 bs_skip( &s, 1 ); // auEndFlag
851 else if( st->i_frame_length_type == 1 )
853 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
855 else if( st->i_frame_length_type == 3 || st->i_frame_length_type == 5 || st->i_frame_length_type == 7 )
857 bs_read( &s, 2 ); // muxSlotLengthCoded
863 for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
865 //const int i_program = pi_program[i_chunk];
866 //const int i_layer = pi_layer[i_chunk];
873 if( p_sys->latm.i_other_data > 0 )
875 /* Other data XXX we just ignore them */
879 return i_accumulated;
882 /****************************************************************************
883 * PacketizeStreamBlock: ADTS/LOAS packetizer
884 ****************************************************************************/
885 static void SetupOutput( decoder_t *p_dec, block_t *p_block );
886 static block_t *PacketizeStreamBlock( decoder_t *p_dec, block_t **pp_block )
888 decoder_sys_t *p_sys = p_dec->p_sys;
889 uint8_t p_header[ADTS_HEADER_SIZE + LOAS_HEADER_SIZE];
890 block_t *p_out_buffer;
893 if( !pp_block || !*pp_block ) return NULL;
895 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
897 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
899 p_sys->i_state = STATE_NOSYNC;
900 block_BytestreamFlush( &p_sys->bytestream );
902 //aout_DateSet( &p_sys->end_date, 0 );
903 block_Release( *pp_block );
907 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
909 /* We've just started the stream, wait for the first PTS. */
910 block_Release( *pp_block );
914 if( (*pp_block)->i_rate > 0 )
915 p_sys->i_input_rate = (*pp_block)->i_rate;
917 block_BytestreamPush( &p_sys->bytestream, *pp_block );
921 switch( p_sys->i_state )
925 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
928 /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
929 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
931 if( p_sys->i_type != TYPE_ADTS )
932 msg_Dbg( p_dec, "detected ADTS format" );
934 p_sys->i_state = STATE_SYNC;
935 p_sys->i_type = TYPE_ADTS;
938 else if( p_header[0] == 0x56 && (p_header[1] & 0xe0) == 0xe0 )
940 if( p_sys->i_type != TYPE_LOAS )
941 msg_Dbg( p_dec, "detected LOAS format" );
943 p_sys->i_state = STATE_SYNC;
944 p_sys->i_type = TYPE_LOAS;
947 block_SkipByte( &p_sys->bytestream );
949 if( p_sys->i_state != STATE_SYNC )
951 block_BytestreamFlush( &p_sys->bytestream );
958 /* New frame, set the Presentation Time Stamp */
959 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
960 if( p_sys->i_pts != 0 &&
961 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
963 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
965 p_sys->i_state = STATE_HEADER;
969 if( p_sys->i_type == TYPE_ADTS )
971 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
972 if( block_PeekBytes( &p_sys->bytestream, p_header,
973 ADTS_HEADER_SIZE ) != VLC_SUCCESS )
979 /* Check if frame is valid and get frame info */
980 p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
983 &p_sys->i_frame_length,
984 &p_sys->i_header_size );
988 assert( p_sys->i_type == TYPE_LOAS );
989 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
990 if( block_PeekBytes( &p_sys->bytestream, p_header,
991 LOAS_HEADER_SIZE ) != VLC_SUCCESS )
997 /* Check if frame is valid and get frame info */
998 p_sys->i_frame_size = LOASSyncInfo( p_dec, p_header, &p_sys->i_header_size );
1001 if( p_sys->i_frame_size <= 0 )
1003 msg_Dbg( p_dec, "emulated sync word" );
1004 block_SkipByte( &p_sys->bytestream );
1005 p_sys->i_state = STATE_NOSYNC;
1009 p_sys->i_state = STATE_NEXT_SYNC;
1011 case STATE_NEXT_SYNC:
1012 /* TODO: If p_block == NULL, flush the buffer without checking the
1015 /* Check if next expected frame contains the sync word */
1016 if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
1017 + p_sys->i_header_size, p_header, 2 )
1020 /* Need more data */
1024 assert( p_sys->i_type == TYPE_ADTS || p_sys->i_type == TYPE_LOAS );
1025 if( ( p_sys->i_type == TYPE_ADTS && ( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 ) ) ||
1026 ( p_sys->i_type == TYPE_LOAS && ( p_header[0] != 0x56 || (p_header[1] & 0xe0) != 0xe0 ) ) )
1028 msg_Dbg( p_dec, "emulated sync word "
1029 "(no sync on following frame)" );
1030 p_sys->i_state = STATE_NOSYNC;
1031 block_SkipByte( &p_sys->bytestream );
1035 p_sys->i_state = STATE_SEND_DATA;
1038 case STATE_GET_DATA:
1039 /* Make sure we have enough data.
1040 * (Not useful if we went through NEXT_SYNC) */
1041 if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
1042 p_sys->i_header_size) != VLC_SUCCESS )
1044 /* Need more data */
1047 p_sys->i_state = STATE_SEND_DATA;
1049 case STATE_SEND_DATA:
1050 /* When we reach this point we already know we have enough
1051 * data available. */
1053 p_out_buffer = block_New( p_dec, p_sys->i_frame_size );
1056 //p_dec->b_error = VLC_TRUE;
1059 p_buf = p_out_buffer->p_buffer;
1061 /* Skip the ADTS/LOAS header */
1062 block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
1064 if( p_sys->i_type == TYPE_ADTS )
1066 /* Copy the whole frame into the buffer */
1067 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1071 assert( p_sys->i_type == TYPE_LOAS );
1072 /* Copy the whole frame into the buffer and parse/extract it */
1073 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1074 p_out_buffer->i_buffer = LOASParse( p_dec, p_buf, p_sys->i_frame_size );
1075 if( p_out_buffer->i_buffer <= 0 )
1077 if( !p_sys->b_latm_cfg )
1078 msg_Warn( p_dec, "waiting for header" );
1080 block_Release( p_out_buffer );
1081 p_out_buffer = NULL;
1082 p_sys->i_state = STATE_NOSYNC;
1086 SetupOutput( p_dec, p_out_buffer );
1087 /* Make sure we don't reuse the same pts twice */
1088 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
1089 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
1091 /* So p_block doesn't get re-added several times */
1092 *pp_block = block_BytestreamPop( &p_sys->bytestream );
1094 p_sys->i_state = STATE_NOSYNC;
1096 return p_out_buffer;
1104 /*****************************************************************************
1106 *****************************************************************************/
1107 static void SetupOutput( decoder_t *p_dec, block_t *p_block )
1109 decoder_sys_t *p_sys = p_dec->p_sys;
1111 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
1113 msg_Info( p_dec, "AAC channels: %d samplerate: %d",
1114 p_sys->i_channels, p_sys->i_rate );
1116 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
1117 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
1120 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
1121 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
1122 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
1123 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
1126 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
1127 p_dec->fmt_out.audio.i_physical_channels =
1128 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
1131 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
1133 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
1134 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
1138 /*****************************************************************************
1139 * ClosePacketizer: clean up the packetizer
1140 *****************************************************************************/
1141 static void ClosePacketizer( vlc_object_t *p_this )
1143 decoder_t *p_dec = (decoder_t *)p_this;
1144 decoder_sys_t *p_sys = p_dec->p_sys;
1146 block_BytestreamRelease( &p_sys->bytestream );
1148 free( p_dec->p_sys );