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.

184 lines
5.1KB

  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/bprint.h"
  25. #include "libavutil/intreadwrite.h"
  26. typedef struct SCCContext {
  27. FFDemuxSubtitlesQueue q;
  28. } SCCContext;
  29. static int scc_probe(AVProbeData *p)
  30. {
  31. char buf[128];
  32. FFTextReader tr;
  33. ff_text_init_buf(&tr, p->buf, p->buf_size);
  34. while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
  35. ff_text_r8(&tr);
  36. if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0)
  37. return 0;
  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. int count = 0, ret = 0;
  58. ptrdiff_t len2, len;
  59. uint8_t out[4096];
  60. FFTextReader tr;
  61. ff_text_init_avio(s, &tr, s->pb);
  62. if (!st)
  63. return AVERROR(ENOMEM);
  64. avpriv_set_pts_info(st, 64, 1, 1000);
  65. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  66. st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
  67. while (!ff_text_eof(&tr)) {
  68. const int64_t pos = ff_text_pos(&tr);
  69. char *saveptr = NULL, *lline;
  70. int hh1, mm1, ss1, fs1, i;
  71. int hh2, mm2, ss2, fs2;
  72. int64_t ts_start, ts_end;
  73. AVPacket *sub;
  74. if (count == 0) {
  75. while (!ff_text_eof(&tr)) {
  76. len = ff_subtitles_read_line(&tr, line, sizeof(line));
  77. if (len > 13)
  78. break;
  79. }
  80. }
  81. if (!strncmp(line, "Scenarist_SCC V1.0", 18))
  82. continue;
  83. if (sscanf(line, "%d:%d:%d:%d", &hh1, &mm1, &ss1, &fs1) != 4 &&
  84. 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. while (!ff_text_eof(&tr)) {
  88. len2 = ff_subtitles_read_line(&tr, line2, sizeof(line2));
  89. if (len2 > 13)
  90. break;
  91. }
  92. if (sscanf(line2, "%d:%d:%d:%d", &hh2, &mm2, &ss2, &fs2) != 4 &&
  93. 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. lline = (char *)&line;
  98. lline += 12;
  99. for (i = 0; i < 4095; i += 3) {
  100. char *ptr = av_strtok(lline, " ", &saveptr);
  101. char c1, c2, c3, c4;
  102. if (!ptr)
  103. break;
  104. if (sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
  105. break;
  106. lline = NULL;
  107. out[i+0] = 0xfc;
  108. out[i+1] = convert(c2) | (convert(c1) << 4);
  109. out[i+2] = convert(c4) | (convert(c3) << 4);
  110. }
  111. out[i] = 0;
  112. sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
  113. if (!sub)
  114. return AVERROR(ENOMEM);
  115. sub->pos = pos;
  116. sub->pts = ts_start;
  117. sub->duration = FFMAX(1200, ts_end - ts_start);
  118. memmove(line, line2, sizeof(line));
  119. FFSWAP(ptrdiff_t, len, len2);
  120. }
  121. ff_subtitles_queue_finalize(s, &scc->q);
  122. return ret;
  123. }
  124. static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
  125. {
  126. SCCContext *scc = s->priv_data;
  127. return ff_subtitles_queue_read_packet(&scc->q, pkt);
  128. }
  129. static int scc_read_seek(AVFormatContext *s, int stream_index,
  130. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  131. {
  132. SCCContext *scc = s->priv_data;
  133. return ff_subtitles_queue_seek(&scc->q, s, stream_index,
  134. min_ts, ts, max_ts, flags);
  135. }
  136. static int scc_read_close(AVFormatContext *s)
  137. {
  138. SCCContext *scc = s->priv_data;
  139. ff_subtitles_queue_clean(&scc->q);
  140. return 0;
  141. }
  142. AVInputFormat ff_scc_demuxer = {
  143. .name = "scc",
  144. .long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
  145. .priv_data_size = sizeof(SCCContext),
  146. .read_probe = scc_probe,
  147. .read_header = scc_read_header,
  148. .read_packet = scc_read_packet,
  149. .read_seek2 = scc_read_seek,
  150. .read_close = scc_read_close,
  151. .extensions = "scc",
  152. };