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