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.

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