Fix a crash caused by a null coded_picture pointer in ffserver.
[libav.git] / libavcodec / adpcm.c
CommitLineData
0147f198
FR
1/*
2 * ADPCM codecs
3 * Copyright (c) 2001 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "avcodec.h"
20
21/*
22 * First version by Francois Revol revol@free.fr
23 *
24 * Features and limitations:
25 *
26 * Reference documents:
27 * http://www.pcisys.net/~melanson/codecs/adpcm.txt
28 * http://www.geocities.com/SiliconValley/8682/aud3.txt
29 * http://openquicktime.sourceforge.net/plugins.htm
30 * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
889c5224
FR
31 * http://www.cs.ucla.edu/~leec/mediabench/applications.html
32 * SoX source code http://home.sprynet.com/~cbagwell/sox.html
0147f198
FR
33 */
34
35#define BLKSIZE 1024
36
37#define CLAMP_TO_SHORT(value) \
38if (value > 32767) \
39 value = 32767; \
40else if (value < -32768) \
41 value = -32768; \
42
43/* step_table[] and index_table[] are from the ADPCM reference source */
44/* This is the index table: */
45static int index_table[16] = {
46 -1, -1, -1, -1, 2, 4, 6, 8,
47 -1, -1, -1, -1, 2, 4, 6, 8,
48};
49
50/* This is the step table. Note that many programs use slight deviations from
51 * this table, but such deviations are negligible:
52 */
53static int step_table[89] = {
54 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
55 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
56 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
57 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
58 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
59 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
60 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
61 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
62 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
63};
64
889c5224 65/* Those are for MS-ADPCM */
0147f198
FR
66/* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
67static int AdaptationTable[] = {
68 230, 230, 230, 230, 307, 409, 512, 614,
69 768, 614, 512, 409, 307, 230, 230, 230
70};
71
72static int AdaptCoeff1[] = {
73 256, 512, 0, 192, 240, 460, 392
74};
75
76static int AdaptCoeff2[] = {
77 0, -256, 0, 64, 0, -208, -232
78};
79
80/* end of tables */
81
82typedef struct ADPCMChannelStatus {
83 int predictor;
84 short int step_index;
85 int step;
889c5224
FR
86 /* for encoding */
87 int prev_sample;
0147f198
FR
88
89 /* MS version */
90 short sample1;
91 short sample2;
92 int coeff1;
93 int coeff2;
94 int idelta;
95} ADPCMChannelStatus;
96
97typedef struct ADPCMContext {
98 int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
99 ADPCMChannelStatus status[2];
100 short sample_buffer[32]; /* hold left samples while waiting for right samples */
101} ADPCMContext;
102
103/* XXX: implement encoding */
104
105static int adpcm_encode_init(AVCodecContext *avctx)
106{
889c5224
FR
107 if (avctx->channels > 2)
108 return -1; /* only stereo or mono =) */
0147f198
FR
109 switch(avctx->codec->id) {
110 case CODEC_ID_ADPCM_IMA_QT:
889c5224
FR
111 fprintf(stderr, "ADPCM: codec admcp_ima_qt unsupported for encoding !\n");
112 avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
113 return -1;
0147f198
FR
114 break;
115 case CODEC_ID_ADPCM_IMA_WAV:
889c5224
FR
116 avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
117 /* and we have 4 bytes per channel overhead */
118 avctx->block_align = BLKSIZE;
119 /* seems frame_size isn't taken into account... have to buffer the samples :-( */
120 break;
121 case CODEC_ID_ADPCM_MS:
122 fprintf(stderr, "ADPCM: codec admcp_ms unsupported for encoding !\n");
123 return -1;
0147f198
FR
124 break;
125 default:
889c5224 126 return -1;
0147f198
FR
127 break;
128 }
129 return 0;
130}
131
132static int adpcm_encode_close(AVCodecContext *avctx)
133{
889c5224 134 /* nothing to free */
0147f198
FR
135 return 0;
136}
137
889c5224
FR
138
139static 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;
152 }
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;
185}
186
0147f198
FR
187static int adpcm_encode_frame(AVCodecContext *avctx,
188 unsigned char *frame, int buf_size, void *data)
189{
889c5224 190 int n;
0147f198
FR
191 short *samples;
192 unsigned char *dst;
889c5224
FR
193 ADPCMContext *c = avctx->priv_data;
194
195 dst = frame;
196 samples = (short *)data;
197/* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
0147f198
FR
198
199 switch(avctx->codec->id) {
889c5224
FR
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;
0147f198
FR
253 default:
254 return -1;
255 }
256 avctx->key_frame = 1;
0147f198
FR
257 return dst - frame;
258}
259
260static int adpcm_decode_init(AVCodecContext * avctx)
261{
262 ADPCMContext *c = avctx->priv_data;
263
264 c->channel = 0;
265 c->status[0].predictor = c->status[1].predictor = 0;
266 c->status[0].step_index = c->status[1].step_index = 0;
267 c->status[0].step = c->status[1].step = 0;
268
269 switch(avctx->codec->id) {
270 default:
271 break;
272 }
273 return 0;
274}
275
276static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
277{
278 int step_index;
279 int predictor;
280 int sign, delta, diff, step;
281
282 predictor = c->predictor;
283 step_index = c->step_index + index_table[(unsigned)nibble];
284 if (step_index < 0) step_index = 0;
285 if (step_index > 88) step_index = 88;
286
287 step = c->step;
288
289/*
290 diff = ((signed)((nibble & 0x08)?(nibble | 0xF0):(nibble)) + 0.5) * step / 4;
291 predictor += diff;
292*/
293 sign = nibble & 8;
294 delta = nibble & 7;
295 diff = step >> 3;
296 if (delta & 4) diff += step;
297 if (delta & 2) diff += step >> 1;
298 if (delta & 1) diff += step >> 2;
299 if (sign) predictor -= diff;
300 else predictor += diff;
301
302 CLAMP_TO_SHORT(predictor);
303 c->predictor = predictor;
304 c->step_index = step_index;
305 c->step = step_table[step_index];
306
307 return (short)predictor;
308}
309
310static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
311{
312 int predictor;
313
314 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
315 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
316 CLAMP_TO_SHORT(predictor);
317
318 c->sample2 = c->sample1;
319 c->sample1 = predictor;
320 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
321 if (c->idelta < 16) c->idelta = 16;
322
323 return (short)predictor;
324}
325
326static int adpcm_decode_frame(AVCodecContext *avctx,
327 void *data, int *data_size,
328 UINT8 *buf, int buf_size)
329{
330 ADPCMContext *c = avctx->priv_data;
331 ADPCMChannelStatus *cs;
332 int n, m, channel;
333 int block_predictor[2];
334 short *samples;
335 UINT8 *src;
336 int st; /* stereo */
337
338 samples = data;
339 src = buf;
340
0147f198
FR
341 st = avctx->channels == 2;
342
343 switch(avctx->codec->id) {
344 case CODEC_ID_ADPCM_IMA_QT:
345 n = (buf_size - 2);/* >> 2*avctx->channels;*/
346 channel = c->channel;
347 cs = &(c->status[channel]);
348 /* (pppppp) (piiiiiii) */
349
350 /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
351 cs->predictor = (*src++) << 8;
352 cs->predictor |= (*src & 0x80);
353 cs->predictor &= 0xFF80;
354
355 /* sign extension */
356 if(cs->predictor & 0x8000)
357 cs->predictor -= 0x10000;
358
359 CLAMP_TO_SHORT(cs->predictor);
360
361 cs->step_index = (*src++) & 0x7F;
362
889c5224 363 if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
0147f198
FR
364 if (cs->step_index > 88) cs->step_index = 88;
365
366 cs->step = step_table[cs->step_index];
367
368 if (st && channel)
369 samples++;
370
889c5224
FR
371 *samples++ = cs->predictor;
372 samples += st;
373
0147f198
FR
374 for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
375 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
376 samples += avctx->channels;
377 *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
378 samples += avctx->channels;
379 src ++;
380 }
381
382 if(st) { /* handle stereo interlacing */
383 c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
384 if(channel == 0) { /* wait for the other packet before outputing anything */
385 *data_size = 0;
386 return src - buf;
387 }
388 }
389 break;
390 case CODEC_ID_ADPCM_IMA_WAV:
391 if (buf_size > BLKSIZE) {
392 if (avctx->block_align != 0)
393 buf_size = avctx->block_align;
394 else
395 buf_size = BLKSIZE;
396 }
397 n = buf_size - 4 * avctx->channels;
398 cs = &(c->status[0]);
399 cs->predictor = (*src++) & 0x0FF;
400 cs->predictor |= ((*src++) << 8) & 0x0FF00;
401 if(cs->predictor & 0x8000)
402 cs->predictor -= 0x10000;
403 CLAMP_TO_SHORT(cs->predictor);
404
889c5224
FR
405 *samples++ = cs->predictor;
406
0147f198
FR
407 cs->step_index = *src++;
408 if (cs->step_index < 0) cs->step_index = 0;
409 if (cs->step_index > 88) cs->step_index = 88;
889c5224
FR
410 if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
411
0147f198
FR
412 if (st) {
413 cs = &(c->status[1]);
414 cs->predictor = (*src++) & 0x0FF;
415 cs->predictor |= ((*src++) << 8) & 0x0FF00;
416 if(cs->predictor & 0x8000)
417 cs->predictor -= 0x10000;
418 CLAMP_TO_SHORT(cs->predictor);
419
889c5224
FR
420 *samples++ = cs->predictor;
421
0147f198
FR
422 cs->step_index = *src++;
423 if (cs->step_index < 0) cs->step_index = 0;
424 if (cs->step_index > 88) cs->step_index = 88;
425 src++; /* unused */
426 }
427 cs = &(c->status[0]);
428
429
430 for(m=3; n>0; n--, m--) {
431 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
432 if (st)
433 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
434 *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
435 if (st)
436 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
437 src ++;
438 if (st && !m) {
439 m=3;
440 src+=4;
441 }
442 }
443 break;
444 case CODEC_ID_ADPCM_MS:
445
446 if (buf_size > BLKSIZE) {
447 if (avctx->block_align != 0)
448 buf_size = avctx->block_align;
449 else
450 buf_size = BLKSIZE;
451 }
452 n = buf_size - 7 * avctx->channels;
453 if (n < 0)
454 return -1;
455 block_predictor[0] = (*src++); /* should be bound */
456 block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
457 block_predictor[1] = 0;
458 if (st)
459 block_predictor[1] = (*src++);
460 block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
461 c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
462 if (c->status[0].idelta & 0x08000)
463 c->status[0].idelta -= 0x10000;
464 src+=2;
465 if (st)
466 c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
467 if (st && c->status[1].idelta & 0x08000)
468 c->status[1].idelta |= 0xFFFF0000;
469 if (st)
470 src+=2;
471 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
472 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
473 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
474 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
475
476 c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
477 src+=2;
478 if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
479 if (st) src+=2;
480 c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
481 src+=2;
482 if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
483 if (st) src+=2;
484
485 *samples++ = c->status[0].sample1;
486 if (st) *samples++ = c->status[1].sample1;
487 *samples++ = c->status[0].sample2;
488 if (st) *samples++ = c->status[1].sample2;
489 for(;n>0;n--) {
490 *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
491 *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
492 src ++;
493 }
494 break;
495 default:
496 *data_size = 0;
497 return -1;
498 }
499 *data_size = (UINT8 *)samples - (UINT8 *)data;
500 return src - buf;
501}
502
503#define ADPCM_CODEC(id, name) \
504AVCodec name ## _encoder = { \
505 #name, \
506 CODEC_TYPE_AUDIO, \
507 id, \
508 sizeof(ADPCMContext), \
509 adpcm_encode_init, \
510 adpcm_encode_frame, \
511 adpcm_encode_close, \
512 NULL, \
513}; \
514AVCodec name ## _decoder = { \
515 #name, \
516 CODEC_TYPE_AUDIO, \
517 id, \
518 sizeof(ADPCMContext), \
519 adpcm_decode_init, \
520 NULL, \
521 NULL, \
522 adpcm_decode_frame, \
523};
524
525ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
526ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
527ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
528
529#undef ADPCM_CODEC
889c5224 530