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 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
46 set_category( CAT_INPUT )
47 set_subcategory( SUBCAT_INPUT_DEMUX )
48 set_description( N_("MPEG-I/II/4 / A52 / DTS / MLP audio" ) )
49 set_capability( "demux", 155 )
50 set_callbacks( Open, Close )
52 add_shortcut( "mpga" )
56 add_shortcut( "mp4a" )
62 add_shortcut( "eac3" )
69 /*****************************************************************************
71 *****************************************************************************/
72 static int Demux ( demux_t * );
73 static int Control( demux_t *, int, va_list );
80 int (*pf_probe)( demux_t *p_demux, int64_t *pi_offset );
81 int (*pf_init)( demux_t *p_demux );
91 decoder_t *p_packetizer;
94 mtime_t i_time_offset;
98 bool b_estimate_bitrate;
99 int i_bitrate_avg; /* extracted from Xing header */
101 bool b_initial_sync_failed;
105 int64_t i_stream_offset;
117 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset );
118 static int MpgaInit( demux_t *p_demux );
120 static int AacProbe( demux_t *p_demux, int64_t *pi_offset );
121 static int AacInit( demux_t *p_demux );
123 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset );
124 static int A52Probe( demux_t *p_demux, int64_t *pi_offset );
125 static int A52Init( demux_t *p_demux );
127 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset );
128 static int DtsInit( demux_t *p_demux );
130 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset );
131 static int MlpInit( demux_t *p_demux );
133 static const codec_t p_codec[] = {
134 { VLC_FOURCC( 'm', 'p', '4', 'a' ), false, "mp4 audio", AacProbe, AacInit },
135 { VLC_FOURCC( 'm', 'p', 'g', 'a' ), false, "mpeg audio", MpgaProbe, MpgaInit },
136 { VLC_FOURCC( 'a', '5', '2', ' ' ), true, "a52 audio", A52Probe, A52Init },
137 { VLC_FOURCC( 'e', 'a', 'c', '3' ), true, "eac3 audio", EA52Probe, A52Init },
138 { VLC_FOURCC( 'd', 't', 's', ' ' ), false, "dts audio", DtsProbe, DtsInit },
139 { VLC_FOURCC( 'm', 'l', 'p', ' ' ), false, "mlp audio", MlpProbe, MlpInit },
141 { 0, false, NULL, NULL, NULL }
144 /*****************************************************************************
145 * Open: initializes demux structures
146 *****************************************************************************/
147 static int Open( vlc_object_t * p_this )
149 demux_t *p_demux = (demux_t*)p_this;
156 for( i_index = 0; p_codec[i_index].i_codec != 0; i_index++ )
158 if( !p_codec[i_index].pf_probe( p_demux, &i_offset ) )
162 if( p_codec[i_index].i_codec == 0 )
165 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
166 memset( p_sys, 0, sizeof( demux_sys_t ) );
167 p_sys->codec = p_codec[i_index];
169 p_sys->b_start = true;
170 p_sys->i_stream_offset = i_offset;
171 p_sys->b_estimate_bitrate = true;
172 p_sys->i_bitrate_avg = 0;
173 p_sys->b_big_endian = false;
175 if( stream_Seek( p_demux->s, p_sys->i_stream_offset ) )
181 if( p_sys->codec.pf_init( p_demux ) )
187 msg_Dbg( p_demux, "detected format %4.4s", (const char*)&p_sys->codec.i_codec );
189 /* Load the audio packetizer */
190 es_format_Init( &fmt, AUDIO_ES, p_sys->codec.i_codec );
191 p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, p_sys->codec.psz_name );
192 if( !p_sys->p_packetizer )
200 /*****************************************************************************
201 * Demux: reads and demuxes data packets
202 *****************************************************************************
203 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
204 *****************************************************************************/
205 static int Demux( demux_t *p_demux )
207 demux_sys_t *p_sys = p_demux->p_sys;
208 block_t *p_block_in, *p_block_out;
210 if( p_sys->codec.b_use_word )
212 /* Make sure we are word aligned */
213 int64_t i_pos = stream_Tell( p_demux->s );
215 stream_Read( p_demux->s, NULL, 1 );
218 if( ( p_block_in = stream_Block( p_demux->s, p_sys->i_packet_size ) ) == NULL )
221 if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
223 /* Convert to big endian */
224 swab( p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer );
227 p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start || p_sys->b_initial_sync_failed ? 1 : 0;
228 p_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
230 while( ( p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in ) ) )
232 p_sys->b_initial_sync_failed = false;
235 block_t *p_next = p_block_out->p_next;
239 p_sys->p_packetizer->fmt_out.b_packetized = true;
240 p_sys->p_es = es_out_Add( p_demux->out,
241 &p_sys->p_packetizer->fmt_out);
244 /* Try the xing header */
245 if( p_sys->xing.i_bytes && p_sys->xing.i_frames &&
246 p_sys->xing.i_frame_samples )
248 p_sys->i_bitrate_avg = p_sys->xing.i_bytes * INT64_C(8) *
249 p_sys->p_packetizer->fmt_out.audio.i_rate /
250 p_sys->xing.i_frames / p_sys->xing.i_frame_samples;
252 if( p_sys->i_bitrate_avg > 0 )
253 p_sys->b_estimate_bitrate = false;
255 /* Use the bitrate as initual value */
256 if( p_sys->b_estimate_bitrate )
257 p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
260 p_sys->i_pts = p_block_out->i_pts;
262 /* Re-estimate bitrate */
263 if( p_sys->b_estimate_bitrate && p_sys->i_pts > 1 + INT64_C(500000) )
264 p_sys->i_bitrate_avg = 8*INT64_C(1000000)*p_sys->i_bytes/(p_sys->i_pts-1);
265 p_sys->i_bytes += p_block_out->i_buffer;
267 /* Correct timestamp */
268 p_block_out->i_pts += p_sys->i_time_offset;
269 p_block_out->i_dts += p_sys->i_time_offset;
271 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
273 es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
275 p_block_out = p_next;
279 if( p_sys->b_initial_sync_failed )
280 msg_Dbg( p_demux, "did not sync on first block" );
281 p_sys->b_start = false;
285 /*****************************************************************************
286 * Close: frees unused data
287 *****************************************************************************/
288 static void Close( vlc_object_t * p_this )
290 demux_t *p_demux = (demux_t*)p_this;
291 demux_sys_t *p_sys = p_demux->p_sys;
293 demux_PacketizerDestroy( p_sys->p_packetizer );
297 /*****************************************************************************
299 *****************************************************************************/
300 static int Control( demux_t *p_demux, int i_query, va_list args )
302 demux_sys_t *p_sys = p_demux->p_sys;
308 va_copy ( args_save, args );
312 case DEMUX_HAS_UNSUPPORTED_META:
313 pb_bool = (bool*)va_arg( args, bool* );
318 pi64 = (int64_t*)va_arg( args, int64_t * );
319 *pi64 = p_sys->i_pts + p_sys->i_time_offset;
322 case DEMUX_GET_LENGTH:
323 i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
324 p_sys->i_bitrate_avg, 1, i_query,
326 /* No bitrate, we can't have it precisely, but we can compute
327 * a raw approximation with time/position */
328 if( i_ret && !p_sys->i_bitrate_avg )
330 float f_pos = (double)(uint64_t)( stream_Tell( p_demux->s ) ) /
331 (double)(uint64_t)( stream_Size( p_demux->s ) );
332 /* The first few seconds are guaranteed to be very whacky,
333 * don't bother trying ... Too bad */
335 (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
338 pi64 = (int64_t *)va_arg( args_save, int64_t * );
339 *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
346 /* FIXME TODO: implement a high precision seek (with mp3 parsing)
347 * needed for multi-input */
349 i_ret = demux_vaControlHelper( p_demux->s, p_sys->i_stream_offset, -1,
350 p_sys->i_bitrate_avg, 1, i_query,
352 if( !i_ret && p_sys->i_bitrate_avg > 0 &&
353 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
355 int64_t i_time = INT64_C(8000000) * ( stream_Tell(p_demux->s) - p_sys->i_stream_offset ) /
356 p_sys->i_bitrate_avg;
358 /* Fix time_offset */
360 p_sys->i_time_offset = i_time - p_sys->i_pts;
366 /*****************************************************************************
368 *****************************************************************************/
369 static int MpgaCheckSync( const uint8_t *p_peek )
371 uint32_t h = GetDWBE( p_peek );
373 if( ((( h >> 21 )&0x07FF) != 0x07FF ) /* header sync */
374 || (((h >> 17)&0x03) == 0 ) /* valid layer ?*/
375 || (((h >> 12)&0x0F) == 0x0F )
376 || (((h >> 12)&0x0F) == 0x00 ) /* valid bitrate ? */
377 || (((h >> 10) & 0x03) == 0x03 ) /* valide sampling freq ? */
378 || ((h & 0x03) == 0x02 )) /* valid emphasis ? */
385 #define MPGA_VERSION( h ) ( 1 - (((h)>>19)&0x01) )
386 #define MPGA_MODE(h) (((h)>> 6)&0x03)
388 static int MpgaGetFrameSamples( uint32_t h )
390 const int i_layer = 3 - (((h)>>17)&0x03);
398 return MPGA_VERSION(h) ? 576 : 1152;
404 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset )
410 const uint8_t *p_peek;
412 b_forced = demux_IsPathExtension( p_demux, ".mp3" );
413 b_forced_demux = demux_IsForced( p_demux, "mp3" ) ||
414 demux_IsForced( p_demux, "mpga" );
416 i_offset = stream_Tell( p_demux->s );
417 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
420 if( !MpgaCheckSync( p_peek ) )
425 if( !b_forced_demux && !b_forced )
428 i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
431 if( MpgaCheckSync( p_peek ) )
440 if( !b_ok && !b_forced_demux )
443 *pi_offset = i_offset;
447 static void MpgaXingSkip( const uint8_t **pp_xing, int *pi_xing, int i_count )
449 if(i_count > *pi_xing )
452 (*pp_xing) += i_count;
453 (*pi_xing) -= i_count;
456 static uint32_t MpgaXingGetDWBE( const uint8_t **pp_xing, int *pi_xing, uint32_t i_default )
461 uint32_t v = GetDWBE( *pp_xing );
463 MpgaXingSkip( pp_xing, pi_xing, 4 );
468 static int MpgaInit( demux_t *p_demux )
470 demux_sys_t *p_sys = p_demux->p_sys;
472 const uint8_t *p_peek;
476 p_sys->i_packet_size = 1024;
478 /* Load a potential xing header */
479 i_peek = stream_Peek( p_demux->s, &p_peek, 4 + 1024 );
480 if( i_peek < 4 + 21 )
483 const uint32_t header = GetDWBE( p_peek );
484 if( !MpgaCheckSync( p_peek ) )
488 const uint8_t *p_xing = p_peek;
492 if( MPGA_VERSION( header ) == 0 )
493 i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
495 i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
497 if( i_skip + 8 >= i_xing || memcmp( &p_xing[i_skip], "Xing", 4 ) )
500 const uint32_t i_flags = GetDWBE( &p_xing[i_skip+4] );
502 MpgaXingSkip( &p_xing, &i_xing, i_skip + 8 );
505 p_sys->xing.i_frames = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
507 p_sys->xing.i_bytes = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
508 if( i_flags&0x04 ) /* TODO Support XING TOC to improve seeking accuracy */
509 MpgaXingSkip( &p_xing, &i_xing, 100 );
512 /* FIXME: doesn't return the right bitrage average, at least
514 p_sys->xing.i_bitrate_avg = MpgaXingGetDWBE( &p_xing, &i_xing, 0 );
515 msg_Dbg( p_demux, "xing vbr value present (%d)",
516 p_sys->xing.i_bitrate_avg );
519 if( p_sys->xing.i_frames > 0 && p_sys->xing.i_bytes > 0 )
521 p_sys->xing.i_frame_samples = MpgaGetFrameSamples( header );
522 msg_Dbg( p_demux, "xing frames&bytes value present "
523 "(%d bytes, %d frames, %d samples/frame)",
524 p_sys->xing.i_bytes, p_sys->xing.i_frames,
525 p_sys->xing.i_frame_samples );
530 /*****************************************************************************
532 *****************************************************************************/
533 static int AacProbe( demux_t *p_demux, int64_t *pi_offset )
539 const uint8_t *p_peek;
541 b_forced = demux_IsPathExtension( p_demux, ".aac" ) ||
542 demux_IsPathExtension( p_demux, ".aacp" );
543 b_forced_demux = demux_IsForced( p_demux, "m4a" ) ||
544 demux_IsForced( p_demux, "aac" ) ||
545 demux_IsForced( p_demux, "mp4a" );
547 if( !b_forced_demux && !b_forced )
550 i_offset = stream_Tell( p_demux->s );
552 /* peek the begining (10 is for adts header) */
553 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
555 msg_Err( p_demux, "cannot peek" );
558 if( !strncmp( (char *)p_peek, "ADIF", 4 ) )
560 msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
564 *pi_offset = i_offset;
567 static int AacInit( demux_t *p_demux )
569 demux_sys_t *p_sys = p_demux->p_sys;
571 p_sys->i_packet_size = 4096;
576 /*****************************************************************************
578 *****************************************************************************/
579 #define WAV_PROBE_SIZE (512*1024)
580 static int WavSkipHeader( demux_t *p_demux, int *pi_skip )
582 const uint8_t *p_peek;
588 /* Check if we are dealing with a WAV file */
589 if( 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 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
600 if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
604 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
608 /* Sanity check the wave format header */
609 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
610 if( i_len > WAV_PROBE_SIZE )
614 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
616 if( GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ ) !=
617 1 /* WAVE_FORMAT_PCM */ )
619 if( GetWLE( p_peek + i_peek - i_len - 6 /* nChannels */ ) != 2 )
621 if( GetDWLE( p_peek + i_peek - i_len - 4 /* nSamplesPerSec */ ) !=
625 /* Skip the wave header */
626 while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
628 uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
629 if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
633 if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
640 static int GenericProbe( demux_t *p_demux, int64_t *pi_offset,
641 const char * ppsz_name[],
642 bool (*pf_check)( const uint8_t * ), int i_check_size )
647 const uint8_t *p_peek;
650 b_forced_demux = false;
651 for( int i = 0; ppsz_name[i] != NULL; i++ )
653 b_forced_demux |= demux_IsForced( p_demux, ppsz_name[i] );
656 i_offset = stream_Tell( p_demux->s );
658 if( WavSkipHeader( p_demux, &i_skip ) )
660 if( !b_forced_demux )
663 const bool b_wav = i_skip > 0;
666 * It is common that wav files have some sort of garbage at the begining */
667 const int i_probe = i_skip + i_check_size + ( b_wav ? 16000 : 0);
668 const int i_peek = stream_Peek( p_demux->s, &p_peek, i_probe );
669 if( i_peek < i_skip + i_check_size )
671 msg_Err( p_demux, "cannot peek" );
676 if( i_skip + i_check_size > i_peek )
678 if( !b_forced_demux )
682 if( pf_check( &p_peek[i_skip] ) )
687 *pi_offset = i_offset + i_skip;
691 /*****************************************************************************
693 *****************************************************************************/
694 static bool A52CheckSync( const uint8_t *p_peek, bool *p_big_endian, bool b_eac3 )
696 /* bsid: 0-8 11-16 */
698 /* Little endian version of the bitstream */
699 if( p_peek[0] == 0x77 && p_peek[1] == 0x0b &&
700 ( p_peek[4] >> 3 ) <= ( b_eac3 ? 16 : 10 ) /* bsid */ )
702 *p_big_endian = false;
705 /* Big endian version of the bitstream */
706 else if( p_peek[0] == 0x0b && p_peek[1] == 0x77 &&
707 ( p_peek[5] >> 3 ) <= ( b_eac3 ? 16 : 10 ) /* bsid */ )
709 *p_big_endian = true;
715 static bool EA52CheckSyncProbe( const uint8_t *p_peek )
718 return A52CheckSync( p_peek, &b_dummy, true );
721 static int EA52Probe( demux_t *p_demux, int64_t *pi_offset )
723 const char *ppsz_name[] = { "eac3", NULL };
725 return GenericProbe( p_demux, pi_offset, ppsz_name, EA52CheckSyncProbe, 10 );
728 static bool A52CheckSyncProbe( const uint8_t *p_peek )
731 return A52CheckSync( p_peek, &b_dummy, false );
734 static int A52Probe( demux_t *p_demux, int64_t *pi_offset )
736 const char *ppsz_name[] = { "a52", "ac3", NULL };
738 return GenericProbe( p_demux, pi_offset, ppsz_name, A52CheckSyncProbe, 10 );
741 static int A52Init( demux_t *p_demux )
743 demux_sys_t *p_sys = p_demux->p_sys;
745 p_sys->b_big_endian = false;
746 p_sys->i_packet_size = 1024;
748 const uint8_t *p_peek;
750 /* peek the begining (10 is for a52 header) */
751 if( stream_Peek( p_demux->s, &p_peek, 10 ) >= 10 )
753 A52CheckSync( p_peek, &p_sys->b_big_endian, true );
758 /*****************************************************************************
760 *****************************************************************************/
761 static bool DtsCheckSync( const uint8_t *p_peek )
763 /* 14 bits, little endian version of the bitstream */
764 if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
765 p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
766 (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
770 /* 14 bits, big endian version of the bitstream */
771 else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
772 p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
773 p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
777 /* 16 bits, big endian version of the bitstream */
778 else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
779 p_peek[2] == 0x80 && p_peek[3] == 0x01 )
783 /* 16 bits, little endian version of the bitstream */
784 else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
785 p_peek[2] == 0x01 && p_peek[3] == 0x80 )
793 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset )
795 const char *ppsz_name[] = { "dts", NULL };
797 return GenericProbe( p_demux, pi_offset, ppsz_name, DtsCheckSync, 11 );
799 static int DtsInit( demux_t *p_demux )
801 demux_sys_t *p_sys = p_demux->p_sys;
803 p_sys->i_packet_size = 16384;
808 /*****************************************************************************
810 *****************************************************************************/
811 static bool MlpCheckSync( const uint8_t *p_peek )
813 if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
816 if( p_peek[4+3] != 0xba && p_peek[4+3] != 0xbb )
823 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset )
825 const char *ppsz_name[] = { "mlp", NULL };
827 return GenericProbe( p_demux, pi_offset, ppsz_name, MlpCheckSync, 4+28+16*4 );
829 static int MlpInit( demux_t *p_demux )
832 demux_sys_t *p_sys = p_demux->p_sys;
834 p_sys->i_packet_size = 4096;