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