yading@10: /* yading@10: * SSA/ASS decoder yading@10: * Copyright (c) 2010 Aurelien Jacobs 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: #include yading@10: yading@10: #include "avcodec.h" yading@10: #include "ass.h" yading@10: #include "ass_split.h" yading@10: #include "libavutil/internal.h" yading@10: #include "libavutil/mem.h" yading@10: yading@10: static av_cold int ass_decode_init(AVCodecContext *avctx) yading@10: { yading@10: avctx->subtitle_header = av_malloc(avctx->extradata_size + 1); yading@10: if (!avctx->subtitle_header) yading@10: return AVERROR(ENOMEM); yading@10: memcpy(avctx->subtitle_header, avctx->extradata, avctx->extradata_size); yading@10: avctx->subtitle_header[avctx->extradata_size] = 0; yading@10: avctx->subtitle_header_size = avctx->extradata_size; yading@10: avctx->priv_data = ff_ass_split(avctx->extradata); yading@10: if(!avctx->priv_data) yading@10: return -1; yading@10: return 0; yading@10: } yading@10: yading@10: static int ass_decode_close(AVCodecContext *avctx) yading@10: { yading@10: ff_ass_split_free(avctx->priv_data); yading@10: avctx->priv_data = NULL; yading@10: return 0; yading@10: } yading@10: yading@10: #if CONFIG_SSA_DECODER yading@10: static int ssa_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr, yading@10: AVPacket *avpkt) yading@10: { yading@10: const char *ptr = avpkt->data; yading@10: int len, size = avpkt->size; yading@10: yading@10: while (size > 0) { yading@10: int duration; yading@10: ASSDialog *dialog = ff_ass_split_dialog(avctx->priv_data, ptr, 0, NULL); yading@10: if (!dialog) yading@10: return AVERROR_INVALIDDATA; yading@10: duration = dialog->end - dialog->start; yading@10: len = ff_ass_add_rect(data, ptr, 0, duration, 1); yading@10: if (len < 0) yading@10: return len; yading@10: ptr += len; yading@10: size -= len; yading@10: } yading@10: yading@10: *got_sub_ptr = avpkt->size > 0; yading@10: return avpkt->size; yading@10: } yading@10: yading@10: AVCodec ff_ssa_decoder = { yading@10: .name = "ssa", yading@10: .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"), yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_SSA, yading@10: .init = ass_decode_init, yading@10: .decode = ssa_decode_frame, yading@10: .close = ass_decode_close, yading@10: }; yading@10: #endif yading@10: yading@10: #if CONFIG_ASS_DECODER yading@10: static int ass_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr, yading@10: AVPacket *avpkt) yading@10: { yading@10: int ret; yading@10: AVSubtitle *sub = data; yading@10: const char *ptr = avpkt->data; yading@10: static const AVRational ass_tb = {1, 100}; yading@10: const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, ass_tb); yading@10: const int ts_duration = av_rescale_q(avpkt->duration, avctx->time_base, ass_tb); yading@10: yading@10: if (avpkt->size <= 0) yading@10: return avpkt->size; yading@10: yading@10: ret = ff_ass_add_rect(sub, ptr, ts_start, ts_duration, 2); yading@10: if (ret < 0) { yading@10: if (ret == AVERROR_INVALIDDATA) yading@10: av_log(avctx, AV_LOG_ERROR, "Invalid ASS packet\n"); yading@10: return ret; yading@10: } yading@10: yading@10: *got_sub_ptr = avpkt->size > 0; yading@10: return avpkt->size; yading@10: } yading@10: yading@10: AVCodec ff_ass_decoder = { yading@10: .name = "ass", yading@10: .long_name = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"), yading@10: .type = AVMEDIA_TYPE_SUBTITLE, yading@10: .id = AV_CODEC_ID_ASS, yading@10: .init = ass_decode_init, yading@10: .decode = ass_decode_frame, yading@10: .close = ass_decode_close, yading@10: }; yading@10: #endif