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 * Raw subtitles decoder
28 #include "libavutil/bprint.h"
29 #include "libavutil/opt.h"
33 const char *linebreaks;
37 #define OFFSET(x) offsetof(TextContext, x)
38 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
39 static const AVOption options[] = {
40 { "keep_ass_markup", "Set if ASS tags must be escaped", OFFSET(keep_ass_markup), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags=SD },
44 static int text_event_to_ass(const AVCodecContext *avctx, AVBPrint *buf,
45 const char *p, const char *p_end)
47 const TextContext *text = avctx->priv_data;
49 for (; p < p_end && *p; p++) {
51 /* forced custom line breaks, not accounted as "normal" EOL */
52 if (text->linebreaks && strchr(text->linebreaks, *p)) {
53 av_bprintf(buf, "\\N");
55 /* standard ASS escaping so random characters don't get mis-interpreted
57 } else if (!text->keep_ass_markup && strchr("{}\\", *p)) {
58 av_bprintf(buf, "\\%c", *p);
60 /* some packets might end abruptly (no \0 at the end, like for example
61 * in some cases of demuxing from a classic video container), some
62 * might be terminated with \n or \r\n which we have to remove (for
63 * consistency with those who haven't), and we also have to deal with
64 * evil cases such as \r at the end of the buffer (and no \0 terminated
66 } else if (p[0] == '\n') {
67 /* some stuff left so we can insert a line break */
69 av_bprintf(buf, "\\N");
70 } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
71 /* \r followed by a \n, we can skip it. We don't insert the \N yet
72 * because we don't know if it is followed by more text */
75 /* finally, a sane character */
77 av_bprint_chars(buf, *p, 1);
80 av_bprintf(buf, "\r\n");
84 static int text_decode_frame(AVCodecContext *avctx, void *data,
85 int *got_sub_ptr, AVPacket *avpkt)
88 AVSubtitle *sub = data;
89 const char *ptr = avpkt->data;
90 const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
91 const int ts_duration = avpkt->duration != -1 ?
92 av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
94 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
95 if (ptr && avpkt->size > 0 && *ptr &&
96 !text_event_to_ass(avctx, &buf, ptr, ptr + avpkt->size)) {
97 if (!av_bprint_is_complete(&buf)) {
98 av_bprint_finalize(&buf, NULL);
99 return AVERROR(ENOMEM);
101 ff_ass_add_rect(sub, buf.str, ts_start, ts_duration, 0);
103 *got_sub_ptr = sub->num_rects > 0;
104 av_bprint_finalize(&buf, NULL);
108 #define DECLARE_CLASS(decname) static const AVClass decname ## _decoder_class = { \
109 .class_name = #decname " decoder", \
110 .item_name = av_default_item_name, \
111 .option = decname ## _options, \
112 .version = LIBAVUTIL_VERSION_INT, \
115 #if CONFIG_TEXT_DECODER
116 #define text_options options
119 AVCodec ff_text_decoder = {
121 .priv_data_size = sizeof(TextContext),
122 .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
123 .type = AVMEDIA_TYPE_SUBTITLE,
124 .id = AV_CODEC_ID_TEXT,
125 .decode = text_decode_frame,
126 .init = ff_ass_subtitle_header_default,
127 .priv_class = &text_decoder_class,
131 #if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER
133 static int linebreak_init(AVCodecContext *avctx)
135 TextContext *text = avctx->priv_data;
136 text->linebreaks = "|";
137 return ff_ass_subtitle_header_default(avctx);
140 #if CONFIG_VPLAYER_DECODER
141 #define vplayer_options options
142 DECLARE_CLASS(vplayer);
144 AVCodec ff_vplayer_decoder = {
146 .priv_data_size = sizeof(TextContext),
147 .long_name = NULL_IF_CONFIG_SMALL("VPlayer subtitle"),
148 .type = AVMEDIA_TYPE_SUBTITLE,
149 .id = AV_CODEC_ID_VPLAYER,
150 .decode = text_decode_frame,
151 .init = linebreak_init,
152 .priv_class = &vplayer_decoder_class,
156 #if CONFIG_PJS_DECODER
157 #define pjs_options options
160 AVCodec ff_pjs_decoder = {
162 .priv_data_size = sizeof(TextContext),
163 .long_name = NULL_IF_CONFIG_SMALL("PJS subtitle"),
164 .type = AVMEDIA_TYPE_SUBTITLE,
165 .id = AV_CODEC_ID_PJS,
166 .decode = text_decode_frame,
167 .init = linebreak_init,
168 .priv_class = &pjs_decoder_class,
172 #if CONFIG_SUBVIEWER1_DECODER
173 #define subviewer1_options options
174 DECLARE_CLASS(subviewer1);
176 AVCodec ff_subviewer1_decoder = {
177 .name = "subviewer1",
178 .priv_data_size = sizeof(TextContext),
179 .long_name = NULL_IF_CONFIG_SMALL("SubViewer1 subtitle"),
180 .type = AVMEDIA_TYPE_SUBTITLE,
181 .id = AV_CODEC_ID_SUBVIEWER1,
182 .decode = text_decode_frame,
183 .init = linebreak_init,
184 .priv_class = &subviewer1_decoder_class,
188 #endif /* text subtitles with '|' line break */