FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/xbmdec.c
Date: 2023-12-07 21:54:23
Exec Total Coverage
Lines: 65 71 91.5%
Functions: 3 3 100.0%
Branches: 39 54 72.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/reverse.h"
24
25 #include "avcodec.h"
26 #include "codec_internal.h"
27 #include "decode.h"
28
29 1458922 static int get_nibble(uint8_t x)
30 {
31 1458922 int ret = 255;
32
33
2/2
✓ Branch 0 taken 1146321 times.
✓ Branch 1 taken 312601 times.
1458922 if (x <= '9') {
34
2/2
✓ Branch 0 taken 660035 times.
✓ Branch 1 taken 486286 times.
1146321 if (x >= '0')
35 660035 ret = x - '0';
36
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 312589 times.
312601 } else if (x >= 'a') {
37
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (x <= 'f')
38 12 ret = x - ('a' - 10);
39
2/4
✓ Branch 0 taken 312589 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 312589 times.
✗ Branch 3 not taken.
312589 } else if (x >= 'A' && x <= 'F')
40 312589 ret = x - ('A' - 10);
41 1458922 return ret;
42 }
43
44 90 static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
45 {
46 90 int keylen = strlen(key);
47 90 const uint8_t *e = end - keylen;
48
49
2/2
✓ Branch 0 taken 2382 times.
✓ Branch 1 taken 2 times.
2384 for(; p < e; p++) {
50
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 2294 times.
2382 if (!memcmp(p, key, keylen))
51 88 break;
52 }
53 90 p += keylen;
54
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 88 times.
90 if (p >= end)
55 2 return INT_MIN;
56
57
1/2
✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
88 for(; p<end; p++) {
58 char *eptr;
59 88 int64_t ret = strtol(p, &eptr, 10);
60
1/2
✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
88 if ((const uint8_t *)eptr != p)
61 88 return ret;
62 }
63 return INT_MIN;
64 }
65
66 45 static int xbm_decode_frame(AVCodecContext *avctx, AVFrame *p,
67 int *got_frame, AVPacket *avpkt)
68 {
69 int ret, linesize, i, j;
70 45 int width = 0;
71 45 int height = 0;
72 45 const uint8_t *end, *ptr = avpkt->data;
73 const uint8_t *next;
74 uint8_t *dst;
75
76 45 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
77 45 end = avpkt->data + avpkt->size;
78
79 45 width = parse_str_int(avpkt->data, end, "_width");
80 45 height = parse_str_int(avpkt->data, end, "_height");
81
82
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 44 times.
45 if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
83 1 return ret;
84
85
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 40 times.
44 if (avctx->skip_frame >= AVDISCARD_ALL)
86 4 return avpkt->size;
87
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
40 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
89 return ret;
90
91 // goto start of image data
92 40 next = memchr(ptr, '{', avpkt->size);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (!next)
94 next = memchr(ptr, '(', avpkt->size);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (!next)
96 return AVERROR_INVALIDDATA;
97 40 ptr = next + 1;
98
99 40 linesize = (avctx->width + 7) / 8;
100
2/2
✓ Branch 0 taken 11150 times.
✓ Branch 1 taken 40 times.
11190 for (i = 0; i < avctx->height; i++) {
101 11150 dst = p->data[0] + i * p->linesize[0];
102
2/2
✓ Branch 0 taken 486302 times.
✓ Branch 1 taken 11150 times.
497452 for (j = 0; j < linesize; j++) {
103 uint8_t nib, val;
104
105
4/6
✓ Branch 0 taken 1956306 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1470004 times.
✓ Branch 3 taken 486302 times.
✓ Branch 4 taken 1470004 times.
✗ Branch 5 not taken.
1956306 while (ptr < end && *ptr != 'x' && *ptr != '$')
106 1470004 ptr++;
107
108 486302 ptr ++;
109
2/4
✓ Branch 0 taken 486302 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 486302 times.
✗ Branch 4 not taken.
486302 if (ptr < end && (val = get_nibble(*ptr)) <= 15) {
110 486302 ptr++;
111
1/2
✓ Branch 1 taken 486302 times.
✗ Branch 2 not taken.
486302 if ((nib = get_nibble(*ptr)) <= 15) {
112 486302 val = (val << 4) + nib;
113 486302 ptr++;
114 }
115 486302 *dst++ = ff_reverse[val];
116
3/4
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 486286 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
486318 if ((val = get_nibble(*ptr)) <= 15 && j+1 < linesize) {
117 16 j++;
118 16 ptr++;
119
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 if ((nib = get_nibble(*ptr)) <= 15) {
120 16 val = (val << 4) + nib;
121 16 ptr++;
122 }
123 16 *dst++ = ff_reverse[val];
124 }
125 } else {
126 av_log(avctx, AV_LOG_ERROR,
127 "Unexpected data at %.8s.\n", ptr);
128 return AVERROR_INVALIDDATA;
129 }
130 }
131 }
132
133 40 p->flags |= AV_FRAME_FLAG_KEY;
134 40 p->pict_type = AV_PICTURE_TYPE_I;
135
136 40 *got_frame = 1;
137
138 40 return avpkt->size;
139 }
140
141 const FFCodec ff_xbm_decoder = {
142 .p.name = "xbm",
143 CODEC_LONG_NAME("XBM (X BitMap) image"),
144 .p.type = AVMEDIA_TYPE_VIDEO,
145 .p.id = AV_CODEC_ID_XBM,
146 .p.capabilities = AV_CODEC_CAP_DR1,
147 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
148 FF_CODEC_DECODE_CB(xbm_decode_frame),
149 };
150