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