28
* Westwood SNDx codecs.
28
* Westwood SNDx codecs
30
30
* Reference documents about VQA format and its audio codecs
31
31
* can be found here:
32
32
* http://www.multimedia.cx
35
static const int8_t ws_adpcm_2bit[] = { -2, -1, 0, 1};
36
35
static const int8_t ws_adpcm_4bit[] = {
37
36
-9, -8, -6, -5, -4, -3, -2, -1,
38
0, 1, 2, 3, 4, 5, 6, 8 };
40
#define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128;
42
static av_cold int ws_snd_decode_init(AVCodecContext * avctx)
37
0, 1, 2, 3, 4, 5, 6, 8
40
typedef struct WSSndContext {
44
static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
44
// WSSNDContext *c = avctx->priv_data;
46
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
46
WSSndContext *s = avctx->priv_data;
48
if (avctx->channels != 1) {
49
av_log_ask_for_sample(avctx, "unsupported number of channels\n");
50
return AVERROR(EINVAL);
53
avctx->sample_fmt = AV_SAMPLE_FMT_U8;
55
avcodec_get_frame_defaults(&s->frame);
56
avctx->coded_frame = &s->frame;
50
static int ws_snd_decode_frame(AVCodecContext *avctx,
51
void *data, int *data_size,
61
static int ws_snd_decode_frame(AVCodecContext *avctx, void *data,
62
int *got_frame_ptr, AVPacket *avpkt)
64
WSSndContext *s = avctx->priv_data;
54
65
const uint8_t *buf = avpkt->data;
55
int buf_size = avpkt->size;
56
// WSSNDContext *c = avctx->priv_data;
66
int buf_size = avpkt->size;
58
int in_size, out_size;
61
short *samples = data;
68
int in_size, out_size, ret;
77
av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
78
return AVERROR(EINVAL);
66
81
out_size = AV_RL16(&buf[0]);
67
*data_size = out_size * 2;
68
in_size = AV_RL16(&buf[2]);
82
in_size = AV_RL16(&buf[2]);
71
if (out_size > *data_size) {
72
av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
75
85
if (in_size > buf_size) {
76
86
av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
90
/* get output buffer */
91
s->frame.nb_samples = out_size;
92
if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
93
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
96
samples = s->frame.data[0];
97
samples_end = samples + out_size;
79
99
if (in_size == out_size) {
80
for (i = 0; i < out_size; i++)
81
*samples++ = (*buf++ - 0x80) << 8;
100
memcpy(samples, buf, out_size);
102
*(AVFrame *)data = s->frame;
85
while (out_size > 0) {
106
while (samples < samples_end && buf - avpkt->data < buf_size) {
89
count = (*buf) & 0x3F;
113
/* make sure we don't write past the output buffer */
115
case 0: smp = 4; break;
116
case 1: smp = 2; break;
117
case 2: smp = (count & 0x20) ? 1 : count + 1; break;
118
default: smp = count + 1; break;
120
if (samples_end - samples < smp)
123
/* make sure we don't read past the input buffer */
124
size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
125
if ((buf - avpkt->data) + size > buf_size)
92
129
case 0: /* ADPCM 2-bit */
93
130
for (count++; count > 0; count--) {
95
sample += ws_adpcm_2bit[code & 0x3];
97
*samples++ = sample << 8;
98
sample += ws_adpcm_2bit[(code >> 2) & 0x3];
100
*samples++ = sample << 8;
101
sample += ws_adpcm_2bit[(code >> 4) & 0x3];
103
*samples++ = sample << 8;
104
sample += ws_adpcm_2bit[(code >> 6) & 0x3];
106
*samples++ = sample << 8;
132
sample += ( code & 0x3) - 2;
133
sample = av_clip_uint8(sample);
135
sample += ((code >> 2) & 0x3) - 2;
136
sample = av_clip_uint8(sample);
138
sample += ((code >> 4) & 0x3) - 2;
139
sample = av_clip_uint8(sample);
141
sample += (code >> 6) - 2;
142
sample = av_clip_uint8(sample);
110
146
case 1: /* ADPCM 4-bit */
111
147
for (count++; count > 0; count--) {
113
149
sample += ws_adpcm_4bit[code & 0xF];
115
*samples++ = sample << 8;
150
sample = av_clip_uint8(sample);
116
152
sample += ws_adpcm_4bit[code >> 4];
118
*samples++ = sample << 8;
153
sample = av_clip_uint8(sample);
122
157
case 2: /* no compression */
127
162
sample += t >> 3;
128
*samples++ = sample << 8;
163
sample = av_clip_uint8(sample);
130
165
} else { /* copy */
131
for (count++; count > 0; count--) {
132
*samples++ = (*buf++ - 0x80) << 8;
135
sample = buf[-1] - 0x80;
166
memcpy(samples, buf, smp);
138
172
default: /* run */
139
for(count++; count > 0; count--) {
140
*samples++ = sample << 8;
173
memset(samples, sample, smp);
178
s->frame.nb_samples = samples - s->frame.data[0];
180
*(AVFrame *)data = s->frame;
149
185
AVCodec ff_ws_snd1_decoder = {
152
CODEC_ID_WESTWOOD_SND1,
187
.type = AVMEDIA_TYPE_AUDIO,
188
.id = CODEC_ID_WESTWOOD_SND1,
189
.priv_data_size = sizeof(WSSndContext),
190
.init = ws_snd_decode_init,
191
.decode = ws_snd_decode_frame,
192
.capabilities = CODEC_CAP_DR1,
158
193
.long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),