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 ThdProbe( demux_t *p_demux, int64_t *pi_offset );
145 static int MlpInit( demux_t *p_demux );
147 static bool Parse( demux_t *p_demux, block_t **pp_output );
149 static const codec_t p_codecs[] = {
150 { VLC_CODEC_MP4A, false, "mp4 audio", AacProbe, AacInit },
151 { VLC_CODEC_MPGA, false, "mpeg audio", MpgaProbe, MpgaInit },
152 { VLC_CODEC_A52, true, "a52 audio", A52Probe, A52Init },
153 { VLC_CODEC_EAC3, true, "eac3 audio", EA52Probe, A52Init },
154 { VLC_CODEC_DTS, false, "dts audio", DtsProbe, DtsInit },
155 { VLC_CODEC_MLP, false, "mlp audio", MlpProbe, MlpInit },
156 { VLC_CODEC_TRUEHD, false, "TrueHD audio", ThdProbe, MlpInit },
158 { 0, false, NULL, NULL, NULL }
161 static int VideoInit( demux_t *p_demux );
163 static const codec_t codec_m4v = {
164 VLC_CODEC_MP4V, false, "mp4 video", NULL, VideoInit
167 /*****************************************************************************
168 * OpenCommon: initializes demux structures
169 *****************************************************************************/
170 static int OpenCommon( demux_t *p_demux,
171 int i_cat, const codec_t *p_codec, int64_t i_bs_offset )
177 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
178 memset( p_sys, 0, sizeof( demux_sys_t ) );
179 p_sys->codec = *p_codec;
181 p_sys->b_start = true;
182 p_sys->i_stream_offset = i_bs_offset;
183 p_sys->b_estimate_bitrate = true;
184 p_sys->i_bitrate_avg = 0;
185 p_sys->b_big_endian = false;
186 p_sys->f_fps = var_InheritFloat( p_demux, "es-fps" );
187 p_sys->p_packetized_data = NULL;
189 if( stream_Seek( p_demux->s, p_sys->i_stream_offset ) )
195 if( p_sys->codec.pf_init( p_demux ) )
201 msg_Dbg( p_demux, "detected format %4.4s", (const char*)&p_sys->codec.i_codec );
203 /* Load the audio packetizer */
204 es_format_Init( &fmt, i_cat, p_sys->codec.i_codec );
205 p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, p_sys->codec.psz_name );
206 if( !p_sys->p_packetizer )
212 while( vlc_object_alive( p_demux ) )
214 if( Parse( p_demux, &p_sys->p_packetized_data ) )
216 if( p_sys->p_packetized_data )
221 static int OpenAudio( vlc_object_t *p_this )
223 demux_t *p_demux = (demux_t*)p_this;
224 for( int i = 0; p_codecs[i].i_codec != 0; i++ )
227 if( !p_codecs[i].pf_probe( p_demux, &i_offset ) )
228 return OpenCommon( p_demux, AUDIO_ES, &p_codecs[i], i_offset );
232 static int OpenVideo( vlc_object_t *p_this )
234 demux_t *p_demux = (demux_t*)p_this;
236 /* Only m4v is supported for the moment */
237 bool b_m4v_ext = demux_IsPathExtension( p_demux, ".m4v" );
238 bool b_m4v_forced = demux_IsForced( p_demux, "m4v" ) ||
239 demux_IsForced( p_demux, "mp4v" );
240 if( !b_m4v_ext && !b_m4v_forced )
243 const uint8_t *p_peek;
244 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
246 if( p_peek[0] != 0x00 || p_peek[1] != 0x00 || p_peek[2] != 0x01 )
251 "this doesn't look like an MPEG ES stream, continuing anyway" );
253 return OpenCommon( p_demux, VIDEO_ES, &codec_m4v, 0 );
255 /*****************************************************************************
256 * Demux: reads and demuxes data packets
257 *****************************************************************************
258 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
259 *****************************************************************************/
260 static int Demux( demux_t *p_demux )
263 demux_sys_t *p_sys = p_demux->p_sys;
265 block_t *p_block_out = p_sys->p_packetized_data;
267 p_sys->p_packetized_data = NULL;
269 ret = Parse( p_demux, &p_block_out ) ? 0 : 1;
273 block_t *p_next = p_block_out->p_next;
275 /* Correct timestamp */
276 if( p_sys->p_packetizer->fmt_out.i_cat == VIDEO_ES )
278 if( p_block_out->i_pts <= VLC_TS_INVALID &&
279 p_block_out->i_dts <= VLC_TS_INVALID )
280 p_block_out->i_dts = VLC_TS_0 + p_sys->i_pts + 1000000 / p_sys->f_fps;
281 if( p_block_out->i_dts > VLC_TS_INVALID )
282 p_sys->i_pts = p_block_out->i_dts - VLC_TS_0;
286 p_sys->i_pts = p_block_out->i_pts - VLC_TS_0;
289 if( p_block_out->i_pts > VLC_TS_INVALID )
291 p_block_out->i_pts += p_sys->i_time_offset;
293 if( p_block_out->i_dts > VLC_TS_INVALID )
295 p_block_out->i_dts += p_sys->i_time_offset;
296 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
298 /* Re-estimate bitrate */
299 if( p_sys->b_estimate_bitrate && p_sys->i_pts > INT64_C(500000) )
300 p_sys->i_bitrate_avg = 8*INT64_C(1000000)*p_sys->i_bytes/(p_sys->i_pts-1);
301 p_sys->i_bytes += p_block_out->i_buffer;
304 p_block_out->p_next = NULL;
305 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
307 p_block_out = p_next;
312 /*****************************************************************************
313 * Close: frees unused data
314 *****************************************************************************/
315 static void Close( vlc_object_t * p_this )
317 demux_t *p_demux = (demux_t*)p_this;
318 demux_sys_t *p_sys = p_demux->p_sys;
320 if( p_sys->p_packetized_data )
321 block_ChainRelease( p_sys->p_packetized_data );
322 demux_PacketizerDestroy( p_sys->p_packetizer );
326 /*****************************************************************************
328 *****************************************************************************/
329 static int Control( demux_t *p_demux, int i_query, va_list args )
331 demux_sys_t *p_sys = p_demux->p_sys;
338 case DEMUX_HAS_UNSUPPORTED_META:
339 pb_bool = (bool*)va_arg( args, bool* );
344 pi64 = (int64_t*)va_arg( args, int64_t * );
345 *pi64 = p_sys->i_pts + p_sys->i_time_offset;
348 case DEMUX_GET_LENGTH:
352 va_copy ( ap, args );
353 i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset,
354 -1, p_sys->i_bitrate_avg, 1, i_query, ap );
357 /* No bitrate, we can't have it precisely, but we can compute
358 * a raw approximation with time/position */
359 if( i_ret && !p_sys->i_bitrate_avg )
361 float f_pos = (double)(uint64_t)( stream_Tell( p_demux->s ) ) /
362 (double)(uint64_t)( stream_Size( p_demux->s ) );
363 /* The first few seconds are guaranteed to be very whacky,
364 * don't bother trying ... Too bad */
366 (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
371 pi64 = (int64_t *)va_arg( args, int64_t * );
372 *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
379 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
380 * needed for multi-input */
382 i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
383 p_sys->i_bitrate_avg, 1, i_query,
385 if( !i_ret && p_sys->i_bitrate_avg > 0 &&
386 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
388 int64_t i_time = INT64_C(8000000) * ( stream_Tell(p_demux->s) - p_sys->i_stream_offset ) /
389 p_sys->i_bitrate_avg;
391 /* Fix time_offset */
393 p_sys->i_time_offset = i_time - p_sys->i_pts;
394 /* And reset buffered data */
395 if( p_sys->p_packetized_data )
396 block_ChainRelease( p_sys->p_packetized_data );
397 p_sys->p_packetized_data = NULL;
403 /*****************************************************************************
404 * Makes a link list of buffer of parsed data
405 * Returns true if EOF
406 *****************************************************************************/
407 static bool Parse( demux_t *p_demux, block_t **pp_output )
409 demux_sys_t *p_sys = p_demux->p_sys;
410 block_t *p_block_in, *p_block_out;
414 if( p_sys->codec.b_use_word )
416 /* Make sure we are word aligned */
417 int64_t i_pos = stream_Tell( p_demux->s );
419 stream_Read( p_demux->s, NULL, 1 );
422 p_block_in = stream_Block( p_demux->s, p_sys->i_packet_size );
423 bool b_eof = p_block_in == NULL;
427 if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
429 /* Convert to big endian */
430 swab( p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer );
433 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;
435 p_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
437 while( ( p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, p_block_in ? &p_block_in : NULL ) ) )
439 p_sys->b_initial_sync_failed = false;
444 p_sys->p_packetizer->fmt_out.b_packetized = true;
445 p_sys->p_es = es_out_Add( p_demux->out,
446 &p_sys->p_packetizer->fmt_out);
449 /* Try the xing header */
450 if( p_sys->xing.i_bytes && p_sys->xing.i_frames &&
451 p_sys->xing.i_frame_samples )
453 p_sys->i_bitrate_avg = p_sys->xing.i_bytes * INT64_C(8) *
454 p_sys->p_packetizer->fmt_out.audio.i_rate /
455 p_sys->xing.i_frames / p_sys->xing.i_frame_samples;
457 if( p_sys->i_bitrate_avg > 0 )
458 p_sys->b_estimate_bitrate = false;
460 /* Use the bitrate as initual value */
461 if( p_sys->b_estimate_bitrate )
462 p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
465 block_t *p_next = p_block_out->p_next;
466 p_block_out->p_next = NULL;
468 block_ChainLastAppend( &pp_output, p_block_out );
470 p_block_out = p_next;
474 if( p_sys->b_initial_sync_failed )
475 msg_Dbg( p_demux, "did not sync on first block" );
476 p_sys->b_start = false;
481 /*****************************************************************************
483 *****************************************************************************/
484 #define WAV_PROBE_SIZE (512*1024)
485 static int WavSkipHeader( demux_t *p_demux, int *pi_skip, const int pi_format[] )
487 const uint8_t *p_peek;
493 /* Check if we are dealing with a WAV file */
494 if( stream_Peek( p_demux->s, &p_peek, 12+8 ) != 12 + 8 )
497 if( memcmp( p_peek, "RIFF", 4 ) || memcmp( &p_peek[8], "WAVE", 4 ) )
500 /* Find the wave format header */
502 while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
504 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
505 if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
509 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
513 /* Sanity check the wave format header */
514 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
515 if( i_len > WAV_PROBE_SIZE )
519 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
521 const int i_format = GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ );
523 for( i_format_idx = 0; pi_format[i_format_idx] != WAVE_FORMAT_UNKNOWN; i_format_idx++ )
525 if( i_format == pi_format[i_format_idx] )
528 if( pi_format[i_format_idx] == WAVE_FORMAT_UNKNOWN )
531 if( i_format == WAVE_FORMAT_PCM )
533 if( GetWLE( p_peek + i_peek - i_len - 6 /* nChannels */ ) != 2 )
535 if( GetDWLE( p_peek + i_peek - i_len - 4 /* nSamplesPerSec */ ) !=
540 /* Skip the wave header */
541 while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
543 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
544 if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
548 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
555 static int GenericProbe( demux_t *p_demux, int64_t *pi_offset,
556 const char * ppsz_name[],
557 int (*pf_check)( const uint8_t *, int * ), int i_check_size,
558 const int pi_wav_format[] )
563 const uint8_t *p_peek;
566 b_forced_demux = false;
567 for( int i = 0; ppsz_name[i] != NULL; i++ )
569 b_forced_demux |= demux_IsForced( p_demux, ppsz_name[i] );
572 i_offset = stream_Tell( p_demux->s );
574 if( WavSkipHeader( p_demux, &i_skip, pi_wav_format ) )
576 if( !b_forced_demux )
579 const bool b_wav = i_skip > 0;
582 * It is common that wav files have some sort of garbage at the begining
583 * We will accept probing 0.5s of data in this case.
585 const int i_probe = i_skip + i_check_size + 8000 + ( b_wav ? (44000/2*2*2) : 0);
586 const int i_peek = stream_Peek( p_demux->s, &p_peek, i_probe );
587 if( i_peek < i_skip + i_check_size )
589 msg_Err( p_demux, "cannot peek" );
594 if( i_skip + i_check_size > i_peek )
596 if( !b_forced_demux )
601 int i_size = pf_check( &p_peek[i_skip], &i_samples );
607 /* If we have the frame size, check the next frame for
609 * The second test is because some .wav have paddings
612 for( int t = 0; t < 1 + !!b_wav; t++ )
615 i_size = i_samples * 2 * 2;
616 if( i_skip + i_check_size + i_size <= i_peek )
618 b_ok = pf_check( &p_peek[i_skip+i_size], NULL ) >= 0;
627 if( !b_wav && !b_forced_demux )
631 *pi_offset = i_offset + i_skip;
635 /*****************************************************************************
637 *****************************************************************************/
638 static int MpgaCheckSync( const uint8_t *p_peek )
640 uint32_t h = GetDWBE( p_peek );
642 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
643 || (((h >> 19)&0x03) == 1 ) /* valid version ID ? */
644 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
645 || (((h >> 12)&0x0F) == 0x0F ) /* valid bitrate ?*/
646 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
647 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
654 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
655 #define MPGA_MODE(h) (((h)>> 6)&0x03)
657 static int MpgaGetFrameSamples( uint32_t h )
659 const int i_layer = 3 - (((h)>>17)&0x03);
667 return MPGA_VERSION(h) ? 576 : 1152;
673 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset )
675 const int pi_wav[] = { WAVE_FORMAT_MPEG, WAVE_FORMAT_MPEGLAYER3, WAVE_FORMAT_UNKNOWN };
680 const uint8_t *p_peek;
683 b_forced = demux_IsPathExtension( p_demux, ".mp3" );
684 b_forced_demux = demux_IsForced( p_demux, "mp3" ) ||
685 demux_IsForced( p_demux, "mpga" );
687 i_offset = stream_Tell( p_demux->s );
689 if( WavSkipHeader( p_demux, &i_skip, pi_wav ) )
691 if( !b_forced_demux )
697 if( stream_Peek( p_demux->s, &p_peek, i_skip + 4 ) < i_skip + 4 )
700 if( !MpgaCheckSync( &p_peek[i_skip] ) )
705 if( !b_forced_demux && !b_forced )
708 i_peek = stream_Peek( p_demux->s, &p_peek, i_skip + 8096 );
709 while( i_skip + 4 < i_peek )
711 if( MpgaCheckSync( &p_peek[i_skip] ) )
718 if( !b_ok && !b_forced_demux )
721 *pi_offset = i_offset + i_skip;
725 static void MpgaXingSkip( const uint8_t **pp_xing, int *pi_xing, int i_count )
727 if(i_count > *pi_xing )
730 (*pp_xing) += i_count;
731 (*pi_xing) -= i_count;
734 static uint32_t MpgaXingGetDWBE( const uint8_t **pp_xing, int *pi_xing, uint32_t i_default )
739 uint32_t v = GetDWBE( *pp_xing );
741 MpgaXingSkip( pp_xing, pi_xing, 4 );
746 static int MpgaInit( demux_t *p_demux )
748 demux_sys_t *p_sys = p_demux->p_sys;
750 const uint8_t *p_peek;
754 p_sys->i_packet_size = 1024;
756 /* Load a potential xing header */
757 i_peek = stream_Peek( p_demux->s, &p_peek, 4 + 1024 );
758 if( i_peek < 4 + 21 )
761 const uint32_t header = GetDWBE( p_peek );
762 if( !MpgaCheckSync( p_peek ) )
766 const uint8_t *p_xing = p_peek;
770 if( MPGA_VERSION( header ) == 0 )
771 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
773 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
775 if( i_skip + 8 >= i_xing || memcmp( &p_xing[i_skip], "Xing", 4 ) )
778 const uint32_t i_flags = GetDWBE( &p_xing[i_skip+4] );
780 MpgaXingSkip( &p_xing, &i_xing, i_skip + 8 );
783 p_sys->xing.i_frames = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
785 p_sys->xing.i_bytes = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
786 if( i_flags&0x04 ) /* TODO Support XING TOC to improve seeking accuracy */
787 MpgaXingSkip( &p_xing, &i_xing, 100 );
790 /* FIXME: doesn't return the right bitrage average, at least
792 p_sys->xing.i_bitrate_avg = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
793 msg_Dbg( p_demux, "xing vbr value present (%d)",
794 p_sys->xing.i_bitrate_avg );
797 if( p_sys->xing.i_frames > 0 && p_sys->xing.i_bytes > 0 )
799 p_sys->xing.i_frame_samples = MpgaGetFrameSamples( header );
800 msg_Dbg( p_demux, "xing frames&bytes value present "
801 "(%d bytes, %d frames, %d samples/frame)",
802 p_sys->xing.i_bytes, p_sys->xing.i_frames,
803 p_sys->xing.i_frame_samples );
808 /*****************************************************************************
810 *****************************************************************************/
811 static int AacProbe( demux_t *p_demux, int64_t *pi_offset )
817 const uint8_t *p_peek;
819 b_forced = demux_IsPathExtension( p_demux, ".aac" ) ||
820 demux_IsPathExtension( p_demux, ".aacp" );
821 b_forced_demux = demux_IsForced( p_demux, "m4a" ) ||
822 demux_IsForced( p_demux, "aac" ) ||
823 demux_IsForced( p_demux, "mp4a" );
825 if( !b_forced_demux && !b_forced )
828 i_offset = stream_Tell( p_demux->s );
830 /* peek the begining (10 is for adts header) */
831 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
833 msg_Err( p_demux, "cannot peek" );
836 if( !strncmp( (char *)p_peek, "ADIF", 4 ) )
838 msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
842 *pi_offset = i_offset;
845 static int AacInit( demux_t *p_demux )
847 demux_sys_t *p_sys = p_demux->p_sys;
849 p_sys->i_packet_size = 4096;
855 /*****************************************************************************
857 *****************************************************************************/
858 static int A52CheckSync( const uint8_t *p_peek, bool *p_big_endian, int *pi_samples, bool b_eac3 )
860 vlc_a52_header_t header;
861 uint8_t p_tmp[VLC_A52_HEADER_SIZE];
863 *p_big_endian = p_peek[0] == 0x0b && p_peek[1] == 0x77;
866 swab( p_peek, p_tmp, VLC_A52_HEADER_SIZE );
870 if( vlc_a52_header_Parse( &header, p_peek, VLC_A52_HEADER_SIZE ) )
873 if( !header.b_eac3 != !b_eac3 )
876 *pi_samples = header.i_samples;
877 return header.i_size;
879 static int EA52CheckSyncProbe( const uint8_t *p_peek, int *pi_samples )
882 return A52CheckSync( p_peek, &b_dummy, pi_samples, true );
885 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset )
887 const char *ppsz_name[] = { "eac3", NULL };
888 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
890 return GenericProbe( p_demux, pi_offset, ppsz_name, EA52CheckSyncProbe, VLC_A52_HEADER_SIZE, pi_wav );
893 static int A52CheckSyncProbe( const uint8_t *p_peek, int *pi_samples )
896 return A52CheckSync( p_peek, &b_dummy, pi_samples, false );
899 static int A52Probe( demux_t *p_demux, int64_t *pi_offset )
901 const char *ppsz_name[] = { "a52", "ac3", NULL };
902 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_A52, WAVE_FORMAT_UNKNOWN };
904 return GenericProbe( p_demux, pi_offset, ppsz_name, A52CheckSyncProbe, VLC_A52_HEADER_SIZE, pi_wav );
907 static int A52Init( demux_t *p_demux )
909 demux_sys_t *p_sys = p_demux->p_sys;
911 p_sys->b_big_endian = false;
912 p_sys->i_packet_size = 1024;
914 const uint8_t *p_peek;
916 /* peek the begining */
917 if( stream_Peek( p_demux->s, &p_peek, VLC_A52_HEADER_SIZE ) >= VLC_A52_HEADER_SIZE )
919 A52CheckSync( p_peek, &p_sys->b_big_endian, NULL, true );
924 /*****************************************************************************
926 *****************************************************************************/
927 static int DtsCheckSync( const uint8_t *p_peek, int *pi_samples )
929 unsigned int i_sample_rate, i_bit_rate, i_frame_length, i_audio_mode;
932 VLC_UNUSED(pi_samples);
934 int i_frame_size = GetSyncInfo( p_peek,
941 if( i_frame_size != VLC_EGENERIC && i_frame_size <= 8192 )
947 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset )
949 const char *ppsz_name[] = { "dts", NULL };
950 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_DTS, WAVE_FORMAT_UNKNOWN };
952 return GenericProbe( p_demux, pi_offset, ppsz_name, DtsCheckSync, 11, pi_wav );
954 static int DtsInit( demux_t *p_demux )
956 demux_sys_t *p_sys = p_demux->p_sys;
958 p_sys->i_packet_size = 16384;
963 /*****************************************************************************
965 *****************************************************************************/
966 static int MlpCheckSync( const uint8_t *p_peek, int *pi_samples )
968 if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
971 if( p_peek[4+3] != 0xbb )
974 /* TODO checksum and real size for robustness */
975 VLC_UNUSED(pi_samples);
978 static int ThdCheckSync( const uint8_t *p_peek, int *pi_samples )
980 if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
983 if( p_peek[4+3] != 0xba )
986 /* TODO checksum and real size for robustness */
987 VLC_UNUSED(pi_samples);
990 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset )
992 const char *ppsz_name[] = { "mlp", NULL };
993 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_UNKNOWN };
995 return GenericProbe( p_demux, pi_offset, ppsz_name, MlpCheckSync, 4+28+16*4, pi_wav );
997 static int ThdProbe( demux_t *p_demux, int64_t *pi_offset )
999 const char *ppsz_name[] = { "thd", NULL };
1000 const int pi_wav[] = { WAVE_FORMAT_PCM, WAVE_FORMAT_UNKNOWN };
1002 return GenericProbe( p_demux, pi_offset, ppsz_name, ThdCheckSync, 4+28+16*4, pi_wav );
1004 static int MlpInit( demux_t *p_demux )
1007 demux_sys_t *p_sys = p_demux->p_sys;
1009 p_sys->i_packet_size = 4096;
1014 /*****************************************************************************
1016 *****************************************************************************/
1017 static int VideoInit( demux_t *p_demux )
1019 demux_sys_t *p_sys = p_demux->p_sys;
1021 p_sys->i_packet_size = 4096;