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