00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "bytestream.h"
00024
00033
00034 #define CDG_FULL_WIDTH 300
00035 #define CDG_FULL_HEIGHT 216
00036 #define CDG_DISPLAY_WIDTH 294
00037 #define CDG_DISPLAY_HEIGHT 204
00038 #define CDG_BORDER_WIDTH 6
00039 #define CDG_BORDER_HEIGHT 12
00040
00042 #define CDG_COMMAND 0x09
00043 #define CDG_MASK 0x3F
00044
00046 #define CDG_INST_MEMORY_PRESET 1
00047 #define CDG_INST_BORDER_PRESET 2
00048 #define CDG_INST_TILE_BLOCK 6
00049 #define CDG_INST_SCROLL_PRESET 20
00050 #define CDG_INST_SCROLL_COPY 24
00051 #define CDG_INST_LOAD_PAL_LO 30
00052 #define CDG_INST_LOAD_PAL_HIGH 31
00053 #define CDG_INST_TILE_BLOCK_XOR 38
00054
00056 #define CDG_PACKET_SIZE 24
00057 #define CDG_DATA_SIZE 16
00058 #define CDG_TILE_HEIGHT 12
00059 #define CDG_TILE_WIDTH 6
00060 #define CDG_MINIMUM_PKT_SIZE 6
00061 #define CDG_MINIMUM_SCROLL_SIZE 3
00062 #define CDG_HEADER_SIZE 8
00063 #define CDG_PALETTE_SIZE 16
00064
00065 typedef struct CDGraphicsContext {
00066 AVFrame frame;
00067 int hscroll;
00068 int vscroll;
00069 } CDGraphicsContext;
00070
00071 static void cdg_init_frame(AVFrame *frame)
00072 {
00073 avcodec_get_frame_defaults(frame);
00074 frame->reference = 3;
00075 frame->buffer_hints = FF_BUFFER_HINTS_VALID |
00076 FF_BUFFER_HINTS_READABLE |
00077 FF_BUFFER_HINTS_PRESERVE |
00078 FF_BUFFER_HINTS_REUSABLE;
00079 }
00080
00081 static av_cold int cdg_decode_init(AVCodecContext *avctx)
00082 {
00083 CDGraphicsContext *cc = avctx->priv_data;
00084
00085 cdg_init_frame(&cc->frame);
00086
00087 avctx->width = CDG_FULL_WIDTH;
00088 avctx->height = CDG_FULL_HEIGHT;
00089 avctx->pix_fmt = PIX_FMT_PAL8;
00090
00091 return 0;
00092 }
00093
00094 static void cdg_border_preset(CDGraphicsContext *cc, uint8_t *data)
00095 {
00096 int y;
00097 int lsize = cc->frame.linesize[0];
00098 uint8_t *buf = cc->frame.data[0];
00099 int color = data[0] & 0x0F;
00100
00101 if (!(data[1] & 0x0F)) {
00103 memset(buf, color, CDG_BORDER_HEIGHT * lsize);
00104 memset(buf + (CDG_FULL_HEIGHT - CDG_BORDER_HEIGHT) * lsize,
00105 color, CDG_BORDER_HEIGHT * lsize);
00106
00108 for (y = CDG_BORDER_HEIGHT; y < CDG_FULL_HEIGHT - CDG_BORDER_HEIGHT; y++) {
00109 memset(buf + y * lsize, color, CDG_BORDER_WIDTH);
00110 memset(buf + CDG_FULL_WIDTH - CDG_BORDER_WIDTH + y * lsize,
00111 color, CDG_BORDER_WIDTH);
00112 }
00113 }
00114 }
00115
00116 static void cdg_load_palette(CDGraphicsContext *cc, uint8_t *data, int low)
00117 {
00118 uint8_t r, g, b;
00119 uint16_t color;
00120 int i;
00121 int array_offset = low ? 0 : 8;
00122 uint32_t *palette = (uint32_t *) cc->frame.data[1];
00123
00124 for (i = 0; i < 8; i++) {
00125 color = (data[2 * i] << 6) + (data[2 * i + 1] & 0x3F);
00126 r = ((color >> 8) & 0x000F) * 17;
00127 g = ((color >> 4) & 0x000F) * 17;
00128 b = ((color ) & 0x000F) * 17;
00129 palette[i + array_offset] = 0xFF << 24 | r << 16 | g << 8 | b;
00130 }
00131 cc->frame.palette_has_changed = 1;
00132 }
00133
00134 static int cdg_tile_block(CDGraphicsContext *cc, uint8_t *data, int b)
00135 {
00136 unsigned ci, ri;
00137 int color;
00138 int x, y;
00139 int ai;
00140 int stride = cc->frame.linesize[0];
00141 uint8_t *buf = cc->frame.data[0];
00142
00143 ri = (data[2] & 0x1F) * CDG_TILE_HEIGHT + cc->vscroll;
00144 ci = (data[3] & 0x3F) * CDG_TILE_WIDTH + cc->hscroll;
00145
00146 if (ri > (CDG_FULL_HEIGHT - CDG_TILE_HEIGHT))
00147 return AVERROR(EINVAL);
00148 if (ci > (CDG_FULL_WIDTH - CDG_TILE_WIDTH))
00149 return AVERROR(EINVAL);
00150
00151 for (y = 0; y < CDG_TILE_HEIGHT; y++) {
00152 for (x = 0; x < CDG_TILE_WIDTH; x++) {
00153 if (!((data[4 + y] >> (5 - x)) & 0x01))
00154 color = data[0] & 0x0F;
00155 else
00156 color = data[1] & 0x0F;
00157
00158 ai = ci + x + (stride * (ri + y));
00159 if (b)
00160 color ^= buf[ai];
00161 buf[ai] = color;
00162 }
00163 }
00164
00165 return 0;
00166 }
00167
00168 #define UP 2
00169 #define DOWN 1
00170 #define LEFT 2
00171 #define RIGHT 1
00172
00173 static void cdg_copy_rect_buf(int out_tl_x, int out_tl_y, uint8_t *out,
00174 int in_tl_x, int in_tl_y, uint8_t *in,
00175 int w, int h, int stride)
00176 {
00177 int y;
00178
00179 in += in_tl_x + in_tl_y * stride;
00180 out += out_tl_x + out_tl_y * stride;
00181 for (y = 0; y < h; y++)
00182 memcpy(out + y * stride, in + y * stride, w);
00183 }
00184
00185 static void cdg_fill_rect_preset(int tl_x, int tl_y, uint8_t *out,
00186 int color, int w, int h, int stride)
00187 {
00188 int y;
00189
00190 for (y = tl_y; y < tl_y + h; y++)
00191 memset(out + tl_x + y * stride, color, w);
00192 }
00193
00194 static void cdg_fill_wrapper(int out_tl_x, int out_tl_y, uint8_t *out,
00195 int in_tl_x, int in_tl_y, uint8_t *in,
00196 int color, int w, int h, int stride, int roll)
00197 {
00198 if (roll) {
00199 cdg_copy_rect_buf(out_tl_x, out_tl_y, out, in_tl_x, in_tl_y,
00200 in, w, h, stride);
00201 } else {
00202 cdg_fill_rect_preset(out_tl_x, out_tl_y, out, color, w, h, stride);
00203 }
00204 }
00205
00206 static void cdg_scroll(CDGraphicsContext *cc, uint8_t *data,
00207 AVFrame *new_frame, int roll_over)
00208 {
00209 int color;
00210 int hscmd, h_off, hinc, vscmd, v_off, vinc;
00211 int y;
00212 int stride = cc->frame.linesize[0];
00213 uint8_t *in = cc->frame.data[0];
00214 uint8_t *out = new_frame->data[0];
00215
00216 color = data[0] & 0x0F;
00217 hscmd = (data[1] & 0x30) >> 4;
00218 vscmd = (data[2] & 0x30) >> 4;
00219
00220 h_off = FFMIN(data[1] & 0x07, CDG_BORDER_WIDTH - 1);
00221 v_off = FFMIN(data[2] & 0x07, CDG_BORDER_HEIGHT - 1);
00222
00224 hinc = h_off - cc->hscroll;
00225 vinc = v_off - cc->vscroll;
00226 cc->hscroll = h_off;
00227 cc->vscroll = v_off;
00228
00229 if (vscmd == UP)
00230 vinc -= 12;
00231 if (vscmd == DOWN)
00232 vinc += 12;
00233 if (hscmd == LEFT)
00234 hinc -= 6;
00235 if (hscmd == RIGHT)
00236 hinc += 6;
00237
00238 if (!hinc && !vinc)
00239 return;
00240
00241 memcpy(new_frame->data[1], cc->frame.data[1], CDG_PALETTE_SIZE * 4);
00242
00243 for (y = FFMAX(0, vinc); y < FFMIN(CDG_FULL_HEIGHT + vinc, CDG_FULL_HEIGHT); y++)
00244 memcpy(out + FFMAX(0, hinc) + stride * y,
00245 in + FFMAX(0, hinc) - hinc + (y - vinc) * stride,
00246 FFMIN(stride + hinc, stride));
00247
00248 if (vinc > 0)
00249 cdg_fill_wrapper(0, 0, out,
00250 0, CDG_FULL_HEIGHT - vinc, in, color,
00251 stride, vinc, stride, roll_over);
00252 else if (vinc < 0)
00253 cdg_fill_wrapper(0, CDG_FULL_HEIGHT + vinc, out,
00254 0, 0, in, color,
00255 stride, -1 * vinc, stride, roll_over);
00256
00257 if (hinc > 0)
00258 cdg_fill_wrapper(0, 0, out,
00259 CDG_FULL_WIDTH - hinc, 0, in, color,
00260 hinc, CDG_FULL_HEIGHT, stride, roll_over);
00261 else if (hinc < 0)
00262 cdg_fill_wrapper(CDG_FULL_WIDTH + hinc, 0, out,
00263 0, 0, in, color,
00264 -1 * hinc, CDG_FULL_HEIGHT, stride, roll_over);
00265
00266 }
00267
00268 static int cdg_decode_frame(AVCodecContext *avctx,
00269 void *data, int *data_size, AVPacket *avpkt)
00270 {
00271 const uint8_t *buf = avpkt->data;
00272 int buf_size = avpkt->size;
00273 int ret;
00274 uint8_t command, inst;
00275 uint8_t cdg_data[CDG_DATA_SIZE];
00276 AVFrame new_frame;
00277 CDGraphicsContext *cc = avctx->priv_data;
00278
00279 if (buf_size < CDG_MINIMUM_PKT_SIZE) {
00280 av_log(avctx, AV_LOG_ERROR, "buffer too small for decoder\n");
00281 return AVERROR(EINVAL);
00282 }
00283 if (buf_size > CDG_HEADER_SIZE + CDG_DATA_SIZE) {
00284 av_log(avctx, AV_LOG_ERROR, "buffer too big for decoder\n");
00285 return AVERROR(EINVAL);
00286 }
00287
00288 ret = avctx->reget_buffer(avctx, &cc->frame);
00289 if (ret) {
00290 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00291 return ret;
00292 }
00293
00294 command = bytestream_get_byte(&buf);
00295 inst = bytestream_get_byte(&buf);
00296 inst &= CDG_MASK;
00297 buf += 2;
00298 bytestream_get_buffer(&buf, cdg_data, buf_size - CDG_HEADER_SIZE);
00299
00300 if ((command & CDG_MASK) == CDG_COMMAND) {
00301 switch (inst) {
00302 case CDG_INST_MEMORY_PRESET:
00303 if (!(cdg_data[1] & 0x0F))
00304 memset(cc->frame.data[0], cdg_data[0] & 0x0F,
00305 cc->frame.linesize[0] * CDG_FULL_HEIGHT);
00306 break;
00307 case CDG_INST_LOAD_PAL_LO:
00308 case CDG_INST_LOAD_PAL_HIGH:
00309 if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
00310 av_log(avctx, AV_LOG_ERROR, "buffer too small for loading palette\n");
00311 return AVERROR(EINVAL);
00312 }
00313
00314 cdg_load_palette(cc, cdg_data, inst == CDG_INST_LOAD_PAL_LO);
00315 break;
00316 case CDG_INST_BORDER_PRESET:
00317 cdg_border_preset(cc, cdg_data);
00318 break;
00319 case CDG_INST_TILE_BLOCK_XOR:
00320 case CDG_INST_TILE_BLOCK:
00321 if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
00322 av_log(avctx, AV_LOG_ERROR, "buffer too small for drawing tile\n");
00323 return AVERROR(EINVAL);
00324 }
00325
00326 ret = cdg_tile_block(cc, cdg_data, inst == CDG_INST_TILE_BLOCK_XOR);
00327 if (ret) {
00328 av_log(avctx, AV_LOG_ERROR, "tile is out of range\n");
00329 return ret;
00330 }
00331 break;
00332 case CDG_INST_SCROLL_PRESET:
00333 case CDG_INST_SCROLL_COPY:
00334 if (buf_size - CDG_HEADER_SIZE < CDG_MINIMUM_SCROLL_SIZE) {
00335 av_log(avctx, AV_LOG_ERROR, "buffer too small for scrolling\n");
00336 return AVERROR(EINVAL);
00337 }
00338
00339 cdg_init_frame(&new_frame);
00340 ret = avctx->get_buffer(avctx, &new_frame);
00341 if (ret) {
00342 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00343 return ret;
00344 }
00345
00346 cdg_scroll(cc, cdg_data, &new_frame, inst == CDG_INST_SCROLL_COPY);
00347 avctx->release_buffer(avctx, &cc->frame);
00348 cc->frame = new_frame;
00349 break;
00350 default:
00351 break;
00352 }
00353
00354 *data_size = sizeof(AVFrame);
00355 } else {
00356 *data_size = 0;
00357 buf_size = 0;
00358 }
00359
00360 *(AVFrame *) data = cc->frame;
00361 return buf_size;
00362 }
00363
00364 static av_cold int cdg_decode_end(AVCodecContext *avctx)
00365 {
00366 CDGraphicsContext *cc = avctx->priv_data;
00367
00368 if (cc->frame.data[0])
00369 avctx->release_buffer(avctx, &cc->frame);
00370
00371 return 0;
00372 }
00373
00374 AVCodec ff_cdgraphics_decoder = {
00375 .name = "cdgraphics",
00376 .type = AVMEDIA_TYPE_VIDEO,
00377 .id = CODEC_ID_CDGRAPHICS,
00378 .priv_data_size = sizeof(CDGraphicsContext),
00379 .init = cdg_decode_init,
00380 .close = cdg_decode_end,
00381 .decode = cdg_decode_frame,
00382 .capabilities = CODEC_CAP_DR1,
00383 .long_name = NULL_IF_CONFIG_SMALL("CD Graphics video"),
00384 };