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.

271 lines
7.5KB

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