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 | 7351e28 | 2008-02-08 15:09:45 +1100 | [diff] [blame] | 45 | #include "kiss_fftr.h" |
Jean-Marc Valin | 991c0f0 | 2007-11-30 16:07:46 +1100 | [diff] [blame] | 46 | #include "bands.h" |
Jean-Marc Valin | ecb36a3 | 2007-12-05 01:31:49 +1100 | [diff] [blame] | 47 | #include "modes.h" |
Jean-Marc Valin | 6238bc0 | 2008-01-28 22:28:54 +1100 | [diff] [blame] | 48 | #include "entcode.h" |
Jean-Marc Valin | 98d2a49 | 2007-12-07 22:46:47 +1100 | [diff] [blame] | 49 | #include "quant_bands.h" |
Jean-Marc Valin | 46014ca | 2007-12-14 13:47:04 +1100 | [diff] [blame] | 50 | #include "psy.h" |
Jean-Marc Valin | 4fbd18d | 2008-01-17 14:07:55 +1100 | [diff] [blame] | 51 | #include "rate.h" |
Jean-Marc Valin | c7e0b76 | 2008-03-16 07:55:29 +1100 | [diff] [blame] | 52 | #include "stack_alloc.h" |
Jean-Marc Valin | e4aeb47 | 2008-06-29 12:06:05 +1000 | [diff] [blame] | 53 | #include "mathops.h" |
Jean-Marc Valin | d9b9565 | 2008-08-31 23:34:47 -0400 | [diff] [blame] | 54 | #include "float_cast.h" |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 55 | #include <stdarg.h> |
Jean-Marc Valin | 013c31d | 2007-11-30 11:36:46 +1100 | [diff] [blame] | 56 | |
Jean-Marc Valin | 0758689 | 2008-03-03 17:19:17 +1100 | [diff] [blame] | 57 | static const celt_word16_t preemph = QCONST16(0.8f,15); |
| 58 | |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 59 | #ifdef FIXED_POINT |
| 60 | static const celt_word16_t transientWindow[16] = { |
| 61 | 279, 1106, 2454, 4276, 6510, 9081, 11900, 14872, |
| 62 | 17896, 20868, 23687, 26258, 28492, 30314, 31662, 32489}; |
| 63 | #else |
| 64 | static const float transientWindow[16] = { |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 65 | 0.0085135, 0.0337639, 0.0748914, 0.1304955, |
| 66 | 0.1986827, 0.2771308, 0.3631685, 0.4538658, |
| 67 | 0.5461342, 0.6368315, 0.7228692, 0.8013173, |
| 68 | 0.8695045, 0.9251086, 0.9662361, 0.9914865}; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 69 | #endif |
| 70 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 71 | #define ENCODERVALID 0x4c434554 |
| 72 | #define ENCODERPARTIAL 0x5445434c |
| 73 | #define ENCODERFREED 0x4c004500 |
Jean-Marc Valin | c994394 | 2008-08-30 00:55:07 -0400 | [diff] [blame] | 74 | |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 75 | /** Encoder state |
| 76 | @brief Encoder state |
| 77 | */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 78 | struct CELTEncoder { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 79 | celt_uint32 marker; |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 80 | const CELTMode *mode; /**< Mode used by the encoder */ |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 81 | int frame_size; |
| 82 | int block_size; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 83 | int overlap; |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 84 | int channels; |
| 85 | |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 86 | int pitch_enabled; /* Complexity level is allowed to use pitch */ |
| 87 | int pitch_permitted; /* Use of the LTP is permitted by the user */ |
| 88 | int pitch_available; /* Amount of pitch buffer available */ |
| 89 | int force_intra; |
Jean-Marc Valin | 74f4e9f | 2009-05-02 09:57:50 -0400 | [diff] [blame] | 90 | int delayedIntra; |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 91 | celt_word16_t tonal_average; |
| 92 | int fold_decision; |
Jean-Marc Valin | d35d677 | 2009-10-16 23:16:47 -0400 | [diff] [blame] | 93 | celt_word16_t gain_prod; |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 94 | |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 95 | int VBR_rate; /* Target number of 16th bits per frame */ |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 96 | celt_word16_t * restrict preemph_memE; |
Jean-Marc Valin | 65fb92e | 2008-04-19 22:24:53 +1000 | [diff] [blame] | 97 | celt_sig_t * restrict preemph_memD; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 98 | |
Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 99 | celt_sig_t *in_mem; |
Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 100 | celt_sig_t *out_mem; |
Jean-Marc Valin | 991c0f0 | 2007-11-30 16:07:46 +1100 | [diff] [blame] | 101 | |
Jean-Marc Valin | 5d56183 | 2008-02-27 11:59:05 +1100 | [diff] [blame] | 102 | celt_word16_t *oldBandE; |
Jean-Marc Valin | c6ca499 | 2008-06-01 23:19:16 +1000 | [diff] [blame] | 103 | #ifdef EXP_PSY |
| 104 | celt_word16_t *psy_mem; |
| 105 | struct PsyDecay psy; |
| 106 | #endif |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 107 | }; |
| 108 | |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 109 | static int check_encoder(const CELTEncoder *st) |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 110 | { |
| 111 | if (st==NULL) |
| 112 | { |
| 113 | celt_warning("NULL passed as an encoder structure"); |
| 114 | return CELT_INVALID_STATE; |
| 115 | } |
| 116 | if (st->marker == ENCODERVALID) |
| 117 | return CELT_OK; |
| 118 | if (st->marker == ENCODERFREED) |
| 119 | celt_warning("Referencing an encoder that has already been freed"); |
| 120 | else |
| 121 | celt_warning("This is not a valid CELT encoder structure"); |
| 122 | return CELT_INVALID_STATE; |
| 123 | } |
| 124 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 125 | CELTEncoder *celt_encoder_create(const CELTMode *mode, int channels, int *error) |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 126 | { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 127 | int N, C; |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 128 | CELTEncoder *st; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 129 | |
| 130 | if (check_mode(mode) != CELT_OK) |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 131 | { |
| 132 | if (error) |
| 133 | *error = CELT_INVALID_MODE; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 134 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 135 | } |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 136 | if (channels < 0 || channels > 2) |
| 137 | { |
| 138 | celt_warning("Only mono and stereo supported"); |
| 139 | if (error) |
| 140 | *error = CELT_BAD_ARG; |
| 141 | return NULL; |
| 142 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 143 | |
Jean-Marc Valin | 73e51b3 | 2007-12-05 17:48:24 +1100 | [diff] [blame] | 144 | N = mode->mdctSize; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 145 | C = channels; |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 146 | st = celt_alloc(sizeof(CELTEncoder)); |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 147 | |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 148 | if (st==NULL) |
| 149 | { |
| 150 | if (error) |
| 151 | *error = CELT_ALLOC_FAIL; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 152 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 153 | } |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 154 | st->marker = ENCODERPARTIAL; |
Jean-Marc Valin | 73e51b3 | 2007-12-05 17:48:24 +1100 | [diff] [blame] | 155 | st->mode = mode; |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 156 | st->frame_size = N; |
Jean-Marc Valin | 73e51b3 | 2007-12-05 17:48:24 +1100 | [diff] [blame] | 157 | st->block_size = N; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 158 | st->overlap = mode->overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 159 | st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 160 | |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 161 | st->VBR_rate = 0; |
Jean-Marc Valin | c994394 | 2008-08-30 00:55:07 -0400 | [diff] [blame] | 162 | st->pitch_enabled = 1; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 163 | st->pitch_permitted = 1; |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 164 | st->pitch_available = 1; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 165 | st->force_intra = 0; |
Gregory Maxwell | 8842fde | 2009-05-04 15:58:40 -0400 | [diff] [blame] | 166 | st->delayedIntra = 1; |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 167 | st->tonal_average = QCONST16(1.,8); |
| 168 | st->fold_decision = 1; |
Jean-Marc Valin | a76a0b2 | 2008-01-17 22:43:05 +1100 | [diff] [blame] | 169 | |
Jean-Marc Valin | 7aa9d8c | 2008-04-21 00:04:23 +1000 | [diff] [blame] | 170 | st->in_mem = celt_alloc(st->overlap*C*sizeof(celt_sig_t)); |
Jean-Marc Valin | 8ddd7f4 | 2008-04-22 13:37:16 +1000 | [diff] [blame] | 171 | st->out_mem = celt_alloc((MAX_PERIOD+st->overlap)*C*sizeof(celt_sig_t)); |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 172 | |
Jean-Marc Valin | 5d56183 | 2008-02-27 11:59:05 +1100 | [diff] [blame] | 173 | st->oldBandE = (celt_word16_t*)celt_alloc(C*mode->nbEBands*sizeof(celt_word16_t)); |
Jean-Marc Valin | fa24873 | 2007-12-06 07:51:27 +1100 | [diff] [blame] | 174 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 175 | st->preemph_memE = (celt_word16_t*)celt_alloc(C*sizeof(celt_word16_t)); |
| 176 | st->preemph_memD = (celt_sig_t*)celt_alloc(C*sizeof(celt_sig_t)); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 177 | |
Jean-Marc Valin | c6ca499 | 2008-06-01 23:19:16 +1000 | [diff] [blame] | 178 | #ifdef EXP_PSY |
| 179 | st->psy_mem = celt_alloc(MAX_PERIOD*sizeof(celt_word16_t)); |
| 180 | psydecay_init(&st->psy, MAX_PERIOD/2, st->mode->Fs); |
| 181 | #endif |
| 182 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 183 | if ((st->in_mem!=NULL) && (st->out_mem!=NULL) && (st->oldBandE!=NULL) |
| 184 | #ifdef EXP_PSY |
| 185 | && (st->psy_mem!=NULL) |
| 186 | #endif |
| 187 | && (st->preemph_memE!=NULL) && (st->preemph_memD!=NULL)) |
| 188 | { |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 189 | if (error) |
| 190 | *error = CELT_OK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 191 | st->marker = ENCODERVALID; |
| 192 | return st; |
| 193 | } |
| 194 | /* If the setup fails for some reason deallocate it. */ |
| 195 | celt_encoder_destroy(st); |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 196 | if (error) |
| 197 | *error = CELT_ALLOC_FAIL; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 198 | return NULL; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 199 | } |
| 200 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 201 | void celt_encoder_destroy(CELTEncoder *st) |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 202 | { |
| 203 | if (st == NULL) |
| 204 | { |
| 205 | celt_warning("NULL passed to celt_encoder_destroy"); |
| 206 | return; |
| 207 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 208 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 209 | if (st->marker == ENCODERFREED) |
| 210 | { |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 211 | celt_warning("Freeing an encoder which has already been freed"); |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 212 | return; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (st->marker != ENCODERVALID && st->marker != ENCODERPARTIAL) |
| 216 | { |
| 217 | celt_warning("This is not a valid CELT encoder structure"); |
| 218 | return; |
| 219 | } |
| 220 | /*Check_mode is non-fatal here because we can still free |
| 221 | the encoder memory even if the mode is bad, although calling |
| 222 | the free functions in this order is a violation of the API.*/ |
| 223 | check_mode(st->mode); |
| 224 | |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 225 | celt_free(st->in_mem); |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 226 | celt_free(st->out_mem); |
Jean-Marc Valin | 991c0f0 | 2007-11-30 16:07:46 +1100 | [diff] [blame] | 227 | |
Jean-Marc Valin | c245a22 | 2007-12-06 19:14:20 +1100 | [diff] [blame] | 228 | celt_free(st->oldBandE); |
Jean-Marc Valin | 7351e28 | 2008-02-08 15:09:45 +1100 | [diff] [blame] | 229 | |
| 230 | celt_free(st->preemph_memE); |
| 231 | celt_free(st->preemph_memD); |
| 232 | |
Jean-Marc Valin | c6ca499 | 2008-06-01 23:19:16 +1000 | [diff] [blame] | 233 | #ifdef EXP_PSY |
| 234 | celt_free (st->psy_mem); |
| 235 | psydecay_clear(&st->psy); |
| 236 | #endif |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 237 | st->marker = ENCODERFREED; |
Jean-Marc Valin | c6ca499 | 2008-06-01 23:19:16 +1000 | [diff] [blame] | 238 | |
Jean-Marc Valin | 14191b3 | 2007-11-30 12:15:49 +1100 | [diff] [blame] | 239 | celt_free(st); |
| 240 | } |
| 241 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 242 | static inline celt_int16 FLOAT2INT16(float x) |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 243 | { |
Gregory Maxwell | 0ac2b2f | 2009-05-21 23:08:46 -0400 | [diff] [blame] | 244 | x = x*CELT_SIG_SCALE; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 245 | x = MAX32(x, -32768); |
| 246 | x = MIN32(x, 32767); |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 247 | return (celt_int16)float2int(x); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static inline celt_word16_t SIG2WORD16(celt_sig_t x) |
| 251 | { |
| 252 | #ifdef FIXED_POINT |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 253 | x = PSHR32(x, SIG_SHIFT); |
Jean-Marc Valin | abdfc38 | 2008-04-18 15:57:18 +1000 | [diff] [blame] | 254 | x = MAX32(x, -32768); |
| 255 | x = MIN32(x, 32767); |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 256 | return EXTRACT16(x); |
| 257 | #else |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 258 | return (celt_word16_t)x; |
Jean-Marc Valin | 4207438 | 2008-02-27 11:08:53 +1100 | [diff] [blame] | 259 | #endif |
| 260 | } |
| 261 | |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 262 | static int transient_analysis(celt_word32_t *in, int len, int C, int *transient_time, int *transient_shift) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 263 | { |
| 264 | int c, i, n; |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 265 | celt_word32_t ratio; |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 266 | VARDECL(celt_word32_t, begin); |
| 267 | SAVE_STACK; |
| 268 | ALLOC(begin, len, celt_word32_t); |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 269 | for (i=0;i<len;i++) |
Jean-Marc Valin | 5c3bc67 | 2008-08-28 23:34:24 -0400 | [diff] [blame] | 270 | begin[i] = ABS32(SHR32(in[C*i],SIG_SHIFT)); |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 271 | for (c=1;c<C;c++) |
| 272 | { |
| 273 | for (i=0;i<len;i++) |
Jean-Marc Valin | 5c3bc67 | 2008-08-28 23:34:24 -0400 | [diff] [blame] | 274 | begin[i] = MAX32(begin[i], ABS32(SHR32(in[C*i+c],SIG_SHIFT))); |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 275 | } |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 276 | for (i=1;i<len;i++) |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 277 | begin[i] = MAX32(begin[i-1],begin[i]); |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 278 | n = -1; |
| 279 | for (i=8;i<len-8;i++) |
| 280 | { |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 281 | if (begin[i] < MULT16_32_Q15(QCONST16(.2f,15),begin[len-1])) |
| 282 | n=i; |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 283 | } |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 284 | if (n<32) |
| 285 | { |
| 286 | n = -1; |
| 287 | ratio = 0; |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 288 | } else { |
| 289 | ratio = DIV32(begin[len-1],1+begin[n-16]); |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 290 | } |
Jean-Marc Valin | f457819 | 2008-07-04 16:47:28 -0400 | [diff] [blame] | 291 | if (ratio < 0) |
| 292 | ratio = 0; |
| 293 | if (ratio > 1000) |
| 294 | ratio = 1000; |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 295 | ratio *= ratio; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 296 | |
| 297 | if (ratio > 2048) |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 298 | *transient_shift = 3; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 299 | else |
| 300 | *transient_shift = 0; |
| 301 | |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 302 | *transient_time = n; |
| 303 | |
Jean-Marc Valin | 0ceccb2 | 2008-07-03 20:41:06 -0400 | [diff] [blame] | 304 | RESTORE_STACK; |
Jean-Marc Valin | bd7de0a | 2008-07-06 10:57:26 -0400 | [diff] [blame] | 305 | return ratio > 20; |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 306 | } |
| 307 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 308 | /** Apply window and compute the MDCT for all sub-frames and |
| 309 | all channels in a frame */ |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 310 | static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig_t * restrict in, celt_sig_t * restrict out, int _C) |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 311 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 312 | const int C = CHANNELS(_C); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 313 | if (C==1 && !shortBlocks) |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 314 | { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 315 | const mdct_lookup *lookup = MDCT(mode); |
| 316 | const int overlap = OVERLAP(mode); |
| 317 | mdct_forward(lookup, in, out, mode->window, overlap); |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 318 | } else { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 319 | const mdct_lookup *lookup = MDCT(mode); |
| 320 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 321 | int N = FRAMESIZE(mode); |
| 322 | int B = 1; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 323 | int b, c; |
| 324 | VARDECL(celt_word32_t, x); |
| 325 | VARDECL(celt_word32_t, tmp); |
| 326 | SAVE_STACK; |
Jean-Marc Valin | aa4f58b | 2009-10-03 09:27:59 -0400 | [diff] [blame] | 327 | if (shortBlocks) |
| 328 | { |
| 329 | lookup = &mode->shortMdct; |
| 330 | N = mode->shortMdctSize; |
| 331 | B = mode->nbShortMdcts; |
| 332 | } |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 333 | ALLOC(x, N+overlap, celt_word32_t); |
| 334 | ALLOC(tmp, N, celt_word32_t); |
| 335 | for (c=0;c<C;c++) |
| 336 | { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 337 | for (b=0;b<B;b++) |
| 338 | { |
| 339 | int j; |
| 340 | for (j=0;j<N+overlap;j++) |
| 341 | x[j] = in[C*(b*N+j)+c]; |
| 342 | mdct_forward(lookup, x, tmp, mode->window, overlap); |
| 343 | /* Interleaving the sub-frames */ |
| 344 | for (j=0;j<N;j++) |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 345 | out[(j*B+b)+c*N*B] = tmp[j]; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | RESTORE_STACK; |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 349 | } |
Jean-Marc Valin | da72188 | 2007-11-30 15:17:42 +1100 | [diff] [blame] | 350 | } |
| 351 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 352 | /** Compute the IMDCT and apply window for all sub-frames and |
| 353 | all channels in a frame */ |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 354 | static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig_t *X, int transient_time, int transient_shift, celt_sig_t * restrict out_mem, int _C) |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 355 | { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 356 | int c, N4; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 357 | const int C = CHANNELS(_C); |
Jean-Marc Valin | b18ec0b | 2008-04-11 04:07:52 +1000 | [diff] [blame] | 358 | const int N = FRAMESIZE(mode); |
| 359 | const int overlap = OVERLAP(mode); |
Jean-Marc Valin | a536f77 | 2008-03-22 09:01:50 +1100 | [diff] [blame] | 360 | N4 = (N-overlap)>>1; |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 361 | for (c=0;c<C;c++) |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 362 | { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 363 | int j; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 364 | if (transient_shift==0 && C==1 && !shortBlocks) { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 365 | const mdct_lookup *lookup = MDCT(mode); |
| 366 | mdct_backward(lookup, X, out_mem+C*(MAX_PERIOD-N-N4), mode->window, overlap); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 367 | } else { |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 368 | VARDECL(celt_word32_t, x); |
| 369 | VARDECL(celt_word32_t, tmp); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 370 | int b; |
| 371 | int N2 = N; |
| 372 | int B = 1; |
| 373 | int n4offset=0; |
| 374 | const mdct_lookup *lookup = MDCT(mode); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 375 | SAVE_STACK; |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 376 | |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 377 | ALLOC(x, 2*N, celt_word32_t); |
| 378 | ALLOC(tmp, N, celt_word32_t); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 379 | |
| 380 | if (shortBlocks) |
| 381 | { |
| 382 | lookup = &mode->shortMdct; |
| 383 | N2 = mode->shortMdctSize; |
| 384 | B = mode->nbShortMdcts; |
| 385 | n4offset = N4; |
| 386 | } |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 387 | /* Prevents problems from the imdct doing the overlap-add */ |
Gregory Maxwell | 23e654f | 2008-09-27 16:20:03 -0400 | [diff] [blame] | 388 | CELT_MEMSET(x+N4, 0, N2); |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 389 | |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 390 | for (b=0;b<B;b++) |
| 391 | { |
| 392 | /* De-interleaving the sub-frames */ |
| 393 | for (j=0;j<N2;j++) |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 394 | tmp[j] = X[(j*B+b)+c*N2*B]; |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 395 | mdct_backward(lookup, tmp, x+n4offset+N2*b, mode->window, overlap); |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 396 | } |
Jean-Marc Valin | de67858 | 2009-10-03 10:36:27 -0400 | [diff] [blame] | 397 | |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 398 | if (transient_shift > 0) |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 399 | { |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 400 | #ifdef FIXED_POINT |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 401 | for (j=0;j<16;j++) |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 402 | 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] | 403 | for (j=transient_time;j<N+overlap;j++) |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 404 | x[N4+j] = SHL32(x[N4+j], transient_shift); |
| 405 | #else |
| 406 | for (j=0;j<16;j++) |
Jean-Marc Valin | e4aeb47 | 2008-06-29 12:06:05 +1000 | [diff] [blame] | 407 | 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] | 408 | for (j=transient_time;j<N+overlap;j++) |
| 409 | x[N4+j] *= 1<<transient_shift; |
| 410 | #endif |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 411 | } |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 412 | /* The first and last part would need to be set to zero |
| 413 | if we actually wanted to use them. */ |
Jean-Marc Valin | 8ddd7f4 | 2008-04-22 13:37:16 +1000 | [diff] [blame] | 414 | for (j=0;j<overlap;j++) |
| 415 | out_mem[C*(MAX_PERIOD-N)+C*j+c] += x[j+N4]; |
| 416 | for (j=0;j<overlap;j++) |
| 417 | out_mem[C*(MAX_PERIOD)+C*(overlap-j-1)+c] = x[2*N-j-N4-1]; |
| 418 | for (j=0;j<2*N4;j++) |
| 419 | out_mem[C*(MAX_PERIOD-N)+C*(j+overlap)+c] = x[j+N4+overlap]; |
| 420 | RESTORE_STACK; |
| 421 | } |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 425 | #define FLAG_NONE 0 |
Jean-Marc Valin | 5a70097 | 2009-08-08 13:23:03 -0400 | [diff] [blame] | 426 | #define FLAG_INTRA (1U<<13) |
| 427 | #define FLAG_PITCH (1U<<12) |
| 428 | #define FLAG_SHORT (1U<<11) |
| 429 | #define FLAG_FOLD (1U<<10) |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 430 | #define FLAG_MASK (FLAG_INTRA|FLAG_PITCH|FLAG_SHORT|FLAG_FOLD) |
| 431 | |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 432 | static const int flaglist[8] = { |
Jean-Marc Valin | a8be38a | 2009-04-29 22:16:26 -0400 | [diff] [blame] | 433 | 0 /*00 */ | FLAG_FOLD, |
| 434 | 1 /*01 */ | FLAG_PITCH|FLAG_FOLD, |
| 435 | 8 /*1000*/ | FLAG_NONE, |
| 436 | 9 /*1001*/ | FLAG_SHORT|FLAG_FOLD, |
| 437 | 10 /*1010*/ | FLAG_PITCH, |
| 438 | 11 /*1011*/ | FLAG_INTRA, |
| 439 | 6 /*110 */ | FLAG_INTRA|FLAG_FOLD, |
| 440 | 7 /*111 */ | FLAG_INTRA|FLAG_SHORT|FLAG_FOLD |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 441 | }; |
| 442 | |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 443 | static void encode_flags(ec_enc *enc, int intra_ener, int has_pitch, int shortBlocks, int has_fold) |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 444 | { |
| 445 | int i; |
| 446 | int flags=FLAG_NONE; |
| 447 | int flag_bits; |
| 448 | flags |= intra_ener ? FLAG_INTRA : 0; |
| 449 | flags |= has_pitch ? FLAG_PITCH : 0; |
| 450 | flags |= shortBlocks ? FLAG_SHORT : 0; |
| 451 | flags |= has_fold ? FLAG_FOLD : 0; |
| 452 | for (i=0;i<8;i++) |
| 453 | if (flags == (flaglist[i]&FLAG_MASK)) |
| 454 | break; |
| 455 | celt_assert(i<8); |
| 456 | flag_bits = flaglist[i]&0xf; |
| 457 | /*printf ("enc %d: %d %d %d %d\n", flag_bits, intra_ener, has_pitch, shortBlocks, has_fold);*/ |
| 458 | if (i<2) |
Jean-Marc Valin | c08be44 | 2009-06-17 23:23:46 -0400 | [diff] [blame] | 459 | ec_enc_uint(enc, flag_bits, 4); |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 460 | else if (i<6) |
Jean-Marc Valin | c08be44 | 2009-06-17 23:23:46 -0400 | [diff] [blame] | 461 | ec_enc_uint(enc, flag_bits, 16); |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 462 | else |
Jean-Marc Valin | c08be44 | 2009-06-17 23:23:46 -0400 | [diff] [blame] | 463 | ec_enc_uint(enc, flag_bits, 8); |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 464 | } |
| 465 | |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 466 | static void decode_flags(ec_dec *dec, int *intra_ener, int *has_pitch, int *shortBlocks, int *has_fold) |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 467 | { |
| 468 | int i; |
| 469 | int flag_bits; |
Jean-Marc Valin | c08be44 | 2009-06-17 23:23:46 -0400 | [diff] [blame] | 470 | flag_bits = ec_dec_uint(dec, 4); |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 471 | /*printf ("(%d) ", flag_bits);*/ |
| 472 | if (flag_bits==2) |
Jean-Marc Valin | c08be44 | 2009-06-17 23:23:46 -0400 | [diff] [blame] | 473 | flag_bits = (flag_bits<<2) | ec_dec_uint(dec, 4); |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 474 | else if (flag_bits==3) |
Jean-Marc Valin | c08be44 | 2009-06-17 23:23:46 -0400 | [diff] [blame] | 475 | flag_bits = (flag_bits<<1) | ec_dec_uint(dec, 2); |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 476 | for (i=0;i<8;i++) |
| 477 | if (flag_bits == (flaglist[i]&0xf)) |
| 478 | break; |
| 479 | celt_assert(i<8); |
| 480 | *intra_ener = (flaglist[i]&FLAG_INTRA) != 0; |
| 481 | *has_pitch = (flaglist[i]&FLAG_PITCH) != 0; |
| 482 | *shortBlocks = (flaglist[i]&FLAG_SHORT) != 0; |
| 483 | *has_fold = (flaglist[i]&FLAG_FOLD ) != 0; |
| 484 | /*printf ("dec %d: %d %d %d %d\n", flag_bits, *intra_ener, *has_pitch, *shortBlocks, *has_fold);*/ |
| 485 | } |
| 486 | |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 487 | static void deemphasis(celt_sig_t *in, celt_word16_t *pcm, int N, int _C, celt_word16_t coef, celt_sig_t *mem) |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 488 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 489 | const int C = CHANNELS(_C); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 490 | int c; |
| 491 | for (c=0;c<C;c++) |
| 492 | { |
| 493 | int j; |
| 494 | for (j=0;j<N;j++) |
| 495 | { |
| 496 | celt_sig_t tmp = MAC16_32_Q15(in[C*(MAX_PERIOD-N)+C*j+c], |
| 497 | coef,mem[c]); |
| 498 | mem[c] = tmp; |
| 499 | pcm[C*j+c] = SCALEOUT(SIG2WORD16(tmp)); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | } |
| 504 | |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 505 | static void mdct_shape(const CELTMode *mode, celt_norm_t *X, int start, int end, int N, int nbShortMdcts, int mdct_weight_shift, int _C) |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 506 | { |
| 507 | int m, i, c; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 508 | const int C = CHANNELS(_C); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 509 | for (c=0;c<C;c++) |
| 510 | for (m=start;m<end;m++) |
| 511 | for (i=m+c*N;i<(c+1)*N;i+=nbShortMdcts) |
| 512 | #ifdef FIXED_POINT |
| 513 | X[i] = SHR16(X[i], mdct_weight_shift); |
| 514 | #else |
| 515 | X[i] = (1.f/(1<<mdct_weight_shift))*X[i]; |
| 516 | #endif |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 517 | renormalise_bands(mode, X, C); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 521 | #ifdef FIXED_POINT |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 522 | int celt_encode(CELTEncoder * restrict st, const celt_int16 * pcm, celt_int16 * optional_synthesis, unsigned char *compressed, int nbCompressedBytes) |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 523 | { |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 524 | #else |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 525 | int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_sig_t * optional_synthesis, unsigned char *compressed, int nbCompressedBytes) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 526 | { |
| 527 | #endif |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 528 | int i, c, N, NN, N4; |
Jean-Marc Valin | 7e8a487 | 2008-02-11 16:55:34 +1100 | [diff] [blame] | 529 | int has_pitch; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 530 | int pitch_index; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 531 | int bits; |
Jean-Marc Valin | bc79991 | 2008-11-07 22:53:13 -0500 | [diff] [blame] | 532 | int has_fold=1; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 533 | unsigned coarse_needed; |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 534 | ec_byte_buffer buf; |
| 535 | ec_enc enc; |
Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 536 | VARDECL(celt_sig_t, in); |
| 537 | VARDECL(celt_sig_t, freq); |
Jean-Marc Valin | ab220aa | 2009-09-15 22:38:40 -0400 | [diff] [blame] | 538 | VARDECL(celt_sig_t, pitch_freq); |
Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 539 | VARDECL(celt_norm_t, X); |
Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 540 | VARDECL(celt_ener_t, bandE); |
Jean-Marc Valin | 7a56741 | 2009-06-13 22:59:43 -0400 | [diff] [blame] | 541 | VARDECL(celt_word16_t, bandLogE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 542 | VARDECL(int, fine_quant); |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 543 | VARDECL(celt_word16_t, error); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 544 | VARDECL(int, pulses); |
| 545 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 546 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 18a3b79 | 2009-05-01 19:58:55 -0400 | [diff] [blame] | 547 | int intra_ener = 0; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 548 | int shortBlocks=0; |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 549 | int transient_time; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 550 | int transient_shift; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 551 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 552 | int mdct_weight_shift = 0; |
| 553 | int mdct_weight_pos=0; |
Jean-Marc Valin | ab220aa | 2009-09-15 22:38:40 -0400 | [diff] [blame] | 554 | int gain_id=0; |
Jean-Marc Valin | 92ae370 | 2009-09-16 07:57:17 -0400 | [diff] [blame] | 555 | int norm_rate; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 556 | SAVE_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 557 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 558 | if (check_encoder(st) != CELT_OK) |
| 559 | return CELT_INVALID_STATE; |
| 560 | |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 561 | if (check_mode(st->mode) != CELT_OK) |
| 562 | return CELT_INVALID_MODE; |
| 563 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 564 | if (nbCompressedBytes<0 || pcm==NULL) |
Gregory Maxwell | 520eeae | 2009-02-09 01:33:21 -0500 | [diff] [blame] | 565 | return CELT_BAD_ARG; |
| 566 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 567 | /* The memset is important for now in case the encoder doesn't |
| 568 | fill up all the bytes */ |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 569 | CELT_MEMSET(compressed, 0, nbCompressedBytes); |
| 570 | ec_byte_writeinit_buffer(&buf, compressed, nbCompressedBytes); |
| 571 | ec_enc_init(&enc,&buf); |
| 572 | |
Jean-Marc Valin | f02ba11 | 2007-11-30 01:10:42 +1100 | [diff] [blame] | 573 | N = st->block_size; |
Jean-Marc Valin | a536f77 | 2008-03-22 09:01:50 +1100 | [diff] [blame] | 574 | N4 = (N-st->overlap)>>1; |
Jean-Marc Valin | bdb5883 | 2008-04-20 17:42:10 +1000 | [diff] [blame] | 575 | ALLOC(in, 2*C*N-2*C*N4, celt_sig_t); |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 576 | |
Jean-Marc Valin | bdb5883 | 2008-04-20 17:42:10 +1000 | [diff] [blame] | 577 | CELT_COPY(in, st->in_mem, C*st->overlap); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 578 | for (c=0;c<C;c++) |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 579 | { |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 580 | const celt_word16_t * restrict pcmp = pcm+c; |
Jean-Marc Valin | 6229006 | 2008-04-20 22:16:02 +1000 | [diff] [blame] | 581 | celt_sig_t * restrict inp = in+C*st->overlap+c; |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 582 | for (i=0;i<N;i++) |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 583 | { |
Jean-Marc Valin | 6229006 | 2008-04-20 22:16:02 +1000 | [diff] [blame] | 584 | /* Apply pre-emphasis */ |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 585 | celt_sig_t tmp = SCALEIN(SHL32(EXTEND32(*pcmp), SIG_SHIFT)); |
Jean-Marc Valin | 5c3bc67 | 2008-08-28 23:34:24 -0400 | [diff] [blame] | 586 | *inp = SUB32(tmp, SHR32(MULT16_16(preemph,st->preemph_memE[c]),3)); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 587 | st->preemph_memE[c] = SCALEIN(*pcmp); |
Jean-Marc Valin | 6229006 | 2008-04-20 22:16:02 +1000 | [diff] [blame] | 588 | inp += C; |
| 589 | pcmp += C; |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 590 | } |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 591 | } |
Jean-Marc Valin | bdb5883 | 2008-04-20 17:42:10 +1000 | [diff] [blame] | 592 | 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] | 593 | |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 594 | /* Transient handling */ |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 595 | transient_time = -1; |
| 596 | transient_shift = 0; |
| 597 | shortBlocks = 0; |
| 598 | |
| 599 | if (st->mode->nbShortMdcts > 1 && transient_analysis(in, N+st->overlap, C, &transient_time, &transient_shift)) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 600 | { |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 601 | #ifndef FIXED_POINT |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 602 | float gain_1; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 603 | #endif |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 604 | /* Apply the inverse shaping window */ |
| 605 | if (transient_shift) |
| 606 | { |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 607 | #ifdef FIXED_POINT |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 608 | for (c=0;c<C;c++) |
| 609 | for (i=0;i<16;i++) |
| 610 | 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]); |
| 611 | for (c=0;c<C;c++) |
| 612 | for (i=transient_time;i<N+st->overlap;i++) |
| 613 | in[C*i+c] = SHR32(in[C*i+c], transient_shift); |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 614 | #else |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 615 | for (c=0;c<C;c++) |
| 616 | for (i=0;i<16;i++) |
| 617 | in[C*(transient_time+i-16)+c] /= 1+transientWindow[i]*((1<<transient_shift)-1); |
| 618 | gain_1 = 1./(1<<transient_shift); |
| 619 | for (c=0;c<C;c++) |
| 620 | for (i=transient_time;i<N+st->overlap;i++) |
| 621 | in[C*i+c] *= gain_1; |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 622 | #endif |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 623 | } |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 624 | shortBlocks = 1; |
| 625 | has_fold = 1; |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 626 | } |
Jean-Marc Valin | c5f2a9d | 2008-10-26 22:00:26 -0400 | [diff] [blame] | 627 | |
Jean-Marc Valin | 32ec58c | 2009-05-01 21:28:58 -0400 | [diff] [blame] | 628 | ALLOC(freq, C*N, celt_sig_t); /**< Interleaved signal MDCTs */ |
| 629 | ALLOC(bandE,st->mode->nbEBands*C, celt_ener_t); |
Jean-Marc Valin | 7a56741 | 2009-06-13 22:59:43 -0400 | [diff] [blame] | 630 | ALLOC(bandLogE,st->mode->nbEBands*C, celt_word16_t); |
Jean-Marc Valin | 32ec58c | 2009-05-01 21:28:58 -0400 | [diff] [blame] | 631 | /* Compute MDCTs */ |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 632 | compute_mdcts(st->mode, shortBlocks, in, freq, C); |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 633 | |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 634 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 635 | norm_rate = (nbCompressedBytes-5)*8*(celt_uint32)st->mode->Fs/(C*N)>>10; |
Jean-Marc Valin | ddb181b | 2008-03-03 14:53:47 +1100 | [diff] [blame] | 636 | /* Pitch analysis: we do it early to save on the peak stack space */ |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 637 | /* Don't use pitch if there isn't enough data available yet, |
| 638 | or if we're using shortBlocks */ |
Jean-Marc Valin | e6d832a | 2009-07-08 22:21:31 -0400 | [diff] [blame] | 639 | has_pitch = st->pitch_enabled && st->pitch_permitted && (N <= 512) |
Jean-Marc Valin | 92ae370 | 2009-09-16 07:57:17 -0400 | [diff] [blame] | 640 | && (st->pitch_available >= MAX_PERIOD) && (!shortBlocks) |
| 641 | && norm_rate < 50; |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 642 | if (has_pitch) |
Jean-Marc Valin | c5f2a9d | 2008-10-26 22:00:26 -0400 | [diff] [blame] | 643 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 644 | find_spectral_pitch(st->mode, st->mode->fft, &st->mode->psy, in, st->out_mem, st->mode->window, NULL, 2*N-2*N4, MAX_PERIOD-(2*N-2*N4), &pitch_index, C); |
Jean-Marc Valin | c5f2a9d | 2008-10-26 22:00:26 -0400 | [diff] [blame] | 645 | } |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 646 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 647 | /* Deferred allocation after find_spectral_pitch() to reduce |
| 648 | the peak memory usage */ |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 649 | ALLOC(X, C*N, celt_norm_t); /**< Interleaved normalised MDCTs */ |
Jean-Marc Valin | 8d4ac15 | 2008-02-29 17:24:02 +1100 | [diff] [blame] | 650 | |
Jean-Marc Valin | ab220aa | 2009-09-15 22:38:40 -0400 | [diff] [blame] | 651 | ALLOC(pitch_freq, C*N, celt_sig_t); /**< Interleaved signal MDCTs */ |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 652 | if (has_pitch) |
| 653 | { |
Jean-Marc Valin | 92ae370 | 2009-09-16 07:57:17 -0400 | [diff] [blame] | 654 | |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 655 | compute_mdcts(st->mode, 0, st->out_mem+pitch_index*C, pitch_freq, C); |
Jean-Marc Valin | d35d677 | 2009-10-16 23:16:47 -0400 | [diff] [blame] | 656 | has_pitch = compute_pitch_gain(st->mode, freq, pitch_freq, norm_rate, &gain_id, C, &st->gain_prod); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | if (has_pitch) |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 660 | apply_pitch(st->mode, freq, pitch_freq, gain_id, 1, C); |
Jean-Marc Valin | 6c24524 | 2007-12-29 23:27:42 +1100 | [diff] [blame] | 661 | |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 662 | compute_band_energies(st->mode, freq, bandE, C); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 663 | for (i=0;i<st->mode->nbEBands*C;i++) |
| 664 | bandLogE[i] = amp2Log(bandE[i]); |
| 665 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 666 | /* Band normalisation */ |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 667 | normalise_bands(st->mode, freq, X, bandE, C); |
| 668 | if (!shortBlocks && !folding_decision(st->mode, X, &st->tonal_average, &st->fold_decision, C)) |
Jean-Marc Valin | 527db5c | 2009-06-02 07:56:19 -0400 | [diff] [blame] | 669 | has_fold = 0; |
Jean-Marc Valin | 26c9e14 | 2007-12-31 21:27:54 +1100 | [diff] [blame] | 670 | |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 671 | /* Don't use intra energy when we're operating at low bit-rate */ |
| 672 | intra_ener = st->force_intra || (!has_pitch && st->delayedIntra && nbCompressedBytes > st->mode->nbEBands); |
| 673 | if (shortBlocks || intra_decision(bandLogE, st->oldBandE, st->mode->nbEBands)) |
| 674 | st->delayedIntra = 1; |
| 675 | else |
| 676 | st->delayedIntra = 0; |
| 677 | |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 678 | NN = st->mode->eBands[st->mode->nbEBands]; |
| 679 | if (shortBlocks && !transient_shift) |
| 680 | { |
| 681 | celt_word32_t sum[8]={1,1,1,1,1,1,1,1}; |
| 682 | int m; |
| 683 | for (c=0;c<C;c++) |
| 684 | { |
| 685 | m=0; |
| 686 | do { |
| 687 | celt_word32_t tmp=0; |
| 688 | for (i=m+c*N;i<c*N+NN;i+=st->mode->nbShortMdcts) |
| 689 | tmp += ABS32(X[i]); |
| 690 | sum[m++] += tmp; |
| 691 | } while (m<st->mode->nbShortMdcts); |
| 692 | } |
| 693 | m=0; |
| 694 | #ifdef FIXED_POINT |
| 695 | do { |
| 696 | if (SHR32(sum[m+1],3) > sum[m]) |
| 697 | { |
| 698 | mdct_weight_shift=2; |
| 699 | mdct_weight_pos = m; |
| 700 | } else if (SHR32(sum[m+1],1) > sum[m] && mdct_weight_shift < 2) |
| 701 | { |
| 702 | mdct_weight_shift=1; |
| 703 | mdct_weight_pos = m; |
| 704 | } |
| 705 | m++; |
| 706 | } while (m<st->mode->nbShortMdcts-1); |
| 707 | #else |
| 708 | do { |
| 709 | if (sum[m+1] > 8*sum[m]) |
| 710 | { |
| 711 | mdct_weight_shift=2; |
| 712 | mdct_weight_pos = m; |
| 713 | } else if (sum[m+1] > 2*sum[m] && mdct_weight_shift < 2) |
| 714 | { |
| 715 | mdct_weight_shift=1; |
| 716 | mdct_weight_pos = m; |
| 717 | } |
| 718 | m++; |
| 719 | } while (m<st->mode->nbShortMdcts-1); |
| 720 | #endif |
| 721 | if (mdct_weight_shift) |
| 722 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 723 | mdct_shape(st->mode, X, mdct_weight_pos+1, st->mode->nbShortMdcts, N, st->mode->nbShortMdcts, mdct_weight_shift, C); |
| 724 | renormalise_bands(st->mode, X, C); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 728 | |
Jean-Marc Valin | 18a3b79 | 2009-05-01 19:58:55 -0400 | [diff] [blame] | 729 | encode_flags(&enc, intra_ener, has_pitch, shortBlocks, has_fold); |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 730 | if (has_pitch) |
| 731 | { |
Gregory Maxwell | abe40f0 | 2008-12-13 01:31:32 -0500 | [diff] [blame] | 732 | ec_enc_uint(&enc, pitch_index, MAX_PERIOD-(2*N-2*N4)); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 733 | ec_enc_uint(&enc, gain_id, 16); |
Jean-Marc Valin | 96870d9 | 2007-12-05 21:14:22 +1100 | [diff] [blame] | 734 | } |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 735 | if (shortBlocks) |
| 736 | { |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 737 | if (transient_shift) |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 738 | { |
Jean-Marc Valin | e610864 | 2009-08-01 23:05:47 +0200 | [diff] [blame] | 739 | ec_enc_uint(&enc, transient_shift, 4); |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 740 | ec_enc_uint(&enc, transient_time, N+st->overlap); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 741 | } else { |
Jean-Marc Valin | e610864 | 2009-08-01 23:05:47 +0200 | [diff] [blame] | 742 | ec_enc_uint(&enc, mdct_weight_shift, 4); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 743 | if (mdct_weight_shift && st->mode->nbShortMdcts!=2) |
| 744 | ec_enc_uint(&enc, mdct_weight_pos, st->mode->nbShortMdcts-1); |
| 745 | } |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 746 | } |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 747 | |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 748 | ALLOC(fine_quant, st->mode->nbEBands, int); |
Jean-Marc Valin | 8dff923 | 2008-08-13 22:07:20 -0400 | [diff] [blame] | 749 | ALLOC(pulses, st->mode->nbEBands, int); |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 750 | |
| 751 | /* Bit allocation */ |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 752 | ALLOC(error, C*st->mode->nbEBands, celt_word16_t); |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 753 | coarse_needed = quant_coarse_energy(st->mode, bandLogE, st->oldBandE, nbCompressedBytes*8/3, intra_ener, st->mode->prob, error, &enc, C); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 754 | coarse_needed = ((coarse_needed*3-1)>>3)+1; |
| 755 | |
| 756 | /* Variable bitrate */ |
| 757 | if (st->VBR_rate>0) |
| 758 | { |
| 759 | /* The target rate in 16th bits per frame */ |
| 760 | int target=st->VBR_rate; |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 761 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 762 | /* Shortblocks get a large boost in bitrate, but since they |
| 763 | are uncommon long blocks are not greatly effected */ |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 764 | if (shortBlocks) |
| 765 | target*=2; |
| 766 | else if (st->mode->nbShortMdcts > 1) |
| 767 | target-=(target+14)/28; |
| 768 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 769 | /* The average energy is removed from the target and the actual |
| 770 | energy added*/ |
Jean-Marc Valin | 91f07dc | 2009-07-25 20:42:24 -0400 | [diff] [blame] | 771 | target=target-588+ec_enc_tell(&enc, BITRES); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 772 | |
| 773 | /* In VBR mode the frame size must not be reduced so much that it would result in the coarse energy busting its budget */ |
| 774 | target=IMAX(coarse_needed,(target+64)/128); |
| 775 | nbCompressedBytes=IMIN(nbCompressedBytes,target); |
Jean-Marc Valin | e610864 | 2009-08-01 23:05:47 +0200 | [diff] [blame] | 776 | ec_byte_shrink(&buf, nbCompressedBytes); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 777 | } |
| 778 | |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 779 | ALLOC(offsets, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 780 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 781 | |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 782 | for (i=0;i<st->mode->nbEBands;i++) |
| 783 | offsets[i] = 0; |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 784 | bits = nbCompressedBytes*8 - ec_enc_tell(&enc, 0) - 1; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 785 | compute_allocation(st->mode, offsets, bits, pulses, fine_quant, fine_priority, C); |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 786 | |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 787 | quant_fine_energy(st->mode, bandE, st->oldBandE, error, fine_quant, &enc, C); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 788 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 789 | /* Residual quantisation */ |
Jean-Marc Valin | ca53b7c | 2009-03-26 20:23:14 -0400 | [diff] [blame] | 790 | if (C==1) |
Jean-Marc Valin | a9947c4 | 2009-10-08 23:10:45 -0400 | [diff] [blame] | 791 | quant_bands(st->mode, X, bandE, pulses, shortBlocks, has_fold, nbCompressedBytes*8, 1, &enc); |
Jean-Marc Valin | 7353203 | 2009-06-02 20:07:25 -0400 | [diff] [blame] | 792 | #ifndef DISABLE_STEREO |
Jean-Marc Valin | ca53b7c | 2009-03-26 20:23:14 -0400 | [diff] [blame] | 793 | else |
Jean-Marc Valin | d5e5436 | 2009-09-30 20:50:41 -0400 | [diff] [blame] | 794 | quant_bands_stereo(st->mode, X, bandE, pulses, shortBlocks, has_fold, nbCompressedBytes*8, &enc); |
Jean-Marc Valin | 7353203 | 2009-06-02 20:07:25 -0400 | [diff] [blame] | 795 | #endif |
Jean-Marc Valin | 3971053 | 2009-06-09 00:10:32 -0400 | [diff] [blame] | 796 | |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 797 | quant_energy_finalise(st->mode, 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] | 798 | |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 799 | /* Re-synthesis of the coded audio if required */ |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 800 | if (st->pitch_available>0 || optional_synthesis!=NULL) |
Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 801 | { |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 802 | if (st->pitch_available>0 && st->pitch_available<MAX_PERIOD) |
| 803 | st->pitch_available+=st->frame_size; |
| 804 | |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 805 | if (mdct_weight_shift) |
| 806 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 807 | mdct_shape(st->mode, X, 0, mdct_weight_pos+1, N, st->mode->nbShortMdcts, mdct_weight_shift, C); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 808 | } |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 809 | |
| 810 | /* Synthesis */ |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 811 | denormalise_bands(st->mode, X, freq, bandE, C); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 812 | |
| 813 | CELT_MOVE(st->out_mem, st->out_mem+C*N, C*(MAX_PERIOD+st->overlap-N)); |
| 814 | |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 815 | if (has_pitch) |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 816 | apply_pitch(st->mode, freq, pitch_freq, gain_id, 0, C); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 817 | |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 818 | compute_inv_mdcts(st->mode, shortBlocks, freq, transient_time, transient_shift, st->out_mem, C); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 819 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 820 | /* De-emphasis and put everything back at the right place |
| 821 | in the synthesis history */ |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 822 | if (optional_synthesis != NULL) { |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 823 | deemphasis(st->out_mem, optional_synthesis, N, C, preemph, st->preemph_memD); |
| 824 | |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 825 | } |
Jean-Marc Valin | d9b9565 | 2008-08-31 23:34:47 -0400 | [diff] [blame] | 826 | } |
Gregory Maxwell | 54547f1 | 2009-02-16 18:56:44 -0500 | [diff] [blame] | 827 | |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 828 | ec_enc_done(&enc); |
Jean-Marc Valin | c871c8d | 2009-06-09 00:57:00 -0400 | [diff] [blame] | 829 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 830 | RESTORE_STACK; |
Jean-Marc Valin | a76a0b2 | 2008-01-17 22:43:05 +1100 | [diff] [blame] | 831 | return nbCompressedBytes; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 832 | } |
| 833 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 834 | #ifdef FIXED_POINT |
| 835 | #ifndef DISABLE_FLOAT_API |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 836 | int celt_encode_float(CELTEncoder * restrict st, const float * pcm, float * optional_synthesis, unsigned char *compressed, int nbCompressedBytes) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 837 | { |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 838 | int j, ret, C, N; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 839 | VARDECL(celt_int16, in); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 840 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 841 | |
| 842 | if (check_encoder(st) != CELT_OK) |
| 843 | return CELT_INVALID_STATE; |
| 844 | |
| 845 | if (check_mode(st->mode) != CELT_OK) |
| 846 | return CELT_INVALID_MODE; |
| 847 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 848 | if (pcm==NULL) |
| 849 | return CELT_BAD_ARG; |
| 850 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 851 | C = CHANNELS(st->channels); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 852 | N = st->block_size; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 853 | ALLOC(in, C*N, celt_int16); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 854 | |
| 855 | for (j=0;j<C*N;j++) |
| 856 | in[j] = FLOAT2INT16(pcm[j]); |
| 857 | |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 858 | if (optional_synthesis != NULL) { |
| 859 | ret=celt_encode(st,in,in,compressed,nbCompressedBytes); |
Gregory Maxwell | b0a73a0 | 2008-12-12 16:54:25 -0500 | [diff] [blame] | 860 | for (j=0;j<C*N;j++) |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 861 | optional_synthesis[j]=in[j]*(1/32768.); |
| 862 | } else { |
| 863 | ret=celt_encode(st,in,NULL,compressed,nbCompressedBytes); |
| 864 | } |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 865 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 866 | return ret; |
| 867 | |
| 868 | } |
| 869 | #endif /*DISABLE_FLOAT_API*/ |
| 870 | #else |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 871 | int celt_encode(CELTEncoder * restrict st, const celt_int16 * pcm, celt_int16 * optional_synthesis, unsigned char *compressed, int nbCompressedBytes) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 872 | { |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 873 | int j, ret, C, N; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 874 | VARDECL(celt_sig_t, in); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 875 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 876 | |
| 877 | if (check_encoder(st) != CELT_OK) |
| 878 | return CELT_INVALID_STATE; |
| 879 | |
| 880 | if (check_mode(st->mode) != CELT_OK) |
| 881 | return CELT_INVALID_MODE; |
| 882 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 883 | if (pcm==NULL) |
| 884 | return CELT_BAD_ARG; |
| 885 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 886 | C=CHANNELS(st->channels); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 887 | N=st->block_size; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 888 | ALLOC(in, C*N, celt_sig_t); |
| 889 | for (j=0;j<C*N;j++) { |
| 890 | in[j] = SCALEOUT(pcm[j]); |
| 891 | } |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 892 | |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 893 | if (optional_synthesis != NULL) { |
| 894 | ret = celt_encode_float(st,in,in,compressed,nbCompressedBytes); |
| 895 | for (j=0;j<C*N;j++) |
| 896 | optional_synthesis[j] = FLOAT2INT16(in[j]); |
| 897 | } else { |
| 898 | ret = celt_encode_float(st,in,NULL,compressed,nbCompressedBytes); |
| 899 | } |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 900 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 901 | return ret; |
| 902 | } |
| 903 | #endif |
Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 904 | |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 905 | int celt_encoder_ctl(CELTEncoder * restrict st, int request, ...) |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 906 | { |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 907 | va_list ap; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 908 | |
| 909 | if (check_encoder(st) != CELT_OK) |
| 910 | return CELT_INVALID_STATE; |
| 911 | |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 912 | va_start(ap, request); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 913 | if ((request!=CELT_GET_MODE_REQUEST) && (check_mode(st->mode) != CELT_OK)) |
| 914 | goto bad_mode; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 915 | switch (request) |
| 916 | { |
Gregory Maxwell | f3b44ef | 2009-06-03 13:37:45 -0400 | [diff] [blame] | 917 | case CELT_GET_MODE_REQUEST: |
| 918 | { |
| 919 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 920 | if (value==0) |
| 921 | goto bad_arg; |
| 922 | *value=st->mode; |
| 923 | } |
| 924 | break; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 925 | case CELT_SET_COMPLEXITY_REQUEST: |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 926 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 927 | int value = va_arg(ap, celt_int32); |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 928 | if (value<0 || value>10) |
| 929 | goto bad_arg; |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 930 | if (value<=2) { |
| 931 | st->pitch_enabled = 0; |
| 932 | st->pitch_available = 0; |
| 933 | } else { |
| 934 | st->pitch_enabled = 1; |
| 935 | if (st->pitch_available<1) |
| 936 | st->pitch_available = 1; |
| 937 | } |
| 938 | } |
| 939 | break; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 940 | case CELT_SET_PREDICTION_REQUEST: |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 941 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 942 | int value = va_arg(ap, celt_int32); |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 943 | if (value<0 || value>2) |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 944 | goto bad_arg; |
| 945 | if (value==0) |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 946 | { |
| 947 | st->force_intra = 1; |
| 948 | st->pitch_permitted = 0; |
Gregory Maxwell | a80958b | 2009-06-29 12:48:57 -0400 | [diff] [blame] | 949 | } else if (value==1) { |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 950 | st->force_intra = 0; |
| 951 | st->pitch_permitted = 0; |
| 952 | } else { |
| 953 | st->force_intra = 0; |
| 954 | st->pitch_permitted = 1; |
| 955 | } |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 956 | } |
| 957 | break; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 958 | case CELT_SET_VBR_RATE_REQUEST: |
| 959 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 960 | celt_int32 value = va_arg(ap, celt_int32); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 961 | if (value<0) |
| 962 | goto bad_arg; |
| 963 | if (value>3072000) |
| 964 | value = 3072000; |
| 965 | st->VBR_rate = ((st->mode->Fs<<3)+(st->block_size>>1))/st->block_size; |
| 966 | st->VBR_rate = ((value<<7)+(st->VBR_rate>>1))/st->VBR_rate; |
| 967 | } |
| 968 | break; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 969 | case CELT_RESET_STATE: |
| 970 | { |
| 971 | const CELTMode *mode = st->mode; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 972 | int C = st->channels; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 973 | |
| 974 | if (st->pitch_available > 0) st->pitch_available = 1; |
| 975 | |
| 976 | CELT_MEMSET(st->in_mem, 0, st->overlap*C); |
| 977 | CELT_MEMSET(st->out_mem, 0, (MAX_PERIOD+st->overlap)*C); |
| 978 | |
| 979 | CELT_MEMSET(st->oldBandE, 0, C*mode->nbEBands); |
| 980 | |
| 981 | CELT_MEMSET(st->preemph_memE, 0, C); |
| 982 | CELT_MEMSET(st->preemph_memD, 0, C); |
| 983 | st->delayedIntra = 1; |
| 984 | } |
| 985 | break; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 986 | default: |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 987 | goto bad_request; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 988 | } |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 989 | va_end(ap); |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 990 | return CELT_OK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 991 | bad_mode: |
| 992 | va_end(ap); |
| 993 | return CELT_INVALID_MODE; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 994 | bad_arg: |
| 995 | va_end(ap); |
| 996 | return CELT_BAD_ARG; |
| 997 | bad_request: |
| 998 | va_end(ap); |
| 999 | return CELT_UNIMPLEMENTED; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1000 | } |
| 1001 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 1002 | /**********************************************************************/ |
| 1003 | /* */ |
| 1004 | /* DECODER */ |
| 1005 | /* */ |
| 1006 | /**********************************************************************/ |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1007 | #ifdef NEW_PLC |
| 1008 | #define DECODE_BUFFER_SIZE 2048 |
| 1009 | #else |
| 1010 | #define DECODE_BUFFER_SIZE MAX_PERIOD |
| 1011 | #endif |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1012 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1013 | #define DECODERVALID 0x4c434454 |
| 1014 | #define DECODERPARTIAL 0x5444434c |
| 1015 | #define DECODERFREED 0x4c004400 |
| 1016 | |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 1017 | /** Decoder state |
| 1018 | @brief Decoder state |
| 1019 | */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1020 | struct CELTDecoder { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1021 | celt_uint32 marker; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1022 | const CELTMode *mode; |
| 1023 | int frame_size; |
| 1024 | int block_size; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1025 | int overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1026 | int channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1027 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1028 | ec_byte_buffer buf; |
| 1029 | ec_enc enc; |
| 1030 | |
Jean-Marc Valin | 65fb92e | 2008-04-19 22:24:53 +1000 | [diff] [blame] | 1031 | celt_sig_t * restrict preemph_memD; |
Jean-Marc Valin | 81b38c2 | 2008-02-29 21:08:49 +1100 | [diff] [blame] | 1032 | |
Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 1033 | celt_sig_t *out_mem; |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1034 | celt_sig_t *decode_mem; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1035 | |
Jean-Marc Valin | 5d56183 | 2008-02-27 11:59:05 +1100 | [diff] [blame] | 1036 | celt_word16_t *oldBandE; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1037 | |
| 1038 | int last_pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1039 | int loss_count; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1040 | }; |
| 1041 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1042 | int check_decoder(const CELTDecoder *st) |
| 1043 | { |
| 1044 | if (st==NULL) |
| 1045 | { |
| 1046 | celt_warning("NULL passed a decoder structure"); |
| 1047 | return CELT_INVALID_STATE; |
| 1048 | } |
| 1049 | if (st->marker == DECODERVALID) |
| 1050 | return CELT_OK; |
| 1051 | if (st->marker == DECODERFREED) |
| 1052 | celt_warning("Referencing a decoder that has already been freed"); |
| 1053 | else |
| 1054 | celt_warning("This is not a valid CELT decoder structure"); |
| 1055 | return CELT_INVALID_STATE; |
| 1056 | } |
| 1057 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1058 | CELTDecoder *celt_decoder_create(const CELTMode *mode, int channels, int *error) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1059 | { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 1060 | int N, C; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1061 | CELTDecoder *st; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1062 | |
| 1063 | if (check_mode(mode) != CELT_OK) |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1064 | { |
| 1065 | if (error) |
| 1066 | *error = CELT_INVALID_MODE; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1067 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1068 | } |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1069 | if (channels < 0 || channels > 2) |
| 1070 | { |
| 1071 | celt_warning("Only mono and stereo supported"); |
| 1072 | if (error) |
| 1073 | *error = CELT_BAD_ARG; |
| 1074 | return NULL; |
| 1075 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1076 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1077 | N = mode->mdctSize; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1078 | C = CHANNELS(channels); |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1079 | st = celt_alloc(sizeof(CELTDecoder)); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1080 | |
| 1081 | if (st==NULL) |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1082 | { |
| 1083 | if (error) |
| 1084 | *error = CELT_ALLOC_FAIL; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1085 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1086 | } |
| 1087 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1088 | st->marker = DECODERPARTIAL; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1089 | st->mode = mode; |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 1090 | st->frame_size = N; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1091 | st->block_size = N; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1092 | st->overlap = mode->overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1093 | st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1094 | |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1095 | st->decode_mem = celt_alloc((DECODE_BUFFER_SIZE+st->overlap)*C*sizeof(celt_sig_t)); |
| 1096 | st->out_mem = st->decode_mem+DECODE_BUFFER_SIZE-MAX_PERIOD; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1097 | |
Jean-Marc Valin | 5d56183 | 2008-02-27 11:59:05 +1100 | [diff] [blame] | 1098 | st->oldBandE = (celt_word16_t*)celt_alloc(C*mode->nbEBands*sizeof(celt_word16_t)); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1099 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1100 | st->preemph_memD = (celt_sig_t*)celt_alloc(C*sizeof(celt_sig_t)); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 1101 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1102 | st->loss_count = 0; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1103 | |
| 1104 | if ((st->decode_mem!=NULL) && (st->out_mem!=NULL) && (st->oldBandE!=NULL) && |
| 1105 | (st->preemph_memD!=NULL)) |
| 1106 | { |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1107 | if (error) |
| 1108 | *error = CELT_OK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1109 | st->marker = DECODERVALID; |
| 1110 | return st; |
| 1111 | } |
| 1112 | /* If the setup fails for some reason deallocate it. */ |
| 1113 | celt_decoder_destroy(st); |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1114 | if (error) |
| 1115 | *error = CELT_ALLOC_FAIL; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1116 | return NULL; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1117 | } |
| 1118 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 1119 | void celt_decoder_destroy(CELTDecoder *st) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1120 | { |
| 1121 | if (st == NULL) |
| 1122 | { |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1123 | celt_warning("NULL passed to celt_decoder_destroy"); |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1124 | return; |
| 1125 | } |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1126 | |
| 1127 | if (st->marker == DECODERFREED) |
| 1128 | { |
| 1129 | celt_warning("Freeing a decoder which has already been freed"); |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1130 | return; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | if (st->marker != DECODERVALID && st->marker != DECODERPARTIAL) |
| 1134 | { |
| 1135 | celt_warning("This is not a valid CELT decoder structure"); |
| 1136 | return; |
| 1137 | } |
| 1138 | |
| 1139 | /*Check_mode is non-fatal here because we can still free |
| 1140 | the encoder memory even if the mode is bad, although calling |
| 1141 | the free functions in this order is a violation of the API.*/ |
| 1142 | check_mode(st->mode); |
| 1143 | |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1144 | celt_free(st->decode_mem); |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1145 | celt_free(st->oldBandE); |
Jean-Marc Valin | 7351e28 | 2008-02-08 15:09:45 +1100 | [diff] [blame] | 1146 | celt_free(st->preemph_memD); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1147 | |
| 1148 | st->marker = DECODERFREED; |
| 1149 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1150 | celt_free(st); |
| 1151 | } |
| 1152 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 1153 | /** Handles lost packets by just copying past data with the same |
| 1154 | offset as the last |
Jean-Marc Valin | d501f61 | 2008-02-21 12:16:57 +1100 | [diff] [blame] | 1155 | pitch period */ |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1156 | #ifdef NEW_PLC |
| 1157 | #include "plc.c" |
| 1158 | #else |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1159 | static void celt_decode_lost(CELTDecoder * restrict st, celt_word16_t * restrict pcm) |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1160 | { |
Jean-Marc Valin | 05e56c4 | 2008-04-10 09:13:05 +1000 | [diff] [blame] | 1161 | int c, N; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1162 | int pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1163 | celt_word16_t fade = Q15ONE; |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1164 | int i, len; |
Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 1165 | VARDECL(celt_sig_t, freq); |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1166 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1167 | int offset; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1168 | SAVE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1169 | N = st->block_size; |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 1170 | ALLOC(freq,C*N, celt_sig_t); /**< Interleaved signal MDCTs */ |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1171 | |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1172 | len = N+st->mode->overlap; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1173 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1174 | if (st->loss_count == 0) |
| 1175 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1176 | find_spectral_pitch(st->mode, st->mode->fft, &st->mode->psy, st->out_mem+MAX_PERIOD-len, st->out_mem, st->mode->window, NULL, len, MAX_PERIOD-len-100, &pitch_index, C); |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1177 | pitch_index = MAX_PERIOD-len-pitch_index; |
| 1178 | st->last_pitch_index = pitch_index; |
| 1179 | } else { |
| 1180 | pitch_index = st->last_pitch_index; |
| 1181 | if (st->loss_count < 5) |
| 1182 | fade = QCONST16(.8f,15); |
| 1183 | else |
| 1184 | fade = 0; |
| 1185 | } |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1186 | |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1187 | offset = MAX_PERIOD-pitch_index; |
| 1188 | while (offset+len >= MAX_PERIOD) |
| 1189 | offset -= pitch_index; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1190 | compute_mdcts(st->mode, 0, st->out_mem+offset*C, freq, C); |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 1191 | for (i=0;i<C*N;i++) |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1192 | freq[i] = ADD32(VERY_SMALL, MULT16_32_Q15(fade,freq[i])); |
| 1193 | |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1194 | CELT_MOVE(st->out_mem, st->out_mem+C*N, C*(MAX_PERIOD+st->mode->overlap-N)); |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1195 | /* Compute inverse MDCTs */ |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1196 | compute_inv_mdcts(st->mode, 0, freq, -1, 0, st->out_mem, C); |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 1197 | |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 1198 | for (c=0;c<C;c++) |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1199 | { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 1200 | int j; |
| 1201 | for (j=0;j<N;j++) |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1202 | { |
Jean-Marc Valin | 1dab60c | 2008-09-16 13:29:37 -0400 | [diff] [blame] | 1203 | celt_sig_t tmp = MAC16_32_Q15(st->out_mem[C*(MAX_PERIOD-N)+C*j+c], |
| 1204 | preemph,st->preemph_memD[c]); |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 1205 | st->preemph_memD[c] = tmp; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1206 | pcm[C*j+c] = SCALEOUT(SIG2WORD16(tmp)); |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1207 | } |
| 1208 | } |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1209 | |
| 1210 | st->loss_count++; |
| 1211 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1212 | RESTORE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1213 | } |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1214 | #endif |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1215 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1216 | #ifdef FIXED_POINT |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1217 | int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1218 | { |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1219 | #else |
Jean-Marc Valin | 6db6cbd | 2009-04-11 22:01:20 -0400 | [diff] [blame] | 1220 | int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, celt_sig_t * restrict pcm) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1221 | { |
| 1222 | #endif |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1223 | int i, N, N4; |
Jean-Marc Valin | c994394 | 2008-08-30 00:55:07 -0400 | [diff] [blame] | 1224 | int has_pitch, has_fold; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1225 | int pitch_index; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 1226 | int bits; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1227 | ec_dec dec; |
| 1228 | ec_byte_buffer buf; |
Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 1229 | VARDECL(celt_sig_t, freq); |
Jean-Marc Valin | ab220aa | 2009-09-15 22:38:40 -0400 | [diff] [blame] | 1230 | VARDECL(celt_sig_t, pitch_freq); |
Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 1231 | VARDECL(celt_norm_t, X); |
Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 1232 | VARDECL(celt_ener_t, bandE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1233 | VARDECL(int, fine_quant); |
| 1234 | VARDECL(int, pulses); |
| 1235 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1236 | VARDECL(int, fine_priority); |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 1237 | |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 1238 | int shortBlocks; |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 1239 | int intra_ener; |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 1240 | int transient_time; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 1241 | int transient_shift; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1242 | int mdct_weight_shift=0; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1243 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1244 | int mdct_weight_pos=0; |
Jean-Marc Valin | ab220aa | 2009-09-15 22:38:40 -0400 | [diff] [blame] | 1245 | int gain_id=0; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1246 | SAVE_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1247 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1248 | if (check_decoder(st) != CELT_OK) |
| 1249 | return CELT_INVALID_STATE; |
| 1250 | |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1251 | if (check_mode(st->mode) != CELT_OK) |
| 1252 | return CELT_INVALID_MODE; |
| 1253 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1254 | if (pcm==NULL) |
| 1255 | return CELT_BAD_ARG; |
| 1256 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1257 | N = st->block_size; |
Jean-Marc Valin | a536f77 | 2008-03-22 09:01:50 +1100 | [diff] [blame] | 1258 | N4 = (N-st->overlap)>>1; |
Jean-Marc Valin | 0141723 | 2008-03-03 13:59:55 +1100 | [diff] [blame] | 1259 | |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 1260 | ALLOC(freq, C*N, celt_sig_t); /**< Interleaved signal MDCTs */ |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 1261 | ALLOC(X, C*N, celt_norm_t); /**< Interleaved normalised MDCTs */ |
Jean-Marc Valin | e901fe3 | 2008-02-26 14:46:26 +1100 | [diff] [blame] | 1262 | ALLOC(bandE, st->mode->nbEBands*C, celt_ener_t); |
Jean-Marc Valin | 70c8ffd | 2007-12-07 14:20:01 +1100 | [diff] [blame] | 1263 | |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1264 | if (data == NULL) |
| 1265 | { |
| 1266 | celt_decode_lost(st, pcm); |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1267 | RESTORE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1268 | return 0; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1269 | } else { |
| 1270 | st->loss_count = 0; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1271 | } |
Gregory Maxwell | 520eeae | 2009-02-09 01:33:21 -0500 | [diff] [blame] | 1272 | if (len<0) { |
| 1273 | RESTORE_STACK; |
| 1274 | return CELT_BAD_ARG; |
| 1275 | } |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1276 | |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 1277 | ec_byte_readinit(&buf,(unsigned char*)data,len); |
Jean-Marc Valin | 70c8ffd | 2007-12-07 14:20:01 +1100 | [diff] [blame] | 1278 | ec_dec_init(&dec,&buf); |
| 1279 | |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 1280 | decode_flags(&dec, &intra_ener, &has_pitch, &shortBlocks, &has_fold); |
Gregory Maxwell | 0527f37 | 2008-09-23 19:28:35 -0400 | [diff] [blame] | 1281 | if (shortBlocks) |
| 1282 | { |
Jean-Marc Valin | e610864 | 2009-08-01 23:05:47 +0200 | [diff] [blame] | 1283 | transient_shift = ec_dec_uint(&dec, 4); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1284 | if (transient_shift == 3) |
| 1285 | { |
Gregory Maxwell | 0527f37 | 2008-09-23 19:28:35 -0400 | [diff] [blame] | 1286 | transient_time = ec_dec_uint(&dec, N+st->mode->overlap); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1287 | } else { |
| 1288 | mdct_weight_shift = transient_shift; |
| 1289 | if (mdct_weight_shift && st->mode->nbShortMdcts>2) |
| 1290 | mdct_weight_pos = ec_dec_uint(&dec, st->mode->nbShortMdcts-1); |
| 1291 | transient_shift = 0; |
Gregory Maxwell | 0527f37 | 2008-09-23 19:28:35 -0400 | [diff] [blame] | 1292 | transient_time = 0; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1293 | } |
Gregory Maxwell | 0527f37 | 2008-09-23 19:28:35 -0400 | [diff] [blame] | 1294 | } else { |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 1295 | transient_time = -1; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 1296 | transient_shift = 0; |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 1297 | } |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 1298 | |
Jean-Marc Valin | 7e8a487 | 2008-02-11 16:55:34 +1100 | [diff] [blame] | 1299 | if (has_pitch) |
| 1300 | { |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 1301 | pitch_index = ec_dec_uint(&dec, MAX_PERIOD-(2*N-2*N4)); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 1302 | gain_id = ec_dec_uint(&dec, 16); |
Jean-Marc Valin | 7e8a487 | 2008-02-11 16:55:34 +1100 | [diff] [blame] | 1303 | } else { |
Jean-Marc Valin | 7e8a487 | 2008-02-11 16:55:34 +1100 | [diff] [blame] | 1304 | pitch_index = 0; |
| 1305 | } |
Jean-Marc Valin | 827f931 | 2008-05-06 23:21:55 +1000 | [diff] [blame] | 1306 | |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1307 | ALLOC(fine_quant, st->mode->nbEBands, int); |
Jean-Marc Valin | 827f931 | 2008-05-06 23:21:55 +1000 | [diff] [blame] | 1308 | /* Get band energies */ |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1309 | unquant_coarse_energy(st->mode, bandE, st->oldBandE, len*8/3, intra_ener, st->mode->prob, &dec, C); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1310 | |
| 1311 | ALLOC(pulses, st->mode->nbEBands, int); |
| 1312 | ALLOC(offsets, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1313 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1314 | |
| 1315 | for (i=0;i<st->mode->nbEBands;i++) |
| 1316 | offsets[i] = 0; |
| 1317 | |
| 1318 | bits = len*8 - ec_dec_tell(&dec, 0) - 1; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1319 | compute_allocation(st->mode, offsets, bits, pulses, fine_quant, fine_priority, C); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1320 | /*bits = ec_dec_tell(&dec, 0); |
| 1321 | compute_fine_allocation(st->mode, fine_quant, (20*C+len*8/5-(ec_dec_tell(&dec, 0)-bits))/C);*/ |
| 1322 | |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1323 | unquant_fine_energy(st->mode, bandE, st->oldBandE, fine_quant, &dec, C); |
Jean-Marc Valin | 827f931 | 2008-05-06 23:21:55 +1000 | [diff] [blame] | 1324 | |
Jean-Marc Valin | ab220aa | 2009-09-15 22:38:40 -0400 | [diff] [blame] | 1325 | ALLOC(pitch_freq, C*N, celt_sig_t); /**< Interleaved signal MDCTs */ |
Jean-Marc Valin | c994394 | 2008-08-30 00:55:07 -0400 | [diff] [blame] | 1326 | if (has_pitch) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1327 | { |
Jean-Marc Valin | c994394 | 2008-08-30 00:55:07 -0400 | [diff] [blame] | 1328 | /* Pitch MDCT */ |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1329 | compute_mdcts(st->mode, 0, st->out_mem+pitch_index*C, pitch_freq, C); |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1330 | } |
| 1331 | |
Jean-Marc Valin | 70c8ffd | 2007-12-07 14:20:01 +1100 | [diff] [blame] | 1332 | /* Decode fixed codebook and merge with pitch */ |
Jean-Marc Valin | ca53b7c | 2009-03-26 20:23:14 -0400 | [diff] [blame] | 1333 | if (C==1) |
Jean-Marc Valin | a9947c4 | 2009-10-08 23:10:45 -0400 | [diff] [blame] | 1334 | quant_bands(st->mode, X, bandE, pulses, shortBlocks, has_fold, len*8, 0, &dec); |
Jean-Marc Valin | 7353203 | 2009-06-02 20:07:25 -0400 | [diff] [blame] | 1335 | #ifndef DISABLE_STEREO |
Jean-Marc Valin | ca53b7c | 2009-03-26 20:23:14 -0400 | [diff] [blame] | 1336 | else |
Jean-Marc Valin | d5e5436 | 2009-09-30 20:50:41 -0400 | [diff] [blame] | 1337 | unquant_bands_stereo(st->mode, X, bandE, pulses, shortBlocks, has_fold, len*8, &dec); |
Jean-Marc Valin | 7353203 | 2009-06-02 20:07:25 -0400 | [diff] [blame] | 1338 | #endif |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1339 | unquant_energy_finalise(st->mode, bandE, st->oldBandE, fine_quant, fine_priority, len*8-ec_dec_tell(&dec, 0), &dec, C); |
Jean-Marc Valin | 3971053 | 2009-06-09 00:10:32 -0400 | [diff] [blame] | 1340 | |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1341 | if (mdct_weight_shift) |
| 1342 | { |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1343 | mdct_shape(st->mode, X, 0, mdct_weight_pos+1, N, st->mode->nbShortMdcts, mdct_weight_shift, C); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1344 | } |
| 1345 | |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 1346 | /* Synthesis */ |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1347 | denormalise_bands(st->mode, X, freq, bandE, C); |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 1348 | |
| 1349 | |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1350 | CELT_MOVE(st->decode_mem, st->decode_mem+C*N, C*(DECODE_BUFFER_SIZE+st->overlap-N)); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1351 | |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 1352 | if (has_pitch) |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1353 | apply_pitch(st->mode, freq, pitch_freq, gain_id, 0, C); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 1354 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1355 | /* Compute inverse MDCTs */ |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 1356 | compute_inv_mdcts(st->mode, shortBlocks, freq, transient_time, transient_shift, st->out_mem, C); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 1357 | |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 1358 | deemphasis(st->out_mem, pcm, N, C, preemph, st->preemph_memD); |
Jean-Marc Valin | 388f1d6 | 2008-01-18 16:28:11 +1100 | [diff] [blame] | 1359 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1360 | RESTORE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1361 | return 0; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1362 | } |
| 1363 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1364 | #ifdef FIXED_POINT |
| 1365 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | 6db6cbd | 2009-04-11 22:01:20 -0400 | [diff] [blame] | 1366 | int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1367 | { |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1368 | int j, ret, C, N; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1369 | VARDECL(celt_int16, out); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1370 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1371 | |
| 1372 | if (check_decoder(st) != CELT_OK) |
| 1373 | return CELT_INVALID_STATE; |
| 1374 | |
| 1375 | if (check_mode(st->mode) != CELT_OK) |
| 1376 | return CELT_INVALID_MODE; |
| 1377 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1378 | if (pcm==NULL) |
| 1379 | return CELT_BAD_ARG; |
| 1380 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1381 | C = CHANNELS(st->channels); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1382 | N = st->block_size; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1383 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1384 | ALLOC(out, C*N, celt_int16); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1385 | ret=celt_decode(st, data, len, out); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1386 | for (j=0;j<C*N;j++) |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1387 | pcm[j]=out[j]*(1/32768.); |
| 1388 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1389 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1390 | return ret; |
| 1391 | } |
| 1392 | #endif /*DISABLE_FLOAT_API*/ |
| 1393 | #else |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1394 | int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm) |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1395 | { |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1396 | int j, ret, C, N; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1397 | VARDECL(celt_sig_t, out); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1398 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1399 | |
| 1400 | if (check_decoder(st) != CELT_OK) |
| 1401 | return CELT_INVALID_STATE; |
| 1402 | |
| 1403 | if (check_mode(st->mode) != CELT_OK) |
| 1404 | return CELT_INVALID_MODE; |
| 1405 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1406 | if (pcm==NULL) |
| 1407 | return CELT_BAD_ARG; |
| 1408 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1409 | C = CHANNELS(st->channels); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1410 | N = st->block_size; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1411 | ALLOC(out, C*N, celt_sig_t); |
| 1412 | |
| 1413 | ret=celt_decode_float(st, data, len, out); |
| 1414 | |
| 1415 | for (j=0;j<C*N;j++) |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1416 | pcm[j] = FLOAT2INT16 (out[j]); |
| 1417 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1418 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1419 | return ret; |
| 1420 | } |
| 1421 | #endif |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1422 | |
| 1423 | int celt_decoder_ctl(CELTDecoder * restrict st, int request, ...) |
| 1424 | { |
| 1425 | va_list ap; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1426 | |
| 1427 | if (check_decoder(st) != CELT_OK) |
| 1428 | return CELT_INVALID_STATE; |
| 1429 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1430 | va_start(ap, request); |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1431 | if ((request!=CELT_GET_MODE_REQUEST) && (check_mode(st->mode) != CELT_OK)) |
| 1432 | goto bad_mode; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1433 | switch (request) |
| 1434 | { |
Gregory Maxwell | f3b44ef | 2009-06-03 13:37:45 -0400 | [diff] [blame] | 1435 | case CELT_GET_MODE_REQUEST: |
| 1436 | { |
| 1437 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 1438 | if (value==0) |
| 1439 | goto bad_arg; |
| 1440 | *value=st->mode; |
| 1441 | } |
| 1442 | break; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1443 | case CELT_RESET_STATE: |
| 1444 | { |
| 1445 | const CELTMode *mode = st->mode; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1446 | int C = st->channels; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1447 | |
| 1448 | CELT_MEMSET(st->decode_mem, 0, (DECODE_BUFFER_SIZE+st->overlap)*C); |
| 1449 | CELT_MEMSET(st->oldBandE, 0, C*mode->nbEBands); |
| 1450 | |
| 1451 | CELT_MEMSET(st->preemph_memD, 0, C); |
| 1452 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1453 | st->loss_count = 0; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1454 | } |
| 1455 | break; |
| 1456 | default: |
| 1457 | goto bad_request; |
| 1458 | } |
| 1459 | va_end(ap); |
| 1460 | return CELT_OK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1461 | bad_mode: |
| 1462 | va_end(ap); |
| 1463 | return CELT_INVALID_MODE; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1464 | bad_arg: |
| 1465 | va_end(ap); |
| 1466 | return CELT_BAD_ARG; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1467 | bad_request: |
| 1468 | va_end(ap); |
| 1469 | return CELT_UNIMPLEMENTED; |
| 1470 | } |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1471 | |
| 1472 | const char *celt_strerror(int error) |
| 1473 | { |
| 1474 | static const char *error_strings[8] = { |
| 1475 | "success", |
| 1476 | "invalid argument", |
| 1477 | "invalid mode", |
| 1478 | "internal error", |
| 1479 | "corrupted stream", |
| 1480 | "request not implemented", |
| 1481 | "invalid state", |
| 1482 | "memory allocation failed" |
| 1483 | }; |
| 1484 | if (error > 0 || error < -7) |
| 1485 | return "unknown error"; |
| 1486 | else |
| 1487 | return error_strings[-error]; |
| 1488 | } |
| 1489 | |