3 * Copyright (c) 2001-2003 The ffmpeg Project
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.
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.
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
24 * First version by Francois Revol revol@free.fr
25 * Fringe ADPCM codecs (e.g., DK3 and DK4)
26 * by Mike Melanson (melanson@pcisys.net)
28 * Features and limitations:
30 * Reference documents:
31 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
32 * http://www.geocities.com/SiliconValley/8682/aud3.txt
33 * http://openquicktime.sourceforge.net/plugins.htm
34 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
35 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
36 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
41 #define CLAMP_TO_SHORT(value) \
44 else if (value < -32768) \
47 /* step_table[] and index_table[] are from the ADPCM reference source */
48 /* This is the index table: */
49 static const int index_table[16] = {
50 -1, -1, -1, -1, 2, 4, 6, 8,
51 -1, -1, -1, -1, 2, 4, 6, 8,
55 * This is the step table. Note that many programs use slight deviations from
56 * this table, but such deviations are negligible:
58 static const int step_table[89] = {
59 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
60 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
61 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
62 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
63 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
64 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
65 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
66 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
67 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
70 /* Those are for MS-ADPCM */
71 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
72 static const int AdaptationTable[] = {
73 230, 230, 230, 230, 307, 409, 512, 614,
74 768, 614, 512, 409, 307, 230, 230, 230
77 static const int AdaptCoeff1[] = {
78 256, 512, 0, 192, 240, 460, 392
81 static const int AdaptCoeff2[] = {
82 0, -256, 0, 64, 0, -208, -232
87 typedef struct ADPCMChannelStatus {
100 } ADPCMChannelStatus;
102 typedef struct ADPCMContext {
103 int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
104 ADPCMChannelStatus status[2];
105 short sample_buffer[32]; /* hold left samples while waiting for right samples */
108 /* XXX: implement encoding */
110 static int adpcm_encode_init(AVCodecContext *avctx)
112 if (avctx->channels > 2)
113 return -1; /* only stereo or mono =) */
114 switch(avctx->codec->id) {
115 case CODEC_ID_ADPCM_IMA_QT:
116 fprintf(stderr, "ADPCM: codec admcp_ima_qt unsupported for encoding !\n");
117 avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
120 case CODEC_ID_ADPCM_IMA_WAV:
121 avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
122 /* and we have 4 bytes per channel overhead */
123 avctx->block_align = BLKSIZE;
124 /* seems frame_size isn't taken into account... have to buffer the samples :-( */
126 case CODEC_ID_ADPCM_MS:
127 fprintf(stderr, "ADPCM: codec admcp_ms unsupported for encoding !\n");
135 avctx->coded_frame= avcodec_alloc_frame();
136 avctx->coded_frame->key_frame= 1;
141 static int adpcm_encode_close(AVCodecContext *avctx)
143 av_freep(&avctx->coded_frame);
149 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
152 unsigned char nibble;
154 int sign = 0; /* sign bit of the nibble (MSB) */
155 int delta, predicted_delta;
157 delta = sample - c->prev_sample;
164 step_index = c->step_index;
166 /* nibble = 4 * delta / step_table[step_index]; */
167 nibble = (delta << 2) / step_table[step_index];
172 step_index += index_table[nibble];
178 /* what the decoder will find */
179 predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
182 c->prev_sample -= predicted_delta;
184 c->prev_sample += predicted_delta;
186 CLAMP_TO_SHORT(c->prev_sample);
189 nibble += sign << 3; /* sign * 8 */
192 c->step_index = step_index;
197 static int adpcm_encode_frame(AVCodecContext *avctx,
198 unsigned char *frame, int buf_size, void *data)
203 ADPCMContext *c = avctx->priv_data;
206 samples = (short *)data;
207 /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
209 switch(avctx->codec->id) {
210 case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
212 case CODEC_ID_ADPCM_IMA_WAV:
213 n = avctx->frame_size / 8;
214 c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
215 /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
216 *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
217 *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
218 *dst++ = (unsigned char)c->status[0].step_index;
219 *dst++ = 0; /* unknown */
221 if (avctx->channels == 2) {
222 c->status[1].prev_sample = (signed short)samples[0];
223 /* c->status[1].step_index = 0; */
224 *dst++ = (c->status[1].prev_sample) & 0xFF;
225 *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
226 *dst++ = (unsigned char)c->status[1].step_index;
231 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
233 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
234 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
236 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
237 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
239 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
240 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
242 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
243 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
246 if (avctx->channels == 2) {
247 *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
248 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
250 *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
251 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
253 *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
254 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
256 *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
257 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
260 samples += 8 * avctx->channels;
269 static int adpcm_decode_init(AVCodecContext * avctx)
271 ADPCMContext *c = avctx->priv_data;
274 c->status[0].predictor = c->status[1].predictor = 0;
275 c->status[0].step_index = c->status[1].step_index = 0;
276 c->status[0].step = c->status[1].step = 0;
278 switch(avctx->codec->id) {
285 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
289 int sign, delta, diff, step;
291 step = step_table[c->step_index];
292 step_index = c->step_index + index_table[(unsigned)nibble];
293 if (step_index < 0) step_index = 0;
294 else if (step_index > 88) step_index = 88;
298 /* perform direct multiplication instead of series of jumps proposed by
299 * the reference ADPCM implementation since modern CPUs can do the mults
301 diff = ((2 * delta + 1) * step) >> 3;
302 predictor = c->predictor;
303 if (sign) predictor -= diff;
304 else predictor += diff;
306 CLAMP_TO_SHORT(predictor);
307 c->predictor = predictor;
308 c->step_index = step_index;
310 return (short)predictor;
313 static inline short adpcm_4xa_expand_nibble(ADPCMChannelStatus *c, char nibble)
317 int sign, delta, diff, step;
319 step = step_table[c->step_index];
320 step_index = c->step_index + index_table[(unsigned)nibble];
321 if (step_index < 0) step_index = 0;
322 else if (step_index > 88) step_index = 88;
327 diff = (delta*step + (step>>1))>>3; // difference to code above
329 predictor = c->predictor;
330 if (sign) predictor -= diff;
331 else predictor += diff;
333 CLAMP_TO_SHORT(predictor);
334 c->predictor = predictor;
335 c->step_index = step_index;
337 return (short)predictor;
340 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
344 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
345 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
346 CLAMP_TO_SHORT(predictor);
348 c->sample2 = c->sample1;
349 c->sample1 = predictor;
350 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
351 if (c->idelta < 16) c->idelta = 16;
353 return (short)predictor;
356 /* DK3 ADPCM support macro */
357 #define DK3_GET_NEXT_NIBBLE() \
358 if (decode_top_nibble_next) \
360 nibble = (last_byte >> 4) & 0x0F; \
361 decode_top_nibble_next = 0; \
365 last_byte = *src++; \
366 if (src >= buf + buf_size) break; \
367 nibble = last_byte & 0x0F; \
368 decode_top_nibble_next = 1; \
371 static int adpcm_decode_frame(AVCodecContext *avctx,
372 void *data, int *data_size,
373 uint8_t *buf, int buf_size)
375 ADPCMContext *c = avctx->priv_data;
376 ADPCMChannelStatus *cs;
377 int n, m, channel, i;
378 int block_predictor[2];
383 /* DK3 ADPCM accounting variables */
384 unsigned char last_byte = 0;
385 unsigned char nibble;
386 int decode_top_nibble_next = 0;
392 st = avctx->channels == 2;
394 switch(avctx->codec->id) {
395 case CODEC_ID_ADPCM_IMA_QT:
396 n = (buf_size - 2);/* >> 2*avctx->channels;*/
397 channel = c->channel;
398 cs = &(c->status[channel]);
399 /* (pppppp) (piiiiiii) */
401 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
402 cs->predictor = (*src++) << 8;
403 cs->predictor |= (*src & 0x80);
404 cs->predictor &= 0xFF80;
407 if(cs->predictor & 0x8000)
408 cs->predictor -= 0x10000;
410 CLAMP_TO_SHORT(cs->predictor);
412 cs->step_index = (*src++) & 0x7F;
414 if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
415 if (cs->step_index > 88) cs->step_index = 88;
417 cs->step = step_table[cs->step_index];
422 *samples++ = cs->predictor;
425 for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
426 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
427 samples += avctx->channels;
428 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
429 samples += avctx->channels;
433 if(st) { /* handle stereo interlacing */
434 c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
435 if(channel == 0) { /* wait for the other packet before outputing anything */
441 case CODEC_ID_ADPCM_IMA_WAV:
442 if (buf_size > BLKSIZE) {
443 if (avctx->block_align != 0)
444 buf_size = avctx->block_align;
448 // XXX: do as per-channel loop
449 cs = &(c->status[0]);
450 cs->predictor = (*src++) & 0x0FF;
451 cs->predictor |= ((*src++) << 8) & 0x0FF00;
452 if(cs->predictor & 0x8000)
453 cs->predictor -= 0x10000;
454 CLAMP_TO_SHORT(cs->predictor);
456 // XXX: is this correct ??: *samples++ = cs->predictor;
458 cs->step_index = *src++;
459 if (cs->step_index < 0) cs->step_index = 0;
460 if (cs->step_index > 88) cs->step_index = 88;
461 if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
464 cs = &(c->status[1]);
465 cs->predictor = (*src++) & 0x0FF;
466 cs->predictor |= ((*src++) << 8) & 0x0FF00;
467 if(cs->predictor & 0x8000)
468 cs->predictor -= 0x10000;
469 CLAMP_TO_SHORT(cs->predictor);
471 // XXX: is this correct ??: *samples++ = cs->predictor;
473 cs->step_index = *src++;
474 if (cs->step_index < 0) cs->step_index = 0;
475 if (cs->step_index > 88) cs->step_index = 88;
476 src++; /* if != 0 -> out-of-sync */
479 for(m=4; src < (buf + buf_size);) {
480 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
482 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
483 *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
485 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
494 case CODEC_ID_ADPCM_4XM:
495 cs = &(c->status[0]);
496 c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
498 c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
500 c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
502 c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
504 // if (cs->step_index < 0) cs->step_index = 0;
505 // if (cs->step_index > 88) cs->step_index = 88;
507 m= (buf_size - (src - buf))>>st;
508 //printf("%d %d %d %d\n", st, m, c->status[0].predictor, c->status[0].step_index);
509 //FIXME / XXX decode chanels individual & interleave samples
511 *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] & 0x0F);
513 *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F);
514 *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4);
516 *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4);
522 case CODEC_ID_ADPCM_MS:
524 if (buf_size > BLKSIZE) {
525 if (avctx->block_align != 0)
526 buf_size = avctx->block_align;
530 n = buf_size - 7 * avctx->channels;
533 block_predictor[0] = (*src++); /* should be bound */
534 block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
535 block_predictor[1] = 0;
537 block_predictor[1] = (*src++);
538 block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
539 c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
540 if (c->status[0].idelta & 0x08000)
541 c->status[0].idelta -= 0x10000;
544 c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
545 if (st && c->status[1].idelta & 0x08000)
546 c->status[1].idelta |= 0xFFFF0000;
549 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
550 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
551 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
552 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
554 c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
556 if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
558 c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
560 if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
563 *samples++ = c->status[0].sample1;
564 if (st) *samples++ = c->status[1].sample1;
565 *samples++ = c->status[0].sample2;
566 if (st) *samples++ = c->status[1].sample2;
568 *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
569 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
573 case CODEC_ID_ADPCM_IMA_DK4:
574 if (buf_size > BLKSIZE) {
575 if (avctx->block_align != 0)
576 buf_size = avctx->block_align;
580 c->status[0].predictor = (src[0] | (src[1] << 8));
581 c->status[0].step_index = src[2];
583 if(c->status[0].predictor & 0x8000)
584 c->status[0].predictor -= 0x10000;
585 *samples++ = c->status[0].predictor;
587 c->status[1].predictor = (src[0] | (src[1] << 8));
588 c->status[1].step_index = src[2];
590 if(c->status[1].predictor & 0x8000)
591 c->status[1].predictor -= 0x10000;
592 *samples++ = c->status[1].predictor;
594 while (src < buf + buf_size) {
596 /* take care of the top nibble (always left or mono channel) */
597 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
598 (src[0] >> 4) & 0x0F);
600 /* take care of the bottom nibble, which is right sample for
601 * stereo, or another mono sample */
603 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
606 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
612 case CODEC_ID_ADPCM_IMA_DK3:
613 if (buf_size > BLKSIZE) {
614 if (avctx->block_align != 0)
615 buf_size = avctx->block_align;
619 c->status[0].predictor = (src[10] | (src[11] << 8));
620 c->status[1].predictor = (src[12] | (src[13] << 8));
621 c->status[0].step_index = src[14];
622 c->status[1].step_index = src[15];
623 /* sign extend the predictors */
624 if(c->status[0].predictor & 0x8000)
625 c->status[0].predictor -= 0x10000;
626 if(c->status[1].predictor & 0x8000)
627 c->status[1].predictor -= 0x10000;
629 diff_channel = c->status[1].predictor;
631 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
632 * the buffer is consumed */
635 /* for this algorithm, c->status[0] is the sum channel and
636 * c->status[1] is the diff channel */
638 /* process the first predictor of the sum channel */
639 DK3_GET_NEXT_NIBBLE();
640 adpcm_ima_expand_nibble(&c->status[0], nibble);
642 /* process the diff channel predictor */
643 DK3_GET_NEXT_NIBBLE();
644 adpcm_ima_expand_nibble(&c->status[1], nibble);
646 /* process the first pair of stereo PCM samples */
647 diff_channel = (diff_channel + c->status[1].predictor) / 2;
648 *samples++ = c->status[0].predictor + c->status[1].predictor;
649 *samples++ = c->status[0].predictor - c->status[1].predictor;
651 /* process the second predictor of the sum channel */
652 DK3_GET_NEXT_NIBBLE();
653 adpcm_ima_expand_nibble(&c->status[0], nibble);
655 /* process the second pair of stereo PCM samples */
656 diff_channel = (diff_channel + c->status[1].predictor) / 2;
657 *samples++ = c->status[0].predictor + c->status[1].predictor;
658 *samples++ = c->status[0].predictor - c->status[1].predictor;
665 *data_size = (uint8_t *)samples - (uint8_t *)data;
669 #define ADPCM_CODEC(id, name) \
670 AVCodec name ## _encoder = { \
674 sizeof(ADPCMContext), \
676 adpcm_encode_frame, \
677 adpcm_encode_close, \
680 AVCodec name ## _decoder = { \
684 sizeof(ADPCMContext), \
688 adpcm_decode_frame, \
691 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
692 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
693 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
694 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
695 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
696 ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);