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