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