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