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