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
31 #define STYLE_FLAG_ITALIC 2
32 #define STYLE_FLAG_UNDERLINE 4
34 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
35 int **style_start, int **style_end,
36 int **style_flags, int style_entries)
40 while (text < text_end) {
41 for (i = 0; i < style_entries; i++) {
42 if (*style_flags[i] && style_pos == *style_start[i]) {
43 if (*style_flags[i] & STYLE_FLAG_BOLD)
44 av_bprintf(buf, "{\\b1}");
45 if (*style_flags[i] & STYLE_FLAG_ITALIC)
46 av_bprintf(buf, "{\\i1}");
47 if (*style_flags[i] & STYLE_FLAG_UNDERLINE)
48 av_bprintf(buf, "{\\u1}");
56 av_bprintf(buf, "\\N");
59 av_bprint_chars(buf, *text, 1);
63 for (i = 0; i < style_entries; i++) {
64 if (*style_flags[i] && style_pos == *style_end[i]) {
65 if (*style_flags[i] & STYLE_FLAG_BOLD)
66 av_bprintf(buf, "{\\b0}");
67 if (*style_flags[i] & STYLE_FLAG_ITALIC)
68 av_bprintf(buf, "{\\i0}");
69 if (*style_flags[i] & STYLE_FLAG_UNDERLINE)
70 av_bprintf(buf, "{\\u0}");
80 static int mov_text_init(AVCodecContext *avctx) {
82 * TODO: Handle the default text style.
83 * NB: Most players ignore styles completely, with the result that
84 * it's very common to find files where the default style is broken
85 * and respecting it results in a worse experience than ignoring it.
87 return ff_ass_subtitle_header_default(avctx);
90 static int mov_text_decode_frame(AVCodecContext *avctx,
91 void *data, int *got_sub_ptr, AVPacket *avpkt)
93 AVSubtitle *sub = data;
94 int ret, ts_start, ts_end;
96 char *ptr = avpkt->data;
99 int text_length, tsmb_type, style_entries, tsmb_size;
100 int **style_start = {0,};
101 int **style_end = {0,};
102 int **style_flags = {0,};
108 if (!ptr || avpkt->size < 2)
109 return AVERROR_INVALIDDATA;
112 * A packet of size two with value zero is an empty subtitle
113 * used to mark the end of the previous non-empty subtitle.
114 * We can just drop them here as we have duration information
115 * already. If the value is non-zero, then it's technically a
118 if (avpkt->size == 2)
119 return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
122 * The first two bytes of the packet are the length of the text string
123 * In complex cases, there are style descriptors appended to the string
124 * so we can't just assume the packet size is the string size.
126 text_length = AV_RB16(ptr);
127 end = ptr + FFMIN(2 + text_length, avpkt->size);
130 ts_start = av_rescale_q(avpkt->pts,
132 (AVRational){1,100});
133 ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
135 (AVRational){1,100});
138 // Note that the spec recommends lines be no longer than 2048 characters.
139 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
140 if (text_length + 2 != avpkt->size) {
141 while (text_length + 2 + tsmb_size < avpkt->size) {
142 tsmb = ptr + text_length + tsmb_size;
143 tsmb_size = AV_RB32(tsmb);
145 tsmb_type = AV_RB32(tsmb);
148 if (tsmb_type == MKBETAG('s','t','y','l')) {
149 style_entries = AV_RB16(tsmb);
152 for(i = 0; i < style_entries; i++) {
153 style_pos = av_malloc(4);
154 *style_pos = AV_RB16(tsmb);
156 av_dynarray_add(&style_start, &index, style_pos);
158 style_pos = av_malloc(4);
159 *style_pos = AV_RB16(tsmb);
161 av_dynarray_add(&style_end, &index, style_pos);
163 // fontID = AV_RB16(tsmb);
166 *flag = AV_RB8(tsmb);
168 av_dynarray_add(&style_flags, &index, flag);
169 //fontsize=AV_RB8(tsmb);
174 text_to_ass(&buf, ptr, end, style_start, style_end, style_flags, style_entries);
175 av_freep(&style_start);
176 av_freep(&style_end);
177 av_freep(&style_flags);
181 text_to_ass(&buf, ptr, end, NULL, NULL, 0, 0);
183 ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_end - ts_start);
184 av_bprint_finalize(&buf, NULL);
187 *got_sub_ptr = sub->num_rects > 0;
191 AVCodec ff_movtext_decoder = {
193 .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
194 .type = AVMEDIA_TYPE_SUBTITLE,
195 .id = AV_CODEC_ID_MOV_TEXT,
196 .init = mov_text_init,
197 .decode = mov_text_decode_frame,