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.

138 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. const unsigned char *ptr = p->buf;
  38. if (AV_RB24(ptr) == 0xEFBBBF)
  39. ptr += 3; /* skip UTF-8 BOM */
  40. return !strncmp(ptr, "<SAMI>", 6) ? AVPROBE_SCORE_MAX : 0;
  41. }
  42. static int sami_read_header(AVFormatContext *s)
  43. {
  44. SAMIContext *sami = s->priv_data;
  45. AVStream *st = avformat_new_stream(s, NULL);
  46. AVBPrint buf, hdr_buf;
  47. char c = 0;
  48. int res = 0, got_first_sync_point = 0;
  49. if (!st)
  50. return AVERROR(ENOMEM);
  51. avpriv_set_pts_info(st, 64, 1, 1000);
  52. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  53. st->codec->codec_id = AV_CODEC_ID_SAMI;
  54. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  55. av_bprint_init(&hdr_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  56. while (!url_feof(s->pb)) {
  57. AVPacket *sub;
  58. const int64_t pos = avio_tell(s->pb) - (c != 0);
  59. int is_sync, n = ff_smil_extract_next_chunk(s->pb, &buf, &c);
  60. if (n == 0)
  61. break;
  62. is_sync = !av_strncasecmp(buf.str, "<SYNC", 5);
  63. if (is_sync)
  64. got_first_sync_point = 1;
  65. if (!got_first_sync_point) {
  66. av_bprintf(&hdr_buf, "%s", buf.str);
  67. } else {
  68. sub = ff_subtitles_queue_insert(&sami->q, buf.str, buf.len, !is_sync);
  69. if (!sub) {
  70. res = AVERROR(ENOMEM);
  71. goto end;
  72. }
  73. if (is_sync) {
  74. const char *p = ff_smil_get_attr_ptr(buf.str, "Start");
  75. sub->pos = pos;
  76. sub->pts = p ? strtol(p, NULL, 10) : 0;
  77. sub->duration = -1;
  78. }
  79. }
  80. av_bprint_clear(&buf);
  81. }
  82. res = avpriv_bprint_to_extradata(st->codec, &hdr_buf);
  83. if (res < 0)
  84. goto end;
  85. ff_subtitles_queue_finalize(&sami->q);
  86. end:
  87. av_bprint_finalize(&buf, NULL);
  88. return res;
  89. }
  90. static int sami_read_packet(AVFormatContext *s, AVPacket *pkt)
  91. {
  92. SAMIContext *sami = s->priv_data;
  93. return ff_subtitles_queue_read_packet(&sami->q, pkt);
  94. }
  95. static int sami_read_seek(AVFormatContext *s, int stream_index,
  96. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  97. {
  98. SAMIContext *sami = s->priv_data;
  99. return ff_subtitles_queue_seek(&sami->q, s, stream_index,
  100. min_ts, ts, max_ts, flags);
  101. }
  102. static int sami_read_close(AVFormatContext *s)
  103. {
  104. SAMIContext *sami = s->priv_data;
  105. ff_subtitles_queue_clean(&sami->q);
  106. return 0;
  107. }
  108. AVInputFormat ff_sami_demuxer = {
  109. .name = "sami",
  110. .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle format"),
  111. .priv_data_size = sizeof(SAMIContext),
  112. .read_probe = sami_probe,
  113. .read_header = sami_read_header,
  114. .read_packet = sami_read_packet,
  115. .read_seek2 = sami_read_seek,
  116. .read_close = sami_read_close,
  117. .extensions = "smi,sami",
  118. };