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
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)
100 av_log(NULL, AV_LOG_ERROR, "length too long\n");
103 if(!get_bits1(gb)){ //Leaf
104 if(hc->current >= 256){
105 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
109 hc->bits[hc->current] = prefix;
110 hc->lengths[hc->current] = length;
112 hc->bits[hc->current] = 0;
113 hc->lengths[hc->current] = 0;
115 hc->values[hc->current] = get_bits(gb, 8);
117 if(hc->maxlength < length)
118 hc->maxlength = length;
123 r = smacker_decode_tree(gb, hc, prefix, length);
126 return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
133 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
135 if (hc->current + 1 >= hc->length) {
136 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
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 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 FFmpeg'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++;
263 if(huff.current > huff.length){
264 ctx.last[0] = ctx.last[1] = ctx.last[2] = 1;
265 av_log(smk->avctx, AV_LOG_ERROR, "bigtree damaged\n");
269 *recodes = huff.values;
272 ff_free_vlc(&vlc[0]);
274 ff_free_vlc(&vlc[1]);
276 av_free(tmp1.lengths);
277 av_free(tmp1.values);
279 av_free(tmp2.lengths);
280 av_free(tmp2.values);
285 static int decode_header_trees(SmackVContext *smk) {
287 int mmap_size, mclr_size, full_size, type_size;
289 mmap_size = AV_RL32(smk->avctx->extradata);
290 mclr_size = AV_RL32(smk->avctx->extradata + 4);
291 full_size = AV_RL32(smk->avctx->extradata + 8);
292 type_size = AV_RL32(smk->avctx->extradata + 12);
294 init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
296 if(!get_bits1(&gb)) {
297 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
298 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
299 smk->mmap_tbl[0] = 0;
300 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
302 if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
305 if(!get_bits1(&gb)) {
306 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
307 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
308 smk->mclr_tbl[0] = 0;
309 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
311 if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
314 if(!get_bits1(&gb)) {
315 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
316 smk->full_tbl = av_malloc(sizeof(int) * 2);
317 smk->full_tbl[0] = 0;
318 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
320 if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
323 if(!get_bits1(&gb)) {
324 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
325 smk->type_tbl = av_malloc(sizeof(int) * 2);
326 smk->type_tbl[0] = 0;
327 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
329 if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
336 static av_always_inline void last_reset(int *recode, int *last) {
337 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
340 /* get code and update history */
341 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
342 register int *table = recode;
345 while(*table & SMK_NODE) {
347 table += (*table) & (~SMK_NODE);
352 if(v != recode[last[0]]) {
353 recode[last[2]] = recode[last[1]];
354 recode[last[1]] = recode[last[0]];
360 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
362 SmackVContext * const smk = avctx->priv_data;
367 int blocks, blk, bw, bh;
372 if (avpkt->size <= 769)
375 smk->pic.reference = 3;
376 smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
377 if(avctx->reget_buffer(avctx, &smk->pic) < 0){
378 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
382 /* make the palette available on the way out */
383 pal = (uint32_t*)smk->pic.data[1];
384 bytestream2_init(&gb2, avpkt->data, avpkt->size);
385 flags = bytestream2_get_byteu(&gb2);
386 smk->pic.palette_has_changed = flags & 1;
387 smk->pic.key_frame = !!(flags & 2);
388 if(smk->pic.key_frame)
389 smk->pic.pict_type = AV_PICTURE_TYPE_I;
391 smk->pic.pict_type = AV_PICTURE_TYPE_P;
393 for(i = 0; i < 256; i++)
394 *pal++ = 0xFF << 24 | bytestream2_get_be24u(&gb2);
396 last_reset(smk->mmap_tbl, smk->mmap_last);
397 last_reset(smk->mclr_tbl, smk->mclr_last);
398 last_reset(smk->full_tbl, smk->full_last);
399 last_reset(smk->type_tbl, smk->type_last);
400 init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
403 bw = avctx->width >> 2;
404 bh = avctx->height >> 2;
406 out = smk->pic.data[0];
407 stride = smk->pic.linesize[0];
408 while(blk < blocks) {
412 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
413 run = block_runs[(type >> 2) & 0x3F];
416 while(run-- && blk < blocks){
419 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
420 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
421 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
424 for(i = 0; i < 4; i++) {
425 if(map & 1) out[0] = hi; else out[0] = lo;
426 if(map & 2) out[1] = hi; else out[1] = lo;
427 if(map & 4) out[2] = hi; else out[2] = lo;
428 if(map & 8) out[3] = hi; else out[3] = lo;
437 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
438 if(get_bits1(&gb)) mode = 1;
439 else if(get_bits1(&gb)) mode = 2;
441 while(run-- && blk < blocks){
442 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
445 for(i = 0; i < 4; i++) {
446 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
448 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
454 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
455 out[0] = out[1] = pix & 0xFF;
456 out[2] = out[3] = pix >> 8;
458 out[0] = out[1] = pix & 0xFF;
459 out[2] = out[3] = pix >> 8;
461 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
462 out[0] = out[1] = pix & 0xFF;
463 out[2] = out[3] = pix >> 8;
465 out[0] = out[1] = pix & 0xFF;
466 out[2] = out[3] = pix >> 8;
470 for(i = 0; i < 2; i++) {
472 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
473 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
487 while(run-- && blk < blocks)
492 while(run-- && blk < blocks){
494 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
495 col = mode * 0x01010101;
496 for(i = 0; i < 4; i++) {
497 *((uint32_t*)out) = col;
507 *data_size = sizeof(AVFrame);
508 *(AVFrame*)data = smk->pic;
510 /* always report that the buffer was completely consumed */
518 * Init smacker decoder
521 static av_cold int decode_init(AVCodecContext *avctx)
523 SmackVContext * const c = avctx->priv_data;
527 avctx->pix_fmt = PIX_FMT_PAL8;
529 avcodec_get_frame_defaults(&c->pic);
531 /* decode huffman trees from extradata */
532 if(avctx->extradata_size < 16){
533 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
537 if (decode_header_trees(c))
547 * Uninit smacker decoder
550 static av_cold int decode_end(AVCodecContext *avctx)
552 SmackVContext * const smk = avctx->priv_data;
554 av_freep(&smk->mmap_tbl);
555 av_freep(&smk->mclr_tbl);
556 av_freep(&smk->full_tbl);
557 av_freep(&smk->type_tbl);
559 if (smk->pic.data[0])
560 avctx->release_buffer(avctx, &smk->pic);
566 typedef struct SmackerAudioContext {
568 } SmackerAudioContext;
570 static av_cold int smka_decode_init(AVCodecContext *avctx)
572 SmackerAudioContext *s = avctx->priv_data;
574 if (avctx->channels < 1 || avctx->channels > 2) {
575 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
576 return AVERROR(EINVAL);
578 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
579 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
581 avcodec_get_frame_defaults(&s->frame);
582 avctx->coded_frame = &s->frame;
588 * Decode Smacker audio data
590 static int smka_decode_frame(AVCodecContext *avctx, void *data,
591 int *got_frame_ptr, AVPacket *avpkt)
593 SmackerAudioContext *s = avctx->priv_data;
594 const uint8_t *buf = avpkt->data;
595 int buf_size = avpkt->size;
597 HuffContext h[4] = { { 0 } };
598 VLC vlc[4] = { { 0 } };
605 int pred[2] = {0, 0};
608 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
609 return AVERROR(EINVAL);
612 unp_size = AV_RL32(buf);
614 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
617 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
621 stereo = get_bits1(&gb);
622 bits = get_bits1(&gb);
623 if (stereo ^ (avctx->channels != 1)) {
624 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
625 return AVERROR(EINVAL);
627 if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
628 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
629 return AVERROR(EINVAL);
632 /* get output buffer */
633 s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));
634 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
635 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
638 samples = (int16_t *)s->frame.data[0];
639 samples8 = s->frame.data[0];
642 for(i = 0; i < (1 << (bits + stereo)); i++) {
646 h[i].bits = av_mallocz(256 * 4);
647 h[i].lengths = av_mallocz(256 * sizeof(int));
648 h[i].values = av_mallocz(256 * sizeof(int));
650 smacker_decode_tree(&gb, &h[i], 0, 0);
652 if(h[i].current > 1) {
653 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
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 if(bits) { //decode 16-bit data
663 for(i = stereo; i >= 0; i--)
664 pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
665 for(i = 0; i <= stereo; i++)
666 *samples++ = pred[i];
667 for(; i < unp_size / 2; i++) {
668 if(get_bits_left(&gb)<0)
672 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
676 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
677 return AVERROR_INVALIDDATA;
679 val = h[2].values[res];
681 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
685 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
686 return AVERROR_INVALIDDATA;
688 val |= h[3].values[res] << 8;
689 pred[1] += sign_extend(val, 16);
690 *samples++ = av_clip_int16(pred[1]);
693 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
697 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
698 return AVERROR_INVALIDDATA;
700 val = h[0].values[res];
702 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
706 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
707 return AVERROR_INVALIDDATA;
709 val |= h[1].values[res] << 8;
710 pred[0] += sign_extend(val, 16);
711 *samples++ = av_clip_int16(pred[0]);
714 } else { //8-bit data
715 for(i = stereo; i >= 0; i--)
716 pred[i] = get_bits(&gb, 8);
717 for(i = 0; i <= stereo; i++)
718 *samples8++ = pred[i];
719 for(; i < unp_size; i++) {
720 if(get_bits_left(&gb)<0)
724 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
728 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
729 return AVERROR_INVALIDDATA;
731 pred[1] += sign_extend(h[1].values[res], 8);
732 *samples8++ = av_clip_uint8(pred[1]);
735 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
739 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
740 return AVERROR_INVALIDDATA;
742 pred[0] += sign_extend(h[0].values[res], 8);
743 *samples8++ = av_clip_uint8(pred[0]);
748 for(i = 0; i < 4; i++) {
750 ff_free_vlc(&vlc[i]);
752 av_free(h[i].lengths);
753 av_free(h[i].values);
757 *(AVFrame *)data = s->frame;
762 AVCodec ff_smacker_decoder = {
764 .type = AVMEDIA_TYPE_VIDEO,
765 .id = AV_CODEC_ID_SMACKVIDEO,
766 .priv_data_size = sizeof(SmackVContext),
769 .decode = decode_frame,
770 .capabilities = CODEC_CAP_DR1,
771 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
774 AVCodec ff_smackaud_decoder = {
776 .type = AVMEDIA_TYPE_AUDIO,
777 .id = AV_CODEC_ID_SMACKAUDIO,
778 .priv_data_size = sizeof(SmackerAudioContext),
779 .init = smka_decode_init,
780 .decode = smka_decode_frame,
781 .capabilities = CODEC_CAP_DR1,
782 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),