microdvddec.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * MicroDVD subtitle demuxer
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  4. * Copyright (c) 2012 Clément Bœsch <ubitux@gmail.com>
  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, *ptr = p->buf;
  33. int i;
  34. if (AV_RB24(ptr) == 0xEFBBBF)
  35. ptr += 3; /* skip UTF-8 BOM */
  36. for (i=0; i<3; i++) {
  37. if (sscanf(ptr, "{%*d}{}%c", &c) != 1 &&
  38. sscanf(ptr, "{%*d}{%*d}%c", &c) != 1 &&
  39. sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
  40. return 0;
  41. ptr += strcspn(ptr, "\n") + 1;
  42. }
  43. return AVPROBE_SCORE_MAX;
  44. }
  45. static int64_t get_pts(const char *buf)
  46. {
  47. int frame;
  48. char c;
  49. if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
  50. return frame;
  51. return AV_NOPTS_VALUE;
  52. }
  53. static int get_duration(const char *buf)
  54. {
  55. int frame_start, frame_end;
  56. if (sscanf(buf, "{%d}{%d}", &frame_start, &frame_end) == 2)
  57. return frame_end - frame_start;
  58. return -1;
  59. }
  60. static int microdvd_read_header(AVFormatContext *s)
  61. {
  62. AVRational pts_info = (AVRational){ 2997, 125 }; /* default: 23.976 fps */
  63. MicroDVDContext *microdvd = s->priv_data;
  64. AVStream *st = avformat_new_stream(s, NULL);
  65. int i = 0;
  66. char line[MAX_LINESIZE];
  67. if (!st)
  68. return AVERROR(ENOMEM);
  69. while (!url_feof(s->pb)) {
  70. AVPacket *sub;
  71. int64_t pos = avio_tell(s->pb);
  72. int len = ff_get_line(s->pb, line, sizeof(line));
  73. if (!len)
  74. break;
  75. if (i < 3) {
  76. int frame;
  77. double fps;
  78. char c;
  79. i++;
  80. if ((sscanf(line, "{%d}{}%6lf", &frame, &fps) == 2 ||
  81. sscanf(line, "{%d}{%*d}%6lf", &frame, &fps) == 2)
  82. && frame <= 1 && fps > 3 && fps < 100)
  83. pts_info = av_d2q(fps, 100000);
  84. if (!st->codec->extradata && sscanf(line, "{DEFAULT}{}%c", &c) == 1) {
  85. st->codec->extradata = av_strdup(line + 11);
  86. if (!st->codec->extradata)
  87. return AVERROR(ENOMEM);
  88. st->codec->extradata_size = strlen(st->codec->extradata) + 1;
  89. continue;
  90. }
  91. }
  92. sub = ff_subtitles_queue_insert(&microdvd->q, line, len, 0);
  93. if (!sub)
  94. return AVERROR(ENOMEM);
  95. sub->pos = pos;
  96. sub->pts = get_pts(sub->data);
  97. sub->duration = get_duration(sub->data);
  98. }
  99. ff_subtitles_queue_finalize(&microdvd->q);
  100. avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
  101. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  102. st->codec->codec_id = AV_CODEC_ID_MICRODVD;
  103. return 0;
  104. }
  105. static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
  106. {
  107. MicroDVDContext *microdvd = s->priv_data;
  108. return ff_subtitles_queue_read_packet(&microdvd->q, pkt);
  109. }
  110. static int microdvd_read_close(AVFormatContext *s)
  111. {
  112. MicroDVDContext *microdvd = s->priv_data;
  113. ff_subtitles_queue_clean(&microdvd->q);
  114. return 0;
  115. }
  116. AVInputFormat ff_microdvd_demuxer = {
  117. .name = "microdvd",
  118. .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
  119. .priv_data_size = sizeof(MicroDVDContext),
  120. .read_probe = microdvd_probe,
  121. .read_header = microdvd_read_header,
  122. .read_packet = microdvd_read_packet,
  123. .read_close = microdvd_read_close,
  124. .flags = AVFMT_GENERIC_INDEX,
  125. };