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>
This commit is contained in:
Andreas Rheinhardt 2019-11-03 13:25:08 +01:00
parent cb255b616c
commit 449eaeb7a7

View File

@ -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;