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