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.

843 lines
26KB

  1. /*
  2. * Smacker decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Smacker decoder
  24. */
  25. /*
  26. * Based on http://wiki.multimedia.cx/index.php?title=Smacker
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "libavutil/channel_layout.h"
  31. #define BITSTREAM_READER_LE
  32. #include "avcodec.h"
  33. #include "bytestream.h"
  34. #include "get_bits.h"
  35. #include "internal.h"
  36. #include "mathops.h"
  37. #define SMKTREE_BITS 9
  38. #define SMK_NODE 0x80000000
  39. #define SMKTREE_DECODE_MAX_RECURSION 32
  40. #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
  41. typedef struct SmackVContext {
  42. AVCodecContext *avctx;
  43. AVFrame *pic;
  44. int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
  45. int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
  46. } SmackVContext;
  47. /**
  48. * Context used for code reconstructing
  49. */
  50. typedef struct HuffContext {
  51. int length;
  52. int current;
  53. uint32_t *bits;
  54. int *lengths;
  55. int *values;
  56. } HuffContext;
  57. /* common parameters used for decode_bigtree */
  58. typedef struct DBCtx {
  59. VLC *v1, *v2;
  60. int *recode1, *recode2;
  61. int escapes[3];
  62. int *last;
  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 > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
  85. av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\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. return 0;
  103. } else { //Node
  104. int r;
  105. length++;
  106. r = smacker_decode_tree(gb, hc, prefix, length);
  107. if(r)
  108. return r;
  109. return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
  110. }
  111. }
  112. /**
  113. * Decode header tree
  114. */
  115. static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc,
  116. DBCtx *ctx, int length)
  117. {
  118. // Larger length can cause segmentation faults due to too deep recursion.
  119. if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
  120. av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\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.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.current = 0;
  183. tmp2.bits = av_mallocz(256 * 4);
  184. tmp2.lengths = av_mallocz(256 * sizeof(int));
  185. tmp2.values = av_mallocz(256 * sizeof(int));
  186. if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
  187. !tmp2.bits || !tmp2.lengths || !tmp2.values) {
  188. err = AVERROR(ENOMEM);
  189. goto error;
  190. }
  191. if(get_bits1(gb)) {
  192. res = smacker_decode_tree(gb, &tmp1, 0, 0);
  193. if (res < 0) {
  194. err = res;
  195. goto error;
  196. }
  197. skip_bits1(gb);
  198. if(tmp1.current > 1) {
  199. res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
  200. tmp1.lengths, sizeof(int), sizeof(int),
  201. tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  202. if(res < 0) {
  203. av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  204. err = res;
  205. goto error;
  206. }
  207. }
  208. } else {
  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. err = res;
  215. goto error;
  216. }
  217. skip_bits1(gb);
  218. if(tmp2.current > 1) {
  219. res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
  220. tmp2.lengths, sizeof(int), sizeof(int),
  221. tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  222. if(res < 0) {
  223. av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  224. err = res;
  225. goto error;
  226. }
  227. }
  228. } else {
  229. av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
  230. }
  231. escapes[0] = get_bits(gb, 16);
  232. escapes[1] = get_bits(gb, 16);
  233. escapes[2] = get_bits(gb, 16);
  234. last[0] = last[1] = last[2] = -1;
  235. ctx.escapes[0] = escapes[0];
  236. ctx.escapes[1] = escapes[1];
  237. ctx.escapes[2] = escapes[2];
  238. ctx.v1 = &vlc[0];
  239. ctx.v2 = &vlc[1];
  240. ctx.recode1 = tmp1.values;
  241. ctx.recode2 = tmp2.values;
  242. ctx.last = last;
  243. huff.length = ((size + 3) >> 2) + 4;
  244. huff.current = 0;
  245. huff.values = av_mallocz_array(huff.length, sizeof(int));
  246. if (!huff.values) {
  247. err = AVERROR(ENOMEM);
  248. goto error;
  249. }
  250. res = smacker_decode_bigtree(gb, &huff, &ctx, 0);
  251. if (res < 0)
  252. err = res;
  253. skip_bits1(gb);
  254. if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
  255. if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
  256. if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
  257. if (ctx.last[0] >= huff.length ||
  258. ctx.last[1] >= huff.length ||
  259. ctx.last[2] >= huff.length) {
  260. av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
  261. err = AVERROR_INVALIDDATA;
  262. }
  263. *recodes = huff.values;
  264. error:
  265. if(vlc[0].table)
  266. ff_free_vlc(&vlc[0]);
  267. if(vlc[1].table)
  268. ff_free_vlc(&vlc[1]);
  269. av_free(tmp1.bits);
  270. av_free(tmp1.lengths);
  271. av_free(tmp1.values);
  272. av_free(tmp2.bits);
  273. av_free(tmp2.lengths);
  274. av_free(tmp2.values);
  275. return err;
  276. }
  277. static int decode_header_trees(SmackVContext *smk) {
  278. GetBitContext gb;
  279. int mmap_size, mclr_size, full_size, type_size, ret;
  280. int skip = 0;
  281. mmap_size = AV_RL32(smk->avctx->extradata);
  282. mclr_size = AV_RL32(smk->avctx->extradata + 4);
  283. full_size = AV_RL32(smk->avctx->extradata + 8);
  284. type_size = AV_RL32(smk->avctx->extradata + 12);
  285. ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
  286. if (ret < 0)
  287. return ret;
  288. if(!get_bits1(&gb)) {
  289. skip ++;
  290. av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
  291. smk->mmap_tbl = av_malloc(sizeof(int) * 2);
  292. if (!smk->mmap_tbl)
  293. return AVERROR(ENOMEM);
  294. smk->mmap_tbl[0] = 0;
  295. smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
  296. } else {
  297. ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
  298. if (ret < 0)
  299. return ret;
  300. }
  301. if(!get_bits1(&gb)) {
  302. skip ++;
  303. av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
  304. smk->mclr_tbl = av_malloc(sizeof(int) * 2);
  305. if (!smk->mclr_tbl)
  306. return AVERROR(ENOMEM);
  307. smk->mclr_tbl[0] = 0;
  308. smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
  309. } else {
  310. ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
  311. if (ret < 0)
  312. return ret;
  313. }
  314. if(!get_bits1(&gb)) {
  315. skip ++;
  316. av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
  317. smk->full_tbl = av_malloc(sizeof(int) * 2);
  318. if (!smk->full_tbl)
  319. return AVERROR(ENOMEM);
  320. smk->full_tbl[0] = 0;
  321. smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
  322. } else {
  323. ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
  324. if (ret < 0)
  325. return ret;
  326. }
  327. if(!get_bits1(&gb)) {
  328. skip ++;
  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. if (skip == 4)
  341. return AVERROR_INVALIDDATA;
  342. return 0;
  343. }
  344. static av_always_inline void last_reset(int *recode, int *last) {
  345. recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
  346. }
  347. /* get code and update history */
  348. static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
  349. register int *table = recode;
  350. int v;
  351. while(*table & SMK_NODE) {
  352. if (get_bits_left(gb) < 1)
  353. return AVERROR_INVALIDDATA;
  354. if(get_bits1(gb))
  355. table += (*table) & (~SMK_NODE);
  356. table++;
  357. }
  358. v = *table;
  359. if(v != recode[last[0]]) {
  360. recode[last[2]] = recode[last[1]];
  361. recode[last[1]] = recode[last[0]];
  362. recode[last[0]] = v;
  363. }
  364. return v;
  365. }
  366. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  367. AVPacket *avpkt)
  368. {
  369. SmackVContext * const smk = avctx->priv_data;
  370. uint8_t *out;
  371. uint32_t *pal;
  372. GetByteContext gb2;
  373. GetBitContext gb;
  374. int blocks, blk, bw, bh;
  375. int i, ret;
  376. int stride;
  377. int flags;
  378. if (avpkt->size <= 769)
  379. return AVERROR_INVALIDDATA;
  380. if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
  381. return ret;
  382. /* make the palette available on the way out */
  383. pal = (uint32_t*)smk->pic->data[1];
  384. bytestream2_init(&gb2, avpkt->data, avpkt->size);
  385. flags = bytestream2_get_byteu(&gb2);
  386. smk->pic->palette_has_changed = flags & 1;
  387. smk->pic->key_frame = !!(flags & 2);
  388. if (smk->pic->key_frame)
  389. smk->pic->pict_type = AV_PICTURE_TYPE_I;
  390. else
  391. smk->pic->pict_type = AV_PICTURE_TYPE_P;
  392. for(i = 0; i < 256; i++)
  393. *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
  394. last_reset(smk->mmap_tbl, smk->mmap_last);
  395. last_reset(smk->mclr_tbl, smk->mclr_last);
  396. last_reset(smk->full_tbl, smk->full_last);
  397. last_reset(smk->type_tbl, smk->type_last);
  398. if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
  399. return ret;
  400. blk = 0;
  401. bw = avctx->width >> 2;
  402. bh = avctx->height >> 2;
  403. blocks = bw * bh;
  404. stride = smk->pic->linesize[0];
  405. while(blk < blocks) {
  406. int type, run, mode;
  407. uint16_t pix;
  408. type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
  409. if (type < 0)
  410. return type;
  411. run = block_runs[(type >> 2) & 0x3F];
  412. switch(type & 3){
  413. case SMK_BLK_MONO:
  414. while(run-- && blk < blocks){
  415. int clr, map;
  416. int hi, lo;
  417. clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
  418. map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
  419. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  420. hi = clr >> 8;
  421. lo = clr & 0xFF;
  422. for(i = 0; i < 4; i++) {
  423. if(map & 1) out[0] = hi; else out[0] = lo;
  424. if(map & 2) out[1] = hi; else out[1] = lo;
  425. if(map & 4) out[2] = hi; else out[2] = lo;
  426. if(map & 8) out[3] = hi; else out[3] = lo;
  427. map >>= 4;
  428. out += stride;
  429. }
  430. blk++;
  431. }
  432. break;
  433. case SMK_BLK_FULL:
  434. mode = 0;
  435. if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
  436. if(get_bits1(&gb)) mode = 1;
  437. else if(get_bits1(&gb)) mode = 2;
  438. }
  439. while(run-- && blk < blocks){
  440. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  441. switch(mode){
  442. case 0:
  443. for(i = 0; i < 4; i++) {
  444. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  445. AV_WL16(out+2,pix);
  446. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  447. AV_WL16(out,pix);
  448. out += stride;
  449. }
  450. break;
  451. case 1:
  452. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  453. out[0] = out[1] = pix & 0xFF;
  454. out[2] = out[3] = pix >> 8;
  455. out += stride;
  456. out[0] = out[1] = pix & 0xFF;
  457. out[2] = out[3] = pix >> 8;
  458. out += stride;
  459. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  460. out[0] = out[1] = pix & 0xFF;
  461. out[2] = out[3] = pix >> 8;
  462. out += stride;
  463. out[0] = out[1] = pix & 0xFF;
  464. out[2] = out[3] = pix >> 8;
  465. break;
  466. case 2:
  467. for(i = 0; i < 2; i++) {
  468. uint16_t pix1, pix2;
  469. pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  470. pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  471. AV_WL16(out,pix1);
  472. AV_WL16(out+2,pix2);
  473. out += stride;
  474. AV_WL16(out,pix1);
  475. AV_WL16(out+2,pix2);
  476. out += stride;
  477. }
  478. break;
  479. }
  480. blk++;
  481. }
  482. break;
  483. case SMK_BLK_SKIP:
  484. while(run-- && blk < blocks)
  485. blk++;
  486. break;
  487. case SMK_BLK_FILL:
  488. mode = type >> 8;
  489. while(run-- && blk < blocks){
  490. uint32_t col;
  491. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  492. col = mode * 0x01010101U;
  493. for(i = 0; i < 4; i++) {
  494. *((uint32_t*)out) = col;
  495. out += stride;
  496. }
  497. blk++;
  498. }
  499. break;
  500. }
  501. }
  502. if ((ret = av_frame_ref(data, smk->pic)) < 0)
  503. return ret;
  504. *got_frame = 1;
  505. /* always report that the buffer was completely consumed */
  506. return avpkt->size;
  507. }
  508. static av_cold int decode_end(AVCodecContext *avctx)
  509. {
  510. SmackVContext * const smk = avctx->priv_data;
  511. av_freep(&smk->mmap_tbl);
  512. av_freep(&smk->mclr_tbl);
  513. av_freep(&smk->full_tbl);
  514. av_freep(&smk->type_tbl);
  515. av_frame_free(&smk->pic);
  516. return 0;
  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. return AVERROR(EINVAL);
  531. }
  532. ret = decode_header_trees(c);
  533. if (ret < 0) {
  534. return ret;
  535. }
  536. return 0;
  537. }
  538. static av_cold int smka_decode_init(AVCodecContext *avctx)
  539. {
  540. if (avctx->channels < 1 || avctx->channels > 2) {
  541. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  542. return AVERROR_INVALIDDATA;
  543. }
  544. avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
  545. avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
  546. return 0;
  547. }
  548. /**
  549. * Decode Smacker audio data
  550. */
  551. static int smka_decode_frame(AVCodecContext *avctx, void *data,
  552. int *got_frame_ptr, AVPacket *avpkt)
  553. {
  554. AVFrame *frame = data;
  555. const uint8_t *buf = avpkt->data;
  556. int buf_size = avpkt->size;
  557. GetBitContext gb;
  558. HuffContext h[4] = { { 0 } };
  559. VLC vlc[4] = { { 0 } };
  560. int16_t *samples;
  561. uint8_t *samples8;
  562. int val;
  563. int i, res, ret;
  564. int unp_size;
  565. int bits, stereo;
  566. int pred[2] = {0, 0};
  567. if (buf_size <= 4) {
  568. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  569. return AVERROR_INVALIDDATA;
  570. }
  571. unp_size = AV_RL32(buf);
  572. if (unp_size > (1U<<24)) {
  573. av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
  574. return AVERROR_INVALIDDATA;
  575. }
  576. if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
  577. return ret;
  578. if(!get_bits1(&gb)){
  579. av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
  580. *got_frame_ptr = 0;
  581. return 1;
  582. }
  583. stereo = get_bits1(&gb);
  584. bits = get_bits1(&gb);
  585. if (stereo ^ (avctx->channels != 1)) {
  586. av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
  587. return AVERROR_INVALIDDATA;
  588. }
  589. if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
  590. av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
  591. return AVERROR_INVALIDDATA;
  592. }
  593. /* get output buffer */
  594. frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
  595. if (unp_size % (avctx->channels * (bits + 1))) {
  596. av_log(avctx, AV_LOG_ERROR,
  597. "The buffer does not contain an integer number of samples\n");
  598. return AVERROR_INVALIDDATA;
  599. }
  600. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  601. return ret;
  602. samples = (int16_t *)frame->data[0];
  603. samples8 = frame->data[0];
  604. // Initialize
  605. for(i = 0; i < (1 << (bits + stereo)); i++) {
  606. h[i].length = 256;
  607. h[i].current = 0;
  608. h[i].bits = av_mallocz(256 * 4);
  609. h[i].lengths = av_mallocz(256 * sizeof(int));
  610. h[i].values = av_mallocz(256 * sizeof(int));
  611. if (!h[i].bits || !h[i].lengths || !h[i].values) {
  612. ret = AVERROR(ENOMEM);
  613. goto error;
  614. }
  615. skip_bits1(&gb);
  616. if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
  617. ret = AVERROR_INVALIDDATA;
  618. goto error;
  619. }
  620. skip_bits1(&gb);
  621. if(h[i].current > 1) {
  622. res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
  623. h[i].lengths, sizeof(int), sizeof(int),
  624. h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  625. if(res < 0) {
  626. av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  627. ret = AVERROR_INVALIDDATA;
  628. goto error;
  629. }
  630. }
  631. }
  632. /* this codec relies on wraparound instead of clipping audio */
  633. if(bits) { //decode 16-bit data
  634. for(i = stereo; i >= 0; i--)
  635. pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
  636. for(i = 0; i <= stereo; i++)
  637. *samples++ = pred[i];
  638. for(; i < unp_size / 2; i++) {
  639. if (get_bits_left(&gb) < 0) {
  640. ret = AVERROR_INVALIDDATA;
  641. goto error;
  642. }
  643. if(i & stereo) {
  644. if(vlc[2].table)
  645. res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
  646. else
  647. res = 0;
  648. if (res < 0) {
  649. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  650. ret = AVERROR_INVALIDDATA;
  651. goto error;
  652. }
  653. val = h[2].values[res];
  654. if(vlc[3].table)
  655. res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
  656. else
  657. res = 0;
  658. if (res < 0) {
  659. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  660. ret = AVERROR_INVALIDDATA;
  661. goto error;
  662. }
  663. val |= h[3].values[res] << 8;
  664. pred[1] += (unsigned)sign_extend(val, 16);
  665. *samples++ = pred[1];
  666. } else {
  667. if(vlc[0].table)
  668. res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
  669. else
  670. res = 0;
  671. if (res < 0) {
  672. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  673. ret = AVERROR_INVALIDDATA;
  674. goto error;
  675. }
  676. val = h[0].values[res];
  677. if(vlc[1].table)
  678. res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
  679. else
  680. res = 0;
  681. if (res < 0) {
  682. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  683. ret = AVERROR_INVALIDDATA;
  684. goto error;
  685. }
  686. val |= h[1].values[res] << 8;
  687. pred[0] += (unsigned)sign_extend(val, 16);
  688. *samples++ = pred[0];
  689. }
  690. }
  691. } else { //8-bit data
  692. for(i = stereo; i >= 0; i--)
  693. pred[i] = get_bits(&gb, 8);
  694. for(i = 0; i <= stereo; i++)
  695. *samples8++ = pred[i];
  696. for(; i < unp_size; i++) {
  697. if (get_bits_left(&gb) < 0) {
  698. ret = AVERROR_INVALIDDATA;
  699. goto error;
  700. }
  701. if(i & stereo){
  702. if(vlc[1].table)
  703. res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
  704. else
  705. res = 0;
  706. if (res < 0) {
  707. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  708. ret = AVERROR_INVALIDDATA;
  709. goto error;
  710. }
  711. pred[1] += sign_extend(h[1].values[res], 8);
  712. *samples8++ = pred[1];
  713. } else {
  714. if(vlc[0].table)
  715. res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
  716. else
  717. res = 0;
  718. if (res < 0) {
  719. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  720. ret = AVERROR_INVALIDDATA;
  721. goto error;
  722. }
  723. pred[0] += sign_extend(h[0].values[res], 8);
  724. *samples8++ = pred[0];
  725. }
  726. }
  727. }
  728. *got_frame_ptr = 1;
  729. ret = buf_size;
  730. error:
  731. for(i = 0; i < 4; i++) {
  732. if(vlc[i].table)
  733. ff_free_vlc(&vlc[i]);
  734. av_free(h[i].bits);
  735. av_free(h[i].lengths);
  736. av_free(h[i].values);
  737. }
  738. return ret;
  739. }
  740. AVCodec ff_smacker_decoder = {
  741. .name = "smackvid",
  742. .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
  743. .type = AVMEDIA_TYPE_VIDEO,
  744. .id = AV_CODEC_ID_SMACKVIDEO,
  745. .priv_data_size = sizeof(SmackVContext),
  746. .init = decode_init,
  747. .close = decode_end,
  748. .decode = decode_frame,
  749. .capabilities = AV_CODEC_CAP_DR1,
  750. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  751. };
  752. AVCodec ff_smackaud_decoder = {
  753. .name = "smackaud",
  754. .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
  755. .type = AVMEDIA_TYPE_AUDIO,
  756. .id = AV_CODEC_ID_SMACKAUDIO,
  757. .init = smka_decode_init,
  758. .decode = smka_decode_frame,
  759. .capabilities = AV_CODEC_CAP_DR1,
  760. };