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