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.7 2002/05/17 23:01:02 fenrir Exp $
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <videolan/vlc.h>
31 #include <sys/types.h>
32 #include "stream_control.h"
33 #include "input_ext-intf.h"
34 #include "input_ext-dec.h"
35 #include "input_ext-plugins.h"
37 /*****************************************************************************
39 *****************************************************************************/
40 static void input_getfunctions( function_list_t * p_function_list );
41 static int MPEGAudioDemux ( struct input_thread_s * );
42 static int MPEGAudioInit ( struct input_thread_s * );
43 static void MPEGAudioEnd ( struct input_thread_s * );
45 /* TODO: support MPEG-2.5, not difficult */
47 /*****************************************************************************
48 * Build configuration tree.
49 *****************************************************************************/
54 SET_DESCRIPTION( "MPEG I/II Audio stream demux" )
55 ADD_CAPABILITY( DEMUX, 110 )
56 ADD_SHORTCUT( "mpegaudio" )
60 input_getfunctions( &p_module->p_functions->demux );
63 MODULE_DEACTIVATE_START
64 MODULE_DEACTIVATE_STOP
66 /*****************************************************************************
67 * Functions exported as capabilities. They are declared as static so that
68 * we don't pollute the namespace too much.
69 *****************************************************************************/
70 static void input_getfunctions( function_list_t * p_function_list )
72 #define input p_function_list->functions.demux
73 input.pf_init = MPEGAudioInit;
74 input.pf_end = MPEGAudioEnd;
75 input.pf_demux = MPEGAudioDemux;
76 input.pf_rewind = NULL;
80 /*****************************************************************************
81 * Definitions of structures and functions used by this plugins
82 *****************************************************************************/
84 /* XXX set this to 0 to avoid problem with PS XXX */
85 /* but with some file or web radio will failed to detect */
86 /* it's you to choose */
87 #define MPEGAUDIO_MAXTESTPOS 0
89 #define MPEGAUDIO_MAXFRAMESIZE 1500 /* no exactly */
91 typedef struct mpegaudio_format_s
107 } mpegaudio_format_t;
109 /* Xing Header if present */
110 #define FRAMES_FLAG 0x0001 /* these flags is for i_flags */
111 #define BYTES_FLAG 0x0002 /* because all is optionnal */
112 #define TOC_FLAG 0x0004
113 #define VBR_SCALE_FLAG 0x0008
114 typedef struct mpegaudio_xing_header_s
116 int i_flags; /* from Xing header data */
117 int i_frames; /* total bit stream frames from Xing header data */
118 int i_bytes; /* total bit stream bytes from Xing header data */
119 int i_vbr_scale; /* encoded vbr scale from Xing header data */
120 u8 i_toc[100]; /* for seek */
121 int i_avgbitrate; /* calculated, XXX: bits/sec not Kb */
122 } mpegaudio_xing_header_t;
124 typedef struct demux_data_mpegaudio_s
130 es_descriptor_t *p_es;
131 mpegaudio_format_t mpeg;
132 mpegaudio_xing_header_t xingheader;
134 } demux_data_mpegaudio_t;
137 static int mpegaudio_bitrate[2][3][16] =
140 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0 }, /* v1 l1 */
141 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0 }, /* v1 l2 */
142 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0 } /* v1 l3 */
146 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0 }, /* v2 l1 */
147 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 }, /* v2 l2 */
148 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 } /* v2 l3 */
153 static int mpegaudio_samplingfreq[2][4] = /* version 1 then 2 */
155 { 44100, 48000, 32000, 0 },
156 { 22050, 24000, 16000, 0 }
159 static char* mpegaudio_mode[4] =
161 "stereo", "joint stereo", "dual channel", "mono"
164 static __inline__ u32 __GetDWBE( byte_t *p_buff )
166 return( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) +
167 ( (*(p_buff+2)) << 8 ) + ( (*(p_buff+3)) ) );
170 static int __CheckPS( input_thread_t *p_input )
173 int i_size = input_Peek( p_input, &p_buff, 8196 );
177 if( !(*p_buff) && !(*(p_buff + 1))
178 && (*(p_buff + 2) == 1 ) && (*(p_buff + 3) >= 0xB9 ) )
180 return( 1 ); /* it could be ps so ...*/
189 #define __GetDWBE( p_buff ) \
190 ( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) + \
191 ( (*(p_buff+2)) << 8 ) + ( (*(p_buff+3)) ) )
193 /*****************************************************************************
194 * MPEGAudio_CheckHeader : Test the validity of the header
195 *****************************************************************************/
196 static int MPEGAudio_CheckHeader( u32 i_header )
198 if( ((( i_header >> 20 )&0x0FFF) != 0x0FFF ) /* header sync */
199 || (((i_header >> 17)&0x03) == 0 ) /* valid layer ?*/
200 || (((i_header >> 12)&0x0F) == 0x0F )
201 || (((i_header >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
202 || (((i_header >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
203 || ((i_header & 0x03) == 0x02 )) /* valid emphasis ? */
205 return( 0 ); /*invalid */
207 return( 1 ); /* valid */
210 /*****************************************************************************
211 * MPEGAudio_ParseHeader : Parse a header ;)
212 *****************************************************************************/
213 static void MPEGAudio_ParseHeader( u32 i_header, mpegaudio_format_t *p_mpeg )
215 p_mpeg->i_header = i_header;
216 p_mpeg->i_version = 1 - ( ( i_header >> 19 ) & 0x01 );
217 p_mpeg->i_layer = 3 - ( ( i_header >> 17 ) & 0x03 );
218 p_mpeg->i_crc = 1 - (( i_header >> 16 ) & 0x01);
219 p_mpeg->i_bitrate = mpegaudio_bitrate[p_mpeg->i_version][p_mpeg->i_layer][(i_header>>12)&0x0F];
220 p_mpeg->i_samplingfreq = mpegaudio_samplingfreq[p_mpeg->i_version][(i_header>>10)&0x03];
221 p_mpeg->i_padding = (( i_header >> 9 ) & 0x01);
222 p_mpeg->i_extension = ( i_header >> 7 ) & 0x01;
223 p_mpeg->i_mode = ( i_header >> 6 ) & 0x03;
224 p_mpeg->i_modeext = ( i_header >> 4 ) & 0x03;
225 p_mpeg->i_copyright = ( i_header >> 3 ) & 0x01;
226 p_mpeg->i_original = ( i_header >> 2 ) & 0x01;
227 p_mpeg->i_emphasis = ( i_header ) & 0x03;
230 /*****************************************************************************
231 * MPEGAudio_FrameSize : give the size of a frame in the mpeg stream
232 *****************************************************************************/
233 static int MPEGAudio_FrameSize( mpegaudio_format_t *p_mpeg )
235 /* XXX if crc do i need to add 2 bytes or not? */
236 switch( p_mpeg->i_layer )
239 return( ( ( ( !p_mpeg->i_version ? 12000 : 6000 ) *
240 p_mpeg->i_bitrate ) /
241 p_mpeg->i_samplingfreq + p_mpeg->i_padding ) * 4);
244 return( ( ( !p_mpeg->i_version ? 144000 : 72000 ) *
245 p_mpeg->i_bitrate ) /
246 p_mpeg->i_samplingfreq + p_mpeg->i_padding );
248 return( 1024 ); /* must never happen, 1k to advance in stream*/
251 /*****************************************************************************
252 * MPEGAudio_DecodedFrameSize : give the length of the decoded pcm data
253 *****************************************************************************/
254 static int MPEGAudio_DecodedFrameSize( mpegaudio_format_t *p_mpeg )
256 switch( p_mpeg->i_layer )
258 case( 0 ): /* layer 1 */
260 case( 1 ): /* layer 2 */
262 case( 2 ): /* layer 3 */
263 return( !p_mpeg->i_version ? 1152 : 576 );
264 /* XXX: perhaps we have to /2 for all layer but i'm not sure */
269 /*****************************************************************************
270 * MPEGAudio_FindFrame : Find a header that could be valid.
271 *****************************************************************************
272 * The idea is to search for 2 consecutive headers that seem valid
273 * Perhaps we can search 2 header with same version or samplefreq(...) to be
274 * more secure but this seems to be enougth
275 *****************************************************************************/
276 static int MPEGAudio_FindFrame( input_thread_t *p_input,
278 mpegaudio_format_t *p_mpeg,
286 int i_size = input_Peek( p_input, &p_buff, i_posmax+MPEGAUDIO_MAXFRAMESIZE);
288 while( i_pos <= __MIN( i_posmax, i_size - 4) )
290 i_header = __GetDWBE( p_buff );
291 if( MPEGAudio_CheckHeader( i_header ) )
293 MPEGAudio_ParseHeader( i_header, p_mpeg );
294 i_framesize = MPEGAudio_FrameSize( p_mpeg );
295 if( i_pos + i_framesize + 4 > i_size )
302 if( MPEGAudio_CheckHeader( __GetDWBE( p_buff + i_framesize ) ) )
317 /*****************************************************************************
318 * MPEGAudio_ExtractXingHeader : extract a Xing header if exist
319 *****************************************************************************
320 * It also calcul avgbitrate, using Xing header if present or assume that
321 * the bitrate of the first frame is the same for the all file
322 *****************************************************************************/
323 static void MPEGAudio_ExtractXingHeader( input_thread_t *p_input,
324 mpegaudio_xing_header_t *p_xh )
328 mpegaudio_format_t mpeg;
331 p_xh->i_flags = 0; /* nothing present */
332 if( !(MPEGAudio_FindFrame( p_input, &i_pos, &mpeg, 2024 )) )
334 return; /* failed , can't */
336 p_xh->i_avgbitrate = mpeg.i_bitrate * 1000; /* default */
338 /* 1024 is enougth */
339 if( ( i_size = input_Peek( p_input, &p_buff, 1024 + i_pos ) ) < 8 )
345 /* calculate pos of xing header */
346 if( !mpeg.i_version )
348 p_buff += mpeg.i_mode != 3 ? 36 : 21;
352 p_buff += mpeg.i_mode != 3 ? 21 : 13;
355 if( (*p_buff != 'X' )||(*(p_buff+1) != 'i' )
356 ||(*(p_buff+2) != 'n' )||(*(p_buff+3) != 'g' ) )
362 p_xh->i_flags = __GetDWBE( p_buff );
365 if( p_xh->i_flags&FRAMES_FLAG )
367 p_xh->i_frames = __GetDWBE( p_buff );
370 if( p_xh->i_flags&BYTES_FLAG )
372 p_xh->i_bytes = __GetDWBE( p_buff );
375 if( p_xh->i_flags&TOC_FLAG )
377 FAST_MEMCPY( p_xh->i_toc, p_buff, 100 );
380 if( p_xh->i_flags&VBR_SCALE_FLAG )
382 p_xh->i_vbr_scale = __GetDWBE( p_buff );
385 if( ( p_xh->i_flags&FRAMES_FLAG )&&( p_xh->i_flags&BYTES_FLAG ) )
388 ((u64)p_xh->i_bytes * (u64)8 * (u64)mpeg.i_samplingfreq) /
389 ((u64)p_xh->i_frames * (u64)MPEGAudio_DecodedFrameSize( &mpeg));
394 /*****************************************************************************
395 * MPEGaudioInit : initializes MPEGaudio structures
396 *****************************************************************************/
397 static int MPEGAudioInit( input_thread_t * p_input )
399 demux_data_mpegaudio_t *p_mpegaudio;
400 mpegaudio_format_t mpeg;
401 es_descriptor_t * p_es;
405 /* XXX: i don't know what it's supposed to do, copied from ESInit */
406 /* Initialize access plug-in structures. */
407 if( p_input->i_mtu == 0 )
410 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
412 if( ( *p_input->psz_demux )
413 &&( !strncmp( p_input->psz_demux, "mpegaudio", 10 ) ) )
422 /* check if it can be a ps stream */
423 if( __CheckPS( p_input ) && !b_forced )
427 /* must be sure that is mpeg audio stream */
428 if( MPEGAudio_FindFrame( p_input,
431 (b_forced ? 2 * MPEGAUDIO_MAXFRAMESIZE :
432 MPEGAUDIO_MAXTESTPOS) )
433 < (b_forced ? 1 : 2) )
435 intf_WarnMsg( 2,"input: MPEGAudio plug-in discarded" );
439 vlc_mutex_lock( &p_input->stream.stream_lock );
440 if( input_InitStream( p_input, 0 ) == -1)
442 intf_ErrMsg( "input error: cannot init stream" );
445 if( input_AddProgram( p_input, 0, 0) == NULL )
447 intf_ErrMsg( "input error: cannot add program" );
450 p_input->stream.pp_programs[0]->b_is_ok = 0;
451 p_input->stream.p_selected_program =
452 p_input->stream.p_new_program = p_input->stream.pp_programs[0];
455 p_es = input_AddES( p_input,
456 p_input->stream.p_selected_program,
461 vlc_mutex_unlock( &p_input->stream.stream_lock );
462 intf_ErrMsg( "input error: not enough memory." );
465 p_es->i_stream_id = 1;
466 p_es->i_type = !mpeg.i_layer ? MPEG1_AUDIO_ES : MPEG2_AUDIO_ES;
467 p_es->i_cat = AUDIO_ES;
469 input_SelectES( p_input, p_es );
471 p_input->stream.p_selected_program->b_is_ok = 1;
472 vlc_mutex_unlock( &p_input->stream.stream_lock );
474 /* create p_mpegaudio and init it */
475 p_input->p_demux_data =
476 p_mpegaudio = malloc( sizeof( demux_data_mpegaudio_t ));
480 intf_ErrMsg( "input error: not enough memory." );
484 /*input_ClockInit( p_input->stream.p_selected_program );
485 done by AddProgram */
486 p_mpegaudio->p_es = p_es;
487 p_mpegaudio->mpeg = mpeg;
488 p_mpegaudio->i_framecount = 0;
489 p_mpegaudio->i_pts = 0;
491 /* parse Xing Header if present */
492 MPEGAudio_ExtractXingHeader( p_input, &p_mpegaudio->xingheader );
494 vlc_mutex_lock( &p_input->stream.stream_lock );
495 p_input->stream.i_mux_rate = p_mpegaudio->xingheader.i_avgbitrate / 50 / 8;
496 vlc_mutex_unlock( &p_input->stream.stream_lock );
498 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
499 /* if i don't do that, it don't work correctly but why ??? */
500 if( p_input->stream.b_seekable )
502 p_input->pf_seek( p_input, 0 );
503 input_AccessReinit( p_input );
505 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
508 intf_Msg( "input init: Audio MPEG-%d layer %d %s %dHz %dKb/s %s",
511 mpegaudio_mode[mpeg.i_mode],
513 p_mpegaudio->xingheader.i_avgbitrate / 1000,
514 p_mpegaudio->xingheader.i_flags ?
521 /*****************************************************************************
522 * MPEGAudioEnd: frees unused data
523 *****************************************************************************/
524 static void MPEGAudioEnd( input_thread_t * p_input )
530 /*****************************************************************************
531 * MPEGAudioDemux: reads and demuxes data packets
532 *****************************************************************************
533 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
534 *****************************************************************************/
535 static int MPEGAudioDemux( input_thread_t * p_input )
540 mpegaudio_format_t mpeg;
541 demux_data_mpegaudio_t *p_mpegaudio =
542 (demux_data_mpegaudio_t*) p_input->p_demux_data;
544 /* look for a frame */
545 if( !MPEGAudio_FindFrame( p_input, &i_pos, &mpeg, 4096 ) )
547 intf_WarnMsg( 1, "input error: cannot find next frame");
551 /* if stream has changed */
552 if( ( mpeg.i_version != p_mpegaudio->mpeg.i_version )
553 ||( mpeg.i_layer != p_mpegaudio->mpeg.i_layer )
554 ||( mpeg.i_samplingfreq != p_mpegaudio->mpeg.i_samplingfreq ) )
556 intf_WarnMsg( 1, "input demux: stream has changed" );
557 p_mpegaudio->i_framecount = 0;
558 p_mpegaudio->i_pts = 0;
561 input_ClockManageRef( p_input,
562 p_input->stream.p_selected_program,
563 p_mpegaudio->i_pts );
565 /* in fact i_pos may be garbage but ... i don't want to skip it
568 i_toread = MPEGAudio_FrameSize( &mpeg ) + i_pos;
570 if( !(p_pes = input_NewPES( p_input->p_method_data )) )
572 intf_ErrMsg( "input demux: out of memory" );
576 while( i_toread > 0 )
578 data_packet_t *p_data;
581 if( (i_read = input_SplitBuffer( p_input, &p_data, i_toread ) ) <= 0 )
585 if( !p_pes->p_first )
587 p_pes->p_first = p_data;
588 p_pes->i_nb_data = 1;
589 p_pes->i_pes_size = i_read;
593 p_pes->p_last->p_next = p_data;
595 p_pes->i_pes_size += i_read;
597 p_pes->p_last = p_data;
600 p_mpegaudio->i_pts = (mtime_t)90000 *
601 (mtime_t)p_mpegaudio->i_framecount *
602 (mtime_t)MPEGAudio_DecodedFrameSize( &mpeg ) /
603 (mtime_t)mpeg.i_samplingfreq;
605 p_pes->i_pts = input_ClockGetTS( p_input,
606 p_input->stream.p_selected_program,
607 p_mpegaudio->i_pts );
609 if( !p_mpegaudio->p_es->p_decoder_fifo )
611 intf_ErrMsg( "input demux: no audio decoder" );
612 input_DeletePES( p_input->p_method_data, p_pes );
613 return( -1 ); /* perhaps not, it's my choice */
617 input_DecodePES( p_mpegaudio->p_es->p_decoder_fifo, p_pes );
620 p_mpegaudio->i_framecount++;
621 p_mpegaudio->mpeg = mpeg;