You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

601 lines
23KB

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