"Fossies" - the Fresh Open Source Software Archive

Member "libav-12.3/libavcodec/smacker.c" (12 Feb 2018, 24283 Bytes) of package /linux/misc/libav-12.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "smacker.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 12.2_vs_12.3.

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