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