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 *****************************************************************************/
34 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_block.h>
39 #include <vlc_codecs.h>
40 #include <vlc_input.h>
43 #include "vlc_block_helper.h"
47 * AudioObjectType 5 bits
48 * samplingFrequencyIndex 4 bits
49 * if (samplingFrequencyIndex == 0xF)
50 * samplingFrequency 24 bits
51 * channelConfiguration 4 bits
53 * FrameLengthFlag 1 bit 1024 or 960
54 * DependsOnCoreCoder 1 bit (always 0)
55 * ExtensionFlag 1 bit (always 0)
58 /*****************************************************************************
59 * decoder_sys_t : decoder descriptor
60 *****************************************************************************/
66 int i_sbr; // 0: no sbr, 1: sbr, -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;
129 audio_date_t end_date;
133 unsigned int i_channels;
134 unsigned int i_rate, i_frame_length, i_header_size;
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();
213 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
214 p_sys->b_latm_cfg = 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 p_dec->fmt_out.audio.i_channels =
244 (p_config[i_index == 0x0f ? 4 : 1] >> 3) & 0x0f;
246 msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
247 p_dec->fmt_out.audio.i_rate,
248 p_dec->fmt_out.audio.i_frame_length );
250 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
252 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
253 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
254 if( !p_dec->fmt_out.p_extra )
256 p_dec->fmt_out.i_extra = 0;
259 memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
260 p_dec->fmt_in.i_extra );
263 p_dec->pf_packetize = PacketizeRawBlock;
264 p_sys->i_type = TYPE_RAW;
268 msg_Dbg( p_dec, "no decoder specific info, must be an ADTS or LOAS stream" );
270 aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
272 /* We will try to create a AAC Config from adts/loas */
273 p_dec->fmt_out.i_extra = 0;
274 p_dec->fmt_out.p_extra = NULL;
277 p_dec->pf_packetize = PacketizeStreamBlock;
278 p_sys->i_type = TYPE_NONE;
284 /****************************************************************************
285 * PacketizeRawBlock: the whole thing
286 ****************************************************************************
287 * This function must be fed with complete frames.
288 ****************************************************************************/
289 static block_t *PacketizeRawBlock( decoder_t *p_dec, block_t **pp_block )
291 decoder_sys_t *p_sys = p_dec->p_sys;
294 if( !pp_block || !*pp_block ) return NULL;
296 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
298 //aout_DateSet( &p_sys->end_date, 0 );
299 block_Release( *pp_block );
304 *pp_block = NULL; /* Don't reuse this block */
306 if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
308 /* We've just started the stream, wait for the first PTS. */
309 block_Release( p_block );
312 else if( p_block->i_pts != 0 &&
313 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
315 aout_DateSet( &p_sys->end_date, p_block->i_pts );
318 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
320 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
321 p_dec->fmt_out.audio.i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) - p_block->i_pts;
326 /****************************************************************************
328 ****************************************************************************/
329 static int ADTSSyncInfo( decoder_t * p_dec, const uint8_t * p_buf,
330 unsigned int * pi_channels,
331 unsigned int * pi_sample_rate,
332 unsigned int * pi_frame_length,
333 unsigned int * pi_header_size )
335 int i_profile, i_sample_rate_idx, i_frame_size;
338 /* Fixed header between frames */
339 //int i_id = ( (p_buf[1] >> 3) & 0x01) ? 2 : 4; /* MPEG-2 or 4 */
340 b_crc = !(p_buf[1] & 0x01);
341 i_profile = p_buf[2] >> 6;
342 i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
343 *pi_sample_rate = pi_sample_rates[i_sample_rate_idx];
344 //private_bit = (p_buf[2] >> 1) & 0x01;
345 *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
346 //original_copy = (p_buf[3] >> 5) & 0x01;
347 //home = (p_buf[3] >> 4) & 0x01;
349 /* Variable header */
350 //copyright_id_bit = (p_buf[3] >> 3) & 0x01;
351 //copyright_id_start = (p_buf[3] >> 2) & 0x01;
352 i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
353 ((p_buf[5] >> 5) /*& 0x7*/);
354 //uint16_t buffer_fullness = ((p_buf[5] & 0x1f) << 6) | (p_buf[6] >> 2);
355 unsigned short i_raw_blocks_in_frame = p_buf[6] & 0x03;
357 if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
359 msg_Warn( p_dec, "Invalid ADTS header" );
363 *pi_frame_length = 1024;
365 if( i_raw_blocks_in_frame == 0 )
369 msg_Warn( p_dec, "ADTS CRC not supported" );
370 //uint16_t crc = (p_buf[7] << 8) | p_buf[8];
375 msg_Err( p_dec, "Multiple blocks per frame in ADTS not supported" );
379 const uint8_t *p_pos = p_buf + 7;
381 uint16_t i_block_pos[3];
384 for( i = 0 ; i < i_raw_blocks_in_frame ; i++ )
385 { /* the 1st block's position is known ... */
386 i_block_pos[i] = (*p_pos << 8) | *(p_pos+1);
389 crc_block = (*p_pos << 8) | *(p_pos+1);
392 for( i = 0 ; i <= i_raw_blocks_in_frame ; i++ )
397 msg_Err( p_dec, "ADTS CRC not supported" );
398 //uint16_t crc = (*p_pos << 8) | *(p_pos+1);
406 /* Build the decoder specific info header */
407 if( !p_dec->fmt_out.i_extra )
409 p_dec->fmt_out.p_extra = malloc( 2 );
410 if( !p_dec->fmt_out.p_extra )
412 p_dec->fmt_out.i_extra = 0;
415 p_dec->fmt_out.i_extra = 2;
416 ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
417 (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
418 ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
419 ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
422 /* ADTS header length */
423 *pi_header_size = b_crc ? 9 : 7;
425 return i_frame_size - *pi_header_size;
428 /****************************************************************************
430 ****************************************************************************/
431 static int LOASSyncInfo( uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_header_size )
434 return ( ( p_header[1] & 0x1f ) << 8 ) + p_header[2];
437 static int Mpeg4GAProgramConfigElement( bs_t *s )
439 /* TODO compute channels count ? */
440 int i_tag = bs_read( s, 4 );
443 bs_skip( s, 2 + 4 ); // object type + sampling index
444 int i_num_front = bs_read( s, 4 );
445 int i_num_side = bs_read( s, 4 );
446 int i_num_back = bs_read( s, 4 );
447 int i_num_lfe = bs_read( s, 2 );
448 int i_num_assoc_data = bs_read( s, 3 );
449 int i_num_valid_cc = bs_read( s, 4 );
452 bs_skip( s, 4 ); // mono downmix
454 bs_skip( s, 4 ); // stereo downmix
456 bs_skip( s, 2+1 ); // matrix downmix + pseudo_surround
458 bs_skip( s, i_num_front * (1+4) );
459 bs_skip( s, i_num_side * (1+4) );
460 bs_skip( s, i_num_back * (1+4) );
461 bs_skip( s, i_num_lfe * (4) );
462 bs_skip( s, i_num_assoc_data * (4) );
463 bs_skip( s, i_num_valid_cc * (5) );
465 int i_comment = bs_read( s, 8 );
466 bs_skip( s, i_comment * 8 );
470 static int Mpeg4GASpecificConfig( mpeg4_cfg_t *p_cfg, bs_t *s )
472 p_cfg->i_frame_length = bs_read1(s) ? 960 : 1024;
474 if( bs_read1( s ) ) // depend on core coder
475 bs_skip( s, 14 ); // core coder delay
477 int i_extension_flag = bs_read1( s );
478 if( p_cfg->i_channel == 0 )
480 Mpeg4GAProgramConfigElement( s );
482 if( p_cfg->i_object_type == 6 || p_cfg->i_object_type == 20 )
483 bs_skip( s, 3 ); // layer
485 if( i_extension_flag )
487 if( p_cfg->i_object_type == 22 )
489 bs_skip( s, 5 + 11 ); // numOfSubFrame + layer length
491 if( p_cfg->i_object_type == 17 || p_cfg->i_object_type == 19 ||
492 p_cfg->i_object_type == 20 || p_cfg->i_object_type == 23 )
494 bs_skip( s, 1+1+1 ); // ER data : section scale spectral */
496 if( bs_read1( s ) ) // extension 3
497 fprintf( stderr, "Mpeg4GASpecificConfig: error 1\n" );
502 static int Mpeg4ReadAudioObjectType( bs_t *s )
504 int i_type = bs_read( s, 5 );
506 i_type += bs_read( s, 6 );
510 static int Mpeg4ReadAudioSamplerate( bs_t *s )
512 int i_index = bs_read( s, 4 );
513 if( i_index != 0x0f )
514 return pi_sample_rates[i_index];
515 return bs_read( s, 24 );
518 static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_t *p_extra, bs_t *s, int i_max_size )
521 static const char *ppsz_otype[] = {
523 "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
526 "Reserved", "Reserved",
528 "Main Synthetic", "Wavetables Synthesis", "General MIDI",
529 "Algorithmic Synthesis and Audio FX",
532 "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
533 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
535 "Reserved", "Reserved", "Escape",
536 "Layer 1", "Layer 2", "Layer 3",
540 const int i_pos_start = bs_pos( s );
545 memset( p_cfg, 0, sizeof(*p_cfg) );
548 p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
549 p_cfg->i_samplerate = Mpeg4ReadAudioSamplerate( s );
551 p_cfg->i_channel = bs_read( s, 4 );
552 if( p_cfg->i_channel == 7 )
553 p_cfg->i_channel = 8; // 7.1
554 else if( p_cfg->i_channel >= 8 )
555 p_cfg->i_channel = -1;
558 p_cfg->extension.i_object_type = 0;
559 p_cfg->extension.i_samplerate = 0;
560 if( p_cfg->i_object_type == 5 )
563 p_cfg->extension.i_object_type = p_cfg->i_object_type;
564 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
566 p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
569 switch( p_cfg->i_object_type )
571 case 1: case 2: case 3: case 4:
573 case 17: case 19: case 20: case 21: case 22: case 23:
574 Mpeg4GASpecificConfig( p_cfg, s );
577 // CelpSpecificConfig();
580 // HvxcSpecificConfig();
583 // TTSSSpecificConfig();
585 case 13: case 14: case 15: case 16:
586 // StructuredAudioSpecificConfig();
589 // ERCelpSpecificConfig();
592 // ERHvxcSpecificConfig();
595 // ParametricSpecificConfig();
598 // SSCSpecificConfig();
600 case 32: case 33: case 34:
601 // MPEG_1_2_SpecificConfig();
604 // DSTSpecificConfig();
610 switch( p_cfg->i_object_type )
612 case 17: case 19: case 20: case 21: case 22: case 23:
613 case 24: case 25: case 26: case 27:
615 int epConfig = bs_read( s, 2 );
616 if( epConfig == 2 || epConfig == 3 )
618 //ErrorProtectionSpecificConfig();
622 int directMapping = bs_read1( s );
634 if( p_cfg->extension.i_object_type != 5 && i_max_size > 0 && i_max_size - (bs_pos(s) - i_pos_start) >= 16 &&
635 bs_read( s, 11 ) == 0x2b7 )
637 p_cfg->extension.i_object_type = Mpeg4ReadAudioObjectType( s );
638 if( p_cfg->extension.i_object_type == 5 )
640 p_cfg->i_sbr = bs_read1( s );
641 if( p_cfg->i_sbr == 1 )
642 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
646 //fprintf( stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
647 // ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr );
649 i_bits = bs_pos(s) - i_pos_start;
651 *pi_extra = ( i_bits + 7 ) / 8;
652 for( i = 0; i < __MIN( LATM_MAX_EXTRA_SIZE, *pi_extra ); i++ )
654 const int i_read = __MIN( 8, i_bits - 8*i );
655 p_extra[i] = bs_read( &s_sav, i_read ) << (8-i_read);
660 static int LatmGetValue( bs_t *s )
662 int i_bytes = bs_read( s, 2 );
665 for( i = 0; i < i_bytes; i++ )
666 v = (v << 8) + bs_read( s, 8 );
671 static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
677 i_mux_version = bs_read( s, 1 );
680 i_mux_versionA = bs_read( s, 1 );
682 if( i_mux_versionA != 0 ) /* support only A=0 */
685 memset( m, 0, sizeof(*m) );
687 if( i_mux_versionA == 0 )
689 if( i_mux_version == 1 )
691 LatmGetValue(s); /* taraBufferFullness */
695 m->b_same_time_framing = bs_read1( s );
696 m->i_sub_frames = 1 + bs_read( s, 6 );
697 m->i_programs = 1 + bs_read( s, 4 );
699 for( i_program = 0; i_program < m->i_programs; i_program++ )
703 m->pi_layers[i_program] = 1+bs_read( s, 3 );
705 for( i_layer = 0; i_layer < m->pi_layers[i_program]; i_layer++ )
707 latm_stream_t *st = &m->stream[m->i_streams];
710 m->pi_stream[i_program][i_layer] = m->i_streams;
711 st->i_program = i_program;
712 st->i_layer = i_layer;
714 b_previous_cfg = false;
715 if( i_program != 0 || i_layer != 0 )
716 b_previous_cfg = bs_read1( s );
720 assert( m->i_streams > 0 );
721 st->cfg = m->stream[m->i_streams-1].cfg;
726 if( i_mux_version == 1 )
727 i_cfg_size = LatmGetValue(s);
728 i_cfg_size -= Mpeg4ReadAudioSpecificInfo( &st->cfg, &st->i_extra, st->extra, s, i_cfg_size );
730 bs_skip( s, i_cfg_size );
733 st->i_frame_length_type = bs_read( s, 3 );
734 switch( st->i_frame_length_type )
738 bs_skip( s, 8 ); /* latmBufferFullnes */
739 if( !m->b_same_time_framing )
741 if( st->cfg.i_object_type == 6 || st->cfg.i_object_type == 20 ||
742 st->cfg.i_object_type == 8 || st->cfg.i_object_type == 24 )
744 bs_skip( s, 6 ); /* eFrameOffset */
750 st->i_frame_length = bs_read( s, 9 );
752 case 3: case 4: case 5:
753 st->i_frame_length_index = bs_read( s, 6 ); // celp
756 st->i_frame_length_index = bs_read( s, 1 ); // hvxc
768 if( i_mux_version == 1 )
770 m->i_other_data = LatmGetValue( s );
776 b_continue = bs_read1(s);
777 m->i_other_data = (m->i_other_data << 8) + bs_read( s, 8 );
778 } while( b_continue );
785 m->i_crc = bs_read( s, 8 );
790 static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
792 decoder_sys_t *p_sys = p_dec->p_sys;
795 int i_accumulated = 0;
797 bs_init( &s, p_buffer, i_buffer );
799 /* Read the stream mux configuration if present */
800 if( !bs_read1( &s ) )
802 if( !LatmReadStreamMuxConfiguration( &p_sys->latm, &s ) &&
803 p_sys->latm.i_streams > 0 )
805 const latm_stream_t *st = &p_sys->latm.stream[0];
807 p_sys->i_channels = st->cfg.i_channel;
808 p_sys->i_rate = st->cfg.i_samplerate;
809 p_sys->i_frame_length = st->cfg.i_frame_length;
811 /* FIXME And if it changes ? */
812 if( !p_dec->fmt_out.i_extra && st->i_extra > 0 )
814 p_dec->fmt_out.i_extra = st->i_extra;
815 p_dec->fmt_out.p_extra = malloc( st->i_extra );
816 if( !p_dec->fmt_out.p_extra )
818 p_dec->fmt_out.i_extra = 0;
821 memcpy( p_dec->fmt_out.p_extra, st->extra, st->i_extra );
824 p_sys->b_latm_cfg = true;
827 /* Wait for the configuration */
828 if( !p_sys->b_latm_cfg )
831 /* FIXME do we need to split the subframe into independant packet ? */
832 if( p_sys->latm.i_sub_frames > 1 )
833 msg_Err( p_dec, "latm sub frames not yet supported, please send a sample" );
835 for( i_sub = 0; i_sub < p_sys->latm.i_sub_frames; i_sub++ )
837 int pi_payload[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
838 if( p_sys->latm.b_same_time_framing )
842 for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
845 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
847 latm_stream_t *st = &p_sys->latm.stream[p_sys->latm.pi_stream[i_program][i_layer]];
848 if( st->i_frame_length_type == 0 )
853 int i_tmp = bs_read( &s, 8 );
858 pi_payload[i_program][i_layer] = i_payload;
860 else if( st->i_frame_length_type == 1 )
862 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
864 else if( ( st->i_frame_length_type == 3 ) ||
865 ( st->i_frame_length_type == 5 ) ||
866 ( st->i_frame_length_type == 7 ) )
868 bs_skip( &s, 2 ); // muxSlotLengthCoded
869 pi_payload[i_program][i_layer] = 0; /* TODO */
873 pi_payload[i_program][i_layer] = 0; /* TODO */
878 for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
882 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
884 /* XXX we only extract 1 stream */
885 if( i_program != 0 || i_layer != 0 )
888 if( pi_payload[i_program][i_layer] <= 0 )
891 /* FIXME that's slow (and a bit ugly to write in place) */
892 for( i = 0; i < pi_payload[i_program][i_layer]; i++ )
893 p_buffer[i_accumulated++] = bs_read( &s, 8 );
899 const int i_chunks = bs_read( &s, 4 );
904 msg_Err( p_dec, "latm without same time frameing not yet supported, please send a sample" );
906 for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
908 const int streamIndex = bs_read( &s, 4 );
909 latm_stream_t *st = &p_sys->latm.stream[streamIndex];
910 const int i_program = st->i_program;
911 const int i_layer = st->i_layer;
913 pi_program[i_chunk] = i_program;
914 pi_layer[i_chunk] = i_layer;
916 if( st->i_frame_length_type == 0 )
921 int i_tmp = bs_read( &s, 8 );
926 pi_payload[i_program][i_layer] = i_payload;
927 bs_skip( &s, 1 ); // auEndFlag
929 else if( st->i_frame_length_type == 1 )
931 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
933 else if( ( st->i_frame_length_type == 3 ) ||
934 ( st->i_frame_length_type == 5 ) ||
935 ( st->i_frame_length_type == 7 ) )
937 bs_read( &s, 2 ); // muxSlotLengthCoded
943 for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
945 //const int i_program = pi_program[i_chunk];
946 //const int i_layer = pi_layer[i_chunk];
953 if( p_sys->latm.i_other_data > 0 )
955 /* Other data XXX we just ignore them */
959 return i_accumulated;
962 /****************************************************************************
963 * PacketizeStreamBlock: ADTS/LOAS packetizer
964 ****************************************************************************/
965 static void SetupOutput( decoder_t *p_dec, block_t *p_block );
966 static block_t *PacketizeStreamBlock( decoder_t *p_dec, block_t **pp_block )
968 decoder_sys_t *p_sys = p_dec->p_sys;
969 uint8_t p_header[ADTS_HEADER_SIZE + LOAS_HEADER_SIZE];
970 block_t *p_out_buffer;
973 if( !pp_block || !*pp_block ) return NULL;
975 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
977 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
979 p_sys->i_state = STATE_NOSYNC;
980 block_BytestreamFlush( &p_sys->bytestream );
982 //aout_DateSet( &p_sys->end_date, 0 );
983 block_Release( *pp_block );
987 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
989 /* We've just started the stream, wait for the first PTS. */
990 block_Release( *pp_block );
994 if( (*pp_block)->i_rate > 0 )
995 p_sys->i_input_rate = (*pp_block)->i_rate;
997 block_BytestreamPush( &p_sys->bytestream, *pp_block );
1001 switch( p_sys->i_state )
1005 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
1008 /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
1009 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
1011 if( p_sys->i_type != TYPE_ADTS )
1012 msg_Dbg( p_dec, "detected ADTS format" );
1014 p_sys->i_state = STATE_SYNC;
1015 p_sys->i_type = TYPE_ADTS;
1018 else if( p_header[0] == 0x56 && (p_header[1] & 0xe0) == 0xe0 )
1020 if( p_sys->i_type != TYPE_LOAS )
1021 msg_Dbg( p_dec, "detected LOAS format" );
1023 p_sys->i_state = STATE_SYNC;
1024 p_sys->i_type = TYPE_LOAS;
1027 block_SkipByte( &p_sys->bytestream );
1029 if( p_sys->i_state != STATE_SYNC )
1031 block_BytestreamFlush( &p_sys->bytestream );
1033 /* Need more data */
1038 /* New frame, set the Presentation Time Stamp */
1039 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
1040 if( p_sys->i_pts != 0 &&
1041 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
1043 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
1045 p_sys->i_state = STATE_HEADER;
1049 if( p_sys->i_type == TYPE_ADTS )
1051 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
1052 if( block_PeekBytes( &p_sys->bytestream, p_header,
1053 ADTS_HEADER_SIZE ) != VLC_SUCCESS )
1055 /* Need more data */
1059 /* Check if frame is valid and get frame info */
1060 p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
1063 &p_sys->i_frame_length,
1064 &p_sys->i_header_size );
1068 assert( p_sys->i_type == TYPE_LOAS );
1069 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
1070 if( block_PeekBytes( &p_sys->bytestream, p_header,
1071 LOAS_HEADER_SIZE ) != VLC_SUCCESS )
1073 /* Need more data */
1077 /* Check if frame is valid and get frame info */
1078 p_sys->i_frame_size = LOASSyncInfo( p_header, &p_sys->i_header_size );
1081 if( p_sys->i_frame_size <= 0 )
1083 msg_Dbg( p_dec, "emulated sync word" );
1084 block_SkipByte( &p_sys->bytestream );
1085 p_sys->i_state = STATE_NOSYNC;
1089 p_sys->i_state = STATE_NEXT_SYNC;
1091 case STATE_NEXT_SYNC:
1092 /* TODO: If p_block == NULL, flush the buffer without checking the
1094 if( p_sys->bytestream.p_block == NULL )
1096 p_sys->i_state = STATE_NOSYNC;
1097 block_BytestreamFlush( &p_sys->bytestream );
1101 /* Check if next expected frame contains the sync word */
1102 if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
1103 + p_sys->i_header_size, p_header, 2 )
1106 /* Need more data */
1110 assert( (p_sys->i_type == TYPE_ADTS) || (p_sys->i_type == TYPE_LOAS) );
1111 if( ( ( p_sys->i_type == TYPE_ADTS ) &&
1112 ( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 ) ) ||
1113 ( ( p_sys->i_type == TYPE_LOAS ) &&
1114 ( p_header[0] != 0x56 || (p_header[1] & 0xe0) != 0xe0 ) ) )
1116 msg_Dbg( p_dec, "emulated sync word "
1117 "(no sync on following frame)" );
1118 p_sys->i_state = STATE_NOSYNC;
1119 block_SkipByte( &p_sys->bytestream );
1123 p_sys->i_state = STATE_SEND_DATA;
1126 case STATE_GET_DATA:
1127 /* Make sure we have enough data.
1128 * (Not useful if we went through NEXT_SYNC) */
1129 if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
1130 p_sys->i_header_size) != VLC_SUCCESS )
1132 /* Need more data */
1135 p_sys->i_state = STATE_SEND_DATA;
1137 case STATE_SEND_DATA:
1138 /* When we reach this point we already know we have enough
1139 * data available. */
1141 p_out_buffer = block_New( p_dec, p_sys->i_frame_size );
1144 //p_dec->b_error = true;
1147 p_buf = p_out_buffer->p_buffer;
1149 /* Skip the ADTS/LOAS header */
1150 block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
1152 if( p_sys->i_type == TYPE_ADTS )
1154 /* Copy the whole frame into the buffer */
1155 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1159 assert( p_sys->i_type == TYPE_LOAS );
1160 /* Copy the whole frame into the buffer and parse/extract it */
1161 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1162 p_out_buffer->i_buffer = LOASParse( p_dec, p_buf, p_sys->i_frame_size );
1163 if( p_out_buffer->i_buffer <= 0 )
1165 if( !p_sys->b_latm_cfg )
1166 msg_Warn( p_dec, "waiting for header" );
1168 block_Release( p_out_buffer );
1169 p_out_buffer = NULL;
1170 p_sys->i_state = STATE_NOSYNC;
1174 SetupOutput( p_dec, p_out_buffer );
1175 /* Make sure we don't reuse the same pts twice */
1176 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
1177 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
1179 /* So p_block doesn't get re-added several times */
1180 *pp_block = block_BytestreamPop( &p_sys->bytestream );
1182 p_sys->i_state = STATE_NOSYNC;
1184 return p_out_buffer;
1191 /*****************************************************************************
1193 *****************************************************************************/
1194 static void SetupOutput( decoder_t *p_dec, block_t *p_block )
1196 decoder_sys_t *p_sys = p_dec->p_sys;
1198 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
1200 msg_Info( p_dec, "AAC channels: %d samplerate: %d",
1201 p_sys->i_channels, p_sys->i_rate );
1203 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
1204 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
1207 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
1208 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
1209 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
1210 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
1213 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
1214 p_dec->fmt_out.audio.i_physical_channels =
1215 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
1218 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
1220 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
1221 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
1225 /*****************************************************************************
1226 * ClosePacketizer: clean up the packetizer
1227 *****************************************************************************/
1228 static void ClosePacketizer( vlc_object_t *p_this )
1230 decoder_t *p_dec = (decoder_t *)p_this;
1231 decoder_sys_t *p_sys = p_dec->p_sys;
1233 block_BytestreamRelease( &p_sys->bytestream );
1235 free( p_dec->p_sys );