]> git.sesse.net Git - ffmpeg/blob - libavcodec/assdec.c
Kill timed SSA
[ffmpeg] / libavcodec / assdec.c
1 /*
2  * SSA/ASS decoder
3  * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <string.h>
23
24 #include "avcodec.h"
25 #include "ass.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/mem.h"
28
29 static av_cold int ass_decode_init(AVCodecContext *avctx)
30 {
31     avctx->subtitle_header = av_malloc(avctx->extradata_size + 1);
32     if (!avctx->subtitle_header)
33         return AVERROR(ENOMEM);
34     memcpy(avctx->subtitle_header, avctx->extradata, avctx->extradata_size);
35     avctx->subtitle_header[avctx->extradata_size] = 0;
36     avctx->subtitle_header_size = avctx->extradata_size;
37     return 0;
38 }
39
40 static int ass_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr,
41                             AVPacket *avpkt)
42 {
43     int ret;
44     AVSubtitle *sub = data;
45     const char *ptr = avpkt->data;
46     static const AVRational ass_tb = {1, 100};
47     const int ts_start    = av_rescale_q(avpkt->pts,      avctx->time_base, ass_tb);
48     const int ts_duration = av_rescale_q(avpkt->duration, avctx->time_base, ass_tb);
49
50     if (avpkt->size <= 0)
51         return avpkt->size;
52
53     ret = ff_ass_add_rect(sub, ptr, ts_start, ts_duration, 2);
54     if (ret < 0) {
55         if (ret == AVERROR_INVALIDDATA)
56             av_log(avctx, AV_LOG_ERROR, "Invalid ASS packet\n");
57         return ret;
58     }
59
60     *got_sub_ptr = avpkt->size > 0;
61     return avpkt->size;
62 }
63
64 #if CONFIG_SSA_DECODER
65 AVCodec ff_ssa_decoder = {
66     .name         = "ssa",
67     .long_name    = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
68     .type         = AVMEDIA_TYPE_SUBTITLE,
69     .id           = AV_CODEC_ID_ASS,
70     .init         = ass_decode_init,
71     .decode       = ass_decode_frame,
72 };
73 #endif
74
75 #if CONFIG_ASS_DECODER
76 AVCodec ff_ass_decoder = {
77     .name         = "ass",
78     .long_name    = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
79     .type         = AVMEDIA_TYPE_SUBTITLE,
80     .id           = AV_CODEC_ID_ASS,
81     .init         = ass_decode_init,
82     .decode       = ass_decode_frame,
83 };
84 #endif