demux_audio: fix WAVEFORMATEXTENSIBLE support
[mplayer/glamo.git] / libmpcodecs / ad_dk3adpcm.c
blobd1792ee46b9853c05d0b1d8f1ac60305f813b4dd
1 /*
2 * DK3 ADPCM decoder
4 * "This format number was used by Duck Corp. but not officially
5 * registered with Microsoft"
7 * This file is responsible for decoding audio data encoded with
8 * Duck Corp's DK3 ADPCM algorithm. Details about the data format
9 * can be found here:
10 * http://www.pcisys.net/~melanson/codecs/
12 * Copyright (c) 2002 Mike Melanson
14 * This file is part of MPlayer.
16 * MPlayer is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * MPlayer is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
35 #include "config.h"
36 #include "ffmpeg_files/intreadwrite.h"
37 #include "ad_internal.h"
39 static const ad_info_t info =
41 "Duck Corp. DK3 ADPCM decoder",
42 "dk3adpcm",
43 "Nick Kurshev",
44 "Mike Melanson",
48 LIBAD_EXTERN(dk3adpcm)
50 #define DK3_ADPCM_PREAMBLE_SIZE 16
52 // useful macros
53 // clamp a number between 0 and 88
54 #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88;
55 // clamp a number within a signed 16-bit range
56 #define CLAMP_S16(x) if (x < -32768) x = -32768; \
57 else if (x > 32767) x = 32767;
58 // clamp a number above 16
59 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16;
60 // sign extend a 16-bit value
61 #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000;
62 // sign extend a 4-bit value
63 #define SE_4BIT(x) if (x & 0x8) x -= 0x10;
65 // pertinent tables
66 static int adpcm_step[89] =
68 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
69 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
70 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
71 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
72 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
73 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
74 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
75 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
76 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
79 static int adpcm_index[16] =
81 -1, -1, -1, -1, 2, 4, 6, 8,
82 -1, -1, -1, -1, 2, 4, 6, 8
85 static int preinit(sh_audio_t *sh_audio)
87 sh_audio->audio_out_minsize = sh_audio->wf->nBlockAlign * 6;
88 sh_audio->ds->ss_div =
89 (sh_audio->wf->nBlockAlign - DK3_ADPCM_PREAMBLE_SIZE) * 8 / 3;
90 sh_audio->audio_in_minsize=
91 sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign;
92 return 1;
95 static int init(sh_audio_t *sh_audio)
97 sh_audio->channels = sh_audio->wf->nChannels;
98 sh_audio->samplerate = sh_audio->wf->nSamplesPerSec;
99 sh_audio->i_bps =
100 (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div;
101 sh_audio->samplesize=2;
102 return 1;
105 static void uninit(sh_audio_t *sh_audio)
109 static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...)
111 if(cmd==ADCTRL_SKIP_FRAME){
112 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul);
113 return CONTROL_TRUE;
115 return CONTROL_UNKNOWN;
118 #define DK3_GET_NEXT_NIBBLE() \
119 if (decode_top_nibble_next) \
121 nibble = (last_byte >> 4) & 0x0F; \
122 decode_top_nibble_next = 0; \
124 else \
126 last_byte = input[in_ptr++]; \
127 nibble = last_byte & 0x0F; \
128 decode_top_nibble_next = 1; \
131 // note: This decoder assumes the format 0x62 data always comes in
132 // stereo flavor
133 static int dk3_adpcm_decode_block(unsigned short *output, unsigned char *input,
134 int block_size)
136 int sum_pred;
137 int diff_pred;
138 int sum_index;
139 int diff_index;
140 int diff_channel;
141 int in_ptr = 0x10;
142 int out_ptr = 0;
144 unsigned char last_byte = 0;
145 unsigned char nibble;
146 int decode_top_nibble_next = 0;
148 // ADPCM work variables
149 int sign;
150 int delta;
151 int step;
152 int diff;
154 sum_pred = AV_RL16(&input[10]);
155 diff_pred = AV_RL16(&input[12]);
156 SE_16BIT(sum_pred);
157 SE_16BIT(diff_pred);
158 diff_channel = diff_pred;
159 sum_index = input[14];
160 diff_index = input[15];
162 while (in_ptr < block_size - !decode_top_nibble_next)
163 // while (in_ptr < 2048)
165 // process the first predictor of the sum channel
166 DK3_GET_NEXT_NIBBLE();
168 step = adpcm_step[sum_index];
170 sign = nibble & 8;
171 delta = nibble & 7;
173 diff = step >> 3;
174 if (delta & 4) diff += step;
175 if (delta & 2) diff += step >> 1;
176 if (delta & 1) diff += step >> 2;
178 if (sign)
179 sum_pred -= diff;
180 else
181 sum_pred += diff;
183 CLAMP_S16(sum_pred);
185 sum_index += adpcm_index[nibble];
186 CLAMP_0_TO_88(sum_index);
188 // process the diff channel predictor
189 DK3_GET_NEXT_NIBBLE();
191 step = adpcm_step[diff_index];
193 sign = nibble & 8;
194 delta = nibble & 7;
196 diff = step >> 3;
197 if (delta & 4) diff += step;
198 if (delta & 2) diff += step >> 1;
199 if (delta & 1) diff += step >> 2;
201 if (sign)
202 diff_pred -= diff;
203 else
204 diff_pred += diff;
206 CLAMP_S16(diff_pred);
208 diff_index += adpcm_index[nibble];
209 CLAMP_0_TO_88(diff_index);
211 // output the first pair of stereo PCM samples
212 diff_channel = (diff_channel + diff_pred) / 2;
213 output[out_ptr++] = sum_pred + diff_channel;
214 output[out_ptr++] = sum_pred - diff_channel;
216 // process the second predictor of the sum channel
217 DK3_GET_NEXT_NIBBLE();
219 step = adpcm_step[sum_index];
221 sign = nibble & 8;
222 delta = nibble & 7;
224 diff = step >> 3;
225 if (delta & 4) diff += step;
226 if (delta & 2) diff += step >> 1;
227 if (delta & 1) diff += step >> 2;
229 if (sign)
230 sum_pred -= diff;
231 else
232 sum_pred += diff;
234 CLAMP_S16(sum_pred);
236 sum_index += adpcm_index[nibble];
237 CLAMP_0_TO_88(sum_index);
239 // output the second pair of stereo PCM samples
240 output[out_ptr++] = sum_pred + diff_channel;
241 output[out_ptr++] = sum_pred - diff_channel;
244 return out_ptr;
247 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
249 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,
250 sh_audio->ds->ss_mul) !=
251 sh_audio->ds->ss_mul)
252 return -1; /* EOF */
254 if (maxlen < 2 * 4 * sh_audio->wf->nBlockAlign * 2 / 3) {
255 mp_msg(MSGT_DECAUDIO, MSGL_V, "dk3adpcm: maxlen too small in decode_audio\n");
256 return -1;
258 return 2 * dk3_adpcm_decode_block(
259 (unsigned short*)buf, sh_audio->a_in_buffer,
260 sh_audio->ds->ss_mul);