comparison adpcm.c @ 2979:bfabfdf9ce55 libavcodec

COSMETICS: tabs --> spaces, some prettyprinting
author diego
date Thu, 22 Dec 2005 01:10:11 +0000
parents ef2149182f1c
children 0b546eab515d
comparison
equal deleted inserted replaced
2978:403183bbb505 2979:bfabfdf9ce55
298 298
299 return i1; 299 return i1;
300 } 300 }
301 301
302 static int adpcm_encode_frame(AVCodecContext *avctx, 302 static int adpcm_encode_frame(AVCodecContext *avctx,
303 unsigned char *frame, int buf_size, void *data) 303 unsigned char *frame, int buf_size, void *data)
304 { 304 {
305 int n, i, st; 305 int n, i, st;
306 short *samples; 306 short *samples;
307 unsigned char *dst; 307 unsigned char *dst;
308 ADPCMContext *c = avctx->priv_data; 308 ADPCMContext *c = avctx->priv_data;
429 c->status[0].step_index = c->status[1].step_index = 0; 429 c->status[0].step_index = c->status[1].step_index = 0;
430 c->status[0].step = c->status[1].step = 0; 430 c->status[0].step = c->status[1].step = 0;
431 431
432 switch(avctx->codec->id) { 432 switch(avctx->codec->id) {
433 case CODEC_ID_ADPCM_CT: 433 case CODEC_ID_ADPCM_CT:
434 c->status[0].step = c->status[1].step = 511; 434 c->status[0].step = c->status[1].step = 511;
435 break; 435 break;
436 default: 436 default:
437 break; 437 break;
438 } 438 }
439 return 0; 439 return 0;
440 } 440 }
496 * quickly enough */ 496 * quickly enough */
497 diff = ((2 * delta + 1) * c->step) >> 3; 497 diff = ((2 * delta + 1) * c->step) >> 3;
498 predictor = c->predictor; 498 predictor = c->predictor;
499 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */ 499 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
500 if(sign) 500 if(sign)
501 predictor = ((predictor * 254) >> 8) - diff; 501 predictor = ((predictor * 254) >> 8) - diff;
502 else 502 else
503 predictor = ((predictor * 254) >> 8) + diff; 503 predictor = ((predictor * 254) >> 8) + diff;
504 /* calculate new step and clamp it to range 511..32767 */ 504 /* calculate new step and clamp it to range 511..32767 */
505 new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8; 505 new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
506 c->step = new_step; 506 c->step = new_step;
507 if(c->step < 511) 507 if(c->step < 511)
508 c->step = 511; 508 c->step = 511;
509 if(c->step > 32767) 509 if(c->step > 32767)
510 c->step = 32767; 510 c->step = 32767;
511 511
512 CLAMP_TO_SHORT(predictor); 512 CLAMP_TO_SHORT(predictor);
513 c->predictor = predictor; 513 c->predictor = predictor;
514 return (short)predictor; 514 return (short)predictor;
515 } 515 }
610 nibble = last_byte & 0x0F; \ 610 nibble = last_byte & 0x0F; \
611 decode_top_nibble_next = 1; \ 611 decode_top_nibble_next = 1; \
612 } 612 }
613 613
614 static int adpcm_decode_frame(AVCodecContext *avctx, 614 static int adpcm_decode_frame(AVCodecContext *avctx,
615 void *data, int *data_size, 615 void *data, int *data_size,
616 uint8_t *buf, int buf_size) 616 uint8_t *buf, int buf_size)
617 { 617 {
618 ADPCMContext *c = avctx->priv_data; 618 ADPCMContext *c = avctx->priv_data;
619 ADPCMChannelStatus *cs; 619 ADPCMChannelStatus *cs;
620 int n, m, channel, i; 620 int n, m, channel, i;
621 int block_predictor[2]; 621 int block_predictor[2];
699 cs->predictor |= (*src++) << 8; 699 cs->predictor |= (*src++) << 8;
700 if(cs->predictor & 0x8000) 700 if(cs->predictor & 0x8000)
701 cs->predictor -= 0x10000; 701 cs->predictor -= 0x10000;
702 CLAMP_TO_SHORT(cs->predictor); 702 CLAMP_TO_SHORT(cs->predictor);
703 703
704 // XXX: is this correct ??: *samples++ = cs->predictor; 704 // XXX: is this correct ??: *samples++ = cs->predictor;
705 705
706 cs->step_index = *src++; 706 cs->step_index = *src++;
707 if (cs->step_index < 0) cs->step_index = 0; 707 if (cs->step_index < 0) cs->step_index = 0;
708 if (cs->step_index > 88) cs->step_index = 88; 708 if (cs->step_index > 88) cs->step_index = 88;
709 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */ 709 if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
710 } 710 }
711 711
712 for(m=4; src < (buf + buf_size);) { 712 for(m=4; src < (buf + buf_size);) {
713 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3); 713 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
714 if (st) 714 if (st)
715 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3); 715 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
716 *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3); 716 *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
717 if (st) { 717 if (st) {
718 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3); 718 *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
719 if (!--m) { 719 if (!--m) {
720 m=4; 720 m=4;
721 src+=4; 721 src+=4;
722 } 722 }
723 } 723 }
724 src++; 724 src++;
725 } 725 }
726 break; 726 break;
727 case CODEC_ID_ADPCM_4XM: 727 case CODEC_ID_ADPCM_4XM:
728 cs = &(c->status[0]); 728 cs = &(c->status[0]);
729 c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2; 729 c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
730 if(st){ 730 if(st){
737 if (cs->step_index < 0) cs->step_index = 0; 737 if (cs->step_index < 0) cs->step_index = 0;
738 if (cs->step_index > 88) cs->step_index = 88; 738 if (cs->step_index > 88) cs->step_index = 88;
739 739
740 m= (buf_size - (src - buf))>>st; 740 m= (buf_size - (src - buf))>>st;
741 for(i=0; i<m; i++) { 741 for(i=0; i<m; i++) {
742 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4); 742 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
743 if (st) 743 if (st)
744 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4); 744 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
745 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4); 745 *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
746 if (st) 746 if (st)
747 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4); 747 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
748 } 748 }
749 749
750 src += m<<st; 750 src += m<<st;
751 751
752 break; 752 break;
753 case CODEC_ID_ADPCM_MS: 753 case CODEC_ID_ADPCM_MS:
956 (*src >> 4) & 0x0F, 3); 956 (*src >> 4) & 0x0F, 3);
957 src++; 957 src++;
958 } 958 }
959 break; 959 break;
960 case CODEC_ID_ADPCM_CT: 960 case CODEC_ID_ADPCM_CT:
961 while (src < buf + buf_size) { 961 while (src < buf + buf_size) {
962 if (st) { 962 if (st) {
963 *samples++ = adpcm_ct_expand_nibble(&c->status[0], 963 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
964 (src[0] >> 4) & 0x0F); 964 (src[0] >> 4) & 0x0F);
965 *samples++ = adpcm_ct_expand_nibble(&c->status[1], 965 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
966 src[0] & 0x0F); 966 src[0] & 0x0F);
968 *samples++ = adpcm_ct_expand_nibble(&c->status[0], 968 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
969 (src[0] >> 4) & 0x0F); 969 (src[0] >> 4) & 0x0F);
970 *samples++ = adpcm_ct_expand_nibble(&c->status[0], 970 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
971 src[0] & 0x0F); 971 src[0] & 0x0F);
972 } 972 }
973 src++; 973 src++;
974 } 974 }
975 break; 975 break;
976 case CODEC_ID_ADPCM_SWF: 976 case CODEC_ID_ADPCM_SWF:
977 { 977 {
978 GetBitContext gb; 978 GetBitContext gb;
979 const int *table; 979 const int *table;
980 int k0, signmask; 980 int k0, signmask;
981 int size = buf_size*8; 981 int size = buf_size*8;
982 982
983 init_get_bits(&gb, buf, size); 983 init_get_bits(&gb, buf, size);
984 984
985 // first frame, read bits & inital values 985 // first frame, read bits & inital values
986 if (!c->nb_bits) 986 if (!c->nb_bits)
987 { 987 {
988 c->nb_bits = get_bits(&gb, 2)+2; 988 c->nb_bits = get_bits(&gb, 2)+2;
989 // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits); 989 // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
990 } 990 }
991 991
992 table = swf_index_tables[c->nb_bits-2]; 992 table = swf_index_tables[c->nb_bits-2];
993 k0 = 1 << (c->nb_bits-2); 993 k0 = 1 << (c->nb_bits-2);
994 signmask = 1 << (c->nb_bits-1); 994 signmask = 1 << (c->nb_bits-1);
995 995
996 while (get_bits_count(&gb) <= size) 996 while (get_bits_count(&gb) <= size)
997 { 997 {
998 int i; 998 int i;
999 999
1000 c->nb_samples++; 1000 c->nb_samples++;
1001 // wrap around at every 4096 samples... 1001 // wrap around at every 4096 samples...
1002 if ((c->nb_samples & 0xfff) == 1) 1002 if ((c->nb_samples & 0xfff) == 1)
1003 { 1003 {
1004 for (i = 0; i <= st; i++) 1004 for (i = 0; i <= st; i++)
1005 { 1005 {
1006 *samples++ = c->status[i].predictor = get_sbits(&gb, 16); 1006 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
1007 c->status[i].step_index = get_bits(&gb, 6); 1007 c->status[i].step_index = get_bits(&gb, 6);
1008 } 1008 }
1009 } 1009 }
1010 1010
1011 // similar to IMA adpcm 1011 // similar to IMA adpcm
1012 for (i = 0; i <= st; i++) 1012 for (i = 0; i <= st; i++)
1013 { 1013 {
1014 int delta = get_bits(&gb, c->nb_bits); 1014 int delta = get_bits(&gb, c->nb_bits);
1015 int step = step_table[c->status[i].step_index]; 1015 int step = step_table[c->status[i].step_index];
1016 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4 1016 long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
1017 int k = k0; 1017 int k = k0;
1018 1018
1019 do { 1019 do {
1020 if (delta & k) 1020 if (delta & k)
1021 vpdiff += step; 1021 vpdiff += step;
1022 step >>= 1; 1022 step >>= 1;
1023 k >>= 1; 1023 k >>= 1;
1024 } while(k); 1024 } while(k);
1025 vpdiff += step; 1025 vpdiff += step;
1026 1026
1027 if (delta & signmask) 1027 if (delta & signmask)
1028 c->status[i].predictor -= vpdiff; 1028 c->status[i].predictor -= vpdiff;
1029 else 1029 else
1030 c->status[i].predictor += vpdiff; 1030 c->status[i].predictor += vpdiff;
1031 1031
1032 c->status[i].step_index += table[delta & (~signmask)]; 1032 c->status[i].step_index += table[delta & (~signmask)];
1033 1033
1034 c->status[i].step_index = clip(c->status[i].step_index, 0, 88); 1034 c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
1035 c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767); 1035 c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
1036 1036
1037 *samples++ = c->status[i].predictor; 1037 *samples++ = c->status[i].predictor;
1038 } 1038 }
1039 } 1039 }
1040 1040
1041 // src += get_bits_count(&gb)*8; 1041 // src += get_bits_count(&gb)*8;
1042 src += size; 1042 src += size;
1043 1043
1044 break; 1044 break;
1045 } 1045 }
1046 case CODEC_ID_ADPCM_YAMAHA: 1046 case CODEC_ID_ADPCM_YAMAHA:
1047 while (src < buf + buf_size) { 1047 while (src < buf + buf_size) {
1048 if (st) { 1048 if (st) {
1049 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0], 1049 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],