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