| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 "demux.h" | ||
| 31 | #include "internal.h" | ||
| 32 | |||
| 33 | typedef struct { | ||
| 34 | int packet_size; | ||
| 35 | } MSPContext; | ||
| 36 | |||
| 37 | 7474 | static int msp_probe(const AVProbeData *p) | |
| 38 | { | ||
| 39 | unsigned int i, sum; | ||
| 40 | |||
| 41 |
4/6✓ Branch 0 taken 7473 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7473 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7473 times.
✗ Branch 5 not taken.
|
7474 | if (p->buf_size <= 32 || (memcmp(p->buf, "DanM", 4) && memcmp(p->buf, "LinS", 4))) |
| 42 | 7474 | 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 | |||
| 51 | ✗ | static int msp_read_header(AVFormatContext *s) | |
| 52 | { | ||
| 53 | ✗ | MSPContext * cntx = s->priv_data; | |
| 54 | ✗ | AVIOContext *pb = s->pb; | |
| 55 | AVStream *st; | ||
| 56 | |||
| 57 | ✗ | st = avformat_new_stream(s, NULL); | |
| 58 | ✗ | if (!st) | |
| 59 | ✗ | return AVERROR(ENOMEM); | |
| 60 | |||
| 61 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 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); | |
| 66 | ✗ | st->codecpar->format = AV_PIX_FMT_MONOBLACK; | |
| 67 | |||
| 68 | ✗ | st->sample_aspect_ratio.num = avio_rl16(pb); | |
| 69 | ✗ | st->sample_aspect_ratio.den = avio_rl16(pb); | |
| 70 | ✗ | avio_skip(pb, 20); | |
| 71 | |||
| 72 | ✗ | if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) { | |
| 73 | ✗ | cntx->packet_size = av_image_get_buffer_size(st->codecpar->format, st->codecpar->width, st->codecpar->height, 1); | |
| 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 | |||
| 83 | ✗ | static int msp_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 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 | |||
| 93 | ✗ | if (st->codecpar->codec_id == AV_CODEC_ID_MSP2) { | |
| 94 | unsigned int size, i; | ||
| 95 | ✗ | if (pkt->size != 2 * st->codecpar->height) | |
| 96 | ✗ | return AVERROR_INVALIDDATA; | |
| 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 | |||
| 110 | const FFInputFormat ff_msp_demuxer = { | ||
| 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 | }; | ||
| 119 |