TIFF-LZW encoding support by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
[libav.git] / libavcodec / adpcm.c
CommitLineData
0147f198
FR
1/*
2 * ADPCM codecs
9937e686 3 * Copyright (c) 2001-2003 The ffmpeg Project
0147f198 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
0147f198
FR
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
0147f198 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
0147f198
FR
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0147f198
FR
20 */
21#include "avcodec.h"
659c3692 22#include "bitstream.h"
0147f198 23
983e3246
MN
24/**
25 * @file adpcm.c
26 * ADPCM codecs.
fc384777 27 * First version by Francois Revol (revol@free.fr)
2fdf638b 28 * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
9937e686 29 * by Mike Melanson (melanson@pcisys.net)
fc384777 30 * CD-ROM XA ADPCM codec by BERO
7d8379f2 31 * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
0147f198
FR
32 *
33 * Features and limitations:
34 *
35 * Reference documents:
9937e686 36 * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
0147f198
FR
37 * http://www.geocities.com/SiliconValley/8682/aud3.txt
38 * http://openquicktime.sourceforge.net/plugins.htm
39 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
889c5224
FR
40 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
41 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
fc384777
MM
42 *
43 * CD-ROM XA:
44 * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
45 * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
46 * readstr http://www.geocities.co.jp/Playtown/2004/
0147f198
FR
47 */
48
49#define BLKSIZE 1024
50
51#define CLAMP_TO_SHORT(value) \
52if (value > 32767) \
53 value = 32767; \
54else if (value < -32768) \
55 value = -32768; \
56
57/* step_table[] and index_table[] are from the ADPCM reference source */
58/* This is the index table: */
135ee03a 59static const int index_table[16] = {
0147f198
FR
60 -1, -1, -1, -1, 2, 4, 6, 8,
61 -1, -1, -1, -1, 2, 4, 6, 8,
62};
63
115329f1 64/**
983e3246 65 * This is the step table. Note that many programs use slight deviations from
0147f198
FR
66 * this table, but such deviations are negligible:
67 */
135ee03a 68static const int step_table[89] = {
0147f198
FR
69 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
70 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
71 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
72 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
73 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
74 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
75 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
76 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
77 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
78};
79
fc384777 80/* These are for MS-ADPCM */
0147f198 81/* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
135ee03a 82static const int AdaptationTable[] = {
0147f198
FR
83 230, 230, 230, 230, 307, 409, 512, 614,
84 768, 614, 512, 409, 307, 230, 230, 230
85};
86
135ee03a 87static const int AdaptCoeff1[] = {
0147f198
FR
88 256, 512, 0, 192, 240, 460, 392
89};
90
135ee03a 91static const int AdaptCoeff2[] = {
0147f198
FR
92 0, -256, 0, 64, 0, -208, -232
93};
94
fc384777 95/* These are for CD-ROM XA ADPCM */
1ffb0091 96static const int xa_adpcm_table[5][2] = {
fc384777
MM
97 { 0, 0 },
98 { 60, 0 },
99 { 115, -52 },
100 { 98, -55 },
101 { 122, -60 }
102};
103
c26ae41d 104static const int ea_adpcm_table[] = {
7d8379f2
MM
105 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
106 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
107};
108
c26ae41d 109static const int ct_adpcm_table[8] = {
b3bfb299
MM
110 0x00E6, 0x00E6, 0x00E6, 0x00E6,
111 0x0133, 0x0199, 0x0200, 0x0266
112};
113
659c3692 114// padded to zero where table size is less then 16
c26ae41d 115static const int swf_index_tables[4][16] = {
659c3692
AB
116 /*2*/ { -1, 2 },
117 /*3*/ { -1, -1, 2, 4 },
118 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
119 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
120};
121
2ff4524e
VM
122static const int yamaha_indexscale[] = {
123 230, 230, 230, 230, 307, 409, 512, 614,
124 230, 230, 230, 230, 307, 409, 512, 614
125};
126
127static const int yamaha_difflookup[] = {
128 1, 3, 5, 7, 9, 11, 13, 15,
129 -1, -3, -5, -7, -9, -11, -13, -15
130};
131
0147f198
FR
132/* end of tables */
133
134typedef struct ADPCMChannelStatus {
135 int predictor;
136 short int step_index;
137 int step;
889c5224
FR
138 /* for encoding */
139 int prev_sample;
0147f198
FR
140
141 /* MS version */
142 short sample1;
143 short sample2;
144 int coeff1;
145 int coeff2;
146 int idelta;
147} ADPCMChannelStatus;
148
149typedef struct ADPCMContext {
150 int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
151 ADPCMChannelStatus status[2];
152 short sample_buffer[32]; /* hold left samples while waiting for right samples */
153} ADPCMContext;
154
155/* XXX: implement encoding */
156
764ef400 157#ifdef CONFIG_ENCODERS
0147f198
FR
158static int adpcm_encode_init(AVCodecContext *avctx)
159{
889c5224
FR
160 if (avctx->channels > 2)
161 return -1; /* only stereo or mono =) */
0147f198
FR
162 switch(avctx->codec->id) {
163 case CODEC_ID_ADPCM_IMA_QT:
8dbcc9f2 164 av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
889c5224
FR
165 avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
166 return -1;
0147f198
FR
167 break;
168 case CODEC_ID_ADPCM_IMA_WAV:
889c5224
FR
169 avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
170 /* and we have 4 bytes per channel overhead */
171 avctx->block_align = BLKSIZE;
172 /* seems frame_size isn't taken into account... have to buffer the samples :-( */
173 break;
174 case CODEC_ID_ADPCM_MS:
6cf9d5eb
MN
175 avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
176 /* and we have 7 bytes per channel overhead */
177 avctx->block_align = BLKSIZE;
0147f198 178 break;
2ff4524e
VM
179 case CODEC_ID_ADPCM_YAMAHA:
180 avctx->frame_size = BLKSIZE * avctx->channels;
181 avctx->block_align = BLKSIZE;
182 break;
0147f198 183 default:
889c5224 184 return -1;
0147f198
FR
185 break;
186 }
492cd3a9
MN
187
188 avctx->coded_frame= avcodec_alloc_frame();
189 avctx->coded_frame->key_frame= 1;
190
0147f198
FR
191 return 0;
192}
193
194static int adpcm_encode_close(AVCodecContext *avctx)
195{
492cd3a9
MN
196 av_freep(&avctx->coded_frame);
197
0147f198
FR
198 return 0;
199}
200
889c5224
FR
201
202static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
203{
7e537051
LM
204 int delta = sample - c->prev_sample;
205 int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
206 c->prev_sample = c->prev_sample + ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
889c5224 207 CLAMP_TO_SHORT(c->prev_sample);
f66e4f5f 208 c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
889c5224
FR
209 return nibble;
210}
211
6cf9d5eb
MN
212static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
213{
214 int predictor, nibble, bias;
215
216 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
115329f1 217
6cf9d5eb
MN
218 nibble= sample - predictor;
219 if(nibble>=0) bias= c->idelta/2;
220 else bias=-c->idelta/2;
115329f1 221
6cf9d5eb 222 nibble= (nibble + bias) / c->idelta;
f66e4f5f 223 nibble= av_clip(nibble, -8, 7)&0x0F;
115329f1 224
6cf9d5eb
MN
225 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
226 CLAMP_TO_SHORT(predictor);
227
228 c->sample2 = c->sample1;
229 c->sample1 = predictor;
230
231 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
232 if (c->idelta < 16) c->idelta = 16;
233
234 return nibble;
235}
236
2ff4524e
VM
237static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
238{
7e537051 239 int nibble, delta;
2ff4524e
VM
240
241 if(!c->step) {
242 c->predictor = 0;
243 c->step = 127;
244 }
2ff4524e 245
7e537051 246 delta = sample - c->predictor;
2ff4524e 247
7e537051
LM
248 nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
249
250 c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8);
2ff4524e 251 CLAMP_TO_SHORT(c->predictor);
7e537051 252 c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
f66e4f5f 253 c->step = av_clip(c->step, 127, 24567);
2ff4524e 254
7e537051 255 return nibble;
2ff4524e
VM
256}
257
696d6889
LM
258typedef struct TrellisPath {
259 int nibble;
260 int prev;
261} TrellisPath;
262
263typedef struct TrellisNode {
264 uint32_t ssd;
265 int path;
266 int sample1;
267 int sample2;
268 int step;
269} TrellisNode;
270
271static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
272 uint8_t *dst, ADPCMChannelStatus *c, int n)
273{
274#define FREEZE_INTERVAL 128
275 //FIXME 6% faster if frontier is a compile-time constant
276 const int frontier = 1 << avctx->trellis;
277 const int stride = avctx->channels;
278 const int version = avctx->codec->id;
279 const int max_paths = frontier*FREEZE_INTERVAL;
280 TrellisPath paths[max_paths], *p;
281 TrellisNode node_buf[2][frontier];
282 TrellisNode *nodep_buf[2][frontier];
283 TrellisNode **nodes = nodep_buf[0]; // nodes[] is always sorted by .ssd
284 TrellisNode **nodes_next = nodep_buf[1];
285 int pathn = 0, froze = -1, i, j, k;
286
287 assert(!(max_paths&(max_paths-1)));
288
289 memset(nodep_buf, 0, sizeof(nodep_buf));
290 nodes[0] = &node_buf[1][0];
291 nodes[0]->ssd = 0;
292 nodes[0]->path = 0;
293 nodes[0]->step = c->step_index;
294 nodes[0]->sample1 = c->sample1;
295 nodes[0]->sample2 = c->sample2;
296 if(version == CODEC_ID_ADPCM_IMA_WAV)
297 nodes[0]->sample1 = c->prev_sample;
298 if(version == CODEC_ID_ADPCM_MS)
299 nodes[0]->step = c->idelta;
300 if(version == CODEC_ID_ADPCM_YAMAHA) {
301 if(c->step == 0) {
302 nodes[0]->step = 127;
303 nodes[0]->sample1 = 0;
304 } else {
305 nodes[0]->step = c->step;
306 nodes[0]->sample1 = c->predictor;
307 }
308 }
309
310 for(i=0; i<n; i++) {
311 TrellisNode *t = node_buf[i&1];
312 TrellisNode **u;
313 int sample = samples[i*stride];
314 memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
315 for(j=0; j<frontier && nodes[j]; j++) {
316 // higher j have higher ssd already, so they're unlikely to use a suboptimal next sample too
317 const int range = (j < frontier/2) ? 1 : 0;
318 const int step = nodes[j]->step;
319 int nidx;
320 if(version == CODEC_ID_ADPCM_MS) {
321 const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 256;
322 const int div = (sample - predictor) / step;
f66e4f5f
RD
323 const int nmin = av_clip(div-range, -8, 6);
324 const int nmax = av_clip(div+range, -7, 7);
696d6889
LM
325 for(nidx=nmin; nidx<=nmax; nidx++) {
326 const int nibble = nidx & 0xf;
327 int dec_sample = predictor + nidx * step;
328#define STORE_NODE(NAME, STEP_INDEX)\
329 int d;\
330 uint32_t ssd;\
331 CLAMP_TO_SHORT(dec_sample);\
332 d = sample - dec_sample;\
333 ssd = nodes[j]->ssd + d*d;\
334 if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\
335 continue;\
336 /* Collapse any two states with the same previous sample value. \
337 * One could also distinguish states by step and by 2nd to last
338 * sample, but the effects of that are negligible. */\
339 for(k=0; k<frontier && nodes_next[k]; k++) {\
340 if(dec_sample == nodes_next[k]->sample1) {\
341 assert(ssd >= nodes_next[k]->ssd);\
342 goto next_##NAME;\
343 }\
344 }\
345 for(k=0; k<frontier; k++) {\
346 if(!nodes_next[k] || ssd < nodes_next[k]->ssd) {\
347 TrellisNode *u = nodes_next[frontier-1];\
348 if(!u) {\
349 assert(pathn < max_paths);\
350 u = t++;\
351 u->path = pathn++;\
352 }\
353 u->ssd = ssd;\
354 u->step = STEP_INDEX;\
355 u->sample2 = nodes[j]->sample1;\
356 u->sample1 = dec_sample;\
357 paths[u->path].nibble = nibble;\
358 paths[u->path].prev = nodes[j]->path;\
359 memmove(&nodes_next[k+1], &nodes_next[k], (frontier-k-1)*sizeof(TrellisNode*));\
360 nodes_next[k] = u;\
361 break;\
362 }\
363 }\
364 next_##NAME:;
365 STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
366 }
367 } else if(version == CODEC_ID_ADPCM_IMA_WAV) {
368#define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
369 const int predictor = nodes[j]->sample1;\
370 const int div = (sample - predictor) * 4 / STEP_TABLE;\
f66e4f5f
RD
371 int nmin = av_clip(div-range, -7, 6);\
372 int nmax = av_clip(div+range, -6, 7);\
696d6889
LM
373 if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
374 if(nmax<0) nmax--;\
375 for(nidx=nmin; nidx<=nmax; nidx++) {\
376 const int nibble = nidx<0 ? 7-nidx : nidx;\
377 int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
378 STORE_NODE(NAME, STEP_INDEX);\
379 }
f66e4f5f 380 LOOP_NODES(ima, step_table[step], av_clip(step + index_table[nibble], 0, 88));
696d6889 381 } else { //CODEC_ID_ADPCM_YAMAHA
f66e4f5f 382 LOOP_NODES(yamaha, step, av_clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
696d6889
LM
383#undef LOOP_NODES
384#undef STORE_NODE
385 }
386 }
387
388 u = nodes;
389 nodes = nodes_next;
390 nodes_next = u;
391
392 // prevent overflow
393 if(nodes[0]->ssd > (1<<28)) {
394 for(j=1; j<frontier && nodes[j]; j++)
395 nodes[j]->ssd -= nodes[0]->ssd;
396 nodes[0]->ssd = 0;
397 }
398
399 // merge old paths to save memory
400 if(i == froze + FREEZE_INTERVAL) {
401 p = &paths[nodes[0]->path];
402 for(k=i; k>froze; k--) {
403 dst[k] = p->nibble;
404 p = &paths[p->prev];
405 }
406 froze = i;
407 pathn = 0;
408 // other nodes might use paths that don't coincide with the frozen one.
409 // checking which nodes do so is too slow, so just kill them all.
410 // this also slightly improves quality, but I don't know why.
411 memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
412 }
413 }
414
415 p = &paths[nodes[0]->path];
416 for(i=n-1; i>froze; i--) {
417 dst[i] = p->nibble;
418 p = &paths[p->prev];
419 }
420
421 c->predictor = nodes[0]->sample1;
422 c->sample1 = nodes[0]->sample1;
423 c->sample2 = nodes[0]->sample2;
424 c->step_index = nodes[0]->step;
425 c->step = nodes[0]->step;
426 c->idelta = nodes[0]->step;
427}
428
0147f198 429static int adpcm_encode_frame(AVCodecContext *avctx,
bb270c08 430 unsigned char *frame, int buf_size, void *data)
0147f198 431{
6cf9d5eb 432 int n, i, st;
0147f198
FR
433 short *samples;
434 unsigned char *dst;
889c5224
FR
435 ADPCMContext *c = avctx->priv_data;
436
437 dst = frame;
438 samples = (short *)data;
6cf9d5eb 439 st= avctx->channels == 2;
889c5224 440/* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
0147f198
FR
441
442 switch(avctx->codec->id) {
889c5224
FR
443 case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
444 break;
445 case CODEC_ID_ADPCM_IMA_WAV:
446 n = avctx->frame_size / 8;
447 c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
448/* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
449 *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
450 *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
451 *dst++ = (unsigned char)c->status[0].step_index;
452 *dst++ = 0; /* unknown */
453 samples++;
454 if (avctx->channels == 2) {
1ffb0091 455 c->status[1].prev_sample = (signed short)samples[1];
889c5224
FR
456/* c->status[1].step_index = 0; */
457 *dst++ = (c->status[1].prev_sample) & 0xFF;
458 *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
459 *dst++ = (unsigned char)c->status[1].step_index;
460 *dst++ = 0;
461 samples++;
462 }
115329f1 463
889c5224 464 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
696d6889
LM
465 if(avctx->trellis > 0) {
466 uint8_t buf[2][n*8];
467 adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n*8);
468 if(avctx->channels == 2)
469 adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n*8);
470 for(i=0; i<n; i++) {
471 *dst++ = buf[0][8*i+0] | (buf[0][8*i+1] << 4);
472 *dst++ = buf[0][8*i+2] | (buf[0][8*i+3] << 4);
473 *dst++ = buf[0][8*i+4] | (buf[0][8*i+5] << 4);
474 *dst++ = buf[0][8*i+6] | (buf[0][8*i+7] << 4);
475 if (avctx->channels == 2) {
476 *dst++ = buf[1][8*i+0] | (buf[1][8*i+1] << 4);
477 *dst++ = buf[1][8*i+2] | (buf[1][8*i+3] << 4);
478 *dst++ = buf[1][8*i+4] | (buf[1][8*i+5] << 4);
479 *dst++ = buf[1][8*i+6] | (buf[1][8*i+7] << 4);
480 }
481 }
482 } else
889c5224
FR
483 for (; n>0; n--) {
484 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
485 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
486 dst++;
487 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
488 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
489 dst++;
490 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
491 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
492 dst++;
493 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
494 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
495 dst++;
496 /* right channel */
497 if (avctx->channels == 2) {
498 *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
499 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
500 dst++;
501 *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
502 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
503 dst++;
504 *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
505 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
506 dst++;
507 *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
508 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
509 dst++;
510 }
511 samples += 8 * avctx->channels;
512 }
513 break;
6cf9d5eb
MN
514 case CODEC_ID_ADPCM_MS:
515 for(i=0; i<avctx->channels; i++){
516 int predictor=0;
517
518 *dst++ = predictor;
519 c->status[i].coeff1 = AdaptCoeff1[predictor];
520 c->status[i].coeff2 = AdaptCoeff2[predictor];
521 }
522 for(i=0; i<avctx->channels; i++){
115329f1 523 if (c->status[i].idelta < 16)
6cf9d5eb 524 c->status[i].idelta = 16;
115329f1 525
6cf9d5eb
MN
526 *dst++ = c->status[i].idelta & 0xFF;
527 *dst++ = c->status[i].idelta >> 8;
528 }
529 for(i=0; i<avctx->channels; i++){
530 c->status[i].sample1= *samples++;
531
532 *dst++ = c->status[i].sample1 & 0xFF;
533 *dst++ = c->status[i].sample1 >> 8;
534 }
535 for(i=0; i<avctx->channels; i++){
536 c->status[i].sample2= *samples++;
537
538 *dst++ = c->status[i].sample2 & 0xFF;
539 *dst++ = c->status[i].sample2 >> 8;
540 }
541
696d6889
LM
542 if(avctx->trellis > 0) {
543 int n = avctx->block_align - 7*avctx->channels;
544 uint8_t buf[2][n];
545 if(avctx->channels == 1) {
546 n *= 2;
547 adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
548 for(i=0; i<n; i+=2)
549 *dst++ = (buf[0][i] << 4) | buf[0][i+1];
550 } else {
551 adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
552 adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
553 for(i=0; i<n; i++)
554 *dst++ = (buf[0][i] << 4) | buf[1][i];
555 }
556 } else
6cf9d5eb
MN
557 for(i=7*avctx->channels; i<avctx->block_align; i++) {
558 int nibble;
559 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
560 nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
561 *dst++ = nibble;
562 }
563 break;
2ff4524e
VM
564 case CODEC_ID_ADPCM_YAMAHA:
565 n = avctx->frame_size / 2;
696d6889
LM
566 if(avctx->trellis > 0) {
567 uint8_t buf[2][n*2];
568 n *= 2;
569 if(avctx->channels == 1) {
570 adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
571 for(i=0; i<n; i+=2)
572 *dst++ = buf[0][i] | (buf[0][i+1] << 4);
573 } else {
574 adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
575 adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
576 for(i=0; i<n; i++)
577 *dst++ = buf[0][i] | (buf[1][i] << 4);
578 }
579 } else
2ff4524e
VM
580 for (; n>0; n--) {
581 for(i = 0; i < avctx->channels; i++) {
582 int nibble;
b194c327
MN
583 nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
584 nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
2ff4524e
VM
585 *dst++ = nibble;
586 }
587 samples += 2 * avctx->channels;
588 }
589 break;
0147f198
FR
590 default:
591 return -1;
592 }
0147f198
FR
593 return dst - frame;
594}
764ef400 595#endif //CONFIG_ENCODERS
0147f198
FR
596
597static int adpcm_decode_init(AVCodecContext * avctx)
598{
599 ADPCMContext *c = avctx->priv_data;
600
14c49573
MN
601 if(avctx->channels > 2U){
602 return -1;
603 }
604
0147f198
FR
605 c->channel = 0;
606 c->status[0].predictor = c->status[1].predictor = 0;
607 c->status[0].step_index = c->status[1].step_index = 0;
608 c->status[0].step = c->status[1].step = 0;
609
610 switch(avctx->codec->id) {
b3bfb299 611 case CODEC_ID_ADPCM_CT:
bb270c08
DB
612 c->status[0].step = c->status[1].step = 511;
613 break;
0147f198
FR
614 default:
615 break;
616 }
617 return 0;
618}
619
d94728c3 620static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
0147f198
FR
621{
622 int step_index;
623 int predictor;
624 int sign, delta, diff, step;
625
135ee03a 626 step = step_table[c->step_index];
0147f198
FR
627 step_index = c->step_index + index_table[(unsigned)nibble];
628 if (step_index < 0) step_index = 0;
135ee03a 629 else if (step_index > 88) step_index = 88;
0147f198 630
0147f198
FR
631 sign = nibble & 8;
632 delta = nibble & 7;
9937e686
MM
633 /* perform direct multiplication instead of series of jumps proposed by
634 * the reference ADPCM implementation since modern CPUs can do the mults
635 * quickly enough */
d94728c3 636 diff = ((2 * delta + 1) * step) >> shift;
4b465299
MN
637 predictor = c->predictor;
638 if (sign) predictor -= diff;
639 else predictor += diff;
640
641 CLAMP_TO_SHORT(predictor);
642 c->predictor = predictor;
643 c->step_index = step_index;
644
645 return (short)predictor;
646}
647
0147f198
FR
648static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
649{
650 int predictor;
651
652 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
653 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
654 CLAMP_TO_SHORT(predictor);
655
656 c->sample2 = c->sample1;
657 c->sample1 = predictor;
6cf9d5eb 658 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
0147f198
FR
659 if (c->idelta < 16) c->idelta = 16;
660
661 return (short)predictor;
662}
663
b3bfb299
MM
664static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
665{
666 int predictor;
667 int sign, delta, diff;
668 int new_step;
669
670 sign = nibble & 8;
671 delta = nibble & 7;
672 /* perform direct multiplication instead of series of jumps proposed by
673 * the reference ADPCM implementation since modern CPUs can do the mults
674 * quickly enough */
675 diff = ((2 * delta + 1) * c->step) >> 3;
676 predictor = c->predictor;
677 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
678 if(sign)
bb270c08 679 predictor = ((predictor * 254) >> 8) - diff;
b3bfb299 680 else
bb270c08 681 predictor = ((predictor * 254) >> 8) + diff;
b3bfb299
MM
682 /* calculate new step and clamp it to range 511..32767 */
683 new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
684 c->step = new_step;
685 if(c->step < 511)
bb270c08 686 c->step = 511;
b3bfb299 687 if(c->step > 32767)
bb270c08 688 c->step = 32767;
b3bfb299
MM
689
690 CLAMP_TO_SHORT(predictor);
691 c->predictor = predictor;
692 return (short)predictor;
693}
694
2433f24f
AJ
695static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
696{
697 int sign, delta, diff;
698
699 sign = nibble & (1<<(size-1));
700 delta = nibble & ((1<<(size-1))-1);
701 diff = delta << (7 + c->step + shift);
702
703 if (sign)
704 c->predictor -= diff;
705 else
706 c->predictor += diff;
707
708 /* clamp result */
709 if (c->predictor > 16256)
710 c->predictor = 16256;
711 else if (c->predictor < -16384)
712 c->predictor = -16384;
713
714 /* calculate new step */
715 if (delta >= (2*size - 3) && c->step < 3)
716 c->step++;
717 else if (delta == 0 && c->step > 0)
718 c->step--;
719
720 return (short) c->predictor;
721}
722
2ff4524e
VM
723static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
724{
725 if(!c->step) {
726 c->predictor = 0;
727 c->step = 127;
728 }
729
730 c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
731 CLAMP_TO_SHORT(c->predictor);
732 c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
f66e4f5f 733 c->step = av_clip(c->step, 127, 24567);
2ff4524e
VM
734 return c->predictor;
735}
736
115329f1 737static void xa_decode(short *out, const unsigned char *in,
fc384777
MM
738 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
739{
740 int i, j;
741 int shift,filter,f0,f1;
742 int s_1,s_2;
743 int d,s,t;
744
745 for(i=0;i<4;i++) {
746
747 shift = 12 - (in[4+i*2] & 15);
748 filter = in[4+i*2] >> 4;
749 f0 = xa_adpcm_table[filter][0];
750 f1 = xa_adpcm_table[filter][1];
751
752 s_1 = left->sample1;
753 s_2 = left->sample2;
754
755 for(j=0;j<28;j++) {
756 d = in[16+i+j*4];
757
758 t = (signed char)(d<<4)>>4;
759 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
760 CLAMP_TO_SHORT(s);
761 *out = s;
762 out += inc;
763 s_2 = s_1;
764 s_1 = s;
765 }
766
767 if (inc==2) { /* stereo */
768 left->sample1 = s_1;
769 left->sample2 = s_2;
770 s_1 = right->sample1;
771 s_2 = right->sample2;
772 out = out + 1 - 28*2;
773 }
774
775 shift = 12 - (in[5+i*2] & 15);
776 filter = in[5+i*2] >> 4;
777
778 f0 = xa_adpcm_table[filter][0];
779 f1 = xa_adpcm_table[filter][1];
780
781 for(j=0;j<28;j++) {
782 d = in[16+i+j*4];
783
784 t = (signed char)d >> 4;
785 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
786 CLAMP_TO_SHORT(s);
787 *out = s;
788 out += inc;
789 s_2 = s_1;
790 s_1 = s;
791 }
792
793 if (inc==2) { /* stereo */
794 right->sample1 = s_1;
795 right->sample2 = s_2;
796 out -= 1;
797 } else {
798 left->sample1 = s_1;
799 left->sample2 = s_2;
800 }
801 }
802}
803
804
9937e686
MM
805/* DK3 ADPCM support macro */
806#define DK3_GET_NEXT_NIBBLE() \
807 if (decode_top_nibble_next) \
808 { \
809 nibble = (last_byte >> 4) & 0x0F; \
810 decode_top_nibble_next = 0; \
811 } \
812 else \
813 { \
814 last_byte = *src++; \
815 if (src >= buf + buf_size) break; \
816 nibble = last_byte & 0x0F; \
817 decode_top_nibble_next = 1; \
818 }
819
0147f198 820static int adpcm_decode_frame(AVCodecContext *avctx,
bb270c08
DB
821 void *data, int *data_size,
822 uint8_t *buf, int buf_size)
0147f198
FR
823{
824 ADPCMContext *c = avctx->priv_data;
825 ADPCMChannelStatus *cs;
4b465299 826 int n, m, channel, i;
0147f198
FR
827 int block_predictor[2];
828 short *samples;
14c49573 829 short *samples_end;
0c1a9eda 830 uint8_t *src;
0147f198
FR
831 int st; /* stereo */
832
9937e686
MM
833 /* DK3 ADPCM accounting variables */
834 unsigned char last_byte = 0;
835 unsigned char nibble;
836 int decode_top_nibble_next = 0;
837 int diff_channel;
838
7d8379f2
MM
839 /* EA ADPCM state variables */
840 uint32_t samples_in_chunk;
841 int32_t previous_left_sample, previous_right_sample;
842 int32_t current_left_sample, current_right_sample;
843 int32_t next_left_sample, next_right_sample;
844 int32_t coeff1l, coeff2l, coeff1r, coeff2r;
845 uint8_t shift_left, shift_right;
846 int count1, count2;
847
df72754d
MM
848 if (!buf_size)
849 return 0;
850
14c49573
MN
851 //should protect all 4bit ADPCM variants
852 //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
853 //
854 if(*data_size/4 < buf_size + 8)
855 return -1;
856
0147f198 857 samples = data;
14c49573
MN
858 samples_end= samples + *data_size/2;
859 *data_size= 0;
0147f198
FR
860 src = buf;
861
2433f24f 862 st = avctx->channels == 2 ? 1 : 0;
0147f198
FR
863
864 switch(avctx->codec->id) {
865 case CODEC_ID_ADPCM_IMA_QT:
866 n = (buf_size - 2);/* >> 2*avctx->channels;*/
867 channel = c->channel;
868 cs = &(c->status[channel]);
869 /* (pppppp) (piiiiiii) */
870
871 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
872 cs->predictor = (*src++) << 8;
873 cs->predictor |= (*src & 0x80);
874 cs->predictor &= 0xFF80;
875
876 /* sign extension */
877 if(cs->predictor & 0x8000)
878 cs->predictor -= 0x10000;
879
880 CLAMP_TO_SHORT(cs->predictor);
881
882 cs->step_index = (*src++) & 0x7F;
883
8d359bba
MN
884 if (cs->step_index > 88){
885 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
886 cs->step_index = 88;
887 }
0147f198
FR
888
889 cs->step = step_table[cs->step_index];
890
891 if (st && channel)
892 samples++;
893
894 for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
d94728c3 895 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
0147f198 896 samples += avctx->channels;
d94728c3 897 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
0147f198
FR
898 samples += avctx->channels;
899 src ++;
900 }
901
902 if(st) { /* handle stereo interlacing */
903 c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
ac3d5cac 904 if(channel == 1) { /* wait for the other packet before outputing anything */
0147f198
FR
905 return src - buf;
906 }
907 }
908 break;
909 case CODEC_ID_ADPCM_IMA_WAV:
ca1d62f4
AY
910 if (avctx->block_align != 0 && buf_size > avctx->block_align)
911 buf_size = avctx->block_align;
912
8d359bba
MN
913// samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
914
d94728c3
MN
915 for(i=0; i<avctx->channels; i++){
916 cs = &(c->status[i]);
8d359bba
MN
917 cs->predictor = (int16_t)(src[0] + (src[1]<<8));
918 src+=2;
0147f198 919
bb270c08 920 // XXX: is this correct ??: *samples++ = cs->predictor;
889c5224 921
d94728c3 922 cs->step_index = *src++;
8d359bba
MN
923 if (cs->step_index > 88){
924 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
925 cs->step_index = 88;
926 }
927 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
0147f198 928 }
0147f198 929
8d359bba
MN
930 while(src < buf + buf_size){
931 for(m=0; m<4; m++){
932 for(i=0; i<=st; i++)
933 *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
934 for(i=0; i<=st; i++)
935 *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
936 src++;
bb270c08 937 }
8d359bba 938 src += 4*st;
bb270c08 939 }
0147f198 940 break;
4b465299
MN
941 case CODEC_ID_ADPCM_4XM:
942 cs = &(c->status[0]);
943 c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
944 if(st){
945 c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
946 }
947 c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
948 if(st){
949 c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
950 }
ac3d5cac
MM
951 if (cs->step_index < 0) cs->step_index = 0;
952 if (cs->step_index > 88) cs->step_index = 88;
4b465299
MN
953
954 m= (buf_size - (src - buf))>>st;
4b465299 955 for(i=0; i<m; i++) {
bb270c08 956 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
4b465299 957 if (st)
d94728c3
MN
958 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
959 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
bb270c08 960 if (st)
d94728c3 961 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
bb270c08 962 }
4b465299
MN
963
964 src += m<<st;
965
966 break;
0147f198 967 case CODEC_ID_ADPCM_MS:
ca1d62f4
AY
968 if (avctx->block_align != 0 && buf_size > avctx->block_align)
969 buf_size = avctx->block_align;
0147f198
FR
970 n = buf_size - 7 * avctx->channels;
971 if (n < 0)
972 return -1;
f66e4f5f 973 block_predictor[0] = av_clip(*src++, 0, 7);
0147f198
FR
974 block_predictor[1] = 0;
975 if (st)
f66e4f5f 976 block_predictor[1] = av_clip(*src++, 0, 7);
6cf9d5eb 977 c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
0147f198 978 src+=2;
6cf9d5eb
MN
979 if (st){
980 c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
0147f198 981 src+=2;
6cf9d5eb 982 }
0147f198
FR
983 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
984 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
985 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
986 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
115329f1 987
0147f198
FR
988 c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
989 src+=2;
990 if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
991 if (st) src+=2;
992 c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
993 src+=2;
994 if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
995 if (st) src+=2;
996
997 *samples++ = c->status[0].sample1;
998 if (st) *samples++ = c->status[1].sample1;
999 *samples++ = c->status[0].sample2;
1000 if (st) *samples++ = c->status[1].sample2;
1001 for(;n>0;n--) {
1002 *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
1003 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
1004 src ++;
1005 }
1006 break;
9937e686 1007 case CODEC_ID_ADPCM_IMA_DK4:
5c69a4fd
MN
1008 if (avctx->block_align != 0 && buf_size > avctx->block_align)
1009 buf_size = avctx->block_align;
1010
6cf9d5eb 1011 c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
9937e686
MM
1012 c->status[0].step_index = src[2];
1013 src += 4;
9937e686
MM
1014 *samples++ = c->status[0].predictor;
1015 if (st) {
6cf9d5eb 1016 c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
9937e686
MM
1017 c->status[1].step_index = src[2];
1018 src += 4;
9937e686
MM
1019 *samples++ = c->status[1].predictor;
1020 }
1021 while (src < buf + buf_size) {
1022
1023 /* take care of the top nibble (always left or mono channel) */
115329f1 1024 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
d94728c3 1025 (src[0] >> 4) & 0x0F, 3);
9937e686
MM
1026
1027 /* take care of the bottom nibble, which is right sample for
1028 * stereo, or another mono sample */
1029 if (st)
115329f1 1030 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
d94728c3 1031 src[0] & 0x0F, 3);
9937e686 1032 else
115329f1 1033 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
d94728c3 1034 src[0] & 0x0F, 3);
9937e686
MM
1035
1036 src++;
1037 }
1038 break;
1039 case CODEC_ID_ADPCM_IMA_DK3:
5c69a4fd
MN
1040 if (avctx->block_align != 0 && buf_size > avctx->block_align)
1041 buf_size = avctx->block_align;
1042
14c49573
MN
1043 if(buf_size + 16 > (samples_end - samples)*3/8)
1044 return -1;
1045
6cf9d5eb
MN
1046 c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
1047 c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
9937e686
MM
1048 c->status[0].step_index = src[14];
1049 c->status[1].step_index = src[15];
1050 /* sign extend the predictors */
9937e686
MM
1051 src += 16;
1052 diff_channel = c->status[1].predictor;
1053
1054 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
1055 * the buffer is consumed */
1056 while (1) {
1057
1058 /* for this algorithm, c->status[0] is the sum channel and
1059 * c->status[1] is the diff channel */
1060
1061 /* process the first predictor of the sum channel */
1062 DK3_GET_NEXT_NIBBLE();
d94728c3 1063 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
9937e686
MM
1064
1065 /* process the diff channel predictor */
1066 DK3_GET_NEXT_NIBBLE();
d94728c3 1067 adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
9937e686
MM
1068
1069 /* process the first pair of stereo PCM samples */
1070 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1071 *samples++ = c->status[0].predictor + c->status[1].predictor;
1072 *samples++ = c->status[0].predictor - c->status[1].predictor;
1073
1074 /* process the second predictor of the sum channel */
1075 DK3_GET_NEXT_NIBBLE();
d94728c3 1076 adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
9937e686
MM
1077
1078 /* process the second pair of stereo PCM samples */
1079 diff_channel = (diff_channel + c->status[1].predictor) / 2;
1080 *samples++ = c->status[0].predictor + c->status[1].predictor;
1081 *samples++ = c->status[0].predictor - c->status[1].predictor;
1082 }
1083 break;
2fdf638b
MM
1084 case CODEC_ID_ADPCM_IMA_WS:
1085 /* no per-block initialization; just start decoding the data */
1086 while (src < buf + buf_size) {
1087
1088 if (st) {
115329f1 1089 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
d94728c3 1090 (src[0] >> 4) & 0x0F, 3);
115329f1 1091 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
d94728c3 1092 src[0] & 0x0F, 3);
2fdf638b 1093 } else {
115329f1 1094 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
d94728c3 1095 (src[0] >> 4) & 0x0F, 3);
115329f1 1096 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
d94728c3 1097 src[0] & 0x0F, 3);
2fdf638b
MM
1098 }
1099
1100 src++;
1101 }
1102 break;
fc384777 1103 case CODEC_ID_ADPCM_XA:
115329f1 1104 c->status[0].sample1 = c->status[0].sample2 =
fc384777
MM
1105 c->status[1].sample1 = c->status[1].sample2 = 0;
1106 while (buf_size >= 128) {
115329f1 1107 xa_decode(samples, src, &c->status[0], &c->status[1],
fc384777
MM
1108 avctx->channels);
1109 src += 128;
1110 samples += 28 * 8;
1111 buf_size -= 128;
1112 }
1113 break;
7d8379f2 1114 case CODEC_ID_ADPCM_EA:
fead30d4 1115 samples_in_chunk = AV_RL32(src);
7d8379f2
MM
1116 if (samples_in_chunk >= ((buf_size - 12) * 2)) {
1117 src += buf_size;
1118 break;
1119 }
1120 src += 4;
fead30d4 1121 current_left_sample = (int16_t)AV_RL16(src);
7d8379f2 1122 src += 2;
fead30d4 1123 previous_left_sample = (int16_t)AV_RL16(src);
7d8379f2 1124 src += 2;
fead30d4 1125 current_right_sample = (int16_t)AV_RL16(src);
7d8379f2 1126 src += 2;
fead30d4 1127 previous_right_sample = (int16_t)AV_RL16(src);
7d8379f2
MM
1128 src += 2;
1129
1130 for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
1131 coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
1132 coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
1133 coeff1r = ea_adpcm_table[*src & 0x0F];
1134 coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
1135 src++;
1136
1137 shift_left = ((*src >> 4) & 0x0F) + 8;
1138 shift_right = (*src & 0x0F) + 8;
1139 src++;
1140
1141 for (count2 = 0; count2 < 28; count2++) {
1142 next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
1143 next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
1144 src++;
1145
115329f1
DB
1146 next_left_sample = (next_left_sample +
1147 (current_left_sample * coeff1l) +
7d8379f2 1148 (previous_left_sample * coeff2l) + 0x80) >> 8;
115329f1
DB
1149 next_right_sample = (next_right_sample +
1150 (current_right_sample * coeff1r) +
7d8379f2
MM
1151 (previous_right_sample * coeff2r) + 0x80) >> 8;
1152 CLAMP_TO_SHORT(next_left_sample);
1153 CLAMP_TO_SHORT(next_right_sample);
1154
1155 previous_left_sample = current_left_sample;
1156 current_left_sample = next_left_sample;
1157 previous_right_sample = current_right_sample;
1158 current_right_sample = next_right_sample;
1159 *samples++ = (unsigned short)current_left_sample;
1160 *samples++ = (unsigned short)current_right_sample;
1161 }
1162 }
1163 break;
1164 case CODEC_ID_ADPCM_IMA_SMJPEG:
1165 c->status[0].predictor = *src;
1166 src += 2;
1167 c->status[0].step_index = *src++;
1168 src++; /* skip another byte before getting to the meat */
1169 while (src < buf + buf_size) {
1170 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1171 *src & 0x0F, 3);
1172 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
1173 (*src >> 4) & 0x0F, 3);
1174 src++;
1175 }
1176 break;
b3bfb299 1177 case CODEC_ID_ADPCM_CT:
bb270c08 1178 while (src < buf + buf_size) {
b3bfb299 1179 if (st) {
115329f1 1180 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
b3bfb299 1181 (src[0] >> 4) & 0x0F);
115329f1 1182 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
b3bfb299
MM
1183 src[0] & 0x0F);
1184 } else {
115329f1 1185 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
b3bfb299 1186 (src[0] >> 4) & 0x0F);
115329f1 1187 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
b3bfb299
MM
1188 src[0] & 0x0F);
1189 }
bb270c08 1190 src++;
b3bfb299
MM
1191 }
1192 break;
2433f24f
AJ
1193 case CODEC_ID_ADPCM_SBPRO_4:
1194 case CODEC_ID_ADPCM_SBPRO_3:
1195 case CODEC_ID_ADPCM_SBPRO_2:
1196 if (!c->status[0].step_index) {
1197 /* the first byte is a raw sample */
1198 *samples++ = 128 * (*src++ - 0x80);
1199 if (st)
1200 *samples++ = 128 * (*src++ - 0x80);
1201 c->status[0].step_index = 1;
1202 }
1203 if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
1204 while (src < buf + buf_size) {
1205 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1206 (src[0] >> 4) & 0x0F, 4, 0);
1207 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1208 src[0] & 0x0F, 4, 0);
1209 src++;
1210 }
1211 } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
14c49573 1212 while (src < buf + buf_size && samples + 2 < samples_end) {
2433f24f
AJ
1213 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1214 (src[0] >> 5) & 0x07, 3, 0);
1215 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1216 (src[0] >> 2) & 0x07, 3, 0);
1217 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1218 src[0] & 0x03, 2, 0);
1219 src++;
1220 }
1221 } else {
14c49573 1222 while (src < buf + buf_size && samples + 3 < samples_end) {
2433f24f
AJ
1223 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1224 (src[0] >> 6) & 0x03, 2, 2);
1225 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1226 (src[0] >> 4) & 0x03, 2, 2);
1227 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
1228 (src[0] >> 2) & 0x03, 2, 2);
1229 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
1230 src[0] & 0x03, 2, 2);
1231 src++;
1232 }
1233 }
1234 break;
659c3692
AB
1235 case CODEC_ID_ADPCM_SWF:
1236 {
bb270c08
DB
1237 GetBitContext gb;
1238 const int *table;
387afa9d 1239 int k0, signmask, nb_bits;
bb270c08
DB
1240 int size = buf_size*8;
1241
1242 init_get_bits(&gb, buf, size);
1243
387afa9d
BC
1244 //read bits & inital values
1245 nb_bits = get_bits(&gb, 2)+2;
1246 //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
1247 table = swf_index_tables[nb_bits-2];
1248 k0 = 1 << (nb_bits-2);
1249 signmask = 1 << (nb_bits-1);
bb270c08 1250
387afa9d
BC
1251 for (i = 0; i < avctx->channels; i++) {
1252 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1253 c->status[i].step_index = get_bits(&gb, 6);
1254 }
bb270c08 1255
387afa9d 1256 while (get_bits_count(&gb) < size)
bb270c08
DB
1257 {
1258 int i;
1259
387afa9d
BC
1260 for (i = 0; i < avctx->channels; i++) {
1261 // similar to IMA adpcm
1262 int delta = get_bits(&gb, nb_bits);
bb270c08
DB
1263 int step = step_table[c->status[i].step_index];
1264 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1265 int k = k0;
1266
1267 do {
1268 if (delta & k)
1269 vpdiff += step;
1270 step >>= 1;
1271 k >>= 1;
1272 } while(k);
1273 vpdiff += step;
1274
1275 if (delta & signmask)
1276 c->status[i].predictor -= vpdiff;
1277 else
1278 c->status[i].predictor += vpdiff;
1279
1280 c->status[i].step_index += table[delta & (~signmask)];
1281
f66e4f5f
RD
1282 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
1283 c->status[i].predictor = av_clip(c->status[i].predictor, -32768, 32767);
bb270c08
DB
1284
1285 *samples++ = c->status[i].predictor;
387afa9d
BC
1286 if (samples >= samples_end) {
1287 av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
1288 return -1;
1289 }
bb270c08
DB
1290 }
1291 }
387afa9d 1292 src += buf_size;
bb270c08 1293 break;
659c3692 1294 }
2ff4524e
VM
1295 case CODEC_ID_ADPCM_YAMAHA:
1296 while (src < buf + buf_size) {
1297 if (st) {
1298 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
2ff4524e 1299 src[0] & 0x0F);
b194c327 1300 *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
2ff4524e 1301 (src[0] >> 4) & 0x0F);
b194c327 1302 } else {
2ff4524e
VM
1303 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1304 src[0] & 0x0F);
b194c327
MN
1305 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
1306 (src[0] >> 4) & 0x0F);
2ff4524e
VM
1307 }
1308 src++;
1309 }
1310 break;
0147f198 1311 default:
0147f198
FR
1312 return -1;
1313 }
0c1a9eda 1314 *data_size = (uint8_t *)samples - (uint8_t *)data;
0147f198
FR
1315 return src - buf;
1316}
1317
764ef400
MM
1318
1319
1320#ifdef CONFIG_ENCODERS
1321#define ADPCM_ENCODER(id,name) \
0147f198
FR
1322AVCodec name ## _encoder = { \
1323 #name, \
1324 CODEC_TYPE_AUDIO, \
1325 id, \
1326 sizeof(ADPCMContext), \
1327 adpcm_encode_init, \
1328 adpcm_encode_frame, \
1329 adpcm_encode_close, \
1330 NULL, \
764ef400
MM
1331};
1332#else
1333#define ADPCM_ENCODER(id,name)
1334#endif
1335
1336#ifdef CONFIG_DECODERS
1337#define ADPCM_DECODER(id,name) \
0147f198
FR
1338AVCodec name ## _decoder = { \
1339 #name, \
1340 CODEC_TYPE_AUDIO, \
1341 id, \
1342 sizeof(ADPCMContext), \
1343 adpcm_decode_init, \
1344 NULL, \
1345 NULL, \
1346 adpcm_decode_frame, \
1347};
764ef400
MM
1348#else
1349#define ADPCM_DECODER(id,name)
1350#endif
1351
1352#define ADPCM_CODEC(id, name) \
1353ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
0147f198
FR
1354
1355ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
1356ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
9937e686
MM
1357ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
1358ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
2fdf638b 1359ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
7d8379f2 1360ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
0147f198 1361ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
4b465299 1362ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
fc384777 1363ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
7d8379f2 1364ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
b3bfb299 1365ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
659c3692 1366ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
2ff4524e 1367ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
2433f24f
AJ
1368ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
1369ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
1370ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
0147f198
FR
1371
1372#undef ADPCM_CODEC