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 STYL_BOX (1<<0)
35 #define HLIT_BOX (1<<1)
36 #define HCLR_BOX (1<<2)
51 uint8_t hlit_color[4];
60 uint16_t style_entries;
69 int (*decode)(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt);
72 static void mov_text_cleanup(MovTextContext *m)
75 if (m->box_flags & STYL_BOX) {
76 for(i = 0; i < m->count_s; i++) {
83 static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
85 m->box_flags |= HLIT_BOX;
86 m->h.hlit_start = AV_RB16(tsmb);
88 m->h.hlit_end = AV_RB16(tsmb);
93 static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
95 m->box_flags |= HCLR_BOX;
96 memcpy(m->c.hlit_color, tsmb, 4);
101 static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
104 m->style_entries = AV_RB16(tsmb);
106 // A single style record is of length 12 bytes.
107 if (m->tracksize + m->size_var + 2 + m->style_entries * 12 > avpkt->size)
110 m->box_flags |= STYL_BOX;
111 for(i = 0; i < m->style_entries; i++) {
112 m->s_temp = av_malloc(sizeof(*m->s_temp));
115 return AVERROR(ENOMEM);
117 m->s_temp->style_start = AV_RB16(tsmb);
119 m->s_temp->style_end = AV_RB16(tsmb);
121 // fontID = AV_RB16(tsmb);
123 m->s_temp->style_flag = AV_RB8(tsmb);
125 m->s_temp->fontsize = AV_RB8(tsmb);
126 av_dynarray_add(&m->s, &m->count_s, m->s_temp);
129 return AVERROR(ENOMEM);
138 static const Box box_types[] = {
139 { MKBETAG('s','t','y','l'), 2, decode_styl },
140 { MKBETAG('h','l','i','t'), 4, decode_hlit },
141 { MKBETAG('h','c','l','r'), 4, decode_hclr }
144 const static size_t box_count = FF_ARRAY_ELEMS(box_types);
146 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
151 while (text < text_end) {
152 if (m->box_flags & STYL_BOX) {
153 for (i = 0; i < m->style_entries; i++) {
154 if (m->s[i]->style_flag && text_pos == m->s[i]->style_end) {
155 av_bprintf(buf, "{\\r}");
158 for (i = 0; i < m->style_entries; i++) {
159 if (m->s[i]->style_flag && text_pos == m->s[i]->style_start) {
160 if (m->s[i]->style_flag & STYLE_FLAG_BOLD)
161 av_bprintf(buf, "{\\b1}");
162 if (m->s[i]->style_flag & STYLE_FLAG_ITALIC)
163 av_bprintf(buf, "{\\i1}");
164 if (m->s[i]->style_flag & STYLE_FLAG_UNDERLINE)
165 av_bprintf(buf, "{\\u1}");
166 av_bprintf(buf, "{\\fs%d}", m->s[i]->fontsize);
170 if (m->box_flags & HLIT_BOX) {
171 if (text_pos == m->h.hlit_start) {
172 /* If hclr box is present, set the secondary color to the color
173 * specified. Otherwise, set primary color to white and secondary
174 * color to black. These colors will come from TextSampleModifier
175 * boxes in future and inverse video technique for highlight will
178 if (m->box_flags & HCLR_BOX) {
179 av_bprintf(buf, "{\\2c&H%02x%02x%02x&}", m->c.hlit_color[2],
180 m->c.hlit_color[1], m->c.hlit_color[0]);
182 av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF&}");
185 if (text_pos == m->h.hlit_end) {
186 if (m->box_flags & HCLR_BOX) {
187 av_bprintf(buf, "{\\2c&H000000&}");
189 av_bprintf(buf, "{\\1c&HFFFFFF&}{\\2c&H000000&}");
198 av_bprintf(buf, "\\N");
201 av_bprint_chars(buf, *text, 1);
211 static int mov_text_init(AVCodecContext *avctx) {
213 * TODO: Handle the default text style.
214 * NB: Most players ignore styles completely, with the result that
215 * it's very common to find files where the default style is broken
216 * and respecting it results in a worse experience than ignoring it.
218 return ff_ass_subtitle_header_default(avctx);
221 static int mov_text_decode_frame(AVCodecContext *avctx,
222 void *data, int *got_sub_ptr, AVPacket *avpkt)
224 AVSubtitle *sub = data;
225 MovTextContext *m = avctx->priv_data;
226 int ret, ts_start, ts_end;
228 char *ptr = avpkt->data;
230 int text_length, tsmb_type, ret_tsmb;
234 if (!ptr || avpkt->size < 2)
235 return AVERROR_INVALIDDATA;
238 * A packet of size two with value zero is an empty subtitle
239 * used to mark the end of the previous non-empty subtitle.
240 * We can just drop them here as we have duration information
241 * already. If the value is non-zero, then it's technically a
244 if (avpkt->size == 2)
245 return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
248 * The first two bytes of the packet are the length of the text string
249 * In complex cases, there are style descriptors appended to the string
250 * so we can't just assume the packet size is the string size.
252 text_length = AV_RB16(ptr);
253 end = ptr + FFMIN(2 + text_length, avpkt->size);
256 ts_start = av_rescale_q(avpkt->pts,
258 (AVRational){1,100});
259 ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
261 (AVRational){1,100});
264 m->tracksize = 2 + text_length;
265 m->style_entries = 0;
268 // Note that the spec recommends lines be no longer than 2048 characters.
269 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
270 if (text_length + 2 != avpkt->size) {
271 while (m->tracksize + 8 <= avpkt->size) {
272 // A box is a minimum of 8 bytes.
273 tsmb = ptr + m->tracksize - 2;
274 tsmb_size = AV_RB32(tsmb);
276 tsmb_type = AV_RB32(tsmb);
279 if (tsmb_size == 1) {
280 if (m->tracksize + 16 > avpkt->size)
282 tsmb_size = AV_RB64(tsmb);
287 //size_var is equal to 8 or 16 depending on the size of box
289 if (m->tracksize + tsmb_size > avpkt->size)
292 for (size_t i = 0; i < box_count; i++) {
293 if (tsmb_type == box_types[i].type) {
294 if (m->tracksize + m->size_var + box_types[i].base_size > avpkt->size)
296 ret_tsmb = box_types[i].decode(tsmb, m, avpkt);
301 m->tracksize = m->tracksize + tsmb_size;
303 text_to_ass(&buf, ptr, end, m);
306 text_to_ass(&buf, ptr, end, m);
308 ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_end - ts_start);
309 av_bprint_finalize(&buf, NULL);
312 *got_sub_ptr = sub->num_rects > 0;
316 AVCodec ff_movtext_decoder = {
318 .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
319 .type = AVMEDIA_TYPE_SUBTITLE,
320 .id = AV_CODEC_ID_MOV_TEXT,
321 .priv_data_size = sizeof(MovTextContext),
322 .init = mov_text_init,
323 .decode = mov_text_decode_frame,