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