]> git.sesse.net Git - vlc/blobdiff - modules/demux/mpeg/es.c
Always use swab.
[vlc] / modules / demux / mpeg / es.c
index 5b5b8d7e42c24a67d1e153dc2fe00f6146e29570..862f1024d9935a18ff0a108048278c2ca560c4e7 100644 (file)
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-vlc_module_begin();
-    set_category( CAT_INPUT );
-    set_subcategory( SUBCAT_INPUT_DEMUX );
-    set_description( N_("MPEG-I/II/4 audio" ) );
-    set_capability( "demux", 100 );
-    set_callbacks( Open, Close );
+vlc_module_begin ()
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_DEMUX )
+    set_description( N_("MPEG-I/II/4 / A52 / DTS / MLP audio" ) )
+    set_capability( "demux", 155 )
+    set_callbacks( Open, Close )
 
-    add_shortcut( "mpga" );
-    add_shortcut( "mp3" );
+    add_shortcut( "mpga" )
+    add_shortcut( "mp3" )
 
-    add_shortcut( "m4a" );
-    add_shortcut( "mp4a" );
-    add_shortcut( "aac" );
+    add_shortcut( "m4a" )
+    add_shortcut( "mp4a" )
+    add_shortcut( "aac" )
 
-    //add_shortcut( "ac3" );
-    //add_shortcut( "a52" );
-vlc_module_end();
+    add_shortcut( "ac3" )
+    add_shortcut( "a52" )
+
+    add_shortcut( "eac3" )
+
+    add_shortcut( "dts" )
+
+    add_shortcut( "mlp" )
+vlc_module_end ()
 
 /*****************************************************************************
  * Local prototypes
@@ -69,6 +75,7 @@ static int Control( demux_t *, int, va_list );
 typedef struct
 {
     vlc_fourcc_t i_codec;
+    bool       b_use_word;
     const char *psz_name;
     int  (*pf_probe)( demux_t *p_demux, int64_t *pi_offset );
     int  (*pf_init)( demux_t *p_demux );
@@ -87,6 +94,7 @@ struct demux_sys_t
     mtime_t     i_time_offset;
     int64_t     i_bytes;
 
+    bool        b_big_endian;
     bool        b_estimate_bitrate;
     int         i_bitrate_avg;  /* extracted from Xing header */
 
@@ -106,53 +114,32 @@ struct demux_sys_t
     } xing;
 };
 
-#define FCC_MPGA VLC_FOURCC( 'm', 'p', 'g', 'a' )
-
 static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset );
 static int MpgaInit( demux_t *p_demux );
 
 static int AacProbe( demux_t *p_demux, int64_t *pi_offset );
 static int AacInit( demux_t *p_demux );
 
-static const codec_t p_codec[] = {
-    { VLC_FOURCC( 'm', 'p', '4', 'a' ), "mp4 audio", AacProbe, AacInit },
-    { VLC_FOURCC( 'm', 'p', 'g', 'a' ), "mpeg audio", MpgaProbe, MpgaInit },
-
-    { 0, NULL, NULL, NULL }
-};
-
-static inline decoder_t *demux_PacketizerNew( demux_t *p_demux, int i_cat, vlc_fourcc_t i_codec, const char *psz_msg )
-{
-    decoder_t *p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_PACKETIZER );
-
-    if( !p_packetizer )
-        return NULL;
-
-    p_packetizer->pf_decode_audio = NULL;
-    p_packetizer->pf_decode_video = NULL;
-    p_packetizer->pf_decode_sub = NULL;
-    p_packetizer->pf_packetize = NULL;
+static int EA52Probe( demux_t *p_demux, int64_t *pi_offset );
+static int A52Probe( demux_t *p_demux, int64_t *pi_offset );
+static int A52Init( demux_t *p_demux );
 
-    es_format_Init( &p_packetizer->fmt_in, i_cat, i_codec );
-    es_format_Init( &p_packetizer->fmt_out, UNKNOWN_ES, 0 );
+static int DtsProbe( demux_t *p_demux, int64_t *pi_offset );
+static int DtsInit( demux_t *p_demux );
 
-    p_packetizer->p_module = module_Need( p_packetizer, "packetizer", NULL, 0 );
-    if( !p_packetizer->p_module )
-    {
-        vlc_object_release( p_packetizer );
-        msg_Err( p_demux, "cannot find packetizer for %s", psz_msg );
-        return NULL;
-    }
-
-    return p_packetizer;
-}
+static int MlpProbe( demux_t *p_demux, int64_t *pi_offset );
+static int MlpInit( demux_t *p_demux );
 
-static inline void demux_PacketizerDestroy( decoder_t *p_packetizer )
-{
-    if( p_packetizer->p_module )
-        module_Unneed( p_packetizer, p_packetizer->p_module );
-    vlc_object_release( p_packetizer );
-}
+static const codec_t p_codec[] = {
+    { VLC_FOURCC( 'm', 'p', '4', 'a' ), false, "mp4 audio",  AacProbe,  AacInit },
+    { VLC_FOURCC( 'm', 'p', 'g', 'a' ), false, "mpeg audio", MpgaProbe, MpgaInit },
+    { VLC_FOURCC( 'a', '5', '2', ' ' ), true,  "a52 audio",  A52Probe,  A52Init },
+    { VLC_FOURCC( 'e', 'a', 'c', '3' ), true,  "eac3 audio", EA52Probe, A52Init },
+    { VLC_FOURCC( 'd', 't', 's', ' ' ), false, "dts audio",  DtsProbe,  DtsInit },
+    { VLC_FOURCC( 'm', 'l', 'p', ' ' ), false, "mlp audio",  MlpProbe,  MlpInit },
+
+    { 0, false, NULL, NULL, NULL }
+};
 
 /*****************************************************************************
  * Open: initializes demux structures
@@ -162,6 +149,7 @@ static int Open( vlc_object_t * p_this )
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys;
 
+    es_format_t fmt;
     int64_t i_offset;
     int i_index;
 
@@ -182,6 +170,7 @@ static int Open( vlc_object_t * p_this )
     p_sys->i_stream_offset = i_offset;
     p_sys->b_estimate_bitrate = true;
     p_sys->i_bitrate_avg = 0;
+    p_sys->b_big_endian = false;
 
     if( stream_Seek( p_demux->s, p_sys->i_stream_offset ) )
     {
@@ -195,9 +184,11 @@ static int Open( vlc_object_t * p_this )
         return VLC_EGENERIC;
     }
 
+    msg_Dbg( p_demux, "detected format %4.4s", (const char*)&p_sys->codec.i_codec );
+
     /* Load the audio packetizer */
-    p_sys->p_packetizer = demux_PacketizerNew( p_demux, AUDIO_ES, p_sys->codec.i_codec,
-                                               p_sys->codec.psz_name );
+    es_format_Init( &fmt, AUDIO_ES, p_sys->codec.i_codec );
+    p_sys->p_packetizer = demux_PacketizerNew( p_demux, &fmt, p_sys->codec.psz_name );
     if( !p_sys->p_packetizer )
     {
         free( p_sys );
@@ -216,9 +207,23 @@ static int Demux( demux_t *p_demux )
     demux_sys_t *p_sys = p_demux->p_sys;
     block_t *p_block_in, *p_block_out;
 
+    if( p_sys->codec.b_use_word )
+    {
+        /* Make sure we are word aligned */
+        int64_t i_pos = stream_Tell( p_demux->s );
+        if( i_pos % 2 )
+            stream_Read( p_demux->s, NULL, 1 );
+    }
+
     if( ( p_block_in = stream_Block( p_demux->s, p_sys->i_packet_size ) ) == NULL )
         return 0;
 
+    if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
+    {
+        /* Convert to big endian */
+        swab( p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer );
+    }
+
     p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start || p_sys->b_initial_sync_failed ? 1 : 0;
     p_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
 
@@ -361,7 +366,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
 /*****************************************************************************
  * Mpeg I/II Audio
  *****************************************************************************/
-static int CheckSyncMpga( const uint8_t *p_peek )
+static int MpgaCheckSync( const uint8_t *p_peek )
 {
     uint32_t h = GetDWBE( p_peek );
 
@@ -412,7 +417,7 @@ static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset )
     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
         return VLC_EGENERIC;
 
-    if( !CheckSyncMpga( p_peek ) )
+    if( !MpgaCheckSync( p_peek ) )
     {
         bool b_ok = false;
         int i_peek;
@@ -423,7 +428,7 @@ static int MpgaProbe( demux_t *p_demux, int64_t *pi_offset )
         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
         while( i_peek > 4 )
         {
-            if( CheckSyncMpga( p_peek ) )
+            if( MpgaCheckSync( p_peek ) )
             {
                 b_ok = true;
                 break;
@@ -476,7 +481,7 @@ static int MpgaInit( demux_t *p_demux )
         return VLC_SUCCESS;
 
     const uint32_t header = GetDWBE( p_peek );
-    if( !CheckSyncMpga( p_peek ) )
+    if( !MpgaCheckSync( p_peek ) )
         return VLC_SUCCESS;
 
     /* Xing header */
@@ -568,3 +573,266 @@ static int AacInit( demux_t *p_demux )
     return VLC_SUCCESS;
 }
 
+/*****************************************************************************
+ * Wav header skipper
+ *****************************************************************************/
+#define WAV_PROBE_SIZE (512*1024)
+static int WavSkipHeader( demux_t *p_demux, int *pi_skip )
+{
+    const uint8_t *p_peek;
+    int         i_peek = 0;
+
+    /* */
+    *pi_skip = 0;
+
+    /* Check if we are dealing with a WAV file */
+    if( stream_Peek( p_demux->s, &p_peek, 12+8 ) != 12 + 8 )
+        return VLC_SUCCESS;
+
+    if( memcmp( p_peek, "RIFF", 4 ) || memcmp( &p_peek[8], "WAVE", 4 ) )
+        return VLC_SUCCESS;
+
+    /* Find the wave format header */
+    i_peek = 12 + 8;
+    while( memcmp( p_peek + i_peek - 8, "fmt ", 4 ) )
+    {
+        uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
+        if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
+            return VLC_EGENERIC;
+
+        i_peek += i_len + 8;
+        if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
+            return VLC_EGENERIC;
+    }
+
+    /* Sanity check the wave format header */
+    uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
+    if( i_len > WAV_PROBE_SIZE )
+        return VLC_EGENERIC;
+
+    i_peek += i_len + 8;
+    if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
+        return VLC_EGENERIC;
+    if( GetWLE( p_peek + i_peek - i_len - 8 /* wFormatTag */ ) !=
+        1 /* WAVE_FORMAT_PCM */ )
+        return VLC_EGENERIC;
+    if( GetWLE( p_peek + i_peek - i_len - 6 /* nChannels */ ) != 2 )
+        return VLC_EGENERIC;
+    if( GetDWLE( p_peek + i_peek - i_len - 4 /* nSamplesPerSec */ ) !=
+        44100 )
+        return VLC_EGENERIC;
+
+    /* Skip the wave header */
+    while( memcmp( p_peek + i_peek - 8, "data", 4 ) )
+    {
+        uint32_t i_len = GetDWLE( p_peek + i_peek - 4 );
+        if( i_len > WAV_PROBE_SIZE || i_peek + i_len > WAV_PROBE_SIZE )
+            return VLC_EGENERIC;
+
+        i_peek += i_len + 8;
+        if( stream_Peek( p_demux->s, &p_peek, i_peek ) != i_peek )
+            return VLC_EGENERIC;
+    }
+    *pi_skip = i_peek;
+    return VLC_SUCCESS;
+}
+
+static int GenericProbe( demux_t *p_demux, int64_t *pi_offset,
+                         const char * ppsz_name[],
+                         bool (*pf_check)( const uint8_t * ), int i_check_size )
+{
+    bool   b_forced_demux;
+
+    int64_t i_offset;
+    const uint8_t *p_peek;
+    int i_skip;
+
+    b_forced_demux = false;
+    for( int i = 0; ppsz_name[i] != NULL; i++ )
+    {
+        b_forced_demux |= demux_IsForced( p_demux, ppsz_name[i] );
+    }
+
+    i_offset = stream_Tell( p_demux->s );
+
+    if( WavSkipHeader( p_demux, &i_skip ) )
+    {
+        if( !b_forced_demux )
+            return VLC_EGENERIC;
+    }
+    const bool b_wav = i_skip > 0;
+
+    /* peek the begining
+     * It is common that wav files have some sort of garbage at the begining */
+    const int i_probe = i_skip + i_check_size + ( b_wav ? 16000 : 0);
+    const int i_peek = stream_Peek( p_demux->s, &p_peek, i_probe );
+    if( i_peek < i_skip + i_check_size )
+    {
+        msg_Err( p_demux, "cannot peek" );
+        return VLC_EGENERIC;
+    }
+    for( ;; )
+    {
+        if( i_skip + i_check_size > i_peek )
+        {
+            if( !b_forced_demux )
+                return VLC_EGENERIC;
+            break;
+        }
+        if( pf_check( &p_peek[i_skip] ) )
+            break;
+        i_skip++;
+    }
+
+    *pi_offset = i_offset + i_skip;
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * A52
+ *****************************************************************************/
+static bool A52CheckSync( const uint8_t *p_peek, bool *p_big_endian, bool b_eac3 )
+{
+    /* bsid: 0-8 11-16 */
+
+    /* Little endian version of the bitstream */
+    if( p_peek[0] == 0x77 && p_peek[1] == 0x0b &&
+        ( p_peek[4] >> 3 ) <= ( b_eac3 ? 16 : 10 ) /* bsid */ )
+    {
+        *p_big_endian = false;
+        return true;
+    }
+    /* Big endian version of the bitstream */
+    else if( p_peek[0] == 0x0b && p_peek[1] == 0x77 &&
+             ( p_peek[5] >> 3 ) <= ( b_eac3 ? 16 : 10 ) /* bsid */ )
+    {
+        *p_big_endian = true;
+        return true;
+    }
+
+    return false;
+}
+static bool EA52CheckSyncProbe( const uint8_t *p_peek )
+{
+    bool b_dummy;
+    return A52CheckSync( p_peek, &b_dummy, true );
+}
+
+static int EA52Probe( demux_t *p_demux, int64_t *pi_offset )
+{
+    const char *ppsz_name[] = { "eac3", NULL };
+
+    return GenericProbe( p_demux, pi_offset, ppsz_name, EA52CheckSyncProbe, 10 );
+}
+
+static bool A52CheckSyncProbe( const uint8_t *p_peek )
+{
+    bool b_dummy;
+    return A52CheckSync( p_peek, &b_dummy, false );
+}
+
+static int A52Probe( demux_t *p_demux, int64_t *pi_offset )
+{
+    const char *ppsz_name[] = { "a52", "ac3", NULL };
+
+    return GenericProbe( p_demux, pi_offset, ppsz_name, A52CheckSyncProbe, 10 );
+}
+
+static int A52Init( demux_t *p_demux )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
+
+    p_sys->b_big_endian = false;
+    p_sys->i_packet_size = 1024;
+
+    const uint8_t *p_peek;
+
+    /* peek the begining (10 is for a52 header) */
+    if( stream_Peek( p_demux->s, &p_peek, 10 ) >= 10 )
+    {
+        A52CheckSync( p_peek, &p_sys->b_big_endian, true );
+    }
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * DTS
+ *****************************************************************************/
+static bool DtsCheckSync( const uint8_t *p_peek )
+{
+    /* 14 bits, little endian version of the bitstream */
+    if( p_peek[0] == 0xff && p_peek[1] == 0x1f &&
+        p_peek[2] == 0x00 && p_peek[3] == 0xe8 &&
+        (p_peek[4] & 0xf0) == 0xf0 && p_peek[5] == 0x07 )
+    {
+        return true;
+    }
+    /* 14 bits, big endian version of the bitstream */
+    else if( p_peek[0] == 0x1f && p_peek[1] == 0xff &&
+             p_peek[2] == 0xe8 && p_peek[3] == 0x00 &&
+             p_peek[4] == 0x07 && (p_peek[5] & 0xf0) == 0xf0)
+    {
+        return true;
+    }
+    /* 16 bits, big endian version of the bitstream */
+    else if( p_peek[0] == 0x7f && p_peek[1] == 0xfe &&
+             p_peek[2] == 0x80 && p_peek[3] == 0x01 )
+    {
+        return true;
+    }
+    /* 16 bits, little endian version of the bitstream */
+    else if( p_peek[0] == 0xfe && p_peek[1] == 0x7f &&
+             p_peek[2] == 0x01 && p_peek[3] == 0x80 )
+    {
+        return true;
+    }
+
+    return false;
+}
+
+static int DtsProbe( demux_t *p_demux, int64_t *pi_offset )
+{
+    const char *ppsz_name[] = { "dts", NULL };
+
+    return GenericProbe( p_demux, pi_offset, ppsz_name, DtsCheckSync, 11 );
+}
+static int DtsInit( demux_t *p_demux )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
+
+    p_sys->i_packet_size = 16384;
+
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * MLP
+ *****************************************************************************/
+static bool MlpCheckSync( const uint8_t *p_peek )
+{
+    if( p_peek[4+0] != 0xf8 || p_peek[4+1] != 0x72 || p_peek[4+2] != 0x6f )
+        return false;
+
+    if( p_peek[4+3] != 0xba && p_peek[4+3] != 0xbb )
+        return false;
+
+    /* TODO checksum */
+
+    return true;
+}
+static int MlpProbe( demux_t *p_demux, int64_t *pi_offset )
+{
+    const char *ppsz_name[] = { "mlp", NULL };
+
+    return GenericProbe( p_demux, pi_offset, ppsz_name, MlpCheckSync, 4+28+16*4 );
+}
+static int MlpInit( demux_t *p_demux )
+
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
+
+    p_sys->i_packet_size = 4096;
+
+    return VLC_SUCCESS;
+}
+