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