]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/mpga.c
bbe5d44353c4daa8514a0bea0067c41773c50f55
[vlc] / modules / demux / mpeg / mpga.c
1 /*****************************************************************************
2  * mpga.c : MPEG-I/II Audio input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_demux.h>
31 #include <vlc_codec.h>
32 #include <vlc_input.h>
33
34 #define MPGA_PACKET_SIZE 1024
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 vlc_module_begin();
43     set_category( CAT_INPUT );
44     set_subcategory( SUBCAT_INPUT_DEMUX );
45     set_description( _("MPEG audio / MP3 demuxer" ) );
46     set_capability( "demux2", 100 );
47     set_callbacks( Open, Close );
48     add_shortcut( "mpga" );
49     add_shortcut( "mp3" );
50 vlc_module_end();
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int Demux  ( demux_t * );
56 static int Control( demux_t *, int, va_list );
57
58 struct demux_sys_t
59 {
60     es_out_id_t *p_es;
61     vlc_meta_t  *meta;
62
63     vlc_bool_t  b_start;
64     decoder_t   *p_packetizer;
65
66     mtime_t     i_pts;
67     mtime_t     i_time_offset;
68     int         i_bitrate_avg;  /* extracted from Xing header */
69
70     vlc_bool_t b_initial_sync_failed;
71
72     int i_xing_frames;
73     int i_xing_bytes;
74     int i_xing_bitrate_avg;
75     int i_xing_frame_samples;
76     block_t *p_block_in, *p_block_out;
77 };
78
79 static int HeaderCheck( uint32_t h )
80 {
81     if( ((( h >> 21 )&0x07FF) != 0x07FF )   /* header sync */
82         || (((h >> 17)&0x03) == 0 )         /* valid layer ?*/
83         || (((h >> 12)&0x0F) == 0x0F )
84         || (((h >> 12)&0x0F) == 0x00 )      /* valid bitrate ? */
85         || (((h >> 10) & 0x03) == 0x03 )    /* valide sampling freq ? */
86         || ((h & 0x03) == 0x02 ))           /* valid emphasis ? */
87     {
88         return VLC_FALSE;
89     }
90     return VLC_TRUE;
91 }
92
93 #define MPGA_VERSION( h )   ( 1 - (((h)>>19)&0x01) )
94 #define MPGA_LAYER( h )     ( 3 - (((h)>>17)&0x03) )
95 #define MPGA_MODE(h)        (((h)>> 6)&0x03)
96
97 static int mpga_frame_samples( uint32_t h )
98 {
99     switch( MPGA_LAYER(h) )
100     {
101         case 0:
102             return 384;
103         case 1:
104             return 1152;
105         case 2:
106             return MPGA_VERSION(h) ? 576 : 1152;
107         default:
108             return 0;
109     }
110 }
111
112
113 /*****************************************************************************
114  * Open: initializes demux structures
115  *****************************************************************************/
116 static int Open( vlc_object_t * p_this )
117 {
118     demux_t     *p_demux = (demux_t*)p_this;
119     demux_sys_t *p_sys;
120     vlc_bool_t   b_forced = VLC_FALSE;
121
122     uint32_t     header;
123     uint8_t     *p_peek;
124     module_t    *p_id3;
125     block_t     *p_block_in, *p_block_out;
126
127     if( p_demux->psz_path )
128     {
129         int  i_len = strlen( p_demux->psz_path );
130         if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".mp3" ) )
131         {
132             b_forced = VLC_TRUE;
133         }
134     }
135
136     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
137
138     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
139     {
140         vlc_bool_t b_ok = VLC_FALSE;
141         int i_peek;
142
143         if( !p_demux->b_force && !b_forced ) return VLC_EGENERIC;
144
145         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
146         while( i_peek > 4 )
147         {
148             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
149             {
150                 b_ok = VLC_TRUE;
151                 break;
152             }
153             p_peek += 1;
154             i_peek -= 1;
155         }
156         if( !b_ok && !p_demux->b_force ) return VLC_EGENERIC;
157     }
158
159     STANDARD_DEMUX_INIT; p_sys = p_demux->p_sys;
160     memset( p_sys, 0, sizeof( demux_sys_t ) );
161     p_sys->p_es = 0;
162     p_sys->b_start = VLC_TRUE;
163     p_sys->meta = 0;
164
165     /* Load the mpeg audio packetizer */
166     INIT_APACKETIZER( p_sys->p_packetizer, 'm', 'p', 'g', 'a' );
167     es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
168     LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "mpga" );
169
170     /* Xing header */
171     if( HeaderCheck( header ) )
172     {
173         int i_xing, i_skip;
174         uint8_t *p_xing;
175
176         if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) < 21 )
177             return VLC_SUCCESS; /* No header */
178
179         if( MPGA_VERSION( header ) == 0 )
180         {
181             i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
182         }
183         else
184         {
185             i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
186         }
187
188         if( i_skip + 8 < i_xing && !strncmp( (char *)&p_xing[i_skip], "Xing", 4 ) )
189         {
190             unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
191
192             p_xing += i_skip + 8;
193             i_xing -= i_skip + 8;
194
195             i_skip = 0;
196             if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
197             {
198                 p_sys->i_xing_frames = GetDWBE( &p_xing[i_skip] );
199                 i_skip += 4;
200             }
201             if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
202             {
203                 p_sys->i_xing_bytes = GetDWBE( &p_xing[i_skip] );
204                 i_skip += 4;
205             }
206             if( i_flags&0x04 )   /* XING_TOC */
207             {
208                 i_skip += 100;
209             }
210
211             // FIXME: doesn't return the right bitrage average, at least
212             // with some MP3's
213             if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
214             {
215                 p_sys->i_xing_bitrate_avg = GetDWBE( &p_xing[i_skip] );
216                 msg_Dbg( p_demux, "xing vbr value present (%d)",
217                          p_sys->i_xing_bitrate_avg );
218             }
219
220             if( p_sys->i_xing_frames > 0 && p_sys->i_xing_bytes > 0 )
221             {
222                 p_sys->i_xing_frame_samples = mpga_frame_samples( header );
223                 msg_Dbg( p_demux, "xing frames&bytes value present "
224                          "(%d bytes, %d frames, %d samples/frame)",
225                          p_sys->i_xing_bytes, p_sys->i_xing_frames,
226                          p_sys->i_xing_frame_samples );
227             }
228         }
229     }
230
231     if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) ) == NULL )
232     {
233         return VLC_EGENERIC;
234     }
235     p_block_in->i_pts = p_block_in->i_dts = 1;
236     p_block_out = p_sys->p_packetizer->pf_packetize(
237         p_sys->p_packetizer, &p_block_in );
238
239     if( p_block_out == NULL )
240     {
241         msg_Dbg( p_demux, "did not sync on first block" );
242         p_sys->b_initial_sync_failed = VLC_TRUE;
243     }
244     else
245         p_sys->b_initial_sync_failed = VLC_FALSE;
246
247     p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
248
249     if( p_sys->i_xing_bytes && p_sys->i_xing_frames &&
250         p_sys->i_xing_frame_samples )
251     {
252         p_sys->i_bitrate_avg = p_sys->i_xing_bytes * I64C(8) *
253             p_sys->p_packetizer->fmt_out.audio.i_rate /
254             p_sys->i_xing_frames / p_sys->i_xing_frame_samples;
255     }
256
257     p_sys->p_block_in = p_block_in;
258     p_sys->p_block_out = p_block_out;
259
260     /* Parse possible id3 header */
261     if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
262     {
263         p_sys->meta = (vlc_meta_t *)p_demux->p_private;
264         p_demux->p_private = NULL;
265         module_Unneed( p_demux, p_id3 );
266     }
267
268     /* */
269     p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
270     vlc_audio_replay_gain_MergeFromMeta( &p_sys->p_packetizer->fmt_out.audio_replay_gain,
271                                          p_sys->meta );
272     p_sys->p_es = es_out_Add( p_demux->out,
273                               &p_sys->p_packetizer->fmt_out);
274     return VLC_SUCCESS;
275 }
276
277 /*****************************************************************************
278  * Demux: reads and demuxes data packets
279  *****************************************************************************
280  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
281  *****************************************************************************/
282 static int Demux( demux_t *p_demux )
283 {
284     demux_sys_t *p_sys = p_demux->p_sys;
285     block_t *p_block_in, *p_block_out;
286     if( p_sys->b_start )
287     {
288         p_sys->b_start = VLC_FALSE;
289         p_block_in = p_sys->p_block_in;
290         p_sys->p_block_in = NULL;
291         p_block_out = p_sys->p_block_out;
292         p_sys->p_block_out = NULL;
293     }
294     else
295     {
296         if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) )
297             == NULL )
298         {
299             return 0;
300         }
301         if( p_demux->p_sys->b_initial_sync_failed == VLC_TRUE )
302         {
303             p_block_in->i_pts = p_block_in->i_dts = 1;
304             /* Only try to resync once */
305             p_demux->p_sys->b_initial_sync_failed = 0;
306         }
307         else
308             p_block_in->i_pts = p_block_in->i_dts = 0;
309         p_block_out = p_sys->p_packetizer->pf_packetize(
310             p_sys->p_packetizer, &p_block_in );
311     }
312
313
314     while( p_block_out )
315     {
316         while( p_block_out )
317         {
318             block_t *p_next = p_block_out->p_next;
319
320             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
321
322             p_block_out->p_next = NULL;
323             p_sys->i_pts = p_block_out->i_pts;
324             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
325
326             p_block_out = p_next;
327         }
328         p_block_out = p_sys->p_packetizer->pf_packetize(
329             p_sys->p_packetizer, &p_block_in );
330     }
331     return 1;
332 }
333
334 /*****************************************************************************
335  * Close: frees unused data
336  *****************************************************************************/
337 static void Close( vlc_object_t * p_this )
338 {
339     demux_t     *p_demux = (demux_t*)p_this;
340     demux_sys_t *p_sys = p_demux->p_sys;
341
342     DESTROY_PACKETIZER( p_sys->p_packetizer );
343     if( p_sys->meta ) vlc_meta_Delete( p_sys->meta );
344     if( p_sys->p_block_out ) block_Release( p_sys->p_block_out );
345
346     free( p_sys );
347 }
348
349 /*****************************************************************************
350  * Control:
351  *****************************************************************************/
352 static int Control( demux_t *p_demux, int i_query, va_list args )
353 {
354     demux_sys_t *p_sys  = p_demux->p_sys;
355     int64_t *pi64;
356     vlc_meta_t *p_meta;
357     int i_ret;
358
359     switch( i_query )
360     {
361         case DEMUX_GET_META:
362             p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
363             vlc_meta_Merge( p_meta, p_sys->meta );
364             return VLC_SUCCESS;
365
366         case DEMUX_GET_TIME:
367             pi64 = (int64_t*)va_arg( args, int64_t * );
368             *pi64 = p_sys->i_pts + p_sys->i_time_offset;
369             return VLC_SUCCESS;
370
371         case DEMUX_SET_TIME:
372             /* FIXME TODO: implement a high precision seek (with mp3 parsing)
373              * needed for multi-input */
374
375         default:
376             i_ret = demux2_vaControlHelper( p_demux->s, 0, -1,
377                                             p_sys->i_bitrate_avg, 1, i_query,
378                                             args );
379             /* No bitrate, we can't have it precisely, but we can compute
380              * a raw approximation with time/position */
381             if( i_ret && i_query == DEMUX_GET_LENGTH &&!p_sys->i_bitrate_avg )
382             {
383                 float f_pos = (double)( stream_Tell( p_demux->s ) ) /
384                               (double)( stream_Size( p_demux->s ) );
385                 /* The first few seconds are guaranteed to be very whacky,
386                  * don't bother trying ... Too bad */
387                 if( f_pos < 0.01 ||
388                     (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
389                     return VLC_EGENERIC;
390
391                 pi64 = (int64_t *)va_arg( args, int64_t * );
392                 *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
393                 return VLC_SUCCESS;
394             }
395             if( !i_ret && p_sys->i_bitrate_avg > 0 &&
396                 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
397             {
398                 int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) /
399                     p_sys->i_bitrate_avg;
400
401                 /* Fix time_offset */
402                 if( i_time >= 0 ) p_sys->i_time_offset = i_time - p_sys->i_pts;
403             }
404             return i_ret;
405     }
406 }