VirtualBox

source: vbox/trunk/src/libs/ffmpeg-20060710/libavcodec/adpcm.c@ 7692

Last change on this file since 7692 was 5776, checked in by vboxsync, 17 years ago

ffmpeg: exported to OSE

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette