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