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