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.

118 lines
3.4KB

  1. /*
  2. * Microsoft Paint (MSP) demuxer
  3. * Copyright (c) 2020 Peter Ross (pross@xvid.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. /**
  22. * @file
  23. * Microsoft Paint (MSP) demuxer
  24. */
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/imgutils.h"
  27. #include "avformat.h"
  28. #include "internal.h"
  29. typedef struct {
  30. int packet_size;
  31. } MSPContext;
  32. static int msp_probe(const AVProbeData *p)
  33. {
  34. unsigned int i, sum;
  35. if (p->buf_size <= 32 || (memcmp(p->buf, "DanM", 4) && memcmp(p->buf, "LinS", 4)))
  36. return 0;
  37. sum = 0;
  38. for (i = 0; i < 24; i += 2)
  39. sum ^= AV_RL16(p->buf + i);
  40. return AV_RL16(p->buf + 24) == sum ? AVPROBE_SCORE_MAX : 0;
  41. }
  42. static int msp_read_header(AVFormatContext *s)
  43. {
  44. MSPContext * cntx = s->priv_data;
  45. AVIOContext *pb = s->pb;
  46. AVStream *st;
  47. st = avformat_new_stream(s, NULL);
  48. if (!st)
  49. return AVERROR(ENOMEM);
  50. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  51. st->codecpar->codec_id = avio_rl32(pb) == MKTAG('D', 'a', 'n', 'M') ? AV_CODEC_ID_RAWVIDEO : AV_CODEC_ID_MSP2;
  52. st->codecpar->width = avio_rl16(pb);
  53. st->codecpar->height = avio_rl16(pb);
  54. st->codecpar->format = AV_PIX_FMT_MONOBLACK;
  55. st->sample_aspect_ratio.num = avio_rl16(pb);
  56. st->sample_aspect_ratio.den = avio_rl16(pb);
  57. avio_skip(pb, 20);
  58. if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) {
  59. cntx->packet_size = av_image_get_buffer_size(st->codecpar->format, st->codecpar->width, st->codecpar->height, 1);
  60. } else
  61. cntx->packet_size = 2 * st->codecpar->height;
  62. if (cntx->packet_size <= 0)
  63. return cntx->packet_size < 0 ? cntx->packet_size : AVERROR_INVALIDDATA;
  64. return 0;
  65. }
  66. static int msp_read_packet(AVFormatContext *s, AVPacket *pkt)
  67. {
  68. AVStream *st = s->streams[0];
  69. MSPContext *cntx = s->priv_data;
  70. int ret;
  71. ret = av_get_packet(s->pb, pkt, cntx->packet_size);
  72. if (ret < 0)
  73. return ret;
  74. if (st->codecpar->codec_id == AV_CODEC_ID_MSP2) {
  75. unsigned int size, i;
  76. if (pkt->size != 2 * st->codecpar->height)
  77. return AVERROR_INVALIDDATA;
  78. size = 0;
  79. for (i = 0; i < st->codecpar->height; i++)
  80. size += AV_RL16(&pkt->data[i * 2]);
  81. ret = av_append_packet(s->pb, pkt, size);
  82. if (ret < 0)
  83. return ret;
  84. }
  85. pkt->stream_index = 0;
  86. pkt->flags |= AV_PKT_FLAG_KEY;
  87. return 0;
  88. }
  89. AVInputFormat ff_msp_demuxer = {
  90. .name = "msp",
  91. .long_name = NULL_IF_CONFIG_SMALL("Microsoft Paint (MSP))"),
  92. .read_probe = msp_probe,
  93. .read_header = msp_read_header,
  94. .read_packet = msp_read_packet,
  95. .flags = AVFMT_NOTIMESTAMPS,
  96. .priv_data_size = sizeof(MSPContext),
  97. };