1c4d9ec1d15e7c9ee39aa59d0757aedd73220157
[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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_codec.h>
37 #include <vlc_input.h>
38
39 #define MPGA_PACKET_SIZE 1024
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
46
47 vlc_module_begin();
48     set_category( CAT_INPUT );
49     set_subcategory( SUBCAT_INPUT_DEMUX );
50     set_description( _("MPEG audio / MP3 demuxer" ) );
51     set_capability( "demux", 100 );
52     set_callbacks( Open, Close );
53     add_shortcut( "mpga" );
54     add_shortcut( "mp3" );
55 vlc_module_end();
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 static int Demux  ( demux_t * );
61 static int Control( demux_t *, int, va_list );
62
63 struct demux_sys_t
64 {
65     es_out_id_t *p_es;
66
67     bool  b_start;
68     decoder_t   *p_packetizer;
69
70     mtime_t     i_pts;
71     mtime_t     i_time_offset;
72     int         i_bitrate_avg;  /* extracted from Xing header */
73
74     bool b_initial_sync_failed;
75
76     int i_xing_frames;
77     int i_xing_bytes;
78     int i_xing_bitrate_avg;
79     int i_xing_frame_samples;
80     block_t *p_block_in, *p_block_out;
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 false;
93     }
94     return 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     bool   b_forced = false;
125
126     uint32_t     header;
127     const uint8_t     *p_peek;
128     block_t     *p_block_in, *p_block_out;
129
130     if( demux_IsPathExtension( p_demux, ".mp3" ) )
131         b_forced = true;
132
133     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
134
135     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
136     {
137         bool b_ok = false;
138         int i_peek;
139
140         if( !p_demux->b_force && !b_forced ) return VLC_EGENERIC;
141
142         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
143         while( i_peek > 4 )
144         {
145             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
146             {
147                 b_ok = true;
148                 break;
149             }
150             p_peek += 1;
151             i_peek -= 1;
152         }
153         if( !b_ok && !p_demux->b_force ) return VLC_EGENERIC;
154     }
155
156     DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
157     memset( p_sys, 0, sizeof( demux_sys_t ) );
158     p_sys->p_es = 0;
159     p_sys->b_start = true;
160
161     /* Load the mpeg audio packetizer */
162     INIT_APACKETIZER( p_sys->p_packetizer, 'm', 'p', 'g', 'a' );
163     es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
164     LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "mpga" );
165
166     /* Xing header */
167     if( HeaderCheck( header ) )
168     {
169         int i_xing, i_skip;
170         const uint8_t *p_xing;
171
172         if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) < 21 )
173             return VLC_SUCCESS; /* No header */
174
175         if( MPGA_VERSION( header ) == 0 )
176         {
177             i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
178         }
179         else
180         {
181             i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
182         }
183
184         if( i_skip + 8 < i_xing && !strncmp( (char *)&p_xing[i_skip], "Xing", 4 ) )
185         {
186             unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
187
188             p_xing += i_skip + 8;
189             i_xing -= i_skip + 8;
190
191             i_skip = 0;
192             if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
193             {
194                 p_sys->i_xing_frames = GetDWBE( &p_xing[i_skip] );
195                 i_skip += 4;
196             }
197             if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
198             {
199                 p_sys->i_xing_bytes = GetDWBE( &p_xing[i_skip] );
200                 i_skip += 4;
201             }
202             if( i_flags&0x04 )   /* XING_TOC */
203             {
204                 i_skip += 100;
205             }
206
207             // FIXME: doesn't return the right bitrage average, at least
208             // with some MP3's
209             if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
210             {
211                 p_sys->i_xing_bitrate_avg = GetDWBE( &p_xing[i_skip] );
212                 msg_Dbg( p_demux, "xing vbr value present (%d)",
213                          p_sys->i_xing_bitrate_avg );
214             }
215
216             if( p_sys->i_xing_frames > 0 && p_sys->i_xing_bytes > 0 )
217             {
218                 p_sys->i_xing_frame_samples = mpga_frame_samples( header );
219                 msg_Dbg( p_demux, "xing frames&bytes value present "
220                          "(%d bytes, %d frames, %d samples/frame)",
221                          p_sys->i_xing_bytes, p_sys->i_xing_frames,
222                          p_sys->i_xing_frame_samples );
223             }
224         }
225     }
226
227     if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) ) == NULL )
228     {
229         return VLC_EGENERIC;
230     }
231     p_block_in->i_pts = p_block_in->i_dts = 1;
232     p_block_out = p_sys->p_packetizer->pf_packetize(
233         p_sys->p_packetizer, &p_block_in );
234
235     if( p_block_out == NULL )
236     {
237         msg_Dbg( p_demux, "did not sync on first block" );
238         p_sys->b_initial_sync_failed = true;
239     }
240     else
241         p_sys->b_initial_sync_failed = false;
242
243     p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
244
245     if( p_sys->i_xing_bytes && p_sys->i_xing_frames &&
246         p_sys->i_xing_frame_samples )
247     {
248         p_sys->i_bitrate_avg = p_sys->i_xing_bytes * INT64_C(8) *
249             p_sys->p_packetizer->fmt_out.audio.i_rate /
250             p_sys->i_xing_frames / p_sys->i_xing_frame_samples;
251     }
252
253     p_sys->p_block_in = p_block_in;
254     p_sys->p_block_out = p_block_out;
255
256     /* */
257     p_sys->p_packetizer->fmt_out.b_packetized = true;
258     p_sys->p_es = es_out_Add( p_demux->out,
259                               &p_sys->p_packetizer->fmt_out);
260     return VLC_SUCCESS;
261 }
262
263 /*****************************************************************************
264  * Demux: reads and demuxes data packets
265  *****************************************************************************
266  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
267  *****************************************************************************/
268 static int Demux( demux_t *p_demux )
269 {
270     demux_sys_t *p_sys = p_demux->p_sys;
271     block_t *p_block_in, *p_block_out;
272     if( p_sys->b_start )
273     {
274         p_sys->b_start = false;
275         p_block_in = p_sys->p_block_in;
276         p_block_out = p_sys->p_block_out;
277     }
278     else
279     {
280         if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) )
281             == NULL )
282         {
283             return 0;
284         }
285         if( p_demux->p_sys->b_initial_sync_failed == true )
286         {
287             p_block_in->i_pts = p_block_in->i_dts = 1;
288             /* Only try to resync once */
289             p_demux->p_sys->b_initial_sync_failed = 0;
290         }
291         else
292             p_block_in->i_pts = p_block_in->i_dts = 0;
293         p_block_out = p_sys->p_packetizer->pf_packetize(
294             p_sys->p_packetizer, &p_block_in );
295     }
296
297
298     while( p_block_out )
299     {
300         while( p_block_out )
301         {
302             block_t *p_next = p_block_out->p_next;
303
304             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
305
306             p_block_out->p_next = NULL;
307             p_sys->i_pts = p_block_out->i_pts;
308             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
309
310             p_block_out = p_next;
311         }
312         p_block_out = p_sys->p_packetizer->pf_packetize(
313             p_sys->p_packetizer, &p_block_in );
314     }
315     return 1;
316 }
317
318 /*****************************************************************************
319  * Close: frees unused data
320  *****************************************************************************/
321 static void Close( vlc_object_t * p_this )
322 {
323     demux_t     *p_demux = (demux_t*)p_this;
324     demux_sys_t *p_sys = p_demux->p_sys;
325
326     DESTROY_PACKETIZER( p_sys->p_packetizer );
327     if( p_sys->p_block_out ) block_Release( p_sys->p_block_out );
328     if( p_sys->p_block_in ) block_Release( p_sys->p_block_in );
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     bool *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 = (bool*)va_arg( args, bool* );
350             *pb_bool = 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 = demux_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 = demux_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 = INT64_C(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 }