FFmpeg
msp2dec.c
Go to the documentation of this file.
1 /*
2  * Microsoft Paint (MSP) version 2 decoder
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) version 2 decoder
25  */
26 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30 
32  void *data, int *got_frame,
33  AVPacket *avpkt)
34 {
35  const uint8_t *buf = avpkt->data;
36  int buf_size = avpkt->size;
37  AVFrame *p = data;
38  int ret;
39  unsigned int x, y, width = (avctx->width + 7) / 8;
40  GetByteContext idx, gb;
41 
42  if (buf_size <= 2 * avctx->height)
43  return AVERROR_INVALIDDATA;
44 
46 
47  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
48  return ret;
49 
51  p->key_frame = 1;
52 
53  bytestream2_init(&idx, buf, 2 * avctx->height);
54  buf += 2 * avctx->height;
55  buf_size -= 2 * avctx->height;
56 
57  for (y = 0; y < avctx->height; y++) {
58  unsigned int pkt_size = bytestream2_get_le16(&idx);
59  if (!pkt_size) {
60  memset(p->data[0] + y * p->linesize[0], 0xFF, width);
61  continue;
62  }
63 
64  if (pkt_size > buf_size) {
65  av_log(avctx, AV_LOG_WARNING, "image probably corrupt\n");
66  pkt_size = buf_size;
67  }
68 
69  bytestream2_init(&gb, buf, pkt_size);
70  x = 0;
71  while (bytestream2_get_bytes_left(&gb) && x < width) {
72  int size = bytestream2_get_byte(&gb);
73  if (size) {
75  memcpy(p->data[0] + y * p->linesize[0] + x, gb.buffer, FFMIN(size, width - x));
76  bytestream2_skip(&gb, size);
77  } else {
78  int value;
79  size = bytestream2_get_byte(&gb);
80  if (!size)
81  avpriv_request_sample(avctx, "escape value");
82  value = bytestream2_get_byte(&gb);
83  memset(p->data[0] + y * p->linesize[0] + x, value, FFMIN(size, width - x));
84  }
85  x += size;
86  }
87 
88  buf += pkt_size;
89  buf_size -= pkt_size;
90  }
91 
92  *got_frame = 1;
93  return buf_size;
94 }
95 
97  .name = "msp2",
98  .long_name = NULL_IF_CONFIG_SMALL("Microsoft Paint (MSP) version 2"),
99  .type = AVMEDIA_TYPE_VIDEO,
100  .id = AV_CODEC_ID_MSP2,
101  .decode = msp2_decode_frame,
102  .capabilities = AV_CODEC_CAP_DR1,
103 };
AVCodec
AVCodec.
Definition: codec.h:197
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:187
GetByteContext
Definition: bytestream.h:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:365
data
const char data[16]
Definition: mxf.c:142
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:381
width
#define width
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
AV_CODEC_ID_MSP2
@ AV_CODEC_ID_MSP2
Definition: codec_id.h:247
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
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
msp2_decode_frame
static int msp2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: msp2dec.c:31
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:386
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1638
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:366
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
height
#define height
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
ff_msp2_decoder
const AVCodec ff_msp2_decoder
Definition: msp2dec.c:96
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
AVCodecContext::height
int height
Definition: avcodec.h:557
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:594
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:384
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:39
AVPacket
This structure stores compressed data.
Definition: packet.h:342
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:557
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:334
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59