]> git.sesse.net Git - ffmpeg/blob - libavcodec/ws-snd1.c
Use AV_XX16 macros
[ffmpeg] / libavcodec / ws-snd1.c
1 /*
2  * Westwood SNDx codecs
3  * Copyright (c) 2005 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "avcodec.h"
22
23 /**
24  * @file ws-snd.c
25  * Westwood SNDx codecs.
26  *
27  * Reference documents about VQA format and its audio codecs
28  * can be found here:
29  * http://www.multimedia.cx
30  */
31
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 };
36
37 #define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128;
38
39 static int ws_snd_decode_init(AVCodecContext * avctx)
40 {
41 //    WSSNDContext *c = avctx->priv_data;
42
43     return 0;
44 }
45
46 static int ws_snd_decode_frame(AVCodecContext *avctx,
47                 void *data, int *data_size,
48                 uint8_t *buf, int buf_size)
49 {
50 //    WSSNDContext *c = avctx->priv_data;
51
52     int in_size, out_size;
53     int sample = 0;
54     int i;
55     short *samples = data;
56
57     if (!buf_size)
58         return 0;
59
60     out_size = AV_RL16(&buf[0]);
61     *data_size = out_size * 2;
62     in_size = AV_RL16(&buf[2]);
63     buf += 4;
64
65     if (in_size == out_size) {
66         for (i = 0; i < out_size; i++)
67             *samples++ = (*buf++ - 0x80) << 8;
68         return buf_size;
69     }
70
71     while (out_size > 0) {
72         int code;
73         uint8_t count;
74         code = (*buf) >> 6;
75         count = (*buf) & 0x3F;
76         buf++;
77         switch(code) {
78         case 0: /* ADPCM 2-bit */
79             for (count++; count > 0; count--) {
80                 code = *buf++;
81                 sample += ws_adpcm_2bit[code & 0x3];
82                 CLIP8(sample);
83                 *samples++ = sample << 8;
84                 sample += ws_adpcm_2bit[(code >> 2) & 0x3];
85                 CLIP8(sample);
86                 *samples++ = sample << 8;
87                 sample += ws_adpcm_2bit[(code >> 4) & 0x3];
88                 CLIP8(sample);
89                 *samples++ = sample << 8;
90                 sample += ws_adpcm_2bit[(code >> 6) & 0x3];
91                 CLIP8(sample);
92                 *samples++ = sample << 8;
93                 out_size -= 4;
94             }
95             break;
96         case 1: /* ADPCM 4-bit */
97             for (count++; count > 0; count--) {
98                 code = *buf++;
99                 sample += ws_adpcm_4bit[code & 0xF];
100                 CLIP8(sample);
101                 *samples++ = sample << 8;
102                 sample += ws_adpcm_4bit[code >> 4];
103                 CLIP8(sample);
104                 *samples++ = sample << 8;
105                 out_size -= 2;
106             }
107             break;
108         case 2: /* no compression */
109             if (count & 0x20) { /* big delta */
110                 char t;
111                 t = count;
112                 t <<= 3;
113                 sample += t >> 3;
114                 *samples++ = sample << 8;
115                 out_size--;
116             } else { /* copy */
117                 for (count++; count > 0; count--) {
118                     *samples++ = (*buf++ - 0x80) << 8;
119                     out_size--;
120                 }
121                 sample = buf[-1] - 0x80;
122             }
123             break;
124         default: /* run */
125             for(count++; count > 0; count--) {
126                 *samples++ = sample << 8;
127                 out_size--;
128             }
129         }
130     }
131
132     return buf_size;
133 }
134
135 AVCodec ws_snd1_decoder = {
136     "ws_snd1",
137     CODEC_TYPE_AUDIO,
138     CODEC_ID_WESTWOOD_SND1,
139     0,
140     ws_snd_decode_init,
141     NULL,
142     NULL,
143     ws_snd_decode_frame,
144 };