]> git.sesse.net Git - ffmpeg/blob - libavcodec/movtextdec.c
lavf/segment: fix crash when failing to open segment list
[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 BOX_SIZE_INITIAL    40
35
36 #define STYL_BOX   (1<<0)
37 #define HLIT_BOX   (1<<1)
38 #define HCLR_BOX   (1<<2)
39 #define TWRP_BOX   (1<<3)
40
41 #define BOTTOM_LEFT     1
42 #define BOTTOM_CENTER   2
43 #define BOTTOM_RIGHT    3
44 #define MIDDLE_LEFT     4
45 #define MIDDLE_CENTER   5
46 #define MIDDLE_RIGHT    6
47 #define TOP_LEFT        7
48 #define TOP_CENTER      8
49 #define TOP_RIGHT       9
50
51 typedef struct {
52     char *font;
53     int fontsize;
54     int color;
55     int back_color;
56     int bold;
57     int italic;
58     int underline;
59     int alignment;
60 } MovTextDefault;
61
62 typedef struct {
63     uint16_t fontID;
64     char *font;
65 } FontRecord;
66
67 typedef struct {
68     uint16_t style_start;
69     uint16_t style_end;
70     uint8_t style_flag;
71     uint8_t fontsize;
72     uint16_t style_fontID;
73 } StyleBox;
74
75 typedef struct {
76     uint16_t hlit_start;
77     uint16_t hlit_end;
78 } HighlightBox;
79
80 typedef struct {
81    uint8_t hlit_color[4];
82 } HilightcolorBox;
83
84 typedef struct {
85     uint8_t wrap_flag;
86 } TextWrapBox;
87
88 typedef struct {
89     StyleBox **s;
90     StyleBox *s_temp;
91     HighlightBox h;
92     HilightcolorBox c;
93     FontRecord **ftab;
94     FontRecord *ftab_temp;
95     TextWrapBox w;
96     MovTextDefault d;
97     uint8_t box_flags;
98     uint16_t style_entries, ftab_entries;
99     uint64_t tracksize;
100     int size_var;
101     int count_s, count_f;
102     int readorder;
103 } MovTextContext;
104
105 typedef struct {
106     uint32_t type;
107     size_t base_size;
108     int (*decode)(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt);
109 } Box;
110
111 static void mov_text_cleanup(MovTextContext *m)
112 {
113     int i;
114     if (m->box_flags & STYL_BOX) {
115         for(i = 0; i < m->count_s; i++) {
116             av_freep(&m->s[i]);
117         }
118         av_freep(&m->s);
119     }
120 }
121
122 static void mov_text_cleanup_ftab(MovTextContext *m)
123 {
124     int i;
125     if (m->ftab_temp)
126         av_freep(&m->ftab_temp->font);
127     av_freep(&m->ftab_temp);
128     if (m->ftab) {
129         for(i = 0; i < m->count_f; i++) {
130             av_freep(&m->ftab[i]->font);
131             av_freep(&m->ftab[i]);
132         }
133     }
134     av_freep(&m->ftab);
135 }
136
137 static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m)
138 {
139     uint8_t *tx3g_ptr = avctx->extradata;
140     int i, box_size, font_length;
141     int8_t v_align, h_align;
142     int style_fontID;
143     StyleBox s_default;
144
145     m->count_f = 0;
146     m->ftab_entries = 0;
147     box_size = BOX_SIZE_INITIAL; /* Size till ftab_entries */
148     if (avctx->extradata_size < box_size)
149         return -1;
150
151     // Display Flags
152     tx3g_ptr += 4;
153     // Alignment
154     h_align = *tx3g_ptr++;
155     v_align = *tx3g_ptr++;
156     if (h_align == 0) {
157         if (v_align == 0)
158             m->d.alignment = TOP_LEFT;
159         if (v_align == 1)
160             m->d.alignment = MIDDLE_LEFT;
161         if (v_align == -1)
162             m->d.alignment = BOTTOM_LEFT;
163     }
164     if (h_align == 1) {
165         if (v_align == 0)
166             m->d.alignment = TOP_CENTER;
167         if (v_align == 1)
168             m->d.alignment = MIDDLE_CENTER;
169         if (v_align == -1)
170             m->d.alignment = BOTTOM_CENTER;
171     }
172     if (h_align == -1) {
173         if (v_align == 0)
174             m->d.alignment = TOP_RIGHT;
175         if (v_align == 1)
176             m->d.alignment = MIDDLE_RIGHT;
177         if (v_align == -1)
178             m->d.alignment = BOTTOM_RIGHT;
179     }
180     // Background Color
181     m->d.back_color = AV_RB24(tx3g_ptr);
182     tx3g_ptr += 4;
183     // BoxRecord
184     tx3g_ptr += 8;
185     // StyleRecord
186     tx3g_ptr += 4;
187     // fontID
188     style_fontID = AV_RB16(tx3g_ptr);
189     tx3g_ptr += 2;
190     // face-style-flags
191     s_default.style_flag = *tx3g_ptr++;
192     m->d.bold = s_default.style_flag & STYLE_FLAG_BOLD;
193     m->d.italic = s_default.style_flag & STYLE_FLAG_ITALIC;
194     m->d.underline = s_default.style_flag & STYLE_FLAG_UNDERLINE;
195     // fontsize
196     m->d.fontsize = *tx3g_ptr++;
197     // Primary color
198     m->d.color = AV_RB24(tx3g_ptr);
199     tx3g_ptr += 4;
200     // FontRecord
201     // FontRecord Size
202     tx3g_ptr += 4;
203     // ftab
204     tx3g_ptr += 4;
205
206     m->ftab_entries = AV_RB16(tx3g_ptr);
207     tx3g_ptr += 2;
208
209     for (i = 0; i < m->ftab_entries; i++) {
210
211         box_size += 3;
212         if (avctx->extradata_size < box_size) {
213             mov_text_cleanup_ftab(m);
214             m->ftab_entries = 0;
215             return -1;
216         }
217         m->ftab_temp = av_mallocz(sizeof(*m->ftab_temp));
218         if (!m->ftab_temp) {
219             mov_text_cleanup_ftab(m);
220             return AVERROR(ENOMEM);
221         }
222         m->ftab_temp->fontID = AV_RB16(tx3g_ptr);
223         tx3g_ptr += 2;
224         font_length = *tx3g_ptr++;
225
226         box_size = box_size + font_length;
227         if (avctx->extradata_size < box_size) {
228             mov_text_cleanup_ftab(m);
229             m->ftab_entries = 0;
230             return -1;
231         }
232         m->ftab_temp->font = av_malloc(font_length + 1);
233         if (!m->ftab_temp->font) {
234             mov_text_cleanup_ftab(m);
235             return AVERROR(ENOMEM);
236         }
237         memcpy(m->ftab_temp->font, tx3g_ptr, font_length);
238         m->ftab_temp->font[font_length] = '\0';
239         av_dynarray_add(&m->ftab, &m->count_f, m->ftab_temp);
240         if (!m->ftab) {
241             mov_text_cleanup_ftab(m);
242             return AVERROR(ENOMEM);
243         }
244         m->ftab_temp = NULL;
245         tx3g_ptr = tx3g_ptr + font_length;
246     }
247     for (i = 0; i < m->ftab_entries; i++) {
248         if (style_fontID == m->ftab[i]->fontID)
249             m->d.font = m->ftab[i]->font;
250     }
251     return 0;
252 }
253
254 static int decode_twrp(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
255 {
256     m->box_flags |= TWRP_BOX;
257     m->w.wrap_flag = *tsmb++;
258     return 0;
259 }
260
261 static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
262 {
263     m->box_flags |= HLIT_BOX;
264     m->h.hlit_start = AV_RB16(tsmb);
265     tsmb += 2;
266     m->h.hlit_end = AV_RB16(tsmb);
267     tsmb += 2;
268     return 0;
269 }
270
271 static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
272 {
273     m->box_flags |= HCLR_BOX;
274     memcpy(m->c.hlit_color, tsmb, 4);
275     tsmb += 4;
276     return 0;
277 }
278
279 static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
280 {
281     int i;
282     m->style_entries = AV_RB16(tsmb);
283     tsmb += 2;
284     // A single style record is of length 12 bytes.
285     if (m->tracksize + m->size_var + 2 + m->style_entries * 12 > avpkt->size)
286         return -1;
287
288     m->box_flags |= STYL_BOX;
289     for(i = 0; i < m->style_entries; i++) {
290         m->s_temp = av_malloc(sizeof(*m->s_temp));
291         if (!m->s_temp) {
292             mov_text_cleanup(m);
293             return AVERROR(ENOMEM);
294         }
295         m->s_temp->style_start = AV_RB16(tsmb);
296         tsmb += 2;
297         m->s_temp->style_end = AV_RB16(tsmb);
298         tsmb += 2;
299         m->s_temp->style_fontID = AV_RB16(tsmb);
300         tsmb += 2;
301         m->s_temp->style_flag = AV_RB8(tsmb);
302         tsmb++;
303         m->s_temp->fontsize = AV_RB8(tsmb);
304         av_dynarray_add(&m->s, &m->count_s, m->s_temp);
305         if(!m->s) {
306             mov_text_cleanup(m);
307             return AVERROR(ENOMEM);
308         }
309         tsmb++;
310         // text-color-rgba
311         tsmb += 4;
312     }
313     return 0;
314 }
315
316 static const Box box_types[] = {
317     { MKBETAG('s','t','y','l'), 2, decode_styl },
318     { MKBETAG('h','l','i','t'), 4, decode_hlit },
319     { MKBETAG('h','c','l','r'), 4, decode_hclr },
320     { MKBETAG('t','w','r','p'), 1, decode_twrp }
321 };
322
323 const static size_t box_count = FF_ARRAY_ELEMS(box_types);
324
325 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
326                         MovTextContext *m)
327 {
328     int i = 0;
329     int j = 0;
330     int text_pos = 0;
331
332     if (text < text_end && m->box_flags & TWRP_BOX) {
333         if (m->w.wrap_flag == 1) {
334             av_bprintf(buf, "{\\q1}"); /* End of line wrap */
335         } else {
336             av_bprintf(buf, "{\\q2}"); /* No wrap */
337         }
338     }
339
340     while (text < text_end) {
341         if (m->box_flags & STYL_BOX) {
342             for (i = 0; i < m->style_entries; i++) {
343                 if (m->s[i]->style_flag && text_pos == m->s[i]->style_end) {
344                     av_bprintf(buf, "{\\r}");
345                 }
346             }
347             for (i = 0; i < m->style_entries; i++) {
348                 if (m->s[i]->style_flag && text_pos == m->s[i]->style_start) {
349                     if (m->s[i]->style_flag & STYLE_FLAG_BOLD)
350                         av_bprintf(buf, "{\\b1}");
351                     if (m->s[i]->style_flag & STYLE_FLAG_ITALIC)
352                         av_bprintf(buf, "{\\i1}");
353                     if (m->s[i]->style_flag & STYLE_FLAG_UNDERLINE)
354                         av_bprintf(buf, "{\\u1}");
355                     av_bprintf(buf, "{\\fs%d}", m->s[i]->fontsize);
356                     for (j = 0; j < m->ftab_entries; j++) {
357                         if (m->s[i]->style_fontID == m->ftab[j]->fontID)
358                             av_bprintf(buf, "{\\fn%s}", m->ftab[j]->font);
359                     }
360                 }
361             }
362         }
363         if (m->box_flags & HLIT_BOX) {
364             if (text_pos == m->h.hlit_start) {
365                 /* If hclr box is present, set the secondary color to the color
366                  * specified. Otherwise, set primary color to white and secondary
367                  * color to black. These colors will come from TextSampleModifier
368                  * boxes in future and inverse video technique for highlight will
369                  * be implemented.
370                  */
371                 if (m->box_flags & HCLR_BOX) {
372                     av_bprintf(buf, "{\\2c&H%02x%02x%02x&}", m->c.hlit_color[2],
373                                 m->c.hlit_color[1], m->c.hlit_color[0]);
374                 } else {
375                     av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF&}");
376                 }
377             }
378             if (text_pos == m->h.hlit_end) {
379                 if (m->box_flags & HCLR_BOX) {
380                     av_bprintf(buf, "{\\2c&H000000&}");
381                 } else {
382                     av_bprintf(buf, "{\\1c&HFFFFFF&}{\\2c&H000000&}");
383                 }
384             }
385         }
386
387         switch (*text) {
388         case '\r':
389             break;
390         case '\n':
391             av_bprintf(buf, "\\N");
392             break;
393         default:
394             av_bprint_chars(buf, *text, 1);
395             break;
396         }
397         text++;
398         text_pos++;
399     }
400
401     return 0;
402 }
403
404 static int mov_text_init(AVCodecContext *avctx) {
405     /*
406      * TODO: Handle the default text style.
407      * NB: Most players ignore styles completely, with the result that
408      * it's very common to find files where the default style is broken
409      * and respecting it results in a worse experience than ignoring it.
410      */
411     int ret;
412     MovTextContext *m = avctx->priv_data;
413     ret = mov_text_tx3g(avctx, m);
414     if (ret == 0) {
415         return ff_ass_subtitle_header(avctx, m->d.font, m->d.fontsize, m->d.color,
416                                 m->d.back_color, m->d.bold, m->d.italic,
417                                 m->d.underline, ASS_DEFAULT_BORDERSTYLE,
418                                 m->d.alignment);
419     } else
420         return ff_ass_subtitle_header_default(avctx);
421 }
422
423 static int mov_text_decode_frame(AVCodecContext *avctx,
424                             void *data, int *got_sub_ptr, AVPacket *avpkt)
425 {
426     AVSubtitle *sub = data;
427     MovTextContext *m = avctx->priv_data;
428     int ret;
429     AVBPrint buf;
430     char *ptr = avpkt->data;
431     char *end;
432     int text_length, tsmb_type, ret_tsmb;
433     uint64_t tsmb_size;
434     const uint8_t *tsmb;
435
436     if (!ptr || avpkt->size < 2)
437         return AVERROR_INVALIDDATA;
438
439     /*
440      * A packet of size two with value zero is an empty subtitle
441      * used to mark the end of the previous non-empty subtitle.
442      * We can just drop them here as we have duration information
443      * already. If the value is non-zero, then it's technically a
444      * bad packet.
445      */
446     if (avpkt->size == 2)
447         return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
448
449     /*
450      * The first two bytes of the packet are the length of the text string
451      * In complex cases, there are style descriptors appended to the string
452      * so we can't just assume the packet size is the string size.
453      */
454     text_length = AV_RB16(ptr);
455     end = ptr + FFMIN(2 + text_length, avpkt->size);
456     ptr += 2;
457
458     tsmb_size = 0;
459     m->tracksize = 2 + text_length;
460     m->style_entries = 0;
461     m->box_flags = 0;
462     m->count_s = 0;
463     // Note that the spec recommends lines be no longer than 2048 characters.
464     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
465     if (text_length + 2 != avpkt->size) {
466         while (m->tracksize + 8 <= avpkt->size) {
467             // A box is a minimum of 8 bytes.
468             tsmb = ptr + m->tracksize - 2;
469             tsmb_size = AV_RB32(tsmb);
470             tsmb += 4;
471             tsmb_type = AV_RB32(tsmb);
472             tsmb += 4;
473
474             if (tsmb_size == 1) {
475                 if (m->tracksize + 16 > avpkt->size)
476                     break;
477                 tsmb_size = AV_RB64(tsmb);
478                 tsmb += 8;
479                 m->size_var = 16;
480             } else
481                 m->size_var = 8;
482             //size_var is equal to 8 or 16 depending on the size of box
483
484             if (tsmb_size == 0) {
485                 av_log(avctx, AV_LOG_ERROR, "tsmb_size is 0\n");
486                 return AVERROR_INVALIDDATA;
487             }
488
489             if (tsmb_size > avpkt->size - m->tracksize)
490                 break;
491
492             for (size_t i = 0; i < box_count; i++) {
493                 if (tsmb_type == box_types[i].type) {
494                     if (m->tracksize + m->size_var + box_types[i].base_size > avpkt->size)
495                         break;
496                     ret_tsmb = box_types[i].decode(tsmb, m, avpkt);
497                     if (ret_tsmb == -1)
498                         break;
499                 }
500             }
501             m->tracksize = m->tracksize + tsmb_size;
502         }
503         text_to_ass(&buf, ptr, end, m);
504         mov_text_cleanup(m);
505     } else
506         text_to_ass(&buf, ptr, end, m);
507
508     ret = ff_ass_add_rect(sub, buf.str, m->readorder++, 0, NULL, NULL);
509     av_bprint_finalize(&buf, NULL);
510     if (ret < 0)
511         return ret;
512     *got_sub_ptr = sub->num_rects > 0;
513     return avpkt->size;
514 }
515
516 static int mov_text_decode_close(AVCodecContext *avctx)
517 {
518     MovTextContext *m = avctx->priv_data;
519     mov_text_cleanup_ftab(m);
520     return 0;
521 }
522
523 static void mov_text_flush(AVCodecContext *avctx)
524 {
525     MovTextContext *m = avctx->priv_data;
526     if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
527         m->readorder = 0;
528 }
529
530 AVCodec ff_movtext_decoder = {
531     .name         = "mov_text",
532     .long_name    = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
533     .type         = AVMEDIA_TYPE_SUBTITLE,
534     .id           = AV_CODEC_ID_MOV_TEXT,
535     .priv_data_size = sizeof(MovTextContext),
536     .init         = mov_text_init,
537     .decode       = mov_text_decode_frame,
538     .close        = mov_text_decode_close,
539     .flush        = mov_text_flush,
540 };