[vlc-commits] [Git][videolan/vlc][master] 8 commits: demux: mkv: add a template to get mandatory elements

Steve Lhomme (@robUx4) gitlab at videolan.org
Tue Jul 16 14:00:40 UTC 2024



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
29ce258f by Steve Lhomme at 2024-07-16T13:35:05+00:00
demux: mkv: add a template to get mandatory elements

We get a reference to the object, or it throws an error that a mandatory element
is missing.

This should only be used for elements without a default value. Otherwise the
missing element would be added automatically to get the default value. But
since it's a const EbmlElement, we don't want to modify it.

- - - - -
6001b567 by Steve Lhomme at 2024-07-16T13:35:05+00:00
demux: mkv: use GetMandatoryChild to read the track type

If it's missing, we can't use the track. For now we keep using a dummy track.

- - - - -
bd43669f by Steve Lhomme at 2024-07-16T13:35:05+00:00
demux: mkv: use GetMandatoryChild to read attachment data

If any of the mandatory element is missing, we should not use it.

- - - - -
acd40d53 by Steve Lhomme at 2024-07-16T13:35:05+00:00
demux: mkv: fix infinite loop when creating an attachement fails

When it fails we need to skip to the next element in the list which is not done by the while.

- - - - -
284f4c2a by Steve Lhomme at 2024-07-16T13:35:05+00:00
demux: mkv: don't drop consecutive attachments if the current one is empty

- - - - -
5509178b by Steve Lhomme at 2024-07-16T13:35:05+00:00
demux: mkv: don't try to create elements if they may not exist

GetNextChild() should not be used to check for a nullptr value.

- - - - -
9c25393c by Steve Lhomme at 2024-07-16T13:35:05+00:00
demux: mkv: use the EbmlMaster container directly

And use MKV_CHECKED_PTR_DECL_CONST more.

- - - - -
e49b7192 by Steve Lhomme at 2024-07-16T13:35:05+00:00
demux: mkv: use standard C types

libebml no longer needs special types.

- - - - -


4 changed files:

- modules/demux/mkv/matroska_segment.cpp
- modules/demux/mkv/matroska_segment_parse.cpp
- modules/demux/mkv/mkv.cpp
- modules/demux/mkv/mkv.hpp


Changes:

=====================================
modules/demux/mkv/matroska_segment.cpp
=====================================
@@ -1072,28 +1072,24 @@ void matroska_segment_c::EnsureDuration()
     {
         // use the last block + duration
         uint64_t i_last_timecode = p_last_cluster->GlobalTimestamp();
-        for( unsigned int i = 0; i < p_last_cluster->ListSize(); i++ )
+        for (auto l : *p_last_cluster)
         {
-            EbmlElement *l = (*p_last_cluster)[i];
-
             if( MKV_CHECKED_PTR_DECL ( simpleblock, KaxSimpleBlock, l ) )
             {
                 simpleblock->SetParent( *p_last_cluster );
                 i_last_timecode = std::max(i_last_timecode, simpleblock->GlobalTimestamp());
             }
-            else if( MKV_CHECKED_PTR_DECL ( group, KaxBlockGroup, l ) )
+            else if( MKV_CHECKED_PTR_DECL_CONST ( group, KaxBlockGroup, l ) )
             {
                 uint64_t i_group_timecode = 0;
-                for( unsigned int j = 0; j < group->ListSize(); j++ )
+                for (auto g : *group)
                 {
-                    EbmlElement *g = (*group)[j];
-
                     if( MKV_CHECKED_PTR_DECL ( block, KaxBlock, g ) )
                     {
                         block->SetParent( *p_last_cluster );
                         i_group_timecode += block->GlobalTimestamp();
                     }
-                    else if( MKV_CHECKED_PTR_DECL ( kbd_ptr, KaxBlockDuration, g ) )
+                    else if( MKV_CHECKED_PTR_DECL_CONST ( kbd_ptr, KaxBlockDuration, g ) )
                     {
                         i_group_timecode += static_cast<uint64_t>( *kbd_ptr );
                     }


=====================================
modules/demux/mkv/matroska_segment_parse.cpp
=====================================
@@ -206,12 +206,14 @@ void matroska_segment_c::ParseTrackEntry( const KaxTrackEntry *m )
 {
     bool bSupported = true;
 
-    EbmlUInteger *pTrackType = static_cast<EbmlUInteger*>(m->FindElt(EBML_INFO(KaxTrackType)));
     uint8_t ttype;
-    if (likely(pTrackType != NULL))
-        ttype = (uint8_t) *pTrackType;
-    else
+    try {
+        KaxTrackType & pTrackType = GetMandatoryChild<KaxTrackType>(*m);
+        ttype = pTrackType;
+    } catch (const MissingMandatory & err) {
+        msg_Dbg( &sys.demuxer, "%s", err.what());
         ttype = 0;
+    }
 
     enum es_format_category_e es_cat;
     switch( ttype )
@@ -585,8 +587,8 @@ void matroska_segment_c::ParseTrackEntry( const KaxTrackEntry *m )
         E_CASE( KaxVideoAlphaMode, mode )
         {
             ONLY_FMT(VIDEO);
-            debug( vars, "Track Video Alpha Mode %u", static_cast<uint8>( mode ) ) ;
-            vars.tk->b_has_alpha = static_cast<uint8>( mode ) == 1;
+            debug( vars, "Track Video Alpha Mode %u", static_cast<uint8_t>( mode ) ) ;
+            vars.tk->b_has_alpha = static_cast<uint8_t>( mode ) == 1;
         }
 #endif
 #if LIBMATROSKA_VERSION >= 0x010406
@@ -1435,9 +1437,9 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
 
             chapter_codec_cmds_c *p_ccodec = NULL;
 
-            for( size_t j = 0; j < cp.ListSize(); j++ )
+            for (auto proc : cp)
             {
-                if( MKV_CHECKED_PTR_DECL( p_codec_id, KaxChapterProcessCodecID, cp[j] ) )
+                if( MKV_CHECKED_PTR_DECL_CONST( p_codec_id, KaxChapterProcessCodecID, proc ) )
                 {
                     if ( p_codec_id->GetValue() == MATROSKA_CHAPTER_CODEC_NATIVE )
                         p_ccodec = new matroska_script_codec_c( vlc_object_logger( &vars.obj->sys.demuxer ), vars.obj->sys );
@@ -1455,15 +1457,13 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
 
             if ( p_ccodec != NULL )
             {
-                for( size_t j = 0; j < cp.ListSize(); j++ )
+                for (auto k : cp)
                 {
-                    EbmlElement *k= cp[j];
-
-                    if( MKV_CHECKED_PTR_DECL( p_private, KaxChapterProcessPrivate, k ) )
+                    if( MKV_CHECKED_PTR_DECL_CONST( p_private, KaxChapterProcessPrivate, k ) )
                     {
                         p_ccodec->SetPrivate( *p_private );
                     }
-                    else if ( MKV_CHECKED_PTR_DECL( cmd, KaxChapterProcessCommand, k ) )
+                    else if ( MKV_CHECKED_PTR_DECL_CONST( cmd, KaxChapterProcessCommand, k ) )
                     {
                         p_ccodec->AddCommand( *cmd );
                     }
@@ -1509,35 +1509,41 @@ void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
 
     KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
 
-    while( attachedFile && ( attachedFile->GetSize() > 0 ) )
+    while( attachedFile )
     {
-        KaxFileData  &img_data     = GetChild<KaxFileData>( *attachedFile );
-        std::string attached_filename( UTFstring( GetChild<KaxFileName>( *attachedFile ) ).GetUTF8() );
-        auto new_attachment = vlc_input_attachment_New( attached_filename.c_str(),
-                                                        GetChild<KaxMimeType>( *attachedFile ).GetValue().c_str(),
-                                                        nullptr,
-                                                        img_data.GetBuffer(),
-                                                        img_data.GetSize() );
-        if (!new_attachment)
-            continue;
-        msg_Dbg( &sys.demuxer, "|   |   - %s (%s)", new_attachment->psz_name,
-                 new_attachment->psz_mime );
-
-        if( !strncmp( new_attachment->psz_mime, "image/", 6 ) )
-        {
-            char *psz_url;
-            if( asprintf( &psz_url, "attachment://%s",
-                          new_attachment->psz_name ) >= 0 )
+        if (attachedFile->GetSize() > 0 )
+        try {
+            KaxFileData  &img_data     = GetMandatoryChild<KaxFileData>( *attachedFile );
+            std::string attached_filename( UTFstring( GetMandatoryChild<KaxFileName>( *attachedFile ) ).GetUTF8() );
+            auto new_attachment = vlc_input_attachment_New( attached_filename.c_str(),
+                                                            GetMandatoryChild<KaxMimeType>( *attachedFile ).GetValue().c_str(),
+                                                            nullptr,
+                                                            img_data.GetBuffer(),
+                                                            img_data.GetSize() );
+            if (new_attachment)
             {
-                if( !sys.meta )
-                    sys.meta = vlc_meta_New();
-                vlc_meta_SetArtURL( sys.meta, psz_url );
-                free( psz_url );
+                msg_Dbg( &sys.demuxer, "|   |   - %s (%s)", new_attachment->psz_name,
+                        new_attachment->psz_mime );
+
+                if( !strncmp( new_attachment->psz_mime, "image/", 6 ) )
+                {
+                    char *psz_url;
+                    if( asprintf( &psz_url, "attachment://%s",
+                                new_attachment->psz_name ) >= 0 )
+                    {
+                        if( !sys.meta )
+                            sys.meta = vlc_meta_New();
+                        vlc_meta_SetArtURL( sys.meta, psz_url );
+                        free( psz_url );
+                    }
+                }
+                sys.stored_attachments.push_back( vlc::wrap_cptr( new_attachment,
+                                                                &vlc_input_attachment_Release ) );
             }
+        } catch (const MissingMandatory & err) {
+            msg_Dbg( &sys.demuxer, "%s", err.what());
         }
-        sys.stored_attachments.push_back( vlc::wrap_cptr( new_attachment,
-                                                          &vlc_input_attachment_Release ) );
-        attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
+        attachedFile = FindNextChild<KaxAttached>( *attachments, *attachedFile );
     }
 }
 
@@ -1655,9 +1661,9 @@ bool matroska_segment_c::ParseCluster( KaxCluster *cluster, bool b_update_start_
 
     bool b_has_timecode = false;
 
-    for( unsigned int i = 0; i < cluster->ListSize(); ++i )
+    for (auto c : *cluster)
     {
-        if( MKV_CHECKED_PTR_DECL( p_ctc, KaxClusterTimestamp, (*cluster)[i] ) )
+        if( MKV_CHECKED_PTR_DECL_CONST( p_ctc, KaxClusterTimestamp, c ) )
         {
             cluster->InitTimestamp( static_cast<uint64_t>( *p_ctc ), i_timescale );
             _seeker.add_cluster( cluster );


=====================================
modules/demux/mkv/mkv.cpp
=====================================
@@ -711,7 +711,7 @@ void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock
                 KaxBlockAddID *addId = FindChild<KaxBlockAddID>(*blockMore);
                 if(addId == nullptr)
                     break;
-                if (static_cast<uint64>(*addId) != 1)
+                if (static_cast<uint64_t>(*addId) != 1)
                     break;
 
                 KaxBlockAdditional *addition = FindChild<KaxBlockAdditional>(*blockMore);


=====================================
modules/demux/mkv/mkv.hpp
=====================================
@@ -124,6 +124,24 @@ enum chapter_codec_id
 #define MKV_CHECKED_PTR_DECL( name, type, src ) type * name = MKV_IS_ID(src, type) ? static_cast<type*>(src) : NULL
 #define MKV_CHECKED_PTR_DECL_CONST( name, type, src ) const type * name = MKV_IS_ID(src, type) ? static_cast<const type*>(src) : NULL
 
+class MissingMandatory : public std::runtime_error
+{
+public:
+    MissingMandatory(const char * type_name)
+        :std::runtime_error(std::string("missing mandatory element without a default ") + type_name)
+    {}
+};
+
+template <typename Type>
+Type & GetMandatoryChild(const EbmlMaster & Master)
+{
+  auto p = static_cast<Type *>(Master.FindFirstElt(EBML_INFO(Type)));
+  if (p == nullptr)
+  {
+    throw MissingMandatory(EBML_INFO_NAME(EBML_INFO(Type)));
+  }
+  return *p;
+}
 #if LIBEBML_VERSION < 0x020000
 template <typename Type>
 Type * FindChild(const EbmlMaster & Master)



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/f9f022198b4dfeb48302fbadffdc309ac0d40004...e49b7192a01931c8cca64c0e5568e9eaf2c0e7e3

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/f9f022198b4dfeb48302fbadffdc309ac0d40004...e49b7192a01931c8cca64c0e5568e9eaf2c0e7e3
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list