1 /*****************************************************************************
2 * audio.c : mpeg audio Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: audio.c,v 1.17 2003/04/26 20:51:54 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 demuxer" ) );
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 );
381 if( p_xh->i_frames == 0 ) p_xh->i_flags &= ~FRAMES_FLAG;
385 if( ( p_xh->i_flags&BYTES_FLAG ) &&( i_size >= 4 ) )
388 p_xh->i_bytes = GetDWBE( p_peek );
389 if( p_xh->i_bytes == 0 ) p_xh->i_flags &= ~BYTES_FLAG;
393 if( ( p_xh->i_flags&TOC_FLAG ) &&( i_size >= 100 ) )
396 memcpy( p_xh->i_toc, p_peek, 100 );
400 if( ( p_xh->i_flags&VBR_SCALE_FLAG ) &&( i_size >= 4 ) )
403 p_xh->i_vbr_scale = GetDWBE( p_peek );
408 if( ( p_xh->i_flags&FRAMES_FLAG )&&( p_xh->i_flags&BYTES_FLAG ) )
411 ( (uint64_t)p_xh->i_bytes *
413 (uint64_t)mpeg.i_samplerate) /
414 ((uint64_t)p_xh->i_frames * (uint64_t)DecodedFrameSize( &mpeg ) );
418 /****************************************************************************
419 * ExtractConfiguration : extract usefull informations from mpeg_header_t
420 ****************************************************************************/
421 static void ExtractConfiguration( demux_sys_t *p_demux )
423 p_demux->i_samplerate = p_demux->mpeg.i_samplerate;
425 p_demux->i_samplelength = DecodedFrameSize( &p_demux->mpeg );
427 /* XXX if crc do i need to add 2 bytes or not? */
428 switch( p_demux->mpeg.i_layer )
431 p_demux->i_framelength =
432 ( ( 12000 * p_demux->mpeg.i_bitrate ) /
433 p_demux->mpeg.i_samplerate + p_demux->mpeg.i_padding ) * 4;
436 p_demux->i_framelength =
437 ( 144000 * p_demux->mpeg.i_bitrate ) /
438 p_demux->mpeg.i_samplerate + p_demux->mpeg.i_padding;
441 p_demux->i_framelength =
442 (p_demux->mpeg.i_version ? 72000 : 144000) *
443 p_demux->mpeg.i_bitrate /
444 p_demux->mpeg.i_samplerate + p_demux->mpeg.i_padding;
450 /****************************************************************************
451 * CheckPS : check if this stream could be some ps,
452 * yes it's ugly ... but another idea ?
454 ****************************************************************************/
455 static int CheckPS( input_thread_t *p_input )
459 int i_size = input_Peek( p_input, &p_peek, 8196 );
463 if( ( p_peek[0] == 0 ) && ( p_peek[1] == 0 ) &&
464 ( p_peek[2] == 1 ) && ( p_peek[3] >= 0xb9 ) &&
476 /*****************************************************************************
477 * Activate: initializes MPEGaudio structures
478 *****************************************************************************/
479 static int Activate( vlc_object_t * p_this )
481 input_thread_t * p_input = (input_thread_t *)p_this;
482 demux_sys_t * p_demux;
483 input_info_category_t * p_category;
492 /* Set the demux function */
493 p_input->pf_demux = Demux;
495 /* Initialize access plug-in structures. */
496 if( p_input->i_mtu == 0 )
499 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
502 b_forced = VLC_FALSE;
503 i_max_pos = MPEGAUDIO_MAXTESTPOS;
505 if( ( *p_input->psz_demux )
506 &&( ( !strncmp( p_input->psz_demux, "mpegaudio", 10 ) )||
507 ( !strncmp( p_input->psz_demux, "mp3", 3 ) ) ) )
512 else if( p_input->psz_name )
514 char *name = p_input->psz_name;
515 int i_len = strlen( name );
517 if( i_len > 4 && !strcasecmp( &name[i_len - 4], ".mp3" ) )
523 p_id3 = module_Need( p_input, "id3", NULL );
525 module_Unneed( p_input, p_id3 );
528 /* create p_demux and init it */
529 if( !( p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t) ) ) )
531 msg_Err( p_input, "out of memory" );
534 memset( p_demux, 0, sizeof(demux_sys_t) );
536 /* check if it could be a ps stream */
537 if( !b_forced && CheckPS( p_input ))
539 free( p_input->p_demux_data );
544 /* must be sure that is mpeg audio stream unless forced */
545 if( !( i_found = GetHeader( p_input,
553 "this does not look like an MPEG audio stream, "
554 "but continuing anyway" );
558 msg_Warn( p_input, "MPEGAudio module discarded (no frame found)" );
559 free( p_input->p_demux_data );
565 ExtractConfiguration( p_demux );
569 vlc_mutex_lock( &p_input->stream.stream_lock );
570 if( input_InitStream( p_input, 0 ) == -1)
572 msg_Err( p_input, "cannot init stream" );
573 free( p_input->p_demux_data );
576 if( input_AddProgram( p_input, 0, 0) == NULL )
578 msg_Err( p_input, "cannot add program" );
579 free( p_input->p_demux_data );
582 p_input->stream.pp_programs[0]->b_is_ok = 0;
583 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
586 p_demux->p_es = input_AddES( p_input,
587 p_input->stream.p_selected_program,
592 vlc_mutex_unlock( &p_input->stream.stream_lock );
593 msg_Err( p_input, "out of memory" );
594 free( p_input->p_demux_data );
597 p_demux->p_es->i_stream_id = 1;
598 p_demux->p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
599 p_demux->p_es->i_cat = AUDIO_ES;
601 input_SelectES( p_input, p_demux->p_es );
603 p_input->stream.p_selected_program->b_is_ok = 1;
604 vlc_mutex_unlock( &p_input->stream.stream_lock );
609 /* parse Xing Header if present */
610 ExtractXingHeader( p_input, &p_demux->xingheader );
612 vlc_mutex_lock( &p_input->stream.stream_lock );
613 p_input->stream.i_mux_rate = p_demux->xingheader.i_avgbitrate / 50 / 8;
614 vlc_mutex_unlock( &p_input->stream.stream_lock );
618 msg_Dbg( p_input, "audio MPEG-%d layer %d %s %dHz %dKb/s %s",
619 p_demux->mpeg.i_version + 1,
620 p_demux->mpeg.i_layer + 1,
621 mpegaudio_mode[p_demux->mpeg.i_mode],
622 p_demux->mpeg.i_samplerate,
623 p_demux->xingheader.i_avgbitrate / 1000,
624 p_demux->xingheader.i_flags ?
628 vlc_mutex_lock( &p_input->stream.stream_lock );
629 p_category = input_InfoCategory( p_input, _("mpeg") );
630 input_AddInfo( p_category, _("Input Type"), "Audio MPEG-%d",
631 p_demux->mpeg.i_version +1 );
632 input_AddInfo( p_category, _("Layer"), "%d", p_demux->mpeg.i_layer + 1 );
633 input_AddInfo( p_category, _("Mode"),
634 mpegaudio_mode[p_demux->mpeg.i_mode] );
635 input_AddInfo( p_category, _("Sample Rate"), "%dHz",
636 p_demux->mpeg.i_samplerate );
637 input_AddInfo( p_category, _("Average Bitrate"), "%dKb/s",
638 p_demux->xingheader.i_avgbitrate / 1000 );
639 vlc_mutex_unlock( &p_input->stream.stream_lock );
644 "assuming audio MPEG, but not frame header yet found" );
645 vlc_mutex_lock( &p_input->stream.stream_lock );
646 p_category = input_InfoCategory( p_input, _("mpeg") );
647 input_AddInfo( p_category, _("Input Type"), "Audio MPEG-?" );
648 vlc_mutex_unlock( &p_input->stream.stream_lock );
655 /*****************************************************************************
656 * Demux: reads and demuxes data packets
657 *****************************************************************************
658 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
659 *****************************************************************************/
660 static int Demux( input_thread_t * p_input )
662 demux_sys_t *p_demux = p_input->p_demux_data;
666 if( !GetHeader( p_input,
674 "skipping %d bytes (garbage ?)",
676 SkipBytes( p_input, i_skip );
682 "cannot find next frame (EOF ?)" );
687 ExtractConfiguration( p_demux );
689 input_ClockManageRef( p_input,
690 p_input->stream.p_selected_program,
694 * For layer 1 and 2 i_skip is garbage but for layer 3 it is not.
695 * Since mad accept without to much trouble garbage I don't skip
696 * it ( in case I misdetect garbage ... )
699 if( !ReadPES( p_input, &p_pes, p_demux->i_framelength + i_skip) )
702 "cannot read data" );
706 p_pes->i_rate = p_input->stream.control.i_rate;
708 p_pes->i_pts = input_ClockGetTS( p_input,
709 p_input->stream.p_selected_program,
712 if( !p_demux->p_es->p_decoder_fifo )
714 msg_Err( p_input, "no audio decoder" );
715 input_DeletePES( p_input->p_method_data, p_pes );
716 return( -1 ); /* perhaps not, it's my choice */
720 input_DecodePES( p_demux->p_es->p_decoder_fifo, p_pes );
722 p_demux->i_pts += (mtime_t)90000 *
723 (mtime_t)p_demux->i_samplelength /
724 (mtime_t)p_demux->i_samplerate;