3209
|
1 /*
|
|
2 * Smacker decoder
|
|
3 * Copyright (c) 2006 Konstantin Shishkov
|
|
4 *
|
|
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.
|
|
9 *
|
|
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.
|
|
14 *
|
|
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
|
|
18 *
|
|
19 */
|
|
20
|
|
21 /**
|
|
22 * @file smacker.c
|
|
23 * Smacker decoder
|
|
24 */
|
|
25
|
|
26 /*
|
|
27 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
|
|
28 */
|
|
29
|
|
30 #include <stdio.h>
|
|
31 #include <stdlib.h>
|
|
32
|
|
33 #include "common.h"
|
|
34 #include "avcodec.h"
|
|
35
|
|
36 #define ALT_BITSTREAM_READER_LE
|
|
37 #include "bitstream.h"
|
|
38
|
|
39 #define SMKTREE_BITS 9
|
|
40 #define SMK_NODE 0x80000000
|
|
41
|
|
42 /*
|
|
43 * Decoder context
|
|
44 */
|
|
45 typedef struct SmackVContext {
|
|
46 AVCodecContext *avctx;
|
|
47 AVFrame pic;
|
|
48
|
|
49 int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
|
|
50 int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
|
|
51 } SmackVContext;
|
|
52
|
|
53 /**
|
|
54 * Context used for code reconstructing
|
|
55 */
|
|
56 typedef struct HuffContext {
|
|
57 int length;
|
|
58 int maxlength;
|
|
59 int current;
|
|
60 uint32_t *bits;
|
|
61 int *lengths;
|
|
62 int *values;
|
|
63 } HuffContext;
|
|
64
|
|
65 /* common parameters used for decode_bigtree */
|
|
66 typedef struct DBCtx {
|
|
67 VLC *v1, *v2;
|
|
68 int *recode1, *recode2;
|
|
69 int escapes[3];
|
|
70 int *last;
|
|
71 int lcur;
|
|
72 } DBCtx;
|
|
73
|
|
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 };
|
|
84
|
|
85 enum SmkBlockTypes {
|
|
86 SMK_BLK_MONO = 0,
|
|
87 SMK_BLK_FULL = 1,
|
|
88 SMK_BLK_SKIP = 2,
|
|
89 SMK_BLK_FILL = 3 };
|
|
90
|
|
91 /**
|
|
92 * Decode local frame tree
|
|
93 */
|
|
94 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
|
|
95 {
|
|
96 if(!get_bits1(gb)){ //Leaf
|
|
97 if(hc->current >= 256){
|
|
98 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
|
|
99 return -1;
|
|
100 }
|
|
101 if(length){
|
|
102 hc->bits[hc->current] = prefix;
|
|
103 hc->lengths[hc->current] = length;
|
|
104 } else {
|
|
105 hc->bits[hc->current] = 0;
|
|
106 hc->lengths[hc->current] = 0;
|
|
107 }
|
|
108 hc->values[hc->current] = get_bits(gb, 8);
|
|
109 hc->current++;
|
|
110 if(hc->maxlength < length)
|
|
111 hc->maxlength = length;
|
|
112 return 0;
|
|
113 } else { //Node
|
|
114 int r;
|
|
115 length++;
|
|
116 r = smacker_decode_tree(gb, hc, prefix, length);
|
|
117 if(r)
|
|
118 return r;
|
|
119 return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
|
|
120 }
|
|
121 }
|
|
122
|
|
123 /**
|
|
124 * Decode header tree
|
|
125 */
|
|
126 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
|
|
127 {
|
|
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");
|
|
132 return -1;
|
|
133 }
|
|
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;
|
|
143 val = 0;
|
|
144 } else if(val == ctx->escapes[1]) {
|
|
145 ctx->last[1] = hc->current;
|
|
146 val = 0;
|
|
147 } else if(val == ctx->escapes[2]) {
|
|
148 ctx->last[2] = hc->current;
|
|
149 val = 0;
|
|
150 }
|
|
151
|
|
152 hc->values[hc->current++] = val;
|
|
153 return 1;
|
|
154 } else { //Node
|
|
155 int r = 0, t;
|
|
156
|
|
157 t = hc->current++;
|
|
158 r = smacker_decode_bigtree(gb, hc, ctx);
|
|
159 if(r < 0)
|
|
160 return r;
|
|
161 hc->values[t] = SMK_NODE | r;
|
|
162 r++;
|
|
163 r += smacker_decode_bigtree(gb, hc, ctx);
|
|
164 return r;
|
|
165 }
|
|
166 }
|
|
167
|
|
168 /**
|
|
169 * Store large tree as FFmpeg's vlc codes
|
|
170 */
|
|
171 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
|
|
172 {
|
|
173 int res;
|
|
174 HuffContext huff;
|
|
175 HuffContext tmp1, tmp2;
|
|
176 VLC vlc[2];
|
|
177 int escapes[3];
|
|
178 DBCtx ctx;
|
|
179
|
|
180 tmp1.length = 256;
|
|
181 tmp1.maxlength = 0;
|
|
182 tmp1.current = 0;
|
|
183 tmp1.bits = av_mallocz(256 * 4);
|
|
184 tmp1.lengths = av_mallocz(256 * sizeof(int));
|
|
185 tmp1.values = av_mallocz(256 * sizeof(int));
|
|
186
|
|
187 tmp2.length = 256;
|
|
188 tmp2.maxlength = 0;
|
|
189 tmp2.current = 0;
|
|
190 tmp2.bits = av_mallocz(256 * 4);
|
|
191 tmp2.lengths = av_mallocz(256 * sizeof(int));
|
|
192 tmp2.values = av_mallocz(256 * sizeof(int));
|
|
193
|
|
194 memset(&vlc[0], 0, sizeof(VLC));
|
|
195 memset(&vlc[1], 0, sizeof(VLC));
|
|
196
|
|
197 if(get_bits1(gb)) {
|
|
198 smacker_decode_tree(gb, &tmp1, 0, 0);
|
|
199 get_bits1(gb);
|
|
200 res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
|
|
201 tmp1.lengths, sizeof(int), sizeof(int),
|
|
202 tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
|
|
203 if(res < 0) {
|
|
204 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
|
|
205 return -1;
|
|
206 }
|
|
207 } else {
|
|
208 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
|
|
209 }
|
|
210 if(get_bits1(gb)){
|
|
211 smacker_decode_tree(gb, &tmp2, 0, 0);
|
|
212 get_bits1(gb);
|
|
213 res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
|
|
214 tmp2.lengths, sizeof(int), sizeof(int),
|
|
215 tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
|
|
216 if(res < 0) {
|
|
217 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
|
|
218 return -1;
|
|
219 }
|
|
220 } else {
|
|
221 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
|
|
222 }
|
|
223
|
|
224 escapes[0] = get_bits(gb, 8);
|
|
225 escapes[0] |= get_bits(gb, 8) << 8;
|
|
226 escapes[1] = get_bits(gb, 8);
|
|
227 escapes[1] |= get_bits(gb, 8) << 8;
|
|
228 escapes[2] = get_bits(gb, 8);
|
|
229 escapes[2] |= get_bits(gb, 8) << 8;
|
|
230
|
|
231 last[0] = last[1] = last[2] = -1;
|
|
232
|
|
233 ctx.escapes[0] = escapes[0];
|
|
234 ctx.escapes[1] = escapes[1];
|
|
235 ctx.escapes[2] = escapes[2];
|
|
236 ctx.v1 = &vlc[0];
|
|
237 ctx.v2 = &vlc[1];
|
|
238 ctx.recode1 = tmp1.values;
|
|
239 ctx.recode2 = tmp2.values;
|
|
240 ctx.last = last;
|
|
241
|
|
242 huff.length = ((size + 3) >> 2) + 3;
|
|
243 huff.maxlength = 0;
|
|
244 huff.current = 0;
|
|
245 huff.values = av_mallocz(huff.length * sizeof(int));
|
|
246
|
|
247 smacker_decode_bigtree(gb, &huff, &ctx);
|
|
248 get_bits1(gb);
|
|
249 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
|
|
250 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
|
|
251 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
|
|
252
|
|
253 *recodes = huff.values;
|
|
254
|
|
255 if(vlc[0].table)
|
|
256 free_vlc(&vlc[0]);
|
|
257 if(vlc[1].table)
|
|
258 free_vlc(&vlc[1]);
|
|
259 av_free(tmp1.bits);
|
|
260 av_free(tmp1.lengths);
|
|
261 av_free(tmp1.values);
|
|
262 av_free(tmp2.bits);
|
|
263 av_free(tmp2.lengths);
|
|
264 av_free(tmp2.values);
|
|
265
|
|
266 return 0;
|
|
267 }
|
|
268
|
|
269 static int decode_header_trees(SmackVContext *smk) {
|
|
270 GetBitContext gb;
|
|
271 int mmap_size, mclr_size, full_size, type_size;
|
|
272
|
|
273 mmap_size = LE_32(smk->avctx->extradata);
|
|
274 mclr_size = LE_32(smk->avctx->extradata + 4);
|
|
275 full_size = LE_32(smk->avctx->extradata + 8);
|
|
276 type_size = LE_32(smk->avctx->extradata + 12);
|
|
277
|
|
278 init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
|
|
279
|
|
280 if(!get_bits1(&gb)) {
|
|
281 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
|
|
282 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
|
|
283 smk->mmap_tbl[0] = 0;
|
|
284 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
|
|
285 } else {
|
|
286 smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
|
|
287 }
|
|
288 if(!get_bits(&gb, 1)) {
|
|
289 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
|
|
290 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
|
|
291 smk->mclr_tbl[0] = 0;
|
|
292 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
|
|
293 } else {
|
|
294 smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
|
|
295 }
|
|
296 if(!get_bits(&gb, 1)) {
|
|
297 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
|
|
298 smk->full_tbl = av_malloc(sizeof(int) * 2);
|
|
299 smk->full_tbl[0] = 0;
|
|
300 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
|
|
301 } else {
|
|
302 smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
|
|
303 }
|
|
304 if(!get_bits(&gb, 1)) {
|
|
305 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
|
|
306 smk->type_tbl = av_malloc(sizeof(int) * 2);
|
|
307 smk->type_tbl[0] = 0;
|
|
308 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
|
|
309 } else {
|
|
310 smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
|
|
311 }
|
|
312
|
|
313 return 0;
|
|
314 }
|
|
315
|
|
316 static always_inline void last_reset(int *recode, int *last) {
|
|
317 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
|
|
318 }
|
|
319
|
|
320 /* get code and update history */
|
|
321 static always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
|
|
322 register int *table = recode;
|
|
323 int v, b;
|
|
324
|
|
325 b = get_bits_count(gb);
|
|
326 while(*table & SMK_NODE) {
|
|
327 if(get_bits1(gb))
|
|
328 table += (*table) & (~SMK_NODE);
|
|
329 table++;
|
|
330 }
|
|
331 v = *table;
|
|
332 b = get_bits_count(gb) - b;
|
|
333
|
|
334 if(v != recode[last[0]]) {
|
|
335 recode[last[2]] = recode[last[1]];
|
|
336 recode[last[1]] = recode[last[0]];
|
|
337 recode[last[0]] = v;
|
|
338 }
|
|
339 return v;
|
|
340 }
|
|
341
|
|
342 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
|
|
343 {
|
|
344 SmackVContext * const smk = (SmackVContext *)avctx->priv_data;
|
|
345 uint8_t *out;
|
|
346 uint32_t *pal;
|
|
347 GetBitContext gb;
|
|
348 int blocks, blk, bw, bh;
|
|
349 int i;
|
|
350 int stride;
|
|
351
|
|
352 if(buf_size == 769)
|
|
353 return 0;
|
|
354 if(smk->pic.data[0])
|
|
355 avctx->release_buffer(avctx, &smk->pic);
|
|
356
|
|
357 smk->pic.reference = 1;
|
|
358 smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
|
|
359 if(avctx->reget_buffer(avctx, &smk->pic) < 0){
|
|
360 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
|
361 return -1;
|
|
362 }
|
|
363
|
|
364 /* make the palette available on the way out */
|
|
365 out = buf + 1;
|
|
366 pal = (uint32_t*)smk->pic.data[1];
|
|
367 smk->pic.palette_has_changed = buf[0] & 1;
|
|
368 smk->pic.key_frame = !!(buf[0] & 2);
|
|
369 if(smk->pic.key_frame)
|
|
370 smk->pic.pict_type = FF_I_TYPE;
|
|
371 else
|
|
372 smk->pic.pict_type = FF_P_TYPE;
|
|
373
|
|
374 for(i = 0; i < 256; i++) {
|
|
375 int r, g, b;
|
|
376 r = *out++;
|
|
377 g = *out++;
|
|
378 b = *out++;
|
|
379 *pal++ = (r << 16) | (g << 8) | b;
|
|
380 }
|
|
381
|
|
382 last_reset(smk->mmap_tbl, smk->mmap_last);
|
|
383 last_reset(smk->mclr_tbl, smk->mclr_last);
|
|
384 last_reset(smk->full_tbl, smk->full_last);
|
|
385 last_reset(smk->type_tbl, smk->type_last);
|
|
386 init_get_bits(&gb, buf + 769, (buf_size - 769) * 8);
|
|
387
|
|
388 blk = 0;
|
|
389 bw = avctx->width >> 2;
|
|
390 bh = avctx->height >> 2;
|
|
391 blocks = bw * bh;
|
|
392 out = smk->pic.data[0];
|
|
393 stride = smk->pic.linesize[0];
|
|
394 while(blk < blocks) {
|
|
395 int type, run, mode;
|
|
396 uint16_t pix;
|
|
397
|
|
398 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
|
|
399 run = block_runs[(type >> 2) & 0x3F];
|
|
400 switch(type & 3){
|
|
401 case SMK_BLK_MONO:
|
|
402 while(run-- && blk < blocks){
|
|
403 int clr, map;
|
|
404 int hi, lo;
|
|
405 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
|
|
406 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
|
|
407 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
|
|
408 hi = clr >> 8;
|
|
409 lo = clr & 0xFF;
|
|
410 for(i = 0; i < 4; i++) {
|
|
411 if(map & 1) out[0] = hi; else out[0] = lo;
|
|
412 if(map & 2) out[1] = hi; else out[1] = lo;
|
|
413 if(map & 4) out[2] = hi; else out[2] = lo;
|
|
414 if(map & 8) out[3] = hi; else out[3] = lo;
|
|
415 map >>= 4;
|
|
416 out += stride;
|
|
417 }
|
|
418 blk++;
|
|
419 }
|
|
420 break;
|
|
421 case SMK_BLK_FULL:
|
|
422 mode = 0;
|
|
423 if(avctx->codec_tag != 0) { // In case of Smacker v4 we have three modes
|
|
424 if(get_bits1(&gb)) mode = 1;
|
|
425 else if(get_bits1(&gb)) mode = 2;
|
|
426 }
|
|
427 while(run-- && blk < blocks){
|
|
428 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
|
|
429 switch(mode){
|
|
430 case 0:
|
|
431 for(i = 0; i < 4; i++) {
|
|
432 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
|
|
433 out[2] = pix & 0xFF;
|
|
434 out[3] = pix >> 8;
|
|
435 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
|
|
436 out[0] = pix & 0xFF;
|
|
437 out[1] = pix >> 8;
|
|
438 out += stride;
|
|
439 }
|
|
440 break;
|
|
441 case 1:
|
|
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;
|
|
445 out += stride;
|
|
446 out[0] = out[1] = pix & 0xFF;
|
|
447 out[2] = out[3] = pix >> 8;
|
|
448 out += stride;
|
|
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;
|
|
452 out += stride;
|
|
453 out[0] = out[1] = pix & 0xFF;
|
|
454 out[2] = out[3] = pix >> 8;
|
|
455 out += stride;
|
|
456 break;
|
|
457 case 2:
|
|
458 for(i = 0; i < 2; i++) {
|
|
459 uint16_t pix1, pix2;
|
|
460 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
|
|
461 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
|
|
462 out[0] = pix1 & 0xFF; out[1] = pix1 >> 8;
|
|
463 out[2] = pix2 & 0xFF; out[3] = pix2 >> 8;
|
|
464 out += stride;
|
|
465 out[0] = pix1 & 0xFF; out[1] = pix1 >> 8;
|
|
466 out[2] = pix2 & 0xFF; out[3] = pix2 >> 8;
|
|
467 out += stride;
|
|
468 }
|
|
469 break;
|
|
470 }
|
|
471 blk++;
|
|
472 }
|
|
473 break;
|
|
474 case SMK_BLK_SKIP:
|
|
475 while(run-- && blk < blocks)
|
|
476 blk++;
|
|
477 break;
|
|
478 case SMK_BLK_FILL:
|
|
479 mode = type >> 8;
|
|
480 while(run-- && blk < blocks){
|
|
481 uint32_t col;
|
|
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;
|
|
486 out += stride;
|
|
487 }
|
|
488 blk++;
|
|
489 }
|
|
490 break;
|
|
491 }
|
|
492
|
|
493 }
|
|
494
|
|
495 *data_size = sizeof(AVFrame);
|
|
496 *(AVFrame*)data = smk->pic;
|
|
497
|
|
498 /* always report that the buffer was completely consumed */
|
|
499 return buf_size;
|
|
500 }
|
|
501
|
|
502
|
|
503
|
|
504 /*
|
|
505 *
|
|
506 * Init smacker decoder
|
|
507 *
|
|
508 */
|
|
509 static int decode_init(AVCodecContext *avctx)
|
|
510 {
|
|
511 SmackVContext * const c = (SmackVContext *)avctx->priv_data;
|
|
512
|
|
513 c->avctx = avctx;
|
|
514 avctx->has_b_frames = 0;
|
|
515
|
|
516 c->pic.data[0] = NULL;
|
|
517
|
|
518 if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
|
|
519 return 1;
|
|
520 }
|
|
521
|
|
522 avctx->pix_fmt = PIX_FMT_PAL8;
|
|
523
|
|
524
|
|
525 /* decode huffman trees from extradata */
|
|
526 if(avctx->extradata_size < 16){
|
|
527 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
|
|
528 return -1;
|
|
529 }
|
|
530
|
|
531 decode_header_trees(c);
|
|
532
|
|
533
|
|
534 return 0;
|
|
535 }
|
|
536
|
|
537
|
|
538
|
|
539 /*
|
|
540 *
|
|
541 * Uninit smacker decoder
|
|
542 *
|
|
543 */
|
|
544 static int decode_end(AVCodecContext *avctx)
|
|
545 {
|
|
546 SmackVContext * const smk = (SmackVContext *)avctx->priv_data;
|
|
547
|
|
548 if(smk->mmap_tbl)
|
|
549 av_free(smk->mmap_tbl);
|
|
550 if(smk->mclr_tbl)
|
|
551 av_free(smk->mclr_tbl);
|
|
552 if(smk->full_tbl)
|
|
553 av_free(smk->full_tbl);
|
|
554 if(smk->type_tbl)
|
|
555 av_free(smk->type_tbl);
|
|
556
|
|
557 if (smk->pic.data[0])
|
|
558 avctx->release_buffer(avctx, &smk->pic);
|
|
559
|
|
560 return 0;
|
|
561 }
|
|
562
|
|
563
|
|
564 static int smka_decode_init(AVCodecContext *avctx)
|
|
565 {
|
|
566 return 0;
|
|
567 }
|
|
568
|
|
569 /**
|
|
570 * Decode Smacker audio data
|
|
571 */
|
|
572 static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
|
|
573 {
|
|
574 GetBitContext gb;
|
|
575 HuffContext h[4];
|
|
576 VLC vlc[4];
|
|
577 int16_t *samples = data;
|
|
578 int val;
|
|
579 int i, res;
|
|
580 int unp_size;
|
|
581 int bits, stereo;
|
|
582 int pred[2] = {0, 0};
|
|
583
|
|
584 unp_size = LE_32(buf);
|
|
585
|
|
586 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
|
|
587
|
|
588 if(!get_bits1(&gb)){
|
|
589 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
|
|
590 *data_size = 0;
|
|
591 return 1;
|
|
592 }
|
|
593 stereo = get_bits1(&gb);
|
|
594 bits = get_bits1(&gb);
|
|
595
|
|
596 memset(vlc, 0, sizeof(VLC) * 4);
|
|
597 memset(h, 0, sizeof(HuffContext) * 4);
|
|
598 // Initialize
|
|
599 for(i = 0; i < (1 << (bits + stereo)); i++) {
|
|
600 h[i].length = 256;
|
|
601 h[i].maxlength = 0;
|
|
602 h[i].current = 0;
|
|
603 h[i].bits = av_mallocz(256 * 4);
|
|
604 h[i].lengths = av_mallocz(256 * sizeof(int));
|
|
605 h[i].values = av_mallocz(256 * sizeof(int));
|
|
606 get_bits1(&gb);
|
|
607 smacker_decode_tree(&gb, &h[i], 0, 0);
|
|
608 get_bits1(&gb);
|
|
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);
|
|
612 if(res < 0) {
|
|
613 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
|
|
614 return -1;
|
|
615 }
|
|
616 }
|
|
617 if(bits) { //decode 16-bit data
|
|
618 pred[0] = get_bits(&gb, 8);
|
|
619 pred[0] |= get_bits(&gb, 8);
|
|
620 if(stereo) {
|
|
621 pred[1] = get_bits(&gb, 8);
|
|
622 pred[1] |= get_bits(&gb, 8);
|
|
623 }
|
|
624 for(i = 0; i < unp_size / 2; i++) {
|
|
625 if(i & stereo) {
|
|
626 val = h[2].values[get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3)];
|
|
627 val |= (int8_t)h[3].values[get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3)] << 8;
|
|
628 pred[1] += val;
|
|
629 *samples++ = pred[1];
|
|
630 } else {
|
|
631 val = h[0].values[get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3)];
|
|
632 val |= (int8_t)h[1].values[get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3)] << 8;
|
|
633 pred[0] += val;
|
|
634 *samples++ = pred[0];
|
|
635 }
|
|
636 }
|
|
637 } else { //8-bit data
|
|
638 pred[0] = get_bits(&gb, 8);
|
|
639 if(stereo)
|
|
640 pred[1] = get_bits(&gb, 8);
|
|
641 for(i = 0; i < unp_size; i++) {
|
|
642 if(i & stereo){
|
|
643 pred[1] += (int8_t)h[1].values[get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3)];
|
|
644 *samples++ = (pred[1] - 0x80) << 8;
|
|
645 } else {
|
|
646 pred[0] += (int8_t)h[0].values[get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3)];
|
|
647 *samples++ = (pred[0] - 0x80) << 8;
|
|
648 }
|
|
649 }
|
|
650 unp_size *= 2;
|
|
651 }
|
|
652
|
|
653 for(i = 0; i < 4; i++) {
|
|
654 if(vlc[i].table)
|
|
655 free_vlc(&vlc[i]);
|
|
656 if(h[i].bits)
|
|
657 av_free(h[i].bits);
|
|
658 if(h[i].lengths)
|
|
659 av_free(h[i].lengths);
|
|
660 if(h[i].values)
|
|
661 av_free(h[i].values);
|
|
662 }
|
|
663
|
|
664 *data_size = unp_size;
|
|
665 return buf_size;
|
|
666 }
|
|
667
|
|
668 AVCodec smacker_decoder = {
|
|
669 "smackvid",
|
|
670 CODEC_TYPE_VIDEO,
|
|
671 CODEC_ID_SMACKVIDEO,
|
|
672 sizeof(SmackVContext),
|
|
673 decode_init,
|
|
674 NULL,
|
|
675 decode_end,
|
|
676 decode_frame
|
|
677 };
|
|
678
|
|
679 AVCodec smackaud_decoder = {
|
|
680 "smackaud",
|
|
681 CODEC_TYPE_AUDIO,
|
|
682 CODEC_ID_SMACKAUDIO,
|
|
683 0,
|
|
684 smka_decode_init,
|
|
685 NULL,
|
|
686 NULL,
|
|
687 smka_decode_frame
|
|
688 };
|
|
689
|