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: * SAMI subtitle decoder yading@10: * @see http://msdn.microsoft.com/en-us/library/ms971327.aspx yading@10: */ yading@10: yading@10: #include "ass.h" yading@10: #include "libavutil/avstring.h" yading@10: #include "libavutil/bprint.h" yading@10: yading@10: typedef struct { yading@10: AVBPrint source; yading@10: AVBPrint content; yading@10: AVBPrint full; yading@10: } SAMIContext; yading@10: yading@10: static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src) yading@10: { yading@10: SAMIContext *sami = avctx->priv_data; yading@10: int ret = 0; yading@10: char *tag = NULL; yading@10: char *dupsrc = av_strdup(src); yading@10: char *p = dupsrc; yading@10: yading@10: av_bprint_clear(&sami->content); yading@10: for (;;) { yading@10: char *saveptr = NULL; yading@10: int prev_chr_is_space = 0; yading@10: AVBPrint *dst = &sami->content; yading@10: yading@10: /* parse & extract paragraph tag */ yading@10: p = av_stristr(p, "
' && !av_isspace(p[2])) { // avoid confusion with tags such as
yading@10: p++; yading@10: continue; yading@10: } yading@10: if (dst->len) // add a separator with the previous paragraph if there was one yading@10: av_bprintf(dst, "\\N"); yading@10: tag = av_strtok(p, ">", &saveptr); yading@10: if (!tag || !saveptr) yading@10: break; yading@10: p = saveptr; yading@10: yading@10: /* check if the current paragraph is the "source" (speaker name) */ yading@10: if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) { yading@10: dst = &sami->source; yading@10: av_bprint_clear(dst); yading@10: } yading@10: yading@10: /* if empty event -> skip subtitle */ yading@10: while (av_isspace(*p)) yading@10: p++; yading@10: if (!strncmp(p, " ", 6)) { yading@10: ret = -1; yading@10: goto end; yading@10: } yading@10: yading@10: /* extract the text, stripping most of the tags */ yading@10: while (*p) { yading@10: if (*p == '<') { yading@10: if (!av_strncasecmp(p, "' || av_isspace(p[2]))) yading@10: break; yading@10: if (!av_strncasecmp(p, "
') yading@10: p++; yading@10: if (!*p) yading@10: break; yading@10: if (*p == '>') yading@10: p++; yading@10: } yading@10: if (!av_isspace(*p)) yading@10: av_bprint_chars(dst, *p, 1); yading@10: else if (!prev_chr_is_space) yading@10: av_bprint_chars(dst, ' ', 1); yading@10: prev_chr_is_space = av_isspace(*p); yading@10: p++; yading@10: } yading@10: } yading@10: yading@10: av_bprint_clear(&sami->full); yading@10: if (sami->source.len) yading@10: av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->source.str); yading@10: av_bprintf(&sami->full, "%s\r\n", sami->content.str); yading@10: yading@10: end: yading@10: av_free(dupsrc); yading@10: return ret; yading@10: } yading@10: yading@10: static int sami_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: SAMIContext *sami = avctx->priv_data; yading@10: yading@10: if (ptr && avpkt->size > 0 && !sami_paragraph_to_ass(avctx, 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, sami->full.str, ts_start, ts_duration, 0); yading@10: } yading@10: *got_sub_ptr = sub->num_rects > 0; yading@10: return avpkt->size; yading@10: } yading@10: yading@10: static av_cold int sami_init(AVCodecContext *avctx) yading@10: { yading@10: SAMIContext *sami = avctx->priv_data; yading@10: av_bprint_init(&sami->source, 0, 2048); yading@10: av_bprint_init(&sami->content, 0, 2048); yading@10: av_bprint_init(&sami->full, 0, 2048); yading@10: return ff_ass_subtitle_header_default(avctx); yading@10: } yading@10: yading@10: static av_cold int sami_close(AVCodecContext *avctx) yading@10: { yading@10: SAMIContext *sami = avctx->priv_data; yading@10: av_bprint_finalize(&sami->source, NULL); yading@10: av_bprint_finalize(&sami->content, NULL); yading@10: av_bprint_finalize(&sami->full, NULL); yading@10: return 0; yading@10: } yading@10: yading@10: AVCodec ff_sami_decoder = { yading@10: .name = "sami", yading@10: .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle"), yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_SAMI, yading@10: .priv_data_size = sizeof(SAMIContext), yading@10: .init = sami_init, yading@10: .close = sami_close, yading@10: .decode = sami_decode_frame, yading@10: };