FFmpeg
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 
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/imgutils.h"
29 #include "avformat.h"
30 #include "internal.h"
31 
32 typedef struct {
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 
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 
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);
66 
69  avio_skip(pb, 20);
70 
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 
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;
106  return 0;
107 }
108 
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 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:475
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:63
MSPContext::packet_size
int packet_size
Definition: mspdec.c:33
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:450
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:428
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:650
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
msp_probe
static int msp_probe(const AVProbeData *p)
Definition: mspdec.c:36
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
AV_CODEC_ID_MSP2
@ AV_CODEC_ID_MSP2
Definition: codec_id.h:247
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
NULL
#define NULL
Definition: coverity.c:32
AV_PIX_FMT_MONOBLACK
@ 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:76
msp_read_header
static int msp_read_header(AVFormatContext *s)
Definition: mspdec.c:50
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
size
int size
Definition: twinvq_data.h:10344
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:1004
av_image_get_buffer_size
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
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVCodecParameters::height
int height
Definition: codec_par.h:127
MSPContext
Definition: mspdec.c:32
msp_read_packet
static int msp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mspdec.c:82
ff_msp_demuxer
const AVInputFormat ff_msp_demuxer
Definition: mspdec.c:109
av_get_packet
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:197
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
av_append_packet
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:213
avformat.h
AVRational::den
int den
Denominator.
Definition: rational.h:60
AVPacket::stream_index
int stream_index
Definition: packet.h:375
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:347
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
imgutils.h
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55