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.

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