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.

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