yading@10: /*
yading@10: * Copyright (c) 2012 Clément Bœsch
yading@10: *
yading@10: * This file is part of FFmpeg.
yading@10: *
yading@10: * FFmpeg is free software; you can redistribute it and/or
yading@10: * modify it under the terms of the GNU Lesser General Public
yading@10: * License as published by the Free Software Foundation; either
yading@10: * version 2.1 of the License, or (at your option) any later version.
yading@10: *
yading@10: * FFmpeg is distributed in the hope that it will be useful,
yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10: * Lesser General Public License for more details.
yading@10: *
yading@10: * You should have received a copy of the GNU Lesser General Public
yading@10: * License along with FFmpeg; if not, write to the Free Software
yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10: */
yading@10:
yading@10: /**
yading@10: * @file
yading@10: * WebVTT subtitle decoder
yading@10: * @see http://dev.w3.org/html5/webvtt/
yading@10: * @todo need to support extended markups and cue settings
yading@10: */
yading@10:
yading@10: #include "avcodec.h"
yading@10: #include "ass.h"
yading@10: #include "libavutil/bprint.h"
yading@10:
yading@10: static const struct {
yading@10: const char *from;
yading@10: const char *to;
yading@10: } webvtt_tag_replace[] = {
yading@10: {"", "{\\i1}"}, {"", "{\\i0}"},
yading@10: {"", "{\\b1}"}, {"", "{\\b0}"},
yading@10: {"", "{\\u1}"}, {"", "{\\u0}"},
yading@10: {"{", "\\{"}, {"}", "\\}"}, // escape to avoid ASS markup conflicts
yading@10: };
yading@10:
yading@10: static int webvtt_event_to_ass(AVBPrint *buf, const char *p)
yading@10: {
yading@10: int i, skip = 0;
yading@10:
yading@10: while (*p) {
yading@10:
yading@10: for (i = 0; i < FF_ARRAY_ELEMS(webvtt_tag_replace); i++) {
yading@10: const char *from = webvtt_tag_replace[i].from;
yading@10: const size_t len = strlen(from);
yading@10: if (!strncmp(p, from, len)) {
yading@10: av_bprintf(buf, "%s", webvtt_tag_replace[i].to);
yading@10: p += len;
yading@10: break;
yading@10: }
yading@10: }
yading@10: if (!*p)
yading@10: break;
yading@10:
yading@10: if (*p == '<')
yading@10: skip = 1;
yading@10: else if (*p == '>')
yading@10: skip = 0;
yading@10: else if (p[0] == '\n' && p[1])
yading@10: av_bprintf(buf, "\\N");
yading@10: else if (!skip && *p != '\r')
yading@10: av_bprint_chars(buf, *p, 1);
yading@10: p++;
yading@10: }
yading@10: av_bprintf(buf, "\r\n");
yading@10: return 0;
yading@10: }
yading@10:
yading@10: static int webvtt_decode_frame(AVCodecContext *avctx,
yading@10: void *data, int *got_sub_ptr, AVPacket *avpkt)
yading@10: {
yading@10: AVSubtitle *sub = data;
yading@10: const char *ptr = avpkt->data;
yading@10: AVBPrint buf;
yading@10:
yading@10: av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
yading@10: if (ptr && avpkt->size > 0 && !webvtt_event_to_ass(&buf, ptr)) {
yading@10: int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
yading@10: int ts_duration = avpkt->duration != -1 ?
yading@10: av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
yading@10: ff_ass_add_rect(sub, buf.str, ts_start, ts_duration, 0);
yading@10: }
yading@10: *got_sub_ptr = sub->num_rects > 0;
yading@10: av_bprint_finalize(&buf, NULL);
yading@10: return avpkt->size;
yading@10: }
yading@10:
yading@10: AVCodec ff_webvtt_decoder = {
yading@10: .name = "webvtt",
yading@10: .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
yading@10: .type = AVMEDIA_TYPE_SUBTITLE,
yading@10: .id = AV_CODEC_ID_WEBVTT,
yading@10: .decode = webvtt_decode_frame,
yading@10: .init = ff_ass_subtitle_header_default,
yading@10: };