]> git.sesse.net Git - ffmpeg/blob - libavcodec/g722.c
51a0f39abc398ae7eaee79837d9cbf45f9cc3105
[ffmpeg] / libavcodec / g722.c
1 /*
2  * G.722 ADPCM audio encoder/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 #if CONFIG_ADPCM_G722_DECODER
223 static const int16_t low_inv_quant5[32] = {
224      -35,   -35, -2919, -2195, -1765, -1458, -1219, -1023,
225     -858,  -714,  -587,  -473,  -370,  -276,  -190,  -110,
226     2919,  2195,  1765,  1458,  1219,  1023,   858,   714,
227      587,   473,   370,   276,   190,   110,    35,   -35
228 };
229 static const int16_t low_inv_quant6[64] = {
230      -17,   -17,   -17,   -17, -3101, -2738, -2376, -2088,
231    -1873, -1689, -1535, -1399, -1279, -1170, -1072,  -982,
232     -899,  -822,  -750,  -682,  -618,  -558,  -501,  -447,
233     -396,  -347,  -300,  -254,  -211,  -170,  -130,   -91,
234     3101,  2738,  2376,  2088,  1873,  1689,  1535,  1399,
235     1279,  1170,  1072,   982,   899,   822,   750,   682,
236      618,   558,   501,   447,   396,   347,   300,   254,
237      211,   170,   130,    91,    54,    17,   -54,   -17
238 };
239
240 static const int16_t *low_inv_quants[3] = { low_inv_quant6, low_inv_quant5,
241                                  low_inv_quant4 };
242
243 static int g722_decode_frame(AVCodecContext *avctx, void *data,
244                              int *data_size, AVPacket *avpkt)
245 {
246     G722Context *c = avctx->priv_data;
247     int16_t *out_buf = data;
248     int j, out_len = 0;
249     const int skip = 8 - avctx->bits_per_coded_sample;
250     const int16_t *quantizer_table = low_inv_quants[skip];
251     GetBitContext gb;
252
253     init_get_bits(&gb, avpkt->data, avpkt->size * 8);
254
255     for (j = 0; j < avpkt->size; j++) {
256         int ilow, ihigh, rlow;
257
258         ihigh = get_bits(&gb, 2);
259         ilow = get_bits(&gb, 6 - skip);
260         skip_bits(&gb, skip);
261
262         rlow = av_clip((c->band[0].scale_factor * quantizer_table[ilow] >> 10)
263                       + c->band[0].s_predictor, -16384, 16383);
264
265         update_low_predictor(&c->band[0], ilow >> (2 - skip));
266
267         if (!avctx->lowres) {
268             const int dhigh = c->band[1].scale_factor *
269                               high_inv_quant[ihigh] >> 10;
270             const int rhigh = av_clip(dhigh + c->band[1].s_predictor,
271                                       -16384, 16383);
272             int xout1, xout2;
273
274             update_high_predictor(&c->band[1], dhigh, ihigh);
275
276             c->prev_samples[c->prev_samples_pos++] = rlow + rhigh;
277             c->prev_samples[c->prev_samples_pos++] = rlow - rhigh;
278             apply_qmf(c->prev_samples + c->prev_samples_pos - 24,
279                       &xout1, &xout2);
280             out_buf[out_len++] = av_clip_int16(xout1 >> 12);
281             out_buf[out_len++] = av_clip_int16(xout2 >> 12);
282             if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
283                 memmove(c->prev_samples,
284                         c->prev_samples + c->prev_samples_pos - 22,
285                         22 * sizeof(c->prev_samples[0]));
286                 c->prev_samples_pos = 22;
287             }
288         } else
289             out_buf[out_len++] = rlow;
290     }
291     *data_size = out_len << 1;
292     return avpkt->size;
293 }
294
295 AVCodec adpcm_g722_decoder = {
296     .name           = "g722",
297     .type           = AVMEDIA_TYPE_AUDIO,
298     .id             = CODEC_ID_ADPCM_G722,
299     .priv_data_size = sizeof(G722Context),
300     .init           = g722_init,
301     .decode         = g722_decode_frame,
302     .long_name      = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
303     .max_lowres     = 1,
304 };
305 #endif
306
307 #if CONFIG_ADPCM_G722_ENCODER
308 static const int16_t low_quant[33] = {
309       35,   72,  110,  150,  190,  233,  276,  323,
310      370,  422,  473,  530,  587,  650,  714,  786,
311      858,  940, 1023, 1121, 1219, 1339, 1458, 1612,
312     1765, 1980, 2195, 2557, 2919
313 };
314
315 static inline void filter_samples(G722Context *c, const int16_t *samples,
316                                   int *xlow, int *xhigh)
317 {
318     int xout1, xout2;
319     c->prev_samples[c->prev_samples_pos++] = samples[0];
320     c->prev_samples[c->prev_samples_pos++] = samples[1];
321     apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2);
322     *xlow  = xout1 + xout2 >> 13;
323     *xhigh = xout1 - xout2 >> 13;
324     if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
325         memmove(c->prev_samples,
326                 c->prev_samples + c->prev_samples_pos - 22,
327                 22 * sizeof(c->prev_samples[0]));
328         c->prev_samples_pos = 22;
329     }
330 }
331
332 static inline int encode_high(const struct G722Band *state, int xhigh)
333 {
334     int diff = av_clip_int16(xhigh - state->s_predictor);
335     int pred = 141 * state->scale_factor >> 8;
336            /* = diff >= 0 ? (diff < pred) + 2 : diff >= -pred */
337     return ((diff ^ (diff >> (sizeof(diff)*8-1))) < pred) + 2*(diff >= 0);
338 }
339
340 static inline int encode_low(const struct G722Band* state, int xlow)
341 {
342     int diff  = av_clip_int16(xlow - state->s_predictor);
343            /* = diff >= 0 ? diff : -(diff + 1) */
344     int limit = diff ^ (diff >> (sizeof(diff)*8-1));
345     int i = 0;
346     limit = limit + 1 << 10;
347     if (limit > low_quant[8] * state->scale_factor)
348         i = 9;
349     while (i < 29 && limit > low_quant[i] * state->scale_factor)
350         i++;
351     return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i;
352 }
353
354 static int g722_encode_frame(AVCodecContext *avctx,
355                              uint8_t *dst, int buf_size, void *data)
356 {
357     G722Context *c = avctx->priv_data;
358     const int16_t *samples = data;
359     int i;
360
361     for (i = 0; i < buf_size >> 1; i++) {
362         int xlow, xhigh, ihigh, ilow;
363         filter_samples(c, &samples[2*i], &xlow, &xhigh);
364         ihigh = encode_high(&c->band[1], xhigh);
365         ilow  = encode_low(&c->band[0], xlow);
366         update_high_predictor(&c->band[1], c->band[1].scale_factor *
367                               high_inv_quant[ihigh] >> 10, ihigh);
368         update_low_predictor(&c->band[0], ilow >> 2);
369         *dst++ = ihigh << 6 | ilow;
370     }
371     return i;
372 }
373
374 AVCodec adpcm_g722_encoder = {
375     .name           = "g722",
376     .type           = AVMEDIA_TYPE_AUDIO,
377     .id             = CODEC_ID_ADPCM_G722,
378     .priv_data_size = sizeof(G722Context),
379     .init           = g722_init,
380     .encode         = g722_encode_frame,
381     .long_name      = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
382     .sample_fmts    = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
383 };
384 #endif
385