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
36 #define ALT_BITSTREAM_READER_LE
37 #include "bitstream.h"
38 #include "bytestream.h"
40 #define SMKTREE_BITS 9
41 #define SMK_NODE 0x80000000
46 typedef struct SmackVContext
{
47 AVCodecContext
*avctx
;
50 int *mmap_tbl
, *mclr_tbl
, *full_tbl
, *type_tbl
;
51 int mmap_last
[3], mclr_last
[3], full_last
[3], type_last
[3];
55 * Context used for code reconstructing
57 typedef struct HuffContext
{
66 /* common parameters used for decode_bigtree */
67 typedef struct DBCtx
{
69 int *recode1
, *recode2
;
75 /* possible runs of blocks */
76 static const int block_runs
[64] = {
77 1, 2, 3, 4, 5, 6, 7, 8,
78 9, 10, 11, 12, 13, 14, 15, 16,
79 17, 18, 19, 20, 21, 22, 23, 24,
80 25, 26, 27, 28, 29, 30, 31, 32,
81 33, 34, 35, 36, 37, 38, 39, 40,
82 41, 42, 43, 44, 45, 46, 47, 48,
83 49, 50, 51, 52, 53, 54, 55, 56,
84 57, 58, 59, 128, 256, 512, 1024, 2048 };
93 * Decode local frame tree
95 static int smacker_decode_tree(GetBitContext
*gb
, HuffContext
*hc
, uint32_t prefix
, int length
)
97 if(!get_bits1(gb
)){ //Leaf
98 if(hc
->current
>= 256){
99 av_log(NULL
, AV_LOG_ERROR
, "Tree size exceeded!\n");
103 hc
->bits
[hc
->current
] = prefix
;
104 hc
->lengths
[hc
->current
] = length
;
106 hc
->bits
[hc
->current
] = 0;
107 hc
->lengths
[hc
->current
] = 0;
109 hc
->values
[hc
->current
] = get_bits(gb
, 8);
111 if(hc
->maxlength
< length
)
112 hc
->maxlength
= length
;
117 r
= smacker_decode_tree(gb
, hc
, prefix
, length
);
120 return smacker_decode_tree(gb
, hc
, prefix
| (1 << (length
- 1)), length
);
127 static int smacker_decode_bigtree(GetBitContext
*gb
, HuffContext
*hc
, DBCtx
*ctx
)
129 if(!get_bits1(gb
)){ //Leaf
130 int val
, i1
, i2
, b1
, b2
;
131 if(hc
->current
>= hc
->length
){
132 av_log(NULL
, AV_LOG_ERROR
, "Tree size exceeded!\n");
135 b1
= get_bits_count(gb
);
136 i1
= get_vlc2(gb
, ctx
->v1
->table
, SMKTREE_BITS
, 3);
137 b1
= get_bits_count(gb
) - b1
;
138 b2
= get_bits_count(gb
);
139 i2
= get_vlc2(gb
, ctx
->v2
->table
, SMKTREE_BITS
, 3);
140 b2
= get_bits_count(gb
) - b2
;
141 val
= ctx
->recode1
[i1
] | (ctx
->recode2
[i2
] << 8);
142 if(val
== ctx
->escapes
[0]) {
143 ctx
->last
[0] = hc
->current
;
145 } else if(val
== ctx
->escapes
[1]) {
146 ctx
->last
[1] = hc
->current
;
148 } else if(val
== ctx
->escapes
[2]) {
149 ctx
->last
[2] = hc
->current
;
153 hc
->values
[hc
->current
++] = val
;
159 r
= smacker_decode_bigtree(gb
, hc
, ctx
);
162 hc
->values
[t
] = SMK_NODE
| r
;
164 r
+= smacker_decode_bigtree(gb
, hc
, ctx
);
170 * Store large tree as FFmpeg's vlc codes
172 static int smacker_decode_header_tree(SmackVContext
*smk
, GetBitContext
*gb
, int **recodes
, int *last
, int size
)
176 HuffContext tmp1
, tmp2
;
181 if(size
>= UINT_MAX
>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
182 av_log(smk
->avctx
, AV_LOG_ERROR
, "size too large\n");
189 tmp1
.bits
= av_mallocz(256 * 4);
190 tmp1
.lengths
= av_mallocz(256 * sizeof(int));
191 tmp1
.values
= av_mallocz(256 * sizeof(int));
196 tmp2
.bits
= av_mallocz(256 * 4);
197 tmp2
.lengths
= av_mallocz(256 * sizeof(int));
198 tmp2
.values
= av_mallocz(256 * sizeof(int));
200 memset(&vlc
[0], 0, sizeof(VLC
));
201 memset(&vlc
[1], 0, sizeof(VLC
));
204 smacker_decode_tree(gb
, &tmp1
, 0, 0);
206 res
= init_vlc(&vlc
[0], SMKTREE_BITS
, tmp1
.length
,
207 tmp1
.lengths
, sizeof(int), sizeof(int),
208 tmp1
.bits
, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE
);
210 av_log(smk
->avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
214 av_log(smk
->avctx
, AV_LOG_ERROR
, "Skipping low bytes tree\n");
217 smacker_decode_tree(gb
, &tmp2
, 0, 0);
219 res
= init_vlc(&vlc
[1], SMKTREE_BITS
, tmp2
.length
,
220 tmp2
.lengths
, sizeof(int), sizeof(int),
221 tmp2
.bits
, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE
);
223 av_log(smk
->avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
227 av_log(smk
->avctx
, AV_LOG_ERROR
, "Skipping high bytes tree\n");
230 escapes
[0] = get_bits(gb
, 8);
231 escapes
[0] |= get_bits(gb
, 8) << 8;
232 escapes
[1] = get_bits(gb
, 8);
233 escapes
[1] |= get_bits(gb
, 8) << 8;
234 escapes
[2] = get_bits(gb
, 8);
235 escapes
[2] |= get_bits(gb
, 8) << 8;
237 last
[0] = last
[1] = last
[2] = -1;
239 ctx
.escapes
[0] = escapes
[0];
240 ctx
.escapes
[1] = escapes
[1];
241 ctx
.escapes
[2] = escapes
[2];
244 ctx
.recode1
= tmp1
.values
;
245 ctx
.recode2
= tmp2
.values
;
248 huff
.length
= ((size
+ 3) >> 2) + 3;
251 huff
.values
= av_mallocz(huff
.length
* sizeof(int));
253 smacker_decode_bigtree(gb
, &huff
, &ctx
);
255 if(ctx
.last
[0] == -1) ctx
.last
[0] = huff
.current
++;
256 if(ctx
.last
[1] == -1) ctx
.last
[1] = huff
.current
++;
257 if(ctx
.last
[2] == -1) ctx
.last
[2] = huff
.current
++;
259 *recodes
= huff
.values
;
266 av_free(tmp1
.lengths
);
267 av_free(tmp1
.values
);
269 av_free(tmp2
.lengths
);
270 av_free(tmp2
.values
);
275 static int decode_header_trees(SmackVContext
*smk
) {
277 int mmap_size
, mclr_size
, full_size
, type_size
;
279 mmap_size
= AV_RL32(smk
->avctx
->extradata
);
280 mclr_size
= AV_RL32(smk
->avctx
->extradata
+ 4);
281 full_size
= AV_RL32(smk
->avctx
->extradata
+ 8);
282 type_size
= AV_RL32(smk
->avctx
->extradata
+ 12);
284 init_get_bits(&gb
, smk
->avctx
->extradata
+ 16, (smk
->avctx
->extradata_size
- 16) * 8);
286 if(!get_bits1(&gb
)) {
287 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping MMAP tree\n");
288 smk
->mmap_tbl
= av_malloc(sizeof(int) * 2);
289 smk
->mmap_tbl
[0] = 0;
290 smk
->mmap_last
[0] = smk
->mmap_last
[1] = smk
->mmap_last
[2] = 1;
292 smacker_decode_header_tree(smk
, &gb
, &smk
->mmap_tbl
, smk
->mmap_last
, mmap_size
);
294 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);
297 smk
->mclr_tbl
[0] = 0;
298 smk
->mclr_last
[0] = smk
->mclr_last
[1] = smk
->mclr_last
[2] = 1;
300 smacker_decode_header_tree(smk
, &gb
, &smk
->mclr_tbl
, smk
->mclr_last
, mclr_size
);
302 if(!get_bits1(&gb
)) {
303 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping FULL tree\n");
304 smk
->full_tbl
= av_malloc(sizeof(int) * 2);
305 smk
->full_tbl
[0] = 0;
306 smk
->full_last
[0] = smk
->full_last
[1] = smk
->full_last
[2] = 1;
308 smacker_decode_header_tree(smk
, &gb
, &smk
->full_tbl
, smk
->full_last
, full_size
);
310 if(!get_bits1(&gb
)) {
311 av_log(smk
->avctx
, AV_LOG_INFO
, "Skipping TYPE tree\n");
312 smk
->type_tbl
= av_malloc(sizeof(int) * 2);
313 smk
->type_tbl
[0] = 0;
314 smk
->type_last
[0] = smk
->type_last
[1] = smk
->type_last
[2] = 1;
316 smacker_decode_header_tree(smk
, &gb
, &smk
->type_tbl
, smk
->type_last
, type_size
);
322 static av_always_inline
void last_reset(int *recode
, int *last
) {
323 recode
[last
[0]] = recode
[last
[1]] = recode
[last
[2]] = 0;
326 /* get code and update history */
327 static av_always_inline
int smk_get_code(GetBitContext
*gb
, int *recode
, int *last
) {
328 register int *table
= recode
;
331 b
= get_bits_count(gb
);
332 while(*table
& SMK_NODE
) {
334 table
+= (*table
) & (~SMK_NODE
);
338 b
= get_bits_count(gb
) - b
;
340 if(v
!= recode
[last
[0]]) {
341 recode
[last
[2]] = recode
[last
[1]];
342 recode
[last
[1]] = recode
[last
[0]];
348 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *data_size
, const uint8_t *buf
, int buf_size
)
350 SmackVContext
* const smk
= avctx
->priv_data
;
354 int blocks
, blk
, bw
, bh
;
361 avctx
->release_buffer(avctx
, &smk
->pic
);
363 smk
->pic
.reference
= 1;
364 smk
->pic
.buffer_hints
= FF_BUFFER_HINTS_VALID
| FF_BUFFER_HINTS_PRESERVE
| FF_BUFFER_HINTS_REUSABLE
;
365 if(avctx
->reget_buffer(avctx
, &smk
->pic
) < 0){
366 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
370 /* 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
;
380 for(i
= 0; i
< 256; i
++)
381 *pal
++ = bytestream_get_be24(&buf
);
384 last_reset(smk
->mmap_tbl
, smk
->mmap_last
);
385 last_reset(smk
->mclr_tbl
, smk
->mclr_last
);
386 last_reset(smk
->full_tbl
, smk
->full_last
);
387 last_reset(smk
->type_tbl
, smk
->type_last
);
388 init_get_bits(&gb
, buf
, buf_size
* 8);
391 bw
= avctx
->width
>> 2;
392 bh
= avctx
->height
>> 2;
394 out
= smk
->pic
.data
[0];
395 stride
= smk
->pic
.linesize
[0];
396 while(blk
< blocks
) {
400 type
= smk_get_code(&gb
, smk
->type_tbl
, smk
->type_last
);
401 run
= block_runs
[(type
>> 2) & 0x3F];
404 while(run
-- && blk
< blocks
){
407 clr
= smk_get_code(&gb
, smk
->mclr_tbl
, smk
->mclr_last
);
408 map
= smk_get_code(&gb
, smk
->mmap_tbl
, smk
->mmap_last
);
409 out
= smk
->pic
.data
[0] + (blk
/ bw
) * (stride
* 4) + (blk
% bw
) * 4;
412 for(i
= 0; i
< 4; i
++) {
413 if(map
& 1) out
[0] = hi
; else out
[0] = lo
;
414 if(map
& 2) out
[1] = hi
; else out
[1] = lo
;
415 if(map
& 4) out
[2] = hi
; else out
[2] = lo
;
416 if(map
& 8) out
[3] = hi
; else out
[3] = lo
;
425 if(avctx
->codec_tag
== MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
426 if(get_bits1(&gb
)) mode
= 1;
427 else if(get_bits1(&gb
)) mode
= 2;
429 while(run
-- && blk
< blocks
){
430 out
= smk
->pic
.data
[0] + (blk
/ bw
) * (stride
* 4) + (blk
% bw
) * 4;
433 for(i
= 0; i
< 4; i
++) {
434 pix
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
436 pix
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
442 pix
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
443 out
[0] = out
[1] = pix
& 0xFF;
444 out
[2] = out
[3] = pix
>> 8;
446 out
[0] = out
[1] = pix
& 0xFF;
447 out
[2] = out
[3] = pix
>> 8;
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;
458 for(i
= 0; i
< 2; i
++) {
460 pix1
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
461 pix2
= smk_get_code(&gb
, smk
->full_tbl
, smk
->full_last
);
475 while(run
-- && blk
< blocks
)
480 while(run
-- && blk
< blocks
){
482 out
= smk
->pic
.data
[0] + (blk
/ bw
) * (stride
* 4) + (blk
% bw
) * 4;
483 col
= mode
* 0x01010101;
484 for(i
= 0; i
< 4; i
++) {
485 *((uint32_t*)out
) = col
;
495 *data_size
= sizeof(AVFrame
);
496 *(AVFrame
*)data
= smk
->pic
;
498 /* always report that the buffer was completely consumed */
506 * Init smacker decoder
509 static av_cold
int decode_init(AVCodecContext
*avctx
)
511 SmackVContext
* const c
= avctx
->priv_data
;
515 c
->pic
.data
[0] = NULL
;
517 if (avcodec_check_dimensions(avctx
, avctx
->width
, avctx
->height
) < 0) {
521 avctx
->pix_fmt
= PIX_FMT_PAL8
;
524 /* decode huffman trees from extradata */
525 if(avctx
->extradata_size
< 16){
526 av_log(avctx
, AV_LOG_ERROR
, "Extradata missing!\n");
530 decode_header_trees(c
);
540 * Uninit smacker decoder
543 static av_cold
int decode_end(AVCodecContext
*avctx
)
545 SmackVContext
* const smk
= avctx
->priv_data
;
547 av_freep(&smk
->mmap_tbl
);
548 av_freep(&smk
->mclr_tbl
);
549 av_freep(&smk
->full_tbl
);
550 av_freep(&smk
->type_tbl
);
552 if (smk
->pic
.data
[0])
553 avctx
->release_buffer(avctx
, &smk
->pic
);
559 static av_cold
int smka_decode_init(AVCodecContext
*avctx
)
565 * Decode Smacker audio data
567 static int smka_decode_frame(AVCodecContext
*avctx
, void *data
, int *data_size
, const uint8_t *buf
, int buf_size
)
572 int16_t *samples
= data
;
577 int pred
[2] = {0, 0};
579 unp_size
= AV_RL32(buf
);
581 init_get_bits(&gb
, buf
+ 4, (buf_size
- 4) * 8);
584 av_log(avctx
, AV_LOG_INFO
, "Sound: no data\n");
588 stereo
= get_bits1(&gb
);
589 bits
= get_bits1(&gb
);
590 if (unp_size
& 0xC0000000 || (unp_size
<< !bits
) > *data_size
) {
591 av_log(avctx
, AV_LOG_ERROR
, "Frame is too large to fit in buffer\n");
595 memset(vlc
, 0, sizeof(VLC
) * 4);
596 memset(h
, 0, sizeof(HuffContext
) * 4);
598 for(i
= 0; i
< (1 << (bits
+ stereo
)); i
++) {
602 h
[i
].bits
= av_mallocz(256 * 4);
603 h
[i
].lengths
= av_mallocz(256 * sizeof(int));
604 h
[i
].values
= av_mallocz(256 * sizeof(int));
606 smacker_decode_tree(&gb
, &h
[i
], 0, 0);
608 if(h
[i
].current
> 1) {
609 res
= init_vlc(&vlc
[i
], SMKTREE_BITS
, h
[i
].length
,
610 h
[i
].lengths
, sizeof(int), sizeof(int),
611 h
[i
].bits
, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE
);
613 av_log(avctx
, AV_LOG_ERROR
, "Cannot build VLC table\n");
618 if(bits
) { //decode 16-bit data
619 for(i
= stereo
; i
>= 0; i
--)
620 pred
[i
] = bswap_16(get_bits(&gb
, 16));
621 for(i
= 0; i
< stereo
; i
++)
622 *samples
++ = pred
[i
];
623 for(i
= 0; i
< unp_size
/ 2; i
++) {
626 res
= get_vlc2(&gb
, vlc
[2].table
, SMKTREE_BITS
, 3);
629 val
= h
[2].values
[res
];
631 res
= get_vlc2(&gb
, vlc
[3].table
, SMKTREE_BITS
, 3);
634 val
|= h
[3].values
[res
] << 8;
635 pred
[1] += (int16_t)val
;
636 *samples
++ = pred
[1];
639 res
= get_vlc2(&gb
, vlc
[0].table
, SMKTREE_BITS
, 3);
642 val
= h
[0].values
[res
];
644 res
= get_vlc2(&gb
, vlc
[1].table
, SMKTREE_BITS
, 3);
647 val
|= h
[1].values
[res
] << 8;
649 *samples
++ = pred
[0];
652 } else { //8-bit data
653 for(i
= stereo
; i
>= 0; i
--)
654 pred
[i
] = get_bits(&gb
, 8);
655 for(i
= 0; i
< stereo
; i
++)
656 *samples
++ = (pred
[i
] - 0x80) << 8;
657 for(i
= 0; i
< unp_size
; i
++) {
660 res
= get_vlc2(&gb
, vlc
[1].table
, SMKTREE_BITS
, 3);
663 pred
[1] += (int8_t)h
[1].values
[res
];
664 *samples
++ = (pred
[1] - 0x80) << 8;
667 res
= get_vlc2(&gb
, vlc
[0].table
, SMKTREE_BITS
, 3);
670 pred
[0] += (int8_t)h
[0].values
[res
];
671 *samples
++ = (pred
[0] - 0x80) << 8;
677 for(i
= 0; i
< 4; i
++) {
683 av_free(h
[i
].lengths
);
685 av_free(h
[i
].values
);
688 *data_size
= unp_size
;
692 AVCodec smacker_decoder
= {
696 sizeof(SmackVContext
),
701 .long_name
= "Smacker video",
704 AVCodec smackaud_decoder
= {
713 .long_name
= "Smacker audio",