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)
50 uint8_t hlit_color[4];
59 uint16_t style_entries;
68 int (*decode)(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt);
71 static void mov_text_cleanup(MovTextContext *m)
74 if (m->box_flags & STYL_BOX) {
75 for(i = 0; i < m->count_s; i++) {
82 static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
84 m->box_flags |= HLIT_BOX;
85 m->h.hlit_start = AV_RB16(tsmb);
87 m->h.hlit_end = AV_RB16(tsmb);
92 static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
94 m->box_flags |= HCLR_BOX;
95 memcpy(m->c.hlit_color, tsmb, 4);
100 static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
103 m->style_entries = AV_RB16(tsmb);
105 // A single style record is of length 12 bytes.
106 if (m->tracksize + m->size_var + 2 + m->style_entries * 12 > avpkt->size)
109 m->box_flags |= STYL_BOX;
110 for(i = 0; i < m->style_entries; i++) {
111 m->s_temp = av_malloc(sizeof(*m->s_temp));
114 return AVERROR(ENOMEM);
116 m->s_temp->style_start = AV_RB16(tsmb);
118 m->s_temp->style_end = AV_RB16(tsmb);
120 // fontID = AV_RB16(tsmb);
122 m->s_temp->style_flag = AV_RB8(tsmb);
123 av_dynarray_add(&m->s, &m->count_s, m->s_temp);
126 return AVERROR(ENOMEM);
128 // fontsize = AV_RB8(tsmb);
136 static const Box box_types[] = {
137 { MKBETAG('s','t','y','l'), 2, decode_styl },
138 { MKBETAG('h','l','i','t'), 4, decode_hlit },
139 { MKBETAG('h','c','l','r'), 4, decode_hclr }
142 const static size_t box_count = FF_ARRAY_ELEMS(box_types);
144 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
149 while (text < text_end) {
150 if (m->box_flags & STYL_BOX) {
151 for (i = 0; i < m->style_entries; i++) {
152 if (m->s[i]->style_flag && text_pos == m->s[i]->style_end) {
153 if (m->s[i]->style_flag & STYLE_FLAG_BOLD)
154 av_bprintf(buf, "{\\b0}");
155 if (m->s[i]->style_flag & STYLE_FLAG_ITALIC)
156 av_bprintf(buf, "{\\i0}");
157 if (m->s[i]->style_flag & STYLE_FLAG_UNDERLINE)
158 av_bprintf(buf, "{\\u0}");
161 for (i = 0; i < m->style_entries; i++) {
162 if (m->s[i]->style_flag && text_pos == m->s[i]->style_start) {
163 if (m->s[i]->style_flag & STYLE_FLAG_BOLD)
164 av_bprintf(buf, "{\\b1}");
165 if (m->s[i]->style_flag & STYLE_FLAG_ITALIC)
166 av_bprintf(buf, "{\\i1}");
167 if (m->s[i]->style_flag & STYLE_FLAG_UNDERLINE)
168 av_bprintf(buf, "{\\u1}");
172 if (m->box_flags & HLIT_BOX) {
173 if (text_pos == m->h.hlit_start) {
174 /* If hclr box is present, set the secondary color to the color
175 * specified. Otherwise, set primary color to white and secondary
176 * color to black. These colors will come from TextSampleModifier
177 * boxes in future and inverse video technique for highlight will
180 if (m->box_flags & HCLR_BOX) {
181 av_bprintf(buf, "{\\2c&H%02x%02x%02x&}", m->c.hlit_color[2],
182 m->c.hlit_color[1], m->c.hlit_color[0]);
184 av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF&}");
187 if (text_pos == m->h.hlit_end) {
188 if (m->box_flags & HCLR_BOX) {
189 av_bprintf(buf, "{\\2c&H000000&}");
191 av_bprintf(buf, "{\\1c&HFFFFFF&}{\\2c&H000000&}");
200 av_bprintf(buf, "\\N");
203 av_bprint_chars(buf, *text, 1);
213 static int mov_text_init(AVCodecContext *avctx) {
215 * TODO: Handle the default text style.
216 * NB: Most players ignore styles completely, with the result that
217 * it's very common to find files where the default style is broken
218 * and respecting it results in a worse experience than ignoring it.
220 return ff_ass_subtitle_header_default(avctx);
223 static int mov_text_decode_frame(AVCodecContext *avctx,
224 void *data, int *got_sub_ptr, AVPacket *avpkt)
226 AVSubtitle *sub = data;
227 MovTextContext *m = avctx->priv_data;
228 int ret, ts_start, ts_end;
230 char *ptr = avpkt->data;
232 int text_length, tsmb_type, ret_tsmb;
236 if (!ptr || avpkt->size < 2)
237 return AVERROR_INVALIDDATA;
240 * A packet of size two with value zero is an empty subtitle
241 * used to mark the end of the previous non-empty subtitle.
242 * We can just drop them here as we have duration information
243 * already. If the value is non-zero, then it's technically a
246 if (avpkt->size == 2)
247 return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
250 * The first two bytes of the packet are the length of the text string
251 * In complex cases, there are style descriptors appended to the string
252 * so we can't just assume the packet size is the string size.
254 text_length = AV_RB16(ptr);
255 end = ptr + FFMIN(2 + text_length, avpkt->size);
258 ts_start = av_rescale_q(avpkt->pts,
260 (AVRational){1,100});
261 ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
263 (AVRational){1,100});
266 m->tracksize = 2 + text_length;
267 m->style_entries = 0;
270 // Note that the spec recommends lines be no longer than 2048 characters.
271 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
272 if (text_length + 2 != avpkt->size) {
273 while (m->tracksize + 8 <= avpkt->size) {
274 // A box is a minimum of 8 bytes.
275 tsmb = ptr + m->tracksize - 2;
276 tsmb_size = AV_RB32(tsmb);
278 tsmb_type = AV_RB32(tsmb);
281 if (tsmb_size == 1) {
282 if (m->tracksize + 16 > avpkt->size)
284 tsmb_size = AV_RB64(tsmb);
289 //size_var is equal to 8 or 16 depending on the size of box
291 if (m->tracksize + tsmb_size > avpkt->size)
294 for (size_t i = 0; i < box_count; i++) {
295 if (tsmb_type == box_types[i].type) {
296 if (m->tracksize + m->size_var + box_types[i].base_size > avpkt->size)
298 ret_tsmb = box_types[i].decode(tsmb, m, avpkt);
303 m->tracksize = m->tracksize + tsmb_size;
305 text_to_ass(&buf, ptr, end, m);
308 text_to_ass(&buf, ptr, end, m);
310 ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_end - ts_start);
311 av_bprint_finalize(&buf, NULL);
314 *got_sub_ptr = sub->num_rects > 0;
318 AVCodec ff_movtext_decoder = {
320 .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
321 .type = AVMEDIA_TYPE_SUBTITLE,
322 .id = AV_CODEC_ID_MOV_TEXT,
323 .priv_data_size = sizeof(MovTextContext),
324 .init = mov_text_init,
325 .decode = mov_text_decode_frame,