[FFmpeg-devel] [PATCH 7/7] avformat/matroskaenc: Check mimetypes earlier

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Tue Apr 28 11:36:45 EEST 2020


This avoids errors lateron after the file header has already been
partially written.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
Also avoids a memleak in case of missing mimetype metadata.

 libavformat/matroskaenc.c | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 09f08c1e9f..a1b613290c 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -1687,6 +1687,23 @@ static int mkv_write_tags(AVFormatContext *s)
     return 0;
 }
 
+static const char *get_mimetype(const AVStream *st)
+{
+    const AVDictionaryEntry *t;
+
+    if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
+        return t->value;
+    if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
+        const AVCodecDescriptor *desc = avcodec_descriptor_get(st->codecpar->codec_id);
+        if (desc && desc->mime_types) {
+            return desc->mime_types[0];
+        } else if (st->codecpar->codec_id == AV_CODEC_ID_TEXT)
+            return "text/plain";
+    }
+
+    return NULL;
+}
+
 static int mkv_write_attachments(AVFormatContext *s)
 {
     MatroskaMuxContext *mkv = s->priv_data;
@@ -1707,7 +1724,7 @@ static int mkv_write_attachments(AVFormatContext *s)
         mkv_track *track = &mkv->tracks[i];
         ebml_master attached_file;
         const AVDictionaryEntry *t;
-        const char *mimetype = NULL;
+        const char *mimetype;
 
         if (st->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT)
             continue;
@@ -1721,21 +1738,9 @@ static int mkv_write_attachments(AVFormatContext *s)
             return AVERROR(EINVAL);
         }
         put_ebml_string(dyn_cp, MATROSKA_ID_FILENAME, t->value);
-        if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
-            mimetype = t->value;
-        else if (st->codecpar->codec_id != AV_CODEC_ID_NONE ) {
-            const AVCodecDescriptor *desc = avcodec_descriptor_get(st->codecpar->codec_id);
-            if (desc && desc->mime_types) {
-                mimetype = desc->mime_types[0];
-            } else if (st->codecpar->codec_id == AV_CODEC_ID_TEXT)
-                mimetype = "text/plain";
-        }
-        if (!mimetype) {
-            av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
-                                    "it cannot be deduced from the codec id.\n", i);
-            return AVERROR(EINVAL);
-        }
 
+        mimetype = get_mimetype(st);
+        av_assert0(mimetype);
         put_ebml_string(dyn_cp, MATROSKA_ID_FILEMIMETYPE, mimetype);
         put_ebml_binary(dyn_cp, MATROSKA_ID_FILEDATA, st->codecpar->extradata, st->codecpar->extradata_size);
         put_ebml_uid(dyn_cp, MATROSKA_ID_FILEUID, track->uid);
@@ -2672,6 +2677,10 @@ static int mkv_init(struct AVFormatContext *s)
             if (mkv->mode == MODE_WEBM) {
                 av_log(s, AV_LOG_WARNING, "Stream %d will be ignored "
                        "as WebM doesn't support attachments.\n", i);
+            } else if (!get_mimetype(st)) {
+                av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype "
+                       "tag and it cannot be deduced from the codec id.\n", i);
+                return AVERROR(EINVAL);
             }
             mkv->nb_attachments++;
             continue;
-- 
2.20.1



More information about the ffmpeg-devel mailing list