srtdec.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, AVFormatParameters *ap)
  39. {
  40. AVStream *st = avformat_new_stream(s, NULL);
  41. if (!st)
  42. return -1;
  43. avpriv_set_pts_info(st, 64, 1, 1000);
  44. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  45. st->codec->codec_id = CODEC_ID_SRT;
  46. return 0;
  47. }
  48. static int64_t get_pts(const char *buf)
  49. {
  50. int i, v, hour, min, sec, hsec;
  51. for (i=0; i<2; i++) {
  52. if (sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %*d:%*2d:%*2d%*1[,.]%3d",
  53. &hour, &min, &sec, &hsec, &v) == 5) {
  54. min += 60*hour;
  55. sec += 60*min;
  56. return sec*1000+hsec;
  57. }
  58. buf += strcspn(buf, "\n") + 1;
  59. }
  60. return AV_NOPTS_VALUE;
  61. }
  62. static inline int is_eol(char c)
  63. {
  64. return c == '\r' || c == '\n';
  65. }
  66. static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
  67. {
  68. char buffer[2048], *ptr = buffer, *ptr2;
  69. int64_t pos = avio_tell(s->pb);
  70. int res = AVERROR_EOF;
  71. do {
  72. ptr2 = ptr;
  73. ptr += ff_get_line(s->pb, ptr, sizeof(buffer)+buffer-ptr);
  74. } while (!is_eol(*ptr2) && !url_feof(s->pb) && ptr-buffer<sizeof(buffer)-1);
  75. if (buffer[0] && !(res = av_new_packet(pkt, ptr-buffer))) {
  76. memcpy(pkt->data, buffer, pkt->size);
  77. pkt->flags |= AV_PKT_FLAG_KEY;
  78. pkt->pos = pos;
  79. pkt->pts = pkt->dts = get_pts(pkt->data);
  80. }
  81. return res;
  82. }
  83. AVInputFormat ff_srt_demuxer = {
  84. .name = "srt",
  85. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle format"),
  86. .read_probe = srt_probe,
  87. .read_header = srt_read_header,
  88. .read_packet = srt_read_packet,
  89. .flags = AVFMT_GENERIC_INDEX,
  90. };