comparison adpcm.c @ 577:babaca0899f1 libavcodec

adpcm encoding patch by Franois Revol <revol at free dot fr>
author michaelni
date Mon, 22 Jul 2002 01:44:08 +0000
parents b0f52172f4c5
children 7fccaa0d699d
comparison
equal deleted inserted replaced
576:9aa5f0d0124e 577:babaca0899f1
26 * Reference documents: 26 * Reference documents:
27 * http://www.pcisys.net/~melanson/codecs/adpcm.txt 27 * http://www.pcisys.net/~melanson/codecs/adpcm.txt
28 * http://www.geocities.com/SiliconValley/8682/aud3.txt 28 * http://www.geocities.com/SiliconValley/8682/aud3.txt
29 * http://openquicktime.sourceforge.net/plugins.htm 29 * http://openquicktime.sourceforge.net/plugins.htm
30 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html 30 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
31 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
32 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
31 */ 33 */
32 34
33 #define BLKSIZE 1024 35 #define BLKSIZE 1024
34 36
35 #define CLAMP_TO_SHORT(value) \ 37 #define CLAMP_TO_SHORT(value) \
58 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 60 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
59 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 61 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
60 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 62 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
61 }; 63 };
62 64
65 /* Those are for MS-ADPCM */
63 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */ 66 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
64 static int AdaptationTable[] = { 67 static int AdaptationTable[] = {
65 230, 230, 230, 230, 307, 409, 512, 614, 68 230, 230, 230, 230, 307, 409, 512, 614,
66 768, 614, 512, 409, 307, 230, 230, 230 69 768, 614, 512, 409, 307, 230, 230, 230
67 }; 70 };
78 81
79 typedef struct ADPCMChannelStatus { 82 typedef struct ADPCMChannelStatus {
80 int predictor; 83 int predictor;
81 short int step_index; 84 short int step_index;
82 int step; 85 int step;
86 /* for encoding */
87 int prev_sample;
83 88
84 /* MS version */ 89 /* MS version */
85 short sample1; 90 short sample1;
86 short sample2; 91 short sample2;
87 int coeff1; 92 int coeff1;
97 102
98 /* XXX: implement encoding */ 103 /* XXX: implement encoding */
99 104
100 static int adpcm_encode_init(AVCodecContext *avctx) 105 static int adpcm_encode_init(AVCodecContext *avctx)
101 { 106 {
107 if (avctx->channels > 2)
108 return -1; /* only stereo or mono =) */
102 switch(avctx->codec->id) { 109 switch(avctx->codec->id) {
103 case CODEC_ID_ADPCM_IMA_QT: 110 case CODEC_ID_ADPCM_IMA_QT:
104 avctx->frame_size = 64; /* XXX: ??? */ 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;
105 break; 114 break;
106 case CODEC_ID_ADPCM_IMA_WAV: 115 case CODEC_ID_ADPCM_IMA_WAV:
107 avctx->frame_size = 64; /* XXX: ??? */ 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;
108 break; 124 break;
109 default: 125 default:
110 avctx->frame_size = 1; 126 return -1;
111 break; 127 break;
112 } 128 }
113 return 0; 129 return 0;
114 } 130 }
115 131
116 static int adpcm_encode_close(AVCodecContext *avctx) 132 static int adpcm_encode_close(AVCodecContext *avctx)
117 { 133 {
118 switch(avctx->codec->id) { 134 /* nothing to free */
119 default: 135 return 0;
120 /* nothing to free */ 136 }
121 break; 137
138
139 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
140 {
141 int step_index;
142 unsigned char nibble;
143
144 int sign = 0; /* sign bit of the nibble (MSB) */
145 int delta, predicted_delta;
146
147 delta = sample - c->prev_sample;
148
149 if (delta < 0) {
150 sign = 1;
151 delta = -delta;
122 } 152 }
123 return 0; 153
154 step_index = c->step_index;
155
156 /* nibble = 4 * delta / step_table[step_index]; */
157 nibble = (delta << 2) / step_table[step_index];
158
159 if (nibble > 7)
160 nibble = 7;
161
162 step_index += index_table[nibble];
163 if (step_index < 0)
164 step_index = 0;
165 if (step_index > 88)
166 step_index = 88;
167
168 /* what the decoder will find */
169 predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
170
171 if (sign)
172 c->prev_sample -= predicted_delta;
173 else
174 c->prev_sample += predicted_delta;
175
176 CLAMP_TO_SHORT(c->prev_sample);
177
178
179 nibble += sign << 3; /* sign * 8 */
180
181 /* save back */
182 c->step_index = step_index;
183
184 return nibble;
124 } 185 }
125 186
126 static int adpcm_encode_frame(AVCodecContext *avctx, 187 static int adpcm_encode_frame(AVCodecContext *avctx,
127 unsigned char *frame, int buf_size, void *data) 188 unsigned char *frame, int buf_size, void *data)
128 { 189 {
129 int n, sample_size, v; 190 int n;
130 short *samples; 191 short *samples;
131 unsigned char *dst; 192 unsigned char *dst;
193 ADPCMContext *c = avctx->priv_data;
194
195 dst = frame;
196 samples = (short *)data;
197 /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
132 198
133 switch(avctx->codec->id) { 199 switch(avctx->codec->id) {
200 case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
201 break;
202 case CODEC_ID_ADPCM_IMA_WAV:
203 n = avctx->frame_size / 8;
204 c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
205 /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
206 *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
207 *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
208 *dst++ = (unsigned char)c->status[0].step_index;
209 *dst++ = 0; /* unknown */
210 samples++;
211 if (avctx->channels == 2) {
212 c->status[1].prev_sample = (signed short)samples[0];
213 /* c->status[1].step_index = 0; */
214 *dst++ = (c->status[1].prev_sample) & 0xFF;
215 *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
216 *dst++ = (unsigned char)c->status[1].step_index;
217 *dst++ = 0;
218 samples++;
219 }
220
221 /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
222 for (; n>0; n--) {
223 *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
224 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
225 dst++;
226 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
227 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
228 dst++;
229 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
230 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
231 dst++;
232 *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
233 *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
234 dst++;
235 /* right channel */
236 if (avctx->channels == 2) {
237 *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
238 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
239 dst++;
240 *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
241 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
242 dst++;
243 *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
244 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
245 dst++;
246 *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
247 *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
248 dst++;
249 }
250 samples += 8 * avctx->channels;
251 }
252 break;
134 default: 253 default:
135 return -1; 254 return -1;
136 } 255 }
137 avctx->key_frame = 1; 256 avctx->key_frame = 1;
138 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
139
140 return dst - frame; 257 return dst - frame;
141 } 258 }
142 259
143 static int adpcm_decode_init(AVCodecContext * avctx) 260 static int adpcm_decode_init(AVCodecContext * avctx)
144 { 261 {
219 int st; /* stereo */ 336 int st; /* stereo */
220 337
221 samples = data; 338 samples = data;
222 src = buf; 339 src = buf;
223 340
224 //printf("adpcm_decode_frame() buf_size=%i\n", buf_size);
225
226 st = avctx->channels == 2; 341 st = avctx->channels == 2;
227 342
228 switch(avctx->codec->id) { 343 switch(avctx->codec->id) {
229 case CODEC_ID_ADPCM_IMA_QT: 344 case CODEC_ID_ADPCM_IMA_QT:
230 n = (buf_size - 2);/* >> 2*avctx->channels;*/ 345 n = (buf_size - 2);/* >> 2*avctx->channels;*/
243 358
244 CLAMP_TO_SHORT(cs->predictor); 359 CLAMP_TO_SHORT(cs->predictor);
245 360
246 cs->step_index = (*src++) & 0x7F; 361 cs->step_index = (*src++) & 0x7F;
247 362
248 if (cs->step_index > 88) printf("ERROR: step_index = %i\n", cs->step_index); 363 if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
249 if (cs->step_index > 88) cs->step_index = 88; 364 if (cs->step_index > 88) cs->step_index = 88;
250 365
251 cs->step = step_table[cs->step_index]; 366 cs->step = step_table[cs->step_index];
252 367
253 if (st && channel) 368 if (st && channel)
254 samples++; 369 samples++;
370
371 *samples++ = cs->predictor;
372 samples += st;
255 373
256 for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */ 374 for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
257 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F); 375 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
258 samples += avctx->channels; 376 samples += avctx->channels;
259 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F); 377 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
282 cs->predictor |= ((*src++) << 8) & 0x0FF00; 400 cs->predictor |= ((*src++) << 8) & 0x0FF00;
283 if(cs->predictor & 0x8000) 401 if(cs->predictor & 0x8000)
284 cs->predictor -= 0x10000; 402 cs->predictor -= 0x10000;
285 CLAMP_TO_SHORT(cs->predictor); 403 CLAMP_TO_SHORT(cs->predictor);
286 404
405 *samples++ = cs->predictor;
406
287 cs->step_index = *src++; 407 cs->step_index = *src++;
288 if (cs->step_index < 0) cs->step_index = 0; 408 if (cs->step_index < 0) cs->step_index = 0;
289 if (cs->step_index > 88) cs->step_index = 88; 409 if (cs->step_index > 88) cs->step_index = 88;
290 if (*src++) puts("unused byte should be null !!"); /* unused */ 410 if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
411
291 if (st) { 412 if (st) {
292 cs = &(c->status[1]); 413 cs = &(c->status[1]);
293 cs->predictor = (*src++) & 0x0FF; 414 cs->predictor = (*src++) & 0x0FF;
294 cs->predictor |= ((*src++) << 8) & 0x0FF00; 415 cs->predictor |= ((*src++) << 8) & 0x0FF00;
295 if(cs->predictor & 0x8000) 416 if(cs->predictor & 0x8000)
296 cs->predictor -= 0x10000; 417 cs->predictor -= 0x10000;
297 CLAMP_TO_SHORT(cs->predictor); 418 CLAMP_TO_SHORT(cs->predictor);
419
420 *samples++ = cs->predictor;
298 421
299 cs->step_index = *src++; 422 cs->step_index = *src++;
300 if (cs->step_index < 0) cs->step_index = 0; 423 if (cs->step_index < 0) cs->step_index = 0;
301 if (cs->step_index > 88) cs->step_index = 88; 424 if (cs->step_index > 88) cs->step_index = 88;
302 src++; /* unused */ 425 src++; /* unused */
402 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt); 525 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
403 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav); 526 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
404 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms); 527 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
405 528
406 #undef ADPCM_CODEC 529 #undef ADPCM_CODEC
530