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 "codec_internal.h"
30 #include "decode.h"
31 
33  int *got_frame, AVPacket *avpkt)
34 {
35  const uint8_t *buf = avpkt->data;
36  int buf_size = avpkt->size;
37  int ret;
38  unsigned int x, y, width = (avctx->width + 7) / 8;
39  GetByteContext idx, gb;
40 
41  if (buf_size <= 2 * avctx->height)
42  return AVERROR_INVALIDDATA;
43 
45 
46  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
47  return ret;
48 
49  bytestream2_init(&idx, buf, 2 * avctx->height);
50  buf += 2 * avctx->height;
51  buf_size -= 2 * avctx->height;
52 
53  for (y = 0; y < avctx->height; y++) {
54  unsigned int pkt_size = bytestream2_get_le16(&idx);
55  if (!pkt_size) {
56  memset(p->data[0] + y * p->linesize[0], 0xFF, width);
57  continue;
58  }
59 
60  if (pkt_size > buf_size) {
61  av_log(avctx, AV_LOG_WARNING, "image probably corrupt\n");
62  pkt_size = buf_size;
63  }
64 
65  bytestream2_init(&gb, buf, pkt_size);
66  x = 0;
67  while (bytestream2_get_bytes_left(&gb) && x < width) {
68  int size = bytestream2_get_byte(&gb);
69  if (size) {
71  memcpy(p->data[0] + y * p->linesize[0] + x, gb.buffer, FFMIN(size, width - x));
72  bytestream2_skip(&gb, size);
73  } else {
74  int value;
75  size = bytestream2_get_byte(&gb);
76  if (!size)
77  avpriv_request_sample(avctx, "escape value");
78  value = bytestream2_get_byte(&gb);
79  memset(p->data[0] + y * p->linesize[0] + x, value, FFMIN(size, width - x));
80  }
81  x += size;
82  }
83 
84  buf += pkt_size;
85  buf_size -= pkt_size;
86  }
87 
88  *got_frame = 1;
89  return buf_size;
90 }
91 
93  .p.name = "msp2",
94  CODEC_LONG_NAME("Microsoft Paint (MSP) version 2"),
95  .p.type = AVMEDIA_TYPE_VIDEO,
96  .p.id = AV_CODEC_ID_MSP2,
97  .p.capabilities = AV_CODEC_CAP_DR1,
99 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
GetByteContext
Definition: bytestream.h:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVPacket::data
uint8_t * data
Definition: packet.h:524
FFCodec
Definition: codec_internal.h:126
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:395
ff_msp2_decoder
const FFCodec ff_msp2_decoder
Definition: msp2dec.c:92
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
decode.h
AV_CODEC_ID_MSP2
@ AV_CODEC_ID_MSP2
Definition: codec_id.h:249
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:271
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:83
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1575
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:525
codec_internal.h
size
int size
Definition: twinvq_data.h:10344
msp2_decode_frame
static int msp2_decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: msp2dec.c:32
height
#define height
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
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
AVPacket
This structure stores compressed data.
Definition: packet.h:501
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
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, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:419
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61