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