]> git.sesse.net Git - ffmpeg/blob - libavcodec/ws-snd1.c
slightly faster on P3 slightly slower on athlon and probably faster on P4
[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 typedef struct {
33 } WSSNDContext;
34
35 static const char ws_adpcm_2bit[] = { -2, -1, 0, 1};
36 static const char ws_adpcm_4bit[] = {
37     -9, -8, -6, -5, -4, -3, -2, -1,
38      0,  1,  2,  3,  4,  5,  6,  8 };
39
40 #define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128;
41
42 static int ws_snd_decode_init(AVCodecContext * avctx)
43 {
44 //    WSSNDContext *c = avctx->priv_data;
45
46     return 0;
47 }
48
49 static int ws_snd_decode_frame(AVCodecContext *avctx,
50                 void *data, int *data_size,
51                 uint8_t *buf, int buf_size)
52 {
53 //    WSSNDContext *c = avctx->priv_data;
54
55     int in_size, out_size;
56     int sample = 0;
57     int i;
58     short *samples = data;
59
60     if (!buf_size)
61         return 0;
62
63     out_size = LE_16(&buf[0]);
64     *data_size = out_size * 2;
65     in_size = LE_16(&buf[2]);
66     buf += 4;
67
68     if (in_size == out_size) {
69         for (i = 0; i < out_size; i++)
70             *samples++ = (*buf++ - 0x80) << 8;
71         return buf_size;
72     }
73
74     while (out_size > 0) {
75         int code;
76         uint8_t count;
77         code = (*buf) >> 6;
78         count = (*buf) & 0x3F;
79         buf++;
80         switch(code) {
81         case 0: /* ADPCM 2-bit */
82             for (count++; count > 0; count--) {
83                 code = *buf++;
84                 sample += ws_adpcm_2bit[code & 0x3];
85                 CLIP8(sample);
86                 *samples++ = sample << 8;
87                 sample += ws_adpcm_2bit[(code >> 2) & 0x3];
88                 CLIP8(sample);
89                 *samples++ = sample << 8;
90                 sample += ws_adpcm_2bit[(code >> 4) & 0x3];
91                 CLIP8(sample);
92                 *samples++ = sample << 8;
93                 sample += ws_adpcm_2bit[(code >> 6) & 0x3];
94                 CLIP8(sample);
95                 *samples++ = sample << 8;
96                 out_size -= 4;
97             }
98             break;
99         case 1: /* ADPCM 4-bit */
100             for (count++; count > 0; count--) {
101                 code = *buf++;
102                 sample += ws_adpcm_4bit[code & 0xF];
103                 CLIP8(sample);
104                 *samples++ = sample << 8;
105                 sample += ws_adpcm_4bit[code >> 4];
106                 CLIP8(sample);
107                 *samples++ = sample << 8;
108                 out_size -= 2;
109             }
110             break;
111         case 2: /* no compression */
112             if (count & 0x20) { /* big delta */
113                 char t;
114                 t = count;
115                 t <<= 3;
116                 sample += t >> 3;
117                 *samples++ = sample << 8;
118                 out_size--;
119             } else { /* copy */
120                 for (count++; count > 0; count--) {
121                     *samples++ = (*buf++ - 0x80) << 8;
122                     out_size--;
123                 }
124                 sample = buf[-1] - 0x80;
125             }
126             break;
127         default: /* run */
128             for(count++; count > 0; count--) {
129                 *samples++ = sample << 8;
130                 out_size--;
131             }
132         }
133     }
134
135     return buf_size;
136 }
137
138 AVCodec ws_snd1_decoder = {
139     "ws_snd1",
140     CODEC_TYPE_AUDIO,
141     CODEC_ID_WESTWOOD_SND1,
142     sizeof(WSSNDContext),
143     ws_snd_decode_init,
144     NULL,
145     NULL,
146     ws_snd_decode_frame,
147 };