smacker.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. * @file libavcodec/smacker.c
  23. * Smacker decoder
  24. */
  25. /*
  26. * Based on http://wiki.multimedia.cx/index.php?title=Smacker
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "avcodec.h"
  31. #define ALT_BITSTREAM_READER_LE
  32. #include "bitstream.h"
  33. #include "bytestream.h"
  34. #define SMKTREE_BITS 9
  35. #define SMK_NODE 0x80000000
  36. /*
  37. * Decoder context
  38. */
  39. typedef struct SmackVContext {
  40. AVCodecContext *avctx;
  41. AVFrame pic;
  42. int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
  43. int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
  44. } SmackVContext;
  45. /**
  46. * Context used for code reconstructing
  47. */
  48. typedef struct HuffContext {
  49. int length;
  50. int maxlength;
  51. int current;
  52. uint32_t *bits;
  53. int *lengths;
  54. int *values;
  55. } HuffContext;
  56. /* common parameters used for decode_bigtree */
  57. typedef struct DBCtx {
  58. VLC *v1, *v2;
  59. int *recode1, *recode2;
  60. int escapes[3];
  61. int *last;
  62. int lcur;
  63. } DBCtx;
  64. /* possible runs of blocks */
  65. static const int block_runs[64] = {
  66. 1, 2, 3, 4, 5, 6, 7, 8,
  67. 9, 10, 11, 12, 13, 14, 15, 16,
  68. 17, 18, 19, 20, 21, 22, 23, 24,
  69. 25, 26, 27, 28, 29, 30, 31, 32,
  70. 33, 34, 35, 36, 37, 38, 39, 40,
  71. 41, 42, 43, 44, 45, 46, 47, 48,
  72. 49, 50, 51, 52, 53, 54, 55, 56,
  73. 57, 58, 59, 128, 256, 512, 1024, 2048 };
  74. enum SmkBlockTypes {
  75. SMK_BLK_MONO = 0,
  76. SMK_BLK_FULL = 1,
  77. SMK_BLK_SKIP = 2,
  78. SMK_BLK_FILL = 3 };
  79. /**
  80. * Decode local frame tree
  81. */
  82. static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
  83. {
  84. if(!get_bits1(gb)){ //Leaf
  85. if(hc->current >= 256){
  86. av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
  87. return -1;
  88. }
  89. if(length){
  90. hc->bits[hc->current] = prefix;
  91. hc->lengths[hc->current] = length;
  92. } else {
  93. hc->bits[hc->current] = 0;
  94. hc->lengths[hc->current] = 0;
  95. }
  96. hc->values[hc->current] = get_bits(gb, 8);
  97. hc->current++;
  98. if(hc->maxlength < length)
  99. hc->maxlength = length;
  100. return 0;
  101. } else { //Node
  102. int r;
  103. length++;
  104. r = smacker_decode_tree(gb, hc, prefix, length);
  105. if(r)
  106. return r;
  107. return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
  108. }
  109. }
  110. /**
  111. * Decode header tree
  112. */
  113. static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
  114. {
  115. if(!get_bits1(gb)){ //Leaf
  116. int val, i1, i2, b1, b2;
  117. if(hc->current >= hc->length){
  118. av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
  119. return -1;
  120. }
  121. b1 = get_bits_count(gb);
  122. i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
  123. b1 = get_bits_count(gb) - b1;
  124. b2 = get_bits_count(gb);
  125. i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
  126. b2 = get_bits_count(gb) - b2;
  127. if (i1 < 0 || i2 < 0)
  128. return -1;
  129. val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
  130. if(val == ctx->escapes[0]) {
  131. ctx->last[0] = hc->current;
  132. val = 0;
  133. } else if(val == ctx->escapes[1]) {
  134. ctx->last[1] = hc->current;
  135. val = 0;
  136. } else if(val == ctx->escapes[2]) {
  137. ctx->last[2] = hc->current;
  138. val = 0;
  139. }
  140. hc->values[hc->current++] = val;
  141. return 1;
  142. } else { //Node
  143. int r = 0, t;
  144. t = hc->current++;
  145. r = smacker_decode_bigtree(gb, hc, ctx);
  146. if(r < 0)
  147. return r;
  148. hc->values[t] = SMK_NODE | r;
  149. r++;
  150. r += smacker_decode_bigtree(gb, hc, ctx);
  151. return r;
  152. }
  153. }
  154. /**
  155. * Store large tree as FFmpeg's vlc codes
  156. */
  157. static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
  158. {
  159. int res;
  160. HuffContext huff;
  161. HuffContext tmp1, tmp2;
  162. VLC vlc[2];
  163. int escapes[3];
  164. DBCtx ctx;
  165. if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
  166. av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
  167. return -1;
  168. }
  169. tmp1.length = 256;
  170. tmp1.maxlength = 0;
  171. tmp1.current = 0;
  172. tmp1.bits = av_mallocz(256 * 4);
  173. tmp1.lengths = av_mallocz(256 * sizeof(int));
  174. tmp1.values = av_mallocz(256 * sizeof(int));
  175. tmp2.length = 256;
  176. tmp2.maxlength = 0;
  177. tmp2.current = 0;
  178. tmp2.bits = av_mallocz(256 * 4);
  179. tmp2.lengths = av_mallocz(256 * sizeof(int));
  180. tmp2.values = av_mallocz(256 * sizeof(int));
  181. memset(&vlc[0], 0, sizeof(VLC));
  182. memset(&vlc[1], 0, sizeof(VLC));
  183. if(get_bits1(gb)) {
  184. smacker_decode_tree(gb, &tmp1, 0, 0);
  185. skip_bits1(gb);
  186. res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
  187. tmp1.lengths, sizeof(int), sizeof(int),
  188. tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  189. if(res < 0) {
  190. av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  191. return -1;
  192. }
  193. } else {
  194. av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
  195. }
  196. if(get_bits1(gb)){
  197. smacker_decode_tree(gb, &tmp2, 0, 0);
  198. skip_bits1(gb);
  199. res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
  200. tmp2.lengths, sizeof(int), sizeof(int),
  201. tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  202. if(res < 0) {
  203. av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  204. return -1;
  205. }
  206. } else {
  207. av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
  208. }
  209. escapes[0] = get_bits(gb, 8);
  210. escapes[0] |= get_bits(gb, 8) << 8;
  211. escapes[1] = get_bits(gb, 8);
  212. escapes[1] |= get_bits(gb, 8) << 8;
  213. escapes[2] = get_bits(gb, 8);
  214. escapes[2] |= get_bits(gb, 8) << 8;
  215. last[0] = last[1] = last[2] = -1;
  216. ctx.escapes[0] = escapes[0];
  217. ctx.escapes[1] = escapes[1];
  218. ctx.escapes[2] = escapes[2];
  219. ctx.v1 = &vlc[0];
  220. ctx.v2 = &vlc[1];
  221. ctx.recode1 = tmp1.values;
  222. ctx.recode2 = tmp2.values;
  223. ctx.last = last;
  224. huff.length = ((size + 3) >> 2) + 3;
  225. huff.maxlength = 0;
  226. huff.current = 0;
  227. huff.values = av_mallocz(huff.length * sizeof(int));
  228. smacker_decode_bigtree(gb, &huff, &ctx);
  229. skip_bits1(gb);
  230. if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
  231. if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
  232. if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
  233. *recodes = huff.values;
  234. if(vlc[0].table)
  235. free_vlc(&vlc[0]);
  236. if(vlc[1].table)
  237. free_vlc(&vlc[1]);
  238. av_free(tmp1.bits);
  239. av_free(tmp1.lengths);
  240. av_free(tmp1.values);
  241. av_free(tmp2.bits);
  242. av_free(tmp2.lengths);
  243. av_free(tmp2.values);
  244. return 0;
  245. }
  246. static int decode_header_trees(SmackVContext *smk) {
  247. GetBitContext gb;
  248. int mmap_size, mclr_size, full_size, type_size;
  249. mmap_size = AV_RL32(smk->avctx->extradata);
  250. mclr_size = AV_RL32(smk->avctx->extradata + 4);
  251. full_size = AV_RL32(smk->avctx->extradata + 8);
  252. type_size = AV_RL32(smk->avctx->extradata + 12);
  253. init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
  254. if(!get_bits1(&gb)) {
  255. av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
  256. smk->mmap_tbl = av_malloc(sizeof(int) * 2);
  257. smk->mmap_tbl[0] = 0;
  258. smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
  259. } else {
  260. if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
  261. return -1;
  262. }
  263. if(!get_bits1(&gb)) {
  264. av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
  265. smk->mclr_tbl = av_malloc(sizeof(int) * 2);
  266. smk->mclr_tbl[0] = 0;
  267. smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
  268. } else {
  269. if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
  270. return -1;
  271. }
  272. if(!get_bits1(&gb)) {
  273. av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
  274. smk->full_tbl = av_malloc(sizeof(int) * 2);
  275. smk->full_tbl[0] = 0;
  276. smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
  277. } else {
  278. if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
  279. return -1;
  280. }
  281. if(!get_bits1(&gb)) {
  282. av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
  283. smk->type_tbl = av_malloc(sizeof(int) * 2);
  284. smk->type_tbl[0] = 0;
  285. smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
  286. } else {
  287. if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
  288. return -1;
  289. }
  290. return 0;
  291. }
  292. static av_always_inline void last_reset(int *recode, int *last) {
  293. recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
  294. }
  295. /* get code and update history */
  296. static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
  297. register int *table = recode;
  298. int v, b;
  299. b = get_bits_count(gb);
  300. while(*table & SMK_NODE) {
  301. if(get_bits1(gb))
  302. table += (*table) & (~SMK_NODE);
  303. table++;
  304. }
  305. v = *table;
  306. b = get_bits_count(gb) - b;
  307. if(v != recode[last[0]]) {
  308. recode[last[2]] = recode[last[1]];
  309. recode[last[1]] = recode[last[0]];
  310. recode[last[0]] = v;
  311. }
  312. return v;
  313. }
  314. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
  315. {
  316. SmackVContext * const smk = avctx->priv_data;
  317. uint8_t *out;
  318. uint32_t *pal;
  319. GetBitContext gb;
  320. int blocks, blk, bw, bh;
  321. int i;
  322. int stride;
  323. if(buf_size <= 769)
  324. return 0;
  325. if(smk->pic.data[0])
  326. avctx->release_buffer(avctx, &smk->pic);
  327. smk->pic.reference = 1;
  328. smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  329. if(avctx->reget_buffer(avctx, &smk->pic) < 0){
  330. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  331. return -1;
  332. }
  333. /* make the palette available on the way out */
  334. pal = (uint32_t*)smk->pic.data[1];
  335. smk->pic.palette_has_changed = buf[0] & 1;
  336. smk->pic.key_frame = !!(buf[0] & 2);
  337. if(smk->pic.key_frame)
  338. smk->pic.pict_type = FF_I_TYPE;
  339. else
  340. smk->pic.pict_type = FF_P_TYPE;
  341. buf++;
  342. for(i = 0; i < 256; i++)
  343. *pal++ = bytestream_get_be24(&buf);
  344. buf_size -= 769;
  345. last_reset(smk->mmap_tbl, smk->mmap_last);
  346. last_reset(smk->mclr_tbl, smk->mclr_last);
  347. last_reset(smk->full_tbl, smk->full_last);
  348. last_reset(smk->type_tbl, smk->type_last);
  349. init_get_bits(&gb, buf, buf_size * 8);
  350. blk = 0;
  351. bw = avctx->width >> 2;
  352. bh = avctx->height >> 2;
  353. blocks = bw * bh;
  354. out = smk->pic.data[0];
  355. stride = smk->pic.linesize[0];
  356. while(blk < blocks) {
  357. int type, run, mode;
  358. uint16_t pix;
  359. type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
  360. run = block_runs[(type >> 2) & 0x3F];
  361. switch(type & 3){
  362. case SMK_BLK_MONO:
  363. while(run-- && blk < blocks){
  364. int clr, map;
  365. int hi, lo;
  366. clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
  367. map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
  368. out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  369. hi = clr >> 8;
  370. lo = clr & 0xFF;
  371. for(i = 0; i < 4; i++) {
  372. if(map & 1) out[0] = hi; else out[0] = lo;
  373. if(map & 2) out[1] = hi; else out[1] = lo;
  374. if(map & 4) out[2] = hi; else out[2] = lo;
  375. if(map & 8) out[3] = hi; else out[3] = lo;
  376. map >>= 4;
  377. out += stride;
  378. }
  379. blk++;
  380. }
  381. break;
  382. case SMK_BLK_FULL:
  383. mode = 0;
  384. if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
  385. if(get_bits1(&gb)) mode = 1;
  386. else if(get_bits1(&gb)) mode = 2;
  387. }
  388. while(run-- && blk < blocks){
  389. out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  390. switch(mode){
  391. case 0:
  392. for(i = 0; i < 4; i++) {
  393. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  394. AV_WL16(out+2,pix);
  395. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  396. AV_WL16(out,pix);
  397. out += stride;
  398. }
  399. break;
  400. case 1:
  401. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  402. out[0] = out[1] = pix & 0xFF;
  403. out[2] = out[3] = pix >> 8;
  404. out += stride;
  405. out[0] = out[1] = pix & 0xFF;
  406. out[2] = out[3] = pix >> 8;
  407. out += stride;
  408. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  409. out[0] = out[1] = pix & 0xFF;
  410. out[2] = out[3] = pix >> 8;
  411. out += stride;
  412. out[0] = out[1] = pix & 0xFF;
  413. out[2] = out[3] = pix >> 8;
  414. out += stride;
  415. break;
  416. case 2:
  417. for(i = 0; i < 2; i++) {
  418. uint16_t pix1, pix2;
  419. pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  420. pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  421. AV_WL16(out,pix1);
  422. AV_WL16(out+2,pix2);
  423. out += stride;
  424. AV_WL16(out,pix1);
  425. AV_WL16(out+2,pix2);
  426. out += stride;
  427. }
  428. break;
  429. }
  430. blk++;
  431. }
  432. break;
  433. case SMK_BLK_SKIP:
  434. while(run-- && blk < blocks)
  435. blk++;
  436. break;
  437. case SMK_BLK_FILL:
  438. mode = type >> 8;
  439. while(run-- && blk < blocks){
  440. uint32_t col;
  441. out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  442. col = mode * 0x01010101;
  443. for(i = 0; i < 4; i++) {
  444. *((uint32_t*)out) = col;
  445. out += stride;
  446. }
  447. blk++;
  448. }
  449. break;
  450. }
  451. }
  452. *data_size = sizeof(AVFrame);
  453. *(AVFrame*)data = smk->pic;
  454. /* always report that the buffer was completely consumed */
  455. return buf_size;
  456. }
  457. /*
  458. *
  459. * Init smacker decoder
  460. *
  461. */
  462. static av_cold int decode_init(AVCodecContext *avctx)
  463. {
  464. SmackVContext * const c = avctx->priv_data;
  465. c->avctx = avctx;
  466. c->pic.data[0] = NULL;
  467. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  468. return 1;
  469. }
  470. avctx->pix_fmt = PIX_FMT_PAL8;
  471. /* decode huffman trees from extradata */
  472. if(avctx->extradata_size < 16){
  473. av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
  474. return -1;
  475. }
  476. if (decode_header_trees(c))
  477. return -1;
  478. return 0;
  479. }
  480. /*
  481. *
  482. * Uninit smacker decoder
  483. *
  484. */
  485. static av_cold int decode_end(AVCodecContext *avctx)
  486. {
  487. SmackVContext * const smk = avctx->priv_data;
  488. av_freep(&smk->mmap_tbl);
  489. av_freep(&smk->mclr_tbl);
  490. av_freep(&smk->full_tbl);
  491. av_freep(&smk->type_tbl);
  492. if (smk->pic.data[0])
  493. avctx->release_buffer(avctx, &smk->pic);
  494. return 0;
  495. }
  496. static av_cold int smka_decode_init(AVCodecContext *avctx)
  497. {
  498. avctx->sample_fmt = SAMPLE_FMT_S16;
  499. avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
  500. return 0;
  501. }
  502. /**
  503. * Decode Smacker audio data
  504. */
  505. static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
  506. {
  507. GetBitContext gb;
  508. HuffContext h[4];
  509. VLC vlc[4];
  510. int16_t *samples = data;
  511. int val;
  512. int i, res;
  513. int unp_size;
  514. int bits, stereo;
  515. int pred[2] = {0, 0};
  516. unp_size = AV_RL32(buf);
  517. init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
  518. if(!get_bits1(&gb)){
  519. av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
  520. *data_size = 0;
  521. return 1;
  522. }
  523. stereo = get_bits1(&gb);
  524. bits = get_bits1(&gb);
  525. if (unp_size & 0xC0000000 || (unp_size << !bits) > *data_size) {
  526. av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
  527. return -1;
  528. }
  529. memset(vlc, 0, sizeof(VLC) * 4);
  530. memset(h, 0, sizeof(HuffContext) * 4);
  531. // Initialize
  532. for(i = 0; i < (1 << (bits + stereo)); i++) {
  533. h[i].length = 256;
  534. h[i].maxlength = 0;
  535. h[i].current = 0;
  536. h[i].bits = av_mallocz(256 * 4);
  537. h[i].lengths = av_mallocz(256 * sizeof(int));
  538. h[i].values = av_mallocz(256 * sizeof(int));
  539. skip_bits1(&gb);
  540. smacker_decode_tree(&gb, &h[i], 0, 0);
  541. skip_bits1(&gb);
  542. if(h[i].current > 1) {
  543. res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
  544. h[i].lengths, sizeof(int), sizeof(int),
  545. h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  546. if(res < 0) {
  547. av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  548. return -1;
  549. }
  550. }
  551. }
  552. if(bits) { //decode 16-bit data
  553. for(i = stereo; i >= 0; i--)
  554. pred[i] = bswap_16(get_bits(&gb, 16));
  555. for(i = 0; i < stereo; i++)
  556. *samples++ = pred[i];
  557. for(i = 0; i < unp_size / 2; i++) {
  558. if(i & stereo) {
  559. if(vlc[2].table)
  560. res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
  561. else
  562. res = 0;
  563. val = h[2].values[res];
  564. if(vlc[3].table)
  565. res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
  566. else
  567. res = 0;
  568. val |= h[3].values[res] << 8;
  569. pred[1] += (int16_t)val;
  570. *samples++ = pred[1];
  571. } else {
  572. if(vlc[0].table)
  573. res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
  574. else
  575. res = 0;
  576. val = h[0].values[res];
  577. if(vlc[1].table)
  578. res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
  579. else
  580. res = 0;
  581. val |= h[1].values[res] << 8;
  582. pred[0] += val;
  583. *samples++ = pred[0];
  584. }
  585. }
  586. } else { //8-bit data
  587. for(i = stereo; i >= 0; i--)
  588. pred[i] = get_bits(&gb, 8);
  589. if (stereo + unp_size > *data_size)
  590. return -1;
  591. for(i = 0; i < stereo; i++)
  592. *samples++ = (pred[i] - 0x80) << 8;
  593. for(i = 0; i < unp_size; i++) {
  594. if(i & stereo){
  595. if(vlc[1].table)
  596. res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
  597. else
  598. res = 0;
  599. pred[1] += (int8_t)h[1].values[res];
  600. *samples++ = (pred[1] - 0x80) << 8;
  601. } else {
  602. if(vlc[0].table)
  603. res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
  604. else
  605. res = 0;
  606. pred[0] += (int8_t)h[0].values[res];
  607. *samples++ = (pred[0] - 0x80) << 8;
  608. }
  609. }
  610. unp_size *= 2;
  611. }
  612. for(i = 0; i < 4; i++) {
  613. if(vlc[i].table)
  614. free_vlc(&vlc[i]);
  615. if(h[i].bits)
  616. av_free(h[i].bits);
  617. if(h[i].lengths)
  618. av_free(h[i].lengths);
  619. if(h[i].values)
  620. av_free(h[i].values);
  621. }
  622. *data_size = unp_size;
  623. return buf_size;
  624. }
  625. AVCodec smacker_decoder = {
  626. "smackvid",
  627. CODEC_TYPE_VIDEO,
  628. CODEC_ID_SMACKVIDEO,
  629. sizeof(SmackVContext),
  630. decode_init,
  631. NULL,
  632. decode_end,
  633. decode_frame,
  634. .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
  635. };
  636. AVCodec smackaud_decoder = {
  637. "smackaud",
  638. CODEC_TYPE_AUDIO,
  639. CODEC_ID_SMACKAUDIO,
  640. 0,
  641. smka_decode_init,
  642. NULL,
  643. NULL,
  644. smka_decode_frame,
  645. .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
  646. };