You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

850 lines
26KB

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