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"
39 #define BITSTREAM_READER_LE
41 #include "bytestream.h"
43 #define SMKTREE_BITS 9
44 #define SMK_NODE 0x80000000
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(!get_bits1(gb
)){ //Leaf
101 if(hc
->current
>= 256){
102 av_log(NULL
, AV_LOG_ERROR
, "Tree size exceeded!\n");
106 hc
->bits
[hc
->current
] = prefix
;
107 hc
->lengths
[hc
->current
] = length
;
109 hc
->bits
[hc
->current
] = 0;
110 hc
->lengths
[hc
->current
] = 0;
112 hc
->values
[hc
->current
] = get_bits(gb
, 8);
114 if(hc
->maxlength
< length
)
115 hc
->maxlength
= length
;
120 r
= smacker_decode_tree(gb
, hc
, prefix
, length
);
123 return smacker_decode_tree(gb
, hc
, prefix
| (1 << (length
- 1)), length
);
130 static int smacker_decode_bigtree(GetBitContext
*gb
, HuffContext
*hc
, DBCtx
*ctx
)
132 if (hc
->current
+ 1 >= hc
->length
) {
133 av_log(NULL
, AV_LOG_ERROR
, "Tree size exceeded!\n");
136 if(!get_bits1(gb
)){ //Leaf
138 i1
= ctx
->v1
->table ?
get_vlc2(gb
, ctx
->v1
->table
, SMKTREE_BITS
, 3) : 0;
139 i2
= ctx
->v2
->table ?
get_vlc2(gb
, ctx
->v2
->table
, SMKTREE_BITS
, 3) : 0;
140 if (i1
< 0 || i2
< 0)
142 val
= ctx
->recode1
[i1
] | (ctx
->recode2
[i2
] << 8);
143 if(val
== ctx
->escapes
[0]) {
144 ctx
->last
[0] = hc
->current
;
146 } else if(val
== ctx
->escapes
[1]) {
147 ctx
->last
[1] = hc
->current
;
149 } else if(val
== ctx
->escapes
[2]) {
150 ctx
->last
[2] = hc
->current
;
154 hc
->values
[hc
->current
++] = val
;
160 r
= smacker_decode_bigtree(gb
, hc
, ctx
);
163 hc
->values
[t
] = SMK_NODE
| r
;
165 r_new
= smacker_decode_bigtree(gb
, hc
, ctx
);
173 * Store large tree as Libav's vlc codes
175 static int smacker_decode_header_tree(SmackVContext
*smk
, GetBitContext
*gb
, int **recodes
, int *last
, int size
)
179 HuffContext tmp1
, tmp2
;
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");
193 tmp1
.bits
= av_mallocz(256 * 4);
194 tmp1
.lengths
= av_mallocz(256 * sizeof(int));
195 tmp1
.values
= av_mallocz(256 * sizeof(int));
200 tmp2
.bits
= av_mallocz(256 * 4);
201 tmp2
.lengths
= av_mallocz(256 * sizeof(int));
202 tmp2
.values
= av_mallocz(256 * sizeof(int));
205 smacker_decode_tree(gb
, &tmp1
, 0, 0);
207 res
= init_vlc(&vlc
[0], SMKTREE_BITS
, tmp1
.length
,
208 tmp1
.lengths
, sizeof(int), sizeof(int),
209 tmp1
.bits
, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE
);
211 av_log(smk
->avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
215 av_log(smk
->avctx
, AV_LOG_ERROR
, "Skipping low bytes tree\n");
218 smacker_decode_tree(gb
, &tmp2
, 0, 0);
220 res
= init_vlc(&vlc
[1], SMKTREE_BITS
, tmp2
.length
,
221 tmp2
.lengths
, sizeof(int), sizeof(int),
222 tmp2
.bits
, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE
);
224 av_log(smk
->avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
228 av_log(smk
->avctx
, AV_LOG_ERROR
, "Skipping high bytes tree\n");
231 escapes
[0] = get_bits(gb
, 8);
232 escapes
[0] |= get_bits(gb
, 8) << 8;
233 escapes
[1] = get_bits(gb
, 8);
234 escapes
[1] |= get_bits(gb
, 8) << 8;
235 escapes
[2] = get_bits(gb
, 8);
236 escapes
[2] |= get_bits(gb
, 8) << 8;
238 last
[0] = last
[1] = last
[2] = -1;
240 ctx
.escapes
[0] = escapes
[0];
241 ctx
.escapes
[1] = escapes
[1];
242 ctx
.escapes
[2] = escapes
[2];
245 ctx
.recode1
= tmp1
.values
;
246 ctx
.recode2
= tmp2
.values
;
249 huff
.length
= ((size
+ 3) >> 2) + 4;
252 huff
.values
= av_mallocz(huff
.length
* sizeof(int));
254 if (smacker_decode_bigtree(gb
, &huff
, &ctx
) < 0)
257 if(ctx
.last
[0] == -1) ctx
.last
[0] = huff
.current
++;
258 if(ctx
.last
[1] == -1) ctx
.last
[1] = huff
.current
++;
259 if(ctx
.last
[2] == -1) ctx
.last
[2] = huff
.current
++;
261 *recodes
= huff
.values
;
264 ff_free_vlc(&vlc
[0]);
266 ff_free_vlc(&vlc
[1]);
268 av_free(tmp1
.lengths
);
269 av_free(tmp1
.values
);
271 av_free(tmp2
.lengths
);
272 av_free(tmp2
.values
);
277 static int decode_header_trees(SmackVContext
*smk
) {
279 int mmap_size
, mclr_size
, full_size
, type_size
;
281 mmap_size
= AV_RL32(smk
->avctx
->extradata
);
282 mclr_size
= AV_RL32(smk
->avctx
->extradata
+ 4);
283 full_size
= AV_RL32(smk
->avctx
->extradata
+ 8);
284 type_size
= AV_RL32(smk
->avctx
->extradata
+ 12);
286 init_get_bits(&gb
, smk
->avctx
->extradata
+ 16, (smk
->avctx
->extradata_size
- 16) * 8);
288 if(!get_bits1(&gb
)) {
289 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping MMAP tree\n");
290 smk
->mmap_tbl
= av_malloc(sizeof(int) * 2);
291 smk
->mmap_tbl
[0] = 0;
292 smk
->mmap_last
[0] = smk
->mmap_last
[1] = smk
->mmap_last
[2] = 1;
294 if (smacker_decode_header_tree(smk
, &gb
, &smk
->mmap_tbl
, smk
->mmap_last
, mmap_size
))
297 if(!get_bits1(&gb
)) {
298 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping MCLR tree\n");
299 smk
->mclr_tbl
= av_malloc(sizeof(int) * 2);
300 smk
->mclr_tbl
[0] = 0;
301 smk
->mclr_last
[0] = smk
->mclr_last
[1] = smk
->mclr_last
[2] = 1;
303 if (smacker_decode_header_tree(smk
, &gb
, &smk
->mclr_tbl
, smk
->mclr_last
, mclr_size
))
306 if(!get_bits1(&gb
)) {
307 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping FULL tree\n");
308 smk
->full_tbl
= av_malloc(sizeof(int) * 2);
309 smk
->full_tbl
[0] = 0;
310 smk
->full_last
[0] = smk
->full_last
[1] = smk
->full_last
[2] = 1;
312 if (smacker_decode_header_tree(smk
, &gb
, &smk
->full_tbl
, smk
->full_last
, full_size
))
315 if(!get_bits1(&gb
)) {
316 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping TYPE tree\n");
317 smk
->type_tbl
= av_malloc(sizeof(int) * 2);
318 smk
->type_tbl
[0] = 0;
319 smk
->type_last
[0] = smk
->type_last
[1] = smk
->type_last
[2] = 1;
321 if (smacker_decode_header_tree(smk
, &gb
, &smk
->type_tbl
, smk
->type_last
, type_size
))
328 static av_always_inline
void last_reset(int *recode
, int *last
) {
329 recode
[last
[0]] = recode
[last
[1]] = recode
[last
[2]] = 0;
332 /* get code and update history */
333 static av_always_inline
int smk_get_code(GetBitContext
*gb
, int *recode
, int *last
) {
334 register int *table
= recode
;
337 while(*table
& SMK_NODE
) {
339 table
+= (*table
) & (~SMK_NODE
);
344 if(v
!= recode
[last
[0]]) {
345 recode
[last
[2]] = recode
[last
[1]];
346 recode
[last
[1]] = recode
[last
[0]];
352 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *got_frame
,
355 SmackVContext
* const smk
= avctx
->priv_data
;
360 int blocks
, blk
, bw
, bh
;
365 if (avpkt
->size
<= 769)
368 if ((ret
= ff_reget_buffer(avctx
, &smk
->pic
)) < 0) {
369 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
373 /* make the palette available on the way out */
374 pal
= (uint32_t*)smk
->pic
.data
[1];
375 bytestream2_init(&gb2
, avpkt
->data
, avpkt
->size
);
376 flags
= bytestream2_get_byteu(&gb2
);
377 smk
->pic
.palette_has_changed
= flags
& 1;
378 smk
->pic
.key_frame
= !!(flags
& 2);
379 if(smk
->pic
.key_frame
)
380 smk
->pic
.pict_type
= AV_PICTURE_TYPE_I
;
382 smk
->pic
.pict_type
= AV_PICTURE_TYPE_P
;
384 for(i
= 0; i
< 256; i
++)
385 *pal
++ = bytestream2_get_be24u(&gb2
);
387 last_reset(smk
->mmap_tbl
, smk
->mmap_last
);
388 last_reset(smk
->mclr_tbl
, smk
->mclr_last
);
389 last_reset(smk
->full_tbl
, smk
->full_last
);
390 last_reset(smk
->type_tbl
, smk
->type_last
);
391 init_get_bits(&gb
, avpkt
->data
+ 769, (avpkt
->size
- 769) * 8);
394 bw
= avctx
->width
>> 2;
395 bh
= avctx
->height
>> 2;
397 out
= smk
->pic
.data
[0];
398 stride
= smk
->pic
.linesize
[0];
399 while(blk
< blocks
) {
403 type
= smk_get_code(&gb
, smk
->type_tbl
, smk
->type_last
);
404 run
= block_runs
[(type
>> 2) & 0x3F];
407 while(run
-- && blk
< blocks
){
410 clr
= smk_get_code(&gb
, smk
->mclr_tbl
, smk
->mclr_last
);
411 map
= smk_get_code(&gb
, smk
->mmap_tbl
, smk
->mmap_last
);
412 out
= smk
->pic
.data
[0] + (blk
/ bw
) * (stride
* 4) + (blk
% bw
) * 4;
415 for(i
= 0; i
< 4; i
++) {
416 if(map
& 1) out
[0] = hi
; else out
[0] = lo
;
417 if(map
& 2) out
[1] = hi
; else out
[1] = lo
;
418 if(map
& 4) out
[2] = hi
; else out
[2] = lo
;
419 if(map
& 8) out
[3] = hi
; else out
[3] = lo
;
428 if(avctx
->codec_tag
== MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
429 if(get_bits1(&gb
)) mode
= 1;
430 else if(get_bits1(&gb
)) mode
= 2;
432 while(run
-- && blk
< blocks
){
433 out
= smk
->pic
.data
[0] + (blk
/ bw
) * (stride
* 4) + (blk
% bw
) * 4;
436 for(i
= 0; i
< 4; i
++) {
437 pix
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
439 pix
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
445 pix
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
446 out
[0] = out
[1] = pix
& 0xFF;
447 out
[2] = out
[3] = pix
>> 8;
449 out
[0] = out
[1] = pix
& 0xFF;
450 out
[2] = out
[3] = pix
>> 8;
452 pix
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
453 out
[0] = out
[1] = pix
& 0xFF;
454 out
[2] = out
[3] = pix
>> 8;
456 out
[0] = out
[1] = pix
& 0xFF;
457 out
[2] = out
[3] = pix
>> 8;
461 for(i
= 0; i
< 2; i
++) {
463 pix2
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
464 pix1
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
478 while(run
-- && blk
< blocks
)
483 while(run
-- && blk
< blocks
){
485 out
= smk
->pic
.data
[0] + (blk
/ bw
) * (stride
* 4) + (blk
% bw
) * 4;
486 col
= mode
* 0x01010101;
487 for(i
= 0; i
< 4; i
++) {
488 *((uint32_t*)out
) = col
;
498 if ((ret
= av_frame_ref(data
, &smk
->pic
)) < 0)
503 /* always report that the buffer was completely consumed */
511 * Init smacker decoder
514 static av_cold
int decode_init(AVCodecContext
*avctx
)
516 SmackVContext
* const c
= avctx
->priv_data
;
520 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
521 avcodec_get_frame_defaults(&c
->pic
);
523 /* decode huffman trees from extradata */
524 if(avctx
->extradata_size
< 16){
525 av_log(avctx
, AV_LOG_ERROR
, "Extradata missing!\n");
529 if (decode_header_trees(c
))
539 * Uninit smacker decoder
542 static av_cold
int decode_end(AVCodecContext
*avctx
)
544 SmackVContext
* const smk
= avctx
->priv_data
;
546 av_freep(&smk
->mmap_tbl
);
547 av_freep(&smk
->mclr_tbl
);
548 av_freep(&smk
->full_tbl
);
549 av_freep(&smk
->type_tbl
);
551 av_frame_unref(&smk
->pic
);
557 static av_cold
int smka_decode_init(AVCodecContext
*avctx
)
559 if (avctx
->channels
< 1 || avctx
->channels
> 2) {
560 av_log(avctx
, AV_LOG_ERROR
, "invalid number of channels\n");
561 return AVERROR(EINVAL
);
563 avctx
->channel_layout
= (avctx
->channels
==2) ? AV_CH_LAYOUT_STEREO
: AV_CH_LAYOUT_MONO
;
564 avctx
->sample_fmt
= avctx
->bits_per_coded_sample
== 8 ? AV_SAMPLE_FMT_U8
: AV_SAMPLE_FMT_S16
;
570 * Decode Smacker audio data
572 static int smka_decode_frame(AVCodecContext
*avctx
, void *data
,
573 int *got_frame_ptr
, AVPacket
*avpkt
)
575 AVFrame
*frame
= data
;
576 const uint8_t *buf
= avpkt
->data
;
577 int buf_size
= avpkt
->size
;
579 HuffContext h
[4] = { { 0 } };
580 VLC vlc
[4] = { { 0 } };
587 int pred
[2] = {0, 0};
590 av_log(avctx
, AV_LOG_ERROR
, "packet is too small\n");
591 return AVERROR(EINVAL
);
594 unp_size
= AV_RL32(buf
);
596 init_get_bits(&gb
, buf
+ 4, (buf_size
- 4) * 8);
599 av_log(avctx
, AV_LOG_INFO
, "Sound: no data\n");
603 stereo
= get_bits1(&gb
);
604 bits
= get_bits1(&gb
);
605 if (stereo
^ (avctx
->channels
!= 1)) {
606 av_log(avctx
, AV_LOG_ERROR
, "channels mismatch\n");
607 return AVERROR(EINVAL
);
609 if (bits
&& avctx
->sample_fmt
== AV_SAMPLE_FMT_U8
) {
610 av_log(avctx
, AV_LOG_ERROR
, "sample format mismatch\n");
611 return AVERROR(EINVAL
);
614 /* get output buffer */
615 frame
->nb_samples
= unp_size
/ (avctx
->channels
* (bits
+ 1));
616 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0) {
617 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
620 samples
= (int16_t *)frame
->data
[0];
621 samples8
= frame
->data
[0];
624 for(i
= 0; i
< (1 << (bits
+ stereo
)); i
++) {
628 h
[i
].bits
= av_mallocz(256 * 4);
629 h
[i
].lengths
= av_mallocz(256 * sizeof(int));
630 h
[i
].values
= av_mallocz(256 * sizeof(int));
632 smacker_decode_tree(&gb
, &h
[i
], 0, 0);
634 if(h
[i
].current
> 1) {
635 res
= init_vlc(&vlc
[i
], SMKTREE_BITS
, h
[i
].length
,
636 h
[i
].lengths
, sizeof(int), sizeof(int),
637 h
[i
].bits
, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE
);
639 av_log(avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
644 /* this codec relies on wraparound instead of clipping audio */
645 if(bits
) { //decode 16-bit data
646 for(i
= stereo
; i
>= 0; i
--)
647 pred
[i
] = sign_extend(av_bswap16(get_bits(&gb
, 16)), 16);
648 for(i
= 0; i
<= stereo
; i
++)
649 *samples
++ = pred
[i
];
650 for(; i
< unp_size
/ 2; i
++) {
653 res
= get_vlc2(&gb
, vlc
[2].table
, SMKTREE_BITS
, 3);
656 val
= h
[2].values
[res
];
658 res
= get_vlc2(&gb
, vlc
[3].table
, SMKTREE_BITS
, 3);
661 val
|= h
[3].values
[res
] << 8;
662 pred
[1] += sign_extend(val
, 16);
663 *samples
++ = pred
[1];
666 res
= get_vlc2(&gb
, vlc
[0].table
, SMKTREE_BITS
, 3);
669 val
= h
[0].values
[res
];
671 res
= get_vlc2(&gb
, vlc
[1].table
, SMKTREE_BITS
, 3);
674 val
|= h
[1].values
[res
] << 8;
675 pred
[0] += sign_extend(val
, 16);
676 *samples
++ = pred
[0];
679 } else { //8-bit data
680 for(i
= stereo
; i
>= 0; i
--)
681 pred
[i
] = get_bits(&gb
, 8);
682 for(i
= 0; i
<= stereo
; i
++)
683 *samples8
++ = pred
[i
];
684 for(; i
< unp_size
; i
++) {
687 res
= get_vlc2(&gb
, vlc
[1].table
, SMKTREE_BITS
, 3);
690 pred
[1] += sign_extend(h
[1].values
[res
], 8);
691 *samples8
++ = pred
[1];
694 res
= get_vlc2(&gb
, vlc
[0].table
, SMKTREE_BITS
, 3);
697 pred
[0] += sign_extend(h
[0].values
[res
], 8);
698 *samples8
++ = pred
[0];
703 for(i
= 0; i
< 4; i
++) {
705 ff_free_vlc(&vlc
[i
]);
707 av_free(h
[i
].lengths
);
708 av_free(h
[i
].values
);
716 AVCodec ff_smacker_decoder
= {
718 .type
= AVMEDIA_TYPE_VIDEO
,
719 .id
= AV_CODEC_ID_SMACKVIDEO
,
720 .priv_data_size
= sizeof(SmackVContext
),
723 .decode
= decode_frame
,
724 .capabilities
= CODEC_CAP_DR1
,
725 .long_name
= NULL_IF_CONFIG_SMALL("Smacker video"),
728 AVCodec ff_smackaud_decoder
= {
730 .type
= AVMEDIA_TYPE_AUDIO
,
731 .id
= AV_CODEC_ID_SMACKAUDIO
,
732 .init
= smka_decode_init
,
733 .decode
= smka_decode_frame
,
734 .capabilities
= CODEC_CAP_DR1
,
735 .long_name
= NULL_IF_CONFIG_SMALL("Smacker audio"),