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"
36 #define BITSTREAM_READER_LE
38 #include "bytestream.h"
43 #define SMKTREE_BITS 9
44 #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(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
100 if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
101 av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
102 return AVERROR_INVALIDDATA;
105 if(!get_bits1(gb)){ //Leaf
106 if(hc->current >= hc->length){
107 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
108 return AVERROR_INVALIDDATA;
111 hc->bits[hc->current] = prefix;
112 hc->lengths[hc->current] = length;
114 hc->bits[hc->current] = 0;
115 hc->lengths[hc->current] = 0;
117 hc->values[hc->current] = get_bits(gb, 8);
119 if(hc->maxlength < length)
120 hc->maxlength = length;
125 r = smacker_decode_tree(gb, hc, prefix, length);
128 return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
135 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc,
136 DBCtx *ctx, int length)
138 // Larger length can cause segmentation faults due to too deep recursion.
139 if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
140 av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
141 return AVERROR_INVALIDDATA;
144 if (hc->current + 1 >= hc->length) {
145 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
146 return AVERROR_INVALIDDATA;
148 if(!get_bits1(gb)){ //Leaf
150 i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
151 i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
152 if (i1 < 0 || i2 < 0)
153 return AVERROR_INVALIDDATA;
154 val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
155 if(val == ctx->escapes[0]) {
156 ctx->last[0] = hc->current;
158 } else if(val == ctx->escapes[1]) {
159 ctx->last[1] = hc->current;
161 } else if(val == ctx->escapes[2]) {
162 ctx->last[2] = hc->current;
166 hc->values[hc->current++] = val;
172 r = smacker_decode_bigtree(gb, hc, ctx, length + 1);
175 hc->values[t] = SMK_NODE | r;
177 r_new = smacker_decode_bigtree(gb, hc, ctx, length + 1);
185 * Store large tree as FFmpeg's vlc codes
187 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
191 HuffContext tmp1, tmp2;
192 VLC vlc[2] = { { 0 } };
197 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
198 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
199 return AVERROR_INVALIDDATA;
205 tmp1.bits = av_mallocz(256 * 4);
206 tmp1.lengths = av_mallocz(256 * sizeof(int));
207 tmp1.values = av_mallocz(256 * sizeof(int));
212 tmp2.bits = av_mallocz(256 * 4);
213 tmp2.lengths = av_mallocz(256 * sizeof(int));
214 tmp2.values = av_mallocz(256 * sizeof(int));
215 if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
216 !tmp2.bits || !tmp2.lengths || !tmp2.values) {
217 err = AVERROR(ENOMEM);
222 res = smacker_decode_tree(gb, &tmp1, 0, 0);
228 if(tmp1.current > 1) {
229 res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
230 tmp1.lengths, sizeof(int), sizeof(int),
231 tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
233 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
240 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
243 res = smacker_decode_tree(gb, &tmp2, 0, 0);
249 if(tmp2.current > 1) {
250 res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
251 tmp2.lengths, sizeof(int), sizeof(int),
252 tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
254 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
261 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
264 escapes[0] = get_bits(gb, 16);
265 escapes[1] = get_bits(gb, 16);
266 escapes[2] = get_bits(gb, 16);
268 last[0] = last[1] = last[2] = -1;
270 ctx.escapes[0] = escapes[0];
271 ctx.escapes[1] = escapes[1];
272 ctx.escapes[2] = escapes[2];
275 ctx.recode1 = tmp1.values;
276 ctx.recode2 = tmp2.values;
279 huff.length = ((size + 3) >> 2) + 4;
282 huff.values = av_mallocz_array(huff.length, sizeof(int));
284 err = AVERROR(ENOMEM);
288 res = smacker_decode_bigtree(gb, &huff, &ctx, 0);
292 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
293 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
294 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
295 if (ctx.last[0] >= huff.length ||
296 ctx.last[1] >= huff.length ||
297 ctx.last[2] >= huff.length) {
298 av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
299 err = AVERROR_INVALIDDATA;
302 *recodes = huff.values;
306 ff_free_vlc(&vlc[0]);
308 ff_free_vlc(&vlc[1]);
310 av_free(tmp1.lengths);
311 av_free(tmp1.values);
313 av_free(tmp2.lengths);
314 av_free(tmp2.values);
319 static int decode_header_trees(SmackVContext *smk) {
321 int mmap_size, mclr_size, full_size, type_size, ret;
323 mmap_size = AV_RL32(smk->avctx->extradata);
324 mclr_size = AV_RL32(smk->avctx->extradata + 4);
325 full_size = AV_RL32(smk->avctx->extradata + 8);
326 type_size = AV_RL32(smk->avctx->extradata + 12);
328 ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
332 if(!get_bits1(&gb)) {
333 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
334 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
336 return AVERROR(ENOMEM);
337 smk->mmap_tbl[0] = 0;
338 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
340 ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
344 if(!get_bits1(&gb)) {
345 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
346 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
348 return AVERROR(ENOMEM);
349 smk->mclr_tbl[0] = 0;
350 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
352 ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
356 if(!get_bits1(&gb)) {
357 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
358 smk->full_tbl = av_malloc(sizeof(int) * 2);
360 return AVERROR(ENOMEM);
361 smk->full_tbl[0] = 0;
362 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
364 ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
368 if(!get_bits1(&gb)) {
369 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
370 smk->type_tbl = av_malloc(sizeof(int) * 2);
372 return AVERROR(ENOMEM);
373 smk->type_tbl[0] = 0;
374 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
376 ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
384 static av_always_inline void last_reset(int *recode, int *last) {
385 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
388 /* get code and update history */
389 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
390 register int *table = recode;
393 while(*table & SMK_NODE) {
395 table += (*table) & (~SMK_NODE);
400 if(v != recode[last[0]]) {
401 recode[last[2]] = recode[last[1]];
402 recode[last[1]] = recode[last[0]];
408 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
411 SmackVContext * const smk = avctx->priv_data;
416 int blocks, blk, bw, bh;
421 if (avpkt->size <= 769)
422 return AVERROR_INVALIDDATA;
424 if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0)
427 /* make the palette available on the way out */
428 pal = (uint32_t*)smk->pic->data[1];
429 bytestream2_init(&gb2, avpkt->data, avpkt->size);
430 flags = bytestream2_get_byteu(&gb2);
431 smk->pic->palette_has_changed = flags & 1;
432 smk->pic->key_frame = !!(flags & 2);
433 if (smk->pic->key_frame)
434 smk->pic->pict_type = AV_PICTURE_TYPE_I;
436 smk->pic->pict_type = AV_PICTURE_TYPE_P;
438 for(i = 0; i < 256; i++)
439 *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
441 last_reset(smk->mmap_tbl, smk->mmap_last);
442 last_reset(smk->mclr_tbl, smk->mclr_last);
443 last_reset(smk->full_tbl, smk->full_last);
444 last_reset(smk->type_tbl, smk->type_last);
445 if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
449 bw = avctx->width >> 2;
450 bh = avctx->height >> 2;
452 stride = smk->pic->linesize[0];
453 while(blk < blocks) {
457 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
458 run = block_runs[(type >> 2) & 0x3F];
461 while(run-- && blk < blocks){
464 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
465 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
466 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
469 for(i = 0; i < 4; i++) {
470 if(map & 1) out[0] = hi; else out[0] = lo;
471 if(map & 2) out[1] = hi; else out[1] = lo;
472 if(map & 4) out[2] = hi; else out[2] = lo;
473 if(map & 8) out[3] = hi; else out[3] = lo;
482 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
483 if(get_bits1(&gb)) mode = 1;
484 else if(get_bits1(&gb)) mode = 2;
486 while(run-- && blk < blocks){
487 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
490 for(i = 0; i < 4; i++) {
491 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
493 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
499 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
500 out[0] = out[1] = pix & 0xFF;
501 out[2] = out[3] = pix >> 8;
503 out[0] = out[1] = pix & 0xFF;
504 out[2] = out[3] = pix >> 8;
506 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
507 out[0] = out[1] = pix & 0xFF;
508 out[2] = out[3] = pix >> 8;
510 out[0] = out[1] = pix & 0xFF;
511 out[2] = out[3] = pix >> 8;
514 for(i = 0; i < 2; i++) {
516 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
517 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
531 while(run-- && blk < blocks)
536 while(run-- && blk < blocks){
538 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
539 col = mode * 0x01010101;
540 for(i = 0; i < 4; i++) {
541 *((uint32_t*)out) = col;
551 if ((ret = av_frame_ref(data, smk->pic)) < 0)
556 /* always report that the buffer was completely consumed */
561 static av_cold int decode_end(AVCodecContext *avctx)
563 SmackVContext * const smk = avctx->priv_data;
565 av_freep(&smk->mmap_tbl);
566 av_freep(&smk->mclr_tbl);
567 av_freep(&smk->full_tbl);
568 av_freep(&smk->type_tbl);
570 av_frame_free(&smk->pic);
576 static av_cold int decode_init(AVCodecContext *avctx)
578 SmackVContext * const c = avctx->priv_data;
583 avctx->pix_fmt = AV_PIX_FMT_PAL8;
585 c->pic = av_frame_alloc();
587 return AVERROR(ENOMEM);
589 /* decode huffman trees from extradata */
590 if(avctx->extradata_size < 16){
591 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
593 return AVERROR(EINVAL);
596 ret = decode_header_trees(c);
606 static av_cold int smka_decode_init(AVCodecContext *avctx)
608 if (avctx->channels < 1 || avctx->channels > 2) {
609 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
610 return AVERROR_INVALIDDATA;
612 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
613 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
619 * Decode Smacker audio data
621 static int smka_decode_frame(AVCodecContext *avctx, void *data,
622 int *got_frame_ptr, AVPacket *avpkt)
624 AVFrame *frame = data;
625 const uint8_t *buf = avpkt->data;
626 int buf_size = avpkt->size;
628 HuffContext h[4] = { { 0 } };
629 VLC vlc[4] = { { 0 } };
636 int pred[2] = {0, 0};
639 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
640 return AVERROR_INVALIDDATA;
643 unp_size = AV_RL32(buf);
645 if (unp_size > (1U<<24)) {
646 av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
647 return AVERROR_INVALIDDATA;
650 if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
654 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
658 stereo = get_bits1(&gb);
659 bits = get_bits1(&gb);
660 if (stereo ^ (avctx->channels != 1)) {
661 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
662 return AVERROR_INVALIDDATA;
664 if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
665 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
666 return AVERROR_INVALIDDATA;
669 /* get output buffer */
670 frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
671 if (unp_size % (avctx->channels * (bits + 1))) {
672 av_log(avctx, AV_LOG_ERROR,
673 "The buffer does not contain an integer number of samples\n");
674 return AVERROR_INVALIDDATA;
676 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
678 samples = (int16_t *)frame->data[0];
679 samples8 = frame->data[0];
682 for(i = 0; i < (1 << (bits + stereo)); i++) {
686 h[i].bits = av_mallocz(256 * 4);
687 h[i].lengths = av_mallocz(256 * sizeof(int));
688 h[i].values = av_mallocz(256 * sizeof(int));
689 if (!h[i].bits || !h[i].lengths || !h[i].values) {
690 ret = AVERROR(ENOMEM);
694 if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
695 ret = AVERROR_INVALIDDATA;
699 if(h[i].current > 1) {
700 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
701 h[i].lengths, sizeof(int), sizeof(int),
702 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
704 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
705 ret = AVERROR_INVALIDDATA;
710 /* this codec relies on wraparound instead of clipping audio */
711 if(bits) { //decode 16-bit data
712 for(i = stereo; i >= 0; i--)
713 pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
714 for(i = 0; i <= stereo; i++)
715 *samples++ = pred[i];
716 for(; i < unp_size / 2; i++) {
717 if(get_bits_left(&gb)<0)
718 return AVERROR_INVALIDDATA;
721 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
725 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
726 return AVERROR_INVALIDDATA;
728 val = h[2].values[res];
730 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
734 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
735 return AVERROR_INVALIDDATA;
737 val |= h[3].values[res] << 8;
738 pred[1] += sign_extend(val, 16);
739 *samples++ = pred[1];
742 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
746 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
747 return AVERROR_INVALIDDATA;
749 val = h[0].values[res];
751 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
755 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
756 return AVERROR_INVALIDDATA;
758 val |= h[1].values[res] << 8;
759 pred[0] += sign_extend(val, 16);
760 *samples++ = pred[0];
763 } else { //8-bit data
764 for(i = stereo; i >= 0; i--)
765 pred[i] = get_bits(&gb, 8);
766 for(i = 0; i <= stereo; i++)
767 *samples8++ = pred[i];
768 for(; i < unp_size; i++) {
769 if(get_bits_left(&gb)<0)
770 return AVERROR_INVALIDDATA;
773 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
777 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
778 return AVERROR_INVALIDDATA;
780 pred[1] += sign_extend(h[1].values[res], 8);
781 *samples8++ = pred[1];
784 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
788 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
789 return AVERROR_INVALIDDATA;
791 pred[0] += sign_extend(h[0].values[res], 8);
792 *samples8++ = pred[0];
801 for(i = 0; i < 4; i++) {
803 ff_free_vlc(&vlc[i]);
805 av_free(h[i].lengths);
806 av_free(h[i].values);
812 AVCodec ff_smacker_decoder = {
814 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
815 .type = AVMEDIA_TYPE_VIDEO,
816 .id = AV_CODEC_ID_SMACKVIDEO,
817 .priv_data_size = sizeof(SmackVContext),
820 .decode = decode_frame,
821 .capabilities = AV_CODEC_CAP_DR1,
824 AVCodec ff_smackaud_decoder = {
826 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
827 .type = AVMEDIA_TYPE_AUDIO,
828 .id = AV_CODEC_ID_SMACKAUDIO,
829 .init = smka_decode_init,
830 .decode = smka_decode_frame,
831 .capabilities = AV_CODEC_CAP_DR1,