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