• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/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 
00022 #include "libavutil/intreadwrite.h"
00023 #include "avcodec.h"
00024 #include "adx.h"
00025 
00035 /* 18 bytes <-> 32 samples */
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     /* -8..+7 */
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; /* 0x80000000 + sample start - 4 */
00084         unsigned char unknown1[3]; /* 03 12 04 */
00085         unsigned char channel; /* 1 or 2 */
00086         uint32_t freq;
00087         uint32_t size;
00088         uint32_t unknown2; /* 01 f4 03 00 */
00089         uint32_t unknown3; /* 00 00 00 00 */
00090         uint32_t unknown4; /* 00 00 00 00 */
00091 
00092     /* if loop
00093         unknown3 00 15 00 01
00094         unknown4 00 00 00 01
00095         long loop_start_sample;
00096         long loop_start_byte;
00097         long loop_end_sample;
00098         long loop_end_byte;
00099         long
00100     */
00101     } adxhdr; /* big endian */
00102     /* offset-6 "(c)CRI" */
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); /* FIXME: set after */
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; /* only stereo or mono =) */
00119     avctx->frame_size = 32;
00120 
00121     avctx->coded_frame= avcodec_alloc_frame();
00122     avctx->coded_frame->key_frame= 1;
00123 
00124 //    avctx->bit_rate = avctx->sample_rate*avctx->channels*18*8/32;
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     input data size =
00148     ffmpeg.c: do_audio_out()
00149     frame_bytes = enc->frame_size * 2 * enc->channels;
00150 */
00151 
00152 //    printf("sz=%d ",buf_size); fflush(stdout);
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     AVMEDIA_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 = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
00196     .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
00197 };

Generated on Fri Sep 16 2011 17:17:33 for FFmpeg by  doxygen 1.7.1