1 /*****************************************************************************
2 * es.c : Generic audio ES input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2008 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
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"
41 #include "../../codec/dts_header.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int OpenAudio( vlc_object_t * );
47 static int OpenVideo( vlc_object_t * );
48 static void Close ( vlc_object_t * );
50 #define FPS_TEXT N_("Frames per Second")
51 #define FPS_LONGTEXT N_("This is the frame rate used as a fallback when " \
52 "playing MPEG video elementary streams.")
55 set_category( CAT_INPUT )
56 set_subcategory( SUBCAT_INPUT_DEMUX )
57 set_description( N_("MPEG-I/II/4 / A52 / DTS / MLP audio" ) )
58 set_shortname( N_("Audio ES") )
59 set_capability( "demux", 155 )
60 set_callbacks( OpenAudio, Close )
62 add_shortcut( "mpga", "mp3",
70 set_description( N_("MPEG-4 video" ) )
71 set_capability( "demux", 0 )
72 set_callbacks( OpenVideo, Close )
73 add_float( "es-fps", 25, FPS_TEXT, FPS_LONGTEXT, false )
76 add_shortcut( "mp4v" )
79 /*****************************************************************************
81 *****************************************************************************/
82 static int Demux ( demux_t * );
83 static int Control( demux_t *, int, va_list );
90 int (*pf_probe)( demux_t *p_demux, int64_t *pi_offset );
91 int (*pf_init)( demux_t *p_demux );
101 decoder_t *p_packetizer;
102 block_t *p_packetized_data;
105 mtime_t i_time_offset;
109 bool b_estimate_bitrate;
110 int i_bitrate_avg; /* extracted from Xing header */
112 bool b_initial_sync_failed;
116 int64_t i_stream_offset;
130 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset );
131 static int MpgaInit( demux_t *p_demux );
133 static int AacProbe( demux_t *p_demux, int64_t *pi_offset );
134 static int AacInit( demux_t *p_demux );
136 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset );
137 static int A52Probe( demux_t *p_demux, int64_t *pi_offset );
138 static int A52Init( demux_t *p_demux );
140 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset );
141 static int DtsInit( demux_t *p_demux );
143 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset );
144 static int MlpInit( demux_t *p_demux );
146 static bool Parse( demux_t *p_demux, block_t **pp_output );
148 static const codec_t p_codecs[] = {
149 { VLC_CODEC_MP4A, false, "mp4 audio", AacProbe, AacInit },
150 { VLC_CODEC_MPGA, false, "mpeg audio", MpgaProbe, MpgaInit },
151 { VLC_CODEC_A52, true, "a52 audio", A52Probe, A52Init },
152 { VLC_CODEC_EAC3, true, "eac3 audio", EA52Probe, A52Init },
153 { VLC_CODEC_DTS, false, "dts audio", DtsProbe, DtsInit },
154 { VLC_CODEC_TRUEHD, false, "mlp audio", MlpProbe, MlpInit },
156 { 0, false, NULL, NULL, NULL }
159 static int VideoInit( demux_t *p_demux );
161 static const codec_t codec_m4v = {
162 VLC_CODEC_MP4V, false, "mp4 video", NULL, VideoInit
165 /*****************************************************************************
166 * OpenCommon: initializes demux structures
167 *****************************************************************************/
168 static int OpenCommon( demux_t *p_demux,
169 int i_cat, const codec_t *p_codec, int64_t i_bs_offset )
175 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
176 memset( p_sys, 0, sizeof( demux_sys_t ) );
177 p_sys->codec = *p_codec;
179 p_sys->b_start = true;
180 p_sys->i_stream_offset = i_bs_offset;
181 p_sys->b_estimate_bitrate = true;
182 p_sys->i_bitrate_avg = 0;
183 p_sys->b_big_endian = false;
184 p_sys->f_fps = var_InheritFloat( p_demux, "es-fps" );
185 p_sys->p_packetized_data = NULL;
187 if( stream_Seek( p_demux->s, p_sys->i_stream_offset ) )
193 if( p_sys->codec.pf_init( p_demux ) )
199 msg_Dbg( p_demux, "detected format %4.4s", (const char*)&p_sys->codec.i_codec );
201 /* Load the audio packetizer */
202 es_format_Init( &fmt, i_cat, p_sys->codec.i_codec );
203 p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, p_sys->codec.psz_name );
204 if( !p_sys->p_packetizer )
210 while( vlc_object_alive( p_demux ) )
212 if( Parse( p_demux, &p_sys->p_packetized_data ) )
214 if( p_sys->p_packetized_data )
219 static int OpenAudio( vlc_object_t *p_this )
221 demux_t *p_demux = (demux_t*)p_this;
222 for( int i = 0; p_codecs[i].i_codec != 0; i++ )
225 if( !p_codecs[i].pf_probe( p_demux, &i_offset ) )
226 return OpenCommon( p_demux, AUDIO_ES, &p_codecs[i], i_offset );
230 static int OpenVideo( vlc_object_t *p_this )
232 demux_t *p_demux = (demux_t*)p_this;
234 /* Only m4v is supported for the moment */
235 bool b_m4v_ext = demux_IsPathExtension( p_demux, ".m4v" );
236 bool b_m4v_forced = demux_IsForced( p_demux, "m4v" ) ||
237 demux_IsForced( p_demux, "mp4v" );
238 if( !b_m4v_ext && !b_m4v_forced )
241 const uint8_t *p_peek;
242 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
244 if( p_peek[0] != 0x00 || p_peek[1] != 0x00 || p_peek[2] != 0x01 )
249 "this doesn't look like an MPEG ES stream, continuing anyway" );
251 return OpenCommon( p_demux, VIDEO_ES, &codec_m4v, 0 );
253 /*****************************************************************************
254 * Demux: reads and demuxes data packets
255 *****************************************************************************
256 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
257 *****************************************************************************/
258 static int Demux( demux_t *p_demux )
261 demux_sys_t *p_sys = p_demux->p_sys;
263 block_t *p_block_out = p_sys->p_packetized_data;
265 p_sys->p_packetized_data = NULL;
267 ret = Parse( p_demux, &p_block_out ) ? 0 : 1;
271 block_t *p_next = p_block_out->p_next;
273 /* Correct timestamp */
274 if( p_sys->p_packetizer->fmt_out.i_cat == VIDEO_ES )
276 if( p_block_out->i_pts <= VLC_TS_INVALID &&
277 p_block_out->i_dts <= VLC_TS_INVALID )
278 p_block_out->i_dts = VLC_TS_0 + p_sys->i_pts + 1000000 / p_sys->f_fps;
279 if( p_block_out->i_dts > VLC_TS_INVALID )
280 p_sys->i_pts = p_block_out->i_dts - VLC_TS_0;
284 p_sys->i_pts = p_block_out->i_pts - VLC_TS_0;
287 if( p_block_out->i_pts > VLC_TS_INVALID )
289 p_block_out->i_pts += p_sys->i_time_offset;
291 if( p_block_out->i_dts > VLC_TS_INVALID )
293 p_block_out->i_dts += p_sys->i_time_offset;
294 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
296 /* Re-estimate bitrate */
297 if( p_sys->b_estimate_bitrate && p_sys->i_pts > INT64_C(500000) )
298 p_sys->i_bitrate_avg = 8*INT64_C(1000000)*p_sys->i_bytes/(p_sys->i_pts-1);
299 p_sys->i_bytes += p_block_out->i_buffer;
302 p_block_out->p_next = NULL;
303 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
305 p_block_out = p_next;
310 /*****************************************************************************
311 * Close: frees unused data
312 *****************************************************************************/
313 static void Close( vlc_object_t * p_this )
315 demux_t *p_demux = (demux_t*)p_this;
316 demux_sys_t *p_sys = p_demux->p_sys;
318 if( p_sys->p_packetized_data )
319 block_ChainRelease( p_sys->p_packetized_data );
320 demux_PacketizerDestroy( p_sys->p_packetizer );
324 /*****************************************************************************
326 *****************************************************************************/
327 static int Control( demux_t *p_demux, int i_query, va_list args )
329 demux_sys_t *p_sys = p_demux->p_sys;
335 va_copy ( args_save, args );
339 case DEMUX_HAS_UNSUPPORTED_META:
340 pb_bool = (bool*)va_arg( args, bool* );
345 pi64 = (int64_t*)va_arg( args, int64_t * );
346 *pi64 = p_sys->i_pts + p_sys->i_time_offset;
349 case DEMUX_GET_LENGTH:
350 i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
351 p_sys->i_bitrate_avg, 1, i_query,
353 /* No bitrate, we can't have it precisely, but we can compute
354 * a raw approximation with time/position */
355 if( i_ret && !p_sys->i_bitrate_avg )
357 float f_pos = (double)(uint64_t)( stream_Tell( p_demux->s ) ) /
358 (double)(uint64_t)( stream_Size( p_demux->s ) );
359 /* The first few seconds are guaranteed to be very whacky,
360 * don't bother trying ... Too bad */
362 (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
365 pi64 = (int64_t *)va_arg( args_save, int64_t * );
366 *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
373 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
374 * needed for multi-input */
376 i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
377 p_sys->i_bitrate_avg, 1, i_query,
379 if( !i_ret && p_sys->i_bitrate_avg > 0 &&
380 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
382 int64_t i_time = INT64_C(8000000) * ( stream_Tell(p_demux->s) - p_sys->i_stream_offset ) /
383 p_sys->i_bitrate_avg;
385 /* Fix time_offset */
387 p_sys->i_time_offset = i_time - p_sys->i_pts;
388 /* And reset buffered data */
389 if( p_sys->p_packetized_data )
390 block_ChainRelease( p_sys->p_packetized_data );
391 p_sys->p_packetized_data = NULL;
397 /*****************************************************************************
398 * Makes a link list of buffer of parsed data
399 * Returns true if EOF
400 *****************************************************************************/
401 static bool Parse( demux_t *p_demux, block_t **pp_output )
403 demux_sys_t *p_sys = p_demux->p_sys;
404 block_t *p_block_in, *p_block_out;
408 if( p_sys->codec.b_use_word )
410 /* Make sure we are word aligned */
411 int64_t i_pos = stream_Tell( p_demux->s );
413 stream_Read( p_demux->s, NULL, 1 );
416 p_block_in = stream_Block( p_demux->s, p_sys->i_packet_size );
417 bool b_eof = p_block_in == NULL;
421 if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
423 /* Convert to big endian */
424 swab( p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer );
427 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;
429 p_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
431 while( ( p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, p_block_in ? &p_block_in : NULL ) ) )
433 p_sys->b_initial_sync_failed = false;
438 p_sys->p_packetizer->fmt_out.b_packetized = true;
439 p_sys->p_es = es_out_Add( p_demux->out,
440 &p_sys->p_packetizer->fmt_out);
443 /* Try the xing header */
444 if( p_sys->xing.i_bytes && p_sys->xing.i_frames &&
445 p_sys->xing.i_frame_samples )
447 p_sys->i_bitrate_avg = p_sys->xing.i_bytes * INT64_C(8) *
448 p_sys->p_packetizer->fmt_out.audio.i_rate /
449 p_sys->xing.i_frames / p_sys->xing.i_frame_samples;
451 if( p_sys->i_bitrate_avg > 0 )
452 p_sys->b_estimate_bitrate = false;
454 /* Use the bitrate as initual value */
455 if( p_sys->b_estimate_bitrate )
456 p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
459 block_t *p_next = p_block_out->p_next;
460 p_block_out->p_next = NULL;
462 block_ChainLastAppend( &pp_output, p_block_out );
464 p_block_out = p_next;
468 if( p_sys->b_initial_sync_failed )
469 msg_Dbg( p_demux, "did not sync on first block" );
470 p_sys->b_start = false;
475 /*****************************************************************************
477 *****************************************************************************/
478 #define WAV_PROBE_SIZE (512*1024)
479 static int WavSkipHeader( demux_t *p_demux, int *pi_skip, const int pi_format[] )
481 const uint8_t *p_peek;
487 /* Check if we are dealing with a WAV file */
488 if( stream_Peek( p_demux->s, &p_peek, 12+8 ) != 12 + 8 )
491 if( memcmp( p_peek, "RIFF", 4 ) || memcmp( &p_peek[8], "WAVE", 4 ) )
494 /* Find the wave format header */
496 while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
498 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
499 if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
503 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
507 /* Sanity check the wave format header */
508 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
509 if( i_len > WAV_PROBE_SIZE )
513 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
515 const int i_format = GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ );
517 for( i_format_idx = 0; pi_format[i_format_idx] != WAVE_FORMAT_UNKNOWN; i_format_idx++ )
519 if( i_format == pi_format[i_format_idx] )
522 if( pi_format[i_format_idx] == WAVE_FORMAT_UNKNOWN )
525 if( i_format == WAVE_FORMAT_PCM )
527 if( GetWLE( p_peek + i_peek - i_len - 6 /* nChannels */ ) != 2 )
529 if( GetDWLE( p_peek + i_peek - i_len - 4 /* nSamplesPerSec */ ) !=
534 /* Skip the wave header */
535 while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
537 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
538 if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
542 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
549 static int GenericProbe( demux_t *p_demux, int64_t *pi_offset,
550 const char * ppsz_name[],
551 int (*pf_check)( const uint8_t *, int * ), int i_check_size,
552 const int pi_wav_format[] )
557 const uint8_t *p_peek;
560 b_forced_demux = false;
561 for( int i = 0; ppsz_name[i] != NULL; i++ )
563 b_forced_demux |= demux_IsForced( p_demux, ppsz_name[i] );
566 i_offset = stream_Tell( p_demux->s );
568 if( WavSkipHeader( p_demux, &i_skip, pi_wav_format ) )
570 if( !b_forced_demux )
573 const bool b_wav = i_skip > 0;
576 * It is common that wav files have some sort of garbage at the begining
577 * We will accept probing 0.5s of data in this case.
579 const int i_probe = i_skip + i_check_size + 8000 + ( b_wav ? (44000/2*2*2) : 0);
580 const int i_peek = stream_Peek( p_demux->s, &p_peek, i_probe );
581 if( i_peek < i_skip + i_check_size )
583 msg_Err( p_demux, "cannot peek" );
588 if( i_skip + i_check_size > i_peek )
590 if( !b_forced_demux )
595 int i_size = pf_check( &p_peek[i_skip], &i_samples );
601 /* If we have the frame size, check the next frame for
603 * The second test is because some .wav have paddings
606 for( int t = 0; t < 1 + !!b_wav; t++ )
609 i_size = i_samples * 2 * 2;
610 if( i_skip + i_check_size + i_size <= i_peek )
612 b_ok = pf_check( &p_peek[i_skip+i_size], NULL ) >= 0;
621 if( !b_wav && !b_forced_demux )
625 *pi_offset = i_offset + i_skip;
629 /*****************************************************************************
631 *****************************************************************************/
632 static int MpgaCheckSync( const uint8_t *p_peek )
634 uint32_t h = GetDWBE( p_peek );
636 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
637 || (((h >> 19)&0x03) == 1 ) /* valid version ID ? */
638 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
639 || (((h >> 12)&0x0F) == 0x0F ) /* valid bitrate ?*/
640 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
641 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
648 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
649 #define MPGA_MODE(h) (((h)>> 6)&0x03)
651 static int MpgaGetFrameSamples( uint32_t h )
653 const int i_layer = 3 - (((h)>>17)&0x03);
661 return MPGA_VERSION(h) ? 576 : 1152;
667 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset )
669 const int pi_wav[] = { WAVE_FORMAT_MPEG, WAVE_FORMAT_MPEGLAYER3, WAVE_FORMAT_UNKNOWN };
674 const uint8_t *p_peek;
677 b_forced = demux_IsPathExtension( p_demux, ".mp3" );
678 b_forced_demux = demux_IsForced( p_demux, "mp3" ) ||
679 demux_IsForced( p_demux, "mpga" );
681 i_offset = stream_Tell( p_demux->s );
683 if( WavSkipHeader( p_demux, &i_skip, pi_wav ) )
685 if( !b_forced_demux )
691 if( stream_Peek( p_demux->s, &p_peek, i_skip + 4 ) < i_skip + 4 )
694 if( !MpgaCheckSync( &p_peek[i_skip] ) )
699 if( !b_forced_demux && !b_forced )
702 i_peek = stream_Peek( p_demux->s, &p_peek, i_skip + 8096 );
703 while( i_skip + 4 < i_peek )
705 if( MpgaCheckSync( &p_peek[i_skip] ) )
712 if( !b_ok && !b_forced_demux )
715 *pi_offset = i_offset + i_skip;
719 static void MpgaXingSkip( const uint8_t **pp_xing, int *pi_xing, int i_count )
721 if(i_count > *pi_xing )
724 (*pp_xing) += i_count;
725 (*pi_xing) -= i_count;
728 static uint32_t MpgaXingGetDWBE( const uint8_t **pp_xing, int *pi_xing, uint32_t i_default )
733 uint32_t v = GetDWBE( *pp_xing );
735 MpgaXingSkip( pp_xing, pi_xing, 4 );
740 static int MpgaInit( demux_t *p_demux )
742 demux_sys_t *p_sys = p_demux->p_sys;
744 const uint8_t *p_peek;
748 p_sys->i_packet_size = 1024;
750 /* Load a potential xing header */
751 i_peek = stream_Peek( p_demux->s, &p_peek, 4 + 1024 );
752 if( i_peek < 4 + 21 )
755 const uint32_t header = GetDWBE( p_peek );
756 if( !MpgaCheckSync( p_peek ) )
760 const uint8_t *p_xing = p_peek;
764 if( MPGA_VERSION( header ) == 0 )
765 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
767 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
769 if( i_skip + 8 >= i_xing || memcmp( &p_xing[i_skip], "Xing", 4 ) )
772 const uint32_t i_flags = GetDWBE( &p_xing[i_skip+4] );
774 MpgaXingSkip( &p_xing, &i_xing, i_skip + 8 );
777 p_sys->xing.i_frames = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
779 p_sys->xing.i_bytes = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
780 if( i_flags&0x04 ) /* TODO Support XING TOC to improve seeking accuracy */
781 MpgaXingSkip( &p_xing, &i_xing, 100 );
784 /* FIXME: doesn't return the right bitrage average, at least
786 p_sys->xing.i_bitrate_avg = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
787 msg_Dbg( p_demux, "xing vbr value present (%d)",
788 p_sys->xing.i_bitrate_avg );
791 if( p_sys->xing.i_frames > 0 && p_sys->xing.i_bytes > 0 )
793 p_sys->xing.i_frame_samples = MpgaGetFrameSamples( header );
794 msg_Dbg( p_demux, "xing frames&bytes value present "
795 "(%d bytes, %d frames, %d samples/frame)",
796 p_sys->xing.i_bytes, p_sys->xing.i_frames,
797 p_sys->xing.i_frame_samples );
802 /*****************************************************************************
804 *****************************************************************************/
805 static int AacProbe( demux_t *p_demux, int64_t *pi_offset )
811 const uint8_t *p_peek;
813 b_forced = demux_IsPathExtension( p_demux, ".aac" ) ||
814 demux_IsPathExtension( p_demux, ".aacp" );
815 b_forced_demux = demux_IsForced( p_demux, "m4a" ) ||
816 demux_IsForced( p_demux, "aac" ) ||
817 demux_IsForced( p_demux, "mp4a" );
819 if( !b_forced_demux && !b_forced )
822 i_offset = stream_Tell( p_demux->s );
824 /* peek the begining (10 is for adts header) */
825 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
827 msg_Err( p_demux, "cannot peek" );
830 if( !strncmp( (char *)p_peek, "ADIF", 4 ) )
832 msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
836 *pi_offset = i_offset;
839 static int AacInit( demux_t *p_demux )
841 demux_sys_t *p_sys = p_demux->p_sys;
843 p_sys->i_packet_size = 4096;
849 /*****************************************************************************
851 *****************************************************************************/
852 static int A52CheckSync( const uint8_t *p_peek, bool *p_big_endian, int *pi_samples, bool b_eac3 )
854 vlc_a52_header_t header;
855 uint8_t p_tmp[VLC_A52_HEADER_SIZE];
857 *p_big_endian = p_peek[0] == 0x0b && p_peek[1] == 0x77;
860 swab( p_peek, p_tmp, VLC_A52_HEADER_SIZE );
864 if( vlc_a52_header_Parse( &header, p_peek, VLC_A52_HEADER_SIZE ) )
867 if( !header.b_eac3 != !b_eac3 )
870 *pi_samples = header.i_samples;
871 return header.i_size;
873 static int EA52CheckSyncProbe( const uint8_t *p_peek, int *pi_samples )
876 return A52CheckSync( p_peek, &b_dummy, pi_samples, true );
879 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset )
881 const char *ppsz_name[] = { "eac3", NULL };
882 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
884 return GenericProbe( p_demux, pi_offset, ppsz_name, EA52CheckSyncProbe, VLC_A52_HEADER_SIZE, pi_wav );
887 static int A52CheckSyncProbe( const uint8_t *p_peek, int *pi_samples )
890 return A52CheckSync( p_peek, &b_dummy, pi_samples, false );
893 static int A52Probe( demux_t *p_demux, int64_t *pi_offset )
895 const char *ppsz_name[] = { "a52", "ac3", NULL };
896 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
898 return GenericProbe( p_demux, pi_offset, ppsz_name, A52CheckSyncProbe, VLC_A52_HEADER_SIZE, pi_wav );
901 static int A52Init( demux_t *p_demux )
903 demux_sys_t *p_sys = p_demux->p_sys;
905 p_sys->b_big_endian = false;
906 p_sys->i_packet_size = 1024;
908 const uint8_t *p_peek;
910 /* peek the begining */
911 if( stream_Peek( p_demux->s, &p_peek, VLC_A52_HEADER_SIZE ) >= VLC_A52_HEADER_SIZE )
913 A52CheckSync( p_peek, &p_sys->b_big_endian, NULL, true );
918 /*****************************************************************************
920 *****************************************************************************/
921 static int DtsCheckSync( const uint8_t *p_peek, int *pi_samples )
923 unsigned int i_sample_rate, i_bit_rate, i_frame_length, i_audio_mode;
926 VLC_UNUSED(pi_samples);
928 int i_frame_size = GetSyncInfo( p_peek,
935 if( i_frame_size != VLC_EGENERIC && i_frame_size <= 8192 )
941 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset )
943 const char *ppsz_name[] = { "dts", NULL };
944 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_DTS, WAVE_FORMAT_UNKNOWN };
946 return GenericProbe( p_demux, pi_offset, ppsz_name, DtsCheckSync, 11, pi_wav );
948 static int DtsInit( demux_t *p_demux )
950 demux_sys_t *p_sys = p_demux->p_sys;
952 p_sys->i_packet_size = 16384;
957 /*****************************************************************************
959 *****************************************************************************/
960 static int MlpCheckSync( const uint8_t *p_peek, int *pi_samples )
962 if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
965 if( p_peek[4+3] != 0xba && p_peek[4+3] != 0xbb )
968 /* TODO checksum and real size for robustness */
969 VLC_UNUSED(pi_samples);
972 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset )
974 const char *ppsz_name[] = { "mlp", "thd", NULL };
975 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_UNKNOWN };
977 return GenericProbe( p_demux, pi_offset, ppsz_name, MlpCheckSync, 4+28+16*4, pi_wav );
979 static int MlpInit( demux_t *p_demux )
982 demux_sys_t *p_sys = p_demux->p_sys;
984 p_sys->i_packet_size = 4096;
989 /*****************************************************************************
991 *****************************************************************************/
992 static int VideoInit( demux_t *p_demux )
994 demux_sys_t *p_sys = p_demux->p_sys;
996 p_sys->i_packet_size = 4096;