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"
30 #include "htmlsubtitles.h"
35 AVBPrint encoded_source;
36 AVBPrint encoded_content;
41 static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
43 SAMIContext *sami = avctx->priv_data;
46 char *dupsrc = av_strdup(src);
48 AVBPrint *dst_content = &sami->encoded_content;
49 AVBPrint *dst_source = &sami->encoded_source;
51 av_bprint_clear(&sami->encoded_content);
52 av_bprint_clear(&sami->content);
53 av_bprint_clear(&sami->encoded_source);
56 int prev_chr_is_space = 0;
57 AVBPrint *dst = &sami->content;
59 /* parse & extract paragraph tag */
60 p = av_stristr(p, "<P");
63 if (p[2] != '>' && !av_isspace(p[2])) { // avoid confusion with tags such as <PRE>
67 if (dst->len) // add a separator with the previous paragraph if there was one
68 av_bprintf(dst, "\\N");
69 tag = av_strtok(p, ">", &saveptr);
74 /* check if the current paragraph is the "source" (speaker name) */
75 if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
80 /* if empty event -> skip subtitle */
81 while (av_isspace(*p))
83 if (!strncmp(p, " ", 6)) {
88 /* extract the text, stripping most of the tags */
91 if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
94 if (!av_strncasecmp(p, "<BR", 3)) {
95 av_bprintf(dst, "\\N");
97 while (*p && *p != '>')
106 av_bprint_chars(dst, *p, 1);
107 else if (!prev_chr_is_space)
108 av_bprint_chars(dst, ' ', 1);
109 prev_chr_is_space = av_isspace(*p);
114 av_bprint_clear(&sami->full);
115 if (sami->source.len) {
116 ff_htmlmarkup_to_ass(avctx, dst_source, sami->source.str);
117 av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->encoded_source.str);
119 ff_htmlmarkup_to_ass(avctx, dst_content, sami->content.str);
120 av_bprintf(&sami->full, "%s", sami->encoded_content.str);
127 static int sami_decode_frame(AVCodecContext *avctx,
128 void *data, int *got_sub_ptr, AVPacket *avpkt)
130 AVSubtitle *sub = data;
131 const char *ptr = avpkt->data;
132 SAMIContext *sami = avctx->priv_data;
134 if (ptr && avpkt->size > 0 && !sami_paragraph_to_ass(avctx, ptr)) {
135 // TODO: pass escaped sami->encoded_source.str as source
136 int ret = ff_ass_add_rect(sub, sami->full.str, sami->readorder++, 0, NULL, NULL);
140 *got_sub_ptr = sub->num_rects > 0;
144 static av_cold int sami_init(AVCodecContext *avctx)
146 SAMIContext *sami = avctx->priv_data;
147 av_bprint_init(&sami->source, 0, 2048);
148 av_bprint_init(&sami->content, 0, 2048);
149 av_bprint_init(&sami->encoded_source, 0, 2048);
150 av_bprint_init(&sami->encoded_content, 0, 2048);
151 av_bprint_init(&sami->full, 0, 2048);
152 return ff_ass_subtitle_header_default(avctx);
155 static av_cold int sami_close(AVCodecContext *avctx)
157 SAMIContext *sami = avctx->priv_data;
158 av_bprint_finalize(&sami->source, NULL);
159 av_bprint_finalize(&sami->content, NULL);
160 av_bprint_finalize(&sami->encoded_source, NULL);
161 av_bprint_finalize(&sami->encoded_content, NULL);
162 av_bprint_finalize(&sami->full, NULL);
166 static void sami_flush(AVCodecContext *avctx)
168 SAMIContext *sami = avctx->priv_data;
169 if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
173 AVCodec ff_sami_decoder = {
175 .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle"),
176 .type = AVMEDIA_TYPE_SUBTITLE,
177 .id = AV_CODEC_ID_SAMI,
178 .priv_data_size = sizeof(SAMIContext),
181 .decode = sami_decode_frame,