subviewerdec.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2012 Clément Bœsch
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * SubViewer subtitle demuxer
  23. * @see https://en.wikipedia.org/wiki/SubViewer
  24. */
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "subtitles.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/bprint.h"
  30. #include "libavutil/intreadwrite.h"
  31. typedef struct {
  32. FFDemuxSubtitlesQueue q;
  33. } SubViewerContext;
  34. static int subviewer_probe(AVProbeData *p)
  35. {
  36. char c;
  37. const unsigned char *ptr = p->buf;
  38. if (AV_RB24(ptr) == 0xEFBBBF)
  39. ptr += 3; /* skip UTF-8 BOM */
  40. if (sscanf(ptr, "%*u:%*u:%*u.%*u,%*u:%*u:%*u.%*u%c", &c) == 1)
  41. return AVPROBE_SCORE_MAX/2;
  42. if (!strncmp(ptr, "[INFORMATION]", 13))
  43. return AVPROBE_SCORE_MAX/3;
  44. return 0;
  45. }
  46. static int read_ts(const char *s, int64_t *start, int *duration)
  47. {
  48. int64_t end;
  49. int hh1, mm1, ss1, ms1;
  50. int hh2, mm2, ss2, ms2;
  51. if (sscanf(s, "%u:%u:%u.%u,%u:%u:%u.%u",
  52. &hh1, &mm1, &ss1, &ms1, &hh2, &mm2, &ss2, &ms2) == 8) {
  53. end = (hh2*3600 + mm2*60 + ss2) * 100 + ms2;
  54. *start = (hh1*3600 + mm1*60 + ss1) * 100 + ms1;
  55. *duration = end - *start;
  56. return 0;
  57. }
  58. return -1;
  59. }
  60. static int subviewer_read_header(AVFormatContext *s)
  61. {
  62. SubViewerContext *subviewer = s->priv_data;
  63. AVStream *st = avformat_new_stream(s, NULL);
  64. AVBPrint header;
  65. int res = 0;
  66. if (!st)
  67. return AVERROR(ENOMEM);
  68. avpriv_set_pts_info(st, 64, 1, 100);
  69. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  70. st->codec->codec_id = AV_CODEC_ID_SUBVIEWER;
  71. av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
  72. while (!url_feof(s->pb)) {
  73. char line[2048];
  74. const int64_t pos = avio_tell(s->pb);
  75. int len = ff_get_line(s->pb, line, sizeof(line));
  76. if (!len)
  77. break;
  78. if (line[0] == '[' && strncmp(line, "[br]", 4)) {
  79. /* ignore event style, XXX: add to side_data? */
  80. if (strstr(line, "[COLF]") || strstr(line, "[SIZE]") ||
  81. strstr(line, "[FONT]") || strstr(line, "[STYLE]"))
  82. continue;
  83. if (!st->codec->extradata) { // header not finalized yet
  84. av_bprintf(&header, "%s", line);
  85. if (!strncmp(line, "[END INFORMATION]", 17) || !strncmp(line, "[SUBTITLE]", 10)) {
  86. /* end of header */
  87. av_bprint_finalize(&header, (char **)&st->codec->extradata);
  88. if (!st->codec->extradata) {
  89. res = AVERROR(ENOMEM);
  90. goto end;
  91. }
  92. st->codec->extradata_size = header.len + 1;
  93. } else if (strncmp(line, "[INFORMATION]", 13)) {
  94. /* assume file metadata at this point */
  95. int i, j = 0;
  96. char key[32], value[128];
  97. for (i = 1; i < sizeof(key) - 1 && line[i] && line[i] != ']'; i++)
  98. key[i - 1] = av_tolower(line[i]);
  99. key[i - 1] = 0;
  100. if (line[i] == ']')
  101. i++;
  102. while (line[i] == ' ')
  103. i++;
  104. while (j < sizeof(value) - 1 && line[i] && !strchr("]\r\n", line[i]))
  105. value[j++] = line[i++];
  106. value[j] = 0;
  107. av_dict_set(&s->metadata, key, value, 0);
  108. }
  109. }
  110. } else {
  111. int64_t pts_start = AV_NOPTS_VALUE;
  112. int duration = -1;
  113. int timed_line = !read_ts(line, &pts_start, &duration);
  114. AVPacket *sub;
  115. sub = ff_subtitles_queue_insert(&subviewer->q, line, len, !timed_line);
  116. if (!sub) {
  117. res = AVERROR(ENOMEM);
  118. goto end;
  119. }
  120. if (timed_line) {
  121. sub->pos = pos;
  122. sub->pts = pts_start;
  123. sub->duration = duration;
  124. }
  125. }
  126. }
  127. ff_subtitles_queue_finalize(&subviewer->q);
  128. end:
  129. av_bprint_finalize(&header, NULL);
  130. return res;
  131. }
  132. static int subviewer_read_packet(AVFormatContext *s, AVPacket *pkt)
  133. {
  134. SubViewerContext *subviewer = s->priv_data;
  135. return ff_subtitles_queue_read_packet(&subviewer->q, pkt);
  136. }
  137. static int subviewer_read_close(AVFormatContext *s)
  138. {
  139. SubViewerContext *subviewer = s->priv_data;
  140. ff_subtitles_queue_clean(&subviewer->q);
  141. return 0;
  142. }
  143. AVInputFormat ff_subviewer_demuxer = {
  144. .name = "subviewer",
  145. .long_name = NULL_IF_CONFIG_SMALL("SubViewer subtitle format"),
  146. .priv_data_size = sizeof(SubViewerContext),
  147. .read_probe = subviewer_probe,
  148. .read_header = subviewer_read_header,
  149. .read_packet = subviewer_read_packet,
  150. .read_close = subviewer_read_close,
  151. .flags = AVFMT_GENERIC_INDEX,
  152. .extensions = "sub",
  153. };