1 /*****************************************************************************
2 * es.c : Generic audio ES input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2008 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_codec.h>
37 #include <vlc_input.h>
39 #include "../../codec/a52.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
48 set_category( CAT_INPUT )
49 set_subcategory( SUBCAT_INPUT_DEMUX )
50 set_description( N_("MPEG-I/II/4 / A52 / DTS / MLP audio" ) )
51 set_capability( "demux", 155 )
52 set_callbacks( Open, Close )
54 add_shortcut( "mpga" )
58 add_shortcut( "mp4a" )
64 add_shortcut( "eac3" )
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Demux ( demux_t * );
76 static int Control( demux_t *, int, va_list );
83 int (*pf_probe)( demux_t *p_demux, int64_t *pi_offset );
84 int (*pf_init)( demux_t *p_demux );
94 decoder_t *p_packetizer;
97 mtime_t i_time_offset;
101 bool b_estimate_bitrate;
102 int i_bitrate_avg; /* extracted from Xing header */
104 bool b_initial_sync_failed;
108 int64_t i_stream_offset;
120 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset );
121 static int MpgaInit( demux_t *p_demux );
123 static int AacProbe( demux_t *p_demux, int64_t *pi_offset );
124 static int AacInit( demux_t *p_demux );
126 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset );
127 static int A52Probe( demux_t *p_demux, int64_t *pi_offset );
128 static int A52Init( demux_t *p_demux );
130 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset );
131 static int DtsInit( demux_t *p_demux );
133 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset );
134 static int MlpInit( demux_t *p_demux );
136 static const codec_t p_codec[] = {
137 { VLC_CODEC_MP4A, false, "mp4 audio", AacProbe, AacInit },
138 { VLC_CODEC_MPGA, false, "mpeg audio", MpgaProbe, MpgaInit },
139 { VLC_CODEC_A52, true, "a52 audio", A52Probe, A52Init },
140 { VLC_CODEC_EAC3, true, "eac3 audio", EA52Probe, A52Init },
141 { VLC_CODEC_DTS, false, "dts audio", DtsProbe, DtsInit },
142 { VLC_CODEC_TRUEHD, false, "mlp audio", MlpProbe, MlpInit },
144 { 0, false, NULL, NULL, NULL }
147 /*****************************************************************************
148 * Open: initializes demux structures
149 *****************************************************************************/
150 static int Open( vlc_object_t * p_this )
152 demux_t *p_demux = (demux_t*)p_this;
159 for( i_index = 0; p_codec[i_index].i_codec != 0; i_index++ )
161 if( !p_codec[i_index].pf_probe( p_demux, &i_offset ) )
165 if( p_codec[i_index].i_codec == 0 )
168 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
169 memset( p_sys, 0, sizeof( demux_sys_t ) );
170 p_sys->codec = p_codec[i_index];
172 p_sys->b_start = true;
173 p_sys->i_stream_offset = i_offset;
174 p_sys->b_estimate_bitrate = true;
175 p_sys->i_bitrate_avg = 0;
176 p_sys->b_big_endian = false;
178 if( stream_Seek( p_demux->s, p_sys->i_stream_offset ) )
184 if( p_sys->codec.pf_init( p_demux ) )
190 msg_Dbg( p_demux, "detected format %4.4s", (const char*)&p_sys->codec.i_codec );
192 /* Load the audio packetizer */
193 es_format_Init( &fmt, AUDIO_ES, p_sys->codec.i_codec );
194 p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, p_sys->codec.psz_name );
195 if( !p_sys->p_packetizer )
203 /*****************************************************************************
204 * Demux: reads and demuxes data packets
205 *****************************************************************************
206 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
207 *****************************************************************************/
208 static int Demux( demux_t *p_demux )
210 demux_sys_t *p_sys = p_demux->p_sys;
211 block_t *p_block_in, *p_block_out;
213 if( p_sys->codec.b_use_word )
215 /* Make sure we are word aligned */
216 int64_t i_pos = stream_Tell( p_demux->s );
218 stream_Read( p_demux->s, NULL, 1 );
221 if( ( p_block_in = stream_Block( p_demux->s, p_sys->i_packet_size ) ) == NULL )
224 if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
226 /* Convert to big endian */
227 swab( p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer );
230 p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start || p_sys->b_initial_sync_failed ? 1 : 0;
231 p_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
233 while( ( p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in ) ) )
235 p_sys->b_initial_sync_failed = false;
238 block_t *p_next = p_block_out->p_next;
242 p_sys->p_packetizer->fmt_out.b_packetized = true;
243 p_sys->p_es = es_out_Add( p_demux->out,
244 &p_sys->p_packetizer->fmt_out);
247 /* Try the xing header */
248 if( p_sys->xing.i_bytes && p_sys->xing.i_frames &&
249 p_sys->xing.i_frame_samples )
251 p_sys->i_bitrate_avg = p_sys->xing.i_bytes * INT64_C(8) *
252 p_sys->p_packetizer->fmt_out.audio.i_rate /
253 p_sys->xing.i_frames / p_sys->xing.i_frame_samples;
255 if( p_sys->i_bitrate_avg > 0 )
256 p_sys->b_estimate_bitrate = false;
258 /* Use the bitrate as initual value */
259 if( p_sys->b_estimate_bitrate )
260 p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
263 p_sys->i_pts = p_block_out->i_pts;
265 /* Re-estimate bitrate */
266 if( p_sys->b_estimate_bitrate && p_sys->i_pts > 1 + INT64_C(500000) )
267 p_sys->i_bitrate_avg = 8*INT64_C(1000000)*p_sys->i_bytes/(p_sys->i_pts-1);
268 p_sys->i_bytes += p_block_out->i_buffer;
270 /* Correct timestamp */
271 p_block_out->i_pts += p_sys->i_time_offset;
272 p_block_out->i_dts += p_sys->i_time_offset;
274 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
276 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
278 p_block_out = p_next;
282 if( p_sys->b_initial_sync_failed )
283 msg_Dbg( p_demux, "did not sync on first block" );
284 p_sys->b_start = false;
288 /*****************************************************************************
289 * Close: frees unused data
290 *****************************************************************************/
291 static void Close( vlc_object_t * p_this )
293 demux_t *p_demux = (demux_t*)p_this;
294 demux_sys_t *p_sys = p_demux->p_sys;
296 demux_PacketizerDestroy( p_sys->p_packetizer );
300 /*****************************************************************************
302 *****************************************************************************/
303 static int Control( demux_t *p_demux, int i_query, va_list args )
305 demux_sys_t *p_sys = p_demux->p_sys;
311 va_copy ( args_save, args );
315 case DEMUX_HAS_UNSUPPORTED_META:
316 pb_bool = (bool*)va_arg( args, bool* );
321 pi64 = (int64_t*)va_arg( args, int64_t * );
322 *pi64 = p_sys->i_pts + p_sys->i_time_offset;
325 case DEMUX_GET_LENGTH:
326 i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
327 p_sys->i_bitrate_avg, 1, i_query,
329 /* No bitrate, we can't have it precisely, but we can compute
330 * a raw approximation with time/position */
331 if( i_ret && !p_sys->i_bitrate_avg )
333 float f_pos = (double)(uint64_t)( stream_Tell( p_demux->s ) ) /
334 (double)(uint64_t)( stream_Size( p_demux->s ) );
335 /* The first few seconds are guaranteed to be very whacky,
336 * don't bother trying ... Too bad */
338 (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
341 pi64 = (int64_t *)va_arg( args_save, int64_t * );
342 *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
349 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
350 * needed for multi-input */
352 i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
353 p_sys->i_bitrate_avg, 1, i_query,
355 if( !i_ret && p_sys->i_bitrate_avg > 0 &&
356 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
358 int64_t i_time = INT64_C(8000000) * ( stream_Tell(p_demux->s) - p_sys->i_stream_offset ) /
359 p_sys->i_bitrate_avg;
361 /* Fix time_offset */
363 p_sys->i_time_offset = i_time - p_sys->i_pts;
369 /*****************************************************************************
371 *****************************************************************************/
372 static int MpgaCheckSync( const uint8_t *p_peek )
374 uint32_t h = GetDWBE( p_peek );
376 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
377 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
378 || (((h >> 12)&0x0F) == 0x0F )
379 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
380 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
381 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
388 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
389 #define MPGA_MODE(h) (((h)>> 6)&0x03)
391 static int MpgaGetFrameSamples( uint32_t h )
393 const int i_layer = 3 - (((h)>>17)&0x03);
401 return MPGA_VERSION(h) ? 576 : 1152;
407 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset )
413 const uint8_t *p_peek;
415 b_forced = demux_IsPathExtension( p_demux, ".mp3" );
416 b_forced_demux = demux_IsForced( p_demux, "mp3" ) ||
417 demux_IsForced( p_demux, "mpga" );
419 i_offset = stream_Tell( p_demux->s );
420 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
423 if( !MpgaCheckSync( p_peek ) )
428 if( !b_forced_demux && !b_forced )
431 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
434 if( MpgaCheckSync( p_peek ) )
443 if( !b_ok && !b_forced_demux )
446 *pi_offset = i_offset;
450 static void MpgaXingSkip( const uint8_t **pp_xing, int *pi_xing, int i_count )
452 if(i_count > *pi_xing )
455 (*pp_xing) += i_count;
456 (*pi_xing) -= i_count;
459 static uint32_t MpgaXingGetDWBE( const uint8_t **pp_xing, int *pi_xing, uint32_t i_default )
464 uint32_t v = GetDWBE( *pp_xing );
466 MpgaXingSkip( pp_xing, pi_xing, 4 );
471 static int MpgaInit( demux_t *p_demux )
473 demux_sys_t *p_sys = p_demux->p_sys;
475 const uint8_t *p_peek;
479 p_sys->i_packet_size = 1024;
481 /* Load a potential xing header */
482 i_peek = stream_Peek( p_demux->s, &p_peek, 4 + 1024 );
483 if( i_peek < 4 + 21 )
486 const uint32_t header = GetDWBE( p_peek );
487 if( !MpgaCheckSync( p_peek ) )
491 const uint8_t *p_xing = p_peek;
495 if( MPGA_VERSION( header ) == 0 )
496 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
498 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
500 if( i_skip + 8 >= i_xing || memcmp( &p_xing[i_skip], "Xing", 4 ) )
503 const uint32_t i_flags = GetDWBE( &p_xing[i_skip+4] );
505 MpgaXingSkip( &p_xing, &i_xing, i_skip + 8 );
508 p_sys->xing.i_frames = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
510 p_sys->xing.i_bytes = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
511 if( i_flags&0x04 ) /* TODO Support XING TOC to improve seeking accuracy */
512 MpgaXingSkip( &p_xing, &i_xing, 100 );
515 /* FIXME: doesn't return the right bitrage average, at least
517 p_sys->xing.i_bitrate_avg = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
518 msg_Dbg( p_demux, "xing vbr value present (%d)",
519 p_sys->xing.i_bitrate_avg );
522 if( p_sys->xing.i_frames > 0 && p_sys->xing.i_bytes > 0 )
524 p_sys->xing.i_frame_samples = MpgaGetFrameSamples( header );
525 msg_Dbg( p_demux, "xing frames&bytes value present "
526 "(%d bytes, %d frames, %d samples/frame)",
527 p_sys->xing.i_bytes, p_sys->xing.i_frames,
528 p_sys->xing.i_frame_samples );
533 /*****************************************************************************
535 *****************************************************************************/
536 static int AacProbe( demux_t *p_demux, int64_t *pi_offset )
542 const uint8_t *p_peek;
544 b_forced = demux_IsPathExtension( p_demux, ".aac" ) ||
545 demux_IsPathExtension( p_demux, ".aacp" );
546 b_forced_demux = demux_IsForced( p_demux, "m4a" ) ||
547 demux_IsForced( p_demux, "aac" ) ||
548 demux_IsForced( p_demux, "mp4a" );
550 if( !b_forced_demux && !b_forced )
553 i_offset = stream_Tell( p_demux->s );
555 /* peek the begining (10 is for adts header) */
556 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
558 msg_Err( p_demux, "cannot peek" );
561 if( !strncmp( (char *)p_peek, "ADIF", 4 ) )
563 msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
567 *pi_offset = i_offset;
570 static int AacInit( demux_t *p_demux )
572 demux_sys_t *p_sys = p_demux->p_sys;
574 p_sys->i_packet_size = 4096;
579 /*****************************************************************************
581 *****************************************************************************/
582 #define WAV_PROBE_SIZE (512*1024)
583 static int WavSkipHeader( demux_t *p_demux, int *pi_skip )
585 const uint8_t *p_peek;
591 /* Check if we are dealing with a WAV file */
592 if( stream_Peek( p_demux->s, &p_peek, 12+8 ) != 12 + 8 )
595 if( memcmp( p_peek, "RIFF", 4 ) || memcmp( &p_peek[8], "WAVE", 4 ) )
598 /* Find the wave format header */
600 while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
602 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
603 if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
607 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
611 /* Sanity check the wave format header */
612 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
613 if( i_len > WAV_PROBE_SIZE )
617 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
619 if( GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ ) !=
620 1 /* WAVE_FORMAT_PCM */ )
622 if( GetWLE( p_peek + i_peek - i_len - 6 /* nChannels */ ) != 2 )
624 if( GetDWLE( p_peek + i_peek - i_len - 4 /* nSamplesPerSec */ ) !=
628 /* Skip the wave header */
629 while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
631 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
632 if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
636 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
643 static int GenericProbe( demux_t *p_demux, int64_t *pi_offset,
644 const char * ppsz_name[],
645 int (*pf_check)( const uint8_t * ), int i_check_size )
650 const uint8_t *p_peek;
653 b_forced_demux = false;
654 for( int i = 0; ppsz_name[i] != NULL; i++ )
656 b_forced_demux |= demux_IsForced( p_demux, ppsz_name[i] );
659 i_offset = stream_Tell( p_demux->s );
661 if( WavSkipHeader( p_demux, &i_skip ) )
663 if( !b_forced_demux )
666 const bool b_wav = i_skip > 0;
669 * It is common that wav files have some sort of garbage at the begining */
670 const int i_probe = i_skip + i_check_size + ( b_wav ? 16000 : 0);
671 const int i_peek = stream_Peek( p_demux->s, &p_peek, i_probe );
672 if( i_peek < i_skip + i_check_size )
674 msg_Err( p_demux, "cannot peek" );
679 if( i_skip + i_check_size > i_peek )
681 if( !b_forced_demux )
685 const int i_size = pf_check( &p_peek[i_skip] );
688 if( i_size == 0 || 1)
691 /* If we have the frame size, check the next frame for
692 * extra robustness */
693 if( i_skip + i_check_size + i_size <= i_peek )
695 if( pf_check( &p_peek[i_skip+i_size] ) >= 0 )
702 *pi_offset = i_offset + i_skip;
706 /*****************************************************************************
708 *****************************************************************************/
709 static int A52CheckSync( const uint8_t *p_peek, bool *p_big_endian, bool b_eac3 )
711 vlc_a52_header_t header;
712 uint8_t p_tmp[VLC_A52_HEADER_SIZE];
714 *p_big_endian = p_peek[0] == 0x0b && p_peek[1] == 0x77;
717 swab( p_peek, p_tmp, VLC_A52_HEADER_SIZE );
721 if( vlc_a52_header_Parse( &header, p_peek, VLC_A52_HEADER_SIZE ) )
724 if( !header.b_eac3 != !b_eac3 )
726 return header.i_size;
728 static int EA52CheckSyncProbe( const uint8_t *p_peek )
731 return A52CheckSync( p_peek, &b_dummy, true );
734 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset )
736 const char *ppsz_name[] = { "eac3", NULL };
738 return GenericProbe( p_demux, pi_offset, ppsz_name, EA52CheckSyncProbe, VLC_A52_HEADER_SIZE );
741 static int A52CheckSyncProbe( const uint8_t *p_peek )
744 return A52CheckSync( p_peek, &b_dummy, false );
747 static int A52Probe( demux_t *p_demux, int64_t *pi_offset )
749 const char *ppsz_name[] = { "a52", "ac3", NULL };
751 return GenericProbe( p_demux, pi_offset, ppsz_name, A52CheckSyncProbe, VLC_A52_HEADER_SIZE );
754 static int A52Init( demux_t *p_demux )
756 demux_sys_t *p_sys = p_demux->p_sys;
758 p_sys->b_big_endian = false;
759 p_sys->i_packet_size = 1024;
761 const uint8_t *p_peek;
763 /* peek the begining */
764 if( stream_Peek( p_demux->s, &p_peek, VLC_A52_HEADER_SIZE ) >= VLC_A52_HEADER_SIZE )
766 A52CheckSync( p_peek, &p_sys->b_big_endian, true );
771 /*****************************************************************************
773 *****************************************************************************/
774 static int DtsCheckSync( const uint8_t *p_peek )
776 /* TODO return frame size for robustness */
778 /* 14 bits, little endian version of the bitstream */
779 if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
780 p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
781 (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
785 /* 14 bits, big endian version of the bitstream */
786 else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
787 p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
788 p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
792 /* 16 bits, big endian version of the bitstream */
793 else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
794 p_peek[2] == 0x80 && p_peek[3] == 0x01 )
798 /* 16 bits, little endian version of the bitstream */
799 else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
800 p_peek[2] == 0x01 && p_peek[3] == 0x80 )
808 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset )
810 const char *ppsz_name[] = { "dts", NULL };
812 return GenericProbe( p_demux, pi_offset, ppsz_name, DtsCheckSync, 11 );
814 static int DtsInit( demux_t *p_demux )
816 demux_sys_t *p_sys = p_demux->p_sys;
818 p_sys->i_packet_size = 16384;
823 /*****************************************************************************
825 *****************************************************************************/
826 static int MlpCheckSync( const uint8_t *p_peek )
828 if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
831 if( p_peek[4+3] != 0xba && p_peek[4+3] != 0xbb )
834 /* TODO checksum and real size for robustness */
837 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset )
839 const char *ppsz_name[] = { "mlp", "thd", NULL };
841 return GenericProbe( p_demux, pi_offset, ppsz_name, MlpCheckSync, 4+28+16*4 );
843 static int MlpInit( demux_t *p_demux )
846 demux_sys_t *p_sys = p_demux->p_sys;
848 p_sys->i_packet_size = 4096;