[FFmpeg-cvslog] avformat/matroskaenc: Check mimetypes earlier

Andreas Rheinhardt git at videolan.org
Sun May 3 15:23:10 EEST 2020


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Sun Nov  3 13:25:08 2019 +0100| [449eaeb7a72a9fca437923660bfcdb6af844b353] | committer: Andreas Rheinhardt

avformat/matroskaenc: Check mimetypes earlier

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

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=449eaeb7a72a9fca437923660bfcdb6af844b353
---

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

diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 36b29e2d8d..7cf7724f89 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;



More information about the ffmpeg-cvslog mailing list