summaryrefslogtreecommitdiff
path: root/libavformat
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2019-11-03 13:25:08 +0100
committerAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2020-05-03 14:14:06 +0200
commit449eaeb7a72a9fca437923660bfcdb6af844b353 (patch)
tree64da036d0fbb63c50ffd3901f1a2cd96772d2441 /libavformat
parentcb255b616cf1ebc6bc89b3538b6b7465dc2c526b (diff)
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@gmail.com>
Diffstat (limited to 'libavformat')
-rw-r--r--libavformat/matroskaenc.c39
1 files 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;