3 * Copyright (c) 2006 Konstantin Shishkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
36 #define ALT_BITSTREAM_READER_LE
37 #include "bitstream.h"
39 #define SMKTREE_BITS 9
40 #define SMK_NODE 0x80000000
45 typedef struct SmackVContext
{
46 AVCodecContext
*avctx
;
49 int *mmap_tbl
, *mclr_tbl
, *full_tbl
, *type_tbl
;
50 int mmap_last
[3], mclr_last
[3], full_last
[3], type_last
[3];
54 * Context used for code reconstructing
56 typedef struct HuffContext
{
65 /* common parameters used for decode_bigtree */
66 typedef struct DBCtx
{
68 int *recode1
, *recode2
;
74 /* possible runs of blocks */
75 static const int block_runs
[64] = {
76 1, 2, 3, 4, 5, 6, 7, 8,
77 9, 10, 11, 12, 13, 14, 15, 16,
78 17, 18, 19, 20, 21, 22, 23, 24,
79 25, 26, 27, 28, 29, 30, 31, 32,
80 33, 34, 35, 36, 37, 38, 39, 40,
81 41, 42, 43, 44, 45, 46, 47, 48,
82 49, 50, 51, 52, 53, 54, 55, 56,
83 57, 58, 59, 128, 256, 512, 1024, 2048 };
92 * Decode local frame tree
94 static int smacker_decode_tree(GetBitContext
*gb
, HuffContext
*hc
, uint32_t prefix
, int length
)
96 if(!get_bits1(gb
)){ //Leaf
97 if(hc
->current
>= 256){
98 av_log(NULL
, AV_LOG_ERROR
, "Tree size exceeded!\n");
102 hc
->bits
[hc
->current
] = prefix
;
103 hc
->lengths
[hc
->current
] = length
;
105 hc
->bits
[hc
->current
] = 0;
106 hc
->lengths
[hc
->current
] = 0;
108 hc
->values
[hc
->current
] = get_bits(gb
, 8);
110 if(hc
->maxlength
< length
)
111 hc
->maxlength
= length
;
116 r
= smacker_decode_tree(gb
, hc
, prefix
, length
);
119 return smacker_decode_tree(gb
, hc
, prefix
| (1 << (length
- 1)), length
);
126 static int smacker_decode_bigtree(GetBitContext
*gb
, HuffContext
*hc
, DBCtx
*ctx
)
128 if(!get_bits1(gb
)){ //Leaf
129 int val
, i1
, i2
, b1
, b2
;
130 if(hc
->current
>= hc
->length
){
131 av_log(NULL
, AV_LOG_ERROR
, "Tree size exceeded!\n");
134 b1
= get_bits_count(gb
);
135 i1
= get_vlc2(gb
, ctx
->v1
->table
, SMKTREE_BITS
, 3);
136 b1
= get_bits_count(gb
) - b1
;
137 b2
= get_bits_count(gb
);
138 i2
= get_vlc2(gb
, ctx
->v2
->table
, SMKTREE_BITS
, 3);
139 b2
= get_bits_count(gb
) - b2
;
140 val
= ctx
->recode1
[i1
] | (ctx
->recode2
[i2
] << 8);
141 if(val
== ctx
->escapes
[0]) {
142 ctx
->last
[0] = hc
->current
;
144 } else if(val
== ctx
->escapes
[1]) {
145 ctx
->last
[1] = hc
->current
;
147 } else if(val
== ctx
->escapes
[2]) {
148 ctx
->last
[2] = hc
->current
;
152 hc
->values
[hc
->current
++] = val
;
158 r
= smacker_decode_bigtree(gb
, hc
, ctx
);
161 hc
->values
[t
] = SMK_NODE
| r
;
163 r
+= smacker_decode_bigtree(gb
, hc
, ctx
);
169 * Store large tree as FFmpeg's vlc codes
171 static int smacker_decode_header_tree(SmackVContext
*smk
, GetBitContext
*gb
, int **recodes
, int *last
, int size
)
175 HuffContext tmp1
, tmp2
;
180 if(size
>= UINT_MAX
>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
181 av_log(smk
->avctx
, AV_LOG_ERROR
, "size too large\n");
188 tmp1
.bits
= av_mallocz(256 * 4);
189 tmp1
.lengths
= av_mallocz(256 * sizeof(int));
190 tmp1
.values
= av_mallocz(256 * sizeof(int));
195 tmp2
.bits
= av_mallocz(256 * 4);
196 tmp2
.lengths
= av_mallocz(256 * sizeof(int));
197 tmp2
.values
= av_mallocz(256 * sizeof(int));
199 memset(&vlc
[0], 0, sizeof(VLC
));
200 memset(&vlc
[1], 0, sizeof(VLC
));
203 smacker_decode_tree(gb
, &tmp1
, 0, 0);
205 res
= init_vlc(&vlc
[0], SMKTREE_BITS
, tmp1
.length
,
206 tmp1
.lengths
, sizeof(int), sizeof(int),
207 tmp1
.bits
, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE
);
209 av_log(smk
->avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
213 av_log(smk
->avctx
, AV_LOG_ERROR
, "Skipping low bytes tree\n");
216 smacker_decode_tree(gb
, &tmp2
, 0, 0);
218 res
= init_vlc(&vlc
[1], SMKTREE_BITS
, tmp2
.length
,
219 tmp2
.lengths
, sizeof(int), sizeof(int),
220 tmp2
.bits
, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE
);
222 av_log(smk
->avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
226 av_log(smk
->avctx
, AV_LOG_ERROR
, "Skipping high bytes tree\n");
229 escapes
[0] = get_bits(gb
, 8);
230 escapes
[0] |= get_bits(gb
, 8) << 8;
231 escapes
[1] = get_bits(gb
, 8);
232 escapes
[1] |= get_bits(gb
, 8) << 8;
233 escapes
[2] = get_bits(gb
, 8);
234 escapes
[2] |= get_bits(gb
, 8) << 8;
236 last
[0] = last
[1] = last
[2] = -1;
238 ctx
.escapes
[0] = escapes
[0];
239 ctx
.escapes
[1] = escapes
[1];
240 ctx
.escapes
[2] = escapes
[2];
243 ctx
.recode1
= tmp1
.values
;
244 ctx
.recode2
= tmp2
.values
;
247 huff
.length
= ((size
+ 3) >> 2) + 3;
250 huff
.values
= av_mallocz(huff
.length
* sizeof(int));
252 smacker_decode_bigtree(gb
, &huff
, &ctx
);
254 if(ctx
.last
[0] == -1) ctx
.last
[0] = huff
.current
++;
255 if(ctx
.last
[1] == -1) ctx
.last
[1] = huff
.current
++;
256 if(ctx
.last
[2] == -1) ctx
.last
[2] = huff
.current
++;
258 *recodes
= huff
.values
;
265 av_free(tmp1
.lengths
);
266 av_free(tmp1
.values
);
268 av_free(tmp2
.lengths
);
269 av_free(tmp2
.values
);
274 static int decode_header_trees(SmackVContext
*smk
) {
276 int mmap_size
, mclr_size
, full_size
, type_size
;
278 mmap_size
= LE_32(smk
->avctx
->extradata
);
279 mclr_size
= LE_32(smk
->avctx
->extradata
+ 4);
280 full_size
= LE_32(smk
->avctx
->extradata
+ 8);
281 type_size
= LE_32(smk
->avctx
->extradata
+ 12);
283 init_get_bits(&gb
, smk
->avctx
->extradata
+ 16, (smk
->avctx
->extradata_size
- 16) * 8);
285 if(!get_bits1(&gb
)) {
286 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping MMAP tree\n");
287 smk
->mmap_tbl
= av_malloc(sizeof(int) * 2);
288 smk
->mmap_tbl
[0] = 0;
289 smk
->mmap_last
[0] = smk
->mmap_last
[1] = smk
->mmap_last
[2] = 1;
291 smacker_decode_header_tree(smk
, &gb
, &smk
->mmap_tbl
, smk
->mmap_last
, mmap_size
);
293 if(!get_bits(&gb
, 1)) {
294 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping MCLR tree\n");
295 smk
->mclr_tbl
= av_malloc(sizeof(int) * 2);
296 smk
->mclr_tbl
[0] = 0;
297 smk
->mclr_last
[0] = smk
->mclr_last
[1] = smk
->mclr_last
[2] = 1;
299 smacker_decode_header_tree(smk
, &gb
, &smk
->mclr_tbl
, smk
->mclr_last
, mclr_size
);
301 if(!get_bits(&gb
, 1)) {
302 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping FULL tree\n");
303 smk
->full_tbl
= av_malloc(sizeof(int) * 2);
304 smk
->full_tbl
[0] = 0;
305 smk
->full_last
[0] = smk
->full_last
[1] = smk
->full_last
[2] = 1;
307 smacker_decode_header_tree(smk
, &gb
, &smk
->full_tbl
, smk
->full_last
, full_size
);
309 if(!get_bits(&gb
, 1)) {
310 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping TYPE tree\n");
311 smk
->type_tbl
= av_malloc(sizeof(int) * 2);
312 smk
->type_tbl
[0] = 0;
313 smk
->type_last
[0] = smk
->type_last
[1] = smk
->type_last
[2] = 1;
315 smacker_decode_header_tree(smk
, &gb
, &smk
->type_tbl
, smk
->type_last
, type_size
);
321 static always_inline
void last_reset(int *recode
, int *last
) {
322 recode
[last
[0]] = recode
[last
[1]] = recode
[last
[2]] = 0;
325 /* get code and update history */
326 static always_inline
int smk_get_code(GetBitContext
*gb
, int *recode
, int *last
) {
327 register int *table
= recode
;
330 b
= get_bits_count(gb
);
331 while(*table
& SMK_NODE
) {
333 table
+= (*table
) & (~SMK_NODE
);
337 b
= get_bits_count(gb
) - b
;
339 if(v
!= recode
[last
[0]]) {
340 recode
[last
[2]] = recode
[last
[1]];
341 recode
[last
[1]] = recode
[last
[0]];
347 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *data_size
, uint8_t *buf
, int buf_size
)
349 SmackVContext
* const smk
= (SmackVContext
*)avctx
->priv_data
;
353 int blocks
, blk
, bw
, bh
;
360 avctx
->release_buffer(avctx
, &smk
->pic
);
362 smk
->pic
.reference
= 1;
363 smk
->pic
.buffer_hints
= FF_BUFFER_HINTS_VALID
| FF_BUFFER_HINTS_PRESERVE
| FF_BUFFER_HINTS_REUSABLE
;
364 if(avctx
->reget_buffer(avctx
, &smk
->pic
) < 0){
365 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
369 /* make the palette available on the way out */
371 pal
= (uint32_t*)smk
->pic
.data
[1];
372 smk
->pic
.palette_has_changed
= buf
[0] & 1;
373 smk
->pic
.key_frame
= !!(buf
[0] & 2);
374 if(smk
->pic
.key_frame
)
375 smk
->pic
.pict_type
= FF_I_TYPE
;
377 smk
->pic
.pict_type
= FF_P_TYPE
;
379 for(i
= 0; i
< 256; i
++) {
384 *pal
++ = (r
<< 16) | (g
<< 8) | b
;
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
, buf
+ 769, (buf_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
);
440 pix
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
447 pix
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
448 out
[0] = out
[1] = pix
& 0xFF;
449 out
[2] = out
[3] = pix
>> 8;
451 out
[0] = out
[1] = pix
& 0xFF;
452 out
[2] = out
[3] = pix
>> 8;
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;
463 for(i
= 0; i
< 2; i
++) {
465 pix1
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
466 pix2
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
467 out
[0] = pix1
& 0xFF; out
[1] = pix1
>> 8;
468 out
[2] = pix2
& 0xFF; out
[3] = pix2
>> 8;
470 out
[0] = pix1
& 0xFF; out
[1] = pix1
>> 8;
471 out
[2] = pix2
& 0xFF; out
[3] = pix2
>> 8;
480 while(run
-- && blk
< blocks
)
485 while(run
-- && blk
< blocks
){
487 out
= smk
->pic
.data
[0] + (blk
/ bw
) * (stride
* 4) + (blk
% bw
) * 4;
488 col
= mode
* 0x01010101;
489 for(i
= 0; i
< 4; i
++) {
490 *((uint32_t*)out
) = col
;
500 *data_size
= sizeof(AVFrame
);
501 *(AVFrame
*)data
= smk
->pic
;
503 /* always report that the buffer was completely consumed */
511 * Init smacker decoder
514 static int decode_init(AVCodecContext
*avctx
)
516 SmackVContext
* const c
= (SmackVContext
*)avctx
->priv_data
;
519 avctx
->has_b_frames
= 0;
521 c
->pic
.data
[0] = NULL
;
523 if (avcodec_check_dimensions(avctx
, avctx
->height
, avctx
->width
) < 0) {
527 avctx
->pix_fmt
= PIX_FMT_PAL8
;
530 /* decode huffman trees from extradata */
531 if(avctx
->extradata_size
< 16){
532 av_log(avctx
, AV_LOG_ERROR
, "Extradata missing!\n");
536 decode_header_trees(c
);
546 * Uninit smacker decoder
549 static int decode_end(AVCodecContext
*avctx
)
551 SmackVContext
* const smk
= (SmackVContext
*)avctx
->priv_data
;
554 av_free(smk
->mmap_tbl
);
556 av_free(smk
->mclr_tbl
);
558 av_free(smk
->full_tbl
);
560 av_free(smk
->type_tbl
);
562 if (smk
->pic
.data
[0])
563 avctx
->release_buffer(avctx
, &smk
->pic
);
569 static int smka_decode_init(AVCodecContext
*avctx
)
575 * Decode Smacker audio data
577 static int smka_decode_frame(AVCodecContext
*avctx
, void *data
, int *data_size
, uint8_t *buf
, int buf_size
)
582 int16_t *samples
= data
;
587 int pred
[2] = {0, 0};
589 unp_size
= LE_32(buf
);
591 init_get_bits(&gb
, buf
+ 4, (buf_size
- 4) * 8);
594 av_log(avctx
, AV_LOG_INFO
, "Sound: no data\n");
598 stereo
= get_bits1(&gb
);
599 bits
= get_bits1(&gb
);
601 memset(vlc
, 0, sizeof(VLC
) * 4);
602 memset(h
, 0, sizeof(HuffContext
) * 4);
604 for(i
= 0; i
< (1 << (bits
+ stereo
)); i
++) {
608 h
[i
].bits
= av_mallocz(256 * 4);
609 h
[i
].lengths
= av_mallocz(256 * sizeof(int));
610 h
[i
].values
= av_mallocz(256 * sizeof(int));
612 smacker_decode_tree(&gb
, &h
[i
], 0, 0);
614 if(h
[i
].current
> 1) {
615 res
= init_vlc(&vlc
[i
], SMKTREE_BITS
, h
[i
].length
,
616 h
[i
].lengths
, sizeof(int), sizeof(int),
617 h
[i
].bits
, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE
);
619 av_log(avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
624 if(bits
) { //decode 16-bit data
625 pred
[0] = get_bits(&gb
, 8);
626 pred
[0] |= get_bits(&gb
, 8);
627 *samples
++ = pred
[0];
629 pred
[1] = get_bits(&gb
, 8);
630 pred
[1] |= get_bits(&gb
, 8);
631 *samples
++ = pred
[1];
633 for(i
= 0; i
< unp_size
/ 2; i
++) {
636 res
= get_vlc2(&gb
, vlc
[2].table
, SMKTREE_BITS
, 3);
639 val
= h
[2].values
[res
];
641 res
= get_vlc2(&gb
, vlc
[3].table
, SMKTREE_BITS
, 3);
644 val
|= h
[3].values
[res
] << 8;
645 pred
[1] += (int16_t)val
;
646 *samples
++ = pred
[1];
649 res
= get_vlc2(&gb
, vlc
[0].table
, SMKTREE_BITS
, 3);
652 val
= h
[0].values
[res
];
654 res
= get_vlc2(&gb
, vlc
[1].table
, SMKTREE_BITS
, 3);
657 val
|= h
[1].values
[res
] << 8;
659 *samples
++ = pred
[0];
662 } else { //8-bit data
663 pred
[0] = get_bits(&gb
, 8);
664 *samples
++ = (pred
[0] - 0x80) << 8;
666 pred
[1] = get_bits(&gb
, 8);
667 *samples
++ = (pred
[1] - 0x80) << 8;
669 for(i
= 0; i
< unp_size
; i
++) {
672 res
= get_vlc2(&gb
, vlc
[1].table
, SMKTREE_BITS
, 3);
675 pred
[1] += (int8_t)h
[1].values
[res
];
676 *samples
++ = (pred
[1] - 0x80) << 8;
679 res
= get_vlc2(&gb
, vlc
[0].table
, SMKTREE_BITS
, 3);
682 pred
[0] += (int8_t)h
[0].values
[res
];
683 *samples
++ = (pred
[0] - 0x80) << 8;
689 for(i
= 0; i
< 4; i
++) {
695 av_free(h
[i
].lengths
);
697 av_free(h
[i
].values
);
700 *data_size
= unp_size
;
704 AVCodec smacker_decoder
= {
708 sizeof(SmackVContext
),
715 AVCodec smackaud_decoder
= {