]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/mpga.c
* mpga: mp3 demuxer written from scratch using ninput.h
[vlc] / modules / demux / mpeg / mpga.c
1 /*****************************************************************************
2  * mpga.c : MPEG-I/II Audio input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: mpga.c,v 1.1 2003/08/01 00:37:06 fenrir Exp $
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 <ninput.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( "demux", 100 );
43     set_callbacks( Open, Close );
44     add_shortcut( "mpga" );
45     add_shortcut( "mp3" );
46 vlc_module_end();
47
48 /* TODO:
49  * - mpeg 2.5
50  * - free bitrate
51  */
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int  Demux       ( input_thread_t * );
57
58 struct demux_sys_t
59 {
60     stream_t        *s;
61     mtime_t         i_time;
62
63     int             i_bitrate_avg;  /* extracted from Xing header */
64     es_descriptor_t *p_es;
65 };
66
67
68 static inline uint32_t GetDWBE( uint8_t *p )
69 {
70     return( ( p[0] << 24 )|( p[1] << 16 )|( p[2] <<  8 )|( p[3] ) );
71 }
72
73 static int HeaderCheck( uint32_t h )
74 {
75     if( ((( h >> 20 )&0x0FFF) != 0x0FFF )  /* header sync */
76         || (((h >> 17)&0x03) == 0 )  /* valid layer ?*/
77         || (((h >> 12)&0x0F) == 0x0F )
78         || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
79         || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
80         || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
81     {
82         return( VLC_FALSE );
83     }
84     return( VLC_TRUE );
85 }
86
87 static int mpga_sample_rate[2][4] =
88 {
89     { 44100, 48000, 32000, 0 },
90     { 22050, 24000, 16000, 0 }
91 };
92
93 static int mpga_bitrate[2][3][16] =
94 {
95   {
96     { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
97     { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
98     { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 0}
99   },
100   {
101     { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
102     { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0},
103     { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0}
104   }
105 };
106
107
108 #define MPGA_VERSION( h )   ( 1 - (((h)>>19)&0x01) )
109 #define MPGA_LAYER( h )     ( 3 - (((h)>>17)&0x03) )
110 #define MPGA_SAMPLE_RATE(h) mpga_sample_rate[MPGA_VERSION(h)][((h)>>10)&0x03]
111 #define MPGA_CHANNELS(h)    ( (((h)>>6)&0x03) == 3 ? 1 : 2)
112 #define MPGA_BITRATE(h)     mpga_bitrate[MPGA_VERSION(h)][MPGA_LAYER(h)][((h)>>12)&0x0f]
113 #define MPGA_PADDING(h)     ( ((h)>>9)&0x01 )
114 #define MPGA_MODE(h)        (((h)>> 6)&0x03)
115
116 static int mpga_frame_size( uint32_t h )
117 {
118     switch( MPGA_LAYER(h) )
119     {
120         case 0:
121             return ( ( 12000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h) ) * 4;
122         case 1:
123             return ( 144000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
124         case 2:
125             return ( ( MPGA_VERSION(h) ? 72000 : 144000 ) * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
126         default:
127             return 0;
128     }
129 }
130
131 static int mpga_frame_samples( uint32_t h )
132 {
133     switch( MPGA_LAYER(h) )
134     {
135         case 0:
136             return 384;
137         case 1:
138             return 1152;
139         case 2:
140             return MPGA_VERSION(h) ? 576 : 1152;
141         default:
142             return 0;
143     }
144 }
145
146 #if 0
147 static int CheckPS( input_thread_t *p_input )
148 {
149     uint8_t  *p_peek;
150     int i_startcode = 0;
151     int i_size = input_Peek( p_input, &p_peek, 8196 );
152
153     while( i_size > 4 )
154     {
155         if( ( p_peek[0] == 0 ) && ( p_peek[1] == 0 ) &&
156             ( p_peek[2] == 1 ) && ( p_peek[3] >= 0xb9 ) &&
157             ++i_startcode >= 3 )
158         {
159             return 1;
160         }
161         p_peek++;
162         i_size--;
163     }
164
165     return 0;
166 }
167 #endif
168
169 /*****************************************************************************
170  * Open: initializes demux structures
171  *****************************************************************************/
172 static int Open( vlc_object_t * p_this )
173 {
174     input_thread_t *p_input = (input_thread_t *)p_this;
175     demux_sys_t    *p_sys;
176     vlc_bool_t     b_forced = VLC_FALSE;
177     vlc_bool_t     b_extention = VLC_FALSE;
178
179     uint32_t       header;
180
181     uint8_t        *p_peek;
182
183     module_t       *p_id3;
184
185
186     if( p_input->psz_demux &&
187         ( !strncmp( p_input->psz_demux, "mpga", 4 ) ||
188           !strncmp( p_input->psz_demux, "mp3", 3 ) ) )
189     {
190         b_forced = VLC_TRUE;
191     }
192     if( p_input->psz_name )
193     {
194         int  i_len = strlen( p_input->psz_name );
195
196         if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".mp3" ) )
197         {
198             b_extention = VLC_TRUE;
199         }
200     }
201
202     /* skip possible id3 header */
203     p_id3 = module_Need( p_input, "id3", NULL );
204     if ( p_id3 )
205     {
206         module_Unneed( p_input, p_id3 );
207     }
208
209     if( input_Peek( p_input, &p_peek, 4 ) < 4 )
210     {
211         msg_Err( p_input, "cannot peek" );
212         return VLC_EGENERIC;
213     }
214
215     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
216     {
217         vlc_bool_t b_ok = VLC_FALSE;
218         int i_peek;
219
220         if( !b_forced && !b_extention )
221         {
222             msg_Warn( p_input, "mpga module discarded" );
223             return VLC_EGENERIC;
224         }
225
226         i_peek = input_Peek( p_input, &p_peek, 8096 );
227
228         while( i_peek > 4 )
229         {
230             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
231             {
232                 b_ok = VLC_TRUE;
233                 break;
234             }
235             p_peek += 4;
236             i_peek -= 4;
237         }
238         if( !b_ok && !b_forced )
239         {
240             msg_Warn( p_input, "mpga module discarded" );
241             return VLC_EGENERIC;
242         }
243     }
244
245     p_input->pf_demux = Demux;
246
247     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
248     p_sys->i_time = 0;
249     p_sys->i_bitrate_avg = 0;
250
251     if( ( p_sys->s = stream_OpenInput( p_input ) ) == NULL )
252     {
253         msg_Err( p_input, "cannot create stream" );
254         goto error;
255     }
256
257     if( HeaderCheck( header ) )
258     {
259         int     i_xing;
260         uint8_t *p_xing;
261
262         input_info_category_t * p_cat;
263         static char* mpga_mode[4] =
264         {
265             "stereo", "joint stereo", "dual channel", "mono"
266         };
267
268         p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
269         if( ( i_xing = stream_Peek( p_sys->s, &p_xing, 1024 ) ) >= 21 )
270         {
271             int i_skip;
272
273             if( MPGA_VERSION( header) == 0 )
274             {
275                 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
276             }
277             else
278             {
279                 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
280             }
281             if( i_skip + 8 < i_xing &&
282                 !strncmp( &p_xing[i_skip], "Xing", 4 ) )
283             {
284                 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
285                 unsigned int i_bytes = 0, i_frames = 0;
286
287                 p_xing += i_skip + 8;
288                 i_xing -= i_skip + 8;
289
290                 i_skip = 0;
291                 if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
292                 {
293                     i_frames = GetDWBE( &p_xing[i_skip] );
294                     i_skip += 4;
295                 }
296                 if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
297                 {
298                     i_bytes = GetDWBE( &p_xing[i_skip] );
299                     i_skip += 4;
300                 }
301                 if( i_flags&0x04 )   /* XING_TOC */
302                 {
303                     i_skip += 100;
304                 }
305                 if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
306                 {
307                     p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
308                     msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
309                 }
310                 else if( i_frames > 0 && i_bytes > 0 )
311                 {
312                     p_sys->i_bitrate_avg = (int64_t)i_bytes *
313                                            (int64_t)8 *
314                                            (int64_t)MPGA_SAMPLE_RATE( header ) /
315                                            (int64_t)i_frames /
316                                            (int64_t)mpga_frame_samples( header );
317                     msg_Dbg( p_input, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
318                 }
319             }
320         }
321
322         msg_Dbg( p_input, "version=%d layer=%d channels=%d samplerate=%d",
323                  MPGA_VERSION( header) + 1,
324                  MPGA_LAYER( header ) + 1,
325                  MPGA_CHANNELS( header ),
326                  MPGA_SAMPLE_RATE( header ) );
327
328         vlc_mutex_lock( &p_input->stream.stream_lock );
329
330         p_cat = input_InfoCategory( p_input, _("MPEG") );
331         input_AddInfo( p_cat, _("Input Type"), "Audio MPEG-%d",
332                        MPGA_VERSION( header) + 1 );
333         input_AddInfo( p_cat, _("Layer"), "%d",
334                        MPGA_LAYER( header ) + 1 );
335         input_AddInfo( p_cat, _("Mode"),
336                        mpga_mode[MPGA_MODE( header )] );
337         input_AddInfo( p_cat, _("Sample Rate"), "%dHz",
338                        MPGA_SAMPLE_RATE( header ) );
339         input_AddInfo( p_cat, _("Average Bitrate"), "%dKb/s",
340                        p_sys->i_bitrate_avg / 1000 );
341         vlc_mutex_unlock( &p_input->stream.stream_lock );
342     }
343
344     vlc_mutex_lock( &p_input->stream.stream_lock );
345     if( input_InitStream( p_input, 0 ) == -1)
346     {
347         vlc_mutex_unlock( &p_input->stream.stream_lock );
348         msg_Err( p_input, "cannot init stream" );
349         goto error;
350     }
351     if( input_AddProgram( p_input, 0, 0) == NULL )
352     {
353         vlc_mutex_unlock( &p_input->stream.stream_lock );
354         msg_Err( p_input, "cannot add program" );
355         goto error;
356     }
357     p_input->stream.pp_programs[0]->b_is_ok = 0;
358     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
359
360     p_input->stream.i_mux_rate = p_sys->i_bitrate_avg / 8 / 50;
361
362     p_sys->p_es = input_AddES( p_input,
363                                p_input->stream.p_selected_program,
364                                1 , AUDIO_ES, NULL, 0 );
365
366     p_sys->p_es->i_stream_id = 1;
367     p_sys->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
368     input_SelectES( p_input, p_sys->p_es );
369
370     p_input->stream.p_selected_program->b_is_ok = 1;
371     vlc_mutex_unlock( &p_input->stream.stream_lock );
372
373     return VLC_SUCCESS;
374
375 error:
376     if( p_sys->s )
377     {
378         stream_Release( p_sys->s );
379     }
380     free( p_sys );
381     return VLC_EGENERIC;
382 }
383
384
385 /*****************************************************************************
386  * Demux: reads and demuxes data packets
387  *****************************************************************************
388  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
389  *****************************************************************************/
390 static int Demux( input_thread_t * p_input )
391 {
392     demux_sys_t  *p_sys = p_input->p_demux_data;
393     pes_packet_t *p_pes;
394
395     uint32_t     header;
396     uint8_t      *p_peek;
397
398     if( stream_Peek( p_sys->s, &p_peek, 4 ) < 4 )
399     {
400         msg_Warn( p_input, "cannot peek" );
401         return 0;
402     }
403
404     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
405     {
406         /* we need to resynch */
407         vlc_bool_t  b_ok = VLC_FALSE;
408         int         i_skip = 0;
409         int         i_peek;
410
411         i_peek = stream_Peek( p_sys->s, &p_peek, 8096 );
412         if( i_peek < 4 )
413         {
414             msg_Warn( p_input, "cannot peek" );
415             return 0;
416         }
417
418         while( i_peek >= 4 )
419         {
420             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
421             {
422                 b_ok = VLC_TRUE;
423                 break;
424             }
425
426             p_peek++;
427             i_peek--;
428             i_skip++;
429         }
430
431         msg_Warn( p_input, "garbage=%d bytes", i_skip );
432         stream_Read( p_sys->s, NULL, i_skip );
433         return 1;
434     }
435
436     input_ClockManageRef( p_input,
437                           p_input->stream.p_selected_program,
438                           p_sys->i_time * 9 / 100 );
439
440     if( ( p_pes = stream_PesPacket( p_sys->s, mpga_frame_size( header ) ) ) == NULL )
441     {
442         msg_Warn( p_input, "cannot read data" );
443         return 0;
444     }
445
446     p_pes->i_dts =
447     p_pes->i_pts = input_ClockGetTS( p_input,
448                                      p_input->stream.p_selected_program,
449                                      p_sys->i_time * 9 / 100 );
450
451     if( !p_sys->p_es->p_decoder_fifo )
452     {
453         msg_Err( p_input, "no audio decoder" );
454         input_DeletePES( p_input->p_method_data, p_pes );
455         return( -1 );
456     }
457
458     input_DecodePES( p_sys->p_es->p_decoder_fifo, p_pes );
459     p_sys->i_time += (mtime_t)1000000 *
460                      (mtime_t)mpga_frame_samples( header ) /
461                      (mtime_t)MPGA_SAMPLE_RATE( header );
462     return( 1 );
463 }
464
465 /*****************************************************************************
466  * Close: frees unused data
467  *****************************************************************************/
468 static void Close( vlc_object_t * p_this )
469 {
470     input_thread_t *p_input = (input_thread_t*)p_this;
471     demux_sys_t    *p_sys = p_input->p_demux_data;
472
473     if( p_sys->s )
474     {
475         stream_Release( p_sys->s );
476     }
477     free( p_sys );
478 }
479