adxdec.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_decode(short *out,const unsigned char *in,PREV *prev)
00036 {
00037     int scale = AV_RB16(in);
00038     int i;
00039     int s0,s1,s2,d;
00040 
00041 //    printf("%x ",scale);
00042 
00043     in+=2;
00044     s1 = prev->s1;
00045     s2 = prev->s2;
00046     for(i=0;i<16;i++) {
00047         d = in[i];
00048         // d>>=4; if (d&8) d-=16;
00049         d = ((signed char)d >> 4);
00050         s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
00051         s2 = s1;
00052         s1 = av_clip_int16(s0);
00053         *out++=s1;
00054 
00055         d = in[i];
00056         //d&=15; if (d&8) d-=16;
00057         d = ((signed char)(d<<4) >> 4);
00058         s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
00059         s2 = s1;
00060         s1 = av_clip_int16(s0);
00061         *out++=s1;
00062     }
00063     prev->s1 = s1;
00064     prev->s2 = s2;
00065 
00066 }
00067 
00068 static void adx_decode_stereo(short *out,const unsigned char *in,PREV *prev)
00069 {
00070     short tmp[32*2];
00071     int i;
00072 
00073     adx_decode(tmp   ,in   ,prev);
00074     adx_decode(tmp+32,in+18,prev+1);
00075     for(i=0;i<32;i++) {
00076         out[i*2]   = tmp[i];
00077         out[i*2+1] = tmp[i+32];
00078     }
00079 }
00080 
00081 /* return data offset or 0 */
00082 static int adx_decode_header(AVCodecContext *avctx,const unsigned char *buf,size_t bufsize)
00083 {
00084     int offset;
00085 
00086     if (buf[0]!=0x80) return 0;
00087     offset = (AV_RB32(buf)^0x80000000)+4;
00088     if (bufsize<offset || memcmp(buf+offset-6,"(c)CRI",6)) return 0;
00089 
00090     avctx->channels    = buf[7];
00091     avctx->sample_rate = AV_RB32(buf+8);
00092     avctx->bit_rate    = avctx->sample_rate*avctx->channels*18*8/32;
00093 
00094     return offset;
00095 }
00096 
00097 static int adx_decode_frame(AVCodecContext *avctx,
00098                 void *data, int *data_size,
00099                 const uint8_t *buf0, int buf_size)
00100 {
00101     ADXContext *c = avctx->priv_data;
00102     short *samples = data;
00103     const uint8_t *buf = buf0;
00104     int rest = buf_size;
00105 
00106     if (!c->header_parsed) {
00107         int hdrsize = adx_decode_header(avctx,buf,rest);
00108         if (hdrsize==0) return -1;
00109         c->header_parsed = 1;
00110         buf  += hdrsize;
00111         rest -= hdrsize;
00112     }
00113 
00114     /* 18 bytes of data are expanded into 32*2 bytes of audio,
00115        so guard against buffer overflows */
00116     if(rest/18 > *data_size/64)
00117         rest = (*data_size/64) * 18;
00118 
00119     if (c->in_temp) {
00120         int copysize = 18*avctx->channels - c->in_temp;
00121         memcpy(c->dec_temp+c->in_temp,buf,copysize);
00122         rest -= copysize;
00123         buf  += copysize;
00124         if (avctx->channels==1) {
00125             adx_decode(samples,c->dec_temp,c->prev);
00126             samples += 32;
00127         } else {
00128             adx_decode_stereo(samples,c->dec_temp,c->prev);
00129             samples += 32*2;
00130         }
00131     }
00132     //
00133     if (avctx->channels==1) {
00134         while(rest>=18) {
00135             adx_decode(samples,buf,c->prev);
00136             rest-=18;
00137             buf+=18;
00138             samples+=32;
00139         }
00140     } else {
00141         while(rest>=18*2) {
00142             adx_decode_stereo(samples,buf,c->prev);
00143             rest-=18*2;
00144             buf+=18*2;
00145             samples+=32*2;
00146         }
00147     }
00148     //
00149     c->in_temp = rest;
00150     if (rest) {
00151         memcpy(c->dec_temp,buf,rest);
00152         buf+=rest;
00153     }
00154     *data_size = (uint8_t*)samples - (uint8_t*)data;
00155 //    printf("%d:%d ",buf-buf0,*data_size); fflush(stdout);
00156     return buf-buf0;
00157 }
00158 
00159 AVCodec adpcm_adx_decoder = {
00160     "adpcm_adx",
00161     CODEC_TYPE_AUDIO,
00162     CODEC_ID_ADPCM_ADX,
00163     sizeof(ADXContext),
00164     NULL,
00165     NULL,
00166     NULL,
00167     adx_decode_frame,
00168     .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX"),
00169 };
00170 

Generated on Sat Oct 11 19:44:27 2008 for libextractor by  doxygen 1.5.1