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(length > 32) {
00100 av_log(NULL, AV_LOG_ERROR, "length too long\n");
00101 return -1;
00102 }
00103 if(!get_bits1(gb)){
00104 if(hc->current >= 256){
00105 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
00106 return -1;
00107 }
00108 if(length){
00109 hc->bits[hc->current] = prefix;
00110 hc->lengths[hc->current] = length;
00111 } else {
00112 hc->bits[hc->current] = 0;
00113 hc->lengths[hc->current] = 0;
00114 }
00115 hc->values[hc->current] = get_bits(gb, 8);
00116 hc->current++;
00117 if(hc->maxlength < length)
00118 hc->maxlength = length;
00119 return 0;
00120 } else {
00121 int r;
00122 length++;
00123 r = smacker_decode_tree(gb, hc, prefix, length);
00124 if(r)
00125 return r;
00126 return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
00127 }
00128 }
00129
00133 static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
00134 {
00135 if (hc->current + 1 >= hc->length) {
00136 av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
00137 return -1;
00138 }
00139 if(!get_bits1(gb)){
00140 int val, i1, i2;
00141 i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
00142 i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
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] = { { 0 } };
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 if(get_bits1(gb)) {
00208 smacker_decode_tree(gb, &tmp1, 0, 0);
00209 skip_bits1(gb);
00210 res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
00211 tmp1.lengths, sizeof(int), sizeof(int),
00212 tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
00213 if(res < 0) {
00214 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
00215 return -1;
00216 }
00217 } else {
00218 av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
00219 }
00220 if(get_bits1(gb)){
00221 smacker_decode_tree(gb, &tmp2, 0, 0);
00222 skip_bits1(gb);
00223 res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
00224 tmp2.lengths, sizeof(int), sizeof(int),
00225 tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
00226 if(res < 0) {
00227 av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
00228 return -1;
00229 }
00230 } else {
00231 av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
00232 }
00233
00234 escapes[0] = get_bits(gb, 8);
00235 escapes[0] |= get_bits(gb, 8) << 8;
00236 escapes[1] = get_bits(gb, 8);
00237 escapes[1] |= get_bits(gb, 8) << 8;
00238 escapes[2] = get_bits(gb, 8);
00239 escapes[2] |= get_bits(gb, 8) << 8;
00240
00241 last[0] = last[1] = last[2] = -1;
00242
00243 ctx.escapes[0] = escapes[0];
00244 ctx.escapes[1] = escapes[1];
00245 ctx.escapes[2] = escapes[2];
00246 ctx.v1 = &vlc[0];
00247 ctx.v2 = &vlc[1];
00248 ctx.recode1 = tmp1.values;
00249 ctx.recode2 = tmp2.values;
00250 ctx.last = last;
00251
00252 huff.length = ((size + 3) >> 2) + 3;
00253 huff.maxlength = 0;
00254 huff.current = 0;
00255 huff.values = av_mallocz(huff.length * sizeof(int));
00256
00257 if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
00258 err = -1;
00259 skip_bits1(gb);
00260 if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
00261 if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
00262 if(ctx.last[2] == -1) ctx.last[2] = huff.current++;
00263 if(huff.current > huff.length){
00264 ctx.last[0] = ctx.last[1] = ctx.last[2] = 1;
00265 av_log(smk->avctx, AV_LOG_ERROR, "bigtree damaged\n");
00266 return -1;
00267 }
00268
00269 *recodes = huff.values;
00270
00271 if(vlc[0].table)
00272 ff_free_vlc(&vlc[0]);
00273 if(vlc[1].table)
00274 ff_free_vlc(&vlc[1]);
00275 av_free(tmp1.bits);
00276 av_free(tmp1.lengths);
00277 av_free(tmp1.values);
00278 av_free(tmp2.bits);
00279 av_free(tmp2.lengths);
00280 av_free(tmp2.values);
00281
00282 return err;
00283 }
00284
00285 static int decode_header_trees(SmackVContext *smk) {
00286 GetBitContext gb;
00287 int mmap_size, mclr_size, full_size, type_size;
00288
00289 mmap_size = AV_RL32(smk->avctx->extradata);
00290 mclr_size = AV_RL32(smk->avctx->extradata + 4);
00291 full_size = AV_RL32(smk->avctx->extradata + 8);
00292 type_size = AV_RL32(smk->avctx->extradata + 12);
00293
00294 init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
00295
00296 if(!get_bits1(&gb)) {
00297 av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
00298 smk->mmap_tbl = av_malloc(sizeof(int) * 2);
00299 smk->mmap_tbl[0] = 0;
00300 smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
00301 } else {
00302 if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
00303 return -1;
00304 }
00305 if(!get_bits1(&gb)) {
00306 av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
00307 smk->mclr_tbl = av_malloc(sizeof(int) * 2);
00308 smk->mclr_tbl[0] = 0;
00309 smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
00310 } else {
00311 if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
00312 return -1;
00313 }
00314 if(!get_bits1(&gb)) {
00315 av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
00316 smk->full_tbl = av_malloc(sizeof(int) * 2);
00317 smk->full_tbl[0] = 0;
00318 smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
00319 } else {
00320 if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
00321 return -1;
00322 }
00323 if(!get_bits1(&gb)) {
00324 av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
00325 smk->type_tbl = av_malloc(sizeof(int) * 2);
00326 smk->type_tbl[0] = 0;
00327 smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
00328 } else {
00329 if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
00330 return -1;
00331 }
00332
00333 return 0;
00334 }
00335
00336 static av_always_inline void last_reset(int *recode, int *last) {
00337 recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
00338 }
00339
00340
00341 static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
00342 register int *table = recode;
00343 int v;
00344
00345 while(*table & SMK_NODE) {
00346 if(get_bits1(gb))
00347 table += (*table) & (~SMK_NODE);
00348 table++;
00349 }
00350 v = *table;
00351
00352 if(v != recode[last[0]]) {
00353 recode[last[2]] = recode[last[1]];
00354 recode[last[1]] = recode[last[0]];
00355 recode[last[0]] = v;
00356 }
00357 return v;
00358 }
00359
00360 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00361 {
00362 SmackVContext * const smk = avctx->priv_data;
00363 uint8_t *out;
00364 uint32_t *pal;
00365 GetByteContext gb2;
00366 GetBitContext gb;
00367 int blocks, blk, bw, bh;
00368 int i;
00369 int stride;
00370 int flags;
00371
00372 if (avpkt->size <= 769)
00373 return 0;
00374
00375 smk->pic.reference = 3;
00376 smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00377 if(avctx->reget_buffer(avctx, &smk->pic) < 0){
00378 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00379 return -1;
00380 }
00381
00382
00383 pal = (uint32_t*)smk->pic.data[1];
00384 bytestream2_init(&gb2, avpkt->data, avpkt->size);
00385 flags = bytestream2_get_byteu(&gb2);
00386 smk->pic.palette_has_changed = flags & 1;
00387 smk->pic.key_frame = !!(flags & 2);
00388 if(smk->pic.key_frame)
00389 smk->pic.pict_type = AV_PICTURE_TYPE_I;
00390 else
00391 smk->pic.pict_type = AV_PICTURE_TYPE_P;
00392
00393 for(i = 0; i < 256; i++)
00394 *pal++ = 0xFF << 24 | bytestream2_get_be24u(&gb2);
00395
00396 last_reset(smk->mmap_tbl, smk->mmap_last);
00397 last_reset(smk->mclr_tbl, smk->mclr_last);
00398 last_reset(smk->full_tbl, smk->full_last);
00399 last_reset(smk->type_tbl, smk->type_last);
00400 init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
00401
00402 blk = 0;
00403 bw = avctx->width >> 2;
00404 bh = avctx->height >> 2;
00405 blocks = bw * bh;
00406 out = smk->pic.data[0];
00407 stride = smk->pic.linesize[0];
00408 while(blk < blocks) {
00409 int type, run, mode;
00410 uint16_t pix;
00411
00412 type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
00413 run = block_runs[(type >> 2) & 0x3F];
00414 switch(type & 3){
00415 case SMK_BLK_MONO:
00416 while(run-- && blk < blocks){
00417 int clr, map;
00418 int hi, lo;
00419 clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
00420 map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last);
00421 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
00422 hi = clr >> 8;
00423 lo = clr & 0xFF;
00424 for(i = 0; i < 4; i++) {
00425 if(map & 1) out[0] = hi; else out[0] = lo;
00426 if(map & 2) out[1] = hi; else out[1] = lo;
00427 if(map & 4) out[2] = hi; else out[2] = lo;
00428 if(map & 8) out[3] = hi; else out[3] = lo;
00429 map >>= 4;
00430 out += stride;
00431 }
00432 blk++;
00433 }
00434 break;
00435 case SMK_BLK_FULL:
00436 mode = 0;
00437 if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) {
00438 if(get_bits1(&gb)) mode = 1;
00439 else if(get_bits1(&gb)) mode = 2;
00440 }
00441 while(run-- && blk < blocks){
00442 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
00443 switch(mode){
00444 case 0:
00445 for(i = 0; i < 4; i++) {
00446 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00447 AV_WL16(out+2,pix);
00448 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00449 AV_WL16(out,pix);
00450 out += stride;
00451 }
00452 break;
00453 case 1:
00454 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00455 out[0] = out[1] = pix & 0xFF;
00456 out[2] = out[3] = pix >> 8;
00457 out += stride;
00458 out[0] = out[1] = pix & 0xFF;
00459 out[2] = out[3] = pix >> 8;
00460 out += stride;
00461 pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00462 out[0] = out[1] = pix & 0xFF;
00463 out[2] = out[3] = pix >> 8;
00464 out += stride;
00465 out[0] = out[1] = pix & 0xFF;
00466 out[2] = out[3] = pix >> 8;
00467 out += stride;
00468 break;
00469 case 2:
00470 for(i = 0; i < 2; i++) {
00471 uint16_t pix1, pix2;
00472 pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00473 pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
00474 AV_WL16(out,pix1);
00475 AV_WL16(out+2,pix2);
00476 out += stride;
00477 AV_WL16(out,pix1);
00478 AV_WL16(out+2,pix2);
00479 out += stride;
00480 }
00481 break;
00482 }
00483 blk++;
00484 }
00485 break;
00486 case SMK_BLK_SKIP:
00487 while(run-- && blk < blocks)
00488 blk++;
00489 break;
00490 case SMK_BLK_FILL:
00491 mode = type >> 8;
00492 while(run-- && blk < blocks){
00493 uint32_t col;
00494 out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
00495 col = mode * 0x01010101;
00496 for(i = 0; i < 4; i++) {
00497 *((uint32_t*)out) = col;
00498 out += stride;
00499 }
00500 blk++;
00501 }
00502 break;
00503 }
00504
00505 }
00506
00507 *data_size = sizeof(AVFrame);
00508 *(AVFrame*)data = smk->pic;
00509
00510
00511 return avpkt->size;
00512 }
00513
00514
00515
00516
00517
00518
00519
00520
00521 static av_cold int decode_init(AVCodecContext *avctx)
00522 {
00523 SmackVContext * const c = avctx->priv_data;
00524
00525 c->avctx = avctx;
00526
00527 avctx->pix_fmt = PIX_FMT_PAL8;
00528
00529 avcodec_get_frame_defaults(&c->pic);
00530
00531
00532 if(avctx->extradata_size < 16){
00533 av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
00534 return -1;
00535 }
00536
00537 if (decode_header_trees(c))
00538 return -1;
00539
00540 return 0;
00541 }
00542
00543
00544
00545
00546
00547
00548
00549
00550 static av_cold int decode_end(AVCodecContext *avctx)
00551 {
00552 SmackVContext * const smk = avctx->priv_data;
00553
00554 av_freep(&smk->mmap_tbl);
00555 av_freep(&smk->mclr_tbl);
00556 av_freep(&smk->full_tbl);
00557 av_freep(&smk->type_tbl);
00558
00559 if (smk->pic.data[0])
00560 avctx->release_buffer(avctx, &smk->pic);
00561
00562 return 0;
00563 }
00564
00565
00566 typedef struct SmackerAudioContext {
00567 AVFrame frame;
00568 } SmackerAudioContext;
00569
00570 static av_cold int smka_decode_init(AVCodecContext *avctx)
00571 {
00572 SmackerAudioContext *s = avctx->priv_data;
00573
00574 if (avctx->channels < 1 || avctx->channels > 2) {
00575 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
00576 return AVERROR(EINVAL);
00577 }
00578 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
00579 avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
00580
00581 avcodec_get_frame_defaults(&s->frame);
00582 avctx->coded_frame = &s->frame;
00583
00584 return 0;
00585 }
00586
00590 static int smka_decode_frame(AVCodecContext *avctx, void *data,
00591 int *got_frame_ptr, AVPacket *avpkt)
00592 {
00593 SmackerAudioContext *s = avctx->priv_data;
00594 const uint8_t *buf = avpkt->data;
00595 int buf_size = avpkt->size;
00596 GetBitContext gb;
00597 HuffContext h[4] = { { 0 } };
00598 VLC vlc[4] = { { 0 } };
00599 int16_t *samples;
00600 uint8_t *samples8;
00601 int val;
00602 int i, res, ret;
00603 int unp_size;
00604 int bits, stereo;
00605 int pred[2] = {0, 0};
00606
00607 if (buf_size <= 4) {
00608 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
00609 return AVERROR(EINVAL);
00610 }
00611
00612 unp_size = AV_RL32(buf);
00613
00614 init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
00615
00616 if(!get_bits1(&gb)){
00617 av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
00618 *got_frame_ptr = 0;
00619 return 1;
00620 }
00621 stereo = get_bits1(&gb);
00622 bits = get_bits1(&gb);
00623 if (stereo ^ (avctx->channels != 1)) {
00624 av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
00625 return AVERROR(EINVAL);
00626 }
00627 if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
00628 av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
00629 return AVERROR(EINVAL);
00630 }
00631
00632
00633 s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));
00634 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00635 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00636 return ret;
00637 }
00638 samples = (int16_t *)s->frame.data[0];
00639 samples8 = s->frame.data[0];
00640
00641
00642 for(i = 0; i < (1 << (bits + stereo)); i++) {
00643 h[i].length = 256;
00644 h[i].maxlength = 0;
00645 h[i].current = 0;
00646 h[i].bits = av_mallocz(256 * 4);
00647 h[i].lengths = av_mallocz(256 * sizeof(int));
00648 h[i].values = av_mallocz(256 * sizeof(int));
00649 skip_bits1(&gb);
00650 smacker_decode_tree(&gb, &h[i], 0, 0);
00651 skip_bits1(&gb);
00652 if(h[i].current > 1) {
00653 res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
00654 h[i].lengths, sizeof(int), sizeof(int),
00655 h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
00656 if(res < 0) {
00657 av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
00658 return -1;
00659 }
00660 }
00661 }
00662 if(bits) {
00663 for(i = stereo; i >= 0; i--)
00664 pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
00665 for(i = 0; i <= stereo; i++)
00666 *samples++ = pred[i];
00667 for(; i < unp_size / 2; i++) {
00668 if(get_bits_left(&gb)<0)
00669 return -1;
00670 if(i & stereo) {
00671 if(vlc[2].table)
00672 res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
00673 else
00674 res = 0;
00675 if (res < 0) {
00676 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
00677 return AVERROR_INVALIDDATA;
00678 }
00679 val = h[2].values[res];
00680 if(vlc[3].table)
00681 res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
00682 else
00683 res = 0;
00684 if (res < 0) {
00685 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
00686 return AVERROR_INVALIDDATA;
00687 }
00688 val |= h[3].values[res] << 8;
00689 pred[1] += sign_extend(val, 16);
00690 *samples++ = av_clip_int16(pred[1]);
00691 } else {
00692 if(vlc[0].table)
00693 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
00694 else
00695 res = 0;
00696 if (res < 0) {
00697 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
00698 return AVERROR_INVALIDDATA;
00699 }
00700 val = h[0].values[res];
00701 if(vlc[1].table)
00702 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
00703 else
00704 res = 0;
00705 if (res < 0) {
00706 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
00707 return AVERROR_INVALIDDATA;
00708 }
00709 val |= h[1].values[res] << 8;
00710 pred[0] += sign_extend(val, 16);
00711 *samples++ = av_clip_int16(pred[0]);
00712 }
00713 }
00714 } else {
00715 for(i = stereo; i >= 0; i--)
00716 pred[i] = get_bits(&gb, 8);
00717 for(i = 0; i <= stereo; i++)
00718 *samples8++ = pred[i];
00719 for(; i < unp_size; i++) {
00720 if(get_bits_left(&gb)<0)
00721 return -1;
00722 if(i & stereo){
00723 if(vlc[1].table)
00724 res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
00725 else
00726 res = 0;
00727 if (res < 0) {
00728 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
00729 return AVERROR_INVALIDDATA;
00730 }
00731 pred[1] += sign_extend(h[1].values[res], 8);
00732 *samples8++ = av_clip_uint8(pred[1]);
00733 } else {
00734 if(vlc[0].table)
00735 res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
00736 else
00737 res = 0;
00738 if (res < 0) {
00739 av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
00740 return AVERROR_INVALIDDATA;
00741 }
00742 pred[0] += sign_extend(h[0].values[res], 8);
00743 *samples8++ = av_clip_uint8(pred[0]);
00744 }
00745 }
00746 }
00747
00748 for(i = 0; i < 4; i++) {
00749 if(vlc[i].table)
00750 ff_free_vlc(&vlc[i]);
00751 av_free(h[i].bits);
00752 av_free(h[i].lengths);
00753 av_free(h[i].values);
00754 }
00755
00756 *got_frame_ptr = 1;
00757 *(AVFrame *)data = s->frame;
00758
00759 return buf_size;
00760 }
00761
00762 AVCodec ff_smacker_decoder = {
00763 .name = "smackvid",
00764 .type = AVMEDIA_TYPE_VIDEO,
00765 .id = AV_CODEC_ID_SMACKVIDEO,
00766 .priv_data_size = sizeof(SmackVContext),
00767 .init = decode_init,
00768 .close = decode_end,
00769 .decode = decode_frame,
00770 .capabilities = CODEC_CAP_DR1,
00771 .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
00772 };
00773
00774 AVCodec ff_smackaud_decoder = {
00775 .name = "smackaud",
00776 .type = AVMEDIA_TYPE_AUDIO,
00777 .id = AV_CODEC_ID_SMACKAUDIO,
00778 .priv_data_size = sizeof(SmackerAudioContext),
00779 .init = smka_decode_init,
00780 .decode = smka_decode_frame,
00781 .capabilities = CODEC_CAP_DR1,
00782 .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
00783 };