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 |
Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 3 | Copyright (c) 2008 Gregory Maxwell |
| 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: |
| 9 | |
| 10 | - Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | |
| 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. |
| 16 | |
| 17 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 18 | contributors may be used to endorse or promote products derived from |
| 19 | this software without specific prior written permission. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 34 | #ifdef HAVE_CONFIG_H |
| 35 | #include "config.h" |
| 36 | #endif |
| 37 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 38 | #define CELT_C |
| 39 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 40 | #include "os_support.h" |
Jean-Marc Valin | f02ba11 | 2007-11-30 01:10:42 +1100 | [diff] [blame] | 41 | #include "mdct.h" |
| 42 | #include <math.h> |
Jean-Marc Valin | 013c31d | 2007-11-30 11:36:46 +1100 | [diff] [blame] | 43 | #include "celt.h" |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 44 | #include "pitch.h" |
Jean-Marc Valin | 991c0f0 | 2007-11-30 16:07:46 +1100 | [diff] [blame] | 45 | #include "bands.h" |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 46 | #include "modes.h" |
Jean-Marc Valin | 6238bc0 | 2008-01-28 22:28:54 +1100 | [diff] [blame] | 47 | #include "entcode.h" |
Jean-Marc Valin | 98d2a49 | 2007-12-07 22:46:47 +1100 | [diff] [blame] | 48 | #include "quant_bands.h" |
Jean-Marc Valin | 4fbd18d | 2008-01-17 14:07:55 +1100 | [diff] [blame] | 49 | #include "rate.h" |
Jean-Marc Valin | c7e0b76 | 2008-03-16 07:55:29 +1100 | [diff] [blame] | 50 | #include "stack_alloc.h" |
Jean-Marc Valin | e4aeb47 | 2008-06-29 12:06:05 +1000 | [diff] [blame] | 51 | #include "mathops.h" |
Jean-Marc Valin | d9b9565 | 2008-08-31 23:34:47 -0400 | [diff] [blame] | 52 | #include "float_cast.h" |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 53 | #include <stdarg.h> |
Jean-Marc Valin | 6c3788c | 2010-06-20 22:48:50 -0400 | [diff] [blame] | 54 | #include "plc.h" |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 55 | |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 56 | static const int trim_cdf[12] = {0, 2, 4, 9, 19, 41, 87, 109, 119, 124, 126, 128}; |
| 57 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 58 | #define COMBFILTER_MAXPERIOD 1024 |
| 59 | #define COMBFILTER_MINPERIOD 16 |
Jean-Marc Valin | 6bf0462 | 2010-09-30 10:16:22 -0400 | [diff] [blame] | 60 | |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 61 | /** Encoder state |
| 62 | @brief Encoder state |
| 63 | */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 64 | struct CELTEncoder { |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 65 | const CELTMode *mode; /**< Mode used by the encoder */ |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 66 | int overlap; |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 67 | int channels; |
| 68 | |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 69 | int force_intra; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 70 | int complexity; |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 71 | int start, end; |
| 72 | |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 73 | celt_int32 vbr_rate_norm; /* Target number of 8th bits per frame */ |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 74 | int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */ |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 75 | |
| 76 | /* Everything beyond this point gets cleared on a reset */ |
| 77 | #define ENCODER_RESET_START frame_max |
| 78 | |
| 79 | celt_word32 frame_max; |
| 80 | int fold_decision; |
Jean-Marc Valin | 74f4e9f | 2009-05-02 09:57:50 -0400 | [diff] [blame] | 81 | int delayedIntra; |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 82 | int tonal_average; |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 83 | int lastCodedBands; |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 84 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 85 | int prefilter_period; |
| 86 | celt_word16 prefilter_gain; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 87 | #ifdef RESYNTH |
| 88 | int prefilter_period_old; |
| 89 | celt_word16 prefilter_gain_old; |
| 90 | #endif |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 91 | |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 92 | /* VBR-related parameters */ |
| 93 | celt_int32 vbr_reservoir; |
| 94 | celt_int32 vbr_drift; |
| 95 | celt_int32 vbr_offset; |
| 96 | celt_int32 vbr_count; |
| 97 | |
Jean-Marc Valin | ac1da4f | 2010-07-24 11:48:10 -0400 | [diff] [blame] | 98 | celt_word32 preemph_memE[2]; |
| 99 | celt_word32 preemph_memD[2]; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 100 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 101 | #ifdef RESYNTH |
| 102 | celt_sig syn_mem[2][2*MAX_PERIOD]; |
| 103 | #endif |
| 104 | |
Jean-Marc Valin | ca8b992 | 2010-08-27 16:23:03 -0400 | [diff] [blame] | 105 | celt_sig in_mem[1]; /* Size = channels*mode->overlap */ |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 106 | /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */ |
Jean-Marc Valin | ca8b992 | 2010-08-27 16:23:03 -0400 | [diff] [blame] | 107 | /* celt_sig overlap_mem[], Size = channels*mode->overlap */ |
| 108 | /* celt_word16 oldEBands[], Size = channels*mode->nbEBands */ |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 109 | }; |
| 110 | |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 111 | int celt_encoder_get_size(const CELTMode *mode, int channels) |
| 112 | { |
| 113 | int size = sizeof(struct CELTEncoder) |
| 114 | + (2*channels*mode->overlap-1)*sizeof(celt_sig) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 115 | + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 116 | + channels*mode->nbEBands*sizeof(celt_word16); |
| 117 | return size; |
| 118 | } |
| 119 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 120 | CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error) |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 121 | { |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 122 | return celt_encoder_init( |
| 123 | (CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels)), |
| 124 | mode, channels, error); |
| 125 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 126 | |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 127 | CELTEncoder *celt_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels, int *error) |
| 128 | { |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 129 | if (channels < 0 || channels > 2) |
| 130 | { |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 131 | if (error) |
| 132 | *error = CELT_BAD_ARG; |
| 133 | return NULL; |
| 134 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 135 | |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 136 | if (st==NULL) |
| 137 | { |
| 138 | if (error) |
| 139 | *error = CELT_ALLOC_FAIL; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 140 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 141 | } |
Jean-Marc Valin | 6d3829f | 2010-08-27 17:52:38 -0400 | [diff] [blame] | 142 | |
| 143 | CELT_MEMSET((char*)st, 0, celt_encoder_get_size(mode, channels)); |
| 144 | |
Jean-Marc Valin | 73e51b3 | 2007-12-05 17:48:24 +1100 | [diff] [blame] | 145 | st->mode = mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 146 | st->overlap = mode->overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 147 | st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 148 | |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 149 | st->start = 0; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 150 | st->end = st->mode->effEBands; |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 151 | st->constrained_vbr = 1; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 152 | |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 153 | st->vbr_rate_norm = 0; |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 154 | st->vbr_offset = 0; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 155 | st->force_intra = 0; |
Gregory Maxwell | 8842fde | 2009-05-04 15:58:40 -0400 | [diff] [blame] | 156 | st->delayedIntra = 1; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 157 | st->tonal_average = 256; |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 158 | st->fold_decision = 1; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 159 | st->complexity = 5; |
Jean-Marc Valin | a76a0b2 | 2008-01-17 22:43:05 +1100 | [diff] [blame] | 160 | |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 161 | if (error) |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 162 | *error = CELT_OK; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 163 | return st; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 164 | } |
| 165 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 166 | void celt_encoder_destroy(CELTEncoder *st) |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 167 | { |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 168 | celt_free(st); |
| 169 | } |
| 170 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 171 | static inline celt_int16 FLOAT2INT16(float x) |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 172 | { |
Gregory Maxwell | 0ac2b2f | 2009-05-21 23:08:46 -0400 | [diff] [blame] | 173 | x = x*CELT_SIG_SCALE; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 174 | x = MAX32(x, -32768); |
| 175 | x = MIN32(x, 32767); |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 176 | return (celt_int16)float2int(x); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 179 | static inline celt_word16 SIG2WORD16(celt_sig x) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 180 | { |
| 181 | #ifdef FIXED_POINT |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 182 | x = PSHR32(x, SIG_SHIFT); |
Jean-Marc Valin | abdfc38 | 2008-04-18 15:57:18 +1000 | [diff] [blame] | 183 | x = MAX32(x, -32768); |
| 184 | x = MIN32(x, 32767); |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 185 | return EXTRACT16(x); |
| 186 | #else |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 187 | return (celt_word16)x; |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 188 | #endif |
| 189 | } |
| 190 | |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 191 | static int transient_analysis(const celt_word32 * restrict in, int len, int C, |
Jean-Marc Valin | bdb5871 | 2010-05-28 18:58:42 -0400 | [diff] [blame] | 192 | celt_word32 *frame_max, int overlap) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 193 | { |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 194 | int i; |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 195 | VARDECL(celt_word16, tmp); |
| 196 | celt_word32 mem0=0,mem1=0; |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 197 | int is_transient = 0; |
| 198 | int block; |
| 199 | int N; |
| 200 | /* FIXME: Make that smaller */ |
| 201 | celt_word16 bins[50]; |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 202 | SAVE_STACK; |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 203 | ALLOC(tmp, len, celt_word16); |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 204 | |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 205 | block = overlap/2; |
| 206 | N=len/block; |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 207 | if (C==1) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 208 | { |
| 209 | for (i=0;i<len;i++) |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 210 | tmp[i] = SHR32(in[i],SIG_SHIFT); |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 211 | } else { |
| 212 | for (i=0;i<len;i++) |
Jean-Marc Valin | 933dd83 | 2010-10-24 00:08:16 -0400 | [diff] [blame] | 213 | 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] | 214 | } |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 215 | |
| 216 | /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */ |
| 217 | for (i=0;i<len;i++) |
| 218 | { |
| 219 | celt_word32 x,y; |
| 220 | x = tmp[i]; |
| 221 | y = ADD32(mem0, x); |
| 222 | #ifdef FIXED_POINT |
| 223 | mem0 = mem1 + y - SHL32(x,1); |
| 224 | mem1 = x - SHR32(y,1); |
| 225 | #else |
| 226 | mem0 = mem1 + y - 2*x; |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 227 | mem1 = x - .5f*y; |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 228 | #endif |
| 229 | tmp[i] = EXTRACT16(SHR(y,2)); |
| 230 | } |
| 231 | /* 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] | 232 | for (i=0;i<12;i++) |
Jean-Marc Valin | 2794b63 | 2010-10-13 17:32:57 -0400 | [diff] [blame] | 233 | tmp[i] = 0; |
| 234 | |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 235 | for (i=0;i<N;i++) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 236 | { |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 237 | int j; |
| 238 | float max_abs=0; |
| 239 | for (j=0;j<block;j++) |
| 240 | max_abs = MAX32(max_abs, tmp[i*block+j]); |
| 241 | bins[i] = max_abs; |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 242 | } |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 243 | for (i=0;i<N;i++) |
Jean-Marc Valin | 4a8c1f1 | 2010-10-13 18:03:50 -0400 | [diff] [blame] | 244 | { |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 245 | int j; |
| 246 | int conseq=0; |
| 247 | celt_word16 t1, t2, t3; |
| 248 | |
| 249 | t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]); |
| 250 | t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]); |
| 251 | t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]); |
| 252 | for (j=0;j<i;j++) |
| 253 | { |
| 254 | if (bins[j] < t1) |
| 255 | conseq++; |
| 256 | if (bins[j] < t2) |
| 257 | conseq++; |
| 258 | else |
| 259 | conseq = 0; |
| 260 | } |
| 261 | if (conseq>=3) |
| 262 | is_transient=1; |
| 263 | conseq = 0; |
| 264 | for (j=i+1;j<N;j++) |
| 265 | { |
| 266 | if (bins[j] < t3) |
| 267 | conseq++; |
| 268 | else |
| 269 | conseq = 0; |
| 270 | } |
| 271 | if (conseq>=7) |
| 272 | is_transient=1; |
Jean-Marc Valin | 4a8c1f1 | 2010-10-13 18:03:50 -0400 | [diff] [blame] | 273 | } |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 274 | RESTORE_STACK; |
Jean-Marc Valin | fddc521 | 2010-12-09 14:28:26 -0500 | [diff] [blame] | 275 | return is_transient; |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 276 | } |
| 277 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 278 | /** Apply window and compute the MDCT for all sub-frames and |
| 279 | all channels in a frame */ |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 280 | 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] | 281 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 282 | const int C = CHANNELS(_C); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 283 | if (C==1 && !shortBlocks) |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 284 | { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 285 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 286 | clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM); |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 287 | } else { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 288 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | ce4dd36 | 2010-05-07 07:45:18 -0400 | [diff] [blame] | 289 | int N = mode->shortMdctSize<<LM; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 290 | int B = 1; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 291 | int b, c; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 292 | VARDECL(celt_word32, tmp); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 293 | SAVE_STACK; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 294 | if (shortBlocks) |
| 295 | { |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 296 | /*lookup = &mode->mdct[0];*/ |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 297 | N = mode->shortMdctSize; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 298 | B = shortBlocks; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 299 | } |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 300 | ALLOC(tmp, N, celt_word32); |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 301 | c=0; do { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 302 | for (b=0;b<B;b++) |
| 303 | { |
| 304 | int j; |
Jean-Marc Valin | eedb422 | 2010-10-24 00:22:45 -0400 | [diff] [blame] | 305 | clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, tmp, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 306 | /* Interleaving the sub-frames */ |
| 307 | for (j=0;j<N;j++) |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 308 | out[(j*B+b)+c*N*B] = tmp[j]; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 309 | } |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 310 | } while (++c<C); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 311 | RESTORE_STACK; |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 312 | } |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 313 | } |
| 314 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 315 | /** Compute the IMDCT and apply window for all sub-frames and |
| 316 | all channels in a frame */ |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 317 | 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] | 318 | celt_sig * restrict out_mem[], |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 319 | celt_sig * restrict overlap_mem[], int _C, int LM) |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 320 | { |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 321 | int c; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 322 | const int C = CHANNELS(_C); |
Jean-Marc Valin | ce4dd36 | 2010-05-07 07:45:18 -0400 | [diff] [blame] | 323 | const int N = mode->shortMdctSize<<LM; |
Jean-Marc Valin | b18ec0b | 2008-04-11 04:07:52 +1000 | [diff] [blame] | 324 | const int overlap = OVERLAP(mode); |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 325 | c=0; do { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 326 | int j; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 327 | VARDECL(celt_word32, x); |
| 328 | VARDECL(celt_word32, tmp); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 329 | int b; |
| 330 | int N2 = N; |
| 331 | int B = 1; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 332 | SAVE_STACK; |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 333 | |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 334 | ALLOC(x, N+overlap, celt_word32); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 335 | ALLOC(tmp, N, celt_word32); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 336 | |
| 337 | if (shortBlocks) |
| 338 | { |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 339 | N2 = mode->shortMdctSize; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 340 | B = shortBlocks; |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 341 | } |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 342 | /* Prevents problems from the imdct doing the overlap-add */ |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 343 | CELT_MEMSET(x, 0, overlap); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 344 | |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 345 | for (b=0;b<B;b++) |
| 346 | { |
| 347 | /* De-interleaving the sub-frames */ |
| 348 | for (j=0;j<N2;j++) |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 349 | tmp[j] = X[(j*B+b)+c*N2*B]; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 350 | clt_mdct_backward(&mode->mdct, tmp, x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 351 | } |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 352 | |
Jean-Marc Valin | 8ddd7f4 | 2008-04-22 13:37:16 +1000 | [diff] [blame] | 353 | for (j=0;j<overlap;j++) |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 354 | out_mem[c][j] = x[j] + overlap_mem[c][j]; |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 355 | for (;j<N;j++) |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 356 | out_mem[c][j] = x[j]; |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 357 | for (j=0;j<overlap;j++) |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 358 | overlap_mem[c][j] = x[N+j]; |
Jean-Marc Valin | 8ddd7f4 | 2008-04-22 13:37:16 +1000 | [diff] [blame] | 359 | RESTORE_STACK; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 360 | } while (++c<C); |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 361 | } |
| 362 | |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame] | 363 | static void deemphasis(celt_sig *in[], celt_word16 *pcm, int N, int _C, const celt_word16 *coef, celt_sig *mem) |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 364 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 365 | const int C = CHANNELS(_C); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 366 | int c; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 367 | c=0; do { |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 368 | int j; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 369 | celt_sig * restrict x; |
| 370 | celt_word16 * restrict y; |
| 371 | celt_sig m = mem[c]; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 372 | x =in[c]; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 373 | y = pcm+c; |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 374 | for (j=0;j<N;j++) |
| 375 | { |
Jean-Marc Valin | af1fce9 | 2010-07-16 11:05:06 -0400 | [diff] [blame] | 376 | celt_sig tmp = *x + m; |
| 377 | m = MULT16_32_Q15(coef[0], tmp) |
| 378 | - MULT16_32_Q15(coef[1], *x); |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 379 | tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2); |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 380 | *y = SCALEOUT(SIG2WORD16(tmp)); |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame] | 381 | x++; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 382 | y+=C; |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 383 | } |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 384 | mem[c] = m; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 385 | } while (++c<C); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 388 | #ifdef ENABLE_POSTFILTER |
| 389 | /* FIXME: Handle the case where T = maxperiod */ |
| 390 | static void comb_filter(celt_word32 *y, celt_word32 *x, int T0, int T1, int N, |
| 391 | int C, celt_word16 g0, celt_word16 g1, const celt_word16 *window, int overlap) |
| 392 | { |
| 393 | int i; |
| 394 | /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */ |
| 395 | celt_word16 g00, g01, g02, g10, g11, g12; |
| 396 | celt_word16 t0, t1, t2; |
| 397 | /* zeros at theta = +/- 5*pi/6 */ |
| 398 | t0 = QCONST16(.26795f, 15); |
| 399 | t1 = QCONST16(.46410f, 15); |
| 400 | t2 = QCONST16(.26795f, 15); |
| 401 | g00 = MULT16_16_Q15(g0, t0); |
| 402 | g01 = MULT16_16_Q15(g0, t1); |
| 403 | g02 = MULT16_16_Q15(g0, t2); |
| 404 | g10 = MULT16_16_Q15(g1, t0); |
| 405 | g11 = MULT16_16_Q15(g1, t1); |
| 406 | g12 = MULT16_16_Q15(g1, t2); |
| 407 | for (i=0;i<overlap;i++) |
| 408 | { |
| 409 | celt_word16 f; |
| 410 | f = MULT16_16_Q15(window[i],window[i]); |
| 411 | y[i] = x[i] |
| 412 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0]) |
| 413 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0-1]) |
| 414 | + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+1]) |
| 415 | + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1]) |
| 416 | + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1-1]) |
| 417 | + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+1]); |
| 418 | |
| 419 | } |
| 420 | for (i=overlap;i<N;i++) |
| 421 | y[i] = x[i] |
| 422 | + MULT16_32_Q15(g11,x[i-T1]) |
| 423 | + MULT16_32_Q15(g10,x[i-T1-1]) |
| 424 | + MULT16_32_Q15(g12,x[i-T1+1]); |
| 425 | } |
| 426 | #endif /* ENABLE_POSTFILTER */ |
| 427 | |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 428 | static const signed char tf_select_table[4][8] = { |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 429 | {0, -1, 0, -1, 0,-1, 0,-1}, |
| 430 | {0, -1, 0, -2, 1, 0, 1 -1}, |
| 431 | {0, -2, 0, -3, 2, 0, 1 -1}, |
| 432 | {0, -2, 0, -3, 2, 0, 1 -1}, |
| 433 | }; |
| 434 | |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 435 | static celt_word32 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] | 436 | { |
| 437 | int i, j; |
| 438 | static const celt_word16 sqrtM_1[4] = {Q15ONE, QCONST16(0.70711f,15), QCONST16(0.5f,15), QCONST16(0.35355f,15)}; |
| 439 | celt_word32 L1; |
| 440 | celt_word16 bias; |
| 441 | L1=0; |
| 442 | for (i=0;i<1<<LM;i++) |
| 443 | { |
| 444 | celt_word32 L2 = 0; |
| 445 | for (j=0;j<N>>LM;j++) |
| 446 | L2 = MAC16_16(L2, tmp[(j<<LM)+i], tmp[(j<<LM)+i]); |
| 447 | L1 += celt_sqrt(L2); |
| 448 | } |
| 449 | L1 = MULT16_32_Q15(sqrtM_1[LM], L1); |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 450 | if (width==1) |
| 451 | bias = QCONST16(.12f,15)*LM; |
| 452 | else if (width==2) |
| 453 | bias = QCONST16(.05f,15)*LM; |
| 454 | else |
| 455 | bias = QCONST16(.02f,15)*LM; |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 456 | L1 = MAC16_32_Q15(L1, bias, L1); |
| 457 | return L1; |
| 458 | } |
| 459 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 460 | static int tf_analysis(const CELTMode *m, celt_word16 *bandLogE, celt_word16 *oldBandE, |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 461 | int len, int C, int isTransient, int *tf_res, int nbCompressedBytes, celt_norm *X, |
| 462 | int N0, int LM, int *tf_sum) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 463 | { |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 464 | int i; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 465 | VARDECL(int, metric); |
| 466 | int cost0; |
| 467 | int cost1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 468 | VARDECL(int, path0); |
| 469 | VARDECL(int, path1); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 470 | VARDECL(celt_norm, tmp); |
| 471 | int lambda; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 472 | int tf_select=0; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 473 | SAVE_STACK; |
| 474 | |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 475 | /* FIXME: Should check number of bytes *left* */ |
| 476 | if (nbCompressedBytes<15*C) |
| 477 | { |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 478 | *tf_sum = 0; |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 479 | for (i=0;i<len;i++) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 480 | tf_res[i] = isTransient; |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 481 | return 0; |
| 482 | } |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 483 | if (nbCompressedBytes<40) |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 484 | lambda = 12; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 485 | else if (nbCompressedBytes<60) |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 486 | lambda = 6; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 487 | else if (nbCompressedBytes<100) |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 488 | lambda = 4; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 489 | else |
Jean-Marc Valin | 54fb7e5 | 2010-10-15 11:26:32 -0400 | [diff] [blame] | 490 | lambda = 3; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 491 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 492 | ALLOC(metric, len, int); |
| 493 | 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] | 494 | ALLOC(path0, len, int); |
| 495 | ALLOC(path1, len, int); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 496 | |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 497 | *tf_sum = 0; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 498 | for (i=0;i<len;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 499 | { |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 500 | int j, k, N; |
| 501 | celt_word32 L1, best_L1; |
| 502 | int best_level=0; |
| 503 | N = (m->eBands[i+1]-m->eBands[i])<<LM; |
| 504 | for (j=0;j<N;j++) |
| 505 | tmp[j] = X[j+(m->eBands[i]<<LM)]; |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 506 | /* FIXME: Do something with the right channel */ |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 507 | if (C==2) |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 508 | for (j=0;j<N;j++) |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 509 | 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] | 510 | L1 = l1_metric(tmp, N, isTransient ? LM : 0, N>>LM); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 511 | best_L1 = L1; |
| 512 | /*printf ("%f ", L1);*/ |
| 513 | for (k=0;k<LM;k++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 514 | { |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 515 | int B; |
| 516 | |
| 517 | if (isTransient) |
| 518 | B = (LM-k-1); |
| 519 | else |
| 520 | B = k+1; |
| 521 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 522 | if (isTransient) |
| 523 | haar1(tmp, N>>(LM-k), 1<<(LM-k)); |
| 524 | else |
| 525 | haar1(tmp, N>>k, 1<<k); |
| 526 | |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 527 | L1 = l1_metric(tmp, N, B, N>>LM); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 528 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 529 | if (L1 < best_L1) |
| 530 | { |
| 531 | best_L1 = L1; |
| 532 | best_level = k+1; |
| 533 | } |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 534 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 535 | /*printf ("%d ", isTransient ? LM-best_level : best_level);*/ |
| 536 | if (isTransient) |
| 537 | metric[i] = best_level; |
| 538 | else |
| 539 | metric[i] = -best_level; |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 540 | *tf_sum += metric[i]; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 541 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 542 | /*printf("\n");*/ |
| 543 | /* FIXME: Figure out how to set this */ |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 544 | tf_select = 0; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 545 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 546 | cost0 = 0; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 547 | cost1 = isTransient ? 0 : lambda; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 548 | /* Viterbi forward pass */ |
| 549 | for (i=1;i<len;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 550 | { |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 551 | int curr0, curr1; |
| 552 | int from0, from1; |
Jean-Marc Valin | 581fdba | 2010-05-28 06:56:23 -0400 | [diff] [blame] | 553 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 554 | from0 = cost0; |
| 555 | from1 = cost1 + lambda; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 556 | if (from0 < from1) |
| 557 | { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 558 | curr0 = from0; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 559 | path0[i]= 0; |
| 560 | } else { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 561 | curr0 = from1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 562 | path0[i]= 1; |
| 563 | } |
| 564 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 565 | from0 = cost0 + lambda; |
| 566 | from1 = cost1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 567 | if (from0 < from1) |
| 568 | { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 569 | curr1 = from0; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 570 | path1[i]= 0; |
| 571 | } else { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 572 | curr1 = from1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 573 | path1[i]= 1; |
| 574 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame] | 575 | cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]); |
| 576 | 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] | 577 | } |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 578 | tf_res[len-1] = cost0 < cost1 ? 0 : 1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 579 | /* Viterbi backward pass to check the decisions */ |
| 580 | for (i=len-2;i>=0;i--) |
| 581 | { |
| 582 | if (tf_res[i+1] == 1) |
| 583 | tf_res[i] = path1[i+1]; |
| 584 | else |
| 585 | tf_res[i] = path0[i+1]; |
| 586 | } |
Jean-Marc Valin | 71ae6d4 | 2010-06-27 21:55:08 -0400 | [diff] [blame] | 587 | RESTORE_STACK; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 588 | return tf_select; |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 589 | } |
| 590 | |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 591 | 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] | 592 | { |
| 593 | int curr, i; |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 594 | ec_enc_bit_prob(enc, tf_res[start], isTransient ? 16384 : 4096); |
| 595 | curr = tf_res[start]; |
| 596 | for (i=start+1;i<end;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 597 | { |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 598 | ec_enc_bit_prob(enc, tf_res[i] ^ curr, isTransient ? 4096 : 2048); |
| 599 | curr = tf_res[i]; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 600 | } |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 601 | if (LM!=0) |
| 602 | ec_enc_bits(enc, tf_select, 1); |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 603 | for (i=start;i<end;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 604 | 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] | 605 | /*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] | 606 | } |
| 607 | |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 608 | static void tf_decode(int start, int end, int C, int isTransient, int *tf_res, int LM, ec_dec *dec) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 609 | { |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 610 | int i, curr, tf_select; |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 611 | tf_res[start] = ec_dec_bit_prob(dec, isTransient ? 16384 : 4096); |
| 612 | curr = tf_res[start]; |
| 613 | for (i=start+1;i<end;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 614 | { |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 615 | tf_res[i] = ec_dec_bit_prob(dec, isTransient ? 4096 : 2048) ^ curr; |
| 616 | curr = tf_res[i]; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 617 | } |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 618 | if (LM!=0) |
| 619 | tf_select = ec_dec_bits(dec, 1); |
| 620 | else |
| 621 | tf_select = 0; |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 622 | for (i=start;i<end;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 623 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 624 | } |
| 625 | |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 626 | static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X, |
| 627 | const celt_word16 *bandLogE, int nbEBands, int LM, int C, int N0) |
| 628 | { |
| 629 | int i; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 630 | celt_word32 diff=0; |
| 631 | int c; |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 632 | int trim_index = 5; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 633 | if (C==2) |
| 634 | { |
| 635 | celt_word16 sum = 0; /* Q10 */ |
| 636 | /* Compute inter-channel correlation for low frequencies */ |
| 637 | for (i=0;i<8;i++) |
| 638 | { |
| 639 | int j; |
| 640 | celt_word32 partial = 0; |
| 641 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) |
| 642 | partial = MAC16_16(partial, X[j], X[N0+j]); |
| 643 | sum = ADD16(sum, EXTRACT16(SHR32(partial, 18))); |
| 644 | } |
| 645 | sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum); |
| 646 | /*printf ("%f\n", sum);*/ |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 647 | if (sum > QCONST16(.995f,10)) |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 648 | trim_index-=4; |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 649 | else if (sum > QCONST16(.92f,10)) |
Jean-Marc Valin | 4b087df | 2010-11-30 21:08:31 -0500 | [diff] [blame] | 650 | trim_index-=3; |
| 651 | else if (sum > QCONST16(.85f,10)) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 652 | trim_index-=2; |
Gregory Maxwell | 60c316b | 2010-11-04 20:14:19 -0400 | [diff] [blame] | 653 | else if (sum > QCONST16(.8f,10)) |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 654 | trim_index-=1; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 655 | } |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 656 | |
| 657 | /* Estimate spectral tilt */ |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 658 | c=0; do { |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 659 | for (i=0;i<nbEBands-1;i++) |
| 660 | { |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 661 | diff += bandLogE[i+c*nbEBands]*(celt_int32)(2+2*i-nbEBands); |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 662 | } |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 663 | } while (++c<0); |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 664 | diff /= C*(nbEBands-1); |
| 665 | /*printf("%f\n", diff);*/ |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 666 | if (diff > QCONST16(2.f, DB_SHIFT)) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 667 | trim_index--; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 668 | if (diff > QCONST16(8.f, DB_SHIFT)) |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 669 | trim_index--; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 670 | if (diff < -QCONST16(4.f, DB_SHIFT)) |
Jean-Marc Valin | 9037757 | 2010-10-22 15:12:01 -0400 | [diff] [blame] | 671 | trim_index++; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 672 | if (diff < -QCONST16(10.f, DB_SHIFT)) |
| 673 | trim_index++; |
| 674 | |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 675 | if (trim_index<0) |
| 676 | trim_index = 0; |
Jean-Marc Valin | 546dfa1 | 2010-12-10 17:18:17 -0500 | [diff] [blame] | 677 | if (trim_index>10) |
| 678 | trim_index = 10; |
Jean-Marc Valin | c5792de | 2010-10-19 14:24:50 -0400 | [diff] [blame] | 679 | return trim_index; |
| 680 | } |
| 681 | |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 682 | static int stereo_analysis(const CELTMode *m, const celt_norm *X, |
| 683 | int nbEBands, int LM, int C, int N0) |
| 684 | { |
| 685 | int i; |
| 686 | int thetas; |
| 687 | celt_word32 sumLR = EPSILON, sumMS = EPSILON; |
| 688 | |
| 689 | /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */ |
| 690 | for (i=0;i<13;i++) |
| 691 | { |
| 692 | int j; |
| 693 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) |
| 694 | { |
| 695 | celt_word16 L, R, M, S; |
| 696 | L = X[j]; |
| 697 | R = X[N0+j]; |
| 698 | M = L+R; |
| 699 | S = L-R; |
| 700 | sumLR += EXTEND32(ABS16(L)) + EXTEND32(ABS16(R)); |
| 701 | sumMS += EXTEND32(ABS16(M)) + EXTEND32(ABS16(S)); |
| 702 | } |
| 703 | } |
| 704 | sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS); |
| 705 | thetas = 13; |
| 706 | /* We don't need thetas for lower bands with LM<=1 */ |
| 707 | if (LM<=1) |
| 708 | thetas -= 8; |
| 709 | return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS) |
| 710 | > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR); |
| 711 | } |
| 712 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 713 | #ifdef FIXED_POINT |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 714 | int celt_encode_with_ec(CELTEncoder * restrict st, const celt_int16 * 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] | 715 | { |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 716 | #else |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 717 | int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 718 | { |
| 719 | #endif |
Jean-Marc Valin | 7a08ddd | 2010-10-18 14:55:42 -0400 | [diff] [blame] | 720 | int i, c, N; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 721 | int bits; |
Jean-Marc Valin | bc79991 | 2008-11-07 22:53:13 -0500 | [diff] [blame] | 722 | int has_fold=1; |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 723 | ec_byte_buffer buf; |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 724 | ec_enc _enc; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 725 | VARDECL(celt_sig, in); |
| 726 | VARDECL(celt_sig, freq); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 727 | VARDECL(celt_norm, X); |
| 728 | VARDECL(celt_ener, bandE); |
| 729 | VARDECL(celt_word16, bandLogE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 730 | VARDECL(int, fine_quant); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 731 | VARDECL(celt_word16, error); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 732 | VARDECL(int, pulses); |
| 733 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 734 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 735 | VARDECL(int, tf_res); |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 736 | celt_sig *_overlap_mem; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 737 | celt_sig *prefilter_mem; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 738 | celt_word16 *oldBandE; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 739 | int shortBlocks=0; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 740 | int isTransient=0; |
Jean-Marc Valin | b8ba70c | 2010-04-18 22:10:24 -0400 | [diff] [blame] | 741 | int resynth; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 742 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 743 | int LM, M; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 744 | int tf_select; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 745 | int nbFilledBytes, nbAvailableBytes; |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 746 | int effEnd; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 747 | int codedBands; |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 748 | int tf_sum; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 749 | int alloc_trim; |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 750 | int pitch_index=COMBFILTER_MINPERIOD; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 751 | celt_word16 gain1 = 0; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 752 | int intensity=0; |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 753 | int dual_stereo=0; |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 754 | int effectiveBytes; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 755 | SAVE_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 756 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 757 | if (nbCompressedBytes<0 || pcm==NULL) |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 758 | return CELT_BAD_ARG; |
Gregory Maxwell | 520eeae | 2009-02-09 01:33:21 -0500 | [diff] [blame] | 759 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 760 | for (LM=0;LM<4;LM++) |
| 761 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 762 | break; |
| 763 | if (LM>=MAX_CONFIG_SIZES) |
| 764 | return CELT_BAD_ARG; |
| 765 | M=1<<LM; |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 766 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 767 | prefilter_mem = st->in_mem+C*(st->overlap); |
| 768 | _overlap_mem = prefilter_mem+C*COMBFILTER_MAXPERIOD; |
| 769 | /*_overlap_mem = st->in_mem+C*(st->overlap);*/ |
| 770 | oldBandE = (celt_word16*)(st->in_mem+C*(2*st->overlap+COMBFILTER_MAXPERIOD)); |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 771 | |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 772 | if (enc==NULL) |
| 773 | { |
| 774 | ec_byte_writeinit_buffer(&buf, compressed, nbCompressedBytes); |
| 775 | ec_enc_init(&_enc,&buf); |
| 776 | enc = &_enc; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 777 | nbFilledBytes=0; |
| 778 | } else { |
Jean-Marc Valin | bdcaaf7 | 2010-07-05 13:52:41 -0400 | [diff] [blame] | 779 | nbFilledBytes=(ec_enc_tell(enc, 0)+4)>>3; |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 780 | } |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 781 | nbAvailableBytes = nbCompressedBytes - nbFilledBytes; |
| 782 | |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 783 | if (st->vbr_rate_norm>0) |
| 784 | effectiveBytes = st->vbr_rate_norm>>BITRES<<LM>>3; |
| 785 | else |
| 786 | effectiveBytes = nbCompressedBytes; |
| 787 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 788 | effEnd = st->end; |
| 789 | if (effEnd > st->mode->effEBands) |
| 790 | effEnd = st->mode->effEBands; |
| 791 | |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 792 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 793 | ALLOC(in, C*(N+st->overlap), celt_sig); |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 794 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 795 | /* Find pitch period and gain */ |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 796 | { |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 797 | VARDECL(celt_sig, _pre); |
| 798 | celt_sig *pre[2]; |
| 799 | SAVE_STACK; |
| 800 | c = 0; |
| 801 | ALLOC(_pre, C*(N+COMBFILTER_MAXPERIOD), celt_sig); |
| 802 | |
| 803 | pre[0] = _pre; |
| 804 | pre[1] = _pre + (N+COMBFILTER_MAXPERIOD); |
| 805 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 806 | c=0; do { |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 807 | const celt_word16 * restrict pcmp = pcm+c; |
| 808 | celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap; |
| 809 | |
| 810 | for (i=0;i<N;i++) |
| 811 | { |
| 812 | /* Apply pre-emphasis */ |
| 813 | celt_sig tmp = MULT16_16(st->mode->preemph[2], SCALEIN(*pcmp)); |
| 814 | *inp = tmp + st->preemph_memE[c]; |
| 815 | st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp) |
| 816 | - MULT16_32_Q15(st->mode->preemph[0], tmp); |
| 817 | inp++; |
| 818 | pcmp+=C; |
| 819 | } |
| 820 | CELT_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD); |
| 821 | CELT_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N); |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 822 | } while (++c<C); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 823 | |
| 824 | #ifdef ENABLE_POSTFILTER |
Jean-Marc Valin | cd84e3d | 2010-12-16 22:29:35 -0500 | [diff] [blame^] | 825 | if (nbAvailableBytes>12*C) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 826 | { |
| 827 | VARDECL(celt_word16, pitch_buf); |
| 828 | ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, celt_word16); |
| 829 | celt_word32 tmp=0; |
| 830 | celt_word32 mem0[2]={0,0}; |
| 831 | celt_word16 mem1[2]={0,0}; |
| 832 | |
| 833 | pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD+N, |
| 834 | C, mem0, mem1); |
| 835 | pitch_search(st->mode, pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N, |
| 836 | COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index, &tmp, 1<<LM); |
| 837 | pitch_index = COMBFILTER_MAXPERIOD-pitch_index; |
| 838 | |
| 839 | gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD, |
| 840 | N, &pitch_index, st->prefilter_period, st->prefilter_gain); |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 841 | if (pitch_index > COMBFILTER_MAXPERIOD) |
| 842 | pitch_index = COMBFILTER_MAXPERIOD; |
| 843 | gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1); |
| 844 | if (gain1 > QCONST16(.6f,15)) |
| 845 | gain1 = QCONST16(.6f,15); |
| 846 | if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1,15)) |
| 847 | gain1=st->prefilter_gain; |
| 848 | } else { |
| 849 | gain1 = 0; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 850 | } |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 851 | if (gain1<QCONST16(.2f,15) || (nbAvailableBytes<30 && gain1<QCONST16(.4f,15))) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 852 | { |
| 853 | ec_enc_bit_prob(enc, 0, 32768); |
| 854 | gain1 = 0; |
| 855 | } else { |
| 856 | int qg; |
| 857 | int octave; |
| 858 | #ifdef FIXED_POINT |
| 859 | qg = ((gain1+2048)>>12)-2; |
| 860 | #else |
| 861 | qg = floor(.5+gain1*8)-2; |
| 862 | #endif |
| 863 | ec_enc_bit_prob(enc, 1, 32768); |
| 864 | octave = EC_ILOG(pitch_index)-5; |
| 865 | ec_enc_uint(enc, octave, 6); |
| 866 | ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave); |
| 867 | ec_enc_bits(enc, qg, 2); |
| 868 | gain1 = QCONST16(.125f,15)*(qg+2); |
| 869 | } |
| 870 | /*printf("%d %f\n", pitch_index, gain1);*/ |
| 871 | #else /* ENABLE_POSTFILTER */ |
| 872 | ec_enc_bit_prob(enc, 0, 32768); |
| 873 | #endif /* ENABLE_POSTFILTER */ |
| 874 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 875 | c=0; do { |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 876 | st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 877 | CELT_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap); |
| 878 | #ifdef ENABLE_POSTFILTER |
| 879 | comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD, |
| 880 | st->prefilter_period, pitch_index, N, C, -st->prefilter_gain, -gain1, st->mode->window, st->mode->overlap); |
| 881 | #endif /* ENABLE_POSTFILTER */ |
| 882 | CELT_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap); |
| 883 | |
| 884 | #ifdef ENABLE_POSTFILTER |
| 885 | if (N>COMBFILTER_MAXPERIOD) |
| 886 | { |
| 887 | CELT_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD); |
| 888 | } else { |
| 889 | CELT_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N); |
| 890 | CELT_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N); |
| 891 | } |
| 892 | #endif /* ENABLE_POSTFILTER */ |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 893 | } while (++c<C); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 894 | |
| 895 | RESTORE_STACK; |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 896 | } |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 897 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 898 | #ifdef RESYNTH |
| 899 | resynth = 1; |
| 900 | #else |
| 901 | resynth = 0; |
| 902 | #endif |
Jean-Marc Valin | b8ba70c | 2010-04-18 22:10:24 -0400 | [diff] [blame] | 903 | |
Jean-Marc Valin | f9fdbff | 2010-09-05 21:02:38 -0400 | [diff] [blame] | 904 | if (st->complexity > 1 && LM>0) |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 905 | { |
| 906 | isTransient = M > 1 && |
Jean-Marc Valin | 7a08ddd | 2010-10-18 14:55:42 -0400 | [diff] [blame] | 907 | transient_analysis(in, N+st->overlap, C, &st->frame_max, st->overlap); |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 908 | } else { |
| 909 | isTransient = 0; |
| 910 | } |
Jean-Marc Valin | c5f2a9d | 2008-10-26 22:00:26 -0400 | [diff] [blame] | 911 | |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 912 | if (isTransient) |
| 913 | shortBlocks = M; |
| 914 | else |
| 915 | shortBlocks = 0; |
| 916 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 917 | ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 918 | ALLOC(bandE,st->mode->nbEBands*C, celt_ener); |
| 919 | ALLOC(bandLogE,st->mode->nbEBands*C, celt_word16); |
Jean-Marc Valin | 32ec58c | 2009-05-01 21:28:58 -0400 | [diff] [blame] | 920 | /* Compute MDCTs */ |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 921 | compute_mdcts(st->mode, shortBlocks, in, freq, C, LM); |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 922 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 923 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
Jean-Marc Valin | 8d4ac15 | 2008-02-29 17:24:02 +1100 | [diff] [blame] | 924 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 925 | compute_band_energies(st->mode, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | 504fb3c | 2010-08-06 15:56:22 -0400 | [diff] [blame] | 926 | |
| 927 | amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 928 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 929 | /* Band normalisation */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 930 | normalise_bands(st->mode, freq, X, bandE, effEnd, C, M); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 931 | |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 932 | ALLOC(tf_res, st->mode->nbEBands, int); |
| 933 | /* Needs to be before coarse energy quantization because otherwise the energy gets modified */ |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 934 | tf_select = tf_analysis(st->mode, bandLogE, oldBandE, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum); |
Jean-Marc Valin | 8200b2d | 2010-10-15 02:18:47 -0400 | [diff] [blame] | 935 | for (i=effEnd;i<st->end;i++) |
| 936 | tf_res[i] = tf_res[effEnd-1]; |
| 937 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 938 | ALLOC(error, C*st->mode->nbEBands, celt_word16); |
Jean-Marc Valin | 5e7f02d | 2010-08-08 09:48:22 -0400 | [diff] [blame] | 939 | quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE, |
Timothy B. Terriberry | ef2e650 | 2010-11-09 01:43:18 -0800 | [diff] [blame] | 940 | oldBandE, nbCompressedBytes*8, error, enc, |
| 941 | C, LM, nbAvailableBytes, st->force_intra, |
Jean-Marc Valin | 1b36d6c | 2010-08-31 17:21:52 -0400 | [diff] [blame] | 942 | &st->delayedIntra, st->complexity >= 4); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 943 | |
Jean-Marc Valin | f9fdbff | 2010-09-05 21:02:38 -0400 | [diff] [blame] | 944 | if (LM > 0) |
| 945 | ec_enc_bit_prob(enc, shortBlocks!=0, 8192); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 946 | |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 947 | 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] | 948 | |
Jean-Marc Valin | 5c80391 | 2010-12-16 14:11:48 -0500 | [diff] [blame] | 949 | if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C) |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 950 | { |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 951 | if (st->complexity == 0) |
| 952 | { |
| 953 | has_fold = 0; |
| 954 | st->fold_decision = 3; |
| 955 | } else { |
| 956 | has_fold = 1; |
| 957 | st->fold_decision = 1; |
| 958 | } |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 959 | } else { |
| 960 | has_fold = folding_decision(st->mode, X, &st->tonal_average, &st->fold_decision, effEnd, C, M); |
| 961 | } |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 962 | ec_enc_bit_prob(enc, has_fold>>1, 8192); |
| 963 | ec_enc_bit_prob(enc, has_fold&1, (has_fold>>1) ? 32768 : 49152); |
| 964 | |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 965 | ALLOC(offsets, st->mode->nbEBands, int); |
| 966 | |
| 967 | for (i=0;i<st->mode->nbEBands;i++) |
| 968 | offsets[i] = 0; |
| 969 | /* Dynamic allocation code */ |
| 970 | /* 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] | 971 | if (effectiveBytes > 50 && LM>=1) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 972 | { |
| 973 | int t1, t2; |
| 974 | if (LM <= 1) |
| 975 | { |
| 976 | t1 = 3; |
| 977 | t2 = 5; |
| 978 | } else { |
| 979 | t1 = 2; |
| 980 | t2 = 4; |
| 981 | } |
| 982 | for (i=1;i<st->mode->nbEBands-1;i++) |
| 983 | { |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 984 | celt_word32 d2; |
| 985 | d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1]; |
| 986 | if (C==2) |
| 987 | d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]- |
| 988 | bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]); |
| 989 | if (d2 > SHL16(t1,DB_SHIFT)) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 990 | offsets[i] += 1; |
Jean-Marc Valin | a3a066c | 2010-11-04 15:15:54 -0400 | [diff] [blame] | 991 | if (d2 > SHL16(t2,DB_SHIFT)) |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 992 | offsets[i] += 1; |
| 993 | } |
| 994 | } |
| 995 | for (i=0;i<st->mode->nbEBands;i++) |
| 996 | { |
| 997 | int j; |
| 998 | ec_enc_bit_prob(enc, offsets[i]!=0, 1024); |
| 999 | if (offsets[i]!=0) |
| 1000 | { |
| 1001 | for (j=0;j<offsets[i]-1;j++) |
| 1002 | ec_enc_bit_prob(enc, 1, 32768); |
| 1003 | ec_enc_bit_prob(enc, 0, 32768); |
| 1004 | } |
| 1005 | offsets[i] *= (6<<BITRES); |
| 1006 | } |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 1007 | alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE, st->mode->nbEBands, LM, C, N); |
| 1008 | ec_encode_bin(enc, trim_cdf[alloc_trim], trim_cdf[alloc_trim+1], 7); |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1009 | |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1010 | /* Variable bitrate */ |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1011 | if (st->vbr_rate_norm>0) |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1012 | { |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1013 | celt_word16 alpha; |
Jean-Marc Valin | a4badac | 2010-12-03 15:20:11 -0500 | [diff] [blame] | 1014 | celt_int32 delta, tell; |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1015 | /* The target rate in 8th bits per frame */ |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1016 | celt_int32 vbr_rate; |
| 1017 | celt_int32 target; |
Jean-Marc Valin | a4badac | 2010-12-03 15:20:11 -0500 | [diff] [blame] | 1018 | celt_int32 vbr_bound, max_allowed, min_allowed; |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1019 | |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1020 | target = vbr_rate = M*st->vbr_rate_norm; |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1021 | |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 1022 | target = target + st->vbr_offset - ((40*C+20)<<BITRES); |
| 1023 | |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1024 | /* Shortblocks get a large boost in bitrate, but since they |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 1025 | are uncommon long blocks are not greatly affected */ |
| 1026 | if (shortBlocks || tf_sum < -2*(st->end-st->start)) |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 1027 | target = 7*target/4; |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 1028 | else if (tf_sum < -(st->end-st->start)) |
| 1029 | target = 3*target/2; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1030 | else if (M > 1) |
Jean-Marc Valin | ccd5a61 | 2010-10-13 01:11:55 -0400 | [diff] [blame] | 1031 | target-=(target+14)/28; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1032 | |
Jean-Marc Valin | a4badac | 2010-12-03 15:20:11 -0500 | [diff] [blame] | 1033 | tell = ec_enc_tell(enc, BITRES); |
| 1034 | |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1035 | /* The current offset is removed from the target and the space used |
| 1036 | so far is added*/ |
Jean-Marc Valin | a4badac | 2010-12-03 15:20:11 -0500 | [diff] [blame] | 1037 | target=target+tell; |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 1038 | /* By how much did we "miss" the target on that frame */ |
| 1039 | delta = target - vbr_rate; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1040 | |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1041 | /* Computes the max bit-rate allowed in VBR more to avoid violating the target rate and buffering */ |
| 1042 | vbr_bound = vbr_rate; |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 1043 | if (st->constrained_vbr) |
| 1044 | max_allowed = IMIN(vbr_rate+vbr_bound-st->vbr_reservoir>>(BITRES+3),nbAvailableBytes); |
| 1045 | else |
| 1046 | max_allowed = nbAvailableBytes; |
Jean-Marc Valin | a4badac | 2010-12-03 15:20:11 -0500 | [diff] [blame] | 1047 | min_allowed = (tell>>(BITRES+3)) + 2 - nbFilledBytes; |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1048 | |
| 1049 | /* In VBR mode the frame size must not be reduced so much that it would result in the encoder running out of bits */ |
| 1050 | nbAvailableBytes = target+(1<<(BITRES+2))>>(BITRES+3); |
Jean-Marc Valin | a4badac | 2010-12-03 15:20:11 -0500 | [diff] [blame] | 1051 | nbAvailableBytes=IMAX(min_allowed,IMIN(max_allowed,nbAvailableBytes)); |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1052 | target=nbAvailableBytes<<(BITRES+3); |
| 1053 | |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1054 | if (st->vbr_count < 970) |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1055 | { |
| 1056 | st->vbr_count++; |
Gregory Maxwell | a941147 | 2010-10-28 03:52:21 -0400 | [diff] [blame] | 1057 | alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16)); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1058 | } else |
| 1059 | alpha = QCONST16(.001f,15); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1060 | /* 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] | 1061 | if (st->constrained_vbr) |
| 1062 | st->vbr_reservoir += target - vbr_rate; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1063 | /*printf ("%d\n", st->vbr_reservoir);*/ |
| 1064 | |
| 1065 | /* Compute the offset we need to apply in order to reach the target */ |
Jean-Marc Valin | 736efd6 | 2010-08-31 11:52:45 -0400 | [diff] [blame] | 1066 | st->vbr_drift += (celt_int32)MULT16_32_Q15(alpha,delta-st->vbr_offset-st->vbr_drift); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1067 | st->vbr_offset = -st->vbr_drift; |
| 1068 | /*printf ("%d\n", st->vbr_drift);*/ |
| 1069 | |
| 1070 | /* We could use any multiple of vbr_rate as bound (depending on the delay) */ |
Jean-Marc Valin | 9faf740 | 2010-12-04 10:27:22 -0500 | [diff] [blame] | 1071 | if (st->constrained_vbr && st->vbr_reservoir < 0) |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1072 | { |
| 1073 | /* We're under the min value -- increase rate */ |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1074 | int adjust = (-st->vbr_reservoir)/(8<<BITRES); |
| 1075 | nbAvailableBytes += adjust; |
| 1076 | st->vbr_reservoir = 0; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1077 | /*printf ("+%d\n", adjust);*/ |
| 1078 | } |
Gregory Maxwell | fac6c98 | 2010-10-28 15:00:37 -0400 | [diff] [blame] | 1079 | nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes); |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 1080 | |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1081 | /* This moves the raw bits to take into account the new compressed size */ |
Jean-Marc Valin | e610864 | 2009-08-01 23:05:47 +0200 | [diff] [blame] | 1082 | ec_byte_shrink(&buf, nbCompressedBytes); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1083 | } |
| 1084 | |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1085 | if (C==2) |
| 1086 | { |
Jean-Marc Valin | cd84e3d | 2010-12-16 22:29:35 -0500 | [diff] [blame^] | 1087 | /* Always use MS for 2.5 ms frames until we can do a better analysis */ |
| 1088 | if (LM==0) |
| 1089 | dual_stereo = 0; |
| 1090 | else |
| 1091 | dual_stereo = stereo_analysis(st->mode, X, st->mode->nbEBands, LM, C, N); |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 1092 | ec_enc_bit_prob(enc, dual_stereo, 32768); |
| 1093 | } |
| 1094 | if (C==2) |
| 1095 | { |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1096 | int effectiveRate; |
| 1097 | |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1098 | /* Account for coarse energy */ |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 1099 | effectiveRate = (8*effectiveBytes - 80)>>LM; |
| 1100 | |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1101 | /* effectiveRate in kb/s */ |
| 1102 | effectiveRate = 2*effectiveRate/5; |
| 1103 | if (effectiveRate<35) |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 1104 | intensity = 8; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1105 | else if (effectiveRate<50) |
| 1106 | intensity = 12; |
| 1107 | else if (effectiveRate<68) |
| 1108 | intensity = 16; |
| 1109 | else if (effectiveRate<84) |
| 1110 | intensity = 18; |
| 1111 | else if (effectiveRate<102) |
| 1112 | intensity = 19; |
| 1113 | else if (effectiveRate<130) |
| 1114 | intensity = 20; |
| 1115 | else |
| 1116 | intensity = 100; |
| 1117 | intensity = IMIN(st->end,IMAX(st->start, intensity)); |
| 1118 | ec_enc_uint(enc, intensity, 1+st->end-st->start); |
| 1119 | } |
| 1120 | |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 1121 | /* Bit allocation */ |
| 1122 | ALLOC(fine_quant, st->mode->nbEBands, int); |
| 1123 | ALLOC(pulses, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1124 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 1125 | |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 1126 | /* bits = packet size - where we are - safety */ |
| 1127 | bits = (nbCompressedBytes*8<<BITRES) - ec_enc_tell(enc, BITRES) - 1; |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 1128 | codedBands = compute_allocation(st->mode, st->start, st->end, offsets, |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 1129 | alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands); |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 1130 | st->lastCodedBands = codedBands; |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 1131 | |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1132 | quant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, error, fine_quant, enc, C); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1133 | |
Jean-Marc Valin | 7b5a086 | 2010-07-29 15:01:24 +0200 | [diff] [blame] | 1134 | #ifdef MEASURE_NORM_MSE |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 1135 | float X0[3000]; |
| 1136 | float bandE0[60]; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1137 | c=0; do |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 1138 | for (i=0;i<N;i++) |
| 1139 | X0[i+c*N] = X[i+c*N]; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1140 | while (++c<C); |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 1141 | for (i=0;i<C*st->mode->nbEBands;i++) |
Jean-Marc Valin | 7b5a086 | 2010-07-29 15:01:24 +0200 | [diff] [blame] | 1142 | bandE0[i] = bandE[i]; |
| 1143 | #endif |
| 1144 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 1145 | /* Residual quantisation */ |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1146 | quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 1147 | bandE, pulses, shortBlocks, has_fold, dual_stereo, intensity, tf_res, resynth, |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1148 | nbCompressedBytes*8, enc, LM, codedBands); |
Jean-Marc Valin | 3971053 | 2009-06-09 00:10:32 -0400 | [diff] [blame] | 1149 | |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1150 | quant_energy_finalise(st->mode, st->start, st->end, bandE, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_enc_tell(enc, 0), enc, C); |
Jean-Marc Valin | 3971053 | 2009-06-09 00:10:32 -0400 | [diff] [blame] | 1151 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1152 | #ifdef RESYNTH |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 1153 | /* Re-synthesis of the coded audio if required */ |
Jean-Marc Valin | b8ba70c | 2010-04-18 22:10:24 -0400 | [diff] [blame] | 1154 | if (resynth) |
Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 1155 | { |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 1156 | celt_sig *out_mem[2]; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1157 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 1158 | |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1159 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 1160 | |
| 1161 | #ifdef MEASURE_NORM_MSE |
| 1162 | measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C); |
| 1163 | #endif |
| 1164 | |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1165 | /* Synthesis */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1166 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1167 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1168 | CELT_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD); |
| 1169 | if (C==2) |
| 1170 | CELT_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD); |
| 1171 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1172 | c=0; do |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 1173 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 1174 | freq[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1175 | while (++c<C); |
| 1176 | c=0; do |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 1177 | for (i=M*st->mode->eBands[st->end];i<N;i++) |
| 1178 | freq[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1179 | while (++c<C); |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 1180 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1181 | out_mem[0] = st->syn_mem[0]+MAX_PERIOD; |
| 1182 | if (C==2) |
| 1183 | out_mem[1] = st->syn_mem[1]+MAX_PERIOD; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1184 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1185 | c=0; do |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 1186 | overlap_mem[c] = _overlap_mem + c*st->overlap; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1187 | while (++c<C); |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 1188 | |
Jean-Marc Valin | 7a08ddd | 2010-10-18 14:55:42 -0400 | [diff] [blame] | 1189 | compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, C, LM); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 1190 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1191 | #ifdef ENABLE_POSTFILTER |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1192 | c=0; do { |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1193 | st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); |
| 1194 | st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD); |
| 1195 | if (LM!=0) |
| 1196 | { |
| 1197 | comb_filter(out_mem[c], out_mem[c], st->prefilter_period, st->prefilter_period, st->overlap, C, |
| 1198 | st->prefilter_gain, st->prefilter_gain, NULL, 0); |
| 1199 | comb_filter(out_mem[c]+st->overlap, out_mem[c]+st->overlap, st->prefilter_period, pitch_index, N-st->overlap, C, |
| 1200 | st->prefilter_gain, gain1, st->mode->window, st->mode->overlap); |
| 1201 | } else { |
| 1202 | comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, N, C, |
| 1203 | st->prefilter_gain_old, st->prefilter_gain, st->mode->window, st->mode->overlap); |
| 1204 | } |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1205 | } while (++c<C); |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1206 | #endif /* ENABLE_POSTFILTER */ |
| 1207 | |
| 1208 | deemphasis(out_mem, (celt_word16*)pcm, N, C, st->mode->preemph, st->preemph_memD); |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1209 | st->prefilter_period_old = st->prefilter_period; |
| 1210 | st->prefilter_gain_old = st->prefilter_gain; |
Jean-Marc Valin | d9b9565 | 2008-08-31 23:34:47 -0400 | [diff] [blame] | 1211 | } |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1212 | #endif |
| 1213 | |
| 1214 | st->prefilter_period = pitch_index; |
| 1215 | st->prefilter_gain = gain1; |
Gregory Maxwell | 54547f1 | 2009-02-16 18:56:44 -0500 | [diff] [blame] | 1216 | |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 1217 | /* If there's any room left (can only happen for very high rates), |
| 1218 | fill it with zeros */ |
Jean-Marc Valin | 5d774e0 | 2010-08-04 17:17:18 -0400 | [diff] [blame] | 1219 | while (ec_enc_tell(enc,0) + 8 <= nbCompressedBytes*8) |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 1220 | ec_enc_bits(enc, 0, 8); |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1221 | ec_enc_done(enc); |
Jean-Marc Valin | c871c8d | 2009-06-09 00:57:00 -0400 | [diff] [blame] | 1222 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1223 | RESTORE_STACK; |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1224 | if (ec_enc_get_error(enc)) |
Jean-Marc Valin | 9d785af | 2010-07-18 09:42:05 -0400 | [diff] [blame] | 1225 | return CELT_CORRUPTED_DATA; |
| 1226 | else |
| 1227 | return nbCompressedBytes; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 1228 | } |
| 1229 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1230 | #ifdef FIXED_POINT |
| 1231 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1232 | int celt_encode_with_ec_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1233 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1234 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1235 | VARDECL(celt_int16, in); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1236 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1237 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1238 | if (pcm==NULL) |
| 1239 | return CELT_BAD_ARG; |
| 1240 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1241 | for (LM=0;LM<4;LM++) |
| 1242 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1243 | break; |
| 1244 | if (LM>=MAX_CONFIG_SIZES) |
| 1245 | return CELT_BAD_ARG; |
| 1246 | M=1<<LM; |
| 1247 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1248 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1249 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1250 | ALLOC(in, C*N, celt_int16); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1251 | |
| 1252 | for (j=0;j<C*N;j++) |
| 1253 | in[j] = FLOAT2INT16(pcm[j]); |
| 1254 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1255 | ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, enc); |
| 1256 | #ifdef RESYNTH |
| 1257 | for (j=0;j<C*N;j++) |
| 1258 | ((float*)pcm)[j]=in[j]*(1.f/32768.f); |
| 1259 | #endif |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1260 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1261 | return ret; |
| 1262 | |
| 1263 | } |
| 1264 | #endif /*DISABLE_FLOAT_API*/ |
| 1265 | #else |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1266 | int celt_encode_with_ec(CELTEncoder * restrict st, const celt_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1267 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1268 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1269 | VARDECL(celt_sig, in); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1270 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1271 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1272 | if (pcm==NULL) |
| 1273 | return CELT_BAD_ARG; |
| 1274 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1275 | for (LM=0;LM<4;LM++) |
| 1276 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1277 | break; |
| 1278 | if (LM>=MAX_CONFIG_SIZES) |
| 1279 | return CELT_BAD_ARG; |
| 1280 | M=1<<LM; |
| 1281 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1282 | C=CHANNELS(st->channels); |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1283 | N=M*st->mode->shortMdctSize; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1284 | ALLOC(in, C*N, celt_sig); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1285 | for (j=0;j<C*N;j++) { |
| 1286 | in[j] = SCALEOUT(pcm[j]); |
| 1287 | } |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1288 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1289 | ret = celt_encode_with_ec_float(st,in,frame_size,compressed,nbCompressedBytes, enc); |
| 1290 | #ifdef RESYNTH |
| 1291 | for (j=0;j<C*N;j++) |
| 1292 | ((celt_int16*)pcm)[j] = FLOAT2INT16(in[j]); |
| 1293 | #endif |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1294 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1295 | return ret; |
| 1296 | } |
| 1297 | #endif |
Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 1298 | |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1299 | int celt_encode(CELTEncoder * restrict st, const celt_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
| 1300 | { |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1301 | 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] | 1302 | } |
| 1303 | |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1304 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1305 | int celt_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
| 1306 | { |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1307 | return celt_encode_with_ec_float(st, pcm, frame_size, compressed, nbCompressedBytes, NULL); |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1308 | } |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1309 | #endif /* DISABLE_FLOAT_API */ |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1310 | |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1311 | int celt_encoder_ctl(CELTEncoder * restrict st, int request, ...) |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1312 | { |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1313 | va_list ap; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1314 | |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1315 | va_start(ap, request); |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1316 | switch (request) |
| 1317 | { |
Gregory Maxwell | f3b44ef | 2009-06-03 13:37:45 -0400 | [diff] [blame] | 1318 | case CELT_GET_MODE_REQUEST: |
| 1319 | { |
| 1320 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 1321 | if (value==0) |
| 1322 | goto bad_arg; |
| 1323 | *value=st->mode; |
| 1324 | } |
| 1325 | break; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1326 | case CELT_SET_COMPLEXITY_REQUEST: |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1327 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1328 | int value = va_arg(ap, celt_int32); |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1329 | if (value<0 || value>10) |
| 1330 | goto bad_arg; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1331 | st->complexity = value; |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1332 | } |
| 1333 | break; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1334 | case CELT_SET_START_BAND_REQUEST: |
| 1335 | { |
| 1336 | celt_int32 value = va_arg(ap, celt_int32); |
| 1337 | if (value<0 || value>=st->mode->nbEBands) |
| 1338 | goto bad_arg; |
| 1339 | st->start = value; |
| 1340 | } |
| 1341 | break; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1342 | case CELT_SET_END_BAND_REQUEST: |
| 1343 | { |
| 1344 | celt_int32 value = va_arg(ap, celt_int32); |
| 1345 | if (value<0 || value>=st->mode->nbEBands) |
| 1346 | goto bad_arg; |
| 1347 | st->end = value; |
| 1348 | } |
| 1349 | break; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1350 | case CELT_SET_PREDICTION_REQUEST: |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1351 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1352 | int value = va_arg(ap, celt_int32); |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1353 | if (value<0 || value>2) |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1354 | goto bad_arg; |
| 1355 | if (value==0) |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1356 | { |
| 1357 | st->force_intra = 1; |
Gregory Maxwell | a80958b | 2009-06-29 12:48:57 -0400 | [diff] [blame] | 1358 | } else if (value==1) { |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1359 | st->force_intra = 0; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1360 | } else { |
| 1361 | st->force_intra = 0; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1362 | } |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1363 | } |
| 1364 | break; |
Jean-Marc Valin | 79b34eb | 2010-12-05 17:22:06 -0500 | [diff] [blame] | 1365 | case CELT_SET_VBR_CONSTRAINT_REQUEST: |
| 1366 | { |
| 1367 | celt_int32 value = va_arg(ap, celt_int32); |
| 1368 | st->constrained_vbr = value; |
| 1369 | } |
| 1370 | break; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1371 | case CELT_SET_VBR_RATE_REQUEST: |
| 1372 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1373 | celt_int32 value = va_arg(ap, celt_int32); |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 1374 | int frame_rate; |
| 1375 | int N = st->mode->shortMdctSize; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1376 | if (value<0) |
| 1377 | goto bad_arg; |
| 1378 | if (value>3072000) |
| 1379 | value = 3072000; |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 1380 | frame_rate = ((st->mode->Fs<<3)+(N>>1))/N; |
| 1381 | st->vbr_rate_norm = ((value<<(BITRES+3))+(frame_rate>>1))/frame_rate; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1382 | } |
| 1383 | break; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1384 | case CELT_RESET_STATE: |
| 1385 | { |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1386 | CELT_MEMSET((char*)&st->ENCODER_RESET_START, 0, |
| 1387 | celt_encoder_get_size(st->mode, st->channels)- |
| 1388 | ((char*)&st->ENCODER_RESET_START - (char*)st)); |
Jean-Marc Valin | 30165bb | 2010-12-03 14:35:59 -0500 | [diff] [blame] | 1389 | st->vbr_offset = 0; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1390 | st->delayedIntra = 1; |
John Ridges | 5378bf8 | 2010-02-12 07:08:01 -0500 | [diff] [blame] | 1391 | st->fold_decision = 1; |
Jean-Marc Valin | 628c025 | 2010-04-16 20:57:56 -0400 | [diff] [blame] | 1392 | st->tonal_average = QCONST16(1.f,8); |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1393 | } |
| 1394 | break; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1395 | default: |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1396 | goto bad_request; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1397 | } |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1398 | va_end(ap); |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1399 | return CELT_OK; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1400 | bad_arg: |
| 1401 | va_end(ap); |
| 1402 | return CELT_BAD_ARG; |
| 1403 | bad_request: |
| 1404 | va_end(ap); |
| 1405 | return CELT_UNIMPLEMENTED; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1406 | } |
| 1407 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 1408 | /**********************************************************************/ |
| 1409 | /* */ |
| 1410 | /* DECODER */ |
| 1411 | /* */ |
| 1412 | /**********************************************************************/ |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1413 | #define DECODE_BUFFER_SIZE 2048 |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1414 | |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 1415 | /** Decoder state |
| 1416 | @brief Decoder state |
| 1417 | */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1418 | struct CELTDecoder { |
| 1419 | const CELTMode *mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1420 | int overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1421 | int channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1422 | |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1423 | int start, end; |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1424 | |
| 1425 | /* Everything beyond this point gets cleared on a reset */ |
| 1426 | #define DECODER_RESET_START last_pitch_index |
| 1427 | |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1428 | int last_pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1429 | int loss_count; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1430 | int postfilter_period; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1431 | int postfilter_period_old; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1432 | celt_word16 postfilter_gain; |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1433 | celt_word16 postfilter_gain_old; |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1434 | |
| 1435 | celt_sig preemph_memD[2]; |
| 1436 | |
Jean-Marc Valin | ca8b992 | 2010-08-27 16:23:03 -0400 | [diff] [blame] | 1437 | celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */ |
| 1438 | /* celt_word16 lpc[], Size = channels*LPC_ORDER */ |
| 1439 | /* celt_word16 oldEBands[], Size = channels*mode->nbEBands */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1440 | }; |
| 1441 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1442 | int celt_decoder_get_size(const CELTMode *mode, int channels) |
| 1443 | { |
| 1444 | int size = sizeof(struct CELTDecoder) |
| 1445 | + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig) |
| 1446 | + channels*LPC_ORDER*sizeof(celt_word16) |
| 1447 | + channels*mode->nbEBands*sizeof(celt_word16); |
| 1448 | return size; |
| 1449 | } |
| 1450 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1451 | CELTDecoder *celt_decoder_create(const CELTMode *mode, int channels, int *error) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1452 | { |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 1453 | return celt_decoder_init( |
| 1454 | (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels)), |
| 1455 | mode, channels, error); |
| 1456 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1457 | |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 1458 | CELTDecoder *celt_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels, int *error) |
| 1459 | { |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1460 | if (channels < 0 || channels > 2) |
| 1461 | { |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1462 | if (error) |
| 1463 | *error = CELT_BAD_ARG; |
| 1464 | return NULL; |
| 1465 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1466 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1467 | if (st==NULL) |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1468 | { |
| 1469 | if (error) |
| 1470 | *error = CELT_ALLOC_FAIL; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1471 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1472 | } |
| 1473 | |
Jean-Marc Valin | 6d3829f | 2010-08-27 17:52:38 -0400 | [diff] [blame] | 1474 | CELT_MEMSET((char*)st, 0, celt_decoder_get_size(mode, channels)); |
| 1475 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1476 | st->mode = mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1477 | st->overlap = mode->overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1478 | st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1479 | |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1480 | st->start = 0; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 1481 | st->end = st->mode->effEBands; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1482 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1483 | st->loss_count = 0; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1484 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1485 | if (error) |
| 1486 | *error = CELT_OK; |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1487 | return st; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1488 | } |
| 1489 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 1490 | void celt_decoder_destroy(CELTDecoder *st) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1491 | { |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1492 | celt_free(st); |
| 1493 | } |
| 1494 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1495 | static void celt_decode_lost(CELTDecoder * restrict st, celt_word16 * restrict pcm, int N, int LM) |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1496 | { |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1497 | int c; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1498 | int pitch_index; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1499 | int overlap = st->mode->overlap; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1500 | celt_word16 fade = Q15ONE; |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1501 | int i, len; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1502 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1503 | int offset; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1504 | celt_sig *out_mem[2]; |
| 1505 | celt_sig *decode_mem[2]; |
| 1506 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1507 | celt_word16 *lpc; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1508 | SAVE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1509 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1510 | c=0; do { |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1511 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); |
| 1512 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 1513 | overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1514 | } while (++c<C); |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1515 | lpc = (celt_word16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C); |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1516 | |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1517 | len = N+st->mode->overlap; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1518 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1519 | if (st->loss_count == 0) |
| 1520 | { |
Jean-Marc Valin | 7a7c42a | 2009-11-25 20:38:52 -0500 | [diff] [blame] | 1521 | celt_word16 pitch_buf[MAX_PERIOD>>1]; |
Jean-Marc Valin | 294863b | 2009-11-08 22:29:54 +0900 | [diff] [blame] | 1522 | celt_word32 tmp=0; |
Jean-Marc Valin | e465c14 | 2009-11-26 00:39:36 -0500 | [diff] [blame] | 1523 | celt_word32 mem0[2]={0,0}; |
| 1524 | celt_word16 mem1[2]={0,0}; |
Jean-Marc Valin | 6202c74 | 2010-06-01 00:30:37 -0400 | [diff] [blame] | 1525 | int len2 = len; |
| 1526 | /* FIXME: This is a kludge */ |
| 1527 | if (len2>MAX_PERIOD>>1) |
| 1528 | len2 = MAX_PERIOD>>1; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1529 | pitch_downsample(out_mem, pitch_buf, MAX_PERIOD, MAX_PERIOD, |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1530 | C, mem0, mem1); |
Jean-Marc Valin | 6202c74 | 2010-06-01 00:30:37 -0400 | [diff] [blame] | 1531 | pitch_search(st->mode, pitch_buf+((MAX_PERIOD-len2)>>1), pitch_buf, len2, |
| 1532 | MAX_PERIOD-len2-100, &pitch_index, &tmp, 1<<LM); |
| 1533 | pitch_index = MAX_PERIOD-len2-pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1534 | st->last_pitch_index = pitch_index; |
| 1535 | } else { |
| 1536 | pitch_index = st->last_pitch_index; |
| 1537 | if (st->loss_count < 5) |
| 1538 | fade = QCONST16(.8f,15); |
| 1539 | else |
| 1540 | fade = 0; |
| 1541 | } |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1542 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1543 | c=0; do { |
Jean-Marc Valin | 6202c74 | 2010-06-01 00:30:37 -0400 | [diff] [blame] | 1544 | /* FIXME: This is more memory than necessary */ |
| 1545 | celt_word32 e[2*MAX_PERIOD]; |
| 1546 | celt_word16 exc[2*MAX_PERIOD]; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 1547 | celt_word32 ac[LPC_ORDER+1]; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1548 | celt_word16 decay = 1; |
| 1549 | celt_word32 S1=0; |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 1550 | celt_word16 mem[LPC_ORDER]={0}; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1551 | |
Jean-Marc Valin | d5f9930 | 2009-12-16 22:42:32 -0500 | [diff] [blame] | 1552 | offset = MAX_PERIOD-pitch_index; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1553 | for (i=0;i<MAX_PERIOD;i++) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1554 | exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1555 | |
| 1556 | if (st->loss_count == 0) |
| 1557 | { |
| 1558 | _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap, |
| 1559 | LPC_ORDER, MAX_PERIOD); |
| 1560 | |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 1561 | /* Noise floor -40 dB */ |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1562 | #ifdef FIXED_POINT |
| 1563 | ac[0] += SHR32(ac[0],13); |
| 1564 | #else |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1565 | ac[0] *= 1.0001f; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1566 | #endif |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1567 | /* Lag windowing */ |
| 1568 | for (i=1;i<=LPC_ORDER;i++) |
| 1569 | { |
| 1570 | /*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] | 1571 | #ifdef FIXED_POINT |
| 1572 | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); |
| 1573 | #else |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1574 | ac[i] -= ac[i]*(.008f*i)*(.008f*i); |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1575 | #endif |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1576 | } |
| 1577 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1578 | _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1579 | } |
Jean-Marc Valin | bc4a002 | 2010-11-06 18:11:06 -0400 | [diff] [blame] | 1580 | for (i=0;i<LPC_ORDER;i++) |
Jean-Marc Valin | e53c4bc | 2010-11-06 21:41:40 -0400 | [diff] [blame] | 1581 | mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT); |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1582 | fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem); |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 1583 | /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/ |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1584 | /* Check if the waveform is decaying (and if so how fast) */ |
| 1585 | { |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1586 | celt_word32 E1=1, E2=1; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1587 | int period; |
| 1588 | if (pitch_index <= MAX_PERIOD/2) |
| 1589 | period = pitch_index; |
| 1590 | else |
| 1591 | period = MAX_PERIOD/2; |
| 1592 | for (i=0;i<period;i++) |
| 1593 | { |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1594 | E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8); |
| 1595 | E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1596 | } |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1597 | if (E1 > E2) |
| 1598 | E1 = E2; |
| 1599 | decay = celt_sqrt(frac_div32(SHR(E1,1),E2)); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | /* Copy excitation, taking decay into account */ |
| 1603 | for (i=0;i<len+st->mode->overlap;i++) |
| 1604 | { |
Jean-Marc Valin | 1ad93cf | 2010-11-06 22:02:32 -0400 | [diff] [blame] | 1605 | celt_word16 tmp; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1606 | if (offset+i >= MAX_PERIOD) |
| 1607 | { |
| 1608 | offset -= pitch_index; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1609 | decay = MULT16_16_Q15(decay, decay); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1610 | } |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1611 | e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT); |
Jean-Marc Valin | 1ad93cf | 2010-11-06 22:02:32 -0400 | [diff] [blame] | 1612 | tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT); |
| 1613 | S1 += SHR32(MULT16_16(tmp,tmp),8); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1614 | } |
Jean-Marc Valin | bc2c454 | 2010-10-28 14:27:05 -0400 | [diff] [blame] | 1615 | for (i=0;i<LPC_ORDER;i++) |
Jean-Marc Valin | e53c4bc | 2010-11-06 21:41:40 -0400 | [diff] [blame] | 1616 | mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT); |
Jean-Marc Valin | bc2c454 | 2010-10-28 14:27:05 -0400 | [diff] [blame] | 1617 | for (i=0;i<len+st->mode->overlap;i++) |
| 1618 | e[i] = MULT16_32_Q15(fade, e[i]); |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1619 | iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1620 | |
| 1621 | { |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1622 | celt_word32 S2=0; |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1623 | for (i=0;i<len+overlap;i++) |
Jean-Marc Valin | 1ad93cf | 2010-11-06 22:02:32 -0400 | [diff] [blame] | 1624 | { |
| 1625 | celt_word16 tmp = ROUND16(e[i],SIG_SHIFT); |
| 1626 | S2 += SHR32(MULT16_16(tmp,tmp),8); |
| 1627 | } |
Jean-Marc Valin | feca095 | 2010-06-17 20:25:51 -0400 | [diff] [blame] | 1628 | /* This checks for an "explosion" in the synthesis */ |
| 1629 | #ifdef FIXED_POINT |
| 1630 | if (!(S1 > SHR32(S2,2))) |
| 1631 | #else |
| 1632 | /* Float test is written this way to catch NaNs at the same time */ |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 1633 | if (!(S1 > 0.2f*S2)) |
Jean-Marc Valin | feca095 | 2010-06-17 20:25:51 -0400 | [diff] [blame] | 1634 | #endif |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 1635 | { |
| 1636 | for (i=0;i<len+overlap;i++) |
| 1637 | e[i] = 0; |
| 1638 | } else if (S1 < S2) |
| 1639 | { |
Jean-Marc Valin | 1299640 | 2010-08-04 09:13:24 -0400 | [diff] [blame] | 1640 | celt_word16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1)); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1641 | for (i=0;i<len+overlap;i++) |
Jean-Marc Valin | 1ad93cf | 2010-11-06 22:02:32 -0400 | [diff] [blame] | 1642 | e[i] = MULT16_32_Q15(ratio, e[i]); |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 1643 | } |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1644 | } |
| 1645 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1646 | #ifdef ENABLE_POSTFILTER |
| 1647 | /* Apply post-filter to the MDCT overlap of the previous frame */ |
| 1648 | comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap, C, |
| 1649 | st->postfilter_gain, st->postfilter_gain, NULL, 0); |
| 1650 | #endif /* ENABLE_POSTFILTER */ |
| 1651 | |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1652 | for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1653 | out_mem[c][i] = out_mem[c][N+i]; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1654 | |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1655 | /* Apply TDAC to the concealed audio so that it blends with the |
| 1656 | previous and next frames */ |
| 1657 | for (i=0;i<overlap/2;i++) |
| 1658 | { |
Jean-Marc Valin | d7231dd | 2010-11-06 20:30:17 -0400 | [diff] [blame] | 1659 | celt_word32 tmp; |
| 1660 | tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) + |
| 1661 | MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]); |
| 1662 | out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp); |
| 1663 | out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1664 | } |
Jean-Marc Valin | bc4a002 | 2010-11-06 18:11:06 -0400 | [diff] [blame] | 1665 | for (i=0;i<N;i++) |
| 1666 | out_mem[c][MAX_PERIOD-N+i] = e[i]; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1667 | |
| 1668 | #ifdef ENABLE_POSTFILTER |
| 1669 | /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */ |
| 1670 | comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap, C, |
| 1671 | -st->postfilter_gain, -st->postfilter_gain, NULL, 0); |
| 1672 | #endif /* ENABLE_POSTFILTER */ |
| 1673 | for (i=0;i<overlap;i++) |
| 1674 | out_mem[c][MAX_PERIOD+i] = e[i]; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1675 | } while (++c<C); |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 1676 | |
Jean-Marc Valin | bc2c454 | 2010-10-28 14:27:05 -0400 | [diff] [blame] | 1677 | { |
| 1678 | celt_word32 *out_syn[2]; |
| 1679 | out_syn[0] = out_mem[0]+MAX_PERIOD-N; |
| 1680 | if (C==2) |
| 1681 | out_syn[1] = out_mem[1]+MAX_PERIOD-N; |
| 1682 | deemphasis(out_syn, pcm, N, C, st->mode->preemph, st->preemph_memD); |
| 1683 | } |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1684 | |
| 1685 | st->loss_count++; |
| 1686 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1687 | RESTORE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1688 | } |
| 1689 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1690 | #ifdef FIXED_POINT |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1691 | int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size, ec_dec *dec) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1692 | { |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1693 | #else |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1694 | int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *data, int len, celt_sig * restrict pcm, int frame_size, ec_dec *dec) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1695 | { |
| 1696 | #endif |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 1697 | int c, i, N; |
Jean-Marc Valin | 8cbea17 | 2010-08-05 15:22:57 -0400 | [diff] [blame] | 1698 | int has_fold; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 1699 | int bits; |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1700 | ec_dec _dec; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1701 | ec_byte_buffer buf; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1702 | VARDECL(celt_sig, freq); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1703 | VARDECL(celt_norm, X); |
| 1704 | VARDECL(celt_ener, bandE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1705 | VARDECL(int, fine_quant); |
| 1706 | VARDECL(int, pulses); |
| 1707 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1708 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 1709 | VARDECL(int, tf_res); |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1710 | celt_sig *out_mem[2]; |
| 1711 | celt_sig *decode_mem[2]; |
| 1712 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 1713 | celt_sig *out_syn[2]; |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1714 | celt_word16 *lpc; |
| 1715 | celt_word16 *oldBandE; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 1716 | |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 1717 | int shortBlocks; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1718 | int isTransient; |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 1719 | int intra_ener; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1720 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1721 | int LM, M; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 1722 | int nbFilledBytes, nbAvailableBytes; |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1723 | int effEnd; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 1724 | int codedBands; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1725 | int alloc_trim; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1726 | int postfilter_pitch; |
| 1727 | celt_word16 postfilter_gain; |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1728 | int intensity=0; |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 1729 | int dual_stereo=0; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1730 | SAVE_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1731 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1732 | if (pcm==NULL) |
| 1733 | return CELT_BAD_ARG; |
| 1734 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1735 | for (LM=0;LM<4;LM++) |
| 1736 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1737 | break; |
| 1738 | if (LM>=MAX_CONFIG_SIZES) |
| 1739 | return CELT_BAD_ARG; |
| 1740 | M=1<<LM; |
| 1741 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1742 | c=0; do { |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1743 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); |
| 1744 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 1745 | overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1746 | } while (++c<C); |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1747 | lpc = (celt_word16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C); |
| 1748 | oldBandE = lpc+C*LPC_ORDER; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1749 | |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1750 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 0141723 | 2008-03-03 13:59:55 +1100 | [diff] [blame] | 1751 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1752 | effEnd = st->end; |
| 1753 | if (effEnd > st->mode->effEBands) |
| 1754 | effEnd = st->mode->effEBands; |
| 1755 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1756 | ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 1757 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
| 1758 | ALLOC(bandE, st->mode->nbEBands*C, celt_ener); |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1759 | c=0; do |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1760 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 1761 | X[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1762 | while (++c<C); |
| 1763 | c=0; do |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1764 | for (i=M*st->mode->eBands[effEnd];i<N;i++) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1765 | X[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1766 | while (++c<C); |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1767 | |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1768 | if (data == NULL) |
| 1769 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1770 | celt_decode_lost(st, pcm, N, LM); |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1771 | RESTORE_STACK; |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1772 | return CELT_OK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1773 | } |
Gregory Maxwell | 520eeae | 2009-02-09 01:33:21 -0500 | [diff] [blame] | 1774 | if (len<0) { |
| 1775 | RESTORE_STACK; |
| 1776 | return CELT_BAD_ARG; |
| 1777 | } |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1778 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1779 | if (dec == NULL) |
| 1780 | { |
| 1781 | ec_byte_readinit(&buf,(unsigned char*)data,len); |
| 1782 | ec_dec_init(&_dec,&buf); |
| 1783 | dec = &_dec; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 1784 | nbFilledBytes = 0; |
| 1785 | } else { |
Jean-Marc Valin | bdcaaf7 | 2010-07-05 13:52:41 -0400 | [diff] [blame] | 1786 | nbFilledBytes = (ec_dec_tell(dec, 0)+4)>>3; |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1787 | } |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 1788 | nbAvailableBytes = len-nbFilledBytes; |
| 1789 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1790 | if (ec_dec_bit_prob(dec, 32768)) |
| 1791 | { |
| 1792 | #ifdef ENABLE_POSTFILTER |
| 1793 | int qg, octave; |
| 1794 | octave = ec_dec_uint(dec, 6); |
| 1795 | postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave); |
| 1796 | qg = ec_dec_bits(dec, 2); |
| 1797 | postfilter_gain = QCONST16(.125f,15)*(qg+2); |
| 1798 | #else /* ENABLE_POSTFILTER */ |
| 1799 | RESTORE_STACK; |
| 1800 | return CELT_CORRUPTED_DATA; |
| 1801 | #endif /* ENABLE_POSTFILTER */ |
| 1802 | |
| 1803 | } else { |
| 1804 | postfilter_gain = 0; |
| 1805 | postfilter_pitch = 0; |
| 1806 | } |
| 1807 | |
Jean-Marc Valin | 017001a | 2010-08-05 15:42:50 -0400 | [diff] [blame] | 1808 | /* Decode the global flags (first symbols in the stream) */ |
| 1809 | intra_ener = ec_dec_bit_prob(dec, 8192); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1810 | /* Get band energies */ |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1811 | unquant_coarse_energy(st->mode, st->start, st->end, bandE, oldBandE, |
Timothy B. Terriberry | ef2e650 | 2010-11-09 01:43:18 -0800 | [diff] [blame] | 1812 | intra_ener, dec, C, LM); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1813 | |
Jean-Marc Valin | f9fdbff | 2010-09-05 21:02:38 -0400 | [diff] [blame] | 1814 | if (LM > 0) |
| 1815 | isTransient = ec_dec_bit_prob(dec, 8192); |
| 1816 | else |
| 1817 | isTransient = 0; |
Jean-Marc Valin | 017001a | 2010-08-05 15:42:50 -0400 | [diff] [blame] | 1818 | |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1819 | if (isTransient) |
| 1820 | shortBlocks = M; |
| 1821 | else |
| 1822 | shortBlocks = 0; |
| 1823 | |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 1824 | ALLOC(tf_res, st->mode->nbEBands, int); |
Jean-Marc Valin | e5e9aa7 | 2010-12-02 16:09:51 -0500 | [diff] [blame] | 1825 | tf_decode(st->start, st->end, C, isTransient, tf_res, LM, dec); |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 1826 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1827 | has_fold = ec_dec_bit_prob(dec, 8192)<<1; |
| 1828 | has_fold |= ec_dec_bit_prob(dec, (has_fold>>1) ? 32768 : 49152); |
| 1829 | |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1830 | ALLOC(pulses, st->mode->nbEBands, int); |
| 1831 | ALLOC(offsets, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1832 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1833 | |
| 1834 | for (i=0;i<st->mode->nbEBands;i++) |
| 1835 | offsets[i] = 0; |
Jean-Marc Valin | d74c851 | 2010-09-29 17:39:54 -0400 | [diff] [blame] | 1836 | for (i=0;i<st->mode->nbEBands;i++) |
| 1837 | { |
| 1838 | if (ec_dec_bit_prob(dec, 1024)) |
| 1839 | { |
| 1840 | while (ec_dec_bit_prob(dec, 32768)) |
| 1841 | offsets[i]++; |
| 1842 | offsets[i]++; |
| 1843 | offsets[i] *= (6<<BITRES); |
| 1844 | } |
| 1845 | } |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1846 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1847 | ALLOC(fine_quant, st->mode->nbEBands, int); |
Jean-Marc Valin | 6bf0462 | 2010-09-30 10:16:22 -0400 | [diff] [blame] | 1848 | { |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1849 | int fl; |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 1850 | alloc_trim = 0; |
Jean-Marc Valin | 6bf0462 | 2010-09-30 10:16:22 -0400 | [diff] [blame] | 1851 | fl = ec_decode_bin(dec, 7); |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 1852 | while (trim_cdf[alloc_trim+1] <= fl) |
| 1853 | alloc_trim++; |
| 1854 | ec_dec_update(dec, trim_cdf[alloc_trim], trim_cdf[alloc_trim+1], 128); |
Jean-Marc Valin | 6bf0462 | 2010-09-30 10:16:22 -0400 | [diff] [blame] | 1855 | } |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1856 | |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1857 | if (C==2) |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 1858 | { |
| 1859 | dual_stereo = ec_dec_bit_prob(dec, 32768); |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1860 | intensity = ec_dec_uint(dec, 1+st->end-st->start); |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 1861 | } |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1862 | |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 1863 | bits = (len*8<<BITRES) - ec_dec_tell(dec, BITRES) - 1; |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 1864 | codedBands = compute_allocation(st->mode, st->start, st->end, offsets, |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 1865 | alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, dec, 0, 0); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1866 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1867 | unquant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, fine_quant, dec, C); |
Jean-Marc Valin | 827f931 | 2008-05-06 23:21:55 +1000 | [diff] [blame] | 1868 | |
Jean-Marc Valin | 8cbea17 | 2010-08-05 15:22:57 -0400 | [diff] [blame] | 1869 | /* Decode fixed codebook */ |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1870 | quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, |
Jean-Marc Valin | e65978f | 2010-12-02 13:46:48 -0500 | [diff] [blame] | 1871 | NULL, pulses, shortBlocks, has_fold, dual_stereo, intensity, tf_res, 1, |
Jean-Marc Valin | 4f177e8 | 2010-11-26 10:32:03 -0500 | [diff] [blame] | 1872 | len*8, dec, LM, codedBands); |
Jean-Marc Valin | 746b2a8 | 2010-05-14 22:12:33 -0400 | [diff] [blame] | 1873 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1874 | unquant_energy_finalise(st->mode, st->start, st->end, bandE, oldBandE, |
| 1875 | fine_quant, fine_priority, len*8-ec_dec_tell(dec, 0), dec, C); |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 1876 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1877 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 1878 | |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 1879 | /* Synthesis */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1880 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 1881 | |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1882 | CELT_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N); |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame] | 1883 | if (C==2) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1884 | CELT_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] | 1885 | |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1886 | c=0; do |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1887 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 1888 | freq[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1889 | while (++c<C); |
| 1890 | c=0; do |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1891 | for (i=M*st->mode->eBands[effEnd];i<N;i++) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1892 | freq[c*N+i] = 0; |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1893 | while (++c<C); |
Jean-Marc Valin | 3a0bc3d | 2010-02-21 15:10:22 -0500 | [diff] [blame] | 1894 | |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1895 | out_syn[0] = out_mem[0]+MAX_PERIOD-N; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 1896 | if (C==2) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1897 | out_syn[1] = out_mem[1]+MAX_PERIOD-N; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 1898 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1899 | /* Compute inverse MDCTs */ |
Jean-Marc Valin | 7a08ddd | 2010-10-18 14:55:42 -0400 | [diff] [blame] | 1900 | compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, C, LM); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 1901 | |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1902 | #ifdef ENABLE_POSTFILTER |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1903 | c=0; do { |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1904 | st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD); |
| 1905 | st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD); |
| 1906 | if (LM!=0) |
| 1907 | { |
| 1908 | comb_filter(out_syn[c], out_syn[c], st->postfilter_period, st->postfilter_period, st->overlap, C, |
| 1909 | st->postfilter_gain, st->postfilter_gain, NULL, 0); |
| 1910 | comb_filter(out_syn[c]+st->overlap, out_syn[c]+st->overlap, st->postfilter_period, postfilter_pitch, N-st->overlap, C, |
| 1911 | st->postfilter_gain, postfilter_gain, st->mode->window, st->mode->overlap); |
| 1912 | } else { |
| 1913 | comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, N-st->overlap, C, |
| 1914 | st->postfilter_gain_old, st->postfilter_gain, st->mode->window, st->mode->overlap); |
| 1915 | } |
Gregory Maxwell | 9743bf3 | 2010-11-04 20:45:09 -0400 | [diff] [blame] | 1916 | } while (++c<C); |
Jean-Marc Valin | 70d30ff | 2010-12-13 13:50:29 -0500 | [diff] [blame] | 1917 | st->postfilter_period_old = st->postfilter_period; |
| 1918 | st->postfilter_gain_old = st->postfilter_gain; |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 1919 | st->postfilter_period = postfilter_pitch; |
| 1920 | st->postfilter_gain = postfilter_gain; |
| 1921 | #endif /* ENABLE_POSTFILTER */ |
| 1922 | |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 1923 | deemphasis(out_syn, pcm, N, C, st->mode->preemph, st->preemph_memD); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1924 | st->loss_count = 0; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1925 | RESTORE_STACK; |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1926 | if (ec_dec_get_error(dec)) |
| 1927 | return CELT_CORRUPTED_DATA; |
| 1928 | else |
| 1929 | return CELT_OK; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1930 | } |
| 1931 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1932 | #ifdef FIXED_POINT |
| 1933 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1934 | int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size, ec_dec *dec) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1935 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1936 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1937 | VARDECL(celt_int16, out); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1938 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1939 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1940 | if (pcm==NULL) |
| 1941 | return CELT_BAD_ARG; |
| 1942 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1943 | for (LM=0;LM<4;LM++) |
| 1944 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1945 | break; |
| 1946 | if (LM>=MAX_CONFIG_SIZES) |
| 1947 | return CELT_BAD_ARG; |
| 1948 | M=1<<LM; |
| 1949 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1950 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1951 | N = M*st->mode->shortMdctSize; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1952 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1953 | ALLOC(out, C*N, celt_int16); |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1954 | ret=celt_decode_with_ec(st, data, len, out, frame_size, dec); |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1955 | if (ret==0) |
| 1956 | for (j=0;j<C*N;j++) |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1957 | pcm[j]=out[j]*(1.f/32768.f); |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1958 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1959 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1960 | return ret; |
| 1961 | } |
| 1962 | #endif /*DISABLE_FLOAT_API*/ |
| 1963 | #else |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1964 | int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size, ec_dec *dec) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1965 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1966 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1967 | VARDECL(celt_sig, out); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1968 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1969 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1970 | if (pcm==NULL) |
| 1971 | return CELT_BAD_ARG; |
| 1972 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1973 | for (LM=0;LM<4;LM++) |
| 1974 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1975 | break; |
| 1976 | if (LM>=MAX_CONFIG_SIZES) |
| 1977 | return CELT_BAD_ARG; |
| 1978 | M=1<<LM; |
| 1979 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1980 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1981 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1982 | ALLOC(out, C*N, celt_sig); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1983 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1984 | ret=celt_decode_with_ec_float(st, data, len, out, frame_size, dec); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1985 | |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1986 | if (ret==0) |
| 1987 | for (j=0;j<C*N;j++) |
| 1988 | pcm[j] = FLOAT2INT16 (out[j]); |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1989 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1990 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1991 | return ret; |
| 1992 | } |
| 1993 | #endif |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1994 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1995 | int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size) |
| 1996 | { |
| 1997 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); |
| 1998 | } |
| 1999 | |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 2000 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 2001 | int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size) |
| 2002 | { |
| 2003 | return celt_decode_with_ec_float(st, data, len, pcm, frame_size, NULL); |
| 2004 | } |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 2005 | #endif /* DISABLE_FLOAT_API */ |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 2006 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2007 | int celt_decoder_ctl(CELTDecoder * restrict st, int request, ...) |
| 2008 | { |
| 2009 | va_list ap; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 2010 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2011 | va_start(ap, request); |
| 2012 | switch (request) |
| 2013 | { |
Gregory Maxwell | f3b44ef | 2009-06-03 13:37:45 -0400 | [diff] [blame] | 2014 | case CELT_GET_MODE_REQUEST: |
| 2015 | { |
| 2016 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 2017 | if (value==0) |
| 2018 | goto bad_arg; |
| 2019 | *value=st->mode; |
| 2020 | } |
| 2021 | break; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 2022 | case CELT_SET_START_BAND_REQUEST: |
| 2023 | { |
| 2024 | celt_int32 value = va_arg(ap, celt_int32); |
| 2025 | if (value<0 || value>=st->mode->nbEBands) |
| 2026 | goto bad_arg; |
| 2027 | st->start = value; |
| 2028 | } |
| 2029 | break; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 2030 | case CELT_SET_END_BAND_REQUEST: |
| 2031 | { |
| 2032 | celt_int32 value = va_arg(ap, celt_int32); |
| 2033 | if (value<0 || value>=st->mode->nbEBands) |
| 2034 | goto bad_arg; |
| 2035 | st->end = value; |
| 2036 | } |
| 2037 | break; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2038 | case CELT_RESET_STATE: |
| 2039 | { |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 2040 | CELT_MEMSET((char*)&st->DECODER_RESET_START, 0, |
| 2041 | celt_decoder_get_size(st->mode, st->channels)- |
| 2042 | ((char*)&st->DECODER_RESET_START - (char*)st)); |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2043 | } |
| 2044 | break; |
| 2045 | default: |
| 2046 | goto bad_request; |
| 2047 | } |
| 2048 | va_end(ap); |
| 2049 | return CELT_OK; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2050 | bad_arg: |
| 2051 | va_end(ap); |
| 2052 | return CELT_BAD_ARG; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 2053 | bad_request: |
| 2054 | va_end(ap); |
| 2055 | return CELT_UNIMPLEMENTED; |
| 2056 | } |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 2057 | |
| 2058 | const char *celt_strerror(int error) |
| 2059 | { |
| 2060 | static const char *error_strings[8] = { |
| 2061 | "success", |
| 2062 | "invalid argument", |
| 2063 | "invalid mode", |
| 2064 | "internal error", |
| 2065 | "corrupted stream", |
| 2066 | "request not implemented", |
| 2067 | "invalid state", |
| 2068 | "memory allocation failed" |
| 2069 | }; |
| 2070 | if (error > 0 || error < -7) |
| 2071 | return "unknown error"; |
| 2072 | else |
| 2073 | return error_strings[-error]; |
| 2074 | } |
| 2075 | |