srtdec.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * SubRip 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. static int srt_probe(AVProbeData *p)
  25. {
  26. unsigned char *ptr = p->buf;
  27. int i, v, num = 0;
  28. if (AV_RB24(ptr) == 0xEFBBBF)
  29. ptr += 3; /* skip UTF-8 BOM */
  30. for (i=0; i<2; i++) {
  31. if (num == i && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
  32. return AVPROBE_SCORE_MAX;
  33. num = atoi(ptr);
  34. ptr += strcspn(ptr, "\n") + 1;
  35. }
  36. return 0;
  37. }
  38. static int srt_read_header(AVFormatContext *s)
  39. {
  40. AVStream *st = avformat_new_stream(s, NULL);
  41. if (!st)
  42. return AVERROR(ENOMEM);
  43. avpriv_set_pts_info(st, 64, 1, 1000);
  44. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  45. st->codec->codec_id = AV_CODEC_ID_SRT;
  46. return 0;
  47. }
  48. static int64_t get_pts(const char *buf, int *duration)
  49. {
  50. int i, hour, min, sec, hsec;
  51. int he, me, se, mse;
  52. for (i=0; i<2; i++) {
  53. int64_t start, end;
  54. if (sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
  55. &hour, &min, &sec, &hsec, &he, &me, &se, &mse) == 8) {
  56. min += 60*hour;
  57. sec += 60*min;
  58. start = sec*1000+hsec;
  59. me += 60*he;
  60. se += 60*me;
  61. end = se*1000+mse;
  62. *duration = end - start;
  63. return start;
  64. }
  65. buf += strcspn(buf, "\n") + 1;
  66. }
  67. return AV_NOPTS_VALUE;
  68. }
  69. static inline int is_eol(char c)
  70. {
  71. return c == '\r' || c == '\n';
  72. }
  73. static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
  74. {
  75. char buffer[2048], *ptr = buffer, *ptr2;
  76. int64_t pos = avio_tell(s->pb);
  77. int res = AVERROR_EOF;
  78. do {
  79. ptr2 = ptr;
  80. ptr += ff_get_line(s->pb, ptr, sizeof(buffer)+buffer-ptr);
  81. } while (!is_eol(*ptr2) && !url_feof(s->pb) && ptr-buffer<sizeof(buffer)-1);
  82. if (buffer[0] && !(res = av_new_packet(pkt, ptr-buffer))) {
  83. memcpy(pkt->data, buffer, pkt->size);
  84. pkt->flags |= AV_PKT_FLAG_KEY;
  85. pkt->pos = pos;
  86. pkt->pts = pkt->dts = get_pts(pkt->data, &(pkt->duration));
  87. }
  88. return res;
  89. }
  90. AVInputFormat ff_srt_demuxer = {
  91. .name = "srt",
  92. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  93. .read_probe = srt_probe,
  94. .read_header = srt_read_header,
  95. .read_packet = srt_read_packet,
  96. .flags = AVFMT_GENERIC_INDEX,
  97. };