3 * Copyright (c) 2005 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/intreadwrite.h"
28 * Westwood SNDx codecs
30 * Reference documents about VQA format and its audio codecs
32 * http://www.multimedia.cx
35 static const int8_t ws_adpcm_4bit[] = {
36 -9, -8, -6, -5, -4, -3, -2, -1,
37 0, 1, 2, 3, 4, 5, 6, 8
40 static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
42 if (avctx->channels != 1) {
43 av_log_ask_for_sample(avctx, "unsupported number of channels\n");
44 return AVERROR(EINVAL);
47 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
51 static int ws_snd_decode_frame(AVCodecContext *avctx, void *data,
52 int *data_size, AVPacket *avpkt)
54 const uint8_t *buf = avpkt->data;
55 int buf_size = avpkt->size;
57 int in_size, out_size;
59 uint8_t *samples = data;
66 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
67 return AVERROR(EINVAL);
70 out_size = AV_RL16(&buf[0]);
71 in_size = AV_RL16(&buf[2]);
74 if (out_size > *data_size) {
75 av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
78 if (in_size > buf_size) {
79 av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
82 samples_end = samples + out_size;
84 if (in_size == out_size) {
85 memcpy(samples, buf, out_size);
86 *data_size = out_size;
90 while (samples < samples_end && buf - avpkt->data < buf_size) {
97 /* make sure we don't write past the output buffer */
99 case 0: smp = 4; break;
100 case 1: smp = 2; break;
101 case 2: smp = (count & 0x20) ? 1 : count + 1; break;
102 default: smp = count + 1; break;
104 if (samples_end - samples < smp)
107 /* make sure we don't read past the input buffer */
108 size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
109 if ((buf - avpkt->data) + size > buf_size)
113 case 0: /* ADPCM 2-bit */
114 for (count++; count > 0; count--) {
116 sample += ( code & 0x3) - 2;
117 sample = av_clip_uint8(sample);
119 sample += ((code >> 2) & 0x3) - 2;
120 sample = av_clip_uint8(sample);
122 sample += ((code >> 4) & 0x3) - 2;
123 sample = av_clip_uint8(sample);
125 sample += (code >> 6) - 2;
126 sample = av_clip_uint8(sample);
130 case 1: /* ADPCM 4-bit */
131 for (count++; count > 0; count--) {
133 sample += ws_adpcm_4bit[code & 0xF];
134 sample = av_clip_uint8(sample);
136 sample += ws_adpcm_4bit[code >> 4];
137 sample = av_clip_uint8(sample);
141 case 2: /* no compression */
142 if (count & 0x20) { /* big delta */
147 sample = av_clip_uint8(sample);
150 memcpy(samples, buf, smp);
157 memset(samples, sample, smp);
162 *data_size = samples - (uint8_t *)data;
167 AVCodec ff_ws_snd1_decoder = {
169 .type = AVMEDIA_TYPE_AUDIO,
170 .id = CODEC_ID_WESTWOOD_SND1,
171 .init = ws_snd_decode_init,
172 .decode = ws_snd_decode_frame,
173 .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),