]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/mpga.c
Retry syncing mpeg audio stream if it failed on first block (Closes:#609)
[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/input.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     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
161     memset( p_sys, 0, sizeof( demux_sys_t ) );
162     p_sys->p_es = 0;
163     p_sys->p_packetizer = 0;
164     p_sys->b_start = VLC_TRUE;
165     p_sys->meta = 0;
166     p_demux->pf_demux   = Demux;
167     p_demux->pf_control = Control;
168
169     /*
170      * Load the mpeg audio packetizer
171      */
172     p_sys->p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_PACKETIZER );
173     p_sys->p_packetizer->pf_decode_audio = NULL;
174     p_sys->p_packetizer->pf_decode_video = NULL;
175     p_sys->p_packetizer->pf_decode_sub = NULL;
176     p_sys->p_packetizer->pf_packetize = NULL;
177     es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
178                     VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
179     es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
180     p_sys->p_packetizer->p_module =
181         module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
182
183     if( p_sys->p_packetizer->p_module == NULL )
184     {
185         msg_Err( p_demux, "cannot find mpga packetizer" );
186         Close( VLC_OBJECT(p_demux ) );
187         return VLC_EGENERIC;
188     }
189
190     /* Xing header */
191     if( HeaderCheck( header ) )
192     {
193         int i_xing, i_skip;
194         uint8_t *p_xing;
195
196         if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) < 21 )
197             return VLC_SUCCESS; /* No header */
198
199         if( MPGA_VERSION( header ) == 0 )
200         {
201             i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
202         }
203         else
204         {
205             i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
206         }
207
208         if( i_skip + 8 < i_xing && !strncmp( (char *)&p_xing[i_skip], "Xing", 4 ) )
209         {
210             unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
211
212             p_xing += i_skip + 8;
213             i_xing -= i_skip + 8;
214
215             i_skip = 0;
216             if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
217             {
218                 p_sys->i_xing_frames = GetDWBE( &p_xing[i_skip] );
219                 i_skip += 4;
220             }
221             if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
222             {
223                 p_sys->i_xing_bytes = GetDWBE( &p_xing[i_skip] );
224                 i_skip += 4;
225             }
226             if( i_flags&0x04 )   /* XING_TOC */
227             {
228                 i_skip += 100;
229             }
230
231             // FIXME: doesn't return the right bitrage average, at least
232             // with some MP3's
233             if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
234             {
235                 p_sys->i_xing_bitrate_avg = GetDWBE( &p_xing[i_skip] );
236                 msg_Dbg( p_demux, "xing vbr value present (%d)",
237                          p_sys->i_xing_bitrate_avg );
238             }
239
240             if( p_sys->i_xing_frames > 0 && p_sys->i_xing_bytes > 0 )
241             {
242                 p_sys->i_xing_frame_samples = mpga_frame_samples( header );
243                 msg_Dbg( p_demux, "xing frames&bytes value present "
244                          "(%d bytes, %d frames, %d samples/frame)",
245                          p_sys->i_xing_bytes, p_sys->i_xing_frames,
246                          p_sys->i_xing_frame_samples );
247             }
248         }
249     }
250
251     if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) ) == NULL )
252     {
253         return VLC_EGENERIC;
254     }
255     p_block_in->i_pts = p_block_in->i_dts = 1;
256     p_block_out = p_sys->p_packetizer->pf_packetize(
257         p_sys->p_packetizer, &p_block_in );
258
259     if( p_block_out == NULL )
260     {
261         msg_Dbg( p_demux, "did not sync on first block" );
262         p_sys->b_initial_sync_failed = VLC_TRUE;
263     }
264     else
265         p_sys->b_initial_sync_failed = VLC_FALSE;
266
267     p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
268     p_sys->p_es = es_out_Add( p_demux->out,
269                               &p_sys->p_packetizer->fmt_out);
270     p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
271
272     if( p_sys->i_xing_bytes && p_sys->i_xing_frames &&
273         p_sys->i_xing_frame_samples )
274     {
275         p_sys->i_bitrate_avg = p_sys->i_xing_bytes * I64C(8) *
276             p_sys->p_packetizer->fmt_out.audio.i_rate /
277             p_sys->i_xing_frames / p_sys->i_xing_frame_samples;
278     }
279
280     p_sys->p_block_in = p_block_in;
281     p_sys->p_block_out = p_block_out;
282
283     /* Parse possible id3 header */
284     if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
285     {
286         p_sys->meta = (vlc_meta_t *)p_demux->p_private;
287         p_demux->p_private = NULL;
288         module_Unneed( p_demux, p_id3 );
289     }
290
291     return VLC_SUCCESS;
292 }
293
294 /*****************************************************************************
295  * Demux: reads and demuxes data packets
296  *****************************************************************************
297  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
298  *****************************************************************************/
299 static int Demux( demux_t *p_demux )
300 {
301     demux_sys_t *p_sys = p_demux->p_sys;
302     block_t *p_block_in, *p_block_out;
303     if( p_sys->b_start )
304     {
305         p_sys->b_start = VLC_FALSE;
306         p_block_in = p_sys->p_block_in;
307         p_block_out = p_sys->p_block_out;
308     }
309     else
310     {
311         if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) )
312             == NULL )
313         {
314             return 0;
315         }
316         if( p_demux->p_sys->b_initial_sync_failed == VLC_TRUE )
317         {
318             p_block_in->i_pts = p_block_in->i_dts = 1;
319             /* Only try to resync once */
320             p_demux->p_sys->b_initial_sync_failed = 0;
321         }
322         else
323             p_block_in->i_pts = p_block_in->i_dts = 0;
324         p_block_out = p_sys->p_packetizer->pf_packetize(
325             p_sys->p_packetizer, &p_block_in );
326     }
327
328
329     while( p_block_out )
330     {
331         while( p_block_out )
332         {
333             block_t *p_next = p_block_out->p_next;
334
335             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
336
337             p_block_out->p_next = NULL;
338             p_sys->i_pts = p_block_out->i_pts;
339             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
340
341             p_block_out = p_next;
342         }
343         p_block_out = p_sys->p_packetizer->pf_packetize(
344             p_sys->p_packetizer, &p_block_in );
345     }
346     return 1;
347 }
348
349 /*****************************************************************************
350  * Close: frees unused data
351  *****************************************************************************/
352 static void Close( vlc_object_t * p_this )
353 {
354     demux_t     *p_demux = (demux_t*)p_this;
355     demux_sys_t *p_sys = p_demux->p_sys;
356
357     if( p_sys->meta ) vlc_meta_Delete( p_sys->meta );
358
359     if( p_sys->p_packetizer && p_sys->p_packetizer->p_module )
360         module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
361     if( p_sys->p_packetizer )
362         vlc_object_destroy( p_sys->p_packetizer );
363
364     free( p_sys );
365 }
366
367 /*****************************************************************************
368  * Control:
369  *****************************************************************************/
370 static int Control( demux_t *p_demux, int i_query, va_list args )
371 {
372     demux_sys_t *p_sys  = p_demux->p_sys;
373     int64_t *pi64;
374     vlc_meta_t **pp_meta;
375     int i_ret;
376
377     switch( i_query )
378     {
379         case DEMUX_GET_META:
380             pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
381             if( p_sys->meta ) *pp_meta = vlc_meta_Duplicate( p_sys->meta );
382             else *pp_meta = NULL;
383             return VLC_SUCCESS;
384
385         case DEMUX_GET_TIME:
386             pi64 = (int64_t*)va_arg( args, int64_t * );
387             *pi64 = p_sys->i_pts + p_sys->i_time_offset;
388             return VLC_SUCCESS;
389
390         case DEMUX_SET_TIME:
391             /* FIXME TODO: implement a high precision seek (with mp3 parsing)
392              * needed for multi-input */
393
394         default:
395             i_ret = demux2_vaControlHelper( p_demux->s, 0, -1,
396                                             p_sys->i_bitrate_avg, 1, i_query,
397                                             args );
398             if( !i_ret && p_sys->i_bitrate_avg > 0 &&
399                 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
400             {
401                 int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) /
402                     p_sys->i_bitrate_avg;
403
404                 /* Fix time_offset */
405                 if( i_time >= 0 ) p_sys->i_time_offset = i_time - p_sys->i_pts;
406             }
407             return i_ret;
408     }
409 }