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.

837 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. mmap_size = AV_RL32(smk->avctx->extradata);
  290. mclr_size = AV_RL32(smk->avctx->extradata + 4);
  291. full_size = AV_RL32(smk->avctx->extradata + 8);
  292. type_size = AV_RL32(smk->avctx->extradata + 12);
  293. ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
  294. if (ret < 0)
  295. return ret;
  296. if(!get_bits1(&gb)) {
  297. av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
  298. smk->mmap_tbl = av_malloc(sizeof(int) * 2);
  299. if (!smk->mmap_tbl)
  300. return AVERROR(ENOMEM);
  301. smk->mmap_tbl[0] = 0;
  302. smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
  303. } else {
  304. ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
  305. if (ret < 0)
  306. return ret;
  307. }
  308. if(!get_bits1(&gb)) {
  309. av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
  310. smk->mclr_tbl = av_malloc(sizeof(int) * 2);
  311. if (!smk->mclr_tbl)
  312. return AVERROR(ENOMEM);
  313. smk->mclr_tbl[0] = 0;
  314. smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
  315. } else {
  316. ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
  317. if (ret < 0)
  318. return ret;
  319. }
  320. if(!get_bits1(&gb)) {
  321. av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
  322. smk->full_tbl = av_malloc(sizeof(int) * 2);
  323. if (!smk->full_tbl)
  324. return AVERROR(ENOMEM);
  325. smk->full_tbl[0] = 0;
  326. smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
  327. } else {
  328. ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
  329. if (ret < 0)
  330. return ret;
  331. }
  332. if(!get_bits1(&gb)) {
  333. av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
  334. smk->type_tbl = av_malloc(sizeof(int) * 2);
  335. if (!smk->type_tbl)
  336. return AVERROR(ENOMEM);
  337. smk->type_tbl[0] = 0;
  338. smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
  339. } else {
  340. ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
  341. if (ret < 0)
  342. return ret;
  343. }
  344. return 0;
  345. }
  346. static av_always_inline void last_reset(int *recode, int *last) {
  347. recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
  348. }
  349. /* get code and update history */
  350. static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
  351. register int *table = recode;
  352. int v;
  353. while(*table & SMK_NODE) {
  354. if (get_bits_left(gb) < 1)
  355. return AVERROR_INVALIDDATA;
  356. if(get_bits1(gb))
  357. table += (*table) & (~SMK_NODE);
  358. table++;
  359. }
  360. v = *table;
  361. if(v != recode[last[0]]) {
  362. recode[last[2]] = recode[last[1]];
  363. recode[last[1]] = recode[last[0]];
  364. recode[last[0]] = v;
  365. }
  366. return v;
  367. }
  368. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  369. AVPacket *avpkt)
  370. {
  371. SmackVContext * const smk = avctx->priv_data;
  372. uint8_t *out;
  373. uint32_t *pal;
  374. GetByteContext gb2;
  375. GetBitContext gb;
  376. int blocks, blk, bw, bh;
  377. int i, ret;
  378. int stride;
  379. int flags;
  380. if (avpkt->size <= 769)
  381. return AVERROR_INVALIDDATA;
  382. if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0)
  383. return ret;
  384. /* make the palette available on the way out */
  385. pal = (uint32_t*)smk->pic->data[1];
  386. bytestream2_init(&gb2, avpkt->data, avpkt->size);
  387. flags = bytestream2_get_byteu(&gb2);
  388. smk->pic->palette_has_changed = flags & 1;
  389. smk->pic->key_frame = !!(flags & 2);
  390. if (smk->pic->key_frame)
  391. smk->pic->pict_type = AV_PICTURE_TYPE_I;
  392. else
  393. smk->pic->pict_type = AV_PICTURE_TYPE_P;
  394. for(i = 0; i < 256; i++)
  395. *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
  396. last_reset(smk->mmap_tbl, smk->mmap_last);
  397. last_reset(smk->mclr_tbl, smk->mclr_last);
  398. last_reset(smk->full_tbl, smk->full_last);
  399. last_reset(smk->type_tbl, smk->type_last);
  400. if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
  401. return ret;
  402. blk = 0;
  403. bw = avctx->width >> 2;
  404. bh = avctx->height >> 2;
  405. blocks = bw * bh;
  406. stride = smk->pic->linesize[0];
  407. while(blk < blocks) {
  408. int type, run, mode;
  409. uint16_t pix;
  410. type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
  411. if (type < 0)
  412. return type;
  413. run = block_runs[(type >> 2) & 0x3F];
  414. switch(type & 3){
  415. case SMK_BLK_MONO:
  416. while(run-- && blk < blocks){
  417. int clr, map;
  418. int hi, lo;
  419. clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
  420. map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
  421. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  422. hi = clr >> 8;
  423. lo = clr & 0xFF;
  424. for(i = 0; i < 4; i++) {
  425. if(map & 1) out[0] = hi; else out[0] = lo;
  426. if(map & 2) out[1] = hi; else out[1] = lo;
  427. if(map & 4) out[2] = hi; else out[2] = lo;
  428. if(map & 8) out[3] = hi; else out[3] = lo;
  429. map >>= 4;
  430. out += stride;
  431. }
  432. blk++;
  433. }
  434. break;
  435. case SMK_BLK_FULL:
  436. mode = 0;
  437. if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
  438. if(get_bits1(&gb)) mode = 1;
  439. else if(get_bits1(&gb)) mode = 2;
  440. }
  441. while(run-- && blk < blocks){
  442. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  443. switch(mode){
  444. case 0:
  445. for(i = 0; i < 4; i++) {
  446. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  447. AV_WL16(out+2,pix);
  448. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  449. AV_WL16(out,pix);
  450. out += stride;
  451. }
  452. break;
  453. case 1:
  454. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  455. out[0] = out[1] = pix & 0xFF;
  456. out[2] = out[3] = pix >> 8;
  457. out += stride;
  458. out[0] = out[1] = pix & 0xFF;
  459. out[2] = out[3] = pix >> 8;
  460. out += stride;
  461. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  462. out[0] = out[1] = pix & 0xFF;
  463. out[2] = out[3] = pix >> 8;
  464. out += stride;
  465. out[0] = out[1] = pix & 0xFF;
  466. out[2] = out[3] = pix >> 8;
  467. break;
  468. case 2:
  469. for(i = 0; i < 2; i++) {
  470. uint16_t pix1, pix2;
  471. pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  472. pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  473. AV_WL16(out,pix1);
  474. AV_WL16(out+2,pix2);
  475. out += stride;
  476. AV_WL16(out,pix1);
  477. AV_WL16(out+2,pix2);
  478. out += stride;
  479. }
  480. break;
  481. }
  482. blk++;
  483. }
  484. break;
  485. case SMK_BLK_SKIP:
  486. while(run-- && blk < blocks)
  487. blk++;
  488. break;
  489. case SMK_BLK_FILL:
  490. mode = type >> 8;
  491. while(run-- && blk < blocks){
  492. uint32_t col;
  493. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  494. col = mode * 0x01010101U;
  495. for(i = 0; i < 4; i++) {
  496. *((uint32_t*)out) = col;
  497. out += stride;
  498. }
  499. blk++;
  500. }
  501. break;
  502. }
  503. }
  504. if ((ret = av_frame_ref(data, smk->pic)) < 0)
  505. return ret;
  506. *got_frame = 1;
  507. /* always report that the buffer was completely consumed */
  508. return avpkt->size;
  509. }
  510. static av_cold int decode_end(AVCodecContext *avctx)
  511. {
  512. SmackVContext * const smk = avctx->priv_data;
  513. av_freep(&smk->mmap_tbl);
  514. av_freep(&smk->mclr_tbl);
  515. av_freep(&smk->full_tbl);
  516. av_freep(&smk->type_tbl);
  517. av_frame_free(&smk->pic);
  518. return 0;
  519. }
  520. static av_cold int decode_init(AVCodecContext *avctx)
  521. {
  522. SmackVContext * const c = avctx->priv_data;
  523. int ret;
  524. c->avctx = avctx;
  525. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  526. c->pic = av_frame_alloc();
  527. if (!c->pic)
  528. return AVERROR(ENOMEM);
  529. /* decode huffman trees from extradata */
  530. if(avctx->extradata_size < 16){
  531. av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
  532. decode_end(avctx);
  533. return AVERROR(EINVAL);
  534. }
  535. ret = decode_header_trees(c);
  536. if (ret < 0) {
  537. decode_end(avctx);
  538. return ret;
  539. }
  540. return 0;
  541. }
  542. static av_cold int smka_decode_init(AVCodecContext *avctx)
  543. {
  544. if (avctx->channels < 1 || avctx->channels > 2) {
  545. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  546. return AVERROR_INVALIDDATA;
  547. }
  548. avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
  549. avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
  550. return 0;
  551. }
  552. /**
  553. * Decode Smacker audio data
  554. */
  555. static int smka_decode_frame(AVCodecContext *avctx, void *data,
  556. int *got_frame_ptr, AVPacket *avpkt)
  557. {
  558. AVFrame *frame = data;
  559. const uint8_t *buf = avpkt->data;
  560. int buf_size = avpkt->size;
  561. GetBitContext gb;
  562. HuffContext h[4] = { { 0 } };
  563. VLC vlc[4] = { { 0 } };
  564. int16_t *samples;
  565. uint8_t *samples8;
  566. int val;
  567. int i, res, ret;
  568. int unp_size;
  569. int bits, stereo;
  570. int pred[2] = {0, 0};
  571. if (buf_size <= 4) {
  572. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  573. return AVERROR_INVALIDDATA;
  574. }
  575. unp_size = AV_RL32(buf);
  576. if (unp_size > (1U<<24)) {
  577. av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
  578. return AVERROR_INVALIDDATA;
  579. }
  580. if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
  581. return ret;
  582. if(!get_bits1(&gb)){
  583. av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
  584. *got_frame_ptr = 0;
  585. return 1;
  586. }
  587. stereo = get_bits1(&gb);
  588. bits = get_bits1(&gb);
  589. if (stereo ^ (avctx->channels != 1)) {
  590. av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
  591. return AVERROR_INVALIDDATA;
  592. }
  593. if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
  594. av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
  595. return AVERROR_INVALIDDATA;
  596. }
  597. /* get output buffer */
  598. frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
  599. if (unp_size % (avctx->channels * (bits + 1))) {
  600. av_log(avctx, AV_LOG_ERROR,
  601. "The buffer does not contain an integer number of samples\n");
  602. return AVERROR_INVALIDDATA;
  603. }
  604. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  605. return ret;
  606. samples = (int16_t *)frame->data[0];
  607. samples8 = frame->data[0];
  608. // Initialize
  609. for(i = 0; i < (1 << (bits + stereo)); i++) {
  610. h[i].length = 256;
  611. h[i].maxlength = 0;
  612. h[i].current = 0;
  613. h[i].bits = av_mallocz(256 * 4);
  614. h[i].lengths = av_mallocz(256 * sizeof(int));
  615. h[i].values = av_mallocz(256 * sizeof(int));
  616. if (!h[i].bits || !h[i].lengths || !h[i].values) {
  617. ret = AVERROR(ENOMEM);
  618. goto error;
  619. }
  620. skip_bits1(&gb);
  621. if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
  622. ret = AVERROR_INVALIDDATA;
  623. goto error;
  624. }
  625. skip_bits1(&gb);
  626. if(h[i].current > 1) {
  627. res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
  628. h[i].lengths, sizeof(int), sizeof(int),
  629. h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  630. if(res < 0) {
  631. av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  632. ret = AVERROR_INVALIDDATA;
  633. goto error;
  634. }
  635. }
  636. }
  637. /* this codec relies on wraparound instead of clipping audio */
  638. if(bits) { //decode 16-bit data
  639. for(i = stereo; i >= 0; i--)
  640. pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
  641. for(i = 0; i <= stereo; i++)
  642. *samples++ = pred[i];
  643. for(; i < unp_size / 2; i++) {
  644. if(get_bits_left(&gb)<0)
  645. return AVERROR_INVALIDDATA;
  646. if(i & stereo) {
  647. if(vlc[2].table)
  648. res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
  649. else
  650. res = 0;
  651. if (res < 0) {
  652. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  653. return AVERROR_INVALIDDATA;
  654. }
  655. val = h[2].values[res];
  656. if(vlc[3].table)
  657. res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
  658. else
  659. res = 0;
  660. if (res < 0) {
  661. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  662. return AVERROR_INVALIDDATA;
  663. }
  664. val |= h[3].values[res] << 8;
  665. pred[1] += (unsigned)sign_extend(val, 16);
  666. *samples++ = pred[1];
  667. } else {
  668. if(vlc[0].table)
  669. res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
  670. else
  671. res = 0;
  672. if (res < 0) {
  673. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  674. return AVERROR_INVALIDDATA;
  675. }
  676. val = h[0].values[res];
  677. if(vlc[1].table)
  678. res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
  679. else
  680. res = 0;
  681. if (res < 0) {
  682. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  683. return AVERROR_INVALIDDATA;
  684. }
  685. val |= h[1].values[res] << 8;
  686. pred[0] += (unsigned)sign_extend(val, 16);
  687. *samples++ = pred[0];
  688. }
  689. }
  690. } else { //8-bit data
  691. for(i = stereo; i >= 0; i--)
  692. pred[i] = get_bits(&gb, 8);
  693. for(i = 0; i <= stereo; i++)
  694. *samples8++ = pred[i];
  695. for(; i < unp_size; i++) {
  696. if(get_bits_left(&gb)<0)
  697. return AVERROR_INVALIDDATA;
  698. if(i & stereo){
  699. if(vlc[1].table)
  700. res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
  701. else
  702. res = 0;
  703. if (res < 0) {
  704. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  705. return AVERROR_INVALIDDATA;
  706. }
  707. pred[1] += sign_extend(h[1].values[res], 8);
  708. *samples8++ = pred[1];
  709. } else {
  710. if(vlc[0].table)
  711. res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
  712. else
  713. res = 0;
  714. if (res < 0) {
  715. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  716. return AVERROR_INVALIDDATA;
  717. }
  718. pred[0] += sign_extend(h[0].values[res], 8);
  719. *samples8++ = pred[0];
  720. }
  721. }
  722. }
  723. *got_frame_ptr = 1;
  724. ret = buf_size;
  725. error:
  726. for(i = 0; i < 4; i++) {
  727. if(vlc[i].table)
  728. ff_free_vlc(&vlc[i]);
  729. av_free(h[i].bits);
  730. av_free(h[i].lengths);
  731. av_free(h[i].values);
  732. }
  733. return ret;
  734. }
  735. AVCodec ff_smacker_decoder = {
  736. .name = "smackvid",
  737. .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
  738. .type = AVMEDIA_TYPE_VIDEO,
  739. .id = AV_CODEC_ID_SMACKVIDEO,
  740. .priv_data_size = sizeof(SmackVContext),
  741. .init = decode_init,
  742. .close = decode_end,
  743. .decode = decode_frame,
  744. .capabilities = AV_CODEC_CAP_DR1,
  745. };
  746. AVCodec ff_smackaud_decoder = {
  747. .name = "smackaud",
  748. .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
  749. .type = AVMEDIA_TYPE_AUDIO,
  750. .id = AV_CODEC_ID_SMACKAUDIO,
  751. .init = smka_decode_init,
  752. .decode = smka_decode_frame,
  753. .capabilities = AV_CODEC_CAP_DR1,
  754. };