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 typedef struct WSSndContext
{
44 static av_cold
int ws_snd_decode_init(AVCodecContext
*avctx
)
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
;
61 static int ws_snd_decode_frame(AVCodecContext
*avctx
, void *data
,
62 int *got_frame_ptr
, AVPacket
*avpkt
)
64 WSSndContext
*s
= avctx
->priv_data
;
65 const uint8_t *buf
= avpkt
->data
;
66 int buf_size
= avpkt
->size
;
68 int in_size
, out_size
, ret
;
77 av_log(avctx
, AV_LOG_ERROR
, "packet is too small\n");
78 return AVERROR(EINVAL
);
81 out_size
= AV_RL16(&buf
[0]);
82 in_size
= AV_RL16(&buf
[2]);
85 if (in_size
> buf_size
) {
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
;
99 if (in_size
== out_size
) {
100 memcpy(samples
, buf
, out_size
);
102 *(AVFrame
*)data
= s
->frame
;
106 while (samples
< samples_end
&& buf
- avpkt
->data
< buf_size
) {
113 /* make sure we don't write past the output buffer */
115 case 0: smp
= 4 * (count
+ 1); break;
116 case 1: smp
= 2 * (count
+ 1); 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
)
129 case 0: /* ADPCM 2-bit */
130 for (count
++; count
> 0; count
--) {
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
);
146 case 1: /* ADPCM 4-bit */
147 for (count
++; count
> 0; count
--) {
149 sample
+= ws_adpcm_4bit
[code
& 0xF];
150 sample
= av_clip_uint8(sample
);
152 sample
+= ws_adpcm_4bit
[code
>> 4];
153 sample
= av_clip_uint8(sample
);
157 case 2: /* no compression */
158 if (count
& 0x20) { /* big delta */
163 sample
= av_clip_uint8(sample
);
166 memcpy(samples
, buf
, smp
);
173 memset(samples
, sample
, smp
);
178 s
->frame
.nb_samples
= samples
- s
->frame
.data
[0];
180 *(AVFrame
*)data
= s
->frame
;
185 AVCodec ff_ws_snd1_decoder
= {
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
,
193 .long_name
= NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),