FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/msp2dec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 38 0.0%
Functions: 0 1 0.0%
Branches: 0 20 0.0%

Line Branch Exec Source
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
32 static int msp2_decode_frame(AVCodecContext *avctx, AVFrame *p,
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
44 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
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) {
70 size = FFMIN(size, bytestream2_get_bytes_left(&gb));
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
92 const FFCodec ff_msp2_decoder = {
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,
98 FF_CODEC_DECODE_CB(msp2_decode_frame),
99 };
100