1 /*****************************************************************************
2 * demux.c : Raw aac Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: demux.c,v 1.10 2003/05/14 21:29:42 fenrir 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 * );
40 static void Deactivate ( vlc_object_t * );
42 /*****************************************************************************
44 *****************************************************************************/
46 set_description( _("AAC stream demuxer" ) );
47 set_capability( "demux", 10 );
48 set_callbacks( Activate, NULL );
49 add_shortcut( "aac" );
52 /*****************************************************************************
53 * Definitions of structures and functions used by this plugins
54 *****************************************************************************/
55 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
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 uint8_t 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 uint32_t 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 uint32_t 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;
375 /****************************************************************************
376 * CheckPS : check if this stream could be some ps,
377 * yes it's ugly ... but another idea ?
378 * XXX it seems that aac stream always match ...
379 ****************************************************************************/
380 static int CheckPS( input_thread_t *p_input )
383 int i_size = input_Peek( p_input, &p_peek, 8196 );
387 if( ( p_peek[0] == 0 ) && ( p_peek[1] == 0 )&&
388 ( p_peek[2] == 1 ) && ( p_peek[3] >= 0xb9 ) )
390 return( 1 ); /* Perhaps some ps stream */
399 /*****************************************************************************
400 * Activate: initializes AAC demux structures
401 *****************************************************************************/
402 static int Activate( vlc_object_t * p_this )
404 input_thread_t * p_input = (input_thread_t *)p_this;
406 input_info_category_t * p_category;
410 int b_forced = VLC_FALSE;
412 if( p_input->psz_demux && !strncmp( p_input->psz_demux, "aac", 3 ) )
416 else if( p_input->psz_name )
418 int i_len = strlen( p_input->psz_name );
420 if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".aac" ) )
431 /* Set the demux function */
432 p_input->pf_demux = Demux;
434 /* Initialize access plug-in structures. */
435 if( p_input->i_mtu == 0 )
438 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
441 /* skip possible id3 header */
442 p_id3 = module_Need( p_input, "id3", NULL );
444 module_Unneed( p_input, p_id3 );
448 if( !( p_aac = malloc( sizeof( demux_sys_t ) ) ) )
450 msg_Err( p_input, "out of memory" );
453 memset( p_aac, 0, sizeof( demux_sys_t ) );
455 /* Now check for adif/adts header */
457 if( GetADIF( p_input, &p_aac->adif_header ) )
459 p_aac->b_adif_header = 1;
461 "found ADIF header (unsupported)" );
466 if( GetADTS( p_input,
471 p_aac->b_adts_header = 1;
473 "found ADTS header" );
478 "AAC module discarded (no header found)" );
482 ExtractConfiguration( p_aac );
484 vlc_mutex_lock( &p_input->stream.stream_lock );
485 if( input_InitStream( p_input, 0 ) == -1)
487 vlc_mutex_unlock( &p_input->stream.stream_lock );
488 msg_Err( p_input, "cannot init stream" );
491 if( input_AddProgram( p_input, 0, 0) == NULL )
493 vlc_mutex_unlock( &p_input->stream.stream_lock );
494 msg_Err( p_input, "cannot add program" );
497 p_input->stream.pp_programs[0]->b_is_ok = 0;
498 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
501 p_aac->p_es = input_AddES( p_input, p_input->stream.p_selected_program,
502 1 /* id */, AUDIO_ES, NULL, 0 );
505 vlc_mutex_unlock( &p_input->stream.stream_lock );
506 msg_Err( p_input, "out of memory" );
510 p_aac->p_es->i_stream_id = 1;
511 p_aac->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'a' );
512 input_SelectES( p_input, p_aac->p_es );
514 p_input->stream.p_selected_program->b_is_ok = 1;
515 vlc_mutex_unlock( &p_input->stream.stream_lock );
519 vlc_mutex_lock( &p_input->stream.stream_lock );
520 if( p_aac->b_adif_header )
522 p_input->stream.i_mux_rate = 0 / 50;
524 if( p_aac->b_adts_header )
526 p_input->stream.i_mux_rate = 0 / 50;
528 vlc_mutex_unlock( &p_input->stream.stream_lock );
530 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
531 /* if i don't do that, it don't work correctly but why ??? */
532 if( p_input->stream.b_seekable )
534 p_input->pf_seek( p_input, 0 );
535 input_AccessReinit( p_input );
537 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
541 if( p_aac->b_adif_header )
545 if( p_aac->b_adts_header )
548 "audio AAC MPEG-%d layer-%d %d channels %dHz",
549 p_aac->adts_header.i_id == 1 ? 2 : 4,
550 4 - p_aac->adts_header.i_layer, /* it's always 4 in fact */
552 p_aac->i_samplerate );
554 vlc_mutex_lock( &p_input->stream.stream_lock );
555 p_category = input_InfoCategory( p_input, _("Aac") );
557 input_AddInfo( p_category, _("Input Type"), "MPEG-%d AAC",
558 p_aac->adts_header.i_id == 1 ? 2 : 4 );
560 input_AddInfo( p_category, _("Layer"), "%d",
561 4 - p_aac->adts_header.i_layer );
562 input_AddInfo( p_category, _("Channels"), "%d",
564 input_AddInfo( p_category, _("Sample Rate"), "%dHz",
565 p_aac->i_samplerate );
567 vlc_mutex_unlock( &p_input->stream.stream_lock );
570 p_input->p_demux_data = p_aac;
576 /*****************************************************************************
577 * Demux: reads and demuxes data packets
578 *****************************************************************************
579 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
580 *****************************************************************************/
581 static int Demux( input_thread_t * p_input )
587 demux_sys_t *p_aac = p_input->p_demux_data;
590 if( p_aac->b_adif_header )
593 return( -1 ); /* FIXME */
596 if( p_aac->b_adts_header )
598 i_found = GetADTS( p_input,
607 ExtractConfiguration( p_aac );
609 /* skip garbage bytes */
615 SkipBytes( p_input, i_skip );
620 msg_Info( p_input, "can't find next frame" );
624 input_ClockManageRef( p_input,
625 p_input->stream.p_selected_program,
628 if( !ReadPES( p_input, &p_pes, p_aac->i_aac_frame_length ) )
631 "cannot read data" );
635 p_pes->i_pts = input_ClockGetTS( p_input,
636 p_input->stream.p_selected_program,
639 if( !p_aac->p_es->p_decoder_fifo )
641 msg_Err( p_input, "no audio decoder" );
642 input_DeletePES( p_input->p_method_data, p_pes );
643 return( -1 ); /* perhaps not, it's my choice */
647 input_DecodePES( p_aac->p_es->p_decoder_fifo, p_pes );
650 /* Update date information */
651 p_aac->i_pts += (mtime_t)90000 *
652 (mtime_t)p_aac->i_framelength /
653 (mtime_t)p_aac->i_samplerate;
658 static void Deactivate( vlc_object_t * p_this )
660 input_thread_t *p_input = (input_thread_t*)p_this;
661 // demux_sys_t *p_aac = p_input->p_demux_data;
663 FREE( p_input->p_demux_data );