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