add_float: remove callback parameter
[vlc/asuraparaju-public.git] / modules / demux / mpeg / es.c
blob64e3750ba5a4bc39b8570536e7ee53171e582e38
1 /*****************************************************************************
2 * es.c : Generic audio ES input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2008 the VideoLAN team
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
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>
40 #include "../../codec/a52.h"
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 * );
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.")
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 )
60 add_shortcut( "mpga", "mp3",
61 "m4a", "mp4a", "aac",
62 "ac3", "a52",
63 "eac3",
64 "dts",
65 "mlp", "thd" )
67 add_submodule()
68 set_description( N_("MPEG-4 video" ) )
69 set_capability( "demux", 0 )
70 set_callbacks( OpenVideo, Close )
71 add_float( "es-fps", 25, FPS_TEXT, FPS_LONGTEXT, false )
73 add_shortcut( "m4v" )
74 add_shortcut( "mp4v" )
75 vlc_module_end ()
77 /*****************************************************************************
78 * Local prototypes
79 *****************************************************************************/
80 static int Demux ( demux_t * );
81 static int Control( demux_t *, int, va_list );
83 typedef struct
85 vlc_fourcc_t i_codec;
86 bool b_use_word;
87 const char *psz_name;
88 int (*pf_probe)( demux_t *p_demux, int64_t *pi_offset );
89 int (*pf_init)( demux_t *p_demux );
90 } codec_t;
92 struct demux_sys_t
94 codec_t codec;
96 es_out_id_t *p_es;
98 bool b_start;
99 decoder_t *p_packetizer;
101 mtime_t i_pts;
102 mtime_t i_time_offset;
103 int64_t i_bytes;
105 bool b_big_endian;
106 bool b_estimate_bitrate;
107 int i_bitrate_avg; /* extracted from Xing header */
109 bool b_initial_sync_failed;
111 int i_packet_size;
113 int64_t i_stream_offset;
115 float f_fps;
117 /* Mpga specific */
118 struct
120 int i_frames;
121 int i_bytes;
122 int i_bitrate_avg;
123 int i_frame_samples;
124 } xing;
127 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset );
128 static int MpgaInit( demux_t *p_demux );
130 static int AacProbe( demux_t *p_demux, int64_t *pi_offset );
131 static int AacInit( demux_t *p_demux );
133 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset );
134 static int A52Probe( demux_t *p_demux, int64_t *pi_offset );
135 static int A52Init( demux_t *p_demux );
137 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset );
138 static int DtsInit( demux_t *p_demux );
140 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset );
141 static int MlpInit( demux_t *p_demux );
143 static const codec_t p_codecs[] = {
144 { VLC_CODEC_MP4A, false, "mp4 audio", AacProbe, AacInit },
145 { VLC_CODEC_MPGA, false, "mpeg audio", MpgaProbe, MpgaInit },
146 { VLC_CODEC_A52, true, "a52 audio", A52Probe, A52Init },
147 { VLC_CODEC_EAC3, true, "eac3 audio", EA52Probe, A52Init },
148 { VLC_CODEC_DTS, false, "dts audio", DtsProbe, DtsInit },
149 { VLC_CODEC_TRUEHD, false, "mlp audio", MlpProbe, MlpInit },
151 { 0, false, NULL, NULL, NULL }
154 static int VideoInit( demux_t *p_demux );
156 static const codec_t codec_m4v = {
157 VLC_CODEC_MP4V, false, "mp4 video", NULL, VideoInit
160 /*****************************************************************************
161 * OpenCommon: initializes demux structures
162 *****************************************************************************/
163 static int OpenCommon( demux_t *p_demux,
164 int i_cat, const codec_t *p_codec, int64_t i_bs_offset )
166 demux_sys_t *p_sys;
168 es_format_t fmt;
170 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
171 memset( p_sys, 0, sizeof( demux_sys_t ) );
172 p_sys->codec = *p_codec;
173 p_sys->p_es = NULL;
174 p_sys->b_start = true;
175 p_sys->i_stream_offset = i_bs_offset;
176 p_sys->b_estimate_bitrate = true;
177 p_sys->i_bitrate_avg = 0;
178 p_sys->b_big_endian = false;
179 p_sys->f_fps = var_InheritFloat( p_demux, "es-fps" );
181 if( stream_Seek( p_demux->s, p_sys->i_stream_offset ) )
183 free( p_sys );
184 return VLC_EGENERIC;
187 if( p_sys->codec.pf_init( p_demux ) )
189 free( p_sys );
190 return VLC_EGENERIC;
193 msg_Dbg( p_demux, "detected format %4.4s", (const char*)&p_sys->codec.i_codec );
195 /* Load the audio packetizer */
196 es_format_Init( &fmt, i_cat, p_sys->codec.i_codec );
197 p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, p_sys->codec.psz_name );
198 if( !p_sys->p_packetizer )
200 free( p_sys );
201 return VLC_EGENERIC;
203 return VLC_SUCCESS;
205 static int OpenAudio( vlc_object_t *p_this )
207 demux_t *p_demux = (demux_t*)p_this;
208 for( int i = 0; p_codecs[i].i_codec != 0; i++ )
210 int64_t i_offset;
211 if( !p_codecs[i].pf_probe( p_demux, &i_offset ) )
212 return OpenCommon( p_demux, AUDIO_ES, &p_codecs[i], i_offset );
214 return VLC_EGENERIC;
216 static int OpenVideo( vlc_object_t *p_this )
218 demux_t *p_demux = (demux_t*)p_this;
220 /* Only m4v is supported for the moment */
221 bool b_m4v_ext = demux_IsPathExtension( p_demux, ".m4v" );
222 bool b_m4v_forced = demux_IsForced( p_demux, "m4v" ) ||
223 demux_IsForced( p_demux, "mp4v" );
224 if( !b_m4v_ext && !b_m4v_forced )
225 return VLC_EGENERIC;
227 const uint8_t *p_peek;
228 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
229 return VLC_EGENERIC;
230 if( p_peek[0] != 0x00 || p_peek[1] != 0x00 || p_peek[2] != 0x01 )
232 if( !b_m4v_forced)
233 return VLC_EGENERIC;
234 msg_Warn( p_demux,
235 "this doesn't look like an MPEG ES stream, continuing anyway" );
237 return OpenCommon( p_demux, VIDEO_ES, &codec_m4v, 0 );
239 /*****************************************************************************
240 * Demux: reads and demuxes data packets
241 *****************************************************************************
242 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
243 *****************************************************************************/
244 static int Demux( demux_t *p_demux )
246 demux_sys_t *p_sys = p_demux->p_sys;
247 block_t *p_block_in, *p_block_out;
249 if( p_sys->codec.b_use_word )
251 /* Make sure we are word aligned */
252 int64_t i_pos = stream_Tell( p_demux->s );
253 if( i_pos % 2 )
254 stream_Read( p_demux->s, NULL, 1 );
257 if( ( p_block_in = stream_Block( p_demux->s, p_sys->i_packet_size ) ) == NULL )
258 return 0;
260 if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
262 /* Convert to big endian */
263 swab( p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer );
266 p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start || p_sys->b_initial_sync_failed ? VLC_TS_0 : VLC_TS_INVALID;
267 p_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
269 while( ( p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in ) ) )
271 p_sys->b_initial_sync_failed = false;
272 while( p_block_out )
274 block_t *p_next = p_block_out->p_next;
276 if( !p_sys->p_es )
278 p_sys->p_packetizer->fmt_out.b_packetized = true;
279 p_sys->p_es = es_out_Add( p_demux->out,
280 &p_sys->p_packetizer->fmt_out);
283 /* Try the xing header */
284 if( p_sys->xing.i_bytes && p_sys->xing.i_frames &&
285 p_sys->xing.i_frame_samples )
287 p_sys->i_bitrate_avg = p_sys->xing.i_bytes * INT64_C(8) *
288 p_sys->p_packetizer->fmt_out.audio.i_rate /
289 p_sys->xing.i_frames / p_sys->xing.i_frame_samples;
291 if( p_sys->i_bitrate_avg > 0 )
292 p_sys->b_estimate_bitrate = false;
294 /* Use the bitrate as initual value */
295 if( p_sys->b_estimate_bitrate )
296 p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
298 if( p_sys->p_packetizer->fmt_out.i_cat == VIDEO_ES )
300 if( p_block_out->i_pts <= VLC_TS_INVALID &&
301 p_block_out->i_dts <= VLC_TS_INVALID )
302 p_block_out->i_dts = VLC_TS_0 + p_sys->i_pts + 1000000 / p_sys->f_fps;
303 if( p_block_out->i_dts > VLC_TS_INVALID )
304 p_sys->i_pts = p_block_out->i_dts - VLC_TS_0;
306 else
308 p_sys->i_pts = p_block_out->i_pts - VLC_TS_0;
311 /* Re-estimate bitrate */
312 if( p_sys->b_estimate_bitrate && p_sys->i_pts > INT64_C(500000) )
313 p_sys->i_bitrate_avg = 8*INT64_C(1000000)*p_sys->i_bytes/(p_sys->i_pts-1);
314 p_sys->i_bytes += p_block_out->i_buffer;
316 /* Correct timestamp */
317 if( p_block_out->i_pts > VLC_TS_INVALID )
319 p_block_out->i_pts += p_sys->i_time_offset;
321 if( p_block_out->i_dts > VLC_TS_INVALID )
323 p_block_out->i_dts += p_sys->i_time_offset;
324 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
327 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
329 p_block_out = p_next;
333 if( p_sys->b_initial_sync_failed )
334 msg_Dbg( p_demux, "did not sync on first block" );
335 p_sys->b_start = false;
336 return 1;
339 /*****************************************************************************
340 * Close: frees unused data
341 *****************************************************************************/
342 static void Close( vlc_object_t * p_this )
344 demux_t *p_demux = (demux_t*)p_this;
345 demux_sys_t *p_sys = p_demux->p_sys;
347 demux_PacketizerDestroy( p_sys->p_packetizer );
348 free( p_sys );
351 /*****************************************************************************
352 * Control:
353 *****************************************************************************/
354 static int Control( demux_t *p_demux, int i_query, va_list args )
356 demux_sys_t *p_sys = p_demux->p_sys;
357 int64_t *pi64;
358 bool *pb_bool;
359 int i_ret;
360 va_list args_save;
362 va_copy ( args_save, args );
364 switch( i_query )
366 case DEMUX_HAS_UNSUPPORTED_META:
367 pb_bool = (bool*)va_arg( args, bool* );
368 *pb_bool = true;
369 return VLC_SUCCESS;
371 case DEMUX_GET_TIME:
372 pi64 = (int64_t*)va_arg( args, int64_t * );
373 *pi64 = p_sys->i_pts + p_sys->i_time_offset;
374 return VLC_SUCCESS;
376 case DEMUX_GET_LENGTH:
377 i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
378 p_sys->i_bitrate_avg, 1, i_query,
379 args );
380 /* No bitrate, we can't have it precisely, but we can compute
381 * a raw approximation with time/position */
382 if( i_ret && !p_sys->i_bitrate_avg )
384 float f_pos = (double)(uint64_t)( stream_Tell( p_demux->s ) ) /
385 (double)(uint64_t)( stream_Size( p_demux->s ) );
386 /* The first few seconds are guaranteed to be very whacky,
387 * don't bother trying ... Too bad */
388 if( f_pos < 0.01 ||
389 (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
390 return VLC_EGENERIC;
392 pi64 = (int64_t *)va_arg( args_save, int64_t * );
393 *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
394 return VLC_SUCCESS;
396 va_end( args_save );
397 return i_ret;
399 case DEMUX_SET_TIME:
400 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
401 * needed for multi-input */
402 default:
403 i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
404 p_sys->i_bitrate_avg, 1, i_query,
405 args );
406 if( !i_ret && p_sys->i_bitrate_avg > 0 &&
407 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
409 int64_t i_time = INT64_C(8000000) * ( stream_Tell(p_demux->s) - p_sys->i_stream_offset ) /
410 p_sys->i_bitrate_avg;
412 /* Fix time_offset */
413 if( i_time >= 0 )
414 p_sys->i_time_offset = i_time - p_sys->i_pts;
416 return i_ret;
420 /*****************************************************************************
421 * Wav header skipper
422 *****************************************************************************/
423 #define WAV_PROBE_SIZE (512*1024)
424 static int WavSkipHeader( demux_t *p_demux, int *pi_skip, const int pi_format[] )
426 const uint8_t *p_peek;
427 int i_peek = 0;
429 /* */
430 *pi_skip = 0;
432 /* Check if we are dealing with a WAV file */
433 if( stream_Peek( p_demux->s, &p_peek, 12+8 ) != 12 + 8 )
434 return VLC_SUCCESS;
436 if( memcmp( p_peek, "RIFF", 4 ) || memcmp( &p_peek[8], "WAVE", 4 ) )
437 return VLC_SUCCESS;
439 /* Find the wave format header */
440 i_peek = 12 + 8;
441 while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
443 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
444 if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
445 return VLC_EGENERIC;
447 i_peek += i_len + 8;
448 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
449 return VLC_EGENERIC;
452 /* Sanity check the wave format header */
453 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
454 if( i_len > WAV_PROBE_SIZE )
455 return VLC_EGENERIC;
457 i_peek += i_len + 8;
458 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
459 return VLC_EGENERIC;
460 const int i_format = GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ );
461 int i_format_idx;
462 for( i_format_idx = 0; pi_format[i_format_idx] != WAVE_FORMAT_UNKNOWN; i_format_idx++ )
464 if( i_format == pi_format[i_format_idx] )
465 break;
467 if( pi_format[i_format_idx] == WAVE_FORMAT_UNKNOWN )
468 return VLC_EGENERIC;
470 if( i_format == WAVE_FORMAT_PCM )
472 if( GetWLE( p_peek + i_peek - i_len - 6 /* nChannels */ ) != 2 )
473 return VLC_EGENERIC;
474 if( GetDWLE( p_peek + i_peek - i_len - 4 /* nSamplesPerSec */ ) !=
475 44100 )
476 return VLC_EGENERIC;
479 /* Skip the wave header */
480 while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
482 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
483 if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
484 return VLC_EGENERIC;
486 i_peek += i_len + 8;
487 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
488 return VLC_EGENERIC;
490 *pi_skip = i_peek;
491 return VLC_SUCCESS;
494 static int GenericProbe( demux_t *p_demux, int64_t *pi_offset,
495 const char * ppsz_name[],
496 int (*pf_check)( const uint8_t *, int * ), int i_check_size,
497 const int pi_wav_format[] )
499 bool b_forced_demux;
501 int64_t i_offset;
502 const uint8_t *p_peek;
503 int i_skip;
505 b_forced_demux = false;
506 for( int i = 0; ppsz_name[i] != NULL; i++ )
508 b_forced_demux |= demux_IsForced( p_demux, ppsz_name[i] );
511 i_offset = stream_Tell( p_demux->s );
513 if( WavSkipHeader( p_demux, &i_skip, pi_wav_format ) )
515 if( !b_forced_demux )
516 return VLC_EGENERIC;
518 const bool b_wav = i_skip > 0;
520 /* peek the begining
521 * It is common that wav files have some sort of garbage at the begining
522 * We will accept probing 0.5s of data in this case.
524 const int i_probe = i_skip + i_check_size + 8000 + ( b_wav ? (44000/2*2*2) : 0);
525 const int i_peek = stream_Peek( p_demux->s, &p_peek, i_probe );
526 if( i_peek < i_skip + i_check_size )
528 msg_Err( p_demux, "cannot peek" );
529 return VLC_EGENERIC;
531 for( ;; )
533 if( i_skip + i_check_size > i_peek )
535 if( !b_forced_demux )
536 return VLC_EGENERIC;
537 break;
539 int i_samples = 0;
540 int i_size = pf_check( &p_peek[i_skip], &i_samples );
541 if( i_size >= 0 )
543 if( i_size == 0 )
544 break;
546 /* If we have the frame size, check the next frame for
547 * extra robustness
548 * The second test is because some .wav have paddings
550 bool b_ok = false;
551 for( int t = 0; t < 1 + !!b_wav; t++ )
553 if( t == 1 )
554 i_size = i_samples * 2 * 2;
555 if( i_skip + i_check_size + i_size <= i_peek )
557 b_ok = pf_check( &p_peek[i_skip+i_size], NULL ) >= 0;
558 if( b_ok )
559 break;
562 if( b_ok )
563 break;
565 i_skip++;
566 if( !b_wav && !b_forced_demux )
567 return VLC_EGENERIC;
570 *pi_offset = i_offset + i_skip;
571 return VLC_SUCCESS;
574 /*****************************************************************************
575 * Mpeg I/II Audio
576 *****************************************************************************/
577 static int MpgaCheckSync( const uint8_t *p_peek )
579 uint32_t h = GetDWBE( p_peek );
581 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
582 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
583 || (((h >> 12)&0x0F) == 0x0F )
584 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
585 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
586 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
588 return false;
590 return true;
593 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
594 #define MPGA_MODE(h) (((h)>> 6)&0x03)
596 static int MpgaGetFrameSamples( uint32_t h )
598 const int i_layer = 3 - (((h)>>17)&0x03);
599 switch( i_layer )
601 case 0:
602 return 384;
603 case 1:
604 return 1152;
605 case 2:
606 return MPGA_VERSION(h) ? 576 : 1152;
607 default:
608 return 0;
612 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset )
614 const int pi_wav[] = { WAVE_FORMAT_MPEG, WAVE_FORMAT_MPEGLAYER3, WAVE_FORMAT_UNKNOWN };
615 bool b_forced;
616 bool b_forced_demux;
617 int64_t i_offset;
619 const uint8_t *p_peek;
620 int i_skip;
622 b_forced = demux_IsPathExtension( p_demux, ".mp3" );
623 b_forced_demux = demux_IsForced( p_demux, "mp3" ) ||
624 demux_IsForced( p_demux, "mpga" );
626 i_offset = stream_Tell( p_demux->s );
628 if( WavSkipHeader( p_demux, &i_skip, pi_wav ) )
630 if( !b_forced_demux )
631 return VLC_EGENERIC;
633 return VLC_EGENERIC;
636 if( stream_Peek( p_demux->s, &p_peek, i_skip + 4 ) < i_skip + 4 )
637 return VLC_EGENERIC;
639 if( !MpgaCheckSync( &p_peek[i_skip] ) )
641 bool b_ok = false;
642 int i_peek;
644 if( !b_forced_demux && !b_forced )
645 return VLC_EGENERIC;
647 i_peek = stream_Peek( p_demux->s, &p_peek, i_skip + 8096 );
648 while( i_skip + 4 < i_peek )
650 if( MpgaCheckSync( &p_peek[i_skip] ) )
652 b_ok = true;
653 break;
655 i_skip++;
657 if( !b_ok && !b_forced_demux )
658 return VLC_EGENERIC;
660 *pi_offset = i_offset + i_skip;
661 return VLC_SUCCESS;
664 static void MpgaXingSkip( const uint8_t **pp_xing, int *pi_xing, int i_count )
666 if(i_count > *pi_xing )
667 i_count = *pi_xing;
669 (*pp_xing) += i_count;
670 (*pi_xing) -= i_count;
673 static uint32_t MpgaXingGetDWBE( const uint8_t **pp_xing, int *pi_xing, uint32_t i_default )
675 if( *pi_xing < 4 )
676 return i_default;
678 uint32_t v = GetDWBE( *pp_xing );
680 MpgaXingSkip( pp_xing, pi_xing, 4 );
682 return v;
685 static int MpgaInit( demux_t *p_demux )
687 demux_sys_t *p_sys = p_demux->p_sys;
689 const uint8_t *p_peek;
690 int i_peek;
692 /* */
693 p_sys->i_packet_size = 1024;
695 /* Load a potential xing header */
696 i_peek = stream_Peek( p_demux->s, &p_peek, 4 + 1024 );
697 if( i_peek < 4 + 21 )
698 return VLC_SUCCESS;
700 const uint32_t header = GetDWBE( p_peek );
701 if( !MpgaCheckSync( p_peek ) )
702 return VLC_SUCCESS;
704 /* Xing header */
705 const uint8_t *p_xing = p_peek;
706 int i_xing = i_peek;
707 int i_skip;
709 if( MPGA_VERSION( header ) == 0 )
710 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
711 else
712 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
714 if( i_skip + 8 >= i_xing || memcmp( &p_xing[i_skip], "Xing", 4 ) )
715 return VLC_SUCCESS;
717 const uint32_t i_flags = GetDWBE( &p_xing[i_skip+4] );
719 MpgaXingSkip( &p_xing, &i_xing, i_skip + 8 );
721 if( i_flags&0x01 )
722 p_sys->xing.i_frames = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
723 if( i_flags&0x02 )
724 p_sys->xing.i_bytes = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
725 if( i_flags&0x04 ) /* TODO Support XING TOC to improve seeking accuracy */
726 MpgaXingSkip( &p_xing, &i_xing, 100 );
727 if( i_flags&0x08 )
729 /* FIXME: doesn't return the right bitrage average, at least
730 with some MP3's */
731 p_sys->xing.i_bitrate_avg = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
732 msg_Dbg( p_demux, "xing vbr value present (%d)",
733 p_sys->xing.i_bitrate_avg );
736 if( p_sys->xing.i_frames > 0 && p_sys->xing.i_bytes > 0 )
738 p_sys->xing.i_frame_samples = MpgaGetFrameSamples( header );
739 msg_Dbg( p_demux, "xing frames&bytes value present "
740 "(%d bytes, %d frames, %d samples/frame)",
741 p_sys->xing.i_bytes, p_sys->xing.i_frames,
742 p_sys->xing.i_frame_samples );
744 return VLC_SUCCESS;
747 /*****************************************************************************
748 * AAC
749 *****************************************************************************/
750 static int AacProbe( demux_t *p_demux, int64_t *pi_offset )
752 bool b_forced;
753 bool b_forced_demux;
755 int64_t i_offset;
756 const uint8_t *p_peek;
758 b_forced = demux_IsPathExtension( p_demux, ".aac" ) ||
759 demux_IsPathExtension( p_demux, ".aacp" );
760 b_forced_demux = demux_IsForced( p_demux, "m4a" ) ||
761 demux_IsForced( p_demux, "aac" ) ||
762 demux_IsForced( p_demux, "mp4a" );
764 if( !b_forced_demux && !b_forced )
765 return VLC_EGENERIC;
767 i_offset = stream_Tell( p_demux->s );
769 /* peek the begining (10 is for adts header) */
770 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
772 msg_Err( p_demux, "cannot peek" );
773 return VLC_EGENERIC;
775 if( !strncmp( (char *)p_peek, "ADIF", 4 ) )
777 msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
778 return VLC_EGENERIC;
781 *pi_offset = i_offset;
782 return VLC_SUCCESS;
784 static int AacInit( demux_t *p_demux )
786 demux_sys_t *p_sys = p_demux->p_sys;
788 p_sys->i_packet_size = 4096;
790 return VLC_SUCCESS;
794 /*****************************************************************************
795 * A52
796 *****************************************************************************/
797 static int A52CheckSync( const uint8_t *p_peek, bool *p_big_endian, int *pi_samples, bool b_eac3 )
799 vlc_a52_header_t header;
800 uint8_t p_tmp[VLC_A52_HEADER_SIZE];
802 *p_big_endian = p_peek[0] == 0x0b && p_peek[1] == 0x77;
803 if( !*p_big_endian )
805 swab( p_peek, p_tmp, VLC_A52_HEADER_SIZE );
806 p_peek = p_tmp;
809 if( vlc_a52_header_Parse( &header, p_peek, VLC_A52_HEADER_SIZE ) )
810 return VLC_EGENERIC;
812 if( !header.b_eac3 != !b_eac3 )
813 return VLC_EGENERIC;
814 if( pi_samples )
815 *pi_samples = header.i_samples;
816 return header.i_size;
818 static int EA52CheckSyncProbe( const uint8_t *p_peek, int *pi_samples )
820 bool b_dummy;
821 return A52CheckSync( p_peek, &b_dummy, pi_samples, true );
824 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset )
826 const char *ppsz_name[] = { "eac3", NULL };
827 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
829 return GenericProbe( p_demux, pi_offset, ppsz_name, EA52CheckSyncProbe, VLC_A52_HEADER_SIZE, pi_wav );
832 static int A52CheckSyncProbe( const uint8_t *p_peek, int *pi_samples )
834 bool b_dummy;
835 return A52CheckSync( p_peek, &b_dummy, pi_samples, false );
838 static int A52Probe( demux_t *p_demux, int64_t *pi_offset )
840 const char *ppsz_name[] = { "a52", "ac3", NULL };
841 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
843 return GenericProbe( p_demux, pi_offset, ppsz_name, A52CheckSyncProbe, VLC_A52_HEADER_SIZE, pi_wav );
846 static int A52Init( demux_t *p_demux )
848 demux_sys_t *p_sys = p_demux->p_sys;
850 p_sys->b_big_endian = false;
851 p_sys->i_packet_size = 1024;
853 const uint8_t *p_peek;
855 /* peek the begining */
856 if( stream_Peek( p_demux->s, &p_peek, VLC_A52_HEADER_SIZE ) >= VLC_A52_HEADER_SIZE )
858 A52CheckSync( p_peek, &p_sys->b_big_endian, NULL, true );
860 return VLC_SUCCESS;
863 /*****************************************************************************
864 * DTS
865 *****************************************************************************/
866 static int DtsCheckSync( const uint8_t *p_peek, int *pi_samples )
868 /* TODO return frame size for robustness */
870 /* 14 bits, little endian version of the bitstream */
871 if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
872 p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
873 (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
875 return 0;
877 /* 14 bits, big endian version of the bitstream */
878 else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
879 p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
880 p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
882 return 0;
884 /* 16 bits, big endian version of the bitstream */
885 else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
886 p_peek[2] == 0x80 && p_peek[3] == 0x01 )
888 return 0;
890 /* 16 bits, little endian version of the bitstream */
891 else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
892 p_peek[2] == 0x01 && p_peek[3] == 0x80 )
894 return 0;
897 VLC_UNUSED(pi_samples);
898 return VLC_EGENERIC;
901 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset )
903 const char *ppsz_name[] = { "dts", NULL };
904 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_DTS, WAVE_FORMAT_UNKNOWN };
906 return GenericProbe( p_demux, pi_offset, ppsz_name, DtsCheckSync, 11, pi_wav );
908 static int DtsInit( demux_t *p_demux )
910 demux_sys_t *p_sys = p_demux->p_sys;
912 p_sys->i_packet_size = 16384;
914 return VLC_SUCCESS;
917 /*****************************************************************************
918 * MLP
919 *****************************************************************************/
920 static int MlpCheckSync( const uint8_t *p_peek, int *pi_samples )
922 if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
923 return -1;
925 if( p_peek[4+3] != 0xba && p_peek[4+3] != 0xbb )
926 return -1;
928 /* TODO checksum and real size for robustness */
929 VLC_UNUSED(pi_samples);
930 return 0;
932 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset )
934 const char *ppsz_name[] = { "mlp", "thd", NULL };
935 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_UNKNOWN };
937 return GenericProbe( p_demux, pi_offset, ppsz_name, MlpCheckSync, 4+28+16*4, pi_wav );
939 static int MlpInit( demux_t *p_demux )
942 demux_sys_t *p_sys = p_demux->p_sys;
944 p_sys->i_packet_size = 4096;
946 return VLC_SUCCESS;
949 /*****************************************************************************
950 * Video
951 *****************************************************************************/
952 static int VideoInit( demux_t *p_demux )
954 demux_sys_t *p_sys = p_demux->p_sys;
956 p_sys->i_packet_size = 4096;
958 return VLC_SUCCESS;