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.

145 lines
4.2KB

  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. * MPlayer subtitles format demuxer
  23. */
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "subtitles.h"
  27. typedef struct {
  28. FFDemuxSubtitlesQueue q;
  29. } MPSubContext;
  30. static int mpsub_probe(AVProbeData *p)
  31. {
  32. const char *ptr = p->buf;
  33. const char *ptr_end = p->buf + p->buf_size;
  34. while (ptr < ptr_end) {
  35. int inc;
  36. if (!memcmp(ptr, "FORMAT=TIME", 11))
  37. return AVPROBE_SCORE_EXTENSION;
  38. if (!memcmp(ptr, "FORMAT=", 7))
  39. return AVPROBE_SCORE_EXTENSION / 3;
  40. inc = ff_subtitles_next_line(ptr);
  41. if (!inc)
  42. break;
  43. ptr += inc;
  44. }
  45. return 0;
  46. }
  47. static int mpsub_read_header(AVFormatContext *s)
  48. {
  49. MPSubContext *mpsub = s->priv_data;
  50. AVStream *st;
  51. AVBPrint buf;
  52. AVRational pts_info = (AVRational){ 100, 1 }; // ts based by default
  53. int res = 0;
  54. float multiplier = 100.0;
  55. float current_pts = 0;
  56. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  57. while (!url_feof(s->pb)) {
  58. char line[1024];
  59. float start, duration;
  60. int fps, len = ff_get_line(s->pb, line, sizeof(line));
  61. if (!len)
  62. break;
  63. line[strcspn(line, "\r\n")] = 0;
  64. if (sscanf(line, "FORMAT=%d", &fps) == 1 && fps > 3 && fps < 100) {
  65. /* frame based timing */
  66. pts_info = (AVRational){ fps, 1 };
  67. multiplier = 1.0;
  68. } else if (sscanf(line, "%f %f", &start, &duration) == 2) {
  69. AVPacket *sub;
  70. const int64_t pos = avio_tell(s->pb);
  71. ff_subtitles_read_chunk(s->pb, &buf);
  72. if (buf.len) {
  73. sub = ff_subtitles_queue_insert(&mpsub->q, buf.str, buf.len, 0);
  74. if (!sub) {
  75. res = AVERROR(ENOMEM);
  76. goto end;
  77. }
  78. sub->pts = (int64_t)(current_pts + start*multiplier);
  79. sub->duration = (int)(duration * multiplier);
  80. current_pts += (start + duration) * multiplier;
  81. sub->pos = pos;
  82. }
  83. }
  84. }
  85. st = avformat_new_stream(s, NULL);
  86. if (!st)
  87. return AVERROR(ENOMEM);
  88. avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
  89. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  90. st->codec->codec_id = AV_CODEC_ID_TEXT;
  91. ff_subtitles_queue_finalize(&mpsub->q);
  92. end:
  93. av_bprint_finalize(&buf, NULL);
  94. return res;
  95. }
  96. static int mpsub_read_packet(AVFormatContext *s, AVPacket *pkt)
  97. {
  98. MPSubContext *mpsub = s->priv_data;
  99. return ff_subtitles_queue_read_packet(&mpsub->q, pkt);
  100. }
  101. static int mpsub_read_seek(AVFormatContext *s, int stream_index,
  102. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  103. {
  104. MPSubContext *mpsub = s->priv_data;
  105. return ff_subtitles_queue_seek(&mpsub->q, s, stream_index,
  106. min_ts, ts, max_ts, flags);
  107. }
  108. static int mpsub_read_close(AVFormatContext *s)
  109. {
  110. MPSubContext *mpsub = s->priv_data;
  111. ff_subtitles_queue_clean(&mpsub->q);
  112. return 0;
  113. }
  114. AVInputFormat ff_mpsub_demuxer = {
  115. .name = "mpsub",
  116. .long_name = NULL_IF_CONFIG_SMALL("MPlayer subtitles"),
  117. .priv_data_size = sizeof(MPSubContext),
  118. .read_probe = mpsub_probe,
  119. .read_header = mpsub_read_header,
  120. .read_packet = mpsub_read_packet,
  121. .read_seek2 = mpsub_read_seek,
  122. .read_close = mpsub_read_close,
  123. .extensions = "sub",
  124. };