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.

748 lines
23KB

  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. #define SMKTREE_BITS 9
  33. #define SMK_NODE 0x80000000
  34. #define SMKTREE_DECODE_MAX_RECURSION FFMIN(32, 3 * SMKTREE_BITS)
  35. #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
  36. /* The maximum possible unchecked overread happens in decode_header_trees:
  37. * Decoding the MMAP tree can overread by 6 * SMKTREE_BITS + 1, followed by
  38. * three get_bits1, followed by at most 2 + 3 * 16 read bits when reading
  39. * the TYPE tree before the next check. 64 is because of 64 bit reads. */
  40. #if (6 * SMKTREE_BITS + 1 + 3 + (2 + 3 * 16) + 64) <= 8 * AV_INPUT_BUFFER_PADDING_SIZE
  41. #define UNCHECKED_BITSTREAM_READER 1
  42. #endif
  43. #define BITSTREAM_READER_LE
  44. #include "bytestream.h"
  45. #include "get_bits.h"
  46. #include "internal.h"
  47. #include "mathops.h"
  48. typedef struct SmackVContext {
  49. AVCodecContext *avctx;
  50. AVFrame *pic;
  51. int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
  52. int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
  53. } SmackVContext;
  54. /**
  55. * Context used for code reconstructing
  56. */
  57. typedef struct HuffContext {
  58. int current;
  59. uint32_t bits[256];
  60. uint8_t lengths[256];
  61. uint8_t values[256];
  62. } HuffContext;
  63. /* common parameters used for decode_bigtree */
  64. typedef struct DBCtx {
  65. int current, length;
  66. int *values;
  67. VLC *v1, *v2;
  68. uint8_t vals[2];
  69. int escapes[3];
  70. int *last;
  71. } DBCtx;
  72. /* possible runs of blocks */
  73. static const int block_runs[64] = {
  74. 1, 2, 3, 4, 5, 6, 7, 8,
  75. 9, 10, 11, 12, 13, 14, 15, 16,
  76. 17, 18, 19, 20, 21, 22, 23, 24,
  77. 25, 26, 27, 28, 29, 30, 31, 32,
  78. 33, 34, 35, 36, 37, 38, 39, 40,
  79. 41, 42, 43, 44, 45, 46, 47, 48,
  80. 49, 50, 51, 52, 53, 54, 55, 56,
  81. 57, 58, 59, 128, 256, 512, 1024, 2048 };
  82. enum SmkBlockTypes {
  83. SMK_BLK_MONO = 0,
  84. SMK_BLK_FULL = 1,
  85. SMK_BLK_SKIP = 2,
  86. SMK_BLK_FILL = 3 };
  87. /**
  88. * Decode local frame tree
  89. *
  90. * Can read SMKTREE_DECODE_MAX_RECURSION before the first check;
  91. * does not overread gb on success.
  92. */
  93. static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
  94. {
  95. if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
  96. av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
  97. return AVERROR_INVALIDDATA;
  98. }
  99. if(!get_bits1(gb)){ //Leaf
  100. if (hc->current >= 256) {
  101. av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
  102. return AVERROR_INVALIDDATA;
  103. }
  104. if (get_bits_left(gb) < 8)
  105. return AVERROR_INVALIDDATA;
  106. hc->bits[hc->current] = prefix;
  107. hc->lengths[hc->current] = length;
  108. hc->values[hc->current] = get_bits(gb, 8);
  109. hc->current++;
  110. return 0;
  111. } else { //Node
  112. int r;
  113. length++;
  114. r = smacker_decode_tree(gb, hc, prefix, length);
  115. if(r)
  116. return r;
  117. return smacker_decode_tree(gb, hc, prefix | (1U << (length - 1)), length);
  118. }
  119. }
  120. /**
  121. * Decode header tree
  122. *
  123. * Checks before the first read, can overread by 6 * SMKTREE_BITS on success.
  124. */
  125. static int smacker_decode_bigtree(GetBitContext *gb, DBCtx *ctx, int length)
  126. {
  127. // Larger length can cause segmentation faults due to too deep recursion.
  128. if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
  129. av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
  130. return AVERROR_INVALIDDATA;
  131. }
  132. if (ctx->current >= ctx->length) {
  133. av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. if (get_bits_left(gb) <= 0)
  137. return AVERROR_INVALIDDATA;
  138. if(!get_bits1(gb)){ //Leaf
  139. int val, i1, i2;
  140. i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3)
  141. : ctx->vals[0];
  142. i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3)
  143. : ctx->vals[1];
  144. val = i1 | (i2 << 8);
  145. if(val == ctx->escapes[0]) {
  146. ctx->last[0] = ctx->current;
  147. val = 0;
  148. } else if(val == ctx->escapes[1]) {
  149. ctx->last[1] = ctx->current;
  150. val = 0;
  151. } else if(val == ctx->escapes[2]) {
  152. ctx->last[2] = ctx->current;
  153. val = 0;
  154. }
  155. ctx->values[ctx->current++] = val;
  156. return 1;
  157. } else { //Node
  158. int r = 0, r_new, t;
  159. t = ctx->current++;
  160. r = smacker_decode_bigtree(gb, ctx, length + 1);
  161. if(r < 0)
  162. return r;
  163. ctx->values[t] = SMK_NODE | r;
  164. r++;
  165. r_new = smacker_decode_bigtree(gb, ctx, length + 1);
  166. if (r_new < 0)
  167. return r_new;
  168. return r + r_new;
  169. }
  170. }
  171. /**
  172. * Store large tree as FFmpeg's vlc codes
  173. *
  174. * Can read FFMAX(1 + SMKTREE_DECODE_MAX_RECURSION, 2 + 3 * 16) bits
  175. * before the first check; can overread by 6 * SMKTREE_BITS + 1 on success.
  176. */
  177. static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
  178. {
  179. VLC vlc[2] = { { 0 } };
  180. int escapes[3];
  181. DBCtx ctx;
  182. int err;
  183. if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
  184. av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
  185. return AVERROR_INVALIDDATA;
  186. }
  187. for (int i = 0; i < 2; i++) {
  188. HuffContext h;
  189. h.current = 0;
  190. if (!get_bits1(gb)) {
  191. ctx.vals[i] = 0;
  192. av_log(smk->avctx, AV_LOG_ERROR, "Skipping %s bytes tree\n",
  193. i ? "high" : "low");
  194. continue;
  195. }
  196. err = smacker_decode_tree(gb, &h, 0, 0);
  197. if (err < 0)
  198. goto error;
  199. skip_bits1(gb);
  200. if (h.current > 1) {
  201. err = ff_init_vlc_sparse(&vlc[i], SMKTREE_BITS, h.current,
  202. h.lengths, sizeof(*h.lengths), sizeof(*h.lengths),
  203. h.bits, sizeof(*h.bits), sizeof(*h.bits),
  204. h.values, sizeof(*h.values), sizeof(*h.values),
  205. INIT_VLC_LE);
  206. if (err < 0) {
  207. av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  208. goto error;
  209. }
  210. } else
  211. ctx.vals[i] = h.values[0];
  212. }
  213. escapes[0] = get_bits(gb, 16);
  214. escapes[1] = get_bits(gb, 16);
  215. escapes[2] = get_bits(gb, 16);
  216. last[0] = last[1] = last[2] = -1;
  217. ctx.escapes[0] = escapes[0];
  218. ctx.escapes[1] = escapes[1];
  219. ctx.escapes[2] = escapes[2];
  220. ctx.v1 = &vlc[0];
  221. ctx.v2 = &vlc[1];
  222. ctx.last = last;
  223. ctx.length = (size + 3) >> 2;
  224. ctx.current = 0;
  225. ctx.values = av_malloc_array(ctx.length + 3, sizeof(ctx.values[0]));
  226. if (!ctx.values) {
  227. err = AVERROR(ENOMEM);
  228. goto error;
  229. }
  230. *recodes = ctx.values;
  231. err = smacker_decode_bigtree(gb, &ctx, 0);
  232. if (err < 0)
  233. goto error;
  234. skip_bits1(gb);
  235. if (ctx.last[0] == -1) ctx.last[0] = ctx.current++;
  236. if (ctx.last[1] == -1) ctx.last[1] = ctx.current++;
  237. if (ctx.last[2] == -1) ctx.last[2] = ctx.current++;
  238. err = 0;
  239. error:
  240. for (int i = 0; i < 2; i++) {
  241. ff_free_vlc(&vlc[i]);
  242. }
  243. return err;
  244. }
  245. static int decode_header_trees(SmackVContext *smk) {
  246. GetBitContext gb;
  247. int mmap_size, mclr_size, full_size, type_size, ret;
  248. int skip = 0;
  249. mmap_size = AV_RL32(smk->avctx->extradata);
  250. mclr_size = AV_RL32(smk->avctx->extradata + 4);
  251. full_size = AV_RL32(smk->avctx->extradata + 8);
  252. type_size = AV_RL32(smk->avctx->extradata + 12);
  253. ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
  254. if (ret < 0)
  255. return ret;
  256. if(!get_bits1(&gb)) {
  257. skip ++;
  258. av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
  259. smk->mmap_tbl = av_malloc(sizeof(int) * 2);
  260. if (!smk->mmap_tbl)
  261. return AVERROR(ENOMEM);
  262. smk->mmap_tbl[0] = 0;
  263. smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
  264. } else {
  265. ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
  266. if (ret < 0)
  267. return ret;
  268. }
  269. if(!get_bits1(&gb)) {
  270. skip ++;
  271. av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
  272. smk->mclr_tbl = av_malloc(sizeof(int) * 2);
  273. if (!smk->mclr_tbl)
  274. return AVERROR(ENOMEM);
  275. smk->mclr_tbl[0] = 0;
  276. smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
  277. } else {
  278. ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
  279. if (ret < 0)
  280. return ret;
  281. }
  282. if(!get_bits1(&gb)) {
  283. skip ++;
  284. av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
  285. smk->full_tbl = av_malloc(sizeof(int) * 2);
  286. if (!smk->full_tbl)
  287. return AVERROR(ENOMEM);
  288. smk->full_tbl[0] = 0;
  289. smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
  290. } else {
  291. ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
  292. if (ret < 0)
  293. return ret;
  294. }
  295. if(!get_bits1(&gb)) {
  296. skip ++;
  297. av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
  298. smk->type_tbl = av_malloc(sizeof(int) * 2);
  299. if (!smk->type_tbl)
  300. return AVERROR(ENOMEM);
  301. smk->type_tbl[0] = 0;
  302. smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
  303. } else {
  304. ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
  305. if (ret < 0)
  306. return ret;
  307. }
  308. if (skip == 4 || get_bits_left(&gb) < 0)
  309. return AVERROR_INVALIDDATA;
  310. return 0;
  311. }
  312. static av_always_inline void last_reset(int *recode, int *last) {
  313. recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
  314. }
  315. /* Get code and update history.
  316. * Checks before reading, does not overread. */
  317. static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
  318. register int *table = recode;
  319. int v;
  320. while(*table & SMK_NODE) {
  321. if (get_bits_left(gb) < 1)
  322. return AVERROR_INVALIDDATA;
  323. if(get_bits1(gb))
  324. table += (*table) & (~SMK_NODE);
  325. table++;
  326. }
  327. v = *table;
  328. if(v != recode[last[0]]) {
  329. recode[last[2]] = recode[last[1]];
  330. recode[last[1]] = recode[last[0]];
  331. recode[last[0]] = v;
  332. }
  333. return v;
  334. }
  335. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  336. AVPacket *avpkt)
  337. {
  338. SmackVContext * const smk = avctx->priv_data;
  339. uint8_t *out;
  340. uint32_t *pal;
  341. GetByteContext gb2;
  342. GetBitContext gb;
  343. int blocks, blk, bw, bh;
  344. int i, ret;
  345. int stride;
  346. int flags;
  347. if (avpkt->size <= 769)
  348. return AVERROR_INVALIDDATA;
  349. if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
  350. return ret;
  351. /* make the palette available on the way out */
  352. pal = (uint32_t*)smk->pic->data[1];
  353. bytestream2_init(&gb2, avpkt->data, avpkt->size);
  354. flags = bytestream2_get_byteu(&gb2);
  355. smk->pic->palette_has_changed = flags & 1;
  356. smk->pic->key_frame = !!(flags & 2);
  357. if (smk->pic->key_frame)
  358. smk->pic->pict_type = AV_PICTURE_TYPE_I;
  359. else
  360. smk->pic->pict_type = AV_PICTURE_TYPE_P;
  361. for(i = 0; i < 256; i++)
  362. *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
  363. last_reset(smk->mmap_tbl, smk->mmap_last);
  364. last_reset(smk->mclr_tbl, smk->mclr_last);
  365. last_reset(smk->full_tbl, smk->full_last);
  366. last_reset(smk->type_tbl, smk->type_last);
  367. if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
  368. return ret;
  369. blk = 0;
  370. bw = avctx->width >> 2;
  371. bh = avctx->height >> 2;
  372. blocks = bw * bh;
  373. stride = smk->pic->linesize[0];
  374. while(blk < blocks) {
  375. int type, run, mode;
  376. uint16_t pix;
  377. type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
  378. if (type < 0)
  379. return type;
  380. run = block_runs[(type >> 2) & 0x3F];
  381. switch(type & 3){
  382. case SMK_BLK_MONO:
  383. while(run-- && blk < blocks){
  384. int clr, map;
  385. int hi, lo;
  386. clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
  387. map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
  388. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  389. hi = clr >> 8;
  390. lo = clr & 0xFF;
  391. for(i = 0; i < 4; i++) {
  392. if(map & 1) out[0] = hi; else out[0] = lo;
  393. if(map & 2) out[1] = hi; else out[1] = lo;
  394. if(map & 4) out[2] = hi; else out[2] = lo;
  395. if(map & 8) out[3] = hi; else out[3] = lo;
  396. map >>= 4;
  397. out += stride;
  398. }
  399. blk++;
  400. }
  401. break;
  402. case SMK_BLK_FULL:
  403. mode = 0;
  404. if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
  405. if (get_bits_left(&gb) < 1)
  406. return AVERROR_INVALIDDATA;
  407. if(get_bits1(&gb)) mode = 1;
  408. else if(get_bits1(&gb)) mode = 2;
  409. }
  410. while(run-- && blk < blocks){
  411. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  412. switch(mode){
  413. case 0:
  414. for(i = 0; i < 4; i++) {
  415. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  416. AV_WL16(out+2,pix);
  417. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  418. AV_WL16(out,pix);
  419. out += stride;
  420. }
  421. break;
  422. case 1:
  423. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  424. out[0] = out[1] = pix & 0xFF;
  425. out[2] = out[3] = pix >> 8;
  426. out += stride;
  427. out[0] = out[1] = pix & 0xFF;
  428. out[2] = out[3] = pix >> 8;
  429. out += stride;
  430. pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  431. out[0] = out[1] = pix & 0xFF;
  432. out[2] = out[3] = pix >> 8;
  433. out += stride;
  434. out[0] = out[1] = pix & 0xFF;
  435. out[2] = out[3] = pix >> 8;
  436. break;
  437. case 2:
  438. for(i = 0; i < 2; i++) {
  439. uint16_t pix1, pix2;
  440. pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  441. pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
  442. AV_WL16(out,pix1);
  443. AV_WL16(out+2,pix2);
  444. out += stride;
  445. AV_WL16(out,pix1);
  446. AV_WL16(out+2,pix2);
  447. out += stride;
  448. }
  449. break;
  450. }
  451. blk++;
  452. }
  453. break;
  454. case SMK_BLK_SKIP:
  455. while(run-- && blk < blocks)
  456. blk++;
  457. break;
  458. case SMK_BLK_FILL:
  459. mode = type >> 8;
  460. while(run-- && blk < blocks){
  461. uint32_t col;
  462. out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
  463. col = mode * 0x01010101U;
  464. for(i = 0; i < 4; i++) {
  465. *((uint32_t*)out) = col;
  466. out += stride;
  467. }
  468. blk++;
  469. }
  470. break;
  471. }
  472. }
  473. if ((ret = av_frame_ref(data, smk->pic)) < 0)
  474. return ret;
  475. *got_frame = 1;
  476. /* always report that the buffer was completely consumed */
  477. return avpkt->size;
  478. }
  479. static av_cold int decode_end(AVCodecContext *avctx)
  480. {
  481. SmackVContext * const smk = avctx->priv_data;
  482. av_freep(&smk->mmap_tbl);
  483. av_freep(&smk->mclr_tbl);
  484. av_freep(&smk->full_tbl);
  485. av_freep(&smk->type_tbl);
  486. av_frame_free(&smk->pic);
  487. return 0;
  488. }
  489. static av_cold int decode_init(AVCodecContext *avctx)
  490. {
  491. SmackVContext * const c = avctx->priv_data;
  492. int ret;
  493. c->avctx = avctx;
  494. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  495. c->pic = av_frame_alloc();
  496. if (!c->pic)
  497. return AVERROR(ENOMEM);
  498. /* decode huffman trees from extradata */
  499. if (avctx->extradata_size <= 16){
  500. av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
  501. return AVERROR(EINVAL);
  502. }
  503. ret = decode_header_trees(c);
  504. if (ret < 0) {
  505. return ret;
  506. }
  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_INVALIDDATA;
  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. VLC vlc[4] = { { 0 } };
  530. int16_t *samples;
  531. uint8_t *samples8;
  532. uint8_t values[4];
  533. int i, res, ret;
  534. int unp_size;
  535. int bits, stereo;
  536. unsigned pred[2], val;
  537. if (buf_size <= 4) {
  538. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  539. return AVERROR_INVALIDDATA;
  540. }
  541. unp_size = AV_RL32(buf);
  542. if (unp_size > (1U<<24)) {
  543. av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
  544. return AVERROR_INVALIDDATA;
  545. }
  546. if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
  547. return ret;
  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_INVALIDDATA;
  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_INVALIDDATA;
  562. }
  563. /* get output buffer */
  564. frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
  565. if (unp_size % (avctx->channels * (bits + 1))) {
  566. av_log(avctx, AV_LOG_ERROR,
  567. "The buffer does not contain an integer number of samples\n");
  568. return AVERROR_INVALIDDATA;
  569. }
  570. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  571. return ret;
  572. samples = (int16_t *)frame->data[0];
  573. samples8 = frame->data[0];
  574. // Initialize
  575. for(i = 0; i < (1 << (bits + stereo)); i++) {
  576. HuffContext h;
  577. h.current = 0;
  578. skip_bits1(&gb);
  579. if ((ret = smacker_decode_tree(&gb, &h, 0, 0)) < 0)
  580. goto error;
  581. skip_bits1(&gb);
  582. if (h.current > 1) {
  583. ret = ff_init_vlc_sparse(&vlc[i], SMKTREE_BITS, h.current,
  584. h.lengths, sizeof(*h.lengths), sizeof(*h.lengths),
  585. h.bits, sizeof(*h.bits), sizeof(*h.bits),
  586. h.values, sizeof(*h.values), sizeof(*h.values),
  587. INIT_VLC_LE);
  588. if (ret < 0) {
  589. av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
  590. goto error;
  591. }
  592. } else
  593. values[i] = h.values[0];
  594. }
  595. /* this codec relies on wraparound instead of clipping audio */
  596. if(bits) { //decode 16-bit data
  597. for(i = stereo; i >= 0; i--)
  598. pred[i] = av_bswap16(get_bits(&gb, 16));
  599. for(i = 0; i <= stereo; i++)
  600. *samples++ = pred[i];
  601. for(; i < unp_size / 2; i++) {
  602. unsigned idx = 2 * (i & stereo);
  603. if (get_bits_left(&gb) < 0) {
  604. ret = AVERROR_INVALIDDATA;
  605. goto error;
  606. }
  607. if (vlc[idx].table)
  608. res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
  609. else
  610. res = values[idx];
  611. val = res;
  612. if (vlc[++idx].table)
  613. res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
  614. else
  615. res = values[idx];
  616. val |= res << 8;
  617. pred[idx / 2] += val;
  618. *samples++ = pred[idx / 2];
  619. }
  620. } else { //8-bit data
  621. for(i = stereo; i >= 0; i--)
  622. pred[i] = get_bits(&gb, 8);
  623. for(i = 0; i <= stereo; i++)
  624. *samples8++ = pred[i];
  625. for(; i < unp_size; i++) {
  626. unsigned idx = i & stereo;
  627. if (get_bits_left(&gb) < 0) {
  628. ret = AVERROR_INVALIDDATA;
  629. goto error;
  630. }
  631. if (vlc[idx].table)
  632. val = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
  633. else
  634. val = values[idx];
  635. pred[idx] += val;
  636. *samples8++ = pred[idx];
  637. }
  638. }
  639. *got_frame_ptr = 1;
  640. ret = buf_size;
  641. error:
  642. for(i = 0; i < 4; i++) {
  643. ff_free_vlc(&vlc[i]);
  644. }
  645. return ret;
  646. }
  647. AVCodec ff_smacker_decoder = {
  648. .name = "smackvid",
  649. .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
  650. .type = AVMEDIA_TYPE_VIDEO,
  651. .id = AV_CODEC_ID_SMACKVIDEO,
  652. .priv_data_size = sizeof(SmackVContext),
  653. .init = decode_init,
  654. .close = decode_end,
  655. .decode = decode_frame,
  656. .capabilities = AV_CODEC_CAP_DR1,
  657. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE,
  658. };
  659. AVCodec ff_smackaud_decoder = {
  660. .name = "smackaud",
  661. .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
  662. .type = AVMEDIA_TYPE_AUDIO,
  663. .id = AV_CODEC_ID_SMACKAUDIO,
  664. .init = smka_decode_init,
  665. .decode = smka_decode_frame,
  666. .capabilities = AV_CODEC_CAP_DR1,
  667. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  668. };