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.

239 lines
7.1KB

  1. /*
  2. * MCC subtitle demuxer
  3. * Copyright (c) 2020 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 MCCContext {
  28. FFDemuxSubtitlesQueue q;
  29. } MCCContext;
  30. static int mcc_probe(const AVProbeData *p)
  31. {
  32. char buf[28];
  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, "File Format=MacCaption_MCC V", 28))
  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. typedef struct alias {
  53. uint8_t key;
  54. int len;
  55. const char *value;
  56. } alias;
  57. static const alias aliases[20] = {
  58. { .key = 16, .len = 3, .value = "\xFA\x0\x0", },
  59. { .key = 17, .len = 6, .value = "\xFA\x0\x0\xFA\x0\x0", },
  60. { .key = 18, .len = 9, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
  61. { .key = 19, .len = 12, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
  62. { .key = 20, .len = 15, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
  63. { .key = 21, .len = 18, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
  64. { .key = 22, .len = 21, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
  65. { .key = 23, .len = 24, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
  66. { .key = 24, .len = 27, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
  67. { .key = 25, .len = 3, .value = "\xFB\x80\x80", },
  68. { .key = 26, .len = 3, .value = "\xFC\x80\x80", },
  69. { .key = 27, .len = 3, .value = "\xFD\x80\x80", },
  70. { .key = 28, .len = 2, .value = "\x96\x69", },
  71. { .key = 29, .len = 2, .value = "\x61\x01", },
  72. { .key = 30, .len = 3, .value = "\xFC\x80\x80", },
  73. { .key = 31, .len = 3, .value = "\xFC\x80\x80", },
  74. { .key = 32, .len = 4, .value = "\xE1\x00\x00\x00", },
  75. { .key = 33, .len = 0, .value = NULL, },
  76. { .key = 34, .len = 0, .value = NULL, },
  77. { .key = 35, .len = 1, .value = "\x0", },
  78. };
  79. static int mcc_read_header(AVFormatContext *s)
  80. {
  81. MCCContext *mcc = s->priv_data;
  82. AVStream *st = avformat_new_stream(s, NULL);
  83. AVRational rate;
  84. int64_t ts, pos;
  85. uint8_t out[4096];
  86. char line[4096];
  87. FFTextReader tr;
  88. int ret = 0;
  89. ff_text_init_avio(s, &tr, s->pb);
  90. if (!st)
  91. return AVERROR(ENOMEM);
  92. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  93. st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
  94. avpriv_set_pts_info(st, 64, 1, 30);
  95. while (!ff_text_eof(&tr)) {
  96. int hh, mm, ss, fs, i = 0, j = 0;
  97. int start = 12, count = 0;
  98. AVPacket *sub;
  99. char *lline;
  100. ff_subtitles_read_line(&tr, line, sizeof(line));
  101. if (!strncmp(line, "File Format=MacCaption_MCC V", 28))
  102. continue;
  103. if (!strncmp(line, "//", 2))
  104. continue;
  105. if (!strncmp(line, "Time Code Rate=", 15)) {
  106. char *rate_str = line + 15;
  107. char *df = NULL;
  108. int num = -1, den = -1;
  109. if (rate_str[0]) {
  110. num = strtol(rate_str, &df, 10);
  111. den = 1;
  112. if (df && !av_strncasecmp(df, "DF", 2)) {
  113. num *= 1000;
  114. den = 1001;
  115. }
  116. }
  117. if (num > 0 && den > 0) {
  118. rate = av_make_q(num, den);
  119. avpriv_set_pts_info(st, 64, rate.den, rate.num);
  120. }
  121. continue;
  122. }
  123. if (av_sscanf(line, "%d:%d:%d:%d", &hh, &mm, &ss, &fs) != 4)
  124. continue;
  125. ts = av_rescale(hh * 3600LL + mm * 60LL + ss, rate.num, rate.den) + fs;
  126. lline = (char *)&line;
  127. lline += 12;
  128. pos = ff_text_pos(&tr);
  129. while (lline[i]) {
  130. uint8_t v = convert(lline[i]);
  131. if (v >= 16 && v <= 35) {
  132. int idx = v - 16;
  133. if (aliases[idx].len) {
  134. if (j >= sizeof(out) - 1 - aliases[idx].len) {
  135. j = 0;
  136. break;
  137. }
  138. memcpy(out + j, aliases[idx].value, aliases[idx].len);
  139. j += aliases[idx].len;
  140. }
  141. } else {
  142. uint8_t vv;
  143. if (i + 13 >= sizeof(line) - 1)
  144. break;
  145. vv = convert(lline[i + 1]);
  146. if (j >= sizeof(out) - 1) {
  147. j = 0;
  148. break;
  149. }
  150. out[j++] = vv | (v << 4);
  151. i++;
  152. }
  153. i++;
  154. }
  155. out[j] = 0;
  156. if (out[7] & 0x80)
  157. start += 4;
  158. count = (out[11] & 0x1f) * 3;
  159. if (j < start + count + 1)
  160. continue;
  161. if (!count)
  162. continue;
  163. sub = ff_subtitles_queue_insert(&mcc->q, out + start, count, 0);
  164. if (!sub)
  165. goto fail;
  166. sub->pos = pos;
  167. sub->pts = ts;
  168. sub->duration = 1;
  169. }
  170. ff_subtitles_queue_finalize(s, &mcc->q);
  171. return ret;
  172. fail:
  173. ff_subtitles_queue_clean(&mcc->q);
  174. return AVERROR(ENOMEM);
  175. }
  176. static int mcc_read_packet(AVFormatContext *s, AVPacket *pkt)
  177. {
  178. MCCContext *mcc = s->priv_data;
  179. return ff_subtitles_queue_read_packet(&mcc->q, pkt);
  180. }
  181. static int mcc_read_seek(AVFormatContext *s, int stream_index,
  182. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  183. {
  184. MCCContext *mcc = s->priv_data;
  185. return ff_subtitles_queue_seek(&mcc->q, s, stream_index,
  186. min_ts, ts, max_ts, flags);
  187. }
  188. static int mcc_read_close(AVFormatContext *s)
  189. {
  190. MCCContext *mcc = s->priv_data;
  191. ff_subtitles_queue_clean(&mcc->q);
  192. return 0;
  193. }
  194. AVInputFormat ff_mcc_demuxer = {
  195. .name = "mcc",
  196. .long_name = NULL_IF_CONFIG_SMALL("MacCaption"),
  197. .priv_data_size = sizeof(MCCContext),
  198. .read_probe = mcc_probe,
  199. .read_header = mcc_read_header,
  200. .read_packet = mcc_read_packet,
  201. .read_seek2 = mcc_read_seek,
  202. .read_close = mcc_read_close,
  203. .extensions = "mcc",
  204. };