smacker.c 22.8 KB
Newer Older
1 2 3 4
/*
 * Smacker decoder
 * Copyright (c) 2006 Konstantin Shishkov
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24 25 26 27 28 29 30 31 32 33 34
 * Smacker decoder
 */

/*
 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
 */

#include <stdio.h>
#include <stdlib.h>

#include "avcodec.h"
35
#include "libavutil/audioconvert.h"
36
#include "mathops.h"
37

38
#define BITSTREAM_READER_LE
39
#include "get_bits.h"
40
#include "bytestream.h"
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

#define SMKTREE_BITS 9
#define SMK_NODE 0x80000000

/*
 * Decoder context
 */
typedef struct SmackVContext {
    AVCodecContext *avctx;
    AVFrame pic;

    int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
    int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
} SmackVContext;

/**
 * Context used for code reconstructing
 */
typedef struct HuffContext {
    int length;
    int maxlength;
    int current;
    uint32_t *bits;
    int *lengths;
    int *values;
} HuffContext;

/* common parameters used for decode_bigtree */
typedef struct DBCtx {
    VLC *v1, *v2;
    int *recode1, *recode2;
    int escapes[3];
    int *last;
    int lcur;
} DBCtx;

/* possible runs of blocks */
static const int block_runs[64] = {
      1,    2,    3,    4,    5,    6,    7,    8,
      9,   10,   11,   12,   13,   14,   15,   16,
     17,   18,   19,   20,   21,   22,   23,   24,
     25,   26,   27,   28,   29,   30,   31,   32,
     33,   34,   35,   36,   37,   38,   39,   40,
     41,   42,   43,   44,   45,   46,   47,   48,
     49,   50,   51,   52,   53,   54,   55,   56,
     57,   58,   59,  128,  256,  512, 1024, 2048 };

enum SmkBlockTypes {
    SMK_BLK_MONO = 0,
    SMK_BLK_FULL = 1,
    SMK_BLK_SKIP = 2,
    SMK_BLK_FILL = 3 };

/**
 * Decode local frame tree
 */
static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
{
    if(!get_bits1(gb)){ //Leaf
        if(hc->current >= 256){
            av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
            return -1;
        }
        if(length){
            hc->bits[hc->current] = prefix;
            hc->lengths[hc->current] = length;
        } else {
            hc->bits[hc->current] = 0;
            hc->lengths[hc->current] = 0;
        }
        hc->values[hc->current] = get_bits(gb, 8);
        hc->current++;
        if(hc->maxlength < length)
            hc->maxlength = length;
        return 0;
    } else { //Node
        int r;
        length++;
        r = smacker_decode_tree(gb, hc, prefix, length);
        if(r)
            return r;
        return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
    }
}

/**
 * Decode header tree
 */
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
{
131 132 133 134
    if (hc->current + 1 >= hc->length) {
        av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
        return -1;
    }
135 136 137
    if(!get_bits1(gb)){ //Leaf
        int val, i1, i2, b1, b2;
        b1 = get_bits_count(gb);
138
        i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
139 140
        b1 = get_bits_count(gb) - b1;
        b2 = get_bits_count(gb);
141
        i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
142
        b2 = get_bits_count(gb) - b2;
143 144
        if (i1 < 0 || i2 < 0)
            return -1;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
        if(val == ctx->escapes[0]) {
            ctx->last[0] = hc->current;
            val = 0;
        } else if(val == ctx->escapes[1]) {
            ctx->last[1] = hc->current;
            val = 0;
        } else if(val == ctx->escapes[2]) {
            ctx->last[2] = hc->current;
            val = 0;
        }

        hc->values[hc->current++] = val;
        return 1;
    } else { //Node
160
        int r = 0, r_new, t;
161 162 163 164 165 166 167

        t = hc->current++;
        r = smacker_decode_bigtree(gb, hc, ctx);
        if(r < 0)
            return r;
        hc->values[t] = SMK_NODE | r;
        r++;
168 169 170 171
        r_new = smacker_decode_bigtree(gb, hc, ctx);
        if (r_new < 0)
            return r_new;
        return r + r_new;
172 173 174 175 176 177 178 179 180 181 182 183 184 185
    }
}

/**
 * Store large tree as FFmpeg's vlc codes
 */
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
{
    int res;
    HuffContext huff;
    HuffContext tmp1, tmp2;
    VLC vlc[2];
    int escapes[3];
    DBCtx ctx;
186
    int err = 0;
187

188 189 190 191 192
    if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
        av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
        return -1;
    }

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    tmp1.length = 256;
    tmp1.maxlength = 0;
    tmp1.current = 0;
    tmp1.bits = av_mallocz(256 * 4);
    tmp1.lengths = av_mallocz(256 * sizeof(int));
    tmp1.values = av_mallocz(256 * sizeof(int));

    tmp2.length = 256;
    tmp2.maxlength = 0;
    tmp2.current = 0;
    tmp2.bits = av_mallocz(256 * 4);
    tmp2.lengths = av_mallocz(256 * sizeof(int));
    tmp2.values = av_mallocz(256 * sizeof(int));

    memset(&vlc[0], 0, sizeof(VLC));
    memset(&vlc[1], 0, sizeof(VLC));

    if(get_bits1(gb)) {
        smacker_decode_tree(gb, &tmp1, 0, 0);
212
        skip_bits1(gb);
213 214 215 216 217 218 219 220 221 222 223 224
        res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
                    tmp1.lengths, sizeof(int), sizeof(int),
                    tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
        if(res < 0) {
            av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
            return -1;
        }
    } else {
        av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
    }
    if(get_bits1(gb)){
        smacker_decode_tree(gb, &tmp2, 0, 0);
225
        skip_bits1(gb);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
                    tmp2.lengths, sizeof(int), sizeof(int),
                    tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
        if(res < 0) {
            av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
            return -1;
        }
    } else {
        av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
    }

    escapes[0]  = get_bits(gb, 8);
    escapes[0] |= get_bits(gb, 8) << 8;
    escapes[1]  = get_bits(gb, 8);
    escapes[1] |= get_bits(gb, 8) << 8;
    escapes[2]  = get_bits(gb, 8);
    escapes[2] |= get_bits(gb, 8) << 8;

    last[0] = last[1] = last[2] = -1;

    ctx.escapes[0] = escapes[0];
    ctx.escapes[1] = escapes[1];
    ctx.escapes[2] = escapes[2];
    ctx.v1 = &vlc[0];
    ctx.v2 = &vlc[1];
    ctx.recode1 = tmp1.values;
    ctx.recode2 = tmp2.values;
    ctx.last = last;

    huff.length = ((size + 3) >> 2) + 3;
    huff.maxlength = 0;
    huff.current = 0;
    huff.values = av_mallocz(huff.length * sizeof(int));

260 261
    if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
        err = -1;
262
    skip_bits1(gb);
263 264 265
    if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
    if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
    if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
266 267 268 269 270
    if(huff.current > huff.length){
        ctx.last[0] = ctx.last[1] = ctx.last[2] = 1;
        av_log(smk->avctx, AV_LOG_ERROR, "bigtree damaged\n");
        return -1;
    }
271 272 273 274

    *recodes = huff.values;

    if(vlc[0].table)
275
        ff_free_vlc(&vlc[0]);
276
    if(vlc[1].table)
277
        ff_free_vlc(&vlc[1]);
278 279 280 281 282 283 284
    av_free(tmp1.bits);
    av_free(tmp1.lengths);
    av_free(tmp1.values);
    av_free(tmp2.bits);
    av_free(tmp2.lengths);
    av_free(tmp2.values);

285
    return err;
286 287 288 289 290 291
}

static int decode_header_trees(SmackVContext *smk) {
    GetBitContext gb;
    int mmap_size, mclr_size, full_size, type_size;

292 293 294 295
    mmap_size = AV_RL32(smk->avctx->extradata);
    mclr_size = AV_RL32(smk->avctx->extradata + 4);
    full_size = AV_RL32(smk->avctx->extradata + 8);
    type_size = AV_RL32(smk->avctx->extradata + 12);
296 297 298 299 300 301 302 303 304

    init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);

    if(!get_bits1(&gb)) {
        av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
        smk->mmap_tbl = av_malloc(sizeof(int) * 2);
        smk->mmap_tbl[0] = 0;
        smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
    } else {
305 306
        if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
            return -1;
307
    }
308
    if(!get_bits1(&gb)) {
309 310 311 312 313
        av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
        smk->mclr_tbl = av_malloc(sizeof(int) * 2);
        smk->mclr_tbl[0] = 0;
        smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
    } else {
314 315
        if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
            return -1;
316
    }
317
    if(!get_bits1(&gb)) {
318 319 320 321 322
        av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
        smk->full_tbl = av_malloc(sizeof(int) * 2);
        smk->full_tbl[0] = 0;
        smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
    } else {
323 324
        if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
            return -1;
325
    }
326
    if(!get_bits1(&gb)) {
327 328 329 330 331
        av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
        smk->type_tbl = av_malloc(sizeof(int) * 2);
        smk->type_tbl[0] = 0;
        smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
    } else {
332 333
        if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
            return -1;
334 335 336 337 338
    }

    return 0;
}

339
static av_always_inline void last_reset(int *recode, int *last) {
340 341 342 343
    recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
}

/* get code and update history */
344
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
345
    register int *table = recode;
346
    int v;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

    while(*table & SMK_NODE) {
        if(get_bits1(gb))
            table += (*table) & (~SMK_NODE);
        table++;
    }
    v = *table;

    if(v != recode[last[0]]) {
        recode[last[2]] = recode[last[1]];
        recode[last[1]] = recode[last[0]];
        recode[last[0]] = v;
    }
    return v;
}

363
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
364
{
365 366
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
367
    SmackVContext * const smk = avctx->priv_data;
368 369 370 371 372 373 374
    uint8_t *out;
    uint32_t *pal;
    GetBitContext gb;
    int blocks, blk, bw, bh;
    int i;
    int stride;

375
    if(buf_size <= 769)
376 377
        return 0;

378
    smk->pic.reference = 3;
379 380 381 382 383 384 385 386 387 388 389
    smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
    if(avctx->reget_buffer(avctx, &smk->pic) < 0){
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        return -1;
    }

    /* make the palette available on the way out */
    pal = (uint32_t*)smk->pic.data[1];
    smk->pic.palette_has_changed = buf[0] & 1;
    smk->pic.key_frame = !!(buf[0] & 2);
    if(smk->pic.key_frame)
390
        smk->pic.pict_type = AV_PICTURE_TYPE_I;
391
    else
392
        smk->pic.pict_type = AV_PICTURE_TYPE_P;
393

394 395
    buf++;
    for(i = 0; i < 256; i++)
396
        *pal++ = 0xFF << 24 | bytestream_get_be24(&buf);
397
    buf_size -= 769;
398 399 400 401 402

    last_reset(smk->mmap_tbl, smk->mmap_last);
    last_reset(smk->mclr_tbl, smk->mclr_last);
    last_reset(smk->full_tbl, smk->full_last);
    last_reset(smk->type_tbl, smk->type_last);
403
    init_get_bits(&gb, buf, buf_size * 8);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

    blk = 0;
    bw = avctx->width >> 2;
    bh = avctx->height >> 2;
    blocks = bw * bh;
    out = smk->pic.data[0];
    stride = smk->pic.linesize[0];
    while(blk < blocks) {
        int type, run, mode;
        uint16_t pix;

        type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
        run = block_runs[(type >> 2) & 0x3F];
        switch(type & 3){
        case SMK_BLK_MONO:
            while(run-- && blk < blocks){
                int clr, map;
                int hi, lo;
                clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
                map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
                out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
                hi = clr >> 8;
                lo = clr & 0xFF;
                for(i = 0; i < 4; i++) {
                    if(map & 1) out[0] = hi; else out[0] = lo;
                    if(map & 2) out[1] = hi; else out[1] = lo;
                    if(map & 4) out[2] = hi; else out[2] = lo;
                    if(map & 8) out[3] = hi; else out[3] = lo;
                    map >>= 4;
                    out += stride;
                }
                blk++;
            }
            break;
        case SMK_BLK_FULL:
            mode = 0;
440
            if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
441 442 443 444 445 446 447 448 449
                if(get_bits1(&gb)) mode = 1;
                else if(get_bits1(&gb)) mode = 2;
            }
            while(run-- && blk < blocks){
                out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
                switch(mode){
                case 0:
                    for(i = 0; i < 4; i++) {
                        pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
450
                        AV_WL16(out+2,pix);
451
                        pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
452
                        AV_WL16(out,pix);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
                        out += stride;
                    }
                    break;
                case 1:
                    pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
                    out[0] = out[1] = pix & 0xFF;
                    out[2] = out[3] = pix >> 8;
                    out += stride;
                    out[0] = out[1] = pix & 0xFF;
                    out[2] = out[3] = pix >> 8;
                    out += stride;
                    pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
                    out[0] = out[1] = pix & 0xFF;
                    out[2] = out[3] = pix >> 8;
                    out += stride;
                    out[0] = out[1] = pix & 0xFF;
                    out[2] = out[3] = pix >> 8;
                    out += stride;
                    break;
                case 2:
                    for(i = 0; i < 2; i++) {
                        uint16_t pix1, pix2;
                        pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
476
                        pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
477 478
                        AV_WL16(out,pix1);
                        AV_WL16(out+2,pix2);
479
                        out += stride;
480 481
                        AV_WL16(out,pix1);
                        AV_WL16(out+2,pix2);
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
                        out += stride;
                    }
                    break;
                }
                blk++;
            }
            break;
        case SMK_BLK_SKIP:
            while(run-- && blk < blocks)
                blk++;
            break;
        case SMK_BLK_FILL:
            mode = type >> 8;
            while(run-- && blk < blocks){
                uint32_t col;
                out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
                col = mode * 0x01010101;
                for(i = 0; i < 4; i++) {
                    *((uint32_t*)out) = col;
                    out += stride;
                }
                blk++;
            }
            break;
        }

    }

    *data_size = sizeof(AVFrame);
    *(AVFrame*)data = smk->pic;

    /* always report that the buffer was completely consumed */
    return buf_size;
}



/*
 *
 * Init smacker decoder
 *
 */
524
static av_cold int decode_init(AVCodecContext *avctx)
525
{
526
    SmackVContext * const c = avctx->priv_data;
527 528 529 530 531

    c->avctx = avctx;

    avctx->pix_fmt = PIX_FMT_PAL8;

532
    avcodec_get_frame_defaults(&c->pic);
533 534 535 536 537 538 539

    /* decode huffman trees from extradata */
    if(avctx->extradata_size < 16){
        av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
        return -1;
    }

540 541
    if (decode_header_trees(c))
        return -1;
542 543 544 545 546 547 548 549 550 551 552

    return 0;
}



/*
 *
 * Uninit smacker decoder
 *
 */
553
static av_cold int decode_end(AVCodecContext *avctx)
554
{
555
    SmackVContext * const smk = avctx->priv_data;
556

557 558 559 560
    av_freep(&smk->mmap_tbl);
    av_freep(&smk->mclr_tbl);
    av_freep(&smk->full_tbl);
    av_freep(&smk->type_tbl);
561 562 563 564 565 566 567 568

    if (smk->pic.data[0])
        avctx->release_buffer(avctx, &smk->pic);

    return 0;
}


569 570 571 572
typedef struct SmackerAudioContext {
    AVFrame frame;
} SmackerAudioContext;

573
static av_cold int smka_decode_init(AVCodecContext *avctx)
574
{
575 576
    SmackerAudioContext *s = avctx->priv_data;

577 578 579 580
    if (avctx->channels < 1 || avctx->channels > 2) {
        av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
        return AVERROR(EINVAL);
    }
581
    avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
582
    avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
583 584 585 586

    avcodec_get_frame_defaults(&s->frame);
    avctx->coded_frame = &s->frame;

587 588 589 590 591 592
    return 0;
}

/**
 * Decode Smacker audio data
 */
593 594
static int smka_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame_ptr, AVPacket *avpkt)
595
{
596
    SmackerAudioContext *s = avctx->priv_data;
597 598
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
599 600 601
    GetBitContext gb;
    HuffContext h[4];
    VLC vlc[4];
602 603
    int16_t *samples;
    uint8_t *samples8;
604
    int val;
605
    int i, res, ret;
606 607 608 609
    int unp_size;
    int bits, stereo;
    int pred[2] = {0, 0};

610 611 612 613 614
    if (buf_size <= 4) {
        av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
        return AVERROR(EINVAL);
    }

615
    unp_size = AV_RL32(buf);
616 617 618 619 620

    init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);

    if(!get_bits1(&gb)){
        av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
621
        *got_frame_ptr = 0;
622 623 624 625
        return 1;
    }
    stereo = get_bits1(&gb);
    bits = get_bits1(&gb);
626 627 628 629 630 631 632 633
    if (stereo ^ (avctx->channels != 1)) {
        av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
        return AVERROR(EINVAL);
    }
    if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
        av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
        return AVERROR(EINVAL);
    }
634

635 636 637 638 639 640 641 642 643
    /* get output buffer */
    s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));
    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        return ret;
    }
    samples  = (int16_t *)s->frame.data[0];
    samples8 =            s->frame.data[0];

644 645 646 647 648 649 650 651 652 653
    memset(vlc, 0, sizeof(VLC) * 4);
    memset(h, 0, sizeof(HuffContext) * 4);
    // Initialize
    for(i = 0; i < (1 << (bits + stereo)); i++) {
        h[i].length = 256;
        h[i].maxlength = 0;
        h[i].current = 0;
        h[i].bits = av_mallocz(256 * 4);
        h[i].lengths = av_mallocz(256 * sizeof(int));
        h[i].values = av_mallocz(256 * sizeof(int));
654
        skip_bits1(&gb);
655
        smacker_decode_tree(&gb, &h[i], 0, 0);
656
        skip_bits1(&gb);
657 658
        if(h[i].current > 1) {
            res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
659 660
                    h[i].lengths, sizeof(int), sizeof(int),
                    h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
661 662 663 664
            if(res < 0) {
                av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
                return -1;
            }
665 666 667
        }
    }
    if(bits) { //decode 16-bit data
668
        for(i = stereo; i >= 0; i--)
669
            pred[i] = av_bswap16(get_bits(&gb, 16));
670
        for(i = 0; i <= stereo; i++)
671
            *samples++ = pred[i];
672
        for(; i < unp_size / 2; i++) {
673 674
            if(get_bits_left(&gb)<0)
                return -1;
675
            if(i & stereo) {
676 677 678 679 680 681 682 683 684 685
                if(vlc[2].table)
                    res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
                else
                    res = 0;
                val  = h[2].values[res];
                if(vlc[3].table)
                    res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
                else
                    res = 0;
                val |= h[3].values[res] << 8;
686
                pred[1] += sign_extend(val, 16);
687
                *samples++ = av_clip_int16(pred[1]);
688
            } else {
689 690 691 692 693 694 695 696 697 698
                if(vlc[0].table)
                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
                else
                    res = 0;
                val  = h[0].values[res];
                if(vlc[1].table)
                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
                else
                    res = 0;
                val |= h[1].values[res] << 8;
699
                pred[0] += sign_extend(val, 16);
700
                *samples++ = av_clip_int16(pred[0]);
701 702 703
            }
        }
    } else { //8-bit data
704 705
        for(i = stereo; i >= 0; i--)
            pred[i] = get_bits(&gb, 8);
706
        for(i = 0; i <= stereo; i++)
707
            *samples8++ = pred[i];
708
        for(; i < unp_size; i++) {
709 710
            if(get_bits_left(&gb)<0)
                return -1;
711
            if(i & stereo){
712 713 714 715
                if(vlc[1].table)
                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
                else
                    res = 0;
716
                pred[1] += sign_extend(h[1].values[res], 8);
717
                *samples8++ = av_clip_uint8(pred[1]);
718
            } else {
719 720 721 722
                if(vlc[0].table)
                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
                else
                    res = 0;
723
                pred[0] += sign_extend(h[0].values[res], 8);
724
                *samples8++ = av_clip_uint8(pred[0]);
725 726 727 728 729 730
            }
        }
    }

    for(i = 0; i < 4; i++) {
        if(vlc[i].table)
731
            ff_free_vlc(&vlc[i]);
732 733 734
        av_free(h[i].bits);
        av_free(h[i].lengths);
        av_free(h[i].values);
735 736
    }

737 738 739
    *got_frame_ptr   = 1;
    *(AVFrame *)data = s->frame;

740 741 742
    return buf_size;
}

743
AVCodec ff_smacker_decoder = {
744 745 746 747 748 749 750 751
    .name           = "smackvid",
    .type           = AVMEDIA_TYPE_VIDEO,
    .id             = CODEC_ID_SMACKVIDEO,
    .priv_data_size = sizeof(SmackVContext),
    .init           = decode_init,
    .close          = decode_end,
    .decode         = decode_frame,
    .capabilities   = CODEC_CAP_DR1,
752
    .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
753 754
};

755
AVCodec ff_smackaud_decoder = {
756 757 758
    .name           = "smackaud",
    .type           = AVMEDIA_TYPE_AUDIO,
    .id             = CODEC_ID_SMACKAUDIO,
759
    .priv_data_size = sizeof(SmackerAudioContext),
760 761
    .init           = smka_decode_init,
    .decode         = smka_decode_frame,
762
    .capabilities   = CODEC_CAP_DR1,
763
    .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
764
};