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/avstring.h"
25 #include "libavutil/common.h"
26 #include "libavutil/bprint.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mem.h"
30 #define STYLE_FLAG_BOLD (1<<0)
31 #define STYLE_FLAG_ITALIC (1<<1)
32 #define STYLE_FLAG_UNDERLINE (1<<2)
34 #define BOX_SIZE_INITIAL 40
36 #define STYL_BOX (1<<0)
37 #define HLIT_BOX (1<<1)
38 #define HCLR_BOX (1<<2)
39 #define TWRP_BOX (1<<3)
42 #define BOTTOM_CENTER 2
43 #define BOTTOM_RIGHT 3
45 #define MIDDLE_CENTER 5
46 #define MIDDLE_RIGHT 6
51 #define RGB_TO_BGR(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((c) >> 16) & 0xff))
80 uint16_t style_fontID;
89 uint8_t hlit_color[4];
102 FontRecord *ftab_temp;
106 uint16_t style_entries, ftab_entries;
109 int count_s, count_f;
116 int (*decode)(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt);
119 static void mov_text_cleanup(MovTextContext *m)
122 if (m->box_flags & STYL_BOX) {
123 for(i = 0; i < m->count_s; i++) {
128 m->style_entries = 0;
132 static void mov_text_cleanup_ftab(MovTextContext *m)
136 av_freep(&m->ftab_temp->font);
137 av_freep(&m->ftab_temp);
139 for(i = 0; i < m->count_f; i++) {
140 av_freep(&m->ftab[i]->font);
141 av_freep(&m->ftab[i]);
147 static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m)
149 uint8_t *tx3g_ptr = avctx->extradata;
150 int i, box_size, font_length;
151 int8_t v_align, h_align;
156 box_size = BOX_SIZE_INITIAL; /* Size till ftab_entries */
157 if (avctx->extradata_size < box_size)
163 h_align = *tx3g_ptr++;
164 v_align = *tx3g_ptr++;
167 m->d.alignment = TOP_LEFT;
169 m->d.alignment = MIDDLE_LEFT;
171 m->d.alignment = BOTTOM_LEFT;
175 m->d.alignment = TOP_CENTER;
177 m->d.alignment = MIDDLE_CENTER;
179 m->d.alignment = BOTTOM_CENTER;
183 m->d.alignment = TOP_RIGHT;
185 m->d.alignment = MIDDLE_RIGHT;
187 m->d.alignment = BOTTOM_RIGHT;
190 m->d.back_color = AV_RB24(tx3g_ptr);
192 m->d.back_alpha = AV_RB8(tx3g_ptr);
199 m->d.fontID = AV_RB16(tx3g_ptr);
202 s_default.style_flag = *tx3g_ptr++;
203 m->d.bold = !!(s_default.style_flag & STYLE_FLAG_BOLD);
204 m->d.italic = !!(s_default.style_flag & STYLE_FLAG_ITALIC);
205 m->d.underline = !!(s_default.style_flag & STYLE_FLAG_UNDERLINE);
207 m->d.fontsize = *tx3g_ptr++;
209 m->d.color = AV_RB24(tx3g_ptr);
211 m->d.alpha = AV_RB8(tx3g_ptr);
219 m->ftab_entries = AV_RB16(tx3g_ptr);
222 for (i = 0; i < m->ftab_entries; i++) {
225 if (avctx->extradata_size < box_size) {
226 mov_text_cleanup_ftab(m);
230 m->ftab_temp = av_mallocz(sizeof(*m->ftab_temp));
232 mov_text_cleanup_ftab(m);
233 return AVERROR(ENOMEM);
235 m->ftab_temp->fontID = AV_RB16(tx3g_ptr);
237 font_length = *tx3g_ptr++;
239 box_size = box_size + font_length;
240 if (avctx->extradata_size < box_size) {
241 mov_text_cleanup_ftab(m);
245 m->ftab_temp->font = av_malloc(font_length + 1);
246 if (!m->ftab_temp->font) {
247 mov_text_cleanup_ftab(m);
248 return AVERROR(ENOMEM);
250 memcpy(m->ftab_temp->font, tx3g_ptr, font_length);
251 m->ftab_temp->font[font_length] = '\0';
252 av_dynarray_add(&m->ftab, &m->count_f, m->ftab_temp);
254 mov_text_cleanup_ftab(m);
255 return AVERROR(ENOMEM);
258 tx3g_ptr = tx3g_ptr + font_length;
260 // In case of broken header, init default font
261 m->d.font = ASS_DEFAULT_FONT;
262 for (i = 0; i < m->ftab_entries; i++) {
263 if (m->d.fontID == m->ftab[i]->fontID)
264 m->d.font = m->ftab[i]->font;
269 static int decode_twrp(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
271 m->box_flags |= TWRP_BOX;
272 m->w.wrap_flag = *tsmb++;
276 static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
278 m->box_flags |= HLIT_BOX;
279 m->h.hlit_start = AV_RB16(tsmb);
281 m->h.hlit_end = AV_RB16(tsmb);
286 static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
288 m->box_flags |= HCLR_BOX;
289 memcpy(m->c.hlit_color, tsmb, 4);
294 static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
297 int style_entries = AV_RB16(tsmb);
299 // A single style record is of length 12 bytes.
300 if (m->tracksize + m->size_var + 2 + style_entries * 12 > avpkt->size)
303 m->style_entries = style_entries;
305 m->box_flags |= STYL_BOX;
306 for(i = 0; i < m->style_entries; i++) {
307 m->s_temp = av_malloc(sizeof(*m->s_temp));
310 return AVERROR(ENOMEM);
312 m->s_temp->style_start = AV_RB16(tsmb);
314 m->s_temp->style_end = AV_RB16(tsmb);
316 if ( m->s_temp->style_end < m->s_temp->style_start
317 || (m->count_s && m->s_temp->style_start < m->s[m->count_s - 1]->style_end)) {
318 av_freep(&m->s_temp);
320 return AVERROR(ENOMEM);
324 m->s_temp->style_fontID = AV_RB16(tsmb);
326 m->s_temp->style_flag = AV_RB8(tsmb);
327 m->s_temp->bold = !!(m->s_temp->style_flag & STYLE_FLAG_BOLD);
328 m->s_temp->italic = !!(m->s_temp->style_flag & STYLE_FLAG_ITALIC);
329 m->s_temp->underline = !!(m->s_temp->style_flag & STYLE_FLAG_UNDERLINE);
331 m->s_temp->fontsize = AV_RB8(tsmb);
332 av_dynarray_add(&m->s, &m->count_s, m->s_temp);
335 return AVERROR(ENOMEM);
344 static const Box box_types[] = {
345 { MKBETAG('s','t','y','l'), 2, decode_styl },
346 { MKBETAG('h','l','i','t'), 4, decode_hlit },
347 { MKBETAG('h','c','l','r'), 4, decode_hclr },
348 { MKBETAG('t','w','r','p'), 1, decode_twrp }
351 const static size_t box_count = FF_ARRAY_ELEMS(box_types);
353 // Return byte length of the UTF-8 sequence starting at text[0]. 0 on error.
354 static int get_utf8_length_at(const char *text, const char *text_end)
356 const char *start = text;
359 GET_UTF8(c, text < text_end ? (uint8_t)*text++ : (err = 1, 0), goto error;);
367 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
368 AVCodecContext *avctx)
370 MovTextContext *m = avctx->priv_data;
373 int style_active = 0;
376 if (text < text_end && m->box_flags & TWRP_BOX) {
377 if (m->w.wrap_flag == 1) {
378 av_bprintf(buf, "{\\q1}"); /* End of line wrap */
380 av_bprintf(buf, "{\\q2}"); /* No wrap */
384 while (text < text_end) {
387 if ((m->box_flags & STYL_BOX) && entry < m->style_entries) {
388 if (text_pos == m->s[entry]->style_start) {
390 if (m->s[entry]->bold ^ m->d.bold)
391 av_bprintf(buf, "{\\b%d}", m->s[entry]->bold);
392 if (m->s[entry]->italic ^ m->d.italic)
393 av_bprintf(buf, "{\\i%d}", m->s[entry]->italic);
394 if (m->s[entry]->underline ^ m->d.underline)
395 av_bprintf(buf, "{\\u%d}", m->s[entry]->underline);
396 if (m->s[entry]->fontsize != m->d.fontsize)
397 av_bprintf(buf, "{\\fs%d}", m->s[entry]->fontsize);
398 if (m->s[entry]->style_fontID != m->d.fontID)
399 for (i = 0; i < m->ftab_entries; i++) {
400 if (m->s[entry]->style_fontID == m->ftab[i]->fontID)
401 av_bprintf(buf, "{\\fn%s}", m->ftab[i]->font);
404 if (text_pos == m->s[entry]->style_end) {
406 av_bprintf(buf, "{\\r}");
412 if (m->box_flags & HLIT_BOX) {
413 if (text_pos == m->h.hlit_start) {
414 /* If hclr box is present, set the secondary color to the color
415 * specified. Otherwise, set primary color to white and secondary
416 * color to black. These colors will come from TextSampleModifier
417 * boxes in future and inverse video technique for highlight will
420 if (m->box_flags & HCLR_BOX) {
421 av_bprintf(buf, "{\\2c&H%02x%02x%02x&}", m->c.hlit_color[2],
422 m->c.hlit_color[1], m->c.hlit_color[0]);
424 av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF&}");
427 if (text_pos == m->h.hlit_end) {
428 if (m->box_flags & HCLR_BOX) {
429 av_bprintf(buf, "{\\2c&H000000&}");
431 av_bprintf(buf, "{\\1c&HFFFFFF&}{\\2c&H000000&}");
436 len = get_utf8_length_at(text, text_end);
438 av_log(avctx, AV_LOG_ERROR, "invalid UTF-8 byte in subtitle\n");
441 for (i = 0; i < len; i++) {
446 av_bprintf(buf, "\\N");
449 av_bprint_chars(buf, *text, 1);
460 static int mov_text_init(AVCodecContext *avctx) {
462 * TODO: Handle the default text style.
463 * NB: Most players ignore styles completely, with the result that
464 * it's very common to find files where the default style is broken
465 * and respecting it results in a worse experience than ignoring it.
468 MovTextContext *m = avctx->priv_data;
469 ret = mov_text_tx3g(avctx, m);
471 return ff_ass_subtitle_header(avctx, m->d.font, m->d.fontsize,
472 (255 - m->d.alpha) << 24 | RGB_TO_BGR(m->d.color),
473 (255 - m->d.back_alpha) << 24 | RGB_TO_BGR(m->d.back_color),
474 m->d.bold, m->d.italic, m->d.underline,
475 ASS_DEFAULT_BORDERSTYLE, m->d.alignment);
477 return ff_ass_subtitle_header_default(avctx);
480 static int mov_text_decode_frame(AVCodecContext *avctx,
481 void *data, int *got_sub_ptr, AVPacket *avpkt)
483 AVSubtitle *sub = data;
484 MovTextContext *m = avctx->priv_data;
487 char *ptr = avpkt->data;
489 int text_length, tsmb_type, ret_tsmb;
494 if (!ptr || avpkt->size < 2)
495 return AVERROR_INVALIDDATA;
498 * A packet of size two with value zero is an empty subtitle
499 * used to mark the end of the previous non-empty subtitle.
500 * We can just drop them here as we have duration information
501 * already. If the value is non-zero, then it's technically a
504 if (avpkt->size == 2)
505 return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
508 * The first two bytes of the packet are the length of the text string
509 * In complex cases, there are style descriptors appended to the string
510 * so we can't just assume the packet size is the string size.
512 text_length = AV_RB16(ptr);
513 end = ptr + FFMIN(2 + text_length, avpkt->size);
519 m->tracksize = 2 + text_length;
520 m->style_entries = 0;
523 // Note that the spec recommends lines be no longer than 2048 characters.
524 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
525 if (text_length + 2 != avpkt->size) {
526 while (m->tracksize + 8 <= avpkt->size) {
527 // A box is a minimum of 8 bytes.
528 tsmb = ptr + m->tracksize - 2;
529 tsmb_size = AV_RB32(tsmb);
531 tsmb_type = AV_RB32(tsmb);
534 if (tsmb_size == 1) {
535 if (m->tracksize + 16 > avpkt->size)
537 tsmb_size = AV_RB64(tsmb);
542 //size_var is equal to 8 or 16 depending on the size of box
544 if (tsmb_size == 0) {
545 av_log(avctx, AV_LOG_ERROR, "tsmb_size is 0\n");
546 return AVERROR_INVALIDDATA;
549 if (tsmb_size > avpkt->size - m->tracksize)
552 for (i = 0; i < box_count; i++) {
553 if (tsmb_type == box_types[i].type) {
554 if (m->tracksize + m->size_var + box_types[i].base_size > avpkt->size)
556 ret_tsmb = box_types[i].decode(tsmb, m, avpkt);
561 m->tracksize = m->tracksize + tsmb_size;
563 text_to_ass(&buf, ptr, end, avctx);
566 text_to_ass(&buf, ptr, end, avctx);
568 ret = ff_ass_add_rect(sub, buf.str, m->readorder++, 0, NULL, NULL);
569 av_bprint_finalize(&buf, NULL);
572 *got_sub_ptr = sub->num_rects > 0;
576 static int mov_text_decode_close(AVCodecContext *avctx)
578 MovTextContext *m = avctx->priv_data;
579 mov_text_cleanup_ftab(m);
584 static void mov_text_flush(AVCodecContext *avctx)
586 MovTextContext *m = avctx->priv_data;
587 if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
591 AVCodec ff_movtext_decoder = {
593 .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
594 .type = AVMEDIA_TYPE_SUBTITLE,
595 .id = AV_CODEC_ID_MOV_TEXT,
596 .priv_data_size = sizeof(MovTextContext),
597 .init = mov_text_init,
598 .decode = mov_text_decode_frame,
599 .close = mov_text_decode_close,
600 .flush = mov_text_flush,