make sure that the last coupling band stops at the end of the coupling range
[ffmpeg-lucabe.git] / libavcodec / ws-snd1.c
bloba419e3dfb53c05dcb5757b844a39a64273fcccd6
1 /*
2 * Westwood SNDx codecs
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
21 #include "avcodec.h"
23 /**
24 * @file ws-snd.c
25 * Westwood SNDx codecs.
27 * Reference documents about VQA format and its audio codecs
28 * can be found here:
29 * http://www.multimedia.cx
32 static const char ws_adpcm_2bit[] = { -2, -1, 0, 1};
33 static const char ws_adpcm_4bit[] = {
34 -9, -8, -6, -5, -4, -3, -2, -1,
35 0, 1, 2, 3, 4, 5, 6, 8 };
37 #define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128;
39 static int ws_snd_decode_init(AVCodecContext * avctx)
41 // WSSNDContext *c = avctx->priv_data;
43 return 0;
46 static int ws_snd_decode_frame(AVCodecContext *avctx,
47 void *data, int *data_size,
48 uint8_t *buf, int buf_size)
50 // WSSNDContext *c = avctx->priv_data;
52 int in_size, out_size;
53 int sample = 0;
54 int i;
55 short *samples = data;
57 if (!buf_size)
58 return 0;
60 out_size = AV_RL16(&buf[0]);
61 *data_size = out_size * 2;
62 in_size = AV_RL16(&buf[2]);
63 buf += 4;
65 if (out_size > *data_size) {
66 av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
67 return -1;
69 if (in_size > buf_size) {
70 av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
71 return -1;
73 if (in_size == out_size) {
74 for (i = 0; i < out_size; i++)
75 *samples++ = (*buf++ - 0x80) << 8;
76 return buf_size;
79 while (out_size > 0) {
80 int code;
81 uint8_t count;
82 code = (*buf) >> 6;
83 count = (*buf) & 0x3F;
84 buf++;
85 switch(code) {
86 case 0: /* ADPCM 2-bit */
87 for (count++; count > 0; count--) {
88 code = *buf++;
89 sample += ws_adpcm_2bit[code & 0x3];
90 CLIP8(sample);
91 *samples++ = sample << 8;
92 sample += ws_adpcm_2bit[(code >> 2) & 0x3];
93 CLIP8(sample);
94 *samples++ = sample << 8;
95 sample += ws_adpcm_2bit[(code >> 4) & 0x3];
96 CLIP8(sample);
97 *samples++ = sample << 8;
98 sample += ws_adpcm_2bit[(code >> 6) & 0x3];
99 CLIP8(sample);
100 *samples++ = sample << 8;
101 out_size -= 4;
103 break;
104 case 1: /* ADPCM 4-bit */
105 for (count++; count > 0; count--) {
106 code = *buf++;
107 sample += ws_adpcm_4bit[code & 0xF];
108 CLIP8(sample);
109 *samples++ = sample << 8;
110 sample += ws_adpcm_4bit[code >> 4];
111 CLIP8(sample);
112 *samples++ = sample << 8;
113 out_size -= 2;
115 break;
116 case 2: /* no compression */
117 if (count & 0x20) { /* big delta */
118 char t;
119 t = count;
120 t <<= 3;
121 sample += t >> 3;
122 *samples++ = sample << 8;
123 out_size--;
124 } else { /* copy */
125 for (count++; count > 0; count--) {
126 *samples++ = (*buf++ - 0x80) << 8;
127 out_size--;
129 sample = buf[-1] - 0x80;
131 break;
132 default: /* run */
133 for(count++; count > 0; count--) {
134 *samples++ = sample << 8;
135 out_size--;
140 return buf_size;
143 AVCodec ws_snd1_decoder = {
144 "ws_snd1",
145 CODEC_TYPE_AUDIO,
146 CODEC_ID_WESTWOOD_SND1,
148 ws_snd_decode_init,
149 NULL,
150 NULL,
151 ws_snd_decode_frame,