libavcodec/cdgraphics.c
Go to the documentation of this file.
00001 /*
00002  * CD Graphics Video Decoder
00003  * Copyright (c) 2009 Michael Tison
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avcodec.h"
00023 #include "internal.h"
00024 #include "bytestream.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 = 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] = 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] & 0x07, 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 *data_size, AVPacket *avpkt)
00271 {
00272     GetByteContext gb;
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     bytestream2_init(&gb, avpkt->data, avpkt->size);
00281 
00282 
00283     ret = avctx->reget_buffer(avctx, &cc->frame);
00284     if (ret) {
00285         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00286         return ret;
00287     }
00288 
00289     command = bytestream2_get_byte(&gb);
00290     inst    = bytestream2_get_byte(&gb);
00291     inst    &= CDG_MASK;
00292     bytestream2_skip(&gb, 2);
00293     bytestream2_get_buffer(&gb, cdg_data, sizeof(cdg_data));
00294 
00295     if ((command & CDG_MASK) == CDG_COMMAND) {
00296         switch (inst) {
00297         case CDG_INST_MEMORY_PRESET:
00298             if (!(cdg_data[1] & 0x0F))
00299                 memset(cc->frame.data[0], cdg_data[0] & 0x0F,
00300                        cc->frame.linesize[0] * CDG_FULL_HEIGHT);
00301             break;
00302         case CDG_INST_LOAD_PAL_LO:
00303         case CDG_INST_LOAD_PAL_HIGH:
00304             if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
00305                 av_log(avctx, AV_LOG_ERROR, "buffer too small for loading palette\n");
00306                 return AVERROR(EINVAL);
00307             }
00308 
00309             cdg_load_palette(cc, cdg_data, inst == CDG_INST_LOAD_PAL_LO);
00310             break;
00311         case CDG_INST_BORDER_PRESET:
00312             cdg_border_preset(cc, cdg_data);
00313             break;
00314         case CDG_INST_TILE_BLOCK_XOR:
00315         case CDG_INST_TILE_BLOCK:
00316             if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
00317                 av_log(avctx, AV_LOG_ERROR, "buffer too small for drawing tile\n");
00318                 return AVERROR(EINVAL);
00319             }
00320 
00321             ret = cdg_tile_block(cc, cdg_data, inst == CDG_INST_TILE_BLOCK_XOR);
00322             if (ret) {
00323                 av_log(avctx, AV_LOG_ERROR, "tile is out of range\n");
00324                 return ret;
00325             }
00326             break;
00327         case CDG_INST_SCROLL_PRESET:
00328         case CDG_INST_SCROLL_COPY:
00329             if (buf_size - CDG_HEADER_SIZE < CDG_MINIMUM_SCROLL_SIZE) {
00330                 av_log(avctx, AV_LOG_ERROR, "buffer too small for scrolling\n");
00331                 return AVERROR(EINVAL);
00332             }
00333 
00334             cdg_init_frame(&new_frame);
00335             ret = ff_get_buffer(avctx, &new_frame);
00336             if (ret) {
00337                 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00338                 return ret;
00339             }
00340 
00341             cdg_scroll(cc, cdg_data, &new_frame, inst == CDG_INST_SCROLL_COPY);
00342             avctx->release_buffer(avctx, &cc->frame);
00343             cc->frame = new_frame;
00344             break;
00345         default:
00346             break;
00347         }
00348 
00349         *data_size = sizeof(AVFrame);
00350     } else {
00351         *data_size = 0;
00352     }
00353 
00354     *(AVFrame *) data = cc->frame;
00355     return avpkt->size;
00356 }
00357 
00358 static av_cold int cdg_decode_end(AVCodecContext *avctx)
00359 {
00360     CDGraphicsContext *cc = avctx->priv_data;
00361 
00362     if (cc->frame.data[0])
00363         avctx->release_buffer(avctx, &cc->frame);
00364 
00365     return 0;
00366 }
00367 
00368 AVCodec ff_cdgraphics_decoder = {
00369     .name           = "cdgraphics",
00370     .type           = AVMEDIA_TYPE_VIDEO,
00371     .id             = CODEC_ID_CDGRAPHICS,
00372     .priv_data_size = sizeof(CDGraphicsContext),
00373     .init           = cdg_decode_init,
00374     .close          = cdg_decode_end,
00375     .decode         = cdg_decode_frame,
00376     .capabilities   = CODEC_CAP_DR1,
00377     .long_name = NULL_IF_CONFIG_SMALL("CD Graphics video"),
00378 };