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