* Smacker decoder
* Copyright (c) 2006 Konstantin Shishkov
*
- * This library is free software; you can redistribute it and/or
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
- * This library is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
* 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
- * License along with this library; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
*/
/**
- * @file smacker.c
+ * @file
* Smacker decoder
*/
#include <stdio.h>
#include <stdlib.h>
-#include "common.h"
+#include "libavutil/channel_layout.h"
#include "avcodec.h"
+#include "internal.h"
+#include "mathops.h"
-#define ALT_BITSTREAM_READER_LE
-#include "bitstream.h"
+#define BITSTREAM_READER_LE
+#include "get_bits.h"
+#include "bytestream.h"
#define SMKTREE_BITS 9
#define SMK_NODE 0x80000000
*/
typedef struct SmackVContext {
AVCodecContext *avctx;
- AVFrame pic;
+ AVFrame *pic;
int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
*/
static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx)
{
+ if (hc->current + 1 >= hc->length) {
+ av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
+ return -1;
+ }
if(!get_bits1(gb)){ //Leaf
- int val, i1, i2, b1, b2;
- if(hc->current >= hc->length){
- av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
+ int val, i1, i2;
+ 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)
return -1;
- }
- b1 = get_bits_count(gb);
- i1 = get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3);
- b1 = get_bits_count(gb) - b1;
- b2 = get_bits_count(gb);
- i2 = get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3);
- b2 = get_bits_count(gb) - b2;
val = ctx->recode1[i1] | (ctx->recode2[i2] << 8);
if(val == ctx->escapes[0]) {
ctx->last[0] = hc->current;
hc->values[hc->current++] = val;
return 1;
} else { //Node
- int r = 0, t;
+ int r = 0, r_new, t;
t = hc->current++;
r = smacker_decode_bigtree(gb, hc, ctx);
return r;
hc->values[t] = SMK_NODE | r;
r++;
- r += smacker_decode_bigtree(gb, hc, ctx);
- return r;
+ r_new = smacker_decode_bigtree(gb, hc, ctx);
+ if (r_new < 0)
+ return r_new;
+ return r + r_new;
}
}
/**
- * Store large tree as FFmpeg's vlc codes
+ * Store large tree as Libav'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;
- VLC vlc[2];
+ VLC vlc[2] = { { 0 } };
int escapes[3];
DBCtx ctx;
+ int err = 0;
+
+ if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
+ av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
+ return -1;
+ }
tmp1.length = 256;
tmp1.maxlength = 0;
tmp2.bits = av_mallocz(256 * 4);
tmp2.lengths = av_mallocz(256 * sizeof(int));
tmp2.values = av_mallocz(256 * sizeof(int));
-
- memset(&vlc[0], 0, sizeof(VLC));
- memset(&vlc[1], 0, sizeof(VLC));
+ if (!tmp1.bits || !tmp1.lengths || !tmp1.values ||
+ !tmp2.bits || !tmp2.lengths || !tmp2.values) {
+ err = AVERROR(ENOMEM);
+ goto error;
+ }
if(get_bits1(gb)) {
smacker_decode_tree(gb, &tmp1, 0, 0);
- get_bits1(gb);
+ skip_bits1(gb);
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");
- return -1;
+ err = res;
+ goto error;
}
} else {
av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n");
}
if(get_bits1(gb)){
smacker_decode_tree(gb, &tmp2, 0, 0);
- get_bits1(gb);
+ skip_bits1(gb);
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");
- return -1;
+ err = res;
+ goto error;
}
} else {
av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n");
ctx.recode2 = tmp2.values;
ctx.last = last;
- huff.length = ((size + 3) >> 2) + 3;
+ huff.length = ((size + 3) >> 2) + 4;
huff.maxlength = 0;
huff.current = 0;
huff.values = av_mallocz(huff.length * sizeof(int));
+ if (!huff.values) {
+ err = AVERROR(ENOMEM);
+ goto error;
+ }
- smacker_decode_bigtree(gb, &huff, &ctx);
- get_bits1(gb);
+ if (smacker_decode_bigtree(gb, &huff, &ctx) < 0)
+ err = -1;
+ skip_bits1(gb);
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++;
+ 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;
+ }
*recodes = huff.values;
+error:
if(vlc[0].table)
- free_vlc(&vlc[0]);
+ ff_free_vlc(&vlc[0]);
if(vlc[1].table)
- free_vlc(&vlc[1]);
+ ff_free_vlc(&vlc[1]);
av_free(tmp1.bits);
av_free(tmp1.lengths);
av_free(tmp1.values);
av_free(tmp2.lengths);
av_free(tmp2.values);
- return 0;
+ return err;
}
static int decode_header_trees(SmackVContext *smk) {
GetBitContext gb;
int mmap_size, mclr_size, full_size, type_size;
- mmap_size = LE_32(smk->avctx->extradata);
- mclr_size = LE_32(smk->avctx->extradata + 4);
- full_size = LE_32(smk->avctx->extradata + 8);
- type_size = LE_32(smk->avctx->extradata + 12);
+ 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);
init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
smk->mmap_tbl = av_malloc(sizeof(int) * 2);
+ if (!smk->mmap_tbl)
+ return AVERROR(ENOMEM);
smk->mmap_tbl[0] = 0;
smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
} else {
- smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
+ if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))
+ return -1;
}
- if(!get_bits(&gb, 1)) {
+ if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
smk->mclr_tbl = av_malloc(sizeof(int) * 2);
+ if (!smk->mclr_tbl)
+ return AVERROR(ENOMEM);
smk->mclr_tbl[0] = 0;
smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
} else {
- smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
+ if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))
+ return -1;
}
- if(!get_bits(&gb, 1)) {
+ if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
smk->full_tbl = av_malloc(sizeof(int) * 2);
+ if (!smk->full_tbl)
+ return AVERROR(ENOMEM);
smk->full_tbl[0] = 0;
smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
} else {
- smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
+ if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))
+ return -1;
}
- if(!get_bits(&gb, 1)) {
+ if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
smk->type_tbl = av_malloc(sizeof(int) * 2);
+ if (!smk->type_tbl)
+ return AVERROR(ENOMEM);
smk->type_tbl[0] = 0;
smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
} else {
- smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
+ if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))
+ return -1;
}
return 0;
}
-static always_inline void last_reset(int *recode, int *last) {
+static av_always_inline void last_reset(int *recode, int *last) {
recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
}
/* get code and update history */
-static always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
+static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
register int *table = recode;
- int v, b;
+ int v;
- b = get_bits_count(gb);
while(*table & SMK_NODE) {
if(get_bits1(gb))
table += (*table) & (~SMK_NODE);
table++;
}
v = *table;
- b = get_bits_count(gb) - b;
if(v != recode[last[0]]) {
recode[last[2]] = recode[last[1]];
return v;
}
-static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
+static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
+ AVPacket *avpkt)
{
- SmackVContext * const smk = (SmackVContext *)avctx->priv_data;
+ SmackVContext * const smk = avctx->priv_data;
uint8_t *out;
uint32_t *pal;
+ GetByteContext gb2;
GetBitContext gb;
int blocks, blk, bw, bh;
- int i;
+ int i, ret;
int stride;
+ int flags;
- if(buf_size == 769)
+ if (avpkt->size <= 769)
return 0;
- if(smk->pic.data[0])
- avctx->release_buffer(avctx, &smk->pic);
- smk->pic.reference = 1;
- smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
- if(avctx->reget_buffer(avctx, &smk->pic) < 0){
+ if ((ret = ff_reget_buffer(avctx, smk->pic)) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
- return -1;
+ return ret;
}
/* make the palette available on the way out */
- out = buf + 1;
- pal = (uint32_t*)smk->pic.data[1];
- smk->pic.palette_has_changed = buf[0] & 1;
- smk->pic.key_frame = !!(buf[0] & 2);
- if(smk->pic.key_frame)
- smk->pic.pict_type = FF_I_TYPE;
+ pal = (uint32_t*)smk->pic->data[1];
+ bytestream2_init(&gb2, avpkt->data, avpkt->size);
+ flags = bytestream2_get_byteu(&gb2);
+ 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;
else
- smk->pic.pict_type = FF_P_TYPE;
-
- for(i = 0; i < 256; i++) {
- int r, g, b;
- r = *out++;
- g = *out++;
- b = *out++;
- *pal++ = (r << 16) | (g << 8) | b;
- }
+ smk->pic->pict_type = AV_PICTURE_TYPE_P;
+
+ for(i = 0; i < 256; i++)
+ *pal++ = bytestream2_get_be24u(&gb2);
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);
- init_get_bits(&gb, buf + 769, (buf_size - 769) * 8);
+ init_get_bits(&gb, avpkt->data + 769, (avpkt->size - 769) * 8);
blk = 0;
bw = avctx->width >> 2;
bh = avctx->height >> 2;
blocks = bw * bh;
- out = smk->pic.data[0];
- stride = smk->pic.linesize[0];
+ out = smk->pic->data[0];
+ stride = smk->pic->linesize[0];
while(blk < blocks) {
int type, run, mode;
uint16_t pix;
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);
- out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
+ out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
hi = clr >> 8;
lo = clr & 0xFF;
for(i = 0; i < 4; i++) {
break;
case SMK_BLK_FULL:
mode = 0;
- if(avctx->codec_tag != 0) { // In case of Smacker v4 we have three modes
+ if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
if(get_bits1(&gb)) mode = 1;
else if(get_bits1(&gb)) mode = 2;
}
while(run-- && blk < blocks){
- out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
+ out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
switch(mode){
case 0:
for(i = 0; i < 4; i++) {
pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
- out[2] = pix & 0xFF;
- out[3] = pix >> 8;
+ AV_WL16(out+2,pix);
pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
- out[0] = pix & 0xFF;
- out[1] = pix >> 8;
+ AV_WL16(out,pix);
out += stride;
}
break;
case 2:
for(i = 0; i < 2; i++) {
uint16_t pix1, pix2;
- pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
- out[0] = pix1 & 0xFF; out[1] = pix1 >> 8;
- out[2] = pix2 & 0xFF; out[3] = pix2 >> 8;
+ pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
+ AV_WL16(out,pix1);
+ AV_WL16(out+2,pix2);
out += stride;
- out[0] = pix1 & 0xFF; out[1] = pix1 >> 8;
- out[2] = pix2 & 0xFF; out[3] = pix2 >> 8;
+ AV_WL16(out,pix1);
+ AV_WL16(out+2,pix2);
out += stride;
}
break;
mode = type >> 8;
while(run-- && blk < blocks){
uint32_t col;
- out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
+ out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
col = mode * 0x01010101;
for(i = 0; i < 4; i++) {
*((uint32_t*)out) = col;
}
- *data_size = sizeof(AVFrame);
- *(AVFrame*)data = smk->pic;
+ if ((ret = av_frame_ref(data, smk->pic)) < 0)
+ return ret;
+
+ *got_frame = 1;
/* always report that the buffer was completely consumed */
- return buf_size;
+ return avpkt->size;
}
/*
*
- * Init smacker decoder
+ * Uninit smacker decoder
*
*/
-static int decode_init(AVCodecContext *avctx)
+static av_cold int decode_end(AVCodecContext *avctx)
{
- SmackVContext * const c = (SmackVContext *)avctx->priv_data;
-
- c->avctx = avctx;
- avctx->has_b_frames = 0;
+ SmackVContext * const smk = avctx->priv_data;
- c->pic.data[0] = NULL;
-
- if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
- return 1;
- }
-
- avctx->pix_fmt = PIX_FMT_PAL8;
-
-
- /* decode huffman trees from extradata */
- if(avctx->extradata_size < 16){
- av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
- return -1;
- }
-
- decode_header_trees(c);
+ av_freep(&smk->mmap_tbl);
+ av_freep(&smk->mclr_tbl);
+ av_freep(&smk->full_tbl);
+ av_freep(&smk->type_tbl);
+ av_frame_free(&smk->pic);
return 0;
}
-
/*
*
- * Uninit smacker decoder
+ * Init smacker decoder
*
*/
-static int decode_end(AVCodecContext *avctx)
+static av_cold int decode_init(AVCodecContext *avctx)
{
- SmackVContext * const smk = (SmackVContext *)avctx->priv_data;
+ SmackVContext * const c = avctx->priv_data;
- if(smk->mmap_tbl)
- av_free(smk->mmap_tbl);
- if(smk->mclr_tbl)
- av_free(smk->mclr_tbl);
- if(smk->full_tbl)
- av_free(smk->full_tbl);
- if(smk->type_tbl)
- av_free(smk->type_tbl);
+ c->avctx = avctx;
- if (smk->pic.data[0])
- avctx->release_buffer(avctx, &smk->pic);
+ avctx->pix_fmt = AV_PIX_FMT_PAL8;
+
+ c->pic = av_frame_alloc();
+ if (!c->pic)
+ return AVERROR(ENOMEM);
+
+ /* decode huffman trees from extradata */
+ if(avctx->extradata_size < 16){
+ av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
+ return -1;
+ }
+
+ if (decode_header_trees(c)) {
+ decode_end(avctx);
+ return -1;
+ }
return 0;
}
-static int smka_decode_init(AVCodecContext *avctx)
+
+static av_cold int smka_decode_init(AVCodecContext *avctx)
{
+ if (avctx->channels < 1 || avctx->channels > 2) {
+ av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
+ return AVERROR(EINVAL);
+ }
+ avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
+ avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
+
return 0;
}
/**
* Decode Smacker audio data
*/
-static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
+static int smka_decode_frame(AVCodecContext *avctx, void *data,
+ int *got_frame_ptr, AVPacket *avpkt)
{
+ AVFrame *frame = data;
+ const uint8_t *buf = avpkt->data;
+ int buf_size = avpkt->size;
GetBitContext gb;
- HuffContext h[4];
- VLC vlc[4];
- int16_t *samples = data;
+ HuffContext h[4] = { { 0 } };
+ VLC vlc[4] = { { 0 } };
+ int16_t *samples;
+ uint8_t *samples8;
int val;
- int i, res;
+ int i, res, ret;
int unp_size;
int bits, stereo;
int pred[2] = {0, 0};
- unp_size = LE_32(buf);
+ if (buf_size <= 4) {
+ av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
+ return AVERROR(EINVAL);
+ }
+
+ unp_size = AV_RL32(buf);
init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);
if(!get_bits1(&gb)){
av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
- *data_size = 0;
+ *got_frame_ptr = 0;
return 1;
}
stereo = get_bits1(&gb);
bits = get_bits1(&gb);
+ if (stereo ^ (avctx->channels != 1)) {
+ av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
+ return AVERROR(EINVAL);
+ }
+ if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {
+ av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
+ return AVERROR(EINVAL);
+ }
+
+ /* get output buffer */
+ frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
+ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+ return ret;
+ }
+ samples = (int16_t *)frame->data[0];
+ samples8 = frame->data[0];
- memset(vlc, 0, sizeof(VLC) * 4);
- memset(h, 0, sizeof(HuffContext) * 4);
// Initialize
for(i = 0; i < (1 << (bits + stereo)); i++) {
h[i].length = 256;
h[i].bits = av_mallocz(256 * 4);
h[i].lengths = av_mallocz(256 * sizeof(int));
h[i].values = av_mallocz(256 * sizeof(int));
- get_bits1(&gb);
- smacker_decode_tree(&gb, &h[i], 0, 0);
- get_bits1(&gb);
- res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
+ if (!h[i].bits || !h[i].lengths || !h[i].values) {
+ ret = AVERROR(ENOMEM);
+ goto error;
+ }
+ skip_bits1(&gb);
+ if (smacker_decode_tree(&gb, &h[i], 0, 0) < 0) {
+ ret = AVERROR_INVALIDDATA;
+ goto error;
+ }
+ skip_bits1(&gb);
+ if(h[i].current > 1) {
+ res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,
h[i].lengths, sizeof(int), sizeof(int),
h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);
- if(res < 0) {
- av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
- return -1;
+ if(res < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
+ ret = AVERROR_INVALIDDATA;
+ goto error;
+ }
}
}
+ /* this codec relies on wraparound instead of clipping audio */
if(bits) { //decode 16-bit data
- pred[0] = get_bits(&gb, 8);
- pred[0] |= get_bits(&gb, 8);
- if(stereo) {
- pred[1] = get_bits(&gb, 8);
- pred[1] |= get_bits(&gb, 8);
- }
- for(i = 0; i < unp_size / 2; i++) {
+ for(i = stereo; i >= 0; i--)
+ pred[i] = sign_extend(av_bswap16(get_bits(&gb, 16)), 16);
+ for(i = 0; i <= stereo; i++)
+ *samples++ = pred[i];
+ for(; i < unp_size / 2; i++) {
if(i & stereo) {
- val = h[2].values[get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3)];
- val |= (int8_t)h[3].values[get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3)] << 8;
- pred[1] += val;
+ if(vlc[2].table)
+ res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);
+ else
+ res = 0;
+ val = h[2].values[res];
+ if(vlc[3].table)
+ res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);
+ else
+ res = 0;
+ val |= h[3].values[res] << 8;
+ pred[1] += sign_extend(val, 16);
*samples++ = pred[1];
} else {
- val = h[0].values[get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3)];
- val |= (int8_t)h[1].values[get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3)] << 8;
- pred[0] += val;
+ if(vlc[0].table)
+ res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
+ else
+ res = 0;
+ val = h[0].values[res];
+ if(vlc[1].table)
+ res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
+ else
+ res = 0;
+ val |= h[1].values[res] << 8;
+ pred[0] += sign_extend(val, 16);
*samples++ = pred[0];
}
}
} else { //8-bit data
- pred[0] = get_bits(&gb, 8);
- if(stereo)
- pred[1] = get_bits(&gb, 8);
- for(i = 0; i < unp_size; i++) {
+ for(i = stereo; i >= 0; i--)
+ pred[i] = get_bits(&gb, 8);
+ for(i = 0; i <= stereo; i++)
+ *samples8++ = pred[i];
+ for(; i < unp_size; i++) {
if(i & stereo){
- pred[1] += (int8_t)h[1].values[get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3)];
- *samples++ = (pred[1] - 0x80) << 8;
+ if(vlc[1].table)
+ res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);
+ else
+ res = 0;
+ pred[1] += sign_extend(h[1].values[res], 8);
+ *samples8++ = pred[1];
} else {
- pred[0] += (int8_t)h[0].values[get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3)];
- *samples++ = (pred[0] - 0x80) << 8;
+ if(vlc[0].table)
+ res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);
+ else
+ res = 0;
+ pred[0] += sign_extend(h[0].values[res], 8);
+ *samples8++ = pred[0];
}
}
- unp_size *= 2;
}
+ *got_frame_ptr = 1;
+ ret = buf_size;
+
+error:
for(i = 0; i < 4; i++) {
if(vlc[i].table)
- free_vlc(&vlc[i]);
- if(h[i].bits)
- av_free(h[i].bits);
- if(h[i].lengths)
- av_free(h[i].lengths);
- if(h[i].values)
- av_free(h[i].values);
+ ff_free_vlc(&vlc[i]);
+ av_free(h[i].bits);
+ av_free(h[i].lengths);
+ av_free(h[i].values);
}
- *data_size = unp_size;
- return buf_size;
+ return ret;
}
-AVCodec smacker_decoder = {
- "smackvid",
- CODEC_TYPE_VIDEO,
- CODEC_ID_SMACKVIDEO,
- sizeof(SmackVContext),
- decode_init,
- NULL,
- decode_end,
- decode_frame
+AVCodec ff_smacker_decoder = {
+ .name = "smackvid",
+ .long_name = NULL_IF_CONFIG_SMALL("Smacker video"),
+ .type = AVMEDIA_TYPE_VIDEO,
+ .id = AV_CODEC_ID_SMACKVIDEO,
+ .priv_data_size = sizeof(SmackVContext),
+ .init = decode_init,
+ .close = decode_end,
+ .decode = decode_frame,
+ .capabilities = CODEC_CAP_DR1,
};
-AVCodec smackaud_decoder = {
- "smackaud",
- CODEC_TYPE_AUDIO,
- CODEC_ID_SMACKAUDIO,
- 0,
- smka_decode_init,
- NULL,
- NULL,
- smka_decode_frame
+AVCodec ff_smackaud_decoder = {
+ .name = "smackaud",
+ .long_name = NULL_IF_CONFIG_SMALL("Smacker audio"),
+ .type = AVMEDIA_TYPE_AUDIO,
+ .id = AV_CODEC_ID_SMACKAUDIO,
+ .init = smka_decode_init,
+ .decode = smka_decode_frame,
+ .capabilities = CODEC_CAP_DR1,
};
-