Mercurial > libavcodec.hg
annotate smacker.c @ 3995:b00c06477dff libavcodec
write cabac low and range variables as early as possible to prevent stalls from reading them before they where written, the P4 is said to disslike that alot, on P3 its 2% faster (START/STOP_TIMER over decode_residual)
author | michael |
---|---|
date | Wed, 11 Oct 2006 16:11:41 +0000 |
parents | c8c591fe26f8 |
children | d6f83e2f8804 |
rev | line source |
---|---|
3209 | 1 /* |
2 * Smacker decoder | |
3 * Copyright (c) 2006 Konstantin Shishkov | |
4 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3800
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3800
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3800
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
3209 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3800
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
3209 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3800
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
3209 | 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. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3800
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3209 | 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 * | |
21 */ | |
22 | |
23 /** | |
24 * @file smacker.c | |
25 * Smacker decoder | |
26 */ | |
27 | |
28 /* | |
29 * Based on http://wiki.multimedia.cx/index.php?title=Smacker | |
30 */ | |
31 | |
32 #include <stdio.h> | |
33 #include <stdlib.h> | |
34 | |
35 #include "common.h" | |
36 #include "avcodec.h" | |
37 | |
38 #define ALT_BITSTREAM_READER_LE | |
39 #include "bitstream.h" | |
40 | |
41 #define SMKTREE_BITS 9 | |
42 #define SMK_NODE 0x80000000 | |
43 | |
44 /* | |
45 * Decoder context | |
46 */ | |
47 typedef struct SmackVContext { | |
48 AVCodecContext *avctx; | |
49 AVFrame pic; | |
50 | |
51 int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl; | |
52 int mmap_last[3], mclr_last[3], full_last[3], type_last[3]; | |
53 } SmackVContext; | |
54 | |
55 /** | |
56 * Context used for code reconstructing | |
57 */ | |
58 typedef struct HuffContext { | |
59 int length; | |
60 int maxlength; | |
61 int current; | |
62 uint32_t *bits; | |
63 int *lengths; | |
64 int *values; | |
65 } HuffContext; | |
66 | |
67 /* common parameters used for decode_bigtree */ | |
68 typedef struct DBCtx { | |
69 VLC *v1, *v2; | |
70 int *recode1, *recode2; | |
71 int escapes[3]; | |
72 int *last; | |
73 int lcur; | |
74 } DBCtx; | |
75 | |
76 /* possible runs of blocks */ | |
77 static const int block_runs[64] = { | |
78 1, 2, 3, 4, 5, 6, 7, 8, | |
79 9, 10, 11, 12, 13, 14, 15, 16, | |
80 17, 18, 19, 20, 21, 22, 23, 24, | |
81 25, 26, 27, 28, 29, 30, 31, 32, | |
82 33, 34, 35, 36, 37, 38, 39, 40, | |
83 41, 42, 43, 44, 45, 46, 47, 48, | |
84 49, 50, 51, 52, 53, 54, 55, 56, | |
85 57, 58, 59, 128, 256, 512, 1024, 2048 }; | |
86 | |
87 enum SmkBlockTypes { | |
88 SMK_BLK_MONO = 0, | |
89 SMK_BLK_FULL = 1, | |
90 SMK_BLK_SKIP = 2, | |
91 SMK_BLK_FILL = 3 }; | |
92 | |
93 /** | |
94 * Decode local frame tree | |
95 */ | |
96 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length) | |
97 { | |
98 if(!get_bits1(gb)){ //Leaf | |
99 if(hc->current >= 256){ | |
100 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n"); | |
101 return -1; | |
102 } | |
103 if(length){ | |
104 hc->bits[hc->current] = prefix; | |
105 hc->lengths[hc->current] = length; | |
106 } else { | |
107 hc->bits[hc->current] = 0; | |
108 hc->lengths[hc->current] = 0; | |
109 } | |
110 hc->values[hc->current] = get_bits(gb, 8); | |
111 hc->current++; | |
112 if(hc->maxlength < length) | |
113 hc->maxlength = length; | |
114 return 0; | |
115 } else { //Node | |
116 int r; | |
117 length++; | |
118 r = smacker_decode_tree(gb, hc, prefix, length); | |
119 if(r) | |
120 return r; | |
121 return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length); | |
122 } | |
123 } | |
124 | |
125 /** | |
126 * Decode header tree | |
127 */ | |
128 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx) | |
129 { | |
130 if(!get_bits1(gb)){ //Leaf | |
131 int val, i1, i2, b1, b2; | |
132 if(hc->current >= hc->length){ | |
133 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n"); | |
134 return -1; | |
135 } | |
136 b1 = get_bits_count(gb); | |
137 i1 = get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3); | |
138 b1 = get_bits_count(gb) - b1; | |
139 b2 = get_bits_count(gb); | |
140 i2 = get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3); | |
141 b2 = get_bits_count(gb) - b2; | |
142 val = ctx->recode1[i1] | (ctx->recode2[i2] << 8); | |
143 if(val == ctx->escapes[0]) { | |
144 ctx->last[0] = hc->current; | |
145 val = 0; | |
146 } else if(val == ctx->escapes[1]) { | |
147 ctx->last[1] = hc->current; | |
148 val = 0; | |
149 } else if(val == ctx->escapes[2]) { | |
150 ctx->last[2] = hc->current; | |
151 val = 0; | |
152 } | |
153 | |
154 hc->values[hc->current++] = val; | |
155 return 1; | |
156 } else { //Node | |
157 int r = 0, t; | |
158 | |
159 t = hc->current++; | |
160 r = smacker_decode_bigtree(gb, hc, ctx); | |
161 if(r < 0) | |
162 return r; | |
163 hc->values[t] = SMK_NODE | r; | |
164 r++; | |
165 r += smacker_decode_bigtree(gb, hc, ctx); | |
166 return r; | |
167 } | |
168 } | |
169 | |
170 /** | |
171 * Store large tree as FFmpeg's vlc codes | |
172 */ | |
173 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size) | |
174 { | |
175 int res; | |
176 HuffContext huff; | |
177 HuffContext tmp1, tmp2; | |
178 VLC vlc[2]; | |
179 int escapes[3]; | |
180 DBCtx ctx; | |
181 | |
3303
68721b62a528
sanity checks, some might have been exploitable ...
michael
parents:
3220
diff
changeset
|
182 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow |
68721b62a528
sanity checks, some might have been exploitable ...
michael
parents:
3220
diff
changeset
|
183 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n"); |
68721b62a528
sanity checks, some might have been exploitable ...
michael
parents:
3220
diff
changeset
|
184 return -1; |
68721b62a528
sanity checks, some might have been exploitable ...
michael
parents:
3220
diff
changeset
|
185 } |
68721b62a528
sanity checks, some might have been exploitable ...
michael
parents:
3220
diff
changeset
|
186 |
3209 | 187 tmp1.length = 256; |
188 tmp1.maxlength = 0; | |
189 tmp1.current = 0; | |
190 tmp1.bits = av_mallocz(256 * 4); | |
191 tmp1.lengths = av_mallocz(256 * sizeof(int)); | |
192 tmp1.values = av_mallocz(256 * sizeof(int)); | |
193 | |
194 tmp2.length = 256; | |
195 tmp2.maxlength = 0; | |
196 tmp2.current = 0; | |
197 tmp2.bits = av_mallocz(256 * 4); | |
198 tmp2.lengths = av_mallocz(256 * sizeof(int)); | |
199 tmp2.values = av_mallocz(256 * sizeof(int)); | |
200 | |
201 memset(&vlc[0], 0, sizeof(VLC)); | |
202 memset(&vlc[1], 0, sizeof(VLC)); | |
203 | |
204 if(get_bits1(gb)) { | |
205 smacker_decode_tree(gb, &tmp1, 0, 0); | |
206 get_bits1(gb); | |
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); | |
210 if(res < 0) { | |
211 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); | |
212 return -1; | |
213 } | |
214 } else { | |
215 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n"); | |
216 } | |
217 if(get_bits1(gb)){ | |
218 smacker_decode_tree(gb, &tmp2, 0, 0); | |
219 get_bits1(gb); | |
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); | |
223 if(res < 0) { | |
224 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); | |
225 return -1; | |
226 } | |
227 } else { | |
228 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n"); | |
229 } | |
230 | |
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; | |
237 | |
238 last[0] = last[1] = last[2] = -1; | |
239 | |
240 ctx.escapes[0] = escapes[0]; | |
241 ctx.escapes[1] = escapes[1]; | |
242 ctx.escapes[2] = escapes[2]; | |
243 ctx.v1 = &vlc[0]; | |
244 ctx.v2 = &vlc[1]; | |
245 ctx.recode1 = tmp1.values; | |
246 ctx.recode2 = tmp2.values; | |
247 ctx.last = last; | |
248 | |
249 huff.length = ((size + 3) >> 2) + 3; | |
250 huff.maxlength = 0; | |
251 huff.current = 0; | |
252 huff.values = av_mallocz(huff.length * sizeof(int)); | |
253 | |
254 smacker_decode_bigtree(gb, &huff, &ctx); | |
255 get_bits1(gb); | |
256 if(ctx.last[0] == -1) ctx.last[0] = huff.current++; | |
257 if(ctx.last[1] == -1) ctx.last[1] = huff.current++; | |
258 if(ctx.last[2] == -1) ctx.last[2] = huff.current++; | |
259 | |
260 *recodes = huff.values; | |
261 | |
262 if(vlc[0].table) | |
263 free_vlc(&vlc[0]); | |
264 if(vlc[1].table) | |
265 free_vlc(&vlc[1]); | |
266 av_free(tmp1.bits); | |
267 av_free(tmp1.lengths); | |
268 av_free(tmp1.values); | |
269 av_free(tmp2.bits); | |
270 av_free(tmp2.lengths); | |
271 av_free(tmp2.values); | |
272 | |
273 return 0; | |
274 } | |
275 | |
276 static int decode_header_trees(SmackVContext *smk) { | |
277 GetBitContext gb; | |
278 int mmap_size, mclr_size, full_size, type_size; | |
279 | |
280 mmap_size = LE_32(smk->avctx->extradata); | |
281 mclr_size = LE_32(smk->avctx->extradata + 4); | |
282 full_size = LE_32(smk->avctx->extradata + 8); | |
283 type_size = LE_32(smk->avctx->extradata + 12); | |
284 | |
285 init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8); | |
286 | |
287 if(!get_bits1(&gb)) { | |
288 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n"); | |
289 smk->mmap_tbl = av_malloc(sizeof(int) * 2); | |
290 smk->mmap_tbl[0] = 0; | |
291 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1; | |
292 } else { | |
293 smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size); | |
294 } | |
295 if(!get_bits(&gb, 1)) { | |
296 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n"); | |
297 smk->mclr_tbl = av_malloc(sizeof(int) * 2); | |
298 smk->mclr_tbl[0] = 0; | |
299 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1; | |
300 } else { | |
301 smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size); | |
302 } | |
303 if(!get_bits(&gb, 1)) { | |
304 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n"); | |
305 smk->full_tbl = av_malloc(sizeof(int) * 2); | |
306 smk->full_tbl[0] = 0; | |
307 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1; | |
308 } else { | |
309 smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size); | |
310 } | |
311 if(!get_bits(&gb, 1)) { | |
312 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n"); | |
313 smk->type_tbl = av_malloc(sizeof(int) * 2); | |
314 smk->type_tbl[0] = 0; | |
315 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1; | |
316 } else { | |
317 smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size); | |
318 } | |
319 | |
320 return 0; | |
321 } | |
322 | |
323 static always_inline void last_reset(int *recode, int *last) { | |
324 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0; | |
325 } | |
326 | |
327 /* get code and update history */ | |
328 static always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) { | |
329 register int *table = recode; | |
330 int v, b; | |
331 | |
332 b = get_bits_count(gb); | |
333 while(*table & SMK_NODE) { | |
334 if(get_bits1(gb)) | |
335 table += (*table) & (~SMK_NODE); | |
336 table++; | |
337 } | |
338 v = *table; | |
339 b = get_bits_count(gb) - b; | |
340 | |
341 if(v != recode[last[0]]) { | |
342 recode[last[2]] = recode[last[1]]; | |
343 recode[last[1]] = recode[last[0]]; | |
344 recode[last[0]] = v; | |
345 } | |
346 return v; | |
347 } | |
348 | |
349 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size) | |
350 { | |
351 SmackVContext * const smk = (SmackVContext *)avctx->priv_data; | |
352 uint8_t *out; | |
353 uint32_t *pal; | |
354 GetBitContext gb; | |
355 int blocks, blk, bw, bh; | |
356 int i; | |
357 int stride; | |
358 | |
359 if(buf_size == 769) | |
360 return 0; | |
361 if(smk->pic.data[0]) | |
362 avctx->release_buffer(avctx, &smk->pic); | |
363 | |
364 smk->pic.reference = 1; | |
365 smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; | |
366 if(avctx->reget_buffer(avctx, &smk->pic) < 0){ | |
367 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
368 return -1; | |
369 } | |
370 | |
371 /* make the palette available on the way out */ | |
372 out = buf + 1; | |
373 pal = (uint32_t*)smk->pic.data[1]; | |
374 smk->pic.palette_has_changed = buf[0] & 1; | |
375 smk->pic.key_frame = !!(buf[0] & 2); | |
376 if(smk->pic.key_frame) | |
377 smk->pic.pict_type = FF_I_TYPE; | |
378 else | |
379 smk->pic.pict_type = FF_P_TYPE; | |
380 | |
381 for(i = 0; i < 256; i++) { | |
382 int r, g, b; | |
383 r = *out++; | |
384 g = *out++; | |
385 b = *out++; | |
386 *pal++ = (r << 16) | (g << 8) | b; | |
387 } | |
388 | |
389 last_reset(smk->mmap_tbl, smk->mmap_last); | |
390 last_reset(smk->mclr_tbl, smk->mclr_last); | |
391 last_reset(smk->full_tbl, smk->full_last); | |
392 last_reset(smk->type_tbl, smk->type_last); | |
393 init_get_bits(&gb, buf + 769, (buf_size - 769) * 8); | |
394 | |
395 blk = 0; | |
396 bw = avctx->width >> 2; | |
397 bh = avctx->height >> 2; | |
398 blocks = bw * bh; | |
399 out = smk->pic.data[0]; | |
400 stride = smk->pic.linesize[0]; | |
401 while(blk < blocks) { | |
402 int type, run, mode; | |
403 uint16_t pix; | |
404 | |
405 type = smk_get_code(&gb, smk->type_tbl, smk->type_last); | |
406 run = block_runs[(type >> 2) & 0x3F]; | |
407 switch(type & 3){ | |
408 case SMK_BLK_MONO: | |
409 while(run-- && blk < blocks){ | |
410 int clr, map; | |
411 int hi, lo; | |
412 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last); | |
413 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last); | |
414 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; | |
415 hi = clr >> 8; | |
416 lo = clr & 0xFF; | |
417 for(i = 0; i < 4; i++) { | |
418 if(map & 1) out[0] = hi; else out[0] = lo; | |
419 if(map & 2) out[1] = hi; else out[1] = lo; | |
420 if(map & 4) out[2] = hi; else out[2] = lo; | |
421 if(map & 8) out[3] = hi; else out[3] = lo; | |
422 map >>= 4; | |
423 out += stride; | |
424 } | |
425 blk++; | |
426 } | |
427 break; | |
428 case SMK_BLK_FULL: | |
429 mode = 0; | |
3310
48fc664f7348
Now MPlayer should understand Smacker audio and video codecs.
kostya
parents:
3303
diff
changeset
|
430 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes |
3209 | 431 if(get_bits1(&gb)) mode = 1; |
432 else if(get_bits1(&gb)) mode = 2; | |
433 } | |
434 while(run-- && blk < blocks){ | |
435 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; | |
436 switch(mode){ | |
437 case 0: | |
438 for(i = 0; i < 4; i++) { | |
439 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last); | |
440 out[2] = pix & 0xFF; | |
441 out[3] = pix >> 8; | |
442 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last); | |
443 out[0] = pix & 0xFF; | |
444 out[1] = pix >> 8; | |
445 out += stride; | |
446 } | |
447 break; | |
448 case 1: | |
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 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last); | |
457 out[0] = out[1] = pix & 0xFF; | |
458 out[2] = out[3] = pix >> 8; | |
459 out += stride; | |
460 out[0] = out[1] = pix & 0xFF; | |
461 out[2] = out[3] = pix >> 8; | |
462 out += stride; | |
463 break; | |
464 case 2: | |
465 for(i = 0; i < 2; i++) { | |
466 uint16_t pix1, pix2; | |
467 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last); | |
468 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last); | |
469 out[0] = pix1 & 0xFF; out[1] = pix1 >> 8; | |
470 out[2] = pix2 & 0xFF; out[3] = pix2 >> 8; | |
471 out += stride; | |
472 out[0] = pix1 & 0xFF; out[1] = pix1 >> 8; | |
473 out[2] = pix2 & 0xFF; out[3] = pix2 >> 8; | |
474 out += stride; | |
475 } | |
476 break; | |
477 } | |
478 blk++; | |
479 } | |
480 break; | |
481 case SMK_BLK_SKIP: | |
482 while(run-- && blk < blocks) | |
483 blk++; | |
484 break; | |
485 case SMK_BLK_FILL: | |
486 mode = type >> 8; | |
487 while(run-- && blk < blocks){ | |
488 uint32_t col; | |
489 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; | |
490 col = mode * 0x01010101; | |
491 for(i = 0; i < 4; i++) { | |
492 *((uint32_t*)out) = col; | |
493 out += stride; | |
494 } | |
495 blk++; | |
496 } | |
497 break; | |
498 } | |
499 | |
500 } | |
501 | |
502 *data_size = sizeof(AVFrame); | |
503 *(AVFrame*)data = smk->pic; | |
504 | |
505 /* always report that the buffer was completely consumed */ | |
506 return buf_size; | |
507 } | |
508 | |
509 | |
510 | |
511 /* | |
512 * | |
513 * Init smacker decoder | |
514 * | |
515 */ | |
516 static int decode_init(AVCodecContext *avctx) | |
517 { | |
518 SmackVContext * const c = (SmackVContext *)avctx->priv_data; | |
519 | |
520 c->avctx = avctx; | |
521 avctx->has_b_frames = 0; | |
522 | |
523 c->pic.data[0] = NULL; | |
524 | |
3800
9b75ab171fa9
1l: correct argument order in avcodec_check_dimensions
kostya
parents:
3694
diff
changeset
|
525 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) { |
3209 | 526 return 1; |
527 } | |
528 | |
529 avctx->pix_fmt = PIX_FMT_PAL8; | |
530 | |
531 | |
532 /* decode huffman trees from extradata */ | |
533 if(avctx->extradata_size < 16){ | |
534 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n"); | |
535 return -1; | |
536 } | |
537 | |
538 decode_header_trees(c); | |
539 | |
540 | |
541 return 0; | |
542 } | |
543 | |
544 | |
545 | |
546 /* | |
547 * | |
548 * Uninit smacker decoder | |
549 * | |
550 */ | |
551 static int decode_end(AVCodecContext *avctx) | |
552 { | |
553 SmackVContext * const smk = (SmackVContext *)avctx->priv_data; | |
554 | |
3694
8765ee4eaa45
Drop unneeded checks before av_free() and change to av_freep() where it's more suitable.
kostya
parents:
3310
diff
changeset
|
555 av_freep(&smk->mmap_tbl); |
8765ee4eaa45
Drop unneeded checks before av_free() and change to av_freep() where it's more suitable.
kostya
parents:
3310
diff
changeset
|
556 av_freep(&smk->mclr_tbl); |
8765ee4eaa45
Drop unneeded checks before av_free() and change to av_freep() where it's more suitable.
kostya
parents:
3310
diff
changeset
|
557 av_freep(&smk->full_tbl); |
8765ee4eaa45
Drop unneeded checks before av_free() and change to av_freep() where it's more suitable.
kostya
parents:
3310
diff
changeset
|
558 av_freep(&smk->type_tbl); |
3209 | 559 |
560 if (smk->pic.data[0]) | |
561 avctx->release_buffer(avctx, &smk->pic); | |
562 | |
563 return 0; | |
564 } | |
565 | |
566 | |
567 static int smka_decode_init(AVCodecContext *avctx) | |
568 { | |
569 return 0; | |
570 } | |
571 | |
572 /** | |
573 * Decode Smacker audio data | |
574 */ | |
575 static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size) | |
576 { | |
577 GetBitContext gb; | |
578 HuffContext h[4]; | |
579 VLC vlc[4]; | |
580 int16_t *samples = data; | |
581 int val; | |
582 int i, res; | |
583 int unp_size; | |
584 int bits, stereo; | |
585 int pred[2] = {0, 0}; | |
586 | |
587 unp_size = LE_32(buf); | |
588 | |
589 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8); | |
590 | |
591 if(!get_bits1(&gb)){ | |
592 av_log(avctx, AV_LOG_INFO, "Sound: no data\n"); | |
593 *data_size = 0; | |
594 return 1; | |
595 } | |
596 stereo = get_bits1(&gb); | |
597 bits = get_bits1(&gb); | |
598 | |
599 memset(vlc, 0, sizeof(VLC) * 4); | |
600 memset(h, 0, sizeof(HuffContext) * 4); | |
601 // Initialize | |
602 for(i = 0; i < (1 << (bits + stereo)); i++) { | |
603 h[i].length = 256; | |
604 h[i].maxlength = 0; | |
605 h[i].current = 0; | |
606 h[i].bits = av_mallocz(256 * 4); | |
607 h[i].lengths = av_mallocz(256 * sizeof(int)); | |
608 h[i].values = av_mallocz(256 * sizeof(int)); | |
609 get_bits1(&gb); | |
610 smacker_decode_tree(&gb, &h[i], 0, 0); | |
611 get_bits1(&gb); | |
3220
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
612 if(h[i].current > 1) { |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
613 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length, |
3209 | 614 h[i].lengths, sizeof(int), sizeof(int), |
615 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE); | |
3220
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
616 if(res < 0) { |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
617 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
618 return -1; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
619 } |
3209 | 620 } |
621 } | |
622 if(bits) { //decode 16-bit data | |
623 pred[0] = get_bits(&gb, 8); | |
624 pred[0] |= get_bits(&gb, 8); | |
3220
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
625 *samples++ = pred[0]; |
3209 | 626 if(stereo) { |
627 pred[1] = get_bits(&gb, 8); | |
628 pred[1] |= get_bits(&gb, 8); | |
3220
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
629 *samples++ = pred[1]; |
3209 | 630 } |
631 for(i = 0; i < unp_size / 2; i++) { | |
632 if(i & stereo) { | |
3220
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
633 if(vlc[2].table) |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
634 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3); |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
635 else |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
636 res = 0; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
637 val = h[2].values[res]; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
638 if(vlc[3].table) |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
639 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3); |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
640 else |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
641 res = 0; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
642 val |= h[3].values[res] << 8; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
643 pred[1] += (int16_t)val; |
3209 | 644 *samples++ = pred[1]; |
645 } else { | |
3220
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
646 if(vlc[0].table) |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
647 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3); |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
648 else |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
649 res = 0; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
650 val = h[0].values[res]; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
651 if(vlc[1].table) |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
652 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
653 else |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
654 res = 0; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
655 val |= h[1].values[res] << 8; |
3209 | 656 pred[0] += val; |
657 *samples++ = pred[0]; | |
658 } | |
659 } | |
660 } else { //8-bit data | |
661 pred[0] = get_bits(&gb, 8); | |
3220
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
662 *samples++ = (pred[0] - 0x80) << 8; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
663 if(stereo) { |
3209 | 664 pred[1] = get_bits(&gb, 8); |
3220
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
665 *samples++ = (pred[1] - 0x80) << 8; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
666 } |
3209 | 667 for(i = 0; i < unp_size; i++) { |
668 if(i & stereo){ | |
3220
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
669 if(vlc[1].table) |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
670 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
671 else |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
672 res = 0; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
673 pred[1] += (int8_t)h[1].values[res]; |
3209 | 674 *samples++ = (pred[1] - 0x80) << 8; |
675 } else { | |
3220
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
676 if(vlc[0].table) |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
677 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3); |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
678 else |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
679 res = 0; |
a931984ec6ab
Don't use get_vlc2() when tree is one symbol. This fixes audio decoding
kostya
parents:
3209
diff
changeset
|
680 pred[0] += (int8_t)h[0].values[res]; |
3209 | 681 *samples++ = (pred[0] - 0x80) << 8; |
682 } | |
683 } | |
684 unp_size *= 2; | |
685 } | |
686 | |
687 for(i = 0; i < 4; i++) { | |
688 if(vlc[i].table) | |
689 free_vlc(&vlc[i]); | |
690 if(h[i].bits) | |
691 av_free(h[i].bits); | |
692 if(h[i].lengths) | |
693 av_free(h[i].lengths); | |
694 if(h[i].values) | |
695 av_free(h[i].values); | |
696 } | |
697 | |
698 *data_size = unp_size; | |
699 return buf_size; | |
700 } | |
701 | |
702 AVCodec smacker_decoder = { | |
703 "smackvid", | |
704 CODEC_TYPE_VIDEO, | |
705 CODEC_ID_SMACKVIDEO, | |
706 sizeof(SmackVContext), | |
707 decode_init, | |
708 NULL, | |
709 decode_end, | |
710 decode_frame | |
711 }; | |
712 | |
713 AVCodec smackaud_decoder = { | |
714 "smackaud", | |
715 CODEC_TYPE_AUDIO, | |
716 CODEC_ID_SMACKAUDIO, | |
717 0, | |
718 smka_decode_init, | |
719 NULL, | |
720 NULL, | |
721 smka_decode_frame | |
722 }; | |
723 |