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.

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