adx: calculate correct LPC coeffs
[libav.git] / libavcodec / adxdec.c
1 /*
2 * ADX ADPCM codecs
3 * Copyright (c) 2001,2003 BERO
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "libavutil/intreadwrite.h"
23 #include "avcodec.h"
24 #include "adx.h"
25 #include "get_bits.h"
26
27 /**
28 * @file
29 * SEGA CRI adx codecs.
30 *
31 * Reference documents:
32 * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
33 * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
34 */
35
36 static av_cold int adx_decode_init(AVCodecContext *avctx)
37 {
38 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
39 return 0;
40 }
41
42 /**
43 * Decode 32 samples from 18 bytes.
44 *
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
47 * channel.
48 */
49 static void adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
50 {
51 ADXChannelState *prev = &c->prev[ch];
52 GetBitContext gb;
53 int scale = AV_RB16(in);
54 int i;
55 int s0, s1, s2, d;
56
57 init_get_bits(&gb, in + 2, (18 - 2) * 8);
58 s1 = prev->s1;
59 s2 = prev->s2;
60 for (i = 0; i < 32; i++) {
61 d = get_sbits(&gb, 4);
62 s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
63 s2 = s1;
64 s1 = av_clip_int16(s0);
65 *out = s1;
66 out += c->channels;
67 }
68 prev->s1 = s1;
69 prev->s2 = s2;
70 }
71
72 /**
73 * Decode stream header.
74 *
75 * @param avctx codec context
76 * @param buf packet data
77 * @param bufsize packet size
78 * @return data offset or negative error code if header is invalid
79 */
80 static int adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
81 int bufsize)
82 {
83 ADXContext *c = avctx->priv_data;
84 int offset, cutoff;
85
86 if (AV_RB16(buf) != 0x8000)
87 return AVERROR_INVALIDDATA;
88 offset = AV_RB16(buf + 2) + 4;
89 if (bufsize < offset || memcmp(buf + offset - 6, "(c)CRI", 6))
90 return AVERROR_INVALIDDATA;
91
92 c->channels = avctx->channels = buf[7];
93 if (avctx->channels > 2)
94 return AVERROR_INVALIDDATA;
95 avctx->sample_rate = AV_RB32(buf + 8);
96 if (avctx->sample_rate < 1 ||
97 avctx->sample_rate > INT_MAX / (avctx->channels * 18 * 8))
98 return AVERROR_INVALIDDATA;
99 avctx->bit_rate = avctx->sample_rate * avctx->channels * 18 * 8 / 32;
100
101 cutoff = AV_RB16(buf + 16);
102 ff_adx_calculate_coeffs(cutoff, avctx->sample_rate, COEFF_BITS, c->coeff);
103
104 return offset;
105 }
106
107 static int adx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
108 AVPacket *avpkt)
109 {
110 const uint8_t *buf0 = avpkt->data;
111 int buf_size = avpkt->size;
112 ADXContext *c = avctx->priv_data;
113 int16_t *samples = data;
114 const uint8_t *buf = buf0;
115 int rest = buf_size;
116
117 if (!c->header_parsed) {
118 int hdrsize = adx_decode_header(avctx, buf, rest);
119 if (hdrsize < 0) {
120 av_log(avctx, AV_LOG_ERROR, "invalid stream header\n");
121 return hdrsize;
122 }
123 c->header_parsed = 1;
124 buf += hdrsize;
125 rest -= hdrsize;
126 }
127
128 /* 18 bytes of data are expanded into 32*2 bytes of audio,
129 so guard against buffer overflows */
130 if (rest / 18 > *data_size / 64)
131 rest = (*data_size / 64) * 18;
132
133 if (c->in_temp) {
134 int copysize = 18 * avctx->channels - c->in_temp;
135 memcpy(c->dec_temp + c->in_temp, buf, copysize);
136 rest -= copysize;
137 buf += copysize;
138 adx_decode(c, samples, c->dec_temp, 0);
139 if (avctx->channels == 2)
140 adx_decode(c, samples + 1, c->dec_temp + 18, 1);
141 samples += 32 * c->channels;
142 }
143
144 while (rest >= 18 * c->channels) {
145 adx_decode(c, samples, buf, 0);
146 if (c->channels == 2)
147 adx_decode(c, samples + 1, buf + 18, 1);
148 rest -= 18 * c->channels;
149 buf += 18 * c->channels;
150 samples += 32 * c->channels;
151 }
152
153 c->in_temp = rest;
154 if (rest) {
155 memcpy(c->dec_temp, buf, rest);
156 buf += rest;
157 }
158 *data_size = (uint8_t*)samples - (uint8_t*)data;
159 return buf - buf0;
160 }
161
162 AVCodec ff_adpcm_adx_decoder = {
163 .name = "adpcm_adx",
164 .type = AVMEDIA_TYPE_AUDIO,
165 .id = CODEC_ID_ADPCM_ADX,
166 .priv_data_size = sizeof(ADXContext),
167 .init = adx_decode_init,
168 .decode = adx_decode_frame,
169 .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
170 };