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