]> git.sesse.net Git - ffmpeg/blob - libavcodec/assdec.c
Merge commit '7c278d2ae410a64bdd89f1777026b4b963c30a1a'
[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 "ass_split.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/mem.h"
29
30 static av_cold int ass_decode_init(AVCodecContext *avctx)
31 {
32     avctx->subtitle_header = av_malloc(avctx->extradata_size);
33     if (!avctx->subtitle_header)
34         return AVERROR(ENOMEM);
35     memcpy(avctx->subtitle_header, avctx->extradata, avctx->extradata_size);
36     avctx->subtitle_header_size = avctx->extradata_size;
37     avctx->priv_data = ff_ass_split(avctx->extradata);
38     if(!avctx->priv_data)
39         return -1;
40     return 0;
41 }
42
43 static int ass_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr,
44                             AVPacket *avpkt)
45 {
46     const char *ptr = avpkt->data;
47     int len, size = avpkt->size;
48
49     while (size > 0) {
50         int duration;
51         ASSDialog *dialog = ff_ass_split_dialog(avctx->priv_data, ptr, 0, NULL);
52         if (!dialog)
53             return AVERROR_INVALIDDATA;
54         duration = dialog->end - dialog->start;
55         len = ff_ass_add_rect(data, ptr, 0, duration, 1);
56         if (len < 0)
57             return len;
58         ptr  += len;
59         size -= len;
60     }
61
62     *got_sub_ptr = avpkt->size > 0;
63     return avpkt->size;
64 }
65
66 static int ass_decode_close(AVCodecContext *avctx)
67 {
68     ff_ass_split_free(avctx->priv_data);
69     avctx->priv_data = NULL;
70     return 0;
71 }
72
73 AVCodec ff_ass_decoder = {
74     .name         = "ass",
75     .long_name    = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
76     .type         = AVMEDIA_TYPE_SUBTITLE,
77     .id           = AV_CODEC_ID_SSA,
78     .init         = ass_decode_init,
79     .decode       = ass_decode_frame,
80     .close        = ass_decode_close,
81 };