GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/xbmdec.c Lines: 64 70 91.4 %
Date: 2021-04-15 16:04:23 Branches: 37 52 71.2 %

Line Branch Exec Source
1
/*
2
 * XBM image format
3
 *
4
 * Copyright (c) 2012 Paul B Mahol
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#include "libavutil/avstring.h"
24
25
#include "avcodec.h"
26
#include "internal.h"
27
#include "mathops.h"
28
29
560852
static int get_nibble(uint8_t x)
30
{
31
560852
    int ret = 255;
32
33
560852
    if (x <= '9') {
34
440193
        if (x >= '0')
35
253285
            ret = x - '0';
36
120659
    } else if (x >= 'a') {
37
24
        if (x <= 'f')
38
24
            ret = x - ('a' - 10);
39

120635
    } else if (x >= 'A' && x <= 'F')
40
120635
        ret = x - ('A' - 10);
41
560852
    return ret;
42
}
43
44
38
static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
45
{
46
38
    int keylen = strlen(key);
47
38
    const uint8_t *e = end - keylen;
48
49
1032
    for(; p < e; p++) {
50
1030
        if (!memcmp(p, key, keylen))
51
36
            break;
52
    }
53
38
    p += keylen;
54
38
    if (p >= end)
55
2
        return INT_MIN;
56
57
36
    for(; p<end; p++) {
58
        char *eptr;
59
36
        int64_t ret = strtol(p, &eptr, 10);
60
36
        if ((const uint8_t *)eptr != p)
61
36
            return ret;
62
    }
63
    return INT_MIN;
64
}
65
66
19
static int xbm_decode_frame(AVCodecContext *avctx, void *data,
67
                            int *got_frame, AVPacket *avpkt)
68
{
69
19
    AVFrame *p = data;
70
    int ret, linesize, i, j;
71
19
    int width  = 0;
72
19
    int height = 0;
73
19
    const uint8_t *end, *ptr = avpkt->data;
74
    const uint8_t *next;
75
    uint8_t *dst;
76
77
19
    avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
78
19
    end = avpkt->data + avpkt->size;
79
80
19
    width  = parse_str_int(avpkt->data, end, "_width");
81
19
    height = parse_str_int(avpkt->data, end, "_height");
82
83
19
    if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
84
1
        return ret;
85
86
18
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
87
        return ret;
88
89
    // goto start of image data
90
18
    next = memchr(ptr, '{', avpkt->size);
91
18
    if (!next)
92
        next = memchr(ptr, '(', avpkt->size);
93
18
    if (!next)
94
        return AVERROR_INVALIDDATA;
95
18
    ptr = next + 1;
96
97
18
    linesize = (avctx->width + 7) / 8;
98
4462
    for (i = 0; i < avctx->height; i++) {
99
4444
        dst = p->data[0] + i * p->linesize[0];
100
191384
        for (j = 0; j < linesize; j++) {
101
            uint8_t nib, val;
102
103

752162
            while (ptr < end && *ptr != 'x' && *ptr != '$')
104
565222
                ptr++;
105
106
186940
            ptr ++;
107

186940
            if (ptr < end && (val = get_nibble(*ptr)) <= 15) {
108
186940
                ptr++;
109
186940
                if ((nib = get_nibble(*ptr)) <= 15) {
110
186940
                    val = (val << 4) + nib;
111
186940
                    ptr++;
112
                }
113
186940
                *dst++ = ff_reverse[val];
114

186940
                if ((val = get_nibble(*ptr)) <= 15 && j+1 < linesize) {
115
32
                    j++;
116
32
                    ptr++;
117
32
                    if ((nib = get_nibble(*ptr)) <= 15) {
118
32
                        val = (val << 4) + nib;
119
32
                        ptr++;
120
                    }
121
32
                    *dst++ = ff_reverse[val];
122
                }
123
            } else {
124
                av_log(avctx, AV_LOG_ERROR,
125
                       "Unexpected data at %.8s.\n", ptr);
126
                return AVERROR_INVALIDDATA;
127
            }
128
        }
129
    }
130
131
18
    p->key_frame = 1;
132
18
    p->pict_type = AV_PICTURE_TYPE_I;
133
134
18
    *got_frame       = 1;
135
136
18
    return avpkt->size;
137
}
138
139
AVCodec ff_xbm_decoder = {
140
    .name         = "xbm",
141
    .long_name    = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
142
    .type         = AVMEDIA_TYPE_VIDEO,
143
    .id           = AV_CODEC_ID_XBM,
144
    .decode       = xbm_decode_frame,
145
    .capabilities = AV_CODEC_CAP_DR1,
146
};