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 {
75
79
83
84 return 0;
85 }
86
88 {
92 int color = data[0] & 0x0F;
93
94 if (!(data[1] & 0x0F)) {
95 /// fill the top and bottom borders
99
100 /// fill the side borders
105 }
106 }
107 }
108
110 {
113 int i;
114 int array_offset = low ? 0 : 8;
116
117 for (i = 0; i < 8; i++) {
118 color = (data[2 * i] << 6) + (data[2 * i + 1] & 0x3F);
119 r = ((color >> 8) & 0x000F) * 17;
120 g = ((color >> 4) & 0x000F) * 17;
121 b = ((
color ) & 0x000F) * 17;
122 palette[i + array_offset] = 0xFF
U << 24 | r << 16 | g << 8 |
b;
123 }
125 }
126
128 {
129 unsigned ci, ri;
132 int ai;
135
138
143
146 if (!((data[4 + y] >> (5 - x)) & 0x01))
147 color = data[0] & 0x0F;
148 else
149 color = data[1] & 0x0F;
150
151 ai = ci + x + (stride * (ri +
y));
152 if (b)
153 color ^= buf[ai];
155 }
156 }
157
158 return 0;
159 }
160
165
169 {
171
172 in += in_tl_x + in_tl_y * stride;
173 out += out_tl_x + out_tl_y * stride;
174 for (y = 0; y < h; y++)
175 memcpy(out + y * stride, in + y * stride, w);
176 }
177
180 {
182
183 for (y = tl_y; y < tl_y + h; y++)
184 memset(out + tl_x + y * stride, color, w);
185 }
186
190 {
191 if (roll) {
193 in, w, h, stride);
194 } else {
196 }
197 }
198
200 AVFrame *new_frame,
int roll_over)
201 {
203 int hscmd, h_off, hinc, vscmd, v_off, vinc;
208
209 color = data[0] & 0x0F;
210 hscmd = (data[1] & 0x30) >> 4;
211 vscmd = (data[2] & 0x30) >> 4;
212
215
216 /// find the difference and save the offset for cdg_tile_block usage
221
223 vinc -= 12;
225 vinc += 12;
227 hinc -= 6;
229 hinc += 6;
230
231 if (!hinc && !vinc)
232 return;
233
235
237 memcpy(out +
FFMAX(0, hinc) + stride *
y,
238 in +
FFMAX(0, hinc) - hinc + (y - vinc) * stride,
239 FFMIN(stride + hinc, stride));
240
241 if (vinc > 0)
244 stride, vinc, stride, roll_over);
245 else if (vinc < 0)
247 0, 0, in, color,
248 stride, -1 * vinc, stride, roll_over);
249
250 if (hinc > 0)
254 else if (hinc < 0)
256 0, 0, in, color,
258
259 }
260
263 {
265 int buf_size = avpkt->
size;
271
275 }
279 }
280
282
288 }
289
290 command = bytestream2_get_byte(&gb);
291 inst = bytestream2_get_byte(&gb);
295
297 switch (inst) {
299 if (!(cdg_data[1] & 0x0F))
300 memset(cc->
frame->
data[0], cdg_data[0] & 0x0F,
302 break;
308 }
309
311 break;
314 break;
320 }
321
323 if (ret) {
326 }
327 break;
333 }
334
337
341 if (ret < 0)
343 break;
344 default:
345 break;
346 }
347
348 if (!frame->
data[0]) {
350 if (ret < 0)
352 }
353 *got_frame = 1;
354 } else {
355 *got_frame = 0;
356 }
357
359 }
360
362 {
364
366
367 return 0;
368 }
369
371 .
name =
"cdgraphics",
380 };