diff mbox series

[FFmpeg-devel,15/20] avformat/matroskaenc: Only write Tracks if there is a track

Message ID 20200405155928.9323-16-andreas.rheinhardt@gmail.com
State Accepted
Commit a9844341f78e6eba78a9bb503e71a5d9e3ceea81
Headers show
Series Matroska muxer patches | expand

Checks

Context Check Description
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Andreas Rheinhardt April 5, 2020, 3:59 p.m. UTC
The Matroska muxer does not write every stream as a Matroska track;
some streams are written as AttachedFile. 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 | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 1b9ea45bc0..45a2341c22 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -133,7 +133,7 @@  typedef struct MatroskaMuxContext {
 
     AVPacket        cur_audio_pkt;
 
-    int have_attachments;
+    unsigned nb_attachments;
     int have_video;
 
     int reserve_cues_space;
@@ -1099,7 +1099,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;
     }
 
@@ -1356,6 +1355,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(&mkv->tracks_bc, mkv);
@@ -1610,7 +1612,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];
@@ -1644,7 +1646,7 @@  static int mkv_write_attachments(AVFormatContext *s)
     AVIOContext *dyn_cp = NULL, *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));
@@ -2495,9 +2497,12 @@  static int mkv_write_trailer(AVFormatContext *s)
         put_ebml_float(mkv->info_bc, MATROSKA_ID_DURATION, mkv->duration);
         end_ebml_master_crc32(pb, &mkv->info_bc, mkv, MATROSKA_ID_INFO, 0, 0);
 
-        // write tracks master
-        avio_seek(pb, mkv->tracks_pos, SEEK_SET);
-        end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv, MATROSKA_ID_TRACKS, 0, 0);
+        if (mkv->tracks_bc) {
+            // write Tracks master
+            avio_seek(pb, mkv->tracks_pos, SEEK_SET);
+            end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv,
+                                  MATROSKA_ID_TRACKS, 0, 0);
+        }
 
         // update stream durations
         if (mkv->tags_bc) {
@@ -2645,6 +2650,7 @@  static int mkv_init(struct AVFormatContext *s)
                 av_log(s, AV_LOG_WARNING, "Stream %d will be ignored "
                        "as WebM doesn't support attachments.\n", i);
             }
+            mkv->nb_attachments++;
             continue;
         }