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.

784 lines
24KB

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