2 * Copyright (c) 2012 Clément Bœsch
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * SAMI subtitle decoder
24 * @see http://msdn.microsoft.com/en-us/library/ms971327.aspx
28 #include "libavutil/avstring.h"
29 #include "libavutil/bprint.h"
37 static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
39 SAMIContext *sami = avctx->priv_data;
42 char *dupsrc = av_strdup(src);
45 av_bprint_clear(&sami->content);
48 int prev_chr_is_space = 0;
49 AVBPrint *dst = &sami->content;
51 /* parse & extract paragraph tag */
52 p = av_stristr(p, "<P");
55 if (p[2] != '>' && !av_isspace(p[2])) { // avoid confusion with tags such as <PRE>
59 if (dst->len) // add a separator with the previous paragraph if there was one
60 av_bprintf(dst, "\\N");
61 tag = av_strtok(p, ">", &saveptr);
66 /* check if the current paragraph is the "source" (speaker name) */
67 if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
72 /* if empty event -> skip subtitle */
73 while (av_isspace(*p))
75 if (!strncmp(p, " ", 6)) {
80 /* extract the text, stripping most of the tags */
83 if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
85 if (!av_strncasecmp(p, "<BR", 3))
86 av_bprintf(dst, "\\N");
88 while (*p && *p != '>')
97 av_bprint_chars(dst, *p, 1);
98 else if (!prev_chr_is_space)
99 av_bprint_chars(dst, ' ', 1);
100 prev_chr_is_space = av_isspace(*p);
105 av_bprint_clear(&sami->full);
106 if (sami->source.len)
107 av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->source.str);
108 av_bprintf(&sami->full, "%s", sami->content.str);
115 static int sami_decode_frame(AVCodecContext *avctx,
116 void *data, int *got_sub_ptr, AVPacket *avpkt)
118 AVSubtitle *sub = data;
119 const char *ptr = avpkt->data;
120 SAMIContext *sami = avctx->priv_data;
122 if (ptr && avpkt->size > 0 && !sami_paragraph_to_ass(avctx, ptr)) {
123 int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
124 int ts_duration = avpkt->duration != -1 ?
125 av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
126 int ret = ff_ass_add_rect_bprint(sub, &sami->full, ts_start, ts_duration);
130 *got_sub_ptr = sub->num_rects > 0;
134 static av_cold int sami_init(AVCodecContext *avctx)
136 SAMIContext *sami = avctx->priv_data;
137 av_bprint_init(&sami->source, 0, 2048);
138 av_bprint_init(&sami->content, 0, 2048);
139 av_bprint_init(&sami->full, 0, 2048);
140 return ff_ass_subtitle_header_default(avctx);
143 static av_cold int sami_close(AVCodecContext *avctx)
145 SAMIContext *sami = avctx->priv_data;
146 av_bprint_finalize(&sami->source, NULL);
147 av_bprint_finalize(&sami->content, NULL);
148 av_bprint_finalize(&sami->full, NULL);
152 AVCodec ff_sami_decoder = {
154 .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle"),
155 .type = AVMEDIA_TYPE_SUBTITLE,
156 .id = AV_CODEC_ID_SAMI,
157 .priv_data_size = sizeof(SAMIContext),
160 .decode = sami_decode_frame,