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))
78 uint16_t style_fontID;
87 uint8_t hlit_color[4];
100 FontRecord *ftab_temp;
104 uint16_t style_entries, ftab_entries;
107 int count_s, count_f;
114 int (*decode)(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt);
117 static void mov_text_cleanup(MovTextContext *m)
120 if (m->box_flags & STYL_BOX) {
121 for(i = 0; i < m->count_s; i++) {
126 m->style_entries = 0;
130 static void mov_text_cleanup_ftab(MovTextContext *m)
134 av_freep(&m->ftab_temp->font);
135 av_freep(&m->ftab_temp);
137 for(i = 0; i < m->count_f; i++) {
138 av_freep(&m->ftab[i]->font);
139 av_freep(&m->ftab[i]);
145 static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m)
147 uint8_t *tx3g_ptr = avctx->extradata;
148 int i, box_size, font_length;
149 int8_t v_align, h_align;
154 box_size = BOX_SIZE_INITIAL; /* Size till ftab_entries */
155 if (avctx->extradata_size < box_size)
161 h_align = *tx3g_ptr++;
162 v_align = *tx3g_ptr++;
165 m->d.alignment = TOP_LEFT;
167 m->d.alignment = MIDDLE_LEFT;
169 m->d.alignment = BOTTOM_LEFT;
173 m->d.alignment = TOP_CENTER;
175 m->d.alignment = MIDDLE_CENTER;
177 m->d.alignment = BOTTOM_CENTER;
181 m->d.alignment = TOP_RIGHT;
183 m->d.alignment = MIDDLE_RIGHT;
185 m->d.alignment = BOTTOM_RIGHT;
188 m->d.back_color = AV_RB24(tx3g_ptr);
195 m->d.fontID = AV_RB16(tx3g_ptr);
198 s_default.style_flag = *tx3g_ptr++;
199 m->d.bold = !!(s_default.style_flag & STYLE_FLAG_BOLD);
200 m->d.italic = !!(s_default.style_flag & STYLE_FLAG_ITALIC);
201 m->d.underline = !!(s_default.style_flag & STYLE_FLAG_UNDERLINE);
203 m->d.fontsize = *tx3g_ptr++;
205 m->d.color = AV_RB24(tx3g_ptr);
213 m->ftab_entries = AV_RB16(tx3g_ptr);
216 for (i = 0; i < m->ftab_entries; i++) {
219 if (avctx->extradata_size < box_size) {
220 mov_text_cleanup_ftab(m);
224 m->ftab_temp = av_mallocz(sizeof(*m->ftab_temp));
226 mov_text_cleanup_ftab(m);
227 return AVERROR(ENOMEM);
229 m->ftab_temp->fontID = AV_RB16(tx3g_ptr);
231 font_length = *tx3g_ptr++;
233 box_size = box_size + font_length;
234 if (avctx->extradata_size < box_size) {
235 mov_text_cleanup_ftab(m);
239 m->ftab_temp->font = av_malloc(font_length + 1);
240 if (!m->ftab_temp->font) {
241 mov_text_cleanup_ftab(m);
242 return AVERROR(ENOMEM);
244 memcpy(m->ftab_temp->font, tx3g_ptr, font_length);
245 m->ftab_temp->font[font_length] = '\0';
246 av_dynarray_add(&m->ftab, &m->count_f, m->ftab_temp);
248 mov_text_cleanup_ftab(m);
249 return AVERROR(ENOMEM);
252 tx3g_ptr = tx3g_ptr + font_length;
254 // In case of broken header, init default font
255 m->d.font = ASS_DEFAULT_FONT;
256 for (i = 0; i < m->ftab_entries; i++) {
257 if (m->d.fontID == m->ftab[i]->fontID)
258 m->d.font = m->ftab[i]->font;
263 static int decode_twrp(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
265 m->box_flags |= TWRP_BOX;
266 m->w.wrap_flag = *tsmb++;
270 static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
272 m->box_flags |= HLIT_BOX;
273 m->h.hlit_start = AV_RB16(tsmb);
275 m->h.hlit_end = AV_RB16(tsmb);
280 static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
282 m->box_flags |= HCLR_BOX;
283 memcpy(m->c.hlit_color, tsmb, 4);
288 static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
291 int style_entries = AV_RB16(tsmb);
293 // A single style record is of length 12 bytes.
294 if (m->tracksize + m->size_var + 2 + style_entries * 12 > avpkt->size)
297 m->style_entries = style_entries;
299 m->box_flags |= STYL_BOX;
300 for(i = 0; i < m->style_entries; i++) {
301 m->s_temp = av_malloc(sizeof(*m->s_temp));
304 return AVERROR(ENOMEM);
306 m->s_temp->style_start = AV_RB16(tsmb);
308 m->s_temp->style_end = AV_RB16(tsmb);
310 if ( m->s_temp->style_end < m->s_temp->style_start
311 || (m->count_s && m->s_temp->style_start < m->s[m->count_s - 1]->style_end)) {
312 av_freep(&m->s_temp);
314 return AVERROR(ENOMEM);
318 m->s_temp->style_fontID = AV_RB16(tsmb);
320 m->s_temp->style_flag = AV_RB8(tsmb);
321 m->s_temp->bold = !!(m->s_temp->style_flag & STYLE_FLAG_BOLD);
322 m->s_temp->italic = !!(m->s_temp->style_flag & STYLE_FLAG_ITALIC);
323 m->s_temp->underline = !!(m->s_temp->style_flag & STYLE_FLAG_UNDERLINE);
325 m->s_temp->fontsize = AV_RB8(tsmb);
326 av_dynarray_add(&m->s, &m->count_s, m->s_temp);
329 return AVERROR(ENOMEM);
338 static const Box box_types[] = {
339 { MKBETAG('s','t','y','l'), 2, decode_styl },
340 { MKBETAG('h','l','i','t'), 4, decode_hlit },
341 { MKBETAG('h','c','l','r'), 4, decode_hclr },
342 { MKBETAG('t','w','r','p'), 1, decode_twrp }
345 const static size_t box_count = FF_ARRAY_ELEMS(box_types);
347 // Return byte length of the UTF-8 sequence starting at text[0]. 0 on error.
348 static int get_utf8_length_at(const char *text, const char *text_end)
350 const char *start = text;
353 GET_UTF8(c, text < text_end ? (uint8_t)*text++ : (err = 1, 0), goto error;);
361 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
362 AVCodecContext *avctx)
364 MovTextContext *m = avctx->priv_data;
367 int style_active = 0;
370 if (text < text_end && m->box_flags & TWRP_BOX) {
371 if (m->w.wrap_flag == 1) {
372 av_bprintf(buf, "{\\q1}"); /* End of line wrap */
374 av_bprintf(buf, "{\\q2}"); /* No wrap */
378 while (text < text_end) {
381 if ((m->box_flags & STYL_BOX) && entry < m->style_entries) {
382 if (text_pos == m->s[entry]->style_start) {
384 if (m->s[entry]->bold ^ m->d.bold)
385 av_bprintf(buf, "{\\b%d}", m->s[entry]->bold);
386 if (m->s[entry]->italic ^ m->d.italic)
387 av_bprintf(buf, "{\\i%d}", m->s[entry]->italic);
388 if (m->s[entry]->underline ^ m->d.underline)
389 av_bprintf(buf, "{\\u%d}", m->s[entry]->underline);
390 if (m->s[entry]->fontsize != m->d.fontsize)
391 av_bprintf(buf, "{\\fs%d}", m->s[entry]->fontsize);
392 if (m->s[entry]->style_fontID != m->d.fontID)
393 for (i = 0; i < m->ftab_entries; i++) {
394 if (m->s[entry]->style_fontID == m->ftab[i]->fontID)
395 av_bprintf(buf, "{\\fn%s}", m->ftab[i]->font);
398 if (text_pos == m->s[entry]->style_end) {
400 av_bprintf(buf, "{\\r}");
406 if (m->box_flags & HLIT_BOX) {
407 if (text_pos == m->h.hlit_start) {
408 /* If hclr box is present, set the secondary color to the color
409 * specified. Otherwise, set primary color to white and secondary
410 * color to black. These colors will come from TextSampleModifier
411 * boxes in future and inverse video technique for highlight will
414 if (m->box_flags & HCLR_BOX) {
415 av_bprintf(buf, "{\\2c&H%02x%02x%02x&}", m->c.hlit_color[2],
416 m->c.hlit_color[1], m->c.hlit_color[0]);
418 av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF&}");
421 if (text_pos == m->h.hlit_end) {
422 if (m->box_flags & HCLR_BOX) {
423 av_bprintf(buf, "{\\2c&H000000&}");
425 av_bprintf(buf, "{\\1c&HFFFFFF&}{\\2c&H000000&}");
430 len = get_utf8_length_at(text, text_end);
432 av_log(avctx, AV_LOG_ERROR, "invalid UTF-8 byte in subtitle\n");
435 for (i = 0; i < len; i++) {
440 av_bprintf(buf, "\\N");
443 av_bprint_chars(buf, *text, 1);
454 static int mov_text_init(AVCodecContext *avctx) {
456 * TODO: Handle the default text style.
457 * NB: Most players ignore styles completely, with the result that
458 * it's very common to find files where the default style is broken
459 * and respecting it results in a worse experience than ignoring it.
462 MovTextContext *m = avctx->priv_data;
463 ret = mov_text_tx3g(avctx, m);
465 return ff_ass_subtitle_header(avctx, m->d.font, m->d.fontsize,
466 RGB_TO_BGR(m->d.color),
467 RGB_TO_BGR(m->d.back_color),
468 m->d.bold, m->d.italic, m->d.underline,
469 ASS_DEFAULT_BORDERSTYLE, m->d.alignment);
471 return ff_ass_subtitle_header_default(avctx);
474 static int mov_text_decode_frame(AVCodecContext *avctx,
475 void *data, int *got_sub_ptr, AVPacket *avpkt)
477 AVSubtitle *sub = data;
478 MovTextContext *m = avctx->priv_data;
481 char *ptr = avpkt->data;
483 int text_length, tsmb_type, ret_tsmb;
488 if (!ptr || avpkt->size < 2)
489 return AVERROR_INVALIDDATA;
492 * A packet of size two with value zero is an empty subtitle
493 * used to mark the end of the previous non-empty subtitle.
494 * We can just drop them here as we have duration information
495 * already. If the value is non-zero, then it's technically a
498 if (avpkt->size == 2)
499 return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
502 * The first two bytes of the packet are the length of the text string
503 * In complex cases, there are style descriptors appended to the string
504 * so we can't just assume the packet size is the string size.
506 text_length = AV_RB16(ptr);
507 end = ptr + FFMIN(2 + text_length, avpkt->size);
513 m->tracksize = 2 + text_length;
514 m->style_entries = 0;
517 // Note that the spec recommends lines be no longer than 2048 characters.
518 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
519 if (text_length + 2 != avpkt->size) {
520 while (m->tracksize + 8 <= avpkt->size) {
521 // A box is a minimum of 8 bytes.
522 tsmb = ptr + m->tracksize - 2;
523 tsmb_size = AV_RB32(tsmb);
525 tsmb_type = AV_RB32(tsmb);
528 if (tsmb_size == 1) {
529 if (m->tracksize + 16 > avpkt->size)
531 tsmb_size = AV_RB64(tsmb);
536 //size_var is equal to 8 or 16 depending on the size of box
538 if (tsmb_size == 0) {
539 av_log(avctx, AV_LOG_ERROR, "tsmb_size is 0\n");
540 return AVERROR_INVALIDDATA;
543 if (tsmb_size > avpkt->size - m->tracksize)
546 for (i = 0; i < box_count; i++) {
547 if (tsmb_type == box_types[i].type) {
548 if (m->tracksize + m->size_var + box_types[i].base_size > avpkt->size)
550 ret_tsmb = box_types[i].decode(tsmb, m, avpkt);
555 m->tracksize = m->tracksize + tsmb_size;
557 text_to_ass(&buf, ptr, end, avctx);
560 text_to_ass(&buf, ptr, end, avctx);
562 ret = ff_ass_add_rect(sub, buf.str, m->readorder++, 0, NULL, NULL);
563 av_bprint_finalize(&buf, NULL);
566 *got_sub_ptr = sub->num_rects > 0;
570 static int mov_text_decode_close(AVCodecContext *avctx)
572 MovTextContext *m = avctx->priv_data;
573 mov_text_cleanup_ftab(m);
578 static void mov_text_flush(AVCodecContext *avctx)
580 MovTextContext *m = avctx->priv_data;
581 if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
585 AVCodec ff_movtext_decoder = {
587 .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
588 .type = AVMEDIA_TYPE_SUBTITLE,
589 .id = AV_CODEC_ID_MOV_TEXT,
590 .priv_data_size = sizeof(MovTextContext),
591 .init = mov_text_init,
592 .decode = mov_text_decode_frame,
593 .close = mov_text_decode_close,
594 .flush = mov_text_flush,