2012-09-01 12:34:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Clément Bœsch
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* WebVTT subtitle demuxer
|
|
|
|
* @see http://dev.w3.org/html5/webvtt/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "subtitles.h"
|
|
|
|
#include "libavutil/bprint.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2013-06-24 18:20:32 +00:00
|
|
|
#include "libavutil/opt.h"
|
2012-09-01 12:34:24 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2013-06-24 18:20:32 +00:00
|
|
|
const AVClass *class;
|
2012-09-01 12:34:24 +00:00
|
|
|
FFDemuxSubtitlesQueue q;
|
2013-06-24 18:20:32 +00:00
|
|
|
int kind;
|
2012-09-01 12:34:24 +00:00
|
|
|
} WebVTTContext;
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int webvtt_probe(const AVProbeData *p)
|
2012-09-01 12:34:24 +00:00
|
|
|
{
|
|
|
|
const uint8_t *ptr = p->buf;
|
|
|
|
|
|
|
|
if (AV_RB24(ptr) == 0xEFBBBF)
|
|
|
|
ptr += 3; /* skip UTF-8 BOM */
|
|
|
|
if (!strncmp(ptr, "WEBVTT", 6) &&
|
|
|
|
(!ptr[6] || strchr("\n\r\t ", ptr[6])))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t read_ts(const char *s)
|
|
|
|
{
|
|
|
|
int hh, mm, ss, ms;
|
2012-10-20 23:16:49 +00:00
|
|
|
if (sscanf(s, "%u:%u:%u.%u", &hh, &mm, &ss, &ms) == 4) return (hh*3600LL + mm*60LL + ss) * 1000LL + ms;
|
|
|
|
if (sscanf(s, "%u:%u.%u", &mm, &ss, &ms) == 3) return ( mm*60LL + ss) * 1000LL + ms;
|
2012-09-01 12:34:24 +00:00
|
|
|
return AV_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int webvtt_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
WebVTTContext *webvtt = s->priv_data;
|
2020-04-17 01:47:25 +00:00
|
|
|
AVBPrint cue;
|
2012-09-01 12:34:24 +00:00
|
|
|
int res = 0;
|
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
|
|
|
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
avpriv_set_pts_info(st, 64, 1, 1000);
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_WEBVTT;
|
2013-06-24 18:20:32 +00:00
|
|
|
st->disposition |= webvtt->kind;
|
2012-09-01 12:34:24 +00:00
|
|
|
|
|
|
|
av_bprint_init(&cue, 0, AV_BPRINT_SIZE_UNLIMITED);
|
|
|
|
|
|
|
|
for (;;) {
|
2013-01-03 06:45:29 +00:00
|
|
|
int i;
|
|
|
|
int64_t pos;
|
2012-09-01 12:34:24 +00:00
|
|
|
AVPacket *sub;
|
2013-05-31 23:27:32 +00:00
|
|
|
const char *p, *identifier, *settings;
|
2024-04-25 09:18:18 +00:00
|
|
|
size_t identifier_len, settings_len;
|
2012-09-01 12:34:24 +00:00
|
|
|
int64_t ts_start, ts_end;
|
|
|
|
|
2013-01-03 06:45:29 +00:00
|
|
|
ff_subtitles_read_chunk(s->pb, &cue);
|
|
|
|
|
|
|
|
if (!cue.len)
|
2012-09-01 12:34:24 +00:00
|
|
|
break;
|
|
|
|
|
2013-01-03 06:45:29 +00:00
|
|
|
p = identifier = cue.str;
|
|
|
|
pos = avio_tell(s->pb);
|
|
|
|
|
2012-09-01 12:34:24 +00:00
|
|
|
/* ignore header chunk */
|
|
|
|
if (!strncmp(p, "\xEF\xBB\xBFWEBVTT", 9) ||
|
2015-10-11 16:11:02 +00:00
|
|
|
!strncmp(p, "WEBVTT", 6) ||
|
|
|
|
!strncmp(p, "NOTE", 4))
|
2012-09-01 12:34:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* optional cue identifier (can be a number like in SRT or some kind of
|
2013-05-31 23:27:32 +00:00
|
|
|
* chaptering id) */
|
|
|
|
for (i = 0; p[i] && p[i] != '\n' && p[i] != '\r'; i++) {
|
2012-09-01 12:34:24 +00:00
|
|
|
if (!strncmp(p + i, "-->", 3)) {
|
|
|
|
identifier = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-05-31 23:27:32 +00:00
|
|
|
if (!identifier)
|
|
|
|
identifier_len = 0;
|
|
|
|
else {
|
|
|
|
identifier_len = strcspn(p, "\r\n");
|
|
|
|
p += identifier_len;
|
|
|
|
if (*p == '\r')
|
|
|
|
p++;
|
|
|
|
if (*p == '\n')
|
|
|
|
p++;
|
|
|
|
}
|
2012-09-01 12:34:24 +00:00
|
|
|
|
|
|
|
/* cue timestamps */
|
|
|
|
if ((ts_start = read_ts(p)) == AV_NOPTS_VALUE)
|
|
|
|
break;
|
|
|
|
if (!(p = strstr(p, "-->")))
|
|
|
|
break;
|
2014-01-07 03:08:18 +00:00
|
|
|
p += 2;
|
2012-09-01 12:34:24 +00:00
|
|
|
do p++; while (*p == ' ' || *p == '\t');
|
|
|
|
if ((ts_end = read_ts(p)) == AV_NOPTS_VALUE)
|
|
|
|
break;
|
|
|
|
|
2013-05-31 23:27:32 +00:00
|
|
|
/* optional cue settings */
|
2022-05-23 03:23:35 +00:00
|
|
|
p += strcspn(p, "\n\r\t ");
|
2012-09-01 12:34:24 +00:00
|
|
|
while (*p == '\t' || *p == ' ')
|
|
|
|
p++;
|
2013-05-31 23:27:32 +00:00
|
|
|
settings = p;
|
|
|
|
settings_len = strcspn(p, "\r\n");
|
|
|
|
p += settings_len;
|
|
|
|
if (*p == '\r')
|
|
|
|
p++;
|
2012-09-01 12:34:24 +00:00
|
|
|
if (*p == '\n')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* create packet */
|
2013-01-03 06:45:29 +00:00
|
|
|
sub = ff_subtitles_queue_insert(&webvtt->q, p, strlen(p), 0);
|
2012-09-01 12:34:24 +00:00
|
|
|
if (!sub) {
|
|
|
|
res = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
sub->pos = pos;
|
|
|
|
sub->pts = ts_start;
|
|
|
|
sub->duration = ts_end - ts_start;
|
2013-05-31 23:27:32 +00:00
|
|
|
|
2013-06-01 13:08:00 +00:00
|
|
|
#define SET_SIDE_DATA(name, type) do { \
|
|
|
|
if (name##_len) { \
|
|
|
|
uint8_t *buf = av_packet_new_side_data(sub, type, name##_len); \
|
|
|
|
if (!buf) { \
|
|
|
|
res = AVERROR(ENOMEM); \
|
|
|
|
goto end; \
|
|
|
|
} \
|
|
|
|
memcpy(buf, name, name##_len); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
SET_SIDE_DATA(identifier, AV_PKT_DATA_WEBVTT_IDENTIFIER);
|
|
|
|
SET_SIDE_DATA(settings, AV_PKT_DATA_WEBVTT_SETTINGS);
|
2012-09-01 12:34:24 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 19:40:07 +00:00
|
|
|
ff_subtitles_queue_finalize(s, &webvtt->q);
|
2012-09-01 12:34:24 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
av_bprint_finalize(&cue, NULL);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int webvtt_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
WebVTTContext *webvtt = s->priv_data;
|
|
|
|
return ff_subtitles_queue_read_packet(&webvtt->q, pkt);
|
|
|
|
}
|
|
|
|
|
2012-11-23 20:43:54 +00:00
|
|
|
static int webvtt_read_seek(AVFormatContext *s, int stream_index,
|
|
|
|
int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
|
|
|
|
{
|
|
|
|
WebVTTContext *webvtt = s->priv_data;
|
|
|
|
return ff_subtitles_queue_seek(&webvtt->q, s, stream_index,
|
|
|
|
min_ts, ts, max_ts, flags);
|
|
|
|
}
|
|
|
|
|
2012-09-01 12:34:24 +00:00
|
|
|
static int webvtt_read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
WebVTTContext *webvtt = s->priv_data;
|
|
|
|
ff_subtitles_queue_clean(&webvtt->q);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-24 18:20:32 +00:00
|
|
|
#define OFFSET(x) offsetof(WebVTTContext, x)
|
2019-06-04 02:54:45 +00:00
|
|
|
#define KIND_FLAGS AV_OPT_FLAG_SUBTITLE_PARAM|AV_OPT_FLAG_DECODING_PARAM
|
2013-06-24 18:20:32 +00:00
|
|
|
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "kind", "Set kind of WebVTT track", OFFSET(kind), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, KIND_FLAGS, "webvtt_kind" },
|
2019-06-04 02:54:45 +00:00
|
|
|
{ "subtitles", "WebVTT subtitles kind", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, KIND_FLAGS, "webvtt_kind" },
|
|
|
|
{ "captions", "WebVTT captions kind", 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS }, INT_MIN, INT_MAX, KIND_FLAGS, "webvtt_kind" },
|
|
|
|
{ "descriptions", "WebVTT descriptions kind", 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS }, INT_MIN, INT_MAX, KIND_FLAGS, "webvtt_kind" },
|
|
|
|
{ "metadata", "WebVTT metadata kind", 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA }, INT_MIN, INT_MAX, KIND_FLAGS, "webvtt_kind" },
|
2013-06-24 18:20:32 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass webvtt_demuxer_class = {
|
|
|
|
.class_name = "WebVTT demuxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
const AVInputFormat ff_webvtt_demuxer = {
|
2012-09-01 12:34:24 +00:00
|
|
|
.name = "webvtt",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
|
|
|
|
.priv_data_size = sizeof(WebVTTContext),
|
2024-04-25 09:18:18 +00:00
|
|
|
.flags_internal = FF_FMT_INIT_CLEANUP,
|
2012-09-01 12:34:24 +00:00
|
|
|
.read_probe = webvtt_probe,
|
|
|
|
.read_header = webvtt_read_header,
|
|
|
|
.read_packet = webvtt_read_packet,
|
2012-11-23 20:43:54 +00:00
|
|
|
.read_seek2 = webvtt_read_seek,
|
2012-09-01 12:34:24 +00:00
|
|
|
.read_close = webvtt_read_close,
|
|
|
|
.extensions = "vtt",
|
2013-06-24 18:20:32 +00:00
|
|
|
.priv_class = &webvtt_demuxer_class,
|
2012-09-01 12:34:24 +00:00
|
|
|
};
|