LCOV - code coverage report
Current view: top level - libavcodec - dfa.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 136 249 54.6 %
Date: 2017-12-11 04:34:20 Functions: 6 11 54.5 %

          Line data    Source code
       1             : /*
       2             :  * Chronomaster DFA Video Decoder
       3             :  * Copyright (c) 2011 Konstantin Shishkov
       4             :  * based on work by Vladimir "VAG" Gneushev
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : #include <inttypes.h>
      24             : 
      25             : #include "avcodec.h"
      26             : #include "bytestream.h"
      27             : #include "internal.h"
      28             : 
      29             : #include "libavutil/avassert.h"
      30             : #include "libavutil/imgutils.h"
      31             : #include "libavutil/mem.h"
      32             : 
      33             : typedef struct DfaContext {
      34             :     uint32_t pal[256];
      35             :     uint8_t *frame_buf;
      36             : } DfaContext;
      37             : 
      38          22 : static av_cold int dfa_decode_init(AVCodecContext *avctx)
      39             : {
      40          22 :     DfaContext *s = avctx->priv_data;
      41             : 
      42          22 :     avctx->pix_fmt = AV_PIX_FMT_PAL8;
      43             : 
      44          22 :     if (!avctx->width || !avctx->height)
      45           0 :         return AVERROR_INVALIDDATA;
      46             : 
      47          22 :     av_assert0(av_image_check_size(avctx->width, avctx->height, 0, avctx) >= 0);
      48             : 
      49          22 :     s->frame_buf = av_mallocz(avctx->width * avctx->height);
      50          22 :     if (!s->frame_buf)
      51           0 :         return AVERROR(ENOMEM);
      52             : 
      53          22 :     return 0;
      54             : }
      55             : 
      56           0 : static int decode_copy(GetByteContext *gb, uint8_t *frame, int width, int height)
      57             : {
      58           0 :     const int size = width * height;
      59             : 
      60           0 :     if (bytestream2_get_buffer(gb, frame, size) != size)
      61           0 :         return AVERROR_INVALIDDATA;
      62           0 :     return 0;
      63             : }
      64             : 
      65          35 : static int decode_tsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
      66             : {
      67          35 :     const uint8_t *frame_start = frame;
      68          35 :     const uint8_t *frame_end   = frame + width * height;
      69          35 :     int mask = 0x10000, bitbuf = 0;
      70             :     int v, count;
      71             :     unsigned segments;
      72             :     unsigned offset;
      73             : 
      74          35 :     segments = bytestream2_get_le32(gb);
      75          35 :     offset   = bytestream2_get_le32(gb);
      76          35 :     if (segments == 0 && offset == frame_end - frame)
      77           1 :         return 0; // skip frame
      78          34 :     if (frame_end - frame <= offset)
      79           0 :         return AVERROR_INVALIDDATA;
      80          34 :     frame += offset;
      81      566949 :     while (segments--) {
      82      566883 :         if (bytestream2_get_bytes_left(gb) < 2)
      83           2 :             return AVERROR_INVALIDDATA;
      84      566881 :         if (mask == 0x10000) {
      85       35445 :             bitbuf = bytestream2_get_le16u(gb);
      86       35445 :             mask = 1;
      87             :         }
      88      566881 :         if (frame_end - frame < 2)
      89           0 :             return AVERROR_INVALIDDATA;
      90      566881 :         if (bitbuf & mask) {
      91      222937 :             v = bytestream2_get_le16(gb);
      92      222937 :             offset = (v & 0x1FFF) << 1;
      93      222937 :             count = ((v >> 13) + 2) << 1;
      94      222937 :             if (frame - frame_start < offset || frame_end - frame < count)
      95           0 :                 return AVERROR_INVALIDDATA;
      96      222937 :             av_memcpy_backptr(frame, offset, count);
      97      222937 :             frame += count;
      98             :         } else {
      99      343944 :             *frame++ = bytestream2_get_byte(gb);
     100      343944 :             *frame++ = bytestream2_get_byte(gb);
     101             :         }
     102      566881 :         mask <<= 1;
     103             :     }
     104             : 
     105          32 :     return 0;
     106             : }
     107             : 
     108         121 : static int decode_dsw1(GetByteContext *gb, uint8_t *frame, int width, int height)
     109             : {
     110         121 :     const uint8_t *frame_start = frame;
     111         121 :     const uint8_t *frame_end   = frame + width * height;
     112         121 :     int mask = 0x10000, bitbuf = 0;
     113             :     int v, offset, count, segments;
     114             : 
     115         121 :     segments = bytestream2_get_le16(gb);
     116      461083 :     while (segments--) {
     117      460846 :         if (bytestream2_get_bytes_left(gb) < 2)
     118           5 :             return AVERROR_INVALIDDATA;
     119      460841 :         if (mask == 0x10000) {
     120       57659 :             bitbuf = bytestream2_get_le16u(gb);
     121       57659 :             mask = 1;
     122             :         }
     123      460841 :         if (frame_end - frame < 2)
     124           0 :             return AVERROR_INVALIDDATA;
     125      460841 :         if (bitbuf & mask) {
     126      124238 :             v = bytestream2_get_le16(gb);
     127      124238 :             offset = (v & 0x1FFF) << 1;
     128      124238 :             count = ((v >> 13) + 2) << 1;
     129      124238 :             if (frame - frame_start < offset || frame_end - frame < count)
     130           0 :                 return AVERROR_INVALIDDATA;
     131      124238 :             av_memcpy_backptr(frame, offset, count);
     132      124238 :             frame += count;
     133      336603 :         } else if (bitbuf & (mask << 1)) {
     134       70168 :             frame += bytestream2_get_le16(gb);
     135             :         } else {
     136      266435 :             *frame++ = bytestream2_get_byte(gb);
     137      266435 :             *frame++ = bytestream2_get_byte(gb);
     138             :         }
     139      460841 :         mask <<= 2;
     140             :     }
     141             : 
     142         116 :     return 0;
     143             : }
     144             : 
     145           0 : static int decode_dds1(GetByteContext *gb, uint8_t *frame, int width, int height)
     146             : {
     147           0 :     const uint8_t *frame_start = frame;
     148           0 :     const uint8_t *frame_end   = frame + width * height;
     149           0 :     int mask = 0x10000, bitbuf = 0;
     150             :     int i, v, offset, count, segments;
     151             : 
     152           0 :     if ((width | height) & 1)
     153           0 :         return AVERROR_INVALIDDATA;
     154           0 :     segments = bytestream2_get_le16(gb);
     155           0 :     while (segments--) {
     156           0 :         if (bytestream2_get_bytes_left(gb) < 2)
     157           0 :             return AVERROR_INVALIDDATA;
     158           0 :         if (mask == 0x10000) {
     159           0 :             bitbuf = bytestream2_get_le16u(gb);
     160           0 :             mask = 1;
     161             :         }
     162             : 
     163           0 :         if (bitbuf & mask) {
     164           0 :             v = bytestream2_get_le16(gb);
     165           0 :             offset = (v & 0x1FFF) << 2;
     166           0 :             count = ((v >> 13) + 2) << 1;
     167           0 :             if (frame - frame_start < offset || frame_end - frame < count*2 + width)
     168           0 :                 return AVERROR_INVALIDDATA;
     169           0 :             for (i = 0; i < count; i++) {
     170           0 :                 frame[0] = frame[1] =
     171           0 :                 frame[width] = frame[width + 1] = frame[-offset];
     172             : 
     173           0 :                 frame += 2;
     174             :             }
     175           0 :         } else if (bitbuf & (mask << 1)) {
     176           0 :             v = bytestream2_get_le16(gb)*2;
     177           0 :             if (frame - frame_end < v)
     178           0 :                 return AVERROR_INVALIDDATA;
     179           0 :             frame += v;
     180             :         } else {
     181           0 :             if (width < 4 || frame_end - frame < width + 4)
     182           0 :                 return AVERROR_INVALIDDATA;
     183           0 :             frame[0] = frame[1] =
     184           0 :             frame[width] = frame[width + 1] =  bytestream2_get_byte(gb);
     185           0 :             frame += 2;
     186           0 :             frame[0] = frame[1] =
     187           0 :             frame[width] = frame[width + 1] =  bytestream2_get_byte(gb);
     188           0 :             frame += 2;
     189             :         }
     190           0 :         mask <<= 2;
     191             :     }
     192             : 
     193           0 :     return 0;
     194             : }
     195             : 
     196           0 : static int decode_bdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
     197             : {
     198             :     uint8_t *line_ptr;
     199             :     int count, lines, segments;
     200             : 
     201           0 :     count = bytestream2_get_le16(gb);
     202           0 :     if (count >= height)
     203           0 :         return AVERROR_INVALIDDATA;
     204           0 :     frame += width * count;
     205           0 :     lines = bytestream2_get_le16(gb);
     206           0 :     if (count + lines > height)
     207           0 :         return AVERROR_INVALIDDATA;
     208             : 
     209           0 :     while (lines--) {
     210           0 :         if (bytestream2_get_bytes_left(gb) < 1)
     211           0 :             return AVERROR_INVALIDDATA;
     212           0 :         line_ptr = frame;
     213           0 :         frame += width;
     214           0 :         segments = bytestream2_get_byteu(gb);
     215           0 :         while (segments--) {
     216           0 :             if (frame - line_ptr <= bytestream2_peek_byte(gb))
     217           0 :                 return AVERROR_INVALIDDATA;
     218           0 :             line_ptr += bytestream2_get_byte(gb);
     219           0 :             count = (int8_t)bytestream2_get_byte(gb);
     220           0 :             if (count >= 0) {
     221           0 :                 if (frame - line_ptr < count)
     222           0 :                     return AVERROR_INVALIDDATA;
     223           0 :                 if (bytestream2_get_buffer(gb, line_ptr, count) != count)
     224           0 :                     return AVERROR_INVALIDDATA;
     225             :             } else {
     226           0 :                 count = -count;
     227           0 :                 if (frame - line_ptr < count)
     228           0 :                     return AVERROR_INVALIDDATA;
     229           0 :                 memset(line_ptr, bytestream2_get_byte(gb), count);
     230             :             }
     231           0 :             line_ptr += count;
     232             :         }
     233             :     }
     234             : 
     235           0 :     return 0;
     236             : }
     237             : 
     238          11 : static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
     239             : {
     240          11 :     const uint8_t *frame_end   = frame + width * height;
     241             :     uint8_t *line_ptr;
     242             :     int count, i, v, lines, segments;
     243          11 :     int y = 0;
     244             : 
     245          11 :     lines = bytestream2_get_le16(gb);
     246          11 :     if (lines > height)
     247           0 :         return AVERROR_INVALIDDATA;
     248             : 
     249        1459 :     while (lines--) {
     250        1438 :         if (bytestream2_get_bytes_left(gb) < 2)
     251           0 :             return AVERROR_INVALIDDATA;
     252        1438 :         segments = bytestream2_get_le16u(gb);
     253        2890 :         while ((segments & 0xC000) == 0xC000) {
     254          14 :             unsigned skip_lines = -(int16_t)segments;
     255          14 :             int64_t delta = -((int16_t)segments * (int64_t)width);
     256          14 :             if (frame_end - frame <= delta || y + lines + skip_lines > height)
     257           0 :                 return AVERROR_INVALIDDATA;
     258          14 :             frame    += delta;
     259          14 :             y        += skip_lines;
     260          14 :             segments = bytestream2_get_le16(gb);
     261             :         }
     262             : 
     263        1438 :         if (frame_end <= frame)
     264           0 :             return AVERROR_INVALIDDATA;
     265        1438 :         if (segments & 0x8000) {
     266           0 :             frame[width - 1] = segments & 0xFF;
     267           0 :             segments = bytestream2_get_le16(gb);
     268             :         }
     269        1438 :         line_ptr = frame;
     270        1438 :         if (frame_end - frame < width)
     271           0 :             return AVERROR_INVALIDDATA;
     272        1438 :         frame += width;
     273        1438 :         y++;
     274        8015 :         while (segments--) {
     275        5140 :             if (frame - line_ptr <= bytestream2_peek_byte(gb))
     276           0 :                 return AVERROR_INVALIDDATA;
     277        5140 :             line_ptr += bytestream2_get_byte(gb);
     278        5140 :             count = (int8_t)bytestream2_get_byte(gb);
     279        5140 :             if (count >= 0) {
     280        4691 :                 if (frame - line_ptr < count * 2)
     281           0 :                     return AVERROR_INVALIDDATA;
     282        4691 :                 if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)
     283           1 :                     return AVERROR_INVALIDDATA;
     284        4690 :                 line_ptr += count * 2;
     285             :             } else {
     286         449 :                 count = -count;
     287         449 :                 if (frame - line_ptr < count * 2)
     288           0 :                     return AVERROR_INVALIDDATA;
     289         449 :                 v = bytestream2_get_le16(gb);
     290       17044 :                 for (i = 0; i < count; i++)
     291       16595 :                     bytestream_put_le16(&line_ptr, v);
     292             :             }
     293             :         }
     294             :     }
     295             : 
     296          10 :     return 0;
     297             : }
     298             : 
     299           0 : static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
     300             : {
     301           0 :     const uint8_t *frame_end = frame + width * height;
     302           0 :     uint32_t segments = bytestream2_get_le32(gb);
     303             :     int skip, copy;
     304             : 
     305           0 :     while (segments--) {
     306           0 :         if (bytestream2_get_bytes_left(gb) < 2)
     307           0 :             return AVERROR_INVALIDDATA;
     308           0 :         copy = bytestream2_get_byteu(gb) * 2;
     309           0 :         skip = bytestream2_get_byteu(gb) * 2;
     310           0 :         if (frame_end - frame < copy + skip ||
     311           0 :             bytestream2_get_bytes_left(gb) < copy)
     312           0 :             return AVERROR_INVALIDDATA;
     313           0 :         frame += skip;
     314           0 :         bytestream2_get_buffer(gb, frame, copy);
     315           0 :         frame += copy;
     316             :     }
     317             : 
     318           0 :     return 0;
     319             : }
     320             : 
     321           0 : static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
     322             : {
     323           0 :     memset(frame, 0, width * height);
     324           0 :     return 0;
     325             : }
     326             : 
     327             : 
     328             : typedef int (*chunk_decoder)(GetByteContext *gb, uint8_t *frame, int width, int height);
     329             : 
     330             : static const chunk_decoder decoder[8] = {
     331             :     decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
     332             :     decode_tdlt, decode_dsw1, decode_blck, decode_dds1,
     333             : };
     334             : 
     335             : static const char * const chunk_name[8] = {
     336             :     "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
     337             : };
     338             : 
     339         172 : static int dfa_decode_frame(AVCodecContext *avctx,
     340             :                             void *data, int *got_frame,
     341             :                             AVPacket *avpkt)
     342             : {
     343         172 :     AVFrame *frame = data;
     344         172 :     DfaContext *s = avctx->priv_data;
     345             :     GetByteContext gb;
     346         172 :     const uint8_t *buf = avpkt->data;
     347             :     uint32_t chunk_type, chunk_size;
     348             :     uint8_t *dst;
     349             :     int ret;
     350             :     int i, pal_elems;
     351         172 :     int version = avctx->extradata_size==2 ? AV_RL16(avctx->extradata) : 0;
     352             : 
     353         172 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
     354           0 :         return ret;
     355             : 
     356         172 :     bytestream2_init(&gb, avpkt->data, avpkt->size);
     357         515 :     while (bytestream2_get_bytes_left(&gb) > 0) {
     358         343 :         bytestream2_skip(&gb, 4);
     359         343 :         chunk_size = bytestream2_get_le32(&gb);
     360         343 :         chunk_type = bytestream2_get_le32(&gb);
     361         343 :         if (!chunk_type)
     362         164 :             break;
     363         179 :         if (chunk_type == 1) {
     364          12 :             pal_elems = FFMIN(chunk_size / 3, 256);
     365        3084 :             for (i = 0; i < pal_elems; i++) {
     366        3072 :                 s->pal[i] = bytestream2_get_be24(&gb) << 2;
     367        3072 :                 s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
     368             :             }
     369          12 :             frame->palette_has_changed = 1;
     370         167 :         } else if (chunk_type <= 9) {
     371         167 :             if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
     372           8 :                 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
     373           8 :                        chunk_name[chunk_type - 2]);
     374           8 :                 return AVERROR_INVALIDDATA;
     375             :             }
     376             :         } else {
     377           0 :             av_log(avctx, AV_LOG_WARNING,
     378             :                    "Ignoring unknown chunk type %"PRIu32"\n",
     379             :                    chunk_type);
     380             :         }
     381         171 :         buf += chunk_size;
     382             :     }
     383             : 
     384         164 :     buf = s->frame_buf;
     385         164 :     dst = frame->data[0];
     386       47294 :     for (i = 0; i < avctx->height; i++) {
     387       47130 :         if(version == 0x100) {
     388             :             int j;
     389           0 :             for(j = 0; j < avctx->width; j++) {
     390           0 :                 dst[j] = buf[ (i&3)*(avctx->width /4) + (j/4) +
     391           0 :                              ((j&3)*(avctx->height/4) + (i/4))*avctx->width];
     392             :             }
     393             :         } else {
     394       47130 :             memcpy(dst, buf, avctx->width);
     395       47130 :             buf += avctx->width;
     396             :         }
     397       47130 :         dst += frame->linesize[0];
     398             :     }
     399         164 :     memcpy(frame->data[1], s->pal, sizeof(s->pal));
     400             : 
     401         164 :     *got_frame = 1;
     402             : 
     403         164 :     return avpkt->size;
     404             : }
     405             : 
     406          22 : static av_cold int dfa_decode_end(AVCodecContext *avctx)
     407             : {
     408          22 :     DfaContext *s = avctx->priv_data;
     409             : 
     410          22 :     av_freep(&s->frame_buf);
     411             : 
     412          22 :     return 0;
     413             : }
     414             : 
     415             : AVCodec ff_dfa_decoder = {
     416             :     .name           = "dfa",
     417             :     .long_name      = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
     418             :     .type           = AVMEDIA_TYPE_VIDEO,
     419             :     .id             = AV_CODEC_ID_DFA,
     420             :     .priv_data_size = sizeof(DfaContext),
     421             :     .init           = dfa_decode_init,
     422             :     .close          = dfa_decode_end,
     423             :     .decode         = dfa_decode_frame,
     424             :     .capabilities   = AV_CODEC_CAP_DR1,
     425             : };

Generated by: LCOV version 1.13