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