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 {
68 /* common parameters used for decode_bigtree */
69 typedef struct DBCtx {
71 int *recode1, *recode2;
76 /* possible runs of blocks */
77 static const int block_runs[64] = {
78 1, 2, 3, 4, 5, 6, 7, 8,
79 9, 10, 11, 12, 13, 14, 15, 16,
80 17, 18, 19, 20, 21, 22, 23, 24,
81 25, 26, 27, 28, 29, 30, 31, 32,
82 33, 34, 35, 36, 37, 38, 39, 40,
83 41, 42, 43, 44, 45, 46, 47, 48,
84 49, 50, 51, 52, 53, 54, 55, 56,
85 57, 58, 59, 128, 256, 512, 1024, 2048 };
94 * Decode local frame tree
96 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
98 if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
99 av_log(NULL, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
100 return AVERROR_INVALIDDATA;
103 if(!get_bits1(gb)){ //Leaf
104 if(hc->current >= hc->length){
105 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
106 return AVERROR_INVALIDDATA;
108 hc->bits[hc->current] = prefix;
109 hc->lengths[hc->current] = length;
110 hc->values[hc->current] = get_bits(gb, 8);
116 r = smacker_decode_tree(gb, hc, prefix, length);
119 return smacker_decode_tree(gb, hc, prefix | (1U << (length - 1)), length);
126 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc,
127 DBCtx *ctx, int length)
129 // Larger length can cause segmentation faults due to too deep recursion.
130 if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
131 av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
132 return AVERROR_INVALIDDATA;
135 if (hc->current >= hc->length) {
136 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
137 return AVERROR_INVALIDDATA;
139 if(!get_bits1(gb)){ //Leaf
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] = hc->current;
147 } else if(val == ctx->escapes[1]) {
148 ctx->last[1] = hc->current;
150 } else if(val == ctx->escapes[2]) {
151 ctx->last[2] = hc->current;
155 hc->values[hc->current++] = val;
161 r = smacker_decode_bigtree(gb, hc, ctx, length + 1);
164 hc->values[t] = SMK_NODE | r;
166 r_new = smacker_decode_bigtree(gb, hc, ctx, length + 1);
174 * Store large tree as FFmpeg's vlc codes
176 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
179 HuffContext h[2] = { 0 };
180 VLC vlc[2] = { { 0 } };
185 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
186 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
187 return AVERROR_INVALIDDATA;
190 for (int i = 0; i < 2; i++) {
193 h[i].bits = av_malloc(256 * sizeof(h[i].bits[0]));
194 h[i].lengths = av_malloc(256 * sizeof(h[i].lengths[0]));
195 h[i].values = av_malloc(256 * sizeof(h[i].values[0]));
196 if (!h[i].bits || !h[i].lengths || !h[i].values) {
197 err = AVERROR(ENOMEM);
200 if (!get_bits1(gb)) {
202 av_log(smk->avctx, AV_LOG_ERROR, "Skipping %s bytes tree\n",
206 err = smacker_decode_tree(gb, &h[i], 0, 0);
210 if (h[i].current > 1) {
211 err = init_vlc(&vlc[i], SMKTREE_BITS, h[i].current,
212 INIT_VLC_DEFAULT_SIZES(h[i].lengths),
213 INIT_VLC_DEFAULT_SIZES(h[i].bits),
216 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
222 escapes[0] = get_bits(gb, 16);
223 escapes[1] = get_bits(gb, 16);
224 escapes[2] = get_bits(gb, 16);
226 last[0] = last[1] = last[2] = -1;
228 ctx.escapes[0] = escapes[0];
229 ctx.escapes[1] = escapes[1];
230 ctx.escapes[2] = escapes[2];
233 ctx.recode1 = h[0].values;
234 ctx.recode2 = h[1].values;
237 huff.length = (size + 3) >> 2;
239 huff.values = av_malloc_array(huff.length + 3, sizeof(huff.values[0]));
241 err = AVERROR(ENOMEM);
244 *recodes = huff.values;
246 err = smacker_decode_bigtree(gb, &huff, &ctx, 0);
250 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
251 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
252 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
256 for (int i = 0; i < 2; i++) {
257 ff_free_vlc(&vlc[i]);
259 av_free(h[i].lengths);
260 av_free(h[i].values);
266 static int decode_header_trees(SmackVContext *smk) {
268 int mmap_size, mclr_size, full_size, type_size, ret;
271 mmap_size = AV_RL32(smk->avctx->extradata);
272 mclr_size = AV_RL32(smk->avctx->extradata + 4);
273 full_size = AV_RL32(smk->avctx->extradata + 8);
274 type_size = AV_RL32(smk->avctx->extradata + 12);
276 ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
280 if(!get_bits1(&gb)) {
282 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
283 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
285 return AVERROR(ENOMEM);
286 smk->mmap_tbl[0] = 0;
287 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
289 ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
293 if(!get_bits1(&gb)) {
295 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
296 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
298 return AVERROR(ENOMEM);
299 smk->mclr_tbl[0] = 0;
300 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
302 ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
306 if(!get_bits1(&gb)) {
308 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
309 smk->full_tbl = av_malloc(sizeof(int) * 2);
311 return AVERROR(ENOMEM);
312 smk->full_tbl[0] = 0;
313 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
315 ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
319 if(!get_bits1(&gb)) {
321 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
322 smk->type_tbl = av_malloc(sizeof(int) * 2);
324 return AVERROR(ENOMEM);
325 smk->type_tbl[0] = 0;
326 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
328 ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
333 return AVERROR_INVALIDDATA;
338 static av_always_inline void last_reset(int *recode, int *last) {
339 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
342 /* get code and update history */
343 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
344 register int *table = recode;
347 while(*table & SMK_NODE) {
348 if (get_bits_left(gb) < 1)
349 return AVERROR_INVALIDDATA;
351 table += (*table) & (~SMK_NODE);
356 if(v != recode[last[0]]) {
357 recode[last[2]] = recode[last[1]];
358 recode[last[1]] = recode[last[0]];
364 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
367 SmackVContext * const smk = avctx->priv_data;
372 int blocks, blk, bw, bh;
377 if (avpkt->size <= 769)
378 return AVERROR_INVALIDDATA;
380 if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
383 /* make the palette available on the way out */
384 pal = (uint32_t*)smk->pic->data[1];
385 bytestream2_init(&gb2, avpkt->data, avpkt->size);
386 flags = bytestream2_get_byteu(&gb2);
387 smk->pic->palette_has_changed = flags & 1;
388 smk->pic->key_frame = !!(flags & 2);
389 if (smk->pic->key_frame)
390 smk->pic->pict_type = AV_PICTURE_TYPE_I;
392 smk->pic->pict_type = AV_PICTURE_TYPE_P;
394 for(i = 0; i < 256; i++)
395 *pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
397 last_reset(smk->mmap_tbl, smk->mmap_last);
398 last_reset(smk->mclr_tbl, smk->mclr_last);
399 last_reset(smk->full_tbl, smk->full_last);
400 last_reset(smk->type_tbl, smk->type_last);
401 if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
405 bw = avctx->width >> 2;
406 bh = avctx->height >> 2;
408 stride = smk->pic->linesize[0];
409 while(blk < blocks) {
413 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
416 run = block_runs[(type >> 2) & 0x3F];
419 while(run-- && blk < blocks){
422 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
423 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
424 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
427 for(i = 0; i < 4; i++) {
428 if(map & 1) out[0] = hi; else out[0] = lo;
429 if(map & 2) out[1] = hi; else out[1] = lo;
430 if(map & 4) out[2] = hi; else out[2] = lo;
431 if(map & 8) out[3] = hi; else out[3] = lo;
440 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
441 if(get_bits1(&gb)) mode = 1;
442 else if(get_bits1(&gb)) mode = 2;
444 while(run-- && blk < blocks){
445 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
448 for(i = 0; i < 4; i++) {
449 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
451 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
457 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
458 out[0] = out[1] = pix & 0xFF;
459 out[2] = out[3] = pix >> 8;
461 out[0] = out[1] = pix & 0xFF;
462 out[2] = out[3] = pix >> 8;
464 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
465 out[0] = out[1] = pix & 0xFF;
466 out[2] = out[3] = pix >> 8;
468 out[0] = out[1] = pix & 0xFF;
469 out[2] = out[3] = pix >> 8;
472 for(i = 0; i < 2; i++) {
474 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
475 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
489 while(run-- && blk < blocks)
494 while(run-- && blk < blocks){
496 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
497 col = mode * 0x01010101U;
498 for(i = 0; i < 4; i++) {
499 *((uint32_t*)out) = col;
509 if ((ret = av_frame_ref(data, smk->pic)) < 0)
514 /* always report that the buffer was completely consumed */
519 static av_cold int decode_end(AVCodecContext *avctx)
521 SmackVContext * const smk = avctx->priv_data;
523 av_freep(&smk->mmap_tbl);
524 av_freep(&smk->mclr_tbl);
525 av_freep(&smk->full_tbl);
526 av_freep(&smk->type_tbl);
528 av_frame_free(&smk->pic);
534 static av_cold int decode_init(AVCodecContext *avctx)
536 SmackVContext * const c = avctx->priv_data;
541 avctx->pix_fmt = AV_PIX_FMT_PAL8;
543 c->pic = av_frame_alloc();
545 return AVERROR(ENOMEM);
547 /* decode huffman trees from extradata */
548 if(avctx->extradata_size < 16){
549 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
550 return AVERROR(EINVAL);
553 ret = decode_header_trees(c);
562 static av_cold int smka_decode_init(AVCodecContext *avctx)
564 if (avctx->channels < 1 || avctx->channels > 2) {
565 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
566 return AVERROR_INVALIDDATA;
568 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
569 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
575 * Decode Smacker audio data
577 static int smka_decode_frame(AVCodecContext *avctx, void *data,
578 int *got_frame_ptr, AVPacket *avpkt)
580 AVFrame *frame = data;
581 const uint8_t *buf = avpkt->data;
582 int buf_size = avpkt->size;
584 HuffContext h[4] = { { 0 } };
585 VLC vlc[4] = { { 0 } };
592 int pred[2] = {0, 0};
595 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
596 return AVERROR_INVALIDDATA;
599 unp_size = AV_RL32(buf);
601 if (unp_size > (1U<<24)) {
602 av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
603 return AVERROR_INVALIDDATA;
606 if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
610 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
614 stereo = get_bits1(&gb);
615 bits = get_bits1(&gb);
616 if (stereo ^ (avctx->channels != 1)) {
617 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
618 return AVERROR_INVALIDDATA;
620 if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
621 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
622 return AVERROR_INVALIDDATA;
625 /* get output buffer */
626 frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
627 if (unp_size % (avctx->channels * (bits + 1))) {
628 av_log(avctx, AV_LOG_ERROR,
629 "The buffer does not contain an integer number of samples\n");
630 return AVERROR_INVALIDDATA;
632 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
634 samples = (int16_t *)frame->data[0];
635 samples8 = frame->data[0];
638 for(i = 0; i < (1 << (bits + stereo)); i++) {
641 h[i].bits = av_malloc(256 * sizeof(h[i].bits));
642 h[i].lengths = av_malloc(256 * sizeof(h[i].lengths));
643 h[i].values = av_malloc(256 * sizeof(h[i].values));
644 if (!h[i].bits || !h[i].lengths || !h[i].values) {
645 ret = AVERROR(ENOMEM);
649 if ((ret = smacker_decode_tree(&gb, &h[i], 0, 0)) < 0)
652 if(h[i].current > 1) {
653 ret = init_vlc(&vlc[i], SMKTREE_BITS, h[i].current,
654 h[i].lengths, sizeof(int), sizeof(int),
655 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
657 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
662 /* this codec relies on wraparound instead of clipping audio */
663 if(bits) { //decode 16-bit data
664 for(i = stereo; i >= 0; i--)
665 pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
666 for(i = 0; i <= stereo; i++)
667 *samples++ = pred[i];
668 for(; i < unp_size / 2; i++) {
669 if (get_bits_left(&gb) < 0) {
670 ret = AVERROR_INVALIDDATA;
675 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
678 val = h[2].values[res];
680 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
683 val |= h[3].values[res] << 8;
684 pred[1] += (unsigned)sign_extend(val, 16);
685 *samples++ = pred[1];
688 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
691 val = h[0].values[res];
693 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
696 val |= h[1].values[res] << 8;
697 pred[0] += (unsigned)sign_extend(val, 16);
698 *samples++ = pred[0];
701 } else { //8-bit data
702 for(i = stereo; i >= 0; i--)
703 pred[i] = get_bits(&gb, 8);
704 for(i = 0; i <= stereo; i++)
705 *samples8++ = pred[i];
706 for(; i < unp_size; i++) {
707 if (get_bits_left(&gb) < 0) {
708 ret = AVERROR_INVALIDDATA;
713 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
716 pred[1] += sign_extend(h[1].values[res], 8);
717 *samples8++ = pred[1];
720 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
723 pred[0] += sign_extend(h[0].values[res], 8);
724 *samples8++ = pred[0];
733 for(i = 0; i < 4; i++) {
734 ff_free_vlc(&vlc[i]);
736 av_free(h[i].lengths);
737 av_free(h[i].values);
743 AVCodec ff_smacker_decoder = {
745 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
746 .type = AVMEDIA_TYPE_VIDEO,
747 .id = AV_CODEC_ID_SMACKVIDEO,
748 .priv_data_size = sizeof(SmackVContext),
751 .decode = decode_frame,
752 .capabilities = AV_CODEC_CAP_DR1,
753 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
756 AVCodec ff_smackaud_decoder = {
758 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
759 .type = AVMEDIA_TYPE_AUDIO,
760 .id = AV_CODEC_ID_SMACKAUDIO,
761 .init = smka_decode_init,
762 .decode = smka_decode_frame,
763 .capabilities = AV_CODEC_CAP_DR1,