Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2007-2008 CSIRO |
| 2 | Copyright (c) 2007-2009 Xiph.Org Foundation |
| 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 | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 56 | #ifdef FIXED_POINT |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 57 | static const celt_word16 transientWindow[16] = { |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 58 | 279, 1106, 2454, 4276, 6510, 9081, 11900, 14872, |
| 59 | 17896, 20868, 23687, 26258, 28492, 30314, 31662, 32489}; |
| 60 | #else |
| 61 | static const float transientWindow[16] = { |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 62 | 0.0085135f, 0.0337639f, 0.0748914f, 0.1304955f, |
| 63 | 0.1986827f, 0.2771308f, 0.3631685f, 0.4538658f, |
| 64 | 0.5461342f, 0.6368315f, 0.7228692f, 0.8013173f, |
| 65 | 0.8695045f, 0.9251086f, 0.9662361f, 0.9914865f}; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 66 | #endif |
| 67 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 68 | #define ENCODERVALID 0x4c434554 |
| 69 | #define ENCODERPARTIAL 0x5445434c |
| 70 | #define ENCODERFREED 0x4c004500 |
Jean-Marc Valin | c994394 | 2008-08-30 00:55:07 -0400 | [diff] [blame] | 71 | |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 72 | /** Encoder state |
| 73 | @brief Encoder state |
| 74 | */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 75 | struct CELTEncoder { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 76 | celt_uint32 marker; |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 77 | const CELTMode *mode; /**< Mode used by the encoder */ |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 78 | int overlap; |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 79 | int channels; |
| 80 | |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 81 | int force_intra; |
Jean-Marc Valin | 74f4e9f | 2009-05-02 09:57:50 -0400 | [diff] [blame] | 82 | int delayedIntra; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 83 | celt_word16 tonal_average; |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 84 | int fold_decision; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 85 | celt_word16 gain_prod; |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 86 | celt_word32 frame_max; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 87 | int start, end; |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 88 | |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 89 | /* VBR-related parameters */ |
| 90 | celt_int32 vbr_reservoir; |
| 91 | celt_int32 vbr_drift; |
| 92 | celt_int32 vbr_offset; |
| 93 | celt_int32 vbr_count; |
| 94 | |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 95 | celt_int32 vbr_rate_norm; /* Target number of 16th bits per frame */ |
Jean-Marc Valin | ac1da4f | 2010-07-24 11:48:10 -0400 | [diff] [blame] | 96 | celt_word32 preemph_memE[2]; |
| 97 | celt_word32 preemph_memD[2]; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 98 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 99 | celt_sig *in_mem; |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 100 | celt_sig *out_mem[2]; |
Jean-Marc Valin | 991c0f0 | 2007-11-30 16:07:46 +1100 | [diff] [blame] | 101 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 102 | celt_word16 *oldBandE; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 103 | }; |
| 104 | |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 105 | static int check_encoder(const CELTEncoder *st) |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 106 | { |
| 107 | if (st==NULL) |
| 108 | { |
| 109 | celt_warning("NULL passed as an encoder structure"); |
| 110 | return CELT_INVALID_STATE; |
| 111 | } |
| 112 | if (st->marker == ENCODERVALID) |
| 113 | return CELT_OK; |
| 114 | if (st->marker == ENCODERFREED) |
| 115 | celt_warning("Referencing an encoder that has already been freed"); |
| 116 | else |
| 117 | celt_warning("This is not a valid CELT encoder structure"); |
| 118 | return CELT_INVALID_STATE; |
| 119 | } |
| 120 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 121 | CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error) |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 122 | { |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 123 | int C; |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 124 | CELTEncoder *st; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 125 | |
| 126 | if (check_mode(mode) != CELT_OK) |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 127 | { |
| 128 | if (error) |
| 129 | *error = CELT_INVALID_MODE; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 130 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 131 | } |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 132 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 133 | if (channels < 0 || channels > 2) |
| 134 | { |
| 135 | celt_warning("Only mono and stereo supported"); |
| 136 | if (error) |
| 137 | *error = CELT_BAD_ARG; |
| 138 | return NULL; |
| 139 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 140 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 141 | C = channels; |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 142 | st = celt_alloc(sizeof(CELTEncoder)); |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 143 | |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 144 | if (st==NULL) |
| 145 | { |
| 146 | if (error) |
| 147 | *error = CELT_ALLOC_FAIL; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 148 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 149 | } |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 150 | st->marker = ENCODERPARTIAL; |
Jean-Marc Valin | 73e51b3 | 2007-12-05 17:48:24 +1100 | [diff] [blame] | 151 | st->mode = mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 152 | st->overlap = mode->overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 153 | st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 154 | |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 155 | st->start = 0; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 156 | st->end = st->mode->effEBands; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 157 | |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 158 | st->vbr_rate_norm = 0; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 159 | st->force_intra = 0; |
Gregory Maxwell | 8842fde | 2009-05-04 15:58:40 -0400 | [diff] [blame] | 160 | st->delayedIntra = 1; |
Jean-Marc Valin | 628c025 | 2010-04-16 20:57:56 -0400 | [diff] [blame] | 161 | st->tonal_average = QCONST16(1.f,8); |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 162 | st->fold_decision = 1; |
Jean-Marc Valin | a76a0b2 | 2008-01-17 22:43:05 +1100 | [diff] [blame] | 163 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 164 | st->in_mem = celt_alloc(st->overlap*C*sizeof(celt_sig)); |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 165 | st->out_mem[0] = celt_alloc((MAX_PERIOD+st->overlap)*C*sizeof(celt_sig)); |
| 166 | if (C==2) |
| 167 | st->out_mem[1] = st->out_mem[0] + MAX_PERIOD+st->overlap; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 168 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 169 | st->oldBandE = (celt_word16*)celt_alloc(C*mode->nbEBands*sizeof(celt_word16)); |
Jean-Marc Valin | fa24873 | 2007-12-06 07:51:27 +1100 | [diff] [blame] | 170 | |
Jean-Marc Valin | ac1da4f | 2010-07-24 11:48:10 -0400 | [diff] [blame] | 171 | if ((st->in_mem!=NULL) && (st->out_mem!=NULL) && (st->oldBandE!=NULL)) |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 172 | { |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 173 | if (error) |
| 174 | *error = CELT_OK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 175 | st->marker = ENCODERVALID; |
| 176 | return st; |
| 177 | } |
| 178 | /* If the setup fails for some reason deallocate it. */ |
| 179 | celt_encoder_destroy(st); |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 180 | if (error) |
| 181 | *error = CELT_ALLOC_FAIL; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 182 | return NULL; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 183 | } |
| 184 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 185 | void celt_encoder_destroy(CELTEncoder *st) |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 186 | { |
| 187 | if (st == NULL) |
| 188 | { |
| 189 | celt_warning("NULL passed to celt_encoder_destroy"); |
| 190 | return; |
| 191 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 192 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 193 | if (st->marker == ENCODERFREED) |
| 194 | { |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 195 | celt_warning("Freeing an encoder which has already been freed"); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 196 | return; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (st->marker != ENCODERVALID && st->marker != ENCODERPARTIAL) |
| 200 | { |
| 201 | celt_warning("This is not a valid CELT encoder structure"); |
| 202 | return; |
| 203 | } |
| 204 | /*Check_mode is non-fatal here because we can still free |
| 205 | the encoder memory even if the mode is bad, although calling |
| 206 | the free functions in this order is a violation of the API.*/ |
| 207 | check_mode(st->mode); |
| 208 | |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 209 | celt_free(st->in_mem); |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 210 | celt_free(st->out_mem[0]); |
Jean-Marc Valin | c245a22 | 2007-12-06 19:14:20 +1100 | [diff] [blame] | 211 | celt_free(st->oldBandE); |
Jean-Marc Valin | 7351e28 | 2008-02-08 15:09:45 +1100 | [diff] [blame] | 212 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 213 | st->marker = ENCODERFREED; |
Jean-Marc Valin | c6ca499 | 2008-06-01 23:19:16 +1000 | [diff] [blame] | 214 | |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 215 | celt_free(st); |
| 216 | } |
| 217 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 218 | static inline celt_int16 FLOAT2INT16(float x) |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 219 | { |
Gregory Maxwell | 0ac2b2f | 2009-05-21 23:08:46 -0400 | [diff] [blame] | 220 | x = x*CELT_SIG_SCALE; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 221 | x = MAX32(x, -32768); |
| 222 | x = MIN32(x, 32767); |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 223 | return (celt_int16)float2int(x); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 226 | static inline celt_word16 SIG2WORD16(celt_sig x) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 227 | { |
| 228 | #ifdef FIXED_POINT |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 229 | x = PSHR32(x, SIG_SHIFT); |
Jean-Marc Valin | abdfc38 | 2008-04-18 15:57:18 +1000 | [diff] [blame] | 230 | x = MAX32(x, -32768); |
| 231 | x = MIN32(x, 32767); |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 232 | return EXTRACT16(x); |
| 233 | #else |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 234 | return (celt_word16)x; |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 235 | #endif |
| 236 | } |
| 237 | |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 238 | static int transient_analysis(const celt_word32 * restrict in, int len, int C, |
| 239 | int *transient_time, int *transient_shift, |
Jean-Marc Valin | bdb5871 | 2010-05-28 18:58:42 -0400 | [diff] [blame] | 240 | celt_word32 *frame_max, int overlap) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 241 | { |
Jean-Marc Valin | bdb5871 | 2010-05-28 18:58:42 -0400 | [diff] [blame] | 242 | int i, n; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 243 | celt_word32 ratio; |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 244 | celt_word32 threshold; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 245 | VARDECL(celt_word32, begin); |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 246 | SAVE_STACK; |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 247 | ALLOC(begin, len+1, celt_word32); |
| 248 | begin[0] = 0; |
| 249 | if (C==1) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 250 | { |
| 251 | for (i=0;i<len;i++) |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 252 | begin[i+1] = MAX32(begin[i], ABS32(in[i])); |
| 253 | } else { |
| 254 | for (i=0;i<len;i++) |
| 255 | begin[i+1] = MAX32(begin[i], MAX32(ABS32(in[C*i]), |
| 256 | ABS32(in[C*i+1]))); |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 257 | } |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 258 | n = -1; |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 259 | |
Jean-Marc Valin | e34c85a | 2010-07-15 17:57:50 -0400 | [diff] [blame] | 260 | threshold = MULT16_32_Q15(QCONST16(.4f,15),begin[len]); |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 261 | /* If the following condition isn't met, there's just no way |
| 262 | we'll have a transient*/ |
| 263 | if (*frame_max < threshold) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 264 | { |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 265 | /* It's likely we have a transient, now find it */ |
| 266 | for (i=8;i<len-8;i++) |
| 267 | { |
| 268 | if (begin[i+1] < threshold) |
| 269 | n=i; |
| 270 | } |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 271 | } |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 272 | if (n<32) |
| 273 | { |
| 274 | n = -1; |
| 275 | ratio = 0; |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 276 | } else { |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 277 | ratio = DIV32(begin[len],1+MAX32(*frame_max, begin[n-16])); |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 278 | } |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 279 | |
Jean-Marc Valin | 816f893 | 2010-05-31 21:35:40 -0400 | [diff] [blame] | 280 | if (ratio > 45) |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 281 | *transient_shift = 3; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 282 | else |
| 283 | *transient_shift = 0; |
| 284 | |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 285 | *transient_time = n; |
Jean-Marc Valin | bdb5871 | 2010-05-28 18:58:42 -0400 | [diff] [blame] | 286 | *frame_max = begin[len-overlap]; |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 287 | |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 288 | RESTORE_STACK; |
Jean-Marc Valin | e34c85a | 2010-07-15 17:57:50 -0400 | [diff] [blame] | 289 | return ratio > 0; |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 290 | } |
| 291 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 292 | /** Apply window and compute the MDCT for all sub-frames and |
| 293 | all channels in a frame */ |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 294 | 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] | 295 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 296 | const int C = CHANNELS(_C); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 297 | if (C==1 && !shortBlocks) |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 298 | { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 299 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 300 | 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] | 301 | } else { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 302 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | ce4dd36 | 2010-05-07 07:45:18 -0400 | [diff] [blame] | 303 | int N = mode->shortMdctSize<<LM; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 304 | int B = 1; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 305 | int b, c; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 306 | VARDECL(celt_word32, x); |
| 307 | VARDECL(celt_word32, tmp); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 308 | SAVE_STACK; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 309 | if (shortBlocks) |
| 310 | { |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 311 | /*lookup = &mode->mdct[0];*/ |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 312 | N = mode->shortMdctSize; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 313 | B = shortBlocks; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 314 | } |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 315 | ALLOC(x, N+overlap, celt_word32); |
| 316 | ALLOC(tmp, N, celt_word32); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 317 | for (c=0;c<C;c++) |
| 318 | { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 319 | for (b=0;b<B;b++) |
| 320 | { |
| 321 | int j; |
| 322 | for (j=0;j<N+overlap;j++) |
| 323 | x[j] = in[C*(b*N+j)+c]; |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 324 | clt_mdct_forward(&mode->mdct, x, tmp, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 325 | /* Interleaving the sub-frames */ |
| 326 | for (j=0;j<N;j++) |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 327 | out[(j*B+b)+c*N*B] = tmp[j]; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | RESTORE_STACK; |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 331 | } |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 332 | } |
| 333 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 334 | /** Compute the IMDCT and apply window for all sub-frames and |
| 335 | all channels in a frame */ |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 336 | static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X, int transient_time, int transient_shift, celt_sig * restrict out_mem[], int _C, int LM) |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 337 | { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 338 | int c, N4; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 339 | const int C = CHANNELS(_C); |
Jean-Marc Valin | ce4dd36 | 2010-05-07 07:45:18 -0400 | [diff] [blame] | 340 | const int N = mode->shortMdctSize<<LM; |
Jean-Marc Valin | b18ec0b | 2008-04-11 04:07:52 +1000 | [diff] [blame] | 341 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | a536f77 | 2008-03-22 09:01:50 +1100 | [diff] [blame] | 342 | N4 = (N-overlap)>>1; |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 343 | for (c=0;c<C;c++) |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 344 | { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 345 | int j; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 346 | if (transient_shift==0 && C==1 && !shortBlocks) { |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 347 | clt_mdct_backward(&mode->mdct, X, out_mem[0]+(MAX_PERIOD-N-N4), mode->window, overlap, mode->maxLM-LM); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 348 | } else { |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 349 | VARDECL(celt_word32, x); |
| 350 | VARDECL(celt_word32, tmp); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 351 | int b; |
| 352 | int N2 = N; |
| 353 | int B = 1; |
| 354 | int n4offset=0; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 355 | SAVE_STACK; |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 356 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 357 | ALLOC(x, 2*N, celt_word32); |
| 358 | ALLOC(tmp, N, celt_word32); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 359 | |
| 360 | if (shortBlocks) |
| 361 | { |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 362 | /*lookup = &mode->mdct[0];*/ |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 363 | N2 = mode->shortMdctSize; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 364 | B = shortBlocks; |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 365 | n4offset = N4; |
| 366 | } |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 367 | /* Prevents problems from the imdct doing the overlap-add */ |
Gregory Maxwell | 23e654f | 2008-09-27 16:20:03 -0400 | [diff] [blame] | 368 | CELT_MEMSET(x+N4, 0, N2); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 369 | |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 370 | for (b=0;b<B;b++) |
| 371 | { |
| 372 | /* De-interleaving the sub-frames */ |
| 373 | for (j=0;j<N2;j++) |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 374 | tmp[j] = X[(j*B+b)+c*N2*B]; |
Jean-Marc Valin | 72513f3 | 2010-07-07 21:26:38 -0400 | [diff] [blame] | 375 | clt_mdct_backward(&mode->mdct, tmp, x+n4offset+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 376 | } |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 377 | |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 378 | if (transient_shift > 0) |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 379 | { |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 380 | #ifdef FIXED_POINT |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 381 | for (j=0;j<16;j++) |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 382 | x[N4+transient_time+j-16] = MULT16_32_Q15(SHR16(Q15_ONE-transientWindow[j],transient_shift)+transientWindow[j], SHL32(x[N4+transient_time+j-16],transient_shift)); |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 383 | for (j=transient_time;j<N+overlap;j++) |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 384 | x[N4+j] = SHL32(x[N4+j], transient_shift); |
| 385 | #else |
| 386 | for (j=0;j<16;j++) |
Jean-Marc Valin | e4aeb47 | 2008-06-29 12:06:05 +1000 | [diff] [blame] | 387 | x[N4+transient_time+j-16] *= 1+transientWindow[j]*((1<<transient_shift)-1); |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 388 | for (j=transient_time;j<N+overlap;j++) |
| 389 | x[N4+j] *= 1<<transient_shift; |
| 390 | #endif |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 391 | } |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 392 | /* The first and last part would need to be set to zero |
| 393 | if we actually wanted to use them. */ |
Jean-Marc Valin | 8ddd7f4 | 2008-04-22 13:37:16 +1000 | [diff] [blame] | 394 | for (j=0;j<overlap;j++) |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 395 | out_mem[c][MAX_PERIOD-N+j] += x[j+N4]; |
Jean-Marc Valin | ab3784e | 2010-08-26 18:08:44 -0400 | [diff] [blame] | 396 | for (;j<overlap+N;j++) |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 397 | out_mem[c][MAX_PERIOD-N+j] = x[j+N4]; |
Jean-Marc Valin | 8ddd7f4 | 2008-04-22 13:37:16 +1000 | [diff] [blame] | 398 | RESTORE_STACK; |
| 399 | } |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 403 | 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] | 404 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 405 | const int C = CHANNELS(_C); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 406 | int c; |
| 407 | for (c=0;c<C;c++) |
| 408 | { |
| 409 | int j; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 410 | celt_sig * restrict x; |
| 411 | celt_word16 * restrict y; |
| 412 | celt_sig m = mem[c]; |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 413 | x = &in[c][MAX_PERIOD-N]; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 414 | y = pcm+c; |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 415 | for (j=0;j<N;j++) |
| 416 | { |
Jean-Marc Valin | af1fce9 | 2010-07-16 11:05:06 -0400 | [diff] [blame] | 417 | celt_sig tmp = *x + m; |
| 418 | m = MULT16_32_Q15(coef[0], tmp) |
| 419 | - MULT16_32_Q15(coef[1], *x); |
Jean-Marc Valin | 223b69d | 2010-07-16 11:47:50 -0400 | [diff] [blame] | 420 | tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2); |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 421 | *y = SCALEOUT(SIG2WORD16(tmp)); |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 422 | x++; |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 423 | y+=C; |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 424 | } |
Jean-Marc Valin | 64209a3 | 2010-04-05 09:26:22 -0400 | [diff] [blame] | 425 | mem[c] = m; |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 426 | } |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 427 | } |
| 428 | |
Jean-Marc Valin | d6b79ee | 2010-04-20 17:31:45 -0400 | [diff] [blame] | 429 | static void mdct_shape(const CELTMode *mode, celt_norm *X, int start, |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 430 | int end, int N, |
Jean-Marc Valin | 69921c4 | 2010-07-16 15:25:30 -0400 | [diff] [blame] | 431 | int mdct_weight_shift, int end_band, int _C, int renorm, int M) |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 432 | { |
| 433 | int m, i, c; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 434 | const int C = CHANNELS(_C); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 435 | for (c=0;c<C;c++) |
| 436 | for (m=start;m<end;m++) |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 437 | for (i=m+c*N;i<(c+1)*N;i+=M) |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 438 | #ifdef FIXED_POINT |
| 439 | X[i] = SHR16(X[i], mdct_weight_shift); |
| 440 | #else |
| 441 | X[i] = (1.f/(1<<mdct_weight_shift))*X[i]; |
| 442 | #endif |
Jean-Marc Valin | d6b79ee | 2010-04-20 17:31:45 -0400 | [diff] [blame] | 443 | if (renorm) |
Jean-Marc Valin | 69921c4 | 2010-07-16 15:25:30 -0400 | [diff] [blame] | 444 | renormalise_bands(mode, X, end_band, C, M); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 447 | static signed char tf_select_table[4][8] = { |
| 448 | {0, -1, 0, -1, 0,-1, 0,-1}, |
| 449 | {0, -1, 0, -2, 1, 0, 1 -1}, |
| 450 | {0, -2, 0, -3, 2, 0, 1 -1}, |
| 451 | {0, -2, 0, -3, 2, 0, 1 -1}, |
| 452 | }; |
| 453 | |
| 454 | static int tf_analysis(celt_word16 *bandLogE, celt_word16 *oldBandE, int len, int C, int isTransient, int *tf_res, int nbCompressedBytes) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 455 | { |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 456 | int i; |
Jean-Marc Valin | 0ef39c9 | 2010-05-28 17:50:34 -0400 | [diff] [blame] | 457 | celt_word16 threshold; |
Jean-Marc Valin | b6a5f9d | 2010-07-06 17:54:33 -0400 | [diff] [blame] | 458 | VARDECL(celt_word16, metric); |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 459 | celt_word32 average=0; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 460 | celt_word32 cost0; |
| 461 | celt_word32 cost1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 462 | VARDECL(int, path0); |
| 463 | VARDECL(int, path1); |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 464 | celt_word16 lambda; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 465 | int tf_select=0; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 466 | SAVE_STACK; |
| 467 | |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 468 | /* FIXME: Should check number of bytes *left* */ |
| 469 | if (nbCompressedBytes<15*C) |
| 470 | { |
| 471 | for (i=0;i<len;i++) |
| 472 | tf_res[i] = 0; |
| 473 | return 0; |
| 474 | } |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 475 | if (nbCompressedBytes<40) |
| 476 | lambda = QCONST16(5.f, DB_SHIFT); |
| 477 | else if (nbCompressedBytes<60) |
| 478 | lambda = QCONST16(2.f, DB_SHIFT); |
| 479 | else if (nbCompressedBytes<100) |
| 480 | lambda = QCONST16(1.f, DB_SHIFT); |
| 481 | else |
| 482 | lambda = QCONST16(.5f, DB_SHIFT); |
| 483 | |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 484 | ALLOC(metric, len, celt_word16); |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 485 | ALLOC(path0, len, int); |
| 486 | ALLOC(path1, len, int); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 487 | for (i=0;i<len;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 488 | { |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 489 | metric[i] = SUB16(bandLogE[i], oldBandE[i]); |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 490 | average += metric[i]; |
| 491 | } |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 492 | if (C==2) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 493 | { |
| 494 | average = 0; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 495 | for (i=0;i<len;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 496 | { |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 497 | metric[i] = HALF32(metric[i]) + HALF32(SUB16(bandLogE[i+len], oldBandE[i+len])); |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 498 | average += metric[i]; |
| 499 | } |
| 500 | } |
| 501 | average = DIV32(average, len); |
| 502 | /*if (!isTransient) |
| 503 | printf ("%f\n", average);*/ |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 504 | if (isTransient) |
| 505 | { |
Jean-Marc Valin | 0ef39c9 | 2010-05-28 17:50:34 -0400 | [diff] [blame] | 506 | threshold = QCONST16(1.f,DB_SHIFT); |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 507 | tf_select = average > QCONST16(3.f,DB_SHIFT); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 508 | } else { |
Jean-Marc Valin | 0ef39c9 | 2010-05-28 17:50:34 -0400 | [diff] [blame] | 509 | threshold = QCONST16(.5f,DB_SHIFT); |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 510 | tf_select = average > QCONST16(1.f,DB_SHIFT); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 511 | } |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 512 | cost0 = 0; |
| 513 | cost1 = lambda; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 514 | /* Viterbi forward pass */ |
| 515 | for (i=1;i<len;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 516 | { |
Jean-Marc Valin | b6a5f9d | 2010-07-06 17:54:33 -0400 | [diff] [blame] | 517 | celt_word32 curr0, curr1; |
| 518 | celt_word32 from0, from1; |
Jean-Marc Valin | 581fdba | 2010-05-28 06:56:23 -0400 | [diff] [blame] | 519 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 520 | from0 = cost0; |
| 521 | from1 = cost1 + lambda; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 522 | if (from0 < from1) |
| 523 | { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 524 | curr0 = from0; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 525 | path0[i]= 0; |
| 526 | } else { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 527 | curr0 = from1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 528 | path0[i]= 1; |
| 529 | } |
| 530 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 531 | from0 = cost0 + lambda; |
| 532 | from1 = cost1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 533 | if (from0 < from1) |
| 534 | { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 535 | curr1 = from0; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 536 | path1[i]= 0; |
| 537 | } else { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 538 | curr1 = from1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 539 | path1[i]= 1; |
| 540 | } |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 541 | cost0 = curr0 + (metric[i]-threshold); |
| 542 | cost1 = curr1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 543 | } |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 544 | tf_res[len-1] = cost0 < cost1 ? 0 : 1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 545 | /* Viterbi backward pass to check the decisions */ |
| 546 | for (i=len-2;i>=0;i--) |
| 547 | { |
| 548 | if (tf_res[i+1] == 1) |
| 549 | tf_res[i] = path1[i+1]; |
| 550 | else |
| 551 | tf_res[i] = path0[i+1]; |
| 552 | } |
Jean-Marc Valin | 71ae6d4 | 2010-06-27 21:55:08 -0400 | [diff] [blame] | 553 | RESTORE_STACK; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 554 | return tf_select; |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 555 | } |
| 556 | |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 557 | static void tf_encode(int start, int end, int isTransient, int *tf_res, int nbCompressedBytes, int LM, int tf_select, ec_enc *enc) |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 558 | { |
| 559 | int curr, i; |
Jean-Marc Valin | e2d5b33 | 2010-06-06 23:16:40 -0400 | [diff] [blame] | 560 | if (8*nbCompressedBytes - ec_enc_tell(enc, 0) < 100) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 561 | { |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 562 | for (i=start;i<end;i++) |
Jean-Marc Valin | e2d5b33 | 2010-06-06 23:16:40 -0400 | [diff] [blame] | 563 | tf_res[i] = isTransient; |
| 564 | } else { |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 565 | ec_enc_bit_prob(enc, tf_res[start], isTransient ? 16384 : 4096); |
| 566 | curr = tf_res[start]; |
| 567 | for (i=start+1;i<end;i++) |
Jean-Marc Valin | e2d5b33 | 2010-06-06 23:16:40 -0400 | [diff] [blame] | 568 | { |
| 569 | ec_enc_bit_prob(enc, tf_res[i] ^ curr, isTransient ? 4096 : 2048); |
| 570 | curr = tf_res[i]; |
| 571 | } |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 572 | } |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 573 | ec_enc_bits(enc, tf_select, 1); |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 574 | for (i=start;i<end;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 575 | 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] | 576 | } |
| 577 | |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 578 | static void tf_decode(int start, int end, int C, int isTransient, int *tf_res, int nbCompressedBytes, int LM, ec_dec *dec) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 579 | { |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 580 | int i, curr, tf_select; |
Jean-Marc Valin | e2d5b33 | 2010-06-06 23:16:40 -0400 | [diff] [blame] | 581 | if (8*nbCompressedBytes - ec_dec_tell(dec, 0) < 100) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 582 | { |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 583 | for (i=start;i<end;i++) |
Jean-Marc Valin | e2d5b33 | 2010-06-06 23:16:40 -0400 | [diff] [blame] | 584 | tf_res[i] = isTransient; |
| 585 | } else { |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 586 | tf_res[start] = ec_dec_bit_prob(dec, isTransient ? 16384 : 4096); |
| 587 | curr = tf_res[start]; |
| 588 | for (i=start+1;i<end;i++) |
Jean-Marc Valin | e2d5b33 | 2010-06-06 23:16:40 -0400 | [diff] [blame] | 589 | { |
| 590 | tf_res[i] = ec_dec_bit_prob(dec, isTransient ? 4096 : 2048) ^ curr; |
| 591 | curr = tf_res[i]; |
| 592 | } |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 593 | } |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 594 | tf_select = ec_dec_bits(dec, 1); |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 595 | for (i=start;i<end;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 596 | 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] | 597 | } |
| 598 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 599 | #ifdef FIXED_POINT |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 600 | int celt_encode_with_ec(CELTEncoder * restrict st, const celt_int16 * pcm, celt_int16 * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 601 | { |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 602 | #else |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 603 | int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, celt_sig * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 604 | { |
| 605 | #endif |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 606 | int i, c, N, NN, N4; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 607 | int bits; |
Jean-Marc Valin | bc79991 | 2008-11-07 22:53:13 -0500 | [diff] [blame] | 608 | int has_fold=1; |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 609 | ec_byte_buffer buf; |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 610 | ec_enc _enc; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 611 | VARDECL(celt_sig, in); |
| 612 | VARDECL(celt_sig, freq); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 613 | VARDECL(celt_norm, X); |
| 614 | VARDECL(celt_ener, bandE); |
| 615 | VARDECL(celt_word16, bandLogE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 616 | VARDECL(int, fine_quant); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 617 | VARDECL(celt_word16, error); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 618 | VARDECL(int, pulses); |
| 619 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 620 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 621 | VARDECL(int, tf_res); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 622 | int shortBlocks=0; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 623 | int isTransient=0; |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 624 | int transient_time, transient_time_quant; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 625 | int transient_shift; |
Jean-Marc Valin | b8ba70c | 2010-04-18 22:10:24 -0400 | [diff] [blame] | 626 | int resynth; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 627 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 628 | int mdct_weight_shift = 0; |
| 629 | int mdct_weight_pos=0; |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 630 | int LM, M; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 631 | int tf_select; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 632 | int nbFilledBytes, nbAvailableBytes; |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 633 | int effEnd; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 634 | SAVE_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 635 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 636 | if (check_encoder(st) != CELT_OK) |
| 637 | return CELT_INVALID_STATE; |
| 638 | |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 639 | if (check_mode(st->mode) != CELT_OK) |
| 640 | return CELT_INVALID_MODE; |
| 641 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 642 | if (nbCompressedBytes<0 || pcm==NULL) |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 643 | return CELT_BAD_ARG; |
Gregory Maxwell | 520eeae | 2009-02-09 01:33:21 -0500 | [diff] [blame] | 644 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 645 | for (LM=0;LM<4;LM++) |
| 646 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 647 | break; |
| 648 | if (LM>=MAX_CONFIG_SIZES) |
| 649 | return CELT_BAD_ARG; |
| 650 | M=1<<LM; |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 651 | |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 652 | if (enc==NULL) |
| 653 | { |
| 654 | ec_byte_writeinit_buffer(&buf, compressed, nbCompressedBytes); |
| 655 | ec_enc_init(&_enc,&buf); |
| 656 | enc = &_enc; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 657 | nbFilledBytes=0; |
| 658 | } else { |
Jean-Marc Valin | bdcaaf7 | 2010-07-05 13:52:41 -0400 | [diff] [blame] | 659 | nbFilledBytes=(ec_enc_tell(enc, 0)+4)>>3; |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 660 | } |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 661 | nbAvailableBytes = nbCompressedBytes - nbFilledBytes; |
| 662 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 663 | effEnd = st->end; |
| 664 | if (effEnd > st->mode->effEBands) |
| 665 | effEnd = st->mode->effEBands; |
| 666 | |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 667 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | a536f77 | 2008-03-22 09:01:50 +1100 | [diff] [blame] | 668 | N4 = (N-st->overlap)>>1; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 669 | ALLOC(in, 2*C*N-2*C*N4, celt_sig); |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 670 | |
Jean-Marc Valin | bdb5883 | 2008-04-20 17:42:10 +1000 | [diff] [blame] | 671 | CELT_COPY(in, st->in_mem, C*st->overlap); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 672 | for (c=0;c<C;c++) |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 673 | { |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 674 | const celt_word16 * restrict pcmp = pcm+c; |
| 675 | celt_sig * restrict inp = in+C*st->overlap+c; |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 676 | for (i=0;i<N;i++) |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 677 | { |
Jean-Marc Valin | 6229006 | 2008-04-20 22:16:02 +1000 | [diff] [blame] | 678 | /* Apply pre-emphasis */ |
Jean-Marc Valin | af1fce9 | 2010-07-16 11:05:06 -0400 | [diff] [blame] | 679 | celt_sig tmp = MULT16_16(st->mode->preemph[2], SCALEIN(*pcmp)); |
| 680 | *inp = tmp + st->preemph_memE[c]; |
| 681 | st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp) |
| 682 | - MULT16_32_Q15(st->mode->preemph[0], tmp); |
Jean-Marc Valin | 6229006 | 2008-04-20 22:16:02 +1000 | [diff] [blame] | 683 | inp += C; |
| 684 | pcmp += C; |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 685 | } |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 686 | } |
Jean-Marc Valin | bdb5883 | 2008-04-20 17:42:10 +1000 | [diff] [blame] | 687 | CELT_COPY(st->in_mem, in+C*(2*N-2*N4-st->overlap), C*st->overlap); |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 688 | |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 689 | /* Transient handling */ |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 690 | transient_time = -1; |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 691 | transient_time_quant = -1; |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 692 | transient_shift = 0; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 693 | isTransient = 0; |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 694 | |
Jean-Marc Valin | 8cbea17 | 2010-08-05 15:22:57 -0400 | [diff] [blame] | 695 | resynth = optional_resynthesis!=NULL; |
Jean-Marc Valin | b8ba70c | 2010-04-18 22:10:24 -0400 | [diff] [blame] | 696 | |
Jean-Marc Valin | bdb5871 | 2010-05-28 18:58:42 -0400 | [diff] [blame] | 697 | if (M > 1 && transient_analysis(in, N+st->overlap, C, &transient_time, &transient_shift, &st->frame_max, st->overlap)) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 698 | { |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 699 | #ifndef FIXED_POINT |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 700 | float gain_1; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 701 | #endif |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 702 | /* Apply the inverse shaping window */ |
| 703 | if (transient_shift) |
| 704 | { |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 705 | transient_time_quant = transient_time*(celt_int32)8000/st->mode->Fs; |
| 706 | transient_time = transient_time_quant*(celt_int32)st->mode->Fs/8000; |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 707 | #ifdef FIXED_POINT |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 708 | for (c=0;c<C;c++) |
| 709 | for (i=0;i<16;i++) |
| 710 | in[C*(transient_time+i-16)+c] = MULT16_32_Q15(EXTRACT16(SHR32(celt_rcp(Q15ONE+MULT16_16(transientWindow[i],((1<<transient_shift)-1))),1)), in[C*(transient_time+i-16)+c]); |
| 711 | for (c=0;c<C;c++) |
| 712 | for (i=transient_time;i<N+st->overlap;i++) |
| 713 | in[C*i+c] = SHR32(in[C*i+c], transient_shift); |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 714 | #else |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 715 | for (c=0;c<C;c++) |
| 716 | for (i=0;i<16;i++) |
| 717 | in[C*(transient_time+i-16)+c] /= 1+transientWindow[i]*((1<<transient_shift)-1); |
Jean-Marc Valin | 1299640 | 2010-08-04 09:13:24 -0400 | [diff] [blame] | 718 | gain_1 = 1.f/(1<<transient_shift); |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 719 | for (c=0;c<C;c++) |
| 720 | for (i=transient_time;i<N+st->overlap;i++) |
| 721 | in[C*i+c] *= gain_1; |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 722 | #endif |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 723 | } |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 724 | isTransient = 1; |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 725 | has_fold = 1; |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 726 | } |
Jean-Marc Valin | c5f2a9d | 2008-10-26 22:00:26 -0400 | [diff] [blame] | 727 | |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 728 | if (isTransient) |
| 729 | shortBlocks = M; |
| 730 | else |
| 731 | shortBlocks = 0; |
| 732 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 733 | ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 734 | ALLOC(bandE,st->mode->nbEBands*C, celt_ener); |
| 735 | ALLOC(bandLogE,st->mode->nbEBands*C, celt_word16); |
Jean-Marc Valin | 32ec58c | 2009-05-01 21:28:58 -0400 | [diff] [blame] | 736 | /* Compute MDCTs */ |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 737 | compute_mdcts(st->mode, shortBlocks, in, freq, C, LM); |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 738 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 739 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
Jean-Marc Valin | 8d4ac15 | 2008-02-29 17:24:02 +1100 | [diff] [blame] | 740 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 741 | compute_band_energies(st->mode, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | 504fb3c | 2010-08-06 15:56:22 -0400 | [diff] [blame] | 742 | |
| 743 | amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 744 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 745 | /* Band normalisation */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 746 | normalise_bands(st->mode, freq, X, bandE, effEnd, C, M); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 747 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 748 | NN = M*st->mode->eBands[effEnd]; |
Jean-Marc Valin | 9f89cab | 2010-05-21 14:18:38 -0400 | [diff] [blame] | 749 | if (shortBlocks && !transient_shift) |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 750 | { |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 751 | celt_word32 sum[8]={1,1,1,1,1,1,1,1}; |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 752 | int m; |
| 753 | for (c=0;c<C;c++) |
| 754 | { |
| 755 | m=0; |
| 756 | do { |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 757 | celt_word32 tmp=0; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 758 | for (i=m+c*N;i<c*N+NN;i+=M) |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 759 | tmp += ABS32(X[i]); |
| 760 | sum[m++] += tmp; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 761 | } while (m<M); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 762 | } |
| 763 | m=0; |
| 764 | #ifdef FIXED_POINT |
| 765 | do { |
| 766 | if (SHR32(sum[m+1],3) > sum[m]) |
| 767 | { |
| 768 | mdct_weight_shift=2; |
| 769 | mdct_weight_pos = m; |
| 770 | } else if (SHR32(sum[m+1],1) > sum[m] && mdct_weight_shift < 2) |
| 771 | { |
| 772 | mdct_weight_shift=1; |
| 773 | mdct_weight_pos = m; |
| 774 | } |
| 775 | m++; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 776 | } while (m<M-1); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 777 | #else |
| 778 | do { |
| 779 | if (sum[m+1] > 8*sum[m]) |
| 780 | { |
| 781 | mdct_weight_shift=2; |
| 782 | mdct_weight_pos = m; |
| 783 | } else if (sum[m+1] > 2*sum[m] && mdct_weight_shift < 2) |
| 784 | { |
| 785 | mdct_weight_shift=1; |
| 786 | mdct_weight_pos = m; |
| 787 | } |
| 788 | m++; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 789 | } while (m<M-1); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 790 | #endif |
| 791 | if (mdct_weight_shift) |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 792 | mdct_shape(st->mode, X, mdct_weight_pos+1, M, N, mdct_weight_shift, effEnd, C, 0, M); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 793 | } |
| 794 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 795 | ALLOC(tf_res, st->mode->nbEBands, int); |
Jean-Marc Valin | 5e7f02d | 2010-08-08 09:48:22 -0400 | [diff] [blame] | 796 | /* Needs to be before coarse energy quantization because otherwise the energy gets modified */ |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 797 | tf_select = tf_analysis(bandLogE, st->oldBandE, effEnd, C, isTransient, tf_res, nbAvailableBytes); |
| 798 | for (i=effEnd;i<st->end;i++) |
| 799 | tf_res[i] = tf_res[effEnd-1]; |
| 800 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 801 | ALLOC(error, C*st->mode->nbEBands, celt_word16); |
Jean-Marc Valin | 5e7f02d | 2010-08-08 09:48:22 -0400 | [diff] [blame] | 802 | quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE, |
| 803 | st->oldBandE, nbCompressedBytes*8, st->mode->prob, |
| 804 | error, enc, C, LM, nbAvailableBytes, st->force_intra, &st->delayedIntra); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 805 | |
Jean-Marc Valin | 017001a | 2010-08-05 15:42:50 -0400 | [diff] [blame] | 806 | ec_enc_bit_prob(enc, shortBlocks!=0, 8192); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 807 | |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 808 | if (shortBlocks) |
| 809 | { |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 810 | if (transient_shift) |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 811 | { |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 812 | int max_time = (N+st->mode->overlap)*(celt_int32)8000/st->mode->Fs; |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 813 | ec_enc_uint(enc, transient_shift, 4); |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 814 | ec_enc_uint(enc, transient_time_quant, max_time); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 815 | } else { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 816 | ec_enc_uint(enc, mdct_weight_shift, 4); |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 817 | if (mdct_weight_shift && M!=2) |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 818 | ec_enc_uint(enc, mdct_weight_pos, M-1); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 819 | } |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 820 | } |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 821 | |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 822 | tf_encode(st->start, st->end, isTransient, tf_res, nbAvailableBytes, LM, tf_select, enc); |
| 823 | |
Jean-Marc Valin | 908958c | 2010-08-07 22:15:02 -0400 | [diff] [blame] | 824 | if (!shortBlocks && !folding_decision(st->mode, X, &st->tonal_average, &st->fold_decision, effEnd, C, M)) |
| 825 | has_fold = 0; |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 826 | ec_enc_bit_prob(enc, has_fold>>1, 8192); |
| 827 | ec_enc_bit_prob(enc, has_fold&1, (has_fold>>1) ? 32768 : 49152); |
| 828 | |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 829 | /* Variable bitrate */ |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 830 | if (st->vbr_rate_norm>0) |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 831 | { |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 832 | celt_word16 alpha; |
Jean-Marc Valin | 25767d1 | 2009-10-21 23:24:18 -0400 | [diff] [blame] | 833 | celt_int32 delta; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 834 | /* The target rate in 16th bits per frame */ |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 835 | celt_int32 vbr_rate; |
| 836 | celt_int32 target; |
| 837 | celt_int32 vbr_bound, max_allowed; |
| 838 | |
| 839 | vbr_rate = M*st->vbr_rate_norm; |
| 840 | |
| 841 | /* Computes the max bit-rate allowed in VBR more to avoid busting the budget */ |
| 842 | vbr_bound = vbr_rate; |
| 843 | max_allowed = (vbr_rate + vbr_bound - st->vbr_reservoir)>>(BITRES+3); |
| 844 | if (max_allowed < 4) |
| 845 | max_allowed = 4; |
| 846 | if (max_allowed < nbAvailableBytes) |
| 847 | nbAvailableBytes = max_allowed; |
| 848 | target=vbr_rate; |
| 849 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 850 | /* Shortblocks get a large boost in bitrate, but since they |
| 851 | are uncommon long blocks are not greatly effected */ |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 852 | if (shortBlocks) |
| 853 | target*=2; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 854 | else if (M > 1) |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 855 | target-=(target+14)/28; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 856 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 857 | /* The average energy is removed from the target and the actual |
| 858 | energy added*/ |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 859 | target=target+st->vbr_offset-588+ec_enc_tell(enc, BITRES); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 860 | |
| 861 | /* In VBR mode the frame size must not be reduced so much that it would result in the coarse energy busting its budget */ |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 862 | target=IMIN(nbAvailableBytes,target); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 863 | /* Make the adaptation coef (alpha) higher at the beginning */ |
| 864 | if (st->vbr_count < 990) |
| 865 | { |
| 866 | st->vbr_count++; |
| 867 | alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+10),16)); |
| 868 | /*printf ("%d %d\n", st->vbr_count+10, alpha);*/ |
| 869 | } else |
| 870 | alpha = QCONST16(.001f,15); |
| 871 | |
| 872 | /* By how much did we "miss" the target on that frame */ |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 873 | delta = (8<<BITRES)*(celt_int32)target - vbr_rate; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 874 | /* How many bits have we used in excess of what we're allowed */ |
| 875 | st->vbr_reservoir += delta; |
| 876 | /*printf ("%d\n", st->vbr_reservoir);*/ |
| 877 | |
| 878 | /* Compute the offset we need to apply in order to reach the target */ |
| 879 | st->vbr_drift += MULT16_32_Q15(alpha,delta-st->vbr_offset-st->vbr_drift); |
| 880 | st->vbr_offset = -st->vbr_drift; |
| 881 | /*printf ("%d\n", st->vbr_drift);*/ |
| 882 | |
| 883 | /* We could use any multiple of vbr_rate as bound (depending on the delay) */ |
Jean-Marc Valin | 25767d1 | 2009-10-21 23:24:18 -0400 | [diff] [blame] | 884 | if (st->vbr_reservoir < 0) |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 885 | { |
| 886 | /* We're under the min value -- increase rate */ |
| 887 | int adjust = 1-(st->vbr_reservoir-1)/(8<<BITRES); |
| 888 | st->vbr_reservoir += adjust*(8<<BITRES); |
Jean-Marc Valin | 45f1110 | 2009-10-22 00:12:31 -0400 | [diff] [blame] | 889 | target += adjust; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 890 | /*printf ("+%d\n", adjust);*/ |
| 891 | } |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 892 | if (target < nbAvailableBytes) |
| 893 | nbAvailableBytes = target; |
| 894 | nbCompressedBytes = nbAvailableBytes + nbFilledBytes; |
| 895 | |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 896 | /* 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] | 897 | ec_byte_shrink(&buf, nbCompressedBytes); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 898 | } |
| 899 | |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 900 | /* Bit allocation */ |
| 901 | ALLOC(fine_quant, st->mode->nbEBands, int); |
| 902 | ALLOC(pulses, st->mode->nbEBands, int); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 903 | ALLOC(offsets, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 904 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 905 | |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 906 | for (i=0;i<st->mode->nbEBands;i++) |
| 907 | offsets[i] = 0; |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 908 | bits = nbCompressedBytes*8 - ec_enc_tell(enc, 0) - 1; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 909 | compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, M); |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 910 | |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 911 | quant_fine_energy(st->mode, st->start, st->end, bandE, st->oldBandE, error, fine_quant, enc, C); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 912 | |
Jean-Marc Valin | 7b5a086 | 2010-07-29 15:01:24 +0200 | [diff] [blame] | 913 | #ifdef MEASURE_NORM_MSE |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 914 | float X0[3000]; |
| 915 | float bandE0[60]; |
| 916 | for (c=0;c<C;c++) |
| 917 | for (i=0;i<N;i++) |
| 918 | X0[i+c*N] = X[i+c*N]; |
| 919 | for (i=0;i<C*st->mode->nbEBands;i++) |
Jean-Marc Valin | 7b5a086 | 2010-07-29 15:01:24 +0200 | [diff] [blame] | 920 | bandE0[i] = bandE[i]; |
| 921 | #endif |
| 922 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 923 | /* Residual quantisation */ |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 924 | quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, bandE, pulses, shortBlocks, has_fold, tf_res, resynth, nbCompressedBytes*8, enc, LM); |
Jean-Marc Valin | 3971053 | 2009-06-09 00:10:32 -0400 | [diff] [blame] | 925 | |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 926 | quant_energy_finalise(st->mode, st->start, st->end, bandE, st->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] | 927 | |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 928 | /* Re-synthesis of the coded audio if required */ |
Jean-Marc Valin | b8ba70c | 2010-04-18 22:10:24 -0400 | [diff] [blame] | 929 | if (resynth) |
Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 930 | { |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 931 | log2Amp(st->mode, st->start, st->end, bandE, st->oldBandE, C); |
| 932 | |
| 933 | #ifdef MEASURE_NORM_MSE |
| 934 | measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C); |
| 935 | #endif |
| 936 | |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 937 | if (mdct_weight_shift) |
| 938 | { |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 939 | mdct_shape(st->mode, X, 0, mdct_weight_pos+1, N, mdct_weight_shift, effEnd, C, 1, M); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 940 | } |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 941 | |
| 942 | /* Synthesis */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 943 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 944 | |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 945 | CELT_MOVE(st->out_mem[0], st->out_mem[0]+N, MAX_PERIOD+st->overlap-N); |
| 946 | if (C==2) |
| 947 | CELT_MOVE(st->out_mem[1], st->out_mem[1]+N, MAX_PERIOD+st->overlap-N); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 948 | |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 949 | for (c=0;c<C;c++) |
| 950 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 951 | freq[c*N+i] = 0; |
| 952 | for (c=0;c<C;c++) |
| 953 | for (i=M*st->mode->eBands[st->end];i<N;i++) |
| 954 | freq[c*N+i] = 0; |
| 955 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 956 | compute_inv_mdcts(st->mode, shortBlocks, freq, transient_time, transient_shift, st->out_mem, C, LM); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 957 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 958 | /* De-emphasis and put everything back at the right place |
| 959 | in the synthesis history */ |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 960 | if (optional_resynthesis != NULL) { |
Jean-Marc Valin | af1fce9 | 2010-07-16 11:05:06 -0400 | [diff] [blame] | 961 | deemphasis(st->out_mem, optional_resynthesis, N, C, st->mode->preemph, st->preemph_memD); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 962 | |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 963 | } |
Jean-Marc Valin | d9b9565 | 2008-08-31 23:34:47 -0400 | [diff] [blame] | 964 | } |
Gregory Maxwell | 54547f1 | 2009-02-16 18:56:44 -0500 | [diff] [blame] | 965 | |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 966 | /* If there's any room left (can only happen for very high rates), |
| 967 | fill it with zeros */ |
Jean-Marc Valin | 5d774e0 | 2010-08-04 17:17:18 -0400 | [diff] [blame] | 968 | while (ec_enc_tell(enc,0) + 8 <= nbCompressedBytes*8) |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 969 | ec_enc_bits(enc, 0, 8); |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 970 | ec_enc_done(enc); |
Jean-Marc Valin | c871c8d | 2009-06-09 00:57:00 -0400 | [diff] [blame] | 971 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 972 | RESTORE_STACK; |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 973 | if (ec_enc_get_error(enc)) |
Jean-Marc Valin | 9d785af | 2010-07-18 09:42:05 -0400 | [diff] [blame] | 974 | return CELT_CORRUPTED_DATA; |
| 975 | else |
| 976 | return nbCompressedBytes; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 977 | } |
| 978 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 979 | #ifdef FIXED_POINT |
| 980 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 981 | int celt_encode_with_ec_float(CELTEncoder * restrict st, const float * pcm, float * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 982 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 983 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 984 | VARDECL(celt_int16, in); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 985 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 986 | |
| 987 | if (check_encoder(st) != CELT_OK) |
| 988 | return CELT_INVALID_STATE; |
| 989 | |
| 990 | if (check_mode(st->mode) != CELT_OK) |
| 991 | return CELT_INVALID_MODE; |
| 992 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 993 | if (pcm==NULL) |
| 994 | return CELT_BAD_ARG; |
| 995 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 996 | for (LM=0;LM<4;LM++) |
| 997 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 998 | break; |
| 999 | if (LM>=MAX_CONFIG_SIZES) |
| 1000 | return CELT_BAD_ARG; |
| 1001 | M=1<<LM; |
| 1002 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1003 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1004 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1005 | ALLOC(in, C*N, celt_int16); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1006 | |
| 1007 | for (j=0;j<C*N;j++) |
| 1008 | in[j] = FLOAT2INT16(pcm[j]); |
| 1009 | |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1010 | if (optional_resynthesis != NULL) { |
| 1011 | ret=celt_encode_with_ec(st,in,in,frame_size,compressed,nbCompressedBytes, enc); |
Gregory Maxwell | b0a73a0 | 2008-12-12 16:54:25 -0500 | [diff] [blame] | 1012 | for (j=0;j<C*N;j++) |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1013 | optional_resynthesis[j]=in[j]*(1.f/32768.f); |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 1014 | } else { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1015 | ret=celt_encode_with_ec(st,in,NULL,frame_size,compressed,nbCompressedBytes, enc); |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 1016 | } |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1017 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1018 | return ret; |
| 1019 | |
| 1020 | } |
| 1021 | #endif /*DISABLE_FLOAT_API*/ |
| 1022 | #else |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1023 | int celt_encode_with_ec(CELTEncoder * restrict st, const celt_int16 * pcm, celt_int16 * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1024 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1025 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1026 | VARDECL(celt_sig, in); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1027 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1028 | |
| 1029 | if (check_encoder(st) != CELT_OK) |
| 1030 | return CELT_INVALID_STATE; |
| 1031 | |
| 1032 | if (check_mode(st->mode) != CELT_OK) |
| 1033 | return CELT_INVALID_MODE; |
| 1034 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1035 | if (pcm==NULL) |
| 1036 | return CELT_BAD_ARG; |
| 1037 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1038 | for (LM=0;LM<4;LM++) |
| 1039 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1040 | break; |
| 1041 | if (LM>=MAX_CONFIG_SIZES) |
| 1042 | return CELT_BAD_ARG; |
| 1043 | M=1<<LM; |
| 1044 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1045 | C=CHANNELS(st->channels); |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1046 | N=M*st->mode->shortMdctSize; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1047 | ALLOC(in, C*N, celt_sig); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1048 | for (j=0;j<C*N;j++) { |
| 1049 | in[j] = SCALEOUT(pcm[j]); |
| 1050 | } |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1051 | |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1052 | if (optional_resynthesis != NULL) { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1053 | ret = celt_encode_with_ec_float(st,in,in,frame_size,compressed,nbCompressedBytes, enc); |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 1054 | for (j=0;j<C*N;j++) |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1055 | optional_resynthesis[j] = FLOAT2INT16(in[j]); |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 1056 | } else { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1057 | ret = celt_encode_with_ec_float(st,in,NULL,frame_size,compressed,nbCompressedBytes, enc); |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 1058 | } |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1059 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1060 | return ret; |
| 1061 | } |
| 1062 | #endif |
Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 1063 | |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1064 | int celt_encode(CELTEncoder * restrict st, const celt_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
| 1065 | { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1066 | return celt_encode_with_ec(st, pcm, NULL, frame_size, compressed, nbCompressedBytes, NULL); |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1069 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1070 | int celt_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
| 1071 | { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1072 | return celt_encode_with_ec_float(st, pcm, NULL, frame_size, compressed, nbCompressedBytes, NULL); |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1073 | } |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1074 | #endif /* DISABLE_FLOAT_API */ |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1075 | |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1076 | int celt_encode_resynthesis(CELTEncoder * restrict st, const celt_int16 * pcm, celt_int16 * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
| 1077 | { |
| 1078 | return celt_encode_with_ec(st, pcm, optional_resynthesis, frame_size, compressed, nbCompressedBytes, NULL); |
| 1079 | } |
| 1080 | |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1081 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1082 | int celt_encode_resynthesis_float(CELTEncoder * restrict st, const float * pcm, float * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
| 1083 | { |
| 1084 | return celt_encode_with_ec_float(st, pcm, optional_resynthesis, frame_size, compressed, nbCompressedBytes, NULL); |
| 1085 | } |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1086 | #endif /* DISABLE_FLOAT_API */ |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1087 | |
| 1088 | |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1089 | int celt_encoder_ctl(CELTEncoder * restrict st, int request, ...) |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1090 | { |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1091 | va_list ap; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1092 | |
| 1093 | if (check_encoder(st) != CELT_OK) |
| 1094 | return CELT_INVALID_STATE; |
| 1095 | |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1096 | va_start(ap, request); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1097 | if ((request!=CELT_GET_MODE_REQUEST) && (check_mode(st->mode) != CELT_OK)) |
| 1098 | goto bad_mode; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1099 | switch (request) |
| 1100 | { |
Gregory Maxwell | f3b44ef | 2009-06-03 13:37:45 -0400 | [diff] [blame] | 1101 | case CELT_GET_MODE_REQUEST: |
| 1102 | { |
| 1103 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 1104 | if (value==0) |
| 1105 | goto bad_arg; |
| 1106 | *value=st->mode; |
| 1107 | } |
| 1108 | break; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1109 | case CELT_SET_COMPLEXITY_REQUEST: |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1110 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1111 | int value = va_arg(ap, celt_int32); |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1112 | if (value<0 || value>10) |
| 1113 | goto bad_arg; |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1114 | } |
| 1115 | break; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1116 | case CELT_SET_START_BAND_REQUEST: |
| 1117 | { |
| 1118 | celt_int32 value = va_arg(ap, celt_int32); |
| 1119 | if (value<0 || value>=st->mode->nbEBands) |
| 1120 | goto bad_arg; |
| 1121 | st->start = value; |
| 1122 | } |
| 1123 | break; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1124 | case CELT_SET_END_BAND_REQUEST: |
| 1125 | { |
| 1126 | celt_int32 value = va_arg(ap, celt_int32); |
| 1127 | if (value<0 || value>=st->mode->nbEBands) |
| 1128 | goto bad_arg; |
| 1129 | st->end = value; |
| 1130 | } |
| 1131 | break; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1132 | case CELT_SET_PREDICTION_REQUEST: |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1133 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1134 | int value = va_arg(ap, celt_int32); |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1135 | if (value<0 || value>2) |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1136 | goto bad_arg; |
| 1137 | if (value==0) |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1138 | { |
| 1139 | st->force_intra = 1; |
Gregory Maxwell | a80958b | 2009-06-29 12:48:57 -0400 | [diff] [blame] | 1140 | } else if (value==1) { |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1141 | st->force_intra = 0; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1142 | } else { |
| 1143 | st->force_intra = 0; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1144 | } |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1145 | } |
| 1146 | break; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1147 | case CELT_SET_VBR_RATE_REQUEST: |
| 1148 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1149 | celt_int32 value = va_arg(ap, celt_int32); |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 1150 | int frame_rate; |
| 1151 | int N = st->mode->shortMdctSize; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1152 | if (value<0) |
| 1153 | goto bad_arg; |
| 1154 | if (value>3072000) |
| 1155 | value = 3072000; |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 1156 | frame_rate = ((st->mode->Fs<<3)+(N>>1))/N; |
| 1157 | st->vbr_rate_norm = ((value<<(BITRES+3))+(frame_rate>>1))/frame_rate; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1158 | } |
| 1159 | break; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1160 | case CELT_RESET_STATE: |
| 1161 | { |
| 1162 | const CELTMode *mode = st->mode; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1163 | int C = st->channels; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1164 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1165 | CELT_MEMSET(st->in_mem, 0, st->overlap*C); |
| 1166 | CELT_MEMSET(st->out_mem, 0, (MAX_PERIOD+st->overlap)*C); |
| 1167 | |
| 1168 | CELT_MEMSET(st->oldBandE, 0, C*mode->nbEBands); |
| 1169 | |
| 1170 | CELT_MEMSET(st->preemph_memE, 0, C); |
| 1171 | CELT_MEMSET(st->preemph_memD, 0, C); |
| 1172 | st->delayedIntra = 1; |
John Ridges | 5378bf8 | 2010-02-12 07:08:01 -0500 | [diff] [blame] | 1173 | |
| 1174 | st->fold_decision = 1; |
Jean-Marc Valin | 628c025 | 2010-04-16 20:57:56 -0400 | [diff] [blame] | 1175 | st->tonal_average = QCONST16(1.f,8); |
John Ridges | 5378bf8 | 2010-02-12 07:08:01 -0500 | [diff] [blame] | 1176 | st->gain_prod = 0; |
| 1177 | st->vbr_reservoir = 0; |
| 1178 | st->vbr_drift = 0; |
| 1179 | st->vbr_offset = 0; |
| 1180 | st->vbr_count = 0; |
Jean-Marc Valin | 9c30de5 | 2010-04-19 13:32:15 -0400 | [diff] [blame] | 1181 | st->frame_max = 0; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1182 | } |
| 1183 | break; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1184 | default: |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1185 | goto bad_request; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1186 | } |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1187 | va_end(ap); |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1188 | return CELT_OK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1189 | bad_mode: |
| 1190 | va_end(ap); |
| 1191 | return CELT_INVALID_MODE; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1192 | bad_arg: |
| 1193 | va_end(ap); |
| 1194 | return CELT_BAD_ARG; |
| 1195 | bad_request: |
| 1196 | va_end(ap); |
| 1197 | return CELT_UNIMPLEMENTED; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1198 | } |
| 1199 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 1200 | /**********************************************************************/ |
| 1201 | /* */ |
| 1202 | /* DECODER */ |
| 1203 | /* */ |
| 1204 | /**********************************************************************/ |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1205 | #define DECODE_BUFFER_SIZE 2048 |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1206 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1207 | #define DECODERVALID 0x4c434454 |
| 1208 | #define DECODERPARTIAL 0x5444434c |
| 1209 | #define DECODERFREED 0x4c004400 |
| 1210 | |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 1211 | /** Decoder state |
| 1212 | @brief Decoder state |
| 1213 | */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1214 | struct CELTDecoder { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1215 | celt_uint32 marker; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1216 | const CELTMode *mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1217 | int overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1218 | int channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1219 | |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1220 | int start, end; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1221 | |
Jean-Marc Valin | ac1da4f | 2010-07-24 11:48:10 -0400 | [diff] [blame] | 1222 | celt_sig preemph_memD[2]; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 1223 | |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 1224 | celt_sig *out_mem[2]; |
| 1225 | celt_word32 *decode_mem[2]; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1226 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1227 | celt_word16 *oldBandE; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1228 | |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 1229 | celt_word16 *lpc; |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1230 | |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1231 | int last_pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1232 | int loss_count; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1233 | }; |
| 1234 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1235 | int check_decoder(const CELTDecoder *st) |
| 1236 | { |
| 1237 | if (st==NULL) |
| 1238 | { |
| 1239 | celt_warning("NULL passed a decoder structure"); |
| 1240 | return CELT_INVALID_STATE; |
| 1241 | } |
| 1242 | if (st->marker == DECODERVALID) |
| 1243 | return CELT_OK; |
| 1244 | if (st->marker == DECODERFREED) |
| 1245 | celt_warning("Referencing a decoder that has already been freed"); |
| 1246 | else |
| 1247 | celt_warning("This is not a valid CELT decoder structure"); |
| 1248 | return CELT_INVALID_STATE; |
| 1249 | } |
| 1250 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1251 | CELTDecoder *celt_decoder_create(const CELTMode *mode, int channels, int *error) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1252 | { |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1253 | int C; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1254 | CELTDecoder *st; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1255 | |
| 1256 | if (check_mode(mode) != CELT_OK) |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1257 | { |
| 1258 | if (error) |
| 1259 | *error = CELT_INVALID_MODE; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1260 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1261 | } |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 1262 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1263 | if (channels < 0 || channels > 2) |
| 1264 | { |
| 1265 | celt_warning("Only mono and stereo supported"); |
| 1266 | if (error) |
| 1267 | *error = CELT_BAD_ARG; |
| 1268 | return NULL; |
| 1269 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1270 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1271 | C = CHANNELS(channels); |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1272 | st = celt_alloc(sizeof(CELTDecoder)); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1273 | |
| 1274 | if (st==NULL) |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1275 | { |
| 1276 | if (error) |
| 1277 | *error = CELT_ALLOC_FAIL; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1278 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1279 | } |
| 1280 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1281 | st->marker = DECODERPARTIAL; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1282 | st->mode = mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1283 | st->overlap = mode->overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1284 | st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1285 | |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1286 | st->start = 0; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 1287 | st->end = st->mode->effEBands; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1288 | |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 1289 | st->decode_mem[0] = (celt_sig*)celt_alloc((DECODE_BUFFER_SIZE+st->overlap)*C*sizeof(celt_sig)); |
| 1290 | if (C==2) |
| 1291 | st->decode_mem[1] = st->decode_mem[0] + (DECODE_BUFFER_SIZE+st->overlap); |
| 1292 | st->out_mem[0] = st->decode_mem[0]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 1293 | if (C==2) |
| 1294 | st->out_mem[1] = st->decode_mem[1]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1295 | st->oldBandE = (celt_word16*)celt_alloc(C*mode->nbEBands*sizeof(celt_word16)); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1296 | |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 1297 | st->lpc = (celt_word16*)celt_alloc(C*LPC_ORDER*sizeof(celt_word16)); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1298 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1299 | st->loss_count = 0; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1300 | |
| 1301 | if ((st->decode_mem!=NULL) && (st->out_mem!=NULL) && (st->oldBandE!=NULL) && |
Jean-Marc Valin | ac1da4f | 2010-07-24 11:48:10 -0400 | [diff] [blame] | 1302 | (st->lpc!=NULL)) |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1303 | { |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1304 | if (error) |
| 1305 | *error = CELT_OK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1306 | st->marker = DECODERVALID; |
| 1307 | return st; |
| 1308 | } |
| 1309 | /* If the setup fails for some reason deallocate it. */ |
| 1310 | celt_decoder_destroy(st); |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1311 | if (error) |
| 1312 | *error = CELT_ALLOC_FAIL; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1313 | return NULL; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1314 | } |
| 1315 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 1316 | void celt_decoder_destroy(CELTDecoder *st) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1317 | { |
| 1318 | if (st == NULL) |
| 1319 | { |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1320 | celt_warning("NULL passed to celt_decoder_destroy"); |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1321 | return; |
| 1322 | } |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1323 | |
| 1324 | if (st->marker == DECODERFREED) |
| 1325 | { |
| 1326 | celt_warning("Freeing a decoder which has already been freed"); |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1327 | return; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | if (st->marker != DECODERVALID && st->marker != DECODERPARTIAL) |
| 1331 | { |
| 1332 | celt_warning("This is not a valid CELT decoder structure"); |
| 1333 | return; |
| 1334 | } |
| 1335 | |
| 1336 | /*Check_mode is non-fatal here because we can still free |
| 1337 | the encoder memory even if the mode is bad, although calling |
| 1338 | the free functions in this order is a violation of the API.*/ |
| 1339 | check_mode(st->mode); |
| 1340 | |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 1341 | celt_free(st->decode_mem[0]); |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1342 | celt_free(st->oldBandE); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1343 | celt_free(st->lpc); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1344 | |
| 1345 | st->marker = DECODERFREED; |
| 1346 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1347 | celt_free(st); |
| 1348 | } |
| 1349 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1350 | 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] | 1351 | { |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1352 | int c; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1353 | int pitch_index; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1354 | int overlap = st->mode->overlap; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1355 | celt_word16 fade = Q15ONE; |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1356 | int i, len; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1357 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1358 | int offset; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1359 | SAVE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1360 | |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1361 | len = N+st->mode->overlap; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1362 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1363 | if (st->loss_count == 0) |
| 1364 | { |
Jean-Marc Valin | 7a7c42a | 2009-11-25 20:38:52 -0500 | [diff] [blame] | 1365 | celt_word16 pitch_buf[MAX_PERIOD>>1]; |
Jean-Marc Valin | 294863b | 2009-11-08 22:29:54 +0900 | [diff] [blame] | 1366 | celt_word32 tmp=0; |
Jean-Marc Valin | e465c14 | 2009-11-26 00:39:36 -0500 | [diff] [blame] | 1367 | celt_word32 mem0[2]={0,0}; |
| 1368 | celt_word16 mem1[2]={0,0}; |
Jean-Marc Valin | 6202c74 | 2010-06-01 00:30:37 -0400 | [diff] [blame] | 1369 | int len2 = len; |
| 1370 | /* FIXME: This is a kludge */ |
| 1371 | if (len2>MAX_PERIOD>>1) |
| 1372 | len2 = MAX_PERIOD>>1; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1373 | pitch_downsample(st->out_mem, pitch_buf, MAX_PERIOD, MAX_PERIOD, |
| 1374 | C, mem0, mem1); |
Jean-Marc Valin | 6202c74 | 2010-06-01 00:30:37 -0400 | [diff] [blame] | 1375 | pitch_search(st->mode, pitch_buf+((MAX_PERIOD-len2)>>1), pitch_buf, len2, |
| 1376 | MAX_PERIOD-len2-100, &pitch_index, &tmp, 1<<LM); |
| 1377 | pitch_index = MAX_PERIOD-len2-pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1378 | st->last_pitch_index = pitch_index; |
| 1379 | } else { |
| 1380 | pitch_index = st->last_pitch_index; |
| 1381 | if (st->loss_count < 5) |
| 1382 | fade = QCONST16(.8f,15); |
| 1383 | else |
| 1384 | fade = 0; |
| 1385 | } |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1386 | |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1387 | for (c=0;c<C;c++) |
| 1388 | { |
Jean-Marc Valin | 6202c74 | 2010-06-01 00:30:37 -0400 | [diff] [blame] | 1389 | /* FIXME: This is more memory than necessary */ |
| 1390 | celt_word32 e[2*MAX_PERIOD]; |
| 1391 | celt_word16 exc[2*MAX_PERIOD]; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 1392 | celt_word32 ac[LPC_ORDER+1]; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1393 | celt_word16 decay = 1; |
| 1394 | celt_word32 S1=0; |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 1395 | celt_word16 mem[LPC_ORDER]={0}; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1396 | |
Jean-Marc Valin | d5f9930 | 2009-12-16 22:42:32 -0500 | [diff] [blame] | 1397 | offset = MAX_PERIOD-pitch_index; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1398 | for (i=0;i<MAX_PERIOD;i++) |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 1399 | exc[i] = ROUND16(st->out_mem[c][i], SIG_SHIFT); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1400 | |
| 1401 | if (st->loss_count == 0) |
| 1402 | { |
| 1403 | _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap, |
| 1404 | LPC_ORDER, MAX_PERIOD); |
| 1405 | |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 1406 | /* Noise floor -40 dB */ |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1407 | #ifdef FIXED_POINT |
| 1408 | ac[0] += SHR32(ac[0],13); |
| 1409 | #else |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1410 | ac[0] *= 1.0001f; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1411 | #endif |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1412 | /* Lag windowing */ |
| 1413 | for (i=1;i<=LPC_ORDER;i++) |
| 1414 | { |
| 1415 | /*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] | 1416 | #ifdef FIXED_POINT |
| 1417 | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); |
| 1418 | #else |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1419 | ac[i] -= ac[i]*(.008f*i)*(.008f*i); |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1420 | #endif |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1421 | } |
| 1422 | |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 1423 | _celt_lpc(st->lpc+c*LPC_ORDER, ac, LPC_ORDER); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1424 | } |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 1425 | fir(exc, st->lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem); |
| 1426 | /*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] | 1427 | /* Check if the waveform is decaying (and if so how fast) */ |
| 1428 | { |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1429 | celt_word32 E1=1, E2=1; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1430 | int period; |
| 1431 | if (pitch_index <= MAX_PERIOD/2) |
| 1432 | period = pitch_index; |
| 1433 | else |
| 1434 | period = MAX_PERIOD/2; |
| 1435 | for (i=0;i<period;i++) |
| 1436 | { |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1437 | E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8); |
| 1438 | 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] | 1439 | } |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1440 | if (E1 > E2) |
| 1441 | E1 = E2; |
| 1442 | decay = celt_sqrt(frac_div32(SHR(E1,1),E2)); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | /* Copy excitation, taking decay into account */ |
| 1446 | for (i=0;i<len+st->mode->overlap;i++) |
| 1447 | { |
| 1448 | if (offset+i >= MAX_PERIOD) |
| 1449 | { |
| 1450 | offset -= pitch_index; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1451 | decay = MULT16_16_Q15(decay, decay); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1452 | } |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1453 | e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT); |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 1454 | S1 += SHR32(MULT16_16(st->out_mem[c][offset+i],st->out_mem[c][offset+i]),8); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1455 | } |
| 1456 | |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 1457 | iir(e, st->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] | 1458 | |
| 1459 | { |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1460 | celt_word32 S2=0; |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1461 | for (i=0;i<len+overlap;i++) |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1462 | S2 += SHR32(MULT16_16(e[i],e[i]),8); |
Jean-Marc Valin | feca095 | 2010-06-17 20:25:51 -0400 | [diff] [blame] | 1463 | /* This checks for an "explosion" in the synthesis */ |
| 1464 | #ifdef FIXED_POINT |
| 1465 | if (!(S1 > SHR32(S2,2))) |
| 1466 | #else |
| 1467 | /* 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] | 1468 | if (!(S1 > 0.2f*S2)) |
Jean-Marc Valin | feca095 | 2010-06-17 20:25:51 -0400 | [diff] [blame] | 1469 | #endif |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 1470 | { |
| 1471 | for (i=0;i<len+overlap;i++) |
| 1472 | e[i] = 0; |
| 1473 | } else if (S1 < S2) |
| 1474 | { |
Jean-Marc Valin | 1299640 | 2010-08-04 09:13:24 -0400 | [diff] [blame] | 1475 | 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] | 1476 | for (i=0;i<len+overlap;i++) |
Jean-Marc Valin | feca095 | 2010-06-17 20:25:51 -0400 | [diff] [blame] | 1477 | e[i] = MULT16_16_Q15(ratio, e[i]); |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 1478 | } |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++) |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 1482 | st->out_mem[c][i] = st->out_mem[c][N+i]; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1483 | |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1484 | /* Apply TDAC to the concealed audio so that it blends with the |
| 1485 | previous and next frames */ |
| 1486 | for (i=0;i<overlap/2;i++) |
| 1487 | { |
Jean-Marc Valin | d69c1cb | 2009-12-28 00:34:29 -0500 | [diff] [blame] | 1488 | celt_word32 tmp1, tmp2; |
| 1489 | tmp1 = MULT16_32_Q15(st->mode->window[i ], e[i ]) - |
| 1490 | MULT16_32_Q15(st->mode->window[overlap-i-1], e[overlap-i-1]); |
| 1491 | tmp2 = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) + |
| 1492 | MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]); |
| 1493 | tmp1 = MULT16_32_Q15(fade, tmp1); |
| 1494 | tmp2 = MULT16_32_Q15(fade, tmp2); |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 1495 | st->out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp2); |
| 1496 | st->out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp2); |
| 1497 | st->out_mem[c][MAX_PERIOD-N+i] += MULT16_32_Q15(st->mode->window[i], tmp1); |
| 1498 | st->out_mem[c][MAX_PERIOD-N+overlap-i-1] -= MULT16_32_Q15(st->mode->window[overlap-i-1], tmp1); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1499 | } |
| 1500 | for (i=0;i<N-overlap;i++) |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 1501 | st->out_mem[c][MAX_PERIOD-N+overlap+i] = MULT16_32_Q15(fade, e[overlap+i]); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1502 | } |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 1503 | |
Jean-Marc Valin | af1fce9 | 2010-07-16 11:05:06 -0400 | [diff] [blame] | 1504 | deemphasis(st->out_mem, pcm, N, C, st->mode->preemph, st->preemph_memD); |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1505 | |
| 1506 | st->loss_count++; |
| 1507 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1508 | RESTORE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1509 | } |
| 1510 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1511 | #ifdef FIXED_POINT |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1512 | 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] | 1513 | { |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1514 | #else |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1515 | 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] | 1516 | { |
| 1517 | #endif |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1518 | int c, i, N, N4; |
Jean-Marc Valin | 8cbea17 | 2010-08-05 15:22:57 -0400 | [diff] [blame] | 1519 | int has_fold; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 1520 | int bits; |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1521 | ec_dec _dec; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1522 | ec_byte_buffer buf; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1523 | VARDECL(celt_sig, freq); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1524 | VARDECL(celt_norm, X); |
| 1525 | VARDECL(celt_ener, bandE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1526 | VARDECL(int, fine_quant); |
| 1527 | VARDECL(int, pulses); |
| 1528 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1529 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 1530 | VARDECL(int, tf_res); |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 1531 | |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 1532 | int shortBlocks; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1533 | int isTransient; |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 1534 | int intra_ener; |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 1535 | int transient_time; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 1536 | int transient_shift; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1537 | int mdct_weight_shift=0; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1538 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1539 | int mdct_weight_pos=0; |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1540 | int LM, M; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 1541 | int nbFilledBytes, nbAvailableBytes; |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1542 | int effEnd; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1543 | SAVE_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1544 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1545 | if (check_decoder(st) != CELT_OK) |
| 1546 | return CELT_INVALID_STATE; |
| 1547 | |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1548 | if (check_mode(st->mode) != CELT_OK) |
| 1549 | return CELT_INVALID_MODE; |
| 1550 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1551 | if (pcm==NULL) |
| 1552 | return CELT_BAD_ARG; |
| 1553 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1554 | for (LM=0;LM<4;LM++) |
| 1555 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1556 | break; |
| 1557 | if (LM>=MAX_CONFIG_SIZES) |
| 1558 | return CELT_BAD_ARG; |
| 1559 | M=1<<LM; |
| 1560 | |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1561 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | a536f77 | 2008-03-22 09:01:50 +1100 | [diff] [blame] | 1562 | N4 = (N-st->overlap)>>1; |
Jean-Marc Valin | 0141723 | 2008-03-03 13:59:55 +1100 | [diff] [blame] | 1563 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1564 | effEnd = st->end; |
| 1565 | if (effEnd > st->mode->effEBands) |
| 1566 | effEnd = st->mode->effEBands; |
| 1567 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1568 | ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 1569 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
| 1570 | ALLOC(bandE, st->mode->nbEBands*C, celt_ener); |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1571 | for (c=0;c<C;c++) |
| 1572 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 1573 | X[c*N+i] = 0; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1574 | for (c=0;c<C;c++) |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1575 | for (i=M*st->mode->eBands[effEnd];i<N;i++) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1576 | X[c*N+i] = 0; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1577 | |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1578 | if (data == NULL) |
| 1579 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1580 | celt_decode_lost(st, pcm, N, LM); |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1581 | RESTORE_STACK; |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1582 | return CELT_OK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1583 | } |
Gregory Maxwell | 520eeae | 2009-02-09 01:33:21 -0500 | [diff] [blame] | 1584 | if (len<0) { |
| 1585 | RESTORE_STACK; |
| 1586 | return CELT_BAD_ARG; |
| 1587 | } |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1588 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1589 | if (dec == NULL) |
| 1590 | { |
| 1591 | ec_byte_readinit(&buf,(unsigned char*)data,len); |
| 1592 | ec_dec_init(&_dec,&buf); |
| 1593 | dec = &_dec; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 1594 | nbFilledBytes = 0; |
| 1595 | } else { |
Jean-Marc Valin | bdcaaf7 | 2010-07-05 13:52:41 -0400 | [diff] [blame] | 1596 | nbFilledBytes = (ec_dec_tell(dec, 0)+4)>>3; |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1597 | } |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 1598 | nbAvailableBytes = len-nbFilledBytes; |
| 1599 | |
Jean-Marc Valin | 017001a | 2010-08-05 15:42:50 -0400 | [diff] [blame] | 1600 | /* Decode the global flags (first symbols in the stream) */ |
| 1601 | intra_ener = ec_dec_bit_prob(dec, 8192); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1602 | /* Get band energies */ |
| 1603 | unquant_coarse_energy(st->mode, st->start, st->end, bandE, st->oldBandE, intra_ener, st->mode->prob, dec, C, LM); |
| 1604 | |
Jean-Marc Valin | 017001a | 2010-08-05 15:42:50 -0400 | [diff] [blame] | 1605 | isTransient = ec_dec_bit_prob(dec, 8192); |
Jean-Marc Valin | 017001a | 2010-08-05 15:42:50 -0400 | [diff] [blame] | 1606 | |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1607 | if (isTransient) |
| 1608 | shortBlocks = M; |
| 1609 | else |
| 1610 | shortBlocks = 0; |
| 1611 | |
| 1612 | if (isTransient) |
Gregory Maxwell | 0527f37 | 2008-09-23 19:28:35 -0400 | [diff] [blame] | 1613 | { |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1614 | transient_shift = ec_dec_uint(dec, 4); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1615 | if (transient_shift == 3) |
| 1616 | { |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 1617 | int transient_time_quant; |
| 1618 | int max_time = (N+st->mode->overlap)*(celt_int32)8000/st->mode->Fs; |
| 1619 | transient_time_quant = ec_dec_uint(dec, max_time); |
| 1620 | transient_time = transient_time_quant*(celt_int32)st->mode->Fs/8000; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1621 | } else { |
| 1622 | mdct_weight_shift = transient_shift; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1623 | if (mdct_weight_shift && M>2) |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1624 | mdct_weight_pos = ec_dec_uint(dec, M-1); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1625 | transient_shift = 0; |
Gregory Maxwell | 0527f37 | 2008-09-23 19:28:35 -0400 | [diff] [blame] | 1626 | transient_time = 0; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1627 | } |
Gregory Maxwell | 0527f37 | 2008-09-23 19:28:35 -0400 | [diff] [blame] | 1628 | } else { |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 1629 | transient_time = -1; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 1630 | transient_shift = 0; |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 1631 | } |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 1632 | |
| 1633 | ALLOC(tf_res, st->mode->nbEBands, int); |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 1634 | tf_decode(st->start, st->end, C, isTransient, tf_res, nbAvailableBytes, LM, dec); |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 1635 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1636 | has_fold = ec_dec_bit_prob(dec, 8192)<<1; |
| 1637 | has_fold |= ec_dec_bit_prob(dec, (has_fold>>1) ? 32768 : 49152); |
| 1638 | |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1639 | ALLOC(pulses, st->mode->nbEBands, int); |
| 1640 | ALLOC(offsets, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1641 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1642 | |
| 1643 | for (i=0;i<st->mode->nbEBands;i++) |
| 1644 | offsets[i] = 0; |
| 1645 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1646 | bits = len*8 - ec_dec_tell(dec, 0) - 1; |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1647 | ALLOC(fine_quant, st->mode->nbEBands, int); |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1648 | compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, M); |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1649 | /*bits = ec_dec_tell(dec, 0); |
| 1650 | compute_fine_allocation(st->mode, fine_quant, (20*C+len*8/5-(ec_dec_tell(dec, 0)-bits))/C);*/ |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1651 | |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1652 | unquant_fine_energy(st->mode, st->start, st->end, bandE, st->oldBandE, fine_quant, dec, C); |
Jean-Marc Valin | 827f931 | 2008-05-06 23:21:55 +1000 | [diff] [blame] | 1653 | |
Jean-Marc Valin | 8cbea17 | 2010-08-05 15:22:57 -0400 | [diff] [blame] | 1654 | /* Decode fixed codebook */ |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 1655 | quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, NULL, pulses, shortBlocks, has_fold, tf_res, 1, len*8, dec, LM); |
Jean-Marc Valin | 746b2a8 | 2010-05-14 22:12:33 -0400 | [diff] [blame] | 1656 | |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1657 | unquant_energy_finalise(st->mode, st->start, st->end, bandE, st->oldBandE, 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] | 1658 | |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 1659 | log2Amp(st->mode, st->start, st->end, bandE, st->oldBandE, C); |
| 1660 | |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1661 | if (mdct_weight_shift) |
| 1662 | { |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1663 | mdct_shape(st->mode, X, 0, mdct_weight_pos+1, N, mdct_weight_shift, effEnd, C, 1, M); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1664 | } |
| 1665 | |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 1666 | /* Synthesis */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1667 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 1668 | |
Jean-Marc Valin | 903dbf7 | 2010-08-26 20:06:49 -0400 | [diff] [blame^] | 1669 | CELT_MOVE(st->decode_mem[0], st->decode_mem[0]+N, DECODE_BUFFER_SIZE+st->overlap-N); |
| 1670 | if (C==2) |
| 1671 | CELT_MOVE(st->decode_mem[1], st->decode_mem[1]+N, DECODE_BUFFER_SIZE+st->overlap-N); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1672 | |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1673 | for (c=0;c<C;c++) |
| 1674 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 1675 | freq[c*N+i] = 0; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1676 | for (c=0;c<C;c++) |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1677 | for (i=M*st->mode->eBands[effEnd];i<N;i++) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1678 | freq[c*N+i] = 0; |
Jean-Marc Valin | 3a0bc3d | 2010-02-21 15:10:22 -0500 | [diff] [blame] | 1679 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1680 | /* Compute inverse MDCTs */ |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1681 | compute_inv_mdcts(st->mode, shortBlocks, freq, transient_time, transient_shift, st->out_mem, C, LM); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 1682 | |
Jean-Marc Valin | af1fce9 | 2010-07-16 11:05:06 -0400 | [diff] [blame] | 1683 | deemphasis(st->out_mem, pcm, N, C, st->mode->preemph, st->preemph_memD); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1684 | st->loss_count = 0; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1685 | RESTORE_STACK; |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1686 | if (ec_dec_get_error(dec)) |
| 1687 | return CELT_CORRUPTED_DATA; |
| 1688 | else |
| 1689 | return CELT_OK; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1690 | } |
| 1691 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1692 | #ifdef FIXED_POINT |
| 1693 | #ifndef DISABLE_FLOAT_API |
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, float * restrict pcm, int frame_size, ec_dec *dec) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1695 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1696 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1697 | VARDECL(celt_int16, out); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1698 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1699 | |
| 1700 | if (check_decoder(st) != CELT_OK) |
| 1701 | return CELT_INVALID_STATE; |
| 1702 | |
| 1703 | if (check_mode(st->mode) != CELT_OK) |
| 1704 | return CELT_INVALID_MODE; |
| 1705 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1706 | if (pcm==NULL) |
| 1707 | return CELT_BAD_ARG; |
| 1708 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1709 | for (LM=0;LM<4;LM++) |
| 1710 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1711 | break; |
| 1712 | if (LM>=MAX_CONFIG_SIZES) |
| 1713 | return CELT_BAD_ARG; |
| 1714 | M=1<<LM; |
| 1715 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1716 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1717 | N = M*st->mode->shortMdctSize; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1718 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1719 | ALLOC(out, C*N, celt_int16); |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1720 | 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] | 1721 | if (ret==0) |
| 1722 | for (j=0;j<C*N;j++) |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1723 | pcm[j]=out[j]*(1.f/32768.f); |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1724 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1725 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1726 | return ret; |
| 1727 | } |
| 1728 | #endif /*DISABLE_FLOAT_API*/ |
| 1729 | #else |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1730 | 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] | 1731 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1732 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1733 | VARDECL(celt_sig, out); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1734 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1735 | |
| 1736 | if (check_decoder(st) != CELT_OK) |
| 1737 | return CELT_INVALID_STATE; |
| 1738 | |
| 1739 | if (check_mode(st->mode) != CELT_OK) |
| 1740 | return CELT_INVALID_MODE; |
| 1741 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1742 | if (pcm==NULL) |
| 1743 | return CELT_BAD_ARG; |
| 1744 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1745 | for (LM=0;LM<4;LM++) |
| 1746 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1747 | break; |
| 1748 | if (LM>=MAX_CONFIG_SIZES) |
| 1749 | return CELT_BAD_ARG; |
| 1750 | M=1<<LM; |
| 1751 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1752 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1753 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1754 | ALLOC(out, C*N, celt_sig); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1755 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1756 | 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] | 1757 | |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1758 | if (ret==0) |
| 1759 | for (j=0;j<C*N;j++) |
| 1760 | pcm[j] = FLOAT2INT16 (out[j]); |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1761 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1762 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1763 | return ret; |
| 1764 | } |
| 1765 | #endif |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1766 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1767 | int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size) |
| 1768 | { |
| 1769 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); |
| 1770 | } |
| 1771 | |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1772 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1773 | int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size) |
| 1774 | { |
| 1775 | return celt_decode_with_ec_float(st, data, len, pcm, frame_size, NULL); |
| 1776 | } |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1777 | #endif /* DISABLE_FLOAT_API */ |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1778 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1779 | int celt_decoder_ctl(CELTDecoder * restrict st, int request, ...) |
| 1780 | { |
| 1781 | va_list ap; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1782 | |
| 1783 | if (check_decoder(st) != CELT_OK) |
| 1784 | return CELT_INVALID_STATE; |
| 1785 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1786 | va_start(ap, request); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1787 | if ((request!=CELT_GET_MODE_REQUEST) && (check_mode(st->mode) != CELT_OK)) |
| 1788 | goto bad_mode; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1789 | switch (request) |
| 1790 | { |
Gregory Maxwell | f3b44ef | 2009-06-03 13:37:45 -0400 | [diff] [blame] | 1791 | case CELT_GET_MODE_REQUEST: |
| 1792 | { |
| 1793 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 1794 | if (value==0) |
| 1795 | goto bad_arg; |
| 1796 | *value=st->mode; |
| 1797 | } |
| 1798 | break; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1799 | case CELT_SET_START_BAND_REQUEST: |
| 1800 | { |
| 1801 | celt_int32 value = va_arg(ap, celt_int32); |
| 1802 | if (value<0 || value>=st->mode->nbEBands) |
| 1803 | goto bad_arg; |
| 1804 | st->start = value; |
| 1805 | } |
| 1806 | break; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1807 | case CELT_SET_END_BAND_REQUEST: |
| 1808 | { |
| 1809 | celt_int32 value = va_arg(ap, celt_int32); |
| 1810 | if (value<0 || value>=st->mode->nbEBands) |
| 1811 | goto bad_arg; |
| 1812 | st->end = value; |
| 1813 | } |
| 1814 | break; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1815 | case CELT_RESET_STATE: |
| 1816 | { |
| 1817 | const CELTMode *mode = st->mode; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1818 | int C = st->channels; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1819 | |
| 1820 | CELT_MEMSET(st->decode_mem, 0, (DECODE_BUFFER_SIZE+st->overlap)*C); |
| 1821 | CELT_MEMSET(st->oldBandE, 0, C*mode->nbEBands); |
| 1822 | |
| 1823 | CELT_MEMSET(st->preemph_memD, 0, C); |
| 1824 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1825 | st->loss_count = 0; |
John Ridges | 5378bf8 | 2010-02-12 07:08:01 -0500 | [diff] [blame] | 1826 | |
John Ridges | 5378bf8 | 2010-02-12 07:08:01 -0500 | [diff] [blame] | 1827 | CELT_MEMSET(st->lpc, 0, C*LPC_ORDER); |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1828 | } |
| 1829 | break; |
| 1830 | default: |
| 1831 | goto bad_request; |
| 1832 | } |
| 1833 | va_end(ap); |
| 1834 | return CELT_OK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1835 | bad_mode: |
| 1836 | va_end(ap); |
| 1837 | return CELT_INVALID_MODE; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1838 | bad_arg: |
| 1839 | va_end(ap); |
| 1840 | return CELT_BAD_ARG; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1841 | bad_request: |
| 1842 | va_end(ap); |
| 1843 | return CELT_UNIMPLEMENTED; |
| 1844 | } |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1845 | |
| 1846 | const char *celt_strerror(int error) |
| 1847 | { |
| 1848 | static const char *error_strings[8] = { |
| 1849 | "success", |
| 1850 | "invalid argument", |
| 1851 | "invalid mode", |
| 1852 | "internal error", |
| 1853 | "corrupted stream", |
| 1854 | "request not implemented", |
| 1855 | "invalid state", |
| 1856 | "memory allocation failed" |
| 1857 | }; |
| 1858 | if (error > 0 || error < -7) |
| 1859 | return "unknown error"; |
| 1860 | else |
| 1861 | return error_strings[-error]; |
| 1862 | } |
| 1863 | |