00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "adx.h"
00023
00033
00034
00035 static void adx_encode(unsigned char *adx,const short *wav,PREV *prev)
00036 {
00037 int scale;
00038 int i;
00039 int s0,s1,s2,d;
00040 int max=0;
00041 int min=0;
00042 int data[32];
00043
00044 s1 = prev->s1;
00045 s2 = prev->s2;
00046 for(i=0;i<32;i++) {
00047 s0 = wav[i];
00048 d = ((s0<<14) - SCALE1*s1 + SCALE2*s2)/BASEVOL;
00049 data[i]=d;
00050 if (max<d) max=d;
00051 if (min>d) min=d;
00052 s2 = s1;
00053 s1 = s0;
00054 }
00055 prev->s1 = s1;
00056 prev->s2 = s2;
00057
00058
00059
00060 if (max==0 && min==0) {
00061 memset(adx,0,18);
00062 return;
00063 }
00064
00065 if (max/7>-min/8) scale = max/7;
00066 else scale = -min/8;
00067
00068 if (scale==0) scale=1;
00069
00070 AV_WB16(adx, scale);
00071
00072 for(i=0;i<16;i++) {
00073 adx[i+2] = ((data[i*2]/scale)<<4) | ((data[i*2+1]/scale)&0xf);
00074 }
00075 }
00076
00077 static int adx_encode_header(AVCodecContext *avctx,unsigned char *buf,size_t bufsize)
00078 {
00079 #if 0
00080 struct {
00081 uint32_t offset;
00082 unsigned char unknown1[3];
00083 unsigned char channel;
00084 uint32_t freq;
00085 uint32_t size;
00086 uint32_t unknown2;
00087 uint32_t unknown3;
00088 uint32_t unknown4;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 } adxhdr;
00100
00101 #endif
00102 AV_WB32(buf+0x00,0x80000000|0x20);
00103 AV_WB32(buf+0x04,0x03120400|avctx->channels);
00104 AV_WB32(buf+0x08,avctx->sample_rate);
00105 AV_WB32(buf+0x0c,0);
00106 AV_WB32(buf+0x10,0x01040300);
00107 AV_WB32(buf+0x14,0x00000000);
00108 AV_WB32(buf+0x18,0x00000000);
00109 memcpy(buf+0x1c,"\0\0(c)CRI",8);
00110 return 0x20+4;
00111 }
00112
00113 static int adx_encode_init(AVCodecContext *avctx)
00114 {
00115 if (avctx->channels > 2)
00116 return -1;
00117 avctx->frame_size = 32;
00118
00119 avctx->coded_frame= avcodec_alloc_frame();
00120 avctx->coded_frame->key_frame= 1;
00121
00122
00123
00124 av_log(avctx, AV_LOG_DEBUG, "adx encode init\n");
00125
00126 return 0;
00127 }
00128
00129 static int adx_encode_close(AVCodecContext *avctx)
00130 {
00131 av_freep(&avctx->coded_frame);
00132
00133 return 0;
00134 }
00135
00136 static int adx_encode_frame(AVCodecContext *avctx,
00137 uint8_t *frame, int buf_size, void *data)
00138 {
00139 ADXContext *c = avctx->priv_data;
00140 const short *samples = data;
00141 unsigned char *dst = frame;
00142 int rest = avctx->frame_size;
00143
00144
00145
00146
00147
00148
00149
00150
00151 if (!c->header_parsed) {
00152 int hdrsize = adx_encode_header(avctx,dst,buf_size);
00153 dst+=hdrsize;
00154 c->header_parsed = 1;
00155 }
00156
00157 if (avctx->channels==1) {
00158 while(rest>=32) {
00159 adx_encode(dst,samples,c->prev);
00160 dst+=18;
00161 samples+=32;
00162 rest-=32;
00163 }
00164 } else {
00165 while(rest>=32*2) {
00166 short tmpbuf[32*2];
00167 int i;
00168
00169 for(i=0;i<32;i++) {
00170 tmpbuf[i] = samples[i*2];
00171 tmpbuf[i+32] = samples[i*2+1];
00172 }
00173
00174 adx_encode(dst,tmpbuf,c->prev);
00175 adx_encode(dst+18,tmpbuf+32,c->prev+1);
00176 dst+=18*2;
00177 samples+=32*2;
00178 rest-=32*2;
00179 }
00180 }
00181 return dst-frame;
00182 }
00183
00184 AVCodec adpcm_adx_encoder = {
00185 "adpcm_adx",
00186 CODEC_TYPE_AUDIO,
00187 CODEC_ID_ADPCM_ADX,
00188 sizeof(ADXContext),
00189 adx_encode_init,
00190 adx_encode_frame,
00191 adx_encode_close,
00192 NULL,
00193 };