annotate ws-snd1.c @ 2743:9f469a5d3a33 libavcodec

more trivial optimizations
author michael
date Mon, 30 May 2005 20:34:05 +0000
parents 1ef8fab234c8
children ef2149182f1c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2585
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
1 /*
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
2 * Westwood SNDx codecs
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
3 * Copyright (c) 2005 Konstantin Shishkov
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
4 *
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
9 *
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
13 * Lesser General Public License for more details.
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
14 *
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
18 */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
19 #include "avcodec.h"
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
20
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
21 /**
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
22 * @file ws-snd.c
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
23 * Westwood SNDx codecs.
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
24 *
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
25 * Reference documents about VQA format and its audio codecs
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
26 * can be found here:
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
27 * http://www.multimedia.cx
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
28 */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
29
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
30 typedef struct {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
31 } WSSNDContext;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
32
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
33 static const char ws_adpcm_2bit[] = { -2, -1, 0, 1};
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
34 static const char ws_adpcm_4bit[] = {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
35 -9, -8, -6, -5, -4, -3, -2, -1,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
36 0, 1, 2, 3, 4, 5, 6, 8 };
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
37
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
38 #define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
39
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
40 static int ws_snd_decode_init(AVCodecContext * avctx)
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
41 {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
42 // WSSNDContext *c = avctx->priv_data;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
43
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
44 return 0;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
45 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
46
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
47 static int ws_snd_decode_frame(AVCodecContext *avctx,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
48 void *data, int *data_size,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
49 uint8_t *buf, int buf_size)
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
50 {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
51 // WSSNDContext *c = avctx->priv_data;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
52
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
53 int in_size, out_size;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
54 int sample = 0;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
55 int i;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
56 short *samples = data;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
57
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
58 if (!buf_size)
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
59 return 0;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
60
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
61 out_size = LE_16(&buf[0]);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
62 *data_size = out_size * 2;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
63 in_size = LE_16(&buf[2]);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
64 buf += 4;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
65
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
66 if (in_size == out_size) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
67 for (i = 0; i < out_size; i++)
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
68 *samples++ = (*buf++ - 0x80) << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
69 return buf_size;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
70 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
71
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
72 while (out_size > 0) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
73 int code;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
74 uint8_t count;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
75 code = (*buf) >> 6;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
76 count = (*buf) & 0x3F;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
77 buf++;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
78 switch(code) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
79 case 0: /* ADPCM 2-bit */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
80 for (count++; count > 0; count--) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
81 code = *buf++;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
82 sample += ws_adpcm_2bit[code & 0x3];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
83 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
84 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
85 sample += ws_adpcm_2bit[(code >> 2) & 0x3];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
86 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
87 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
88 sample += ws_adpcm_2bit[(code >> 4) & 0x3];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
89 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
90 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
91 sample += ws_adpcm_2bit[(code >> 6) & 0x3];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
92 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
93 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
94 out_size -= 4;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
95 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
96 break;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
97 case 1: /* ADPCM 4-bit */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
98 for (count++; count > 0; count--) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
99 code = *buf++;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
100 sample += ws_adpcm_4bit[code & 0xF];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
101 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
102 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
103 sample += ws_adpcm_4bit[code >> 4];
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
104 CLIP8(sample);
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
105 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
106 out_size -= 2;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
107 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
108 break;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
109 case 2: /* no compression */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
110 if (count & 0x20) { /* big delta */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
111 char t;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
112 t = count;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
113 t <<= 3;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
114 sample += t >> 3;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
115 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
116 out_size--;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
117 } else { /* copy */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
118 for (count++; count > 0; count--) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
119 *samples++ = (*buf++ - 0x80) << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
120 out_size--;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
121 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
122 sample = buf[-1] - 0x80;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
123 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
124 break;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
125 default: /* run */
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
126 for(count++; count > 0; count--) {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
127 *samples++ = sample << 8;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
128 out_size--;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
129 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
130 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
131 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
132
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
133 return buf_size;
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
134 }
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
135
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
136 AVCodec ws_snd1_decoder = {
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
137 "ws_snd1",
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
138 CODEC_TYPE_AUDIO,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
139 CODEC_ID_WESTWOOD_SND1,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
140 sizeof(WSSNDContext),
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
141 ws_snd_decode_init,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
142 NULL,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
143 NULL,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
144 ws_snd_decode_frame,
1ef8fab234c8 Westwood SND1 decoder, courtesy of Kostya
melanson
parents:
diff changeset
145 };