| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * TTML subtitle encoder | ||
| 3 | * Copyright (c) 2020 24i | ||
| 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 | /** | ||
| 23 | * @file | ||
| 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 | ||
| 28 | */ | ||
| 29 | |||
| 30 | #include "avcodec.h" | ||
| 31 | #include "codec_internal.h" | ||
| 32 | #include "libavutil/bprint.h" | ||
| 33 | #include "libavutil/internal.h" | ||
| 34 | #include "libavutil/mem.h" | ||
| 35 | #include "ass_split.h" | ||
| 36 | #include "ttmlenc.h" | ||
| 37 | |||
| 38 | typedef struct { | ||
| 39 | AVCodecContext *avctx; | ||
| 40 | ASSSplitContext *ass_ctx; | ||
| 41 | AVBPrint buffer; | ||
| 42 | } TTMLContext; | ||
| 43 | |||
| 44 | 510 | static void ttml_text_cb(void *priv, const char *text, int len) | |
| 45 | { | ||
| 46 | 510 | TTMLContext *s = priv; | |
| 47 | AVBPrint cur_line; | ||
| 48 | 510 | AVBPrint *buffer = &s->buffer; | |
| 49 | |||
| 50 | 510 | av_bprint_init(&cur_line, len, AV_BPRINT_SIZE_UNLIMITED); | |
| 51 | |||
| 52 | 510 | av_bprint_append_data(&cur_line, text, len); | |
| 53 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 510 times.
|
510 | if (!av_bprint_is_complete(&cur_line)) { |
| 54 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 55 | "Failed to move the current subtitle dialog to AVBPrint!\n"); | ||
| 56 | ✗ | av_bprint_finalize(&cur_line, NULL); | |
| 57 | ✗ | return; | |
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | 510 | av_bprint_escape(buffer, cur_line.str, NULL, AV_ESCAPE_MODE_XML, | |
| 62 | 0); | ||
| 63 | |||
| 64 | 510 | av_bprint_finalize(&cur_line, NULL); | |
| 65 | } | ||
| 66 | |||
| 67 | 325 | static void ttml_new_line_cb(void *priv, int forced) | |
| 68 | { | ||
| 69 | 325 | TTMLContext *s = priv; | |
| 70 | |||
| 71 | 325 | av_bprintf(&s->buffer, "<br/>"); | |
| 72 | 325 | } | |
| 73 | |||
| 74 | static const ASSCodesCallbacks ttml_callbacks = { | ||
| 75 | .text = ttml_text_cb, | ||
| 76 | .new_line = ttml_new_line_cb, | ||
| 77 | }; | ||
| 78 | |||
| 79 | 185 | static int ttml_encode_frame(AVCodecContext *avctx, uint8_t *buf, | |
| 80 | int bufsize, const AVSubtitle *sub) | ||
| 81 | { | ||
| 82 | 185 | TTMLContext *s = avctx->priv_data; | |
| 83 | ASSDialog *dialog; | ||
| 84 | int i; | ||
| 85 | |||
| 86 | 185 | av_bprint_init_for_buffer(&s->buffer, buf, bufsize); | |
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 185 times.
✓ Branch 1 taken 185 times.
|
370 | for (i=0; i<sub->num_rects; i++) { |
| 89 | 185 | const char *ass = sub->rects[i]->ass; | |
| 90 | int ret; | ||
| 91 | |||
| 92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (sub->rects[i]->type != SUBTITLE_ASS) { |
| 93 | ✗ | av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n"); | |
| 94 | ✗ | return AVERROR(EINVAL); | |
| 95 | } | ||
| 96 | |||
| 97 | 185 | dialog = ff_ass_split_dialog(s->ass_ctx, ass); | |
| 98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (!dialog) |
| 99 | ✗ | return AVERROR(ENOMEM); | |
| 100 | |||
| 101 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | if (dialog->style) { |
| 102 | 185 | av_bprintf(&s->buffer, "<span region=\""); | |
| 103 | 185 | av_bprint_escape(&s->buffer, dialog->style, NULL, | |
| 104 | AV_ESCAPE_MODE_XML, | ||
| 105 | AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES); | ||
| 106 | 185 | av_bprintf(&s->buffer, "\">"); | |
| 107 | } | ||
| 108 | |||
| 109 | 185 | ret = ff_ass_split_override_codes(&ttml_callbacks, s, dialog->text); | |
| 110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (ret < 0) { |
| 111 | ✗ | int log_level = (ret != AVERROR_INVALIDDATA || | |
| 112 | ✗ | avctx->err_recognition & AV_EF_EXPLODE) ? | |
| 113 | ✗ | AV_LOG_ERROR : AV_LOG_WARNING; | |
| 114 | ✗ | av_log(avctx, log_level, | |
| 115 | "Splitting received ASS dialog text %s failed: %s\n", | ||
| 116 | ✗ | dialog->text, | |
| 117 | ✗ | av_err2str(ret)); | |
| 118 | |||
| 119 | ✗ | if (log_level == AV_LOG_ERROR) { | |
| 120 | ✗ | ff_ass_free_dialog(&dialog); | |
| 121 | ✗ | return ret; | |
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | if (dialog->style) |
| 126 | 185 | av_bprintf(&s->buffer, "</span>"); | |
| 127 | |||
| 128 | 185 | ff_ass_free_dialog(&dialog); | |
| 129 | } | ||
| 130 | |||
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (!s->buffer.len) |
| 132 | ✗ | return 0; | |
| 133 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
|
185 | if (!av_bprint_is_complete(&s->buffer)) { |
| 134 | ✗ | av_log(avctx, AV_LOG_ERROR, "Buffer too small for TTML event.\n"); | |
| 135 | ✗ | return AVERROR_BUFFER_TOO_SMALL; | |
| 136 | } | ||
| 137 | |||
| 138 | 185 | return s->buffer.len; | |
| 139 | } | ||
| 140 | |||
| 141 | 5 | static av_cold int ttml_encode_close(AVCodecContext *avctx) | |
| 142 | { | ||
| 143 | 5 | TTMLContext *s = avctx->priv_data; | |
| 144 | |||
| 145 | 5 | ff_ass_split_free(s->ass_ctx); | |
| 146 | |||
| 147 | 5 | return 0; | |
| 148 | } | ||
| 149 | |||
| 150 | 5 | static const char *ttml_get_display_alignment(int alignment) | |
| 151 | { | ||
| 152 |
1/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
5 | switch (alignment) { |
| 153 | 5 | case 1: | |
| 154 | case 2: | ||
| 155 | case 3: | ||
| 156 | 5 | return "after"; | |
| 157 | ✗ | case 4: | |
| 158 | case 5: | ||
| 159 | case 6: | ||
| 160 | ✗ | return "center"; | |
| 161 | ✗ | case 7: | |
| 162 | case 8: | ||
| 163 | case 9: | ||
| 164 | ✗ | return "before"; | |
| 165 | ✗ | default: | |
| 166 | ✗ | return NULL; | |
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | 5 | static const char *ttml_get_text_alignment(int alignment) | |
| 171 | { | ||
| 172 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
5 | switch (alignment) { |
| 173 | ✗ | case 1: | |
| 174 | case 4: | ||
| 175 | case 7: | ||
| 176 | ✗ | return "left"; | |
| 177 | 5 | case 2: | |
| 178 | case 5: | ||
| 179 | case 8: | ||
| 180 | 5 | return "center"; | |
| 181 | ✗ | case 3: | |
| 182 | case 6: | ||
| 183 | case 9: | ||
| 184 | ✗ | return "right"; | |
| 185 | ✗ | default: | |
| 186 | ✗ | return NULL; | |
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | 5 | static void ttml_get_origin(ASSScriptInfo script_info, ASSStyle style, | |
| 191 | int *origin_left, int *origin_top) | ||
| 192 | { | ||
| 193 | 5 | *origin_left = av_rescale(style.margin_l, 100, script_info.play_res_x); | |
| 194 | 5 | *origin_top = | |
| 195 | 5 | av_rescale((style.alignment >= 7) ? style.margin_v : 0, | |
| 196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | 100, script_info.play_res_y); |
| 197 | 5 | } | |
| 198 | |||
| 199 | 5 | static void ttml_get_extent(ASSScriptInfo script_info, ASSStyle style, | |
| 200 | int *width, int *height) | ||
| 201 | { | ||
| 202 | 5 | *width = av_rescale(script_info.play_res_x - style.margin_r, | |
| 203 | 5 | 100, script_info.play_res_x); | |
| 204 | 15 | *height = av_rescale((style.alignment <= 3) ? | |
| 205 | 5 | script_info.play_res_y - style.margin_v : | |
| 206 | ✗ | script_info.play_res_y, | |
| 207 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | 100, script_info.play_res_y); |
| 208 | 5 | } | |
| 209 | |||
| 210 | 5 | static int ttml_write_region(AVCodecContext *avctx, AVBPrint *buf, | |
| 211 | ASSScriptInfo script_info, ASSStyle style) | ||
| 212 | { | ||
| 213 | 5 | const char *display_alignment = NULL; | |
| 214 | 5 | const char *text_alignment = NULL; | |
| 215 | 5 | int origin_left = 0; | |
| 216 | 5 | int origin_top = 0; | |
| 217 | 5 | int width = 0; | |
| 218 | 5 | int height = 0; | |
| 219 | |||
| 220 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!style.name) { |
| 221 | ✗ | av_log(avctx, AV_LOG_ERROR, "Subtitle style name not set!\n"); | |
| 222 | ✗ | return AVERROR_INVALIDDATA; | |
| 223 | } | ||
| 224 | |||
| 225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (style.font_size < 0) { |
| 226 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid font size for TTML: %d!\n", | |
| 227 | style.font_size); | ||
| 228 | ✗ | return AVERROR_INVALIDDATA; | |
| 229 | } | ||
| 230 | |||
| 231 |
3/6✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
|
5 | if (style.margin_l < 0 || style.margin_r < 0 || style.margin_v < 0) { |
| 232 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 233 | "One or more negative margin values in subtitle style: " | ||
| 234 | "left: %d, right: %d, vertical: %d!\n", | ||
| 235 | style.margin_l, style.margin_r, style.margin_v); | ||
| 236 | ✗ | return AVERROR_INVALIDDATA; | |
| 237 | } | ||
| 238 | |||
| 239 | 5 | display_alignment = ttml_get_display_alignment(style.alignment); | |
| 240 | 5 | text_alignment = ttml_get_text_alignment(style.alignment); | |
| 241 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | if (!display_alignment || !text_alignment) { |
| 242 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 243 | "Failed to convert ASS style alignment %d of style %s to " | ||
| 244 | "TTML display and text alignment!\n", | ||
| 245 | style.alignment, | ||
| 246 | style.name); | ||
| 247 | ✗ | return AVERROR_INVALIDDATA; | |
| 248 | } | ||
| 249 | |||
| 250 | 5 | ttml_get_origin(script_info, style, &origin_left, &origin_top); | |
| 251 | 5 | ttml_get_extent(script_info, style, &width, &height); | |
| 252 | |||
| 253 | 5 | av_bprintf(buf, " <region xml:id=\""); | |
| 254 | 5 | av_bprint_escape(buf, style.name, NULL, AV_ESCAPE_MODE_XML, | |
| 255 | AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES); | ||
| 256 | 5 | av_bprintf(buf, "\"\n"); | |
| 257 | |||
| 258 | 5 | av_bprintf(buf, " tts:origin=\"%d%% %d%%\"\n", | |
| 259 | origin_left, origin_top); | ||
| 260 | 5 | av_bprintf(buf, " tts:extent=\"%d%% %d%%\"\n", | |
| 261 | width, height); | ||
| 262 | |||
| 263 | 5 | av_bprintf(buf, " tts:displayAlign=\""); | |
| 264 | 5 | av_bprint_escape(buf, display_alignment, NULL, AV_ESCAPE_MODE_XML, | |
| 265 | AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES); | ||
| 266 | 5 | av_bprintf(buf, "\"\n"); | |
| 267 | |||
| 268 | 5 | av_bprintf(buf, " tts:textAlign=\""); | |
| 269 | 5 | av_bprint_escape(buf, text_alignment, NULL, AV_ESCAPE_MODE_XML, | |
| 270 | AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES); | ||
| 271 | 5 | av_bprintf(buf, "\"\n"); | |
| 272 | |||
| 273 | // if we set cell resolution to our script reference resolution, | ||
| 274 | // then a single line is a single "point" on our canvas. Thus, by setting | ||
| 275 | // our font size to font size in cells, we should gain a similar enough | ||
| 276 | // scale without resorting to explicit pixel based font sizing, which is | ||
| 277 | // frowned upon in the TTML community. | ||
| 278 | 5 | av_bprintf(buf, " tts:fontSize=\"%dc\"\n", | |
| 279 | style.font_size); | ||
| 280 | |||
| 281 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (style.font_name) { |
| 282 | 5 | av_bprintf(buf, " tts:fontFamily=\""); | |
| 283 | 5 | av_bprint_escape(buf, style.font_name, NULL, AV_ESCAPE_MODE_XML, | |
| 284 | AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES); | ||
| 285 | 5 | av_bprintf(buf, "\"\n"); | |
| 286 | } | ||
| 287 | |||
| 288 | 5 | av_bprintf(buf, " tts:overflow=\"visible\" />\n"); | |
| 289 | |||
| 290 | 5 | return 0; | |
| 291 | } | ||
| 292 | |||
| 293 | 5 | static int ttml_write_header_content(AVCodecContext *avctx) | |
| 294 | { | ||
| 295 | 5 | TTMLContext *s = avctx->priv_data; | |
| 296 | 5 | ASS *ass = (ASS *)s->ass_ctx; | |
| 297 | 5 | ASSScriptInfo script_info = ass->script_info; | |
| 298 | 5 | const size_t base_extradata_size = TTMLENC_EXTRADATA_SIGNATURE_SIZE + 1 + | |
| 299 | AV_INPUT_BUFFER_PADDING_SIZE; | ||
| 300 | 5 | size_t additional_extradata_size = 0; | |
| 301 | int ret; | ||
| 302 | |||
| 303 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | if (script_info.play_res_x <= 0 || script_info.play_res_y <= 0) { |
| 304 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 305 | "Invalid subtitle reference resolution %dx%d!\n", | ||
| 306 | script_info.play_res_x, script_info.play_res_y); | ||
| 307 | ✗ | return AVERROR_INVALIDDATA; | |
| 308 | } | ||
| 309 | |||
| 310 | 5 | av_bprint_init(&s->buffer, 0, INT_MAX - base_extradata_size); | |
| 311 | |||
| 312 | // write the first string in extradata, attributes in the base "tt" element. | ||
| 313 | 5 | av_bprintf(&s->buffer, TTML_DEFAULT_NAMESPACING); | |
| 314 | // the cell resolution is in character cells, so not exactly 1:1 against | ||
| 315 | // a pixel based resolution, but as the tts:extent in the root | ||
| 316 | // "tt" element is frowned upon (and disallowed in the EBU-TT profile), | ||
| 317 | // we mimic the reference resolution by setting it as the cell resolution. | ||
| 318 | 5 | av_bprintf(&s->buffer, " ttp:cellResolution=\"%d %d\"\n", | |
| 319 | script_info.play_res_x, script_info.play_res_y); | ||
| 320 | 5 | av_bprint_chars(&s->buffer, '\0', 1); | |
| 321 | |||
| 322 | // write the second string in extradata, head element containing the styles | ||
| 323 | 5 | av_bprintf(&s->buffer, " <head>\n"); | |
| 324 | 5 | av_bprintf(&s->buffer, " <layout>\n"); | |
| 325 | |||
| 326 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | for (int i = 0; i < ass->styles_count; i++) { |
| 327 | 5 | ret = ttml_write_region(avctx, &s->buffer, script_info, | |
| 328 | 5 | ass->styles[i]); | |
| 329 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (ret < 0) |
| 330 | ✗ | goto fail; | |
| 331 | } | ||
| 332 | |||
| 333 | 5 | av_bprintf(&s->buffer, " </layout>\n"); | |
| 334 | 5 | av_bprintf(&s->buffer, " </head>\n"); | |
| 335 | 5 | av_bprint_chars(&s->buffer, '\0', 1); | |
| 336 | |||
| 337 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
|
5 | if (!av_bprint_is_complete(&s->buffer)) { |
| 338 | ✗ | ret = AVERROR(ENOMEM); | |
| 339 | ✗ | goto fail; | |
| 340 | } | ||
| 341 | |||
| 342 | 5 | additional_extradata_size = s->buffer.len; | |
| 343 | |||
| 344 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!(avctx->extradata = |
| 345 | 5 | av_mallocz(base_extradata_size + additional_extradata_size))) { | |
| 346 | ✗ | ret = AVERROR(ENOMEM); | |
| 347 | ✗ | goto fail; | |
| 348 | } | ||
| 349 | |||
| 350 | 5 | avctx->extradata_size = | |
| 351 | 5 | TTMLENC_EXTRADATA_SIGNATURE_SIZE + additional_extradata_size; | |
| 352 | 5 | memcpy(avctx->extradata, TTMLENC_EXTRADATA_SIGNATURE, | |
| 353 | TTMLENC_EXTRADATA_SIGNATURE_SIZE); | ||
| 354 | |||
| 355 | 5 | memcpy(avctx->extradata + TTMLENC_EXTRADATA_SIGNATURE_SIZE, | |
| 356 | 5 | s->buffer.str, additional_extradata_size); | |
| 357 | |||
| 358 | 5 | ret = 0; | |
| 359 | 5 | fail: | |
| 360 | 5 | av_bprint_finalize(&s->buffer, NULL); | |
| 361 | |||
| 362 | 5 | return ret; | |
| 363 | } | ||
| 364 | |||
| 365 | 5 | static av_cold int ttml_encode_init(AVCodecContext *avctx) | |
| 366 | { | ||
| 367 | 5 | TTMLContext *s = avctx->priv_data; | |
| 368 | 5 | int ret = AVERROR_BUG; | |
| 369 | 5 | s->avctx = avctx; | |
| 370 | |||
| 371 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
|
5 | if (!(s->ass_ctx = ff_ass_split(avctx->subtitle_header))) { |
| 372 | ✗ | return AVERROR_INVALIDDATA; | |
| 373 | } | ||
| 374 | |||
| 375 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
|
5 | if ((ret = ttml_write_header_content(avctx)) < 0) { |
| 376 | ✗ | return ret; | |
| 377 | } | ||
| 378 | |||
| 379 | 5 | return 0; | |
| 380 | } | ||
| 381 | |||
| 382 | const FFCodec ff_ttml_encoder = { | ||
| 383 | .p.name = "ttml", | ||
| 384 | CODEC_LONG_NAME("TTML subtitle"), | ||
| 385 | .p.type = AVMEDIA_TYPE_SUBTITLE, | ||
| 386 | .p.id = AV_CODEC_ID_TTML, | ||
| 387 | .priv_data_size = sizeof(TTMLContext), | ||
| 388 | .init = ttml_encode_init, | ||
| 389 | FF_CODEC_ENCODE_SUB_CB(ttml_encode_frame), | ||
| 390 | .close = ttml_encode_close, | ||
| 391 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 392 | }; | ||
| 393 |