jacosubdec.c 7.2 KB

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