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