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.

110 lines
3.2KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "avformat.h"
  19. #include "internal.h"
  20. #include "libavutil/intreadwrite.h"
  21. #define SUP_PGS_MAGIC 0x5047 /* "PG", big endian */
  22. static int sup_read_header(AVFormatContext *s)
  23. {
  24. AVStream *st = avformat_new_stream(s, NULL);
  25. if (!st)
  26. return AVERROR(ENOMEM);
  27. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  28. st->codec->codec_id = AV_CODEC_ID_HDMV_PGS_SUBTITLE;
  29. avpriv_set_pts_info(st, 32, 1, 90000);
  30. return 0;
  31. }
  32. static int sup_read_packet(AVFormatContext *s, AVPacket *pkt)
  33. {
  34. int64_t pts, dts, pos;
  35. int ret;
  36. pos = avio_tell(s->pb);
  37. if (avio_rb16(s->pb) != SUP_PGS_MAGIC)
  38. return avio_feof(s->pb) ? AVERROR_EOF : AVERROR_INVALIDDATA;
  39. pts = avio_rb32(s->pb);
  40. dts = avio_rb32(s->pb);
  41. if ((ret = av_get_packet(s->pb, pkt, 3)) < 0)
  42. return ret;
  43. pkt->stream_index = 0;
  44. pkt->flags |= AV_PKT_FLAG_KEY;
  45. pkt->pos = pos;
  46. pkt->pts = pts;
  47. // Many files have DTS set to 0 for all packets, so assume 0 means unset.
  48. pkt->dts = dts ? dts : AV_NOPTS_VALUE;
  49. if (pkt->size >= 3) {
  50. // The full packet size is stored as part of the packet.
  51. size_t len = AV_RB16(pkt->data + 1);
  52. if ((ret = av_append_packet(s->pb, pkt, len)) < 0)
  53. return ret;
  54. }
  55. return 0;
  56. }
  57. static int sup_probe(AVProbeData *p)
  58. {
  59. unsigned char *buf = p->buf;
  60. size_t buf_size = p->buf_size;
  61. int nb_packets;
  62. for (nb_packets = 0; nb_packets < 10; nb_packets++) {
  63. size_t full_packet_size;
  64. if (buf_size < 10 + 3)
  65. break;
  66. if (AV_RB16(buf) != SUP_PGS_MAGIC)
  67. return 0;
  68. full_packet_size = AV_RB16(buf + 10 + 1) + 10 + 3;
  69. if (buf_size < full_packet_size)
  70. break;
  71. buf += full_packet_size;
  72. buf_size -= full_packet_size;
  73. }
  74. if (!nb_packets)
  75. return 0;
  76. if (nb_packets < 2)
  77. return AVPROBE_SCORE_RETRY / 2;
  78. if (nb_packets < 4)
  79. return AVPROBE_SCORE_RETRY;
  80. if (nb_packets < 10)
  81. return AVPROBE_SCORE_EXTENSION;
  82. return AVPROBE_SCORE_MAX;
  83. }
  84. AVInputFormat ff_sup_demuxer = {
  85. .name = "sup",
  86. .long_name = NULL_IF_CONFIG_SMALL("raw HDMV Presentation Graphic Stream subtitles"),
  87. .extensions = "sup",
  88. .mime_type = "application/x-pgs",
  89. .read_probe = sup_probe,
  90. .read_header = sup_read_header,
  91. .read_packet = sup_read_packet,
  92. .flags = AVFMT_GENERIC_INDEX,
  93. };