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