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 "../../packetizer/a52.h"
41 #include "../../packetizer/dts_header.h"
42 #include "../meta_engine/ID3Tag.h"
43 #include "../meta_engine/ID3Text.h"
44 #include "../meta_engine/ID3Meta.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int OpenAudio( vlc_object_t
* );
50 static int OpenVideo( vlc_object_t
* );
51 static void Close ( vlc_object_t
* );
53 #define FPS_TEXT N_("Frames per Second")
54 #define FPS_LONGTEXT N_("This is the frame rate used as a fallback when " \
55 "playing MPEG video elementary streams.")
58 set_category( CAT_INPUT
)
59 set_subcategory( SUBCAT_INPUT_DEMUX
)
60 set_description( N_("MPEG-I/II/4 / A52 / DTS / MLP audio" ) )
61 set_shortname( N_("Audio ES") )
62 set_capability( "demux", 155 )
63 set_callbacks( OpenAudio
, Close
)
65 add_shortcut( "mpga", "mp3",
73 set_description( N_("MPEG-4 video" ) )
74 set_capability( "demux", 5 )
75 set_callbacks( OpenVideo
, Close
)
76 add_float( "es-fps", 25, FPS_TEXT
, FPS_LONGTEXT
, false )
79 add_shortcut( "mp4v" )
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Demux ( demux_t
* );
86 static int Control( demux_t
*, int, va_list );
88 #define WAV_PROBE_SIZE (512*1024)
89 #define BASE_PROBE_SIZE (8000)
90 #define WAV_EXTRA_PROBE_SIZE (44000/2*2*2)
97 int (*pf_probe
)( demux_t
*p_demux
, uint64_t *pi_offset
);
98 int (*pf_init
)( demux_t
*p_demux
);
103 char psz_version
[10];
116 uint16_t i_frames_btw_refs
;
117 uint32_t i_bytes_btw_refs
;
118 uint32_t i_ms_btw_refs
;
119 uint8_t i_bits_per_bytes_dev
;
120 uint8_t i_bits_per_ms_dev
;
123 sync_table_ctx_t current
;
129 vlc_fourcc_t i_original
;
134 decoder_t
*p_packetizer
;
135 block_t
*p_packetized_data
;
138 vlc_tick_t i_time_offset
;
142 bool b_estimate_bitrate
;
143 int i_bitrate_avg
; /* extracted from Xing header */
145 bool b_initial_sync_failed
;
149 uint64_t i_stream_offset
;
164 float rgf_replay_gain
[AUDIO_REPLAY_GAIN_MAX
];
165 float rgf_replay_peak
[AUDIO_REPLAY_GAIN_MAX
];
170 static int MpgaProbe( demux_t
*p_demux
, uint64_t *pi_offset
);
171 static int MpgaInit( demux_t
*p_demux
);
173 static int AacProbe( demux_t
*p_demux
, uint64_t *pi_offset
);
174 static int AacInit( demux_t
*p_demux
);
176 static int EA52Probe( demux_t
*p_demux
, uint64_t *pi_offset
);
177 static int A52Probe( demux_t
*p_demux
, uint64_t *pi_offset
);
178 static int A52Init( demux_t
*p_demux
);
180 static int DtsProbe( demux_t
*p_demux
, uint64_t *pi_offset
);
181 static int DtsInit( demux_t
*p_demux
);
183 static int MlpProbe( demux_t
*p_demux
, uint64_t *pi_offset
);
184 static int ThdProbe( demux_t
*p_demux
, uint64_t *pi_offset
);
185 static int MlpInit( demux_t
*p_demux
);
187 static bool Parse( demux_t
*p_demux
, block_t
**pp_output
);
188 static uint64_t SeekByMlltTable( demux_t
*p_demux
, vlc_tick_t
*pi_time
);
190 static const codec_t p_codecs
[] = {
191 { VLC_CODEC_MP4A
, false, "mp4 audio", AacProbe
, AacInit
},
192 { VLC_CODEC_MPGA
, false, "mpeg audio", MpgaProbe
, MpgaInit
},
193 { VLC_CODEC_A52
, true, "a52 audio", A52Probe
, A52Init
},
194 { VLC_CODEC_EAC3
, true, "eac3 audio", EA52Probe
, A52Init
},
195 { VLC_CODEC_DTS
, false, "dts audio", DtsProbe
, DtsInit
},
196 { VLC_CODEC_MLP
, false, "mlp audio", MlpProbe
, MlpInit
},
197 { VLC_CODEC_TRUEHD
, false, "TrueHD audio", ThdProbe
, MlpInit
},
199 { 0, false, NULL
, NULL
, NULL
}
202 static int VideoInit( demux_t
*p_demux
);
204 static const codec_t codec_m4v
= {
205 VLC_CODEC_MP4V
, false, "mp4 video", NULL
, VideoInit
208 /*****************************************************************************
209 * OpenCommon: initializes demux structures
210 *****************************************************************************/
211 static int OpenCommon( demux_t
*p_demux
,
212 int i_cat
, const codec_t
*p_codec
, uint64_t i_bs_offset
)
218 DEMUX_INIT_COMMON(); p_sys
= p_demux
->p_sys
;
219 memset( p_sys
, 0, sizeof( demux_sys_t
) );
220 p_sys
->codec
= *p_codec
;
222 p_sys
->b_start
= true;
223 p_sys
->i_stream_offset
= i_bs_offset
;
224 p_sys
->b_estimate_bitrate
= true;
225 p_sys
->i_bitrate_avg
= 0;
226 p_sys
->b_big_endian
= false;
227 p_sys
->f_fps
= var_InheritFloat( p_demux
, "es-fps" );
228 p_sys
->p_packetized_data
= NULL
;
230 if( vlc_stream_Seek( p_demux
->s
, p_sys
->i_stream_offset
) )
236 if( p_sys
->codec
.pf_init( p_demux
) )
242 msg_Dbg( p_demux
, "detected format %4.4s", (const char*)&p_sys
->codec
.i_codec
);
244 /* Load the audio packetizer */
245 es_format_Init( &fmt
, i_cat
, p_sys
->codec
.i_codec
);
246 fmt
.i_original_fourcc
= p_sys
->i_original
;
247 p_sys
->p_packetizer
= demux_PacketizerNew( p_demux
, &fmt
, p_sys
->codec
.psz_name
);
248 if( !p_sys
->p_packetizer
)
254 es_format_t
*p_fmt
= &p_sys
->p_packetizer
->fmt_out
;
255 for( int i
= 0; i
< AUDIO_REPLAY_GAIN_MAX
; i
++ )
257 if ( p_sys
->rgf_replay_gain
[i
] != 0.0 )
259 p_fmt
->audio_replay_gain
.pb_gain
[i
] = true;
260 p_fmt
->audio_replay_gain
.pf_gain
[i
] = p_sys
->rgf_replay_gain
[i
];
262 if ( p_sys
->rgf_replay_peak
[i
] != 0.0 )
264 p_fmt
->audio_replay_gain
.pb_peak
[i
] = true;
265 p_fmt
->audio_replay_gain
.pf_peak
[i
] = p_sys
->rgf_replay_peak
[i
];
271 if( Parse( p_demux
, &p_sys
->p_packetized_data
) )
273 if( p_sys
->p_packetized_data
)
279 static int OpenAudio( vlc_object_t
*p_this
)
281 demux_t
*p_demux
= (demux_t
*)p_this
;
282 for( int i
= 0; p_codecs
[i
].i_codec
!= 0; i
++ )
285 if( !p_codecs
[i
].pf_probe( p_demux
, &i_offset
) )
286 return OpenCommon( p_demux
, AUDIO_ES
, &p_codecs
[i
], i_offset
);
290 static int OpenVideo( vlc_object_t
*p_this
)
292 demux_t
*p_demux
= (demux_t
*)p_this
;
294 /* Only m4v is supported for the moment */
295 bool b_m4v_ext
= demux_IsPathExtension( p_demux
, ".m4v" );
296 bool b_re4_ext
= !b_m4v_ext
&& demux_IsPathExtension( p_demux
, ".re4" );
297 bool b_m4v_forced
= demux_IsForced( p_demux
, "m4v" ) ||
298 demux_IsForced( p_demux
, "mp4v" );
300 if( !b_m4v_ext
&& !b_m4v_forced
&& !b_re4_ext
)
303 ssize_t i_off
= b_re4_ext
? 220 : 0;
304 const uint8_t *p_peek
;
305 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, i_off
+ 4 ) < i_off
+ 4 )
307 if( p_peek
[i_off
+ 0] != 0x00 || p_peek
[i_off
+ 1] != 0x00 || p_peek
[i_off
+ 2] != 0x01 )
312 "this doesn't look like an MPEG ES stream, continuing anyway" );
314 return OpenCommon( p_demux
, VIDEO_ES
, &codec_m4v
, i_off
);
316 /*****************************************************************************
317 * Demux: reads and demuxes data packets
318 *****************************************************************************
319 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
320 *****************************************************************************/
321 static int Demux( demux_t
*p_demux
)
324 demux_sys_t
*p_sys
= p_demux
->p_sys
;
326 block_t
*p_block_out
= p_sys
->p_packetized_data
;
328 p_sys
->p_packetized_data
= NULL
;
330 ret
= Parse( p_demux
, &p_block_out
) ? 0 : 1;
334 block_t
*p_next
= p_block_out
->p_next
;
336 /* Correct timestamp */
337 if( p_sys
->p_packetizer
->fmt_out
.i_cat
== VIDEO_ES
)
339 if( p_block_out
->i_pts
== VLC_TICK_INVALID
&&
340 p_block_out
->i_dts
== VLC_TICK_INVALID
)
341 p_block_out
->i_dts
= VLC_TICK_0
+ p_sys
->i_pts
+ VLC_TICK_FROM_SEC(1) / p_sys
->f_fps
;
342 if( p_block_out
->i_dts
!= VLC_TICK_INVALID
)
343 p_sys
->i_pts
= p_block_out
->i_dts
- VLC_TICK_0
;
347 p_sys
->i_pts
= p_block_out
->i_pts
- VLC_TICK_0
;
350 if( p_block_out
->i_pts
!= VLC_TICK_INVALID
)
352 p_block_out
->i_pts
+= p_sys
->i_time_offset
;
354 if( p_block_out
->i_dts
!= VLC_TICK_INVALID
)
356 p_block_out
->i_dts
+= p_sys
->i_time_offset
;
357 es_out_SetPCR( p_demux
->out
, p_block_out
->i_dts
);
359 /* Re-estimate bitrate */
360 if( p_sys
->b_estimate_bitrate
&& p_sys
->i_pts
> VLC_TICK_FROM_MS(500) )
361 p_sys
->i_bitrate_avg
= 8 * CLOCK_FREQ
* p_sys
->i_bytes
362 / (p_sys
->i_pts
- 1);
363 p_sys
->i_bytes
+= p_block_out
->i_buffer
;
366 p_block_out
->p_next
= NULL
;
367 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_block_out
);
369 p_block_out
= p_next
;
374 /*****************************************************************************
375 * Close: frees unused data
376 *****************************************************************************/
377 static void Close( vlc_object_t
* p_this
)
379 demux_t
*p_demux
= (demux_t
*)p_this
;
380 demux_sys_t
*p_sys
= p_demux
->p_sys
;
382 if( p_sys
->p_packetized_data
)
383 block_ChainRelease( p_sys
->p_packetized_data
);
384 if( p_sys
->mllt
.p_bits
)
385 free( p_sys
->mllt
.p_bits
);
386 demux_PacketizerDestroy( p_sys
->p_packetizer
);
390 /*****************************************************************************
392 *****************************************************************************/
393 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
395 demux_sys_t
*p_sys
= p_demux
->p_sys
;
400 case DEMUX_HAS_UNSUPPORTED_META
:
401 pb_bool
= va_arg( args
, bool * );
406 *va_arg( args
, vlc_tick_t
* ) = p_sys
->i_pts
+ p_sys
->i_time_offset
;
409 case DEMUX_GET_LENGTH
:
414 va_copy ( ap
, args
);
415 i_ret
= demux_vaControlHelper( p_demux
->s
, p_sys
->i_stream_offset
,
416 -1, p_sys
->i_bitrate_avg
, 1, i_query
, ap
);
419 /* No bitrate, we can't have it precisely, but we can compute
420 * a raw approximation with time/position */
421 if( i_ret
&& !p_sys
->i_bitrate_avg
)
423 float f_pos
= (double)(uint64_t)( vlc_stream_Tell( p_demux
->s
) ) /
424 (double)(uint64_t)( stream_Size( p_demux
->s
) );
425 /* The first few seconds are guaranteed to be very whacky,
426 * don't bother trying ... Too bad */
428 (p_sys
->i_pts
+ p_sys
->i_time_offset
) < VLC_TICK_FROM_SEC(8) )
433 *va_arg( args
, vlc_tick_t
* ) =
434 (p_sys
->i_pts
+ p_sys
->i_time_offset
) / f_pos
;
441 if( p_sys
->mllt
.p_bits
)
443 vlc_tick_t i_time
= va_arg(args
, vlc_tick_t
);
444 uint64_t i_pos
= SeekByMlltTable( p_demux
, &i_time
);
445 int i_ret
= vlc_stream_Seek( p_demux
->s
, p_sys
->i_stream_offset
+ i_pos
);
446 if( i_ret
!= VLC_SUCCESS
)
448 p_sys
->i_time_offset
= i_time
- p_sys
->i_pts
;
449 /* And reset buffered data */
450 if( p_sys
->p_packetized_data
)
451 block_ChainRelease( p_sys
->p_packetized_data
);
452 p_sys
->p_packetized_data
= NULL
;
455 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
456 * needed for multi-input */
460 int ret
= demux_vaControlHelper( p_demux
->s
, p_sys
->i_stream_offset
, -1,
461 p_sys
->i_bitrate_avg
, 1, i_query
, args
);
462 if( ret
!= VLC_SUCCESS
)
465 if( p_sys
->i_bitrate_avg
> 0
466 && (i_query
== DEMUX_SET_POSITION
|| i_query
== DEMUX_SET_TIME
) )
468 int64_t i_time
= INT64_C(8000000)
469 * ( vlc_stream_Tell(p_demux
->s
) - p_sys
->i_stream_offset
)
470 / p_sys
->i_bitrate_avg
;
472 /* Fix time_offset */
474 p_sys
->i_time_offset
= i_time
- p_sys
->i_pts
;
475 /* And reset buffered data */
476 if( p_sys
->p_packetized_data
)
477 block_ChainRelease( p_sys
->p_packetized_data
);
478 p_sys
->p_packetized_data
= NULL
;
483 /*****************************************************************************
484 * Makes a link list of buffer of parsed data
485 * Returns true if EOF
486 *****************************************************************************/
487 static bool Parse( demux_t
*p_demux
, block_t
**pp_output
)
489 demux_sys_t
*p_sys
= p_demux
->p_sys
;
490 block_t
*p_block_in
, *p_block_out
;
494 if( p_sys
->codec
.b_use_word
)
496 /* Make sure we are word aligned */
497 int64_t i_pos
= vlc_stream_Tell( p_demux
->s
);
498 if( (i_pos
& 1) && vlc_stream_Read( p_demux
->s
, NULL
, 1 ) != 1 )
502 p_block_in
= vlc_stream_Block( p_demux
->s
, p_sys
->i_packet_size
);
503 bool b_eof
= p_block_in
== NULL
;
507 if( p_sys
->codec
.b_use_word
&& !p_sys
->b_big_endian
&& p_block_in
->i_buffer
> 0 )
509 /* Convert to big endian */
510 block_t
*old
= p_block_in
;
511 p_block_in
= block_Alloc( p_block_in
->i_buffer
);
514 block_CopyProperties( p_block_in
, old
);
515 swab( old
->p_buffer
, p_block_in
->p_buffer
, old
->i_buffer
);
517 block_Release( old
);
523 p_block_in
->i_dts
= (p_sys
->b_start
|| p_sys
->b_initial_sync_failed
) ?
524 VLC_TICK_0
: VLC_TICK_INVALID
;
527 p_sys
->b_initial_sync_failed
= p_sys
->b_start
; /* Only try to resync once */
529 while( ( p_block_out
= p_sys
->p_packetizer
->pf_packetize( p_sys
->p_packetizer
, p_block_in
? &p_block_in
: NULL
) ) )
531 p_sys
->b_initial_sync_failed
= false;
536 p_sys
->p_packetizer
->fmt_out
.b_packetized
= true;
537 p_sys
->p_es
= es_out_Add( p_demux
->out
,
538 &p_sys
->p_packetizer
->fmt_out
);
541 /* Try the xing header */
542 if( p_sys
->xing
.i_bytes
&& p_sys
->xing
.i_frames
&&
543 p_sys
->xing
.i_frame_samples
)
545 p_sys
->i_bitrate_avg
= p_sys
->xing
.i_bytes
* INT64_C(8) *
546 p_sys
->p_packetizer
->fmt_out
.audio
.i_rate
/
547 p_sys
->xing
.i_frames
/ p_sys
->xing
.i_frame_samples
;
549 if( p_sys
->i_bitrate_avg
> 0 )
550 p_sys
->b_estimate_bitrate
= false;
552 /* Use the bitrate as initual value */
553 if( p_sys
->b_estimate_bitrate
)
554 p_sys
->i_bitrate_avg
= p_sys
->p_packetizer
->fmt_out
.i_bitrate
;
557 block_t
*p_next
= p_block_out
->p_next
;
558 p_block_out
->p_next
= NULL
;
560 block_ChainLastAppend( &pp_output
, p_block_out
);
562 p_block_out
= p_next
;
566 if( p_sys
->b_initial_sync_failed
)
567 msg_Dbg( p_demux
, "did not sync on first block" );
568 p_sys
->b_start
= false;
573 /* Check to apply to WAVE fmt header */
574 static int GenericFormatCheck( int i_format
, const uint8_t *p_head
)
576 if ( i_format
== WAVE_FORMAT_PCM
)
578 if( GetWLE( p_head
/* nChannels */ ) != 2 )
580 if( GetDWLE( p_head
+ 2 /* nSamplesPerSec */ ) != 44100 )
586 /*****************************************************************************
588 *****************************************************************************/
589 static int WavSkipHeader( demux_t
*p_demux
, uint64_t *pi_skip
,
590 const uint16_t rgi_twocc
[],
591 int (*pf_format_check
)( int, const uint8_t * ) )
593 const uint8_t *p_peek
;
600 /* Check if we are dealing with a WAV file */
601 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 12+8 ) != 12 + 8 )
604 if( memcmp( p_peek
, "RIFF", 4 ) || memcmp( &p_peek
[8], "WAVE", 4 ) )
607 /* Find the wave format header */
609 while( memcmp( p_peek
+ i_peek
- 8, "fmt ", 4 ) )
611 i_len
= GetDWLE( p_peek
+ i_peek
- 4 );
612 if( i_len
> WAV_PROBE_SIZE
|| i_peek
+ i_len
> WAV_PROBE_SIZE
)
616 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, i_peek
) != (ssize_t
) i_peek
)
620 /* Sanity check the wave format header */
621 i_len
= GetDWLE( p_peek
+ i_peek
- 4 );
622 if( i_len
> WAV_PROBE_SIZE
)
626 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, i_peek
) != (ssize_t
) i_peek
)
628 const uint16_t i_twocc
= GetWLE( p_peek
+ i_peek
- i_len
- 8 /* wFormatTag */ );
630 for( i_format_idx
= 0; rgi_twocc
[i_format_idx
] != WAVE_FORMAT_UNKNOWN
; i_format_idx
++ )
632 if( i_twocc
== rgi_twocc
[i_format_idx
] )
635 if( rgi_twocc
[i_format_idx
] == WAVE_FORMAT_UNKNOWN
)
638 if( pf_format_check
&&
639 pf_format_check( i_twocc
, p_peek
+ i_peek
- i_len
- 6 ) != VLC_SUCCESS
)
642 /* Skip the wave header */
643 while( memcmp( p_peek
+ i_peek
- 8, "data", 4 ) )
645 i_len
= GetDWLE( p_peek
+ i_peek
- 4 );
646 if( i_len
> WAV_PROBE_SIZE
|| i_peek
+ i_len
> WAV_PROBE_SIZE
)
650 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, i_peek
) != (ssize_t
) i_peek
)
657 static int GenericProbe( demux_t
*p_demux
, uint64_t *pi_offset
,
658 const char * ppsz_name
[],
659 int (*pf_check
)( const uint8_t *, unsigned * ),
660 unsigned i_check_size
,
661 unsigned i_base_probing
,
662 unsigned i_wav_extra_probing
,
664 const uint16_t pi_twocc
[],
665 int (*pf_format_check
)( int, const uint8_t * ) )
670 const uint8_t *p_peek
;
673 b_forced_demux
= false;
674 for( size_t i
= 0; ppsz_name
[i
] != NULL
; i
++ )
676 b_forced_demux
|= demux_IsForced( p_demux
, ppsz_name
[i
] );
679 i_offset
= vlc_stream_Tell( p_demux
->s
);
681 if( WavSkipHeader( p_demux
, &i_skip
, pi_twocc
, pf_format_check
) )
683 if( !b_forced_demux
)
686 const bool b_wav
= i_skip
> 0;
689 * It is common that wav files have some sort of garbage at the begining
690 * We will accept probing 0.5s of data in this case.
692 const size_t i_probe
= i_skip
+ i_check_size
+ i_base_probing
+ ( b_wav
? i_wav_extra_probing
: 0);
693 const size_t i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, i_probe
);
694 if( i_peek
< i_skip
+ i_check_size
)
696 msg_Dbg( p_demux
, "cannot peek" );
701 if( i_skip
+ i_check_size
> i_peek
)
703 if( !b_forced_demux
)
707 unsigned i_samples
= 0;
708 int i_size
= pf_check( &p_peek
[i_skip
], &i_samples
);
711 if( i_size
== 0 || /* 0 sized frame ?? */
712 i_skip
== 0 /* exact match from start, we're not WAVE either, so skip multiple checks (would break if padding) */ )
715 /* If we have the frame size, check the next frame for
717 * The second test is because some .wav have paddings
720 for( int t
= 0; t
< 1 + !!b_wav
; t
++ )
726 i_size
= i_samples
* 2 * 2;
729 if( i_skip
+ i_check_size
+ i_size
<= i_peek
)
731 b_ok
= pf_check( &p_peek
[i_skip
+i_size
], NULL
) >= 0;
740 i_skip
+= ((i_offset
+ i_skip
) % 2 == 0) ? 2 : 1;
743 if( !b_wav
&& !b_forced_demux
)
747 *pi_offset
= i_offset
+ i_skip
;
751 /*****************************************************************************
753 *****************************************************************************/
754 static int MpgaCheckSync( const uint8_t *p_peek
)
756 uint32_t h
= GetDWBE( p_peek
);
758 if( ((( h
>> 21 )&0x07FF) != 0x07FF ) /* header sync */
759 || (((h
>> 19)&0x03) == 1 ) /* valid version ID ? */
760 || (((h
>> 17)&0x03) == 0 ) /* valid layer ?*/
761 || (((h
>> 12)&0x0F) == 0x0F ) /* valid bitrate ?*/
762 || (((h
>> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
763 || ((h
& 0x03) == 0x02 )) /* valid emphasis ? */
770 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
771 #define MPGA_MODE(h) (((h)>> 6)&0x03)
773 static int MpgaGetFrameSamples( uint32_t h
)
775 const int i_layer
= 3 - (((h
)>>17)&0x03);
783 return MPGA_VERSION(h
) ? 576 : 1152;
789 static int MpgaProbe( demux_t
*p_demux
, uint64_t *pi_offset
)
791 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_MPEG
, WAVE_FORMAT_MPEGLAYER3
, WAVE_FORMAT_UNKNOWN
};
796 const uint8_t *p_peek
;
800 b_forced
= demux_IsPathExtension( p_demux
, ".mp3" );
801 b_forced_demux
= demux_IsForced( p_demux
, "mp3" ) ||
802 demux_IsForced( p_demux
, "mpga" );
804 i_offset
= vlc_stream_Tell( p_demux
->s
);
806 if( WavSkipHeader( p_demux
, &i_skip
, rgi_twocc
, NULL
) )
808 if( !b_forced_demux
)
814 i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, i_skip
+ 4 );
815 if( i_peek
<= 0 || (uint64_t) i_peek
< i_skip
+ 4 )
818 if( !MpgaCheckSync( &p_peek
[i_skip
] ) )
822 if( !b_forced_demux
&& !b_forced
)
825 i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, i_skip
+ 8096 );
826 while( i_peek
> 0 && i_skip
+ 4 < (uint64_t) i_peek
)
828 if( MpgaCheckSync( &p_peek
[i_skip
] ) )
835 if( !b_ok
&& !b_forced_demux
)
838 *pi_offset
= i_offset
+ i_skip
;
842 static void MpgaXingSkip( const uint8_t **pp_xing
, int *pi_xing
, int i_count
)
844 if(i_count
> *pi_xing
)
847 (*pp_xing
) += i_count
;
848 (*pi_xing
) -= i_count
;
851 static uint32_t MpgaXingGetDWBE( const uint8_t **pp_xing
, int *pi_xing
, uint32_t i_default
)
856 uint32_t v
= GetDWBE( *pp_xing
);
858 MpgaXingSkip( pp_xing
, pi_xing
, 4 );
863 static uint16_t MpgaXingGetWBE( const uint8_t **pp_xing
, int *pi_xing
, uint16_t i_default
)
868 uint16_t v
= GetWBE( *pp_xing
);
870 MpgaXingSkip( pp_xing
, pi_xing
, 2 );
875 static double MpgaXingLameConvertGain( uint16_t x
)
877 double gain
= (x
& 0x1FF) / 10.0;
879 return x
& 0x200 ? -gain
: gain
;
882 static double MpgaXingLameConvertPeak( uint32_t x
)
884 return x
/ 8388608.0; /* pow(2, 23) */
887 static uint64_t SeekByMlltTable( demux_t
*p_demux
, vlc_tick_t
*pi_time
)
889 demux_sys_t
*p_sys
= p_demux
->p_sys
;
890 sync_table_ctx_t
*p_cur
= &p_sys
->mllt
.current
;
892 /* reset or init context */
893 if( *pi_time
< p_cur
->i_time
|| !p_cur
->br
.p
)
897 bs_init(&p_cur
->br
, p_sys
->mllt
.p_bits
, p_sys
->mllt
.i_bits
);
900 while(bs_remain(&p_cur
->br
) >= p_sys
->mllt
.i_bits_per_bytes_dev
+ p_sys
->mllt
.i_bits_per_ms_dev
)
902 const uint32_t i_bytesdev
= bs_read(&p_cur
->br
, p_sys
->mllt
.i_bits_per_bytes_dev
);
903 const uint32_t i_msdev
= bs_read(&p_cur
->br
, p_sys
->mllt
.i_bits_per_ms_dev
);
904 const vlc_tick_t i_deltatime
= VLC_TICK_FROM_MS(p_sys
->mllt
.i_ms_btw_refs
+ i_msdev
);
905 if( p_cur
->i_time
+ i_deltatime
> *pi_time
)
907 p_cur
->i_time
+= i_deltatime
;
908 p_cur
->i_pos
+= p_sys
->mllt
.i_bytes_btw_refs
+ i_bytesdev
;
910 *pi_time
= p_cur
->i_time
;
914 static int ID3TAG_Parse_Handler( uint32_t i_tag
, const uint8_t *p_payload
, size_t i_payload
, void *p_priv
)
916 demux_t
*p_demux
= (demux_t
*) p_priv
;
917 demux_sys_t
*p_sys
= p_demux
->p_sys
;
919 if( i_tag
== VLC_FOURCC('M', 'L', 'L', 'T') )
923 p_sys
->mllt
.i_frames_btw_refs
= GetWBE(p_payload
);
924 p_sys
->mllt
.i_bytes_btw_refs
= GetDWBE(&p_payload
[1]) & 0x00FFFFFF;
925 p_sys
->mllt
.i_ms_btw_refs
= GetDWBE(&p_payload
[4]) & 0x00FFFFFF;
926 if( !p_sys
->mllt
.i_frames_btw_refs
|| !p_sys
->mllt
.i_bytes_btw_refs
||
927 !p_sys
->mllt
.i_ms_btw_refs
||
928 p_payload
[8] > 31 || p_payload
[9] > 31 || /* bits length sanity check */
929 ((p_payload
[8] + p_payload
[9]) % 4) || p_payload
[8] + p_payload
[9] < 4 )
931 p_sys
->mllt
.i_bits_per_bytes_dev
= p_payload
[8];
932 p_sys
->mllt
.i_bits_per_ms_dev
= p_payload
[9];
933 p_sys
->mllt
.p_bits
= malloc(i_payload
- 10);
934 if( likely(p_sys
->mllt
.p_bits
) )
936 p_sys
->mllt
.i_bits
= i_payload
- 10;
937 memcpy(p_sys
->mllt
.p_bits
, &p_payload
[10], p_sys
->mllt
.i_bits
);
938 msg_Dbg(p_demux
, "read MLLT sync table with %zu entries",
939 (p_sys
->mllt
.i_bits
* 8) / (p_sys
->mllt
.i_bits_per_bytes_dev
+ p_sys
->mllt
.i_bits_per_ms_dev
) );
944 else if( i_tag
== VLC_FOURCC('T', 'X', 'X', 'X') )
946 vlc_meta_t
*p_meta
= vlc_meta_New();
950 if( ID3HandleTag( p_payload
, i_payload
, i_tag
, p_meta
, &b_updated
) )
952 char ** ppsz_keys
= vlc_meta_CopyExtraNames( p_meta
);
955 for( size_t i
= 0; ppsz_keys
[i
]; ++i
)
958 if( !strcasecmp( ppsz_keys
[i
], "REPLAYGAIN_TRACK_GAIN" ) )
959 pf
= &p_sys
->rgf_replay_gain
[AUDIO_REPLAY_GAIN_TRACK
];
960 else if( !strcasecmp( ppsz_keys
[i
], "REPLAYGAIN_TRACK_PEAK" ) )
961 pf
= &p_sys
->rgf_replay_peak
[AUDIO_REPLAY_GAIN_TRACK
];
962 else if( !strcasecmp( ppsz_keys
[i
], "REPLAYGAIN_ALBUM_GAIN" ) )
963 pf
= &p_sys
->rgf_replay_gain
[AUDIO_REPLAY_GAIN_ALBUM
];
964 else if( !strcasecmp( ppsz_keys
[i
], "REPLAYGAIN_ALBUM_PEAK" ) )
965 pf
= &p_sys
->rgf_replay_peak
[AUDIO_REPLAY_GAIN_ALBUM
];
968 const char *psz_val
= vlc_meta_GetExtra( p_meta
, ppsz_keys
[i
] );
970 *pf
= us_atof( psz_val
);
972 free( ppsz_keys
[i
] );
977 vlc_meta_Delete( p_meta
);
984 static int ID3Parse( demux_t
*p_demux
,
985 int (*pf_callback
)(uint32_t, const uint8_t *, size_t, void *) )
987 const block_t
*p_tags
= NULL
;
989 if( vlc_stream_Control( p_demux
->s
, STREAM_GET_TAGS
, &p_tags
) != VLC_SUCCESS
)
992 for( ; p_tags
; p_tags
= p_tags
->p_next
)
993 ID3TAG_Parse( p_tags
->p_buffer
, p_tags
->i_buffer
, pf_callback
, (void *) p_demux
);
998 static int MpgaInit( demux_t
*p_demux
)
1000 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1002 const uint8_t *p_peek
;
1006 p_sys
->i_packet_size
= 1024;
1008 ID3Parse( p_demux
, ID3TAG_Parse_Handler
);
1010 /* Load a potential xing header */
1011 i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, 4 + 1024 );
1012 if( i_peek
< 4 + 21 )
1015 const uint32_t header
= GetDWBE( p_peek
);
1016 if( !MpgaCheckSync( p_peek
) )
1020 const uint8_t *p_xing
= p_peek
;
1021 int i_xing
= i_peek
;
1024 if( MPGA_VERSION( header
) == 0 )
1025 i_skip
= MPGA_MODE( header
) != 3 ? 36 : 21;
1027 i_skip
= MPGA_MODE( header
) != 3 ? 21 : 13;
1029 if( i_skip
+ 8 >= i_xing
|| memcmp( &p_xing
[i_skip
], "Xing", 4 ) )
1032 const uint32_t i_flags
= GetDWBE( &p_xing
[i_skip
+4] );
1034 MpgaXingSkip( &p_xing
, &i_xing
, i_skip
+ 8 );
1037 p_sys
->xing
.i_frames
= MpgaXingGetDWBE( &p_xing
, &i_xing
, 0 );
1039 p_sys
->xing
.i_bytes
= MpgaXingGetDWBE( &p_xing
, &i_xing
, 0 );
1040 if( i_flags
&0x04 ) /* TODO Support XING TOC to improve seeking accuracy */
1041 MpgaXingSkip( &p_xing
, &i_xing
, 100 );
1044 /* FIXME: doesn't return the right bitrage average, at least
1046 p_sys
->xing
.i_bitrate_avg
= MpgaXingGetDWBE( &p_xing
, &i_xing
, 0 );
1047 msg_Dbg( p_demux
, "xing vbr value present (%d)",
1048 p_sys
->xing
.i_bitrate_avg
);
1051 if( p_sys
->xing
.i_frames
> 0 && p_sys
->xing
.i_bytes
> 0 )
1053 p_sys
->xing
.i_frame_samples
= MpgaGetFrameSamples( header
);
1054 msg_Dbg( p_demux
, "xing frames&bytes value present "
1055 "(%d bytes, %d frames, %d samples/frame)",
1056 p_sys
->xing
.i_bytes
, p_sys
->xing
.i_frames
,
1057 p_sys
->xing
.i_frame_samples
);
1060 if( i_xing
>= 20 && memcmp( p_xing
, "LAME", 4 ) == 0)
1062 p_sys
->xing
.b_lame
= true;
1063 lame_extra_t
*p_lame
= &p_sys
->xing
.lame
;
1065 memcpy( p_lame
->psz_version
, p_xing
, 9 );
1066 p_lame
->psz_version
[9] = '\0';
1068 MpgaXingSkip( &p_xing
, &i_xing
, 9 );
1069 MpgaXingSkip( &p_xing
, &i_xing
, 1 ); /* rev_method */
1071 p_lame
->i_lowpass
= (*p_xing
) * 100;
1072 MpgaXingSkip( &p_xing
, &i_xing
, 1 );
1074 uint32_t peak
= MpgaXingGetDWBE( &p_xing
, &i_xing
, 0 );
1075 uint16_t track
= MpgaXingGetWBE( &p_xing
, &i_xing
, 0 );
1076 uint16_t album
= MpgaXingGetWBE( &p_xing
, &i_xing
, 0 );
1078 p_sys
->rgf_replay_peak
[AUDIO_REPLAY_GAIN_TRACK
] = (float) MpgaXingLameConvertPeak( peak
);
1079 p_sys
->rgf_replay_gain
[AUDIO_REPLAY_GAIN_TRACK
] = (float) MpgaXingLameConvertGain( track
);
1080 p_sys
->rgf_replay_gain
[AUDIO_REPLAY_GAIN_ALBUM
] = (float) MpgaXingLameConvertGain( album
);
1082 MpgaXingSkip( &p_xing
, &i_xing
, 1 ); /* flags */
1088 /*****************************************************************************
1090 *****************************************************************************/
1091 static int AacProbe( demux_t
*p_demux
, uint64_t *pi_offset
)
1094 bool b_forced_demux
;
1097 const uint8_t *p_peek
;
1099 b_forced
= demux_IsPathExtension( p_demux
, ".aac" ) ||
1100 demux_IsPathExtension( p_demux
, ".aacp" );
1101 b_forced_demux
= demux_IsForced( p_demux
, "m4a" ) ||
1102 demux_IsForced( p_demux
, "aac" ) ||
1103 demux_IsForced( p_demux
, "mp4a" );
1105 if( !b_forced_demux
&& !b_forced
)
1106 return VLC_EGENERIC
;
1108 i_offset
= vlc_stream_Tell( p_demux
->s
);
1110 /* peek the begining (10 is for adts header) */
1111 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 10 ) < 10 )
1113 msg_Dbg( p_demux
, "cannot peek" );
1114 return VLC_EGENERIC
;
1116 if( !strncmp( (char *)p_peek
, "ADIF", 4 ) )
1118 msg_Err( p_demux
, "ADIF file. Not yet supported. (Please report)" );
1119 return VLC_EGENERIC
;
1122 *pi_offset
= i_offset
;
1125 static int AacInit( demux_t
*p_demux
)
1127 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1129 p_sys
->i_packet_size
= 4096;
1130 p_sys
->i_original
= VLC_FOURCC('H','E','A','D');
1136 /*****************************************************************************
1138 *****************************************************************************/
1139 static int A52CheckSync( const uint8_t *p_peek
, bool *p_big_endian
, unsigned *pi_samples
, bool b_eac3
)
1141 vlc_a52_header_t header
;
1142 uint8_t p_tmp
[VLC_A52_MIN_HEADER_SIZE
];
1144 *p_big_endian
= p_peek
[0] == 0x0b && p_peek
[1] == 0x77;
1145 if( !*p_big_endian
)
1147 swab( p_peek
, p_tmp
, VLC_A52_MIN_HEADER_SIZE
);
1151 if( vlc_a52_header_Parse( &header
, p_peek
, VLC_A52_MIN_HEADER_SIZE
) )
1152 return VLC_EGENERIC
;
1154 if( !header
.b_eac3
!= !b_eac3
)
1155 return VLC_EGENERIC
;
1157 *pi_samples
= header
.i_samples
;
1158 return header
.i_size
;
1160 static int EA52CheckSyncProbe( const uint8_t *p_peek
, unsigned *pi_samples
)
1163 return A52CheckSync( p_peek
, &b_dummy
, pi_samples
, true );
1166 static int EA52Probe( demux_t
*p_demux
, uint64_t *pi_offset
)
1168 const char *ppsz_name
[] = { "eac3", NULL
};
1169 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_PCM
, WAVE_FORMAT_A52
, WAVE_FORMAT_UNKNOWN
};
1171 return GenericProbe( p_demux
, pi_offset
, ppsz_name
, EA52CheckSyncProbe
,
1172 VLC_A52_MIN_HEADER_SIZE
,
1173 1920 + VLC_A52_MIN_HEADER_SIZE
+ 1,
1174 WAV_EXTRA_PROBE_SIZE
,
1175 true, rgi_twocc
, GenericFormatCheck
);
1178 static int A52CheckSyncProbe( const uint8_t *p_peek
, unsigned *pi_samples
)
1181 return A52CheckSync( p_peek
, &b_dummy
, pi_samples
, false );
1184 static int A52Probe( demux_t
*p_demux
, uint64_t *pi_offset
)
1186 const char *ppsz_name
[] = { "a52", "ac3", NULL
};
1187 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_PCM
, WAVE_FORMAT_A52
, WAVE_FORMAT_UNKNOWN
};
1189 return GenericProbe( p_demux
, pi_offset
, ppsz_name
, A52CheckSyncProbe
,
1190 VLC_A52_MIN_HEADER_SIZE
,
1191 1920 + VLC_A52_MIN_HEADER_SIZE
+ 1,
1192 WAV_EXTRA_PROBE_SIZE
,
1193 true, rgi_twocc
, GenericFormatCheck
);
1196 static int A52Init( demux_t
*p_demux
)
1198 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1200 p_sys
->b_big_endian
= false;
1201 p_sys
->i_packet_size
= 1024;
1203 const uint8_t *p_peek
;
1205 /* peek the begining */
1206 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, VLC_A52_MIN_HEADER_SIZE
) >= VLC_A52_MIN_HEADER_SIZE
)
1208 A52CheckSync( p_peek
, &p_sys
->b_big_endian
, NULL
, true );
1213 /*****************************************************************************
1215 *****************************************************************************/
1216 static int DtsCheckSync( const uint8_t *p_peek
, unsigned *pi_samples
)
1218 VLC_UNUSED(pi_samples
);
1220 vlc_dts_header_t dts
;
1221 if( vlc_dts_header_Parse( &dts
, p_peek
, VLC_DTS_HEADER_SIZE
) == VLC_SUCCESS
1222 && dts
.i_frame_size
> 0 && dts
.i_frame_size
<= 8192 )
1225 *pi_samples
= dts
.i_frame_length
;
1226 return dts
.i_frame_size
;
1229 return VLC_EGENERIC
;
1232 static int DtsProbe( demux_t
*p_demux
, uint64_t *pi_offset
)
1234 const char *ppsz_name
[] = { "dts", NULL
};
1235 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_PCM
, WAVE_FORMAT_DTS
, WAVE_FORMAT_UNKNOWN
};
1237 return GenericProbe( p_demux
, pi_offset
, ppsz_name
, DtsCheckSync
,
1238 VLC_DTS_HEADER_SIZE
,
1239 16384 + VLC_DTS_HEADER_SIZE
+ 1,
1240 WAV_EXTRA_PROBE_SIZE
,
1241 false, rgi_twocc
, NULL
);
1243 static int DtsInit( demux_t
*p_demux
)
1245 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1247 p_sys
->i_packet_size
= 16384;
1252 /*****************************************************************************
1254 *****************************************************************************/
1255 static int MlpCheckSync( const uint8_t *p_peek
, unsigned *pi_samples
)
1257 if( p_peek
[4+0] != 0xf8 || p_peek
[4+1] != 0x72 || p_peek
[4+2] != 0x6f )
1260 if( p_peek
[4+3] != 0xbb )
1263 /* TODO checksum and real size for robustness */
1264 VLC_UNUSED(pi_samples
);
1267 static int ThdCheckSync( const uint8_t *p_peek
, unsigned *pi_samples
)
1269 if( p_peek
[4+0] != 0xf8 || p_peek
[4+1] != 0x72 || p_peek
[4+2] != 0x6f )
1272 if( p_peek
[4+3] != 0xba )
1275 /* TODO checksum and real size for robustness */
1276 VLC_UNUSED(pi_samples
);
1279 static int MlpProbe( demux_t
*p_demux
, uint64_t *pi_offset
)
1281 const char *ppsz_name
[] = { "mlp", NULL
};
1282 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_PCM
, WAVE_FORMAT_UNKNOWN
};
1284 return GenericProbe( p_demux
, pi_offset
, ppsz_name
, MlpCheckSync
,
1285 4+28+16*4, BASE_PROBE_SIZE
, WAV_EXTRA_PROBE_SIZE
,
1286 false, rgi_twocc
, GenericFormatCheck
);
1288 static int ThdProbe( demux_t
*p_demux
, uint64_t *pi_offset
)
1290 const char *ppsz_name
[] = { "thd", NULL
};
1291 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_PCM
, WAVE_FORMAT_UNKNOWN
};
1293 return GenericProbe( p_demux
, pi_offset
, ppsz_name
, ThdCheckSync
,
1294 4+28+16*4, BASE_PROBE_SIZE
, WAV_EXTRA_PROBE_SIZE
,
1295 false, rgi_twocc
, GenericFormatCheck
);
1297 static int MlpInit( demux_t
*p_demux
)
1300 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1302 p_sys
->i_packet_size
= 4096;
1307 /*****************************************************************************
1309 *****************************************************************************/
1310 static int VideoInit( demux_t
*p_demux
)
1312 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1314 p_sys
->i_packet_size
= 4096;