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 "libavutil/mem.h"
31 #include "codec_internal.h"
32 #include "htmlsubtitles.h"
37 AVBPrint encoded_source
;
38 AVBPrint encoded_content
;
43 static int sami_paragraph_to_ass(AVCodecContext
*avctx
, const char *src
)
45 SAMIContext
*sami
= avctx
->priv_data
;
48 char *dupsrc
= av_strdup(src
);
50 AVBPrint
*dst_content
= &sami
->encoded_content
;
51 AVBPrint
*dst_source
= &sami
->encoded_source
;
54 return AVERROR(ENOMEM
);
56 av_bprint_clear(&sami
->encoded_content
);
57 av_bprint_clear(&sami
->content
);
58 av_bprint_clear(&sami
->encoded_source
);
61 int prev_chr_is_space
= 0;
62 AVBPrint
*dst
= &sami
->content
;
64 /* parse & extract paragraph tag */
65 p
= av_stristr(p
, "<P");
68 if (p
[2] != '>' && !av_isspace(p
[2])) { // avoid confusion with tags such as <PRE>
72 if (dst
->len
) // add a separator with the previous paragraph if there was one
73 av_bprintf(dst
, "\\N");
74 tag
= av_strtok(p
, ">", &saveptr
);
79 /* check if the current paragraph is the "source" (speaker name) */
80 if (av_stristr(tag
, "ID=Source") || av_stristr(tag
, "ID=\"Source\"")) {
85 /* if empty event -> skip subtitle */
86 while (av_isspace(*p
))
88 if (!strncmp(p
, " ", 6)) {
93 /* extract the text, stripping most of the tags */
96 if (!av_strncasecmp(p
, "<P", 2) && (p
[2] == '>' || av_isspace(p
[2])))
99 if (!av_strncasecmp(p
, "<BR", 3)) {
100 av_bprintf(dst
, "\\N");
102 while (*p
&& *p
!= '>')
111 av_bprint_chars(dst
, *p
, 1);
112 else if (!prev_chr_is_space
)
113 av_bprint_chars(dst
, ' ', 1);
114 prev_chr_is_space
= av_isspace(*p
);
119 av_bprint_clear(&sami
->full
);
120 if (sami
->source
.len
) {
121 ret
= ff_htmlmarkup_to_ass(avctx
, dst_source
, sami
->source
.str
);
124 av_bprintf(&sami
->full
, "{\\i1}%s{\\i0}\\N", sami
->encoded_source
.str
);
126 ret
= ff_htmlmarkup_to_ass(avctx
, dst_content
, sami
->content
.str
);
129 av_bprintf(&sami
->full
, "%s", sami
->encoded_content
.str
);
136 static int sami_decode_frame(AVCodecContext
*avctx
, AVSubtitle
*sub
,
137 int *got_sub_ptr
, const AVPacket
*avpkt
)
139 const char *ptr
= avpkt
->data
;
140 SAMIContext
*sami
= avctx
->priv_data
;
142 if (ptr
&& avpkt
->size
> 0) {
143 int ret
= sami_paragraph_to_ass(avctx
, ptr
);
146 // TODO: pass escaped sami->encoded_source.str as source
147 ret
= ff_ass_add_rect(sub
, sami
->full
.str
, sami
->readorder
++, 0, NULL
, NULL
);
151 *got_sub_ptr
= sub
->num_rects
> 0;
155 static av_cold
int sami_init(AVCodecContext
*avctx
)
157 SAMIContext
*sami
= avctx
->priv_data
;
158 av_bprint_init(&sami
->source
, 0, 2048);
159 av_bprint_init(&sami
->content
, 0, 2048);
160 av_bprint_init(&sami
->encoded_source
, 0, 2048);
161 av_bprint_init(&sami
->encoded_content
, 0, 2048);
162 av_bprint_init(&sami
->full
, 0, 2048);
163 return ff_ass_subtitle_header_default(avctx
);
166 static av_cold
int sami_close(AVCodecContext
*avctx
)
168 SAMIContext
*sami
= avctx
->priv_data
;
169 av_bprint_finalize(&sami
->source
, NULL
);
170 av_bprint_finalize(&sami
->content
, NULL
);
171 av_bprint_finalize(&sami
->encoded_source
, NULL
);
172 av_bprint_finalize(&sami
->encoded_content
, NULL
);
173 av_bprint_finalize(&sami
->full
, NULL
);
177 static void sami_flush(AVCodecContext
*avctx
)
179 SAMIContext
*sami
= avctx
->priv_data
;
180 if (!(avctx
->flags2
& AV_CODEC_FLAG2_RO_FLUSH_NOOP
))
184 const FFCodec ff_sami_decoder
= {
186 CODEC_LONG_NAME("SAMI subtitle"),
187 .p
.type
= AVMEDIA_TYPE_SUBTITLE
,
188 .p
.id
= AV_CODEC_ID_SAMI
,
189 .priv_data_size
= sizeof(SAMIContext
),
192 FF_CODEC_DECODE_SUB_CB(sami_decode_frame
),