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.

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