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