Mercurial > libavcodec.hg
comparison adpcm.c @ 1794:d133b4ad0620 libavcodec
M$ ADPCM encoding & some simplifications
author | michael |
---|---|
date | Sun, 08 Feb 2004 02:16:48 +0000 |
parents | 9848ca4ad861 |
children | 39ad6cd5d4a6 |
comparison
equal
deleted
inserted
replaced
1793:11124a5d04f6 | 1794:d133b4ad0620 |
---|---|
151 /* and we have 4 bytes per channel overhead */ | 151 /* and we have 4 bytes per channel overhead */ |
152 avctx->block_align = BLKSIZE; | 152 avctx->block_align = BLKSIZE; |
153 /* seems frame_size isn't taken into account... have to buffer the samples :-( */ | 153 /* seems frame_size isn't taken into account... have to buffer the samples :-( */ |
154 break; | 154 break; |
155 case CODEC_ID_ADPCM_MS: | 155 case CODEC_ID_ADPCM_MS: |
156 av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ms unsupported for encoding !\n"); | 156 avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */ |
157 return -1; | 157 /* and we have 7 bytes per channel overhead */ |
158 avctx->block_align = BLKSIZE; | |
158 break; | 159 break; |
159 default: | 160 default: |
160 return -1; | 161 return -1; |
161 break; | 162 break; |
162 } | 163 } |
221 c->step_index = step_index; | 222 c->step_index = step_index; |
222 | 223 |
223 return nibble; | 224 return nibble; |
224 } | 225 } |
225 | 226 |
227 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample) | |
228 { | |
229 int predictor, nibble, bias; | |
230 | |
231 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256; | |
232 | |
233 nibble= sample - predictor; | |
234 if(nibble>=0) bias= c->idelta/2; | |
235 else bias=-c->idelta/2; | |
236 | |
237 nibble= (nibble + bias) / c->idelta; | |
238 nibble= clip(nibble, -8, 7)&0x0F; | |
239 | |
240 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; | |
241 CLAMP_TO_SHORT(predictor); | |
242 | |
243 c->sample2 = c->sample1; | |
244 c->sample1 = predictor; | |
245 | |
246 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; | |
247 if (c->idelta < 16) c->idelta = 16; | |
248 | |
249 return nibble; | |
250 } | |
251 | |
226 static int adpcm_encode_frame(AVCodecContext *avctx, | 252 static int adpcm_encode_frame(AVCodecContext *avctx, |
227 unsigned char *frame, int buf_size, void *data) | 253 unsigned char *frame, int buf_size, void *data) |
228 { | 254 { |
229 int n; | 255 int n, i, st; |
230 short *samples; | 256 short *samples; |
231 unsigned char *dst; | 257 unsigned char *dst; |
232 ADPCMContext *c = avctx->priv_data; | 258 ADPCMContext *c = avctx->priv_data; |
233 | 259 |
234 dst = frame; | 260 dst = frame; |
235 samples = (short *)data; | 261 samples = (short *)data; |
262 st= avctx->channels == 2; | |
236 /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */ | 263 /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */ |
237 | 264 |
238 switch(avctx->codec->id) { | 265 switch(avctx->codec->id) { |
239 case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */ | 266 case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */ |
240 break; | 267 break; |
287 dst++; | 314 dst++; |
288 } | 315 } |
289 samples += 8 * avctx->channels; | 316 samples += 8 * avctx->channels; |
290 } | 317 } |
291 break; | 318 break; |
319 case CODEC_ID_ADPCM_MS: | |
320 for(i=0; i<avctx->channels; i++){ | |
321 int predictor=0; | |
322 | |
323 *dst++ = predictor; | |
324 c->status[i].coeff1 = AdaptCoeff1[predictor]; | |
325 c->status[i].coeff2 = AdaptCoeff2[predictor]; | |
326 } | |
327 for(i=0; i<avctx->channels; i++){ | |
328 if (c->status[i].idelta < 16) | |
329 c->status[i].idelta = 16; | |
330 | |
331 *dst++ = c->status[i].idelta & 0xFF; | |
332 *dst++ = c->status[i].idelta >> 8; | |
333 } | |
334 for(i=0; i<avctx->channels; i++){ | |
335 c->status[i].sample1= *samples++; | |
336 | |
337 *dst++ = c->status[i].sample1 & 0xFF; | |
338 *dst++ = c->status[i].sample1 >> 8; | |
339 } | |
340 for(i=0; i<avctx->channels; i++){ | |
341 c->status[i].sample2= *samples++; | |
342 | |
343 *dst++ = c->status[i].sample2 & 0xFF; | |
344 *dst++ = c->status[i].sample2 >> 8; | |
345 } | |
346 | |
347 for(i=7*avctx->channels; i<avctx->block_align; i++) { | |
348 int nibble; | |
349 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4; | |
350 nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++); | |
351 *dst++ = nibble; | |
352 } | |
353 break; | |
292 default: | 354 default: |
293 return -1; | 355 return -1; |
294 } | 356 } |
295 return dst - frame; | 357 return dst - frame; |
296 } | 358 } |
348 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; | 410 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; |
349 CLAMP_TO_SHORT(predictor); | 411 CLAMP_TO_SHORT(predictor); |
350 | 412 |
351 c->sample2 = c->sample1; | 413 c->sample2 = c->sample1; |
352 c->sample1 = predictor; | 414 c->sample1 = predictor; |
353 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256; | 415 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; |
354 if (c->idelta < 16) c->idelta = 16; | 416 if (c->idelta < 16) c->idelta = 16; |
355 | 417 |
356 return (short)predictor; | 418 return (short)predictor; |
357 } | 419 } |
358 | 420 |
583 if (avctx->block_align != 0 && buf_size > avctx->block_align) | 645 if (avctx->block_align != 0 && buf_size > avctx->block_align) |
584 buf_size = avctx->block_align; | 646 buf_size = avctx->block_align; |
585 n = buf_size - 7 * avctx->channels; | 647 n = buf_size - 7 * avctx->channels; |
586 if (n < 0) | 648 if (n < 0) |
587 return -1; | 649 return -1; |
588 block_predictor[0] = (*src++); /* should be bound */ | 650 block_predictor[0] = clip(*src++, 0, 7); |
589 block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0])); | |
590 block_predictor[1] = 0; | 651 block_predictor[1] = 0; |
591 if (st) | 652 if (st) |
592 block_predictor[1] = (*src++); | 653 block_predictor[1] = clip(*src++, 0, 7); |
593 block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1])); | 654 c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); |
594 c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | |
595 if (c->status[0].idelta & 0x08000) | |
596 c->status[0].idelta -= 0x10000; | |
597 src+=2; | 655 src+=2; |
598 if (st) | 656 if (st){ |
599 c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); | 657 c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); |
600 if (st && c->status[1].idelta & 0x08000) | |
601 c->status[1].idelta |= 0xFFFF0000; | |
602 if (st) | |
603 src+=2; | 658 src+=2; |
659 } | |
604 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]]; | 660 c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]]; |
605 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]]; | 661 c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]]; |
606 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]]; | 662 c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]]; |
607 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]]; | 663 c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]]; |
608 | 664 |
627 break; | 683 break; |
628 case CODEC_ID_ADPCM_IMA_DK4: | 684 case CODEC_ID_ADPCM_IMA_DK4: |
629 if (avctx->block_align != 0 && buf_size > avctx->block_align) | 685 if (avctx->block_align != 0 && buf_size > avctx->block_align) |
630 buf_size = avctx->block_align; | 686 buf_size = avctx->block_align; |
631 | 687 |
632 c->status[0].predictor = (src[0] | (src[1] << 8)); | 688 c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8)); |
633 c->status[0].step_index = src[2]; | 689 c->status[0].step_index = src[2]; |
634 src += 4; | 690 src += 4; |
635 if(c->status[0].predictor & 0x8000) | |
636 c->status[0].predictor -= 0x10000; | |
637 *samples++ = c->status[0].predictor; | 691 *samples++ = c->status[0].predictor; |
638 if (st) { | 692 if (st) { |
639 c->status[1].predictor = (src[0] | (src[1] << 8)); | 693 c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8)); |
640 c->status[1].step_index = src[2]; | 694 c->status[1].step_index = src[2]; |
641 src += 4; | 695 src += 4; |
642 if(c->status[1].predictor & 0x8000) | |
643 c->status[1].predictor -= 0x10000; | |
644 *samples++ = c->status[1].predictor; | 696 *samples++ = c->status[1].predictor; |
645 } | 697 } |
646 while (src < buf + buf_size) { | 698 while (src < buf + buf_size) { |
647 | 699 |
648 /* take care of the top nibble (always left or mono channel) */ | 700 /* take care of the top nibble (always left or mono channel) */ |
663 break; | 715 break; |
664 case CODEC_ID_ADPCM_IMA_DK3: | 716 case CODEC_ID_ADPCM_IMA_DK3: |
665 if (avctx->block_align != 0 && buf_size > avctx->block_align) | 717 if (avctx->block_align != 0 && buf_size > avctx->block_align) |
666 buf_size = avctx->block_align; | 718 buf_size = avctx->block_align; |
667 | 719 |
668 c->status[0].predictor = (src[10] | (src[11] << 8)); | 720 c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8)); |
669 c->status[1].predictor = (src[12] | (src[13] << 8)); | 721 c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8)); |
670 c->status[0].step_index = src[14]; | 722 c->status[0].step_index = src[14]; |
671 c->status[1].step_index = src[15]; | 723 c->status[1].step_index = src[15]; |
672 /* sign extend the predictors */ | 724 /* sign extend the predictors */ |
673 if(c->status[0].predictor & 0x8000) | |
674 c->status[0].predictor -= 0x10000; | |
675 if(c->status[1].predictor & 0x8000) | |
676 c->status[1].predictor -= 0x10000; | |
677 src += 16; | 725 src += 16; |
678 diff_channel = c->status[1].predictor; | 726 diff_channel = c->status[1].predictor; |
679 | 727 |
680 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when | 728 /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when |
681 * the buffer is consumed */ | 729 * the buffer is consumed */ |