2 * Microsoft Paint (MSP) demuxer
3 * Copyright (c) 2020 Peter Ross (pross@xvid.org)
5 * This file is part of FFmpeg.
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.
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.
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
24 * Microsoft Paint (MSP) demuxer
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/imgutils.h"
36 static int msp_probe(const AVProbeData *p)
40 if (p->buf_size <= 32 || (memcmp(p->buf, "DanM", 4) && memcmp(p->buf, "LinS", 4)))
44 for (i = 0; i < 24; i += 2)
45 sum ^= AV_RL16(p->buf + i);
47 return AV_RL16(p->buf + 24) == sum ? AVPROBE_SCORE_MAX : 0;
50 static int msp_read_header(AVFormatContext *s)
52 MSPContext * cntx = s->priv_data;
53 AVIOContext *pb = s->pb;
56 st = avformat_new_stream(s, NULL);
58 return AVERROR(ENOMEM);
60 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
61 st->codecpar->codec_id = avio_rl32(pb) == MKTAG('D', 'a', 'n', 'M') ? AV_CODEC_ID_RAWVIDEO : AV_CODEC_ID_MSP2;
63 st->codecpar->width = avio_rl16(pb);
64 st->codecpar->height = avio_rl16(pb);
65 st->codecpar->format = AV_PIX_FMT_MONOBLACK;
67 st->sample_aspect_ratio.num = avio_rl16(pb);
68 st->sample_aspect_ratio.den = avio_rl16(pb);
71 if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) {
72 cntx->packet_size = av_image_get_buffer_size(st->codecpar->format, st->codecpar->width, st->codecpar->height, 1);
74 cntx->packet_size = 2 * st->codecpar->height;
76 if (cntx->packet_size <= 0)
77 return cntx->packet_size < 0 ? cntx->packet_size : AVERROR_INVALIDDATA;
82 static int msp_read_packet(AVFormatContext *s, AVPacket *pkt)
84 AVStream *st = s->streams[0];
85 MSPContext *cntx = s->priv_data;
88 ret = av_get_packet(s->pb, pkt, cntx->packet_size);
92 if (st->codecpar->codec_id == AV_CODEC_ID_MSP2) {
94 if (pkt->size != 2 * st->codecpar->height)
95 return AVERROR_INVALIDDATA;
97 for (i = 0; i < st->codecpar->height; i++)
98 size += AV_RL16(&pkt->data[i * 2]);
99 ret = av_append_packet(s->pb, pkt, size);
104 pkt->stream_index = 0;
105 pkt->flags |= AV_PKT_FLAG_KEY;
109 const AVInputFormat ff_msp_demuxer = {
111 .long_name = NULL_IF_CONFIG_SMALL("Microsoft Paint (MSP))"),
112 .read_probe = msp_probe,
113 .read_header = msp_read_header,
114 .read_packet = msp_read_packet,
115 .flags = AVFMT_NOTIMESTAMPS,
116 .priv_data_size = sizeof(MSPContext),