Mercurial > libavcodec.hg
comparison adpcm.c @ 2794:ed1ab1566353 libavcodec
Add Yamaha ADPCM encoding/decoding patch by (Vidar Madsen: vidarino, gmail com)
author | michael |
---|---|
date | Fri, 15 Jul 2005 06:51:36 +0000 |
parents | ba8ecddf5598 |
children | de03cac6f7c2 |
comparison
equal
deleted
inserted
replaced
2793:ae85da6c5c2f | 2794:ed1ab1566353 |
---|---|
115 /*3*/ { -1, -1, 2, 4 }, | 115 /*3*/ { -1, -1, 2, 4 }, |
116 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 }, | 116 /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 }, |
117 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 } | 117 /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 } |
118 }; | 118 }; |
119 | 119 |
120 static const int yamaha_indexscale[] = { | |
121 230, 230, 230, 230, 307, 409, 512, 614, | |
122 230, 230, 230, 230, 307, 409, 512, 614 | |
123 }; | |
124 | |
125 static const int yamaha_difflookup[] = { | |
126 1, 3, 5, 7, 9, 11, 13, 15, | |
127 -1, -3, -5, -7, -9, -11, -13, -15 | |
128 }; | |
129 | |
120 /* end of tables */ | 130 /* end of tables */ |
121 | 131 |
122 typedef struct ADPCMChannelStatus { | 132 typedef struct ADPCMChannelStatus { |
123 int predictor; | 133 int predictor; |
124 short int step_index; | 134 short int step_index; |
164 /* seems frame_size isn't taken into account... have to buffer the samples :-( */ | 174 /* seems frame_size isn't taken into account... have to buffer the samples :-( */ |
165 break; | 175 break; |
166 case CODEC_ID_ADPCM_MS: | 176 case CODEC_ID_ADPCM_MS: |
167 avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */ | 177 avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */ |
168 /* and we have 7 bytes per channel overhead */ | 178 /* and we have 7 bytes per channel overhead */ |
179 avctx->block_align = BLKSIZE; | |
180 break; | |
181 case CODEC_ID_ADPCM_YAMAHA: | |
182 avctx->frame_size = BLKSIZE * avctx->channels; | |
169 avctx->block_align = BLKSIZE; | 183 avctx->block_align = BLKSIZE; |
170 break; | 184 break; |
171 default: | 185 default: |
172 return -1; | 186 return -1; |
173 break; | 187 break; |
256 | 270 |
257 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; | 271 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; |
258 if (c->idelta < 16) c->idelta = 16; | 272 if (c->idelta < 16) c->idelta = 16; |
259 | 273 |
260 return nibble; | 274 return nibble; |
275 } | |
276 | |
277 static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample) | |
278 { | |
279 int i1 = 0, j1; | |
280 | |
281 if(!c->step) { | |
282 c->predictor = 0; | |
283 c->step = 127; | |
284 } | |
285 j1 = sample - c->predictor; | |
286 | |
287 j1 = (j1 * 8) / c->step; | |
288 i1 = abs(j1) / 2; | |
289 if (i1 > 7) | |
290 i1 = 7; | |
291 if (j1 < 0) | |
292 i1 += 8; | |
293 | |
294 c->predictor = c->predictor + ((c->step * yamaha_difflookup[i1]) / 8); | |
295 CLAMP_TO_SHORT(c->predictor); | |
296 c->step = (c->step * yamaha_indexscale[i1]) >> 8; | |
297 c->step = clip(c->step, 127, 24567); | |
298 | |
299 return i1; | |
261 } | 300 } |
262 | 301 |
263 static int adpcm_encode_frame(AVCodecContext *avctx, | 302 static int adpcm_encode_frame(AVCodecContext *avctx, |
264 unsigned char *frame, int buf_size, void *data) | 303 unsigned char *frame, int buf_size, void *data) |
265 { | 304 { |
360 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4; | 399 nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4; |
361 nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++); | 400 nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++); |
362 *dst++ = nibble; | 401 *dst++ = nibble; |
363 } | 402 } |
364 break; | 403 break; |
404 case CODEC_ID_ADPCM_YAMAHA: | |
405 n = avctx->frame_size / 2; | |
406 for (; n>0; n--) { | |
407 for(i = 0; i < avctx->channels; i++) { | |
408 int nibble; | |
409 nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]) << 4; | |
410 nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]); | |
411 *dst++ = nibble; | |
412 } | |
413 samples += 2 * avctx->channels; | |
414 } | |
415 break; | |
365 default: | 416 default: |
366 return -1; | 417 return -1; |
367 } | 418 } |
368 return dst - frame; | 419 return dst - frame; |
369 } | 420 } |
459 c->step = 32767; | 510 c->step = 32767; |
460 | 511 |
461 CLAMP_TO_SHORT(predictor); | 512 CLAMP_TO_SHORT(predictor); |
462 c->predictor = predictor; | 513 c->predictor = predictor; |
463 return (short)predictor; | 514 return (short)predictor; |
515 } | |
516 | |
517 static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble) | |
518 { | |
519 if(!c->step) { | |
520 c->predictor = 0; | |
521 c->step = 127; | |
522 } | |
523 | |
524 c->predictor += (c->step * yamaha_difflookup[nibble]) / 8; | |
525 CLAMP_TO_SHORT(c->predictor); | |
526 c->step = (c->step * yamaha_indexscale[nibble]) >> 8; | |
527 c->step = clip(c->step, 127, 24567); | |
528 return c->predictor; | |
464 } | 529 } |
465 | 530 |
466 static void xa_decode(short *out, const unsigned char *in, | 531 static void xa_decode(short *out, const unsigned char *in, |
467 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc) | 532 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc) |
468 { | 533 { |
976 // src += get_bits_count(&gb)*8; | 1041 // src += get_bits_count(&gb)*8; |
977 src += size; | 1042 src += size; |
978 | 1043 |
979 break; | 1044 break; |
980 } | 1045 } |
1046 case CODEC_ID_ADPCM_YAMAHA: | |
1047 while (src < buf + buf_size) { | |
1048 if (st) { | |
1049 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0], | |
1050 (src[0] >> 4) & 0x0F); | |
1051 *samples++ = adpcm_yamaha_expand_nibble(&c->status[1], | |
1052 src[0] & 0x0F); | |
1053 } else { | |
1054 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0], | |
1055 (src[0] >> 4) & 0x0F); | |
1056 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0], | |
1057 src[0] & 0x0F); | |
1058 } | |
1059 src++; | |
1060 } | |
1061 break; | |
981 default: | 1062 default: |
982 return -1; | 1063 return -1; |
983 } | 1064 } |
984 *data_size = (uint8_t *)samples - (uint8_t *)data; | 1065 *data_size = (uint8_t *)samples - (uint8_t *)data; |
985 return src - buf; | 1066 return src - buf; |
1033 ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa); | 1114 ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa); |
1034 ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx); | 1115 ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx); |
1035 ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea); | 1116 ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea); |
1036 ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct); | 1117 ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct); |
1037 ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf); | 1118 ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf); |
1119 ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha); | |
1038 | 1120 |
1039 #undef ADPCM_CODEC | 1121 #undef ADPCM_CODEC |