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