f5610466 |
/*
* ADX ADPCM codecs
* Copyright (c) 2001,2003 BERO
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
f5610466 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
f5610466 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
f5610466 |
* 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 |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
f5610466 |
*/ |
6a5d31ac |
#include "libavutil/intreadwrite.h" |
f5610466 |
#include "avcodec.h" |
c31dea61 |
#include "adx.h" |
c52ddc60 |
#include "get_bits.h" |
f5610466 |
/** |
ba87f080 |
* @file |
f5610466 |
* 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/
*/
|
defa0cd6 |
static av_cold int adx_decode_init(AVCodecContext *avctx) |
fd76c37f |
{ |
27360ccc |
ADXContext *c = avctx->priv_data;
int ret, header_size;
|
bdd62a61 |
if (avctx->extradata_size >= 24) {
if ((ret = avpriv_adx_decode_header(avctx, avctx->extradata,
avctx->extradata_size, &header_size,
c->coeff)) < 0) {
av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
return AVERROR_INVALIDDATA;
} |
4f1a7877 |
c->channels = avctx->channels;
c->header_parsed = 1; |
27360ccc |
}
|
5d6e4c16 |
avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
0eea2129 |
avcodec_get_frame_defaults(&c->frame);
avctx->coded_frame = &c->frame;
|
fd76c37f |
return 0;
}
|
8db67610 |
/**
* Decode 32 samples from 18 bytes.
*
* A 16-bit scalar value is applied to 32 residuals, which then have a
* 2nd-order LPC filter applied to it to form the output signal for a single
* channel.
*/ |
27360ccc |
static int adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch) |
f5610466 |
{ |
837bbd19 |
ADXChannelState *prev = &c->prev[ch]; |
c52ddc60 |
GetBitContext gb; |
67a5daf0 |
int scale = AV_RB16(in); |
f19af812 |
int i; |
8db67610 |
int s0, s1, s2, d; |
f19af812 |
|
27360ccc |
/* check if this is an EOF packet */
if (scale & 0x8000)
return -1; |
f19af812 |
|
05c1f11b |
init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8); |
f19af812 |
s1 = prev->s1;
s2 = prev->s2; |
05c1f11b |
for (i = 0; i < BLOCK_SAMPLES; i++) { |
c52ddc60 |
d = get_sbits(&gb, 4); |
b237248e |
s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS; |
f19af812 |
s2 = s1; |
295f3737 |
s1 = av_clip_int16(s0); |
837bbd19 |
*out = s1;
out += c->channels; |
f19af812 |
}
prev->s1 = s1;
prev->s2 = s2; |
f5610466 |
|
27360ccc |
return 0; |
f5610466 |
}
|
0eea2129 |
static int adx_decode_frame(AVCodecContext *avctx, void *data,
int *got_frame_ptr, AVPacket *avpkt) |
f5610466 |
{ |
8db67610 |
int buf_size = avpkt->size;
ADXContext *c = avctx->priv_data; |
0eea2129 |
int16_t *samples; |
27360ccc |
const uint8_t *buf = avpkt->data; |
8dfb13ea |
const uint8_t *buf_end = buf + avpkt->size; |
0eea2129 |
int num_blocks, ch, ret; |
27360ccc |
if (c->eof) { |
0eea2129 |
*got_frame_ptr = 0; |
27360ccc |
return buf_size; |
f19af812 |
}
|
4f1a7877 |
if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) { |
bdd62a61 |
int header_size; |
4f1a7877 |
if ((ret = avpriv_adx_decode_header(avctx, buf, buf_size, &header_size, |
bdd62a61 |
c->coeff)) < 0) {
av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
return AVERROR_INVALIDDATA;
} |
4f1a7877 |
c->channels = avctx->channels;
c->header_parsed = 1;
if (buf_size < header_size) |
bdd62a61 |
return AVERROR_INVALIDDATA; |
4f1a7877 |
buf += header_size; |
bdd62a61 |
buf_size -= header_size;
} |
4f1a7877 |
if (!c->header_parsed) |
6b6b84ae |
return AVERROR_INVALIDDATA; |
bdd62a61 |
|
0eea2129 |
/* calculate number of blocks in the packet */ |
27360ccc |
num_blocks = buf_size / (BLOCK_SIZE * c->channels); |
0eea2129 |
/* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
packet */
if (!num_blocks || buf_size % (BLOCK_SIZE * avctx->channels)) { |
27360ccc |
if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
c->eof = 1; |
0eea2129 |
*got_frame_ptr = 0; |
27360ccc |
return avpkt->size; |
f19af812 |
} |
7ff55d9b |
return AVERROR_INVALIDDATA; |
f19af812 |
} |
4783cf0d |
|
0eea2129 |
/* get output buffer */
c->frame.nb_samples = num_blocks * BLOCK_SAMPLES;
if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return ret;
}
samples = (int16_t *)c->frame.data[0];
|
7ff55d9b |
while (num_blocks--) { |
27360ccc |
for (ch = 0; ch < c->channels; ch++) { |
8dfb13ea |
if (buf_end - buf < BLOCK_SIZE || adx_decode(c, samples + ch, buf, ch)) { |
27360ccc |
c->eof = 1;
buf = avpkt->data + avpkt->size;
break;
}
buf_size -= BLOCK_SIZE;
buf += BLOCK_SIZE;
} |
05c1f11b |
samples += BLOCK_SAMPLES * c->channels; |
f19af812 |
} |
fbc79a91 |
|
0eea2129 |
*got_frame_ptr = 1;
*(AVFrame *)data = c->frame;
|
27360ccc |
return buf - avpkt->data; |
f5610466 |
}
|
730280f9 |
static void adx_decode_flush(AVCodecContext *avctx)
{
ADXContext *c = avctx->priv_data;
memset(c->prev, 0, sizeof(c->prev));
c->eof = 0;
}
|
e7e2df27 |
AVCodec ff_adpcm_adx_decoder = { |
ec6402b7 |
.name = "adpcm_adx",
.type = AVMEDIA_TYPE_AUDIO,
.id = CODEC_ID_ADPCM_ADX,
.priv_data_size = sizeof(ADXContext),
.init = adx_decode_init,
.decode = adx_decode_frame, |
730280f9 |
.flush = adx_decode_flush, |
0eea2129 |
.capabilities = CODEC_CAP_DR1, |
8db67610 |
.long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"), |
f5610466 |
}; |