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.

828 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_array(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. stride = smk->pic->linesize[0];
  397. while(blk < blocks) {
  398. int type, run, mode;
  399. uint16_t pix;
  400. type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
  401. run = block_runs[(type >> 2) & 0x3F];
  402. switch(type & 3){
  403. case SMK_BLK_MONO:
  404. while(run-- && blk < blocks){
  405. int clr, map;
  406. int hi, lo;
  407. clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
  408. map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
  409. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  410. hi = clr >> 8;
  411. lo = clr & 0xFF;
  412. for(i = 0; i < 4; i++) {
  413. if(map & 1) out[0] = hi; else out[0] = lo;
  414. if(map & 2) out[1] = hi; else out[1] = lo;
  415. if(map & 4) out[2] = hi; else out[2] = lo;
  416. if(map & 8) out[3] = hi; else out[3] = lo;
  417. map >>= 4;
  418. out += stride;
  419. }
  420. blk++;
  421. }
  422. break;
  423. case SMK_BLK_FULL:
  424. mode = 0;
  425. if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
  426. if(get_bits1(&gb)) mode = 1;
  427. else if(get_bits1(&gb)) mode = 2;
  428. }
  429. while(run-- && blk < blocks){
  430. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  431. switch(mode){
  432. case 0:
  433. for(i = 0; i < 4; i++) {
  434. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  435. AV_WL16(out+2,pix);
  436. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  437. AV_WL16(out,pix);
  438. out += stride;
  439. }
  440. break;
  441. case 1:
  442. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  443. out[0] = out[1] = pix & 0xFF;
  444. out[2] = out[3] = pix >> 8;
  445. out += stride;
  446. out[0] = out[1] = pix & 0xFF;
  447. out[2] = out[3] = pix >> 8;
  448. out += stride;
  449. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  450. out[0] = out[1] = pix & 0xFF;
  451. out[2] = out[3] = pix >> 8;
  452. out += stride;
  453. out[0] = out[1] = pix & 0xFF;
  454. out[2] = out[3] = pix >> 8;
  455. break;
  456. case 2:
  457. for(i = 0; i < 2; i++) {
  458. uint16_t pix1, pix2;
  459. pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  460. pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  461. AV_WL16(out,pix1);
  462. AV_WL16(out+2,pix2);
  463. out += stride;
  464. AV_WL16(out,pix1);
  465. AV_WL16(out+2,pix2);
  466. out += stride;
  467. }
  468. break;
  469. }
  470. blk++;
  471. }
  472. break;
  473. case SMK_BLK_SKIP:
  474. while(run-- && blk < blocks)
  475. blk++;
  476. break;
  477. case SMK_BLK_FILL:
  478. mode = type >> 8;
  479. while(run-- && blk < blocks){
  480. uint32_t col;
  481. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  482. col = mode * 0x01010101;
  483. for(i = 0; i < 4; i++) {
  484. *((uint32_t*)out) = col;
  485. out += stride;
  486. }
  487. blk++;
  488. }
  489. break;
  490. }
  491. }
  492. if ((ret = av_frame_ref(data, smk->pic)) < 0)
  493. return ret;
  494. *got_frame = 1;
  495. /* always report that the buffer was completely consumed */
  496. return avpkt->size;
  497. }
  498. /*
  499. *
  500. * Uninit smacker decoder
  501. *
  502. */
  503. static av_cold int decode_end(AVCodecContext *avctx)
  504. {
  505. SmackVContext * const smk = avctx->priv_data;
  506. av_freep(&smk->mmap_tbl);
  507. av_freep(&smk->mclr_tbl);
  508. av_freep(&smk->full_tbl);
  509. av_freep(&smk->type_tbl);
  510. av_frame_free(&smk->pic);
  511. return 0;
  512. }
  513. /*
  514. *
  515. * Init smacker decoder
  516. *
  517. */
  518. static av_cold int decode_init(AVCodecContext *avctx)
  519. {
  520. SmackVContext * const c = avctx->priv_data;
  521. int ret;
  522. c->avctx = avctx;
  523. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  524. c->pic = av_frame_alloc();
  525. if (!c->pic)
  526. return AVERROR(ENOMEM);
  527. /* decode huffman trees from extradata */
  528. if(avctx->extradata_size < 16){
  529. av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
  530. decode_end(avctx);
  531. return AVERROR(EINVAL);
  532. }
  533. ret = decode_header_trees(c);
  534. if (ret < 0) {
  535. decode_end(avctx);
  536. return ret;
  537. }
  538. return 0;
  539. }
  540. static av_cold int smka_decode_init(AVCodecContext *avctx)
  541. {
  542. if (avctx->channels < 1 || avctx->channels > 2) {
  543. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  544. return AVERROR(EINVAL);
  545. }
  546. avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
  547. avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
  548. return 0;
  549. }
  550. /**
  551. * Decode Smacker audio data
  552. */
  553. static int smka_decode_frame(AVCodecContext *avctx, void *data,
  554. int *got_frame_ptr, AVPacket *avpkt)
  555. {
  556. AVFrame *frame = data;
  557. const uint8_t *buf = avpkt->data;
  558. int buf_size = avpkt->size;
  559. GetBitContext gb;
  560. HuffContext h[4] = { { 0 } };
  561. VLC vlc[4] = { { 0 } };
  562. int16_t *samples;
  563. uint8_t *samples8;
  564. int val;
  565. int i, res, ret;
  566. int unp_size;
  567. int bits, stereo;
  568. int pred[2] = {0, 0};
  569. if (buf_size <= 4) {
  570. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  571. return AVERROR(EINVAL);
  572. }
  573. unp_size = AV_RL32(buf);
  574. if (unp_size > (1U<<24)) {
  575. av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
  576. return AVERROR_INVALIDDATA;
  577. }
  578. if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
  579. return ret;
  580. if(!get_bits1(&gb)){
  581. av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
  582. *got_frame_ptr = 0;
  583. return 1;
  584. }
  585. stereo = get_bits1(&gb);
  586. bits = get_bits1(&gb);
  587. if (stereo ^ (avctx->channels != 1)) {
  588. av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
  589. return AVERROR(EINVAL);
  590. }
  591. if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
  592. av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
  593. return AVERROR(EINVAL);
  594. }
  595. /* get output buffer */
  596. frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
  597. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  598. return ret;
  599. samples = (int16_t *)frame->data[0];
  600. samples8 = frame->data[0];
  601. // Initialize
  602. for(i = 0; i < (1 << (bits + stereo)); i++) {
  603. h[i].length = 256;
  604. h[i].maxlength = 0;
  605. h[i].current = 0;
  606. h[i].bits = av_mallocz(256 * 4);
  607. h[i].lengths = av_mallocz(256 * sizeof(int));
  608. h[i].values = av_mallocz(256 * sizeof(int));
  609. if (!h[i].bits || !h[i].lengths || !h[i].values) {
  610. ret = AVERROR(ENOMEM);
  611. goto error;
  612. }
  613. skip_bits1(&gb);
  614. if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
  615. ret = AVERROR_INVALIDDATA;
  616. goto error;
  617. }
  618. skip_bits1(&gb);
  619. if(h[i].current > 1) {
  620. res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
  621. h[i].lengths, sizeof(int), sizeof(int),
  622. h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  623. if(res < 0) {
  624. av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  625. ret = AVERROR_INVALIDDATA;
  626. goto error;
  627. }
  628. }
  629. }
  630. /* this codec relies on wraparound instead of clipping audio */
  631. if(bits) { //decode 16-bit data
  632. for(i = stereo; i >= 0; i--)
  633. pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
  634. for(i = 0; i <= stereo; i++)
  635. *samples++ = pred[i];
  636. for(; i < unp_size / 2; i++) {
  637. if(get_bits_left(&gb)<0)
  638. return AVERROR_INVALIDDATA;
  639. if(i & stereo) {
  640. if(vlc[2].table)
  641. res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
  642. else
  643. res = 0;
  644. if (res < 0) {
  645. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  646. return AVERROR_INVALIDDATA;
  647. }
  648. val = h[2].values[res];
  649. if(vlc[3].table)
  650. res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
  651. else
  652. res = 0;
  653. if (res < 0) {
  654. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  655. return AVERROR_INVALIDDATA;
  656. }
  657. val |= h[3].values[res] << 8;
  658. pred[1] += sign_extend(val, 16);
  659. *samples++ = pred[1];
  660. } else {
  661. if(vlc[0].table)
  662. res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
  663. else
  664. res = 0;
  665. if (res < 0) {
  666. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  667. return AVERROR_INVALIDDATA;
  668. }
  669. val = h[0].values[res];
  670. if(vlc[1].table)
  671. res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
  672. else
  673. res = 0;
  674. if (res < 0) {
  675. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  676. return AVERROR_INVALIDDATA;
  677. }
  678. val |= h[1].values[res] << 8;
  679. pred[0] += sign_extend(val, 16);
  680. *samples++ = pred[0];
  681. }
  682. }
  683. } else { //8-bit data
  684. for(i = stereo; i >= 0; i--)
  685. pred[i] = get_bits(&gb, 8);
  686. for(i = 0; i <= stereo; i++)
  687. *samples8++ = pred[i];
  688. for(; i < unp_size; i++) {
  689. if(get_bits_left(&gb)<0)
  690. return AVERROR_INVALIDDATA;
  691. if(i & stereo){
  692. if(vlc[1].table)
  693. res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
  694. else
  695. res = 0;
  696. if (res < 0) {
  697. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  698. return AVERROR_INVALIDDATA;
  699. }
  700. pred[1] += sign_extend(h[1].values[res], 8);
  701. *samples8++ = pred[1];
  702. } else {
  703. if(vlc[0].table)
  704. res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
  705. else
  706. res = 0;
  707. if (res < 0) {
  708. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  709. return AVERROR_INVALIDDATA;
  710. }
  711. pred[0] += sign_extend(h[0].values[res], 8);
  712. *samples8++ = pred[0];
  713. }
  714. }
  715. }
  716. *got_frame_ptr = 1;
  717. ret = buf_size;
  718. error:
  719. for(i = 0; i < 4; i++) {
  720. if(vlc[i].table)
  721. ff_free_vlc(&vlc[i]);
  722. av_free(h[i].bits);
  723. av_free(h[i].lengths);
  724. av_free(h[i].values);
  725. }
  726. return ret;
  727. }
  728. AVCodec ff_smacker_decoder = {
  729. .name = "smackvid",
  730. .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
  731. .type = AVMEDIA_TYPE_VIDEO,
  732. .id = AV_CODEC_ID_SMACKVIDEO,
  733. .priv_data_size = sizeof(SmackVContext),
  734. .init = decode_init,
  735. .close = decode_end,
  736. .decode = decode_frame,
  737. .capabilities = AV_CODEC_CAP_DR1,
  738. };
  739. AVCodec ff_smackaud_decoder = {
  740. .name = "smackaud",
  741. .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
  742. .type = AVMEDIA_TYPE_AUDIO,
  743. .id = AV_CODEC_ID_SMACKAUDIO,
  744. .init = smka_decode_init,
  745. .decode = smka_decode_frame,
  746. .capabilities = AV_CODEC_CAP_DR1,
  747. };