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.

219 lines
5.9KB

  1. /*
  2. * SCC subtitle demuxer
  3. * Copyright (c) 2017 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "internal.h"
  23. #include "subtitles.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/bprint.h"
  26. #include "libavutil/intreadwrite.h"
  27. typedef struct SCCContext {
  28. FFDemuxSubtitlesQueue q;
  29. } SCCContext;
  30. static int scc_probe(const AVProbeData *p)
  31. {
  32. char buf[18];
  33. FFTextReader tr;
  34. ff_text_init_buf(&tr, p->buf, p->buf_size);
  35. while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
  36. ff_text_r8(&tr);
  37. ff_text_read(&tr, buf, sizeof(buf));
  38. if (!memcmp(buf, "Scenarist_SCC V1.0", 18))
  39. return AVPROBE_SCORE_MAX;
  40. return 0;
  41. }
  42. static int convert(uint8_t x)
  43. {
  44. if (x >= 'a')
  45. x -= 87;
  46. else if (x >= 'A')
  47. x -= 55;
  48. else
  49. x -= '0';
  50. return x;
  51. }
  52. static int scc_read_header(AVFormatContext *s)
  53. {
  54. SCCContext *scc = s->priv_data;
  55. AVStream *st = avformat_new_stream(s, NULL);
  56. char line[4096], line2[4096];
  57. int64_t current_pos, next_pos;
  58. int64_t ts_start, ts_end;
  59. int count = 0, ret = 0;
  60. ptrdiff_t len2, len;
  61. uint8_t out[4096];
  62. FFTextReader tr;
  63. ff_text_init_avio(s, &tr, s->pb);
  64. if (!st)
  65. return AVERROR(ENOMEM);
  66. avpriv_set_pts_info(st, 64, 1, 1000);
  67. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  68. st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
  69. while (!ff_text_eof(&tr)) {
  70. char *saveptr = NULL, *lline;
  71. int hh1, mm1, ss1, fs1, i;
  72. int hh2, mm2, ss2, fs2;
  73. AVPacket *sub;
  74. if (count == 0) {
  75. current_pos = ff_text_pos(&tr);
  76. while (!ff_text_eof(&tr)) {
  77. len = ff_subtitles_read_line(&tr, line, sizeof(line));
  78. if (len > 13)
  79. break;
  80. }
  81. }
  82. if (!strncmp(line, "Scenarist_SCC V1.0", 18))
  83. continue;
  84. if (av_sscanf(line, "%d:%d:%d%*[:;]%d", &hh1, &mm1, &ss1, &fs1) != 4)
  85. continue;
  86. ts_start = (hh1 * 3600LL + mm1 * 60LL + ss1) * 1000LL + fs1 * 33;
  87. next_pos = ff_text_pos(&tr);
  88. while (!ff_text_eof(&tr)) {
  89. len2 = ff_subtitles_read_line(&tr, line2, sizeof(line2));
  90. if (len2 > 13)
  91. break;
  92. }
  93. if (av_sscanf(line2, "%d:%d:%d%*[:;]%d", &hh2, &mm2, &ss2, &fs2) != 4)
  94. continue;
  95. ts_end = (hh2 * 3600LL + mm2 * 60LL + ss2) * 1000LL + fs2 * 33;
  96. count++;
  97. try_again:
  98. lline = (char *)&line;
  99. lline += 12;
  100. for (i = 0; i < 4095; i += 3) {
  101. char *ptr = av_strtok(lline, " ", &saveptr);
  102. char c1, c2, c3, c4;
  103. uint8_t o1, o2;
  104. if (!ptr)
  105. break;
  106. if (av_sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
  107. break;
  108. o1 = convert(c2) | (convert(c1) << 4);
  109. o2 = convert(c4) | (convert(c3) << 4);
  110. lline = NULL;
  111. if (i > 12 && o1 == 0x94 && o2 == 0x20 && saveptr &&
  112. (av_strncasecmp(saveptr, "942f", 4) || !av_strncasecmp(saveptr, "942c", 4))) {
  113. int64_t duration;
  114. out[i] = 0;
  115. duration = i * 11;
  116. sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
  117. if (!sub)
  118. goto fail;
  119. sub->pos = current_pos;
  120. current_pos += i;
  121. sub->pts = ts_start;
  122. sub->duration = duration;
  123. ts_start += duration;
  124. i = 0;
  125. }
  126. out[i+0] = 0xfc;
  127. out[i+1] = o1;
  128. out[i+2] = o2;
  129. }
  130. out[i] = 0;
  131. sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
  132. if (!sub)
  133. goto fail;
  134. sub->pos = current_pos;
  135. sub->pts = ts_start;
  136. sub->duration = ts_end - ts_start;
  137. memmove(line, line2, sizeof(line));
  138. current_pos = next_pos;
  139. line2[0] = 0;
  140. }
  141. if (line[0]) {
  142. ts_start = ts_end;
  143. ts_end += 1200;
  144. goto try_again;
  145. }
  146. ff_subtitles_queue_finalize(s, &scc->q);
  147. return ret;
  148. fail:
  149. ff_subtitles_queue_clean(&scc->q);
  150. return AVERROR(ENOMEM);
  151. }
  152. static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
  153. {
  154. SCCContext *scc = s->priv_data;
  155. return ff_subtitles_queue_read_packet(&scc->q, pkt);
  156. }
  157. static int scc_read_seek(AVFormatContext *s, int stream_index,
  158. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  159. {
  160. SCCContext *scc = s->priv_data;
  161. return ff_subtitles_queue_seek(&scc->q, s, stream_index,
  162. min_ts, ts, max_ts, flags);
  163. }
  164. static int scc_read_close(AVFormatContext *s)
  165. {
  166. SCCContext *scc = s->priv_data;
  167. ff_subtitles_queue_clean(&scc->q);
  168. return 0;
  169. }
  170. AVInputFormat ff_scc_demuxer = {
  171. .name = "scc",
  172. .long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
  173. .priv_data_size = sizeof(SCCContext),
  174. .read_probe = scc_probe,
  175. .read_header = scc_read_header,
  176. .read_packet = scc_read_packet,
  177. .read_seek2 = scc_read_seek,
  178. .read_close = scc_read_close,
  179. .extensions = "scc",
  180. };