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.

471 lines
17KB

  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[10];
  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. {"BackColour", ASS_COLOR,offsetof(ASSStyle, back_color) },
  67. {"Bold", ASS_INT, offsetof(ASSStyle, bold) },
  68. {"Italic", ASS_INT, offsetof(ASSStyle, italic) },
  69. {"Underline", ASS_INT, offsetof(ASSStyle, underline) },
  70. {"Alignment", ASS_INT, offsetof(ASSStyle, alignment) },
  71. {0},
  72. }
  73. },
  74. { .section = "V4 Styles",
  75. .format_header = "Format",
  76. .fields_header = "Style",
  77. .size = sizeof(ASSStyle),
  78. .offset = offsetof(ASS, styles),
  79. .offset_count = offsetof(ASS, styles_count),
  80. .fields = {{"Name", ASS_STR, offsetof(ASSStyle, name) },
  81. {"Fontname", ASS_STR, offsetof(ASSStyle, font_name) },
  82. {"Fontsize", ASS_INT, offsetof(ASSStyle, font_size) },
  83. {"PrimaryColour",ASS_COLOR,offsetof(ASSStyle, primary_color)},
  84. {"BackColour", ASS_COLOR,offsetof(ASSStyle, back_color) },
  85. {"Bold", ASS_INT, offsetof(ASSStyle, bold) },
  86. {"Italic", ASS_INT, offsetof(ASSStyle, italic) },
  87. {"Alignment", ASS_ALGN, offsetof(ASSStyle, alignment) },
  88. {0},
  89. }
  90. },
  91. { .section = "Events",
  92. .format_header = "Format",
  93. .fields_header = "Dialogue",
  94. .size = sizeof(ASSDialog),
  95. .offset = offsetof(ASS, dialogs),
  96. .offset_count = offsetof(ASS, dialogs_count),
  97. .fields = {{"Layer", ASS_INT, offsetof(ASSDialog, layer) },
  98. {"Start", ASS_TIMESTAMP, offsetof(ASSDialog, start) },
  99. {"End", ASS_TIMESTAMP, offsetof(ASSDialog, end) },
  100. {"Style", ASS_STR, offsetof(ASSDialog, style) },
  101. {"Text", ASS_STR, offsetof(ASSDialog, text) },
  102. {0},
  103. }
  104. },
  105. };
  106. typedef int (*ASSConvertFunc)(void *dest, const char *buf, int len);
  107. static int convert_str(void *dest, const char *buf, int len)
  108. {
  109. char *str = av_malloc(len + 1);
  110. if (str) {
  111. memcpy(str, buf, len);
  112. str[len] = 0;
  113. if (*(void **)dest)
  114. av_free(*(void **)dest);
  115. *(char **)dest = str;
  116. }
  117. return !str;
  118. }
  119. static int convert_int(void *dest, const char *buf, int len)
  120. {
  121. return sscanf(buf, "%d", (int *)dest) == 1;
  122. }
  123. static int convert_flt(void *dest, const char *buf, int len)
  124. {
  125. return sscanf(buf, "%f", (float *)dest) == 1;
  126. }
  127. static int convert_color(void *dest, const char *buf, int len)
  128. {
  129. return sscanf(buf, "&H%8x", (int *)dest) == 1 ||
  130. sscanf(buf, "%d", (int *)dest) == 1;
  131. }
  132. static int convert_timestamp(void *dest, const char *buf, int len)
  133. {
  134. int c, h, m, s, cs;
  135. if ((c = sscanf(buf, "%d:%02d:%02d.%02d", &h, &m, &s, &cs)) == 4)
  136. *(int *)dest = 360000*h + 6000*m + 100*s + cs;
  137. return c == 4;
  138. }
  139. static int convert_alignment(void *dest, const char *buf, int len)
  140. {
  141. int a;
  142. if (sscanf(buf, "%d", &a) == 1) {
  143. /* convert V4 Style alignment to V4+ Style */
  144. *(int *)dest = a + ((a&4) >> 1) - 5*!!(a&8);
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. static const ASSConvertFunc convert_func[] = {
  150. [ASS_STR] = convert_str,
  151. [ASS_INT] = convert_int,
  152. [ASS_FLT] = convert_flt,
  153. [ASS_COLOR] = convert_color,
  154. [ASS_TIMESTAMP] = convert_timestamp,
  155. [ASS_ALGN] = convert_alignment,
  156. };
  157. struct ASSSplitContext {
  158. ASS ass;
  159. int current_section;
  160. int field_number[FF_ARRAY_ELEMS(ass_sections)];
  161. int *field_order[FF_ARRAY_ELEMS(ass_sections)];
  162. };
  163. static uint8_t *realloc_section_array(ASSSplitContext *ctx)
  164. {
  165. const ASSSection *section = &ass_sections[ctx->current_section];
  166. int *count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
  167. void **section_ptr = (void **)((uint8_t *)&ctx->ass + section->offset);
  168. uint8_t *tmp = av_realloc(*section_ptr, (*count+1)*section->size);
  169. if (!tmp)
  170. return NULL;
  171. *section_ptr = tmp;
  172. tmp += *count * section->size;
  173. memset(tmp, 0, section->size);
  174. (*count)++;
  175. return tmp;
  176. }
  177. static inline int is_eol(char buf)
  178. {
  179. return buf == '\r' || buf == '\n' || buf == 0;
  180. }
  181. static inline const char *skip_space(const char *buf)
  182. {
  183. while (*buf == ' ')
  184. buf++;
  185. return buf;
  186. }
  187. static const char *ass_split_section(ASSSplitContext *ctx, const char *buf)
  188. {
  189. const ASSSection *section = &ass_sections[ctx->current_section];
  190. int *number = &ctx->field_number[ctx->current_section];
  191. int *order = ctx->field_order[ctx->current_section];
  192. int *tmp, i, len;
  193. while (buf && *buf) {
  194. if (buf[0] == '[') {
  195. ctx->current_section = -1;
  196. break;
  197. }
  198. if (buf[0] == ';' || (buf[0] == '!' && buf[1] == ':')) {
  199. /* skip comments */
  200. } else if (section->format_header && !order) {
  201. len = strlen(section->format_header);
  202. if (strncmp(buf, section->format_header, len) || buf[len] != ':')
  203. return NULL;
  204. buf += len + 1;
  205. while (!is_eol(*buf)) {
  206. buf = skip_space(buf);
  207. len = strcspn(buf, ", \r\n");
  208. if (!(tmp = av_realloc(order, (*number + 1) * sizeof(*order))))
  209. return NULL;
  210. order = tmp;
  211. order[*number] = -1;
  212. for (i=0; section->fields[i].name; i++)
  213. if (!strncmp(buf, section->fields[i].name, len)) {
  214. order[*number] = i;
  215. break;
  216. }
  217. (*number)++;
  218. buf = skip_space(buf + len + (buf[len] == ','));
  219. }
  220. ctx->field_order[ctx->current_section] = order;
  221. } else if (section->fields_header) {
  222. len = strlen(section->fields_header);
  223. if (!strncmp(buf, section->fields_header, len) && buf[len] == ':') {
  224. uint8_t *ptr, *struct_ptr = realloc_section_array(ctx);
  225. if (!struct_ptr) return NULL;
  226. buf += len + 1;
  227. for (i=0; !is_eol(*buf) && i < *number; i++) {
  228. int last = i == *number - 1;
  229. buf = skip_space(buf);
  230. len = strcspn(buf, last ? "\r\n" : ",\r\n");
  231. if (order[i] >= 0) {
  232. ASSFieldType type = section->fields[order[i]].type;
  233. ptr = struct_ptr + section->fields[order[i]].offset;
  234. convert_func[type](ptr, buf, len);
  235. }
  236. buf = skip_space(buf + len + !last);
  237. }
  238. }
  239. } else {
  240. len = strcspn(buf, ":\r\n");
  241. if (buf[len] == ':') {
  242. for (i=0; section->fields[i].name; i++)
  243. if (!strncmp(buf, section->fields[i].name, len)) {
  244. ASSFieldType type = section->fields[i].type;
  245. uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
  246. ptr += section->fields[i].offset;
  247. buf = skip_space(buf + len + 1);
  248. convert_func[type](ptr, buf, strcspn(buf, "\r\n"));
  249. break;
  250. }
  251. }
  252. }
  253. buf += strcspn(buf, "\n") + 1;
  254. }
  255. return buf;
  256. }
  257. static int ass_split(ASSSplitContext *ctx, const char *buf)
  258. {
  259. char c, section[16];
  260. int i;
  261. if (ctx->current_section >= 0)
  262. buf = ass_split_section(ctx, buf);
  263. while (buf && *buf) {
  264. if (sscanf(buf, "[%15[0-9A-Za-z+ ]]%c", section, &c) == 2) {
  265. buf += strcspn(buf, "\n") + 1;
  266. for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
  267. if (!strcmp(section, ass_sections[i].section)) {
  268. ctx->current_section = i;
  269. buf = ass_split_section(ctx, buf);
  270. }
  271. } else
  272. buf += strcspn(buf, "\n") + 1;
  273. }
  274. return buf ? 0 : AVERROR_INVALIDDATA;
  275. }
  276. ASSSplitContext *ff_ass_split(const char *buf)
  277. {
  278. ASSSplitContext *ctx = av_mallocz(sizeof(*ctx));
  279. ctx->current_section = -1;
  280. if (ass_split(ctx, buf) < 0) {
  281. ff_ass_split_free(ctx);
  282. return NULL;
  283. }
  284. return ctx;
  285. }
  286. static void free_section(ASSSplitContext *ctx, const ASSSection *section)
  287. {
  288. uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
  289. int i, j, *count, c = 1;
  290. if (section->format_header) {
  291. ptr = *(void **)ptr;
  292. count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
  293. } else
  294. count = &c;
  295. if (ptr)
  296. for (i=0; i<*count; i++, ptr += section->size)
  297. for (j=0; section->fields[j].name; j++) {
  298. const ASSFields *field = &section->fields[j];
  299. if (field->type == ASS_STR)
  300. av_freep(ptr + field->offset);
  301. }
  302. *count = 0;
  303. if (section->format_header)
  304. av_freep((uint8_t *)&ctx->ass + section->offset);
  305. }
  306. ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
  307. int cache, int *number)
  308. {
  309. ASSDialog *dialog = NULL;
  310. int i, count;
  311. if (!cache)
  312. for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
  313. if (!strcmp(ass_sections[i].section, "Events")) {
  314. free_section(ctx, &ass_sections[i]);
  315. break;
  316. }
  317. count = ctx->ass.dialogs_count;
  318. if (ass_split(ctx, buf) == 0)
  319. dialog = ctx->ass.dialogs + count;
  320. if (number)
  321. *number = ctx->ass.dialogs_count - count;
  322. return dialog;
  323. }
  324. void ff_ass_split_free(ASSSplitContext *ctx)
  325. {
  326. if (ctx) {
  327. int i;
  328. for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++) {
  329. free_section(ctx, &ass_sections[i]);
  330. av_freep(&(ctx->field_order[i]));
  331. }
  332. av_free(ctx);
  333. }
  334. }
  335. int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
  336. const char *buf)
  337. {
  338. const char *text = NULL;
  339. char new_line[2];
  340. int text_len = 0;
  341. while (*buf) {
  342. if (text && callbacks->text &&
  343. (sscanf(buf, "\\%1[nN]", new_line) == 1 ||
  344. !strncmp(buf, "{\\", 2))) {
  345. callbacks->text(priv, text, text_len);
  346. text = NULL;
  347. }
  348. if (sscanf(buf, "\\%1[nN]", new_line) == 1) {
  349. if (callbacks->new_line)
  350. callbacks->new_line(priv, new_line[0] == 'N');
  351. buf += 2;
  352. } else if (!strncmp(buf, "{\\", 2)) {
  353. buf++;
  354. while (*buf == '\\') {
  355. char style[2], c[2], sep[2], c_num[2] = "0", tmp[128] = {0};
  356. unsigned int color = 0xFFFFFFFF;
  357. int len, size = -1, an = -1, alpha = -1;
  358. int x1, y1, x2, y2, t1 = -1, t2 = -1;
  359. if (sscanf(buf, "\\%1[bisu]%1[01\\}]%n", style, c, &len) > 1) {
  360. int close = c[0] == '0' ? 1 : c[0] == '1' ? 0 : -1;
  361. len += close != -1;
  362. if (callbacks->style)
  363. callbacks->style(priv, style[0], close);
  364. } else if (sscanf(buf, "\\c%1[\\}]%n", sep, &len) > 0 ||
  365. sscanf(buf, "\\c&H%X&%1[\\}]%n", &color, sep, &len) > 1 ||
  366. sscanf(buf, "\\%1[1234]c%1[\\}]%n", c_num, sep, &len) > 1 ||
  367. sscanf(buf, "\\%1[1234]c&H%X&%1[\\}]%n", c_num, &color, sep, &len) > 2) {
  368. if (callbacks->color)
  369. callbacks->color(priv, color, c_num[0] - '0');
  370. } else if (sscanf(buf, "\\alpha%1[\\}]%n", sep, &len) > 0 ||
  371. sscanf(buf, "\\alpha&H%2X&%1[\\}]%n", &alpha, sep, &len) > 1 ||
  372. sscanf(buf, "\\%1[1234]a%1[\\}]%n", c_num, sep, &len) > 1 ||
  373. sscanf(buf, "\\%1[1234]a&H%2X&%1[\\}]%n", c_num, &alpha, sep, &len) > 2) {
  374. if (callbacks->alpha)
  375. callbacks->alpha(priv, alpha, c_num[0] - '0');
  376. } else if (sscanf(buf, "\\fn%1[\\}]%n", sep, &len) > 0 ||
  377. sscanf(buf, "\\fn%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
  378. if (callbacks->font_name)
  379. callbacks->font_name(priv, tmp[0] ? tmp : NULL);
  380. } else if (sscanf(buf, "\\fs%1[\\}]%n", sep, &len) > 0 ||
  381. sscanf(buf, "\\fs%u%1[\\}]%n", &size, sep, &len) > 1) {
  382. if (callbacks->font_size)
  383. callbacks->font_size(priv, size);
  384. } else if (sscanf(buf, "\\a%1[\\}]%n", sep, &len) > 0 ||
  385. sscanf(buf, "\\a%2u%1[\\}]%n", &an, sep, &len) > 1 ||
  386. sscanf(buf, "\\an%1[\\}]%n", sep, &len) > 0 ||
  387. sscanf(buf, "\\an%1u%1[\\}]%n", &an, sep, &len) > 1) {
  388. if (an != -1 && buf[2] != 'n')
  389. an = (an&3) + (an&4 ? 6 : an&8 ? 3 : 0);
  390. if (callbacks->alignment)
  391. callbacks->alignment(priv, an);
  392. } else if (sscanf(buf, "\\r%1[\\}]%n", sep, &len) > 0 ||
  393. sscanf(buf, "\\r%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
  394. if (callbacks->cancel_overrides)
  395. callbacks->cancel_overrides(priv, tmp);
  396. } else if (sscanf(buf, "\\move(%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, sep, &len) > 4 ||
  397. sscanf(buf, "\\move(%d,%d,%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, &t1, &t2, sep, &len) > 6) {
  398. if (callbacks->move)
  399. callbacks->move(priv, x1, y1, x2, y2, t1, t2);
  400. } else if (sscanf(buf, "\\pos(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
  401. if (callbacks->move)
  402. callbacks->move(priv, x1, y1, x1, y1, -1, -1);
  403. } else if (sscanf(buf, "\\org(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
  404. if (callbacks->origin)
  405. callbacks->origin(priv, x1, y1);
  406. } else {
  407. len = strcspn(buf+1, "\\}") + 2; /* skip unknown code */
  408. }
  409. buf += len - 1;
  410. }
  411. if (*buf++ != '}')
  412. return AVERROR_INVALIDDATA;
  413. } else {
  414. if (!text) {
  415. text = buf;
  416. text_len = 1;
  417. } else
  418. text_len++;
  419. buf++;
  420. }
  421. }
  422. if (text && callbacks->text)
  423. callbacks->text(priv, text, text_len);
  424. if (callbacks->end)
  425. callbacks->end(priv);
  426. return 0;
  427. }
  428. ASSStyle *ass_style_get(ASSSplitContext *ctx, const char *style)
  429. {
  430. ASS *ass = &ctx->ass;
  431. int i;
  432. if (!style || !*style)
  433. style = "Default";
  434. for (i=0; i<ass->styles_count; i++)
  435. if (!strcmp(ass->styles[i].name, style))
  436. return ass->styles + i;
  437. return NULL;
  438. }