diff mbox

[FFmpeg-devel,21/23] avformat/matroskaenc: Only write tracks if there is a track

Message ID 20191106024922.19228-21-andreas.rheinhardt@gmail.com
State Superseded
Headers show

Commit Message

Andreas Rheinhardt Nov. 6, 2019, 2:49 a.m. UTC
The Matroska muxer does not write every stream as a Matroska track; some
streams are written as attachments. But should no stream be written as a
Matroska track, the Matroska muxer would nevertheless write a Tracks
element without a TrackEntry. This is against the spec. This commit
changes this and only writes a Tracks if there is a Matroska track.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/matroskaenc.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
diff mbox

Patch

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 1effe209b4..df13d05dab 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -134,7 +134,7 @@  typedef struct MatroskaMuxContext {
 
     AVPacket        cur_audio_pkt;
 
-    int have_attachments;
+    int nb_attachments;
     int have_video;
 
     int reserve_cues_space;
@@ -1112,7 +1112,6 @@  static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv,
     AVDictionaryEntry *tag;
 
     if (par->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
-        mkv->have_attachments = 1;
         return 0;
     }
 
@@ -1374,6 +1373,9 @@  static int mkv_write_tracks(AVFormatContext *s)
     AVIOContext *pb = s->pb;
     int i, ret, default_stream_exists = 0;
 
+    if (mkv->nb_attachments == s->nb_streams)
+        return 0;
+
     mkv_add_seekhead_entry(mkv, MATROSKA_ID_TRACKS, avio_tell(pb));
 
     ret = start_ebml_master_crc32(pb, &mkv->tracks_bc, mkv, MATROSKA_ID_TRACKS);
@@ -1631,7 +1633,7 @@  static int mkv_write_tags(AVFormatContext *s)
         }
     }
 
-    if (mkv->have_attachments && mkv->mode != MODE_WEBM) {
+    if (mkv->nb_attachments && mkv->mode != MODE_WEBM) {
         for (i = 0; i < s->nb_streams; i++) {
             mkv_track *track = &mkv->tracks[i];
             AVStream *st = s->streams[i];
@@ -1678,7 +1680,7 @@  static int mkv_write_attachments(AVFormatContext *s)
     AVIOContext *dyn_cp, *pb = s->pb;
     int i, ret;
 
-    if (!mkv->have_attachments)
+    if (!mkv->nb_attachments)
         return 0;
 
     mkv_add_seekhead_entry(mkv, MATROSKA_ID_ATTACHMENTS, avio_tell(pb));
@@ -2487,9 +2489,11 @@  static int mkv_write_trailer(AVFormatContext *s)
         avio_seek(pb, mkv->info_pos, SEEK_SET);
         end_ebml_master_crc32(pb, &mkv->info_bc, mkv);
 
-        // write tracks master
-        avio_seek(pb, mkv->tracks_pos, SEEK_SET);
-        end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv);
+        if (mkv->tracks_bc) {
+            // write tracks master
+            avio_seek(pb, mkv->tracks_pos, SEEK_SET);
+            end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv);
+        }
 
         // update stream durations
         if (mkv->tags_bc) {
@@ -2650,6 +2654,7 @@  static int mkv_init(struct AVFormatContext *s)
                                     "it cannot be deduced from the codec id.\n", i);
             return AVERROR(EINVAL);
             }
+            mkv->nb_attachments++;
             continue;
         }