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"
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 { "linebreaks", "Extra line breaks characters", OFFSET(linebreaks), AV_OPT_TYPE_STRING, {.str=NULL}, .flags=SD },
41 { "keep_ass_markup", "Set if ASS tags must be escaped", OFFSET(keep_ass_markup), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags=SD },
45 static const AVClass text_decoder_class = {
46 .class_name = "text decoder",
47 .item_name = av_default_item_name,
49 .version = LIBAVUTIL_VERSION_INT,
52 static int text_event_to_ass(const AVCodecContext *avctx, AVBPrint *buf,
53 const char *p, const char *p_end)
55 const TextContext *text = avctx->priv_data;
57 for (; p < p_end && *p; p++) {
59 /* forced custom line breaks, not accounted as "normal" EOL */
60 if (text->linebreaks && strchr(text->linebreaks, *p)) {
61 av_bprintf(buf, "\\N");
63 /* standard ASS escaping so random characters don't get mis-interpreted
65 } else if (!text->keep_ass_markup && strchr("{}\\", *p)) {
66 av_bprintf(buf, "\\%c", *p);
68 /* some packets might end abruptly (no \0 at the end, like for example
69 * in some cases of demuxing from a classic video container), some
70 * might be terminated with \n or \r\n which we have to remove (for
71 * consistency with those who haven't), and we also have to deal with
72 * evil cases such as \r at the end of the buffer (and no \0 terminated
74 } else if (p[0] == '\n') {
75 /* some stuff left so we can insert a line break */
77 av_bprintf(buf, "\\N");
78 } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
79 /* \r followed by a \n, we can skip it. We don't insert the \N yet
80 * because we don't know if it is followed by more text */
83 /* finally, a sane character */
85 av_bprint_chars(buf, *p, 1);
88 av_bprintf(buf, "\r\n");
92 static int text_decode_frame(AVCodecContext *avctx, void *data,
93 int *got_sub_ptr, AVPacket *avpkt)
96 AVSubtitle *sub = data;
97 const char *ptr = avpkt->data;
98 const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
99 const int ts_duration = avpkt->duration != -1 ?
100 av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
102 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
103 if (ptr && avpkt->size > 0 && *ptr &&
104 !text_event_to_ass(avctx, &buf, ptr, ptr + avpkt->size)) {
105 if (!av_bprint_is_complete(&buf)) {
106 av_bprint_finalize(&buf, NULL);
107 return AVERROR(ENOMEM);
109 ff_ass_add_rect(sub, buf.str, ts_start, ts_duration, 0);
111 *got_sub_ptr = sub->num_rects > 0;
112 av_bprint_finalize(&buf, NULL);
116 AVCodec ff_text_decoder = {
118 .priv_data_size = sizeof(TextContext),
119 .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
120 .type = AVMEDIA_TYPE_SUBTITLE,
121 .id = AV_CODEC_ID_TEXT,
122 .decode = text_decode_frame,
123 .init = ff_ass_subtitle_header_default,
124 .priv_class = &text_decoder_class,