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