📄 adpcm.c
字号:
c->status[0].step = c->status[1].step = 0; switch(avctx->codec->id) { default: break; } return 0;}static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble){ int step_index; int predictor; int sign, delta, diff, step; predictor = c->predictor; step_index = c->step_index + index_table[(unsigned)nibble]; if (step_index < 0) step_index = 0; if (step_index > 88) step_index = 88; step = c->step;/* diff = ((signed)((nibble & 0x08)?(nibble | 0xF0):(nibble)) + 0.5) * step / 4; predictor += diff;*/ sign = nibble & 8; delta = nibble & 7; diff = step >> 3; if (delta & 4) diff += step; if (delta & 2) diff += step >> 1; if (delta & 1) diff += step >> 2; if (sign) predictor -= diff; else predictor += diff; CLAMP_TO_SHORT(predictor); c->predictor = predictor; c->step_index = step_index; c->step = step_table[step_index]; return (short)predictor;}static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble){ int predictor; predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256; predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; CLAMP_TO_SHORT(predictor); c->sample2 = c->sample1; c->sample1 = predictor; c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256; if (c->idelta < 16) c->idelta = 16; return (short)predictor;}static int adpcm_decode_frame(AVCodecContext *avctx, void *data, int *data_size, UINT8 *buf, int buf_size){ ADPCMContext *c = avctx->priv_data; ADPCMChannelStatus *cs; int n, m, channel; int block_predictor[2]; short *samples; UINT8 *src; int st; /* stereo */ samples = data; src = buf; st = avctx->channels == 2; switch(avctx->codec->id) { case CODEC_ID_ADPCM_IMA_QT: n = (buf_size - 2);/* >> 2*avctx->channels;*/ channel = c->channel; cs = &(c->status[channel]); /* (pppppp) (piiiiiii) */ /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */ cs->predictor = (*src++) << 8; cs->predictor |= (*src & 0x80); cs->predictor &= 0xFF80; /* sign extension */ if(cs->predictor & 0x8000) cs->predictor -= 0x10000; CLAMP_TO_SHORT(cs->predictor); cs->step_index = (*src++) & 0x7F; if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index); if (cs->step_index > 88) cs->step_index = 88; cs->step = step_table[cs->step_index]; if (st && channel) samples++; *samples++ = cs->predictor; samples += st; for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */ *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F); samples += avctx->channels; *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F); samples += avctx->channels; src ++; } if(st) { /* handle stereo interlacing */ c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */ if(channel == 0) { /* wait for the other packet before outputing anything */ *data_size = 0; return src - buf; } } break; case CODEC_ID_ADPCM_IMA_WAV: if (buf_size > BLKSIZE) { if (avctx->block_align != 0) buf_size = avctx->block_align; else buf_size = BLKSIZE; } n = buf_size - 4 * avctx->channels; cs = &(c->status[0]); cs->predictor = (*src++) & 0x0FF; cs->predictor |= ((*src++) << 8) & 0x0FF00; if(cs->predictor & 0x8000) cs->predictor -= 0x10000; CLAMP_TO_SHORT(cs->predictor); *samples++ = cs->predictor; cs->step_index = *src++; if (cs->step_index < 0) cs->step_index = 0; if (cs->step_index > 88) cs->step_index = 88; if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */ if (st) { cs = &(c->status[1]); cs->predictor = (*src++) & 0x0FF; cs->predictor |= ((*src++) << 8) & 0x0FF00; if(cs->predictor & 0x8000) cs->predictor -= 0x10000; CLAMP_TO_SHORT(cs->predictor); *samples++ = cs->predictor; cs->step_index = *src++; if (cs->step_index < 0) cs->step_index = 0; if (cs->step_index > 88) cs->step_index = 88; src++; /* unused */ } cs = &(c->status[0]); for(m=3; n>0; n--, m--) { *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F); if (st) *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F); *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F); if (st) *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F); src ++; if (st && !m) { m=3; src+=4; } } break; case CODEC_ID_ADPCM_MS: if (buf_size > BLKSIZE) { if (avctx->block_align != 0) buf_size = avctx->block_align; else buf_size = BLKSIZE; } n = buf_size - 7 * avctx->channels; if (n < 0) return -1; block_predictor[0] = (*src++); /* should be bound */ block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0])); block_predictor[1] = 0; if (st) block_predictor[1] = (*src++); block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1])); c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); if (c->status[0].idelta & 0x08000) c->status[0].idelta -= 0x10000; src+=2; if (st) c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); if (st && c->status[1].idelta & 0x08000) c->status[1].idelta |= 0xFFFF0000; if (st) src+=2; c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]]; c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]]; c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]]; c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]]; c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); src+=2; if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); if (st) src+=2; c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); src+=2; if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00)); if (st) src+=2; *samples++ = c->status[0].sample1; if (st) *samples++ = c->status[1].sample1; *samples++ = c->status[0].sample2; if (st) *samples++ = c->status[1].sample2; for(;n>0;n--) { *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F); *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F); src ++; } break; default: *data_size = 0; return -1; } *data_size = (UINT8 *)samples - (UINT8 *)data; return src - buf;}#define ADPCM_CODEC(id, name) \AVCodec name ## _encoder = { \ #name, \ CODEC_TYPE_AUDIO, \ id, \ sizeof(ADPCMContext), \ adpcm_encode_init, \ adpcm_encode_frame, \ adpcm_encode_close, \ NULL, \}; \AVCodec name ## _decoder = { \ #name, \ CODEC_TYPE_AUDIO, \ id, \ sizeof(ADPCMContext), \ adpcm_decode_init, \ NULL, \ NULL, \ adpcm_decode_frame, \};ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);#undef ADPCM_CODEC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -