]> git.sesse.net Git - vlc/blobdiff - modules/demux/mpeg/es.c
LGPL
[vlc] / modules / demux / mpeg / es.c
index 0103e1412532469e33276668a6026a542e25c45b..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 )
 
@@ -68,7 +70,7 @@ vlc_module_begin ()
     set_description( N_("MPEG-4 video" ) )
     set_capability( "demux", 0 )
     set_callbacks( OpenVideo, Close )
-    add_float( "es-fps", 25, NULL, FPS_TEXT, FPS_LONGTEXT, false )
+    add_float( "es-fps", 25, FPS_TEXT, FPS_LONGTEXT, false )
 
     add_shortcut( "m4v" )
     add_shortcut( "mp4v" )
@@ -97,6 +99,7 @@ struct demux_sys_t
 
     bool  b_start;
     decoder_t   *p_packetizer;
+    block_t     *p_packetized_data;
 
     mtime_t     i_pts;
     mtime_t     i_time_offset;
@@ -140,6 +143,8 @@ 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 bool Parse( demux_t *p_demux, block_t **pp_output );
+
 static const codec_t p_codecs[] = {
     { VLC_CODEC_MP4A, false, "mp4 audio",  AacProbe,  AacInit },
     { VLC_CODEC_MPGA, false, "mpeg audio", MpgaProbe, MpgaInit },
@@ -177,6 +182,7 @@ static int OpenCommon( demux_t *p_demux,
     p_sys->i_bitrate_avg = 0;
     p_sys->b_big_endian = false;
     p_sys->f_fps = var_InheritFloat( p_demux, "es-fps" );
+    p_sys->p_packetized_data = NULL;
 
     if( stream_Seek( p_demux->s, p_sys->i_stream_offset ) )
     {
@@ -200,6 +206,14 @@ static int OpenCommon( demux_t *p_demux,
         free( p_sys );
         return VLC_EGENERIC;
     }
+
+    while( vlc_object_alive( p_demux ) )
+    {
+        if( Parse( p_demux, &p_sys->p_packetized_data ) )
+            break;
+        if( p_sys->p_packetized_data )
+            break;
+    }
     return VLC_SUCCESS;
 }
 static int OpenAudio( vlc_object_t *p_this )
@@ -243,97 +257,54 @@ 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_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;
+    block_t *p_block_out = p_sys->p_packetized_data;
+    if( p_block_out )
+        p_sys->p_packetized_data = NULL;
+    else
+        ret = Parse( p_demux, &p_block_out ) ? 0 : 1;
 
-    if( p_sys->codec.b_use_word && !p_sys->b_big_endian && p_block_in->i_buffer > 0 )
+    while( p_block_out )
     {
-        /* 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_sys->b_initial_sync_failed = p_sys->b_start; /* Only try to resync once */
+        block_t *p_next = p_block_out->p_next;
 
-    while( ( p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer, &p_block_in ) ) )
-    {
-        p_sys->b_initial_sync_failed = false;
-        while( p_block_out )
+        /* Correct timestamp */
+        if( p_sys->p_packetizer->fmt_out.i_cat == VIDEO_ES )
         {
-            block_t *p_next = p_block_out->p_next;
-
-            if( !p_sys->p_es )
-            {
-                p_sys->p_packetizer->fmt_out.b_packetized = true;
-                p_sys->p_es = es_out_Add( p_demux->out,
-                                          &p_sys->p_packetizer->fmt_out);
-
-
-                /* Try the xing header */
-                if( p_sys->xing.i_bytes && p_sys->xing.i_frames &&
-                    p_sys->xing.i_frame_samples )
-                {
-                    p_sys->i_bitrate_avg = p_sys->xing.i_bytes * INT64_C(8) *
-                        p_sys->p_packetizer->fmt_out.audio.i_rate /
-                        p_sys->xing.i_frames / p_sys->xing.i_frame_samples;
-
-                    if( p_sys->i_bitrate_avg > 0 )
-                        p_sys->b_estimate_bitrate = false;
-                }
-                /* Use the bitrate as initual value */
-                if( p_sys->b_estimate_bitrate )
-                    p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
-            }
-            if( p_sys->p_packetizer->fmt_out.i_cat == VIDEO_ES )
-            {
-                if( p_block_out->i_pts <= VLC_TS_INVALID &&
-                    p_block_out->i_dts <= VLC_TS_INVALID )
-                    p_block_out->i_dts = VLC_TS_0 + p_sys->i_pts + 1000000 / p_sys->f_fps;
-                if( p_block_out->i_dts > VLC_TS_INVALID )
-                    p_sys->i_pts = p_block_out->i_dts - VLC_TS_0;
-            }
-            else
-            {
-                p_sys->i_pts = p_block_out->i_pts - VLC_TS_0;
-            }
+            if( p_block_out->i_pts <= VLC_TS_INVALID &&
+                p_block_out->i_dts <= VLC_TS_INVALID )
+                p_block_out->i_dts = VLC_TS_0 + p_sys->i_pts + 1000000 / p_sys->f_fps;
+            if( p_block_out->i_dts > VLC_TS_INVALID )
+                p_sys->i_pts = p_block_out->i_dts - VLC_TS_0;
+        }
+        else
+        {
+            p_sys->i_pts = p_block_out->i_pts - VLC_TS_0;
+        }
 
-            /* Re-estimate bitrate */
-            if( p_sys->b_estimate_bitrate && p_sys->i_pts > INT64_C(500000) )
-                p_sys->i_bitrate_avg = 8*INT64_C(1000000)*p_sys->i_bytes/(p_sys->i_pts-1);
-            p_sys->i_bytes += p_block_out->i_buffer;
+        if( p_block_out->i_pts > VLC_TS_INVALID )
+        {
+            p_block_out->i_pts += p_sys->i_time_offset;
+        }
+        if( p_block_out->i_dts > VLC_TS_INVALID )
+        {
+            p_block_out->i_dts += p_sys->i_time_offset;
+            es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
+        }
+        /* Re-estimate bitrate */
+        if( p_sys->b_estimate_bitrate && p_sys->i_pts > INT64_C(500000) )
+            p_sys->i_bitrate_avg = 8*INT64_C(1000000)*p_sys->i_bytes/(p_sys->i_pts-1);
+        p_sys->i_bytes += p_block_out->i_buffer;
 
-            /* Correct timestamp */
-            if( p_block_out->i_pts > VLC_TS_INVALID )
-            {
-                p_block_out->i_pts += p_sys->i_time_offset;
-            }
-            if( p_block_out->i_dts > VLC_TS_INVALID )
-            {
-                p_block_out->i_dts += p_sys->i_time_offset;
-                es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
-            }
 
-            es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
+        p_block_out->p_next = NULL;
+        es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
 
-            p_block_out = p_next;
-        }
+        p_block_out = p_next;
     }
-
-    if( p_sys->b_initial_sync_failed )
-        msg_Dbg( p_demux, "did not sync on first block" );
-    p_sys->b_start = false;
-    return 1;
+    return ret;
 }
 
 /*****************************************************************************
@@ -344,6 +315,8 @@ static void Close( vlc_object_t * p_this )
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys = p_demux->p_sys;
 
+    if( p_sys->p_packetized_data )
+        block_ChainRelease( p_sys->p_packetized_data );
     demux_PacketizerDestroy( p_sys->p_packetizer );
     free( p_sys );
 }
@@ -412,11 +385,93 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
                 /* Fix time_offset */
                 if( i_time >= 0 )
                     p_sys->i_time_offset = i_time - p_sys->i_pts;
+                /* And reset buffered data */
+                if( p_sys->p_packetized_data )
+                    block_ChainRelease( p_sys->p_packetized_data );
+                p_sys->p_packetized_data = NULL;
             }
             return i_ret;
     }
 }
 
+/*****************************************************************************
+ * Makes a link list of buffer of parsed data
+ * Returns true if EOF
+ *****************************************************************************/
+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;
+
+    *pp_output = NULL;
+
+    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 );
+    }
+
+    p_block_in = stream_Block( p_demux->s, p_sys->i_packet_size );
+    bool b_eof = p_block_in == NULL;
+
+    if( p_block_in )
+    {
+        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_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 ? &p_block_in : NULL ) ) )
+    {
+        p_sys->b_initial_sync_failed = false;
+        while( p_block_out )
+        {
+            if( !p_sys->p_es )
+            {
+                p_sys->p_packetizer->fmt_out.b_packetized = true;
+                p_sys->p_es = es_out_Add( p_demux->out,
+                                          &p_sys->p_packetizer->fmt_out);
+
+
+                /* Try the xing header */
+                if( p_sys->xing.i_bytes && p_sys->xing.i_frames &&
+                    p_sys->xing.i_frame_samples )
+                {
+                    p_sys->i_bitrate_avg = p_sys->xing.i_bytes * INT64_C(8) *
+                        p_sys->p_packetizer->fmt_out.audio.i_rate /
+                        p_sys->xing.i_frames / p_sys->xing.i_frame_samples;
+
+                    if( p_sys->i_bitrate_avg > 0 )
+                        p_sys->b_estimate_bitrate = false;
+                }
+                /* Use the bitrate as initual value */
+                if( p_sys->b_estimate_bitrate )
+                    p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
+            }
+
+            block_t *p_next = p_block_out->p_next;
+            p_block_out->p_next = NULL;
+
+            block_ChainLastAppend( &pp_output, p_block_out );
+
+            p_block_out = p_next;
+        }
+    }
+
+    if( p_sys->b_initial_sync_failed )
+        msg_Dbg( p_demux, "did not sync on first block" );
+    p_sys->b_start = false;
+
+    return b_eof;
+}
+
 /*****************************************************************************
  * Wav header skipper
  *****************************************************************************/
@@ -579,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 ? */
     {
@@ -865,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 )