1 /*****************************************************************************
2 * demux.c : Raw aac Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: demux.c,v 1.2 2002/08/24 21:35:31 sigmunau Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc/input.h>
33 #include <sys/types.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Activate ( vlc_object_t * );
39 static int Demux ( input_thread_t * );
42 /*****************************************************************************
44 *****************************************************************************/
46 set_description( _("AAC stream demux" ) );
47 set_capability( "demux", 0 );
48 set_callbacks( Activate, NULL );
49 add_shortcut( "aac" );
52 /*****************************************************************************
53 * Definitions of structures and functions used by this plugins
54 *****************************************************************************/
56 /* XXX set this to 0 to avoid problem with PS XXX */
57 /* but with some file or web radio will failed to detect */
58 /* it's you to choose */
59 #define AAC_MAXTESTPOS 0
61 static int i_aac_samplerate[16] =
63 96000, 88200, 64000, 48000, 44100, 32000,
64 24000, 22050, 16000, 12000, 11025, 8000,
68 typedef struct adts_header_s
73 int i_protection_absent;
75 int i_samplerate_index;
77 int i_channel_configuration;
81 int i_copyright_identification_bit;
82 int i_copyright_identification_start;
83 int i_aac_frame_length;
84 int i_adts_buffer_fullness;
85 int i_no_raw_data_blocks_in_frame;
91 typedef struct adif_header_s
93 int b_copyright_id_present;
94 u8 i_copyright_id[10];
101 int i_num_program_config_elements;
102 int adif_buffer_fullness;
104 // program_config_element
113 es_descriptor_t *p_es;
116 adif_header_t adif_header;
119 adts_header_t adts_header;
121 /* extracted information */
122 int i_samplerate_index;
127 int i_aac_frame_length;
130 /****************************************************************************
131 * bit_* : function to get a bitstream from a memory buffer
132 ****************************************************************************
134 ****************************************************************************/
142 static void bit_init( bit_t *p_bit,
146 p_bit->p_buffer = p_buffer;
147 p_bit->i_buffer = i_buffer;
148 p_bit->i_mask = 0x80;
151 static u32 bit_get( bit_t *p_bit )
154 if( p_bit->i_buffer <= 0 )
158 i_bit = ( p_bit->p_buffer[0]&p_bit->i_mask ) ? 1 : 0;
165 p_bit->i_mask = 0x80;
170 static u32 bit_gets( bit_t *p_bit, int i_count )
174 for( ; i_count > 0; i_count-- )
176 i_bits = ( i_bits << 1 )|bit_get( p_bit );
181 /*****************************************************************************
182 * Function to manipulate stream easily
183 *****************************************************************************
185 * SkipBytes : skip bytes :) not yet uoptimised ( read bytes to be skipped )
187 * ReadPes : read data and make a PES
189 *****************************************************************************/
190 static int SkipBytes( input_thread_t *p_input, int i_size )
192 data_packet_t *p_data;
197 i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
202 input_DeletePacket( p_input->p_method_data, p_data );
208 static int ReadPES( input_thread_t *p_input,
209 pes_packet_t **pp_pes,
216 if( !(p_pes = input_NewPES( p_input->p_method_data )) )
218 msg_Err( p_input, "cannot allocate new PES" );
224 data_packet_t *p_data;
227 if( (i_read = input_SplitBuffer( p_input,
229 __MIN( i_size, 1024 ) ) ) <= 0 )
231 input_DeletePES( p_input->p_method_data, p_pes );
234 if( !p_pes->p_first )
236 p_pes->p_first = p_data;
237 p_pes->i_nb_data = 1;
238 p_pes->i_pes_size = i_read;
242 p_pes->p_last->p_next = p_data;
244 p_pes->i_pes_size += i_read;
246 p_pes->p_last = p_data;
253 /*****************************************************************************
254 * GetADIF: find and load adif header if present
255 *****************************************************************************/
256 static int GetADIF( input_thread_t *p_input,
257 adif_header_t *p_adif )
262 if( ( i_size = input_Peek( p_input, &p_peek, 60 ) ) < 60 )
264 /* it's false, min size is 11 byte but easier ;) */
267 if( ( p_peek[0] != 'A' )||( p_peek[1] != 'D' )||
268 ( p_peek[2] != 'I' )||( p_peek[3] != 'F' ) )
273 /* we now that we have an adif header */
276 return( 0 ); /* need some work */
279 /*****************************************************************************
280 * GetADTS: find and load adts header
281 *****************************************************************************/
282 #define ADTS_HEADERSIZE 10 /* 8+2 for crc */
283 static int GetADTS( input_thread_t *p_input,
284 adts_header_t *p_adts,
292 if( ( i_size = input_Peek( p_input, &p_peek, i_max_pos + ADTS_HEADERSIZE ) )
300 if( i_size < ADTS_HEADERSIZE )
304 if( ( p_peek[0] != 0xff )||
305 ( ( p_peek[1]&0xf6 ) != 0xf0 ) ) /* Layer == 0 */
312 /* we have find an adts header, load it */
316 bit_init( &bit, p_peek, i_size );
317 bit_gets( &bit, 12 ); /* synchro bits */
319 p_adts->i_id = bit_get( &bit );
320 p_adts->i_layer = bit_gets( &bit, 2);
321 p_adts->i_protection_absent = bit_get( &bit );
322 p_adts->i_profile = bit_gets( &bit, 2 );
323 p_adts->i_samplerate_index = bit_gets( &bit, 4);
324 p_adts->i_private_bit = bit_get( &bit );
325 p_adts->i_channel_configuration = bit_gets( &bit, 3);
326 p_adts->i_original = bit_get( &bit );
327 p_adts->i_home = bit_get( &bit );
328 if( p_adts->i_id == 0 )
330 p_adts->i_emphasis = bit_gets( &bit, 2);
333 p_adts->i_copyright_identification_bit = bit_get( &bit );
334 p_adts->i_copyright_identification_start = bit_get( &bit );
335 p_adts->i_aac_frame_length = bit_gets( &bit, 13 );
336 p_adts->i_adts_buffer_fullness = bit_gets( &bit, 11 );
337 p_adts->i_no_raw_data_blocks_in_frame = bit_gets( &bit, 2 );
339 if( p_adts->i_protection_absent == 0 )
341 p_adts->i_crc_check = bit_gets( &bit, 16 );
346 static void ExtractConfiguration( demux_sys_t *p_aac )
348 if( p_aac->b_adif_header )
352 if( p_aac->b_adts_header )
354 p_aac->i_samplerate_index = p_aac->adts_header.i_samplerate_index;
355 p_aac->i_object_type = p_aac->adts_header.i_profile;
356 p_aac->i_samplerate = i_aac_samplerate[p_aac->i_samplerate_index];
357 p_aac->i_channels = p_aac->adts_header.i_channel_configuration;
358 if( p_aac->i_channels > 6 )
360 /* I'm not sure of that, got from faad */
361 p_aac->i_channels = 2;
363 p_aac->i_aac_frame_length = p_aac->adts_header.i_aac_frame_length;
364 p_aac->i_framelength = 1024;
366 /* FIXME FIXME for LD, but LD = ?
367 if( p_aac->i_object_type == LD )
369 p_aac->i_framelength /= 2;
374 /****************************************************************************
375 * CheckPS : check if this stream could be some ps,
376 * yes it's ugly ... but another idea ?
378 * XXX it seems that aac stream always match ...
380 ****************************************************************************/
382 static int CheckPS( input_thread_t *p_input )
385 int i_size = input_Peek( p_input, &p_peek, 8196 );
389 if( ( p_peek[0] == 0 ) && ( p_peek[1] == 0 )&&
390 ( p_peek[2] == 1 ) && ( p_peek[3] >= 0xb9 ) )
392 return( 1 ); /* Perhaps some ps stream */
400 /*****************************************************************************
401 * Activate: initializes AAC demux structures
402 *****************************************************************************/
403 static int Activate( vlc_object_t * p_this )
405 input_thread_t * p_input = (input_thread_t *)p_this;
407 input_info_category_t * p_category;
413 /* Set the demux function */
414 p_input->pf_demux = Demux;
416 /* Initialize access plug-in structures. */
417 if( p_input->i_mtu == 0 )
420 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
423 b_forced = ( ( *p_input->psz_demux )&&
424 ( !strncmp( p_input->psz_demux, "aac", 10 ) ) ) ? 1 : 0;
426 /* check if it can be a ps stream */
427 if( !b_forced && CheckPS( p_input ) )
432 /* skip possible id3 header */
433 p_id3 = module_Need( p_input, "id3", NULL );
435 module_Unneed( p_input, p_id3 );
439 if( !( p_aac = malloc( sizeof( demux_sys_t ) ) ) )
441 msg_Err( p_input, "out of memory" );
444 memset( p_aac, 0, sizeof( demux_sys_t ) );
446 /* Now check for adif/adts header */
448 if( GetADIF( p_input, &p_aac->adif_header ) )
450 p_aac->b_adif_header = 1;
452 "found ADIF header (unsupported)" );
457 if( GetADTS( p_input,
462 p_aac->b_adts_header = 1;
464 "found ADTS header" );
469 "AAC module discarded (no header found)" );
473 ExtractConfiguration( p_aac );
475 vlc_mutex_lock( &p_input->stream.stream_lock );
476 if( input_InitStream( p_input, 0 ) == -1)
478 msg_Err( p_input, "cannot init stream" );
481 if( input_AddProgram( p_input, 0, 0) == NULL )
483 msg_Err( p_input, "cannot add program" );
486 p_input->stream.pp_programs[0]->b_is_ok = 0;
487 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
490 p_aac->p_es = input_AddES( p_input,
491 p_input->stream.p_selected_program,
496 vlc_mutex_unlock( &p_input->stream.stream_lock );
497 msg_Err( p_input, "out of memory" );
501 p_aac->p_es->i_stream_id = 1;
502 p_aac->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
503 p_aac->p_es->i_cat = AUDIO_ES;
504 input_SelectES( p_input, p_aac->p_es );
506 p_input->stream.p_selected_program->b_is_ok = 1;
507 vlc_mutex_unlock( &p_input->stream.stream_lock );
511 vlc_mutex_lock( &p_input->stream.stream_lock );
512 if( p_aac->b_adif_header )
514 p_input->stream.i_mux_rate = 0 / 50;
516 if( p_aac->b_adts_header )
518 p_input->stream.i_mux_rate = 0 / 50;
520 vlc_mutex_unlock( &p_input->stream.stream_lock );
522 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
523 /* if i don't do that, it don't work correctly but why ??? */
524 if( p_input->stream.b_seekable )
526 p_input->pf_seek( p_input, 0 );
527 input_AccessReinit( p_input );
529 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
533 if( p_aac->b_adif_header )
537 if( p_aac->b_adts_header )
540 "audio AAC MPEG-%d layer-%d %d channels %dHz",
541 p_aac->adts_header.i_id == 1 ? 2 : 4,
542 4 - p_aac->adts_header.i_layer, /* it's always 4 in fact */
544 p_aac->i_samplerate );
546 vlc_mutex_lock( &p_input->stream.stream_lock );
547 p_category = input_InfoCategory( p_input, "aac" );
549 input_AddInfo( p_category, "input type", "MPEG-%d AAC",
550 p_aac->adts_header.i_id == 1 ? 2 : 4 );
552 input_AddInfo( p_category, "layer", "%d",
553 4 - p_aac->adts_header.i_layer );
554 input_AddInfo( p_category, "channels", "%d",
556 input_AddInfo( p_category, "sample rate", "%dHz",
557 p_aac->i_samplerate );
559 vlc_mutex_unlock( &p_input->stream.stream_lock );
562 p_input->p_demux_data = p_aac;
568 /*****************************************************************************
569 * Demux: reads and demuxes data packets
570 *****************************************************************************
571 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
572 *****************************************************************************/
573 static int Demux( input_thread_t * p_input )
579 demux_sys_t *p_aac = p_input->p_demux_data;
582 if( p_aac->b_adif_header )
585 return( -1 ); /* FIXME */
588 if( p_aac->b_adts_header )
590 i_found = GetADTS( p_input,
599 ExtractConfiguration( p_aac );
601 /* skip garbage bytes */
607 SkipBytes( p_input, i_skip );
612 msg_Info( p_input, "can't find next frame" );
616 input_ClockManageRef( p_input,
617 p_input->stream.p_selected_program,
620 if( !ReadPES( p_input, &p_pes, p_aac->i_aac_frame_length ) )
623 "cannot read data" );
627 p_pes->i_pts = input_ClockGetTS( p_input,
628 p_input->stream.p_selected_program,
631 if( !p_aac->p_es->p_decoder_fifo )
633 msg_Err( p_input, "no audio decoder" );
634 input_DeletePES( p_input->p_method_data, p_pes );
635 return( -1 ); /* perhaps not, it's my choice */
639 input_DecodePES( p_aac->p_es->p_decoder_fifo, p_pes );
642 /* Update date information */
643 p_aac->i_pts += (mtime_t)90000 *
644 (mtime_t)p_aac->i_framelength /
645 (mtime_t)p_aac->i_samplerate;