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