3 * Copyright (c) 2005 Konstantin Shishkov
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intreadwrite.h"
26 * @file libavcodec/ws-snd1.c
27 * Westwood SNDx codecs.
29 * Reference documents about VQA format and its audio codecs
31 * http://www.multimedia.cx
34 static const char ws_adpcm_2bit
[] = { -2, -1, 0, 1};
35 static const char ws_adpcm_4bit
[] = {
36 -9, -8, -6, -5, -4, -3, -2, -1,
37 0, 1, 2, 3, 4, 5, 6, 8 };
39 #define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128;
41 static av_cold
int ws_snd_decode_init(AVCodecContext
* avctx
)
43 // WSSNDContext *c = avctx->priv_data;
45 avctx
->sample_fmt
= SAMPLE_FMT_S16
;
49 static int ws_snd_decode_frame(AVCodecContext
*avctx
,
50 void *data
, int *data_size
,
53 const uint8_t *buf
= avpkt
->data
;
54 int buf_size
= avpkt
->size
;
55 // WSSNDContext *c = avctx->priv_data;
57 int in_size
, out_size
;
60 short *samples
= data
;
65 out_size
= AV_RL16(&buf
[0]);
66 *data_size
= out_size
* 2;
67 in_size
= AV_RL16(&buf
[2]);
70 if (out_size
> *data_size
) {
71 av_log(avctx
, AV_LOG_ERROR
, "Frame is too large to fit in buffer\n");
74 if (in_size
> buf_size
) {
75 av_log(avctx
, AV_LOG_ERROR
, "Frame data is larger than input buffer\n");
78 if (in_size
== out_size
) {
79 for (i
= 0; i
< out_size
; i
++)
80 *samples
++ = (*buf
++ - 0x80) << 8;
84 while (out_size
> 0) {
88 count
= (*buf
) & 0x3F;
91 case 0: /* ADPCM 2-bit */
92 for (count
++; count
> 0; count
--) {
94 sample
+= ws_adpcm_2bit
[code
& 0x3];
96 *samples
++ = sample
<< 8;
97 sample
+= ws_adpcm_2bit
[(code
>> 2) & 0x3];
99 *samples
++ = sample
<< 8;
100 sample
+= ws_adpcm_2bit
[(code
>> 4) & 0x3];
102 *samples
++ = sample
<< 8;
103 sample
+= ws_adpcm_2bit
[(code
>> 6) & 0x3];
105 *samples
++ = sample
<< 8;
109 case 1: /* ADPCM 4-bit */
110 for (count
++; count
> 0; count
--) {
112 sample
+= ws_adpcm_4bit
[code
& 0xF];
114 *samples
++ = sample
<< 8;
115 sample
+= ws_adpcm_4bit
[code
>> 4];
117 *samples
++ = sample
<< 8;
121 case 2: /* no compression */
122 if (count
& 0x20) { /* big delta */
127 *samples
++ = sample
<< 8;
130 for (count
++; count
> 0; count
--) {
131 *samples
++ = (*buf
++ - 0x80) << 8;
134 sample
= buf
[-1] - 0x80;
138 for(count
++; count
> 0; count
--) {
139 *samples
++ = sample
<< 8;
148 AVCodec ws_snd1_decoder
= {
151 CODEC_ID_WESTWOOD_SND1
,
157 .long_name
= NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),