]> git.sesse.net Git - ffmpeg/blob - libavformat/webvttdec.c
lavfi: convert remaining input/output list compound literals to named objects.
[ffmpeg] / libavformat / webvttdec.c
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 http://dev.w3.org/html5/webvtt/
25  */
26
27 #include "avformat.h"
28 #include "internal.h"
29 #include "subtitles.h"
30 #include "libavutil/bprint.h"
31 #include "libavutil/intreadwrite.h"
32
33 typedef struct {
34     FFDemuxSubtitlesQueue q;
35 } WebVTTContext;
36
37 static int webvtt_probe(AVProbeData *p)
38 {
39     const uint8_t *ptr = p->buf;
40
41     if (AV_RB24(ptr) == 0xEFBBBF)
42         ptr += 3;  /* skip UTF-8 BOM */
43     if (!strncmp(ptr, "WEBVTT", 6) &&
44         (!ptr[6] || strchr("\n\r\t ", ptr[6])))
45         return AVPROBE_SCORE_MAX;
46     return 0;
47 }
48
49 static int64_t read_ts(const char *s)
50 {
51     int hh, mm, ss, ms;
52     if (sscanf(s, "%u:%u:%u.%u", &hh, &mm, &ss, &ms) == 4) return (hh*3600LL + mm*60LL + ss) * 1000LL + ms;
53     if (sscanf(s,    "%u:%u.%u",      &mm, &ss, &ms) == 3) return (            mm*60LL + ss) * 1000LL + ms;
54     return AV_NOPTS_VALUE;
55 }
56
57 static int64_t extract_cue(AVBPrint *buf, AVIOContext *pb)
58 {
59     int prev_chr_is_eol = 0;
60     int64_t pos = avio_tell(pb);
61
62     av_bprint_clear(buf);
63     for (;;) {
64         char c = avio_r8(pb);
65         if (!c)
66             break;
67         if (c == '\r' || c == '\n') {
68             if (prev_chr_is_eol)
69                 break;
70             prev_chr_is_eol = (c == '\n');
71         } else
72             prev_chr_is_eol = 0;
73         if (c != '\r')
74             av_bprint_chars(buf, c, 1);
75     }
76     av_bprint_chars(buf, '\0', 1);
77     return pos;
78 }
79
80 static int webvtt_read_header(AVFormatContext *s)
81 {
82     WebVTTContext *webvtt = s->priv_data;
83     AVBPrint header, cue;
84     int res = 0;
85     AVStream *st = avformat_new_stream(s, NULL);
86
87     if (!st)
88         return AVERROR(ENOMEM);
89     avpriv_set_pts_info(st, 64, 1, 1000);
90     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
91     st->codec->codec_id   = AV_CODEC_ID_WEBVTT;
92
93     av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
94     av_bprint_init(&cue,    0, AV_BPRINT_SIZE_UNLIMITED);
95
96     for (;;) {
97         int i, len;
98         int64_t pos = extract_cue(&cue, s->pb);
99         AVPacket *sub;
100         const char *p = cue.str;
101         const char *identifier = p;
102         //const char *settings = NULL;
103         int64_t ts_start, ts_end;
104
105         if (!*p) // EOF
106             break;
107
108         /* ignore header chunk */
109         if (!strncmp(p, "\xEF\xBB\xBFWEBVTT", 9) ||
110             !strncmp(p, "WEBVTT", 6))
111             continue;
112
113         /* optional cue identifier (can be a number like in SRT or some kind of
114          * chaptering id), silently skip it */
115         for (i = 0; p[i] && p[i] != '\n'; i++) {
116             if (!strncmp(p + i, "-->", 3)) {
117                 identifier = NULL;
118                 break;
119             }
120         }
121         if (identifier)
122             p += strcspn(p, "\n");
123
124         /* cue timestamps */
125         if ((ts_start = read_ts(p)) == AV_NOPTS_VALUE)
126             break;
127         if (!(p = strstr(p, "-->")))
128             break;
129         p += 3;
130         do p++; while (*p == ' ' || *p == '\t');
131         if ((ts_end = read_ts(p)) == AV_NOPTS_VALUE)
132             break;
133
134         /* optional cue settings, TODO: store in side_data */
135         p += strcspn(p, "\n\t ");
136         while (*p == '\t' || *p == ' ')
137             p++;
138         if (*p != '\n') {
139             //settings = p;
140             p += strcspn(p, "\n");
141         }
142         if (*p == '\n')
143             p++;
144
145         /* create packet */
146         len = cue.str + cue.len - p - 1;
147         sub = ff_subtitles_queue_insert(&webvtt->q, p, len, 0);
148         if (!sub) {
149             res = AVERROR(ENOMEM);
150             goto end;
151         }
152         sub->pos = pos;
153         sub->pts = ts_start;
154         sub->duration = ts_end - ts_start;
155     }
156
157     ff_subtitles_queue_finalize(&webvtt->q);
158
159 end:
160     av_bprint_finalize(&cue,    NULL);
161     av_bprint_finalize(&header, NULL);
162     return res;
163 }
164
165 static int webvtt_read_packet(AVFormatContext *s, AVPacket *pkt)
166 {
167     WebVTTContext *webvtt = s->priv_data;
168     return ff_subtitles_queue_read_packet(&webvtt->q, pkt);
169 }
170
171 static int webvtt_read_close(AVFormatContext *s)
172 {
173     WebVTTContext *webvtt = s->priv_data;
174     ff_subtitles_queue_clean(&webvtt->q);
175     return 0;
176 }
177
178 AVInputFormat ff_webvtt_demuxer = {
179     .name           = "webvtt",
180     .long_name      = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
181     .priv_data_size = sizeof(WebVTTContext),
182     .read_probe     = webvtt_probe,
183     .read_header    = webvtt_read_header,
184     .read_packet    = webvtt_read_packet,
185     .read_close     = webvtt_read_close,
186     .flags          = AVFMT_GENERIC_INDEX,
187     .extensions     = "vtt",
188 };