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.

821 lines
25KB

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