[PATCH] Matroska demuxer adds WebVTT support
WebM files now support inband text tracks, as described in the following specification: http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm The Matroska demuxer now detects the presence of WebVTT tracks, synthesizing WebVTT packets (having codec id AV_CODEC_ID_WEBVTT) and pushing them downstream in the normal way. --- libavformat/matroska.c | 5 ++ libavformat/matroskadec.c | 130 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/libavformat/matroska.c b/libavformat/matroska.c index ee57c18..953c572 100644 --- a/libavformat/matroska.c +++ b/libavformat/matroska.c @@ -57,6 +57,11 @@ const CodecTags ff_mkv_codec_tags[]={ {"A_VORBIS" , AV_CODEC_ID_VORBIS}, {"A_WAVPACK4" , AV_CODEC_ID_WAVPACK}, + {"D_WEBVTT/SUBTITLES" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/CAPTIONS" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/DESCRIPTIONS", AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/METADATA" , AV_CODEC_ID_WEBVTT}, + {"S_TEXT/UTF8" , AV_CODEC_ID_SUBRIP}, {"S_TEXT/UTF8" , AV_CODEC_ID_TEXT}, {"S_TEXT/UTF8" , AV_CODEC_ID_SRT}, diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index a1aa0eb..d08194d 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -1566,7 +1566,8 @@ static int matroska_read_header(AVFormatContext *s) /* Apply some sanity checks. */ if (track->type != MATROSKA_TRACK_TYPE_VIDEO && track->type != MATROSKA_TRACK_TYPE_AUDIO && - track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { + track->type != MATROSKA_TRACK_TYPE_SUBTITLE && + track->type != MATROSKA_TRACK_TYPE_METADATA) { av_log(matroska->ctx, AV_LOG_INFO, "Unknown or unsupported track type %"PRIu64"\n", track->type); @@ -1860,6 +1861,16 @@ static int matroska_read_header(AVFormatContext *s) st->codec->bits_per_coded_sample = track->audio.bitdepth; if (st->codec->codec_id != AV_CODEC_ID_AAC) st->need_parsing = AVSTREAM_PARSE_HEADERS; + } else if (codec_id == AV_CODEC_ID_WEBVTT) { + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; + + if (!strcmp(track->codec_id, "D_WEBVTT/CAPTIONS")) { + st->disposition |= AV_DISPOSITION_CAPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/DESCRIPTIONS")) { + st->disposition |= AV_DISPOSITION_DESCRIPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/METADATA")) { + st->disposition |= AV_DISPOSITION_METADATA; + } } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; #if FF_API_ASS_SSA @@ -2226,6 +2237,115 @@ fail: return ret; } +static int matroska_parse_webvtt(MatroskaDemuxContext *matroska, + MatroskaTrack *track, + AVStream *st, + uint8_t *data, int data_len, + uint64_t timecode, + uint64_t duration, + int64_t pos) +{ + AVPacket *pkt; + uint8_t *id, *settings, *text, *buf; + int id_len, settings_len, text_len; + + uint8_t *p = data; + uint8_t *q = data + data_len; + + if (p >= q) + return AVERROR_INVALIDDATA; + + id = p; + id_len = strcspn(p, "\r\n"); + + p += id_len; + if (p >= q) + return AVERROR_INVALIDDATA; + + if (*p == '\r') + p++; + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + if (p >= q) + return AVERROR_INVALIDDATA; + + settings = p; + settings_len = strcspn(p, "\r\n"); + + p += settings_len; + if (p >= q) + return AVERROR_INVALIDDATA; + + if (*p == '\r') + p++; + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + if (p >= q) + return AVERROR_INVALIDDATA; + + text = p; + text_len = q - p; + + while (text_len > 0) { + const int len = text_len - 1; + const uint8_t c = p[len]; + if (c != '\r' && c != '\n') + break; + text_len = len; + } + + pkt = av_mallocz(sizeof(AVPacket)); + if (av_new_packet(pkt, text_len) < 0) { + av_free(pkt); + return AVERROR(ENOMEM); + } + + memcpy(pkt->data, text, text_len); + + if (id_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_IDENTIFIER, + id_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, id, id_len); + } + + if (settings_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_SETTINGS, + settings_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, settings, settings_len); + } + + // Do we need this for subtitles? + // pkt->flags = AV_PKT_FLAG_KEY; + + pkt->stream_index = st->index; + pkt->pts = timecode; + + // Do we need this for subtitles? + // pkt->dts = timecode; + + pkt->duration = duration; + pkt->pos = pos; + + dynarray_add(&matroska->packets, &matroska->num_packets, pkt); + matroska->prev_pkt = pkt; + + return 0; +} + static int matroska_parse_frame(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, @@ -2445,6 +2565,14 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, if (res) goto end; + } else if (st->codec->codec_id == AV_CODEC_ID_WEBVTT) { + res = matroska_parse_webvtt(matroska, track, st, + data, lace_size[n], + timecode, lace_duration, + pos); + if (res) + goto end; + } else { res = matroska_parse_frame(matroska, track, st, data, lace_size[n], timecode, lace_duration, -- 1.8.3
WebM files now support inband text tracks, as described in the following specification: http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm The Matroska demuxer now detects the presence of WebVTT tracks, synthesizing WebVTT packets (having codec id AV_CODEC_ID_WEBVTT) and pushing them downstream in the normal way. --- libavformat/matroska.c | 5 ++ libavformat/matroskadec.c | 130 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/libavformat/matroska.c b/libavformat/matroska.c index ee57c18..953c572 100644 --- a/libavformat/matroska.c +++ b/libavformat/matroska.c @@ -57,6 +57,11 @@ const CodecTags ff_mkv_codec_tags[]={ {"A_VORBIS" , AV_CODEC_ID_VORBIS}, {"A_WAVPACK4" , AV_CODEC_ID_WAVPACK}, + {"D_WEBVTT/SUBTITLES" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/CAPTIONS" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/DESCRIPTIONS", AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/METADATA" , AV_CODEC_ID_WEBVTT}, + {"S_TEXT/UTF8" , AV_CODEC_ID_SUBRIP}, {"S_TEXT/UTF8" , AV_CODEC_ID_TEXT}, {"S_TEXT/UTF8" , AV_CODEC_ID_SRT}, diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index a1aa0eb..d08194d 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -1566,7 +1566,8 @@ static int matroska_read_header(AVFormatContext *s) /* Apply some sanity checks. */ if (track->type != MATROSKA_TRACK_TYPE_VIDEO && track->type != MATROSKA_TRACK_TYPE_AUDIO && - track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { + track->type != MATROSKA_TRACK_TYPE_SUBTITLE && + track->type != MATROSKA_TRACK_TYPE_METADATA) { av_log(matroska->ctx, AV_LOG_INFO, "Unknown or unsupported track type %"PRIu64"\n", track->type); @@ -1860,6 +1861,16 @@ static int matroska_read_header(AVFormatContext *s) st->codec->bits_per_coded_sample = track->audio.bitdepth; if (st->codec->codec_id != AV_CODEC_ID_AAC) st->need_parsing = AVSTREAM_PARSE_HEADERS; + } else if (codec_id == AV_CODEC_ID_WEBVTT) { + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; + + if (!strcmp(track->codec_id, "D_WEBVTT/CAPTIONS")) { + st->disposition |= AV_DISPOSITION_CAPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/DESCRIPTIONS")) { + st->disposition |= AV_DISPOSITION_DESCRIPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/METADATA")) { + st->disposition |= AV_DISPOSITION_METADATA; + } } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; #if FF_API_ASS_SSA @@ -2226,6 +2237,115 @@ fail: return ret; } +static int matroska_parse_webvtt(MatroskaDemuxContext *matroska, + MatroskaTrack *track, + AVStream *st, + uint8_t *data, int data_len, + uint64_t timecode, + uint64_t duration, + int64_t pos) +{ + AVPacket *pkt; + uint8_t *id, *settings, *text, *buf; + int id_len, settings_len, text_len; + + uint8_t *p = data; + uint8_t *q = data + data_len; + + if (p >= q) + return AVERROR_INVALIDDATA; + + id = p; + id_len = strcspn(p, "\r\n"); + + p += id_len; + if (p >= q) + return AVERROR_INVALIDDATA; + + if (*p == '\r') + p++; + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + if (p >= q) + return AVERROR_INVALIDDATA; + + settings = p; + settings_len = strcspn(p, "\r\n"); + + p += settings_len; + if (p >= q) + return AVERROR_INVALIDDATA; + + if (*p == '\r') + p++; + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + if (p >= q) + return AVERROR_INVALIDDATA; + + text = p; + text_len = q - p; + + while (text_len > 0) { + const int len = text_len - 1; + const uint8_t c = p[len]; + if (c != '\r' && c != '\n') + break; + text_len = len; + } + + pkt = av_mallocz(sizeof(AVPacket)); + if (av_new_packet(pkt, text_len) < 0) { + av_free(pkt); + return AVERROR(ENOMEM); + } + + memcpy(pkt->data, text, text_len); + + if (id_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_IDENTIFIER, + id_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, id, id_len); + } + + if (settings_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_SETTINGS, + settings_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, settings, settings_len); + } + + // Do we need this for subtitles? + // pkt->flags = AV_PKT_FLAG_KEY; + + pkt->stream_index = st->index; + pkt->pts = timecode; + + // Do we need this for subtitles? + // pkt->dts = timecode; + + pkt->duration = duration; + pkt->pos = pos; + + dynarray_add(&matroska->packets, &matroska->num_packets, pkt); + matroska->prev_pkt = pkt; + + return 0; +} + static int matroska_parse_frame(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, @@ -2445,6 +2565,14 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, if (res) goto end; + } else if (st->codec->codec_id == AV_CODEC_ID_WEBVTT) { + res = matroska_parse_webvtt(matroska, track, st, + data, lace_size[n], + timecode, lace_duration, + pos); + if (res) + goto end; + } else { res = matroska_parse_frame(matroska, track, st, data, lace_size[n], timecode, lace_duration, -- 1.8.3
rebase + ping
WebM files now support inband text tracks, as described in the following specification: http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm The Matroska demuxer now detects the presence of WebVTT tracks, synthesizing WebVTT packets (having codec id AV_CODEC_ID_WEBVTT) and pushing them downstream in the normal way. --- libavformat/matroska.c | 5 ++ libavformat/matroskadec.c | 130 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/libavformat/matroska.c b/libavformat/matroska.c index ee57c18..953c572 100644 --- a/libavformat/matroska.c +++ b/libavformat/matroska.c @@ -57,6 +57,11 @@ const CodecTags ff_mkv_codec_tags[]={ {"A_VORBIS" , AV_CODEC_ID_VORBIS}, {"A_WAVPACK4" , AV_CODEC_ID_WAVPACK}, + {"D_WEBVTT/SUBTITLES" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/CAPTIONS" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/DESCRIPTIONS", AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/METADATA" , AV_CODEC_ID_WEBVTT}, + {"S_TEXT/UTF8" , AV_CODEC_ID_SUBRIP}, {"S_TEXT/UTF8" , AV_CODEC_ID_TEXT}, {"S_TEXT/UTF8" , AV_CODEC_ID_SRT}, diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 9a15a2b..3f8eabc 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -1566,7 +1566,8 @@ static int matroska_read_header(AVFormatContext *s) /* Apply some sanity checks. */ if (track->type != MATROSKA_TRACK_TYPE_VIDEO && track->type != MATROSKA_TRACK_TYPE_AUDIO && - track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { + track->type != MATROSKA_TRACK_TYPE_SUBTITLE && + track->type != MATROSKA_TRACK_TYPE_METADATA) { av_log(matroska->ctx, AV_LOG_INFO, "Unknown or unsupported track type %"PRIu64"\n", track->type); @@ -1860,6 +1861,16 @@ static int matroska_read_header(AVFormatContext *s) st->codec->bits_per_coded_sample = track->audio.bitdepth; if (st->codec->codec_id != AV_CODEC_ID_AAC) st->need_parsing = AVSTREAM_PARSE_HEADERS; + } else if (codec_id == AV_CODEC_ID_WEBVTT) { + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; + + if (!strcmp(track->codec_id, "D_WEBVTT/CAPTIONS")) { + st->disposition |= AV_DISPOSITION_CAPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/DESCRIPTIONS")) { + st->disposition |= AV_DISPOSITION_DESCRIPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/METADATA")) { + st->disposition |= AV_DISPOSITION_METADATA; + } } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; #if FF_API_ASS_SSA @@ -2226,6 +2237,115 @@ fail: return ret; } +static int matroska_parse_webvtt(MatroskaDemuxContext *matroska, + MatroskaTrack *track, + AVStream *st, + uint8_t *data, int data_len, + uint64_t timecode, + uint64_t duration, + int64_t pos) +{ + AVPacket *pkt; + uint8_t *id, *settings, *text, *buf; + int id_len, settings_len, text_len; + + uint8_t *p = data; + uint8_t *q = data + data_len; + + if (p >= q) + return AVERROR_INVALIDDATA; + + id = p; + id_len = strcspn(p, "\r\n"); + + p += id_len; + if (p >= q) + return AVERROR_INVALIDDATA; + + if (*p == '\r') + p++; + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + if (p >= q) + return AVERROR_INVALIDDATA; + + settings = p; + settings_len = strcspn(p, "\r\n"); + + p += settings_len; + if (p >= q) + return AVERROR_INVALIDDATA; + + if (*p == '\r') + p++; + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + if (p >= q) + return AVERROR_INVALIDDATA; + + text = p; + text_len = q - p; + + while (text_len > 0) { + const int len = text_len - 1; + const uint8_t c = p[len]; + if (c != '\r' && c != '\n') + break; + text_len = len; + } + + pkt = av_mallocz(sizeof(AVPacket)); + if (av_new_packet(pkt, text_len) < 0) { + av_free(pkt); + return AVERROR(ENOMEM); + } + + memcpy(pkt->data, text, text_len); + + if (id_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_IDENTIFIER, + id_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, id, id_len); + } + + if (settings_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_SETTINGS, + settings_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, settings, settings_len); + } + + // Do we need this for subtitles? + // pkt->flags = AV_PKT_FLAG_KEY; + + pkt->stream_index = st->index; + pkt->pts = timecode; + + // Do we need this for subtitles? + // pkt->dts = timecode; + + pkt->duration = duration; + pkt->pos = pos; + + dynarray_add(&matroska->packets, &matroska->num_packets, pkt); + matroska->prev_pkt = pkt; + + return 0; +} + static int matroska_parse_frame(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, @@ -2454,6 +2574,14 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, if (res) goto end; + } else if (st->codec->codec_id == AV_CODEC_ID_WEBVTT) { + res = matroska_parse_webvtt(matroska, track, st, + data, lace_size[n], + timecode, lace_duration, + pos); + if (res) + goto end; + } else { res = matroska_parse_frame(matroska, track, st, data, lace_size[n], timecode, lace_duration, -- 1.8.3
rebase + ping
On Mon, Jul 15, 2013 at 02:08:24PM -0700, Matthew Heaney wrote: [...]
@@ -2226,6 +2237,115 @@ fail: return ret; }
+static int matroska_parse_webvtt(MatroskaDemuxContext *matroska, + MatroskaTrack *track, + AVStream *st, + uint8_t *data, int data_len, + uint64_t timecode, + uint64_t duration, + int64_t pos) +{ + AVPacket *pkt; + uint8_t *id, *settings, *text, *buf; + int id_len, settings_len, text_len; + + uint8_t *p = data; + uint8_t *q = data + data_len; + + if (p >= q) + return AVERROR_INVALIDDATA;
if data_len can be negative then it should be checked directly checking after adding it to a pointer could fail due to overflow
+ + id = p; + id_len = strcspn(p, "\r\n");
is it guranteed that the array is zero terminated ? [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I do not agree with what you have to say, but I'll defend to the death your right to say it. -- Voltaire
WebM files now support inband text tracks, as described in the following specification: http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm The Matroska demuxer now detects the presence of WebVTT tracks, synthesizing WebVTT packets (having codec id AV_CODEC_ID_WEBVTT) and pushing them downstream in the normal way. --- libavformat/matroska.c | 5 ++ libavformat/matroskadec.c | 150 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 154 insertions(+), 1 deletion(-) diff --git a/libavformat/matroska.c b/libavformat/matroska.c index ee57c18..953c572 100644 --- a/libavformat/matroska.c +++ b/libavformat/matroska.c @@ -57,6 +57,11 @@ const CodecTags ff_mkv_codec_tags[]={ {"A_VORBIS" , AV_CODEC_ID_VORBIS}, {"A_WAVPACK4" , AV_CODEC_ID_WAVPACK}, + {"D_WEBVTT/SUBTITLES" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/CAPTIONS" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/DESCRIPTIONS", AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/METADATA" , AV_CODEC_ID_WEBVTT}, + {"S_TEXT/UTF8" , AV_CODEC_ID_SUBRIP}, {"S_TEXT/UTF8" , AV_CODEC_ID_TEXT}, {"S_TEXT/UTF8" , AV_CODEC_ID_SRT}, diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 9a15a2b..7466258 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -1566,7 +1566,8 @@ static int matroska_read_header(AVFormatContext *s) /* Apply some sanity checks. */ if (track->type != MATROSKA_TRACK_TYPE_VIDEO && track->type != MATROSKA_TRACK_TYPE_AUDIO && - track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { + track->type != MATROSKA_TRACK_TYPE_SUBTITLE && + track->type != MATROSKA_TRACK_TYPE_METADATA) { av_log(matroska->ctx, AV_LOG_INFO, "Unknown or unsupported track type %"PRIu64"\n", track->type); @@ -1860,6 +1861,16 @@ static int matroska_read_header(AVFormatContext *s) st->codec->bits_per_coded_sample = track->audio.bitdepth; if (st->codec->codec_id != AV_CODEC_ID_AAC) st->need_parsing = AVSTREAM_PARSE_HEADERS; + } else if (codec_id == AV_CODEC_ID_WEBVTT) { + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; + + if (!strcmp(track->codec_id, "D_WEBVTT/CAPTIONS")) { + st->disposition |= AV_DISPOSITION_CAPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/DESCRIPTIONS")) { + st->disposition |= AV_DISPOSITION_DESCRIPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/METADATA")) { + st->disposition |= AV_DISPOSITION_METADATA; + } } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; #if FF_API_ASS_SSA @@ -2226,6 +2237,135 @@ fail: return ret; } +static int matroska_parse_webvtt(MatroskaDemuxContext *matroska, + MatroskaTrack *track, + AVStream *st, + uint8_t *data, int data_len, + uint64_t timecode, + uint64_t duration, + int64_t pos) +{ + AVPacket *pkt; + uint8_t *id, *settings, *text, *buf; + int id_len, settings_len, text_len; + uint8_t *p, *q; + + if (data_len <= 0) + return AVERROR_INVALIDDATA; + + p = data; + q = data + data_len; + + id = p; + id_len = -1; + while (p != q && id_len < 0) { + switch (*p) { + case '\r': + case '\n': + id_len = p - id; + break; + default: + p++; + break; + } + } + + if (p >= q) + return AVERROR_INVALIDDATA; + + if (*p == '\r') + p++; + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + if (p >= q) + return AVERROR_INVALIDDATA; + + settings = p; + settings_len = -1; + while (p != q && settings_len < 0) { + switch (*p) { + case '\r': + case '\n': + settings_len = p - settings; + break; + default: + p++; + break; + } + } + + if (p >= q) + return AVERROR_INVALIDDATA; + + if (*p == '\r') + p++; + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + if (p >= q) + return AVERROR_INVALIDDATA; + + text = p; + text_len = q - p; + while (text_len > 0) { + const int len = text_len - 1; + const uint8_t c = p[len]; + if (c != '\r' && c != '\n') + break; + text_len = len; + } + + pkt = av_mallocz(sizeof(AVPacket)); + if (av_new_packet(pkt, text_len) < 0) { + av_free(pkt); + return AVERROR(ENOMEM); + } + + memcpy(pkt->data, text, text_len); + + if (id_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_IDENTIFIER, + id_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, id, id_len); + } + + if (settings_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_SETTINGS, + settings_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, settings, settings_len); + } + + // Do we need this for subtitles? + // pkt->flags = AV_PKT_FLAG_KEY; + + pkt->stream_index = st->index; + pkt->pts = timecode; + + // Do we need this for subtitles? + // pkt->dts = timecode; + + pkt->duration = duration; + pkt->pos = pos; + + dynarray_add(&matroska->packets, &matroska->num_packets, pkt); + matroska->prev_pkt = pkt; + + return 0; +} + static int matroska_parse_frame(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, @@ -2454,6 +2594,14 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, if (res) goto end; + } else if (st->codec->codec_id == AV_CODEC_ID_WEBVTT) { + res = matroska_parse_webvtt(matroska, track, st, + data, lace_size[n], + timecode, lace_duration, + pos); + if (res) + goto end; + } else { res = matroska_parse_frame(matroska, track, st, data, lace_size[n], timecode, lace_duration, -- 1.8.3
On Mon, 15 Jul 2013 17:02:55 -0700 Matthew Heaney <matthewjheaney@google.com> wrote:
+static int matroska_parse_webvtt(MatroskaDemuxContext *matroska, + MatroskaTrack *track, + AVStream *st, + uint8_t *data, int data_len, + uint64_t timecode, + uint64_t duration, + int64_t pos) +{ [...]
Can I ask why there is subtitle parsing in the Matroska demuxer? I find this a bit odd. Why split them into side-data, instead of just leaving the packet as-is? Not even ASS subtitles, a rather complicated format, require special treatment in the demuxer. (Not anymore; it used to be the case that ASS packets were converted into special ffmpeg-specific packets, but that was recently fixed AFAIK.)
L'octidi 28 messidor, an CCXXI, wm4 a écrit :
Can I ask why there is subtitle parsing in the Matroska demuxer?
That is usually a sign that the people that work on WebM did chose a different packet format that the format used in ffmpeg. It can happen because WebVTT packets were only extracted from text files until now, and the person implementing it (Clément) had to decide what part of the text file goes to the packet data and what part is parsed to fill-in the packet structure. IMHO, since a specification for WebVTT packets inside a generic container exists now, we should change the packet format used by lavf instead of adding glue in the generic demuxer. (Also, the commit message needs to be fixed: "matroskaenc: add support for WebVTT".) Regards, -- Nicolas George
On Tue, Jul 16, 2013 at 2:35 AM, Nicolas George < nicolas.george@normalesup.org> wrote:
That is usually a sign that the people that work on WebM did chose a different packet format that the format used in ffmpeg.
All we needed was a way to losslessly embed WebVTT cues in a Matroska frame. For a demuxer, the parsing duties aren't very complicated -- you just need to demarcate the id and settings lines from the cue text.
L'octidi 28 messidor, an CCXXI, Matthew Heaney a écrit :
All we needed was a way to losslessly embed WebVTT cues in a Matroska frame. For a demuxer, the parsing duties aren't very complicated -- you just need to demarcate the id and settings lines from the cue text.
Complicated or not really the issue: if there are n demuxers and p codecs, we do not want n×p special cases. Therefore, the correct solution here would be to change the WebVTT demuxer to output packets in the same format as Matroska rather than changing the Matroska demuxer. The reason is that Matroska is generic while WebVTT is specialized. Exactly the same change that happened to ASS recently. By the way, do you know any other container format that supports WebVTT? Regards, -- Nicolas George
On Thu, 18 Jul 2013 13:02:42 +0200 Nicolas George <nicolas.george@normalesup.org> wrote:
By the way, do you know any other container format that supports WebVTT?
The WebVTT muxer/demuxer, and it uses side data too. The WebVTT->ASS "decoder" is involved as well.
Le decadi 30 messidor, an CCXXI, wm4 a écrit :
The WebVTT muxer/demuxer, and it uses side data too.
That is not a real container format. Regards, -- Nicolas George
On Thu, Jul 18, 2013 at 4:02 AM, Nicolas George < nicolas.george@normalesup.org> wrote:
Therefore, the correct solution here would be to change the WebVTT demuxer to output packets in the same format as Matroska rather than changing the Matroska demuxer. The reason is that Matroska is generic while WebVTT is specialized.
Just so I'm understanding, I think the change you asking me to make involves some or all of the following steps: (1) change the existing webvtt demuxer to output packets that match their embedded representation in WebM. (2) change this Matroska demuxer patch to push webvtt frames downstream, as is, in their embedded form. (3) add a WebVTT codec that understands webvtt frames as embedded in a WebM file, and converts them to some canonical text track format (4) change the existing webm muxer to accept a new packet format (5) change the existing webvtt muxer to accept a new packet format Is my understanding correct? I'm on vacation for a couple of weeks, so this work will have to wait until I return.
By the way, do you know any other container format that supports WebVTT?
No, Just *.vtt and *.webm.
Le decadi 30 messidor, an CCXXI, Matthew Heaney a écrit :
Just so I'm understanding, I think the change you asking me to make involves some or all of the following steps:
(1) change the existing webvtt demuxer to output packets that match their embedded representation in WebM.
Yes.
(2) change this Matroska demuxer patch to push webvtt frames downstream, as is, in their embedded form.
Yes. Unless I am mistaken, it would make the patch trivial. (That is a general principle IMHO: adding support for codec X in format Y should only require adding the map between the codec id for X and whatever identifier used in Y; see commit 66aabd7 for example. If it requires more, then there is something wrong somewhere, but not necessarily under ffmpeg's control.)
(3) add a WebVTT codec that understands webvtt frames as embedded in a WebM file, and converts them to some canonical text track format
I suspect WebVTT is recent and confidential enough, and we can dispense with the "add new / deprecate old" routine and directly change the packet format.
(4) change the existing webm muxer to accept a new packet format
Yes. I am very sorry I missed the discussion at this time, or I would have said the same thing, and it may have avoided redundant work.
(5) change the existing webvtt muxer to accept a new packet format
Yes. Of course, other can disagree with me, this is open to discussion.
I'm on vacation for a couple of weeks, so this work will have to wait until I return.
No problem.
No, Just *.vtt and *.webm.
That is very good news. I just hope that if someone adds support for WebVTT in another generic format, they will have the intelligence of using the same format as Matroska. Otherwise, one of the demuxers will need a special case; that is what I had in mind when I spoke of "not necessarily under ffmpeg's control" above.
No account log-in should be necessary.
They asked me for it when I tried to download the file. Google's requirement of an account or not is a mystery to me.
I'm on vacation, so I don't have access to my work computer, but you can simply extract the text track from the file, as ffmpeg has a webvtt muxer now.
I would rather have a pair of corresponding authoritative files instead of producing one myself, but I will try to do without. Thanks. Regards, -- Nicolas George
On Fri, Jul 19, 2013 at 6:33 AM, Nicolas George < nicolas.george@normalesup.org> wrote:
That is very good news. I just hope that if someone adds support for WebVTT in another generic format, they will have the intelligence of using the same format as Matroska. Otherwise, one of the demuxers will need a special case; that is what I had in mind when I spoke of "not necessarily under ffmpeg's control" above.
The representation of WebVTT cues in WebM is strictly an artifact of Matroska. There's no expectation that that representation would make sense for some other container. Frank and I (along with various browser vendors) designed the representation that way so that a Matroska demuxer could losslessly reconstruct the original WebVTT cue from its embedded representation. It was never intended as a general-purpose embedded representation of WebVTT cues. As it stands now, the Matroska demuxer reconstructs the WebVTT cue such that it exactly matches what the WebVTT demuxer already pushes downstream. To me this makes perfect sense (hence the patch), since the format of the WebVTT cue itself is canonical. I can change the WebVTT demuxer to convert the original WebVTT cues into a format that matches the embedded representation in a WebM file, but one issue is that this will pollute the rest of ffmpeg with artifacts of WebM. Are you sure this is what you want? If you use WebVTT cues as the canonical representation (as the WebVTT demuxer does now, and the current patch does for the WebM demuxer), this has the benefit that only the WebM demuxer has to care about the representation WebVTT cues in a WebM file.
On Fri, 19 Jul 2013 10:21:41 -0400 Matthew Heaney <matthewjheaney@google.com> wrote:
On Fri, Jul 19, 2013 at 6:33 AM, Nicolas George < nicolas.george@normalesup.org> wrote:
That is very good news. I just hope that if someone adds support for WebVTT in another generic format, they will have the intelligence of using the same format as Matroska. Otherwise, one of the demuxers will need a special case; that is what I had in mind when I spoke of "not necessarily under ffmpeg's control" above.
The representation of WebVTT cues in WebM is strictly an artifact of Matroska. There's no expectation that that representation would make sense for some other container. Frank and I (along with various browser vendors) designed the representation that way so that a Matroska demuxer could losslessly reconstruct the original WebVTT cue from its embedded representation. It was never intended as a general-purpose embedded representation of WebVTT cues.
To be fair, it's exactly the same for ASS subtitles. FFmpeg now has two packet formats for ASS: one as used in Matroska packets ("ass"), and one as used in .ass files ("ssa", has nothing to do with SSA). This is a recent change, and before this, matroskadec.c mangled the Matroska packets to make them look like the packets assdec.c outputs. But this was a problem in some ways (for example, duplicated timestamps), so it was changed. In my opinion, the Matroska ASS packet format is a superset of what assdec.c outputs, so assdec.c should be changed to output the Matroska format. Could the same apply in this case?
As it stands now, the Matroska demuxer reconstructs the WebVTT cue such that it exactly matches what the WebVTT demuxer already pushes downstream. To me this makes perfect sense (hence the patch), since the format of the WebVTT cue itself is canonical.
Could be the other way around as well... or is there any "higher" standard that defines what a WebVTT packet is? Personally, and as non-participant, the side data format looks a bit awkward, while packets that are byte chunks make sense to me.
I can change the WebVTT demuxer to convert the original WebVTT cues into a format that matches the embedded representation in a WebM file, but one issue is that this will pollute the rest of ffmpeg with artifacts of WebM. Are you sure this is what you want? If you use WebVTT cues as the canonical representation (as the WebVTT demuxer does now, and the current patch does for the WebM demuxer), this has the benefit that only the WebM demuxer has to care about the representation WebVTT cues in a WebM file.
Le primidi 1er thermidor, an CCXXI, wm4 a écrit :
To be fair, it's exactly the same for ASS subtitles. FFmpeg now has two packet formats for ASS: one as used in Matroska packets ("ass"), and one as used in .ass files ("ssa", has nothing to do with SSA). This is a recent change, and before this, matroskadec.c mangled the Matroska packets to make them look like the packets assdec.c outputs. But this was a problem in some ways (for example, duplicated timestamps), so it was changed.
In my opinion, the Matroska ASS packet format is a superset of what assdec.c outputs, so assdec.c should be changed to output the Matroska format.
That is the plan. The packet format for the SSA codec id was a quick hack to get the interface with libass working, but it was very badly designed from an API point of view. The plan is to deprecate the SSA codec id in favour of ASS, both for the Matroska muxer and demuxer (done), the .ass text file muxer (done) and the .ass text file demuxer (not yet done, should be easy). The ultimate goal would be that ffprobe -show_data shows the same thing when reading from a .ass file or from a Matroska file produced by a third-party application (mkvmerge), and also the same thing as visible in a hex dump of the Matroska file directly. Regards, -- Nicolas George
On Fri, 19 Jul 2013 20:01:26 +0200 Nicolas George <nicolas.george@normalesup.org> wrote:
Le primidi 1er thermidor, an CCXXI, wm4 a écrit :
To be fair, it's exactly the same for ASS subtitles. FFmpeg now has two packet formats for ASS: one as used in Matroska packets ("ass"), and one as used in .ass files ("ssa", has nothing to do with SSA). This is a recent change, and before this, matroskadec.c mangled the Matroska packets to make them look like the packets assdec.c outputs. But this was a problem in some ways (for example, duplicated timestamps), so it was changed.
In my opinion, the Matroska ASS packet format is a superset of what assdec.c outputs, so assdec.c should be changed to output the Matroska format.
That is the plan. The packet format for the SSA codec id was a quick hack to get the interface with libass working, but it was very badly designed from an API point of view.
The plan is to deprecate the SSA codec id in favour of ASS, both for the Matroska muxer and demuxer (done), the .ass text file muxer (done) and the .ass text file demuxer (not yet done, should be easy).
What about "decoded" subtitles? They use the "ssa" format as well.
The ultimate goal would be that ffprobe -show_data shows the same thing when reading from a .ass file or from a Matroska file produced by a third-party application (mkvmerge), and also the same thing as visible in a hex dump of the Matroska file directly.
Regards,
Le primidi 1er thermidor, an CCXXI, wm4 a écrit :
What about "decoded" subtitles? They use the "ssa" format as well.
True, but that is old and ugly. It was added to get interface with libass working, and then people built upon it, but it was never really thought for the long term or support for other codecs. Clément and I have been discussing how to replace it with a more generic and clean structure, but it appears that designing a data structure for styled subtitles is harder than designing a structure for an image or PCM audio. Clément had a proposal at some time, but I do not know how much it progressed. Regards, -- Nicolas George
On Fri, Jul 19, 2013 at 08:01:26PM +0200, Nicolas George wrote:
Le primidi 1er thermidor, an CCXXI, wm4 a écrit :
To be fair, it's exactly the same for ASS subtitles. FFmpeg now has two packet formats for ASS: one as used in Matroska packets ("ass"), and one as used in .ass files ("ssa", has nothing to do with SSA). This is a recent change, and before this, matroskadec.c mangled the Matroska packets to make them look like the packets assdec.c outputs. But this was a problem in some ways (for example, duplicated timestamps), so it was changed.
In my opinion, the Matroska ASS packet format is a superset of what assdec.c outputs, so assdec.c should be changed to output the Matroska format.
That is the plan. The packet format for the SSA codec id was a quick hack to get the interface with libass working, but it was very badly designed from an API point of view.
The plan is to deprecate the SSA codec id in favour of ASS, both for the Matroska muxer and demuxer (done), the .ass text file muxer (done) and the .ass text file demuxer (not yet done, should be easy).
Actually, I'm not so sure about having SSA deprecated (aside from mkv format), but that can be discussed (in another thread please). Anyway, about the whole current thread, I'm sorry I didn't have time to read every single post, but I'd like to have a few words before random development is done, like changing the current (de)muxing of WebVTT. Summary of the current situation: WebVTT "codec" is defined by a payload (the text with its markup) and two extra information. WebVTT and WebM formats mux them differently: - in WebVTT a cue looks like this: [<chapter>\n]<timestamp>[<settings>]\n<text>\n\n - in WebM a cue looks like this: <chapter>\n<settings>\n<text> Please correct me if that's incorrect. Note how the chaptering and its \n are optional in WebVTT for instance, and how the settings are strangely placed along the timing information. Now the problem in my opinion is that WebM uses a full textual way of muxing the 3 informations: requiring some strchr or similar in a binary parser is a bit insane (and dangerous? what about a non null terminated payload?). IMO it would have been much more wise to mux it with \0 separators, but whatever. The point is, if someone decides to mux WebVTT in another format, he might come with a different way and more relevant way of muxing it. TL;DR: the <chapter>, <settings> and <text> are so weirdla (badly?) muxed in *both* WebVTT format and WebM that it, in my opinion, makes sense to separate them at AVPacket level like it is now (payload for <text> and side data for the two other extra info), and let every muxer sanely mux it using the API interface. Note: sorry for not being active recently, I'm back from holidays pretty soon. Regards, -- Clément B.
On Wed, Jul 31, 2013 at 7:47 PM, Clément Bœsch <ubitux@gmail.com> wrote:
- in WebVTT a cue looks like this: [<chapter>\n]<timestamp>[<settings>]\n<text>\n\n - in WebM a cue looks like this: <chapter>\n<settings>\n<text>
Please correct me if that's incorrect.
Of WebM I would say instead: - in WebM a cue looks like this: [<chapter>]\n[<settings>]\n<text>
Now the problem in my opinion is that WebM uses a full textual way of muxing the 3 informations: requiring some strchr or similar in a binary parser is a bit insane (and dangerous? what about a non null terminated payload?).
No worries, that issue was addressed in my last patch. (In WebM, all such payloads are non-null terminated. This is not a problem.)
TL;DR: the <chapter>, <settings> and <text> are so weirdla (badly?) muxed in *both* WebVTT format and WebM that it, in my opinion, makes sense to separate them at AVPacket level like it is now (payload for <text> and side data for the two other extra info), and let every muxer sanely mux it using the API interface.
Right -- that's how it is now. I was hoping that we could commit this patch and then make any additional changes (which are strictly an internal ffmpeg-design matter) as part of a separate patch set. The webvtt changes to ffmpeg that are already present (along with the current patch) do what we need for adding inband text track support to Chrome, which solves our immediate problem. -Matt
Le tridi 13 thermidor, an CCXXI, Matthew Heaney a écrit : > > - in WebM a cue looks like this: <chapter>\n<settings>\n<text> > Of WebM I would say instead: > - in WebM a cue looks like this: [<chapter>]\n[<settings>]\n<text> That only means that <chapter> and <settings> can be empty strings. It makes no difference. > I was hoping that we could commit this patch and then make any additional > changes (which are strictly an internal ffmpeg-design matter) as part of a > separate patch set. There are already a lot of design mistakes in this WebVTT mess, rushing things now will produce even more mistake and make current ones harder to fix. Side question: is the format of WebVTT inside WebM definitely decided? Or is it still time to fix the mistake of using as a delimiter a character that can happen in the payload? Regards, -- Nicolas George
On Thu, Aug 1, 2013 at 4:54 AM, Nicolas George < nicolas.george@normalesup.org> wrote:
Side question: is the format of WebVTT inside WebM definitely decided? Or is it still time to fix the mistake of using as a delimiter a character that can happen in the payload?
The format was decided over a year ago. http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm The delimiters for WebVTT embedded in WebM are the same delimiters as a WebVTT. The parser I included in my last patch works perfectly.
Le quartidi 14 thermidor, an CCXXI, Matthew Heaney a écrit :
The format was decided over a year ago.
http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm
Too bad :(
The delimiters for WebVTT embedded in WebM are the same delimiters as a WebVTT.
WebVTT is a text format, using \0, for example, was not an option.
The parser I included in my last patch works perfectly.
I am sure of it, for valid files. But this choice of delimiter makes it harder to detect broken files, like files where the muxer neglected one of the fields, and makes it also harder to extend the format with additional fields. Anyway, there is no point in dwelling on what can not be changed. I suggest we wait for Clément to be able to join the discussion comfortably. Regards, -- Nicolas George
On Sun, Aug 4, 2013 at 3:14 AM, Nicolas George < nicolas.george@normalesup.org> wrote:
I am sure of it, for valid files. But this choice of delimiter makes it harder to detect broken files, like files where the muxer neglected one of the fields, and makes it also harder to extend the format with additional fields.
Broken files are detected properly, and an error is returned by the parser.
Le quartidi 14 thermidor, an CCXXI, Clement Boesch a écrit :
Actually, I'm not so sure about having SSA deprecated (aside from mkv format), but that can be discussed (in another thread please).
Please share the reasons indeed.
Anyway, about the whole current thread, I'm sorry I didn't have time to read every single post, but I'd like to have a few words before random development is done, like changing the current (de)muxing of WebVTT.
Of course. If you had not replied today, I would have insisted on exactly that.
Summary of the current situation: WebVTT "codec" is defined by a payload (the text with its markup) and two extra information. WebVTT and WebM formats mux them differently:
- in WebVTT a cue looks like this: [<chapter>\n]<timestamp>[<settings>]\n<text>\n\n - in WebM a cue looks like this: <chapter>\n<settings>\n<text>
Please correct me if that's incorrect.
I believe this is technically correct, but by using the "cue" vocable, you forget a very important difference: - in WebVTT, a cue is part of a text file that is being parsed in full; - in WebM, a cue is a packetized in a container format with generic structures and no WebVTT-specific code.
Now the problem in my opinion is that WebM uses a full textual way of muxing the 3 informations: requiring some strchr or similar in a binary parser is a bit insane (and dangerous? what about a non null terminated payload?). IMO it would have been much more wise to mux it with \0 separators, but whatever.
I believe you did not think this argument through: memchr(data, '\n', size) works just as well as memchr(data, 0, size), and you can not use any str* function on untrusted data anyway (unless you rely on padding for 0-termination, but I consider that bad practice). Note that I disagree with the argument you give, but I fully agree with the conclusion itself: \n is a very bad choice for a delimiter in Matroska packets. Because \n can also appear in the payload of one of the fields. That will make extensibility much harder.
The point is, if someone decides to mux WebVTT in another format, he might come with a different way and more relevant way of muxing it.
We will deal with that if that ever happens.
TL;DR: the <chapter>, <settings> and <text> are so weirdla (badly?) muxed in *both* WebVTT format and WebM that it, in my opinion, makes sense to separate them at AVPacket level like it is now (payload for <text> and side data for the two other extra info), and let every muxer sanely mux it using the API interface.
That means adding specific code for the codec in all muxers: you need to have a _very_ good reason to do that, having a "weird" packet format is not enough. Also, note that ASS packets have exactly the same issues, and you agreed that it was better to eliminate the special cases from the Matroska code. Regards, -- Nicolas George
On Thu, Aug 01, 2013 at 01:51:43PM +0200, Nicolas George wrote:
Le quartidi 14 thermidor, an CCXXI, Clement Boesch a écrit :
Actually, I'm not so sure about having SSA deprecated (aside from mkv format), but that can be discussed (in another thread please).
Please share the reasons indeed.
Anyway, about the whole current thread, I'm sorry I didn't have time to read every single post, but I'd like to have a few words before random development is done, like changing the current (de)muxing of WebVTT.
Of course. If you had not replied today, I would have insisted on exactly that.
Summary of the current situation: WebVTT "codec" is defined by a payload (the text with its markup) and two extra information. WebVTT and WebM formats mux them differently:
- in WebVTT a cue looks like this: [<chapter>\n]<timestamp>[<settings>]\n<text>\n\n - in WebM a cue looks like this: <chapter>\n<settings>\n<text>
Please correct me if that's incorrect.
I believe this is technically correct, but by using the "cue" vocable, you forget a very important difference:
- in WebVTT, a cue is part of a text file that is being parsed in full;
- in WebM, a cue is a packetized in a container format with generic structures and no WebVTT-specific code.
Now the problem in my opinion is that WebM uses a full textual way of muxing the 3 informations: requiring some strchr or similar in a binary parser is a bit insane (and dangerous? what about a non null terminated payload?). IMO it would have been much more wise to mux it with \0 separators, but whatever.
I believe you did not think this argument through: memchr(data, '\n', size) works just as well as memchr(data, 0, size), and you can not use any str* function on untrusted data anyway (unless you rely on padding for 0-termination, but I consider that bad practice).
Note that I disagree with the argument you give, but I fully agree with the conclusion itself: \n is a very bad choice for a delimiter in Matroska packets. Because \n can also appear in the payload of one of the fields. That will make extensibility much harder.
The point is, if someone decides to mux WebVTT in another format, he might come with a different way and more relevant way of muxing it.
We will deal with that if that ever happens.
TL;DR: the <chapter>, <settings> and <text> are so weirdla (badly?) muxed in *both* WebVTT format and WebM that it, in my opinion, makes sense to separate them at AVPacket level like it is now (payload for <text> and side data for the two other extra info), and let every muxer sanely mux it using the API interface.
That means adding specific code for the codec in all muxers: you need to have a _very_ good reason to do that, having a "weird" packet format is not enough.
Also, note that ASS packets have exactly the same issues, and you agreed that it was better to eliminate the special cases from the Matroska code.
If you're suggesting to use the WebM representation as the standard, we have to deal with: - retro compat: changing the format of the packets for WebVTT format create version clashes between lavc & lavf (+ deprecation of the side data types) - move the complexity into lavf for webvtt muxing and demuxing - create more hacks if webvtt is muxed differently in another format (will you oppose to a sane muxing of webvtt in NUT?) OTOH, adding a special case only in WebM demuxer where a bad muxing decision was made doesn't look broken. That way of muxing into WebM is merely the result of two successive bad decisions, it doesn't look correct to rely on its layout (and relying on the original WebVTT layout with timestamps will lead to the ASS insanity you know: writing timestamps into demuxer etc). Also note that the hack I wanted to remove with ASS/SSA was the insane string *constructions* of SSA packets within the matroska demuxer, which is not the case here: the text is extracted, and extra data put into side data. Now if you insist on comparing with ASS/SSA, it might be a better solution for that case to abstract the differences between ASS and SSA into side data (like the ReadOrder), but the problem is that ASS/SSA is really on a complete different level from a sanity PoV, so it's really hard to compare with that. And the main issue with ASS/SSA is mainly at the decoding level now, the different codecs might not be that much a problem. Feel free to propose some changes, but I'm a bit tired of jazzing with subtitles issues recently... -- Clément B.
Le primidi 21 thermidor, an CCXXI, Clement Boesch a écrit :
If you're suggesting to use the WebM representation as the standard, we have to deal with:
- retro compat: changing the format of the packets for WebVTT format create version clashes between lavc & lavf (+ deprecation of the side data types)
That is always a burden, but I do not think it should come in the way of deciding a better design: if the design is bad, it will need to be changed sooner or later, and it is less work if it is done sooner. In that particular case, I suspect we could just change the format of the packets and get away with it, although some additional safeguards would probably be better.
- move the complexity into lavf for webvtt muxing and demuxing
I suppose you mean "into lavc"? Otherwise I do not understand your sentence. Then, yes: better the complexity once in lavc than twice in lavf!
- create more hacks if webvtt is muxed differently in another format
We will deal with it if it ever happens. Hopefully, people who decide on a format will chose to adhere to the choices made for WebM, for compatibility's sake.
(will you oppose to a sane muxing of webvtt in NUT?)
You imply that WebM's format is insane. I do not agree at all. It is not perfect¹, but it rather good. (1: it would have been better with 0 as a separator instead of LF and tags in front of each fields.)
OTOH, adding a special case only in WebM demuxer where a bad muxing decision was made doesn't look broken. That way of muxing into WebM is merely the result of two successive bad decisions, it doesn't look correct to rely on its layout
The broken thing is the heavy use of side data. Side data is a hack to code whatever information is present somewhere but does not fit the common data structure. How do you mux side data into NUT? Into MP4? Into elementary streams? Answer: you do not, it does not exist. Side data exist because one particular codec or container has a very specific feature to store one field that does not exist elsewhere and it was not deemed important enough to warrant a new field in AVPacket or AVFrame. It does not apply there.
(and relying on the original WebVTT layout with timestamps will lead to the ASS insanity you know: writing timestamps into demuxer etc).
No one is suggesting that, naturally.
Also note that the hack I wanted to remove with ASS/SSA was the insane string *constructions* of SSA packets within the matroska demuxer, which is not the case here: the text is extracted, and extra data put into side data.
Granted, that was not exactly what I was referring to.
Now if you insist on comparing with ASS/SSA, it might be a better solution for that case to abstract the differences between ASS and SSA into side data (like the ReadOrder),
That is exactly what I was referring to: ASS-in-Matroska and WebVTT-in-WebM share the exact same structure: technical_field, delimiter, technical-field... delimiter, actual_text. And NO! it is not the demuxer's job to split the fields in the packet; a packet is a blob of arbitrary data, it should be totally opaque to the demuxer. The job of splitting the fields in the packet is for the decoder. And the result should go, in the end, in specific fields in the new AVSubtitle2 structure, not in the magical bag of holding called side data. Regards, -- Nicolas George
On Sat, Aug 10, 2013 at 12:37:03PM +0200, Nicolas George wrote:
Le primidi 21 thermidor, an CCXXI, Clement Boesch a écrit :
If you're suggesting to use the WebM representation as the standard, we have to deal with:
- retro compat: changing the format of the packets for WebVTT format create version clashes between lavc & lavf (+ deprecation of the side data types)
That is always a burden, but I do not think it should come in the way of deciding a better design: if the design is bad, it will need to be changed sooner or later, and it is less work if it is done sooner.
In that particular case, I suspect we could just change the format of the packets and get away with it, although some additional safeguards would probably be better.
We have a release with that packet form already.
- move the complexity into lavf for webvtt muxing and demuxing
I suppose you mean "into lavc"? Otherwise I do not understand your sentence. Then, yes: better the complexity once in lavc than twice in lavf!
My point is that the complexity will be twice in lavf: WebVTT demuxer will have to craft a AVPacket in the WebM layout (not really more complex in comparison to the current code I admit, even though I don't like doing that), and the lavf muxer will have to parse strings from the AVPacket data. And on a side note, you'll also need to add some code in webvtt decoder to skip those irrelevant information (which are currently accessible anyway through the side data if you really want them).
- create more hacks if webvtt is muxed differently in another format
We will deal with it if it ever happens. Hopefully, people who decide on a format will chose to adhere to the choices made for WebM, for compatibility's sake.
(will you oppose to a sane muxing of webvtt in NUT?)
You imply that WebM's format is insane. I do not agree at all. It is not perfect¹, but it rather good.
(1: it would have been better with 0 as a separator instead of LF and tags in front of each fields.)
That was my point. After all the muxing operation only defines how to merge those fields, and that's the ugly thing here. But sure, that's not that much a problem.
OTOH, adding a special case only in WebM demuxer where a bad muxing decision was made doesn't look broken. That way of muxing into WebM is merely the result of two successive bad decisions, it doesn't look correct to rely on its layout
The broken thing is the heavy use of side data. Side data is a hack to code whatever information is present somewhere but does not fit the common data structure. How do you mux side data into NUT? Into MP4? Into elementary streams? Answer: you do not, it does not exist.
Sure, and in MKV either. The main interest of having chapter & settings separate into side data was purposely to avoid each muxers from parsing them from the AVPacket to merge them in their desired way.
Side data exist because one particular codec or container has a very specific feature to store one field that does not exist elsewhere and it was not deemed important enough to warrant a new field in AVPacket or AVFrame.
It does not apply there.
I don't really consider the use of side data an abuse here, but simply a different usage of it.
(and relying on the original WebVTT layout with timestamps will lead to the ASS insanity you know: writing timestamps into demuxer etc).
No one is suggesting that, naturally.
'Just wanted to be sure :)
Also note that the hack I wanted to remove with ASS/SSA was the insane string *constructions* of SSA packets within the matroska demuxer, which is not the case here: the text is extracted, and extra data put into side data.
Granted, that was not exactly what I was referring to.
Now if you insist on comparing with ASS/SSA, it might be a better solution for that case to abstract the differences between ASS and SSA into side data (like the ReadOrder),
That is exactly what I was referring to: ASS-in-Matroska and WebVTT-in-WebM share the exact same structure: technical_field, delimiter, technical-field... delimiter, actual_text.
And NO! it is not the demuxer's job to split the fields in the packet; a packet is a blob of arbitrary data, it should be totally opaque to the demuxer.
WebVTT demuxer needs to split them anyway. So the only additional burden code will remain in webm demuxer. Not doing this means having that parsing burden in a lot of other places (see top of my reply).
The job of splitting the fields in the packet is for the decoder. And the result should go, in the end, in specific fields in the new AVSubtitle2 structure, not in the magical bag of holding called side data.
So you want the guy proposing a relatively trivial demuxing of WebVTT in WebM to wait (or implement) AVSubtitle2 and deal with all your concerns about the current state? That doesn't sound very wise to me, unless you can propose a patchset in the following week... And I'm not even sure your changes proposition is really an improvement over the current situation. -- Clément B.
On Sat, Aug 10, 2013 at 08:09:40PM +0200, Clément Bœsch wrote: [...]
My point is that the complexity will be twice in lavf: WebVTT demuxer will have to craft a AVPacket in the WebM layout (not really more complex in comparison to the current code I admit, even though I don't like doing that), and the lavf muxer will have to parse strings from the AVPacket ^^^^ WebVTT data.
[...] -- Clément B.
On Sat, Aug 10, 2013 at 11:09 AM, Clément Bœsch <ubitux@gmail.com> wrote:
So you want the guy proposing a relatively trivial demuxing of WebVTT in WebM to wait (or implement) AVSubtitle2 and deal with all your concerns about the current state?
As a compromise, can we commit the patch we have now, and then later, make a separate decision about whether to move webvtt frame parsing elsewhere? The reason I ask is because the Chrome team (that would be me) needs the ffmpeg demuxer to support parsing of text tracks that appear in WebM files. For now, storing the cue id and cue settings in the side data works fine for us. (Storing it somewhere else would work for us too!) But without this patch, then the ffmpeg demuxer doesn't support text tracks at all. If the ffmpeg development team decides to change things up later, that's fine -- I'll fix up Chrome to accommodate the later changes. (I'll even volunteer to make the ffmpeg changes.) But we just need some modicum of support now in the ffmpeg demuxer for handle inband text tracks, so we are able to commit the associated changes to the media pipeline in Chrome. Thanks, Matt
On Sat, Aug 17, 2013 at 03:38:32PM -0700, Matthew Heaney wrote:
On Sat, Aug 10, 2013 at 11:09 AM, Clément Bœsch <ubitux@gmail.com> wrote:
So you want the guy proposing a relatively trivial demuxing of WebVTT in WebM to wait (or implement) AVSubtitle2 and deal with all your concerns about the current state?
As a compromise, can we commit the patch we have now, and then later, make
ok applied thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB There will always be a question for which you do not know the correct answer.
Le primidi 1er thermidor, an CCXXI, Matthew Heaney a écrit :
The representation of WebVTT cues in WebM is strictly an artifact of Matroska. There's no expectation that that representation would make sense for some other container. Frank and I (along with various browser vendors) designed the representation that way so that a Matroska demuxer could losslessly reconstruct the original WebVTT cue from its embedded representation.
I do not see why other container formats would not want to losslessly represent WebVTT files too. The reason behind the technical choices you made should also apply to any format similar to Matroska.
As it stands now, the Matroska demuxer reconstructs the WebVTT cue such that it exactly matches what the WebVTT demuxer already pushes downstream. To me this makes perfect sense (hence the patch), since the format of the WebVTT cue itself is canonical.
I can change the WebVTT demuxer to convert the original WebVTT cues into a format that matches the embedded representation in a WebM file, but one issue is that this will pollute the rest of ffmpeg with artifacts of WebM. Are you sure this is what you want? If you use WebVTT cues as the canonical representation (as the WebVTT demuxer does now, and the current patch does for the WebM demuxer), this has the benefit that only the WebM demuxer has to care about the representation WebVTT cues in a WebM file.
If I understand you correctly, you seem to consider the text file representation of WebVTT as the authoritative form. That may be the source of the misunderstandings, since I do not share that assumption. For me, the basic unit is the packet. A packet can be decoded into... something (that currently looks like ASS markup, until a real solution is filanized), that can later be encoded into a packet for the same or another codec; a packet can be read from a file or stored into a file. .mkv or .vtt files are just serialization of sequences of packets. Since ffmpeg does everything (encoding, decoding, muxing, demuxing), it can pretty much choose the packet format as it pleases. But there are choices smarter than others: the choices that lead to minimum code complexity and maximum compatibility. They are usually the same, unless someone did something stupid (H.264-in-MP4, I am looking at you). Compatibility means that you can use ffmpeg's components with third-party libraries. For example, it is nice to be able to use ffmpeg's Ogg demuxer with Xiph's Vorbis decoder, or Xiph's Ogg demuxer with ffmpeg's Vorbis decoder. For that to work, all libraries must have the same packet format; usually they do. I do not believe it applies here. Is there a common library for parsing WebVTT files that outputs something that can count as a packet? That leaves us code complexity. Let us assume we are one year ago, when ffmpeg had no support for WebVTT at all. To read from text .vtt files (and write to them, that is symmetrical, I will only talk about demuxing), a brand new demuxer must be written, and it does only that. In that case, the packet format is pretty much free: adding a counter or a line break there or here, putting this info in that field or this one, it does not change anything. The Matroska demuxer is quite another story: it already knows about packets. If you leave it as is, it should output the WebVTT packets just as they are stored in the file. The code complexity argument is there: you can get full WebVTT packets by just adding the map between CODEC_ID_WEBVTT and the code used in Matroska. A single line of code. Of course, now that the text WebVTT parser and writer exist, things are a little more complex. But on the long run I believe it would be better to review the packet format without considering that issue. I can state another argument, but I believe it boils down to the same basic principles: the reasons that made you choose this particular format to store in Matroska probably apply to AVPacket as well. Sure, AVPacket is slightly more powerful than Matroska, with its side data and stuff. But the way I see it, side data is for situations where the container features really exceeds the packet model, while WebVTT-in-Matroska fits the model perfectly. Regards, -- Nicolas George
On Fri, Jul 19, 2013 at 3:12 PM, Nicolas George < nicolas.george@normalesup.org> wrote:
I can state another argument, but I believe it boils down to the same basic principles: the reasons that made you choose this particular format to store in Matroska probably apply to AVPacket as well. Sure, AVPacket is slightly more powerful than Matroska, with its side data and stuff. But the way I see it, side data is for situations where the container features really exceeds the packet model, while WebVTT-in-Matroska fits the model perfectly.
To summarize my task: (0) Remove the webvtt side data enumeration values declared in avcodec.h. (1) Rewrite the webvtt decoder to strip out the timestamp from the original webvtt cue, and use that result as the value of the avpacket data, such that we don't use side data anymore. (2) Rewrite the matroska decoder to detect the presence of subtitle track, and use the Matroska frame as the value of the avpacket data. (3) Rewrite the webvtt encoder to use the new format of the avpacket (instead of the current format, which uses side data). (4) Rewrite the webm encoder to use the new format of the avpacket when writing a subtitle track. Can someone confirm whether my understanding of the task is correct? Thanks, Matt
On Tue, Jul 16, 2013 at 1:23 AM, wm4 <nfxjfg@googlemail.com> wrote:
Can I ask why there is subtitle parsing in the Matroska demuxer? I find this a bit odd. Why split them into side-data, instead of just leaving the packet as-is?
The cue id and settings are an artifact of WebVTT. ffmpeg expects to see just the cue payload as the packet data, so we have to play nice with the other codecs.
On Mon, Jul 15, 2013 at 05:02:55PM -0700, Matthew Heaney wrote:
WebM files now support inband text tracks, as described in the following specification:
http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm
The Matroska demuxer now detects the presence of WebVTT tracks, synthesizing WebVTT packets (having codec id AV_CODEC_ID_WEBVTT) and pushing them downstream in the normal way. --- libavformat/matroska.c | 5 ++ libavformat/matroskadec.c | 150 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 154 insertions(+), 1 deletion(-)
I'm assuming this is the latest version.
diff --git a/libavformat/matroska.c b/libavformat/matroska.c index ee57c18..953c572 100644 --- a/libavformat/matroska.c +++ b/libavformat/matroska.c @@ -57,6 +57,11 @@ const CodecTags ff_mkv_codec_tags[]={ {"A_VORBIS" , AV_CODEC_ID_VORBIS}, {"A_WAVPACK4" , AV_CODEC_ID_WAVPACK},
+ {"D_WEBVTT/SUBTITLES" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/CAPTIONS" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/DESCRIPTIONS", AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/METADATA" , AV_CODEC_ID_WEBVTT}, + {"S_TEXT/UTF8" , AV_CODEC_ID_SUBRIP}, {"S_TEXT/UTF8" , AV_CODEC_ID_TEXT}, {"S_TEXT/UTF8" , AV_CODEC_ID_SRT}, diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 9a15a2b..7466258 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -1566,7 +1566,8 @@ static int matroska_read_header(AVFormatContext *s) /* Apply some sanity checks. */ if (track->type != MATROSKA_TRACK_TYPE_VIDEO && track->type != MATROSKA_TRACK_TYPE_AUDIO && - track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { + track->type != MATROSKA_TRACK_TYPE_SUBTITLE && + track->type != MATROSKA_TRACK_TYPE_METADATA) { av_log(matroska->ctx, AV_LOG_INFO, "Unknown or unsupported track type %"PRIu64"\n", track->type); @@ -1860,6 +1861,16 @@ static int matroska_read_header(AVFormatContext *s) st->codec->bits_per_coded_sample = track->audio.bitdepth; if (st->codec->codec_id != AV_CODEC_ID_AAC) st->need_parsing = AVSTREAM_PARSE_HEADERS; + } else if (codec_id == AV_CODEC_ID_WEBVTT) { + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; + + if (!strcmp(track->codec_id, "D_WEBVTT/CAPTIONS")) { + st->disposition |= AV_DISPOSITION_CAPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/DESCRIPTIONS")) { + st->disposition |= AV_DISPOSITION_DESCRIPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/METADATA")) { + st->disposition |= AV_DISPOSITION_METADATA; + } } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; #if FF_API_ASS_SSA @@ -2226,6 +2237,135 @@ fail: return ret; }
+static int matroska_parse_webvtt(MatroskaDemuxContext *matroska, + MatroskaTrack *track, + AVStream *st, + uint8_t *data, int data_len, + uint64_t timecode, + uint64_t duration, + int64_t pos) +{ + AVPacket *pkt; + uint8_t *id, *settings, *text, *buf; + int id_len, settings_len, text_len; + uint8_t *p, *q; + + if (data_len <= 0) + return AVERROR_INVALIDDATA; + + p = data; + q = data + data_len; + + id = p; + id_len = -1;
+ while (p != q && id_len < 0) { + switch (*p) { + case '\r': + case '\n': + id_len = p - id; + break; + default: + p++; + break; + } + }
You can save a few lines with: while (p < q) { if (*p == '\r' || *p == '\n') { id_len = p - id; break; } p++; } (untested)
+ + if (p >= q) + return AVERROR_INVALIDDATA; +
(A) see below
+ if (*p == '\r') + p++;
I would include that check just after defining id_len in the previous loop...
+ if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA;
...which will make (A) uneeded because of that p >= q check here.
+ p++; + + if (p >= q) + return AVERROR_INVALIDDATA; +
Just check for p >= q - 1 previously to remove that redundancy.
+ settings = p; + settings_len = -1; + while (p != q && settings_len < 0) { + switch (*p) { + case '\r': + case '\n': + settings_len = p - settings; + break; + default: + p++; + break; + } + } + + if (p >= q) + return AVERROR_INVALIDDATA; + + if (*p == '\r') + p++; + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + if (p >= q) + return AVERROR_INVALIDDATA; +
It might be possible to simplify that whole chunk the same way.
+ text = p; + text_len = q - p; + while (text_len > 0) { + const int len = text_len - 1; + const uint8_t c = p[len]; + if (c != '\r' && c != '\n') + break; + text_len = len; + } +
+ pkt = av_mallocz(sizeof(AVPacket));
sizeof(*pkt)
+ if (av_new_packet(pkt, text_len) < 0) { + av_free(pkt); + return AVERROR(ENOMEM); + } +
While it's certainly an ENOMEM, it might be better to raise the return value from av_new_packet().
+ memcpy(pkt->data, text, text_len); + + if (id_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_IDENTIFIER, + id_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, id, id_len); + } + + if (settings_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_SETTINGS, + settings_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, settings, settings_len); + } +
+ // Do we need this for subtitles? + // pkt->flags = AV_PKT_FLAG_KEY; +
That's a good question
+ pkt->stream_index = st->index; + pkt->pts = timecode; + + // Do we need this for subtitles? + // pkt->dts = timecode; + + pkt->duration = duration; + pkt->pos = pos; + + dynarray_add(&matroska->packets, &matroska->num_packets, pkt); + matroska->prev_pkt = pkt; + + return 0; +} + static int matroska_parse_frame(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, @@ -2454,6 +2594,14 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, if (res) goto end;
+ } else if (st->codec->codec_id == AV_CODEC_ID_WEBVTT) { + res = matroska_parse_webvtt(matroska, track, st, + data, lace_size[n], + timecode, lace_duration, + pos); + if (res) + goto end; + } else { res = matroska_parse_frame(matroska, track, st, data, lace_size[n], timecode, lace_duration,
I'm not the mkv/webm maintainer, but it looks correct otherwise. -- Clément B.
Le primidi 21 thermidor, an CCXXI, Clement Boesch a écrit :
I'm not the mkv/webm maintainer, but it looks correct otherwise.
Except we still do not have decided whether this is a good idea to have WebVTT-specific code in the Matroska muxer and demuxer. I still am strongly against it. This mail: http://ffmpeg.org/pipermail/ffmpeg-devel/2013-August/146743.html still needs an answer from you. Regards, -- Nicolas George
WebM files now support inband text tracks, as described in the following specification: http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm The Matroska demuxer now detects the presence of WebVTT tracks, synthesizing WebVTT packets (having codec id AV_CODEC_ID_WEBVTT) and pushing them downstream in the normal way. --- libavformat/matroska.c | 5 ++ libavformat/matroska.h | 2 +- libavformat/matroskadec.c | 135 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 140 insertions(+), 2 deletions(-) diff --git a/libavformat/matroska.c b/libavformat/matroska.c index ee57c18..953c572 100644 --- a/libavformat/matroska.c +++ b/libavformat/matroska.c @@ -57,6 +57,11 @@ const CodecTags ff_mkv_codec_tags[]={ {"A_VORBIS" , AV_CODEC_ID_VORBIS}, {"A_WAVPACK4" , AV_CODEC_ID_WAVPACK}, + {"D_WEBVTT/SUBTITLES" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/CAPTIONS" , AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/DESCRIPTIONS", AV_CODEC_ID_WEBVTT}, + {"D_WEBVTT/METADATA" , AV_CODEC_ID_WEBVTT}, + {"S_TEXT/UTF8" , AV_CODEC_ID_SUBRIP}, {"S_TEXT/UTF8" , AV_CODEC_ID_TEXT}, {"S_TEXT/UTF8" , AV_CODEC_ID_SRT}, diff --git a/libavformat/matroska.h b/libavformat/matroska.h index 2eb2fe6..1ff97d7 100644 --- a/libavformat/matroska.h +++ b/libavformat/matroska.h @@ -264,7 +264,7 @@ typedef enum { */ typedef struct CodecTags{ - char str[20]; + char str[22]; enum AVCodecID id; }CodecTags; diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index d52877c..7a5dd1d 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -1566,7 +1566,8 @@ static int matroska_read_header(AVFormatContext *s) /* Apply some sanity checks. */ if (track->type != MATROSKA_TRACK_TYPE_VIDEO && track->type != MATROSKA_TRACK_TYPE_AUDIO && - track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { + track->type != MATROSKA_TRACK_TYPE_SUBTITLE && + track->type != MATROSKA_TRACK_TYPE_METADATA) { av_log(matroska->ctx, AV_LOG_INFO, "Unknown or unsupported track type %"PRIu64"\n", track->type); @@ -1860,6 +1861,16 @@ static int matroska_read_header(AVFormatContext *s) st->codec->bits_per_coded_sample = track->audio.bitdepth; if (st->codec->codec_id != AV_CODEC_ID_AAC) st->need_parsing = AVSTREAM_PARSE_HEADERS; + } else if (codec_id == AV_CODEC_ID_WEBVTT) { + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; + + if (!strcmp(track->codec_id, "D_WEBVTT/CAPTIONS")) { + st->disposition |= AV_DISPOSITION_CAPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/DESCRIPTIONS")) { + st->disposition |= AV_DISPOSITION_DESCRIPTIONS; + } else if (!strcmp(track->codec_id, "D_WEBVTT/METADATA")) { + st->disposition |= AV_DISPOSITION_METADATA; + } } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; #if FF_API_ASS_SSA @@ -2226,6 +2237,120 @@ fail: return ret; } +static int matroska_parse_webvtt(MatroskaDemuxContext *matroska, + MatroskaTrack *track, + AVStream *st, + uint8_t *data, int data_len, + uint64_t timecode, + uint64_t duration, + int64_t pos) +{ + AVPacket *pkt; + uint8_t *id, *settings, *text, *buf; + int id_len, settings_len, text_len; + uint8_t *p, *q; + int err; + + if (data_len <= 0) + return AVERROR_INVALIDDATA; + + p = data; + q = data + data_len; + + id = p; + id_len = -1; + while (p < q) { + if (*p == '\r' || *p == '\n') { + id_len = p - id; + if (*p == '\r') + p++; + break; + } + p++; + } + + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + settings = p; + settings_len = -1; + while (p < q) { + if (*p == '\r' || *p == '\n') { + settings_len = p - settings; + if (*p == '\r') + p++; + break; + } + p++; + } + + if (p >= q || *p != '\n') + return AVERROR_INVALIDDATA; + p++; + + text = p; + text_len = q - p; + while (text_len > 0) { + const int len = text_len - 1; + const uint8_t c = p[len]; + if (c != '\r' && c != '\n') + break; + text_len = len; + } + + if (text_len <= 0) + return AVERROR_INVALIDDATA; + + pkt = av_mallocz(sizeof(*pkt)); + err = av_new_packet(pkt, text_len); + if (err < 0) { + av_free(pkt); + return AVERROR(err); + } + + memcpy(pkt->data, text, text_len); + + if (id_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_IDENTIFIER, + id_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, id, id_len); + } + + if (settings_len > 0) { + buf = av_packet_new_side_data(pkt, + AV_PKT_DATA_WEBVTT_SETTINGS, + settings_len); + if (buf == NULL) { + av_free(pkt); + return AVERROR(ENOMEM); + } + memcpy(buf, settings, settings_len); + } + + // Do we need this for subtitles? + // pkt->flags = AV_PKT_FLAG_KEY; + + pkt->stream_index = st->index; + pkt->pts = timecode; + + // Do we need this for subtitles? + // pkt->dts = timecode; + + pkt->duration = duration; + pkt->pos = pos; + + dynarray_add(&matroska->packets, &matroska->num_packets, pkt); + matroska->prev_pkt = pkt; + + return 0; +} + static int matroska_parse_frame(MatroskaDemuxContext *matroska, MatroskaTrack *track, AVStream *st, @@ -2454,6 +2579,14 @@ static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, if (res) goto end; + } else if (st->codec->codec_id == AV_CODEC_ID_WEBVTT) { + res = matroska_parse_webvtt(matroska, track, st, + data, lace_size[n], + timecode, lace_duration, + pos); + if (res) + goto end; + } else { res = matroska_parse_frame(matroska, track, st, data, lace_size[n], timecode, lace_duration, -- 1.8.3
On Mon, Jul 15, 2013 at 02:08:24PM -0700, Matthew Heaney wrote:
WebM files now support inband text tracks, as described in the following specification:
http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm
The Matroska demuxer now detects the presence of WebVTT tracks, synthesizing WebVTT packets (having codec id AV_CODEC_ID_WEBVTT) and pushing them downstream in the normal way.
Hello, Can you provide some sample to test the feature ? Thanks in advance. Matthieu [...]
See if this link works for you guys: https://docs.google.com/file/d/0B_3umRpv6Zhcb0lKRXh5aUttTnc/edit?usp=sharing The file is 141 KB. Note that you can already use ffmpeg to make a file like this, if you have a .vtt file lying around. On Tue, Jul 16, 2013 at 1:02 AM, Matthieu Bouron <matthieu.bouron@gmail.com>wrote:
On Mon, Jul 15, 2013 at 02:08:24PM -0700, Matthew Heaney wrote:
WebM files now support inband text tracks, as described in the following specification:
http://wiki.webmproject.org/webm-metadata/temporal-metadata/webvtt-in-webm
The Matroska demuxer now detects the presence of WebVTT tracks, synthesizing WebVTT packets (having codec id AV_CODEC_ID_WEBVTT) and pushing them downstream in the normal way.
Hello,
Can you provide some sample to test the feature ? Thanks in advance.
Matthieu
[...]
L'octidi 28 messidor, an CCXXI, Matthew Heaney a écrit :
See if this link works for you guys:
https://docs.google.com/file/d/0B_3umRpv6Zhcb0lKRXh5aUttTnc/edit?usp=sharing
The file is 141 KB.
I think some people here would not be happy to have to log into a Google account to get the file. Better use less intrusive hosting providers whenever possible. Also, could you share the corresponding text WebVTT file? (And please remember not to top-post.) Regards, -- Nicolas George
On Thu, Jul 18, 2013 at 3:59 AM, Nicolas George < nicolas.george@normalesup.org> wrote:
I think some people here would not be happy to have to log into a Google account to get the file. Better use less intrusive hosting providers whenever possible.
No account log-in should be necessary.
Also, could you share the corresponding text WebVTT file?
I'm on vacation, so I don't have access to my work computer, but you can simply extract the text track from the file, as ffmpeg has a webvtt muxer now.
participants (6)
-
Clément Bœsch -
Matthew Heaney -
Matthieu Bouron -
Michael Niedermayer -
Nicolas George -
wm4