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.14 2002/07/31 20:56:52 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 int Activate ( vlc_object_t * );
39 static int Demux ( input_thread_t * );
41 /* TODO: support MPEG-2.5, not difficult */
43 /*****************************************************************************
45 *****************************************************************************/
47 set_description( _("ISO 13818-3 MPEG I/II audio stream demux" ) );
48 set_capability( "demux", 100 );
49 set_callbacks( Activate, NULL );
50 add_shortcut( "mpegaudio" );
53 /*****************************************************************************
54 * Definitions of structures and functions used by this plugins
55 *****************************************************************************/
57 /* XXX set this to 0 to avoid problem with PS XXX */
58 /* but with some file or web radio will failed to detect */
59 /* it's you to choose */
60 #define MPEGAUDIO_MAXTESTPOS 0
62 #define MPEGAUDIO_MAXFRAMESIZE 1500 /* no exactly */
64 typedef struct mpegaudio_format_s
82 /* Xing Header if present */
83 #define FRAMES_FLAG 0x0001 /* these flags is for i_flags */
84 #define BYTES_FLAG 0x0002 /* because all is optionnal */
85 #define TOC_FLAG 0x0004
86 #define VBR_SCALE_FLAG 0x0008
87 typedef struct mpegaudio_xing_header_s
89 int i_flags; /* from Xing header data */
90 int i_frames; /* total bit stream frames from Xing header data */
91 int i_bytes; /* total bit stream bytes from Xing header data */
92 int i_vbr_scale; /* encoded vbr scale from Xing header data */
93 u8 i_toc[100]; /* for seek */
94 int i_avgbitrate; /* calculated, XXX: bits/sec not Kb */
95 } mpegaudio_xing_header_t;
97 typedef struct demux_data_mpegaudio_s
103 es_descriptor_t *p_es;
104 mpegaudio_format_t mpeg;
105 mpegaudio_xing_header_t xingheader;
107 } demux_data_mpegaudio_t;
110 static int mpegaudio_bitrate[2][3][16] =
113 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0 }, /* v1 l1 */
114 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0 }, /* v1 l2 */
115 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0 } /* v1 l3 */
119 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0 }, /* v2 l1 */
120 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 }, /* v2 l2 */
121 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 } /* v2 l3 */
126 static int mpegaudio_samplingfreq[2][4] = /* version 1 then 2 */
128 { 44100, 48000, 32000, 0 },
129 { 22050, 24000, 16000, 0 }
132 static char* mpegaudio_mode[4] =
134 "stereo", "joint stereo", "dual channel", "mono"
137 static inline u32 __GetDWBE( byte_t *p_buff )
139 return( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) +
140 ( (*(p_buff+2)) << 8 ) + ( (*(p_buff+3)) ) );
143 static int __CheckPS( input_thread_t *p_input )
146 int i_size = input_Peek( p_input, &p_buff, 8196 );
150 if( !(*p_buff) && !(*(p_buff + 1))
151 && (*(p_buff + 2) == 1 ) && (*(p_buff + 3) >= 0xB9 ) )
153 return( 1 ); /* it could be ps so ...*/
162 #define __GetDWBE( p_buff ) \
163 ( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) + \
164 ( (*(p_buff+2)) << 8 ) + ( (*(p_buff+3)) ) )
166 /*****************************************************************************
167 * MPEGAudio_CheckHeader : Test the validity of the header
168 *****************************************************************************/
169 static int MPEGAudio_CheckHeader( u32 i_header )
171 if( ((( i_header >> 20 )&0x0FFF) != 0x0FFF ) /* header sync */
172 || (((i_header >> 17)&0x03) == 0 ) /* valid layer ?*/
173 || (((i_header >> 12)&0x0F) == 0x0F )
174 || (((i_header >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
175 || (((i_header >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
176 || ((i_header & 0x03) == 0x02 )) /* valid emphasis ? */
178 return( 0 ); /*invalid */
180 return( 1 ); /* valid */
183 /*****************************************************************************
184 * MPEGAudio_ParseHeader : Parse a header ;)
185 *****************************************************************************/
186 static void MPEGAudio_ParseHeader( u32 i_header, mpegaudio_format_t *p_mpeg )
188 p_mpeg->i_header = i_header;
189 p_mpeg->i_version = 1 - ( ( i_header >> 19 ) & 0x01 );
190 p_mpeg->i_layer = 3 - ( ( i_header >> 17 ) & 0x03 );
191 p_mpeg->i_crc = 1 - (( i_header >> 16 ) & 0x01);
192 p_mpeg->i_bitrate = mpegaudio_bitrate[p_mpeg->i_version][p_mpeg->i_layer][(i_header>>12)&0x0F];
193 p_mpeg->i_samplingfreq = mpegaudio_samplingfreq[p_mpeg->i_version][(i_header>>10)&0x03];
194 p_mpeg->i_padding = (( i_header >> 9 ) & 0x01);
195 p_mpeg->i_extension = ( i_header >> 7 ) & 0x01;
196 p_mpeg->i_mode = ( i_header >> 6 ) & 0x03;
197 p_mpeg->i_modeext = ( i_header >> 4 ) & 0x03;
198 p_mpeg->i_copyright = ( i_header >> 3 ) & 0x01;
199 p_mpeg->i_original = ( i_header >> 2 ) & 0x01;
200 p_mpeg->i_emphasis = ( i_header ) & 0x03;
203 /*****************************************************************************
204 * MPEGAudio_FrameSize : give the size of a frame in the mpeg stream
205 *****************************************************************************/
206 static int MPEGAudio_FrameSize( mpegaudio_format_t *p_mpeg )
208 /* XXX if crc do i need to add 2 bytes or not? */
209 switch( p_mpeg->i_layer )
212 return( ( ( ( !p_mpeg->i_version ? 12000 : 6000 ) *
213 p_mpeg->i_bitrate ) /
214 p_mpeg->i_samplingfreq + p_mpeg->i_padding ) * 4);
217 return( ( ( !p_mpeg->i_version ? 144000 : 72000 ) *
218 p_mpeg->i_bitrate ) /
219 p_mpeg->i_samplingfreq + p_mpeg->i_padding );
221 return( 1024 ); /* must never happen, 1k to advance in stream*/
224 /*****************************************************************************
225 * MPEGAudio_DecodedFrameSize : give the length of the decoded pcm data
226 *****************************************************************************/
227 static int MPEGAudio_DecodedFrameSize( mpegaudio_format_t *p_mpeg )
229 switch( p_mpeg->i_layer )
231 case( 0 ): /* layer 1 */
233 case( 1 ): /* layer 2 */
235 case( 2 ): /* layer 3 */
236 return( !p_mpeg->i_version ? 1152 : 576 );
237 /* XXX: perhaps we have to /2 for all layer but i'm not sure */
242 static int MPEGAudio_SkipID3Tag( input_thread_t *p_input )
246 byte_t version, revision;
250 msg_Dbg( p_input, "Checking for ID3 tag" );
251 /* get 10 byte id3 header */
252 if( ( count = input_Peek( p_input, &p_peek, 10 ) ) < 10 )
254 msg_Err( p_input, "cannot peek()" );
258 msg_Info( p_input, "Three first bytes are: %d %d %d",
264 if ( !( (p_peek[0] == 0x49) && (p_peek[1] == 0x44) && (p_peek[2] == 0x33)))
269 version = p_peek[3]; /* These may become usfull later, */
270 revision = p_peek[4]; /* but we ignore them for now */
272 b_footer = p_peek[5] & 0x10;
273 i_size = (p_peek[6] << 21) +
276 p_peek[9]; //Is this safe?
282 msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
283 if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
285 msg_Err( p_input, "cannot peek()" );
289 p_input->p_current_data += i_size; //seek passed end of ID3 tag
294 /*****************************************************************************
295 * MPEGAudio_FindFrame : Find a header that could be valid.
296 *****************************************************************************
297 * The idea is to search for 2 consecutive headers that seem valid
298 * Perhaps we can search 2 header with same version or samplefreq(...) to be
299 * more secure but this seems to be enougth
300 *****************************************************************************/
301 static int MPEGAudio_FindFrame( input_thread_t *p_input,
303 mpegaudio_format_t *p_mpeg,
311 int i_size = input_Peek( p_input, &p_buff, i_posmax+MPEGAUDIO_MAXFRAMESIZE);
313 while( i_pos <= __MIN( i_posmax, i_size - 4) )
315 i_header = __GetDWBE( p_buff );
316 if( MPEGAudio_CheckHeader( i_header ) )
318 MPEGAudio_ParseHeader( i_header, p_mpeg );
319 i_framesize = MPEGAudio_FrameSize( p_mpeg );
320 if( i_pos + i_framesize + 4 > i_size )
327 if( MPEGAudio_CheckHeader( __GetDWBE( p_buff + i_framesize ) ) )
342 /*****************************************************************************
343 * MPEGAudio_ExtractXingHeader : extract a Xing header if exist
344 *****************************************************************************
345 * It also calcul avgbitrate, using Xing header if present or assume that
346 * the bitrate of the first frame is the same for the all file
347 *****************************************************************************/
348 static void MPEGAudio_ExtractXingHeader( input_thread_t *p_input,
349 mpegaudio_xing_header_t *p_xh )
353 mpegaudio_format_t mpeg;
356 p_xh->i_flags = 0; /* nothing present */
357 if( !(MPEGAudio_FindFrame( p_input, &i_pos, &mpeg, 2024 )) )
359 return; /* failed , can't */
361 p_xh->i_avgbitrate = mpeg.i_bitrate * 1000; /* default */
363 /* 1024 is enougth */
364 if( ( i_size = input_Peek( p_input, &p_buff, 1024 + i_pos ) ) < 8 )
370 /* calculate pos of xing header */
371 if( !mpeg.i_version )
373 p_buff += mpeg.i_mode != 3 ? 36 : 21;
377 p_buff += mpeg.i_mode != 3 ? 21 : 13;
380 if( (*p_buff != 'X' )||(*(p_buff+1) != 'i' )
381 ||(*(p_buff+2) != 'n' )||(*(p_buff+3) != 'g' ) )
387 p_xh->i_flags = __GetDWBE( p_buff );
390 if( p_xh->i_flags&FRAMES_FLAG )
392 p_xh->i_frames = __GetDWBE( p_buff );
395 if( p_xh->i_flags&BYTES_FLAG )
397 p_xh->i_bytes = __GetDWBE( p_buff );
400 if( p_xh->i_flags&TOC_FLAG )
402 p_input->p_vlc->pf_memcpy( p_xh->i_toc, p_buff, 100 );
405 if( p_xh->i_flags&VBR_SCALE_FLAG )
407 p_xh->i_vbr_scale = __GetDWBE( p_buff );
410 if( ( p_xh->i_flags&FRAMES_FLAG )&&( p_xh->i_flags&BYTES_FLAG ) )
413 ((u64)p_xh->i_bytes * (u64)8 * (u64)mpeg.i_samplingfreq) /
414 ((u64)p_xh->i_frames * (u64)MPEGAudio_DecodedFrameSize( &mpeg));
419 /*****************************************************************************
420 * Activate: initializes MPEGaudio structures
421 *****************************************************************************/
422 static int Activate( vlc_object_t * p_this )
424 input_thread_t * p_input = (input_thread_t *)p_this;
425 demux_data_mpegaudio_t * p_mpegaudio;
426 mpegaudio_format_t mpeg;
427 es_descriptor_t * p_es;
430 input_info_category_t * p_category;
432 /* Set the demux function */
433 p_input->pf_demux = Demux;
435 /* XXX: i don't know what it's supposed to do, copied from ESInit */
436 /* Initialize access plug-in structures. */
437 if( p_input->i_mtu == 0 )
440 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
442 if( ( *p_input->psz_demux )
443 &&( !strncmp( p_input->psz_demux, "mpegaudio", 10 ) ) )
452 if ( MPEGAudio_SkipID3Tag( p_input ) )
457 /* check if it can be a ps stream */
458 if( __CheckPS( p_input ) && !b_forced )
463 /* must be sure that is mpeg audio stream */
464 if( MPEGAudio_FindFrame( p_input,
467 (b_forced ? 2 * MPEGAUDIO_MAXFRAMESIZE :
468 MPEGAUDIO_MAXTESTPOS) )
469 < (b_forced ? 1 : 2) )
471 msg_Warn( p_input, "MPEGAudio module discarded (no frame found)" );
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_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" );
500 p_es->i_stream_id = 1;
501 p_es->i_fourcc = !mpeg.i_layer ? VLC_FOURCC('m','p','g','a') /* layer 1 */
502 : VLC_FOURCC('m','p','g','a'); /* layer 2 */
503 p_es->i_cat = AUDIO_ES;
504 input_SelectES( p_input, p_es );
506 p_input->stream.p_selected_program->b_is_ok = 1;
507 vlc_mutex_unlock( &p_input->stream.stream_lock );
509 /* create p_mpegaudio and init it */
510 p_input->p_demux_data =
511 p_mpegaudio = malloc( sizeof( demux_data_mpegaudio_t ));
515 msg_Err( p_input, "out of memory" );
519 /*input_ClockInit( p_input->stream.p_selected_program );
520 done by AddProgram */
521 p_mpegaudio->p_es = p_es;
522 p_mpegaudio->mpeg = mpeg;
523 p_mpegaudio->i_framecount = 0;
524 p_mpegaudio->i_pts = 0;
526 /* parse Xing Header if present */
527 MPEGAudio_ExtractXingHeader( p_input, &p_mpegaudio->xingheader );
529 vlc_mutex_lock( &p_input->stream.stream_lock );
530 p_input->stream.i_mux_rate = p_mpegaudio->xingheader.i_avgbitrate / 50 / 8;
531 vlc_mutex_unlock( &p_input->stream.stream_lock );
533 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
534 /* if i don't do that, it don't work correctly but why ??? */
535 if( p_input->stream.b_seekable )
537 p_input->pf_seek( p_input, 0 );
538 input_AccessReinit( p_input );
540 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
543 msg_Dbg( p_input, "audio MPEG-%d layer %d %s %dHz %dKb/s %s",
546 mpegaudio_mode[mpeg.i_mode],
548 p_mpegaudio->xingheader.i_avgbitrate / 1000,
549 p_mpegaudio->xingheader.i_flags ?
553 vlc_mutex_lock( &p_input->stream.stream_lock );
554 p_category = input_InfoCategory( p_input, "mpeg" );
555 input_AddInfo( p_category, "input type", "audio MPEG-%d",
557 input_AddInfo( p_category, "layer", "%d", mpeg.i_layer + 1 );
558 input_AddInfo( p_category, "mode", mpegaudio_mode[mpeg.i_mode] );
559 input_AddInfo( p_category, "sample rate", "%dHz", mpeg.i_samplingfreq );
560 input_AddInfo( p_category, "average bitrate", "%dKb/s",
561 p_mpegaudio->xingheader.i_avgbitrate / 1000 );
562 vlc_mutex_unlock( &p_input->stream.stream_lock );
567 /*****************************************************************************
568 * Demux: reads and demuxes data packets
569 *****************************************************************************
570 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
571 *****************************************************************************/
572 static int Demux( input_thread_t * p_input )
577 mpegaudio_format_t mpeg;
578 demux_data_mpegaudio_t *p_mpegaudio =
579 (demux_data_mpegaudio_t*) p_input->p_demux_data;
580 /* look for a frame */
581 if( !MPEGAudio_FindFrame( p_input, &i_pos, &mpeg, 4096 ) )
583 msg_Warn( p_input, "cannot find next frame" );
587 /* if stream has changed */
588 if( ( mpeg.i_version != p_mpegaudio->mpeg.i_version )
589 ||( mpeg.i_layer != p_mpegaudio->mpeg.i_layer )
590 ||( mpeg.i_samplingfreq != p_mpegaudio->mpeg.i_samplingfreq ) )
592 msg_Dbg( p_input, "stream has changed" );
593 p_mpegaudio->i_framecount = 0;
594 p_mpegaudio->i_pts = 0;
597 input_ClockManageRef( p_input,
598 p_input->stream.p_selected_program,
599 p_mpegaudio->i_pts );
601 /* in fact i_pos may be garbage but ... i don't want to skip it
604 i_toread = MPEGAudio_FrameSize( &mpeg ) + i_pos;
606 if( !(p_pes = input_NewPES( p_input->p_method_data )) )
608 msg_Err( p_input, "cannot allocate new PES" );
612 while( i_toread > 0 )
614 data_packet_t *p_data;
617 if( (i_read = input_SplitBuffer( p_input, &p_data, i_toread ) ) <= 0 )
621 if( !p_pes->p_first )
623 p_pes->p_first = p_data;
624 p_pes->i_nb_data = 1;
625 p_pes->i_pes_size = i_read;
629 p_pes->p_last->p_next = p_data;
631 p_pes->i_pes_size += i_read;
633 p_pes->p_last = p_data;
636 p_mpegaudio->i_pts = (mtime_t)90000 *
637 (mtime_t)p_mpegaudio->i_framecount *
638 (mtime_t)MPEGAudio_DecodedFrameSize( &mpeg ) /
639 (mtime_t)mpeg.i_samplingfreq;
641 p_pes->i_pts = input_ClockGetTS( p_input,
642 p_input->stream.p_selected_program,
643 p_mpegaudio->i_pts );
645 if( !p_mpegaudio->p_es->p_decoder_fifo )
647 msg_Err( p_input, "no audio decoder" );
648 input_DeletePES( p_input->p_method_data, p_pes );
649 return( -1 ); /* perhaps not, it's my choice */
653 input_DecodePES( p_mpegaudio->p_es->p_decoder_fifo, p_pes );
656 p_mpegaudio->i_framecount++;
657 p_mpegaudio->mpeg = mpeg;