samidec.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "libavutil/avstring.h"
  29. #include "libavutil/bprint.h"
  30. #include "libavutil/intreadwrite.h"
  31. typedef struct {
  32. FFDemuxSubtitlesQueue q;
  33. } SAMIContext;
  34. static int sami_probe(AVProbeData *p)
  35. {
  36. const unsigned char *ptr = p->buf;
  37. if (AV_RB24(ptr) == 0xEFBBBF)
  38. ptr += 3; /* skip UTF-8 BOM */
  39. return !strncmp(ptr, "<SAMI>", 6) ? AVPROBE_SCORE_MAX : 0;
  40. }
  41. static int sami_read_header(AVFormatContext *s)
  42. {
  43. SAMIContext *sami = s->priv_data;
  44. AVStream *st = avformat_new_stream(s, NULL);
  45. AVBPrint buf, hdr_buf;
  46. char c = 0;
  47. int res = 0, got_first_sync_point = 0;
  48. if (!st)
  49. return AVERROR(ENOMEM);
  50. avpriv_set_pts_info(st, 64, 1, 1000);
  51. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  52. st->codec->codec_id = AV_CODEC_ID_SAMI;
  53. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  54. av_bprint_init(&hdr_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  55. while (!url_feof(s->pb)) {
  56. AVPacket *sub;
  57. const int64_t pos = avio_tell(s->pb) - (c != 0);
  58. int is_sync, n = ff_smil_extract_next_chunk(s->pb, &buf, &c);
  59. if (n == 0)
  60. break;
  61. is_sync = !av_strncasecmp(buf.str, "<SYNC", 5);
  62. if (is_sync)
  63. got_first_sync_point = 1;
  64. if (!got_first_sync_point) {
  65. av_bprintf(&hdr_buf, "%s", buf.str);
  66. } else {
  67. sub = ff_subtitles_queue_insert(&sami->q, buf.str, buf.len, !is_sync);
  68. if (!sub) {
  69. res = AVERROR(ENOMEM);
  70. goto end;
  71. }
  72. if (is_sync) {
  73. const char *p = ff_smil_get_attr_ptr(buf.str, "Start");
  74. sub->pos = pos;
  75. sub->pts = p ? strtol(p, NULL, 10) : 0;
  76. sub->duration = -1;
  77. }
  78. }
  79. av_bprint_clear(&buf);
  80. }
  81. st->codec->extradata_size = hdr_buf.len + 1;
  82. av_bprint_finalize(&hdr_buf, (char **)&st->codec->extradata);
  83. if (!st->codec->extradata) {
  84. st->codec->extradata_size = 0;
  85. res = AVERROR(ENOMEM);
  86. goto end;
  87. }
  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_close(AVFormatContext *s)
  99. {
  100. SAMIContext *sami = s->priv_data;
  101. ff_subtitles_queue_clean(&sami->q);
  102. return 0;
  103. }
  104. AVInputFormat ff_sami_demuxer = {
  105. .name = "sami",
  106. .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle format"),
  107. .priv_data_size = sizeof(SAMIContext),
  108. .read_probe = sami_probe,
  109. .read_header = sami_read_header,
  110. .read_packet = sami_read_packet,
  111. .read_close = sami_read_close,
  112. .flags = AVFMT_GENERIC_INDEX,
  113. .extensions = "smi,sami",
  114. };