]> git.sesse.net Git - ffmpeg/blob - libavcodec/smacker.c
avcodec/smacker: Remove redundant checks for NULL before freeing
[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         ff_free_vlc(&vlc[i]);
258         av_free(h[i].bits);
259         av_free(h[i].lengths);
260         av_free(h[i].values);
261     }
262
263     return err;
264 }
265
266 static int decode_header_trees(SmackVContext *smk) {
267     GetBitContext gb;
268     int mmap_size, mclr_size, full_size, type_size, ret;
269     int skip = 0;
270
271     mmap_size = AV_RL32(smk->avctx->extradata);
272     mclr_size = AV_RL32(smk->avctx->extradata + 4);
273     full_size = AV_RL32(smk->avctx->extradata + 8);
274     type_size = AV_RL32(smk->avctx->extradata + 12);
275
276     ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
277     if (ret < 0)
278         return ret;
279
280     if(!get_bits1(&gb)) {
281         skip ++;
282         av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
283         smk->mmap_tbl = av_malloc(sizeof(int) * 2);
284         if (!smk->mmap_tbl)
285             return AVERROR(ENOMEM);
286         smk->mmap_tbl[0] = 0;
287         smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
288     } else {
289         ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
290         if (ret < 0)
291             return ret;
292     }
293     if(!get_bits1(&gb)) {
294         skip ++;
295         av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
296         smk->mclr_tbl = av_malloc(sizeof(int) * 2);
297         if (!smk->mclr_tbl)
298             return AVERROR(ENOMEM);
299         smk->mclr_tbl[0] = 0;
300         smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
301     } else {
302         ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
303         if (ret < 0)
304             return ret;
305     }
306     if(!get_bits1(&gb)) {
307         skip ++;
308         av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
309         smk->full_tbl = av_malloc(sizeof(int) * 2);
310         if (!smk->full_tbl)
311             return AVERROR(ENOMEM);
312         smk->full_tbl[0] = 0;
313         smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
314     } else {
315         ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
316         if (ret < 0)
317             return ret;
318     }
319     if(!get_bits1(&gb)) {
320         skip ++;
321         av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
322         smk->type_tbl = av_malloc(sizeof(int) * 2);
323         if (!smk->type_tbl)
324             return AVERROR(ENOMEM);
325         smk->type_tbl[0] = 0;
326         smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
327     } else {
328         ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
329         if (ret < 0)
330             return ret;
331     }
332     if (skip == 4)
333         return AVERROR_INVALIDDATA;
334
335     return 0;
336 }
337
338 static av_always_inline void last_reset(int *recode, int *last) {
339     recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
340 }
341
342 /* get code and update history */
343 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
344     register int *table = recode;
345     int v;
346
347     while(*table & SMK_NODE) {
348         if (get_bits_left(gb) < 1)
349             return AVERROR_INVALIDDATA;
350         if(get_bits1(gb))
351             table += (*table) & (~SMK_NODE);
352         table++;
353     }
354     v = *table;
355
356     if(v != recode[last[0]]) {
357         recode[last[2]] = recode[last[1]];
358         recode[last[1]] = recode[last[0]];
359         recode[last[0]] = v;
360     }
361     return v;
362 }
363
364 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
365                         AVPacket *avpkt)
366 {
367     SmackVContext * const smk = avctx->priv_data;
368     uint8_t *out;
369     uint32_t *pal;
370     GetByteContext gb2;
371     GetBitContext gb;
372     int blocks, blk, bw, bh;
373     int i, ret;
374     int stride;
375     int flags;
376
377     if (avpkt->size <= 769)
378         return AVERROR_INVALIDDATA;
379
380     if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
381         return ret;
382
383     /* make the palette available on the way out */
384     pal = (uint32_t*)smk->pic->data[1];
385     bytestream2_init(&gb2, avpkt->data, avpkt->size);
386     flags = bytestream2_get_byteu(&gb2);
387     smk->pic->palette_has_changed = flags & 1;
388     smk->pic->key_frame = !!(flags & 2);
389     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     for(i = 0; i < 256; i++)
395         *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
396
397     last_reset(smk->mmap_tbl, smk->mmap_last);
398     last_reset(smk->mclr_tbl, smk->mclr_last);
399     last_reset(smk->full_tbl, smk->full_last);
400     last_reset(smk->type_tbl, smk->type_last);
401     if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
402         return ret;
403
404     blk = 0;
405     bw = avctx->width >> 2;
406     bh = avctx->height >> 2;
407     blocks = bw * bh;
408     stride = smk->pic->linesize[0];
409     while(blk < blocks) {
410         int type, run, mode;
411         uint16_t pix;
412
413         type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
414         if (type < 0)
415             return type;
416         run = block_runs[(type >> 2) & 0x3F];
417         switch(type & 3){
418         case SMK_BLK_MONO:
419             while(run-- && blk < blocks){
420                 int clr, map;
421                 int hi, lo;
422                 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
423                 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
424                 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
425                 hi = clr >> 8;
426                 lo = clr & 0xFF;
427                 for(i = 0; i < 4; i++) {
428                     if(map & 1) out[0] = hi; else out[0] = lo;
429                     if(map & 2) out[1] = hi; else out[1] = lo;
430                     if(map & 4) out[2] = hi; else out[2] = lo;
431                     if(map & 8) out[3] = hi; else out[3] = lo;
432                     map >>= 4;
433                     out += stride;
434                 }
435                 blk++;
436             }
437             break;
438         case SMK_BLK_FULL:
439             mode = 0;
440             if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
441                 if(get_bits1(&gb)) mode = 1;
442                 else if(get_bits1(&gb)) mode = 2;
443             }
444             while(run-- && blk < blocks){
445                 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
446                 switch(mode){
447                 case 0:
448                     for(i = 0; i < 4; i++) {
449                         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                         out += stride;
454                     }
455                     break;
456                 case 1:
457                     pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
458                     out[0] = out[1] = pix & 0xFF;
459                     out[2] = out[3] = pix >> 8;
460                     out += stride;
461                     out[0] = out[1] = pix & 0xFF;
462                     out[2] = out[3] = pix >> 8;
463                     out += stride;
464                     pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
465                     out[0] = out[1] = pix & 0xFF;
466                     out[2] = out[3] = pix >> 8;
467                     out += stride;
468                     out[0] = out[1] = pix & 0xFF;
469                     out[2] = out[3] = pix >> 8;
470                     break;
471                 case 2:
472                     for(i = 0; i < 2; i++) {
473                         uint16_t pix1, pix2;
474                         pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
475                         pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
476                         AV_WL16(out,pix1);
477                         AV_WL16(out+2,pix2);
478                         out += stride;
479                         AV_WL16(out,pix1);
480                         AV_WL16(out+2,pix2);
481                         out += stride;
482                     }
483                     break;
484                 }
485                 blk++;
486             }
487             break;
488         case SMK_BLK_SKIP:
489             while(run-- && blk < blocks)
490                 blk++;
491             break;
492         case SMK_BLK_FILL:
493             mode = type >> 8;
494             while(run-- && blk < blocks){
495                 uint32_t col;
496                 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
497                 col = mode * 0x01010101U;
498                 for(i = 0; i < 4; i++) {
499                     *((uint32_t*)out) = col;
500                     out += stride;
501                 }
502                 blk++;
503             }
504             break;
505         }
506
507     }
508
509     if ((ret = av_frame_ref(data, smk->pic)) < 0)
510         return ret;
511
512     *got_frame = 1;
513
514     /* always report that the buffer was completely consumed */
515     return avpkt->size;
516 }
517
518
519 static av_cold int decode_end(AVCodecContext *avctx)
520 {
521     SmackVContext * const smk = avctx->priv_data;
522
523     av_freep(&smk->mmap_tbl);
524     av_freep(&smk->mclr_tbl);
525     av_freep(&smk->full_tbl);
526     av_freep(&smk->type_tbl);
527
528     av_frame_free(&smk->pic);
529
530     return 0;
531 }
532
533
534 static av_cold int decode_init(AVCodecContext *avctx)
535 {
536     SmackVContext * const c = avctx->priv_data;
537     int ret;
538
539     c->avctx = avctx;
540
541     avctx->pix_fmt = AV_PIX_FMT_PAL8;
542
543     c->pic = av_frame_alloc();
544     if (!c->pic)
545         return AVERROR(ENOMEM);
546
547     /* decode huffman trees from extradata */
548     if(avctx->extradata_size < 16){
549         av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
550         return AVERROR(EINVAL);
551     }
552
553     ret = decode_header_trees(c);
554     if (ret < 0) {
555         return ret;
556     }
557
558     return 0;
559 }
560
561
562 static av_cold int smka_decode_init(AVCodecContext *avctx)
563 {
564     if (avctx->channels < 1 || avctx->channels > 2) {
565         av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
566         return AVERROR_INVALIDDATA;
567     }
568     avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
569     avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
570
571     return 0;
572 }
573
574 /**
575  * Decode Smacker audio data
576  */
577 static int smka_decode_frame(AVCodecContext *avctx, void *data,
578                              int *got_frame_ptr, AVPacket *avpkt)
579 {
580     AVFrame *frame     = data;
581     const uint8_t *buf = avpkt->data;
582     int buf_size = avpkt->size;
583     GetBitContext gb;
584     HuffContext h[4] = { { 0 } };
585     VLC vlc[4]       = { { 0 } };
586     int16_t *samples;
587     uint8_t *samples8;
588     int val;
589     int i, res, ret;
590     int unp_size;
591     int bits, stereo;
592     int pred[2] = {0, 0};
593
594     if (buf_size <= 4) {
595         av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
596         return AVERROR_INVALIDDATA;
597     }
598
599     unp_size = AV_RL32(buf);
600
601     if (unp_size > (1U<<24)) {
602         av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
603         return AVERROR_INVALIDDATA;
604     }
605
606     if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
607         return ret;
608
609     if(!get_bits1(&gb)){
610         av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
611         *got_frame_ptr = 0;
612         return 1;
613     }
614     stereo = get_bits1(&gb);
615     bits = get_bits1(&gb);
616     if (stereo ^ (avctx->channels != 1)) {
617         av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
618         return AVERROR_INVALIDDATA;
619     }
620     if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
621         av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
622         return AVERROR_INVALIDDATA;
623     }
624
625     /* get output buffer */
626     frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
627     if (unp_size % (avctx->channels * (bits + 1))) {
628         av_log(avctx, AV_LOG_ERROR,
629                "The buffer does not contain an integer number of samples\n");
630         return AVERROR_INVALIDDATA;
631     }
632     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
633         return ret;
634     samples  = (int16_t *)frame->data[0];
635     samples8 =            frame->data[0];
636
637     // Initialize
638     for(i = 0; i < (1 << (bits + stereo)); i++) {
639         h[i].length = 256;
640         h[i].current = 0;
641         h[i].bits    = av_malloc(256 * sizeof(h[i].bits));
642         h[i].lengths = av_malloc(256 * sizeof(h[i].lengths));
643         h[i].values  = av_malloc(256 * sizeof(h[i].values));
644         if (!h[i].bits || !h[i].lengths || !h[i].values) {
645             ret = AVERROR(ENOMEM);
646             goto error;
647         }
648         skip_bits1(&gb);
649         if ((ret = smacker_decode_tree(&gb, &h[i], 0, 0)) < 0)
650             goto error;
651         skip_bits1(&gb);
652         if(h[i].current > 1) {
653             ret = init_vlc(&vlc[i], SMKTREE_BITS, h[i].current,
654                     h[i].lengths, sizeof(int), sizeof(int),
655                     h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
656             if (ret < 0) {
657                 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
658                 goto error;
659             }
660         }
661     }
662     /* this codec relies on wraparound instead of clipping audio */
663     if(bits) { //decode 16-bit data
664         for(i = stereo; i >= 0; i--)
665             pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
666         for(i = 0; i <= stereo; i++)
667             *samples++ = pred[i];
668         for(; i < unp_size / 2; i++) {
669             if (get_bits_left(&gb) < 0) {
670                 ret = AVERROR_INVALIDDATA;
671                 goto error;
672             }
673             if(i & stereo) {
674                 if(vlc[2].table)
675                     res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
676                 else
677                     res = 0;
678                 val  = h[2].values[res];
679                 if(vlc[3].table)
680                     res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
681                 else
682                     res = 0;
683                 val |= h[3].values[res] << 8;
684                 pred[1] += (unsigned)sign_extend(val, 16);
685                 *samples++ = pred[1];
686             } else {
687                 if(vlc[0].table)
688                     res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
689                 else
690                     res = 0;
691                 val  = h[0].values[res];
692                 if(vlc[1].table)
693                     res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
694                 else
695                     res = 0;
696                 val |= h[1].values[res] << 8;
697                 pred[0] += (unsigned)sign_extend(val, 16);
698                 *samples++ = pred[0];
699             }
700         }
701     } else { //8-bit data
702         for(i = stereo; i >= 0; i--)
703             pred[i] = get_bits(&gb, 8);
704         for(i = 0; i <= stereo; i++)
705             *samples8++ = pred[i];
706         for(; i < unp_size; i++) {
707             if (get_bits_left(&gb) < 0) {
708                 ret = AVERROR_INVALIDDATA;
709                 goto error;
710             }
711             if(i & stereo){
712                 if(vlc[1].table)
713                     res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
714                 else
715                     res = 0;
716                 pred[1] += sign_extend(h[1].values[res], 8);
717                 *samples8++ = pred[1];
718             } else {
719                 if(vlc[0].table)
720                     res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
721                 else
722                     res = 0;
723                 pred[0] += sign_extend(h[0].values[res], 8);
724                 *samples8++ = pred[0];
725             }
726         }
727     }
728
729     *got_frame_ptr = 1;
730     ret = buf_size;
731
732 error:
733     for(i = 0; i < 4; i++) {
734         ff_free_vlc(&vlc[i]);
735         av_free(h[i].bits);
736         av_free(h[i].lengths);
737         av_free(h[i].values);
738     }
739
740     return ret;
741 }
742
743 AVCodec ff_smacker_decoder = {
744     .name           = "smackvid",
745     .long_name      = NULL_IF_CONFIG_SMALL("Smacker video"),
746     .type           = AVMEDIA_TYPE_VIDEO,
747     .id             = AV_CODEC_ID_SMACKVIDEO,
748     .priv_data_size = sizeof(SmackVContext),
749     .init           = decode_init,
750     .close          = decode_end,
751     .decode         = decode_frame,
752     .capabilities   = AV_CODEC_CAP_DR1,
753     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
754 };
755
756 AVCodec ff_smackaud_decoder = {
757     .name           = "smackaud",
758     .long_name      = NULL_IF_CONFIG_SMALL("Smacker audio"),
759     .type           = AVMEDIA_TYPE_AUDIO,
760     .id             = AV_CODEC_ID_SMACKAUDIO,
761     .init           = smka_decode_init,
762     .decode         = smka_decode_frame,
763     .capabilities   = AV_CODEC_CAP_DR1,
764 };