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.

215 lines
5.8KB

  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 ts_start, ts_end;
  58. int count = 0, ret = 0;
  59. ptrdiff_t len2, 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)) {
  69. int64_t current_pos, next_pos;
  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. out[i+0] = 0xfc;
  112. out[i+1] = o1;
  113. out[i+2] = o2;
  114. if (o1 == 0x94 && o2 == 0x2f && saveptr && av_strcasecmp(saveptr, "942f")) {
  115. int64_t duration;
  116. i += 3;
  117. out[i] = 0;
  118. duration = i * 11;
  119. sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
  120. if (!sub)
  121. return AVERROR(ENOMEM);
  122. current_pos += i;
  123. sub->pos = current_pos;
  124. sub->pts = ts_start;
  125. sub->duration = duration;
  126. ts_start += duration;
  127. i = 0;
  128. }
  129. }
  130. out[i] = 0;
  131. sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
  132. if (!sub)
  133. return AVERROR(ENOMEM);
  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. }
  149. static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
  150. {
  151. SCCContext *scc = s->priv_data;
  152. return ff_subtitles_queue_read_packet(&scc->q, pkt);
  153. }
  154. static int scc_read_seek(AVFormatContext *s, int stream_index,
  155. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  156. {
  157. SCCContext *scc = s->priv_data;
  158. return ff_subtitles_queue_seek(&scc->q, s, stream_index,
  159. min_ts, ts, max_ts, flags);
  160. }
  161. static int scc_read_close(AVFormatContext *s)
  162. {
  163. SCCContext *scc = s->priv_data;
  164. ff_subtitles_queue_clean(&scc->q);
  165. return 0;
  166. }
  167. AVInputFormat ff_scc_demuxer = {
  168. .name = "scc",
  169. .long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
  170. .priv_data_size = sizeof(SCCContext),
  171. .read_probe = scc_probe,
  172. .read_header = scc_read_header,
  173. .read_packet = scc_read_packet,
  174. .read_seek2 = scc_read_seek,
  175. .read_close = scc_read_close,
  176. .extensions = "scc",
  177. };