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