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.

789 lines
24KB

  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. #include "avcodec.h"
  32. #include "internal.h"
  33. #include "mathops.h"
  34. #define BITSTREAM_READER_LE
  35. #include "get_bits.h"
  36. #include "bytestream.h"
  37. #define SMKTREE_BITS 9
  38. #define SMK_NODE 0x80000000
  39. /*
  40. * Decoder context
  41. */
  42. typedef struct SmackVContext {
  43. AVCodecContext *avctx;
  44. AVFrame pic;
  45. int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
  46. int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
  47. } SmackVContext;
  48. /**
  49. * Context used for code reconstructing
  50. */
  51. typedef struct HuffContext {
  52. int length;
  53. int maxlength;
  54. int current;
  55. uint32_t *bits;
  56. int *lengths;
  57. int *values;
  58. } HuffContext;
  59. /* common parameters used for decode_bigtree */
  60. typedef struct DBCtx {
  61. VLC *v1, *v2;
  62. int *recode1, *recode2;
  63. int escapes[3];
  64. int *last;
  65. int lcur;
  66. } DBCtx;
  67. /* possible runs of blocks */
  68. static const int block_runs[64] = {
  69. 1, 2, 3, 4, 5, 6, 7, 8,
  70. 9, 10, 11, 12, 13, 14, 15, 16,
  71. 17, 18, 19, 20, 21, 22, 23, 24,
  72. 25, 26, 27, 28, 29, 30, 31, 32,
  73. 33, 34, 35, 36, 37, 38, 39, 40,
  74. 41, 42, 43, 44, 45, 46, 47, 48,
  75. 49, 50, 51, 52, 53, 54, 55, 56,
  76. 57, 58, 59, 128, 256, 512, 1024, 2048 };
  77. enum SmkBlockTypes {
  78. SMK_BLK_MONO = 0,
  79. SMK_BLK_FULL = 1,
  80. SMK_BLK_SKIP = 2,
  81. SMK_BLK_FILL = 3 };
  82. /**
  83. * Decode local frame tree
  84. */
  85. static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
  86. {
  87. if(length > 32 || length > 3*SMKTREE_BITS) {
  88. av_log(NULL, AV_LOG_ERROR, "length too long\n");
  89. return AVERROR_INVALIDDATA;
  90. }
  91. if(!get_bits1(gb)){ //Leaf
  92. if(hc->current >= 256){
  93. av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
  94. return AVERROR_INVALIDDATA;
  95. }
  96. if(length){
  97. hc->bits[hc->current] = prefix;
  98. hc->lengths[hc->current] = length;
  99. } else {
  100. hc->bits[hc->current] = 0;
  101. hc->lengths[hc->current] = 0;
  102. }
  103. hc->values[hc->current] = get_bits(gb, 8);
  104. hc->current++;
  105. if(hc->maxlength < length)
  106. hc->maxlength = length;
  107. return 0;
  108. } else { //Node
  109. int r;
  110. length++;
  111. r = smacker_decode_tree(gb, hc, prefix, length);
  112. if(r)
  113. return r;
  114. return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
  115. }
  116. }
  117. /**
  118. * Decode header tree
  119. */
  120. static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
  121. {
  122. if (hc->current + 1 >= hc->length) {
  123. av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
  124. return AVERROR_INVALIDDATA;
  125. }
  126. if(!get_bits1(gb)){ //Leaf
  127. int val, i1, i2;
  128. i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
  129. i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
  130. if (i1 < 0 || i2 < 0)
  131. return AVERROR_INVALIDDATA;
  132. val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
  133. if(val == ctx->escapes[0]) {
  134. ctx->last[0] = hc->current;
  135. val = 0;
  136. } else if(val == ctx->escapes[1]) {
  137. ctx->last[1] = hc->current;
  138. val = 0;
  139. } else if(val == ctx->escapes[2]) {
  140. ctx->last[2] = hc->current;
  141. val = 0;
  142. }
  143. hc->values[hc->current++] = val;
  144. return 1;
  145. } else { //Node
  146. int r = 0, r_new, t;
  147. t = hc->current++;
  148. r = smacker_decode_bigtree(gb, hc, ctx);
  149. if(r < 0)
  150. return r;
  151. hc->values[t] = SMK_NODE | r;
  152. r++;
  153. r_new = smacker_decode_bigtree(gb, hc, ctx);
  154. if (r_new < 0)
  155. return r_new;
  156. return r + r_new;
  157. }
  158. }
  159. /**
  160. * Store large tree as FFmpeg's vlc codes
  161. */
  162. static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
  163. {
  164. int res;
  165. HuffContext huff;
  166. HuffContext tmp1, tmp2;
  167. VLC vlc[2] = { { 0 } };
  168. int escapes[3];
  169. DBCtx ctx;
  170. int err = 0;
  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. tmp1.length = 256;
  176. tmp1.maxlength = 0;
  177. tmp1.current = 0;
  178. tmp1.bits = av_mallocz(256 * 4);
  179. tmp1.lengths = av_mallocz(256 * sizeof(int));
  180. tmp1.values = av_mallocz(256 * sizeof(int));
  181. tmp2.length = 256;
  182. tmp2.maxlength = 0;
  183. tmp2.current = 0;
  184. tmp2.bits = av_mallocz(256 * 4);
  185. tmp2.lengths = av_mallocz(256 * sizeof(int));
  186. tmp2.values = av_mallocz(256 * sizeof(int));
  187. if(get_bits1(gb)) {
  188. res = smacker_decode_tree(gb, &tmp1, 0, 0);
  189. if (res < 0)
  190. return res;
  191. skip_bits1(gb);
  192. if(tmp1.current > 1) {
  193. res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
  194. tmp1.lengths, sizeof(int), sizeof(int),
  195. tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  196. if(res < 0) {
  197. av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  198. return AVERROR_INVALIDDATA;
  199. }
  200. }
  201. }
  202. if (!vlc[0].table) {
  203. av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
  204. }
  205. if(get_bits1(gb)){
  206. res = smacker_decode_tree(gb, &tmp2, 0, 0);
  207. if (res < 0)
  208. return res;
  209. skip_bits1(gb);
  210. if(tmp2.current > 1) {
  211. res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
  212. tmp2.lengths, sizeof(int), sizeof(int),
  213. tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  214. if(res < 0) {
  215. av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  216. return AVERROR_INVALIDDATA;
  217. }
  218. }
  219. }
  220. if (!vlc[1].table) {
  221. av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
  222. }
  223. escapes[0] = get_bits(gb, 16);
  224. escapes[1] = get_bits(gb, 16);
  225. escapes[2] = get_bits(gb, 16);
  226. last[0] = last[1] = last[2] = -1;
  227. ctx.escapes[0] = escapes[0];
  228. ctx.escapes[1] = escapes[1];
  229. ctx.escapes[2] = escapes[2];
  230. ctx.v1 = &vlc[0];
  231. ctx.v2 = &vlc[1];
  232. ctx.recode1 = tmp1.values;
  233. ctx.recode2 = tmp2.values;
  234. ctx.last = last;
  235. huff.length = ((size + 3) >> 2) + 3;
  236. huff.maxlength = 0;
  237. huff.current = 0;
  238. huff.values = av_mallocz(huff.length * sizeof(int));
  239. if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
  240. err = -1;
  241. skip_bits1(gb);
  242. if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
  243. if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
  244. if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
  245. if(huff.current > huff.length){
  246. ctx.last[0] = ctx.last[1] = ctx.last[2] = 1;
  247. av_log(smk->avctx, AV_LOG_ERROR, "bigtree damaged\n");
  248. return AVERROR_INVALIDDATA;
  249. }
  250. *recodes = huff.values;
  251. if(vlc[0].table)
  252. ff_free_vlc(&vlc[0]);
  253. if(vlc[1].table)
  254. ff_free_vlc(&vlc[1]);
  255. av_free(tmp1.bits);
  256. av_free(tmp1.lengths);
  257. av_free(tmp1.values);
  258. av_free(tmp2.bits);
  259. av_free(tmp2.lengths);
  260. av_free(tmp2.values);
  261. return err;
  262. }
  263. static int decode_header_trees(SmackVContext *smk) {
  264. GetBitContext gb;
  265. int mmap_size, mclr_size, full_size, type_size;
  266. mmap_size = AV_RL32(smk->avctx->extradata);
  267. mclr_size = AV_RL32(smk->avctx->extradata + 4);
  268. full_size = AV_RL32(smk->avctx->extradata + 8);
  269. type_size = AV_RL32(smk->avctx->extradata + 12);
  270. init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
  271. if(!get_bits1(&gb)) {
  272. av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
  273. smk->mmap_tbl = av_malloc(sizeof(int) * 2);
  274. smk->mmap_tbl[0] = 0;
  275. smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
  276. } else {
  277. if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
  278. return AVERROR_INVALIDDATA;
  279. }
  280. if(!get_bits1(&gb)) {
  281. av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
  282. smk->mclr_tbl = av_malloc(sizeof(int) * 2);
  283. smk->mclr_tbl[0] = 0;
  284. smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
  285. } else {
  286. if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
  287. return AVERROR_INVALIDDATA;
  288. }
  289. if(!get_bits1(&gb)) {
  290. av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
  291. smk->full_tbl = av_malloc(sizeof(int) * 2);
  292. smk->full_tbl[0] = 0;
  293. smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
  294. } else {
  295. if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
  296. return AVERROR_INVALIDDATA;
  297. }
  298. if(!get_bits1(&gb)) {
  299. av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
  300. smk->type_tbl = av_malloc(sizeof(int) * 2);
  301. smk->type_tbl[0] = 0;
  302. smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
  303. } else {
  304. if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
  305. return AVERROR_INVALIDDATA;
  306. }
  307. return 0;
  308. }
  309. static av_always_inline void last_reset(int *recode, int *last) {
  310. recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
  311. }
  312. /* get code and update history */
  313. static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
  314. register int *table = recode;
  315. int v;
  316. while(*table & SMK_NODE) {
  317. if(get_bits1(gb))
  318. table += (*table) & (~SMK_NODE);
  319. table++;
  320. }
  321. v = *table;
  322. if(v != recode[last[0]]) {
  323. recode[last[2]] = recode[last[1]];
  324. recode[last[1]] = recode[last[0]];
  325. recode[last[0]] = v;
  326. }
  327. return v;
  328. }
  329. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  330. AVPacket *avpkt)
  331. {
  332. SmackVContext * const smk = avctx->priv_data;
  333. uint8_t *out;
  334. uint32_t *pal;
  335. GetByteContext gb2;
  336. GetBitContext gb;
  337. int blocks, blk, bw, bh;
  338. int i, ret;
  339. int stride;
  340. int flags;
  341. if (avpkt->size <= 769)
  342. return AVERROR_INVALIDDATA;
  343. smk->pic.reference = 3;
  344. smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  345. if((ret = avctx->reget_buffer(avctx, &smk->pic)) < 0){
  346. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  347. return ret;
  348. }
  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. init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
  366. blk = 0;
  367. bw = avctx->width >> 2;
  368. bh = avctx->height >> 2;
  369. blocks = bw * bh;
  370. out = smk->pic.data[0];
  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. run = block_runs[(type >> 2) & 0x3F];
  377. switch(type & 3){
  378. case SMK_BLK_MONO:
  379. while(run-- && blk < blocks){
  380. int clr, map;
  381. int hi, lo;
  382. clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
  383. map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
  384. out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  385. hi = clr >> 8;
  386. lo = clr & 0xFF;
  387. for(i = 0; i < 4; i++) {
  388. if(map & 1) out[0] = hi; else out[0] = lo;
  389. if(map & 2) out[1] = hi; else out[1] = lo;
  390. if(map & 4) out[2] = hi; else out[2] = lo;
  391. if(map & 8) out[3] = hi; else out[3] = lo;
  392. map >>= 4;
  393. out += stride;
  394. }
  395. blk++;
  396. }
  397. break;
  398. case SMK_BLK_FULL:
  399. mode = 0;
  400. if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
  401. if(get_bits1(&gb)) mode = 1;
  402. else if(get_bits1(&gb)) mode = 2;
  403. }
  404. while(run-- && blk < blocks){
  405. out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  406. switch(mode){
  407. case 0:
  408. for(i = 0; i < 4; i++) {
  409. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  410. AV_WL16(out+2,pix);
  411. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  412. AV_WL16(out,pix);
  413. out += stride;
  414. }
  415. break;
  416. case 1:
  417. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  418. out[0] = out[1] = pix & 0xFF;
  419. out[2] = out[3] = pix >> 8;
  420. out += stride;
  421. out[0] = out[1] = pix & 0xFF;
  422. out[2] = out[3] = pix >> 8;
  423. out += stride;
  424. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  425. out[0] = out[1] = pix & 0xFF;
  426. out[2] = out[3] = pix >> 8;
  427. out += stride;
  428. out[0] = out[1] = pix & 0xFF;
  429. out[2] = out[3] = pix >> 8;
  430. out += stride;
  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 * 0x01010101;
  459. for(i = 0; i < 4; i++) {
  460. *((uint32_t*)out) = col;
  461. out += stride;
  462. }
  463. blk++;
  464. }
  465. break;
  466. }
  467. }
  468. *got_frame = 1;
  469. *(AVFrame*)data = smk->pic;
  470. /* always report that the buffer was completely consumed */
  471. return avpkt->size;
  472. }
  473. /*
  474. *
  475. * Init smacker decoder
  476. *
  477. */
  478. static av_cold int decode_init(AVCodecContext *avctx)
  479. {
  480. SmackVContext * const c = avctx->priv_data;
  481. c->avctx = avctx;
  482. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  483. avcodec_get_frame_defaults(&c->pic);
  484. /* decode huffman trees from extradata */
  485. if(avctx->extradata_size < 16){
  486. av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
  487. return AVERROR(EINVAL);
  488. }
  489. if (decode_header_trees(c))
  490. return AVERROR_INVALIDDATA;
  491. return 0;
  492. }
  493. /*
  494. *
  495. * Uninit smacker decoder
  496. *
  497. */
  498. static av_cold int decode_end(AVCodecContext *avctx)
  499. {
  500. SmackVContext * const smk = avctx->priv_data;
  501. av_freep(&smk->mmap_tbl);
  502. av_freep(&smk->mclr_tbl);
  503. av_freep(&smk->full_tbl);
  504. av_freep(&smk->type_tbl);
  505. if (smk->pic.data[0])
  506. avctx->release_buffer(avctx, &smk->pic);
  507. return 0;
  508. }
  509. static av_cold int smka_decode_init(AVCodecContext *avctx)
  510. {
  511. if (avctx->channels < 1 || avctx->channels > 2) {
  512. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  513. return AVERROR(EINVAL);
  514. }
  515. avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
  516. avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
  517. return 0;
  518. }
  519. /**
  520. * Decode Smacker audio data
  521. */
  522. static int smka_decode_frame(AVCodecContext *avctx, void *data,
  523. int *got_frame_ptr, AVPacket *avpkt)
  524. {
  525. AVFrame *frame = data;
  526. const uint8_t *buf = avpkt->data;
  527. int buf_size = avpkt->size;
  528. GetBitContext gb;
  529. HuffContext h[4] = { { 0 } };
  530. VLC vlc[4] = { { 0 } };
  531. int16_t *samples;
  532. uint8_t *samples8;
  533. int val;
  534. int i, res, ret;
  535. int unp_size;
  536. int bits, stereo;
  537. int pred[2] = {0, 0};
  538. if (buf_size <= 4) {
  539. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  540. return AVERROR(EINVAL);
  541. }
  542. unp_size = AV_RL32(buf);
  543. if (unp_size > (1U<<24)) {
  544. av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
  545. return AVERROR_INVALIDDATA;
  546. }
  547. init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
  548. if(!get_bits1(&gb)){
  549. av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
  550. *got_frame_ptr = 0;
  551. return 1;
  552. }
  553. stereo = get_bits1(&gb);
  554. bits = get_bits1(&gb);
  555. if (stereo ^ (avctx->channels != 1)) {
  556. av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
  557. return AVERROR(EINVAL);
  558. }
  559. if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
  560. av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
  561. return AVERROR(EINVAL);
  562. }
  563. /* get output buffer */
  564. frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
  565. if ((ret = ff_get_buffer(avctx, frame)) < 0) {
  566. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  567. return ret;
  568. }
  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].maxlength = 0;
  575. h[i].current = 0;
  576. h[i].bits = av_mallocz(256 * 4);
  577. h[i].lengths = av_mallocz(256 * sizeof(int));
  578. h[i].values = av_mallocz(256 * sizeof(int));
  579. skip_bits1(&gb);
  580. res = smacker_decode_tree(&gb, &h[i], 0, 0);
  581. if (res < 0)
  582. return res;
  583. skip_bits1(&gb);
  584. if(h[i].current > 1) {
  585. res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
  586. h[i].lengths, sizeof(int), sizeof(int),
  587. h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  588. if(res < 0) {
  589. av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  590. return AVERROR_INVALIDDATA;
  591. }
  592. }
  593. }
  594. if(bits) { //decode 16-bit data
  595. for(i = stereo; i >= 0; i--)
  596. pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
  597. for(i = 0; i <= stereo; i++)
  598. *samples++ = pred[i];
  599. for(; i < unp_size / 2; i++) {
  600. if(get_bits_left(&gb)<0)
  601. return AVERROR_INVALIDDATA;
  602. if(i & stereo) {
  603. if(vlc[2].table)
  604. res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
  605. else
  606. res = 0;
  607. if (res < 0) {
  608. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  609. return AVERROR_INVALIDDATA;
  610. }
  611. val = h[2].values[res];
  612. if(vlc[3].table)
  613. res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
  614. else
  615. res = 0;
  616. if (res < 0) {
  617. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  618. return AVERROR_INVALIDDATA;
  619. }
  620. val |= h[3].values[res] << 8;
  621. pred[1] += sign_extend(val, 16);
  622. *samples++ = av_clip_int16(pred[1]);
  623. } else {
  624. if(vlc[0].table)
  625. res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
  626. else
  627. res = 0;
  628. if (res < 0) {
  629. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  630. return AVERROR_INVALIDDATA;
  631. }
  632. val = h[0].values[res];
  633. if(vlc[1].table)
  634. res = get_vlc2(&gb, vlc[1].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[1].values[res] << 8;
  642. pred[0] += sign_extend(val, 16);
  643. *samples++ = av_clip_int16(pred[0]);
  644. }
  645. }
  646. } else { //8-bit data
  647. for(i = stereo; i >= 0; i--)
  648. pred[i] = get_bits(&gb, 8);
  649. for(i = 0; i <= stereo; i++)
  650. *samples8++ = pred[i];
  651. for(; i < unp_size; i++) {
  652. if(get_bits_left(&gb)<0)
  653. return AVERROR_INVALIDDATA;
  654. if(i & stereo){
  655. if(vlc[1].table)
  656. res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
  657. else
  658. res = 0;
  659. if (res < 0) {
  660. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  661. return AVERROR_INVALIDDATA;
  662. }
  663. pred[1] += sign_extend(h[1].values[res], 8);
  664. *samples8++ = av_clip_uint8(pred[1]);
  665. } else {
  666. if(vlc[0].table)
  667. res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
  668. else
  669. res = 0;
  670. if (res < 0) {
  671. av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
  672. return AVERROR_INVALIDDATA;
  673. }
  674. pred[0] += sign_extend(h[0].values[res], 8);
  675. *samples8++ = av_clip_uint8(pred[0]);
  676. }
  677. }
  678. }
  679. for(i = 0; i < 4; i++) {
  680. if(vlc[i].table)
  681. ff_free_vlc(&vlc[i]);
  682. av_free(h[i].bits);
  683. av_free(h[i].lengths);
  684. av_free(h[i].values);
  685. }
  686. *got_frame_ptr = 1;
  687. return buf_size;
  688. }
  689. AVCodec ff_smacker_decoder = {
  690. .name = "smackvid",
  691. .type = AVMEDIA_TYPE_VIDEO,
  692. .id = AV_CODEC_ID_SMACKVIDEO,
  693. .priv_data_size = sizeof(SmackVContext),
  694. .init = decode_init,
  695. .close = decode_end,
  696. .decode = decode_frame,
  697. .capabilities = CODEC_CAP_DR1,
  698. .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
  699. };
  700. AVCodec ff_smackaud_decoder = {
  701. .name = "smackaud",
  702. .type = AVMEDIA_TYPE_AUDIO,
  703. .id = AV_CODEC_ID_SMACKAUDIO,
  704. .init = smka_decode_init,
  705. .decode = smka_decode_frame,
  706. .capabilities = CODEC_CAP_DR1,
  707. .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
  708. };