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.

380 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. if (!*s)
  133. break;
  134. tag.persistent = MICRODVD_PERSISTENT_ON;
  135. tag.data1 = (*s++ == '1');
  136. if (*s != '}')
  137. break;
  138. tag.key = 'p';
  139. break;
  140. /* Coordinates */
  141. case 'o':
  142. tag.persistent = MICRODVD_PERSISTENT_ON;
  143. tag.data1 = strtol(s, &s, 10);
  144. if (*s != ',')
  145. break;
  146. s++;
  147. tag.data2 = strtol(s, &s, 10);
  148. if (*s != '}')
  149. break;
  150. tag.key = 'o';
  151. break;
  152. default: /* Unknown tag, we consider it's text */
  153. break;
  154. }
  155. if (tag.key == 0)
  156. return start;
  157. microdvd_set_tag(tags, tag);
  158. s++;
  159. }
  160. return s;
  161. }
  162. static void microdvd_open_tags(AVBPrint *new_line, struct microdvd_tag *tags)
  163. {
  164. int i, sidx;
  165. for (i = 0; i < sizeof(MICRODVD_TAGS) - 1; i++) {
  166. if (tags[i].persistent == MICRODVD_PERSISTENT_OPENED)
  167. continue;
  168. switch (tags[i].key) {
  169. case 'Y':
  170. case 'y':
  171. for (sidx = 0; sidx < sizeof(MICRODVD_STYLES) - 1; sidx++)
  172. if (tags[i].data1 & (1 << sidx))
  173. av_bprintf(new_line, "{\\%c1}", MICRODVD_STYLES[sidx]);
  174. break;
  175. case 'c':
  176. av_bprintf(new_line, "{\\c&H%06X&}", tags[i].data1);
  177. break;
  178. case 'f':
  179. av_bprintf(new_line, "{\\fn%.*s}",
  180. tags[i].data_string_len, tags[i].data_string);
  181. break;
  182. case 's':
  183. av_bprintf(new_line, "{\\fs%d}", tags[i].data1);
  184. break;
  185. case 'p':
  186. if (tags[i].data1 == 0)
  187. av_bprintf(new_line, "{\\an8}");
  188. break;
  189. case 'o':
  190. av_bprintf(new_line, "{\\pos(%d,%d)}",
  191. tags[i].data1, tags[i].data2);
  192. break;
  193. }
  194. if (tags[i].persistent == MICRODVD_PERSISTENT_ON)
  195. tags[i].persistent = MICRODVD_PERSISTENT_OPENED;
  196. }
  197. }
  198. static void microdvd_close_no_persistent_tags(AVBPrint *new_line,
  199. struct microdvd_tag *tags)
  200. {
  201. int i, sidx;
  202. for (i = sizeof(MICRODVD_TAGS) - 2; i >= 0; i--) {
  203. if (tags[i].persistent != MICRODVD_PERSISTENT_OFF)
  204. continue;
  205. switch (tags[i].key) {
  206. case 'y':
  207. for (sidx = sizeof(MICRODVD_STYLES) - 2; sidx >= 0; sidx--)
  208. if (tags[i].data1 & (1 << sidx))
  209. av_bprintf(new_line, "{\\%c0}", MICRODVD_STYLES[sidx]);
  210. break;
  211. case 'c':
  212. av_bprintf(new_line, "{\\c}");
  213. break;
  214. case 'f':
  215. av_bprintf(new_line, "{\\fn}");
  216. break;
  217. case 's':
  218. av_bprintf(new_line, "{\\fs}");
  219. break;
  220. }
  221. tags[i].key = 0;
  222. }
  223. }
  224. static int microdvd_decode_frame(AVCodecContext *avctx,
  225. void *data, int *got_sub_ptr, AVPacket *avpkt)
  226. {
  227. AVSubtitle *sub = data;
  228. AVBPrint new_line;
  229. char c;
  230. char *decoded_sub;
  231. char *line = avpkt->data;
  232. char *end = avpkt->data + avpkt->size;
  233. struct microdvd_tag tags[sizeof(MICRODVD_TAGS) - 1] = {{0}};
  234. if (avpkt->size <= 0)
  235. return avpkt->size;
  236. /* To be removed later */
  237. if (sscanf(line, "{%*d}{%*[0123456789]}%c", &c) == 1 &&
  238. line[avpkt->size - 1] == '\n') {
  239. av_log(avctx, AV_LOG_ERROR, "AVPacket is not clean (contains timing "
  240. "information and a trailing line break). You need to upgrade "
  241. "your libavformat or sanitize your packet.\n");
  242. return AVERROR_INVALIDDATA;
  243. }
  244. av_bprint_init(&new_line, 0, 2048);
  245. // subtitle content
  246. while (line < end && *line) {
  247. // parse MicroDVD tags, and open them in ASS
  248. line = microdvd_load_tags(tags, line);
  249. microdvd_open_tags(&new_line, tags);
  250. // simple copy until EOL or forced carriage return
  251. while (line < end && *line && *line != '|') {
  252. av_bprint_chars(&new_line, *line, 1);
  253. line++;
  254. }
  255. // line split
  256. if (line < end && *line == '|') {
  257. microdvd_close_no_persistent_tags(&new_line, tags);
  258. av_bprintf(&new_line, "\\N");
  259. line++;
  260. }
  261. }
  262. if (new_line.len) {
  263. av_bprintf(&new_line, "\r\n");
  264. av_bprint_finalize(&new_line, &decoded_sub);
  265. if (*decoded_sub) {
  266. int64_t start = avpkt->pts;
  267. int64_t duration = avpkt->duration;
  268. int ts_start = av_rescale_q(start, avctx->time_base, (AVRational){1,100});
  269. int ts_duration = duration != -1 ?
  270. av_rescale_q(duration, avctx->time_base, (AVRational){1,100}) : -1;
  271. ff_ass_add_rect(sub, decoded_sub, ts_start, ts_duration, 0);
  272. }
  273. av_free(decoded_sub);
  274. }
  275. *got_sub_ptr = sub->num_rects > 0;
  276. return avpkt->size;
  277. }
  278. static int microdvd_init(AVCodecContext *avctx)
  279. {
  280. int i, sidx;
  281. AVBPrint font_buf;
  282. int font_size = ASS_DEFAULT_FONT_SIZE;
  283. int color = ASS_DEFAULT_COLOR;
  284. int bold = ASS_DEFAULT_BOLD;
  285. int italic = ASS_DEFAULT_ITALIC;
  286. int underline = ASS_DEFAULT_UNDERLINE;
  287. int alignment = ASS_DEFAULT_ALIGNMENT;
  288. struct microdvd_tag tags[sizeof(MICRODVD_TAGS) - 1] = {{0}};
  289. av_bprint_init(&font_buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
  290. av_bprintf(&font_buf, "%s", ASS_DEFAULT_FONT);
  291. if (avctx->extradata) {
  292. microdvd_load_tags(tags, avctx->extradata);
  293. for (i = 0; i < sizeof(MICRODVD_TAGS) - 1; i++) {
  294. switch (av_tolower(tags[i].key)) {
  295. case 'y':
  296. for (sidx = 0; sidx < sizeof(MICRODVD_STYLES) - 1; sidx++) {
  297. if (tags[i].data1 & (1 << sidx)) {
  298. switch (MICRODVD_STYLES[sidx]) {
  299. case 'i': italic = 1; break;
  300. case 'b': bold = 1; break;
  301. case 'u': underline = 1; break;
  302. }
  303. }
  304. }
  305. break;
  306. case 'c': color = tags[i].data1; break;
  307. case 's': font_size = tags[i].data1; break;
  308. case 'p': alignment = 8; break;
  309. case 'f':
  310. av_bprint_clear(&font_buf);
  311. av_bprintf(&font_buf, "%.*s",
  312. tags[i].data_string_len, tags[i].data_string);
  313. break;
  314. }
  315. }
  316. }
  317. return ff_ass_subtitle_header(avctx, font_buf.str, font_size, color,
  318. ASS_DEFAULT_BACK_COLOR, bold, italic,
  319. underline, alignment);
  320. }
  321. AVCodec ff_microdvd_decoder = {
  322. .name = "microdvd",
  323. .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle"),
  324. .type = AVMEDIA_TYPE_SUBTITLE,
  325. .id = AV_CODEC_ID_MICRODVD,
  326. .init = microdvd_init,
  327. .decode = microdvd_decode_frame,
  328. };