microdvddec.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * MicroDVD subtitle demuxer
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "internal.h"
  23. #include "libavutil/intreadwrite.h"
  24. #define MAX_LINESIZE 2048
  25. typedef struct {
  26. uint8_t lines[3][MAX_LINESIZE];
  27. int64_t pos[3];
  28. } MicroDVDContext;
  29. static int microdvd_probe(AVProbeData *p)
  30. {
  31. unsigned char c, *ptr = p->buf;
  32. int i;
  33. if (AV_RB24(ptr) == 0xEFBBBF)
  34. ptr += 3; /* skip UTF-8 BOM */
  35. for (i=0; i<3; i++) {
  36. if (sscanf(ptr, "{%*d}{}%c", &c) != 1 &&
  37. sscanf(ptr, "{%*d}{%*d}%c", &c) != 1 &&
  38. sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
  39. return 0;
  40. ptr += strcspn(ptr, "\n") + 1;
  41. }
  42. return AVPROBE_SCORE_MAX;
  43. }
  44. static int microdvd_read_header(AVFormatContext *s, AVFormatParameters *ap)
  45. {
  46. AVRational pts_info = (AVRational){ 2997, 125 }; /* default: 23.976 fps */
  47. MicroDVDContext *microdvd = s->priv_data;
  48. AVStream *st = av_new_stream(s, 0);
  49. int i, frame;
  50. double fps;
  51. char c;
  52. if (!st)
  53. return -1;
  54. for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
  55. microdvd->pos[i] = avio_tell(s->pb);
  56. ff_get_line(s->pb, microdvd->lines[i], sizeof(microdvd->lines[i]));
  57. if ((sscanf(microdvd->lines[i], "{%d}{}%6lf", &frame, &fps) == 2 ||
  58. sscanf(microdvd->lines[i], "{%d}{%*d}%6lf", &frame, &fps) == 2)
  59. && frame <= 1 && fps > 3 && fps < 100)
  60. pts_info = av_d2q(fps, 100000);
  61. if (sscanf(microdvd->lines[i], "{DEFAULT}{}%c", &c) == 1) {
  62. st->codec->extradata = av_strdup(microdvd->lines[i] + 11);
  63. st->codec->extradata_size = strlen(st->codec->extradata);
  64. i--;
  65. }
  66. }
  67. av_set_pts_info(st, 64, pts_info.den, pts_info.num);
  68. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  69. st->codec->codec_id = CODEC_ID_MICRODVD;
  70. return 0;
  71. }
  72. static int64_t get_pts(const char *buf)
  73. {
  74. int frame;
  75. char c;
  76. if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
  77. return frame;
  78. return AV_NOPTS_VALUE;
  79. }
  80. static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
  81. {
  82. MicroDVDContext *microdvd = s->priv_data;
  83. char buffer[MAX_LINESIZE];
  84. int64_t pos = avio_tell(s->pb);
  85. int i, len = 0, res = AVERROR_EOF;
  86. for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
  87. if (microdvd->lines[i][0]) {
  88. strcpy(buffer, microdvd->lines[i]);
  89. pos = microdvd->pos[i];
  90. len = strlen(buffer);
  91. microdvd->lines[i][0] = 0;
  92. break;
  93. }
  94. }
  95. if (!len)
  96. len = ff_get_line(s->pb, buffer, sizeof(buffer));
  97. if (buffer[0] && !(res = av_new_packet(pkt, len))) {
  98. memcpy(pkt->data, buffer, len);
  99. pkt->flags |= AV_PKT_FLAG_KEY;
  100. pkt->pos = pos;
  101. pkt->pts = pkt->dts = get_pts(buffer);
  102. }
  103. return res;
  104. }
  105. AVInputFormat ff_microdvd_demuxer = {
  106. .name = "microdvd",
  107. .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
  108. .priv_data_size = sizeof(MicroDVDContext),
  109. .read_probe = microdvd_probe,
  110. .read_header = microdvd_read_header,
  111. .read_packet = microdvd_read_packet,
  112. .flags = AVFMT_GENERIC_INDEX,
  113. };