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;
158 static const int pi_sample_rates[16] =
160 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
161 16000, 12000, 11025, 8000, 7350, 0, 0, 0
164 #define ADTS_HEADER_SIZE 9
165 #define LOAS_HEADER_SIZE 3
167 /****************************************************************************
169 ****************************************************************************/
170 static int OpenPacketizer( vlc_object_t * );
171 static void ClosePacketizer( vlc_object_t * );
173 static block_t *PacketizeRawBlock ( decoder_t *, block_t ** );
174 static block_t *PacketizeStreamBlock( decoder_t *, block_t ** );
176 /*****************************************************************************
178 *****************************************************************************/
180 set_category( CAT_SOUT );
181 set_subcategory( SUBCAT_SOUT_PACKETIZER );
182 set_description( _("MPEG4 audio packetizer") );
183 set_capability( "packetizer", 50 );
184 set_callbacks( OpenPacketizer, ClosePacketizer );
187 /*****************************************************************************
188 * OpenPacketizer: probe the packetizer and return score
189 *****************************************************************************/
190 static int OpenPacketizer( vlc_object_t *p_this )
192 decoder_t *p_dec = (decoder_t*)p_this;
193 decoder_sys_t *p_sys;
195 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', '4', 'a' ) )
200 /* Allocate the memory needed to store the decoder's structure */
201 if( ( p_dec->p_sys = p_sys =
202 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
204 msg_Err( p_dec, "out of memory" );
209 p_sys->i_state = STATE_NOSYNC;
210 aout_DateSet( &p_sys->end_date, 0 );
211 p_sys->bytestream = block_BytestreamInit();
212 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
213 p_sys->b_latm_cfg = VLC_FALSE;
215 /* Set output properties */
216 p_dec->fmt_out.i_cat = AUDIO_ES;
217 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','4','a');
219 msg_Dbg( p_dec, "running MPEG4 audio packetizer" );
221 if( p_dec->fmt_in.i_extra > 0 )
223 uint8_t *p_config = (uint8_t*)p_dec->fmt_in.p_extra;
226 i_index = ( ( p_config[0] << 1 ) | ( p_config[1] >> 7 ) ) & 0x0f;
227 if( i_index != 0x0f )
229 p_dec->fmt_out.audio.i_rate = pi_sample_rates[i_index];
230 p_dec->fmt_out.audio.i_frame_length =
231 (( p_config[1] >> 2 ) & 0x01) ? 960 : 1024;
235 p_dec->fmt_out.audio.i_rate = ( ( p_config[1] & 0x7f ) << 17 ) |
236 ( p_config[2] << 9 ) | ( p_config[3] << 1 ) |
237 ( p_config[4] >> 7 );
238 p_dec->fmt_out.audio.i_frame_length =
239 (( p_config[4] >> 2 ) & 0x01) ? 960 : 1024;
242 msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
243 p_dec->fmt_out.audio.i_rate,
244 p_dec->fmt_out.audio.i_frame_length );
246 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
248 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
249 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
250 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
251 if( !p_dec->fmt_out.p_extra )
253 p_dec->fmt_out.i_extra = 0;
256 memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
257 p_dec->fmt_in.i_extra );
260 p_dec->pf_packetize = PacketizeRawBlock;
261 p_sys->i_type = TYPE_RAW;
265 msg_Dbg( p_dec, "no decoder specific info, must be an ADTS or LOAS stream" );
267 aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
269 /* We will try to create a AAC Config from adts/loas */
270 p_dec->fmt_out.i_extra = 0;
271 p_dec->fmt_out.p_extra = NULL;
274 p_dec->pf_packetize = PacketizeStreamBlock;
275 p_sys->i_type = TYPE_NONE;
281 /****************************************************************************
282 * PacketizeRawBlock: the whole thing
283 ****************************************************************************
284 * This function must be fed with complete frames.
285 ****************************************************************************/
286 static block_t *PacketizeRawBlock( decoder_t *p_dec, block_t **pp_block )
288 decoder_sys_t *p_sys = p_dec->p_sys;
291 if( !pp_block || !*pp_block ) return NULL;
293 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
295 //aout_DateSet( &p_sys->end_date, 0 );
296 block_Release( *pp_block );
301 *pp_block = NULL; /* Don't reuse this block */
303 if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
305 /* We've just started the stream, wait for the first PTS. */
306 block_Release( p_block );
309 else if( p_block->i_pts != 0 &&
310 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
312 aout_DateSet( &p_sys->end_date, p_block->i_pts );
315 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
317 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
318 p_dec->fmt_out.audio.i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) - p_block->i_pts;
323 /****************************************************************************
325 ****************************************************************************/
326 static int ADTSSyncInfo( decoder_t * p_dec, const byte_t * p_buf,
327 unsigned int * pi_channels,
328 unsigned int * pi_sample_rate,
329 unsigned int * pi_frame_length,
330 unsigned int * pi_header_size )
332 int i_id, i_profile, i_sample_rate_idx, i_frame_size;
335 /* Fixed header between frames */
336 i_id = ( (p_buf[1] >> 3) & 0x01 ) ? 2 : 4;
337 b_crc = !(p_buf[1] & 0x01);
338 i_profile = p_buf[2] >> 6;
339 i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
340 *pi_sample_rate = pi_sample_rates[i_sample_rate_idx];
341 *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
343 /* Variable header */
344 i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
345 ((p_buf[5] >> 5) & 0x7);
346 //i_raw_blocks_in_frame = (p_buf[6] & 0x02) + 1;
348 if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
354 *pi_frame_length = 1024;
356 /* Build the decoder specific info header */
357 if( !p_dec->fmt_out.i_extra )
359 p_dec->fmt_out.p_extra = malloc( 2 );
360 if( !p_dec->fmt_out.p_extra )
362 p_dec->fmt_out.i_extra = 0;
365 p_dec->fmt_out.i_extra = 2;
366 ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
367 (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
368 ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
369 ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
372 /* ADTS header length */
373 *pi_header_size = b_crc ? 9 : 7;
375 return i_frame_size - *pi_header_size;
378 /****************************************************************************
380 ****************************************************************************/
381 static int LOASSyncInfo( uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_header_size )
384 return ( ( p_header[1] & 0x1f ) << 8 ) + p_header[2];
387 static int Mpeg4GAProgramConfigElement( bs_t *s )
389 /* TODO compute channels count ? */
390 int i_tag = bs_read( s, 4 );
393 bs_skip( s, 2 + 4 ); // object type + sampling index
394 int i_num_front = bs_read( s, 4 );
395 int i_num_side = bs_read( s, 4 );
396 int i_num_back = bs_read( s, 4 );
397 int i_num_lfe = bs_read( s, 2 );
398 int i_num_assoc_data = bs_read( s, 3 );
399 int i_num_valid_cc = bs_read( s, 4 );
402 bs_skip( s, 4 ); // mono downmix
404 bs_skip( s, 4 ); // stereo downmix
406 bs_skip( s, 2+1 ); // matrix downmix + pseudo_surround
408 bs_skip( s, i_num_front * (1+4) );
409 bs_skip( s, i_num_side * (1+4) );
410 bs_skip( s, i_num_back * (1+4) );
411 bs_skip( s, i_num_lfe * (4) );
412 bs_skip( s, i_num_assoc_data * (4) );
413 bs_skip( s, i_num_valid_cc * (5) );
415 int i_comment = bs_read( s, 8 );
416 bs_skip( s, i_comment * 8 );
420 static int Mpeg4GASpecificConfig( mpeg4_cfg_t *p_cfg, bs_t *s )
422 p_cfg->i_frame_length = bs_read1(s) ? 960 : 1024;
424 if( bs_read1( s ) ) // depend on core coder
425 bs_skip( s, 14 ); // core coder delay
427 int i_extension_flag = bs_read1( s );
428 if( p_cfg->i_channel == 0 )
430 Mpeg4GAProgramConfigElement( s );
432 if( p_cfg->i_object_type == 6 || p_cfg->i_object_type == 20 )
433 bs_skip( s, 3 ); // layer
435 if( i_extension_flag )
437 if( p_cfg->i_object_type == 22 )
439 bs_skip( s, 5 + 11 ); // numOfSubFrame + layer length
441 if( p_cfg->i_object_type == 17 || p_cfg->i_object_type == 19 ||
442 p_cfg->i_object_type == 20 || p_cfg->i_object_type == 23 )
444 bs_skip( s, 1+1+1 ); // ER data : section scale spectral */
446 if( bs_read1( s ) ) // extension 3
447 fprintf( stderr, "Mpeg4GASpecificConfig: error 1\n" );
452 static int Mpeg4ReadAudioObjectType( bs_t *s )
454 int i_type = bs_read( s, 5 );
456 i_type += bs_read( s, 6 );
460 static int Mpeg4ReadAudioSamplerate( bs_t *s )
462 int i_index = bs_read( s, 4 );
463 if( i_index != 0x0f )
464 return pi_sample_rates[i_index];
465 return bs_read( s, 24 );
468 static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_t *p_extra, bs_t *s, int i_max_size )
471 static const char *ppsz_otype[] = {
473 "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
476 "Reserved", "Reserved",
478 "Main Synthetic", "Wavetables Synthesis", "General MIDI",
479 "Algorithmic Synthesis and Audio FX",
482 "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
483 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
485 "Reserved", "Reserved", "Escape",
486 "Layer 1", "Layer 2", "Layer 3",
490 const int i_pos_start = bs_pos( s );
495 memset( p_cfg, 0, sizeof(*p_cfg) );
498 p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
499 p_cfg->i_samplerate = Mpeg4ReadAudioSamplerate( s );
501 p_cfg->i_channel = bs_read( s, 4 );
502 if( p_cfg->i_channel == 7 )
503 p_cfg->i_channel = 8; // 7.1
504 else if( p_cfg->i_channel >= 8 )
505 p_cfg->i_channel = -1;
508 p_cfg->extension.i_object_type = 0;
509 p_cfg->extension.i_samplerate = 0;
510 if( p_cfg->i_object_type == 5 )
513 p_cfg->extension.i_object_type = p_cfg->i_object_type;
514 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
516 p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
519 switch( p_cfg->i_object_type )
521 case 1: case 2: case 3: case 4:
523 case 17: case 19: case 20: case 21: case 22: case 23:
524 Mpeg4GASpecificConfig( p_cfg, s );
527 // CelpSpecificConfig();
530 // HvxcSpecificConfig();
533 // TTSSSpecificConfig();
535 case 13: case 14: case 15: case 16:
536 // StructuredAudioSpecificConfig();
539 // ERCelpSpecificConfig();
542 // ERHvxcSpecificConfig();
545 // ParametricSpecificConfig();
548 // SSCSpecificConfig();
550 case 32: case 33: case 34:
551 // MPEG_1_2_SpecificConfig();
554 // DSTSpecificConfig();
560 switch( p_cfg->i_object_type )
562 case 17: case 19: case 20: case 21: case 22: case 23:
563 case 24: case 25: case 26: case 27:
565 int epConfig = bs_read( s, 2 );
566 if( epConfig == 2 || epConfig == 3 )
568 //ErrorProtectionSpecificConfig();
572 int directMapping = bs_read1( s );
584 if( p_cfg->extension.i_object_type != 5 && i_max_size > 0 && i_max_size - (bs_pos(s) - i_pos_start) >= 16 &&
585 bs_read( s, 11 ) == 0x2b7 )
587 p_cfg->extension.i_object_type = Mpeg4ReadAudioObjectType( s );
588 if( p_cfg->extension.i_object_type == 5 )
590 p_cfg->i_sbr = bs_read1( s );
591 if( p_cfg->i_sbr == 1 )
592 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
596 //fprintf( stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
597 // ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr );
599 i_bits = bs_pos(s) - i_pos_start;
601 *pi_extra = ( i_bits + 7 ) / 8;
602 for( i = 0; i < __MIN( LATM_MAX_EXTRA_SIZE, *pi_extra ); i++ )
604 const int i_read = __MIN( 8, i_bits - 8*i );
605 p_extra[i] = bs_read( &s_sav, i_read ) << (8-i_read);
610 static int LatmGetValue( bs_t *s )
612 int i_bytes = bs_read( s, 2 );
615 for( i = 0; i < i_bytes; i++ )
616 v = (v << 8) + bs_read( s, 8 );
621 static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
627 i_mux_version = bs_read( s, 1 );
630 i_mux_versionA = bs_read( s, 1 );
632 if( i_mux_versionA != 0 ) /* support only A=0 */
635 memset( m, 0, sizeof(*m) );
637 if( i_mux_versionA == 0 )
639 if( i_mux_version == 1 )
641 LatmGetValue(s); /* taraBufferFullness */
645 m->b_same_time_framing = bs_read1( s );
646 m->i_sub_frames = 1 + bs_read( s, 6 );
647 m->i_programs = 1 + bs_read( s, 4 );
649 for( i_program = 0; i_program < m->i_programs; i_program++ )
653 m->pi_layers[i_program] = 1+bs_read( s, 3 );
655 for( i_layer = 0; i_layer < m->pi_layers[i_program]; i_layer++ )
657 latm_stream_t *st = &m->stream[m->i_streams];
658 vlc_bool_t b_previous_cfg;
660 m->pi_stream[i_program][i_layer] = m->i_streams;
661 st->i_program = i_program;
662 st->i_layer = i_layer;
664 b_previous_cfg = VLC_FALSE;
665 if( i_program != 0 || i_layer != 0 )
666 b_previous_cfg = bs_read1( s );
670 assert( m->i_streams > 0 );
671 st->cfg = m->stream[m->i_streams-1].cfg;
676 if( i_mux_version == 1 )
677 i_cfg_size = LatmGetValue(s);
678 i_cfg_size -= Mpeg4ReadAudioSpecificInfo( &st->cfg, &st->i_extra, st->extra, s, i_cfg_size );
680 bs_skip( s, i_cfg_size );
683 st->i_frame_length_type = bs_read( s, 3 );
684 switch( st->i_frame_length_type )
688 bs_skip( s, 8 ); /* latmBufferFullnes */
689 if( !m->b_same_time_framing )
691 if( st->cfg.i_object_type == 6 || st->cfg.i_object_type == 20 ||
692 st->cfg.i_object_type == 8 || st->cfg.i_object_type == 24 )
694 bs_skip( s, 6 ); /* eFrameOffset */
700 st->i_frame_length = bs_read( s, 9 );
702 case 3: case 4: case 5:
703 st->i_frame_length_index = bs_read( s, 6 ); // celp
706 st->i_frame_length_index = bs_read( s, 1 ); // hvxc
718 if( i_mux_version == 1 )
720 m->i_other_data = LatmGetValue( s );
726 b_continue = bs_read1(s);
727 m->i_other_data = (m->i_other_data << 8) + bs_read( s, 8 );
728 } while( b_continue );
735 m->i_crc = bs_read( s, 8 );
740 static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
742 decoder_sys_t *p_sys = p_dec->p_sys;
745 int i_accumulated = 0;
747 bs_init( &s, p_buffer, i_buffer );
749 /* Read the stream mux configuration if present */
750 if( !bs_read1( &s ) )
752 if( !LatmReadStreamMuxConfiguration( &p_sys->latm, &s ) &&
753 p_sys->latm.i_streams > 0 )
755 const latm_stream_t *st = &p_sys->latm.stream[0];
757 p_sys->i_channels = st->cfg.i_channel;
758 p_sys->i_rate = st->cfg.i_samplerate;
759 p_sys->i_frame_length = st->cfg.i_frame_length;
761 /* FIXME And if it changes ? */
762 if( !p_dec->fmt_out.i_extra && st->i_extra > 0 )
764 p_dec->fmt_out.i_extra = st->i_extra;
765 p_dec->fmt_out.p_extra = malloc( st->i_extra );
766 if( !p_dec->fmt_out.p_extra )
768 p_dec->fmt_out.i_extra = 0;
771 memcpy( p_dec->fmt_out.p_extra, st->extra, st->i_extra );
774 p_sys->b_latm_cfg = VLC_TRUE;
777 /* Wait for the configuration */
778 if( !p_sys->b_latm_cfg )
781 /* FIXME do we need to split the subframe into independant packet ? */
782 if( p_sys->latm.i_sub_frames > 1 )
783 msg_Err( p_dec, "latm sub frames not yet supported, please send a sample" );
785 for( i_sub = 0; i_sub < p_sys->latm.i_sub_frames; i_sub++ )
787 int pi_payload[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
788 if( p_sys->latm.b_same_time_framing )
792 for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
795 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
797 latm_stream_t *st = &p_sys->latm.stream[p_sys->latm.pi_stream[i_program][i_layer]];
798 if( st->i_frame_length_type == 0 )
803 int i_tmp = bs_read( &s, 8 );
808 pi_payload[i_program][i_layer] = i_payload;
810 else if( st->i_frame_length_type == 1 )
812 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
814 else if( ( st->i_frame_length_type == 3 ) ||
815 ( st->i_frame_length_type == 5 ) ||
816 ( st->i_frame_length_type == 7 ) )
818 bs_skip( &s, 2 ); // muxSlotLengthCoded
819 pi_payload[i_program][i_layer] = 0; /* TODO */
823 pi_payload[i_program][i_layer] = 0; /* TODO */
828 for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
832 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
834 /* XXX we only extract 1 stream */
835 if( i_program != 0 || i_layer != 0 )
838 if( pi_payload[i_program][i_layer] <= 0 )
841 /* FIXME that's slow (and a bit ugly to write in place) */
842 for( i = 0; i < pi_payload[i_program][i_layer]; i++ )
843 p_buffer[i_accumulated++] = bs_read( &s, 8 );
849 const int i_chunks = bs_read( &s, 4 );
854 msg_Err( p_dec, "latm without same time frameing not yet supported, please send a sample" );
856 for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
858 const int streamIndex = bs_read( &s, 4 );
859 latm_stream_t *st = &p_sys->latm.stream[streamIndex];
860 const int i_program = st->i_program;
861 const int i_layer = st->i_layer;
863 pi_program[i_chunk] = i_program;
864 pi_layer[i_chunk] = i_layer;
866 if( st->i_frame_length_type == 0 )
871 int i_tmp = bs_read( &s, 8 );
876 pi_payload[i_program][i_layer] = i_payload;
877 bs_skip( &s, 1 ); // auEndFlag
879 else if( st->i_frame_length_type == 1 )
881 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
883 else if( ( st->i_frame_length_type == 3 ) ||
884 ( st->i_frame_length_type == 5 ) ||
885 ( st->i_frame_length_type == 7 ) )
887 bs_read( &s, 2 ); // muxSlotLengthCoded
893 for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
895 //const int i_program = pi_program[i_chunk];
896 //const int i_layer = pi_layer[i_chunk];
903 if( p_sys->latm.i_other_data > 0 )
905 /* Other data XXX we just ignore them */
909 return i_accumulated;
912 /****************************************************************************
913 * PacketizeStreamBlock: ADTS/LOAS packetizer
914 ****************************************************************************/
915 static void SetupOutput( decoder_t *p_dec, block_t *p_block );
916 static block_t *PacketizeStreamBlock( decoder_t *p_dec, block_t **pp_block )
918 decoder_sys_t *p_sys = p_dec->p_sys;
919 uint8_t p_header[ADTS_HEADER_SIZE + LOAS_HEADER_SIZE];
920 block_t *p_out_buffer;
923 if( !pp_block || !*pp_block ) return NULL;
925 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
927 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
929 p_sys->i_state = STATE_NOSYNC;
930 block_BytestreamFlush( &p_sys->bytestream );
932 //aout_DateSet( &p_sys->end_date, 0 );
933 block_Release( *pp_block );
937 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
939 /* We've just started the stream, wait for the first PTS. */
940 block_Release( *pp_block );
944 if( (*pp_block)->i_rate > 0 )
945 p_sys->i_input_rate = (*pp_block)->i_rate;
947 block_BytestreamPush( &p_sys->bytestream, *pp_block );
951 switch( p_sys->i_state )
955 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
958 /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
959 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
961 if( p_sys->i_type != TYPE_ADTS )
962 msg_Dbg( p_dec, "detected ADTS format" );
964 p_sys->i_state = STATE_SYNC;
965 p_sys->i_type = TYPE_ADTS;
968 else if( p_header[0] == 0x56 && (p_header[1] & 0xe0) == 0xe0 )
970 if( p_sys->i_type != TYPE_LOAS )
971 msg_Dbg( p_dec, "detected LOAS format" );
973 p_sys->i_state = STATE_SYNC;
974 p_sys->i_type = TYPE_LOAS;
977 block_SkipByte( &p_sys->bytestream );
979 if( p_sys->i_state != STATE_SYNC )
981 block_BytestreamFlush( &p_sys->bytestream );
988 /* New frame, set the Presentation Time Stamp */
989 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
990 if( p_sys->i_pts != 0 &&
991 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
993 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
995 p_sys->i_state = STATE_HEADER;
999 if( p_sys->i_type == TYPE_ADTS )
1001 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
1002 if( block_PeekBytes( &p_sys->bytestream, p_header,
1003 ADTS_HEADER_SIZE ) != VLC_SUCCESS )
1005 /* Need more data */
1009 /* Check if frame is valid and get frame info */
1010 p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
1013 &p_sys->i_frame_length,
1014 &p_sys->i_header_size );
1018 assert( p_sys->i_type == TYPE_LOAS );
1019 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
1020 if( block_PeekBytes( &p_sys->bytestream, p_header,
1021 LOAS_HEADER_SIZE ) != VLC_SUCCESS )
1023 /* Need more data */
1027 /* Check if frame is valid and get frame info */
1028 p_sys->i_frame_size = LOASSyncInfo( p_header, &p_sys->i_header_size );
1031 if( p_sys->i_frame_size <= 0 )
1033 msg_Dbg( p_dec, "emulated sync word" );
1034 block_SkipByte( &p_sys->bytestream );
1035 p_sys->i_state = STATE_NOSYNC;
1039 p_sys->i_state = STATE_NEXT_SYNC;
1041 case STATE_NEXT_SYNC:
1042 /* TODO: If p_block == NULL, flush the buffer without checking the
1044 if( p_sys->bytestream.p_block == NULL )
1046 p_sys->i_state = STATE_NOSYNC;
1047 block_BytestreamFlush( &p_sys->bytestream );
1051 /* Check if next expected frame contains the sync word */
1052 if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
1053 + p_sys->i_header_size, p_header, 2 )
1056 /* Need more data */
1060 assert( (p_sys->i_type == TYPE_ADTS) || (p_sys->i_type == TYPE_LOAS) );
1061 if( ( ( p_sys->i_type == TYPE_ADTS ) &&
1062 ( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 ) ) ||
1063 ( ( p_sys->i_type == TYPE_LOAS ) &&
1064 ( p_header[0] != 0x56 || (p_header[1] & 0xe0) != 0xe0 ) ) )
1066 msg_Dbg( p_dec, "emulated sync word "
1067 "(no sync on following frame)" );
1068 p_sys->i_state = STATE_NOSYNC;
1069 block_SkipByte( &p_sys->bytestream );
1073 p_sys->i_state = STATE_SEND_DATA;
1076 case STATE_GET_DATA:
1077 /* Make sure we have enough data.
1078 * (Not useful if we went through NEXT_SYNC) */
1079 if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
1080 p_sys->i_header_size) != VLC_SUCCESS )
1082 /* Need more data */
1085 p_sys->i_state = STATE_SEND_DATA;
1087 case STATE_SEND_DATA:
1088 /* When we reach this point we already know we have enough
1089 * data available. */
1091 p_out_buffer = block_New( p_dec, p_sys->i_frame_size );
1094 //p_dec->b_error = VLC_TRUE;
1097 p_buf = p_out_buffer->p_buffer;
1099 /* Skip the ADTS/LOAS header */
1100 block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
1102 if( p_sys->i_type == TYPE_ADTS )
1104 /* Copy the whole frame into the buffer */
1105 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1109 assert( p_sys->i_type == TYPE_LOAS );
1110 /* Copy the whole frame into the buffer and parse/extract it */
1111 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1112 p_out_buffer->i_buffer = LOASParse( p_dec, p_buf, p_sys->i_frame_size );
1113 if( p_out_buffer->i_buffer <= 0 )
1115 if( !p_sys->b_latm_cfg )
1116 msg_Warn( p_dec, "waiting for header" );
1118 block_Release( p_out_buffer );
1119 p_out_buffer = NULL;
1120 p_sys->i_state = STATE_NOSYNC;
1124 SetupOutput( p_dec, p_out_buffer );
1125 /* Make sure we don't reuse the same pts twice */
1126 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
1127 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
1129 /* So p_block doesn't get re-added several times */
1130 *pp_block = block_BytestreamPop( &p_sys->bytestream );
1132 p_sys->i_state = STATE_NOSYNC;
1134 return p_out_buffer;
1141 /*****************************************************************************
1143 *****************************************************************************/
1144 static void SetupOutput( decoder_t *p_dec, block_t *p_block )
1146 decoder_sys_t *p_sys = p_dec->p_sys;
1148 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
1150 msg_Info( p_dec, "AAC channels: %d samplerate: %d",
1151 p_sys->i_channels, p_sys->i_rate );
1153 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
1154 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
1157 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
1158 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
1159 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
1160 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
1163 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
1164 p_dec->fmt_out.audio.i_physical_channels =
1165 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
1168 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
1170 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
1171 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
1175 /*****************************************************************************
1176 * ClosePacketizer: clean up the packetizer
1177 *****************************************************************************/
1178 static void ClosePacketizer( vlc_object_t *p_this )
1180 decoder_t *p_dec = (decoder_t *)p_this;
1181 decoder_sys_t *p_sys = p_dec->p_sys;
1183 block_BytestreamRelease( &p_sys->bytestream );
1185 free( p_dec->p_sys );