034eeaa1 |
/*
* Westwood SNDx codecs
* Copyright (c) 2005 Konstantin Shishkov
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
034eeaa1 |
* 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. |
034eeaa1 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
034eeaa1 |
* 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 |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
034eeaa1 |
*/ |
6a5d31ac |
|
ef4a9342 |
#include <stdint.h> |
6a5d31ac |
#include "libavutil/intreadwrite.h" |
034eeaa1 |
#include "avcodec.h"
/** |
ba87f080 |
* @file |
034eeaa1 |
* Westwood SNDx codecs.
*
* Reference documents about VQA format and its audio codecs
* can be found here:
* http://www.multimedia.cx
*/
|
774b20ca |
static const int8_t ws_adpcm_2bit[] = { -2, -1, 0, 1};
static const int8_t ws_adpcm_4bit[] = { |
034eeaa1 |
-9, -8, -6, -5, -4, -3, -2, -1,
0, 1, 2, 3, 4, 5, 6, 8 };
|
98a6fff9 |
static av_cold int ws_snd_decode_init(AVCodecContext * avctx) |
034eeaa1 |
{
// WSSNDContext *c = avctx->priv_data; |
115329f1 |
|
857c7e12 |
if (avctx->channels != 1) {
av_log_ask_for_sample(avctx, "unsupported number of channels\n");
return AVERROR(EINVAL);
}
|
4db466db |
avctx->sample_fmt = AV_SAMPLE_FMT_U8; |
034eeaa1 |
return 0;
}
static int ws_snd_decode_frame(AVCodecContext *avctx,
void *data, int *data_size, |
7a00bbad |
AVPacket *avpkt) |
034eeaa1 |
{ |
7a00bbad |
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; |
034eeaa1 |
// WSSNDContext *c = avctx->priv_data; |
115329f1 |
|
034eeaa1 |
int in_size, out_size; |
4db466db |
int sample = 128; |
034eeaa1 |
int i; |
4db466db |
uint8_t *samples = data; |
115329f1 |
|
034eeaa1 |
if (!buf_size)
return 0;
|
915b905a |
if (buf_size < 4) {
av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
return AVERROR(EINVAL);
}
|
fead30d4 |
out_size = AV_RL16(&buf[0]);
in_size = AV_RL16(&buf[2]); |
034eeaa1 |
buf += 4; |
115329f1 |
|
e938637b |
if (out_size > *data_size) {
av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
return -1;
}
if (in_size > buf_size) {
av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
return -1;
} |
4db466db |
|
034eeaa1 |
if (in_size == out_size) {
for (i = 0; i < out_size; i++) |
4db466db |
*samples++ = *buf++; |
915b905a |
*data_size = out_size; |
034eeaa1 |
return buf_size;
} |
115329f1 |
|
915b905a |
while (out_size > 0 && buf - avpkt->data < buf_size) {
int code, smp, size; |
034eeaa1 |
uint8_t count;
code = (*buf) >> 6;
count = (*buf) & 0x3F;
buf++; |
915b905a |
/* make sure we don't write more than out_size samples */
switch (code) {
case 0: smp = 4; break;
case 1: smp = 2; break;
case 2: smp = (count & 0x20) ? 1 : count + 1; break;
default: smp = count + 1; break;
}
if (out_size < smp) {
out_size = 0;
break;
}
/* make sure we don't read past the input buffer */
size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
if ((buf - avpkt->data) + size > buf_size)
break;
|
034eeaa1 |
switch(code) {
case 0: /* ADPCM 2-bit */
for (count++; count > 0; count--) {
code = *buf++;
sample += ws_adpcm_2bit[code & 0x3]; |
4db466db |
sample = av_clip_uint8(sample);
*samples++ = sample; |
034eeaa1 |
sample += ws_adpcm_2bit[(code >> 2) & 0x3]; |
4db466db |
sample = av_clip_uint8(sample);
*samples++ = sample; |
034eeaa1 |
sample += ws_adpcm_2bit[(code >> 4) & 0x3]; |
4db466db |
sample = av_clip_uint8(sample);
*samples++ = sample; |
034eeaa1 |
sample += ws_adpcm_2bit[(code >> 6) & 0x3]; |
4db466db |
sample = av_clip_uint8(sample);
*samples++ = sample; |
034eeaa1 |
out_size -= 4;
}
break;
case 1: /* ADPCM 4-bit */
for (count++; count > 0; count--) {
code = *buf++;
sample += ws_adpcm_4bit[code & 0xF]; |
4db466db |
sample = av_clip_uint8(sample);
*samples++ = sample; |
034eeaa1 |
sample += ws_adpcm_4bit[code >> 4]; |
4db466db |
sample = av_clip_uint8(sample);
*samples++ = sample; |
034eeaa1 |
out_size -= 2;
}
break;
case 2: /* no compression */
if (count & 0x20) { /* big delta */ |
774b20ca |
int8_t t; |
034eeaa1 |
t = count;
t <<= 3;
sample += t >> 3; |
4db466db |
sample = av_clip_uint8(sample);
*samples++ = sample; |
034eeaa1 |
out_size--;
} else { /* copy */
for (count++; count > 0; count--) { |
4db466db |
*samples++ = *buf++; |
034eeaa1 |
out_size--;
} |
4db466db |
sample = buf[-1]; |
034eeaa1 |
}
break;
default: /* run */
for(count++; count > 0; count--) { |
4db466db |
*samples++ = sample; |
034eeaa1 |
out_size--;
}
}
} |
115329f1 |
|
915b905a |
*data_size = samples - (uint8_t *)data;
|
034eeaa1 |
return buf_size;
}
|
e7e2df27 |
AVCodec ff_ws_snd1_decoder = { |
034eeaa1 |
"ws_snd1", |
72415b2a |
AVMEDIA_TYPE_AUDIO, |
034eeaa1 |
CODEC_ID_WESTWOOD_SND1, |
8aaed74c |
0, |
034eeaa1 |
ws_snd_decode_init,
NULL,
NULL,
ws_snd_decode_frame, |
fe4bf374 |
.long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"), |
034eeaa1 |
}; |