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