]> git.sesse.net Git - ffmpeg/blob - libavcodec/assenc.c
avformat/avio: Add Metacube support
[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 "ass.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/mem.h"
29
30 static av_cold int ass_encode_init(AVCodecContext *avctx)
31 {
32     avctx->extradata = av_malloc(avctx->subtitle_header_size + 1);
33     if (!avctx->extradata)
34         return AVERROR(ENOMEM);
35     memcpy(avctx->extradata, avctx->subtitle_header, avctx->subtitle_header_size);
36     avctx->extradata_size = avctx->subtitle_header_size;
37     avctx->extradata[avctx->extradata_size] = 0;
38     return 0;
39 }
40
41 static int ass_encode_frame(AVCodecContext *avctx,
42                             unsigned char *buf, int bufsize,
43                             const AVSubtitle *sub)
44 {
45     int i, len, total_len = 0;
46
47     for (i=0; i<sub->num_rects; i++) {
48         const char *ass = sub->rects[i]->ass;
49
50         if (sub->rects[i]->type != SUBTITLE_ASS) {
51             av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
52             return AVERROR(EINVAL);
53         }
54
55         len = av_strlcpy(buf+total_len, ass, bufsize-total_len);
56
57         if (len > bufsize-total_len-1) {
58             av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
59             return AVERROR_BUFFER_TOO_SMALL;
60         }
61
62         total_len += len;
63     }
64
65     return total_len;
66 }
67
68 #if CONFIG_SSA_ENCODER
69 const AVCodec ff_ssa_encoder = {
70     .name         = "ssa",
71     .long_name    = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
72     .type         = AVMEDIA_TYPE_SUBTITLE,
73     .id           = AV_CODEC_ID_ASS,
74     .init         = ass_encode_init,
75     .encode_sub   = ass_encode_frame,
76 };
77 #endif
78
79 #if CONFIG_ASS_ENCODER
80 const AVCodec ff_ass_encoder = {
81     .name         = "ass",
82     .long_name    = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
83     .type         = AVMEDIA_TYPE_SUBTITLE,
84     .id           = AV_CODEC_ID_ASS,
85     .init         = ass_encode_init,
86     .encode_sub   = ass_encode_frame,
87 };
88 #endif