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.

308 lines
8.7KB

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