LCOV - code coverage report
Current view: top level - libavcodec - smacker.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 312 488 63.9 %
Date: 2017-12-16 01:21:47 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Smacker decoder
       3             :  * Copyright (c) 2006 Konstantin Shishkov
       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             :  * Smacker decoder
      25             :  */
      26             : 
      27             : /*
      28             :  * Based on http://wiki.multimedia.cx/index.php?title=Smacker
      29             :  */
      30             : 
      31             : #include <stdio.h>
      32             : #include <stdlib.h>
      33             : 
      34             : #include "libavutil/channel_layout.h"
      35             : 
      36             : #define BITSTREAM_READER_LE
      37             : #include "avcodec.h"
      38             : #include "bytestream.h"
      39             : #include "get_bits.h"
      40             : #include "internal.h"
      41             : #include "mathops.h"
      42             : 
      43             : #define SMKTREE_BITS 9
      44             : #define SMK_NODE 0x80000000
      45             : 
      46             : #define SMKTREE_DECODE_MAX_RECURSION 32
      47             : #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
      48             : 
      49             : typedef struct SmackVContext {
      50             :     AVCodecContext *avctx;
      51             :     AVFrame *pic;
      52             : 
      53             :     int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
      54             :     int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
      55             : } SmackVContext;
      56             : 
      57             : /**
      58             :  * Context used for code reconstructing
      59             :  */
      60             : typedef struct HuffContext {
      61             :     int length;
      62             :     int maxlength;
      63             :     int current;
      64             :     uint32_t *bits;
      65             :     int *lengths;
      66             :     int *values;
      67             : } HuffContext;
      68             : 
      69             : /* common parameters used for decode_bigtree */
      70             : typedef struct DBCtx {
      71             :     VLC *v1, *v2;
      72             :     int *recode1, *recode2;
      73             :     int escapes[3];
      74             :     int *last;
      75             :     int lcur;
      76             : } DBCtx;
      77             : 
      78             : /* possible runs of blocks */
      79             : static const int block_runs[64] = {
      80             :       1,    2,    3,    4,    5,    6,    7,    8,
      81             :       9,   10,   11,   12,   13,   14,   15,   16,
      82             :      17,   18,   19,   20,   21,   22,   23,   24,
      83             :      25,   26,   27,   28,   29,   30,   31,   32,
      84             :      33,   34,   35,   36,   37,   38,   39,   40,
      85             :      41,   42,   43,   44,   45,   46,   47,   48,
      86             :      49,   50,   51,   52,   53,   54,   55,   56,
      87             :      57,   58,   59,  128,  256,  512, 1024, 2048 };
      88             : 
      89             : enum SmkBlockTypes {
      90             :     SMK_BLK_MONO = 0,
      91             :     SMK_BLK_FULL = 1,
      92             :     SMK_BLK_SKIP = 2,
      93             :     SMK_BLK_FILL = 3 };
      94             : 
      95             : /**
      96             :  * Decode local frame tree
      97             :  */
      98        9904 : static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
      99             : {
     100        9904 :     if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
     101           0 :         av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
     102           0 :         return AVERROR_INVALIDDATA;
     103             :     }
     104             : 
     105        9904 :     if(!get_bits1(gb)){ //Leaf
     106        5007 :         if(hc->current >= hc->length){
     107           0 :             av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
     108           0 :             return AVERROR_INVALIDDATA;
     109             :         }
     110        5007 :         if(length){
     111        5001 :             hc->bits[hc->current] = prefix;
     112        5001 :             hc->lengths[hc->current] = length;
     113             :         } else {
     114           6 :             hc->bits[hc->current] = 0;
     115           6 :             hc->lengths[hc->current] = 0;
     116             :         }
     117        5007 :         hc->values[hc->current] = get_bits(gb, 8);
     118        5007 :         hc->current++;
     119        5007 :         if(hc->maxlength < length)
     120         367 :             hc->maxlength = length;
     121        5007 :         return 0;
     122             :     } else { //Node
     123             :         int r;
     124        4897 :         length++;
     125        4897 :         r = smacker_decode_tree(gb, hc, prefix, length);
     126        4897 :         if(r)
     127           0 :             return r;
     128        4897 :         return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
     129             :     }
     130             : }
     131             : 
     132             : /**
     133             :  * Decode header tree
     134             :  */
     135       81192 : static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc,
     136             :                                   DBCtx *ctx, int length)
     137             : {
     138             :     // Larger length can cause segmentation faults due to too deep recursion.
     139       81192 :     if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
     140           0 :         av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
     141           0 :         return AVERROR_INVALIDDATA;
     142             :     }
     143             : 
     144       81192 :     if (hc->current + 1 >= hc->length) {
     145           0 :         av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
     146           0 :         return AVERROR_INVALIDDATA;
     147             :     }
     148       81192 :     if(!get_bits1(gb)){ //Leaf
     149             :         int val, i1, i2;
     150       40602 :         i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
     151       40602 :         i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
     152       40602 :         if (i1 < 0 || i2 < 0)
     153           0 :             return AVERROR_INVALIDDATA;
     154       40602 :         val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
     155       40602 :         if(val == ctx->escapes[0]) {
     156           9 :             ctx->last[0] = hc->current;
     157           9 :             val = 0;
     158       40593 :         } else if(val == ctx->escapes[1]) {
     159          12 :             ctx->last[1] = hc->current;
     160          12 :             val = 0;
     161       40581 :         } else if(val == ctx->escapes[2]) {
     162          12 :             ctx->last[2] = hc->current;
     163          12 :             val = 0;
     164             :         }
     165             : 
     166       40602 :         hc->values[hc->current++] = val;
     167       40602 :         return 1;
     168             :     } else { //Node
     169       40590 :         int r = 0, r_new, t;
     170             : 
     171       40590 :         t = hc->current++;
     172       40590 :         r = smacker_decode_bigtree(gb, hc, ctx, length + 1);
     173       40590 :         if(r < 0)
     174           0 :             return r;
     175       40590 :         hc->values[t] = SMK_NODE | r;
     176       40590 :         r++;
     177       40590 :         r_new = smacker_decode_bigtree(gb, hc, ctx, length + 1);
     178       40590 :         if (r_new < 0)
     179           0 :             return r_new;
     180       40590 :         return r + r_new;
     181             :     }
     182             : }
     183             : 
     184             : /**
     185             :  * Store large tree as FFmpeg's vlc codes
     186             :  */
     187          12 : static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
     188             : {
     189             :     int res;
     190             :     HuffContext huff;
     191             :     HuffContext tmp1, tmp2;
     192          12 :     VLC vlc[2] = { { 0 } };
     193             :     int escapes[3];
     194             :     DBCtx ctx;
     195          12 :     int err = 0;
     196             : 
     197          12 :     if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
     198           0 :         av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
     199           0 :         return AVERROR_INVALIDDATA;
     200             :     }
     201             : 
     202          12 :     tmp1.length = 256;
     203          12 :     tmp1.maxlength = 0;
     204          12 :     tmp1.current = 0;
     205          12 :     tmp1.bits = av_mallocz(256 * 4);
     206          12 :     tmp1.lengths = av_mallocz(256 * sizeof(int));
     207          12 :     tmp1.values = av_mallocz(256 * sizeof(int));
     208             : 
     209          12 :     tmp2.length = 256;
     210          12 :     tmp2.maxlength = 0;
     211          12 :     tmp2.current = 0;
     212          12 :     tmp2.bits = av_mallocz(256 * 4);
     213          12 :     tmp2.lengths = av_mallocz(256 * sizeof(int));
     214          12 :     tmp2.values = av_mallocz(256 * sizeof(int));
     215          24 :     if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
     216          24 :         !tmp2.bits || !tmp2.lengths || !tmp2.values) {
     217           0 :         err = AVERROR(ENOMEM);
     218           0 :         goto error;
     219             :     }
     220             : 
     221          12 :     if(get_bits1(gb)) {
     222          12 :         res = smacker_decode_tree(gb, &tmp1, 0, 0);
     223          12 :         if (res < 0) {
     224           0 :             err = res;
     225           0 :             goto error;
     226             :         }
     227          12 :         skip_bits1(gb);
     228          12 :         if(tmp1.current > 1) {
     229          12 :             res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
     230             :                         tmp1.lengths, sizeof(int), sizeof(int),
     231             :                         tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
     232          12 :             if(res < 0) {
     233           0 :                 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
     234           0 :                 err = res;
     235           0 :                 goto error;
     236             :             }
     237             :         }
     238             :     }
     239          12 :     if (!vlc[0].table) {
     240           0 :         av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
     241             :     }
     242          12 :     if(get_bits1(gb)){
     243          12 :         res = smacker_decode_tree(gb, &tmp2, 0, 0);
     244          12 :         if (res < 0) {
     245           0 :             err = res;
     246           0 :             goto error;
     247             :         }
     248          12 :         skip_bits1(gb);
     249          12 :         if(tmp2.current > 1) {
     250          12 :             res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
     251             :                         tmp2.lengths, sizeof(int), sizeof(int),
     252             :                         tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
     253          12 :             if(res < 0) {
     254           0 :                 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
     255           0 :                 err = res;
     256           0 :                 goto error;
     257             :             }
     258             :         }
     259             :     }
     260          12 :     if (!vlc[1].table) {
     261           0 :         av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
     262             :     }
     263             : 
     264          12 :     escapes[0]  = get_bits(gb, 16);
     265          12 :     escapes[1]  = get_bits(gb, 16);
     266          12 :     escapes[2]  = get_bits(gb, 16);
     267             : 
     268          12 :     last[0] = last[1] = last[2] = -1;
     269             : 
     270          12 :     ctx.escapes[0] = escapes[0];
     271          12 :     ctx.escapes[1] = escapes[1];
     272          12 :     ctx.escapes[2] = escapes[2];
     273          12 :     ctx.v1 = &vlc[0];
     274          12 :     ctx.v2 = &vlc[1];
     275          12 :     ctx.recode1 = tmp1.values;
     276          12 :     ctx.recode2 = tmp2.values;
     277          12 :     ctx.last = last;
     278             : 
     279          12 :     huff.length = ((size + 3) >> 2) + 4;
     280          12 :     huff.maxlength = 0;
     281          12 :     huff.current = 0;
     282          12 :     huff.values = av_mallocz_array(huff.length, sizeof(int));
     283          12 :     if (!huff.values) {
     284           0 :         err = AVERROR(ENOMEM);
     285           0 :         goto error;
     286             :     }
     287             : 
     288          12 :     res = smacker_decode_bigtree(gb, &huff, &ctx, 0);
     289          12 :     if (res < 0)
     290           0 :         err = res;
     291          12 :     skip_bits1(gb);
     292          12 :     if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
     293          12 :     if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
     294          12 :     if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
     295          24 :     if (ctx.last[0] >= huff.length ||
     296          24 :         ctx.last[1] >= huff.length ||
     297          12 :         ctx.last[2] >= huff.length) {
     298           0 :         av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
     299           0 :         err = AVERROR_INVALIDDATA;
     300             :     }
     301             : 
     302          12 :     *recodes = huff.values;
     303             : 
     304          12 : error:
     305          12 :     if(vlc[0].table)
     306          12 :         ff_free_vlc(&vlc[0]);
     307          12 :     if(vlc[1].table)
     308          12 :         ff_free_vlc(&vlc[1]);
     309          12 :     av_free(tmp1.bits);
     310          12 :     av_free(tmp1.lengths);
     311          12 :     av_free(tmp1.values);
     312          12 :     av_free(tmp2.bits);
     313          12 :     av_free(tmp2.lengths);
     314          12 :     av_free(tmp2.values);
     315             : 
     316          12 :     return err;
     317             : }
     318             : 
     319           3 : static int decode_header_trees(SmackVContext *smk) {
     320             :     GetBitContext gb;
     321             :     int mmap_size, mclr_size, full_size, type_size, ret;
     322             : 
     323           3 :     mmap_size = AV_RL32(smk->avctx->extradata);
     324           3 :     mclr_size = AV_RL32(smk->avctx->extradata + 4);
     325           3 :     full_size = AV_RL32(smk->avctx->extradata + 8);
     326           3 :     type_size = AV_RL32(smk->avctx->extradata + 12);
     327             : 
     328           3 :     ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
     329           3 :     if (ret < 0)
     330           0 :         return ret;
     331             : 
     332           3 :     if(!get_bits1(&gb)) {
     333           0 :         av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
     334           0 :         smk->mmap_tbl = av_malloc(sizeof(int) * 2);
     335           0 :         if (!smk->mmap_tbl)
     336           0 :             return AVERROR(ENOMEM);
     337           0 :         smk->mmap_tbl[0] = 0;
     338           0 :         smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
     339             :     } else {
     340           3 :         ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
     341           3 :         if (ret < 0)
     342           0 :             return ret;
     343             :     }
     344           3 :     if(!get_bits1(&gb)) {
     345           0 :         av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
     346           0 :         smk->mclr_tbl = av_malloc(sizeof(int) * 2);
     347           0 :         if (!smk->mclr_tbl)
     348           0 :             return AVERROR(ENOMEM);
     349           0 :         smk->mclr_tbl[0] = 0;
     350           0 :         smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
     351             :     } else {
     352           3 :         ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
     353           3 :         if (ret < 0)
     354           0 :             return ret;
     355             :     }
     356           3 :     if(!get_bits1(&gb)) {
     357           0 :         av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
     358           0 :         smk->full_tbl = av_malloc(sizeof(int) * 2);
     359           0 :         if (!smk->full_tbl)
     360           0 :             return AVERROR(ENOMEM);
     361           0 :         smk->full_tbl[0] = 0;
     362           0 :         smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
     363             :     } else {
     364           3 :         ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
     365           3 :         if (ret < 0)
     366           0 :             return ret;
     367             :     }
     368           3 :     if(!get_bits1(&gb)) {
     369           0 :         av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
     370           0 :         smk->type_tbl = av_malloc(sizeof(int) * 2);
     371           0 :         if (!smk->type_tbl)
     372           0 :             return AVERROR(ENOMEM);
     373           0 :         smk->type_tbl[0] = 0;
     374           0 :         smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
     375             :     } else {
     376           3 :         ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
     377           3 :         if (ret < 0)
     378           0 :             return ret;
     379             :     }
     380             : 
     381           3 :     return 0;
     382             : }
     383             : 
     384         400 : static av_always_inline void last_reset(int *recode, int *last) {
     385         400 :     recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
     386         400 : }
     387             : 
     388             : /* get code and update history */
     389      721937 : static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
     390      721937 :     register int *table = recode;
     391             :     int v;
     392             : 
     393     6615806 :     while(*table & SMK_NODE) {
     394     5171932 :         if(get_bits1(gb))
     395     2453865 :             table += (*table) & (~SMK_NODE);
     396     5171932 :         table++;
     397             :     }
     398      721937 :     v = *table;
     399             : 
     400      721937 :     if(v != recode[last[0]]) {
     401      603120 :         recode[last[2]] = recode[last[1]];
     402      603120 :         recode[last[1]] = recode[last[0]];
     403      603120 :         recode[last[0]] = v;
     404             :     }
     405      721937 :     return v;
     406             : }
     407             : 
     408         100 : static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
     409             :                         AVPacket *avpkt)
     410             : {
     411         100 :     SmackVContext * const smk = avctx->priv_data;
     412             :     uint8_t *out;
     413             :     uint32_t *pal;
     414             :     GetByteContext gb2;
     415             :     GetBitContext gb;
     416             :     int blocks, blk, bw, bh;
     417             :     int i, ret;
     418             :     int stride;
     419             :     int flags;
     420             : 
     421         100 :     if (avpkt->size <= 769)
     422           0 :         return AVERROR_INVALIDDATA;
     423             : 
     424         100 :     if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0)
     425           0 :         return ret;
     426             : 
     427             :     /* make the palette available on the way out */
     428         100 :     pal = (uint32_t*)smk->pic->data[1];
     429         100 :     bytestream2_init(&gb2, avpkt->data, avpkt->size);
     430         100 :     flags = bytestream2_get_byteu(&gb2);
     431         100 :     smk->pic->palette_has_changed = flags & 1;
     432         100 :     smk->pic->key_frame = !!(flags & 2);
     433         100 :     if (smk->pic->key_frame)
     434           0 :         smk->pic->pict_type = AV_PICTURE_TYPE_I;
     435             :     else
     436         100 :         smk->pic->pict_type = AV_PICTURE_TYPE_P;
     437             : 
     438       25700 :     for(i = 0; i < 256; i++)
     439       25600 :         *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
     440             : 
     441         100 :     last_reset(smk->mmap_tbl, smk->mmap_last);
     442         100 :     last_reset(smk->mclr_tbl, smk->mclr_last);
     443         100 :     last_reset(smk->full_tbl, smk->full_last);
     444         100 :     last_reset(smk->type_tbl, smk->type_last);
     445         100 :     if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
     446           0 :         return ret;
     447             : 
     448         100 :     blk = 0;
     449         100 :     bw = avctx->width >> 2;
     450         100 :     bh = avctx->height >> 2;
     451         100 :     blocks = bw * bh;
     452         100 :     stride = smk->pic->linesize[0];
     453       44793 :     while(blk < blocks) {
     454             :         int type, run, mode;
     455             :         uint16_t pix;
     456             : 
     457       44593 :         type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
     458       44593 :         run = block_runs[(type >> 2) & 0x3F];
     459       44593 :         switch(type & 3){
     460       15331 :         case SMK_BLK_MONO:
     461       59010 :             while(run-- && blk < blocks){
     462             :                 int clr, map;
     463             :                 int hi, lo;
     464       28348 :                 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
     465       28348 :                 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
     466       28348 :                 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
     467       28348 :                 hi = clr >> 8;
     468       28348 :                 lo = clr & 0xFF;
     469      141740 :                 for(i = 0; i < 4; i++) {
     470      113392 :                     if(map & 1) out[0] = hi; else out[0] = lo;
     471      113392 :                     if(map & 2) out[1] = hi; else out[1] = lo;
     472      113392 :                     if(map & 4) out[2] = hi; else out[2] = lo;
     473      113392 :                     if(map & 8) out[3] = hi; else out[3] = lo;
     474      113392 :                     map >>= 4;
     475      113392 :                     out += stride;
     476             :                 }
     477       28348 :                 blk++;
     478             :             }
     479       15331 :             break;
     480       11617 :         case SMK_BLK_FULL:
     481       11617 :             mode = 0;
     482       11617 :             if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
     483           0 :                 if(get_bits1(&gb)) mode = 1;
     484           0 :                 else if(get_bits1(&gb)) mode = 2;
     485             :             }
     486      100815 :             while(run-- && blk < blocks){
     487       77581 :                 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
     488       77581 :                 switch(mode){
     489       77581 :                 case 0:
     490      387905 :                     for(i = 0; i < 4; i++) {
     491      310324 :                         pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
     492      310324 :                         AV_WL16(out+2,pix);
     493      310324 :                         pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
     494      310324 :                         AV_WL16(out,pix);
     495      310324 :                         out += stride;
     496             :                     }
     497       77581 :                     break;
     498           0 :                 case 1:
     499           0 :                     pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
     500           0 :                     out[0] = out[1] = pix & 0xFF;
     501           0 :                     out[2] = out[3] = pix >> 8;
     502           0 :                     out += stride;
     503           0 :                     out[0] = out[1] = pix & 0xFF;
     504           0 :                     out[2] = out[3] = pix >> 8;
     505           0 :                     out += stride;
     506           0 :                     pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
     507           0 :                     out[0] = out[1] = pix & 0xFF;
     508           0 :                     out[2] = out[3] = pix >> 8;
     509           0 :                     out += stride;
     510           0 :                     out[0] = out[1] = pix & 0xFF;
     511           0 :                     out[2] = out[3] = pix >> 8;
     512           0 :                     break;
     513           0 :                 case 2:
     514           0 :                     for(i = 0; i < 2; i++) {
     515             :                         uint16_t pix1, pix2;
     516           0 :                         pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
     517           0 :                         pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
     518           0 :                         AV_WL16(out,pix1);
     519           0 :                         AV_WL16(out+2,pix2);
     520           0 :                         out += stride;
     521           0 :                         AV_WL16(out,pix1);
     522           0 :                         AV_WL16(out+2,pix2);
     523           0 :                         out += stride;
     524             :                     }
     525           0 :                     break;
     526             :                 }
     527       77581 :                 blk++;
     528             :             }
     529       11617 :             break;
     530       10001 :         case SMK_BLK_SKIP:
     531      298423 :             while(run-- && blk < blocks)
     532      278421 :                 blk++;
     533       10001 :             break;
     534        7644 :         case SMK_BLK_FILL:
     535        7644 :             mode = type >> 8;
     536       30938 :             while(run-- && blk < blocks){
     537             :                 uint32_t col;
     538       15650 :                 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
     539       15650 :                 col = mode * 0x01010101;
     540       78250 :                 for(i = 0; i < 4; i++) {
     541       62600 :                     *((uint32_t*)out) = col;
     542       62600 :                     out += stride;
     543             :                 }
     544       15650 :                 blk++;
     545             :             }
     546        7644 :             break;
     547             :         }
     548             : 
     549             :     }
     550             : 
     551         100 :     if ((ret = av_frame_ref(data, smk->pic)) < 0)
     552           0 :         return ret;
     553             : 
     554         100 :     *got_frame = 1;
     555             : 
     556             :     /* always report that the buffer was completely consumed */
     557         100 :     return avpkt->size;
     558             : }
     559             : 
     560             : 
     561           3 : static av_cold int decode_end(AVCodecContext *avctx)
     562             : {
     563           3 :     SmackVContext * const smk = avctx->priv_data;
     564             : 
     565           3 :     av_freep(&smk->mmap_tbl);
     566           3 :     av_freep(&smk->mclr_tbl);
     567           3 :     av_freep(&smk->full_tbl);
     568           3 :     av_freep(&smk->type_tbl);
     569             : 
     570           3 :     av_frame_free(&smk->pic);
     571             : 
     572           3 :     return 0;
     573             : }
     574             : 
     575             : 
     576           3 : static av_cold int decode_init(AVCodecContext *avctx)
     577             : {
     578           3 :     SmackVContext * const c = avctx->priv_data;
     579             :     int ret;
     580             : 
     581           3 :     c->avctx = avctx;
     582             : 
     583           3 :     avctx->pix_fmt = AV_PIX_FMT_PAL8;
     584             : 
     585           3 :     c->pic = av_frame_alloc();
     586           3 :     if (!c->pic)
     587           0 :         return AVERROR(ENOMEM);
     588             : 
     589             :     /* decode huffman trees from extradata */
     590           3 :     if(avctx->extradata_size < 16){
     591           0 :         av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
     592           0 :         decode_end(avctx);
     593           0 :         return AVERROR(EINVAL);
     594             :     }
     595             : 
     596           3 :     ret = decode_header_trees(c);
     597           3 :     if (ret < 0) {
     598           0 :         decode_end(avctx);
     599           0 :         return ret;
     600             :     }
     601             : 
     602           3 :     return 0;
     603             : }
     604             : 
     605             : 
     606           3 : static av_cold int smka_decode_init(AVCodecContext *avctx)
     607             : {
     608           3 :     if (avctx->channels < 1 || avctx->channels > 2) {
     609           0 :         av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
     610           0 :         return AVERROR_INVALIDDATA;
     611             :     }
     612           3 :     avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
     613           3 :     avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
     614             : 
     615           3 :     return 0;
     616             : }
     617             : 
     618             : /**
     619             :  * Decode Smacker audio data
     620             :  */
     621          86 : static int smka_decode_frame(AVCodecContext *avctx, void *data,
     622             :                              int *got_frame_ptr, AVPacket *avpkt)
     623             : {
     624          86 :     AVFrame *frame     = data;
     625          86 :     const uint8_t *buf = avpkt->data;
     626          86 :     int buf_size = avpkt->size;
     627             :     GetBitContext gb;
     628          86 :     HuffContext h[4] = { { 0 } };
     629          86 :     VLC vlc[4]       = { { 0 } };
     630             :     int16_t *samples;
     631             :     uint8_t *samples8;
     632             :     int val;
     633             :     int i, res, ret;
     634             :     int unp_size;
     635             :     int bits, stereo;
     636          86 :     int pred[2] = {0, 0};
     637             : 
     638          86 :     if (buf_size <= 4) {
     639           0 :         av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
     640           0 :         return AVERROR_INVALIDDATA;
     641             :     }
     642             : 
     643          86 :     unp_size = AV_RL32(buf);
     644             : 
     645          86 :     if (unp_size > (1U<<24)) {
     646           0 :         av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
     647           0 :         return AVERROR_INVALIDDATA;
     648             :     }
     649             : 
     650          86 :     if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
     651           0 :         return ret;
     652             : 
     653          86 :     if(!get_bits1(&gb)){
     654           0 :         av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
     655           0 :         *got_frame_ptr = 0;
     656           0 :         return 1;
     657             :     }
     658          86 :     stereo = get_bits1(&gb);
     659          86 :     bits = get_bits1(&gb);
     660          86 :     if (stereo ^ (avctx->channels != 1)) {
     661           0 :         av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
     662           0 :         return AVERROR_INVALIDDATA;
     663             :     }
     664          86 :     if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
     665           0 :         av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
     666           0 :         return AVERROR_INVALIDDATA;
     667             :     }
     668             : 
     669             :     /* get output buffer */
     670          86 :     frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
     671          86 :     if (unp_size % (avctx->channels * (bits + 1))) {
     672           0 :         av_log(avctx, AV_LOG_ERROR,
     673             :                "The buffer does not contain an integer number of samples\n");
     674           0 :         return AVERROR_INVALIDDATA;
     675             :     }
     676          86 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
     677           0 :         return ret;
     678          86 :     samples  = (int16_t *)frame->data[0];
     679          86 :     samples8 =            frame->data[0];
     680             : 
     681             :     // Initialize
     682         172 :     for(i = 0; i < (1 << (bits + stereo)); i++) {
     683          86 :         h[i].length = 256;
     684          86 :         h[i].maxlength = 0;
     685          86 :         h[i].current = 0;
     686          86 :         h[i].bits = av_mallocz(256 * 4);
     687          86 :         h[i].lengths = av_mallocz(256 * sizeof(int));
     688          86 :         h[i].values = av_mallocz(256 * sizeof(int));
     689          86 :         if (!h[i].bits || !h[i].lengths || !h[i].values) {
     690           0 :             ret = AVERROR(ENOMEM);
     691           0 :             goto error;
     692             :         }
     693          86 :         skip_bits1(&gb);
     694          86 :         if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
     695           0 :             ret = AVERROR_INVALIDDATA;
     696           0 :             goto error;
     697             :         }
     698          86 :         skip_bits1(&gb);
     699          86 :         if(h[i].current > 1) {
     700          80 :             res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
     701             :                     h[i].lengths, sizeof(int), sizeof(int),
     702             :                     h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
     703          80 :             if(res < 0) {
     704           0 :                 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
     705           0 :                 ret = AVERROR_INVALIDDATA;
     706           0 :                 goto error;
     707             :             }
     708             :         }
     709             :     }
     710             :     /* this codec relies on wraparound instead of clipping audio */
     711          86 :     if(bits) { //decode 16-bit data
     712           0 :         for(i = stereo; i >= 0; i--)
     713           0 :             pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
     714           0 :         for(i = 0; i <= stereo; i++)
     715           0 :             *samples++ = pred[i];
     716           0 :         for(; i < unp_size / 2; i++) {
     717           0 :             if(get_bits_left(&gb)<0)
     718           0 :                 return AVERROR_INVALIDDATA;
     719           0 :             if(i & stereo) {
     720           0 :                 if(vlc[2].table)
     721           0 :                     res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
     722             :                 else
     723           0 :                     res = 0;
     724           0 :                 if (res < 0) {
     725           0 :                     av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
     726           0 :                     return AVERROR_INVALIDDATA;
     727             :                 }
     728           0 :                 val  = h[2].values[res];
     729           0 :                 if(vlc[3].table)
     730           0 :                     res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
     731             :                 else
     732           0 :                     res = 0;
     733           0 :                 if (res < 0) {
     734           0 :                     av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
     735           0 :                     return AVERROR_INVALIDDATA;
     736             :                 }
     737           0 :                 val |= h[3].values[res] << 8;
     738           0 :                 pred[1] += sign_extend(val, 16);
     739           0 :                 *samples++ = pred[1];
     740             :             } else {
     741           0 :                 if(vlc[0].table)
     742           0 :                     res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
     743             :                 else
     744           0 :                     res = 0;
     745           0 :                 if (res < 0) {
     746           0 :                     av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
     747           0 :                     return AVERROR_INVALIDDATA;
     748             :                 }
     749           0 :                 val  = h[0].values[res];
     750           0 :                 if(vlc[1].table)
     751           0 :                     res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
     752             :                 else
     753           0 :                     res = 0;
     754           0 :                 if (res < 0) {
     755           0 :                     av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
     756           0 :                     return AVERROR_INVALIDDATA;
     757             :                 }
     758           0 :                 val |= h[1].values[res] << 8;
     759           0 :                 pred[0] += sign_extend(val, 16);
     760           0 :                 *samples++ = pred[0];
     761             :             }
     762             :         }
     763             :     } else { //8-bit data
     764         172 :         for(i = stereo; i >= 0; i--)
     765          86 :             pred[i] = get_bits(&gb, 8);
     766         172 :         for(i = 0; i <= stereo; i++)
     767          86 :             *samples8++ = pred[i];
     768      156552 :         for(; i < unp_size; i++) {
     769      156466 :             if(get_bits_left(&gb)<0)
     770           0 :                 return AVERROR_INVALIDDATA;
     771      156466 :             if(i & stereo){
     772           0 :                 if(vlc[1].table)
     773           0 :                     res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
     774             :                 else
     775           0 :                     res = 0;
     776           0 :                 if (res < 0) {
     777           0 :                     av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
     778           0 :                     return AVERROR_INVALIDDATA;
     779             :                 }
     780           0 :                 pred[1] += sign_extend(h[1].values[res], 8);
     781           0 :                 *samples8++ = pred[1];
     782             :             } else {
     783      156466 :                 if(vlc[0].table)
     784      147216 :                     res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
     785             :                 else
     786        9250 :                     res = 0;
     787      156466 :                 if (res < 0) {
     788           0 :                     av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
     789           0 :                     return AVERROR_INVALIDDATA;
     790             :                 }
     791      156466 :                 pred[0] += sign_extend(h[0].values[res], 8);
     792      156466 :                 *samples8++ = pred[0];
     793             :             }
     794             :         }
     795             :     }
     796             : 
     797          86 :     *got_frame_ptr = 1;
     798          86 :     ret = buf_size;
     799             : 
     800          86 : error:
     801         430 :     for(i = 0; i < 4; i++) {
     802         344 :         if(vlc[i].table)
     803          80 :             ff_free_vlc(&vlc[i]);
     804         344 :         av_free(h[i].bits);
     805         344 :         av_free(h[i].lengths);
     806         344 :         av_free(h[i].values);
     807             :     }
     808             : 
     809          86 :     return ret;
     810             : }
     811             : 
     812             : AVCodec ff_smacker_decoder = {
     813             :     .name           = "smackvid",
     814             :     .long_name      = NULL_IF_CONFIG_SMALL("Smacker video"),
     815             :     .type           = AVMEDIA_TYPE_VIDEO,
     816             :     .id             = AV_CODEC_ID_SMACKVIDEO,
     817             :     .priv_data_size = sizeof(SmackVContext),
     818             :     .init           = decode_init,
     819             :     .close          = decode_end,
     820             :     .decode         = decode_frame,
     821             :     .capabilities   = AV_CODEC_CAP_DR1,
     822             : };
     823             : 
     824             : AVCodec ff_smackaud_decoder = {
     825             :     .name           = "smackaud",
     826             :     .long_name      = NULL_IF_CONFIG_SMALL("Smacker audio"),
     827             :     .type           = AVMEDIA_TYPE_AUDIO,
     828             :     .id             = AV_CODEC_ID_SMACKAUDIO,
     829             :     .init           = smka_decode_init,
     830             :     .decode         = smka_decode_frame,
     831             :     .capabilities   = AV_CODEC_CAP_DR1,
     832             : };

Generated by: LCOV version 1.13