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.

216 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 line2[4096], line[4096];
  57. int64_t pos, ts, next_ts = AV_NOPTS_VALUE;
  58. int ret = 0;
  59. ptrdiff_t len;
  60. uint8_t out[4096];
  61. FFTextReader tr;
  62. ff_text_init_avio(s, &tr, s->pb);
  63. if (!st)
  64. return AVERROR(ENOMEM);
  65. avpriv_set_pts_info(st, 64, 1, 1000);
  66. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  67. st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
  68. while (!ff_text_eof(&tr) || next_ts == AV_NOPTS_VALUE || line2[0]) {
  69. char *saveptr = NULL, *lline;
  70. int hh, mm, ss, fs, i;
  71. AVPacket *sub;
  72. if (next_ts == AV_NOPTS_VALUE) {
  73. while (!ff_text_eof(&tr)) {
  74. len = ff_subtitles_read_line(&tr, line, sizeof(line));
  75. if (len <= 13)
  76. continue;
  77. if (!strncmp(line, "Scenarist_SCC V1.0", 18))
  78. continue;
  79. if (av_sscanf(line, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
  80. break;
  81. }
  82. ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33;
  83. while (!ff_text_eof(&tr)) {
  84. len = ff_subtitles_read_line(&tr, line2, sizeof(line2));
  85. if (len <= 13)
  86. continue;
  87. if (av_sscanf(line2, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
  88. break;
  89. }
  90. } else {
  91. memmove(line, line2, sizeof(line));
  92. line2[0] = 0;
  93. while (!ff_text_eof(&tr)) {
  94. len = ff_subtitles_read_line(&tr, line2, sizeof(line2));
  95. if (len <= 13)
  96. continue;
  97. if (av_sscanf(line2, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
  98. break;
  99. }
  100. }
  101. next_ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33;
  102. pos = ff_text_pos(&tr);
  103. lline = (char *)&line;
  104. lline += 12;
  105. for (i = 0; i < 4095; i += 3) {
  106. char *ptr = av_strtok(lline, " ", &saveptr);
  107. char c1, c2, c3, c4;
  108. uint8_t o1, o2;
  109. if (!ptr)
  110. break;
  111. if (av_sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
  112. break;
  113. o1 = convert(c2) | (convert(c1) << 4);
  114. o2 = convert(c4) | (convert(c3) << 4);
  115. lline = NULL;
  116. if (i > 12 && o1 == 0x94 && o2 == 0x20 && saveptr &&
  117. (av_strncasecmp(saveptr, "942f", 4) && !av_strncasecmp(saveptr, "942c", 4))) {
  118. out[i] = 0;
  119. sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
  120. if (!sub)
  121. goto fail;
  122. sub->pos = pos;
  123. pos += i;
  124. sub->pts = ts;
  125. sub->duration = i * 11;
  126. ts += sub->duration;
  127. i = 0;
  128. }
  129. out[i+0] = 0xfc;
  130. out[i+1] = o1;
  131. out[i+2] = o2;
  132. }
  133. out[i] = 0;
  134. sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
  135. if (!sub)
  136. goto fail;
  137. sub->pos = pos;
  138. sub->pts = ts;
  139. sub->duration = next_ts - ts;
  140. ts = next_ts;
  141. }
  142. ff_subtitles_queue_finalize(s, &scc->q);
  143. return ret;
  144. fail:
  145. ff_subtitles_queue_clean(&scc->q);
  146. return AVERROR(ENOMEM);
  147. }
  148. static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
  149. {
  150. SCCContext *scc = s->priv_data;
  151. return ff_subtitles_queue_read_packet(&scc->q, pkt);
  152. }
  153. static int scc_read_seek(AVFormatContext *s, int stream_index,
  154. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  155. {
  156. SCCContext *scc = s->priv_data;
  157. return ff_subtitles_queue_seek(&scc->q, s, stream_index,
  158. min_ts, ts, max_ts, flags);
  159. }
  160. static int scc_read_close(AVFormatContext *s)
  161. {
  162. SCCContext *scc = s->priv_data;
  163. ff_subtitles_queue_clean(&scc->q);
  164. return 0;
  165. }
  166. AVInputFormat ff_scc_demuxer = {
  167. .name = "scc",
  168. .long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
  169. .priv_data_size = sizeof(SCCContext),
  170. .read_probe = scc_probe,
  171. .read_header = scc_read_header,
  172. .read_packet = scc_read_packet,
  173. .read_seek2 = scc_read_seek,
  174. .read_close = scc_read_close,
  175. .extensions = "scc",
  176. };