c31dea61 |
/*
* ADX ADPCM codecs
* Copyright (c) 2001,2003 BERO
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* 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.
*
* FFmpeg is distributed in the hope that it will be useful,
* 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
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ |
6a5d31ac |
|
c31dea61 |
#include "avcodec.h"
#include "adx.h" |
1fb47728 |
#include "bytestream.h" |
6aba117f |
#include "internal.h" |
99423853 |
#include "put_bits.h" |
c31dea61 |
/** |
ba87f080 |
* @file |
c31dea61 |
* 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/
*/
|
25edfc88 |
static void adx_encode(ADXContext *c, uint8_t *adx, const int16_t *wav, |
61366821 |
ADXChannelState *prev, int channels) |
c31dea61 |
{ |
99423853 |
PutBitContext pb; |
c31dea61 |
int scale; |
61366821 |
int i, j; |
a85ab8ad |
int s0, s1, s2, d;
int max = 0;
int min = 0; |
c31dea61 |
s1 = prev->s1;
s2 = prev->s2; |
61366821 |
for (i = 0, j = 0; j < 32; i += channels, j++) { |
c31dea61 |
s0 = wav[i]; |
b237248e |
d = ((s0 << COEFF_BITS) - c->coeff[0] * s1 - c->coeff[1] * s2) >> COEFF_BITS; |
a85ab8ad |
if (max < d)
max = d;
if (min > d)
min = d; |
c31dea61 |
s2 = s1;
s1 = s0;
}
|
a85ab8ad |
if (max == 0 && min == 0) { |
6b0ab561 |
prev->s1 = s1;
prev->s2 = s2; |
656e606c |
memset(adx, 0, BLOCK_SIZE); |
c31dea61 |
return;
}
|
a85ab8ad |
if (max / 7 > -min / 8)
scale = max / 7;
else
scale = -min / 8; |
c31dea61 |
|
a85ab8ad |
if (scale == 0)
scale = 1; |
c31dea61 |
AV_WB16(adx, scale);
|
99423853 |
init_put_bits(&pb, adx + 2, 16); |
6b0ab561 |
s1 = prev->s1;
s2 = prev->s2;
for (i = 0, j = 0; j < 32; i += channels, j++) {
d = ((wav[i] << COEFF_BITS) - c->coeff[0] * s1 - c->coeff[1] * s2) >> COEFF_BITS;
|
d74c8d3a |
d = av_clip_intp2(ROUNDED_DIV(d, scale), 3); |
6b0ab561 |
put_sbits(&pb, 4, d);
s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
s2 = s1;
s1 = s0;
}
prev->s1 = s1;
prev->s2 = s2;
|
99423853 |
flush_put_bits(&pb); |
c31dea61 |
}
|
1fb47728 |
#define HEADER_SIZE 36
|
25edfc88 |
static int adx_encode_header(AVCodecContext *avctx, uint8_t *buf, int bufsize) |
c31dea61 |
{ |
b237248e |
ADXContext *c = avctx->priv_data;
|
1fb47728 |
bytestream_put_be16(&buf, 0x8000); /* header signature */
bytestream_put_be16(&buf, HEADER_SIZE - 4); /* copyright offset */
bytestream_put_byte(&buf, 3); /* encoding */
bytestream_put_byte(&buf, BLOCK_SIZE); /* block size */
bytestream_put_byte(&buf, 4); /* sample size */
bytestream_put_byte(&buf, avctx->channels); /* channels */
bytestream_put_be32(&buf, avctx->sample_rate); /* sample rate */
bytestream_put_be32(&buf, 0); /* total sample count */
bytestream_put_be16(&buf, c->cutoff); /* cutoff frequency */
bytestream_put_byte(&buf, 3); /* version */
bytestream_put_byte(&buf, 0); /* flags */
bytestream_put_be32(&buf, 0); /* unknown */
bytestream_put_be32(&buf, 0); /* loop enabled */
bytestream_put_be16(&buf, 0); /* padding */
bytestream_put_buffer(&buf, "(c)CRI", 6); /* copyright signature */
return HEADER_SIZE; |
c31dea61 |
}
|
98a6fff9 |
static av_cold int adx_encode_init(AVCodecContext *avctx) |
c31dea61 |
{ |
b237248e |
ADXContext *c = avctx->priv_data;
|
cc40c056 |
if (avctx->channels > 2) {
av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
return AVERROR(EINVAL);
} |
656e606c |
avctx->frame_size = BLOCK_SAMPLES; |
c31dea61 |
|
b237248e |
/* 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);
|
c31dea61 |
return 0;
}
|
6aba117f |
static int adx_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
const AVFrame *frame, int *got_packet_ptr) |
c31dea61 |
{ |
a85ab8ad |
ADXContext *c = avctx->priv_data; |
6aba117f |
const int16_t *samples = (const int16_t *)frame->data[0];
uint8_t *dst;
int ch, out_size, ret;
out_size = BLOCK_SIZE * avctx->channels + !c->header_parsed * HEADER_SIZE; |
e36db49b |
if ((ret = ff_alloc_packet2(avctx, avpkt, out_size, 0)) < 0) |
6aba117f |
return ret;
dst = avpkt->data; |
c31dea61 |
if (!c->header_parsed) { |
754ebd1a |
int hdrsize; |
6aba117f |
if ((hdrsize = adx_encode_header(avctx, dst, avpkt->size)) < 0) { |
754ebd1a |
av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
return AVERROR(EINVAL);
}
dst += hdrsize; |
c31dea61 |
c->header_parsed = 1;
}
|
f1be41c6 |
for (ch = 0; ch < avctx->channels; ch++) {
adx_encode(c, dst, samples + ch, &c->prev[ch], avctx->channels); |
656e606c |
dst += BLOCK_SIZE; |
c31dea61 |
} |
6aba117f |
*got_packet_ptr = 1;
return 0; |
c31dea61 |
}
|
e7e2df27 |
AVCodec ff_adpcm_adx_encoder = { |
ec6402b7 |
.name = "adpcm_adx", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"), |
ec6402b7 |
.type = AVMEDIA_TYPE_AUDIO, |
36ef5369 |
.id = AV_CODEC_ID_ADPCM_ADX, |
ec6402b7 |
.priv_data_size = sizeof(ADXContext),
.init = adx_encode_init, |
6aba117f |
.encode2 = adx_encode_frame, |
a85ab8ad |
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
AV_SAMPLE_FMT_NONE }, |
c31dea61 |
}; |