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