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