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