FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ass_split.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 231 292 79.1%
Branches: 175 270 64.8%

Line Branch Exec Source
1 /*
2 * SSA/ASS spliting functions
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.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 "libavutil/common.h"
23 #include "libavutil/error.h"
24 #include "libavutil/mem.h"
25 #include "ass_split.h"
26
27 typedef enum {
28 ASS_STR,
29 ASS_INT,
30 ASS_FLT,
31 ASS_COLOR,
32 ASS_TIMESTAMP,
33 ASS_ALGN,
34 } ASSFieldType;
35
36 typedef struct {
37 const char *name;
38 int type;
39 int offset;
40 } ASSFields;
41
42 typedef struct {
43 const char *section;
44 const char *format_header;
45 const char *fields_header;
46 int size;
47 int offset;
48 int offset_count;
49 ASSFields fields[24];
50 } ASSSection;
51
52 static const ASSSection ass_sections[] = {
53 { .section = "Script Info",
54 .offset = offsetof(ASS, script_info),
55 .fields = {{"ScriptType", ASS_STR, offsetof(ASSScriptInfo, script_type)},
56 {"Collisions", ASS_STR, offsetof(ASSScriptInfo, collisions) },
57 {"PlayResX", ASS_INT, offsetof(ASSScriptInfo, play_res_x) },
58 {"PlayResY", ASS_INT, offsetof(ASSScriptInfo, play_res_y) },
59 {"Timer", ASS_FLT, offsetof(ASSScriptInfo, timer) },
60 {0},
61 }
62 },
63 { .section = "V4+ Styles",
64 .format_header = "Format",
65 .fields_header = "Style",
66 .size = sizeof(ASSStyle),
67 .offset = offsetof(ASS, styles),
68 .offset_count = offsetof(ASS, styles_count),
69 .fields = {{"Name", ASS_STR, offsetof(ASSStyle, name) },
70 {"Fontname", ASS_STR, offsetof(ASSStyle, font_name) },
71 {"Fontsize", ASS_INT, offsetof(ASSStyle, font_size) },
72 {"PrimaryColour", ASS_COLOR, offsetof(ASSStyle, primary_color) },
73 {"SecondaryColour", ASS_COLOR, offsetof(ASSStyle, secondary_color)},
74 {"OutlineColour", ASS_COLOR, offsetof(ASSStyle, outline_color) },
75 {"BackColour", ASS_COLOR, offsetof(ASSStyle, back_color) },
76 {"Bold", ASS_INT, offsetof(ASSStyle, bold) },
77 {"Italic", ASS_INT, offsetof(ASSStyle, italic) },
78 {"Underline", ASS_INT, offsetof(ASSStyle, underline) },
79 {"StrikeOut", ASS_INT, offsetof(ASSStyle, strikeout) },
80 {"ScaleX", ASS_FLT, offsetof(ASSStyle, scalex) },
81 {"ScaleY", ASS_FLT, offsetof(ASSStyle, scaley) },
82 {"Spacing", ASS_FLT, offsetof(ASSStyle, spacing) },
83 {"Angle", ASS_FLT, offsetof(ASSStyle, angle) },
84 {"BorderStyle", ASS_INT, offsetof(ASSStyle, border_style) },
85 {"Outline", ASS_FLT, offsetof(ASSStyle, outline) },
86 {"Shadow", ASS_FLT, offsetof(ASSStyle, shadow) },
87 {"Alignment", ASS_INT, offsetof(ASSStyle, alignment) },
88 {"MarginL", ASS_INT, offsetof(ASSStyle, margin_l) },
89 {"MarginR", ASS_INT, offsetof(ASSStyle, margin_r) },
90 {"MarginV", ASS_INT, offsetof(ASSStyle, margin_v) },
91 {"Encoding", ASS_INT, offsetof(ASSStyle, encoding) },
92 {0},
93 }
94 },
95 { .section = "V4 Styles",
96 .format_header = "Format",
97 .fields_header = "Style",
98 .size = sizeof(ASSStyle),
99 .offset = offsetof(ASS, styles),
100 .offset_count = offsetof(ASS, styles_count),
101 .fields = {{"Name", ASS_STR, offsetof(ASSStyle, name) },
102 {"Fontname", ASS_STR, offsetof(ASSStyle, font_name) },
103 {"Fontsize", ASS_INT, offsetof(ASSStyle, font_size) },
104 {"PrimaryColour", ASS_COLOR, offsetof(ASSStyle, primary_color) },
105 {"SecondaryColour", ASS_COLOR, offsetof(ASSStyle, secondary_color)},
106 {"TertiaryColour", ASS_COLOR, offsetof(ASSStyle, outline_color) },
107 {"BackColour", ASS_COLOR, offsetof(ASSStyle, back_color) },
108 {"Bold", ASS_INT, offsetof(ASSStyle, bold) },
109 {"Italic", ASS_INT, offsetof(ASSStyle, italic) },
110 {"BorderStyle", ASS_INT, offsetof(ASSStyle, border_style) },
111 {"Outline", ASS_FLT, offsetof(ASSStyle, outline) },
112 {"Shadow", ASS_FLT, offsetof(ASSStyle, shadow) },
113 {"Alignment", ASS_ALGN, offsetof(ASSStyle, alignment) },
114 {"MarginL", ASS_INT, offsetof(ASSStyle, margin_l) },
115 {"MarginR", ASS_INT, offsetof(ASSStyle, margin_r) },
116 {"MarginV", ASS_INT, offsetof(ASSStyle, margin_v) },
117 {"AlphaLevel", ASS_INT, offsetof(ASSStyle, alpha_level) },
118 {"Encoding", ASS_INT, offsetof(ASSStyle, encoding) },
119 {0},
120 }
121 },
122 { .section = "Events",
123 .format_header = "Format",
124 .fields_header = "Dialogue",
125 .size = sizeof(ASSDialog),
126 .offset = offsetof(ASS, dialogs),
127 .offset_count = offsetof(ASS, dialogs_count),
128 .fields = {{"Layer", ASS_INT, offsetof(ASSDialog, layer) },
129 {"Start", ASS_TIMESTAMP, offsetof(ASSDialog, start) },
130 {"End", ASS_TIMESTAMP, offsetof(ASSDialog, end) },
131 {"Style", ASS_STR, offsetof(ASSDialog, style) },
132 {"Name", ASS_STR, offsetof(ASSDialog, name) },
133 {"MarginL", ASS_INT, offsetof(ASSDialog, margin_l)},
134 {"MarginR", ASS_INT, offsetof(ASSDialog, margin_r)},
135 {"MarginV", ASS_INT, offsetof(ASSDialog, margin_v)},
136 {"Effect", ASS_STR, offsetof(ASSDialog, effect) },
137 {"Text", ASS_STR, offsetof(ASSDialog, text) },
138 {0},
139 }
140 },
141 };
142
143
144 typedef int (*ASSConvertFunc)(void *dest, const char *buf, int len);
145
146 785 static int convert_str(void *dest, const char *buf, int len)
147 {
148 785 char *str = av_malloc(len + 1);
149
1/2
✓ Branch 0 taken 785 times.
✗ Branch 1 not taken.
785 if (str) {
150 785 memcpy(str, buf, len);
151 785 str[len] = 0;
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 785 times.
785 if (*(void **)dest)
153 av_free(*(void **)dest);
154 785 *(char **)dest = str;
155 }
156 785 return !str;
157 }
158 1046 static int convert_int(void *dest, const char *buf, int len)
159 {
160 1046 return sscanf(buf, "%d", (int *)dest) == 1;
161 }
162 35 static int convert_flt(void *dest, const char *buf, int len)
163 {
164 35 return sscanf(buf, "%f", (float *)dest) == 1;
165 }
166 35 static int convert_color(void *dest, const char *buf, int len)
167 {
168
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 28 times.
42 return sscanf(buf, "&H%8x", (int *)dest) == 1 ||
169
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 sscanf(buf, "%d", (int *)dest) == 1;
170 }
171 static int convert_timestamp(void *dest, const char *buf, int len)
172 {
173 int c, h, m, s, cs;
174 if ((c = sscanf(buf, "%d:%02d:%02d.%02d", &h, &m, &s, &cs)) == 4)
175 *(int *)dest = 360000*h + 6000*m + 100*s + cs;
176 return c == 4;
177 }
178 static int convert_alignment(void *dest, const char *buf, int len)
179 {
180 int a;
181 if (sscanf(buf, "%d", &a) == 1) {
182 /* convert V4 Style alignment to V4+ Style */
183 *(int *)dest = a + ((a&4) >> 1) - 5*!!(a&8);
184 return 1;
185 }
186 return 0;
187 }
188
189 static const ASSConvertFunc convert_func[] = {
190 [ASS_STR] = convert_str,
191 [ASS_INT] = convert_int,
192 [ASS_FLT] = convert_flt,
193 [ASS_COLOR] = convert_color,
194 [ASS_TIMESTAMP] = convert_timestamp,
195 [ASS_ALGN] = convert_alignment,
196 };
197
198
199 struct ASSSplitContext {
200 ASS ass;
201 int current_section;
202 int field_number[FF_ARRAY_ELEMS(ass_sections)];
203 int *field_order[FF_ARRAY_ELEMS(ass_sections)];
204 };
205
206
207 7 static uint8_t *realloc_section_array(ASSSplitContext *ctx)
208 {
209 7 const ASSSection *section = &ass_sections[ctx->current_section];
210 7 int *count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
211 7 void **section_ptr = (void **)((uint8_t *)&ctx->ass + section->offset);
212 7 uint8_t *tmp = av_realloc_array(*section_ptr, (*count+1), section->size);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!tmp)
214 return NULL;
215 7 *section_ptr = tmp;
216 7 tmp += *count * section->size;
217 7 memset(tmp, 0, section->size);
218 7 (*count)++;
219 7 return tmp;
220 }
221
222 413 static inline int is_eol(char buf)
223 {
224
4/6
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 392 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 392 times.
413 return buf == '\r' || buf == '\n' || buf == 0;
225 }
226
227 2524 static inline const char *skip_space(const char *buf)
228 {
229
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 2524 times.
2783 while (*buf == ' ')
230 259 buf++;
231 2524 return buf;
232 }
233
234 static int *get_default_field_orders(const ASSSection *section, int *number)
235 {
236 int i;
237 int *order = av_malloc_array(FF_ARRAY_ELEMS(section->fields), sizeof(*order));
238
239 if (!order)
240 return NULL;
241 for (i = 0; section->fields[i].name; i++)
242 order[i] = i;
243 *number = i;
244 while (i < FF_ARRAY_ELEMS(section->fields))
245 order[i++] = -1;
246 return order;
247 }
248
249 21 static const char *ass_split_section(ASSSplitContext *ctx, const char *buf)
250 {
251 21 const ASSSection *section = &ass_sections[ctx->current_section];
252 21 int *number = &ctx->field_number[ctx->current_section];
253 21 int *order = ctx->field_order[ctx->current_section];
254 int i, len;
255
256
3/4
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 7 times.
91 while (buf && *buf) {
257
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 70 times.
84 if (buf[0] == '[') {
258 14 ctx->current_section = -1;
259 14 break;
260 }
261
3/6
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 63 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
70 if (buf[0] == ';' || (buf[0] == '!' && buf[1] == ':'))
262 7 goto next_line; // skip comments
263
264 63 len = strcspn(buf, ":\r\n");
265
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 14 times.
63 if (buf[len] == ':' &&
266
4/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 7 times.
49 (!section->fields_header || strncmp(buf, section->fields_header, len))) {
267
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 42 times.
210 for (i = 0; i < FF_ARRAY_ELEMS(ass_sections); i++) {
268
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 42 times.
168 if (ass_sections[i].fields_header &&
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 !strncmp(buf, ass_sections[i].fields_header, len)) {
270 ctx->current_section = i;
271 section = &ass_sections[ctx->current_section];
272 number = &ctx->field_number[ctx->current_section];
273 order = ctx->field_order[ctx->current_section];
274 break;
275 }
276 }
277 }
278
4/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 14 times.
63 if (section->format_header && !order) {
279 14 len = strlen(section->format_header);
280
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 if (!strncmp(buf, section->format_header, len) && buf[len] == ':') {
281 14 buf += len + 1;
282
2/2
✓ Branch 1 taken 231 times.
✓ Branch 2 taken 14 times.
245 while (!is_eol(*buf)) {
283 231 buf = skip_space(buf);
284 231 len = strcspn(buf, ", \r\n");
285
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 231 times.
231 if (av_reallocp_array(&order, (*number + 1), sizeof(*order)) != 0)
286 return NULL;
287
288 231 order[*number] = -1;
289
1/2
✓ Branch 0 taken 2240 times.
✗ Branch 1 not taken.
2240 for (i=0; section->fields[i].name; i++)
290
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 2009 times.
2240 if (!strncmp(buf, section->fields[i].name, len)) {
291 231 order[*number] = i;
292 231 break;
293 }
294 231 (*number)++;
295 231 buf = skip_space(buf + len + (buf[len] == ','));
296 }
297 14 ctx->field_order[ctx->current_section] = order;
298 14 goto next_line;
299 }
300 }
301
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 35 times.
49 if (section->fields_header) {
302 14 len = strlen(section->fields_header);
303
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 if (!strncmp(buf, section->fields_header, len) && buf[len] == ':') {
304 7 uint8_t *ptr, *struct_ptr = realloc_section_array(ctx);
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!struct_ptr) return NULL;
306
307 /* No format header line found so far, assume default */
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!order) {
309 order = get_default_field_orders(section, number);
310 if (!order)
311 return NULL;
312 ctx->field_order[ctx->current_section] = order;
313 }
314
315 7 buf += len + 1;
316
3/4
✓ Branch 1 taken 161 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 161 times.
✗ Branch 4 not taken.
168 for (i=0; !is_eol(*buf) && i < *number; i++) {
317 161 int last = i == *number - 1;
318 161 buf = skip_space(buf);
319
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 154 times.
161 len = strcspn(buf, last ? "\r\n" : ",\r\n");
320
1/2
✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
161 if (order[i] >= 0) {
321 161 ASSFieldType type = section->fields[order[i]].type;
322 161 ptr = struct_ptr + section->fields[order[i]].offset;
323 161 convert_func[type](ptr, buf, len);
324 }
325 161 buf += len;
326
3/4
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 154 times.
✗ Branch 3 not taken.
161 if (!last && *buf) buf++;
327 161 buf = skip_space(buf);
328 }
329 }
330 } else {
331 35 len = strcspn(buf, ":\r\n");
332
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 28 times.
35 if (buf[len] == ':') {
333
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 7 times.
98 for (i=0; section->fields[i].name; i++)
334
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 70 times.
91 if (!strncmp(buf, section->fields[i].name, len)) {
335 21 ASSFieldType type = section->fields[i].type;
336 21 uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
337 21 ptr += section->fields[i].offset;
338 21 buf = skip_space(buf + len + 1);
339 21 convert_func[type](ptr, buf, strcspn(buf, "\r\n"));
340 21 break;
341 }
342 }
343 }
344 14 next_line:
345 70 buf += strcspn(buf, "\n");
346 70 buf += !!*buf;
347 }
348 21 return buf;
349 }
350
351 7 static int ass_split(ASSSplitContext *ctx, const char *buf)
352 {
353 char c, section[16];
354 int i;
355
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ctx->current_section >= 0)
357 buf = ass_split_section(ctx, buf);
358
359
3/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 7 times.
28 while (buf && *buf) {
360
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (sscanf(buf, "[%15[0-9A-Za-z+ ]]%c", section, &c) == 2) {
361 21 buf += strcspn(buf, "\n");
362 21 buf += !!*buf;
363
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 21 times.
105 for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
364
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 63 times.
84 if (!strcmp(section, ass_sections[i].section)) {
365 21 ctx->current_section = i;
366 21 buf = ass_split_section(ctx, buf);
367 }
368 } else {
369 buf += strcspn(buf, "\n");
370 buf += !!*buf;
371 }
372 }
373
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 return buf ? 0 : AVERROR_INVALIDDATA;
374 }
375
376 7 ASSSplitContext *ff_ass_split(const char *buf)
377 {
378 7 ASSSplitContext *ctx = av_mallocz(sizeof(*ctx));
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!ctx)
380 return NULL;
381
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
7 if (buf && !strncmp(buf, "\xef\xbb\xbf", 3)) // Skip UTF-8 BOM header
382 buf += 3;
383 7 ctx->current_section = -1;
384
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (ass_split(ctx, buf) < 0) {
385 ff_ass_split_free(ctx);
386 return NULL;
387 }
388 7 return ctx;
389 }
390
391 28 static void free_section(ASSSplitContext *ctx, const ASSSection *section)
392 {
393 28 uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
394 28 int i, j, *count, c = 1;
395
396
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
28 if (section->format_header) {
397 21 ptr = *(void **)ptr;
398 21 count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
399 } else
400 7 count = &c;
401
402
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 if (ptr)
403
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 for (i=0; i<*count; i++, ptr += section->size)
404
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 14 times.
210 for (j=0; section->fields[j].name; j++) {
405 196 const ASSFields *field = &section->fields[j];
406
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 168 times.
196 if (field->type == ASS_STR)
407 28 av_freep(ptr + field->offset);
408 }
409 28 *count = 0;
410
411
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
28 if (section->format_header)
412 21 av_freep((uint8_t *)&ctx->ass + section->offset);
413 28 }
414
415 191 void ff_ass_free_dialog(ASSDialog **dialogp)
416 {
417 191 ASSDialog *dialog = *dialogp;
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 191 times.
191 if (!dialog)
419 return;
420 191 av_freep(&dialog->style);
421 191 av_freep(&dialog->name);
422 191 av_freep(&dialog->effect);
423 191 av_freep(&dialog->text);
424 191 av_freep(dialogp);
425 }
426
427 191 ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf)
428 {
429 int i;
430 static const ASSFields fields[] = {
431 {"ReadOrder", ASS_INT, offsetof(ASSDialog, readorder)},
432 {"Layer", ASS_INT, offsetof(ASSDialog, layer) },
433 {"Style", ASS_STR, offsetof(ASSDialog, style) },
434 {"Name", ASS_STR, offsetof(ASSDialog, name) },
435 {"MarginL", ASS_INT, offsetof(ASSDialog, margin_l) },
436 {"MarginR", ASS_INT, offsetof(ASSDialog, margin_r) },
437 {"MarginV", ASS_INT, offsetof(ASSDialog, margin_v) },
438 {"Effect", ASS_STR, offsetof(ASSDialog, effect) },
439 {"Text", ASS_STR, offsetof(ASSDialog, text) },
440 };
441
442 191 ASSDialog *dialog = av_mallocz(sizeof(*dialog));
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 191 times.
191 if (!dialog)
444 return NULL;
445
446
2/2
✓ Branch 0 taken 1719 times.
✓ Branch 1 taken 191 times.
1910 for (i = 0; i < FF_ARRAY_ELEMS(fields); i++) {
447 size_t len;
448 1719 const int last = i == FF_ARRAY_ELEMS(fields) - 1;
449 1719 const ASSFieldType type = fields[i].type;
450 1719 uint8_t *ptr = (uint8_t *)dialog + fields[i].offset;
451 1719 buf = skip_space(buf);
452
2/2
✓ Branch 0 taken 191 times.
✓ Branch 1 taken 1528 times.
1719 len = last ? strlen(buf) : strcspn(buf, ",");
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1719 times.
1719 if (len >= INT_MAX) {
454 ff_ass_free_dialog(&dialog);
455 return NULL;
456 }
457 1719 convert_func[type](ptr, buf, len);
458 1719 buf += len;
459
2/2
✓ Branch 0 taken 1528 times.
✓ Branch 1 taken 191 times.
1719 if (*buf) buf++;
460 }
461 191 return dialog;
462 }
463
464 7 void ff_ass_split_free(ASSSplitContext *ctx)
465 {
466
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (ctx) {
467 int i;
468
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 7 times.
35 for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++) {
469 28 free_section(ctx, &ass_sections[i]);
470 28 av_freep(&(ctx->field_order[i]));
471 }
472 7 av_free(ctx);
473 }
474 7 }
475
476
477 191 int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
478 const char *buf)
479 {
480 191 const char *text = NULL;
481 char new_line[2];
482 191 int text_len = 0;
483
484
3/4
✓ Branch 0 taken 17081 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16890 times.
✓ Branch 3 taken 191 times.
17081 while (buf && *buf) {
485
3/4
✓ Branch 0 taken 16025 times.
✓ Branch 1 taken 865 times.
✓ Branch 2 taken 16025 times.
✗ Branch 3 not taken.
16890 if (text && callbacks->text &&
486
2/2
✓ Branch 0 taken 15791 times.
✓ Branch 1 taken 234 times.
16025 (sscanf(buf, "\\%1[nN]", new_line) == 1 ||
487
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 15661 times.
15791 !strncmp(buf, "{\\", 2))) {
488 364 callbacks->text(priv, text, text_len);
489 364 text = NULL;
490 }
491
2/2
✓ Branch 0 taken 329 times.
✓ Branch 1 taken 16561 times.
16890 if (sscanf(buf, "\\%1[nN]", new_line) == 1) {
492
1/2
✓ Branch 0 taken 329 times.
✗ Branch 1 not taken.
329 if (callbacks->new_line)
493 329 callbacks->new_line(priv, new_line[0] == 'N');
494 329 buf += 2;
495
2/2
✓ Branch 0 taken 380 times.
✓ Branch 1 taken 16181 times.
16561 } else if (!strncmp(buf, "{\\", 2)) {
496 380 buf++;
497
2/2
✓ Branch 0 taken 380 times.
✓ Branch 1 taken 380 times.
760 while (*buf == '\\') {
498 380 char style[2], c[2], sep[2], c_num[2] = "0", tmp[128] = {0};
499 380 unsigned int color = 0xFFFFFFFF;
500 380 int len, size = -1, an = -1, alpha = -1;
501 380 int x1, y1, x2, y2, t1 = -1, t2 = -1;
502
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 240 times.
380 if (sscanf(buf, "\\%1[bisu]%1[01\\}]%n", style, c, &len) > 1) {
503
3/4
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 70 times.
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
140 int close = c[0] == '0' ? 1 : c[0] == '1' ? 0 : -1;
504 140 len += close != -1;
505
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 112 times.
140 if (callbacks->style)
506 28 callbacks->style(priv, style[0], close);
507
2/2
✓ Branch 0 taken 205 times.
✓ Branch 1 taken 35 times.
240 } else if (sscanf(buf, "\\c%1[\\}]%n", sep, &len) > 0 ||
508
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 45 times.
205 sscanf(buf, "\\c&H%X&%1[\\}]%n", &color, sep, &len) > 1 ||
509
1/2
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
160 sscanf(buf, "\\%1[1234]c%1[\\}]%n", c_num, sep, &len) > 1 ||
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
160 sscanf(buf, "\\%1[1234]c&H%X&%1[\\}]%n", c_num, &color, sep, &len) > 2) {
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (callbacks->color)
512 callbacks->color(priv, color, c_num[0] - '0');
513
1/2
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
160 } else if (sscanf(buf, "\\alpha%1[\\}]%n", sep, &len) > 0 ||
514
1/2
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
160 sscanf(buf, "\\alpha&H%2X&%1[\\}]%n", &alpha, sep, &len) > 1 ||
515
1/2
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
160 sscanf(buf, "\\%1[1234]a%1[\\}]%n", c_num, sep, &len) > 1 ||
516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
160 sscanf(buf, "\\%1[1234]a&H%2X&%1[\\}]%n", c_num, &alpha, sep, &len) > 2) {
517 if (callbacks->alpha)
518 callbacks->alpha(priv, alpha, c_num[0] - '0');
519
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 5 times.
160 } else if (sscanf(buf, "\\fn%1[\\}]%n", sep, &len) > 0 ||
520
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 150 times.
155 sscanf(buf, "\\fn%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (callbacks->font_name)
522 callbacks->font_name(priv, tmp[0] ? tmp : NULL);
523
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 35 times.
150 } else if (sscanf(buf, "\\fs%1[\\}]%n", sep, &len) > 0 ||
524
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 70 times.
115 sscanf(buf, "\\fs%u%1[\\}]%n", &size, sep, &len) > 1) {
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (callbacks->font_size)
526 callbacks->font_size(priv, size);
527
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 } else if (sscanf(buf, "\\a%1[\\}]%n", sep, &len) > 0 ||
528
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 sscanf(buf, "\\a%2u%1[\\}]%n", &an, sep, &len) > 1 ||
529
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 sscanf(buf, "\\an%1[\\}]%n", sep, &len) > 0 ||
530
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 5 times.
70 sscanf(buf, "\\an%1u%1[\\}]%n", &an, sep, &len) > 1) {
531
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
65 if (an != -1 && buf[2] != 'n')
532 an = (an&3) + (an&4 ? 6 : an&8 ? 3 : 0);
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (callbacks->alignment)
534 callbacks->alignment(priv, an);
535
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 } else if (sscanf(buf, "\\r%1[\\}]%n", sep, &len) > 0 ||
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 sscanf(buf, "\\r%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
537 if (callbacks->cancel_overrides)
538 callbacks->cancel_overrides(priv, tmp);
539
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 } else if (sscanf(buf, "\\move(%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, sep, &len) > 4 ||
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 sscanf(buf, "\\move(%d,%d,%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, &t1, &t2, sep, &len) > 6) {
541 if (callbacks->move)
542 callbacks->move(priv, x1, y1, x2, y2, t1, t2);
543
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 } else if (sscanf(buf, "\\pos(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (callbacks->move)
545 callbacks->move(priv, x1, y1, x1, y1, -1, -1);
546 } else if (sscanf(buf, "\\org(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
547 if (callbacks->origin)
548 callbacks->origin(priv, x1, y1);
549 } else {
550 len = strcspn(buf+1, "\\}") + 2; /* skip unknown code */
551 }
552 380 buf += len - 1;
553 }
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380 times.
380 if (*buf++ != '}')
555 return AVERROR_INVALIDDATA;
556 } else {
557
2/2
✓ Branch 0 taken 520 times.
✓ Branch 1 taken 15661 times.
16181 if (!text) {
558 520 text = buf;
559 520 text_len = 1;
560 } else
561 15661 text_len++;
562 16181 buf++;
563 }
564 }
565
3/4
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 156 times.
✗ Branch 3 not taken.
191 if (text && callbacks->text)
566 156 callbacks->text(priv, text, text_len);
567
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 148 times.
191 if (callbacks->end)
568 43 callbacks->end(priv);
569 191 return 0;
570 }
571
572 44 ASSStyle *ff_ass_style_get(ASSSplitContext *ctx, const char *style)
573 {
574 44 ASS *ass = &ctx->ass;
575 int i;
576
577
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
44 if (!style || !*style)
578 style = "Default";
579
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 for (i=0; i<ass->styles_count; i++)
580
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
44 if (ass->styles[i].name && !strcmp(ass->styles[i].name, style))
581 44 return ass->styles + i;
582 return NULL;
583 }
584