00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027
00028
00029
00030
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033
00034 #include "avcodec.h"
00035 #include "libavutil/audioconvert.h"
00036 #include "mathops.h"
00037
00038 #define BITSTREAM_READER_LE
00039 #include "get_bits.h"
00040 #include "bytestream.h"
00041
00042 #define SMKTREE_BITS 9
00043 #define SMK_NODE 0x80000000
00044
00045
00046
00047
00048 typedef struct SmackVContext {
00049 AVCodecContext *avctx;
00050 AVFrame pic;
00051
00052 int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
00053 int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
00054 } SmackVContext;
00055
00059 typedef struct HuffContext {
00060 int length;
00061 int maxlength;
00062 int current;
00063 uint32_t *bits;
00064 int *lengths;
00065 int *values;
00066 } HuffContext;
00067
00068
00069 typedef struct DBCtx {
00070 VLC *v1, *v2;
00071 int *recode1, *recode2;
00072 int escapes[3];
00073 int *last;
00074 int lcur;
00075 } DBCtx;
00076
00077
00078 static const int block_runs[64] = {
00079 1, 2, 3, 4, 5, 6, 7, 8,
00080 9, 10, 11, 12, 13, 14, 15, 16,
00081 17, 18, 19, 20, 21, 22, 23, 24,
00082 25, 26, 27, 28, 29, 30, 31, 32,
00083 33, 34, 35, 36, 37, 38, 39, 40,
00084 41, 42, 43, 44, 45, 46, 47, 48,
00085 49, 50, 51, 52, 53, 54, 55, 56,
00086 57, 58, 59, 128, 256, 512, 1024, 2048 };
00087
00088 enum SmkBlockTypes {
00089 SMK_BLK_MONO = 0,
00090 SMK_BLK_FULL = 1,
00091 SMK_BLK_SKIP = 2,
00092 SMK_BLK_FILL = 3 };
00093
00097 static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
00098 {
00099 if(!get_bits1(gb)){
00100 if(hc->current >= 256){
00101 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
00102 return -1;
00103 }
00104 if(length){
00105 hc->bits[hc->current] = prefix;
00106 hc->lengths[hc->current] = length;
00107 } else {
00108 hc->bits[hc->current] = 0;
00109 hc->lengths[hc->current] = 0;
00110 }
00111 hc->values[hc->current] = get_bits(gb, 8);
00112 hc->current++;
00113 if(hc->maxlength < length)
00114 hc->maxlength = length;
00115 return 0;
00116 } else {
00117 int r;
00118 length++;
00119 r = smacker_decode_tree(gb, hc, prefix, length);
00120 if(r)
00121 return r;
00122 return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
00123 }
00124 }
00125
00129 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
00130 {
00131 if (hc->current + 1 >= hc->length) {
00132 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
00133 return -1;
00134 }
00135 if(!get_bits1(gb)){
00136 int val, i1, i2, b1, b2;
00137 b1 = get_bits_count(gb);
00138 i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
00139 b1 = get_bits_count(gb) - b1;
00140 b2 = get_bits_count(gb);
00141 i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
00142 b2 = get_bits_count(gb) - b2;
00143 if (i1 < 0 || i2 < 0)
00144 return -1;
00145 val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
00146 if(val == ctx->escapes[0]) {
00147 ctx->last[0] = hc->current;
00148 val = 0;
00149 } else if(val == ctx->escapes[1]) {
00150 ctx->last[1] = hc->current;
00151 val = 0;
00152 } else if(val == ctx->escapes[2]) {
00153 ctx->last[2] = hc->current;
00154 val = 0;
00155 }
00156
00157 hc->values[hc->current++] = val;
00158 return 1;
00159 } else {
00160 int r = 0, r_new, t;
00161
00162 t = hc->current++;
00163 r = smacker_decode_bigtree(gb, hc, ctx);
00164 if(r < 0)
00165 return r;
00166 hc->values[t] = SMK_NODE | r;
00167 r++;
00168 r_new = smacker_decode_bigtree(gb, hc, ctx);
00169 if (r_new < 0)
00170 return r_new;
00171 return r + r_new;
00172 }
00173 }
00174
00178 static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
00179 {
00180 int res;
00181 HuffContext huff;
00182 HuffContext tmp1, tmp2;
00183 VLC vlc[2];
00184 int escapes[3];
00185 DBCtx ctx;
00186 int err = 0;
00187
00188 if(size >= UINT_MAX>>4){
00189 av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
00190 return -1;
00191 }
00192
00193 tmp1.length = 256;
00194 tmp1.maxlength = 0;
00195 tmp1.current = 0;
00196 tmp1.bits = av_mallocz(256 * 4);
00197 tmp1.lengths = av_mallocz(256 * sizeof(int));
00198 tmp1.values = av_mallocz(256 * sizeof(int));
00199
00200 tmp2.length = 256;
00201 tmp2.maxlength = 0;
00202 tmp2.current = 0;
00203 tmp2.bits = av_mallocz(256 * 4);
00204 tmp2.lengths = av_mallocz(256 * sizeof(int));
00205 tmp2.values = av_mallocz(256 * sizeof(int));
00206
00207 memset(&vlc[0], 0, sizeof(VLC));
00208 memset(&vlc[1], 0, sizeof(VLC));
00209
00210 if(get_bits1(gb)) {
00211 smacker_decode_tree(gb, &tmp1, 0, 0);
00212 skip_bits1(gb);
00213 res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
00214 tmp1.lengths, sizeof(int), sizeof(int),
00215 tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
00216 if(res < 0) {
00217 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
00218 return -1;
00219 }
00220 } else {
00221 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
00222 }
00223 if(get_bits1(gb)){
00224 smacker_decode_tree(gb, &tmp2, 0, 0);
00225 skip_bits1(gb);
00226 res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
00227 tmp2.lengths, sizeof(int), sizeof(int),
00228 tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
00229 if(res < 0) {
00230 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
00231 return -1;
00232 }
00233 } else {
00234 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
00235 }
00236
00237 escapes[0] = get_bits(gb, 8);
00238 escapes[0] |= get_bits(gb, 8) << 8;
00239 escapes[1] = get_bits(gb, 8);
00240 escapes[1] |= get_bits(gb, 8) << 8;
00241 escapes[2] = get_bits(gb, 8);
00242 escapes[2] |= get_bits(gb, 8) << 8;
00243
00244 last[0] = last[1] = last[2] = -1;
00245
00246 ctx.escapes[0] = escapes[0];
00247 ctx.escapes[1] = escapes[1];
00248 ctx.escapes[2] = escapes[2];
00249 ctx.v1 = &vlc[0];
00250 ctx.v2 = &vlc[1];
00251 ctx.recode1 = tmp1.values;
00252 ctx.recode2 = tmp2.values;
00253 ctx.last = last;
00254
00255 huff.length = ((size + 3) >> 2) + 3;
00256 huff.maxlength = 0;
00257 huff.current = 0;
00258 huff.values = av_mallocz(huff.length * sizeof(int));
00259
00260 if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
00261 err = -1;
00262 skip_bits1(gb);
00263 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
00264 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
00265 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
00266 if(huff.current > huff.length){
00267 ctx.last[0] = ctx.last[1] = ctx.last[2] = 1;
00268 av_log(smk->avctx, AV_LOG_ERROR, "bigtree damaged\n");
00269 return -1;
00270 }
00271
00272 *recodes = huff.values;
00273
00274 if(vlc[0].table)
00275 ff_free_vlc(&vlc[0]);
00276 if(vlc[1].table)
00277 ff_free_vlc(&vlc[1]);
00278 av_free(tmp1.bits);
00279 av_free(tmp1.lengths);
00280 av_free(tmp1.values);
00281 av_free(tmp2.bits);
00282 av_free(tmp2.lengths);
00283 av_free(tmp2.values);
00284
00285 return err;
00286 }
00287
00288 static int decode_header_trees(SmackVContext *smk) {
00289 GetBitContext gb;
00290 int mmap_size, mclr_size, full_size, type_size;
00291
00292 mmap_size = AV_RL32(smk->avctx->extradata);
00293 mclr_size = AV_RL32(smk->avctx->extradata + 4);
00294 full_size = AV_RL32(smk->avctx->extradata + 8);
00295 type_size = AV_RL32(smk->avctx->extradata + 12);
00296
00297 init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
00298
00299 if(!get_bits1(&gb)) {
00300 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
00301 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
00302 smk->mmap_tbl[0] = 0;
00303 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
00304 } else {
00305 if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
00306 return -1;
00307 }
00308 if(!get_bits1(&gb)) {
00309 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
00310 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
00311 smk->mclr_tbl[0] = 0;
00312 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
00313 } else {
00314 if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
00315 return -1;
00316 }
00317 if(!get_bits1(&gb)) {
00318 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
00319 smk->full_tbl = av_malloc(sizeof(int) * 2);
00320 smk->full_tbl[0] = 0;
00321 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
00322 } else {
00323 if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
00324 return -1;
00325 }
00326 if(!get_bits1(&gb)) {
00327 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
00328 smk->type_tbl = av_malloc(sizeof(int) * 2);
00329 smk->type_tbl[0] = 0;
00330 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
00331 } else {
00332 if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
00333 return -1;
00334 }
00335
00336 return 0;
00337 }
00338
00339 static av_always_inline void last_reset(int *recode, int *last) {
00340 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
00341 }
00342
00343
00344 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
00345 register int *table = recode;
00346 int v, b;
00347
00348 b = get_bits_count(gb);
00349 while(*table & SMK_NODE) {
00350 if(get_bits1(gb))
00351 table += (*table) & (~SMK_NODE);
00352 table++;
00353 }
00354 v = *table;
00355 b = get_bits_count(gb) - b;
00356
00357 if(v != recode[last[0]]) {
00358 recode[last[2]] = recode[last[1]];
00359 recode[last[1]] = recode[last[0]];
00360 recode[last[0]] = v;
00361 }
00362 return v;
00363 }
00364
00365 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00366 {
00367 const uint8_t *buf = avpkt->data;
00368 int buf_size = avpkt->size;
00369 SmackVContext * const smk = avctx->priv_data;
00370 uint8_t *out;
00371 uint32_t *pal;
00372 GetBitContext gb;
00373 int blocks, blk, bw, bh;
00374 int i;
00375 int stride;
00376
00377 if(buf_size <= 769)
00378 return 0;
00379
00380 smk->pic.reference = 3;
00381 smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00382 if(avctx->reget_buffer(avctx, &smk->pic) < 0){
00383 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00384 return -1;
00385 }
00386
00387
00388 pal = (uint32_t*)smk->pic.data[1];
00389 smk->pic.palette_has_changed = buf[0] & 1;
00390 smk->pic.key_frame = !!(buf[0] & 2);
00391 if(smk->pic.key_frame)
00392 smk->pic.pict_type = AV_PICTURE_TYPE_I;
00393 else
00394 smk->pic.pict_type = AV_PICTURE_TYPE_P;
00395
00396 buf++;
00397 for(i = 0; i < 256; i++)
00398 *pal++ = 0xFF << 24 | bytestream_get_be24(&buf);
00399 buf_size -= 769;
00400
00401 last_reset(smk->mmap_tbl, smk->mmap_last);
00402 last_reset(smk->mclr_tbl, smk->mclr_last);
00403 last_reset(smk->full_tbl, smk->full_last);
00404 last_reset(smk->type_tbl, smk->type_last);
00405 init_get_bits(&gb, buf, buf_size * 8);
00406
00407 blk = 0;
00408 bw = avctx->width >> 2;
00409 bh = avctx->height >> 2;
00410 blocks = bw * bh;
00411 out = smk->pic.data[0];
00412 stride = smk->pic.linesize[0];
00413 while(blk < blocks) {
00414 int type, run, mode;
00415 uint16_t pix;
00416
00417 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
00418 run = block_runs[(type >> 2) & 0x3F];
00419 switch(type & 3){
00420 case SMK_BLK_MONO:
00421 while(run-- && blk < blocks){
00422 int clr, map;
00423 int hi, lo;
00424 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
00425 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
00426 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
00427 hi = clr >> 8;
00428 lo = clr & 0xFF;
00429 for(i = 0; i < 4; i++) {
00430 if(map & 1) out[0] = hi; else out[0] = lo;
00431 if(map & 2) out[1] = hi; else out[1] = lo;
00432 if(map & 4) out[2] = hi; else out[2] = lo;
00433 if(map & 8) out[3] = hi; else out[3] = lo;
00434 map >>= 4;
00435 out += stride;
00436 }
00437 blk++;
00438 }
00439 break;
00440 case SMK_BLK_FULL:
00441 mode = 0;
00442 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) {
00443 if(get_bits1(&gb)) mode = 1;
00444 else if(get_bits1(&gb)) mode = 2;
00445 }
00446 while(run-- && blk < blocks){
00447 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
00448 switch(mode){
00449 case 0:
00450 for(i = 0; i < 4; i++) {
00451 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00452 AV_WL16(out+2,pix);
00453 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00454 AV_WL16(out,pix);
00455 out += stride;
00456 }
00457 break;
00458 case 1:
00459 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00460 out[0] = out[1] = pix & 0xFF;
00461 out[2] = out[3] = pix >> 8;
00462 out += stride;
00463 out[0] = out[1] = pix & 0xFF;
00464 out[2] = out[3] = pix >> 8;
00465 out += stride;
00466 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00467 out[0] = out[1] = pix & 0xFF;
00468 out[2] = out[3] = pix >> 8;
00469 out += stride;
00470 out[0] = out[1] = pix & 0xFF;
00471 out[2] = out[3] = pix >> 8;
00472 out += stride;
00473 break;
00474 case 2:
00475 for(i = 0; i < 2; i++) {
00476 uint16_t pix1, pix2;
00477 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00478 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00479 AV_WL16(out,pix1);
00480 AV_WL16(out+2,pix2);
00481 out += stride;
00482 AV_WL16(out,pix1);
00483 AV_WL16(out+2,pix2);
00484 out += stride;
00485 }
00486 break;
00487 }
00488 blk++;
00489 }
00490 break;
00491 case SMK_BLK_SKIP:
00492 while(run-- && blk < blocks)
00493 blk++;
00494 break;
00495 case SMK_BLK_FILL:
00496 mode = type >> 8;
00497 while(run-- && blk < blocks){
00498 uint32_t col;
00499 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
00500 col = mode * 0x01010101;
00501 for(i = 0; i < 4; i++) {
00502 *((uint32_t*)out) = col;
00503 out += stride;
00504 }
00505 blk++;
00506 }
00507 break;
00508 }
00509
00510 }
00511
00512 *data_size = sizeof(AVFrame);
00513 *(AVFrame*)data = smk->pic;
00514
00515
00516 return buf_size;
00517 }
00518
00519
00520
00521
00522
00523
00524
00525
00526 static av_cold int decode_init(AVCodecContext *avctx)
00527 {
00528 SmackVContext * const c = avctx->priv_data;
00529
00530 c->avctx = avctx;
00531
00532 avctx->pix_fmt = PIX_FMT_PAL8;
00533
00534 avcodec_get_frame_defaults(&c->pic);
00535
00536
00537 if(avctx->extradata_size < 16){
00538 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
00539 return -1;
00540 }
00541
00542 if (decode_header_trees(c))
00543 return -1;
00544
00545 return 0;
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555 static av_cold int decode_end(AVCodecContext *avctx)
00556 {
00557 SmackVContext * const smk = avctx->priv_data;
00558
00559 av_freep(&smk->mmap_tbl);
00560 av_freep(&smk->mclr_tbl);
00561 av_freep(&smk->full_tbl);
00562 av_freep(&smk->type_tbl);
00563
00564 if (smk->pic.data[0])
00565 avctx->release_buffer(avctx, &smk->pic);
00566
00567 return 0;
00568 }
00569
00570
00571 typedef struct SmackerAudioContext {
00572 AVFrame frame;
00573 } SmackerAudioContext;
00574
00575 static av_cold int smka_decode_init(AVCodecContext *avctx)
00576 {
00577 SmackerAudioContext *s = avctx->priv_data;
00578
00579 if (avctx->channels < 1 || avctx->channels > 2) {
00580 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
00581 return AVERROR(EINVAL);
00582 }
00583 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
00584 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
00585
00586 avcodec_get_frame_defaults(&s->frame);
00587 avctx->coded_frame = &s->frame;
00588
00589 return 0;
00590 }
00591
00595 static int smka_decode_frame(AVCodecContext *avctx, void *data,
00596 int *got_frame_ptr, AVPacket *avpkt)
00597 {
00598 SmackerAudioContext *s = avctx->priv_data;
00599 const uint8_t *buf = avpkt->data;
00600 int buf_size = avpkt->size;
00601 GetBitContext gb;
00602 HuffContext h[4];
00603 VLC vlc[4];
00604 int16_t *samples;
00605 uint8_t *samples8;
00606 int val;
00607 int i, res, ret;
00608 int unp_size;
00609 int bits, stereo;
00610 int pred[2] = {0, 0};
00611
00612 if (buf_size <= 4) {
00613 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
00614 return AVERROR(EINVAL);
00615 }
00616
00617 unp_size = AV_RL32(buf);
00618
00619 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
00620
00621 if(!get_bits1(&gb)){
00622 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
00623 *got_frame_ptr = 0;
00624 return 1;
00625 }
00626 stereo = get_bits1(&gb);
00627 bits = get_bits1(&gb);
00628 if (stereo ^ (avctx->channels != 1)) {
00629 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
00630 return AVERROR(EINVAL);
00631 }
00632 if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
00633 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
00634 return AVERROR(EINVAL);
00635 }
00636
00637
00638 s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));
00639 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00640 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00641 return ret;
00642 }
00643 samples = (int16_t *)s->frame.data[0];
00644 samples8 = s->frame.data[0];
00645
00646 memset(vlc, 0, sizeof(VLC) * 4);
00647 memset(h, 0, sizeof(HuffContext) * 4);
00648
00649 for(i = 0; i < (1 << (bits + stereo)); i++) {
00650 h[i].length = 256;
00651 h[i].maxlength = 0;
00652 h[i].current = 0;
00653 h[i].bits = av_mallocz(256 * 4);
00654 h[i].lengths = av_mallocz(256 * sizeof(int));
00655 h[i].values = av_mallocz(256 * sizeof(int));
00656 skip_bits1(&gb);
00657 smacker_decode_tree(&gb, &h[i], 0, 0);
00658 skip_bits1(&gb);
00659 if(h[i].current > 1) {
00660 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
00661 h[i].lengths, sizeof(int), sizeof(int),
00662 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
00663 if(res < 0) {
00664 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
00665 return -1;
00666 }
00667 }
00668 }
00669 if(bits) {
00670 for(i = stereo; i >= 0; i--)
00671 pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
00672 for(i = 0; i <= stereo; i++)
00673 *samples++ = pred[i];
00674 for(; i < unp_size / 2; i++) {
00675 if(get_bits_left(&gb)<0)
00676 return -1;
00677 if(i & stereo) {
00678 if(vlc[2].table)
00679 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
00680 else
00681 res = 0;
00682 val = h[2].values[res];
00683 if(vlc[3].table)
00684 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
00685 else
00686 res = 0;
00687 val |= h[3].values[res] << 8;
00688 pred[1] += sign_extend(val, 16);
00689 *samples++ = av_clip_int16(pred[1]);
00690 } else {
00691 if(vlc[0].table)
00692 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
00693 else
00694 res = 0;
00695 val = h[0].values[res];
00696 if(vlc[1].table)
00697 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
00698 else
00699 res = 0;
00700 val |= h[1].values[res] << 8;
00701 pred[0] += sign_extend(val, 16);
00702 *samples++ = av_clip_int16(pred[0]);
00703 }
00704 }
00705 } else {
00706 for(i = stereo; i >= 0; i--)
00707 pred[i] = get_bits(&gb, 8);
00708 for(i = 0; i <= stereo; i++)
00709 *samples8++ = pred[i];
00710 for(; i < unp_size; i++) {
00711 if(get_bits_left(&gb)<0)
00712 return -1;
00713 if(i & stereo){
00714 if(vlc[1].table)
00715 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
00716 else
00717 res = 0;
00718 pred[1] += sign_extend(h[1].values[res], 8);
00719 *samples8++ = av_clip_uint8(pred[1]);
00720 } else {
00721 if(vlc[0].table)
00722 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
00723 else
00724 res = 0;
00725 pred[0] += sign_extend(h[0].values[res], 8);
00726 *samples8++ = av_clip_uint8(pred[0]);
00727 }
00728 }
00729 }
00730
00731 for(i = 0; i < 4; i++) {
00732 if(vlc[i].table)
00733 ff_free_vlc(&vlc[i]);
00734 av_free(h[i].bits);
00735 av_free(h[i].lengths);
00736 av_free(h[i].values);
00737 }
00738
00739 *got_frame_ptr = 1;
00740 *(AVFrame *)data = s->frame;
00741
00742 return buf_size;
00743 }
00744
00745 AVCodec ff_smacker_decoder = {
00746 .name = "smackvid",
00747 .type = AVMEDIA_TYPE_VIDEO,
00748 .id = CODEC_ID_SMACKVIDEO,
00749 .priv_data_size = sizeof(SmackVContext),
00750 .init = decode_init,
00751 .close = decode_end,
00752 .decode = decode_frame,
00753 .capabilities = CODEC_CAP_DR1,
00754 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
00755 };
00756
00757 AVCodec ff_smackaud_decoder = {
00758 .name = "smackaud",
00759 .type = AVMEDIA_TYPE_AUDIO,
00760 .id = CODEC_ID_SMACKAUDIO,
00761 .priv_data_size = sizeof(SmackerAudioContext),
00762 .init = smka_decode_init,
00763 .decode = smka_decode_frame,
00764 .capabilities = CODEC_CAP_DR1,
00765 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
00766 };
00767