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.

204 lines
6.2KB

  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. #include "libavutil/opt.h"
  27. #define MAX_LINESIZE 2048
  28. typedef struct {
  29. const AVClass *class;
  30. FFDemuxSubtitlesQueue q;
  31. AVRational frame_rate;
  32. } MicroDVDContext;
  33. static int microdvd_probe(AVProbeData *p)
  34. {
  35. unsigned char c;
  36. const uint8_t *ptr = p->buf;
  37. int i;
  38. if (AV_RB24(ptr) == 0xEFBBBF)
  39. ptr += 3; /* skip UTF-8 BOM */
  40. for (i=0; i<3; i++) {
  41. if (sscanf(ptr, "{%*d}{}%c", &c) != 1 &&
  42. sscanf(ptr, "{%*d}{%*d}%c", &c) != 1 &&
  43. sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
  44. return 0;
  45. ptr += ff_subtitles_next_line(ptr);
  46. }
  47. return AVPROBE_SCORE_MAX;
  48. }
  49. static int64_t get_pts(const char *buf)
  50. {
  51. int frame;
  52. char c;
  53. if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
  54. return frame;
  55. return AV_NOPTS_VALUE;
  56. }
  57. static int get_duration(const char *buf)
  58. {
  59. int frame_start, frame_end;
  60. if (sscanf(buf, "{%d}{%d}", &frame_start, &frame_end) == 2)
  61. return frame_end - frame_start;
  62. return -1;
  63. }
  64. static const char *bom = "\xEF\xBB\xBF";
  65. static int microdvd_read_header(AVFormatContext *s)
  66. {
  67. AVRational pts_info = (AVRational){ 2997, 125 }; /* default: 23.976 fps */
  68. MicroDVDContext *microdvd = s->priv_data;
  69. AVStream *st = avformat_new_stream(s, NULL);
  70. int i = 0;
  71. char line_buf[MAX_LINESIZE];
  72. int has_real_fps = 0;
  73. if (!st)
  74. return AVERROR(ENOMEM);
  75. while (!url_feof(s->pb)) {
  76. char *p;
  77. AVPacket *sub;
  78. int64_t pos = avio_tell(s->pb);
  79. int len = ff_get_line(s->pb, line_buf, sizeof(line_buf));
  80. char *line = line_buf;
  81. if (!strncmp(line, bom, 3))
  82. line += 3;
  83. p = line;
  84. if (!len)
  85. break;
  86. line[strcspn(line, "\r\n")] = 0;
  87. if (i++ < 3) {
  88. int frame;
  89. double fps;
  90. char c;
  91. if ((sscanf(line, "{%d}{}%6lf", &frame, &fps) == 2 ||
  92. sscanf(line, "{%d}{%*d}%6lf", &frame, &fps) == 2)
  93. && frame <= 1 && fps > 3 && fps < 100) {
  94. pts_info = av_d2q(fps, 100000);
  95. has_real_fps = 1;
  96. }
  97. if (!st->codec->extradata && sscanf(line, "{DEFAULT}{}%c", &c) == 1) {
  98. st->codec->extradata = av_strdup(line + 11);
  99. if (!st->codec->extradata)
  100. return AVERROR(ENOMEM);
  101. st->codec->extradata_size = strlen(st->codec->extradata) + 1;
  102. continue;
  103. }
  104. }
  105. #define SKIP_FRAME_ID \
  106. p = strchr(p, '}'); \
  107. if (!p) { \
  108. av_log(s, AV_LOG_WARNING, "Invalid event \"%s\"" \
  109. " at line %d\n", line, i); \
  110. continue; \
  111. } \
  112. p++
  113. SKIP_FRAME_ID;
  114. SKIP_FRAME_ID;
  115. if (!*p)
  116. continue;
  117. sub = ff_subtitles_queue_insert(&microdvd->q, p, strlen(p), 0);
  118. if (!sub)
  119. return AVERROR(ENOMEM);
  120. sub->pos = pos;
  121. sub->pts = get_pts(line);
  122. sub->duration = get_duration(line);
  123. }
  124. ff_subtitles_queue_finalize(&microdvd->q);
  125. if (has_real_fps) {
  126. /* export the FPS info only if set in the file */
  127. microdvd->frame_rate = pts_info;
  128. } else if (microdvd->frame_rate.num) {
  129. /* fallback on user specified frame rate */
  130. pts_info = microdvd->frame_rate;
  131. }
  132. avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
  133. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  134. st->codec->codec_id = AV_CODEC_ID_MICRODVD;
  135. return 0;
  136. }
  137. static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
  138. {
  139. MicroDVDContext *microdvd = s->priv_data;
  140. return ff_subtitles_queue_read_packet(&microdvd->q, pkt);
  141. }
  142. static int microdvd_read_seek(AVFormatContext *s, int stream_index,
  143. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  144. {
  145. MicroDVDContext *microdvd = s->priv_data;
  146. return ff_subtitles_queue_seek(&microdvd->q, s, stream_index,
  147. min_ts, ts, max_ts, flags);
  148. }
  149. static int microdvd_read_close(AVFormatContext *s)
  150. {
  151. MicroDVDContext *microdvd = s->priv_data;
  152. ff_subtitles_queue_clean(&microdvd->q);
  153. return 0;
  154. }
  155. #define OFFSET(x) offsetof(MicroDVDContext, x)
  156. #define SD AV_OPT_FLAG_SUBTITLE_PARAM|AV_OPT_FLAG_DECODING_PARAM
  157. static const AVOption microdvd_options[] = {
  158. { "subfps", "set the movie frame rate fallback", OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, SD },
  159. { NULL }
  160. };
  161. static const AVClass microdvd_class = {
  162. .class_name = "microdvddec",
  163. .item_name = av_default_item_name,
  164. .option = microdvd_options,
  165. .version = LIBAVUTIL_VERSION_INT,
  166. };
  167. AVInputFormat ff_microdvd_demuxer = {
  168. .name = "microdvd",
  169. .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
  170. .priv_data_size = sizeof(MicroDVDContext),
  171. .read_probe = microdvd_probe,
  172. .read_header = microdvd_read_header,
  173. .read_packet = microdvd_read_packet,
  174. .read_seek2 = microdvd_read_seek,
  175. .read_close = microdvd_read_close,
  176. .priv_class = &microdvd_class,
  177. };