comparison adpcm.c @ 573:b0f52172f4c5 libavcodec

beos/mov/adpcm patch by Franois Revol <revol at free dot fr> * Some BeOS fixes: - errno stuff - nanosleep() replacement - added a doc/README.beos * mov reader still has problems with most videos (skips many chunks), - It should now read .mov files with zlib-compressed moov headers (aka cmov) - added SVQ1 support. - removed mapping 'raw ' and 'yuv2' to h263, was my mistake. - added IMA4 support. (tested) - fixed frame rate (it reported 0 fps or something before) - extended file probing ('wide' and 'free' atoms) - improved .mov/.mp4 detection (or so I think) * adpcm * added zlib support, with header file and lib check
author michaelni
date Sat, 20 Jul 2002 20:05:50 +0000
parents
children babaca0899f1
comparison
equal deleted inserted replaced
572:5a06c3552026 573:b0f52172f4c5
1 /*
2 * ADPCM codecs
3 * Copyright (c) 2001 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avcodec.h"
20
21 /*
22 * First version by Francois Revol revol@free.fr
23 *
24 * Features and limitations:
25 *
26 * Reference documents:
27 * http://www.pcisys.net/~melanson/codecs/adpcm.txt
28 * http://www.geocities.com/SiliconValley/8682/aud3.txt
29 * http://openquicktime.sourceforge.net/plugins.htm
30 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
31 */
32
33 #define BLKSIZE 1024
34
35 #define CLAMP_TO_SHORT(value) \
36 if (value > 32767) \
37 value = 32767; \
38 else if (value < -32768) \
39 value = -32768; \
40
41 /* step_table[] and index_table[] are from the ADPCM reference source */
42 /* This is the index table: */
43 static int index_table[16] = {
44 -1, -1, -1, -1, 2, 4, 6, 8,
45 -1, -1, -1, -1, 2, 4, 6, 8,
46 };
47
48 /* This is the step table. Note that many programs use slight deviations from
49 * this table, but such deviations are negligible:
50 */
51 static int step_table[89] = {
52 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
53 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
54 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
55 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
56 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
57 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
58 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
59 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
60 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
61 };
62
63 /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
64 static int AdaptationTable[] = {
65 230, 230, 230, 230, 307, 409, 512, 614,
66 768, 614, 512, 409, 307, 230, 230, 230
67 };
68
69 static int AdaptCoeff1[] = {
70 256, 512, 0, 192, 240, 460, 392
71 };
72
73 static int AdaptCoeff2[] = {
74 0, -256, 0, 64, 0, -208, -232
75 };
76
77 /* end of tables */
78
79 typedef struct ADPCMChannelStatus {
80 int predictor;
81 short int step_index;
82 int step;
83
84 /* MS version */
85 short sample1;
86 short sample2;
87 int coeff1;
88 int coeff2;
89 int idelta;
90 } ADPCMChannelStatus;
91
92 typedef struct ADPCMContext {
93 int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
94 ADPCMChannelStatus status[2];
95 short sample_buffer[32]; /* hold left samples while waiting for right samples */
96 } ADPCMContext;
97
98 /* XXX: implement encoding */
99
100 static int adpcm_encode_init(AVCodecContext *avctx)
101 {
102 switch(avctx->codec->id) {
103 case CODEC_ID_ADPCM_IMA_QT:
104 avctx->frame_size = 64; /* XXX: ??? */
105 break;
106 case CODEC_ID_ADPCM_IMA_WAV:
107 avctx->frame_size = 64; /* XXX: ??? */
108 break;
109 default:
110 avctx->frame_size = 1;
111 break;
112 }
113 return 0;
114 }
115
116 static int adpcm_encode_close(AVCodecContext *avctx)
117 {
118 switch(avctx->codec->id) {
119 default:
120 /* nothing to free */
121 break;
122 }
123 return 0;
124 }
125
126 static int adpcm_encode_frame(AVCodecContext *avctx,
127 unsigned char *frame, int buf_size, void *data)
128 {
129 int n, sample_size, v;
130 short *samples;
131 unsigned char *dst;
132
133 switch(avctx->codec->id) {
134 default:
135 return -1;
136 }
137 avctx->key_frame = 1;
138 //avctx->frame_size = (dst - frame) / (sample_size * avctx->channels);
139
140 return dst - frame;
141 }
142
143 static int adpcm_decode_init(AVCodecContext * avctx)
144 {
145 ADPCMContext *c = avctx->priv_data;
146
147 c->channel = 0;
148 c->status[0].predictor = c->status[1].predictor = 0;
149 c->status[0].step_index = c->status[1].step_index = 0;
150 c->status[0].step = c->status[1].step = 0;
151
152 switch(avctx->codec->id) {
153 default:
154 break;
155 }
156 return 0;
157 }
158
159 static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
160 {
161 int step_index;
162 int predictor;
163 int sign, delta, diff, step;
164
165 predictor = c->predictor;
166 step_index = c->step_index + index_table[(unsigned)nibble];
167 if (step_index < 0) step_index = 0;
168 if (step_index > 88) step_index = 88;
169
170 step = c->step;
171
172 /*
173 diff = ((signed)((nibble & 0x08)?(nibble | 0xF0):(nibble)) + 0.5) * step / 4;
174 predictor += diff;
175 */
176 sign = nibble & 8;
177 delta = nibble & 7;
178 diff = step >> 3;
179 if (delta & 4) diff += step;
180 if (delta & 2) diff += step >> 1;
181 if (delta & 1) diff += step >> 2;
182 if (sign) predictor -= diff;
183 else predictor += diff;
184
185 CLAMP_TO_SHORT(predictor);
186 c->predictor = predictor;
187 c->step_index = step_index;
188 c->step = step_table[step_index];
189
190 return (short)predictor;
191 }
192
193 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
194 {
195 int predictor;
196
197 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
198 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
199 CLAMP_TO_SHORT(predictor);
200
201 c->sample2 = c->sample1;
202 c->sample1 = predictor;
203 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
204 if (c->idelta < 16) c->idelta = 16;
205
206 return (short)predictor;
207 }
208
209 static int adpcm_decode_frame(AVCodecContext *avctx,
210 void *data, int *data_size,
211 UINT8 *buf, int buf_size)
212 {
213 ADPCMContext *c = avctx->priv_data;
214 ADPCMChannelStatus *cs;
215 int n, m, channel;
216 int block_predictor[2];
217 short *samples;
218 UINT8 *src;
219 int st; /* stereo */
220
221 samples = data;
222 src = buf;
223
224 //printf("adpcm_decode_frame() buf_size=%i\n", buf_size);
225
226 st = avctx->channels == 2;
227
228 switch(avctx->codec->id) {
229 case CODEC_ID_ADPCM_IMA_QT:
230 n = (buf_size - 2);/* >> 2*avctx->channels;*/
231 channel = c->channel;
232 cs = &(c->status[channel]);
233 /* (pppppp) (piiiiiii) */
234
235 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
236 cs->predictor = (*src++) << 8;
237 cs->predictor |= (*src & 0x80);
238 cs->predictor &= 0xFF80;
239
240 /* sign extension */
241 if(cs->predictor & 0x8000)
242 cs->predictor -= 0x10000;
243
244 CLAMP_TO_SHORT(cs->predictor);
245
246 cs->step_index = (*src++) & 0x7F;
247
248 if (cs->step_index > 88) printf("ERROR: step_index = %i\n", cs->step_index);
249 if (cs->step_index > 88) cs->step_index = 88;
250
251 cs->step = step_table[cs->step_index];
252
253 if (st && channel)
254 samples++;
255
256 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);
258 samples += avctx->channels;
259 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
260 samples += avctx->channels;
261 src ++;
262 }
263
264 if(st) { /* handle stereo interlacing */
265 c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
266 if(channel == 0) { /* wait for the other packet before outputing anything */
267 *data_size = 0;
268 return src - buf;
269 }
270 }
271 break;
272 case CODEC_ID_ADPCM_IMA_WAV:
273 if (buf_size > BLKSIZE) {
274 if (avctx->block_align != 0)
275 buf_size = avctx->block_align;
276 else
277 buf_size = BLKSIZE;
278 }
279 n = buf_size - 4 * avctx->channels;
280 cs = &(c->status[0]);
281 cs->predictor = (*src++) & 0x0FF;
282 cs->predictor |= ((*src++) << 8) & 0x0FF00;
283 if(cs->predictor & 0x8000)
284 cs->predictor -= 0x10000;
285 CLAMP_TO_SHORT(cs->predictor);
286
287 cs->step_index = *src++;
288 if (cs->step_index < 0) cs->step_index = 0;
289 if (cs->step_index > 88) cs->step_index = 88;
290 if (*src++) puts("unused byte should be null !!"); /* unused */
291 if (st) {
292 cs = &(c->status[1]);
293 cs->predictor = (*src++) & 0x0FF;
294 cs->predictor |= ((*src++) << 8) & 0x0FF00;
295 if(cs->predictor & 0x8000)
296 cs->predictor -= 0x10000;
297 CLAMP_TO_SHORT(cs->predictor);
298
299 cs->step_index = *src++;
300 if (cs->step_index < 0) cs->step_index = 0;
301 if (cs->step_index > 88) cs->step_index = 88;
302 src++; /* unused */
303 }
304 cs = &(c->status[0]);
305
306
307 for(m=3; n>0; n--, m--) {
308 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
309 if (st)
310 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
311 *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
312 if (st)
313 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
314 src ++;
315 if (st && !m) {
316 m=3;
317 src+=4;
318 }
319 }
320 break;
321 case CODEC_ID_ADPCM_MS:
322
323 if (buf_size > BLKSIZE) {
324 if (avctx->block_align != 0)
325 buf_size = avctx->block_align;
326 else
327 buf_size = BLKSIZE;
328 }
329 n = buf_size - 7 * avctx->channels;
330 if (n < 0)
331 return -1;
332 block_predictor[0] = (*src++); /* should be bound */
333 block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
334 block_predictor[1] = 0;
335 if (st)
336 block_predictor[1] = (*src++);
337 block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
338 c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
339 if (c->status[0].idelta & 0x08000)
340 c->status[0].idelta -= 0x10000;
341 src+=2;
342 if (st)
343 c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
344 if (st && c->status[1].idelta & 0x08000)
345 c->status[1].idelta |= 0xFFFF0000;
346 if (st)
347 src+=2;
348 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
349 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
350 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
351 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
352
353 c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
354 src+=2;
355 if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
356 if (st) src+=2;
357 c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
358 src+=2;
359 if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
360 if (st) src+=2;
361
362 *samples++ = c->status[0].sample1;
363 if (st) *samples++ = c->status[1].sample1;
364 *samples++ = c->status[0].sample2;
365 if (st) *samples++ = c->status[1].sample2;
366 for(;n>0;n--) {
367 *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
368 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
369 src ++;
370 }
371 break;
372 default:
373 *data_size = 0;
374 return -1;
375 }
376 *data_size = (UINT8 *)samples - (UINT8 *)data;
377 return src - buf;
378 }
379
380 #define ADPCM_CODEC(id, name) \
381 AVCodec name ## _encoder = { \
382 #name, \
383 CODEC_TYPE_AUDIO, \
384 id, \
385 sizeof(ADPCMContext), \
386 adpcm_encode_init, \
387 adpcm_encode_frame, \
388 adpcm_encode_close, \
389 NULL, \
390 }; \
391 AVCodec name ## _decoder = { \
392 #name, \
393 CODEC_TYPE_AUDIO, \
394 id, \
395 sizeof(ADPCMContext), \
396 adpcm_decode_init, \
397 NULL, \
398 NULL, \
399 adpcm_decode_frame, \
400 };
401
402 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
403 ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
404 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
405
406 #undef ADPCM_CODEC