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.

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