00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #include "avformat.h"
00028 #include "internal.h"
00029 #include "subtitles.h"
00030 #include "libavutil/bprint.h"
00031 #include "libavutil/intreadwrite.h"
00032
00033 typedef struct {
00034 FFDemuxSubtitlesQueue q;
00035 } WebVTTContext;
00036
00037 static int webvtt_probe(AVProbeData *p)
00038 {
00039 const uint8_t *ptr = p->buf;
00040
00041 if (AV_RB24(ptr) == 0xEFBBBF)
00042 ptr += 3;
00043 if (!strncmp(ptr, "WEBVTT", 6) &&
00044 (!ptr[6] || strchr("\n\r\t ", ptr[6])))
00045 return AVPROBE_SCORE_MAX;
00046 return 0;
00047 }
00048
00049 static int64_t read_ts(const char *s)
00050 {
00051 int hh, mm, ss, ms;
00052 if (sscanf(s, "%u:%u:%u.%u", &hh, &mm, &ss, &ms) == 4) return (hh*3600 + mm*60 + ss) * 1000 + ms;
00053 if (sscanf(s, "%u:%u.%u", &mm, &ss, &ms) == 3) return ( mm*60 + ss) * 1000 + ms;
00054 return AV_NOPTS_VALUE;
00055 }
00056
00057 static int64_t extract_cue(AVBPrint *buf, AVIOContext *pb)
00058 {
00059 int prev_chr_is_eol = 0;
00060 int64_t pos = avio_tell(pb);
00061
00062 av_bprint_clear(buf);
00063 for (;;) {
00064 char c = avio_r8(pb);
00065 if (!c)
00066 break;
00067 if (c == '\r' || c == '\n') {
00068 if (prev_chr_is_eol)
00069 break;
00070 prev_chr_is_eol = (c == '\n');
00071 } else
00072 prev_chr_is_eol = 0;
00073 if (c != '\r')
00074 av_bprint_chars(buf, c, 1);
00075 }
00076 av_bprint_chars(buf, '\0', 1);
00077 return pos;
00078 }
00079
00080 static int webvtt_read_header(AVFormatContext *s)
00081 {
00082 WebVTTContext *webvtt = s->priv_data;
00083 AVBPrint header, cue;
00084 int res = 0;
00085 AVStream *st = avformat_new_stream(s, NULL);
00086
00087 if (!st)
00088 return AVERROR(ENOMEM);
00089 avpriv_set_pts_info(st, 64, 1, 1000);
00090 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00091 st->codec->codec_id = AV_CODEC_ID_WEBVTT;
00092
00093 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
00094 av_bprint_init(&cue, 0, AV_BPRINT_SIZE_UNLIMITED);
00095
00096 for (;;) {
00097 int i, len;
00098 int64_t pos = extract_cue(&cue, s->pb);
00099 AVPacket *sub;
00100 const char *p = cue.str;
00101 const char *identifier = p;
00102
00103 int64_t ts_start, ts_end;
00104
00105 if (!*p)
00106 break;
00107
00108
00109 if (!strncmp(p, "\xEF\xBB\xBFWEBVTT", 9) ||
00110 !strncmp(p, "WEBVTT", 6))
00111 continue;
00112
00113
00114
00115 for (i = 0; p[i] && p[i] != '\n'; i++) {
00116 if (!strncmp(p + i, "-->", 3)) {
00117 identifier = NULL;
00118 break;
00119 }
00120 }
00121 if (identifier)
00122 p += strcspn(p, "\n");
00123
00124
00125 if ((ts_start = read_ts(p)) == AV_NOPTS_VALUE)
00126 break;
00127 if (!(p = strstr(p, "-->")))
00128 break;
00129 p += 3;
00130 do p++; while (*p == ' ' || *p == '\t');
00131 if ((ts_end = read_ts(p)) == AV_NOPTS_VALUE)
00132 break;
00133
00134
00135 p += strcspn(p, "\n\t ");
00136 while (*p == '\t' || *p == ' ')
00137 p++;
00138 if (*p != '\n') {
00139
00140 p += strcspn(p, "\n");
00141 }
00142 if (*p == '\n')
00143 p++;
00144
00145
00146 len = cue.str + cue.len - p - 1;
00147 sub = ff_subtitles_queue_insert(&webvtt->q, p, len, 0);
00148 if (!sub) {
00149 res = AVERROR(ENOMEM);
00150 goto end;
00151 }
00152 sub->pos = pos;
00153 sub->pts = ts_start;
00154 sub->duration = ts_end - ts_start;
00155 }
00156
00157 ff_subtitles_queue_finalize(&webvtt->q);
00158
00159 end:
00160 av_bprint_finalize(&cue, NULL);
00161 av_bprint_finalize(&header, NULL);
00162 return res;
00163 }
00164
00165 static int webvtt_read_packet(AVFormatContext *s, AVPacket *pkt)
00166 {
00167 WebVTTContext *webvtt = s->priv_data;
00168 return ff_subtitles_queue_read_packet(&webvtt->q, pkt);
00169 }
00170
00171 static int webvtt_read_close(AVFormatContext *s)
00172 {
00173 WebVTTContext *webvtt = s->priv_data;
00174 ff_subtitles_queue_clean(&webvtt->q);
00175 return 0;
00176 }
00177
00178 AVInputFormat ff_webvtt_demuxer = {
00179 .name = "webvtt",
00180 .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
00181 .priv_data_size = sizeof(WebVTTContext),
00182 .read_probe = webvtt_probe,
00183 .read_header = webvtt_read_header,
00184 .read_packet = webvtt_read_packet,
00185 .read_close = webvtt_read_close,
00186 .flags = AVFMT_GENERIC_INDEX,
00187 .extensions = "vtt",
00188 };