]> git.sesse.net Git - vlc/blob - plugins/mpeg_system/mpeg_audio.c
e4b252bc9b559b7e387656ca9ff131c24745c8ab
[vlc] / plugins / mpeg_system / mpeg_audio.c
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 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  * 
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include <sys/types.h>
34
35 /*****************************************************************************
36  * Local prototypes
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 * );
42
43 /* TODO: support MPEG-2.5, not difficult */
44
45 /*****************************************************************************
46  * Build configuration tree.
47  *****************************************************************************/
48 MODULE_CONFIG_START
49 MODULE_CONFIG_STOP
50
51 MODULE_INIT_START
52     SET_DESCRIPTION( "MPEG I/II Audio stream demux" )
53     ADD_CAPABILITY( DEMUX, 110 )
54     ADD_SHORTCUT( "mpegaudio" )
55 MODULE_INIT_STOP
56
57 MODULE_ACTIVATE_START
58     input_getfunctions( &p_module->p_functions->demux );
59 MODULE_ACTIVATE_STOP
60
61 MODULE_DEACTIVATE_START
62 MODULE_DEACTIVATE_STOP
63
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 )
69 {
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;
75 #undef input
76 }
77
78 /*****************************************************************************
79  * Definitions of structures  and functions used by this plugins 
80  *****************************************************************************/
81
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
86
87 #define MPEGAUDIO_MAXFRAMESIZE  1500 /* no exactly */
88
89 typedef struct mpegaudio_format_s
90 {
91     u32 i_header;
92     int i_version;
93     int i_layer;
94     int i_crc;
95     int i_bitrate;
96     int i_samplingfreq;
97     int i_padding;
98     int i_extension;
99     int i_mode;
100     int i_modeext;
101     int i_copyright;
102     int i_original;
103     int i_emphasis;
104
105 } mpegaudio_format_t;
106
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
113 {
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;
121
122 typedef struct demux_data_mpegaudio_s
123 {
124     mtime_t i_pts;
125
126     int     i_framecount;
127    
128     es_descriptor_t         *p_es;
129     mpegaudio_format_t      mpeg;
130     mpegaudio_xing_header_t xingheader;
131
132 } demux_data_mpegaudio_t;
133
134
135 static int mpegaudio_bitrate[2][3][16] =
136 {
137     {
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 */
141     },
142     
143     {
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 */
147     }
148
149 };
150
151 static int mpegaudio_samplingfreq[2][4] = /* version 1 then 2 */
152 {
153     { 44100, 48000, 32000, 0 },
154     { 22050, 24000, 16000, 0 }
155 };
156
157 static char* mpegaudio_mode[4] =
158 {
159     "stereo", "joint stereo", "dual channel", "mono"
160 };
161
162 static inline u32 __GetDWBE( byte_t *p_buff )
163 {
164     return( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) +
165                     ( (*(p_buff+2)) << 8 ) +  ( (*(p_buff+3)) ) );
166 }
167
168 static int __CheckPS( input_thread_t *p_input )
169 {
170     byte_t *p_buff;
171     int i_size = input_Peek( p_input, &p_buff, 8196 );
172
173     while( i_size > 0 )
174     {
175         if( !(*p_buff) && !(*(p_buff + 1)) 
176                 && (*(p_buff + 2) == 1 ) && (*(p_buff + 3) >= 0xB9 ) )
177         {
178             return( 1 );  /* it could be ps so ...*/
179         }
180         p_buff++;
181         i_size--;
182     }
183     return( 0 );
184 }
185
186 /*
187 #define __GetDWBE( p_buff ) \
188     ( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) + \
189       ( (*(p_buff+2)) << 8 ) +  ( (*(p_buff+3)) ) )
190 */
191 /*****************************************************************************
192  * MPEGAudio_CheckHeader : Test the validity of the header 
193  *****************************************************************************/
194 static int MPEGAudio_CheckHeader( u32 i_header )
195 {
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 ? */
202     {
203         return( 0 ); /*invalid */
204     }
205     return( 1 ); /* valid */
206 }
207
208 /*****************************************************************************
209  * MPEGAudio_ParseHeader : Parse a header ;)
210  *****************************************************************************/
211 static void MPEGAudio_ParseHeader( u32 i_header, mpegaudio_format_t *p_mpeg )
212 {
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;
226 }
227
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 )
232 {
233     /* XXX if crc do i need to add 2 bytes or not? */
234     switch( p_mpeg->i_layer )
235     {
236         case( 0 ):
237             return( ( ( ( !p_mpeg->i_version ? 12000 : 6000 ) * 
238                          p_mpeg->i_bitrate ) / 
239                          p_mpeg->i_samplingfreq + p_mpeg->i_padding ) * 4);
240         case( 1 ):
241         case( 2 ):
242             return( ( ( !p_mpeg->i_version ? 144000 : 72000 ) * 
243                          p_mpeg->i_bitrate ) /  
244                          p_mpeg->i_samplingfreq + p_mpeg->i_padding );
245     }
246     return( 1024 ); /* must never happen, 1k to advance in stream*/
247 }
248
249 /*****************************************************************************
250  * MPEGAudio_DecodedFrameSize : give the length of the decoded pcm data
251  *****************************************************************************/
252 static int MPEGAudio_DecodedFrameSize( mpegaudio_format_t *p_mpeg )
253 {
254     switch( p_mpeg->i_layer )
255     {
256         case( 0 ): /* layer 1 */
257             return( 384);
258         case( 1 ): /* layer 2 */
259             return( 1152 );
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 */
263     }
264     return( 0 );
265 }
266
267 static int MPEGAudio_SkipID3Tag( input_thread_t *p_input )
268 {
269     int count;
270     byte_t *p_peek;
271     byte_t version, revision;
272     int b_footer;
273     int i_size;
274
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 )
278     {
279         msg_Err( p_input, "cannot peek()" );
280         return( -1 );
281     }
282 /*
283     msg_Info( p_input, "Three first bytes are: %d %d %d",
284               p_peek[0],
285               p_peek[1],
286               p_peek[2]  
287               );
288 */
289     if ( !( (p_peek[0] == 0x49) && (p_peek[1] == 0x44) && (p_peek[2] == 0x33)))
290     {
291         return( 0 );
292     }
293     
294     version = p_peek[3];  /* These may become usfull later, */
295     revision = p_peek[4]; /* but we ignore them for now */
296
297     b_footer = p_peek[5] & 0x10;
298     i_size = (p_peek[6] << 21) +
299              (p_peek[7] << 14) +
300              (p_peek[8] << 7) +
301              p_peek[9];  //Is this safe?
302     if ( b_footer )
303     {
304         i_size += 10;
305     }
306     i_size += 10;
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 )
309     {
310         msg_Err( p_input, "cannot peek()" );
311         return( -1 );
312     }
313         
314     p_input->p_current_data += i_size; //seek passed end of ID3 tag
315
316     return (0);
317 }
318
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, 
327                                  int *pi_pos, 
328                                  mpegaudio_format_t *p_mpeg,
329                                  int i_posmax )
330 {
331     byte_t *p_buff;
332     u32 i_header;
333     int i_framesize;
334
335     int i_pos = 0;
336     int i_size = input_Peek( p_input, &p_buff, i_posmax+MPEGAUDIO_MAXFRAMESIZE);
337
338     while( i_pos <= __MIN( i_posmax, i_size - 4) )
339     {
340         i_header = __GetDWBE( p_buff );
341         if( MPEGAudio_CheckHeader( i_header ) )
342         {
343             MPEGAudio_ParseHeader( i_header, p_mpeg );
344             i_framesize = MPEGAudio_FrameSize( p_mpeg );
345             if(  i_pos + i_framesize + 4 > i_size )
346             {
347                 *pi_pos = i_pos;
348                 return( 1 );
349             }
350             else
351             {
352                 if( MPEGAudio_CheckHeader( __GetDWBE( p_buff + i_framesize ) ) )
353                 {
354                     *pi_pos = i_pos;
355                     return( 2 );
356                 }
357             }
358         }
359         p_buff++;
360         i_pos++;
361     }
362
363     *pi_pos = 0;
364     return( 0 );
365 }
366
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 )
375 {
376     int i_pos;
377     int i_size;
378     mpegaudio_format_t mpeg;
379     byte_t  *p_buff;
380     
381     p_xh->i_flags = 0;  /* nothing present */
382     if( !(MPEGAudio_FindFrame( p_input, &i_pos, &mpeg, 2024 )) )
383     {
384         return; /* failed , can't */
385     }
386     p_xh->i_avgbitrate = mpeg.i_bitrate * 1000; /* default */
387
388     /* 1024 is enougth */
389     if( ( i_size = input_Peek( p_input, &p_buff, 1024 + i_pos ) ) < 8 )
390     {
391         return;
392     }
393     p_buff += i_pos;
394
395     /* calculate pos of xing header */
396     if( !mpeg.i_version )
397     {
398         p_buff += mpeg.i_mode != 3 ? 36 : 21;
399     }
400     else
401     {
402         p_buff += mpeg.i_mode != 3 ? 21 : 13;
403     }
404     
405     if( (*p_buff != 'X' )||(*(p_buff+1) != 'i' )
406         ||(*(p_buff+2) != 'n' )||(*(p_buff+3) != 'g' ) )
407     {
408         return;
409     }
410     p_buff += 4;
411
412     p_xh->i_flags = __GetDWBE( p_buff ); 
413     p_buff += 4;
414
415     if( p_xh->i_flags&FRAMES_FLAG ) 
416     {
417         p_xh->i_frames = __GetDWBE( p_buff );
418         p_buff += 4;
419     }
420     if( p_xh->i_flags&BYTES_FLAG ) 
421     {
422         p_xh->i_bytes = __GetDWBE( p_buff );
423         p_buff += 4;
424     }
425     if( p_xh->i_flags&TOC_FLAG ) 
426     {
427         p_input->p_vlc->pf_memcpy( p_xh->i_toc, p_buff, 100 );
428         p_buff += 100;
429     }
430     if( p_xh->i_flags&VBR_SCALE_FLAG ) 
431     {
432         p_xh->i_vbr_scale = __GetDWBE( p_buff );
433         p_buff += 4;
434     }
435     if( ( p_xh->i_flags&FRAMES_FLAG )&&( p_xh->i_flags&BYTES_FLAG ) )
436     {
437         p_xh->i_avgbitrate = 
438               ((u64)p_xh->i_bytes * (u64)8 * (u64)mpeg.i_samplingfreq) / 
439                ((u64)p_xh->i_frames * (u64)MPEGAudio_DecodedFrameSize( &mpeg));
440     }
441 }
442                                     
443
444 /*****************************************************************************
445  * MPEGaudioInit : initializes MPEGaudio structures
446  *****************************************************************************/
447 static int MPEGAudioInit( input_thread_t * p_input )
448 {
449     demux_data_mpegaudio_t *p_mpegaudio;
450     mpegaudio_format_t mpeg;
451     es_descriptor_t * p_es;
452     int i_pos;
453     int b_forced;
454     input_info_category_t * p_category;
455
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 )
459     {
460     /* Improve speed. */
461         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
462     }
463     if( ( *p_input->psz_demux )
464         &&( !strncmp( p_input->psz_demux, "mpegaudio", 10 ) ) )
465     {
466         b_forced = 1;
467     }
468     else
469     {
470         b_forced = 0;
471     }
472
473     if ( MPEGAudio_SkipID3Tag( p_input ) )
474     {
475         return -1;
476     }
477     
478     /* check if it can be a ps stream */
479     if( __CheckPS(  p_input ) && !b_forced )
480     {
481         return( -1 );
482     }
483
484     /* must be sure that is mpeg audio stream */
485     if( MPEGAudio_FindFrame( p_input, 
486                              &i_pos, 
487                              &mpeg, 
488                              (b_forced ? 2 * MPEGAUDIO_MAXFRAMESIZE : 
489                                              MPEGAUDIO_MAXTESTPOS) ) 
490                     < (b_forced ? 1 : 2)  )
491     {
492         msg_Warn( p_input, "MPEGAudio module discarded (no frame found)" );
493         return( -1 );
494     }
495     
496     vlc_mutex_lock( &p_input->stream.stream_lock );
497     if( input_InitStream( p_input, 0 ) == -1)
498     {
499         msg_Err( p_input, "cannot init stream" );
500         return( -1 );
501     }    
502     if( input_AddProgram( p_input, 0, 0) == NULL )
503     {
504         msg_Err( p_input, "cannot add program" );
505         return( -1 );
506     }
507     p_input->stream.pp_programs[0]->b_is_ok = 0;
508     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
509     
510     /* create our ES */ 
511     p_es = input_AddES( p_input, 
512                         p_input->stream.p_selected_program, 
513                         1, /* id */
514                         0 );
515     if( !p_es )
516     {
517         vlc_mutex_unlock( &p_input->stream.stream_lock );
518         msg_Err( p_input, "out of memory" );
519         return( -1 );
520     }
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 );
526
527     p_input->stream.p_selected_program->b_is_ok = 1;
528     vlc_mutex_unlock( &p_input->stream.stream_lock );
529
530     /* create p_mpegaudio and init it */
531     p_input->p_demux_data =
532            p_mpegaudio = malloc( sizeof( demux_data_mpegaudio_t ));
533
534     if( !p_mpegaudio )
535     {
536         msg_Err( p_input, "out of memory" );
537         return( -1 );
538     }
539
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;  
546
547     /* parse Xing Header if present */
548     MPEGAudio_ExtractXingHeader( p_input, &p_mpegaudio->xingheader );
549     
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 );
553
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 )
557     {
558         p_input->pf_seek( p_input, 0 );
559         input_AccessReinit( p_input );
560     }
561     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
562
563     /* all is ok :)) */
564     msg_Dbg( p_input, "audio MPEG-%d layer %d %s %dHz %dKb/s %s",
565                 mpeg.i_version + 1,
566                 mpeg.i_layer + 1,
567                 mpegaudio_mode[mpeg.i_mode],
568                 mpeg.i_samplingfreq,
569                 p_mpegaudio->xingheader.i_avgbitrate / 1000,
570                 p_mpegaudio->xingheader.i_flags ?
571                         "VBR (Xing)" : "" 
572                     );
573
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",
577                    mpeg.i_version +1 );
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 );
584     
585     return( 0 );
586 }
587
588
589 /*****************************************************************************
590  * MPEGAudioEnd: frees unused data
591  *****************************************************************************/
592 static void MPEGAudioEnd( input_thread_t * p_input )
593 {
594     
595 }
596
597
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 )
604 {
605     int i_pos;
606     int i_toread;
607     pes_packet_t    *p_pes;
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 ) )
613     {
614         msg_Warn( p_input, "cannot find next frame" );
615         return( 0 );
616     }
617     
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 ) )
622     {
623         msg_Dbg( p_input, "stream has changed" );
624         p_mpegaudio->i_framecount = 0;
625         p_mpegaudio->i_pts = 0;
626     }
627
628     input_ClockManageRef( p_input,
629                           p_input->stream.p_selected_program,
630                           p_mpegaudio->i_pts );
631
632     /* in fact i_pos may be garbage but ... i don't want to skip it 
633         it's borring ;) */
634
635     i_toread = MPEGAudio_FrameSize( &mpeg ) + i_pos;
636     /* create one pes */
637     if( !(p_pes = input_NewPES( p_input->p_method_data )) )
638     {
639         msg_Err( p_input, "cannot allocate new PES" );
640         return( -1 );
641     }
642
643     while( i_toread > 0 )
644     {
645         data_packet_t   *p_data;
646         int i_read;
647
648         if( (i_read = input_SplitBuffer( p_input, &p_data, i_toread ) ) <= 0 )
649         {
650             break;
651         }
652         if( !p_pes->p_first )
653         {
654             p_pes->p_first = p_data;
655             p_pes->i_nb_data = 1;
656             p_pes->i_pes_size = i_read;
657         }
658         else
659         {
660             p_pes->p_last->p_next  = p_data;
661             p_pes->i_nb_data++;
662             p_pes->i_pes_size += i_read;
663         }
664         p_pes->p_last  = p_data;
665         i_toread -= i_read;
666     }
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;
671     p_pes->i_dts = 0;
672     p_pes->i_pts = input_ClockGetTS( p_input,
673                                      p_input->stream.p_selected_program,
674                                      p_mpegaudio->i_pts );
675
676     if( !p_mpegaudio->p_es->p_decoder_fifo )
677     {
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 */
681     }
682     else
683     {
684         input_DecodePES( p_mpegaudio->p_es->p_decoder_fifo, p_pes );
685     }
686
687     p_mpegaudio->i_framecount++;
688     p_mpegaudio->mpeg = mpeg; 
689
690     return( 1 );
691 }
692
693