2007-11-01 20:38:15 +02:00
|
|
|
/*
|
|
|
|
* ADX ADPCM codecs
|
|
|
|
* Copyright (c) 2001,2003 BERO
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2007-11-01 20:38:15 +02:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2007-11-01 20:38:15 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2007-11-01 20:38:15 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2007-11-01 20:38:15 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2009-01-12 00:19:48 +02:00
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2007-11-01 20:38:15 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "adx.h"
|
2011-11-21 00:49:13 +03:00
|
|
|
#include "put_bits.h"
|
2007-11-01 20:38:15 +02:00
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2007-11-01 20:38:15 +02:00
|
|
|
* SEGA CRI adx codecs.
|
|
|
|
*
|
|
|
|
* Reference documents:
|
|
|
|
* http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
|
|
|
|
* adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
|
|
|
|
*/
|
|
|
|
|
2011-11-20 22:21:32 +03:00
|
|
|
static void adx_encode(ADXContext *c, unsigned char *adx, const short *wav,
|
2011-11-20 01:05:44 +03:00
|
|
|
ADXChannelState *prev)
|
2007-11-01 20:38:15 +02:00
|
|
|
{
|
2011-11-21 00:49:13 +03:00
|
|
|
PutBitContext pb;
|
2007-11-01 20:38:15 +02:00
|
|
|
int scale;
|
|
|
|
int i;
|
|
|
|
int s0,s1,s2,d;
|
|
|
|
int max=0;
|
|
|
|
int min=0;
|
|
|
|
int data[32];
|
|
|
|
|
|
|
|
s1 = prev->s1;
|
|
|
|
s2 = prev->s2;
|
|
|
|
for(i=0;i<32;i++) {
|
|
|
|
s0 = wav[i];
|
2011-11-20 22:21:32 +03:00
|
|
|
d = ((s0 << COEFF_BITS) - c->coeff[0] * s1 - c->coeff[1] * s2) >> COEFF_BITS;
|
2007-11-01 20:38:15 +02:00
|
|
|
data[i]=d;
|
|
|
|
if (max<d) max=d;
|
|
|
|
if (min>d) min=d;
|
|
|
|
s2 = s1;
|
|
|
|
s1 = s0;
|
|
|
|
}
|
|
|
|
prev->s1 = s1;
|
|
|
|
prev->s2 = s2;
|
|
|
|
|
|
|
|
if (max==0 && min==0) {
|
|
|
|
memset(adx,0,18);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max/7>-min/8) scale = max/7;
|
|
|
|
else scale = -min/8;
|
|
|
|
|
|
|
|
if (scale==0) scale=1;
|
|
|
|
|
|
|
|
AV_WB16(adx, scale);
|
|
|
|
|
2011-11-21 00:49:13 +03:00
|
|
|
init_put_bits(&pb, adx + 2, 16);
|
|
|
|
for (i = 0; i < 32; i++)
|
|
|
|
put_sbits(&pb, 4, av_clip(data[i]/scale, -8, 7));
|
|
|
|
flush_put_bits(&pb);
|
2007-11-01 20:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int adx_encode_header(AVCodecContext *avctx,unsigned char *buf,size_t bufsize)
|
|
|
|
{
|
2011-11-20 22:21:32 +03:00
|
|
|
ADXContext *c = avctx->priv_data;
|
|
|
|
|
2007-11-01 20:38:15 +02:00
|
|
|
AV_WB32(buf+0x00,0x80000000|0x20);
|
|
|
|
AV_WB32(buf+0x04,0x03120400|avctx->channels);
|
|
|
|
AV_WB32(buf+0x08,avctx->sample_rate);
|
2011-12-19 17:28:02 +03:00
|
|
|
AV_WB32(buf+0x0c,0);
|
2011-11-20 22:21:32 +03:00
|
|
|
AV_WB16(buf + 0x10, c->cutoff);
|
|
|
|
AV_WB32(buf + 0x12, 0x03000000);
|
|
|
|
AV_WB32(buf + 0x16, 0x00000000);
|
|
|
|
AV_WB32(buf + 0x1a, 0x00000000);
|
|
|
|
memcpy (buf + 0x1e, "(c)CRI", 6);
|
2007-11-01 20:38:15 +02:00
|
|
|
return 0x20+4;
|
|
|
|
}
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int adx_encode_init(AVCodecContext *avctx)
|
2007-11-01 20:38:15 +02:00
|
|
|
{
|
2011-11-20 22:21:32 +03:00
|
|
|
ADXContext *c = avctx->priv_data;
|
|
|
|
|
2007-11-01 20:38:15 +02:00
|
|
|
if (avctx->channels > 2)
|
2011-12-19 17:28:02 +03:00
|
|
|
return -1;
|
2007-11-01 20:38:15 +02:00
|
|
|
avctx->frame_size = 32;
|
|
|
|
|
|
|
|
avctx->coded_frame= avcodec_alloc_frame();
|
|
|
|
avctx->coded_frame->key_frame= 1;
|
|
|
|
|
2011-11-20 22:21:32 +03:00
|
|
|
/* the cutoff can be adjusted, but this seems to work pretty well */
|
|
|
|
c->cutoff = 500;
|
|
|
|
ff_adx_calculate_coeffs(c->cutoff, avctx->sample_rate, COEFF_BITS, c->coeff);
|
|
|
|
|
2007-11-01 20:38:15 +02:00
|
|
|
av_log(avctx, AV_LOG_DEBUG, "adx encode init\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int adx_encode_close(AVCodecContext *avctx)
|
2007-11-01 20:38:15 +02:00
|
|
|
{
|
|
|
|
av_freep(&avctx->coded_frame);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int adx_encode_frame(AVCodecContext *avctx,
|
|
|
|
uint8_t *frame, int buf_size, void *data)
|
|
|
|
{
|
|
|
|
ADXContext *c = avctx->priv_data;
|
|
|
|
const short *samples = data;
|
|
|
|
unsigned char *dst = frame;
|
|
|
|
int rest = avctx->frame_size;
|
|
|
|
|
|
|
|
if (!c->header_parsed) {
|
|
|
|
int hdrsize = adx_encode_header(avctx,dst,buf_size);
|
|
|
|
dst+=hdrsize;
|
|
|
|
c->header_parsed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avctx->channels==1) {
|
|
|
|
while(rest>=32) {
|
2011-11-20 22:21:32 +03:00
|
|
|
adx_encode(c, dst, samples, c->prev);
|
2007-11-01 20:38:15 +02:00
|
|
|
dst+=18;
|
|
|
|
samples+=32;
|
|
|
|
rest-=32;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while(rest>=32*2) {
|
|
|
|
short tmpbuf[32*2];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0;i<32;i++) {
|
|
|
|
tmpbuf[i] = samples[i*2];
|
|
|
|
tmpbuf[i+32] = samples[i*2+1];
|
|
|
|
}
|
|
|
|
|
2011-11-20 22:21:32 +03:00
|
|
|
adx_encode(c, dst, tmpbuf, c->prev);
|
|
|
|
adx_encode(c, dst + 18, tmpbuf + 32, c->prev + 1);
|
2007-11-01 20:38:15 +02:00
|
|
|
dst+=18*2;
|
|
|
|
samples+=32*2;
|
|
|
|
rest-=32*2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst-frame;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_adpcm_adx_encoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "adpcm_adx",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.id = CODEC_ID_ADPCM_ADX,
|
|
|
|
.priv_data_size = sizeof(ADXContext),
|
|
|
|
.init = adx_encode_init,
|
|
|
|
.encode = adx_encode_frame,
|
|
|
|
.close = adx_encode_close,
|
2010-11-12 13:04:40 +02:00
|
|
|
.sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
|
2009-03-02 07:18:33 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
|
2007-11-01 20:38:15 +02:00
|
|
|
};
|