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