]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/mpga.c
* mpga: fixed a memleak+warnings.
[vlc] / modules / demux / mpeg / mpga.c
1
2 /*****************************************************************************
3  * mpga.c : MPEG-I/II Audio input module for vlc
4  *****************************************************************************
5  * Copyright (C) 2001-2004 VideoLAN
6  * $Id$
7  *
8  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "vlc_meta.h"
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int  Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
40
41 vlc_module_begin();
42     set_description( _("MPEG-I/II audio demuxer" ) );
43     set_capability( "demux2", 100 );
44     set_callbacks( Open, Close );
45     add_shortcut( "mpga" );
46     add_shortcut( "mp3" );
47 vlc_module_end();
48
49 /* TODO:
50  * - free bitrate
51  */
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int Demux  ( demux_t * );
57 static int Control( demux_t *, int, va_list );
58
59 struct demux_sys_t
60 {
61     mtime_t         i_time;
62     mtime_t         i_time_offset;
63
64     int             i_bitrate_avg;  /* extracted from Xing header */
65
66     vlc_meta_t      *meta;
67
68     es_out_id_t     *p_es;
69 };
70
71 static int HeaderCheck( uint32_t h )
72 {
73     if( ((( h >> 21 )&0x07FF) != 0x07FF )   /* header sync */
74         || (((h >> 17)&0x03) == 0 )         /* valid layer ?*/
75         || (((h >> 12)&0x0F) == 0x0F )
76         || (((h >> 12)&0x0F) == 0x00 )      /* valid bitrate ? */
77         || (((h >> 10) & 0x03) == 0x03 )    /* valide sampling freq ? */
78         || ((h & 0x03) == 0x02 ))           /* valid emphasis ? */
79     {
80         return( VLC_FALSE );
81     }
82     return( VLC_TRUE );
83 }
84
85 static int mpga_sample_rate[2][4] =
86 {
87     { 44100, 48000, 32000, 0 },
88     { 22050, 24000, 16000, 0 }
89 };
90
91 static int mpga_bitrate[2][3][16] =
92 {
93   {
94     { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
95     { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
96     { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 0}
97   },
98   {
99     { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
100     { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0},
101     { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0}
102   }
103 };
104
105
106 #define MPGA_VERSION( h )   ( 1 - (((h)>>19)&0x01) )
107 #define MPGA_LAYER( h )     ( 3 - (((h)>>17)&0x03) )
108 #define MPGA_SAMPLE_RATE(h) \
109     ( mpga_sample_rate[MPGA_VERSION(h)][((h)>>10)&0x03] / ( ((h>>20)&0x01) ? 1 : 2) )
110 #define MPGA_CHANNELS(h)    ( (((h)>>6)&0x03) == 3 ? 1 : 2)
111 #define MPGA_BITRATE(h)     mpga_bitrate[MPGA_VERSION(h)][MPGA_LAYER(h)][((h)>>12)&0x0f]
112 #define MPGA_PADDING(h)     ( ((h)>>9)&0x01 )
113 #define MPGA_MODE(h)        (((h)>> 6)&0x03)
114
115 static int mpga_frame_size( uint32_t h )
116 {
117     switch( MPGA_LAYER(h) )
118     {
119         case 0:
120             return ( ( 12000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h) ) * 4;
121         case 1:
122             return ( 144000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
123         case 2:
124             return ( ( MPGA_VERSION(h) ? 72000 : 144000 ) * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
125         default:
126             return 0;
127     }
128 }
129
130 static int mpga_frame_samples( uint32_t h )
131 {
132     switch( MPGA_LAYER(h) )
133     {
134         case 0:
135             return 384;
136         case 1:
137             return 1152;
138         case 2:
139             return MPGA_VERSION(h) ? 576 : 1152;
140         default:
141             return 0;
142     }
143 }
144
145 /*****************************************************************************
146  * Open: initializes demux structures
147  *****************************************************************************/
148 static int Open( vlc_object_t * p_this )
149 {
150     demux_t     *p_demux = (demux_t*)p_this;
151     demux_sys_t *p_sys;
152     vlc_bool_t   b_forced = VLC_FALSE;
153     vlc_bool_t   b_extention = VLC_FALSE;
154
155     uint32_t     header;
156     uint8_t     *p_peek;
157     module_t    *p_id3;
158     es_format_t   fmt;
159
160     if( !strncmp( p_demux->psz_demux, "mpga", 4 ) ||
161         !strncmp( p_demux->psz_demux, "mp3", 3 ) )
162     {
163         b_forced = VLC_TRUE;
164     }
165     if( p_demux->psz_path )
166     {
167         int  i_len = strlen( p_demux->psz_path );
168         if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".mp3" ) )
169         {
170             b_extention = VLC_TRUE;
171         }
172     }
173
174     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
175     p_sys->i_time = 1;
176     p_sys->i_time_offset = 0;
177     p_sys->i_bitrate_avg = 0;
178     p_sys->meta = NULL;
179
180     /* skip/parse possible id3 header */
181     if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
182     {
183         p_sys->meta = (vlc_meta_t *)p_demux->p_private;
184         p_demux->p_private = NULL;
185
186         module_Unneed( p_demux, p_id3 );
187     }
188
189     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
190     {
191         msg_Err( p_demux, "cannot peek" );
192         Close( VLC_OBJECT(p_demux ) );
193         return VLC_EGENERIC;
194     }
195
196     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
197     {
198         vlc_bool_t b_ok = VLC_FALSE;
199         int i_peek;
200
201         if( !b_forced && !b_extention )
202         {
203             msg_Warn( p_demux, "mpga module discarded" );
204             Close( VLC_OBJECT(p_demux) );
205             return VLC_EGENERIC;
206         }
207
208         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
209
210         while( i_peek > 4 )
211         {
212             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
213             {
214                 b_ok = VLC_TRUE;
215                 break;
216             }
217             p_peek += 4;
218             i_peek -= 4;
219         }
220         if( !b_ok && !b_forced )
221         {
222             msg_Warn( p_demux, "mpga module discarded" );
223             Close( VLC_OBJECT(p_demux) );
224             return VLC_EGENERIC;
225         }
226     }
227
228     p_demux->pf_demux   = Demux;
229     p_demux->pf_control = Control;
230
231     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
232
233     if( HeaderCheck( header ) )
234     {
235         int     i_xing;
236         uint8_t *p_xing;
237         char psz_description[50];
238
239         p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
240         if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) >= 21 )
241         {
242             int i_skip;
243
244             if( MPGA_VERSION( header) == 0 )
245             {
246                 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
247             }
248             else
249             {
250                 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
251             }
252             if( i_skip + 8 < i_xing &&
253                 !strncmp( &p_xing[i_skip], "Xing", 4 ) )
254             {
255                 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
256                 unsigned int i_bytes = 0, i_frames = 0;
257
258                 p_xing += i_skip + 8;
259                 i_xing -= i_skip + 8;
260
261                 i_skip = 0;
262                 if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
263                 {
264                     i_frames = GetDWBE( &p_xing[i_skip] );
265                     i_skip += 4;
266                 }
267                 if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
268                 {
269                     i_bytes = GetDWBE( &p_xing[i_skip] );
270                     i_skip += 4;
271                 }
272                 if( i_flags&0x04 )   /* XING_TOC */
273                 {
274                     i_skip += 100;
275                 }
276 #if 0
277 // FIXME: doesn't return the right bitrage average, at least with some MP3's
278                 if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
279                 {
280                     p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
281     fprintf(stderr,"rate2 %d\n", p_sys->i_bitrate_avg);
282                     msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
283                 }
284                 else
285 #endif
286                 if( i_frames > 0 && i_bytes > 0 )
287                 {
288                     p_sys->i_bitrate_avg = (int64_t)i_bytes *
289                                            (int64_t)8 *
290                                            (int64_t)MPGA_SAMPLE_RATE( header ) /
291                                            (int64_t)i_frames /
292                                            (int64_t)mpga_frame_samples( header );
293                     msg_Dbg( p_demux, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
294                 }
295             }
296         }
297
298         msg_Dbg( p_demux, "version=%d layer=%d channels=%d samplerate=%d",
299                  MPGA_VERSION( header ) + 1,
300                  MPGA_LAYER( header ) + 1,
301                  MPGA_CHANNELS( header ),
302                  MPGA_SAMPLE_RATE( header ) );
303
304         fmt.audio.i_channels = MPGA_CHANNELS( header );
305         fmt.audio.i_rate = MPGA_SAMPLE_RATE( header );
306         fmt.i_bitrate = p_sys->i_bitrate_avg;
307         sprintf( psz_description, "MPEG Audio Layer %d, version %d",
308                  MPGA_LAYER ( header ) + 1, MPGA_VERSION ( header ) + 1 );
309         fmt.psz_description = strdup( psz_description );
310     }
311
312     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
313     if( fmt.psz_description ) free( fmt.psz_description );
314
315     return VLC_SUCCESS;
316 }
317
318
319 /*****************************************************************************
320  * Demux: reads and demuxes data packets
321  *****************************************************************************
322  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
323  *****************************************************************************/
324 static int Demux( demux_t *p_demux )
325 {
326     demux_sys_t *p_sys = p_demux->p_sys;
327     block_t     *p_frame;
328
329     uint32_t     header;
330     uint8_t     *p_peek;
331
332     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
333     {
334         msg_Warn( p_demux, "cannot peek" );
335         return 0;
336     }
337
338     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
339     {
340         /* we need to resynch */
341         vlc_bool_t  b_ok = VLC_FALSE;
342         int         i_skip = 0;
343         int         i_peek;
344
345         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
346         if( i_peek < 4 )
347         {
348             msg_Warn( p_demux, "cannot peek" );
349             return 0;
350         }
351
352         while( i_peek >= 4 )
353         {
354             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
355             {
356                 b_ok = VLC_TRUE;
357                 break;
358             }
359
360             p_peek++;
361             i_peek--;
362             i_skip++;
363         }
364
365         msg_Warn( p_demux, "garbage=%d bytes", i_skip );
366         stream_Read( p_demux->s, NULL, i_skip );
367         return 1;
368     }
369
370     if( ( p_frame = stream_Block( p_demux->s,
371                                   mpga_frame_size( header ) ) ) == NULL )
372     {
373         msg_Warn( p_demux, "cannot read data" );
374         return 0;
375     }
376     p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
377
378     /* set PCR */
379     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
380
381     es_out_Send( p_demux->out, p_sys->p_es, p_frame );
382
383     p_sys->i_time += (mtime_t)1000000 *
384                      (mtime_t)mpga_frame_samples( header ) /
385                      (mtime_t)MPGA_SAMPLE_RATE( header );
386     return( 1 );
387 }
388
389 /*****************************************************************************
390  * Close: frees unused data
391  *****************************************************************************/
392 static void Close( vlc_object_t * p_this )
393 {
394     demux_t     *p_demux = (demux_t*)p_this;
395     demux_sys_t *p_sys = p_demux->p_sys;
396     if( p_sys->meta )
397     {
398         vlc_meta_Delete( p_sys->meta );
399     }
400
401     free( p_sys );
402 }
403
404 /*****************************************************************************
405  * Control:
406  *****************************************************************************/
407 static int Control( demux_t *p_demux, int i_query, va_list args )
408 {
409     demux_sys_t *p_sys  = p_demux->p_sys;
410     int64_t *pi64;
411     vlc_meta_t **pp_meta;
412     int i_ret;
413
414     switch( i_query )
415     {
416         case DEMUX_GET_META:
417             pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
418             if( p_sys->meta )
419             {
420                 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
421             }
422             else
423             {
424                 *pp_meta = NULL;
425             }
426             return VLC_SUCCESS;
427
428         case DEMUX_GET_TIME:
429             pi64 = (int64_t*)va_arg( args, int64_t * );
430             *pi64 = p_sys->i_time + p_sys->i_time_offset;
431             return VLC_SUCCESS;
432
433         case DEMUX_SET_TIME:
434             /* FIXME TODO: implement a high precision seek (with mp3 parsing)
435              * needed for multi-input */
436         default:
437             i_ret = demux2_vaControlHelper( p_demux->s,
438                                             0, -1,
439                                             p_sys->i_bitrate_avg, 1, i_query,
440                                             args );
441             if( !i_ret && p_sys->i_bitrate_avg > 0 &&
442                 ( i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME ) )
443             {
444                 int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) / p_sys->i_bitrate_avg;
445
446                 /* fix time_offset */
447                 if( i_time >= 0 )
448                     p_sys->i_time_offset = i_time - p_sys->i_time;
449             }
450             return i_ret;
451     }
452 }
453