LCOV - code coverage report
Current view: top level - libavcodec - dsicinvideo.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 87 171 50.9 %
Date: 2017-12-16 13:57:32 Functions: 7 9 77.8 %

          Line data    Source code
       1             : /*
       2             :  * Delphine Software International CIN video decoder
       3             :  * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
       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             : /**
      23             :  * @file
      24             :  * Delphine Software International CIN video decoder
      25             :  */
      26             : 
      27             : #include "avcodec.h"
      28             : #include "bytestream.h"
      29             : #include "internal.h"
      30             : 
      31             : typedef enum CinVideoBitmapIndex {
      32             :     CIN_CUR_BMP = 0, /* current */
      33             :     CIN_PRE_BMP = 1, /* previous */
      34             :     CIN_INT_BMP = 2  /* intermediate */
      35             : } CinVideoBitmapIndex;
      36             : 
      37             : typedef struct CinVideoContext {
      38             :     AVCodecContext *avctx;
      39             :     AVFrame *frame;
      40             :     unsigned int bitmap_size;
      41             :     uint32_t palette[256];
      42             :     uint8_t *bitmap_table[3];
      43             : } CinVideoContext;
      44             : 
      45           3 : static av_cold void destroy_buffers(CinVideoContext *cin)
      46             : {
      47             :     int i;
      48             : 
      49          12 :     for (i = 0; i < 3; ++i)
      50           9 :         av_freep(&cin->bitmap_table[i]);
      51           3 : }
      52             : 
      53           3 : static av_cold int allocate_buffers(CinVideoContext *cin)
      54             : {
      55             :     int i;
      56             : 
      57          12 :     for (i = 0; i < 3; ++i) {
      58           9 :         cin->bitmap_table[i] = av_mallocz(cin->bitmap_size);
      59           9 :         if (!cin->bitmap_table[i]) {
      60           0 :             av_log(cin->avctx, AV_LOG_ERROR, "Can't allocate bitmap buffers.\n");
      61           0 :             destroy_buffers(cin);
      62           0 :             return AVERROR(ENOMEM);
      63             :         }
      64             :     }
      65             : 
      66           3 :     return 0;
      67             : }
      68             : 
      69           3 : static av_cold int cinvideo_decode_init(AVCodecContext *avctx)
      70             : {
      71           3 :     CinVideoContext *cin = avctx->priv_data;
      72             : 
      73           3 :     cin->avctx = avctx;
      74           3 :     avctx->pix_fmt = AV_PIX_FMT_PAL8;
      75             : 
      76           3 :     cin->frame = av_frame_alloc();
      77           3 :     if (!cin->frame)
      78           0 :         return AVERROR(ENOMEM);
      79             : 
      80           3 :     cin->bitmap_size = avctx->width * avctx->height;
      81           3 :     if (allocate_buffers(cin))
      82           0 :         return AVERROR(ENOMEM);
      83             : 
      84           3 :     return 0;
      85             : }
      86             : 
      87           8 : static void cin_apply_delta_data(const unsigned char *src, unsigned char *dst,
      88             :                                  int size)
      89             : {
      90      409616 :     while (size--)
      91      409600 :         *dst++ += *src++;
      92           8 : }
      93             : 
      94           0 : static int cin_decode_huffman(const unsigned char *src, int src_size,
      95             :                               unsigned char *dst, int dst_size)
      96             : {
      97           0 :     int b, huff_code = 0;
      98             :     unsigned char huff_code_table[15];
      99           0 :     unsigned char *dst_cur       = dst;
     100           0 :     unsigned char *dst_end       = dst + dst_size;
     101           0 :     const unsigned char *src_end = src + src_size;
     102             : 
     103           0 :     memcpy(huff_code_table, src, 15);
     104           0 :     src += 15;
     105             : 
     106           0 :     while (src < src_end) {
     107           0 :         huff_code = *src++;
     108           0 :         if ((huff_code >> 4) == 15) {
     109           0 :             b          = huff_code << 4;
     110           0 :             huff_code  = *src++;
     111           0 :             *dst_cur++ = b | (huff_code >> 4);
     112             :         } else
     113           0 :             *dst_cur++ = huff_code_table[huff_code >> 4];
     114           0 :         if (dst_cur >= dst_end)
     115           0 :             break;
     116             : 
     117           0 :         huff_code &= 15;
     118           0 :         if (huff_code == 15) {
     119           0 :             *dst_cur++ = *src++;
     120             :         } else
     121           0 :             *dst_cur++ = huff_code_table[huff_code];
     122           0 :         if (dst_cur >= dst_end)
     123           0 :             break;
     124             :     }
     125             : 
     126           0 :     return dst_cur - dst;
     127             : }
     128             : 
     129          92 : static int cin_decode_lzss(const unsigned char *src, int src_size,
     130             :                            unsigned char *dst, int dst_size)
     131             : {
     132             :     uint16_t cmd;
     133             :     int i, sz, offset, code;
     134          92 :     unsigned char *dst_end       = dst + dst_size, *dst_start = dst;
     135          92 :     const unsigned char *src_end = src + src_size;
     136             : 
     137       50002 :     while (src < src_end && dst < dst_end) {
     138       49818 :         code = *src++;
     139      448086 :         for (i = 0; i < 8 && src < src_end && dst < dst_end; ++i) {
     140      398268 :             if (code & (1 << i)) {
     141       86707 :                 *dst++ = *src++;
     142             :             } else {
     143      311561 :                 cmd    = AV_RL16(src);
     144      311561 :                 src   += 2;
     145      311561 :                 offset = cmd >> 4;
     146      311561 :                 if ((int)(dst - dst_start) < offset + 1)
     147           0 :                     return AVERROR_INVALIDDATA;
     148      311561 :                 sz = (cmd & 0xF) + 2;
     149             :                 /* don't use memcpy/memmove here as the decoding routine
     150             :                  * (ab)uses buffer overlappings to repeat bytes in the
     151             :                  * destination */
     152      311561 :                 sz = FFMIN(sz, dst_end - dst);
     153     5201569 :                 while (sz--) {
     154     4578447 :                     *dst = *(dst - offset - 1);
     155     4578447 :                     ++dst;
     156             :                 }
     157             :             }
     158             :         }
     159             :     }
     160             : 
     161          92 :     return 0;
     162             : }
     163             : 
     164           0 : static int cin_decode_rle(const unsigned char *src, int src_size,
     165             :                            unsigned char *dst, int dst_size)
     166             : {
     167             :     int len, code;
     168           0 :     unsigned char *dst_end       = dst + dst_size;
     169           0 :     const unsigned char *src_end = src + src_size;
     170             : 
     171           0 :     while (src + 1 < src_end && dst < dst_end) {
     172           0 :         code = *src++;
     173           0 :         if (code & 0x80) {
     174           0 :             len = code - 0x7F;
     175           0 :             memset(dst, *src++, FFMIN(len, dst_end - dst));
     176             :         } else {
     177           0 :             len = code + 1;
     178           0 :             if (len > src_end-src) {
     179           0 :                 av_log(NULL, AV_LOG_ERROR, "RLE overread\n");
     180           0 :                 return AVERROR_INVALIDDATA;
     181             :             }
     182           0 :             memcpy(dst, src, FFMIN3(len, dst_end - dst, src_end - src));
     183           0 :             src += len;
     184             :         }
     185           0 :         dst += len;
     186             :     }
     187           0 :     return 0;
     188             : }
     189             : 
     190          92 : static int cinvideo_decode_frame(AVCodecContext *avctx,
     191             :                                  void *data, int *got_frame,
     192             :                                  AVPacket *avpkt)
     193             : {
     194          92 :     const uint8_t *buf   = avpkt->data;
     195          92 :     int buf_size         = avpkt->size;
     196          92 :     CinVideoContext *cin = avctx->priv_data;
     197             :     int i, y, palette_type, palette_colors_count,
     198          92 :         bitmap_frame_type, bitmap_frame_size, res = 0;
     199             : 
     200          92 :     palette_type         = buf[0];
     201          92 :     palette_colors_count = AV_RL16(buf + 1);
     202          92 :     bitmap_frame_type    = buf[3];
     203          92 :     buf                 += 4;
     204             : 
     205          92 :     bitmap_frame_size = buf_size - 4;
     206             : 
     207             :     /* handle palette */
     208          92 :     if (bitmap_frame_size < palette_colors_count * (3 + (palette_type != 0)))
     209           0 :         return AVERROR_INVALIDDATA;
     210          92 :     if (palette_type == 0) {
     211          92 :         if (palette_colors_count > 256)
     212           0 :             return AVERROR_INVALIDDATA;
     213       11356 :         for (i = 0; i < palette_colors_count; ++i) {
     214       11264 :             cin->palette[i]    = 0xFFU << 24 | bytestream_get_le24(&buf);
     215       11264 :             bitmap_frame_size -= 3;
     216             :         }
     217             :     } else {
     218           0 :         for (i = 0; i < palette_colors_count; ++i) {
     219           0 :             cin->palette[buf[0]] = 0xFFU << 24 | AV_RL24(buf + 1);
     220           0 :             buf                 += 4;
     221           0 :             bitmap_frame_size   -= 4;
     222             :         }
     223             :     }
     224             : 
     225             :     /* note: the decoding routines below assumes that
     226             :      * surface.width = surface.pitch */
     227          92 :     switch (bitmap_frame_type) {
     228           0 :     case 9:
     229           0 :         cin_decode_rle(buf, bitmap_frame_size,
     230           0 :                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
     231           0 :         break;
     232           0 :     case 34:
     233           0 :         cin_decode_rle(buf, bitmap_frame_size,
     234           0 :                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
     235           0 :         cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
     236           0 :                              cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
     237           0 :         break;
     238           0 :     case 35:
     239           0 :         bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
     240           0 :                            cin->bitmap_table[CIN_INT_BMP], cin->bitmap_size);
     241           0 :         cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
     242           0 :                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
     243           0 :         break;
     244           0 :     case 36:
     245           0 :         bitmap_frame_size = cin_decode_huffman(buf, bitmap_frame_size,
     246           0 :                                                cin->bitmap_table[CIN_INT_BMP],
     247           0 :                                                cin->bitmap_size);
     248           0 :         cin_decode_rle(cin->bitmap_table[CIN_INT_BMP], bitmap_frame_size,
     249           0 :                        cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
     250           0 :         cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
     251           0 :                              cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
     252           0 :         break;
     253           0 :     case 37:
     254           0 :         cin_decode_huffman(buf, bitmap_frame_size,
     255           0 :                            cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
     256           0 :         break;
     257          84 :     case 38:
     258         168 :         res = cin_decode_lzss(buf, bitmap_frame_size,
     259          84 :                               cin->bitmap_table[CIN_CUR_BMP],
     260          84 :                               cin->bitmap_size);
     261          84 :         if (res < 0)
     262           0 :             return res;
     263          84 :         break;
     264           8 :     case 39:
     265          16 :         res = cin_decode_lzss(buf, bitmap_frame_size,
     266           8 :                               cin->bitmap_table[CIN_CUR_BMP],
     267           8 :                               cin->bitmap_size);
     268           8 :         if (res < 0)
     269           0 :             return res;
     270          16 :         cin_apply_delta_data(cin->bitmap_table[CIN_PRE_BMP],
     271          16 :                              cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_size);
     272           8 :         break;
     273             :     }
     274             : 
     275          92 :     if ((res = ff_reget_buffer(avctx, cin->frame)) < 0)
     276           0 :         return res;
     277             : 
     278          92 :     memcpy(cin->frame->data[1], cin->palette, sizeof(cin->palette));
     279          92 :     cin->frame->palette_has_changed = 1;
     280       14812 :     for (y = 0; y < cin->avctx->height; ++y)
     281       29440 :         memcpy(cin->frame->data[0] + (cin->avctx->height - 1 - y) * cin->frame->linesize[0],
     282       14720 :                cin->bitmap_table[CIN_CUR_BMP] + y * cin->avctx->width,
     283       14720 :                cin->avctx->width);
     284             : 
     285          92 :     FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP],
     286             :                       cin->bitmap_table[CIN_PRE_BMP]);
     287             : 
     288          92 :     if ((res = av_frame_ref(data, cin->frame)) < 0)
     289           0 :         return res;
     290             : 
     291          92 :     *got_frame = 1;
     292             : 
     293          92 :     return buf_size;
     294             : }
     295             : 
     296           3 : static av_cold int cinvideo_decode_end(AVCodecContext *avctx)
     297             : {
     298           3 :     CinVideoContext *cin = avctx->priv_data;
     299             : 
     300           3 :     av_frame_free(&cin->frame);
     301             : 
     302           3 :     destroy_buffers(cin);
     303             : 
     304           3 :     return 0;
     305             : }
     306             : 
     307             : AVCodec ff_dsicinvideo_decoder = {
     308             :     .name           = "dsicinvideo",
     309             :     .long_name      = NULL_IF_CONFIG_SMALL("Delphine Software International CIN video"),
     310             :     .type           = AVMEDIA_TYPE_VIDEO,
     311             :     .id             = AV_CODEC_ID_DSICINVIDEO,
     312             :     .priv_data_size = sizeof(CinVideoContext),
     313             :     .init           = cinvideo_decode_init,
     314             :     .close          = cinvideo_decode_end,
     315             :     .decode         = cinvideo_decode_frame,
     316             :     .capabilities   = AV_CODEC_CAP_DR1,
     317             : };

Generated by: LCOV version 1.13