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.

168 lines
5.1KB

  1. /*
  2. * MicroDVD subtitle demuxer
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  4. * Copyright (c) 2012 Clément Bœsch <u pkh me>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "subtitles.h"
  25. #include "libavutil/intreadwrite.h"
  26. #define MAX_LINESIZE 2048
  27. typedef struct {
  28. FFDemuxSubtitlesQueue q;
  29. } MicroDVDContext;
  30. static int microdvd_probe(AVProbeData *p)
  31. {
  32. unsigned char c;
  33. const uint8_t *ptr = p->buf;
  34. int i;
  35. if (AV_RB24(ptr) == 0xEFBBBF)
  36. ptr += 3; /* skip UTF-8 BOM */
  37. for (i=0; i<3; i++) {
  38. if (sscanf(ptr, "{%*d}{}%c", &c) != 1 &&
  39. sscanf(ptr, "{%*d}{%*d}%c", &c) != 1 &&
  40. sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
  41. return 0;
  42. ptr += ff_subtitles_next_line(ptr);
  43. }
  44. return AVPROBE_SCORE_MAX;
  45. }
  46. static int64_t get_pts(const char *buf)
  47. {
  48. int frame;
  49. char c;
  50. if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
  51. return frame;
  52. return AV_NOPTS_VALUE;
  53. }
  54. static int get_duration(const char *buf)
  55. {
  56. int frame_start, frame_end;
  57. if (sscanf(buf, "{%d}{%d}", &frame_start, &frame_end) == 2)
  58. return frame_end - frame_start;
  59. return -1;
  60. }
  61. static int microdvd_read_header(AVFormatContext *s)
  62. {
  63. AVRational pts_info = (AVRational){ 2997, 125 }; /* default: 23.976 fps */
  64. MicroDVDContext *microdvd = s->priv_data;
  65. AVStream *st = avformat_new_stream(s, NULL);
  66. int i = 0;
  67. char line[MAX_LINESIZE];
  68. if (!st)
  69. return AVERROR(ENOMEM);
  70. while (!url_feof(s->pb)) {
  71. char *p = line;
  72. AVPacket *sub;
  73. int64_t pos = avio_tell(s->pb);
  74. int len = ff_get_line(s->pb, line, sizeof(line));
  75. if (!len)
  76. break;
  77. line[strcspn(line, "\r\n")] = 0;
  78. if (i++ < 3) {
  79. int frame;
  80. double fps;
  81. char c;
  82. if ((sscanf(line, "{%d}{}%6lf", &frame, &fps) == 2 ||
  83. sscanf(line, "{%d}{%*d}%6lf", &frame, &fps) == 2)
  84. && frame <= 1 && fps > 3 && fps < 100)
  85. pts_info = av_d2q(fps, 100000);
  86. if (!st->codec->extradata && sscanf(line, "{DEFAULT}{}%c", &c) == 1) {
  87. st->codec->extradata = av_strdup(line + 11);
  88. if (!st->codec->extradata)
  89. return AVERROR(ENOMEM);
  90. st->codec->extradata_size = strlen(st->codec->extradata) + 1;
  91. continue;
  92. }
  93. }
  94. #define SKIP_FRAME_ID \
  95. p = strchr(p, '}'); \
  96. if (!p) { \
  97. av_log(s, AV_LOG_WARNING, "Invalid event \"%s\"" \
  98. " at line %d\n", line, i); \
  99. continue; \
  100. } \
  101. p++
  102. SKIP_FRAME_ID;
  103. SKIP_FRAME_ID;
  104. if (!*p)
  105. continue;
  106. sub = ff_subtitles_queue_insert(&microdvd->q, p, strlen(p), 0);
  107. if (!sub)
  108. return AVERROR(ENOMEM);
  109. sub->pos = pos;
  110. sub->pts = get_pts(line);
  111. sub->duration = get_duration(line);
  112. }
  113. ff_subtitles_queue_finalize(&microdvd->q);
  114. avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
  115. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  116. st->codec->codec_id = AV_CODEC_ID_MICRODVD;
  117. return 0;
  118. }
  119. static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
  120. {
  121. MicroDVDContext *microdvd = s->priv_data;
  122. return ff_subtitles_queue_read_packet(&microdvd->q, pkt);
  123. }
  124. static int microdvd_read_seek(AVFormatContext *s, int stream_index,
  125. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  126. {
  127. MicroDVDContext *microdvd = s->priv_data;
  128. return ff_subtitles_queue_seek(&microdvd->q, s, stream_index,
  129. min_ts, ts, max_ts, flags);
  130. }
  131. static int microdvd_read_close(AVFormatContext *s)
  132. {
  133. MicroDVDContext *microdvd = s->priv_data;
  134. ff_subtitles_queue_clean(&microdvd->q);
  135. return 0;
  136. }
  137. AVInputFormat ff_microdvd_demuxer = {
  138. .name = "microdvd",
  139. .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
  140. .priv_data_size = sizeof(MicroDVDContext),
  141. .read_probe = microdvd_probe,
  142. .read_header = microdvd_read_header,
  143. .read_packet = microdvd_read_packet,
  144. .read_seek2 = microdvd_read_seek,
  145. .read_close = microdvd_read_close,
  146. };