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