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 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. ctx->current_section = -1;
  353. if (ass_split(ctx, buf) < 0) {
  354. ff_ass_split_free(ctx);
  355. return NULL;
  356. }
  357. return ctx;
  358. }
  359. static void free_section(ASSSplitContext *ctx, const ASSSection *section)
  360. {
  361. uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
  362. int i, j, *count, c = 1;
  363. if (section->format_header) {
  364. ptr = *(void **)ptr;
  365. count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
  366. } else
  367. count = &c;
  368. if (ptr)
  369. for (i=0; i<*count; i++, ptr += section->size)
  370. for (j=0; section->fields[j].name; j++) {
  371. const ASSFields *field = &section->fields[j];
  372. if (field->type == ASS_STR)
  373. av_freep(ptr + field->offset);
  374. }
  375. *count = 0;
  376. if (section->format_header)
  377. av_freep((uint8_t *)&ctx->ass + section->offset);
  378. }
  379. ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
  380. int cache, int *number)
  381. {
  382. ASSDialog *dialog = NULL;
  383. int i, count;
  384. if (!cache)
  385. for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
  386. if (!strcmp(ass_sections[i].section, "Events")) {
  387. free_section(ctx, &ass_sections[i]);
  388. break;
  389. }
  390. count = ctx->ass.dialogs_count;
  391. if (ass_split(ctx, buf) == 0)
  392. dialog = ctx->ass.dialogs + count;
  393. if (number)
  394. *number = ctx->ass.dialogs_count - count;
  395. return dialog;
  396. }
  397. void ff_ass_free_dialog(ASSDialog **dialogp)
  398. {
  399. ASSDialog *dialog = *dialogp;
  400. if (!dialog)
  401. return;
  402. av_freep(&dialog->style);
  403. av_freep(&dialog->name);
  404. av_freep(&dialog->effect);
  405. av_freep(&dialog->text);
  406. av_freep(dialogp);
  407. }
  408. ASSDialog *ff_ass_split_dialog2(ASSSplitContext *ctx, const char *buf)
  409. {
  410. int i;
  411. static const ASSFields fields[] = {
  412. {"ReadOrder", ASS_INT, offsetof(ASSDialog, readorder)},
  413. {"Layer", ASS_INT, offsetof(ASSDialog, layer) },
  414. {"Style", ASS_STR, offsetof(ASSDialog, style) },
  415. {"Name", ASS_STR, offsetof(ASSDialog, name) },
  416. {"MarginL", ASS_INT, offsetof(ASSDialog, margin_l) },
  417. {"MarginR", ASS_INT, offsetof(ASSDialog, margin_r) },
  418. {"MarginV", ASS_INT, offsetof(ASSDialog, margin_v) },
  419. {"Effect", ASS_STR, offsetof(ASSDialog, effect) },
  420. {"Text", ASS_STR, offsetof(ASSDialog, text) },
  421. };
  422. ASSDialog *dialog = av_mallocz(sizeof(*dialog));
  423. if (!dialog)
  424. return NULL;
  425. for (i = 0; i < FF_ARRAY_ELEMS(fields); i++) {
  426. size_t len;
  427. const int last = i == FF_ARRAY_ELEMS(fields) - 1;
  428. const ASSFieldType type = fields[i].type;
  429. uint8_t *ptr = (uint8_t *)dialog + fields[i].offset;
  430. buf = skip_space(buf);
  431. len = last ? strlen(buf) : strcspn(buf, ",");
  432. if (len >= INT_MAX) {
  433. ff_ass_free_dialog(&dialog);
  434. return NULL;
  435. }
  436. convert_func[type](ptr, buf, len);
  437. buf += len;
  438. if (*buf) buf++;
  439. }
  440. return dialog;
  441. }
  442. void ff_ass_split_free(ASSSplitContext *ctx)
  443. {
  444. if (ctx) {
  445. int i;
  446. for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++) {
  447. free_section(ctx, &ass_sections[i]);
  448. av_freep(&(ctx->field_order[i]));
  449. }
  450. av_free(ctx);
  451. }
  452. }
  453. int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
  454. const char *buf)
  455. {
  456. const char *text = NULL;
  457. char new_line[2];
  458. int text_len = 0;
  459. while (buf && *buf) {
  460. if (text && callbacks->text &&
  461. (sscanf(buf, "\\%1[nN]", new_line) == 1 ||
  462. !strncmp(buf, "{\\", 2))) {
  463. callbacks->text(priv, text, text_len);
  464. text = NULL;
  465. }
  466. if (sscanf(buf, "\\%1[nN]", new_line) == 1) {
  467. if (callbacks->new_line)
  468. callbacks->new_line(priv, new_line[0] == 'N');
  469. buf += 2;
  470. } else if (!strncmp(buf, "{\\", 2)) {
  471. buf++;
  472. while (*buf == '\\') {
  473. char style[2], c[2], sep[2], c_num[2] = "0", tmp[128] = {0};
  474. unsigned int color = 0xFFFFFFFF;
  475. int len, size = -1, an = -1, alpha = -1;
  476. int x1, y1, x2, y2, t1 = -1, t2 = -1;
  477. if (sscanf(buf, "\\%1[bisu]%1[01\\}]%n", style, c, &len) > 1) {
  478. int close = c[0] == '0' ? 1 : c[0] == '1' ? 0 : -1;
  479. len += close != -1;
  480. if (callbacks->style)
  481. callbacks->style(priv, style[0], close);
  482. } else if (sscanf(buf, "\\c%1[\\}]%n", sep, &len) > 0 ||
  483. sscanf(buf, "\\c&H%X&%1[\\}]%n", &color, sep, &len) > 1 ||
  484. sscanf(buf, "\\%1[1234]c%1[\\}]%n", c_num, sep, &len) > 1 ||
  485. sscanf(buf, "\\%1[1234]c&H%X&%1[\\}]%n", c_num, &color, sep, &len) > 2) {
  486. if (callbacks->color)
  487. callbacks->color(priv, color, c_num[0] - '0');
  488. } else if (sscanf(buf, "\\alpha%1[\\}]%n", sep, &len) > 0 ||
  489. sscanf(buf, "\\alpha&H%2X&%1[\\}]%n", &alpha, sep, &len) > 1 ||
  490. sscanf(buf, "\\%1[1234]a%1[\\}]%n", c_num, sep, &len) > 1 ||
  491. sscanf(buf, "\\%1[1234]a&H%2X&%1[\\}]%n", c_num, &alpha, sep, &len) > 2) {
  492. if (callbacks->alpha)
  493. callbacks->alpha(priv, alpha, c_num[0] - '0');
  494. } else if (sscanf(buf, "\\fn%1[\\}]%n", sep, &len) > 0 ||
  495. sscanf(buf, "\\fn%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
  496. if (callbacks->font_name)
  497. callbacks->font_name(priv, tmp[0] ? tmp : NULL);
  498. } else if (sscanf(buf, "\\fs%1[\\}]%n", sep, &len) > 0 ||
  499. sscanf(buf, "\\fs%u%1[\\}]%n", &size, sep, &len) > 1) {
  500. if (callbacks->font_size)
  501. callbacks->font_size(priv, size);
  502. } else if (sscanf(buf, "\\a%1[\\}]%n", sep, &len) > 0 ||
  503. sscanf(buf, "\\a%2u%1[\\}]%n", &an, sep, &len) > 1 ||
  504. sscanf(buf, "\\an%1[\\}]%n", sep, &len) > 0 ||
  505. sscanf(buf, "\\an%1u%1[\\}]%n", &an, sep, &len) > 1) {
  506. if (an != -1 && buf[2] != 'n')
  507. an = (an&3) + (an&4 ? 6 : an&8 ? 3 : 0);
  508. if (callbacks->alignment)
  509. callbacks->alignment(priv, an);
  510. } else if (sscanf(buf, "\\r%1[\\}]%n", sep, &len) > 0 ||
  511. sscanf(buf, "\\r%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
  512. if (callbacks->cancel_overrides)
  513. callbacks->cancel_overrides(priv, tmp);
  514. } else if (sscanf(buf, "\\move(%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, sep, &len) > 4 ||
  515. sscanf(buf, "\\move(%d,%d,%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, &t1, &t2, sep, &len) > 6) {
  516. if (callbacks->move)
  517. callbacks->move(priv, x1, y1, x2, y2, t1, t2);
  518. } else if (sscanf(buf, "\\pos(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
  519. if (callbacks->move)
  520. callbacks->move(priv, x1, y1, x1, y1, -1, -1);
  521. } else if (sscanf(buf, "\\org(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
  522. if (callbacks->origin)
  523. callbacks->origin(priv, x1, y1);
  524. } else {
  525. len = strcspn(buf+1, "\\}") + 2; /* skip unknown code */
  526. }
  527. buf += len - 1;
  528. }
  529. if (*buf++ != '}')
  530. return AVERROR_INVALIDDATA;
  531. } else {
  532. if (!text) {
  533. text = buf;
  534. text_len = 1;
  535. } else
  536. text_len++;
  537. buf++;
  538. }
  539. }
  540. if (text && callbacks->text)
  541. callbacks->text(priv, text, text_len);
  542. if (callbacks->end)
  543. callbacks->end(priv);
  544. return 0;
  545. }
  546. ASSStyle *ff_ass_style_get(ASSSplitContext *ctx, const char *style)
  547. {
  548. ASS *ass = &ctx->ass;
  549. int i;
  550. if (!style || !*style)
  551. style = "Default";
  552. for (i=0; i<ass->styles_count; i++)
  553. if (ass->styles[i].name && !strcmp(ass->styles[i].name, style))
  554. return ass->styles + i;
  555. return NULL;
  556. }