[vlc-devel] [RFC PATCH 05/13] mp4: Extract attachments once

Hugo Beauzée-Luyssen hugo at beauzee.fr
Fri Nov 6 10:22:20 CET 2020


Instead of each time DEMUX_GET_ATTACHMENT gets invoked
---
 modules/demux/mp4/attachments.c |  5 +++--
 modules/demux/mp4/attachments.h |  2 +-
 modules/demux/mp4/mp4.c         | 18 ++++++++++++++++--
 3 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/modules/demux/mp4/attachments.c b/modules/demux/mp4/attachments.c
index 20fa4ea512..2e00c0468e 100644
--- a/modules/demux/mp4/attachments.c
+++ b/modules/demux/mp4/attachments.c
@@ -233,11 +233,11 @@ int MP4_GetCoverMetaURI( const MP4_Box_t *p_root,
     return VLC_SUCCESS;
 }
 
-int MP4_GetAttachments( const MP4_Box_t *p_root, input_attachment_t ***ppp_attach )
+size_t MP4_GetAttachments( const MP4_Box_t *p_root, input_attachment_t ***ppp_attach )
 {
     const MP4_Box_t *p_metaroot = NULL;
     const char *psz_metarootpath;
-    unsigned i_count = 0;
+    size_t i_count = 0;
     input_attachment_t **pp_attach = NULL;
     *ppp_attach = NULL;
 
@@ -371,6 +371,7 @@ int MP4_GetAttachments( const MP4_Box_t *p_root, input_attachment_t ***ppp_attac
     if ( i_count == 0 )
     {
         free( pp_attach );
+        **ppp_attach = NULL;
         return 0;
     }
 
diff --git a/modules/demux/mp4/attachments.h b/modules/demux/mp4/attachments.h
index ee0eb771a7..fe8eaf1b7f 100644
--- a/modules/demux/mp4/attachments.h
+++ b/modules/demux/mp4/attachments.h
@@ -21,7 +21,7 @@
 #ifndef VLC_MP4_ATTACHMENTS_H_
 #define VLC_MP4_ATTACHMENTS_H_
 
-int MP4_GetAttachments( const MP4_Box_t *, input_attachment_t *** );
+size_t MP4_GetAttachments( const MP4_Box_t *, input_attachment_t *** );
 const MP4_Box_t *MP4_GetMetaRoot( const MP4_Box_t *, const char ** );
 int MP4_GetCoverMetaURI( const MP4_Box_t *,  const MP4_Box_t *,
                          const char *, vlc_meta_t * );
diff --git a/modules/demux/mp4/mp4.c b/modules/demux/mp4/mp4.c
index 875d8c4d38..0b76039876 100644
--- a/modules/demux/mp4/mp4.c
+++ b/modules/demux/mp4/mp4.c
@@ -145,6 +145,9 @@ typedef struct
     } hacks;
 
     mp4_fragments_index_t *p_fragsindex;
+
+    size_t i_attachments;
+    input_attachment_t **pp_attachments;
 } demux_sys_t;
 
 #define DEMUX_INCREMENT VLC_TICK_FROM_MS(250) /* How far the pcr will go, each round */
@@ -1166,6 +1169,7 @@ static int Open( vlc_object_t * p_this )
     /* */
     LoadChapter( p_demux );
 
+    p_sys->i_attachments = MP4_GetAttachments( p_sys->p_root, &p_sys->pp_attachments );
     p_sys->asfpacketsys.p_demux = p_demux;
     p_sys->asfpacketsys.pi_preroll = &p_sys->i_preroll;
     p_sys->asfpacketsys.pi_preroll_start = &p_sys->i_preroll_start;
@@ -2043,9 +2047,15 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
             int *pi_int = va_arg( args, int * );
 
-            *pi_int = MP4_GetAttachments( p_sys->p_root, ppp_attach );
-            for( int i=0; i<*pi_int; i++ )
+            *ppp_attach = calloc( p_sys->i_attachments, sizeof(**ppp_attach ) );
+            if( !*ppp_attach )
+                return VLC_ENOMEM;
+            for ( size_t i = 0; i < p_sys->i_attachments; ++i )
+            {
+                (*ppp_attach)[i] = vlc_input_attachment_Hold( p_sys->pp_attachments[i] );
                 msg_Dbg( p_demux, "adding attachment %s", (*ppp_attach)[i]->psz_name );
+            }
+            *pi_int = p_sys->i_attachments;
 
             return VLC_SUCCESS;
         }
@@ -2168,6 +2178,10 @@ static void Close ( vlc_object_t * p_this )
         MP4_TrackClean( p_demux->out, &p_sys->track[i_track] );
     free( p_sys->track );
 
+    for ( size_t i = 0; i < p_sys->i_attachments; ++i )
+        vlc_input_attachment_Release( p_sys->pp_attachments[i] );
+    free( p_sys->pp_attachments );
+
     free( p_sys );
 }
 
-- 
2.28.0



More information about the vlc-devel mailing list