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