yading@10: /* yading@10: * Copyright (c) 2012 Clément Bœsch yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * Raw subtitles decoder yading@10: */ yading@10: yading@10: #include "avcodec.h" yading@10: #include "ass.h" yading@10: #include "libavutil/bprint.h" yading@10: #include "libavutil/opt.h" yading@10: yading@10: typedef struct { yading@10: AVClass *class; yading@10: const char *linebreaks; yading@10: int keep_ass_markup; yading@10: } TextContext; yading@10: yading@10: #define OFFSET(x) offsetof(TextContext, x) yading@10: #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM yading@10: static const AVOption options[] = { yading@10: { "keep_ass_markup", "Set if ASS tags must be escaped", OFFSET(keep_ass_markup), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags=SD }, yading@10: { NULL } yading@10: }; yading@10: yading@10: static int text_event_to_ass(const AVCodecContext *avctx, AVBPrint *buf, yading@10: const char *p, const char *p_end) yading@10: { yading@10: const TextContext *text = avctx->priv_data; yading@10: yading@10: for (; p < p_end && *p; p++) { yading@10: yading@10: /* forced custom line breaks, not accounted as "normal" EOL */ yading@10: if (text->linebreaks && strchr(text->linebreaks, *p)) { yading@10: av_bprintf(buf, "\\N"); yading@10: yading@10: /* standard ASS escaping so random characters don't get mis-interpreted yading@10: * as ASS */ yading@10: } else if (!text->keep_ass_markup && strchr("{}\\", *p)) { yading@10: av_bprintf(buf, "\\%c", *p); yading@10: yading@10: /* some packets might end abruptly (no \0 at the end, like for example yading@10: * in some cases of demuxing from a classic video container), some yading@10: * might be terminated with \n or \r\n which we have to remove (for yading@10: * consistency with those who haven't), and we also have to deal with yading@10: * evil cases such as \r at the end of the buffer (and no \0 terminated yading@10: * character) */ yading@10: } else if (p[0] == '\n') { yading@10: /* some stuff left so we can insert a line break */ yading@10: if (p < p_end - 1) yading@10: av_bprintf(buf, "\\N"); yading@10: } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') { yading@10: /* \r followed by a \n, we can skip it. We don't insert the \N yet yading@10: * because we don't know if it is followed by more text */ yading@10: continue; yading@10: yading@10: /* finally, a sane character */ yading@10: } else { yading@10: av_bprint_chars(buf, *p, 1); yading@10: } yading@10: } yading@10: av_bprintf(buf, "\r\n"); yading@10: return 0; yading@10: } yading@10: yading@10: static int text_decode_frame(AVCodecContext *avctx, void *data, yading@10: int *got_sub_ptr, AVPacket *avpkt) yading@10: { yading@10: AVBPrint buf; yading@10: AVSubtitle *sub = data; yading@10: const char *ptr = avpkt->data; yading@10: const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100}); yading@10: const int ts_duration = avpkt->duration != -1 ? yading@10: av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1; yading@10: yading@10: av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); yading@10: if (ptr && avpkt->size > 0 && *ptr && yading@10: !text_event_to_ass(avctx, &buf, ptr, ptr + avpkt->size)) { yading@10: if (!av_bprint_is_complete(&buf)) { yading@10: av_bprint_finalize(&buf, NULL); yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: ff_ass_add_rect(sub, buf.str, ts_start, ts_duration, 0); yading@10: } yading@10: *got_sub_ptr = sub->num_rects > 0; yading@10: av_bprint_finalize(&buf, NULL); yading@10: return avpkt->size; yading@10: } yading@10: yading@10: #define DECLARE_CLASS(decname) static const AVClass decname ## _decoder_class = { \ yading@10: .class_name = #decname " decoder", \ yading@10: .item_name = av_default_item_name, \ yading@10: .option = decname ## _options, \ yading@10: .version = LIBAVUTIL_VERSION_INT, \ yading@10: } yading@10: yading@10: #if CONFIG_TEXT_DECODER yading@10: #define text_options options yading@10: DECLARE_CLASS(text); yading@10: yading@10: AVCodec ff_text_decoder = { yading@10: .name = "text", yading@10: .priv_data_size = sizeof(TextContext), yading@10: .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"), yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_TEXT, yading@10: .decode = text_decode_frame, yading@10: .init = ff_ass_subtitle_header_default, yading@10: .priv_class = &text_decoder_class, yading@10: }; yading@10: #endif yading@10: yading@10: #if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER yading@10: yading@10: static int linebreak_init(AVCodecContext *avctx) yading@10: { yading@10: TextContext *text = avctx->priv_data; yading@10: text->linebreaks = "|"; yading@10: return ff_ass_subtitle_header_default(avctx); yading@10: } yading@10: yading@10: #if CONFIG_VPLAYER_DECODER yading@10: #define vplayer_options options yading@10: DECLARE_CLASS(vplayer); yading@10: yading@10: AVCodec ff_vplayer_decoder = { yading@10: .name = "vplayer", yading@10: .priv_data_size = sizeof(TextContext), yading@10: .long_name = NULL_IF_CONFIG_SMALL("VPlayer subtitle"), yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_VPLAYER, yading@10: .decode = text_decode_frame, yading@10: .init = linebreak_init, yading@10: .priv_class = &vplayer_decoder_class, yading@10: }; yading@10: #endif yading@10: yading@10: #if CONFIG_PJS_DECODER yading@10: #define pjs_options options yading@10: DECLARE_CLASS(pjs); yading@10: yading@10: AVCodec ff_pjs_decoder = { yading@10: .name = "pjs", yading@10: .priv_data_size = sizeof(TextContext), yading@10: .long_name = NULL_IF_CONFIG_SMALL("PJS subtitle"), yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_PJS, yading@10: .decode = text_decode_frame, yading@10: .init = linebreak_init, yading@10: .priv_class = &pjs_decoder_class, yading@10: }; yading@10: #endif yading@10: yading@10: #if CONFIG_SUBVIEWER1_DECODER yading@10: #define subviewer1_options options yading@10: DECLARE_CLASS(subviewer1); yading@10: yading@10: AVCodec ff_subviewer1_decoder = { yading@10: .name = "subviewer1", yading@10: .priv_data_size = sizeof(TextContext), yading@10: .long_name = NULL_IF_CONFIG_SMALL("SubViewer1 subtitle"), yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_SUBVIEWER1, yading@10: .decode = text_decode_frame, yading@10: .init = linebreak_init, yading@10: .priv_class = &subviewer1_decoder_class, yading@10: }; yading@10: #endif yading@10: yading@10: #endif /* text subtitles with '|' line break */