cosmetics: Drop particularly redundant silly comments
[libav.git] / libavcodec / smacker.c
CommitLineData
348efc18
KS
1/*
2 * Smacker decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
348efc18
KS
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
348efc18 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
348efc18
KS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
348efc18 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
348efc18
KS
20 */
21
22/**
ba87f080 23 * @file
348efc18
KS
24 * Smacker decoder
25 */
26
27/*
28 * Based on http://wiki.multimedia.cx/index.php?title=Smacker
29 */
30
31#include <stdio.h>
32#include <stdlib.h>
33
a903f8f0 34#include "libavutil/channel_layout.h"
348efc18 35#include "avcodec.h"
594d4d5d 36#include "internal.h"
c86c3c80 37#include "mathops.h"
348efc18 38
aaf47bcd 39#define BITSTREAM_READER_LE
9106a698 40#include "get_bits.h"
3e368d72 41#include "bytestream.h"
348efc18
KS
42
43#define SMKTREE_BITS 9
44#define SMK_NODE 0x80000000
45
6b96d2dc 46
348efc18
KS
47typedef struct SmackVContext {
48 AVCodecContext *avctx;
84099f51 49 AVFrame *pic;
348efc18
KS
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 */
58typedef 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 */
68typedef 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 */
77static 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
87enum 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 */
96static 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 */
128static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
129{
9adf25c1
AC
130 if (hc->current + 1 >= hc->length) {
131 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
132 return -1;
133 }
348efc18 134 if(!get_bits1(gb)){ //Leaf
779f8bc2 135 int val, i1, i2;
9676ffba 136 i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
9676ffba 137 i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
64894554
LA
138 if (i1 < 0 || i2 < 0)
139 return -1;
348efc18
KS
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
9adf25c1 155 int r = 0, r_new, t;
348efc18
KS
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++;
9adf25c1
AC
163 r_new = smacker_decode_bigtree(gb, hc, ctx);
164 if (r_new < 0)
165 return r_new;
166 return r + r_new;
348efc18
KS
167 }
168}
169
170/**
2912e87a 171 * Store large tree as Libav's vlc codes
348efc18
KS
172 */
173static 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;
a92be9b8 178 VLC vlc[2] = { { 0 } };
348efc18
KS
179 int escapes[3];
180 DBCtx ctx;
9adf25c1 181 int err = 0;
348efc18 182
3a1a7e32
MN
183 if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
184 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
185 return -1;
186 }
187
348efc18
KS
188 tmp1.length = 256;
189 tmp1.maxlength = 0;
190 tmp1.current = 0;
191 tmp1.bits = av_mallocz(256 * 4);
192 tmp1.lengths = av_mallocz(256 * sizeof(int));
193 tmp1.values = av_mallocz(256 * sizeof(int));
194
195 tmp2.length = 256;
196 tmp2.maxlength = 0;
197 tmp2.current = 0;
198 tmp2.bits = av_mallocz(256 * 4);
199 tmp2.lengths = av_mallocz(256 * sizeof(int));
200 tmp2.values = av_mallocz(256 * sizeof(int));
d002fce2
MS
201 if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
202 !tmp2.bits || !tmp2.lengths || !tmp2.values) {
203 err = AVERROR(ENOMEM);
204 goto error;
205 }
348efc18 206
348efc18
KS
207 if(get_bits1(gb)) {
208 smacker_decode_tree(gb, &tmp1, 0, 0);
7ae7300e 209 skip_bits1(gb);
348efc18
KS
210 res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
211 tmp1.lengths, sizeof(int), sizeof(int),
212 tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
213 if(res < 0) {
214 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
d002fce2
MS
215 err = res;
216 goto error;
348efc18
KS
217 }
218 } else {
219 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
220 }
221 if(get_bits1(gb)){
222 smacker_decode_tree(gb, &tmp2, 0, 0);
7ae7300e 223 skip_bits1(gb);
348efc18
KS
224 res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
225 tmp2.lengths, sizeof(int), sizeof(int),
226 tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
227 if(res < 0) {
228 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
d002fce2
MS
229 err = res;
230 goto error;
348efc18
KS
231 }
232 } else {
233 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
234 }
235
236 escapes[0] = get_bits(gb, 8);
237 escapes[0] |= get_bits(gb, 8) << 8;
238 escapes[1] = get_bits(gb, 8);
239 escapes[1] |= get_bits(gb, 8) << 8;
240 escapes[2] = get_bits(gb, 8);
241 escapes[2] |= get_bits(gb, 8) << 8;
242
243 last[0] = last[1] = last[2] = -1;
244
245 ctx.escapes[0] = escapes[0];
246 ctx.escapes[1] = escapes[1];
247 ctx.escapes[2] = escapes[2];
248 ctx.v1 = &vlc[0];
249 ctx.v2 = &vlc[1];
250 ctx.recode1 = tmp1.values;
251 ctx.recode2 = tmp2.values;
252 ctx.last = last;
253
f52edef3 254 huff.length = ((size + 3) >> 2) + 4;
348efc18
KS
255 huff.maxlength = 0;
256 huff.current = 0;
257 huff.values = av_mallocz(huff.length * sizeof(int));
d002fce2
MS
258 if (!huff.values) {
259 err = AVERROR(ENOMEM);
260 goto error;
261 }
348efc18 262
9adf25c1
AC
263 if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
264 err = -1;
7ae7300e 265 skip_bits1(gb);
348efc18
KS
266 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
267 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
268 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
0679cec6
MS
269 if (ctx.last[0] >= huff.length ||
270 ctx.last[1] >= huff.length ||
271 ctx.last[2] >= huff.length) {
272 av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
273 err = AVERROR_INVALIDDATA;
274 }
348efc18
KS
275
276 *recodes = huff.values;
277
d002fce2 278error:
348efc18 279 if(vlc[0].table)
e96b4a53 280 ff_free_vlc(&vlc[0]);
348efc18 281 if(vlc[1].table)
e96b4a53 282 ff_free_vlc(&vlc[1]);
348efc18
KS
283 av_free(tmp1.bits);
284 av_free(tmp1.lengths);
285 av_free(tmp1.values);
286 av_free(tmp2.bits);
287 av_free(tmp2.lengths);
288 av_free(tmp2.values);
289
9adf25c1 290 return err;
348efc18
KS
291}
292
293static int decode_header_trees(SmackVContext *smk) {
294 GetBitContext gb;
295 int mmap_size, mclr_size, full_size, type_size;
296
fead30d4
AB
297 mmap_size = AV_RL32(smk->avctx->extradata);
298 mclr_size = AV_RL32(smk->avctx->extradata + 4);
299 full_size = AV_RL32(smk->avctx->extradata + 8);
300 type_size = AV_RL32(smk->avctx->extradata + 12);
348efc18
KS
301
302 init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
303
304 if(!get_bits1(&gb)) {
305 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
306 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
d002fce2
MS
307 if (!smk->mmap_tbl)
308 return AVERROR(ENOMEM);
348efc18
KS
309 smk->mmap_tbl[0] = 0;
310 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
311 } else {
9676ffba
LA
312 if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
313 return -1;
348efc18 314 }
5fc32c27 315 if(!get_bits1(&gb)) {
348efc18
KS
316 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
317 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
d002fce2
MS
318 if (!smk->mclr_tbl)
319 return AVERROR(ENOMEM);
348efc18
KS
320 smk->mclr_tbl[0] = 0;
321 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
322 } else {
9676ffba
LA
323 if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
324 return -1;
348efc18 325 }
5fc32c27 326 if(!get_bits1(&gb)) {
348efc18
KS
327 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
328 smk->full_tbl = av_malloc(sizeof(int) * 2);
d002fce2
MS
329 if (!smk->full_tbl)
330 return AVERROR(ENOMEM);
348efc18
KS
331 smk->full_tbl[0] = 0;
332 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
333 } else {
9676ffba
LA
334 if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
335 return -1;
348efc18 336 }
5fc32c27 337 if(!get_bits1(&gb)) {
348efc18
KS
338 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
339 smk->type_tbl = av_malloc(sizeof(int) * 2);
d002fce2
MS
340 if (!smk->type_tbl)
341 return AVERROR(ENOMEM);
348efc18
KS
342 smk->type_tbl[0] = 0;
343 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
344 } else {
9676ffba
LA
345 if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
346 return -1;
348efc18
KS
347 }
348
349 return 0;
350}
351
849f1035 352static av_always_inline void last_reset(int *recode, int *last) {
348efc18
KS
353 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
354}
355
356/* get code and update history */
849f1035 357static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
348efc18 358 register int *table = recode;
90c0c83e 359 int v;
348efc18 360
348efc18
KS
361 while(*table & SMK_NODE) {
362 if(get_bits1(gb))
363 table += (*table) & (~SMK_NODE);
364 table++;
365 }
366 v = *table;
348efc18
KS
367
368 if(v != recode[last[0]]) {
369 recode[last[2]] = recode[last[1]];
370 recode[last[1]] = recode[last[0]];
371 recode[last[0]] = v;
372 }
373 return v;
374}
375
df9b9567
AK
376static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
377 AVPacket *avpkt)
348efc18 378{
e4141433 379 SmackVContext * const smk = avctx->priv_data;
348efc18
KS
380 uint8_t *out;
381 uint32_t *pal;
b2af057a 382 GetByteContext gb2;
348efc18
KS
383 GetBitContext gb;
384 int blocks, blk, bw, bh;
759001c5 385 int i, ret;
348efc18 386 int stride;
b2af057a 387 int flags;
348efc18 388
b2af057a 389 if (avpkt->size <= 769)
348efc18 390 return 0;
348efc18 391
84099f51 392 if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0) {
348efc18 393 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
759001c5 394 return ret;
348efc18
KS
395 }
396
397 /* make the palette available on the way out */
84099f51 398 pal = (uint32_t*)smk->pic->data[1];
b2af057a
RB
399 bytestream2_init(&gb2, avpkt->data, avpkt->size);
400 flags = bytestream2_get_byteu(&gb2);
84099f51
AK
401 smk->pic->palette_has_changed = flags & 1;
402 smk->pic->key_frame = !!(flags & 2);
403 if(smk->pic->key_frame)
404 smk->pic->pict_type = AV_PICTURE_TYPE_I;
348efc18 405 else
84099f51 406 smk->pic->pict_type = AV_PICTURE_TYPE_P;
348efc18 407
3e368d72 408 for(i = 0; i < 256; i++)
b2af057a 409 *pal++ = bytestream2_get_be24u(&gb2);
348efc18
KS
410
411 last_reset(smk->mmap_tbl, smk->mmap_last);
412 last_reset(smk->mclr_tbl, smk->mclr_last);
413 last_reset(smk->full_tbl, smk->full_last);
414 last_reset(smk->type_tbl, smk->type_last);
b2af057a 415 init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
348efc18
KS
416
417 blk = 0;
418 bw = avctx->width >> 2;
419 bh = avctx->height >> 2;
420 blocks = bw * bh;
84099f51
AK
421 out = smk->pic->data[0];
422 stride = smk->pic->linesize[0];
348efc18
KS
423 while(blk < blocks) {
424 int type, run, mode;
425 uint16_t pix;
426
427 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
428 run = block_runs[(type >> 2) & 0x3F];
429 switch(type & 3){
430 case SMK_BLK_MONO:
431 while(run-- && blk < blocks){
432 int clr, map;
433 int hi, lo;
434 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
435 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
84099f51 436 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
348efc18
KS
437 hi = clr >> 8;
438 lo = clr & 0xFF;
439 for(i = 0; i < 4; i++) {
440 if(map & 1) out[0] = hi; else out[0] = lo;
441 if(map & 2) out[1] = hi; else out[1] = lo;
442 if(map & 4) out[2] = hi; else out[2] = lo;
443 if(map & 8) out[3] = hi; else out[3] = lo;
444 map >>= 4;
445 out += stride;
446 }
447 blk++;
448 }
449 break;
450 case SMK_BLK_FULL:
451 mode = 0;
103eee53 452 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
348efc18
KS
453 if(get_bits1(&gb)) mode = 1;
454 else if(get_bits1(&gb)) mode = 2;
455 }
456 while(run-- && blk < blocks){
84099f51 457 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
348efc18
KS
458 switch(mode){
459 case 0:
460 for(i = 0; i < 4; i++) {
461 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
2c124cb6 462 AV_WL16(out+2,pix);
348efc18 463 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
2c124cb6 464 AV_WL16(out,pix);
348efc18
KS
465 out += stride;
466 }
467 break;
468 case 1:
469 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
470 out[0] = out[1] = pix & 0xFF;
471 out[2] = out[3] = pix >> 8;
472 out += stride;
473 out[0] = out[1] = pix & 0xFF;
474 out[2] = out[3] = pix >> 8;
475 out += stride;
476 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
477 out[0] = out[1] = pix & 0xFF;
478 out[2] = out[3] = pix >> 8;
479 out += stride;
480 out[0] = out[1] = pix & 0xFF;
481 out[2] = out[3] = pix >> 8;
482 out += stride;
483 break;
484 case 2:
485 for(i = 0; i < 2; i++) {
486 uint16_t pix1, pix2;
348efc18 487 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
40a19c44 488 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
2c124cb6
RP
489 AV_WL16(out,pix1);
490 AV_WL16(out+2,pix2);
348efc18 491 out += stride;
2c124cb6
RP
492 AV_WL16(out,pix1);
493 AV_WL16(out+2,pix2);
348efc18
KS
494 out += stride;
495 }
496 break;
497 }
498 blk++;
499 }
500 break;
501 case SMK_BLK_SKIP:
502 while(run-- && blk < blocks)
503 blk++;
504 break;
505 case SMK_BLK_FILL:
506 mode = type >> 8;
507 while(run-- && blk < blocks){
508 uint32_t col;
84099f51 509 out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
348efc18
KS
510 col = mode * 0x01010101;
511 for(i = 0; i < 4; i++) {
512 *((uint32_t*)out) = col;
513 out += stride;
514 }
515 blk++;
516 }
517 break;
518 }
519
520 }
521
84099f51 522 if ((ret = av_frame_ref(data, smk->pic)) < 0)
759001c5
AK
523 return ret;
524
df9b9567 525 *got_frame = 1;
348efc18
KS
526
527 /* always report that the buffer was completely consumed */
b2af057a 528 return avpkt->size;
348efc18
KS
529}
530
531
f3d57dc6
MS
532static av_cold int decode_end(AVCodecContext *avctx)
533{
534 SmackVContext * const smk = avctx->priv_data;
535
536 av_freep(&smk->mmap_tbl);
537 av_freep(&smk->mclr_tbl);
538 av_freep(&smk->full_tbl);
539 av_freep(&smk->type_tbl);
540
84099f51 541 av_frame_free(&smk->pic);
f3d57dc6
MS
542
543 return 0;
544}
545
546
98a6fff9 547static av_cold int decode_init(AVCodecContext *avctx)
348efc18 548{
e4141433 549 SmackVContext * const c = avctx->priv_data;
348efc18
KS
550
551 c->avctx = avctx;
348efc18 552
716d413c 553 avctx->pix_fmt = AV_PIX_FMT_PAL8;
84099f51
AK
554
555 c->pic = av_frame_alloc();
556 if (!c->pic)
557 return AVERROR(ENOMEM);
348efc18
KS
558
559 /* decode huffman trees from extradata */
560 if(avctx->extradata_size < 16){
561 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
562 return -1;
563 }
564
f3d57dc6
MS
565 if (decode_header_trees(c)) {
566 decode_end(avctx);
9676ffba 567 return -1;
f3d57dc6 568 }
348efc18
KS
569
570 return 0;
571}
572
573
574
98a6fff9 575static av_cold int smka_decode_init(AVCodecContext *avctx)
348efc18 576{
e190e453
JR
577 if (avctx->channels < 1 || avctx->channels > 2) {
578 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
579 return AVERROR(EINVAL);
580 }
c2fcd0a7 581 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
5d6e4c16 582 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
0eea2129 583
348efc18
KS
584 return 0;
585}
586
587/**
588 * Decode Smacker audio data
589 */
0eea2129
JR
590static int smka_decode_frame(AVCodecContext *avctx, void *data,
591 int *got_frame_ptr, AVPacket *avpkt)
348efc18 592{
903b62cc 593 AVFrame *frame = data;
7a00bbad
TB
594 const uint8_t *buf = avpkt->data;
595 int buf_size = avpkt->size;
348efc18 596 GetBitContext gb;
a92be9b8
DB
597 HuffContext h[4] = { { 0 } };
598 VLC vlc[4] = { { 0 } };
0eea2129
JR
599 int16_t *samples;
600 uint8_t *samples8;
348efc18 601 int val;
0eea2129 602 int i, res, ret;
348efc18
KS
603 int unp_size;
604 int bits, stereo;
605 int pred[2] = {0, 0};
606
cf044f8b
JR
607 if (buf_size <= 4) {
608 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
609 return AVERROR(EINVAL);
610 }
611
fead30d4 612 unp_size = AV_RL32(buf);
348efc18
KS
613
614 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
615
616 if(!get_bits1(&gb)){
617 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
0eea2129 618 *got_frame_ptr = 0;
348efc18
KS
619 return 1;
620 }
621 stereo = get_bits1(&gb);
622 bits = get_bits1(&gb);
ff1f89de
JR
623 if (stereo ^ (avctx->channels != 1)) {
624 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
625 return AVERROR(EINVAL);
626 }
627 if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
628 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
629 return AVERROR(EINVAL);
630 }
348efc18 631
0eea2129 632 /* get output buffer */
903b62cc 633 frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
759001c5 634 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
0eea2129
JR
635 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
636 return ret;
637 }
903b62cc
JR
638 samples = (int16_t *)frame->data[0];
639 samples8 = frame->data[0];
0eea2129 640
348efc18
KS
641 // Initialize
642 for(i = 0; i < (1 << (bits + stereo)); i++) {
643 h[i].length = 256;
644 h[i].maxlength = 0;
645 h[i].current = 0;
646 h[i].bits = av_mallocz(256 * 4);
647 h[i].lengths = av_mallocz(256 * sizeof(int));
648 h[i].values = av_mallocz(256 * sizeof(int));
d002fce2
MS
649 if (!h[i].bits || !h[i].lengths || !h[i].values) {
650 ret = AVERROR(ENOMEM);
651 goto error;
652 }
7ae7300e 653 skip_bits1(&gb);
bbb2945f 654 if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
d002fce2
MS
655 ret = AVERROR_INVALIDDATA;
656 goto error;
bbb2945f 657 }
7ae7300e 658 skip_bits1(&gb);
76fabb45
KS
659 if(h[i].current > 1) {
660 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
348efc18
KS
661 h[i].lengths, sizeof(int), sizeof(int),
662 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
76fabb45
KS
663 if(res < 0) {
664 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
d002fce2
MS
665 ret = AVERROR_INVALIDDATA;
666 goto error;
76fabb45 667 }
348efc18
KS
668 }
669 }
268bb885 670 /* this codec relies on wraparound instead of clipping audio */
348efc18 671 if(bits) { //decode 16-bit data
c72e05ba 672 for(i = stereo; i >= 0; i--)
12cbbbb4 673 pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
5166376f 674 for(i = 0; i <= stereo; i++)
c72e05ba 675 *samples++ = pred[i];
5166376f 676 for(; i < unp_size / 2; i++) {
348efc18 677 if(i & stereo) {
76fabb45
KS
678 if(vlc[2].table)
679 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
680 else
681 res = 0;
682 val = h[2].values[res];
683 if(vlc[3].table)
684 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
685 else
686 res = 0;
687 val |= h[3].values[res] << 8;
c86c3c80 688 pred[1] += sign_extend(val, 16);
5f30c6c8 689 *samples++ = pred[1];
348efc18 690 } else {
76fabb45
KS
691 if(vlc[0].table)
692 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
693 else
694 res = 0;
695 val = h[0].values[res];
696 if(vlc[1].table)
697 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
698 else
699 res = 0;
700 val |= h[1].values[res] << 8;
c86c3c80 701 pred[0] += sign_extend(val, 16);
5f30c6c8 702 *samples++ = pred[0];
348efc18
KS
703 }
704 }
705 } else { //8-bit data
c72e05ba
KS
706 for(i = stereo; i >= 0; i--)
707 pred[i] = get_bits(&gb, 8);
5166376f 708 for(i = 0; i <= stereo; i++)
e1dc16ae 709 *samples8++ = pred[i];
5166376f 710 for(; i < unp_size; i++) {
348efc18 711 if(i & stereo){
76fabb45
KS
712 if(vlc[1].table)
713 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
714 else
715 res = 0;
c86c3c80 716 pred[1] += sign_extend(h[1].values[res], 8);
5f30c6c8 717 *samples8++ = pred[1];
348efc18 718 } else {
76fabb45
KS
719 if(vlc[0].table)
720 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
721 else
722 res = 0;
c86c3c80 723 pred[0] += sign_extend(h[0].values[res], 8);
5f30c6c8 724 *samples8++ = pred[0];
348efc18
KS
725 }
726 }
348efc18
KS
727 }
728
d002fce2
MS
729 *got_frame_ptr = 1;
730 ret = buf_size;
731
732error:
348efc18
KS
733 for(i = 0; i < 4; i++) {
734 if(vlc[i].table)
e96b4a53 735 ff_free_vlc(&vlc[i]);
437fb1c8
CB
736 av_free(h[i].bits);
737 av_free(h[i].lengths);
738 av_free(h[i].values);
348efc18
KS
739 }
740
d002fce2 741 return ret;
348efc18
KS
742}
743
d36beb3f 744AVCodec ff_smacker_decoder = {
ec6402b7 745 .name = "smackvid",
b2bed932 746 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
ec6402b7 747 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 748 .id = AV_CODEC_ID_SMACKVIDEO,
ec6402b7
AK
749 .priv_data_size = sizeof(SmackVContext),
750 .init = decode_init,
751 .close = decode_end,
752 .decode = decode_frame,
def97856 753 .capabilities = AV_CODEC_CAP_DR1,
348efc18
KS
754};
755
d36beb3f 756AVCodec ff_smackaud_decoder = {
ec6402b7 757 .name = "smackaud",
b2bed932 758 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
ec6402b7 759 .type = AVMEDIA_TYPE_AUDIO,
36ef5369 760 .id = AV_CODEC_ID_SMACKAUDIO,
ec6402b7
AK
761 .init = smka_decode_init,
762 .decode = smka_decode_frame,
def97856 763 .capabilities = AV_CODEC_CAP_DR1,
348efc18 764};