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 /**
23 * @file
24 * Microsoft Paint (MSP) demuxer
25 */
26
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/imgutils.h"
29 #include "avformat.h"
30 #include "internal.h"
31
32 typedef struct {
33 int packet_size;
34 } MSPContext;
35
msp_probe(const AVProbeData * p)36 static int msp_probe(const AVProbeData *p)
37 {
38 unsigned int i, sum;
39
40 if (p->buf_size <= 32 || (memcmp(p->buf, "DanM", 4) && memcmp(p->buf, "LinS", 4)))
41 return 0;
42
43 sum = 0;
44 for (i = 0; i < 24; i += 2)
45 sum ^= AV_RL16(p->buf + i);
46
47 return AV_RL16(p->buf + 24) == sum ? AVPROBE_SCORE_MAX : 0;
48 }
49
msp_read_header(AVFormatContext * s)50 static int msp_read_header(AVFormatContext *s)
51 {
52 MSPContext * cntx = s->priv_data;
53 AVIOContext *pb = s->pb;
54 AVStream *st;
55
56 st = avformat_new_stream(s, NULL);
57 if (!st)
58 return AVERROR(ENOMEM);
59
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;
62
63 st->codecpar->width = avio_rl16(pb);
64 st->codecpar->height = avio_rl16(pb);
65 st->codecpar->format = AV_PIX_FMT_MONOBLACK;
66
67 st->sample_aspect_ratio.num = avio_rl16(pb);
68 st->sample_aspect_ratio.den = avio_rl16(pb);
69 avio_skip(pb, 20);
70
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);
73 } else
74 cntx->packet_size = 2 * st->codecpar->height;
75
76 if (cntx->packet_size <= 0)
77 return cntx->packet_size < 0 ? cntx->packet_size : AVERROR_INVALIDDATA;
78
79 return 0;
80 }
81
msp_read_packet(AVFormatContext * s,AVPacket * pkt)82 static int msp_read_packet(AVFormatContext *s, AVPacket *pkt)
83 {
84 AVStream *st = s->streams[0];
85 MSPContext *cntx = s->priv_data;
86 int ret;
87
88 ret = av_get_packet(s->pb, pkt, cntx->packet_size);
89 if (ret < 0)
90 return ret;
91
92 if (st->codecpar->codec_id == AV_CODEC_ID_MSP2) {
93 unsigned int size, i;
94 if (pkt->size != 2 * st->codecpar->height)
95 return AVERROR_INVALIDDATA;
96 size = 0;
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);
100 if (ret < 0)
101 return ret;
102 }
103
104 pkt->stream_index = 0;
105 pkt->flags |= AV_PKT_FLAG_KEY;
106 return 0;
107 }
108
109 AVInputFormat ff_msp_demuxer = {
110 .name = "msp",
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),
117 };
118