| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | * G.722 ADPCM audio codec | ||
| 30 | * | ||
| 31 | * This G.722 decoder is a bit-exact implementation of the ITU G.722 | ||
| 32 | * specification for all three specified bitrates - 64000bps, 56000bps | ||
| 33 | * and 48000bps. It passes the ITU tests. | ||
| 34 | * | ||
| 35 | * @note For the 56000bps and 48000bps bitrates, the lowest 1 or 2 bits | ||
| 36 | * respectively of each byte are ignored. | ||
| 37 | */ | ||
| 38 | |||
| 39 | #include "mathops.h" | ||
| 40 | #include "g722.h" | ||
| 41 | |||
| 42 | static const int8_t sign_lookup[2] = { -1, 1 }; | ||
| 43 | |||
| 44 | static const int16_t inv_log2_table[32] = { | ||
| 45 | 2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383, | ||
| 46 | 2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834, | ||
| 47 | 2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371, | ||
| 48 | 3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008 | ||
| 49 | }; | ||
| 50 | static const int16_t high_log_factor_step[2] = { 798, -214 }; | ||
| 51 | const int16_t ff_g722_high_inv_quant[4] = { -926, -202, 926, 202 }; | ||
| 52 | /** | ||
| 53 | * low_log_factor_step[index] == wl[rl42[index]] | ||
| 54 | */ | ||
| 55 | static const int16_t low_log_factor_step[16] = { | ||
| 56 | -60, 3042, 1198, 538, 334, 172, 58, -30, | ||
| 57 | 3042, 1198, 538, 334, 172, 58, -30, -60 | ||
| 58 | }; | ||
| 59 | const int16_t ff_g722_low_inv_quant4[16] = { | ||
| 60 | 0, -2557, -1612, -1121, -786, -530, -323, -150, | ||
| 61 | 2557, 1612, 1121, 786, 530, 323, 150, 0 | ||
| 62 | }; | ||
| 63 | const int16_t ff_g722_low_inv_quant6[64] = { | ||
| 64 | -17, -17, -17, -17, -3101, -2738, -2376, -2088, | ||
| 65 | -1873, -1689, -1535, -1399, -1279, -1170, -1072, -982, | ||
| 66 | -899, -822, -750, -682, -618, -558, -501, -447, | ||
| 67 | -396, -347, -300, -254, -211, -170, -130, -91, | ||
| 68 | 3101, 2738, 2376, 2088, 1873, 1689, 1535, 1399, | ||
| 69 | 1279, 1170, 1072, 982, 899, 822, 750, 682, | ||
| 70 | 618, 558, 501, 447, 396, 347, 300, 254, | ||
| 71 | 211, 170, 130, 91, 54, 17, -54, -17 | ||
| 72 | }; | ||
| 73 | |||
| 74 | 535724 | static inline void s_zero(int cur_diff, struct G722Band *band) | |
| 75 | { | ||
| 76 | 535724 | int s_zero = 0; | |
| 77 | |||
| 78 | #define ACCUM(k, x, d) do { \ | ||
| 79 | int tmp = x; \ | ||
| 80 | band->zero_mem[k] = ((band->zero_mem[k] * 255) >> 8) + \ | ||
| 81 | d*((band->diff_mem[k]^cur_diff) < 0 ? -128 : 128); \ | ||
| 82 | band->diff_mem[k] = tmp; \ | ||
| 83 | s_zero += (tmp * band->zero_mem[k]) >> 15; \ | ||
| 84 | } while (0) | ||
| 85 |
2/2✓ Branch 0 taken 473324 times.
✓ Branch 1 taken 62400 times.
|
535724 | if (cur_diff) { |
| 86 |
2/2✓ Branch 0 taken 229962 times.
✓ Branch 1 taken 243362 times.
|
473324 | ACCUM(5, band->diff_mem[4], 1); |
| 87 |
2/2✓ Branch 0 taken 225166 times.
✓ Branch 1 taken 248158 times.
|
473324 | ACCUM(4, band->diff_mem[3], 1); |
| 88 |
2/2✓ Branch 0 taken 222726 times.
✓ Branch 1 taken 250598 times.
|
473324 | ACCUM(3, band->diff_mem[2], 1); |
| 89 |
2/2✓ Branch 0 taken 218178 times.
✓ Branch 1 taken 255146 times.
|
473324 | ACCUM(2, band->diff_mem[1], 1); |
| 90 |
2/2✓ Branch 0 taken 206122 times.
✓ Branch 1 taken 267202 times.
|
473324 | ACCUM(1, band->diff_mem[0], 1); |
| 91 |
2/2✓ Branch 0 taken 211557 times.
✓ Branch 1 taken 261767 times.
|
473324 | ACCUM(0, cur_diff * 2, 1); |
| 92 | } else { | ||
| 93 | 62400 | ACCUM(5, band->diff_mem[4], 0); | |
| 94 | 62400 | ACCUM(4, band->diff_mem[3], 0); | |
| 95 | 62400 | ACCUM(3, band->diff_mem[2], 0); | |
| 96 | 62400 | ACCUM(2, band->diff_mem[1], 0); | |
| 97 | 62400 | ACCUM(1, band->diff_mem[0], 0); | |
| 98 | 62400 | ACCUM(0, cur_diff * 2, 0); | |
| 99 | } | ||
| 100 | #undef ACCUM | ||
| 101 | 535724 | band->s_zero = s_zero; | |
| 102 | 535724 | } | |
| 103 | |||
| 104 | /** | ||
| 105 | * adaptive predictor | ||
| 106 | * | ||
| 107 | * @param cur_diff the dequantized and scaled delta calculated from the | ||
| 108 | * current codeword | ||
| 109 | */ | ||
| 110 | 535724 | static void do_adaptive_prediction(struct G722Band *band, const int cur_diff) | |
| 111 | { | ||
| 112 | int sg[2], limit, cur_qtzd_reconst; | ||
| 113 | |||
| 114 | 535724 | const int cur_part_reconst = band->s_zero + cur_diff < 0; | |
| 115 | |||
| 116 | 535724 | sg[0] = sign_lookup[cur_part_reconst != band->part_reconst_mem[0]]; | |
| 117 | 535724 | sg[1] = sign_lookup[cur_part_reconst == band->part_reconst_mem[1]]; | |
| 118 | 535724 | band->part_reconst_mem[1] = band->part_reconst_mem[0]; | |
| 119 | 535724 | band->part_reconst_mem[0] = cur_part_reconst; | |
| 120 | |||
| 121 | 535724 | band->pole_mem[1] = av_clip((sg[0] * av_clip(band->pole_mem[0], -8191, 8191) >> 5) + | |
| 122 | 535724 | (sg[1] * 128) + (band->pole_mem[1] * 127 >> 7), -12288, 12288); | |
| 123 | |||
| 124 | 535724 | limit = 15360 - band->pole_mem[1]; | |
| 125 | 535724 | band->pole_mem[0] = av_clip(-192 * sg[0] + (band->pole_mem[0] * 255 >> 8), -limit, limit); | |
| 126 | |||
| 127 | 535724 | s_zero(cur_diff, band); | |
| 128 | |||
| 129 | 535724 | cur_qtzd_reconst = av_clip_int16((band->s_predictor + cur_diff) * 2); | |
| 130 | 535724 | band->s_predictor = av_clip_int16(band->s_zero + | |
| 131 | 535724 | (band->pole_mem[0] * cur_qtzd_reconst >> 15) + | |
| 132 | 535724 | (band->pole_mem[1] * band->prev_qtzd_reconst >> 15)); | |
| 133 | 535724 | band->prev_qtzd_reconst = cur_qtzd_reconst; | |
| 134 | 535724 | } | |
| 135 | |||
| 136 | 535724 | static inline int linear_scale_factor(const int log_factor) | |
| 137 | { | ||
| 138 | 535724 | const int wd1 = inv_log2_table[(log_factor >> 6) & 31]; | |
| 139 | 535724 | const int shift = log_factor >> 11; | |
| 140 |
2/2✓ Branch 0 taken 472573 times.
✓ Branch 1 taken 63151 times.
|
535724 | return shift < 0 ? wd1 >> -shift : wd1 << shift; |
| 141 | } | ||
| 142 | |||
| 143 | 267862 | void ff_g722_update_low_predictor(struct G722Band *band, const int ilow) | |
| 144 | { | ||
| 145 | 267862 | do_adaptive_prediction(band, | |
| 146 | 267862 | band->scale_factor * ff_g722_low_inv_quant4[ilow] >> 10); | |
| 147 | |||
| 148 | // quantizer adaptation | ||
| 149 | 267862 | band->log_factor = av_clip((band->log_factor * 127 >> 7) + | |
| 150 | 267862 | low_log_factor_step[ilow], 0, 18432); | |
| 151 | 267862 | band->scale_factor = linear_scale_factor(band->log_factor - (8 << 11)); | |
| 152 | 267862 | } | |
| 153 | |||
| 154 | 267862 | void ff_g722_update_high_predictor(struct G722Band *band, const int dhigh, | |
| 155 | const int ihigh) | ||
| 156 | { | ||
| 157 | 267862 | do_adaptive_prediction(band, dhigh); | |
| 158 | |||
| 159 | // quantizer adaptation | ||
| 160 | 267862 | band->log_factor = av_clip((band->log_factor * 127 >> 7) + | |
| 161 | 267862 | high_log_factor_step[ihigh&1], 0, 22528); | |
| 162 | 267862 | band->scale_factor = linear_scale_factor(band->log_factor - (10 << 11)); | |
| 163 | 267862 | } | |
| 164 |