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.

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