FFmpeg
Loading...
Searching...
No Matches
webvttdec.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Clément Bœsch
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * WebVTT subtitle demuxer
24 * @see https://www.w3.org/TR/webvtt1/
25 */
26
27#include "avformat.h"
28#include "demux.h"
29#include "internal.h"
30#include "subtitles.h"
31#include "libavutil/avstring.h"
32#include "libavutil/bprint.h"
34#include "libavutil/opt.h"
35
36typedef struct {
37 const AVClass *class;
39 int kind;
42
43static int webvtt_probe(const AVProbeData *p)
44{
45 const uint8_t *ptr = p->buf;
46
47 if (AV_RB24(ptr) == 0xEFBBBF)
48 ptr += 3; /* skip UTF-8 BOM */
49 if (!strncmp(ptr, "WEBVTT", 6) &&
50 (!ptr[6] || strchr("\n\r\t ", ptr[6])))
51 return AVPROBE_SCORE_MAX;
52 return 0;
53}
54
55static int64_t read_ts(const char *s)
56{
57 int hh, mm, ss, ms;
58 if (sscanf(s, "%u:%u:%u.%u", &hh, &mm, &ss, &ms) == 4) return (hh*3600LL + mm*60LL + ss) * 1000LL + ms;
59 if (sscanf(s, "%u:%u.%u", &mm, &ss, &ms) == 3) return ( mm*60LL + ss) * 1000LL + ms;
60 return AV_NOPTS_VALUE;
61}
62
63/* X-TIMESTAMP-MAP=LOCAL:<cue time>,MPEGTS:<MPEG-2 time>, in either order,
64 * on a header line of its own */
65static void parse_timestamp_map(AVFormatContext *s, const char *p,
66 AVRational time_base, int64_t *offset)
67{
68 int64_t local = AV_NOPTS_VALUE, mpegts = AV_NOPTS_VALUE;
69
70 while (*p && !av_strstart(p, "X-TIMESTAMP-MAP=", NULL)) {
71 p += strcspn(p, "\r\n");
72 p += strspn(p, "\r\n");
73 }
74 if (!*p)
75 return;
76 p += strlen("X-TIMESTAMP-MAP=");
77 while (*p && *p != '\r' && *p != '\n') {
78 const char *val;
79 if (av_strstart(p, "LOCAL:", &val)) {
80 local = read_ts(val);
81 } else if (av_strstart(p, "MPEGTS:", &val)) {
82 char *end;
83 mpegts = strtoll(val, &end, 10);
84 if (end == val || (*end && !strchr(",\r\n", *end)) ||
85 mpegts < 0 || mpegts >= (1LL << 33))
86 mpegts = AV_NOPTS_VALUE;
87 }
88 p += strcspn(p, ",\r\n");
89 if (*p == ',')
90 p++;
91 }
92 if (local == AV_NOPTS_VALUE || mpegts == AV_NOPTS_VALUE) {
93 av_log(s, AV_LOG_WARNING, "Invalid X-TIMESTAMP-MAP header\n");
94 return;
95 }
96
97 *offset = av_rescale_q(mpegts, (AVRational){ 1, 90000 }, time_base) -
98 av_rescale_q(local, (AVRational){ 1, 1000 }, time_base);
99}
100
102{
103 WebVTTContext *webvtt = s->priv_data;
104 AVBPrint cue;
105 int res = 0;
106 int64_t ts_offset = 0;
108
109 if (!st)
110 return AVERROR(ENOMEM);
111 avpriv_set_pts_info(st, 64, 1, webvtt->timestamp_map ? 90000 : 1000);
114 st->disposition |= webvtt->kind;
115
117
118 for (;;) {
119 int i;
120 int64_t pos;
121 AVPacket *sub;
122 const char *p, *identifier, *settings;
123 size_t identifier_len, settings_len;
124 int64_t ts_start, ts_end;
125
126 res = ff_subtitles_read_chunk(s->pb, &cue);
127 if (res < 0)
128 goto end;
129
130 if (!cue.len)
131 break;
132
133 p = identifier = cue.str;
134 pos = avio_tell(s->pb);
135
136 /* header chunk */
137 if (!strncmp(p, "\xEF\xBB\xBFWEBVTT", 9) ||
138 !strncmp(p, "WEBVTT", 6) ||
139 (!strncmp(p, "X-TIMESTAMP-MAP=", 16) && !strstr(p, "-->"))) {
140 if (webvtt->timestamp_map)
141 parse_timestamp_map(s, p, st->time_base, &ts_offset);
142 continue;
143 }
144
145 if (!strncmp(p, "STYLE", 5) ||
146 !strncmp(p, "REGION", 6) ||
147 !strncmp(p, "NOTE", 4))
148 continue;
149
150 /* optional cue identifier (can be a number like in SRT or some kind of
151 * chaptering id) */
152 for (i = 0; p[i] && p[i] != '\n' && p[i] != '\r'; i++) {
153 if (!strncmp(p + i, "-->", 3)) {
154 identifier = NULL;
155 break;
156 }
157 }
158 if (!identifier)
159 identifier_len = 0;
160 else {
161 identifier_len = strcspn(p, "\r\n");
162 p += identifier_len;
163 if (*p == '\r')
164 p++;
165 if (*p == '\n')
166 p++;
167 }
168
169 /* cue timestamps */
170 if ((ts_start = read_ts(p)) == AV_NOPTS_VALUE)
171 break;
172 if (!(p = strstr(p, "-->")))
173 break;
174 p += 2;
175 do p++; while (*p == ' ' || *p == '\t');
176 if ((ts_end = read_ts(p)) == AV_NOPTS_VALUE)
177 break;
178
179 /* optional cue settings */
180 p += strcspn(p, "\n\r\t ");
181 while (*p == '\t' || *p == ' ')
182 p++;
183 settings = p;
184 settings_len = strcspn(p, "\r\n");
185 p += settings_len;
186 if (*p == '\r')
187 p++;
188 if (*p == '\n')
189 p++;
190
191 /* create packet */
192 sub = ff_subtitles_queue_insert(&webvtt->q, p, strlen(p), 0);
193 if (!sub) {
194 res = AVERROR(ENOMEM);
195 goto end;
196 }
197 sub->pos = pos;
198 sub->pts = av_rescale_q(ts_start, (AVRational){ 1, 1000 }, st->time_base) +
199 ts_offset;
200 sub->duration = av_rescale_q(ts_end - ts_start, (AVRational){ 1, 1000 },
201 st->time_base);
202
203#define SET_SIDE_DATA(name, type) do { \
204 if (name##_len) { \
205 uint8_t *buf = av_packet_new_side_data(sub, type, name##_len); \
206 if (!buf) { \
207 res = AVERROR(ENOMEM); \
208 goto end; \
209 } \
210 memcpy(buf, name, name##_len); \
211 } \
212} while (0)
213
216 }
217
219
220end:
222 return res;
223}
224
226{
227 WebVTTContext *webvtt = s->priv_data;
228 return ff_subtitles_queue_read_packet(&webvtt->q, pkt);
229}
230
231static int webvtt_read_seek(AVFormatContext *s, int stream_index,
232 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
233{
234 WebVTTContext *webvtt = s->priv_data;
235 return ff_subtitles_queue_seek(&webvtt->q, s, stream_index,
236 min_ts, ts, max_ts, flags);
237}
238
240{
241 WebVTTContext *webvtt = s->priv_data;
242 ff_subtitles_queue_clean(&webvtt->q);
243 return 0;
244}
245
246#define OFFSET(x) offsetof(WebVTTContext, x)
247#define KIND_FLAGS AV_OPT_FLAG_SUBTITLE_PARAM|AV_OPT_FLAG_DECODING_PARAM
248
249static const AVOption options[] = {
250 { "kind", "Set kind of WebVTT track", OFFSET(kind), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, KIND_FLAGS, .unit = "webvtt_kind" },
251 { "subtitles", "WebVTT subtitles kind", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, KIND_FLAGS, .unit = "webvtt_kind" },
252 { "captions", "WebVTT captions kind", 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS }, INT_MIN, INT_MAX, KIND_FLAGS, .unit = "webvtt_kind" },
253 { "descriptions", "WebVTT descriptions kind", 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS }, INT_MIN, INT_MAX, KIND_FLAGS, .unit = "webvtt_kind" },
254 { "metadata", "WebVTT metadata kind", 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA }, INT_MIN, INT_MAX, KIND_FLAGS, .unit = "webvtt_kind" },
255 { "timestamp_map", "Apply the X-TIMESTAMP-MAP header to the cue timestamps, in the 90 kHz MPEG-TS time base", OFFSET(timestamp_map), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, KIND_FLAGS },
256 { NULL }
257};
258
260 .class_name = "WebVTT demuxer",
261 .item_name = av_default_item_name,
262 .option = options,
263 .version = LIBAVUTIL_VERSION_INT,
264};
265
267 .p.name = "webvtt",
268 .p.long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
269 .p.mime_type = "text/vtt",
270 .p.extensions = "vtt,webvtt",
271 .p.priv_class = &webvtt_demuxer_class,
272 .priv_data_size = sizeof(WebVTTContext),
273 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
277 .read_seek2 = webvtt_read_seek,
279};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFInputFormat ff_webvtt_demuxer
Definition webvttdec.c:266
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:865
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AV_DISPOSITION_CAPTIONS
The subtitle stream contains captions, providing a transcription and possibly a translation of audio.
Definition avformat.h:712
#define AV_DISPOSITION_METADATA
The subtitle stream contains time-aligned metadata that is not intended to be directly presented to t...
Definition avformat.h:723
#define AV_DISPOSITION_DESCRIPTIONS
The subtitle stream contains a textual description of the video content.
Definition avformat.h:718
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:507
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
AVBPrint public header.
#define AV_BPRINT_SIZE_UNLIMITED
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define ss(width, name, subs,...)
Definition cbs_vp9.c:202
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:580
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_CODEC_ID_WEBVTT
Definition codec_id.h:590
@ AV_PKT_DATA_WEBVTT_SETTINGS
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
Definition packet.h:199
@ AV_PKT_DATA_WEBVTT_IDENTIFIER
The optional first identifier line of a WebVTT cue.
Definition packet.h:193
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:234
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition avstring.c:36
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define AV_RB24(x)
unsigned offset
Definition libaomenc.c:763
#define KIND_FLAGS
Definition webvttdec.c:247
static int webvtt_read_seek(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition webvttdec.c:231
#define SET_SIDE_DATA(name, type)
static int webvtt_probe(const AVProbeData *p)
Definition webvttdec.c:43
static int64_t read_ts(const char *s)
Definition webvttdec.c:55
static const AVClass webvtt_demuxer_class
Definition webvttdec.c:259
static int webvtt_read_header(AVFormatContext *s)
Definition webvttdec.c:101
static int webvtt_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition webvttdec.c:225
static int webvtt_read_close(AVFormatContext *s)
Definition webvttdec.c:239
static void parse_timestamp_map(AVFormatContext *s, const char *p, AVRational time_base, int64_t *offset)
Definition webvttdec.c:65
#define OFFSET(x)
Definition webvttdec.c:246
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
AVOptions.
unsigned int pos
Definition spdifenc.c:431
Describe the class of an AVClass context structure.
Definition log.h:76
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1401
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition packet.h:628
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:603
int64_t pos
byte position in stream, -1 if unknown
Definition packet.h:630
This structure contains the data a format has to probe a file.
Definition avformat.h:473
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:837
int timestamp_map
Definition webvttdec.c:40
FFDemuxSubtitlesQueue q
Definition webvttdec.c:38
int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Update current_sub_idx to emulate a seek.
Definition subtitles.c:269
AVPacket * ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, const uint8_t *event, size_t len, int merge)
Insert a new subtitle event.
Definition subtitles.c:111
int ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
Same as ff_subtitles_read_text_chunk(), but read from an AVIOContext.
Definition subtitles.c:445
int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
Generic read_packet() callback for subtitles demuxers using this queue system.
Definition subtitles.c:230
void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q)
Set missing durations, sort subtitles by PTS (and then byte position), and drop duplicated events.
Definition subtitles.c:212
void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
Remove and destroy all the subtitles packets.
Definition subtitles.c:321
#define av_log(a,...)