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.

599 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 *tmp, 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 (!(tmp = av_realloc_array(order, (*number + 1), sizeof(*order))))
  264. return NULL;
  265. order = tmp;
  266. order[*number] = -1;
  267. for (i=0; section->fields[i].name; i++)
  268. if (!strncmp(buf, section->fields[i].name, len)) {
  269. order[*number] = i;
  270. break;
  271. }
  272. (*number)++;
  273. buf = skip_space(buf + len + (buf[len] == ','));
  274. }
  275. ctx->field_order[ctx->current_section] = order;
  276. goto next_line;
  277. }
  278. }
  279. if (section->fields_header) {
  280. len = strlen(section->fields_header);
  281. if (!strncmp(buf, section->fields_header, len) && buf[len] == ':') {
  282. uint8_t *ptr, *struct_ptr = realloc_section_array(ctx);
  283. if (!struct_ptr) return NULL;
  284. /* No format header line found so far, assume default */
  285. if (!order) {
  286. order = get_default_field_orders(section, number);
  287. if (!order)
  288. return NULL;
  289. ctx->field_order[ctx->current_section] = order;
  290. }
  291. buf += len + 1;
  292. for (i=0; !is_eol(*buf) && i < *number; i++) {
  293. int last = i == *number - 1;
  294. buf = skip_space(buf);
  295. len = strcspn(buf, last ? "\r\n" : ",\r\n");
  296. if (order[i] >= 0) {
  297. ASSFieldType type = section->fields[order[i]].type;
  298. ptr = struct_ptr + section->fields[order[i]].offset;
  299. convert_func[type](ptr, buf, len);
  300. }
  301. buf += len;
  302. if (!last && *buf) buf++;
  303. buf = skip_space(buf);
  304. }
  305. }
  306. } else {
  307. len = strcspn(buf, ":\r\n");
  308. if (buf[len] == ':') {
  309. for (i=0; section->fields[i].name; i++)
  310. if (!strncmp(buf, section->fields[i].name, len)) {
  311. ASSFieldType type = section->fields[i].type;
  312. uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
  313. ptr += section->fields[i].offset;
  314. buf = skip_space(buf + len + 1);
  315. convert_func[type](ptr, buf, strcspn(buf, "\r\n"));
  316. break;
  317. }
  318. }
  319. }
  320. next_line:
  321. buf += strcspn(buf, "\n");
  322. buf += !!*buf;
  323. }
  324. return buf;
  325. }
  326. static int ass_split(ASSSplitContext *ctx, const char *buf)
  327. {
  328. char c, section[16];
  329. int i;
  330. if (ctx->current_section >= 0)
  331. buf = ass_split_section(ctx, buf);
  332. while (buf && *buf) {
  333. if (sscanf(buf, "[%15[0-9A-Za-z+ ]]%c", section, &c) == 2) {
  334. buf += strcspn(buf, "\n");
  335. buf += !!*buf;
  336. for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
  337. if (!strcmp(section, ass_sections[i].section)) {
  338. ctx->current_section = i;
  339. buf = ass_split_section(ctx, buf);
  340. }
  341. } else {
  342. buf += strcspn(buf, "\n");
  343. buf += !!*buf;
  344. }
  345. }
  346. return buf ? 0 : AVERROR_INVALIDDATA;
  347. }
  348. ASSSplitContext *ff_ass_split(const char *buf)
  349. {
  350. ASSSplitContext *ctx = av_mallocz(sizeof(*ctx));
  351. if (!ctx)
  352. return NULL;
  353. ctx->current_section = -1;
  354. if (ass_split(ctx, buf) < 0) {
  355. ff_ass_split_free(ctx);
  356. return NULL;
  357. }
  358. return ctx;
  359. }
  360. static void free_section(ASSSplitContext *ctx, const ASSSection *section)
  361. {
  362. uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
  363. int i, j, *count, c = 1;
  364. if (section->format_header) {
  365. ptr = *(void **)ptr;
  366. count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
  367. } else
  368. count = &c;
  369. if (ptr)
  370. for (i=0; i<*count; i++, ptr += section->size)
  371. for (j=0; section->fields[j].name; j++) {
  372. const ASSFields *field = &section->fields[j];
  373. if (field->type == ASS_STR)
  374. av_freep(ptr + field->offset);
  375. }
  376. *count = 0;
  377. if (section->format_header)
  378. av_freep((uint8_t *)&ctx->ass + section->offset);
  379. }
  380. ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
  381. int cache, int *number)
  382. {
  383. ASSDialog *dialog = NULL;
  384. int i, count;
  385. if (!cache)
  386. for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
  387. if (!strcmp(ass_sections[i].section, "Events")) {
  388. free_section(ctx, &ass_sections[i]);
  389. break;
  390. }
  391. count = ctx->ass.dialogs_count;
  392. if (ass_split(ctx, buf) == 0)
  393. dialog = ctx->ass.dialogs + count;
  394. if (number)
  395. *number = ctx->ass.dialogs_count - count;
  396. return dialog;
  397. }
  398. void ff_ass_free_dialog(ASSDialog **dialogp)
  399. {
  400. ASSDialog *dialog = *dialogp;
  401. if (!dialog)
  402. return;
  403. av_freep(&dialog->style);
  404. av_freep(&dialog->name);
  405. av_freep(&dialog->effect);
  406. av_freep(&dialog->text);
  407. av_freep(dialogp);
  408. }
  409. ASSDialog *ff_ass_split_dialog2(ASSSplitContext *ctx, const char *buf)
  410. {
  411. int i;
  412. static const ASSFields fields[] = {
  413. {"ReadOrder", ASS_INT, offsetof(ASSDialog, readorder)},
  414. {"Layer", ASS_INT, offsetof(ASSDialog, layer) },
  415. {"Style", ASS_STR, offsetof(ASSDialog, style) },
  416. {"Name", ASS_STR, offsetof(ASSDialog, name) },
  417. {"MarginL", ASS_INT, offsetof(ASSDialog, margin_l) },
  418. {"MarginR", ASS_INT, offsetof(ASSDialog, margin_r) },
  419. {"MarginV", ASS_INT, offsetof(ASSDialog, margin_v) },
  420. {"Effect", ASS_STR, offsetof(ASSDialog, effect) },
  421. {"Text", ASS_STR, offsetof(ASSDialog, text) },
  422. };
  423. ASSDialog *dialog = av_mallocz(sizeof(*dialog));
  424. if (!dialog)
  425. return NULL;
  426. for (i = 0; i < FF_ARRAY_ELEMS(fields); i++) {
  427. size_t len;
  428. const int last = i == FF_ARRAY_ELEMS(fields) - 1;
  429. const ASSFieldType type = fields[i].type;
  430. uint8_t *ptr = (uint8_t *)dialog + fields[i].offset;
  431. buf = skip_space(buf);
  432. len = last ? strlen(buf) : strcspn(buf, ",");
  433. if (len >= INT_MAX) {
  434. ff_ass_free_dialog(&dialog);
  435. return NULL;
  436. }
  437. convert_func[type](ptr, buf, len);
  438. buf += len;
  439. if (*buf) buf++;
  440. }
  441. return dialog;
  442. }
  443. void ff_ass_split_free(ASSSplitContext *ctx)
  444. {
  445. if (ctx) {
  446. int i;
  447. for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++) {
  448. free_section(ctx, &ass_sections[i]);
  449. av_freep(&(ctx->field_order[i]));
  450. }
  451. av_free(ctx);
  452. }
  453. }
  454. int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
  455. const char *buf)
  456. {
  457. const char *text = NULL;
  458. char new_line[2];
  459. int text_len = 0;
  460. while (buf && *buf) {
  461. if (text && callbacks->text &&
  462. (sscanf(buf, "\\%1[nN]", new_line) == 1 ||
  463. !strncmp(buf, "{\\", 2))) {
  464. callbacks->text(priv, text, text_len);
  465. text = NULL;
  466. }
  467. if (sscanf(buf, "\\%1[nN]", new_line) == 1) {
  468. if (callbacks->new_line)
  469. callbacks->new_line(priv, new_line[0] == 'N');
  470. buf += 2;
  471. } else if (!strncmp(buf, "{\\", 2)) {
  472. buf++;
  473. while (*buf == '\\') {
  474. char style[2], c[2], sep[2], c_num[2] = "0", tmp[128] = {0};
  475. unsigned int color = 0xFFFFFFFF;
  476. int len, size = -1, an = -1, alpha = -1;
  477. int x1, y1, x2, y2, t1 = -1, t2 = -1;
  478. if (sscanf(buf, "\\%1[bisu]%1[01\\}]%n", style, c, &len) > 1) {
  479. int close = c[0] == '0' ? 1 : c[0] == '1' ? 0 : -1;
  480. len += close != -1;
  481. if (callbacks->style)
  482. callbacks->style(priv, style[0], close);
  483. } else if (sscanf(buf, "\\c%1[\\}]%n", sep, &len) > 0 ||
  484. sscanf(buf, "\\c&H%X&%1[\\}]%n", &color, sep, &len) > 1 ||
  485. sscanf(buf, "\\%1[1234]c%1[\\}]%n", c_num, sep, &len) > 1 ||
  486. sscanf(buf, "\\%1[1234]c&H%X&%1[\\}]%n", c_num, &color, sep, &len) > 2) {
  487. if (callbacks->color)
  488. callbacks->color(priv, color, c_num[0] - '0');
  489. } else if (sscanf(buf, "\\alpha%1[\\}]%n", sep, &len) > 0 ||
  490. sscanf(buf, "\\alpha&H%2X&%1[\\}]%n", &alpha, sep, &len) > 1 ||
  491. sscanf(buf, "\\%1[1234]a%1[\\}]%n", c_num, sep, &len) > 1 ||
  492. sscanf(buf, "\\%1[1234]a&H%2X&%1[\\}]%n", c_num, &alpha, sep, &len) > 2) {
  493. if (callbacks->alpha)
  494. callbacks->alpha(priv, alpha, c_num[0] - '0');
  495. } else if (sscanf(buf, "\\fn%1[\\}]%n", sep, &len) > 0 ||
  496. sscanf(buf, "\\fn%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
  497. if (callbacks->font_name)
  498. callbacks->font_name(priv, tmp[0] ? tmp : NULL);
  499. } else if (sscanf(buf, "\\fs%1[\\}]%n", sep, &len) > 0 ||
  500. sscanf(buf, "\\fs%u%1[\\}]%n", &size, sep, &len) > 1) {
  501. if (callbacks->font_size)
  502. callbacks->font_size(priv, size);
  503. } else if (sscanf(buf, "\\a%1[\\}]%n", sep, &len) > 0 ||
  504. sscanf(buf, "\\a%2u%1[\\}]%n", &an, sep, &len) > 1 ||
  505. sscanf(buf, "\\an%1[\\}]%n", sep, &len) > 0 ||
  506. sscanf(buf, "\\an%1u%1[\\}]%n", &an, sep, &len) > 1) {
  507. if (an != -1 && buf[2] != 'n')
  508. an = (an&3) + (an&4 ? 6 : an&8 ? 3 : 0);
  509. if (callbacks->alignment)
  510. callbacks->alignment(priv, an);
  511. } else if (sscanf(buf, "\\r%1[\\}]%n", sep, &len) > 0 ||
  512. sscanf(buf, "\\r%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
  513. if (callbacks->cancel_overrides)
  514. callbacks->cancel_overrides(priv, tmp);
  515. } else if (sscanf(buf, "\\move(%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, sep, &len) > 4 ||
  516. sscanf(buf, "\\move(%d,%d,%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, &t1, &t2, sep, &len) > 6) {
  517. if (callbacks->move)
  518. callbacks->move(priv, x1, y1, x2, y2, t1, t2);
  519. } else if (sscanf(buf, "\\pos(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
  520. if (callbacks->move)
  521. callbacks->move(priv, x1, y1, x1, y1, -1, -1);
  522. } else if (sscanf(buf, "\\org(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
  523. if (callbacks->origin)
  524. callbacks->origin(priv, x1, y1);
  525. } else {
  526. len = strcspn(buf+1, "\\}") + 2; /* skip unknown code */
  527. }
  528. buf += len - 1;
  529. }
  530. if (*buf++ != '}')
  531. return AVERROR_INVALIDDATA;
  532. } else {
  533. if (!text) {
  534. text = buf;
  535. text_len = 1;
  536. } else
  537. text_len++;
  538. buf++;
  539. }
  540. }
  541. if (text && callbacks->text)
  542. callbacks->text(priv, text, text_len);
  543. if (callbacks->end)
  544. callbacks->end(priv);
  545. return 0;
  546. }
  547. ASSStyle *ff_ass_style_get(ASSSplitContext *ctx, const char *style)
  548. {
  549. ASS *ass = &ctx->ass;
  550. int i;
  551. if (!style || !*style)
  552. style = "Default";
  553. for (i=0; i<ass->styles_count; i++)
  554. if (ass->styles[i].name && !strcmp(ass->styles[i].name, style))
  555. return ass->styles + i;
  556. return NULL;
  557. }