annotate ffmpeg/libavformat/webvttdec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Copyright (c) 2012 Clément Bœsch
yading@11 3 *
yading@11 4 * This file is part of FFmpeg.
yading@11 5 *
yading@11 6 * FFmpeg is free software; you can redistribute it and/or
yading@11 7 * modify it under the terms of the GNU Lesser General Public
yading@11 8 * License as published by the Free Software Foundation; either
yading@11 9 * version 2.1 of the License, or (at your option) any later version.
yading@11 10 *
yading@11 11 * FFmpeg is distributed in the hope that it will be useful,
yading@11 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 14 * Lesser General Public License for more details.
yading@11 15 *
yading@11 16 * You should have received a copy of the GNU Lesser General Public
yading@11 17 * License along with FFmpeg; if not, write to the Free Software
yading@11 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 19 */
yading@11 20
yading@11 21 /**
yading@11 22 * @file
yading@11 23 * WebVTT subtitle demuxer
yading@11 24 * @see http://dev.w3.org/html5/webvtt/
yading@11 25 */
yading@11 26
yading@11 27 #include "avformat.h"
yading@11 28 #include "internal.h"
yading@11 29 #include "subtitles.h"
yading@11 30 #include "libavutil/bprint.h"
yading@11 31 #include "libavutil/intreadwrite.h"
yading@11 32
yading@11 33 typedef struct {
yading@11 34 FFDemuxSubtitlesQueue q;
yading@11 35 } WebVTTContext;
yading@11 36
yading@11 37 static int webvtt_probe(AVProbeData *p)
yading@11 38 {
yading@11 39 const uint8_t *ptr = p->buf;
yading@11 40
yading@11 41 if (AV_RB24(ptr) == 0xEFBBBF)
yading@11 42 ptr += 3; /* skip UTF-8 BOM */
yading@11 43 if (!strncmp(ptr, "WEBVTT", 6) &&
yading@11 44 (!ptr[6] || strchr("\n\r\t ", ptr[6])))
yading@11 45 return AVPROBE_SCORE_MAX;
yading@11 46 return 0;
yading@11 47 }
yading@11 48
yading@11 49 static int64_t read_ts(const char *s)
yading@11 50 {
yading@11 51 int hh, mm, ss, ms;
yading@11 52 if (sscanf(s, "%u:%u:%u.%u", &hh, &mm, &ss, &ms) == 4) return (hh*3600LL + mm*60LL + ss) * 1000LL + ms;
yading@11 53 if (sscanf(s, "%u:%u.%u", &mm, &ss, &ms) == 3) return ( mm*60LL + ss) * 1000LL + ms;
yading@11 54 return AV_NOPTS_VALUE;
yading@11 55 }
yading@11 56
yading@11 57 static int webvtt_read_header(AVFormatContext *s)
yading@11 58 {
yading@11 59 WebVTTContext *webvtt = s->priv_data;
yading@11 60 AVBPrint header, cue;
yading@11 61 int res = 0;
yading@11 62 AVStream *st = avformat_new_stream(s, NULL);
yading@11 63
yading@11 64 if (!st)
yading@11 65 return AVERROR(ENOMEM);
yading@11 66 avpriv_set_pts_info(st, 64, 1, 1000);
yading@11 67 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
yading@11 68 st->codec->codec_id = AV_CODEC_ID_WEBVTT;
yading@11 69
yading@11 70 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
yading@11 71 av_bprint_init(&cue, 0, AV_BPRINT_SIZE_UNLIMITED);
yading@11 72
yading@11 73 for (;;) {
yading@11 74 int i;
yading@11 75 int64_t pos;
yading@11 76 AVPacket *sub;
yading@11 77 const char *p, *identifier;
yading@11 78 //const char *settings = NULL;
yading@11 79 int64_t ts_start, ts_end;
yading@11 80
yading@11 81 ff_subtitles_read_chunk(s->pb, &cue);
yading@11 82
yading@11 83 if (!cue.len)
yading@11 84 break;
yading@11 85
yading@11 86 p = identifier = cue.str;
yading@11 87 pos = avio_tell(s->pb);
yading@11 88
yading@11 89 /* ignore header chunk */
yading@11 90 if (!strncmp(p, "\xEF\xBB\xBFWEBVTT", 9) ||
yading@11 91 !strncmp(p, "WEBVTT", 6))
yading@11 92 continue;
yading@11 93
yading@11 94 /* optional cue identifier (can be a number like in SRT or some kind of
yading@11 95 * chaptering id), silently skip it */
yading@11 96 for (i = 0; p[i] && p[i] != '\n'; i++) {
yading@11 97 if (!strncmp(p + i, "-->", 3)) {
yading@11 98 identifier = NULL;
yading@11 99 break;
yading@11 100 }
yading@11 101 }
yading@11 102 if (identifier)
yading@11 103 p += strcspn(p, "\n");
yading@11 104
yading@11 105 /* cue timestamps */
yading@11 106 if ((ts_start = read_ts(p)) == AV_NOPTS_VALUE)
yading@11 107 break;
yading@11 108 if (!(p = strstr(p, "-->")))
yading@11 109 break;
yading@11 110 p += 3;
yading@11 111 do p++; while (*p == ' ' || *p == '\t');
yading@11 112 if ((ts_end = read_ts(p)) == AV_NOPTS_VALUE)
yading@11 113 break;
yading@11 114
yading@11 115 /* optional cue settings, TODO: store in side_data */
yading@11 116 p += strcspn(p, "\n\t ");
yading@11 117 while (*p == '\t' || *p == ' ')
yading@11 118 p++;
yading@11 119 if (*p != '\n') {
yading@11 120 //settings = p;
yading@11 121 p += strcspn(p, "\n");
yading@11 122 }
yading@11 123 if (*p == '\n')
yading@11 124 p++;
yading@11 125
yading@11 126 /* create packet */
yading@11 127 sub = ff_subtitles_queue_insert(&webvtt->q, p, strlen(p), 0);
yading@11 128 if (!sub) {
yading@11 129 res = AVERROR(ENOMEM);
yading@11 130 goto end;
yading@11 131 }
yading@11 132 sub->pos = pos;
yading@11 133 sub->pts = ts_start;
yading@11 134 sub->duration = ts_end - ts_start;
yading@11 135 }
yading@11 136
yading@11 137 ff_subtitles_queue_finalize(&webvtt->q);
yading@11 138
yading@11 139 end:
yading@11 140 av_bprint_finalize(&cue, NULL);
yading@11 141 av_bprint_finalize(&header, NULL);
yading@11 142 return res;
yading@11 143 }
yading@11 144
yading@11 145 static int webvtt_read_packet(AVFormatContext *s, AVPacket *pkt)
yading@11 146 {
yading@11 147 WebVTTContext *webvtt = s->priv_data;
yading@11 148 return ff_subtitles_queue_read_packet(&webvtt->q, pkt);
yading@11 149 }
yading@11 150
yading@11 151 static int webvtt_read_seek(AVFormatContext *s, int stream_index,
yading@11 152 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
yading@11 153 {
yading@11 154 WebVTTContext *webvtt = s->priv_data;
yading@11 155 return ff_subtitles_queue_seek(&webvtt->q, s, stream_index,
yading@11 156 min_ts, ts, max_ts, flags);
yading@11 157 }
yading@11 158
yading@11 159 static int webvtt_read_close(AVFormatContext *s)
yading@11 160 {
yading@11 161 WebVTTContext *webvtt = s->priv_data;
yading@11 162 ff_subtitles_queue_clean(&webvtt->q);
yading@11 163 return 0;
yading@11 164 }
yading@11 165
yading@11 166 AVInputFormat ff_webvtt_demuxer = {
yading@11 167 .name = "webvtt",
yading@11 168 .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
yading@11 169 .priv_data_size = sizeof(WebVTTContext),
yading@11 170 .read_probe = webvtt_probe,
yading@11 171 .read_header = webvtt_read_header,
yading@11 172 .read_packet = webvtt_read_packet,
yading@11 173 .read_seek2 = webvtt_read_seek,
yading@11 174 .read_close = webvtt_read_close,
yading@11 175 .extensions = "vtt",
yading@11 176 };