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.

370 lines
11KB

  1. /*
  2. * Copyright (c) 2012 Clément Bœsch
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * MicroDVD subtitle decoder
  23. *
  24. * Based on the specifications found here:
  25. * https://trac.videolan.org/vlc/ticket/1825#comment:6
  26. */
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/parseutils.h"
  29. #include "libavutil/bprint.h"
  30. #include "avcodec.h"
  31. #include "ass.h"
  32. static int indexof(const char *s, int c)
  33. {
  34. char *f = strchr(s, c);
  35. return f ? (f - s) : -1;
  36. }
  37. struct microdvd_tag {
  38. char key;
  39. int persistent;
  40. uint32_t data1;
  41. uint32_t data2;
  42. char *data_string;
  43. int data_string_len;
  44. };
  45. #define MICRODVD_PERSISTENT_OFF 0
  46. #define MICRODVD_PERSISTENT_ON 1
  47. #define MICRODVD_PERSISTENT_OPENED 2
  48. // Color, Font, Size, cHarset, stYle, Position, cOordinate
  49. #define MICRODVD_TAGS "cfshyYpo"
  50. static void microdvd_set_tag(struct microdvd_tag *tags, struct microdvd_tag tag)
  51. {
  52. int tag_index = indexof(MICRODVD_TAGS, tag.key);
  53. if (tag_index < 0)
  54. return;
  55. memcpy(&tags[tag_index], &tag, sizeof(tag));
  56. }
  57. // italic, bold, underline, strike-through
  58. #define MICRODVD_STYLES "ibus"
  59. static char *microdvd_load_tags(struct microdvd_tag *tags, char *s)
  60. {
  61. while (*s == '{') {
  62. char *start = s;
  63. char tag_char = *(s + 1);
  64. struct microdvd_tag tag = {0};
  65. if (!tag_char || *(s + 2) != ':')
  66. break;
  67. s += 3;
  68. switch (tag_char) {
  69. /* Style */
  70. case 'Y':
  71. tag.persistent = MICRODVD_PERSISTENT_ON;
  72. case 'y':
  73. while (*s && *s != '}') {
  74. int style_index = indexof(MICRODVD_STYLES, *s);
  75. if (style_index >= 0)
  76. tag.data1 |= (1 << style_index);
  77. s++;
  78. }
  79. if (*s != '}')
  80. break;
  81. /* We must distinguish persistent and non-persistent styles
  82. * to handle this kind of style tags: {y:ib}{Y:us} */
  83. tag.key = tag_char;
  84. break;
  85. /* Color */
  86. case 'C':
  87. tag.persistent = MICRODVD_PERSISTENT_ON;
  88. case 'c':
  89. if (*s == '$')
  90. s++;
  91. tag.data1 = strtol(s, &s, 16) & 0x00ffffff;
  92. if (*s != '}')
  93. break;
  94. tag.key = 'c';
  95. break;
  96. /* Font name */
  97. case 'F':
  98. tag.persistent = MICRODVD_PERSISTENT_ON;
  99. case 'f': {
  100. int len = indexof(s, '}');
  101. if (len < 0)
  102. break;
  103. tag.data_string = s;
  104. tag.data_string_len = len;
  105. s += len;
  106. tag.key = 'f';
  107. break;
  108. }
  109. /* Font size */
  110. case 'S':
  111. tag.persistent = MICRODVD_PERSISTENT_ON;
  112. case 's':
  113. tag.data1 = strtol(s, &s, 10);
  114. if (*s != '}')
  115. break;
  116. tag.key = 's';
  117. break;
  118. /* Charset */
  119. case 'H': {
  120. //TODO: not yet handled, just parsed.
  121. int len = indexof(s, '}');
  122. if (len < 0)
  123. break;
  124. tag.data_string = s;
  125. tag.data_string_len = len;
  126. s += len;
  127. tag.key = 'h';
  128. break;
  129. }
  130. /* Position */
  131. case 'P':
  132. tag.persistent = MICRODVD_PERSISTENT_ON;
  133. tag.data1 = (*s++ == '1');
  134. if (*s != '}')
  135. break;
  136. tag.key = 'p';
  137. break;
  138. /* Coordinates */
  139. case 'o':
  140. tag.persistent = MICRODVD_PERSISTENT_ON;
  141. tag.data1 = strtol(s, &s, 10);
  142. if (*s != ',')
  143. break;
  144. s++;
  145. tag.data2 = strtol(s, &s, 10);
  146. if (*s != '}')
  147. break;
  148. tag.key = 'o';
  149. break;
  150. default: /* Unknown tag, we consider it's text */
  151. break;
  152. }
  153. if (tag.key == 0)
  154. return start;
  155. microdvd_set_tag(tags, tag);
  156. s++;
  157. }
  158. return s;
  159. }
  160. static void microdvd_open_tags(AVBPrint *new_line, struct microdvd_tag *tags)
  161. {
  162. int i, sidx;
  163. for (i = 0; i < sizeof(MICRODVD_TAGS) - 1; i++) {
  164. if (tags[i].persistent == MICRODVD_PERSISTENT_OPENED)
  165. continue;
  166. switch (tags[i].key) {
  167. case 'Y':
  168. case 'y':
  169. for (sidx = 0; sidx < sizeof(MICRODVD_STYLES) - 1; sidx++)
  170. if (tags[i].data1 & (1 << sidx))
  171. av_bprintf(new_line, "{\\%c1}", MICRODVD_STYLES[sidx]);
  172. break;
  173. case 'c':
  174. av_bprintf(new_line, "{\\c&H%06X&}", tags[i].data1);
  175. break;
  176. case 'f':
  177. av_bprintf(new_line, "{\\fn%.*s}",
  178. tags[i].data_string_len, tags[i].data_string);
  179. break;
  180. case 's':
  181. av_bprintf(new_line, "{\\fs%d}", tags[i].data1);
  182. break;
  183. case 'p':
  184. if (tags[i].data1 == 0)
  185. av_bprintf(new_line, "{\\an8}");
  186. break;
  187. case 'o':
  188. av_bprintf(new_line, "{\\pos(%d,%d)}",
  189. tags[i].data1, tags[i].data2);
  190. break;
  191. }
  192. if (tags[i].persistent == MICRODVD_PERSISTENT_ON)
  193. tags[i].persistent = MICRODVD_PERSISTENT_OPENED;
  194. }
  195. }
  196. static void microdvd_close_no_persistent_tags(AVBPrint *new_line,
  197. struct microdvd_tag *tags)
  198. {
  199. int i, sidx;
  200. for (i = sizeof(MICRODVD_TAGS) - 2; i >= 0; i--) {
  201. if (tags[i].persistent != MICRODVD_PERSISTENT_OFF)
  202. continue;
  203. switch (tags[i].key) {
  204. case 'y':
  205. for (sidx = sizeof(MICRODVD_STYLES) - 2; sidx >= 0; sidx--)
  206. if (tags[i].data1 & (1 << sidx))
  207. av_bprintf(new_line, "{\\%c0}", MICRODVD_STYLES[sidx]);
  208. break;
  209. case 'c':
  210. av_bprintf(new_line, "{\\c}");
  211. break;
  212. case 'f':
  213. av_bprintf(new_line, "{\\fn}");
  214. break;
  215. case 's':
  216. av_bprintf(new_line, "{\\fs}");
  217. break;
  218. }
  219. tags[i].key = 0;
  220. }
  221. }
  222. static int microdvd_decode_frame(AVCodecContext *avctx,
  223. void *data, int *got_sub_ptr, AVPacket *avpkt)
  224. {
  225. AVSubtitle *sub = data;
  226. AVBPrint new_line;
  227. char *decoded_sub;
  228. char *line = avpkt->data;
  229. char *end = avpkt->data + avpkt->size;
  230. struct microdvd_tag tags[sizeof(MICRODVD_TAGS) - 1] = {{0}};
  231. if (avpkt->size <= 0)
  232. return avpkt->size;
  233. av_bprint_init(&new_line, 0, 2048);
  234. // skip {frame_start}{frame_end}
  235. line = strchr(line, '}'); if (!line) goto end; line++;
  236. line = strchr(line, '}'); if (!line) goto end; line++;
  237. // subtitle content
  238. while (line < end && *line) {
  239. // parse MicroDVD tags, and open them in ASS
  240. line = microdvd_load_tags(tags, line);
  241. microdvd_open_tags(&new_line, tags);
  242. // simple copy until EOL or forced carriage return
  243. while (line < end && *line && *line != '|') {
  244. av_bprint_chars(&new_line, *line, 1);
  245. line++;
  246. }
  247. // line split
  248. if (line < end && *line == '|') {
  249. microdvd_close_no_persistent_tags(&new_line, tags);
  250. av_bprintf(&new_line, "\\N");
  251. line++;
  252. }
  253. }
  254. end:
  255. av_bprint_finalize(&new_line, &decoded_sub);
  256. if (*decoded_sub) {
  257. int64_t start = avpkt->pts;
  258. int64_t duration = avpkt->duration;
  259. int ts_start = av_rescale_q(start, avctx->time_base, (AVRational){1,100});
  260. int ts_duration = duration != -1 ?
  261. av_rescale_q(duration, avctx->time_base, (AVRational){1,100}) : -1;
  262. ff_ass_add_rect(sub, decoded_sub, ts_start, ts_duration, 0);
  263. }
  264. av_free(decoded_sub);
  265. *got_sub_ptr = sub->num_rects > 0;
  266. return avpkt->size;
  267. }
  268. static int microdvd_init(AVCodecContext *avctx)
  269. {
  270. int i, sidx;
  271. AVBPrint font_buf;
  272. int font_size = ASS_DEFAULT_FONT_SIZE;
  273. int color = ASS_DEFAULT_COLOR;
  274. int bold = ASS_DEFAULT_BOLD;
  275. int italic = ASS_DEFAULT_ITALIC;
  276. int underline = ASS_DEFAULT_UNDERLINE;
  277. int alignment = ASS_DEFAULT_ALIGNMENT;
  278. struct microdvd_tag tags[sizeof(MICRODVD_TAGS) - 1] = {{0}};
  279. av_bprint_init(&font_buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
  280. av_bprintf(&font_buf, "%s", ASS_DEFAULT_FONT);
  281. if (avctx->extradata) {
  282. microdvd_load_tags(tags, avctx->extradata);
  283. for (i = 0; i < sizeof(MICRODVD_TAGS) - 1; i++) {
  284. switch (av_tolower(tags[i].key)) {
  285. case 'y':
  286. for (sidx = 0; sidx < sizeof(MICRODVD_STYLES) - 1; sidx++) {
  287. if (tags[i].data1 & (1 << sidx)) {
  288. switch (MICRODVD_STYLES[sidx]) {
  289. case 'i': italic = 1; break;
  290. case 'b': bold = 1; break;
  291. case 'u': underline = 1; break;
  292. }
  293. }
  294. }
  295. break;
  296. case 'c': color = tags[i].data1; break;
  297. case 's': font_size = tags[i].data1; break;
  298. case 'p': alignment = 8; break;
  299. case 'f':
  300. av_bprint_clear(&font_buf);
  301. av_bprintf(&font_buf, "%.*s",
  302. tags[i].data_string_len, tags[i].data_string);
  303. break;
  304. }
  305. }
  306. }
  307. return ff_ass_subtitle_header(avctx, font_buf.str, font_size, color,
  308. ASS_DEFAULT_BACK_COLOR, bold, italic,
  309. underline, alignment);
  310. }
  311. AVCodec ff_microdvd_decoder = {
  312. .name = "microdvd",
  313. .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle"),
  314. .type = AVMEDIA_TYPE_SUBTITLE,
  315. .id = AV_CODEC_ID_MICRODVD,
  316. .init = microdvd_init,
  317. .decode = microdvd_decode_frame,
  318. };