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