1 /*****************************************************************************
2 * audio.c : mpeg audio Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: audio.c,v 1.13 2003/02/18 00:51:40 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 * );
41 /* TODO: support MPEG-2.5, not difficult, but I need somes samples... */
43 /*****************************************************************************
45 *****************************************************************************/
47 set_description( _("MPEG I/II audio stream demux" ) );
48 set_capability( "demux", 50 );
49 set_callbacks( Activate, NULL );
50 add_shortcut( "mpegaudio" );
51 add_shortcut( "mp3" );
54 /*****************************************************************************
55 * Definitions of structures and functions used by this plugins
56 *****************************************************************************/
58 /* XXX set this to 0 to avoid problem with PS XXX */
59 /* but with some file or web radio will failed to detect */
60 /* it's you to choose */
61 #define MPEGAUDIO_MAXTESTPOS 0
63 typedef struct mpeg_header_s
81 /* Xing Header if present */
82 #define FRAMES_FLAG 0x0001 /* these flags is for i_flags */
83 #define BYTES_FLAG 0x0002 /* because all is optionnal */
84 #define TOC_FLAG 0x0004
85 #define VBR_SCALE_FLAG 0x0008
86 typedef struct xing_header_s
88 int i_flags; /* from Xing header data */
89 int i_frames; /* total bit stream frames from Xing header data */
90 int i_bytes; /* total bit stream bytes from Xing header data */
91 int i_vbr_scale; /* encoded vbr scale from Xing header data */
92 uint8_t i_toc[100]; /* for seek */
93 int i_avgbitrate; /* calculated, XXX: bits/sec not Kb */
100 es_descriptor_t *p_es;
102 xing_header_t xingheader;
104 /* extracted information */
111 static int mpegaudio_bitrate[2][3][16] =
115 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
117 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
119 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
124 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
126 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
128 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
133 static int mpegaudio_samplerate[2][4] = /* version 1 then 2 */
135 { 44100, 48000, 32000, 0 },
136 { 22050, 24000, 16000, 0 }
139 static char* mpegaudio_mode[4] =
141 "stereo", "joint stereo", "dual channel", "mono"
144 static inline uint32_t GetDWBE( uint8_t *p_buff )
146 return( ( p_buff[0] << 24 )|( p_buff[1] << 16 )|
147 ( p_buff[2] << 8 )|( p_buff[3] ) );
150 /*****************************************************************************
151 * Function to manipulate stream easily
152 *****************************************************************************
154 * SkipBytes : skip bytes, not yet optimised, read bytes to be skipped :P
156 * ReadPes : read data and make a PES
158 *****************************************************************************/
159 static int SkipBytes( input_thread_t *p_input, int i_size )
161 data_packet_t *p_data;
166 i_read = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
171 input_DeletePacket( p_input->p_method_data, p_data );
177 static int ReadPES( input_thread_t *p_input,
178 pes_packet_t **pp_pes,
185 if( !(p_pes = input_NewPES( p_input->p_method_data )) )
187 msg_Err( p_input, "cannot allocate new PES" );
193 data_packet_t *p_data;
196 if( (i_read = input_SplitBuffer( p_input,
198 __MIN( i_size, 1024 ) ) ) <= 0 )
200 input_DeletePES( p_input->p_method_data, p_pes );
203 if( !p_pes->p_first )
205 p_pes->p_first = p_data;
206 p_pes->i_nb_data = 1;
207 p_pes->i_pes_size = i_read;
211 p_pes->p_last->p_next = p_data;
213 p_pes->i_pes_size += i_read;
215 p_pes->p_last = p_data;
222 /*****************************************************************************
223 * CheckHeader : Test the validity of the header
224 *****************************************************************************/
225 static int CheckHeader( uint32_t i_header )
227 if( ((( i_header >> 20 )&0x0FFF) != 0x0FFF ) /* header sync */
228 || (((i_header >> 17)&0x03) == 0 ) /* valid layer ?*/
229 || (((i_header >> 12)&0x0F) == 0x0F )
230 || (((i_header >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
231 || (((i_header >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
232 || ((i_header & 0x03) == 0x02 )) /* valid emphasis ? */
234 return( 0 ); /*invalid */
236 return( 1 ); /* valid */
239 /*****************************************************************************
240 * DecodedFrameSize : give the length of the decoded pcm data
241 *****************************************************************************/
242 static int DecodedFrameSize( mpeg_header_t *p_mpeg )
244 switch( p_mpeg->i_layer )
246 case( 0 ): /* layer 1 */
248 case( 1 ): /* layer 2 */
250 case( 2 ): /* layer 3 */
251 return( !p_mpeg->i_version ? 1152 : 576 );
252 /* XXX: perhaps we have to /2 for all layer but i'm not sure */
257 /****************************************************************************
258 * GetHeader : find an mpeg header and load it
259 ****************************************************************************/
260 static int GetHeader( input_thread_t *p_input,
261 mpeg_header_t *p_mpeg,
270 i_size = input_Peek( p_input, &p_peek, i_max_pos + 4 );
278 if( !CheckHeader( GetDWBE( p_peek ) ) )
285 /* we found an header, load it */
288 i_header = GetDWBE( p_peek );
289 p_mpeg->i_header = i_header;
290 p_mpeg->i_version = 1 - ( ( i_header >> 19 ) & 0x01 );
291 p_mpeg->i_layer = 3 - ( ( i_header >> 17 ) & 0x03 );
292 p_mpeg->i_crc = 1 - (( i_header >> 16 ) & 0x01);
294 mpegaudio_bitrate[p_mpeg->i_version][p_mpeg->i_layer][(i_header>>12)&0x0F];
295 p_mpeg->i_samplerate = mpegaudio_samplerate[p_mpeg->i_version][(i_header>>10)&0x03];
296 p_mpeg->i_padding = (( i_header >> 9 ) & 0x01);
297 p_mpeg->i_extension = ( i_header >> 7 ) & 0x01;
298 p_mpeg->i_mode = ( i_header >> 6 ) & 0x03;
299 p_mpeg->i_modeext = ( i_header >> 4 ) & 0x03;
300 p_mpeg->i_copyright = ( i_header >> 3 ) & 0x01;
301 p_mpeg->i_original = ( i_header >> 2 ) & 0x01;
302 p_mpeg->i_emphasis = ( i_header ) & 0x03;
307 /*****************************************************************************
308 * ExtractXingHeader : extract a Xing header if exist
309 *****************************************************************************
310 * It also calcul avgbitrate, using Xing header if present or assume that
311 * the bitrate of the first frame is the same for the all file
312 *****************************************************************************/
313 static void ExtractXingHeader( input_thread_t *p_input,
314 xing_header_t *p_xh )
321 p_xh->i_flags = 0; /* nothing present */
322 if( !( GetHeader( p_input,
327 msg_Err( p_input, "ExtractXingHeader failed, shouldn't ..." );
331 p_xh->i_avgbitrate = mpeg.i_bitrate * 1000; /* default */
333 /* 1024 is enougth */
334 if( ( i_size = input_Peek( p_input, &p_peek, 1024 + i_skip ) ) < 8 )
341 /* calculate pos of xing header */
342 if( !mpeg.i_version )
344 p_peek += mpeg.i_mode != 3 ? 36 : 21;
345 i_size -= mpeg.i_mode != 3 ? 36 : 21;
349 p_peek += mpeg.i_mode != 3 ? 21 : 13;
350 i_size -= mpeg.i_mode != 3 ? 21 : 13;
356 if( ( p_peek[0] != 'X' )||( p_peek[1] != 'i' )||
357 ( p_peek[2] != 'n' )||( p_peek[3] != 'g' ) )
363 msg_Dbg( p_input, "Xing header is present" );
373 p_xh->i_flags = GetDWBE( p_peek );
378 if( ( p_xh->i_flags&FRAMES_FLAG )&&( i_size >= 4 ) )
380 p_xh->i_frames = GetDWBE( p_peek );
384 if( ( p_xh->i_flags&BYTES_FLAG ) &&( i_size >= 4 ) )
387 p_xh->i_bytes = GetDWBE( p_peek );
391 if( ( p_xh->i_flags&TOC_FLAG ) &&( i_size >= 100 ) )
394 memcpy( p_xh->i_toc, p_peek, 100 );
398 if( ( p_xh->i_flags&VBR_SCALE_FLAG ) &&( i_size >= 4 ) )
401 p_xh->i_vbr_scale = GetDWBE( p_peek );
406 if( ( p_xh->i_flags&FRAMES_FLAG )&&( p_xh->i_flags&BYTES_FLAG ) )
409 ( (uint64_t)p_xh->i_bytes *
411 (uint64_t)mpeg.i_samplerate) /
412 ((uint64_t)p_xh->i_frames * (uint64_t)DecodedFrameSize( &mpeg ) );
416 /****************************************************************************
417 * ExtractConfiguration : extract usefull informations from mpeg_header_t
418 ****************************************************************************/
419 static void ExtractConfiguration( demux_sys_t *p_demux )
421 p_demux->i_samplerate = p_demux->mpeg.i_samplerate;
423 p_demux->i_samplelength = DecodedFrameSize( &p_demux->mpeg );
425 /* XXX if crc do i need to add 2 bytes or not? */
426 switch( p_demux->mpeg.i_layer )
429 p_demux->i_framelength =
430 ( ( 12000 * p_demux->mpeg.i_bitrate ) /
431 p_demux->mpeg.i_samplerate + p_demux->mpeg.i_padding ) * 4;
434 p_demux->i_framelength =
435 ( 144000 * p_demux->mpeg.i_bitrate ) /
436 p_demux->mpeg.i_samplerate + p_demux->mpeg.i_padding;
439 p_demux->i_framelength =
440 (p_demux->mpeg.i_version ? 72000 : 144000) *
441 p_demux->mpeg.i_bitrate /
442 p_demux->mpeg.i_samplerate + p_demux->mpeg.i_padding;
448 /****************************************************************************
449 * CheckPS : check if this stream could be some ps,
450 * yes it's ugly ... but another idea ?
452 ****************************************************************************/
453 static int CheckPS( input_thread_t *p_input )
457 int i_size = input_Peek( p_input, &p_peek, 8196 );
461 if( ( p_peek[0] == 0 ) && ( p_peek[1] == 0 ) &&
462 ( p_peek[2] == 1 ) && ( p_peek[3] >= 0xb9 ) &&
474 /*****************************************************************************
475 * Activate: initializes MPEGaudio structures
476 *****************************************************************************/
477 static int Activate( vlc_object_t * p_this )
479 input_thread_t * p_input = (input_thread_t *)p_this;
480 demux_sys_t * p_demux;
481 input_info_category_t * p_category;
488 /* Set the demux function */
489 p_input->pf_demux = Demux;
491 /* Initialize access plug-in structures. */
492 if( p_input->i_mtu == 0 )
495 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
498 if( ( *p_input->psz_demux )
499 &&( ( !strncmp( p_input->psz_demux, "mpegaudio", 10 ) )||
500 ( !strncmp( p_input->psz_demux, "mp3", 3 ) ) ) )
508 p_id3 = module_Need( p_input, "id3", NULL );
510 module_Unneed( p_input, p_id3 );
513 /* create p_demux and init it */
514 if( !( p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t) ) ) )
516 msg_Err( p_input, "out of memory" );
519 memset( p_demux, 0, sizeof(demux_sys_t) );
521 /* check if it could be a ps stream */
522 if( !b_forced && CheckPS( p_input ))
524 free( p_input->p_demux_data );
529 /* must be sure that is mpeg audio stream unless forced */
530 if( !( i_found = GetHeader( p_input,
532 b_forced ? 4000 : MPEGAUDIO_MAXTESTPOS,
538 "this does not look like an MPEG audio stream, "
539 "but continuing anyway" );
543 msg_Warn( p_input, "MPEGAudio module discarded (no frame found)" );
544 free( p_input->p_demux_data );
550 ExtractConfiguration( p_demux );
554 vlc_mutex_lock( &p_input->stream.stream_lock );
555 if( input_InitStream( p_input, 0 ) == -1)
557 msg_Err( p_input, "cannot init stream" );
558 free( p_input->p_demux_data );
561 if( input_AddProgram( p_input, 0, 0) == NULL )
563 msg_Err( p_input, "cannot add program" );
564 free( p_input->p_demux_data );
567 p_input->stream.pp_programs[0]->b_is_ok = 0;
568 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
571 p_demux->p_es = input_AddES( p_input,
572 p_input->stream.p_selected_program,
577 vlc_mutex_unlock( &p_input->stream.stream_lock );
578 msg_Err( p_input, "out of memory" );
579 free( p_input->p_demux_data );
582 p_demux->p_es->i_stream_id = 1;
583 p_demux->p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
584 p_demux->p_es->i_cat = AUDIO_ES;
586 input_SelectES( p_input, p_demux->p_es );
588 p_input->stream.p_selected_program->b_is_ok = 1;
589 vlc_mutex_unlock( &p_input->stream.stream_lock );
594 /* parse Xing Header if present */
595 ExtractXingHeader( p_input, &p_demux->xingheader );
597 vlc_mutex_lock( &p_input->stream.stream_lock );
598 p_input->stream.i_mux_rate = p_demux->xingheader.i_avgbitrate / 50 / 8;
599 vlc_mutex_unlock( &p_input->stream.stream_lock );
603 msg_Dbg( p_input, "audio MPEG-%d layer %d %s %dHz %dKb/s %s",
604 p_demux->mpeg.i_version + 1,
605 p_demux->mpeg.i_layer + 1,
606 mpegaudio_mode[p_demux->mpeg.i_mode],
607 p_demux->mpeg.i_samplerate,
608 p_demux->xingheader.i_avgbitrate / 1000,
609 p_demux->xingheader.i_flags ?
613 vlc_mutex_lock( &p_input->stream.stream_lock );
614 p_category = input_InfoCategory( p_input, "mpeg" );
615 input_AddInfo( p_category, "input type", "audio MPEG-%d",
616 p_demux->mpeg.i_version +1 );
617 input_AddInfo( p_category, "layer", "%d", p_demux->mpeg.i_layer + 1 );
618 input_AddInfo( p_category, "mode",
619 mpegaudio_mode[p_demux->mpeg.i_mode] );
620 input_AddInfo( p_category, "sample rate", "%dHz",
621 p_demux->mpeg.i_samplerate );
622 input_AddInfo( p_category, "average bitrate", "%dKb/s",
623 p_demux->xingheader.i_avgbitrate / 1000 );
624 vlc_mutex_unlock( &p_input->stream.stream_lock );
629 "assuming audio MPEG, but not frame header yet found" );
630 vlc_mutex_lock( &p_input->stream.stream_lock );
631 p_category = input_InfoCategory( p_input, "mpeg" );
632 input_AddInfo( p_category, "input type", "audio MPEG-?" );
633 vlc_mutex_unlock( &p_input->stream.stream_lock );
640 /*****************************************************************************
641 * Demux: reads and demuxes data packets
642 *****************************************************************************
643 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
644 *****************************************************************************/
645 static int Demux( input_thread_t * p_input )
647 demux_sys_t *p_demux = p_input->p_demux_data;
651 if( !GetHeader( p_input,
659 "skipping %d bytes (garbage ?)",
661 SkipBytes( p_input, i_skip );
667 "cannot find next frame (EOF ?)" );
672 ExtractConfiguration( p_demux );
674 input_ClockManageRef( p_input,
675 p_input->stream.p_selected_program,
679 * For layer 1 and 2 i_skip is garbage but for layer 3 it is not.
680 * Since mad accept without to much trouble garbage I don't skip
681 * it ( in case I misdetect garbage ... )
684 if( !ReadPES( p_input, &p_pes, p_demux->i_framelength + i_skip) )
687 "cannot read data" );
691 p_pes->i_rate = p_input->stream.control.i_rate;
693 p_pes->i_pts = input_ClockGetTS( p_input,
694 p_input->stream.p_selected_program,
697 if( !p_demux->p_es->p_decoder_fifo )
699 msg_Err( p_input, "no audio decoder" );
700 input_DeletePES( p_input->p_method_data, p_pes );
701 return( -1 ); /* perhaps not, it's my choice */
705 input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
707 p_demux->i_pts += (mtime_t)90000 *
708 (mtime_t)p_demux->i_samplelength /
709 (mtime_t)p_demux->i_samplerate;