3 * Copyright (c) 2001 Fabrice Bellard.
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
26 * Features and limitations:
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
33 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
34 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
39 #define CLAMP_TO_SHORT(value) \
42 else if (value < -32768) \
45 /* step_table[] and index_table[] are from the ADPCM reference source */
46 /* This is the index table: */
47 static const int index_table[16] = {
48 -1, -1, -1, -1, 2, 4, 6, 8,
49 -1, -1, -1, -1, 2, 4, 6, 8,
53 * This is the step table. Note that many programs use slight deviations from
54 * this table, but such deviations are negligible:
56 static const int step_table[89] = {
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
68 /* Those are for MS-ADPCM */
69 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
70 static const int AdaptationTable[] = {
71 230, 230, 230, 230, 307, 409, 512, 614,
72 768, 614, 512, 409, 307, 230, 230, 230
75 static const int AdaptCoeff1[] = {
76 256, 512, 0, 192, 240, 460, 392
79 static const int AdaptCoeff2[] = {
80 0, -256, 0, 64, 0, -208, -232
85 typedef struct ADPCMChannelStatus {
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 */
106 /* XXX: implement encoding */
108 static int adpcm_encode_init(AVCodecContext *avctx)
110 if (avctx->channels > 2)
111 return -1; /* only stereo or mono =) */
112 switch(avctx->codec->id) {
113 case CODEC_ID_ADPCM_IMA_QT:
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) */
118 case CODEC_ID_ADPCM_IMA_WAV:
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 :-( */
124 case CODEC_ID_ADPCM_MS:
125 fprintf(stderr, "ADPCM: codec admcp_ms unsupported for encoding !\n");
133 avctx->coded_frame= avcodec_alloc_frame();
134 avctx->coded_frame->key_frame= 1;
139 static int adpcm_encode_close(AVCodecContext *avctx)
141 av_freep(&avctx->coded_frame);
147 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
150 unsigned char nibble;
152 int sign = 0; /* sign bit of the nibble (MSB) */
153 int delta, predicted_delta;
155 delta = sample - c->prev_sample;
162 step_index = c->step_index;
164 /* nibble = 4 * delta / step_table[step_index]; */
165 nibble = (delta << 2) / step_table[step_index];
170 step_index += index_table[nibble];
176 /* what the decoder will find */
177 predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
180 c->prev_sample -= predicted_delta;
182 c->prev_sample += predicted_delta;
184 CLAMP_TO_SHORT(c->prev_sample);
187 nibble += sign << 3; /* sign * 8 */
190 c->step_index = step_index;
195 static int adpcm_encode_frame(AVCodecContext *avctx,
196 unsigned char *frame, int buf_size, void *data)
201 ADPCMContext *c = avctx->priv_data;
204 samples = (short *)data;
205 /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
207 switch(avctx->codec->id) {
208 case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
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 */
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;
229 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
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;
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;
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;
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;
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;
248 *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
249 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
251 *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
252 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
254 *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
255 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
258 samples += 8 * avctx->channels;
267 static int adpcm_decode_init(AVCodecContext * avctx)
269 ADPCMContext *c = avctx->priv_data;
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;
276 switch(avctx->codec->id) {
283 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
287 int sign, delta, diff, step;
289 step = step_table[c->step_index];
290 step_index = c->step_index + index_table[(unsigned)nibble];
291 if (step_index < 0) step_index = 0;
292 else if (step_index > 88) step_index = 88;
298 if (delta & 4) diff += step;
299 if (delta & 2) diff += step >> 1;
300 if (delta & 1) diff += step >> 2;
302 diff = ((2 * delta + 1) * step) >> 3; // no jumps
304 predictor = c->predictor;
305 if (sign) predictor -= diff;
306 else predictor += diff;
308 CLAMP_TO_SHORT(predictor);
309 c->predictor = predictor;
310 c->step_index = step_index;
312 return (short)predictor;
315 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
319 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
320 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
321 CLAMP_TO_SHORT(predictor);
323 c->sample2 = c->sample1;
324 c->sample1 = predictor;
325 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
326 if (c->idelta < 16) c->idelta = 16;
328 return (short)predictor;
331 static int adpcm_decode_frame(AVCodecContext *avctx,
332 void *data, int *data_size,
333 uint8_t *buf, int buf_size)
335 ADPCMContext *c = avctx->priv_data;
336 ADPCMChannelStatus *cs;
338 int block_predictor[2];
346 st = avctx->channels == 2;
348 switch(avctx->codec->id) {
349 case CODEC_ID_ADPCM_IMA_QT:
350 n = (buf_size - 2);/* >> 2*avctx->channels;*/
351 channel = c->channel;
352 cs = &(c->status[channel]);
353 /* (pppppp) (piiiiiii) */
355 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
356 cs->predictor = (*src++) << 8;
357 cs->predictor |= (*src & 0x80);
358 cs->predictor &= 0xFF80;
361 if(cs->predictor & 0x8000)
362 cs->predictor -= 0x10000;
364 CLAMP_TO_SHORT(cs->predictor);
366 cs->step_index = (*src++) & 0x7F;
368 if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
369 if (cs->step_index > 88) cs->step_index = 88;
371 cs->step = step_table[cs->step_index];
376 *samples++ = cs->predictor;
379 for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
380 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
381 samples += avctx->channels;
382 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
383 samples += avctx->channels;
387 if(st) { /* handle stereo interlacing */
388 c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
389 if(channel == 0) { /* wait for the other packet before outputing anything */
395 case CODEC_ID_ADPCM_IMA_WAV:
396 if (buf_size > BLKSIZE) {
397 if (avctx->block_align != 0)
398 buf_size = avctx->block_align;
402 // XXX: do as per-channel loop
403 cs = &(c->status[0]);
404 cs->predictor = (*src++) & 0x0FF;
405 cs->predictor |= ((*src++) << 8) & 0x0FF00;
406 if(cs->predictor & 0x8000)
407 cs->predictor -= 0x10000;
408 CLAMP_TO_SHORT(cs->predictor);
410 // XXX: is this correct ??: *samples++ = cs->predictor;
412 cs->step_index = *src++;
413 if (cs->step_index < 0) cs->step_index = 0;
414 if (cs->step_index > 88) cs->step_index = 88;
415 if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
418 cs = &(c->status[1]);
419 cs->predictor = (*src++) & 0x0FF;
420 cs->predictor |= ((*src++) << 8) & 0x0FF00;
421 if(cs->predictor & 0x8000)
422 cs->predictor -= 0x10000;
423 CLAMP_TO_SHORT(cs->predictor);
425 // XXX: is this correct ??: *samples++ = cs->predictor;
427 cs->step_index = *src++;
428 if (cs->step_index < 0) cs->step_index = 0;
429 if (cs->step_index > 88) cs->step_index = 88;
430 src++; /* if != 0 -> out-of-sync */
433 for(m=4; src < (buf + buf_size);) {
434 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
436 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
437 *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
439 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
448 case CODEC_ID_ADPCM_MS:
450 if (buf_size > BLKSIZE) {
451 if (avctx->block_align != 0)
452 buf_size = avctx->block_align;
456 n = buf_size - 7 * avctx->channels;
459 block_predictor[0] = (*src++); /* should be bound */
460 block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
461 block_predictor[1] = 0;
463 block_predictor[1] = (*src++);
464 block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
465 c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
466 if (c->status[0].idelta & 0x08000)
467 c->status[0].idelta -= 0x10000;
470 c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
471 if (st && c->status[1].idelta & 0x08000)
472 c->status[1].idelta |= 0xFFFF0000;
475 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
476 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
477 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
478 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
480 c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
482 if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
484 c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
486 if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
489 *samples++ = c->status[0].sample1;
490 if (st) *samples++ = c->status[1].sample1;
491 *samples++ = c->status[0].sample2;
492 if (st) *samples++ = c->status[1].sample2;
494 *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
495 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
503 *data_size = (uint8_t *)samples - (uint8_t *)data;
507 #define ADPCM_CODEC(id, name) \
508 AVCodec name ## _encoder = { \
512 sizeof(ADPCMContext), \
514 adpcm_encode_frame, \
515 adpcm_encode_close, \
518 AVCodec name ## _decoder = { \
522 sizeof(ADPCMContext), \
526 adpcm_decode_frame, \
529 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
530 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
531 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);