FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cdgraphics.c
Date: 2025-11-10 16:28:23
Exec Total Coverage
Lines: 100 189 52.9%
Functions: 6 11 54.5%
Branches: 48 105 45.7%

Line Branch Exec Source
1 /*
2 * CD Graphics Video Decoder
3 * Copyright (c) 2009 Michael Tison
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 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "codec_internal.h"
25 #include "decode.h"
26
27 #include "libavutil/attributes.h"
28
29 /**
30 * @file
31 * @brief CD Graphics Video Decoder
32 * @author Michael Tison
33 * @see http://wiki.multimedia.cx/index.php?title=CD_Graphics
34 * @see http://www.ccs.neu.edu/home/bchafy/cdb/info/cdg
35 */
36
37 /// default screen sizes
38 #define CDG_FULL_WIDTH 300
39 #define CDG_FULL_HEIGHT 216
40 #define CDG_DISPLAY_WIDTH 294
41 #define CDG_DISPLAY_HEIGHT 204
42 #define CDG_BORDER_WIDTH 6
43 #define CDG_BORDER_HEIGHT 12
44
45 /// masks
46 #define CDG_COMMAND 0x09
47 #define CDG_MASK 0x3F
48
49 /// instruction codes
50 #define CDG_INST_MEMORY_PRESET 1
51 #define CDG_INST_BORDER_PRESET 2
52 #define CDG_INST_TILE_BLOCK 6
53 #define CDG_INST_SCROLL_PRESET 20
54 #define CDG_INST_SCROLL_COPY 24
55 #define CDG_INST_TRANSPARENT_COL 28
56 #define CDG_INST_LOAD_PAL_LO 30
57 #define CDG_INST_LOAD_PAL_HIGH 31
58 #define CDG_INST_TILE_BLOCK_XOR 38
59
60 /// data sizes
61 #define CDG_PACKET_SIZE 24
62 #define CDG_DATA_SIZE 16
63 #define CDG_TILE_HEIGHT 12
64 #define CDG_TILE_WIDTH 6
65 #define CDG_MINIMUM_PKT_SIZE 6
66 #define CDG_MINIMUM_SCROLL_SIZE 3
67 #define CDG_HEADER_SIZE 8
68 #define CDG_PALETTE_SIZE 16
69
70 typedef struct CDGraphicsContext {
71 AVFrame *frame;
72 int hscroll;
73 int vscroll;
74 uint8_t alpha[CDG_PALETTE_SIZE];
75 int cleared;
76 } CDGraphicsContext;
77
78 2 static av_cold int cdg_decode_init(AVCodecContext *avctx)
79 {
80 2 CDGraphicsContext *cc = avctx->priv_data;
81
82 2 cc->frame = av_frame_alloc();
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!cc->frame)
84 return AVERROR(ENOMEM);
85
86
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2 times.
34 for (int i = 0; i < CDG_PALETTE_SIZE; i++)
87 32 cc->alpha[i] = 0xFFU;
88
89 2 avctx->pix_fmt = AV_PIX_FMT_PAL8;
90 2 return ff_set_dimensions(avctx, CDG_FULL_WIDTH, CDG_FULL_HEIGHT);
91 }
92
93 6 static void cdg_border_preset(CDGraphicsContext *cc, uint8_t *data)
94 {
95 6 ptrdiff_t lsize = cc->frame->linesize[0];
96 6 uint8_t *buf = cc->frame->data[0];
97 6 int color = data[0] & 0x0F;
98
99
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!(data[1] & 0x0F)) {
100 /// fill the top and bottom borders
101
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 6 times.
78 for (int y = 0; y < CDG_BORDER_HEIGHT; y++)
102 72 memset(buf + y * lsize, color, cc->frame->width);
103
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 6 times.
78 for (int y = CDG_FULL_HEIGHT-CDG_BORDER_HEIGHT; y < CDG_FULL_HEIGHT; y++)
104 72 memset(buf + y * lsize, color, cc->frame->width);
105
106 /// fill the side borders
107
2/2
✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 6 times.
1158 for (int y = CDG_BORDER_HEIGHT; y < CDG_FULL_HEIGHT - CDG_BORDER_HEIGHT; y++) {
108 1152 memset(buf + y * lsize, color, CDG_BORDER_WIDTH);
109 1152 memset(buf + CDG_FULL_WIDTH - CDG_BORDER_WIDTH + y * lsize,
110 color, CDG_BORDER_WIDTH);
111 }
112 }
113 6 }
114
115 10 static void cdg_load_palette(CDGraphicsContext *cc, uint8_t *data, int low)
116 {
117 uint8_t r, g, b;
118 uint16_t color;
119 int i;
120
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 int array_offset = low ? 0 : 8;
121 10 uint32_t *palette = (uint32_t *) cc->frame->data[1];
122
123
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 10 times.
90 for (i = 0; i < 8; i++) {
124 80 color = (data[2 * i] << 6) + (data[2 * i + 1] & 0x3F);
125 80 r = ((color >> 8) & 0x000F) * 17;
126 80 g = ((color >> 4) & 0x000F) * 17;
127 80 b = ((color ) & 0x000F) * 17;
128 80 palette[i + array_offset] = (uint32_t)cc->alpha[i + array_offset] << 24 | r << 16 | g << 8 | b;
129 }
130 10 }
131
132 183 static int cdg_tile_block(CDGraphicsContext *cc, uint8_t *data, int b)
133 {
134 unsigned ci, ri;
135 int color;
136 int x, y;
137 int ai;
138 183 ptrdiff_t stride = cc->frame->linesize[0];
139 183 uint8_t *buf = cc->frame->data[0];
140
141 183 ri = (data[2] & 0x1F) * CDG_TILE_HEIGHT + cc->vscroll;
142 183 ci = (data[3] & 0x3F) * CDG_TILE_WIDTH + cc->hscroll;
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (ri > (CDG_FULL_HEIGHT - CDG_TILE_HEIGHT))
145 return AVERROR(EINVAL);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (ci > (CDG_FULL_WIDTH - CDG_TILE_WIDTH))
147 return AVERROR(EINVAL);
148
149
2/2
✓ Branch 0 taken 2196 times.
✓ Branch 1 taken 183 times.
2379 for (y = 0; y < CDG_TILE_HEIGHT; y++) {
150
2/2
✓ Branch 0 taken 13176 times.
✓ Branch 1 taken 2196 times.
15372 for (x = 0; x < CDG_TILE_WIDTH; x++) {
151
2/2
✓ Branch 0 taken 10318 times.
✓ Branch 1 taken 2858 times.
13176 if (!((data[4 + y] >> (5 - x)) & 0x01))
152 10318 color = data[0] & 0x0F;
153 else
154 2858 color = data[1] & 0x0F;
155
156 13176 ai = ci + x + (stride * (ri + y));
157
1/2
✓ Branch 0 taken 13176 times.
✗ Branch 1 not taken.
13176 if (b)
158 13176 color ^= buf[ai];
159 13176 buf[ai] = color;
160 }
161 }
162
163 183 return 0;
164 }
165
166 #define UP 2
167 #define DOWN 1
168 #define LEFT 2
169 #define RIGHT 1
170
171 static void cdg_copy_rect_buf(int out_tl_x, int out_tl_y, uint8_t *out,
172 int in_tl_x, int in_tl_y, uint8_t *in,
173 int w, int h, int stride)
174 {
175 int y;
176
177 in += in_tl_x + in_tl_y * stride;
178 out += out_tl_x + out_tl_y * stride;
179 for (y = 0; y < h; y++)
180 memcpy(out + y * stride, in + y * stride, w);
181 }
182
183 static void cdg_fill_rect_preset(int tl_x, int tl_y, uint8_t *out,
184 int color, int w, int h, int stride)
185 {
186 int y;
187
188 for (y = tl_y; y < tl_y + h; y++)
189 memset(out + tl_x + y * stride, color, w);
190 }
191
192 static void cdg_fill_wrapper(int out_tl_x, int out_tl_y, uint8_t *out,
193 int in_tl_x, int in_tl_y, uint8_t *in,
194 int color, int w, int h, int stride, int roll)
195 {
196 if (roll) {
197 cdg_copy_rect_buf(out_tl_x, out_tl_y, out, in_tl_x, in_tl_y,
198 in, w, h, stride);
199 } else {
200 cdg_fill_rect_preset(out_tl_x, out_tl_y, out, color, w, h, stride);
201 }
202 }
203
204 static void cdg_scroll(CDGraphicsContext *cc, uint8_t *data,
205 AVFrame *new_frame, int roll_over)
206 {
207 int color;
208 int hscmd, h_off, hinc, vscmd, v_off, vinc;
209 int y;
210 ptrdiff_t stride = cc->frame->linesize[0];
211 uint8_t *in = cc->frame->data[0];
212 uint8_t *out = new_frame->data[0];
213
214 color = data[0] & 0x0F;
215 hscmd = (data[1] & 0x30) >> 4;
216 vscmd = (data[2] & 0x30) >> 4;
217
218 h_off = FFMIN(data[1] & 0x07, CDG_BORDER_WIDTH - 1);
219 v_off = FFMIN(data[2] & 0x0F, CDG_BORDER_HEIGHT - 1);
220
221 /// find the difference and save the offset for cdg_tile_block usage
222 hinc = h_off - cc->hscroll;
223 vinc = cc->vscroll - v_off;
224 cc->hscroll = h_off;
225 cc->vscroll = v_off;
226
227 if (vscmd == UP)
228 vinc -= 12;
229 if (vscmd == DOWN)
230 vinc += 12;
231 if (hscmd == LEFT)
232 hinc -= 6;
233 if (hscmd == RIGHT)
234 hinc += 6;
235
236 if (!hinc && !vinc)
237 return;
238
239 memcpy(new_frame->data[1], cc->frame->data[1], CDG_PALETTE_SIZE * 4);
240
241 for (y = FFMAX(0, vinc); y < FFMIN(CDG_FULL_HEIGHT + vinc, CDG_FULL_HEIGHT); y++)
242 memcpy(out + FFMAX(0, hinc) + stride * y,
243 in + FFMAX(0, hinc) - hinc + (y - vinc) * stride,
244 FFABS(stride) - FFABS(hinc));
245
246 if (vinc > 0)
247 cdg_fill_wrapper(0, 0, out,
248 0, CDG_FULL_HEIGHT - vinc, in, color,
249 FFABS(stride), vinc, stride, roll_over);
250 else if (vinc < 0)
251 cdg_fill_wrapper(0, CDG_FULL_HEIGHT + vinc, out,
252 0, 0, in, color,
253 FFABS(stride), -1 * vinc, stride, roll_over);
254
255 if (hinc > 0)
256 cdg_fill_wrapper(0, 0, out,
257 CDG_FULL_WIDTH - hinc, 0, in, color,
258 hinc, CDG_FULL_HEIGHT, stride, roll_over);
259 else if (hinc < 0)
260 cdg_fill_wrapper(CDG_FULL_WIDTH + hinc, 0, out,
261 0, 0, in, color,
262 -1 * hinc, CDG_FULL_HEIGHT, stride, roll_over);
263
264 }
265
266 306 static int cdg_decode_frame(AVCodecContext *avctx, AVFrame *frame,
267 int *got_frame, AVPacket *avpkt)
268 {
269 GetByteContext gb;
270 306 int buf_size = avpkt->size;
271 int ret;
272 uint8_t command, inst;
273 306 uint8_t cdg_data[CDG_DATA_SIZE] = {0};
274 306 CDGraphicsContext *cc = avctx->priv_data;
275
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 306 times.
306 if (buf_size < CDG_MINIMUM_PKT_SIZE) {
277 av_log(avctx, AV_LOG_ERROR, "buffer too small for decoder\n");
278 return AVERROR(EINVAL);
279 }
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 306 times.
306 if (buf_size > CDG_HEADER_SIZE + CDG_DATA_SIZE) {
281 av_log(avctx, AV_LOG_ERROR, "buffer too big for decoder\n");
282 return AVERROR(EINVAL);
283 }
284
285 306 bytestream2_init(&gb, avpkt->data, avpkt->size);
286
287
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 306 times.
306 if ((ret = ff_reget_buffer(avctx, cc->frame, 0)) < 0)
288 return ret;
289
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 305 times.
306 if (!cc->cleared) {
290
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 1 times.
217 for (int y = 0; y < avctx->height; y++)
291 216 memset(cc->frame->data[0] + y * cc->frame->linesize[0], 0, avctx->width);
292 1 memset(cc->frame->data[1], 0, AVPALETTE_SIZE);
293 1 cc->cleared = 1;
294 }
295
296 306 command = bytestream2_get_byte(&gb);
297 306 inst = bytestream2_get_byte(&gb);
298 306 inst &= CDG_MASK;
299 306 bytestream2_skip(&gb, 2);
300 306 bytestream2_get_buffer(&gb, cdg_data, sizeof(cdg_data));
301
302
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 90 times.
306 if ((command & CDG_MASK) == CDG_COMMAND) {
303
5/7
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 183 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
216 switch (inst) {
304 16 case CDG_INST_MEMORY_PRESET:
305
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 if (!(cdg_data[1] & 0x0F)) {
306
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 1 times.
217 for (int y = 0; y < avctx->height; y++)
307 216 memset(cc->frame->data[0] + y * cc->frame->linesize[0],
308 216 cdg_data[0] & 0x0F, avctx->width);
309 }
310 16 break;
311 10 case CDG_INST_LOAD_PAL_LO:
312 case CDG_INST_LOAD_PAL_HIGH:
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
314 av_log(avctx, AV_LOG_ERROR, "buffer too small for loading palette\n");
315 return AVERROR(EINVAL);
316 }
317
318 10 cdg_load_palette(cc, cdg_data, inst == CDG_INST_LOAD_PAL_LO);
319 10 break;
320 6 case CDG_INST_BORDER_PRESET:
321 6 cdg_border_preset(cc, cdg_data);
322 6 break;
323 183 case CDG_INST_TILE_BLOCK_XOR:
324 case CDG_INST_TILE_BLOCK:
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
326 av_log(avctx, AV_LOG_ERROR, "buffer too small for drawing tile\n");
327 return AVERROR(EINVAL);
328 }
329
330 183 ret = cdg_tile_block(cc, cdg_data, inst == CDG_INST_TILE_BLOCK_XOR);
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (ret) {
332 av_log(avctx, AV_LOG_ERROR, "tile is out of range\n");
333 return ret;
334 }
335 183 break;
336 case CDG_INST_SCROLL_PRESET:
337 case CDG_INST_SCROLL_COPY:
338 if (buf_size - CDG_HEADER_SIZE < CDG_MINIMUM_SCROLL_SIZE) {
339 av_log(avctx, AV_LOG_ERROR, "buffer too small for scrolling\n");
340 return AVERROR(EINVAL);
341 }
342
343 if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
344 return ret;
345
346 cdg_scroll(cc, cdg_data, frame, inst == CDG_INST_SCROLL_COPY);
347 ret = av_frame_replace(cc->frame, frame);
348 if (ret < 0)
349 return ret;
350 break;
351 1 case CDG_INST_TRANSPARENT_COL:
352
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
17 for (int i = 0; i < CDG_PALETTE_SIZE; i++)
353 16 cc->alpha[i] = 255 - ((cdg_data[i] & 0x3f) << 2);
354 1 break;
355 default:
356 break;
357 }
358
359
1/2
✓ Branch 0 taken 216 times.
✗ Branch 1 not taken.
216 if (!frame->data[0]) {
360 216 ret = av_frame_ref(frame, cc->frame);
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (ret < 0)
362 return ret;
363 }
364 216 *got_frame = 1;
365 } else {
366 90 *got_frame = 0;
367 }
368
369 306 return avpkt->size;
370 }
371
372 static av_cold void cdg_decode_flush(AVCodecContext *avctx)
373 {
374 CDGraphicsContext *cc = avctx->priv_data;
375
376 if (!cc->frame->data[0])
377 return;
378
379 for (int y = 0; y < avctx->height; y++)
380 memset(cc->frame->data[0] + y * cc->frame->linesize[0], 0, avctx->width);
381 if (!avctx->frame_num)
382 memset(cc->frame->data[1], 0, AVPALETTE_SIZE);
383 }
384
385 2 static av_cold int cdg_decode_end(AVCodecContext *avctx)
386 {
387 2 CDGraphicsContext *cc = avctx->priv_data;
388
389 2 av_frame_free(&cc->frame);
390
391 2 return 0;
392 }
393
394 const FFCodec ff_cdgraphics_decoder = {
395 .p.name = "cdgraphics",
396 CODEC_LONG_NAME("CD Graphics video"),
397 .p.type = AVMEDIA_TYPE_VIDEO,
398 .p.id = AV_CODEC_ID_CDGRAPHICS,
399 .priv_data_size = sizeof(CDGraphicsContext),
400 .init = cdg_decode_init,
401 .close = cdg_decode_end,
402 FF_CODEC_DECODE_CB(cdg_decode_frame),
403 .flush = cdg_decode_flush,
404 .p.capabilities = AV_CODEC_CAP_DR1,
405 };
406