yading@11: /* yading@11: * Copyright (c) 2012 Clément Bœsch yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * WebVTT subtitle demuxer yading@11: * @see http://dev.w3.org/html5/webvtt/ yading@11: */ yading@11: yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: #include "subtitles.h" yading@11: #include "libavutil/bprint.h" yading@11: #include "libavutil/intreadwrite.h" yading@11: yading@11: typedef struct { yading@11: FFDemuxSubtitlesQueue q; yading@11: } WebVTTContext; yading@11: yading@11: static int webvtt_probe(AVProbeData *p) yading@11: { yading@11: const uint8_t *ptr = p->buf; yading@11: yading@11: if (AV_RB24(ptr) == 0xEFBBBF) yading@11: ptr += 3; /* skip UTF-8 BOM */ yading@11: if (!strncmp(ptr, "WEBVTT", 6) && yading@11: (!ptr[6] || strchr("\n\r\t ", ptr[6]))) yading@11: return AVPROBE_SCORE_MAX; yading@11: return 0; yading@11: } yading@11: yading@11: static int64_t read_ts(const char *s) yading@11: { yading@11: int hh, mm, ss, ms; yading@11: if (sscanf(s, "%u:%u:%u.%u", &hh, &mm, &ss, &ms) == 4) return (hh*3600LL + mm*60LL + ss) * 1000LL + ms; yading@11: if (sscanf(s, "%u:%u.%u", &mm, &ss, &ms) == 3) return ( mm*60LL + ss) * 1000LL + ms; yading@11: return AV_NOPTS_VALUE; yading@11: } yading@11: yading@11: static int webvtt_read_header(AVFormatContext *s) yading@11: { yading@11: WebVTTContext *webvtt = s->priv_data; yading@11: AVBPrint header, cue; yading@11: int res = 0; yading@11: AVStream *st = avformat_new_stream(s, NULL); yading@11: yading@11: if (!st) yading@11: return AVERROR(ENOMEM); yading@11: avpriv_set_pts_info(st, 64, 1, 1000); yading@11: st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; yading@11: st->codec->codec_id = AV_CODEC_ID_WEBVTT; yading@11: yading@11: av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED); yading@11: av_bprint_init(&cue, 0, AV_BPRINT_SIZE_UNLIMITED); yading@11: yading@11: for (;;) { yading@11: int i; yading@11: int64_t pos; yading@11: AVPacket *sub; yading@11: const char *p, *identifier; yading@11: //const char *settings = NULL; yading@11: int64_t ts_start, ts_end; yading@11: yading@11: ff_subtitles_read_chunk(s->pb, &cue); yading@11: yading@11: if (!cue.len) yading@11: break; yading@11: yading@11: p = identifier = cue.str; yading@11: pos = avio_tell(s->pb); yading@11: yading@11: /* ignore header chunk */ yading@11: if (!strncmp(p, "\xEF\xBB\xBFWEBVTT", 9) || yading@11: !strncmp(p, "WEBVTT", 6)) yading@11: continue; yading@11: yading@11: /* optional cue identifier (can be a number like in SRT or some kind of yading@11: * chaptering id), silently skip it */ yading@11: for (i = 0; p[i] && p[i] != '\n'; i++) { yading@11: if (!strncmp(p + i, "-->", 3)) { yading@11: identifier = NULL; yading@11: break; yading@11: } yading@11: } yading@11: if (identifier) yading@11: p += strcspn(p, "\n"); yading@11: yading@11: /* cue timestamps */ yading@11: if ((ts_start = read_ts(p)) == AV_NOPTS_VALUE) yading@11: break; yading@11: if (!(p = strstr(p, "-->"))) yading@11: break; yading@11: p += 3; yading@11: do p++; while (*p == ' ' || *p == '\t'); yading@11: if ((ts_end = read_ts(p)) == AV_NOPTS_VALUE) yading@11: break; yading@11: yading@11: /* optional cue settings, TODO: store in side_data */ yading@11: p += strcspn(p, "\n\t "); yading@11: while (*p == '\t' || *p == ' ') yading@11: p++; yading@11: if (*p != '\n') { yading@11: //settings = p; yading@11: p += strcspn(p, "\n"); yading@11: } yading@11: if (*p == '\n') yading@11: p++; yading@11: yading@11: /* create packet */ yading@11: sub = ff_subtitles_queue_insert(&webvtt->q, p, strlen(p), 0); yading@11: if (!sub) { yading@11: res = AVERROR(ENOMEM); yading@11: goto end; yading@11: } yading@11: sub->pos = pos; yading@11: sub->pts = ts_start; yading@11: sub->duration = ts_end - ts_start; yading@11: } yading@11: yading@11: ff_subtitles_queue_finalize(&webvtt->q); yading@11: yading@11: end: yading@11: av_bprint_finalize(&cue, NULL); yading@11: av_bprint_finalize(&header, NULL); yading@11: return res; yading@11: } yading@11: yading@11: static int webvtt_read_packet(AVFormatContext *s, AVPacket *pkt) yading@11: { yading@11: WebVTTContext *webvtt = s->priv_data; yading@11: return ff_subtitles_queue_read_packet(&webvtt->q, pkt); yading@11: } yading@11: yading@11: static int webvtt_read_seek(AVFormatContext *s, int stream_index, yading@11: int64_t min_ts, int64_t ts, int64_t max_ts, int flags) yading@11: { yading@11: WebVTTContext *webvtt = s->priv_data; yading@11: return ff_subtitles_queue_seek(&webvtt->q, s, stream_index, yading@11: min_ts, ts, max_ts, flags); yading@11: } yading@11: yading@11: static int webvtt_read_close(AVFormatContext *s) yading@11: { yading@11: WebVTTContext *webvtt = s->priv_data; yading@11: ff_subtitles_queue_clean(&webvtt->q); yading@11: return 0; yading@11: } yading@11: yading@11: AVInputFormat ff_webvtt_demuxer = { yading@11: .name = "webvtt", yading@11: .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"), yading@11: .priv_data_size = sizeof(WebVTTContext), yading@11: .read_probe = webvtt_probe, yading@11: .read_header = webvtt_read_header, yading@11: .read_packet = webvtt_read_packet, yading@11: .read_seek2 = webvtt_read_seek, yading@11: .read_close = webvtt_read_close, yading@11: .extensions = "vtt", yading@11: };