2 * TTML subtitle encoder
3 * Copyright (c) 2020 24i
5 * This file is part of FFmpeg.
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.
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.
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
24 * TTML subtitle encoder
25 * @see https://www.w3.org/TR/ttml1/
26 * @see https://www.w3.org/TR/ttml2/
27 * @see https://www.w3.org/TR/ttml-imsc/rec
32 #include "libavutil/avstring.h"
33 #include "libavutil/bprint.h"
34 #include "libavutil/internal.h"
35 #include "ass_split.h"
40 AVCodecContext *avctx;
41 ASSSplitContext *ass_ctx;
45 static void ttml_text_cb(void *priv, const char *text, int len)
47 TTMLContext *s = priv;
48 AVBPrint cur_line = { 0 };
49 AVBPrint *buffer = &s->buffer;
51 av_bprint_init(&cur_line, len, AV_BPRINT_SIZE_UNLIMITED);
53 av_bprint_append_data(&cur_line, text, len);
54 if (!av_bprint_is_complete(&cur_line)) {
55 av_log(s->avctx, AV_LOG_ERROR,
56 "Failed to move the current subtitle dialog to AVBPrint!\n");
57 av_bprint_finalize(&cur_line, NULL);
62 av_bprint_escape(buffer, cur_line.str, NULL, AV_ESCAPE_MODE_XML,
65 av_bprint_finalize(&cur_line, NULL);
68 static void ttml_new_line_cb(void *priv, int forced)
70 TTMLContext *s = priv;
72 av_bprintf(&s->buffer, "<br/>");
75 static const ASSCodesCallbacks ttml_callbacks = {
77 .new_line = ttml_new_line_cb,
80 static int ttml_encode_frame(AVCodecContext *avctx, uint8_t *buf,
81 int bufsize, const AVSubtitle *sub)
83 TTMLContext *s = avctx->priv_data;
87 av_bprint_clear(&s->buffer);
89 for (i=0; i<sub->num_rects; i++) {
90 const char *ass = sub->rects[i]->ass;
92 if (sub->rects[i]->type != SUBTITLE_ASS) {
93 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
94 return AVERROR(EINVAL);
98 if (!strncmp(ass, "Dialogue: ", 10)) {
100 dialog = ff_ass_split_dialog(s->ass_ctx, ass, 0, &num);
102 for (; dialog && num--; dialog++) {
103 int ret = ff_ass_split_override_codes(&ttml_callbacks, s,
105 int log_level = (ret != AVERROR_INVALIDDATA ||
106 avctx->err_recognition & AV_EF_EXPLODE) ?
107 AV_LOG_ERROR : AV_LOG_WARNING;
110 av_log(avctx, log_level,
111 "Splitting received ASS dialog failed: %s\n",
114 if (log_level == AV_LOG_ERROR)
120 dialog = ff_ass_split_dialog2(s->ass_ctx, ass);
122 return AVERROR(ENOMEM);
125 int ret = ff_ass_split_override_codes(&ttml_callbacks, s,
127 int log_level = (ret != AVERROR_INVALIDDATA ||
128 avctx->err_recognition & AV_EF_EXPLODE) ?
129 AV_LOG_ERROR : AV_LOG_WARNING;
132 av_log(avctx, log_level,
133 "Splitting received ASS dialog text %s failed: %s\n",
137 if (log_level == AV_LOG_ERROR) {
138 ff_ass_free_dialog(&dialog);
143 ff_ass_free_dialog(&dialog);
145 #if FF_API_ASS_TIMING
150 if (!av_bprint_is_complete(&s->buffer))
151 return AVERROR(ENOMEM);
155 // force null-termination, so in case our destination buffer is
156 // too small, the return value is larger than bufsize minus null.
157 if (av_strlcpy(buf, s->buffer.str, bufsize) > bufsize - 1) {
158 av_log(avctx, AV_LOG_ERROR, "Buffer too small for TTML event.\n");
159 return AVERROR_BUFFER_TOO_SMALL;
162 return s->buffer.len;
165 static av_cold int ttml_encode_close(AVCodecContext *avctx)
167 TTMLContext *s = avctx->priv_data;
169 ff_ass_split_free(s->ass_ctx);
171 av_bprint_finalize(&s->buffer, NULL);
176 static av_cold int ttml_encode_init(AVCodecContext *avctx)
178 TTMLContext *s = avctx->priv_data;
182 if (!(s->ass_ctx = ff_ass_split(avctx->subtitle_header))) {
183 return AVERROR_INVALIDDATA;
186 if (!(avctx->extradata = av_mallocz(TTMLENC_EXTRADATA_SIGNATURE_SIZE +
187 1 + AV_INPUT_BUFFER_PADDING_SIZE))) {
188 return AVERROR(ENOMEM);
191 avctx->extradata_size = TTMLENC_EXTRADATA_SIGNATURE_SIZE;
192 memcpy(avctx->extradata, TTMLENC_EXTRADATA_SIGNATURE,
193 TTMLENC_EXTRADATA_SIGNATURE_SIZE);
195 av_bprint_init(&s->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
200 AVCodec ff_ttml_encoder = {
202 .long_name = NULL_IF_CONFIG_SMALL("TTML subtitle"),
203 .type = AVMEDIA_TYPE_SUBTITLE,
204 .id = AV_CODEC_ID_TTML,
205 .priv_data_size = sizeof(TTMLContext),
206 .init = ttml_encode_init,
207 .encode_sub = ttml_encode_frame,
208 .close = ttml_encode_close,
209 .capabilities = FF_CODEC_CAP_INIT_CLEANUP,