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.

791 lines
24KB

  1. /*
  2. * Smacker decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "bitstream.h"
  34. #include "bytestream.h"
  35. #include "internal.h"
  36. #include "mathops.h"
  37. #include "vlc.h"
  38. #define SMKTREE_BITS 9
  39. #define SMK_NODE 0x80000000
  40. #define SMKTREE_DECODE_MAX_RECURSION 32
  41. #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
  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(BitstreamContext *bc, HuffContext *hc,
  86. uint32_t prefix, int length)
  87. {
  88. if (length > SMKTREE_DECODE_MAX_RECURSION) {
  89. av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
  90. return AVERROR_INVALIDDATA;
  91. }
  92. if (!bitstream_read_bit(bc)) { // Leaf
  93. if(hc->current >= 256){
  94. av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
  95. return AVERROR_INVALIDDATA;
  96. }
  97. if(length){
  98. hc->bits[hc->current] = prefix;
  99. hc->lengths[hc->current] = length;
  100. } else {
  101. hc->bits[hc->current] = 0;
  102. hc->lengths[hc->current] = 0;
  103. }
  104. hc->values[hc->current] = bitstream_read(bc, 8);
  105. hc->current++;
  106. if(hc->maxlength < length)
  107. hc->maxlength = length;
  108. return 0;
  109. } else { //Node
  110. int r;
  111. length++;
  112. r = smacker_decode_tree(bc, hc, prefix, length);
  113. if(r)
  114. return r;
  115. return smacker_decode_tree(bc, hc, prefix | (1 << (length - 1)), length);
  116. }
  117. }
  118. /**
  119. * Decode header tree
  120. */
  121. static int smacker_decode_bigtree(BitstreamContext *bc, HuffContext *hc,
  122. DBCtx *ctx, int length)
  123. {
  124. // Larger length can cause segmentation faults due to too deep recursion.
  125. if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
  126. av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
  127. return AVERROR_INVALIDDATA;
  128. }
  129. if (hc->current + 1 >= hc->length) {
  130. av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
  131. return AVERROR_INVALIDDATA;
  132. }
  133. if (!bitstream_read_bit(bc)) { // Leaf
  134. int val, i1, i2;
  135. i1 = ctx->v1->table ? bitstream_read_vlc(bc, ctx->v1->table, SMKTREE_BITS, 3) : 0;
  136. i2 = ctx->v2->table ? bitstream_read_vlc(bc, ctx->v2->table, SMKTREE_BITS, 3) : 0;
  137. if (i1 < 0 || i2 < 0)
  138. return AVERROR_INVALIDDATA;
  139. val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
  140. if(val == ctx->escapes[0]) {
  141. ctx->last[0] = hc->current;
  142. val = 0;
  143. } else if(val == ctx->escapes[1]) {
  144. ctx->last[1] = hc->current;
  145. val = 0;
  146. } else if(val == ctx->escapes[2]) {
  147. ctx->last[2] = hc->current;
  148. val = 0;
  149. }
  150. hc->values[hc->current++] = val;
  151. return 1;
  152. } else { //Node
  153. int r = 0, r_new, t;
  154. t = hc->current++;
  155. r = smacker_decode_bigtree(bc, hc, ctx, length + 1);
  156. if(r < 0)
  157. return r;
  158. hc->values[t] = SMK_NODE | r;
  159. r++;
  160. r_new = smacker_decode_bigtree(bc, hc, ctx, length + 1);
  161. if (r_new < 0)
  162. return r_new;
  163. return r + r_new;
  164. }
  165. }
  166. /**
  167. * Store large tree as Libav's vlc codes
  168. */
  169. static int smacker_decode_header_tree(SmackVContext *smk, BitstreamContext *bc,
  170. int **recodes, int *last, int size)
  171. {
  172. int res;
  173. HuffContext huff;
  174. HuffContext tmp1, tmp2;
  175. VLC vlc[2] = { { 0 } };
  176. int escapes[3];
  177. DBCtx ctx;
  178. int err = 0;
  179. if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
  180. av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
  181. return AVERROR_INVALIDDATA;
  182. }
  183. tmp1.length = 256;
  184. tmp1.maxlength = 0;
  185. tmp1.current = 0;
  186. tmp1.bits = av_mallocz(256 * 4);
  187. tmp1.lengths = av_mallocz(256 * sizeof(int));
  188. tmp1.values = av_mallocz(256 * sizeof(int));
  189. tmp2.length = 256;
  190. tmp2.maxlength = 0;
  191. tmp2.current = 0;
  192. tmp2.bits = av_mallocz(256 * 4);
  193. tmp2.lengths = av_mallocz(256 * sizeof(int));
  194. tmp2.values = av_mallocz(256 * sizeof(int));
  195. if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
  196. !tmp2.bits || !tmp2.lengths || !tmp2.values) {
  197. err = AVERROR(ENOMEM);
  198. goto error;
  199. }
  200. if (bitstream_read_bit(bc)) {
  201. smacker_decode_tree(bc, &tmp1, 0, 0);
  202. bitstream_skip(bc, 1);
  203. res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
  204. tmp1.lengths, sizeof(int), sizeof(int),
  205. tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  206. if(res < 0) {
  207. av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  208. err = res;
  209. goto error;
  210. }
  211. } else {
  212. av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
  213. }
  214. if (bitstream_read_bit(bc)) {
  215. smacker_decode_tree(bc, &tmp2, 0, 0);
  216. bitstream_skip(bc, 1);
  217. res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
  218. tmp2.lengths, sizeof(int), sizeof(int),
  219. tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  220. if(res < 0) {
  221. av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  222. err = res;
  223. goto error;
  224. }
  225. } else {
  226. av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
  227. }
  228. escapes[0] = bitstream_read(bc, 8);
  229. escapes[0] |= bitstream_read(bc, 8) << 8;
  230. escapes[1] = bitstream_read(bc, 8);
  231. escapes[1] |= bitstream_read(bc, 8) << 8;
  232. escapes[2] = bitstream_read(bc, 8);
  233. escapes[2] |= bitstream_read(bc, 8) << 8;
  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(huff.length * sizeof(int));
  247. if (!huff.values) {
  248. err = AVERROR(ENOMEM);
  249. goto error;
  250. }
  251. if ((res = smacker_decode_bigtree(bc, &huff, &ctx, 0)) < 0)
  252. err = res;
  253. bitstream_skip(bc, 1);
  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. BitstreamContext bc;
  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. bitstream_init8(&bc, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
  285. if (!bitstream_read_bit(&bc)) {
  286. av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
  287. smk->mmap_tbl = av_malloc(sizeof(int) * 2);
  288. if (!smk->mmap_tbl)
  289. return AVERROR(ENOMEM);
  290. smk->mmap_tbl[0] = 0;
  291. smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
  292. } else {
  293. if ((ret = smacker_decode_header_tree(smk, &bc, &smk->mmap_tbl, smk->mmap_last, mmap_size)) < 0)
  294. return ret;
  295. }
  296. if (!bitstream_read_bit(&bc)) {
  297. av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
  298. smk->mclr_tbl = av_malloc(sizeof(int) * 2);
  299. if (!smk->mclr_tbl)
  300. return AVERROR(ENOMEM);
  301. smk->mclr_tbl[0] = 0;
  302. smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
  303. } else {
  304. if ((ret = smacker_decode_header_tree(smk, &bc, &smk->mclr_tbl, smk->mclr_last, mclr_size)) < 0)
  305. return ret;
  306. }
  307. if (!bitstream_read_bit(&bc)) {
  308. av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
  309. smk->full_tbl = av_malloc(sizeof(int) * 2);
  310. if (!smk->full_tbl)
  311. return AVERROR(ENOMEM);
  312. smk->full_tbl[0] = 0;
  313. smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
  314. } else {
  315. if ((ret = smacker_decode_header_tree(smk, &bc, &smk->full_tbl, smk->full_last, full_size)) < 0)
  316. return ret;
  317. }
  318. if (!bitstream_read_bit(&bc)) {
  319. av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
  320. smk->type_tbl = av_malloc(sizeof(int) * 2);
  321. if (!smk->type_tbl)
  322. return AVERROR(ENOMEM);
  323. smk->type_tbl[0] = 0;
  324. smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
  325. } else {
  326. if ((ret = smacker_decode_header_tree(smk, &bc, &smk->type_tbl, smk->type_last, type_size)) < 0)
  327. return ret;
  328. }
  329. return 0;
  330. }
  331. static av_always_inline void last_reset(int *recode, int *last) {
  332. recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
  333. }
  334. /* get code and update history */
  335. static av_always_inline int smk_get_code(BitstreamContext *bc, int *recode,
  336. int *last)
  337. {
  338. register int *table = recode;
  339. int v;
  340. while(*table & SMK_NODE) {
  341. if (bitstream_read_bit(bc))
  342. table += (*table) & (~SMK_NODE);
  343. table++;
  344. }
  345. v = *table;
  346. if(v != recode[last[0]]) {
  347. recode[last[2]] = recode[last[1]];
  348. recode[last[1]] = recode[last[0]];
  349. recode[last[0]] = v;
  350. }
  351. return v;
  352. }
  353. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  354. AVPacket *avpkt)
  355. {
  356. SmackVContext * const smk = avctx->priv_data;
  357. uint8_t *out;
  358. uint32_t *pal;
  359. GetByteContext gb2;
  360. BitstreamContext bc;
  361. int blocks, blk, bw, bh;
  362. int i, ret;
  363. int stride;
  364. int flags;
  365. if (avpkt->size <= 769)
  366. return 0;
  367. if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0) {
  368. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  369. return ret;
  370. }
  371. /* make the palette available on the way out */
  372. pal = (uint32_t*)smk->pic->data[1];
  373. bytestream2_init(&gb2, avpkt->data, avpkt->size);
  374. flags = bytestream2_get_byteu(&gb2);
  375. smk->pic->palette_has_changed = flags & 1;
  376. smk->pic->key_frame = !!(flags & 2);
  377. if(smk->pic->key_frame)
  378. smk->pic->pict_type = AV_PICTURE_TYPE_I;
  379. else
  380. smk->pic->pict_type = AV_PICTURE_TYPE_P;
  381. for(i = 0; i < 256; i++)
  382. *pal++ = bytestream2_get_be24u(&gb2);
  383. last_reset(smk->mmap_tbl, smk->mmap_last);
  384. last_reset(smk->mclr_tbl, smk->mclr_last);
  385. last_reset(smk->full_tbl, smk->full_last);
  386. last_reset(smk->type_tbl, smk->type_last);
  387. bitstream_init8(&bc, avpkt->data + 769, avpkt->size - 769);
  388. blk = 0;
  389. bw = avctx->width >> 2;
  390. bh = avctx->height >> 2;
  391. blocks = bw * bh;
  392. out = smk->pic->data[0];
  393. stride = smk->pic->linesize[0];
  394. while(blk < blocks) {
  395. int type, run, mode;
  396. uint16_t pix;
  397. type = smk_get_code(&bc, smk->type_tbl, smk->type_last);
  398. run = block_runs[(type >> 2) & 0x3F];
  399. switch(type & 3){
  400. case SMK_BLK_MONO:
  401. while(run-- && blk < blocks){
  402. int clr, map;
  403. int hi, lo;
  404. clr = smk_get_code(&bc, smk->mclr_tbl, smk->mclr_last);
  405. map = smk_get_code(&bc, smk->mmap_tbl, smk->mmap_last);
  406. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  407. hi = clr >> 8;
  408. lo = clr & 0xFF;
  409. for(i = 0; i < 4; i++) {
  410. if(map & 1) out[0] = hi; else out[0] = lo;
  411. if(map & 2) out[1] = hi; else out[1] = lo;
  412. if(map & 4) out[2] = hi; else out[2] = lo;
  413. if(map & 8) out[3] = hi; else out[3] = lo;
  414. map >>= 4;
  415. out += stride;
  416. }
  417. blk++;
  418. }
  419. break;
  420. case SMK_BLK_FULL:
  421. mode = 0;
  422. if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
  423. if (bitstream_read_bit(&bc))
  424. mode = 1;
  425. else if (bitstream_read_bit(&bc))
  426. 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(&bc, smk->full_tbl, smk->full_last);
  434. AV_WL16(out+2,pix);
  435. pix = smk_get_code(&bc, 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(&bc, 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(&bc, 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. out += stride;
  455. break;
  456. case 2:
  457. for(i = 0; i < 2; i++) {
  458. uint16_t pix1, pix2;
  459. pix2 = smk_get_code(&bc, smk->full_tbl, smk->full_last);
  460. pix1 = smk_get_code(&bc, smk->full_tbl, smk->full_last);
  461. AV_WL16(out,pix1);
  462. AV_WL16(out+2,pix2);
  463. out += stride;
  464. AV_WL16(out,pix1);
  465. AV_WL16(out+2,pix2);
  466. out += stride;
  467. }
  468. break;
  469. }
  470. blk++;
  471. }
  472. break;
  473. case SMK_BLK_SKIP:
  474. while(run-- && blk < blocks)
  475. blk++;
  476. break;
  477. case SMK_BLK_FILL:
  478. mode = type >> 8;
  479. while(run-- && blk < blocks){
  480. uint32_t col;
  481. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  482. col = mode * 0x01010101;
  483. for(i = 0; i < 4; i++) {
  484. *((uint32_t*)out) = col;
  485. out += stride;
  486. }
  487. blk++;
  488. }
  489. break;
  490. }
  491. }
  492. if ((ret = av_frame_ref(data, smk->pic)) < 0)
  493. return ret;
  494. *got_frame = 1;
  495. /* always report that the buffer was completely consumed */
  496. return avpkt->size;
  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. av_frame_free(&smk->pic);
  506. return 0;
  507. }
  508. static av_cold int decode_init(AVCodecContext *avctx)
  509. {
  510. SmackVContext * const c = avctx->priv_data;
  511. int ret;
  512. c->avctx = avctx;
  513. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  514. c->pic = av_frame_alloc();
  515. if (!c->pic)
  516. return AVERROR(ENOMEM);
  517. /* decode huffman trees from extradata */
  518. if(avctx->extradata_size < 16){
  519. av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
  520. return AVERROR_INVALIDDATA;
  521. }
  522. if ((ret = decode_header_trees(c))) {
  523. decode_end(avctx);
  524. return ret;
  525. }
  526. return 0;
  527. }
  528. static av_cold int smka_decode_init(AVCodecContext *avctx)
  529. {
  530. if (avctx->channels < 1 || avctx->channels > 2) {
  531. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  532. return AVERROR_INVALIDDATA;
  533. }
  534. avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
  535. avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
  536. return 0;
  537. }
  538. /**
  539. * Decode Smacker audio data
  540. */
  541. static int smka_decode_frame(AVCodecContext *avctx, void *data,
  542. int *got_frame_ptr, AVPacket *avpkt)
  543. {
  544. AVFrame *frame = data;
  545. const uint8_t *buf = avpkt->data;
  546. int buf_size = avpkt->size;
  547. BitstreamContext bc;
  548. HuffContext h[4] = { { 0 } };
  549. VLC vlc[4] = { { 0 } };
  550. int16_t *samples;
  551. uint8_t *samples8;
  552. int val;
  553. int i, res, ret;
  554. int unp_size;
  555. int bits, stereo;
  556. int pred[2] = {0, 0};
  557. if (buf_size <= 4) {
  558. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  559. return AVERROR_INVALIDDATA;
  560. }
  561. unp_size = AV_RL32(buf);
  562. bitstream_init8(&bc, buf + 4, buf_size - 4);
  563. if (!bitstream_read_bit(&bc)) {
  564. av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
  565. *got_frame_ptr = 0;
  566. return 1;
  567. }
  568. stereo = bitstream_read_bit(&bc);
  569. bits = bitstream_read_bit(&bc);
  570. if (stereo ^ (avctx->channels != 1)) {
  571. av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
  572. return AVERROR_INVALIDDATA;
  573. }
  574. if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
  575. av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
  576. return AVERROR_INVALIDDATA;
  577. }
  578. if (unp_size % (avctx->channels * (bits + 1))) {
  579. av_log(avctx, AV_LOG_ERROR,
  580. "The buffer does not contain an integer number of samples\n");
  581. return AVERROR_INVALIDDATA;
  582. }
  583. /* get output buffer */
  584. frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
  585. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  586. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  587. return ret;
  588. }
  589. samples = (int16_t *)frame->data[0];
  590. samples8 = frame->data[0];
  591. // Initialize
  592. for(i = 0; i < (1 << (bits + stereo)); i++) {
  593. h[i].length = 256;
  594. h[i].maxlength = 0;
  595. h[i].current = 0;
  596. h[i].bits = av_mallocz(256 * 4);
  597. h[i].lengths = av_mallocz(256 * sizeof(int));
  598. h[i].values = av_mallocz(256 * sizeof(int));
  599. if (!h[i].bits || !h[i].lengths || !h[i].values) {
  600. ret = AVERROR(ENOMEM);
  601. goto error;
  602. }
  603. bitstream_skip(&bc, 1);
  604. if (smacker_decode_tree(&bc, &h[i], 0, 0) < 0) {
  605. ret = AVERROR_INVALIDDATA;
  606. goto error;
  607. }
  608. bitstream_skip(&bc, 1);
  609. if(h[i].current > 1) {
  610. res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
  611. h[i].lengths, sizeof(int), sizeof(int),
  612. h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
  613. if(res < 0) {
  614. av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  615. ret = AVERROR_INVALIDDATA;
  616. goto error;
  617. }
  618. }
  619. }
  620. /* this codec relies on wraparound instead of clipping audio */
  621. if(bits) { //decode 16-bit data
  622. for(i = stereo; i >= 0; i--)
  623. pred[i] = sign_extend(av_bswap16(bitstream_read(&bc, 16)), 16);
  624. for(i = 0; i <= stereo; i++)
  625. *samples++ = pred[i];
  626. for(; i < unp_size / 2; i++) {
  627. if(i & stereo) {
  628. if(vlc[2].table)
  629. res = bitstream_read_vlc(&bc, vlc[2].table, SMKTREE_BITS, 3);
  630. else
  631. res = 0;
  632. val = h[2].values[res];
  633. if(vlc[3].table)
  634. res = bitstream_read_vlc(&bc, vlc[3].table, SMKTREE_BITS, 3);
  635. else
  636. res = 0;
  637. val |= h[3].values[res] << 8;
  638. pred[1] += sign_extend(val, 16);
  639. *samples++ = pred[1];
  640. } else {
  641. if(vlc[0].table)
  642. res = bitstream_read_vlc(&bc, vlc[0].table, SMKTREE_BITS, 3);
  643. else
  644. res = 0;
  645. val = h[0].values[res];
  646. if(vlc[1].table)
  647. res = bitstream_read_vlc(&bc, vlc[1].table, SMKTREE_BITS, 3);
  648. else
  649. res = 0;
  650. val |= h[1].values[res] << 8;
  651. pred[0] += 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] = bitstream_read(&bc, 8);
  658. for(i = 0; i <= stereo; i++)
  659. *samples8++ = pred[i];
  660. for(; i < unp_size; i++) {
  661. if(i & stereo){
  662. if(vlc[1].table)
  663. res = bitstream_read_vlc(&bc, vlc[1].table, SMKTREE_BITS, 3);
  664. else
  665. res = 0;
  666. pred[1] += sign_extend(h[1].values[res], 8);
  667. *samples8++ = pred[1];
  668. } else {
  669. if(vlc[0].table)
  670. res = bitstream_read_vlc(&bc, vlc[0].table, SMKTREE_BITS, 3);
  671. else
  672. res = 0;
  673. pred[0] += sign_extend(h[0].values[res], 8);
  674. *samples8++ = pred[0];
  675. }
  676. }
  677. }
  678. *got_frame_ptr = 1;
  679. ret = buf_size;
  680. error:
  681. for(i = 0; i < 4; i++) {
  682. if(vlc[i].table)
  683. ff_free_vlc(&vlc[i]);
  684. av_free(h[i].bits);
  685. av_free(h[i].lengths);
  686. av_free(h[i].values);
  687. }
  688. return ret;
  689. }
  690. AVCodec ff_smacker_decoder = {
  691. .name = "smackvid",
  692. .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
  693. .type = AVMEDIA_TYPE_VIDEO,
  694. .id = AV_CODEC_ID_SMACKVIDEO,
  695. .priv_data_size = sizeof(SmackVContext),
  696. .init = decode_init,
  697. .close = decode_end,
  698. .decode = decode_frame,
  699. .capabilities = AV_CODEC_CAP_DR1,
  700. };
  701. AVCodec ff_smackaud_decoder = {
  702. .name = "smackaud",
  703. .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
  704. .type = AVMEDIA_TYPE_AUDIO,
  705. .id = AV_CODEC_ID_SMACKAUDIO,
  706. .init = smka_decode_init,
  707. .decode = smka_decode_frame,
  708. .capabilities = AV_CODEC_CAP_DR1,
  709. };