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