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: * RealText subtitle decoder
yading@10: * @see http://service.real.com/help/library/guides/ProductionGuide/prodguide/htmfiles/realtext.htm
yading@10: */
yading@10:
yading@10: #include "avcodec.h"
yading@10: #include "ass.h"
yading@10: #include "libavutil/avstring.h"
yading@10: #include "libavutil/bprint.h"
yading@10:
yading@10: static int rt_event_to_ass(AVBPrint *buf, const char *p)
yading@10: {
yading@10: int prev_chr_is_space = 1;
yading@10:
yading@10: while (*p) {
yading@10: if (*p != '<') {
yading@10: if (!av_isspace(*p))
yading@10: av_bprint_chars(buf, *p, 1);
yading@10: else if (!prev_chr_is_space)
yading@10: av_bprint_chars(buf, ' ', 1);
yading@10: prev_chr_is_space = av_isspace(*p);
yading@10: } else {
yading@10: const char *end = strchr(p, '>');
yading@10: if (!end)
yading@10: break;
yading@10: if (!av_strncasecmp(p, "
", 5) ||
yading@10: !av_strncasecmp(p, "
", 4)) {
yading@10: av_bprintf(buf, "\\N");
yading@10: }
yading@10: p = end;
yading@10: }
yading@10: p++;
yading@10: }
yading@10: av_bprintf(buf, "\r\n");
yading@10: return 0;
yading@10: }
yading@10:
yading@10: static int realtext_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, 4096);
yading@10: // note: no need to rescale pts & duration since they are in the same
yading@10: // timebase as ASS (1/100)
yading@10: if (ptr && avpkt->size > 0 && !rt_event_to_ass(&buf, ptr))
yading@10: ff_ass_add_rect(sub, buf.str, avpkt->pts, avpkt->duration, 0);
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_realtext_decoder = {
yading@10: .name = "realtext",
yading@10: .long_name = NULL_IF_CONFIG_SMALL("RealText subtitle"),
yading@10: .type = AVMEDIA_TYPE_SUBTITLE,
yading@10: .id = AV_CODEC_ID_REALTEXT,
yading@10: .decode = realtext_decode_frame,
yading@10: .init = ff_ass_subtitle_header_default,
yading@10: };