3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
28 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
34 #include "libavutil/channel_layout.h"
39 #define BITSTREAM_READER_LE
41 #include "bytestream.h"
43 #define SMKTREE_BITS 9
44 #define SMK_NODE 0x80000000
49 typedef struct SmackVContext {
50 AVCodecContext *avctx;
53 int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
54 int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
58 * Context used for code reconstructing
60 typedef struct HuffContext {
69 /* common parameters used for decode_bigtree */
70 typedef struct DBCtx {
72 int *recode1, *recode2;
78 /* possible runs of blocks */
79 static const int block_runs[64] = {
80 1, 2, 3, 4, 5, 6, 7, 8,
81 9, 10, 11, 12, 13, 14, 15, 16,
82 17, 18, 19, 20, 21, 22, 23, 24,
83 25, 26, 27, 28, 29, 30, 31, 32,
84 33, 34, 35, 36, 37, 38, 39, 40,
85 41, 42, 43, 44, 45, 46, 47, 48,
86 49, 50, 51, 52, 53, 54, 55, 56,
87 57, 58, 59, 128, 256, 512, 1024, 2048 };
96 * Decode local frame tree
98 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
100 if(length > 32 || length > 3*SMKTREE_BITS) {
101 av_log(NULL, AV_LOG_ERROR, "length too long\n");
102 return AVERROR_INVALIDDATA;
104 if(!get_bits1(gb)){ //Leaf
105 if(hc->current >= 256){
106 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
107 return AVERROR_INVALIDDATA;
110 hc->bits[hc->current] = prefix;
111 hc->lengths[hc->current] = length;
113 hc->bits[hc->current] = 0;
114 hc->lengths[hc->current] = 0;
116 hc->values[hc->current] = get_bits(gb, 8);
118 if(hc->maxlength < length)
119 hc->maxlength = length;
124 r = smacker_decode_tree(gb, hc, prefix, length);
127 return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
134 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
136 if (hc->current + 1 >= hc->length) {
137 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
138 return AVERROR_INVALIDDATA;
140 if(!get_bits1(gb)){ //Leaf
142 i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
143 i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
144 if (i1 < 0 || i2 < 0)
145 return AVERROR_INVALIDDATA;
146 val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
147 if(val == ctx->escapes[0]) {
148 ctx->last[0] = hc->current;
150 } else if(val == ctx->escapes[1]) {
151 ctx->last[1] = hc->current;
153 } else if(val == ctx->escapes[2]) {
154 ctx->last[2] = hc->current;
158 hc->values[hc->current++] = val;
164 r = smacker_decode_bigtree(gb, hc, ctx);
167 hc->values[t] = SMK_NODE | r;
169 r_new = smacker_decode_bigtree(gb, hc, ctx);
177 * Store large tree as FFmpeg's vlc codes
179 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
183 HuffContext tmp1, tmp2;
184 VLC vlc[2] = { { 0 } };
189 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
190 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
191 return AVERROR_INVALIDDATA;
197 tmp1.bits = av_mallocz(256 * 4);
198 tmp1.lengths = av_mallocz(256 * sizeof(int));
199 tmp1.values = av_mallocz(256 * sizeof(int));
204 tmp2.bits = av_mallocz(256 * 4);
205 tmp2.lengths = av_mallocz(256 * sizeof(int));
206 tmp2.values = av_mallocz(256 * sizeof(int));
209 res = smacker_decode_tree(gb, &tmp1, 0, 0);
213 if(tmp1.current > 1) {
214 res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
215 tmp1.lengths, sizeof(int), sizeof(int),
216 tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
218 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
219 return AVERROR_INVALIDDATA;
224 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
227 res = smacker_decode_tree(gb, &tmp2, 0, 0);
231 if(tmp2.current > 1) {
232 res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
233 tmp2.lengths, sizeof(int), sizeof(int),
234 tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
236 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
237 return AVERROR_INVALIDDATA;
242 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
245 escapes[0] = get_bits(gb, 16);
246 escapes[1] = get_bits(gb, 16);
247 escapes[2] = get_bits(gb, 16);
249 last[0] = last[1] = last[2] = -1;
251 ctx.escapes[0] = escapes[0];
252 ctx.escapes[1] = escapes[1];
253 ctx.escapes[2] = escapes[2];
256 ctx.recode1 = tmp1.values;
257 ctx.recode2 = tmp2.values;
260 huff.length = ((size + 3) >> 2) + 3;
263 huff.values = av_mallocz(huff.length * sizeof(int));
265 if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
268 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
269 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
270 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
271 if(huff.current > huff.length){
272 ctx.last[0] = ctx.last[1] = ctx.last[2] = 1;
273 av_log(smk->avctx, AV_LOG_ERROR, "bigtree damaged\n");
274 return AVERROR_INVALIDDATA;
277 *recodes = huff.values;
280 ff_free_vlc(&vlc[0]);
282 ff_free_vlc(&vlc[1]);
284 av_free(tmp1.lengths);
285 av_free(tmp1.values);
287 av_free(tmp2.lengths);
288 av_free(tmp2.values);
293 static int decode_header_trees(SmackVContext *smk) {
295 int mmap_size, mclr_size, full_size, type_size;
297 mmap_size = AV_RL32(smk->avctx->extradata);
298 mclr_size = AV_RL32(smk->avctx->extradata + 4);
299 full_size = AV_RL32(smk->avctx->extradata + 8);
300 type_size = AV_RL32(smk->avctx->extradata + 12);
302 init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
304 if(!get_bits1(&gb)) {
305 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
306 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
307 smk->mmap_tbl[0] = 0;
308 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
310 if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
311 return AVERROR_INVALIDDATA;
313 if(!get_bits1(&gb)) {
314 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
315 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
316 smk->mclr_tbl[0] = 0;
317 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
319 if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
320 return AVERROR_INVALIDDATA;
322 if(!get_bits1(&gb)) {
323 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
324 smk->full_tbl = av_malloc(sizeof(int) * 2);
325 smk->full_tbl[0] = 0;
326 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
328 if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
329 return AVERROR_INVALIDDATA;
331 if(!get_bits1(&gb)) {
332 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
333 smk->type_tbl = av_malloc(sizeof(int) * 2);
334 smk->type_tbl[0] = 0;
335 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
337 if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
338 return AVERROR_INVALIDDATA;
344 static av_always_inline void last_reset(int *recode, int *last) {
345 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
348 /* get code and update history */
349 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
350 register int *table = recode;
353 while(*table & SMK_NODE) {
355 table += (*table) & (~SMK_NODE);
360 if(v != recode[last[0]]) {
361 recode[last[2]] = recode[last[1]];
362 recode[last[1]] = recode[last[0]];
368 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
371 SmackVContext * const smk = avctx->priv_data;
376 int blocks, blk, bw, bh;
381 if (avpkt->size <= 769)
382 return AVERROR_INVALIDDATA;
384 if ((ret = ff_reget_buffer(avctx, &smk->pic)) < 0)
387 /* make the palette available on the way out */
388 pal = (uint32_t*)smk->pic.data[1];
389 bytestream2_init(&gb2, avpkt->data, avpkt->size);
390 flags = bytestream2_get_byteu(&gb2);
391 smk->pic.palette_has_changed = flags & 1;
392 smk->pic.key_frame = !!(flags & 2);
393 if(smk->pic.key_frame)
394 smk->pic.pict_type = AV_PICTURE_TYPE_I;
396 smk->pic.pict_type = AV_PICTURE_TYPE_P;
398 for(i = 0; i < 256; i++)
399 *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
401 last_reset(smk->mmap_tbl, smk->mmap_last);
402 last_reset(smk->mclr_tbl, smk->mclr_last);
403 last_reset(smk->full_tbl, smk->full_last);
404 last_reset(smk->type_tbl, smk->type_last);
405 init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
408 bw = avctx->width >> 2;
409 bh = avctx->height >> 2;
411 out = smk->pic.data[0];
412 stride = smk->pic.linesize[0];
413 while(blk < blocks) {
417 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
418 run = block_runs[(type >> 2) & 0x3F];
421 while(run-- && blk < blocks){
424 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
425 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
426 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
429 for(i = 0; i < 4; i++) {
430 if(map & 1) out[0] = hi; else out[0] = lo;
431 if(map & 2) out[1] = hi; else out[1] = lo;
432 if(map & 4) out[2] = hi; else out[2] = lo;
433 if(map & 8) out[3] = hi; else out[3] = lo;
442 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
443 if(get_bits1(&gb)) mode = 1;
444 else if(get_bits1(&gb)) mode = 2;
446 while(run-- && blk < blocks){
447 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
450 for(i = 0; i < 4; i++) {
451 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
453 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
459 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
460 out[0] = out[1] = pix & 0xFF;
461 out[2] = out[3] = pix >> 8;
463 out[0] = out[1] = pix & 0xFF;
464 out[2] = out[3] = pix >> 8;
466 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
467 out[0] = out[1] = pix & 0xFF;
468 out[2] = out[3] = pix >> 8;
470 out[0] = out[1] = pix & 0xFF;
471 out[2] = out[3] = pix >> 8;
475 for(i = 0; i < 2; i++) {
477 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
478 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
492 while(run-- && blk < blocks)
497 while(run-- && blk < blocks){
499 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
500 col = mode * 0x01010101;
501 for(i = 0; i < 4; i++) {
502 *((uint32_t*)out) = col;
512 if ((ret = av_frame_ref(data, &smk->pic)) < 0)
517 /* always report that the buffer was completely consumed */
525 * Init smacker decoder
528 static av_cold int decode_init(AVCodecContext *avctx)
530 SmackVContext * const c = avctx->priv_data;
534 avctx->pix_fmt = AV_PIX_FMT_PAL8;
535 avcodec_get_frame_defaults(&c->pic);
537 /* decode huffman trees from extradata */
538 if(avctx->extradata_size < 16){
539 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
540 return AVERROR(EINVAL);
543 if (decode_header_trees(c))
544 return AVERROR_INVALIDDATA;
553 * Uninit smacker decoder
556 static av_cold int decode_end(AVCodecContext *avctx)
558 SmackVContext * const smk = avctx->priv_data;
560 av_freep(&smk->mmap_tbl);
561 av_freep(&smk->mclr_tbl);
562 av_freep(&smk->full_tbl);
563 av_freep(&smk->type_tbl);
565 av_frame_unref(&smk->pic);
571 static av_cold int smka_decode_init(AVCodecContext *avctx)
573 if (avctx->channels < 1 || avctx->channels > 2) {
574 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
575 return AVERROR(EINVAL);
577 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
578 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
584 * Decode Smacker audio data
586 static int smka_decode_frame(AVCodecContext *avctx, void *data,
587 int *got_frame_ptr, AVPacket *avpkt)
589 AVFrame *frame = data;
590 const uint8_t *buf = avpkt->data;
591 int buf_size = avpkt->size;
593 HuffContext h[4] = { { 0 } };
594 VLC vlc[4] = { { 0 } };
601 int pred[2] = {0, 0};
604 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
605 return AVERROR(EINVAL);
608 unp_size = AV_RL32(buf);
610 if (unp_size > (1U<<24)) {
611 av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
612 return AVERROR_INVALIDDATA;
615 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
618 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
622 stereo = get_bits1(&gb);
623 bits = get_bits1(&gb);
624 if (stereo ^ (avctx->channels != 1)) {
625 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
626 return AVERROR(EINVAL);
628 if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
629 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
630 return AVERROR(EINVAL);
633 /* get output buffer */
634 frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
635 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
637 samples = (int16_t *)frame->data[0];
638 samples8 = frame->data[0];
641 for(i = 0; i < (1 << (bits + stereo)); i++) {
645 h[i].bits = av_mallocz(256 * 4);
646 h[i].lengths = av_mallocz(256 * sizeof(int));
647 h[i].values = av_mallocz(256 * sizeof(int));
649 res = smacker_decode_tree(&gb, &h[i], 0, 0);
653 if(h[i].current > 1) {
654 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
655 h[i].lengths, sizeof(int), sizeof(int),
656 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
658 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
659 return AVERROR_INVALIDDATA;
663 /* this codec relies on wraparound instead of clipping audio */
664 if(bits) { //decode 16-bit data
665 for(i = stereo; i >= 0; i--)
666 pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
667 for(i = 0; i <= stereo; i++)
668 *samples++ = pred[i];
669 for(; i < unp_size / 2; i++) {
670 if(get_bits_left(&gb)<0)
671 return AVERROR_INVALIDDATA;
674 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
678 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
679 return AVERROR_INVALIDDATA;
681 val = h[2].values[res];
683 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
687 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
688 return AVERROR_INVALIDDATA;
690 val |= h[3].values[res] << 8;
691 pred[1] += sign_extend(val, 16);
692 *samples++ = pred[1];
695 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
699 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
700 return AVERROR_INVALIDDATA;
702 val = h[0].values[res];
704 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
708 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
709 return AVERROR_INVALIDDATA;
711 val |= h[1].values[res] << 8;
712 pred[0] += sign_extend(val, 16);
713 *samples++ = pred[0];
716 } else { //8-bit data
717 for(i = stereo; i >= 0; i--)
718 pred[i] = get_bits(&gb, 8);
719 for(i = 0; i <= stereo; i++)
720 *samples8++ = pred[i];
721 for(; i < unp_size; i++) {
722 if(get_bits_left(&gb)<0)
723 return AVERROR_INVALIDDATA;
726 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
730 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
731 return AVERROR_INVALIDDATA;
733 pred[1] += sign_extend(h[1].values[res], 8);
734 *samples8++ = pred[1];
737 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
741 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
742 return AVERROR_INVALIDDATA;
744 pred[0] += sign_extend(h[0].values[res], 8);
745 *samples8++ = pred[0];
750 for(i = 0; i < 4; i++) {
752 ff_free_vlc(&vlc[i]);
754 av_free(h[i].lengths);
755 av_free(h[i].values);
763 AVCodec ff_smacker_decoder = {
765 .type = AVMEDIA_TYPE_VIDEO,
766 .id = AV_CODEC_ID_SMACKVIDEO,
767 .priv_data_size = sizeof(SmackVContext),
770 .decode = decode_frame,
771 .capabilities = CODEC_CAP_DR1,
772 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
775 AVCodec ff_smackaud_decoder = {
777 .type = AVMEDIA_TYPE_AUDIO,
778 .id = AV_CODEC_ID_SMACKAUDIO,
779 .init = smka_decode_init,
780 .decode = smka_decode_frame,
781 .capabilities = CODEC_CAP_DR1,
782 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),