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
35 #include "libavutil/audioconvert.h"
38 #define BITSTREAM_READER_LE
40 #include "bytestream.h"
42 #define SMKTREE_BITS 9
43 #define SMK_NODE 0x80000000
48 typedef struct SmackVContext {
49 AVCodecContext *avctx;
52 int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
53 int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
57 * Context used for code reconstructing
59 typedef struct HuffContext {
68 /* common parameters used for decode_bigtree */
69 typedef struct DBCtx {
71 int *recode1, *recode2;
77 /* possible runs of blocks */
78 static const int block_runs[64] = {
79 1, 2, 3, 4, 5, 6, 7, 8,
80 9, 10, 11, 12, 13, 14, 15, 16,
81 17, 18, 19, 20, 21, 22, 23, 24,
82 25, 26, 27, 28, 29, 30, 31, 32,
83 33, 34, 35, 36, 37, 38, 39, 40,
84 41, 42, 43, 44, 45, 46, 47, 48,
85 49, 50, 51, 52, 53, 54, 55, 56,
86 57, 58, 59, 128, 256, 512, 1024, 2048 };
95 * Decode local frame tree
97 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
99 if(!get_bits1(gb)){ //Leaf
100 if(hc->current >= 256){
101 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
105 hc->bits[hc->current] = prefix;
106 hc->lengths[hc->current] = length;
108 hc->bits[hc->current] = 0;
109 hc->lengths[hc->current] = 0;
111 hc->values[hc->current] = get_bits(gb, 8);
113 if(hc->maxlength < length)
114 hc->maxlength = length;
119 r = smacker_decode_tree(gb, hc, prefix, length);
122 return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
129 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
131 if (hc->current + 1 >= hc->length) {
132 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
135 if(!get_bits1(gb)){ //Leaf
136 int val, i1, i2, b1, b2;
137 b1 = get_bits_count(gb);
138 i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
139 b1 = get_bits_count(gb) - b1;
140 b2 = get_bits_count(gb);
141 i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
142 b2 = get_bits_count(gb) - b2;
143 if (i1 < 0 || i2 < 0)
145 val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
146 if(val == ctx->escapes[0]) {
147 ctx->last[0] = hc->current;
149 } else if(val == ctx->escapes[1]) {
150 ctx->last[1] = hc->current;
152 } else if(val == ctx->escapes[2]) {
153 ctx->last[2] = hc->current;
157 hc->values[hc->current++] = val;
163 r = smacker_decode_bigtree(gb, hc, ctx);
166 hc->values[t] = SMK_NODE | r;
168 r_new = smacker_decode_bigtree(gb, hc, ctx);
176 * Store large tree as Libav's vlc codes
178 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
182 HuffContext tmp1, tmp2;
183 VLC vlc[2] = { { 0 } };
188 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
189 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
196 tmp1.bits = av_mallocz(256 * 4);
197 tmp1.lengths = av_mallocz(256 * sizeof(int));
198 tmp1.values = av_mallocz(256 * sizeof(int));
203 tmp2.bits = av_mallocz(256 * 4);
204 tmp2.lengths = av_mallocz(256 * sizeof(int));
205 tmp2.values = av_mallocz(256 * sizeof(int));
208 smacker_decode_tree(gb, &tmp1, 0, 0);
210 res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
211 tmp1.lengths, sizeof(int), sizeof(int),
212 tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
214 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
218 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
221 smacker_decode_tree(gb, &tmp2, 0, 0);
223 res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
224 tmp2.lengths, sizeof(int), sizeof(int),
225 tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
227 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
231 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
234 escapes[0] = get_bits(gb, 8);
235 escapes[0] |= get_bits(gb, 8) << 8;
236 escapes[1] = get_bits(gb, 8);
237 escapes[1] |= get_bits(gb, 8) << 8;
238 escapes[2] = get_bits(gb, 8);
239 escapes[2] |= get_bits(gb, 8) << 8;
241 last[0] = last[1] = last[2] = -1;
243 ctx.escapes[0] = escapes[0];
244 ctx.escapes[1] = escapes[1];
245 ctx.escapes[2] = escapes[2];
248 ctx.recode1 = tmp1.values;
249 ctx.recode2 = tmp2.values;
252 huff.length = ((size + 3) >> 2) + 3;
255 huff.values = av_mallocz(huff.length * sizeof(int));
257 if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
260 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
261 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
262 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
264 *recodes = huff.values;
267 ff_free_vlc(&vlc[0]);
269 ff_free_vlc(&vlc[1]);
271 av_free(tmp1.lengths);
272 av_free(tmp1.values);
274 av_free(tmp2.lengths);
275 av_free(tmp2.values);
280 static int decode_header_trees(SmackVContext *smk) {
282 int mmap_size, mclr_size, full_size, type_size;
284 mmap_size = AV_RL32(smk->avctx->extradata);
285 mclr_size = AV_RL32(smk->avctx->extradata + 4);
286 full_size = AV_RL32(smk->avctx->extradata + 8);
287 type_size = AV_RL32(smk->avctx->extradata + 12);
289 init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
291 if(!get_bits1(&gb)) {
292 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
293 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
294 smk->mmap_tbl[0] = 0;
295 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
297 if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
300 if(!get_bits1(&gb)) {
301 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
302 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
303 smk->mclr_tbl[0] = 0;
304 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
306 if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
309 if(!get_bits1(&gb)) {
310 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
311 smk->full_tbl = av_malloc(sizeof(int) * 2);
312 smk->full_tbl[0] = 0;
313 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
315 if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
318 if(!get_bits1(&gb)) {
319 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
320 smk->type_tbl = av_malloc(sizeof(int) * 2);
321 smk->type_tbl[0] = 0;
322 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
324 if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
331 static av_always_inline void last_reset(int *recode, int *last) {
332 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
335 /* get code and update history */
336 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
337 register int *table = recode;
340 while(*table & SMK_NODE) {
342 table += (*table) & (~SMK_NODE);
347 if(v != recode[last[0]]) {
348 recode[last[2]] = recode[last[1]];
349 recode[last[1]] = recode[last[0]];
355 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
357 SmackVContext * const smk = avctx->priv_data;
362 int blocks, blk, bw, bh;
367 if (avpkt->size <= 769)
370 smk->pic.reference = 1;
371 smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
372 if(avctx->reget_buffer(avctx, &smk->pic) < 0){
373 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
377 /* make the palette available on the way out */
378 pal = (uint32_t*)smk->pic.data[1];
379 bytestream2_init(&gb2, avpkt->data, avpkt->size);
380 flags = bytestream2_get_byteu(&gb2);
381 smk->pic.palette_has_changed = flags & 1;
382 smk->pic.key_frame = !!(flags & 2);
383 if(smk->pic.key_frame)
384 smk->pic.pict_type = AV_PICTURE_TYPE_I;
386 smk->pic.pict_type = AV_PICTURE_TYPE_P;
388 for(i = 0; i < 256; i++)
389 *pal++ = bytestream2_get_be24u(&gb2);
391 last_reset(smk->mmap_tbl, smk->mmap_last);
392 last_reset(smk->mclr_tbl, smk->mclr_last);
393 last_reset(smk->full_tbl, smk->full_last);
394 last_reset(smk->type_tbl, smk->type_last);
395 init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
398 bw = avctx->width >> 2;
399 bh = avctx->height >> 2;
401 out = smk->pic.data[0];
402 stride = smk->pic.linesize[0];
403 while(blk < blocks) {
407 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
408 run = block_runs[(type >> 2) & 0x3F];
411 while(run-- && blk < blocks){
414 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
415 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
416 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
419 for(i = 0; i < 4; i++) {
420 if(map & 1) out[0] = hi; else out[0] = lo;
421 if(map & 2) out[1] = hi; else out[1] = lo;
422 if(map & 4) out[2] = hi; else out[2] = lo;
423 if(map & 8) out[3] = hi; else out[3] = lo;
432 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
433 if(get_bits1(&gb)) mode = 1;
434 else if(get_bits1(&gb)) mode = 2;
436 while(run-- && blk < blocks){
437 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
440 for(i = 0; i < 4; i++) {
441 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
443 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
449 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
450 out[0] = out[1] = pix & 0xFF;
451 out[2] = out[3] = pix >> 8;
453 out[0] = out[1] = pix & 0xFF;
454 out[2] = out[3] = pix >> 8;
456 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
457 out[0] = out[1] = pix & 0xFF;
458 out[2] = out[3] = pix >> 8;
460 out[0] = out[1] = pix & 0xFF;
461 out[2] = out[3] = pix >> 8;
465 for(i = 0; i < 2; i++) {
467 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
468 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
482 while(run-- && blk < blocks)
487 while(run-- && blk < blocks){
489 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
490 col = mode * 0x01010101;
491 for(i = 0; i < 4; i++) {
492 *((uint32_t*)out) = col;
502 *data_size = sizeof(AVFrame);
503 *(AVFrame*)data = smk->pic;
505 /* always report that the buffer was completely consumed */
513 * Init smacker decoder
516 static av_cold int decode_init(AVCodecContext *avctx)
518 SmackVContext * const c = avctx->priv_data;
522 avctx->pix_fmt = PIX_FMT_PAL8;
525 /* decode huffman trees from extradata */
526 if(avctx->extradata_size < 16){
527 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
531 if (decode_header_trees(c))
541 * Uninit smacker decoder
544 static av_cold int decode_end(AVCodecContext *avctx)
546 SmackVContext * const smk = avctx->priv_data;
548 av_freep(&smk->mmap_tbl);
549 av_freep(&smk->mclr_tbl);
550 av_freep(&smk->full_tbl);
551 av_freep(&smk->type_tbl);
553 if (smk->pic.data[0])
554 avctx->release_buffer(avctx, &smk->pic);
560 typedef struct SmackerAudioContext {
562 } SmackerAudioContext;
564 static av_cold int smka_decode_init(AVCodecContext *avctx)
566 SmackerAudioContext *s = avctx->priv_data;
568 if (avctx->channels < 1 || avctx->channels > 2) {
569 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
570 return AVERROR(EINVAL);
572 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
573 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
575 avcodec_get_frame_defaults(&s->frame);
576 avctx->coded_frame = &s->frame;
582 * Decode Smacker audio data
584 static int smka_decode_frame(AVCodecContext *avctx, void *data,
585 int *got_frame_ptr, AVPacket *avpkt)
587 SmackerAudioContext *s = avctx->priv_data;
588 const uint8_t *buf = avpkt->data;
589 int buf_size = avpkt->size;
591 HuffContext h[4] = { { 0 } };
592 VLC vlc[4] = { { 0 } };
599 int pred[2] = {0, 0};
602 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
603 return AVERROR(EINVAL);
606 unp_size = AV_RL32(buf);
608 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
611 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
615 stereo = get_bits1(&gb);
616 bits = get_bits1(&gb);
617 if (stereo ^ (avctx->channels != 1)) {
618 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
619 return AVERROR(EINVAL);
621 if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
622 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
623 return AVERROR(EINVAL);
626 /* get output buffer */
627 s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));
628 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
629 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
632 samples = (int16_t *)s->frame.data[0];
633 samples8 = s->frame.data[0];
636 for(i = 0; i < (1 << (bits + stereo)); i++) {
640 h[i].bits = av_mallocz(256 * 4);
641 h[i].lengths = av_mallocz(256 * sizeof(int));
642 h[i].values = av_mallocz(256 * sizeof(int));
644 smacker_decode_tree(&gb, &h[i], 0, 0);
646 if(h[i].current > 1) {
647 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
648 h[i].lengths, sizeof(int), sizeof(int),
649 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
651 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
656 if(bits) { //decode 16-bit data
657 for(i = stereo; i >= 0; i--)
658 pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
659 for(i = 0; i <= stereo; i++)
660 *samples++ = pred[i];
661 for(; i < unp_size / 2; i++) {
664 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
667 val = h[2].values[res];
669 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
672 val |= h[3].values[res] << 8;
673 pred[1] += sign_extend(val, 16);
674 *samples++ = av_clip_int16(pred[1]);
677 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
680 val = h[0].values[res];
682 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
685 val |= h[1].values[res] << 8;
686 pred[0] += sign_extend(val, 16);
687 *samples++ = av_clip_int16(pred[0]);
690 } else { //8-bit data
691 for(i = stereo; i >= 0; i--)
692 pred[i] = get_bits(&gb, 8);
693 for(i = 0; i <= stereo; i++)
694 *samples8++ = pred[i];
695 for(; i < unp_size; i++) {
698 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
701 pred[1] += sign_extend(h[1].values[res], 8);
702 *samples8++ = av_clip_uint8(pred[1]);
705 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
708 pred[0] += sign_extend(h[0].values[res], 8);
709 *samples8++ = av_clip_uint8(pred[0]);
714 for(i = 0; i < 4; i++) {
716 ff_free_vlc(&vlc[i]);
718 av_free(h[i].lengths);
719 av_free(h[i].values);
723 *(AVFrame *)data = s->frame;
728 AVCodec ff_smacker_decoder = {
730 .type = AVMEDIA_TYPE_VIDEO,
731 .id = CODEC_ID_SMACKVIDEO,
732 .priv_data_size = sizeof(SmackVContext),
735 .decode = decode_frame,
736 .capabilities = CODEC_CAP_DR1,
737 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
740 AVCodec ff_smackaud_decoder = {
742 .type = AVMEDIA_TYPE_AUDIO,
743 .id = CODEC_ID_SMACKAUDIO,
744 .priv_data_size = sizeof(SmackerAudioContext),
745 .init = smka_decode_init,
746 .decode = smka_decode_frame,
747 .capabilities = CODEC_CAP_DR1,
748 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),