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++) {
104 av_bprintf(&s->buffer, "<span region=\"");
105 av_bprint_escape(&s->buffer, dialog->style, NULL,
107 AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
108 av_bprintf(&s->buffer, "\">");
112 int ret = ff_ass_split_override_codes(&ttml_callbacks, s,
114 int log_level = (ret != AVERROR_INVALIDDATA ||
115 avctx->err_recognition & AV_EF_EXPLODE) ?
116 AV_LOG_ERROR : AV_LOG_WARNING;
119 av_log(avctx, log_level,
120 "Splitting received ASS dialog failed: %s\n",
123 if (log_level == AV_LOG_ERROR)
129 av_bprintf(&s->buffer, "</span>");
133 dialog = ff_ass_split_dialog2(s->ass_ctx, ass);
135 return AVERROR(ENOMEM);
138 av_bprintf(&s->buffer, "<span region=\"");
139 av_bprint_escape(&s->buffer, dialog->style, NULL,
141 AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
142 av_bprintf(&s->buffer, "\">");
146 int ret = ff_ass_split_override_codes(&ttml_callbacks, s,
148 int log_level = (ret != AVERROR_INVALIDDATA ||
149 avctx->err_recognition & AV_EF_EXPLODE) ?
150 AV_LOG_ERROR : AV_LOG_WARNING;
153 av_log(avctx, log_level,
154 "Splitting received ASS dialog text %s failed: %s\n",
158 if (log_level == AV_LOG_ERROR) {
159 ff_ass_free_dialog(&dialog);
165 av_bprintf(&s->buffer, "</span>");
167 ff_ass_free_dialog(&dialog);
169 #if FF_API_ASS_TIMING
174 if (!av_bprint_is_complete(&s->buffer))
175 return AVERROR(ENOMEM);
179 // force null-termination, so in case our destination buffer is
180 // too small, the return value is larger than bufsize minus null.
181 if (av_strlcpy(buf, s->buffer.str, bufsize) > bufsize - 1) {
182 av_log(avctx, AV_LOG_ERROR, "Buffer too small for TTML event.\n");
183 return AVERROR_BUFFER_TOO_SMALL;
186 return s->buffer.len;
189 static av_cold int ttml_encode_close(AVCodecContext *avctx)
191 TTMLContext *s = avctx->priv_data;
193 ff_ass_split_free(s->ass_ctx);
195 av_bprint_finalize(&s->buffer, NULL);
200 static const char *ttml_get_display_alignment(int alignment)
220 static const char *ttml_get_text_alignment(int alignment)
240 static int ttml_write_region(AVCodecContext *avctx, AVBPrint *buf,
243 const char *display_alignment = NULL;
244 const char *text_alignment = NULL;
247 av_log(avctx, AV_LOG_ERROR, "Subtitle style name not set!\n");
248 return AVERROR_INVALIDDATA;
251 if (style.font_size < 0) {
252 av_log(avctx, AV_LOG_ERROR, "Invalid font size for TTML: %d!\n",
254 return AVERROR_INVALIDDATA;
257 display_alignment = ttml_get_display_alignment(style.alignment);
258 text_alignment = ttml_get_text_alignment(style.alignment);
259 if (!display_alignment || !text_alignment) {
260 av_log(avctx, AV_LOG_ERROR,
261 "Failed to convert ASS style alignment %d of style %s to "
262 "TTML display and text alignment!\n",
265 return AVERROR_INVALIDDATA;
268 av_bprintf(buf, " <region xml:id=\"");
269 av_bprint_escape(buf, style.name, NULL, AV_ESCAPE_MODE_XML,
270 AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
271 av_bprintf(buf, "\"\n");
273 av_bprintf(buf, " tts:displayAlign=\"");
274 av_bprint_escape(buf, display_alignment, NULL, AV_ESCAPE_MODE_XML,
275 AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
276 av_bprintf(buf, "\"\n");
278 av_bprintf(buf, " tts:textAlign=\"");
279 av_bprint_escape(buf, text_alignment, NULL, AV_ESCAPE_MODE_XML,
280 AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
281 av_bprintf(buf, "\"\n");
283 // if we set cell resolution to our script reference resolution,
284 // then a single line is a single "point" on our canvas. Thus, by setting
285 // our font size to font size in cells, we should gain a similar enough
286 // scale without resorting to explicit pixel based font sizing, which is
287 // frowned upon in the TTML community.
288 av_bprintf(buf, " tts:fontSize=\"%dc\"\n",
291 if (style.font_name) {
292 av_bprintf(buf, " tts:fontFamily=\"");
293 av_bprint_escape(buf, style.font_name, NULL, AV_ESCAPE_MODE_XML,
294 AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
295 av_bprintf(buf, "\"\n");
298 av_bprintf(buf, " tts:overflow=\"visible\" />\n");
303 static int ttml_write_header_content(AVCodecContext *avctx)
305 TTMLContext *s = avctx->priv_data;
306 ASS *ass = (ASS *)s->ass_ctx;
307 ASSScriptInfo script_info = ass->script_info;
308 const size_t base_extradata_size = TTMLENC_EXTRADATA_SIGNATURE_SIZE + 1 +
309 AV_INPUT_BUFFER_PADDING_SIZE;
310 size_t additional_extradata_size = 0;
312 if (script_info.play_res_x <= 0 || script_info.play_res_y <= 0) {
313 av_log(avctx, AV_LOG_ERROR,
314 "Invalid subtitle reference resolution %dx%d!\n",
315 script_info.play_res_x, script_info.play_res_y);
316 return AVERROR_INVALIDDATA;
319 // write the first string in extradata, attributes in the base "tt" element.
320 av_bprintf(&s->buffer, ttml_default_namespacing);
321 // the cell resolution is in character cells, so not exactly 1:1 against
322 // a pixel based resolution, but as the tts:extent in the root
323 // "tt" element is frowned upon (and disallowed in the EBU-TT profile),
324 // we mimic the reference resolution by setting it as the cell resolution.
325 av_bprintf(&s->buffer, " ttp:cellResolution=\"%d %d\"\n",
326 script_info.play_res_x, script_info.play_res_y);
327 av_bprint_chars(&s->buffer, '\0', 1);
329 // write the second string in extradata, head element containing the styles
330 av_bprintf(&s->buffer, " <head>\n");
331 av_bprintf(&s->buffer, " <layout>\n");
333 for (int i = 0; i < ass->styles_count; i++) {
334 int ret = ttml_write_region(avctx, &s->buffer, ass->styles[i]);
339 av_bprintf(&s->buffer, " </layout>\n");
340 av_bprintf(&s->buffer, " </head>\n");
341 av_bprint_chars(&s->buffer, '\0', 1);
343 if (!av_bprint_is_complete(&s->buffer)) {
344 return AVERROR(ENOMEM);
347 additional_extradata_size = s->buffer.len;
349 if (!(avctx->extradata =
350 av_mallocz(base_extradata_size + additional_extradata_size))) {
351 return AVERROR(ENOMEM);
354 avctx->extradata_size =
355 TTMLENC_EXTRADATA_SIGNATURE_SIZE + additional_extradata_size;
356 memcpy(avctx->extradata, TTMLENC_EXTRADATA_SIGNATURE,
357 TTMLENC_EXTRADATA_SIGNATURE_SIZE);
359 if (additional_extradata_size)
360 memcpy(avctx->extradata + TTMLENC_EXTRADATA_SIGNATURE_SIZE,
361 s->buffer.str, additional_extradata_size);
363 av_bprint_clear(&s->buffer);
368 static av_cold int ttml_encode_init(AVCodecContext *avctx)
370 TTMLContext *s = avctx->priv_data;
371 int ret = AVERROR_BUG;
374 av_bprint_init(&s->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
376 if (!(s->ass_ctx = ff_ass_split(avctx->subtitle_header))) {
377 return AVERROR_INVALIDDATA;
380 if ((ret = ttml_write_header_content(avctx)) < 0) {
387 AVCodec ff_ttml_encoder = {
389 .long_name = NULL_IF_CONFIG_SMALL("TTML subtitle"),
390 .type = AVMEDIA_TYPE_SUBTITLE,
391 .id = AV_CODEC_ID_TTML,
392 .priv_data_size = sizeof(TTMLContext),
393 .init = ttml_encode_init,
394 .encode_sub = ttml_encode_frame,
395 .close = ttml_encode_close,
396 .capabilities = FF_CODEC_CAP_INIT_CLEANUP,