FFmpeg
Loading...
Searching...
No Matches
mspdec.c
Go to the documentation of this file.
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
28#include "libavutil/imgutils.h"
29#include "avformat.h"
30#include "demux.h"
31#include "internal.h"
32
33typedef struct {
36
37static int msp_probe(const AVProbeData *p)
38{
39 unsigned int i, sum;
40
41 if (p->buf_size <= 32 || (memcmp(p->buf, "DanM", 4) && memcmp(p->buf, "LinS", 4)))
42 return 0;
43
44 sum = 0;
45 for (i = 0; i < 24; i += 2)
46 sum ^= AV_RL16(p->buf + i);
47
48 return AV_RL16(p->buf + 24) == sum ? AVPROBE_SCORE_MAX : 0;
49}
50
52{
53 MSPContext * cntx = s->priv_data;
54 AVIOContext *pb = s->pb;
55 AVStream *st;
56
58 if (!st)
59 return AVERROR(ENOMEM);
60
62 st->codecpar->codec_id = avio_rl32(pb) == MKTAG('D', 'a', 'n', 'M') ? AV_CODEC_ID_RAWVIDEO : AV_CODEC_ID_MSP2;
63
64 st->codecpar->width = avio_rl16(pb);
65 st->codecpar->height = avio_rl16(pb);
67
70 avio_skip(pb, 20);
71
74 } else
75 cntx->packet_size = 2 * st->codecpar->height;
76
77 if (cntx->packet_size <= 0)
78 return cntx->packet_size < 0 ? cntx->packet_size : AVERROR_INVALIDDATA;
79
80 return 0;
81}
82
84{
85 AVStream *st = s->streams[0];
86 MSPContext *cntx = s->priv_data;
87 int ret;
88
89 ret = av_get_packet(s->pb, pkt, cntx->packet_size);
90 if (ret < 0)
91 return ret;
92
94 unsigned int size, i;
95 if (pkt->size != 2 * st->codecpar->height)
97 size = 0;
98 for (i = 0; i < st->codecpar->height; i++)
99 size += AV_RL16(&pkt->data[i * 2]);
100 ret = av_append_packet(s->pb, pkt, size);
101 if (ret < 0)
102 return ret;
103 }
104
105 pkt->stream_index = 0;
106 pkt->flags |= AV_PKT_FLAG_KEY;
107 return 0;
108}
109
111 .p.name = "msp",
112 .p.long_name = NULL_IF_CONFIG_SMALL("Microsoft Paint (MSP))"),
113 .p.flags = AVFMT_NOTIMESTAMPS,
114 .read_probe = msp_probe,
115 .read_header = msp_read_header,
116 .read_packet = msp_read_packet,
117 .priv_data_size = sizeof(MSPContext),
118};
const FFInputFormat ff_msp_demuxer
Definition mspdec.c:110
Main libavformat public API header.
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition utils.c:114
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static AVPacket * pkt
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_MSP2
Definition codec_id.h:246
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters.
Definition imgutils.c:466
misc image utilities
#define AV_RL16(p)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define MKTAG(a, b, c, d)
Definition macros.h:55
static int msp_read_header(AVFormatContext *s)
Definition mspdec.c:51
static int msp_probe(const AVProbeData *p)
Definition mspdec.c:37
static int msp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition mspdec.c:83
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:83
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:844
int packet_size
Definition mspdec.c:34
int size