adx.c
资源名称:tcpmp.rar [点击查看]
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:9k
源码类别:
Windows CE
开发平台:
C/C++
- /*
- * ADX ADPCM codecs
- * Copyright (c) 2001,2003 BERO
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- #include "avcodec.h"
- /**
- * @file adx.c
- * SEGA CRI adx codecs.
- *
- * Reference documents:
- * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
- * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
- */
- typedef struct {
- int s1,s2;
- } PREV;
- typedef struct {
- PREV prev[2];
- int header_parsed;
- unsigned char dec_temp[18*2];
- unsigned short enc_temp[32*2];
- int in_temp;
- } ADXContext;
- //#define BASEVOL 0x11e0
- #define BASEVOL 0x4000
- #define SCALE1 0x7298
- #define SCALE2 0x3350
- #define CLIP(s) if (s>32767) s=32767; else if (s<-32768) s=-32768
- /* 18 bytes <-> 32 samples */
- #ifdef CONFIG_ENCODERS
- static void adx_encode(unsigned char *adx,const short *wav,PREV *prev)
- {
- int scale;
- int i;
- int s0,s1,s2,d;
- int max=0;
- int min=0;
- int data[32];
- s1 = prev->s1;
- s2 = prev->s2;
- for(i=0;i<32;i++) {
- s0 = wav[i];
- d = ((s0<<14) - SCALE1*s1 + SCALE2*s2)/BASEVOL;
- data[i]=d;
- if (max<d) max=d;
- if (min>d) min=d;
- s2 = s1;
- s1 = s0;
- }
- prev->s1 = s1;
- prev->s2 = s2;
- /* -8..+7 */
- if (max==0 && min==0) {
- memset(adx,0,18);
- return;
- }
- if (max/7>-min/8) scale = max/7;
- else scale = -min/8;
- if (scale==0) scale=1;
- adx[0] = scale>>8;
- adx[1] = scale;
- for(i=0;i<16;i++) {
- adx[i+2] = ((data[i*2]/scale)<<4) | ((data[i*2+1]/scale)&0xf);
- }
- }
- #endif //CONFIG_ENCODERS
- static void adx_decode(short *out,const unsigned char *in,PREV *prev)
- {
- int scale = ((in[0]<<8)|(in[1]));
- int i;
- int s0,s1,s2,d;
- // printf("%x ",scale);
- in+=2;
- s1 = prev->s1;
- s2 = prev->s2;
- for(i=0;i<16;i++) {
- d = in[i];
- // d>>=4; if (d&8) d-=16;
- d = ((signed char)d >> 4);
- s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
- CLIP(s0);
- *out++=s0;
- s2 = s1;
- s1 = s0;
- d = in[i];
- //d&=15; if (d&8) d-=16;
- d = ((signed char)(d<<4) >> 4);
- s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
- CLIP(s0);
- *out++=s0;
- s2 = s1;
- s1 = s0;
- }
- prev->s1 = s1;
- prev->s2 = s2;
- }
- static void adx_decode_stereo(short *out,const unsigned char *in,PREV *prev)
- {
- short tmp[32*2];
- int i;
- adx_decode(tmp ,in ,prev);
- adx_decode(tmp+32,in+18,prev+1);
- for(i=0;i<32;i++) {
- out[i*2] = tmp[i];
- out[i*2+1] = tmp[i+32];
- }
- }
- #ifdef CONFIG_ENCODERS
- static void write_long(unsigned char *p,uint32_t v)
- {
- p[0] = v>>24;
- p[1] = v>>16;
- p[2] = v>>8;
- p[3] = v;
- }
- static int adx_encode_header(AVCodecContext *avctx,unsigned char *buf,size_t bufsize)
- {
- #if 0
- struct {
- uint32_t offset; /* 0x80000000 + sample start - 4 */
- unsigned char unknown1[3]; /* 03 12 04 */
- unsigned char channel; /* 1 or 2 */
- uint32_t freq;
- uint32_t size;
- uint32_t unknown2; /* 01 f4 03 00 */
- uint32_t unknown3; /* 00 00 00 00 */
- uint32_t unknown4; /* 00 00 00 00 */
- /* if loop
- unknown3 00 15 00 01
- unknown4 00 00 00 01
- long loop_start_sample;
- long loop_start_byte;
- long loop_end_sample;
- long loop_end_byte;
- long
- */
- } adxhdr; /* big endian */
- /* offset-6 "(c)CRI" */
- #endif
- write_long(buf+0x00,0x80000000|0x20);
- write_long(buf+0x04,0x03120400|avctx->channels);
- write_long(buf+0x08,avctx->sample_rate);
- write_long(buf+0x0c,0); /* FIXME: set after */
- write_long(buf+0x10,0x01040300);
- write_long(buf+0x14,0x00000000);
- write_long(buf+0x18,0x00000000);
- memcpy(buf+0x1c,"