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