annotate ws-snd1.c @ 3953:ded320ef2a87 libavcodec

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