2006-03-21 19:27:47 +02:00
|
|
|
/*
|
|
|
|
* Smacker decoder
|
|
|
|
* Copyright (c) 2006 Konstantin Shishkov
|
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-03-21 19:27:47 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-03-21 19:27:47 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-03-21 19:27:47 +02:00
|
|
|
* 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
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-03-21 19:27:47 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2006-03-21 19:27:47 +02:00
|
|
|
* Smacker decoder
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Based on http://wiki.multimedia.cx/index.php?title=Smacker
|
|
|
|
*/
|
|
|
|
|
2022-08-24 21:28:16 +02:00
|
|
|
#include <stddef.h>
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2012-11-10 18:00:00 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2016-06-04 15:07:30 +02:00
|
|
|
#include "avcodec.h"
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
#define SMKTREE_BITS 9
|
|
|
|
#define SMK_NODE 0x80000000
|
|
|
|
|
2020-07-25 19:00:28 +02:00
|
|
|
#define SMKTREE_DECODE_MAX_RECURSION FFMIN(32, 3 * SMKTREE_BITS)
|
2016-11-19 15:21:11 +02:00
|
|
|
#define SMKTREE_DECODE_BIG_MAX_RECURSION 500
|
2016-02-16 19:02:14 +02:00
|
|
|
|
2020-07-25 19:00:28 +02:00
|
|
|
/* The maximum possible unchecked overread happens in decode_header_trees:
|
|
|
|
* Decoding the MMAP tree can overread by 6 * SMKTREE_BITS + 1, followed by
|
|
|
|
* three get_bits1, followed by at most 2 + 3 * 16 read bits when reading
|
|
|
|
* the TYPE tree before the next check. 64 is because of 64 bit reads. */
|
|
|
|
#if (6 * SMKTREE_BITS + 1 + 3 + (2 + 3 * 16) + 64) <= 8 * AV_INPUT_BUFFER_PADDING_SIZE
|
|
|
|
#define UNCHECKED_BITSTREAM_READER 1
|
|
|
|
#endif
|
|
|
|
#define BITSTREAM_READER_LE
|
|
|
|
#include "bytestream.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 20:49:25 +02:00
|
|
|
#include "decode.h"
|
2020-07-25 19:00:28 +02:00
|
|
|
#include "get_bits.h"
|
|
|
|
|
2006-03-21 19:27:47 +02:00
|
|
|
typedef struct SmackVContext {
|
|
|
|
AVCodecContext *avctx;
|
2013-07-26 17:41:38 +03:00
|
|
|
AVFrame *pic;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl;
|
|
|
|
int mmap_last[3], mclr_last[3], full_last[3], type_last[3];
|
|
|
|
} SmackVContext;
|
|
|
|
|
2020-10-25 11:24:31 +02:00
|
|
|
typedef struct HuffEntry {
|
|
|
|
uint8_t value;
|
|
|
|
uint8_t length;
|
|
|
|
} HuffEntry;
|
|
|
|
|
2006-03-21 19:27:47 +02:00
|
|
|
/**
|
|
|
|
* Context used for code reconstructing
|
|
|
|
*/
|
|
|
|
typedef struct HuffContext {
|
|
|
|
int current;
|
2020-10-25 11:24:31 +02:00
|
|
|
HuffEntry entries[256];
|
2006-03-21 19:27:47 +02:00
|
|
|
} HuffContext;
|
|
|
|
|
|
|
|
/* common parameters used for decode_bigtree */
|
|
|
|
typedef struct DBCtx {
|
2020-06-25 20:46:00 +02:00
|
|
|
int current, length;
|
|
|
|
int *values;
|
2006-03-21 19:27:47 +02:00
|
|
|
VLC *v1, *v2;
|
2020-07-24 19:57:56 +02:00
|
|
|
uint8_t vals[2];
|
2006-03-21 19:27:47 +02:00
|
|
|
int escapes[3];
|
|
|
|
int *last;
|
|
|
|
} 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
|
2020-07-25 19:00:28 +02:00
|
|
|
*
|
|
|
|
* Can read SMKTREE_DECODE_MAX_RECURSION before the first check;
|
|
|
|
* does not overread gb on success.
|
2006-03-21 19:27:47 +02:00
|
|
|
*/
|
2023-04-27 11:11:27 +02:00
|
|
|
static int smacker_decode_tree(AVCodecContext *avctx, GetBitContext *gb, HuffContext *hc, int length)
|
2006-03-21 19:27:47 +02:00
|
|
|
{
|
2017-11-12 06:08:10 +02:00
|
|
|
if (length > SMKTREE_DECODE_MAX_RECURSION || length > 3 * SMKTREE_BITS) {
|
2023-04-27 11:11:27 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Maximum tree recursion level exceeded.\n");
|
2012-11-11 13:04:04 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-06-21 17:01:52 +03:00
|
|
|
}
|
2012-06-21 17:01:52 +03:00
|
|
|
|
2006-03-21 19:27:47 +02:00
|
|
|
if(!get_bits1(gb)){ //Leaf
|
2020-07-24 21:28:34 +02:00
|
|
|
if (hc->current >= 256) {
|
2023-04-27 11:11:27 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Tree size exceeded!\n");
|
2012-11-11 13:04:04 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
2020-07-25 19:00:28 +02:00
|
|
|
if (get_bits_left(gb) < 8)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2020-10-25 11:24:31 +02:00
|
|
|
hc->entries[hc->current++] = (HuffEntry){ get_bits(gb, 8), length };
|
2006-03-21 19:27:47 +02:00
|
|
|
return 0;
|
|
|
|
} else { //Node
|
|
|
|
int r;
|
|
|
|
length++;
|
2023-04-27 11:11:27 +02:00
|
|
|
r = smacker_decode_tree(avctx, gb, hc, length);
|
2006-03-21 19:27:47 +02:00
|
|
|
if(r)
|
|
|
|
return r;
|
2023-04-27 11:11:27 +02:00
|
|
|
return smacker_decode_tree(avctx, gb, hc, length);
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode header tree
|
2020-07-25 19:00:28 +02:00
|
|
|
*
|
|
|
|
* Checks before the first read, can overread by 6 * SMKTREE_BITS on success.
|
2006-03-21 19:27:47 +02:00
|
|
|
*/
|
2023-04-27 11:11:27 +02:00
|
|
|
static int smacker_decode_bigtree(AVCodecContext *avctx, GetBitContext *gb, DBCtx *ctx, int length)
|
2006-03-21 19:27:47 +02:00
|
|
|
{
|
2016-11-19 15:21:11 +02:00
|
|
|
// Larger length can cause segmentation faults due to too deep recursion.
|
|
|
|
if (length > SMKTREE_DECODE_BIG_MAX_RECURSION) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Maximum bigtree recursion level exceeded.\n");
|
2016-11-19 15:21:11 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2016-11-19 15:21:11 +02:00
|
|
|
|
2020-06-25 20:46:00 +02:00
|
|
|
if (ctx->current >= ctx->length) {
|
2012-01-26 03:12:42 +03:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n");
|
2012-11-11 13:04:04 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-01-26 03:12:42 +03:00
|
|
|
}
|
2020-07-25 19:00:28 +02:00
|
|
|
if (get_bits_left(gb) <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2006-03-21 19:27:47 +02:00
|
|
|
if(!get_bits1(gb)){ //Leaf
|
2012-07-01 16:56:16 +03:00
|
|
|
int val, i1, i2;
|
2020-07-24 19:57:56 +02:00
|
|
|
i1 = ctx->v1->table ? get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3)
|
|
|
|
: ctx->vals[0];
|
|
|
|
i2 = ctx->v2->table ? get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3)
|
|
|
|
: ctx->vals[1];
|
|
|
|
val = i1 | (i2 << 8);
|
2006-03-21 19:27:47 +02:00
|
|
|
if(val == ctx->escapes[0]) {
|
2020-06-25 20:46:00 +02:00
|
|
|
ctx->last[0] = ctx->current;
|
2006-03-21 19:27:47 +02:00
|
|
|
val = 0;
|
|
|
|
} else if(val == ctx->escapes[1]) {
|
2020-06-25 20:46:00 +02:00
|
|
|
ctx->last[1] = ctx->current;
|
2006-03-21 19:27:47 +02:00
|
|
|
val = 0;
|
|
|
|
} else if(val == ctx->escapes[2]) {
|
2020-06-25 20:46:00 +02:00
|
|
|
ctx->last[2] = ctx->current;
|
2006-03-21 19:27:47 +02:00
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:46:00 +02:00
|
|
|
ctx->values[ctx->current++] = val;
|
2006-03-21 19:27:47 +02:00
|
|
|
return 1;
|
|
|
|
} else { //Node
|
2012-01-26 03:12:42 +03:00
|
|
|
int r = 0, r_new, t;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2020-06-25 20:46:00 +02:00
|
|
|
t = ctx->current++;
|
2023-04-27 11:11:27 +02:00
|
|
|
r = smacker_decode_bigtree(avctx, gb, ctx, length + 1);
|
2006-03-21 19:27:47 +02:00
|
|
|
if(r < 0)
|
|
|
|
return r;
|
2020-06-25 20:46:00 +02:00
|
|
|
ctx->values[t] = SMK_NODE | r;
|
2006-03-21 19:27:47 +02:00
|
|
|
r++;
|
2023-04-27 11:11:27 +02:00
|
|
|
r_new = smacker_decode_bigtree(avctx, gb, ctx, length + 1);
|
2012-01-26 03:12:42 +03:00
|
|
|
if (r_new < 0)
|
|
|
|
return r_new;
|
|
|
|
return r + r_new;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store large tree as FFmpeg's vlc codes
|
2020-07-25 19:00:28 +02:00
|
|
|
*
|
|
|
|
* Can read FFMAX(1 + SMKTREE_DECODE_MAX_RECURSION, 2 + 3 * 16) bits
|
|
|
|
* before the first check; can overread by 6 * SMKTREE_BITS + 1 on success.
|
2006-03-21 19:27:47 +02:00
|
|
|
*/
|
|
|
|
static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size)
|
|
|
|
{
|
2011-06-06 15:13:02 +03:00
|
|
|
VLC vlc[2] = { { 0 } };
|
2006-03-21 19:27:47 +02:00
|
|
|
int escapes[3];
|
|
|
|
DBCtx ctx;
|
2020-07-25 13:29:52 +02:00
|
|
|
int err;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2006-05-13 13:45:26 +03:00
|
|
|
if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow
|
|
|
|
av_log(smk->avctx, AV_LOG_ERROR, "size too large\n");
|
2012-11-11 13:04:04 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-05-13 13:45:26 +03:00
|
|
|
}
|
|
|
|
|
2020-07-25 08:21:37 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2020-07-24 21:28:34 +02:00
|
|
|
HuffContext h;
|
|
|
|
h.current = 0;
|
2020-07-25 08:21:37 +02:00
|
|
|
if (!get_bits1(gb)) {
|
2020-07-24 19:57:56 +02:00
|
|
|
ctx.vals[i] = 0;
|
2020-07-25 08:21:37 +02:00
|
|
|
av_log(smk->avctx, AV_LOG_ERROR, "Skipping %s bytes tree\n",
|
|
|
|
i ? "high" : "low");
|
|
|
|
continue;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
2023-04-27 11:11:27 +02:00
|
|
|
err = smacker_decode_tree(smk->avctx, gb, &h, 0);
|
2020-07-25 13:29:52 +02:00
|
|
|
if (err < 0)
|
2013-09-12 16:44:32 +03:00
|
|
|
goto error;
|
2007-08-09 03:57:36 +03:00
|
|
|
skip_bits1(gb);
|
2020-07-24 21:28:34 +02:00
|
|
|
if (h.current > 1) {
|
2020-10-25 11:24:31 +02:00
|
|
|
err = ff_init_vlc_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
|
|
|
|
&h.entries[0].length, sizeof(*h.entries),
|
|
|
|
&h.entries[0].value, sizeof(*h.entries), 1,
|
|
|
|
0, INIT_VLC_OUTPUT_LE, smk->avctx);
|
2020-07-25 13:29:52 +02:00
|
|
|
if (err < 0) {
|
2012-11-25 04:36:34 +03:00
|
|
|
av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
|
2013-09-12 12:55:08 +03:00
|
|
|
goto error;
|
2012-11-25 04:36:34 +03:00
|
|
|
}
|
2020-07-24 19:57:56 +02:00
|
|
|
} else
|
2020-10-25 11:24:31 +02:00
|
|
|
ctx.vals[i] = h.entries[0].value;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
|
|
|
|
2012-09-30 17:49:08 +03:00
|
|
|
escapes[0] = get_bits(gb, 16);
|
|
|
|
escapes[1] = get_bits(gb, 16);
|
|
|
|
escapes[2] = get_bits(gb, 16);
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
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.last = last;
|
2020-06-25 20:46:00 +02:00
|
|
|
ctx.length = (size + 3) >> 2;
|
|
|
|
ctx.current = 0;
|
|
|
|
ctx.values = av_malloc_array(ctx.length + 3, sizeof(ctx.values[0]));
|
|
|
|
if (!ctx.values) {
|
2013-09-11 15:35:19 +03:00
|
|
|
err = AVERROR(ENOMEM);
|
|
|
|
goto error;
|
|
|
|
}
|
2020-06-25 20:46:00 +02:00
|
|
|
*recodes = ctx.values;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2023-04-27 11:11:27 +02:00
|
|
|
err = smacker_decode_bigtree(smk->avctx, gb, &ctx, 0);
|
2020-07-25 13:29:52 +02:00
|
|
|
if (err < 0)
|
2020-07-25 13:05:06 +02:00
|
|
|
goto error;
|
2007-08-09 03:57:36 +03:00
|
|
|
skip_bits1(gb);
|
2020-06-25 20:46:00 +02:00
|
|
|
if (ctx.last[0] == -1) ctx.last[0] = ctx.current++;
|
|
|
|
if (ctx.last[1] == -1) ctx.last[1] = ctx.current++;
|
|
|
|
if (ctx.last[2] == -1) ctx.last[2] = ctx.current++;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2020-07-25 13:29:52 +02:00
|
|
|
err = 0;
|
2013-09-11 15:35:19 +03:00
|
|
|
error:
|
2020-07-25 08:21:37 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2020-07-26 09:13:16 +02:00
|
|
|
ff_free_vlc(&vlc[i]);
|
2020-07-25 08:21:37 +02:00
|
|
|
}
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2012-01-26 03:12:42 +03:00
|
|
|
return err;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_header_trees(SmackVContext *smk) {
|
|
|
|
GetBitContext gb;
|
2013-06-29 17:02:43 +03:00
|
|
|
int mmap_size, mclr_size, full_size, type_size, ret;
|
2019-08-08 22:38:11 +02:00
|
|
|
int skip = 0;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2007-01-20 00:12:59 +02:00
|
|
|
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);
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2015-09-04 01:18:13 +02:00
|
|
|
ret = init_get_bits8(&gb, smk->avctx->extradata + 16, smk->avctx->extradata_size - 16);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
if(!get_bits1(&gb)) {
|
2019-08-08 22:38:11 +02:00
|
|
|
skip ++;
|
2006-03-21 19:27:47 +02:00
|
|
|
av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
|
|
|
|
smk->mmap_tbl = av_malloc(sizeof(int) * 2);
|
2013-09-11 15:35:19 +03:00
|
|
|
if (!smk->mmap_tbl)
|
|
|
|
return AVERROR(ENOMEM);
|
2006-03-21 19:27:47 +02:00
|
|
|
smk->mmap_tbl[0] = 0;
|
|
|
|
smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
|
|
|
|
} else {
|
2013-06-29 17:02:43 +03:00
|
|
|
ret = smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
2007-08-09 03:13:31 +03:00
|
|
|
if(!get_bits1(&gb)) {
|
2019-08-08 22:38:11 +02:00
|
|
|
skip ++;
|
2006-03-21 19:27:47 +02:00
|
|
|
av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
|
|
|
|
smk->mclr_tbl = av_malloc(sizeof(int) * 2);
|
2013-09-11 15:35:19 +03:00
|
|
|
if (!smk->mclr_tbl)
|
|
|
|
return AVERROR(ENOMEM);
|
2006-03-21 19:27:47 +02:00
|
|
|
smk->mclr_tbl[0] = 0;
|
|
|
|
smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
|
|
|
|
} else {
|
2013-06-29 17:02:43 +03:00
|
|
|
ret = smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
2007-08-09 03:13:31 +03:00
|
|
|
if(!get_bits1(&gb)) {
|
2019-08-08 22:38:11 +02:00
|
|
|
skip ++;
|
2006-03-21 19:27:47 +02:00
|
|
|
av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
|
|
|
|
smk->full_tbl = av_malloc(sizeof(int) * 2);
|
2013-09-11 15:35:19 +03:00
|
|
|
if (!smk->full_tbl)
|
|
|
|
return AVERROR(ENOMEM);
|
2006-03-21 19:27:47 +02:00
|
|
|
smk->full_tbl[0] = 0;
|
|
|
|
smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
|
|
|
|
} else {
|
2013-06-29 17:02:43 +03:00
|
|
|
ret = smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
2007-08-09 03:13:31 +03:00
|
|
|
if(!get_bits1(&gb)) {
|
2019-08-08 22:38:11 +02:00
|
|
|
skip ++;
|
2006-03-21 19:27:47 +02:00
|
|
|
av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
|
|
|
|
smk->type_tbl = av_malloc(sizeof(int) * 2);
|
2013-09-11 15:35:19 +03:00
|
|
|
if (!smk->type_tbl)
|
|
|
|
return AVERROR(ENOMEM);
|
2006-03-21 19:27:47 +02:00
|
|
|
smk->type_tbl[0] = 0;
|
|
|
|
smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
|
|
|
|
} else {
|
2013-06-29 17:02:43 +03:00
|
|
|
ret = smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
2020-07-25 19:00:28 +02:00
|
|
|
if (skip == 4 || get_bits_left(&gb) < 0)
|
2019-08-08 22:38:11 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-08 02:35:08 +02:00
|
|
|
static av_always_inline void last_reset(int *recode, int *last) {
|
2006-03-21 19:27:47 +02:00
|
|
|
recode[last[0]] = recode[last[1]] = recode[last[2]] = 0;
|
|
|
|
}
|
|
|
|
|
2020-07-25 19:00:28 +02:00
|
|
|
/* Get code and update history.
|
|
|
|
* Checks before reading, does not overread. */
|
2006-12-08 02:35:08 +02:00
|
|
|
static av_always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) {
|
2006-03-21 19:27:47 +02:00
|
|
|
register int *table = recode;
|
2012-01-26 03:22:05 +03:00
|
|
|
int v;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
while(*table & SMK_NODE) {
|
2020-01-01 21:43:05 +02:00
|
|
|
if (get_bits_left(gb) < 1)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2006-03-21 19:27:47 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
2006-03-21 19:27:47 +02:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
SmackVContext * const smk = avctx->priv_data;
|
2006-03-21 19:27:47 +02:00
|
|
|
uint8_t *out;
|
|
|
|
uint32_t *pal;
|
2012-03-25 03:37:43 +03:00
|
|
|
GetByteContext gb2;
|
2006-03-21 19:27:47 +02:00
|
|
|
GetBitContext gb;
|
|
|
|
int blocks, blk, bw, bh;
|
2012-10-15 15:08:24 +03:00
|
|
|
int i, ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
int stride;
|
2012-03-25 03:37:43 +03:00
|
|
|
int flags;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2012-03-25 03:37:43 +03:00
|
|
|
if (avpkt->size <= 769)
|
2012-10-15 15:08:24 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2019-08-30 16:37:25 +02:00
|
|
|
if ((ret = ff_reget_buffer(avctx, smk->pic, 0)) < 0)
|
2012-10-15 15:08:24 +03:00
|
|
|
return ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
/* make the palette available on the way out */
|
2013-07-26 17:41:38 +03:00
|
|
|
pal = (uint32_t*)smk->pic->data[1];
|
2012-03-25 03:37:43 +03:00
|
|
|
bytestream2_init(&gb2, avpkt->data, avpkt->size);
|
|
|
|
flags = bytestream2_get_byteu(&gb2);
|
2023-05-18 03:39:57 +02:00
|
|
|
#if FF_API_PALETTE_HAS_CHANGED
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2013-07-26 17:41:38 +03:00
|
|
|
smk->pic->palette_has_changed = flags & 1;
|
2023-05-18 03:39:57 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2023-04-12 18:58:54 +02:00
|
|
|
if (flags & 2) {
|
|
|
|
smk->pic->flags |= AV_FRAME_FLAG_KEY;
|
2013-07-26 17:41:38 +03:00
|
|
|
smk->pic->pict_type = AV_PICTURE_TYPE_I;
|
2023-04-12 18:58:54 +02:00
|
|
|
} else {
|
|
|
|
smk->pic->flags &= ~AV_FRAME_FLAG_KEY;
|
2013-07-26 17:41:38 +03:00
|
|
|
smk->pic->pict_type = AV_PICTURE_TYPE_P;
|
2023-04-12 18:58:54 +02:00
|
|
|
}
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2008-02-03 00:31:11 +02:00
|
|
|
for(i = 0; i < 256; i++)
|
2012-10-14 04:17:13 +03:00
|
|
|
*pal++ = 0xFFU << 24 | bytestream2_get_be24u(&gb2);
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
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);
|
2013-09-12 19:34:54 +03:00
|
|
|
if ((ret = init_get_bits8(&gb, avpkt->data + 769, avpkt->size - 769)) < 0)
|
|
|
|
return ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
blk = 0;
|
|
|
|
bw = avctx->width >> 2;
|
|
|
|
bh = avctx->height >> 2;
|
|
|
|
blocks = bw * bh;
|
2013-07-26 17:41:38 +03:00
|
|
|
stride = smk->pic->linesize[0];
|
2006-03-21 19:27:47 +02:00
|
|
|
while(blk < blocks) {
|
|
|
|
int type, run, mode;
|
|
|
|
uint16_t pix;
|
|
|
|
|
|
|
|
type = smk_get_code(&gb, smk->type_tbl, smk->type_last);
|
2020-01-01 21:43:05 +02:00
|
|
|
if (type < 0)
|
|
|
|
return type;
|
2006-03-21 19:27:47 +02:00
|
|
|
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);
|
2013-07-26 17:41:38 +03:00
|
|
|
out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
|
2006-03-21 19:27:47 +02:00
|
|
|
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;
|
2006-05-17 06:03:53 +03:00
|
|
|
if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes
|
2020-10-02 10:54:31 +02:00
|
|
|
if (get_bits_left(&gb) < 1)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2006-03-21 19:27:47 +02:00
|
|
|
if(get_bits1(&gb)) mode = 1;
|
|
|
|
else if(get_bits1(&gb)) mode = 2;
|
|
|
|
}
|
|
|
|
while(run-- && blk < blocks){
|
2013-07-26 17:41:38 +03:00
|
|
|
out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
|
2006-03-21 19:27:47 +02:00
|
|
|
switch(mode){
|
|
|
|
case 0:
|
|
|
|
for(i = 0; i < 4; i++) {
|
|
|
|
pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
|
2007-06-02 04:41:07 +03:00
|
|
|
AV_WL16(out+2,pix);
|
2006-03-21 19:27:47 +02:00
|
|
|
pix = smk_get_code(&gb, smk->full_tbl, smk->full_last);
|
2007-06-02 04:41:07 +03:00
|
|
|
AV_WL16(out,pix);
|
2006-03-21 19:27:47 +02:00
|
|
|
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);
|
2008-12-15 19:51:10 +02:00
|
|
|
pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last);
|
2007-06-02 04:41:07 +03:00
|
|
|
AV_WL16(out,pix1);
|
|
|
|
AV_WL16(out+2,pix2);
|
2006-03-21 19:27:47 +02:00
|
|
|
out += stride;
|
2007-06-02 04:41:07 +03:00
|
|
|
AV_WL16(out,pix1);
|
|
|
|
AV_WL16(out+2,pix2);
|
2006-03-21 19:27:47 +02:00
|
|
|
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;
|
2013-07-26 17:41:38 +03:00
|
|
|
out = smk->pic->data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4;
|
2019-09-06 10:55:26 +02:00
|
|
|
col = mode * 0x01010101U;
|
2006-03-21 19:27:47 +02:00
|
|
|
for(i = 0; i < 4; i++) {
|
|
|
|
*((uint32_t*)out) = col;
|
|
|
|
out += stride;
|
|
|
|
}
|
|
|
|
blk++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
if ((ret = av_frame_ref(rframe, smk->pic)) < 0)
|
2012-11-21 23:34:46 +03:00
|
|
|
return ret;
|
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
/* always report that the buffer was completely consumed */
|
2012-03-25 03:37:43 +03:00
|
|
|
return avpkt->size;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-11 15:55:18 +03:00
|
|
|
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);
|
|
|
|
|
2013-09-12 13:57:58 +03:00
|
|
|
av_frame_free(&smk->pic);
|
2013-09-11 15:55:18 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int decode_init(AVCodecContext *avctx)
|
2006-03-21 19:27:47 +02:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
SmackVContext * const c = avctx->priv_data;
|
2013-06-29 17:02:43 +03:00
|
|
|
int ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
c->avctx = avctx;
|
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2013-11-09 12:14:46 +03:00
|
|
|
c->pic = av_frame_alloc();
|
|
|
|
if (!c->pic)
|
|
|
|
return AVERROR(ENOMEM);
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
/* decode huffman trees from extradata */
|
2020-07-25 19:00:28 +02:00
|
|
|
if (avctx->extradata_size <= 16){
|
2006-03-21 19:27:47 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n");
|
2012-10-15 15:08:24 +03:00
|
|
|
return AVERROR(EINVAL);
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
|
|
|
|
2013-06-29 17:02:43 +03:00
|
|
|
ret = decode_header_trees(c);
|
2013-09-12 13:57:58 +03:00
|
|
|
if (ret < 0) {
|
2013-06-29 17:02:43 +03:00
|
|
|
return ret;
|
2013-09-11 15:55:18 +03:00
|
|
|
}
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int smka_decode_init(AVCodecContext *avctx)
|
2006-03-21 19:27:47 +02:00
|
|
|
{
|
2013-05-07 08:20:32 +03:00
|
|
|
int channels = avctx->ch_layout.nb_channels;
|
|
|
|
if (channels < 1 || channels > 2) {
|
2011-09-21 18:37:51 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
|
2017-05-30 18:12:48 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-09-21 18:37:51 +03:00
|
|
|
}
|
2013-05-07 08:20:32 +03:00
|
|
|
av_channel_layout_uninit(&avctx->ch_layout);
|
|
|
|
av_channel_layout_default(&avctx->ch_layout, channels);
|
2010-11-12 13:04:40 +02:00
|
|
|
avctx->sample_fmt = avctx->bits_per_coded_sample == 8 ? AV_SAMPLE_FMT_U8 : AV_SAMPLE_FMT_S16;
|
2011-09-06 19:17:45 +03:00
|
|
|
|
2006-03-21 19:27:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode Smacker audio data
|
|
|
|
*/
|
2022-03-30 21:33:24 +02:00
|
|
|
static int smka_decode_frame(AVCodecContext *avctx, AVFrame *frame,
|
2011-09-06 19:17:45 +03:00
|
|
|
int *got_frame_ptr, AVPacket *avpkt)
|
2006-03-21 19:27:47 +02:00
|
|
|
{
|
2009-04-07 18:59:50 +03:00
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2006-03-21 19:27:47 +02:00
|
|
|
GetBitContext gb;
|
2011-06-06 15:13:02 +03:00
|
|
|
VLC vlc[4] = { { 0 } };
|
2011-09-06 19:17:45 +03:00
|
|
|
int16_t *samples;
|
|
|
|
uint8_t *samples8;
|
2020-07-24 19:57:56 +02:00
|
|
|
uint8_t values[4];
|
2011-09-06 19:17:45 +03:00
|
|
|
int i, res, ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
int unp_size;
|
|
|
|
int bits, stereo;
|
2022-05-03 18:30:59 +02:00
|
|
|
unsigned pred[2], val, val2;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2011-09-21 18:42:55 +03:00
|
|
|
if (buf_size <= 4) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
|
2017-05-30 18:12:48 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-09-21 18:42:55 +03:00
|
|
|
}
|
|
|
|
|
2007-01-20 00:12:59 +02:00
|
|
|
unp_size = AV_RL32(buf);
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2012-11-24 20:02:08 +03:00
|
|
|
if (unp_size > (1U<<24)) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "packet is too big\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2013-09-12 19:34:54 +03:00
|
|
|
if ((ret = init_get_bits8(&gb, buf + 4, buf_size - 4)) < 0)
|
|
|
|
return ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
|
|
|
|
if(!get_bits1(&gb)){
|
|
|
|
av_log(avctx, AV_LOG_INFO, "Sound: no data\n");
|
2011-09-06 19:17:45 +03:00
|
|
|
*got_frame_ptr = 0;
|
2006-03-21 19:27:47 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
stereo = get_bits1(&gb);
|
|
|
|
bits = get_bits1(&gb);
|
2013-05-07 08:20:32 +03:00
|
|
|
if (stereo ^ (avctx->ch_layout.nb_channels != 1)) {
|
2011-09-21 18:49:33 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "channels mismatch\n");
|
2017-05-30 18:12:48 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-09-21 18:49:33 +03:00
|
|
|
}
|
2013-12-20 20:07:30 +03:00
|
|
|
if (bits == (avctx->sample_fmt == AV_SAMPLE_FMT_U8)) {
|
2011-09-21 18:49:33 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n");
|
2017-05-30 18:12:48 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-09-21 18:49:33 +03:00
|
|
|
}
|
2006-03-21 19:27:47 +02:00
|
|
|
|
2011-09-06 19:17:45 +03:00
|
|
|
/* get output buffer */
|
2013-05-07 08:20:32 +03:00
|
|
|
frame->nb_samples = unp_size / (avctx->ch_layout.nb_channels * (bits + 1));
|
|
|
|
if (unp_size % (avctx->ch_layout.nb_channels * (bits + 1))) {
|
2015-11-15 15:52:08 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"The buffer does not contain an integer number of samples\n");
|
2017-05-30 18:12:48 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2015-11-15 15:52:08 +02:00
|
|
|
}
|
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 10:41:53 +03:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
2011-09-06 19:17:45 +03:00
|
|
|
return ret;
|
2012-12-24 04:00:41 +03:00
|
|
|
samples = (int16_t *)frame->data[0];
|
|
|
|
samples8 = frame->data[0];
|
2011-09-06 19:17:45 +03:00
|
|
|
|
2006-03-21 19:27:47 +02:00
|
|
|
// Initialize
|
|
|
|
for(i = 0; i < (1 << (bits + stereo)); i++) {
|
2020-07-24 21:28:34 +02:00
|
|
|
HuffContext h;
|
|
|
|
h.current = 0;
|
2007-08-09 03:57:36 +03:00
|
|
|
skip_bits1(&gb);
|
2023-04-27 11:11:27 +02:00
|
|
|
if ((ret = smacker_decode_tree(avctx, &gb, &h, 0)) < 0)
|
2013-09-11 15:35:19 +03:00
|
|
|
goto error;
|
2007-08-09 03:57:36 +03:00
|
|
|
skip_bits1(&gb);
|
2020-07-24 21:28:34 +02:00
|
|
|
if (h.current > 1) {
|
2020-10-25 11:24:31 +02:00
|
|
|
ret = ff_init_vlc_from_lengths(&vlc[i], SMKTREE_BITS, h.current,
|
|
|
|
&h.entries[0].length, sizeof(*h.entries),
|
|
|
|
&h.entries[0].value, sizeof(*h.entries), 1,
|
|
|
|
0, INIT_VLC_OUTPUT_LE, avctx);
|
2020-07-29 22:00:31 +02:00
|
|
|
if (ret < 0) {
|
2006-03-25 17:37:08 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n");
|
2013-09-11 15:35:19 +03:00
|
|
|
goto error;
|
2006-03-25 17:37:08 +02:00
|
|
|
}
|
2020-07-24 19:57:56 +02:00
|
|
|
} else
|
2020-10-25 11:24:31 +02:00
|
|
|
values[i] = h.entries[0].value;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
2013-05-27 19:21:04 +03:00
|
|
|
/* this codec relies on wraparound instead of clipping audio */
|
2006-03-21 19:27:47 +02:00
|
|
|
if(bits) { //decode 16-bit data
|
2007-03-21 07:56:29 +02:00
|
|
|
for(i = stereo; i >= 0; i--)
|
2020-06-26 11:36:57 +02:00
|
|
|
pred[i] = av_bswap16(get_bits(&gb, 16));
|
2011-09-13 02:14:44 +03:00
|
|
|
for(i = 0; i <= stereo; i++)
|
2007-03-21 07:56:29 +02:00
|
|
|
*samples++ = pred[i];
|
2022-05-03 18:30:59 +02:00
|
|
|
unp_size /= 2;
|
|
|
|
|
|
|
|
if (vlc[0 ].table || vlc[ 1].table ||
|
|
|
|
vlc[2*stereo].table || vlc[2*stereo+1].table) {
|
2022-07-07 14:35:19 +02:00
|
|
|
for(; i < unp_size ; i++) {
|
|
|
|
unsigned idx = 2 * (i & stereo);
|
|
|
|
if (get_bits_left(&gb) < 0) {
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (vlc[idx].table)
|
|
|
|
res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
|
|
|
|
else
|
|
|
|
res = values[idx];
|
|
|
|
val = res;
|
|
|
|
if (vlc[++idx].table)
|
|
|
|
res = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
|
|
|
|
else
|
|
|
|
res = values[idx];
|
|
|
|
val |= res << 8;
|
|
|
|
pred[idx / 2] += val;
|
|
|
|
*samples++ = pred[idx / 2];
|
2019-09-26 22:22:31 +02:00
|
|
|
}
|
2022-05-03 18:30:59 +02:00
|
|
|
} else if (stereo) {
|
|
|
|
val = 256*values[1] + values[0];
|
|
|
|
val2 = 256*values[3] + values[2];
|
|
|
|
for(; i < unp_size; i+=2) {
|
|
|
|
pred[0] += val;
|
|
|
|
pred[1] += val2;
|
|
|
|
*samples++ = pred[0];
|
|
|
|
*samples++ = pred[1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val = 256*values[1] + values[0];
|
|
|
|
for(; i < unp_size; i++) {
|
|
|
|
pred[0] += val;
|
|
|
|
*samples++ = pred[0];
|
|
|
|
}
|
|
|
|
}
|
2006-03-21 19:27:47 +02:00
|
|
|
} else { //8-bit data
|
2007-03-21 07:56:29 +02:00
|
|
|
for(i = stereo; i >= 0; i--)
|
|
|
|
pred[i] = get_bits(&gb, 8);
|
2011-09-13 02:14:44 +03:00
|
|
|
for(i = 0; i <= stereo; i++)
|
2009-03-21 17:52:14 +02:00
|
|
|
*samples8++ = pred[i];
|
2011-09-13 02:14:44 +03:00
|
|
|
for(; i < unp_size; i++) {
|
2020-07-24 15:51:09 +02:00
|
|
|
unsigned idx = i & stereo;
|
2019-09-26 22:22:31 +02:00
|
|
|
if (get_bits_left(&gb) < 0) {
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto error;
|
|
|
|
}
|
2020-07-24 15:51:09 +02:00
|
|
|
if (vlc[idx].table)
|
|
|
|
val = get_vlc2(&gb, vlc[idx].table, SMKTREE_BITS, 3);
|
|
|
|
else
|
|
|
|
val = values[idx];
|
|
|
|
pred[idx] += val;
|
|
|
|
*samples8++ = pred[idx];
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 15:35:19 +03:00
|
|
|
*got_frame_ptr = 1;
|
|
|
|
ret = buf_size;
|
|
|
|
|
|
|
|
error:
|
2006-03-21 19:27:47 +02:00
|
|
|
for(i = 0; i < 4; i++) {
|
2020-07-26 09:13:16 +02:00
|
|
|
ff_free_vlc(&vlc[i]);
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
|
|
|
|
2013-09-11 15:35:19 +03:00
|
|
|
return ret;
|
2006-03-21 19:27:47 +02:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_smacker_decoder = {
|
|
|
|
.p.name = "smackvid",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("Smacker video"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_SMACKVIDEO,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(SmackVContext),
|
|
|
|
.init = decode_init,
|
|
|
|
.close = decode_end,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(decode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1,
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2006-03-21 19:27:47 +02:00
|
|
|
};
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_smackaud_decoder = {
|
|
|
|
.p.name = "smackaud",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("Smacker audio"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.p.id = AV_CODEC_ID_SMACKAUDIO,
|
2011-07-17 13:54:31 +03:00
|
|
|
.init = smka_decode_init,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(smka_decode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1,
|
2006-03-21 19:27:47 +02:00
|
|
|
};
|