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