]> git.sesse.net Git - vlc/blobdiff - modules/demux/mpeg/mpga.c
Demuxers: do not load "meta reader" module if the input item has already been preparsed.
[vlc] / modules / demux / mpeg / mpga.c
index fefc4299a77d2a8018e96a6812f8e661305eaf83..2d2400ef12c1cae5555d7020955c959773b33364 100644 (file)
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
 
 #include <vlc/vlc.h>
-#include <vlc/input.h>
-#include "vlc_codec.h"
-#include "vlc_meta.h"
+#include <vlc_demux.h>
+#include <vlc_codec.h>
+#include <vlc_input.h>
 
-#define MPGA_PACKET_SIZE 4096
+#define MPGA_PACKET_SIZE 1024
 
 /*****************************************************************************
  * Module descriptor
@@ -43,7 +42,7 @@ static void Close( vlc_object_t * );
 vlc_module_begin();
     set_category( CAT_INPUT );
     set_subcategory( SUBCAT_INPUT_DEMUX );
-    set_description( _("MPEG-I/II audio demuxer" ) );
+    set_description( _("MPEG audio / MP3 demuxer" ) );
     set_capability( "demux2", 100 );
     set_callbacks( Open, Close );
     add_shortcut( "mpga" );
@@ -68,11 +67,16 @@ struct demux_sys_t
     mtime_t     i_time_offset;
     int         i_bitrate_avg;  /* extracted from Xing header */
 
+    vlc_bool_t b_initial_sync_failed;
+
     int i_xing_frames;
     int i_xing_bytes;
     int i_xing_bitrate_avg;
     int i_xing_frame_samples;
     block_t *p_block_in, *p_block_out;
+
+    int                i_attachments;
+    input_attachment_t **attachments;
 };
 
 static int HeaderCheck( uint32_t h )
@@ -108,6 +112,7 @@ static int mpga_frame_samples( uint32_t h )
     }
 }
 
+
 /*****************************************************************************
  * Open: initializes demux structures
  *****************************************************************************/
@@ -118,18 +123,12 @@ static int Open( vlc_object_t * p_this )
     vlc_bool_t   b_forced = VLC_FALSE;
 
     uint32_t     header;
-    uint8_t     *p_peek;
+    const uint8_t     *p_peek;
     module_t    *p_id3;
     block_t     *p_block_in, *p_block_out;
 
-    if( p_demux->psz_path )
-    {
-        int  i_len = strlen( p_demux->psz_path );
-        if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".mp3" ) )
-        {
-            b_forced = VLC_TRUE;
-        }
-    }
+    if( demux2_IsPathExtension( p_demux, ".mp3" ) )
+        b_forced = VLC_TRUE;
 
     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
 
@@ -154,41 +153,22 @@ static int Open( vlc_object_t * p_this )
         if( !b_ok && !p_demux->b_force ) return VLC_EGENERIC;
     }
 
-    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
+    DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
     memset( p_sys, 0, sizeof( demux_sys_t ) );
     p_sys->p_es = 0;
-    p_sys->p_packetizer = 0;
     p_sys->b_start = VLC_TRUE;
     p_sys->meta = 0;
-    p_demux->pf_demux   = Demux;
-    p_demux->pf_control = Control;
-
-    /*
-     * Load the mpeg audio packetizer
-     */
-    p_sys->p_packetizer = vlc_object_create( p_demux, VLC_OBJECT_PACKETIZER );
-    p_sys->p_packetizer->pf_decode_audio = NULL;
-    p_sys->p_packetizer->pf_decode_video = NULL;
-    p_sys->p_packetizer->pf_decode_sub = NULL;
-    p_sys->p_packetizer->pf_packetize = NULL;
-    es_format_Init( &p_sys->p_packetizer->fmt_in, AUDIO_ES,
-                    VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
-    es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
-    p_sys->p_packetizer->p_module =
-        module_Need( p_sys->p_packetizer, "packetizer", NULL, 0 );
 
-    if( p_sys->p_packetizer->p_module == NULL )
-    {
-        msg_Err( p_demux, "cannot find mpga packetizer" );
-        Close( VLC_OBJECT(p_demux ) );
-        return VLC_EGENERIC;
-    }
+    /* Load the mpeg audio packetizer */
+    INIT_APACKETIZER( p_sys->p_packetizer, 'm', 'p', 'g', 'a' );
+    es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
+    LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "mpga" );
 
     /* Xing header */
     if( HeaderCheck( header ) )
     {
         int i_xing, i_skip;
-        uint8_t *p_xing;
+        const uint8_t *p_xing;
 
         if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) < 21 )
             return VLC_SUCCESS; /* No header */
@@ -252,12 +232,17 @@ static int Open( vlc_object_t * p_this )
     p_block_in->i_pts = p_block_in->i_dts = 1;
     p_block_out = p_sys->p_packetizer->pf_packetize(
         p_sys->p_packetizer, &p_block_in );
-    
-    p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
-    p_sys->p_es = es_out_Add( p_demux->out,
-                              &p_sys->p_packetizer->fmt_out);
+
+    if( p_block_out == NULL )
+    {
+        msg_Dbg( p_demux, "did not sync on first block" );
+        p_sys->b_initial_sync_failed = VLC_TRUE;
+    }
+    else
+        p_sys->b_initial_sync_failed = VLC_FALSE;
+
     p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
-    
+
     if( p_sys->i_xing_bytes && p_sys->i_xing_frames &&
         p_sys->i_xing_frame_samples )
     {
@@ -270,13 +255,38 @@ static int Open( vlc_object_t * p_this )
     p_sys->p_block_out = p_block_out;
 
     /* Parse possible id3 header */
-    if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
+    input_thread_t *p_input = (input_thread_t *)
+            vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
+    if( p_input )
     {
-        p_sys->meta = (vlc_meta_t *)p_demux->p_private;
-        p_demux->p_private = NULL;
-        module_Unneed( p_demux, p_id3 );
+        if( !( input_GetItem( p_input )->p_meta->i_status & ITEM_PREPARSED ) )
+        {
+            p_demux->p_private = malloc( sizeof( demux_meta_t ) );
+            if( !p_demux->p_private )
+            {
+                vlc_object_release( p_input );
+                return VLC_ENOMEM;
+            }
+            if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
+            {
+                demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
+                p_sys->meta = p_demux_meta->p_meta;
+                p_demux->p_private = NULL;
+                module_Unneed( p_demux, p_id3 );
+                p_sys->i_attachments = p_demux_meta->i_attachments;
+                p_sys->attachments = p_demux_meta->attachments;
+            }
+            free( p_demux->p_private );
+        }
+        vlc_object_release( p_input );
     }
 
+    /* */
+    p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
+    vlc_audio_replay_gain_MergeFromMeta( &p_sys->p_packetizer->fmt_out.audio_replay_gain,
+                                         p_sys->meta );
+    p_sys->p_es = es_out_Add( p_demux->out,
+                              &p_sys->p_packetizer->fmt_out);
     return VLC_SUCCESS;
 }
 
@@ -293,7 +303,9 @@ static int Demux( demux_t *p_demux )
     {
         p_sys->b_start = VLC_FALSE;
         p_block_in = p_sys->p_block_in;
+        p_sys->p_block_in = NULL;
         p_block_out = p_sys->p_block_out;
+        p_sys->p_block_out = NULL;
     }
     else
     {
@@ -302,7 +314,14 @@ static int Demux( demux_t *p_demux )
         {
             return 0;
         }
-        p_block_in->i_pts = p_block_in->i_dts = 0;
+        if( p_demux->p_sys->b_initial_sync_failed == VLC_TRUE )
+        {
+            p_block_in->i_pts = p_block_in->i_dts = 1;
+            /* Only try to resync once */
+            p_demux->p_sys->b_initial_sync_failed = 0;
+        }
+        else
+            p_block_in->i_pts = p_block_in->i_dts = 0;
         p_block_out = p_sys->p_packetizer->pf_packetize(
             p_sys->p_packetizer, &p_block_in );
     }
@@ -336,12 +355,14 @@ 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;
 
+    DESTROY_PACKETIZER( p_sys->p_packetizer );
     if( p_sys->meta ) vlc_meta_Delete( p_sys->meta );
+    if( p_sys->p_block_out ) block_Release( p_sys->p_block_out );
 
-    if( p_sys->p_packetizer && p_sys->p_packetizer->p_module )
-        module_Unneed( p_sys->p_packetizer, p_sys->p_packetizer->p_module );
-    if( p_sys->p_packetizer )
-        vlc_object_destroy( p_sys->p_packetizer );
+    int i;
+    for( i = 0; i < p_sys->i_attachments; i++ )
+        free( p_sys->attachments[i] );
+    TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
 
     free( p_sys );
 }
@@ -353,15 +374,31 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
 {
     demux_sys_t *p_sys  = p_demux->p_sys;
     int64_t *pi64;
-    vlc_meta_t **pp_meta;
+    vlc_meta_t *p_meta;
     int i_ret;
 
+    input_attachment_t ***ppp_attach;
+    int *pi_int, i;
+
     switch( i_query )
     {
         case DEMUX_GET_META:
-            pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
-            if( p_sys->meta ) *pp_meta = vlc_meta_Duplicate( p_sys->meta );
-            else *pp_meta = NULL;
+            p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
+            vlc_meta_Merge( p_meta, p_sys->meta );
+            return VLC_SUCCESS;
+
+        case DEMUX_GET_ATTACHMENTS:
+            ppp_attach =
+                (input_attachment_t***)va_arg( args, input_attachment_t*** );
+            pi_int = (int*)va_arg( args, int * );
+
+            if( p_sys->i_attachments <= 0 )
+                return VLC_EGENERIC;
+
+            *pi_int = p_sys->i_attachments;
+            *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
+            for( i = 0; i < p_sys->i_attachments; i++ )
+                (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
             return VLC_SUCCESS;
 
         case DEMUX_GET_TIME:
@@ -377,6 +414,22 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             i_ret = demux2_vaControlHelper( p_demux->s, 0, -1,
                                             p_sys->i_bitrate_avg, 1, i_query,
                                             args );
+            /* No bitrate, we can't have it precisely, but we can compute
+             * a raw approximation with time/position */
+            if( i_ret && i_query == DEMUX_GET_LENGTH &&!p_sys->i_bitrate_avg )
+            {
+                float f_pos = (double)( stream_Tell( p_demux->s ) ) /
+                              (double)( stream_Size( p_demux->s ) );
+                /* The first few seconds are guaranteed to be very whacky,
+                 * don't bother trying ... Too bad */
+                if( f_pos < 0.01 ||
+                    (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
+                    return VLC_EGENERIC;
+
+                pi64 = (int64_t *)va_arg( args, int64_t * );
+                *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
+                return VLC_SUCCESS;
+            }
             if( !i_ret && p_sys->i_bitrate_avg > 0 &&
                 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
             {