]> git.sesse.net Git - ffmpeg/blob - libavcodec/movtextdec.c
avcodec/flacenc: Do not copy unused udata array -> 5x faster calc_rice_params()
[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
31 #define STYLE_FLAG_ITALIC       2
32 #define STYLE_FLAG_UNDERLINE    4
33
34 static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
35                         int **style_start, int **style_end,
36                         int **style_flags, int style_entries)
37 {
38     int i = 0;
39     int style_pos = 0;
40     while (text < text_end) {
41         for (i = 0; i < style_entries; i++) {
42             if (*style_flags[i] && style_pos == *style_start[i]) {
43                 if (*style_flags[i] & STYLE_FLAG_BOLD)
44                     av_bprintf(buf, "{\\b1}");
45                 if (*style_flags[i] & STYLE_FLAG_ITALIC)
46                     av_bprintf(buf, "{\\i1}");
47                 if (*style_flags[i] & STYLE_FLAG_UNDERLINE)
48                     av_bprintf(buf, "{\\u1}");
49             }
50         }
51
52         switch (*text) {
53         case '\r':
54             break;
55         case '\n':
56             av_bprintf(buf, "\\N");
57             break;
58         default:
59             av_bprint_chars(buf, *text, 1);
60             break;
61         }
62
63         for (i = 0; i < style_entries; i++) {
64             if (*style_flags[i] && style_pos == *style_end[i]) {
65                 if (*style_flags[i] & STYLE_FLAG_BOLD)
66                     av_bprintf(buf, "{\\b0}");
67                 if (*style_flags[i] & STYLE_FLAG_ITALIC)
68                     av_bprintf(buf, "{\\i0}");
69                 if (*style_flags[i] & STYLE_FLAG_UNDERLINE)
70                     av_bprintf(buf, "{\\u0}");
71             }
72         }
73         text++;
74         style_pos++;
75     }
76
77     return 0;
78 }
79
80 static int mov_text_init(AVCodecContext *avctx) {
81     /*
82      * TODO: Handle the default text style.
83      * NB: Most players ignore styles completely, with the result that
84      * it's very common to find files where the default style is broken
85      * and respecting it results in a worse experience than ignoring it.
86      */
87     return ff_ass_subtitle_header_default(avctx);
88 }
89
90 static int mov_text_decode_frame(AVCodecContext *avctx,
91                             void *data, int *got_sub_ptr, AVPacket *avpkt)
92 {
93     AVSubtitle *sub = data;
94     int ret, ts_start, ts_end;
95     AVBPrint buf;
96     char *ptr = avpkt->data;
97     char *end;
98     //char *ptr_temp;
99     int text_length, tsmb_type, style_entries, tsmb_size;
100     int **style_start = {0,};
101     int **style_end = {0,};
102     int **style_flags = {0,};
103     const uint8_t *tsmb;
104     int index, i;
105     int *flag;
106     int *style_pos;
107
108     if (!ptr || avpkt->size < 2)
109         return AVERROR_INVALIDDATA;
110
111     /*
112      * A packet of size two with value zero is an empty subtitle
113      * used to mark the end of the previous non-empty subtitle.
114      * We can just drop them here as we have duration information
115      * already. If the value is non-zero, then it's technically a
116      * bad packet.
117      */
118     if (avpkt->size == 2)
119         return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
120
121     /*
122      * The first two bytes of the packet are the length of the text string
123      * In complex cases, there are style descriptors appended to the string
124      * so we can't just assume the packet size is the string size.
125      */
126     text_length = AV_RB16(ptr);
127     end = ptr + FFMIN(2 + text_length, avpkt->size);
128     ptr += 2;
129
130     ts_start = av_rescale_q(avpkt->pts,
131                             avctx->time_base,
132                             (AVRational){1,100});
133     ts_end   = av_rescale_q(avpkt->pts + avpkt->duration,
134                             avctx->time_base,
135                             (AVRational){1,100});
136
137     tsmb_size = 0;
138     // Note that the spec recommends lines be no longer than 2048 characters.
139     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
140     if (text_length + 2 != avpkt->size) {
141         while (text_length + 2 + tsmb_size < avpkt->size)  {
142             tsmb = ptr + text_length + tsmb_size;
143             tsmb_size = AV_RB32(tsmb);
144             tsmb += 4;
145             tsmb_type = AV_RB32(tsmb);
146             tsmb += 4;
147
148             if (tsmb_type == MKBETAG('s','t','y','l')) {
149                 style_entries = AV_RB16(tsmb);
150                 tsmb += 2;
151
152                 for(i = 0; i < style_entries; i++) {
153                     style_pos = av_malloc(4);
154                     *style_pos = AV_RB16(tsmb);
155                     index = i;
156                     av_dynarray_add(&style_start, &index, style_pos);
157                     tsmb += 2;
158                     style_pos = av_malloc(4);
159                     *style_pos = AV_RB16(tsmb);
160                     index = i;
161                     av_dynarray_add(&style_end, &index, style_pos);
162                     tsmb += 2;
163                     // fontID = AV_RB16(tsmb);
164                     tsmb += 2;
165                     flag = av_malloc(4);
166                     *flag = AV_RB8(tsmb);
167                     index = i;
168                     av_dynarray_add(&style_flags, &index, flag);
169                     //fontsize=AV_RB8(tsmb);
170                     tsmb += 2;
171                     // text-color-rgba
172                     tsmb += 4;
173                 }
174                 text_to_ass(&buf, ptr, end, style_start, style_end, style_flags, style_entries);
175                 av_freep(&style_start);
176                 av_freep(&style_end);
177                 av_freep(&style_flags);
178             }
179         }
180     } else
181         text_to_ass(&buf, ptr, end, NULL, NULL, 0, 0);
182
183     ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_end - ts_start);
184     av_bprint_finalize(&buf, NULL);
185     if (ret < 0)
186         return ret;
187     *got_sub_ptr = sub->num_rects > 0;
188     return avpkt->size;
189 }
190
191 AVCodec ff_movtext_decoder = {
192     .name         = "mov_text",
193     .long_name    = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
194     .type         = AVMEDIA_TYPE_SUBTITLE,
195     .id           = AV_CODEC_ID_MOV_TEXT,
196     .init         = mov_text_init,
197     .decode       = mov_text_decode_frame,
198 };