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