2 * 3GPP TS 26.245 Timed Text encoder
3 * Copyright (c) 2012 Philip Langdale <philipl@overt.org>
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 #include "libavutil/opt.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/common.h"
30 #include "ass_split.h"
32 #include "bytestream.h"
35 #define STYLE_FLAG_BOLD (1<<0)
36 #define STYLE_FLAG_ITALIC (1<<1)
37 #define STYLE_FLAG_UNDERLINE (1<<2)
38 #define STYLE_RECORD_SIZE 12
41 #define STYL_BOX (1<<0)
42 #define HLIT_BOX (1<<1)
43 #define HCLR_BOX (1<<2)
45 #define DEFAULT_STYLE_FONT_ID 0x01
46 #define DEFAULT_STYLE_FONTSIZE 0x12
47 #define DEFAULT_STYLE_COLOR 0xffffffff
48 #define DEFAULT_STYLE_FLAG 0x00
50 #define BGR_TO_RGB(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((uint32_t)(c) >> 16) & 0xff))
51 #define FONTSIZE_SCALE(s,fs) ((fs) * (s)->font_scale_factor + 0.5)
52 #define av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size)
58 uint16_t style_fontID;
59 uint8_t style_fontsize;
74 AVCodecContext *avctx;
76 ASSSplitContext *ass_ctx;
77 ASSStyle *ass_dialog_style;
78 StyleBox *style_attributes;
80 unsigned style_attributes_bytes_allocated;
81 StyleBox style_attributes_temp;
91 double font_scale_factor;
96 void (*encode)(MovTextContext *s);
99 static void mov_text_cleanup(MovTextContext *s)
102 s->style_attributes_temp = s->d;
105 static void encode_styl(MovTextContext *s)
107 if ((s->box_flags & STYL_BOX) && s->count) {
108 uint8_t buf[12], *p = buf;
110 bytestream_put_be32(&p, s->count * STYLE_RECORD_SIZE + SIZE_ADD);
111 bytestream_put_be32(&p, MKBETAG('s','t','y','l'));
112 bytestream_put_be16(&p, s->count);
113 /*The above three attributes are hard coded for now
114 but will come from ASS style in the future*/
115 av_bprint_append_any(&s->buffer, buf, 10);
116 for (unsigned j = 0; j < s->count; j++) {
117 const StyleBox *style = &s->style_attributes[j];
120 bytestream_put_be16(&p, style->style_start);
121 bytestream_put_be16(&p, style->style_end);
122 bytestream_put_be16(&p, style->style_fontID);
123 bytestream_put_byte(&p, style->style_flag);
124 bytestream_put_byte(&p, style->style_fontsize);
125 bytestream_put_be32(&p, style->style_color);
127 av_bprint_append_any(&s->buffer, buf, 12);
133 static void encode_hlit(MovTextContext *s)
135 if (s->box_flags & HLIT_BOX) {
136 uint8_t buf[12], *p = buf;
138 bytestream_put_be32(&p, 12);
139 bytestream_put_be32(&p, MKBETAG('h','l','i','t'));
140 bytestream_put_be16(&p, s->hlit.start);
141 bytestream_put_be16(&p, s->hlit.end);
143 av_bprint_append_any(&s->buffer, buf, 12);
147 static void encode_hclr(MovTextContext *s)
149 if (s->box_flags & HCLR_BOX) {
150 uint8_t buf[12], *p = buf;
152 bytestream_put_be32(&p, 12);
153 bytestream_put_be32(&p, MKBETAG('h','c','l','r'));
154 bytestream_put_be32(&p, s->hclr.color);
156 av_bprint_append_any(&s->buffer, buf, 12);
160 static const Box box_types[] = {
166 const static size_t box_count = FF_ARRAY_ELEMS(box_types);
168 static int mov_text_encode_close(AVCodecContext *avctx)
170 MovTextContext *s = avctx->priv_data;
172 ff_ass_split_free(s->ass_ctx);
173 av_freep(&s->style_attributes);
175 av_bprint_finalize(&s->buffer, NULL);
179 static int encode_sample_description(AVCodecContext *avctx)
184 uint32_t back_color = 0;
185 int font_names_total_len = 0;
186 MovTextContext *s = avctx->priv_data;
187 uint8_t buf[30], *p = buf;
189 // 0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags
190 // 0x01, // int8_t horizontal-justification
191 // 0xFF, // int8_t vertical-justification
192 // 0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4]
194 // 0x00, 0x00, // int16_t top
195 // 0x00, 0x00, // int16_t left
196 // 0x00, 0x00, // int16_t bottom
197 // 0x00, 0x00, // int16_t right
200 // 0x00, 0x00, // uint16_t startChar
201 // 0x00, 0x00, // uint16_t endChar
202 // 0x00, 0x01, // uint16_t font-ID
203 // 0x00, // uint8_t face-style-flags
204 // 0x12, // uint8_t font-size
205 // 0xFF, 0xFF, 0xFF, 0xFF, // uint8_t text-color-rgba[4]
208 // 0x00, 0x00, 0x00, 0x12, // uint32_t size
209 // 'f', 't', 'a', 'b', // uint8_t name[4]
210 // 0x00, 0x01, // uint16_t entry-count
212 // 0x00, 0x01, // uint16_t font-ID
213 // 0x05, // uint8_t font-name-length
214 // 'S', 'e', 'r', 'i', 'f',// uint8_t font[font-name-length]
218 // Populate sample description from ASS header
219 ass = (ASS*)s->ass_ctx;
220 // Compute font scaling factor based on (optionally) provided
221 // output video height and ASS script play_res_y
222 if (s->frame_height && ass->script_info.play_res_y)
223 s->font_scale_factor = (double)s->frame_height / ass->script_info.play_res_y;
225 s->font_scale_factor = 1;
227 style = ff_ass_style_get(s->ass_ctx, "Default");
228 if (!style && ass->styles_count) {
229 style = &ass->styles[0];
231 s->d.style_fontID = DEFAULT_STYLE_FONT_ID;
232 s->d.style_fontsize = DEFAULT_STYLE_FONTSIZE;
233 s->d.style_color = DEFAULT_STYLE_COLOR;
234 s->d.style_flag = DEFAULT_STYLE_FLAG;
236 s->d.style_fontsize = FONTSIZE_SCALE(s, style->font_size);
237 s->d.style_color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8 |
238 255 - ((uint32_t)style->primary_color >> 24);
239 s->d.style_flag = (!!style->bold * STYLE_FLAG_BOLD) |
240 (!!style->italic * STYLE_FLAG_ITALIC) |
241 (!!style->underline * STYLE_FLAG_UNDERLINE);
242 back_color = (BGR_TO_RGB(style->back_color & 0xffffff) << 8) |
243 (255 - ((uint32_t)style->back_color >> 24));
246 bytestream_put_be32(&p, 0); // displayFlags
247 bytestream_put_be16(&p, 0x01FF); // horizontal/vertical justification (2x int8_t)
248 bytestream_put_be32(&p, back_color);
249 bytestream_put_be64(&p, 0); // BoxRecord - 4xint16_t: top, left, bottom, right
251 bytestream_put_be16(&p, s->d.style_start);
252 bytestream_put_be16(&p, s->d.style_end);
253 bytestream_put_be16(&p, s->d.style_fontID);
254 bytestream_put_byte(&p, s->d.style_flag);
255 bytestream_put_byte(&p, s->d.style_fontsize);
256 bytestream_put_be32(&p, s->d.style_color);
258 av_bprint_append_any(&s->buffer, buf, 30);
261 // We can't build a complete font table since that would require
262 // scanning all dialogs first. But we can at least fill in what
263 // is avaiable in the ASS header
264 if (style && ass->styles_count) {
265 // Find unique font names
266 if (style->font_name) {
267 av_dynarray_add(&s->fonts, &s->font_count, style->font_name);
268 font_names_total_len += strlen(style->font_name);
270 for (i = 0; i < ass->styles_count; i++) {
272 if (!ass->styles[i].font_name)
274 for (j = 0; j < s->font_count; j++) {
275 if (!strcmp(s->fonts[j], ass->styles[i].font_name)) {
281 av_dynarray_add(&s->fonts, &s->font_count,
282 ass->styles[i].font_name);
283 font_names_total_len += strlen(ass->styles[i].font_name);
287 av_dynarray_add(&s->fonts, &s->font_count, (char*)"Serif");
291 bytestream_put_be32(&p, SIZE_ADD + 3 * s->font_count + font_names_total_len); // Size
292 bytestream_put_be32(&p, MKBETAG('f','t','a','b'));
293 bytestream_put_be16(&p, s->font_count);
295 av_bprint_append_any(&s->buffer, buf, 10);
297 for (i = 0; i < s->font_count; i++) {
298 size_t len = strlen(s->fonts[i]);
301 bytestream_put_be16(&p, i + 1); //fontID
302 bytestream_put_byte(&p, len);
304 av_bprint_append_any(&s->buffer, buf, 3);
305 av_bprint_append_any(&s->buffer, s->fonts[i], len);
310 if (!av_bprint_is_complete(&s->buffer)) {
311 return AVERROR(ENOMEM);
314 avctx->extradata_size = s->buffer.len;
315 avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
316 if (!avctx->extradata) {
317 return AVERROR(ENOMEM);
320 memcpy(avctx->extradata, s->buffer.str, avctx->extradata_size);
321 av_bprint_clear(&s->buffer);
326 static av_cold int mov_text_encode_init(AVCodecContext *avctx)
329 MovTextContext *s = avctx->priv_data;
332 av_bprint_init(&s->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
334 s->ass_ctx = ff_ass_split(avctx->subtitle_header);
336 return AVERROR_INVALIDDATA;
337 ret = encode_sample_description(avctx);
344 // Start a new style box if needed
345 static int mov_text_style_start(MovTextContext *s)
347 // there's an existing style entry
348 if (s->style_attributes_temp.style_start == s->text_pos)
349 // Still at same text pos, use same entry
351 if (s->style_attributes_temp.style_flag != s->d.style_flag ||
352 s->style_attributes_temp.style_color != s->d.style_color ||
353 s->style_attributes_temp.style_fontID != s->d.style_fontID ||
354 s->style_attributes_temp.style_fontsize != s->d.style_fontsize) {
357 // last style != defaults, end the style entry and start a new one
358 if (s->count + 1 > FFMIN(SIZE_MAX / sizeof(*s->style_attributes), UINT16_MAX) ||
359 !(tmp = av_fast_realloc(s->style_attributes,
360 &s->style_attributes_bytes_allocated,
361 (s->count + 1) * sizeof(*s->style_attributes)))) {
363 av_bprint_clear(&s->buffer);
364 s->box_flags &= ~STYL_BOX;
367 s->style_attributes = tmp;
368 s->style_attributes_temp.style_end = s->text_pos;
369 s->style_attributes[s->count++] = s->style_attributes_temp;
370 s->box_flags |= STYL_BOX;
371 s->style_attributes_temp = s->d;
372 s->style_attributes_temp.style_start = s->text_pos;
373 } else { // style entry matches defaults, drop entry
374 s->style_attributes_temp = s->d;
375 s->style_attributes_temp.style_start = s->text_pos;
380 static uint8_t mov_text_style_to_flag(const char style)
382 uint8_t style_flag = 0;
386 style_flag = STYLE_FLAG_BOLD;
389 style_flag = STYLE_FLAG_ITALIC;
392 style_flag = STYLE_FLAG_UNDERLINE;
398 static void mov_text_style_set(MovTextContext *s, uint8_t style_flags)
400 if (!((s->style_attributes_temp.style_flag & style_flags) ^ style_flags)) {
401 // setting flags that that are already set
404 if (mov_text_style_start(s))
405 s->style_attributes_temp.style_flag |= style_flags;
408 static void mov_text_style_cb(void *priv, const char style, int close)
410 MovTextContext *s = priv;
411 uint8_t style_flag = mov_text_style_to_flag(style);
413 if (!!(s->style_attributes_temp.style_flag & style_flag) != close) {
414 // setting flag that is already set
417 if (mov_text_style_start(s)) {
419 s->style_attributes_temp.style_flag |= style_flag;
421 s->style_attributes_temp.style_flag &= ~style_flag;
425 static void mov_text_color_set(MovTextContext *s, uint32_t color)
427 if ((s->style_attributes_temp.style_color & 0xffffff00) == color) {
428 // color hasn't changed
431 if (mov_text_style_start(s))
432 s->style_attributes_temp.style_color = (color & 0xffffff00) |
433 (s->style_attributes_temp.style_color & 0xff);
436 static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id)
438 MovTextContext *s = priv;
440 color = BGR_TO_RGB(color) << 8;
441 if (color_id == 1) { //primary color changes
442 mov_text_color_set(s, color);
443 } else if (color_id == 2) { //secondary color changes
444 if (!(s->box_flags & HCLR_BOX))
445 // Highlight alpha not set yet, use current primary alpha
446 s->hclr.color = s->style_attributes_temp.style_color;
447 if (!(s->box_flags & HLIT_BOX) || s->hlit.start == s->text_pos) {
448 s->box_flags |= HCLR_BOX;
449 s->box_flags |= HLIT_BOX;
450 s->hlit.start = s->text_pos;
451 s->hclr.color = color | (s->hclr.color & 0xFF);
454 s->hlit.end = s->text_pos;
455 /* If there are more than one secondary color changes in ASS,
456 take start of first section and end of last section. Movtext
457 allows only one highlight box per sample.
460 // Movtext does not support changes to other color_id (outline, background)
463 static void mov_text_alpha_set(MovTextContext *s, uint8_t alpha)
465 if ((s->style_attributes_temp.style_color & 0xff) == alpha) {
466 // color hasn't changed
469 if (mov_text_style_start(s))
470 s->style_attributes_temp.style_color =
471 (s->style_attributes_temp.style_color & 0xffffff00) | alpha;
474 static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id)
476 MovTextContext *s = priv;
479 if (alpha_id == 1) // primary alpha changes
480 mov_text_alpha_set(s, alpha);
481 else if (alpha_id == 2) { //secondary alpha changes
482 if (!(s->box_flags & HCLR_BOX))
483 // Highlight color not set yet, use current primary color
484 s->hclr.color = s->style_attributes_temp.style_color;
485 if (!(s->box_flags & HLIT_BOX) || s->hlit.start == s->text_pos) {
486 s->box_flags |= HCLR_BOX;
487 s->box_flags |= HLIT_BOX;
488 s->hlit.start = s->text_pos;
489 s->hclr.color = (s->hclr.color & 0xffffff00) | alpha;
492 s->hlit.end = s->text_pos;
494 // Movtext does not support changes to other alpha_id (outline, background)
497 static uint16_t find_font_id(MovTextContext *s, const char *name)
500 for (i = 0; i < s->font_count; i++) {
501 if (!strcmp(name, s->fonts[i]))
507 static void mov_text_font_name_set(MovTextContext *s, const char *name)
509 int fontID = find_font_id(s, name);
510 if (s->style_attributes_temp.style_fontID == fontID) {
511 // color hasn't changed
514 if (mov_text_style_start(s))
515 s->style_attributes_temp.style_fontID = fontID;
518 static void mov_text_font_name_cb(void *priv, const char *name)
520 mov_text_font_name_set((MovTextContext*)priv, name);
523 static void mov_text_font_size_set(MovTextContext *s, int size)
525 size = FONTSIZE_SCALE(s, size);
526 if (s->style_attributes_temp.style_fontsize == size) {
527 // color hasn't changed
530 if (mov_text_style_start(s))
531 s->style_attributes_temp.style_fontsize = size;
534 static void mov_text_font_size_cb(void *priv, int size)
536 mov_text_font_size_set((MovTextContext*)priv, size);
539 static void mov_text_end_cb(void *priv)
541 // End of text, close any open style record
542 mov_text_style_start((MovTextContext*)priv);
545 static void mov_text_ass_style_set(MovTextContext *s, ASSStyle *style)
547 uint8_t style_flags, alpha;
551 style_flags = (!!style->bold * STYLE_FLAG_BOLD) |
552 (!!style->italic * STYLE_FLAG_ITALIC) |
553 (!!style->underline * STYLE_FLAG_UNDERLINE);
554 mov_text_style_set(s, style_flags);
555 color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8;
556 mov_text_color_set(s, color);
557 alpha = 255 - ((uint32_t)style->primary_color >> 24);
558 mov_text_alpha_set(s, alpha);
559 mov_text_font_size_set(s, style->font_size);
560 mov_text_font_name_set(s, style->font_name);
562 // End current style record, go back to defaults
563 mov_text_style_start(s);
567 static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog)
569 ASSStyle *style = ff_ass_style_get(s->ass_ctx, dialog->style);
571 s->ass_dialog_style = style;
572 mov_text_ass_style_set(s, style);
575 static void mov_text_cancel_overrides_cb(void *priv, const char *style_name)
577 MovTextContext *s = priv;
580 if (!style_name || !*style_name)
581 style = s->ass_dialog_style;
583 style= ff_ass_style_get(s->ass_ctx, style_name);
585 mov_text_ass_style_set(s, style);
588 static uint16_t utf8_strlen(const char *text, int len)
590 uint16_t i = 0, ret = 0;
595 else if ((c & 0xE0) == 0xC0)
597 else if ((c & 0xF0) == 0xE0)
599 else if ((c & 0xF8) == 0xF0)
608 static void mov_text_text_cb(void *priv, const char *text, int len)
610 uint16_t utf8_len = utf8_strlen(text, len);
611 MovTextContext *s = priv;
612 av_bprint_append_data(&s->buffer, text, len);
613 // If it's not utf-8, just use the byte length
614 s->text_pos += utf8_len ? utf8_len : len;
615 s->byte_count += len;
618 static void mov_text_new_line_cb(void *priv, int forced)
620 MovTextContext *s = priv;
621 av_bprint_append_data(&s->buffer, "\n", 1);
626 static const ASSCodesCallbacks mov_text_callbacks = {
627 .text = mov_text_text_cb,
628 .new_line = mov_text_new_line_cb,
629 .style = mov_text_style_cb,
630 .color = mov_text_color_cb,
631 .alpha = mov_text_alpha_cb,
632 .font_name = mov_text_font_name_cb,
633 .font_size = mov_text_font_size_cb,
634 .cancel_overrides = mov_text_cancel_overrides_cb,
635 .end = mov_text_end_cb,
638 static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
639 int bufsize, const AVSubtitle *sub)
641 MovTextContext *s = avctx->priv_data;
650 for (i = 0; i < sub->num_rects; i++) {
651 const char *ass = sub->rects[i]->ass;
653 if (sub->rects[i]->type != SUBTITLE_ASS) {
654 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
655 return AVERROR(EINVAL);
658 dialog = ff_ass_split_dialog2(s->ass_ctx, ass);
660 return AVERROR(ENOMEM);
661 mov_text_dialog(s, dialog);
662 ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
663 ff_ass_free_dialog(&dialog);
665 for (j = 0; j < box_count; j++) {
666 box_types[j].encode(s);
670 AV_WB16(buf, s->byte_count);
673 if (!av_bprint_is_complete(&s->buffer)) {
674 length = AVERROR(ENOMEM);
678 if (!s->buffer.len) {
683 if (s->buffer.len > bufsize - 3) {
684 av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
685 length = AVERROR_BUFFER_TOO_SMALL;
689 memcpy(buf, s->buffer.str, s->buffer.len);
690 length = s->buffer.len + 2;
693 av_bprint_clear(&s->buffer);
697 #define OFFSET(x) offsetof(MovTextContext, x)
698 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
699 static const AVOption options[] = {
700 { "height", "Frame height, usually video height", OFFSET(frame_height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
704 static const AVClass mov_text_encoder_class = {
705 .class_name = "MOV text enoder",
706 .item_name = av_default_item_name,
708 .version = LIBAVUTIL_VERSION_INT,
711 AVCodec ff_movtext_encoder = {
713 .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
714 .type = AVMEDIA_TYPE_SUBTITLE,
715 .id = AV_CODEC_ID_MOV_TEXT,
716 .priv_data_size = sizeof(MovTextContext),
717 .priv_class = &mov_text_encoder_class,
718 .init = mov_text_encode_init,
719 .encode_sub = mov_text_encode_frame,
720 .close = mov_text_encode_close,
721 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,