2 * SSA/ASS spliting functions
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
5 * This file is part of FFmpeg.
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.
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.
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
23 #include "ass_split.h"
42 const char *format_header;
43 const char *fields_header;
50 static const ASSSection ass_sections[] = {
51 { .section = "Script Info",
52 .offset = offsetof(ASS, script_info),
53 .fields = {{"ScriptType", ASS_STR, offsetof(ASSScriptInfo, script_type)},
54 {"Collisions", ASS_STR, offsetof(ASSScriptInfo, collisions) },
55 {"PlayResX", ASS_INT, offsetof(ASSScriptInfo, play_res_x) },
56 {"PlayResY", ASS_INT, offsetof(ASSScriptInfo, play_res_y) },
57 {"Timer", ASS_FLT, offsetof(ASSScriptInfo, timer) },
61 { .section = "V4+ Styles",
62 .format_header = "Format",
63 .fields_header = "Style",
64 .size = sizeof(ASSStyle),
65 .offset = offsetof(ASS, styles),
66 .offset_count = offsetof(ASS, styles_count),
67 .fields = {{"Name", ASS_STR, offsetof(ASSStyle, name) },
68 {"Fontname", ASS_STR, offsetof(ASSStyle, font_name) },
69 {"Fontsize", ASS_INT, offsetof(ASSStyle, font_size) },
70 {"PrimaryColour", ASS_COLOR, offsetof(ASSStyle, primary_color) },
71 {"SecondaryColour", ASS_COLOR, offsetof(ASSStyle, secondary_color)},
72 {"OutlineColour", ASS_COLOR, offsetof(ASSStyle, outline_color) },
73 {"BackColour", ASS_COLOR, offsetof(ASSStyle, back_color) },
74 {"Bold", ASS_INT, offsetof(ASSStyle, bold) },
75 {"Italic", ASS_INT, offsetof(ASSStyle, italic) },
76 {"Underline", ASS_INT, offsetof(ASSStyle, underline) },
77 {"StrikeOut", ASS_INT, offsetof(ASSStyle, strikeout) },
78 {"ScaleX", ASS_FLT, offsetof(ASSStyle, scalex) },
79 {"ScaleY", ASS_FLT, offsetof(ASSStyle, scaley) },
80 {"Spacing", ASS_FLT, offsetof(ASSStyle, spacing) },
81 {"Angle", ASS_FLT, offsetof(ASSStyle, angle) },
82 {"BorderStyle", ASS_INT, offsetof(ASSStyle, border_style) },
83 {"Outline", ASS_FLT, offsetof(ASSStyle, outline) },
84 {"Shadow", ASS_FLT, offsetof(ASSStyle, shadow) },
85 {"Alignment", ASS_INT, offsetof(ASSStyle, alignment) },
86 {"MarginL", ASS_INT, offsetof(ASSStyle, margin_l) },
87 {"MarginR", ASS_INT, offsetof(ASSStyle, margin_r) },
88 {"MarginV", ASS_INT, offsetof(ASSStyle, margin_v) },
89 {"Encoding", ASS_INT, offsetof(ASSStyle, encoding) },
93 { .section = "V4 Styles",
94 .format_header = "Format",
95 .fields_header = "Style",
96 .size = sizeof(ASSStyle),
97 .offset = offsetof(ASS, styles),
98 .offset_count = offsetof(ASS, styles_count),
99 .fields = {{"Name", ASS_STR, offsetof(ASSStyle, name) },
100 {"Fontname", ASS_STR, offsetof(ASSStyle, font_name) },
101 {"Fontsize", ASS_INT, offsetof(ASSStyle, font_size) },
102 {"PrimaryColour", ASS_COLOR, offsetof(ASSStyle, primary_color) },
103 {"SecondaryColour", ASS_COLOR, offsetof(ASSStyle, secondary_color)},
104 {"TertiaryColour", ASS_COLOR, offsetof(ASSStyle, outline_color) },
105 {"BackColour", ASS_COLOR, offsetof(ASSStyle, back_color) },
106 {"Bold", ASS_INT, offsetof(ASSStyle, bold) },
107 {"Italic", ASS_INT, offsetof(ASSStyle, italic) },
108 {"BorderStyle", ASS_INT, offsetof(ASSStyle, border_style) },
109 {"Outline", ASS_FLT, offsetof(ASSStyle, outline) },
110 {"Shadow", ASS_FLT, offsetof(ASSStyle, shadow) },
111 {"Alignment", ASS_ALGN, offsetof(ASSStyle, alignment) },
112 {"MarginL", ASS_INT, offsetof(ASSStyle, margin_l) },
113 {"MarginR", ASS_INT, offsetof(ASSStyle, margin_r) },
114 {"MarginV", ASS_INT, offsetof(ASSStyle, margin_v) },
115 {"AlphaLevel", ASS_INT, offsetof(ASSStyle, alpha_level) },
116 {"Encoding", ASS_INT, offsetof(ASSStyle, encoding) },
120 { .section = "Events",
121 .format_header = "Format",
122 .fields_header = "Dialogue",
123 .size = sizeof(ASSDialog),
124 .offset = offsetof(ASS, dialogs),
125 .offset_count = offsetof(ASS, dialogs_count),
126 .fields = {{"Layer", ASS_INT, offsetof(ASSDialog, layer) },
127 {"Start", ASS_TIMESTAMP, offsetof(ASSDialog, start) },
128 {"End", ASS_TIMESTAMP, offsetof(ASSDialog, end) },
129 {"Style", ASS_STR, offsetof(ASSDialog, style) },
130 {"Name", ASS_STR, offsetof(ASSDialog, name) },
131 {"MarginL", ASS_INT, offsetof(ASSDialog, margin_l)},
132 {"MarginR", ASS_INT, offsetof(ASSDialog, margin_r)},
133 {"MarginV", ASS_INT, offsetof(ASSDialog, margin_v)},
134 {"Effect", ASS_STR, offsetof(ASSDialog, effect) },
135 {"Text", ASS_STR, offsetof(ASSDialog, text) },
142 typedef int (*ASSConvertFunc)(void *dest, const char *buf, int len);
144 static int convert_str(void *dest, const char *buf, int len)
146 char *str = av_malloc(len + 1);
148 memcpy(str, buf, len);
151 av_free(*(void **)dest);
152 *(char **)dest = str;
156 static int convert_int(void *dest, const char *buf, int len)
158 return sscanf(buf, "%d", (int *)dest) == 1;
160 static int convert_flt(void *dest, const char *buf, int len)
162 return sscanf(buf, "%f", (float *)dest) == 1;
164 static int convert_color(void *dest, const char *buf, int len)
166 return sscanf(buf, "&H%8x", (int *)dest) == 1 ||
167 sscanf(buf, "%d", (int *)dest) == 1;
169 static int convert_timestamp(void *dest, const char *buf, int len)
172 if ((c = sscanf(buf, "%d:%02d:%02d.%02d", &h, &m, &s, &cs)) == 4)
173 *(int *)dest = 360000*h + 6000*m + 100*s + cs;
176 static int convert_alignment(void *dest, const char *buf, int len)
179 if (sscanf(buf, "%d", &a) == 1) {
180 /* convert V4 Style alignment to V4+ Style */
181 *(int *)dest = a + ((a&4) >> 1) - 5*!!(a&8);
187 static const ASSConvertFunc convert_func[] = {
188 [ASS_STR] = convert_str,
189 [ASS_INT] = convert_int,
190 [ASS_FLT] = convert_flt,
191 [ASS_COLOR] = convert_color,
192 [ASS_TIMESTAMP] = convert_timestamp,
193 [ASS_ALGN] = convert_alignment,
197 struct ASSSplitContext {
200 int field_number[FF_ARRAY_ELEMS(ass_sections)];
201 int *field_order[FF_ARRAY_ELEMS(ass_sections)];
205 static uint8_t *realloc_section_array(ASSSplitContext *ctx)
207 const ASSSection *section = &ass_sections[ctx->current_section];
208 int *count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
209 void **section_ptr = (void **)((uint8_t *)&ctx->ass + section->offset);
210 uint8_t *tmp = av_realloc_array(*section_ptr, (*count+1), section->size);
214 tmp += *count * section->size;
215 memset(tmp, 0, section->size);
220 static inline int is_eol(char buf)
222 return buf == '\r' || buf == '\n' || buf == 0;
225 static inline const char *skip_space(const char *buf)
232 static int *get_default_field_orders(const ASSSection *section, int *number)
235 int *order = av_malloc_array(FF_ARRAY_ELEMS(section->fields), sizeof(*order));
239 for (i = 0; section->fields[i].name; i++)
242 while (i < FF_ARRAY_ELEMS(section->fields))
247 static const char *ass_split_section(ASSSplitContext *ctx, const char *buf)
249 const ASSSection *section = &ass_sections[ctx->current_section];
250 int *number = &ctx->field_number[ctx->current_section];
251 int *order = ctx->field_order[ctx->current_section];
254 while (buf && *buf) {
256 ctx->current_section = -1;
259 if (buf[0] == ';' || (buf[0] == '!' && buf[1] == ':'))
260 goto next_line; // skip comments
262 len = strcspn(buf, ":\r\n");
263 if (buf[len] == ':' &&
264 (!section->fields_header || strncmp(buf, section->fields_header, len))) {
265 for (i = 0; i < FF_ARRAY_ELEMS(ass_sections); i++) {
266 if (ass_sections[i].fields_header &&
267 !strncmp(buf, ass_sections[i].fields_header, len)) {
268 ctx->current_section = i;
269 section = &ass_sections[ctx->current_section];
270 number = &ctx->field_number[ctx->current_section];
271 order = ctx->field_order[ctx->current_section];
276 if (section->format_header && !order) {
277 len = strlen(section->format_header);
278 if (!strncmp(buf, section->format_header, len) && buf[len] == ':') {
280 while (!is_eol(*buf)) {
281 buf = skip_space(buf);
282 len = strcspn(buf, ", \r\n");
283 if (!(tmp = av_realloc_array(order, (*number + 1), sizeof(*order))))
287 for (i=0; section->fields[i].name; i++)
288 if (!strncmp(buf, section->fields[i].name, len)) {
293 buf = skip_space(buf + len + (buf[len] == ','));
295 ctx->field_order[ctx->current_section] = order;
299 if (section->fields_header) {
300 len = strlen(section->fields_header);
301 if (!strncmp(buf, section->fields_header, len) && buf[len] == ':') {
302 uint8_t *ptr, *struct_ptr = realloc_section_array(ctx);
303 if (!struct_ptr) return NULL;
305 /* No format header line found so far, assume default */
307 order = get_default_field_orders(section, number);
310 ctx->field_order[ctx->current_section] = order;
314 for (i=0; !is_eol(*buf) && i < *number; i++) {
315 int last = i == *number - 1;
316 buf = skip_space(buf);
317 len = strcspn(buf, last ? "\r\n" : ",\r\n");
319 ASSFieldType type = section->fields[order[i]].type;
320 ptr = struct_ptr + section->fields[order[i]].offset;
321 convert_func[type](ptr, buf, len);
324 if (!last && *buf) buf++;
325 buf = skip_space(buf);
329 len = strcspn(buf, ":\r\n");
330 if (buf[len] == ':') {
331 for (i=0; section->fields[i].name; i++)
332 if (!strncmp(buf, section->fields[i].name, len)) {
333 ASSFieldType type = section->fields[i].type;
334 uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
335 ptr += section->fields[i].offset;
336 buf = skip_space(buf + len + 1);
337 convert_func[type](ptr, buf, strcspn(buf, "\r\n"));
343 buf += strcspn(buf, "\n");
349 static int ass_split(ASSSplitContext *ctx, const char *buf)
354 if (ctx->current_section >= 0)
355 buf = ass_split_section(ctx, buf);
357 while (buf && *buf) {
358 if (sscanf(buf, "[%15[0-9A-Za-z+ ]]%c", section, &c) == 2) {
359 buf += strcspn(buf, "\n");
361 for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
362 if (!strcmp(section, ass_sections[i].section)) {
363 ctx->current_section = i;
364 buf = ass_split_section(ctx, buf);
367 buf += strcspn(buf, "\n");
371 return buf ? 0 : AVERROR_INVALIDDATA;
374 ASSSplitContext *ff_ass_split(const char *buf)
376 ASSSplitContext *ctx = av_mallocz(sizeof(*ctx));
379 ctx->current_section = -1;
380 if (ass_split(ctx, buf) < 0) {
381 ff_ass_split_free(ctx);
387 static void free_section(ASSSplitContext *ctx, const ASSSection *section)
389 uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
390 int i, j, *count, c = 1;
392 if (section->format_header) {
394 count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
399 for (i=0; i<*count; i++, ptr += section->size)
400 for (j=0; section->fields[j].name; j++) {
401 const ASSFields *field = §ion->fields[j];
402 if (field->type == ASS_STR)
403 av_freep(ptr + field->offset);
407 if (section->format_header)
408 av_freep((uint8_t *)&ctx->ass + section->offset);
411 ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
412 int cache, int *number)
414 ASSDialog *dialog = NULL;
417 for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
418 if (!strcmp(ass_sections[i].section, "Events")) {
419 free_section(ctx, &ass_sections[i]);
422 count = ctx->ass.dialogs_count;
423 if (ass_split(ctx, buf) == 0)
424 dialog = ctx->ass.dialogs + count;
426 *number = ctx->ass.dialogs_count - count;
430 void ff_ass_free_dialog(ASSDialog **dialogp)
432 ASSDialog *dialog = *dialogp;
435 av_freep(&dialog->style);
436 av_freep(&dialog->name);
437 av_freep(&dialog->effect);
438 av_freep(&dialog->text);
442 ASSDialog *ff_ass_split_dialog2(ASSSplitContext *ctx, const char *buf)
445 static const ASSFields fields[] = {
446 {"ReadOrder", ASS_INT, offsetof(ASSDialog, readorder)},
447 {"Layer", ASS_INT, offsetof(ASSDialog, layer) },
448 {"Style", ASS_STR, offsetof(ASSDialog, style) },
449 {"Name", ASS_STR, offsetof(ASSDialog, name) },
450 {"MarginL", ASS_INT, offsetof(ASSDialog, margin_l) },
451 {"MarginR", ASS_INT, offsetof(ASSDialog, margin_r) },
452 {"MarginV", ASS_INT, offsetof(ASSDialog, margin_v) },
453 {"Effect", ASS_STR, offsetof(ASSDialog, effect) },
454 {"Text", ASS_STR, offsetof(ASSDialog, text) },
457 ASSDialog *dialog = av_mallocz(sizeof(*dialog));
461 for (i = 0; i < FF_ARRAY_ELEMS(fields); i++) {
463 const int last = i == FF_ARRAY_ELEMS(fields) - 1;
464 const ASSFieldType type = fields[i].type;
465 uint8_t *ptr = (uint8_t *)dialog + fields[i].offset;
466 buf = skip_space(buf);
467 len = last ? strlen(buf) : strcspn(buf, ",");
468 if (len >= INT_MAX) {
469 ff_ass_free_dialog(&dialog);
472 convert_func[type](ptr, buf, len);
479 void ff_ass_split_free(ASSSplitContext *ctx)
483 for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++) {
484 free_section(ctx, &ass_sections[i]);
485 av_freep(&(ctx->field_order[i]));
492 int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
495 const char *text = NULL;
499 while (buf && *buf) {
500 if (text && callbacks->text &&
501 (sscanf(buf, "\\%1[nN]", new_line) == 1 ||
502 !strncmp(buf, "{\\", 2))) {
503 callbacks->text(priv, text, text_len);
506 if (sscanf(buf, "\\%1[nN]", new_line) == 1) {
507 if (callbacks->new_line)
508 callbacks->new_line(priv, new_line[0] == 'N');
510 } else if (!strncmp(buf, "{\\", 2)) {
512 while (*buf == '\\') {
513 char style[2], c[2], sep[2], c_num[2] = "0", tmp[128] = {0};
514 unsigned int color = 0xFFFFFFFF;
515 int len, size = -1, an = -1, alpha = -1;
516 int x1, y1, x2, y2, t1 = -1, t2 = -1;
517 if (sscanf(buf, "\\%1[bisu]%1[01\\}]%n", style, c, &len) > 1) {
518 int close = c[0] == '0' ? 1 : c[0] == '1' ? 0 : -1;
520 if (callbacks->style)
521 callbacks->style(priv, style[0], close);
522 } else if (sscanf(buf, "\\c%1[\\}]%n", sep, &len) > 0 ||
523 sscanf(buf, "\\c&H%X&%1[\\}]%n", &color, sep, &len) > 1 ||
524 sscanf(buf, "\\%1[1234]c%1[\\}]%n", c_num, sep, &len) > 1 ||
525 sscanf(buf, "\\%1[1234]c&H%X&%1[\\}]%n", c_num, &color, sep, &len) > 2) {
526 if (callbacks->color)
527 callbacks->color(priv, color, c_num[0] - '0');
528 } else if (sscanf(buf, "\\alpha%1[\\}]%n", sep, &len) > 0 ||
529 sscanf(buf, "\\alpha&H%2X&%1[\\}]%n", &alpha, sep, &len) > 1 ||
530 sscanf(buf, "\\%1[1234]a%1[\\}]%n", c_num, sep, &len) > 1 ||
531 sscanf(buf, "\\%1[1234]a&H%2X&%1[\\}]%n", c_num, &alpha, sep, &len) > 2) {
532 if (callbacks->alpha)
533 callbacks->alpha(priv, alpha, c_num[0] - '0');
534 } else if (sscanf(buf, "\\fn%1[\\}]%n", sep, &len) > 0 ||
535 sscanf(buf, "\\fn%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
536 if (callbacks->font_name)
537 callbacks->font_name(priv, tmp[0] ? tmp : NULL);
538 } else if (sscanf(buf, "\\fs%1[\\}]%n", sep, &len) > 0 ||
539 sscanf(buf, "\\fs%u%1[\\}]%n", &size, sep, &len) > 1) {
540 if (callbacks->font_size)
541 callbacks->font_size(priv, size);
542 } else if (sscanf(buf, "\\a%1[\\}]%n", sep, &len) > 0 ||
543 sscanf(buf, "\\a%2u%1[\\}]%n", &an, sep, &len) > 1 ||
544 sscanf(buf, "\\an%1[\\}]%n", sep, &len) > 0 ||
545 sscanf(buf, "\\an%1u%1[\\}]%n", &an, sep, &len) > 1) {
546 if (an != -1 && buf[2] != 'n')
547 an = (an&3) + (an&4 ? 6 : an&8 ? 3 : 0);
548 if (callbacks->alignment)
549 callbacks->alignment(priv, an);
550 } else if (sscanf(buf, "\\r%1[\\}]%n", sep, &len) > 0 ||
551 sscanf(buf, "\\r%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
552 if (callbacks->cancel_overrides)
553 callbacks->cancel_overrides(priv, tmp);
554 } else if (sscanf(buf, "\\move(%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, sep, &len) > 4 ||
555 sscanf(buf, "\\move(%d,%d,%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, &t1, &t2, sep, &len) > 6) {
557 callbacks->move(priv, x1, y1, x2, y2, t1, t2);
558 } else if (sscanf(buf, "\\pos(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
560 callbacks->move(priv, x1, y1, x1, y1, -1, -1);
561 } else if (sscanf(buf, "\\org(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
562 if (callbacks->origin)
563 callbacks->origin(priv, x1, y1);
565 len = strcspn(buf+1, "\\}") + 2; /* skip unknown code */
570 return AVERROR_INVALIDDATA;
580 if (text && callbacks->text)
581 callbacks->text(priv, text, text_len);
583 callbacks->end(priv);
587 ASSStyle *ff_ass_style_get(ASSSplitContext *ctx, const char *style)
589 ASS *ass = &ctx->ass;
592 if (!style || !*style)
594 for (i=0; i<ass->styles_count; i++)
595 if (ass->styles[i].name && !strcmp(ass->styles[i].name, style))
596 return ass->styles + i;