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