3 * Copyright (c) 2001,2003 BERO
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intreadwrite.h"
29 * SEGA CRI adx codecs.
31 * Reference documents:
32 * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
33 * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
36 static av_cold int adx_decode_init(AVCodecContext *avctx)
38 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
43 * Decode 32 samples from 18 bytes.
45 * A 16-bit scalar value is applied to 32 residuals, which then have a
46 * 2nd-order LPC filter applied to it to form the output signal for a single
49 static void adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
51 ADXChannelState *prev = &c->prev[ch];
53 int scale = AV_RB16(in);
57 init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
60 for (i = 0; i < BLOCK_SAMPLES; i++) {
61 d = get_sbits(&gb, 4);
62 s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
64 s1 = av_clip_int16(s0);
72 static int adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
75 ADXContext *c = avctx->priv_data;
78 if ((ret = ff_adx_decode_header(avctx, buf, bufsize, &header_size,
82 c->channels = avctx->channels;
86 static int adx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
89 const uint8_t *buf0 = avpkt->data;
90 int buf_size = avpkt->size;
91 ADXContext *c = avctx->priv_data;
92 int16_t *samples = data;
93 const uint8_t *buf = buf0;
97 if (!c->header_parsed) {
98 int hdrsize = adx_decode_header(avctx, buf, rest);
100 av_log(avctx, AV_LOG_ERROR, "invalid stream header\n");
103 c->header_parsed = 1;
108 /* 18 bytes of data are expanded into 32*2 bytes of audio,
109 so guard against buffer overflows */
110 num_blocks = (rest + c->in_temp) / (BLOCK_SIZE * c->channels);
111 if (num_blocks > *data_size / (BLOCK_SAMPLES * c->channels)) {
112 rest = (*data_size / (BLOCK_SAMPLES * c->channels)) * BLOCK_SIZE;
113 num_blocks = (rest + c->in_temp) / (BLOCK_SIZE * c->channels);
116 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
117 return AVERROR_INVALIDDATA;
121 int copysize = BLOCK_SIZE * avctx->channels - c->in_temp;
122 memcpy(c->dec_temp + c->in_temp, buf, copysize);
125 adx_decode(c, samples, c->dec_temp, 0);
126 if (avctx->channels == 2)
127 adx_decode(c, samples + 1, c->dec_temp + BLOCK_SIZE, 1);
128 samples += BLOCK_SAMPLES * c->channels;
132 while (num_blocks--) {
133 adx_decode(c, samples, buf, 0);
134 if (c->channels == 2)
135 adx_decode(c, samples + 1, buf + BLOCK_SIZE, 1);
136 rest -= BLOCK_SIZE * c->channels;
137 buf += BLOCK_SIZE * c->channels;
138 samples += BLOCK_SAMPLES * c->channels;
143 memcpy(c->dec_temp, buf, rest);
146 *data_size = (uint8_t*)samples - (uint8_t*)data;
150 AVCodec ff_adpcm_adx_decoder = {
152 .type = AVMEDIA_TYPE_AUDIO,
153 .id = CODEC_ID_ADPCM_ADX,
154 .priv_data_size = sizeof(ADXContext),
155 .init = adx_decode_init,
156 .decode = adx_decode_frame,
157 .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),