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