2 * 3GPP TS 26.245 Timed Text decoder
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/avstring.h"
26 #include "libavutil/common.h"
27 #include "libavutil/bprint.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/mem.h"
31 #define STYLE_FLAG_BOLD (1<<0)
32 #define STYLE_FLAG_ITALIC (1<<1)
33 #define STYLE_FLAG_UNDERLINE (1<<2)
35 #define BOX_SIZE_INITIAL 40
37 #define STYL_BOX (1<<0)
38 #define HLIT_BOX (1<<1)
39 #define HCLR_BOX (1<<2)
40 #define TWRP_BOX (1<<3)
43 #define BOTTOM_CENTER 2
44 #define BOTTOM_RIGHT 3
46 #define MIDDLE_CENTER 5
47 #define MIDDLE_RIGHT 6
52 #define RGB_TO_BGR(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((c) >> 16) & 0xff))
83 uint16_t style_fontID;
92 uint8_t hlit_color[4];
109 uint16_t style_entries, ftab_entries;
121 int (*decode)(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt);
124 static void mov_text_cleanup(MovTextContext *m)
127 if (m->box_flags & STYL_BOX) {
128 for(i = 0; i < m->count_s; i++) {
133 m->style_entries = 0;
137 static void mov_text_cleanup_ftab(MovTextContext *m)
139 for (unsigned i = 0; i < m->ftab_entries; i++)
140 av_freep(&m->ftab[i].font);
145 static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m)
147 uint8_t *tx3g_ptr = avctx->extradata;
148 int i, font_length, remaining = avctx->extradata_size - BOX_SIZE_INITIAL;
149 int8_t v_align, h_align;
150 unsigned ftab_entries;
160 h_align = *tx3g_ptr++;
161 v_align = *tx3g_ptr++;
164 m->d.alignment = TOP_LEFT;
166 m->d.alignment = MIDDLE_LEFT;
168 m->d.alignment = BOTTOM_LEFT;
172 m->d.alignment = TOP_CENTER;
174 m->d.alignment = MIDDLE_CENTER;
176 m->d.alignment = BOTTOM_CENTER;
180 m->d.alignment = TOP_RIGHT;
182 m->d.alignment = MIDDLE_RIGHT;
184 m->d.alignment = BOTTOM_RIGHT;
187 m->d.back_color = AV_RB24(tx3g_ptr);
189 m->d.back_alpha = AV_RB8(tx3g_ptr);
196 m->d.fontID = AV_RB16(tx3g_ptr);
199 s_default.style_flag = *tx3g_ptr++;
200 m->d.bold = !!(s_default.style_flag & STYLE_FLAG_BOLD);
201 m->d.italic = !!(s_default.style_flag & STYLE_FLAG_ITALIC);
202 m->d.underline = !!(s_default.style_flag & STYLE_FLAG_UNDERLINE);
204 m->d.fontsize = *tx3g_ptr++;
206 m->d.color = AV_RB24(tx3g_ptr);
208 m->d.alpha = AV_RB8(tx3g_ptr);
216 // In case of broken header, init default font
217 m->d.font = ASS_DEFAULT_FONT;
219 ftab_entries = AV_RB16(tx3g_ptr);
222 remaining -= 3 * ftab_entries;
224 return AVERROR_INVALIDDATA;
225 m->ftab = av_calloc(ftab_entries, sizeof(*m->ftab));
227 return AVERROR(ENOMEM);
228 m->ftab_entries = ftab_entries;
231 for (i = 0; i < m->ftab_entries; i++) {
232 m->ftab[i].fontID = AV_RB16(tx3g_ptr);
234 font_length = *tx3g_ptr++;
236 remaining -= font_length;
238 mov_text_cleanup_ftab(m);
241 m->ftab[i].font = av_malloc(font_length + 1);
242 if (!m->ftab[i].font) {
243 mov_text_cleanup_ftab(m);
244 return AVERROR(ENOMEM);
246 memcpy(m->ftab[i].font, tx3g_ptr, font_length);
247 m->ftab[i].font[font_length] = '\0';
248 tx3g_ptr = tx3g_ptr + font_length;
250 for (i = 0; i < m->ftab_entries; i++) {
251 if (m->d.fontID == m->ftab[i].fontID)
252 m->d.font = m->ftab[i].font;
257 static int decode_twrp(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
259 m->box_flags |= TWRP_BOX;
260 m->w.wrap_flag = *tsmb++;
264 static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
266 m->box_flags |= HLIT_BOX;
267 m->h.hlit_start = AV_RB16(tsmb);
269 m->h.hlit_end = AV_RB16(tsmb);
274 static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
276 m->box_flags |= HCLR_BOX;
277 memcpy(m->c.hlit_color, tsmb, 4);
282 static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
285 int style_entries = AV_RB16(tsmb);
287 // A single style record is of length 12 bytes.
288 if (m->tracksize + m->size_var + 2 + style_entries * 12 > avpkt->size)
291 m->style_entries = style_entries;
293 m->box_flags |= STYL_BOX;
294 for(i = 0; i < m->style_entries; i++) {
295 m->s_temp = av_malloc(sizeof(*m->s_temp));
298 return AVERROR(ENOMEM);
300 m->s_temp->style_start = AV_RB16(tsmb);
302 m->s_temp->style_end = AV_RB16(tsmb);
304 if ( m->s_temp->style_end < m->s_temp->style_start
305 || (m->count_s && m->s_temp->style_start < m->s[m->count_s - 1]->style_end)) {
306 av_freep(&m->s_temp);
308 return AVERROR(ENOMEM);
312 m->s_temp->style_fontID = AV_RB16(tsmb);
314 m->s_temp->style_flag = AV_RB8(tsmb);
315 m->s_temp->bold = !!(m->s_temp->style_flag & STYLE_FLAG_BOLD);
316 m->s_temp->italic = !!(m->s_temp->style_flag & STYLE_FLAG_ITALIC);
317 m->s_temp->underline = !!(m->s_temp->style_flag & STYLE_FLAG_UNDERLINE);
319 m->s_temp->fontsize = AV_RB8(tsmb);
321 m->s_temp->color = AV_RB24(tsmb);
323 m->s_temp->alpha = AV_RB8(tsmb);
325 av_dynarray_add(&m->s, &m->count_s, m->s_temp);
328 return AVERROR(ENOMEM);
334 static const Box box_types[] = {
335 { MKBETAG('s','t','y','l'), 2, decode_styl },
336 { MKBETAG('h','l','i','t'), 4, decode_hlit },
337 { MKBETAG('h','c','l','r'), 4, decode_hclr },
338 { MKBETAG('t','w','r','p'), 1, decode_twrp }
341 const static size_t box_count = FF_ARRAY_ELEMS(box_types);
343 // Return byte length of the UTF-8 sequence starting at text[0]. 0 on error.
344 static int get_utf8_length_at(const char *text, const char *text_end)
346 const char *start = text;
349 GET_UTF8(c, text < text_end ? (uint8_t)*text++ : (err = 1, 0), goto error;);
357 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
358 AVCodecContext *avctx)
360 MovTextContext *m = avctx->priv_data;
363 int style_active = 0;
365 int color = m->d.color;
367 if (text < text_end && m->box_flags & TWRP_BOX) {
368 if (m->w.wrap_flag == 1) {
369 av_bprintf(buf, "{\\q1}"); /* End of line wrap */
371 av_bprintf(buf, "{\\q2}"); /* No wrap */
375 while (text < text_end) {
378 if ((m->box_flags & STYL_BOX) && entry < m->style_entries) {
379 if (text_pos == m->s[entry]->style_start) {
381 if (m->s[entry]->bold ^ m->d.bold)
382 av_bprintf(buf, "{\\b%d}", m->s[entry]->bold);
383 if (m->s[entry]->italic ^ m->d.italic)
384 av_bprintf(buf, "{\\i%d}", m->s[entry]->italic);
385 if (m->s[entry]->underline ^ m->d.underline)
386 av_bprintf(buf, "{\\u%d}", m->s[entry]->underline);
387 if (m->s[entry]->fontsize != m->d.fontsize)
388 av_bprintf(buf, "{\\fs%d}", m->s[entry]->fontsize);
389 if (m->s[entry]->style_fontID != m->d.fontID)
390 for (i = 0; i < m->ftab_entries; i++) {
391 if (m->s[entry]->style_fontID == m->ftab[i].fontID)
392 av_bprintf(buf, "{\\fn%s}", m->ftab[i].font);
394 if (m->d.color != m->s[entry]->color) {
395 color = m->s[entry]->color;
396 av_bprintf(buf, "{\\1c&H%X&}", RGB_TO_BGR(color));
398 if (m->d.alpha != m->s[entry]->alpha)
399 av_bprintf(buf, "{\\1a&H%02X&}", 255 - m->s[entry]->alpha);
401 if (text_pos == m->s[entry]->style_end) {
403 av_bprintf(buf, "{\\r}");
410 if (m->box_flags & HLIT_BOX) {
411 if (text_pos == m->h.hlit_start) {
412 /* If hclr box is present, set the secondary color to the color
413 * specified. Otherwise, set primary color to white and secondary
414 * color to black. These colors will come from TextSampleModifier
415 * boxes in future and inverse video technique for highlight will
418 if (m->box_flags & HCLR_BOX) {
419 av_bprintf(buf, "{\\2c&H%02x%02x%02x&}", m->c.hlit_color[2],
420 m->c.hlit_color[1], m->c.hlit_color[0]);
422 av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF&}");
425 if (text_pos == m->h.hlit_end) {
426 if (m->box_flags & HCLR_BOX) {
427 av_bprintf(buf, "{\\2c&H%X&}", RGB_TO_BGR(m->d.color));
429 av_bprintf(buf, "{\\1c&H%X&}{\\2c&H%X&}",
430 RGB_TO_BGR(color), RGB_TO_BGR(m->d.color));
435 len = get_utf8_length_at(text, text_end);
437 av_log(avctx, AV_LOG_ERROR, "invalid UTF-8 byte in subtitle\n");
440 for (i = 0; i < len; i++) {
445 av_bprintf(buf, "\\N");
448 av_bprint_chars(buf, *text, 1);
459 static int mov_text_init(AVCodecContext *avctx) {
461 * TODO: Handle the default text style.
462 * NB: Most players ignore styles completely, with the result that
463 * it's very common to find files where the default style is broken
464 * and respecting it results in a worse experience than ignoring it.
467 MovTextContext *m = avctx->priv_data;
468 ret = mov_text_tx3g(avctx, m);
470 if (!m->frame_width || !m->frame_height) {
471 m->frame_width = ASS_DEFAULT_PLAYRESX;
472 m->frame_height = ASS_DEFAULT_PLAYRESY;
474 return ff_ass_subtitle_header_full(avctx,
475 m->frame_width, m->frame_height,
476 m->d.font, m->d.fontsize,
477 (255U - m->d.alpha) << 24 | RGB_TO_BGR(m->d.color),
478 (255U - m->d.alpha) << 24 | RGB_TO_BGR(m->d.color),
479 (255U - m->d.back_alpha) << 24 | RGB_TO_BGR(m->d.back_color),
480 (255U - m->d.back_alpha) << 24 | RGB_TO_BGR(m->d.back_color),
481 m->d.bold, m->d.italic, m->d.underline,
482 ASS_DEFAULT_BORDERSTYLE, m->d.alignment);
484 return ff_ass_subtitle_header_default(avctx);
487 static int mov_text_decode_frame(AVCodecContext *avctx,
488 void *data, int *got_sub_ptr, AVPacket *avpkt)
490 AVSubtitle *sub = data;
491 MovTextContext *m = avctx->priv_data;
494 char *ptr = avpkt->data;
496 int text_length, tsmb_type, ret_tsmb;
501 if (!ptr || avpkt->size < 2)
502 return AVERROR_INVALIDDATA;
505 * A packet of size two with value zero is an empty subtitle
506 * used to mark the end of the previous non-empty subtitle.
507 * We can just drop them here as we have duration information
508 * already. If the value is non-zero, then it's technically a
511 if (avpkt->size == 2)
512 return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
515 * The first two bytes of the packet are the length of the text string
516 * In complex cases, there are style descriptors appended to the string
517 * so we can't just assume the packet size is the string size.
519 text_length = AV_RB16(ptr);
520 end = ptr + FFMIN(2 + text_length, avpkt->size);
526 m->tracksize = 2 + text_length;
527 m->style_entries = 0;
530 // Note that the spec recommends lines be no longer than 2048 characters.
531 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
532 if (text_length + 2 != avpkt->size) {
533 while (m->tracksize + 8 <= avpkt->size) {
534 // A box is a minimum of 8 bytes.
535 tsmb = ptr + m->tracksize - 2;
536 tsmb_size = AV_RB32(tsmb);
538 tsmb_type = AV_RB32(tsmb);
541 if (tsmb_size == 1) {
542 if (m->tracksize + 16 > avpkt->size)
544 tsmb_size = AV_RB64(tsmb);
549 //size_var is equal to 8 or 16 depending on the size of box
551 if (tsmb_size == 0) {
552 av_log(avctx, AV_LOG_ERROR, "tsmb_size is 0\n");
553 return AVERROR_INVALIDDATA;
556 if (tsmb_size > avpkt->size - m->tracksize)
559 for (i = 0; i < box_count; i++) {
560 if (tsmb_type == box_types[i].type) {
561 if (m->tracksize + m->size_var + box_types[i].base_size > avpkt->size)
563 ret_tsmb = box_types[i].decode(tsmb, m, avpkt);
568 m->tracksize = m->tracksize + tsmb_size;
570 text_to_ass(&buf, ptr, end, avctx);
573 text_to_ass(&buf, ptr, end, avctx);
575 ret = ff_ass_add_rect(sub, buf.str, m->readorder++, 0, NULL, NULL);
576 av_bprint_finalize(&buf, NULL);
579 *got_sub_ptr = sub->num_rects > 0;
583 static int mov_text_decode_close(AVCodecContext *avctx)
585 MovTextContext *m = avctx->priv_data;
586 mov_text_cleanup_ftab(m);
591 static void mov_text_flush(AVCodecContext *avctx)
593 MovTextContext *m = avctx->priv_data;
594 if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
598 #define OFFSET(x) offsetof(MovTextContext, x)
599 #define FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
600 static const AVOption options[] = {
601 { "width", "Frame width, usually video width", OFFSET(frame_width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
602 { "height", "Frame height, usually video height", OFFSET(frame_height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
606 static const AVClass mov_text_decoder_class = {
607 .class_name = "MOV text decoder",
608 .item_name = av_default_item_name,
610 .version = LIBAVUTIL_VERSION_INT,
613 AVCodec ff_movtext_decoder = {
615 .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
616 .type = AVMEDIA_TYPE_SUBTITLE,
617 .id = AV_CODEC_ID_MOV_TEXT,
618 .priv_data_size = sizeof(MovTextContext),
619 .priv_class = &mov_text_decoder_class,
620 .init = mov_text_init,
621 .decode = mov_text_decode_frame,
622 .close = mov_text_decode_close,
623 .flush = mov_text_flush,