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
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
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_LOAD_PAL_LO 30
53 #define CDG_INST_LOAD_PAL_HIGH 31
54 #define CDG_INST_TILE_BLOCK_XOR 38
55
56 /// data sizes
57 #define CDG_PACKET_SIZE 24
58 #define CDG_DATA_SIZE 16
59 #define CDG_TILE_HEIGHT 12
60 #define CDG_TILE_WIDTH 6
61 #define CDG_MINIMUM_PKT_SIZE 6
62 #define CDG_MINIMUM_SCROLL_SIZE 3
63 #define CDG_HEADER_SIZE 8
64 #define CDG_PALETTE_SIZE 16
65
71
73 {
80 }
81
83 {
85
87
91
92 return 0;
93 }
94
96 {
97 int y;
100 int color = data[0] & 0x0F;
101
102 if (!(data[1] & 0x0F)) {
103 /// fill the top and bottom borders
107
108 /// fill the side borders
113 }
114 }
115 }
116
118 {
121 int i;
122 int array_offset = low ? 0 : 8;
124
125 for (i = 0; i < 8; i++) {
126 color = (data[2 * i] << 6) + (data[2 * i + 1] & 0x3F);
127 r = ((color >> 8) & 0x000F) * 17;
128 g = ((color >> 4) & 0x000F) * 17;
129 b = ((
color ) & 0x000F) * 17;
130 palette[i + array_offset] = 0xFF
U << 24 | r << 16 | g << 8 |
b;
131 }
133 }
134
136 {
137 unsigned ci, ri;
139 int x, y;
140 int ai;
143
146
151
154 if (!((data[4 + y] >> (5 - x)) & 0x01))
155 color = data[0] & 0x0F;
156 else
157 color = data[1] & 0x0F;
158
159 ai = ci + x + (stride * (ri + y));
160 if (b)
161 color ^= buf[ai];
163 }
164 }
165
166 return 0;
167 }
168
173
175 int in_tl_x,
int in_tl_y,
uint8_t *in,
177 {
178 int y;
179
180 in += in_tl_x + in_tl_y *
stride;
181 out += out_tl_x + out_tl_y *
stride;
182 for (y = 0; y < h; y++)
183 memcpy(out + y * stride, in + y * stride, w);
184 }
185
188 {
189 int y;
190
191 for (y = tl_y; y < tl_y + h; y++)
192 memset(out + tl_x + y * stride, color, w);
193 }
194
196 int in_tl_x,
int in_tl_y,
uint8_t *in,
198 {
199 if (roll) {
201 in, w, h, stride);
202 } else {
204 }
205 }
206
208 AVFrame *new_frame,
int roll_over)
209 {
211 int hscmd, h_off, hinc, vscmd, v_off, vinc;
212 int y;
216
217 color = data[0] & 0x0F;
218 hscmd = (data[1] & 0x30) >> 4;
219 vscmd = (data[2] & 0x30) >> 4;
220
223
224 /// find the difference and save the offset for cdg_tile_block usage
229
231 vinc -= 12;
233 vinc += 12;
235 hinc -= 6;
237 hinc += 6;
238
239 if (!hinc && !vinc)
240 return;
241
243
245 memcpy(out +
FFMAX(0, hinc) + stride * y,
246 in +
FFMAX(0, hinc) - hinc + (y - vinc) * stride,
247 FFMIN(stride + hinc, stride));
248
249 if (vinc > 0)
252 stride, vinc, stride, roll_over);
253 else if (vinc < 0)
255 0, 0, in, color,
256 stride, -1 * vinc, stride, roll_over);
257
258 if (hinc > 0)
262 else if (hinc < 0)
264 0, 0, in, color,
266
267 }
268
271 {
273 int buf_size = avpkt->
size;
274 int ret;
279
283 }
287 }
288
290 if (ret) {
292 return ret;
293 }
294
295 command = bytestream_get_byte(&buf);
296 inst = bytestream_get_byte(&buf);
298 buf += 2; /// skipping 2 unneeded bytes
299
302
304 switch (inst) {
306 if (!(cdg_data[1] & 0x0F))
307 memset(cc->
frame.
data[0], cdg_data[0] & 0x0F,
309 break;
315 }
316
318 break;
321 break;
327 }
328
330 if (ret) {
332 return ret;
333 }
334 break;
340 }
341
344 if (ret) {
346 return ret;
347 }
348
351 cc->
frame = new_frame;
352 break;
353 default:
354 break;
355 }
356
357 *got_frame = 1;
358 } else {
359 *got_frame = 0;
360 buf_size = 0;
361 }
362
364 return buf_size;
365 }
366
368 {
370
373
374 return 0;
375 }
376
378 .
name =
"cdgraphics",
387 };