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.

141 lines
4.0KB

  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(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->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  56. st->codec->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, n = ff_smil_extract_next_text_chunk(&tr, &buf, &c);
  63. if (n == 0)
  64. break;
  65. is_sync = !av_strncasecmp(buf.str, "<SYNC", 5);
  66. if (is_sync)
  67. got_first_sync_point = 1;
  68. if (!got_first_sync_point) {
  69. av_bprintf(&hdr_buf, "%s", buf.str);
  70. } else {
  71. sub = ff_subtitles_queue_insert(&sami->q, buf.str, buf.len, !is_sync);
  72. if (!sub) {
  73. res = AVERROR(ENOMEM);
  74. goto end;
  75. }
  76. if (is_sync) {
  77. const char *p = ff_smil_get_attr_ptr(buf.str, "Start");
  78. sub->pos = pos;
  79. sub->pts = p ? strtol(p, NULL, 10) : 0;
  80. sub->duration = -1;
  81. }
  82. }
  83. av_bprint_clear(&buf);
  84. }
  85. res = avpriv_bprint_to_extradata(st->codec, &hdr_buf);
  86. if (res < 0)
  87. goto end;
  88. ff_subtitles_queue_finalize(&sami->q);
  89. end:
  90. av_bprint_finalize(&buf, NULL);
  91. return res;
  92. }
  93. static int sami_read_packet(AVFormatContext *s, AVPacket *pkt)
  94. {
  95. SAMIContext *sami = s->priv_data;
  96. return ff_subtitles_queue_read_packet(&sami->q, pkt);
  97. }
  98. static int sami_read_seek(AVFormatContext *s, int stream_index,
  99. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  100. {
  101. SAMIContext *sami = s->priv_data;
  102. return ff_subtitles_queue_seek(&sami->q, s, stream_index,
  103. min_ts, ts, max_ts, flags);
  104. }
  105. static int sami_read_close(AVFormatContext *s)
  106. {
  107. SAMIContext *sami = s->priv_data;
  108. ff_subtitles_queue_clean(&sami->q);
  109. return 0;
  110. }
  111. AVInputFormat ff_sami_demuxer = {
  112. .name = "sami",
  113. .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle format"),
  114. .priv_data_size = sizeof(SAMIContext),
  115. .read_probe = sami_probe,
  116. .read_header = sami_read_header,
  117. .read_packet = sami_read_packet,
  118. .read_seek2 = sami_read_seek,
  119. .read_close = sami_read_close,
  120. .extensions = "smi,sami",
  121. };