Statistics
| Branch: | Revision:

ffmpeg / libavcodec / smacker.c @ 2912e87a

History | View | Annotate | Download (20.8 KB)

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 "avcodec.h"
35
#include "libavutil/audioconvert.h"
36

    
37
#define ALT_BITSTREAM_READER_LE
38
#include "get_bits.h"
39
#include "bytestream.h"
40

    
41
#define SMKTREE_BITS 9
42
#define SMK_NODE 0x80000000
43

    
44
/*
45
 * Decoder context
46
 */
47
typedef struct SmackVContext {
48
    AVCodecContext *avctx;
49
    AVFrame pic;
50

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

    
55
/**
56
 * Context used for code reconstructing
57
 */
58
typedef struct HuffContext {
59
    int length;
60
    int maxlength;
61
    int current;
62
    uint32_t *bits;
63
    int *lengths;
64
    int *values;
65
} HuffContext;
66

    
67
/* common parameters used for decode_bigtree */
68
typedef struct DBCtx {
69
    VLC *v1, *v2;
70
    int *recode1, *recode2;
71
    int escapes[3];
72
    int *last;
73
    int lcur;
74
} DBCtx;
75

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

    
87
enum SmkBlockTypes {
88
    SMK_BLK_MONO = 0,
89
    SMK_BLK_FULL = 1,
90
    SMK_BLK_SKIP = 2,
91
    SMK_BLK_FILL = 3 };
92

    
93
/**
94
 * Decode local frame tree
95
 */
96
static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
97
{
98
    if(!get_bits1(gb)){ //Leaf
99
        if(hc->current >= 256){
100
            av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
101
            return -1;
102
        }
103
        if(length){
104
            hc->bits[hc->current] = prefix;
105
            hc->lengths[hc->current] = length;
106
        } else {
107
            hc->bits[hc->current] = 0;
108
            hc->lengths[hc->current] = 0;
109
        }
110
        hc->values[hc->current] = get_bits(gb, 8);
111
        hc->current++;
112
        if(hc->maxlength < length)
113
            hc->maxlength = length;
114
        return 0;
115
    } else { //Node
116
        int r;
117
        length++;
118
        r = smacker_decode_tree(gb, hc, prefix, length);
119
        if(r)
120
            return r;
121
        return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
122
    }
123
}
124

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

    
154
        hc->values[hc->current++] = val;
155
        return 1;
156
    } else { //Node
157
        int r = 0, t;
158

    
159
        t = hc->current++;
160
        r = smacker_decode_bigtree(gb, hc, ctx);
161
        if(r < 0)
162
            return r;
163
        hc->values[t] = SMK_NODE | r;
164
        r++;
165
        r += smacker_decode_bigtree(gb, hc, ctx);
166
        return r;
167
    }
168
}
169

    
170
/**
171
 * Store large tree as Libav's vlc codes
172
 */
173
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
174
{
175
    int res;
176
    HuffContext huff;
177
    HuffContext tmp1, tmp2;
178
    VLC vlc[2];
179
    int escapes[3];
180
    DBCtx ctx;
181

    
182
    if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
183
        av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
184
        return -1;
185
    }
186

    
187
    tmp1.length = 256;
188
    tmp1.maxlength = 0;
189
    tmp1.current = 0;
190
    tmp1.bits = av_mallocz(256 * 4);
191
    tmp1.lengths = av_mallocz(256 * sizeof(int));
192
    tmp1.values = av_mallocz(256 * sizeof(int));
193

    
194
    tmp2.length = 256;
195
    tmp2.maxlength = 0;
196
    tmp2.current = 0;
197
    tmp2.bits = av_mallocz(256 * 4);
198
    tmp2.lengths = av_mallocz(256 * sizeof(int));
199
    tmp2.values = av_mallocz(256 * sizeof(int));
200

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

    
204
    if(get_bits1(gb)) {
205
        smacker_decode_tree(gb, &tmp1, 0, 0);
206
        skip_bits1(gb);
207
        res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
208
                    tmp1.lengths, sizeof(int), sizeof(int),
209
                    tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
210
        if(res < 0) {
211
            av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
212
            return -1;
213
        }
214
    } else {
215
        av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
216
    }
217
    if(get_bits1(gb)){
218
        smacker_decode_tree(gb, &tmp2, 0, 0);
219
        skip_bits1(gb);
220
        res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
221
                    tmp2.lengths, sizeof(int), sizeof(int),
222
                    tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
223
        if(res < 0) {
224
            av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
225
            return -1;
226
        }
227
    } else {
228
        av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
229
    }
230

    
231
    escapes[0]  = get_bits(gb, 8);
232
    escapes[0] |= get_bits(gb, 8) << 8;
233
    escapes[1]  = get_bits(gb, 8);
234
    escapes[1] |= get_bits(gb, 8) << 8;
235
    escapes[2]  = get_bits(gb, 8);
236
    escapes[2] |= get_bits(gb, 8) << 8;
237

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

    
240
    ctx.escapes[0] = escapes[0];
241
    ctx.escapes[1] = escapes[1];
242
    ctx.escapes[2] = escapes[2];
243
    ctx.v1 = &vlc[0];
244
    ctx.v2 = &vlc[1];
245
    ctx.recode1 = tmp1.values;
246
    ctx.recode2 = tmp2.values;
247
    ctx.last = last;
248

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

    
254
    smacker_decode_bigtree(gb, &huff, &ctx);
255
    skip_bits1(gb);
256
    if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
257
    if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
258
    if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
259

    
260
    *recodes = huff.values;
261

    
262
    if(vlc[0].table)
263
        free_vlc(&vlc[0]);
264
    if(vlc[1].table)
265
        free_vlc(&vlc[1]);
266
    av_free(tmp1.bits);
267
    av_free(tmp1.lengths);
268
    av_free(tmp1.values);
269
    av_free(tmp2.bits);
270
    av_free(tmp2.lengths);
271
    av_free(tmp2.values);
272

    
273
    return 0;
274
}
275

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

    
280
    mmap_size = AV_RL32(smk->avctx->extradata);
281
    mclr_size = AV_RL32(smk->avctx->extradata + 4);
282
    full_size = AV_RL32(smk->avctx->extradata + 8);
283
    type_size = AV_RL32(smk->avctx->extradata + 12);
284

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

    
287
    if(!get_bits1(&gb)) {
288
        av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
289
        smk->mmap_tbl = av_malloc(sizeof(int) * 2);
290
        smk->mmap_tbl[0] = 0;
291
        smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
292
    } else {
293
        smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
294
    }
295
    if(!get_bits1(&gb)) {
296
        av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
297
        smk->mclr_tbl = av_malloc(sizeof(int) * 2);
298
        smk->mclr_tbl[0] = 0;
299
        smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
300
    } else {
301
        smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
302
    }
303
    if(!get_bits1(&gb)) {
304
        av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
305
        smk->full_tbl = av_malloc(sizeof(int) * 2);
306
        smk->full_tbl[0] = 0;
307
        smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
308
    } else {
309
        smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
310
    }
311
    if(!get_bits1(&gb)) {
312
        av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
313
        smk->type_tbl = av_malloc(sizeof(int) * 2);
314
        smk->type_tbl[0] = 0;
315
        smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
316
    } else {
317
        smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
318
    }
319

    
320
    return 0;
321
}
322

    
323
static av_always_inline void last_reset(int *recode, int *last) {
324
    recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
325
}
326

    
327
/* get code and update history */
328
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
329
    register int *table = recode;
330
    int v, b;
331

    
332
    b = get_bits_count(gb);
333
    while(*table & SMK_NODE) {
334
        if(get_bits1(gb))
335
            table += (*table) & (~SMK_NODE);
336
        table++;
337
    }
338
    v = *table;
339
    b = get_bits_count(gb) - b;
340

    
341
    if(v != recode[last[0]]) {
342
        recode[last[2]] = recode[last[1]];
343
        recode[last[1]] = recode[last[0]];
344
        recode[last[0]] = v;
345
    }
346
    return v;
347
}
348

    
349
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
350
{
351
    const uint8_t *buf = avpkt->data;
352
    int buf_size = avpkt->size;
353
    SmackVContext * const smk = avctx->priv_data;
354
    uint8_t *out;
355
    uint32_t *pal;
356
    GetBitContext gb;
357
    int blocks, blk, bw, bh;
358
    int i;
359
    int stride;
360

    
361
    if(buf_size <= 769)
362
        return 0;
363
    if(smk->pic.data[0])
364
            avctx->release_buffer(avctx, &smk->pic);
365

    
366
    smk->pic.reference = 1;
367
    smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
368
    if(avctx->reget_buffer(avctx, &smk->pic) < 0){
369
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
370
        return -1;
371
    }
372

    
373
    /* make the palette available on the way out */
374
    pal = (uint32_t*)smk->pic.data[1];
375
    smk->pic.palette_has_changed = buf[0] & 1;
376
    smk->pic.key_frame = !!(buf[0] & 2);
377
    if(smk->pic.key_frame)
378
        smk->pic.pict_type = FF_I_TYPE;
379
    else
380
        smk->pic.pict_type = FF_P_TYPE;
381

    
382
    buf++;
383
    for(i = 0; i < 256; i++)
384
        *pal++ = bytestream_get_be24(&buf);
385
    buf_size -= 769;
386

    
387
    last_reset(smk->mmap_tbl, smk->mmap_last);
388
    last_reset(smk->mclr_tbl, smk->mclr_last);
389
    last_reset(smk->full_tbl, smk->full_last);
390
    last_reset(smk->type_tbl, smk->type_last);
391
    init_get_bits(&gb, buf, buf_size * 8);
392

    
393
    blk = 0;
394
    bw = avctx->width >> 2;
395
    bh = avctx->height >> 2;
396
    blocks = bw * bh;
397
    out = smk->pic.data[0];
398
    stride = smk->pic.linesize[0];
399
    while(blk < blocks) {
400
        int type, run, mode;
401
        uint16_t pix;
402

    
403
        type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
404
        run = block_runs[(type >> 2) & 0x3F];
405
        switch(type & 3){
406
        case SMK_BLK_MONO:
407
            while(run-- && blk < blocks){
408
                int clr, map;
409
                int hi, lo;
410
                clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
411
                map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
412
                out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
413
                hi = clr >> 8;
414
                lo = clr & 0xFF;
415
                for(i = 0; i < 4; i++) {
416
                    if(map & 1) out[0] = hi; else out[0] = lo;
417
                    if(map & 2) out[1] = hi; else out[1] = lo;
418
                    if(map & 4) out[2] = hi; else out[2] = lo;
419
                    if(map & 8) out[3] = hi; else out[3] = lo;
420
                    map >>= 4;
421
                    out += stride;
422
                }
423
                blk++;
424
            }
425
            break;
426
        case SMK_BLK_FULL:
427
            mode = 0;
428
            if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
429
                if(get_bits1(&gb)) mode = 1;
430
                else if(get_bits1(&gb)) mode = 2;
431
            }
432
            while(run-- && blk < blocks){
433
                out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
434
                switch(mode){
435
                case 0:
436
                    for(i = 0; i < 4; i++) {
437
                        pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
438
                        AV_WL16(out+2,pix);
439
                        pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
440
                        AV_WL16(out,pix);
441
                        out += stride;
442
                    }
443
                    break;
444
                case 1:
445
                    pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
446
                    out[0] = out[1] = pix & 0xFF;
447
                    out[2] = out[3] = pix >> 8;
448
                    out += stride;
449
                    out[0] = out[1] = pix & 0xFF;
450
                    out[2] = out[3] = pix >> 8;
451
                    out += stride;
452
                    pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
453
                    out[0] = out[1] = pix & 0xFF;
454
                    out[2] = out[3] = pix >> 8;
455
                    out += stride;
456
                    out[0] = out[1] = pix & 0xFF;
457
                    out[2] = out[3] = pix >> 8;
458
                    out += stride;
459
                    break;
460
                case 2:
461
                    for(i = 0; i < 2; i++) {
462
                        uint16_t pix1, pix2;
463
                        pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
464
                        pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
465
                        AV_WL16(out,pix1);
466
                        AV_WL16(out+2,pix2);
467
                        out += stride;
468
                        AV_WL16(out,pix1);
469
                        AV_WL16(out+2,pix2);
470
                        out += stride;
471
                    }
472
                    break;
473
                }
474
                blk++;
475
            }
476
            break;
477
        case SMK_BLK_SKIP:
478
            while(run-- && blk < blocks)
479
                blk++;
480
            break;
481
        case SMK_BLK_FILL:
482
            mode = type >> 8;
483
            while(run-- && blk < blocks){
484
                uint32_t col;
485
                out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
486
                col = mode * 0x01010101;
487
                for(i = 0; i < 4; i++) {
488
                    *((uint32_t*)out) = col;
489
                    out += stride;
490
                }
491
                blk++;
492
            }
493
            break;
494
        }
495

    
496
    }
497

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

    
501
    /* always report that the buffer was completely consumed */
502
    return buf_size;
503
}
504

    
505

    
506

    
507
/*
508
 *
509
 * Init smacker decoder
510
 *
511
 */
512
static av_cold int decode_init(AVCodecContext *avctx)
513
{
514
    SmackVContext * const c = avctx->priv_data;
515

    
516
    c->avctx = avctx;
517

    
518
    avctx->pix_fmt = PIX_FMT_PAL8;
519

    
520

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

    
527
    decode_header_trees(c);
528

    
529

    
530
    return 0;
531
}
532

    
533

    
534

    
535
/*
536
 *
537
 * Uninit smacker decoder
538
 *
539
 */
540
static av_cold int decode_end(AVCodecContext *avctx)
541
{
542
    SmackVContext * const smk = avctx->priv_data;
543

    
544
    av_freep(&smk->mmap_tbl);
545
    av_freep(&smk->mclr_tbl);
546
    av_freep(&smk->full_tbl);
547
    av_freep(&smk->type_tbl);
548

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

    
552
    return 0;
553
}
554

    
555

    
556
static av_cold int smka_decode_init(AVCodecContext *avctx)
557
{
558
    avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
559
    avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
560
    return 0;
561
}
562

    
563
/**
564
 * Decode Smacker audio data
565
 */
566
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
567
{
568
    const uint8_t *buf = avpkt->data;
569
    int buf_size = avpkt->size;
570
    GetBitContext gb;
571
    HuffContext h[4];
572
    VLC vlc[4];
573
    int16_t *samples = data;
574
    int8_t *samples8 = data;
575
    int val;
576
    int i, res;
577
    int unp_size;
578
    int bits, stereo;
579
    int pred[2] = {0, 0};
580

    
581
    unp_size = AV_RL32(buf);
582

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

    
585
    if(!get_bits1(&gb)){
586
        av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
587
        *data_size = 0;
588
        return 1;
589
    }
590
    stereo = get_bits1(&gb);
591
    bits = get_bits1(&gb);
592
    if (unp_size & 0xC0000000 || unp_size > *data_size) {
593
        av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
594
        return -1;
595
    }
596

    
597
    memset(vlc, 0, sizeof(VLC) * 4);
598
    memset(h, 0, sizeof(HuffContext) * 4);
599
    // Initialize
600
    for(i = 0; i < (1 << (bits + stereo)); i++) {
601
        h[i].length = 256;
602
        h[i].maxlength = 0;
603
        h[i].current = 0;
604
        h[i].bits = av_mallocz(256 * 4);
605
        h[i].lengths = av_mallocz(256 * sizeof(int));
606
        h[i].values = av_mallocz(256 * sizeof(int));
607
        skip_bits1(&gb);
608
        smacker_decode_tree(&gb, &h[i], 0, 0);
609
        skip_bits1(&gb);
610
        if(h[i].current > 1) {
611
            res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
612
                    h[i].lengths, sizeof(int), sizeof(int),
613
                    h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
614
            if(res < 0) {
615
                av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
616
                return -1;
617
            }
618
        }
619
    }
620
    if(bits) { //decode 16-bit data
621
        for(i = stereo; i >= 0; i--)
622
            pred[i] = av_bswap16(get_bits(&gb, 16));
623
        for(i = 0; i < stereo; i++)
624
            *samples++ = pred[i];
625
        for(i = 0; i < unp_size / 2; i++) {
626
            if(i & stereo) {
627
                if(vlc[2].table)
628
                    res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
629
                else
630
                    res = 0;
631
                val  = h[2].values[res];
632
                if(vlc[3].table)
633
                    res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
634
                else
635
                    res = 0;
636
                val |= h[3].values[res] << 8;
637
                pred[1] += (int16_t)val;
638
                *samples++ = pred[1];
639
            } else {
640
                if(vlc[0].table)
641
                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
642
                else
643
                    res = 0;
644
                val  = h[0].values[res];
645
                if(vlc[1].table)
646
                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
647
                else
648
                    res = 0;
649
                val |= h[1].values[res] << 8;
650
                pred[0] += val;
651
                *samples++ = pred[0];
652
            }
653
        }
654
    } else { //8-bit data
655
        for(i = stereo; i >= 0; i--)
656
            pred[i] = get_bits(&gb, 8);
657
        for(i = 0; i < stereo; i++)
658
            *samples8++ = pred[i];
659
        for(i = 0; i < unp_size; i++) {
660
            if(i & stereo){
661
                if(vlc[1].table)
662
                    res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
663
                else
664
                    res = 0;
665
                pred[1] += (int8_t)h[1].values[res];
666
                *samples8++ = pred[1];
667
            } else {
668
                if(vlc[0].table)
669
                    res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
670
                else
671
                    res = 0;
672
                pred[0] += (int8_t)h[0].values[res];
673
                *samples8++ = pred[0];
674
            }
675
        }
676
    }
677

    
678
    for(i = 0; i < 4; i++) {
679
        if(vlc[i].table)
680
            free_vlc(&vlc[i]);
681
        av_free(h[i].bits);
682
        av_free(h[i].lengths);
683
        av_free(h[i].values);
684
    }
685

    
686
    *data_size = unp_size;
687
    return buf_size;
688
}
689

    
690
AVCodec ff_smacker_decoder = {
691
    "smackvid",
692
    AVMEDIA_TYPE_VIDEO,
693
    CODEC_ID_SMACKVIDEO,
694
    sizeof(SmackVContext),
695
    decode_init,
696
    NULL,
697
    decode_end,
698
    decode_frame,
699
    CODEC_CAP_DR1,
700
    .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
701
};
702

    
703
AVCodec ff_smackaud_decoder = {
704
    "smackaud",
705
    AVMEDIA_TYPE_AUDIO,
706
    CODEC_ID_SMACKAUDIO,
707
    0,
708
    smka_decode_init,
709
    NULL,
710
    NULL,
711
    smka_decode_frame,
712
    .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
713
};
714