Mercurial > mplayer.hg
annotate libmpcodecs/ad_dk3adpcm.c @ 31382:f7b0ceac5aae
Move LIRC-related extern variable declarations to lirc.h.
author | diego |
---|---|
date | Thu, 17 Jun 2010 09:19:05 +0000 |
parents | cc27da5d7286 |
children | b40d593a463f |
rev | line source |
---|---|
5408 | 1 /* |
30421
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
2 * DK3 ADPCM decoder |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
3 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
4 * "This format number was used by Duck Corp. but not officially |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
5 * registered with Microsoft" |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
6 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
7 * This file is responsible for decoding audio data encoded with |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
8 * Duck Corp's DK3 ADPCM algorithm. Details about the data format |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
9 * can be found here: |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
10 * http://www.pcisys.net/~melanson/codecs/ |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
11 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
12 * Copyright (c) 2002 Mike Melanson |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
13 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
14 * This file is part of MPlayer. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
15 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
16 * MPlayer is free software; you can redistribute it and/or modify |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
17 * it under the terms of the GNU General Public License as published by |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
18 * the Free Software Foundation; either version 2 of the License, or |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
19 * (at your option) any later version. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
20 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
21 * MPlayer is distributed in the hope that it will be useful, |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
24 * GNU General Public License for more details. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
25 * |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
26 * You should have received a copy of the GNU General Public License along |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
27 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
bbb6ebec87a0
Add missing license headers to all files in the libmpcodecs directory.
diego
parents:
29263
diff
changeset
|
29 */ |
5408 | 30 |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
31 #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
|
32 #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
|
33 #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
|
34 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
35 #include "config.h" |
21507
fa99b3d31d13
Hack around libavutil/bswap.h compilation problems due to always_inline undefined.
reimar
parents:
21372
diff
changeset
|
36 #include "mpbswap.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
|
37 #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
|
38 |
30504
cc27da5d7286
Mark all ad_info_t/vd_info_t structure declarations as const.
diego
parents:
30421
diff
changeset
|
39 static const ad_info_t info = |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
40 { |
7191
1eadce15446c
-afm/-vfm help implemenetd, some cosmetics of ad/vd codec names/comments
arpi
parents:
7180
diff
changeset
|
41 "Duck Corp. DK3 ADPCM decoder", |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
42 "dk3adpcm", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
43 "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
|
44 "Mike Melanson", |
7191
1eadce15446c
-afm/-vfm help implemenetd, some cosmetics of ad/vd codec names/comments
arpi
parents:
7180
diff
changeset
|
45 "" |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
46 }; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
47 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
48 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
|
49 |
5408 | 50 #define DK3_ADPCM_PREAMBLE_SIZE 16 |
51 | |
52 #define LE_16(x) (le2me_16(*(unsigned short *)(x))) | |
53 #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
|
54 |
5408 | 55 // useful macros |
56 // clamp a number between 0 and 88 | |
57 #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88; | |
58 // clamp a number within a signed 16-bit range | |
59 #define CLAMP_S16(x) if (x < -32768) x = -32768; \ | |
60 else if (x > 32767) x = 32767; | |
61 // clamp a number above 16 | |
62 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16; | |
63 // sign extend a 16-bit value | |
64 #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000; | |
65 // sign extend a 4-bit value | |
66 #define SE_4BIT(x) if (x & 0x8) x -= 0x10; | |
67 | |
68 // pertinent tables | |
69 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
|
70 { |
5408 | 71 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, |
72 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, | |
73 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, | |
74 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, | |
75 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, | |
76 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, | |
77 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, | |
78 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | |
79 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 | |
80 }; | |
81 | |
82 static int adpcm_index[16] = | |
83 { | |
84 -1, -1, -1, -1, 2, 4, 6, 8, | |
85 -1, -1, -1, -1, 2, 4, 6, 8 | |
86 }; | |
87 | |
88 static int preinit(sh_audio_t *sh_audio) | |
89 { | |
90 sh_audio->audio_out_minsize = sh_audio->wf->nBlockAlign * 6; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26280
diff
changeset
|
91 sh_audio->ds->ss_div = |
5408 | 92 (sh_audio->wf->nBlockAlign - DK3_ADPCM_PREAMBLE_SIZE) * 8 / 3; |
5458 | 93 sh_audio->audio_in_minsize= |
5408 | 94 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
|
95 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
|
96 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
97 |
5408 | 98 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
|
99 { |
5408 | 100 sh_audio->channels = sh_audio->wf->nChannels; |
101 sh_audio->samplerate = sh_audio->wf->nSamplesPerSec; | |
102 sh_audio->i_bps = | |
103 (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div; | |
13427
9d0b052c4f74
setting samplesize to 2 in decoders where neccessary.
reimar
parents:
7191
diff
changeset
|
104 sh_audio->samplesize=2; |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
105 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
|
106 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
107 |
5408 | 108 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
|
109 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
110 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
111 |
5481 | 112 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
|
113 { |
5481 | 114 if(cmd==ADCTRL_SKIP_FRAME){ |
115 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul); | |
116 return CONTROL_TRUE; | |
117 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
118 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
|
119 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
120 |
5408 | 121 #define DK3_GET_NEXT_NIBBLE() \ |
122 if (decode_top_nibble_next) \ | |
123 { \ | |
124 nibble = (last_byte >> 4) & 0x0F; \ | |
125 decode_top_nibble_next = 0; \ | |
126 } \ | |
127 else \ | |
128 { \ | |
129 last_byte = input[in_ptr++]; \ | |
130 nibble = last_byte & 0x0F; \ | |
131 decode_top_nibble_next = 1; \ | |
132 } | |
133 | |
134 // note: This decoder assumes the format 0x62 data always comes in | |
135 // stereo flavor | |
136 static int dk3_adpcm_decode_block(unsigned short *output, unsigned char *input, | |
137 int block_size) | |
138 { | |
139 int sum_pred; | |
140 int diff_pred; | |
141 int sum_index; | |
142 int diff_index; | |
143 int diff_channel; | |
144 int in_ptr = 0x10; | |
145 int out_ptr = 0; | |
146 | |
147 unsigned char last_byte = 0; | |
148 unsigned char nibble; | |
149 int decode_top_nibble_next = 0; | |
150 | |
151 // ADPCM work variables | |
152 int sign; | |
153 int delta; | |
154 int step; | |
155 int diff; | |
156 | |
157 sum_pred = LE_16(&input[10]); | |
158 diff_pred = LE_16(&input[12]); | |
159 SE_16BIT(sum_pred); | |
160 SE_16BIT(diff_pred); | |
161 diff_channel = diff_pred; | |
162 sum_index = input[14]; | |
163 diff_index = input[15]; | |
164 | |
16965
d8755974657a
attempt to fix missing and/or broken boundary checks
reimar
parents:
13427
diff
changeset
|
165 while (in_ptr < block_size - !decode_top_nibble_next) |
5408 | 166 // while (in_ptr < 2048) |
167 { | |
168 // process the first predictor of the sum channel | |
169 DK3_GET_NEXT_NIBBLE(); | |
170 | |
171 step = adpcm_step[sum_index]; | |
172 | |
173 sign = nibble & 8; | |
174 delta = nibble & 7; | |
175 | |
176 diff = step >> 3; | |
177 if (delta & 4) diff += step; | |
178 if (delta & 2) diff += step >> 1; | |
179 if (delta & 1) diff += step >> 2; | |
180 | |
181 if (sign) | |
182 sum_pred -= diff; | |
183 else | |
184 sum_pred += diff; | |
185 | |
186 CLAMP_S16(sum_pred); | |
187 | |
188 sum_index += adpcm_index[nibble]; | |
189 CLAMP_0_TO_88(sum_index); | |
190 | |
191 // process the diff channel predictor | |
192 DK3_GET_NEXT_NIBBLE(); | |
193 | |
194 step = adpcm_step[diff_index]; | |
195 | |
196 sign = nibble & 8; | |
197 delta = nibble & 7; | |
198 | |
199 diff = step >> 3; | |
200 if (delta & 4) diff += step; | |
201 if (delta & 2) diff += step >> 1; | |
202 if (delta & 1) diff += step >> 2; | |
203 | |
204 if (sign) | |
205 diff_pred -= diff; | |
206 else | |
207 diff_pred += diff; | |
208 | |
209 CLAMP_S16(diff_pred); | |
210 | |
211 diff_index += adpcm_index[nibble]; | |
212 CLAMP_0_TO_88(diff_index); | |
213 | |
214 // output the first pair of stereo PCM samples | |
215 diff_channel = (diff_channel + diff_pred) / 2; | |
216 output[out_ptr++] = sum_pred + diff_channel; | |
217 output[out_ptr++] = sum_pred - diff_channel; | |
218 | |
219 // process the second predictor of the sum channel | |
220 DK3_GET_NEXT_NIBBLE(); | |
221 | |
222 step = adpcm_step[sum_index]; | |
223 | |
224 sign = nibble & 8; | |
225 delta = nibble & 7; | |
226 | |
227 diff = step >> 3; | |
228 if (delta & 4) diff += step; | |
229 if (delta & 2) diff += step >> 1; | |
230 if (delta & 1) diff += step >> 2; | |
231 | |
232 if (sign) | |
233 sum_pred -= diff; | |
234 else | |
235 sum_pred += diff; | |
236 | |
237 CLAMP_S16(sum_pred); | |
238 | |
239 sum_index += adpcm_index[nibble]; | |
240 CLAMP_0_TO_88(sum_index); | |
241 | |
242 // output the second pair of stereo PCM samples | |
243 output[out_ptr++] = sum_pred + diff_channel; | |
244 output[out_ptr++] = sum_pred - diff_channel; | |
245 } | |
246 | |
247 return out_ptr; | |
248 } | |
249 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
250 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
|
251 { |
5408 | 252 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer, |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26280
diff
changeset
|
253 sh_audio->ds->ss_mul) != |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
26280
diff
changeset
|
254 sh_audio->ds->ss_mul) |
5408 | 255 return -1; /* EOF */ |
256 | |
16965
d8755974657a
attempt to fix missing and/or broken boundary checks
reimar
parents:
13427
diff
changeset
|
257 if (maxlen < 2 * 4 * sh_audio->wf->nBlockAlign * 2 / 3) { |
d8755974657a
attempt to fix missing and/or broken boundary checks
reimar
parents:
13427
diff
changeset
|
258 mp_msg(MSGT_DECAUDIO, MSGL_V, "dk3adpcm: maxlen too small in decode_audio\n"); |
d8755974657a
attempt to fix missing and/or broken boundary checks
reimar
parents:
13427
diff
changeset
|
259 return -1; |
d8755974657a
attempt to fix missing and/or broken boundary checks
reimar
parents:
13427
diff
changeset
|
260 } |
5408 | 261 return 2 * dk3_adpcm_decode_block( |
262 (unsigned short*)buf, sh_audio->a_in_buffer, | |
16965
d8755974657a
attempt to fix missing and/or broken boundary checks
reimar
parents:
13427
diff
changeset
|
263 sh_audio->ds->ss_mul); |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
264 } |