Commit | Line | Data |
---|---|---|
0147f198 FR |
1 | /* |
2 | * ADPCM codecs | |
3 | * Copyright (c) 2001 Fabrice Bellard. | |
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. | |
0147f198 FR |
24 | * First version by Francois Revol revol@free.fr |
25 | * | |
26 | * Features and limitations: | |
27 | * | |
28 | * Reference documents: | |
29 | * http://www.pcisys.net/~melanson/codecs/adpcm.txt | |
30 | * http://www.geocities.com/SiliconValley/8682/aud3.txt | |
31 | * http://openquicktime.sourceforge.net/plugins.htm | |
32 | * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html | |
889c5224 FR |
33 | * http://www.cs.ucla.edu/~leec/mediabench/applications.html |
34 | * SoX source code http://home.sprynet.com/~cbagwell/sox.html | |
0147f198 FR |
35 | */ |
36 | ||
37 | #define BLKSIZE 1024 | |
38 | ||
39 | #define CLAMP_TO_SHORT(value) \ | |
40 | if (value > 32767) \ | |
41 | value = 32767; \ | |
42 | else if (value < -32768) \ | |
43 | value = -32768; \ | |
44 | ||
45 | /* step_table[] and index_table[] are from the ADPCM reference source */ | |
46 | /* This is the index table: */ | |
135ee03a | 47 | static const int index_table[16] = { |
0147f198 FR |
48 | -1, -1, -1, -1, 2, 4, 6, 8, |
49 | -1, -1, -1, -1, 2, 4, 6, 8, | |
50 | }; | |
51 | ||
983e3246 MN |
52 | /** |
53 | * This is the step table. Note that many programs use slight deviations from | |
0147f198 FR |
54 | * this table, but such deviations are negligible: |
55 | */ | |
135ee03a | 56 | static const int step_table[89] = { |
0147f198 FR |
57 | 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, |
58 | 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, | |
59 | 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, | |
60 | 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, | |
61 | 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, | |
62 | 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, | |
63 | 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, | |
64 | 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | |
65 | 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 | |
66 | }; | |
67 | ||
889c5224 | 68 | /* Those are for MS-ADPCM */ |
0147f198 | 69 | /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */ |
135ee03a | 70 | static const int AdaptationTable[] = { |
0147f198 FR |
71 | 230, 230, 230, 230, 307, 409, 512, 614, |
72 | 768, 614, 512, 409, 307, 230, 230, 230 | |
73 | }; | |
74 | ||
135ee03a | 75 | static const int AdaptCoeff1[] = { |
0147f198 FR |
76 | 256, 512, 0, 192, 240, 460, 392 |
77 | }; | |
78 | ||
135ee03a | 79 | static const int AdaptCoeff2[] = { |
0147f198 FR |
80 | 0, -256, 0, 64, 0, -208, -232 |
81 | }; | |
82 | ||
83 | /* end of tables */ | |
84 | ||
85 | typedef struct ADPCMChannelStatus { | |
86 | int predictor; | |
87 | short int step_index; | |
88 | int step; | |
889c5224 FR |
89 | /* for encoding */ |
90 | int prev_sample; | |
0147f198 FR |
91 | |
92 | /* MS version */ | |
93 | short sample1; | |
94 | short sample2; | |
95 | int coeff1; | |
96 | int coeff2; | |
97 | int idelta; | |
98 | } ADPCMChannelStatus; | |
99 | ||
100 | typedef struct ADPCMContext { | |
101 | int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */ | |
102 | ADPCMChannelStatus status[2]; | |
103 | short sample_buffer[32]; /* hold left samples while waiting for right samples */ | |
104 | } ADPCMContext; | |
105 | ||
106 | /* XXX: implement encoding */ | |
107 | ||
108 | static int adpcm_encode_init(AVCodecContext *avctx) | |
109 | { | |
889c5224 FR |
110 | if (avctx->channels > 2) |
111 | return -1; /* only stereo or mono =) */ | |
0147f198 FR |
112 | switch(avctx->codec->id) { |
113 | case CODEC_ID_ADPCM_IMA_QT: | |
889c5224 FR |
114 | fprintf(stderr, "ADPCM: codec admcp_ima_qt unsupported for encoding !\n"); |
115 | avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */ | |
116 | return -1; | |
0147f198 FR |
117 | break; |
118 | case CODEC_ID_ADPCM_IMA_WAV: | |
889c5224 FR |
119 | avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */ |
120 | /* and we have 4 bytes per channel overhead */ | |
121 | avctx->block_align = BLKSIZE; | |
122 | /* seems frame_size isn't taken into account... have to buffer the samples :-( */ | |
123 | break; | |
124 | case CODEC_ID_ADPCM_MS: | |
125 | fprintf(stderr, "ADPCM: codec admcp_ms unsupported for encoding !\n"); | |
126 | return -1; | |
0147f198 FR |
127 | break; |
128 | default: | |
889c5224 | 129 | return -1; |
0147f198 FR |
130 | break; |
131 | } | |
492cd3a9 MN |
132 | |
133 | avctx->coded_frame= avcodec_alloc_frame(); | |
134 | avctx->coded_frame->key_frame= 1; | |
135 | ||
0147f198 FR |
136 | return 0; |
137 | } | |
138 | ||
139 | static int adpcm_encode_close(AVCodecContext *avctx) | |
140 | { | |
492cd3a9 MN |
141 | av_freep(&avctx->coded_frame); |
142 | ||
0147f198 FR |
143 | return 0; |
144 | } | |
145 | ||
889c5224 FR |
146 | |
147 | static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample) | |
148 | { | |
149 | int step_index; | |
150 | unsigned char nibble; | |
151 | ||
152 | int sign = 0; /* sign bit of the nibble (MSB) */ | |
153 | int delta, predicted_delta; | |
154 | ||
155 | delta = sample - c->prev_sample; | |
156 | ||
157 | if (delta < 0) { | |
158 | sign = 1; | |
159 | delta = -delta; | |
160 | } | |
161 | ||
162 | step_index = c->step_index; | |
163 | ||
164 | /* nibble = 4 * delta / step_table[step_index]; */ | |
165 | nibble = (delta << 2) / step_table[step_index]; | |
166 | ||
167 | if (nibble > 7) | |
168 | nibble = 7; | |
169 | ||
170 | step_index += index_table[nibble]; | |
171 | if (step_index < 0) | |
172 | step_index = 0; | |
173 | if (step_index > 88) | |
174 | step_index = 88; | |
175 | ||
176 | /* what the decoder will find */ | |
177 | predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8); | |
178 | ||
179 | if (sign) | |
180 | c->prev_sample -= predicted_delta; | |
181 | else | |
182 | c->prev_sample += predicted_delta; | |
183 | ||
184 | CLAMP_TO_SHORT(c->prev_sample); | |
185 | ||
186 | ||
187 | nibble += sign << 3; /* sign * 8 */ | |
188 | ||
189 | /* save back */ | |
190 | c->step_index = step_index; | |
191 | ||
192 | return nibble; | |
193 | } | |
194 | ||
0147f198 FR |
195 | static int adpcm_encode_frame(AVCodecContext *avctx, |
196 | unsigned char *frame, int buf_size, void *data) | |
197 | { | |
889c5224 | 198 | int n; |
0147f198 FR |
199 | short *samples; |
200 | unsigned char *dst; | |
889c5224 FR |
201 | ADPCMContext *c = avctx->priv_data; |
202 | ||
203 | dst = frame; | |
204 | samples = (short *)data; | |
205 | /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */ | |
0147f198 FR |
206 | |
207 | switch(avctx->codec->id) { | |
889c5224 FR |
208 | case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */ |
209 | break; | |
210 | case CODEC_ID_ADPCM_IMA_WAV: | |
211 | n = avctx->frame_size / 8; | |
212 | c->status[0].prev_sample = (signed short)samples[0]; /* XXX */ | |
213 | /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */ | |
214 | *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */ | |
215 | *dst++ = (c->status[0].prev_sample >> 8) & 0xFF; | |
216 | *dst++ = (unsigned char)c->status[0].step_index; | |
217 | *dst++ = 0; /* unknown */ | |
218 | samples++; | |
219 | if (avctx->channels == 2) { | |
220 | c->status[1].prev_sample = (signed short)samples[0]; | |
221 | /* c->status[1].step_index = 0; */ | |
222 | *dst++ = (c->status[1].prev_sample) & 0xFF; | |
223 | *dst++ = (c->status[1].prev_sample >> 8) & 0xFF; | |
224 | *dst++ = (unsigned char)c->status[1].step_index; | |
225 | *dst++ = 0; | |
226 | samples++; | |
227 | } | |
228 | ||
229 | /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */ | |
230 | for (; n>0; n--) { | |
231 | *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F; | |
232 | *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0; | |
233 | dst++; | |
234 | *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F; | |
235 | *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0; | |
236 | dst++; | |
237 | *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F; | |
238 | *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0; | |
239 | dst++; | |
240 | *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F; | |
241 | *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0; | |
242 | dst++; | |
243 | /* right channel */ | |
244 | if (avctx->channels == 2) { | |
245 | *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]); | |
246 | *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4; | |
247 | dst++; | |
248 | *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]); | |
249 | *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4; | |
250 | dst++; | |
251 | *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]); | |
252 | *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4; | |
253 | dst++; | |
254 | *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]); | |
255 | *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4; | |
256 | dst++; | |
257 | } | |
258 | samples += 8 * avctx->channels; | |
259 | } | |
260 | break; | |
0147f198 FR |
261 | default: |
262 | return -1; | |
263 | } | |
0147f198 FR |
264 | return dst - frame; |
265 | } | |
266 | ||
267 | static int adpcm_decode_init(AVCodecContext * avctx) | |
268 | { | |
269 | ADPCMContext *c = avctx->priv_data; | |
270 | ||
271 | c->channel = 0; | |
272 | c->status[0].predictor = c->status[1].predictor = 0; | |
273 | c->status[0].step_index = c->status[1].step_index = 0; | |
274 | c->status[0].step = c->status[1].step = 0; | |
275 | ||
276 | switch(avctx->codec->id) { | |
277 | default: | |
278 | break; | |
279 | } | |
280 | return 0; | |
281 | } | |
282 | ||
283 | static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble) | |
284 | { | |
285 | int step_index; | |
286 | int predictor; | |
287 | int sign, delta, diff, step; | |
288 | ||
135ee03a | 289 | step = step_table[c->step_index]; |
0147f198 FR |
290 | step_index = c->step_index + index_table[(unsigned)nibble]; |
291 | if (step_index < 0) step_index = 0; | |
135ee03a | 292 | else if (step_index > 88) step_index = 88; |
0147f198 | 293 | |
0147f198 FR |
294 | sign = nibble & 8; |
295 | delta = nibble & 7; | |
135ee03a | 296 | #if 0 |
0147f198 FR |
297 | diff = step >> 3; |
298 | if (delta & 4) diff += step; | |
299 | if (delta & 2) diff += step >> 1; | |
300 | if (delta & 1) diff += step >> 2; | |
135ee03a ZK |
301 | #else |
302 | diff = ((2 * delta + 1) * step) >> 3; // no jumps | |
303 | #endif | |
304 | predictor = c->predictor; | |
0147f198 FR |
305 | if (sign) predictor -= diff; |
306 | else predictor += diff; | |
307 | ||
308 | CLAMP_TO_SHORT(predictor); | |
309 | c->predictor = predictor; | |
310 | c->step_index = step_index; | |
135ee03a | 311 | |
0147f198 FR |
312 | return (short)predictor; |
313 | } | |
314 | ||
4b465299 MN |
315 | static inline short adpcm_4xa_expand_nibble(ADPCMChannelStatus *c, char nibble) |
316 | { | |
317 | int step_index; | |
318 | int predictor; | |
319 | int sign, delta, diff, step; | |
320 | ||
321 | step = step_table[c->step_index]; | |
322 | step_index = c->step_index + index_table[(unsigned)nibble]; | |
323 | if (step_index < 0) step_index = 0; | |
324 | else if (step_index > 88) step_index = 88; | |
325 | ||
326 | sign = nibble & 8; | |
327 | delta = nibble & 7; | |
328 | ||
329 | diff = (delta*step + (step>>1))>>3; // difference to code above | |
330 | ||
331 | predictor = c->predictor; | |
332 | if (sign) predictor -= diff; | |
333 | else predictor += diff; | |
334 | ||
335 | CLAMP_TO_SHORT(predictor); | |
336 | c->predictor = predictor; | |
337 | c->step_index = step_index; | |
338 | ||
339 | return (short)predictor; | |
340 | } | |
341 | ||
0147f198 FR |
342 | static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble) |
343 | { | |
344 | int predictor; | |
345 | ||
346 | predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256; | |
347 | predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; | |
348 | CLAMP_TO_SHORT(predictor); | |
349 | ||
350 | c->sample2 = c->sample1; | |
351 | c->sample1 = predictor; | |
352 | c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256; | |
353 | if (c->idelta < 16) c->idelta = 16; | |
354 | ||
355 | return (short)predictor; | |
356 | } | |
357 | ||
358 | static int adpcm_decode_frame(AVCodecContext *avctx, | |
359 | void *data, int *data_size, | |
0c1a9eda | 360 | uint8_t *buf, int buf_size) |
0147f198 FR |
361 | { |
362 | ADPCMContext *c = avctx->priv_data; | |
363 | ADPCMChannelStatus *cs; | |
4b465299 | 364 | int n, m, channel, i; |
0147f198 FR |
365 | int block_predictor[2]; |
366 | short *samples; | |
0c1a9eda | 367 | uint8_t *src; |
0147f198 FR |
368 | int st; /* stereo */ |
369 | ||
370 | samples = data; | |
371 | src = buf; | |
372 | ||
0147f198 FR |
373 | st = avctx->channels == 2; |
374 | ||
375 | switch(avctx->codec->id) { | |
376 | case CODEC_ID_ADPCM_IMA_QT: | |
377 | n = (buf_size - 2);/* >> 2*avctx->channels;*/ | |
378 | channel = c->channel; | |
379 | cs = &(c->status[channel]); | |
380 | /* (pppppp) (piiiiiii) */ | |
381 | ||
382 | /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */ | |
383 | cs->predictor = (*src++) << 8; | |
384 | cs->predictor |= (*src & 0x80); | |
385 | cs->predictor &= 0xFF80; | |
386 | ||
387 | /* sign extension */ | |
388 | if(cs->predictor & 0x8000) | |
389 | cs->predictor -= 0x10000; | |
390 | ||
391 | CLAMP_TO_SHORT(cs->predictor); | |
392 | ||
393 | cs->step_index = (*src++) & 0x7F; | |
394 | ||
889c5224 | 395 | if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index); |
0147f198 FR |
396 | if (cs->step_index > 88) cs->step_index = 88; |
397 | ||
398 | cs->step = step_table[cs->step_index]; | |
399 | ||
400 | if (st && channel) | |
401 | samples++; | |
402 | ||
889c5224 FR |
403 | *samples++ = cs->predictor; |
404 | samples += st; | |
405 | ||
0147f198 FR |
406 | for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */ |
407 | *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F); | |
408 | samples += avctx->channels; | |
409 | *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F); | |
410 | samples += avctx->channels; | |
411 | src ++; | |
412 | } | |
413 | ||
414 | if(st) { /* handle stereo interlacing */ | |
415 | c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */ | |
416 | if(channel == 0) { /* wait for the other packet before outputing anything */ | |
417 | *data_size = 0; | |
418 | return src - buf; | |
419 | } | |
420 | } | |
421 | break; | |
422 | case CODEC_ID_ADPCM_IMA_WAV: | |
423 | if (buf_size > BLKSIZE) { | |
424 | if (avctx->block_align != 0) | |
425 | buf_size = avctx->block_align; | |
426 | else | |
427 | buf_size = BLKSIZE; | |
428 | } | |
135ee03a | 429 | // XXX: do as per-channel loop |
0147f198 FR |
430 | cs = &(c->status[0]); |
431 | cs->predictor = (*src++) & 0x0FF; | |
432 | cs->predictor |= ((*src++) << 8) & 0x0FF00; | |
433 | if(cs->predictor & 0x8000) | |
434 | cs->predictor -= 0x10000; | |
435 | CLAMP_TO_SHORT(cs->predictor); | |
436 | ||
135ee03a | 437 | // XXX: is this correct ??: *samples++ = cs->predictor; |
889c5224 | 438 | |
135ee03a | 439 | cs->step_index = *src++; |
0147f198 FR |
440 | if (cs->step_index < 0) cs->step_index = 0; |
441 | if (cs->step_index > 88) cs->step_index = 88; | |
889c5224 FR |
442 | if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */ |
443 | ||
0147f198 FR |
444 | if (st) { |
445 | cs = &(c->status[1]); | |
446 | cs->predictor = (*src++) & 0x0FF; | |
447 | cs->predictor |= ((*src++) << 8) & 0x0FF00; | |
448 | if(cs->predictor & 0x8000) | |
449 | cs->predictor -= 0x10000; | |
450 | CLAMP_TO_SHORT(cs->predictor); | |
451 | ||
135ee03a | 452 | // XXX: is this correct ??: *samples++ = cs->predictor; |
889c5224 | 453 | |
135ee03a | 454 | cs->step_index = *src++; |
0147f198 FR |
455 | if (cs->step_index < 0) cs->step_index = 0; |
456 | if (cs->step_index > 88) cs->step_index = 88; | |
135ee03a | 457 | src++; /* if != 0 -> out-of-sync */ |
0147f198 | 458 | } |
0147f198 | 459 | |
6b9b85c1 ZK |
460 | for(m=4; src < (buf + buf_size);) { |
461 | *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F); | |
0147f198 FR |
462 | if (st) |
463 | *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F); | |
464 | *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F); | |
6b9b85c1 | 465 | if (st) { |
0147f198 | 466 | *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F); |
6b9b85c1 ZK |
467 | if (!--m) { |
468 | m=4; | |
469 | src+=4; | |
470 | } | |
471 | } | |
472 | src++; | |
135ee03a | 473 | } |
0147f198 | 474 | break; |
4b465299 MN |
475 | case CODEC_ID_ADPCM_4XM: |
476 | cs = &(c->status[0]); | |
477 | c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2; | |
478 | if(st){ | |
479 | c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2; | |
480 | } | |
481 | c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2; | |
482 | if(st){ | |
483 | c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2; | |
484 | } | |
485 | // if (cs->step_index < 0) cs->step_index = 0; | |
486 | // if (cs->step_index > 88) cs->step_index = 88; | |
487 | ||
488 | m= (buf_size - (src - buf))>>st; | |
489 | //printf("%d %d %d %d\n", st, m, c->status[0].predictor, c->status[0].step_index); | |
490 | //FIXME / XXX decode chanels individual & interleave samples | |
491 | for(i=0; i<m; i++) { | |
492 | *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] & 0x0F); | |
493 | if (st) | |
494 | *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F); | |
495 | *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4); | |
496 | if (st) | |
497 | *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4); | |
498 | } | |
499 | ||
500 | src += m<<st; | |
501 | ||
502 | break; | |
0147f198 FR |
503 | case CODEC_ID_ADPCM_MS: |
504 | ||
505 | if (buf_size > BLKSIZE) { | |
506 | if (avctx->block_align != 0) | |
507 | buf_size = avctx->block_align; | |
508 | else | |
509 | buf_size = BLKSIZE; | |
510 | } | |
511 | n = buf_size - 7 * avctx->channels; | |
512 | if (n < 0) | |
513 | return -1; | |
514 | block_predictor[0] = (*src++); /* should be bound */ | |
515 | block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0])); | |
516 | block_predictor[1] = 0; | |
517 | if (st) | |
518 | block_predictor[1] = (*src++); | |
519 | block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1])); | |
520 | c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
521 | if (c->status[0].idelta & 0x08000) | |
522 | c->status[0].idelta -= 0x10000; | |
523 | src+=2; | |
524 | if (st) | |
525 | c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
526 | if (st && c->status[1].idelta & 0x08000) | |
527 | c->status[1].idelta |= 0xFFFF0000; | |
528 | if (st) | |
529 | src+=2; | |
530 | c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]]; | |
531 | c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]]; | |
532 | c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]]; | |
533 | c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]]; | |
534 | ||
535 | c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
536 | src+=2; | |
537 | if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
538 | if (st) src+=2; | |
539 | c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
540 | src+=2; | |
541 | if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
542 | if (st) src+=2; | |
543 | ||
544 | *samples++ = c->status[0].sample1; | |
545 | if (st) *samples++ = c->status[1].sample1; | |
546 | *samples++ = c->status[0].sample2; | |
547 | if (st) *samples++ = c->status[1].sample2; | |
548 | for(;n>0;n--) { | |
549 | *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F); | |
550 | *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F); | |
551 | src ++; | |
552 | } | |
553 | break; | |
554 | default: | |
555 | *data_size = 0; | |
556 | return -1; | |
557 | } | |
0c1a9eda | 558 | *data_size = (uint8_t *)samples - (uint8_t *)data; |
0147f198 FR |
559 | return src - buf; |
560 | } | |
561 | ||
562 | #define ADPCM_CODEC(id, name) \ | |
563 | AVCodec name ## _encoder = { \ | |
564 | #name, \ | |
565 | CODEC_TYPE_AUDIO, \ | |
566 | id, \ | |
567 | sizeof(ADPCMContext), \ | |
568 | adpcm_encode_init, \ | |
569 | adpcm_encode_frame, \ | |
570 | adpcm_encode_close, \ | |
571 | NULL, \ | |
572 | }; \ | |
573 | AVCodec name ## _decoder = { \ | |
574 | #name, \ | |
575 | CODEC_TYPE_AUDIO, \ | |
576 | id, \ | |
577 | sizeof(ADPCMContext), \ | |
578 | adpcm_decode_init, \ | |
579 | NULL, \ | |
580 | NULL, \ | |
581 | adpcm_decode_frame, \ | |
582 | }; | |
583 | ||
584 | ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt); | |
585 | ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav); | |
586 | ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms); | |
4b465299 | 587 | ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm); |
0147f198 FR |
588 | |
589 | #undef ADPCM_CODEC | |
889c5224 | 590 |