Mercurial > mplayer.hg
annotate libmpcodecs/ad_dk3adpcm.c @ 6717:9d34c286f0ba
Patch by Jiri Svoboda:
-should not fail if keyboard not present
-preparation for vsync flipping
-card capabilities should be reported better at query format
author | atmos4 |
---|---|
date | Fri, 12 Jul 2002 10:27:08 +0000 |
parents | 8ae2bf330ad5 |
children | 28677d779205 |
rev | line source |
---|---|
5408 | 1 /* |
2 DK3 ADPCM Decoder for MPlayer | |
3 by Mike Melanson | |
4 | |
5 This file is responsible for decoding audio data encoded with | |
6 Duck Corp's DK3 ADPCM algorithm. Details about the data format | |
7 can be found here: | |
8 http://www.pcisys.net/~melanson/codecs/ | |
9 */ | |
10 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
11 #include <stdio.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
12 #include <stdlib.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
13 #include <unistd.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
14 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
15 #include "config.h" |
5408 | 16 #include "bswap.h" |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
17 #include "ad_internal.h" |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
18 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
19 static ad_info_t info = |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
20 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
21 "Duck DK3 ADPCM decoder", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
22 "dk3adpcm", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
23 AFM_DK3ADPCM, |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
24 "Nick Kurshev", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
25 "Mike Melanson", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
26 "This format number was used by Duck Corp. but not officially registered with Microsoft" |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
27 }; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
28 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
29 LIBAD_EXTERN(dk3adpcm) |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
30 |
5408 | 31 #define DK3_ADPCM_PREAMBLE_SIZE 16 |
32 | |
33 #define LE_16(x) (le2me_16(*(unsigned short *)(x))) | |
34 #define LE_32(x) (le2me_32(*(unsigned int *)(x))) | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
35 |
5408 | 36 // useful macros |
37 // clamp a number between 0 and 88 | |
38 #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88; | |
39 // clamp a number within a signed 16-bit range | |
40 #define CLAMP_S16(x) if (x < -32768) x = -32768; \ | |
41 else if (x > 32767) x = 32767; | |
42 // clamp a number above 16 | |
43 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16; | |
44 // sign extend a 16-bit value | |
45 #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000; | |
46 // sign extend a 4-bit value | |
47 #define SE_4BIT(x) if (x & 0x8) x -= 0x10; | |
48 | |
49 // pertinent tables | |
50 static int adpcm_step[89] = | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
51 { |
5408 | 52 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, |
53 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, | |
54 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, | |
55 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, | |
56 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, | |
57 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, | |
58 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, | |
59 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | |
60 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 | |
61 }; | |
62 | |
63 static int adpcm_index[16] = | |
64 { | |
65 -1, -1, -1, -1, 2, 4, 6, 8, | |
66 -1, -1, -1, -1, 2, 4, 6, 8 | |
67 }; | |
68 | |
69 static int preinit(sh_audio_t *sh_audio) | |
70 { | |
71 sh_audio->audio_out_minsize = sh_audio->wf->nBlockAlign * 6; | |
72 sh_audio->ds->ss_div = | |
73 (sh_audio->wf->nBlockAlign - DK3_ADPCM_PREAMBLE_SIZE) * 8 / 3; | |
5458 | 74 sh_audio->audio_in_minsize= |
5408 | 75 sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign; |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
76 return 1; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
77 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
78 |
5408 | 79 static int init(sh_audio_t *sh_audio) |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
80 { |
5408 | 81 sh_audio->channels = sh_audio->wf->nChannels; |
82 sh_audio->samplerate = sh_audio->wf->nSamplesPerSec; | |
83 sh_audio->i_bps = | |
84 (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div; | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
85 return 1; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
86 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
87 |
5408 | 88 static void uninit(sh_audio_t *sh_audio) |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
89 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
90 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
91 |
5481 | 92 static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...) |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
93 { |
5481 | 94 if(cmd==ADCTRL_SKIP_FRAME){ |
95 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul); | |
96 return CONTROL_TRUE; | |
97 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
98 return CONTROL_UNKNOWN; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
99 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
100 |
5408 | 101 #define DK3_GET_NEXT_NIBBLE() \ |
102 if (decode_top_nibble_next) \ | |
103 { \ | |
104 nibble = (last_byte >> 4) & 0x0F; \ | |
105 decode_top_nibble_next = 0; \ | |
106 } \ | |
107 else \ | |
108 { \ | |
109 last_byte = input[in_ptr++]; \ | |
110 nibble = last_byte & 0x0F; \ | |
111 decode_top_nibble_next = 1; \ | |
112 } | |
113 | |
114 // note: This decoder assumes the format 0x62 data always comes in | |
115 // stereo flavor | |
116 static int dk3_adpcm_decode_block(unsigned short *output, unsigned char *input, | |
117 int block_size) | |
118 { | |
119 int sum_pred; | |
120 int diff_pred; | |
121 int sum_index; | |
122 int diff_index; | |
123 int diff_channel; | |
124 int in_ptr = 0x10; | |
125 int out_ptr = 0; | |
126 | |
127 unsigned char last_byte = 0; | |
128 unsigned char nibble; | |
129 int decode_top_nibble_next = 0; | |
130 | |
131 // ADPCM work variables | |
132 int sign; | |
133 int delta; | |
134 int step; | |
135 int diff; | |
136 | |
137 sum_pred = LE_16(&input[10]); | |
138 diff_pred = LE_16(&input[12]); | |
139 SE_16BIT(sum_pred); | |
140 SE_16BIT(diff_pred); | |
141 diff_channel = diff_pred; | |
142 sum_index = input[14]; | |
143 diff_index = input[15]; | |
144 | |
145 while (in_ptr < block_size) | |
146 // while (in_ptr < 2048) | |
147 { | |
148 // process the first predictor of the sum channel | |
149 DK3_GET_NEXT_NIBBLE(); | |
150 | |
151 step = adpcm_step[sum_index]; | |
152 | |
153 sign = nibble & 8; | |
154 delta = nibble & 7; | |
155 | |
156 diff = step >> 3; | |
157 if (delta & 4) diff += step; | |
158 if (delta & 2) diff += step >> 1; | |
159 if (delta & 1) diff += step >> 2; | |
160 | |
161 if (sign) | |
162 sum_pred -= diff; | |
163 else | |
164 sum_pred += diff; | |
165 | |
166 CLAMP_S16(sum_pred); | |
167 | |
168 sum_index += adpcm_index[nibble]; | |
169 CLAMP_0_TO_88(sum_index); | |
170 | |
171 // process the diff channel predictor | |
172 DK3_GET_NEXT_NIBBLE(); | |
173 | |
174 step = adpcm_step[diff_index]; | |
175 | |
176 sign = nibble & 8; | |
177 delta = nibble & 7; | |
178 | |
179 diff = step >> 3; | |
180 if (delta & 4) diff += step; | |
181 if (delta & 2) diff += step >> 1; | |
182 if (delta & 1) diff += step >> 2; | |
183 | |
184 if (sign) | |
185 diff_pred -= diff; | |
186 else | |
187 diff_pred += diff; | |
188 | |
189 CLAMP_S16(diff_pred); | |
190 | |
191 diff_index += adpcm_index[nibble]; | |
192 CLAMP_0_TO_88(diff_index); | |
193 | |
194 // output the first pair of stereo PCM samples | |
195 diff_channel = (diff_channel + diff_pred) / 2; | |
196 output[out_ptr++] = sum_pred + diff_channel; | |
197 output[out_ptr++] = sum_pred - diff_channel; | |
198 | |
199 // process the second predictor of the sum channel | |
200 DK3_GET_NEXT_NIBBLE(); | |
201 | |
202 step = adpcm_step[sum_index]; | |
203 | |
204 sign = nibble & 8; | |
205 delta = nibble & 7; | |
206 | |
207 diff = step >> 3; | |
208 if (delta & 4) diff += step; | |
209 if (delta & 2) diff += step >> 1; | |
210 if (delta & 1) diff += step >> 2; | |
211 | |
212 if (sign) | |
213 sum_pred -= diff; | |
214 else | |
215 sum_pred += diff; | |
216 | |
217 CLAMP_S16(sum_pred); | |
218 | |
219 sum_index += adpcm_index[nibble]; | |
220 CLAMP_0_TO_88(sum_index); | |
221 | |
222 // output the second pair of stereo PCM samples | |
223 output[out_ptr++] = sum_pred + diff_channel; | |
224 output[out_ptr++] = sum_pred - diff_channel; | |
225 } | |
226 | |
227 return out_ptr; | |
228 } | |
229 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
230 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen) |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
231 { |
5408 | 232 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer, |
233 sh_audio->ds->ss_mul) != | |
234 sh_audio->ds->ss_mul) | |
235 return -1; /* EOF */ | |
236 | |
237 return 2 * dk3_adpcm_decode_block( | |
238 (unsigned short*)buf, sh_audio->a_in_buffer, | |
239 sh_audio->wf->nBlockAlign); | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
240 } |