]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/audio.c
4240897d97ebc8ad2bf5b8543321ddc2b25d43ed
[vlc] / modules / demux / mpeg / audio.c
1 /*****************************************************************************
2  * mpeg_audio.c : mpeg_audio Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: audio.c,v 1.2 2002/08/07 00:29:36 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 int  Activate ( vlc_object_t * );
39 static int  Demux ( input_thread_t * );
40
41 /* TODO: support MPEG-2.5, not difficult */
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 vlc_module_begin();
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" );
51 vlc_module_end();
52
53 /*****************************************************************************
54  * Definitions of structures  and functions used by this plugins 
55  *****************************************************************************/
56
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
61
62 #define MPEGAUDIO_MAXFRAMESIZE  1500 /* no exactly */
63
64 typedef struct mpegaudio_format_s
65 {
66     u32 i_header;
67     int i_version;
68     int i_layer;
69     int i_crc;
70     int i_bitrate;
71     int i_samplingfreq;
72     int i_padding;
73     int i_extension;
74     int i_mode;
75     int i_modeext;
76     int i_copyright;
77     int i_original;
78     int i_emphasis;
79
80 } mpegaudio_format_t;
81
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
88 {
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;
96
97 struct demux_sys_t
98 {
99     mtime_t i_pts;
100
101     int     i_framecount;
102    
103     es_descriptor_t         *p_es;
104     mpegaudio_format_t      mpeg;
105     mpegaudio_xing_header_t xingheader;
106 };
107
108
109 static int mpegaudio_bitrate[2][3][16] =
110 {
111     {
112         { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0 }, /* v1 l1 */
113         { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384, 0 }, /* v1 l2 */
114         { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 0 }  /* v1 l3 */
115     },
116     
117     {
118         { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256, 0 }, /* v2 l1 */
119         { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0 }, /* v2 l2 */
120         { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0 }  /* v2 l3 */
121     }
122
123 };
124
125 static int mpegaudio_samplingfreq[2][4] = /* version 1 then 2 */
126 {
127     { 44100, 48000, 32000, 0 },
128     { 22050, 24000, 16000, 0 }
129 };
130
131 static char* mpegaudio_mode[4] =
132 {
133     "stereo", "joint stereo", "dual channel", "mono"
134 };
135
136 static inline u32 __GetDWBE( byte_t *p_buff )
137 {
138     return( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) +
139                     ( (*(p_buff+2)) << 8 ) +  ( (*(p_buff+3)) ) );
140 }
141
142 static int __CheckPS( input_thread_t *p_input )
143 {
144     byte_t *p_buff;
145     int i_size = input_Peek( p_input, &p_buff, 8196 );
146
147     while( i_size > 0 )
148     {
149         if( !(*p_buff) && !(*(p_buff + 1)) 
150                 && (*(p_buff + 2) == 1 ) && (*(p_buff + 3) >= 0xB9 ) )
151         {
152             return( 1 );  /* it could be ps so ...*/
153         }
154         p_buff++;
155         i_size--;
156     }
157     return( 0 );
158 }
159
160 /*
161 #define __GetDWBE( p_buff ) \
162     ( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) + \
163       ( (*(p_buff+2)) << 8 ) +  ( (*(p_buff+3)) ) )
164 */
165 /*****************************************************************************
166  * MPEGAudio_CheckHeader : Test the validity of the header 
167  *****************************************************************************/
168 static int MPEGAudio_CheckHeader( u32 i_header )
169 {
170     if( ((( i_header >> 20 )&0x0FFF) != 0x0FFF )  /* header sync */
171         || (((i_header >> 17)&0x03) == 0 )  /* valid layer ?*/
172         || (((i_header >> 12)&0x0F) == 0x0F )
173         || (((i_header >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
174         || (((i_header >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
175         || ((i_header & 0x03) == 0x02 )) /* valid emphasis ? */
176     {
177         return( 0 ); /*invalid */
178     }
179     return( 1 ); /* valid */
180 }
181
182 /*****************************************************************************
183  * MPEGAudio_ParseHeader : Parse a header ;)
184  *****************************************************************************/
185 static void MPEGAudio_ParseHeader( u32 i_header, mpegaudio_format_t *p_mpeg )
186 {
187     p_mpeg->i_header = i_header;
188     p_mpeg->i_version =  1 - ( ( i_header >> 19 ) & 0x01 );
189     p_mpeg->i_layer =  3 - ( ( i_header >> 17 ) & 0x03 );
190     p_mpeg->i_crc = 1 - (( i_header >> 16 ) & 0x01);
191     p_mpeg->i_bitrate = mpegaudio_bitrate[p_mpeg->i_version][p_mpeg->i_layer][(i_header>>12)&0x0F];
192     p_mpeg->i_samplingfreq = mpegaudio_samplingfreq[p_mpeg->i_version][(i_header>>10)&0x03];
193     p_mpeg->i_padding = (( i_header >> 9 ) & 0x01);
194     p_mpeg->i_extension = ( i_header >> 7 ) & 0x01;
195     p_mpeg->i_mode = ( i_header >> 6 ) & 0x03;
196     p_mpeg->i_modeext = ( i_header >> 4 ) & 0x03;
197     p_mpeg->i_copyright = ( i_header >> 3 ) & 0x01;
198     p_mpeg->i_original = ( i_header >> 2 ) & 0x01;
199     p_mpeg->i_emphasis = ( i_header ) & 0x03;
200 }
201
202 /*****************************************************************************
203  * MPEGAudio_FrameSize : give the size of a frame in the mpeg stream
204  *****************************************************************************/
205 static int MPEGAudio_FrameSize( mpegaudio_format_t *p_mpeg )
206 {
207     /* XXX if crc do i need to add 2 bytes or not? */
208     switch( p_mpeg->i_layer )
209     {
210         case( 0 ):
211             return( ( ( ( !p_mpeg->i_version ? 12000 : 6000 ) * 
212                          p_mpeg->i_bitrate ) / 
213                          p_mpeg->i_samplingfreq + p_mpeg->i_padding ) * 4);
214         case( 1 ):
215         case( 2 ):
216             return( ( ( !p_mpeg->i_version ? 144000 : 72000 ) * 
217                          p_mpeg->i_bitrate ) /  
218                          p_mpeg->i_samplingfreq + p_mpeg->i_padding );
219     }
220     return( 1024 ); /* must never happen, 1k to advance in stream*/
221 }
222
223 /*****************************************************************************
224  * MPEGAudio_DecodedFrameSize : give the length of the decoded pcm data
225  *****************************************************************************/
226 static int MPEGAudio_DecodedFrameSize( mpegaudio_format_t *p_mpeg )
227 {
228     switch( p_mpeg->i_layer )
229     {
230         case( 0 ): /* layer 1 */
231             return( 384);
232         case( 1 ): /* layer 2 */
233             return( 1152 );
234         case( 2 ): /* layer 3 */
235             return( !p_mpeg->i_version ? 1152 : 576 ); 
236             /* XXX: perhaps we have to /2 for all layer but i'm not sure */
237     }
238     return( 0 );
239 }
240
241 static int MPEGAudio_SkipID3Tag( input_thread_t *p_input )
242 {
243     int count;
244     byte_t *p_peek;
245     byte_t version, revision;
246     int b_footer;
247     int i_size;
248
249     msg_Dbg( p_input, "Checking for ID3 tag" );
250     /* get 10 byte id3 header */    
251     if( ( count = input_Peek( p_input, &p_peek, 10 ) ) < 10 )
252     {
253         msg_Err( p_input, "cannot peek()" );
254         return( -1 );
255     }
256 /*
257     msg_Info( p_input, "Three first bytes are: %d %d %d",
258               p_peek[0],
259               p_peek[1],
260               p_peek[2]  
261               );
262 */
263     if ( !( (p_peek[0] == 0x49) && (p_peek[1] == 0x44) && (p_peek[2] == 0x33)))
264     {
265         return( 0 );
266     }
267     
268     version = p_peek[3];  /* These may become usfull later, */
269     revision = p_peek[4]; /* but we ignore them for now */
270
271     b_footer = p_peek[5] & 0x10;
272     i_size = (p_peek[6] << 21) +
273              (p_peek[7] << 14) +
274              (p_peek[8] << 7) +
275              p_peek[9];  //Is this safe?
276     if ( b_footer )
277     {
278         i_size += 10;
279     }
280     i_size += 10;
281     msg_Dbg( p_input, "ID3 tag found, skiping %d bytes", i_size );
282     if ( input_Peek( p_input, &p_peek, i_size ) < i_size )
283     {
284         msg_Err( p_input, "cannot peek()" );
285         return( -1 );
286     }
287         
288     p_input->p_current_data += i_size; //seek passed end of ID3 tag
289
290     return (0);
291 }
292
293 /*****************************************************************************
294  * MPEGAudio_FindFrame : Find a header that could be valid. 
295  *****************************************************************************
296  * The idea is to search for 2 consecutive headers that seem valid 
297  * Perhaps we can search 2 header with same version or samplefreq(...) to be
298  * more secure but this seems to be enougth
299  *****************************************************************************/
300 static int MPEGAudio_FindFrame( input_thread_t *p_input, 
301                                  int *pi_pos, 
302                                  mpegaudio_format_t *p_mpeg,
303                                  int i_posmax )
304 {
305     byte_t *p_buff;
306     u32 i_header;
307     int i_framesize;
308
309     int i_pos = 0;
310     int i_size = input_Peek( p_input, &p_buff, i_posmax+MPEGAUDIO_MAXFRAMESIZE);
311
312     while( i_pos <= __MIN( i_posmax, i_size - 4) )
313     {
314         i_header = __GetDWBE( p_buff );
315         if( MPEGAudio_CheckHeader( i_header ) )
316         {
317             MPEGAudio_ParseHeader( i_header, p_mpeg );
318             i_framesize = MPEGAudio_FrameSize( p_mpeg );
319             if(  i_pos + i_framesize + 4 > i_size )
320             {
321                 *pi_pos = i_pos;
322                 return( 1 );
323             }
324             else
325             {
326                 if( MPEGAudio_CheckHeader( __GetDWBE( p_buff + i_framesize ) ) )
327                 {
328                     *pi_pos = i_pos;
329                     return( 2 );
330                 }
331             }
332         }
333         p_buff++;
334         i_pos++;
335     }
336
337     *pi_pos = 0;
338     return( 0 );
339 }
340
341 /*****************************************************************************
342  * MPEGAudio_ExtractXingHeader : extract a Xing header if exist
343  *****************************************************************************
344  * It also calcul avgbitrate, using Xing header if present or assume that
345  * the bitrate of the first frame is the same for the all file
346  *****************************************************************************/
347 static void MPEGAudio_ExtractXingHeader( input_thread_t *p_input,
348                                     mpegaudio_xing_header_t *p_xh )
349 {
350     int i_pos;
351     int i_size;
352     mpegaudio_format_t mpeg;
353     byte_t  *p_buff;
354     
355     p_xh->i_flags = 0;  /* nothing present */
356     if( !(MPEGAudio_FindFrame( p_input, &i_pos, &mpeg, 2024 )) )
357     {
358         return; /* failed , can't */
359     }
360     p_xh->i_avgbitrate = mpeg.i_bitrate * 1000; /* default */
361
362     /* 1024 is enougth */
363     if( ( i_size = input_Peek( p_input, &p_buff, 1024 + i_pos ) ) < 8 )
364     {
365         return;
366     }
367     p_buff += i_pos;
368
369     /* calculate pos of xing header */
370     if( !mpeg.i_version )
371     {
372         p_buff += mpeg.i_mode != 3 ? 36 : 21;
373     }
374     else
375     {
376         p_buff += mpeg.i_mode != 3 ? 21 : 13;
377     }
378     
379     if( (*p_buff != 'X' )||(*(p_buff+1) != 'i' )
380         ||(*(p_buff+2) != 'n' )||(*(p_buff+3) != 'g' ) )
381     {
382         return;
383     }
384     p_buff += 4;
385
386     p_xh->i_flags = __GetDWBE( p_buff ); 
387     p_buff += 4;
388
389     if( p_xh->i_flags&FRAMES_FLAG ) 
390     {
391         p_xh->i_frames = __GetDWBE( p_buff );
392         p_buff += 4;
393     }
394     if( p_xh->i_flags&BYTES_FLAG ) 
395     {
396         p_xh->i_bytes = __GetDWBE( p_buff );
397         p_buff += 4;
398     }
399     if( p_xh->i_flags&TOC_FLAG ) 
400     {
401         p_input->p_vlc->pf_memcpy( p_xh->i_toc, p_buff, 100 );
402         p_buff += 100;
403     }
404     if( p_xh->i_flags&VBR_SCALE_FLAG ) 
405     {
406         p_xh->i_vbr_scale = __GetDWBE( p_buff );
407         p_buff += 4;
408     }
409     if( ( p_xh->i_flags&FRAMES_FLAG )&&( p_xh->i_flags&BYTES_FLAG ) )
410     {
411         p_xh->i_avgbitrate = 
412               ((u64)p_xh->i_bytes * (u64)8 * (u64)mpeg.i_samplingfreq) / 
413                ((u64)p_xh->i_frames * (u64)MPEGAudio_DecodedFrameSize( &mpeg));
414     }
415 }
416                                     
417
418 /*****************************************************************************
419  * Activate: initializes MPEGaudio structures
420  *****************************************************************************/
421 static int Activate( vlc_object_t * p_this )
422 {
423     input_thread_t * p_input = (input_thread_t *)p_this;
424     demux_sys_t * p_mpegaudio;
425     mpegaudio_format_t mpeg;
426     es_descriptor_t * p_es;
427     int i_pos;
428     int b_forced;
429     input_info_category_t * p_category;
430
431     /* Set the demux function */
432     p_input->pf_demux = Demux;
433
434     /* XXX: i don't know what it's supposed to do, copied from ESInit */
435     /* Initialize access plug-in structures. */
436     if( p_input->i_mtu == 0 )
437     {
438     /* Improve speed. */
439         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
440     }
441     if( ( *p_input->psz_demux )
442         &&( !strncmp( p_input->psz_demux, "mpegaudio", 10 ) ) )
443     {
444         b_forced = 1;
445     }
446     else
447     {
448         b_forced = 0;
449     }
450
451     if ( MPEGAudio_SkipID3Tag( p_input ) )
452     {
453         return -1;
454     }
455     
456     /* check if it can be a ps stream */
457     if( __CheckPS(  p_input ) && !b_forced )
458     {
459         return( -1 );
460     }
461
462     /* must be sure that is mpeg audio stream */
463     if( MPEGAudio_FindFrame( p_input, 
464                              &i_pos, 
465                              &mpeg, 
466                              (b_forced ? 2 * MPEGAUDIO_MAXFRAMESIZE : 
467                                              MPEGAUDIO_MAXTESTPOS) ) 
468                     < (b_forced ? 1 : 2)  )
469     {
470         msg_Warn( p_input, "MPEGAudio module discarded (no frame found)" );
471         return( -1 );
472     }
473     
474     vlc_mutex_lock( &p_input->stream.stream_lock );
475     if( input_InitStream( p_input, 0 ) == -1)
476     {
477         msg_Err( p_input, "cannot init stream" );
478         return( -1 );
479     }    
480     if( input_AddProgram( p_input, 0, 0) == NULL )
481     {
482         msg_Err( p_input, "cannot add program" );
483         return( -1 );
484     }
485     p_input->stream.pp_programs[0]->b_is_ok = 0;
486     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
487     
488     /* create our ES */ 
489     p_es = input_AddES( p_input, 
490                         p_input->stream.p_selected_program, 
491                         1, /* id */
492                         0 );
493     if( !p_es )
494     {
495         vlc_mutex_unlock( &p_input->stream.stream_lock );
496         msg_Err( p_input, "out of memory" );
497         return( -1 );
498     }
499     p_es->i_stream_id = 1;
500     p_es->i_fourcc = !mpeg.i_layer ? VLC_FOURCC('m','p','g','a') /* layer 1 */
501                                    : VLC_FOURCC('m','p','g','a'); /* layer 2 */
502     p_es->i_cat = AUDIO_ES;
503     input_SelectES( p_input, p_es );
504
505     p_input->stream.p_selected_program->b_is_ok = 1;
506     vlc_mutex_unlock( &p_input->stream.stream_lock );
507
508     /* create p_mpegaudio and init it */
509     p_mpegaudio = p_input->p_demux_data = malloc( sizeof(demux_sys_t) );
510
511     if( !p_mpegaudio )
512     {
513         msg_Err( p_input, "out of memory" );
514         return( -1 );
515     }
516
517     /*input_ClockInit(  p_input->stream.p_selected_program ); 
518       done by AddProgram */
519     p_mpegaudio->p_es = p_es;
520     p_mpegaudio->mpeg = mpeg;
521     p_mpegaudio->i_framecount = 0;
522     p_mpegaudio->i_pts = 0;  
523
524     /* parse Xing Header if present */
525     MPEGAudio_ExtractXingHeader( p_input, &p_mpegaudio->xingheader );
526     
527     vlc_mutex_lock( &p_input->stream.stream_lock );
528     p_input->stream.i_mux_rate = p_mpegaudio->xingheader.i_avgbitrate / 50 / 8;
529     vlc_mutex_unlock( &p_input->stream.stream_lock );
530
531     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
532     /* if i don't do that, it don't work correctly but why ??? */
533     if( p_input->stream.b_seekable )
534     {
535         p_input->pf_seek( p_input, 0 );
536         input_AccessReinit( p_input );
537     }
538     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
539
540     /* all is ok :)) */
541     msg_Dbg( p_input, "audio MPEG-%d layer %d %s %dHz %dKb/s %s",
542                 mpeg.i_version + 1,
543                 mpeg.i_layer + 1,
544                 mpegaudio_mode[mpeg.i_mode],
545                 mpeg.i_samplingfreq,
546                 p_mpegaudio->xingheader.i_avgbitrate / 1000,
547                 p_mpegaudio->xingheader.i_flags ?
548                         "VBR (Xing)" : "" 
549                     );
550
551     vlc_mutex_lock( &p_input->stream.stream_lock );
552     p_category = input_InfoCategory( p_input, "mpeg" );
553     input_AddInfo( p_category, "input type", "audio MPEG-%d",
554                    mpeg.i_version +1 );
555     input_AddInfo( p_category, "layer", "%d", mpeg.i_layer + 1 );
556     input_AddInfo( p_category, "mode", mpegaudio_mode[mpeg.i_mode] );
557     input_AddInfo( p_category, "sample rate", "%dHz", mpeg.i_samplingfreq );
558     input_AddInfo( p_category, "average bitrate", "%dKb/s",
559                    p_mpegaudio->xingheader.i_avgbitrate / 1000 );
560     vlc_mutex_unlock( &p_input->stream.stream_lock );
561     
562     return( 0 );
563 }
564
565 /*****************************************************************************
566  * Demux: reads and demuxes data packets
567  *****************************************************************************
568  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
569  *****************************************************************************/
570 static int Demux( input_thread_t * p_input )
571 {
572     int i_pos;
573     int i_toread;
574     pes_packet_t    *p_pes;
575     mpegaudio_format_t mpeg;
576     demux_sys_t *p_mpegaudio = p_input->p_demux_data;
577
578     /*  look for a frame */
579     if( !MPEGAudio_FindFrame( p_input, &i_pos, &mpeg, 4096 ) )
580     {
581         msg_Warn( p_input, "cannot find next frame" );
582         return( 0 );
583     }
584     
585     /* if stream has changed */
586     if( ( mpeg.i_version != p_mpegaudio->mpeg.i_version )
587         ||( mpeg.i_layer != p_mpegaudio->mpeg.i_layer )
588         ||( mpeg.i_samplingfreq != p_mpegaudio->mpeg.i_samplingfreq ) )
589     {
590         msg_Dbg( p_input, "stream has changed" );
591         p_mpegaudio->i_framecount = 0;
592         p_mpegaudio->i_pts = 0;
593     }
594
595     input_ClockManageRef( p_input,
596                           p_input->stream.p_selected_program,
597                           p_mpegaudio->i_pts );
598
599     /* in fact i_pos may be garbage but ... i don't want to skip it 
600         it's borring ;) */
601
602     i_toread = MPEGAudio_FrameSize( &mpeg ) + i_pos;
603     /* create one pes */
604     if( !(p_pes = input_NewPES( p_input->p_method_data )) )
605     {
606         msg_Err( p_input, "cannot allocate new PES" );
607         return( -1 );
608     }
609
610     while( i_toread > 0 )
611     {
612         data_packet_t   *p_data;
613         int i_read;
614
615         if( (i_read = input_SplitBuffer( p_input, &p_data, i_toread ) ) <= 0 )
616         {
617             break;
618         }
619         if( !p_pes->p_first )
620         {
621             p_pes->p_first = p_data;
622             p_pes->i_nb_data = 1;
623             p_pes->i_pes_size = i_read;
624         }
625         else
626         {
627             p_pes->p_last->p_next  = p_data;
628             p_pes->i_nb_data++;
629             p_pes->i_pes_size += i_read;
630         }
631         p_pes->p_last  = p_data;
632         i_toread -= i_read;
633     }
634     p_mpegaudio->i_pts = (mtime_t)90000 * 
635                                (mtime_t)p_mpegaudio->i_framecount * 
636                                (mtime_t)MPEGAudio_DecodedFrameSize( &mpeg ) /
637                                (mtime_t)mpeg.i_samplingfreq;
638     p_pes->i_dts = 0;
639     p_pes->i_pts = input_ClockGetTS( p_input,
640                                      p_input->stream.p_selected_program,
641                                      p_mpegaudio->i_pts );
642
643     if( !p_mpegaudio->p_es->p_decoder_fifo )
644     {
645         msg_Err( p_input, "no audio decoder" );
646         input_DeletePES( p_input->p_method_data, p_pes );
647         return( -1 ); /* perhaps not, it's my choice */
648     }
649     else
650     {
651         input_DecodePES( p_mpegaudio->p_es->p_decoder_fifo, p_pes );
652     }
653
654     p_mpegaudio->i_framecount++;
655     p_mpegaudio->mpeg = mpeg; 
656
657     return( 1 );
658 }
659
660