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