Commit | Line | Data |
---|---|---|
0147f198 FR |
1 | /* |
2 | * ADPCM codecs | |
9937e686 | 3 | * Copyright (c) 2001-2003 The ffmpeg Project |
0147f198 FR |
4 | * |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2 of the License, or (at your option) any later version. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | #include "avcodec.h" | |
20 | ||
983e3246 MN |
21 | /** |
22 | * @file adpcm.c | |
23 | * ADPCM codecs. | |
fc384777 | 24 | * First version by Francois Revol (revol@free.fr) |
2fdf638b | 25 | * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood) |
9937e686 | 26 | * by Mike Melanson (melanson@pcisys.net) |
fc384777 | 27 | * CD-ROM XA ADPCM codec by BERO |
0147f198 FR |
28 | * |
29 | * Features and limitations: | |
30 | * | |
31 | * Reference documents: | |
9937e686 | 32 | * http://www.pcisys.net/~melanson/codecs/simpleaudio.html |
0147f198 FR |
33 | * http://www.geocities.com/SiliconValley/8682/aud3.txt |
34 | * http://openquicktime.sourceforge.net/plugins.htm | |
35 | * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html | |
889c5224 FR |
36 | * http://www.cs.ucla.edu/~leec/mediabench/applications.html |
37 | * SoX source code http://home.sprynet.com/~cbagwell/sox.html | |
fc384777 MM |
38 | * |
39 | * CD-ROM XA: | |
40 | * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html | |
41 | * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html | |
42 | * readstr http://www.geocities.co.jp/Playtown/2004/ | |
0147f198 FR |
43 | */ |
44 | ||
45 | #define BLKSIZE 1024 | |
46 | ||
47 | #define CLAMP_TO_SHORT(value) \ | |
48 | if (value > 32767) \ | |
49 | value = 32767; \ | |
50 | else if (value < -32768) \ | |
51 | value = -32768; \ | |
52 | ||
53 | /* step_table[] and index_table[] are from the ADPCM reference source */ | |
54 | /* This is the index table: */ | |
135ee03a | 55 | static const int index_table[16] = { |
0147f198 FR |
56 | -1, -1, -1, -1, 2, 4, 6, 8, |
57 | -1, -1, -1, -1, 2, 4, 6, 8, | |
58 | }; | |
59 | ||
983e3246 MN |
60 | /** |
61 | * This is the step table. Note that many programs use slight deviations from | |
0147f198 FR |
62 | * this table, but such deviations are negligible: |
63 | */ | |
135ee03a | 64 | static const int step_table[89] = { |
0147f198 FR |
65 | 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, |
66 | 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, | |
67 | 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, | |
68 | 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, | |
69 | 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, | |
70 | 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, | |
71 | 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, | |
72 | 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | |
73 | 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 | |
74 | }; | |
75 | ||
fc384777 | 76 | /* These are for MS-ADPCM */ |
0147f198 | 77 | /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */ |
135ee03a | 78 | static const int AdaptationTable[] = { |
0147f198 FR |
79 | 230, 230, 230, 230, 307, 409, 512, 614, |
80 | 768, 614, 512, 409, 307, 230, 230, 230 | |
81 | }; | |
82 | ||
135ee03a | 83 | static const int AdaptCoeff1[] = { |
0147f198 FR |
84 | 256, 512, 0, 192, 240, 460, 392 |
85 | }; | |
86 | ||
135ee03a | 87 | static const int AdaptCoeff2[] = { |
0147f198 FR |
88 | 0, -256, 0, 64, 0, -208, -232 |
89 | }; | |
90 | ||
fc384777 | 91 | /* These are for CD-ROM XA ADPCM */ |
1ffb0091 | 92 | static const int xa_adpcm_table[5][2] = { |
fc384777 MM |
93 | { 0, 0 }, |
94 | { 60, 0 }, | |
95 | { 115, -52 }, | |
96 | { 98, -55 }, | |
97 | { 122, -60 } | |
98 | }; | |
99 | ||
0147f198 FR |
100 | /* end of tables */ |
101 | ||
102 | typedef struct ADPCMChannelStatus { | |
103 | int predictor; | |
104 | short int step_index; | |
105 | int step; | |
889c5224 FR |
106 | /* for encoding */ |
107 | int prev_sample; | |
0147f198 FR |
108 | |
109 | /* MS version */ | |
110 | short sample1; | |
111 | short sample2; | |
112 | int coeff1; | |
113 | int coeff2; | |
114 | int idelta; | |
115 | } ADPCMChannelStatus; | |
116 | ||
117 | typedef struct ADPCMContext { | |
118 | int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */ | |
119 | ADPCMChannelStatus status[2]; | |
120 | short sample_buffer[32]; /* hold left samples while waiting for right samples */ | |
121 | } ADPCMContext; | |
122 | ||
123 | /* XXX: implement encoding */ | |
124 | ||
764ef400 | 125 | #ifdef CONFIG_ENCODERS |
0147f198 FR |
126 | static int adpcm_encode_init(AVCodecContext *avctx) |
127 | { | |
889c5224 FR |
128 | if (avctx->channels > 2) |
129 | return -1; /* only stereo or mono =) */ | |
0147f198 FR |
130 | switch(avctx->codec->id) { |
131 | case CODEC_ID_ADPCM_IMA_QT: | |
8dbcc9f2 | 132 | av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n"); |
889c5224 FR |
133 | avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */ |
134 | return -1; | |
0147f198 FR |
135 | break; |
136 | case CODEC_ID_ADPCM_IMA_WAV: | |
889c5224 FR |
137 | avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */ |
138 | /* and we have 4 bytes per channel overhead */ | |
139 | avctx->block_align = BLKSIZE; | |
140 | /* seems frame_size isn't taken into account... have to buffer the samples :-( */ | |
141 | break; | |
142 | case CODEC_ID_ADPCM_MS: | |
8dbcc9f2 | 143 | av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ms unsupported for encoding !\n"); |
889c5224 | 144 | return -1; |
0147f198 FR |
145 | break; |
146 | default: | |
889c5224 | 147 | return -1; |
0147f198 FR |
148 | break; |
149 | } | |
492cd3a9 MN |
150 | |
151 | avctx->coded_frame= avcodec_alloc_frame(); | |
152 | avctx->coded_frame->key_frame= 1; | |
153 | ||
0147f198 FR |
154 | return 0; |
155 | } | |
156 | ||
157 | static int adpcm_encode_close(AVCodecContext *avctx) | |
158 | { | |
492cd3a9 MN |
159 | av_freep(&avctx->coded_frame); |
160 | ||
0147f198 FR |
161 | return 0; |
162 | } | |
163 | ||
889c5224 FR |
164 | |
165 | static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample) | |
166 | { | |
167 | int step_index; | |
168 | unsigned char nibble; | |
169 | ||
170 | int sign = 0; /* sign bit of the nibble (MSB) */ | |
171 | int delta, predicted_delta; | |
172 | ||
173 | delta = sample - c->prev_sample; | |
174 | ||
175 | if (delta < 0) { | |
176 | sign = 1; | |
177 | delta = -delta; | |
178 | } | |
179 | ||
180 | step_index = c->step_index; | |
181 | ||
182 | /* nibble = 4 * delta / step_table[step_index]; */ | |
183 | nibble = (delta << 2) / step_table[step_index]; | |
184 | ||
185 | if (nibble > 7) | |
186 | nibble = 7; | |
187 | ||
188 | step_index += index_table[nibble]; | |
189 | if (step_index < 0) | |
190 | step_index = 0; | |
191 | if (step_index > 88) | |
192 | step_index = 88; | |
193 | ||
194 | /* what the decoder will find */ | |
195 | predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8); | |
196 | ||
197 | if (sign) | |
198 | c->prev_sample -= predicted_delta; | |
199 | else | |
200 | c->prev_sample += predicted_delta; | |
201 | ||
202 | CLAMP_TO_SHORT(c->prev_sample); | |
203 | ||
204 | ||
205 | nibble += sign << 3; /* sign * 8 */ | |
206 | ||
207 | /* save back */ | |
208 | c->step_index = step_index; | |
209 | ||
210 | return nibble; | |
211 | } | |
212 | ||
0147f198 FR |
213 | static int adpcm_encode_frame(AVCodecContext *avctx, |
214 | unsigned char *frame, int buf_size, void *data) | |
215 | { | |
889c5224 | 216 | int n; |
0147f198 FR |
217 | short *samples; |
218 | unsigned char *dst; | |
889c5224 FR |
219 | ADPCMContext *c = avctx->priv_data; |
220 | ||
221 | dst = frame; | |
222 | samples = (short *)data; | |
223 | /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */ | |
0147f198 FR |
224 | |
225 | switch(avctx->codec->id) { | |
889c5224 FR |
226 | case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */ |
227 | break; | |
228 | case CODEC_ID_ADPCM_IMA_WAV: | |
229 | n = avctx->frame_size / 8; | |
230 | c->status[0].prev_sample = (signed short)samples[0]; /* XXX */ | |
231 | /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */ | |
232 | *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */ | |
233 | *dst++ = (c->status[0].prev_sample >> 8) & 0xFF; | |
234 | *dst++ = (unsigned char)c->status[0].step_index; | |
235 | *dst++ = 0; /* unknown */ | |
236 | samples++; | |
237 | if (avctx->channels == 2) { | |
1ffb0091 | 238 | c->status[1].prev_sample = (signed short)samples[1]; |
889c5224 FR |
239 | /* c->status[1].step_index = 0; */ |
240 | *dst++ = (c->status[1].prev_sample) & 0xFF; | |
241 | *dst++ = (c->status[1].prev_sample >> 8) & 0xFF; | |
242 | *dst++ = (unsigned char)c->status[1].step_index; | |
243 | *dst++ = 0; | |
244 | samples++; | |
245 | } | |
246 | ||
247 | /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */ | |
248 | for (; n>0; n--) { | |
249 | *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F; | |
250 | *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0; | |
251 | dst++; | |
252 | *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F; | |
253 | *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0; | |
254 | dst++; | |
255 | *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F; | |
256 | *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0; | |
257 | dst++; | |
258 | *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F; | |
259 | *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0; | |
260 | dst++; | |
261 | /* right channel */ | |
262 | if (avctx->channels == 2) { | |
263 | *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]); | |
264 | *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4; | |
265 | dst++; | |
266 | *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]); | |
267 | *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4; | |
268 | dst++; | |
269 | *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]); | |
270 | *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4; | |
271 | dst++; | |
272 | *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]); | |
273 | *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4; | |
274 | dst++; | |
275 | } | |
276 | samples += 8 * avctx->channels; | |
277 | } | |
278 | break; | |
0147f198 FR |
279 | default: |
280 | return -1; | |
281 | } | |
0147f198 FR |
282 | return dst - frame; |
283 | } | |
764ef400 | 284 | #endif //CONFIG_ENCODERS |
0147f198 FR |
285 | |
286 | static int adpcm_decode_init(AVCodecContext * avctx) | |
287 | { | |
288 | ADPCMContext *c = avctx->priv_data; | |
289 | ||
290 | c->channel = 0; | |
291 | c->status[0].predictor = c->status[1].predictor = 0; | |
292 | c->status[0].step_index = c->status[1].step_index = 0; | |
293 | c->status[0].step = c->status[1].step = 0; | |
294 | ||
295 | switch(avctx->codec->id) { | |
296 | default: | |
297 | break; | |
298 | } | |
299 | return 0; | |
300 | } | |
301 | ||
302 | static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble) | |
303 | { | |
304 | int step_index; | |
305 | int predictor; | |
306 | int sign, delta, diff, step; | |
307 | ||
135ee03a | 308 | step = step_table[c->step_index]; |
0147f198 FR |
309 | step_index = c->step_index + index_table[(unsigned)nibble]; |
310 | if (step_index < 0) step_index = 0; | |
135ee03a | 311 | else if (step_index > 88) step_index = 88; |
0147f198 | 312 | |
0147f198 FR |
313 | sign = nibble & 8; |
314 | delta = nibble & 7; | |
9937e686 MM |
315 | /* perform direct multiplication instead of series of jumps proposed by |
316 | * the reference ADPCM implementation since modern CPUs can do the mults | |
317 | * quickly enough */ | |
318 | diff = ((2 * delta + 1) * step) >> 3; | |
135ee03a | 319 | predictor = c->predictor; |
0147f198 FR |
320 | if (sign) predictor -= diff; |
321 | else predictor += diff; | |
322 | ||
323 | CLAMP_TO_SHORT(predictor); | |
324 | c->predictor = predictor; | |
325 | c->step_index = step_index; | |
135ee03a | 326 | |
0147f198 FR |
327 | return (short)predictor; |
328 | } | |
329 | ||
4b465299 MN |
330 | static inline short adpcm_4xa_expand_nibble(ADPCMChannelStatus *c, char nibble) |
331 | { | |
332 | int step_index; | |
333 | int predictor; | |
334 | int sign, delta, diff, step; | |
335 | ||
336 | step = step_table[c->step_index]; | |
337 | step_index = c->step_index + index_table[(unsigned)nibble]; | |
338 | if (step_index < 0) step_index = 0; | |
339 | else if (step_index > 88) step_index = 88; | |
340 | ||
341 | sign = nibble & 8; | |
342 | delta = nibble & 7; | |
343 | ||
344 | diff = (delta*step + (step>>1))>>3; // difference to code above | |
345 | ||
346 | predictor = c->predictor; | |
347 | if (sign) predictor -= diff; | |
348 | else predictor += diff; | |
349 | ||
350 | CLAMP_TO_SHORT(predictor); | |
351 | c->predictor = predictor; | |
352 | c->step_index = step_index; | |
353 | ||
354 | return (short)predictor; | |
355 | } | |
356 | ||
0147f198 FR |
357 | static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble) |
358 | { | |
359 | int predictor; | |
360 | ||
361 | predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256; | |
362 | predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; | |
363 | CLAMP_TO_SHORT(predictor); | |
364 | ||
365 | c->sample2 = c->sample1; | |
366 | c->sample1 = predictor; | |
367 | c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256; | |
368 | if (c->idelta < 16) c->idelta = 16; | |
369 | ||
370 | return (short)predictor; | |
371 | } | |
372 | ||
fc384777 MM |
373 | static void xa_decode(short *out, const unsigned char *in, |
374 | ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc) | |
375 | { | |
376 | int i, j; | |
377 | int shift,filter,f0,f1; | |
378 | int s_1,s_2; | |
379 | int d,s,t; | |
380 | ||
381 | for(i=0;i<4;i++) { | |
382 | ||
383 | shift = 12 - (in[4+i*2] & 15); | |
384 | filter = in[4+i*2] >> 4; | |
385 | f0 = xa_adpcm_table[filter][0]; | |
386 | f1 = xa_adpcm_table[filter][1]; | |
387 | ||
388 | s_1 = left->sample1; | |
389 | s_2 = left->sample2; | |
390 | ||
391 | for(j=0;j<28;j++) { | |
392 | d = in[16+i+j*4]; | |
393 | ||
394 | t = (signed char)(d<<4)>>4; | |
395 | s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6); | |
396 | CLAMP_TO_SHORT(s); | |
397 | *out = s; | |
398 | out += inc; | |
399 | s_2 = s_1; | |
400 | s_1 = s; | |
401 | } | |
402 | ||
403 | if (inc==2) { /* stereo */ | |
404 | left->sample1 = s_1; | |
405 | left->sample2 = s_2; | |
406 | s_1 = right->sample1; | |
407 | s_2 = right->sample2; | |
408 | out = out + 1 - 28*2; | |
409 | } | |
410 | ||
411 | shift = 12 - (in[5+i*2] & 15); | |
412 | filter = in[5+i*2] >> 4; | |
413 | ||
414 | f0 = xa_adpcm_table[filter][0]; | |
415 | f1 = xa_adpcm_table[filter][1]; | |
416 | ||
417 | for(j=0;j<28;j++) { | |
418 | d = in[16+i+j*4]; | |
419 | ||
420 | t = (signed char)d >> 4; | |
421 | s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6); | |
422 | CLAMP_TO_SHORT(s); | |
423 | *out = s; | |
424 | out += inc; | |
425 | s_2 = s_1; | |
426 | s_1 = s; | |
427 | } | |
428 | ||
429 | if (inc==2) { /* stereo */ | |
430 | right->sample1 = s_1; | |
431 | right->sample2 = s_2; | |
432 | out -= 1; | |
433 | } else { | |
434 | left->sample1 = s_1; | |
435 | left->sample2 = s_2; | |
436 | } | |
437 | } | |
438 | } | |
439 | ||
440 | ||
9937e686 MM |
441 | /* DK3 ADPCM support macro */ |
442 | #define DK3_GET_NEXT_NIBBLE() \ | |
443 | if (decode_top_nibble_next) \ | |
444 | { \ | |
445 | nibble = (last_byte >> 4) & 0x0F; \ | |
446 | decode_top_nibble_next = 0; \ | |
447 | } \ | |
448 | else \ | |
449 | { \ | |
450 | last_byte = *src++; \ | |
451 | if (src >= buf + buf_size) break; \ | |
452 | nibble = last_byte & 0x0F; \ | |
453 | decode_top_nibble_next = 1; \ | |
454 | } | |
455 | ||
0147f198 FR |
456 | static int adpcm_decode_frame(AVCodecContext *avctx, |
457 | void *data, int *data_size, | |
0c1a9eda | 458 | uint8_t *buf, int buf_size) |
0147f198 FR |
459 | { |
460 | ADPCMContext *c = avctx->priv_data; | |
461 | ADPCMChannelStatus *cs; | |
4b465299 | 462 | int n, m, channel, i; |
0147f198 FR |
463 | int block_predictor[2]; |
464 | short *samples; | |
0c1a9eda | 465 | uint8_t *src; |
0147f198 FR |
466 | int st; /* stereo */ |
467 | ||
9937e686 MM |
468 | /* DK3 ADPCM accounting variables */ |
469 | unsigned char last_byte = 0; | |
470 | unsigned char nibble; | |
471 | int decode_top_nibble_next = 0; | |
472 | int diff_channel; | |
473 | ||
0147f198 FR |
474 | samples = data; |
475 | src = buf; | |
476 | ||
0147f198 FR |
477 | st = avctx->channels == 2; |
478 | ||
479 | switch(avctx->codec->id) { | |
480 | case CODEC_ID_ADPCM_IMA_QT: | |
481 | n = (buf_size - 2);/* >> 2*avctx->channels;*/ | |
482 | channel = c->channel; | |
483 | cs = &(c->status[channel]); | |
484 | /* (pppppp) (piiiiiii) */ | |
485 | ||
486 | /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */ | |
487 | cs->predictor = (*src++) << 8; | |
488 | cs->predictor |= (*src & 0x80); | |
489 | cs->predictor &= 0xFF80; | |
490 | ||
491 | /* sign extension */ | |
492 | if(cs->predictor & 0x8000) | |
493 | cs->predictor -= 0x10000; | |
494 | ||
495 | CLAMP_TO_SHORT(cs->predictor); | |
496 | ||
497 | cs->step_index = (*src++) & 0x7F; | |
498 | ||
9b879566 | 499 | if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index); |
0147f198 FR |
500 | if (cs->step_index > 88) cs->step_index = 88; |
501 | ||
502 | cs->step = step_table[cs->step_index]; | |
503 | ||
504 | if (st && channel) | |
505 | samples++; | |
506 | ||
507 | for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */ | |
508 | *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F); | |
509 | samples += avctx->channels; | |
510 | *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F); | |
511 | samples += avctx->channels; | |
512 | src ++; | |
513 | } | |
514 | ||
515 | if(st) { /* handle stereo interlacing */ | |
516 | c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */ | |
ac3d5cac | 517 | if(channel == 1) { /* wait for the other packet before outputing anything */ |
0147f198 FR |
518 | *data_size = 0; |
519 | return src - buf; | |
520 | } | |
521 | } | |
522 | break; | |
523 | case CODEC_ID_ADPCM_IMA_WAV: | |
ca1d62f4 AY |
524 | if (avctx->block_align != 0 && buf_size > avctx->block_align) |
525 | buf_size = avctx->block_align; | |
526 | ||
135ee03a | 527 | // XXX: do as per-channel loop |
0147f198 FR |
528 | cs = &(c->status[0]); |
529 | cs->predictor = (*src++) & 0x0FF; | |
530 | cs->predictor |= ((*src++) << 8) & 0x0FF00; | |
531 | if(cs->predictor & 0x8000) | |
532 | cs->predictor -= 0x10000; | |
533 | CLAMP_TO_SHORT(cs->predictor); | |
534 | ||
135ee03a | 535 | // XXX: is this correct ??: *samples++ = cs->predictor; |
889c5224 | 536 | |
135ee03a | 537 | cs->step_index = *src++; |
0147f198 FR |
538 | if (cs->step_index < 0) cs->step_index = 0; |
539 | if (cs->step_index > 88) cs->step_index = 88; | |
9b879566 | 540 | if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */ |
889c5224 | 541 | |
0147f198 FR |
542 | if (st) { |
543 | cs = &(c->status[1]); | |
544 | cs->predictor = (*src++) & 0x0FF; | |
545 | cs->predictor |= ((*src++) << 8) & 0x0FF00; | |
546 | if(cs->predictor & 0x8000) | |
547 | cs->predictor -= 0x10000; | |
548 | CLAMP_TO_SHORT(cs->predictor); | |
549 | ||
135ee03a | 550 | // XXX: is this correct ??: *samples++ = cs->predictor; |
889c5224 | 551 | |
135ee03a | 552 | cs->step_index = *src++; |
0147f198 FR |
553 | if (cs->step_index < 0) cs->step_index = 0; |
554 | if (cs->step_index > 88) cs->step_index = 88; | |
135ee03a | 555 | src++; /* if != 0 -> out-of-sync */ |
0147f198 | 556 | } |
0147f198 | 557 | |
6b9b85c1 ZK |
558 | for(m=4; src < (buf + buf_size);) { |
559 | *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F); | |
0147f198 FR |
560 | if (st) |
561 | *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F); | |
562 | *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F); | |
6b9b85c1 | 563 | if (st) { |
0147f198 | 564 | *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F); |
6b9b85c1 ZK |
565 | if (!--m) { |
566 | m=4; | |
567 | src+=4; | |
568 | } | |
569 | } | |
570 | src++; | |
135ee03a | 571 | } |
0147f198 | 572 | break; |
4b465299 MN |
573 | case CODEC_ID_ADPCM_4XM: |
574 | cs = &(c->status[0]); | |
575 | c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2; | |
576 | if(st){ | |
577 | c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2; | |
578 | } | |
579 | c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2; | |
580 | if(st){ | |
581 | c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2; | |
582 | } | |
ac3d5cac MM |
583 | if (cs->step_index < 0) cs->step_index = 0; |
584 | if (cs->step_index > 88) cs->step_index = 88; | |
4b465299 MN |
585 | |
586 | m= (buf_size - (src - buf))>>st; | |
4b465299 MN |
587 | for(i=0; i<m; i++) { |
588 | *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] & 0x0F); | |
589 | if (st) | |
590 | *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F); | |
591 | *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4); | |
592 | if (st) | |
593 | *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4); | |
594 | } | |
595 | ||
596 | src += m<<st; | |
597 | ||
598 | break; | |
0147f198 | 599 | case CODEC_ID_ADPCM_MS: |
ca1d62f4 AY |
600 | if (avctx->block_align != 0 && buf_size > avctx->block_align) |
601 | buf_size = avctx->block_align; | |
0147f198 FR |
602 | n = buf_size - 7 * avctx->channels; |
603 | if (n < 0) | |
604 | return -1; | |
605 | block_predictor[0] = (*src++); /* should be bound */ | |
606 | block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0])); | |
607 | block_predictor[1] = 0; | |
608 | if (st) | |
609 | block_predictor[1] = (*src++); | |
610 | block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1])); | |
611 | c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
612 | if (c->status[0].idelta & 0x08000) | |
613 | c->status[0].idelta -= 0x10000; | |
614 | src+=2; | |
615 | if (st) | |
616 | c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
617 | if (st && c->status[1].idelta & 0x08000) | |
618 | c->status[1].idelta |= 0xFFFF0000; | |
619 | if (st) | |
620 | src+=2; | |
621 | c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]]; | |
622 | c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]]; | |
623 | c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]]; | |
624 | c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]]; | |
625 | ||
626 | c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
627 | src+=2; | |
628 | if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
629 | if (st) src+=2; | |
630 | c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
631 | src+=2; | |
632 | if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
633 | if (st) src+=2; | |
634 | ||
635 | *samples++ = c->status[0].sample1; | |
636 | if (st) *samples++ = c->status[1].sample1; | |
637 | *samples++ = c->status[0].sample2; | |
638 | if (st) *samples++ = c->status[1].sample2; | |
639 | for(;n>0;n--) { | |
640 | *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F); | |
641 | *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F); | |
642 | src ++; | |
643 | } | |
644 | break; | |
9937e686 | 645 | case CODEC_ID_ADPCM_IMA_DK4: |
5c69a4fd MN |
646 | if (avctx->block_align != 0 && buf_size > avctx->block_align) |
647 | buf_size = avctx->block_align; | |
648 | ||
9937e686 MM |
649 | c->status[0].predictor = (src[0] | (src[1] << 8)); |
650 | c->status[0].step_index = src[2]; | |
651 | src += 4; | |
652 | if(c->status[0].predictor & 0x8000) | |
653 | c->status[0].predictor -= 0x10000; | |
654 | *samples++ = c->status[0].predictor; | |
655 | if (st) { | |
656 | c->status[1].predictor = (src[0] | (src[1] << 8)); | |
657 | c->status[1].step_index = src[2]; | |
658 | src += 4; | |
659 | if(c->status[1].predictor & 0x8000) | |
660 | c->status[1].predictor -= 0x10000; | |
661 | *samples++ = c->status[1].predictor; | |
662 | } | |
663 | while (src < buf + buf_size) { | |
664 | ||
665 | /* take care of the top nibble (always left or mono channel) */ | |
666 | *samples++ = adpcm_ima_expand_nibble(&c->status[0], | |
667 | (src[0] >> 4) & 0x0F); | |
668 | ||
669 | /* take care of the bottom nibble, which is right sample for | |
670 | * stereo, or another mono sample */ | |
671 | if (st) | |
672 | *samples++ = adpcm_ima_expand_nibble(&c->status[1], | |
673 | src[0] & 0x0F); | |
674 | else | |
675 | *samples++ = adpcm_ima_expand_nibble(&c->status[0], | |
676 | src[0] & 0x0F); | |
677 | ||
678 | src++; | |
679 | } | |
680 | break; | |
681 | case CODEC_ID_ADPCM_IMA_DK3: | |
5c69a4fd MN |
682 | if (avctx->block_align != 0 && buf_size > avctx->block_align) |
683 | buf_size = avctx->block_align; | |
684 | ||
9937e686 MM |
685 | c->status[0].predictor = (src[10] | (src[11] << 8)); |
686 | c->status[1].predictor = (src[12] | (src[13] << 8)); | |
687 | c->status[0].step_index = src[14]; | |
688 | c->status[1].step_index = src[15]; | |
689 | /* sign extend the predictors */ | |
690 | if(c->status[0].predictor & 0x8000) | |
691 | c->status[0].predictor -= 0x10000; | |
692 | if(c->status[1].predictor & 0x8000) | |
693 | c->status[1].predictor -= 0x10000; | |
694 | src += 16; | |
695 | diff_channel = c->status[1].predictor; | |
696 | ||
697 | /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when | |
698 | * the buffer is consumed */ | |
699 | while (1) { | |
700 | ||
701 | /* for this algorithm, c->status[0] is the sum channel and | |
702 | * c->status[1] is the diff channel */ | |
703 | ||
704 | /* process the first predictor of the sum channel */ | |
705 | DK3_GET_NEXT_NIBBLE(); | |
706 | adpcm_ima_expand_nibble(&c->status[0], nibble); | |
707 | ||
708 | /* process the diff channel predictor */ | |
709 | DK3_GET_NEXT_NIBBLE(); | |
710 | adpcm_ima_expand_nibble(&c->status[1], nibble); | |
711 | ||
712 | /* process the first pair of stereo PCM samples */ | |
713 | diff_channel = (diff_channel + c->status[1].predictor) / 2; | |
714 | *samples++ = c->status[0].predictor + c->status[1].predictor; | |
715 | *samples++ = c->status[0].predictor - c->status[1].predictor; | |
716 | ||
717 | /* process the second predictor of the sum channel */ | |
718 | DK3_GET_NEXT_NIBBLE(); | |
719 | adpcm_ima_expand_nibble(&c->status[0], nibble); | |
720 | ||
721 | /* process the second pair of stereo PCM samples */ | |
722 | diff_channel = (diff_channel + c->status[1].predictor) / 2; | |
723 | *samples++ = c->status[0].predictor + c->status[1].predictor; | |
724 | *samples++ = c->status[0].predictor - c->status[1].predictor; | |
725 | } | |
726 | break; | |
2fdf638b MM |
727 | case CODEC_ID_ADPCM_IMA_WS: |
728 | /* no per-block initialization; just start decoding the data */ | |
729 | while (src < buf + buf_size) { | |
730 | ||
731 | if (st) { | |
732 | *samples++ = adpcm_ima_expand_nibble(&c->status[0], | |
733 | (src[0] >> 4) & 0x0F); | |
734 | *samples++ = adpcm_ima_expand_nibble(&c->status[1], | |
735 | src[0] & 0x0F); | |
736 | } else { | |
737 | *samples++ = adpcm_ima_expand_nibble(&c->status[0], | |
738 | (src[0] >> 4) & 0x0F); | |
739 | *samples++ = adpcm_ima_expand_nibble(&c->status[0], | |
740 | src[0] & 0x0F); | |
741 | } | |
742 | ||
743 | src++; | |
744 | } | |
745 | break; | |
fc384777 MM |
746 | case CODEC_ID_ADPCM_XA: |
747 | c->status[0].sample1 = c->status[0].sample2 = | |
748 | c->status[1].sample1 = c->status[1].sample2 = 0; | |
749 | while (buf_size >= 128) { | |
750 | xa_decode(samples, src, &c->status[0], &c->status[1], | |
751 | avctx->channels); | |
752 | src += 128; | |
753 | samples += 28 * 8; | |
754 | buf_size -= 128; | |
755 | } | |
756 | break; | |
0147f198 FR |
757 | default: |
758 | *data_size = 0; | |
759 | return -1; | |
760 | } | |
0c1a9eda | 761 | *data_size = (uint8_t *)samples - (uint8_t *)data; |
0147f198 FR |
762 | return src - buf; |
763 | } | |
764 | ||
764ef400 MM |
765 | |
766 | ||
767 | #ifdef CONFIG_ENCODERS | |
768 | #define ADPCM_ENCODER(id,name) \ | |
0147f198 FR |
769 | AVCodec name ## _encoder = { \ |
770 | #name, \ | |
771 | CODEC_TYPE_AUDIO, \ | |
772 | id, \ | |
773 | sizeof(ADPCMContext), \ | |
774 | adpcm_encode_init, \ | |
775 | adpcm_encode_frame, \ | |
776 | adpcm_encode_close, \ | |
777 | NULL, \ | |
764ef400 MM |
778 | }; |
779 | #else | |
780 | #define ADPCM_ENCODER(id,name) | |
781 | #endif | |
782 | ||
783 | #ifdef CONFIG_DECODERS | |
784 | #define ADPCM_DECODER(id,name) \ | |
0147f198 FR |
785 | AVCodec name ## _decoder = { \ |
786 | #name, \ | |
787 | CODEC_TYPE_AUDIO, \ | |
788 | id, \ | |
789 | sizeof(ADPCMContext), \ | |
790 | adpcm_decode_init, \ | |
791 | NULL, \ | |
792 | NULL, \ | |
793 | adpcm_decode_frame, \ | |
794 | }; | |
764ef400 MM |
795 | #else |
796 | #define ADPCM_DECODER(id,name) | |
797 | #endif | |
798 | ||
799 | #define ADPCM_CODEC(id, name) \ | |
800 | ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name) | |
0147f198 FR |
801 | |
802 | ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt); | |
803 | ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav); | |
9937e686 MM |
804 | ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3); |
805 | ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4); | |
2fdf638b | 806 | ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws); |
0147f198 | 807 | ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms); |
4b465299 | 808 | ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm); |
fc384777 MM |
809 | ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa); |
810 | ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx); | |
0147f198 FR |
811 | |
812 | #undef ADPCM_CODEC |