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