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.

154 lines
4.4KB

  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. * SAMI subtitle demuxer
  23. * @see http://msdn.microsoft.com/en-us/library/ms971327.aspx
  24. */
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "subtitles.h"
  28. #include "libavcodec/internal.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/bprint.h"
  31. #include "libavutil/intreadwrite.h"
  32. typedef struct {
  33. FFDemuxSubtitlesQueue q;
  34. } SAMIContext;
  35. static int sami_probe(const AVProbeData *p)
  36. {
  37. char buf[6];
  38. FFTextReader tr;
  39. ff_text_init_buf(&tr, p->buf, p->buf_size);
  40. ff_text_read(&tr, buf, sizeof(buf));
  41. return !strncmp(buf, "<SAMI>", 6) ? AVPROBE_SCORE_MAX : 0;
  42. }
  43. static int sami_read_header(AVFormatContext *s)
  44. {
  45. SAMIContext *sami = s->priv_data;
  46. AVStream *st = avformat_new_stream(s, NULL);
  47. AVBPrint buf, hdr_buf;
  48. char c = 0;
  49. int res = 0, got_first_sync_point = 0;
  50. FFTextReader tr;
  51. ff_text_init_avio(s, &tr, s->pb);
  52. if (!st)
  53. return AVERROR(ENOMEM);
  54. avpriv_set_pts_info(st, 64, 1, 1000);
  55. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  56. st->codecpar->codec_id = AV_CODEC_ID_SAMI;
  57. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  58. av_bprint_init(&hdr_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  59. while (!ff_text_eof(&tr)) {
  60. AVPacket *sub;
  61. const int64_t pos = ff_text_pos(&tr) - (c != 0);
  62. int is_sync, is_body, n = ff_smil_extract_next_text_chunk(&tr, &buf, &c);
  63. if (n == 0)
  64. break;
  65. is_body = !av_strncasecmp(buf.str, "</BODY", 6);
  66. if (is_body) {
  67. av_bprint_clear(&buf);
  68. break;
  69. }
  70. is_sync = !av_strncasecmp(buf.str, "<SYNC", 5);
  71. if (is_sync)
  72. got_first_sync_point = 1;
  73. if (!got_first_sync_point) {
  74. av_bprintf(&hdr_buf, "%s", buf.str);
  75. } else {
  76. sub = ff_subtitles_queue_insert(&sami->q, buf.str, buf.len, !is_sync);
  77. if (!sub) {
  78. res = AVERROR(ENOMEM);
  79. goto end;
  80. }
  81. if (is_sync) {
  82. const char *p = ff_smil_get_attr_ptr(buf.str, "Start");
  83. sub->pos = pos;
  84. sub->pts = p ? strtol(p, NULL, 10) : 0;
  85. if (sub->pts <= INT64_MIN/2 || sub->pts >= INT64_MAX/2) {
  86. res = AVERROR_PATCHWELCOME;
  87. goto end;
  88. }
  89. sub->duration = -1;
  90. }
  91. }
  92. av_bprint_clear(&buf);
  93. }
  94. res = ff_bprint_to_codecpar_extradata(st->codecpar, &hdr_buf);
  95. if (res < 0)
  96. goto end;
  97. ff_subtitles_queue_finalize(s, &sami->q);
  98. end:
  99. if (res < 0)
  100. ff_subtitles_queue_clean(&sami->q);
  101. av_bprint_finalize(&buf, NULL);
  102. return res;
  103. }
  104. static int sami_read_packet(AVFormatContext *s, AVPacket *pkt)
  105. {
  106. SAMIContext *sami = s->priv_data;
  107. return ff_subtitles_queue_read_packet(&sami->q, pkt);
  108. }
  109. static int sami_read_seek(AVFormatContext *s, int stream_index,
  110. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  111. {
  112. SAMIContext *sami = s->priv_data;
  113. return ff_subtitles_queue_seek(&sami->q, s, stream_index,
  114. min_ts, ts, max_ts, flags);
  115. }
  116. static int sami_read_close(AVFormatContext *s)
  117. {
  118. SAMIContext *sami = s->priv_data;
  119. ff_subtitles_queue_clean(&sami->q);
  120. return 0;
  121. }
  122. AVInputFormat ff_sami_demuxer = {
  123. .name = "sami",
  124. .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle format"),
  125. .priv_data_size = sizeof(SAMIContext),
  126. .read_probe = sami_probe,
  127. .read_header = sami_read_header,
  128. .read_packet = sami_read_packet,
  129. .read_seek2 = sami_read_seek,
  130. .read_close = sami_read_close,
  131. .extensions = "smi,sami",
  132. };