📄 wtaudioplay.c
字号:
I6400WriteMem(0x2020,dwData&0xFFFF); //user_iic_ctrl Lower 16bits write dwAddress=0x2020; while(1) { WORD wLower16=I6400ReadMem(0x2020); if((wLower16&0x0002)==0) break; } // pUserIIC++; // } I6400WriteMem(0x2020,0); return;}void I6400I2CInit(int iic_select){ switch (iic_select) { case 0: I6400WriteUserIIC(BT829_320x240,0x88); printf("BT829_320X240\n"); break; case 1: I6400WriteUserIIC(BT829_320x240,0x88); printf("---\n"); sleep(2); I6400WriteUserIIC(BT829p_640x480,0x88); printf("BT829_640x480(829-PAL|||6400-NTSC\n"); break; case 2: I6400WriteUserIIC(BT829_720x480,0x88); printf("BT829_720x480\n"); break; case 4: I6400WriteUserIIC(BT829_320x288,0x88); printf("BT829_320x288\n"); break; case 5: I6400WriteUserIIC(BT829_352x288,0x88); printf("BT829_352x288\n"); break; case 6: I6400WriteUserIIC(BT829_704x576,0x88); printf("BT829_704x576\n"); break; case 7: I6400WriteUserIIC(BT829_720x576,0x88); printf("BT829_720x576\n"); break; case 8: I6400WriteUserIIC(BT829p_640x480,0x88); printf("BT829p_640x480\n"); break; case 9: I6400WriteUserIIC(BT829_704x288,0x88); printf("BT829_704x288\n"); break; case 10: I6400WriteUserIIC(BT829_640x240,0x88); printf("BT829_640x240\n"); break; default: I6400WriteUserIIC(BT829_320x240,0x88); printf("BT829_320X240\n"); break; } return;}int Init829(void){ //int select; char buffer[30]; ReadValue(buffer,VIDEO_FORMAT,1); buffer[20]=0; printf(" requesting %s\n",buffer); if(strcmp(buffer,"PAL 352*288")==0) I6400I2CInit(5); else if(strcmp(buffer,"NTSC 320*240")==0) I6400I2CInit(0); else if(strcmp(buffer,"NTSC 640*480")==0) I6400I2CInit(1); else if(strcmp(buffer,"PAL 704*576")==0) I6400I2CInit(6); else if(strcmp(buffer,"PAL 720*576")==0) I6400I2CInit(7); else if(strcmp(buffer,"PAL 640*480")==0) I6400I2CInit(8); else if(strcmp(buffer,"PAL 704*288")==0) I6400I2CInit(9); else if(strcmp(buffer,"NTSC 640*240")==0) I6400I2CInit(10); else { printf("VIDEO FORMAT ERROR.USE DEFAULT:352x288"); I6400I2CInit(5); } printf("Initial BT829 I2C data finish!\n"); return 0;}//============================================void set_IME6400_audio(int Stereo){//sample rate:8Khz// output bit rate:Stereo 32Kbps// Mono 128Kbps if(Stereo) I6400WriteMem(0x200C,0x11); else{#ifdef _FWP16_ I6400WriteMem(0x200C,0x44);//uPCM#else _FWA16_ I6400WriteMem(0x200C,0x84);//ADPCM#endif } //I6400WriteMem(0x200E,0x0000); I6400WriteMem(0x200E,0x1000);//Enable Channel 0 audio }/////////////////////////////////////////////////////////////void StartEncoding(void){ char buf[30];//Audio set_IME6400_audio(0); //1 for stereo ,0 for mono ReadValue(buf,BITRATE_CONTROL,1); if(strncmp(buf,"Vbr",3)==0) { printf("BITRATE_CONTROL:VBR\n"); ReadValue(buf,VBR_INIT_QUALITY,1); if(atoi(buf)>31||atoi(buf)<1) { printf("VBR_INIT_QUALITY:%d\n",atoi(buf)); I6400WriteMem(0x2010,0xa); } else{ printf("VBR_INIT_QUALITY:%d\n",atoi(buf)); I6400WriteMem(0x2010,atoi(buf)); } } //Time Increment I6400WriteMem(0x2008,30000/12.5); //Start encoding I6400WriteReg(User4,0); //printf("g_i64Command=[%x:%x]\n",wComLower,wCfgUpper); I6400WriteMem(0x2002,0x0180); I6400WriteMem(0x2000,0x0a99); printf("Start 6400 Encoding successfully!\n"); }//*** auido decoder/encoder#define AUDIO_ENCODING_ULAW (1) /* ISDN u-law */#define AUDIO_ENCODING_ALAW (2) /* ISDN A-law */#define AUDIO_ENCODING_LINEAR (3) /* PCM 2's-complement (0-center) */#define AUDIO_ENCODING_ADPCM (4) /*IMA ADPCM *//* * g711.c * * u-law, A-law and linear PCM conversions. */#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */#define QUANT_MASK (0xf) /* Quantization field mask. */#define NSEGS (8) /* Number of A-law segments. */#define SEG_SHIFT (4) /* Left shift for segment number. */#define SEG_MASK (0x70) /* Segment field mask. */#define CLIP (8159)static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};static short seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF};static short seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF};/* copy from CCITT G.711 specifications */unsigned char _u2a[128] = { /* u- to A-law conversions */ 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128};unsigned char _a2u[128] = { /* A- to u-law conversions */ 1, 3, 5, 7, 9, 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 48, 49, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127};static intsearch( int val, short *table, int size){ int i; for (i = 0; i < size; i++) { if (val <= *table++) return (i); } return (size);}/* * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law * * linear2alaw() accepts an 16-bit integer and encodes it as A-law data. * * Linear Input Code Compressed Code * ------------------------ --------------- * 0000000wxyza 000wxyz * 0000001wxyza 001wxyz * 000001wxyzab 010wxyz * 00001wxyzabc 011wxyz * 0001wxyzabcd 100wxyz * 001wxyzabcde 101wxyz * 01wxyzabcdef 110wxyz * 1wxyzabcdefg 111wxyz * * For further information see John C. Bellamy's Digital Telephony, 1982, * John Wiley & Sons, pps 98-111 and 472-476. */unsigned charlinear2alaw( int pcm_val) /* 2's complement (16-bit range) */{ int mask; int seg; unsigned char aval; if (pcm_val >= 0) { mask = 0xD5; /* sign (7th) bit = 1 */ } else { mask = 0x55; /* sign bit = 0 */ pcm_val = -pcm_val - 8; } /* Convert the scaled magnitude to segment number. */ seg = search(pcm_val, seg_end, 8); /* Combine the sign, segment, and quantization bits. */ if (seg >= 8) /* out of range, return maximum value. */ return (0x7F ^ mask); else { aval = seg << SEG_SHIFT; if (seg < 2) aval |= (pcm_val >> 4) & QUANT_MASK; else aval |= (pcm_val >> (seg + 3)) & QUANT_MASK; return (aval ^ mask); }}/* * alaw2linear() - Convert an A-law value to 16-bit linear PCM * */intalaw2linear( unsigned char a_val){ int t; int seg; a_val ^= 0x55; t = (a_val & QUANT_MASK) << 4; seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT; switch (seg) { case 0: t += 8; break; case 1: t += 0x108; break; default: t += 0x108; t <<= seg - 1; } return ((a_val & SIGN_BIT) ? t : -t);}#define BIAS (0x84) /* Bias for linear code. *//* * linear2ulaw() - Convert a linear PCM value to u-law * * In order to simplify the encoding process, the original linear magnitude * is biased by adding 33 which shifts the encoding range from (0 - 8158) to * (33 - 8191). The result can be seen in the following encoding table: * * Biased Linear Input Code Compressed Code * ------------------------ --------------- * 00000001wxyza 000wxyz * 0000001wxyzab 001wxyz * 000001wxyzabc 010wxyz * 00001wxyzabcd 011wxyz * 0001wxyzabcde 100wxyz * 001wxyzabcdef 101wxyz * 01wxyzabcdefg 110wxyz * 1wxyzabcdefgh 111wxyz * * Each biased linear code has a leading 1 which identifies the segment * number. The value of the segment number is equal to 7 minus the number * of leading 0's. The quantization interval is directly available as the * four bits wxyz. * The trailing bits (a - h) are ignored. * * Ordinarily the complement of the resulting code word is used for * transmission, and so the code word is complemented before it is returned. * * For further information see John C. Bellamy's Digital Telephony, 1982, * John Wiley & Sons, pps 98-111 and 472-476. */static inline unsigned charlinear2ulaw(short pcm_val) /* 2's complement (16-bit range) */{ short mask; short seg; unsigned char uval; /* Get the sign and the magnitude of the value. */ pcm_val = pcm_val >> 2; if (pcm_val < 0) { pcm_val = -pcm_val; mask = 0x7F; } else { mask = 0xFF; } if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */ pcm_val += (BIAS >> 2); /* Convert the scaled magnitude to segment number. */ seg = search(pcm_val, seg_uend, 8); /* * Combine the sign, segment, quantization bits; * and complement the code word. */ if (seg >= 8) /* out of range, return maximum value. */ return (unsigned char) (0x7F ^ mask); else { uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF); return (uval ^ mask); }}/* * ulaw2linear() - Convert a u-law value to 16-bit linear PCM * * First, a biased linear code is derived from the code word. An unbiased * output can then be obtained by subtracting 33 from the biased code. * * Note that this function expects to be passed the complement of the * original code word. This is in keeping with ISDN conventions. */intulaw2linear( unsigned char u_val){ int t; /* Complement to obtain normal u-law value. */ u_val = ~u_val; /* * Extract and bias the quantization bits. Then * shift up by the segment number and subtract out the bias. */ t = ((u_val & QUANT_MASK) << 3) + BIAS; t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));}/* A-law to u-law conversion */unsigned charalaw2ulaw( unsigned char aval){ aval &= 0xff; return ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) : (0x7F ^ _a2u[aval ^ 0x55]));}/* u-law to A-law conversion */unsigned charulaw2alaw( unsigned char uval){ uval &= 0xff; return ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) : (0x55 ^ (_u2a[0x7F ^ uval] - 1)));}//*******************************************/** IMA ADPCM*/#define CLAMP_TO_SHORT(value) \if (value > 32767) \ value = 32767; \else if (value < -32768) \ value = -32768; \/* Intel ADPCM step variation table */static int indexTable[16] = { -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8,};static int stepsizeTable[89] = { 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767};typedef struct adpcm_state{ int index; int valprev;}_ADPCM,*pADPCM;voidadpcm_coder(short *indata, unsigned char *outdata, int len, struct adpcm_state *state){ short *inp; /* Input buffer pointer */ signed char *outp; /* output buffer pointer */ int val; /* Current input sample value */ int sign; /* Current adpcm sign bit */ int delta; /* Current adpcm output value */ int diff; /* Difference between val and valprev */ int step; /* Stepsize */ int valpred; /* Predicted output value */ int vpdiff; /* Current change to valpred */ int index; /* Current step change index */ int outputbuffer; /* place to keep previous 4-bit value */ int bufferstep; /* toggle between outputbuffer/output */ outp = (signed char *)outdata; inp = indata; valpred = state->valprev; index = state->index; step = stepsizeTable[index]; bufferstep = 1; for ( ; len > 0 ; len-- ) { val = *inp++; /* Step 1 - compute difference with previous value */ diff = val - valpred; sign = (diff < 0) ? 8 : 0; if ( sign ) diff = (-diff); /* Step 2 - Divide and clamp */ /* Note: ** This code *approximately* computes: ** delta = diff*4/step; ** vpdiff = (delta+0.5)*step/4; ** but in shift step bits are dropped. The net result of this is ** that even if you have fast mul/div hardware you cannot put it to ** good use since the fixup would be too expensive. */ delta = 0; vpdiff = (step >> 3); if ( diff >= step ) { delta = 4; diff -= step; vpdiff += step; } step >>= 1; if ( diff >= step ) { delta |= 2; diff -= step; vpdiff += step; } step >>= 1; if ( diff >= step ) { delta |= 1; vpdiff += step; } /* Step 3 - Update previous value */ if ( sign ) valpred -= vpdiff; else valpred += vpdiff; /* Step 4 - Clamp previous value to 16 bits */ if ( valpred > 32767 ) valpred = 32767; else if ( valpred < -32768 ) valpred = -32768; /* Step 5 - Assemble value, update index and step values */ delta |= sign; index += indexTable[delta]; if ( index < 0 ) index = 0; if ( index > 88 ) index = 88; step = stepsizeTable[index]; /* Step 6 - Output value */ if ( bufferstep ) { outputbuffer = (delta & 0x0f) ; } else { *outp++ = ((delta << 4) & 0xf0) | outputbuffer; } bufferstep = !bufferstep; } /* Output last step, if needed */ if ( !bufferstep ) *outp++ = outputbuffer; state->valprev = valpred; state->index = index;}voidadpcm_decoder(char *indata, short *outdata,int len, struct adpcm_state* state) { signed char *inp; /* Input buffer pointer */ short *outp; /* output buffer pointer */ int sign; /* Current adpcm sign bit */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -