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