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.0KB

  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[18];
  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. ff_text_read(&tr, buf, sizeof(buf));
  37. if (!memcmp(buf, "Scenarist_SCC V1.0", 18))
  38. return AVPROBE_SCORE_MAX;
  39. return 0;
  40. }
  41. static int convert(uint8_t x)
  42. {
  43. if (x >= 'a')
  44. x -= 87;
  45. else if (x >= 'A')
  46. x -= 55;
  47. else
  48. x -= '0';
  49. return x;
  50. }
  51. static int scc_read_header(AVFormatContext *s)
  52. {
  53. SCCContext *scc = s->priv_data;
  54. AVStream *st = avformat_new_stream(s, NULL);
  55. char line[4096], line2[4096];
  56. int count = 0, ret = 0;
  57. ptrdiff_t len2, len;
  58. uint8_t out[4096];
  59. FFTextReader tr;
  60. ff_text_init_avio(s, &tr, s->pb);
  61. if (!st)
  62. return AVERROR(ENOMEM);
  63. avpriv_set_pts_info(st, 64, 1, 1000);
  64. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  65. st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
  66. while (!ff_text_eof(&tr)) {
  67. const int64_t pos = ff_text_pos(&tr);
  68. char *saveptr = NULL, *lline;
  69. int hh1, mm1, ss1, fs1, i;
  70. int hh2, mm2, ss2, fs2;
  71. int64_t ts_start, ts_end;
  72. AVPacket *sub;
  73. if (count == 0) {
  74. while (!ff_text_eof(&tr)) {
  75. len = ff_subtitles_read_line(&tr, line, sizeof(line));
  76. if (len > 13)
  77. break;
  78. }
  79. }
  80. if (!strncmp(line, "Scenarist_SCC V1.0", 18))
  81. continue;
  82. if (sscanf(line, "%d:%d:%d%*[:;]%d", &hh1, &mm1, &ss1, &fs1) != 4)
  83. continue;
  84. ts_start = (hh1 * 3600LL + mm1 * 60LL + ss1) * 1000LL + fs1 * 33;
  85. while (!ff_text_eof(&tr)) {
  86. len2 = ff_subtitles_read_line(&tr, line2, sizeof(line2));
  87. if (len2 > 13)
  88. break;
  89. }
  90. if (sscanf(line2, "%d:%d:%d%*[:;]%d", &hh2, &mm2, &ss2, &fs2) != 4)
  91. continue;
  92. ts_end = (hh2 * 3600LL + mm2 * 60LL + ss2) * 1000LL + fs2 * 33;
  93. count++;
  94. lline = (char *)&line;
  95. lline += 12;
  96. for (i = 0; i < 4095; i += 3) {
  97. char *ptr = av_strtok(lline, " ", &saveptr);
  98. char c1, c2, c3, c4;
  99. if (!ptr)
  100. break;
  101. if (sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
  102. break;
  103. lline = NULL;
  104. out[i+0] = 0xfc;
  105. out[i+1] = convert(c2) | (convert(c1) << 4);
  106. out[i+2] = convert(c4) | (convert(c3) << 4);
  107. }
  108. out[i] = 0;
  109. sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
  110. if (!sub)
  111. goto fail;
  112. sub->pos = pos;
  113. sub->pts = ts_start;
  114. sub->duration = FFMAX(1200, ts_end - ts_start);
  115. memmove(line, line2, sizeof(line));
  116. FFSWAP(ptrdiff_t, len, len2);
  117. }
  118. ff_subtitles_queue_finalize(s, &scc->q);
  119. return ret;
  120. fail:
  121. ff_subtitles_queue_clean(&scc->q);
  122. return AVERROR(ENOMEM);
  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. };