3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of Libav.
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.
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.
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
28 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
34 #include "libavutil/channel_layout.h"
36 #define BITSTREAM_READER_LE
38 #include "bitstream.h"
39 #include "bytestream.h"
44 #define SMKTREE_BITS 9
45 #define SMK_NODE 0x80000000
46 #define SMKTREE_DECODE_MAX_RECURSION 32
47 #define SMKTREE_DECODE_BIG_MAX_RECURSION 500
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(BitstreamContext *bc, HuffContext *hc,
99 uint32_t prefix, int length)
101 if (length > SMKTREE_DECODE_MAX_RECURSION) {
102 av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
103 return AVERROR_INVALIDDATA;
106 if (!bitstream_read_bit(bc)) { // Leaf
107 if(hc->current >= 256){
108 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
109 return AVERROR_INVALIDDATA;
112 hc->bits[hc->current] = prefix;
113 hc->lengths[hc->current] = length;
115 hc->bits[hc->current] = 0;
116 hc->lengths[hc->current] = 0;
118 hc->values[hc->current] = bitstream_read(bc, 8);
120 if(hc->maxlength < length)
121 hc->maxlength = length;
126 r = smacker_decode_tree(bc, hc, prefix, length);
129 return smacker_decode_tree(bc, hc, prefix | (1 << (length - 1)), length);
136 static int smacker_decode_bigtree(BitstreamContext *bc, HuffContext *hc,
137 DBCtx *ctx, int length)
139 // Larger length can cause segmentation faults due to too deep recursion.
140 if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
141 av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
142 return AVERROR_INVALIDDATA;
145 if (hc->current + 1 >= hc->length) {
146 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
147 return AVERROR_INVALIDDATA;
149 if (!bitstream_read_bit(bc)) { // Leaf
151 i1 = ctx->v1->table ? bitstream_read_vlc(bc, ctx->v1->table, SMKTREE_BITS, 3) : 0;
152 i2 = ctx->v2->table ? bitstream_read_vlc(bc, ctx->v2->table, SMKTREE_BITS, 3) : 0;
153 if (i1 < 0 || i2 < 0)
154 return AVERROR_INVALIDDATA;
155 val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
156 if(val == ctx->escapes[0]) {
157 ctx->last[0] = hc->current;
159 } else if(val == ctx->escapes[1]) {
160 ctx->last[1] = hc->current;
162 } else if(val == ctx->escapes[2]) {
163 ctx->last[2] = hc->current;
167 hc->values[hc->current++] = val;
173 r = smacker_decode_bigtree(bc, hc, ctx, length + 1);
176 hc->values[t] = SMK_NODE | r;
178 r_new = smacker_decode_bigtree(bc, hc, ctx, length + 1);
186 * Store large tree as Libav's vlc codes
188 static int smacker_decode_header_tree(SmackVContext *smk, BitstreamContext *bc,
189 int **recodes, int *last, int size)
193 HuffContext tmp1, tmp2;
194 VLC vlc[2] = { { 0 } };
199 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
200 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
201 return AVERROR_INVALIDDATA;
207 tmp1.bits = av_mallocz(256 * 4);
208 tmp1.lengths = av_mallocz(256 * sizeof(int));
209 tmp1.values = av_mallocz(256 * sizeof(int));
214 tmp2.bits = av_mallocz(256 * 4);
215 tmp2.lengths = av_mallocz(256 * sizeof(int));
216 tmp2.values = av_mallocz(256 * sizeof(int));
217 if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
218 !tmp2.bits || !tmp2.lengths || !tmp2.values) {
219 err = AVERROR(ENOMEM);
223 if (bitstream_read_bit(bc)) {
224 smacker_decode_tree(bc, &tmp1, 0, 0);
225 bitstream_skip(bc, 1);
226 res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
227 tmp1.lengths, sizeof(int), sizeof(int),
228 tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
230 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
235 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
237 if (bitstream_read_bit(bc)) {
238 smacker_decode_tree(bc, &tmp2, 0, 0);
239 bitstream_skip(bc, 1);
240 res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
241 tmp2.lengths, sizeof(int), sizeof(int),
242 tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
244 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
249 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
252 escapes[0] = bitstream_read(bc, 8);
253 escapes[0] |= bitstream_read(bc, 8) << 8;
254 escapes[1] = bitstream_read(bc, 8);
255 escapes[1] |= bitstream_read(bc, 8) << 8;
256 escapes[2] = bitstream_read(bc, 8);
257 escapes[2] |= bitstream_read(bc, 8) << 8;
259 last[0] = last[1] = last[2] = -1;
261 ctx.escapes[0] = escapes[0];
262 ctx.escapes[1] = escapes[1];
263 ctx.escapes[2] = escapes[2];
266 ctx.recode1 = tmp1.values;
267 ctx.recode2 = tmp2.values;
270 huff.length = ((size + 3) >> 2) + 4;
273 huff.values = av_mallocz(huff.length * sizeof(int));
275 err = AVERROR(ENOMEM);
279 if ((res = smacker_decode_bigtree(bc, &huff, &ctx, 0)) < 0)
281 bitstream_skip(bc, 1);
282 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
283 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
284 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
285 if (ctx.last[0] >= huff.length ||
286 ctx.last[1] >= huff.length ||
287 ctx.last[2] >= huff.length) {
288 av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
289 err = AVERROR_INVALIDDATA;
292 *recodes = huff.values;
296 ff_free_vlc(&vlc[0]);
298 ff_free_vlc(&vlc[1]);
300 av_free(tmp1.lengths);
301 av_free(tmp1.values);
303 av_free(tmp2.lengths);
304 av_free(tmp2.values);
309 static int decode_header_trees(SmackVContext *smk) {
311 int mmap_size, mclr_size, full_size, type_size, ret;
313 mmap_size = AV_RL32(smk->avctx->extradata);
314 mclr_size = AV_RL32(smk->avctx->extradata + 4);
315 full_size = AV_RL32(smk->avctx->extradata + 8);
316 type_size = AV_RL32(smk->avctx->extradata + 12);
318 bitstream_init8(&bc, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
320 if (!bitstream_read_bit(&bc)) {
321 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
322 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
324 return AVERROR(ENOMEM);
325 smk->mmap_tbl[0] = 0;
326 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
328 if ((ret = smacker_decode_header_tree(smk, &bc, &smk->mmap_tbl, smk->mmap_last, mmap_size)) < 0)
331 if (!bitstream_read_bit(&bc)) {
332 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
333 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
335 return AVERROR(ENOMEM);
336 smk->mclr_tbl[0] = 0;
337 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
339 if ((ret = smacker_decode_header_tree(smk, &bc, &smk->mclr_tbl, smk->mclr_last, mclr_size)) < 0)
342 if (!bitstream_read_bit(&bc)) {
343 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
344 smk->full_tbl = av_malloc(sizeof(int) * 2);
346 return AVERROR(ENOMEM);
347 smk->full_tbl[0] = 0;
348 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
350 if ((ret = smacker_decode_header_tree(smk, &bc, &smk->full_tbl, smk->full_last, full_size)) < 0)
353 if (!bitstream_read_bit(&bc)) {
354 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
355 smk->type_tbl = av_malloc(sizeof(int) * 2);
357 return AVERROR(ENOMEM);
358 smk->type_tbl[0] = 0;
359 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
361 if ((ret = smacker_decode_header_tree(smk, &bc, &smk->type_tbl, smk->type_last, type_size)) < 0)
368 static av_always_inline void last_reset(int *recode, int *last) {
369 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
372 /* get code and update history */
373 static av_always_inline int smk_get_code(BitstreamContext *bc, int *recode,
376 register int *table = recode;
379 while(*table & SMK_NODE) {
380 if (bitstream_read_bit(bc))
381 table += (*table) & (~SMK_NODE);
386 if(v != recode[last[0]]) {
387 recode[last[2]] = recode[last[1]];
388 recode[last[1]] = recode[last[0]];
394 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
397 SmackVContext * const smk = avctx->priv_data;
402 int blocks, blk, bw, bh;
407 if (avpkt->size <= 769)
410 if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0) {
411 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
415 /* make the palette available on the way out */
416 pal = (uint32_t*)smk->pic->data[1];
417 bytestream2_init(&gb2, avpkt->data, avpkt->size);
418 flags = bytestream2_get_byteu(&gb2);
419 smk->pic->palette_has_changed = flags & 1;
420 smk->pic->key_frame = !!(flags & 2);
421 if(smk->pic->key_frame)
422 smk->pic->pict_type = AV_PICTURE_TYPE_I;
424 smk->pic->pict_type = AV_PICTURE_TYPE_P;
426 for(i = 0; i < 256; i++)
427 *pal++ = bytestream2_get_be24u(&gb2);
429 last_reset(smk->mmap_tbl, smk->mmap_last);
430 last_reset(smk->mclr_tbl, smk->mclr_last);
431 last_reset(smk->full_tbl, smk->full_last);
432 last_reset(smk->type_tbl, smk->type_last);
433 bitstream_init8(&bc, avpkt->data + 769, avpkt->size - 769);
436 bw = avctx->width >> 2;
437 bh = avctx->height >> 2;
439 out = smk->pic->data[0];
440 stride = smk->pic->linesize[0];
441 while(blk < blocks) {
445 type = smk_get_code(&bc, smk->type_tbl, smk->type_last);
446 run = block_runs[(type >> 2) & 0x3F];
449 while(run-- && blk < blocks){
452 clr = smk_get_code(&bc, smk->mclr_tbl, smk->mclr_last);
453 map = smk_get_code(&bc, smk->mmap_tbl, smk->mmap_last);
454 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
457 for(i = 0; i < 4; i++) {
458 if(map & 1) out[0] = hi; else out[0] = lo;
459 if(map & 2) out[1] = hi; else out[1] = lo;
460 if(map & 4) out[2] = hi; else out[2] = lo;
461 if(map & 8) out[3] = hi; else out[3] = lo;
470 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
471 if (bitstream_read_bit(&bc))
473 else if (bitstream_read_bit(&bc))
476 while(run-- && blk < blocks){
477 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
480 for(i = 0; i < 4; i++) {
481 pix = smk_get_code(&bc, smk->full_tbl, smk->full_last);
483 pix = smk_get_code(&bc, smk->full_tbl, smk->full_last);
489 pix = smk_get_code(&bc, smk->full_tbl, smk->full_last);
490 out[0] = out[1] = pix & 0xFF;
491 out[2] = out[3] = pix >> 8;
493 out[0] = out[1] = pix & 0xFF;
494 out[2] = out[3] = pix >> 8;
496 pix = smk_get_code(&bc, smk->full_tbl, smk->full_last);
497 out[0] = out[1] = pix & 0xFF;
498 out[2] = out[3] = pix >> 8;
500 out[0] = out[1] = pix & 0xFF;
501 out[2] = out[3] = pix >> 8;
505 for(i = 0; i < 2; i++) {
507 pix2 = smk_get_code(&bc, smk->full_tbl, smk->full_last);
508 pix1 = smk_get_code(&bc, smk->full_tbl, smk->full_last);
522 while(run-- && blk < blocks)
527 while(run-- && blk < blocks){
529 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
530 col = mode * 0x01010101;
531 for(i = 0; i < 4; i++) {
532 *((uint32_t*)out) = col;
542 if ((ret = av_frame_ref(data, smk->pic)) < 0)
547 /* always report that the buffer was completely consumed */
552 static av_cold int decode_end(AVCodecContext *avctx)
554 SmackVContext * const smk = avctx->priv_data;
556 av_freep(&smk->mmap_tbl);
557 av_freep(&smk->mclr_tbl);
558 av_freep(&smk->full_tbl);
559 av_freep(&smk->type_tbl);
561 av_frame_free(&smk->pic);
567 static av_cold int decode_init(AVCodecContext *avctx)
569 SmackVContext * const c = avctx->priv_data;
574 avctx->pix_fmt = AV_PIX_FMT_PAL8;
576 c->pic = av_frame_alloc();
578 return AVERROR(ENOMEM);
580 /* decode huffman trees from extradata */
581 if(avctx->extradata_size < 16){
582 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
583 return AVERROR_INVALIDDATA;
586 if ((ret = decode_header_trees(c))) {
596 static av_cold int smka_decode_init(AVCodecContext *avctx)
598 if (avctx->channels < 1 || avctx->channels > 2) {
599 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
600 return AVERROR_INVALIDDATA;
602 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
603 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
609 * Decode Smacker audio data
611 static int smka_decode_frame(AVCodecContext *avctx, void *data,
612 int *got_frame_ptr, AVPacket *avpkt)
614 AVFrame *frame = data;
615 const uint8_t *buf = avpkt->data;
616 int buf_size = avpkt->size;
618 HuffContext h[4] = { { 0 } };
619 VLC vlc[4] = { { 0 } };
626 int pred[2] = {0, 0};
629 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
630 return AVERROR_INVALIDDATA;
633 unp_size = AV_RL32(buf);
635 bitstream_init8(&bc, buf + 4, buf_size - 4);
637 if (!bitstream_read_bit(&bc)) {
638 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
642 stereo = bitstream_read_bit(&bc);
643 bits = bitstream_read_bit(&bc);
644 if (stereo ^ (avctx->channels != 1)) {
645 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
646 return AVERROR_INVALIDDATA;
648 if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
649 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
650 return AVERROR_INVALIDDATA;
652 if (unp_size % (avctx->channels * (bits + 1))) {
653 av_log(avctx, AV_LOG_ERROR,
654 "The buffer does not contain an integer number of samples\n");
655 return AVERROR_INVALIDDATA;
658 /* get output buffer */
659 frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
660 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
661 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
664 samples = (int16_t *)frame->data[0];
665 samples8 = frame->data[0];
668 for(i = 0; i < (1 << (bits + stereo)); i++) {
672 h[i].bits = av_mallocz(256 * 4);
673 h[i].lengths = av_mallocz(256 * sizeof(int));
674 h[i].values = av_mallocz(256 * sizeof(int));
675 if (!h[i].bits || !h[i].lengths || !h[i].values) {
676 ret = AVERROR(ENOMEM);
679 bitstream_skip(&bc, 1);
680 if (smacker_decode_tree(&bc, &h[i], 0, 0) < 0) {
681 ret = AVERROR_INVALIDDATA;
684 bitstream_skip(&bc, 1);
685 if(h[i].current > 1) {
686 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
687 h[i].lengths, sizeof(int), sizeof(int),
688 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
690 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
691 ret = AVERROR_INVALIDDATA;
696 /* this codec relies on wraparound instead of clipping audio */
697 if(bits) { //decode 16-bit data
698 for(i = stereo; i >= 0; i--)
699 pred[i] = sign_extend(av_bswap16(bitstream_read(&bc, 16)), 16);
700 for(i = 0; i <= stereo; i++)
701 *samples++ = pred[i];
702 for(; i < unp_size / 2; i++) {
705 res = bitstream_read_vlc(&bc, vlc[2].table, SMKTREE_BITS, 3);
708 val = h[2].values[res];
710 res = bitstream_read_vlc(&bc, vlc[3].table, SMKTREE_BITS, 3);
713 val |= h[3].values[res] << 8;
714 pred[1] += sign_extend(val, 16);
715 *samples++ = pred[1];
718 res = bitstream_read_vlc(&bc, vlc[0].table, SMKTREE_BITS, 3);
721 val = h[0].values[res];
723 res = bitstream_read_vlc(&bc, vlc[1].table, SMKTREE_BITS, 3);
726 val |= h[1].values[res] << 8;
727 pred[0] += sign_extend(val, 16);
728 *samples++ = pred[0];
731 } else { //8-bit data
732 for(i = stereo; i >= 0; i--)
733 pred[i] = bitstream_read(&bc, 8);
734 for(i = 0; i <= stereo; i++)
735 *samples8++ = pred[i];
736 for(; i < unp_size; i++) {
739 res = bitstream_read_vlc(&bc, vlc[1].table, SMKTREE_BITS, 3);
742 pred[1] += sign_extend(h[1].values[res], 8);
743 *samples8++ = pred[1];
746 res = bitstream_read_vlc(&bc, vlc[0].table, SMKTREE_BITS, 3);
749 pred[0] += sign_extend(h[0].values[res], 8);
750 *samples8++ = pred[0];
759 for(i = 0; i < 4; i++) {
761 ff_free_vlc(&vlc[i]);
763 av_free(h[i].lengths);
764 av_free(h[i].values);
770 AVCodec ff_smacker_decoder = {
772 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
773 .type = AVMEDIA_TYPE_VIDEO,
774 .id = AV_CODEC_ID_SMACKVIDEO,
775 .priv_data_size = sizeof(SmackVContext),
778 .decode = decode_frame,
779 .capabilities = AV_CODEC_CAP_DR1,
782 AVCodec ff_smackaud_decoder = {
784 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
785 .type = AVMEDIA_TYPE_AUDIO,
786 .id = AV_CODEC_ID_SMACKAUDIO,
787 .init = smka_decode_init,
788 .decode = smka_decode_frame,
789 .capabilities = AV_CODEC_CAP_DR1,