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