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.

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