]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/mpga.c
mpga.c: fixed some memleaks in the probing process
[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         if( p_sys->meta != NULL )
185         {
186             /* temporary */
187             msg_Dbg( p_demux, "Title : %s",
188                      vlc_meta_GetValue( p_sys->meta,VLC_META_TITLE ) );
189             p_demux->p_private = NULL;
190         }
191         module_Unneed( p_demux, p_id3 );
192     }
193
194     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
195     {
196         msg_Err( p_demux, "cannot peek" );
197         Close( p_demux );
198         return VLC_EGENERIC;
199     }
200
201     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
202     {
203         vlc_bool_t b_ok = VLC_FALSE;
204         int i_peek;
205
206         if( !b_forced && !b_extention )
207         {
208             msg_Warn( p_demux, "mpga module discarded" );
209             Close( p_demux );
210             return VLC_EGENERIC;
211         }
212
213         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
214
215         while( i_peek > 4 )
216         {
217             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
218             {
219                 b_ok = VLC_TRUE;
220                 break;
221             }
222             p_peek += 4;
223             i_peek -= 4;
224         }
225         if( !b_ok && !b_forced )
226         {
227             msg_Warn( p_demux, "mpga module discarded" );
228             Close( p_demux );
229             return VLC_EGENERIC;
230         }
231     }
232
233     p_demux->pf_demux   = Demux;
234     p_demux->pf_control = Control;
235
236     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
237
238     if( HeaderCheck( header ) )
239     {
240         int     i_xing;
241         uint8_t *p_xing;
242         char psz_description[50];
243
244         p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
245         if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) >= 21 )
246         {
247             int i_skip;
248
249             if( MPGA_VERSION( header) == 0 )
250             {
251                 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
252             }
253             else
254             {
255                 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
256             }
257             if( i_skip + 8 < i_xing &&
258                 !strncmp( &p_xing[i_skip], "Xing", 4 ) )
259             {
260                 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
261                 unsigned int i_bytes = 0, i_frames = 0;
262
263                 p_xing += i_skip + 8;
264                 i_xing -= i_skip + 8;
265
266                 i_skip = 0;
267                 if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
268                 {
269                     i_frames = GetDWBE( &p_xing[i_skip] );
270                     i_skip += 4;
271                 }
272                 if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
273                 {
274                     i_bytes = GetDWBE( &p_xing[i_skip] );
275                     i_skip += 4;
276                 }
277                 if( i_flags&0x04 )   /* XING_TOC */
278                 {
279                     i_skip += 100;
280                 }
281 #if 0
282 // FIXME: doesn't return the right bitrage average, at least with some MP3's
283                 if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
284                 {
285                     p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
286     fprintf(stderr,"rate2 %d\n", p_sys->i_bitrate_avg);
287                     msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
288                 }
289                 else
290 #endif
291                 if( i_frames > 0 && i_bytes > 0 )
292                 {
293                     p_sys->i_bitrate_avg = (int64_t)i_bytes *
294                                            (int64_t)8 *
295                                            (int64_t)MPGA_SAMPLE_RATE( header ) /
296                                            (int64_t)i_frames /
297                                            (int64_t)mpga_frame_samples( header );
298                     msg_Dbg( p_demux, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
299                 }
300             }
301         }
302
303         msg_Dbg( p_demux, "version=%d layer=%d channels=%d samplerate=%d",
304                  MPGA_VERSION( header ) + 1,
305                  MPGA_LAYER( header ) + 1,
306                  MPGA_CHANNELS( header ),
307                  MPGA_SAMPLE_RATE( header ) );
308
309         fmt.audio.i_channels = MPGA_CHANNELS( header );
310         fmt.audio.i_rate = MPGA_SAMPLE_RATE( header );
311         fmt.i_bitrate = p_sys->i_bitrate_avg;
312         sprintf( psz_description, "MPEG Audio Layer %d, version %d",
313                  MPGA_LAYER ( header ) + 1, MPGA_VERSION ( header ) + 1 );
314         fmt.psz_description = strdup( psz_description );
315     }
316
317     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
318     if( fmt.psz_description ) free( fmt.psz_description );
319
320     return VLC_SUCCESS;
321 }
322
323
324 /*****************************************************************************
325  * Demux: reads and demuxes data packets
326  *****************************************************************************
327  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
328  *****************************************************************************/
329 static int Demux( demux_t *p_demux )
330 {
331     demux_sys_t *p_sys = p_demux->p_sys;
332     block_t     *p_frame;
333
334     uint32_t     header;
335     uint8_t     *p_peek;
336
337     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
338     {
339         msg_Warn( p_demux, "cannot peek" );
340         return 0;
341     }
342
343     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
344     {
345         /* we need to resynch */
346         vlc_bool_t  b_ok = VLC_FALSE;
347         int         i_skip = 0;
348         int         i_peek;
349
350         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
351         if( i_peek < 4 )
352         {
353             msg_Warn( p_demux, "cannot peek" );
354             return 0;
355         }
356
357         while( i_peek >= 4 )
358         {
359             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
360             {
361                 b_ok = VLC_TRUE;
362                 break;
363             }
364
365             p_peek++;
366             i_peek--;
367             i_skip++;
368         }
369
370         msg_Warn( p_demux, "garbage=%d bytes", i_skip );
371         stream_Read( p_demux->s, NULL, i_skip );
372         return 1;
373     }
374
375     if( ( p_frame = stream_Block( p_demux->s,
376                                   mpga_frame_size( header ) ) ) == NULL )
377     {
378         msg_Warn( p_demux, "cannot read data" );
379         return 0;
380     }
381     p_frame->i_dts = p_frame->i_pts = p_sys->i_time;
382
383     /* set PCR */
384     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time );
385
386     es_out_Send( p_demux->out, p_sys->p_es, p_frame );
387
388     p_sys->i_time += (mtime_t)1000000 *
389                      (mtime_t)mpga_frame_samples( header ) /
390                      (mtime_t)MPGA_SAMPLE_RATE( header );
391     return( 1 );
392 }
393
394 /*****************************************************************************
395  * Close: frees unused data
396  *****************************************************************************/
397 static void Close( vlc_object_t * p_this )
398 {
399     demux_t     *p_demux = (demux_t*)p_this;
400     demux_sys_t *p_sys = p_demux->p_sys;
401     if( p_sys->meta )
402     {
403         vlc_meta_Delete( p_sys->meta );
404     }
405
406     free( p_sys );
407 }
408
409 /*****************************************************************************
410  * Control:
411  *****************************************************************************/
412 static int Control( demux_t *p_demux, int i_query, va_list args )
413 {
414     demux_sys_t *p_sys  = p_demux->p_sys;
415     int64_t *pi64;
416     vlc_meta_t **pp_meta;
417     int i_ret;
418
419     switch( i_query )
420     {
421         case DEMUX_GET_META:
422             pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
423             if( p_sys->meta )
424             {
425                 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
426             }
427             else
428             {
429                 *pp_meta = NULL;
430             }
431             return VLC_SUCCESS;
432
433         case DEMUX_GET_TIME:
434             pi64 = (int64_t*)va_arg( args, int64_t * );
435             *pi64 = p_sys->i_time + p_sys->i_time_offset;
436             return VLC_SUCCESS;
437
438         case DEMUX_SET_TIME:
439             /* FIXME TODO: implement a high precision seek (with mp3 parsing)
440              * needed for multi-input */
441         default:
442             i_ret = demux2_vaControlHelper( p_demux->s,
443                                             0, -1,
444                                             p_sys->i_bitrate_avg, 1, i_query,
445                                             args );
446             if( !i_ret && p_sys->i_bitrate_avg > 0 &&
447                 ( i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME ) )
448             {
449                 int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) / p_sys->i_bitrate_avg;
450
451                 /* fix time_offset */
452                 if( i_time >= 0 )
453                     p_sys->i_time_offset = i_time - p_sys->i_time;
454             }
455             return i_ret;
456     }
457 }
458