]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/mpga.c
* src/video_output/video_output.c : do not use FIND_ANYWHERE to catch
[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.6 2003/09/13 17:42:16 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     mtime_t         i_time;
58
59     int             i_bitrate_avg;  /* extracted from Xing header */
60     es_out_id_t     *p_es;
61
62     //es_descriptor_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 #if 0
140 static int CheckPS( input_thread_t *p_input )
141 {
142     uint8_t  *p_peek;
143     int i_startcode = 0;
144     int i_size = input_Peek( p_input, &p_peek, 8196 );
145
146     while( i_size > 4 )
147     {
148         if( ( p_peek[0] == 0 ) && ( p_peek[1] == 0 ) &&
149             ( p_peek[2] == 1 ) && ( p_peek[3] >= 0xb9 ) &&
150             ++i_startcode >= 3 )
151         {
152             return 1;
153         }
154         p_peek++;
155         i_size--;
156     }
157
158     return 0;
159 }
160 #endif
161
162 /*****************************************************************************
163  * Open: initializes demux structures
164  *****************************************************************************/
165 static int Open( vlc_object_t * p_this )
166 {
167     input_thread_t *p_input = (input_thread_t *)p_this;
168     demux_sys_t    *p_sys;
169     vlc_bool_t     b_forced = VLC_FALSE;
170     vlc_bool_t     b_extention = VLC_FALSE;
171
172     uint32_t       header;
173
174     uint8_t        *p_peek;
175
176     module_t       *p_id3;
177
178     es_format_t    fmt;
179
180
181     if( p_input->psz_demux &&
182         ( !strncmp( p_input->psz_demux, "mpga", 4 ) ||
183           !strncmp( p_input->psz_demux, "mp3", 3 ) ) )
184     {
185         b_forced = VLC_TRUE;
186     }
187     if( p_input->psz_name )
188     {
189         int  i_len = strlen( p_input->psz_name );
190
191         if( i_len > 4 && !strcasecmp( &p_input->psz_name[i_len - 4], ".mp3" ) )
192         {
193             b_extention = VLC_TRUE;
194         }
195     }
196
197     /* skip possible id3 header */
198     p_id3 = module_Need( p_input, "id3", NULL );
199     if ( p_id3 )
200     {
201         module_Unneed( p_input, p_id3 );
202     }
203
204     if( input_Peek( p_input, &p_peek, 4 ) < 4 )
205     {
206         msg_Err( p_input, "cannot peek" );
207         return VLC_EGENERIC;
208     }
209
210     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
211     {
212         vlc_bool_t b_ok = VLC_FALSE;
213         int i_peek;
214
215         if( !b_forced && !b_extention )
216         {
217             msg_Warn( p_input, "mpga module discarded" );
218             return VLC_EGENERIC;
219         }
220
221         i_peek = input_Peek( p_input, &p_peek, 8096 );
222
223         while( i_peek > 4 )
224         {
225             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
226             {
227                 b_ok = VLC_TRUE;
228                 break;
229             }
230             p_peek += 4;
231             i_peek -= 4;
232         }
233         if( !b_ok && !b_forced )
234         {
235             msg_Warn( p_input, "mpga module discarded" );
236             return VLC_EGENERIC;
237         }
238     }
239
240     p_input->pf_demux = Demux;
241     p_input->pf_demux_control = demux_vaControlDefault;
242
243     p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
244     p_sys->i_time = 0;
245     p_sys->i_bitrate_avg = 0;
246
247     if( HeaderCheck( header ) )
248     {
249         int     i_xing;
250         uint8_t *p_xing;
251
252         input_info_category_t * p_cat;
253         static char* mpga_mode[4] =
254         {
255             "stereo", "joint stereo", "dual channel", "mono"
256         };
257
258         p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
259         if( ( i_xing = stream_Peek( p_input->s, &p_xing, 1024 ) ) >= 21 )
260         {
261             int i_skip;
262
263             if( MPGA_VERSION( header) == 0 )
264             {
265                 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
266             }
267             else
268             {
269                 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
270             }
271             if( i_skip + 8 < i_xing &&
272                 !strncmp( &p_xing[i_skip], "Xing", 4 ) )
273             {
274                 unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
275                 unsigned int i_bytes = 0, i_frames = 0;
276
277                 p_xing += i_skip + 8;
278                 i_xing -= i_skip + 8;
279
280                 i_skip = 0;
281                 if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
282                 {
283                     i_frames = GetDWBE( &p_xing[i_skip] );
284                     i_skip += 4;
285                 }
286                 if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
287                 {
288                     i_bytes = GetDWBE( &p_xing[i_skip] );
289                     i_skip += 4;
290                 }
291                 if( i_flags&0x04 )   /* XING_TOC */
292                 {
293                     i_skip += 100;
294                 }
295                 if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
296                 {
297                     p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
298                     msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
299                 }
300                 else if( i_frames > 0 && i_bytes > 0 )
301                 {
302                     p_sys->i_bitrate_avg = (int64_t)i_bytes *
303                                            (int64_t)8 *
304                                            (int64_t)MPGA_SAMPLE_RATE( header ) /
305                                            (int64_t)i_frames /
306                                            (int64_t)mpga_frame_samples( header );
307                     msg_Dbg( p_input, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
308                 }
309             }
310         }
311
312         msg_Dbg( p_input, "version=%d layer=%d channels=%d samplerate=%d",
313                  MPGA_VERSION( header) + 1,
314                  MPGA_LAYER( header ) + 1,
315                  MPGA_CHANNELS( header ),
316                  MPGA_SAMPLE_RATE( header ) );
317
318         vlc_mutex_lock( &p_input->stream.stream_lock );
319
320         p_cat = input_InfoCategory( p_input, _("MPEG") );
321         input_AddInfo( p_cat, _("Input Type"), "Audio MPEG-%d",
322                        MPGA_VERSION( header) + 1 );
323         input_AddInfo( p_cat, _("Layer"), "%d",
324                        MPGA_LAYER( header ) + 1 );
325         input_AddInfo( p_cat, _("Mode"),
326                        mpga_mode[MPGA_MODE( header )] );
327         input_AddInfo( p_cat, _("Sample Rate"), "%dHz",
328                        MPGA_SAMPLE_RATE( header ) );
329         input_AddInfo( p_cat, _("Average Bitrate"), "%dKb/s",
330                        p_sys->i_bitrate_avg / 1000 );
331         vlc_mutex_unlock( &p_input->stream.stream_lock );
332     }
333
334     vlc_mutex_lock( &p_input->stream.stream_lock );
335     if( input_InitStream( p_input, 0 ) == -1)
336     {
337         vlc_mutex_unlock( &p_input->stream.stream_lock );
338         msg_Err( p_input, "cannot init stream" );
339         goto error;
340     }
341     p_input->stream.i_mux_rate = p_sys->i_bitrate_avg / 8 / 50;
342     vlc_mutex_unlock( &p_input->stream.stream_lock );
343
344     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
345     p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
346     return VLC_SUCCESS;
347
348 error:
349     free( p_sys );
350     return VLC_EGENERIC;
351 }
352
353
354 /*****************************************************************************
355  * Demux: reads and demuxes data packets
356  *****************************************************************************
357  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
358  *****************************************************************************/
359 static int Demux( input_thread_t * p_input )
360 {
361     demux_sys_t  *p_sys = p_input->p_demux_data;
362     pes_packet_t *p_pes;
363
364     uint32_t     header;
365     uint8_t      *p_peek;
366
367     if( stream_Peek( p_input->s, &p_peek, 4 ) < 4 )
368     {
369         msg_Warn( p_input, "cannot peek" );
370         return 0;
371     }
372
373     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
374     {
375         /* we need to resynch */
376         vlc_bool_t  b_ok = VLC_FALSE;
377         int         i_skip = 0;
378         int         i_peek;
379
380         i_peek = stream_Peek( p_input->s, &p_peek, 8096 );
381         if( i_peek < 4 )
382         {
383             msg_Warn( p_input, "cannot peek" );
384             return 0;
385         }
386
387         while( i_peek >= 4 )
388         {
389             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
390             {
391                 b_ok = VLC_TRUE;
392                 break;
393             }
394
395             p_peek++;
396             i_peek--;
397             i_skip++;
398         }
399
400         msg_Warn( p_input, "garbage=%d bytes", i_skip );
401         stream_Read( p_input->s, NULL, i_skip );
402         return 1;
403     }
404
405     input_ClockManageRef( p_input,
406                           p_input->stream.p_selected_program,
407                           p_sys->i_time * 9 / 100 );
408
409     if( ( p_pes = stream_PesPacket( p_input->s, mpga_frame_size( header ) ) )
410                                                                       == NULL )
411     {
412         msg_Warn( p_input, "cannot read data" );
413         return 0;
414     }
415
416     p_pes->i_dts =
417     p_pes->i_pts = input_ClockGetTS( p_input,
418                                      p_input->stream.p_selected_program,
419                                      p_sys->i_time * 9 / 100 );
420
421     es_out_Send( p_input->p_es_out, p_sys->p_es, p_pes );
422
423     p_sys->i_time += (mtime_t)1000000 *
424                      (mtime_t)mpga_frame_samples( header ) /
425                      (mtime_t)MPGA_SAMPLE_RATE( header );
426     return( 1 );
427 }
428
429 /*****************************************************************************
430  * Close: frees unused data
431  *****************************************************************************/
432 static void Close( vlc_object_t * p_this )
433 {
434     input_thread_t *p_input = (input_thread_t*)p_this;
435     demux_sys_t    *p_sys = p_input->p_demux_data;
436
437     free( p_sys );
438 }
439