adxenc.c

Go to the documentation of this file.
00001 /*
00002  * ADX ADPCM codecs
00003  * Copyright (c) 2001,2003 BERO
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "avcodec.h"
00022 #include "adx.h"
00023 
00024 /**
00025  * @file adx.c
00026  * SEGA CRI adx codecs.
00027  *
00028  * Reference documents:
00029  * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
00030  * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
00031  */
00032 
00033 /* 18 bytes <-> 32 samples */
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     /* -8..+7 */
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; /* 0x80000000 + sample start - 4 */
00082         unsigned char unknown1[3]; /* 03 12 04 */
00083         unsigned char channel; /* 1 or 2 */
00084         uint32_t freq;
00085         uint32_t size;
00086         uint32_t unknown2; /* 01 f4 03 00 */
00087         uint32_t unknown3; /* 00 00 00 00 */
00088         uint32_t unknown4; /* 00 00 00 00 */
00089 
00090     /* if loop
00091         unknown3 00 15 00 01
00092         unknown4 00 00 00 01
00093         long loop_start_sample;
00094         long loop_start_byte;
00095         long loop_end_sample;
00096         long loop_end_byte;
00097         long
00098     */
00099     } adxhdr; /* big endian */
00100     /* offset-6 "(c)CRI" */
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); /* FIXME: set after */
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 av_cold int adx_encode_init(AVCodecContext *avctx)
00114 {
00115     if (avctx->channels > 2)
00116         return -1; /* only stereo or mono =) */
00117     avctx->frame_size = 32;
00118 
00119     avctx->coded_frame= avcodec_alloc_frame();
00120     avctx->coded_frame->key_frame= 1;
00121 
00122 //    avctx->bit_rate = avctx->sample_rate*avctx->channels*18*8/32;
00123 
00124     av_log(avctx, AV_LOG_DEBUG, "adx encode init\n");
00125 
00126     return 0;
00127 }
00128 
00129 static av_cold 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     input data size =
00146     ffmpeg.c: do_audio_out()
00147     frame_bytes = enc->frame_size * 2 * enc->channels;
00148 */
00149 
00150 //    printf("sz=%d ",buf_size); fflush(stdout);
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     .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX"),
00194 };

Generated on Thu Aug 28 16:44:15 2008 for libextractor by  doxygen 1.5.1