]> git.sesse.net Git - vlc/blobdiff - modules/demux/mpeg/es.c
vout_macosx: protect vout_display_SendEvent calls to prevent potential crashes
[vlc] / modules / demux / mpeg / es.c
index 627fc7c3c8c2f4254caf7f3d14611b757a317f0f..5d5da94777767c16c6f4a71fff50f0815e6762eb 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * es.c : Generic audio ES input module for vlc
  *****************************************************************************
- * Copyright (C) 2001-2008 the VideoLAN team
+ * Copyright (C) 2001-2008 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Gildas Bazin <gbazin@videolan.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -38,6 +38,7 @@
 #include <vlc_input.h>
 
 #include "../../codec/a52.h"
+#include "../../codec/dts_header.h"
 
 /*****************************************************************************
  * Module descriptor
@@ -54,6 +55,7 @@ vlc_module_begin ()
     set_category( CAT_INPUT )
     set_subcategory( SUBCAT_INPUT_DEMUX )
     set_description( N_("MPEG-I/II/4 / A52 / DTS / MLP audio" ) )
+    set_shortname( N_("Audio ES") )
     set_capability( "demux", 155 )
     set_callbacks( OpenAudio, Close )
 
@@ -141,7 +143,7 @@ static int DtsInit( demux_t *p_demux );
 static int MlpProbe( demux_t *p_demux, int64_t *pi_offset );
 static int MlpInit( demux_t *p_demux );
 
-static int Parse( demux_t *p_demux, block_t **pp_output );
+static bool Parse( demux_t *p_demux, block_t **pp_output );
 
 static const codec_t p_codecs[] = {
     { VLC_CODEC_MP4A, false, "mp4 audio",  AacProbe,  AacInit },
@@ -255,13 +257,14 @@ static int OpenVideo( vlc_object_t *p_this )
  *****************************************************************************/
 static int Demux( demux_t *p_demux )
 {
+    int ret = 1;
     demux_sys_t *p_sys = p_demux->p_sys;
 
     block_t *p_block_out = p_sys->p_packetized_data;
     if( p_block_out )
         p_sys->p_packetized_data = NULL;
-    else if( Parse( p_demux, &p_block_out ) )
-        return 0;
+    else
+        ret = Parse( p_demux, &p_block_out ) ? 0 : 1;
 
     while( p_block_out )
     {
@@ -301,7 +304,7 @@ static int Demux( demux_t *p_demux )
 
         p_block_out = p_next;
     }
-    return 1;
+    return ret;
 }
 
 /*****************************************************************************
@@ -392,9 +395,10 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
 }
 
 /*****************************************************************************
- * Returned a link list of buffer of parsed data
+ * Makes a link list of buffer of parsed data
+ * Returns true if EOF
  *****************************************************************************/
-static int Parse( demux_t *p_demux, block_t **pp_output )
+static bool Parse( demux_t *p_demux, block_t **pp_output )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
     block_t *p_block_in, *p_block_out;
@@ -409,19 +413,22 @@ static int Parse( demux_t *p_demux, block_t **pp_output )
             stream_Read( p_demux->s, NULL, 1 );
     }
 
-    if( ( p_block_in = stream_Block( p_demux->s, p_sys->i_packet_size ) ) == NULL )
-        return VLC_EGENERIC;
+    p_block_in = stream_Block( p_demux->s, p_sys->i_packet_size );
+    bool b_eof = p_block_in == NULL;
 
-    if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
+    if( p_block_in )
     {
-        /* Convert to big endian */
-        swab( p_block_in->p_buffer, p_block_in->p_buffer, p_block_in->i_buffer );
-    }
+        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 ? VLC_TS_0 : VLC_TS_INVALID;
+        p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start || p_sys->b_initial_sync_failed ? VLC_TS_0 : VLC_TS_INVALID;
+    }
     p_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
 
-    while( ( p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in ) ) )
+    while( ( p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, p_block_in ? &p_block_in : NULL ) ) )
     {
         p_sys->b_initial_sync_failed = false;
         while( p_block_out )
@@ -461,7 +468,8 @@ static int Parse( demux_t *p_demux, block_t **pp_output )
     if( p_sys->b_initial_sync_failed )
         msg_Dbg( p_demux, "did not sync on first block" );
     p_sys->b_start = false;
-    return VLC_SUCCESS;
+
+    return b_eof;
 }
 
 /*****************************************************************************
@@ -626,9 +634,9 @@ static int MpgaCheckSync( const uint8_t *p_peek )
     uint32_t h = GetDWBE( p_peek );
 
     if( ((( h >> 21 )&0x07FF) != 0x07FF )   /* header sync */
+        || (((h >> 19)&0x03) == 1 )         /* valid version ID ? */
         || (((h >> 17)&0x03) == 0 )         /* valid layer ?*/
-        || (((h >> 12)&0x0F) == 0x0F )
-        || (((h >> 12)&0x0F) == 0x00 )      /* valid bitrate ? */
+        || (((h >> 12)&0x0F) == 0x0F )      /* valid bitrate ?*/
         || (((h >> 10) & 0x03) == 0x03 )    /* valide sampling freq ? */
         || ((h & 0x03) == 0x02 ))           /* valid emphasis ? */
     {
@@ -912,37 +920,22 @@ static int A52Init( demux_t *p_demux )
  *****************************************************************************/
 static int DtsCheckSync( const uint8_t *p_peek, int *pi_samples )
 {
-    /* TODO return frame size for robustness */
-
-    /* 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 0;
-    }
-    /* 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 0;
-    }
-    /* 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 0;
-    }
-    /* 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 0;
-    }
+    unsigned int i_sample_rate, i_bit_rate, i_frame_length, i_audio_mode;
+    bool b_dts_hd;
 
     VLC_UNUSED(pi_samples);
-    return VLC_EGENERIC;
+
+    int i_frame_size = GetSyncInfo( p_peek,
+                                    &b_dts_hd,
+                                    &i_sample_rate,
+                                    &i_bit_rate,
+                                    &i_frame_length,
+                                    &i_audio_mode );
+
+    if( i_frame_size != VLC_EGENERIC && i_frame_size <= 8192 )
+        return VLC_SUCCESS;
+    else
+        return VLC_EGENERIC;
 }
 
 static int DtsProbe( demux_t *p_demux, int64_t *pi_offset )