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: * SubViewer subtitle decoder yading@10: * @see https://en.wikipedia.org/wiki/SubViewer yading@10: */ yading@10: yading@10: #include "avcodec.h" yading@10: #include "ass.h" yading@10: #include "libavutil/bprint.h" yading@10: yading@10: static int subviewer_event_to_ass(AVBPrint *buf, const char *p) yading@10: { yading@10: while (*p) { yading@10: if (!strncmp(p, "[br]", 4)) { yading@10: av_bprintf(buf, "\\N"); yading@10: p += 4; yading@10: } else { yading@10: if (p[0] == '\n' && p[1]) yading@10: av_bprintf(buf, "\\N"); yading@10: else if (*p != '\n' && *p != '\r') yading@10: av_bprint_chars(buf, *p, 1); yading@10: p++; yading@10: } yading@10: } yading@10: yading@10: av_bprintf(buf, "\r\n"); yading@10: return 0; yading@10: } yading@10: yading@10: static int subviewer_decode_frame(AVCodecContext *avctx, yading@10: void *data, int *got_sub_ptr, AVPacket *avpkt) yading@10: { yading@10: char c; yading@10: AVSubtitle *sub = data; yading@10: const char *ptr = avpkt->data; yading@10: AVBPrint buf; yading@10: yading@10: /* To be removed later */ yading@10: if (ptr && sscanf(ptr, "%*u:%*u:%*u.%*u,%*u:%*u:%*u.%*u%c", &c) == 1) { yading@10: av_log(avctx, AV_LOG_ERROR, "AVPacket is not clean (contains timing " yading@10: "information). You need to upgrade your libavformat or " yading@10: "sanitize your packet.\n"); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: yading@10: av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); yading@10: // note: no need to rescale pts & duration since they are in the same yading@10: // timebase as ASS (1/100) yading@10: if (ptr && avpkt->size > 0 && !subviewer_event_to_ass(&buf, ptr)) yading@10: ff_ass_add_rect(sub, buf.str, avpkt->pts, avpkt->duration, 0); 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: AVCodec ff_subviewer_decoder = { yading@10: .name = "subviewer", yading@10: .long_name = NULL_IF_CONFIG_SMALL("SubViewer subtitle"), yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_SUBVIEWER, yading@10: .decode = subviewer_decode_frame, yading@10: .init = ff_ass_subtitle_header_default, yading@10: };