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)
40 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
41 StyleBox **s, int style_entries)
45 while (text < text_end) {
46 for (i = 0; i < style_entries; i++) {
47 if (s[i]->style_flag && text_pos == s[i]->style_end) {
48 if (s[i]->style_flag & STYLE_FLAG_BOLD)
49 av_bprintf(buf, "{\\b0}");
50 if (s[i]->style_flag & STYLE_FLAG_ITALIC)
51 av_bprintf(buf, "{\\i0}");
52 if (s[i]->style_flag & STYLE_FLAG_UNDERLINE)
53 av_bprintf(buf, "{\\u0}");
57 for (i = 0; i < style_entries; i++) {
58 if (s[i]->style_flag && text_pos == s[i]->style_start) {
59 if (s[i]->style_flag & STYLE_FLAG_BOLD)
60 av_bprintf(buf, "{\\b1}");
61 if (s[i]->style_flag & STYLE_FLAG_ITALIC)
62 av_bprintf(buf, "{\\i1}");
63 if (s[i]->style_flag & STYLE_FLAG_UNDERLINE)
64 av_bprintf(buf, "{\\u1}");
72 av_bprintf(buf, "\\N");
75 av_bprint_chars(buf, *text, 1);
85 static int mov_text_init(AVCodecContext *avctx) {
87 * TODO: Handle the default text style.
88 * NB: Most players ignore styles completely, with the result that
89 * it's very common to find files where the default style is broken
90 * and respecting it results in a worse experience than ignoring it.
92 return ff_ass_subtitle_header_default(avctx);
95 static int mov_text_decode_frame(AVCodecContext *avctx,
96 void *data, int *got_sub_ptr, AVPacket *avpkt)
98 AVSubtitle *sub = data;
99 int ret, ts_start, ts_end;
101 char *ptr = avpkt->data;
104 int text_length, tsmb_type, style_entries;
105 uint64_t tsmb_size, tracksize;
106 StyleBox **s = {0, };
109 int count, i, size_var;
111 if (!ptr || avpkt->size < 2)
112 return AVERROR_INVALIDDATA;
115 * A packet of size two with value zero is an empty subtitle
116 * used to mark the end of the previous non-empty subtitle.
117 * We can just drop them here as we have duration information
118 * already. If the value is non-zero, then it's technically a
121 if (avpkt->size == 2)
122 return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
125 * The first two bytes of the packet are the length of the text string
126 * In complex cases, there are style descriptors appended to the string
127 * so we can't just assume the packet size is the string size.
129 text_length = AV_RB16(ptr);
130 end = ptr + FFMIN(2 + text_length, avpkt->size);
133 ts_start = av_rescale_q(avpkt->pts,
135 (AVRational){1,100});
136 ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
138 (AVRational){1,100});
141 tracksize = 2 + text_length;
142 // Note that the spec recommends lines be no longer than 2048 characters.
143 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
144 if (text_length + 2 != avpkt->size) {
145 while (tracksize + 8 <= avpkt->size) {
146 // A box is a minimum of 8 bytes.
147 tsmb = ptr + tracksize - 2;
148 tsmb_size = AV_RB32(tsmb);
150 tsmb_type = AV_RB32(tsmb);
153 if (tsmb_size == 1) {
154 if (tracksize + 16 > avpkt->size)
156 tsmb_size = AV_RB64(tsmb);
161 //size_var is equal to 10 or 18 depending on the size of box
163 if (tracksize + tsmb_size > avpkt->size)
166 if (tsmb_type == MKBETAG('s','t','y','l')) {
167 if (tracksize + size_var > avpkt->size)
169 style_entries = AV_RB16(tsmb);
172 // A single style record is of length 12 bytes.
173 if (tracksize + size_var + style_entries * 12 > avpkt->size)
177 for(i = 0; i < style_entries; i++) {
178 s_temp = av_malloc(sizeof(*s_temp));
182 s_temp->style_start = AV_RB16(tsmb);
184 s_temp->style_end = AV_RB16(tsmb);
186 // fontID = AV_RB16(tsmb);
188 s_temp->style_flag = AV_RB8(tsmb);
189 av_dynarray_add(&s, &count, s_temp);
192 //fontsize=AV_RB8(tsmb);
197 text_to_ass(&buf, ptr, end, s, style_entries);
199 for(i = 0; i < count; i++) {
204 tracksize = tracksize + tsmb_size;
207 text_to_ass(&buf, ptr, end, NULL, 0);
209 ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_end - ts_start);
210 av_bprint_finalize(&buf, NULL);
213 *got_sub_ptr = sub->num_rects > 0;
217 for(i = 0; i < count; i++) {
223 av_bprint_finalize(&buf, NULL);
224 return AVERROR(ENOMEM);
227 AVCodec ff_movtext_decoder = {
229 .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
230 .type = AVMEDIA_TYPE_SUBTITLE,
231 .id = AV_CODEC_ID_MOV_TEXT,
232 .init = mov_text_init,
233 .decode = mov_text_decode_frame,