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 swab( p_block_in
->p_buffer
, p_block_in
->p_buffer
, p_block_in
->i_buffer
);
513 p_block_in
->i_pts
= p_block_in
->i_dts
= p_sys
->b_start
|| p_sys
->b_initial_sync_failed
? VLC_TICK_0
: VLC_TICK_INVALID
;
515 p_sys
->b_initial_sync_failed
= p_sys
->b_start
; /* Only try to resync once */
517 while( ( p_block_out
= p_sys
->p_packetizer
->pf_packetize( p_sys
->p_packetizer
, p_block_in
? &p_block_in
: NULL
) ) )
519 p_sys
->b_initial_sync_failed
= false;
524 p_sys
->p_packetizer
->fmt_out
.b_packetized
= true;
525 p_sys
->p_es
= es_out_Add( p_demux
->out
,
526 &p_sys
->p_packetizer
->fmt_out
);
529 /* Try the xing header */
530 if( p_sys
->xing
.i_bytes
&& p_sys
->xing
.i_frames
&&
531 p_sys
->xing
.i_frame_samples
)
533 p_sys
->i_bitrate_avg
= p_sys
->xing
.i_bytes
* INT64_C(8) *
534 p_sys
->p_packetizer
->fmt_out
.audio
.i_rate
/
535 p_sys
->xing
.i_frames
/ p_sys
->xing
.i_frame_samples
;
537 if( p_sys
->i_bitrate_avg
> 0 )
538 p_sys
->b_estimate_bitrate
= false;
540 /* Use the bitrate as initual value */
541 if( p_sys
->b_estimate_bitrate
)
542 p_sys
->i_bitrate_avg
= p_sys
->p_packetizer
->fmt_out
.i_bitrate
;
545 block_t
*p_next
= p_block_out
->p_next
;
546 p_block_out
->p_next
= NULL
;
548 block_ChainLastAppend( &pp_output
, p_block_out
);
550 p_block_out
= p_next
;
554 if( p_sys
->b_initial_sync_failed
)
555 msg_Dbg( p_demux
, "did not sync on first block" );
556 p_sys
->b_start
= false;
561 /* Check to apply to WAVE fmt header */
562 static int GenericFormatCheck( int i_format
, const uint8_t *p_head
)
564 if ( i_format
== WAVE_FORMAT_PCM
)
566 if( GetWLE( p_head
/* nChannels */ ) != 2 )
568 if( GetDWLE( p_head
+ 2 /* nSamplesPerSec */ ) != 44100 )
574 /*****************************************************************************
576 *****************************************************************************/
577 static int WavSkipHeader( demux_t
*p_demux
, uint64_t *pi_skip
,
578 const uint16_t rgi_twocc
[],
579 int (*pf_format_check
)( int, const uint8_t * ) )
581 const uint8_t *p_peek
;
588 /* Check if we are dealing with a WAV file */
589 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 12+8 ) != 12 + 8 )
592 if( memcmp( p_peek
, "RIFF", 4 ) || memcmp( &p_peek
[8], "WAVE", 4 ) )
595 /* Find the wave format header */
597 while( memcmp( p_peek
+ i_peek
- 8, "fmt ", 4 ) )
599 i_len
= GetDWLE( p_peek
+ i_peek
- 4 );
600 if( i_len
> WAV_PROBE_SIZE
|| i_peek
+ i_len
> WAV_PROBE_SIZE
)
604 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, i_peek
) != (ssize_t
) i_peek
)
608 /* Sanity check the wave format header */
609 i_len
= GetDWLE( p_peek
+ i_peek
- 4 );
610 if( i_len
> WAV_PROBE_SIZE
)
614 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, i_peek
) != (ssize_t
) i_peek
)
616 const uint16_t i_twocc
= GetWLE( p_peek
+ i_peek
- i_len
- 8 /* wFormatTag */ );
618 for( i_format_idx
= 0; rgi_twocc
[i_format_idx
] != WAVE_FORMAT_UNKNOWN
; i_format_idx
++ )
620 if( i_twocc
== rgi_twocc
[i_format_idx
] )
623 if( rgi_twocc
[i_format_idx
] == WAVE_FORMAT_UNKNOWN
)
626 if( pf_format_check
&&
627 pf_format_check( i_twocc
, p_peek
+ i_peek
- i_len
- 6 ) != VLC_SUCCESS
)
630 /* Skip the wave header */
631 while( memcmp( p_peek
+ i_peek
- 8, "data", 4 ) )
633 i_len
= GetDWLE( p_peek
+ i_peek
- 4 );
634 if( i_len
> WAV_PROBE_SIZE
|| i_peek
+ i_len
> WAV_PROBE_SIZE
)
638 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, i_peek
) != (ssize_t
) i_peek
)
645 static int GenericProbe( demux_t
*p_demux
, uint64_t *pi_offset
,
646 const char * ppsz_name
[],
647 int (*pf_check
)( const uint8_t *, unsigned * ),
648 unsigned i_check_size
,
649 unsigned i_base_probing
,
650 unsigned i_wav_extra_probing
,
652 const uint16_t pi_twocc
[],
653 int (*pf_format_check
)( int, const uint8_t * ) )
658 const uint8_t *p_peek
;
661 b_forced_demux
= false;
662 for( size_t i
= 0; ppsz_name
[i
] != NULL
; i
++ )
664 b_forced_demux
|= demux_IsForced( p_demux
, ppsz_name
[i
] );
667 i_offset
= vlc_stream_Tell( p_demux
->s
);
669 if( WavSkipHeader( p_demux
, &i_skip
, pi_twocc
, pf_format_check
) )
671 if( !b_forced_demux
)
674 const bool b_wav
= i_skip
> 0;
677 * It is common that wav files have some sort of garbage at the begining
678 * We will accept probing 0.5s of data in this case.
680 const size_t i_probe
= i_skip
+ i_check_size
+ i_base_probing
+ ( b_wav
? i_wav_extra_probing
: 0);
681 const size_t i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, i_probe
);
682 if( i_peek
< i_skip
+ i_check_size
)
684 msg_Dbg( p_demux
, "cannot peek" );
689 if( i_skip
+ i_check_size
> i_peek
)
691 if( !b_forced_demux
)
695 unsigned i_samples
= 0;
696 int i_size
= pf_check( &p_peek
[i_skip
], &i_samples
);
699 if( i_size
== 0 || /* 0 sized frame ?? */
700 i_skip
== 0 /* exact match from start, we're not WAVE either, so skip multiple checks (would break if padding) */ )
703 /* If we have the frame size, check the next frame for
705 * The second test is because some .wav have paddings
708 for( int t
= 0; t
< 1 + !!b_wav
; t
++ )
714 i_size
= i_samples
* 2 * 2;
717 if( i_skip
+ i_check_size
+ i_size
<= i_peek
)
719 b_ok
= pf_check( &p_peek
[i_skip
+i_size
], NULL
) >= 0;
728 i_skip
+= ((i_offset
+ i_skip
) % 2 == 0) ? 2 : 1;
731 if( !b_wav
&& !b_forced_demux
)
735 *pi_offset
= i_offset
+ i_skip
;
739 /*****************************************************************************
741 *****************************************************************************/
742 static int MpgaCheckSync( const uint8_t *p_peek
)
744 uint32_t h
= GetDWBE( p_peek
);
746 if( ((( h
>> 21 )&0x07FF) != 0x07FF ) /* header sync */
747 || (((h
>> 19)&0x03) == 1 ) /* valid version ID ? */
748 || (((h
>> 17)&0x03) == 0 ) /* valid layer ?*/
749 || (((h
>> 12)&0x0F) == 0x0F ) /* valid bitrate ?*/
750 || (((h
>> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
751 || ((h
& 0x03) == 0x02 )) /* valid emphasis ? */
758 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
759 #define MPGA_MODE(h) (((h)>> 6)&0x03)
761 static int MpgaGetFrameSamples( uint32_t h
)
763 const int i_layer
= 3 - (((h
)>>17)&0x03);
771 return MPGA_VERSION(h
) ? 576 : 1152;
777 static int MpgaProbe( demux_t
*p_demux
, uint64_t *pi_offset
)
779 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_MPEG
, WAVE_FORMAT_MPEGLAYER3
, WAVE_FORMAT_UNKNOWN
};
784 const uint8_t *p_peek
;
788 b_forced
= demux_IsPathExtension( p_demux
, ".mp3" );
789 b_forced_demux
= demux_IsForced( p_demux
, "mp3" ) ||
790 demux_IsForced( p_demux
, "mpga" );
792 i_offset
= vlc_stream_Tell( p_demux
->s
);
794 if( WavSkipHeader( p_demux
, &i_skip
, rgi_twocc
, NULL
) )
796 if( !b_forced_demux
)
802 i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, i_skip
+ 4 );
803 if( i_peek
<= 0 || (uint64_t) i_peek
< i_skip
+ 4 )
806 if( !MpgaCheckSync( &p_peek
[i_skip
] ) )
810 if( !b_forced_demux
&& !b_forced
)
813 i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, i_skip
+ 8096 );
814 while( i_peek
> 0 && i_skip
+ 4 < (uint64_t) i_peek
)
816 if( MpgaCheckSync( &p_peek
[i_skip
] ) )
823 if( !b_ok
&& !b_forced_demux
)
826 *pi_offset
= i_offset
+ i_skip
;
830 static void MpgaXingSkip( const uint8_t **pp_xing
, int *pi_xing
, int i_count
)
832 if(i_count
> *pi_xing
)
835 (*pp_xing
) += i_count
;
836 (*pi_xing
) -= i_count
;
839 static uint32_t MpgaXingGetDWBE( const uint8_t **pp_xing
, int *pi_xing
, uint32_t i_default
)
844 uint32_t v
= GetDWBE( *pp_xing
);
846 MpgaXingSkip( pp_xing
, pi_xing
, 4 );
851 static uint16_t MpgaXingGetWBE( const uint8_t **pp_xing
, int *pi_xing
, uint16_t i_default
)
856 uint16_t v
= GetWBE( *pp_xing
);
858 MpgaXingSkip( pp_xing
, pi_xing
, 2 );
863 static double MpgaXingLameConvertGain( uint16_t x
)
865 double gain
= (x
& 0x1FF) / 10.0;
867 return x
& 0x200 ? -gain
: gain
;
870 static double MpgaXingLameConvertPeak( uint32_t x
)
872 return x
/ 8388608.0; /* pow(2, 23) */
875 static uint64_t SeekByMlltTable( demux_t
*p_demux
, vlc_tick_t
*pi_time
)
877 demux_sys_t
*p_sys
= p_demux
->p_sys
;
878 sync_table_ctx_t
*p_cur
= &p_sys
->mllt
.current
;
880 /* reset or init context */
881 if( *pi_time
< p_cur
->i_time
|| !p_cur
->br
.p
)
885 bs_init(&p_cur
->br
, p_sys
->mllt
.p_bits
, p_sys
->mllt
.i_bits
);
888 while(bs_remain(&p_cur
->br
) >= p_sys
->mllt
.i_bits_per_bytes_dev
+ p_sys
->mllt
.i_bits_per_ms_dev
)
890 const uint32_t i_bytesdev
= bs_read(&p_cur
->br
, p_sys
->mllt
.i_bits_per_bytes_dev
);
891 const uint32_t i_msdev
= bs_read(&p_cur
->br
, p_sys
->mllt
.i_bits_per_ms_dev
);
892 const vlc_tick_t i_deltatime
= VLC_TICK_FROM_MS(p_sys
->mllt
.i_ms_btw_refs
+ i_msdev
);
893 if( p_cur
->i_time
+ i_deltatime
> *pi_time
)
895 p_cur
->i_time
+= i_deltatime
;
896 p_cur
->i_pos
+= p_sys
->mllt
.i_bytes_btw_refs
+ i_bytesdev
;
898 *pi_time
= p_cur
->i_time
;
902 static int ID3TAG_Parse_Handler( uint32_t i_tag
, const uint8_t *p_payload
, size_t i_payload
, void *p_priv
)
904 demux_t
*p_demux
= (demux_t
*) p_priv
;
905 demux_sys_t
*p_sys
= p_demux
->p_sys
;
907 if( i_tag
== VLC_FOURCC('M', 'L', 'L', 'T') )
911 p_sys
->mllt
.i_frames_btw_refs
= GetWBE(p_payload
);
912 p_sys
->mllt
.i_bytes_btw_refs
= GetDWBE(&p_payload
[1]) & 0x00FFFFFF;
913 p_sys
->mllt
.i_ms_btw_refs
= GetDWBE(&p_payload
[4]) & 0x00FFFFFF;
914 if( !p_sys
->mllt
.i_frames_btw_refs
|| !p_sys
->mllt
.i_bytes_btw_refs
||
915 !p_sys
->mllt
.i_ms_btw_refs
||
916 p_payload
[8] > 31 || p_payload
[9] > 31 || /* bits length sanity check */
917 ((p_payload
[8] + p_payload
[9]) % 4) || p_payload
[8] + p_payload
[9] < 4 )
919 p_sys
->mllt
.i_bits_per_bytes_dev
= p_payload
[8];
920 p_sys
->mllt
.i_bits_per_ms_dev
= p_payload
[9];
921 p_sys
->mllt
.p_bits
= malloc(i_payload
- 10);
922 if( likely(p_sys
->mllt
.p_bits
) )
924 p_sys
->mllt
.i_bits
= i_payload
- 10;
925 memcpy(p_sys
->mllt
.p_bits
, &p_payload
[10], p_sys
->mllt
.i_bits
);
926 msg_Dbg(p_demux
, "read MLLT sync table with %zu entries",
927 (p_sys
->mllt
.i_bits
* 8) / (p_sys
->mllt
.i_bits_per_bytes_dev
+ p_sys
->mllt
.i_bits_per_ms_dev
) );
932 else if( i_tag
== VLC_FOURCC('T', 'X', 'X', 'X') )
934 vlc_meta_t
*p_meta
= vlc_meta_New();
938 if( ID3HandleTag( p_payload
, i_payload
, i_tag
, p_meta
, &b_updated
) )
940 char ** ppsz_keys
= vlc_meta_CopyExtraNames( p_meta
);
943 for( size_t i
= 0; ppsz_keys
[i
]; ++i
)
946 if( !strcasecmp( ppsz_keys
[i
], "REPLAYGAIN_TRACK_GAIN" ) )
947 pf
= &p_sys
->rgf_replay_gain
[AUDIO_REPLAY_GAIN_TRACK
];
948 else if( !strcasecmp( ppsz_keys
[i
], "REPLAYGAIN_TRACK_PEAK" ) )
949 pf
= &p_sys
->rgf_replay_peak
[AUDIO_REPLAY_GAIN_TRACK
];
950 else if( !strcasecmp( ppsz_keys
[i
], "REPLAYGAIN_ALBUM_GAIN" ) )
951 pf
= &p_sys
->rgf_replay_gain
[AUDIO_REPLAY_GAIN_ALBUM
];
952 else if( !strcasecmp( ppsz_keys
[i
], "REPLAYGAIN_ALBUM_PEAK" ) )
953 pf
= &p_sys
->rgf_replay_peak
[AUDIO_REPLAY_GAIN_ALBUM
];
956 const char *psz_val
= vlc_meta_GetExtra( p_meta
, ppsz_keys
[i
] );
958 *pf
= us_atof( psz_val
);
960 free( ppsz_keys
[i
] );
965 vlc_meta_Delete( p_meta
);
972 static int ID3Parse( demux_t
*p_demux
,
973 int (*pf_callback
)(uint32_t, const uint8_t *, size_t, void *) )
975 const block_t
*p_tags
= NULL
;
977 if( vlc_stream_Control( p_demux
->s
, STREAM_GET_TAGS
, &p_tags
) != VLC_SUCCESS
)
980 for( ; p_tags
; p_tags
= p_tags
->p_next
)
981 ID3TAG_Parse( p_tags
->p_buffer
, p_tags
->i_buffer
, pf_callback
, (void *) p_demux
);
986 static int MpgaInit( demux_t
*p_demux
)
988 demux_sys_t
*p_sys
= p_demux
->p_sys
;
990 const uint8_t *p_peek
;
994 p_sys
->i_packet_size
= 1024;
996 ID3Parse( p_demux
, ID3TAG_Parse_Handler
);
998 /* Load a potential xing header */
999 i_peek
= vlc_stream_Peek( p_demux
->s
, &p_peek
, 4 + 1024 );
1000 if( i_peek
< 4 + 21 )
1003 const uint32_t header
= GetDWBE( p_peek
);
1004 if( !MpgaCheckSync( p_peek
) )
1008 const uint8_t *p_xing
= p_peek
;
1009 int i_xing
= i_peek
;
1012 if( MPGA_VERSION( header
) == 0 )
1013 i_skip
= MPGA_MODE( header
) != 3 ? 36 : 21;
1015 i_skip
= MPGA_MODE( header
) != 3 ? 21 : 13;
1017 if( i_skip
+ 8 >= i_xing
|| memcmp( &p_xing
[i_skip
], "Xing", 4 ) )
1020 const uint32_t i_flags
= GetDWBE( &p_xing
[i_skip
+4] );
1022 MpgaXingSkip( &p_xing
, &i_xing
, i_skip
+ 8 );
1025 p_sys
->xing
.i_frames
= MpgaXingGetDWBE( &p_xing
, &i_xing
, 0 );
1027 p_sys
->xing
.i_bytes
= MpgaXingGetDWBE( &p_xing
, &i_xing
, 0 );
1028 if( i_flags
&0x04 ) /* TODO Support XING TOC to improve seeking accuracy */
1029 MpgaXingSkip( &p_xing
, &i_xing
, 100 );
1032 /* FIXME: doesn't return the right bitrage average, at least
1034 p_sys
->xing
.i_bitrate_avg
= MpgaXingGetDWBE( &p_xing
, &i_xing
, 0 );
1035 msg_Dbg( p_demux
, "xing vbr value present (%d)",
1036 p_sys
->xing
.i_bitrate_avg
);
1039 if( p_sys
->xing
.i_frames
> 0 && p_sys
->xing
.i_bytes
> 0 )
1041 p_sys
->xing
.i_frame_samples
= MpgaGetFrameSamples( header
);
1042 msg_Dbg( p_demux
, "xing frames&bytes value present "
1043 "(%d bytes, %d frames, %d samples/frame)",
1044 p_sys
->xing
.i_bytes
, p_sys
->xing
.i_frames
,
1045 p_sys
->xing
.i_frame_samples
);
1048 if( i_xing
>= 20 && memcmp( p_xing
, "LAME", 4 ) == 0)
1050 p_sys
->xing
.b_lame
= true;
1051 lame_extra_t
*p_lame
= &p_sys
->xing
.lame
;
1053 memcpy( p_lame
->psz_version
, p_xing
, 9 );
1054 p_lame
->psz_version
[9] = '\0';
1056 MpgaXingSkip( &p_xing
, &i_xing
, 9 );
1057 MpgaXingSkip( &p_xing
, &i_xing
, 1 ); /* rev_method */
1059 p_lame
->i_lowpass
= (*p_xing
) * 100;
1060 MpgaXingSkip( &p_xing
, &i_xing
, 1 );
1062 uint32_t peak
= MpgaXingGetDWBE( &p_xing
, &i_xing
, 0 );
1063 uint16_t track
= MpgaXingGetWBE( &p_xing
, &i_xing
, 0 );
1064 uint16_t album
= MpgaXingGetWBE( &p_xing
, &i_xing
, 0 );
1066 p_sys
->rgf_replay_peak
[AUDIO_REPLAY_GAIN_TRACK
] = (float) MpgaXingLameConvertPeak( peak
);
1067 p_sys
->rgf_replay_gain
[AUDIO_REPLAY_GAIN_TRACK
] = (float) MpgaXingLameConvertGain( track
);
1068 p_sys
->rgf_replay_gain
[AUDIO_REPLAY_GAIN_ALBUM
] = (float) MpgaXingLameConvertGain( album
);
1070 MpgaXingSkip( &p_xing
, &i_xing
, 1 ); /* flags */
1076 /*****************************************************************************
1078 *****************************************************************************/
1079 static int AacProbe( demux_t
*p_demux
, uint64_t *pi_offset
)
1082 bool b_forced_demux
;
1085 const uint8_t *p_peek
;
1087 b_forced
= demux_IsPathExtension( p_demux
, ".aac" ) ||
1088 demux_IsPathExtension( p_demux
, ".aacp" );
1089 b_forced_demux
= demux_IsForced( p_demux
, "m4a" ) ||
1090 demux_IsForced( p_demux
, "aac" ) ||
1091 demux_IsForced( p_demux
, "mp4a" );
1093 if( !b_forced_demux
&& !b_forced
)
1094 return VLC_EGENERIC
;
1096 i_offset
= vlc_stream_Tell( p_demux
->s
);
1098 /* peek the begining (10 is for adts header) */
1099 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 10 ) < 10 )
1101 msg_Dbg( p_demux
, "cannot peek" );
1102 return VLC_EGENERIC
;
1104 if( !strncmp( (char *)p_peek
, "ADIF", 4 ) )
1106 msg_Err( p_demux
, "ADIF file. Not yet supported. (Please report)" );
1107 return VLC_EGENERIC
;
1110 *pi_offset
= i_offset
;
1113 static int AacInit( demux_t
*p_demux
)
1115 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1117 p_sys
->i_packet_size
= 4096;
1118 p_sys
->i_original
= VLC_FOURCC('H','E','A','D');
1124 /*****************************************************************************
1126 *****************************************************************************/
1127 static int A52CheckSync( const uint8_t *p_peek
, bool *p_big_endian
, unsigned *pi_samples
, bool b_eac3
)
1129 vlc_a52_header_t header
;
1130 uint8_t p_tmp
[VLC_A52_MIN_HEADER_SIZE
];
1132 *p_big_endian
= p_peek
[0] == 0x0b && p_peek
[1] == 0x77;
1133 if( !*p_big_endian
)
1135 swab( p_peek
, p_tmp
, VLC_A52_MIN_HEADER_SIZE
);
1139 if( vlc_a52_header_Parse( &header
, p_peek
, VLC_A52_MIN_HEADER_SIZE
) )
1140 return VLC_EGENERIC
;
1142 if( !header
.b_eac3
!= !b_eac3
)
1143 return VLC_EGENERIC
;
1145 *pi_samples
= header
.i_samples
;
1146 return header
.i_size
;
1148 static int EA52CheckSyncProbe( const uint8_t *p_peek
, unsigned *pi_samples
)
1151 return A52CheckSync( p_peek
, &b_dummy
, pi_samples
, true );
1154 static int EA52Probe( demux_t
*p_demux
, uint64_t *pi_offset
)
1156 const char *ppsz_name
[] = { "eac3", NULL
};
1157 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_PCM
, WAVE_FORMAT_A52
, WAVE_FORMAT_UNKNOWN
};
1159 return GenericProbe( p_demux
, pi_offset
, ppsz_name
, EA52CheckSyncProbe
,
1160 VLC_A52_MIN_HEADER_SIZE
,
1161 1920 + VLC_A52_MIN_HEADER_SIZE
+ 1,
1162 WAV_EXTRA_PROBE_SIZE
,
1163 true, rgi_twocc
, GenericFormatCheck
);
1166 static int A52CheckSyncProbe( const uint8_t *p_peek
, unsigned *pi_samples
)
1169 return A52CheckSync( p_peek
, &b_dummy
, pi_samples
, false );
1172 static int A52Probe( demux_t
*p_demux
, uint64_t *pi_offset
)
1174 const char *ppsz_name
[] = { "a52", "ac3", NULL
};
1175 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_PCM
, WAVE_FORMAT_A52
, WAVE_FORMAT_UNKNOWN
};
1177 return GenericProbe( p_demux
, pi_offset
, ppsz_name
, A52CheckSyncProbe
,
1178 VLC_A52_MIN_HEADER_SIZE
,
1179 1920 + VLC_A52_MIN_HEADER_SIZE
+ 1,
1180 WAV_EXTRA_PROBE_SIZE
,
1181 true, rgi_twocc
, GenericFormatCheck
);
1184 static int A52Init( demux_t
*p_demux
)
1186 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1188 p_sys
->b_big_endian
= false;
1189 p_sys
->i_packet_size
= 1024;
1191 const uint8_t *p_peek
;
1193 /* peek the begining */
1194 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, VLC_A52_MIN_HEADER_SIZE
) >= VLC_A52_MIN_HEADER_SIZE
)
1196 A52CheckSync( p_peek
, &p_sys
->b_big_endian
, NULL
, true );
1201 /*****************************************************************************
1203 *****************************************************************************/
1204 static int DtsCheckSync( const uint8_t *p_peek
, unsigned *pi_samples
)
1206 VLC_UNUSED(pi_samples
);
1208 vlc_dts_header_t dts
;
1209 if( vlc_dts_header_Parse( &dts
, p_peek
, VLC_DTS_HEADER_SIZE
) == VLC_SUCCESS
1210 && dts
.i_frame_size
> 0 && dts
.i_frame_size
<= 8192 )
1213 *pi_samples
= dts
.i_frame_length
;
1214 return dts
.i_frame_size
;
1217 return VLC_EGENERIC
;
1220 static int DtsProbe( demux_t
*p_demux
, uint64_t *pi_offset
)
1222 const char *ppsz_name
[] = { "dts", NULL
};
1223 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_PCM
, WAVE_FORMAT_DTS
, WAVE_FORMAT_UNKNOWN
};
1225 return GenericProbe( p_demux
, pi_offset
, ppsz_name
, DtsCheckSync
,
1226 VLC_DTS_HEADER_SIZE
,
1227 16384 + VLC_DTS_HEADER_SIZE
+ 1,
1228 WAV_EXTRA_PROBE_SIZE
,
1229 false, rgi_twocc
, NULL
);
1231 static int DtsInit( demux_t
*p_demux
)
1233 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1235 p_sys
->i_packet_size
= 16384;
1240 /*****************************************************************************
1242 *****************************************************************************/
1243 static int MlpCheckSync( const uint8_t *p_peek
, unsigned *pi_samples
)
1245 if( p_peek
[4+0] != 0xf8 || p_peek
[4+1] != 0x72 || p_peek
[4+2] != 0x6f )
1248 if( p_peek
[4+3] != 0xbb )
1251 /* TODO checksum and real size for robustness */
1252 VLC_UNUSED(pi_samples
);
1255 static int ThdCheckSync( 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] != 0xba )
1263 /* TODO checksum and real size for robustness */
1264 VLC_UNUSED(pi_samples
);
1267 static int MlpProbe( demux_t
*p_demux
, uint64_t *pi_offset
)
1269 const char *ppsz_name
[] = { "mlp", NULL
};
1270 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_PCM
, WAVE_FORMAT_UNKNOWN
};
1272 return GenericProbe( p_demux
, pi_offset
, ppsz_name
, MlpCheckSync
,
1273 4+28+16*4, BASE_PROBE_SIZE
, WAV_EXTRA_PROBE_SIZE
,
1274 false, rgi_twocc
, GenericFormatCheck
);
1276 static int ThdProbe( demux_t
*p_demux
, uint64_t *pi_offset
)
1278 const char *ppsz_name
[] = { "thd", NULL
};
1279 const uint16_t rgi_twocc
[] = { WAVE_FORMAT_PCM
, WAVE_FORMAT_UNKNOWN
};
1281 return GenericProbe( p_demux
, pi_offset
, ppsz_name
, ThdCheckSync
,
1282 4+28+16*4, BASE_PROBE_SIZE
, WAV_EXTRA_PROBE_SIZE
,
1283 false, rgi_twocc
, GenericFormatCheck
);
1285 static int MlpInit( demux_t
*p_demux
)
1288 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1290 p_sys
->i_packet_size
= 4096;
1295 /*****************************************************************************
1297 *****************************************************************************/
1298 static int VideoInit( demux_t
*p_demux
)
1300 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1302 p_sys
->i_packet_size
= 4096;