]> git.sesse.net Git - ffmpeg/blob - libavcodec/movtextdec.c
movtextdec.c: Add support for highlight and hilightcolor box
[ffmpeg] / libavcodec / movtextdec.c
1 /*
2  * 3GPP TS 26.245 Timed Text decoder
3  * Copyright (c) 2012  Philip Langdale <philipl@overt.org>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "avcodec.h"
23 #include "ass.h"
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"
29
30 #define STYLE_FLAG_BOLD         (1<<0)
31 #define STYLE_FLAG_ITALIC       (1<<1)
32 #define STYLE_FLAG_UNDERLINE    (1<<2)
33
34 #define STYL_BOX   (1<<0)
35 #define HLIT_BOX   (1<<1)
36 #define HCLR_BOX   (1<<2)
37
38 typedef struct {
39     uint16_t style_start;
40     uint16_t style_end;
41     uint8_t style_flag;
42 } StyleBox;
43
44 typedef struct {
45     uint16_t hlit_start;
46     uint16_t hlit_end;
47 } HighlightBox;
48
49 typedef struct {
50    uint8_t hlit_color[4];
51 } HilightcolorBox;
52
53 typedef struct {
54     StyleBox **s;
55     StyleBox *s_temp;
56     HighlightBox h;
57     HilightcolorBox c;
58     uint8_t box_flags;
59     uint16_t style_entries;
60     uint64_t tracksize;
61     int size_var;
62     int count_s;
63 } MovTextContext;
64
65 typedef struct {
66     uint32_t type;
67     size_t base_size;
68     int (*decode)(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt);
69 } Box;
70
71 static void mov_text_cleanup(MovTextContext *m)
72 {
73     int i;
74     if (m->box_flags & STYL_BOX) {
75         for(i = 0; i < m->count_s; i++) {
76             av_freep(&m->s[i]);
77         }
78         av_freep(&m->s);
79     }
80 }
81
82 static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
83 {
84     m->box_flags |= HLIT_BOX;
85     m->h.hlit_start = AV_RB16(tsmb);
86     tsmb += 2;
87     m->h.hlit_end = AV_RB16(tsmb);
88     tsmb += 2;
89     return 0;
90 }
91
92 static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
93 {
94     m->box_flags |= HCLR_BOX;
95     memcpy(m->c.hlit_color, tsmb, 4);
96     tsmb += 4;
97     return 0;
98 }
99
100 static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
101 {
102     int i;
103     m->style_entries = AV_RB16(tsmb);
104     tsmb += 2;
105     // A single style record is of length 12 bytes.
106     if (m->tracksize + m->size_var + 2 + m->style_entries * 12 > avpkt->size)
107         return -1;
108
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));
112         if (!m->s_temp) {
113             mov_text_cleanup(m);
114             return AVERROR(ENOMEM);
115         }
116         m->s_temp->style_start = AV_RB16(tsmb);
117         tsmb += 2;
118         m->s_temp->style_end = AV_RB16(tsmb);
119         tsmb += 2;
120         // fontID = AV_RB16(tsmb);
121         tsmb += 2;
122         m->s_temp->style_flag = AV_RB8(tsmb);
123         av_dynarray_add(&m->s, &m->count_s, m->s_temp);
124         if(!m->s) {
125             mov_text_cleanup(m);
126             return AVERROR(ENOMEM);
127         }
128         // fontsize = AV_RB8(tsmb);
129         tsmb += 2;
130         // text-color-rgba
131         tsmb += 4;
132     }
133     return 0;
134 }
135
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 }
140 };
141
142 const static size_t box_count = FF_ARRAY_ELEMS(box_types);
143
144 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
145                         MovTextContext *m)
146 {
147     int i = 0;
148     int text_pos = 0;
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}");
159                 }
160             }
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}");
169                 }
170             }
171         }
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
178                  * be implemented.
179                  */
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]);
183                 } else {
184                     av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF}");
185                 }
186             }
187             if (text_pos == m->h.hlit_end) {
188                 if (m->box_flags & HCLR_BOX) {
189                     av_bprintf(buf, "{\\2c&H000000}");
190                 } else {
191                     av_bprintf(buf, "{\\1c&HFFFFFF&}{\\2c&H000000}");
192                 }
193             }
194         }
195
196         switch (*text) {
197         case '\r':
198             break;
199         case '\n':
200             av_bprintf(buf, "\\N");
201             break;
202         default:
203             av_bprint_chars(buf, *text, 1);
204             break;
205         }
206         text++;
207         text_pos++;
208     }
209
210     return 0;
211 }
212
213 static int mov_text_init(AVCodecContext *avctx) {
214     /*
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.
219      */
220     return ff_ass_subtitle_header_default(avctx);
221 }
222
223 static int mov_text_decode_frame(AVCodecContext *avctx,
224                             void *data, int *got_sub_ptr, AVPacket *avpkt)
225 {
226     AVSubtitle *sub = data;
227     MovTextContext *m = avctx->priv_data;
228     int ret, ts_start, ts_end;
229     AVBPrint buf;
230     char *ptr = avpkt->data;
231     char *end;
232     int text_length, tsmb_type, ret_tsmb;
233     uint64_t tsmb_size;
234     const uint8_t *tsmb;
235
236     if (!ptr || avpkt->size < 2)
237         return AVERROR_INVALIDDATA;
238
239     /*
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
244      * bad packet.
245      */
246     if (avpkt->size == 2)
247         return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
248
249     /*
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.
253      */
254     text_length = AV_RB16(ptr);
255     end = ptr + FFMIN(2 + text_length, avpkt->size);
256     ptr += 2;
257
258     ts_start = av_rescale_q(avpkt->pts,
259                             avctx->time_base,
260                             (AVRational){1,100});
261     ts_end   = av_rescale_q(avpkt->pts + avpkt->duration,
262                             avctx->time_base,
263                             (AVRational){1,100});
264
265     tsmb_size = 0;
266     m->tracksize = 2 + text_length;
267     m->style_entries = 0;
268     m->box_flags = 0;
269     m->count_s = 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);
277             tsmb += 4;
278             tsmb_type = AV_RB32(tsmb);
279             tsmb += 4;
280
281             if (tsmb_size == 1) {
282                 if (m->tracksize + 16 > avpkt->size)
283                     break;
284                 tsmb_size = AV_RB64(tsmb);
285                 tsmb += 8;
286                 m->size_var = 16;
287             } else
288                 m->size_var = 8;
289             //size_var is equal to 8 or 16 depending on the size of box
290
291             if (m->tracksize + tsmb_size > avpkt->size)
292                 break;
293
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)
297                         break;
298                     ret_tsmb = box_types[i].decode(tsmb, m, avpkt);
299                     if (ret_tsmb == -1)
300                         break;
301                 }
302             }
303             m->tracksize = m->tracksize + tsmb_size;
304         }
305         text_to_ass(&buf, ptr, end, m);
306         mov_text_cleanup(m);
307     } else
308         text_to_ass(&buf, ptr, end, m);
309
310     ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_end - ts_start);
311     av_bprint_finalize(&buf, NULL);
312     if (ret < 0)
313         return ret;
314     *got_sub_ptr = sub->num_rects > 0;
315     return avpkt->size;
316 }
317
318 AVCodec ff_movtext_decoder = {
319     .name         = "mov_text",
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,
326 };