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.

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