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