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