contribs: update to fribidi2 beta (0.19.1) to hopefully solve problems with arabic...
[vlc.git] / modules / packetizer / mpeg4audio.c
blob6f238634aebfbb28a960b01978fa8c4e9becd277
1 /*****************************************************************************
2 * mpeg4audio.c: parse and packetize an MPEG 4 audio stream
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc/vlc.h>
34 #include <vlc_aout.h>
35 #include <vlc_codec.h>
36 #include <vlc_block.h>
37 #include <vlc_sout.h>
38 #include <vlc_codecs.h>
39 #include <vlc_input.h>
40 #include <vlc_bits.h>
42 #include "vlc_block_helper.h"
44 /* AAC Config in ES:
46 * AudioObjectType 5 bits
47 * samplingFrequencyIndex 4 bits
48 * if (samplingFrequencyIndex == 0xF)
49 * samplingFrequency 24 bits
50 * channelConfiguration 4 bits
51 * GA_SpecificConfig
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 *****************************************************************************/
60 typedef struct
62 int i_object_type;
63 int i_samplerate;
64 int i_channel;
65 int i_sbr; // 0: no sbr, 1: sbr, -1: unknown
67 struct
69 int i_object_type;
70 int i_samplerate;
71 } extension;
73 /* GASpecific */
74 int i_frame_length; // 1024 or 960
76 } mpeg4_cfg_t;
78 #define LATM_MAX_EXTRA_SIZE 64
79 typedef struct
81 int i_program;
82 int i_layer;
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
88 mpeg4_cfg_t cfg;
90 /* Raw configuration */
91 int i_extra;
92 uint8_t extra[LATM_MAX_EXTRA_SIZE];
94 } latm_stream_t;
96 #define LATM_MAX_LAYER (8)
97 #define LATM_MAX_PROGRAM (16)
98 typedef struct
100 int b_same_time_framing;
101 int i_sub_frames;
102 int i_programs;
104 int pi_layers[LATM_MAX_PROGRAM];
106 int pi_stream[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
108 int i_streams;
109 latm_stream_t stream[LATM_MAX_PROGRAM*LATM_MAX_LAYER];
111 int i_other_data;
112 int i_crc; /* -1 if not set */
113 } latm_mux_t;
115 struct decoder_sys_t
118 * Input properties
120 int i_state;
121 int i_type;
123 block_bytestream_t bytestream;
126 * Common properties
128 audio_date_t end_date;
129 mtime_t i_pts;
131 int i_frame_size;
132 unsigned int i_channels;
133 unsigned int i_rate, i_frame_length, i_header_size;
135 int i_input_rate;
137 /* LOAS */
138 vlc_bool_t b_latm_cfg;
139 latm_mux_t latm;
142 enum {
144 STATE_NOSYNC,
145 STATE_SYNC,
146 STATE_HEADER,
147 STATE_NEXT_SYNC,
148 STATE_GET_DATA,
149 STATE_SEND_DATA
152 enum {
153 TYPE_NONE,
154 TYPE_RAW,
155 TYPE_ADTS,
156 TYPE_LOAS
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 /****************************************************************************
169 * Local prototypes
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 /*****************************************************************************
178 * Module descriptor
179 *****************************************************************************/
180 vlc_module_begin();
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 );
186 vlc_module_end();
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' ) )
198 return VLC_EGENERIC;
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" );
206 return VLC_EGENERIC;
209 /* Misc init */
210 p_sys->i_state = STATE_NOSYNC;
211 aout_DateSet( &p_sys->end_date, 0 );
212 p_sys->bytestream = block_BytestreamInit( p_dec );
213 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
214 p_sys->b_latm_cfg = VLC_FALSE;
216 /* Set output properties */
217 p_dec->fmt_out.i_cat = AUDIO_ES;
218 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','4','a');
220 msg_Dbg( p_dec, "running MPEG4 audio packetizer" );
222 if( p_dec->fmt_in.i_extra > 0 )
224 uint8_t *p_config = (uint8_t*)p_dec->fmt_in.p_extra;
225 int i_index;
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;
234 else
236 p_dec->fmt_out.audio.i_rate = ( ( p_config[1] & 0x7f ) << 17 ) |
237 ( p_config[2] << 9 ) | ( p_config[3] << 1 ) |
238 ( p_config[4] >> 7 );
239 p_dec->fmt_out.audio.i_frame_length =
240 (( p_config[4] >> 2 ) & 0x01) ? 960 : 1024;
243 msg_Dbg( p_dec, "AAC %dHz %d samples/frame",
244 p_dec->fmt_out.audio.i_rate,
245 p_dec->fmt_out.audio.i_frame_length );
247 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
249 p_dec->fmt_out.audio.i_channels = p_dec->fmt_in.audio.i_channels;
250 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
251 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
252 memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
253 p_dec->fmt_in.i_extra );
255 /* Set callback */
256 p_dec->pf_packetize = PacketizeRawBlock;
257 p_sys->i_type = TYPE_RAW;
259 else
261 msg_Dbg( p_dec, "no decoder specific info, must be an ADTS or LOAS stream" );
263 aout_DateInit( &p_sys->end_date, p_dec->fmt_in.audio.i_rate );
265 /* We will try to create a AAC Config from adts/loas */
266 p_dec->fmt_out.i_extra = 0;
267 p_dec->fmt_out.p_extra = NULL;
269 /* Set callback */
270 p_dec->pf_packetize = PacketizeStreamBlock;
271 p_sys->i_type = TYPE_NONE;
274 return VLC_SUCCESS;
277 /****************************************************************************
278 * PacketizeRawBlock: the whole thing
279 ****************************************************************************
280 * This function must be fed with complete frames.
281 ****************************************************************************/
282 static block_t *PacketizeRawBlock( decoder_t *p_dec, block_t **pp_block )
284 decoder_sys_t *p_sys = p_dec->p_sys;
285 block_t *p_block;
287 if( !pp_block || !*pp_block ) return NULL;
289 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
291 //aout_DateSet( &p_sys->end_date, 0 );
292 block_Release( *pp_block );
293 return NULL;
296 p_block = *pp_block;
297 *pp_block = NULL; /* Don't reuse this block */
299 if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
301 /* We've just started the stream, wait for the first PTS. */
302 block_Release( p_block );
303 return NULL;
305 else if( p_block->i_pts != 0 &&
306 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
308 aout_DateSet( &p_sys->end_date, p_block->i_pts );
311 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
313 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
314 p_dec->fmt_out.audio.i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) - p_block->i_pts;
316 return p_block;
319 /****************************************************************************
320 * ADTS helpers
321 ****************************************************************************/
322 static int ADTSSyncInfo( decoder_t * p_dec, const byte_t * p_buf,
323 unsigned int * pi_channels,
324 unsigned int * pi_sample_rate,
325 unsigned int * pi_frame_length,
326 unsigned int * pi_header_size )
328 int i_id, i_profile, i_sample_rate_idx, i_frame_size;
329 vlc_bool_t b_crc;
331 /* Fixed header between frames */
332 i_id = ( (p_buf[1] >> 3) & 0x01 ) ? 2 : 4;
333 b_crc = !(p_buf[1] & 0x01);
334 i_profile = p_buf[2] >> 6;
335 i_sample_rate_idx = (p_buf[2] >> 2) & 0x0f;
336 *pi_sample_rate = pi_sample_rates[i_sample_rate_idx];
337 *pi_channels = ((p_buf[2] & 0x01) << 2) | ((p_buf[3] >> 6) & 0x03);
339 /* Variable header */
340 i_frame_size = ((p_buf[3] & 0x03) << 11) | (p_buf[4] << 3) |
341 ((p_buf[5] >> 5) & 0x7);
342 //i_raw_blocks_in_frame = (p_buf[6] & 0x02) + 1;
344 if( !*pi_sample_rate || !*pi_channels || !i_frame_size )
346 return 0;
349 /* Fixme */
350 *pi_frame_length = 1024;
352 /* Build the decoder specific info header */
353 if( !p_dec->fmt_out.i_extra )
355 p_dec->fmt_out.i_extra = 2;
356 p_dec->fmt_out.p_extra = malloc( 2 );
357 ((uint8_t *)p_dec->fmt_out.p_extra)[0] =
358 (i_profile + 1) << 3 | (i_sample_rate_idx >> 1);
359 ((uint8_t *)p_dec->fmt_out.p_extra)[1] =
360 ((i_sample_rate_idx & 0x01) << 7) | (*pi_channels <<3);
363 /* ADTS header length */
364 *pi_header_size = b_crc ? 9 : 7;
366 return i_frame_size - *pi_header_size;
368 /****************************************************************************
369 * LOAS helpers
370 ****************************************************************************/
371 static int LOASSyncInfo( decoder_t *p_dec, uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_header_size )
373 *pi_header_size = 3;
374 return ( ( p_header[1] & 0x1f ) << 8 ) + p_header[2];
376 static int Mpeg4GAProgramConfigElement( bs_t *s )
378 /* TODO compute channels count ? */
379 int i_tag = bs_read( s, 4 );
380 if( i_tag != 0x05 )
381 return -1;
382 bs_skip( s, 2 + 4 ); // object type + sampling index
383 int i_num_front = bs_read( s, 4 );
384 int i_num_side = bs_read( s, 4 );
385 int i_num_back = bs_read( s, 4 );
386 int i_num_lfe = bs_read( s, 2 );
387 int i_num_assoc_data = bs_read( s, 3 );
388 int i_num_valid_cc = bs_read( s, 4 );
390 if( bs_read1(s) )
391 bs_skip( s, 4 ); // mono downmix
392 if( bs_read1(s) )
393 bs_skip( s, 4 ); // stereo downmix
394 if( bs_read1(s) )
395 bs_skip( s, 2+1 ); // matrix downmix + pseudo_surround
397 bs_skip( s, i_num_front * (1+4) );
398 bs_skip( s, i_num_side * (1+4) );
399 bs_skip( s, i_num_back * (1+4) );
400 bs_skip( s, i_num_lfe * (4) );
401 bs_skip( s, i_num_assoc_data * (4) );
402 bs_skip( s, i_num_valid_cc * (5) );
403 bs_align( s );
404 int i_comment = bs_read( s, 8 );
405 bs_skip( s, i_comment * 8 );
406 return 0;
408 static int Mpeg4GASpecificConfig( mpeg4_cfg_t *p_cfg, bs_t *s )
410 p_cfg->i_frame_length = bs_read1(s) ? 960 : 1024;
412 if( bs_read1( s ) ) // depend on core coder
413 bs_skip( s, 14 ); // core coder delay
415 int i_extension_flag = bs_read1( s );
416 if( p_cfg->i_channel == 0 )
418 Mpeg4GAProgramConfigElement( s );
420 if( p_cfg->i_object_type == 6 || p_cfg->i_object_type == 20 )
421 bs_skip( s, 3 ); // layer
423 if( i_extension_flag )
425 if( p_cfg->i_object_type == 22 )
427 bs_skip( s, 5 + 11 ); // numOfSubFrame + layer length
429 if( p_cfg->i_object_type == 17 || p_cfg->i_object_type == 19 ||
430 p_cfg->i_object_type == 20 || p_cfg->i_object_type == 23 )
432 bs_skip( s, 1+1+1 ); // ER data : section scale spectral */
434 if( bs_read1( s ) ) // extension 3
435 fprintf( stderr, "Mpeg4GASpecificConfig: error 1\n" );
437 return 0;
439 static int Mpeg4ReadAudioObjectType( bs_t *s )
441 int i_type = bs_read( s, 5 );
442 if( i_type == 0x1f )
443 i_type += bs_read( s, 6 );
444 return i_type;
446 static int Mpeg4ReadAudioSamplerate( bs_t *s )
448 int i_index = bs_read( s, 4 );
449 if( i_index != 0x0f )
450 return pi_sample_rates[i_index];
451 return bs_read( s, 24 );
453 static int Mpeg4ReadAudioSpecificInfo( mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_t *p_extra, bs_t *s, int i_max_size )
455 #if 0
456 static const char *ppsz_otype[] = {
457 "NULL",
458 "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
459 "TwinVQ",
460 "CELP", "HVXC",
461 "Reserved", "Reserved",
462 "TTSI",
463 "Main Synthetic", "Wavetables Synthesis", "General MIDI",
464 "Algorithmic Synthesis and Audio FX",
465 "ER AAC LC",
466 "Reserved",
467 "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
468 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
469 "SSC",
470 "Reserved", "Reserved", "Escape",
471 "Layer 1", "Layer 2", "Layer 3",
472 "DST",
474 #endif
475 const int i_pos_start = bs_pos( s );
476 bs_t s_sav = *s;
477 int i_bits;
478 int i;
480 memset( p_cfg, 0, sizeof(*p_cfg) );
481 *pi_extra = 0;
483 p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
484 p_cfg->i_samplerate = Mpeg4ReadAudioSamplerate( s );
486 p_cfg->i_channel = bs_read( s, 4 );
487 if( p_cfg->i_channel == 7 )
488 p_cfg->i_channel = 8; // 7.1
489 else if( p_cfg->i_channel >= 8 )
490 p_cfg->i_channel = -1;
492 p_cfg->i_sbr = -1;
493 p_cfg->extension.i_object_type = 0;
494 p_cfg->extension.i_samplerate = 0;
495 if( p_cfg->i_object_type == 5 )
497 p_cfg->i_sbr = 1;
498 p_cfg->extension.i_object_type = p_cfg->i_object_type;
499 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
501 p_cfg->i_object_type = Mpeg4ReadAudioObjectType( s );
504 switch( p_cfg->i_object_type )
506 case 1: case 2: case 3: case 4:
507 case 6: case 7:
508 case 17: case 19: case 20: case 21: case 22: case 23:
509 Mpeg4GASpecificConfig( p_cfg, s );
510 break;
511 case 8:
512 // CelpSpecificConfig();
513 break;
514 case 9:
515 // HvxcSpecificConfig();
516 break;
517 case 12:
518 // TTSSSpecificConfig();
519 break;
520 case 13: case 14: case 15: case 16:
521 // StructuredAudioSpecificConfig();
522 break;
523 case 24:
524 // ERCelpSpecificConfig();
525 break;
526 case 25:
527 // ERHvxcSpecificConfig();
528 break;
529 case 26: case 27:
530 // ParametricSpecificConfig();
531 break;
532 case 28:
533 // SSCSpecificConfig();
534 break;
535 case 32: case 33: case 34:
536 // MPEG_1_2_SpecificConfig();
537 break;
538 case 35:
539 // DSTSpecificConfig();
540 break;
541 default:
542 // error
543 break;
545 switch( p_cfg->i_object_type )
547 case 17: case 19: case 20: case 21: case 22: case 23:
548 case 24: case 25: case 26: case 27:
550 int epConfig = bs_read( s, 2 );
551 if( epConfig == 2 || epConfig == 3 )
553 //ErrorProtectionSpecificConfig();
555 if( epConfig == 3 )
557 int directMapping = bs_read1( s );
558 if( directMapping )
560 // tbd ...
563 break;
565 default:
566 break;
568 if( p_cfg->extension.i_object_type != 5 && i_max_size > 0 && i_max_size - (bs_pos(s) - i_pos_start) >= 16 &&
569 bs_read( s, 11 ) == 0x2b7 )
571 p_cfg->extension.i_object_type = Mpeg4ReadAudioObjectType( s );
572 if( p_cfg->extension.i_object_type == 5 )
574 p_cfg->i_sbr = bs_read1( s );
575 if( p_cfg->i_sbr == 1 )
576 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate( s );
580 //fprintf( stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
581 // ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr );
583 i_bits = bs_pos(s) - i_pos_start;
585 *pi_extra = ( i_bits + 7 ) / 8;
586 for( i = 0; i < __MIN( LATM_MAX_EXTRA_SIZE, *pi_extra ); i++ )
588 const int i_read = __MIN( 8, i_bits - 8*i );
589 p_extra[i] = bs_read( &s_sav, i_read ) << (8-i_read);
591 return i_bits;
594 static int LatmGetValue( bs_t *s )
596 int i_bytes = bs_read( s, 2 );
597 int v = 0;
598 int i;
599 for( i = 0; i < i_bytes; i++ )
600 v = (v << 8) + bs_read( s, 8 );
602 return v;
605 static int LatmReadStreamMuxConfiguration( latm_mux_t *m, bs_t *s )
607 int i_mux_version;
608 int i_mux_versionA;
609 int i_program;
611 i_mux_version = bs_read( s, 1 );
612 i_mux_versionA = 0;
613 if( i_mux_version )
614 i_mux_versionA = bs_read( s, 1 );
616 if( i_mux_versionA != 0 ) /* support only A=0 */
617 return -1;
619 memset( m, 0, sizeof(*m) );
621 if( i_mux_versionA == 0 )
623 if( i_mux_version == 1 )
625 LatmGetValue(s); /* taraBufferFullness */
629 m->b_same_time_framing = bs_read1( s );
630 m->i_sub_frames = 1 + bs_read( s, 6 );
631 m->i_programs = 1 + bs_read( s, 4 );
633 for( i_program = 0; i_program < m->i_programs; i_program++ )
635 int i_layer;
637 m->pi_layers[i_program] = 1+bs_read( s, 3 );
639 for( i_layer = 0; i_layer < m->pi_layers[i_program]; i_layer++ )
641 latm_stream_t *st = &m->stream[m->i_streams];
642 vlc_bool_t b_previous_cfg;
644 m->pi_stream[i_program][i_layer] = m->i_streams;
645 st->i_program = i_program;
646 st->i_layer = i_layer;
648 b_previous_cfg = VLC_FALSE;
649 if( i_program != 0 || i_layer != 0 )
650 b_previous_cfg = bs_read1( s );
652 if( b_previous_cfg )
654 assert( m->i_streams > 0 );
655 st->cfg = m->stream[m->i_streams-1].cfg;
657 else
659 int i_cfg_size = 0;
660 if( i_mux_version == 1 )
661 i_cfg_size = LatmGetValue(s);
662 i_cfg_size -= Mpeg4ReadAudioSpecificInfo( &st->cfg, &st->i_extra, st->extra, s, i_cfg_size );
663 if( i_cfg_size > 0 )
664 bs_skip( s, i_cfg_size );
667 st->i_frame_length_type = bs_read( s, 3 );
668 switch( st->i_frame_length_type )
670 case 0:
672 bs_skip( s, 8 ); /* latmBufferFullnes */
673 if( !m->b_same_time_framing )
675 if( st->cfg.i_object_type == 6 || st->cfg.i_object_type == 20 ||
676 st->cfg.i_object_type == 8 || st->cfg.i_object_type == 24 )
678 bs_skip( s, 6 ); /* eFrameOffset */
681 break;
683 case 1:
684 st->i_frame_length = bs_read( s, 9 );
685 break;
686 case 3: case 4: case 5:
687 st->i_frame_length_index = bs_read( s, 6 ); // celp
688 break;
689 case 6: case 7:
690 st->i_frame_length_index = bs_read( s, 1 ); // hvxc
691 default:
692 break;
694 /* Next stream */
695 m->i_streams++;
699 /* other data */
700 if( bs_read1( s ) )
702 if( i_mux_version == 1 )
704 m->i_other_data = LatmGetValue( s );
706 else
708 int b_continue;
709 do {
710 b_continue = bs_read1(s);
711 m->i_other_data = (m->i_other_data << 8) + bs_read( s, 8 );
712 } while( b_continue );
716 /* crc */
717 m->i_crc = -1;
718 if( bs_read1( s ) )
719 m->i_crc = bs_read( s, 8 );
721 return 0;
724 static int LOASParse( decoder_t *p_dec, uint8_t *p_buffer, int i_buffer )
726 decoder_sys_t *p_sys = p_dec->p_sys;
727 bs_t s;
728 int i_sub;
729 int i_accumulated = 0;
731 bs_init( &s, p_buffer, i_buffer );
733 /* Read the stream mux configuration if present */
734 if( !bs_read1( &s ) )
736 if( !LatmReadStreamMuxConfiguration( &p_sys->latm, &s ) && p_sys->latm.i_streams > 0 )
738 const latm_stream_t *st = &p_sys->latm.stream[0];
740 p_sys->i_channels = st->cfg.i_channel;
741 p_sys->i_rate = st->cfg.i_samplerate;
742 p_sys->i_frame_length = st->cfg.i_frame_length;
744 /* FIXME And if it changes ? */
745 if( !p_dec->fmt_out.i_extra && st->i_extra > 0 )
747 p_dec->fmt_out.i_extra = st->i_extra;
748 p_dec->fmt_out.p_extra = malloc( st->i_extra );
749 memcpy( p_dec->fmt_out.p_extra, st->extra, st->i_extra );
752 p_sys->b_latm_cfg = VLC_TRUE;
755 /* Wait for the configuration */
756 if( !p_sys->b_latm_cfg )
757 return 0;
759 /* FIXME do we need to split the subframe into independant packet ? */
760 if( p_sys->latm.i_sub_frames > 1 )
761 msg_Err( p_dec, "latm sub frames not yet supported, please send a sample" );
763 for( i_sub = 0; i_sub < p_sys->latm.i_sub_frames; i_sub++ )
765 int pi_payload[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
766 if( p_sys->latm.b_same_time_framing )
768 int i_program;
769 /* Payload length */
770 for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
772 int i_layer;
773 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
775 latm_stream_t *st = &p_sys->latm.stream[p_sys->latm.pi_stream[i_program][i_layer]];
776 if( st->i_frame_length_type == 0 )
778 int i_payload = 0;
779 for( ;; )
781 int i_tmp = bs_read( &s, 8 );
782 i_payload += i_tmp;
783 if( i_tmp != 255 )
784 break;
786 pi_payload[i_program][i_layer] = i_payload;
788 else if( st->i_frame_length_type == 1 )
790 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
792 else if( st->i_frame_length_type == 3 || st->i_frame_length_type == 5 || st->i_frame_length_type == 7 )
794 bs_skip( &s, 2 ); // muxSlotLengthCoded
795 pi_payload[i_program][i_layer] = 0; /* TODO */
797 else
799 pi_payload[i_program][i_layer] = 0; /* TODO */
803 /* Payload Data */
804 for( i_program = 0; i_program < p_sys->latm.i_programs; i_program++ )
806 int i_layer;
807 int i;
808 for( i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++ )
810 /* XXX we only extract 1 stream */
811 if( i_program != 0 || i_layer != 0 )
812 break;
814 if( pi_payload[i_program][i_layer] <= 0 )
815 continue;
817 /* FIXME that's slow (and a bit ugly to write in place) */
818 for( i = 0; i < pi_payload[i_program][i_layer]; i++ )
819 p_buffer[i_accumulated++] = bs_read( &s, 8 );
823 else
825 const int i_chunks = bs_read( &s, 4 );
826 int pi_program[16];
827 int pi_layer[16];
828 int i_chunk;
830 msg_Err( p_dec, "latm without same time frameing not yet supported, please send a sample" );
832 for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
834 const int streamIndex = bs_read( &s, 4 );
835 latm_stream_t *st = &p_sys->latm.stream[streamIndex];
836 const int i_program = st->i_program;
837 const int i_layer = st->i_layer;
839 pi_program[i_chunk] = i_program;
840 pi_layer[i_chunk] = i_layer;
842 if( st->i_frame_length_type == 0 )
844 int i_payload = 0;
845 for( ;; )
847 int i_tmp = bs_read( &s, 8 );
848 i_payload += i_tmp;
849 if( i_tmp != 255 )
850 break;
852 pi_payload[i_program][i_layer] = i_payload;
853 bs_skip( &s, 1 ); // auEndFlag
855 else if( st->i_frame_length_type == 1 )
857 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
859 else if( st->i_frame_length_type == 3 || st->i_frame_length_type == 5 || st->i_frame_length_type == 7 )
861 bs_read( &s, 2 ); // muxSlotLengthCoded
863 else
867 for( i_chunk = 0; i_chunk < i_chunks; i_chunk++ )
869 //const int i_program = pi_program[i_chunk];
870 //const int i_layer = pi_layer[i_chunk];
872 /* TODO ? Payload */
877 if( p_sys->latm.i_other_data > 0 )
879 /* Other data XXX we just ignore them */
881 bs_align( &s );
883 return i_accumulated;
886 /****************************************************************************
887 * PacketizeStreamBlock: ADTS/LOAS packetizer
888 ****************************************************************************/
889 static void SetupOutput( decoder_t *p_dec, block_t *p_block );
890 static block_t *PacketizeStreamBlock( decoder_t *p_dec, block_t **pp_block )
892 decoder_sys_t *p_sys = p_dec->p_sys;
893 uint8_t p_header[ADTS_HEADER_SIZE + LOAS_HEADER_SIZE];
894 block_t *p_out_buffer;
895 uint8_t *p_buf;
897 if( !pp_block || !*pp_block ) return NULL;
899 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
901 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
903 p_sys->i_state = STATE_NOSYNC;
904 block_BytestreamFlush( &p_sys->bytestream );
906 //aout_DateSet( &p_sys->end_date, 0 );
907 block_Release( *pp_block );
908 return NULL;
911 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
913 /* We've just started the stream, wait for the first PTS. */
914 block_Release( *pp_block );
915 return NULL;
918 if( (*pp_block)->i_rate > 0 )
919 p_sys->i_input_rate = (*pp_block)->i_rate;
921 block_BytestreamPush( &p_sys->bytestream, *pp_block );
923 for( ;; )
925 switch( p_sys->i_state )
928 case STATE_NOSYNC:
929 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
930 == VLC_SUCCESS )
932 /* Look for sync word - should be 0xfff(adts) or 0x2b7(loas) */
933 if( p_header[0] == 0xff && (p_header[1] & 0xf6) == 0xf0 )
935 if( p_sys->i_type != TYPE_ADTS )
936 msg_Dbg( p_dec, "detected ADTS format" );
938 p_sys->i_state = STATE_SYNC;
939 p_sys->i_type = TYPE_ADTS;
940 break;
942 else if( p_header[0] == 0x56 && (p_header[1] & 0xe0) == 0xe0 )
944 if( p_sys->i_type != TYPE_LOAS )
945 msg_Dbg( p_dec, "detected LOAS format" );
947 p_sys->i_state = STATE_SYNC;
948 p_sys->i_type = TYPE_LOAS;
949 break;
951 block_SkipByte( &p_sys->bytestream );
953 if( p_sys->i_state != STATE_SYNC )
955 block_BytestreamFlush( &p_sys->bytestream );
957 /* Need more data */
958 return NULL;
961 case STATE_SYNC:
962 /* New frame, set the Presentation Time Stamp */
963 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
964 if( p_sys->i_pts != 0 &&
965 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
967 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
969 p_sys->i_state = STATE_HEADER;
970 break;
972 case STATE_HEADER:
973 if( p_sys->i_type == TYPE_ADTS )
975 /* Get ADTS frame header (ADTS_HEADER_SIZE bytes) */
976 if( block_PeekBytes( &p_sys->bytestream, p_header,
977 ADTS_HEADER_SIZE ) != VLC_SUCCESS )
979 /* Need more data */
980 return NULL;
983 /* Check if frame is valid and get frame info */
984 p_sys->i_frame_size = ADTSSyncInfo( p_dec, p_header,
985 &p_sys->i_channels,
986 &p_sys->i_rate,
987 &p_sys->i_frame_length,
988 &p_sys->i_header_size );
990 else
992 assert( p_sys->i_type == TYPE_LOAS );
993 /* Get LOAS frame header (LOAS_HEADER_SIZE bytes) */
994 if( block_PeekBytes( &p_sys->bytestream, p_header,
995 LOAS_HEADER_SIZE ) != VLC_SUCCESS )
997 /* Need more data */
998 return NULL;
1001 /* Check if frame is valid and get frame info */
1002 p_sys->i_frame_size = LOASSyncInfo( p_dec, p_header, &p_sys->i_header_size );
1005 if( p_sys->i_frame_size <= 0 )
1007 msg_Dbg( p_dec, "emulated sync word" );
1008 block_SkipByte( &p_sys->bytestream );
1009 p_sys->i_state = STATE_NOSYNC;
1010 break;
1013 p_sys->i_state = STATE_NEXT_SYNC;
1015 case STATE_NEXT_SYNC:
1016 /* TODO: If p_block == NULL, flush the buffer without checking the
1017 * next sync word */
1019 /* Check if next expected frame contains the sync word */
1020 if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->i_frame_size
1021 + p_sys->i_header_size, p_header, 2 )
1022 != VLC_SUCCESS )
1024 /* Need more data */
1025 return NULL;
1028 assert( p_sys->i_type == TYPE_ADTS || p_sys->i_type == TYPE_LOAS );
1029 if( ( p_sys->i_type == TYPE_ADTS && ( p_header[0] != 0xff || (p_header[1] & 0xf6) != 0xf0 ) ) ||
1030 ( p_sys->i_type == TYPE_LOAS && ( p_header[0] != 0x56 || (p_header[1] & 0xe0) != 0xe0 ) ) )
1032 msg_Dbg( p_dec, "emulated sync word "
1033 "(no sync on following frame)" );
1034 p_sys->i_state = STATE_NOSYNC;
1035 block_SkipByte( &p_sys->bytestream );
1036 break;
1039 p_sys->i_state = STATE_SEND_DATA;
1040 break;
1042 case STATE_GET_DATA:
1043 /* Make sure we have enough data.
1044 * (Not useful if we went through NEXT_SYNC) */
1045 if( block_WaitBytes( &p_sys->bytestream, p_sys->i_frame_size +
1046 p_sys->i_header_size) != VLC_SUCCESS )
1048 /* Need more data */
1049 return NULL;
1051 p_sys->i_state = STATE_SEND_DATA;
1053 case STATE_SEND_DATA:
1054 /* When we reach this point we already know we have enough
1055 * data available. */
1057 p_out_buffer = block_New( p_dec, p_sys->i_frame_size );
1058 if( !p_out_buffer )
1060 //p_dec->b_error = VLC_TRUE;
1061 return NULL;
1063 p_buf = p_out_buffer->p_buffer;
1065 /* Skip the ADTS/LOAS header */
1066 block_SkipBytes( &p_sys->bytestream, p_sys->i_header_size );
1068 if( p_sys->i_type == TYPE_ADTS )
1070 /* Copy the whole frame into the buffer */
1071 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1073 else
1075 assert( p_sys->i_type == TYPE_LOAS );
1076 /* Copy the whole frame into the buffer and parse/extract it */
1077 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
1078 p_out_buffer->i_buffer = LOASParse( p_dec, p_buf, p_sys->i_frame_size );
1079 if( p_out_buffer->i_buffer <= 0 )
1081 if( !p_sys->b_latm_cfg )
1082 msg_Warn( p_dec, "waiting for header" );
1084 block_Release( p_out_buffer );
1085 p_out_buffer = NULL;
1086 p_sys->i_state = STATE_NOSYNC;
1087 break;
1090 SetupOutput( p_dec, p_out_buffer );
1091 /* Make sure we don't reuse the same pts twice */
1092 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
1093 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
1095 /* So p_block doesn't get re-added several times */
1096 *pp_block = block_BytestreamPop( &p_sys->bytestream );
1098 p_sys->i_state = STATE_NOSYNC;
1100 return p_out_buffer;
1104 return NULL;
1108 /*****************************************************************************
1109 * SetupBuffer:
1110 *****************************************************************************/
1111 static void SetupOutput( decoder_t *p_dec, block_t *p_block )
1113 decoder_sys_t *p_sys = p_dec->p_sys;
1115 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
1117 msg_Info( p_dec, "AAC channels: %d samplerate: %d",
1118 p_sys->i_channels, p_sys->i_rate );
1120 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
1121 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
1124 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
1125 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
1126 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
1127 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
1129 #if 0
1130 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
1131 p_dec->fmt_out.audio.i_physical_channels =
1132 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
1133 #endif
1135 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
1137 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
1138 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
1139 p_block->i_pts;
1142 /*****************************************************************************
1143 * ClosePacketizer: clean up the packetizer
1144 *****************************************************************************/
1145 static void ClosePacketizer( vlc_object_t *p_this )
1147 decoder_t *p_dec = (decoder_t *)p_this;
1148 decoder_sys_t *p_sys = p_dec->p_sys;
1150 block_BytestreamRelease( &p_sys->bytestream );
1152 free( p_dec->p_sys );