1 /*****************************************************************************
2 * mpeg_audio.c : mpeg_audio Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mpeg_audio.c,v 1.13 2002/07/23 00:39:17 sam 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 void input_getfunctions ( function_list_t * p_function_list );
39 static int MPEGAudioDemux ( input_thread_t * );
40 static int MPEGAudioInit ( input_thread_t * );
41 static void MPEGAudioEnd ( input_thread_t * );
43 /* TODO: support MPEG-2.5, not difficult */
45 /*****************************************************************************
46 * Build configuration tree.
47 *****************************************************************************/
52 SET_DESCRIPTION( "MPEG I/II Audio stream demux" )
53 ADD_CAPABILITY( DEMUX, 110 )
54 ADD_SHORTCUT( "mpegaudio" )
58 input_getfunctions( &p_module->p_functions->demux );
61 MODULE_DEACTIVATE_START
62 MODULE_DEACTIVATE_STOP
64 /*****************************************************************************
65 * Functions exported as capabilities. They are declared as static so that
66 * we don't pollute the namespace too much.
67 *****************************************************************************/
68 static void input_getfunctions( function_list_t * p_function_list )
70 #define input p_function_list->functions.demux
71 input.pf_init = MPEGAudioInit;
72 input.pf_end = MPEGAudioEnd;
73 input.pf_demux = MPEGAudioDemux;
74 input.pf_rewind = NULL;
78 /*****************************************************************************
79 * Definitions of structures and functions used by this plugins
80 *****************************************************************************/
82 /* XXX set this to 0 to avoid problem with PS XXX */
83 /* but with some file or web radio will failed to detect */
84 /* it's you to choose */
85 #define MPEGAUDIO_MAXTESTPOS 0
87 #define MPEGAUDIO_MAXFRAMESIZE 1500 /* no exactly */
89 typedef struct mpegaudio_format_s
105 } mpegaudio_format_t;
107 /* Xing Header if present */
108 #define FRAMES_FLAG 0x0001 /* these flags is for i_flags */
109 #define BYTES_FLAG 0x0002 /* because all is optionnal */
110 #define TOC_FLAG 0x0004
111 #define VBR_SCALE_FLAG 0x0008
112 typedef struct mpegaudio_xing_header_s
114 int i_flags; /* from Xing header data */
115 int i_frames; /* total bit stream frames from Xing header data */
116 int i_bytes; /* total bit stream bytes from Xing header data */
117 int i_vbr_scale; /* encoded vbr scale from Xing header data */
118 u8 i_toc[100]; /* for seek */
119 int i_avgbitrate; /* calculated, XXX: bits/sec not Kb */
120 } mpegaudio_xing_header_t;
122 typedef struct demux_data_mpegaudio_s
128 es_descriptor_t *p_es;
129 mpegaudio_format_t mpeg;
130 mpegaudio_xing_header_t xingheader;
132 } demux_data_mpegaudio_t;
135 static int mpegaudio_bitrate[2][3][16] =
138 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0 }, /* v1 l1 */
139 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0 }, /* v1 l2 */
140 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0 } /* v1 l3 */
144 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0 }, /* v2 l1 */
145 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 }, /* v2 l2 */
146 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 } /* v2 l3 */
151 static int mpegaudio_samplingfreq[2][4] = /* version 1 then 2 */
153 { 44100, 48000, 32000, 0 },
154 { 22050, 24000, 16000, 0 }
157 static char* mpegaudio_mode[4] =
159 "stereo", "joint stereo", "dual channel", "mono"
162 static inline u32 __GetDWBE( byte_t *p_buff )
164 return( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) +
165 ( (*(p_buff+2)) << 8 ) + ( (*(p_buff+3)) ) );
168 static int __CheckPS( input_thread_t *p_input )
171 int i_size = input_Peek( p_input, &p_buff, 8196 );
175 if( !(*p_buff) && !(*(p_buff + 1))
176 && (*(p_buff + 2) == 1 ) && (*(p_buff + 3) >= 0xB9 ) )
178 return( 1 ); /* it could be ps so ...*/
187 #define __GetDWBE( p_buff ) \
188 ( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) + \
189 ( (*(p_buff+2)) << 8 ) + ( (*(p_buff+3)) ) )
191 /*****************************************************************************
192 * MPEGAudio_CheckHeader : Test the validity of the header
193 *****************************************************************************/
194 static int MPEGAudio_CheckHeader( u32 i_header )
196 if( ((( i_header >> 20 )&0x0FFF) != 0x0FFF ) /* header sync */
197 || (((i_header >> 17)&0x03) == 0 ) /* valid layer ?*/
198 || (((i_header >> 12)&0x0F) == 0x0F )
199 || (((i_header >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
200 || (((i_header >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
201 || ((i_header & 0x03) == 0x02 )) /* valid emphasis ? */
203 return( 0 ); /*invalid */
205 return( 1 ); /* valid */
208 /*****************************************************************************
209 * MPEGAudio_ParseHeader : Parse a header ;)
210 *****************************************************************************/
211 static void MPEGAudio_ParseHeader( u32 i_header, mpegaudio_format_t *p_mpeg )
213 p_mpeg->i_header = i_header;
214 p_mpeg->i_version = 1 - ( ( i_header >> 19 ) & 0x01 );
215 p_mpeg->i_layer = 3 - ( ( i_header >> 17 ) & 0x03 );
216 p_mpeg->i_crc = 1 - (( i_header >> 16 ) & 0x01);
217 p_mpeg->i_bitrate = mpegaudio_bitrate[p_mpeg->i_version][p_mpeg->i_layer][(i_header>>12)&0x0F];
218 p_mpeg->i_samplingfreq = mpegaudio_samplingfreq[p_mpeg->i_version][(i_header>>10)&0x03];
219 p_mpeg->i_padding = (( i_header >> 9 ) & 0x01);
220 p_mpeg->i_extension = ( i_header >> 7 ) & 0x01;
221 p_mpeg->i_mode = ( i_header >> 6 ) & 0x03;
222 p_mpeg->i_modeext = ( i_header >> 4 ) & 0x03;
223 p_mpeg->i_copyright = ( i_header >> 3 ) & 0x01;
224 p_mpeg->i_original = ( i_header >> 2 ) & 0x01;
225 p_mpeg->i_emphasis = ( i_header ) & 0x03;
228 /*****************************************************************************
229 * MPEGAudio_FrameSize : give the size of a frame in the mpeg stream
230 *****************************************************************************/
231 static int MPEGAudio_FrameSize( mpegaudio_format_t *p_mpeg )
233 /* XXX if crc do i need to add 2 bytes or not? */
234 switch( p_mpeg->i_layer )
237 return( ( ( ( !p_mpeg->i_version ? 12000 : 6000 ) *
238 p_mpeg->i_bitrate ) /
239 p_mpeg->i_samplingfreq + p_mpeg->i_padding ) * 4);
242 return( ( ( !p_mpeg->i_version ? 144000 : 72000 ) *
243 p_mpeg->i_bitrate ) /
244 p_mpeg->i_samplingfreq + p_mpeg->i_padding );
246 return( 1024 ); /* must never happen, 1k to advance in stream*/
249 /*****************************************************************************
250 * MPEGAudio_DecodedFrameSize : give the length of the decoded pcm data
251 *****************************************************************************/
252 static int MPEGAudio_DecodedFrameSize( mpegaudio_format_t *p_mpeg )
254 switch( p_mpeg->i_layer )
256 case( 0 ): /* layer 1 */
258 case( 1 ): /* layer 2 */
260 case( 2 ): /* layer 3 */
261 return( !p_mpeg->i_version ? 1152 : 576 );
262 /* XXX: perhaps we have to /2 for all layer but i'm not sure */
267 static int MPEGAudio_SkipID3Tag( input_thread_t *p_input )
271 byte_t version, revision;
275 msg_Dbg( p_input, "Checking for ID3 tag" );
276 /* get 10 byte id3 header */
277 if( ( count = input_Peek( p_input, &p_peek, 10 ) ) < 10 )
279 msg_Err( p_input, "cannot peek()" );
283 msg_Info( p_input, "Three first bytes are: %d %d %d",
289 if ( !( (p_peek[0] == 0x49) && (p_peek[1] == 0x44) && (p_peek[2] == 0x33)))
294 version = p_peek[3]; /* These may become usfull later, */
295 revision = p_peek[4]; /* but we ignore them for now */
297 b_footer = p_peek[5] & 0x10;
298 i_size = (p_peek[6] << 21) +
301 p_peek[9]; //Is this safe?
307 msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
308 if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
310 msg_Err( p_input, "cannot peek()" );
314 p_input->p_current_data += i_size; //seek passed end of ID3 tag
319 /*****************************************************************************
320 * MPEGAudio_FindFrame : Find a header that could be valid.
321 *****************************************************************************
322 * The idea is to search for 2 consecutive headers that seem valid
323 * Perhaps we can search 2 header with same version or samplefreq(...) to be
324 * more secure but this seems to be enougth
325 *****************************************************************************/
326 static int MPEGAudio_FindFrame( input_thread_t *p_input,
328 mpegaudio_format_t *p_mpeg,
336 int i_size = input_Peek( p_input, &p_buff, i_posmax+MPEGAUDIO_MAXFRAMESIZE);
338 while( i_pos <= __MIN( i_posmax, i_size - 4) )
340 i_header = __GetDWBE( p_buff );
341 if( MPEGAudio_CheckHeader( i_header ) )
343 MPEGAudio_ParseHeader( i_header, p_mpeg );
344 i_framesize = MPEGAudio_FrameSize( p_mpeg );
345 if( i_pos + i_framesize + 4 > i_size )
352 if( MPEGAudio_CheckHeader( __GetDWBE( p_buff + i_framesize ) ) )
367 /*****************************************************************************
368 * MPEGAudio_ExtractXingHeader : extract a Xing header if exist
369 *****************************************************************************
370 * It also calcul avgbitrate, using Xing header if present or assume that
371 * the bitrate of the first frame is the same for the all file
372 *****************************************************************************/
373 static void MPEGAudio_ExtractXingHeader( input_thread_t *p_input,
374 mpegaudio_xing_header_t *p_xh )
378 mpegaudio_format_t mpeg;
381 p_xh->i_flags = 0; /* nothing present */
382 if( !(MPEGAudio_FindFrame( p_input, &i_pos, &mpeg, 2024 )) )
384 return; /* failed , can't */
386 p_xh->i_avgbitrate = mpeg.i_bitrate * 1000; /* default */
388 /* 1024 is enougth */
389 if( ( i_size = input_Peek( p_input, &p_buff, 1024 + i_pos ) ) < 8 )
395 /* calculate pos of xing header */
396 if( !mpeg.i_version )
398 p_buff += mpeg.i_mode != 3 ? 36 : 21;
402 p_buff += mpeg.i_mode != 3 ? 21 : 13;
405 if( (*p_buff != 'X' )||(*(p_buff+1) != 'i' )
406 ||(*(p_buff+2) != 'n' )||(*(p_buff+3) != 'g' ) )
412 p_xh->i_flags = __GetDWBE( p_buff );
415 if( p_xh->i_flags&FRAMES_FLAG )
417 p_xh->i_frames = __GetDWBE( p_buff );
420 if( p_xh->i_flags&BYTES_FLAG )
422 p_xh->i_bytes = __GetDWBE( p_buff );
425 if( p_xh->i_flags&TOC_FLAG )
427 p_input->p_vlc->pf_memcpy( p_xh->i_toc, p_buff, 100 );
430 if( p_xh->i_flags&VBR_SCALE_FLAG )
432 p_xh->i_vbr_scale = __GetDWBE( p_buff );
435 if( ( p_xh->i_flags&FRAMES_FLAG )&&( p_xh->i_flags&BYTES_FLAG ) )
438 ((u64)p_xh->i_bytes * (u64)8 * (u64)mpeg.i_samplingfreq) /
439 ((u64)p_xh->i_frames * (u64)MPEGAudio_DecodedFrameSize( &mpeg));
444 /*****************************************************************************
445 * MPEGaudioInit : initializes MPEGaudio structures
446 *****************************************************************************/
447 static int MPEGAudioInit( input_thread_t * p_input )
449 demux_data_mpegaudio_t *p_mpegaudio;
450 mpegaudio_format_t mpeg;
451 es_descriptor_t * p_es;
454 input_info_category_t * p_category;
456 /* XXX: i don't know what it's supposed to do, copied from ESInit */
457 /* Initialize access plug-in structures. */
458 if( p_input->i_mtu == 0 )
461 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
463 if( ( *p_input->psz_demux )
464 &&( !strncmp( p_input->psz_demux, "mpegaudio", 10 ) ) )
473 if ( MPEGAudio_SkipID3Tag( p_input ) )
478 /* check if it can be a ps stream */
479 if( __CheckPS( p_input ) && !b_forced )
484 /* must be sure that is mpeg audio stream */
485 if( MPEGAudio_FindFrame( p_input,
488 (b_forced ? 2 * MPEGAUDIO_MAXFRAMESIZE :
489 MPEGAUDIO_MAXTESTPOS) )
490 < (b_forced ? 1 : 2) )
492 msg_Warn( p_input, "MPEGAudio module discarded (no frame found)" );
496 vlc_mutex_lock( &p_input->stream.stream_lock );
497 if( input_InitStream( p_input, 0 ) == -1)
499 msg_Err( p_input, "cannot init stream" );
502 if( input_AddProgram( p_input, 0, 0) == NULL )
504 msg_Err( p_input, "cannot add program" );
507 p_input->stream.pp_programs[0]->b_is_ok = 0;
508 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
511 p_es = input_AddES( p_input,
512 p_input->stream.p_selected_program,
517 vlc_mutex_unlock( &p_input->stream.stream_lock );
518 msg_Err( p_input, "out of memory" );
521 p_es->i_stream_id = 1;
522 p_es->i_fourcc = !mpeg.i_layer ? VLC_FOURCC('m','p','g','a') /* layer 1 */
523 : VLC_FOURCC('m','p','g','a'); /* layer 2 */
524 p_es->i_cat = AUDIO_ES;
525 input_SelectES( p_input, p_es );
527 p_input->stream.p_selected_program->b_is_ok = 1;
528 vlc_mutex_unlock( &p_input->stream.stream_lock );
530 /* create p_mpegaudio and init it */
531 p_input->p_demux_data =
532 p_mpegaudio = malloc( sizeof( demux_data_mpegaudio_t ));
536 msg_Err( p_input, "out of memory" );
540 /*input_ClockInit( p_input->stream.p_selected_program );
541 done by AddProgram */
542 p_mpegaudio->p_es = p_es;
543 p_mpegaudio->mpeg = mpeg;
544 p_mpegaudio->i_framecount = 0;
545 p_mpegaudio->i_pts = 0;
547 /* parse Xing Header if present */
548 MPEGAudio_ExtractXingHeader( p_input, &p_mpegaudio->xingheader );
550 vlc_mutex_lock( &p_input->stream.stream_lock );
551 p_input->stream.i_mux_rate = p_mpegaudio->xingheader.i_avgbitrate / 50 / 8;
552 vlc_mutex_unlock( &p_input->stream.stream_lock );
554 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
555 /* if i don't do that, it don't work correctly but why ??? */
556 if( p_input->stream.b_seekable )
558 p_input->pf_seek( p_input, 0 );
559 input_AccessReinit( p_input );
561 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
564 msg_Dbg( p_input, "audio MPEG-%d layer %d %s %dHz %dKb/s %s",
567 mpegaudio_mode[mpeg.i_mode],
569 p_mpegaudio->xingheader.i_avgbitrate / 1000,
570 p_mpegaudio->xingheader.i_flags ?
574 vlc_mutex_lock( &p_input->stream.stream_lock );
575 p_category = input_InfoCategory( p_input, "mpeg" );
576 input_AddInfo( p_category, "input type", "audio MPEG-%d",
578 input_AddInfo( p_category, "layer", "%d", mpeg.i_layer + 1 );
579 input_AddInfo( p_category, "mode", mpegaudio_mode[mpeg.i_mode] );
580 input_AddInfo( p_category, "sample rate", "%dHz", mpeg.i_samplingfreq );
581 input_AddInfo( p_category, "average bitrate", "%dKb/s",
582 p_mpegaudio->xingheader.i_avgbitrate / 1000 );
583 vlc_mutex_unlock( &p_input->stream.stream_lock );
589 /*****************************************************************************
590 * MPEGAudioEnd: frees unused data
591 *****************************************************************************/
592 static void MPEGAudioEnd( input_thread_t * p_input )
598 /*****************************************************************************
599 * MPEGAudioDemux: reads and demuxes data packets
600 *****************************************************************************
601 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
602 *****************************************************************************/
603 static int MPEGAudioDemux( input_thread_t * p_input )
608 mpegaudio_format_t mpeg;
609 demux_data_mpegaudio_t *p_mpegaudio =
610 (demux_data_mpegaudio_t*) p_input->p_demux_data;
611 /* look for a frame */
612 if( !MPEGAudio_FindFrame( p_input, &i_pos, &mpeg, 4096 ) )
614 msg_Warn( p_input, "cannot find next frame" );
618 /* if stream has changed */
619 if( ( mpeg.i_version != p_mpegaudio->mpeg.i_version )
620 ||( mpeg.i_layer != p_mpegaudio->mpeg.i_layer )
621 ||( mpeg.i_samplingfreq != p_mpegaudio->mpeg.i_samplingfreq ) )
623 msg_Dbg( p_input, "stream has changed" );
624 p_mpegaudio->i_framecount = 0;
625 p_mpegaudio->i_pts = 0;
628 input_ClockManageRef( p_input,
629 p_input->stream.p_selected_program,
630 p_mpegaudio->i_pts );
632 /* in fact i_pos may be garbage but ... i don't want to skip it
635 i_toread = MPEGAudio_FrameSize( &mpeg ) + i_pos;
637 if( !(p_pes = input_NewPES( p_input->p_method_data )) )
639 msg_Err( p_input, "cannot allocate new PES" );
643 while( i_toread > 0 )
645 data_packet_t *p_data;
648 if( (i_read = input_SplitBuffer( p_input, &p_data, i_toread ) ) <= 0 )
652 if( !p_pes->p_first )
654 p_pes->p_first = p_data;
655 p_pes->i_nb_data = 1;
656 p_pes->i_pes_size = i_read;
660 p_pes->p_last->p_next = p_data;
662 p_pes->i_pes_size += i_read;
664 p_pes->p_last = p_data;
667 p_mpegaudio->i_pts = (mtime_t)90000 *
668 (mtime_t)p_mpegaudio->i_framecount *
669 (mtime_t)MPEGAudio_DecodedFrameSize( &mpeg ) /
670 (mtime_t)mpeg.i_samplingfreq;
672 p_pes->i_pts = input_ClockGetTS( p_input,
673 p_input->stream.p_selected_program,
674 p_mpegaudio->i_pts );
676 if( !p_mpegaudio->p_es->p_decoder_fifo )
678 msg_Err( p_input, "no audio decoder" );
679 input_DeletePES( p_input->p_method_data, p_pes );
680 return( -1 ); /* perhaps not, it's my choice */
684 input_DecodePES( p_mpegaudio->p_es->p_decoder_fifo, p_pes );
687 p_mpegaudio->i_framecount++;
688 p_mpegaudio->mpeg = mpeg;