]> git.sesse.net Git - vlc/blob - modules/demux/mpeg/es.c
Increased probing size for ac3/dts in wav (close #3298).
[vlc] / modules / demux / mpeg / es.c
1 /*****************************************************************************
2  * es.c : Generic audio ES input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2008 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_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_codec.h>
37 #include <vlc_codecs.h>
38 #include <vlc_input.h>
39
40 #include "../../codec/a52.h"
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  OpenAudio( vlc_object_t * );
46 static int  OpenVideo( vlc_object_t * );
47 static void Close    ( vlc_object_t * );
48
49 #define FPS_TEXT N_("Frames per Second")
50 #define FPS_LONGTEXT N_("This is the frame rate used as a fallback when " \
51     "playing MPEG video elementary streams.")
52
53 vlc_module_begin ()
54     set_category( CAT_INPUT )
55     set_subcategory( SUBCAT_INPUT_DEMUX )
56     set_description( N_("MPEG-I/II/4 / A52 / DTS / MLP audio" ) )
57     set_capability( "demux", 155 )
58     set_callbacks( OpenAudio, Close )
59
60     add_shortcut( "mpga" )
61     add_shortcut( "mp3" )
62
63     add_shortcut( "m4a" )
64     add_shortcut( "mp4a" )
65     add_shortcut( "aac" )
66
67     add_shortcut( "ac3" )
68     add_shortcut( "a52" )
69
70     add_shortcut( "eac3" )
71
72     add_shortcut( "dts" )
73
74     add_shortcut( "mlp" )
75     add_shortcut( "thd" )
76
77     add_submodule()
78     set_description( N_("MPEG-4 video" ) )
79     set_capability( "demux", 0 )
80     set_callbacks( OpenVideo, Close )
81     add_float( "es-fps", 25, NULL, FPS_TEXT, FPS_LONGTEXT, false )
82
83     add_shortcut( "m4v" )
84     add_shortcut( "mp4v" )
85 vlc_module_end ()
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90 static int Demux  ( demux_t * );
91 static int Control( demux_t *, int, va_list );
92
93 typedef struct
94 {
95     vlc_fourcc_t i_codec;
96     bool       b_use_word;
97     const char *psz_name;
98     int  (*pf_probe)( demux_t *p_demux, int64_t *pi_offset );
99     int  (*pf_init)( demux_t *p_demux );
100 } codec_t;
101
102 struct demux_sys_t
103 {
104     codec_t codec;
105
106     es_out_id_t *p_es;
107
108     bool  b_start;
109     decoder_t   *p_packetizer;
110
111     mtime_t     i_pts;
112     mtime_t     i_time_offset;
113     int64_t     i_bytes;
114
115     bool        b_big_endian;
116     bool        b_estimate_bitrate;
117     int         i_bitrate_avg;  /* extracted from Xing header */
118
119     bool b_initial_sync_failed;
120
121     int i_packet_size;
122
123     int64_t i_stream_offset;
124
125     float   f_fps;
126
127     /* Mpga specific */
128     struct
129     {
130         int i_frames;
131         int i_bytes;
132         int i_bitrate_avg;
133         int i_frame_samples;
134     } xing;
135 };
136
137 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset );
138 static int MpgaInit( demux_t *p_demux );
139
140 static int AacProbe( demux_t *p_demux, int64_t *pi_offset );
141 static int AacInit( demux_t *p_demux );
142
143 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset );
144 static int A52Probe( demux_t *p_demux, int64_t *pi_offset );
145 static int A52Init( demux_t *p_demux );
146
147 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset );
148 static int DtsInit( demux_t *p_demux );
149
150 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset );
151 static int MlpInit( demux_t *p_demux );
152
153 static const codec_t p_codecs[] = {
154     { VLC_CODEC_MP4A, false, "mp4 audio",  AacProbe,  AacInit },
155     { VLC_CODEC_MPGA, false, "mpeg audio", MpgaProbe, MpgaInit },
156     { VLC_CODEC_A52, true,  "a52 audio",  A52Probe,  A52Init },
157     { VLC_CODEC_EAC3, true,  "eac3 audio", EA52Probe, A52Init },
158     { VLC_CODEC_DTS, false, "dts audio",  DtsProbe,  DtsInit },
159     { VLC_CODEC_TRUEHD, false, "mlp audio",  MlpProbe,  MlpInit },
160
161     { 0, false, NULL, NULL, NULL }
162 };
163
164 static int VideoInit( demux_t *p_demux );
165
166 static const codec_t codec_m4v = {
167     VLC_CODEC_MP4V, false, "mp4 video", NULL,  VideoInit
168 };
169
170 /*****************************************************************************
171  * OpenCommon: initializes demux structures
172  *****************************************************************************/
173 static int OpenCommon( demux_t *p_demux,
174                        int i_cat, const codec_t *p_codec, int64_t i_bs_offset )
175 {
176     demux_sys_t *p_sys;
177
178     es_format_t fmt;
179
180     DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
181     memset( p_sys, 0, sizeof( demux_sys_t ) );
182     p_sys->codec = *p_codec;
183     p_sys->p_es = NULL;
184     p_sys->b_start = true;
185     p_sys->i_stream_offset = i_bs_offset;
186     p_sys->b_estimate_bitrate = true;
187     p_sys->i_bitrate_avg = 0;
188     p_sys->b_big_endian = false;
189     p_sys->f_fps = var_InheritFloat( p_demux, "es-fps" );
190
191     if( stream_Seek( p_demux->s, p_sys->i_stream_offset ) )
192     {
193         free( p_sys );
194         return VLC_EGENERIC;
195     }
196
197     if( p_sys->codec.pf_init( p_demux ) )
198     {
199         free( p_sys );
200         return VLC_EGENERIC;
201     }
202
203     msg_Dbg( p_demux, "detected format %4.4s", (const char*)&p_sys->codec.i_codec );
204
205     /* Load the audio packetizer */
206     es_format_Init( &fmt, i_cat, p_sys->codec.i_codec );
207     p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, p_sys->codec.psz_name );
208     if( !p_sys->p_packetizer )
209     {
210         free( p_sys );
211         return VLC_EGENERIC;
212     }
213     return VLC_SUCCESS;
214 }
215 static int OpenAudio( vlc_object_t *p_this )
216 {
217     demux_t *p_demux = (demux_t*)p_this;
218     for( int i = 0; p_codecs[i].i_codec != 0; i++ )
219     {
220         int64_t i_offset;
221         if( !p_codecs[i].pf_probe( p_demux, &i_offset ) )
222             return OpenCommon( p_demux, AUDIO_ES, &p_codecs[i], i_offset );
223     }
224     return VLC_EGENERIC;
225 }
226 static int OpenVideo( vlc_object_t *p_this )
227 {
228     demux_t *p_demux = (demux_t*)p_this;
229
230     /* Only m4v is supported for the moment */
231     bool b_m4v_ext    = demux_IsPathExtension( p_demux, ".m4v" );
232     bool b_m4v_forced = demux_IsForced( p_demux, "m4v" ) ||
233                         demux_IsForced( p_demux, "mp4v" );
234     if( !b_m4v_ext && !b_m4v_forced )
235         return VLC_EGENERIC;
236
237     const uint8_t *p_peek;
238     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
239         return VLC_EGENERIC;
240     if( p_peek[0] != 0x00 || p_peek[1] != 0x00 || p_peek[2] != 0x01 )
241     {
242         if( !b_m4v_forced)
243             return VLC_EGENERIC;
244         msg_Warn( p_demux,
245                   "this doesn't look like an MPEG ES stream, continuing anyway" );
246     }
247     return OpenCommon( p_demux, VIDEO_ES, &codec_m4v, 0 );
248 }
249 /*****************************************************************************
250  * Demux: reads and demuxes data packets
251  *****************************************************************************
252  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
253  *****************************************************************************/
254 static int Demux( demux_t *p_demux )
255 {
256     demux_sys_t *p_sys = p_demux->p_sys;
257     block_t *p_block_in, *p_block_out;
258
259     if( p_sys->codec.b_use_word )
260     {
261         /* Make sure we are word aligned */
262         int64_t i_pos = stream_Tell( p_demux->s );
263         if( i_pos % 2 )
264             stream_Read( p_demux->s, NULL, 1 );
265     }
266
267     if( ( p_block_in = stream_Block( p_demux->s, p_sys->i_packet_size ) ) == NULL )
268         return 0;
269
270     if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
271     {
272         /* Convert to big endian */
273         swab( p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer );
274     }
275
276     p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start || p_sys->b_initial_sync_failed ? VLC_TS_0 : VLC_TS_INVALID;
277     p_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
278
279     while( ( p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in ) ) )
280     {
281         p_sys->b_initial_sync_failed = false;
282         while( p_block_out )
283         {
284             block_t *p_next = p_block_out->p_next;
285
286             if( !p_sys->p_es )
287             {
288                 p_sys->p_packetizer->fmt_out.b_packetized = true;
289                 p_sys->p_es = es_out_Add( p_demux->out,
290                                           &p_sys->p_packetizer->fmt_out);
291
292
293                 /* Try the xing header */
294                 if( p_sys->xing.i_bytes && p_sys->xing.i_frames &&
295                     p_sys->xing.i_frame_samples )
296                 {
297                     p_sys->i_bitrate_avg = p_sys->xing.i_bytes * INT64_C(8) *
298                         p_sys->p_packetizer->fmt_out.audio.i_rate /
299                         p_sys->xing.i_frames / p_sys->xing.i_frame_samples;
300
301                     if( p_sys->i_bitrate_avg > 0 )
302                         p_sys->b_estimate_bitrate = false;
303                 }
304                 /* Use the bitrate as initual value */
305                 if( p_sys->b_estimate_bitrate )
306                     p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
307             }
308             if( p_sys->p_packetizer->fmt_out.i_cat == VIDEO_ES )
309             {
310                 if( p_block_out->i_pts <= VLC_TS_INVALID &&
311                     p_block_out->i_dts <= VLC_TS_INVALID )
312                     p_block_out->i_dts = VLC_TS_0 + p_sys->i_pts + 1000000 / p_sys->f_fps;
313                 if( p_block_out->i_dts > VLC_TS_INVALID )
314                     p_sys->i_pts = p_block_out->i_dts - VLC_TS_0;
315             }
316             else
317             {
318                 p_sys->i_pts = p_block_out->i_pts - VLC_TS_0;
319             }
320
321             /* Re-estimate bitrate */
322             if( p_sys->b_estimate_bitrate && p_sys->i_pts > INT64_C(500000) )
323                 p_sys->i_bitrate_avg = 8*INT64_C(1000000)*p_sys->i_bytes/(p_sys->i_pts-1);
324             p_sys->i_bytes += p_block_out->i_buffer;
325
326             /* Correct timestamp */
327             if( p_block_out->i_pts > VLC_TS_INVALID )
328             {
329                 p_block_out->i_pts += p_sys->i_time_offset;
330             }
331             if( p_block_out->i_dts > VLC_TS_INVALID )
332             {
333                 p_block_out->i_dts += p_sys->i_time_offset;
334                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
335             }
336
337             es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
338
339             p_block_out = p_next;
340         }
341     }
342
343     if( p_sys->b_initial_sync_failed )
344         msg_Dbg( p_demux, "did not sync on first block" );
345     p_sys->b_start = false;
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     demux_PacketizerDestroy( p_sys->p_packetizer );
358     free( p_sys );
359 }
360
361 /*****************************************************************************
362  * Control:
363  *****************************************************************************/
364 static int Control( demux_t *p_demux, int i_query, va_list args )
365 {
366     demux_sys_t *p_sys  = p_demux->p_sys;
367     int64_t *pi64;
368     bool *pb_bool;
369     int i_ret;
370     va_list args_save;
371
372     va_copy ( args_save, args );
373
374     switch( i_query )
375     {
376         case DEMUX_HAS_UNSUPPORTED_META:
377             pb_bool = (bool*)va_arg( args, bool* );
378             *pb_bool = true;
379             return VLC_SUCCESS;
380
381         case DEMUX_GET_TIME:
382             pi64 = (int64_t*)va_arg( args, int64_t * );
383             *pi64 = p_sys->i_pts + p_sys->i_time_offset;
384             return VLC_SUCCESS;
385
386         case DEMUX_GET_LENGTH:
387             i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
388                                             p_sys->i_bitrate_avg, 1, i_query,
389                                             args );
390             /* No bitrate, we can't have it precisely, but we can compute
391              * a raw approximation with time/position */
392             if( i_ret && !p_sys->i_bitrate_avg )
393             {
394                 float f_pos = (double)(uint64_t)( stream_Tell( p_demux->s ) ) /
395                               (double)(uint64_t)( stream_Size( p_demux->s ) );
396                 /* The first few seconds are guaranteed to be very whacky,
397                  * don't bother trying ... Too bad */
398                 if( f_pos < 0.01 ||
399                     (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
400                     return VLC_EGENERIC;
401
402                 pi64 = (int64_t *)va_arg( args_save, int64_t * );
403                 *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
404                 return VLC_SUCCESS;
405             }
406             va_end( args_save );
407             return i_ret;
408
409         case DEMUX_SET_TIME:
410             /* FIXME TODO: implement a high precision seek (with mp3 parsing)
411              * needed for multi-input */
412         default:
413             i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
414                                             p_sys->i_bitrate_avg, 1, i_query,
415                                             args );
416             if( !i_ret && p_sys->i_bitrate_avg > 0 &&
417                 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
418             {
419                 int64_t i_time = INT64_C(8000000) * ( stream_Tell(p_demux->s) - p_sys->i_stream_offset ) /
420                     p_sys->i_bitrate_avg;
421
422                 /* Fix time_offset */
423                 if( i_time >= 0 )
424                     p_sys->i_time_offset = i_time - p_sys->i_pts;
425             }
426             return i_ret;
427     }
428 }
429
430 /*****************************************************************************
431  * Wav header skipper
432  *****************************************************************************/
433 #define WAV_PROBE_SIZE (512*1024)
434 static int WavSkipHeader( demux_t *p_demux, int *pi_skip, const int pi_format[] )
435 {
436     const uint8_t *p_peek;
437     int         i_peek = 0;
438
439     /* */
440     *pi_skip = 0;
441
442     /* Check if we are dealing with a WAV file */
443     if( stream_Peek( p_demux->s, &p_peek, 12+8 ) != 12 + 8 )
444         return VLC_SUCCESS;
445
446     if( memcmp( p_peek, "RIFF", 4 ) || memcmp( &p_peek[8], "WAVE", 4 ) )
447         return VLC_SUCCESS;
448
449     /* Find the wave format header */
450     i_peek = 12 + 8;
451     while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
452     {
453         uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
454         if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
455             return VLC_EGENERIC;
456
457         i_peek += i_len + 8;
458         if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
459             return VLC_EGENERIC;
460     }
461
462     /* Sanity check the wave format header */
463     uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
464     if( i_len > WAV_PROBE_SIZE )
465         return VLC_EGENERIC;
466
467     i_peek += i_len + 8;
468     if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
469         return VLC_EGENERIC;
470     const int i_format = GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ );
471     int i_format_idx;
472     for( i_format_idx = 0; pi_format[i_format_idx] != WAVE_FORMAT_UNKNOWN; i_format_idx++ )
473     {
474         if( i_format == pi_format[i_format_idx] )
475             break;
476     }
477     if( pi_format[i_format_idx] == WAVE_FORMAT_UNKNOWN )
478         return VLC_EGENERIC;
479
480     if( i_format == WAVE_FORMAT_PCM )
481     {
482         if( GetWLE( p_peek + i_peek - i_len - 6 /* nChannels */ ) != 2 )
483             return VLC_EGENERIC;
484         if( GetDWLE( p_peek + i_peek - i_len - 4 /* nSamplesPerSec */ ) !=
485             44100 )
486             return VLC_EGENERIC;
487     }
488
489     /* Skip the wave header */
490     while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
491     {
492         uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
493         if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
494             return VLC_EGENERIC;
495
496         i_peek += i_len + 8;
497         if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
498             return VLC_EGENERIC;
499     }
500     *pi_skip = i_peek;
501     return VLC_SUCCESS;
502 }
503
504 static int GenericProbe( demux_t *p_demux, int64_t *pi_offset,
505                          const char * ppsz_name[],
506                          int (*pf_check)( const uint8_t *, int * ), int i_check_size,
507                          const int pi_wav_format[] )
508 {
509     bool   b_forced_demux;
510
511     int64_t i_offset;
512     const uint8_t *p_peek;
513     int i_skip;
514
515     b_forced_demux = false;
516     for( int i = 0; ppsz_name[i] != NULL; i++ )
517     {
518         b_forced_demux |= demux_IsForced( p_demux, ppsz_name[i] );
519     }
520
521     i_offset = stream_Tell( p_demux->s );
522
523     if( WavSkipHeader( p_demux, &i_skip, pi_wav_format ) )
524     {
525         if( !b_forced_demux )
526             return VLC_EGENERIC;
527     }
528     const bool b_wav = i_skip > 0;
529
530     /* peek the begining
531      * It is common that wav files have some sort of garbage at the begining
532      * We will accept probing 0.5s of data in this case.
533      */
534     const int i_probe = i_skip + i_check_size + 8000 + ( b_wav ? (44000/2*2*2) : 0);
535     const int i_peek = stream_Peek( p_demux->s, &p_peek, i_probe );
536     if( i_peek < i_skip + i_check_size )
537     {
538         msg_Err( p_demux, "cannot peek" );
539         return VLC_EGENERIC;
540     }
541     for( ;; )
542     {
543         if( i_skip + i_check_size > i_peek )
544         {
545             if( !b_forced_demux )
546                 return VLC_EGENERIC;
547             break;
548         }
549         int i_samples = 0;
550         int i_size = pf_check( &p_peek[i_skip], &i_samples );
551         if( i_size >= 0 )
552         {
553             if( i_size == 0 )
554                 break;
555
556             /* If we have the frame size, check the next frame for
557              * extra robustness
558              * The second test is because some .wav have paddings
559              */
560             bool b_ok = false;
561             for( int t = 0; t < 1 + !!b_wav; t++ )
562             {
563                 if( t == 1 )
564                     i_size = i_samples * 2 * 2;
565                 if( i_skip + i_check_size + i_size <= i_peek )
566                 {
567                     b_ok = pf_check( &p_peek[i_skip+i_size], NULL ) >= 0;
568                     if( b_ok )
569                         break;
570                 }
571             }
572             if( b_ok )
573                 break;
574         }
575         i_skip++;
576         if( !b_wav && !b_forced_demux )
577             return VLC_EGENERIC;
578     }
579
580     *pi_offset = i_offset + i_skip;
581     return VLC_SUCCESS;
582 }
583
584 /*****************************************************************************
585  * Mpeg I/II Audio
586  *****************************************************************************/
587 static int MpgaCheckSync( const uint8_t *p_peek )
588 {
589     uint32_t h = GetDWBE( p_peek );
590
591     if( ((( h >> 21 )&0x07FF) != 0x07FF )   /* header sync */
592         || (((h >> 17)&0x03) == 0 )         /* valid layer ?*/
593         || (((h >> 12)&0x0F) == 0x0F )
594         || (((h >> 12)&0x0F) == 0x00 )      /* valid bitrate ? */
595         || (((h >> 10) & 0x03) == 0x03 )    /* valide sampling freq ? */
596         || ((h & 0x03) == 0x02 ))           /* valid emphasis ? */
597     {
598         return false;
599     }
600     return true;
601 }
602
603 #define MPGA_VERSION( h )   ( 1 - (((h)>>19)&0x01) )
604 #define MPGA_MODE(h)        (((h)>> 6)&0x03)
605
606 static int MpgaGetFrameSamples( uint32_t h )
607 {
608     const int i_layer = 3 - (((h)>>17)&0x03);
609     switch( i_layer )
610     {
611     case 0:
612         return 384;
613     case 1:
614         return 1152;
615     case 2:
616         return MPGA_VERSION(h) ? 576 : 1152;
617     default:
618         return 0;
619     }
620 }
621
622 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset )
623 {
624     const int pi_wav[] = { WAVE_FORMAT_MPEG, WAVE_FORMAT_MPEGLAYER3, WAVE_FORMAT_UNKNOWN };
625     bool   b_forced;
626     bool   b_forced_demux;
627     int64_t i_offset;
628
629     const uint8_t *p_peek;
630     int i_skip;
631
632     b_forced = demux_IsPathExtension( p_demux, ".mp3" );
633     b_forced_demux = demux_IsForced( p_demux, "mp3" ) ||
634                      demux_IsForced( p_demux, "mpga" );
635
636     i_offset = stream_Tell( p_demux->s );
637
638     if( WavSkipHeader( p_demux, &i_skip, pi_wav ) )
639     {
640         if( !b_forced_demux )
641             return VLC_EGENERIC;
642
643         return VLC_EGENERIC;
644     }
645
646     if( stream_Peek( p_demux->s, &p_peek, i_skip + 4 ) < i_skip + 4 )
647         return VLC_EGENERIC;
648
649     if( !MpgaCheckSync( &p_peek[i_skip] ) )
650     {
651         bool b_ok = false;
652         int i_peek;
653
654         if( !b_forced_demux && !b_forced )
655             return VLC_EGENERIC;
656
657         i_peek = stream_Peek( p_demux->s, &p_peek, i_skip + 8096 );
658         while( i_skip + 4 < i_peek )
659         {
660             if( MpgaCheckSync( &p_peek[i_skip] ) )
661             {
662                 b_ok = true;
663                 break;
664             }
665             i_skip++;
666         }
667         if( !b_ok && !b_forced_demux )
668             return VLC_EGENERIC;
669     }
670     *pi_offset = i_offset + i_skip;
671     return VLC_SUCCESS;
672 }
673
674 static void MpgaXingSkip( const uint8_t **pp_xing, int *pi_xing, int i_count )
675 {
676     if(i_count > *pi_xing )
677         i_count = *pi_xing;
678
679     (*pp_xing) += i_count;
680     (*pi_xing) -= i_count;
681 }
682
683 static uint32_t MpgaXingGetDWBE( const uint8_t **pp_xing, int *pi_xing, uint32_t i_default )
684 {
685     if( *pi_xing < 4 )
686         return i_default;
687
688     uint32_t v = GetDWBE( *pp_xing );
689
690     MpgaXingSkip( pp_xing, pi_xing, 4 );
691
692     return v;
693 }
694
695 static int MpgaInit( demux_t *p_demux )
696 {
697     demux_sys_t *p_sys = p_demux->p_sys;
698
699     const uint8_t *p_peek;
700     int i_peek;
701
702     /* */
703     p_sys->i_packet_size = 1024;
704
705     /* Load a potential xing header */
706     i_peek = stream_Peek( p_demux->s, &p_peek, 4 + 1024 );
707     if( i_peek < 4 + 21 )
708         return VLC_SUCCESS;
709
710     const uint32_t header = GetDWBE( p_peek );
711     if( !MpgaCheckSync( p_peek ) )
712         return VLC_SUCCESS;
713
714     /* Xing header */
715     const uint8_t *p_xing = p_peek;
716     int i_xing = i_peek;
717     int i_skip;
718
719     if( MPGA_VERSION( header ) == 0 )
720         i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
721     else
722         i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
723
724     if( i_skip + 8 >= i_xing || memcmp( &p_xing[i_skip], "Xing", 4 ) )
725         return VLC_SUCCESS;
726
727     const uint32_t i_flags = GetDWBE( &p_xing[i_skip+4] );
728
729     MpgaXingSkip( &p_xing, &i_xing, i_skip + 8 );
730
731     if( i_flags&0x01 )
732         p_sys->xing.i_frames = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
733     if( i_flags&0x02 )
734         p_sys->xing.i_bytes = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
735     if( i_flags&0x04 ) /* TODO Support XING TOC to improve seeking accuracy */
736         MpgaXingSkip( &p_xing, &i_xing, 100 );
737     if( i_flags&0x08 )
738     {
739         /* FIXME: doesn't return the right bitrage average, at least
740            with some MP3's */
741         p_sys->xing.i_bitrate_avg = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
742         msg_Dbg( p_demux, "xing vbr value present (%d)",
743                  p_sys->xing.i_bitrate_avg );
744     }
745
746     if( p_sys->xing.i_frames > 0 && p_sys->xing.i_bytes > 0 )
747     {
748         p_sys->xing.i_frame_samples = MpgaGetFrameSamples( header );
749         msg_Dbg( p_demux, "xing frames&bytes value present "
750                  "(%d bytes, %d frames, %d samples/frame)",
751                  p_sys->xing.i_bytes, p_sys->xing.i_frames,
752                  p_sys->xing.i_frame_samples );
753     }
754     return VLC_SUCCESS;
755 }
756
757 /*****************************************************************************
758  * AAC
759  *****************************************************************************/
760 static int AacProbe( demux_t *p_demux, int64_t *pi_offset )
761 {
762     bool   b_forced;
763     bool   b_forced_demux;
764
765     int64_t i_offset;
766     const uint8_t *p_peek;
767
768     b_forced = demux_IsPathExtension( p_demux, ".aac" ) ||
769                demux_IsPathExtension( p_demux, ".aacp" );
770     b_forced_demux = demux_IsForced( p_demux, "m4a" ) ||
771                      demux_IsForced( p_demux, "aac" ) ||
772                      demux_IsForced( p_demux, "mp4a" );
773
774     if( !b_forced_demux && !b_forced )
775         return VLC_EGENERIC;
776
777     i_offset = stream_Tell( p_demux->s );
778
779     /* peek the begining (10 is for adts header) */
780     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
781     {
782         msg_Err( p_demux, "cannot peek" );
783         return VLC_EGENERIC;
784     }
785     if( !strncmp( (char *)p_peek, "ADIF", 4 ) )
786     {
787         msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
788         return VLC_EGENERIC;
789     }
790
791     *pi_offset = i_offset;
792     return VLC_SUCCESS;
793 }
794 static int AacInit( demux_t *p_demux )
795 {
796     demux_sys_t *p_sys = p_demux->p_sys;
797
798     p_sys->i_packet_size = 4096;
799
800     return VLC_SUCCESS;
801 }
802
803
804 /*****************************************************************************
805  * A52
806  *****************************************************************************/
807 static int A52CheckSync( const uint8_t *p_peek, bool *p_big_endian, int *pi_samples, bool b_eac3 )
808 {
809     vlc_a52_header_t header;
810     uint8_t p_tmp[VLC_A52_HEADER_SIZE];
811
812     *p_big_endian =  p_peek[0] == 0x0b && p_peek[1] == 0x77;
813     if( !*p_big_endian )
814     {
815         swab( p_peek, p_tmp, VLC_A52_HEADER_SIZE );
816         p_peek = p_tmp;
817     }
818
819     if( vlc_a52_header_Parse( &header, p_peek, VLC_A52_HEADER_SIZE ) )
820         return VLC_EGENERIC;
821
822     if( !header.b_eac3 != !b_eac3 )
823         return VLC_EGENERIC;
824     if( pi_samples )
825         *pi_samples = header.i_samples;
826     return header.i_size;
827 }
828 static int EA52CheckSyncProbe( const uint8_t *p_peek, int *pi_samples )
829 {
830     bool b_dummy;
831     return A52CheckSync( p_peek, &b_dummy, pi_samples, true );
832 }
833
834 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset )
835 {
836     const char *ppsz_name[] = { "eac3", NULL };
837     const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
838
839     return GenericProbe( p_demux, pi_offset, ppsz_name, EA52CheckSyncProbe, VLC_A52_HEADER_SIZE, pi_wav );
840 }
841
842 static int A52CheckSyncProbe( const uint8_t *p_peek, int *pi_samples )
843 {
844     bool b_dummy;
845     return A52CheckSync( p_peek, &b_dummy, pi_samples, false );
846 }
847
848 static int A52Probe( demux_t *p_demux, int64_t *pi_offset )
849 {
850     const char *ppsz_name[] = { "a52", "ac3", NULL };
851     const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
852
853     return GenericProbe( p_demux, pi_offset, ppsz_name, A52CheckSyncProbe, VLC_A52_HEADER_SIZE, pi_wav );
854 }
855
856 static int A52Init( demux_t *p_demux )
857 {
858     demux_sys_t *p_sys = p_demux->p_sys;
859
860     p_sys->b_big_endian = false;
861     p_sys->i_packet_size = 1024;
862
863     const uint8_t *p_peek;
864
865     /* peek the begining */
866     if( stream_Peek( p_demux->s, &p_peek, VLC_A52_HEADER_SIZE ) >= VLC_A52_HEADER_SIZE )
867     {
868         A52CheckSync( p_peek, &p_sys->b_big_endian, NULL, true );
869     }
870     return VLC_SUCCESS;
871 }
872
873 /*****************************************************************************
874  * DTS
875  *****************************************************************************/
876 static int DtsCheckSync( const uint8_t *p_peek, int *pi_samples )
877 {
878     /* TODO return frame size for robustness */
879
880     /* 14 bits, little endian version of the bitstream */
881     if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
882         p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
883         (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
884     {
885         return 0;
886     }
887     /* 14 bits, big endian version of the bitstream */
888     else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
889              p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
890              p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
891     {
892         return 0;
893     }
894     /* 16 bits, big endian version of the bitstream */
895     else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
896              p_peek[2] == 0x80 && p_peek[3] == 0x01 )
897     {
898         return 0;
899     }
900     /* 16 bits, little endian version of the bitstream */
901     else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
902              p_peek[2] == 0x01 && p_peek[3] == 0x80 )
903     {
904         return 0;
905     }
906
907     VLC_UNUSED(pi_samples);
908     return VLC_EGENERIC;
909 }
910
911 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset )
912 {
913     const char *ppsz_name[] = { "dts", NULL };
914     const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_DTS, WAVE_FORMAT_UNKNOWN };
915
916     return GenericProbe( p_demux, pi_offset, ppsz_name, DtsCheckSync, 11, pi_wav );
917 }
918 static int DtsInit( demux_t *p_demux )
919 {
920     demux_sys_t *p_sys = p_demux->p_sys;
921
922     p_sys->i_packet_size = 16384;
923
924     return VLC_SUCCESS;
925 }
926
927 /*****************************************************************************
928  * MLP
929  *****************************************************************************/
930 static int MlpCheckSync( const uint8_t *p_peek, int *pi_samples )
931 {
932     if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
933         return -1;
934
935     if( p_peek[4+3] != 0xba && p_peek[4+3] != 0xbb )
936         return -1;
937
938     /* TODO checksum and real size for robustness */
939     VLC_UNUSED(pi_samples);
940     return 0;
941 }
942 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset )
943 {
944     const char *ppsz_name[] = { "mlp", "thd", NULL };
945     const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_UNKNOWN };
946
947     return GenericProbe( p_demux, pi_offset, ppsz_name, MlpCheckSync, 4+28+16*4, pi_wav );
948 }
949 static int MlpInit( demux_t *p_demux )
950
951 {
952     demux_sys_t *p_sys = p_demux->p_sys;
953
954     p_sys->i_packet_size = 4096;
955
956     return VLC_SUCCESS;
957 }
958
959 /*****************************************************************************
960  * Video
961  *****************************************************************************/
962 static int VideoInit( demux_t *p_demux )
963 {
964     demux_sys_t *p_sys = p_demux->p_sys;
965
966     p_sys->i_packet_size = 4096;
967
968     return VLC_SUCCESS;
969 }