]> git.sesse.net Git - vlc/blob - plugins/mpeg_system/mpeg_audio.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[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.14 2002/07/31 20:56:52 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 typedef struct demux_data_mpegaudio_s
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 } demux_data_mpegaudio_t;
108
109
110 static int mpegaudio_bitrate[2][3][16] =
111 {
112     {
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 */
116     },
117     
118     {
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 */
122     }
123
124 };
125
126 static int mpegaudio_samplingfreq[2][4] = /* version 1 then 2 */
127 {
128     { 44100, 48000, 32000, 0 },
129     { 22050, 24000, 16000, 0 }
130 };
131
132 static char* mpegaudio_mode[4] =
133 {
134     "stereo", "joint stereo", "dual channel", "mono"
135 };
136
137 static inline u32 __GetDWBE( byte_t *p_buff )
138 {
139     return( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) +
140                     ( (*(p_buff+2)) << 8 ) +  ( (*(p_buff+3)) ) );
141 }
142
143 static int __CheckPS( input_thread_t *p_input )
144 {
145     byte_t *p_buff;
146     int i_size = input_Peek( p_input, &p_buff, 8196 );
147
148     while( i_size > 0 )
149     {
150         if( !(*p_buff) && !(*(p_buff + 1)) 
151                 && (*(p_buff + 2) == 1 ) && (*(p_buff + 3) >= 0xB9 ) )
152         {
153             return( 1 );  /* it could be ps so ...*/
154         }
155         p_buff++;
156         i_size--;
157     }
158     return( 0 );
159 }
160
161 /*
162 #define __GetDWBE( p_buff ) \
163     ( ( (*(p_buff)) << 24 ) + ( (*(p_buff+1)) << 16 ) + \
164       ( (*(p_buff+2)) << 8 ) +  ( (*(p_buff+3)) ) )
165 */
166 /*****************************************************************************
167  * MPEGAudio_CheckHeader : Test the validity of the header 
168  *****************************************************************************/
169 static int MPEGAudio_CheckHeader( u32 i_header )
170 {
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 ? */
177     {
178         return( 0 ); /*invalid */
179     }
180     return( 1 ); /* valid */
181 }
182
183 /*****************************************************************************
184  * MPEGAudio_ParseHeader : Parse a header ;)
185  *****************************************************************************/
186 static void MPEGAudio_ParseHeader( u32 i_header, mpegaudio_format_t *p_mpeg )
187 {
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;
201 }
202
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 )
207 {
208     /* XXX if crc do i need to add 2 bytes or not? */
209     switch( p_mpeg->i_layer )
210     {
211         case( 0 ):
212             return( ( ( ( !p_mpeg->i_version ? 12000 : 6000 ) * 
213                          p_mpeg->i_bitrate ) / 
214                          p_mpeg->i_samplingfreq + p_mpeg->i_padding ) * 4);
215         case( 1 ):
216         case( 2 ):
217             return( ( ( !p_mpeg->i_version ? 144000 : 72000 ) * 
218                          p_mpeg->i_bitrate ) /  
219                          p_mpeg->i_samplingfreq + p_mpeg->i_padding );
220     }
221     return( 1024 ); /* must never happen, 1k to advance in stream*/
222 }
223
224 /*****************************************************************************
225  * MPEGAudio_DecodedFrameSize : give the length of the decoded pcm data
226  *****************************************************************************/
227 static int MPEGAudio_DecodedFrameSize( mpegaudio_format_t *p_mpeg )
228 {
229     switch( p_mpeg->i_layer )
230     {
231         case( 0 ): /* layer 1 */
232             return( 384);
233         case( 1 ): /* layer 2 */
234             return( 1152 );
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 */
238     }
239     return( 0 );
240 }
241
242 static int MPEGAudio_SkipID3Tag( input_thread_t *p_input )
243 {
244     int count;
245     byte_t *p_peek;
246     byte_t version, revision;
247     int b_footer;
248     int i_size;
249
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 )
253     {
254         msg_Err( p_input, "cannot peek()" );
255         return( -1 );
256     }
257 /*
258     msg_Info( p_input, "Three first bytes are: %d %d %d",
259               p_peek[0],
260               p_peek[1],
261               p_peek[2]  
262               );
263 */
264     if ( !( (p_peek[0] == 0x49) && (p_peek[1] == 0x44) && (p_peek[2] == 0x33)))
265     {
266         return( 0 );
267     }
268     
269     version = p_peek[3];  /* These may become usfull later, */
270     revision = p_peek[4]; /* but we ignore them for now */
271
272     b_footer = p_peek[5] & 0x10;
273     i_size = (p_peek[6] << 21) +
274              (p_peek[7] << 14) +
275              (p_peek[8] << 7) +
276              p_peek[9];  //Is this safe?
277     if ( b_footer )
278     {
279         i_size += 10;
280     }
281     i_size += 10;
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 )
284     {
285         msg_Err( p_input, "cannot peek()" );
286         return( -1 );
287     }
288         
289     p_input->p_current_data += i_size; //seek passed end of ID3 tag
290
291     return (0);
292 }
293
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, 
302                                  int *pi_pos, 
303                                  mpegaudio_format_t *p_mpeg,
304                                  int i_posmax )
305 {
306     byte_t *p_buff;
307     u32 i_header;
308     int i_framesize;
309
310     int i_pos = 0;
311     int i_size = input_Peek( p_input, &p_buff, i_posmax+MPEGAUDIO_MAXFRAMESIZE);
312
313     while( i_pos <= __MIN( i_posmax, i_size - 4) )
314     {
315         i_header = __GetDWBE( p_buff );
316         if( MPEGAudio_CheckHeader( i_header ) )
317         {
318             MPEGAudio_ParseHeader( i_header, p_mpeg );
319             i_framesize = MPEGAudio_FrameSize( p_mpeg );
320             if(  i_pos + i_framesize + 4 > i_size )
321             {
322                 *pi_pos = i_pos;
323                 return( 1 );
324             }
325             else
326             {
327                 if( MPEGAudio_CheckHeader( __GetDWBE( p_buff + i_framesize ) ) )
328                 {
329                     *pi_pos = i_pos;
330                     return( 2 );
331                 }
332             }
333         }
334         p_buff++;
335         i_pos++;
336     }
337
338     *pi_pos = 0;
339     return( 0 );
340 }
341
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 )
350 {
351     int i_pos;
352     int i_size;
353     mpegaudio_format_t mpeg;
354     byte_t  *p_buff;
355     
356     p_xh->i_flags = 0;  /* nothing present */
357     if( !(MPEGAudio_FindFrame( p_input, &i_pos, &mpeg, 2024 )) )
358     {
359         return; /* failed , can't */
360     }
361     p_xh->i_avgbitrate = mpeg.i_bitrate * 1000; /* default */
362
363     /* 1024 is enougth */
364     if( ( i_size = input_Peek( p_input, &p_buff, 1024 + i_pos ) ) < 8 )
365     {
366         return;
367     }
368     p_buff += i_pos;
369
370     /* calculate pos of xing header */
371     if( !mpeg.i_version )
372     {
373         p_buff += mpeg.i_mode != 3 ? 36 : 21;
374     }
375     else
376     {
377         p_buff += mpeg.i_mode != 3 ? 21 : 13;
378     }
379     
380     if( (*p_buff != 'X' )||(*(p_buff+1) != 'i' )
381         ||(*(p_buff+2) != 'n' )||(*(p_buff+3) != 'g' ) )
382     {
383         return;
384     }
385     p_buff += 4;
386
387     p_xh->i_flags = __GetDWBE( p_buff ); 
388     p_buff += 4;
389
390     if( p_xh->i_flags&FRAMES_FLAG ) 
391     {
392         p_xh->i_frames = __GetDWBE( p_buff );
393         p_buff += 4;
394     }
395     if( p_xh->i_flags&BYTES_FLAG ) 
396     {
397         p_xh->i_bytes = __GetDWBE( p_buff );
398         p_buff += 4;
399     }
400     if( p_xh->i_flags&TOC_FLAG ) 
401     {
402         p_input->p_vlc->pf_memcpy( p_xh->i_toc, p_buff, 100 );
403         p_buff += 100;
404     }
405     if( p_xh->i_flags&VBR_SCALE_FLAG ) 
406     {
407         p_xh->i_vbr_scale = __GetDWBE( p_buff );
408         p_buff += 4;
409     }
410     if( ( p_xh->i_flags&FRAMES_FLAG )&&( p_xh->i_flags&BYTES_FLAG ) )
411     {
412         p_xh->i_avgbitrate = 
413               ((u64)p_xh->i_bytes * (u64)8 * (u64)mpeg.i_samplingfreq) / 
414                ((u64)p_xh->i_frames * (u64)MPEGAudio_DecodedFrameSize( &mpeg));
415     }
416 }
417                                     
418
419 /*****************************************************************************
420  * Activate: initializes MPEGaudio structures
421  *****************************************************************************/
422 static int Activate( vlc_object_t * p_this )
423 {
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;
428     int i_pos;
429     int b_forced;
430     input_info_category_t * p_category;
431
432     /* Set the demux function */
433     p_input->pf_demux = Demux;
434
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 )
438     {
439     /* Improve speed. */
440         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
441     }
442     if( ( *p_input->psz_demux )
443         &&( !strncmp( p_input->psz_demux, "mpegaudio", 10 ) ) )
444     {
445         b_forced = 1;
446     }
447     else
448     {
449         b_forced = 0;
450     }
451
452     if ( MPEGAudio_SkipID3Tag( p_input ) )
453     {
454         return -1;
455     }
456     
457     /* check if it can be a ps stream */
458     if( __CheckPS(  p_input ) && !b_forced )
459     {
460         return( -1 );
461     }
462
463     /* must be sure that is mpeg audio stream */
464     if( MPEGAudio_FindFrame( p_input, 
465                              &i_pos, 
466                              &mpeg, 
467                              (b_forced ? 2 * MPEGAUDIO_MAXFRAMESIZE : 
468                                              MPEGAUDIO_MAXTESTPOS) ) 
469                     < (b_forced ? 1 : 2)  )
470     {
471         msg_Warn( p_input, "MPEGAudio module discarded (no frame found)" );
472         return( -1 );
473     }
474     
475     vlc_mutex_lock( &p_input->stream.stream_lock );
476     if( input_InitStream( p_input, 0 ) == -1)
477     {
478         msg_Err( p_input, "cannot init stream" );
479         return( -1 );
480     }    
481     if( input_AddProgram( p_input, 0, 0) == NULL )
482     {
483         msg_Err( p_input, "cannot add program" );
484         return( -1 );
485     }
486     p_input->stream.pp_programs[0]->b_is_ok = 0;
487     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
488     
489     /* create our ES */ 
490     p_es = input_AddES( p_input, 
491                         p_input->stream.p_selected_program, 
492                         1, /* id */
493                         0 );
494     if( !p_es )
495     {
496         vlc_mutex_unlock( &p_input->stream.stream_lock );
497         msg_Err( p_input, "out of memory" );
498         return( -1 );
499     }
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 );
505
506     p_input->stream.p_selected_program->b_is_ok = 1;
507     vlc_mutex_unlock( &p_input->stream.stream_lock );
508
509     /* create p_mpegaudio and init it */
510     p_input->p_demux_data =
511            p_mpegaudio = malloc( sizeof( demux_data_mpegaudio_t ));
512
513     if( !p_mpegaudio )
514     {
515         msg_Err( p_input, "out of memory" );
516         return( -1 );
517     }
518
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;  
525
526     /* parse Xing Header if present */
527     MPEGAudio_ExtractXingHeader( p_input, &p_mpegaudio->xingheader );
528     
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 );
532
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 )
536     {
537         p_input->pf_seek( p_input, 0 );
538         input_AccessReinit( p_input );
539     }
540     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME */
541
542     /* all is ok :)) */
543     msg_Dbg( p_input, "audio MPEG-%d layer %d %s %dHz %dKb/s %s",
544                 mpeg.i_version + 1,
545                 mpeg.i_layer + 1,
546                 mpegaudio_mode[mpeg.i_mode],
547                 mpeg.i_samplingfreq,
548                 p_mpegaudio->xingheader.i_avgbitrate / 1000,
549                 p_mpegaudio->xingheader.i_flags ?
550                         "VBR (Xing)" : "" 
551                     );
552
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",
556                    mpeg.i_version +1 );
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 );
563     
564     return( 0 );
565 }
566
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 )
573 {
574     int i_pos;
575     int i_toread;
576     pes_packet_t    *p_pes;
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 ) )
582     {
583         msg_Warn( p_input, "cannot find next frame" );
584         return( 0 );
585     }
586     
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 ) )
591     {
592         msg_Dbg( p_input, "stream has changed" );
593         p_mpegaudio->i_framecount = 0;
594         p_mpegaudio->i_pts = 0;
595     }
596
597     input_ClockManageRef( p_input,
598                           p_input->stream.p_selected_program,
599                           p_mpegaudio->i_pts );
600
601     /* in fact i_pos may be garbage but ... i don't want to skip it 
602         it's borring ;) */
603
604     i_toread = MPEGAudio_FrameSize( &mpeg ) + i_pos;
605     /* create one pes */
606     if( !(p_pes = input_NewPES( p_input->p_method_data )) )
607     {
608         msg_Err( p_input, "cannot allocate new PES" );
609         return( -1 );
610     }
611
612     while( i_toread > 0 )
613     {
614         data_packet_t   *p_data;
615         int i_read;
616
617         if( (i_read = input_SplitBuffer( p_input, &p_data, i_toread ) ) <= 0 )
618         {
619             break;
620         }
621         if( !p_pes->p_first )
622         {
623             p_pes->p_first = p_data;
624             p_pes->i_nb_data = 1;
625             p_pes->i_pes_size = i_read;
626         }
627         else
628         {
629             p_pes->p_last->p_next  = p_data;
630             p_pes->i_nb_data++;
631             p_pes->i_pes_size += i_read;
632         }
633         p_pes->p_last  = p_data;
634         i_toread -= i_read;
635     }
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;
640     p_pes->i_dts = 0;
641     p_pes->i_pts = input_ClockGetTS( p_input,
642                                      p_input->stream.p_selected_program,
643                                      p_mpegaudio->i_pts );
644
645     if( !p_mpegaudio->p_es->p_decoder_fifo )
646     {
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 */
650     }
651     else
652     {
653         input_DecodePES( p_mpegaudio->p_es->p_decoder_fifo, p_pes );
654     }
655
656     p_mpegaudio->i_framecount++;
657     p_mpegaudio->mpeg = mpeg; 
658
659     return( 1 );
660 }
661
662