LCOV - code coverage report
Current view: top level - libavcodec - cdgraphics.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 99 180 55.0 %
Date: 2017-12-17 04:34:43 Functions: 6 10 60.0 %

          Line data    Source code
       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             : #include "internal.h"
      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
      44             : #define CDG_MASK                0x3F
      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_TRANSPARENT_COL  28
      53             : #define CDG_INST_LOAD_PAL_LO      30
      54             : #define CDG_INST_LOAD_PAL_HIGH    31
      55             : #define CDG_INST_TILE_BLOCK_XOR   38
      56             : 
      57             : /// data sizes
      58             : #define CDG_PACKET_SIZE           24
      59             : #define CDG_DATA_SIZE             16
      60             : #define CDG_TILE_HEIGHT           12
      61             : #define CDG_TILE_WIDTH             6
      62             : #define CDG_MINIMUM_PKT_SIZE       6
      63             : #define CDG_MINIMUM_SCROLL_SIZE    3
      64             : #define CDG_HEADER_SIZE            8
      65             : #define CDG_PALETTE_SIZE          16
      66             : 
      67             : typedef struct CDGraphicsContext {
      68             :     AVFrame *frame;
      69             :     int hscroll;
      70             :     int vscroll;
      71             :     int transparency;
      72             : } CDGraphicsContext;
      73             : 
      74           2 : static av_cold int cdg_decode_init(AVCodecContext *avctx)
      75             : {
      76           2 :     CDGraphicsContext *cc = avctx->priv_data;
      77             : 
      78           2 :     cc->frame = av_frame_alloc();
      79           2 :     if (!cc->frame)
      80           0 :         return AVERROR(ENOMEM);
      81           2 :     cc->transparency = -1;
      82             : 
      83           2 :     avctx->width   = CDG_FULL_WIDTH;
      84           2 :     avctx->height  = CDG_FULL_HEIGHT;
      85           2 :     avctx->pix_fmt = AV_PIX_FMT_PAL8;
      86             : 
      87           2 :     return 0;
      88             : }
      89             : 
      90           6 : static void cdg_border_preset(CDGraphicsContext *cc, uint8_t *data)
      91             : {
      92             :     int y;
      93           6 :     int lsize    = cc->frame->linesize[0];
      94           6 :     uint8_t *buf = cc->frame->data[0];
      95           6 :     int color    = data[0] & 0x0F;
      96             : 
      97           6 :     if (!(data[1] & 0x0F)) {
      98             :         /// fill the top and bottom borders
      99           6 :         memset(buf, color, CDG_BORDER_HEIGHT * lsize);
     100           6 :         memset(buf + (CDG_FULL_HEIGHT - CDG_BORDER_HEIGHT) * lsize,
     101           6 :                color, CDG_BORDER_HEIGHT * lsize);
     102             : 
     103             :         /// fill the side borders
     104        1158 :         for (y = CDG_BORDER_HEIGHT; y < CDG_FULL_HEIGHT - CDG_BORDER_HEIGHT; y++) {
     105        1152 :             memset(buf + y * lsize, color, CDG_BORDER_WIDTH);
     106        1152 :             memset(buf + CDG_FULL_WIDTH - CDG_BORDER_WIDTH + y * lsize,
     107             :                    color, CDG_BORDER_WIDTH);
     108             :         }
     109             :     }
     110           6 : }
     111             : 
     112          10 : static void cdg_load_palette(CDGraphicsContext *cc, uint8_t *data, int low)
     113             : {
     114             :     uint8_t r, g, b;
     115             :     uint16_t color;
     116             :     int i;
     117          10 :     int array_offset  = low ? 0 : 8;
     118          10 :     uint32_t *palette = (uint32_t *) cc->frame->data[1];
     119             : 
     120          90 :     for (i = 0; i < 8; i++) {
     121          80 :         color = (data[2 * i] << 6) + (data[2 * i + 1] & 0x3F);
     122          80 :         r = ((color >> 8) & 0x000F) * 17;
     123          80 :         g = ((color >> 4) & 0x000F) * 17;
     124          80 :         b = ((color     ) & 0x000F) * 17;
     125          80 :         palette[i + array_offset] = 0xFFU << 24 | r << 16 | g << 8 | b;
     126          80 :         if (cc->transparency >= 0)
     127          16 :             palette[cc->transparency] &= 0xFFFFFF;
     128             :     }
     129          10 :     cc->frame->palette_has_changed = 1;
     130          10 : }
     131             : 
     132         180 : static int cdg_tile_block(CDGraphicsContext *cc, uint8_t *data, int b)
     133             : {
     134             :     unsigned ci, ri;
     135             :     int color;
     136             :     int x, y;
     137             :     int ai;
     138         180 :     int stride   = cc->frame->linesize[0];
     139         180 :     uint8_t *buf = cc->frame->data[0];
     140             : 
     141         180 :     ri = (data[2] & 0x1F) * CDG_TILE_HEIGHT + cc->vscroll;
     142         180 :     ci = (data[3] & 0x3F) * CDG_TILE_WIDTH  + cc->hscroll;
     143             : 
     144         180 :     if (ri > (CDG_FULL_HEIGHT - CDG_TILE_HEIGHT))
     145           0 :         return AVERROR(EINVAL);
     146         180 :     if (ci > (CDG_FULL_WIDTH - CDG_TILE_WIDTH))
     147           0 :         return AVERROR(EINVAL);
     148             : 
     149        2340 :     for (y = 0; y < CDG_TILE_HEIGHT; y++) {
     150       15120 :         for (x = 0; x < CDG_TILE_WIDTH; x++) {
     151       12960 :             if (!((data[4 + y] >> (5 - x)) & 0x01))
     152       10153 :                 color = data[0] & 0x0F;
     153             :             else
     154        2807 :                 color = data[1] & 0x0F;
     155             : 
     156       12960 :             ai = ci + x + (stride * (ri + y));
     157       12960 :             if (b)
     158       12960 :                 color ^= buf[ai];
     159       12960 :             buf[ai] = color;
     160             :         }
     161             :     }
     162             : 
     163         180 :     return 0;
     164             : }
     165             : 
     166             : #define UP    2
     167             : #define DOWN  1
     168             : #define LEFT  2
     169             : #define RIGHT 1
     170             : 
     171           0 : static void cdg_copy_rect_buf(int out_tl_x, int out_tl_y, uint8_t *out,
     172             :                               int in_tl_x, int in_tl_y, uint8_t *in,
     173             :                               int w, int h, int stride)
     174             : {
     175             :     int y;
     176             : 
     177           0 :     in  += in_tl_x  + in_tl_y  * stride;
     178           0 :     out += out_tl_x + out_tl_y * stride;
     179           0 :     for (y = 0; y < h; y++)
     180           0 :         memcpy(out + y * stride, in + y * stride, w);
     181           0 : }
     182             : 
     183           0 : static void cdg_fill_rect_preset(int tl_x, int tl_y, uint8_t *out,
     184             :                                  int color, int w, int h, int stride)
     185             : {
     186             :     int y;
     187             : 
     188           0 :     for (y = tl_y; y < tl_y + h; y++)
     189           0 :         memset(out + tl_x + y * stride, color, w);
     190           0 : }
     191             : 
     192           0 : static void cdg_fill_wrapper(int out_tl_x, int out_tl_y, uint8_t *out,
     193             :                              int in_tl_x, int in_tl_y, uint8_t *in,
     194             :                              int color, int w, int h, int stride, int roll)
     195             : {
     196           0 :     if (roll) {
     197           0 :         cdg_copy_rect_buf(out_tl_x, out_tl_y, out, in_tl_x, in_tl_y,
     198             :                           in, w, h, stride);
     199             :     } else {
     200           0 :         cdg_fill_rect_preset(out_tl_x, out_tl_y, out, color, w, h, stride);
     201             :     }
     202           0 : }
     203             : 
     204           0 : static void cdg_scroll(CDGraphicsContext *cc, uint8_t *data,
     205             :                        AVFrame *new_frame, int roll_over)
     206             : {
     207             :     int color;
     208             :     int hscmd, h_off, hinc, vscmd, v_off, vinc;
     209             :     int y;
     210           0 :     int stride   = cc->frame->linesize[0];
     211           0 :     uint8_t *in  = cc->frame->data[0];
     212           0 :     uint8_t *out = new_frame->data[0];
     213             : 
     214           0 :     color =  data[0] & 0x0F;
     215           0 :     hscmd = (data[1] & 0x30) >> 4;
     216           0 :     vscmd = (data[2] & 0x30) >> 4;
     217             : 
     218           0 :     h_off =  FFMIN(data[1] & 0x07, CDG_BORDER_WIDTH  - 1);
     219           0 :     v_off =  FFMIN(data[2] & 0x0F, CDG_BORDER_HEIGHT - 1);
     220             : 
     221             :     /// find the difference and save the offset for cdg_tile_block usage
     222           0 :     hinc = h_off - cc->hscroll;
     223           0 :     vinc = v_off - cc->vscroll;
     224           0 :     cc->hscroll = h_off;
     225           0 :     cc->vscroll = v_off;
     226             : 
     227           0 :     if (vscmd == UP)
     228           0 :         vinc -= 12;
     229           0 :     if (vscmd == DOWN)
     230           0 :         vinc += 12;
     231           0 :     if (hscmd == LEFT)
     232           0 :         hinc -= 6;
     233           0 :     if (hscmd == RIGHT)
     234           0 :         hinc += 6;
     235             : 
     236           0 :     if (!hinc && !vinc)
     237           0 :         return;
     238             : 
     239           0 :     memcpy(new_frame->data[1], cc->frame->data[1], CDG_PALETTE_SIZE * 4);
     240             : 
     241           0 :     for (y = FFMAX(0, vinc); y < FFMIN(CDG_FULL_HEIGHT + vinc, CDG_FULL_HEIGHT); y++)
     242           0 :         memcpy(out + FFMAX(0, hinc) + stride * y,
     243           0 :                in + FFMAX(0, hinc) - hinc + (y - vinc) * stride,
     244           0 :                FFMIN(stride + hinc, stride));
     245             : 
     246           0 :     if (vinc > 0)
     247           0 :         cdg_fill_wrapper(0, 0, out,
     248             :                          0, CDG_FULL_HEIGHT - vinc, in, color,
     249             :                          stride, vinc, stride, roll_over);
     250           0 :     else if (vinc < 0)
     251           0 :         cdg_fill_wrapper(0, CDG_FULL_HEIGHT + vinc, out,
     252             :                          0, 0, in, color,
     253             :                          stride, -1 * vinc, stride, roll_over);
     254             : 
     255           0 :     if (hinc > 0)
     256           0 :         cdg_fill_wrapper(0, 0, out,
     257             :                          CDG_FULL_WIDTH - hinc, 0, in, color,
     258             :                          hinc, CDG_FULL_HEIGHT, stride, roll_over);
     259           0 :     else if (hinc < 0)
     260           0 :         cdg_fill_wrapper(CDG_FULL_WIDTH + hinc, 0, out,
     261             :                          0, 0, in, color,
     262             :                          -1 * hinc, CDG_FULL_HEIGHT, stride, roll_over);
     263             : 
     264             : }
     265             : 
     266         213 : static int cdg_decode_frame(AVCodecContext *avctx,
     267             :                             void *data, int *got_frame, AVPacket *avpkt)
     268             : {
     269             :     GetByteContext gb;
     270         213 :     int buf_size       = avpkt->size;
     271             :     int ret;
     272             :     uint8_t command, inst;
     273         213 :     uint8_t cdg_data[CDG_DATA_SIZE] = {0};
     274         213 :     AVFrame *frame = data;
     275         213 :     CDGraphicsContext *cc = avctx->priv_data;
     276             : 
     277         213 :     if (buf_size < CDG_MINIMUM_PKT_SIZE) {
     278           0 :         av_log(avctx, AV_LOG_ERROR, "buffer too small for decoder\n");
     279           0 :         return AVERROR(EINVAL);
     280             :     }
     281         213 :     if (buf_size > CDG_HEADER_SIZE + CDG_DATA_SIZE) {
     282           0 :         av_log(avctx, AV_LOG_ERROR, "buffer too big for decoder\n");
     283           0 :         return AVERROR(EINVAL);
     284             :     }
     285             : 
     286         213 :     bytestream2_init(&gb, avpkt->data, avpkt->size);
     287             : 
     288         213 :     if ((ret = ff_reget_buffer(avctx, cc->frame)) < 0)
     289           0 :         return ret;
     290         213 :     if (!avctx->frame_number) {
     291           1 :         memset(cc->frame->data[0], 0, cc->frame->linesize[0] * avctx->height);
     292           1 :         memset(cc->frame->data[1], 0, AVPALETTE_SIZE);
     293             :     }
     294             : 
     295         213 :     command = bytestream2_get_byte(&gb);
     296         213 :     inst    = bytestream2_get_byte(&gb);
     297         213 :     inst    &= CDG_MASK;
     298         213 :     bytestream2_skip(&gb, 2);
     299         213 :     bytestream2_get_buffer(&gb, cdg_data, sizeof(cdg_data));
     300             : 
     301         213 :     if ((command & CDG_MASK) == CDG_COMMAND) {
     302         213 :         switch (inst) {
     303          16 :         case CDG_INST_MEMORY_PRESET:
     304          16 :             if (!(cdg_data[1] & 0x0F))
     305           1 :                 memset(cc->frame->data[0], cdg_data[0] & 0x0F,
     306           1 :                        cc->frame->linesize[0] * CDG_FULL_HEIGHT);
     307          16 :             break;
     308          10 :         case CDG_INST_LOAD_PAL_LO:
     309             :         case CDG_INST_LOAD_PAL_HIGH:
     310          10 :             if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
     311           0 :                 av_log(avctx, AV_LOG_ERROR, "buffer too small for loading palette\n");
     312           0 :                 return AVERROR(EINVAL);
     313             :             }
     314             : 
     315          10 :             cdg_load_palette(cc, cdg_data, inst == CDG_INST_LOAD_PAL_LO);
     316          10 :             break;
     317           6 :         case CDG_INST_BORDER_PRESET:
     318           6 :             cdg_border_preset(cc, cdg_data);
     319           6 :             break;
     320         180 :         case CDG_INST_TILE_BLOCK_XOR:
     321             :         case CDG_INST_TILE_BLOCK:
     322         180 :             if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) {
     323           0 :                 av_log(avctx, AV_LOG_ERROR, "buffer too small for drawing tile\n");
     324           0 :                 return AVERROR(EINVAL);
     325             :             }
     326             : 
     327         180 :             ret = cdg_tile_block(cc, cdg_data, inst == CDG_INST_TILE_BLOCK_XOR);
     328         180 :             if (ret) {
     329           0 :                 av_log(avctx, AV_LOG_ERROR, "tile is out of range\n");
     330           0 :                 return ret;
     331             :             }
     332         180 :             break;
     333           0 :         case CDG_INST_SCROLL_PRESET:
     334             :         case CDG_INST_SCROLL_COPY:
     335           0 :             if (buf_size - CDG_HEADER_SIZE < CDG_MINIMUM_SCROLL_SIZE) {
     336           0 :                 av_log(avctx, AV_LOG_ERROR, "buffer too small for scrolling\n");
     337           0 :                 return AVERROR(EINVAL);
     338             :             }
     339             : 
     340           0 :             if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
     341           0 :                 return ret;
     342             : 
     343           0 :             cdg_scroll(cc, cdg_data, frame, inst == CDG_INST_SCROLL_COPY);
     344           0 :             av_frame_unref(cc->frame);
     345           0 :             ret = av_frame_ref(cc->frame, frame);
     346           0 :             if (ret < 0)
     347           0 :                 return ret;
     348           0 :             break;
     349           1 :         case CDG_INST_TRANSPARENT_COL:
     350           1 :             cc->transparency = cdg_data[0] & 0xF;
     351           1 :             break;
     352           0 :         default:
     353           0 :             break;
     354             :         }
     355             : 
     356         213 :         if (!frame->data[0]) {
     357         213 :             ret = av_frame_ref(frame, cc->frame);
     358         213 :             if (ret < 0)
     359           0 :                 return ret;
     360             :         }
     361         213 :         *got_frame = 1;
     362             :     } else {
     363           0 :         *got_frame = 0;
     364             :     }
     365             : 
     366         213 :     return avpkt->size;
     367             : }
     368             : 
     369           2 : static av_cold int cdg_decode_end(AVCodecContext *avctx)
     370             : {
     371           2 :     CDGraphicsContext *cc = avctx->priv_data;
     372             : 
     373           2 :     av_frame_free(&cc->frame);
     374             : 
     375           2 :     return 0;
     376             : }
     377             : 
     378             : AVCodec ff_cdgraphics_decoder = {
     379             :     .name           = "cdgraphics",
     380             :     .long_name      = NULL_IF_CONFIG_SMALL("CD Graphics video"),
     381             :     .type           = AVMEDIA_TYPE_VIDEO,
     382             :     .id             = AV_CODEC_ID_CDGRAPHICS,
     383             :     .priv_data_size = sizeof(CDGraphicsContext),
     384             :     .init           = cdg_decode_init,
     385             :     .close          = cdg_decode_end,
     386             :     .decode         = cdg_decode_frame,
     387             :     .capabilities   = AV_CODEC_CAP_DR1,
     388             : };

Generated by: LCOV version 1.13