61283e83fb6ef4e899e63deef01f9e777e194a60
[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 "mathops.h"
37
38 #define BITSTREAM_READER_LE
39 #include "get_bits.h"
40 #include "bytestream.h"
41
42 #define SMKTREE_BITS 9
43 #define SMK_NODE 0x80000000
44
45 /*
46 * Decoder context
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(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
98 {
99 if(!get_bits1(gb)){ //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] = get_bits(gb, 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(gb, hc, prefix, length);
120 if(r)
121 return r;
122 return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
123 }
124 }
125
126 /**
127 * Decode header tree
128 */
129 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
130 {
131 if (hc->current + 1 >= hc->length) {
132 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
133 return -1;
134 }
135 if(!get_bits1(gb)){ //Leaf
136 int val, i1, i2;
137 i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
138 i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
139 if (i1 < 0 || i2 < 0)
140 return -1;
141 val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
142 if(val == ctx->escapes[0]) {
143 ctx->last[0] = hc->current;
144 val = 0;
145 } else if(val == ctx->escapes[1]) {
146 ctx->last[1] = hc->current;
147 val = 0;
148 } else if(val == ctx->escapes[2]) {
149 ctx->last[2] = hc->current;
150 val = 0;
151 }
152
153 hc->values[hc->current++] = val;
154 return 1;
155 } else { //Node
156 int r = 0, r_new, t;
157
158 t = hc->current++;
159 r = smacker_decode_bigtree(gb, hc, ctx);
160 if(r < 0)
161 return r;
162 hc->values[t] = SMK_NODE | r;
163 r++;
164 r_new = smacker_decode_bigtree(gb, hc, ctx);
165 if (r_new < 0)
166 return r_new;
167 return r + r_new;
168 }
169 }
170
171 /**
172 * Store large tree as Libav's vlc codes
173 */
174 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
175 {
176 int res;
177 HuffContext huff;
178 HuffContext tmp1, tmp2;
179 VLC vlc[2] = { { 0 } };
180 int escapes[3];
181 DBCtx ctx;
182 int err = 0;
183
184 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
185 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
186 return -1;
187 }
188
189 tmp1.length = 256;
190 tmp1.maxlength = 0;
191 tmp1.current = 0;
192 tmp1.bits = av_mallocz(256 * 4);
193 tmp1.lengths = av_mallocz(256 * sizeof(int));
194 tmp1.values = av_mallocz(256 * sizeof(int));
195
196 tmp2.length = 256;
197 tmp2.maxlength = 0;
198 tmp2.current = 0;
199 tmp2.bits = av_mallocz(256 * 4);
200 tmp2.lengths = av_mallocz(256 * sizeof(int));
201 tmp2.values = av_mallocz(256 * sizeof(int));
202
203 if(get_bits1(gb)) {
204 smacker_decode_tree(gb, &tmp1, 0, 0);
205 skip_bits1(gb);
206 res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
207 tmp1.lengths, sizeof(int), sizeof(int),
208 tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
209 if(res < 0) {
210 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
211 return -1;
212 }
213 } else {
214 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
215 }
216 if(get_bits1(gb)){
217 smacker_decode_tree(gb, &tmp2, 0, 0);
218 skip_bits1(gb);
219 res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
220 tmp2.lengths, sizeof(int), sizeof(int),
221 tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
222 if(res < 0) {
223 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
224 return -1;
225 }
226 } else {
227 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
228 }
229
230 escapes[0] = get_bits(gb, 8);
231 escapes[0] |= get_bits(gb, 8) << 8;
232 escapes[1] = get_bits(gb, 8);
233 escapes[1] |= get_bits(gb, 8) << 8;
234 escapes[2] = get_bits(gb, 8);
235 escapes[2] |= get_bits(gb, 8) << 8;
236
237 last[0] = last[1] = last[2] = -1;
238
239 ctx.escapes[0] = escapes[0];
240 ctx.escapes[1] = escapes[1];
241 ctx.escapes[2] = escapes[2];
242 ctx.v1 = &vlc[0];
243 ctx.v2 = &vlc[1];
244 ctx.recode1 = tmp1.values;
245 ctx.recode2 = tmp2.values;
246 ctx.last = last;
247
248 huff.length = ((size + 3) >> 2) + 3;
249 huff.maxlength = 0;
250 huff.current = 0;
251 huff.values = av_mallocz(huff.length * sizeof(int));
252
253 if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
254 err = -1;
255 skip_bits1(gb);
256 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
257 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
258 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
259
260 *recodes = huff.values;
261
262 if(vlc[0].table)
263 ff_free_vlc(&vlc[0]);
264 if(vlc[1].table)
265 ff_free_vlc(&vlc[1]);
266 av_free(tmp1.bits);
267 av_free(tmp1.lengths);
268 av_free(tmp1.values);
269 av_free(tmp2.bits);
270 av_free(tmp2.lengths);
271 av_free(tmp2.values);
272
273 return err;
274 }
275
276 static int decode_header_trees(SmackVContext *smk) {
277 GetBitContext gb;
278 int mmap_size, mclr_size, full_size, type_size;
279
280 mmap_size = AV_RL32(smk->avctx->extradata);
281 mclr_size = AV_RL32(smk->avctx->extradata + 4);
282 full_size = AV_RL32(smk->avctx->extradata + 8);
283 type_size = AV_RL32(smk->avctx->extradata + 12);
284
285 init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
286
287 if(!get_bits1(&gb)) {
288 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
289 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
290 smk->mmap_tbl[0] = 0;
291 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
292 } else {
293 if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
294 return -1;
295 }
296 if(!get_bits1(&gb)) {
297 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
298 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
299 smk->mclr_tbl[0] = 0;
300 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
301 } else {
302 if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
303 return -1;
304 }
305 if(!get_bits1(&gb)) {
306 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
307 smk->full_tbl = av_malloc(sizeof(int) * 2);
308 smk->full_tbl[0] = 0;
309 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
310 } else {
311 if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
312 return -1;
313 }
314 if(!get_bits1(&gb)) {
315 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
316 smk->type_tbl = av_malloc(sizeof(int) * 2);
317 smk->type_tbl[0] = 0;
318 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
319 } else {
320 if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
321 return -1;
322 }
323
324 return 0;
325 }
326
327 static av_always_inline void last_reset(int *recode, int *last) {
328 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
329 }
330
331 /* get code and update history */
332 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
333 register int *table = recode;
334 int v;
335
336 while(*table & SMK_NODE) {
337 if(get_bits1(gb))
338 table += (*table) & (~SMK_NODE);
339 table++;
340 }
341 v = *table;
342
343 if(v != recode[last[0]]) {
344 recode[last[2]] = recode[last[1]];
345 recode[last[1]] = recode[last[0]];
346 recode[last[0]] = v;
347 }
348 return v;
349 }
350
351 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
352 {
353 SmackVContext * const smk = avctx->priv_data;
354 uint8_t *out;
355 uint32_t *pal;
356 GetByteContext gb2;
357 GetBitContext gb;
358 int blocks, blk, bw, bh;
359 int i;
360 int stride;
361 int flags;
362
363 if (avpkt->size <= 769)
364 return 0;
365
366 smk->pic.reference = 1;
367 smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
368 if(avctx->reget_buffer(avctx, &smk->pic) < 0){
369 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
370 return -1;
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 *data_size = sizeof(AVFrame);
499 *(AVFrame*)data = smk->pic;
500
501 /* always report that the buffer was completely consumed */
502 return avpkt->size;
503 }
504
505
506
507 /*
508 *
509 * Init smacker decoder
510 *
511 */
512 static av_cold int decode_init(AVCodecContext *avctx)
513 {
514 SmackVContext * const c = avctx->priv_data;
515
516 c->avctx = avctx;
517
518 avctx->pix_fmt = AV_PIX_FMT_PAL8;
519
520
521 /* decode huffman trees from extradata */
522 if(avctx->extradata_size < 16){
523 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
524 return -1;
525 }
526
527 if (decode_header_trees(c))
528 return -1;
529
530 return 0;
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 if (smk->pic.data[0])
550 avctx->release_buffer(avctx, &smk->pic);
551
552 return 0;
553 }
554
555
556 typedef struct SmackerAudioContext {
557 AVFrame frame;
558 } SmackerAudioContext;
559
560 static av_cold int smka_decode_init(AVCodecContext *avctx)
561 {
562 SmackerAudioContext *s = avctx->priv_data;
563
564 if (avctx->channels < 1 || avctx->channels > 2) {
565 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
566 return AVERROR(EINVAL);
567 }
568 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
569 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
570
571 avcodec_get_frame_defaults(&s->frame);
572 avctx->coded_frame = &s->frame;
573
574 return 0;
575 }
576
577 /**
578 * Decode Smacker audio data
579 */
580 static int smka_decode_frame(AVCodecContext *avctx, void *data,
581 int *got_frame_ptr, AVPacket *avpkt)
582 {
583 SmackerAudioContext *s = avctx->priv_data;
584 const uint8_t *buf = avpkt->data;
585 int buf_size = avpkt->size;
586 GetBitContext gb;
587 HuffContext h[4] = { { 0 } };
588 VLC vlc[4] = { { 0 } };
589 int16_t *samples;
590 uint8_t *samples8;
591 int val;
592 int i, res, ret;
593 int unp_size;
594 int bits, stereo;
595 int pred[2] = {0, 0};
596
597 if (buf_size <= 4) {
598 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
599 return AVERROR(EINVAL);
600 }
601
602 unp_size = AV_RL32(buf);
603
604 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
605
606 if(!get_bits1(&gb)){
607 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
608 *got_frame_ptr = 0;
609 return 1;
610 }
611 stereo = get_bits1(&gb);
612 bits = get_bits1(&gb);
613 if (stereo ^ (avctx->channels != 1)) {
614 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
615 return AVERROR(EINVAL);
616 }
617 if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
618 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
619 return AVERROR(EINVAL);
620 }
621
622 /* get output buffer */
623 s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));
624 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
625 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
626 return ret;
627 }
628 samples = (int16_t *)s->frame.data[0];
629 samples8 = s->frame.data[0];
630
631 // Initialize
632 for(i = 0; i < (1 << (bits + stereo)); i++) {
633 h[i].length = 256;
634 h[i].maxlength = 0;
635 h[i].current = 0;
636 h[i].bits = av_mallocz(256 * 4);
637 h[i].lengths = av_mallocz(256 * sizeof(int));
638 h[i].values = av_mallocz(256 * sizeof(int));
639 skip_bits1(&gb);
640 smacker_decode_tree(&gb, &h[i], 0, 0);
641 skip_bits1(&gb);
642 if(h[i].current > 1) {
643 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
644 h[i].lengths, sizeof(int), sizeof(int),
645 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
646 if(res < 0) {
647 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
648 return -1;
649 }
650 }
651 }
652 if(bits) { //decode 16-bit data
653 for(i = stereo; i >= 0; i--)
654 pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
655 for(i = 0; i <= stereo; i++)
656 *samples++ = pred[i];
657 for(; i < unp_size / 2; i++) {
658 if(i & stereo) {
659 if(vlc[2].table)
660 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
661 else
662 res = 0;
663 val = h[2].values[res];
664 if(vlc[3].table)
665 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
666 else
667 res = 0;
668 val |= h[3].values[res] << 8;
669 pred[1] += sign_extend(val, 16);
670 *samples++ = av_clip_int16(pred[1]);
671 } else {
672 if(vlc[0].table)
673 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
674 else
675 res = 0;
676 val = h[0].values[res];
677 if(vlc[1].table)
678 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
679 else
680 res = 0;
681 val |= h[1].values[res] << 8;
682 pred[0] += sign_extend(val, 16);
683 *samples++ = av_clip_int16(pred[0]);
684 }
685 }
686 } else { //8-bit data
687 for(i = stereo; i >= 0; i--)
688 pred[i] = get_bits(&gb, 8);
689 for(i = 0; i <= stereo; i++)
690 *samples8++ = pred[i];
691 for(; i < unp_size; i++) {
692 if(i & stereo){
693 if(vlc[1].table)
694 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
695 else
696 res = 0;
697 pred[1] += sign_extend(h[1].values[res], 8);
698 *samples8++ = av_clip_uint8(pred[1]);
699 } else {
700 if(vlc[0].table)
701 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
702 else
703 res = 0;
704 pred[0] += sign_extend(h[0].values[res], 8);
705 *samples8++ = av_clip_uint8(pred[0]);
706 }
707 }
708 }
709
710 for(i = 0; i < 4; i++) {
711 if(vlc[i].table)
712 ff_free_vlc(&vlc[i]);
713 av_free(h[i].bits);
714 av_free(h[i].lengths);
715 av_free(h[i].values);
716 }
717
718 *got_frame_ptr = 1;
719 *(AVFrame *)data = s->frame;
720
721 return buf_size;
722 }
723
724 AVCodec ff_smacker_decoder = {
725 .name = "smackvid",
726 .type = AVMEDIA_TYPE_VIDEO,
727 .id = AV_CODEC_ID_SMACKVIDEO,
728 .priv_data_size = sizeof(SmackVContext),
729 .init = decode_init,
730 .close = decode_end,
731 .decode = decode_frame,
732 .capabilities = CODEC_CAP_DR1,
733 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
734 };
735
736 AVCodec ff_smackaud_decoder = {
737 .name = "smackaud",
738 .type = AVMEDIA_TYPE_AUDIO,
739 .id = AV_CODEC_ID_SMACKAUDIO,
740 .priv_data_size = sizeof(SmackerAudioContext),
741 .init = smka_decode_init,
742 .decode = smka_decode_frame,
743 .capabilities = CODEC_CAP_DR1,
744 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
745 };