348efc18 |
/*
* Smacker decoder
* Copyright (c) 2006 Konstantin Shishkov
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
348efc18 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
348efc18 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
348efc18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
348efc18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/** |
ba87f080 |
* @file |
348efc18 |
* Smacker decoder
*/
/*
* Based on http://wiki.multimedia.cx/index.php?title=Smacker
*/
#include <stdio.h>
#include <stdlib.h>
|
a903f8f0 |
#include "libavutil/channel_layout.h" |
348efc18 |
#include "avcodec.h" |
594d4d5d |
#include "internal.h" |
c86c3c80 |
#include "mathops.h" |
348efc18 |
|
aaf47bcd |
#define BITSTREAM_READER_LE |
9106a698 |
#include "get_bits.h" |
3e368d72 |
#include "bytestream.h" |
348efc18 |
#define SMKTREE_BITS 9
#define SMK_NODE 0x80000000
/*
* Decoder context
*/
typedef struct SmackVContext {
AVCodecContext *avctx; |
76e27b1d |
AVFrame *pic; |
348efc18 |
int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
} SmackVContext;
/**
* Context used for code reconstructing
*/
typedef struct HuffContext {
int length;
int maxlength;
int current;
uint32_t *bits;
int *lengths;
int *values;
} HuffContext;
/* common parameters used for decode_bigtree */
typedef struct DBCtx {
VLC *v1, *v2;
int *recode1, *recode2;
int escapes[3];
int *last;
int lcur;
} DBCtx;
/* possible runs of blocks */
static const int block_runs[64] = {
1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 128, 256, 512, 1024, 2048 };
enum SmkBlockTypes {
SMK_BLK_MONO = 0,
SMK_BLK_FULL = 1,
SMK_BLK_SKIP = 2,
SMK_BLK_FILL = 3 };
/**
* Decode local frame tree
*/
static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length)
{ |
2c69fcc2 |
if(length > 32 || length > 3*SMKTREE_BITS) { |
b829da36 |
av_log(NULL, AV_LOG_ERROR, "length too long\n"); |
c1947015 |
return AVERROR_INVALIDDATA; |
b829da36 |
} |
348efc18 |
if(!get_bits1(gb)){ //Leaf |
59a08d19 |
if(hc->current >= hc->length){ |
348efc18 |
av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n"); |
c1947015 |
return AVERROR_INVALIDDATA; |
348efc18 |
}
if(length){
hc->bits[hc->current] = prefix;
hc->lengths[hc->current] = length;
} else {
hc->bits[hc->current] = 0;
hc->lengths[hc->current] = 0;
}
hc->values[hc->current] = get_bits(gb, 8);
hc->current++;
if(hc->maxlength < length)
hc->maxlength = length;
return 0;
} else { //Node
int r;
length++;
r = smacker_decode_tree(gb, hc, prefix, length);
if(r)
return r;
return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length);
}
}
/**
* Decode header tree
*/
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
{ |
9adf25c1 |
if (hc->current + 1 >= hc->length) {
av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n"); |
c1947015 |
return AVERROR_INVALIDDATA; |
9adf25c1 |
} |
348efc18 |
if(!get_bits1(gb)){ //Leaf |
779f8bc2 |
int val, i1, i2; |
d07ac185 |
i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3) : 0;
i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3) : 0;
if (i1 < 0 || i2 < 0) |
c1947015 |
return AVERROR_INVALIDDATA; |
348efc18 |
val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
if(val == ctx->escapes[0]) {
ctx->last[0] = hc->current;
val = 0;
} else if(val == ctx->escapes[1]) {
ctx->last[1] = hc->current;
val = 0;
} else if(val == ctx->escapes[2]) {
ctx->last[2] = hc->current;
val = 0;
}
hc->values[hc->current++] = val;
return 1;
} else { //Node |
9adf25c1 |
int r = 0, r_new, t; |
348efc18 |
t = hc->current++;
r = smacker_decode_bigtree(gb, hc, ctx);
if(r < 0)
return r;
hc->values[t] = SMK_NODE | r;
r++; |
9adf25c1 |
r_new = smacker_decode_bigtree(gb, hc, ctx);
if (r_new < 0)
return r_new;
return r + r_new; |
348efc18 |
}
}
/**
* Store large tree as FFmpeg's vlc codes
*/
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
{
int res;
HuffContext huff;
HuffContext tmp1, tmp2; |
a92be9b8 |
VLC vlc[2] = { { 0 } }; |
348efc18 |
int escapes[3];
DBCtx ctx; |
9adf25c1 |
int err = 0; |
348efc18 |
|
3a1a7e32 |
if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
av_log(smk->avctx, AV_LOG_ERROR, "size too large\n"); |
c1947015 |
return AVERROR_INVALIDDATA; |
3a1a7e32 |
}
|
348efc18 |
tmp1.length = 256;
tmp1.maxlength = 0;
tmp1.current = 0;
tmp1.bits = av_mallocz(256 * 4);
tmp1.lengths = av_mallocz(256 * sizeof(int));
tmp1.values = av_mallocz(256 * sizeof(int));
tmp2.length = 256;
tmp2.maxlength = 0;
tmp2.current = 0;
tmp2.bits = av_mallocz(256 * 4);
tmp2.lengths = av_mallocz(256 * sizeof(int));
tmp2.values = av_mallocz(256 * sizeof(int)); |
d002fce2 |
if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
!tmp2.bits || !tmp2.lengths || !tmp2.values) {
err = AVERROR(ENOMEM);
goto error;
} |
348efc18 |
if(get_bits1(gb)) { |
50cbe09d |
res = smacker_decode_tree(gb, &tmp1, 0, 0); |
f144168d |
if (res < 0) {
err = res;
goto error;
} |
7ae7300e |
skip_bits1(gb); |
48cbdaea |
if(tmp1.current > 1) {
res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length,
tmp1.lengths, sizeof(int), sizeof(int),
tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
if(res < 0) {
av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); |
d510d70d |
err = res;
goto error; |
48cbdaea |
} |
348efc18 |
} |
48cbdaea |
}
if (!vlc[0].table) { |
348efc18 |
av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
}
if(get_bits1(gb)){ |
50cbe09d |
res = smacker_decode_tree(gb, &tmp2, 0, 0); |
f144168d |
if (res < 0) {
err = res;
goto error;
} |
7ae7300e |
skip_bits1(gb); |
48cbdaea |
if(tmp2.current > 1) {
res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length,
tmp2.lengths, sizeof(int), sizeof(int),
tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
if(res < 0) {
av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); |
d510d70d |
err = res;
goto error; |
48cbdaea |
} |
348efc18 |
} |
48cbdaea |
}
if (!vlc[1].table) { |
348efc18 |
av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
}
|
adc5539e |
escapes[0] = get_bits(gb, 16);
escapes[1] = get_bits(gb, 16);
escapes[2] = get_bits(gb, 16); |
348efc18 |
last[0] = last[1] = last[2] = -1;
ctx.escapes[0] = escapes[0];
ctx.escapes[1] = escapes[1];
ctx.escapes[2] = escapes[2];
ctx.v1 = &vlc[0];
ctx.v2 = &vlc[1];
ctx.recode1 = tmp1.values;
ctx.recode2 = tmp2.values;
ctx.last = last;
|
f52edef3 |
huff.length = ((size + 3) >> 2) + 4; |
348efc18 |
huff.maxlength = 0;
huff.current = 0; |
683b6e31 |
huff.values = av_mallocz_array(huff.length, sizeof(int)); |
d002fce2 |
if (!huff.values) {
err = AVERROR(ENOMEM);
goto error;
} |
348efc18 |
|
9adf25c1 |
if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
err = -1; |
7ae7300e |
skip_bits1(gb); |
348efc18 |
if(ctx.last[0] == -1) ctx.last[0] = huff.current++;
if(ctx.last[1] == -1) ctx.last[1] = huff.current++;
if(ctx.last[2] == -1) ctx.last[2] = huff.current++; |
0679cec6 |
if (ctx.last[0] >= huff.length ||
ctx.last[1] >= huff.length ||
ctx.last[2] >= huff.length) {
av_log(smk->avctx, AV_LOG_ERROR, "Huffman codes out of range\n");
err = AVERROR_INVALIDDATA;
} |
348efc18 |
*recodes = huff.values;
|
d002fce2 |
error: |
348efc18 |
if(vlc[0].table) |
e96b4a53 |
ff_free_vlc(&vlc[0]); |
348efc18 |
if(vlc[1].table) |
e96b4a53 |
ff_free_vlc(&vlc[1]); |
348efc18 |
av_free(tmp1.bits);
av_free(tmp1.lengths);
av_free(tmp1.values);
av_free(tmp2.bits);
av_free(tmp2.lengths);
av_free(tmp2.values);
|
9adf25c1 |
return err; |
348efc18 |
}
static int decode_header_trees(SmackVContext *smk) {
GetBitContext gb; |
a1dbe49d |
int mmap_size, mclr_size, full_size, type_size, ret; |
348efc18 |
|
fead30d4 |
mmap_size = AV_RL32(smk->avctx->extradata);
mclr_size = AV_RL32(smk->avctx->extradata + 4);
full_size = AV_RL32(smk->avctx->extradata + 8);
type_size = AV_RL32(smk->avctx->extradata + 12); |
348efc18 |
|
1b99514d |
init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16); |
348efc18 |
if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
smk->mmap_tbl = av_malloc(sizeof(int) * 2); |
d002fce2 |
if (!smk->mmap_tbl)
return AVERROR(ENOMEM); |
348efc18 |
smk->mmap_tbl[0] = 0;
smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
} else { |
a1dbe49d |
ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
if (ret < 0)
return ret; |
348efc18 |
} |
5fc32c27 |
if(!get_bits1(&gb)) { |
348efc18 |
av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
smk->mclr_tbl = av_malloc(sizeof(int) * 2); |
d002fce2 |
if (!smk->mclr_tbl)
return AVERROR(ENOMEM); |
348efc18 |
smk->mclr_tbl[0] = 0;
smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
} else { |
a1dbe49d |
ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
if (ret < 0)
return ret; |
348efc18 |
} |
5fc32c27 |
if(!get_bits1(&gb)) { |
348efc18 |
av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
smk->full_tbl = av_malloc(sizeof(int) * 2); |
d002fce2 |
if (!smk->full_tbl)
return AVERROR(ENOMEM); |
348efc18 |
smk->full_tbl[0] = 0;
smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
} else { |
a1dbe49d |
ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
if (ret < 0)
return ret; |
348efc18 |
} |
5fc32c27 |
if(!get_bits1(&gb)) { |
348efc18 |
av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
smk->type_tbl = av_malloc(sizeof(int) * 2); |
d002fce2 |
if (!smk->type_tbl)
return AVERROR(ENOMEM); |
348efc18 |
smk->type_tbl[0] = 0;
smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
} else { |
a1dbe49d |
ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
if (ret < 0)
return ret; |
348efc18 |
}
return 0;
}
|
849f1035 |
static av_always_inline void last_reset(int *recode, int *last) { |
348efc18 |
recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
}
/* get code and update history */ |
849f1035 |
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) { |
348efc18 |
register int *table = recode; |
90c0c83e |
int v; |
348efc18 |
while(*table & SMK_NODE) {
if(get_bits1(gb))
table += (*table) & (~SMK_NODE);
table++;
}
v = *table;
if(v != recode[last[0]]) {
recode[last[2]] = recode[last[1]];
recode[last[1]] = recode[last[0]];
recode[last[0]] = v;
}
return v;
}
|
df9b9567 |
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
AVPacket *avpkt) |
348efc18 |
{ |
e4141433 |
SmackVContext * const smk = avctx->priv_data; |
348efc18 |
uint8_t *out;
uint32_t *pal; |
b2af057a |
GetByteContext gb2; |
348efc18 |
GetBitContext gb;
int blocks, blk, bw, bh; |
916e40b5 |
int i, ret; |
348efc18 |
int stride; |
b2af057a |
int flags; |
348efc18 |
|
b2af057a |
if (avpkt->size <= 769) |
916e40b5 |
return AVERROR_INVALIDDATA; |
348efc18 |
|
76e27b1d |
if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0) |
916e40b5 |
return ret; |
348efc18 |
/* make the palette available on the way out */ |
76e27b1d |
pal = (uint32_t*)smk->pic->data[1]; |
b2af057a |
bytestream2_init(&gb2, avpkt->data, avpkt->size);
flags = bytestream2_get_byteu(&gb2); |
76e27b1d |
smk->pic->palette_has_changed = flags & 1;
smk->pic->key_frame = !!(flags & 2);
if (smk->pic->key_frame)
smk->pic->pict_type = AV_PICTURE_TYPE_I; |
348efc18 |
else |
76e27b1d |
smk->pic->pict_type = AV_PICTURE_TYPE_P; |
348efc18 |
|
3e368d72 |
for(i = 0; i < 256; i++) |
b12d92ef |
*pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2); |
348efc18 |
last_reset(smk->mmap_tbl, smk->mmap_last);
last_reset(smk->mclr_tbl, smk->mclr_last);
last_reset(smk->full_tbl, smk->full_last);
last_reset(smk->type_tbl, smk->type_last); |
1b99514d |
if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
return ret; |
348efc18 |
blk = 0;
bw = avctx->width >> 2;
bh = avctx->height >> 2;
blocks = bw * bh; |
76e27b1d |
stride = smk->pic->linesize[0]; |
348efc18 |
while(blk < blocks) {
int type, run, mode;
uint16_t pix;
type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
run = block_runs[(type >> 2) & 0x3F];
switch(type & 3){
case SMK_BLK_MONO:
while(run-- && blk < blocks){
int clr, map;
int hi, lo;
clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last);
map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last); |
76e27b1d |
out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; |
348efc18 |
hi = clr >> 8;
lo = clr & 0xFF;
for(i = 0; i < 4; i++) {
if(map & 1) out[0] = hi; else out[0] = lo;
if(map & 2) out[1] = hi; else out[1] = lo;
if(map & 4) out[2] = hi; else out[2] = lo;
if(map & 8) out[3] = hi; else out[3] = lo;
map >>= 4;
out += stride;
}
blk++;
}
break;
case SMK_BLK_FULL:
mode = 0; |
103eee53 |
if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes |
348efc18 |
if(get_bits1(&gb)) mode = 1;
else if(get_bits1(&gb)) mode = 2;
}
while(run-- && blk < blocks){ |
76e27b1d |
out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; |
348efc18 |
switch(mode){
case 0:
for(i = 0; i < 4; i++) {
pix = smk_get_code(&gb, smk->full_tbl, smk->full_last); |
2c124cb6 |
AV_WL16(out+2,pix); |
348efc18 |
pix = smk_get_code(&gb, smk->full_tbl, smk->full_last); |
2c124cb6 |
AV_WL16(out,pix); |
348efc18 |
out += stride;
}
break;
case 1:
pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
out[0] = out[1] = pix & 0xFF;
out[2] = out[3] = pix >> 8;
out += stride;
out[0] = out[1] = pix & 0xFF;
out[2] = out[3] = pix >> 8;
out += stride;
pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
out[0] = out[1] = pix & 0xFF;
out[2] = out[3] = pix >> 8;
out += stride;
out[0] = out[1] = pix & 0xFF;
out[2] = out[3] = pix >> 8;
break;
case 2:
for(i = 0; i < 2; i++) {
uint16_t pix1, pix2;
pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last); |
40a19c44 |
pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last); |
2c124cb6 |
AV_WL16(out,pix1);
AV_WL16(out+2,pix2); |
348efc18 |
out += stride; |
2c124cb6 |
AV_WL16(out,pix1);
AV_WL16(out+2,pix2); |
348efc18 |
out += stride;
}
break;
}
blk++;
}
break;
case SMK_BLK_SKIP:
while(run-- && blk < blocks)
blk++;
break;
case SMK_BLK_FILL:
mode = type >> 8;
while(run-- && blk < blocks){
uint32_t col; |
76e27b1d |
out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; |
348efc18 |
col = mode * 0x01010101;
for(i = 0; i < 4; i++) {
*((uint32_t*)out) = col;
out += stride;
}
blk++;
}
break;
}
}
|
76e27b1d |
if ((ret = av_frame_ref(data, smk->pic)) < 0) |
759001c5 |
return ret;
|
df9b9567 |
*got_frame = 1; |
348efc18 |
/* always report that the buffer was completely consumed */ |
b2af057a |
return avpkt->size; |
348efc18 |
}
/*
* |
f3d57dc6 |
* Uninit smacker decoder
*
*/
static av_cold int decode_end(AVCodecContext *avctx)
{
SmackVContext * const smk = avctx->priv_data;
av_freep(&smk->mmap_tbl);
av_freep(&smk->mclr_tbl);
av_freep(&smk->full_tbl);
av_freep(&smk->type_tbl);
|
af539319 |
av_frame_free(&smk->pic); |
f3d57dc6 |
return 0;
}
/*
* |
348efc18 |
* Init smacker decoder
*
*/ |
98a6fff9 |
static av_cold int decode_init(AVCodecContext *avctx) |
348efc18 |
{ |
e4141433 |
SmackVContext * const c = avctx->priv_data; |
a1dbe49d |
int ret; |
348efc18 |
c->avctx = avctx;
|
716d413c |
avctx->pix_fmt = AV_PIX_FMT_PAL8; |
348efc18 |
|
84099f51 |
c->pic = av_frame_alloc();
if (!c->pic)
return AVERROR(ENOMEM); |
348efc18 |
/* decode huffman trees from extradata */
if(avctx->extradata_size < 16){
av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n"); |
96938216 |
decode_end(avctx); |
916e40b5 |
return AVERROR(EINVAL); |
348efc18 |
}
|
a1dbe49d |
ret = decode_header_trees(c); |
af539319 |
if (ret < 0) { |
f3d57dc6 |
decode_end(avctx); |
a1dbe49d |
return ret; |
f3d57dc6 |
} |
348efc18 |
return 0;
}
|
98a6fff9 |
static av_cold int smka_decode_init(AVCodecContext *avctx) |
348efc18 |
{ |
e190e453 |
if (avctx->channels < 1 || avctx->channels > 2) {
av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
return AVERROR(EINVAL);
} |
fbdcdaee |
avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO; |
5d6e4c16 |
avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16; |
0eea2129 |
|
348efc18 |
return 0;
}
/**
* Decode Smacker audio data
*/ |
0eea2129 |
static int smka_decode_frame(AVCodecContext *avctx, void *data,
int *got_frame_ptr, AVPacket *avpkt) |
348efc18 |
{ |
903b62cc |
AVFrame *frame = data; |
7a00bbad |
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; |
348efc18 |
GetBitContext gb; |
a92be9b8 |
HuffContext h[4] = { { 0 } };
VLC vlc[4] = { { 0 } }; |
0eea2129 |
int16_t *samples;
uint8_t *samples8; |
348efc18 |
int val; |
0eea2129 |
int i, res, ret; |
348efc18 |
int unp_size;
int bits, stereo;
int pred[2] = {0, 0};
|
cf044f8b |
if (buf_size <= 4) {
av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
return AVERROR(EINVAL);
}
|
fead30d4 |
unp_size = AV_RL32(buf); |
348efc18 |
|
57c0da6f |
if (unp_size > (1U<<24)) {
av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
return AVERROR_INVALIDDATA;
}
|
1b99514d |
if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
return ret; |
348efc18 |
if(!get_bits1(&gb)){
av_log(avctx, AV_LOG_INFO, "Sound: no data\n"); |
0eea2129 |
*got_frame_ptr = 0; |
348efc18 |
return 1;
}
stereo = get_bits1(&gb);
bits = get_bits1(&gb); |
ff1f89de |
if (stereo ^ (avctx->channels != 1)) {
av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
return AVERROR(EINVAL);
} |
b07a5e9b |
if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) { |
ff1f89de |
av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
return AVERROR(EINVAL);
} |
348efc18 |
|
0eea2129 |
/* get output buffer */ |
903b62cc |
frame->nb_samples = unp_size / (avctx->channels * (bits + 1)); |
1ec94b0f |
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
0eea2129 |
return ret; |
903b62cc |
samples = (int16_t *)frame->data[0];
samples8 = frame->data[0]; |
0eea2129 |
|
348efc18 |
// Initialize
for(i = 0; i < (1 << (bits + stereo)); i++) {
h[i].length = 256;
h[i].maxlength = 0;
h[i].current = 0;
h[i].bits = av_mallocz(256 * 4);
h[i].lengths = av_mallocz(256 * sizeof(int));
h[i].values = av_mallocz(256 * sizeof(int)); |
d002fce2 |
if (!h[i].bits || !h[i].lengths || !h[i].values) {
ret = AVERROR(ENOMEM);
goto error;
} |
7ae7300e |
skip_bits1(&gb); |
bbb2945f |
if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) { |
d002fce2 |
ret = AVERROR_INVALIDDATA;
goto error; |
bbb2945f |
} |
7ae7300e |
skip_bits1(&gb); |
76fabb45 |
if(h[i].current > 1) {
res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length, |
348efc18 |
h[i].lengths, sizeof(int), sizeof(int),
h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE); |
76fabb45 |
if(res < 0) {
av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); |
d002fce2 |
ret = AVERROR_INVALIDDATA;
goto error; |
76fabb45 |
} |
348efc18 |
}
} |
268bb885 |
/* this codec relies on wraparound instead of clipping audio */ |
348efc18 |
if(bits) { //decode 16-bit data |
c72e05ba |
for(i = stereo; i >= 0; i--) |
12cbbbb4 |
pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16); |
d67e7492 |
for(i = 0; i <= stereo; i++) |
c72e05ba |
*samples++ = pred[i]; |
d67e7492 |
for(; i < unp_size / 2; i++) { |
d0f79271 |
if(get_bits_left(&gb)<0) |
c1947015 |
return AVERROR_INVALIDDATA; |
348efc18 |
if(i & stereo) { |
76fabb45 |
if(vlc[2].table)
res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
else
res = 0; |
71d3c25a |
if (res < 0) {
av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
return AVERROR_INVALIDDATA;
} |
76fabb45 |
val = h[2].values[res];
if(vlc[3].table)
res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
else
res = 0; |
71d3c25a |
if (res < 0) {
av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
return AVERROR_INVALIDDATA;
} |
76fabb45 |
val |= h[3].values[res] << 8; |
c86c3c80 |
pred[1] += sign_extend(val, 16); |
2211c762 |
*samples++ = pred[1]; |
348efc18 |
} else { |
76fabb45 |
if(vlc[0].table)
res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
else
res = 0; |
71d3c25a |
if (res < 0) {
av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
return AVERROR_INVALIDDATA;
} |
76fabb45 |
val = h[0].values[res];
if(vlc[1].table)
res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
else
res = 0; |
71d3c25a |
if (res < 0) {
av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
return AVERROR_INVALIDDATA;
} |
76fabb45 |
val |= h[1].values[res] << 8; |
c86c3c80 |
pred[0] += sign_extend(val, 16); |
2211c762 |
*samples++ = pred[0]; |
348efc18 |
}
}
} else { //8-bit data |
c72e05ba |
for(i = stereo; i >= 0; i--)
pred[i] = get_bits(&gb, 8); |
d67e7492 |
for(i = 0; i <= stereo; i++) |
e1dc16ae |
*samples8++ = pred[i]; |
d67e7492 |
for(; i < unp_size; i++) { |
d0f79271 |
if(get_bits_left(&gb)<0) |
c1947015 |
return AVERROR_INVALIDDATA; |
348efc18 |
if(i & stereo){ |
76fabb45 |
if(vlc[1].table)
res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
else
res = 0; |
71d3c25a |
if (res < 0) {
av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
return AVERROR_INVALIDDATA;
} |
c86c3c80 |
pred[1] += sign_extend(h[1].values[res], 8); |
5f30c6c8 |
*samples8++ = pred[1]; |
348efc18 |
} else { |
76fabb45 |
if(vlc[0].table)
res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
else
res = 0; |
71d3c25a |
if (res < 0) {
av_log(avctx, AV_LOG_ERROR, "invalid vlc\n");
return AVERROR_INVALIDDATA;
} |
c86c3c80 |
pred[0] += sign_extend(h[0].values[res], 8); |
5f30c6c8 |
*samples8++ = pred[0]; |
348efc18 |
}
}
}
|
d002fce2 |
*got_frame_ptr = 1;
ret = buf_size;
error: |
348efc18 |
for(i = 0; i < 4; i++) {
if(vlc[i].table) |
e96b4a53 |
ff_free_vlc(&vlc[i]); |
e31a7441 |
av_free(h[i].bits);
av_free(h[i].lengths);
av_free(h[i].values); |
348efc18 |
}
|
d002fce2 |
return ret; |
348efc18 |
}
|
e7e2df27 |
AVCodec ff_smacker_decoder = { |
ec6402b7 |
.name = "smackvid", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("Smacker video"), |
ec6402b7 |
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_SMACKVIDEO, |
ec6402b7 |
.priv_data_size = sizeof(SmackVContext),
.init = decode_init,
.close = decode_end,
.decode = decode_frame,
.capabilities = CODEC_CAP_DR1, |
348efc18 |
};
|
e7e2df27 |
AVCodec ff_smackaud_decoder = { |
ec6402b7 |
.name = "smackaud", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("Smacker audio"), |
ec6402b7 |
.type = AVMEDIA_TYPE_AUDIO, |
36ef5369 |
.id = AV_CODEC_ID_SMACKAUDIO, |
ec6402b7 |
.init = smka_decode_init,
.decode = smka_decode_frame, |
0eea2129 |
.capabilities = CODEC_CAP_DR1, |
348efc18 |
}; |