mgsts.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Metar Gear Solid: The Twin Snakes demuxer
  3. * Copyright (c) 2012 Paul B Mahol
  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 "libavutil/intreadwrite.h"
  22. #include "libavutil/intfloat.h"
  23. #include "avformat.h"
  24. #include "riff.h"
  25. static int read_probe(AVProbeData *p)
  26. {
  27. if (AV_RB32(p->buf ) != 0x000E ||
  28. AV_RB32(p->buf + 4) != 0x0050 ||
  29. AV_RB32(p->buf + 12) != 0x0034)
  30. return 0;
  31. return AVPROBE_SCORE_MAX;
  32. }
  33. static int read_header(AVFormatContext *s)
  34. {
  35. AVIOContext *pb = s->pb;
  36. AVStream *st;
  37. AVRational fps;
  38. uint32_t chunk_size;
  39. avio_skip(pb, 4);
  40. chunk_size = avio_rb32(pb);
  41. if (chunk_size != 80)
  42. return AVERROR(EIO);
  43. avio_skip(pb, 20);
  44. st = avformat_new_stream(s, 0);
  45. if (!st)
  46. return AVERROR(ENOMEM);
  47. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  48. st->start_time = 0;
  49. st->nb_frames =
  50. st->duration = avio_rb32(pb);
  51. fps = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX);
  52. st->codec->width = avio_rb32(pb);
  53. st->codec->height = avio_rb32(pb);
  54. avio_skip(pb, 12);
  55. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  56. st->codec->codec_tag = avio_rb32(pb);
  57. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags,
  58. st->codec->codec_tag);
  59. avpriv_set_pts_info(st, 64, fps.den, fps.num);
  60. avio_skip(pb, 20);
  61. return 0;
  62. }
  63. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  64. {
  65. AVIOContext *pb = s->pb;
  66. uint32_t chunk_size, payload_size;
  67. int ret;
  68. if (url_feof(pb))
  69. return AVERROR_EOF;
  70. avio_skip(pb, 4);
  71. chunk_size = avio_rb32(pb);
  72. avio_skip(pb, 4);
  73. payload_size = avio_rb32(pb);
  74. if (chunk_size < payload_size + 16)
  75. return AVERROR(EIO);
  76. ret = av_get_packet(pb, pkt, payload_size);
  77. if (ret < 0)
  78. return ret;
  79. pkt->pos -= 16;
  80. pkt->duration = 1;
  81. avio_skip(pb, chunk_size - (ret + 16));
  82. return ret;
  83. }
  84. AVInputFormat ff_mgsts_demuxer = {
  85. .name = "mgsts",
  86. .long_name = NULL_IF_CONFIG_SMALL("Metal Gear Solid: The Twin Snakes"),
  87. .read_probe = read_probe,
  88. .read_header = read_header,
  89. .read_packet = read_packet,
  90. .flags = AVFMT_GENERIC_INDEX,
  91. };