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