Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2007-2008 CSIRO |
Jean-Marc Valin | 190b782 | 2010-08-27 15:03:20 -0400 | [diff] [blame] | 2 | Copyright (c) 2007-2010 Xiph.Org Foundation |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 3 | Copyright (c) 2008 Gregory Maxwell |
Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 4 | Written by Jean-Marc Valin and Gregory Maxwell */ |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 5 | /* |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions |
| 8 | are met: |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 9 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 10 | - Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 12 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 13 | - Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in the |
| 15 | documentation and/or other materials provided with the distribution. |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 16 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 21 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include "config.h" |
| 32 | #endif |
| 33 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 34 | #define CELT_C |
| 35 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 36 | #include "os_support.h" |
Jean-Marc Valin | f02ba11 | 2007-11-30 01:10:42 +1100 | [diff] [blame] | 37 | #include "mdct.h" |
| 38 | #include <math.h> |
Jean-Marc Valin | 013c31d | 2007-11-30 11:36:46 +1100 | [diff] [blame] | 39 | #include "celt.h" |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 40 | #include "pitch.h" |
Jean-Marc Valin | 991c0f0 | 2007-11-30 16:07:46 +1100 | [diff] [blame] | 41 | #include "bands.h" |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 42 | #include "modes.h" |
Jean-Marc Valin | 6238bc0 | 2008-01-28 22:28:54 +1100 | [diff] [blame] | 43 | #include "entcode.h" |
Jean-Marc Valin | 98d2a49 | 2007-12-07 22:46:47 +1100 | [diff] [blame] | 44 | #include "quant_bands.h" |
Jean-Marc Valin | 4fbd18d | 2008-01-17 14:07:55 +1100 | [diff] [blame] | 45 | #include "rate.h" |
Jean-Marc Valin | c7e0b76 | 2008-03-16 07:55:29 +1100 | [diff] [blame] | 46 | #include "stack_alloc.h" |
Jean-Marc Valin | e4aeb47 | 2008-06-29 12:06:05 +1000 | [diff] [blame] | 47 | #include "mathops.h" |
Jean-Marc Valin | d9b9565 | 2008-08-31 23:34:47 -0400 | [diff] [blame] | 48 | #include "float_cast.h" |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 49 | #include <stdarg.h> |
Jean-Marc Valin | 2779df7 | 2011-10-04 13:26:53 -0400 | [diff] [blame] | 50 | #include "celt_lpc.h" |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 51 | #include "vq.h" |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 52 | |
Ralph Giles | b0f4e90 | 2011-09-07 13:16:29 -0700 | [diff] [blame] | 53 | #ifndef OPUS_VERSION |
| 54 | #define OPUS_VERSION "unknown" |
| 55 | #endif |
| 56 | |
Gregory Maxwell | afc8d53 | 2011-09-28 15:13:26 -0400 | [diff] [blame] | 57 | #ifdef CUSTOM_MODES |
| 58 | #define OPUS_CUSTOM_NOSTATIC |
| 59 | #else |
| 60 | #define OPUS_CUSTOM_NOSTATIC static inline |
| 61 | #endif |
| 62 | |
Timothy B. Terriberry | 845dfa1 | 2011-01-02 16:53:28 -0800 | [diff] [blame] | 63 | static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0}; |
| 64 | /* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */ |
| 65 | static const unsigned char spread_icdf[4] = {25, 23, 2, 0}; |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 66 | |
Jean-Marc Valin | 2ce5c63 | 2011-01-17 20:50:18 -0500 | [diff] [blame] | 67 | static const unsigned char tapset_icdf[3]={2,1,0}; |
| 68 | |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 69 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 70 | static const unsigned char toOpusTable[20] = { |
| 71 | 0xE0, 0xE8, 0xF0, 0xF8, |
| 72 | 0xC0, 0xC8, 0xD0, 0xD8, |
| 73 | 0xA0, 0xA8, 0xB0, 0xB8, |
| 74 | 0x00, 0x00, 0x00, 0x00, |
| 75 | 0x80, 0x88, 0x90, 0x98, |
| 76 | }; |
| 77 | |
| 78 | static const unsigned char fromOpusTable[16] = { |
| 79 | 0x80, 0x88, 0x90, 0x98, |
| 80 | 0x40, 0x48, 0x50, 0x58, |
| 81 | 0x20, 0x28, 0x30, 0x38, |
| 82 | 0x00, 0x08, 0x10, 0x18 |
| 83 | }; |
| 84 | |
| 85 | static inline int toOpus(unsigned char c) |
| 86 | { |
| 87 | int ret=0; |
| 88 | if (c<0xA0) |
| 89 | ret = toOpusTable[c>>3]; |
| 90 | if (ret == 0) |
| 91 | return -1; |
| 92 | else |
| 93 | return ret|(c&0x7); |
| 94 | } |
| 95 | |
| 96 | static inline int fromOpus(unsigned char c) |
| 97 | { |
| 98 | if (c<0x80) |
| 99 | return -1; |
| 100 | else |
| 101 | return fromOpusTable[(c>>3)-16] | (c&0x7); |
| 102 | } |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 103 | #endif /*CUSTOM_MODES*/ |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 104 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 105 | #define COMBFILTER_MAXPERIOD 1024 |
Jean-Marc Valin | d121260 | 2011-01-25 13:11:36 -0500 | [diff] [blame] | 106 | #define COMBFILTER_MINPERIOD 15 |
Jean-Marc Valin | 6bf0462 | 2010-09-30 10:16:22 -0400 | [diff] [blame] | 107 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 108 | static int resampling_factor(opus_int32 rate) |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 109 | { |
| 110 | int ret; |
| 111 | switch (rate) |
| 112 | { |
| 113 | case 48000: |
| 114 | ret = 1; |
| 115 | break; |
| 116 | case 24000: |
| 117 | ret = 2; |
| 118 | break; |
| 119 | case 16000: |
| 120 | ret = 3; |
| 121 | break; |
| 122 | case 12000: |
| 123 | ret = 4; |
| 124 | break; |
| 125 | case 8000: |
| 126 | ret = 6; |
| 127 | break; |
| 128 | default: |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 129 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 130 | ret = 0; |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 131 | break; |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 132 | #else |
| 133 | celt_assert(0); |
| 134 | #endif |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 135 | } |
| 136 | return ret; |
| 137 | } |
| 138 | |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 139 | /** Encoder state |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 140 | @brief Encoder state |
| 141 | */ |
Jean-Marc Valin | 242da53 | 2011-09-05 20:53:33 -0400 | [diff] [blame] | 142 | struct OpusCustomEncoder { |
| 143 | const OpusCustomMode *mode; /**< Mode used by the encoder */ |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 144 | int overlap; |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 145 | int channels; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 146 | int stream_channels; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 147 | |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 148 | int force_intra; |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 149 | int clip; |
Jean-Marc Valin | d539c6b | 2011-02-03 13:36:03 -0500 | [diff] [blame] | 150 | int disable_pf; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 151 | int complexity; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 152 | int upsample; |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 153 | int start, end; |
| 154 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 155 | opus_int32 bitrate; |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 156 | int vbr; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 157 | int signalling; |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 158 | int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */ |
Jean-Marc Valin | 6965388 | 2011-04-21 10:41:13 -0400 | [diff] [blame] | 159 | int loss_rate; |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 160 | |
| 161 | /* Everything beyond this point gets cleared on a reset */ |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 162 | #define ENCODER_RESET_START rng |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 163 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 164 | opus_uint32 rng; |
Timothy B. Terriberry | 320cf2e | 2010-12-17 05:52:06 -0800 | [diff] [blame] | 165 | int spread_decision; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 166 | opus_val32 delayedIntra; |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 167 | int tonal_average; |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 168 | int lastCodedBands; |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 169 | int hf_average; |
| 170 | int tapset_decision; |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 171 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 172 | int prefilter_period; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 173 | opus_val16 prefilter_gain; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 174 | int prefilter_tapset; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 175 | #ifdef RESYNTH |
| 176 | int prefilter_period_old; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 177 | opus_val16 prefilter_gain_old; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 178 | int prefilter_tapset_old; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 179 | #endif |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 180 | int consec_transient; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 181 | |
Gregory Maxwell | 86dd984 | 2011-08-12 19:03:29 -0400 | [diff] [blame] | 182 | opus_val32 preemph_memE[2]; |
| 183 | opus_val32 preemph_memD[2]; |
| 184 | |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 185 | /* VBR-related parameters */ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 186 | opus_int32 vbr_reservoir; |
| 187 | opus_int32 vbr_drift; |
| 188 | opus_int32 vbr_offset; |
| 189 | opus_int32 vbr_count; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 190 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 191 | #ifdef RESYNTH |
| 192 | celt_sig syn_mem[2][2*MAX_PERIOD]; |
| 193 | #endif |
| 194 | |
Jean-Marc Valin | ca8b992 | 2010-08-27 16:23:03 -0400 | [diff] [blame] | 195 | celt_sig in_mem[1]; /* Size = channels*mode->overlap */ |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 196 | /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */ |
Jean-Marc Valin | ca8b992 | 2010-08-27 16:23:03 -0400 | [diff] [blame] | 197 | /* celt_sig overlap_mem[], Size = channels*mode->overlap */ |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 198 | /* opus_val16 oldEBands[], Size = 2*channels*mode->nbEBands */ |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 199 | }; |
| 200 | |
Jean-Marc Valin | 8cf29f0 | 2011-01-30 23:38:28 -0500 | [diff] [blame] | 201 | int celt_encoder_get_size(int channels) |
| 202 | { |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 203 | CELTMode *mode = opus_custom_mode_create(48000, 960, NULL); |
| 204 | return opus_custom_encoder_get_size(mode, channels); |
Jean-Marc Valin | 8cf29f0 | 2011-01-30 23:38:28 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Gregory Maxwell | afc8d53 | 2011-09-28 15:13:26 -0400 | [diff] [blame] | 207 | OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels) |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 208 | { |
| 209 | int size = sizeof(struct CELTEncoder) |
| 210 | + (2*channels*mode->overlap-1)*sizeof(celt_sig) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 211 | + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 212 | + 3*channels*mode->nbEBands*sizeof(opus_val16); |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 213 | return size; |
| 214 | } |
| 215 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 216 | #ifdef CUSTOM_MODES |
| 217 | CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error) |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 218 | { |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 219 | int ret; |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 220 | CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels)); |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 221 | /* init will handle the NULL case */ |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 222 | ret = opus_custom_encoder_init(st, mode, channels); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 223 | if (ret != OPUS_OK) |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 224 | { |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 225 | opus_custom_encoder_destroy(st); |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 226 | st = NULL; |
| 227 | } |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 228 | if (error) |
| 229 | *error = ret; |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 230 | return st; |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 231 | } |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 232 | #endif /* CUSTOM_MODES */ |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 233 | |
Gregory Maxwell | 64a3541 | 2011-09-02 10:31:17 -0400 | [diff] [blame] | 234 | int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels) |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 235 | { |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 236 | int ret; |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 237 | ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 238 | if (ret != OPUS_OK) |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 239 | return ret; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 240 | st->upsample = resampling_factor(sampling_rate); |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 241 | return OPUS_OK; |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 242 | } |
| 243 | |
Gregory Maxwell | afc8d53 | 2011-09-28 15:13:26 -0400 | [diff] [blame] | 244 | OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels) |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 245 | { |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 246 | if (channels < 0 || channels > 2) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 247 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 248 | |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 249 | if (st==NULL || mode==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 250 | return OPUS_ALLOC_FAIL; |
Jean-Marc Valin | 6d3829f | 2010-08-27 17:52:38 -0400 | [diff] [blame] | 251 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 252 | OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels)); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 253 | |
Jean-Marc Valin | 73e51b3 | 2007-12-05 17:48:24 +1100 | [diff] [blame] | 254 | st->mode = mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 255 | st->overlap = mode->overlap; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 256 | st->stream_channels = st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 257 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 258 | st->upsample = 1; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 259 | st->start = 0; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 260 | st->end = st->mode->effEBands; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 261 | st->signalling = 1; |
| 262 | |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 263 | st->constrained_vbr = 1; |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 264 | st->clip = 1; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 265 | |
Jean-Marc Valin | 663a7fe | 2011-09-06 14:05:19 -0400 | [diff] [blame] | 266 | st->bitrate = OPUS_BITRATE_MAX; |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 267 | st->vbr = 0; |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 268 | st->vbr_offset = 0; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 269 | st->force_intra = 0; |
Gregory Maxwell | 8842fde | 2009-05-04 15:58:40 -0400 | [diff] [blame] | 270 | st->delayedIntra = 1; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 271 | st->tonal_average = 256; |
Timothy B. Terriberry | 320cf2e | 2010-12-17 05:52:06 -0800 | [diff] [blame] | 272 | st->spread_decision = SPREAD_NORMAL; |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 273 | st->hf_average = 0; |
| 274 | st->tapset_decision = 0; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 275 | st->complexity = 5; |
Jean-Marc Valin | a76a0b2 | 2008-01-17 22:43:05 +1100 | [diff] [blame] | 276 | |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 277 | return OPUS_OK; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 278 | } |
| 279 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 280 | #ifdef CUSTOM_MODES |
| 281 | void opus_custom_encoder_destroy(CELTEncoder *st) |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 282 | { |
Jean-Marc Valin | 07f8840 | 2011-08-29 15:08:51 -0400 | [diff] [blame] | 283 | opus_free(st); |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 284 | } |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 285 | #endif /* CUSTOM_MODES */ |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 286 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 287 | static inline opus_val16 SIG2WORD16(celt_sig x) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 288 | { |
| 289 | #ifdef FIXED_POINT |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 290 | x = PSHR32(x, SIG_SHIFT); |
Jean-Marc Valin | abdfc38 | 2008-04-18 15:57:18 +1000 | [diff] [blame] | 291 | x = MAX32(x, -32768); |
| 292 | x = MIN32(x, 32767); |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 293 | return EXTRACT16(x); |
| 294 | #else |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 295 | return (opus_val16)x; |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 296 | #endif |
| 297 | } |
| 298 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 299 | static int transient_analysis(const opus_val32 * restrict in, int len, int C, |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 300 | int overlap) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 301 | { |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 302 | int i; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 303 | VARDECL(opus_val16, tmp); |
| 304 | opus_val32 mem0=0,mem1=0; |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 305 | int is_transient = 0; |
| 306 | int block; |
| 307 | int N; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 308 | VARDECL(opus_val16, bins); |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 309 | SAVE_STACK; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 310 | ALLOC(tmp, len, opus_val16); |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 311 | |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 312 | block = overlap/2; |
| 313 | N=len/block; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 314 | ALLOC(bins, N, opus_val16); |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 315 | if (C==1) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 316 | { |
| 317 | for (i=0;i<len;i++) |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 318 | tmp[i] = SHR32(in[i],SIG_SHIFT); |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 319 | } else { |
| 320 | for (i=0;i<len;i++) |
Jean-Marc Valin | 933dd83 | 2010-10-24 00:08:16 -0400 | [diff] [blame] | 321 | tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1); |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 322 | } |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 323 | |
| 324 | /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */ |
| 325 | for (i=0;i<len;i++) |
| 326 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 327 | opus_val32 x,y; |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 328 | x = tmp[i]; |
| 329 | y = ADD32(mem0, x); |
| 330 | #ifdef FIXED_POINT |
| 331 | mem0 = mem1 + y - SHL32(x,1); |
| 332 | mem1 = x - SHR32(y,1); |
| 333 | #else |
| 334 | mem0 = mem1 + y - 2*x; |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 335 | mem1 = x - .5f*y; |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 336 | #endif |
| 337 | tmp[i] = EXTRACT16(SHR(y,2)); |
| 338 | } |
| 339 | /* First few samples are bad because we don't propagate the memory */ |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 340 | for (i=0;i<12;i++) |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 341 | tmp[i] = 0; |
| 342 | |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 343 | for (i=0;i<N;i++) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 344 | { |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 345 | int j; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 346 | opus_val16 max_abs=0; |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 347 | for (j=0;j<block;j++) |
Jean-Marc Valin | 168888f | 2011-03-02 17:26:48 -0500 | [diff] [blame] | 348 | max_abs = MAX16(max_abs, ABS16(tmp[i*block+j])); |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 349 | bins[i] = max_abs; |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 350 | } |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 351 | for (i=0;i<N;i++) |
Jean-Marc Valin | 4a8c1f1 | 2010-10-13 18:03:50 -0400 | [diff] [blame] | 352 | { |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 353 | int j; |
| 354 | int conseq=0; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 355 | opus_val16 t1, t2, t3; |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 356 | |
| 357 | t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]); |
| 358 | t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]); |
| 359 | t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]); |
| 360 | for (j=0;j<i;j++) |
| 361 | { |
| 362 | if (bins[j] < t1) |
| 363 | conseq++; |
| 364 | if (bins[j] < t2) |
| 365 | conseq++; |
| 366 | else |
| 367 | conseq = 0; |
| 368 | } |
| 369 | if (conseq>=3) |
| 370 | is_transient=1; |
| 371 | conseq = 0; |
| 372 | for (j=i+1;j<N;j++) |
| 373 | { |
| 374 | if (bins[j] < t3) |
| 375 | conseq++; |
| 376 | else |
| 377 | conseq = 0; |
| 378 | } |
| 379 | if (conseq>=7) |
| 380 | is_transient=1; |
Jean-Marc Valin | 4a8c1f1 | 2010-10-13 18:03:50 -0400 | [diff] [blame] | 381 | } |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 382 | RESTORE_STACK; |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 383 | #ifdef FUZZING |
| 384 | is_transient = rand()&0x1; |
| 385 | #endif |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 386 | return is_transient; |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 387 | } |
| 388 | |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 389 | /** Apply window and compute the MDCT for all sub-frames and |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 390 | all channels in a frame */ |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 391 | static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * restrict in, celt_sig * restrict out, int _C, int LM) |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 392 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 393 | const int C = CHANNELS(_C); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 394 | if (C==1 && !shortBlocks) |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 395 | { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 396 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | a6b4e25 | 2011-08-15 10:20:06 -0400 | [diff] [blame] | 397 | clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1); |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 398 | } else { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 399 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | ce4dd36 | 2010-05-07 07:45:18 -0400 | [diff] [blame] | 400 | int N = mode->shortMdctSize<<LM; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 401 | int B = 1; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 402 | int b, c; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 403 | if (shortBlocks) |
| 404 | { |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 405 | N = mode->shortMdctSize; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 406 | B = shortBlocks; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 407 | } |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 408 | c=0; do { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 409 | for (b=0;b<B;b++) |
| 410 | { |
Jean-Marc Valin | a6b4e25 | 2011-08-15 10:20:06 -0400 | [diff] [blame] | 411 | /* Interleaving the sub-frames while doing the MDCTs */ |
| 412 | clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 413 | } |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 414 | } while (++c<C); |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 415 | } |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 416 | } |
| 417 | |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 418 | /** Compute the IMDCT and apply window for all sub-frames and |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 419 | all channels in a frame */ |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 420 | static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X, |
Jean-Marc Valin | 7a08ddd | 2010-10-18 14:55:42 -0400 | [diff] [blame] | 421 | celt_sig * restrict out_mem[], |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 422 | celt_sig * restrict overlap_mem[], int _C, int LM) |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 423 | { |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 424 | int c; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 425 | const int C = CHANNELS(_C); |
Jean-Marc Valin | ce4dd36 | 2010-05-07 07:45:18 -0400 | [diff] [blame] | 426 | const int N = mode->shortMdctSize<<LM; |
Jean-Marc Valin | b18ec0b | 2008-04-11 04:07:52 +1000 | [diff] [blame] | 427 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 428 | VARDECL(opus_val32, x); |
| 429 | SAVE_STACK; |
| 430 | |
| 431 | ALLOC(x, N+overlap, opus_val32); |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 432 | c=0; do { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 433 | int j; |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 434 | int b; |
| 435 | int N2 = N; |
| 436 | int B = 1; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 437 | |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 438 | if (shortBlocks) |
| 439 | { |
| 440 | N2 = mode->shortMdctSize; |
| 441 | B = shortBlocks; |
| 442 | } |
| 443 | /* Prevents problems from the imdct doing the overlap-add */ |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 444 | OPUS_CLEAR(x, overlap); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 445 | |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 446 | for (b=0;b<B;b++) |
| 447 | { |
| 448 | /* IMDCT on the interleaved the sub-frames */ |
| 449 | clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B); |
| 450 | } |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 451 | |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 452 | for (j=0;j<overlap;j++) |
| 453 | out_mem[c][j] = x[j] + overlap_mem[c][j]; |
| 454 | for (;j<N;j++) |
| 455 | out_mem[c][j] = x[j]; |
| 456 | for (j=0;j<overlap;j++) |
| 457 | overlap_mem[c][j] = x[N+j]; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 458 | } while (++c<C); |
Jean-Marc Valin | dbeb86f | 2011-08-15 10:01:00 -0400 | [diff] [blame] | 459 | RESTORE_STACK; |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 460 | } |
| 461 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 462 | static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int _C, int downsample, const opus_val16 *coef, celt_sig *mem) |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 463 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 464 | const int C = CHANNELS(_C); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 465 | int c; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 466 | int count=0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 467 | c=0; do { |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 468 | int j; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 469 | celt_sig * restrict x; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 470 | opus_val16 * restrict y; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 471 | celt_sig m = mem[c]; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 472 | x =in[c]; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 473 | y = pcm+c; |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 474 | for (j=0;j<N;j++) |
| 475 | { |
Jean-Marc Valin | af1fce9 | 2010-07-16 11:05:06 -0400 | [diff] [blame] | 476 | celt_sig tmp = *x + m; |
| 477 | m = MULT16_32_Q15(coef[0], tmp) |
| 478 | - MULT16_32_Q15(coef[1], *x); |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 479 | tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2); |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame] | 480 | x++; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 481 | /* Technically the store could be moved outside of the if because |
| 482 | the stores we don't want will just be overwritten */ |
| 483 | if (++count==downsample) |
| 484 | { |
| 485 | *y = SCALEOUT(SIG2WORD16(tmp)); |
| 486 | y+=C; |
| 487 | count=0; |
| 488 | } |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 489 | } |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 490 | mem[c] = m; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 491 | } while (++c<C); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 492 | } |
| 493 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 494 | static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N, |
| 495 | opus_val16 g0, opus_val16 g1, int tapset0, int tapset1, |
| 496 | const opus_val16 *window, int overlap) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 497 | { |
| 498 | int i; |
| 499 | /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */ |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 500 | opus_val16 g00, g01, g02, g10, g11, g12; |
| 501 | static const opus_val16 gains[3][3] = { |
Jean-Marc Valin | 61f4041 | 2011-01-27 17:14:33 -0500 | [diff] [blame] | 502 | {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)}, |
| 503 | {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)}, |
| 504 | {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}}; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 505 | g00 = MULT16_16_Q15(g0, gains[tapset0][0]); |
| 506 | g01 = MULT16_16_Q15(g0, gains[tapset0][1]); |
| 507 | g02 = MULT16_16_Q15(g0, gains[tapset0][2]); |
| 508 | g10 = MULT16_16_Q15(g1, gains[tapset1][0]); |
| 509 | g11 = MULT16_16_Q15(g1, gains[tapset1][1]); |
| 510 | g12 = MULT16_16_Q15(g1, gains[tapset1][2]); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 511 | for (i=0;i<overlap;i++) |
| 512 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 513 | opus_val16 f; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 514 | f = MULT16_16_Q15(window[i],window[i]); |
| 515 | y[i] = x[i] |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 516 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0]) |
| 517 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1]) |
| 518 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1]) |
| 519 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2]) |
| 520 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2]) |
| 521 | + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1]) |
| 522 | + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1]) |
| 523 | + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1]) |
| 524 | + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2]) |
| 525 | + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 526 | |
| 527 | } |
| 528 | for (i=overlap;i<N;i++) |
| 529 | y[i] = x[i] |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 530 | + MULT16_32_Q15(g10,x[i-T1]) |
| 531 | + MULT16_32_Q15(g11,x[i-T1-1]) |
| 532 | + MULT16_32_Q15(g11,x[i-T1+1]) |
| 533 | + MULT16_32_Q15(g12,x[i-T1-2]) |
| 534 | + MULT16_32_Q15(g12,x[i-T1+2]); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 535 | } |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 536 | |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 537 | static const signed char tf_select_table[4][8] = { |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 538 | {0, -1, 0, -1, 0,-1, 0,-1}, |
Jean-Marc Valin | 41a15e6 | 2010-12-27 16:33:38 -0500 | [diff] [blame] | 539 | {0, -1, 0, -2, 1, 0, 1,-1}, |
| 540 | {0, -2, 0, -3, 2, 0, 1,-1}, |
Jean-Marc Valin | 2b13401 | 2011-01-12 16:13:46 -0500 | [diff] [blame] | 541 | {0, -2, 0, -3, 3, 0, 1,-1}, |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 542 | }; |
| 543 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 544 | static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, int width) |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 545 | { |
| 546 | int i, j; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 547 | static const opus_val16 sqrtM_1[4] = {Q15ONE, QCONST16(.70710678f,15), QCONST16(0.5f,15), QCONST16(0.35355339f,15)}; |
| 548 | opus_val32 L1; |
| 549 | opus_val16 bias; |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 550 | L1=0; |
| 551 | for (i=0;i<1<<LM;i++) |
| 552 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 553 | opus_val32 L2 = 0; |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 554 | for (j=0;j<N>>LM;j++) |
| 555 | L2 = MAC16_16(L2, tmp[(j<<LM)+i], tmp[(j<<LM)+i]); |
| 556 | L1 += celt_sqrt(L2); |
| 557 | } |
| 558 | L1 = MULT16_32_Q15(sqrtM_1[LM], L1); |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 559 | if (width==1) |
| 560 | bias = QCONST16(.12f,15)*LM; |
| 561 | else if (width==2) |
| 562 | bias = QCONST16(.05f,15)*LM; |
| 563 | else |
| 564 | bias = QCONST16(.02f,15)*LM; |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 565 | L1 = MAC16_32_Q15(L1, bias, L1); |
| 566 | return L1; |
| 567 | } |
| 568 | |
Gregory Maxwell | 06d57b2 | 2011-08-01 22:02:25 -0400 | [diff] [blame] | 569 | static int tf_analysis(const CELTMode *m, int len, int C, int isTransient, |
| 570 | int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM, |
| 571 | int *tf_sum) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 572 | { |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 573 | int i; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 574 | VARDECL(int, metric); |
| 575 | int cost0; |
| 576 | int cost1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 577 | VARDECL(int, path0); |
| 578 | VARDECL(int, path1); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 579 | VARDECL(celt_norm, tmp); |
| 580 | int lambda; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 581 | int tf_select=0; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 582 | SAVE_STACK; |
| 583 | |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 584 | if (nbCompressedBytes<15*C) |
| 585 | { |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 586 | *tf_sum = 0; |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 587 | for (i=0;i<len;i++) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 588 | tf_res[i] = isTransient; |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 589 | return 0; |
| 590 | } |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 591 | if (nbCompressedBytes<40) |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 592 | lambda = 12; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 593 | else if (nbCompressedBytes<60) |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 594 | lambda = 6; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 595 | else if (nbCompressedBytes<100) |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 596 | lambda = 4; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 597 | else |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 598 | lambda = 3; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 599 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 600 | ALLOC(metric, len, int); |
| 601 | ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm); |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 602 | ALLOC(path0, len, int); |
| 603 | ALLOC(path1, len, int); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 604 | |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 605 | *tf_sum = 0; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 606 | for (i=0;i<len;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 607 | { |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 608 | int j, k, N; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 609 | opus_val32 L1, best_L1; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 610 | int best_level=0; |
| 611 | N = (m->eBands[i+1]-m->eBands[i])<<LM; |
| 612 | for (j=0;j<N;j++) |
| 613 | tmp[j] = X[j+(m->eBands[i]<<LM)]; |
Jean-Marc Valin | 20e4c6a | 2010-12-21 22:27:08 -0500 | [diff] [blame] | 614 | /* Just add the right channel if we're in stereo */ |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 615 | if (C==2) |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 616 | for (j=0;j<N;j++) |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 617 | tmp[j] = ADD16(tmp[j],X[N0+j+(m->eBands[i]<<LM)]); |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 618 | L1 = l1_metric(tmp, N, isTransient ? LM : 0, N>>LM); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 619 | best_L1 = L1; |
| 620 | /*printf ("%f ", L1);*/ |
| 621 | for (k=0;k<LM;k++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 622 | { |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 623 | int B; |
| 624 | |
| 625 | if (isTransient) |
| 626 | B = (LM-k-1); |
| 627 | else |
| 628 | B = k+1; |
| 629 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 630 | if (isTransient) |
| 631 | haar1(tmp, N>>(LM-k), 1<<(LM-k)); |
| 632 | else |
| 633 | haar1(tmp, N>>k, 1<<k); |
| 634 | |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 635 | L1 = l1_metric(tmp, N, B, N>>LM); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 636 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 637 | if (L1 < best_L1) |
| 638 | { |
| 639 | best_L1 = L1; |
| 640 | best_level = k+1; |
| 641 | } |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 642 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 643 | /*printf ("%d ", isTransient ? LM-best_level : best_level);*/ |
| 644 | if (isTransient) |
| 645 | metric[i] = best_level; |
| 646 | else |
| 647 | metric[i] = -best_level; |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 648 | *tf_sum += metric[i]; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 649 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 650 | /*printf("\n");*/ |
Jean-Marc Valin | 9267506 | 2011-09-11 22:00:46 -0400 | [diff] [blame] | 651 | /* NOTE: Future optimized implementations could detect extreme transients and set |
| 652 | tf_select = 1 but so far we have not found a reliable way of making this useful */ |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 653 | tf_select = 0; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 654 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 655 | cost0 = 0; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 656 | cost1 = isTransient ? 0 : lambda; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 657 | /* Viterbi forward pass */ |
| 658 | for (i=1;i<len;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 659 | { |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 660 | int curr0, curr1; |
| 661 | int from0, from1; |
Jean-Marc Valin | 581fdba | 2010-05-28 06:56:23 -0400 | [diff] [blame] | 662 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 663 | from0 = cost0; |
| 664 | from1 = cost1 + lambda; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 665 | if (from0 < from1) |
| 666 | { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 667 | curr0 = from0; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 668 | path0[i]= 0; |
| 669 | } else { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 670 | curr0 = from1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 671 | path0[i]= 1; |
| 672 | } |
| 673 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 674 | from0 = cost0 + lambda; |
| 675 | from1 = cost1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 676 | if (from0 < from1) |
| 677 | { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 678 | curr1 = from0; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 679 | path1[i]= 0; |
| 680 | } else { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 681 | curr1 = from1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 682 | path1[i]= 1; |
| 683 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 684 | cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]); |
| 685 | cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]); |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 686 | } |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 687 | tf_res[len-1] = cost0 < cost1 ? 0 : 1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 688 | /* Viterbi backward pass to check the decisions */ |
| 689 | for (i=len-2;i>=0;i--) |
| 690 | { |
| 691 | if (tf_res[i+1] == 1) |
| 692 | tf_res[i] = path1[i+1]; |
| 693 | else |
| 694 | tf_res[i] = path0[i+1]; |
| 695 | } |
Jean-Marc Valin | 71ae6d4 | 2010-06-27 21:55:08 -0400 | [diff] [blame] | 696 | RESTORE_STACK; |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 697 | #ifdef FUZZING |
| 698 | tf_select = rand()&0x1; |
| 699 | tf_res[0] = rand()&0x1; |
| 700 | for (i=1;i<len;i++) |
| 701 | tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0); |
| 702 | #endif |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 703 | return tf_select; |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 704 | } |
| 705 | |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 706 | static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc) |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 707 | { |
| 708 | int curr, i; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 709 | int tf_select_rsv; |
| 710 | int tf_changed; |
| 711 | int logp; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 712 | opus_uint32 budget; |
| 713 | opus_uint32 tell; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 714 | budget = enc->storage*8; |
| 715 | tell = ec_tell(enc); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 716 | logp = isTransient ? 2 : 4; |
| 717 | /* Reserve space to code the tf_select decision. */ |
| 718 | tf_select_rsv = LM>0 && tell+logp+1 <= budget; |
| 719 | budget -= tf_select_rsv; |
| 720 | curr = tf_changed = 0; |
| 721 | for (i=start;i<end;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 722 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 723 | if (tell+logp<=budget) |
| 724 | { |
| 725 | ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 726 | tell = ec_tell(enc); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 727 | curr = tf_res[i]; |
| 728 | tf_changed |= curr; |
| 729 | } |
| 730 | else |
| 731 | tf_res[i] = curr; |
| 732 | logp = isTransient ? 4 : 5; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 733 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 734 | /* Only code tf_select if it would actually make a difference. */ |
| 735 | if (tf_select_rsv && |
| 736 | tf_select_table[LM][4*isTransient+0+tf_changed]!= |
| 737 | tf_select_table[LM][4*isTransient+2+tf_changed]) |
| 738 | ec_enc_bit_logp(enc, tf_select, 1); |
| 739 | else |
| 740 | tf_select = 0; |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 741 | for (i=start;i<end;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 742 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 743 | /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/ |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 744 | } |
| 745 | |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 746 | static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 747 | { |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 748 | int i, curr, tf_select; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 749 | int tf_select_rsv; |
| 750 | int tf_changed; |
| 751 | int logp; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 752 | opus_uint32 budget; |
| 753 | opus_uint32 tell; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 754 | |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 755 | budget = dec->storage*8; |
| 756 | tell = ec_tell(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 757 | logp = isTransient ? 2 : 4; |
| 758 | tf_select_rsv = LM>0 && tell+logp+1<=budget; |
| 759 | budget -= tf_select_rsv; |
| 760 | tf_changed = curr = 0; |
| 761 | for (i=start;i<end;i++) |
Timothy B. Terriberry | 509ad20 | 2010-12-27 18:20:20 -0800 | [diff] [blame] | 762 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 763 | if (tell+logp<=budget) |
| 764 | { |
| 765 | curr ^= ec_dec_bit_logp(dec, logp); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 766 | tell = ec_tell(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 767 | tf_changed |= curr; |
| 768 | } |
| 769 | tf_res[i] = curr; |
| 770 | logp = isTransient ? 4 : 5; |
| 771 | } |
| 772 | tf_select = 0; |
| 773 | if (tf_select_rsv && |
| 774 | tf_select_table[LM][4*isTransient+0+tf_changed] != |
| 775 | tf_select_table[LM][4*isTransient+2+tf_changed]) |
| 776 | { |
| 777 | tf_select = ec_dec_bit_logp(dec, 1); |
| 778 | } |
| 779 | for (i=start;i<end;i++) |
| 780 | { |
| 781 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; |
Timothy B. Terriberry | 509ad20 | 2010-12-27 18:20:20 -0800 | [diff] [blame] | 782 | } |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 783 | } |
| 784 | |
Timothy B. Terriberry | ce6d090 | 2011-02-01 17:41:12 -0800 | [diff] [blame] | 785 | static void init_caps(const CELTMode *m,int *cap,int LM,int C) |
| 786 | { |
| 787 | int i; |
| 788 | for (i=0;i<m->nbEBands;i++) |
| 789 | { |
| 790 | int N; |
| 791 | N=(m->eBands[i+1]-m->eBands[i])<<LM; |
| 792 | cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2; |
| 793 | } |
| 794 | } |
| 795 | |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 796 | static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X, |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 797 | const opus_val16 *bandLogE, int end, int LM, int C, int N0) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 798 | { |
| 799 | int i; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 800 | opus_val32 diff=0; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 801 | int c; |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 802 | int trim_index = 5; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 803 | if (C==2) |
| 804 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 805 | opus_val16 sum = 0; /* Q10 */ |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 806 | /* Compute inter-channel correlation for low frequencies */ |
| 807 | for (i=0;i<8;i++) |
| 808 | { |
| 809 | int j; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 810 | opus_val32 partial = 0; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 811 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) |
| 812 | partial = MAC16_16(partial, X[j], X[N0+j]); |
| 813 | sum = ADD16(sum, EXTRACT16(SHR32(partial, 18))); |
| 814 | } |
| 815 | sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum); |
| 816 | /*printf ("%f\n", sum);*/ |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 817 | if (sum > QCONST16(.995f,10)) |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 818 | trim_index-=4; |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 819 | else if (sum > QCONST16(.92f,10)) |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 820 | trim_index-=3; |
| 821 | else if (sum > QCONST16(.85f,10)) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 822 | trim_index-=2; |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 823 | else if (sum > QCONST16(.8f,10)) |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 824 | trim_index-=1; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 825 | } |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 826 | |
| 827 | /* Estimate spectral tilt */ |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 828 | c=0; do { |
Gregory Maxwell | 7007f1b | 2011-02-08 16:17:47 -0500 | [diff] [blame] | 829 | for (i=0;i<end-1;i++) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 830 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 831 | diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands); |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 832 | } |
Gregory Maxwell | f93d81b | 2011-08-07 00:57:31 -0400 | [diff] [blame] | 833 | } while (++c<C); |
Jean-Marc Valin | 817220e | 2011-08-08 11:33:17 -0400 | [diff] [blame] | 834 | /* We divide by two here to avoid making the tilt larger for stereo as a |
| 835 | result of a bug in the loop above */ |
| 836 | diff /= 2*C*(end-1); |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 837 | /*printf("%f\n", diff);*/ |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 838 | if (diff > QCONST16(2.f, DB_SHIFT)) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 839 | trim_index--; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 840 | if (diff > QCONST16(8.f, DB_SHIFT)) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 841 | trim_index--; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 842 | if (diff < -QCONST16(4.f, DB_SHIFT)) |
Jean-Marc Valin | 9037757 | 2010-10-22 15:12:01 -0400 | [diff] [blame] | 843 | trim_index++; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 844 | if (diff < -QCONST16(10.f, DB_SHIFT)) |
| 845 | trim_index++; |
| 846 | |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 847 | if (trim_index<0) |
| 848 | trim_index = 0; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 849 | if (trim_index>10) |
| 850 | trim_index = 10; |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 851 | #ifdef FUZZING |
| 852 | trim_index = rand()%11; |
| 853 | #endif |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 854 | return trim_index; |
| 855 | } |
| 856 | |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 857 | static int stereo_analysis(const CELTMode *m, const celt_norm *X, |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 858 | int LM, int N0) |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 859 | { |
| 860 | int i; |
| 861 | int thetas; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 862 | opus_val32 sumLR = EPSILON, sumMS = EPSILON; |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 863 | |
| 864 | /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */ |
| 865 | for (i=0;i<13;i++) |
| 866 | { |
| 867 | int j; |
| 868 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) |
| 869 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 870 | opus_val16 L, R, M, S; |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 871 | L = X[j]; |
| 872 | R = X[N0+j]; |
| 873 | M = L+R; |
| 874 | S = L-R; |
| 875 | sumLR += EXTEND32(ABS16(L)) + EXTEND32(ABS16(R)); |
| 876 | sumMS += EXTEND32(ABS16(M)) + EXTEND32(ABS16(S)); |
| 877 | } |
| 878 | } |
| 879 | sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS); |
| 880 | thetas = 13; |
| 881 | /* We don't need thetas for lower bands with LM<=1 */ |
| 882 | if (LM<=1) |
| 883 | thetas -= 8; |
| 884 | return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS) |
| 885 | > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR); |
| 886 | } |
| 887 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 888 | int celt_encode_with_ec(CELTEncoder * restrict st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 889 | { |
Jean-Marc Valin | 7a08ddd | 2010-10-18 14:55:42 -0400 | [diff] [blame] | 890 | int i, c, N; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 891 | opus_int32 bits; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 892 | ec_enc _enc; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 893 | VARDECL(celt_sig, in); |
| 894 | VARDECL(celt_sig, freq); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 895 | VARDECL(celt_norm, X); |
| 896 | VARDECL(celt_ener, bandE); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 897 | VARDECL(opus_val16, bandLogE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 898 | VARDECL(int, fine_quant); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 899 | VARDECL(opus_val16, error); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 900 | VARDECL(int, pulses); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 901 | VARDECL(int, cap); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 902 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 903 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 904 | VARDECL(int, tf_res); |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 905 | VARDECL(unsigned char, collapse_masks); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 906 | celt_sig *prefilter_mem; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 907 | opus_val16 *oldBandE, *oldLogE, *oldLogE2; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 908 | int shortBlocks=0; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 909 | int isTransient=0; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 910 | const int CC = CHANNELS(st->channels); |
| 911 | const int C = CHANNELS(st->stream_channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 912 | int LM, M; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 913 | int tf_select; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 914 | int nbFilledBytes, nbAvailableBytes; |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 915 | int effEnd; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 916 | int codedBands; |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 917 | int tf_sum; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 918 | int alloc_trim; |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 919 | int pitch_index=COMBFILTER_MINPERIOD; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 920 | opus_val16 gain1 = 0; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 921 | int intensity=0; |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 922 | int dual_stereo=0; |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 923 | int effectiveBytes; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 924 | opus_val16 pf_threshold; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 925 | int dynalloc_logp; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 926 | opus_int32 vbr_rate; |
| 927 | opus_int32 total_bits; |
| 928 | opus_int32 total_boost; |
| 929 | opus_int32 balance; |
| 930 | opus_int32 tell; |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 931 | int prefilter_tapset=0; |
| 932 | int pf_on; |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 933 | int anti_collapse_rsv; |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 934 | int anti_collapse_on=0; |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 935 | int silence=0; |
Jean-Marc Valin | f62b3bb | 2011-03-09 11:56:29 -0500 | [diff] [blame] | 936 | ALLOC_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 937 | |
Jean-Marc Valin | b3dae4b | 2011-02-04 21:50:54 -0500 | [diff] [blame] | 938 | if (nbCompressedBytes<2 || pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 939 | return OPUS_BAD_ARG; |
Gregory Maxwell | 520eeae | 2009-02-09 01:33:21 -0500 | [diff] [blame] | 940 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 941 | frame_size *= st->upsample; |
Gregory Maxwell | 95becbe | 2011-02-03 21:06:43 -0500 | [diff] [blame] | 942 | for (LM=0;LM<=st->mode->maxLM;LM++) |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 943 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 944 | break; |
Gregory Maxwell | 95becbe | 2011-02-03 21:06:43 -0500 | [diff] [blame] | 945 | if (LM>st->mode->maxLM) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 946 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 947 | M=1<<LM; |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 948 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 949 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 950 | prefilter_mem = st->in_mem+CC*(st->overlap); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 951 | oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD)); |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 952 | oldLogE = oldBandE + CC*st->mode->nbEBands; |
| 953 | oldLogE2 = oldLogE + CC*st->mode->nbEBands; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 954 | |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 955 | if (enc==NULL) |
| 956 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 957 | tell=1; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 958 | nbFilledBytes=0; |
| 959 | } else { |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 960 | tell=ec_tell(enc); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 961 | nbFilledBytes=(tell+4)>>3; |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 962 | } |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 963 | |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 964 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 965 | if (st->signalling && enc==NULL) |
| 966 | { |
| 967 | int tmp = (st->mode->effEBands-st->end)>>1; |
| 968 | st->end = IMAX(1, st->mode->effEBands-tmp); |
| 969 | compressed[0] = tmp<<5; |
| 970 | compressed[0] |= LM<<3; |
| 971 | compressed[0] |= (C==2)<<2; |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 972 | /* Convert "standard mode" to Opus header */ |
| 973 | if (st->mode->Fs==48000 && st->mode->shortMdctSize==120) |
| 974 | { |
| 975 | int c0 = toOpus(compressed[0]); |
| 976 | if (c0<0) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 977 | return OPUS_BAD_ARG; |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 978 | compressed[0] = c0; |
| 979 | } |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 980 | compressed++; |
| 981 | nbCompressedBytes--; |
| 982 | } |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 983 | #else |
| 984 | celt_assert(st->signalling==0); |
| 985 | #endif |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 986 | |
| 987 | /* Can't produce more than 1275 output bytes */ |
| 988 | nbCompressedBytes = IMIN(nbCompressedBytes,1275); |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 989 | nbAvailableBytes = nbCompressedBytes - nbFilledBytes; |
| 990 | |
Jean-Marc Valin | 663a7fe | 2011-09-06 14:05:19 -0400 | [diff] [blame] | 991 | if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX) |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 992 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 993 | opus_int32 den=st->mode->Fs>>BITRES; |
Jean-Marc Valin | 23340e2 | 2011-02-03 23:21:00 -0500 | [diff] [blame] | 994 | vbr_rate=(st->bitrate*frame_size+(den>>1))/den; |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 995 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 996 | if (st->signalling) |
| 997 | vbr_rate -= 8<<BITRES; |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 998 | #endif |
Jean-Marc Valin | 3beb70e | 2011-03-01 18:08:15 -0500 | [diff] [blame] | 999 | effectiveBytes = vbr_rate>>(3+BITRES); |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1000 | } else { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1001 | opus_int32 tmp; |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1002 | vbr_rate = 0; |
| 1003 | tmp = st->bitrate*frame_size; |
| 1004 | if (tell>1) |
| 1005 | tmp += tell; |
Jean-Marc Valin | 663a7fe | 2011-09-06 14:05:19 -0400 | [diff] [blame] | 1006 | if (st->bitrate!=OPUS_BITRATE_MAX) |
Jean-Marc Valin | 66ff26f | 2011-08-15 22:37:11 -0400 | [diff] [blame] | 1007 | nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes, |
| 1008 | (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling)); |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1009 | effectiveBytes = nbCompressedBytes; |
| 1010 | } |
| 1011 | |
| 1012 | if (enc==NULL) |
| 1013 | { |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1014 | ec_enc_init(&_enc, compressed, nbCompressedBytes); |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1015 | enc = &_enc; |
| 1016 | } |
| 1017 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1018 | if (vbr_rate>0) |
| 1019 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1020 | /* Computes the max bit-rate allowed in VBR mode to avoid violating the |
| 1021 | target rate and buffering. |
| 1022 | We must do this up front so that bust-prevention logic triggers |
| 1023 | correctly if we don't have enough bits. */ |
| 1024 | if (st->constrained_vbr) |
| 1025 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1026 | opus_int32 vbr_bound; |
| 1027 | opus_int32 max_allowed; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1028 | /* We could use any multiple of vbr_rate as bound (depending on the |
| 1029 | delay). |
Gregory Maxwell | 420c325 | 2011-01-27 22:35:50 -0500 | [diff] [blame] | 1030 | This is clamped to ensure we use at least two bytes if the encoder |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1031 | was entirely empty, but to allow 0 in hybrid mode. */ |
| 1032 | vbr_bound = vbr_rate; |
Gregory Maxwell | 420c325 | 2011-01-27 22:35:50 -0500 | [diff] [blame] | 1033 | max_allowed = IMIN(IMAX(tell==1?2:0, |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 1034 | (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)), |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1035 | nbAvailableBytes); |
| 1036 | if(max_allowed < nbAvailableBytes) |
| 1037 | { |
| 1038 | nbCompressedBytes = nbFilledBytes+max_allowed; |
| 1039 | nbAvailableBytes = max_allowed; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1040 | ec_enc_shrink(enc, nbCompressedBytes); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1041 | } |
| 1042 | } |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1043 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1044 | total_bits = nbCompressedBytes*8; |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 1045 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1046 | effEnd = st->end; |
| 1047 | if (effEnd > st->mode->effEBands) |
| 1048 | effEnd = st->mode->effEBands; |
| 1049 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1050 | ALLOC(in, CC*(N+st->overlap), celt_sig); |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1051 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1052 | /* Find pitch period and gain */ |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 1053 | { |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1054 | VARDECL(celt_sig, _pre); |
| 1055 | celt_sig *pre[2]; |
| 1056 | SAVE_STACK; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1057 | ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1058 | |
| 1059 | pre[0] = _pre; |
| 1060 | pre[1] = _pre + (N+COMBFILTER_MAXPERIOD); |
| 1061 | |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 1062 | silence = 1; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1063 | c=0; do { |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1064 | int count = 0; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1065 | const opus_val16 * restrict pcmp = pcm+c; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1066 | celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap; |
| 1067 | |
| 1068 | for (i=0;i<N;i++) |
| 1069 | { |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1070 | celt_sig x, tmp; |
| 1071 | |
| 1072 | x = SCALEIN(*pcmp); |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 1073 | #ifndef FIXED_POINT |
Gregory Maxwell | 58ecb1a | 2011-05-09 13:16:30 -0400 | [diff] [blame] | 1074 | if (!(x==x)) |
| 1075 | x = 0; |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 1076 | if (st->clip) |
| 1077 | x = MAX32(-65536.f, MIN32(65536.f,x)); |
| 1078 | #endif |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1079 | if (++count==st->upsample) |
| 1080 | { |
| 1081 | count=0; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1082 | pcmp+=CC; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1083 | } else { |
| 1084 | x = 0; |
| 1085 | } |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1086 | /* Apply pre-emphasis */ |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1087 | tmp = MULT16_16(st->mode->preemph[2], x); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1088 | *inp = tmp + st->preemph_memE[c]; |
| 1089 | st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp) |
| 1090 | - MULT16_32_Q15(st->mode->preemph[0], tmp); |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 1091 | silence = silence && *inp == 0; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1092 | inp++; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1093 | } |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1094 | OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD); |
| 1095 | OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N); |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1096 | } while (++c<CC); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1097 | |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 1098 | #ifdef FUZZING |
| 1099 | if ((rand()&0x3F)==0) |
| 1100 | silence = 1; |
| 1101 | #endif |
Jean-Marc Valin | 65d35a3 | 2011-01-26 22:04:59 -0500 | [diff] [blame] | 1102 | if (tell==1) |
| 1103 | ec_enc_bit_logp(enc, silence, 15); |
| 1104 | else |
| 1105 | silence=0; |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 1106 | if (silence) |
| 1107 | { |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1108 | /*In VBR mode there is no need to send more than the minimum. */ |
| 1109 | if (vbr_rate>0) |
| 1110 | { |
Jean-Marc Valin | 65d35a3 | 2011-01-26 22:04:59 -0500 | [diff] [blame] | 1111 | effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2); |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1112 | total_bits=nbCompressedBytes*8; |
| 1113 | nbAvailableBytes=2; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1114 | ec_enc_shrink(enc, nbCompressedBytes); |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1115 | } |
Jean-Marc Valin | 13a7c26 | 2011-01-26 10:58:33 -0500 | [diff] [blame] | 1116 | /* Pretend we've filled all the remaining bits with zeros |
| 1117 | (that's what the initialiser did anyway) */ |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 1118 | tell = nbCompressedBytes*8; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1119 | enc->nbits_total+=tell-ec_tell(enc); |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 1120 | } |
Jean-Marc Valin | d539c6b | 2011-02-03 13:36:03 -0500 | [diff] [blame] | 1121 | if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1122 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1123 | VARDECL(opus_val16, pitch_buf); |
| 1124 | ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1125 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1126 | pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC); |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 1127 | pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N, |
Jean-Marc Valin | e3e2c26 | 2011-01-26 13:09:53 -0500 | [diff] [blame] | 1128 | COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1129 | pitch_index = COMBFILTER_MAXPERIOD-pitch_index; |
| 1130 | |
| 1131 | gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD, |
| 1132 | N, &pitch_index, st->prefilter_period, st->prefilter_gain); |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1133 | if (pitch_index > COMBFILTER_MAXPERIOD-2) |
| 1134 | pitch_index = COMBFILTER_MAXPERIOD-2; |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 1135 | gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1); |
Jean-Marc Valin | 6965388 | 2011-04-21 10:41:13 -0400 | [diff] [blame] | 1136 | if (st->loss_rate>2) |
| 1137 | gain1 = HALF32(gain1); |
| 1138 | if (st->loss_rate>4) |
| 1139 | gain1 = HALF32(gain1); |
| 1140 | if (st->loss_rate>8) |
| 1141 | gain1 = 0; |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 1142 | prefilter_tapset = st->tapset_decision; |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 1143 | } else { |
| 1144 | gain1 = 0; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1145 | } |
Jean-Marc Valin | 6ba0b35 | 2010-12-20 11:40:30 -0500 | [diff] [blame] | 1146 | |
| 1147 | /* Gain threshold for enabling the prefilter/postfilter */ |
| 1148 | pf_threshold = QCONST16(.2f,15); |
| 1149 | |
| 1150 | /* Adjusting the threshold based on rate and continuity */ |
| 1151 | if (abs(pitch_index-st->prefilter_period)*10>pitch_index) |
| 1152 | pf_threshold += QCONST16(.2f,15); |
| 1153 | if (nbAvailableBytes<25) |
| 1154 | pf_threshold += QCONST16(.1f,15); |
| 1155 | if (nbAvailableBytes<35) |
| 1156 | pf_threshold += QCONST16(.1f,15); |
| 1157 | if (st->prefilter_gain > QCONST16(.4f,15)) |
| 1158 | pf_threshold -= QCONST16(.1f,15); |
| 1159 | if (st->prefilter_gain > QCONST16(.55f,15)) |
| 1160 | pf_threshold -= QCONST16(.1f,15); |
| 1161 | |
| 1162 | /* Hard threshold at 0.2 */ |
| 1163 | pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15)); |
| 1164 | if (gain1<pf_threshold) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1165 | { |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1166 | if(st->start==0 && tell+16<=total_bits) |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1167 | ec_enc_bit_logp(enc, 0, 1); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1168 | gain1 = 0; |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 1169 | pf_on = 0; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1170 | } else { |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1171 | /*This block is not gated by a total bits check only because |
| 1172 | of the nbAvailableBytes check above.*/ |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1173 | int qg; |
| 1174 | int octave; |
Jean-Marc Valin | 6ba0b35 | 2010-12-20 11:40:30 -0500 | [diff] [blame] | 1175 | |
Jean-Marc Valin | b417d83 | 2011-01-27 17:19:49 -0500 | [diff] [blame] | 1176 | if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15)) |
Jean-Marc Valin | 6ba0b35 | 2010-12-20 11:40:30 -0500 | [diff] [blame] | 1177 | gain1=st->prefilter_gain; |
| 1178 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1179 | #ifdef FIXED_POINT |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1180 | qg = ((gain1+1536)>>10)/3-1; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1181 | #else |
Gregory Maxwell | 662587d | 2011-08-01 20:41:54 -0400 | [diff] [blame] | 1182 | qg = (int)floor(.5f+gain1*32/3)-1; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1183 | #endif |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1184 | qg = IMAX(0, IMIN(7, qg)); |
Timothy B. Terriberry | e86fb26 | 2010-12-17 14:50:19 -0800 | [diff] [blame] | 1185 | ec_enc_bit_logp(enc, 1, 1); |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1186 | pitch_index += 1; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1187 | octave = EC_ILOG(pitch_index)-5; |
| 1188 | ec_enc_uint(enc, octave, 6); |
| 1189 | ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave); |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1190 | pitch_index -= 1; |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1191 | ec_enc_bits(enc, qg, 3); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1192 | if (ec_tell(enc)+2<=total_bits) |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 1193 | ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2); |
| 1194 | else |
| 1195 | prefilter_tapset = 0; |
| 1196 | gain1 = QCONST16(0.09375f,15)*(qg+1); |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 1197 | pf_on = 1; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1198 | } |
| 1199 | /*printf("%d %f\n", pitch_index, gain1);*/ |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1200 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1201 | c=0; do { |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 1202 | int offset = st->mode->shortMdctSize-st->mode->overlap; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1203 | st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1204 | OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap); |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 1205 | if (offset) |
| 1206 | comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD, |
| 1207 | st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain, |
| 1208 | st->prefilter_tapset, st->prefilter_tapset, NULL, 0); |
| 1209 | |
| 1210 | comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset, |
| 1211 | st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1, |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1212 | st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap); |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1213 | OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1214 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1215 | if (N>COMBFILTER_MAXPERIOD) |
| 1216 | { |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1217 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1218 | } else { |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1219 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N); |
| 1220 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1221 | } |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1222 | } while (++c<CC); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1223 | |
| 1224 | RESTORE_STACK; |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 1225 | } |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 1226 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1227 | isTransient = 0; |
| 1228 | shortBlocks = 0; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1229 | if (LM>0 && ec_tell(enc)+3<=total_bits) |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1230 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1231 | if (st->complexity > 1) |
| 1232 | { |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1233 | isTransient = transient_analysis(in, N+st->overlap, CC, |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 1234 | st->overlap); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1235 | if (isTransient) |
| 1236 | shortBlocks = M; |
| 1237 | } |
| 1238 | ec_enc_bit_logp(enc, isTransient, 3); |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1239 | } |
Jean-Marc Valin | c5f2a9d | 2008-10-26 22:00:26 -0400 | [diff] [blame] | 1240 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1241 | ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 1242 | ALLOC(bandE,st->mode->nbEBands*CC, celt_ener); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1243 | ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16); |
Jean-Marc Valin | 32ec58c | 2009-05-01 21:28:58 -0400 | [diff] [blame] | 1244 | /* Compute MDCTs */ |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1245 | compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM); |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 1246 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1247 | if (CC==2&&C==1) |
| 1248 | { |
| 1249 | for (i=0;i<N;i++) |
| 1250 | freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i])); |
| 1251 | } |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1252 | if (st->upsample != 1) |
| 1253 | { |
| 1254 | c=0; do |
| 1255 | { |
| 1256 | int bound = N/st->upsample; |
| 1257 | for (i=0;i<bound;i++) |
| 1258 | freq[c*N+i] *= st->upsample; |
| 1259 | for (;i<N;i++) |
| 1260 | freq[c*N+i] = 0; |
| 1261 | } while (++c<C); |
| 1262 | } |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1263 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
Jean-Marc Valin | 8d4ac15 | 2008-02-29 17:24:02 +1100 | [diff] [blame] | 1264 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1265 | compute_band_energies(st->mode, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | 504fb3c | 2010-08-06 15:56:22 -0400 | [diff] [blame] | 1266 | |
| 1267 | amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1268 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 1269 | /* Band normalisation */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1270 | normalise_bands(st->mode, freq, X, bandE, effEnd, C, M); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 1271 | |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 1272 | ALLOC(tf_res, st->mode->nbEBands, int); |
Gregory Maxwell | 06d57b2 | 2011-08-01 22:02:25 -0400 | [diff] [blame] | 1273 | tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum); |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 1274 | for (i=effEnd;i<st->end;i++) |
| 1275 | tf_res[i] = tf_res[effEnd-1]; |
| 1276 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1277 | ALLOC(error, C*st->mode->nbEBands, opus_val16); |
Jean-Marc Valin | 5e7f02d | 2010-08-08 09:48:22 -0400 | [diff] [blame] | 1278 | quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE, |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1279 | oldBandE, total_bits, error, enc, |
Timothy B. Terriberry | ef2e650 | 2010-11-09 01:43:18 -0800 | [diff] [blame] | 1280 | C, LM, nbAvailableBytes, st->force_intra, |
Jean-Marc Valin | eda2dee | 2011-04-21 16:04:27 -0400 | [diff] [blame] | 1281 | &st->delayedIntra, st->complexity >= 4, st->loss_rate); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1282 | |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 1283 | tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc); |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1284 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1285 | st->spread_decision = SPREAD_NORMAL; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1286 | if (ec_tell(enc)+4<=total_bits) |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 1287 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1288 | if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C) |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1289 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1290 | if (st->complexity == 0) |
| 1291 | st->spread_decision = SPREAD_NONE; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1292 | } else { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1293 | st->spread_decision = spreading_decision(st->mode, X, |
Jean-Marc Valin | 8d36702 | 2011-01-17 16:37:51 -0500 | [diff] [blame] | 1294 | &st->tonal_average, st->spread_decision, &st->hf_average, |
| 1295 | &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M); |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1296 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1297 | ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5); |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 1298 | } |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1299 | |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 1300 | ALLOC(cap, st->mode->nbEBands, int); |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1301 | ALLOC(offsets, st->mode->nbEBands, int); |
| 1302 | |
Timothy B. Terriberry | ce6d090 | 2011-02-01 17:41:12 -0800 | [diff] [blame] | 1303 | init_caps(st->mode,cap,LM,C); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 1304 | for (i=0;i<st->mode->nbEBands;i++) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1305 | offsets[i] = 0; |
| 1306 | /* Dynamic allocation code */ |
| 1307 | /* Make sure that dynamic allocation can't make us bust the budget */ |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 1308 | if (effectiveBytes > 50 && LM>=1) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1309 | { |
| 1310 | int t1, t2; |
| 1311 | if (LM <= 1) |
| 1312 | { |
| 1313 | t1 = 3; |
| 1314 | t2 = 5; |
| 1315 | } else { |
| 1316 | t1 = 2; |
| 1317 | t2 = 4; |
| 1318 | } |
Gregory Maxwell | 7007f1b | 2011-02-08 16:17:47 -0500 | [diff] [blame] | 1319 | for (i=st->start+1;i<st->end-1;i++) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1320 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1321 | opus_val32 d2; |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 1322 | d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1]; |
| 1323 | if (C==2) |
| 1324 | d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]- |
| 1325 | bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]); |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 1326 | #ifdef FUZZING |
| 1327 | if((rand()&0xF)==0) |
| 1328 | { |
| 1329 | offsets[i] += 1; |
| 1330 | if((rand()&0x3)==0) |
| 1331 | offsets[i] += 1+(rand()&0x3); |
| 1332 | } |
| 1333 | #else |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 1334 | if (d2 > SHL16(t1,DB_SHIFT)) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1335 | offsets[i] += 1; |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 1336 | if (d2 > SHL16(t2,DB_SHIFT)) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1337 | offsets[i] += 1; |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 1338 | #endif |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1339 | } |
| 1340 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1341 | dynalloc_logp = 6; |
| 1342 | total_bits<<=BITRES; |
| 1343 | total_boost = 0; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1344 | tell = ec_tell_frac(enc); |
Timothy B. Terriberry | d6f6157 | 2010-12-30 09:04:16 -0800 | [diff] [blame] | 1345 | for (i=st->start;i<st->end;i++) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1346 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1347 | int width, quanta; |
| 1348 | int dynalloc_loop_logp; |
| 1349 | int boost; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1350 | int j; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1351 | width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM; |
| 1352 | /* quanta is 6 bits, but no more than 1 bit/sample |
| 1353 | and no less than 1/8 bit/sample */ |
| 1354 | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); |
| 1355 | dynalloc_loop_logp = dynalloc_logp; |
| 1356 | boost = 0; |
| 1357 | for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 1358 | && boost < cap[i]; j++) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1359 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1360 | int flag; |
| 1361 | flag = j<offsets[i]; |
| 1362 | ec_enc_bit_logp(enc, flag, dynalloc_loop_logp); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1363 | tell = ec_tell_frac(enc); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1364 | if (!flag) |
| 1365 | break; |
| 1366 | boost += quanta; |
| 1367 | total_boost += quanta; |
| 1368 | dynalloc_loop_logp = 1; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1369 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1370 | /* Making dynalloc more likely */ |
| 1371 | if (j) |
| 1372 | dynalloc_logp = IMAX(2, dynalloc_logp-1); |
| 1373 | offsets[i] = boost; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1374 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1375 | alloc_trim = 5; |
| 1376 | if (tell+(6<<BITRES) <= total_bits - total_boost) |
| 1377 | { |
| 1378 | alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE, |
Gregory Maxwell | 7007f1b | 2011-02-08 16:17:47 -0500 | [diff] [blame] | 1379 | st->end, LM, C, N); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1380 | ec_enc_icdf(enc, alloc_trim, trim_icdf, 7); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1381 | tell = ec_tell_frac(enc); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1382 | } |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1383 | |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1384 | /* Variable bitrate */ |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1385 | if (vbr_rate>0) |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1386 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1387 | opus_val16 alpha; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1388 | opus_int32 delta; |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1389 | /* The target rate in 8th bits per frame */ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1390 | opus_int32 target; |
| 1391 | opus_int32 min_allowed; |
Gregory Maxwell | de0b977 | 2011-08-23 02:00:55 -0400 | [diff] [blame] | 1392 | int lm_diff = st->mode->maxLM - LM; |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1393 | |
Gregory Maxwell | de0b977 | 2011-08-23 02:00:55 -0400 | [diff] [blame] | 1394 | target = vbr_rate + (st->vbr_offset>>lm_diff) - ((40*C+20)<<BITRES); |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 1395 | |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1396 | /* Shortblocks get a large boost in bitrate, but since they |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 1397 | are uncommon long blocks are not greatly affected */ |
| 1398 | if (shortBlocks || tf_sum < -2*(st->end-st->start)) |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 1399 | target = 7*target/4; |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 1400 | else if (tf_sum < -(st->end-st->start)) |
| 1401 | target = 3*target/2; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1402 | else if (M > 1) |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 1403 | target-=(target+14)/28; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1404 | |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1405 | /* The current offset is removed from the target and the space used |
| 1406 | so far is added*/ |
Jean-Marc Valin | a4badac | 2010-12-03 15:20:11 -0500 | [diff] [blame] | 1407 | target=target+tell; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1408 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1409 | /* In VBR mode the frame size must not be reduced so much that it would |
| 1410 | result in the encoder running out of bits. |
| 1411 | The margin of 2 bytes ensures that none of the bust-prevention logic |
| 1412 | in the decoder will have triggered so far. */ |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 1413 | min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes; |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1414 | |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 1415 | nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3); |
Gregory Maxwell | d85018c | 2011-01-13 14:38:24 -0500 | [diff] [blame] | 1416 | nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes); |
| 1417 | nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes; |
Jean-Marc Valin | 6b56526 | 2011-01-12 11:27:03 -0500 | [diff] [blame] | 1418 | |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1419 | /* By how much did we "miss" the target on that frame */ |
| 1420 | delta = target - vbr_rate; |
| 1421 | |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1422 | target=nbAvailableBytes<<(BITRES+3); |
| 1423 | |
Gregory Maxwell | fdd8675 | 2011-04-13 17:08:22 -0400 | [diff] [blame] | 1424 | /*If the frame is silent we don't adjust our drift, otherwise |
| 1425 | the encoder will shoot to very high rates after hitting a |
| 1426 | span of silence, but we do allow the bitres to refill. |
| 1427 | This means that we'll undershoot our target in CVBR/VBR modes |
| 1428 | on files with lots of silence. */ |
| 1429 | if(silence) |
| 1430 | { |
| 1431 | nbAvailableBytes = 2; |
| 1432 | target = 2*8<<BITRES; |
| 1433 | delta = 0; |
| 1434 | } |
| 1435 | |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1436 | if (st->vbr_count < 970) |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1437 | { |
| 1438 | st->vbr_count++; |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1439 | alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16)); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1440 | } else |
| 1441 | alpha = QCONST16(.001f,15); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1442 | /* How many bits have we used in excess of what we're allowed */ |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 1443 | if (st->constrained_vbr) |
| 1444 | st->vbr_reservoir += target - vbr_rate; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1445 | /*printf ("%d\n", st->vbr_reservoir);*/ |
| 1446 | |
| 1447 | /* Compute the offset we need to apply in order to reach the target */ |
Jean-Marc Valin | 803ec8c | 2011-09-23 23:48:40 -0400 | [diff] [blame] | 1448 | st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1449 | st->vbr_offset = -st->vbr_drift; |
| 1450 | /*printf ("%d\n", st->vbr_drift);*/ |
| 1451 | |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 1452 | if (st->constrained_vbr && st->vbr_reservoir < 0) |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1453 | { |
| 1454 | /* We're under the min value -- increase rate */ |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1455 | int adjust = (-st->vbr_reservoir)/(8<<BITRES); |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1456 | /* Unless we're just coding silence */ |
| 1457 | nbAvailableBytes += silence?0:adjust; |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1458 | st->vbr_reservoir = 0; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1459 | /*printf ("+%d\n", adjust);*/ |
| 1460 | } |
Gregory Maxwell | d85018c | 2011-01-13 14:38:24 -0500 | [diff] [blame] | 1461 | nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1462 | /* This moves the raw bits to take into account the new compressed size */ |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1463 | ec_enc_shrink(enc, nbCompressedBytes); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1464 | } |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1465 | if (C==2) |
| 1466 | { |
| 1467 | int effectiveRate; |
| 1468 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1469 | /* Always use MS for 2.5 ms frames until we can do a better analysis */ |
| 1470 | if (LM!=0) |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 1471 | dual_stereo = stereo_analysis(st->mode, X, LM, N); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 1472 | |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1473 | /* Account for coarse energy */ |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 1474 | effectiveRate = (8*effectiveBytes - 80)>>LM; |
| 1475 | |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1476 | /* effectiveRate in kb/s */ |
| 1477 | effectiveRate = 2*effectiveRate/5; |
| 1478 | if (effectiveRate<35) |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 1479 | intensity = 8; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1480 | else if (effectiveRate<50) |
| 1481 | intensity = 12; |
| 1482 | else if (effectiveRate<68) |
| 1483 | intensity = 16; |
| 1484 | else if (effectiveRate<84) |
| 1485 | intensity = 18; |
| 1486 | else if (effectiveRate<102) |
| 1487 | intensity = 19; |
| 1488 | else if (effectiveRate<130) |
| 1489 | intensity = 20; |
| 1490 | else |
| 1491 | intensity = 100; |
| 1492 | intensity = IMIN(st->end,IMAX(st->start, intensity)); |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1493 | } |
| 1494 | |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1495 | /* Bit allocation */ |
| 1496 | ALLOC(fine_quant, st->mode->nbEBands, int); |
| 1497 | ALLOC(pulses, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1498 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 1499 | |
Timothy B. Terriberry | 285bc37 | 2011-02-06 13:29:00 -0800 | [diff] [blame] | 1500 | /* bits = packet size - where we are - safety*/ |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 1501 | bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1; |
| 1502 | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 1503 | bits -= anti_collapse_rsv; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 1504 | codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap, |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 1505 | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, |
| 1506 | fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands); |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 1507 | st->lastCodedBands = codedBands; |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 1508 | |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 1509 | quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1510 | |
Jean-Marc Valin | 7b5a086 | 2010-07-29 15:01:24 +0200 | [diff] [blame] | 1511 | #ifdef MEASURE_NORM_MSE |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 1512 | float X0[3000]; |
| 1513 | float bandE0[60]; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1514 | c=0; do |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 1515 | for (i=0;i<N;i++) |
| 1516 | X0[i+c*N] = X[i+c*N]; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1517 | while (++c<C); |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 1518 | for (i=0;i<C*st->mode->nbEBands;i++) |
Jean-Marc Valin | 7b5a086 | 2010-07-29 15:01:24 +0200 | [diff] [blame] | 1519 | bandE0[i] = bandE[i]; |
| 1520 | #endif |
| 1521 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 1522 | /* Residual quantisation */ |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 1523 | ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char); |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 1524 | quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks, |
Gregory Maxwell | d830d08 | 2011-08-15 16:05:40 -0400 | [diff] [blame] | 1525 | bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res, |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 1526 | nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng); |
Jean-Marc Valin | 3971053 | 2009-06-09 00:10:32 -0400 | [diff] [blame] | 1527 | |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 1528 | if (anti_collapse_rsv > 0) |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 1529 | { |
| 1530 | anti_collapse_on = st->consec_transient<2; |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 1531 | #ifdef FUZZING |
| 1532 | anti_collapse_on = rand()&0x1; |
| 1533 | #endif |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 1534 | ec_enc_bits(enc, anti_collapse_on, 1); |
| 1535 | } |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1536 | quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C); |
Jean-Marc Valin | 3971053 | 2009-06-09 00:10:32 -0400 | [diff] [blame] | 1537 | |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1538 | if (silence) |
| 1539 | { |
| 1540 | for (i=0;i<C*st->mode->nbEBands;i++) |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1541 | oldBandE[i] = -QCONST16(28.f,DB_SHIFT); |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 1542 | } |
| 1543 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1544 | #ifdef RESYNTH |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 1545 | /* Re-synthesis of the coded audio if required */ |
Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 1546 | { |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 1547 | celt_sig *out_mem[2]; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1548 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 1549 | |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1550 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); |
Jean-Marc Valin | 4b000c3 | 2011-01-26 20:30:21 -0500 | [diff] [blame] | 1551 | if (silence) |
| 1552 | { |
| 1553 | for (i=0;i<C*st->mode->nbEBands;i++) |
| 1554 | bandE[i] = 0; |
| 1555 | } |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 1556 | |
| 1557 | #ifdef MEASURE_NORM_MSE |
| 1558 | measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C); |
| 1559 | #endif |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 1560 | if (anti_collapse_on) |
| 1561 | { |
Timothy B. Terriberry | 682b6cf | 2011-01-31 13:34:54 -0800 | [diff] [blame] | 1562 | anti_collapse(st->mode, X, collapse_masks, LM, C, CC, N, |
Jean-Marc Valin | 63fb61f | 2011-01-20 22:52:55 -0500 | [diff] [blame] | 1563 | st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 1564 | } |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 1565 | |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1566 | /* Synthesis */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1567 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1568 | |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1569 | OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD); |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1570 | if (CC==2) |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1571 | OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1572 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1573 | c=0; do |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 1574 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 1575 | freq[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1576 | while (++c<C); |
| 1577 | c=0; do |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 1578 | for (i=M*st->mode->eBands[st->end];i<N;i++) |
| 1579 | freq[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1580 | while (++c<C); |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 1581 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1582 | if (CC==2&&C==1) |
| 1583 | { |
| 1584 | for (i=0;i<N;i++) |
| 1585 | freq[N+i] = freq[i]; |
| 1586 | } |
| 1587 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1588 | out_mem[0] = st->syn_mem[0]+MAX_PERIOD; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1589 | if (CC==2) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1590 | out_mem[1] = st->syn_mem[1]+MAX_PERIOD; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1591 | |
Gregory Maxwell | a45724e | 2011-07-29 11:53:45 -0400 | [diff] [blame] | 1592 | overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD; |
| 1593 | if (CC==2) |
| 1594 | overlap_mem[1] = overlap_mem[0] + st->overlap; |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 1595 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1596 | compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 1597 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1598 | c=0; do { |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1599 | st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); |
| 1600 | st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD); |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 1601 | comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize, |
Jean-Marc Valin | a14e86d | 2011-02-11 16:27:27 -0500 | [diff] [blame] | 1602 | st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset, |
| 1603 | st->mode->window, st->overlap); |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1604 | if (LM!=0) |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 1605 | comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize, |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1606 | st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset, |
| 1607 | st->mode->window, st->mode->overlap); |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1608 | } while (++c<CC); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1609 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1610 | deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD); |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1611 | st->prefilter_period_old = st->prefilter_period; |
| 1612 | st->prefilter_gain_old = st->prefilter_gain; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1613 | st->prefilter_tapset_old = st->prefilter_tapset; |
Jean-Marc Valin | d9b9565 | 2008-08-31 23:34:47 -0400 | [diff] [blame] | 1614 | } |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1615 | #endif |
| 1616 | |
| 1617 | st->prefilter_period = pitch_index; |
| 1618 | st->prefilter_gain = gain1; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1619 | st->prefilter_tapset = prefilter_tapset; |
Jean-Marc Valin | a14e86d | 2011-02-11 16:27:27 -0500 | [diff] [blame] | 1620 | #ifdef RESYNTH |
| 1621 | if (LM!=0) |
| 1622 | { |
| 1623 | st->prefilter_period_old = st->prefilter_period; |
| 1624 | st->prefilter_gain_old = st->prefilter_gain; |
| 1625 | st->prefilter_tapset_old = st->prefilter_tapset; |
| 1626 | } |
| 1627 | #endif |
Gregory Maxwell | 54547f1 | 2009-02-16 18:56:44 -0500 | [diff] [blame] | 1628 | |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1629 | if (CC==2&&C==1) { |
| 1630 | for (i=0;i<st->mode->nbEBands;i++) |
| 1631 | oldBandE[st->mode->nbEBands+i]=oldBandE[i]; |
| 1632 | } |
| 1633 | |
Jean-Marc Valin | 5677e34 | 2011-01-13 16:15:53 -0500 | [diff] [blame] | 1634 | /* In case start or end were to change */ |
Jean-Marc Valin | 5c2ac2b | 2011-01-22 14:48:20 -0500 | [diff] [blame] | 1635 | c=0; do |
| 1636 | { |
| 1637 | for (i=0;i<st->start;i++) |
| 1638 | oldBandE[c*st->mode->nbEBands+i]=0; |
| 1639 | for (i=st->end;i<st->mode->nbEBands;i++) |
| 1640 | oldBandE[c*st->mode->nbEBands+i]=0; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1641 | } while (++c<CC); |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 1642 | if (!isTransient) |
| 1643 | { |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1644 | for (i=0;i<CC*st->mode->nbEBands;i++) |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 1645 | oldLogE2[i] = oldLogE[i]; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1646 | for (i=0;i<CC*st->mode->nbEBands;i++) |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 1647 | oldLogE[i] = oldBandE[i]; |
Jean-Marc Valin | 47e905d | 2011-01-27 18:05:47 -0500 | [diff] [blame] | 1648 | } else { |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1649 | for (i=0;i<CC*st->mode->nbEBands;i++) |
Jean-Marc Valin | 47e905d | 2011-01-27 18:05:47 -0500 | [diff] [blame] | 1650 | oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 1651 | } |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 1652 | if (isTransient) |
| 1653 | st->consec_transient++; |
| 1654 | else |
| 1655 | st->consec_transient=0; |
Jean-Marc Valin | 63fb61f | 2011-01-20 22:52:55 -0500 | [diff] [blame] | 1656 | st->rng = enc->rng; |
Jean-Marc Valin | 5677e34 | 2011-01-13 16:15:53 -0500 | [diff] [blame] | 1657 | |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 1658 | /* If there's any room left (can only happen for very high rates), |
Jean-Marc Valin | 13a7c26 | 2011-01-26 10:58:33 -0500 | [diff] [blame] | 1659 | it's already filled with zeros */ |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1660 | ec_enc_done(enc); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1661 | |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 1662 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 1663 | if (st->signalling) |
| 1664 | nbCompressedBytes++; |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 1665 | #endif |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 1666 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1667 | RESTORE_STACK; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 1668 | if (ec_get_error(enc)) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1669 | return OPUS_INTERNAL_ERROR; |
Jean-Marc Valin | 9d785af | 2010-07-18 09:42:05 -0400 | [diff] [blame] | 1670 | else |
| 1671 | return nbCompressedBytes; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 1672 | } |
| 1673 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1674 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1675 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1676 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1677 | #ifdef FIXED_POINT |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1678 | int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1679 | { |
| 1680 | return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL); |
| 1681 | } |
| 1682 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1683 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1684 | int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1685 | { |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 1686 | int j, ret, C, N; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1687 | VARDECL(opus_int16, in); |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 1688 | ALLOC_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1689 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1690 | if (pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1691 | return OPUS_BAD_ARG; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1692 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1693 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 1694 | N = frame_size; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1695 | ALLOC(in, C*N, opus_int16); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1696 | |
| 1697 | for (j=0;j<C*N;j++) |
| 1698 | in[j] = FLOAT2INT16(pcm[j]); |
| 1699 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1700 | ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1701 | #ifdef RESYNTH |
| 1702 | for (j=0;j<C*N;j++) |
| 1703 | ((float*)pcm)[j]=in[j]*(1.f/32768.f); |
| 1704 | #endif |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1705 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1706 | return ret; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1707 | } |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1708 | #endif /* DISABLE_FLOAT_API */ |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1709 | #else |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1710 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1711 | int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1712 | { |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 1713 | int j, ret, C, N; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1714 | VARDECL(celt_sig, in); |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 1715 | ALLOC_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1716 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1717 | if (pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1718 | return OPUS_BAD_ARG; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1719 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1720 | C=CHANNELS(st->channels); |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 1721 | N=frame_size; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1722 | ALLOC(in, C*N, celt_sig); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1723 | for (j=0;j<C*N;j++) { |
| 1724 | in[j] = SCALEOUT(pcm[j]); |
| 1725 | } |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1726 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1727 | ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1728 | #ifdef RESYNTH |
| 1729 | for (j=0;j<C*N;j++) |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1730 | ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1731 | #endif |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1732 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1733 | return ret; |
| 1734 | } |
Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 1735 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1736 | int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1737 | { |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1738 | return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL); |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1739 | } |
| 1740 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 1741 | #endif |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1742 | |
| 1743 | #endif /* CUSTOM_MODES */ |
| 1744 | |
| 1745 | int opus_custom_encoder_ctl(CELTEncoder * restrict st, int request, ...) |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1746 | { |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1747 | va_list ap; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1748 | |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1749 | va_start(ap, request); |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1750 | switch (request) |
| 1751 | { |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1752 | case OPUS_SET_COMPLEXITY_REQUEST: |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1753 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1754 | int value = va_arg(ap, opus_int32); |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1755 | if (value<0 || value>10) |
| 1756 | goto bad_arg; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1757 | st->complexity = value; |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1758 | } |
| 1759 | break; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1760 | case CELT_SET_START_BAND_REQUEST: |
| 1761 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1762 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1763 | if (value<0 || value>=st->mode->nbEBands) |
| 1764 | goto bad_arg; |
| 1765 | st->start = value; |
| 1766 | } |
| 1767 | break; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1768 | case CELT_SET_END_BAND_REQUEST: |
| 1769 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1770 | opus_int32 value = va_arg(ap, opus_int32); |
Timothy B. Terriberry | 8893e53 | 2010-12-30 08:56:49 -0800 | [diff] [blame] | 1771 | if (value<1 || value>st->mode->nbEBands) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1772 | goto bad_arg; |
| 1773 | st->end = value; |
| 1774 | } |
| 1775 | break; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1776 | case CELT_SET_PREDICTION_REQUEST: |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1777 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1778 | int value = va_arg(ap, opus_int32); |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1779 | if (value<0 || value>2) |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1780 | goto bad_arg; |
Jean-Marc Valin | d539c6b | 2011-02-03 13:36:03 -0500 | [diff] [blame] | 1781 | st->disable_pf = value<=1; |
| 1782 | st->force_intra = value==0; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1783 | } |
| 1784 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1785 | case OPUS_SET_PACKET_LOSS_PERC_REQUEST: |
Jean-Marc Valin | 6965388 | 2011-04-21 10:41:13 -0400 | [diff] [blame] | 1786 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1787 | int value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 6965388 | 2011-04-21 10:41:13 -0400 | [diff] [blame] | 1788 | if (value<0 || value>100) |
| 1789 | goto bad_arg; |
| 1790 | st->loss_rate = value; |
| 1791 | } |
| 1792 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1793 | case OPUS_SET_VBR_CONSTRAINT_REQUEST: |
Jean-Marc Valin | 79b34eb | 2010-12-05 17:22:06 -0500 | [diff] [blame] | 1794 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1795 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 79b34eb | 2010-12-05 17:22:06 -0500 | [diff] [blame] | 1796 | st->constrained_vbr = value; |
| 1797 | } |
| 1798 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1799 | case OPUS_SET_VBR_REQUEST: |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1800 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1801 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1802 | st->vbr = value; |
| 1803 | } |
| 1804 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1805 | case OPUS_SET_BITRATE_REQUEST: |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1806 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1807 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 663a7fe | 2011-09-06 14:05:19 -0400 | [diff] [blame] | 1808 | if (value<=500 && value!=OPUS_BITRATE_MAX) |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1809 | goto bad_arg; |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 1810 | value = IMIN(value, 260000*st->channels); |
Jean-Marc Valin | 7bb26e1 | 2011-02-01 17:04:27 -0500 | [diff] [blame] | 1811 | st->bitrate = value; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1812 | } |
| 1813 | break; |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1814 | case CELT_SET_CHANNELS_REQUEST: |
| 1815 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1816 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 00a98f5 | 2011-01-31 11:19:03 -0500 | [diff] [blame] | 1817 | if (value<1 || value>2) |
| 1818 | goto bad_arg; |
| 1819 | st->stream_channels = value; |
| 1820 | } |
| 1821 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1822 | case OPUS_RESET_STATE: |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1823 | { |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 1824 | OPUS_CLEAR((char*)&st->ENCODER_RESET_START, |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1825 | opus_custom_encoder_get_size(st->mode, st->channels)- |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1826 | ((char*)&st->ENCODER_RESET_START - (char*)st)); |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 1827 | st->vbr_offset = 0; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1828 | st->delayedIntra = 1; |
Timothy B. Terriberry | 320cf2e | 2010-12-17 05:52:06 -0800 | [diff] [blame] | 1829 | st->spread_decision = SPREAD_NORMAL; |
Gregory Maxwell | ec8008b | 2011-08-30 16:50:01 -0400 | [diff] [blame] | 1830 | st->tonal_average = 256; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1831 | } |
| 1832 | break; |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 1833 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 1834 | case CELT_SET_INPUT_CLIPPING_REQUEST: |
| 1835 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1836 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 0b405d1 | 2011-02-04 01:03:42 -0500 | [diff] [blame] | 1837 | st->clip = value; |
| 1838 | } |
| 1839 | break; |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 1840 | #endif |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 1841 | case CELT_SET_SIGNALLING_REQUEST: |
| 1842 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1843 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 1844 | st->signalling = value; |
| 1845 | } |
| 1846 | break; |
| 1847 | case CELT_GET_MODE_REQUEST: |
| 1848 | { |
| 1849 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 1850 | if (value==0) |
| 1851 | goto bad_arg; |
| 1852 | *value=st->mode; |
| 1853 | } |
| 1854 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1855 | case OPUS_GET_FINAL_RANGE_REQUEST: |
Jean-Marc Valin | 06cee2b | 2011-08-19 16:11:41 -0400 | [diff] [blame] | 1856 | { |
| 1857 | opus_uint32 * value = va_arg(ap, opus_uint32 *); |
| 1858 | if (value==0) |
| 1859 | goto bad_arg; |
| 1860 | *value=st->rng; |
| 1861 | } |
| 1862 | break; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1863 | default: |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1864 | goto bad_request; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1865 | } |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1866 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1867 | return OPUS_OK; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1868 | bad_arg: |
| 1869 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1870 | return OPUS_BAD_ARG; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1871 | bad_request: |
| 1872 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1873 | return OPUS_UNIMPLEMENTED; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1874 | } |
| 1875 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 1876 | /**********************************************************************/ |
| 1877 | /* */ |
| 1878 | /* DECODER */ |
| 1879 | /* */ |
| 1880 | /**********************************************************************/ |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1881 | #define DECODE_BUFFER_SIZE 2048 |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1882 | |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1883 | /** Decoder state |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 1884 | @brief Decoder state |
| 1885 | */ |
Jean-Marc Valin | 242da53 | 2011-09-05 20:53:33 -0400 | [diff] [blame] | 1886 | struct OpusCustomDecoder { |
| 1887 | const OpusCustomMode *mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1888 | int overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1889 | int channels; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 1890 | int stream_channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1891 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1892 | int downsample; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1893 | int start, end; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 1894 | int signalling; |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1895 | |
| 1896 | /* Everything beyond this point gets cleared on a reset */ |
Jean-Marc Valin | 63fb61f | 2011-01-20 22:52:55 -0500 | [diff] [blame] | 1897 | #define DECODER_RESET_START rng |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1898 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 1899 | opus_uint32 rng; |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 1900 | int error; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1901 | int last_pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1902 | int loss_count; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1903 | int postfilter_period; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1904 | int postfilter_period_old; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1905 | opus_val16 postfilter_gain; |
| 1906 | opus_val16 postfilter_gain_old; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 1907 | int postfilter_tapset; |
| 1908 | int postfilter_tapset_old; |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1909 | |
| 1910 | celt_sig preemph_memD[2]; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 1911 | |
Jean-Marc Valin | ca8b992 | 2010-08-27 16:23:03 -0400 | [diff] [blame] | 1912 | celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */ |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1913 | /* opus_val16 lpc[], Size = channels*LPC_ORDER */ |
| 1914 | /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */ |
| 1915 | /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */ |
| 1916 | /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */ |
| 1917 | /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1918 | }; |
| 1919 | |
Jean-Marc Valin | 8cf29f0 | 2011-01-30 23:38:28 -0500 | [diff] [blame] | 1920 | int celt_decoder_get_size(int channels) |
| 1921 | { |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1922 | const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL); |
| 1923 | return opus_custom_decoder_get_size(mode, channels); |
Jean-Marc Valin | 8cf29f0 | 2011-01-30 23:38:28 -0500 | [diff] [blame] | 1924 | } |
| 1925 | |
Gregory Maxwell | afc8d53 | 2011-09-28 15:13:26 -0400 | [diff] [blame] | 1926 | OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_get_size(const CELTMode *mode, int channels) |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1927 | { |
| 1928 | int size = sizeof(struct CELTDecoder) |
| 1929 | + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig) |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1930 | + channels*LPC_ORDER*sizeof(opus_val16) |
| 1931 | + 4*2*mode->nbEBands*sizeof(opus_val16); |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1932 | return size; |
| 1933 | } |
| 1934 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1935 | #ifdef CUSTOM_MODES |
| 1936 | CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error) |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 1937 | { |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1938 | int ret; |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1939 | CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels)); |
| 1940 | ret = opus_custom_decoder_init(st, mode, channels); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1941 | if (ret != OPUS_OK) |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 1942 | { |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1943 | opus_custom_decoder_destroy(st); |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 1944 | st = NULL; |
| 1945 | } |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1946 | if (error) |
| 1947 | *error = ret; |
Jean-Marc Valin | d6c3d3c | 2011-01-30 10:23:40 -0500 | [diff] [blame] | 1948 | return st; |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 1949 | } |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1950 | #endif /* CUSTOM_MODES */ |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1951 | |
Gregory Maxwell | 64a3541 | 2011-09-02 10:31:17 -0400 | [diff] [blame] | 1952 | int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels) |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 1953 | { |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1954 | int ret; |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1955 | ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1956 | if (ret != OPUS_OK) |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1957 | return ret; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1958 | st->downsample = resampling_factor(sampling_rate); |
| 1959 | if (st->downsample==0) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1960 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 875f8db | 2011-08-31 16:43:08 -0400 | [diff] [blame] | 1961 | else |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1962 | return OPUS_OK; |
Jean-Marc Valin | c97b258 | 2011-01-28 23:07:32 -0500 | [diff] [blame] | 1963 | } |
| 1964 | |
Gregory Maxwell | afc8d53 | 2011-09-28 15:13:26 -0400 | [diff] [blame] | 1965 | OPUS_CUSTOM_NOSTATIC int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels) |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 1966 | { |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1967 | if (channels < 0 || channels > 2) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1968 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1969 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1970 | if (st==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1971 | return OPUS_ALLOC_FAIL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1972 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1973 | OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels)); |
Jean-Marc Valin | 6d3829f | 2010-08-27 17:52:38 -0400 | [diff] [blame] | 1974 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1975 | st->mode = mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1976 | st->overlap = mode->overlap; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 1977 | st->stream_channels = st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1978 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 1979 | st->downsample = 1; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1980 | st->start = 0; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 1981 | st->end = st->mode->effEBands; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 1982 | st->signalling = 1; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1983 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1984 | st->loss_count = 0; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1985 | |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 1986 | return OPUS_OK; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1987 | } |
| 1988 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1989 | #ifdef CUSTOM_MODES |
| 1990 | void opus_custom_decoder_destroy(CELTDecoder *st) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1991 | { |
Jean-Marc Valin | 07f8840 | 2011-08-29 15:08:51 -0400 | [diff] [blame] | 1992 | opus_free(st); |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1993 | } |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 1994 | #endif /* CUSTOM_MODES */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1995 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 1996 | static void celt_decode_lost(CELTDecoder * restrict st, opus_val16 * restrict pcm, int N, int LM) |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1997 | { |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1998 | int c; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1999 | int pitch_index; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2000 | int overlap = st->mode->overlap; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2001 | opus_val16 fade = Q15ONE; |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 2002 | int i, len; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 2003 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 2004 | int offset; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2005 | celt_sig *out_mem[2]; |
| 2006 | celt_sig *decode_mem[2]; |
| 2007 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2008 | opus_val16 *lpc; |
| 2009 | opus_val32 *out_syn[2]; |
| 2010 | opus_val16 *oldBandE, *oldLogE2, *backgroundLogE; |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2011 | int plc=1; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 2012 | SAVE_STACK; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2013 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2014 | c=0; do { |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2015 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); |
| 2016 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 2017 | overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2018 | } while (++c<C); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2019 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C); |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2020 | oldBandE = lpc+C*LPC_ORDER; |
| 2021 | oldLogE2 = oldBandE + C*st->mode->nbEBands; |
| 2022 | backgroundLogE = oldLogE2 + C*st->mode->nbEBands; |
| 2023 | |
| 2024 | out_syn[0] = out_mem[0]+MAX_PERIOD-N; |
| 2025 | if (C==2) |
| 2026 | out_syn[1] = out_mem[1]+MAX_PERIOD-N; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2027 | |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 2028 | len = N+st->mode->overlap; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2029 | |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2030 | if (st->loss_count >= 5) |
| 2031 | { |
| 2032 | VARDECL(celt_sig, freq); |
| 2033 | VARDECL(celt_norm, X); |
| 2034 | VARDECL(celt_ener, bandE); |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2035 | opus_uint32 seed; |
Jean-Marc Valin | 1a8bf37 | 2011-03-16 22:03:15 -0400 | [diff] [blame] | 2036 | int effEnd; |
| 2037 | |
| 2038 | effEnd = st->end; |
| 2039 | if (effEnd > st->mode->effEBands) |
| 2040 | effEnd = st->mode->effEBands; |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2041 | |
| 2042 | ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 2043 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
| 2044 | ALLOC(bandE, st->mode->nbEBands*C, celt_ener); |
| 2045 | |
| 2046 | log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C); |
| 2047 | |
| 2048 | seed = st->rng; |
Timothy B. Terriberry | 115fa35 | 2011-03-02 15:28:08 -0800 | [diff] [blame] | 2049 | for (c=0;c<C;c++) |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2050 | { |
Jean-Marc Valin | 1a8bf37 | 2011-03-16 22:03:15 -0400 | [diff] [blame] | 2051 | for (i=0;i<(st->mode->eBands[st->start]<<LM);i++) |
| 2052 | X[c*N+i] = 0; |
Timothy B. Terriberry | 115fa35 | 2011-03-02 15:28:08 -0800 | [diff] [blame] | 2053 | for (i=0;i<st->mode->effEBands;i++) |
| 2054 | { |
| 2055 | int j; |
| 2056 | int boffs; |
| 2057 | int blen; |
| 2058 | boffs = N*c+(st->mode->eBands[i]<<LM); |
| 2059 | blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM; |
| 2060 | for (j=0;j<blen;j++) |
| 2061 | { |
Jean-Marc Valin | 7fc2fbd | 2011-09-01 13:40:39 -0400 | [diff] [blame] | 2062 | seed = celt_lcg_rand(seed); |
Gregory Maxwell | ec8008b | 2011-08-30 16:50:01 -0400 | [diff] [blame] | 2063 | X[boffs+j] = (celt_norm)((opus_int32)seed>>20); |
Timothy B. Terriberry | 115fa35 | 2011-03-02 15:28:08 -0800 | [diff] [blame] | 2064 | } |
| 2065 | renormalise_vector(X+boffs, blen, Q15ONE); |
| 2066 | } |
Jean-Marc Valin | 1a8bf37 | 2011-03-16 22:03:15 -0400 | [diff] [blame] | 2067 | for (i=(st->mode->eBands[st->end]<<LM);i<N;i++) |
| 2068 | X[c*N+i] = 0; |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2069 | } |
| 2070 | st->rng = seed; |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2071 | |
Timothy B. Terriberry | 115fa35 | 2011-03-02 15:28:08 -0800 | [diff] [blame] | 2072 | denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM); |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2073 | |
Jean-Marc Valin | 1a8bf37 | 2011-03-16 22:03:15 -0400 | [diff] [blame] | 2074 | c=0; do |
| 2075 | for (i=0;i<st->mode->eBands[st->start]<<LM;i++) |
| 2076 | freq[c*N+i] = 0; |
| 2077 | while (++c<C); |
| 2078 | c=0; do { |
| 2079 | int bound = st->mode->eBands[effEnd]<<LM; |
| 2080 | if (st->downsample!=1) |
| 2081 | bound = IMIN(bound, N/st->downsample); |
| 2082 | for (i=bound;i<N;i++) |
| 2083 | freq[c*N+i] = 0; |
| 2084 | } while (++c<C); |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2085 | compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM); |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2086 | plc = 0; |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2087 | } else if (st->loss_count == 0) |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 2088 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2089 | opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1]; |
Jean-Marc Valin | 5f3d1af | 2011-05-13 17:24:25 -0400 | [diff] [blame] | 2090 | /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this |
| 2091 | search by using only part of the decode buffer */ |
| 2092 | int poffset = 720; |
| 2093 | pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C); |
| 2094 | /* Max pitch is 100 samples (480 Hz) */ |
| 2095 | pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset, |
| 2096 | poffset-100, &pitch_index); |
| 2097 | pitch_index = poffset-pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 2098 | st->last_pitch_index = pitch_index; |
| 2099 | } else { |
| 2100 | pitch_index = st->last_pitch_index; |
Jean-Marc Valin | eafd8a7 | 2011-01-23 00:24:45 -0500 | [diff] [blame] | 2101 | fade = QCONST16(.8f,15); |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 2102 | } |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 2103 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2104 | if (plc) |
| 2105 | { |
| 2106 | c=0; do { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2107 | VARDECL(opus_val32, e); |
| 2108 | opus_val16 exc[MAX_PERIOD]; |
| 2109 | opus_val32 ac[LPC_ORDER+1]; |
| 2110 | opus_val16 decay = 1; |
| 2111 | opus_val32 S1=0; |
Gregory Maxwell | 06d57b2 | 2011-08-01 22:02:25 -0400 | [diff] [blame] | 2112 | opus_val16 mem[LPC_ORDER]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2113 | |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2114 | ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32); |
Jean-Marc Valin | f54a0a3 | 2011-05-13 17:36:31 -0400 | [diff] [blame] | 2115 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2116 | offset = MAX_PERIOD-pitch_index; |
| 2117 | for (i=0;i<MAX_PERIOD;i++) |
| 2118 | exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 2119 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2120 | if (st->loss_count == 0) |
| 2121 | { |
| 2122 | _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap, |
| 2123 | LPC_ORDER, MAX_PERIOD); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 2124 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2125 | /* Noise floor -40 dB */ |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2126 | #ifdef FIXED_POINT |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2127 | ac[0] += SHR32(ac[0],13); |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2128 | #else |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2129 | ac[0] *= 1.0001f; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2130 | #endif |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2131 | /* Lag windowing */ |
| 2132 | for (i=1;i<=LPC_ORDER;i++) |
| 2133 | { |
| 2134 | /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/ |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2135 | #ifdef FIXED_POINT |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2136 | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2137 | #else |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2138 | ac[i] -= ac[i]*(.008f*i)*(.008f*i); |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 2139 | #endif |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER); |
| 2143 | } |
| 2144 | for (i=0;i<LPC_ORDER;i++) |
| 2145 | mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT); |
Jean-Marc Valin | 7fc2fbd | 2011-09-01 13:40:39 -0400 | [diff] [blame] | 2146 | celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem); |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2147 | /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/ |
| 2148 | /* Check if the waveform is decaying (and if so how fast) */ |
| 2149 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2150 | opus_val32 E1=1, E2=1; |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2151 | int period; |
| 2152 | if (pitch_index <= MAX_PERIOD/2) |
| 2153 | period = pitch_index; |
| 2154 | else |
| 2155 | period = MAX_PERIOD/2; |
| 2156 | for (i=0;i<period;i++) |
| 2157 | { |
| 2158 | E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8); |
| 2159 | E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8); |
| 2160 | } |
| 2161 | if (E1 > E2) |
| 2162 | E1 = E2; |
| 2163 | decay = celt_sqrt(frac_div32(SHR(E1,1),E2)); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 2164 | } |
| 2165 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2166 | /* Copy excitation, taking decay into account */ |
| 2167 | for (i=0;i<len+st->mode->overlap;i++) |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2168 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2169 | opus_val16 tmp; |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2170 | if (offset+i >= MAX_PERIOD) |
| 2171 | { |
| 2172 | offset -= pitch_index; |
| 2173 | decay = MULT16_16_Q15(decay, decay); |
| 2174 | } |
| 2175 | e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT); |
| 2176 | tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT); |
| 2177 | S1 += SHR32(MULT16_16(tmp,tmp),8); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2178 | } |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2179 | for (i=0;i<LPC_ORDER;i++) |
| 2180 | mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT); |
| 2181 | for (i=0;i<len+st->mode->overlap;i++) |
| 2182 | e[i] = MULT16_32_Q15(fade, e[i]); |
Jean-Marc Valin | 7fc2fbd | 2011-09-01 13:40:39 -0400 | [diff] [blame] | 2183 | celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2184 | |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2185 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2186 | opus_val32 S2=0; |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 2187 | for (i=0;i<len+overlap;i++) |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2188 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2189 | opus_val16 tmp = ROUND16(e[i],SIG_SHIFT); |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2190 | S2 += SHR32(MULT16_16(tmp,tmp),8); |
| 2191 | } |
| 2192 | /* This checks for an "explosion" in the synthesis */ |
| 2193 | #ifdef FIXED_POINT |
| 2194 | if (!(S1 > SHR32(S2,2))) |
| 2195 | #else |
| 2196 | /* Float test is written this way to catch NaNs at the same time */ |
| 2197 | if (!(S1 > 0.2f*S2)) |
| 2198 | #endif |
| 2199 | { |
| 2200 | for (i=0;i<len+overlap;i++) |
| 2201 | e[i] = 0; |
| 2202 | } else if (S1 < S2) |
| 2203 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2204 | opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2205 | for (i=0;i<len+overlap;i++) |
| 2206 | e[i] = MULT16_32_Q15(ratio, e[i]); |
| 2207 | } |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 2208 | } |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2209 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2210 | /* Apply post-filter to the MDCT overlap of the previous frame */ |
| 2211 | comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap, |
| 2212 | st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, |
| 2213 | NULL, 0); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2214 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2215 | for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++) |
| 2216 | out_mem[c][i] = out_mem[c][N+i]; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2217 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2218 | /* Apply TDAC to the concealed audio so that it blends with the |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2219 | previous and next frames */ |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2220 | for (i=0;i<overlap/2;i++) |
| 2221 | { |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2222 | opus_val32 tmp; |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2223 | tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) + |
| 2224 | MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]); |
| 2225 | out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp); |
| 2226 | out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp); |
| 2227 | } |
| 2228 | for (i=0;i<N;i++) |
| 2229 | out_mem[c][MAX_PERIOD-N+i] = e[i]; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2230 | |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2231 | /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */ |
| 2232 | comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap, |
| 2233 | -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset, |
| 2234 | NULL, 0); |
Jean-Marc Valin | 8430a75 | 2011-02-03 23:54:37 -0500 | [diff] [blame] | 2235 | for (i=0;i<overlap;i++) |
| 2236 | out_mem[c][MAX_PERIOD+i] = e[i]; |
| 2237 | } while (++c<C); |
| 2238 | } |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 2239 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 2240 | deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2241 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 2242 | st->loss_count++; |
| 2243 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 2244 | RESTORE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 2245 | } |
| 2246 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2247 | int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, opus_val16 * restrict pcm, int frame_size, ec_dec *dec) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 2248 | { |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 2249 | int c, i, N; |
Timothy B. Terriberry | 320cf2e | 2010-12-17 05:52:06 -0800 | [diff] [blame] | 2250 | int spread_decision; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2251 | opus_int32 bits; |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 2252 | ec_dec _dec; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 2253 | VARDECL(celt_sig, freq); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 2254 | VARDECL(celt_norm, X); |
| 2255 | VARDECL(celt_ener, bandE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2256 | VARDECL(int, fine_quant); |
| 2257 | VARDECL(int, pulses); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 2258 | VARDECL(int, cap); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2259 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 2260 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 2261 | VARDECL(int, tf_res); |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 2262 | VARDECL(unsigned char, collapse_masks); |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2263 | celt_sig *out_mem[2]; |
| 2264 | celt_sig *decode_mem[2]; |
| 2265 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 2266 | celt_sig *out_syn[2]; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2267 | opus_val16 *lpc; |
| 2268 | opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 2269 | |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 2270 | int shortBlocks; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 2271 | int isTransient; |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 2272 | int intra_ener; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2273 | const int CC = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 2274 | int LM, M; |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 2275 | int effEnd; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 2276 | int codedBands; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 2277 | int alloc_trim; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2278 | int postfilter_pitch; |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2279 | opus_val16 postfilter_gain; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 2280 | int intensity=0; |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 2281 | int dual_stereo=0; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2282 | opus_int32 total_bits; |
| 2283 | opus_int32 balance; |
| 2284 | opus_int32 tell; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2285 | int dynalloc_logp; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2286 | int postfilter_tapset; |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 2287 | int anti_collapse_rsv; |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 2288 | int anti_collapse_on=0; |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2289 | int silence; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2290 | int C = CHANNELS(st->stream_channels); |
Jean-Marc Valin | f62b3bb | 2011-03-09 11:56:29 -0500 | [diff] [blame] | 2291 | ALLOC_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 2292 | |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 2293 | frame_size *= st->downsample; |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 2294 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2295 | c=0; do { |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2296 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); |
| 2297 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 2298 | overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2299 | } while (++c<CC); |
Jean-Marc Valin | ff5f722 | 2011-07-29 18:59:12 -0400 | [diff] [blame] | 2300 | lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC); |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2301 | oldBandE = lpc+LPC_ORDER; |
| 2302 | oldLogE = oldBandE + 2*st->mode->nbEBands; |
| 2303 | oldLogE2 = oldLogE + 2*st->mode->nbEBands; |
| 2304 | backgroundLogE = oldLogE2 + 2*st->mode->nbEBands; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2305 | |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 2306 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2307 | if (st->signalling && data!=NULL) |
| 2308 | { |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 2309 | int data0=data[0]; |
| 2310 | /* Convert "standard mode" to Opus header */ |
| 2311 | if (st->mode->Fs==48000 && st->mode->shortMdctSize==120) |
| 2312 | { |
| 2313 | data0 = fromOpus(data0); |
| 2314 | if (data0<0) |
Jean-Marc Valin | 331e9fe | 2011-09-06 14:30:19 -0400 | [diff] [blame] | 2315 | return OPUS_INVALID_PACKET; |
Jean-Marc Valin | d6bf19d | 2011-03-21 07:06:09 -0400 | [diff] [blame] | 2316 | } |
| 2317 | st->end = IMAX(1, st->mode->effEBands-2*(data0>>5)); |
| 2318 | LM = (data0>>3)&0x3; |
| 2319 | C = 1 + ((data0>>2)&0x1); |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2320 | data++; |
| 2321 | len--; |
| 2322 | if (LM>st->mode->maxLM) |
Jean-Marc Valin | 331e9fe | 2011-09-06 14:30:19 -0400 | [diff] [blame] | 2323 | return OPUS_INVALID_PACKET; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2324 | if (frame_size < st->mode->shortMdctSize<<LM) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2325 | return OPUS_BUFFER_TOO_SMALL; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2326 | else |
| 2327 | frame_size = st->mode->shortMdctSize<<LM; |
| 2328 | } else { |
Gregory Maxwell | fd17eab | 2011-10-04 03:31:45 -0400 | [diff] [blame^] | 2329 | #else |
| 2330 | { |
| 2331 | #endif |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2332 | for (LM=0;LM<=st->mode->maxLM;LM++) |
| 2333 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 2334 | break; |
| 2335 | if (LM>st->mode->maxLM) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2336 | return OPUS_BAD_ARG; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2337 | } |
| 2338 | M=1<<LM; |
| 2339 | |
| 2340 | if (len<0 || len>1275 || pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2341 | return OPUS_BAD_ARG; |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2342 | |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 2343 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 0141723 | 2008-03-03 13:59:55 +1100 | [diff] [blame] | 2344 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 2345 | effEnd = st->end; |
| 2346 | if (effEnd > st->mode->effEBands) |
| 2347 | effEnd = st->mode->effEBands; |
| 2348 | |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2349 | ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 2350 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
| 2351 | ALLOC(bandE, st->mode->nbEBands*C, celt_ener); |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2352 | c=0; do |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2353 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 2354 | X[c*N+i] = 0; |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2355 | while (++c<C); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2356 | c=0; do |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 2357 | for (i=M*st->mode->eBands[effEnd];i<N;i++) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 2358 | X[c*N+i] = 0; |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2359 | while (++c<C); |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2360 | |
Jean-Marc Valin | 65d35a3 | 2011-01-26 22:04:59 -0500 | [diff] [blame] | 2361 | if (data == NULL || len<=1) |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 2362 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 2363 | celt_decode_lost(st, pcm, N, LM); |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 2364 | RESTORE_STACK; |
Jean-Marc Valin | 37e788c | 2011-03-16 20:56:28 -0400 | [diff] [blame] | 2365 | return frame_size/st->downsample; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 2366 | } |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2367 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 2368 | if (dec == NULL) |
| 2369 | { |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2370 | ec_dec_init(&_dec,(unsigned char*)data,len); |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 2371 | dec = &_dec; |
| 2372 | } |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 2373 | |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2374 | if (C<CC) |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2375 | { |
| 2376 | for (i=0;i<st->mode->nbEBands;i++) |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2377 | oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]); |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2378 | } |
| 2379 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2380 | total_bits = len*8; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2381 | tell = ec_tell(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2382 | |
Jean-Marc Valin | a0653ed | 2011-07-05 13:18:59 -0400 | [diff] [blame] | 2383 | if (tell >= total_bits) |
| 2384 | silence = 1; |
| 2385 | else if (tell==1) |
Jean-Marc Valin | 65d35a3 | 2011-01-26 22:04:59 -0500 | [diff] [blame] | 2386 | silence = ec_dec_bit_logp(dec, 15); |
| 2387 | else |
| 2388 | silence = 0; |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2389 | if (silence) |
| 2390 | { |
Jean-Marc Valin | 13a7c26 | 2011-01-26 10:58:33 -0500 | [diff] [blame] | 2391 | /* Pretend we've read all the remaining bits */ |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2392 | tell = len*8; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2393 | dec->nbits_total+=tell-ec_tell(dec); |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2394 | } |
| 2395 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2396 | postfilter_gain = 0; |
| 2397 | postfilter_pitch = 0; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2398 | postfilter_tapset = 0; |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 2399 | if (st->start==0 && tell+16 <= total_bits) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2400 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2401 | if(ec_dec_bit_logp(dec, 1)) |
| 2402 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2403 | int qg, octave; |
| 2404 | octave = ec_dec_uint(dec, 6); |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2405 | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1; |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 2406 | qg = ec_dec_bits(dec, 3); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2407 | if (ec_tell(dec)+2<=total_bits) |
Jean-Marc Valin | ef986e4 | 2011-02-03 15:47:10 -0500 | [diff] [blame] | 2408 | postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2); |
| 2409 | postfilter_gain = QCONST16(.09375f,15)*(qg+1); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2410 | } |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2411 | tell = ec_tell(dec); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2412 | } |
| 2413 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2414 | if (LM > 0 && tell+3 <= total_bits) |
| 2415 | { |
Timothy B. Terriberry | e86fb26 | 2010-12-17 14:50:19 -0800 | [diff] [blame] | 2416 | isTransient = ec_dec_bit_logp(dec, 3); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2417 | tell = ec_tell(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2418 | } |
Jean-Marc Valin | f9fdbff | 2010-09-05 21:02:38 -0400 | [diff] [blame] | 2419 | else |
| 2420 | isTransient = 0; |
Jean-Marc Valin | 017001a | 2010-08-05 15:42:50 -0400 | [diff] [blame] | 2421 | |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 2422 | if (isTransient) |
| 2423 | shortBlocks = M; |
| 2424 | else |
| 2425 | shortBlocks = 0; |
| 2426 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2427 | /* Decode the global flags (first symbols in the stream) */ |
| 2428 | intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0; |
| 2429 | /* Get band energies */ |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 2430 | unquant_coarse_energy(st->mode, st->start, st->end, oldBandE, |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2431 | intra_ener, dec, C, LM); |
| 2432 | |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 2433 | ALLOC(tf_res, st->mode->nbEBands, int); |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 2434 | tf_decode(st->start, st->end, isTransient, tf_res, LM, dec); |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 2435 | |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2436 | tell = ec_tell(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2437 | spread_decision = SPREAD_NORMAL; |
| 2438 | if (tell+4 <= total_bits) |
| 2439 | spread_decision = ec_dec_icdf(dec, spread_icdf, 5); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 2440 | |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2441 | ALLOC(pulses, st->mode->nbEBands, int); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 2442 | ALLOC(cap, st->mode->nbEBands, int); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2443 | ALLOC(offsets, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 2444 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2445 | |
Timothy B. Terriberry | ce6d090 | 2011-02-01 17:41:12 -0800 | [diff] [blame] | 2446 | init_caps(st->mode,cap,LM,C); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 2447 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2448 | dynalloc_logp = 6; |
| 2449 | total_bits<<=BITRES; |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2450 | tell = ec_tell_frac(dec); |
Timothy B. Terriberry | d6f6157 | 2010-12-30 09:04:16 -0800 | [diff] [blame] | 2451 | for (i=st->start;i<st->end;i++) |
Jean-Marc Valin | d74c851 | 2010-09-29 17:39:54 -0400 | [diff] [blame] | 2452 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2453 | int width, quanta; |
| 2454 | int dynalloc_loop_logp; |
| 2455 | int boost; |
| 2456 | width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM; |
| 2457 | /* quanta is 6 bits, but no more than 1 bit/sample |
| 2458 | and no less than 1/8 bit/sample */ |
| 2459 | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); |
| 2460 | dynalloc_loop_logp = dynalloc_logp; |
| 2461 | boost = 0; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 2462 | while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i]) |
Jean-Marc Valin | d74c851 | 2010-09-29 17:39:54 -0400 | [diff] [blame] | 2463 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2464 | int flag; |
| 2465 | flag = ec_dec_bit_logp(dec, dynalloc_loop_logp); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2466 | tell = ec_tell_frac(dec); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2467 | if (!flag) |
| 2468 | break; |
| 2469 | boost += quanta; |
| 2470 | total_bits -= quanta; |
| 2471 | dynalloc_loop_logp = 1; |
Jean-Marc Valin | d74c851 | 2010-09-29 17:39:54 -0400 | [diff] [blame] | 2472 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2473 | offsets[i] = boost; |
| 2474 | /* Making dynalloc more likely */ |
| 2475 | if (boost>0) |
| 2476 | dynalloc_logp = IMAX(2, dynalloc_logp-1); |
Jean-Marc Valin | d74c851 | 2010-09-29 17:39:54 -0400 | [diff] [blame] | 2477 | } |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 2478 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 2479 | ALLOC(fine_quant, st->mode->nbEBands, int); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 2480 | alloc_trim = tell+(6<<BITRES) <= total_bits ? |
| 2481 | ec_dec_icdf(dec, trim_icdf, 7) : 5; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 2482 | |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 2483 | bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1; |
| 2484 | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 2485 | bits -= anti_collapse_rsv; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 2486 | codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap, |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 2487 | alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses, |
| 2488 | fine_quant, fine_priority, C, LM, dec, 0, 0); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2489 | |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 2490 | unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C); |
Jean-Marc Valin | 827f931 | 2008-05-06 23:21:55 +1000 | [diff] [blame] | 2491 | |
Jean-Marc Valin | 8cbea17 | 2010-08-05 15:22:57 -0400 | [diff] [blame] | 2492 | /* Decode fixed codebook */ |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 2493 | ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char); |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 2494 | quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks, |
Gregory Maxwell | d830d08 | 2011-08-15 16:05:40 -0400 | [diff] [blame] | 2495 | NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res, |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 2496 | len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng); |
Jean-Marc Valin | 746b2a8 | 2010-05-14 22:12:33 -0400 | [diff] [blame] | 2497 | |
Timothy B. Terriberry | 21af73e | 2011-01-19 16:30:03 -0800 | [diff] [blame] | 2498 | if (anti_collapse_rsv > 0) |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 2499 | { |
| 2500 | anti_collapse_on = ec_dec_bits(dec, 1); |
| 2501 | } |
| 2502 | |
Jean-Marc Valin | e3e2c26 | 2011-01-26 13:09:53 -0500 | [diff] [blame] | 2503 | unquant_energy_finalise(st->mode, st->start, st->end, oldBandE, |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 2504 | fine_quant, fine_priority, len*8-ec_tell(dec), dec, C); |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 2505 | |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 2506 | if (anti_collapse_on) |
Gregory Maxwell | b288c50 | 2011-08-12 14:22:16 -0400 | [diff] [blame] | 2507 | anti_collapse(st->mode, X, collapse_masks, LM, C, CC, N, |
Jean-Marc Valin | 63fb61f | 2011-01-20 22:52:55 -0500 | [diff] [blame] | 2508 | st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); |
Jean-Marc Valin | 87efe1d | 2011-01-18 14:44:04 -0500 | [diff] [blame] | 2509 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 2510 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 2511 | |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2512 | if (silence) |
| 2513 | { |
| 2514 | for (i=0;i<C*st->mode->nbEBands;i++) |
| 2515 | { |
| 2516 | bandE[i] = 0; |
Gregory Maxwell | 8b631f2 | 2011-01-26 20:19:01 -0500 | [diff] [blame] | 2517 | oldBandE[i] = -QCONST16(28.f,DB_SHIFT); |
Jean-Marc Valin | de79c37 | 2011-01-26 09:24:33 -0500 | [diff] [blame] | 2518 | } |
| 2519 | } |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 2520 | /* Synthesis */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 2521 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 2522 | |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 2523 | OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N); |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2524 | if (CC==2) |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 2525 | OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 2526 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2527 | c=0; do |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2528 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 2529 | freq[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2530 | while (++c<C); |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 2531 | c=0; do { |
| 2532 | int bound = M*st->mode->eBands[effEnd]; |
| 2533 | if (st->downsample!=1) |
| 2534 | bound = IMIN(bound, N/st->downsample); |
Jean-Marc Valin | 23340e2 | 2011-02-03 23:21:00 -0500 | [diff] [blame] | 2535 | for (i=bound;i<N;i++) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 2536 | freq[c*N+i] = 0; |
Jean-Marc Valin | 913a174 | 2011-01-29 10:00:20 -0500 | [diff] [blame] | 2537 | } while (++c<C); |
Jean-Marc Valin | 3a0bc3d | 2010-02-21 15:10:22 -0500 | [diff] [blame] | 2538 | |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2539 | out_syn[0] = out_mem[0]+MAX_PERIOD-N; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2540 | if (CC==2) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 2541 | out_syn[1] = out_mem[1]+MAX_PERIOD-N; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 2542 | |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2543 | if (CC==2&&C==1) |
| 2544 | { |
| 2545 | for (i=0;i<N;i++) |
| 2546 | freq[N+i] = freq[i]; |
| 2547 | } |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2548 | if (CC==1&&C==2) |
| 2549 | { |
| 2550 | for (i=0;i<N;i++) |
| 2551 | freq[i] = HALF32(ADD32(freq[i],freq[N+i])); |
| 2552 | } |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2553 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 2554 | /* Compute inverse MDCTs */ |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2555 | compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 2556 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 2557 | c=0; do { |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 2558 | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); |
| 2559 | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 2560 | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize, |
Jean-Marc Valin | a14e86d | 2011-02-11 16:27:27 -0500 | [diff] [blame] | 2561 | st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset, |
| 2562 | st->mode->window, st->overlap); |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 2563 | if (LM!=0) |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 2564 | comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize, |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2565 | st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset, |
| 2566 | st->mode->window, st->mode->overlap); |
Jean-Marc Valin | 64805fd | 2011-02-12 00:50:53 -0500 | [diff] [blame] | 2567 | |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2568 | } while (++c<CC); |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 2569 | st->postfilter_period_old = st->postfilter_period; |
| 2570 | st->postfilter_gain_old = st->postfilter_gain; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2571 | st->postfilter_tapset_old = st->postfilter_tapset; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2572 | st->postfilter_period = postfilter_pitch; |
| 2573 | st->postfilter_gain = postfilter_gain; |
Jean-Marc Valin | dfa847a | 2011-01-17 11:37:08 -0500 | [diff] [blame] | 2574 | st->postfilter_tapset = postfilter_tapset; |
Jean-Marc Valin | a14e86d | 2011-02-11 16:27:27 -0500 | [diff] [blame] | 2575 | if (LM!=0) |
| 2576 | { |
| 2577 | st->postfilter_period_old = st->postfilter_period; |
| 2578 | st->postfilter_gain_old = st->postfilter_gain; |
| 2579 | st->postfilter_tapset_old = st->postfilter_tapset; |
| 2580 | } |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 2581 | |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2582 | if (C==1) { |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2583 | for (i=0;i<st->mode->nbEBands;i++) |
| 2584 | oldBandE[st->mode->nbEBands+i]=oldBandE[i]; |
| 2585 | } |
| 2586 | |
Jean-Marc Valin | 5677e34 | 2011-01-13 16:15:53 -0500 | [diff] [blame] | 2587 | /* In case start or end were to change */ |
Jean-Marc Valin | 5c2ac2b | 2011-01-22 14:48:20 -0500 | [diff] [blame] | 2588 | c=0; do |
| 2589 | { |
| 2590 | for (i=0;i<st->start;i++) |
| 2591 | oldBandE[c*st->mode->nbEBands+i]=0; |
| 2592 | for (i=st->end;i<st->mode->nbEBands;i++) |
| 2593 | oldBandE[c*st->mode->nbEBands+i]=0; |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2594 | } while (++c<2); |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 2595 | if (!isTransient) |
| 2596 | { |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2597 | for (i=0;i<2*st->mode->nbEBands;i++) |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 2598 | oldLogE2[i] = oldLogE[i]; |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2599 | for (i=0;i<2*st->mode->nbEBands;i++) |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 2600 | oldLogE[i] = oldBandE[i]; |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2601 | for (i=0;i<2*st->mode->nbEBands;i++) |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 2602 | backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]); |
Jean-Marc Valin | 47e905d | 2011-01-27 18:05:47 -0500 | [diff] [blame] | 2603 | } else { |
Jean-Marc Valin | 9dec74d | 2011-03-28 01:39:41 -0400 | [diff] [blame] | 2604 | for (i=0;i<2*st->mode->nbEBands;i++) |
Jean-Marc Valin | 47e905d | 2011-01-27 18:05:47 -0500 | [diff] [blame] | 2605 | oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); |
Jean-Marc Valin | 9ce95e0 | 2011-01-25 19:12:06 -0500 | [diff] [blame] | 2606 | } |
Jean-Marc Valin | 63fb61f | 2011-01-20 22:52:55 -0500 | [diff] [blame] | 2607 | st->rng = dec->rng; |
Jean-Marc Valin | 5677e34 | 2011-01-13 16:15:53 -0500 | [diff] [blame] | 2608 | |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2609 | deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 2610 | st->loss_count = 0; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 2611 | RESTORE_STACK; |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 2612 | if (ec_tell(dec) > 8*len) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2613 | return OPUS_INTERNAL_ERROR; |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 2614 | if(ec_get_error(dec)) |
| 2615 | st->error = 1; |
| 2616 | return frame_size/st->downsample; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 2617 | } |
| 2618 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2619 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 2620 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2621 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2622 | #ifdef FIXED_POINT |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 2623 | int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size) |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2624 | { |
| 2625 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); |
| 2626 | } |
| 2627 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2628 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 2629 | int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2630 | { |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 2631 | int j, ret, C, N; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2632 | VARDECL(opus_int16, out); |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 2633 | ALLOC_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 2634 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 2635 | if (pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2636 | return OPUS_BAD_ARG; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 2637 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 2638 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 2639 | N = frame_size; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2640 | |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2641 | ALLOC(out, C*N, opus_int16); |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2642 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2643 | if (ret>0) |
| 2644 | for (j=0;j<C*ret;j++) |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 2645 | pcm[j]=out[j]*(1.f/32768.f); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2646 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 2647 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2648 | return ret; |
| 2649 | } |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2650 | #endif /* DISABLE_FLOAT_API */ |
| 2651 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2652 | #else |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2653 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 2654 | int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size) |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2655 | { |
| 2656 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); |
| 2657 | } |
| 2658 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 2659 | int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2660 | { |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 2661 | int j, ret, C, N; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 2662 | VARDECL(celt_sig, out); |
Jean-Marc Valin | 7e98319 | 2011-02-01 18:00:29 -0500 | [diff] [blame] | 2663 | ALLOC_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 2664 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 2665 | if (pcm==NULL) |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2666 | return OPUS_BAD_ARG; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 2667 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 2668 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 713d7a4 | 2011-01-31 13:41:01 -0500 | [diff] [blame] | 2669 | N = frame_size; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 2670 | ALLOC(out, C*N, celt_sig); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2671 | |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2672 | ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2673 | |
Jean-Marc Valin | e6acfe0 | 2011-03-11 16:31:24 -0500 | [diff] [blame] | 2674 | if (ret>0) |
| 2675 | for (j=0;j<C*ret;j++) |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 2676 | pcm[j] = FLOAT2INT16 (out[j]); |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 2677 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 2678 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2679 | return ret; |
| 2680 | } |
Jean-Marc Valin | 222494f | 2011-08-17 15:53:37 -0400 | [diff] [blame] | 2681 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 2682 | #endif |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 2683 | #endif /* CUSTOM_MODES */ |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2684 | |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 2685 | int opus_custom_decoder_ctl(CELTDecoder * restrict st, int request, ...) |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2686 | { |
| 2687 | va_list ap; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 2688 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2689 | va_start(ap, request); |
| 2690 | switch (request) |
| 2691 | { |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2692 | case CELT_SET_START_BAND_REQUEST: |
| 2693 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2694 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2695 | if (value<0 || value>=st->mode->nbEBands) |
| 2696 | goto bad_arg; |
| 2697 | st->start = value; |
| 2698 | } |
| 2699 | break; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 2700 | case CELT_SET_END_BAND_REQUEST: |
| 2701 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2702 | opus_int32 value = va_arg(ap, opus_int32); |
Gregory Maxwell | 1928f8d | 2011-02-08 22:32:56 -0500 | [diff] [blame] | 2703 | if (value<1 || value>st->mode->nbEBands) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 2704 | goto bad_arg; |
| 2705 | st->end = value; |
| 2706 | } |
| 2707 | break; |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2708 | case CELT_SET_CHANNELS_REQUEST: |
| 2709 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2710 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | f1916a1 | 2011-01-31 10:51:30 -0500 | [diff] [blame] | 2711 | if (value<1 || value>2) |
| 2712 | goto bad_arg; |
| 2713 | st->stream_channels = value; |
| 2714 | } |
Jean-Marc Valin | dd2973d | 2011-03-11 17:46:02 -0500 | [diff] [blame] | 2715 | break; |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 2716 | case CELT_GET_AND_CLEAR_ERROR_REQUEST: |
| 2717 | { |
Jean-Marc Valin | 25f7f35 | 2011-09-14 09:50:06 -0700 | [diff] [blame] | 2718 | int *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | ef20e39 | 2011-03-18 15:34:11 -0400 | [diff] [blame] | 2719 | if (value==NULL) |
| 2720 | goto bad_arg; |
| 2721 | *value=st->error; |
| 2722 | st->error = 0; |
| 2723 | } |
| 2724 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2725 | case OPUS_GET_LOOKAHEAD_REQUEST: |
Jean-Marc Valin | ff96b16 | 2011-03-21 11:32:50 -0400 | [diff] [blame] | 2726 | { |
Jean-Marc Valin | 25f7f35 | 2011-09-14 09:50:06 -0700 | [diff] [blame] | 2727 | int *value = va_arg(ap, opus_int32*); |
Jean-Marc Valin | ff96b16 | 2011-03-21 11:32:50 -0400 | [diff] [blame] | 2728 | if (value==NULL) |
| 2729 | goto bad_arg; |
| 2730 | *value = st->overlap/st->downsample; |
| 2731 | } |
| 2732 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2733 | case OPUS_RESET_STATE: |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2734 | { |
Jean-Marc Valin | be89c39 | 2011-08-30 12:39:51 -0400 | [diff] [blame] | 2735 | OPUS_CLEAR((char*)&st->DECODER_RESET_START, |
Jean-Marc Valin | 06237d7 | 2011-09-01 13:20:40 -0400 | [diff] [blame] | 2736 | opus_custom_decoder_get_size(st->mode, st->channels)- |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 2737 | ((char*)&st->DECODER_RESET_START - (char*)st)); |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2738 | } |
| 2739 | break; |
Jean-Marc Valin | 25f7f35 | 2011-09-14 09:50:06 -0700 | [diff] [blame] | 2740 | case OPUS_GET_PITCH_REQUEST: |
| 2741 | { |
| 2742 | int *value = va_arg(ap, opus_int32*); |
| 2743 | if (value==NULL) |
| 2744 | goto bad_arg; |
| 2745 | *value = st->postfilter_period; |
| 2746 | } |
| 2747 | break; |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 2748 | #ifdef OPUS_BUILD |
| 2749 | case CELT_GET_MODE_REQUEST: |
| 2750 | { |
| 2751 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 2752 | if (value==0) |
| 2753 | goto bad_arg; |
| 2754 | *value=st->mode; |
| 2755 | } |
| 2756 | break; |
| 2757 | case CELT_SET_SIGNALLING_REQUEST: |
| 2758 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 2759 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 2760 | st->signalling = value; |
| 2761 | } |
| 2762 | break; |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2763 | case OPUS_GET_FINAL_RANGE_REQUEST: |
Jean-Marc Valin | 06cee2b | 2011-08-19 16:11:41 -0400 | [diff] [blame] | 2764 | { |
| 2765 | opus_uint32 * value = va_arg(ap, opus_uint32 *); |
| 2766 | if (value==0) |
| 2767 | goto bad_arg; |
| 2768 | *value=st->rng; |
| 2769 | } |
| 2770 | break; |
Jean-Marc Valin | c3086a9 | 2011-03-21 13:26:03 -0400 | [diff] [blame] | 2771 | #endif |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2772 | default: |
| 2773 | goto bad_request; |
| 2774 | } |
| 2775 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2776 | return OPUS_OK; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2777 | bad_arg: |
| 2778 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2779 | return OPUS_BAD_ARG; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2780 | bad_request: |
| 2781 | va_end(ap); |
Jean-Marc Valin | f9e701a | 2011-08-31 17:47:48 -0400 | [diff] [blame] | 2782 | return OPUS_UNIMPLEMENTED; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2783 | } |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 2784 | |
Jean-Marc Valin | f9265dd | 2011-09-01 13:46:18 -0400 | [diff] [blame] | 2785 | |
| 2786 | |
| 2787 | const char *opus_strerror(int error) |
| 2788 | { |
| 2789 | static const char *error_strings[8] = { |
| 2790 | "success", |
| 2791 | "invalid argument", |
| 2792 | "buffer too small", |
| 2793 | "internal error", |
| 2794 | "corrupted stream", |
| 2795 | "request not implemented", |
| 2796 | "invalid state", |
| 2797 | "memory allocation failed" |
| 2798 | }; |
| 2799 | if (error > 0 || error < -7) |
| 2800 | return "unknown error"; |
| 2801 | else |
| 2802 | return error_strings[-error]; |
| 2803 | } |
| 2804 | |
| 2805 | const char *opus_get_version_string(void) |
| 2806 | { |
| 2807 | return "libopus " OPUS_VERSION |
| 2808 | #ifdef FUZZING |
| 2809 | "-fuzzing" |
| 2810 | #endif |
| 2811 | ; |
| 2812 | } |