FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/microdvddec.c
Date: 2024-10-04 17:46:48
Exec Total Coverage
Lines: 146 209 69.9%
Functions: 8 8 100.0%
Branches: 75 126 59.5%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Clément Bœsch
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * MicroDVD subtitle decoder
24 *
25 * Based on the specifications found here:
26 * https://trac.videolan.org/vlc/ticket/1825#comment:6
27 */
28
29 #include "libavutil/avstring.h"
30 #include "libavutil/parseutils.h"
31 #include "libavutil/bprint.h"
32 #include "avcodec.h"
33 #include "ass.h"
34 #include "codec_internal.h"
35
36 21 static int indexof(const char *s, int c)
37 {
38 21 char *f = strchr(s, c);
39
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 return f ? (f - s) : -1;
40 }
41
42 struct microdvd_tag {
43 char key;
44 int persistent;
45 uint32_t data1;
46 uint32_t data2;
47 char *data_string;
48 int data_string_len;
49 };
50
51 #define MICRODVD_PERSISTENT_OFF 0
52 #define MICRODVD_PERSISTENT_ON 1
53 #define MICRODVD_PERSISTENT_OPENED 2
54
55 // Color, Font, Size, cHarset, stYle, Position, cOordinate
56 #define MICRODVD_TAGS "cfshyYpo"
57
58 14 static void microdvd_set_tag(struct microdvd_tag *tags, struct microdvd_tag tag)
59 {
60 14 int tag_index = indexof(MICRODVD_TAGS, tag.key);
61
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (tag_index < 0)
63 return;
64 14 memcpy(&tags[tag_index], &tag, sizeof(tag));
65 }
66
67 // italic, bold, underline, strike-through
68 #define MICRODVD_STYLES "ibus"
69
70 /* some samples have lines that start with a / indicating non persistent italic
71 * marker */
72 190 static char *check_for_italic_slash_marker(struct microdvd_tag *tags, char *s)
73 {
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
190 if (*s == '/') {
75 struct microdvd_tag tag = tags[indexof(MICRODVD_TAGS, 'y')];
76 tag.key = 'y';
77 tag.data1 |= 1 << 0 /* 'i' position in MICRODVD_STYLES */;
78 microdvd_set_tag(tags, tag);
79 s++;
80 }
81 190 return s;
82 }
83
84 95 static char *microdvd_load_tags(struct microdvd_tag *tags, char *s)
85 {
86 95 s = check_for_italic_slash_marker(tags, s);
87
88
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 95 times.
109 while (*s == '{') {
89 14 char *start = s;
90 14 char tag_char = *(s + 1);
91 14 struct microdvd_tag tag = {0};
92
93
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 if (!tag_char || *(s + 2) != ':')
94 break;
95 14 s += 3;
96
97
6/12
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
14 switch (tag_char) {
98
99 /* Style */
100 1 case 'Y':
101 1 tag.persistent = MICRODVD_PERSISTENT_ON;
102 2 case 'y':
103
4/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
6 while (*s && *s != '}' && s - start < 256) {
104 4 int style_index = indexof(MICRODVD_STYLES, *s);
105
106
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (style_index >= 0)
107 4 tag.data1 |= (1 << style_index);
108 4 s++;
109 }
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (*s != '}')
111 break;
112 /* We must distinguish persistent and non-persistent styles
113 * to handle this kind of style tags: {y:ib}{Y:us} */
114 2 tag.key = tag_char;
115 2 break;
116
117 /* Color */
118 case 'C':
119 tag.persistent = MICRODVD_PERSISTENT_ON;
120 5 case 'c':
121
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
10 while (*s == '$' || *s == '#')
122 5 s++;
123 5 tag.data1 = strtol(s, &s, 16) & 0x00ffffff;
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (*s != '}')
125 break;
126 5 tag.key = 'c';
127 5 break;
128
129 /* Font name */
130 3 case 'F':
131 3 tag.persistent = MICRODVD_PERSISTENT_ON;
132 3 case 'f': {
133 3 int len = indexof(s, '}');
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (len < 0)
135 break;
136 3 tag.data_string = s;
137 3 tag.data_string_len = len;
138 3 s += len;
139 3 tag.key = 'f';
140 3 break;
141 }
142
143 /* Font size */
144 3 case 'S':
145 3 tag.persistent = MICRODVD_PERSISTENT_ON;
146 3 case 's':
147 3 tag.data1 = strtol(s, &s, 10);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (*s != '}')
149 break;
150 3 tag.key = 's';
151 3 break;
152
153 /* Charset */
154 case 'H': {
155 //TODO: not yet handled, just parsed.
156 int len = indexof(s, '}');
157 if (len < 0)
158 break;
159 tag.data_string = s;
160 tag.data_string_len = len;
161 s += len;
162 tag.key = 'h';
163 break;
164 }
165
166 /* Position */
167 case 'P':
168 if (!*s)
169 break;
170 tag.persistent = MICRODVD_PERSISTENT_ON;
171 tag.data1 = (*s++ == '1');
172 if (*s != '}')
173 break;
174 tag.key = 'p';
175 break;
176
177 /* Coordinates */
178 1 case 'o':
179 1 tag.persistent = MICRODVD_PERSISTENT_ON;
180 1 tag.data1 = strtol(s, &s, 10);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (*s != ',')
182 break;
183 1 s++;
184 1 tag.data2 = strtol(s, &s, 10);
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (*s != '}')
186 break;
187 1 tag.key = 'o';
188 1 break;
189
190 default: /* Unknown tag, we consider it's text */
191 break;
192 }
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (tag.key == 0)
195 return start;
196
197 14 microdvd_set_tag(tags, tag);
198 14 s++;
199 }
200 95 return check_for_italic_slash_marker(tags, s);
201 }
202
203 92 static void microdvd_open_tags(AVBPrint *new_line, struct microdvd_tag *tags)
204 {
205 int i, sidx;
206
2/2
✓ Branch 0 taken 736 times.
✓ Branch 1 taken 92 times.
828 for (i = 0; i < sizeof(MICRODVD_TAGS) - 1; i++) {
207
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 735 times.
736 if (tags[i].persistent == MICRODVD_PERSISTENT_OPENED)
208 1 continue;
209
4/7
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 730 times.
735 switch (tags[i].key) {
210 2 case 'Y':
211 case 'y':
212
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (sidx = 0; sidx < sizeof(MICRODVD_STYLES) - 1; sidx++)
213
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (tags[i].data1 & (1 << sidx))
214 4 av_bprintf(new_line, "{\\%c1}", MICRODVD_STYLES[sidx]);
215 2 break;
216
217 2 case 'c':
218 2 av_bprintf(new_line, "{\\c&H%06"PRIX32"&}", tags[i].data1);
219 2 break;
220
221 case 'f':
222 av_bprintf(new_line, "{\\fn%.*s}",
223 tags[i].data_string_len, tags[i].data_string);
224 break;
225
226 case 's':
227 av_bprintf(new_line, "{\\fs%"PRId32"}", tags[i].data1);
228 break;
229
230 case 'p':
231 if (tags[i].data1 == 0)
232 av_bprintf(new_line, "{\\an8}");
233 break;
234
235 1 case 'o':
236 1 av_bprintf(new_line, "{\\pos(%"PRId32",%"PRId32")}",
237 1 tags[i].data1, tags[i].data2);
238 1 break;
239 }
240
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 733 times.
735 if (tags[i].persistent == MICRODVD_PERSISTENT_ON)
241 2 tags[i].persistent = MICRODVD_PERSISTENT_OPENED;
242 }
243 92 }
244
245 32 static void microdvd_close_no_persistent_tags(AVBPrint *new_line,
246 struct microdvd_tag *tags)
247 {
248 int i, sidx;
249
250
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 32 times.
288 for (i = sizeof(MICRODVD_TAGS) - 2; i >= 0; i--) {
251
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 255 times.
256 if (tags[i].persistent != MICRODVD_PERSISTENT_OFF)
252 1 continue;
253
3/5
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 252 times.
255 switch (tags[i].key) {
254
255 1 case 'y':
256
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (sidx = sizeof(MICRODVD_STYLES) - 2; sidx >= 0; sidx--)
257
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (tags[i].data1 & (1 << sidx))
258 2 av_bprintf(new_line, "{\\%c0}", MICRODVD_STYLES[sidx]);
259 1 break;
260
261 2 case 'c':
262 2 av_bprintf(new_line, "{\\c}");
263 2 break;
264
265 case 'f':
266 av_bprintf(new_line, "{\\fn}");
267 break;
268
269 case 's':
270 av_bprintf(new_line, "{\\fs}");
271 break;
272 }
273 255 tags[i].key = 0;
274 }
275 32 }
276
277 60 static int microdvd_decode_frame(AVCodecContext *avctx, AVSubtitle *sub,
278 int *got_sub_ptr, const AVPacket *avpkt)
279 {
280 AVBPrint new_line;
281 60 char *line = avpkt->data;
282 60 char *end = avpkt->data + avpkt->size;
283 60 FFASSDecoderContext *s = avctx->priv_data;
284 60 struct microdvd_tag tags[sizeof(MICRODVD_TAGS) - 1] = {{0}};
285
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (avpkt->size <= 0)
287 return avpkt->size;
288
289 60 av_bprint_init(&new_line, 0, 2048);
290
291 // subtitle content
292
3/4
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 92 times.
✗ Branch 3 not taken.
152 while (line < end && *line) {
293
294 // parse MicroDVD tags, and open them in ASS
295 92 line = microdvd_load_tags(tags, line);
296 92 microdvd_open_tags(&new_line, tags);
297
298 // simple copy until EOL or forced carriage return
299
5/6
✓ Branch 0 taken 3014 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 3014 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2982 times.
✓ Branch 5 taken 32 times.
3074 while (line < end && *line && *line != '|') {
300 2982 av_bprint_chars(&new_line, *line, 1);
301 2982 line++;
302 }
303
304 // line split
305
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
92 if (line < end && *line == '|') {
306 32 microdvd_close_no_persistent_tags(&new_line, tags);
307 32 av_bprintf(&new_line, "\\N");
308 32 line++;
309 }
310 }
311
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (new_line.len) {
312 60 int ret = ff_ass_add_rect(sub, new_line.str, s->readorder++, 0, NULL, NULL);
313 60 av_bprint_finalize(&new_line, NULL);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (ret < 0)
315 return ret;
316 }
317
318 60 *got_sub_ptr = sub->num_rects > 0;
319 60 return avpkt->size;
320 }
321
322 5 static int microdvd_init(AVCodecContext *avctx)
323 {
324 int i, sidx;
325 AVBPrint font_buf;
326 5 int font_size = ASS_DEFAULT_FONT_SIZE;
327 5 int color = ASS_DEFAULT_COLOR;
328 5 int bold = ASS_DEFAULT_BOLD;
329 5 int italic = ASS_DEFAULT_ITALIC;
330 5 int underline = ASS_DEFAULT_UNDERLINE;
331 5 int alignment = ASS_DEFAULT_ALIGNMENT;
332 5 struct microdvd_tag tags[sizeof(MICRODVD_TAGS) - 1] = {{0}};
333
334 5 av_bprint_init(&font_buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
335 5 av_bprintf(&font_buf, "%s", ASS_DEFAULT_FONT);
336
337
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (avctx->extradata) {
338 3 microdvd_load_tags(tags, avctx->extradata);
339
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
27 for (i = 0; i < sizeof(MICRODVD_TAGS) - 1; i++) {
340
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 15 times.
24 switch (av_tolower(tags[i].key)) {
341 case 'y':
342 for (sidx = 0; sidx < sizeof(MICRODVD_STYLES) - 1; sidx++) {
343 if (tags[i].data1 & (1 << sidx)) {
344 switch (MICRODVD_STYLES[sidx]) {
345 case 'i': italic = 1; break;
346 case 'b': bold = 1; break;
347 case 'u': underline = 1; break;
348 }
349 }
350 }
351 break;
352
353 3 case 'c': color = tags[i].data1; break;
354 3 case 's': font_size = tags[i].data1; break;
355 case 'p': alignment = 8; break;
356
357 3 case 'f':
358 3 av_bprint_clear(&font_buf);
359 3 av_bprintf(&font_buf, "%.*s",
360 tags[i].data_string_len, tags[i].data_string);
361 3 break;
362 }
363 }
364 }
365 5 return ff_ass_subtitle_header(avctx, font_buf.str, font_size, color,
366 ASS_DEFAULT_BACK_COLOR, bold, italic,
367 underline, ASS_DEFAULT_BORDERSTYLE,
368 alignment);
369 }
370
371 const FFCodec ff_microdvd_decoder = {
372 .p.name = "microdvd",
373 CODEC_LONG_NAME("MicroDVD subtitle"),
374 .p.type = AVMEDIA_TYPE_SUBTITLE,
375 .p.id = AV_CODEC_ID_MICRODVD,
376 .init = microdvd_init,
377 FF_CODEC_DECODE_SUB_CB(microdvd_decode_frame),
378 .flush = ff_ass_decoder_flush,
379 .priv_data_size = sizeof(FFASSDecoderContext),
380 };
381