annotate ffmpeg/libavcodec/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 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Copyright (c) 2012 Clément Bœsch
yading@10 3 *
yading@10 4 * This file is part of FFmpeg.
yading@10 5 *
yading@10 6 * FFmpeg is free software; you can redistribute it and/or
yading@10 7 * modify it under the terms of the GNU Lesser General Public
yading@10 8 * License as published by the Free Software Foundation; either
yading@10 9 * version 2.1 of the License, or (at your option) any later version.
yading@10 10 *
yading@10 11 * FFmpeg is distributed in the hope that it will be useful,
yading@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 14 * Lesser General Public License for more details.
yading@10 15 *
yading@10 16 * You should have received a copy of the GNU Lesser General Public
yading@10 17 * License along with FFmpeg; if not, write to the Free Software
yading@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 19 */
yading@10 20
yading@10 21 /**
yading@10 22 * @file
yading@10 23 * WebVTT subtitle decoder
yading@10 24 * @see http://dev.w3.org/html5/webvtt/
yading@10 25 * @todo need to support extended markups and cue settings
yading@10 26 */
yading@10 27
yading@10 28 #include "avcodec.h"
yading@10 29 #include "ass.h"
yading@10 30 #include "libavutil/bprint.h"
yading@10 31
yading@10 32 static const struct {
yading@10 33 const char *from;
yading@10 34 const char *to;
yading@10 35 } webvtt_tag_replace[] = {
yading@10 36 {"<i>", "{\\i1}"}, {"</i>", "{\\i0}"},
yading@10 37 {"<b>", "{\\b1}"}, {"</b>", "{\\b0}"},
yading@10 38 {"<u>", "{\\u1}"}, {"</u>", "{\\u0}"},
yading@10 39 {"{", "\\{"}, {"}", "\\}"}, // escape to avoid ASS markup conflicts
yading@10 40 };
yading@10 41
yading@10 42 static int webvtt_event_to_ass(AVBPrint *buf, const char *p)
yading@10 43 {
yading@10 44 int i, skip = 0;
yading@10 45
yading@10 46 while (*p) {
yading@10 47
yading@10 48 for (i = 0; i < FF_ARRAY_ELEMS(webvtt_tag_replace); i++) {
yading@10 49 const char *from = webvtt_tag_replace[i].from;
yading@10 50 const size_t len = strlen(from);
yading@10 51 if (!strncmp(p, from, len)) {
yading@10 52 av_bprintf(buf, "%s", webvtt_tag_replace[i].to);
yading@10 53 p += len;
yading@10 54 break;
yading@10 55 }
yading@10 56 }
yading@10 57 if (!*p)
yading@10 58 break;
yading@10 59
yading@10 60 if (*p == '<')
yading@10 61 skip = 1;
yading@10 62 else if (*p == '>')
yading@10 63 skip = 0;
yading@10 64 else if (p[0] == '\n' && p[1])
yading@10 65 av_bprintf(buf, "\\N");
yading@10 66 else if (!skip && *p != '\r')
yading@10 67 av_bprint_chars(buf, *p, 1);
yading@10 68 p++;
yading@10 69 }
yading@10 70 av_bprintf(buf, "\r\n");
yading@10 71 return 0;
yading@10 72 }
yading@10 73
yading@10 74 static int webvtt_decode_frame(AVCodecContext *avctx,
yading@10 75 void *data, int *got_sub_ptr, AVPacket *avpkt)
yading@10 76 {
yading@10 77 AVSubtitle *sub = data;
yading@10 78 const char *ptr = avpkt->data;
yading@10 79 AVBPrint buf;
yading@10 80
yading@10 81 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
yading@10 82 if (ptr && avpkt->size > 0 && !webvtt_event_to_ass(&buf, ptr)) {
yading@10 83 int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
yading@10 84 int ts_duration = avpkt->duration != -1 ?
yading@10 85 av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
yading@10 86 ff_ass_add_rect(sub, buf.str, ts_start, ts_duration, 0);
yading@10 87 }
yading@10 88 *got_sub_ptr = sub->num_rects > 0;
yading@10 89 av_bprint_finalize(&buf, NULL);
yading@10 90 return avpkt->size;
yading@10 91 }
yading@10 92
yading@10 93 AVCodec ff_webvtt_decoder = {
yading@10 94 .name = "webvtt",
yading@10 95 .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
yading@10 96 .type = AVMEDIA_TYPE_SUBTITLE,
yading@10 97 .id = AV_CODEC_ID_WEBVTT,
yading@10 98 .decode = webvtt_decode_frame,
yading@10 99 .init = ff_ass_subtitle_header_default,
yading@10 100 };