FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cdxl.c
Date: 2026-10-06 20:08:58
Exec Total Coverage
Lines: 160 199 80.4%
Functions: 10 12 83.3%
Branches: 72 118 61.0%

Line Branch Exec Source
1 /*
2 * CDXL video decoder
3 * Copyright (c) 2011-2012 Paul B Mahol
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 * Commodore CDXL video decoder
25 * @author Paul B Mahol
26 */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mem.h"
32 #include "avcodec.h"
33 #include "bytestream.h"
34 #include "codec_internal.h"
35 #include "decode.h"
36 #include "get_bits.h"
37
38 #define BIT_PLANAR 0x00
39 #define CHUNKY 0x20
40 #define BYTE_PLANAR 0x40
41 #define BIT_LINE 0x80
42 #define BYTE_LINE 0xC0
43
44 typedef struct CDXLVideoContext {
45 AVCodecContext *avctx;
46 int bpp;
47 int type;
48 int format;
49 int padded_bits;
50 const uint8_t *palette;
51 int palette_size;
52 const uint8_t *video;
53 int video_size;
54 uint8_t *new_video;
55 int new_video_size;
56 } CDXLVideoContext;
57
58 11 static av_cold int cdxl_decode_init(AVCodecContext *avctx)
59 {
60 11 CDXLVideoContext *c = avctx->priv_data;
61
62 11 c->new_video_size = 0;
63 11 c->avctx = avctx;
64
65 11 return 0;
66 }
67
68 92 static int import_palette(CDXLVideoContext *c, uint32_t *new_palette)
69 {
70
1/2
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
92 if (c->type == 1) {
71
2/2
✓ Branch 0 taken 4592 times.
✓ Branch 1 taken 92 times.
4684 for (int i = 0; i < c->palette_size / 2; i++) {
72 4592 unsigned rgb = AV_RB16(&c->palette[i * 2]);
73 4592 unsigned r = ((rgb >> 8) & 0xF) * 0x11;
74 4592 unsigned g = ((rgb >> 4) & 0xF) * 0x11;
75 4592 unsigned b = (rgb & 0xF) * 0x11;
76 4592 AV_WN32(&new_palette[i], (0xFFU << 24) | (r << 16) | (g << 8) | b);
77 }
78 92 return c->palette_size / 2;
79 } else {
80 ✗ for (int i = 0; i < c->palette_size / 3; i++) {
81 ✗ unsigned rgb = AV_RB24(&c->palette[i * 3]);
82 ✗ AV_WN32(&new_palette[i], (0xFFU << 24) | rgb);
83 }
84 ✗ return c->palette_size / 3;
85 }
86 }
87
88 81 static void bitplanar2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
89 {
90 GetBitContext gb;
91 int x, y, plane;
92
93
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
81 if (init_get_bits8(&gb, c->video, c->video_size) < 0)
94 ✗ return;
95
2/2
✓ Branch 0 taken 426 times.
✓ Branch 1 taken 81 times.
507 for (plane = 0; plane < c->bpp; plane++) {
96
2/2
✓ Branch 0 taken 44688 times.
✓ Branch 1 taken 426 times.
45114 for (y = 0; y < c->avctx->height; y++) {
97
2/2
✓ Branch 0 taken 6947328 times.
✓ Branch 1 taken 44688 times.
6992016 for (x = 0; x < c->avctx->width; x++)
98 6947328 out[linesize * y + x] |= get_bits1(&gb) << plane;
99 44688 skip_bits(&gb, c->padded_bits);
100 }
101 }
102 }
103
104 11 static void bitline2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
105 {
106 GetBitContext gb;
107 int x, y, plane;
108
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 if (init_get_bits8(&gb, c->video, c->video_size) < 0)
110 ✗ return;
111
2/2
✓ Branch 0 taken 1430 times.
✓ Branch 1 taken 11 times.
1441 for (y = 0; y < c->avctx->height; y++) {
112
2/2
✓ Branch 0 taken 8580 times.
✓ Branch 1 taken 1430 times.
10010 for (plane = 0; plane < c->bpp; plane++) {
113
2/2
✓ Branch 0 taken 1389960 times.
✓ Branch 1 taken 8580 times.
1398540 for (x = 0; x < c->avctx->width; x++)
114 1389960 out[linesize * y + x] |= get_bits1(&gb) << plane;
115 8580 skip_bits(&gb, c->padded_bits);
116 }
117 }
118 }
119
120 ✗ static void chunky2chunky(CDXLVideoContext *c, int linesize, uint8_t *out)
121 {
122 GetByteContext gb;
123 int y;
124
125 ✗ bytestream2_init(&gb, c->video, c->video_size);
126 ✗ for (y = 0; y < c->avctx->height; y++) {
127 ✗ bytestream2_get_buffer(&gb, out + linesize * y, c->avctx->width * 3);
128 }
129 ✗ }
130
131 92 static void import_format(CDXLVideoContext *c, ptrdiff_t linesize, uint8_t *out)
132 {
133
2/2
✓ Branch 0 taken 9406 times.
✓ Branch 1 taken 92 times.
9498 for (int y = 0; y < c->avctx->height; y++)
134 9406 memset(out + y * linesize, 0, c->avctx->width);
135
136
2/4
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
92 switch (c->format) {
137 81 case BIT_PLANAR:
138 81 bitplanar2chunky(c, linesize, out);
139 81 break;
140 11 case BIT_LINE:
141 11 bitline2chunky(c, linesize, out);
142 11 break;
143 ✗ case CHUNKY:
144 ✗ chunky2chunky(c, linesize, out);
145 ✗ break;
146 }
147 92 }
148
149 59 static void cdxl_decode_rgb(CDXLVideoContext *c, AVFrame *frame)
150 {
151 59 uint32_t *new_palette = (uint32_t *)frame->data[1];
152
153 59 memset(frame->data[1], 0, AVPALETTE_SIZE);
154 59 import_palette(c, new_palette);
155 59 import_format(c, frame->linesize[0], frame->data[0]);
156 59 }
157
158 ✗ static void cdxl_decode_raw(CDXLVideoContext *c, AVFrame *frame)
159 {
160 ✗ import_format(c, frame->linesize[0], frame->data[0]);
161 ✗ }
162
163 28 static int cdxl_decode_ham6(CDXLVideoContext *c, AVFrame *frame)
164 {
165 28 AVCodecContext *avctx = c->avctx;
166 uint32_t new_palette[16], r, g, b;
167 uint8_t *ptr, *out, index, op;
168 int x, y, nb_colors;
169
170 28 ptr = c->new_video;
171 28 out = frame->data[0];
172
173 28 nb_colors = import_palette(c, new_palette);
174 28 import_format(c, avctx->width, c->new_video);
175
176
2/2
✓ Branch 0 taken 3470 times.
✓ Branch 1 taken 28 times.
3498 for (y = 0; y < avctx->height; y++) {
177 3470 r = new_palette[0] & 0xFF0000;
178 3470 g = new_palette[0] & 0xFF00;
179 3470 b = new_palette[0] & 0xFF;
180
2/2
✓ Branch 0 taken 558060 times.
✓ Branch 1 taken 3470 times.
561530 for (x = 0; x < avctx->width; x++) {
181 558060 index = *ptr++;
182 558060 op = index >> 4;
183 558060 index &= 15;
184
4/5
✓ Branch 0 taken 131279 times.
✓ Branch 1 taken 108493 times.
✓ Branch 2 taken 212370 times.
✓ Branch 3 taken 105918 times.
✗ Branch 4 not taken.
558060 switch (op) {
185 131279 case 0:
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131279 times.
131279 if (index >= nb_colors)
187 ✗ return AVERROR_INVALIDDATA;
188 131279 r = new_palette[index] & 0xFF0000;
189 131279 g = new_palette[index] & 0xFF00;
190 131279 b = new_palette[index] & 0xFF;
191 131279 break;
192 108493 case 1:
193 108493 b = index * 0x11;
194 108493 break;
195 212370 case 2:
196 212370 r = index * 0x11 << 16;
197 212370 break;
198 105918 case 3:
199 105918 g = index * 0x11 << 8;
200 105918 break;
201 }
202 558060 AV_WL24(out + x * 3, r | g | b);
203 }
204 3470 out += frame->linesize[0];
205 }
206 28 return 0;
207 }
208
209 5 static int cdxl_decode_ham8(CDXLVideoContext *c, AVFrame *frame)
210 {
211 5 AVCodecContext *avctx = c->avctx;
212 uint32_t new_palette[64], r, g, b;
213 uint8_t *ptr, *out, index, op;
214 int x, y, nb_colors;
215
216 5 ptr = c->new_video;
217 5 out = frame->data[0];
218
219 5 nb_colors = import_palette(c, new_palette);
220 5 import_format(c, avctx->width, c->new_video);
221
222
2/2
✓ Branch 0 taken 640 times.
✓ Branch 1 taken 5 times.
645 for (y = 0; y < avctx->height; y++) {
223 640 r = new_palette[0] & 0xFF0000;
224 640 g = new_palette[0] & 0xFF00;
225 640 b = new_palette[0] & 0xFF;
226
2/2
✓ Branch 0 taken 112640 times.
✓ Branch 1 taken 640 times.
113280 for (x = 0; x < avctx->width; x++) {
227 112640 index = *ptr++;
228 112640 op = index >> 6;
229 112640 index &= 63;
230
4/5
✓ Branch 0 taken 5695 times.
✓ Branch 1 taken 1045 times.
✓ Branch 2 taken 1870 times.
✓ Branch 3 taken 104030 times.
✗ Branch 4 not taken.
112640 switch (op) {
231 5695 case 0:
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5695 times.
5695 if (index >= nb_colors)
233 ✗ return AVERROR_INVALIDDATA;
234 5695 r = new_palette[index] & 0xFF0000;
235 5695 g = new_palette[index] & 0xFF00;
236 5695 b = new_palette[index] & 0xFF;
237 5695 break;
238 1045 case 1:
239 1045 b = (index << 2) | (b & 3);
240 1045 break;
241 1870 case 2:
242 1870 r = (index << 18) | (r & (3 << 16));
243 1870 break;
244 104030 case 3:
245 104030 g = (index << 10) | (g & (3 << 8));
246 104030 break;
247 }
248 112640 AV_WL24(out + x * 3, r | g | b);
249 }
250 640 out += frame->linesize[0];
251 }
252 5 return 0;
253 }
254
255 95 static int cdxl_decode_frame(AVCodecContext *avctx, AVFrame *p,
256 int *got_frame, AVPacket *pkt)
257 {
258 95 CDXLVideoContext *c = avctx->priv_data;
259 95 int ret, w, h, encoding, aligned_width, buf_size = pkt->size;
260 95 const uint8_t *buf = pkt->data;
261
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (buf_size < 32)
263 ✗ return AVERROR_INVALIDDATA;
264 95 c->type = buf[0];
265 95 encoding = buf[1] & 7;
266 95 c->format = buf[1] & 0xE0;
267 95 w = AV_RB16(&buf[14]);
268 95 h = AV_RB16(&buf[16]);
269 95 c->bpp = buf[19];
270 95 c->palette_size = AV_RB16(&buf[20]);
271 95 c->palette = buf + 32;
272 95 c->video = c->palette + c->palette_size;
273 95 c->video_size = buf_size - c->palette_size - 32;
274
275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (c->type > 1)
276 ✗ return AVERROR_INVALIDDATA;
277
2/4
✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 95 times.
95 if (c->type == 1 && c->palette_size > 512)
278 ✗ return AVERROR_INVALIDDATA;
279
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
95 if (c->type == 0 && c->palette_size > 768)
280 ✗ return AVERROR_INVALIDDATA;
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (buf_size < c->palette_size + 32)
282 ✗ return AVERROR_INVALIDDATA;
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (c->bpp < 1)
284 ✗ return AVERROR_INVALIDDATA;
285
3/6
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
95 if (c->format != BIT_PLANAR && c->format != BIT_LINE && c->format != CHUNKY) {
286 ✗ avpriv_request_sample(avctx, "Pixel format 0x%0x", c->format);
287 ✗ return AVERROR_PATCHWELCOME;
288 }
289
290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 95 times.
95 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
291 ✗ return ret;
292
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (c->format == CHUNKY)
294 ✗ aligned_width = avctx->width;
295 else
296 95 aligned_width = FFALIGN(c->avctx->width, 16);
297 95 c->padded_bits = aligned_width - c->avctx->width;
298
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 92 times.
95 if (c->video_size < aligned_width * avctx->height * (int64_t)c->bpp / 8)
299 3 return AVERROR_INVALIDDATA;
300
5/8
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 59 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 59 times.
✗ Branch 7 not taken.
92 if (!encoding && c->palette_size && c->bpp <= 8 && c->format != CHUNKY) {
301 59 avctx->pix_fmt = AV_PIX_FMT_PAL8;
302
5/8
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
33 } else if (encoding == 1 && (c->bpp == 6 || c->bpp == 8) && c->format != CHUNKY) {
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (c->palette_size != (1 << (c->bpp - 1)))
304 ✗ return AVERROR_INVALIDDATA;
305 33 avctx->pix_fmt = AV_PIX_FMT_BGR24;
306 ✗ } else if (!encoding && c->bpp == 24 && c->format == CHUNKY &&
307 ✗ !c->palette_size) {
308 ✗ avctx->pix_fmt = AV_PIX_FMT_RGB24;
309 } else {
310 ✗ avpriv_request_sample(avctx, "Encoding %d, bpp %d and format 0x%x",
311 encoding, c->bpp, c->format);
312 ✗ return AVERROR_PATCHWELCOME;
313 }
314
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
92 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
316 ✗ return ret;
317
318
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 59 times.
92 if (encoding) {
319 33 av_fast_padded_malloc(&c->new_video, &c->new_video_size,
320 33 h * w + AV_INPUT_BUFFER_PADDING_SIZE);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (!c->new_video)
322 ✗ return AVERROR(ENOMEM);
323
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 28 times.
33 if (c->bpp == 8)
324 5 ret = cdxl_decode_ham8(c, p);
325 else
326 28 ret = cdxl_decode_ham6(c, p);
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
328 ✗ return ret;
329
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
59 } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
330 59 cdxl_decode_rgb(c, p);
331 } else {
332 ✗ cdxl_decode_raw(c, p);
333 }
334 92 *got_frame = 1;
335
336 92 return buf_size;
337 }
338
339 11 static av_cold int cdxl_decode_end(AVCodecContext *avctx)
340 {
341 11 CDXLVideoContext *c = avctx->priv_data;
342
343 11 av_freep(&c->new_video);
344
345 11 return 0;
346 }
347
348 const FFCodec ff_cdxl_decoder = {
349 .p.name = "cdxl",
350 CODEC_LONG_NAME("Commodore CDXL video"),
351 .p.type = AVMEDIA_TYPE_VIDEO,
352 .p.id = AV_CODEC_ID_CDXL,
353 .priv_data_size = sizeof(CDXLVideoContext),
354 .init = cdxl_decode_init,
355 .close = cdxl_decode_end,
356 FF_CODEC_DECODE_CB(cdxl_decode_frame),
357 .p.capabilities = AV_CODEC_CAP_DR1,
358 };
359