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