2012-03-19 02:52:08 +00:00
|
|
|
/*
|
2012-09-30 15:06:57 +00:00
|
|
|
* LucasArts VIMA decoder
|
2012-03-19 02:52:08 +00:00
|
|
|
* Copyright (c) 2012 Paul B Mahol
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg 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.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg 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 FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-04-25 20:17:05 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* LucasArts VIMA audio decoder
|
|
|
|
* @author Paul B Mahol
|
|
|
|
*/
|
|
|
|
|
2012-11-13 13:16:48 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2024-04-25 09:18:18 +00:00
|
|
|
#include "libavutil/thread.h"
|
2012-03-19 02:52:08 +00:00
|
|
|
|
|
|
|
#include "adpcm_data.h"
|
2012-03-19 02:52:08 +00:00
|
|
|
#include "avcodec.h"
|
2024-04-25 09:18:18 +00:00
|
|
|
#include "codec_internal.h"
|
2012-03-19 02:52:08 +00:00
|
|
|
#include "get_bits.h"
|
2012-12-05 14:18:12 +00:00
|
|
|
#include "internal.h"
|
2012-03-19 02:52:08 +00:00
|
|
|
|
2013-04-25 20:12:14 +00:00
|
|
|
static uint16_t predict_table[5786 * 2];
|
2012-03-19 02:52:08 +00:00
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
static const uint8_t size_table[] = {
|
2012-03-19 02:52:08 +00:00
|
|
|
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
|
|
|
|
};
|
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
static const int8_t index_table1[] = {
|
2012-03-19 02:52:08 +00:00
|
|
|
-1, 4, -1, 4
|
|
|
|
};
|
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
static const int8_t index_table2[] = {
|
2012-03-19 02:52:08 +00:00
|
|
|
-1, -1, 2, 6, -1, -1, 2, 6
|
|
|
|
};
|
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
static const int8_t index_table3[] = {
|
|
|
|
-1, -1, -1, -1, 1, 2, 4, 6, -1, -1, -1, -1, 1, 2, 4, 6
|
2012-03-19 02:52:08 +00:00
|
|
|
};
|
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
static const int8_t index_table4[] = {
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 2, 2, 4, 5, 6,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 2, 2, 4, 5, 6
|
2012-03-19 02:52:08 +00:00
|
|
|
};
|
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
static const int8_t index_table5[] = {
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 5, 5, 6, 6,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 5, 5, 6, 6
|
2012-03-19 02:52:08 +00:00
|
|
|
};
|
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
static const int8_t index_table6[] = {
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6
|
2012-03-19 02:52:08 +00:00
|
|
|
};
|
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
static const int8_t *const step_index_tables[] = {
|
2012-03-19 02:52:08 +00:00
|
|
|
index_table1, index_table2, index_table3,
|
|
|
|
index_table4, index_table5, index_table6
|
|
|
|
};
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
static av_cold void predict_table_init(void)
|
2012-03-19 02:52:08 +00:00
|
|
|
{
|
2024-04-25 09:18:18 +00:00
|
|
|
for (int start_pos = 0; start_pos < 64; start_pos++) {
|
2012-03-19 02:52:08 +00:00
|
|
|
unsigned int dest_pos, table_pos;
|
|
|
|
|
|
|
|
for (table_pos = 0, dest_pos = start_pos;
|
|
|
|
table_pos < FF_ARRAY_ELEMS(ff_adpcm_step_table);
|
|
|
|
table_pos++, dest_pos += 64) {
|
|
|
|
int put = 0, count, table_value;
|
|
|
|
|
|
|
|
table_value = ff_adpcm_step_table[table_pos];
|
|
|
|
for (count = 32; count != 0; count >>= 1) {
|
|
|
|
if (start_pos & count)
|
|
|
|
put += table_value;
|
|
|
|
table_value >>= 1;
|
|
|
|
}
|
2013-04-25 20:12:14 +00:00
|
|
|
predict_table[dest_pos] = put;
|
2012-03-19 02:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-25 09:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
static AVOnce init_static_once = AV_ONCE_INIT;
|
|
|
|
|
|
|
|
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
|
|
|
|
|
|
|
|
ff_thread_once(&init_static_once, predict_table_init);
|
2012-03-19 02:52:08 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
|
2012-03-19 02:52:08 +00:00
|
|
|
int *got_frame_ptr, AVPacket *pkt)
|
|
|
|
{
|
2012-03-19 02:52:08 +00:00
|
|
|
GetBitContext gb;
|
|
|
|
int16_t pcm_data[2];
|
|
|
|
uint32_t samples;
|
|
|
|
int8_t channel_hint[2];
|
|
|
|
int ret, chan;
|
|
|
|
int channels = 1;
|
2012-03-19 02:52:08 +00:00
|
|
|
|
|
|
|
if (pkt->size < 13)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2013-01-16 19:51:39 +00:00
|
|
|
if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
|
|
|
|
return ret;
|
2012-09-30 15:14:28 +00:00
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
samples = get_bits_long(&gb, 32);
|
|
|
|
if (samples == 0xffffffff) {
|
|
|
|
skip_bits_long(&gb, 32);
|
|
|
|
samples = get_bits_long(&gb, 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (samples > pkt->size * 2)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
channel_hint[0] = get_sbits(&gb, 8);
|
|
|
|
if (channel_hint[0] & 0x80) {
|
|
|
|
channel_hint[0] = ~channel_hint[0];
|
|
|
|
channels = 2;
|
|
|
|
}
|
2024-04-25 09:18:18 +00:00
|
|
|
av_channel_layout_uninit(&avctx->ch_layout);
|
|
|
|
av_channel_layout_default(&avctx->ch_layout, channels);
|
2012-03-19 02:52:08 +00:00
|
|
|
pcm_data[0] = get_sbits(&gb, 16);
|
|
|
|
if (channels > 1) {
|
|
|
|
channel_hint[1] = get_sbits(&gb, 8);
|
2012-03-19 02:52:08 +00:00
|
|
|
pcm_data[1] = get_sbits(&gb, 16);
|
2012-03-19 02:52:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 12:06:40 +00:00
|
|
|
frame->nb_samples = samples;
|
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 07:41:53 +00:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
2012-03-19 02:52:08 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (chan = 0; chan < channels; chan++) {
|
2012-03-19 02:52:08 +00:00
|
|
|
uint16_t *dest = (uint16_t *)frame->data[0] + chan;
|
2012-03-19 02:52:08 +00:00
|
|
|
int step_index = channel_hint[chan];
|
|
|
|
int output = pcm_data[chan];
|
|
|
|
int sample;
|
|
|
|
|
|
|
|
for (sample = 0; sample < samples; sample++) {
|
|
|
|
int lookup_size, lookup, highbit, lowbits;
|
|
|
|
|
|
|
|
step_index = av_clip(step_index, 0, 88);
|
|
|
|
lookup_size = size_table[step_index];
|
|
|
|
lookup = get_bits(&gb, lookup_size);
|
|
|
|
highbit = 1 << (lookup_size - 1);
|
|
|
|
lowbits = highbit - 1;
|
|
|
|
|
|
|
|
if (lookup & highbit)
|
|
|
|
lookup ^= highbit;
|
|
|
|
else
|
|
|
|
highbit = 0;
|
|
|
|
|
|
|
|
if (lookup == lowbits) {
|
|
|
|
output = get_sbits(&gb, 16);
|
|
|
|
} else {
|
|
|
|
int predict_index, diff;
|
|
|
|
|
|
|
|
predict_index = (lookup << (7 - lookup_size)) | (step_index << 6);
|
|
|
|
predict_index = av_clip(predict_index, 0, 5785);
|
2013-04-25 20:12:14 +00:00
|
|
|
diff = predict_table[predict_index];
|
2012-03-19 02:52:08 +00:00
|
|
|
if (lookup)
|
|
|
|
diff += ff_adpcm_step_table[step_index] >> (lookup_size - 1);
|
|
|
|
if (highbit)
|
2012-03-19 02:52:08 +00:00
|
|
|
diff = -diff;
|
2012-03-19 02:52:08 +00:00
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
output = av_clip_int16(output + diff);
|
2012-03-19 02:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*dest = output;
|
|
|
|
dest += channels;
|
|
|
|
|
|
|
|
step_index += step_index_tables[lookup_size - 2][lookup];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-19 02:52:08 +00:00
|
|
|
*got_frame_ptr = 1;
|
2012-03-19 02:52:08 +00:00
|
|
|
|
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
const FFCodec ff_adpcm_vima_decoder = {
|
|
|
|
.p.name = "adpcm_vima",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("LucasArts VIMA audio"),
|
|
|
|
.p.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.p.id = AV_CODEC_ID_ADPCM_VIMA,
|
2012-03-19 02:52:08 +00:00
|
|
|
.init = decode_init,
|
2024-04-25 09:18:18 +00:00
|
|
|
FF_CODEC_DECODE_CB(decode_frame),
|
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
|
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
|
2012-03-19 02:52:08 +00:00
|
|
|
};
|