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