]> git.sesse.net Git - ffmpeg/blob - libavcodec/assenc.c
Merge commit '42d324694883cdf1fff1612ac70fa403692a1ad4'
[ffmpeg] / libavcodec / assenc.c
1 /*
2  * SSA/ASS encoder
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 "libavutil/avstring.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/mem.h"
28
29 static av_cold int ass_encode_init(AVCodecContext *avctx)
30 {
31     avctx->extradata = av_malloc(avctx->subtitle_header_size + 1);
32     if (!avctx->extradata)
33         return AVERROR(ENOMEM);
34     memcpy(avctx->extradata, avctx->subtitle_header, avctx->subtitle_header_size);
35     avctx->extradata_size = avctx->subtitle_header_size;
36     avctx->extradata[avctx->extradata_size] = 0;
37     return 0;
38 }
39
40 static int ass_encode_frame(AVCodecContext *avctx,
41                             unsigned char *buf, int bufsize,
42                             const AVSubtitle *sub)
43 {
44     int i, len, total_len = 0;
45
46     for (i=0; i<sub->num_rects; i++) {
47         if (sub->rects[i]->type != SUBTITLE_ASS) {
48             av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
49             return -1;
50         }
51
52         len = av_strlcpy(buf+total_len, sub->rects[i]->ass, bufsize-total_len);
53
54         if (len > bufsize-total_len-1) {
55             av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
56             return -1;
57         }
58
59         total_len += len;
60     }
61
62     return total_len;
63 }
64
65 AVCodec ff_ass_encoder = {
66     .name         = "ass",
67     .long_name    = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
68     .type         = AVMEDIA_TYPE_SUBTITLE,
69     .id           = AV_CODEC_ID_SSA,
70     .init         = ass_encode_init,
71     .encode_sub   = ass_encode_frame,
72 };