]> git.sesse.net Git - ffmpeg/blob - libavformat/mspdec.c
avformat/mspdec: Microsoft Paint (MSP) demuxer
[ffmpeg] / libavformat / mspdec.c
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
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
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         if (cntx->packet_size < 0)
74             return cntx->packet_size;
75     } else
76         cntx->packet_size = 2 * st->codecpar->height;
77
78     return 0;
79 }
80
81 static int msp_read_packet(AVFormatContext *s, AVPacket *pkt)
82 {
83     AVStream *st = s->streams[0];
84     MSPContext *cntx = s->priv_data;
85     int ret;
86
87     ret = av_get_packet(s->pb, pkt, cntx->packet_size);
88     if (ret < 0)
89         return ret;
90
91     if (st->codecpar->codec_id == AV_CODEC_ID_MSP2) {
92         unsigned int size, i;
93         if (pkt->size != 2 * st->codecpar->height)
94             return AVERROR_INVALIDDATA;
95         size = 0;
96         for (i = 0; i < st->codecpar->height; i++)
97              size += AV_RL16(&pkt->data[i * 2]);
98         ret = av_append_packet(s->pb, pkt, size);
99         if (ret < 0)
100             return ret;
101     }
102
103     pkt->stream_index = 0;
104     pkt->flags |= AV_PKT_FLAG_KEY;
105     return 0;
106 }
107
108 AVInputFormat ff_msp_demuxer = {
109     .name         = "msp",
110     .long_name    = NULL_IF_CONFIG_SMALL("Microsoft Paint (MSP))"),
111     .read_probe   = msp_probe,
112     .read_header  = msp_read_header,
113     .read_packet  = msp_read_packet,
114     .flags        = AVFMT_NOTIMESTAMPS,
115     .priv_data_size = sizeof(MSPContext),
116 };