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