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
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/common.h"
26 #include "libavutil/intreadwrite.h"
32 * Westwood SNDx codecs
34 * Reference documents about VQA format and its audio codecs
36 * http://www.multimedia.cx
39 static const int8_t ws_adpcm_4bit
[] = {
40 -9, -8, -6, -5, -4, -3, -2, -1,
41 0, 1, 2, 3, 4, 5, 6, 8
44 typedef struct WSSndContext
{
48 static av_cold
int ws_snd_decode_init(AVCodecContext
*avctx
)
50 WSSndContext
*s
= avctx
->priv_data
;
53 avctx
->channel_layout
= AV_CH_LAYOUT_MONO
;
54 avctx
->sample_fmt
= AV_SAMPLE_FMT_U8
;
56 avcodec_get_frame_defaults(&s
->frame
);
57 avctx
->coded_frame
= &s
->frame
;
62 static int ws_snd_decode_frame(AVCodecContext
*avctx
, void *data
,
63 int *got_frame_ptr
, AVPacket
*avpkt
)
65 WSSndContext
*s
= avctx
->priv_data
;
66 const uint8_t *buf
= avpkt
->data
;
67 int buf_size
= avpkt
->size
;
69 int in_size
, out_size
, ret
;
78 av_log(avctx
, AV_LOG_ERROR
, "packet is too small\n");
79 return AVERROR(EINVAL
);
82 out_size
= AV_RL16(&buf
[0]);
83 in_size
= AV_RL16(&buf
[2]);
86 if (in_size
> buf_size
) {
87 av_log(avctx
, AV_LOG_ERROR
, "Frame data is larger than input buffer\n");
91 /* get output buffer */
92 s
->frame
.nb_samples
= out_size
;
93 if ((ret
= ff_get_buffer(avctx
, &s
->frame
)) < 0) {
94 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
97 samples
= s
->frame
.data
[0];
98 samples_end
= samples
+ out_size
;
100 if (in_size
== out_size
) {
101 memcpy(samples
, buf
, out_size
);
103 *(AVFrame
*)data
= s
->frame
;
107 while (samples
< samples_end
&& buf
- avpkt
->data
< buf_size
) {
114 /* make sure we don't write past the output buffer */
116 case 0: smp
= 4 * (count
+ 1); break;
117 case 1: smp
= 2 * (count
+ 1); break;
118 case 2: smp
= (count
& 0x20) ? 1 : count
+ 1; break;
119 default: smp
= count
+ 1; break;
121 if (samples_end
- samples
< smp
)
124 /* make sure we don't read past the input buffer */
125 size
= ((code
== 2 && (count
& 0x20)) || code
== 3) ? 0 : count
+ 1;
126 if ((buf
- avpkt
->data
) + size
> buf_size
)
130 case 0: /* ADPCM 2-bit */
131 for (count
++; count
> 0; count
--) {
133 sample
+= ( code
& 0x3) - 2;
134 sample
= av_clip_uint8(sample
);
136 sample
+= ((code
>> 2) & 0x3) - 2;
137 sample
= av_clip_uint8(sample
);
139 sample
+= ((code
>> 4) & 0x3) - 2;
140 sample
= av_clip_uint8(sample
);
142 sample
+= (code
>> 6) - 2;
143 sample
= av_clip_uint8(sample
);
147 case 1: /* ADPCM 4-bit */
148 for (count
++; count
> 0; count
--) {
150 sample
+= ws_adpcm_4bit
[code
& 0xF];
151 sample
= av_clip_uint8(sample
);
153 sample
+= ws_adpcm_4bit
[code
>> 4];
154 sample
= av_clip_uint8(sample
);
158 case 2: /* no compression */
159 if (count
& 0x20) { /* big delta */
164 sample
= av_clip_uint8(sample
);
167 memcpy(samples
, buf
, smp
);
174 memset(samples
, sample
, smp
);
179 s
->frame
.nb_samples
= samples
- s
->frame
.data
[0];
181 *(AVFrame
*)data
= s
->frame
;
186 AVCodec ff_ws_snd1_decoder
= {
188 .type
= AVMEDIA_TYPE_AUDIO
,
189 .id
= AV_CODEC_ID_WESTWOOD_SND1
,
190 .priv_data_size
= sizeof(WSSndContext
),
191 .init
= ws_snd_decode_init
,
192 .decode
= ws_snd_decode_frame
,
193 .capabilities
= CODEC_CAP_DR1
,
194 .long_name
= NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),