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.

305 lines
8.6KB

  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. * JACOsub subtitle demuxer
  23. * @see http://unicorn.us.com/jacosub/jscripts.html
  24. * @todo Support P[ALETTE] directive.
  25. */
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "libavcodec/jacosub.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/bprint.h"
  31. #include "libavutil/intreadwrite.h"
  32. typedef struct {
  33. char *line; ///< null-terminated heap allocated subtitle line
  34. int64_t pos; ///< offset position
  35. int start; ///< timestamp start
  36. int end; ///< timestamp end
  37. } SubEntry;
  38. typedef struct {
  39. int shift;
  40. unsigned timeres;
  41. SubEntry *subs; ///< subtitles list
  42. int nsub; ///< number of subtitles
  43. int sid; ///< current subtitle
  44. } JACOsubContext;
  45. static int timed_line(const char *ptr)
  46. {
  47. char c;
  48. return (sscanf(ptr, "%*u:%*u:%*u.%*u %*u:%*u:%*u.%*u %c", &c) == 1 ||
  49. sscanf(ptr, "@%*u @%*u %c", &c) == 1);
  50. }
  51. static int jacosub_probe(AVProbeData *p)
  52. {
  53. const char *ptr = p->buf;
  54. const char *ptr_end = p->buf + p->buf_size;
  55. if (AV_RB24(ptr) == 0xEFBBBF)
  56. ptr += 3; /* skip UTF-8 BOM */
  57. while (ptr < ptr_end) {
  58. if (timed_line(ptr))
  59. return AVPROBE_SCORE_MAX / 2;
  60. while (jss_whitespace(*ptr))
  61. ptr++;
  62. ptr += strcspn(ptr, "\n") + 1;
  63. }
  64. return 0;
  65. }
  66. static const char * const cmds[] = {
  67. "CLOCKPAUSE",
  68. "DIRECTIVE",
  69. "FONT",
  70. "HRES",
  71. "INCLUDE",
  72. "PALETTE",
  73. "QUANTIZE",
  74. "RAMP",
  75. "SHIFT",
  76. "TIMERES",
  77. };
  78. static int get_jss_cmd(char k)
  79. {
  80. int i;
  81. k = av_toupper(k);
  82. for (i = 0; i < FF_ARRAY_ELEMS(cmds); i++)
  83. if (k == cmds[i][0])
  84. return i;
  85. return -1;
  86. }
  87. static int jacosub_read_close(AVFormatContext *s)
  88. {
  89. int i;
  90. JACOsubContext *jacosub = s->priv_data;
  91. for (i = 0; i < jacosub->nsub; i++)
  92. av_freep(&jacosub->subs[i].line);
  93. jacosub->nsub = 0;
  94. av_freep(&jacosub->subs);
  95. return 0;
  96. }
  97. static const char *read_ts(JACOsubContext *jacosub, const char *buf,
  98. int *ts_start, int *ts_end)
  99. {
  100. int len;
  101. unsigned hs, ms, ss, fs; // hours, minutes, seconds, frame start
  102. unsigned he, me, se, fe; // hours, minutes, seconds, frame end
  103. /* timed format */
  104. if (sscanf(buf, "%u:%u:%u.%u %u:%u:%u.%u %n",
  105. &hs, &ms, &ss, &fs,
  106. &he, &me, &se, &fe, &len) == 8) {
  107. *ts_start = (hs*3600 + ms*60 + ss) * jacosub->timeres + fs;
  108. *ts_end = (he*3600 + me*60 + se) * jacosub->timeres + fe;
  109. goto shift_and_ret;
  110. }
  111. /* timestamps format */
  112. if (sscanf(buf, "@%u @%u %n", ts_start, ts_end, &len) == 2)
  113. goto shift_and_ret;
  114. return NULL;
  115. shift_and_ret:
  116. *ts_start = (*ts_start + jacosub->shift) * 100 / jacosub->timeres;
  117. *ts_end = (*ts_end + jacosub->shift) * 100 / jacosub->timeres;
  118. return buf + len;
  119. }
  120. static int get_shift(int timeres, const char *buf)
  121. {
  122. int sign = 1;
  123. int a = 0, b = 0, c = 0, d = 0;
  124. #define SSEP "%*1[.:]"
  125. int n = sscanf(buf, "%d"SSEP"%d"SSEP"%d"SSEP"%d", &a, &b, &c, &d);
  126. #undef SSEP
  127. if (*buf == '-' || a < 0) {
  128. sign = -1;
  129. a = FFABS(a);
  130. }
  131. switch (n) {
  132. case 4: return sign * ((a*3600 + b*60 + c) * timeres + d);
  133. case 3: return sign * (( a*60 + b) * timeres + c);
  134. case 2: return sign * (( a) * timeres + b);
  135. }
  136. return 0;
  137. }
  138. static int cmp_timed_sub(const void *a, const void *b)
  139. {
  140. return ((const SubEntry*)a)->start - ((const SubEntry*)b)->start;
  141. }
  142. static int jacosub_read_header(AVFormatContext *s)
  143. {
  144. AVBPrint header;
  145. AVIOContext *pb = s->pb;
  146. char line[JSS_MAX_LINESIZE];
  147. JACOsubContext *jacosub = s->priv_data;
  148. int shift_set = 0; // only the first shift matters
  149. int merge_line = 0;
  150. int i;
  151. AVStream *st = avformat_new_stream(s, NULL);
  152. if (!st)
  153. return AVERROR(ENOMEM);
  154. avpriv_set_pts_info(st, 64, 1, 100);
  155. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  156. st->codec->codec_id = CODEC_ID_JACOSUB;
  157. jacosub->timeres = 30;
  158. av_bprint_init(&header, 1024+FF_INPUT_BUFFER_PADDING_SIZE, 4096);
  159. while (!url_feof(pb)) {
  160. int cmd_len;
  161. const char *p = line;
  162. int64_t pos = avio_tell(pb);
  163. ff_get_line(pb, line, sizeof(line));
  164. p = jss_skip_whitespace(p);
  165. /* queue timed line */
  166. if (merge_line || timed_line(p)) {
  167. SubEntry *subs, *sub;
  168. const int len = strlen(line);
  169. if (merge_line) {
  170. char *tmp;
  171. const int old_len = strlen(sub->line);
  172. sub = &subs[jacosub->nsub];
  173. tmp = av_realloc(sub->line, old_len + len + 1);
  174. if (!tmp)
  175. return AVERROR(ENOMEM);
  176. sub->line = tmp;
  177. strcpy(sub->line + old_len, line);
  178. } else {
  179. subs = av_realloc(jacosub->subs,
  180. sizeof(*jacosub->subs) * (jacosub->nsub+1));
  181. if (!subs)
  182. return AVERROR(ENOMEM);
  183. jacosub->subs = subs;
  184. sub = &subs[jacosub->nsub];
  185. sub->pos = pos;
  186. sub->line = av_strdup(line);
  187. if (!sub->line)
  188. return AVERROR(ENOMEM);
  189. }
  190. merge_line = len > 1 && !strcmp(&line[len - 2], "\\\n");
  191. if (!merge_line)
  192. jacosub->nsub++;
  193. continue;
  194. }
  195. /* skip all non-compiler commands and focus on the command */
  196. if (*p != '#')
  197. continue;
  198. p++;
  199. i = get_jss_cmd(p[0]);
  200. if (i == -1)
  201. continue;
  202. /* trim command + spaces */
  203. cmd_len = strlen(cmds[i]);
  204. if (av_strncasecmp(p, cmds[i], cmd_len) == 0)
  205. p += cmd_len;
  206. else
  207. p++;
  208. p = jss_skip_whitespace(p);
  209. /* handle commands which affect the whole script */
  210. switch (cmds[i][0]) {
  211. case 'S': // SHIFT command affect the whole script...
  212. if (!shift_set) {
  213. jacosub->shift = get_shift(jacosub->timeres, p);
  214. shift_set = 1;
  215. }
  216. av_bprintf(&header, "#S %s", p);
  217. break;
  218. case 'T': // ...but must be placed after TIMERES
  219. jacosub->timeres = strtol(p, NULL, 10);
  220. av_bprintf(&header, "#T %s", p);
  221. break;
  222. }
  223. }
  224. /* general/essential directives in the extradata */
  225. av_bprint_finalize(&header, (char **)&st->codec->extradata);
  226. st->codec->extradata_size = header.len + 1;
  227. /* SHIFT and TIMERES affect the whole script so packet timing can only be
  228. * done in a second pass */
  229. for (i = 0; i < jacosub->nsub; i++) {
  230. SubEntry *sub = &jacosub->subs[i];
  231. read_ts(jacosub, sub->line, &sub->start, &sub->end);
  232. }
  233. qsort(jacosub->subs, jacosub->nsub, sizeof(*jacosub->subs), cmp_timed_sub);
  234. return 0;
  235. }
  236. static int jacosub_read_packet(AVFormatContext *s, AVPacket *pkt)
  237. {
  238. int res;
  239. JACOsubContext *jacosub = s->priv_data;
  240. const SubEntry *sub = &jacosub->subs[jacosub->sid++];
  241. if (jacosub->sid == jacosub->nsub)
  242. return AVERROR_EOF;
  243. res = av_new_packet(pkt, strlen(sub->line));
  244. if (res)
  245. return res;
  246. strcpy(pkt->data, sub->line);
  247. pkt->flags |= AV_PKT_FLAG_KEY;
  248. pkt->pos = sub->pos;
  249. pkt->pts = pkt->dts = sub->start;
  250. pkt->duration = sub->end - sub->start;
  251. return 0;
  252. }
  253. AVInputFormat ff_jacosub_demuxer = {
  254. .name = "jacosub",
  255. .long_name = NULL_IF_CONFIG_SMALL("JACOsub subtitle format"),
  256. .priv_data_size = sizeof(JACOsubContext),
  257. .read_probe = jacosub_probe,
  258. .read_header = jacosub_read_header,
  259. .read_packet = jacosub_read_packet,
  260. .read_close = jacosub_read_close,
  261. .flags = AVFMT_GENERIC_INDEX,
  262. };