]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/mpga.c
demuxers: remove the need for input_thread_t by using the new "meta-preparsed" input...
[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_meta.h>
32 #include <vlc_codec.h>
33 #include <vlc_input.h>
34
35 #define MPGA_PACKET_SIZE 1024
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 audio / MP3 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     vlc_bool_t b_initial_sync_failed;
72
73     int i_xing_frames;
74     int i_xing_bytes;
75     int i_xing_bitrate_avg;
76     int i_xing_frame_samples;
77     block_t *p_block_in, *p_block_out;
78
79     int                i_attachments;
80     input_attachment_t **attachments;
81 };
82
83 static int HeaderCheck( uint32_t h )
84 {
85     if( ((( h >> 21 )&0x07FF) != 0x07FF )   /* header sync */
86         || (((h >> 17)&0x03) == 0 )         /* valid layer ?*/
87         || (((h >> 12)&0x0F) == 0x0F )
88         || (((h >> 12)&0x0F) == 0x00 )      /* valid bitrate ? */
89         || (((h >> 10) & 0x03) == 0x03 )    /* valide sampling freq ? */
90         || ((h & 0x03) == 0x02 ))           /* valid emphasis ? */
91     {
92         return VLC_FALSE;
93     }
94     return VLC_TRUE;
95 }
96
97 #define MPGA_VERSION( h )   ( 1 - (((h)>>19)&0x01) )
98 #define MPGA_LAYER( h )     ( 3 - (((h)>>17)&0x03) )
99 #define MPGA_MODE(h)        (((h)>> 6)&0x03)
100
101 static int mpga_frame_samples( uint32_t h )
102 {
103     switch( MPGA_LAYER(h) )
104     {
105         case 0:
106             return 384;
107         case 1:
108             return 1152;
109         case 2:
110             return MPGA_VERSION(h) ? 576 : 1152;
111         default:
112             return 0;
113     }
114 }
115
116
117 /*****************************************************************************
118  * Open: initializes demux structures
119  *****************************************************************************/
120 static int Open( vlc_object_t * p_this )
121 {
122     demux_t     *p_demux = (demux_t*)p_this;
123     demux_sys_t *p_sys;
124     vlc_bool_t   b_forced = VLC_FALSE;
125
126     uint32_t     header;
127     const uint8_t     *p_peek;
128     module_t    *p_id3;
129     block_t     *p_block_in, *p_block_out;
130
131     if( demux2_IsPathExtension( p_demux, ".mp3" ) )
132         b_forced = VLC_TRUE;
133
134     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
135
136     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
137     {
138         vlc_bool_t b_ok = VLC_FALSE;
139         int i_peek;
140
141         if( !p_demux->b_force && !b_forced ) return VLC_EGENERIC;
142
143         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
144         while( i_peek > 4 )
145         {
146             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
147             {
148                 b_ok = VLC_TRUE;
149                 break;
150             }
151             p_peek += 1;
152             i_peek -= 1;
153         }
154         if( !b_ok && !p_demux->b_force ) return VLC_EGENERIC;
155     }
156
157     DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
158     memset( p_sys, 0, sizeof( demux_sys_t ) );
159     p_sys->p_es = 0;
160     p_sys->b_start = VLC_TRUE;
161     p_sys->meta = 0;
162
163     /* Load the mpeg audio packetizer */
164     INIT_APACKETIZER( p_sys->p_packetizer, 'm', 'p', 'g', 'a' );
165     es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
166     LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "mpga" );
167
168     /* Xing header */
169     if( HeaderCheck( header ) )
170     {
171         int i_xing, i_skip;
172         const uint8_t *p_xing;
173
174         if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) < 21 )
175             return VLC_SUCCESS; /* No header */
176
177         if( MPGA_VERSION( header ) == 0 )
178         {
179             i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
180         }
181         else
182         {
183             i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
184         }
185
186         if( i_skip + 8 < i_xing && !strncmp( (char *)&p_xing[i_skip], "Xing", 4 ) )
187         {
188             unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
189
190             p_xing += i_skip + 8;
191             i_xing -= i_skip + 8;
192
193             i_skip = 0;
194             if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
195             {
196                 p_sys->i_xing_frames = GetDWBE( &p_xing[i_skip] );
197                 i_skip += 4;
198             }
199             if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
200             {
201                 p_sys->i_xing_bytes = GetDWBE( &p_xing[i_skip] );
202                 i_skip += 4;
203             }
204             if( i_flags&0x04 )   /* XING_TOC */
205             {
206                 i_skip += 100;
207             }
208
209             // FIXME: doesn't return the right bitrage average, at least
210             // with some MP3's
211             if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
212             {
213                 p_sys->i_xing_bitrate_avg = GetDWBE( &p_xing[i_skip] );
214                 msg_Dbg( p_demux, "xing vbr value present (%d)",
215                          p_sys->i_xing_bitrate_avg );
216             }
217
218             if( p_sys->i_xing_frames > 0 && p_sys->i_xing_bytes > 0 )
219             {
220                 p_sys->i_xing_frame_samples = mpga_frame_samples( header );
221                 msg_Dbg( p_demux, "xing frames&bytes value present "
222                          "(%d bytes, %d frames, %d samples/frame)",
223                          p_sys->i_xing_bytes, p_sys->i_xing_frames,
224                          p_sys->i_xing_frame_samples );
225             }
226         }
227     }
228
229     if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) ) == NULL )
230     {
231         return VLC_EGENERIC;
232     }
233     p_block_in->i_pts = p_block_in->i_dts = 1;
234     p_block_out = p_sys->p_packetizer->pf_packetize(
235         p_sys->p_packetizer, &p_block_in );
236
237     if( p_block_out == NULL )
238     {
239         msg_Dbg( p_demux, "did not sync on first block" );
240         p_sys->b_initial_sync_failed = VLC_TRUE;
241     }
242     else
243         p_sys->b_initial_sync_failed = VLC_FALSE;
244
245     p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
246
247     if( p_sys->i_xing_bytes && p_sys->i_xing_frames &&
248         p_sys->i_xing_frame_samples )
249     {
250         p_sys->i_bitrate_avg = p_sys->i_xing_bytes * I64C(8) *
251             p_sys->p_packetizer->fmt_out.audio.i_rate /
252             p_sys->i_xing_frames / p_sys->i_xing_frame_samples;
253     }
254
255     p_sys->p_block_in = p_block_in;
256     p_sys->p_block_out = p_block_out;
257
258     /* Parse possible id3 header */
259     if( !var_CreateGetBool( p_demux, "meta-preparsed" ) )
260     {
261         p_demux->p_private = malloc( sizeof( demux_meta_t ) );
262         if( !p_demux->p_private )
263             return VLC_ENOMEM;
264         if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
265         {
266             module_Unneed( p_demux, p_id3 );
267             demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
268             p_sys->meta = p_demux_meta->p_meta;
269             p_sys->i_attachments = p_demux_meta->i_attachments;
270             p_sys->attachments = p_demux_meta->attachments;
271         }
272         free( p_demux->p_private );
273     }
274     else printf("SKIP\n");
275     /* */
276     p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
277     vlc_audio_replay_gain_MergeFromMeta( &p_sys->p_packetizer->fmt_out.audio_replay_gain,
278                                          p_sys->meta );
279     p_sys->p_es = es_out_Add( p_demux->out,
280                               &p_sys->p_packetizer->fmt_out);
281     return VLC_SUCCESS;
282 }
283
284 /*****************************************************************************
285  * Demux: reads and demuxes data packets
286  *****************************************************************************
287  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
288  *****************************************************************************/
289 static int Demux( demux_t *p_demux )
290 {
291     demux_sys_t *p_sys = p_demux->p_sys;
292     block_t *p_block_in, *p_block_out;
293     if( p_sys->b_start )
294     {
295         p_sys->b_start = VLC_FALSE;
296         p_block_in = p_sys->p_block_in;
297         p_sys->p_block_in = NULL;
298         p_block_out = p_sys->p_block_out;
299         p_sys->p_block_out = NULL;
300     }
301     else
302     {
303         if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) )
304             == NULL )
305         {
306             return 0;
307         }
308         if( p_demux->p_sys->b_initial_sync_failed == VLC_TRUE )
309         {
310             p_block_in->i_pts = p_block_in->i_dts = 1;
311             /* Only try to resync once */
312             p_demux->p_sys->b_initial_sync_failed = 0;
313         }
314         else
315             p_block_in->i_pts = p_block_in->i_dts = 0;
316         p_block_out = p_sys->p_packetizer->pf_packetize(
317             p_sys->p_packetizer, &p_block_in );
318     }
319
320
321     while( p_block_out )
322     {
323         while( p_block_out )
324         {
325             block_t *p_next = p_block_out->p_next;
326
327             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
328
329             p_block_out->p_next = NULL;
330             p_sys->i_pts = p_block_out->i_pts;
331             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
332
333             p_block_out = p_next;
334         }
335         p_block_out = p_sys->p_packetizer->pf_packetize(
336             p_sys->p_packetizer, &p_block_in );
337     }
338     return 1;
339 }
340
341 /*****************************************************************************
342  * Close: frees unused data
343  *****************************************************************************/
344 static void Close( vlc_object_t * p_this )
345 {
346     demux_t     *p_demux = (demux_t*)p_this;
347     demux_sys_t *p_sys = p_demux->p_sys;
348
349     var_Destroy( p_demux, "meta-preparsed" );
350
351     DESTROY_PACKETIZER( p_sys->p_packetizer );
352     if( p_sys->meta ) vlc_meta_Delete( p_sys->meta );
353     if( p_sys->p_block_out ) block_Release( p_sys->p_block_out );
354
355     int i;
356     for( i = 0; i < p_sys->i_attachments; i++ )
357         free( p_sys->attachments[i] );
358     TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
359
360     free( p_sys );
361 }
362
363 /*****************************************************************************
364  * Control:
365  *****************************************************************************/
366 static int Control( demux_t *p_demux, int i_query, va_list args )
367 {
368     demux_sys_t *p_sys  = p_demux->p_sys;
369     int64_t *pi64;
370     vlc_meta_t *p_meta;
371     int i_ret;
372
373     input_attachment_t ***ppp_attach;
374     int *pi_int, i;
375
376     switch( i_query )
377     {
378         case DEMUX_GET_META:
379             p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
380             vlc_meta_Merge( p_meta, p_sys->meta );
381             return VLC_SUCCESS;
382
383         case DEMUX_GET_ATTACHMENTS:
384             ppp_attach =
385                 (input_attachment_t***)va_arg( args, input_attachment_t*** );
386             pi_int = (int*)va_arg( args, int * );
387
388             if( p_sys->i_attachments <= 0 )
389                 return VLC_EGENERIC;
390
391             *pi_int = p_sys->i_attachments;
392             *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
393             for( i = 0; i < p_sys->i_attachments; i++ )
394                 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
395             return VLC_SUCCESS;
396
397         case DEMUX_GET_TIME:
398             pi64 = (int64_t*)va_arg( args, int64_t * );
399             *pi64 = p_sys->i_pts + p_sys->i_time_offset;
400             return VLC_SUCCESS;
401
402         case DEMUX_SET_TIME:
403             /* FIXME TODO: implement a high precision seek (with mp3 parsing)
404              * needed for multi-input */
405
406         default:
407             i_ret = demux2_vaControlHelper( p_demux->s, 0, -1,
408                                             p_sys->i_bitrate_avg, 1, i_query,
409                                             args );
410             /* No bitrate, we can't have it precisely, but we can compute
411              * a raw approximation with time/position */
412             if( i_ret && i_query == DEMUX_GET_LENGTH &&!p_sys->i_bitrate_avg )
413             {
414                 float f_pos = (double)( stream_Tell( p_demux->s ) ) /
415                               (double)( stream_Size( p_demux->s ) );
416                 /* The first few seconds are guaranteed to be very whacky,
417                  * don't bother trying ... Too bad */
418                 if( f_pos < 0.01 ||
419                     (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
420                     return VLC_EGENERIC;
421
422                 pi64 = (int64_t *)va_arg( args, int64_t * );
423                 *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
424                 return VLC_SUCCESS;
425             }
426             if( !i_ret && p_sys->i_bitrate_avg > 0 &&
427                 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
428             {
429                 int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) /
430                     p_sys->i_bitrate_avg;
431
432                 /* Fix time_offset */
433                 if( i_time >= 0 ) p_sys->i_time_offset = i_time - p_sys->i_pts;
434             }
435             return i_ret;
436     }
437 }