Mercurial > libavcodec.hg
comparison g722.c @ 12478:5942c42c44e5 libavcodec
Add G.722 ADPCM audio decoder
author | mstorsjo |
---|---|
date | Thu, 09 Sep 2010 19:21:16 +0000 |
parents | |
children | 750ff18b7394 |
comparison
equal
deleted
inserted
replaced
12477:c707911013ad | 12478:5942c42c44e5 |
---|---|
1 /* | |
2 * G.722 ADPCM audio decoder | |
3 * | |
4 * Copyright (c) CMU 1993 Computer Science, Speech Group | |
5 * Chengxiang Lu and Alex Hauptmann | |
6 * Copyright (c) 2005 Steve Underwood <steveu at coppice.org> | |
7 * Copyright (c) 2009 Kenan Gillet | |
8 * Copyright (c) 2010 Martin Storsjo | |
9 * | |
10 * This file is part of FFmpeg. | |
11 * | |
12 * FFmpeg is free software; you can redistribute it and/or | |
13 * modify it under the terms of the GNU Lesser General Public | |
14 * License as published by the Free Software Foundation; either | |
15 * version 2.1 of the License, or (at your option) any later version. | |
16 * | |
17 * FFmpeg is distributed in the hope that it will be useful, | |
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 * Lesser General Public License for more details. | |
21 * | |
22 * You should have received a copy of the GNU Lesser General Public | |
23 * License along with FFmpeg; if not, write to the Free Software | |
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
25 */ | |
26 | |
27 /** | |
28 * @file | |
29 * | |
30 * G.722 ADPCM audio codec | |
31 * | |
32 * This G.722 decoder is a bit-exact implementation of the ITU G.722 | |
33 * specification for all three specified bitrates - 64000bps, 56000bps | |
34 * and 48000bps. It passes the ITU tests. | |
35 * | |
36 * @note For the 56000bps and 48000bps bitrates, the lowest 1 or 2 bits | |
37 * respectively of each byte are ignored. | |
38 */ | |
39 | |
40 #include "avcodec.h" | |
41 #include "mathops.h" | |
42 #include "get_bits.h" | |
43 | |
44 #define PREV_SAMPLES_BUF_SIZE 1024 | |
45 | |
46 typedef struct { | |
47 int16_t prev_samples[PREV_SAMPLES_BUF_SIZE]; ///< memory of past decoded samples | |
48 int prev_samples_pos; ///< the number of values in prev_samples | |
49 | |
50 /** | |
51 * The band[0] and band[1] correspond respectively to the lower band and higher band. | |
52 */ | |
53 struct G722Band { | |
54 int16_t s_predictor; ///< predictor output value | |
55 int32_t s_zero; ///< previous output signal from zero predictor | |
56 int8_t part_reconst_mem[2]; ///< signs of previous partially reconstructed signals | |
57 int16_t prev_qtzd_reconst; ///< previous quantized reconstructed signal (internal value, using low_inv_quant4) | |
58 int16_t pole_mem[2]; ///< second-order pole section coefficient buffer | |
59 int32_t diff_mem[6]; ///< quantizer difference signal memory | |
60 int16_t zero_mem[6]; ///< Seventh-order zero section coefficient buffer | |
61 int16_t log_factor; ///< delayed 2-logarithmic quantizer factor | |
62 int16_t scale_factor; ///< delayed quantizer scale factor | |
63 } band[2]; | |
64 } G722Context; | |
65 | |
66 | |
67 static const int8_t sign_lookup[2] = { -1, 1 }; | |
68 | |
69 static const int16_t inv_log2_table[32] = { | |
70 2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383, | |
71 2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834, | |
72 2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371, | |
73 3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008 | |
74 }; | |
75 static const int16_t high_log_factor_step[2] = { 798, -214 }; | |
76 static const int16_t high_inv_quant[4] = { -926, -202, 926, 202 }; | |
77 /** | |
78 * low_log_factor_step[index] == wl[rl42[index]] | |
79 */ | |
80 static const int16_t low_log_factor_step[16] = { | |
81 -60, 3042, 1198, 538, 334, 172, 58, -30, | |
82 3042, 1198, 538, 334, 172, 58, -30, -60 | |
83 }; | |
84 static const int16_t low_inv_quant4[16] = { | |
85 0, -2557, -1612, -1121, -786, -530, -323, -150, | |
86 2557, 1612, 1121, 786, 530, 323, 150, 0 | |
87 }; | |
88 | |
89 /** | |
90 * quadrature mirror filter (QMF) coefficients | |
91 * | |
92 * ITU-T G.722 Table 11 | |
93 */ | |
94 static const int16_t qmf_coeffs[12] = { | |
95 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11, | |
96 }; | |
97 | |
98 | |
99 /** | |
100 * adaptive predictor | |
101 * | |
102 * @param cur_diff the dequantized and scaled delta calculated from the | |
103 * current codeword | |
104 */ | |
105 static void do_adaptive_prediction(struct G722Band *band, const int cur_diff) | |
106 { | |
107 int sg[2], limit, i, cur_qtzd_reconst; | |
108 | |
109 const int cur_part_reconst = band->s_zero + cur_diff < 0; | |
110 | |
111 sg[0] = sign_lookup[cur_part_reconst != band->part_reconst_mem[0]]; | |
112 sg[1] = sign_lookup[cur_part_reconst == band->part_reconst_mem[1]]; | |
113 band->part_reconst_mem[1] = band->part_reconst_mem[0]; | |
114 band->part_reconst_mem[0] = cur_part_reconst; | |
115 | |
116 band->pole_mem[1] = av_clip((sg[0] * av_clip(band->pole_mem[0], -8191, 8191) >> 5) + | |
117 (sg[1] << 7) + (band->pole_mem[1] * 127 >> 7), -12288, 12288); | |
118 | |
119 limit = 15360 - band->pole_mem[1]; | |
120 band->pole_mem[0] = av_clip(-192 * sg[0] + (band->pole_mem[0] * 255 >> 8), -limit, limit); | |
121 | |
122 | |
123 if (cur_diff) { | |
124 for (i = 0; i < 6; i++) | |
125 band->zero_mem[i] = ((band->zero_mem[i]*255) >> 8) + | |
126 ((band->diff_mem[i]^cur_diff) < 0 ? -128 : 128); | |
127 } else | |
128 for (i = 0; i < 6; i++) | |
129 band->zero_mem[i] = (band->zero_mem[i]*255) >> 8; | |
130 | |
131 for (i = 5; i > 0; i--) | |
132 band->diff_mem[i] = band->diff_mem[i-1]; | |
133 band->diff_mem[0] = av_clip_int16(cur_diff << 1); | |
134 | |
135 band->s_zero = 0; | |
136 for (i = 5; i >= 0; i--) | |
137 band->s_zero += (band->zero_mem[i]*band->diff_mem[i]) >> 15; | |
138 | |
139 | |
140 cur_qtzd_reconst = av_clip_int16((band->s_predictor + cur_diff) << 1); | |
141 band->s_predictor = av_clip_int16(band->s_zero + | |
142 (band->pole_mem[0] * cur_qtzd_reconst >> 15) + | |
143 (band->pole_mem[1] * band->prev_qtzd_reconst >> 15)); | |
144 band->prev_qtzd_reconst = cur_qtzd_reconst; | |
145 } | |
146 | |
147 static int inline linear_scale_factor(const int log_factor) | |
148 { | |
149 const int wd1 = inv_log2_table[(log_factor >> 6) & 31]; | |
150 const int shift = log_factor >> 11; | |
151 return shift < 0 ? wd1 >> -shift : wd1 << shift; | |
152 } | |
153 | |
154 static void update_low_predictor(struct G722Band *band, const int ilow) | |
155 { | |
156 do_adaptive_prediction(band, | |
157 band->scale_factor * low_inv_quant4[ilow] >> 10); | |
158 | |
159 // quantizer adaptation | |
160 band->log_factor = av_clip((band->log_factor * 127 >> 7) + | |
161 low_log_factor_step[ilow], 0, 18432); | |
162 band->scale_factor = linear_scale_factor(band->log_factor - (8 << 11)); | |
163 } | |
164 | |
165 static void update_high_predictor(struct G722Band *band, const int dhigh, | |
166 const int ihigh) | |
167 { | |
168 do_adaptive_prediction(band, dhigh); | |
169 | |
170 // quantizer adaptation | |
171 band->log_factor = av_clip((band->log_factor * 127 >> 7) + | |
172 high_log_factor_step[ihigh&1], 0, 22528); | |
173 band->scale_factor = linear_scale_factor(band->log_factor - (10 << 11)); | |
174 } | |
175 | |
176 static void apply_qmf(const int16_t *prev_samples, int *xout1, int *xout2) | |
177 { | |
178 int i; | |
179 | |
180 *xout1 = 0; | |
181 *xout2 = 0; | |
182 for (i = 0; i < 12; i++) { | |
183 MAC16(*xout2, prev_samples[2*i ], qmf_coeffs[i ]); | |
184 MAC16(*xout1, prev_samples[2*i+1], qmf_coeffs[11-i]); | |
185 } | |
186 } | |
187 | |
188 static av_cold int g722_init(AVCodecContext * avctx) | |
189 { | |
190 G722Context *c = avctx->priv_data; | |
191 | |
192 if (avctx->channels != 1) { | |
193 av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n"); | |
194 return AVERROR_INVALIDDATA; | |
195 } | |
196 avctx->sample_fmt = SAMPLE_FMT_S16; | |
197 | |
198 switch (avctx->bits_per_coded_sample) { | |
199 case 8: | |
200 case 7: | |
201 case 6: | |
202 break; | |
203 default: | |
204 av_log(avctx, AV_LOG_WARNING, "Unsupported bits_per_coded_sample [%d], " | |
205 "assuming 8\n", | |
206 avctx->bits_per_coded_sample); | |
207 case 0: | |
208 avctx->bits_per_coded_sample = 8; | |
209 break; | |
210 } | |
211 | |
212 c->band[0].scale_factor = 8; | |
213 c->band[1].scale_factor = 2; | |
214 c->prev_samples_pos = 22; | |
215 | |
216 if (avctx->lowres) | |
217 avctx->sample_rate /= 2; | |
218 | |
219 return 0; | |
220 } | |
221 | |
222 static const int16_t low_inv_quant5[32] = { | |
223 -35, -35, -2919, -2195, -1765, -1458, -1219, -1023, | |
224 -858, -714, -587, -473, -370, -276, -190, -110, | |
225 2919, 2195, 1765, 1458, 1219, 1023, 858, 714, | |
226 587, 473, 370, 276, 190, 110, 35, -35 | |
227 }; | |
228 static const int16_t low_inv_quant6[64] = { | |
229 -17, -17, -17, -17, -3101, -2738, -2376, -2088, | |
230 -1873, -1689, -1535, -1399, -1279, -1170, -1072, -982, | |
231 -899, -822, -750, -682, -618, -558, -501, -447, | |
232 -396, -347, -300, -254, -211, -170, -130, -91, | |
233 3101, 2738, 2376, 2088, 1873, 1689, 1535, 1399, | |
234 1279, 1170, 1072, 982, 899, 822, 750, 682, | |
235 618, 558, 501, 447, 396, 347, 300, 254, | |
236 211, 170, 130, 91, 54, 17, -54, -17 | |
237 }; | |
238 | |
239 static const int16_t *low_inv_quants[3] = { low_inv_quant6, low_inv_quant5, | |
240 low_inv_quant4 }; | |
241 | |
242 static int g722_decode_frame(AVCodecContext *avctx, void *data, | |
243 int *data_size, AVPacket *avpkt) | |
244 { | |
245 G722Context *c = avctx->priv_data; | |
246 int16_t *out_buf = data; | |
247 int j, out_len = 0; | |
248 const int skip = 8 - avctx->bits_per_coded_sample; | |
249 const int16_t *quantizer_table = low_inv_quants[skip]; | |
250 GetBitContext gb; | |
251 | |
252 init_get_bits(&gb, avpkt->data, avpkt->size * 8); | |
253 | |
254 for (j = 0; j < avpkt->size; j++) { | |
255 int ilow, ihigh, rlow; | |
256 | |
257 ihigh = get_bits(&gb, 2); | |
258 ilow = get_bits(&gb, 6 - skip); | |
259 skip_bits(&gb, skip); | |
260 | |
261 rlow = av_clip((c->band[0].scale_factor * quantizer_table[ilow] >> 10) | |
262 + c->band[0].s_predictor, -16384, 16383); | |
263 | |
264 update_low_predictor(&c->band[0], ilow >> (2 - skip)); | |
265 | |
266 if (!avctx->lowres) { | |
267 const int dhigh = c->band[1].scale_factor * | |
268 high_inv_quant[ihigh] >> 10; | |
269 const int rhigh = av_clip(dhigh + c->band[1].s_predictor, | |
270 -16384, 16383); | |
271 int xout1, xout2; | |
272 | |
273 update_high_predictor(&c->band[1], dhigh, ihigh); | |
274 | |
275 c->prev_samples[c->prev_samples_pos++] = rlow + rhigh; | |
276 c->prev_samples[c->prev_samples_pos++] = rlow - rhigh; | |
277 apply_qmf(c->prev_samples + c->prev_samples_pos - 24, | |
278 &xout1, &xout2); | |
279 out_buf[out_len++] = av_clip_int16(xout1 >> 12); | |
280 out_buf[out_len++] = av_clip_int16(xout2 >> 12); | |
281 if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) { | |
282 memmove(c->prev_samples, | |
283 c->prev_samples + c->prev_samples_pos - 22, | |
284 22 * sizeof(c->prev_samples[0])); | |
285 c->prev_samples_pos = 22; | |
286 } | |
287 } else | |
288 out_buf[out_len++] = rlow; | |
289 } | |
290 *data_size = out_len << 1; | |
291 return avpkt->size; | |
292 } | |
293 | |
294 AVCodec adpcm_g722_decoder = { | |
295 .name = "g722", | |
296 .type = AVMEDIA_TYPE_AUDIO, | |
297 .id = CODEC_ID_ADPCM_G722, | |
298 .priv_data_size = sizeof(G722Context), | |
299 .init = g722_init, | |
300 .decode = g722_decode_frame, | |
301 .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"), | |
302 .max_lowres = 1, | |
303 }; | |
304 |