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