smacker: fix an off by one in huff.length computation
[libav.git] / libavcodec / smacker.c
CommitLineData
348efc18
KS
1/*
2 * Smacker decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
348efc18
KS
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
348efc18 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
348efc18
KS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
348efc18 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
348efc18
KS
20 */
21
22/**
ba87f080 23 * @file
348efc18
KS
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
a903f8f0 34#include "libavutil/channel_layout.h"
348efc18 35#include "avcodec.h"
594d4d5d 36#include "internal.h"
c86c3c80 37#include "mathops.h"
348efc18 38
aaf47bcd 39#define BITSTREAM_READER_LE
9106a698 40#include "get_bits.h"
3e368d72 41#include "bytestream.h"
348efc18
KS
42
43#define SMKTREE_BITS 9
44#define SMK_NODE 0x80000000
45
46/*
47 * Decoder context
48 */
49typedef 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 */
60typedef 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 */
70typedef 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 */
79static 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
89enum 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 */
98static 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 */
130static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
131{
9adf25c1
AC
132 if (hc->current + 1 >= hc->length) {
133 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
134 return -1;
135 }
348efc18 136 if(!get_bits1(gb)){ //Leaf
779f8bc2 137 int val, i1, i2;
9676ffba 138 i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
9676ffba 139 i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
64894554
LA
140 if (i1 < 0 || i2 < 0)
141 return -1;
348efc18
KS
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
9adf25c1 157 int r = 0, r_new, t;
348efc18
KS
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++;
9adf25c1
AC
165 r_new = smacker_decode_bigtree(gb, hc, ctx);
166 if (r_new < 0)
167 return r_new;
168 return r + r_new;
348efc18
KS
169 }
170}
171
172/**
2912e87a 173 * Store large tree as Libav's vlc codes
348efc18
KS
174 */
175static 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;
a92be9b8 180 VLC vlc[2] = { { 0 } };
348efc18
KS
181 int escapes[3];
182 DBCtx ctx;
9adf25c1 183 int err = 0;
348efc18 184
3a1a7e32
MN
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
348efc18
KS
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
348efc18
KS
204 if(get_bits1(gb)) {
205 smacker_decode_tree(gb, &tmp1, 0, 0);
7ae7300e 206 skip_bits1(gb);
348efc18
KS
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);
7ae7300e 219 skip_bits1(gb);
348efc18
KS
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
f52edef3 249 huff.length = ((size + 3) >> 2) + 4;
348efc18
KS
250 huff.maxlength = 0;
251 huff.current = 0;
252 huff.values = av_mallocz(huff.length * sizeof(int));
253
9adf25c1
AC
254 if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
255 err = -1;
7ae7300e 256 skip_bits1(gb);
348efc18
KS
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)
e96b4a53 264 ff_free_vlc(&vlc[0]);
348efc18 265 if(vlc[1].table)
e96b4a53 266 ff_free_vlc(&vlc[1]);
348efc18
KS
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
9adf25c1 274 return err;
348efc18
KS
275}
276
277static int decode_header_trees(SmackVContext *smk) {
278 GetBitContext gb;
279 int mmap_size, mclr_size, full_size, type_size;
280
fead30d4
AB
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);
348efc18
KS
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 {
9676ffba
LA
294 if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
295 return -1;
348efc18 296 }
5fc32c27 297 if(!get_bits1(&gb)) {
348efc18
KS
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 {
9676ffba
LA
303 if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
304 return -1;
348efc18 305 }
5fc32c27 306 if(!get_bits1(&gb)) {
348efc18
KS
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 {
9676ffba
LA
312 if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
313 return -1;
348efc18 314 }
5fc32c27 315 if(!get_bits1(&gb)) {
348efc18
KS
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 {
9676ffba
LA
321 if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
322 return -1;
348efc18
KS
323 }
324
325 return 0;
326}
327
849f1035 328static av_always_inline void last_reset(int *recode, int *last) {
348efc18
KS
329 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
330}
331
332/* get code and update history */
849f1035 333static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
348efc18 334 register int *table = recode;
90c0c83e 335 int v;
348efc18 336
348efc18
KS
337 while(*table & SMK_NODE) {
338 if(get_bits1(gb))
339 table += (*table) & (~SMK_NODE);
340 table++;
341 }
342 v = *table;
348efc18
KS
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
df9b9567
AK
352static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
353 AVPacket *avpkt)
348efc18 354{
e4141433 355 SmackVContext * const smk = avctx->priv_data;
348efc18
KS
356 uint8_t *out;
357 uint32_t *pal;
b2af057a 358 GetByteContext gb2;
348efc18
KS
359 GetBitContext gb;
360 int blocks, blk, bw, bh;
759001c5 361 int i, ret;
348efc18 362 int stride;
b2af057a 363 int flags;
348efc18 364
b2af057a 365 if (avpkt->size <= 769)
348efc18 366 return 0;
348efc18 367
759001c5 368 if ((ret = ff_reget_buffer(avctx, &smk->pic)) < 0) {
348efc18 369 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
759001c5 370 return ret;
348efc18
KS
371 }
372
373 /* make the palette available on the way out */
348efc18 374 pal = (uint32_t*)smk->pic.data[1];
b2af057a
RB
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);
348efc18 379 if(smk->pic.key_frame)
975a1447 380 smk->pic.pict_type = AV_PICTURE_TYPE_I;
348efc18 381 else
975a1447 382 smk->pic.pict_type = AV_PICTURE_TYPE_P;
348efc18 383
3e368d72 384 for(i = 0; i < 256; i++)
b2af057a 385 *pal++ = bytestream2_get_be24u(&gb2);
348efc18
KS
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);
b2af057a 391 init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
348efc18
KS
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;
103eee53 428 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
348efc18
KS
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);
2c124cb6 438 AV_WL16(out+2,pix);
348efc18 439 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
2c124cb6 440 AV_WL16(out,pix);
348efc18
KS
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;
348efc18 463 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
40a19c44 464 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
2c124cb6
RP
465 AV_WL16(out,pix1);
466 AV_WL16(out+2,pix2);
348efc18 467 out += stride;
2c124cb6
RP
468 AV_WL16(out,pix1);
469 AV_WL16(out+2,pix2);
348efc18
KS
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
759001c5
AK
498 if ((ret = av_frame_ref(data, &smk->pic)) < 0)
499 return ret;
500
df9b9567 501 *got_frame = 1;
348efc18
KS
502
503 /* always report that the buffer was completely consumed */
b2af057a 504 return avpkt->size;
348efc18
KS
505}
506
507
508
509/*
510 *
511 * Init smacker decoder
512 *
513 */
98a6fff9 514static av_cold int decode_init(AVCodecContext *avctx)
348efc18 515{
e4141433 516 SmackVContext * const c = avctx->priv_data;
348efc18
KS
517
518 c->avctx = avctx;
348efc18 519
716d413c 520 avctx->pix_fmt = AV_PIX_FMT_PAL8;
3b199d29 521 avcodec_get_frame_defaults(&c->pic);
348efc18
KS
522
523 /* decode huffman trees from extradata */
524 if(avctx->extradata_size < 16){
525 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
526 return -1;
527 }
528
9676ffba
LA
529 if (decode_header_trees(c))
530 return -1;
348efc18
KS
531
532 return 0;
533}
534
535
536
537/*
538 *
539 * Uninit smacker decoder
540 *
541 */
98a6fff9 542static av_cold int decode_end(AVCodecContext *avctx)
348efc18 543{
e4141433 544 SmackVContext * const smk = avctx->priv_data;
348efc18 545
34a8dcd0
KS
546 av_freep(&smk->mmap_tbl);
547 av_freep(&smk->mclr_tbl);
548 av_freep(&smk->full_tbl);
549 av_freep(&smk->type_tbl);
348efc18 550
759001c5 551 av_frame_unref(&smk->pic);
348efc18
KS
552
553 return 0;
554}
555
556
98a6fff9 557static av_cold int smka_decode_init(AVCodecContext *avctx)
348efc18 558{
e190e453
JR
559 if (avctx->channels < 1 || avctx->channels > 2) {
560 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
561 return AVERROR(EINVAL);
562 }
c2fcd0a7 563 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
5d6e4c16 564 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
0eea2129 565
348efc18
KS
566 return 0;
567}
568
569/**
570 * Decode Smacker audio data
571 */
0eea2129
JR
572static int smka_decode_frame(AVCodecContext *avctx, void *data,
573 int *got_frame_ptr, AVPacket *avpkt)
348efc18 574{
903b62cc 575 AVFrame *frame = data;
7a00bbad
TB
576 const uint8_t *buf = avpkt->data;
577 int buf_size = avpkt->size;
348efc18 578 GetBitContext gb;
a92be9b8
DB
579 HuffContext h[4] = { { 0 } };
580 VLC vlc[4] = { { 0 } };
0eea2129
JR
581 int16_t *samples;
582 uint8_t *samples8;
348efc18 583 int val;
0eea2129 584 int i, res, ret;
348efc18
KS
585 int unp_size;
586 int bits, stereo;
587 int pred[2] = {0, 0};
588
cf044f8b
JR
589 if (buf_size <= 4) {
590 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
591 return AVERROR(EINVAL);
592 }
593
fead30d4 594 unp_size = AV_RL32(buf);
348efc18
KS
595
596 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
597
598 if(!get_bits1(&gb)){
599 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
0eea2129 600 *got_frame_ptr = 0;
348efc18
KS
601 return 1;
602 }
603 stereo = get_bits1(&gb);
604 bits = get_bits1(&gb);
ff1f89de
JR
605 if (stereo ^ (avctx->channels != 1)) {
606 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
607 return AVERROR(EINVAL);
608 }
609 if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
610 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
611 return AVERROR(EINVAL);
612 }
348efc18 613
0eea2129 614 /* get output buffer */
903b62cc 615 frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
759001c5 616 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
0eea2129
JR
617 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
618 return ret;
619 }
903b62cc
JR
620 samples = (int16_t *)frame->data[0];
621 samples8 = frame->data[0];
0eea2129 622
348efc18
KS
623 // Initialize
624 for(i = 0; i < (1 << (bits + stereo)); i++) {
625 h[i].length = 256;
626 h[i].maxlength = 0;
627 h[i].current = 0;
628 h[i].bits = av_mallocz(256 * 4);
629 h[i].lengths = av_mallocz(256 * sizeof(int));
630 h[i].values = av_mallocz(256 * sizeof(int));
7ae7300e 631 skip_bits1(&gb);
348efc18 632 smacker_decode_tree(&gb, &h[i], 0, 0);
7ae7300e 633 skip_bits1(&gb);
76fabb45
KS
634 if(h[i].current > 1) {
635 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
348efc18
KS
636 h[i].lengths, sizeof(int), sizeof(int),
637 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
76fabb45
KS
638 if(res < 0) {
639 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
640 return -1;
641 }
348efc18
KS
642 }
643 }
268bb885 644 /* this codec relies on wraparound instead of clipping audio */
348efc18 645 if(bits) { //decode 16-bit data
c72e05ba 646 for(i = stereo; i >= 0; i--)
12cbbbb4 647 pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
5166376f 648 for(i = 0; i <= stereo; i++)
c72e05ba 649 *samples++ = pred[i];
5166376f 650 for(; i < unp_size / 2; i++) {
348efc18 651 if(i & stereo) {
76fabb45
KS
652 if(vlc[2].table)
653 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
654 else
655 res = 0;
656 val = h[2].values[res];
657 if(vlc[3].table)
658 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
659 else
660 res = 0;
661 val |= h[3].values[res] << 8;
c86c3c80 662 pred[1] += sign_extend(val, 16);
5f30c6c8 663 *samples++ = pred[1];
348efc18 664 } else {
76fabb45
KS
665 if(vlc[0].table)
666 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
667 else
668 res = 0;
669 val = h[0].values[res];
670 if(vlc[1].table)
671 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
672 else
673 res = 0;
674 val |= h[1].values[res] << 8;
c86c3c80 675 pred[0] += sign_extend(val, 16);
5f30c6c8 676 *samples++ = pred[0];
348efc18
KS
677 }
678 }
679 } else { //8-bit data
c72e05ba
KS
680 for(i = stereo; i >= 0; i--)
681 pred[i] = get_bits(&gb, 8);
5166376f 682 for(i = 0; i <= stereo; i++)
e1dc16ae 683 *samples8++ = pred[i];
5166376f 684 for(; i < unp_size; i++) {
348efc18 685 if(i & stereo){
76fabb45
KS
686 if(vlc[1].table)
687 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
688 else
689 res = 0;
c86c3c80 690 pred[1] += sign_extend(h[1].values[res], 8);
5f30c6c8 691 *samples8++ = pred[1];
348efc18 692 } else {
76fabb45
KS
693 if(vlc[0].table)
694 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
695 else
696 res = 0;
c86c3c80 697 pred[0] += sign_extend(h[0].values[res], 8);
5f30c6c8 698 *samples8++ = pred[0];
348efc18
KS
699 }
700 }
348efc18
KS
701 }
702
703 for(i = 0; i < 4; i++) {
704 if(vlc[i].table)
e96b4a53 705 ff_free_vlc(&vlc[i]);
437fb1c8
CB
706 av_free(h[i].bits);
707 av_free(h[i].lengths);
708 av_free(h[i].values);
348efc18
KS
709 }
710
903b62cc 711 *got_frame_ptr = 1;
0eea2129 712
348efc18
KS
713 return buf_size;
714}
715
d36beb3f 716AVCodec ff_smacker_decoder = {
ec6402b7
AK
717 .name = "smackvid",
718 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 719 .id = AV_CODEC_ID_SMACKVIDEO,
ec6402b7
AK
720 .priv_data_size = sizeof(SmackVContext),
721 .init = decode_init,
722 .close = decode_end,
723 .decode = decode_frame,
724 .capabilities = CODEC_CAP_DR1,
00c3b67b 725 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
348efc18
KS
726};
727
d36beb3f 728AVCodec ff_smackaud_decoder = {
ec6402b7
AK
729 .name = "smackaud",
730 .type = AVMEDIA_TYPE_AUDIO,
36ef5369 731 .id = AV_CODEC_ID_SMACKAUDIO,
ec6402b7
AK
732 .init = smka_decode_init,
733 .decode = smka_decode_frame,
0eea2129 734 .capabilities = CODEC_CAP_DR1,
00c3b67b 735 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
348efc18 736};