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